-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.go
312 lines (249 loc) · 7.16 KB
/
ops.go
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright 2020, 2021, 2022 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package switchtec
import (
"encoding/binary"
"fmt"
"os"
"path"
"strconv"
"strings"
"syscall"
"unsafe"
)
// ops defines the device operations unique to the particular transport type.
// - character device (i.e. /dev/switchtec0)
// - UART (i.e. /dev/ttyUSB0)
// - others (i.e. ethernet, i2c)
type ops interface {
close(dev *Device) error
getDeviceID(dev *Device) (uint32, error)
submitCommand(dev *Device, cmd Command, payload []byte) error
readResponse(dev *Device, response []byte) error
gasMap(dev *Device, readonly bool) error
gasUnmap(dev *Device) error
// GAS direct access
gasWrite(dev *Device, offset int, data []byte) error
gasRead(dev *Device, offset int, size int) ([]byte, error)
// GAS MRPC access
gasWriteMrpcPayload(dev *Device, data []byte) error
gasReadMrpcPayload(dev *Device, size int) ([]byte, error)
// GAS uint32 access shortcuts
gasRead32(dev *Device, offset int) (uint32, error)
gasWrite32(dev *Device, offset int, data uint32) error
}
type gasMap struct {
config *[]byte
resources *[]byte
size int64
}
type charOps struct {
gas *gasMap
}
func (ops *charOps) close(dev *Device) error {
if ops.gas != nil {
ops.gasUnmap(dev)
}
return nil
}
func (ops *charOps) getDeviceID(dev *Device) (uint32, error) {
linkPath := fmt.Sprintf("%s/%s/device/device", "/sys/class/switchtec", path.Base(dev.name))
ret, err := sysFsReadInt(linkPath)
return uint32(ret), err
}
func (ops *charOps) submitCommand(dev *Device, cmd Command, payload []byte) error {
cmd = Command(int32(cmd) | (dev.paxID << PAXIdShift))
sz := len(payload) + int(unsafe.Sizeof(cmd))
buf := make([]byte, sz)
binary.LittleEndian.PutUint32(buf[:], uint32(cmd))
copy(buf[unsafe.Sizeof(cmd):], payload)
_, err := dev.file.Write(buf)
return err
}
func (ops *charOps) readResponse(dev *Device, response []byte) error {
var ret uint32 = 0
sz := len(response) + int(unsafe.Sizeof(ret))
buf := make([]byte, sz)
if _, err := dev.file.Read(buf); err != nil {
return err
}
ret = binary.LittleEndian.Uint32(buf[0:4])
if ret != 0 {
return fmt.Errorf("Failed Command: Response Code %#x (%d)", ret, ret)
}
copy(response, buf[unsafe.Sizeof(ret):])
return nil
}
func (ops *charOps) gasMap(dev *Device, readonly bool) error {
size, err := dev.ResourceSize("device/resource0")
if err != nil {
return err
}
config, err := dev.MapResource("device/resource0_wc", 0, gasTopCfgOffset, readonly)
if err != nil {
return err
}
resources, err := dev.MapResource("device/resource0", gasTopCfgOffset, uint64(size-gasTopCfgOffset), readonly)
if err != nil {
return err
}
ops.gas = &gasMap{
config: config,
resources: resources,
size: size,
}
return nil
}
func (gas *gasMap) close() {
gas.config = nil
gas.resources = nil
gas.size = -1
}
func (ops *charOps) gasUnmap(dev *Device) error {
defer ops.gas.close()
if err := syscall.Munmap(*(ops.gas.config)); err != nil {
return err
}
if err := syscall.Munmap(*(ops.gas.resources)); err != nil {
return err
}
return nil
}
func (ops *charOps) gasWrite(dev *Device, offset int, data []byte) error {
if offset < gasTopCfgOffset {
if offset+len(data) > gasTopCfgOffset {
return fmt.Errorf("GAS Write cannot span individual resoureces")
}
copy((*ops.gas.config)[offset:], data)
} else {
copy((*ops.gas.resources)[offset-gasTopCfgOffset:], data)
}
return nil
}
func (ops *charOps) gasWriteMrpcPayload(dev *Device, data []byte) error {
return ops.gasWrite(dev, gasMrpcOffset, data)
}
func (ops *charOps) gasWrite32(dev *Device, offset int, data uint32) error {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, data)
return ops.gasWrite(dev, offset, b)
}
func (ops *charOps) gasRead(dev *Device, offset int, size int) ([]byte, error) {
b := make([]byte, size)
if offset < gasTopCfgOffset {
if offset+size > gasTopCfgOffset {
return nil, fmt.Errorf("GAS Read cannot span individual resources")
}
copy(b, (*ops.gas.config)[offset:])
} else {
copy(b, (*ops.gas.resources)[offset-gasTopCfgOffset:])
}
return b, nil
}
func (ops *charOps) gasReadMrpcPayload(dev *Device, size int) ([]byte, error) {
return ops.gasRead(dev, gasMrpcOffset, size)
}
func (ops *charOps) gasRead32(dev *Device, offset int) (uint32, error) {
ret, err := ops.gasRead(dev, offset, 4)
return binary.LittleEndian.Uint32(ret), err
}
func (dev *Device) ResourceSize(resource string) (int64, error) {
path, err := dev.SystemPath(resource)
if err != nil {
return 0, err
}
file, err := os.Open(path)
if err != nil {
return 0, err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return 0, err
}
return info.Size(), err
}
func (dev *Device) SystemPath(resource string) (string, error) {
var stat syscall.Stat_t
/*
Translation from C to GO of the huge_encode_dev/huge_decode_dev
routines. This is some hacky level shit here.
here: https://github.com/torvalds/linux/blob/master/include/linux/kdev_t.h#L9
*/
major := func(stat syscall.Stat_t) int {
return int((stat.Rdev & 0xfff00) >> 8)
}
minor := func(stat syscall.Stat_t) int {
return int((stat.Rdev & 0xff) | ((stat.Rdev >> 12) & 0xfff00))
}
if err := syscall.Fstat(int(dev.file.Fd()), &stat); err != nil {
return "", err
}
return fmt.Sprintf("/sys/dev/char/%d:%d/%s", major(stat), minor(stat), resource), nil
}
func (dev *Device) MapResource(path string, offset uint64, length uint64, readonly bool) (*[]byte, error) {
path, err := dev.SystemPath(path)
if err != nil {
return nil, err
}
openFlags := func(readonly bool) int {
if readonly {
return syscall.O_RDONLY
}
return syscall.O_WRONLY
}
file, err := os.OpenFile(path, openFlags(readonly), 0)
if err != nil {
fmt.Printf("Cannot open file %s\n", path)
return nil, err
}
defer file.Close()
mmapFlags := func(readonly bool) int {
if readonly {
return syscall.PROT_READ
}
return syscall.PROT_WRITE
}
mmap, err := syscall.Mmap(int(file.Fd()), int64(offset), int(length), mmapFlags(readonly), syscall.MAP_SHARED)
if err != nil {
fmt.Printf("Cannot mmap file %s\n", path)
return nil, err
}
return &mmap, nil
}
func sysFsReadString(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
buf := make([]byte, 1024)
n, err := f.Read(buf)
if err != nil {
return "", err
}
return strings.Trim(string(buf[:n]), "\n"), err
}
func sysFsReadInt(path string) (int64, error) {
str, err := sysFsReadString(path)
if err != nil {
return -1, err
}
return strconv.ParseInt(str, 0, 64)
}