13f7d54a6SGarrett D'Amore /*
23f7d54a6SGarrett D'Amore * CDDL HEADER START
33f7d54a6SGarrett D'Amore *
43f7d54a6SGarrett D'Amore * The contents of this file are subject to the terms of the
53f7d54a6SGarrett D'Amore * Common Development and Distribution License (the "License").
63f7d54a6SGarrett D'Amore * You may not use this file except in compliance with the License.
73f7d54a6SGarrett D'Amore *
83f7d54a6SGarrett D'Amore * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93f7d54a6SGarrett D'Amore * or http://www.opensolaris.org/os/licensing.
103f7d54a6SGarrett D'Amore * See the License for the specific language governing permissions
113f7d54a6SGarrett D'Amore * and limitations under the License.
123f7d54a6SGarrett D'Amore *
133f7d54a6SGarrett D'Amore * When distributing Covered Code, include this CDDL HEADER in each
143f7d54a6SGarrett D'Amore * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153f7d54a6SGarrett D'Amore * If applicable, add the following below this CDDL HEADER, with the
163f7d54a6SGarrett D'Amore * fields enclosed by brackets "[]" replaced with your own identifying
173f7d54a6SGarrett D'Amore * information: Portions Copyright [yyyy] [name of copyright owner]
183f7d54a6SGarrett D'Amore *
193f7d54a6SGarrett D'Amore * CDDL HEADER END
203f7d54a6SGarrett D'Amore */
213f7d54a6SGarrett D'Amore /*
223f7d54a6SGarrett D'Amore * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23cd21e7c5SGarrett D'Amore * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
24679ac156SAlexey Zaytsev * Copyright 2012 Alexey Zaytsev <alexey.zaytsev@gmail.com> All rights reserved.
25c0591a0cSYouzhong Yang * Copyright 2017 The MathWorks, Inc. All rights reserved.
2633f84ecfSJason King * Copyright 2020 Joyent, Inc.
276f0e4dc9SAndy Fiddaman * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
28da00bec1SHans Rosenfeld * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
293fe80ca4SDan Cross * Copyright 2023 Oxide Computer Company
3086e3bca6SGarrett D'Amore */
3186e3bca6SGarrett D'Amore
323f7d54a6SGarrett D'Amore #include <sys/types.h>
333f7d54a6SGarrett D'Amore #include <sys/ksynch.h>
343f7d54a6SGarrett D'Amore #include <sys/kmem.h>
353f7d54a6SGarrett D'Amore #include <sys/file.h>
363f7d54a6SGarrett D'Amore #include <sys/errno.h>
373f7d54a6SGarrett D'Amore #include <sys/open.h>
383f7d54a6SGarrett D'Amore #include <sys/buf.h>
393f7d54a6SGarrett D'Amore #include <sys/uio.h>
403f7d54a6SGarrett D'Amore #include <sys/aio_req.h>
413f7d54a6SGarrett D'Amore #include <sys/cred.h>
423f7d54a6SGarrett D'Amore #include <sys/modctl.h>
433f7d54a6SGarrett D'Amore #include <sys/cmlb.h>
443f7d54a6SGarrett D'Amore #include <sys/conf.h>
453f7d54a6SGarrett D'Amore #include <sys/devops.h>
463f7d54a6SGarrett D'Amore #include <sys/list.h>
473f7d54a6SGarrett D'Amore #include <sys/sysmacros.h>
483f7d54a6SGarrett D'Amore #include <sys/dkio.h>
491a5ae140SJason King #include <sys/dkioc_free_util.h>
503f7d54a6SGarrett D'Amore #include <sys/vtoc.h>
513f7d54a6SGarrett D'Amore #include <sys/scsi/scsi.h> /* for DTYPE_DIRECT */
523f7d54a6SGarrett D'Amore #include <sys/kstat.h>
533f7d54a6SGarrett D'Amore #include <sys/fs/dv_node.h>
543f7d54a6SGarrett D'Amore #include <sys/ddi.h>
553f7d54a6SGarrett D'Amore #include <sys/sunddi.h>
563f7d54a6SGarrett D'Amore #include <sys/note.h>
573f7d54a6SGarrett D'Amore #include <sys/blkdev.h>
58510a6847SHans Rosenfeld #include <sys/scsi/impl/inquiry.h>
596f0e4dc9SAndy Fiddaman #include <sys/taskq.h>
606f0e4dc9SAndy Fiddaman #include <sys/taskq_impl.h>
616f0e4dc9SAndy Fiddaman #include <sys/disp.h>
626f0e4dc9SAndy Fiddaman #include <sys/sysevent/eventdefs.h>
636f0e4dc9SAndy Fiddaman #include <sys/sysevent/dev.h>
643f7d54a6SGarrett D'Amore
654d95620bSPaul Winder /*
664d95620bSPaul Winder * blkdev is a driver which provides a lot of the common functionality
674d95620bSPaul Winder * a block device driver may need and helps by removing code which
684d95620bSPaul Winder * is frequently duplicated in block device drivers.
694d95620bSPaul Winder *
704d95620bSPaul Winder * Within this driver all the struct cb_ops functions required for a
714d95620bSPaul Winder * block device driver are written with appropriate call back functions
724d95620bSPaul Winder * to be provided by the parent driver.
734d95620bSPaul Winder *
744d95620bSPaul Winder * To use blkdev, a driver needs to:
754d95620bSPaul Winder * 1. Create a bd_ops_t structure which has the call back operations
764d95620bSPaul Winder * blkdev will use.
774d95620bSPaul Winder * 2. Create a handle by calling bd_alloc_handle(). One of the
784d95620bSPaul Winder * arguments to this function is the bd_ops_t.
794d95620bSPaul Winder * 3. Call bd_attach_handle(). This will instantiate a blkdev device
804d95620bSPaul Winder * as a child device node of the calling driver.
814d95620bSPaul Winder *
824d95620bSPaul Winder * A parent driver is not restricted to just allocating and attaching a
834d95620bSPaul Winder * single instance, it may attach as many as it wishes. For each handle
844d95620bSPaul Winder * attached, appropriate entries in /dev/[r]dsk are created.
854d95620bSPaul Winder *
864d95620bSPaul Winder * The bd_ops_t routines that a parent of blkdev need to provide are:
874d95620bSPaul Winder *
884d95620bSPaul Winder * o_drive_info: Provide information to blkdev such as how many I/O queues
894d95620bSPaul Winder * to create and the size of those queues. Also some device
904d95620bSPaul Winder * specifics such as EUI, vendor, product, model, serial
914d95620bSPaul Winder * number ....
924d95620bSPaul Winder *
934d95620bSPaul Winder * o_media_info: Provide information about the media. Eg size and block size.
944d95620bSPaul Winder *
954d95620bSPaul Winder * o_devid_init: Creates and initializes the device id. Typically calls
964d95620bSPaul Winder * ddi_devid_init().
974d95620bSPaul Winder *
984d95620bSPaul Winder * o_sync_cache: Issues a device appropriate command to flush any write
994d95620bSPaul Winder * caches.
1004d95620bSPaul Winder *
1014d95620bSPaul Winder * o_read: Read data as described by bd_xfer_t argument.
1024d95620bSPaul Winder *
1034d95620bSPaul Winder * o_write: Write data as described by bd_xfer_t argument.
1044d95620bSPaul Winder *
1051a5ae140SJason King * o_free_space: Free the space described by bd_xfer_t argument (optional).
1064d95620bSPaul Winder *
1074d95620bSPaul Winder * Queues
1084d95620bSPaul Winder * ------
1094d95620bSPaul Winder * Part of the drive_info data is a queue count. blkdev will create
1104d95620bSPaul Winder * "queue count" number of waitq/runq pairs. Each waitq/runq pair
1114d95620bSPaul Winder * operates independently. As an I/O is scheduled up to the parent
1124d95620bSPaul Winder * driver via o_read or o_write its queue number is given. If the
1134d95620bSPaul Winder * parent driver supports multiple hardware queues it can then select
1144d95620bSPaul Winder * where to submit the I/O request.
1154d95620bSPaul Winder *
1164d95620bSPaul Winder * Currently blkdev uses a simplistic round-robin queue selection method.
1174d95620bSPaul Winder * It has the advantage that it is lockless. In the future it will be
1184d95620bSPaul Winder * worthwhile reviewing this strategy for something which prioritizes queues
1194d95620bSPaul Winder * depending on how busy they are.
1204d95620bSPaul Winder *
1214d95620bSPaul Winder * Each waitq/runq pair is protected by its mutex (q_iomutex). Incoming
1224d95620bSPaul Winder * I/O requests are initially added to the waitq. They are taken off the
1234d95620bSPaul Winder * waitq, added to the runq and submitted, providing the runq is less
1244d95620bSPaul Winder * than the qsize as specified in the drive_info. As an I/O request
1254d95620bSPaul Winder * completes, the parent driver is required to call bd_xfer_done(), which
1264d95620bSPaul Winder * will remove the I/O request from the runq and pass I/O completion
1274d95620bSPaul Winder * status up the stack.
1284d95620bSPaul Winder *
1294d95620bSPaul Winder * Locks
1304d95620bSPaul Winder * -----
1316f0e4dc9SAndy Fiddaman * There are 5 instance global locks d_ocmutex, d_ksmutex, d_errmutex,
1326f0e4dc9SAndy Fiddaman * d_statemutex and d_dle_mutex. As well a q_iomutex per waitq/runq pair.
1334d95620bSPaul Winder *
13419687f06SPaul Winder * Lock Hierarchy
13519687f06SPaul Winder * --------------
13619687f06SPaul Winder * The only two locks which may be held simultaneously are q_iomutex and
13719687f06SPaul Winder * d_ksmutex. In all cases q_iomutex must be acquired before d_ksmutex.
1384d95620bSPaul Winder */
1394d95620bSPaul Winder
1403f7d54a6SGarrett D'Amore #define BD_MAXPART 64
1413f7d54a6SGarrett D'Amore #define BDINST(dev) (getminor(dev) / BD_MAXPART)
1423f7d54a6SGarrett D'Amore #define BDPART(dev) (getminor(dev) % BD_MAXPART)
1433f7d54a6SGarrett D'Amore
1443f7d54a6SGarrett D'Amore typedef struct bd bd_t;
1453f7d54a6SGarrett D'Amore typedef struct bd_xfer_impl bd_xfer_impl_t;
1464d95620bSPaul Winder typedef struct bd_queue bd_queue_t;
1473f7d54a6SGarrett D'Amore
1486f0e4dc9SAndy Fiddaman typedef enum {
1496f0e4dc9SAndy Fiddaman BD_DLE_PENDING = 1 << 0,
1506f0e4dc9SAndy Fiddaman BD_DLE_RUNNING = 1 << 1
1516f0e4dc9SAndy Fiddaman } bd_dle_state_t;
1526f0e4dc9SAndy Fiddaman
1533f7d54a6SGarrett D'Amore struct bd {
1543f7d54a6SGarrett D'Amore void *d_private;
1553f7d54a6SGarrett D'Amore dev_info_t *d_dip;
1566f0e4dc9SAndy Fiddaman kmutex_t d_ocmutex; /* open/close */
1576f0e4dc9SAndy Fiddaman kmutex_t d_ksmutex; /* kstat */
1584d95620bSPaul Winder kmutex_t d_errmutex;
1593f7d54a6SGarrett D'Amore kmutex_t d_statemutex;
1603f7d54a6SGarrett D'Amore kcondvar_t d_statecv;
1613f7d54a6SGarrett D'Amore enum dkio_state d_state;
1623f7d54a6SGarrett D'Amore cmlb_handle_t d_cmlbh;
1633f7d54a6SGarrett D'Amore unsigned d_open_lyr[BD_MAXPART]; /* open count */
1643f7d54a6SGarrett D'Amore uint64_t d_open_excl; /* bit mask indexed by partition */
1653f7d54a6SGarrett D'Amore uint64_t d_open_reg[OTYPCNT]; /* bit mask */
1664d95620bSPaul Winder uint64_t d_io_counter;
1673f7d54a6SGarrett D'Amore
1684d95620bSPaul Winder uint32_t d_qcount;
1693f7d54a6SGarrett D'Amore uint32_t d_qactive;
1703f7d54a6SGarrett D'Amore uint32_t d_maxxfer;
1713f7d54a6SGarrett D'Amore uint32_t d_blkshift;
17232ce6b81SHans Rosenfeld uint32_t d_pblkshift;
1733f7d54a6SGarrett D'Amore uint64_t d_numblks;
1743f7d54a6SGarrett D'Amore ddi_devid_t d_devid;
1753f7d54a6SGarrett D'Amore
1761a5ae140SJason King uint64_t d_max_free_seg;
1771a5ae140SJason King uint64_t d_max_free_blks;
1781a5ae140SJason King uint64_t d_max_free_seg_blks;
1791a5ae140SJason King uint64_t d_free_align;
1801a5ae140SJason King
1813f7d54a6SGarrett D'Amore kmem_cache_t *d_cache;
1824d95620bSPaul Winder bd_queue_t *d_queues;
1833f7d54a6SGarrett D'Amore kstat_t *d_ksp;
1843f7d54a6SGarrett D'Amore kstat_io_t *d_kiop;
185bef9e21aSHans Rosenfeld kstat_t *d_errstats;
186bef9e21aSHans Rosenfeld struct bd_errstats *d_kerr;
1873f7d54a6SGarrett D'Amore
1883f7d54a6SGarrett D'Amore boolean_t d_rdonly;
18959d8f100SGarrett D'Amore boolean_t d_ssd;
1903f7d54a6SGarrett D'Amore boolean_t d_removable;
1913f7d54a6SGarrett D'Amore boolean_t d_hotpluggable;
1923f7d54a6SGarrett D'Amore boolean_t d_use_dma;
1933f7d54a6SGarrett D'Amore
1943f7d54a6SGarrett D'Amore ddi_dma_attr_t d_dma;
1953f7d54a6SGarrett D'Amore bd_ops_t d_ops;
1963f7d54a6SGarrett D'Amore bd_handle_t d_handle;
1976f0e4dc9SAndy Fiddaman
1986f0e4dc9SAndy Fiddaman kmutex_t d_dle_mutex;
1996f0e4dc9SAndy Fiddaman taskq_ent_t d_dle_ent;
2006f0e4dc9SAndy Fiddaman bd_dle_state_t d_dle_state;
2013f7d54a6SGarrett D'Amore };
2023f7d54a6SGarrett D'Amore
2033f7d54a6SGarrett D'Amore struct bd_handle {
2043f7d54a6SGarrett D'Amore bd_ops_t h_ops;
2053f7d54a6SGarrett D'Amore ddi_dma_attr_t *h_dma;
2063f7d54a6SGarrett D'Amore dev_info_t *h_parent;
2073f7d54a6SGarrett D'Amore dev_info_t *h_child;
2083f7d54a6SGarrett D'Amore void *h_private;
2093f7d54a6SGarrett D'Amore bd_t *h_bd;
2103f7d54a6SGarrett D'Amore char *h_name;
211ca0df52aSHans Rosenfeld char h_addr[50]; /* enough for w%0.32x,%X */
2123f7d54a6SGarrett D'Amore };
2133f7d54a6SGarrett D'Amore
2143f7d54a6SGarrett D'Amore struct bd_xfer_impl {
2153f7d54a6SGarrett D'Amore bd_xfer_t i_public;
2163f7d54a6SGarrett D'Amore list_node_t i_linkage;
2173f7d54a6SGarrett D'Amore bd_t *i_bd;
2183f7d54a6SGarrett D'Amore buf_t *i_bp;
2194d95620bSPaul Winder bd_queue_t *i_bq;
2203f7d54a6SGarrett D'Amore uint_t i_num_win;
2213f7d54a6SGarrett D'Amore uint_t i_cur_win;
2223f7d54a6SGarrett D'Amore off_t i_offset;
2233f7d54a6SGarrett D'Amore int (*i_func)(void *, bd_xfer_t *);
2243f7d54a6SGarrett D'Amore uint32_t i_blkshift;
2253f7d54a6SGarrett D'Amore size_t i_len;
2263f7d54a6SGarrett D'Amore size_t i_resid;
2273f7d54a6SGarrett D'Amore };
2283f7d54a6SGarrett D'Amore
2294d95620bSPaul Winder struct bd_queue {
2304d95620bSPaul Winder kmutex_t q_iomutex;
2314d95620bSPaul Winder uint32_t q_qsize;
2324d95620bSPaul Winder uint32_t q_qactive;
2334d95620bSPaul Winder list_t q_runq;
2344d95620bSPaul Winder list_t q_waitq;
2354d95620bSPaul Winder };
2364d95620bSPaul Winder
2373f7d54a6SGarrett D'Amore #define i_dmah i_public.x_dmah
2383f7d54a6SGarrett D'Amore #define i_dmac i_public.x_dmac
2393f7d54a6SGarrett D'Amore #define i_ndmac i_public.x_ndmac
2403f7d54a6SGarrett D'Amore #define i_kaddr i_public.x_kaddr
2413f7d54a6SGarrett D'Amore #define i_nblks i_public.x_nblks
2423f7d54a6SGarrett D'Amore #define i_blkno i_public.x_blkno
24386e3bca6SGarrett D'Amore #define i_flags i_public.x_flags
2444d95620bSPaul Winder #define i_qnum i_public.x_qnum
2451a5ae140SJason King #define i_dfl i_public.x_dfl
2463f7d54a6SGarrett D'Amore
2471a5ae140SJason King #define CAN_FREESPACE(bd) \
2481a5ae140SJason King (((bd)->d_ops.o_free_space == NULL) ? B_FALSE : B_TRUE)
2493f7d54a6SGarrett D'Amore
2503f7d54a6SGarrett D'Amore /*
2513f7d54a6SGarrett D'Amore * Private prototypes.
2523f7d54a6SGarrett D'Amore */
2533f7d54a6SGarrett D'Amore
254510a6847SHans Rosenfeld static void bd_prop_update_inqstring(dev_info_t *, char *, char *, size_t);
255510a6847SHans Rosenfeld static void bd_create_inquiry_props(dev_info_t *, bd_drive_t *);
256bef9e21aSHans Rosenfeld static void bd_create_errstats(bd_t *, int, bd_drive_t *);
25733f84ecfSJason King static void bd_destroy_errstats(bd_t *);
258bef9e21aSHans Rosenfeld static void bd_errstats_setstr(kstat_named_t *, char *, size_t, char *);
259bef9e21aSHans Rosenfeld static void bd_init_errstats(bd_t *, bd_drive_t *);
26033f84ecfSJason King static void bd_fini_errstats(bd_t *);
261510a6847SHans Rosenfeld
2623f7d54a6SGarrett D'Amore static int bd_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
2633f7d54a6SGarrett D'Amore static int bd_attach(dev_info_t *, ddi_attach_cmd_t);
2643f7d54a6SGarrett D'Amore static int bd_detach(dev_info_t *, ddi_detach_cmd_t);
2653f7d54a6SGarrett D'Amore
2663f7d54a6SGarrett D'Amore static int bd_open(dev_t *, int, int, cred_t *);
2673f7d54a6SGarrett D'Amore static int bd_close(dev_t, int, int, cred_t *);
2683f7d54a6SGarrett D'Amore static int bd_strategy(struct buf *);
2693f7d54a6SGarrett D'Amore static int bd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
27086e3bca6SGarrett D'Amore static int bd_dump(dev_t, caddr_t, daddr_t, int);
2713f7d54a6SGarrett D'Amore static int bd_read(dev_t, struct uio *, cred_t *);
2723f7d54a6SGarrett D'Amore static int bd_write(dev_t, struct uio *, cred_t *);
2733f7d54a6SGarrett D'Amore static int bd_aread(dev_t, struct aio_req *, cred_t *);
2743f7d54a6SGarrett D'Amore static int bd_awrite(dev_t, struct aio_req *, cred_t *);
2753f7d54a6SGarrett D'Amore static int bd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *,
2763f7d54a6SGarrett D'Amore caddr_t, int *);
2773f7d54a6SGarrett D'Amore
2783f7d54a6SGarrett D'Amore static int bd_tg_rdwr(dev_info_t *, uchar_t, void *, diskaddr_t, size_t,
2793f7d54a6SGarrett D'Amore void *);
2803f7d54a6SGarrett D'Amore static int bd_tg_getinfo(dev_info_t *, int, void *, void *);
2813f7d54a6SGarrett D'Amore static int bd_xfer_ctor(void *, void *, int);
2823f7d54a6SGarrett D'Amore static void bd_xfer_dtor(void *, void *);
2834d95620bSPaul Winder static void bd_sched(bd_t *, bd_queue_t *);
2843f7d54a6SGarrett D'Amore static void bd_submit(bd_t *, bd_xfer_impl_t *);
2853f7d54a6SGarrett D'Amore static void bd_runq_exit(bd_xfer_impl_t *, int);
2863f7d54a6SGarrett D'Amore static void bd_update_state(bd_t *);
2873f7d54a6SGarrett D'Amore static int bd_check_state(bd_t *, enum dkio_state *);
2883f7d54a6SGarrett D'Amore static int bd_flush_write_cache(bd_t *, struct dk_callback *);
289c0591a0cSYouzhong Yang static int bd_check_uio(dev_t, struct uio *);
2901a5ae140SJason King static int bd_free_space(dev_t, bd_t *, dkioc_free_list_t *);
2913f7d54a6SGarrett D'Amore
2923f7d54a6SGarrett D'Amore struct cmlb_tg_ops bd_tg_ops = {
2933f7d54a6SGarrett D'Amore TG_DK_OPS_VERSION_1,
2943f7d54a6SGarrett D'Amore bd_tg_rdwr,
2953f7d54a6SGarrett D'Amore bd_tg_getinfo,
2963f7d54a6SGarrett D'Amore };
2973f7d54a6SGarrett D'Amore
2983f7d54a6SGarrett D'Amore static struct cb_ops bd_cb_ops = {
2994d95620bSPaul Winder bd_open, /* open */
3004d95620bSPaul Winder bd_close, /* close */
3014d95620bSPaul Winder bd_strategy, /* strategy */
3024d95620bSPaul Winder nodev, /* print */
30386e3bca6SGarrett D'Amore bd_dump, /* dump */
3044d95620bSPaul Winder bd_read, /* read */
3054d95620bSPaul Winder bd_write, /* write */
3064d95620bSPaul Winder bd_ioctl, /* ioctl */
3074d95620bSPaul Winder nodev, /* devmap */
3084d95620bSPaul Winder nodev, /* mmap */
3094d95620bSPaul Winder nodev, /* segmap */
3104d95620bSPaul Winder nochpoll, /* poll */
3114d95620bSPaul Winder bd_prop_op, /* cb_prop_op */
3124d95620bSPaul Winder 0, /* streamtab */
3133f7d54a6SGarrett D'Amore D_64BIT | D_MP, /* Driver comaptibility flag */
3143f7d54a6SGarrett D'Amore CB_REV, /* cb_rev */
3153f7d54a6SGarrett D'Amore bd_aread, /* async read */
3163f7d54a6SGarrett D'Amore bd_awrite /* async write */
3173f7d54a6SGarrett D'Amore };
3183f7d54a6SGarrett D'Amore
3193f7d54a6SGarrett D'Amore struct dev_ops bd_dev_ops = {
3204d95620bSPaul Winder DEVO_REV, /* devo_rev, */
3214d95620bSPaul Winder 0, /* refcnt */
3223f7d54a6SGarrett D'Amore bd_getinfo, /* getinfo */
3234d95620bSPaul Winder nulldev, /* identify */
3244d95620bSPaul Winder nulldev, /* probe */
3254d95620bSPaul Winder bd_attach, /* attach */
3263f7d54a6SGarrett D'Amore bd_detach, /* detach */
3274d95620bSPaul Winder nodev, /* reset */
3284d95620bSPaul Winder &bd_cb_ops, /* driver operations */
3293f7d54a6SGarrett D'Amore NULL, /* bus operations */
3303f7d54a6SGarrett D'Amore NULL, /* power */
3313f7d54a6SGarrett D'Amore ddi_quiesce_not_needed, /* quiesce */
3323f7d54a6SGarrett D'Amore };
3333f7d54a6SGarrett D'Amore
3343f7d54a6SGarrett D'Amore static struct modldrv modldrv = {
3353f7d54a6SGarrett D'Amore &mod_driverops,
3363f7d54a6SGarrett D'Amore "Generic Block Device",
3373f7d54a6SGarrett D'Amore &bd_dev_ops,
3383f7d54a6SGarrett D'Amore };
3393f7d54a6SGarrett D'Amore
3403f7d54a6SGarrett D'Amore static struct modlinkage modlinkage = {
3413f7d54a6SGarrett D'Amore MODREV_1, { &modldrv, NULL }
3423f7d54a6SGarrett D'Amore };
3433f7d54a6SGarrett D'Amore
3443f7d54a6SGarrett D'Amore static void *bd_state;
3453f7d54a6SGarrett D'Amore static krwlock_t bd_lock;
3466f0e4dc9SAndy Fiddaman static taskq_t *bd_taskq;
3473f7d54a6SGarrett D'Amore
3483f7d54a6SGarrett D'Amore int
_init(void)3493f7d54a6SGarrett D'Amore _init(void)
3503f7d54a6SGarrett D'Amore {
3516f0e4dc9SAndy Fiddaman char taskq_name[TASKQ_NAMELEN];
3526f0e4dc9SAndy Fiddaman const char *name;
3536f0e4dc9SAndy Fiddaman int rv;
3543f7d54a6SGarrett D'Amore
3553f7d54a6SGarrett D'Amore rv = ddi_soft_state_init(&bd_state, sizeof (struct bd), 2);
3566f0e4dc9SAndy Fiddaman if (rv != DDI_SUCCESS)
3573f7d54a6SGarrett D'Amore return (rv);
3586f0e4dc9SAndy Fiddaman
3596f0e4dc9SAndy Fiddaman name = mod_modname(&modlinkage);
3606f0e4dc9SAndy Fiddaman (void) snprintf(taskq_name, sizeof (taskq_name), "%s_taskq", name);
3616f0e4dc9SAndy Fiddaman bd_taskq = taskq_create(taskq_name, 1, minclsyspri, 0, 0, 0);
3626f0e4dc9SAndy Fiddaman if (bd_taskq == NULL) {
3636f0e4dc9SAndy Fiddaman cmn_err(CE_WARN, "%s: unable to create %s", name, taskq_name);
3646f0e4dc9SAndy Fiddaman ddi_soft_state_fini(&bd_state);
3656f0e4dc9SAndy Fiddaman return (DDI_FAILURE);
3663f7d54a6SGarrett D'Amore }
3676f0e4dc9SAndy Fiddaman
3683f7d54a6SGarrett D'Amore rw_init(&bd_lock, NULL, RW_DRIVER, NULL);
3696f0e4dc9SAndy Fiddaman
3703f7d54a6SGarrett D'Amore rv = mod_install(&modlinkage);
3713f7d54a6SGarrett D'Amore if (rv != DDI_SUCCESS) {
3723f7d54a6SGarrett D'Amore rw_destroy(&bd_lock);
3736f0e4dc9SAndy Fiddaman taskq_destroy(bd_taskq);
3743f7d54a6SGarrett D'Amore ddi_soft_state_fini(&bd_state);
3753f7d54a6SGarrett D'Amore }
3763f7d54a6SGarrett D'Amore return (rv);
3773f7d54a6SGarrett D'Amore }
3783f7d54a6SGarrett D'Amore
3793f7d54a6SGarrett D'Amore int
_fini(void)3803f7d54a6SGarrett D'Amore _fini(void)
3813f7d54a6SGarrett D'Amore {
3823f7d54a6SGarrett D'Amore int rv;
3833f7d54a6SGarrett D'Amore
3843f7d54a6SGarrett D'Amore rv = mod_remove(&modlinkage);
3853f7d54a6SGarrett D'Amore if (rv == DDI_SUCCESS) {
3863f7d54a6SGarrett D'Amore rw_destroy(&bd_lock);
3876f0e4dc9SAndy Fiddaman taskq_destroy(bd_taskq);
3883f7d54a6SGarrett D'Amore ddi_soft_state_fini(&bd_state);
3893f7d54a6SGarrett D'Amore }
3903f7d54a6SGarrett D'Amore return (rv);
3913f7d54a6SGarrett D'Amore }
3923f7d54a6SGarrett D'Amore
3933f7d54a6SGarrett D'Amore int
_info(struct modinfo * modinfop)3943f7d54a6SGarrett D'Amore _info(struct modinfo *modinfop)
3953f7d54a6SGarrett D'Amore {
3963f7d54a6SGarrett D'Amore return (mod_info(&modlinkage, modinfop));
3973f7d54a6SGarrett D'Amore }
3983f7d54a6SGarrett D'Amore
3993f7d54a6SGarrett D'Amore static int
bd_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** resultp)4003f7d54a6SGarrett D'Amore bd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
4013f7d54a6SGarrett D'Amore {
4023f7d54a6SGarrett D'Amore bd_t *bd;
4033f7d54a6SGarrett D'Amore minor_t inst;
4043f7d54a6SGarrett D'Amore
4053f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(dip));
4063f7d54a6SGarrett D'Amore
4073f7d54a6SGarrett D'Amore inst = BDINST((dev_t)arg);
4083f7d54a6SGarrett D'Amore
4093f7d54a6SGarrett D'Amore switch (cmd) {
4103f7d54a6SGarrett D'Amore case DDI_INFO_DEVT2DEVINFO:
4113f7d54a6SGarrett D'Amore bd = ddi_get_soft_state(bd_state, inst);
4123f7d54a6SGarrett D'Amore if (bd == NULL) {
4133f7d54a6SGarrett D'Amore return (DDI_FAILURE);
4143f7d54a6SGarrett D'Amore }
4153f7d54a6SGarrett D'Amore *resultp = (void *)bd->d_dip;
4163f7d54a6SGarrett D'Amore break;
4173f7d54a6SGarrett D'Amore
4183f7d54a6SGarrett D'Amore case DDI_INFO_DEVT2INSTANCE:
4193f7d54a6SGarrett D'Amore *resultp = (void *)(intptr_t)inst;
4203f7d54a6SGarrett D'Amore break;
4213f7d54a6SGarrett D'Amore
4223f7d54a6SGarrett D'Amore default:
4233f7d54a6SGarrett D'Amore return (DDI_FAILURE);
4243f7d54a6SGarrett D'Amore }
4253f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
4263f7d54a6SGarrett D'Amore }
4273f7d54a6SGarrett D'Amore
428510a6847SHans Rosenfeld static void
bd_prop_update_inqstring(dev_info_t * dip,char * name,char * data,size_t len)429510a6847SHans Rosenfeld bd_prop_update_inqstring(dev_info_t *dip, char *name, char *data, size_t len)
430510a6847SHans Rosenfeld {
431510a6847SHans Rosenfeld int ilen;
432510a6847SHans Rosenfeld char *data_string;
433510a6847SHans Rosenfeld
434510a6847SHans Rosenfeld ilen = scsi_ascii_inquiry_len(data, len);
435510a6847SHans Rosenfeld ASSERT3U(ilen, <=, len);
436510a6847SHans Rosenfeld if (ilen <= 0)
437510a6847SHans Rosenfeld return;
438510a6847SHans Rosenfeld /* ensure null termination */
439510a6847SHans Rosenfeld data_string = kmem_zalloc(ilen + 1, KM_SLEEP);
440510a6847SHans Rosenfeld bcopy(data, data_string, ilen);
441510a6847SHans Rosenfeld (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, name, data_string);
442510a6847SHans Rosenfeld kmem_free(data_string, ilen + 1);
443510a6847SHans Rosenfeld }
444510a6847SHans Rosenfeld
445510a6847SHans Rosenfeld static void
bd_create_inquiry_props(dev_info_t * dip,bd_drive_t * drive)446510a6847SHans Rosenfeld bd_create_inquiry_props(dev_info_t *dip, bd_drive_t *drive)
447510a6847SHans Rosenfeld {
448510a6847SHans Rosenfeld if (drive->d_vendor_len > 0)
449510a6847SHans Rosenfeld bd_prop_update_inqstring(dip, INQUIRY_VENDOR_ID,
450510a6847SHans Rosenfeld drive->d_vendor, drive->d_vendor_len);
451510a6847SHans Rosenfeld
452510a6847SHans Rosenfeld if (drive->d_product_len > 0)
453510a6847SHans Rosenfeld bd_prop_update_inqstring(dip, INQUIRY_PRODUCT_ID,
454510a6847SHans Rosenfeld drive->d_product, drive->d_product_len);
455510a6847SHans Rosenfeld
456510a6847SHans Rosenfeld if (drive->d_serial_len > 0)
457510a6847SHans Rosenfeld bd_prop_update_inqstring(dip, INQUIRY_SERIAL_NO,
458510a6847SHans Rosenfeld drive->d_serial, drive->d_serial_len);
459510a6847SHans Rosenfeld
460510a6847SHans Rosenfeld if (drive->d_revision_len > 0)
461510a6847SHans Rosenfeld bd_prop_update_inqstring(dip, INQUIRY_REVISION_ID,
462510a6847SHans Rosenfeld drive->d_revision, drive->d_revision_len);
463510a6847SHans Rosenfeld }
464510a6847SHans Rosenfeld
465bef9e21aSHans Rosenfeld static void
bd_create_errstats(bd_t * bd,int inst,bd_drive_t * drive)466bef9e21aSHans Rosenfeld bd_create_errstats(bd_t *bd, int inst, bd_drive_t *drive)
467bef9e21aSHans Rosenfeld {
468bef9e21aSHans Rosenfeld char ks_module[KSTAT_STRLEN];
469bef9e21aSHans Rosenfeld char ks_name[KSTAT_STRLEN];
470bef9e21aSHans Rosenfeld int ndata = sizeof (struct bd_errstats) / sizeof (kstat_named_t);
471bef9e21aSHans Rosenfeld
472bef9e21aSHans Rosenfeld if (bd->d_errstats != NULL)
473bef9e21aSHans Rosenfeld return;
474bef9e21aSHans Rosenfeld
475bef9e21aSHans Rosenfeld (void) snprintf(ks_module, sizeof (ks_module), "%serr",
476bef9e21aSHans Rosenfeld ddi_driver_name(bd->d_dip));
477bef9e21aSHans Rosenfeld (void) snprintf(ks_name, sizeof (ks_name), "%s%d,err",
478bef9e21aSHans Rosenfeld ddi_driver_name(bd->d_dip), inst);
479bef9e21aSHans Rosenfeld
480bef9e21aSHans Rosenfeld bd->d_errstats = kstat_create(ks_module, inst, ks_name, "device_error",
481bef9e21aSHans Rosenfeld KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
482bef9e21aSHans Rosenfeld
4834d95620bSPaul Winder mutex_init(&bd->d_errmutex, NULL, MUTEX_DRIVER, NULL);
484bef9e21aSHans Rosenfeld if (bd->d_errstats == NULL) {
485bef9e21aSHans Rosenfeld /*
486bef9e21aSHans Rosenfeld * Even if we cannot create the kstat, we create a
487bef9e21aSHans Rosenfeld * scratch kstat. The reason for this is to ensure
488bef9e21aSHans Rosenfeld * that we can update the kstat all of the time,
489bef9e21aSHans Rosenfeld * without adding an extra branch instruction.
490bef9e21aSHans Rosenfeld */
491bef9e21aSHans Rosenfeld bd->d_kerr = kmem_zalloc(sizeof (struct bd_errstats),
492bef9e21aSHans Rosenfeld KM_SLEEP);
493bef9e21aSHans Rosenfeld } else {
4944d95620bSPaul Winder bd->d_errstats->ks_lock = &bd->d_errmutex;
495bef9e21aSHans Rosenfeld bd->d_kerr = (struct bd_errstats *)bd->d_errstats->ks_data;
496bef9e21aSHans Rosenfeld }
497bef9e21aSHans Rosenfeld
498bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_softerrs, "Soft Errors",
499bef9e21aSHans Rosenfeld KSTAT_DATA_UINT32);
500bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_harderrs, "Hard Errors",
501bef9e21aSHans Rosenfeld KSTAT_DATA_UINT32);
502bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_transerrs, "Transport Errors",
503bef9e21aSHans Rosenfeld KSTAT_DATA_UINT32);
504bef9e21aSHans Rosenfeld
505bef9e21aSHans Rosenfeld if (drive->d_model_len > 0) {
506bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_model, "Model",
507bef9e21aSHans Rosenfeld KSTAT_DATA_STRING);
508bef9e21aSHans Rosenfeld } else {
509bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_vid, "Vendor",
510bef9e21aSHans Rosenfeld KSTAT_DATA_STRING);
511bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_pid, "Product",
512bef9e21aSHans Rosenfeld KSTAT_DATA_STRING);
513bef9e21aSHans Rosenfeld }
514bef9e21aSHans Rosenfeld
515bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_revision, "Revision",
516bef9e21aSHans Rosenfeld KSTAT_DATA_STRING);
517bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_serial, "Serial No",
518bef9e21aSHans Rosenfeld KSTAT_DATA_STRING);
519bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_capacity, "Size",
520bef9e21aSHans Rosenfeld KSTAT_DATA_ULONGLONG);
521bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_rq_media_err, "Media Error",
522bef9e21aSHans Rosenfeld KSTAT_DATA_UINT32);
523bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_rq_ntrdy_err, "Device Not Ready",
524bef9e21aSHans Rosenfeld KSTAT_DATA_UINT32);
525bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_rq_nodev_err, "No Device",
526bef9e21aSHans Rosenfeld KSTAT_DATA_UINT32);
527bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_rq_recov_err, "Recoverable",
528bef9e21aSHans Rosenfeld KSTAT_DATA_UINT32);
529bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_rq_illrq_err, "Illegal Request",
530bef9e21aSHans Rosenfeld KSTAT_DATA_UINT32);
531bef9e21aSHans Rosenfeld kstat_named_init(&bd->d_kerr->bd_rq_pfa_err,
532bef9e21aSHans Rosenfeld "Predictive Failure Analysis", KSTAT_DATA_UINT32);
533bef9e21aSHans Rosenfeld
534bef9e21aSHans Rosenfeld bd->d_errstats->ks_private = bd;
535bef9e21aSHans Rosenfeld
536bef9e21aSHans Rosenfeld kstat_install(bd->d_errstats);
53733f84ecfSJason King bd_init_errstats(bd, drive);
53833f84ecfSJason King }
53933f84ecfSJason King
54033f84ecfSJason King static void
bd_destroy_errstats(bd_t * bd)54133f84ecfSJason King bd_destroy_errstats(bd_t *bd)
54233f84ecfSJason King {
54333f84ecfSJason King if (bd->d_errstats != NULL) {
54433f84ecfSJason King bd_fini_errstats(bd);
54533f84ecfSJason King kstat_delete(bd->d_errstats);
54633f84ecfSJason King bd->d_errstats = NULL;
54733f84ecfSJason King } else {
54833f84ecfSJason King kmem_free(bd->d_kerr, sizeof (struct bd_errstats));
54933f84ecfSJason King bd->d_kerr = NULL;
55033f84ecfSJason King mutex_destroy(&bd->d_errmutex);
55133f84ecfSJason King }
552bef9e21aSHans Rosenfeld }
553bef9e21aSHans Rosenfeld
554bef9e21aSHans Rosenfeld static void
bd_errstats_setstr(kstat_named_t * k,char * str,size_t len,char * alt)555bef9e21aSHans Rosenfeld bd_errstats_setstr(kstat_named_t *k, char *str, size_t len, char *alt)
556bef9e21aSHans Rosenfeld {
557bef9e21aSHans Rosenfeld char *tmp;
558247e9a8eSPaul Winder size_t km_len;
559bef9e21aSHans Rosenfeld
560bef9e21aSHans Rosenfeld if (KSTAT_NAMED_STR_PTR(k) == NULL) {
561247e9a8eSPaul Winder if (len > 0)
562247e9a8eSPaul Winder km_len = strnlen(str, len);
563247e9a8eSPaul Winder else if (alt != NULL)
564247e9a8eSPaul Winder km_len = strlen(alt);
565247e9a8eSPaul Winder else
566247e9a8eSPaul Winder return;
567247e9a8eSPaul Winder
568247e9a8eSPaul Winder tmp = kmem_alloc(km_len + 1, KM_SLEEP);
569247e9a8eSPaul Winder bcopy(len > 0 ? str : alt, tmp, km_len);
570247e9a8eSPaul Winder tmp[km_len] = '\0';
571bef9e21aSHans Rosenfeld
572bef9e21aSHans Rosenfeld kstat_named_setstr(k, tmp);
573bef9e21aSHans Rosenfeld }
574bef9e21aSHans Rosenfeld }
575bef9e21aSHans Rosenfeld
576247e9a8eSPaul Winder static void
bd_errstats_clrstr(kstat_named_t * k)577247e9a8eSPaul Winder bd_errstats_clrstr(kstat_named_t *k)
578247e9a8eSPaul Winder {
579247e9a8eSPaul Winder if (KSTAT_NAMED_STR_PTR(k) == NULL)
580247e9a8eSPaul Winder return;
581247e9a8eSPaul Winder
582247e9a8eSPaul Winder kmem_free(KSTAT_NAMED_STR_PTR(k), KSTAT_NAMED_STR_BUFLEN(k));
583247e9a8eSPaul Winder kstat_named_setstr(k, NULL);
584247e9a8eSPaul Winder }
585247e9a8eSPaul Winder
586bef9e21aSHans Rosenfeld static void
bd_init_errstats(bd_t * bd,bd_drive_t * drive)587bef9e21aSHans Rosenfeld bd_init_errstats(bd_t *bd, bd_drive_t *drive)
588bef9e21aSHans Rosenfeld {
589bef9e21aSHans Rosenfeld struct bd_errstats *est = bd->d_kerr;
590bef9e21aSHans Rosenfeld
5914d95620bSPaul Winder mutex_enter(&bd->d_errmutex);
592bef9e21aSHans Rosenfeld
593bef9e21aSHans Rosenfeld if (drive->d_model_len > 0 &&
594bef9e21aSHans Rosenfeld KSTAT_NAMED_STR_PTR(&est->bd_model) == NULL) {
595bef9e21aSHans Rosenfeld bd_errstats_setstr(&est->bd_model, drive->d_model,
596bef9e21aSHans Rosenfeld drive->d_model_len, NULL);
597bef9e21aSHans Rosenfeld } else {
598bef9e21aSHans Rosenfeld bd_errstats_setstr(&est->bd_vid, drive->d_vendor,
599bef9e21aSHans Rosenfeld drive->d_vendor_len, "Unknown ");
600bef9e21aSHans Rosenfeld bd_errstats_setstr(&est->bd_pid, drive->d_product,
601bef9e21aSHans Rosenfeld drive->d_product_len, "Unknown ");
602bef9e21aSHans Rosenfeld }
603bef9e21aSHans Rosenfeld
604bef9e21aSHans Rosenfeld bd_errstats_setstr(&est->bd_revision, drive->d_revision,
605bef9e21aSHans Rosenfeld drive->d_revision_len, "0001");
606bef9e21aSHans Rosenfeld bd_errstats_setstr(&est->bd_serial, drive->d_serial,
607bef9e21aSHans Rosenfeld drive->d_serial_len, "0 ");
608bef9e21aSHans Rosenfeld
6094d95620bSPaul Winder mutex_exit(&bd->d_errmutex);
6104d95620bSPaul Winder }
6114d95620bSPaul Winder
612247e9a8eSPaul Winder static void
bd_fini_errstats(bd_t * bd)613247e9a8eSPaul Winder bd_fini_errstats(bd_t *bd)
614247e9a8eSPaul Winder {
615247e9a8eSPaul Winder struct bd_errstats *est = bd->d_kerr;
616247e9a8eSPaul Winder
617247e9a8eSPaul Winder mutex_enter(&bd->d_errmutex);
618247e9a8eSPaul Winder
619247e9a8eSPaul Winder bd_errstats_clrstr(&est->bd_model);
620247e9a8eSPaul Winder bd_errstats_clrstr(&est->bd_vid);
621247e9a8eSPaul Winder bd_errstats_clrstr(&est->bd_pid);
622247e9a8eSPaul Winder bd_errstats_clrstr(&est->bd_revision);
623247e9a8eSPaul Winder bd_errstats_clrstr(&est->bd_serial);
624247e9a8eSPaul Winder
625247e9a8eSPaul Winder mutex_exit(&bd->d_errmutex);
626247e9a8eSPaul Winder }
627247e9a8eSPaul Winder
6284d95620bSPaul Winder static void
bd_queues_free(bd_t * bd)6294d95620bSPaul Winder bd_queues_free(bd_t *bd)
6304d95620bSPaul Winder {
6314d95620bSPaul Winder uint32_t i;
6324d95620bSPaul Winder
6334d95620bSPaul Winder for (i = 0; i < bd->d_qcount; i++) {
6344d95620bSPaul Winder bd_queue_t *bq = &bd->d_queues[i];
6354d95620bSPaul Winder
6364d95620bSPaul Winder mutex_destroy(&bq->q_iomutex);
6374d95620bSPaul Winder list_destroy(&bq->q_waitq);
6384d95620bSPaul Winder list_destroy(&bq->q_runq);
6394d95620bSPaul Winder }
6404d95620bSPaul Winder
6414d95620bSPaul Winder kmem_free(bd->d_queues, sizeof (*bd->d_queues) * bd->d_qcount);
642bef9e21aSHans Rosenfeld }
643bef9e21aSHans Rosenfeld
6443f7d54a6SGarrett D'Amore static int
bd_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)6453f7d54a6SGarrett D'Amore bd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
6463f7d54a6SGarrett D'Amore {
6473f7d54a6SGarrett D'Amore int inst;
6483f7d54a6SGarrett D'Amore bd_handle_t hdl;
6493f7d54a6SGarrett D'Amore bd_t *bd;
6503f7d54a6SGarrett D'Amore bd_drive_t drive;
6514d95620bSPaul Winder uint32_t i;
6523f7d54a6SGarrett D'Amore int rv;
6533f7d54a6SGarrett D'Amore char name[16];
6543f7d54a6SGarrett D'Amore char kcache[32];
655ca0df52aSHans Rosenfeld char *node_type;
6563f7d54a6SGarrett D'Amore
6573f7d54a6SGarrett D'Amore switch (cmd) {
6583f7d54a6SGarrett D'Amore case DDI_ATTACH:
6593f7d54a6SGarrett D'Amore break;
6603f7d54a6SGarrett D'Amore case DDI_RESUME:
6613f7d54a6SGarrett D'Amore /* We don't do anything native for suspend/resume */
6623f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
6633f7d54a6SGarrett D'Amore default:
6643f7d54a6SGarrett D'Amore return (DDI_FAILURE);
6653f7d54a6SGarrett D'Amore }
6663f7d54a6SGarrett D'Amore
6673f7d54a6SGarrett D'Amore inst = ddi_get_instance(dip);
6683f7d54a6SGarrett D'Amore hdl = ddi_get_parent_data(dip);
6693f7d54a6SGarrett D'Amore
6703f7d54a6SGarrett D'Amore (void) snprintf(name, sizeof (name), "%s%d",
6713f7d54a6SGarrett D'Amore ddi_driver_name(dip), ddi_get_instance(dip));
6723f7d54a6SGarrett D'Amore (void) snprintf(kcache, sizeof (kcache), "%s_xfer", name);
6733f7d54a6SGarrett D'Amore
6743f7d54a6SGarrett D'Amore if (hdl == NULL) {
6753f7d54a6SGarrett D'Amore cmn_err(CE_WARN, "%s: missing parent data!", name);
6763f7d54a6SGarrett D'Amore return (DDI_FAILURE);
6773f7d54a6SGarrett D'Amore }
6783f7d54a6SGarrett D'Amore
6793f7d54a6SGarrett D'Amore if (ddi_soft_state_zalloc(bd_state, inst) != DDI_SUCCESS) {
6803f7d54a6SGarrett D'Amore cmn_err(CE_WARN, "%s: unable to zalloc soft state!", name);
6813f7d54a6SGarrett D'Amore return (DDI_FAILURE);
6823f7d54a6SGarrett D'Amore }
6833f7d54a6SGarrett D'Amore bd = ddi_get_soft_state(bd_state, inst);
6843f7d54a6SGarrett D'Amore
6853f7d54a6SGarrett D'Amore if (hdl->h_dma) {
6863f7d54a6SGarrett D'Amore bd->d_dma = *(hdl->h_dma);
6873f7d54a6SGarrett D'Amore bd->d_dma.dma_attr_granular =
6883f7d54a6SGarrett D'Amore max(DEV_BSIZE, bd->d_dma.dma_attr_granular);
6893f7d54a6SGarrett D'Amore bd->d_use_dma = B_TRUE;
6903f7d54a6SGarrett D'Amore
6913f7d54a6SGarrett D'Amore if (bd->d_maxxfer &&
6923f7d54a6SGarrett D'Amore (bd->d_maxxfer != bd->d_dma.dma_attr_maxxfer)) {
6933f7d54a6SGarrett D'Amore cmn_err(CE_WARN,
6943f7d54a6SGarrett D'Amore "%s: inconsistent maximum transfer size!",
6953f7d54a6SGarrett D'Amore name);
6963f7d54a6SGarrett D'Amore /* We force it */
6973f7d54a6SGarrett D'Amore bd->d_maxxfer = bd->d_dma.dma_attr_maxxfer;
6983f7d54a6SGarrett D'Amore } else {
6993f7d54a6SGarrett D'Amore bd->d_maxxfer = bd->d_dma.dma_attr_maxxfer;
7003f7d54a6SGarrett D'Amore }
7013f7d54a6SGarrett D'Amore } else {
7023f7d54a6SGarrett D'Amore bd->d_use_dma = B_FALSE;
7033f7d54a6SGarrett D'Amore if (bd->d_maxxfer == 0) {
7043f7d54a6SGarrett D'Amore bd->d_maxxfer = 1024 * 1024;
7053f7d54a6SGarrett D'Amore }
7063f7d54a6SGarrett D'Amore }
7073f7d54a6SGarrett D'Amore bd->d_ops = hdl->h_ops;
7083f7d54a6SGarrett D'Amore bd->d_private = hdl->h_private;
7091a5ae140SJason King bd->d_blkshift = DEV_BSHIFT; /* 512 bytes, to start */
7103f7d54a6SGarrett D'Amore
7113f7d54a6SGarrett D'Amore if (bd->d_maxxfer % DEV_BSIZE) {
7123f7d54a6SGarrett D'Amore cmn_err(CE_WARN, "%s: maximum transfer misaligned!", name);
7133f7d54a6SGarrett D'Amore bd->d_maxxfer &= ~(DEV_BSIZE - 1);
7143f7d54a6SGarrett D'Amore }
7153f7d54a6SGarrett D'Amore if (bd->d_maxxfer < DEV_BSIZE) {
7163f7d54a6SGarrett D'Amore cmn_err(CE_WARN, "%s: maximum transfer size too small!", name);
7173f7d54a6SGarrett D'Amore ddi_soft_state_free(bd_state, inst);
7183f7d54a6SGarrett D'Amore return (DDI_FAILURE);
7193f7d54a6SGarrett D'Amore }
7203f7d54a6SGarrett D'Amore
7213f7d54a6SGarrett D'Amore bd->d_dip = dip;
7223f7d54a6SGarrett D'Amore bd->d_handle = hdl;
7233f7d54a6SGarrett D'Amore ddi_set_driver_private(dip, bd);
7243f7d54a6SGarrett D'Amore
7254d95620bSPaul Winder mutex_init(&bd->d_ksmutex, NULL, MUTEX_DRIVER, NULL);
7263f7d54a6SGarrett D'Amore mutex_init(&bd->d_ocmutex, NULL, MUTEX_DRIVER, NULL);
7273f7d54a6SGarrett D'Amore mutex_init(&bd->d_statemutex, NULL, MUTEX_DRIVER, NULL);
7283f7d54a6SGarrett D'Amore cv_init(&bd->d_statecv, NULL, CV_DRIVER, NULL);
7296f0e4dc9SAndy Fiddaman mutex_init(&bd->d_dle_mutex, NULL, MUTEX_DRIVER, NULL);
7306f0e4dc9SAndy Fiddaman bd->d_dle_state = 0;
7313f7d54a6SGarrett D'Amore
7323f7d54a6SGarrett D'Amore bd->d_cache = kmem_cache_create(kcache, sizeof (bd_xfer_impl_t), 8,
7333f7d54a6SGarrett D'Amore bd_xfer_ctor, bd_xfer_dtor, NULL, bd, NULL, 0);
7343f7d54a6SGarrett D'Amore
7353f7d54a6SGarrett D'Amore bd->d_ksp = kstat_create(ddi_driver_name(dip), inst, NULL, "disk",
7363f7d54a6SGarrett D'Amore KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7373f7d54a6SGarrett D'Amore if (bd->d_ksp != NULL) {
7384d95620bSPaul Winder bd->d_ksp->ks_lock = &bd->d_ksmutex;
7393f7d54a6SGarrett D'Amore kstat_install(bd->d_ksp);
7403f7d54a6SGarrett D'Amore bd->d_kiop = bd->d_ksp->ks_data;
7413f7d54a6SGarrett D'Amore } else {
7423f7d54a6SGarrett D'Amore /*
7433f7d54a6SGarrett D'Amore * Even if we cannot create the kstat, we create a
7443f7d54a6SGarrett D'Amore * scratch kstat. The reason for this is to ensure
7453f7d54a6SGarrett D'Amore * that we can update the kstat all of the time,
7463f7d54a6SGarrett D'Amore * without adding an extra branch instruction.
7473f7d54a6SGarrett D'Amore */
7483f7d54a6SGarrett D'Amore bd->d_kiop = kmem_zalloc(sizeof (kstat_io_t), KM_SLEEP);
7493f7d54a6SGarrett D'Amore }
7503f7d54a6SGarrett D'Amore
7513f7d54a6SGarrett D'Amore cmlb_alloc_handle(&bd->d_cmlbh);
7523f7d54a6SGarrett D'Amore
7533f7d54a6SGarrett D'Amore bd->d_state = DKIO_NONE;
7543f7d54a6SGarrett D'Amore
7553f7d54a6SGarrett D'Amore bzero(&drive, sizeof (drive));
7564d95620bSPaul Winder /*
7571a5ae140SJason King * Default to one queue, and no restrictions on free space requests
7581a5ae140SJason King * (if driver provides method) parent driver can override.
7594d95620bSPaul Winder */
7604d95620bSPaul Winder drive.d_qcount = 1;
7611a5ae140SJason King drive.d_free_align = 1;
7623f7d54a6SGarrett D'Amore bd->d_ops.o_drive_info(bd->d_private, &drive);
7631a5ae140SJason King
7641a5ae140SJason King /*
7651a5ae140SJason King * Several checks to make sure o_drive_info() didn't return bad
7661a5ae140SJason King * values:
7671a5ae140SJason King *
7681a5ae140SJason King * There must be at least one queue
7691a5ae140SJason King */
7701a5ae140SJason King if (drive.d_qcount == 0)
7711a5ae140SJason King goto fail_drive_info;
7721a5ae140SJason King
7731a5ae140SJason King /* FREE/UNMAP/TRIM alignment needs to be at least 1 block */
7741a5ae140SJason King if (drive.d_free_align == 0)
7751a5ae140SJason King goto fail_drive_info;
7761a5ae140SJason King
7771a5ae140SJason King /*
7781a5ae140SJason King * If d_max_free_blks is not unlimited (not 0), then we cannot allow
7791a5ae140SJason King * an unlimited segment size. It is however permissible to not impose
7801a5ae140SJason King * a limit on the total number of blocks freed while limiting the
7811a5ae140SJason King * amount allowed in an individual segment.
7821a5ae140SJason King */
7831a5ae140SJason King if ((drive.d_max_free_blks > 0 && drive.d_max_free_seg_blks == 0))
7841a5ae140SJason King goto fail_drive_info;
7851a5ae140SJason King
7861a5ae140SJason King /*
7871a5ae140SJason King * If a limit is set on d_max_free_blks (by the above check, we know
7881a5ae140SJason King * if there's a limit on d_max_free_blks, d_max_free_seg_blks cannot
7891a5ae140SJason King * be unlimited), it cannot be smaller than the limit on an individual
7901a5ae140SJason King * segment.
7911a5ae140SJason King */
7921a5ae140SJason King if ((drive.d_max_free_blks > 0 &&
7931a5ae140SJason King drive.d_max_free_seg_blks > drive.d_max_free_blks)) {
7941a5ae140SJason King goto fail_drive_info;
7951a5ae140SJason King }
7961a5ae140SJason King
7974d95620bSPaul Winder bd->d_qcount = drive.d_qcount;
7983f7d54a6SGarrett D'Amore bd->d_removable = drive.d_removable;
7993f7d54a6SGarrett D'Amore bd->d_hotpluggable = drive.d_hotpluggable;
8003f7d54a6SGarrett D'Amore
801bc220884SAlexey Zaytsev if (drive.d_maxxfer && drive.d_maxxfer < bd->d_maxxfer)
802bc220884SAlexey Zaytsev bd->d_maxxfer = drive.d_maxxfer;
803bc220884SAlexey Zaytsev
8041a5ae140SJason King bd->d_free_align = drive.d_free_align;
8051a5ae140SJason King bd->d_max_free_seg = drive.d_max_free_seg;
8061a5ae140SJason King bd->d_max_free_blks = drive.d_max_free_blks;
8071a5ae140SJason King bd->d_max_free_seg_blks = drive.d_max_free_seg_blks;
808bc220884SAlexey Zaytsev
8091a5ae140SJason King bd_create_inquiry_props(dip, &drive);
810bef9e21aSHans Rosenfeld bd_create_errstats(bd, inst, &drive);
811bef9e21aSHans Rosenfeld bd_update_state(bd);
812bef9e21aSHans Rosenfeld
8134d95620bSPaul Winder bd->d_queues = kmem_alloc(sizeof (*bd->d_queues) * bd->d_qcount,
8144d95620bSPaul Winder KM_SLEEP);
8154d95620bSPaul Winder for (i = 0; i < bd->d_qcount; i++) {
8164d95620bSPaul Winder bd_queue_t *bq = &bd->d_queues[i];
8174d95620bSPaul Winder
8184d95620bSPaul Winder bq->q_qsize = drive.d_qsize;
8194d95620bSPaul Winder bq->q_qactive = 0;
8204d95620bSPaul Winder mutex_init(&bq->q_iomutex, NULL, MUTEX_DRIVER, NULL);
8214d95620bSPaul Winder
8224d95620bSPaul Winder list_create(&bq->q_waitq, sizeof (bd_xfer_impl_t),
8234d95620bSPaul Winder offsetof(struct bd_xfer_impl, i_linkage));
8244d95620bSPaul Winder list_create(&bq->q_runq, sizeof (bd_xfer_impl_t),
8254d95620bSPaul Winder offsetof(struct bd_xfer_impl, i_linkage));
8264d95620bSPaul Winder }
8274d95620bSPaul Winder
828ca0df52aSHans Rosenfeld if (*(uint64_t *)drive.d_eui64 != 0 ||
829ca0df52aSHans Rosenfeld *(uint64_t *)drive.d_guid != 0 ||
830ca0df52aSHans Rosenfeld *((uint64_t *)drive.d_guid + 1) != 0)
831ca0df52aSHans Rosenfeld node_type = DDI_NT_BLOCK_BLKDEV;
832ca0df52aSHans Rosenfeld else if (drive.d_lun >= 0)
833ca0df52aSHans Rosenfeld node_type = DDI_NT_BLOCK_CHAN;
834ca0df52aSHans Rosenfeld else
835ca0df52aSHans Rosenfeld node_type = DDI_NT_BLOCK;
836ca0df52aSHans Rosenfeld
8373f7d54a6SGarrett D'Amore rv = cmlb_attach(dip, &bd_tg_ops, DTYPE_DIRECT,
838ca0df52aSHans Rosenfeld bd->d_removable, bd->d_hotpluggable, node_type,
83986e3bca6SGarrett D'Amore CMLB_FAKE_LABEL_ONE_PARTITION, bd->d_cmlbh, 0);
8403f7d54a6SGarrett D'Amore if (rv != 0) {
8411a5ae140SJason King goto fail_cmlb_attach;
8423f7d54a6SGarrett D'Amore }
8433f7d54a6SGarrett D'Amore
8443f7d54a6SGarrett D'Amore if (bd->d_ops.o_devid_init != NULL) {
8453f7d54a6SGarrett D'Amore rv = bd->d_ops.o_devid_init(bd->d_private, dip, &bd->d_devid);
8463f7d54a6SGarrett D'Amore if (rv == DDI_SUCCESS) {
8473f7d54a6SGarrett D'Amore if (ddi_devid_register(dip, bd->d_devid) !=
8483f7d54a6SGarrett D'Amore DDI_SUCCESS) {
8493f7d54a6SGarrett D'Amore cmn_err(CE_WARN,
8503f7d54a6SGarrett D'Amore "%s: unable to register devid", name);
8513f7d54a6SGarrett D'Amore }
8523f7d54a6SGarrett D'Amore }
8533f7d54a6SGarrett D'Amore }
8543f7d54a6SGarrett D'Amore
8553f7d54a6SGarrett D'Amore /*
8563f7d54a6SGarrett D'Amore * Add a zero-length attribute to tell the world we support
8573f7d54a6SGarrett D'Amore * kernel ioctls (for layered drivers). Also set up properties
8583f7d54a6SGarrett D'Amore * used by HAL to identify removable media.
8593f7d54a6SGarrett D'Amore */
8603f7d54a6SGarrett D'Amore (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
8613f7d54a6SGarrett D'Amore DDI_KERNEL_IOCTL, NULL, 0);
8623f7d54a6SGarrett D'Amore if (bd->d_removable) {
8633f7d54a6SGarrett D'Amore (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
8643f7d54a6SGarrett D'Amore "removable-media", NULL, 0);
8653f7d54a6SGarrett D'Amore }
8663f7d54a6SGarrett D'Amore if (bd->d_hotpluggable) {
8673f7d54a6SGarrett D'Amore (void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
8683f7d54a6SGarrett D'Amore "hotpluggable", NULL, 0);
8693f7d54a6SGarrett D'Amore }
8703f7d54a6SGarrett D'Amore
8714b2d0200SRobert Mustacchi /*
8724b2d0200SRobert Mustacchi * Before we proceed, we need to ensure that the geometry and labels on
8734b2d0200SRobert Mustacchi * the cmlb disk are reasonable. When cmlb first attaches, it does not
8744b2d0200SRobert Mustacchi * perform label validation and creates minor nodes based on the
8754b2d0200SRobert Mustacchi * assumption of the size. This may not be correct and the rest of the
8764b2d0200SRobert Mustacchi * system assumes that this will have been done before we allow opens
8774b2d0200SRobert Mustacchi * to proceed. Otherwise, on first open, this'll all end up changing
8784b2d0200SRobert Mustacchi * around on users. We do not care if it succeeds or not. It is totally
8794b2d0200SRobert Mustacchi * acceptable for this device to be unlabeled or not to have anything on
8804b2d0200SRobert Mustacchi * it.
8814b2d0200SRobert Mustacchi */
8824b2d0200SRobert Mustacchi (void) cmlb_validate(bd->d_cmlbh, 0, 0);
8834b2d0200SRobert Mustacchi
884da00bec1SHans Rosenfeld hdl->h_bd = bd;
8853f7d54a6SGarrett D'Amore ddi_report_dev(dip);
8863f7d54a6SGarrett D'Amore
8873f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
8881a5ae140SJason King
8891a5ae140SJason King fail_cmlb_attach:
8901a5ae140SJason King bd_queues_free(bd);
8911a5ae140SJason King bd_destroy_errstats(bd);
8921a5ae140SJason King
8931a5ae140SJason King fail_drive_info:
8941a5ae140SJason King cmlb_free_handle(&bd->d_cmlbh);
8951a5ae140SJason King
8961a5ae140SJason King if (bd->d_ksp != NULL) {
8971a5ae140SJason King kstat_delete(bd->d_ksp);
8981a5ae140SJason King bd->d_ksp = NULL;
8991a5ae140SJason King } else {
9001a5ae140SJason King kmem_free(bd->d_kiop, sizeof (kstat_io_t));
9011a5ae140SJason King }
9021a5ae140SJason King
9031a5ae140SJason King kmem_cache_destroy(bd->d_cache);
9041a5ae140SJason King cv_destroy(&bd->d_statecv);
9051a5ae140SJason King mutex_destroy(&bd->d_statemutex);
9061a5ae140SJason King mutex_destroy(&bd->d_ocmutex);
9071a5ae140SJason King mutex_destroy(&bd->d_ksmutex);
9086f0e4dc9SAndy Fiddaman mutex_destroy(&bd->d_dle_mutex);
9091a5ae140SJason King ddi_soft_state_free(bd_state, inst);
9101a5ae140SJason King return (DDI_FAILURE);
9113f7d54a6SGarrett D'Amore }
9123f7d54a6SGarrett D'Amore
9133f7d54a6SGarrett D'Amore static int
bd_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)9143f7d54a6SGarrett D'Amore bd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
9153f7d54a6SGarrett D'Amore {
916da00bec1SHans Rosenfeld bd_handle_t hdl;
917da00bec1SHans Rosenfeld bd_t *bd;
9183f7d54a6SGarrett D'Amore
9193f7d54a6SGarrett D'Amore bd = ddi_get_driver_private(dip);
920da00bec1SHans Rosenfeld hdl = ddi_get_parent_data(dip);
9213f7d54a6SGarrett D'Amore
9223f7d54a6SGarrett D'Amore switch (cmd) {
9233f7d54a6SGarrett D'Amore case DDI_DETACH:
9243f7d54a6SGarrett D'Amore break;
9253f7d54a6SGarrett D'Amore case DDI_SUSPEND:
9263f7d54a6SGarrett D'Amore /* We don't suspend, but our parent does */
9273f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
9283f7d54a6SGarrett D'Amore default:
9293f7d54a6SGarrett D'Amore return (DDI_FAILURE);
9303f7d54a6SGarrett D'Amore }
93133f84ecfSJason King
932da00bec1SHans Rosenfeld hdl->h_bd = NULL;
933da00bec1SHans Rosenfeld
9343f7d54a6SGarrett D'Amore if (bd->d_ksp != NULL) {
9353f7d54a6SGarrett D'Amore kstat_delete(bd->d_ksp);
9363f7d54a6SGarrett D'Amore bd->d_ksp = NULL;
9373f7d54a6SGarrett D'Amore } else {
9383f7d54a6SGarrett D'Amore kmem_free(bd->d_kiop, sizeof (kstat_io_t));
9393f7d54a6SGarrett D'Amore }
940bef9e21aSHans Rosenfeld
94133f84ecfSJason King bd_destroy_errstats(bd);
94286e3bca6SGarrett D'Amore cmlb_detach(bd->d_cmlbh, 0);
9433f7d54a6SGarrett D'Amore cmlb_free_handle(&bd->d_cmlbh);
9443f7d54a6SGarrett D'Amore if (bd->d_devid)
9453f7d54a6SGarrett D'Amore ddi_devid_free(bd->d_devid);
9463f7d54a6SGarrett D'Amore kmem_cache_destroy(bd->d_cache);
9474d95620bSPaul Winder mutex_destroy(&bd->d_ksmutex);
9483f7d54a6SGarrett D'Amore mutex_destroy(&bd->d_ocmutex);
9493f7d54a6SGarrett D'Amore mutex_destroy(&bd->d_statemutex);
9503f7d54a6SGarrett D'Amore cv_destroy(&bd->d_statecv);
9516f0e4dc9SAndy Fiddaman mutex_destroy(&bd->d_dle_mutex);
9524d95620bSPaul Winder bd_queues_free(bd);
9533f7d54a6SGarrett D'Amore ddi_soft_state_free(bd_state, ddi_get_instance(dip));
9543f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
9553f7d54a6SGarrett D'Amore }
9563f7d54a6SGarrett D'Amore
9573f7d54a6SGarrett D'Amore static int
bd_xfer_ctor(void * buf,void * arg,int kmflag)9583f7d54a6SGarrett D'Amore bd_xfer_ctor(void *buf, void *arg, int kmflag)
9593f7d54a6SGarrett D'Amore {
9603f7d54a6SGarrett D'Amore bd_xfer_impl_t *xi;
9613f7d54a6SGarrett D'Amore bd_t *bd = arg;
9623f7d54a6SGarrett D'Amore int (*dcb)(caddr_t);
9633f7d54a6SGarrett D'Amore
964679ac156SAlexey Zaytsev if (kmflag == KM_PUSHPAGE || kmflag == KM_SLEEP) {
9653f7d54a6SGarrett D'Amore dcb = DDI_DMA_SLEEP;
9663f7d54a6SGarrett D'Amore } else {
9673f7d54a6SGarrett D'Amore dcb = DDI_DMA_DONTWAIT;
9683f7d54a6SGarrett D'Amore }
9693f7d54a6SGarrett D'Amore
9703f7d54a6SGarrett D'Amore xi = buf;
9713f7d54a6SGarrett D'Amore bzero(xi, sizeof (*xi));
9723f7d54a6SGarrett D'Amore xi->i_bd = bd;
9733f7d54a6SGarrett D'Amore
9743f7d54a6SGarrett D'Amore if (bd->d_use_dma) {
9753f7d54a6SGarrett D'Amore if (ddi_dma_alloc_handle(bd->d_dip, &bd->d_dma, dcb, NULL,
9763f7d54a6SGarrett D'Amore &xi->i_dmah) != DDI_SUCCESS) {
9773f7d54a6SGarrett D'Amore return (-1);
9783f7d54a6SGarrett D'Amore }
9793f7d54a6SGarrett D'Amore }
9803f7d54a6SGarrett D'Amore
9813f7d54a6SGarrett D'Amore return (0);
9823f7d54a6SGarrett D'Amore }
9833f7d54a6SGarrett D'Amore
9843f7d54a6SGarrett D'Amore static void
bd_xfer_dtor(void * buf,void * arg)9853f7d54a6SGarrett D'Amore bd_xfer_dtor(void *buf, void *arg)
9863f7d54a6SGarrett D'Amore {
9873f7d54a6SGarrett D'Amore bd_xfer_impl_t *xi = buf;
9883f7d54a6SGarrett D'Amore
9893f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(arg));
9903f7d54a6SGarrett D'Amore
9913f7d54a6SGarrett D'Amore if (xi->i_dmah)
9923f7d54a6SGarrett D'Amore ddi_dma_free_handle(&xi->i_dmah);
9933f7d54a6SGarrett D'Amore xi->i_dmah = NULL;
9943f7d54a6SGarrett D'Amore }
9953f7d54a6SGarrett D'Amore
9963f7d54a6SGarrett D'Amore static bd_xfer_impl_t *
bd_xfer_alloc(bd_t * bd,struct buf * bp,int (* func)(void *,bd_xfer_t *),int kmflag)9973f7d54a6SGarrett D'Amore bd_xfer_alloc(bd_t *bd, struct buf *bp, int (*func)(void *, bd_xfer_t *),
9983f7d54a6SGarrett D'Amore int kmflag)
9993f7d54a6SGarrett D'Amore {
10003f7d54a6SGarrett D'Amore bd_xfer_impl_t *xi;
1001a0cb694bSSteve Ma int rv = 0;
10023f7d54a6SGarrett D'Amore int status;
10033f7d54a6SGarrett D'Amore unsigned dir;
10043f7d54a6SGarrett D'Amore int (*cb)(caddr_t);
10053f7d54a6SGarrett D'Amore size_t len;
10063f7d54a6SGarrett D'Amore uint32_t shift;
10073f7d54a6SGarrett D'Amore
10083f7d54a6SGarrett D'Amore if (kmflag == KM_SLEEP) {
10093f7d54a6SGarrett D'Amore cb = DDI_DMA_SLEEP;
10103f7d54a6SGarrett D'Amore } else {
10113f7d54a6SGarrett D'Amore cb = DDI_DMA_DONTWAIT;
10123f7d54a6SGarrett D'Amore }
10133f7d54a6SGarrett D'Amore
10143f7d54a6SGarrett D'Amore xi = kmem_cache_alloc(bd->d_cache, kmflag);
10153f7d54a6SGarrett D'Amore if (xi == NULL) {
10163f7d54a6SGarrett D'Amore bioerror(bp, ENOMEM);
10173f7d54a6SGarrett D'Amore return (NULL);
10183f7d54a6SGarrett D'Amore }
10193f7d54a6SGarrett D'Amore
10203f7d54a6SGarrett D'Amore ASSERT(bp);
10213f7d54a6SGarrett D'Amore
10223f7d54a6SGarrett D'Amore xi->i_bp = bp;
10233f7d54a6SGarrett D'Amore xi->i_func = func;
1024c0591a0cSYouzhong Yang xi->i_blkno = bp->b_lblkno >> (bd->d_blkshift - DEV_BSHIFT);
10253f7d54a6SGarrett D'Amore
10263f7d54a6SGarrett D'Amore if (bp->b_bcount == 0) {
10273f7d54a6SGarrett D'Amore xi->i_len = 0;
10283f7d54a6SGarrett D'Amore xi->i_nblks = 0;
10293f7d54a6SGarrett D'Amore xi->i_kaddr = NULL;
10303f7d54a6SGarrett D'Amore xi->i_resid = 0;
10313f7d54a6SGarrett D'Amore xi->i_num_win = 0;
10323f7d54a6SGarrett D'Amore goto done;
10333f7d54a6SGarrett D'Amore }
10343f7d54a6SGarrett D'Amore
10353f7d54a6SGarrett D'Amore if (bp->b_flags & B_READ) {
10363f7d54a6SGarrett D'Amore dir = DDI_DMA_READ;
10373f7d54a6SGarrett D'Amore xi->i_func = bd->d_ops.o_read;
10383f7d54a6SGarrett D'Amore } else {
10393f7d54a6SGarrett D'Amore dir = DDI_DMA_WRITE;
10403f7d54a6SGarrett D'Amore xi->i_func = bd->d_ops.o_write;
10413f7d54a6SGarrett D'Amore }
10423f7d54a6SGarrett D'Amore
10433f7d54a6SGarrett D'Amore shift = bd->d_blkshift;
10443f7d54a6SGarrett D'Amore xi->i_blkshift = shift;
10453f7d54a6SGarrett D'Amore
10463f7d54a6SGarrett D'Amore if (!bd->d_use_dma) {
10473f7d54a6SGarrett D'Amore bp_mapin(bp);
10483f7d54a6SGarrett D'Amore rv = 0;
10493f7d54a6SGarrett D'Amore xi->i_offset = 0;
10503f7d54a6SGarrett D'Amore xi->i_num_win =
10513f7d54a6SGarrett D'Amore (bp->b_bcount + (bd->d_maxxfer - 1)) / bd->d_maxxfer;
10523f7d54a6SGarrett D'Amore xi->i_cur_win = 0;
10533f7d54a6SGarrett D'Amore xi->i_len = min(bp->b_bcount, bd->d_maxxfer);
105404904ca2SDan McDonald xi->i_nblks = xi->i_len >> shift;
10553f7d54a6SGarrett D'Amore xi->i_kaddr = bp->b_un.b_addr;
10563f7d54a6SGarrett D'Amore xi->i_resid = bp->b_bcount;
10573f7d54a6SGarrett D'Amore } else {
10583f7d54a6SGarrett D'Amore
10593f7d54a6SGarrett D'Amore /*
10603f7d54a6SGarrett D'Amore * We have to use consistent DMA if the address is misaligned.
10613f7d54a6SGarrett D'Amore */
10623f7d54a6SGarrett D'Amore if (((bp->b_flags & (B_PAGEIO | B_REMAPPED)) != B_PAGEIO) &&
10633f7d54a6SGarrett D'Amore ((uintptr_t)bp->b_un.b_addr & 0x7)) {
10643f7d54a6SGarrett D'Amore dir |= DDI_DMA_CONSISTENT | DDI_DMA_PARTIAL;
10653f7d54a6SGarrett D'Amore } else {
10663f7d54a6SGarrett D'Amore dir |= DDI_DMA_STREAMING | DDI_DMA_PARTIAL;
10673f7d54a6SGarrett D'Amore }
10683f7d54a6SGarrett D'Amore
10693f7d54a6SGarrett D'Amore status = ddi_dma_buf_bind_handle(xi->i_dmah, bp, dir, cb,
10703f7d54a6SGarrett D'Amore NULL, &xi->i_dmac, &xi->i_ndmac);
10713f7d54a6SGarrett D'Amore switch (status) {
10723f7d54a6SGarrett D'Amore case DDI_DMA_MAPPED:
10733f7d54a6SGarrett D'Amore xi->i_num_win = 1;
10743f7d54a6SGarrett D'Amore xi->i_cur_win = 0;
10753f7d54a6SGarrett D'Amore xi->i_offset = 0;
10763f7d54a6SGarrett D'Amore xi->i_len = bp->b_bcount;
107704904ca2SDan McDonald xi->i_nblks = xi->i_len >> shift;
10783f7d54a6SGarrett D'Amore xi->i_resid = bp->b_bcount;
10793f7d54a6SGarrett D'Amore rv = 0;
10803f7d54a6SGarrett D'Amore break;
10813f7d54a6SGarrett D'Amore case DDI_DMA_PARTIAL_MAP:
10823f7d54a6SGarrett D'Amore xi->i_cur_win = 0;
10833f7d54a6SGarrett D'Amore
10843f7d54a6SGarrett D'Amore if ((ddi_dma_numwin(xi->i_dmah, &xi->i_num_win) !=
10853f7d54a6SGarrett D'Amore DDI_SUCCESS) ||
10863f7d54a6SGarrett D'Amore (ddi_dma_getwin(xi->i_dmah, 0, &xi->i_offset,
10873f7d54a6SGarrett D'Amore &len, &xi->i_dmac, &xi->i_ndmac) !=
10883f7d54a6SGarrett D'Amore DDI_SUCCESS) ||
1089c0591a0cSYouzhong Yang (P2PHASE(len, (1U << shift)) != 0)) {
10903f7d54a6SGarrett D'Amore (void) ddi_dma_unbind_handle(xi->i_dmah);
10913f7d54a6SGarrett D'Amore rv = EFAULT;
10923f7d54a6SGarrett D'Amore goto done;
10933f7d54a6SGarrett D'Amore }
10943f7d54a6SGarrett D'Amore xi->i_len = len;
109504904ca2SDan McDonald xi->i_nblks = xi->i_len >> shift;
10963f7d54a6SGarrett D'Amore xi->i_resid = bp->b_bcount;
10973f7d54a6SGarrett D'Amore rv = 0;
10983f7d54a6SGarrett D'Amore break;
10993f7d54a6SGarrett D'Amore case DDI_DMA_NORESOURCES:
11003f7d54a6SGarrett D'Amore rv = EAGAIN;
11013f7d54a6SGarrett D'Amore goto done;
11023f7d54a6SGarrett D'Amore case DDI_DMA_TOOBIG:
11033f7d54a6SGarrett D'Amore rv = EINVAL;
11043f7d54a6SGarrett D'Amore goto done;
11053f7d54a6SGarrett D'Amore case DDI_DMA_NOMAPPING:
11063f7d54a6SGarrett D'Amore case DDI_DMA_INUSE:
11073f7d54a6SGarrett D'Amore default:
11083f7d54a6SGarrett D'Amore rv = EFAULT;
11093f7d54a6SGarrett D'Amore goto done;
11103f7d54a6SGarrett D'Amore }
11113f7d54a6SGarrett D'Amore }
11123f7d54a6SGarrett D'Amore
11133f7d54a6SGarrett D'Amore done:
11143f7d54a6SGarrett D'Amore if (rv != 0) {
11153f7d54a6SGarrett D'Amore kmem_cache_free(bd->d_cache, xi);
11163f7d54a6SGarrett D'Amore bioerror(bp, rv);
11173f7d54a6SGarrett D'Amore return (NULL);
11183f7d54a6SGarrett D'Amore }
11193f7d54a6SGarrett D'Amore
11203f7d54a6SGarrett D'Amore return (xi);
11213f7d54a6SGarrett D'Amore }
11223f7d54a6SGarrett D'Amore
11233f7d54a6SGarrett D'Amore static void
bd_xfer_free(bd_xfer_impl_t * xi)11243f7d54a6SGarrett D'Amore bd_xfer_free(bd_xfer_impl_t *xi)
11253f7d54a6SGarrett D'Amore {
11263f7d54a6SGarrett D'Amore if (xi->i_dmah) {
11273f7d54a6SGarrett D'Amore (void) ddi_dma_unbind_handle(xi->i_dmah);
11283f7d54a6SGarrett D'Amore }
11291a5ae140SJason King if (xi->i_dfl != NULL) {
11301a5ae140SJason King dfl_free((dkioc_free_list_t *)xi->i_dfl);
11311a5ae140SJason King xi->i_dfl = NULL;
11321a5ae140SJason King }
11333f7d54a6SGarrett D'Amore kmem_cache_free(xi->i_bd->d_cache, xi);
11343f7d54a6SGarrett D'Amore }
11353f7d54a6SGarrett D'Amore
11363f7d54a6SGarrett D'Amore static int
bd_open(dev_t * devp,int flag,int otyp,cred_t * credp)11373f7d54a6SGarrett D'Amore bd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
11383f7d54a6SGarrett D'Amore {
11393f7d54a6SGarrett D'Amore dev_t dev = *devp;
11403f7d54a6SGarrett D'Amore bd_t *bd;
11413f7d54a6SGarrett D'Amore minor_t part;
11423f7d54a6SGarrett D'Amore minor_t inst;
11433f7d54a6SGarrett D'Amore uint64_t mask;
11443f7d54a6SGarrett D'Amore boolean_t ndelay;
11453f7d54a6SGarrett D'Amore int rv;
11463f7d54a6SGarrett D'Amore diskaddr_t nblks;
11473f7d54a6SGarrett D'Amore diskaddr_t lba;
11483f7d54a6SGarrett D'Amore
11493f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(credp));
11503f7d54a6SGarrett D'Amore
11513f7d54a6SGarrett D'Amore part = BDPART(dev);
11523f7d54a6SGarrett D'Amore inst = BDINST(dev);
11533f7d54a6SGarrett D'Amore
11543f7d54a6SGarrett D'Amore if (otyp >= OTYPCNT)
11553f7d54a6SGarrett D'Amore return (EINVAL);
11563f7d54a6SGarrett D'Amore
11573f7d54a6SGarrett D'Amore ndelay = (flag & (FNDELAY | FNONBLOCK)) ? B_TRUE : B_FALSE;
11583f7d54a6SGarrett D'Amore
11593f7d54a6SGarrett D'Amore /*
11603f7d54a6SGarrett D'Amore * Block any DR events from changing the set of registered
11613f7d54a6SGarrett D'Amore * devices while we function.
11623f7d54a6SGarrett D'Amore */
11633f7d54a6SGarrett D'Amore rw_enter(&bd_lock, RW_READER);
11643f7d54a6SGarrett D'Amore if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
11653f7d54a6SGarrett D'Amore rw_exit(&bd_lock);
11663f7d54a6SGarrett D'Amore return (ENXIO);
11673f7d54a6SGarrett D'Amore }
11683f7d54a6SGarrett D'Amore
11693f7d54a6SGarrett D'Amore mutex_enter(&bd->d_ocmutex);
11703f7d54a6SGarrett D'Amore
11713f7d54a6SGarrett D'Amore ASSERT(part < 64);
11723f7d54a6SGarrett D'Amore mask = (1U << part);
11733f7d54a6SGarrett D'Amore
11743f7d54a6SGarrett D'Amore bd_update_state(bd);
11753f7d54a6SGarrett D'Amore
117686e3bca6SGarrett D'Amore if (cmlb_validate(bd->d_cmlbh, 0, 0) != 0) {
11773f7d54a6SGarrett D'Amore
11783f7d54a6SGarrett D'Amore /* non-blocking opens are allowed to succeed */
11793f7d54a6SGarrett D'Amore if (!ndelay) {
11803f7d54a6SGarrett D'Amore rv = ENXIO;
11813f7d54a6SGarrett D'Amore goto done;
11823f7d54a6SGarrett D'Amore }
11833f7d54a6SGarrett D'Amore } else if (cmlb_partinfo(bd->d_cmlbh, part, &nblks, &lba,
118486e3bca6SGarrett D'Amore NULL, NULL, 0) == 0) {
11853f7d54a6SGarrett D'Amore
11863f7d54a6SGarrett D'Amore /*
11873f7d54a6SGarrett D'Amore * We read the partinfo, verify valid ranges. If the
11883f7d54a6SGarrett D'Amore * partition is invalid, and we aren't blocking or
11893f7d54a6SGarrett D'Amore * doing a raw access, then fail. (Non-blocking and
11903f7d54a6SGarrett D'Amore * raw accesses can still succeed to allow a disk with
11913f7d54a6SGarrett D'Amore * bad partition data to opened by format and fdisk.)
11923f7d54a6SGarrett D'Amore */
11933f7d54a6SGarrett D'Amore if ((!nblks) && ((!ndelay) || (otyp != OTYP_CHR))) {
11943f7d54a6SGarrett D'Amore rv = ENXIO;
11953f7d54a6SGarrett D'Amore goto done;
11963f7d54a6SGarrett D'Amore }
11973f7d54a6SGarrett D'Amore } else if (!ndelay) {
11983f7d54a6SGarrett D'Amore /*
11993f7d54a6SGarrett D'Amore * cmlb_partinfo failed -- invalid partition or no
12003f7d54a6SGarrett D'Amore * disk label.
12013f7d54a6SGarrett D'Amore */
12023f7d54a6SGarrett D'Amore rv = ENXIO;
12033f7d54a6SGarrett D'Amore goto done;
12043f7d54a6SGarrett D'Amore }
12053f7d54a6SGarrett D'Amore
12063f7d54a6SGarrett D'Amore if ((flag & FWRITE) && bd->d_rdonly) {
12073f7d54a6SGarrett D'Amore rv = EROFS;
12083f7d54a6SGarrett D'Amore goto done;
12093f7d54a6SGarrett D'Amore }
12103f7d54a6SGarrett D'Amore
12113f7d54a6SGarrett D'Amore if ((bd->d_open_excl) & (mask)) {
12123f7d54a6SGarrett D'Amore rv = EBUSY;
12133f7d54a6SGarrett D'Amore goto done;
12143f7d54a6SGarrett D'Amore }
12153f7d54a6SGarrett D'Amore if (flag & FEXCL) {
12163f7d54a6SGarrett D'Amore if (bd->d_open_lyr[part]) {
12173f7d54a6SGarrett D'Amore rv = EBUSY;
12183f7d54a6SGarrett D'Amore goto done;
12193f7d54a6SGarrett D'Amore }
12203f7d54a6SGarrett D'Amore for (int i = 0; i < OTYP_LYR; i++) {
12213f7d54a6SGarrett D'Amore if (bd->d_open_reg[i] & mask) {
12223f7d54a6SGarrett D'Amore rv = EBUSY;
12233f7d54a6SGarrett D'Amore goto done;
12243f7d54a6SGarrett D'Amore }
12253f7d54a6SGarrett D'Amore }
12263f7d54a6SGarrett D'Amore }
12273f7d54a6SGarrett D'Amore
12283f7d54a6SGarrett D'Amore if (otyp == OTYP_LYR) {
12293f7d54a6SGarrett D'Amore bd->d_open_lyr[part]++;
12303f7d54a6SGarrett D'Amore } else {
12313f7d54a6SGarrett D'Amore bd->d_open_reg[otyp] |= mask;
12323f7d54a6SGarrett D'Amore }
12333f7d54a6SGarrett D'Amore if (flag & FEXCL) {
12343f7d54a6SGarrett D'Amore bd->d_open_excl |= mask;
12353f7d54a6SGarrett D'Amore }
12363f7d54a6SGarrett D'Amore
12373f7d54a6SGarrett D'Amore rv = 0;
12383f7d54a6SGarrett D'Amore done:
12393f7d54a6SGarrett D'Amore mutex_exit(&bd->d_ocmutex);
12403f7d54a6SGarrett D'Amore rw_exit(&bd_lock);
12413f7d54a6SGarrett D'Amore
12423f7d54a6SGarrett D'Amore return (rv);
12433f7d54a6SGarrett D'Amore }
12443f7d54a6SGarrett D'Amore
12453f7d54a6SGarrett D'Amore static int
bd_close(dev_t dev,int flag,int otyp,cred_t * credp)12463f7d54a6SGarrett D'Amore bd_close(dev_t dev, int flag, int otyp, cred_t *credp)
12473f7d54a6SGarrett D'Amore {
12483f7d54a6SGarrett D'Amore bd_t *bd;
12493f7d54a6SGarrett D'Amore minor_t inst;
12503f7d54a6SGarrett D'Amore minor_t part;
12513f7d54a6SGarrett D'Amore uint64_t mask;
12523f7d54a6SGarrett D'Amore boolean_t last = B_TRUE;
12533f7d54a6SGarrett D'Amore
12543f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(flag));
12553f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(credp));
12563f7d54a6SGarrett D'Amore
12573f7d54a6SGarrett D'Amore part = BDPART(dev);
12583f7d54a6SGarrett D'Amore inst = BDINST(dev);
12593f7d54a6SGarrett D'Amore
12603f7d54a6SGarrett D'Amore ASSERT(part < 64);
12613f7d54a6SGarrett D'Amore mask = (1U << part);
12623f7d54a6SGarrett D'Amore
12633f7d54a6SGarrett D'Amore rw_enter(&bd_lock, RW_READER);
12643f7d54a6SGarrett D'Amore
12653f7d54a6SGarrett D'Amore if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
12663f7d54a6SGarrett D'Amore rw_exit(&bd_lock);
12673f7d54a6SGarrett D'Amore return (ENXIO);
12683f7d54a6SGarrett D'Amore }
12693f7d54a6SGarrett D'Amore
12703f7d54a6SGarrett D'Amore mutex_enter(&bd->d_ocmutex);
12713f7d54a6SGarrett D'Amore if (bd->d_open_excl & mask) {
12723f7d54a6SGarrett D'Amore bd->d_open_excl &= ~mask;
12733f7d54a6SGarrett D'Amore }
12743f7d54a6SGarrett D'Amore if (otyp == OTYP_LYR) {
12753f7d54a6SGarrett D'Amore bd->d_open_lyr[part]--;
12763f7d54a6SGarrett D'Amore } else {
12773f7d54a6SGarrett D'Amore bd->d_open_reg[otyp] &= ~mask;
12783f7d54a6SGarrett D'Amore }
12793f7d54a6SGarrett D'Amore for (int i = 0; i < 64; i++) {
12803f7d54a6SGarrett D'Amore if (bd->d_open_lyr[part]) {
12813f7d54a6SGarrett D'Amore last = B_FALSE;
12823f7d54a6SGarrett D'Amore }
12833f7d54a6SGarrett D'Amore }
12843f7d54a6SGarrett D'Amore for (int i = 0; last && (i < OTYP_LYR); i++) {
12853f7d54a6SGarrett D'Amore if (bd->d_open_reg[i]) {
12863f7d54a6SGarrett D'Amore last = B_FALSE;
12873f7d54a6SGarrett D'Amore }
12883f7d54a6SGarrett D'Amore }
12893f7d54a6SGarrett D'Amore mutex_exit(&bd->d_ocmutex);
12903f7d54a6SGarrett D'Amore
12913f7d54a6SGarrett D'Amore if (last) {
129286e3bca6SGarrett D'Amore cmlb_invalidate(bd->d_cmlbh, 0);
12933f7d54a6SGarrett D'Amore }
12943f7d54a6SGarrett D'Amore rw_exit(&bd_lock);
12953f7d54a6SGarrett D'Amore
12963f7d54a6SGarrett D'Amore return (0);
12973f7d54a6SGarrett D'Amore }
12983f7d54a6SGarrett D'Amore
129986e3bca6SGarrett D'Amore static int
bd_dump(dev_t dev,caddr_t caddr,daddr_t blkno,int nblk)130086e3bca6SGarrett D'Amore bd_dump(dev_t dev, caddr_t caddr, daddr_t blkno, int nblk)
130186e3bca6SGarrett D'Amore {
130286e3bca6SGarrett D'Amore minor_t inst;
130386e3bca6SGarrett D'Amore minor_t part;
130486e3bca6SGarrett D'Amore diskaddr_t pstart;
130586e3bca6SGarrett D'Amore diskaddr_t psize;
130686e3bca6SGarrett D'Amore bd_t *bd;
130786e3bca6SGarrett D'Amore bd_xfer_impl_t *xi;
130886e3bca6SGarrett D'Amore buf_t *bp;
130986e3bca6SGarrett D'Amore int rv;
1310c0591a0cSYouzhong Yang uint32_t shift;
1311c0591a0cSYouzhong Yang daddr_t d_blkno;
1312c0591a0cSYouzhong Yang int d_nblk;
131386e3bca6SGarrett D'Amore
131486e3bca6SGarrett D'Amore rw_enter(&bd_lock, RW_READER);
131586e3bca6SGarrett D'Amore
131686e3bca6SGarrett D'Amore part = BDPART(dev);
131786e3bca6SGarrett D'Amore inst = BDINST(dev);
131886e3bca6SGarrett D'Amore
131986e3bca6SGarrett D'Amore if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
132086e3bca6SGarrett D'Amore rw_exit(&bd_lock);
132186e3bca6SGarrett D'Amore return (ENXIO);
132286e3bca6SGarrett D'Amore }
1323c0591a0cSYouzhong Yang shift = bd->d_blkshift;
1324c0591a0cSYouzhong Yang d_blkno = blkno >> (shift - DEV_BSHIFT);
1325c0591a0cSYouzhong Yang d_nblk = nblk >> (shift - DEV_BSHIFT);
132686e3bca6SGarrett D'Amore /*
132786e3bca6SGarrett D'Amore * do cmlb, but do it synchronously unless we already have the
132886e3bca6SGarrett D'Amore * partition (which we probably should.)
132986e3bca6SGarrett D'Amore */
133086e3bca6SGarrett D'Amore if (cmlb_partinfo(bd->d_cmlbh, part, &psize, &pstart, NULL, NULL,
133186e3bca6SGarrett D'Amore (void *)1)) {
133286e3bca6SGarrett D'Amore rw_exit(&bd_lock);
133386e3bca6SGarrett D'Amore return (ENXIO);
133486e3bca6SGarrett D'Amore }
133586e3bca6SGarrett D'Amore
1336c0591a0cSYouzhong Yang if ((d_blkno + d_nblk) > psize) {
133786e3bca6SGarrett D'Amore rw_exit(&bd_lock);
133886e3bca6SGarrett D'Amore return (EINVAL);
133986e3bca6SGarrett D'Amore }
134086e3bca6SGarrett D'Amore bp = getrbuf(KM_NOSLEEP);
134186e3bca6SGarrett D'Amore if (bp == NULL) {
134286e3bca6SGarrett D'Amore rw_exit(&bd_lock);
134386e3bca6SGarrett D'Amore return (ENOMEM);
134486e3bca6SGarrett D'Amore }
134586e3bca6SGarrett D'Amore
1346c0591a0cSYouzhong Yang bp->b_bcount = nblk << DEV_BSHIFT;
134786e3bca6SGarrett D'Amore bp->b_resid = bp->b_bcount;
134886e3bca6SGarrett D'Amore bp->b_lblkno = blkno;
134986e3bca6SGarrett D'Amore bp->b_un.b_addr = caddr;
135086e3bca6SGarrett D'Amore
135186e3bca6SGarrett D'Amore xi = bd_xfer_alloc(bd, bp, bd->d_ops.o_write, KM_NOSLEEP);
135286e3bca6SGarrett D'Amore if (xi == NULL) {
135386e3bca6SGarrett D'Amore rw_exit(&bd_lock);
135486e3bca6SGarrett D'Amore freerbuf(bp);
135586e3bca6SGarrett D'Amore return (ENOMEM);
135686e3bca6SGarrett D'Amore }
1357c0591a0cSYouzhong Yang xi->i_blkno = d_blkno + pstart;
135886e3bca6SGarrett D'Amore xi->i_flags = BD_XFER_POLL;
135986e3bca6SGarrett D'Amore bd_submit(bd, xi);
136086e3bca6SGarrett D'Amore rw_exit(&bd_lock);
136186e3bca6SGarrett D'Amore
136286e3bca6SGarrett D'Amore /*
136386e3bca6SGarrett D'Amore * Generally, we should have run this entirely synchronously
136486e3bca6SGarrett D'Amore * at this point and the biowait call should be a no-op. If
136586e3bca6SGarrett D'Amore * it didn't happen this way, it's a bug in the underlying
136686e3bca6SGarrett D'Amore * driver not honoring BD_XFER_POLL.
136786e3bca6SGarrett D'Amore */
136886e3bca6SGarrett D'Amore (void) biowait(bp);
136986e3bca6SGarrett D'Amore rv = geterror(bp);
137086e3bca6SGarrett D'Amore freerbuf(bp);
137186e3bca6SGarrett D'Amore return (rv);
137286e3bca6SGarrett D'Amore }
137386e3bca6SGarrett D'Amore
13749d75dfdaSAlexey Zaytsev void
bd_minphys(struct buf * bp)13759d75dfdaSAlexey Zaytsev bd_minphys(struct buf *bp)
13769d75dfdaSAlexey Zaytsev {
13779d75dfdaSAlexey Zaytsev minor_t inst;
13789d75dfdaSAlexey Zaytsev bd_t *bd;
13799d75dfdaSAlexey Zaytsev inst = BDINST(bp->b_edev);
13809d75dfdaSAlexey Zaytsev
13819d75dfdaSAlexey Zaytsev bd = ddi_get_soft_state(bd_state, inst);
13829d75dfdaSAlexey Zaytsev
13839d75dfdaSAlexey Zaytsev /*
13849d75dfdaSAlexey Zaytsev * In a non-debug kernel, bd_strategy will catch !bd as
13859d75dfdaSAlexey Zaytsev * well, and will fail nicely.
13869d75dfdaSAlexey Zaytsev */
13879d75dfdaSAlexey Zaytsev ASSERT(bd);
13889d75dfdaSAlexey Zaytsev
13899d75dfdaSAlexey Zaytsev if (bp->b_bcount > bd->d_maxxfer)
13909d75dfdaSAlexey Zaytsev bp->b_bcount = bd->d_maxxfer;
13919d75dfdaSAlexey Zaytsev }
13929d75dfdaSAlexey Zaytsev
1393c0591a0cSYouzhong Yang static int
bd_check_uio(dev_t dev,struct uio * uio)1394c0591a0cSYouzhong Yang bd_check_uio(dev_t dev, struct uio *uio)
1395c0591a0cSYouzhong Yang {
1396c0591a0cSYouzhong Yang bd_t *bd;
1397c0591a0cSYouzhong Yang uint32_t shift;
1398c0591a0cSYouzhong Yang
1399c0591a0cSYouzhong Yang if ((bd = ddi_get_soft_state(bd_state, BDINST(dev))) == NULL) {
1400c0591a0cSYouzhong Yang return (ENXIO);
1401c0591a0cSYouzhong Yang }
1402c0591a0cSYouzhong Yang
1403c0591a0cSYouzhong Yang shift = bd->d_blkshift;
1404c0591a0cSYouzhong Yang if ((P2PHASE(uio->uio_loffset, (1U << shift)) != 0) ||
1405c0591a0cSYouzhong Yang (P2PHASE(uio->uio_iov->iov_len, (1U << shift)) != 0)) {
1406c0591a0cSYouzhong Yang return (EINVAL);
1407c0591a0cSYouzhong Yang }
1408c0591a0cSYouzhong Yang
1409c0591a0cSYouzhong Yang return (0);
1410c0591a0cSYouzhong Yang }
1411c0591a0cSYouzhong Yang
14123f7d54a6SGarrett D'Amore static int
bd_read(dev_t dev,struct uio * uio,cred_t * credp)14133f7d54a6SGarrett D'Amore bd_read(dev_t dev, struct uio *uio, cred_t *credp)
14143f7d54a6SGarrett D'Amore {
14153f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(credp));
1416c0591a0cSYouzhong Yang int ret = bd_check_uio(dev, uio);
1417c0591a0cSYouzhong Yang if (ret != 0) {
1418c0591a0cSYouzhong Yang return (ret);
1419c0591a0cSYouzhong Yang }
14209d75dfdaSAlexey Zaytsev return (physio(bd_strategy, NULL, dev, B_READ, bd_minphys, uio));
14213f7d54a6SGarrett D'Amore }
14223f7d54a6SGarrett D'Amore
14233f7d54a6SGarrett D'Amore static int
bd_write(dev_t dev,struct uio * uio,cred_t * credp)14243f7d54a6SGarrett D'Amore bd_write(dev_t dev, struct uio *uio, cred_t *credp)
14253f7d54a6SGarrett D'Amore {
14263f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(credp));
1427c0591a0cSYouzhong Yang int ret = bd_check_uio(dev, uio);
1428c0591a0cSYouzhong Yang if (ret != 0) {
1429c0591a0cSYouzhong Yang return (ret);
1430c0591a0cSYouzhong Yang }
14319d75dfdaSAlexey Zaytsev return (physio(bd_strategy, NULL, dev, B_WRITE, bd_minphys, uio));
14323f7d54a6SGarrett D'Amore }
14333f7d54a6SGarrett D'Amore
14343f7d54a6SGarrett D'Amore static int
bd_aread(dev_t dev,struct aio_req * aio,cred_t * credp)14353f7d54a6SGarrett D'Amore bd_aread(dev_t dev, struct aio_req *aio, cred_t *credp)
14363f7d54a6SGarrett D'Amore {
14373f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(credp));
1438c0591a0cSYouzhong Yang int ret = bd_check_uio(dev, aio->aio_uio);
1439c0591a0cSYouzhong Yang if (ret != 0) {
1440c0591a0cSYouzhong Yang return (ret);
1441c0591a0cSYouzhong Yang }
14429d75dfdaSAlexey Zaytsev return (aphysio(bd_strategy, anocancel, dev, B_READ, bd_minphys, aio));
14433f7d54a6SGarrett D'Amore }
14443f7d54a6SGarrett D'Amore
14453f7d54a6SGarrett D'Amore static int
bd_awrite(dev_t dev,struct aio_req * aio,cred_t * credp)14463f7d54a6SGarrett D'Amore bd_awrite(dev_t dev, struct aio_req *aio, cred_t *credp)
14473f7d54a6SGarrett D'Amore {
14483f7d54a6SGarrett D'Amore _NOTE(ARGUNUSED(credp));
1449c0591a0cSYouzhong Yang int ret = bd_check_uio(dev, aio->aio_uio);
1450c0591a0cSYouzhong Yang if (ret != 0) {
1451c0591a0cSYouzhong Yang return (ret);
1452c0591a0cSYouzhong Yang }
14539d75dfdaSAlexey Zaytsev return (aphysio(bd_strategy, anocancel, dev, B_WRITE, bd_minphys, aio));
14543f7d54a6SGarrett D'Amore }
14553f7d54a6SGarrett D'Amore
14563f7d54a6SGarrett D'Amore static int
bd_strategy(struct buf * bp)14573f7d54a6SGarrett D'Amore bd_strategy(struct buf *bp)
14583f7d54a6SGarrett D'Amore {
14593f7d54a6SGarrett D'Amore minor_t inst;
14603f7d54a6SGarrett D'Amore minor_t part;
14613f7d54a6SGarrett D'Amore bd_t *bd;
14623f7d54a6SGarrett D'Amore diskaddr_t p_lba;
14633f7d54a6SGarrett D'Amore diskaddr_t p_nblks;
14643f7d54a6SGarrett D'Amore diskaddr_t b_nblks;
14653f7d54a6SGarrett D'Amore bd_xfer_impl_t *xi;
14663f7d54a6SGarrett D'Amore uint32_t shift;
14673f7d54a6SGarrett D'Amore int (*func)(void *, bd_xfer_t *);
14684d95620bSPaul Winder diskaddr_t lblkno;
14693f7d54a6SGarrett D'Amore
14703f7d54a6SGarrett D'Amore part = BDPART(bp->b_edev);
14713f7d54a6SGarrett D'Amore inst = BDINST(bp->b_edev);
14723f7d54a6SGarrett D'Amore
14733f7d54a6SGarrett D'Amore ASSERT(bp);
14743f7d54a6SGarrett D'Amore
14753f7d54a6SGarrett D'Amore bp->b_resid = bp->b_bcount;
14763f7d54a6SGarrett D'Amore
14773f7d54a6SGarrett D'Amore if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
14783f7d54a6SGarrett D'Amore bioerror(bp, ENXIO);
14793f7d54a6SGarrett D'Amore biodone(bp);
14803f7d54a6SGarrett D'Amore return (0);
14813f7d54a6SGarrett D'Amore }
14823f7d54a6SGarrett D'Amore
14833f7d54a6SGarrett D'Amore if (cmlb_partinfo(bd->d_cmlbh, part, &p_nblks, &p_lba,
148486e3bca6SGarrett D'Amore NULL, NULL, 0)) {
14853f7d54a6SGarrett D'Amore bioerror(bp, ENXIO);
14863f7d54a6SGarrett D'Amore biodone(bp);
14873f7d54a6SGarrett D'Amore return (0);
14883f7d54a6SGarrett D'Amore }
14893f7d54a6SGarrett D'Amore
14903f7d54a6SGarrett D'Amore shift = bd->d_blkshift;
1491c0591a0cSYouzhong Yang lblkno = bp->b_lblkno >> (shift - DEV_BSHIFT);
1492c0591a0cSYouzhong Yang if ((P2PHASE(bp->b_lblkno, (1U << (shift - DEV_BSHIFT))) != 0) ||
1493c0591a0cSYouzhong Yang (P2PHASE(bp->b_bcount, (1U << shift)) != 0) ||
1494c0591a0cSYouzhong Yang (lblkno > p_nblks)) {
1495c0591a0cSYouzhong Yang bioerror(bp, EINVAL);
14963f7d54a6SGarrett D'Amore biodone(bp);
14973f7d54a6SGarrett D'Amore return (0);
14983f7d54a6SGarrett D'Amore }
149904904ca2SDan McDonald b_nblks = bp->b_bcount >> shift;
1500c0591a0cSYouzhong Yang if ((lblkno == p_nblks) || (bp->b_bcount == 0)) {
15013f7d54a6SGarrett D'Amore biodone(bp);
15023f7d54a6SGarrett D'Amore return (0);
15033f7d54a6SGarrett D'Amore }
15043f7d54a6SGarrett D'Amore
1505c0591a0cSYouzhong Yang if ((b_nblks + lblkno) > p_nblks) {
1506c0591a0cSYouzhong Yang bp->b_resid = ((lblkno + b_nblks - p_nblks) << shift);
15073f7d54a6SGarrett D'Amore bp->b_bcount -= bp->b_resid;
15083f7d54a6SGarrett D'Amore } else {
15093f7d54a6SGarrett D'Amore bp->b_resid = 0;
15103f7d54a6SGarrett D'Amore }
15113f7d54a6SGarrett D'Amore func = (bp->b_flags & B_READ) ? bd->d_ops.o_read : bd->d_ops.o_write;
15123f7d54a6SGarrett D'Amore
15133f7d54a6SGarrett D'Amore xi = bd_xfer_alloc(bd, bp, func, KM_NOSLEEP);
15143f7d54a6SGarrett D'Amore if (xi == NULL) {
15153f7d54a6SGarrett D'Amore xi = bd_xfer_alloc(bd, bp, func, KM_PUSHPAGE);
15163f7d54a6SGarrett D'Amore }
15173f7d54a6SGarrett D'Amore if (xi == NULL) {
15183f7d54a6SGarrett D'Amore /* bd_request_alloc will have done bioerror */
15193f7d54a6SGarrett D'Amore biodone(bp);
15203f7d54a6SGarrett D'Amore return (0);
15213f7d54a6SGarrett D'Amore }
1522c0591a0cSYouzhong Yang xi->i_blkno = lblkno + p_lba;
15233f7d54a6SGarrett D'Amore
15243f7d54a6SGarrett D'Amore bd_submit(bd, xi);
15253f7d54a6SGarrett D'Amore
15263f7d54a6SGarrett D'Amore return (0);
15273f7d54a6SGarrett D'Amore }
15283f7d54a6SGarrett D'Amore
15293f7d54a6SGarrett D'Amore static int
bd_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * credp,int * rvalp)15303f7d54a6SGarrett D'Amore bd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *credp, int *rvalp)
15313f7d54a6SGarrett D'Amore {
15323f7d54a6SGarrett D'Amore minor_t inst;
15333f7d54a6SGarrett D'Amore uint16_t part;
15343f7d54a6SGarrett D'Amore bd_t *bd;
15353f7d54a6SGarrett D'Amore void *ptr = (void *)arg;
15363f7d54a6SGarrett D'Amore int rv;
15373f7d54a6SGarrett D'Amore
15383f7d54a6SGarrett D'Amore part = BDPART(dev);
15393f7d54a6SGarrett D'Amore inst = BDINST(dev);
15403f7d54a6SGarrett D'Amore
15413f7d54a6SGarrett D'Amore if ((bd = ddi_get_soft_state(bd_state, inst)) == NULL) {
15423f7d54a6SGarrett D'Amore return (ENXIO);
15433f7d54a6SGarrett D'Amore }
15443f7d54a6SGarrett D'Amore
154586e3bca6SGarrett D'Amore rv = cmlb_ioctl(bd->d_cmlbh, dev, cmd, arg, flag, credp, rvalp, 0);
15463f7d54a6SGarrett D'Amore if (rv != ENOTTY)
15473f7d54a6SGarrett D'Amore return (rv);
15483f7d54a6SGarrett D'Amore
1549a0cb694bSSteve Ma if (rvalp != NULL) {
1550a0cb694bSSteve Ma /* the return value of the ioctl is 0 by default */
1551a0cb694bSSteve Ma *rvalp = 0;
1552a0cb694bSSteve Ma }
1553a0cb694bSSteve Ma
15543f7d54a6SGarrett D'Amore switch (cmd) {
15553f7d54a6SGarrett D'Amore case DKIOCGMEDIAINFO: {
15563f7d54a6SGarrett D'Amore struct dk_minfo minfo;
15573f7d54a6SGarrett D'Amore
15583f7d54a6SGarrett D'Amore /* make sure our state information is current */
15593f7d54a6SGarrett D'Amore bd_update_state(bd);
15603f7d54a6SGarrett D'Amore bzero(&minfo, sizeof (minfo));
15613f7d54a6SGarrett D'Amore minfo.dki_media_type = DK_FIXED_DISK;
15623f7d54a6SGarrett D'Amore minfo.dki_lbsize = (1U << bd->d_blkshift);
15633f7d54a6SGarrett D'Amore minfo.dki_capacity = bd->d_numblks;
1564dba604f9SDan McDonald if (ddi_copyout(&minfo, ptr, sizeof (minfo), flag)) {
1565dba604f9SDan McDonald return (EFAULT);
1566dba604f9SDan McDonald }
1567dba604f9SDan McDonald return (0);
1568dba604f9SDan McDonald }
1569dba604f9SDan McDonald case DKIOCGMEDIAINFOEXT: {
1570dba604f9SDan McDonald struct dk_minfo_ext miext;
15717b421453SRobert Mustacchi size_t len;
1572dba604f9SDan McDonald
1573dba604f9SDan McDonald /* make sure our state information is current */
1574dba604f9SDan McDonald bd_update_state(bd);
1575dba604f9SDan McDonald bzero(&miext, sizeof (miext));
1576dba604f9SDan McDonald miext.dki_media_type = DK_FIXED_DISK;
1577dba604f9SDan McDonald miext.dki_lbsize = (1U << bd->d_blkshift);
157832ce6b81SHans Rosenfeld miext.dki_pbsize = (1U << bd->d_pblkshift);
1579dba604f9SDan McDonald miext.dki_capacity = bd->d_numblks;
15807b421453SRobert Mustacchi
15817b421453SRobert Mustacchi switch (ddi_model_convert_from(flag & FMODELS)) {
15827b421453SRobert Mustacchi case DDI_MODEL_ILP32:
15837b421453SRobert Mustacchi len = sizeof (struct dk_minfo_ext32);
15847b421453SRobert Mustacchi break;
15857b421453SRobert Mustacchi default:
15867b421453SRobert Mustacchi len = sizeof (struct dk_minfo_ext);
15877b421453SRobert Mustacchi break;
15887b421453SRobert Mustacchi }
15897b421453SRobert Mustacchi
15907b421453SRobert Mustacchi if (ddi_copyout(&miext, ptr, len, flag)) {
15913f7d54a6SGarrett D'Amore return (EFAULT);
15923f7d54a6SGarrett D'Amore }
15933f7d54a6SGarrett D'Amore return (0);
15943f7d54a6SGarrett D'Amore }
15953f7d54a6SGarrett D'Amore case DKIOCINFO: {
15963f7d54a6SGarrett D'Amore struct dk_cinfo cinfo;
15973f7d54a6SGarrett D'Amore bzero(&cinfo, sizeof (cinfo));
15983f7d54a6SGarrett D'Amore cinfo.dki_ctype = DKC_BLKDEV;
15993f7d54a6SGarrett D'Amore cinfo.dki_cnum = ddi_get_instance(ddi_get_parent(bd->d_dip));
16003f7d54a6SGarrett D'Amore (void) snprintf(cinfo.dki_cname, sizeof (cinfo.dki_cname),
16013f7d54a6SGarrett D'Amore "%s", ddi_driver_name(ddi_get_parent(bd->d_dip)));
16023f7d54a6SGarrett D'Amore (void) snprintf(cinfo.dki_dname, sizeof (cinfo.dki_dname),
16033f7d54a6SGarrett D'Amore "%s", ddi_driver_name(bd->d_dip));
16043f7d54a6SGarrett D'Amore cinfo.dki_unit = inst;
16053f7d54a6SGarrett D'Amore cinfo.dki_flags = DKI_FMTVOL;
16063f7d54a6SGarrett D'Amore cinfo.dki_partition = part;
16073f7d54a6SGarrett D'Amore cinfo.dki_maxtransfer = bd->d_maxxfer / DEV_BSIZE;
16083f7d54a6SGarrett D'Amore cinfo.dki_addr = 0;
16093f7d54a6SGarrett D'Amore cinfo.dki_slave = 0;
16103f7d54a6SGarrett D'Amore cinfo.dki_space = 0;
16113f7d54a6SGarrett D'Amore cinfo.dki_prio = 0;
16123f7d54a6SGarrett D'Amore cinfo.dki_vec = 0;
1613dba604f9SDan McDonald if (ddi_copyout(&cinfo, ptr, sizeof (cinfo), flag)) {
16143f7d54a6SGarrett D'Amore return (EFAULT);
16153f7d54a6SGarrett D'Amore }
16163f7d54a6SGarrett D'Amore return (0);
16173f7d54a6SGarrett D'Amore }
16183f7d54a6SGarrett D'Amore case DKIOCREMOVABLE: {
16193f7d54a6SGarrett D'Amore int i;
16203f7d54a6SGarrett D'Amore i = bd->d_removable ? 1 : 0;
16213f7d54a6SGarrett D'Amore if (ddi_copyout(&i, ptr, sizeof (i), flag)) {
16223f7d54a6SGarrett D'Amore return (EFAULT);
16233f7d54a6SGarrett D'Amore }
16243f7d54a6SGarrett D'Amore return (0);
16253f7d54a6SGarrett D'Amore }
16263f7d54a6SGarrett D'Amore case DKIOCHOTPLUGGABLE: {
16273f7d54a6SGarrett D'Amore int i;
16283f7d54a6SGarrett D'Amore i = bd->d_hotpluggable ? 1 : 0;
16293f7d54a6SGarrett D'Amore if (ddi_copyout(&i, ptr, sizeof (i), flag)) {
16303f7d54a6SGarrett D'Amore return (EFAULT);
16313f7d54a6SGarrett D'Amore }
16323f7d54a6SGarrett D'Amore return (0);
16333f7d54a6SGarrett D'Amore }
16343f7d54a6SGarrett D'Amore case DKIOCREADONLY: {
16353f7d54a6SGarrett D'Amore int i;
16363f7d54a6SGarrett D'Amore i = bd->d_rdonly ? 1 : 0;
16373f7d54a6SGarrett D'Amore if (ddi_copyout(&i, ptr, sizeof (i), flag)) {
16383f7d54a6SGarrett D'Amore return (EFAULT);
16393f7d54a6SGarrett D'Amore }
16403f7d54a6SGarrett D'Amore return (0);
16413f7d54a6SGarrett D'Amore }
164259d8f100SGarrett D'Amore case DKIOCSOLIDSTATE: {
164359d8f100SGarrett D'Amore int i;
164459d8f100SGarrett D'Amore i = bd->d_ssd ? 1 : 0;
164559d8f100SGarrett D'Amore if (ddi_copyout(&i, ptr, sizeof (i), flag)) {
164659d8f100SGarrett D'Amore return (EFAULT);
164759d8f100SGarrett D'Amore }
164859d8f100SGarrett D'Amore return (0);
164959d8f100SGarrett D'Amore }
16503f7d54a6SGarrett D'Amore case DKIOCSTATE: {
16513f7d54a6SGarrett D'Amore enum dkio_state state;
16523f7d54a6SGarrett D'Amore if (ddi_copyin(ptr, &state, sizeof (state), flag)) {
16533f7d54a6SGarrett D'Amore return (EFAULT);
16543f7d54a6SGarrett D'Amore }
16553f7d54a6SGarrett D'Amore if ((rv = bd_check_state(bd, &state)) != 0) {
16563f7d54a6SGarrett D'Amore return (rv);
16573f7d54a6SGarrett D'Amore }
16583f7d54a6SGarrett D'Amore if (ddi_copyout(&state, ptr, sizeof (state), flag)) {
16593f7d54a6SGarrett D'Amore return (EFAULT);
16603f7d54a6SGarrett D'Amore }
16613f7d54a6SGarrett D'Amore return (0);
16623f7d54a6SGarrett D'Amore }
16633f7d54a6SGarrett D'Amore case DKIOCFLUSHWRITECACHE: {
1664f097ef9cSAlexey Zaytsev struct dk_callback *dkc = NULL;
1665f097ef9cSAlexey Zaytsev
1666f097ef9cSAlexey Zaytsev if (flag & FKIOCTL)
1667f097ef9cSAlexey Zaytsev dkc = (void *)arg;
16683f7d54a6SGarrett D'Amore
16693f7d54a6SGarrett D'Amore rv = bd_flush_write_cache(bd, dkc);
16703f7d54a6SGarrett D'Amore return (rv);
16713f7d54a6SGarrett D'Amore }
16721a5ae140SJason King case DKIOCFREE: {
16731a5ae140SJason King dkioc_free_list_t *dfl = NULL;
16741a5ae140SJason King
16751a5ae140SJason King /*
16761a5ae140SJason King * Check free space support early to avoid copyin/allocation
16771a5ae140SJason King * when unnecessary.
16781a5ae140SJason King */
16791a5ae140SJason King if (!CAN_FREESPACE(bd))
16801a5ae140SJason King return (ENOTSUP);
16811a5ae140SJason King
16821a5ae140SJason King rv = dfl_copyin(ptr, &dfl, flag, KM_SLEEP);
16831a5ae140SJason King if (rv != 0)
16841a5ae140SJason King return (rv);
16851a5ae140SJason King
16861a5ae140SJason King /*
16871a5ae140SJason King * bd_free_space() consumes 'dfl'. bd_free_space() will
16881a5ae140SJason King * call dfl_iter() which will normally try to pass dfl through
16891a5ae140SJason King * to bd_free_space_cb() which attaches dfl to the bd_xfer_t
16901a5ae140SJason King * that is then queued for the underlying driver. Once the
16911a5ae140SJason King * driver processes the request, the bd_xfer_t instance is
16921a5ae140SJason King * disposed of, including any attached dkioc_free_list_t.
16931a5ae140SJason King *
16941a5ae140SJason King * If dfl cannot be processed by the underlying driver due to
16951a5ae140SJason King * size or alignment requirements of the driver, dfl_iter()
16961a5ae140SJason King * will replace dfl with one or more new dkioc_free_list_t
16971a5ae140SJason King * instances with the correct alignment and sizes for the driver
16981a5ae140SJason King * (and free the original dkioc_free_list_t).
16991a5ae140SJason King */
17001a5ae140SJason King rv = bd_free_space(dev, bd, dfl);
17011a5ae140SJason King return (rv);
17021a5ae140SJason King }
17031a5ae140SJason King
17041a5ae140SJason King case DKIOC_CANFREE: {
17051a5ae140SJason King boolean_t supported = CAN_FREESPACE(bd);
17061a5ae140SJason King
17071a5ae140SJason King if (ddi_copyout(&supported, (void *)arg, sizeof (supported),
17081a5ae140SJason King flag) != 0) {
17091a5ae140SJason King return (EFAULT);
17101a5ae140SJason King }
17111a5ae140SJason King
17121a5ae140SJason King return (0);
17131a5ae140SJason King }
17143f7d54a6SGarrett D'Amore
17153f7d54a6SGarrett D'Amore default:
17163f7d54a6SGarrett D'Amore break;
17173f7d54a6SGarrett D'Amore
17183f7d54a6SGarrett D'Amore }
17193f7d54a6SGarrett D'Amore return (ENOTTY);
17203f7d54a6SGarrett D'Amore }
17213f7d54a6SGarrett D'Amore
17223f7d54a6SGarrett D'Amore static int
bd_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int mod_flags,char * name,caddr_t valuep,int * lengthp)17233f7d54a6SGarrett D'Amore bd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
17243f7d54a6SGarrett D'Amore char *name, caddr_t valuep, int *lengthp)
17253f7d54a6SGarrett D'Amore {
17263f7d54a6SGarrett D'Amore bd_t *bd;
17273f7d54a6SGarrett D'Amore
17283f7d54a6SGarrett D'Amore bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip));
17293f7d54a6SGarrett D'Amore if (bd == NULL)
17303f7d54a6SGarrett D'Amore return (ddi_prop_op(dev, dip, prop_op, mod_flags,
17313f7d54a6SGarrett D'Amore name, valuep, lengthp));
17323f7d54a6SGarrett D'Amore
17333f7d54a6SGarrett D'Amore return (cmlb_prop_op(bd->d_cmlbh, dev, dip, prop_op, mod_flags, name,
173486e3bca6SGarrett D'Amore valuep, lengthp, BDPART(dev), 0));
17353f7d54a6SGarrett D'Amore }
17363f7d54a6SGarrett D'Amore
17373f7d54a6SGarrett D'Amore
17383f7d54a6SGarrett D'Amore static int
bd_tg_rdwr(dev_info_t * dip,uchar_t cmd,void * bufaddr,diskaddr_t start,size_t length,void * tg_cookie)17393f7d54a6SGarrett D'Amore bd_tg_rdwr(dev_info_t *dip, uchar_t cmd, void *bufaddr, diskaddr_t start,
17403f7d54a6SGarrett D'Amore size_t length, void *tg_cookie)
17413f7d54a6SGarrett D'Amore {
17423f7d54a6SGarrett D'Amore bd_t *bd;
17433f7d54a6SGarrett D'Amore buf_t *bp;
17443f7d54a6SGarrett D'Amore bd_xfer_impl_t *xi;
17453f7d54a6SGarrett D'Amore int rv;
17463f7d54a6SGarrett D'Amore int (*func)(void *, bd_xfer_t *);
174786e3bca6SGarrett D'Amore int kmflag;
17483f7d54a6SGarrett D'Amore
174986e3bca6SGarrett D'Amore /*
175086e3bca6SGarrett D'Amore * If we are running in polled mode (such as during dump(9e)
175186e3bca6SGarrett D'Amore * execution), then we cannot sleep for kernel allocations.
175286e3bca6SGarrett D'Amore */
175386e3bca6SGarrett D'Amore kmflag = tg_cookie ? KM_NOSLEEP : KM_SLEEP;
17543f7d54a6SGarrett D'Amore
175586e3bca6SGarrett D'Amore bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip));
17563f7d54a6SGarrett D'Amore
175704904ca2SDan McDonald if (P2PHASE(length, (1U << bd->d_blkshift)) != 0) {
17583f7d54a6SGarrett D'Amore /* We can only transfer whole blocks at a time! */
17593f7d54a6SGarrett D'Amore return (EINVAL);
17603f7d54a6SGarrett D'Amore }
17613f7d54a6SGarrett D'Amore
176286e3bca6SGarrett D'Amore if ((bp = getrbuf(kmflag)) == NULL) {
176386e3bca6SGarrett D'Amore return (ENOMEM);
176486e3bca6SGarrett D'Amore }
17653f7d54a6SGarrett D'Amore
17663f7d54a6SGarrett D'Amore switch (cmd) {
17673f7d54a6SGarrett D'Amore case TG_READ:
17683f7d54a6SGarrett D'Amore bp->b_flags = B_READ;
17693f7d54a6SGarrett D'Amore func = bd->d_ops.o_read;
17703f7d54a6SGarrett D'Amore break;
17713f7d54a6SGarrett D'Amore case TG_WRITE:
17723f7d54a6SGarrett D'Amore bp->b_flags = B_WRITE;
17733f7d54a6SGarrett D'Amore func = bd->d_ops.o_write;
17743f7d54a6SGarrett D'Amore break;
17753f7d54a6SGarrett D'Amore default:
17763f7d54a6SGarrett D'Amore freerbuf(bp);
17773f7d54a6SGarrett D'Amore return (EINVAL);
17783f7d54a6SGarrett D'Amore }
17793f7d54a6SGarrett D'Amore
17803f7d54a6SGarrett D'Amore bp->b_un.b_addr = bufaddr;
17813f7d54a6SGarrett D'Amore bp->b_bcount = length;
178286e3bca6SGarrett D'Amore xi = bd_xfer_alloc(bd, bp, func, kmflag);
17833f7d54a6SGarrett D'Amore if (xi == NULL) {
17843f7d54a6SGarrett D'Amore rv = geterror(bp);
17853f7d54a6SGarrett D'Amore freerbuf(bp);
17863f7d54a6SGarrett D'Amore return (rv);
17873f7d54a6SGarrett D'Amore }
178886e3bca6SGarrett D'Amore xi->i_flags = tg_cookie ? BD_XFER_POLL : 0;
17893f7d54a6SGarrett D'Amore xi->i_blkno = start;
17903f7d54a6SGarrett D'Amore bd_submit(bd, xi);
17913f7d54a6SGarrett D'Amore (void) biowait(bp);
17923f7d54a6SGarrett D'Amore rv = geterror(bp);
17933f7d54a6SGarrett D'Amore freerbuf(bp);
17943f7d54a6SGarrett D'Amore
17953f7d54a6SGarrett D'Amore return (rv);
17963f7d54a6SGarrett D'Amore }
17973f7d54a6SGarrett D'Amore
17983f7d54a6SGarrett D'Amore static int
bd_tg_getinfo(dev_info_t * dip,int cmd,void * arg,void * tg_cookie)17993f7d54a6SGarrett D'Amore bd_tg_getinfo(dev_info_t *dip, int cmd, void *arg, void *tg_cookie)
18003f7d54a6SGarrett D'Amore {
18013f7d54a6SGarrett D'Amore bd_t *bd;
18023f7d54a6SGarrett D'Amore
180386e3bca6SGarrett D'Amore _NOTE(ARGUNUSED(tg_cookie));
180486e3bca6SGarrett D'Amore bd = ddi_get_soft_state(bd_state, ddi_get_instance(dip));
18053f7d54a6SGarrett D'Amore
18063f7d54a6SGarrett D'Amore switch (cmd) {
18073f7d54a6SGarrett D'Amore case TG_GETPHYGEOM:
18083f7d54a6SGarrett D'Amore case TG_GETVIRTGEOM:
18093f7d54a6SGarrett D'Amore /*
18103f7d54a6SGarrett D'Amore * We don't have any "geometry" as such, let cmlb
18113f7d54a6SGarrett D'Amore * fabricate something.
18123f7d54a6SGarrett D'Amore */
18133f7d54a6SGarrett D'Amore return (ENOTTY);
18143f7d54a6SGarrett D'Amore
18153f7d54a6SGarrett D'Amore case TG_GETCAPACITY:
18163f7d54a6SGarrett D'Amore bd_update_state(bd);
18173f7d54a6SGarrett D'Amore *(diskaddr_t *)arg = bd->d_numblks;
18183f7d54a6SGarrett D'Amore return (0);
18193f7d54a6SGarrett D'Amore
18203f7d54a6SGarrett D'Amore case TG_GETBLOCKSIZE:
18213f7d54a6SGarrett D'Amore *(uint32_t *)arg = (1U << bd->d_blkshift);
18223f7d54a6SGarrett D'Amore return (0);
18233f7d54a6SGarrett D'Amore
18243f7d54a6SGarrett D'Amore case TG_GETATTR:
18253f7d54a6SGarrett D'Amore /*
18263f7d54a6SGarrett D'Amore * It turns out that cmlb really doesn't do much for
18273f7d54a6SGarrett D'Amore * non-writable media, but lets make the information
18283f7d54a6SGarrett D'Amore * available for it in case it does more in the
18293f7d54a6SGarrett D'Amore * future. (The value is currently used for
18303f7d54a6SGarrett D'Amore * triggering special behavior for CD-ROMs.)
18313f7d54a6SGarrett D'Amore */
18323f7d54a6SGarrett D'Amore bd_update_state(bd);
18333f7d54a6SGarrett D'Amore ((tg_attribute_t *)arg)->media_is_writable =
18343f7d54a6SGarrett D'Amore bd->d_rdonly ? B_FALSE : B_TRUE;
183559d8f100SGarrett D'Amore ((tg_attribute_t *)arg)->media_is_solid_state = bd->d_ssd;
1836acb450ddSYuri Pankov ((tg_attribute_t *)arg)->media_is_rotational = B_FALSE;
18373f7d54a6SGarrett D'Amore return (0);
18383f7d54a6SGarrett D'Amore
18393f7d54a6SGarrett D'Amore default:
18403f7d54a6SGarrett D'Amore return (EINVAL);
18413f7d54a6SGarrett D'Amore }
18423f7d54a6SGarrett D'Amore }
18433f7d54a6SGarrett D'Amore
18443f7d54a6SGarrett D'Amore
18453f7d54a6SGarrett D'Amore static void
bd_sched(bd_t * bd,bd_queue_t * bq)18464d95620bSPaul Winder bd_sched(bd_t *bd, bd_queue_t *bq)
18473f7d54a6SGarrett D'Amore {
18483f7d54a6SGarrett D'Amore bd_xfer_impl_t *xi;
18493f7d54a6SGarrett D'Amore struct buf *bp;
18503f7d54a6SGarrett D'Amore int rv;
18513f7d54a6SGarrett D'Amore
18524d95620bSPaul Winder mutex_enter(&bq->q_iomutex);
18533f7d54a6SGarrett D'Amore
18544d95620bSPaul Winder while ((bq->q_qactive < bq->q_qsize) &&
18554d95620bSPaul Winder ((xi = list_remove_head(&bq->q_waitq)) != NULL)) {
185619687f06SPaul Winder mutex_enter(&bd->d_ksmutex);
185719687f06SPaul Winder kstat_waitq_to_runq(bd->d_kiop);
185819687f06SPaul Winder mutex_exit(&bd->d_ksmutex);
185919687f06SPaul Winder
18604d95620bSPaul Winder bq->q_qactive++;
18614d95620bSPaul Winder list_insert_tail(&bq->q_runq, xi);
18623f7d54a6SGarrett D'Amore
186386e3bca6SGarrett D'Amore /*
186486e3bca6SGarrett D'Amore * Submit the job to the driver. We drop the I/O mutex
186586e3bca6SGarrett D'Amore * so that we can deal with the case where the driver
186686e3bca6SGarrett D'Amore * completion routine calls back into us synchronously.
186786e3bca6SGarrett D'Amore */
186886e3bca6SGarrett D'Amore
18694d95620bSPaul Winder mutex_exit(&bq->q_iomutex);
18704d95620bSPaul Winder
18713f7d54a6SGarrett D'Amore rv = xi->i_func(bd->d_private, &xi->i_public);
18723f7d54a6SGarrett D'Amore if (rv != 0) {
18733f7d54a6SGarrett D'Amore bp = xi->i_bp;
18743f7d54a6SGarrett D'Amore bioerror(bp, rv);
18753f7d54a6SGarrett D'Amore biodone(bp);
187686e3bca6SGarrett D'Amore
1877bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_transerrs.value.ui32);
187819687f06SPaul Winder
187919687f06SPaul Winder mutex_enter(&bq->q_iomutex);
188019687f06SPaul Winder
18814d95620bSPaul Winder mutex_enter(&bd->d_ksmutex);
188286e3bca6SGarrett D'Amore kstat_runq_exit(bd->d_kiop);
18834d95620bSPaul Winder mutex_exit(&bd->d_ksmutex);
18844d95620bSPaul Winder
18854d95620bSPaul Winder bq->q_qactive--;
18864d95620bSPaul Winder list_remove(&bq->q_runq, xi);
1887dd3928f8SHans Rosenfeld bd_xfer_free(xi);
188886e3bca6SGarrett D'Amore } else {
18894d95620bSPaul Winder mutex_enter(&bq->q_iomutex);
18903f7d54a6SGarrett D'Amore }
18913f7d54a6SGarrett D'Amore }
189286e3bca6SGarrett D'Amore
18934d95620bSPaul Winder mutex_exit(&bq->q_iomutex);
18943f7d54a6SGarrett D'Amore }
18953f7d54a6SGarrett D'Amore
18963f7d54a6SGarrett D'Amore static void
bd_submit(bd_t * bd,bd_xfer_impl_t * xi)18973f7d54a6SGarrett D'Amore bd_submit(bd_t *bd, bd_xfer_impl_t *xi)
18983f7d54a6SGarrett D'Amore {
18994d95620bSPaul Winder uint64_t nv = atomic_inc_64_nv(&bd->d_io_counter);
19004d95620bSPaul Winder unsigned q = nv % bd->d_qcount;
19014d95620bSPaul Winder bd_queue_t *bq = &bd->d_queues[q];
19024d95620bSPaul Winder
19034d95620bSPaul Winder xi->i_bq = bq;
19044d95620bSPaul Winder xi->i_qnum = q;
19054d95620bSPaul Winder
19064d95620bSPaul Winder mutex_enter(&bq->q_iomutex);
190719687f06SPaul Winder
19084d95620bSPaul Winder list_insert_tail(&bq->q_waitq, xi);
19094d95620bSPaul Winder
19104d95620bSPaul Winder mutex_enter(&bd->d_ksmutex);
19113f7d54a6SGarrett D'Amore kstat_waitq_enter(bd->d_kiop);
19124d95620bSPaul Winder mutex_exit(&bd->d_ksmutex);
191386e3bca6SGarrett D'Amore
191419687f06SPaul Winder mutex_exit(&bq->q_iomutex);
191519687f06SPaul Winder
19164d95620bSPaul Winder bd_sched(bd, bq);
19173f7d54a6SGarrett D'Amore }
19183f7d54a6SGarrett D'Amore
19193f7d54a6SGarrett D'Amore static void
bd_runq_exit(bd_xfer_impl_t * xi,int err)19203f7d54a6SGarrett D'Amore bd_runq_exit(bd_xfer_impl_t *xi, int err)
19213f7d54a6SGarrett D'Amore {
19224d95620bSPaul Winder bd_t *bd = xi->i_bd;
19234d95620bSPaul Winder buf_t *bp = xi->i_bp;
19244d95620bSPaul Winder bd_queue_t *bq = xi->i_bq;
19253f7d54a6SGarrett D'Amore
19264d95620bSPaul Winder mutex_enter(&bq->q_iomutex);
19274d95620bSPaul Winder bq->q_qactive--;
19284d95620bSPaul Winder
19294d95620bSPaul Winder mutex_enter(&bd->d_ksmutex);
19303f7d54a6SGarrett D'Amore kstat_runq_exit(bd->d_kiop);
19314d95620bSPaul Winder mutex_exit(&bd->d_ksmutex);
193286e3bca6SGarrett D'Amore
193319687f06SPaul Winder list_remove(&bq->q_runq, xi);
193419687f06SPaul Winder mutex_exit(&bq->q_iomutex);
193519687f06SPaul Winder
19363f7d54a6SGarrett D'Amore if (err == 0) {
19373f7d54a6SGarrett D'Amore if (bp->b_flags & B_READ) {
19384d95620bSPaul Winder atomic_inc_uint(&bd->d_kiop->reads);
19394d95620bSPaul Winder atomic_add_64((uint64_t *)&bd->d_kiop->nread,
19404d95620bSPaul Winder bp->b_bcount - xi->i_resid);
19413f7d54a6SGarrett D'Amore } else {
19424d95620bSPaul Winder atomic_inc_uint(&bd->d_kiop->writes);
19434d95620bSPaul Winder atomic_add_64((uint64_t *)&bd->d_kiop->nwritten,
19444d95620bSPaul Winder bp->b_bcount - xi->i_resid);
19453f7d54a6SGarrett D'Amore }
19463f7d54a6SGarrett D'Amore }
19474d95620bSPaul Winder bd_sched(bd, bq);
19483f7d54a6SGarrett D'Amore }
19493f7d54a6SGarrett D'Amore
19506f0e4dc9SAndy Fiddaman static void
bd_dle_sysevent_task(void * arg)19516f0e4dc9SAndy Fiddaman bd_dle_sysevent_task(void *arg)
19526f0e4dc9SAndy Fiddaman {
19536f0e4dc9SAndy Fiddaman nvlist_t *attr = NULL;
19546f0e4dc9SAndy Fiddaman char *path = NULL;
19556f0e4dc9SAndy Fiddaman bd_t *bd = arg;
19566f0e4dc9SAndy Fiddaman dev_info_t *dip = bd->d_dip;
19576f0e4dc9SAndy Fiddaman size_t n;
19586f0e4dc9SAndy Fiddaman
19596f0e4dc9SAndy Fiddaman mutex_enter(&bd->d_dle_mutex);
19606f0e4dc9SAndy Fiddaman bd->d_dle_state &= ~BD_DLE_PENDING;
19616f0e4dc9SAndy Fiddaman bd->d_dle_state |= BD_DLE_RUNNING;
19626f0e4dc9SAndy Fiddaman mutex_exit(&bd->d_dle_mutex);
19636f0e4dc9SAndy Fiddaman
19646f0e4dc9SAndy Fiddaman dev_err(dip, CE_NOTE, "!dynamic LUN expansion");
19656f0e4dc9SAndy Fiddaman
19666f0e4dc9SAndy Fiddaman if (nvlist_alloc(&attr, NV_UNIQUE_NAME_TYPE, KM_SLEEP) != 0) {
19676f0e4dc9SAndy Fiddaman mutex_enter(&bd->d_dle_mutex);
19686f0e4dc9SAndy Fiddaman bd->d_dle_state &= ~(BD_DLE_RUNNING|BD_DLE_PENDING);
19696f0e4dc9SAndy Fiddaman mutex_exit(&bd->d_dle_mutex);
19706f0e4dc9SAndy Fiddaman return;
19716f0e4dc9SAndy Fiddaman }
19726f0e4dc9SAndy Fiddaman
19736f0e4dc9SAndy Fiddaman path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
19746f0e4dc9SAndy Fiddaman
19756f0e4dc9SAndy Fiddaman n = snprintf(path, MAXPATHLEN, "/devices");
19766f0e4dc9SAndy Fiddaman (void) ddi_pathname(dip, path + n);
19776f0e4dc9SAndy Fiddaman n = strlen(path);
19786f0e4dc9SAndy Fiddaman n += snprintf(path + n, MAXPATHLEN - n, ":x");
19796f0e4dc9SAndy Fiddaman
19806f0e4dc9SAndy Fiddaman for (;;) {
19816f0e4dc9SAndy Fiddaman /*
19826f0e4dc9SAndy Fiddaman * On receipt of this event, the ZFS sysevent module will scan
19836f0e4dc9SAndy Fiddaman * active zpools for child vdevs matching this physical path.
19846f0e4dc9SAndy Fiddaman * In order to catch both whole disk pools and those with an
19856f0e4dc9SAndy Fiddaman * EFI boot partition, generate separate sysevents for minor
1986f7457249SAndy Fiddaman * node 'a' and 'b'.
19876f0e4dc9SAndy Fiddaman */
19886f0e4dc9SAndy Fiddaman for (char c = 'a'; c < 'c'; c++) {
19896f0e4dc9SAndy Fiddaman path[n - 1] = c;
19906f0e4dc9SAndy Fiddaman
19916f0e4dc9SAndy Fiddaman if (nvlist_add_string(attr, DEV_PHYS_PATH, path) != 0)
19926f0e4dc9SAndy Fiddaman break;
19936f0e4dc9SAndy Fiddaman
19946f0e4dc9SAndy Fiddaman (void) ddi_log_sysevent(dip, DDI_VENDOR_SUNW,
19956f0e4dc9SAndy Fiddaman EC_DEV_STATUS, ESC_DEV_DLE, attr, NULL, DDI_SLEEP);
19966f0e4dc9SAndy Fiddaman }
19976f0e4dc9SAndy Fiddaman
19986f0e4dc9SAndy Fiddaman mutex_enter(&bd->d_dle_mutex);
19996f0e4dc9SAndy Fiddaman if ((bd->d_dle_state & BD_DLE_PENDING) == 0) {
20006f0e4dc9SAndy Fiddaman bd->d_dle_state &= ~BD_DLE_RUNNING;
20016f0e4dc9SAndy Fiddaman mutex_exit(&bd->d_dle_mutex);
20026f0e4dc9SAndy Fiddaman break;
20036f0e4dc9SAndy Fiddaman }
20046f0e4dc9SAndy Fiddaman bd->d_dle_state &= ~BD_DLE_PENDING;
20056f0e4dc9SAndy Fiddaman mutex_exit(&bd->d_dle_mutex);
20066f0e4dc9SAndy Fiddaman }
20076f0e4dc9SAndy Fiddaman
20086f0e4dc9SAndy Fiddaman nvlist_free(attr);
20096f0e4dc9SAndy Fiddaman kmem_free(path, MAXPATHLEN);
20106f0e4dc9SAndy Fiddaman }
20116f0e4dc9SAndy Fiddaman
20123f7d54a6SGarrett D'Amore static void
bd_update_state(bd_t * bd)20133f7d54a6SGarrett D'Amore bd_update_state(bd_t *bd)
20143f7d54a6SGarrett D'Amore {
201532ce6b81SHans Rosenfeld enum dkio_state state = DKIO_INSERTED;
20163f7d54a6SGarrett D'Amore boolean_t docmlb = B_FALSE;
201732ce6b81SHans Rosenfeld bd_media_t media;
20183f7d54a6SGarrett D'Amore
20193f7d54a6SGarrett D'Amore bzero(&media, sizeof (media));
20203f7d54a6SGarrett D'Amore
20213f7d54a6SGarrett D'Amore mutex_enter(&bd->d_statemutex);
202232ce6b81SHans Rosenfeld if (bd->d_ops.o_media_info(bd->d_private, &media) != 0) {
20233f7d54a6SGarrett D'Amore bd->d_numblks = 0;
20243f7d54a6SGarrett D'Amore state = DKIO_EJECTED;
202532ce6b81SHans Rosenfeld goto done;
202632ce6b81SHans Rosenfeld }
202732ce6b81SHans Rosenfeld
202832ce6b81SHans Rosenfeld if ((media.m_blksize < 512) ||
202932ce6b81SHans Rosenfeld (!ISP2(media.m_blksize)) ||
203032ce6b81SHans Rosenfeld (P2PHASE(bd->d_maxxfer, media.m_blksize))) {
20316f0e4dc9SAndy Fiddaman dev_err(bd->d_dip, CE_WARN, "Invalid media block size (%d)",
203232ce6b81SHans Rosenfeld media.m_blksize);
203332ce6b81SHans Rosenfeld /*
203432ce6b81SHans Rosenfeld * We can't use the media, treat it as not present.
203532ce6b81SHans Rosenfeld */
203632ce6b81SHans Rosenfeld state = DKIO_EJECTED;
203732ce6b81SHans Rosenfeld bd->d_numblks = 0;
203832ce6b81SHans Rosenfeld goto done;
203932ce6b81SHans Rosenfeld }
204032ce6b81SHans Rosenfeld
204132ce6b81SHans Rosenfeld if (((1U << bd->d_blkshift) != media.m_blksize) ||
204232ce6b81SHans Rosenfeld (bd->d_numblks != media.m_nblks)) {
204332ce6b81SHans Rosenfeld /* Device size changed */
204432ce6b81SHans Rosenfeld docmlb = B_TRUE;
20453f7d54a6SGarrett D'Amore }
204632ce6b81SHans Rosenfeld
204732ce6b81SHans Rosenfeld bd->d_blkshift = ddi_ffs(media.m_blksize) - 1;
204832ce6b81SHans Rosenfeld bd->d_pblkshift = bd->d_blkshift;
204932ce6b81SHans Rosenfeld bd->d_numblks = media.m_nblks;
205032ce6b81SHans Rosenfeld bd->d_rdonly = media.m_readonly;
205132ce6b81SHans Rosenfeld bd->d_ssd = media.m_solidstate;
205232ce6b81SHans Rosenfeld
205332ce6b81SHans Rosenfeld /*
205432ce6b81SHans Rosenfeld * Only use the supplied physical block size if it is non-zero,
205532ce6b81SHans Rosenfeld * greater or equal to the block size, and a power of 2. Ignore it
205632ce6b81SHans Rosenfeld * if not, it's just informational and we can still use the media.
205732ce6b81SHans Rosenfeld */
205832ce6b81SHans Rosenfeld if ((media.m_pblksize != 0) &&
205932ce6b81SHans Rosenfeld (media.m_pblksize >= media.m_blksize) &&
206032ce6b81SHans Rosenfeld (ISP2(media.m_pblksize)))
206132ce6b81SHans Rosenfeld bd->d_pblkshift = ddi_ffs(media.m_pblksize) - 1;
206232ce6b81SHans Rosenfeld
206332ce6b81SHans Rosenfeld done:
20643f7d54a6SGarrett D'Amore if (state != bd->d_state) {
20653f7d54a6SGarrett D'Amore bd->d_state = state;
20663f7d54a6SGarrett D'Amore cv_broadcast(&bd->d_statecv);
20673f7d54a6SGarrett D'Amore docmlb = B_TRUE;
20683f7d54a6SGarrett D'Amore }
20693f7d54a6SGarrett D'Amore mutex_exit(&bd->d_statemutex);
20703f7d54a6SGarrett D'Amore
2071bef9e21aSHans Rosenfeld bd->d_kerr->bd_capacity.value.ui64 = bd->d_numblks << bd->d_blkshift;
2072bef9e21aSHans Rosenfeld
20733f7d54a6SGarrett D'Amore if (docmlb) {
20743f7d54a6SGarrett D'Amore if (state == DKIO_INSERTED) {
207586e3bca6SGarrett D'Amore (void) cmlb_validate(bd->d_cmlbh, 0, 0);
20766f0e4dc9SAndy Fiddaman
20776f0e4dc9SAndy Fiddaman mutex_enter(&bd->d_dle_mutex);
20786f0e4dc9SAndy Fiddaman /*
20796f0e4dc9SAndy Fiddaman * If there is already an event pending, there's
20806f0e4dc9SAndy Fiddaman * nothing to do; we coalesce multiple events.
20816f0e4dc9SAndy Fiddaman */
20826f0e4dc9SAndy Fiddaman if ((bd->d_dle_state & BD_DLE_PENDING) == 0) {
20836f0e4dc9SAndy Fiddaman if ((bd->d_dle_state & BD_DLE_RUNNING) == 0) {
20846f0e4dc9SAndy Fiddaman taskq_dispatch_ent(bd_taskq,
20856f0e4dc9SAndy Fiddaman bd_dle_sysevent_task, bd, 0,
20866f0e4dc9SAndy Fiddaman &bd->d_dle_ent);
20876f0e4dc9SAndy Fiddaman }
20886f0e4dc9SAndy Fiddaman bd->d_dle_state |= BD_DLE_PENDING;
20896f0e4dc9SAndy Fiddaman }
20906f0e4dc9SAndy Fiddaman mutex_exit(&bd->d_dle_mutex);
20913f7d54a6SGarrett D'Amore } else {
209286e3bca6SGarrett D'Amore cmlb_invalidate(bd->d_cmlbh, 0);
20933f7d54a6SGarrett D'Amore }
20943f7d54a6SGarrett D'Amore }
20953f7d54a6SGarrett D'Amore }
20963f7d54a6SGarrett D'Amore
20973f7d54a6SGarrett D'Amore static int
bd_check_state(bd_t * bd,enum dkio_state * state)20983f7d54a6SGarrett D'Amore bd_check_state(bd_t *bd, enum dkio_state *state)
20993f7d54a6SGarrett D'Amore {
21003f7d54a6SGarrett D'Amore clock_t when;
21013f7d54a6SGarrett D'Amore
21023f7d54a6SGarrett D'Amore for (;;) {
21033f7d54a6SGarrett D'Amore
21043f7d54a6SGarrett D'Amore bd_update_state(bd);
21053f7d54a6SGarrett D'Amore
21063f7d54a6SGarrett D'Amore mutex_enter(&bd->d_statemutex);
21073f7d54a6SGarrett D'Amore
21083f7d54a6SGarrett D'Amore if (bd->d_state != *state) {
21093f7d54a6SGarrett D'Amore *state = bd->d_state;
21103f7d54a6SGarrett D'Amore mutex_exit(&bd->d_statemutex);
21113f7d54a6SGarrett D'Amore break;
21123f7d54a6SGarrett D'Amore }
21133f7d54a6SGarrett D'Amore
21143f7d54a6SGarrett D'Amore when = drv_usectohz(1000000);
21153f7d54a6SGarrett D'Amore if (cv_reltimedwait_sig(&bd->d_statecv, &bd->d_statemutex,
21163f7d54a6SGarrett D'Amore when, TR_CLOCK_TICK) == 0) {
21173f7d54a6SGarrett D'Amore mutex_exit(&bd->d_statemutex);
21183f7d54a6SGarrett D'Amore return (EINTR);
21193f7d54a6SGarrett D'Amore }
21203f7d54a6SGarrett D'Amore
21213f7d54a6SGarrett D'Amore mutex_exit(&bd->d_statemutex);
21223f7d54a6SGarrett D'Amore }
21233f7d54a6SGarrett D'Amore
21243f7d54a6SGarrett D'Amore return (0);
21253f7d54a6SGarrett D'Amore }
21263f7d54a6SGarrett D'Amore
21273f7d54a6SGarrett D'Amore static int
bd_flush_write_cache_done(struct buf * bp)21283f7d54a6SGarrett D'Amore bd_flush_write_cache_done(struct buf *bp)
21293f7d54a6SGarrett D'Amore {
21303f7d54a6SGarrett D'Amore struct dk_callback *dc = (void *)bp->b_private;
21313f7d54a6SGarrett D'Amore
21323f7d54a6SGarrett D'Amore (*dc->dkc_callback)(dc->dkc_cookie, geterror(bp));
21333f7d54a6SGarrett D'Amore kmem_free(dc, sizeof (*dc));
21343f7d54a6SGarrett D'Amore freerbuf(bp);
21353f7d54a6SGarrett D'Amore return (0);
21363f7d54a6SGarrett D'Amore }
21373f7d54a6SGarrett D'Amore
21383f7d54a6SGarrett D'Amore static int
bd_flush_write_cache(bd_t * bd,struct dk_callback * dkc)21393f7d54a6SGarrett D'Amore bd_flush_write_cache(bd_t *bd, struct dk_callback *dkc)
21403f7d54a6SGarrett D'Amore {
21413f7d54a6SGarrett D'Amore buf_t *bp;
21423f7d54a6SGarrett D'Amore struct dk_callback *dc;
21433f7d54a6SGarrett D'Amore bd_xfer_impl_t *xi;
21443f7d54a6SGarrett D'Amore int rv;
21453f7d54a6SGarrett D'Amore
21463f7d54a6SGarrett D'Amore if (bd->d_ops.o_sync_cache == NULL) {
21473f7d54a6SGarrett D'Amore return (ENOTSUP);
21483f7d54a6SGarrett D'Amore }
21493f7d54a6SGarrett D'Amore if ((bp = getrbuf(KM_SLEEP)) == NULL) {
21503f7d54a6SGarrett D'Amore return (ENOMEM);
21513f7d54a6SGarrett D'Amore }
21523f7d54a6SGarrett D'Amore bp->b_resid = 0;
21533f7d54a6SGarrett D'Amore bp->b_bcount = 0;
21543f7d54a6SGarrett D'Amore
21553f7d54a6SGarrett D'Amore xi = bd_xfer_alloc(bd, bp, bd->d_ops.o_sync_cache, KM_SLEEP);
21563f7d54a6SGarrett D'Amore if (xi == NULL) {
21573f7d54a6SGarrett D'Amore rv = geterror(bp);
21583f7d54a6SGarrett D'Amore freerbuf(bp);
21593f7d54a6SGarrett D'Amore return (rv);
21603f7d54a6SGarrett D'Amore }
21613f7d54a6SGarrett D'Amore
2162f097ef9cSAlexey Zaytsev /* Make an asynchronous flush, but only if there is a callback */
2163f097ef9cSAlexey Zaytsev if (dkc != NULL && dkc->dkc_callback != NULL) {
21643f7d54a6SGarrett D'Amore /* Make a private copy of the callback structure */
21653f7d54a6SGarrett D'Amore dc = kmem_alloc(sizeof (*dc), KM_SLEEP);
21663f7d54a6SGarrett D'Amore *dc = *dkc;
21673f7d54a6SGarrett D'Amore bp->b_private = dc;
21683f7d54a6SGarrett D'Amore bp->b_iodone = bd_flush_write_cache_done;
2169f097ef9cSAlexey Zaytsev
2170f097ef9cSAlexey Zaytsev bd_submit(bd, xi);
2171f097ef9cSAlexey Zaytsev return (0);
21723f7d54a6SGarrett D'Amore }
21733f7d54a6SGarrett D'Amore
2174f097ef9cSAlexey Zaytsev /* In case there is no callback, perform a synchronous flush */
21753f7d54a6SGarrett D'Amore bd_submit(bd, xi);
2176f097ef9cSAlexey Zaytsev (void) biowait(bp);
2177f097ef9cSAlexey Zaytsev rv = geterror(bp);
2178f097ef9cSAlexey Zaytsev freerbuf(bp);
2179f097ef9cSAlexey Zaytsev
21803f7d54a6SGarrett D'Amore return (rv);
21813f7d54a6SGarrett D'Amore }
21823f7d54a6SGarrett D'Amore
21831a5ae140SJason King static int
bd_free_space_done(struct buf * bp)21841a5ae140SJason King bd_free_space_done(struct buf *bp)
21851a5ae140SJason King {
21861a5ae140SJason King freerbuf(bp);
21871a5ae140SJason King return (0);
21881a5ae140SJason King }
21891a5ae140SJason King
21901a5ae140SJason King static int
bd_free_space_cb(dkioc_free_list_t * dfl,void * arg,int kmflag)21911a5ae140SJason King bd_free_space_cb(dkioc_free_list_t *dfl, void *arg, int kmflag)
21921a5ae140SJason King {
21931a5ae140SJason King bd_t *bd = arg;
21941a5ae140SJason King buf_t *bp = NULL;
21951a5ae140SJason King bd_xfer_impl_t *xi = NULL;
21961a5ae140SJason King boolean_t sync = DFL_ISSYNC(dfl) ? B_TRUE : B_FALSE;
21971a5ae140SJason King int rv = 0;
21981a5ae140SJason King
21991a5ae140SJason King bp = getrbuf(KM_SLEEP);
22001a5ae140SJason King bp->b_resid = 0;
22011a5ae140SJason King bp->b_bcount = 0;
22021a5ae140SJason King bp->b_lblkno = 0;
22031a5ae140SJason King
22041a5ae140SJason King xi = bd_xfer_alloc(bd, bp, bd->d_ops.o_free_space, kmflag);
22051a5ae140SJason King xi->i_dfl = dfl;
22061a5ae140SJason King
22071a5ae140SJason King if (!sync) {
22081a5ae140SJason King bp->b_iodone = bd_free_space_done;
22091a5ae140SJason King bd_submit(bd, xi);
22101a5ae140SJason King return (0);
22111a5ae140SJason King }
22121a5ae140SJason King
22131a5ae140SJason King xi->i_flags |= BD_XFER_POLL;
22141a5ae140SJason King bd_submit(bd, xi);
22151a5ae140SJason King
22161a5ae140SJason King (void) biowait(bp);
22171a5ae140SJason King rv = geterror(bp);
22181a5ae140SJason King freerbuf(bp);
22191a5ae140SJason King
22201a5ae140SJason King return (rv);
22211a5ae140SJason King }
22221a5ae140SJason King
22231a5ae140SJason King static int
bd_free_space(dev_t dev,bd_t * bd,dkioc_free_list_t * dfl)22241a5ae140SJason King bd_free_space(dev_t dev, bd_t *bd, dkioc_free_list_t *dfl)
22251a5ae140SJason King {
22261a5ae140SJason King diskaddr_t p_len, p_offset;
22271a5ae140SJason King uint64_t offset_bytes, len_bytes;
22281a5ae140SJason King minor_t part = BDPART(dev);
22291a5ae140SJason King const uint_t bshift = bd->d_blkshift;
22301a5ae140SJason King dkioc_free_info_t dfi = {
22311a5ae140SJason King .dfi_bshift = bshift,
22321a5ae140SJason King .dfi_align = bd->d_free_align << bshift,
22331a5ae140SJason King .dfi_max_bytes = bd->d_max_free_blks << bshift,
22341a5ae140SJason King .dfi_max_ext = bd->d_max_free_seg,
22351a5ae140SJason King .dfi_max_ext_bytes = bd->d_max_free_seg_blks << bshift,
22361a5ae140SJason King };
22371a5ae140SJason King
22381a5ae140SJason King if (cmlb_partinfo(bd->d_cmlbh, part, &p_len, &p_offset, NULL,
22391a5ae140SJason King NULL, 0) != 0) {
22401a5ae140SJason King dfl_free(dfl);
22411a5ae140SJason King return (ENXIO);
22421a5ae140SJason King }
22431a5ae140SJason King
22441a5ae140SJason King /*
22451a5ae140SJason King * bd_ioctl created our own copy of dfl, so we can modify as
22461a5ae140SJason King * necessary
22471a5ae140SJason King */
22481a5ae140SJason King offset_bytes = (uint64_t)p_offset << bshift;
22491a5ae140SJason King len_bytes = (uint64_t)p_len << bshift;
22501a5ae140SJason King
22511a5ae140SJason King dfl->dfl_offset += offset_bytes;
22521a5ae140SJason King if (dfl->dfl_offset < offset_bytes) {
22531a5ae140SJason King dfl_free(dfl);
22541a5ae140SJason King return (EOVERFLOW);
22551a5ae140SJason King }
22561a5ae140SJason King
22571a5ae140SJason King return (dfl_iter(dfl, &dfi, offset_bytes + len_bytes, bd_free_space_cb,
22581a5ae140SJason King bd, KM_SLEEP));
22591a5ae140SJason King }
22601a5ae140SJason King
22613f7d54a6SGarrett D'Amore /*
22623f7d54a6SGarrett D'Amore * Nexus support.
22633f7d54a6SGarrett D'Amore */
22643f7d54a6SGarrett D'Amore int
bd_bus_ctl(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)22653f7d54a6SGarrett D'Amore bd_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
22663f7d54a6SGarrett D'Amore void *arg, void *result)
22673f7d54a6SGarrett D'Amore {
22683f7d54a6SGarrett D'Amore bd_handle_t hdl;
22693f7d54a6SGarrett D'Amore
22703f7d54a6SGarrett D'Amore switch (ctlop) {
22713f7d54a6SGarrett D'Amore case DDI_CTLOPS_REPORTDEV:
22723f7d54a6SGarrett D'Amore cmn_err(CE_CONT, "?Block device: %s@%s, %s%d\n",
22733f7d54a6SGarrett D'Amore ddi_node_name(rdip), ddi_get_name_addr(rdip),
22743f7d54a6SGarrett D'Amore ddi_driver_name(rdip), ddi_get_instance(rdip));
22753f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
22763f7d54a6SGarrett D'Amore
22773f7d54a6SGarrett D'Amore case DDI_CTLOPS_INITCHILD:
22783f7d54a6SGarrett D'Amore hdl = ddi_get_parent_data((dev_info_t *)arg);
22793f7d54a6SGarrett D'Amore if (hdl == NULL) {
22803f7d54a6SGarrett D'Amore return (DDI_NOT_WELL_FORMED);
22813f7d54a6SGarrett D'Amore }
22823f7d54a6SGarrett D'Amore ddi_set_name_addr((dev_info_t *)arg, hdl->h_addr);
22833f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
22843f7d54a6SGarrett D'Amore
22853f7d54a6SGarrett D'Amore case DDI_CTLOPS_UNINITCHILD:
22863f7d54a6SGarrett D'Amore ddi_set_name_addr((dev_info_t *)arg, NULL);
22873f7d54a6SGarrett D'Amore ndi_prop_remove_all((dev_info_t *)arg);
22883f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
22893f7d54a6SGarrett D'Amore
22903f7d54a6SGarrett D'Amore default:
22913f7d54a6SGarrett D'Amore return (ddi_ctlops(dip, rdip, ctlop, arg, result));
22923f7d54a6SGarrett D'Amore }
22933f7d54a6SGarrett D'Amore }
22943f7d54a6SGarrett D'Amore
22953f7d54a6SGarrett D'Amore /*
22963f7d54a6SGarrett D'Amore * Functions for device drivers.
22973f7d54a6SGarrett D'Amore */
22983f7d54a6SGarrett D'Amore bd_handle_t
bd_alloc_handle(void * private,bd_ops_t * ops,ddi_dma_attr_t * dma,int kmflag)22993f7d54a6SGarrett D'Amore bd_alloc_handle(void *private, bd_ops_t *ops, ddi_dma_attr_t *dma, int kmflag)
23003f7d54a6SGarrett D'Amore {
23013f7d54a6SGarrett D'Amore bd_handle_t hdl;
23023f7d54a6SGarrett D'Amore
23034d95620bSPaul Winder switch (ops->o_version) {
23044d95620bSPaul Winder case BD_OPS_VERSION_0:
23051a5ae140SJason King case BD_OPS_VERSION_1:
23061a5ae140SJason King case BD_OPS_VERSION_2:
23074d95620bSPaul Winder break;
23084d95620bSPaul Winder
23094d95620bSPaul Winder default:
23101a5ae140SJason King /* Unsupported version */
23114d95620bSPaul Winder return (NULL);
23124d95620bSPaul Winder }
23134d95620bSPaul Winder
23143f7d54a6SGarrett D'Amore hdl = kmem_zalloc(sizeof (*hdl), kmflag);
23151a5ae140SJason King if (hdl == NULL) {
23161a5ae140SJason King return (NULL);
23173f7d54a6SGarrett D'Amore }
23183f7d54a6SGarrett D'Amore
23191a5ae140SJason King switch (ops->o_version) {
23201a5ae140SJason King case BD_OPS_VERSION_2:
23211a5ae140SJason King hdl->h_ops.o_free_space = ops->o_free_space;
23221a5ae140SJason King /*FALLTHRU*/
23231a5ae140SJason King case BD_OPS_VERSION_1:
23241a5ae140SJason King case BD_OPS_VERSION_0:
23251a5ae140SJason King hdl->h_ops.o_drive_info = ops->o_drive_info;
23261a5ae140SJason King hdl->h_ops.o_media_info = ops->o_media_info;
23271a5ae140SJason King hdl->h_ops.o_devid_init = ops->o_devid_init;
23281a5ae140SJason King hdl->h_ops.o_sync_cache = ops->o_sync_cache;
23291a5ae140SJason King hdl->h_ops.o_read = ops->o_read;
23301a5ae140SJason King hdl->h_ops.o_write = ops->o_write;
23311a5ae140SJason King break;
23321a5ae140SJason King }
23331a5ae140SJason King
23341a5ae140SJason King hdl->h_dma = dma;
23351a5ae140SJason King hdl->h_private = private;
23361a5ae140SJason King
23373f7d54a6SGarrett D'Amore return (hdl);
23383f7d54a6SGarrett D'Amore }
23393f7d54a6SGarrett D'Amore
23403f7d54a6SGarrett D'Amore void
bd_free_handle(bd_handle_t hdl)23413f7d54a6SGarrett D'Amore bd_free_handle(bd_handle_t hdl)
23423f7d54a6SGarrett D'Amore {
23433f7d54a6SGarrett D'Amore kmem_free(hdl, sizeof (*hdl));
23443f7d54a6SGarrett D'Amore }
23453f7d54a6SGarrett D'Amore
23463f7d54a6SGarrett D'Amore int
bd_attach_handle(dev_info_t * dip,bd_handle_t hdl)23473f7d54a6SGarrett D'Amore bd_attach_handle(dev_info_t *dip, bd_handle_t hdl)
23483f7d54a6SGarrett D'Amore {
2349510a6847SHans Rosenfeld bd_drive_t drive = { 0 };
2350ca0df52aSHans Rosenfeld dev_info_t *child;
2351ca0df52aSHans Rosenfeld size_t len;
23523f7d54a6SGarrett D'Amore
23533d9b1a2aSHans Rosenfeld /*
23543d9b1a2aSHans Rosenfeld * It's not an error if bd_attach_handle() is called on a handle that
23553d9b1a2aSHans Rosenfeld * already is attached. We just ignore the request to attach and return.
23563d9b1a2aSHans Rosenfeld * This way drivers using blkdev don't have to keep track about blkdev
23573d9b1a2aSHans Rosenfeld * state, they can just call this function to make sure it attached.
23583d9b1a2aSHans Rosenfeld */
23593d9b1a2aSHans Rosenfeld if (hdl->h_child != NULL) {
23603d9b1a2aSHans Rosenfeld return (DDI_SUCCESS);
23613d9b1a2aSHans Rosenfeld }
23623d9b1a2aSHans Rosenfeld
23633f7d54a6SGarrett D'Amore /* if drivers don't override this, make it assume none */
23643f7d54a6SGarrett D'Amore drive.d_lun = -1;
23653f7d54a6SGarrett D'Amore hdl->h_ops.o_drive_info(hdl->h_private, &drive);
23663f7d54a6SGarrett D'Amore
23673f7d54a6SGarrett D'Amore hdl->h_parent = dip;
23683f7d54a6SGarrett D'Amore hdl->h_name = "blkdev";
23693f7d54a6SGarrett D'Amore
2370ca0df52aSHans Rosenfeld /*
2371ca0df52aSHans Rosenfeld * Prefer the GUID over the EUI64.
2372ca0df52aSHans Rosenfeld */
2373ca0df52aSHans Rosenfeld if (*(uint64_t *)drive.d_guid != 0 ||
2374ca0df52aSHans Rosenfeld *((uint64_t *)drive.d_guid + 1) != 0) {
2375ca0df52aSHans Rosenfeld len = snprintf(hdl->h_addr, sizeof (hdl->h_addr),
2376ca0df52aSHans Rosenfeld "w%02X%02X%02X%02X%02X%02X%02X%02X"
2377ca0df52aSHans Rosenfeld "%02X%02X%02X%02X%02X%02X%02X%02X",
2378ca0df52aSHans Rosenfeld drive.d_guid[0], drive.d_guid[1], drive.d_guid[2],
2379ca0df52aSHans Rosenfeld drive.d_guid[3], drive.d_guid[4], drive.d_guid[5],
2380ca0df52aSHans Rosenfeld drive.d_guid[6], drive.d_guid[7], drive.d_guid[8],
2381ca0df52aSHans Rosenfeld drive.d_guid[9], drive.d_guid[10], drive.d_guid[11],
2382ca0df52aSHans Rosenfeld drive.d_guid[12], drive.d_guid[13], drive.d_guid[14],
2383ca0df52aSHans Rosenfeld drive.d_guid[15]);
2384ca0df52aSHans Rosenfeld } else if (*(uint64_t *)drive.d_eui64 != 0) {
2385ca0df52aSHans Rosenfeld len = snprintf(hdl->h_addr, sizeof (hdl->h_addr),
2386ca0df52aSHans Rosenfeld "w%02X%02X%02X%02X%02X%02X%02X%02X",
2387ca0df52aSHans Rosenfeld drive.d_eui64[0], drive.d_eui64[1],
2388ca0df52aSHans Rosenfeld drive.d_eui64[2], drive.d_eui64[3],
2389ca0df52aSHans Rosenfeld drive.d_eui64[4], drive.d_eui64[5],
2390ca0df52aSHans Rosenfeld drive.d_eui64[6], drive.d_eui64[7]);
23913f7d54a6SGarrett D'Amore } else {
2392ca0df52aSHans Rosenfeld len = snprintf(hdl->h_addr, sizeof (hdl->h_addr),
2393ca0df52aSHans Rosenfeld "%X", drive.d_target);
2394ca0df52aSHans Rosenfeld }
2395ca0df52aSHans Rosenfeld
2396ca0df52aSHans Rosenfeld VERIFY(len <= sizeof (hdl->h_addr));
2397ca0df52aSHans Rosenfeld
2398ca0df52aSHans Rosenfeld if (drive.d_lun >= 0) {
2399ca0df52aSHans Rosenfeld (void) snprintf(hdl->h_addr + len, sizeof (hdl->h_addr) - len,
2400ca0df52aSHans Rosenfeld ",%X", drive.d_lun);
24013f7d54a6SGarrett D'Amore }
2402df8c2287SHans Rosenfeld
24033f7d54a6SGarrett D'Amore if (ndi_devi_alloc(dip, hdl->h_name, (pnode_t)DEVI_SID_NODEID,
24043f7d54a6SGarrett D'Amore &child) != NDI_SUCCESS) {
24053f7d54a6SGarrett D'Amore cmn_err(CE_WARN, "%s%d: unable to allocate node %s@%s",
24063f7d54a6SGarrett D'Amore ddi_driver_name(dip), ddi_get_instance(dip),
24073f7d54a6SGarrett D'Amore "blkdev", hdl->h_addr);
24083f7d54a6SGarrett D'Amore return (DDI_FAILURE);
24093f7d54a6SGarrett D'Amore }
24103f7d54a6SGarrett D'Amore
24113f7d54a6SGarrett D'Amore ddi_set_parent_data(child, hdl);
24123f7d54a6SGarrett D'Amore hdl->h_child = child;
24133f7d54a6SGarrett D'Amore
241433f84ecfSJason King if (ndi_devi_online(child, 0) != NDI_SUCCESS) {
24153f7d54a6SGarrett D'Amore cmn_err(CE_WARN, "%s%d: failed bringing node %s@%s online",
24163f7d54a6SGarrett D'Amore ddi_driver_name(dip), ddi_get_instance(dip),
24173f7d54a6SGarrett D'Amore hdl->h_name, hdl->h_addr);
24183f7d54a6SGarrett D'Amore (void) ndi_devi_free(child);
241933f84ecfSJason King hdl->h_child = NULL;
24203f7d54a6SGarrett D'Amore return (DDI_FAILURE);
24213f7d54a6SGarrett D'Amore }
24223f7d54a6SGarrett D'Amore
24233f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
24243f7d54a6SGarrett D'Amore }
24253f7d54a6SGarrett D'Amore
24263f7d54a6SGarrett D'Amore int
bd_detach_handle(bd_handle_t hdl)24273f7d54a6SGarrett D'Amore bd_detach_handle(bd_handle_t hdl)
24283f7d54a6SGarrett D'Amore {
24293f7d54a6SGarrett D'Amore int rv;
24303f7d54a6SGarrett D'Amore char *devnm;
24313f7d54a6SGarrett D'Amore
24323d9b1a2aSHans Rosenfeld /*
24333d9b1a2aSHans Rosenfeld * It's not an error if bd_detach_handle() is called on a handle that
24343d9b1a2aSHans Rosenfeld * already is detached. We just ignore the request to detach and return.
24353d9b1a2aSHans Rosenfeld * This way drivers using blkdev don't have to keep track about blkdev
24363d9b1a2aSHans Rosenfeld * state, they can just call this function to make sure it detached.
24373d9b1a2aSHans Rosenfeld */
24383f7d54a6SGarrett D'Amore if (hdl->h_child == NULL) {
24393f7d54a6SGarrett D'Amore return (DDI_SUCCESS);
24403f7d54a6SGarrett D'Amore }
24413fe80ca4SDan Cross ndi_devi_enter(hdl->h_parent);
24423f7d54a6SGarrett D'Amore if (i_ddi_node_state(hdl->h_child) < DS_INITIALIZED) {
24433f7d54a6SGarrett D'Amore rv = ddi_remove_child(hdl->h_child, 0);
24443f7d54a6SGarrett D'Amore } else {
24453f7d54a6SGarrett D'Amore devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
24463f7d54a6SGarrett D'Amore (void) ddi_deviname(hdl->h_child, devnm);
24473f7d54a6SGarrett D'Amore (void) devfs_clean(hdl->h_parent, devnm + 1, DV_CLEAN_FORCE);
24483f7d54a6SGarrett D'Amore rv = ndi_devi_unconfig_one(hdl->h_parent, devnm + 1, NULL,
24493f7d54a6SGarrett D'Amore NDI_DEVI_REMOVE | NDI_UNCONFIG);
24503f7d54a6SGarrett D'Amore kmem_free(devnm, MAXNAMELEN + 1);
24513f7d54a6SGarrett D'Amore }
24523f7d54a6SGarrett D'Amore if (rv == 0) {
24533f7d54a6SGarrett D'Amore hdl->h_child = NULL;
24543f7d54a6SGarrett D'Amore }
24553f7d54a6SGarrett D'Amore
24563fe80ca4SDan Cross ndi_devi_exit(hdl->h_parent);
2457aad3a447SHans Rosenfeld return (rv == NDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
24583f7d54a6SGarrett D'Amore }
24593f7d54a6SGarrett D'Amore
24603f7d54a6SGarrett D'Amore void
bd_xfer_done(bd_xfer_t * xfer,int err)24613f7d54a6SGarrett D'Amore bd_xfer_done(bd_xfer_t *xfer, int err)
24623f7d54a6SGarrett D'Amore {
24633f7d54a6SGarrett D'Amore bd_xfer_impl_t *xi = (void *)xfer;
24643f7d54a6SGarrett D'Amore buf_t *bp = xi->i_bp;
246506711e0cSDmitry Yusupov int rv = DDI_SUCCESS;
24663f7d54a6SGarrett D'Amore bd_t *bd = xi->i_bd;
24673f7d54a6SGarrett D'Amore size_t len;
24683f7d54a6SGarrett D'Amore
24693f7d54a6SGarrett D'Amore if (err != 0) {
24703f7d54a6SGarrett D'Amore bd_runq_exit(xi, err);
2471bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_harderrs.value.ui32);
24723f7d54a6SGarrett D'Amore
24733f7d54a6SGarrett D'Amore bp->b_resid += xi->i_resid;
24743f7d54a6SGarrett D'Amore bd_xfer_free(xi);
24753f7d54a6SGarrett D'Amore bioerror(bp, err);
24763f7d54a6SGarrett D'Amore biodone(bp);
24773f7d54a6SGarrett D'Amore return;
24783f7d54a6SGarrett D'Amore }
24793f7d54a6SGarrett D'Amore
24803f7d54a6SGarrett D'Amore xi->i_cur_win++;
24813f7d54a6SGarrett D'Amore xi->i_resid -= xi->i_len;
24823f7d54a6SGarrett D'Amore
24833f7d54a6SGarrett D'Amore if (xi->i_resid == 0) {
24843f7d54a6SGarrett D'Amore /* Job completed succcessfully! */
24853f7d54a6SGarrett D'Amore bd_runq_exit(xi, 0);
24863f7d54a6SGarrett D'Amore
24873f7d54a6SGarrett D'Amore bd_xfer_free(xi);
24883f7d54a6SGarrett D'Amore biodone(bp);
24893f7d54a6SGarrett D'Amore return;
24903f7d54a6SGarrett D'Amore }
24913f7d54a6SGarrett D'Amore
24923f7d54a6SGarrett D'Amore xi->i_blkno += xi->i_nblks;
24933f7d54a6SGarrett D'Amore
24943f7d54a6SGarrett D'Amore if (bd->d_use_dma) {
24953f7d54a6SGarrett D'Amore /* More transfer still pending... advance to next DMA window. */
24963f7d54a6SGarrett D'Amore rv = ddi_dma_getwin(xi->i_dmah, xi->i_cur_win,
24973f7d54a6SGarrett D'Amore &xi->i_offset, &len, &xi->i_dmac, &xi->i_ndmac);
24983f7d54a6SGarrett D'Amore } else {
24993f7d54a6SGarrett D'Amore /* Advance memory window. */
25003f7d54a6SGarrett D'Amore xi->i_kaddr += xi->i_len;
25013f7d54a6SGarrett D'Amore xi->i_offset += xi->i_len;
25023f7d54a6SGarrett D'Amore len = min(bp->b_bcount - xi->i_offset, bd->d_maxxfer);
25033f7d54a6SGarrett D'Amore }
25043f7d54a6SGarrett D'Amore
25053f7d54a6SGarrett D'Amore
25063f7d54a6SGarrett D'Amore if ((rv != DDI_SUCCESS) ||
2507c0591a0cSYouzhong Yang (P2PHASE(len, (1U << xi->i_blkshift)) != 0)) {
25083f7d54a6SGarrett D'Amore bd_runq_exit(xi, EFAULT);
25093f7d54a6SGarrett D'Amore
25103f7d54a6SGarrett D'Amore bp->b_resid += xi->i_resid;
25113f7d54a6SGarrett D'Amore bd_xfer_free(xi);
25123f7d54a6SGarrett D'Amore bioerror(bp, EFAULT);
25133f7d54a6SGarrett D'Amore biodone(bp);
25143f7d54a6SGarrett D'Amore return;
25153f7d54a6SGarrett D'Amore }
25163f7d54a6SGarrett D'Amore xi->i_len = len;
251704904ca2SDan McDonald xi->i_nblks = len >> xi->i_blkshift;
25183f7d54a6SGarrett D'Amore
25193f7d54a6SGarrett D'Amore /* Submit next window to hardware. */
25203f7d54a6SGarrett D'Amore rv = xi->i_func(bd->d_private, &xi->i_public);
25213f7d54a6SGarrett D'Amore if (rv != 0) {
25223f7d54a6SGarrett D'Amore bd_runq_exit(xi, rv);
25233f7d54a6SGarrett D'Amore
2524bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_transerrs.value.ui32);
2525bef9e21aSHans Rosenfeld
25263f7d54a6SGarrett D'Amore bp->b_resid += xi->i_resid;
25273f7d54a6SGarrett D'Amore bd_xfer_free(xi);
25283f7d54a6SGarrett D'Amore bioerror(bp, rv);
25293f7d54a6SGarrett D'Amore biodone(bp);
25303f7d54a6SGarrett D'Amore }
25313f7d54a6SGarrett D'Amore }
25323f7d54a6SGarrett D'Amore
2533bef9e21aSHans Rosenfeld void
bd_error(bd_xfer_t * xfer,int error)2534bef9e21aSHans Rosenfeld bd_error(bd_xfer_t *xfer, int error)
2535bef9e21aSHans Rosenfeld {
2536bef9e21aSHans Rosenfeld bd_xfer_impl_t *xi = (void *)xfer;
2537bef9e21aSHans Rosenfeld bd_t *bd = xi->i_bd;
2538bef9e21aSHans Rosenfeld
2539bef9e21aSHans Rosenfeld switch (error) {
2540bef9e21aSHans Rosenfeld case BD_ERR_MEDIA:
2541bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_rq_media_err.value.ui32);
2542bef9e21aSHans Rosenfeld break;
2543bef9e21aSHans Rosenfeld case BD_ERR_NTRDY:
2544bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_rq_ntrdy_err.value.ui32);
2545bef9e21aSHans Rosenfeld break;
2546bef9e21aSHans Rosenfeld case BD_ERR_NODEV:
2547bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_rq_nodev_err.value.ui32);
2548bef9e21aSHans Rosenfeld break;
2549bef9e21aSHans Rosenfeld case BD_ERR_RECOV:
2550bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_rq_recov_err.value.ui32);
2551bef9e21aSHans Rosenfeld break;
2552bef9e21aSHans Rosenfeld case BD_ERR_ILLRQ:
2553bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_rq_illrq_err.value.ui32);
2554bef9e21aSHans Rosenfeld break;
2555bef9e21aSHans Rosenfeld case BD_ERR_PFA:
2556bef9e21aSHans Rosenfeld atomic_inc_32(&bd->d_kerr->bd_rq_pfa_err.value.ui32);
2557bef9e21aSHans Rosenfeld break;
2558bef9e21aSHans Rosenfeld default:
2559bef9e21aSHans Rosenfeld cmn_err(CE_PANIC, "bd_error: unknown error type %d", error);
2560bef9e21aSHans Rosenfeld break;
2561bef9e21aSHans Rosenfeld }
2562bef9e21aSHans Rosenfeld }
2563bef9e21aSHans Rosenfeld
25643f7d54a6SGarrett D'Amore void
bd_state_change(bd_handle_t hdl)25653f7d54a6SGarrett D'Amore bd_state_change(bd_handle_t hdl)
25663f7d54a6SGarrett D'Amore {
25673f7d54a6SGarrett D'Amore bd_t *bd;
25683f7d54a6SGarrett D'Amore
25693f7d54a6SGarrett D'Amore if ((bd = hdl->h_bd) != NULL) {
25703f7d54a6SGarrett D'Amore bd_update_state(bd);
25713f7d54a6SGarrett D'Amore }
25723f7d54a6SGarrett D'Amore }
25733f7d54a6SGarrett D'Amore
2574*744642a2SRobert Mustacchi const char *
bd_address(bd_handle_t hdl)2575*744642a2SRobert Mustacchi bd_address(bd_handle_t hdl)
2576*744642a2SRobert Mustacchi {
2577*744642a2SRobert Mustacchi return (hdl->h_addr);
2578*744642a2SRobert Mustacchi }
2579*744642a2SRobert Mustacchi
25803f7d54a6SGarrett D'Amore void
bd_mod_init(struct dev_ops * devops)25813f7d54a6SGarrett D'Amore bd_mod_init(struct dev_ops *devops)
25823f7d54a6SGarrett D'Amore {
25833f7d54a6SGarrett D'Amore static struct bus_ops bd_bus_ops = {
25843f7d54a6SGarrett D'Amore BUSO_REV, /* busops_rev */
25853f7d54a6SGarrett D'Amore nullbusmap, /* bus_map */
25863f7d54a6SGarrett D'Amore NULL, /* bus_get_intrspec (OBSOLETE) */
25873f7d54a6SGarrett D'Amore NULL, /* bus_add_intrspec (OBSOLETE) */
25883f7d54a6SGarrett D'Amore NULL, /* bus_remove_intrspec (OBSOLETE) */
25893f7d54a6SGarrett D'Amore i_ddi_map_fault, /* bus_map_fault */
2590cd21e7c5SGarrett D'Amore NULL, /* bus_dma_map (OBSOLETE) */
25913f7d54a6SGarrett D'Amore ddi_dma_allochdl, /* bus_dma_allochdl */
25923f7d54a6SGarrett D'Amore ddi_dma_freehdl, /* bus_dma_freehdl */
25933f7d54a6SGarrett D'Amore ddi_dma_bindhdl, /* bus_dma_bindhdl */
25943f7d54a6SGarrett D'Amore ddi_dma_unbindhdl, /* bus_dma_unbindhdl */
25953f7d54a6SGarrett D'Amore ddi_dma_flush, /* bus_dma_flush */
25963f7d54a6SGarrett D'Amore ddi_dma_win, /* bus_dma_win */
25973f7d54a6SGarrett D'Amore ddi_dma_mctl, /* bus_dma_ctl */
25983f7d54a6SGarrett D'Amore bd_bus_ctl, /* bus_ctl */
25993f7d54a6SGarrett D'Amore ddi_bus_prop_op, /* bus_prop_op */
26003f7d54a6SGarrett D'Amore NULL, /* bus_get_eventcookie */
26013f7d54a6SGarrett D'Amore NULL, /* bus_add_eventcall */
26023f7d54a6SGarrett D'Amore NULL, /* bus_remove_eventcall */
26033f7d54a6SGarrett D'Amore NULL, /* bus_post_event */
26043f7d54a6SGarrett D'Amore NULL, /* bus_intr_ctl (OBSOLETE) */
26053f7d54a6SGarrett D'Amore NULL, /* bus_config */
26063f7d54a6SGarrett D'Amore NULL, /* bus_unconfig */
26073f7d54a6SGarrett D'Amore NULL, /* bus_fm_init */
26083f7d54a6SGarrett D'Amore NULL, /* bus_fm_fini */
26093f7d54a6SGarrett D'Amore NULL, /* bus_fm_access_enter */
26103f7d54a6SGarrett D'Amore NULL, /* bus_fm_access_exit */
26113f7d54a6SGarrett D'Amore NULL, /* bus_power */
26123f7d54a6SGarrett D'Amore NULL, /* bus_intr_op */
26133f7d54a6SGarrett D'Amore };
26143f7d54a6SGarrett D'Amore
26153f7d54a6SGarrett D'Amore devops->devo_bus_ops = &bd_bus_ops;
26163f7d54a6SGarrett D'Amore
26173f7d54a6SGarrett D'Amore /*
26183f7d54a6SGarrett D'Amore * NB: The device driver is free to supply its own
26193f7d54a6SGarrett D'Amore * character entry device support.
26203f7d54a6SGarrett D'Amore */
26213f7d54a6SGarrett D'Amore }
26223f7d54a6SGarrett D'Amore
26233f7d54a6SGarrett D'Amore void
bd_mod_fini(struct dev_ops * devops)26243f7d54a6SGarrett D'Amore bd_mod_fini(struct dev_ops *devops)
26253f7d54a6SGarrett D'Amore {
26263f7d54a6SGarrett D'Amore devops->devo_bus_ops = NULL;
26273f7d54a6SGarrett D'Amore }
2628