1 /*	$OpenBSD: disk.h,v 1.12 2004/02/15 02:45:46 tedu Exp $	*/
2 /*	$NetBSD: disk.h,v 1.11 1996/04/28 20:22:50 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1995 Jason R. Thorpe.  All rights reserved.
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Lawrence Berkeley Laboratory.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  * from: Header: disk.h,v 1.5 92/11/19 04:33:03 torek Exp  (LBL)
43  *
44  *	@(#)disk.h	8.1 (Berkeley) 6/2/93
45  */
46 
47 /*
48  * Disk device structures.
49  */
50 
51 #include <sys/time.h>
52 #include <sys/queue.h>
53 #include <sys/lock.h>
54 
55 struct buf;
56 struct disklabel;
57 struct cpu_disklabel;
58 
59 #define DS_DISKNAMELEN	16
60 
61 struct diskstats {
62 	char		ds_name[DS_DISKNAMELEN];
63 	int		ds_busy;	/* busy counter */
64 	u_int64_t	ds_rxfer;	/* total number of read transfers */
65 	u_int64_t	ds_wxfer;	/* total number of write transfers */
66 	u_int64_t	ds_seek;	/* total independent seek operations */
67 	u_int64_t	ds_rbytes;	/* total bytes read */
68 	u_int64_t	ds_wbytes;	/* total bytes written */
69 	struct timeval	ds_attachtime;	/* time disk was attached */
70 	struct timeval	ds_timestamp;	/* timestamp of last unbusy */
71 	struct timeval	ds_time;	/* total time spent busy */
72 };
73 
74 struct disk {
75 	TAILQ_ENTRY(disk) dk_link;	/* link in global disklist */
76 	struct lock	dk_lock;	/* disk lock */
77 	char		*dk_name;	/* disk name */
78 	int		dk_flags;	/* disk flags */
79 #define DKF_CONSTRUCTED  0x0001
80 
81 	/*
82 	 * Metrics data; note that some metrics may have no meaning
83 	 * on certain types of disks.
84 	 */
85 	int		dk_busy;	/* busy counter */
86 	u_int64_t	dk_rxfer;	/* total number of read transfers */
87 	u_int64_t	dk_wxfer;	/* total number of write transfers */
88 	u_int64_t	dk_seek;	/* total independent seek operations */
89 	u_int64_t	dk_rbytes;	/* total bytes read */
90 	u_int64_t	dk_wbytes;	/* total bytes written */
91 	struct timeval	dk_attachtime;	/* time disk was attached */
92 	struct timeval	dk_timestamp;	/* timestamp of last unbusy */
93 	struct timeval	dk_time;	/* total time spent busy */
94 
95 	int		dk_bopenmask;	/* block devices open */
96 	int		dk_copenmask;	/* character devices open */
97 	int		dk_openmask;	/* composite (bopen|copen) */
98 	int		dk_state;	/* label state   ### */
99 	int		dk_blkshift;	/* shift to convert DEV_BSIZE to blks*/
100 	int		dk_byteshift;	/* shift to convert bytes to blks */
101 
102 	struct	dkdriver *dk_driver;	/* pointer to driver */
103 
104 	/*
105 	 * Disk label information.  Storage for the in-core disk label
106 	 * must be dynamically allocated, otherwise the size of this
107 	 * structure becomes machine-dependent.
108 	 */
109 	daddr_t		dk_labelsector;		/* sector containing label */
110 	struct disklabel *dk_label;	/* label */
111 	struct cpu_disklabel *dk_cpulabel;
112 };
113 
114 struct dkdriver {
115 	void	(*d_strategy)(struct buf *);
116 #ifdef notyet
117 	int	(*d_open)(dev_t dev, int ifmt, int, struct proc *);
118 	int	(*d_close)(dev_t dev, int, int ifmt, struct proc *);
119 	int	(*d_ioctl)(dev_t dev, u_long cmd, caddr_t data, int fflag,
120 				struct proc *);
121 	int	(*d_dump)(dev_t);
122 	void	(*d_start)(struct buf *, daddr_t);
123 	int	(*d_mklabel)(struct disk *);
124 #endif
125 };
126 
127 /* states */
128 #define	DK_CLOSED	0		/* drive is closed */
129 #define	DK_WANTOPEN	1		/* drive being opened */
130 #define	DK_WANTOPENRAW	2		/* drive being opened */
131 #define	DK_RDLABEL	3		/* label being read */
132 #define	DK_OPEN		4		/* label read, drive open */
133 #define	DK_OPENRAW	5		/* open without label */
134 
135 #ifdef DISKSORT_STATS
136 /*
137  * Stats from disksort().
138  */
139 struct disksort_stats {
140 	long	ds_newhead;		/* # new queue heads created */
141 	long	ds_newtail;		/* # new queue tails created */
142 	long	ds_midfirst;		/* # insertions into sort list */
143 	long	ds_endfirst;		/* # insertions at end of sort list */
144 	long	ds_newsecond;		/* # inversions (2nd lists) created */
145 	long	ds_midsecond;		/* # insertions into 2nd list */
146 	long	ds_endsecond;		/* # insertions at end of 2nd list */
147 };
148 #endif
149 
150 /*
151  * disklist_head is defined here so that user-land has access to it.
152  */
153 TAILQ_HEAD(disklist_head, disk);	/* the disklist is a TAILQ */
154 
155 #ifdef _KERNEL
156 extern	int disk_count;			/* number of disks in global disklist */
157 extern	int disk_change;		/* disk attached/detached */
158 
159 void	disk_init(void);
160 int	disk_construct(struct disk *, char *);
161 void	disk_attach(struct disk *);
162 void	disk_detach(struct disk *);
163 void	disk_busy(struct disk *);
164 void	disk_unbusy(struct disk *, long, int);
165 void	disk_resetstat(struct disk *);
166 struct	disk *disk_find(char *);
167 
168 int	disk_lock(struct disk *);
169 void    disk_unlock(struct disk *);
170 
171 struct device;
172 void	dk_establish(struct disk *, struct device *);
173 #endif
174