This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpoll.go
126 lines (115 loc) · 2.76 KB
/
poll.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
package tcmu
import (
"fmt"
"github.com/coreos/go-tcmu/scsi"
"github.com/prometheus/common/log"
"golang.org/x/sys/unix"
)
const (
tcmuSenseBufferSize = 96
)
func (d *Device) beginPoll() {
// Entry point for the goroutine.
go d.recvResponse()
buf := make([]byte, 4)
for {
var n int
var err error
n, err = unix.Read(d.uioFd, buf)
if n == -1 && err != nil {
fmt.Println(err)
break
}
for {
cmd, err := d.getNextCommand()
if err != nil {
log.Errorf("error getting next command: %s", err)
break
}
if cmd == nil {
break
}
d.cmdChan <- cmd
}
}
close(d.cmdChan)
}
func (d *Device) recvResponse() {
var n int
buf := make([]byte, 4)
for resp := range d.respChan {
err := d.completeCommand(resp)
if err != nil {
log.Errorf("error completing command: %s", err)
return
}
/* Tell the fd there's something new */
n, err = unix.Write(d.uioFd, buf)
if n == -1 && err != nil {
log.Errorln("poll write")
return
}
}
}
func (d *Device) completeCommand(resp SCSIResponse) error {
off := d.tailEntryOff()
for d.entHdrOp(off) != tcmuOpCmd {
d.mbSetTail((d.mbCmdTail() + uint32(d.entHdrGetLen(off))) % d.mbCmdrSize())
off = d.tailEntryOff()
}
if d.entCmdId(off) != resp.id {
d.setEntCmdId(off, resp.id)
}
d.setEntRespSCSIStatus(off, resp.status)
if resp.status != scsi.SamStatGood {
d.copyEntRespSenseData(off, resp.senseBuffer)
}
d.mbSetTail((d.mbCmdTail() + uint32(d.entHdrGetLen(off))) % d.mbCmdrSize())
return nil
}
func (d *Device) getNextCommand() (*SCSICmd, error) {
//d.debugPrintMb()
//fmt.Printf("nextEntryOff: %d\n", d.nextEntryOff())
//fmt.Printf("headEntryOff: %d\n", d.headEntryOff())
for d.nextEntryOff() != d.headEntryOff() {
off := d.nextEntryOff()
if d.entHdrOp(off) == tcmuOpPad {
d.cmdTail = (d.cmdTail + uint32(d.entHdrGetLen(off))) % d.mbCmdrSize()
} else if d.entHdrOp(off) == tcmuOpCmd {
//d.printEnt(off)
out := &SCSICmd{
id: d.entCmdId(off),
device: d,
}
out.cdb = d.entCdb(off)
vecs := int(d.entReqIovCnt(off))
out.vecs = make([][]byte, vecs)
for i := 0; i < vecs; i++ {
v := d.entIovecN(off, i)
out.vecs[i] = v
}
d.cmdTail = (d.cmdTail + uint32(d.entHdrGetLen(off))) % d.mbCmdrSize()
return out, nil
} else {
panic(fmt.Sprintf("unsupported command from tcmu? %d", d.entHdrOp(off)))
}
}
return nil, nil
}
func (d *Device) printEnt(off int) {
for i, x := range d.mmap[off : off+d.entHdrGetLen(off)] {
fmt.Printf("0x%02x ", x)
if i%16 == 15 {
fmt.Printf("\n")
}
}
}
func (d *Device) nextEntryOff() int {
return int(d.cmdTail + d.mbCmdrOffset())
}
func (d *Device) headEntryOff() int {
return int(d.mbCmdHead() + d.mbCmdrOffset())
}
func (d *Device) tailEntryOff() int {
return int(d.mbCmdTail() + d.mbCmdrOffset())
}