1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009 Alexander Motin <[email protected]>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
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:
79 switch (cmd->features) {
80 case 0x00: return ("NOP FLUSHQUEUE");
81 case 0x01: return ("NOP AUTOPOLL");
82 }
83 return ("NOP");
84 case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR");
85 case 0x06:
86 switch (cmd->features) {
87 case 0x01: return ("DSM TRIM");
88 }
89 return "DSM";
90 case 0x07:
91 switch (cmd->features) {
92 case 0x01: return ("DSM_XL TRIM");
93 }
94 return "DSM_XL";
95 case 0x08: return ("DEVICE_RESET");
96 case 0x0b: return ("REQUEST_SENSE_DATA_EXT");
97 case 0x12: return ("GET_PHYSICAL_ELEMENT_STATUS");
98 case 0x20: return ("READ");
99 case 0x24: return ("READ48");
100 case 0x25: return ("READ_DMA48");
101 case 0x26: return ("READ_DMA_QUEUED48");
102 case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
103 case 0x29: return ("READ_MUL48");
104 case 0x2a: return ("READ_STREAM_DMA48");
105 case 0x2b: return ("READ_STREAM48");
106 case 0x2f: return ("READ_LOG_EXT");
107 case 0x30: return ("WRITE");
108 case 0x34: return ("WRITE48");
109 case 0x35: return ("WRITE_DMA48");
110 case 0x36: return ("WRITE_DMA_QUEUED48");
111 case 0x37: return ("SET_MAX_ADDRESS48");
112 case 0x39: return ("WRITE_MUL48");
113 case 0x3a: return ("WRITE_STREAM_DMA48");
114 case 0x3b: return ("WRITE_STREAM48");
115 case 0x3d: return ("WRITE_DMA_FUA48");
116 case 0x3e: return ("WRITE_DMA_QUEUED_FUA48");
117 case 0x3f: return ("WRITE_LOG_EXT");
118 case 0x40: return ("READ_VERIFY");
119 case 0x42: return ("READ_VERIFY48");
120 case 0x44:
121 switch (cmd->features) {
122 case 0x01: return ("ZERO_EXT TRIM");
123 }
124 return "ZERO_EXT";
125 case 0x45:
126 switch (cmd->features) {
127 case 0x55: return ("WRITE_UNCORRECTABLE48 PSEUDO");
128 case 0xaa: return ("WRITE_UNCORRECTABLE48 FLAGGED");
129 }
130 return "WRITE_UNCORRECTABLE48";
131 case 0x47: return ("READ_LOG_DMA_EXT");
132 case 0x4a: return ("ZAC_MANAGEMENT_IN");
133 case 0x51: return ("CONFIGURE_STREAM");
134 case 0x57: return ("WRITE_LOG_DMA_EXT");
135 case 0x5b: return ("TRUSTED_NON_DATA");
136 case 0x5c: return ("TRUSTED_RECEIVE");
137 case 0x5d: return ("TRUSTED_RECEIVE_DMA");
138 case 0x5e: return ("TRUSTED_SEND");
139 case 0x5f: return ("TRUSTED_SEND_DMA");
140 case 0x60: return ("READ_FPDMA_QUEUED");
141 case 0x61: return ("WRITE_FPDMA_QUEUED");
142 case 0x63:
143 switch (cmd->features & 0xf) {
144 case 0x00: return ("NCQ_NON_DATA ABORT NCQ QUEUE");
145 case 0x01: return ("NCQ_NON_DATA DEADLINE HANDLING");
146 case 0x02: return ("NCQ_NON_DATA HYBRID DEMOTE BY SIZE");
147 case 0x03: return ("NCQ_NON_DATA HYBRID CHANGE BY LBA RANGE");
148 case 0x04: return ("NCQ_NON_DATA HYBRID CONTROL");
149 case 0x05: return ("NCQ_NON_DATA SET FEATURES");
150 /*
151 * XXX KDM need common decoding between NCQ and non-NCQ
152 * versions of SET FEATURES.
153 */
154 case 0x06: return ("NCQ_NON_DATA ZERO EXT");
155 case 0x07: return ("NCQ_NON_DATA ZAC MANAGEMENT OUT");
156 }
157 return ("NCQ_NON_DATA");
158 case 0x64:
159 switch (cmd->sector_count_exp & 0xf) {
160 case 0x00: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT");
161 case 0x01: return ("SEND_FPDMA_QUEUED HYBRID EVICT");
162 case 0x02: return ("SEND_FPDMA_QUEUED WRITE LOG DMA EXT");
163 case 0x03: return ("SEND_FPDMA_QUEUED ZAC MANAGEMENT OUT");
164 case 0x04: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT XL");
165 }
166 return ("SEND_FPDMA_QUEUED");
167 case 0x65:
168 switch (cmd->sector_count_exp & 0xf) {
169 case 0x01: return ("RECEIVE_FPDMA_QUEUED READ LOG DMA EXT");
170 case 0x02: return ("RECEIVE_FPDMA_QUEUED ZAC MANAGEMENT IN");
171 }
172 return ("RECEIVE_FPDMA_QUEUED");
173 case 0x67:
174 if (cmd->features == 0xec)
175 return ("SEP_ATTN IDENTIFY");
176 switch (cmd->lba_low) {
177 case 0x00: return ("SEP_ATTN READ BUFFER");
178 case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS");
179 case 0x80: return ("SEP_ATTN WRITE BUFFER");
180 case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC");
181 }
182 return ("SEP_ATTN");
183 case 0x70: return ("SEEK");
184 case 0x77: return ("SET_DATE_TIME_EXT");
185 case 0x78:
186 switch (cmd->features) {
187 case 0x00: return ("GET_NATIVE_MAX_ADDRESS_EXT");
188 case 0x01: return ("SET_ACCESSIBLE_MAX_ADDRESS_EXT");
189 case 0x02: return ("FREEZE_ACCESSIBLE_MAX_ADDRESS_EXT");
190 }
191 return ("ACCESSIBLE_MAX_ADDRESS_CONFIGURATION");
192 case 0x7C: return ("REMOVE_ELEMENT_AND_TRUNCATE");
193 case 0x87: return ("CFA_TRANSLATE_SECTOR");
194 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
195 case 0x92: return ("DOWNLOAD_MICROCODE");
196 case 0x93: return ("DOWNLOAD_MICROCODE_DMA");
197 case 0x9a: return ("ZAC_MANAGEMENT_OUT");
198 case 0xa0: return ("PACKET");
199 case 0xa1: return ("ATAPI_IDENTIFY");
200 case 0xa2: return ("SERVICE");
201 case 0xb0:
202 switch(cmd->features) {
203 case 0xd0: return ("SMART READ ATTR VALUES");
204 case 0xd1: return ("SMART READ ATTR THRESHOLDS");
205 case 0xd3: return ("SMART SAVE ATTR VALUES");
206 case 0xd4: return ("SMART EXECUTE OFFLINE IMMEDIATE");
207 case 0xd5: return ("SMART READ LOG");
208 case 0xd6: return ("SMART WRITE LOG");
209 case 0xd8: return ("SMART ENABLE OPERATION");
210 case 0xd9: return ("SMART DISABLE OPERATION");
211 case 0xda: return ("SMART RETURN STATUS");
212 }
213 return ("SMART");
214 case 0xb1: return ("DEVICE CONFIGURATION");
215 case 0xb2: return ("SET_SECTOR_CONFIGURATION_EXT");
216 case 0xb4:
217 switch(cmd->features) {
218 case 0x00: return ("SANITIZE_STATUS_EXT");
219 case 0x11: return ("CRYPTO_SCRAMBLE_EXT");
220 case 0x12: return ("BLOCK_ERASE_EXT");
221 case 0x14: return ("OVERWRITE_EXT");
222 case 0x20: return ("SANITIZE_FREEZE_LOCK_EXT");
223 case 0x40: return ("SANITIZE_ANTIFREEZE_LOCK_EXT");
224 }
225 return ("SANITIZE_DEVICE");
226 case 0xc0: return ("CFA_ERASE");
227 case 0xc4: return ("READ_MUL");
228 case 0xc5: return ("WRITE_MUL");
229 case 0xc6: return ("SET_MULTI");
230 case 0xc7: return ("READ_DMA_QUEUED");
231 case 0xc8: return ("READ_DMA");
232 case 0xca: return ("WRITE_DMA");
233 case 0xcc: return ("WRITE_DMA_QUEUED");
234 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
235 case 0xce: return ("WRITE_MUL_FUA48");
236 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
237 case 0xda: return ("GET_MEDIA_STATUS");
238 case 0xde: return ("MEDIA_LOCK");
239 case 0xdf: return ("MEDIA_UNLOCK");
240 case 0xe0: return ("STANDBY_IMMEDIATE");
241 case 0xe1: return ("IDLE_IMMEDIATE");
242 case 0xe2: return ("STANDBY");
243 case 0xe3: return ("IDLE");
244 case 0xe4: return ("READ_BUFFER/PM");
245 case 0xe5: return ("CHECK_POWER_MODE");
246 case 0xe6: return ("SLEEP");
247 case 0xe7: return ("FLUSHCACHE");
248 case 0xe8: return ("WRITE_BUFFER/PM");
249 case 0xe9: return ("READ_BUFFER_DMA");
250 case 0xea: return ("FLUSHCACHE48");
251 case 0xeb: return ("WRITE_BUFFER_DMA");
252 case 0xec: return ("ATA_IDENTIFY");
253 case 0xed: return ("MEDIA_EJECT");
254 case 0xef:
255 /*
256 * XXX KDM need common decoding between NCQ and non-NCQ
257 * versions of SET FEATURES.
258 */
259 switch (cmd->features) {
260 case 0x02: return ("SETFEATURES ENABLE WCACHE");
261 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
262 case 0x05: return ("SETFEATURES ENABLE APM");
263 case 0x06: return ("SETFEATURES ENABLE PUIS");
264 case 0x07: return ("SETFEATURES SPIN-UP");
265 case 0x0b: return ("SETFEATURES ENABLE WRITE READ VERIFY");
266 case 0x0c: return ("SETFEATURES ENABLE DEVICE LIFE CONTROL");
267 case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
268 case 0x41: return ("SETFEATURES ENABLE FREEFALL CONTROL");
269 case 0x43: return ("SETFEATURES SET MAX HOST INT SECT TIMES");
270 case 0x45: return ("SETFEATURES SET RATE BASIS");
271 case 0x4a: return ("SETFEATURES EXTENDED POWER CONDITIONS");
272 case 0x50: return ("SETFEATURES ADVANCED BACKGROUD OPERATION");
273 case 0x55: return ("SETFEATURES DISABLE RCACHE");
274 case 0x5d: return ("SETFEATURES ENABLE RELIRQ");
275 case 0x5e: return ("SETFEATURES ENABLE SRVIRQ");
276 case 0x62: return ("SETFEATURES LONG PHYS SECT ALIGN ERC");
277 case 0x63: return ("SETFEATURES DSN");
278 case 0x66: return ("SETFEATURES DISABLE DEFAULTS");
279 case 0x82: return ("SETFEATURES DISABLE WCACHE");
280 case 0x85: return ("SETFEATURES DISABLE APM");
281 case 0x86: return ("SETFEATURES DISABLE PUIS");
282 case 0x8b: return ("SETFEATURES DISABLE WRITE READ VERIFY");
283 case 0x8c: return ("SETFEATURES DISABLE DEVICE LIFE CONTROL");
284 case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
285 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
286 case 0xC1: return ("SETFEATURES DISABLE FREEFALL CONTROL");
287 case 0xC3: return ("SETFEATURES SENSE DATA REPORTING");
288 case 0xC4: return ("SETFEATURES NCQ SENSE DATA RETURN");
289 case 0xCC: return ("SETFEATURES ENABLE DEFAULTS");
290 case 0xdd: return ("SETFEATURES DISABLE RELIRQ");
291 case 0xde: return ("SETFEATURES DISABLE SRVIRQ");
292 }
293 return "SETFEATURES";
294 case 0xf1: return ("SECURITY_SET_PASSWORD");
295 case 0xf2: return ("SECURITY_UNLOCK");
296 case 0xf3: return ("SECURITY_ERASE_PREPARE");
297 case 0xf4: return ("SECURITY_ERASE_UNIT");
298 case 0xf5: return ("SECURITY_FREEZE_LOCK");
299 case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
300 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
301 case 0xf9: return ("SET_MAX_ADDRESS");
302 }
303 return "UNKNOWN";
304 }
305
306 char *
ata_cmd_string(struct ata_cmd * cmd,char * cmd_string,size_t len)307 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
308 {
309 struct sbuf sb;
310 int error;
311
312 if (len == 0)
313 return ("");
314
315 sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
316 ata_cmd_sbuf(cmd, &sb);
317
318 error = sbuf_finish(&sb);
319 if (error != 0 &&
320 #ifdef _KERNEL
321 error != ENOMEM)
322 #else
323 errno != ENOMEM)
324 #endif
325 return ("");
326
327 return(sbuf_data(&sb));
328 }
329
330 void
ata_cmd_sbuf(struct ata_cmd * cmd,struct sbuf * sb)331 ata_cmd_sbuf(struct ata_cmd *cmd, struct sbuf *sb)
332 {
333 sbuf_printf(sb, "%02x %02x %02x %02x "
334 "%02x %02x %02x %02x %02x %02x %02x %02x",
335 cmd->command, cmd->features,
336 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
337 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
338 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
339 }
340
341 char *
ata_res_string(struct ata_res * res,char * res_string,size_t len)342 ata_res_string(struct ata_res *res, char *res_string, size_t len)
343 {
344 struct sbuf sb;
345 int error;
346
347 if (len == 0)
348 return ("");
349
350 sbuf_new(&sb, res_string, len, SBUF_FIXEDLEN);
351 ata_res_sbuf(res, &sb);
352
353 error = sbuf_finish(&sb);
354 if (error != 0 &&
355 #ifdef _KERNEL
356 error != ENOMEM)
357 #else
358 errno != ENOMEM)
359 #endif
360 return ("");
361
362 return(sbuf_data(&sb));
363 }
364
365 int
ata_res_sbuf(struct ata_res * res,struct sbuf * sb)366 ata_res_sbuf(struct ata_res *res, struct sbuf *sb)
367 {
368
369 sbuf_printf(sb, "%02x %02x %02x %02x "
370 "%02x %02x %02x %02x %02x %02x %02x",
371 res->status, res->error,
372 res->lba_low, res->lba_mid, res->lba_high, res->device,
373 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
374 res->sector_count, res->sector_count_exp);
375
376 return (0);
377 }
378
379 /*
380 * ata_command_sbuf() returns 0 for success and -1 for failure.
381 */
382 int
ata_command_sbuf(struct ccb_ataio * ataio,struct sbuf * sb)383 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
384 {
385
386 sbuf_printf(sb, "%s. ACB: ",
387 ata_op_string(&ataio->cmd));
388 ata_cmd_sbuf(&ataio->cmd, sb);
389
390 return(0);
391 }
392
393 /*
394 * ata_status_abuf() returns 0 for success and -1 for failure.
395 */
396 int
ata_status_sbuf(struct ccb_ataio * ataio,struct sbuf * sb)397 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
398 {
399
400 sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
401 ataio->res.status,
402 (ataio->res.status & 0x80) ? "BSY " : "",
403 (ataio->res.status & 0x40) ? "DRDY " : "",
404 (ataio->res.status & 0x20) ? "DF " : "",
405 (ataio->res.status & 0x10) ? "SERV " : "",
406 (ataio->res.status & 0x08) ? "DRQ " : "",
407 (ataio->res.status & 0x04) ? "CORR " : "",
408 (ataio->res.status & 0x02) ? "IDX " : "",
409 (ataio->res.status & 0x01) ? "ERR" : "");
410 if (ataio->res.status & 1) {
411 sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
412 ataio->res.error,
413 (ataio->res.error & 0x80) ? "ICRC " : "",
414 (ataio->res.error & 0x40) ? "UNC " : "",
415 (ataio->res.error & 0x20) ? "MC " : "",
416 (ataio->res.error & 0x10) ? "IDNF " : "",
417 (ataio->res.error & 0x08) ? "MCR " : "",
418 (ataio->res.error & 0x04) ? "ABRT " : "",
419 (ataio->res.error & 0x02) ? "NM " : "",
420 (ataio->res.error & 0x01) ? "ILI" : "");
421 }
422
423 return(0);
424 }
425
426 void
ata_print_ident(struct ata_params * ident_data)427 ata_print_ident(struct ata_params *ident_data)
428 {
429 const char *proto;
430 char ata[12], sata[12];
431
432 ata_print_ident_short(ident_data);
433
434 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
435 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
436 if (ata_version(ident_data->version_major) == 0) {
437 snprintf(ata, sizeof(ata), "%s", proto);
438 } else if (ata_version(ident_data->version_major) <= 7) {
439 snprintf(ata, sizeof(ata), "%s-%d", proto,
440 ata_version(ident_data->version_major));
441 } else if (ata_version(ident_data->version_major) == 8) {
442 snprintf(ata, sizeof(ata), "%s8-ACS", proto);
443 } else {
444 snprintf(ata, sizeof(ata), "ACS-%d %s",
445 ata_version(ident_data->version_major) - 7, proto);
446 }
447 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
448 if (ident_data->satacapabilities & ATA_SATA_GEN3)
449 snprintf(sata, sizeof(sata), " SATA 3.x");
450 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
451 snprintf(sata, sizeof(sata), " SATA 2.x");
452 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
453 snprintf(sata, sizeof(sata), " SATA 1.x");
454 else
455 snprintf(sata, sizeof(sata), " SATA");
456 } else
457 sata[0] = 0;
458 printf(" %s%s device\n", ata, sata);
459 }
460
461 void
ata_print_ident_sbuf(struct ata_params * ident_data,struct sbuf * sb)462 ata_print_ident_sbuf(struct ata_params *ident_data, struct sbuf *sb)
463 {
464 const char *proto, *sata;
465 int version;
466
467 ata_print_ident_short_sbuf(ident_data, sb);
468 sbuf_printf(sb, " ");
469
470 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
471 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
472 version = ata_version(ident_data->version_major);
473
474 switch (version) {
475 case 0:
476 sbuf_printf(sb, "%s", proto);
477 break;
478 case 1:
479 case 2:
480 case 3:
481 case 4:
482 case 5:
483 case 6:
484 case 7:
485 sbuf_printf(sb, "%s-%d", proto, version);
486 break;
487 case 8:
488 sbuf_printf(sb, "%s8-ACS", proto);
489 break;
490 default:
491 sbuf_printf(sb, "ACS-%d %s", version - 7, proto);
492 break;
493 }
494
495 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
496 if (ident_data->satacapabilities & ATA_SATA_GEN3)
497 sata = " SATA 3.x";
498 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
499 sata = " SATA 2.x";
500 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
501 sata = " SATA 1.x";
502 else
503 sata = " SATA";
504 } else
505 sata = "";
506 sbuf_printf(sb, "%s device\n", sata);
507 }
508
509 void
ata_print_ident_short(struct ata_params * ident_data)510 ata_print_ident_short(struct ata_params *ident_data)
511 {
512 char product[48], revision[16];
513
514 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
515 sizeof(product));
516 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
517 sizeof(revision));
518 printf("<%s %s>", product, revision);
519 }
520
521 void
ata_print_ident_short_sbuf(struct ata_params * ident_data,struct sbuf * sb)522 ata_print_ident_short_sbuf(struct ata_params *ident_data, struct sbuf *sb)
523 {
524
525 sbuf_printf(sb, "<");
526 cam_strvis_sbuf(sb, ident_data->model, sizeof(ident_data->model), 0);
527 sbuf_printf(sb, " ");
528 cam_strvis_sbuf(sb, ident_data->revision, sizeof(ident_data->revision), 0);
529 sbuf_printf(sb, ">");
530 }
531
532 void
semb_print_ident(struct sep_identify_data * ident_data)533 semb_print_ident(struct sep_identify_data *ident_data)
534 {
535 char in[7], ins[5];
536
537 semb_print_ident_short(ident_data);
538 cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
539 cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
540 printf(" SEMB %s %s device\n", in, ins);
541 }
542
543 void
semb_print_ident_sbuf(struct sep_identify_data * ident_data,struct sbuf * sb)544 semb_print_ident_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
545 {
546
547 semb_print_ident_short_sbuf(ident_data, sb);
548
549 sbuf_printf(sb, " SEMB ");
550 cam_strvis_sbuf(sb, ident_data->interface_id, 6, 0);
551 sbuf_printf(sb, " ");
552 cam_strvis_sbuf(sb, ident_data->interface_rev, 4, 0);
553 sbuf_printf(sb, " device\n");
554 }
555
556 void
semb_print_ident_short(struct sep_identify_data * ident_data)557 semb_print_ident_short(struct sep_identify_data *ident_data)
558 {
559 char vendor[9], product[17], revision[5], fw[5];
560
561 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
562 cam_strvis(product, ident_data->product_id, 16, sizeof(product));
563 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
564 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
565 printf("<%s %s %s %s>", vendor, product, revision, fw);
566 }
567
568 void
semb_print_ident_short_sbuf(struct sep_identify_data * ident_data,struct sbuf * sb)569 semb_print_ident_short_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
570 {
571
572 sbuf_printf(sb, "<");
573 cam_strvis_sbuf(sb, ident_data->vendor_id, 8, 0);
574 sbuf_printf(sb, " ");
575 cam_strvis_sbuf(sb, ident_data->product_id, 16, 0);
576 sbuf_printf(sb, " ");
577 cam_strvis_sbuf(sb, ident_data->product_rev, 4, 0);
578 sbuf_printf(sb, " ");
579 cam_strvis_sbuf(sb, ident_data->firmware_rev, 4, 0);
580 sbuf_printf(sb, ">");
581 }
582
583 uint32_t
ata_logical_sector_size(struct ata_params * ident_data)584 ata_logical_sector_size(struct ata_params *ident_data)
585 {
586 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
587 (ident_data->pss & ATA_PSS_LSSABOVE512)) {
588 return (((uint32_t)ident_data->lss_1 |
589 ((uint32_t)ident_data->lss_2 << 16)) * 2);
590 }
591 return (512);
592 }
593
594 uint64_t
ata_physical_sector_size(struct ata_params * ident_data)595 ata_physical_sector_size(struct ata_params *ident_data)
596 {
597 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
598 if (ident_data->pss & ATA_PSS_MULTLS) {
599 return ((uint64_t)ata_logical_sector_size(ident_data) *
600 (1 << (ident_data->pss & ATA_PSS_LSPPS)));
601 } else {
602 return (uint64_t)ata_logical_sector_size(ident_data);
603 }
604 }
605 return (512);
606 }
607
608 uint64_t
ata_logical_sector_offset(struct ata_params * ident_data)609 ata_logical_sector_offset(struct ata_params *ident_data)
610 {
611 if ((ident_data->lsalign & 0xc000) == 0x4000) {
612 return ((uint64_t)ata_logical_sector_size(ident_data) *
613 (ident_data->lsalign & 0x3fff));
614 }
615 return (0);
616 }
617
618 void
ata_28bit_cmd(struct ccb_ataio * ataio,uint8_t cmd,uint8_t features,uint32_t lba,uint8_t sector_count)619 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
620 uint32_t lba, uint8_t sector_count)
621 {
622 bzero(&ataio->cmd, sizeof(ataio->cmd));
623 ataio->cmd.flags = 0;
624 if (cmd == ATA_READ_DMA ||
625 cmd == ATA_READ_DMA_QUEUED ||
626 cmd == ATA_WRITE_DMA ||
627 cmd == ATA_WRITE_DMA_QUEUED ||
628 cmd == ATA_TRUSTED_RECEIVE_DMA ||
629 cmd == ATA_TRUSTED_SEND_DMA ||
630 cmd == ATA_DOWNLOAD_MICROCODE_DMA ||
631 cmd == ATA_READ_BUFFER_DMA ||
632 cmd == ATA_WRITE_BUFFER_DMA)
633 ataio->cmd.flags |= CAM_ATAIO_DMA;
634 ataio->cmd.command = cmd;
635 ataio->cmd.features = features;
636 ataio->cmd.lba_low = lba;
637 ataio->cmd.lba_mid = lba >> 8;
638 ataio->cmd.lba_high = lba >> 16;
639 ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
640 ataio->cmd.sector_count = sector_count;
641 }
642
643 void
ata_48bit_cmd(struct ccb_ataio * ataio,uint8_t cmd,uint16_t features,uint64_t lba,uint16_t sector_count)644 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
645 uint64_t lba, uint16_t sector_count)
646 {
647
648 ataio->cmd.flags = CAM_ATAIO_48BIT;
649 if (cmd == ATA_READ_DMA48 ||
650 cmd == ATA_READ_DMA_QUEUED48 ||
651 cmd == ATA_READ_STREAM_DMA48 ||
652 cmd == ATA_WRITE_DMA48 ||
653 cmd == ATA_WRITE_DMA_FUA48 ||
654 cmd == ATA_WRITE_DMA_QUEUED48 ||
655 cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
656 cmd == ATA_WRITE_STREAM_DMA48 ||
657 cmd == ATA_DATA_SET_MANAGEMENT ||
658 cmd == ATA_READ_LOG_DMA_EXT ||
659 cmd == ATA_WRITE_LOG_DMA_EXT)
660 ataio->cmd.flags |= CAM_ATAIO_DMA;
661 ataio->cmd.command = cmd;
662 ataio->cmd.features = features;
663 ataio->cmd.lba_low = lba;
664 ataio->cmd.lba_mid = lba >> 8;
665 ataio->cmd.lba_high = lba >> 16;
666 ataio->cmd.device = ATA_DEV_LBA;
667 ataio->cmd.lba_low_exp = lba >> 24;
668 ataio->cmd.lba_mid_exp = lba >> 32;
669 ataio->cmd.lba_high_exp = lba >> 40;
670 ataio->cmd.features_exp = features >> 8;
671 ataio->cmd.sector_count = sector_count;
672 ataio->cmd.sector_count_exp = sector_count >> 8;
673 ataio->cmd.control = 0;
674 }
675
676 void
ata_ncq_cmd(struct ccb_ataio * ataio,uint8_t cmd,uint64_t lba,uint16_t sector_count)677 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
678 uint64_t lba, uint16_t sector_count)
679 {
680
681 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
682 ataio->cmd.command = cmd;
683 ataio->cmd.features = sector_count;
684 ataio->cmd.lba_low = lba;
685 ataio->cmd.lba_mid = lba >> 8;
686 ataio->cmd.lba_high = lba >> 16;
687 ataio->cmd.device = ATA_DEV_LBA;
688 ataio->cmd.lba_low_exp = lba >> 24;
689 ataio->cmd.lba_mid_exp = lba >> 32;
690 ataio->cmd.lba_high_exp = lba >> 40;
691 ataio->cmd.features_exp = sector_count >> 8;
692 ataio->cmd.sector_count = 0;
693 ataio->cmd.sector_count_exp = 0;
694 ataio->cmd.control = 0;
695 }
696
697 void
ata_reset_cmd(struct ccb_ataio * ataio)698 ata_reset_cmd(struct ccb_ataio *ataio)
699 {
700 bzero(&ataio->cmd, sizeof(ataio->cmd));
701 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
702 ataio->cmd.control = 0x04;
703 }
704
705 void
ata_pm_read_cmd(struct ccb_ataio * ataio,int reg,int port)706 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
707 {
708 bzero(&ataio->cmd, sizeof(ataio->cmd));
709 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
710 ataio->cmd.command = ATA_READ_PM;
711 ataio->cmd.features = reg;
712 ataio->cmd.device = port & 0x0f;
713 }
714
715 void
ata_pm_write_cmd(struct ccb_ataio * ataio,int reg,int port,uint32_t val)716 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
717 {
718 bzero(&ataio->cmd, sizeof(ataio->cmd));
719 ataio->cmd.flags = 0;
720 ataio->cmd.command = ATA_WRITE_PM;
721 ataio->cmd.features = reg;
722 ataio->cmd.sector_count = val;
723 ataio->cmd.lba_low = val >> 8;
724 ataio->cmd.lba_mid = val >> 16;
725 ataio->cmd.lba_high = val >> 24;
726 ataio->cmd.device = port & 0x0f;
727 }
728
729 void
ata_read_log(struct ccb_ataio * ataio,uint32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),uint32_t log_address,uint32_t page_number,uint16_t block_count,uint32_t protocol,uint8_t * data_ptr,uint32_t dxfer_len,uint32_t timeout)730 ata_read_log(struct ccb_ataio *ataio, uint32_t retries,
731 void (*cbfcnp)(struct cam_periph *, union ccb *),
732 uint32_t log_address, uint32_t page_number, uint16_t block_count,
733 uint32_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
734 uint32_t timeout)
735 {
736 uint64_t lba;
737
738 cam_fill_ataio(ataio,
739 /*retries*/ 1,
740 /*cbfcnp*/ cbfcnp,
741 /*flags*/ CAM_DIR_IN,
742 /*tag_action*/ 0,
743 /*data_ptr*/ data_ptr,
744 /*dxfer_len*/ dxfer_len,
745 /*timeout*/ timeout);
746
747 lba = (((uint64_t)page_number & 0xff00) << 32) |
748 ((page_number & 0x00ff) << 8) |
749 (log_address & 0xff);
750
751 ata_48bit_cmd(ataio,
752 /*cmd*/ (protocol & CAM_ATAIO_DMA) ? ATA_READ_LOG_DMA_EXT :
753 ATA_READ_LOG_EXT,
754 /*features*/ 0,
755 /*lba*/ lba,
756 /*sector_count*/ block_count);
757 }
758
759 void
ata_bswap(int8_t * buf,int len)760 ata_bswap(int8_t *buf, int len)
761 {
762 uint16_t *ptr = (uint16_t*)(buf + len);
763
764 while (--ptr >= (uint16_t*)buf)
765 *ptr = be16toh(*ptr);
766 }
767
768 void
ata_btrim(int8_t * buf,int len)769 ata_btrim(int8_t *buf, int len)
770 {
771 int8_t *ptr;
772
773 for (ptr = buf; ptr < buf+len; ++ptr)
774 if (!*ptr || *ptr == '_')
775 *ptr = ' ';
776 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
777 *ptr = 0;
778 }
779
780 void
ata_bpack(int8_t * src,int8_t * dst,int len)781 ata_bpack(int8_t *src, int8_t *dst, int len)
782 {
783 int i, j, blank;
784
785 for (i = j = blank = 0 ; i < len; i++) {
786 if (blank && src[i] == ' ') continue;
787 if (blank && src[i] != ' ') {
788 dst[j++] = src[i];
789 blank = 0;
790 continue;
791 }
792 if (src[i] == ' ') {
793 blank = 1;
794 if (i == 0)
795 continue;
796 }
797 dst[j++] = src[i];
798 }
799 while (j < len)
800 dst[j++] = 0x00;
801 }
802
803 int
ata_max_pmode(struct ata_params * ap)804 ata_max_pmode(struct ata_params *ap)
805 {
806 if (ap->atavalid & ATA_FLAG_64_70) {
807 if (ap->apiomodes & 0x02)
808 return ATA_PIO4;
809 if (ap->apiomodes & 0x01)
810 return ATA_PIO3;
811 }
812 if (ap->mwdmamodes & 0x04)
813 return ATA_PIO4;
814 if (ap->mwdmamodes & 0x02)
815 return ATA_PIO3;
816 if (ap->mwdmamodes & 0x01)
817 return ATA_PIO2;
818 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
819 return ATA_PIO2;
820 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
821 return ATA_PIO1;
822 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
823 return ATA_PIO0;
824 return ATA_PIO0;
825 }
826
827 int
ata_max_wmode(struct ata_params * ap)828 ata_max_wmode(struct ata_params *ap)
829 {
830 if (ap->mwdmamodes & 0x04)
831 return ATA_WDMA2;
832 if (ap->mwdmamodes & 0x02)
833 return ATA_WDMA1;
834 if (ap->mwdmamodes & 0x01)
835 return ATA_WDMA0;
836 return -1;
837 }
838
839 int
ata_max_umode(struct ata_params * ap)840 ata_max_umode(struct ata_params *ap)
841 {
842 if (ap->atavalid & ATA_FLAG_88) {
843 if (ap->udmamodes & 0x40)
844 return ATA_UDMA6;
845 if (ap->udmamodes & 0x20)
846 return ATA_UDMA5;
847 if (ap->udmamodes & 0x10)
848 return ATA_UDMA4;
849 if (ap->udmamodes & 0x08)
850 return ATA_UDMA3;
851 if (ap->udmamodes & 0x04)
852 return ATA_UDMA2;
853 if (ap->udmamodes & 0x02)
854 return ATA_UDMA1;
855 if (ap->udmamodes & 0x01)
856 return ATA_UDMA0;
857 }
858 return -1;
859 }
860
861 int
ata_max_mode(struct ata_params * ap,int maxmode)862 ata_max_mode(struct ata_params *ap, int maxmode)
863 {
864
865 if (maxmode == 0)
866 maxmode = ATA_DMA_MAX;
867 if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
868 return (min(maxmode, ata_max_umode(ap)));
869 if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
870 return (min(maxmode, ata_max_wmode(ap)));
871 return (min(maxmode, ata_max_pmode(ap)));
872 }
873
874 char *
ata_mode2string(int mode)875 ata_mode2string(int mode)
876 {
877 switch (mode) {
878 case -1: return "UNSUPPORTED";
879 case 0: return "NONE";
880 case ATA_PIO0: return "PIO0";
881 case ATA_PIO1: return "PIO1";
882 case ATA_PIO2: return "PIO2";
883 case ATA_PIO3: return "PIO3";
884 case ATA_PIO4: return "PIO4";
885 case ATA_WDMA0: return "WDMA0";
886 case ATA_WDMA1: return "WDMA1";
887 case ATA_WDMA2: return "WDMA2";
888 case ATA_UDMA0: return "UDMA0";
889 case ATA_UDMA1: return "UDMA1";
890 case ATA_UDMA2: return "UDMA2";
891 case ATA_UDMA3: return "UDMA3";
892 case ATA_UDMA4: return "UDMA4";
893 case ATA_UDMA5: return "UDMA5";
894 case ATA_UDMA6: return "UDMA6";
895 default:
896 if (mode & ATA_DMA_MASK)
897 return "BIOSDMA";
898 else
899 return "BIOSPIO";
900 }
901 }
902
903 int
ata_string2mode(char * str)904 ata_string2mode(char *str)
905 {
906 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
907 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
908 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
909 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
910 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
911 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
912 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
913 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
914 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
915 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
916 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
917 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
918 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
919 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
920 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
921 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
922 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
923 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
924 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
925 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
926 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
927 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
928 return (-1);
929 }
930
931 u_int
ata_mode2speed(int mode)932 ata_mode2speed(int mode)
933 {
934 switch (mode) {
935 case ATA_PIO0:
936 default:
937 return (3300);
938 case ATA_PIO1:
939 return (5200);
940 case ATA_PIO2:
941 return (8300);
942 case ATA_PIO3:
943 return (11100);
944 case ATA_PIO4:
945 return (16700);
946 case ATA_WDMA0:
947 return (4200);
948 case ATA_WDMA1:
949 return (13300);
950 case ATA_WDMA2:
951 return (16700);
952 case ATA_UDMA0:
953 return (16700);
954 case ATA_UDMA1:
955 return (25000);
956 case ATA_UDMA2:
957 return (33300);
958 case ATA_UDMA3:
959 return (44400);
960 case ATA_UDMA4:
961 return (66700);
962 case ATA_UDMA5:
963 return (100000);
964 case ATA_UDMA6:
965 return (133000);
966 }
967 }
968
969 u_int
ata_revision2speed(int revision)970 ata_revision2speed(int revision)
971 {
972 switch (revision) {
973 case 1:
974 default:
975 return (150000);
976 case 2:
977 return (300000);
978 case 3:
979 return (600000);
980 }
981 }
982
983 int
ata_speed2revision(u_int speed)984 ata_speed2revision(u_int speed)
985 {
986 switch (speed) {
987 case 0:
988 return (0);
989 case 150000:
990 return (1);
991 case 300000:
992 return (2);
993 case 600000:
994 return (3);
995 default:
996 return (-1);
997 }
998 }
999
1000 int
ata_identify_match(caddr_t identbuffer,caddr_t table_entry)1001 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
1002 {
1003 struct scsi_inquiry_pattern *entry;
1004 struct ata_params *ident;
1005
1006 entry = (struct scsi_inquiry_pattern *)table_entry;
1007 ident = (struct ata_params *)identbuffer;
1008
1009 if ((cam_strmatch(ident->model, entry->product,
1010 sizeof(ident->model)) == 0)
1011 && (cam_strmatch(ident->revision, entry->revision,
1012 sizeof(ident->revision)) == 0)) {
1013 return (0);
1014 }
1015 return (-1);
1016 }
1017
1018 int
ata_static_identify_match(caddr_t identbuffer,caddr_t table_entry)1019 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
1020 {
1021 struct scsi_static_inquiry_pattern *entry;
1022 struct ata_params *ident;
1023
1024 entry = (struct scsi_static_inquiry_pattern *)table_entry;
1025 ident = (struct ata_params *)identbuffer;
1026
1027 if ((cam_strmatch(ident->model, entry->product,
1028 sizeof(ident->model)) == 0)
1029 && (cam_strmatch(ident->revision, entry->revision,
1030 sizeof(ident->revision)) == 0)) {
1031 return (0);
1032 }
1033 return (-1);
1034 }
1035
1036 void
semb_receive_diagnostic_results(struct ccb_ataio * ataio,uint32_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)1037 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
1038 uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1039 uint8_t tag_action, int pcv, uint8_t page_code,
1040 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1041 {
1042
1043 length = min(length, 1020);
1044 length = (length + 3) & ~3;
1045 cam_fill_ataio(ataio,
1046 retries,
1047 cbfcnp,
1048 /*flags*/CAM_DIR_IN,
1049 tag_action,
1050 data_ptr,
1051 length,
1052 timeout);
1053 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1054 pcv ? page_code : 0, 0x02, length / 4);
1055 }
1056
1057 void
semb_send_diagnostic(struct ccb_ataio * ataio,uint32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),uint8_t tag_action,uint8_t * data_ptr,uint16_t length,uint32_t timeout)1058 semb_send_diagnostic(struct ccb_ataio *ataio,
1059 uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1060 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1061 {
1062
1063 length = min(length, 1020);
1064 length = (length + 3) & ~3;
1065 cam_fill_ataio(ataio,
1066 retries,
1067 cbfcnp,
1068 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1069 tag_action,
1070 data_ptr,
1071 length,
1072 timeout);
1073 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1074 length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
1075 }
1076
1077 void
semb_read_buffer(struct ccb_ataio * ataio,uint32_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)1078 semb_read_buffer(struct ccb_ataio *ataio,
1079 uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1080 uint8_t tag_action, uint8_t page_code,
1081 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1082 {
1083
1084 length = min(length, 1020);
1085 length = (length + 3) & ~3;
1086 cam_fill_ataio(ataio,
1087 retries,
1088 cbfcnp,
1089 /*flags*/CAM_DIR_IN,
1090 tag_action,
1091 data_ptr,
1092 length,
1093 timeout);
1094 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1095 page_code, 0x00, length / 4);
1096 }
1097
1098 void
semb_write_buffer(struct ccb_ataio * ataio,uint32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),uint8_t tag_action,uint8_t * data_ptr,uint16_t length,uint32_t timeout)1099 semb_write_buffer(struct ccb_ataio *ataio,
1100 uint32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1101 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1102 {
1103
1104 length = min(length, 1020);
1105 length = (length + 3) & ~3;
1106 cam_fill_ataio(ataio,
1107 retries,
1108 cbfcnp,
1109 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1110 tag_action,
1111 data_ptr,
1112 length,
1113 timeout);
1114 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1115 length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
1116 }
1117
1118 void
ata_zac_mgmt_out(struct ccb_ataio * ataio,uint32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),int use_ncq,uint8_t zm_action,uint64_t zone_id,uint8_t zone_flags,uint16_t sector_count,uint8_t * data_ptr,uint32_t dxfer_len,uint32_t timeout)1119 ata_zac_mgmt_out(struct ccb_ataio *ataio, uint32_t retries,
1120 void (*cbfcnp)(struct cam_periph *, union ccb *),
1121 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1122 uint8_t zone_flags, uint16_t sector_count, uint8_t *data_ptr,
1123 uint32_t dxfer_len, uint32_t timeout)
1124 {
1125 uint8_t command_out, ata_flags;
1126 uint16_t features_out, sectors_out;
1127 uint32_t auxiliary;
1128
1129 if (use_ncq == 0) {
1130 command_out = ATA_ZAC_MANAGEMENT_OUT;
1131 features_out = (zm_action & 0xf) | (zone_flags << 8);
1132 if (dxfer_len == 0) {
1133 ata_flags = 0;
1134 sectors_out = 0;
1135 } else {
1136 ata_flags = CAM_ATAIO_DMA;
1137 /* XXX KDM use sector count? */
1138 sectors_out = ((dxfer_len >> 9) & 0xffff);
1139 }
1140 auxiliary = 0;
1141 } else {
1142 if (dxfer_len == 0) {
1143 command_out = ATA_NCQ_NON_DATA;
1144 features_out = ATA_NCQ_ZAC_MGMT_OUT;
1145 sectors_out = 0;
1146 } else {
1147 command_out = ATA_SEND_FPDMA_QUEUED;
1148
1149 /* Note that we're defaulting to normal priority */
1150 sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
1151
1152 /*
1153 * For SEND FPDMA QUEUED, the transfer length is
1154 * encoded in the FEATURE register, and 0 means
1155 * that 65536 512 byte blocks are to be tranferred.
1156 * In practice, it seems unlikely that we'll see
1157 * a transfer that large.
1158 */
1159 if (dxfer_len == (65536 * 512)) {
1160 features_out = 0;
1161 } else {
1162 /*
1163 * Yes, the caller can theoretically send a
1164 * transfer larger than we can handle.
1165 * Anyone using this function needs enough
1166 * knowledge to avoid doing that.
1167 */
1168 features_out = ((dxfer_len >> 9) & 0xffff);
1169 }
1170 }
1171 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1172
1173 ata_flags = CAM_ATAIO_FPDMA;
1174 }
1175
1176 cam_fill_ataio(ataio,
1177 /*retries*/ retries,
1178 /*cbfcnp*/ cbfcnp,
1179 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1180 /*tag_action*/ 0,
1181 /*data_ptr*/ data_ptr,
1182 /*dxfer_len*/ dxfer_len,
1183 /*timeout*/ timeout);
1184
1185 ata_48bit_cmd(ataio,
1186 /*cmd*/ command_out,
1187 /*features*/ features_out,
1188 /*lba*/ zone_id,
1189 /*sector_count*/ sectors_out);
1190
1191 ataio->cmd.flags |= ata_flags;
1192 if (auxiliary != 0) {
1193 ataio->ata_flags |= ATA_FLAG_AUX;
1194 ataio->aux = auxiliary;
1195 }
1196 }
1197
1198 void
ata_zac_mgmt_in(struct ccb_ataio * ataio,uint32_t retries,void (* cbfcnp)(struct cam_periph *,union ccb *),int use_ncq,uint8_t zm_action,uint64_t zone_id,uint8_t zone_flags,uint8_t * data_ptr,uint32_t dxfer_len,uint32_t timeout)1199 ata_zac_mgmt_in(struct ccb_ataio *ataio, uint32_t retries,
1200 void (*cbfcnp)(struct cam_periph *, union ccb *),
1201 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1202 uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
1203 uint32_t timeout)
1204 {
1205 uint8_t command_out, ata_flags;
1206 uint16_t features_out, sectors_out;
1207 uint32_t auxiliary;
1208
1209 if (use_ncq == 0) {
1210 command_out = ATA_ZAC_MANAGEMENT_IN;
1211 /* XXX KDM put a macro here */
1212 features_out = (zm_action & 0xf) | (zone_flags << 8);
1213 ata_flags = CAM_ATAIO_DMA;
1214 sectors_out = ((dxfer_len >> 9) & 0xffff);
1215 auxiliary = 0;
1216 } else {
1217 command_out = ATA_RECV_FPDMA_QUEUED;
1218 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
1219 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1220 ata_flags = CAM_ATAIO_FPDMA;
1221 /*
1222 * For RECEIVE FPDMA QUEUED, the transfer length is
1223 * encoded in the FEATURE register, and 0 means
1224 * that 65536 512 byte blocks are to be tranferred.
1225 * In practice, it is unlikely we will see a transfer that
1226 * large.
1227 */
1228 if (dxfer_len == (65536 * 512)) {
1229 features_out = 0;
1230 } else {
1231 /*
1232 * Yes, the caller can theoretically request a
1233 * transfer larger than we can handle.
1234 * Anyone using this function needs enough
1235 * knowledge to avoid doing that.
1236 */
1237 features_out = ((dxfer_len >> 9) & 0xffff);
1238 }
1239 }
1240
1241 cam_fill_ataio(ataio,
1242 /*retries*/ retries,
1243 /*cbfcnp*/ cbfcnp,
1244 /*flags*/ CAM_DIR_IN,
1245 /*tag_action*/ 0,
1246 /*data_ptr*/ data_ptr,
1247 /*dxfer_len*/ dxfer_len,
1248 /*timeout*/ timeout);
1249
1250 ata_48bit_cmd(ataio,
1251 /*cmd*/ command_out,
1252 /*features*/ features_out,
1253 /*lba*/ zone_id,
1254 /*sector_count*/ sectors_out);
1255
1256 ataio->cmd.flags |= ata_flags;
1257 if (auxiliary != 0) {
1258 ataio->ata_flags |= ATA_FLAG_AUX;
1259 ataio->aux = auxiliary;
1260 }
1261 }
1262
1263 void
ata_param_fixup(struct ata_params * ident_buf)1264 ata_param_fixup(struct ata_params *ident_buf)
1265 {
1266 int16_t *ptr;
1267
1268 for (ptr = (int16_t *)ident_buf;
1269 ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
1270 *ptr = le16toh(*ptr);
1271 }
1272 if (strncmp(ident_buf->model, "FX", 2) &&
1273 strncmp(ident_buf->model, "NEC", 3) &&
1274 strncmp(ident_buf->model, "Pioneer", 7) &&
1275 strncmp(ident_buf->model, "SHARP", 5)) {
1276 ata_bswap(ident_buf->model, sizeof(ident_buf->model));
1277 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
1278 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
1279 }
1280 ata_btrim(ident_buf->model, sizeof(ident_buf->model));
1281 ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
1282 ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
1283 ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
1284 ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
1285 ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
1286 }
1287