xref: /netbsd-src/sys/sys/disk.h (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: disk.h,v 1.28 2004/10/23 17:16:45 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1992, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * This software was developed by the Computer Systems Engineering group
45  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
46  * contributed to Berkeley.
47  *
48  * All advertising materials mentioning features or use of this software
49  * must display the following acknowledgement:
50  *	This product includes software developed by the University of
51  *	California, Lawrence Berkeley Laboratories.
52  *
53  * Redistribution and use in source and binary forms, with or without
54  * modification, are permitted provided that the following conditions
55  * are met:
56  * 1. Redistributions of source code must retain the above copyright
57  *    notice, this list of conditions and the following disclaimer.
58  * 2. Redistributions in binary form must reproduce the above copyright
59  *    notice, this list of conditions and the following disclaimer in the
60  *    documentation and/or other materials provided with the distribution.
61  * 3. Neither the name of the University nor the names of its contributors
62  *    may be used to endorse or promote products derived from this software
63  *    without specific prior written permission.
64  *
65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75  * SUCH DAMAGE.
76  *
77  * from: Header: disk.h,v 1.5 92/11/19 04:33:03 torek Exp  (LBL)
78  *
79  *	@(#)disk.h	8.2 (Berkeley) 1/9/95
80  */
81 
82 #ifndef _SYS_DISK_H_
83 #define _SYS_DISK_H_
84 
85 /*
86  * Disk device structures.
87  */
88 
89 #include <sys/dkio.h>
90 #include <sys/time.h>
91 #include <sys/queue.h>
92 #include <sys/lock.h>
93 
94 struct buf;
95 struct disk;
96 struct disklabel;
97 struct cpu_disklabel;
98 struct vnode;
99 
100 /*
101  * dkwedge_info:
102  *
103  *	Information needed to configure (or query configuration of) a
104  *	disk wedge.
105  */
106 struct dkwedge_info {
107 	char		dkw_devname[16];/* device-style name (e.g. "dk0") */
108 	uint8_t		dkw_wname[128];	/* wedge name (Unicode, UTF-8) */
109 	char		dkw_parent[16];	/* parent disk device name */
110 	daddr_t		dkw_offset;	/* LBA offset of wedge in parent */
111 	uint64_t	dkw_size;	/* size of wedge in blocks */
112 	char		dkw_ptype[32];	/* partition type string */
113 };
114 
115 /*
116  * dkwedge_list:
117  *
118  *	Structure used to query a list of wedges.
119  */
120 struct dkwedge_list {
121 	void		*dkwl_buf;	/* storage for dkwedge_info array */
122 	size_t		dkwl_bufsize;	/* size of that buffer */
123 	u_int		dkwl_nwedges;	/* total number of wedges */
124 	u_int		dkwl_ncopied;	/* number actually copied */
125 };
126 
127 #ifdef _KERNEL
128 /*
129  * dkwedge_discovery_method:
130  *
131  *	Structure used to describe partition map parsing schemes
132  *	used for wedge autodiscovery.
133  */
134 struct dkwedge_discovery_method {
135 					/* link in wedge driver's list */
136 	LIST_ENTRY(dkwedge_discovery_method) ddm_list;
137 	const char	*ddm_name;	/* name of this method */
138 	int		ddm_priority;	/* search priority */
139 	int		(*ddm_discover)(struct disk *, struct vnode *);
140 };
141 
142 #define	DKWEDGE_DISCOVERY_METHOD_DECL(name, prio, discover)		\
143 static struct dkwedge_discovery_method name ## _ddm = {			\
144 	{ 0 },								\
145 	#name,								\
146 	prio,								\
147 	discover							\
148 };									\
149 __link_set_add_data(dkwedge_methods, name ## _ddm)
150 #endif /* _KERNEL */
151 
152 /* Some common partition types */
153 #define	DKW_PTYPE_UNKNOWN	""
154 #define	DKW_PTYPE_UNUSED	"unused"
155 #define	DKW_PTYPE_SWAP		"swap"
156 #define	DKW_PTYPE_FFS		"ffs"
157 #define	DKW_PTYPE_LFS		"lfs"
158 #define	DKW_PTYPE_EXT2FS	"ext2fs"
159 #define	DKW_PTYPE_ISO9660	"cd9660"
160 #define	DKW_PTYPE_AMIGADOS	"ados"
161 #define	DKW_PTYPE_APPLEHFS	"hfs"
162 #define	DKW_PTYPE_FAT		"msdos"
163 #define	DKW_PTYPE_FILECORE	"filecore"
164 #define	DKW_PTYPE_RAIDFRAME	"raidframe"
165 #define	DKW_PTYPE_CCD		"ccd"
166 #define	DKW_PTYPE_APPLEUFS	"appleufs"
167 #define	DKW_PTYPE_NTFS		"ntfs"
168 
169 /*
170  * Disk geometry information.
171  *
172  * NOTE: Not all geometry information is relevant for every kind of disk.
173  */
174 struct disk_geom {
175 	int64_t		dg_secperunit;	/* # of data sectors per unit */
176 	uint32_t	dg_secsize;	/* # of bytes per sector */
177 	uint32_t	dg_nsectors;	/* # of data sectors per track */
178 	uint32_t	dg_ntracks;	/* # of tracks per cylinder */
179 	uint32_t	dg_ncylinders;	/* # of data cylinders per unit */
180 	uint32_t	dg_secpercyl;	/* # of data sectors per cylinder */
181 	uint32_t	dg_pcylinders;	/* # of physical cylinders per unit */
182 
183 	/*
184 	 * Spares (bad sector replacements) below are not counted in
185 	 * dg_nsectors or dg_secpercyl.  Spare sectors are assumed to
186 	 * be physical sectors which occupy space at the end of each
187 	 * track and/or cylinder.
188 	 */
189 	uint32_t	dg_sparespertrack;
190 	uint32_t	dg_sparespercyl;
191 	/*
192 	 * Alternative cylinders include maintenance, replacement,
193 	 * configuration description areas, etc.
194 	 */
195 	uint32_t	dg_acylinders;
196 };
197 
198 struct disk {
199 	TAILQ_ENTRY(disk) dk_link;	/* link in global disklist */
200 	char		*dk_name;	/* disk name */
201 	int		dk_bopenmask;	/* block devices open */
202 	int		dk_copenmask;	/* character devices open */
203 	int		dk_openmask;	/* composite (bopen|copen) */
204 	int		dk_state;	/* label state   ### */
205 	int		dk_blkshift;	/* shift to convert DEV_BSIZE to blks */
206 	int		dk_byteshift;	/* shift to convert bytes to blks */
207 
208 	/*
209 	 * Metrics data; note that some metrics may have no meaning
210 	 * on certain types of disks.
211 	 */
212 	int		dk_busy;	/* busy counter */
213 	u_int64_t	dk_rxfer;	/* total number of read transfers */
214 	u_int64_t	dk_wxfer;	/* total number of write transfers */
215 	u_int64_t	dk_seek;	/* total independent seek operations */
216 	u_int64_t	dk_rbytes;	/* total bytes read */
217 	u_int64_t	dk_wbytes;	/* total bytes written */
218 	struct timeval	dk_attachtime;	/* time disk was attached */
219 	struct timeval	dk_timestamp;	/* timestamp of last unbusy */
220 	struct timeval	dk_time;	/* total time spent busy */
221 
222 	struct	dkdriver *dk_driver;	/* pointer to driver */
223 
224 	/*
225 	 * Information required to be the parent of a disk wedge.
226 	 */
227 	struct lock	dk_rawlock;	/* lock on these fields */
228 	struct vnode	*dk_rawvp;	/* vnode for the RAW_PART bdev */
229 	u_int		dk_rawopens;	/* # of openes of rawvp */
230 
231 	struct lock	dk_openlock;	/* lock on these and openmask */
232 	u_int		dk_nwedges;	/* # of configured wedges */
233 					/* all wedges on this disk */
234 	LIST_HEAD(, dkwedge_softc) dk_wedges;
235 
236 	/*
237 	 * Disk label information.  Storage for the in-core disk label
238 	 * must be dynamically allocated, otherwise the size of this
239 	 * structure becomes machine-dependent.
240 	 */
241 	daddr_t		dk_labelsector;		/* sector containing label */
242 	struct disklabel *dk_label;	/* label */
243 	struct cpu_disklabel *dk_cpulabel;
244 };
245 
246 #define	DK_DISKNAMELEN	16
247 
248 /* The following structure is 64-bit alignment safe */
249 struct disk_sysctl {
250 	char		dk_name[DK_DISKNAMELEN];
251 	int32_t		dk_busy;
252 	int32_t		pad;
253 	u_int64_t	dk_xfer;
254 	u_int64_t	dk_seek;
255 	u_int64_t	dk_bytes;
256 	u_int32_t	dk_attachtime_sec;
257 	u_int32_t	dk_attachtime_usec;
258 	u_int32_t	dk_timestamp_sec;
259 	u_int32_t	dk_timestamp_usec;
260 	u_int32_t	dk_time_sec;
261 	u_int32_t	dk_time_usec;
262 	/* New separate read/write stats */
263 	u_int64_t	dk_rxfer;
264 	u_int64_t	dk_rbytes;
265 	u_int64_t	dk_wxfer;
266 	u_int64_t	dk_wbytes;
267 };
268 
269 struct dkdriver {
270 	void	(*d_strategy) __P((struct buf *));
271 	void	(*d_minphys) __P((struct buf *));
272 #ifdef notyet
273 	int	(*d_open) __P((dev_t, int, int, struct proc *));
274 	int	(*d_close) __P((dev_t, int, int, struct proc *));
275 	int	(*d_ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
276 	int	(*d_dump) __P((dev_t));
277 	void	(*d_start) __P((struct buf *, daddr_t));
278 	int	(*d_mklabel) __P((struct disk *));
279 #endif
280 };
281 
282 /* states */
283 #define	DK_CLOSED	0		/* drive is closed */
284 #define	DK_WANTOPEN	1		/* drive being opened */
285 #define	DK_WANTOPENRAW	2		/* drive being opened */
286 #define	DK_RDLABEL	3		/* label being read */
287 #define	DK_OPEN		4		/* label read, drive open */
288 #define	DK_OPENRAW	5		/* open without label */
289 
290 /*
291  * disklist_head is defined here so that user-land has access to it.
292  */
293 TAILQ_HEAD(disklist_head, disk);	/* the disklist is a TAILQ */
294 
295 /*
296  * Bad sector lists per fixed disk
297  */
298 struct disk_badsectors {
299 	SLIST_ENTRY(disk_badsectors)	dbs_next;
300 	daddr_t		dbs_min;	/* min. sector number */
301 	daddr_t		dbs_max;	/* max. sector number */
302 	struct timeval	dbs_failedat;	/* first failure at */
303 };
304 
305 struct disk_badsecinfo {
306 	u_int32_t	dbsi_bufsize;	/* size of region pointed to */
307 	u_int32_t	dbsi_skip;	/* how many to skip past */
308 	u_int32_t	dbsi_copied;	/* how many got copied back */
309 	u_int32_t	dbsi_left;	/* remaining to copy */
310 	caddr_t		dbsi_buffer;	/* region to copy disk_badsectors to */
311 };
312 
313 #ifdef _KERNEL
314 extern	int disk_count;			/* number of disks in global disklist */
315 
316 struct device;
317 struct proc;
318 
319 void	disk_attach __P((struct disk *));
320 void	disk_detach __P((struct disk *));
321 void	disk_busy __P((struct disk *));
322 void	disk_unbusy __P((struct disk *, long, int));
323 void	disk_resetstat __P((struct disk *));
324 struct	disk *disk_find __P((char *));
325 
326 int	dkwedge_add(struct dkwedge_info *);
327 int	dkwedge_del(struct dkwedge_info *);
328 void	dkwedge_delall(struct disk *);
329 int	dkwedge_list(struct disk *, struct dkwedge_list *, struct proc *);
330 void	dkwedge_discover(struct disk *);
331 void	dkwedge_set_bootwedge(struct device *, daddr_t, uint64_t);
332 int	dkwedge_read(struct disk *, struct vnode *, daddr_t, void *, size_t);
333 #endif
334 
335 #endif /* _SYS_DISK_H_ */
336