]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cam/ata/ata_all.c
MFC r362623:
[FreeBSD/stable/8.git] / sys / cam / ata / ata_all.c
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$");
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
58 int
59 ata_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
71 char *
72 ata_op_string(struct ata_cmd *cmd)
73 {
74
75         if (cmd->control & 0x04)
76                 return ("SOFT_RESET");
77         switch (cmd->command) {
78         case 0x00: return ("NOP");
79         case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR");
80         case 0x06:
81                 switch (cmd->features) {
82                 case 0x01: return ("DSM TRIM");
83                 }
84                 return "DSM";
85         case 0x08: return ("DEVICE_RESET");
86         case 0x20: return ("READ");
87         case 0x24: return ("READ48");
88         case 0x25: return ("READ_DMA48");
89         case 0x26: return ("READ_DMA_QUEUED48");
90         case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
91         case 0x29: return ("READ_MUL48");
92         case 0x2a: return ("READ_STREAM_DMA48");
93         case 0x2b: return ("READ_STREAM48");
94         case 0x2f: return ("READ_LOG_EXT");
95         case 0x30: return ("WRITE");
96         case 0x34: return ("WRITE48");
97         case 0x35: return ("WRITE_DMA48");
98         case 0x36: return ("WRITE_DMA_QUEUED48");
99         case 0x37: return ("SET_MAX_ADDRESS48");
100         case 0x39: return ("WRITE_MUL48");
101         case 0x3a: return ("WRITE_STREAM_DMA48");
102         case 0x3b: return ("WRITE_STREAM48");
103         case 0x3d: return ("WRITE_DMA_FUA48");
104         case 0x3e: return ("WRITE_DMA_QUEUED_FUA48");
105         case 0x3f: return ("WRITE_LOG_EXT");
106         case 0x40: return ("READ_VERIFY");
107         case 0x42: return ("READ_VERIFY48");
108         case 0x51: return ("CONFIGURE_STREAM");
109         case 0x60: return ("READ_FPDMA_QUEUED");
110         case 0x61: return ("WRITE_FPDMA_QUEUED");
111         case 0x70: return ("SEEK");
112         case 0x87: return ("CFA_TRANSLATE_SECTOR");
113         case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
114         case 0x92: return ("DOWNLOAD_MICROCODE");
115         case 0xa0: return ("PACKET");
116         case 0xa1: return ("ATAPI_IDENTIFY");
117         case 0xa2: return ("SERVICE");
118         case 0xb0: return ("SMART");
119         case 0xb1: return ("DEVICE CONFIGURATION");
120         case 0xc0: return ("CFA_ERASE");
121         case 0xc4: return ("READ_MUL");
122         case 0xc5: return ("WRITE_MUL");
123         case 0xc6: return ("SET_MULTI");
124         case 0xc7: return ("READ_DMA_QUEUED");
125         case 0xc8: return ("READ_DMA");
126         case 0xca: return ("WRITE_DMA");
127         case 0xcc: return ("WRITE_DMA_QUEUED");
128         case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
129         case 0xce: return ("WRITE_MUL_FUA48");
130         case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
131         case 0xda: return ("GET_MEDIA_STATUS");
132         case 0xde: return ("MEDIA_LOCK");
133         case 0xdf: return ("MEDIA_UNLOCK");
134         case 0xe0: return ("STANDBY_IMMEDIATE");
135         case 0xe1: return ("IDLE_IMMEDIATE");
136         case 0xe2: return ("STANDBY");
137         case 0xe3: return ("IDLE");
138         case 0xe4: return ("READ_BUFFER/PM");
139         case 0xe5: return ("CHECK_POWER_MODE");
140         case 0xe6: return ("SLEEP");
141         case 0xe7: return ("FLUSHCACHE");
142         case 0xe8: return ("WRITE_PM");
143         case 0xea: return ("FLUSHCACHE48");
144         case 0xec: return ("ATA_IDENTIFY");
145         case 0xed: return ("MEDIA_EJECT");
146         case 0xef:
147                 switch (cmd->features) {
148                 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
149                 case 0x02: return ("SETFEATURES ENABLE WCACHE");
150                 case 0x82: return ("SETFEATURES DISABLE WCACHE");
151                 case 0x06: return ("SETFEATURES ENABLE PUIS");
152                 case 0x86: return ("SETFEATURES DISABLE PUIS");
153                 case 0x07: return ("SETFEATURES SPIN-UP");
154                 case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
155                 case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
156                 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
157                 case 0x55: return ("SETFEATURES DISABLE RCACHE");
158                 }
159                 return "SETFEATURES";
160         case 0xf1: return ("SECURITY_SET_PASSWORD");
161         case 0xf2: return ("SECURITY_UNLOCK");
162         case 0xf3: return ("SECURITY_ERASE_PREPARE");
163         case 0xf4: return ("SECURITY_ERASE_UNIT");
164         case 0xf5: return ("SECURITY_FREEZE_LOCK");
165         case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
166         case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
167         case 0xf9: return ("SET_MAX_ADDRESS");
168         }
169         return "UNKNOWN";
170 }
171
172 char *
173 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
174 {
175
176         snprintf(cmd_string, len, "%02x %02x %02x %02x "
177             "%02x %02x %02x %02x %02x %02x %02x %02x",
178             cmd->command, cmd->features,
179             cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
180             cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
181             cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
182
183         return(cmd_string);
184 }
185
186 char *
187 ata_res_string(struct ata_res *res, char *res_string, size_t len)
188 {
189
190         snprintf(res_string, len, "%02x %02x %02x %02x "
191             "%02x %02x %02x %02x %02x %02x %02x",
192             res->status, res->error,
193             res->lba_low, res->lba_mid, res->lba_high, res->device,
194             res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
195             res->sector_count, res->sector_count_exp);
196
197         return(res_string);
198 }
199
200 /*
201  * ata_command_sbuf() returns 0 for success and -1 for failure.
202  */
203 int
204 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
205 {
206         char cmd_str[(12 * 3) + 1];
207
208         sbuf_printf(sb, "%s. ACB: %s",
209             ata_op_string(&ataio->cmd),
210             ata_cmd_string(&ataio->cmd, cmd_str, sizeof(cmd_str)));
211
212         return(0);
213 }
214
215 /*
216  * ata_status_abuf() returns 0 for success and -1 for failure.
217  */
218 int
219 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
220 {
221
222         sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
223             ataio->res.status,
224             (ataio->res.status & 0x80) ? "BSY " : "",
225             (ataio->res.status & 0x40) ? "DRDY " : "",
226             (ataio->res.status & 0x20) ? "DF " : "",
227             (ataio->res.status & 0x10) ? "SERV " : "",
228             (ataio->res.status & 0x08) ? "DRQ " : "",
229             (ataio->res.status & 0x04) ? "CORR " : "",
230             (ataio->res.status & 0x02) ? "IDX " : "",
231             (ataio->res.status & 0x01) ? "ERR" : "");
232         if (ataio->res.status & 1) {
233             sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
234                 ataio->res.error,
235                 (ataio->res.error & 0x80) ? "ICRC " : "",
236                 (ataio->res.error & 0x40) ? "UNC " : "",
237                 (ataio->res.error & 0x20) ? "MC " : "",
238                 (ataio->res.error & 0x10) ? "IDNF " : "",
239                 (ataio->res.error & 0x08) ? "MCR " : "",
240                 (ataio->res.error & 0x04) ? "ABRT " : "",
241                 (ataio->res.error & 0x02) ? "NM " : "",
242                 (ataio->res.error & 0x01) ? "ILI" : "");
243         }
244
245         return(0);
246 }
247
248 /*
249  * ata_res_sbuf() returns 0 for success and -1 for failure.
250  */
251 int
252 ata_res_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
253 {
254         char res_str[(11 * 3) + 1];
255
256         sbuf_printf(sb, "RES: %s",
257             ata_res_string(&ataio->res, res_str, sizeof(res_str)));
258
259         return(0);
260 }
261
262 void
263 ata_print_ident(struct ata_params *ident_data)
264 {
265         char product[48], revision[16];
266
267         cam_strvis(product, ident_data->model, sizeof(ident_data->model),
268                    sizeof(product));
269         cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
270                    sizeof(revision));
271         printf("<%s %s> %s-%d",
272             product, revision,
273             (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
274             (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA",
275             ata_version(ident_data->version_major));
276         if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
277                 if (ident_data->satacapabilities & ATA_SATA_GEN3)
278                         printf(" SATA 3.x");
279                 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
280                         printf(" SATA 2.x");
281                 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
282                         printf(" SATA 1.x");
283                 else
284                         printf(" SATA");
285         }
286         printf(" device\n");
287 }
288
289 uint32_t
290 ata_logical_sector_size(struct ata_params *ident_data)
291 {
292         if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
293             (ident_data->pss & ATA_PSS_LSSABOVE512)) {
294                 return (((u_int32_t)ident_data->lss_1 |
295                     ((u_int32_t)ident_data->lss_2 << 16)) * 2);
296         }
297         return (512);
298 }
299
300 uint64_t
301 ata_physical_sector_size(struct ata_params *ident_data)
302 {
303         if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
304                 if (ident_data->pss & ATA_PSS_MULTLS) {
305                         return ((uint64_t)ata_logical_sector_size(ident_data) *
306                             (1 << (ident_data->pss & ATA_PSS_LSPPS)));
307                 } else {
308                         return (uint64_t)ata_logical_sector_size(ident_data);
309                 }
310         }
311         return (512);
312 }
313
314 uint64_t
315 ata_logical_sector_offset(struct ata_params *ident_data)
316 {
317         if ((ident_data->lsalign & 0xc000) == 0x4000) {
318                 return ((uint64_t)ata_logical_sector_size(ident_data) *
319                     (ident_data->lsalign & 0x3fff));
320         }
321         return (0);
322 }
323
324 void
325 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
326     uint32_t lba, uint8_t sector_count)
327 {
328         bzero(&ataio->cmd, sizeof(ataio->cmd));
329         ataio->cmd.flags = 0;
330         if (cmd == ATA_READ_DMA ||
331             cmd == ATA_READ_DMA_QUEUED ||
332             cmd == ATA_WRITE_DMA ||
333             cmd == ATA_WRITE_DMA_QUEUED)
334                 ataio->cmd.flags |= CAM_ATAIO_DMA;
335         ataio->cmd.command = cmd;
336         ataio->cmd.features = features;
337         ataio->cmd.lba_low = lba;
338         ataio->cmd.lba_mid = lba >> 8;
339         ataio->cmd.lba_high = lba >> 16;
340         ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
341         ataio->cmd.sector_count = sector_count;
342 }
343
344 void
345 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
346     uint64_t lba, uint16_t sector_count)
347 {
348         bzero(&ataio->cmd, sizeof(ataio->cmd));
349         ataio->cmd.flags = CAM_ATAIO_48BIT;
350         if (cmd == ATA_READ_DMA48 ||
351             cmd == ATA_READ_DMA_QUEUED48 ||
352             cmd == ATA_READ_STREAM_DMA48 ||
353             cmd == ATA_WRITE_DMA48 ||
354             cmd == ATA_WRITE_DMA_FUA48 ||
355             cmd == ATA_WRITE_DMA_QUEUED48 ||
356             cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
357             cmd == ATA_WRITE_STREAM_DMA48 ||
358             cmd == ATA_DATA_SET_MANAGEMENT)
359                 ataio->cmd.flags |= CAM_ATAIO_DMA;
360         ataio->cmd.command = cmd;
361         ataio->cmd.features = features;
362         ataio->cmd.lba_low = lba;
363         ataio->cmd.lba_mid = lba >> 8;
364         ataio->cmd.lba_high = lba >> 16;
365         ataio->cmd.device = ATA_DEV_LBA;
366         ataio->cmd.lba_low_exp = lba >> 24;
367         ataio->cmd.lba_mid_exp = lba >> 32;
368         ataio->cmd.lba_high_exp = lba >> 40;
369         ataio->cmd.features_exp = features >> 8;
370         ataio->cmd.sector_count = sector_count;
371         ataio->cmd.sector_count_exp = sector_count >> 8;
372 }
373
374 void
375 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
376     uint64_t lba, uint16_t sector_count)
377 {
378         bzero(&ataio->cmd, sizeof(ataio->cmd));
379         ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
380         ataio->cmd.command = cmd;
381         ataio->cmd.features = sector_count;
382         ataio->cmd.lba_low = lba;
383         ataio->cmd.lba_mid = lba >> 8;
384         ataio->cmd.lba_high = lba >> 16;
385         ataio->cmd.device = ATA_DEV_LBA;
386         ataio->cmd.lba_low_exp = lba >> 24;
387         ataio->cmd.lba_mid_exp = lba >> 32;
388         ataio->cmd.lba_high_exp = lba >> 40;
389         ataio->cmd.features_exp = sector_count >> 8;
390 }
391
392 void
393 ata_reset_cmd(struct ccb_ataio *ataio)
394 {
395         bzero(&ataio->cmd, sizeof(ataio->cmd));
396         ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
397         ataio->cmd.control = 0x04;
398 }
399
400 void
401 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
402 {
403         bzero(&ataio->cmd, sizeof(ataio->cmd));
404         ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
405         ataio->cmd.command = ATA_READ_PM;
406         ataio->cmd.features = reg;
407         ataio->cmd.device = port & 0x0f;
408 }
409
410 void
411 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
412 {
413         bzero(&ataio->cmd, sizeof(ataio->cmd));
414         ataio->cmd.flags = 0;
415         ataio->cmd.command = ATA_WRITE_PM;
416         ataio->cmd.features = reg;
417         ataio->cmd.sector_count = val;
418         ataio->cmd.lba_low = val >> 8;
419         ataio->cmd.lba_mid = val >> 16;
420         ataio->cmd.lba_high = val >> 24;
421         ataio->cmd.device = port & 0x0f;
422 }
423
424 void
425 ata_bswap(int8_t *buf, int len)
426 {
427         u_int16_t *ptr = (u_int16_t*)(buf + len);
428
429         while (--ptr >= (u_int16_t*)buf)
430                 *ptr = be16toh(*ptr);
431 }
432
433 void
434 ata_btrim(int8_t *buf, int len)
435 {
436         int8_t *ptr;
437
438         for (ptr = buf; ptr < buf+len; ++ptr)
439                 if (!*ptr || *ptr == '_')
440                         *ptr = ' ';
441         for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
442                 *ptr = 0;
443 }
444
445 void
446 ata_bpack(int8_t *src, int8_t *dst, int len)
447 {
448         int i, j, blank;
449
450         for (i = j = blank = 0 ; i < len; i++) {
451                 if (blank && src[i] == ' ') continue;
452                 if (blank && src[i] != ' ') {
453                         dst[j++] = src[i];
454                         blank = 0;
455                         continue;
456                 }
457                 if (src[i] == ' ') {
458                         blank = 1;
459                         if (i == 0)
460                         continue;
461                 }
462                 dst[j++] = src[i];
463         }
464         while (j < len)
465                 dst[j++] = 0x00;
466 }
467
468 int
469 ata_max_pmode(struct ata_params *ap)
470 {
471     if (ap->atavalid & ATA_FLAG_64_70) {
472         if (ap->apiomodes & 0x02)
473             return ATA_PIO4;
474         if (ap->apiomodes & 0x01)
475             return ATA_PIO3;
476     }
477     if (ap->mwdmamodes & 0x04)
478         return ATA_PIO4;
479     if (ap->mwdmamodes & 0x02)
480         return ATA_PIO3;
481     if (ap->mwdmamodes & 0x01)
482         return ATA_PIO2;
483     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
484         return ATA_PIO2;
485     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
486         return ATA_PIO1;
487     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
488         return ATA_PIO0;
489     return ATA_PIO0;
490 }
491
492 int
493 ata_max_wmode(struct ata_params *ap)
494 {
495     if (ap->mwdmamodes & 0x04)
496         return ATA_WDMA2;
497     if (ap->mwdmamodes & 0x02)
498         return ATA_WDMA1;
499     if (ap->mwdmamodes & 0x01)
500         return ATA_WDMA0;
501     return -1;
502 }
503
504 int
505 ata_max_umode(struct ata_params *ap)
506 {
507     if (ap->atavalid & ATA_FLAG_88) {
508         if (ap->udmamodes & 0x40)
509             return ATA_UDMA6;
510         if (ap->udmamodes & 0x20)
511             return ATA_UDMA5;
512         if (ap->udmamodes & 0x10)
513             return ATA_UDMA4;
514         if (ap->udmamodes & 0x08)
515             return ATA_UDMA3;
516         if (ap->udmamodes & 0x04)
517             return ATA_UDMA2;
518         if (ap->udmamodes & 0x02)
519             return ATA_UDMA1;
520         if (ap->udmamodes & 0x01)
521             return ATA_UDMA0;
522     }
523     return -1;
524 }
525
526 int
527 ata_max_mode(struct ata_params *ap, int maxmode)
528 {
529
530         if (maxmode == 0)
531                 maxmode = ATA_DMA_MAX;
532         if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
533                 return (min(maxmode, ata_max_umode(ap)));
534         if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
535                 return (min(maxmode, ata_max_wmode(ap)));
536         return (min(maxmode, ata_max_pmode(ap)));
537 }
538
539 char *
540 ata_mode2string(int mode)
541 {
542     switch (mode) {
543     case -1: return "UNSUPPORTED";
544     case 0: return "NONE";
545     case ATA_PIO0: return "PIO0";
546     case ATA_PIO1: return "PIO1";
547     case ATA_PIO2: return "PIO2";
548     case ATA_PIO3: return "PIO3";
549     case ATA_PIO4: return "PIO4";
550     case ATA_WDMA0: return "WDMA0";
551     case ATA_WDMA1: return "WDMA1";
552     case ATA_WDMA2: return "WDMA2";
553     case ATA_UDMA0: return "UDMA0";
554     case ATA_UDMA1: return "UDMA1";
555     case ATA_UDMA2: return "UDMA2";
556     case ATA_UDMA3: return "UDMA3";
557     case ATA_UDMA4: return "UDMA4";
558     case ATA_UDMA5: return "UDMA5";
559     case ATA_UDMA6: return "UDMA6";
560     default:
561         if (mode & ATA_DMA_MASK)
562             return "BIOSDMA";
563         else
564             return "BIOSPIO";
565     }
566 }
567
568 int
569 ata_string2mode(char *str)
570 {
571         if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
572         if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
573         if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
574         if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
575         if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
576         if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
577         if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
578         if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
579         if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
580         if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
581         if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
582         if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
583         if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
584         if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
585         if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
586         if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
587         if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
588         if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
589         if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
590         if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
591         if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
592         if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
593         return (-1);
594 }
595
596
597 u_int
598 ata_mode2speed(int mode)
599 {
600         switch (mode) {
601         case ATA_PIO0:
602         default:
603                 return (3300);
604         case ATA_PIO1:
605                 return (5200);
606         case ATA_PIO2:
607                 return (8300);
608         case ATA_PIO3:
609                 return (11100);
610         case ATA_PIO4:
611                 return (16700);
612         case ATA_WDMA0:
613                 return (4200);
614         case ATA_WDMA1:
615                 return (13300);
616         case ATA_WDMA2:
617                 return (16700);
618         case ATA_UDMA0:
619                 return (16700);
620         case ATA_UDMA1:
621                 return (25000);
622         case ATA_UDMA2:
623                 return (33300);
624         case ATA_UDMA3:
625                 return (44400);
626         case ATA_UDMA4:
627                 return (66700);
628         case ATA_UDMA5:
629                 return (100000);
630         case ATA_UDMA6:
631                 return (133000);
632         }
633 }
634
635 u_int
636 ata_revision2speed(int revision)
637 {
638         switch (revision) {
639         case 1:
640         default:
641                 return (150000);
642         case 2:
643                 return (300000);
644         case 3:
645                 return (600000);
646         }
647 }
648
649 int
650 ata_speed2revision(u_int speed)
651 {
652         switch (speed) {
653         case 0:
654                 return (0);
655         case 150000:
656                 return (1);
657         case 300000:
658                 return (2);
659         case 600000:
660                 return (3);
661         default:
662                 return (-1);
663         }
664 }
665
666 int
667 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
668 {
669         struct scsi_inquiry_pattern *entry;
670         struct ata_params *ident;
671  
672         entry = (struct scsi_inquiry_pattern *)table_entry;
673         ident = (struct ata_params *)identbuffer;
674
675         if ((cam_strmatch(ident->model, entry->product,
676                           sizeof(ident->model)) == 0)
677          && (cam_strmatch(ident->revision, entry->revision,
678                           sizeof(ident->revision)) == 0)) {
679                 return (0);
680         }
681         return (-1);
682 }
683
684 int
685 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
686 {
687         struct scsi_static_inquiry_pattern *entry;
688         struct ata_params *ident;
689  
690         entry = (struct scsi_static_inquiry_pattern *)table_entry;
691         ident = (struct ata_params *)identbuffer;
692
693         if ((cam_strmatch(ident->model, entry->product,
694                           sizeof(ident->model)) == 0)
695          && (cam_strmatch(ident->revision, entry->revision,
696                           sizeof(ident->revision)) == 0)) {
697                 return (0);
698         }
699         return (-1);
700 }