-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlib_sg.c
164 lines (131 loc) · 3.46 KB
/
vlib_sg.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
// SPDX-License-Identifier: EPL-1.0
/*
* Copyright IBM Corp. 2010, 2024.
*
* Authors: Sven Schuetz <[email protected]>
*
* File: vlib_sg.c
*
* Description:
* All calls that use sg_utils library.
*
*/
/**
* @file vlib_sg.c
* @brief All calls that use sg_utils library.
*/
#include <scsi/sg_cmds_basic.h>
#include "vlib.h"
#define INTERVAL 10000000
#define RETRIES 1500
HBA_STATUS sgutils_SendScsiInquiry(char* sg_dev, HBA_UINT8 EVPD,
HBA_UINT32 PageCode, void *pRspBuffer,
HBA_UINT32 *RspBufferSize)
{
char dev_path[32];
int sg_fd, res;
if(sg_dev == NULL)
return HBA_STATUS_ERROR;
strcpy(dev_path, "/dev/");
strcat(dev_path, sg_dev);
sg_fd = sg_cmds_open_device(dev_path, 0, 0);
if (sg_fd < 0)
return HBA_STATUS_ERROR;
memset(pRspBuffer, 0x0, *RspBufferSize);
res = sg_ll_inquiry(sg_fd, 0, EVPD, PageCode, pRspBuffer,
*RspBufferSize, 0, 0);
if(res < 0) {
sg_cmds_close_device(sg_fd);
return HBA_STATUS_ERROR;
}
res = sg_cmds_close_device(sg_fd);
if(res < 0)
return HBA_STATUS_ERROR;
return HBA_STATUS_OK;
}
HBA_STATUS sgutils_SendReportLUNs(char* sg_dev, char *pRspBuffer,
HBA_UINT32 *RspBufferSize)
{
int sg_fd, res, count = 0;
char dev_path[32];
struct timespec t;
int size;
HBA_STATUS status;
status = HBA_STATUS_OK;
strcpy(dev_path, "/dev/");
if(sg_dev == NULL)
return HBA_STATUS_ERROR;
strcat(dev_path, sg_dev);
t.tv_nsec = INTERVAL;
t.tv_sec = 0;
sg_fd = sg_cmds_open_device(dev_path, 0, 0);
while (sg_fd < 0 && count < RETRIES) {
nanosleep(&t, NULL);
sg_fd = sg_cmds_open_device(dev_path, 0, 0);
count++;
}
if (sg_fd < 0)
return HBA_STATUS_ERROR;
memset(pRspBuffer, 0x0, *RspBufferSize);
res = sg_ll_report_luns(sg_fd, 0, pRspBuffer, *RspBufferSize, 0, 0);
if(res < 0) {
sg_cmds_close_device(sg_fd);
return HBA_STATUS_ERROR;
}
size = ((pRspBuffer[0] << 24) | (pRspBuffer[1] << 16) |
(pRspBuffer[2] << 8) | pRspBuffer[3]);
/* size is size of payload, total response size is 8 bytes larger
* because of the header */
if (*RspBufferSize < (size + 8))
status = HBA_STATUS_ERROR_MORE_DATA;
else
*RspBufferSize = size + 8;
res = sg_cmds_close_device(sg_fd);
if(res < 0)
return HBA_STATUS_ERROR;
return status;
}
HBA_STATUS sgutils_SendReadCap(char* sg_dev, char *pRspBuffer,
HBA_UINT32 *RspBufferSize)
{
char dev_path[32];
int sg_fd, res, blocks;
HBA_STATUS status;
status = HBA_STATUS_OK;
if(sg_dev == NULL)
return HBA_STATUS_ERROR;
strcpy(dev_path, "/dev/");
strcat(dev_path, sg_dev);
sg_fd = sg_cmds_open_device(dev_path, 0, 0);
if (sg_fd < 0)
return HBA_STATUS_ERROR;
memset(pRspBuffer, 0x0, *RspBufferSize);
res = sg_ll_readcap_10(sg_fd, 0, 0, pRspBuffer, READCAP10LEN, 0, 0);
if(res < 0) {
sg_cmds_close_device(sg_fd);
return HBA_STATUS_ERROR;
}
blocks = ((pRspBuffer[0] << 24) | (pRspBuffer[1] << 16) |
(pRspBuffer[2] << 8) | pRspBuffer[3]);
*RspBufferSize = READCAP10LEN;
if (blocks != 0xffffffff)
/* device smaller than 0xffffffff blocks, reply ok */
return HBA_STATUS_OK;
/* device larger than 0xffffffff, readcap16 necessary */
if (*RspBufferSize < READCAP16LEN) {
/* buffer too small to hold the smallest response */
status = HBA_STATUS_ERROR_MORE_DATA;
goto out;
}
*RspBufferSize = READCAP16LEN;
res = sg_ll_readcap_16(sg_fd, 0, 0, pRspBuffer, READCAP10LEN, 0, 0);
if(res < 0) {
sg_cmds_close_device(sg_fd);
return HBA_STATUS_ERROR;
}
out:
res = sg_cmds_close_device(sg_fd);
if(res < 0)
return HBA_STATUS_ERROR;
return status;
}