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: stable/9/sys/cam/ata/ata_all.c 280899 2015-03-31 07:27:29Z 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
58 int
ata_version(int ver)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 *
ata_op_string(struct ata_cmd * cmd)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 0x67:
112 if (cmd->features == 0xec)
113 return ("SEP_ATTN IDENTIFY");
114 switch (cmd->lba_low) {
115 case 0x00: return ("SEP_ATTN READ BUFFER");
116 case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS");
117 case 0x80: return ("SEP_ATTN WRITE BUFFER");
118 case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC");
119 }
120 return ("SEP_ATTN");
121 case 0x70: return ("SEEK");
122 case 0x87: return ("CFA_TRANSLATE_SECTOR");
123 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
124 case 0x92: return ("DOWNLOAD_MICROCODE");
125 case 0xa0: return ("PACKET");
126 case 0xa1: return ("ATAPI_IDENTIFY");
127 case 0xa2: return ("SERVICE");
128 case 0xb0: return ("SMART");
129 case 0xb1: return ("DEVICE CONFIGURATION");
130 case 0xc0: return ("CFA_ERASE");
131 case 0xc4: return ("READ_MUL");
132 case 0xc5: return ("WRITE_MUL");
133 case 0xc6: return ("SET_MULTI");
134 case 0xc7: return ("READ_DMA_QUEUED");
135 case 0xc8: return ("READ_DMA");
136 case 0xca: return ("WRITE_DMA");
137 case 0xcc: return ("WRITE_DMA_QUEUED");
138 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
139 case 0xce: return ("WRITE_MUL_FUA48");
140 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
141 case 0xda: return ("GET_MEDIA_STATUS");
142 case 0xde: return ("MEDIA_LOCK");
143 case 0xdf: return ("MEDIA_UNLOCK");
144 case 0xe0: return ("STANDBY_IMMEDIATE");
145 case 0xe1: return ("IDLE_IMMEDIATE");
146 case 0xe2: return ("STANDBY");
147 case 0xe3: return ("IDLE");
148 case 0xe4: return ("READ_BUFFER/PM");
149 case 0xe5: return ("CHECK_POWER_MODE");
150 case 0xe6: return ("SLEEP");
151 case 0xe7: return ("FLUSHCACHE");
152 case 0xe8: return ("WRITE_PM");
153 case 0xea: return ("FLUSHCACHE48");
154 case 0xec: return ("ATA_IDENTIFY");
155 case 0xed: return ("MEDIA_EJECT");
156 case 0xef:
157 switch (cmd->features) {
158 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
159 case 0x02: return ("SETFEATURES ENABLE WCACHE");
160 case 0x82: return ("SETFEATURES DISABLE WCACHE");
161 case 0x06: return ("SETFEATURES ENABLE PUIS");
162 case 0x86: return ("SETFEATURES DISABLE PUIS");
163 case 0x07: return ("SETFEATURES SPIN-UP");
164 case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
165 case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
166 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
167 case 0x55: return ("SETFEATURES DISABLE RCACHE");
168 }
169 return "SETFEATURES";
170 case 0xf1: return ("SECURITY_SET_PASSWORD");
171 case 0xf2: return ("SECURITY_UNLOCK");
172 case 0xf3: return ("SECURITY_ERASE_PREPARE");
173 case 0xf4: return ("SECURITY_ERASE_UNIT");
174 case 0xf5: return ("SECURITY_FREEZE_LOCK");
175 case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
176 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
177 case 0xf9: return ("SET_MAX_ADDRESS");
178 }
179 return "UNKNOWN";
180 }
181
182 char *
ata_cmd_string(struct ata_cmd * cmd,char * cmd_string,size_t len)183 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
184 {
185
186 snprintf(cmd_string, len, "%02x %02x %02x %02x "
187 "%02x %02x %02x %02x %02x %02x %02x %02x",
188 cmd->command, cmd->features,
189 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
190 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
191 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
192
193 return(cmd_string);
194 }
195
196 char *
ata_res_string(struct ata_res * res,char * res_string,size_t len)197 ata_res_string(struct ata_res *res, char *res_string, size_t len)
198 {
199
200 snprintf(res_string, len, "%02x %02x %02x %02x "
201 "%02x %02x %02x %02x %02x %02x %02x",
202 res->status, res->error,
203 res->lba_low, res->lba_mid, res->lba_high, res->device,
204 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
205 res->sector_count, res->sector_count_exp);
206
207 return(res_string);
208 }
209
210 /*
211 * ata_command_sbuf() returns 0 for success and -1 for failure.
212 */
213 int
ata_command_sbuf(struct ccb_ataio * ataio,struct sbuf * sb)214 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
215 {
216 char cmd_str[(12 * 3) + 1];
217
218 sbuf_printf(sb, "%s. ACB: %s",
219 ata_op_string(&ataio->cmd),
220 ata_cmd_string(&ataio->cmd, cmd_str, sizeof(cmd_str)));
221
222 return(0);
223 }
224
225 /*
226 * ata_status_abuf() returns 0 for success and -1 for failure.
227 */
228 int
ata_status_sbuf(struct ccb_ataio * ataio,struct sbuf * sb)229 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
230 {
231
232 sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
233 ataio->res.status,
234 (ataio->res.status & 0x80) ? "BSY " : "",
235 (ataio->res.status & 0x40) ? "DRDY " : "",
236 (ataio->res.status & 0x20) ? "DF " : "",
237 (ataio->res.status & 0x10) ? "SERV " : "",
238 (ataio->res.status & 0x08) ? "DRQ " : "",
239 (ataio->res.status & 0x04) ? "CORR " : "",
240 (ataio->res.status & 0x02) ? "IDX " : "",
241 (ataio->res.status & 0x01) ? "ERR" : "");
242 if (ataio->res.status & 1) {
243 sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
244 ataio->res.error,
245 (ataio->res.error & 0x80) ? "ICRC " : "",
246 (ataio->res.error & 0x40) ? "UNC " : "",
247 (ataio->res.error & 0x20) ? "MC " : "",
248 (ataio->res.error & 0x10) ? "IDNF " : "",
249 (ataio->res.error & 0x08) ? "MCR " : "",
250 (ataio->res.error & 0x04) ? "ABRT " : "",
251 (ataio->res.error & 0x02) ? "NM " : "",
252 (ataio->res.error & 0x01) ? "ILI" : "");
253 }
254
255 return(0);
256 }
257
258 /*
259 * ata_res_sbuf() returns 0 for success and -1 for failure.
260 */
261 int
ata_res_sbuf(struct ccb_ataio * ataio,struct sbuf * sb)262 ata_res_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
263 {
264 char res_str[(11 * 3) + 1];
265
266 sbuf_printf(sb, "RES: %s",
267 ata_res_string(&ataio->res, res_str, sizeof(res_str)));
268
269 return(0);
270 }
271
272 void
ata_print_ident(struct ata_params * ident_data)273 ata_print_ident(struct ata_params *ident_data)
274 {
275 const char *proto;
276 char product[48], revision[16], ata[12], sata[12];
277
278 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
279 sizeof(product));
280 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
281 sizeof(revision));
282 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
283 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
284 if (ata_version(ident_data->version_major) == 0) {
285 snprintf(ata, sizeof(ata), "%s", proto);
286 } else if (ata_version(ident_data->version_major) <= 7) {
287 snprintf(ata, sizeof(ata), "%s-%d", proto,
288 ata_version(ident_data->version_major));
289 } else if (ata_version(ident_data->version_major) == 8) {
290 snprintf(ata, sizeof(ata), "%s8-ACS", proto);
291 } else {
292 snprintf(ata, sizeof(ata), "ACS-%d %s",
293 ata_version(ident_data->version_major) - 7, proto);
294 }
295 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
296 if (ident_data->satacapabilities & ATA_SATA_GEN3)
297 snprintf(sata, sizeof(sata), " SATA 3.x");
298 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
299 snprintf(sata, sizeof(sata), " SATA 2.x");
300 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
301 snprintf(sata, sizeof(sata), " SATA 1.x");
302 else
303 snprintf(sata, sizeof(sata), " SATA");
304 } else
305 sata[0] = 0;
306 printf("<%s %s> %s%s device\n", product, revision, ata, sata);
307 }
308
309 void
ata_print_ident_short(struct ata_params * ident_data)310 ata_print_ident_short(struct ata_params *ident_data)
311 {
312 char product[48], revision[16];
313
314 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
315 sizeof(product));
316 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
317 sizeof(revision));
318 printf("<%s %s>", product, revision);
319 }
320
321 void
semb_print_ident(struct sep_identify_data * ident_data)322 semb_print_ident(struct sep_identify_data *ident_data)
323 {
324 char vendor[9], product[17], revision[5], fw[5], in[7], ins[5];
325
326 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
327 cam_strvis(product, ident_data->product_id, 16, sizeof(product));
328 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
329 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
330 cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
331 cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
332 printf("<%s %s %s %s> SEMB %s %s device\n",
333 vendor, product, revision, fw, in, ins);
334 }
335
336 void
semb_print_ident_short(struct sep_identify_data * ident_data)337 semb_print_ident_short(struct sep_identify_data *ident_data)
338 {
339 char vendor[9], product[17], revision[5], fw[5];
340
341 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
342 cam_strvis(product, ident_data->product_id, 16, sizeof(product));
343 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
344 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
345 printf("<%s %s %s %s>", vendor, product, revision, fw);
346 }
347
348 uint32_t
ata_logical_sector_size(struct ata_params * ident_data)349 ata_logical_sector_size(struct ata_params *ident_data)
350 {
351 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
352 (ident_data->pss & ATA_PSS_LSSABOVE512)) {
353 return (((u_int32_t)ident_data->lss_1 |
354 ((u_int32_t)ident_data->lss_2 << 16)) * 2);
355 }
356 return (512);
357 }
358
359 uint64_t
ata_physical_sector_size(struct ata_params * ident_data)360 ata_physical_sector_size(struct ata_params *ident_data)
361 {
362 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
363 if (ident_data->pss & ATA_PSS_MULTLS) {
364 return ((uint64_t)ata_logical_sector_size(ident_data) *
365 (1 << (ident_data->pss & ATA_PSS_LSPPS)));
366 } else {
367 return (uint64_t)ata_logical_sector_size(ident_data);
368 }
369 }
370 return (512);
371 }
372
373 uint64_t
ata_logical_sector_offset(struct ata_params * ident_data)374 ata_logical_sector_offset(struct ata_params *ident_data)
375 {
376 if ((ident_data->lsalign & 0xc000) == 0x4000) {
377 return ((uint64_t)ata_logical_sector_size(ident_data) *
378 (ident_data->lsalign & 0x3fff));
379 }
380 return (0);
381 }
382
383 void
ata_28bit_cmd(struct ccb_ataio * ataio,uint8_t cmd,uint8_t features,uint32_t lba,uint8_t sector_count)384 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
385 uint32_t lba, uint8_t sector_count)
386 {
387 bzero(&ataio->cmd, sizeof(ataio->cmd));
388 ataio->cmd.flags = 0;
389 if (cmd == ATA_READ_DMA ||
390 cmd == ATA_READ_DMA_QUEUED ||
391 cmd == ATA_WRITE_DMA ||
392 cmd == ATA_WRITE_DMA_QUEUED)
393 ataio->cmd.flags |= CAM_ATAIO_DMA;
394 ataio->cmd.command = cmd;
395 ataio->cmd.features = features;
396 ataio->cmd.lba_low = lba;
397 ataio->cmd.lba_mid = lba >> 8;
398 ataio->cmd.lba_high = lba >> 16;
399 ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
400 ataio->cmd.sector_count = sector_count;
401 }
402
403 void
ata_48bit_cmd(struct ccb_ataio * ataio,uint8_t cmd,uint16_t features,uint64_t lba,uint16_t sector_count)404 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
405 uint64_t lba, uint16_t sector_count)
406 {
407
408 ataio->cmd.flags = CAM_ATAIO_48BIT;
409 if (cmd == ATA_READ_DMA48 ||
410 cmd == ATA_READ_DMA_QUEUED48 ||
411 cmd == ATA_READ_STREAM_DMA48 ||
412 cmd == ATA_WRITE_DMA48 ||
413 cmd == ATA_WRITE_DMA_FUA48 ||
414 cmd == ATA_WRITE_DMA_QUEUED48 ||
415 cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
416 cmd == ATA_WRITE_STREAM_DMA48 ||
417 cmd == ATA_DATA_SET_MANAGEMENT)
418 ataio->cmd.flags |= CAM_ATAIO_DMA;
419 ataio->cmd.command = cmd;
420 ataio->cmd.features = features;
421 ataio->cmd.lba_low = lba;
422 ataio->cmd.lba_mid = lba >> 8;
423 ataio->cmd.lba_high = lba >> 16;
424 ataio->cmd.device = ATA_DEV_LBA;
425 ataio->cmd.lba_low_exp = lba >> 24;
426 ataio->cmd.lba_mid_exp = lba >> 32;
427 ataio->cmd.lba_high_exp = lba >> 40;
428 ataio->cmd.features_exp = features >> 8;
429 ataio->cmd.sector_count = sector_count;
430 ataio->cmd.sector_count_exp = sector_count >> 8;
431 ataio->cmd.control = 0;
432 }
433
434 void
ata_ncq_cmd(struct ccb_ataio * ataio,uint8_t cmd,uint64_t lba,uint16_t sector_count)435 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
436 uint64_t lba, uint16_t sector_count)
437 {
438
439 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
440 ataio->cmd.command = cmd;
441 ataio->cmd.features = sector_count;
442 ataio->cmd.lba_low = lba;
443 ataio->cmd.lba_mid = lba >> 8;
444 ataio->cmd.lba_high = lba >> 16;
445 ataio->cmd.device = ATA_DEV_LBA;
446 ataio->cmd.lba_low_exp = lba >> 24;
447 ataio->cmd.lba_mid_exp = lba >> 32;
448 ataio->cmd.lba_high_exp = lba >> 40;
449 ataio->cmd.features_exp = sector_count >> 8;
450 ataio->cmd.sector_count = 0;
451 ataio->cmd.sector_count_exp = 0;
452 ataio->cmd.control = 0;
453 }
454
455 void
ata_reset_cmd(struct ccb_ataio * ataio)456 ata_reset_cmd(struct ccb_ataio *ataio)
457 {
458 bzero(&ataio->cmd, sizeof(ataio->cmd));
459 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
460 ataio->cmd.control = 0x04;
461 }
462
463 void
ata_pm_read_cmd(struct ccb_ataio * ataio,int reg,int port)464 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
465 {
466 bzero(&ataio->cmd, sizeof(ataio->cmd));
467 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
468 ataio->cmd.command = ATA_READ_PM;
469 ataio->cmd.features = reg;
470 ataio->cmd.device = port & 0x0f;
471 }
472
473 void
ata_pm_write_cmd(struct ccb_ataio * ataio,int reg,int port,uint32_t val)474 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
475 {
476 bzero(&ataio->cmd, sizeof(ataio->cmd));
477 ataio->cmd.flags = 0;
478 ataio->cmd.command = ATA_WRITE_PM;
479 ataio->cmd.features = reg;
480 ataio->cmd.sector_count = val;
481 ataio->cmd.lba_low = val >> 8;
482 ataio->cmd.lba_mid = val >> 16;
483 ataio->cmd.lba_high = val >> 24;
484 ataio->cmd.device = port & 0x0f;
485 }
486
487 void
ata_bswap(int8_t * buf,int len)488 ata_bswap(int8_t *buf, int len)
489 {
490 u_int16_t *ptr = (u_int16_t*)(buf + len);
491
492 while (--ptr >= (u_int16_t*)buf)
493 *ptr = be16toh(*ptr);
494 }
495
496 void
ata_btrim(int8_t * buf,int len)497 ata_btrim(int8_t *buf, int len)
498 {
499 int8_t *ptr;
500
501 for (ptr = buf; ptr < buf+len; ++ptr)
502 if (!*ptr || *ptr == '_')
503 *ptr = ' ';
504 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
505 *ptr = 0;
506 }
507
508 void
ata_bpack(int8_t * src,int8_t * dst,int len)509 ata_bpack(int8_t *src, int8_t *dst, int len)
510 {
511 int i, j, blank;
512
513 for (i = j = blank = 0 ; i < len; i++) {
514 if (blank && src[i] == ' ') continue;
515 if (blank && src[i] != ' ') {
516 dst[j++] = src[i];
517 blank = 0;
518 continue;
519 }
520 if (src[i] == ' ') {
521 blank = 1;
522 if (i == 0)
523 continue;
524 }
525 dst[j++] = src[i];
526 }
527 while (j < len)
528 dst[j++] = 0x00;
529 }
530
531 int
ata_max_pmode(struct ata_params * ap)532 ata_max_pmode(struct ata_params *ap)
533 {
534 if (ap->atavalid & ATA_FLAG_64_70) {
535 if (ap->apiomodes & 0x02)
536 return ATA_PIO4;
537 if (ap->apiomodes & 0x01)
538 return ATA_PIO3;
539 }
540 if (ap->mwdmamodes & 0x04)
541 return ATA_PIO4;
542 if (ap->mwdmamodes & 0x02)
543 return ATA_PIO3;
544 if (ap->mwdmamodes & 0x01)
545 return ATA_PIO2;
546 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
547 return ATA_PIO2;
548 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
549 return ATA_PIO1;
550 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
551 return ATA_PIO0;
552 return ATA_PIO0;
553 }
554
555 int
ata_max_wmode(struct ata_params * ap)556 ata_max_wmode(struct ata_params *ap)
557 {
558 if (ap->mwdmamodes & 0x04)
559 return ATA_WDMA2;
560 if (ap->mwdmamodes & 0x02)
561 return ATA_WDMA1;
562 if (ap->mwdmamodes & 0x01)
563 return ATA_WDMA0;
564 return -1;
565 }
566
567 int
ata_max_umode(struct ata_params * ap)568 ata_max_umode(struct ata_params *ap)
569 {
570 if (ap->atavalid & ATA_FLAG_88) {
571 if (ap->udmamodes & 0x40)
572 return ATA_UDMA6;
573 if (ap->udmamodes & 0x20)
574 return ATA_UDMA5;
575 if (ap->udmamodes & 0x10)
576 return ATA_UDMA4;
577 if (ap->udmamodes & 0x08)
578 return ATA_UDMA3;
579 if (ap->udmamodes & 0x04)
580 return ATA_UDMA2;
581 if (ap->udmamodes & 0x02)
582 return ATA_UDMA1;
583 if (ap->udmamodes & 0x01)
584 return ATA_UDMA0;
585 }
586 return -1;
587 }
588
589 int
ata_max_mode(struct ata_params * ap,int maxmode)590 ata_max_mode(struct ata_params *ap, int maxmode)
591 {
592
593 if (maxmode == 0)
594 maxmode = ATA_DMA_MAX;
595 if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
596 return (min(maxmode, ata_max_umode(ap)));
597 if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
598 return (min(maxmode, ata_max_wmode(ap)));
599 return (min(maxmode, ata_max_pmode(ap)));
600 }
601
602 char *
ata_mode2string(int mode)603 ata_mode2string(int mode)
604 {
605 switch (mode) {
606 case -1: return "UNSUPPORTED";
607 case 0: return "NONE";
608 case ATA_PIO0: return "PIO0";
609 case ATA_PIO1: return "PIO1";
610 case ATA_PIO2: return "PIO2";
611 case ATA_PIO3: return "PIO3";
612 case ATA_PIO4: return "PIO4";
613 case ATA_WDMA0: return "WDMA0";
614 case ATA_WDMA1: return "WDMA1";
615 case ATA_WDMA2: return "WDMA2";
616 case ATA_UDMA0: return "UDMA0";
617 case ATA_UDMA1: return "UDMA1";
618 case ATA_UDMA2: return "UDMA2";
619 case ATA_UDMA3: return "UDMA3";
620 case ATA_UDMA4: return "UDMA4";
621 case ATA_UDMA5: return "UDMA5";
622 case ATA_UDMA6: return "UDMA6";
623 default:
624 if (mode & ATA_DMA_MASK)
625 return "BIOSDMA";
626 else
627 return "BIOSPIO";
628 }
629 }
630
631 int
ata_string2mode(char * str)632 ata_string2mode(char *str)
633 {
634 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
635 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
636 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
637 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
638 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
639 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
640 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
641 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
642 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
643 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
644 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
645 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
646 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
647 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
648 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
649 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
650 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
651 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
652 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
653 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
654 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
655 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
656 return (-1);
657 }
658
659
660 u_int
ata_mode2speed(int mode)661 ata_mode2speed(int mode)
662 {
663 switch (mode) {
664 case ATA_PIO0:
665 default:
666 return (3300);
667 case ATA_PIO1:
668 return (5200);
669 case ATA_PIO2:
670 return (8300);
671 case ATA_PIO3:
672 return (11100);
673 case ATA_PIO4:
674 return (16700);
675 case ATA_WDMA0:
676 return (4200);
677 case ATA_WDMA1:
678 return (13300);
679 case ATA_WDMA2:
680 return (16700);
681 case ATA_UDMA0:
682 return (16700);
683 case ATA_UDMA1:
684 return (25000);
685 case ATA_UDMA2:
686 return (33300);
687 case ATA_UDMA3:
688 return (44400);
689 case ATA_UDMA4:
690 return (66700);
691 case ATA_UDMA5:
692 return (100000);
693 case ATA_UDMA6:
694 return (133000);
695 }
696 }
697
698 u_int
ata_revision2speed(int revision)699 ata_revision2speed(int revision)
700 {
701 switch (revision) {
702 case 1:
703 default:
704 return (150000);
705 case 2:
706 return (300000);
707 case 3:
708 return (600000);
709 }
710 }
711
712 int
ata_speed2revision(u_int speed)713 ata_speed2revision(u_int speed)
714 {
715 switch (speed) {
716 case 0:
717 return (0);
718 case 150000:
719 return (1);
720 case 300000:
721 return (2);
722 case 600000:
723 return (3);
724 default:
725 return (-1);
726 }
727 }
728
729 int
ata_identify_match(caddr_t identbuffer,caddr_t table_entry)730 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
731 {
732 struct scsi_inquiry_pattern *entry;
733 struct ata_params *ident;
734
735 entry = (struct scsi_inquiry_pattern *)table_entry;
736 ident = (struct ata_params *)identbuffer;
737
738 if ((cam_strmatch(ident->model, entry->product,
739 sizeof(ident->model)) == 0)
740 && (cam_strmatch(ident->revision, entry->revision,
741 sizeof(ident->revision)) == 0)) {
742 return (0);
743 }
744 return (-1);
745 }
746
747 int
ata_static_identify_match(caddr_t identbuffer,caddr_t table_entry)748 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
749 {
750 struct scsi_static_inquiry_pattern *entry;
751 struct ata_params *ident;
752
753 entry = (struct scsi_static_inquiry_pattern *)table_entry;
754 ident = (struct ata_params *)identbuffer;
755
756 if ((cam_strmatch(ident->model, entry->product,
757 sizeof(ident->model)) == 0)
758 && (cam_strmatch(ident->revision, entry->revision,
759 sizeof(ident->revision)) == 0)) {
760 return (0);
761 }
762 return (-1);
763 }
764
765 void
semb_receive_diagnostic_results(struct ccb_ataio * ataio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),uint8_t tag_action,int pcv,uint8_t page_code,uint8_t * data_ptr,uint16_t length,uint32_t timeout)766 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
767 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
768 uint8_t tag_action, int pcv, uint8_t page_code,
769 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
770 {
771
772 length = min(length, 1020);
773 length = (length + 3) & ~3;
774 cam_fill_ataio(ataio,
775 retries,
776 cbfcnp,
777 /*flags*/CAM_DIR_IN,
778 tag_action,
779 data_ptr,
780 length,
781 timeout);
782 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
783 pcv ? page_code : 0, 0x02, length / 4);
784 }
785
786 void
semb_send_diagnostic(struct ccb_ataio * ataio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),uint8_t tag_action,uint8_t * data_ptr,uint16_t length,uint32_t timeout)787 semb_send_diagnostic(struct ccb_ataio *ataio,
788 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
789 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
790 {
791
792 length = min(length, 1020);
793 length = (length + 3) & ~3;
794 cam_fill_ataio(ataio,
795 retries,
796 cbfcnp,
797 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
798 tag_action,
799 data_ptr,
800 length,
801 timeout);
802 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
803 length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
804 }
805
806 void
semb_read_buffer(struct ccb_ataio * ataio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),uint8_t tag_action,uint8_t page_code,uint8_t * data_ptr,uint16_t length,uint32_t timeout)807 semb_read_buffer(struct ccb_ataio *ataio,
808 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
809 uint8_t tag_action, uint8_t page_code,
810 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
811 {
812
813 length = min(length, 1020);
814 length = (length + 3) & ~3;
815 cam_fill_ataio(ataio,
816 retries,
817 cbfcnp,
818 /*flags*/CAM_DIR_IN,
819 tag_action,
820 data_ptr,
821 length,
822 timeout);
823 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
824 page_code, 0x00, length / 4);
825 }
826
827 void
semb_write_buffer(struct ccb_ataio * ataio,u_int32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),uint8_t tag_action,uint8_t * data_ptr,uint16_t length,uint32_t timeout)828 semb_write_buffer(struct ccb_ataio *ataio,
829 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
830 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
831 {
832
833 length = min(length, 1020);
834 length = (length + 3) & ~3;
835 cam_fill_ataio(ataio,
836 retries,
837 cbfcnp,
838 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
839 tag_action,
840 data_ptr,
841 length,
842 timeout);
843 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
844 length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
845 }
846
847