]>
Commit | Line | Data |
---|---|---|
4d59bff9 GG |
1 | /* |
2 | * scsiata.cpp | |
3 | * | |
a86ec89e | 4 | * Home page of code is: http://www.smartmontools.org |
4d59bff9 | 5 | * |
a86ec89e GI |
6 | * Copyright (C) 2006-15 Douglas Gilbert <dgilbert@interlog.com> |
7 | * Copyright (C) 2009-16 Christian Franke | |
4d59bff9 GG |
8 | * |
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) | |
12 | * any later version. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
ee38a438 GI |
15 | * (for example COPYING); if not, write to the Free Software Foundation, |
16 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
4d59bff9 GG |
17 | * |
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. | |
24 | * | |
2127e193 GI |
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. | |
34 | * | |
4d59bff9 GG |
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). | |
46 | * | |
47 | */ | |
48 | ||
49 | #include <stdio.h> | |
50 | #include <string.h> | |
54965743 | 51 | #include <stdlib.h> |
2127e193 | 52 | #include <ctype.h> |
cfbba5b9 | 53 | #include <errno.h> |
4d59bff9 GG |
54 | |
55 | #include "config.h" | |
56 | #include "int64.h" | |
4d59bff9 | 57 | #include "scsicmds.h" |
2127e193 | 58 | #include "atacmds.h" // ataReadHDIdentity() |
e9583e0c | 59 | #include "knowndrives.h" // lookup_usb_device() |
4d59bff9 | 60 | #include "utility.h" |
2127e193 GI |
61 | #include "dev_interface.h" |
62 | #include "dev_ata_cmd_set.h" // ata_device_with_command_set | |
63 | #include "dev_tunnelled.h" // tunnelled_device<> | |
4d59bff9 | 64 | |
17d0b8d9 | 65 | <<<<<<< HEAD |
a86ec89e | 66 | const char * scsiata_cpp_cvsid = "$Id: scsiata.cpp 4276 2016-04-02 19:13:39Z chrfranke $"; |
17d0b8d9 | 67 | ======= |
3d8ad6fa | 68 | const char * scsiata_cpp_cvsid = "$Id: scsiata.cpp 4041 2015-03-14 00:50:20Z dpgilbert $"; |
17d0b8d9 | 69 | >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74 |
4d59bff9 | 70 | |
2127e193 GI |
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; | |
81 | unsigned char asc; | |
82 | unsigned char ascq; | |
83 | unsigned char byte4; | |
84 | unsigned char byte5; | |
85 | unsigned char byte6; | |
86 | unsigned char additional_length; | |
87 | }; | |
88 | ||
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); | |
98 | ||
2127e193 GI |
99 | #define SAT_ATA_PASSTHROUGH_12LEN 12 |
100 | #define SAT_ATA_PASSTHROUGH_16LEN 16 | |
101 | ||
4d59bff9 | 102 | #define DEF_SAT_ATA_PASSTHRU_SIZE 16 |
1953ff6d | 103 | #define ATA_RETURN_DESCRIPTOR 9 |
4d59bff9 GG |
104 | |
105 | ||
2127e193 GI |
106 | namespace sat { // no need to publish anything, name provided for Doxygen |
107 | ||
108 | /// SAT support. | |
109 | /// Implements ATA by tunnelling through SCSI. | |
110 | ||
111 | class sat_device | |
112 | : public tunnelled_device< | |
113 | /*implements*/ ata_device | |
114 | /*by tunnelling through a*/, scsi_device | |
d008864d GI |
115 | >, |
116 | virtual public /*implements*/ scsi_device | |
2127e193 GI |
117 | { |
118 | public: | |
119 | sat_device(smart_interface * intf, scsi_device * scsidev, | |
d008864d | 120 | const char * req_type, int passthrulen = 0, bool enable_auto = false); |
2127e193 GI |
121 | |
122 | virtual ~sat_device() throw(); | |
123 | ||
d008864d GI |
124 | virtual smart_device * autodetect_open(); |
125 | ||
2127e193 GI |
126 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); |
127 | ||
d008864d GI |
128 | virtual bool scsi_pass_through(scsi_cmnd_io * iop); |
129 | ||
2127e193 GI |
130 | private: |
131 | int m_passthrulen; | |
d008864d | 132 | bool m_enable_auto; |
2127e193 GI |
133 | }; |
134 | ||
135 | ||
136 | sat_device::sat_device(smart_interface * intf, scsi_device * scsidev, | |
d008864d GI |
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), | |
2127e193 | 140 | tunnelled_device<ata_device, scsi_device>(scsidev), |
d008864d GI |
141 | m_passthrulen(passthrulen), |
142 | m_enable_auto(enable_auto) | |
2127e193 | 143 | { |
d008864d GI |
144 | if (enable_auto) |
145 | hide_ata(); // Start as SCSI, switch to ATA in autodetect_open() | |
146 | else | |
147 | hide_scsi(); // ATA always | |
ee38a438 GI |
148 | if (strcmp(scsidev->get_dev_type(), "scsi")) |
149 | set_info().dev_type += strprintf("+%s", scsidev->get_dev_type()); | |
d008864d GI |
150 | |
151 | set_info().info_name = strprintf("%s [%sSAT]", scsidev->get_info_name(), | |
152 | (enable_auto ? "SCSI/" : "")); | |
2127e193 GI |
153 | } |
154 | ||
155 | sat_device::~sat_device() throw() | |
156 | { | |
157 | } | |
158 | ||
159 | ||
4d59bff9 GG |
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) | |
173 | // cdb[13]: device | |
174 | // cdb[14]: (ata) command | |
175 | // cdb[15]: control (SCSI, leave as zero) | |
176 | // | |
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] | |
179 | // | |
180 | // | |
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) | |
189 | // cdb[8]: device | |
190 | // cdb[9]: (ata) command | |
191 | // cdb[10]: reserved | |
192 | // cdb[11]: control (SCSI, leave as zero) | |
193 | // | |
194 | // | |
1953ff6d | 195 | // ATA Return Descriptor (component of descriptor sense data) |
4d59bff9 GG |
196 | // des[0]: descriptor code (0x9) |
197 | // des[1]: additional descriptor length (0xc) | |
198 | // des[2]: extend (bit 0) | |
199 | // des[3]: error | |
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) | |
208 | // des[12]: device | |
209 | // des[13]: status | |
a86ec89e GI |
210 | // |
211 | // | |
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' | |
4d59bff9 GG |
229 | |
230 | ||
231 | ||
232 | // PURPOSE | |
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 | |
2127e193 | 236 | // one used is chosen via this->m_passthrulen . |
4d59bff9 GG |
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 | |
241 | // self-test). | |
242 | // data: location to write output data, if needed (512 bytes). | |
243 | // Note: not all commands use all arguments. | |
244 | // RETURN VALUES | |
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" | |
251 | ||
2127e193 | 252 | bool sat_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) |
4d59bff9 | 253 | { |
ee38a438 GI |
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, | |
259 | "SAT") | |
2127e193 GI |
260 | ) |
261 | return false; | |
262 | ||
4d59bff9 GG |
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]; | |
1953ff6d | 268 | const unsigned char * ardp; |
a86ec89e | 269 | int ard_len, have_sense; |
4d59bff9 | 270 | int extend = 0; |
1953ff6d | 271 | int ck_cond = 0; /* set to 1 to read register(s) back */ |
4d59bff9 GG |
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 */ | |
4d59bff9 | 276 | int passthru_size = DEF_SAT_ATA_PASSTHRU_SIZE; |
a86ec89e | 277 | bool sense_descriptor = true; |
4d59bff9 GG |
278 | |
279 | memset(cdb, 0, sizeof(cdb)); | |
280 | memset(sense, 0, sizeof(sense)); | |
281 | ||
2127e193 GI |
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: | |
4d59bff9 | 286 | break; |
2127e193 GI |
287 | case ata_cmd_in::data_in: |
288 | protocol = 4; // PIO data-in | |
289 | t_length = 2; // sector_count holds count | |
4d59bff9 | 290 | break; |
2127e193 GI |
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 | |
4d59bff9 | 295 | break; |
2127e193 GI |
296 | default: |
297 | return set_err(EINVAL, "sat_device::ata_pass_through: invalid direction=%d", | |
298 | (int)in.direction); | |
4d59bff9 | 299 | } |
2127e193 GI |
300 | |
301 | // Check condition if any output register needed | |
302 | if (in.out_needed.is_set()) | |
303 | ck_cond = 1; | |
304 | ||
305 | if ((SAT_ATA_PASSTHROUGH_12LEN == m_passthrulen) || | |
306 | (SAT_ATA_PASSTHROUGH_16LEN == m_passthrulen)) | |
307 | passthru_size = m_passthrulen; | |
308 | ||
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)"); | |
313 | extend = 1; | |
4d59bff9 GG |
314 | } |
315 | ||
4d59bff9 GG |
316 | cdb[0] = (SAT_ATA_PASSTHROUGH_12LEN == passthru_size) ? |
317 | SAT_ATA_PASSTHROUGH_12 : SAT_ATA_PASSTHROUGH_16; | |
318 | ||
319 | cdb[1] = (protocol << 1) | extend; | |
1953ff6d | 320 | cdb[2] = (ck_cond << 5) | (t_dir << 3) | |
4d59bff9 | 321 | (byte_block << 2) | t_length; |
2127e193 GI |
322 | |
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; | |
328 | cdb[5] = lo.lba_low; | |
329 | cdb[6] = lo.lba_mid; | |
330 | cdb[7] = lo.lba_high; | |
331 | cdb[8] = lo.device; | |
332 | cdb[9] = lo.command; | |
333 | } | |
334 | else { | |
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; | |
349 | cdb[13] = lo.device; | |
350 | cdb[14] = lo.command; | |
351 | } | |
4d59bff9 GG |
352 | |
353 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
354 | if (0 == t_length) { | |
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; | |
2127e193 GI |
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 | |
4d59bff9 GG |
362 | } else { /* to device */ |
363 | io_hdr.dxfer_dir = DXFER_TO_DEVICE; | |
2127e193 GI |
364 | io_hdr.dxfer_len = in.size; |
365 | io_hdr.dxferp = (unsigned char *)in.buffer; | |
4d59bff9 GG |
366 | } |
367 | io_hdr.cmnd = cdb; | |
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; | |
372 | ||
2127e193 GI |
373 | scsi_device * scsidev = get_tunnel_dev(); |
374 | if (!scsidev->scsi_pass_through(&io_hdr)) { | |
cfbba5b9 | 375 | if (scsi_debugmode > 0) |
2127e193 GI |
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()); | |
4d59bff9 | 379 | } |
1953ff6d GG |
380 | ardp = NULL; |
381 | ard_len = 0; | |
382 | have_sense = sg_scsi_normalize_sense(io_hdr.sensep, io_hdr.resp_sense_len, | |
383 | &ssh); | |
384 | if (have_sense) { | |
a86ec89e GI |
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); | |
391 | if (ardp) { | |
392 | ard_len = ardp[1] + 2; | |
393 | if (ard_len < 12) | |
394 | ard_len = 12; | |
395 | else if (ard_len > 14) | |
396 | ard_len = 14; | |
397 | } | |
1953ff6d GG |
398 | } |
399 | scsi_do_sense_disect(&io_hdr, &sinfo); | |
a86ec89e | 400 | int status = scsiSimpleSenseFilter(&sinfo); |
17d0b8d9 | 401 | <<<<<<< HEAD |
a86ec89e GI |
402 | |
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", | |
408 | ardp[13]); | |
409 | status = 0; | |
410 | } | |
411 | ||
17d0b8d9 GI |
412 | ======= |
413 | >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74 | |
a86ec89e | 414 | if (0 != status) { /* other than no_sense and recovered_error */ |
cfbba5b9 | 415 | if (scsi_debugmode > 0) { |
2127e193 | 416 | pout("sat_device::ata_pass_through: scsi error: %s\n", |
1953ff6d | 417 | scsiErrString(status)); |
cfbba5b9 | 418 | if (ardp && (scsi_debugmode > 1)) { |
1953ff6d GG |
419 | pout("Values from ATA Return Descriptor are:\n"); |
420 | dStrHex((const char *)ardp, ard_len, 1); | |
421 | } | |
422 | } | |
2127e193 GI |
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)); | |
1953ff6d GG |
426 | } |
427 | } | |
428 | if (ck_cond) { /* expecting SAT specific sense data */ | |
429 | if (have_sense) { | |
430 | if (ardp) { | |
cfbba5b9 | 431 | if (scsi_debugmode > 1) { |
1953ff6d GG |
432 | pout("Values from ATA Return Descriptor are:\n"); |
433 | dStrHex((const char *)ardp, ard_len, 1); | |
4d59bff9 | 434 | } |
2127e193 GI |
435 | // Set output registers |
436 | ata_out_regs & lo = out.out_regs; | |
437 | lo.error = ardp[ 3]; | |
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]; | |
4d59bff9 | 450 | } |
a86ec89e GI |
451 | } else if ((! sense_descriptor) && |
452 | (0 == ssh.asc) && | |
17d0b8d9 | 453 | <<<<<<< HEAD |
a86ec89e GI |
454 | (SCSI_ASCQ_ATA_PASS_THROUGH == ssh.ascq) && |
455 | (0 != io_hdr.sensep[4] /* Some ATA STATUS bit must be set */)) { | |
17d0b8d9 | 456 | ======= |
3d8ad6fa | 457 | (SCSI_ASCQ_ATA_PASS_THROUGH == ssh.ascq)) { |
17d0b8d9 | 458 | >>>>>>> 3d8ad6fa4529eb02ae1391a1e937bf57aad3fb74 |
a86ec89e GI |
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; | |
472 | hi.sector_count = 0; | |
473 | hi.lba_low = 0; | |
474 | hi.lba_mid = 0; | |
475 | hi.lba_high = 0; | |
476 | } else { | |
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. */ | |
482 | } | |
483 | } | |
4d59bff9 GG |
484 | } |
485 | } | |
a86ec89e | 486 | } else { /* ck_cond == 0 */ |
1953ff6d | 487 | if (have_sense) { |
a86ec89e | 488 | if (((SCSI_SK_NO_SENSE == ssh.sense_key) || |
4d59bff9 GG |
489 | (SCSI_SK_RECOVERED_ERR == ssh.sense_key)) && |
490 | (0 == ssh.asc) && | |
491 | (SCSI_ASCQ_ATA_PASS_THROUGH == ssh.ascq)) { | |
a86ec89e GI |
492 | if (scsi_debugmode > 0) { |
493 | if (sense_descriptor && ardp) { | |
1953ff6d GG |
494 | pout("Values from ATA Return Descriptor are:\n"); |
495 | dStrHex((const char *)ardp, ard_len, 1); | |
a86ec89e GI |
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]); | |
4d59bff9 | 502 | } |
4d59bff9 GG |
503 | } |
504 | } | |
a86ec89e | 505 | return set_err(EIO, "SAT command failed"); |
4d59bff9 GG |
506 | } |
507 | } | |
2127e193 | 508 | return true; |
4d59bff9 GG |
509 | } |
510 | ||
d008864d GI |
511 | bool sat_device::scsi_pass_through(scsi_cmnd_io * iop) |
512 | { | |
513 | scsi_device * scsidev = get_tunnel_dev(); | |
514 | if (!scsidev->scsi_pass_through(iop)) { | |
515 | set_err(scsidev->get_err()); | |
516 | return false; | |
517 | } | |
518 | return true; | |
519 | } | |
520 | ||
521 | smart_device * sat_device::autodetect_open() | |
522 | { | |
523 | if (!open() || !m_enable_auto) | |
524 | return this; | |
525 | ||
526 | scsi_device * scsidev = get_tunnel_dev(); | |
527 | ||
528 | unsigned char inqdata[36] = {0, }; | |
529 | if (scsiStdInquiry(scsidev, inqdata, sizeof(inqdata))) { | |
530 | smart_device::error_info err = scsidev->get_err(); | |
531 | close(); | |
532 | set_err(err.no, "INQUIRY [SAT]: %s", err.msg.c_str()); | |
533 | return this; | |
534 | } | |
535 | ||
536 | // Check for SAT "VENDOR" | |
537 | int inqsize = inqdata[4] + 5; | |
538 | bool sat = (inqsize >= 36 && !memcmp(inqdata + 8, "ATA ", 8)); | |
539 | ||
540 | // Change interface | |
541 | hide_ata(!sat); | |
542 | hide_scsi(sat); | |
543 | ||
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")); | |
547 | return this; | |
548 | } | |
549 | ||
2127e193 GI |
550 | } // namespace |
551 | ||
552 | ///////////////////////////////////////////////////////////////////////////// | |
553 | ||
4d59bff9 | 554 | /* Attempt an IDENTIFY DEVICE ATA command via SATL when packet_interface |
2127e193 GI |
555 | is false otherwise attempt IDENTIFY PACKET DEVICE. If successful |
556 | return true, else false */ | |
557 | ||
558 | static bool has_sat_pass_through(ata_device * dev, bool packet_interface = false) | |
4d59bff9 | 559 | { |
54965743 GI |
560 | /* Note: malloc() ensures the read buffer lands on a single |
561 | page. This avoids some bugs seen on LSI controlers under | |
562 | FreeBSD */ | |
563 | char *data = (char *)malloc(512); | |
2127e193 GI |
564 | ata_cmd_in in; |
565 | in.in_regs.command = (packet_interface ? ATA_IDENTIFY_PACKET_DEVICE : ATA_IDENTIFY_DEVICE); | |
2127e193 | 566 | in.set_data_in(data, 1); |
54965743 GI |
567 | bool ret = dev->ata_pass_through(in); |
568 | free(data); | |
569 | return ret; | |
4d59bff9 GG |
570 | } |
571 | ||
2127e193 GI |
572 | ///////////////////////////////////////////////////////////////////////////// |
573 | ||
4d59bff9 GG |
574 | /* Next two functions are borrowed from sg_lib.c in the sg3_utils |
575 | package. Same copyrght owner, same license as this file. */ | |
2127e193 GI |
576 | static int sg_scsi_normalize_sense(const unsigned char * sensep, int sb_len, |
577 | struct sg_scsi_sense_hdr * sshp) | |
4d59bff9 GG |
578 | { |
579 | if (sshp) | |
580 | memset(sshp, 0, sizeof(struct sg_scsi_sense_hdr)); | |
581 | if ((NULL == sensep) || (0 == sb_len) || (0x70 != (0x70 & sensep[0]))) | |
582 | return 0; | |
583 | if (sshp) { | |
584 | sshp->response_code = (0x7f & sensep[0]); | |
585 | if (sshp->response_code >= 0x72) { /* descriptor format */ | |
586 | if (sb_len > 1) | |
587 | sshp->sense_key = (0xf & sensep[1]); | |
588 | if (sb_len > 2) | |
589 | sshp->asc = sensep[2]; | |
590 | if (sb_len > 3) | |
591 | sshp->ascq = sensep[3]; | |
592 | if (sb_len > 7) | |
593 | sshp->additional_length = sensep[7]; | |
594 | } else { /* fixed format */ | |
595 | if (sb_len > 2) | |
596 | sshp->sense_key = (0xf & sensep[2]); | |
597 | if (sb_len > 7) { | |
598 | sb_len = (sb_len < (sensep[7] + 8)) ? sb_len : | |
599 | (sensep[7] + 8); | |
600 | if (sb_len > 12) | |
601 | sshp->asc = sensep[12]; | |
602 | if (sb_len > 13) | |
603 | sshp->ascq = sensep[13]; | |
604 | } | |
605 | } | |
606 | } | |
607 | return 1; | |
608 | } | |
609 | ||
610 | ||
2127e193 GI |
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 = "") | |
615 | { | |
616 | // Provide sense buffer | |
617 | unsigned char sense[32] = {0, }; | |
618 | iop->sensep = sense; | |
619 | iop->max_sense_len = sizeof(sense); | |
620 | iop->timeout = SCSI_TIMEOUT_DEFAULT; | |
621 | ||
622 | // Run cmd | |
623 | if (!scsidev->scsi_pass_through(iop)) { | |
cfbba5b9 | 624 | if (scsi_debugmode > 0) |
2127e193 GI |
625 | pout("%sscsi_pass_through() failed, errno=%d [%s]\n", |
626 | msg, scsidev->get_errno(), scsidev->get_errmsg()); | |
627 | return false; | |
628 | } | |
629 | ||
630 | // Check sense | |
631 | scsi_sense_disect sinfo; | |
632 | scsi_do_sense_disect(iop, &sinfo); | |
633 | int err = scsiSimpleSenseFilter(&sinfo); | |
634 | if (err) { | |
cfbba5b9 | 635 | if (scsi_debugmode > 0) |
2127e193 GI |
636 | pout("%sscsi error: %s\n", msg, scsiErrString(err)); |
637 | return scsidev->set_err(EIO, "scsi error %s", scsiErrString(err)); | |
638 | } | |
639 | ||
640 | return true; | |
641 | } | |
642 | ||
643 | ||
644 | ///////////////////////////////////////////////////////////////////////////// | |
645 | ||
646 | namespace sat { | |
647 | ||
648 | /// Cypress USB Brigde support. | |
649 | ||
650 | class usbcypress_device | |
651 | : public tunnelled_device< | |
652 | /*implements*/ ata_device_with_command_set | |
653 | /*by tunnelling through a*/, scsi_device | |
654 | > | |
655 | { | |
656 | public: | |
657 | usbcypress_device(smart_interface * intf, scsi_device * scsidev, | |
658 | const char * req_type, unsigned char signature); | |
659 | ||
660 | virtual ~usbcypress_device() throw(); | |
661 | ||
662 | protected: | |
663 | virtual int ata_command_interface(smart_command_set command, int select, char * data); | |
664 | ||
665 | unsigned char m_signature; | |
666 | }; | |
667 | ||
668 | ||
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) | |
674 | { | |
675 | set_info().info_name = strprintf("%s [USB Cypress]", scsidev->get_info_name()); | |
676 | } | |
677 | ||
678 | usbcypress_device::~usbcypress_device() throw() | |
679 | { | |
680 | } | |
681 | ||
682 | ||
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) | |
686 | { | |
687 | struct scsi_cmnd_io io_hdr; | |
688 | unsigned char cdb[USBCYPRESS_PASSTHROUGH_LEN]; | |
689 | unsigned char sense[32]; | |
690 | int copydata = 0; | |
691 | int outlen = 0; | |
692 | int ck_cond = 0; /* set to 1 to read register(s) back */ | |
2127e193 GI |
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 */ | |
696 | int feature = 0; | |
697 | int ata_command = 0; | |
698 | int sector_count = 0; | |
699 | int lba_low = 0; | |
700 | int lba_mid = 0; | |
701 | int lba_high = 0; | |
702 | int passthru_size = USBCYPRESS_PASSTHROUGH_LEN; | |
703 | ||
704 | memset(cdb, 0, sizeof(cdb)); | |
705 | memset(sense, 0, sizeof(sense)); | |
706 | ||
707 | ata_command = ATA_SMART_CMD; | |
708 | switch (command) { | |
709 | case CHECK_POWER_MODE: | |
710 | ata_command = ATA_CHECK_POWER_MODE; | |
711 | ck_cond = 1; | |
712 | copydata = 1; | |
713 | break; | |
714 | case READ_VALUES: /* READ DATA */ | |
715 | feature = ATA_SMART_READ_VALUES; | |
716 | sector_count = 1; /* one (512 byte) block */ | |
2127e193 GI |
717 | t_length = 2; /* sector count holds count */ |
718 | copydata = 512; | |
719 | break; | |
720 | case READ_THRESHOLDS: /* obsolete */ | |
721 | feature = ATA_SMART_READ_THRESHOLDS; | |
722 | sector_count = 1; /* one (512 byte) block */ | |
723 | lba_low = 1; | |
2127e193 GI |
724 | t_length = 2; /* sector count holds count */ |
725 | copydata=512; | |
726 | break; | |
727 | case READ_LOG: | |
728 | feature = ATA_SMART_READ_LOG_SECTOR; | |
729 | sector_count = 1; /* one (512 byte) block */ | |
730 | lba_low = select; | |
2127e193 GI |
731 | t_length = 2; /* sector count holds count */ |
732 | copydata = 512; | |
733 | break; | |
734 | case WRITE_LOG: | |
735 | feature = ATA_SMART_WRITE_LOG_SECTOR; | |
736 | sector_count = 1; /* one (512 byte) block */ | |
737 | lba_low = select; | |
2127e193 GI |
738 | t_length = 2; /* sector count holds count */ |
739 | t_dir = 0; /* to device */ | |
740 | outlen = 512; | |
741 | break; | |
742 | case IDENTIFY: | |
743 | ata_command = ATA_IDENTIFY_DEVICE; | |
744 | sector_count = 1; /* one (512 byte) block */ | |
2127e193 GI |
745 | t_length = 2; /* sector count holds count */ |
746 | copydata = 512; | |
747 | break; | |
748 | case PIDENTIFY: | |
749 | ata_command = ATA_IDENTIFY_PACKET_DEVICE; | |
750 | sector_count = 1; /* one (512 byte) block */ | |
2127e193 GI |
751 | t_length = 2; /* sector count (7:0) holds count */ |
752 | copydata = 512; | |
753 | break; | |
754 | case ENABLE: | |
755 | feature = ATA_SMART_ENABLE; | |
756 | lba_low = 1; | |
757 | break; | |
758 | case DISABLE: | |
759 | feature = ATA_SMART_DISABLE; | |
760 | lba_low = 1; | |
761 | break; | |
762 | case STATUS: | |
763 | // this command only says if SMART is working. It could be | |
764 | // replaced with STATUS_CHECK below. | |
765 | feature = ATA_SMART_STATUS; | |
766 | ck_cond = 1; | |
767 | break; | |
768 | case AUTO_OFFLINE: | |
769 | feature = ATA_SMART_AUTO_OFFLINE; | |
770 | sector_count = select; // YET NOTE - THIS IS A NON-DATA COMMAND!! | |
771 | break; | |
772 | case AUTOSAVE: | |
773 | feature = ATA_SMART_AUTOSAVE; | |
774 | sector_count = select; // YET NOTE - THIS IS A NON-DATA COMMAND!! | |
775 | break; | |
776 | case IMMEDIATE_OFFLINE: | |
777 | feature = ATA_SMART_IMMEDIATE_OFFLINE; | |
778 | lba_low = select; | |
779 | break; | |
780 | case STATUS_CHECK: | |
781 | // This command uses HDIO_DRIVE_TASK and has different syntax than | |
782 | // the other commands. | |
783 | feature = ATA_SMART_STATUS; /* SMART RETURN STATUS */ | |
784 | ck_cond = 1; | |
785 | break; | |
786 | default: | |
787 | pout("Unrecognized command %d in usbcypress_device::ata_command_interface()\n" | |
788 | "Please contact " PACKAGE_BUGREPORT "\n", command); | |
789 | errno=ENOSYS; | |
790 | return -1; | |
791 | } | |
792 | if (ATA_SMART_CMD == ata_command) { | |
793 | lba_mid = 0x4f; | |
794 | lba_high = 0xc2; | |
795 | } | |
796 | ||
797 | cdb[0] = m_signature; // bVSCBSignature : vendor-specific command | |
798 | cdb[1] = 0x24; // bVSCBSubCommand : 0x24 for ATACB | |
799 | cdb[2] = 0x0; | |
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 | |
805 | ||
806 | ||
807 | cdb[6] = feature; | |
808 | cdb[7] = sector_count; | |
809 | cdb[8] = lba_low; | |
810 | cdb[9] = lba_mid; | |
811 | cdb[10] = lba_high; | |
812 | cdb[12] = ata_command; | |
813 | ||
814 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
815 | if (0 == t_length) { | |
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; | |
827 | } | |
828 | io_hdr.cmnd = cdb; | |
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; | |
833 | ||
834 | scsi_device * scsidev = get_tunnel_dev(); | |
835 | if (!scsidev->scsi_pass_through(&io_hdr)) { | |
cfbba5b9 | 836 | if (scsi_debugmode > 0) |
2127e193 GI |
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()); | |
840 | return -1; | |
841 | } | |
842 | ||
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)) { | |
847 | return -1; | |
848 | } | |
849 | if (ck_cond) { | |
850 | unsigned char ardp[8]; | |
851 | int ard_len = 8; | |
852 | /* XXX this is racy if there other scsi command between | |
853 | * the first usbcypress command and this one | |
854 | */ | |
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] | |
859 | */ | |
860 | cdb[2] = (1<<0); /* ask read taskfile */ | |
861 | memset(sense, 0, sizeof(sense)); | |
862 | ||
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 */ | |
869 | ||
870 | io_hdr.cmnd = cdb; | |
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; | |
875 | ||
876 | ||
877 | if (!scsidev->scsi_pass_through(&io_hdr)) { | |
cfbba5b9 | 878 | if (scsi_debugmode > 0) |
2127e193 GI |
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()); | |
882 | return -1; | |
883 | } | |
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)) { | |
888 | return -1; | |
889 | } | |
890 | ||
891 | ||
cfbba5b9 | 892 | if (scsi_debugmode > 1) { |
2127e193 GI |
893 | pout("Values from ATA Return Descriptor are:\n"); |
894 | dStrHex((const char *)ardp, ard_len, 1); | |
895 | } | |
896 | ||
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); | |
912 | return -1; | |
913 | } | |
914 | } | |
915 | return 0; | |
916 | } | |
917 | ||
918 | #if 0 // Not used, see autodetect_sat_device() below. | |
919 | static int isprint_string(const char *s) | |
920 | { | |
921 | while (*s) { | |
922 | if (isprint(*s) == 0) | |
923 | return 0; | |
924 | s++; | |
925 | } | |
926 | return 1; | |
927 | } | |
928 | ||
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) | |
933 | { | |
934 | struct ata_identify_device drive; | |
935 | char model[40], serial[20], firm[8]; | |
936 | ||
937 | /* issue the command and do a checksum if possible */ | |
938 | if (ataReadHDIdentity(atadev, &drive) < 0) | |
939 | return 0; | |
940 | ||
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) | |
944 | return 0; | |
945 | ||
946 | if (manufacturer && strncmp(manufacturer, model, 8)) | |
947 | pout("manufacturer doesn't match in pass_through test\n"); | |
948 | if (product && | |
949 | strlen(model) > 8 && strncmp(product, model+8, strlen(model)-8)) | |
950 | pout("product doesn't match in pass_through test\n"); | |
951 | ||
952 | /* check serial */ | |
953 | format_ata_string(serial, drive.serial_no, 20); | |
954 | if (isprint_string(serial) == 0) | |
955 | return 0; | |
956 | format_ata_string(firm, drive.fw_rev, 8); | |
957 | if (isprint_string(firm) == 0) | |
958 | return 0; | |
959 | return 1; | |
960 | } | |
961 | #endif | |
962 | ||
963 | ///////////////////////////////////////////////////////////////////////////// | |
964 | ||
965 | /// JMicron USB Bridge support. | |
966 | ||
967 | class usbjmicron_device | |
968 | : public tunnelled_device< | |
969 | /*implements*/ ata_device, | |
970 | /*by tunnelling through a*/ scsi_device | |
971 | > | |
972 | { | |
973 | public: | |
974 | usbjmicron_device(smart_interface * intf, scsi_device * scsidev, | |
ee38a438 GI |
975 | const char * req_type, bool prolific, |
976 | bool ata_48bit_support, int port); | |
2127e193 GI |
977 | |
978 | virtual ~usbjmicron_device() throw(); | |
979 | ||
980 | virtual bool open(); | |
981 | ||
982 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); | |
983 | ||
984 | private: | |
985 | bool get_registers(unsigned short addr, unsigned char * buf, unsigned short size); | |
986 | ||
ee38a438 | 987 | bool m_prolific; |
2127e193 GI |
988 | bool m_ata_48bit_support; |
989 | int m_port; | |
990 | }; | |
991 | ||
992 | ||
993 | usbjmicron_device::usbjmicron_device(smart_interface * intf, scsi_device * scsidev, | |
ee38a438 GI |
994 | const char * req_type, bool prolific, |
995 | bool ata_48bit_support, int port) | |
2127e193 GI |
996 | : smart_device(intf, scsidev->get_dev_name(), "usbjmicron", req_type), |
997 | tunnelled_device<ata_device, scsi_device>(scsidev), | |
ee38a438 GI |
998 | m_prolific(prolific), m_ata_48bit_support(ata_48bit_support), |
999 | m_port(port >= 0 || !prolific ? port : 0) | |
2127e193 GI |
1000 | { |
1001 | set_info().info_name = strprintf("%s [USB JMicron]", scsidev->get_info_name()); | |
1002 | } | |
1003 | ||
1004 | usbjmicron_device::~usbjmicron_device() throw() | |
1005 | { | |
1006 | } | |
1007 | ||
1008 | ||
1009 | bool usbjmicron_device::open() | |
1010 | { | |
1011 | // Open USB first | |
1012 | if (!tunnelled_device<ata_device, scsi_device>::open()) | |
1013 | return false; | |
1014 | ||
1015 | // Detect port if not specified | |
1016 | if (m_port < 0) { | |
1017 | unsigned char regbuf[1] = {0}; | |
1018 | if (!get_registers(0x720f, regbuf, sizeof(regbuf))) { | |
1019 | close(); | |
1020 | return false; | |
1021 | } | |
1022 | ||
1023 | switch (regbuf[0] & 0x44) { | |
1024 | case 0x04: | |
1025 | m_port = 0; break; | |
1026 | case 0x40: | |
1027 | m_port = 1; break; | |
1028 | case 0x44: | |
1029 | close(); | |
1030 | return set_err(EINVAL, "Two devices connected, try '-d usbjmicron,[01]'"); | |
1031 | default: | |
1032 | close(); | |
1033 | return set_err(ENODEV, "No device connected"); | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | return true; | |
1038 | } | |
1039 | ||
1040 | ||
1041 | bool usbjmicron_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) | |
1042 | { | |
ee38a438 GI |
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), | |
1047 | "JMicron") | |
2127e193 GI |
1048 | ) |
1049 | return false; | |
1050 | ||
2127e193 GI |
1051 | if (m_port < 0) |
1052 | return set_err(EIO, "Unknown JMicron port"); | |
1053 | ||
1054 | scsi_cmnd_io io_hdr; | |
1055 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1056 | ||
1057 | bool rwbit = true; | |
d2e702cf | 1058 | unsigned char smart_status = 0xff; |
2127e193 | 1059 | |
ee38a438 GI |
1060 | bool is_smart_status = ( in.in_regs.command == ATA_SMART_CMD |
1061 | && in.in_regs.features == ATA_SMART_STATUS); | |
1062 | ||
2127e193 GI |
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; | |
1067 | } | |
1068 | else switch (in.direction) { | |
1069 | case ata_cmd_in::no_data: | |
1070 | io_hdr.dxfer_dir = DXFER_NONE; | |
1071 | break; | |
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); | |
1077 | break; | |
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; | |
1082 | rwbit = false; | |
1083 | break; | |
1084 | default: | |
1085 | return set_err(EINVAL); | |
1086 | } | |
1087 | ||
1088 | // Build pass through command | |
ee38a438 | 1089 | unsigned char cdb[14]; |
2127e193 GI |
1090 | cdb[ 0] = 0xdf; |
1091 | cdb[ 1] = (rwbit ? 0x10 : 0x00); | |
1092 | cdb[ 2] = 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; | |
ee38a438 GI |
1102 | // Prolific PL3507 |
1103 | cdb[12] = 0x06; | |
1104 | cdb[13] = 0x7b; | |
2127e193 GI |
1105 | |
1106 | io_hdr.cmnd = cdb; | |
ee38a438 | 1107 | io_hdr.cmnd_len = (!m_prolific ? 12 : 14); |
2127e193 GI |
1108 | |
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()); | |
1113 | ||
1114 | if (in.out_needed.is_set()) { | |
1115 | if (is_smart_status) { | |
d2e702cf GI |
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]"); | |
1119 | ||
2127e193 | 1120 | switch (smart_status) { |
d2e702cf | 1121 | case 0xc2: |
2127e193 GI |
1122 | out.out_regs.lba_high = 0xc2; |
1123 | out.out_regs.lba_mid = 0x4f; | |
1124 | break; | |
d2e702cf | 1125 | case 0x2c: |
2127e193 GI |
1126 | out.out_regs.lba_high = 0x2c; |
1127 | out.out_regs.lba_mid = 0xf4; | |
1128 | break; | |
d2e702cf GI |
1129 | default: |
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); | |
2127e193 GI |
1132 | } |
1133 | } | |
1134 | ||
1135 | #if 0 // Not needed for SMART STATUS, see also notes below | |
1136 | else { | |
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))) | |
1142 | return false; | |
1143 | ||
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]; | |
1151 | } | |
1152 | #endif | |
1153 | } | |
1154 | ||
1155 | return true; | |
1156 | } | |
1157 | ||
1158 | bool usbjmicron_device::get_registers(unsigned short addr, | |
1159 | unsigned char * buf, unsigned short size) | |
1160 | { | |
ee38a438 | 1161 | unsigned char cdb[14]; |
2127e193 GI |
1162 | cdb[ 0] = 0xdf; |
1163 | cdb[ 1] = 0x10; | |
1164 | cdb[ 2] = 0x00; | |
1165 | cdb[ 3] = (unsigned char)(size >> 8); | |
1166 | cdb[ 4] = (unsigned char)(size ); | |
1167 | cdb[ 5] = 0x00; | |
1168 | cdb[ 6] = (unsigned char)(addr >> 8); | |
1169 | cdb[ 7] = (unsigned char)(addr ); | |
1170 | cdb[ 8] = 0x00; | |
1171 | cdb[ 9] = 0x00; | |
1172 | cdb[10] = 0x00; | |
1173 | cdb[11] = 0xfd; | |
ee38a438 GI |
1174 | // Prolific PL3507 |
1175 | cdb[12] = 0x06; | |
1176 | cdb[13] = 0x7b; | |
2127e193 GI |
1177 | |
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; | |
1183 | io_hdr.cmnd = cdb; | |
1184 | io_hdr.cmnd_len = sizeof(cdb); | |
ee38a438 | 1185 | io_hdr.cmnd_len = (!m_prolific ? 12 : 14); |
2127e193 GI |
1186 | |
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()); | |
1191 | ||
1192 | return true; | |
1193 | } | |
1194 | ||
1195 | ||
a86ec89e GI |
1196 | ///////////////////////////////////////////////////////////////////////////// |
1197 | ||
1198 | /// Prolific USB Bridge support. (PL2773) (Probably works on PL2771 also...) | |
1199 | ||
1200 | class usbprolific_device | |
1201 | : public tunnelled_device< | |
1202 | /*implements*/ ata_device, | |
1203 | /*by tunnelling through a*/ scsi_device | |
1204 | > | |
1205 | { | |
1206 | public: | |
1207 | usbprolific_device(smart_interface * intf, scsi_device * scsidev, | |
1208 | const char * req_type); | |
1209 | ||
1210 | virtual ~usbprolific_device() throw(); | |
1211 | ||
1212 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); | |
1213 | }; | |
1214 | ||
1215 | ||
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) | |
1220 | { | |
1221 | set_info().info_name = strprintf("%s [USB Prolific]", scsidev->get_info_name()); | |
1222 | } | |
1223 | ||
1224 | usbprolific_device::~usbprolific_device() throw() | |
1225 | { | |
1226 | } | |
1227 | ||
1228 | bool usbprolific_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) | |
1229 | { | |
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, | |
1235 | "Prolific" ) | |
1236 | ) | |
1237 | return false; | |
1238 | ||
1239 | scsi_cmnd_io io_hdr; | |
1240 | memset(&io_hdr, 0, sizeof(io_hdr)); | |
1241 | unsigned char cmd_rw = 0x10; // Read | |
1242 | ||
1243 | switch (in.direction) { | |
1244 | case ata_cmd_in::no_data: | |
1245 | io_hdr.dxfer_dir = DXFER_NONE; | |
1246 | break; | |
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); | |
1252 | break; | |
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 | |
1258 | break; | |
1259 | default: | |
1260 | return set_err(EINVAL); | |
1261 | } | |
1262 | ||
1263 | // Based on reverse engineering of iSmart.exe with API Monitor. | |
1264 | // Seen commands: | |
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 | |
1273 | ||
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 | |
1293 | ||
1294 | io_hdr.cmnd = cdb; | |
1295 | io_hdr.cmnd_len = 16; | |
1296 | ||
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()); | |
1301 | ||
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; | |
1309 | ||
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) | |
1314 | io_hdr.cmnd = cdb; | |
1315 | io_hdr.cmnd_len = sizeof(cdb); | |
1316 | ||
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()); | |
1320 | ||
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 | |
1332 | } | |
1333 | ||
1334 | return true; | |
1335 | } | |
1336 | ||
1337 | ||
2127e193 GI |
1338 | ///////////////////////////////////////////////////////////////////////////// |
1339 | ||
1340 | /// SunplusIT USB Bridge support. | |
1341 | ||
1342 | class usbsunplus_device | |
1343 | : public tunnelled_device< | |
1344 | /*implements*/ ata_device, | |
1345 | /*by tunnelling through a*/ scsi_device | |
1346 | > | |
1347 | { | |
1348 | public: | |
1349 | usbsunplus_device(smart_interface * intf, scsi_device * scsidev, | |
1350 | const char * req_type); | |
1351 | ||
1352 | virtual ~usbsunplus_device() throw(); | |
1353 | ||
1354 | virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out); | |
1355 | }; | |
1356 | ||
1357 | ||
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) | |
1362 | { | |
1363 | set_info().info_name = strprintf("%s [USB Sunplus]", scsidev->get_info_name()); | |
1364 | } | |
1365 | ||
1366 | usbsunplus_device::~usbsunplus_device() throw() | |
1367 | { | |
1368 | } | |
1369 | ||
1370 | bool usbsunplus_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out) | |
1371 | { | |
ee38a438 GI |
1372 | if (!ata_cmd_is_supported(in, |
1373 | ata_device::supports_data_out | | |
1374 | ata_device::supports_output_regs | | |
1375 | ata_device::supports_48bit, | |
1376 | "Sunplus") | |
2127e193 GI |
1377 | ) |
1378 | return false; | |
1379 | ||
1380 | scsi_cmnd_io io_hdr; | |
1381 | unsigned char cdb[12]; | |
1382 | ||
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; | |
1387 | ||
1388 | cdb[ 0] = 0xf8; | |
1389 | cdb[ 1] = 0x00; | |
1390 | cdb[ 2] = 0x23; // Subcommand: Pass through presetting | |
1391 | cdb[ 3] = 0x00; | |
1392 | cdb[ 4] = 0x00; | |
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; | |
1398 | cdb[10] = 0x00; | |
1399 | cdb[11] = 0x00; | |
1400 | ||
1401 | io_hdr.cmnd = cdb; | |
1402 | io_hdr.cmnd_len = sizeof(cdb); | |
1403 | ||
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()); | |
1408 | } | |
1409 | ||
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; | |
1416 | protocol = 0x00; | |
1417 | break; | |
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); | |
1423 | protocol = 0x10; | |
1424 | break; | |
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; | |
1429 | protocol = 0x11; | |
1430 | break; | |
1431 | default: | |
1432 | return set_err(EINVAL); | |
1433 | } | |
1434 | ||
1435 | cdb[ 0] = 0xf8; | |
1436 | cdb[ 1] = 0x00; | |
1437 | cdb[ 2] = 0x22; // Subcommand: Pass through | |
1438 | cdb[ 3] = protocol; | |
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; | |
1447 | ||
1448 | io_hdr.cmnd = cdb; | |
1449 | io_hdr.cmnd_len = sizeof(cdb); | |
1450 | ||
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()); | |
1456 | ||
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; | |
1464 | ||
1465 | cdb[ 0] = 0xf8; | |
1466 | cdb[ 1] = 0x00; | |
1467 | cdb[ 2] = 0x21; // Subcommand: Get status | |
1468 | memset(cdb+3, 0, sizeof(cdb)-3); | |
1469 | io_hdr.cmnd = cdb; | |
1470 | io_hdr.cmnd_len = sizeof(cdb); | |
1471 | ||
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()); | |
1475 | ||
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]; | |
1483 | } | |
1484 | ||
1485 | return true; | |
1486 | } | |
1487 | ||
1488 | ||
1489 | } // namespace | |
1490 | ||
1491 | using namespace sat; | |
1492 | ||
1493 | ||
1494 | ///////////////////////////////////////////////////////////////////////////// | |
1495 | ||
1496 | // Return ATA->SCSI filter for SAT or USB. | |
1497 | ||
1498 | ata_device * smart_interface::get_sat_device(const char * type, scsi_device * scsidev) | |
1499 | { | |
a86ec89e GI |
1500 | if (!scsidev) |
1501 | throw std::logic_error("smart_interface: get_sat_device() called with scsidev=0"); | |
1502 | ||
1503 | // Take temporary ownership of 'scsidev' to delete it on error | |
1504 | scsi_device_auto_ptr scsidev_holder(scsidev); | |
1505 | ata_device * satdev = 0; | |
1506 | ||
2127e193 | 1507 | if (!strncmp(type, "sat", 3)) { |
d008864d GI |
1508 | const char * t = type + 3; |
1509 | bool enable_auto = false; | |
1510 | if (!strncmp(t, ",auto", 5)) { | |
1511 | t += 5; | |
1512 | enable_auto = true; | |
1513 | } | |
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"); | |
2127e193 GI |
1518 | return 0; |
1519 | } | |
a86ec89e | 1520 | satdev = new sat_device(this, scsidev, type, ptlen, enable_auto); |
2127e193 GI |
1521 | } |
1522 | ||
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"); | |
1529 | return 0; | |
1530 | } | |
a86ec89e | 1531 | satdev = new usbcypress_device(this, scsidev, type, signature); |
2127e193 GI |
1532 | } |
1533 | ||
1534 | else if (!strncmp(type, "usbjmicron", 10)) { | |
1535 | const char * t = type + 10; | |
ee38a438 GI |
1536 | bool prolific = false; |
1537 | if (!strncmp(t, ",p", 2)) { | |
1538 | t += 2; | |
1539 | prolific = true; | |
1540 | } | |
2127e193 GI |
1541 | bool ata_48bit_support = false; |
1542 | if (!strncmp(t, ",x", 2)) { | |
1543 | t += 2; | |
1544 | ata_48bit_support = true; | |
1545 | } | |
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))) { | |
ee38a438 | 1549 | set_err(EINVAL, "Option '-d usbjmicron[,p][,x],<n>' requires <n> to be 0 or 1"); |
2127e193 GI |
1550 | return 0; |
1551 | } | |
a86ec89e GI |
1552 | satdev = new usbjmicron_device(this, scsidev, type, prolific, ata_48bit_support, port); |
1553 | } | |
1554 | ||
1555 | else if (!strcmp(type, "usbprolific")) { | |
1556 | satdev = new usbprolific_device(this, scsidev, type); | |
2127e193 GI |
1557 | } |
1558 | ||
3d8ad6fa JD |
1559 | else if (!strcmp(type, "usbprolific")) { |
1560 | return new usbprolific_device(this, scsidev, type); | |
1561 | } | |
1562 | ||
2127e193 | 1563 | else if (!strcmp(type, "usbsunplus")) { |
a86ec89e | 1564 | satdev = new usbsunplus_device(this, scsidev, type); |
2127e193 GI |
1565 | } |
1566 | ||
1567 | else { | |
1568 | set_err(EINVAL, "Unknown USB device type '%s'", type); | |
1569 | return 0; | |
1570 | } | |
a86ec89e GI |
1571 | |
1572 | // 'scsidev' is now owned by 'satdev' | |
1573 | scsidev_holder.release(); | |
1574 | return satdev; | |
2127e193 GI |
1575 | } |
1576 | ||
1577 | // Try to detect a SAT device behind a SCSI interface. | |
1578 | ||
1579 | ata_device * smart_interface::autodetect_sat_device(scsi_device * scsidev, | |
1580 | const unsigned char * inqdata, unsigned inqsize) | |
1581 | { | |
1582 | if (!scsidev->is_open()) | |
1583 | return 0; | |
1584 | ||
bed94269 GI |
1585 | // SAT ? |
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 | |
2127e193 GI |
1590 | } |
1591 | ||
1592 | return 0; | |
1593 | } | |
1594 | ||
1595 | ||
1596 | ///////////////////////////////////////////////////////////////////////////// | |
1597 | // USB device type detection | |
1598 | ||
2127e193 GI |
1599 | // Format USB ID for error messages |
1600 | static std::string format_usb_id(int vendor_id, int product_id, int version) | |
1601 | { | |
1602 | if (version >= 0) | |
1603 | return strprintf("[0x%04x:0x%04x (0x%03x)]", vendor_id, product_id, version); | |
1604 | else | |
1605 | return strprintf("[0x%04x:0x%04x]", vendor_id, product_id); | |
1606 | } | |
1607 | ||
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*/) | |
1611 | { | |
e9583e0c GI |
1612 | usb_dev_info info, info2; |
1613 | int n = lookup_usb_device(vendor_id, product_id, version, info, info2); | |
2127e193 | 1614 | |
e9583e0c | 1615 | if (n <= 0) { |
2127e193 GI |
1616 | set_err(EINVAL, "Unknown USB bridge %s", |
1617 | format_usb_id(vendor_id, product_id, version).c_str()); | |
1618 | return 0; | |
1619 | } | |
e9583e0c GI |
1620 | |
1621 | if (n > 1) { | |
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]")); | |
1626 | return 0; | |
1627 | } | |
1628 | ||
1629 | if (info.usb_type.empty()) { | |
2127e193 GI |
1630 | set_err(ENOSYS, "Unsupported USB bridge %s", |
1631 | format_usb_id(vendor_id, product_id, version).c_str()); | |
1632 | return 0; | |
1633 | } | |
e9583e0c GI |
1634 | |
1635 | // TODO: change return type to std::string | |
1636 | static std::string type; | |
1637 | type = info.usb_type; | |
1638 | return type.c_str(); | |
2127e193 | 1639 | } |