xref: /minix3/minix/drivers/storage/memory/memory.c (revision 1aad172900a22f403baf366fb7db7ee4560c7544)
1433d6423SLionel Sambuc /* This file contains the device dependent part of the drivers for the
2433d6423SLionel Sambuc  * following special files:
3433d6423SLionel Sambuc  *     /dev/ram		- RAM disk
4433d6423SLionel Sambuc  *     /dev/mem		- absolute memory
5433d6423SLionel Sambuc  *     /dev/kmem	- kernel virtual memory
6433d6423SLionel Sambuc  *     /dev/null	- null device (data sink)
7433d6423SLionel Sambuc  *     /dev/boot	- boot device loaded from boot image
8433d6423SLionel Sambuc  *     /dev/zero	- null byte stream generator
9433d6423SLionel Sambuc  *     /dev/imgrd	- boot image RAM disk
10433d6423SLionel Sambuc  *
11433d6423SLionel Sambuc  *  Changes:
12433d6423SLionel Sambuc  *	Apr 29, 2005	added null byte generator  (Jorrit N. Herder)
13433d6423SLionel Sambuc  *	Apr 09, 2005	added support for boot device  (Jorrit N. Herder)
14433d6423SLionel Sambuc  *	Jul 26, 2004	moved RAM driver to user-space  (Jorrit N. Herder)
15433d6423SLionel Sambuc  *	Apr 20, 1992	device dependent/independent split  (Kees J. Bot)
16433d6423SLionel Sambuc  */
17433d6423SLionel Sambuc 
18433d6423SLionel Sambuc #include <assert.h>
19433d6423SLionel Sambuc #include <minix/drivers.h>
20433d6423SLionel Sambuc #include <minix/chardriver.h>
21433d6423SLionel Sambuc #include <minix/blockdriver.h>
22433d6423SLionel Sambuc #include <sys/ioc_memory.h>
23433d6423SLionel Sambuc #include <minix/ds.h>
24433d6423SLionel Sambuc #include <minix/vm.h>
25433d6423SLionel Sambuc #include <machine/param.h>
26433d6423SLionel Sambuc #include <machine/vmparam.h>
27433d6423SLionel Sambuc #include <sys/mman.h>
28433d6423SLionel Sambuc #include "kernel/const.h"
29433d6423SLionel Sambuc #include "kernel/config.h"
30433d6423SLionel Sambuc #include "kernel/type.h"
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc #include <machine/vm.h>
33433d6423SLionel Sambuc 
34433d6423SLionel Sambuc #include "local.h"
35433d6423SLionel Sambuc 
36433d6423SLionel Sambuc /* ramdisks (/dev/ram*) */
37433d6423SLionel Sambuc #define RAMDISKS     6
38433d6423SLionel Sambuc 
39433d6423SLionel Sambuc #define RAM_DEV_LAST (RAM_DEV_FIRST+RAMDISKS-1)
40433d6423SLionel Sambuc 
41433d6423SLionel Sambuc #define NR_DEVS            (7+RAMDISKS)	/* number of minor devices */
42433d6423SLionel Sambuc 
43433d6423SLionel Sambuc static struct device m_geom[NR_DEVS];  /* base and size of each device */
44433d6423SLionel Sambuc static vir_bytes m_vaddrs[NR_DEVS];
45433d6423SLionel Sambuc 
46433d6423SLionel Sambuc static int openct[NR_DEVS];
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc static ssize_t m_char_read(devminor_t minor, u64_t position, endpoint_t endpt,
49433d6423SLionel Sambuc 	cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
50433d6423SLionel Sambuc static ssize_t m_char_write(devminor_t minor, u64_t position, endpoint_t endpt,
51433d6423SLionel Sambuc 	cp_grant_id_t grant, size_t size, int flags, cdev_id_t id);
52433d6423SLionel Sambuc static int m_char_open(devminor_t minor, int access, endpoint_t user_endpt);
53433d6423SLionel Sambuc static int m_char_close(devminor_t minor);
54433d6423SLionel Sambuc 
55433d6423SLionel Sambuc static struct device *m_block_part(devminor_t minor);
56433d6423SLionel Sambuc static ssize_t m_block_transfer(devminor_t minor, int do_write, u64_t position,
57433d6423SLionel Sambuc 	endpoint_t endpt, iovec_t *iov, unsigned int nr_req, int flags);
58433d6423SLionel Sambuc static int m_block_open(devminor_t minor, int access);
59433d6423SLionel Sambuc static int m_block_close(devminor_t minor);
60433d6423SLionel Sambuc static int m_block_ioctl(devminor_t minor, unsigned long request, endpoint_t
61433d6423SLionel Sambuc 	endpt, cp_grant_id_t grant, endpoint_t user_endpt);
62433d6423SLionel Sambuc 
63433d6423SLionel Sambuc /* Entry points to the CHARACTER part of this driver. */
64433d6423SLionel Sambuc static struct chardriver m_cdtab = {
65433d6423SLionel Sambuc   .cdr_open	= m_char_open,		/* open device */
66433d6423SLionel Sambuc   .cdr_close	= m_char_close,		/* close device */
67433d6423SLionel Sambuc   .cdr_read	= m_char_read,		/* read from device */
68433d6423SLionel Sambuc   .cdr_write	= m_char_write		/* write to device */
69433d6423SLionel Sambuc };
70433d6423SLionel Sambuc 
71433d6423SLionel Sambuc /* Entry points to the BLOCK part of this driver. */
72433d6423SLionel Sambuc static struct blockdriver m_bdtab = {
73433d6423SLionel Sambuc   .bdr_type	= BLOCKDRIVER_TYPE_DISK,/* handle partition requests */
74433d6423SLionel Sambuc   .bdr_open	= m_block_open,		/* open device */
75433d6423SLionel Sambuc   .bdr_close	= m_block_close,	/* nothing on a close */
76433d6423SLionel Sambuc   .bdr_transfer	= m_block_transfer,	/* do the I/O */
77433d6423SLionel Sambuc   .bdr_ioctl	= m_block_ioctl,	/* ram disk I/O control */
78433d6423SLionel Sambuc   .bdr_part	= m_block_part		/* return partition information */
79433d6423SLionel Sambuc };
80433d6423SLionel Sambuc 
81433d6423SLionel Sambuc /* SEF functions and variables. */
82433d6423SLionel Sambuc static void sef_local_startup(void);
83433d6423SLionel Sambuc static int sef_cb_init_fresh(int type, sef_init_info_t *info);
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc /*===========================================================================*
86433d6423SLionel Sambuc  *				   main 				     *
87433d6423SLionel Sambuc  *===========================================================================*/
main(void)88433d6423SLionel Sambuc int main(void)
89433d6423SLionel Sambuc {
90433d6423SLionel Sambuc   message msg;
91433d6423SLionel Sambuc   int r, ipc_status;
92433d6423SLionel Sambuc 
93433d6423SLionel Sambuc   /* SEF local startup. */
94433d6423SLionel Sambuc   sef_local_startup();
95433d6423SLionel Sambuc 
96433d6423SLionel Sambuc   /* The receive loop. */
97433d6423SLionel Sambuc   for (;;) {
98433d6423SLionel Sambuc 	if ((r = driver_receive(ANY, &msg, &ipc_status)) != OK)
99433d6423SLionel Sambuc 		panic("memory: driver_receive failed (%d)", r);
100433d6423SLionel Sambuc 
101433d6423SLionel Sambuc 	if (IS_BDEV_RQ(msg.m_type))
102433d6423SLionel Sambuc 		blockdriver_process(&m_bdtab, &msg, ipc_status);
103433d6423SLionel Sambuc 	else
104433d6423SLionel Sambuc 		chardriver_process(&m_cdtab, &msg, ipc_status);
105433d6423SLionel Sambuc   }
106433d6423SLionel Sambuc 
107433d6423SLionel Sambuc   return(OK);
108433d6423SLionel Sambuc }
109433d6423SLionel Sambuc 
110433d6423SLionel Sambuc /*===========================================================================*
111433d6423SLionel Sambuc  *			       sef_local_startup			     *
112433d6423SLionel Sambuc  *===========================================================================*/
sef_local_startup()113433d6423SLionel Sambuc static void sef_local_startup()
114433d6423SLionel Sambuc {
115433d6423SLionel Sambuc   /* Register init callbacks. */
116433d6423SLionel Sambuc   sef_setcb_init_fresh(sef_cb_init_fresh);
117*2867e60aSDavid van Moolenbroek   sef_setcb_init_restart(SEF_CB_INIT_RESTART_STATEFUL);
118433d6423SLionel Sambuc 
119433d6423SLionel Sambuc   /* Let SEF perform startup. */
120433d6423SLionel Sambuc   sef_startup();
121433d6423SLionel Sambuc }
122433d6423SLionel Sambuc 
123433d6423SLionel Sambuc /*===========================================================================*
124433d6423SLionel Sambuc  *		            sef_cb_init_fresh                                *
125433d6423SLionel Sambuc  *===========================================================================*/
sef_cb_init_fresh(int type,sef_init_info_t * UNUSED (info))126f837aff6SLionel Sambuc static int sef_cb_init_fresh(int type, sef_init_info_t *UNUSED(info))
127433d6423SLionel Sambuc {
128433d6423SLionel Sambuc /* Initialize the memory driver. */
129433d6423SLionel Sambuc   int i;
130433d6423SLionel Sambuc #if 0
131433d6423SLionel Sambuc   struct kinfo kinfo;		/* kernel information */
132433d6423SLionel Sambuc   int s;
133433d6423SLionel Sambuc 
134433d6423SLionel Sambuc   if (OK != (s=sys_getkinfo(&kinfo))) {
135433d6423SLionel Sambuc       panic("Couldn't get kernel information: %d", s);
136433d6423SLionel Sambuc   }
137433d6423SLionel Sambuc 
138433d6423SLionel Sambuc   /* Map in kernel memory for /dev/kmem. */
139433d6423SLionel Sambuc   m_geom[KMEM_DEV].dv_base = kinfo.kmem_base;
140433d6423SLionel Sambuc   m_geom[KMEM_DEV].dv_size = kinfo.kmem_size;
141433d6423SLionel Sambuc   if((m_vaddrs[KMEM_DEV] = vm_map_phys(SELF, (void *) kinfo.kmem_base,
142433d6423SLionel Sambuc 	kinfo.kmem_size)) == MAP_FAILED) {
143433d6423SLionel Sambuc 	printf("MEM: Couldn't map in /dev/kmem.");
144433d6423SLionel Sambuc   }
145433d6423SLionel Sambuc #endif
146433d6423SLionel Sambuc 
147433d6423SLionel Sambuc   /* Ramdisk image built into the memory driver */
148433d6423SLionel Sambuc   m_geom[IMGRD_DEV].dv_base= 0;
149433d6423SLionel Sambuc   m_geom[IMGRD_DEV].dv_size= imgrd_size;
150433d6423SLionel Sambuc   m_vaddrs[IMGRD_DEV] = (vir_bytes) imgrd;
151433d6423SLionel Sambuc 
152433d6423SLionel Sambuc   for(i = 0; i < NR_DEVS; i++)
153433d6423SLionel Sambuc 	openct[i] = 0;
154433d6423SLionel Sambuc 
155433d6423SLionel Sambuc   /* Set up memory range for /dev/mem. */
156433d6423SLionel Sambuc   m_geom[MEM_DEV].dv_base = 0;
157433d6423SLionel Sambuc   m_geom[MEM_DEV].dv_size = 0xffffffffULL;
158433d6423SLionel Sambuc 
159433d6423SLionel Sambuc   m_vaddrs[MEM_DEV] = (vir_bytes) MAP_FAILED; /* we are not mapping this in. */
160433d6423SLionel Sambuc 
161f837aff6SLionel Sambuc   chardriver_announce();
162f837aff6SLionel Sambuc   blockdriver_announce(type);
163f837aff6SLionel Sambuc 
164433d6423SLionel Sambuc   return(OK);
165433d6423SLionel Sambuc }
166433d6423SLionel Sambuc 
167433d6423SLionel Sambuc /*===========================================================================*
168433d6423SLionel Sambuc  *				m_is_block				     *
169433d6423SLionel Sambuc  *===========================================================================*/
m_is_block(devminor_t minor)170433d6423SLionel Sambuc static int m_is_block(devminor_t minor)
171433d6423SLionel Sambuc {
172433d6423SLionel Sambuc /* Return TRUE iff the given minor device number is for a block device. */
173433d6423SLionel Sambuc 
174433d6423SLionel Sambuc   switch (minor) {
175433d6423SLionel Sambuc   case MEM_DEV:
176433d6423SLionel Sambuc   case KMEM_DEV:
177433d6423SLionel Sambuc   case NULL_DEV:
178433d6423SLionel Sambuc   case ZERO_DEV:
179433d6423SLionel Sambuc 	return FALSE;
180433d6423SLionel Sambuc 
181433d6423SLionel Sambuc   default:
182433d6423SLionel Sambuc 	return TRUE;
183433d6423SLionel Sambuc   }
184433d6423SLionel Sambuc }
185433d6423SLionel Sambuc 
186433d6423SLionel Sambuc /*===========================================================================*
187433d6423SLionel Sambuc  *				m_transfer_kmem				     *
188433d6423SLionel Sambuc  *===========================================================================*/
m_transfer_kmem(devminor_t minor,int do_write,u64_t position,endpoint_t endpt,cp_grant_id_t grant,size_t size)189433d6423SLionel Sambuc static ssize_t m_transfer_kmem(devminor_t minor, int do_write, u64_t position,
190433d6423SLionel Sambuc 	endpoint_t endpt, cp_grant_id_t grant, size_t size)
191433d6423SLionel Sambuc {
192433d6423SLionel Sambuc /* Transfer from or to the KMEM device. */
193433d6423SLionel Sambuc   u64_t dv_size, dev_vaddr;
194433d6423SLionel Sambuc   int r;
195433d6423SLionel Sambuc 
196433d6423SLionel Sambuc   dv_size = m_geom[minor].dv_size;
197433d6423SLionel Sambuc   dev_vaddr = m_vaddrs[minor];
198433d6423SLionel Sambuc 
199433d6423SLionel Sambuc   if (!dev_vaddr || dev_vaddr == (vir_bytes) MAP_FAILED) {
200433d6423SLionel Sambuc 	printf("MEM: dev %d not initialized\n", minor);
201433d6423SLionel Sambuc 	return EIO;
202433d6423SLionel Sambuc   }
203433d6423SLionel Sambuc 
204433d6423SLionel Sambuc   if (position >= dv_size) return 0;	/* check for EOF */
205433d6423SLionel Sambuc   if (position + size > dv_size) size = dv_size - position;
206433d6423SLionel Sambuc 
207433d6423SLionel Sambuc   if (!do_write)			/* copy actual data */
208433d6423SLionel Sambuc 	r = sys_safecopyto(endpt, grant, 0, dev_vaddr + position, size);
209433d6423SLionel Sambuc   else
210433d6423SLionel Sambuc 	r = sys_safecopyfrom(endpt, grant, 0, dev_vaddr + position, size);
211433d6423SLionel Sambuc 
212433d6423SLionel Sambuc   return (r != OK) ? r : size;
213433d6423SLionel Sambuc }
214433d6423SLionel Sambuc 
215433d6423SLionel Sambuc /*===========================================================================*
216433d6423SLionel Sambuc  *				m_transfer_mem				     *
217433d6423SLionel Sambuc  *===========================================================================*/
m_transfer_mem(devminor_t minor,int do_write,u64_t position,endpoint_t endpt,cp_grant_id_t grant,size_t size)218433d6423SLionel Sambuc static ssize_t m_transfer_mem(devminor_t minor, int do_write, u64_t position,
219433d6423SLionel Sambuc 	endpoint_t endpt, cp_grant_id_t grant, size_t size)
220433d6423SLionel Sambuc {
221433d6423SLionel Sambuc /* Transfer from or to the MEM device. */
222433d6423SLionel Sambuc   static int any_mapped = 0;
223433d6423SLionel Sambuc   static phys_bytes pagestart_mapped;
224433d6423SLionel Sambuc   static char *vaddr;
225433d6423SLionel Sambuc   phys_bytes mem_phys, pagestart;
226433d6423SLionel Sambuc   size_t off, page_off, subcount;
227433d6423SLionel Sambuc   u64_t dv_size;
228433d6423SLionel Sambuc   int r;
229433d6423SLionel Sambuc 
230433d6423SLionel Sambuc   dv_size = m_geom[minor].dv_size;
231433d6423SLionel Sambuc   if (position >= dv_size) return 0;	/* check for EOF */
232433d6423SLionel Sambuc   if (position + size > dv_size) size = dv_size - position;
233433d6423SLionel Sambuc 
234433d6423SLionel Sambuc   /* Physical copying. Only used to access entire memory.
235433d6423SLionel Sambuc    * Transfer one 'page window' at a time.
236433d6423SLionel Sambuc    */
237433d6423SLionel Sambuc   off = 0;
238433d6423SLionel Sambuc   while (off < size) {
239433d6423SLionel Sambuc 	mem_phys = (phys_bytes) position;
240433d6423SLionel Sambuc 
241433d6423SLionel Sambuc 	page_off = (size_t) (mem_phys % PAGE_SIZE);
242433d6423SLionel Sambuc 	pagestart = mem_phys - page_off;
243433d6423SLionel Sambuc 
244433d6423SLionel Sambuc 	/* All memory to the map call has to be page-aligned.
245433d6423SLionel Sambuc 	 * Don't have to map same page over and over.
246433d6423SLionel Sambuc 	 */
247433d6423SLionel Sambuc 	if (!any_mapped || pagestart_mapped != pagestart) {
248433d6423SLionel Sambuc 		if (any_mapped) {
249433d6423SLionel Sambuc 			if (vm_unmap_phys(SELF, vaddr, PAGE_SIZE) != OK)
250433d6423SLionel Sambuc 				panic("vm_unmap_phys failed");
251433d6423SLionel Sambuc 			any_mapped = 0;
252433d6423SLionel Sambuc 		}
253433d6423SLionel Sambuc 
254433d6423SLionel Sambuc 		vaddr = vm_map_phys(SELF, (void *) pagestart, PAGE_SIZE);
255433d6423SLionel Sambuc 		if (vaddr == MAP_FAILED) {
256433d6423SLionel Sambuc 			printf("memory: vm_map_phys failed\n");
257433d6423SLionel Sambuc 			return ENOMEM;
258433d6423SLionel Sambuc 		}
259433d6423SLionel Sambuc 		any_mapped = 1;
260433d6423SLionel Sambuc 		pagestart_mapped = pagestart;
261433d6423SLionel Sambuc 	}
262433d6423SLionel Sambuc 
263433d6423SLionel Sambuc 	/* how much to be done within this page. */
264433d6423SLionel Sambuc 	subcount = PAGE_SIZE - page_off;
265433d6423SLionel Sambuc 	if (subcount > size)
266433d6423SLionel Sambuc 		subcount = size;
267433d6423SLionel Sambuc 
268433d6423SLionel Sambuc 	if (!do_write)	/* copy data */
269433d6423SLionel Sambuc 		r = sys_safecopyto(endpt, grant, off,
270433d6423SLionel Sambuc 			(vir_bytes) vaddr + page_off, subcount);
271433d6423SLionel Sambuc 	else
272433d6423SLionel Sambuc 		r = sys_safecopyfrom(endpt, grant, off,
273433d6423SLionel Sambuc 			(vir_bytes) vaddr + page_off, subcount);
274433d6423SLionel Sambuc 	if (r != OK)
275433d6423SLionel Sambuc 		return r;
276433d6423SLionel Sambuc 
277433d6423SLionel Sambuc 	position += subcount;
278433d6423SLionel Sambuc 	off += subcount;
279433d6423SLionel Sambuc   }
280433d6423SLionel Sambuc 
281433d6423SLionel Sambuc   return off;
282433d6423SLionel Sambuc }
283433d6423SLionel Sambuc 
284433d6423SLionel Sambuc /*===========================================================================*
285433d6423SLionel Sambuc  *				m_char_read				     *
286433d6423SLionel Sambuc  *===========================================================================*/
m_char_read(devminor_t minor,u64_t position,endpoint_t endpt,cp_grant_id_t grant,size_t size,int UNUSED (flags),cdev_id_t UNUSED (id))287433d6423SLionel Sambuc static ssize_t m_char_read(devminor_t minor, u64_t position, endpoint_t endpt,
288433d6423SLionel Sambuc 	cp_grant_id_t grant, size_t size, int UNUSED(flags),
289433d6423SLionel Sambuc 	cdev_id_t UNUSED(id))
290433d6423SLionel Sambuc {
291433d6423SLionel Sambuc /* Read from one of the driver's character devices. */
292433d6423SLionel Sambuc   ssize_t r;
293433d6423SLionel Sambuc 
294433d6423SLionel Sambuc   /* Check if the minor device number is ok. */
295433d6423SLionel Sambuc   if (minor < 0 || minor >= NR_DEVS || m_is_block(minor)) return ENXIO;
296433d6423SLionel Sambuc 
297433d6423SLionel Sambuc   switch (minor) {
298433d6423SLionel Sambuc   case NULL_DEV:
299433d6423SLionel Sambuc 	r = 0;	/* always at EOF */
300433d6423SLionel Sambuc 	break;
301433d6423SLionel Sambuc 
302433d6423SLionel Sambuc   case ZERO_DEV:
303433d6423SLionel Sambuc 	/* Fill the target area with zeroes. In fact, let the kernel do it! */
304433d6423SLionel Sambuc 	if ((r = sys_safememset(endpt, grant, 0, '\0', size)) == OK)
305433d6423SLionel Sambuc 		r = size;
306433d6423SLionel Sambuc 	break;
307433d6423SLionel Sambuc 
308433d6423SLionel Sambuc   case KMEM_DEV:
309433d6423SLionel Sambuc 	r = m_transfer_kmem(minor, FALSE, position, endpt, grant, size);
310433d6423SLionel Sambuc 	break;
311433d6423SLionel Sambuc 
312433d6423SLionel Sambuc   case MEM_DEV:
313433d6423SLionel Sambuc 	r = m_transfer_mem(minor, FALSE, position, endpt, grant, size);
314433d6423SLionel Sambuc 	break;
315433d6423SLionel Sambuc 
316433d6423SLionel Sambuc   default:
317433d6423SLionel Sambuc 	panic("unknown character device %d", minor);
318433d6423SLionel Sambuc   }
319433d6423SLionel Sambuc 
320433d6423SLionel Sambuc   return r;
321433d6423SLionel Sambuc }
322433d6423SLionel Sambuc 
323433d6423SLionel Sambuc /*===========================================================================*
324433d6423SLionel Sambuc  *				m_char_write				     *
325433d6423SLionel Sambuc  *===========================================================================*/
m_char_write(devminor_t minor,u64_t position,endpoint_t endpt,cp_grant_id_t grant,size_t size,int UNUSED (flags),cdev_id_t UNUSED (id))326433d6423SLionel Sambuc static ssize_t m_char_write(devminor_t minor, u64_t position, endpoint_t endpt,
327433d6423SLionel Sambuc 	cp_grant_id_t grant, size_t size, int UNUSED(flags),
328433d6423SLionel Sambuc 	cdev_id_t UNUSED(id))
329433d6423SLionel Sambuc {
330433d6423SLionel Sambuc /* Write to one of the driver's character devices. */
331433d6423SLionel Sambuc   ssize_t r;
332433d6423SLionel Sambuc 
333433d6423SLionel Sambuc   /* Check if the minor device number is ok. */
334433d6423SLionel Sambuc   if (minor < 0 || minor >= NR_DEVS || m_is_block(minor)) return ENXIO;
335433d6423SLionel Sambuc 
336433d6423SLionel Sambuc   switch (minor) {
337433d6423SLionel Sambuc   case NULL_DEV:
338433d6423SLionel Sambuc   case ZERO_DEV:
339433d6423SLionel Sambuc 	r = size;	/* just eat everything */
340433d6423SLionel Sambuc 	break;
341433d6423SLionel Sambuc 
342433d6423SLionel Sambuc   case KMEM_DEV:
343433d6423SLionel Sambuc 	r = m_transfer_kmem(minor, TRUE, position, endpt, grant, size);
344433d6423SLionel Sambuc 	break;
345433d6423SLionel Sambuc 
346433d6423SLionel Sambuc   case MEM_DEV:
347433d6423SLionel Sambuc 	r = m_transfer_mem(minor, TRUE, position, endpt, grant, size);
348433d6423SLionel Sambuc 	break;
349433d6423SLionel Sambuc 
350433d6423SLionel Sambuc   default:
351433d6423SLionel Sambuc 	panic("unknown character device %d", minor);
352433d6423SLionel Sambuc   }
353433d6423SLionel Sambuc 
354433d6423SLionel Sambuc   return r;
355433d6423SLionel Sambuc }
356433d6423SLionel Sambuc 
357433d6423SLionel Sambuc /*===========================================================================*
358433d6423SLionel Sambuc  *				m_char_open				     *
359433d6423SLionel Sambuc  *===========================================================================*/
m_char_open(devminor_t minor,int access,endpoint_t user_endpt)360433d6423SLionel Sambuc static int m_char_open(devminor_t minor, int access, endpoint_t user_endpt)
361433d6423SLionel Sambuc {
362433d6423SLionel Sambuc /* Open a memory character device. */
363433d6423SLionel Sambuc 
364433d6423SLionel Sambuc   /* Check if the minor device number is ok. */
365433d6423SLionel Sambuc   if (minor < 0 || minor >= NR_DEVS || m_is_block(minor)) return ENXIO;
366433d6423SLionel Sambuc 
367433d6423SLionel Sambuc #if defined(__i386__)
368433d6423SLionel Sambuc   if (minor == MEM_DEV)
369433d6423SLionel Sambuc   {
370433d6423SLionel Sambuc 	int r = sys_enable_iop(user_endpt);
371433d6423SLionel Sambuc 	if (r != OK)
372433d6423SLionel Sambuc 	{
373433d6423SLionel Sambuc 		printf("m_char_open: sys_enable_iop failed for %d: %d\n",
374433d6423SLionel Sambuc 			user_endpt, r);
375433d6423SLionel Sambuc 		return r;
376433d6423SLionel Sambuc 	}
377433d6423SLionel Sambuc   }
378433d6423SLionel Sambuc #endif
379433d6423SLionel Sambuc 
380433d6423SLionel Sambuc   openct[minor]++;
381433d6423SLionel Sambuc 
382433d6423SLionel Sambuc   return(OK);
383433d6423SLionel Sambuc }
384433d6423SLionel Sambuc 
385433d6423SLionel Sambuc /*===========================================================================*
386433d6423SLionel Sambuc  *				m_char_close				     *
387433d6423SLionel Sambuc  *===========================================================================*/
m_char_close(devminor_t minor)388433d6423SLionel Sambuc static int m_char_close(devminor_t minor)
389433d6423SLionel Sambuc {
390433d6423SLionel Sambuc /* Close a memory character device. */
391433d6423SLionel Sambuc 
392433d6423SLionel Sambuc   if (minor < 0 || minor >= NR_DEVS || m_is_block(minor)) return ENXIO;
393433d6423SLionel Sambuc 
394433d6423SLionel Sambuc   if(openct[minor] < 1) {
395433d6423SLionel Sambuc 	printf("MEMORY: closing unopened device %d\n", minor);
396433d6423SLionel Sambuc 	return(EINVAL);
397433d6423SLionel Sambuc   }
398433d6423SLionel Sambuc   openct[minor]--;
399433d6423SLionel Sambuc 
400433d6423SLionel Sambuc   return(OK);
401433d6423SLionel Sambuc }
402433d6423SLionel Sambuc 
403433d6423SLionel Sambuc /*===========================================================================*
404433d6423SLionel Sambuc  *				m_block_part				     *
405433d6423SLionel Sambuc  *===========================================================================*/
m_block_part(devminor_t minor)406433d6423SLionel Sambuc static struct device *m_block_part(devminor_t minor)
407433d6423SLionel Sambuc {
408433d6423SLionel Sambuc /* Prepare for I/O on a device: check if the minor device number is ok. */
409433d6423SLionel Sambuc   if (minor < 0 || minor >= NR_DEVS || !m_is_block(minor)) return(NULL);
410433d6423SLionel Sambuc 
411433d6423SLionel Sambuc   return(&m_geom[minor]);
412433d6423SLionel Sambuc }
413433d6423SLionel Sambuc 
414433d6423SLionel Sambuc /*===========================================================================*
415433d6423SLionel Sambuc  *				m_block_transfer			     *
416433d6423SLionel Sambuc  *===========================================================================*/
m_block_transfer(devminor_t minor,int do_write,u64_t position,endpoint_t endpt,iovec_t * iov,unsigned int nr_req,int UNUSED (flags))417433d6423SLionel Sambuc static int m_block_transfer(
418433d6423SLionel Sambuc   devminor_t minor,		/* minor device number */
419433d6423SLionel Sambuc   int do_write,			/* read or write? */
420433d6423SLionel Sambuc   u64_t position,		/* offset on device to read or write */
421433d6423SLionel Sambuc   endpoint_t endpt,		/* process doing the request */
422433d6423SLionel Sambuc   iovec_t *iov,			/* pointer to read or write request vector */
423433d6423SLionel Sambuc   unsigned int nr_req,		/* length of request vector */
424433d6423SLionel Sambuc   int UNUSED(flags)		/* transfer flags */
425433d6423SLionel Sambuc )
426433d6423SLionel Sambuc {
427433d6423SLionel Sambuc /* Read or write one the driver's block devices. */
428433d6423SLionel Sambuc   unsigned count;
429433d6423SLionel Sambuc   vir_bytes vir_offset = 0;
430433d6423SLionel Sambuc   struct device *dv;
431433d6423SLionel Sambuc   u64_t dv_size;
432433d6423SLionel Sambuc   int r;
433433d6423SLionel Sambuc   vir_bytes dev_vaddr;
434433d6423SLionel Sambuc   cp_grant_id_t grant;
435433d6423SLionel Sambuc   ssize_t total = 0;
436433d6423SLionel Sambuc 
437433d6423SLionel Sambuc   /* Get minor device information. */
438433d6423SLionel Sambuc   if ((dv = m_block_part(minor)) == NULL) return(ENXIO);
439433d6423SLionel Sambuc   dv_size = dv->dv_size;
440433d6423SLionel Sambuc   dev_vaddr = m_vaddrs[minor];
441433d6423SLionel Sambuc 
442433d6423SLionel Sambuc   if (ex64hi(position) != 0)
443433d6423SLionel Sambuc 	return OK;	/* Beyond EOF */
444433d6423SLionel Sambuc 
445433d6423SLionel Sambuc   while (nr_req > 0) {
446433d6423SLionel Sambuc 
447433d6423SLionel Sambuc 	/* How much to transfer and where to / from. */
448433d6423SLionel Sambuc 	count = iov->iov_size;
449433d6423SLionel Sambuc 	grant = (cp_grant_id_t) iov->iov_addr;
450433d6423SLionel Sambuc 
451433d6423SLionel Sambuc 	/* Virtual copying. For RAM disks and internal FS. */
452433d6423SLionel Sambuc 	if(!dev_vaddr || dev_vaddr == (vir_bytes) MAP_FAILED) {
453433d6423SLionel Sambuc 		printf("MEM: dev %d not initialized\n", minor);
454433d6423SLionel Sambuc 		return EIO;
455433d6423SLionel Sambuc 	}
456433d6423SLionel Sambuc 	if (position >= dv_size) return(total);	/* check for EOF */
457433d6423SLionel Sambuc 	if (position + count > dv_size) count = dv_size - position;
458433d6423SLionel Sambuc 	if (!do_write) {	/* copy actual data */
459433d6423SLionel Sambuc 	        r=sys_safecopyto(endpt, grant, vir_offset,
460433d6423SLionel Sambuc 		  dev_vaddr + position, count);
461433d6423SLionel Sambuc 	} else {
462433d6423SLionel Sambuc 	        r=sys_safecopyfrom(endpt, grant, vir_offset,
463433d6423SLionel Sambuc 		  dev_vaddr + position, count);
464433d6423SLionel Sambuc 	}
465433d6423SLionel Sambuc 	if(r != OK) {
466433d6423SLionel Sambuc 		panic("I/O copy failed: %d", r);
467433d6423SLionel Sambuc 	}
468433d6423SLionel Sambuc 
469433d6423SLionel Sambuc 	/* Book the number of bytes transferred. */
470433d6423SLionel Sambuc 	position += count;
471433d6423SLionel Sambuc 	vir_offset += count;
472433d6423SLionel Sambuc 	total += count;
473433d6423SLionel Sambuc 	if ((iov->iov_size -= count) == 0) { iov++; nr_req--; vir_offset = 0; }
474433d6423SLionel Sambuc 
475433d6423SLionel Sambuc   }
476433d6423SLionel Sambuc   return(total);
477433d6423SLionel Sambuc }
478433d6423SLionel Sambuc 
479433d6423SLionel Sambuc /*===========================================================================*
480433d6423SLionel Sambuc  *				m_block_open				     *
481433d6423SLionel Sambuc  *===========================================================================*/
m_block_open(devminor_t minor,int UNUSED (access))482433d6423SLionel Sambuc static int m_block_open(devminor_t minor, int UNUSED(access))
483433d6423SLionel Sambuc {
484433d6423SLionel Sambuc /* Open a memory block device. */
485433d6423SLionel Sambuc   if (m_block_part(minor) == NULL) return(ENXIO);
486433d6423SLionel Sambuc 
487433d6423SLionel Sambuc   openct[minor]++;
488433d6423SLionel Sambuc 
489433d6423SLionel Sambuc   return(OK);
490433d6423SLionel Sambuc }
491433d6423SLionel Sambuc 
492433d6423SLionel Sambuc /*===========================================================================*
493433d6423SLionel Sambuc  *				m_block_close				     *
494433d6423SLionel Sambuc  *===========================================================================*/
m_block_close(devminor_t minor)495433d6423SLionel Sambuc static int m_block_close(devminor_t minor)
496433d6423SLionel Sambuc {
497433d6423SLionel Sambuc /* Close a memory block device. */
498433d6423SLionel Sambuc   if (m_block_part(minor) == NULL) return(ENXIO);
499433d6423SLionel Sambuc 
500433d6423SLionel Sambuc   if(openct[minor] < 1) {
501433d6423SLionel Sambuc 	printf("MEMORY: closing unopened device %d\n", minor);
502433d6423SLionel Sambuc 	return(EINVAL);
503433d6423SLionel Sambuc   }
504433d6423SLionel Sambuc   openct[minor]--;
505433d6423SLionel Sambuc 
506433d6423SLionel Sambuc   return(OK);
507433d6423SLionel Sambuc }
508433d6423SLionel Sambuc 
509433d6423SLionel Sambuc /*===========================================================================*
510433d6423SLionel Sambuc  *				m_block_ioctl				     *
511433d6423SLionel Sambuc  *===========================================================================*/
m_block_ioctl(devminor_t minor,unsigned long request,endpoint_t endpt,cp_grant_id_t grant,endpoint_t UNUSED (user_endpt))512433d6423SLionel Sambuc static int m_block_ioctl(devminor_t minor, unsigned long request,
513433d6423SLionel Sambuc 	endpoint_t endpt, cp_grant_id_t grant, endpoint_t UNUSED(user_endpt))
514433d6423SLionel Sambuc {
515433d6423SLionel Sambuc /* I/O controls for the block devices of the memory driver. Currently there is
516433d6423SLionel Sambuc  * one I/O control specific to the memory driver:
517433d6423SLionel Sambuc  * - MIOCRAMSIZE: to set the size of the RAM disk.
518433d6423SLionel Sambuc  */
519433d6423SLionel Sambuc   struct device *dv;
520433d6423SLionel Sambuc   u32_t ramdev_size;
521433d6423SLionel Sambuc   int s;
522433d6423SLionel Sambuc   void *mem;
523433d6423SLionel Sambuc   int is_imgrd = 0;
524433d6423SLionel Sambuc 
525433d6423SLionel Sambuc   if (request != MIOCRAMSIZE)
526433d6423SLionel Sambuc 	return EINVAL;
527433d6423SLionel Sambuc 
528433d6423SLionel Sambuc   if(minor == IMGRD_DEV)
529433d6423SLionel Sambuc 	is_imgrd = 1;
530433d6423SLionel Sambuc 
531433d6423SLionel Sambuc   /* Someone wants to create a new RAM disk with the given size.
532433d6423SLionel Sambuc    * A ramdisk can be created only once, and only on RAM disk device.
533433d6423SLionel Sambuc    */
534433d6423SLionel Sambuc   if ((dv = m_block_part(minor)) == NULL) return ENXIO;
535433d6423SLionel Sambuc   if((minor < RAM_DEV_FIRST || minor > RAM_DEV_LAST) &&
536433d6423SLionel Sambuc   	minor != RAM_DEV_OLD && !is_imgrd) {
537433d6423SLionel Sambuc 	printf("MEM: MIOCRAMSIZE: %d not a ramdisk\n", minor);
538433d6423SLionel Sambuc 	return EINVAL;
539433d6423SLionel Sambuc   }
540433d6423SLionel Sambuc 
541433d6423SLionel Sambuc   /* Get request structure */
542433d6423SLionel Sambuc   s= sys_safecopyfrom(endpt, grant, 0, (vir_bytes)&ramdev_size,
543433d6423SLionel Sambuc 	sizeof(ramdev_size));
544433d6423SLionel Sambuc   if (s != OK)
545433d6423SLionel Sambuc 	return s;
546433d6423SLionel Sambuc   if(is_imgrd)
547433d6423SLionel Sambuc   	ramdev_size = 0;
548433d6423SLionel Sambuc   if(m_vaddrs[minor] && dv->dv_size == (u64_t) ramdev_size) {
549433d6423SLionel Sambuc 	return(OK);
550433d6423SLionel Sambuc   }
551433d6423SLionel Sambuc   /* openct is 1 for the ioctl(). */
552433d6423SLionel Sambuc   if(openct[minor] != 1) {
553433d6423SLionel Sambuc 	printf("MEM: MIOCRAMSIZE: %d in use (count %d)\n",
554433d6423SLionel Sambuc 		minor, openct[minor]);
555433d6423SLionel Sambuc 	return(EBUSY);
556433d6423SLionel Sambuc   }
557433d6423SLionel Sambuc   if(m_vaddrs[minor]) {
558433d6423SLionel Sambuc 	u32_t a, o;
559433d6423SLionel Sambuc 	u64_t size;
560433d6423SLionel Sambuc 	int r;
561433d6423SLionel Sambuc 	if(ex64hi(dv->dv_size)) {
562433d6423SLionel Sambuc 		panic("huge old ramdisk");
563433d6423SLionel Sambuc 	}
564433d6423SLionel Sambuc 	size = dv->dv_size;
565433d6423SLionel Sambuc 	a = m_vaddrs[minor];
566433d6423SLionel Sambuc 	if((o = a % PAGE_SIZE)) {
567433d6423SLionel Sambuc 		vir_bytes l = PAGE_SIZE - o;
568433d6423SLionel Sambuc 		a += l;
569433d6423SLionel Sambuc 		size -= l;
570433d6423SLionel Sambuc 	}
571433d6423SLionel Sambuc 	size = rounddown(size, PAGE_SIZE);
572433d6423SLionel Sambuc 	r = munmap((void *) a, size);
573433d6423SLionel Sambuc 	if(r != OK) {
574433d6423SLionel Sambuc 		printf("memory: WARNING: munmap failed: %d\n", r);
575433d6423SLionel Sambuc 	}
576433d6423SLionel Sambuc 	m_vaddrs[minor] = (vir_bytes) NULL;
577433d6423SLionel Sambuc 	dv->dv_size = 0;
578433d6423SLionel Sambuc   }
579433d6423SLionel Sambuc 
580433d6423SLionel Sambuc #if DEBUG
581433d6423SLionel Sambuc   printf("MEM:%d: allocating ramdisk of size 0x%x\n", minor, ramdev_size);
582433d6423SLionel Sambuc #endif
583433d6423SLionel Sambuc 
584433d6423SLionel Sambuc   mem = NULL;
585433d6423SLionel Sambuc 
586433d6423SLionel Sambuc   /* Try to allocate a piece of memory for the RAM disk. */
587433d6423SLionel Sambuc   if(ramdev_size > 0 &&
588433d6423SLionel Sambuc   	(mem = mmap(NULL, ramdev_size, PROT_READ|PROT_WRITE,
589433d6423SLionel Sambuc 		MAP_PREALLOC|MAP_ANON, -1, 0)) == MAP_FAILED) {
590433d6423SLionel Sambuc 	printf("MEM: failed to get memory for ramdisk\n");
591433d6423SLionel Sambuc 	return(ENOMEM);
592433d6423SLionel Sambuc   }
593433d6423SLionel Sambuc 
594433d6423SLionel Sambuc   m_vaddrs[minor] = (vir_bytes) mem;
595433d6423SLionel Sambuc 
596433d6423SLionel Sambuc   dv->dv_size = ramdev_size;
597433d6423SLionel Sambuc 
598433d6423SLionel Sambuc   return(OK);
599433d6423SLionel Sambuc }
600