]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/ata/ata_all.c
wpa: Import wpa_supplicant/hostapd commit f91680c15
[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 u_int
934 ata_mode2speed(int mode)
935 {
936         switch (mode) {
937         case ATA_PIO0:
938         default:
939                 return (3300);
940         case ATA_PIO1:
941                 return (5200);
942         case ATA_PIO2:
943                 return (8300);
944         case ATA_PIO3:
945                 return (11100);
946         case ATA_PIO4:
947                 return (16700);
948         case ATA_WDMA0:
949                 return (4200);
950         case ATA_WDMA1:
951                 return (13300);
952         case ATA_WDMA2:
953                 return (16700);
954         case ATA_UDMA0:
955                 return (16700);
956         case ATA_UDMA1:
957                 return (25000);
958         case ATA_UDMA2:
959                 return (33300);
960         case ATA_UDMA3:
961                 return (44400);
962         case ATA_UDMA4:
963                 return (66700);
964         case ATA_UDMA5:
965                 return (100000);
966         case ATA_UDMA6:
967                 return (133000);
968         }
969 }
970
971 u_int
972 ata_revision2speed(int revision)
973 {
974         switch (revision) {
975         case 1:
976         default:
977                 return (150000);
978         case 2:
979                 return (300000);
980         case 3:
981                 return (600000);
982         }
983 }
984
985 int
986 ata_speed2revision(u_int speed)
987 {
988         switch (speed) {
989         case 0:
990                 return (0);
991         case 150000:
992                 return (1);
993         case 300000:
994                 return (2);
995         case 600000:
996                 return (3);
997         default:
998                 return (-1);
999         }
1000 }
1001
1002 int
1003 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
1004 {
1005         struct scsi_inquiry_pattern *entry;
1006         struct ata_params *ident;
1007
1008         entry = (struct scsi_inquiry_pattern *)table_entry;
1009         ident = (struct ata_params *)identbuffer;
1010
1011         if ((cam_strmatch(ident->model, entry->product,
1012                           sizeof(ident->model)) == 0)
1013          && (cam_strmatch(ident->revision, entry->revision,
1014                           sizeof(ident->revision)) == 0)) {
1015                 return (0);
1016         }
1017         return (-1);
1018 }
1019
1020 int
1021 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
1022 {
1023         struct scsi_static_inquiry_pattern *entry;
1024         struct ata_params *ident;
1025
1026         entry = (struct scsi_static_inquiry_pattern *)table_entry;
1027         ident = (struct ata_params *)identbuffer;
1028
1029         if ((cam_strmatch(ident->model, entry->product,
1030                           sizeof(ident->model)) == 0)
1031          && (cam_strmatch(ident->revision, entry->revision,
1032                           sizeof(ident->revision)) == 0)) {
1033                 return (0);
1034         }
1035         return (-1);
1036 }
1037
1038 void
1039 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
1040     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1041     uint8_t tag_action, int pcv, uint8_t page_code,
1042     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1043 {
1044
1045         length = min(length, 1020);
1046         length = (length + 3) & ~3;
1047         cam_fill_ataio(ataio,
1048                       retries,
1049                       cbfcnp,
1050                       /*flags*/CAM_DIR_IN,
1051                       tag_action,
1052                       data_ptr,
1053                       length,
1054                       timeout);
1055         ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1056             pcv ? page_code : 0, 0x02, length / 4);
1057 }
1058
1059 void
1060 semb_send_diagnostic(struct ccb_ataio *ataio,
1061     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1062     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1063 {
1064
1065         length = min(length, 1020);
1066         length = (length + 3) & ~3;
1067         cam_fill_ataio(ataio,
1068                       retries,
1069                       cbfcnp,
1070                       /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1071                       tag_action,
1072                       data_ptr,
1073                       length,
1074                       timeout);
1075         ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1076             length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
1077 }
1078
1079 void
1080 semb_read_buffer(struct ccb_ataio *ataio,
1081     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1082     uint8_t tag_action, uint8_t page_code,
1083     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1084 {
1085
1086         length = min(length, 1020);
1087         length = (length + 3) & ~3;
1088         cam_fill_ataio(ataio,
1089                       retries,
1090                       cbfcnp,
1091                       /*flags*/CAM_DIR_IN,
1092                       tag_action,
1093                       data_ptr,
1094                       length,
1095                       timeout);
1096         ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1097             page_code, 0x00, length / 4);
1098 }
1099
1100 void
1101 semb_write_buffer(struct ccb_ataio *ataio,
1102     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1103     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1104 {
1105
1106         length = min(length, 1020);
1107         length = (length + 3) & ~3;
1108         cam_fill_ataio(ataio,
1109                       retries,
1110                       cbfcnp,
1111                       /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1112                       tag_action,
1113                       data_ptr,
1114                       length,
1115                       timeout);
1116         ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1117             length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
1118 }
1119
1120 void
1121 ata_zac_mgmt_out(struct ccb_ataio *ataio, uint32_t retries, 
1122                  void (*cbfcnp)(struct cam_periph *, union ccb *),
1123                  int use_ncq, uint8_t zm_action, uint64_t zone_id,
1124                  uint8_t zone_flags, uint16_t sector_count, uint8_t *data_ptr,
1125                  uint32_t dxfer_len, uint32_t timeout)
1126 {
1127         uint8_t command_out, ata_flags;
1128         uint16_t features_out, sectors_out;
1129         uint32_t auxiliary;
1130
1131         if (use_ncq == 0) {
1132                 command_out = ATA_ZAC_MANAGEMENT_OUT;
1133                 features_out = (zm_action & 0xf) | (zone_flags << 8);
1134                 if (dxfer_len == 0) {
1135                         ata_flags = 0;
1136                         sectors_out = 0;
1137                 } else {
1138                         ata_flags = CAM_ATAIO_DMA;
1139                         /* XXX KDM use sector count? */
1140                         sectors_out = ((dxfer_len >> 9) & 0xffff);
1141                 }
1142                 auxiliary = 0;
1143         } else {
1144                 if (dxfer_len == 0) {
1145                         command_out = ATA_NCQ_NON_DATA;
1146                         features_out = ATA_NCQ_ZAC_MGMT_OUT;
1147                         sectors_out = 0;
1148                 } else {
1149                         command_out = ATA_SEND_FPDMA_QUEUED;
1150
1151                         /* Note that we're defaulting to normal priority */
1152                         sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
1153
1154                         /*
1155                          * For SEND FPDMA QUEUED, the transfer length is
1156                          * encoded in the FEATURE register, and 0 means
1157                          * that 65536 512 byte blocks are to be tranferred.
1158                          * In practice, it seems unlikely that we'll see
1159                          * a transfer that large.
1160                          */
1161                         if (dxfer_len == (65536 * 512)) {
1162                                 features_out = 0;
1163                         } else {
1164                                 /*
1165                                  * Yes, the caller can theoretically send a
1166                                  * transfer larger than we can handle.
1167                                  * Anyone using this function needs enough
1168                                  * knowledge to avoid doing that.
1169                                  */
1170                                 features_out = ((dxfer_len >> 9) & 0xffff);
1171                         }
1172                 }
1173                 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1174
1175                 ata_flags = CAM_ATAIO_FPDMA;
1176         }
1177
1178         cam_fill_ataio(ataio,
1179             /*retries*/ retries,
1180             /*cbfcnp*/ cbfcnp,
1181             /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1182             /*tag_action*/ 0,
1183             /*data_ptr*/ data_ptr,
1184             /*dxfer_len*/ dxfer_len,
1185             /*timeout*/ timeout);
1186
1187         ata_48bit_cmd(ataio,
1188             /*cmd*/ command_out,
1189             /*features*/ features_out,
1190             /*lba*/ zone_id,
1191             /*sector_count*/ sectors_out);
1192
1193         ataio->cmd.flags |= ata_flags;
1194         if (auxiliary != 0) {
1195                 ataio->ata_flags |= ATA_FLAG_AUX;
1196                 ataio->aux = auxiliary;
1197         }
1198 }
1199
1200 void
1201 ata_zac_mgmt_in(struct ccb_ataio *ataio, uint32_t retries, 
1202                 void (*cbfcnp)(struct cam_periph *, union ccb *),
1203                 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1204                 uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
1205                 uint32_t timeout)
1206 {
1207         uint8_t command_out, ata_flags;
1208         uint16_t features_out, sectors_out;
1209         uint32_t auxiliary;
1210
1211         if (use_ncq == 0) {
1212                 command_out = ATA_ZAC_MANAGEMENT_IN;
1213                 /* XXX KDM put a macro here */
1214                 features_out = (zm_action & 0xf) | (zone_flags << 8);
1215                 ata_flags = CAM_ATAIO_DMA;
1216                 sectors_out = ((dxfer_len >> 9) & 0xffff);
1217                 auxiliary = 0;
1218         } else {
1219                 command_out = ATA_RECV_FPDMA_QUEUED;
1220                 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
1221                 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1222                 ata_flags = CAM_ATAIO_FPDMA;
1223                 /*
1224                  * For RECEIVE FPDMA QUEUED, the transfer length is
1225                  * encoded in the FEATURE register, and 0 means
1226                  * that 65536 512 byte blocks are to be tranferred.
1227                  * In practice, it is unlikely we will see a transfer that
1228                  * large.
1229                  */
1230                 if (dxfer_len == (65536 * 512)) {
1231                         features_out = 0;
1232                 } else {
1233                         /*
1234                          * Yes, the caller can theoretically request a
1235                          * transfer larger than we can handle.
1236                          * Anyone using this function needs enough
1237                          * knowledge to avoid doing that.
1238                          */
1239                         features_out = ((dxfer_len >> 9) & 0xffff);
1240                 }
1241         }
1242
1243         cam_fill_ataio(ataio,
1244             /*retries*/ retries,
1245             /*cbfcnp*/ cbfcnp,
1246             /*flags*/ CAM_DIR_IN,
1247             /*tag_action*/ 0,
1248             /*data_ptr*/ data_ptr,
1249             /*dxfer_len*/ dxfer_len,
1250             /*timeout*/ timeout);
1251
1252         ata_48bit_cmd(ataio,
1253             /*cmd*/ command_out,
1254             /*features*/ features_out,
1255             /*lba*/ zone_id,
1256             /*sector_count*/ sectors_out);
1257
1258         ataio->cmd.flags |= ata_flags;
1259         if (auxiliary != 0) {
1260                 ataio->ata_flags |= ATA_FLAG_AUX;
1261                 ataio->aux = auxiliary;
1262         }
1263 }
1264
1265 void
1266 ata_param_fixup(struct ata_params *ident_buf)
1267 {
1268         int16_t *ptr;
1269
1270         for (ptr = (int16_t *)ident_buf;
1271              ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
1272                 *ptr = le16toh(*ptr);
1273         }
1274         if (strncmp(ident_buf->model, "FX", 2) &&
1275             strncmp(ident_buf->model, "NEC", 3) &&
1276             strncmp(ident_buf->model, "Pioneer", 7) &&
1277             strncmp(ident_buf->model, "SHARP", 5)) {
1278                 ata_bswap(ident_buf->model, sizeof(ident_buf->model));
1279                 ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
1280                 ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
1281         }
1282         ata_btrim(ident_buf->model, sizeof(ident_buf->model));
1283         ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
1284         ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
1285         ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
1286         ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
1287         ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
1288 }