xref: /titanic_41/usr/src/uts/common/xen/io/xdb.h (revision 0a05e7057ae5537db2da83492d375e6524599463)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #ifndef _SYS_XDB_H
29 #define	_SYS_XDB_H
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include <sys/types.h>
38 #include <sys/conf.h>
39 #include <sys/ddi.h>
40 #include <sys/dditypes.h>
41 #include <sys/sunddi.h>
42 #include <sys/sunldi.h>
43 #include <sys/modctl.h>
44 #include <vm/seg_kmem.h>
45 #include <sys/gnttab.h>
46 #include <xen/sys/xenbus_impl.h>
47 #include <xen/sys/xendev.h>
48 #include "xdf.h"
49 
50 #define	XDB_DBG_ALL	0xf
51 #define	XDB_DBG_IO	0x1
52 #define	XDB_DBG_INFO	0x2
53 #define	XDB_DBPRINT(lvl, fmt) { if (xdb_debug & lvl) cmn_err fmt; }
54 
55 /*
56  * Info of the exported blk device
57  */
58 #define	XDB_DEV_RO	(1)	/* read-only or writable */
59 #define	XDB_IS_RO(vdp)	((vdp)->xs_type & XDB_DEV_RO)
60 #define	XDB_DEV_LOFI	(1 << 1) /* lofi device or physical device */
61 #define	XDB_IS_LOFI(vdp)	((vdp)->xs_type & XDB_DEV_LOFI)
62 #define	XDB_DEV_CD	(1 << 2) /* cdrom disc */
63 #define	XDB_IS_CD(vdp)	((vdp)->xs_type & XDB_DEV_CD)
64 #define	XDB_DEV_RMB	(1 << 3) /* removable device */
65 #define	XDB_IS_RMB(vdp)	((vdp)->xs_type & XDB_DEV_RMB)
66 
67 /*
68  * Xdb interface status
69  */
70 enum xdb_state {
71 	/*
72 	 * initial state
73 	 */
74 	XDB_UNKNOWN,
75 	/*
76 	 * frontend xenbus state changed to XenbusStateConnected,
77 	 * we finally connect
78 	 */
79 	XDB_CONNECTED,
80 	/*
81 	 * frontend xenbus state changed to XenbusStateClosed,
82 	 * interface disconnected
83 	 */
84 	XDB_DISCONNECTED
85 };
86 
87 /*
88  * backend device status
89  */
90 enum xdb_dev_state {
91 	/* initial state */
92 	XDB_DEV_UNKNOWN,
93 	/* backend device is ready (hotplug script finishes successfully) */
94 	XDB_DEV_READY
95 };
96 
97 /*
98  * frontend status
99  */
100 enum xdb_fe_state {
101 	/* initial state */
102 	XDB_FE_UNKNOWN,
103 	/*
104 	 * frontend's xenbus state has changed to
105 	 * XenbusStateInitialised, is ready for connecting
106 	 */
107 	XDB_FE_READY
108 };
109 
110 /*
111  * Other handy macrosx
112  */
113 #define	XDB_MINOR2INST(m)	(int)(m)
114 #define	XDB_INST2MINOR(i)	(minor_t)(i)
115 #define	XDB_INST2SOFTS(instance)			\
116 	((xdb_t *)ddi_get_soft_state(xdb_statep, (instance)))
117 #define	XDB_MAX_IO_PAGES BLKIF_RING_SIZE * BLKIF_MAX_SEGMENTS_PER_REQUEST
118 /* get kva of a mapped-in page coresponding to (xreq-index, seg) pair */
119 #define	XDB_IOPAGE_VA(_pagebase, _xreqidx, _seg)	\
120 	((_pagebase) + ((_xreqidx)			\
121 	* BLKIF_MAX_SEGMENTS_PER_REQUEST		\
122 	+ (_seg)) * PAGESIZE)
123 #define	XDB_XREQ2BP(xreq) (&(xreq)->xr_buf)
124 #define	XDB_BP2XREQ(bp) \
125 	((xdb_request_t *)((char *)(bp) - offsetof(xdb_request_t, xr_buf)))
126 
127 /* describe one blkif segment */
128 typedef struct xdb_seg {
129 	uint8_t fs; /* start sector # within this page (segment) */
130 	uint8_t ls; /* end sector # within this page (segment) */
131 } xdb_seg_t;
132 
133 typedef struct xdb xdb_t;
134 
135 /* one blkif_request_t matches one xdb_request_t */
136 typedef struct xdb_request {
137 	/* buf associated with this I/O request */
138 	buf_t		xr_buf;
139 	/* softstate instance associated with this I/O request */
140 	xdb_t		*xr_vdp;
141 	/* the next segment we're going to process */
142 	int		xr_curseg;
143 	/* index of this xdb_request_t in vdp->xs_req */
144 	int		xr_idx;
145 	/* next index for a statical linked list */
146 	int		xr_next;
147 	/* 'id' copied from blkif_request_t */
148 	uint64_t	xr_id;
149 	/* 'operation' copied from blkif_request_t */
150 	uint8_t		xr_op;
151 	/* how many pages(segments) in this I/O request */
152 	uint8_t		xr_buf_pages;
153 	/* all segments of this I/O request */
154 	xdb_seg_t	xr_segs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
155 	/* all grant table handles used in this I/O request */
156 	grant_handle_t	xr_page_hdls[BLKIF_MAX_SEGMENTS_PER_REQUEST];
157 	struct page	xr_plist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
158 	struct page	*xr_pplist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
159 } xdb_request_t;
160 
161 /* Soft state data structure for each backend vbd */
162 struct xdb {
163 	/* devinfo node pointer of this xdb */
164 	dev_info_t	*xs_dip;
165 	/* coresponding frontend domain id */
166 	domid_t		xs_peer;
167 	/* read-only, removable, cdrom? */
168 	uint32_t	xs_type;
169 	/* # of total sectors */
170 	uint64_t	xs_sectors;
171 	/* blkif I/O request ring buffer */
172 	xendev_ring_t	*xs_ring;
173 	/* handle to access the ring buffer */
174 	ddi_acc_handle_t xs_ring_hdl;
175 	ldi_ident_t	xs_ldi_li;
176 	ldi_handle_t	xs_ldi_hdl;
177 	/* base kva for mapped-in I/O page from frontend domain */
178 	caddr_t		xs_iopage_va;
179 	/* mutex lock for I/O related code path */
180 	kmutex_t	xs_iomutex;
181 	/*
182 	 * mutex lock for event handling related code path
183 	 * need to be grabbed before xs_iomutex
184 	 */
185 	kmutex_t	xs_cbmutex;
186 	/* # of on-going I/O buf in backend domain */
187 	uint_t		xs_ionum;
188 	/* task thread for pushing buf to underlying target driver */
189 	ddi_taskq_t	*xs_iotaskq;
190 	/* cv used in I/O code path, protected by xs_iomutex */
191 	kcondvar_t	xs_iocv;
192 	kcondvar_t	xs_ionumcv;
193 	/*
194 	 * head and tail of linked list for I/O bufs need to be pushed to
195 	 * underlying target driver
196 	 */
197 	buf_t		*xs_f_iobuf;
198 	buf_t		*xs_l_iobuf;
199 	/* xdb interface status */
200 	enum xdb_state	xs_if_status;
201 	/* backend device status */
202 	enum xdb_dev_state xs_dev_status;
203 	/* frontend status */
204 	enum xdb_fe_state xs_fe_status;
205 	/* head of free list of xdb_request_t */
206 	int		xs_free_req;
207 	/* pre-allocated xdb_request_t pool */
208 	xdb_request_t	xs_req[BLKIF_RING_SIZE];
209 	kstat_t		*xs_kstats;
210 	uint64_t	xs_stat_req_reads;
211 	uint64_t	xs_stat_req_writes;
212 	uint64_t	xs_stat_req_barriers;
213 	uint64_t	xs_stat_req_flushes;
214 #ifdef DEBUG
215 	uint64_t page_addrs[XDB_MAX_IO_PAGES]; /* for debug aid */
216 #endif /* DEBUG */
217 };
218 
219 #ifdef __cplusplus
220 }
221 #endif
222 
223 #endif	/* _SYS_XDB_H */
224