]>
Commit | Line | Data |
---|---|---|
4d59bff9 GG |
1 | /* |
2 | * scsiata.cpp | |
3 | * | |
a86ec89e | 4 | * Home page of code is: http://www.smartmontools.org |
4d59bff9 | 5 | * |
a86ec89e | 6 | * Copyright (C) 2006-15 Douglas Gilbert <dgilbert@interlog.com> |
ff28b140 | 7 | * Copyright (C) 2009-18 Christian Franke |
4d59bff9 | 8 | * |
ff28b140 | 9 | * SPDX-License-Identifier: GPL-2.0-or-later |
4d59bff9 GG |
10 | * The code in this file is based on the SCSI to ATA Translation (SAT) |
11 | * draft found at http://www.t10.org . The original draft used for this | |
12 | * code is sat-r08.pdf which is not too far away from becoming a | |
13 | * standard. The SAT commands of interest to smartmontools are the | |
14 | * ATA PASS THROUGH SCSI (16) and ATA PASS THROUGH SCSI (12) defined in | |
15 | * section 12 of that document. | |
16 | * | |
2127e193 GI |
17 | * sat-r09.pdf is the most recent, easily accessible draft prior to the |
18 | * original SAT standard (ANSI INCITS 431-2007). By mid-2009 the second | |
19 | * version of the SAT standard (SAT-2) is nearing standardization. In | |
20 | * their wisdom an incompatible change has been introduced in draft | |
21 | * sat2r08a.pdf in the area of the ATA RETURN DESCRIPTOR. A new "fixed | |
22 | * format" ATA RETURN buffer has been defined (sat2r08b.pdf section | |
23 | * 12.2.7) for the case when DSENSE=0 in the Control mode page. | |
24 | * Unfortunately this is the normal case. If the change stands our | |
25 | * code will need to be extended for this case. | |
26 | * | |
4d59bff9 GG |
27 | * With more transports "hiding" SATA disks (and other S-ATAPI devices) |
28 | * behind a SCSI command set, accessing special features like SMART | |
29 | * information becomes a challenge. The SAT standard offers ATA PASS | |
30 | * THROUGH commands for special usages. Note that the SAT layer may | |
31 | * be inside a generic OS layer (e.g. libata in linux), in a host | |
32 | * adapter (HA or HBA) firmware, or somewhere on the interconnect | |
33 | * between the host computer and the SATA devices (e.g. a RAID made | |
34 | * of SATA disks and the RAID talks "SCSI" to the host computer). | |
35 | * Note that in the latter case, this code does not solve the | |
36 | * addressing issue (i.e. which SATA disk to address behind the logical | |
37 | * SCSI (RAID) interface). | |
ff28b140 | 38 | * |
4d59bff9 GG |
39 | */ |
40 | ||
41 | #include <stdio.h> | |
42 | #include <string.h> | |
54965743 | 43 | #include <stdlib.h> |
2127e193 | 44 | #include <ctype.h> |
cfbba5b9 | 45 | #include <errno.h> |
4d59bff9 GG |
46 | |
47 | #include "config.h" | |
ff28b140 | 48 | |
4d59bff9 | 49 | #include "scsicmds.h" |
2127e193 | 50 | #include "atacmds.h" // ataReadHDIdentity() |
e9583e0c | 51 | #include "knowndrives.h" // lookup_usb_device() |
4d59bff9 | 52 | #include "utility.h" |
2127e193 GI |
53 | #include "dev_interface.h" |
54 | #include "dev_ata_cmd_set.h" // ata_device_with_command_set | |
55 | #include "dev_tunnelled.h" // tunnelled_device<> | |
ff28b140 | 56 | #include "sg_unaligned.h" |
4d59bff9 | 57 | |
ff28b140 | 58 | const char * scsiata_cpp_cvsid = "$Id: scsiata.cpp 4848 2018-12-05 18:30:46Z chrfranke $"; |
4d59bff9 | 59 | |
2127e193 GI |
60 | /* This is a slightly stretched SCSI sense "descriptor" format header. |
61 | The addition is to allow the 0x70 and 0x71 response codes. The idea | |
62 | is to place the salient data of both "fixed" and "descriptor" sense | |
63 | format into one structure to ease application processing. | |
64 | The original sense buffer should be kept around for those cases | |
65 | in which more information is required (e.g. the LBA of a MEDIUM ERROR). */ | |
66 | /// Abridged SCSI sense data | |
67 | struct sg_scsi_sense_hdr { | |
68 | unsigned char response_code; /* permit: 0x0, 0x70, 0x71, 0x72, 0x73 */ | |
69 | unsigned char sense_key; | |
70 | unsigned char asc; | |
71 | unsigned char ascq; | |
72 | unsigned char byte4; | |
73 | unsigned char byte5; | |
74 | unsigned char byte6; | |
75 | unsigned char additional_length; | |
76 | }; | |
77 | ||
78 | /* Maps the salient data from a sense buffer which is in either fixed or | |
79 | descriptor format into a structure mimicking a descriptor format | |
80 | header (i.e. the first 8 bytes of sense descriptor format). | |
81 | If zero response code returns 0. Otherwise returns 1 and if 'sshp' is | |
82 | non-NULL then zero all fields and then set the appropriate fields in | |
83 | that structure. sshp::additional_length is always 0 for response | |
84 | codes 0x70 and 0x71 (fixed format). */ | |
85 | static int sg_scsi_normalize_sense(const unsigned char * sensep, int sb_len, | |
86 | struct sg_scsi_sense_hdr * sshp); | |
87 | ||
2127e193 GI |
88 | #define SAT_ATA_PASSTHROUGH_12LEN 12 |
89 | #define SAT_ATA_PASSTHROUGH_16LEN 16 | |
90 | ||
4d59bff9 | 91 | #define DEF_SAT_ATA_PASSTHRU_SIZE 16 |
1953ff6d | 92 | #define ATA_RETURN_DESCRIPTOR 9 |
4d59bff9 GG |
93 | |
94 | ||
2127e193 GI |
95 | namespace sat { // no need to publish anything, name provided for Doxygen |
96 | ||
97 | /// SAT support. | |
98 | /// Implements ATA by tunnelling through SCSI. | |
99 | ||
100 | class sat_device | |
101 | : public tunnelled_device< | |
102 | /*implements*/ ata_device | |
103 | /*by tunnelling through a*/, scsi_device | |
d008864d GI |
104 | >, |
105 | virtual public /*implements*/ scsi_device | |
2127e193 GI |
106 | { |
107 | public: | |
ff28b140 TL |
108 | enum sat_scsi_mode { |
109 | sat_always, | |
110 | sat_auto, | |
111 | scsi_always | |
112 | }; | |
113 | ||
2127e193 | 114 | sat_device(smart_interface * intf, scsi_device * scsidev, |
ff28b140 | 115 | const char * req_type, sat_scsi_mode mode = sat_always, int passthrulen = 0); |
2127e193 GI |
116 | |
117 | virtual ~sat_device() throw(); | |
118 | ||
d008864d GI |
119 | virtual smart_device * autodetect_open(); |
120 | ||
2127e193 GI |
121 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); |
122 | ||
d008864d GI |
123 | virtual bool scsi_pass_through(scsi_cmnd_io * iop); |
124 | ||
2127e193 GI |
125 | private: |
126 | int m_passthrulen; | |
ff28b140 | 127 | sat_scsi_mode m_mode; |
2127e193 GI |
128 | }; |
129 | ||
130 | ||
131 | sat_device::sat_device(smart_interface * intf, scsi_device * scsidev, | |
ff28b140 TL |
132 | const char * req_type, sat_scsi_mode mode /* = sat_always */, |
133 | int passthrulen /* = 0 */) | |
d008864d | 134 | : smart_device(intf, scsidev->get_dev_name(), |
ff28b140 | 135 | (mode == sat_always ? "sat" : mode == sat_auto ? "sat,auto" : "scsi"), req_type), |
2127e193 | 136 | tunnelled_device<ata_device, scsi_device>(scsidev), |
d008864d | 137 | m_passthrulen(passthrulen), |
ff28b140 | 138 | m_mode(mode) |
2127e193 | 139 | { |
ff28b140 | 140 | if (mode != sat_always) |
d008864d GI |
141 | hide_ata(); // Start as SCSI, switch to ATA in autodetect_open() |
142 | else | |
143 | hide_scsi(); // ATA always | |
ee38a438 GI |
144 | if (strcmp(scsidev->get_dev_type(), "scsi")) |
145 | set_info().dev_type += strprintf("+%s", scsidev->get_dev_type()); | |
d008864d | 146 | |
ff28b140 TL |
147 | set_info().info_name = strprintf("%s [%s]", scsidev->get_info_name(), |
148 | (mode == sat_always ? "SAT" : mode == sat_auto ? "SCSI/SAT" : "SCSI")); | |
2127e193 GI |
149 | } |
150 | ||
151 | sat_device::~sat_device() throw() | |
152 | { | |
153 | } | |
154 | ||
155 | ||
4d59bff9 GG |
156 | // cdb[0]: ATA PASS THROUGH (16) SCSI command opcode byte (0x85) |
157 | // cdb[1]: multiple_count, protocol + extend | |
158 | // cdb[2]: offline, ck_cond, t_dir, byte_block + t_length | |
159 | // cdb[3]: features (15:8) | |
160 | // cdb[4]: features (7:0) | |
161 | // cdb[5]: sector_count (15:8) | |
162 | // cdb[6]: sector_count (7:0) | |
163 | // cdb[7]: lba_low (15:8) | |
164 | // cdb[8]: lba_low (7:0) | |
165 | // cdb[9]: lba_mid (15:8) | |
166 | // cdb[10]: lba_mid (7:0) | |
167 | // cdb[11]: lba_high (15:8) | |
168 | // cdb[12]: lba_high (7:0) | |
169 | // cdb[13]: device | |
170 | // cdb[14]: (ata) command | |
171 | // cdb[15]: control (SCSI, leave as zero) | |
172 | // | |
173 | // 24 bit lba (from MSB): cdb[12] cdb[10] cdb[8] | |
174 | // 48 bit lba (from MSB): cdb[11] cdb[9] cdb[7] cdb[12] cdb[10] cdb[8] | |
175 | // | |
176 | // | |
177 | // cdb[0]: ATA PASS THROUGH (12) SCSI command opcode byte (0xa1) | |
178 | // cdb[1]: multiple_count, protocol + extend | |
179 | // cdb[2]: offline, ck_cond, t_dir, byte_block + t_length | |
180 | // cdb[3]: features (7:0) | |
181 | // cdb[4]: sector_count (7:0) | |
182 | // cdb[5]: lba_low (7:0) | |
183 | // cdb[6]: lba_mid (7:0) | |
184 | // cdb[7]: lba_high (7:0) | |
185 | // cdb[8]: device | |
186 | // cdb[9]: (ata) command | |
187 | // cdb[10]: reserved | |
188 | // cdb[11]: control (SCSI, leave as zero) | |
189 | // | |
190 | // | |
1953ff6d | 191 | // ATA Return Descriptor (component of descriptor sense data) |
4d59bff9 GG |
192 | // des[0]: descriptor code (0x9) |
193 | // des[1]: additional descriptor length (0xc) | |
194 | // des[2]: extend (bit 0) | |
195 | // des[3]: error | |
196 | // des[4]: sector_count (15:8) | |
197 | // des[5]: sector_count (7:0) | |
198 | // des[6]: lba_low (15:8) | |
199 | // des[7]: lba_low (7:0) | |
200 | // des[8]: lba_mid (15:8) | |
201 | // des[9]: lba_mid (7:0) | |
202 | // des[10]: lba_high (15:8) | |
203 | // des[11]: lba_high (7:0) | |
204 | // des[12]: device | |
205 | // des[13]: status | |
a86ec89e GI |
206 | // |
207 | // | |
208 | // ATA registers returned via fixed format sense (allowed >= SAT-2) | |
209 | // fxs[0]: info_valid (bit 7); response_code (6:0) | |
210 | // fxs[1]: (obsolete) | |
211 | // fxs[2]: sense_key (3:0) --> recovered error (formerly 'no sense') | |
212 | // fxs[3]: information (31:24) --> ATA Error register | |
213 | // fxs[4]: information (23:16) --> ATA Status register | |
214 | // fxs[5]: information (15:8) --> ATA Device register | |
215 | // fxs[6]: information (7:0) --> ATA Count (7:0) | |
216 | // fxs[7]: additional sense length [should be >= 10] | |
217 | // fxs[8]: command specific info (31:24) --> Extend (7), count_upper_nonzero | |
218 | // (6), lba_upper_nonzero(5), log_index (3:0) | |
219 | // fxs[9]: command specific info (23:16) --> ATA LBA (7:0) | |
220 | // fxs[10]: command specific info (15:8) --> ATA LBA (15:8) | |
221 | // fxs[11]: command specific info (7:0) --> ATA LBA (23:16) | |
222 | // fxs[12]: additional sense code (asc) --> 0x0 | |
223 | // fxs[13]: additional sense code qualifier (ascq) --> 0x1d | |
224 | // asc,ascq = 0x0,0x1d --> 'ATA pass through information available' | |
4d59bff9 GG |
225 | |
226 | ||
227 | ||
228 | // PURPOSE | |
229 | // This interface routine takes ATA SMART commands and packages | |
230 | // them in the SAT-defined ATA PASS THROUGH SCSI commands. There are | |
231 | // two available SCSI commands: a 12 byte and 16 byte variant; the | |
2127e193 | 232 | // one used is chosen via this->m_passthrulen . |
4d59bff9 GG |
233 | // DETAILED DESCRIPTION OF ARGUMENTS |
234 | // device: is the file descriptor provided by (a SCSI dvice type) open() | |
235 | // command: defines the different ATA operations. | |
236 | // select: additional input data if needed (which log, which type of | |
237 | // self-test). | |
238 | // data: location to write output data, if needed (512 bytes). | |
239 | // Note: not all commands use all arguments. | |
240 | // RETURN VALUES | |
241 | // -1 if the command failed | |
242 | // 0 if the command succeeded, | |
ff28b140 | 243 | // STATUS_CHECK routine: |
4d59bff9 GG |
244 | // -1 if the command failed |
245 | // 0 if the command succeeded and disk SMART status is "OK" | |
246 | // 1 if the command succeeded and disk SMART status is "FAILING" | |
247 | ||
2127e193 | 248 | bool sat_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) |
4d59bff9 | 249 | { |
f9e10201 JD |
250 | if (!ata_cmd_is_supported(in, |
251 | ata_device::supports_data_out | | |
252 | ata_device::supports_output_regs | | |
253 | ata_device::supports_multi_sector | | |
254 | ata_device::supports_48bit, | |
255 | "SAT") | |
256 | ) | |
257 | return false; | |
2127e193 | 258 | |
4d59bff9 GG |
259 | struct scsi_cmnd_io io_hdr; |
260 | struct scsi_sense_disect sinfo; | |
261 | struct sg_scsi_sense_hdr ssh; | |
262 | unsigned char cdb[SAT_ATA_PASSTHROUGH_16LEN]; | |
263 | unsigned char sense[32]; | |
1953ff6d | 264 | const unsigned char * ardp; |
a86ec89e | 265 | int ard_len, have_sense; |
4d59bff9 | 266 | int extend = 0; |
1953ff6d | 267 | int ck_cond = 0; /* set to 1 to read register(s) back */ |
4d59bff9 GG |
268 | int protocol = 3; /* non-data */ |
269 | int t_dir = 1; /* 0 -> to device, 1 -> from device */ | |
270 | int byte_block = 1; /* 0 -> bytes, 1 -> 512 byte blocks */ | |
271 | int t_length = 0; /* 0 -> no data transferred */ | |
4d59bff9 | 272 | int passthru_size = DEF_SAT_ATA_PASSTHRU_SIZE; |
a86ec89e | 273 | bool sense_descriptor = true; |
4d59bff9 GG |
274 | |
275 | memset(cdb, 0, sizeof(cdb)); | |
276 | memset(sense, 0, sizeof(sense)); | |
277 | ||
2127e193 GI |
278 | // Set data direction |
279 | // TODO: This works only for commands where sector_count holds count! | |
280 | switch (in.direction) { | |
281 | case ata_cmd_in::no_data: | |
4d59bff9 | 282 | break; |
2127e193 GI |
283 | case ata_cmd_in::data_in: |
284 | protocol = 4; // PIO data-in | |
285 | t_length = 2; // sector_count holds count | |
4d59bff9 | 286 | break; |
2127e193 GI |
287 | case ata_cmd_in::data_out: |
288 | protocol = 5; // PIO data-out | |
289 | t_length = 2; // sector_count holds count | |
290 | t_dir = 0; // to device | |
4d59bff9 | 291 | break; |
2127e193 GI |
292 | default: |
293 | return set_err(EINVAL, "sat_device::ata_pass_through: invalid direction=%d", | |
294 | (int)in.direction); | |
4d59bff9 | 295 | } |
2127e193 GI |
296 | |
297 | // Check condition if any output register needed | |
298 | if (in.out_needed.is_set()) | |
299 | ck_cond = 1; | |
300 | ||
301 | if ((SAT_ATA_PASSTHROUGH_12LEN == m_passthrulen) || | |
302 | (SAT_ATA_PASSTHROUGH_16LEN == m_passthrulen)) | |
303 | passthru_size = m_passthrulen; | |
304 | ||
305 | // Set extend bit on 48-bit ATA command | |
306 | if (in.in_regs.is_48bit_cmd()) { | |
307 | if (passthru_size != SAT_ATA_PASSTHROUGH_16LEN) | |
308 | return set_err(ENOSYS, "48-bit ATA commands require SAT ATA PASS-THROUGH (16)"); | |
309 | extend = 1; | |
4d59bff9 GG |
310 | } |
311 | ||
4d59bff9 GG |
312 | cdb[0] = (SAT_ATA_PASSTHROUGH_12LEN == passthru_size) ? |
313 | SAT_ATA_PASSTHROUGH_12 : SAT_ATA_PASSTHROUGH_16; | |
314 | ||
315 | cdb[1] = (protocol << 1) | extend; | |
1953ff6d | 316 | cdb[2] = (ck_cond << 5) | (t_dir << 3) | |
4d59bff9 | 317 | (byte_block << 2) | t_length; |
2127e193 GI |
318 | |
319 | if (passthru_size == SAT_ATA_PASSTHROUGH_12LEN) { | |
320 | // ATA PASS-THROUGH (12) | |
321 | const ata_in_regs & lo = in.in_regs; | |
322 | cdb[3] = lo.features; | |
323 | cdb[4] = lo.sector_count; | |
324 | cdb[5] = lo.lba_low; | |
325 | cdb[6] = lo.lba_mid; | |
326 | cdb[7] = lo.lba_high; | |
327 | cdb[8] = lo.device; | |
328 | cdb[9] = lo.command; | |
329 | } | |
330 | else { | |
331 | // ATA PASS-THROUGH (16) | |
332 | const ata_in_regs & lo = in.in_regs; | |
333 | const ata_in_regs & hi = in.in_regs.prev; | |
334 | // Note: all 'in.in_regs.prev.*' are always zero for 28-bit commands | |
335 | cdb[ 3] = hi.features; | |
336 | cdb[ 4] = lo.features; | |
337 | cdb[ 5] = hi.sector_count; | |
338 | cdb[ 6] = lo.sector_count; | |
339 | cdb[ 7] = hi.lba_low; | |
340 | cdb[ 8] = lo.lba_low; | |
341 | cdb[ 9] = hi.lba_mid; | |
342 | cdb[10] = lo.lba_mid; | |
343 | cdb[11] = hi.lba_high; | |
344 | cdb[12] = lo.lba_high; | |
345 | cdb[13] = lo.device; | |
346 | cdb[14] = lo.command; | |
347 | } | |
4d59bff9 GG |
348 | |
349 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
350 | if (0 == t_length) { | |
351 | io_hdr.dxfer_dir = DXFER_NONE; | |
352 | io_hdr.dxfer_len = 0; | |
353 | } else if (t_dir) { /* from device */ | |
354 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
2127e193 GI |
355 | io_hdr.dxfer_len = in.size; |
356 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
357 | memset(in.buffer, 0, in.size); // prefill with zeroes | |
4d59bff9 GG |
358 | } else { /* to device */ |
359 | io_hdr.dxfer_dir = DXFER_TO_DEVICE; | |
2127e193 GI |
360 | io_hdr.dxfer_len = in.size; |
361 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
4d59bff9 GG |
362 | } |
363 | io_hdr.cmnd = cdb; | |
364 | io_hdr.cmnd_len = passthru_size; | |
365 | io_hdr.sensep = sense; | |
366 | io_hdr.max_sense_len = sizeof(sense); | |
367 | io_hdr.timeout = SCSI_TIMEOUT_DEFAULT; | |
368 | ||
2127e193 GI |
369 | scsi_device * scsidev = get_tunnel_dev(); |
370 | if (!scsidev->scsi_pass_through(&io_hdr)) { | |
cfbba5b9 | 371 | if (scsi_debugmode > 0) |
2127e193 GI |
372 | pout("sat_device::ata_pass_through: scsi_pass_through() failed, " |
373 | "errno=%d [%s]\n", scsidev->get_errno(), scsidev->get_errmsg()); | |
374 | return set_err(scsidev->get_err()); | |
4d59bff9 | 375 | } |
1953ff6d GG |
376 | ardp = NULL; |
377 | ard_len = 0; | |
378 | have_sense = sg_scsi_normalize_sense(io_hdr.sensep, io_hdr.resp_sense_len, | |
379 | &ssh); | |
380 | if (have_sense) { | |
a86ec89e GI |
381 | sense_descriptor = ssh.response_code >= 0x72; |
382 | if (sense_descriptor) { | |
383 | /* look for SAT ATA Return Descriptor */ | |
384 | ardp = sg_scsi_sense_desc_find(io_hdr.sensep, | |
385 | io_hdr.resp_sense_len, | |
386 | ATA_RETURN_DESCRIPTOR); | |
387 | if (ardp) { | |
388 | ard_len = ardp[1] + 2; | |
389 | if (ard_len < 12) | |
390 | ard_len = 12; | |
391 | else if (ard_len > 14) | |
392 | ard_len = 14; | |
393 | } | |
1953ff6d GG |
394 | } |
395 | scsi_do_sense_disect(&io_hdr, &sinfo); | |
a86ec89e GI |
396 | int status = scsiSimpleSenseFilter(&sinfo); |
397 | ||
398 | // Workaround for bogus sense_key in sense data with SAT ATA Return Descriptor | |
399 | if ( status && ck_cond && ardp && ard_len > 13 | |
400 | && (ardp[13] & 0xc1) == 0x40 /* !BSY && DRDY && !ERR */) { | |
401 | if (scsi_debugmode > 0) | |
402 | pout("ATA status (0x%02x) indicates success, ignoring SCSI sense_key\n", | |
403 | ardp[13]); | |
404 | status = 0; | |
405 | } | |
406 | ||
407 | if (0 != status) { /* other than no_sense and recovered_error */ | |
cfbba5b9 | 408 | if (scsi_debugmode > 0) { |
2127e193 | 409 | pout("sat_device::ata_pass_through: scsi error: %s\n", |
1953ff6d | 410 | scsiErrString(status)); |
cfbba5b9 | 411 | if (ardp && (scsi_debugmode > 1)) { |
1953ff6d | 412 | pout("Values from ATA Return Descriptor are:\n"); |
ff28b140 | 413 | dStrHex((const uint8_t *)ardp, ard_len, 1); |
1953ff6d GG |
414 | } |
415 | } | |
2127e193 GI |
416 | if (t_dir && (t_length > 0) && (in.direction == ata_cmd_in::data_in)) |
417 | memset(in.buffer, 0, in.size); | |
418 | return set_err(EIO, "scsi error %s", scsiErrString(status)); | |
1953ff6d GG |
419 | } |
420 | } | |
421 | if (ck_cond) { /* expecting SAT specific sense data */ | |
422 | if (have_sense) { | |
423 | if (ardp) { | |
cfbba5b9 | 424 | if (scsi_debugmode > 1) { |
1953ff6d | 425 | pout("Values from ATA Return Descriptor are:\n"); |
ff28b140 | 426 | dStrHex((const uint8_t *)ardp, ard_len, 1); |
4d59bff9 | 427 | } |
2127e193 GI |
428 | // Set output registers |
429 | ata_out_regs & lo = out.out_regs; | |
430 | lo.error = ardp[ 3]; | |
431 | lo.sector_count = ardp[ 5]; | |
432 | lo.lba_low = ardp[ 7]; | |
433 | lo.lba_mid = ardp[ 9]; | |
434 | lo.lba_high = ardp[11]; | |
435 | lo.device = ardp[12]; | |
436 | lo.status = ardp[13]; | |
437 | if (in.in_regs.is_48bit_cmd()) { | |
438 | ata_out_regs & hi = out.out_regs.prev; | |
439 | hi.sector_count = ardp[ 4]; | |
440 | hi.lba_low = ardp[ 6]; | |
441 | hi.lba_mid = ardp[ 8]; | |
442 | hi.lba_high = ardp[10]; | |
4d59bff9 | 443 | } |
a86ec89e GI |
444 | } else if ((! sense_descriptor) && |
445 | (0 == ssh.asc) && | |
446 | (SCSI_ASCQ_ATA_PASS_THROUGH == ssh.ascq) && | |
447 | (0 != io_hdr.sensep[4] /* Some ATA STATUS bit must be set */)) { | |
448 | /* in SAT-2 and later, ATA registers may be passed back via | |
449 | * fixed format sense data [ref: sat3r07 section 12.2.2.7] */ | |
450 | ata_out_regs & lo = out.out_regs; | |
451 | lo.error = io_hdr.sensep[ 3]; | |
452 | lo.status = io_hdr.sensep[ 4]; | |
453 | lo.device = io_hdr.sensep[ 5]; | |
454 | lo.sector_count = io_hdr.sensep[ 6]; | |
455 | lo.lba_low = io_hdr.sensep[ 9]; | |
456 | lo.lba_mid = io_hdr.sensep[10]; | |
457 | lo.lba_high = io_hdr.sensep[11]; | |
458 | if (in.in_regs.is_48bit_cmd()) { | |
459 | if (0 == (0x60 & io_hdr.sensep[8])) { | |
460 | ata_out_regs & hi = out.out_regs.prev; | |
461 | hi.sector_count = 0; | |
462 | hi.lba_low = 0; | |
463 | hi.lba_mid = 0; | |
464 | hi.lba_high = 0; | |
465 | } else { | |
466 | /* getting the "hi." values when either | |
467 | * count_upper_nonzero or lba_upper_nonzero are set | |
468 | * involves fetching the SCSI ATA PASS-THROUGH | |
469 | * Results log page and decoding the descriptor with | |
ff28b140 | 470 | * the matching log_index field. Painful. */ |
a86ec89e GI |
471 | } |
472 | } | |
4d59bff9 GG |
473 | } |
474 | } | |
a86ec89e | 475 | } else { /* ck_cond == 0 */ |
1953ff6d | 476 | if (have_sense) { |
a86ec89e | 477 | if (((SCSI_SK_NO_SENSE == ssh.sense_key) || |
4d59bff9 GG |
478 | (SCSI_SK_RECOVERED_ERR == ssh.sense_key)) && |
479 | (0 == ssh.asc) && | |
480 | (SCSI_ASCQ_ATA_PASS_THROUGH == ssh.ascq)) { | |
a86ec89e GI |
481 | if (scsi_debugmode > 0) { |
482 | if (sense_descriptor && ardp) { | |
1953ff6d | 483 | pout("Values from ATA Return Descriptor are:\n"); |
ff28b140 | 484 | dStrHex((const uint8_t *)ardp, ard_len, 1); |
a86ec89e GI |
485 | } else if (! sense_descriptor) { |
486 | pout("Values from ATA fixed format sense are:\n"); | |
487 | pout(" Error: 0x%x\n", io_hdr.sensep[3]); | |
488 | pout(" Status: 0x%x\n", io_hdr.sensep[4]); | |
489 | pout(" Device: 0x%x\n", io_hdr.sensep[5]); | |
490 | pout(" Count: 0x%x\n", io_hdr.sensep[6]); | |
4d59bff9 | 491 | } |
4d59bff9 GG |
492 | } |
493 | } | |
a86ec89e | 494 | return set_err(EIO, "SAT command failed"); |
4d59bff9 GG |
495 | } |
496 | } | |
2127e193 | 497 | return true; |
4d59bff9 GG |
498 | } |
499 | ||
d008864d GI |
500 | bool sat_device::scsi_pass_through(scsi_cmnd_io * iop) |
501 | { | |
502 | scsi_device * scsidev = get_tunnel_dev(); | |
503 | if (!scsidev->scsi_pass_through(iop)) { | |
504 | set_err(scsidev->get_err()); | |
505 | return false; | |
506 | } | |
507 | return true; | |
508 | } | |
509 | ||
510 | smart_device * sat_device::autodetect_open() | |
511 | { | |
ff28b140 | 512 | if (!open() || m_mode != sat_auto) |
d008864d GI |
513 | return this; |
514 | ||
515 | scsi_device * scsidev = get_tunnel_dev(); | |
516 | ||
517 | unsigned char inqdata[36] = {0, }; | |
518 | if (scsiStdInquiry(scsidev, inqdata, sizeof(inqdata))) { | |
519 | smart_device::error_info err = scsidev->get_err(); | |
520 | close(); | |
521 | set_err(err.no, "INQUIRY [SAT]: %s", err.msg.c_str()); | |
522 | return this; | |
523 | } | |
524 | ||
525 | // Check for SAT "VENDOR" | |
526 | int inqsize = inqdata[4] + 5; | |
527 | bool sat = (inqsize >= 36 && !memcmp(inqdata + 8, "ATA ", 8)); | |
528 | ||
529 | // Change interface | |
530 | hide_ata(!sat); | |
531 | hide_scsi(sat); | |
532 | ||
533 | set_info().dev_type = (sat ? "sat" : scsidev->get_dev_type()); | |
534 | set_info().info_name = strprintf("%s [%s]", scsidev->get_info_name(), | |
535 | (sat ? "SAT" : "SCSI")); | |
536 | return this; | |
537 | } | |
538 | ||
2127e193 GI |
539 | } // namespace |
540 | ||
541 | ///////////////////////////////////////////////////////////////////////////// | |
542 | ||
4d59bff9 | 543 | /* Attempt an IDENTIFY DEVICE ATA command via SATL when packet_interface |
2127e193 GI |
544 | is false otherwise attempt IDENTIFY PACKET DEVICE. If successful |
545 | return true, else false */ | |
546 | ||
547 | static bool has_sat_pass_through(ata_device * dev, bool packet_interface = false) | |
4d59bff9 | 548 | { |
54965743 | 549 | /* Note: malloc() ensures the read buffer lands on a single |
ff28b140 | 550 | page. This avoids some bugs seen on LSI controllers under |
54965743 GI |
551 | FreeBSD */ |
552 | char *data = (char *)malloc(512); | |
2127e193 GI |
553 | ata_cmd_in in; |
554 | in.in_regs.command = (packet_interface ? ATA_IDENTIFY_PACKET_DEVICE : ATA_IDENTIFY_DEVICE); | |
2127e193 | 555 | in.set_data_in(data, 1); |
54965743 GI |
556 | bool ret = dev->ata_pass_through(in); |
557 | free(data); | |
558 | return ret; | |
4d59bff9 GG |
559 | } |
560 | ||
2127e193 GI |
561 | ///////////////////////////////////////////////////////////////////////////// |
562 | ||
4d59bff9 GG |
563 | /* Next two functions are borrowed from sg_lib.c in the sg3_utils |
564 | package. Same copyrght owner, same license as this file. */ | |
2127e193 GI |
565 | static int sg_scsi_normalize_sense(const unsigned char * sensep, int sb_len, |
566 | struct sg_scsi_sense_hdr * sshp) | |
4d59bff9 GG |
567 | { |
568 | if (sshp) | |
569 | memset(sshp, 0, sizeof(struct sg_scsi_sense_hdr)); | |
570 | if ((NULL == sensep) || (0 == sb_len) || (0x70 != (0x70 & sensep[0]))) | |
571 | return 0; | |
572 | if (sshp) { | |
573 | sshp->response_code = (0x7f & sensep[0]); | |
574 | if (sshp->response_code >= 0x72) { /* descriptor format */ | |
575 | if (sb_len > 1) | |
576 | sshp->sense_key = (0xf & sensep[1]); | |
577 | if (sb_len > 2) | |
578 | sshp->asc = sensep[2]; | |
579 | if (sb_len > 3) | |
580 | sshp->ascq = sensep[3]; | |
581 | if (sb_len > 7) | |
582 | sshp->additional_length = sensep[7]; | |
583 | } else { /* fixed format */ | |
584 | if (sb_len > 2) | |
585 | sshp->sense_key = (0xf & sensep[2]); | |
586 | if (sb_len > 7) { | |
587 | sb_len = (sb_len < (sensep[7] + 8)) ? sb_len : | |
588 | (sensep[7] + 8); | |
589 | if (sb_len > 12) | |
590 | sshp->asc = sensep[12]; | |
591 | if (sb_len > 13) | |
592 | sshp->ascq = sensep[13]; | |
593 | } | |
594 | } | |
595 | } | |
596 | return 1; | |
597 | } | |
598 | ||
2127e193 GI |
599 | ///////////////////////////////////////////////////////////////////////////// |
600 | ||
601 | namespace sat { | |
602 | ||
603 | /// Cypress USB Brigde support. | |
604 | ||
605 | class usbcypress_device | |
606 | : public tunnelled_device< | |
607 | /*implements*/ ata_device_with_command_set | |
608 | /*by tunnelling through a*/, scsi_device | |
609 | > | |
610 | { | |
611 | public: | |
612 | usbcypress_device(smart_interface * intf, scsi_device * scsidev, | |
613 | const char * req_type, unsigned char signature); | |
614 | ||
615 | virtual ~usbcypress_device() throw(); | |
616 | ||
617 | protected: | |
618 | virtual int ata_command_interface(smart_command_set command, int select, char * data); | |
619 | ||
620 | unsigned char m_signature; | |
621 | }; | |
622 | ||
623 | ||
624 | usbcypress_device::usbcypress_device(smart_interface * intf, scsi_device * scsidev, | |
625 | const char * req_type, unsigned char signature) | |
ff28b140 | 626 | : smart_device(intf, scsidev->get_dev_name(), "usbcypress", req_type), |
2127e193 GI |
627 | tunnelled_device<ata_device_with_command_set, scsi_device>(scsidev), |
628 | m_signature(signature) | |
629 | { | |
630 | set_info().info_name = strprintf("%s [USB Cypress]", scsidev->get_info_name()); | |
631 | } | |
632 | ||
633 | usbcypress_device::~usbcypress_device() throw() | |
634 | { | |
635 | } | |
636 | ||
637 | ||
638 | /* see cy7c68300c_8.pdf for more information */ | |
639 | #define USBCYPRESS_PASSTHROUGH_LEN 16 | |
640 | int usbcypress_device::ata_command_interface(smart_command_set command, int select, char *data) | |
641 | { | |
642 | struct scsi_cmnd_io io_hdr; | |
643 | unsigned char cdb[USBCYPRESS_PASSTHROUGH_LEN]; | |
644 | unsigned char sense[32]; | |
645 | int copydata = 0; | |
646 | int outlen = 0; | |
647 | int ck_cond = 0; /* set to 1 to read register(s) back */ | |
2127e193 GI |
648 | int t_dir = 1; /* 0 -> to device, 1 -> from device */ |
649 | int byte_block = 1; /* 0 -> bytes, 1 -> 512 byte blocks */ | |
650 | int t_length = 0; /* 0 -> no data transferred */ | |
651 | int feature = 0; | |
652 | int ata_command = 0; | |
653 | int sector_count = 0; | |
654 | int lba_low = 0; | |
655 | int lba_mid = 0; | |
656 | int lba_high = 0; | |
657 | int passthru_size = USBCYPRESS_PASSTHROUGH_LEN; | |
658 | ||
659 | memset(cdb, 0, sizeof(cdb)); | |
660 | memset(sense, 0, sizeof(sense)); | |
661 | ||
662 | ata_command = ATA_SMART_CMD; | |
663 | switch (command) { | |
664 | case CHECK_POWER_MODE: | |
665 | ata_command = ATA_CHECK_POWER_MODE; | |
666 | ck_cond = 1; | |
667 | copydata = 1; | |
668 | break; | |
669 | case READ_VALUES: /* READ DATA */ | |
670 | feature = ATA_SMART_READ_VALUES; | |
671 | sector_count = 1; /* one (512 byte) block */ | |
2127e193 GI |
672 | t_length = 2; /* sector count holds count */ |
673 | copydata = 512; | |
674 | break; | |
675 | case READ_THRESHOLDS: /* obsolete */ | |
676 | feature = ATA_SMART_READ_THRESHOLDS; | |
677 | sector_count = 1; /* one (512 byte) block */ | |
678 | lba_low = 1; | |
2127e193 GI |
679 | t_length = 2; /* sector count holds count */ |
680 | copydata=512; | |
681 | break; | |
682 | case READ_LOG: | |
683 | feature = ATA_SMART_READ_LOG_SECTOR; | |
684 | sector_count = 1; /* one (512 byte) block */ | |
685 | lba_low = select; | |
2127e193 GI |
686 | t_length = 2; /* sector count holds count */ |
687 | copydata = 512; | |
688 | break; | |
689 | case WRITE_LOG: | |
690 | feature = ATA_SMART_WRITE_LOG_SECTOR; | |
691 | sector_count = 1; /* one (512 byte) block */ | |
692 | lba_low = select; | |
2127e193 GI |
693 | t_length = 2; /* sector count holds count */ |
694 | t_dir = 0; /* to device */ | |
695 | outlen = 512; | |
696 | break; | |
697 | case IDENTIFY: | |
698 | ata_command = ATA_IDENTIFY_DEVICE; | |
699 | sector_count = 1; /* one (512 byte) block */ | |
2127e193 GI |
700 | t_length = 2; /* sector count holds count */ |
701 | copydata = 512; | |
702 | break; | |
703 | case PIDENTIFY: | |
704 | ata_command = ATA_IDENTIFY_PACKET_DEVICE; | |
705 | sector_count = 1; /* one (512 byte) block */ | |
2127e193 GI |
706 | t_length = 2; /* sector count (7:0) holds count */ |
707 | copydata = 512; | |
708 | break; | |
709 | case ENABLE: | |
710 | feature = ATA_SMART_ENABLE; | |
711 | lba_low = 1; | |
712 | break; | |
713 | case DISABLE: | |
714 | feature = ATA_SMART_DISABLE; | |
715 | lba_low = 1; | |
716 | break; | |
717 | case STATUS: | |
718 | // this command only says if SMART is working. It could be | |
719 | // replaced with STATUS_CHECK below. | |
720 | feature = ATA_SMART_STATUS; | |
721 | ck_cond = 1; | |
722 | break; | |
723 | case AUTO_OFFLINE: | |
724 | feature = ATA_SMART_AUTO_OFFLINE; | |
725 | sector_count = select; // YET NOTE - THIS IS A NON-DATA COMMAND!! | |
726 | break; | |
727 | case AUTOSAVE: | |
728 | feature = ATA_SMART_AUTOSAVE; | |
729 | sector_count = select; // YET NOTE - THIS IS A NON-DATA COMMAND!! | |
730 | break; | |
731 | case IMMEDIATE_OFFLINE: | |
732 | feature = ATA_SMART_IMMEDIATE_OFFLINE; | |
733 | lba_low = select; | |
734 | break; | |
735 | case STATUS_CHECK: | |
736 | // This command uses HDIO_DRIVE_TASK and has different syntax than | |
737 | // the other commands. | |
738 | feature = ATA_SMART_STATUS; /* SMART RETURN STATUS */ | |
739 | ck_cond = 1; | |
740 | break; | |
741 | default: | |
742 | pout("Unrecognized command %d in usbcypress_device::ata_command_interface()\n" | |
743 | "Please contact " PACKAGE_BUGREPORT "\n", command); | |
744 | errno=ENOSYS; | |
745 | return -1; | |
746 | } | |
747 | if (ATA_SMART_CMD == ata_command) { | |
748 | lba_mid = 0x4f; | |
749 | lba_high = 0xc2; | |
750 | } | |
751 | ||
752 | cdb[0] = m_signature; // bVSCBSignature : vendor-specific command | |
753 | cdb[1] = 0x24; // bVSCBSubCommand : 0x24 for ATACB | |
754 | cdb[2] = 0x0; | |
755 | if (ata_command == ATA_IDENTIFY_DEVICE || ata_command == ATA_IDENTIFY_PACKET_DEVICE) | |
756 | cdb[2] |= (1<<7); //set IdentifyPacketDevice for these cmds | |
757 | cdb[3] = 0xff - (1<<0) - (1<<6); //features, sector count, lba low, lba med | |
758 | // lba high, command are valid | |
759 | cdb[4] = byte_block; //TransferBlockCount : 512 | |
760 | ||
761 | ||
762 | cdb[6] = feature; | |
763 | cdb[7] = sector_count; | |
764 | cdb[8] = lba_low; | |
765 | cdb[9] = lba_mid; | |
766 | cdb[10] = lba_high; | |
767 | cdb[12] = ata_command; | |
768 | ||
769 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
770 | if (0 == t_length) { | |
771 | io_hdr.dxfer_dir = DXFER_NONE; | |
772 | io_hdr.dxfer_len = 0; | |
773 | } else if (t_dir) { /* from device */ | |
774 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
775 | io_hdr.dxfer_len = copydata; | |
776 | io_hdr.dxferp = (unsigned char *)data; | |
777 | memset(data, 0, copydata); /* prefill with zeroes */ | |
778 | } else { /* to device */ | |
779 | io_hdr.dxfer_dir = DXFER_TO_DEVICE; | |
780 | io_hdr.dxfer_len = outlen; | |
781 | io_hdr.dxferp = (unsigned char *)data; | |
782 | } | |
783 | io_hdr.cmnd = cdb; | |
784 | io_hdr.cmnd_len = passthru_size; | |
785 | io_hdr.sensep = sense; | |
786 | io_hdr.max_sense_len = sizeof(sense); | |
787 | io_hdr.timeout = SCSI_TIMEOUT_DEFAULT; | |
788 | ||
789 | scsi_device * scsidev = get_tunnel_dev(); | |
790 | if (!scsidev->scsi_pass_through(&io_hdr)) { | |
cfbba5b9 | 791 | if (scsi_debugmode > 0) |
2127e193 GI |
792 | pout("usbcypress_device::ata_command_interface: scsi_pass_through() failed, " |
793 | "errno=%d [%s]\n", scsidev->get_errno(), scsidev->get_errmsg()); | |
794 | set_err(scsidev->get_err()); | |
795 | return -1; | |
796 | } | |
797 | ||
798 | // if there is a sense the command failed or the | |
799 | // device doesn't support usbcypress | |
ff28b140 | 800 | if (io_hdr.scsi_status == SCSI_STATUS_CHECK_CONDITION && |
2127e193 GI |
801 | sg_scsi_normalize_sense(io_hdr.sensep, io_hdr.resp_sense_len, NULL)) { |
802 | return -1; | |
803 | } | |
804 | if (ck_cond) { | |
805 | unsigned char ardp[8]; | |
806 | int ard_len = 8; | |
807 | /* XXX this is racy if there other scsi command between | |
808 | * the first usbcypress command and this one | |
809 | */ | |
810 | //pout("If you got strange result, please retry without traffic on the disc\n"); | |
811 | /* we use the same command as before, but we set | |
812 | * * the read taskfile bit, for not executing usbcypress command, | |
813 | * * but reading register selected in srb->cmnd[4] | |
814 | */ | |
815 | cdb[2] = (1<<0); /* ask read taskfile */ | |
816 | memset(sense, 0, sizeof(sense)); | |
817 | ||
ff28b140 | 818 | /* transfer 8 bytes */ |
2127e193 GI |
819 | memset(&io_hdr, 0, sizeof(io_hdr)); |
820 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
821 | io_hdr.dxfer_len = ard_len; | |
822 | io_hdr.dxferp = (unsigned char *)ardp; | |
823 | memset(ardp, 0, ard_len); /* prefill with zeroes */ | |
824 | ||
825 | io_hdr.cmnd = cdb; | |
826 | io_hdr.cmnd_len = passthru_size; | |
827 | io_hdr.sensep = sense; | |
828 | io_hdr.max_sense_len = sizeof(sense); | |
829 | io_hdr.timeout = SCSI_TIMEOUT_DEFAULT; | |
830 | ||
831 | ||
832 | if (!scsidev->scsi_pass_through(&io_hdr)) { | |
cfbba5b9 | 833 | if (scsi_debugmode > 0) |
2127e193 GI |
834 | pout("usbcypress_device::ata_command_interface: scsi_pass_through() failed, " |
835 | "errno=%d [%s]\n", scsidev->get_errno(), scsidev->get_errmsg()); | |
836 | set_err(scsidev->get_err()); | |
837 | return -1; | |
838 | } | |
839 | // if there is a sense the command failed or the | |
840 | // device doesn't support usbcypress | |
ff28b140 | 841 | if (io_hdr.scsi_status == SCSI_STATUS_CHECK_CONDITION && |
2127e193 GI |
842 | sg_scsi_normalize_sense(io_hdr.sensep, io_hdr.resp_sense_len, NULL)) { |
843 | return -1; | |
844 | } | |
845 | ||
846 | ||
cfbba5b9 | 847 | if (scsi_debugmode > 1) { |
2127e193 | 848 | pout("Values from ATA Return Descriptor are:\n"); |
ff28b140 | 849 | dStrHex((const uint8_t *)ardp, ard_len, 1); |
2127e193 GI |
850 | } |
851 | ||
852 | if (ATA_CHECK_POWER_MODE == ata_command) | |
853 | data[0] = ardp[2]; /* sector count (0:7) */ | |
854 | else if (STATUS_CHECK == command) { | |
855 | if ((ardp[4] == 0x4f) && (ardp[5] == 0xc2)) | |
856 | return 0; /* GOOD smart status */ | |
857 | if ((ardp[4] == 0xf4) && (ardp[5] == 0x2c)) | |
858 | return 1; // smart predicting failure, "bad" status | |
859 | // We haven't gotten output that makes sense so | |
860 | // print out some debugging info | |
861 | syserror("Error SMART Status command failed"); | |
862 | pout("This may be due to a race in usbcypress\n"); | |
863 | pout("Retry without other disc access\n"); | |
864 | pout("Please get assistance from " PACKAGE_HOMEPAGE "\n"); | |
865 | pout("Values from ATA Return Descriptor are:\n"); | |
ff28b140 | 866 | dStrHex((const uint8_t *)ardp, ard_len, 1); |
2127e193 GI |
867 | return -1; |
868 | } | |
869 | } | |
870 | return 0; | |
871 | } | |
872 | ||
873 | #if 0 // Not used, see autodetect_sat_device() below. | |
874 | static int isprint_string(const char *s) | |
875 | { | |
876 | while (*s) { | |
877 | if (isprint(*s) == 0) | |
878 | return 0; | |
879 | s++; | |
880 | } | |
881 | return 1; | |
882 | } | |
883 | ||
884 | /* Attempt an IDENTIFY DEVICE ATA or IDENTIFY PACKET DEVICE command | |
885 | If successful return 1, else 0 */ | |
886 | // TODO: Combine with has_sat_pass_through above | |
887 | static int has_usbcypress_pass_through(ata_device * atadev, const char *manufacturer, const char *product) | |
888 | { | |
889 | struct ata_identify_device drive; | |
890 | char model[40], serial[20], firm[8]; | |
891 | ||
892 | /* issue the command and do a checksum if possible */ | |
893 | if (ataReadHDIdentity(atadev, &drive) < 0) | |
894 | return 0; | |
895 | ||
896 | /* check if model string match, revision doesn't work for me */ | |
897 | format_ata_string(model, drive.model, 40); | |
898 | if (*model == 0 || isprint_string(model) == 0) | |
899 | return 0; | |
900 | ||
901 | if (manufacturer && strncmp(manufacturer, model, 8)) | |
902 | pout("manufacturer doesn't match in pass_through test\n"); | |
903 | if (product && | |
904 | strlen(model) > 8 && strncmp(product, model+8, strlen(model)-8)) | |
905 | pout("product doesn't match in pass_through test\n"); | |
906 | ||
907 | /* check serial */ | |
908 | format_ata_string(serial, drive.serial_no, 20); | |
909 | if (isprint_string(serial) == 0) | |
910 | return 0; | |
911 | format_ata_string(firm, drive.fw_rev, 8); | |
912 | if (isprint_string(firm) == 0) | |
913 | return 0; | |
914 | return 1; | |
915 | } | |
916 | #endif | |
917 | ||
918 | ///////////////////////////////////////////////////////////////////////////// | |
919 | ||
920 | /// JMicron USB Bridge support. | |
921 | ||
922 | class usbjmicron_device | |
923 | : public tunnelled_device< | |
924 | /*implements*/ ata_device, | |
925 | /*by tunnelling through a*/ scsi_device | |
926 | > | |
927 | { | |
928 | public: | |
929 | usbjmicron_device(smart_interface * intf, scsi_device * scsidev, | |
ee38a438 GI |
930 | const char * req_type, bool prolific, |
931 | bool ata_48bit_support, int port); | |
2127e193 GI |
932 | |
933 | virtual ~usbjmicron_device() throw(); | |
934 | ||
935 | virtual bool open(); | |
936 | ||
937 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); | |
938 | ||
939 | private: | |
940 | bool get_registers(unsigned short addr, unsigned char * buf, unsigned short size); | |
941 | ||
ee38a438 | 942 | bool m_prolific; |
2127e193 GI |
943 | bool m_ata_48bit_support; |
944 | int m_port; | |
945 | }; | |
946 | ||
947 | ||
948 | usbjmicron_device::usbjmicron_device(smart_interface * intf, scsi_device * scsidev, | |
ee38a438 GI |
949 | const char * req_type, bool prolific, |
950 | bool ata_48bit_support, int port) | |
2127e193 GI |
951 | : smart_device(intf, scsidev->get_dev_name(), "usbjmicron", req_type), |
952 | tunnelled_device<ata_device, scsi_device>(scsidev), | |
ee38a438 GI |
953 | m_prolific(prolific), m_ata_48bit_support(ata_48bit_support), |
954 | m_port(port >= 0 || !prolific ? port : 0) | |
2127e193 GI |
955 | { |
956 | set_info().info_name = strprintf("%s [USB JMicron]", scsidev->get_info_name()); | |
957 | } | |
958 | ||
959 | usbjmicron_device::~usbjmicron_device() throw() | |
960 | { | |
961 | } | |
962 | ||
963 | ||
964 | bool usbjmicron_device::open() | |
965 | { | |
966 | // Open USB first | |
967 | if (!tunnelled_device<ata_device, scsi_device>::open()) | |
968 | return false; | |
969 | ||
970 | // Detect port if not specified | |
971 | if (m_port < 0) { | |
972 | unsigned char regbuf[1] = {0}; | |
973 | if (!get_registers(0x720f, regbuf, sizeof(regbuf))) { | |
974 | close(); | |
975 | return false; | |
976 | } | |
977 | ||
978 | switch (regbuf[0] & 0x44) { | |
979 | case 0x04: | |
980 | m_port = 0; break; | |
981 | case 0x40: | |
982 | m_port = 1; break; | |
983 | case 0x44: | |
984 | close(); | |
985 | return set_err(EINVAL, "Two devices connected, try '-d usbjmicron,[01]'"); | |
986 | default: | |
987 | close(); | |
988 | return set_err(ENODEV, "No device connected"); | |
989 | } | |
990 | } | |
991 | ||
992 | return true; | |
993 | } | |
994 | ||
995 | ||
996 | bool usbjmicron_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) | |
997 | { | |
ee38a438 GI |
998 | if (!ata_cmd_is_supported(in, |
999 | ata_device::supports_data_out | | |
1000 | ata_device::supports_smart_status | | |
1001 | (m_ata_48bit_support ? ata_device::supports_48bit_hi_null : 0), | |
1002 | "JMicron") | |
2127e193 GI |
1003 | ) |
1004 | return false; | |
1005 | ||
2127e193 GI |
1006 | if (m_port < 0) |
1007 | return set_err(EIO, "Unknown JMicron port"); | |
1008 | ||
1009 | scsi_cmnd_io io_hdr; | |
1010 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1011 | ||
1012 | bool rwbit = true; | |
d2e702cf | 1013 | unsigned char smart_status = 0xff; |
2127e193 | 1014 | |
ee38a438 GI |
1015 | bool is_smart_status = ( in.in_regs.command == ATA_SMART_CMD |
1016 | && in.in_regs.features == ATA_SMART_STATUS); | |
1017 | ||
2127e193 GI |
1018 | if (is_smart_status && in.out_needed.is_set()) { |
1019 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
1020 | io_hdr.dxfer_len = 1; | |
1021 | io_hdr.dxferp = &smart_status; | |
1022 | } | |
1023 | else switch (in.direction) { | |
1024 | case ata_cmd_in::no_data: | |
1025 | io_hdr.dxfer_dir = DXFER_NONE; | |
1026 | break; | |
1027 | case ata_cmd_in::data_in: | |
1028 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
1029 | io_hdr.dxfer_len = in.size; | |
1030 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
1031 | memset(in.buffer, 0, in.size); | |
1032 | break; | |
1033 | case ata_cmd_in::data_out: | |
1034 | io_hdr.dxfer_dir = DXFER_TO_DEVICE; | |
1035 | io_hdr.dxfer_len = in.size; | |
1036 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
1037 | rwbit = false; | |
1038 | break; | |
1039 | default: | |
1040 | return set_err(EINVAL); | |
1041 | } | |
1042 | ||
1043 | // Build pass through command | |
ee38a438 | 1044 | unsigned char cdb[14]; |
2127e193 GI |
1045 | cdb[ 0] = 0xdf; |
1046 | cdb[ 1] = (rwbit ? 0x10 : 0x00); | |
1047 | cdb[ 2] = 0x00; | |
ff28b140 | 1048 | sg_put_unaligned_be16(io_hdr.dxfer_len, cdb + 3); |
2127e193 GI |
1049 | cdb[ 5] = in.in_regs.features; |
1050 | cdb[ 6] = in.in_regs.sector_count; | |
1051 | cdb[ 7] = in.in_regs.lba_low; | |
1052 | cdb[ 8] = in.in_regs.lba_mid; | |
1053 | cdb[ 9] = in.in_regs.lba_high; | |
1054 | cdb[10] = in.in_regs.device | (m_port == 0 ? 0xa0 : 0xb0); | |
1055 | cdb[11] = in.in_regs.command; | |
ee38a438 GI |
1056 | // Prolific PL3507 |
1057 | cdb[12] = 0x06; | |
1058 | cdb[13] = 0x7b; | |
2127e193 GI |
1059 | |
1060 | io_hdr.cmnd = cdb; | |
ee38a438 | 1061 | io_hdr.cmnd_len = (!m_prolific ? 12 : 14); |
2127e193 GI |
1062 | |
1063 | scsi_device * scsidev = get_tunnel_dev(); | |
ff28b140 | 1064 | if (!scsidev->scsi_pass_through_and_check(&io_hdr, |
2127e193 GI |
1065 | "usbjmicron_device::ata_pass_through: ")) |
1066 | return set_err(scsidev->get_err()); | |
1067 | ||
1068 | if (in.out_needed.is_set()) { | |
1069 | if (is_smart_status) { | |
d2e702cf GI |
1070 | if (io_hdr.resid == 1) |
1071 | // Some (Prolific) USB bridges do not transfer a status byte | |
1072 | return set_err(ENOSYS, "Incomplete response, status byte missing [JMicron]"); | |
1073 | ||
2127e193 | 1074 | switch (smart_status) { |
d2e702cf | 1075 | case 0xc2: |
2127e193 GI |
1076 | out.out_regs.lba_high = 0xc2; |
1077 | out.out_regs.lba_mid = 0x4f; | |
1078 | break; | |
d2e702cf | 1079 | case 0x2c: |
2127e193 GI |
1080 | out.out_regs.lba_high = 0x2c; |
1081 | out.out_regs.lba_mid = 0xf4; | |
1082 | break; | |
d2e702cf GI |
1083 | default: |
1084 | // Some (JM20336) USB bridges always return 0x01, regardless of SMART Status | |
1085 | return set_err(ENOSYS, "Invalid status byte (0x%02x) [JMicron]", smart_status); | |
2127e193 GI |
1086 | } |
1087 | } | |
1088 | ||
1089 | #if 0 // Not needed for SMART STATUS, see also notes below | |
1090 | else { | |
1091 | // Read ATA output registers | |
1092 | // NOTE: The register addresses are not valid for some older chip revisions | |
1093 | // NOTE: There is a small race condition here! | |
1094 | unsigned char regbuf[16] = {0, }; | |
1095 | if (!get_registers((m_port == 0 ? 0x8000 : 0x9000), regbuf, sizeof(regbuf))) | |
1096 | return false; | |
1097 | ||
1098 | out.out_regs.sector_count = regbuf[ 0]; | |
1099 | out.out_regs.lba_mid = regbuf[ 4]; | |
1100 | out.out_regs.lba_low = regbuf[ 6]; | |
1101 | out.out_regs.device = regbuf[ 9]; | |
1102 | out.out_regs.lba_high = regbuf[10]; | |
1103 | out.out_regs.error = regbuf[13]; | |
1104 | out.out_regs.status = regbuf[14]; | |
1105 | } | |
1106 | #endif | |
1107 | } | |
1108 | ||
1109 | return true; | |
1110 | } | |
1111 | ||
1112 | bool usbjmicron_device::get_registers(unsigned short addr, | |
1113 | unsigned char * buf, unsigned short size) | |
1114 | { | |
ee38a438 | 1115 | unsigned char cdb[14]; |
2127e193 GI |
1116 | cdb[ 0] = 0xdf; |
1117 | cdb[ 1] = 0x10; | |
1118 | cdb[ 2] = 0x00; | |
ff28b140 | 1119 | sg_put_unaligned_be16(size, cdb + 3); |
2127e193 | 1120 | cdb[ 5] = 0x00; |
ff28b140 | 1121 | sg_put_unaligned_be16(addr, cdb + 6); |
2127e193 GI |
1122 | cdb[ 8] = 0x00; |
1123 | cdb[ 9] = 0x00; | |
1124 | cdb[10] = 0x00; | |
1125 | cdb[11] = 0xfd; | |
ee38a438 GI |
1126 | // Prolific PL3507 |
1127 | cdb[12] = 0x06; | |
1128 | cdb[13] = 0x7b; | |
2127e193 GI |
1129 | |
1130 | scsi_cmnd_io io_hdr; | |
1131 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1132 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
1133 | io_hdr.dxfer_len = size; | |
1134 | io_hdr.dxferp = buf; | |
1135 | io_hdr.cmnd = cdb; | |
ee38a438 | 1136 | io_hdr.cmnd_len = (!m_prolific ? 12 : 14); |
2127e193 GI |
1137 | |
1138 | scsi_device * scsidev = get_tunnel_dev(); | |
ff28b140 | 1139 | if (!scsidev->scsi_pass_through_and_check(&io_hdr, |
2127e193 GI |
1140 | "usbjmicron_device::get_registers: ")) |
1141 | return set_err(scsidev->get_err()); | |
1142 | ||
1143 | return true; | |
1144 | } | |
1145 | ||
1146 | ||
a86ec89e GI |
1147 | ///////////////////////////////////////////////////////////////////////////// |
1148 | ||
1149 | /// Prolific USB Bridge support. (PL2773) (Probably works on PL2771 also...) | |
1150 | ||
1151 | class usbprolific_device | |
1152 | : public tunnelled_device< | |
1153 | /*implements*/ ata_device, | |
1154 | /*by tunnelling through a*/ scsi_device | |
1155 | > | |
1156 | { | |
1157 | public: | |
1158 | usbprolific_device(smart_interface * intf, scsi_device * scsidev, | |
1159 | const char * req_type); | |
1160 | ||
1161 | virtual ~usbprolific_device() throw(); | |
1162 | ||
1163 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); | |
1164 | }; | |
1165 | ||
1166 | ||
1167 | usbprolific_device::usbprolific_device(smart_interface * intf, scsi_device * scsidev, | |
1168 | const char * req_type) | |
1169 | : smart_device(intf, scsidev->get_dev_name(), "usbprolific", req_type), | |
1170 | tunnelled_device<ata_device, scsi_device>(scsidev) | |
1171 | { | |
1172 | set_info().info_name = strprintf("%s [USB Prolific]", scsidev->get_info_name()); | |
1173 | } | |
1174 | ||
1175 | usbprolific_device::~usbprolific_device() throw() | |
1176 | { | |
1177 | } | |
1178 | ||
1179 | bool usbprolific_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) | |
1180 | { | |
1181 | if (!ata_cmd_is_supported(in, | |
1182 | ata_device::supports_data_out | | |
1183 | ata_device::supports_48bit_hi_null | | |
1184 | ata_device::supports_output_regs | | |
1185 | ata_device::supports_smart_status, | |
1186 | "Prolific" ) | |
1187 | ) | |
1188 | return false; | |
1189 | ||
1190 | scsi_cmnd_io io_hdr; | |
1191 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1192 | unsigned char cmd_rw = 0x10; // Read | |
1193 | ||
1194 | switch (in.direction) { | |
1195 | case ata_cmd_in::no_data: | |
1196 | io_hdr.dxfer_dir = DXFER_NONE; | |
1197 | break; | |
1198 | case ata_cmd_in::data_in: | |
1199 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
1200 | io_hdr.dxfer_len = in.size; | |
1201 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
1202 | memset(in.buffer, 0, in.size); | |
1203 | break; | |
1204 | case ata_cmd_in::data_out: | |
1205 | io_hdr.dxfer_dir = DXFER_TO_DEVICE; | |
1206 | io_hdr.dxfer_len = in.size; | |
1207 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
1208 | cmd_rw = 0x0; // Write | |
1209 | break; | |
1210 | default: | |
1211 | return set_err(EINVAL); | |
1212 | } | |
1213 | ||
1214 | // Based on reverse engineering of iSmart.exe with API Monitor. | |
1215 | // Seen commands: | |
1216 | // D0 0 0 0 06 7B 0 0 0 0 0 0 // Read Firmware info?, reads 16 bytes | |
1217 | // F4 0 0 0 06 7B // ?? | |
1218 | // D8 15 0 D8 06 7B 0 0 0 0 1 1 4F C2 A0 B0 // SMART Enable | |
1219 | // D8 15 0 D0 06 7B 0 0 2 0 1 1 4F C2 A0 B0 // SMART Read values | |
1220 | // D8 15 0 D1 06 7B 0 0 2 0 1 1 4F C2 A0 B0 // SMART Read thresholds | |
1221 | // D8 15 0 D4 06 7B 0 0 0 0 0 1 4F C2 A0 B0 // SMART Execute self test | |
1222 | // D7 0 0 0 06 7B 0 0 0 0 0 0 0 0 0 0 // Read status registers, Reads 16 bytes of data | |
1223 | // Additional DATA OUT support based on document from Prolific | |
1224 | ||
1225 | // Build pass through command | |
1226 | unsigned char cdb[16]; | |
1227 | cdb[ 0] = 0xD8; // Operation Code (D8 = Prolific ATA pass through) | |
1228 | cdb[ 1] = cmd_rw|0x5; // Read(0x10)/Write(0x0) | NORMAL(0x5)/PREFIX(0x0)(?) | |
1229 | cdb[ 2] = 0x0; // Reserved | |
1230 | cdb[ 3] = in.in_regs.features; // Feature register (SMART command) | |
1231 | cdb[ 4] = 0x06; // Check Word (VendorID magic, Prolific: 0x067B) | |
1232 | cdb[ 5] = 0x7B; // Check Word (VendorID magic, Prolific: 0x067B) | |
ff28b140 | 1233 | sg_put_unaligned_be32(io_hdr.dxfer_len, cdb + 6); |
a86ec89e GI |
1234 | cdb[10] = in.in_regs.sector_count; // Sector Count |
1235 | cdb[11] = in.in_regs.lba_low; // LBA Low (7:0) | |
1236 | cdb[12] = in.in_regs.lba_mid; // LBA Mid (15:8) | |
1237 | cdb[13] = in.in_regs.lba_high; // LBA High (23:16) | |
1238 | cdb[14] = in.in_regs.device | 0xA0; // Device/Head | |
1239 | cdb[15] = in.in_regs.command; // ATA Command Register (only PIO supported) | |
1240 | // Use '-r scsiioctl,1' to print CDB for debug purposes | |
1241 | ||
1242 | io_hdr.cmnd = cdb; | |
1243 | io_hdr.cmnd_len = 16; | |
1244 | ||
1245 | scsi_device * scsidev = get_tunnel_dev(); | |
ff28b140 | 1246 | if (!scsidev->scsi_pass_through_and_check(&io_hdr, |
a86ec89e GI |
1247 | "usbprolific_device::ata_pass_through: ")) |
1248 | return set_err(scsidev->get_err()); | |
1249 | ||
1250 | if (in.out_needed.is_set()) { | |
1251 | // Read ATA output registers | |
1252 | unsigned char regbuf[16] = {0, }; | |
1253 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1254 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
1255 | io_hdr.dxfer_len = sizeof(regbuf); | |
1256 | io_hdr.dxferp = regbuf; | |
1257 | ||
1258 | memset(cdb, 0, sizeof(cdb)); | |
1259 | cdb[ 0] = 0xD7; // Prolific read registers | |
1260 | cdb[ 4] = 0x06; // Check Word (VendorID magic, Prolific: 0x067B) | |
1261 | cdb[ 5] = 0x7B; // Check Word (VendorID magic, Prolific: 0x067B) | |
1262 | io_hdr.cmnd = cdb; | |
1263 | io_hdr.cmnd_len = sizeof(cdb); | |
1264 | ||
ff28b140 | 1265 | if (!scsidev->scsi_pass_through_and_check(&io_hdr, |
a86ec89e GI |
1266 | "usbprolific_device::scsi_pass_through (get registers): ")) |
1267 | return set_err(scsidev->get_err()); | |
1268 | ||
1269 | // Use '-r scsiioctl,2' to print input registers for debug purposes | |
1270 | // Example: 50 00 00 00 00 01 4f 00 c2 00 a0 da 00 b0 00 50 | |
1271 | out.out_regs.status = regbuf[0]; // Status | |
1272 | out.out_regs.error = regbuf[1]; // Error | |
1273 | out.out_regs.sector_count = regbuf[2]; // Sector Count (7:0) | |
1274 | out.out_regs.lba_low = regbuf[4]; // LBA Low (7:0) | |
1275 | out.out_regs.lba_mid = regbuf[6]; // LBA Mid (7:0) | |
1276 | out.out_regs.lba_high = regbuf[8]; // LBA High (7:0) | |
1277 | out.out_regs.device = regbuf[10]; // Device/Head | |
1278 | // = regbuf[11]; // ATA Feature (7:0) | |
1279 | // = regbuf[13]; // ATA Command | |
1280 | } | |
1281 | ||
1282 | return true; | |
1283 | } | |
1284 | ||
1285 | ||
2127e193 GI |
1286 | ///////////////////////////////////////////////////////////////////////////// |
1287 | ||
1288 | /// SunplusIT USB Bridge support. | |
1289 | ||
1290 | class usbsunplus_device | |
1291 | : public tunnelled_device< | |
1292 | /*implements*/ ata_device, | |
1293 | /*by tunnelling through a*/ scsi_device | |
1294 | > | |
1295 | { | |
1296 | public: | |
1297 | usbsunplus_device(smart_interface * intf, scsi_device * scsidev, | |
1298 | const char * req_type); | |
1299 | ||
1300 | virtual ~usbsunplus_device() throw(); | |
1301 | ||
1302 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); | |
1303 | }; | |
1304 | ||
1305 | ||
1306 | usbsunplus_device::usbsunplus_device(smart_interface * intf, scsi_device * scsidev, | |
1307 | const char * req_type) | |
1308 | : smart_device(intf, scsidev->get_dev_name(), "usbsunplus", req_type), | |
1309 | tunnelled_device<ata_device, scsi_device>(scsidev) | |
1310 | { | |
1311 | set_info().info_name = strprintf("%s [USB Sunplus]", scsidev->get_info_name()); | |
1312 | } | |
1313 | ||
1314 | usbsunplus_device::~usbsunplus_device() throw() | |
1315 | { | |
1316 | } | |
1317 | ||
1318 | bool usbsunplus_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) | |
1319 | { | |
ee38a438 GI |
1320 | if (!ata_cmd_is_supported(in, |
1321 | ata_device::supports_data_out | | |
1322 | ata_device::supports_output_regs | | |
1323 | ata_device::supports_48bit, | |
1324 | "Sunplus") | |
2127e193 GI |
1325 | ) |
1326 | return false; | |
1327 | ||
1328 | scsi_cmnd_io io_hdr; | |
1329 | unsigned char cdb[12]; | |
1330 | ||
1331 | if (in.in_regs.is_48bit_cmd()) { | |
1332 | // Set "previous" registers | |
1333 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1334 | io_hdr.dxfer_dir = DXFER_NONE; | |
1335 | ||
1336 | cdb[ 0] = 0xf8; | |
1337 | cdb[ 1] = 0x00; | |
1338 | cdb[ 2] = 0x23; // Subcommand: Pass through presetting | |
1339 | cdb[ 3] = 0x00; | |
1340 | cdb[ 4] = 0x00; | |
1341 | cdb[ 5] = in.in_regs.prev.features; | |
1342 | cdb[ 6] = in.in_regs.prev.sector_count; | |
1343 | cdb[ 7] = in.in_regs.prev.lba_low; | |
1344 | cdb[ 8] = in.in_regs.prev.lba_mid; | |
1345 | cdb[ 9] = in.in_regs.prev.lba_high; | |
1346 | cdb[10] = 0x00; | |
1347 | cdb[11] = 0x00; | |
1348 | ||
1349 | io_hdr.cmnd = cdb; | |
1350 | io_hdr.cmnd_len = sizeof(cdb); | |
1351 | ||
1352 | scsi_device * scsidev = get_tunnel_dev(); | |
ff28b140 | 1353 | if (!scsidev->scsi_pass_through_and_check(&io_hdr, |
2127e193 GI |
1354 | "usbsunplus_device::scsi_pass_through (presetting): ")) |
1355 | return set_err(scsidev->get_err()); | |
1356 | } | |
1357 | ||
1358 | // Run Pass through command | |
1359 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1360 | unsigned char protocol; | |
1361 | switch (in.direction) { | |
1362 | case ata_cmd_in::no_data: | |
1363 | io_hdr.dxfer_dir = DXFER_NONE; | |
1364 | protocol = 0x00; | |
1365 | break; | |
1366 | case ata_cmd_in::data_in: | |
1367 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
1368 | io_hdr.dxfer_len = in.size; | |
1369 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
1370 | memset(in.buffer, 0, in.size); | |
1371 | protocol = 0x10; | |
1372 | break; | |
1373 | case ata_cmd_in::data_out: | |
1374 | io_hdr.dxfer_dir = DXFER_TO_DEVICE; | |
1375 | io_hdr.dxfer_len = in.size; | |
1376 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
1377 | protocol = 0x11; | |
1378 | break; | |
1379 | default: | |
1380 | return set_err(EINVAL); | |
1381 | } | |
1382 | ||
1383 | cdb[ 0] = 0xf8; | |
1384 | cdb[ 1] = 0x00; | |
1385 | cdb[ 2] = 0x22; // Subcommand: Pass through | |
1386 | cdb[ 3] = protocol; | |
1387 | cdb[ 4] = (unsigned char)(io_hdr.dxfer_len >> 9); | |
1388 | cdb[ 5] = in.in_regs.features; | |
1389 | cdb[ 6] = in.in_regs.sector_count; | |
1390 | cdb[ 7] = in.in_regs.lba_low; | |
1391 | cdb[ 8] = in.in_regs.lba_mid; | |
1392 | cdb[ 9] = in.in_regs.lba_high; | |
1393 | cdb[10] = in.in_regs.device | 0xa0; | |
1394 | cdb[11] = in.in_regs.command; | |
1395 | ||
1396 | io_hdr.cmnd = cdb; | |
1397 | io_hdr.cmnd_len = sizeof(cdb); | |
1398 | ||
1399 | scsi_device * scsidev = get_tunnel_dev(); | |
ff28b140 | 1400 | if (!scsidev->scsi_pass_through_and_check(&io_hdr, |
2127e193 GI |
1401 | "usbsunplus_device::scsi_pass_through: ")) |
1402 | // Returns sense key 0x03 (medium error) on ATA command error | |
1403 | return set_err(scsidev->get_err()); | |
1404 | ||
1405 | if (in.out_needed.is_set()) { | |
1406 | // Read ATA output registers | |
1407 | unsigned char regbuf[8] = {0, }; | |
1408 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1409 | io_hdr.dxfer_dir = DXFER_FROM_DEVICE; | |
1410 | io_hdr.dxfer_len = sizeof(regbuf); | |
1411 | io_hdr.dxferp = regbuf; | |
1412 | ||
1413 | cdb[ 0] = 0xf8; | |
1414 | cdb[ 1] = 0x00; | |
1415 | cdb[ 2] = 0x21; // Subcommand: Get status | |
1416 | memset(cdb+3, 0, sizeof(cdb)-3); | |
1417 | io_hdr.cmnd = cdb; | |
1418 | io_hdr.cmnd_len = sizeof(cdb); | |
1419 | ||
ff28b140 | 1420 | if (!scsidev->scsi_pass_through_and_check(&io_hdr, |
2127e193 GI |
1421 | "usbsunplus_device::scsi_pass_through (get registers): ")) |
1422 | return set_err(scsidev->get_err()); | |
1423 | ||
1424 | out.out_regs.error = regbuf[1]; | |
1425 | out.out_regs.sector_count = regbuf[2]; | |
1426 | out.out_regs.lba_low = regbuf[3]; | |
1427 | out.out_regs.lba_mid = regbuf[4]; | |
1428 | out.out_regs.lba_high = regbuf[5]; | |
1429 | out.out_regs.device = regbuf[6]; | |
1430 | out.out_regs.status = regbuf[7]; | |
1431 | } | |
1432 | ||
1433 | return true; | |
1434 | } | |
1435 | ||
1436 | ||
1437 | } // namespace | |
1438 | ||
1439 | using namespace sat; | |
1440 | ||
1441 | ||
1442 | ///////////////////////////////////////////////////////////////////////////// | |
1443 | ||
1444 | // Return ATA->SCSI filter for SAT or USB. | |
1445 | ||
1446 | ata_device * smart_interface::get_sat_device(const char * type, scsi_device * scsidev) | |
1447 | { | |
a86ec89e GI |
1448 | if (!scsidev) |
1449 | throw std::logic_error("smart_interface: get_sat_device() called with scsidev=0"); | |
1450 | ||
1451 | // Take temporary ownership of 'scsidev' to delete it on error | |
1452 | scsi_device_auto_ptr scsidev_holder(scsidev); | |
1453 | ata_device * satdev = 0; | |
1454 | ||
2127e193 | 1455 | if (!strncmp(type, "sat", 3)) { |
d008864d | 1456 | const char * t = type + 3; |
ff28b140 | 1457 | sat_device::sat_scsi_mode mode = sat_device::sat_always; |
d008864d GI |
1458 | if (!strncmp(t, ",auto", 5)) { |
1459 | t += 5; | |
ff28b140 | 1460 | mode = sat_device::sat_auto; |
d008864d GI |
1461 | } |
1462 | int ptlen = 0, n = -1; | |
1463 | if (*t && !(sscanf(t, ",%d%n", &ptlen, &n) == 1 && n == (int)strlen(t) | |
1464 | && (ptlen == 0 || ptlen == 12 || ptlen == 16))) { | |
1465 | set_err(EINVAL, "Option '-d sat[,auto][,N]' requires N to be 0, 12 or 16"); | |
2127e193 GI |
1466 | return 0; |
1467 | } | |
ff28b140 TL |
1468 | satdev = new sat_device(this, scsidev, type, mode, ptlen); |
1469 | } | |
1470 | ||
1471 | else if (!strcmp(type, "scsi")) { | |
1472 | satdev = new sat_device(this, scsidev, type, sat_device::scsi_always); | |
2127e193 GI |
1473 | } |
1474 | ||
1475 | else if (!strncmp(type, "usbcypress", 10)) { | |
1476 | unsigned signature = 0x24; int n1 = -1, n2 = -1; | |
1477 | if (!(((sscanf(type, "usbcypress%n,0x%x%n", &n1, &signature, &n2) == 1 && n2 == (int)strlen(type)) || n1 == (int)strlen(type)) | |
1478 | && signature <= 0xff)) { | |
1479 | set_err(EINVAL, "Option '-d usbcypress,<n>' requires <n> to be " | |
1480 | "an hexadecimal number between 0x0 and 0xff"); | |
1481 | return 0; | |
1482 | } | |
a86ec89e | 1483 | satdev = new usbcypress_device(this, scsidev, type, signature); |
2127e193 GI |
1484 | } |
1485 | ||
1486 | else if (!strncmp(type, "usbjmicron", 10)) { | |
1487 | const char * t = type + 10; | |
ee38a438 GI |
1488 | bool prolific = false; |
1489 | if (!strncmp(t, ",p", 2)) { | |
1490 | t += 2; | |
1491 | prolific = true; | |
1492 | } | |
2127e193 GI |
1493 | bool ata_48bit_support = false; |
1494 | if (!strncmp(t, ",x", 2)) { | |
1495 | t += 2; | |
1496 | ata_48bit_support = true; | |
1497 | } | |
1498 | int port = -1, n = -1; | |
1499 | if (*t && !( (sscanf(t, ",%d%n", &port, &n) == 1 | |
1500 | && n == (int)strlen(t) && 0 <= port && port <= 1))) { | |
ee38a438 | 1501 | set_err(EINVAL, "Option '-d usbjmicron[,p][,x],<n>' requires <n> to be 0 or 1"); |
2127e193 GI |
1502 | return 0; |
1503 | } | |
a86ec89e GI |
1504 | satdev = new usbjmicron_device(this, scsidev, type, prolific, ata_48bit_support, port); |
1505 | } | |
1506 | ||
1507 | else if (!strcmp(type, "usbprolific")) { | |
1508 | satdev = new usbprolific_device(this, scsidev, type); | |
2127e193 GI |
1509 | } |
1510 | ||
1511 | else if (!strcmp(type, "usbsunplus")) { | |
a86ec89e | 1512 | satdev = new usbsunplus_device(this, scsidev, type); |
2127e193 GI |
1513 | } |
1514 | ||
1515 | else { | |
1516 | set_err(EINVAL, "Unknown USB device type '%s'", type); | |
1517 | return 0; | |
1518 | } | |
a86ec89e GI |
1519 | |
1520 | // 'scsidev' is now owned by 'satdev' | |
1521 | scsidev_holder.release(); | |
1522 | return satdev; | |
2127e193 GI |
1523 | } |
1524 | ||
1525 | // Try to detect a SAT device behind a SCSI interface. | |
1526 | ||
1527 | ata_device * smart_interface::autodetect_sat_device(scsi_device * scsidev, | |
1528 | const unsigned char * inqdata, unsigned inqsize) | |
1529 | { | |
1530 | if (!scsidev->is_open()) | |
1531 | return 0; | |
1532 | ||
bed94269 | 1533 | // SAT ? |
ff28b140 TL |
1534 | if (inqdata && inqsize >= 36 && !memcmp(inqdata + 8, "ATA ", 8)) { |
1535 | // TODO: Linux-specific? No, all SAT standards say the 'T10 Vendor | |
1536 | // Identification' field shall be 'ATA '. | |
bed94269 GI |
1537 | ata_device_auto_ptr atadev( new sat_device(this, scsidev, "") , scsidev); |
1538 | if (has_sat_pass_through(atadev.get())) | |
1539 | return atadev.release(); // Detected SAT | |
2127e193 GI |
1540 | } |
1541 | ||
1542 | return 0; | |
1543 | } | |
1544 | ||
1545 | ||
1546 | ///////////////////////////////////////////////////////////////////////////// | |
1547 | // USB device type detection | |
1548 | ||
2127e193 GI |
1549 | // Format USB ID for error messages |
1550 | static std::string format_usb_id(int vendor_id, int product_id, int version) | |
1551 | { | |
1552 | if (version >= 0) | |
1553 | return strprintf("[0x%04x:0x%04x (0x%03x)]", vendor_id, product_id, version); | |
1554 | else | |
1555 | return strprintf("[0x%04x:0x%04x]", vendor_id, product_id); | |
1556 | } | |
1557 | ||
1558 | // Get type name for USB device with known VENDOR:PRODUCT ID. | |
1559 | const char * smart_interface::get_usb_dev_type_by_id(int vendor_id, int product_id, | |
1560 | int version /*= -1*/) | |
1561 | { | |
e9583e0c GI |
1562 | usb_dev_info info, info2; |
1563 | int n = lookup_usb_device(vendor_id, product_id, version, info, info2); | |
2127e193 | 1564 | |
e9583e0c | 1565 | if (n <= 0) { |
2127e193 GI |
1566 | set_err(EINVAL, "Unknown USB bridge %s", |
1567 | format_usb_id(vendor_id, product_id, version).c_str()); | |
1568 | return 0; | |
1569 | } | |
e9583e0c GI |
1570 | |
1571 | if (n > 1) { | |
1572 | set_err(EINVAL, "USB bridge %s type is ambiguous: '%s' or '%s'", | |
1573 | format_usb_id(vendor_id, product_id, version).c_str(), | |
1574 | (!info.usb_type.empty() ? info.usb_type.c_str() : "[unsupported]"), | |
1575 | (!info2.usb_type.empty() ? info2.usb_type.c_str() : "[unsupported]")); | |
1576 | return 0; | |
1577 | } | |
1578 | ||
1579 | if (info.usb_type.empty()) { | |
2127e193 GI |
1580 | set_err(ENOSYS, "Unsupported USB bridge %s", |
1581 | format_usb_id(vendor_id, product_id, version).c_str()); | |
1582 | return 0; | |
1583 | } | |
e9583e0c GI |
1584 | |
1585 | // TODO: change return type to std::string | |
1586 | static std::string type; | |
1587 | type = info.usb_type; | |
1588 | return type.c_str(); | |
2127e193 | 1589 | } |