forked from hpe-storage/common-host-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioctl_scsi_get_address.go
69 lines (55 loc) · 2.1 KB
/
ioctl_scsi_get_address.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
// (c) Copyright 2019 Hewlett Packard Enterprise Development LP
// +build windows
// Package ioctl provides Windows IOCTL support
package ioctl
import (
"strings"
"syscall"
"unsafe"
log "github.com/hpe-storage/common-host-libs/logger"
)
type SCSI_ADDRESS struct {
Length uint32
PortNumber uint8
PathId uint8
TargetId uint8
Lun uint8
}
// GetScsiAddress issues an IOCTL_SCSI_GET_ADDRESS to the given device and returns back the
// device's SCSI_ADDRESS struct.
func GetScsiAddress(devicePathID string) (scsiAddress *SCSI_ADDRESS, err error) {
log.Tracef(">>>>> GetScsiAddress, devicePathID=%v", devicePathID)
defer log.Trace("<<<<< GetScsiAddress")
// Convert device path to a UTF16 string (strip any trailing backslash)
devicePathID = strings.TrimRight(devicePathID, `\`)
devicePathIDUTF16 := syscall.StringToUTF16(devicePathID)
// Get a handle to the device object
var handle syscall.Handle
handle, err = syscall.CreateFile(&devicePathIDUTF16[0], syscall.GENERIC_READ, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE, nil, syscall.OPEN_EXISTING, syscall.FILE_ATTRIBUTE_NORMAL, 0)
if handle == syscall.Handle(INVALID_HANDLE_VALUE) {
// Return rile not found if INVALID_HANDLE_VALUE returned
if err == nil {
err = syscall.ERROR_FILE_NOT_FOUND
}
} else {
// Close the volume handle when we're done
defer syscall.CloseHandle(handle)
// We only need an 8 byte buffer to populate the SCSI_ADDRESS struct
dataBuffer := make([]uint8, 8)
// Issue the IOCTL
var bytesReturned uint32
err = syscall.DeviceIoControl(handle, IOCTL_SCSI_GET_ADDRESS, nil, 0, &dataBuffer[0], uint32(len(dataBuffer)), &bytesReturned, nil)
// If IOCTL was successful, extract the SCSI_ADDRESS
if err == nil {
scsiAddress = (*SCSI_ADDRESS)(unsafe.Pointer(&dataBuffer[0]))
}
}
// Log the results
if err == nil {
log.Tracef("SCSI_ADDRESS Lengh=%v, ID=%02X:%02X:%02X:%02X", scsiAddress.Length, scsiAddress.PortNumber, scsiAddress.PathId, scsiAddress.TargetId, scsiAddress.Lun)
} else {
// Log error on failure
log.Errorf("Error=%v", err)
}
return scsiAddress, err
}