/
scsi_lli.c
502 lines (433 loc) · 15.3 KB
/
scsi_lli.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Low level SCSI interface.
*
* Marco van Wieringen, March 2012
*/
#include "bareos.h"
/* Forward referenced functions */
#ifdef HAVE_LOWLEVEL_SCSI_INTERFACE
#include "scsi_lli.h"
#if defined(HAVE_LINUX_OS)
#include <scsi/sg.h>
#include <scsi/scsi.h>
/*
* Core interface function to lowlevel SCSI interface.
*/
static inline bool do_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len,
int direction)
{
int rc;
sg_io_hdr_t io_hdr;
SCSI_PAGE_SENSE sense;
bool opened_device = false;
bool retval = false;
/*
* See if we need to open the device_name or if we got an open filedescriptor.
*/
if (fd == -1) {
fd = open(device_name, O_RDWR | O_NONBLOCK | O_BINARY);
if (fd < 0) {
berrno be;
Emsg2(M_ERROR, 0, _("Failed to open %s: ERR=%s\n"),
device_name, be.bstrerror());
Dmsg2(010, "Failed to open %s: ERR=%s\n",
device_name, be.bstrerror());
return false;
}
opened_device = true;
}
memset(&sense, 0, sizeof(sense));
memset(&io_hdr, 0, sizeof(io_hdr));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = cdb_len;
io_hdr.mx_sb_len = sizeof(sense);
io_hdr.dxfer_direction = direction;
io_hdr.dxfer_len = cmd_page_len;
io_hdr.dxferp = (char *)cmd_page;
io_hdr.cmdp = (unsigned char *)cdb;
io_hdr.sbp = (unsigned char *)&sense;
rc = ioctl(fd, SG_IO, &io_hdr);
if (rc < 0) {
berrno be;
Emsg2(M_ERROR, 0, _("Unable to perform SG_IO ioctl on fd %d: ERR=%s\n"),
fd, be.bstrerror());
Dmsg2(010, "Unable to perform SG_IO ioctl on fd %d: ERR=%s\n",
fd, be.bstrerror());
goto bail_out;
}
if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
Emsg3(M_ERROR, 0, _("Failed with info 0x%02x mask status 0x%02x msg status 0x%02x\n"),
io_hdr.info, io_hdr.masked_status, io_hdr.msg_status);
Emsg2(M_ERROR, 0, _(" host status 0x%02x driver status 0x%02x\n"),
io_hdr.host_status, io_hdr.driver_status );
Dmsg3(010, "Failed with info 0x%02x mask status 0x%02x msg status 0x%02x\n",
io_hdr.info, io_hdr.masked_status, io_hdr.msg_status);
Dmsg2(010, " host status 0x%02x driver status 0x%02x\n",
io_hdr.host_status, io_hdr.driver_status );
goto bail_out;
}
retval = true;
bail_out:
/*
* See if we opened the device in this function, if so close it.
*/
if (opened_device) {
close(fd);
}
return retval;
}
/*
* Receive a lowlevel SCSI cmd from a SCSI device using the Linux SG_IO ioctl interface.
*/
bool recv_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len)
{
return do_scsi_cmd_page(fd, device_name, cdb, cdb_len, cmd_page, cmd_page_len, SG_DXFER_FROM_DEV);
}
/*
* Send a lowlevel SCSI cmd to a SCSI device using the Linux SG_IO ioctl interface.
*/
bool send_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len)
{
return do_scsi_cmd_page(fd, device_name, cdb, cdb_len, cmd_page, cmd_page_len, SG_DXFER_TO_DEV);
}
#elif defined(HAVE_SUN_OS)
#include <sys/scsi/impl/uscsi.h>
#ifndef LOBYTE
#define LOBYTE(_w) ((_w) & 0xff)
#endif
/*
* Core interface function to lowlevel SCSI interface.
*/
static inline bool do_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len,
int flags)
{
int rc;
struct uscsi_cmd my_cmd;
SCSI_PAGE_SENSE sense;
bool opened_device = false;
bool retval = false;
/*
* See if we need to open the device_name or if we got an open filedescriptor.
*/
if (fd == -1) {
fd = open(device_name, O_RDWR | O_NONBLOCK | O_BINARY);
if (fd < 0) {
berrno be;
Emsg2(M_ERROR, 0, _("Failed to open %s: ERR=%s\n"),
device_name, be.bstrerror());
Dmsg2(010, "Failed to open %s: ERR=%s\n",
device_name, be.bstrerror());
return false;
}
opened_device = true;
}
memset(&sense, 0, sizeof(sense));
my_cmd.uscsi_flags = flags;
my_cmd.uscsi_timeout = 15; /* Allow 15 seconds for completion */
my_cmd.uscsi_cdb = (char *)cdb;
my_cmd.uscsi_cdblen = cdb_len;
my_cmd.uscsi_bufaddr = (char *)cmd_page;
my_cmd.uscsi_buflen = cmd_page_len;
my_cmd.uscsi_rqlen = sizeof(sense);
my_cmd.uscsi_rqbuf = (char *)&sense;
rc = ioctl(fd, USCSICMD, &my_cmd);
if (rc != 0) {
berrno be;
Emsg2(M_ERROR, 0, _("Unable to perform USCSICMD ioctl on fd %d: ERR=%s\n"),
fd, be.bstrerror());
Emsg3(M_ERROR, 0, _("Sense Key: %0.2X ASC: %0.2X ASCQ: %0.2X\n"),
LOBYTE(sense.senseKey), sense.addSenseCode, sense.addSenseCodeQual);
Dmsg2(010, "Unable to perform USCSICMD ioctl on fd %d: ERR=%s\n",
fd, be.bstrerror());
Dmsg3(010, "Sense Key: %0.2X ASC: %0.2X ASCQ: %0.2X\n",
LOBYTE(sense.senseKey), sense.addSenseCode, sense.addSenseCodeQual);
goto bail_out;
}
retval = true;
bail_out:
/*
* See if we opened the device in this function, if so close it.
*/
if (opened_device) {
close(fd);
}
return retval;
}
/*
* Receive a lowlevel SCSI cmd from a SCSI device using the Solaris USCSI ioctl interface.
*/
bool recv_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len)
{
return do_scsi_cmd_page(fd, device_name, cdb, cdb_len, cmd_page, cmd_page_len,
(USCSI_READ | USCSI_SILENT | USCSI_RQENABLE));
}
/*
* Send a lowlevel SCSI cmd to a SCSI device using the Solaris USCSI ioctl interface.
*/
bool send_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len)
{
return do_scsi_cmd_page(fd, device_name, cdb, cdb_len, cmd_page, cmd_page_len,
(USCSI_WRITE | USCSI_SILENT | USCSI_RQENABLE));
}
#elif defined(HAVE_FREEBSD_OS)
#include <camlib.h>
#include <cam/scsi/scsi_message.h>
#ifndef SAM_STAT_CHECK_CONDITION
#define SAM_STAT_CHECK_CONDITION 0x2
#endif
#ifndef SAM_STAT_COMMAND_TERMINATED
#define SAM_STAT_COMMAND_TERMINATED 0x22
#endif
/*
* Core interface function to lowlevel SCSI interface.
*/
static inline bool do_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len,
int direction)
{
int unitnum, len;
union ccb *ccb;
char errbuf[128];
char cam_devicename[64];
struct cam_device *cam_dev;
SCSI_PAGE_SENSE sense;
bool retval = false;
/*
* See what CAM device to use.
*/
if (cam_get_device(device_name, cam_devicename, sizeof(cam_devicename), &unitnum) == -1) {
berrno be;
Emsg2(M_ERROR, 0, _("Failed to find CAM device for %s: ERR=%s\n"),
device_name, be.bstrerror());
Dmsg2(010, "Failed to find CAM device for %s: ERR=%s\n",
device_name, be.bstrerror());
return false;
}
cam_dev = cam_open_spec_device(cam_devicename, unitnum, O_RDWR, NULL);
if (!cam_dev) {
berrno be;
Emsg2(M_ERROR, 0, _("Failed to open CAM device for %s: ERR=%s\n"),
device_name, be.bstrerror());
Dmsg2(010, "Failed to open CAM device for %s: ERR=%s\n",
device_name, be.bstrerror());
return false;
}
ccb = cam_getccb(cam_dev);
if (!ccb) {
Emsg1(M_ERROR, 0, _("Failed to allocate new ccb for %s\n"),
device_name);
Dmsg1(0, "Failed to allocate new ccb for %s\n",
device_name);
goto bail_out;
}
/*
* Clear out structure, except for header that was filled for us.
*/
memset(&(&ccb->ccb_h)[1], 0, sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));
cam_fill_csio(&ccb->csio,
1, /* retries */
NULL, /* cbfcnp */
direction, /* flags */
MSG_SIMPLE_Q_TAG, /* tagaction */
(u_int8_t *)cmd_page, /* dataptr */
cmd_page_len, /* datalen */
sizeof(sense), /* senselength */
cdb_len, /* cdblength */
15000 /* timeout (millisecs) */);
memcpy(ccb->csio.cdb_io.cdb_bytes, cdb, cdb_len);
if (cam_send_ccb(cam_dev, ccb) < 0) {
Emsg2(M_ERROR, 0, _("Failed to send ccb to device %s: %s\n"),
device_name, cam_error_string(cam_dev, ccb, errbuf, sizeof(errbuf),
CAM_ESF_ALL, CAM_EPF_ALL));
Dmsg2(010, "Failed to send ccb to device %s: %s\n",
device_name, cam_error_string(cam_dev, ccb, errbuf, sizeof(errbuf),
CAM_ESF_ALL, CAM_EPF_ALL));
cam_freeccb(ccb);
goto bail_out;
}
/*
* Retrieve the SCSI sense data.
*/
if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) ||
((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR)) {
if ((SAM_STAT_CHECK_CONDITION == ccb->csio.scsi_status) ||
(SAM_STAT_COMMAND_TERMINATED == ccb->csio.scsi_status)) {
len = sizeof(sense) - ccb->csio.sense_resid;
if (len) {
memcpy(&sense, &(ccb->csio.sense_data), len);
}
}
}
retval = true;
bail_out:
/*
* Close the CAM device.
*/
cam_close_device(cam_dev);
return retval;
}
/*
* Receive a lowlevel SCSI cmd from a SCSI device using the FreeBSD SCSI CAM interface.
*/
bool recv_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len)
{
return do_scsi_cmd_page(fd, device_name, cdb, cdb_len, cmd_page, cmd_page_len, CAM_DIR_IN);
}
/*
* Send a lowlevel SCSI cmd to a SCSI device using the FreeBSD SCSI CAM interface.
*/
bool send_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len)
{
return do_scsi_cmd_page(fd, device_name, cdb, cdb_len, cmd_page, cmd_page_len, CAM_DIR_OUT);
}
#elif defined(HAVE_NETBSD_OS) || defined(HAVE_OPENBSD_OS)
#if defined(HAVE_NETBSD_OS)
#include <dev/scsipi/scsipi_all.h>
#else
#include <scsi/uscsi_all.h>
#endif
#ifndef LOBYTE
#define LOBYTE(_w) ((_w) & 0xff)
#endif
/*
* Core interface function to lowlevel SCSI interface.
*/
static inline bool do_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len,
int flags)
{
int rc;
scsireq_t req;
SCSI_PAGE_SENSE *sense;
bool opened_device = false;
bool retval = false;
/*
* See if we need to open the device_name or if we got an open filedescriptor.
*/
if (fd == -1) {
fd = open(device_name, O_RDWR | O_NONBLOCK| O_BINARY);
if (fd < 0) {
berrno be;
Emsg2(M_ERROR, 0, _("Failed to open %s: ERR=%s\n"),
device_name, be.bstrerror());
Dmsg2(010, "Failed to open %s: ERR=%s\n",
device_name, be.bstrerror());
return false;
}
opened_device = true;
}
memset(&req, 0, sizeof(req));
memcpy(req.cmd, cdb, cdb_len);
req.cmdlen = cdb_len;
req.databuf = cmd_page;
req.datalen = cmd_page_len;
req.timeout = 15000 /* timeout (millisecs) */;
req.flags = flags;
req.senselen = sizeof(SCSI_PAGE_SENSE);
rc = ioctl(fd, SCIOCCOMMAND, &req);
if (rc < 0) {
berrno be;
Emsg2(M_ERROR, 0, _("Unable to perform SCIOCCOMMAND ioctl on fd %d: ERR=%s\n"),
fd, be.bstrerror());
Dmsg2(010, "Unable to perform SCIOCCOMMAND ioctl on fd %d: ERR=%s\n",
fd, be.bstrerror());
goto bail_out;
}
switch (req.retsts) {
case SCCMD_OK:
retval = true;
break;
case SCCMD_SENSE:
sense = req.sense;
Emsg3(M_ERROR, 0, _("Sense Key: %0.2X ASC: %0.2X ASCQ: %0.2X\n"),
LOBYTE(sense.senseKey), sense.addSenseCode, sense.addSenseCodeQual);
Dmsg3(010, "Sense Key: %0.2X ASC: %0.2X ASCQ: %0.2X\n",
LOBYTE(sense.senseKey), sense.addSenseCode, sense.addSenseCodeQual);
break;
case SCCMD_TIMEOUT:
Emsg1(M_ERROR, 0, _("SCIOCCOMMAND ioctl on %s returned SCSI command timed out\n"),
devicename);
Dmsg1(010, "SCIOCCOMMAND ioctl on %s returned SCSI command timed out\n",
devicename);
break;
case SCCMD_BUSY:
Emsg1(M_ERROR, 0, _("SCIOCCOMMAND ioctl on %s returned device is busy\n"),
devicename);
Dmsg1(010, "SCIOCCOMMAND ioctl on %s returned device is busy\n",
devicename);
break;
case SCCMD_SENSE:
break;
default:
Emsg2(M_ERROR, 0, _("SCIOCCOMMAND ioctl on %s returned unknown status %d\n"),
device_name, req.retsts);
Dmsg2(010, "SCIOCCOMMAND ioctl on %s returned unknown status %d\n",
device_name, req.retsts);
break;
}
bail_out:
/*
* See if we opened the device in this function, if so close it.
*/
if (opened_device) {
close(fd);
}
return retval;
}
/*
* Receive a lowlevel SCSI cmd from a SCSI device using the NetBSD/OpenBSD
* SCIOCCOMMAND ioctl interface.
*/
bool recv_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len)
{
return do_scsi_cmd_page(fd, device_name, cdb, cdb_len, cmd_page, cmd_page_len, SCCMD_READ);
}
/*
* Send a lowlevel SCSI cmd to a SCSI device using the NetBSD/OpenBSD
* SCIOCCOMMAND ioctl interface.
*/
bool send_scsi_cmd_page(int fd, const char *device_name,
void *cdb, unsigned int cdb_len,
void *cmd_page, unsigned int cmd_page_len)
{
return do_scsi_cmd_page(fd, device_name, cdb, cdb_len, cmd_page, cmd_page_len, SCCMD_WRITE);
}
#endif
#endif /* HAVE_LOWLEVEL_SCSI_INTERFACE */