4 * Home page of code is: http://www.smartmontools.org
6 * Copyright (C) 2006-15 Douglas Gilbert <dgilbert@interlog.com>
7 * Copyright (C) 2009-16 Christian Franke
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * You should have received a copy of the GNU General Public License
15 * (for example COPYING); if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * The code in this file is based on the SCSI to ATA Translation (SAT)
19 * draft found at http://www.t10.org . The original draft used for this
20 * code is sat-r08.pdf which is not too far away from becoming a
21 * standard. The SAT commands of interest to smartmontools are the
22 * ATA PASS THROUGH SCSI (16) and ATA PASS THROUGH SCSI (12) defined in
23 * section 12 of that document.
25 * sat-r09.pdf is the most recent, easily accessible draft prior to the
26 * original SAT standard (ANSI INCITS 431-2007). By mid-2009 the second
27 * version of the SAT standard (SAT-2) is nearing standardization. In
28 * their wisdom an incompatible change has been introduced in draft
29 * sat2r08a.pdf in the area of the ATA RETURN DESCRIPTOR. A new "fixed
30 * format" ATA RETURN buffer has been defined (sat2r08b.pdf section
31 * 12.2.7) for the case when DSENSE=0 in the Control mode page.
32 * Unfortunately this is the normal case. If the change stands our
33 * code will need to be extended for this case.
35 * With more transports "hiding" SATA disks (and other S-ATAPI devices)
36 * behind a SCSI command set, accessing special features like SMART
37 * information becomes a challenge. The SAT standard offers ATA PASS
38 * THROUGH commands for special usages. Note that the SAT layer may
39 * be inside a generic OS layer (e.g. libata in linux), in a host
40 * adapter (HA or HBA) firmware, or somewhere on the interconnect
41 * between the host computer and the SATA devices (e.g. a RAID made
42 * of SATA disks and the RAID talks "SCSI" to the host computer).
43 * Note that in the latter case, this code does not solve the
44 * addressing issue (i.e. which SATA disk to address behind the logical
45 * SCSI (RAID) interface).
58 #include "atacmds.h" // ataReadHDIdentity()
59 #include "knowndrives.h" // lookup_usb_device()
61 #include "dev_interface.h"
62 #include "dev_ata_cmd_set.h" // ata_device_with_command_set
63 #include "dev_tunnelled.h" // tunnelled_device<>
66 const char * scsiata_cpp_cvsid
= "$Id: scsiata.cpp 4276 2016-04-02 19:13:39Z chrfranke $";
68 const char * scsiata_cpp_cvsid
= "$Id: scsiata.cpp 4041 2015-03-14 00:50:20Z dpgilbert $";
69 >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74
71 /* This is a slightly stretched SCSI sense "descriptor" format header.
72 The addition is to allow the 0x70 and 0x71 response codes. The idea
73 is to place the salient data of both "fixed" and "descriptor" sense
74 format into one structure to ease application processing.
75 The original sense buffer should be kept around for those cases
76 in which more information is required (e.g. the LBA of a MEDIUM ERROR). */
77 /// Abridged SCSI sense data
78 struct sg_scsi_sense_hdr
{
79 unsigned char response_code
; /* permit: 0x0, 0x70, 0x71, 0x72, 0x73 */
80 unsigned char sense_key
;
86 unsigned char additional_length
;
89 /* Maps the salient data from a sense buffer which is in either fixed or
90 descriptor format into a structure mimicking a descriptor format
91 header (i.e. the first 8 bytes of sense descriptor format).
92 If zero response code returns 0. Otherwise returns 1 and if 'sshp' is
93 non-NULL then zero all fields and then set the appropriate fields in
94 that structure. sshp::additional_length is always 0 for response
95 codes 0x70 and 0x71 (fixed format). */
96 static int sg_scsi_normalize_sense(const unsigned char * sensep
, int sb_len
,
97 struct sg_scsi_sense_hdr
* sshp
);
99 #define SAT_ATA_PASSTHROUGH_12LEN 12
100 #define SAT_ATA_PASSTHROUGH_16LEN 16
102 #define DEF_SAT_ATA_PASSTHRU_SIZE 16
103 #define ATA_RETURN_DESCRIPTOR 9
106 namespace sat
{ // no need to publish anything, name provided for Doxygen
109 /// Implements ATA by tunnelling through SCSI.
112 : public tunnelled_device
<
113 /*implements*/ ata_device
114 /*by tunnelling through a*/, scsi_device
116 virtual public /*implements*/ scsi_device
119 sat_device(smart_interface
* intf
, scsi_device
* scsidev
,
120 const char * req_type
, int passthrulen
= 0, bool enable_auto
= false);
122 virtual ~sat_device() throw();
124 virtual smart_device
* autodetect_open();
126 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
);
128 virtual bool scsi_pass_through(scsi_cmnd_io
* iop
);
136 sat_device::sat_device(smart_interface
* intf
, scsi_device
* scsidev
,
137 const char * req_type
, int passthrulen
/* = 0 */, bool enable_auto
/* = false */)
138 : smart_device(intf
, scsidev
->get_dev_name(),
139 (enable_auto
? "sat,auto" : "sat"), req_type
),
140 tunnelled_device
<ata_device
, scsi_device
>(scsidev
),
141 m_passthrulen(passthrulen
),
142 m_enable_auto(enable_auto
)
145 hide_ata(); // Start as SCSI, switch to ATA in autodetect_open()
147 hide_scsi(); // ATA always
148 if (strcmp(scsidev
->get_dev_type(), "scsi"))
149 set_info().dev_type
+= strprintf("+%s", scsidev
->get_dev_type());
151 set_info().info_name
= strprintf("%s [%sSAT]", scsidev
->get_info_name(),
152 (enable_auto
? "SCSI/" : ""));
155 sat_device::~sat_device() throw()
160 // cdb[0]: ATA PASS THROUGH (16) SCSI command opcode byte (0x85)
161 // cdb[1]: multiple_count, protocol + extend
162 // cdb[2]: offline, ck_cond, t_dir, byte_block + t_length
163 // cdb[3]: features (15:8)
164 // cdb[4]: features (7:0)
165 // cdb[5]: sector_count (15:8)
166 // cdb[6]: sector_count (7:0)
167 // cdb[7]: lba_low (15:8)
168 // cdb[8]: lba_low (7:0)
169 // cdb[9]: lba_mid (15:8)
170 // cdb[10]: lba_mid (7:0)
171 // cdb[11]: lba_high (15:8)
172 // cdb[12]: lba_high (7:0)
174 // cdb[14]: (ata) command
175 // cdb[15]: control (SCSI, leave as zero)
177 // 24 bit lba (from MSB): cdb[12] cdb[10] cdb[8]
178 // 48 bit lba (from MSB): cdb[11] cdb[9] cdb[7] cdb[12] cdb[10] cdb[8]
181 // cdb[0]: ATA PASS THROUGH (12) SCSI command opcode byte (0xa1)
182 // cdb[1]: multiple_count, protocol + extend
183 // cdb[2]: offline, ck_cond, t_dir, byte_block + t_length
184 // cdb[3]: features (7:0)
185 // cdb[4]: sector_count (7:0)
186 // cdb[5]: lba_low (7:0)
187 // cdb[6]: lba_mid (7:0)
188 // cdb[7]: lba_high (7:0)
190 // cdb[9]: (ata) command
192 // cdb[11]: control (SCSI, leave as zero)
195 // ATA Return Descriptor (component of descriptor sense data)
196 // des[0]: descriptor code (0x9)
197 // des[1]: additional descriptor length (0xc)
198 // des[2]: extend (bit 0)
200 // des[4]: sector_count (15:8)
201 // des[5]: sector_count (7:0)
202 // des[6]: lba_low (15:8)
203 // des[7]: lba_low (7:0)
204 // des[8]: lba_mid (15:8)
205 // des[9]: lba_mid (7:0)
206 // des[10]: lba_high (15:8)
207 // des[11]: lba_high (7:0)
212 // ATA registers returned via fixed format sense (allowed >= SAT-2)
213 // fxs[0]: info_valid (bit 7); response_code (6:0)
214 // fxs[1]: (obsolete)
215 // fxs[2]: sense_key (3:0) --> recovered error (formerly 'no sense')
216 // fxs[3]: information (31:24) --> ATA Error register
217 // fxs[4]: information (23:16) --> ATA Status register
218 // fxs[5]: information (15:8) --> ATA Device register
219 // fxs[6]: information (7:0) --> ATA Count (7:0)
220 // fxs[7]: additional sense length [should be >= 10]
221 // fxs[8]: command specific info (31:24) --> Extend (7), count_upper_nonzero
222 // (6), lba_upper_nonzero(5), log_index (3:0)
223 // fxs[9]: command specific info (23:16) --> ATA LBA (7:0)
224 // fxs[10]: command specific info (15:8) --> ATA LBA (15:8)
225 // fxs[11]: command specific info (7:0) --> ATA LBA (23:16)
226 // fxs[12]: additional sense code (asc) --> 0x0
227 // fxs[13]: additional sense code qualifier (ascq) --> 0x1d
228 // asc,ascq = 0x0,0x1d --> 'ATA pass through information available'
233 // This interface routine takes ATA SMART commands and packages
234 // them in the SAT-defined ATA PASS THROUGH SCSI commands. There are
235 // two available SCSI commands: a 12 byte and 16 byte variant; the
236 // one used is chosen via this->m_passthrulen .
237 // DETAILED DESCRIPTION OF ARGUMENTS
238 // device: is the file descriptor provided by (a SCSI dvice type) open()
239 // command: defines the different ATA operations.
240 // select: additional input data if needed (which log, which type of
242 // data: location to write output data, if needed (512 bytes).
243 // Note: not all commands use all arguments.
245 // -1 if the command failed
246 // 0 if the command succeeded,
247 // STATUS_CHECK routine:
248 // -1 if the command failed
249 // 0 if the command succeeded and disk SMART status is "OK"
250 // 1 if the command succeeded and disk SMART status is "FAILING"
252 bool sat_device::ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
)
254 if (!ata_cmd_is_supported(in
,
255 ata_device::supports_data_out
|
256 ata_device::supports_output_regs
|
257 ata_device::supports_multi_sector
|
258 ata_device::supports_48bit
,
263 struct scsi_cmnd_io io_hdr
;
264 struct scsi_sense_disect sinfo
;
265 struct sg_scsi_sense_hdr ssh
;
266 unsigned char cdb
[SAT_ATA_PASSTHROUGH_16LEN
];
267 unsigned char sense
[32];
268 const unsigned char * ardp
;
269 int ard_len
, have_sense
;
271 int ck_cond
= 0; /* set to 1 to read register(s) back */
272 int protocol
= 3; /* non-data */
273 int t_dir
= 1; /* 0 -> to device, 1 -> from device */
274 int byte_block
= 1; /* 0 -> bytes, 1 -> 512 byte blocks */
275 int t_length
= 0; /* 0 -> no data transferred */
276 int passthru_size
= DEF_SAT_ATA_PASSTHRU_SIZE
;
277 bool sense_descriptor
= true;
279 memset(cdb
, 0, sizeof(cdb
));
280 memset(sense
, 0, sizeof(sense
));
282 // Set data direction
283 // TODO: This works only for commands where sector_count holds count!
284 switch (in
.direction
) {
285 case ata_cmd_in::no_data
:
287 case ata_cmd_in::data_in
:
288 protocol
= 4; // PIO data-in
289 t_length
= 2; // sector_count holds count
291 case ata_cmd_in::data_out
:
292 protocol
= 5; // PIO data-out
293 t_length
= 2; // sector_count holds count
294 t_dir
= 0; // to device
297 return set_err(EINVAL
, "sat_device::ata_pass_through: invalid direction=%d",
301 // Check condition if any output register needed
302 if (in
.out_needed
.is_set())
305 if ((SAT_ATA_PASSTHROUGH_12LEN
== m_passthrulen
) ||
306 (SAT_ATA_PASSTHROUGH_16LEN
== m_passthrulen
))
307 passthru_size
= m_passthrulen
;
309 // Set extend bit on 48-bit ATA command
310 if (in
.in_regs
.is_48bit_cmd()) {
311 if (passthru_size
!= SAT_ATA_PASSTHROUGH_16LEN
)
312 return set_err(ENOSYS
, "48-bit ATA commands require SAT ATA PASS-THROUGH (16)");
316 cdb
[0] = (SAT_ATA_PASSTHROUGH_12LEN
== passthru_size
) ?
317 SAT_ATA_PASSTHROUGH_12
: SAT_ATA_PASSTHROUGH_16
;
319 cdb
[1] = (protocol
<< 1) | extend
;
320 cdb
[2] = (ck_cond
<< 5) | (t_dir
<< 3) |
321 (byte_block
<< 2) | t_length
;
323 if (passthru_size
== SAT_ATA_PASSTHROUGH_12LEN
) {
324 // ATA PASS-THROUGH (12)
325 const ata_in_regs
& lo
= in
.in_regs
;
326 cdb
[3] = lo
.features
;
327 cdb
[4] = lo
.sector_count
;
330 cdb
[7] = lo
.lba_high
;
335 // ATA PASS-THROUGH (16)
336 const ata_in_regs
& lo
= in
.in_regs
;
337 const ata_in_regs
& hi
= in
.in_regs
.prev
;
338 // Note: all 'in.in_regs.prev.*' are always zero for 28-bit commands
339 cdb
[ 3] = hi
.features
;
340 cdb
[ 4] = lo
.features
;
341 cdb
[ 5] = hi
.sector_count
;
342 cdb
[ 6] = lo
.sector_count
;
343 cdb
[ 7] = hi
.lba_low
;
344 cdb
[ 8] = lo
.lba_low
;
345 cdb
[ 9] = hi
.lba_mid
;
346 cdb
[10] = lo
.lba_mid
;
347 cdb
[11] = hi
.lba_high
;
348 cdb
[12] = lo
.lba_high
;
350 cdb
[14] = lo
.command
;
353 memset(&io_hdr
, 0, sizeof(io_hdr
));
355 io_hdr
.dxfer_dir
= DXFER_NONE
;
356 io_hdr
.dxfer_len
= 0;
357 } else if (t_dir
) { /* from device */
358 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
359 io_hdr
.dxfer_len
= in
.size
;
360 io_hdr
.dxferp
= (unsigned char *)in
.buffer
;
361 memset(in
.buffer
, 0, in
.size
); // prefill with zeroes
362 } else { /* to device */
363 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
364 io_hdr
.dxfer_len
= in
.size
;
365 io_hdr
.dxferp
= (unsigned char *)in
.buffer
;
368 io_hdr
.cmnd_len
= passthru_size
;
369 io_hdr
.sensep
= sense
;
370 io_hdr
.max_sense_len
= sizeof(sense
);
371 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
373 scsi_device
* scsidev
= get_tunnel_dev();
374 if (!scsidev
->scsi_pass_through(&io_hdr
)) {
375 if (scsi_debugmode
> 0)
376 pout("sat_device::ata_pass_through: scsi_pass_through() failed, "
377 "errno=%d [%s]\n", scsidev
->get_errno(), scsidev
->get_errmsg());
378 return set_err(scsidev
->get_err());
382 have_sense
= sg_scsi_normalize_sense(io_hdr
.sensep
, io_hdr
.resp_sense_len
,
385 sense_descriptor
= ssh
.response_code
>= 0x72;
386 if (sense_descriptor
) {
387 /* look for SAT ATA Return Descriptor */
388 ardp
= sg_scsi_sense_desc_find(io_hdr
.sensep
,
389 io_hdr
.resp_sense_len
,
390 ATA_RETURN_DESCRIPTOR
);
392 ard_len
= ardp
[1] + 2;
395 else if (ard_len
> 14)
399 scsi_do_sense_disect(&io_hdr
, &sinfo
);
400 int status
= scsiSimpleSenseFilter(&sinfo
);
403 // Workaround for bogus sense_key in sense data with SAT ATA Return Descriptor
404 if ( status
&& ck_cond
&& ardp
&& ard_len
> 13
405 && (ardp
[13] & 0xc1) == 0x40 /* !BSY && DRDY && !ERR */) {
406 if (scsi_debugmode
> 0)
407 pout("ATA status (0x%02x) indicates success, ignoring SCSI sense_key\n",
413 >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74
414 if (0 != status
) { /* other than no_sense and recovered_error */
415 if (scsi_debugmode
> 0) {
416 pout("sat_device::ata_pass_through: scsi error: %s\n",
417 scsiErrString(status
));
418 if (ardp
&& (scsi_debugmode
> 1)) {
419 pout("Values from ATA Return Descriptor are:\n");
420 dStrHex((const char *)ardp
, ard_len
, 1);
423 if (t_dir
&& (t_length
> 0) && (in
.direction
== ata_cmd_in::data_in
))
424 memset(in
.buffer
, 0, in
.size
);
425 return set_err(EIO
, "scsi error %s", scsiErrString(status
));
428 if (ck_cond
) { /* expecting SAT specific sense data */
431 if (scsi_debugmode
> 1) {
432 pout("Values from ATA Return Descriptor are:\n");
433 dStrHex((const char *)ardp
, ard_len
, 1);
435 // Set output registers
436 ata_out_regs
& lo
= out
.out_regs
;
438 lo
.sector_count
= ardp
[ 5];
439 lo
.lba_low
= ardp
[ 7];
440 lo
.lba_mid
= ardp
[ 9];
441 lo
.lba_high
= ardp
[11];
442 lo
.device
= ardp
[12];
443 lo
.status
= ardp
[13];
444 if (in
.in_regs
.is_48bit_cmd()) {
445 ata_out_regs
& hi
= out
.out_regs
.prev
;
446 hi
.sector_count
= ardp
[ 4];
447 hi
.lba_low
= ardp
[ 6];
448 hi
.lba_mid
= ardp
[ 8];
449 hi
.lba_high
= ardp
[10];
451 } else if ((! sense_descriptor
) &&
454 (SCSI_ASCQ_ATA_PASS_THROUGH
== ssh
.ascq
) &&
455 (0 != io_hdr
.sensep
[4] /* Some ATA STATUS bit must be set */)) {
457 (SCSI_ASCQ_ATA_PASS_THROUGH
== ssh
.ascq
)) {
458 >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74
459 /* in SAT-2 and later, ATA registers may be passed back via
460 * fixed format sense data [ref: sat3r07 section 12.2.2.7] */
461 ata_out_regs
& lo
= out
.out_regs
;
462 lo
.error
= io_hdr
.sensep
[ 3];
463 lo
.status
= io_hdr
.sensep
[ 4];
464 lo
.device
= io_hdr
.sensep
[ 5];
465 lo
.sector_count
= io_hdr
.sensep
[ 6];
466 lo
.lba_low
= io_hdr
.sensep
[ 9];
467 lo
.lba_mid
= io_hdr
.sensep
[10];
468 lo
.lba_high
= io_hdr
.sensep
[11];
469 if (in
.in_regs
.is_48bit_cmd()) {
470 if (0 == (0x60 & io_hdr
.sensep
[8])) {
471 ata_out_regs
& hi
= out
.out_regs
.prev
;
477 /* getting the "hi." values when either
478 * count_upper_nonzero or lba_upper_nonzero are set
479 * involves fetching the SCSI ATA PASS-THROUGH
480 * Results log page and decoding the descriptor with
481 * the matching log_index field. Painful. */
486 } else { /* ck_cond == 0 */
488 if (((SCSI_SK_NO_SENSE
== ssh
.sense_key
) ||
489 (SCSI_SK_RECOVERED_ERR
== ssh
.sense_key
)) &&
491 (SCSI_ASCQ_ATA_PASS_THROUGH
== ssh
.ascq
)) {
492 if (scsi_debugmode
> 0) {
493 if (sense_descriptor
&& ardp
) {
494 pout("Values from ATA Return Descriptor are:\n");
495 dStrHex((const char *)ardp
, ard_len
, 1);
496 } else if (! sense_descriptor
) {
497 pout("Values from ATA fixed format sense are:\n");
498 pout(" Error: 0x%x\n", io_hdr
.sensep
[3]);
499 pout(" Status: 0x%x\n", io_hdr
.sensep
[4]);
500 pout(" Device: 0x%x\n", io_hdr
.sensep
[5]);
501 pout(" Count: 0x%x\n", io_hdr
.sensep
[6]);
505 return set_err(EIO
, "SAT command failed");
511 bool sat_device::scsi_pass_through(scsi_cmnd_io
* iop
)
513 scsi_device
* scsidev
= get_tunnel_dev();
514 if (!scsidev
->scsi_pass_through(iop
)) {
515 set_err(scsidev
->get_err());
521 smart_device
* sat_device::autodetect_open()
523 if (!open() || !m_enable_auto
)
526 scsi_device
* scsidev
= get_tunnel_dev();
528 unsigned char inqdata
[36] = {0, };
529 if (scsiStdInquiry(scsidev
, inqdata
, sizeof(inqdata
))) {
530 smart_device::error_info err
= scsidev
->get_err();
532 set_err(err
.no
, "INQUIRY [SAT]: %s", err
.msg
.c_str());
536 // Check for SAT "VENDOR"
537 int inqsize
= inqdata
[4] + 5;
538 bool sat
= (inqsize
>= 36 && !memcmp(inqdata
+ 8, "ATA ", 8));
544 set_info().dev_type
= (sat
? "sat" : scsidev
->get_dev_type());
545 set_info().info_name
= strprintf("%s [%s]", scsidev
->get_info_name(),
546 (sat
? "SAT" : "SCSI"));
552 /////////////////////////////////////////////////////////////////////////////
554 /* Attempt an IDENTIFY DEVICE ATA command via SATL when packet_interface
555 is false otherwise attempt IDENTIFY PACKET DEVICE. If successful
556 return true, else false */
558 static bool has_sat_pass_through(ata_device
* dev
, bool packet_interface
= false)
560 /* Note: malloc() ensures the read buffer lands on a single
561 page. This avoids some bugs seen on LSI controlers under
563 char *data
= (char *)malloc(512);
565 in
.in_regs
.command
= (packet_interface
? ATA_IDENTIFY_PACKET_DEVICE
: ATA_IDENTIFY_DEVICE
);
566 in
.set_data_in(data
, 1);
567 bool ret
= dev
->ata_pass_through(in
);
572 /////////////////////////////////////////////////////////////////////////////
574 /* Next two functions are borrowed from sg_lib.c in the sg3_utils
575 package. Same copyrght owner, same license as this file. */
576 static int sg_scsi_normalize_sense(const unsigned char * sensep
, int sb_len
,
577 struct sg_scsi_sense_hdr
* sshp
)
580 memset(sshp
, 0, sizeof(struct sg_scsi_sense_hdr
));
581 if ((NULL
== sensep
) || (0 == sb_len
) || (0x70 != (0x70 & sensep
[0])))
584 sshp
->response_code
= (0x7f & sensep
[0]);
585 if (sshp
->response_code
>= 0x72) { /* descriptor format */
587 sshp
->sense_key
= (0xf & sensep
[1]);
589 sshp
->asc
= sensep
[2];
591 sshp
->ascq
= sensep
[3];
593 sshp
->additional_length
= sensep
[7];
594 } else { /* fixed format */
596 sshp
->sense_key
= (0xf & sensep
[2]);
598 sb_len
= (sb_len
< (sensep
[7] + 8)) ? sb_len
:
601 sshp
->asc
= sensep
[12];
603 sshp
->ascq
= sensep
[13];
611 // Call scsi_pass_through and check sense.
612 // TODO: Provide as member function of class scsi_device (?)
613 static bool scsi_pass_through_and_check(scsi_device
* scsidev
, scsi_cmnd_io
* iop
,
614 const char * msg
= "")
616 // Provide sense buffer
617 unsigned char sense
[32] = {0, };
619 iop
->max_sense_len
= sizeof(sense
);
620 iop
->timeout
= SCSI_TIMEOUT_DEFAULT
;
623 if (!scsidev
->scsi_pass_through(iop
)) {
624 if (scsi_debugmode
> 0)
625 pout("%sscsi_pass_through() failed, errno=%d [%s]\n",
626 msg
, scsidev
->get_errno(), scsidev
->get_errmsg());
631 scsi_sense_disect sinfo
;
632 scsi_do_sense_disect(iop
, &sinfo
);
633 int err
= scsiSimpleSenseFilter(&sinfo
);
635 if (scsi_debugmode
> 0)
636 pout("%sscsi error: %s\n", msg
, scsiErrString(err
));
637 return scsidev
->set_err(EIO
, "scsi error %s", scsiErrString(err
));
644 /////////////////////////////////////////////////////////////////////////////
648 /// Cypress USB Brigde support.
650 class usbcypress_device
651 : public tunnelled_device
<
652 /*implements*/ ata_device_with_command_set
653 /*by tunnelling through a*/, scsi_device
657 usbcypress_device(smart_interface
* intf
, scsi_device
* scsidev
,
658 const char * req_type
, unsigned char signature
);
660 virtual ~usbcypress_device() throw();
663 virtual int ata_command_interface(smart_command_set command
, int select
, char * data
);
665 unsigned char m_signature
;
669 usbcypress_device::usbcypress_device(smart_interface
* intf
, scsi_device
* scsidev
,
670 const char * req_type
, unsigned char signature
)
671 : smart_device(intf
, scsidev
->get_dev_name(), "sat", req_type
),
672 tunnelled_device
<ata_device_with_command_set
, scsi_device
>(scsidev
),
673 m_signature(signature
)
675 set_info().info_name
= strprintf("%s [USB Cypress]", scsidev
->get_info_name());
678 usbcypress_device::~usbcypress_device() throw()
683 /* see cy7c68300c_8.pdf for more information */
684 #define USBCYPRESS_PASSTHROUGH_LEN 16
685 int usbcypress_device::ata_command_interface(smart_command_set command
, int select
, char *data
)
687 struct scsi_cmnd_io io_hdr
;
688 unsigned char cdb
[USBCYPRESS_PASSTHROUGH_LEN
];
689 unsigned char sense
[32];
692 int ck_cond
= 0; /* set to 1 to read register(s) back */
693 int t_dir
= 1; /* 0 -> to device, 1 -> from device */
694 int byte_block
= 1; /* 0 -> bytes, 1 -> 512 byte blocks */
695 int t_length
= 0; /* 0 -> no data transferred */
698 int sector_count
= 0;
702 int passthru_size
= USBCYPRESS_PASSTHROUGH_LEN
;
704 memset(cdb
, 0, sizeof(cdb
));
705 memset(sense
, 0, sizeof(sense
));
707 ata_command
= ATA_SMART_CMD
;
709 case CHECK_POWER_MODE
:
710 ata_command
= ATA_CHECK_POWER_MODE
;
714 case READ_VALUES
: /* READ DATA */
715 feature
= ATA_SMART_READ_VALUES
;
716 sector_count
= 1; /* one (512 byte) block */
717 t_length
= 2; /* sector count holds count */
720 case READ_THRESHOLDS
: /* obsolete */
721 feature
= ATA_SMART_READ_THRESHOLDS
;
722 sector_count
= 1; /* one (512 byte) block */
724 t_length
= 2; /* sector count holds count */
728 feature
= ATA_SMART_READ_LOG_SECTOR
;
729 sector_count
= 1; /* one (512 byte) block */
731 t_length
= 2; /* sector count holds count */
735 feature
= ATA_SMART_WRITE_LOG_SECTOR
;
736 sector_count
= 1; /* one (512 byte) block */
738 t_length
= 2; /* sector count holds count */
739 t_dir
= 0; /* to device */
743 ata_command
= ATA_IDENTIFY_DEVICE
;
744 sector_count
= 1; /* one (512 byte) block */
745 t_length
= 2; /* sector count holds count */
749 ata_command
= ATA_IDENTIFY_PACKET_DEVICE
;
750 sector_count
= 1; /* one (512 byte) block */
751 t_length
= 2; /* sector count (7:0) holds count */
755 feature
= ATA_SMART_ENABLE
;
759 feature
= ATA_SMART_DISABLE
;
763 // this command only says if SMART is working. It could be
764 // replaced with STATUS_CHECK below.
765 feature
= ATA_SMART_STATUS
;
769 feature
= ATA_SMART_AUTO_OFFLINE
;
770 sector_count
= select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
773 feature
= ATA_SMART_AUTOSAVE
;
774 sector_count
= select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
776 case IMMEDIATE_OFFLINE
:
777 feature
= ATA_SMART_IMMEDIATE_OFFLINE
;
781 // This command uses HDIO_DRIVE_TASK and has different syntax than
782 // the other commands.
783 feature
= ATA_SMART_STATUS
; /* SMART RETURN STATUS */
787 pout("Unrecognized command %d in usbcypress_device::ata_command_interface()\n"
788 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
792 if (ATA_SMART_CMD
== ata_command
) {
797 cdb
[0] = m_signature
; // bVSCBSignature : vendor-specific command
798 cdb
[1] = 0x24; // bVSCBSubCommand : 0x24 for ATACB
800 if (ata_command
== ATA_IDENTIFY_DEVICE
|| ata_command
== ATA_IDENTIFY_PACKET_DEVICE
)
801 cdb
[2] |= (1<<7); //set IdentifyPacketDevice for these cmds
802 cdb
[3] = 0xff - (1<<0) - (1<<6); //features, sector count, lba low, lba med
803 // lba high, command are valid
804 cdb
[4] = byte_block
; //TransferBlockCount : 512
808 cdb
[7] = sector_count
;
812 cdb
[12] = ata_command
;
814 memset(&io_hdr
, 0, sizeof(io_hdr
));
816 io_hdr
.dxfer_dir
= DXFER_NONE
;
817 io_hdr
.dxfer_len
= 0;
818 } else if (t_dir
) { /* from device */
819 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
820 io_hdr
.dxfer_len
= copydata
;
821 io_hdr
.dxferp
= (unsigned char *)data
;
822 memset(data
, 0, copydata
); /* prefill with zeroes */
823 } else { /* to device */
824 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
825 io_hdr
.dxfer_len
= outlen
;
826 io_hdr
.dxferp
= (unsigned char *)data
;
829 io_hdr
.cmnd_len
= passthru_size
;
830 io_hdr
.sensep
= sense
;
831 io_hdr
.max_sense_len
= sizeof(sense
);
832 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
834 scsi_device
* scsidev
= get_tunnel_dev();
835 if (!scsidev
->scsi_pass_through(&io_hdr
)) {
836 if (scsi_debugmode
> 0)
837 pout("usbcypress_device::ata_command_interface: scsi_pass_through() failed, "
838 "errno=%d [%s]\n", scsidev
->get_errno(), scsidev
->get_errmsg());
839 set_err(scsidev
->get_err());
843 // if there is a sense the command failed or the
844 // device doesn't support usbcypress
845 if (io_hdr
.scsi_status
== SCSI_STATUS_CHECK_CONDITION
&&
846 sg_scsi_normalize_sense(io_hdr
.sensep
, io_hdr
.resp_sense_len
, NULL
)) {
850 unsigned char ardp
[8];
852 /* XXX this is racy if there other scsi command between
853 * the first usbcypress command and this one
855 //pout("If you got strange result, please retry without traffic on the disc\n");
856 /* we use the same command as before, but we set
857 * * the read taskfile bit, for not executing usbcypress command,
858 * * but reading register selected in srb->cmnd[4]
860 cdb
[2] = (1<<0); /* ask read taskfile */
861 memset(sense
, 0, sizeof(sense
));
863 /* transfert 8 bytes */
864 memset(&io_hdr
, 0, sizeof(io_hdr
));
865 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
866 io_hdr
.dxfer_len
= ard_len
;
867 io_hdr
.dxferp
= (unsigned char *)ardp
;
868 memset(ardp
, 0, ard_len
); /* prefill with zeroes */
871 io_hdr
.cmnd_len
= passthru_size
;
872 io_hdr
.sensep
= sense
;
873 io_hdr
.max_sense_len
= sizeof(sense
);
874 io_hdr
.timeout
= SCSI_TIMEOUT_DEFAULT
;
877 if (!scsidev
->scsi_pass_through(&io_hdr
)) {
878 if (scsi_debugmode
> 0)
879 pout("usbcypress_device::ata_command_interface: scsi_pass_through() failed, "
880 "errno=%d [%s]\n", scsidev
->get_errno(), scsidev
->get_errmsg());
881 set_err(scsidev
->get_err());
884 // if there is a sense the command failed or the
885 // device doesn't support usbcypress
886 if (io_hdr
.scsi_status
== SCSI_STATUS_CHECK_CONDITION
&&
887 sg_scsi_normalize_sense(io_hdr
.sensep
, io_hdr
.resp_sense_len
, NULL
)) {
892 if (scsi_debugmode
> 1) {
893 pout("Values from ATA Return Descriptor are:\n");
894 dStrHex((const char *)ardp
, ard_len
, 1);
897 if (ATA_CHECK_POWER_MODE
== ata_command
)
898 data
[0] = ardp
[2]; /* sector count (0:7) */
899 else if (STATUS_CHECK
== command
) {
900 if ((ardp
[4] == 0x4f) && (ardp
[5] == 0xc2))
901 return 0; /* GOOD smart status */
902 if ((ardp
[4] == 0xf4) && (ardp
[5] == 0x2c))
903 return 1; // smart predicting failure, "bad" status
904 // We haven't gotten output that makes sense so
905 // print out some debugging info
906 syserror("Error SMART Status command failed");
907 pout("This may be due to a race in usbcypress\n");
908 pout("Retry without other disc access\n");
909 pout("Please get assistance from " PACKAGE_HOMEPAGE
"\n");
910 pout("Values from ATA Return Descriptor are:\n");
911 dStrHex((const char *)ardp
, ard_len
, 1);
918 #if 0 // Not used, see autodetect_sat_device() below.
919 static int isprint_string(const char *s
)
922 if (isprint(*s
) == 0)
929 /* Attempt an IDENTIFY DEVICE ATA or IDENTIFY PACKET DEVICE command
930 If successful return 1, else 0 */
931 // TODO: Combine with has_sat_pass_through above
932 static int has_usbcypress_pass_through(ata_device
* atadev
, const char *manufacturer
, const char *product
)
934 struct ata_identify_device drive
;
935 char model
[40], serial
[20], firm
[8];
937 /* issue the command and do a checksum if possible */
938 if (ataReadHDIdentity(atadev
, &drive
) < 0)
941 /* check if model string match, revision doesn't work for me */
942 format_ata_string(model
, drive
.model
, 40);
943 if (*model
== 0 || isprint_string(model
) == 0)
946 if (manufacturer
&& strncmp(manufacturer
, model
, 8))
947 pout("manufacturer doesn't match in pass_through test\n");
949 strlen(model
) > 8 && strncmp(product
, model
+8, strlen(model
)-8))
950 pout("product doesn't match in pass_through test\n");
953 format_ata_string(serial
, drive
.serial_no
, 20);
954 if (isprint_string(serial
) == 0)
956 format_ata_string(firm
, drive
.fw_rev
, 8);
957 if (isprint_string(firm
) == 0)
963 /////////////////////////////////////////////////////////////////////////////
965 /// JMicron USB Bridge support.
967 class usbjmicron_device
968 : public tunnelled_device
<
969 /*implements*/ ata_device
,
970 /*by tunnelling through a*/ scsi_device
974 usbjmicron_device(smart_interface
* intf
, scsi_device
* scsidev
,
975 const char * req_type
, bool prolific
,
976 bool ata_48bit_support
, int port
);
978 virtual ~usbjmicron_device() throw();
982 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
);
985 bool get_registers(unsigned short addr
, unsigned char * buf
, unsigned short size
);
988 bool m_ata_48bit_support
;
993 usbjmicron_device::usbjmicron_device(smart_interface
* intf
, scsi_device
* scsidev
,
994 const char * req_type
, bool prolific
,
995 bool ata_48bit_support
, int port
)
996 : smart_device(intf
, scsidev
->get_dev_name(), "usbjmicron", req_type
),
997 tunnelled_device
<ata_device
, scsi_device
>(scsidev
),
998 m_prolific(prolific
), m_ata_48bit_support(ata_48bit_support
),
999 m_port(port
>= 0 || !prolific
? port
: 0)
1001 set_info().info_name
= strprintf("%s [USB JMicron]", scsidev
->get_info_name());
1004 usbjmicron_device::~usbjmicron_device() throw()
1009 bool usbjmicron_device::open()
1012 if (!tunnelled_device
<ata_device
, scsi_device
>::open())
1015 // Detect port if not specified
1017 unsigned char regbuf
[1] = {0};
1018 if (!get_registers(0x720f, regbuf
, sizeof(regbuf
))) {
1023 switch (regbuf
[0] & 0x44) {
1030 return set_err(EINVAL
, "Two devices connected, try '-d usbjmicron,[01]'");
1033 return set_err(ENODEV
, "No device connected");
1041 bool usbjmicron_device::ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
)
1043 if (!ata_cmd_is_supported(in
,
1044 ata_device::supports_data_out
|
1045 ata_device::supports_smart_status
|
1046 (m_ata_48bit_support
? ata_device::supports_48bit_hi_null
: 0),
1052 return set_err(EIO
, "Unknown JMicron port");
1054 scsi_cmnd_io io_hdr
;
1055 memset(&io_hdr
, 0, sizeof(io_hdr
));
1058 unsigned char smart_status
= 0xff;
1060 bool is_smart_status
= ( in
.in_regs
.command
== ATA_SMART_CMD
1061 && in
.in_regs
.features
== ATA_SMART_STATUS
);
1063 if (is_smart_status
&& in
.out_needed
.is_set()) {
1064 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1065 io_hdr
.dxfer_len
= 1;
1066 io_hdr
.dxferp
= &smart_status
;
1068 else switch (in
.direction
) {
1069 case ata_cmd_in::no_data
:
1070 io_hdr
.dxfer_dir
= DXFER_NONE
;
1072 case ata_cmd_in::data_in
:
1073 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1074 io_hdr
.dxfer_len
= in
.size
;
1075 io_hdr
.dxferp
= (unsigned char *)in
.buffer
;
1076 memset(in
.buffer
, 0, in
.size
);
1078 case ata_cmd_in::data_out
:
1079 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
1080 io_hdr
.dxfer_len
= in
.size
;
1081 io_hdr
.dxferp
= (unsigned char *)in
.buffer
;
1085 return set_err(EINVAL
);
1088 // Build pass through command
1089 unsigned char cdb
[14];
1091 cdb
[ 1] = (rwbit
? 0x10 : 0x00);
1093 cdb
[ 3] = (unsigned char)(io_hdr
.dxfer_len
>> 8);
1094 cdb
[ 4] = (unsigned char)(io_hdr
.dxfer_len
);
1095 cdb
[ 5] = in
.in_regs
.features
;
1096 cdb
[ 6] = in
.in_regs
.sector_count
;
1097 cdb
[ 7] = in
.in_regs
.lba_low
;
1098 cdb
[ 8] = in
.in_regs
.lba_mid
;
1099 cdb
[ 9] = in
.in_regs
.lba_high
;
1100 cdb
[10] = in
.in_regs
.device
| (m_port
== 0 ? 0xa0 : 0xb0);
1101 cdb
[11] = in
.in_regs
.command
;
1107 io_hdr
.cmnd_len
= (!m_prolific
? 12 : 14);
1109 scsi_device
* scsidev
= get_tunnel_dev();
1110 if (!scsi_pass_through_and_check(scsidev
, &io_hdr
,
1111 "usbjmicron_device::ata_pass_through: "))
1112 return set_err(scsidev
->get_err());
1114 if (in
.out_needed
.is_set()) {
1115 if (is_smart_status
) {
1116 if (io_hdr
.resid
== 1)
1117 // Some (Prolific) USB bridges do not transfer a status byte
1118 return set_err(ENOSYS
, "Incomplete response, status byte missing [JMicron]");
1120 switch (smart_status
) {
1122 out
.out_regs
.lba_high
= 0xc2;
1123 out
.out_regs
.lba_mid
= 0x4f;
1126 out
.out_regs
.lba_high
= 0x2c;
1127 out
.out_regs
.lba_mid
= 0xf4;
1130 // Some (JM20336) USB bridges always return 0x01, regardless of SMART Status
1131 return set_err(ENOSYS
, "Invalid status byte (0x%02x) [JMicron]", smart_status
);
1135 #if 0 // Not needed for SMART STATUS, see also notes below
1137 // Read ATA output registers
1138 // NOTE: The register addresses are not valid for some older chip revisions
1139 // NOTE: There is a small race condition here!
1140 unsigned char regbuf
[16] = {0, };
1141 if (!get_registers((m_port
== 0 ? 0x8000 : 0x9000), regbuf
, sizeof(regbuf
)))
1144 out
.out_regs
.sector_count
= regbuf
[ 0];
1145 out
.out_regs
.lba_mid
= regbuf
[ 4];
1146 out
.out_regs
.lba_low
= regbuf
[ 6];
1147 out
.out_regs
.device
= regbuf
[ 9];
1148 out
.out_regs
.lba_high
= regbuf
[10];
1149 out
.out_regs
.error
= regbuf
[13];
1150 out
.out_regs
.status
= regbuf
[14];
1158 bool usbjmicron_device::get_registers(unsigned short addr
,
1159 unsigned char * buf
, unsigned short size
)
1161 unsigned char cdb
[14];
1165 cdb
[ 3] = (unsigned char)(size
>> 8);
1166 cdb
[ 4] = (unsigned char)(size
);
1168 cdb
[ 6] = (unsigned char)(addr
>> 8);
1169 cdb
[ 7] = (unsigned char)(addr
);
1178 scsi_cmnd_io io_hdr
;
1179 memset(&io_hdr
, 0, sizeof(io_hdr
));
1180 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1181 io_hdr
.dxfer_len
= size
;
1182 io_hdr
.dxferp
= buf
;
1184 io_hdr
.cmnd_len
= sizeof(cdb
);
1185 io_hdr
.cmnd_len
= (!m_prolific
? 12 : 14);
1187 scsi_device
* scsidev
= get_tunnel_dev();
1188 if (!scsi_pass_through_and_check(scsidev
, &io_hdr
,
1189 "usbjmicron_device::get_registers: "))
1190 return set_err(scsidev
->get_err());
1196 /////////////////////////////////////////////////////////////////////////////
1198 /// Prolific USB Bridge support. (PL2773) (Probably works on PL2771 also...)
1200 class usbprolific_device
1201 : public tunnelled_device
<
1202 /*implements*/ ata_device
,
1203 /*by tunnelling through a*/ scsi_device
1207 usbprolific_device(smart_interface
* intf
, scsi_device
* scsidev
,
1208 const char * req_type
);
1210 virtual ~usbprolific_device() throw();
1212 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
);
1216 usbprolific_device::usbprolific_device(smart_interface
* intf
, scsi_device
* scsidev
,
1217 const char * req_type
)
1218 : smart_device(intf
, scsidev
->get_dev_name(), "usbprolific", req_type
),
1219 tunnelled_device
<ata_device
, scsi_device
>(scsidev
)
1221 set_info().info_name
= strprintf("%s [USB Prolific]", scsidev
->get_info_name());
1224 usbprolific_device::~usbprolific_device() throw()
1228 bool usbprolific_device::ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
)
1230 if (!ata_cmd_is_supported(in
,
1231 ata_device::supports_data_out
|
1232 ata_device::supports_48bit_hi_null
|
1233 ata_device::supports_output_regs
|
1234 ata_device::supports_smart_status
,
1239 scsi_cmnd_io io_hdr
;
1240 memset(&io_hdr
, 0, sizeof(io_hdr
));
1241 unsigned char cmd_rw
= 0x10; // Read
1243 switch (in
.direction
) {
1244 case ata_cmd_in::no_data
:
1245 io_hdr
.dxfer_dir
= DXFER_NONE
;
1247 case ata_cmd_in::data_in
:
1248 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1249 io_hdr
.dxfer_len
= in
.size
;
1250 io_hdr
.dxferp
= (unsigned char *)in
.buffer
;
1251 memset(in
.buffer
, 0, in
.size
);
1253 case ata_cmd_in::data_out
:
1254 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
1255 io_hdr
.dxfer_len
= in
.size
;
1256 io_hdr
.dxferp
= (unsigned char *)in
.buffer
;
1257 cmd_rw
= 0x0; // Write
1260 return set_err(EINVAL
);
1263 // Based on reverse engineering of iSmart.exe with API Monitor.
1265 // D0 0 0 0 06 7B 0 0 0 0 0 0 // Read Firmware info?, reads 16 bytes
1266 // F4 0 0 0 06 7B // ??
1267 // D8 15 0 D8 06 7B 0 0 0 0 1 1 4F C2 A0 B0 // SMART Enable
1268 // D8 15 0 D0 06 7B 0 0 2 0 1 1 4F C2 A0 B0 // SMART Read values
1269 // D8 15 0 D1 06 7B 0 0 2 0 1 1 4F C2 A0 B0 // SMART Read thresholds
1270 // D8 15 0 D4 06 7B 0 0 0 0 0 1 4F C2 A0 B0 // SMART Execute self test
1271 // D7 0 0 0 06 7B 0 0 0 0 0 0 0 0 0 0 // Read status registers, Reads 16 bytes of data
1272 // Additional DATA OUT support based on document from Prolific
1274 // Build pass through command
1275 unsigned char cdb
[16];
1276 cdb
[ 0] = 0xD8; // Operation Code (D8 = Prolific ATA pass through)
1277 cdb
[ 1] = cmd_rw
|0x5; // Read(0x10)/Write(0x0) | NORMAL(0x5)/PREFIX(0x0)(?)
1278 cdb
[ 2] = 0x0; // Reserved
1279 cdb
[ 3] = in
.in_regs
.features
; // Feature register (SMART command)
1280 cdb
[ 4] = 0x06; // Check Word (VendorID magic, Prolific: 0x067B)
1281 cdb
[ 5] = 0x7B; // Check Word (VendorID magic, Prolific: 0x067B)
1282 cdb
[ 6] = (unsigned char)(io_hdr
.dxfer_len
>> 24); // Length MSB
1283 cdb
[ 7] = (unsigned char)(io_hdr
.dxfer_len
>> 16); // Length ...
1284 cdb
[ 8] = (unsigned char)(io_hdr
.dxfer_len
>> 8); // Length ...
1285 cdb
[ 9] = (unsigned char)(io_hdr
.dxfer_len
); // Length LSB
1286 cdb
[10] = in
.in_regs
.sector_count
; // Sector Count
1287 cdb
[11] = in
.in_regs
.lba_low
; // LBA Low (7:0)
1288 cdb
[12] = in
.in_regs
.lba_mid
; // LBA Mid (15:8)
1289 cdb
[13] = in
.in_regs
.lba_high
; // LBA High (23:16)
1290 cdb
[14] = in
.in_regs
.device
| 0xA0; // Device/Head
1291 cdb
[15] = in
.in_regs
.command
; // ATA Command Register (only PIO supported)
1292 // Use '-r scsiioctl,1' to print CDB for debug purposes
1295 io_hdr
.cmnd_len
= 16;
1297 scsi_device
* scsidev
= get_tunnel_dev();
1298 if (!scsi_pass_through_and_check(scsidev
, &io_hdr
,
1299 "usbprolific_device::ata_pass_through: "))
1300 return set_err(scsidev
->get_err());
1302 if (in
.out_needed
.is_set()) {
1303 // Read ATA output registers
1304 unsigned char regbuf
[16] = {0, };
1305 memset(&io_hdr
, 0, sizeof(io_hdr
));
1306 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1307 io_hdr
.dxfer_len
= sizeof(regbuf
);
1308 io_hdr
.dxferp
= regbuf
;
1310 memset(cdb
, 0, sizeof(cdb
));
1311 cdb
[ 0] = 0xD7; // Prolific read registers
1312 cdb
[ 4] = 0x06; // Check Word (VendorID magic, Prolific: 0x067B)
1313 cdb
[ 5] = 0x7B; // Check Word (VendorID magic, Prolific: 0x067B)
1315 io_hdr
.cmnd_len
= sizeof(cdb
);
1317 if (!scsi_pass_through_and_check(scsidev
, &io_hdr
,
1318 "usbprolific_device::scsi_pass_through (get registers): "))
1319 return set_err(scsidev
->get_err());
1321 // Use '-r scsiioctl,2' to print input registers for debug purposes
1322 // Example: 50 00 00 00 00 01 4f 00 c2 00 a0 da 00 b0 00 50
1323 out
.out_regs
.status
= regbuf
[0]; // Status
1324 out
.out_regs
.error
= regbuf
[1]; // Error
1325 out
.out_regs
.sector_count
= regbuf
[2]; // Sector Count (7:0)
1326 out
.out_regs
.lba_low
= regbuf
[4]; // LBA Low (7:0)
1327 out
.out_regs
.lba_mid
= regbuf
[6]; // LBA Mid (7:0)
1328 out
.out_regs
.lba_high
= regbuf
[8]; // LBA High (7:0)
1329 out
.out_regs
.device
= regbuf
[10]; // Device/Head
1330 // = regbuf[11]; // ATA Feature (7:0)
1331 // = regbuf[13]; // ATA Command
1338 /////////////////////////////////////////////////////////////////////////////
1340 /// SunplusIT USB Bridge support.
1342 class usbsunplus_device
1343 : public tunnelled_device
<
1344 /*implements*/ ata_device
,
1345 /*by tunnelling through a*/ scsi_device
1349 usbsunplus_device(smart_interface
* intf
, scsi_device
* scsidev
,
1350 const char * req_type
);
1352 virtual ~usbsunplus_device() throw();
1354 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
);
1358 usbsunplus_device::usbsunplus_device(smart_interface
* intf
, scsi_device
* scsidev
,
1359 const char * req_type
)
1360 : smart_device(intf
, scsidev
->get_dev_name(), "usbsunplus", req_type
),
1361 tunnelled_device
<ata_device
, scsi_device
>(scsidev
)
1363 set_info().info_name
= strprintf("%s [USB Sunplus]", scsidev
->get_info_name());
1366 usbsunplus_device::~usbsunplus_device() throw()
1370 bool usbsunplus_device::ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
)
1372 if (!ata_cmd_is_supported(in
,
1373 ata_device::supports_data_out
|
1374 ata_device::supports_output_regs
|
1375 ata_device::supports_48bit
,
1380 scsi_cmnd_io io_hdr
;
1381 unsigned char cdb
[12];
1383 if (in
.in_regs
.is_48bit_cmd()) {
1384 // Set "previous" registers
1385 memset(&io_hdr
, 0, sizeof(io_hdr
));
1386 io_hdr
.dxfer_dir
= DXFER_NONE
;
1390 cdb
[ 2] = 0x23; // Subcommand: Pass through presetting
1393 cdb
[ 5] = in
.in_regs
.prev
.features
;
1394 cdb
[ 6] = in
.in_regs
.prev
.sector_count
;
1395 cdb
[ 7] = in
.in_regs
.prev
.lba_low
;
1396 cdb
[ 8] = in
.in_regs
.prev
.lba_mid
;
1397 cdb
[ 9] = in
.in_regs
.prev
.lba_high
;
1402 io_hdr
.cmnd_len
= sizeof(cdb
);
1404 scsi_device
* scsidev
= get_tunnel_dev();
1405 if (!scsi_pass_through_and_check(scsidev
, &io_hdr
,
1406 "usbsunplus_device::scsi_pass_through (presetting): "))
1407 return set_err(scsidev
->get_err());
1410 // Run Pass through command
1411 memset(&io_hdr
, 0, sizeof(io_hdr
));
1412 unsigned char protocol
;
1413 switch (in
.direction
) {
1414 case ata_cmd_in::no_data
:
1415 io_hdr
.dxfer_dir
= DXFER_NONE
;
1418 case ata_cmd_in::data_in
:
1419 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1420 io_hdr
.dxfer_len
= in
.size
;
1421 io_hdr
.dxferp
= (unsigned char *)in
.buffer
;
1422 memset(in
.buffer
, 0, in
.size
);
1425 case ata_cmd_in::data_out
:
1426 io_hdr
.dxfer_dir
= DXFER_TO_DEVICE
;
1427 io_hdr
.dxfer_len
= in
.size
;
1428 io_hdr
.dxferp
= (unsigned char *)in
.buffer
;
1432 return set_err(EINVAL
);
1437 cdb
[ 2] = 0x22; // Subcommand: Pass through
1439 cdb
[ 4] = (unsigned char)(io_hdr
.dxfer_len
>> 9);
1440 cdb
[ 5] = in
.in_regs
.features
;
1441 cdb
[ 6] = in
.in_regs
.sector_count
;
1442 cdb
[ 7] = in
.in_regs
.lba_low
;
1443 cdb
[ 8] = in
.in_regs
.lba_mid
;
1444 cdb
[ 9] = in
.in_regs
.lba_high
;
1445 cdb
[10] = in
.in_regs
.device
| 0xa0;
1446 cdb
[11] = in
.in_regs
.command
;
1449 io_hdr
.cmnd_len
= sizeof(cdb
);
1451 scsi_device
* scsidev
= get_tunnel_dev();
1452 if (!scsi_pass_through_and_check(scsidev
, &io_hdr
,
1453 "usbsunplus_device::scsi_pass_through: "))
1454 // Returns sense key 0x03 (medium error) on ATA command error
1455 return set_err(scsidev
->get_err());
1457 if (in
.out_needed
.is_set()) {
1458 // Read ATA output registers
1459 unsigned char regbuf
[8] = {0, };
1460 memset(&io_hdr
, 0, sizeof(io_hdr
));
1461 io_hdr
.dxfer_dir
= DXFER_FROM_DEVICE
;
1462 io_hdr
.dxfer_len
= sizeof(regbuf
);
1463 io_hdr
.dxferp
= regbuf
;
1467 cdb
[ 2] = 0x21; // Subcommand: Get status
1468 memset(cdb
+3, 0, sizeof(cdb
)-3);
1470 io_hdr
.cmnd_len
= sizeof(cdb
);
1472 if (!scsi_pass_through_and_check(scsidev
, &io_hdr
,
1473 "usbsunplus_device::scsi_pass_through (get registers): "))
1474 return set_err(scsidev
->get_err());
1476 out
.out_regs
.error
= regbuf
[1];
1477 out
.out_regs
.sector_count
= regbuf
[2];
1478 out
.out_regs
.lba_low
= regbuf
[3];
1479 out
.out_regs
.lba_mid
= regbuf
[4];
1480 out
.out_regs
.lba_high
= regbuf
[5];
1481 out
.out_regs
.device
= regbuf
[6];
1482 out
.out_regs
.status
= regbuf
[7];
1491 using namespace sat
;
1494 /////////////////////////////////////////////////////////////////////////////
1496 // Return ATA->SCSI filter for SAT or USB.
1498 ata_device
* smart_interface::get_sat_device(const char * type
, scsi_device
* scsidev
)
1501 throw std::logic_error("smart_interface: get_sat_device() called with scsidev=0");
1503 // Take temporary ownership of 'scsidev' to delete it on error
1504 scsi_device_auto_ptr
scsidev_holder(scsidev
);
1505 ata_device
* satdev
= 0;
1507 if (!strncmp(type
, "sat", 3)) {
1508 const char * t
= type
+ 3;
1509 bool enable_auto
= false;
1510 if (!strncmp(t
, ",auto", 5)) {
1514 int ptlen
= 0, n
= -1;
1515 if (*t
&& !(sscanf(t
, ",%d%n", &ptlen
, &n
) == 1 && n
== (int)strlen(t
)
1516 && (ptlen
== 0 || ptlen
== 12 || ptlen
== 16))) {
1517 set_err(EINVAL
, "Option '-d sat[,auto][,N]' requires N to be 0, 12 or 16");
1520 satdev
= new sat_device(this, scsidev
, type
, ptlen
, enable_auto
);
1523 else if (!strncmp(type
, "usbcypress", 10)) {
1524 unsigned signature
= 0x24; int n1
= -1, n2
= -1;
1525 if (!(((sscanf(type
, "usbcypress%n,0x%x%n", &n1
, &signature
, &n2
) == 1 && n2
== (int)strlen(type
)) || n1
== (int)strlen(type
))
1526 && signature
<= 0xff)) {
1527 set_err(EINVAL
, "Option '-d usbcypress,<n>' requires <n> to be "
1528 "an hexadecimal number between 0x0 and 0xff");
1531 satdev
= new usbcypress_device(this, scsidev
, type
, signature
);
1534 else if (!strncmp(type
, "usbjmicron", 10)) {
1535 const char * t
= type
+ 10;
1536 bool prolific
= false;
1537 if (!strncmp(t
, ",p", 2)) {
1541 bool ata_48bit_support
= false;
1542 if (!strncmp(t
, ",x", 2)) {
1544 ata_48bit_support
= true;
1546 int port
= -1, n
= -1;
1547 if (*t
&& !( (sscanf(t
, ",%d%n", &port
, &n
) == 1
1548 && n
== (int)strlen(t
) && 0 <= port
&& port
<= 1))) {
1549 set_err(EINVAL
, "Option '-d usbjmicron[,p][,x],<n>' requires <n> to be 0 or 1");
1552 satdev
= new usbjmicron_device(this, scsidev
, type
, prolific
, ata_48bit_support
, port
);
1555 else if (!strcmp(type
, "usbprolific")) {
1556 satdev
= new usbprolific_device(this, scsidev
, type
);
1559 else if (!strcmp(type
, "usbprolific")) {
1560 return new usbprolific_device(this, scsidev
, type
);
1563 else if (!strcmp(type
, "usbsunplus")) {
1564 satdev
= new usbsunplus_device(this, scsidev
, type
);
1568 set_err(EINVAL
, "Unknown USB device type '%s'", type
);
1572 // 'scsidev' is now owned by 'satdev'
1573 scsidev_holder
.release();
1577 // Try to detect a SAT device behind a SCSI interface.
1579 ata_device
* smart_interface::autodetect_sat_device(scsi_device
* scsidev
,
1580 const unsigned char * inqdata
, unsigned inqsize
)
1582 if (!scsidev
->is_open())
1586 if (inqdata
&& inqsize
>= 36 && !memcmp(inqdata
+ 8, "ATA ", 8)) { // TODO: Linux-specific?
1587 ata_device_auto_ptr
atadev( new sat_device(this, scsidev
, "") , scsidev
);
1588 if (has_sat_pass_through(atadev
.get()))
1589 return atadev
.release(); // Detected SAT
1596 /////////////////////////////////////////////////////////////////////////////
1597 // USB device type detection
1599 // Format USB ID for error messages
1600 static std::string
format_usb_id(int vendor_id
, int product_id
, int version
)
1603 return strprintf("[0x%04x:0x%04x (0x%03x)]", vendor_id
, product_id
, version
);
1605 return strprintf("[0x%04x:0x%04x]", vendor_id
, product_id
);
1608 // Get type name for USB device with known VENDOR:PRODUCT ID.
1609 const char * smart_interface::get_usb_dev_type_by_id(int vendor_id
, int product_id
,
1610 int version
/*= -1*/)
1612 usb_dev_info info
, info2
;
1613 int n
= lookup_usb_device(vendor_id
, product_id
, version
, info
, info2
);
1616 set_err(EINVAL
, "Unknown USB bridge %s",
1617 format_usb_id(vendor_id
, product_id
, version
).c_str());
1622 set_err(EINVAL
, "USB bridge %s type is ambiguous: '%s' or '%s'",
1623 format_usb_id(vendor_id
, product_id
, version
).c_str(),
1624 (!info
.usb_type
.empty() ? info
.usb_type
.c_str() : "[unsupported]"),
1625 (!info2
.usb_type
.empty() ? info2
.usb_type
.c_str() : "[unsupported]"));
1629 if (info
.usb_type
.empty()) {
1630 set_err(ENOSYS
, "Unsupported USB bridge %s",
1631 format_usb_id(vendor_id
, product_id
, version
).c_str());
1635 // TODO: change return type to std::string
1636 static std::string type
;
1637 type
= info
.usb_type
;
1638 return type
.c_str();