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
|
/*
* Copyright 2004 Jeff Garzik
*
* This software may be used and distributed according to the terms
* of the GNU General Public License, incorporated herein by reference.
*
*/
#include "blktool-config.h"
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <glib.h>
#include "blktool.h"
enum {
max_sense = 128,
max_cdb = 32,
max_buf = 8192,
};
static u8 buf[max_buf];
static u8 sensebuf[max_sense];
static u8 cdb[max_cdb];
static struct sg_io_hdr sg_io_hdr;
static inline u32 scsi_u32(u8 *buf)
{
return ((u32)buf[0] << 24) |
((u32)buf[1] << 16) |
((u32)buf[2] << 8) |
((u32)buf[3]);
}
static void init_sg_hdr(void)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
memset(hdr, 0, sizeof(struct sg_io_hdr));
hdr->interface_id = 'S';
hdr->dxfer_direction = SG_DXFER_NONE;
hdr->cmd_len = 12;
hdr->mx_sb_len = max_sense;
hdr->dxferp = buf;
hdr->cmdp = cdb;
hdr->sbp = sensebuf;
hdr->timeout = 5000; /* millisec */
hdr->flags = SG_FLAG_NO_DXFER;
memset(cdb, 0, sizeof(cdb));
memset(sensebuf, 0, sizeof(sensebuf));
}
static void init_inquiry(void)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
init_sg_hdr();
hdr->dxfer_direction = SG_DXFER_FROM_DEV;
hdr->dxfer_len = 128;
hdr->flags &= ~SG_FLAG_NO_DXFER;
cdb[0] = INQUIRY;
cdb[4] = 128;
}
static void init_read_capacity(void)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
init_sg_hdr();
hdr->dxfer_direction = SG_DXFER_FROM_DEV;
hdr->dxfer_len = 8;
hdr->flags &= ~SG_FLAG_NO_DXFER;
cdb[0] = READ_CAPACITY;
}
static void init_media_lock(void)
{
init_sg_hdr();
cdb[0] = ALLOW_MEDIUM_REMOVAL;
}
static void init_mode_sense(void)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
init_sg_hdr();
hdr->dxfer_direction = SG_DXFER_FROM_DEV;
hdr->dxfer_len = 128;
hdr->flags &= ~SG_FLAG_NO_DXFER;
cdb[0] = MODE_SENSE_10;
cdb[1] = (1 << 3); /* do not return any block descriptors */
cdb[8] = 128; /* return max 128 bytes */
}
static void init_mode_select(void)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
init_sg_hdr();
hdr->dxfer_direction = SG_DXFER_TO_DEV;
hdr->flags &= ~SG_FLAG_NO_DXFER;
cdb[0] = MODE_SELECT_10;
}
static void handle_scsi_standby(void)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
memset(buf, 0, 128);
buf[0] = 0x1a; /* power condition mode page */
buf[1] = 0x0a; /* page length - 2 */
buf[3] = 0x01; /* bit 0: standby */
/* because bytes 8-11 (standby condition timer) are zero,
* the device should transition immediately to standby
*/
init_mode_select();
hdr->dxfer_len = 12;
cdb[8] = 12;
IOCTL(SG_IO, hdr);
}
static void handle_scsi_wcache(int do_32)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
int wce;
init_mode_sense();
cdb[2] = 0x8; /* caching mode page */
IOCTL(SG_IO, hdr);
wce = buf[2] & (1 << 2) ? 1 : 0; /* bit 2: write cache enable */
if (do_32 == wce)
return; /* nothing to do */
if (do_32)
buf[2] |= (1 << 2);
else
buf[2] &= ~(1 << 2);
init_mode_select();
hdr->dxfer_len = 20; /* len of caching mode page */
cdb[8] = 20;
IOCTL(SG_IO, hdr);
}
static void handle_scsi_read_ahead(int do_32)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
int ra;
init_mode_sense();
cdb[2] = 0x8; /* caching mode page */
IOCTL(SG_IO, hdr);
ra = buf[12] & (1 << 5) ? 0 : 1; /* bit 5: disable read-ahead */
if (do_32 == ra)
return; /* nothing to do */
if (do_32)
buf[12] &= ~(1 << 5);
else
buf[12] |= (1 << 5);
init_mode_select();
hdr->dxfer_len = 20; /* len of caching mode page */
cdb[8] = 20;
IOCTL(SG_IO, hdr);
}
static void handle_scsi_media(int do_32)
{
struct sg_io_hdr *hdr = &sg_io_hdr;
init_media_lock();
if (do_32)
cdb[4] = 1;
IOCTL(SG_IO, hdr);
}
void handle_bus_id(int argc, char **argv, struct command *cmd)
{
char bus_id[64];
memset(&bus_id, 0, sizeof(bus_id));
IOCTL(SCSI_IOCTL_GET_PCI, &bus_id[0]);
printf("%s\n", bus_id);
}
static void lists_init(void)
{
flag_arr = g_ptr_array_new();
}
static void lists_free(void)
{
#ifdef FREE_ON_EXIT
g_ptr_array_free(flag_arr, FALSE);
#endif
}
static void lists_dump(void)
{
list_dump("flags", flag_arr);
}
static const char *periph_type[] = {
"direct-access",
"sequential-access",
"printer",
"processor",
"write-once",
"cd-rom",
"scanner",
"optimal-memory",
"medium-changer",
"comm",
"prepress1",
"prepress2",
"storage-array",
"enclosure",
"simple-direct",
"optical-reader",
};
static const char *inq_flags3[8] = {
[5] = "norm-aca",
[4] = "hi-sup",
};
static const char *inq_flags5[8] = {
[7] = "sccs",
[6] = "acc",
[3] = "3pc",
};
static const char *inq_flags6[8] = {
[7] = "bque",
[6] = "enc-serv",
[5] = "vs1",
[4] = "multi-p",
[3] = "media-changer",
[2] = NULL,
[1] = NULL,
[0] = "addr16",
};
static const char *inq_flags7[8] = {
[7] = "reladdr",
[6] = NULL,
[5] = "wbus16",
[4] = "sync",
[3] = "linked",
[2] = NULL,
[1] = "cmdque",
[0] = "vs2",
};
#if 0
static const char *inq_flags5[8] = {
[7] = "",
[6] = "",
[5] = "",
[4] = "",
[3] = "",
[2] = "",
[1] = "",
[0] = "",
};
#endif
static void flag_push8(u8 mask, const char **strlist)
{
int i;
for (i = 7; i >= 0; i--)
if ((strlist[i] != NULL) && (mask & (1 << i)))
flag_push(strlist[i]);
}
static void handle_scsi_id(void)
{
char s[128];
struct sg_io_hdr *hdr = &sg_io_hdr;
u8 dev_type;
const char *sp;
lists_init();
init_inquiry();
IOCTL(SG_IO, hdr);
printf("scsi-version: %u\n", buf[2]);
memset(s, 0, sizeof(s));
memcpy(s, &buf[8], 15 - 8 + 1);
printf("vendor-id: %s\n", s);
memset(s, 0, sizeof(s));
memcpy(s, &buf[16], 31 - 16 + 1);
printf("product-id: %s\n", s);
memset(s, 0, sizeof(s));
memcpy(s, &buf[32], 35 - 32 + 1);
printf("product-rev: %s\n", s);
dev_type = buf[0] & 0x1f;
if (dev_type < ARRAY_SIZE(periph_type))
sp = periph_type[dev_type];
else
sp = "unknown";
printf("device-type: %s\n", sp);
flag_push8(buf[0x3], inq_flags3);
flag_push8(buf[0x5], inq_flags5);
flag_push8(buf[0x6], inq_flags6);
flag_push8(buf[0x7], inq_flags7);
lists_dump();
lists_free();
if ((dev_type == 0x00) || (dev_type == 0x0E)) {
init_read_capacity();
IOCTL(SG_IO, hdr);
printf("sectors: %u\n", scsi_u32(&buf[0]) + 1);
printf("sector-size: %u\n", scsi_u32(&buf[4]));
}
}
struct class_operations scsi_operations = {
.id = handle_scsi_id,
.media = handle_scsi_media,
.read_ahead = handle_scsi_read_ahead,
.standby = handle_scsi_standby,
.wcache = handle_scsi_wcache,
};
void scsi_init(void)
{
ops = &scsi_operations;
}
|