-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcachex_netbsd.h
108 lines (94 loc) · 2.93 KB
/
cachex_netbsd.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef CACHEX_NETBSD_H
#define CACHEX_NETBSD_H
#define _POSIX_C_SOURCE 200112L
#define _NETBSD_SOURCE
#include "result.h"
#include <array>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/scsiio.h>
namespace netbsd_detail
{
void req_io_exec(int fd, scsireq_t &io, CommandResult &rv)
{
struct timespec ts1, ts2, ts;
::clock_gettime(CLOCK_MONOTONIC, &ts1);
auto io_rv = ::ioctl(fd, SCIOCCOMMAND, &io);
::clock_gettime(CLOCK_MONOTONIC, &ts2);
timespecsub(&ts2, &ts1, &ts);
rv.Valid = (io_rv == 0);
rv.ScsiStatus = io.retsts;
rv.Duration = (double)((ts.tv_sec * 100000) + (ts.tv_nsec / 10000)) / 100;
}
template <std::size_t CDBLength>
scsireq_t req_io_common(const std::array<std::uint8_t, CDBLength> &cdb)
{
scsireq_t io;
::memset(&io, 0, sizeof(io));
io.timeout = 60000;
std::copy(std::begin(cdb), std::end(cdb), io.cmd);
io.cmdlen = CDBLength;
return io;
}
template <std::size_t CDBLength>
scsireq_t req_io_for_read(CommandResult &rv,
const std::array<std::uint8_t, CDBLength> &cdb)
{
auto io = req_io_common(cdb);
io.flags = SCCMD_READ;
io.datalen = static_cast<unsigned int>(rv.Data.size());
io.databuf = rv.Data.data();
return io;
}
template <std::size_t CDBLength>
scsireq_t req_io_for_write(const std::vector<std::uint8_t> &data,
const std::array<std::uint8_t, CDBLength> &cdb)
{
auto io = req_io_common(cdb);
io.flags = SCCMD_WRITE;
io.datalen = static_cast<unsigned int>(data.size());
io.databuf = const_cast<std::uint8_t *>(data.data());
return io;
}
} // namespace netbsd_detail
struct platform_netbsd
{
using device_handle = int;
static device_handle open_volume(const char *path)
{
return ::open(path, O_RDWR | O_NONBLOCK | O_EXCL);
}
static bool handle_is_valid(device_handle fd) { return fd != -1; }
static void close_handle(device_handle fd) { ::close(fd); }
static std::uint32_t monotonic_clock()
{
struct timespec ts;
::clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<std::uint32_t>((ts.tv_sec * 1000) +
(ts.tv_nsec / 1000000));
}
static void set_critical_priority() {}
static void set_normal_priority() {}
template <std::size_t CDBLength>
static void exec_command(device_handle fd, CommandResult &rv,
const std::array<std::uint8_t, CDBLength> &cdb)
{
auto io = netbsd_detail::req_io_for_read(rv, cdb);
netbsd_detail::req_io_exec(fd, io, rv);
}
template <std::size_t CDBLength>
static void send_data(device_handle fd, CommandResult &rv,
const std::array<std::uint8_t, CDBLength> &cdb,
const std::vector<std::uint8_t> &data)
{
auto io = netbsd_detail::req_io_for_write(data, cdb);
netbsd_detail::req_io_exec(fd, io, rv);
}
};
using platform = platform_netbsd;
#endif