-
Notifications
You must be signed in to change notification settings - Fork 85
/
dnvme_reg.c
185 lines (150 loc) · 5.11 KB
/
dnvme_reg.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* NVM Express Compliance Suite
* Copyright (c) 2011, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/init.h>
#include "dnvme_reg.h"
#include "definitions.h"
#include "sysdnvme.h"
#ifdef QEMU
/* QEMU doesn't suppport writeq() or readq(), thus make our own */
inline u64 READQ(const volatile void __iomem *addr)
{
return (((u64)readl(addr + 4) << 32) | (u64)readl(addr));
}
inline void WRITEQ(u64 val, volatile void __iomem *addr)
{
writel(val, addr);
writel(val >> 32, addr + 4);
}
#else
inline void WRITEQ(u64 val, volatile void __iomem *addr)
{
writeq(val, addr);
}
inline u64 READQ(const volatile void __iomem *addr)
{
return readq(addr);
}
#endif
/*
* read_nvme_reg_generic - Function to read the controller registers located in
* the MLBAR/MUBAR (PCI BAR 0 and 1) that are mapped to memory area which
* supports in-order access.
*/
int read_nvme_reg_generic(u8 __iomem *bar0,
u8 *udata, u32 nbytes, u32 offset, enum nvme_acc_type acc_type)
{
u32 index = 0;
u32 u32data;
u64 u64data;
u16 u16data;
u8 u8data;
bar0 += offset;
/* loop until user requested nbytes are read. */
for (index = 0; index < nbytes; ) {
/* Read data from NVME space */
if (acc_type == BYTE_LEN) {
u8data = readb(bar0);
/* Copy data to user buffer. */
memcpy((u8 *)&udata[index], &u8data, sizeof(u8));
LOG_DBG("NVME Read byte at 0x%llX:0x%0X", (u64)bar0, u8data);
bar0 += 1;
index += 1;
} else if (acc_type == WORD_LEN) {
u16data = readw(bar0);
/* Copy data to user buffer. */
memcpy((u8 *)&udata[index], &u16data, sizeof(u16));
LOG_DBG("NVME Read WORD at 0x%llX:0x%X", (u64)bar0, u16data);
bar0 += 2;
index += 2;
} else if (acc_type == DWORD_LEN) {
u32data = readl(bar0);
/* Copy data to user buffer. */
memcpy((u8 *)&udata[index], &u32data, sizeof(u32));
LOG_DBG("NVME Read DWORD at 0x%llX:0x%X", (u64)bar0, u32data);
bar0 += 4;
index += 4;
} else if (acc_type == QUAD_LEN) {
u64data = READQ(bar0);
/* Copy data to user buffer. */
memcpy((u8 *)&udata[index], &u64data, sizeof(u64));
LOG_DBG("NVME Read QUAD 0x%llX:0x%llX",
(u64)bar0, u64data);
bar0 += 8;
index += 8;
} else {
LOG_ERR("Use only BYTE/WORD/DWORD/QUAD access type");
return -EINVAL;
}
}
return 0;
}
/*
* write_nvme_reg_generic - Function to write the controller registers
* located in the MLBAR/MUBAR (PCI BAR 0 and 1) that are mapped to
* memory area which supports in-order access.
*/
int write_nvme_reg_generic(u8 __iomem *bar0,
u8 *udata, u32 nbytes, u32 offset, enum nvme_acc_type acc_type)
{
u32 index = 0;
u32 u32data;
u64 u64data;
u16 u16data;
u8 u8data;
bar0 += offset;
/* loop until user requested nbytes are written. */
for (index = 0; index < nbytes;) {
/* Check the acc_type and do write as per the access type */
if (acc_type == BYTE_LEN) {
memcpy((u8 *)&u8data, &udata[index], sizeof(u8));
writeb(u8data, bar0);
LOG_DBG("NVME Writing BYTE at Addr:Val::0x%llX:0x%X",
(u64)bar0, u8data);
bar0 += 1;
index += 1;
} else if (acc_type == WORD_LEN) {
memcpy((u8 *)&u16data, &udata[index], sizeof(u16));
writew(u16data, bar0);
LOG_DBG("NVME Writing WORD at Addr:Val::0x%llX:0x%X",
(u64)bar0, u16data);
bar0 += 2;
index += 2;
} else if (acc_type == DWORD_LEN) {
memcpy((u8 *)&u32data, &udata[index], sizeof(u32));
writel(u32data, bar0);
LOG_DBG("NVME Writing DWORD at Addr:Val::0x%llX:0x%X",
(u64)bar0, u32data);
bar0 += 4;
index += 4;
} else if (acc_type == QUAD_LEN) {
memcpy((u8 *)&u64data, &udata[index], sizeof(u64));
WRITEQ(u64data, bar0);
LOG_DBG("NVME Writing QUAD at Addr:Val::0x%llX:0x%llX",
(u64)bar0, u64data);
bar0 += 8;
index += 8;
} else {
LOG_ERR("use only BYTE/WORD/DWORD/QUAD");
return -EINVAL;
}
}
return 0;
}