2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
39 #include <sys/types.h>
41 #include <sys/ioctl.h>
43 #include <sys/syscall.h>
48 #include <xseg/xseg.h>
50 #include <sys/kernel/segdev.h>
51 #include <drivers/xseg_segdev.h>
54 static char errbuf[ERRSIZE];
56 #define SEGDEV_DEVICE "/dev/segdev"
59 static int opendev(void)
64 fdev = open(SEGDEV_DEVICE, O_RDWR);
66 XSEGLOG("Cannot open %s: %s\n", SEGDEV_DEVICE,
67 strerror_r(errno, errbuf, ERRSIZE));
73 static int closedev(void)
81 XSEGLOG("Cannot close %s: %s\n", SEGDEV_DEVICE,
82 strerror_r(errno, errbuf, ERRSIZE));
90 static long segdev_allocate(const char *name, uint64_t size)
99 oldsize = ioctl(fd, SEGDEV_IOC_SEGSIZE, 0);
101 XSEGLOG("Failed to create new segment. Destroy the old one first");
105 XSEGLOG("creating segment of size %llu\n", size);
107 if (ioctl(fd, SEGDEV_IOC_CREATESEG, size)) {
108 XSEGLOG("Failed to create segment");
116 static long segdev_deallocate(const char *name)
119 fd = open(SEGDEV_DEVICE, O_RDWR);
121 XSEGLOG("Cannot open %s: %s\n", SEGDEV_DEVICE,
122 strerror_r(errno, errbuf, ERRSIZE));
126 if (ioctl(fd, SEGDEV_IOC_DESTROYSEG, 0)) {
127 XSEGLOG("Failed to destroy old segment");
135 static void *segdev_map(const char *name, uint64_t size, struct xseg *seg)
141 //XSEGLOG("struct xseg * not NULL. Ignoring...\n");
148 xseg = mmap ( XSEG_BASE_AS_PTR,
150 PROT_READ | PROT_WRITE,
151 MAP_SHARED | MAP_FIXED /* | MAP_LOCKED */,
154 if (xseg == MAP_FAILED) {
155 XSEGLOG("Could not map segment: %s\n",
156 strerror_r(errno, errbuf, ERRSIZE));
164 static void segdev_unmap(void *ptr, uint64_t size)
166 struct xseg *xseg = ptr;
167 (void)munmap(xseg, xseg->segment_size);
172 static struct xseg_type xseg_segdev = {
173 /* xseg_operations */
175 .allocate = segdev_allocate,
176 .deallocate = segdev_deallocate,
178 .unmap = segdev_unmap
184 static int segdev_local_signal_init(struct xseg *xseg, xport portno)
189 static void segdev_local_signal_quit(struct xseg *xseg, xport portno)
194 static int segdev_remote_signal_init(void)
199 static void segdev_remote_signal_quit(void)
204 static int segdev_prepare_wait(struct xseg *xseg, uint32_t portno)
209 static int segdev_cancel_wait(struct xseg *xseg, uint32_t portno)
214 static int segdev_wait_signal(struct xseg *xseg, void *sd, uint32_t timeout)
219 static int segdev_signal(struct xseg *xseg, uint32_t portno)
221 struct xseg_port *port = xseg_get_port(xseg, portno);
224 struct segdev_signal_desc *ssd = xseg_get_signal_desc(xseg, port);
232 return write(opendev(), &portno, sizeof(portno));
235 static void *segdev_malloc(uint64_t size)
240 static void *segdev_realloc(void *mem, uint64_t size)
245 static void segdev_mfree(void *mem) { }
247 static int segdev_init_signal_desc(struct xseg *xseg, void *sd)
252 static void segdev_quit_signal_desc(struct xseg *xseg, void *sd)
257 static void *segdev_alloc_data(struct xseg *xseg)
262 static void segdev_free_data(struct xseg *xseg, void *data)
267 static void *segdev_alloc_signal_desc(struct xseg *xseg, void *data)
272 static void segdev_free_signal_desc(struct xseg *xseg, void *data, void *sd)
277 static struct xseg_peer xseg_peer_segdev = {
278 /* xseg signal operations */
280 .init_signal_desc = segdev_init_signal_desc,
281 .quit_signal_desc = segdev_quit_signal_desc,
282 .alloc_data = segdev_alloc_data,
283 .free_data = segdev_free_data,
284 .alloc_signal_desc = segdev_alloc_signal_desc,
285 .free_signal_desc = segdev_free_signal_desc,
286 .local_signal_init = segdev_local_signal_init,
287 .local_signal_quit = segdev_local_signal_quit,
288 .remote_signal_init = segdev_remote_signal_init,
289 .remote_signal_quit = segdev_remote_signal_quit,
290 .prepare_wait = segdev_prepare_wait,
291 .cancel_wait = segdev_cancel_wait,
292 .wait_signal = segdev_wait_signal,
293 .signal = segdev_signal,
294 .malloc = segdev_malloc,
295 .realloc = segdev_realloc,
296 .mfree = segdev_mfree
302 void xseg_segdev_init(void)
304 xseg_register_type(&xseg_segdev);
305 xseg_register_peer(&xseg_peer_segdev);