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