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("Destroying old segment\n");
102 if (ioctl(fd, SEGDEV_IOC_DESTROYSEG, 0)) {
103 XSEGLOG("Failed to destroy old segment");
109 XSEGLOG("creating segment of size %llu\n", size);
111 if (ioctl(fd, SEGDEV_IOC_CREATESEG, size)) {
112 XSEGLOG("Failed to create segment");
120 static long segdev_deallocate(const char *name)
123 fd = open(SEGDEV_DEVICE, O_RDWR);
125 XSEGLOG("Cannot open %s: %s\n", SEGDEV_DEVICE,
126 strerror_r(errno, errbuf, ERRSIZE));
130 if (ioctl(fd, SEGDEV_IOC_DESTROYSEG, 0)) {
131 XSEGLOG("Failed to destroy old segment");
139 static void *segdev_map(const char *name, uint64_t size, struct xseg *seg)
145 XSEGLOG("struct xseg * not NULL. Ignoring...\n");
151 xseg = mmap ( XSEG_BASE_AS_PTR,
153 PROT_READ | PROT_WRITE,
154 MAP_SHARED | MAP_FIXED /* | MAP_LOCKED */,
157 if (xseg == MAP_FAILED) {
158 XSEGLOG("Could not map segment: %s\n",
159 strerror_r(errno, errbuf, ERRSIZE));
167 static void segdev_unmap(void *ptr, uint64_t size)
169 struct xseg *xseg = ptr;
170 (void)munmap(xseg, xseg->segment_size);
174 static struct xseg_type xseg_segdev = {
175 /* xseg_operations */
177 .allocate = segdev_allocate,
178 .deallocate = segdev_deallocate,
180 .unmap = segdev_unmap
186 static int segdev_local_signal_init(struct xseg *xseg, xport portno)
191 static void segdev_local_signal_quit(struct xseg *xseg, xport portno)
196 static int segdev_remote_signal_init(void)
201 static void segdev_remote_signal_quit(void)
206 static int segdev_prepare_wait(struct xseg *xseg, uint32_t portno)
211 static int segdev_cancel_wait(struct xseg *xseg, uint32_t portno)
216 static int segdev_wait_signal(struct xseg *xseg, uint32_t timeout)
221 static int segdev_signal(struct xseg *xseg, uint32_t portno)
223 struct xseg_port *port = xseg_get_port(xseg, portno);
226 struct segdev_signal_desc *ssd = xseg_get_signal_desc(xseg, port);
234 return write(opendev(), &portno, sizeof(portno));
237 static void *segdev_malloc(uint64_t size)
242 static void *segdev_realloc(void *mem, uint64_t size)
247 static void segdev_mfree(void *mem) { }
249 static int segdev_init_signal_desc(struct xseg *xseg, void *sd)
254 static void segdev_quit_signal_desc(struct xseg *xseg, void *sd)
259 static void *segdev_alloc_data(struct xseg *xseg)
264 static void segdev_free_data(struct xseg *xseg, void *data)
269 static void *segdev_alloc_signal_desc(struct xseg *xseg, void *data)
274 static void segdev_free_signal_desc(struct xseg *xseg, void *data, void *sd)
279 static struct xseg_peer xseg_peer_segdev = {
280 /* xseg signal operations */
282 .init_signal_desc = segdev_init_signal_desc,
283 .quit_signal_desc = segdev_quit_signal_desc,
284 .alloc_data = segdev_alloc_data,
285 .free_data = segdev_free_data,
286 .alloc_signal_desc = segdev_alloc_signal_desc,
287 .free_signal_desc = segdev_free_signal_desc,
288 .local_signal_init = segdev_local_signal_init,
289 .local_signal_quit = segdev_local_signal_quit,
290 .remote_signal_init = segdev_remote_signal_init,
291 .remote_signal_quit = segdev_remote_signal_quit,
292 .prepare_wait = segdev_prepare_wait,
293 .cancel_wait = segdev_cancel_wait,
294 .wait_signal = segdev_wait_signal,
295 .signal = segdev_signal,
296 .malloc = segdev_malloc,
297 .realloc = segdev_realloc,
298 .mfree = segdev_mfree
304 void xseg_segdev_init(void)
306 xseg_register_type(&xseg_segdev);
307 xseg_register_peer(&xseg_peer_segdev);