-
Notifications
You must be signed in to change notification settings - Fork 0
/
sg.c
280 lines (260 loc) · 7.16 KB
/
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
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
/*
* Copyright (c) 2008, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU Lesser General Public License,
* version 2.1, 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser 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 <sys/ioctl.h>
#include "utils.h"
#include "api_lib.h"
#include "adapt_impl.h"
#include "fc_scsi.h"
/*
* Perform INQUIRY of SCSI-generic device.
*/
HBA_STATUS
sg_issue_inquiry(const char *file, HBA_UINT8 cdb_byte1,
HBA_UINT8 cdb_byte2, void *buf, HBA_UINT32 *lenp,
HBA_UINT8 *statp, void *sense, HBA_UINT32 *sense_lenp)
{
struct sg_io_hdr hdr;
struct scsi_inquiry cmd;
size_t len;
HBA_UINT32 slen;
int fd;
int rc;
len = *lenp;
slen = *sense_lenp;
if (slen > 255)
slen = 255; /* must fit in an 8-bit field */
if (len > 255)
len = 255; /* sometimes must fit in 8-byte field */
*lenp = 0;
*statp = 0;
fd = open(file, O_RDWR);
if (fd < 0) {
fprintf(stderr, "%s: open of %s failed, errno=0x%x\n",
__func__, file, errno);
return HBA_STATUS_ERROR;
}
memset(&hdr, 0, sizeof(hdr));
memset(&cmd, 0, sizeof(cmd));
memset(buf, 0, len);
cmd.in_op = SCSI_OP_INQUIRY;
cmd.in_flags = cdb_byte1;
cmd.in_page_code = cdb_byte2;
ua_net16_put(&cmd.in_alloc_len, len); /* field may actually be 8 bits */
hdr.interface_id = 'S';
hdr.dxfer_direction = SG_DXFER_FROM_DEV;
hdr.cmd_len = sizeof(cmd);
hdr.mx_sb_len = slen;
hdr.dxfer_len = len;
hdr.dxferp = (unsigned char *) buf;
hdr.cmdp = (unsigned char *) &cmd;
hdr.sbp = (unsigned char *) sense;
hdr.timeout = 3000; /* mS to wait for result */
rc = ioctl(fd, SG_IO, &hdr);
if (rc < 0) {
rc = errno;
fprintf(stderr, "%s: SG_IO error. file %s, errno=0x%x\n",
__func__, file, errno);
close(fd);
return HBA_STATUS_ERROR;
}
close(fd);
*lenp = len - hdr.resid;
*sense_lenp = hdr.sb_len_wr;
*statp = hdr.status;
return HBA_STATUS_OK;
}
static inline unsigned int
sg_get_id_type(struct scsi_inquiry_desc *dp)
{
return dp->id_type_flags & SCSI_INQT_TYPE_MASK;
}
/*
* Get device ID information for HBA-API.
* See the spec. We get the "best" information and leave the rest.
* The buffer is left empty if nothing is gotten.
*/
void
sg_get_dev_id(const char *name, char *buf, size_t result_len)
{
struct scsi_inquiry_dev_id *idp;
struct scsi_inquiry_desc *dp;
struct scsi_inquiry_desc *best = NULL;
char sense[252];
HBA_UINT32 len;
HBA_UINT32 slen;
u_char scsi_stat;
size_t rlen;
size_t dlen;
unsigned int type;
memset(buf, 0, result_len);
len = result_len;
slen = sizeof(sense);
idp = (struct scsi_inquiry_dev_id *) buf;
sg_issue_inquiry(name, SCSI_INQF_EVPD, SCSI_INQP_DEV_ID,
buf, &len, &scsi_stat, sense, &slen);
if (len < sizeof(*idp))
return;
if (idp->is_page_code != SCSI_INQP_DEV_ID)
return;
len -= sizeof(*idp);
rlen = net16_get(&idp->is_page_len);
if (rlen > len)
rlen = len;
dp = (struct scsi_inquiry_desc *) (idp + 1);
for (; rlen >= sizeof(*dp);
rlen -= dlen,
dp = (struct scsi_inquiry_desc *) ((char *) dp + dlen)) {
dlen = dp->id_designator_len + sizeof(*dp) -
sizeof(dp->id_designator[0]);
if (dlen > rlen)
break;
type = sg_get_id_type(dp);
if (type > SCSI_DTYPE_NAA)
continue;
if (best == NULL)
best = dp;
else if (type == sg_get_id_type(best) &&
(dp->id_designator_len < best->id_designator_len ||
(dp->id_designator_len == best->id_designator_len &&
memcmp(dp->id_designator, best->id_designator,
best->id_designator_len) < 0))) {
best = dp;
} else if (type > sg_get_id_type(best))
best = dp;
}
if (best) {
dp = best;
dlen = dp->id_designator_len + sizeof(*dp) -
sizeof(dp->id_designator[0]);
if (dlen > result_len)
dlen = 0; /* can't happen */
else
memmove(buf, dp, dlen); /* areas may overlap */
memset(buf + dlen, 0, result_len - dlen);
}
}
/*
* Read Capacity for HBA-API.
*/
HBA_STATUS
sg_issue_read_capacity(const char *file, void *resp, HBA_UINT32 *resp_lenp,
HBA_UINT8 *statp, void *sense, HBA_UINT32 *sense_lenp)
{
struct sg_io_hdr hdr;
struct scsi_rcap10 cmd;
struct scsi_rcap16 cmd_16;
size_t len;
int fd;
int rc;
len = *resp_lenp;
*resp_lenp = 0;
fd = open(file, O_RDWR);
if (fd < 0) {
fprintf(stderr, "%s: open of %s failed, errno=0x%x\n",
__func__, file, errno);
return errno;
}
memset(&hdr, 0, sizeof(hdr));
/* If the response buffer size is enough to
* accomodate READ CAPACITY(16) response issue
* SCSI READ CAPACITY(16) else issue
* SCSI READ CAPACITY(10)
*/
if (len >= sizeof(struct scsi_rcap16_resp)) {
memset(&cmd_16, 0, sizeof(cmd_16));
cmd_16.rc_op = SCSI_OP_SA_IN_16;
cmd_16.rc_sa = SCSI_SA_READ_CAP16;
ua_net32_put(&cmd_16.rc_alloc_len, len);
hdr.cmd_len = sizeof(cmd_16);
hdr.cmdp = (unsigned char *) &cmd_16;
} else {
memset(&cmd, 0, sizeof(cmd));
cmd.rc_op = SCSI_OP_READ_CAP10;
hdr.cmd_len = sizeof(cmd);
hdr.cmdp = (unsigned char *) &cmd;
}
hdr.interface_id = 'S';
hdr.dxfer_direction = SG_DXFER_FROM_DEV;
hdr.mx_sb_len = *sense_lenp;
hdr.dxfer_len = len;
hdr.dxferp = (unsigned char *) resp;
hdr.sbp = (unsigned char *) sense;
hdr.timeout = UINT_MAX;
hdr.timeout = 3000; /* mS to wait for result */
rc = ioctl(fd, SG_IO, &hdr);
if (rc < 0) {
rc = errno;
fprintf(stderr, "%s: SG_IO error. file %s, errno=0x%x\n",
__func__, file, errno);
close(fd);
return HBA_STATUS_ERROR;
}
close(fd);
*resp_lenp = len - hdr.resid;
*sense_lenp = hdr.sb_len_wr;
*statp = hdr.status;
return HBA_STATUS_OK;
}
/*
* Report LUNs for HBA-API.
*/
HBA_STATUS
sg_issue_report_luns(const char *file, void *resp, HBA_UINT32 *resp_lenp,
HBA_UINT8 *statp, void *sense, HBA_UINT32 *sense_lenp)
{
struct sg_io_hdr hdr;
struct scsi_report_luns cmd;
size_t len;
int fd;
int rc;
len = *resp_lenp;
*resp_lenp = 0;
fd = open(file, O_RDWR);
if (fd < 0) {
fprintf(stderr, "%s: open of %s failed, errno=0x%x\n",
__func__, file, errno);
return errno;
}
memset(&hdr, 0, sizeof(hdr));
memset(&cmd, 0, sizeof(cmd));
cmd.rl_op = SCSI_OP_REPORT_LUNS;
ua_net32_put(&cmd.rl_alloc_len, len);
hdr.interface_id = 'S';
hdr.dxfer_direction = SG_DXFER_FROM_DEV;
hdr.cmd_len = sizeof(cmd);
hdr.mx_sb_len = *sense_lenp;
hdr.dxfer_len = len;
hdr.dxferp = (unsigned char *) resp;
hdr.cmdp = (unsigned char *) &cmd;
hdr.sbp = (unsigned char *) sense;
hdr.timeout = UINT_MAX;
hdr.timeout = 3000; /* mS to wait for result */
rc = ioctl(fd, SG_IO, &hdr);
if (rc < 0) {
rc = errno;
fprintf(stderr, "%s: SG_IO error. file %s, errno=0x%x\n",
__func__, file, errno);
close(fd);
return HBA_STATUS_ERROR;
}
close(fd);
*resp_lenp = len - hdr.resid;
*sense_lenp = hdr.sb_len_wr;
*statp = hdr.status;
return HBA_STATUS_OK;
}