Deleted Added
full compact
ata_all.c (199178) ata_all.c (199747)
1/*-
2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/cam/ata/ata_all.c 199178 2009-11-11 11:10:36Z mav $");
28__FBSDID("$FreeBSD: head/sys/cam/ata/ata_all.c 199747 2009-11-24 12:47:58Z mav $");
29
30#include <sys/param.h>
31
32#ifdef _KERNEL
33#include <opt_scsi.h>
34
35#include <sys/systm.h>
36#include <sys/libkern.h>
37#include <sys/kernel.h>
38#include <sys/sysctl.h>
39#else
40#include <errno.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#ifndef min
45#define min(a,b) (((a)<(b))?(a):(b))
46#endif
47#endif
48
49#include <cam/cam.h>
50#include <cam/cam_ccb.h>
51#include <cam/cam_queue.h>
52#include <cam/cam_xpt.h>
53#include <sys/ata.h>
54#include <cam/ata/ata_all.h>
55#include <sys/sbuf.h>
56#include <sys/endian.h>
57
58int
59ata_version(int ver)
60{
61 int bit;
62
63 if (ver == 0xffff)
64 return 0;
65 for (bit = 15; bit >= 0; bit--)
66 if (ver & (1<<bit))
67 return bit;
68 return 0;
69}
70
71char *
72ata_op_string(struct ata_cmd *cmd)
73{
74
75 switch (cmd->command) {
76 case 0x00: return ("NOP");
77 case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR");
78 case 0x08: return ("DEVICE_RESET");
79 case 0x20: return ("READ");
80 case 0x24: return ("READ48");
81 case 0x25: return ("READ_DMA48");
82 case 0x26: return ("READ_DMA_QUEUED48");
83 case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
84 case 0x29: return ("READ_MUL48");
85 case 0x2a: return ("READ_STREAM_DMA48");
86 case 0x2b: return ("READ_STREAM48");
87 case 0x2f: return ("READ_LOG_EXT");
88 case 0x30: return ("WRITE");
89 case 0x34: return ("WRITE48");
90 case 0x35: return ("WRITE_DMA48");
91 case 0x36: return ("WRITE_DMA_QUEUED48");
92 case 0x37: return ("SET_MAX_ADDRESS48");
93 case 0x39: return ("WRITE_MUL48");
94 case 0x3a: return ("WRITE_STREAM_DMA48");
95 case 0x3b: return ("WRITE_STREAM48");
96 case 0x3d: return ("WRITE_DMA_FUA");
97 case 0x3e: return ("WRITE_DMA_FUA48");
98 case 0x3f: return ("WRITE_LOG_EXT");
99 case 0x40: return ("READ_VERIFY");
100 case 0x42: return ("READ_VERIFY48");
101 case 0x51: return ("CONFIGURE_STREAM");
102 case 0x60: return ("READ_FPDMA_QUEUED");
103 case 0x61: return ("WRITE_FPDMA_QUEUED");
104 case 0x70: return ("SEEK");
105 case 0x87: return ("CFA_TRANSLATE_SECTOR");
106 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
107 case 0x92: return ("DOWNLOAD_MICROCODE");
108 case 0xa0: return ("PACKET");
109 case 0xa1: return ("ATAPI_IDENTIFY");
110 case 0xa2: return ("SERVICE");
111 case 0xb0: return ("SMART");
112 case 0xb1: return ("DEVICE CONFIGURATION");
113 case 0xc0: return ("CFA_ERASE");
114 case 0xc4: return ("READ_MUL");
115 case 0xc5: return ("WRITE_MUL");
116 case 0xc6: return ("SET_MULTI");
117 case 0xc7: return ("READ_DMA_QUEUED");
118 case 0xc8: return ("READ_DMA");
119 case 0xca: return ("WRITE_DMA");
120 case 0xcc: return ("WRITE_DMA_QUEUED");
121 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
122 case 0xce: return ("WRITE_MULTIPLE_FUA48");
123 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
124 case 0xda: return ("GET_MEDIA_STATUS");
125 case 0xde: return ("MEDIA_LOCK");
126 case 0xdf: return ("MEDIA_UNLOCK");
127 case 0xe0: return ("STANDBY_IMMEDIATE");
128 case 0xe1: return ("IDLE_IMMEDIATE");
129 case 0xe2: return ("STANDBY");
130 case 0xe3: return ("IDLE");
131 case 0xe4: return ("READ_BUFFER/PM");
132 case 0xe5: return ("CHECK_POWER_MODE");
133 case 0xe6: return ("SLEEP");
134 case 0xe7: return ("FLUSHCACHE");
135 case 0xe8: return ("WRITE_PM");
136 case 0xea: return ("FLUSHCACHE48");
137 case 0xec: return ("ATA_IDENTIFY");
138 case 0xed: return ("MEDIA_EJECT");
139 case 0xef:
140 switch (cmd->features) {
141 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
142 case 0x02: return ("SETFEATURES ENABLE WCACHE");
143 case 0x82: return ("SETFEATURES DISABLE WCACHE");
144 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
145 case 0x55: return ("SETFEATURES DISABLE RCACHE");
146 }
147 return "SETFEATURES";
148 case 0xf1: return ("SECURITY_SET_PASSWORD");
149 case 0xf2: return ("SECURITY_UNLOCK");
150 case 0xf3: return ("SECURITY_ERASE_PREPARE");
151 case 0xf4: return ("SECURITY_ERASE_UNIT");
152 case 0xf5: return ("SECURITY_FREE_LOCK");
153 case 0xf6: return ("SECURITY DISABLE PASSWORD");
154 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
155 case 0xf9: return ("SET_MAX_ADDRESS");
156 }
157 return "UNKNOWN";
158}
159
160char *
161ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
162{
163
164 snprintf(cmd_string, len, "%02x %02x %02x %02x "
165 "%02x %02x %02x %02x %02x %02x %02x %02x",
166 cmd->command, cmd->features,
167 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
168 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
169 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
170
171 return(cmd_string);
172}
173
174char *
175ata_res_string(struct ata_res *res, char *res_string, size_t len)
176{
177
178 snprintf(res_string, len, "%02x %02x %02x %02x "
179 "%02x %02x %02x %02x %02x %02x %02x",
180 res->status, res->error,
181 res->lba_low, res->lba_mid, res->lba_high, res->device,
182 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
183 res->sector_count, res->sector_count_exp);
184
185 return(res_string);
186}
187
188/*
189 * ata_command_sbuf() returns 0 for success and -1 for failure.
190 */
191int
192ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
193{
194 char cmd_str[(12 * 3) + 1];
195
196 sbuf_printf(sb, "CMD: %s: %s",
197 ata_op_string(&ataio->cmd),
198 ata_cmd_string(&ataio->cmd, cmd_str, sizeof(cmd_str)));
199
200 return(0);
201}
202
203/*
204 * ata_status_abuf() returns 0 for success and -1 for failure.
205 */
206int
207ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
208{
209
210 sbuf_printf(sb, "ATA Status: %02x (%s%s%s%s%s%s%s%s)",
211 ataio->res.status,
212 (ataio->res.status & 0x80) ? "BSY " : "",
213 (ataio->res.status & 0x40) ? "DRDY " : "",
214 (ataio->res.status & 0x20) ? "DF " : "",
215 (ataio->res.status & 0x10) ? "SERV " : "",
216 (ataio->res.status & 0x08) ? "DRQ " : "",
217 (ataio->res.status & 0x04) ? "CORR " : "",
218 (ataio->res.status & 0x02) ? "IDX " : "",
219 (ataio->res.status & 0x01) ? "ERR" : "");
220 if (ataio->res.status & 1) {
221 sbuf_printf(sb, ", Error: %02x (%s%s%s%s%s%s%s%s)",
222 ataio->res.error,
223 (ataio->res.error & 0x80) ? "ICRC " : "",
224 (ataio->res.error & 0x40) ? "UNC " : "",
225 (ataio->res.error & 0x20) ? "MC " : "",
226 (ataio->res.error & 0x10) ? "IDNF " : "",
227 (ataio->res.error & 0x08) ? "MCR " : "",
228 (ataio->res.error & 0x04) ? "ABRT " : "",
229 (ataio->res.error & 0x02) ? "NM " : "",
230 (ataio->res.error & 0x01) ? "ILI" : "");
231 }
232
233 return(0);
234}
235
236/*
237 * ata_res_sbuf() returns 0 for success and -1 for failure.
238 */
239int
240ata_res_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
241{
242 char res_str[(11 * 3) + 1];
243
244 sbuf_printf(sb, "RES: %s",
245 ata_res_string(&ataio->res, res_str, sizeof(res_str)));
246
247 return(0);
248}
249
250void
251ata_print_ident(struct ata_params *ident_data)
252{
253 char product[48], revision[16];
254
255 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
256 sizeof(product));
257 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
258 sizeof(revision));
259 printf("<%s %s> ATA/ATAPI-%d",
260 product, revision, ata_version(ident_data->version_major));
261 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
262 if (ident_data->satacapabilities & ATA_SATA_GEN3)
263 printf(" SATA 3.x");
264 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
265 printf(" SATA 2.x");
266 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
267 printf(" SATA 1.x");
268 else
269 printf(" SATA");
270 }
271 printf(" device\n");
272}
273
274uint32_t
275ata_logical_sector_size(struct ata_params *ident_data)
276{
277 if ((ident_data->pss & 0xc000) == 0x4000 &&
278 (ident_data->pss & ATA_PSS_LSSABOVE512)) {
279 return ((u_int32_t)ident_data->lss_1 |
280 ((u_int32_t)ident_data->lss_2 << 16));
281 }
282 return (512);
283}
284
285uint64_t
286ata_physical_sector_size(struct ata_params *ident_data)
287{
288 if ((ident_data->pss & 0xc000) == 0x4000 &&
289 (ident_data->pss & ATA_PSS_MULTLS)) {
290 return ((uint64_t)ata_logical_sector_size(ident_data) *
291 (1 << (ident_data->pss & ATA_PSS_LSPPS)));
292 }
293 return (512);
294}
295
296uint64_t
297ata_logical_sector_offset(struct ata_params *ident_data)
298{
299 if ((ident_data->lsalign & 0xc000) == 0x4000) {
300 return ((uint64_t)ata_logical_sector_size(ident_data) *
301 (ident_data->lsalign & 0x3fff));
302 }
303 return (0);
304}
305
306void
307ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
308 uint32_t lba, uint8_t sector_count)
309{
310 bzero(&ataio->cmd, sizeof(ataio->cmd));
311 ataio->cmd.flags = 0;
312 ataio->cmd.command = cmd;
313 ataio->cmd.features = features;
314 ataio->cmd.lba_low = lba;
315 ataio->cmd.lba_mid = lba >> 8;
316 ataio->cmd.lba_high = lba >> 16;
317 ataio->cmd.device = 0x40 | ((lba >> 24) & 0x0f);
318 ataio->cmd.sector_count = sector_count;
319}
320
321void
322ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
323 uint64_t lba, uint16_t sector_count)
324{
325 bzero(&ataio->cmd, sizeof(ataio->cmd));
326 ataio->cmd.flags = CAM_ATAIO_48BIT;
327 ataio->cmd.command = cmd;
328 ataio->cmd.features = features;
329 ataio->cmd.lba_low = lba;
330 ataio->cmd.lba_mid = lba >> 8;
331 ataio->cmd.lba_high = lba >> 16;
332 ataio->cmd.device = 0x40;
333 ataio->cmd.lba_low_exp = lba >> 24;
334 ataio->cmd.lba_mid_exp = lba >> 32;
335 ataio->cmd.lba_high_exp = lba >> 40;
336 ataio->cmd.features_exp = features >> 8;
337 ataio->cmd.sector_count = sector_count;
338 ataio->cmd.sector_count_exp = sector_count >> 8;
339}
340
341void
342ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
343 uint64_t lba, uint16_t sector_count)
344{
345 bzero(&ataio->cmd, sizeof(ataio->cmd));
346 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
347 ataio->cmd.command = cmd;
348 ataio->cmd.features = sector_count;
349 ataio->cmd.lba_low = lba;
350 ataio->cmd.lba_mid = lba >> 8;
351 ataio->cmd.lba_high = lba >> 16;
352 ataio->cmd.device = 0x40;
353 ataio->cmd.lba_low_exp = lba >> 24;
354 ataio->cmd.lba_mid_exp = lba >> 32;
355 ataio->cmd.lba_high_exp = lba >> 40;
356 ataio->cmd.features_exp = sector_count >> 8;
357}
358
359void
360ata_reset_cmd(struct ccb_ataio *ataio)
361{
362 bzero(&ataio->cmd, sizeof(ataio->cmd));
363 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
364 ataio->cmd.control = 0x04;
365}
366
367void
368ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
369{
370 bzero(&ataio->cmd, sizeof(ataio->cmd));
371 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
372 ataio->cmd.command = ATA_READ_PM;
373 ataio->cmd.features = reg;
374 ataio->cmd.device = port & 0x0f;
375}
376
377void
378ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
379{
380 bzero(&ataio->cmd, sizeof(ataio->cmd));
381 ataio->cmd.flags = 0;
382 ataio->cmd.command = ATA_WRITE_PM;
383 ataio->cmd.features = reg;
384 ataio->cmd.sector_count = val;
385 ataio->cmd.lba_low = val >> 8;
386 ataio->cmd.lba_mid = val >> 16;
387 ataio->cmd.lba_high = val >> 24;
388 ataio->cmd.device = port & 0x0f;
389}
390
391void
392ata_bswap(int8_t *buf, int len)
393{
394 u_int16_t *ptr = (u_int16_t*)(buf + len);
395
396 while (--ptr >= (u_int16_t*)buf)
397 *ptr = be16toh(*ptr);
398}
399
400void
401ata_btrim(int8_t *buf, int len)
402{
403 int8_t *ptr;
404
405 for (ptr = buf; ptr < buf+len; ++ptr)
406 if (!*ptr || *ptr == '_')
407 *ptr = ' ';
408 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
409 *ptr = 0;
410}
411
412void
413ata_bpack(int8_t *src, int8_t *dst, int len)
414{
415 int i, j, blank;
416
417 for (i = j = blank = 0 ; i < len; i++) {
418 if (blank && src[i] == ' ') continue;
419 if (blank && src[i] != ' ') {
420 dst[j++] = src[i];
421 blank = 0;
422 continue;
423 }
424 if (src[i] == ' ') {
425 blank = 1;
426 if (i == 0)
427 continue;
428 }
429 dst[j++] = src[i];
430 }
431 while (j < len)
432 dst[j++] = 0x00;
433}
434
435int
436ata_max_pmode(struct ata_params *ap)
437{
438 if (ap->atavalid & ATA_FLAG_64_70) {
439 if (ap->apiomodes & 0x02)
440 return ATA_PIO4;
441 if (ap->apiomodes & 0x01)
442 return ATA_PIO3;
443 }
444 if (ap->mwdmamodes & 0x04)
445 return ATA_PIO4;
446 if (ap->mwdmamodes & 0x02)
447 return ATA_PIO3;
448 if (ap->mwdmamodes & 0x01)
449 return ATA_PIO2;
450 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
451 return ATA_PIO2;
452 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
453 return ATA_PIO1;
454 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
455 return ATA_PIO0;
456 return ATA_PIO0;
457}
458
459int
460ata_max_wmode(struct ata_params *ap)
461{
462 if (ap->mwdmamodes & 0x04)
463 return ATA_WDMA2;
464 if (ap->mwdmamodes & 0x02)
465 return ATA_WDMA1;
466 if (ap->mwdmamodes & 0x01)
467 return ATA_WDMA0;
468 return -1;
469}
470
471int
472ata_max_umode(struct ata_params *ap)
473{
474 if (ap->atavalid & ATA_FLAG_88) {
475 if (ap->udmamodes & 0x40)
476 return ATA_UDMA6;
477 if (ap->udmamodes & 0x20)
478 return ATA_UDMA5;
479 if (ap->udmamodes & 0x10)
480 return ATA_UDMA4;
481 if (ap->udmamodes & 0x08)
482 return ATA_UDMA3;
483 if (ap->udmamodes & 0x04)
484 return ATA_UDMA2;
485 if (ap->udmamodes & 0x02)
486 return ATA_UDMA1;
487 if (ap->udmamodes & 0x01)
488 return ATA_UDMA0;
489 }
490 return -1;
491}
492
493int
29
30#include <sys/param.h>
31
32#ifdef _KERNEL
33#include <opt_scsi.h>
34
35#include <sys/systm.h>
36#include <sys/libkern.h>
37#include <sys/kernel.h>
38#include <sys/sysctl.h>
39#else
40#include <errno.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#ifndef min
45#define min(a,b) (((a)<(b))?(a):(b))
46#endif
47#endif
48
49#include <cam/cam.h>
50#include <cam/cam_ccb.h>
51#include <cam/cam_queue.h>
52#include <cam/cam_xpt.h>
53#include <sys/ata.h>
54#include <cam/ata/ata_all.h>
55#include <sys/sbuf.h>
56#include <sys/endian.h>
57
58int
59ata_version(int ver)
60{
61 int bit;
62
63 if (ver == 0xffff)
64 return 0;
65 for (bit = 15; bit >= 0; bit--)
66 if (ver & (1<<bit))
67 return bit;
68 return 0;
69}
70
71char *
72ata_op_string(struct ata_cmd *cmd)
73{
74
75 switch (cmd->command) {
76 case 0x00: return ("NOP");
77 case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR");
78 case 0x08: return ("DEVICE_RESET");
79 case 0x20: return ("READ");
80 case 0x24: return ("READ48");
81 case 0x25: return ("READ_DMA48");
82 case 0x26: return ("READ_DMA_QUEUED48");
83 case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
84 case 0x29: return ("READ_MUL48");
85 case 0x2a: return ("READ_STREAM_DMA48");
86 case 0x2b: return ("READ_STREAM48");
87 case 0x2f: return ("READ_LOG_EXT");
88 case 0x30: return ("WRITE");
89 case 0x34: return ("WRITE48");
90 case 0x35: return ("WRITE_DMA48");
91 case 0x36: return ("WRITE_DMA_QUEUED48");
92 case 0x37: return ("SET_MAX_ADDRESS48");
93 case 0x39: return ("WRITE_MUL48");
94 case 0x3a: return ("WRITE_STREAM_DMA48");
95 case 0x3b: return ("WRITE_STREAM48");
96 case 0x3d: return ("WRITE_DMA_FUA");
97 case 0x3e: return ("WRITE_DMA_FUA48");
98 case 0x3f: return ("WRITE_LOG_EXT");
99 case 0x40: return ("READ_VERIFY");
100 case 0x42: return ("READ_VERIFY48");
101 case 0x51: return ("CONFIGURE_STREAM");
102 case 0x60: return ("READ_FPDMA_QUEUED");
103 case 0x61: return ("WRITE_FPDMA_QUEUED");
104 case 0x70: return ("SEEK");
105 case 0x87: return ("CFA_TRANSLATE_SECTOR");
106 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
107 case 0x92: return ("DOWNLOAD_MICROCODE");
108 case 0xa0: return ("PACKET");
109 case 0xa1: return ("ATAPI_IDENTIFY");
110 case 0xa2: return ("SERVICE");
111 case 0xb0: return ("SMART");
112 case 0xb1: return ("DEVICE CONFIGURATION");
113 case 0xc0: return ("CFA_ERASE");
114 case 0xc4: return ("READ_MUL");
115 case 0xc5: return ("WRITE_MUL");
116 case 0xc6: return ("SET_MULTI");
117 case 0xc7: return ("READ_DMA_QUEUED");
118 case 0xc8: return ("READ_DMA");
119 case 0xca: return ("WRITE_DMA");
120 case 0xcc: return ("WRITE_DMA_QUEUED");
121 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
122 case 0xce: return ("WRITE_MULTIPLE_FUA48");
123 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
124 case 0xda: return ("GET_MEDIA_STATUS");
125 case 0xde: return ("MEDIA_LOCK");
126 case 0xdf: return ("MEDIA_UNLOCK");
127 case 0xe0: return ("STANDBY_IMMEDIATE");
128 case 0xe1: return ("IDLE_IMMEDIATE");
129 case 0xe2: return ("STANDBY");
130 case 0xe3: return ("IDLE");
131 case 0xe4: return ("READ_BUFFER/PM");
132 case 0xe5: return ("CHECK_POWER_MODE");
133 case 0xe6: return ("SLEEP");
134 case 0xe7: return ("FLUSHCACHE");
135 case 0xe8: return ("WRITE_PM");
136 case 0xea: return ("FLUSHCACHE48");
137 case 0xec: return ("ATA_IDENTIFY");
138 case 0xed: return ("MEDIA_EJECT");
139 case 0xef:
140 switch (cmd->features) {
141 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
142 case 0x02: return ("SETFEATURES ENABLE WCACHE");
143 case 0x82: return ("SETFEATURES DISABLE WCACHE");
144 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
145 case 0x55: return ("SETFEATURES DISABLE RCACHE");
146 }
147 return "SETFEATURES";
148 case 0xf1: return ("SECURITY_SET_PASSWORD");
149 case 0xf2: return ("SECURITY_UNLOCK");
150 case 0xf3: return ("SECURITY_ERASE_PREPARE");
151 case 0xf4: return ("SECURITY_ERASE_UNIT");
152 case 0xf5: return ("SECURITY_FREE_LOCK");
153 case 0xf6: return ("SECURITY DISABLE PASSWORD");
154 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
155 case 0xf9: return ("SET_MAX_ADDRESS");
156 }
157 return "UNKNOWN";
158}
159
160char *
161ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
162{
163
164 snprintf(cmd_string, len, "%02x %02x %02x %02x "
165 "%02x %02x %02x %02x %02x %02x %02x %02x",
166 cmd->command, cmd->features,
167 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
168 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
169 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
170
171 return(cmd_string);
172}
173
174char *
175ata_res_string(struct ata_res *res, char *res_string, size_t len)
176{
177
178 snprintf(res_string, len, "%02x %02x %02x %02x "
179 "%02x %02x %02x %02x %02x %02x %02x",
180 res->status, res->error,
181 res->lba_low, res->lba_mid, res->lba_high, res->device,
182 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
183 res->sector_count, res->sector_count_exp);
184
185 return(res_string);
186}
187
188/*
189 * ata_command_sbuf() returns 0 for success and -1 for failure.
190 */
191int
192ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
193{
194 char cmd_str[(12 * 3) + 1];
195
196 sbuf_printf(sb, "CMD: %s: %s",
197 ata_op_string(&ataio->cmd),
198 ata_cmd_string(&ataio->cmd, cmd_str, sizeof(cmd_str)));
199
200 return(0);
201}
202
203/*
204 * ata_status_abuf() returns 0 for success and -1 for failure.
205 */
206int
207ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
208{
209
210 sbuf_printf(sb, "ATA Status: %02x (%s%s%s%s%s%s%s%s)",
211 ataio->res.status,
212 (ataio->res.status & 0x80) ? "BSY " : "",
213 (ataio->res.status & 0x40) ? "DRDY " : "",
214 (ataio->res.status & 0x20) ? "DF " : "",
215 (ataio->res.status & 0x10) ? "SERV " : "",
216 (ataio->res.status & 0x08) ? "DRQ " : "",
217 (ataio->res.status & 0x04) ? "CORR " : "",
218 (ataio->res.status & 0x02) ? "IDX " : "",
219 (ataio->res.status & 0x01) ? "ERR" : "");
220 if (ataio->res.status & 1) {
221 sbuf_printf(sb, ", Error: %02x (%s%s%s%s%s%s%s%s)",
222 ataio->res.error,
223 (ataio->res.error & 0x80) ? "ICRC " : "",
224 (ataio->res.error & 0x40) ? "UNC " : "",
225 (ataio->res.error & 0x20) ? "MC " : "",
226 (ataio->res.error & 0x10) ? "IDNF " : "",
227 (ataio->res.error & 0x08) ? "MCR " : "",
228 (ataio->res.error & 0x04) ? "ABRT " : "",
229 (ataio->res.error & 0x02) ? "NM " : "",
230 (ataio->res.error & 0x01) ? "ILI" : "");
231 }
232
233 return(0);
234}
235
236/*
237 * ata_res_sbuf() returns 0 for success and -1 for failure.
238 */
239int
240ata_res_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
241{
242 char res_str[(11 * 3) + 1];
243
244 sbuf_printf(sb, "RES: %s",
245 ata_res_string(&ataio->res, res_str, sizeof(res_str)));
246
247 return(0);
248}
249
250void
251ata_print_ident(struct ata_params *ident_data)
252{
253 char product[48], revision[16];
254
255 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
256 sizeof(product));
257 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
258 sizeof(revision));
259 printf("<%s %s> ATA/ATAPI-%d",
260 product, revision, ata_version(ident_data->version_major));
261 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
262 if (ident_data->satacapabilities & ATA_SATA_GEN3)
263 printf(" SATA 3.x");
264 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
265 printf(" SATA 2.x");
266 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
267 printf(" SATA 1.x");
268 else
269 printf(" SATA");
270 }
271 printf(" device\n");
272}
273
274uint32_t
275ata_logical_sector_size(struct ata_params *ident_data)
276{
277 if ((ident_data->pss & 0xc000) == 0x4000 &&
278 (ident_data->pss & ATA_PSS_LSSABOVE512)) {
279 return ((u_int32_t)ident_data->lss_1 |
280 ((u_int32_t)ident_data->lss_2 << 16));
281 }
282 return (512);
283}
284
285uint64_t
286ata_physical_sector_size(struct ata_params *ident_data)
287{
288 if ((ident_data->pss & 0xc000) == 0x4000 &&
289 (ident_data->pss & ATA_PSS_MULTLS)) {
290 return ((uint64_t)ata_logical_sector_size(ident_data) *
291 (1 << (ident_data->pss & ATA_PSS_LSPPS)));
292 }
293 return (512);
294}
295
296uint64_t
297ata_logical_sector_offset(struct ata_params *ident_data)
298{
299 if ((ident_data->lsalign & 0xc000) == 0x4000) {
300 return ((uint64_t)ata_logical_sector_size(ident_data) *
301 (ident_data->lsalign & 0x3fff));
302 }
303 return (0);
304}
305
306void
307ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
308 uint32_t lba, uint8_t sector_count)
309{
310 bzero(&ataio->cmd, sizeof(ataio->cmd));
311 ataio->cmd.flags = 0;
312 ataio->cmd.command = cmd;
313 ataio->cmd.features = features;
314 ataio->cmd.lba_low = lba;
315 ataio->cmd.lba_mid = lba >> 8;
316 ataio->cmd.lba_high = lba >> 16;
317 ataio->cmd.device = 0x40 | ((lba >> 24) & 0x0f);
318 ataio->cmd.sector_count = sector_count;
319}
320
321void
322ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
323 uint64_t lba, uint16_t sector_count)
324{
325 bzero(&ataio->cmd, sizeof(ataio->cmd));
326 ataio->cmd.flags = CAM_ATAIO_48BIT;
327 ataio->cmd.command = cmd;
328 ataio->cmd.features = features;
329 ataio->cmd.lba_low = lba;
330 ataio->cmd.lba_mid = lba >> 8;
331 ataio->cmd.lba_high = lba >> 16;
332 ataio->cmd.device = 0x40;
333 ataio->cmd.lba_low_exp = lba >> 24;
334 ataio->cmd.lba_mid_exp = lba >> 32;
335 ataio->cmd.lba_high_exp = lba >> 40;
336 ataio->cmd.features_exp = features >> 8;
337 ataio->cmd.sector_count = sector_count;
338 ataio->cmd.sector_count_exp = sector_count >> 8;
339}
340
341void
342ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
343 uint64_t lba, uint16_t sector_count)
344{
345 bzero(&ataio->cmd, sizeof(ataio->cmd));
346 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
347 ataio->cmd.command = cmd;
348 ataio->cmd.features = sector_count;
349 ataio->cmd.lba_low = lba;
350 ataio->cmd.lba_mid = lba >> 8;
351 ataio->cmd.lba_high = lba >> 16;
352 ataio->cmd.device = 0x40;
353 ataio->cmd.lba_low_exp = lba >> 24;
354 ataio->cmd.lba_mid_exp = lba >> 32;
355 ataio->cmd.lba_high_exp = lba >> 40;
356 ataio->cmd.features_exp = sector_count >> 8;
357}
358
359void
360ata_reset_cmd(struct ccb_ataio *ataio)
361{
362 bzero(&ataio->cmd, sizeof(ataio->cmd));
363 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
364 ataio->cmd.control = 0x04;
365}
366
367void
368ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
369{
370 bzero(&ataio->cmd, sizeof(ataio->cmd));
371 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
372 ataio->cmd.command = ATA_READ_PM;
373 ataio->cmd.features = reg;
374 ataio->cmd.device = port & 0x0f;
375}
376
377void
378ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
379{
380 bzero(&ataio->cmd, sizeof(ataio->cmd));
381 ataio->cmd.flags = 0;
382 ataio->cmd.command = ATA_WRITE_PM;
383 ataio->cmd.features = reg;
384 ataio->cmd.sector_count = val;
385 ataio->cmd.lba_low = val >> 8;
386 ataio->cmd.lba_mid = val >> 16;
387 ataio->cmd.lba_high = val >> 24;
388 ataio->cmd.device = port & 0x0f;
389}
390
391void
392ata_bswap(int8_t *buf, int len)
393{
394 u_int16_t *ptr = (u_int16_t*)(buf + len);
395
396 while (--ptr >= (u_int16_t*)buf)
397 *ptr = be16toh(*ptr);
398}
399
400void
401ata_btrim(int8_t *buf, int len)
402{
403 int8_t *ptr;
404
405 for (ptr = buf; ptr < buf+len; ++ptr)
406 if (!*ptr || *ptr == '_')
407 *ptr = ' ';
408 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
409 *ptr = 0;
410}
411
412void
413ata_bpack(int8_t *src, int8_t *dst, int len)
414{
415 int i, j, blank;
416
417 for (i = j = blank = 0 ; i < len; i++) {
418 if (blank && src[i] == ' ') continue;
419 if (blank && src[i] != ' ') {
420 dst[j++] = src[i];
421 blank = 0;
422 continue;
423 }
424 if (src[i] == ' ') {
425 blank = 1;
426 if (i == 0)
427 continue;
428 }
429 dst[j++] = src[i];
430 }
431 while (j < len)
432 dst[j++] = 0x00;
433}
434
435int
436ata_max_pmode(struct ata_params *ap)
437{
438 if (ap->atavalid & ATA_FLAG_64_70) {
439 if (ap->apiomodes & 0x02)
440 return ATA_PIO4;
441 if (ap->apiomodes & 0x01)
442 return ATA_PIO3;
443 }
444 if (ap->mwdmamodes & 0x04)
445 return ATA_PIO4;
446 if (ap->mwdmamodes & 0x02)
447 return ATA_PIO3;
448 if (ap->mwdmamodes & 0x01)
449 return ATA_PIO2;
450 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
451 return ATA_PIO2;
452 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
453 return ATA_PIO1;
454 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
455 return ATA_PIO0;
456 return ATA_PIO0;
457}
458
459int
460ata_max_wmode(struct ata_params *ap)
461{
462 if (ap->mwdmamodes & 0x04)
463 return ATA_WDMA2;
464 if (ap->mwdmamodes & 0x02)
465 return ATA_WDMA1;
466 if (ap->mwdmamodes & 0x01)
467 return ATA_WDMA0;
468 return -1;
469}
470
471int
472ata_max_umode(struct ata_params *ap)
473{
474 if (ap->atavalid & ATA_FLAG_88) {
475 if (ap->udmamodes & 0x40)
476 return ATA_UDMA6;
477 if (ap->udmamodes & 0x20)
478 return ATA_UDMA5;
479 if (ap->udmamodes & 0x10)
480 return ATA_UDMA4;
481 if (ap->udmamodes & 0x08)
482 return ATA_UDMA3;
483 if (ap->udmamodes & 0x04)
484 return ATA_UDMA2;
485 if (ap->udmamodes & 0x02)
486 return ATA_UDMA1;
487 if (ap->udmamodes & 0x01)
488 return ATA_UDMA0;
489 }
490 return -1;
491}
492
493int
494ata_max_mode(struct ata_params *ap, int mode, int maxmode)
494ata_max_mode(struct ata_params *ap, int maxmode)
495{
496
495{
496
497 if (maxmode && mode > maxmode)
498 mode = maxmode;
497 if (maxmode == 0)
498 maxmode = ATA_DMA_MAX;
499 if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
500 return (min(maxmode, ata_max_umode(ap)));
501 if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
502 return (min(maxmode, ata_max_wmode(ap)));
503 return (min(maxmode, ata_max_pmode(ap)));
504}
499
505
500 if (mode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
501 return (min(mode, ata_max_umode(ap)));
506char *
507ata_mode2string(int mode)
508{
509 switch (mode) {
510 case -1: return "UNSUPPORTED";
511 case 0: return "NONE";
512 case ATA_PIO0: return "PIO0";
513 case ATA_PIO1: return "PIO1";
514 case ATA_PIO2: return "PIO2";
515 case ATA_PIO3: return "PIO3";
516 case ATA_PIO4: return "PIO4";
517 case ATA_WDMA0: return "WDMA0";
518 case ATA_WDMA1: return "WDMA1";
519 case ATA_WDMA2: return "WDMA2";
520 case ATA_UDMA0: return "UDMA0";
521 case ATA_UDMA1: return "UDMA1";
522 case ATA_UDMA2: return "UDMA2";
523 case ATA_UDMA3: return "UDMA3";
524 case ATA_UDMA4: return "UDMA4";
525 case ATA_UDMA5: return "UDMA5";
526 case ATA_UDMA6: return "UDMA6";
527 default:
528 if (mode & ATA_DMA_MASK)
529 return "BIOSDMA";
530 else
531 return "BIOSPIO";
532 }
533}
502
534
503 if (mode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
504 return (min(mode, ata_max_wmode(ap)));
535u_int
536ata_mode2speed(int mode)
537{
538 switch (mode) {
539 case ATA_PIO0:
540 default:
541 return (3300);
542 case ATA_PIO1:
543 return (5200);
544 case ATA_PIO2:
545 return (8300);
546 case ATA_PIO3:
547 return (11100);
548 case ATA_PIO4:
549 return (16700);
550 case ATA_WDMA0:
551 return (4200);
552 case ATA_WDMA1:
553 return (13300);
554 case ATA_WDMA2:
555 return (16700);
556 case ATA_UDMA0:
557 return (16700);
558 case ATA_UDMA1:
559 return (25000);
560 case ATA_UDMA2:
561 return (33300);
562 case ATA_UDMA3:
563 return (44400);
564 case ATA_UDMA4:
565 return (66700);
566 case ATA_UDMA5:
567 return (100000);
568 case ATA_UDMA6:
569 return (133000);
570 }
571}
505
572
506 if (mode > ata_max_pmode(ap))
507 return (min(mode, ata_max_pmode(ap)));
573u_int
574ata_revision2speed(int revision)
575{
576 switch (revision) {
577 case 1:
578 default:
579 return (150000);
580 case 2:
581 return (300000);
582 case 3:
583 return (600000);
584 }
585}
508
586
509 return (mode);
587int
588ata_speed2revision(u_int speed)
589{
590 switch (speed) {
591 case 150000:
592 default:
593 return (1);
594 case 300000:
595 return (2);
596 case 600000:
597 return (3);
598 }
510}
511
512int
513ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
514{
515 struct scsi_inquiry_pattern *entry;
516 struct ata_params *ident;
517
518 entry = (struct scsi_inquiry_pattern *)table_entry;
519 ident = (struct ata_params *)identbuffer;
520
521 if ((cam_strmatch(ident->model, entry->product,
522 sizeof(ident->model)) == 0)
523 && (cam_strmatch(ident->revision, entry->revision,
524 sizeof(ident->revision)) == 0)) {
525 return (0);
526 }
527 return (-1);
528}
529
530int
531ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
532{
533 struct scsi_static_inquiry_pattern *entry;
534 struct ata_params *ident;
535
536 entry = (struct scsi_static_inquiry_pattern *)table_entry;
537 ident = (struct ata_params *)identbuffer;
538
539 if ((cam_strmatch(ident->model, entry->product,
540 sizeof(ident->model)) == 0)
541 && (cam_strmatch(ident->revision, entry->revision,
542 sizeof(ident->revision)) == 0)) {
543 return (0);
544 }
545 return (-1);
546}
599}
600
601int
602ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
603{
604 struct scsi_inquiry_pattern *entry;
605 struct ata_params *ident;
606
607 entry = (struct scsi_inquiry_pattern *)table_entry;
608 ident = (struct ata_params *)identbuffer;
609
610 if ((cam_strmatch(ident->model, entry->product,
611 sizeof(ident->model)) == 0)
612 && (cam_strmatch(ident->revision, entry->revision,
613 sizeof(ident->revision)) == 0)) {
614 return (0);
615 }
616 return (-1);
617}
618
619int
620ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
621{
622 struct scsi_static_inquiry_pattern *entry;
623 struct ata_params *ident;
624
625 entry = (struct scsi_static_inquiry_pattern *)table_entry;
626 ident = (struct ata_params *)identbuffer;
627
628 if ((cam_strmatch(ident->model, entry->product,
629 sizeof(ident->model)) == 0)
630 && (cam_strmatch(ident->revision, entry->revision,
631 sizeof(ident->revision)) == 0)) {
632 return (0);
633 }
634 return (-1);
635}