xref: /haiku/src/add-ons/kernel/bus_managers/ata/ATAInfoBlock.h (revision c302a243e15e640fae0f689e32cdf0c18749afee)
1 /*
2  * Copyright 2010-2015, Axel Dörfler, axeld@pinc-software.de.
3  * Copyright 2009, Michael Lotz, mmlr@mlotz.ch.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef ATA_INFOBLOCK_H
7 #define ATA_INFOBLOCK_H
8 
9 
10 #include <lendian_bitfield.h>
11 
12 
13 #define ATA_WORD_0_ATA_DEVICE		0
14 #define ATA_WORD_0_ATAPI_DEVICE		2
15 #define ATA_WORD_0_CFA_MAGIC		0x848a
16 
17 
18 typedef struct ata_device_infoblock {
19 	union {
20 		struct {
21 			B_LBITFIELD16_8(
22 				word_0_bit_0_reserved			: 1,
23 				word_0_bit_1_retired			: 1,
24 				response_incomplete				: 1,
25 				word_0_bit_3_5_retired			: 3,
26 				word_0_bit_6_obsolete			: 1,
27 				removable_media_device			: 1,
28 				word_0_bit_8_14_retired			: 7,
29 				ata_device						: 1		// 0 means ATA
30 			);
31 		} ata;
32 		struct {
33 			B_LBITFIELD16_8(
34 				packet_length					: 2,	// 0 = 12, 1 = 16 bytes
35 				response_incomplete				: 1,
36 				word_0_bit_3_4_reserved			: 2,
37 				data_request_delay				: 2,	// 0 = 3ms, 2 = 50us
38 				removable_media_device			: 1,
39 				command_packet_set				: 5,
40 				word_0_bit_13_reserved			: 1,
41 				atapi_device					: 2		// 2 means ATAPI
42 			);
43 		} atapi;
44 		uint16 raw;
45 	} word_0;
46 
47 	uint16	word_1_obsolete;
48 	uint16	specific_configuration;
49 	uint16	word_3_obsolete;
50 	uint16	word_4_5_retired[2];
51 	uint16	word_6_obsolete;
52 	uint16	word_7_8_reserved_compact_flash_assoc[2];
53 	uint16	word_9_retired;
54 	char	serial_number[20];
55 	uint16	word_20_21_retired[2];
56 	uint16	word_22_obsolete;
57 	char	firmware_revision[8];
58 	char	model_number[40];
59 
60 	B_LBITFIELD16_2(
61 		max_sectors_per_interrupt				: 8,
62 		word_47_bit_8_15_80h					: 8		// should be 0x80
63 	);
64 
65 	uint16	word_48_reserved;
66 
67 	B_LBITFIELD16_9(
68 		word_49_bit_0_7_retired					: 8,
69 		dma_supported							: 1,
70 		lba_supported							: 1,
71 		io_ready_disable						: 1,
72 		io_ready_supported						: 1,
73 		word_19_bit_12_obsolete					: 1,
74 		standby_timer_standard					: 1,
75 		atapi_command_queuing_supported			: 1,
76 		atapi_interleaved_dma_supported			: 1
77 	);
78 
79 	B_LBITFIELD16_5(
80 		standby_timer_value_min					: 1,
81 		word_50_bit_1_obsolete					: 1,
82 		word_50_bit_2_13_reserved				: 12,
83 		word_50_bit_14_one						: 1,
84 		word_50_bit_15_zero						: 1
85 	);
86 
87 	uint16	word_51_52_obsolete[2];
88 
89 	B_LBITFIELD16_4(
90 		word_53_bit_0_obsolete					: 1,
91 		word_64_70_valid						: 1,
92 		word_88_valid							: 1,
93 		word_53_bit_3_15						: 13
94 	);
95 
96 	uint16	word_54_58_obsolete[5];
97 
98 	B_LBITFIELD16_3(
99 		current_sectors_per_interrupt			: 8,
100 		multiple_sector_setting_valid			: 1,
101 		word_59_bit_9_15_reserved				: 7
102 	);
103 
104 	uint32	lba_sector_count;
105 	uint16	word_62_obsolete;
106 
107 	B_LBITFIELD16_8(
108 		multiword_dma_0_supported				: 1,
109 		multiword_dma_1_supported				: 1,
110 		multiword_dma_2_supported				: 1,
111 		word_63_bit_3_7_resereved				: 5,
112 		multiword_dma_0_selected				: 1,
113 		multiword_dma_1_selected				: 1,
114 		multiword_dma_2_selected				: 1,
115 		word_63_bit_11_15_reserved				: 5
116 	);
117 
118 	B_LBITFIELD16_2(
119 		pio_modes_supported						: 8,
120 		word_64_bit_8_15_reserved				: 8
121 	);
122 
123 	uint16	min_multiword_dma_cycle_time;
124 	uint16	recommended_multiword_dma_cycle_time;
125 	uint16	min_pio_cycle_time;
126 	uint16	min_pio_cycle_time_io_ready;
127 	B_LBITFIELD16_12(
128 		word_69_bit_0_4_reserved				: 5,
129 		supports_read_zero_after_trim			: 1,
130 		supports_ata28_commands					: 1,
131 		word_69_bit_7_reserved					: 1,
132 		supports_download_microcode_dma			: 1,
133 		supports_set_max_password_unlock_dma	: 1,
134 		supports_write_buffer_dma				: 1,
135 		supports_read_buffer_dma				: 1,
136 		supports_device_configuration_identify_dma		: 1,
137 		supports_long_physical_sector_error_reporting	: 1,
138 		supports_deterministic_read_after_trim	: 1,
139 		supports_cfast_specification			: 1
140 	);
141 	uint16	word_70_reserved;
142 	uint16	atapi_packet_received_to_bus_release_time_ns;
143 	uint16	atapi_service_command_to_busy_clear_time_ns;
144 	uint16	word_71_74_reserved[2];
145 
146 	B_LBITFIELD16_2(
147 		max_queue_depth_minus_one				: 5,
148 		word_75_bit_5_15_reserved				: 11
149 	);
150 
151 	uint16	word_76_79_reserved[4];
152 
153 	B_LBITFIELD16_14(
154 		word_80_bit_0_reserved					: 1,
155 		word_80_bit_1_3_obsolete				: 3,
156 		supports_ata_atapi_4					: 1,
157 		supports_ata_atapi_5					: 1,
158 		supports_ata_atapi_6					: 1,
159 		supports_ata_atapi_7					: 1,
160 		supports_ata_atapi_8					: 1,
161 		supports_ata_atapi_9					: 1,
162 		supports_ata_atapi_10					: 1,
163 		supports_ata_atapi_11					: 1,
164 		supports_ata_atapi_12					: 1,
165 		supports_ata_atapi_13					: 1,
166 		supports_ata_atapi_14					: 1,
167 		word_80_bit_15_reserved					: 1
168 	);
169 
170 	uint16	minor_version;
171 
172 	B_LBITFIELD16_16(
173 		smart_supported							: 1,
174 		security_mode_supported					: 1,
175 		removable_media_supported				: 1,
176 		mandatory_power_management_supported	: 1,
177 		packet_supported						: 1,
178 		write_cache_supported					: 1,
179 		look_ahead_supported					: 1,
180 		release_interrupt_supported				: 1,
181 		service_interrupt_supported				: 1,
182 		device_reset_supported					: 1,
183 		host_protected_area_supported			: 1,
184 		word_82_bit_11_obsolete					: 1,
185 		write_buffer_command_supported			: 1,
186 		read_buffer_command_supported			: 1,
187 		nop_supported							: 1,
188 		word_82_bit_15_obsolete					: 1
189 	);
190 
191 	B_LBITFIELD16_16(
192 		download_microcode_supported			: 1,
193 		read_write_dma_queued_supported			: 1,
194 		compact_flash_assoc_supported			: 1,
195 		advanced_power_management_supported		: 1,
196 		removable_media_status_supported		: 1,
197 		power_up_in_standby_supported			: 1,
198 		set_features_required_for_spinup		: 1,
199 		word_83_bit_7_reserved					: 1,
200 		set_max_security_extension_supported	: 1,
201 		automatic_acoustic_management_supported	: 1,
202 		lba48_supported							: 1,
203 		device_configuration_overlay_supported	: 1,
204 		mandatory_flush_cache_supported			: 1,
205 		flush_cache_ext_supported				: 1,
206 		word_83_bit_14_one						: 1,
207 		word_83_bit_15_zero						: 1
208 	);
209 
210 	B_LBITFIELD16_9(
211 		smart_error_logging_supported			: 1,
212 		smart_self_test_supported				: 1,
213 		media_serial_number_supported			: 1,
214 		media_card_pass_through_supported		: 1,
215 		word_84_bit_4_reserved					: 1,
216 		general_purpose_logging_supported		: 1,
217 		word_84_bit_6_13_reserved				: 8,
218 		word_84_bit_14_one						: 1,
219 		word_84_bit_15_zero						: 1
220 	);
221 
222 	B_LBITFIELD16_16(
223 		smart_enabled							: 1,
224 		security_mode_enabled					: 1,
225 		removable_media_enabled					: 1,
226 		mandatory_power_management_enabled		: 1,
227 		packet_enabled							: 1,
228 		write_cache_enabled						: 1,
229 		look_ahead_enabled						: 1,
230 		release_interrupt_enabled				: 1,
231 		service_interrupt_enabled				: 1,
232 		device_reset_enabled					: 1,
233 		host_protected_area_enabled				: 1,
234 		word_85_bit_11_obsolete					: 1,
235 		write_buffer_command_enabled			: 1,
236 		read_buffer_command_enabled				: 1,
237 		nop_enabled								: 1,
238 		word_85_bit_15_obsolete					: 1
239 	);
240 
241 	B_LBITFIELD16_15(
242 		download_microcode_supported_2			: 1,
243 		read_write_dma_queued_supported_2		: 1,
244 		compact_flash_assoc_enabled				: 1,
245 		advanced_power_management_enabled		: 1,
246 		removable_media_status_enabled			: 1,
247 		power_up_in_standby_enabled				: 1,
248 		set_features_required_for_spinup_2		: 1,
249 		word_86_bit_7_reserved					: 1,
250 		set_max_security_extension_enabled		: 1,
251 		automatic_acoustic_management_enabled	: 1,
252 		lba48_supported_2						: 1,
253 		device_configuration_overlay_supported_2: 1,
254 		mandatory_flush_cache_supported_2		: 1,
255 		flush_cache_ext_supported_2				: 1,
256 		word_86_bit_14_15_reserved				: 2
257 	);
258 
259 	B_LBITFIELD16_9(
260 		smart_error_logging_supported_2			: 1,
261 		smart_self_test_supported_2				: 1,
262 		media_serial_number_valid				: 1,
263 		media_card_pass_through_enabled			: 1,
264 		word_87_bit_4_reserved					: 1,
265 		general_purpose_logging_supported_2		: 1,
266 		word_87_bit_6_13_reserved				: 8,
267 		word_87_bit_14_one						: 1,
268 		word_87_bit_15_zero						: 1
269 	);
270 
271 	B_LBITFIELD16_16(
272 		ultra_dma_0_supported					: 1,
273 		ultra_dma_1_supported					: 1,
274 		ultra_dma_2_supported					: 1,
275 		ultra_dma_3_supported					: 1,
276 		ultra_dma_4_supported					: 1,
277 		ultra_dma_5_supported					: 1,
278 		ultra_dma_6_supported					: 1,
279 		word_88_bit_7_reserved					: 1,
280 		ultra_dma_0_selected					: 1,
281 		ultra_dma_1_selected					: 1,
282 		ultra_dma_2_selected					: 1,
283 		ultra_dma_3_selected					: 1,
284 		ultra_dma_4_selected					: 1,
285 		ultra_dma_5_selected					: 1,
286 		ultra_dma_6_selected					: 1,
287 		word_88_bit_15_reserved					: 1
288 	);
289 
290 	uint16	security_erase_unit_duration;
291 	uint16	enhanced_security_erase_duration;
292 	uint16	current_advanced_power_management_value;
293 	uint16	master_password_revision_code;
294 
295 	B_LBITFIELD16_5(
296 		device_0_hardware_reset_result			: 8,
297 		device_1_hardware_reset_result			: 5,
298 		cable_id_detected						: 1,
299 		word_93_bit_14_one						: 1,
300 		word_93_bit_15_zero						: 1
301 	);
302 
303 	B_LBITFIELD16_2(
304 		current_acoustic_management_value		: 8,
305 		recommended_acoustic_management_value	: 8
306 	);
307 
308 	uint16	word_95_99_reserved[5];
309 	uint64	lba48_sector_count;
310 	uint16	word_104_reserved;
311 	uint16	max_data_set_management_lba_range_blocks;
312 
313 	B_LBITFIELD16_6(
314 		logical_sectors_per_physical_sector		: 4,	// 2^x exponent
315 		word_106_bit_4_11_reserved				: 8,
316 		logical_sector_not_512_bytes			: 1,
317 		multiple_logical_per_physical_sectors	: 1,
318 		word_106_bit_14_one						: 1,
319 		word_106_bit_15_zero					: 1
320 	);
321 
322 	uint16	word_107_116_reserved[10];
323 
324 	uint32	logical_sector_size;						// in words, see 106
325 
326 	uint16	word_119_126_reserved[8];
327 
328 	B_LBITFIELD16_2(
329 		removable_media_status_supported_2		: 2,	// 1 = supported
330 		word_127_bit_2_15_reserved				: 14
331 	);
332 
333 	B_LBITFIELD16_9(
334 		security_supported						: 1,
335 		security_enabled						: 1,
336 		security_locked							: 1,
337 		security_frozen							: 1,
338 		security_count_expired					: 1,
339 		ehnaced_security_erase_supported		: 1,
340 		word_128_bit_6_7_reserved				: 2,
341 		security_level							: 1,	// 0 = high, 1 = max
342 		word_128_bit_9_15						: 7
343 	);
344 
345 	uint16	word_129_159_vendor_specific[31];
346 
347 	B_LBITFIELD16_5(
348 		cfa_max_current_milli_ampers			: 12,
349 		cfa_power_mode_1_disabled				: 1,
350 		cfa_power_mode_1_required				: 1,
351 		word_160_bit_14_reserved				: 1,
352 		word_160_supported						: 1
353 	);
354 
355 	uint16	word_161_167_reserved_compact_flash_assoc[7];
356 	B_LBITFIELD16_2(
357 		device_nominal_form_factor				: 4,
358 		word_168_bits_4_15_reserved				: 12
359 	);
360 	B_LBITFIELD16_2(
361 		data_set_management_support				: 1,
362 		word_169_bits_1_15_reserved				: 15
363 	);
364 	uint16	additional_product_identifier[4];
365 	uint16	word_174_175_reserved[2];
366 	uint16	current_media_serial_number[30];
367 	uint16	word_206_208_reserved[3];
368 
369 	B_LBITFIELD16_3(
370 		logical_sector_offset					: 14,
371 		word_209_bit_14_one						: 1,
372 		word_209_bit_15_zero					: 1
373 	);
374 
375 	uint16	word_210_254_reserved[45];
376 
377 	B_LBITFIELD16_2(
378 		signature								: 8,
379 		checksum								: 8
380 	);
381 
SectorCountata_device_infoblock382 	uint64 SectorCount(bool& use48Bits, bool force)
383 	{
384 		if (lba48_supported && lba48_sector_count >= lba_sector_count) {
385 			use48Bits = true;
386 			return lba48_sector_count;
387 		}
388 
389 		use48Bits = force ? lba48_supported : false;
390 		return lba_sector_count;
391 	}
392 
PhysicalSectorSizeata_device_infoblock393 	uint32 PhysicalSectorSize()
394 	{
395 		uint32 blockSize = 512;
396 		if (word_106_bit_14_one && !word_106_bit_15_zero) {
397 			// contains a valid block size configuration
398 			if (logical_sector_not_512_bytes)
399 				blockSize = logical_sector_size * 2;
400 
401 			if (multiple_logical_per_physical_sectors)
402 				return blockSize << logical_sectors_per_physical_sector;
403 		}
404 		return blockSize;
405 	}
406 
SectorSizeata_device_infoblock407 	uint32 SectorSize()
408 	{
409 		if (word_106_bit_14_one && !word_106_bit_15_zero) {
410 			// contains a valid block size configuration
411 			if (logical_sector_not_512_bytes)
412 				return logical_sector_size * 2;
413 		}
414 		return 512;
415 	}
416 
BlockOffsetata_device_infoblock417 	uint32 BlockOffset()
418 	{
419 		if (word_209_bit_14_one && !word_209_bit_15_zero) {
420 			// contains a valid logical block offset configuration
421 			return logical_sector_offset;
422 		}
423 		return 0;
424 	}
425 } _PACKED ata_device_infoblock;
426 
427 
428 #endif // ATA_INFOBLOCK_H
429