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