xref: /haiku/src/add-ons/kernel/bus_managers/ata/ATADevice.cpp (revision f058886908e994cc5a6c141e212c3bd62e923023)
1 /*
2  * Copyright 2009, Michael Lotz, mmlr@mlotz.ch.
3  * Copyright 2008, Marcus Overhagen.
4  * Copyright 2004-2008, Axel Dörfler, axeld@pinc-software.de.
5  * Copyright 2002-2003, Thomas Kurschel.
6  *
7  * Distributed under the terms of the MIT License.
8  */
9 
10 #include "ATAPrivate.h"
11 
12 
13 ATADevice::ATADevice(ATAChannel *channel, uint8 index)
14 	:
15 	fChannel(channel),
16 	fRegisterMask(0),
17 	fUseDMA(channel->UseDMA()),
18 	fDMAMode(0),
19 	fDMAFailures(0),
20 	fTotalSectors(0),
21 	fBlockSize(512),
22 	fPhysicalBlockSize(512),
23 	fBlockOffset(0),
24 	fIndex(index),
25 	fUse48Bits(false)
26 {
27 	memset(&fInfoBlock, 0, sizeof(fInfoBlock));
28 	memset(&fTaskFile, 0, sizeof(fTaskFile));
29 }
30 
31 
32 ATADevice::~ATADevice()
33 {
34 }
35 
36 
37 status_t
38 ATADevice::TestUnitReady(ATARequest *request)
39 {
40 	TRACE_FUNCTION("%p\n", request);
41 
42 	fRegisterMask = 0;
43 	fTaskFile.write.command = ATA_COMMAND_GET_MEDIA_STATUS;
44 
45 	request->SetTimeout(15 * 1000 * 1000);
46 	status_t result = fChannel->SendRequest(request, ATA_DEVICE_READY_REQUIRED);
47 	if (result != B_OK) {
48 		TRACE_ERROR("failed to send test unit ready request\n");
49 		return result;
50 	}
51 
52 	return fChannel->FinishRequest(request, ATA_WAIT_FINISH
53 		| ATA_DEVICE_READY_REQUIRED, ATA_ERROR_NO_MEDIA | ATA_ERROR_ABORTED
54 		| ATA_ERROR_MEDIA_CHANGE_REQUESTED | ATA_ERROR_MEDIUM_CHANGED);
55 }
56 
57 
58 status_t
59 ATADevice::SynchronizeCache(ATARequest *request)
60 {
61 	TRACE_FUNCTION("%p\n", request);
62 
63 	// we should also ask for FLUSH CACHE support, but everyone denies it
64 	// (looks like they cheat to gain some performance advantage, but
65 	//  that's pretty useless: everyone does it...)
66 	if (!fInfoBlock.write_cache_supported)
67 		return B_OK;
68 
69 	fRegisterMask = 0;
70 	fTaskFile.lba.command
71 		= fUse48Bits ? ATA_COMMAND_FLUSH_CACHE_EXT : ATA_COMMAND_FLUSH_CACHE;
72 
73 	request->SetTimeout(60 * 1000 * 1000);
74 	status_t result = fChannel->SendRequest(request, ATA_DEVICE_READY_REQUIRED);
75 	if (result != B_OK) {
76 		TRACE_ERROR("failed to send synchronize cache request\n");
77 		return result;
78 	}
79 
80 	return fChannel->FinishRequest(request, ATA_WAIT_FINISH
81 		| ATA_DEVICE_READY_REQUIRED, ATA_ERROR_ABORTED);
82 }
83 
84 
85 status_t
86 ATADevice::Eject(ATARequest *request)
87 {
88 	TRACE_FUNCTION("%p\n", request);
89 
90 	fRegisterMask = 0;
91 	fTaskFile.lba.command = ATA_COMMAND_MEDIA_EJECT;
92 
93 	request->SetTimeout(15 * 1000 * 1000);
94 	status_t result = fChannel->SendRequest(request, ATA_DEVICE_READY_REQUIRED);
95 	if (result != B_OK) {
96 		TRACE_ERROR("failed to send eject request\n");
97 		return result;
98 	}
99 
100 	return fChannel->FinishRequest(request, ATA_WAIT_FINISH
101 		| ATA_DEVICE_READY_REQUIRED, ATA_ERROR_ABORTED | ATA_ERROR_NO_MEDIA);
102 }
103 
104 
105 status_t
106 ATADevice::Inquiry(ATARequest *request)
107 {
108 	TRACE_FUNCTION("%p\n", request);
109 
110 	scsi_ccb *ccb = request->CCB();
111 	scsi_cmd_inquiry *command = (scsi_cmd_inquiry *)ccb->cdb;
112 	if (command->evpd || command->page_code) {
113 		request->SetSense(SCSIS_KEY_ILLEGAL_REQUEST, SCSIS_ASC_INV_CDB_FIELD);
114 		return B_ERROR;
115 	}
116 
117 	scsi_res_inquiry data;
118 	memset(&data, 0, sizeof(data));
119 
120 	data.device_type = IsATAPI()
121 		? fInfoBlock.word_0.atapi.command_packet_set : scsi_dev_direct_access;
122 	data.device_qualifier = scsi_periph_qual_connected;
123 
124 	data.device_type_modifier = 0;
125 	data.removable_medium = fInfoBlock.word_0.ata.removable_media_device;
126 
127 	data.ansi_version = 2;
128 	data.ecma_version = 0;
129 	data.iso_version = 0;
130 
131 	data.response_data_format = 2;
132 	data.term_iop = false;
133 		// to be changed if we support TERM I/O
134 
135 	data.additional_length = sizeof(scsi_res_inquiry) - 4;
136 
137 	data.soft_reset = false;
138 	data.cmd_queue = 0;
139 	data.linked = false;
140 
141 	// these values are free-style
142 	data.sync = false;
143 	data.write_bus16 = true;
144 	data.write_bus32 = false;
145 
146 	data.relative_address = false;
147 
148 	// the following fields are *much* to small, sigh...
149 	memcpy(data.vendor_ident, fInfoBlock.model_number,
150 		sizeof(data.vendor_ident));
151 	swap_words(data.vendor_ident, sizeof(data.vendor_ident));
152 
153 	memcpy(data.product_ident, fInfoBlock.model_number + 8,
154 		sizeof(data.product_ident));
155 	swap_words(data.product_ident, sizeof(data.product_ident));
156 
157 	memcpy(data.product_rev, "    ", sizeof(data.product_rev));
158 
159 	uint32 allocationLength = command->allocation_length;
160 	copy_sg_data(ccb, 0, allocationLength, &data, sizeof(data), false);
161 	ccb->data_resid = ccb->data_length - MIN(MIN(sizeof(data),
162 		allocationLength), ccb->data_length);
163 	return B_OK;
164 }
165 
166 
167 status_t
168 ATADevice::ReadCapacity(ATARequest *request)
169 {
170 	TRACE_FUNCTION("%p\n", request);
171 
172 	scsi_ccb *ccb = request->CCB();
173 	scsi_cmd_read_capacity *command = (scsi_cmd_read_capacity *)ccb->cdb;
174 	if (command->pmi || command->lba) {
175 		request->SetSense(SCSIS_KEY_ILLEGAL_REQUEST, SCSIS_ASC_INV_CDB_FIELD);
176 		return B_ERROR;
177 	}
178 
179 	scsi_res_read_capacity data;
180 	data.block_size = B_HOST_TO_BENDIAN_INT32(fBlockSize);
181 
182 	uint32 lastBlock = fTotalSectors - 1;
183 	data.lba = B_HOST_TO_BENDIAN_INT32(lastBlock);
184 	TRACE("returning last block: %lu\n", B_BENDIAN_TO_HOST_INT32(data.lba));
185 
186 	copy_sg_data(ccb, 0, ccb->data_length, &data, sizeof(data), false);
187 	ccb->data_resid = MAX(ccb->data_length - sizeof(data), 0);
188 	return B_OK;
189 }
190 
191 
192 status_t
193 ATADevice::ReadCapacity16(ATARequest *request)
194 {
195 	TRACE_FUNCTION("%p\n", request);
196 
197 	scsi_ccb *ccb = request->CCB();
198 	scsi_res_read_capacity_long data;
199 	data.block_size = B_HOST_TO_BENDIAN_INT32(fBlockSize);
200 
201 	uint64 lastBlock = fTotalSectors - 1;
202 	data.lba = (((uint64)B_HOST_TO_BENDIAN_INT32(lastBlock >> 32)) << 32)
203 		| B_HOST_TO_BENDIAN_INT32(lastBlock);
204 	TRACE("returning last block: %llu\n", data.lba);
205 
206 	copy_sg_data(ccb, 0, ccb->data_length, &data, sizeof(data), false);
207 	ccb->data_resid = MAX(ccb->data_length - sizeof(data), 0);
208 	return B_OK;
209 }
210 
211 
212 status_t
213 ATADevice::ExecuteIO(ATARequest *request)
214 {
215 	TRACE_FUNCTION("%p\n", request);
216 
217 	scsi_ccb *ccb = request->CCB();
218 	request->SetDevice(this);
219 
220 	// ATA devices have one LUN only
221 	if (ccb->target_lun != 0) {
222 		TRACE_ERROR("invalid target lun %d for ATA device\n", ccb->target_lun);
223 		request->SetStatus(SCSI_SEL_TIMEOUT);
224 		return B_BAD_INDEX;
225 	}
226 
227 	TRACE("request: 0x%02x\n", ccb->cdb[0]);
228 
229 	switch (ccb->cdb[0]) {
230 		case SCSI_OP_TEST_UNIT_READY:
231 			return TestUnitReady(request);
232 
233 		case SCSI_OP_FORMAT: /* FORMAT UNIT */
234 			// we could forward ccb to disk, but modern disks cannot
235 			// be formatted anyway, so we just refuse ccb
236 			// (exceptions are removable media devices, but to my knowledge
237 			// they don't have to be formatted as well)
238 			request->SetSense(SCSIS_KEY_ILLEGAL_REQUEST, SCSIS_ASC_INV_OPCODE);
239 			return B_ERROR;
240 
241 		case SCSI_OP_INQUIRY:
242  			return Inquiry(request);
243 
244 		case SCSI_OP_START_STOP:
245 		{
246 			scsi_cmd_ssu *command = (scsi_cmd_ssu *)ccb->cdb;
247 
248 			// with no LoEj bit set, we should only allow/deny further access
249 			// we ignore that (unsupported for ATA)
250 			// with LoEj bit set, we should additionally either load or eject
251 			// the medium (start = 0 - eject; start = 1 - load)
252 
253 			if (!command->start) {
254 				// we must always flush cache if start = 0
255 				SynchronizeCache(request);
256 			}
257 
258 			if (command->load_eject) {
259 				if (!command->start)
260 					return Eject(request);
261 				else {
262 					request->SetSense(SCSIS_KEY_ILLEGAL_REQUEST,
263 						SCSIS_ASC_PARAM_NOT_SUPPORTED);
264 					return B_ERROR;
265 				}
266 			}
267 
268 			return B_OK;
269 		}
270 
271 		case SCSI_OP_READ_CAPACITY:
272 			return ReadCapacity(request);
273 
274 		case SCSI_OP_SERVICE_ACTION_IN:
275 			if ((ccb->cdb[1] & 0x1f) == SCSI_SAI_READ_CAPACITY_16)
276 				return ReadCapacity16(request);
277 			break;
278 
279 		case SCSI_OP_SYNCHRONIZE_CACHE:
280 			// we ignore range and immediate bit, we always immediately
281 			// flush everything
282 			return SynchronizeCache(request);
283 
284 		// sadly, there are two possible read/write operation codes;
285 		// at least, the third one, read/write(12), is not valid for DAS
286 		case SCSI_OP_READ_6:
287 		case SCSI_OP_WRITE_6:
288 		{
289 			scsi_cmd_rw_6 *command = (scsi_cmd_rw_6 *)ccb->cdb;
290 			uint32 address = ((uint32)command->high_lba << 16)
291 				| ((uint32)command->mid_lba << 8) | (uint32)command->low_lba;
292 
293 			request->SetIsWrite(command->opcode == SCSI_OP_WRITE_6);
294 			return ExecuteReadWrite(request, address, command->length != 0
295 				? command->length : 256);
296 		}
297 
298 		case SCSI_OP_READ_10:
299 		case SCSI_OP_WRITE_10:
300 		{
301 			scsi_cmd_rw_10 *command = (scsi_cmd_rw_10 *)ccb->cdb;
302 			uint32 address = B_BENDIAN_TO_HOST_INT32(command->lba);
303 			uint32 sectorCount = B_BENDIAN_TO_HOST_INT16(command->length);
304 
305 			request->SetIsWrite(command->opcode == SCSI_OP_WRITE_10);
306 			if (sectorCount > 0)
307 				return ExecuteReadWrite(request, address, sectorCount);
308 			else {
309 				// we cannot transfer zero blocks (apart from LBA48)
310 				request->SetStatus(SCSI_REQ_CMP);
311 				return B_OK;
312 			}
313 		}
314 
315 		case SCSI_OP_READ_12:
316 		case SCSI_OP_WRITE_12:
317 		{
318 			scsi_cmd_rw_12 *command = (scsi_cmd_rw_12 *)ccb->cdb;
319 			uint32 address = B_BENDIAN_TO_HOST_INT32(command->lba);
320 			uint32 sectorCount = B_BENDIAN_TO_HOST_INT32(command->length);
321 
322 			request->SetIsWrite(command->opcode == SCSI_OP_WRITE_12);
323 			if (sectorCount > 0)
324 				return ExecuteReadWrite(request, address, sectorCount);
325 			else {
326 				// we cannot transfer zero blocks (apart from LBA48)
327 				request->SetStatus(SCSI_REQ_CMP);
328 				return B_OK;
329 			}
330 		}
331 
332 		case SCSI_OP_READ_16:
333 		case SCSI_OP_WRITE_16:
334 		{
335 			scsi_cmd_rw_16 *command = (scsi_cmd_rw_16 *)ccb->cdb;
336 			uint64 address = B_BENDIAN_TO_HOST_INT64(command->lba);
337 			uint32 sectorCount = B_BENDIAN_TO_HOST_INT32(command->length);
338 
339 			request->SetIsWrite(command->opcode == SCSI_OP_WRITE_16);
340 			if (sectorCount > 0)
341 				return ExecuteReadWrite(request, address, sectorCount);
342 			else {
343 				// we cannot transfer zero blocks (apart from LBA48)
344 				request->SetStatus(SCSI_REQ_CMP);
345 				return B_OK;
346 			}
347 		}
348 	}
349 
350 	TRACE("command not implemented\n");
351 	request->SetSense(SCSIS_KEY_ILLEGAL_REQUEST, SCSIS_ASC_INV_OPCODE);
352 	return B_ERROR;
353 }
354 
355 
356 void
357 ATADevice::GetRestrictions(bool *noAutoSense, uint32 *maxBlocks)
358 {
359 	if (IsATAPI())
360 		*noAutoSense = true;
361 	else {
362 		if (fUse48Bits)
363 			*maxBlocks = 0xffff;
364 		else
365 			*maxBlocks = 0x100;
366 	}
367 }
368 
369 
370 status_t
371 ATADevice::Select()
372 {
373 	status_t err = fChannel->SelectDevice(fIndex);
374 #if 1
375     // for debugging only
376 	if (fChannel->SelectedDevice() != fIndex) {
377 		TRACE_ERROR("device %d not selected!\n", fIndex);
378 		return B_ERROR;
379 	}
380 #endif
381 	return err;
382 }
383 
384 
385 status_t
386 ATADevice::SetFeature(int feature)
387 {
388 	TRACE("device_set_feature: feature %d\n", feature);
389 
390 	ATARequest request(false);
391 	request.SetDevice(this);
392 	request.SetTimeout(1 * 1000 * 1000);
393 
394 	fTaskFile.write.features = feature;
395 	fTaskFile.write.command = ATA_COMMAND_SET_FEATURES;
396 	fRegisterMask = ATA_MASK_FEATURES;
397 
398 	status_t result = fChannel->SendRequest(&request, ATA_DEVICE_READY_REQUIRED);
399 	if (result != B_OK) {
400 		TRACE_ERROR("sending set feature request failed\n");
401 		return result;
402 	}
403 
404 	result = fChannel->FinishRequest(&request,
405 		ATA_WAIT_FINISH | ATA_DEVICE_READY_REQUIRED, ATA_ERROR_ABORTED);
406 	if (result != B_OK) {
407 		TRACE_ERROR("set feature request failed\n");
408 		return result;
409 	}
410 
411 	return B_OK;
412 }
413 
414 
415 status_t
416 ATADevice::DisableCommandQueueing()
417 {
418 	if (!fInfoBlock.read_write_dma_queued_supported)
419 		return B_OK;
420 
421 	if (fInfoBlock.release_interrupt_supported) {
422 		status_t result = SetFeature(
423 			ATA_COMMAND_SET_FEATURES_DISABLE_RELEASE_INT);
424 		if (result != B_OK) {
425 			TRACE_ERROR("failed to disable release interrupt\n");
426 			return result;
427 		}
428 	}
429 
430 	if (fInfoBlock.service_interrupt_supported) {
431 		status_t result = SetFeature(
432 			ATA_COMMAND_SET_FEATURES_DISABLE_SERVICE_INT);
433 		if (result != B_OK) {
434 			TRACE_ERROR("failed to disable service interrupt\n");
435 			return result;
436 		}
437 	}
438 
439 	return B_OK;
440 }
441 
442 
443 status_t
444 ATADevice::ConfigureDMA()
445 {
446 	if (!fUseDMA)
447 		return B_OK;
448 
449 	if (!fInfoBlock.dma_supported) {
450 		TRACE_ALWAYS("DMA not supported by device\n");
451 		fUseDMA = false;
452 		return B_OK;
453 	}
454 
455 	#define CHECK_DMA_MODE(element, mode) \
456 		if (fInfoBlock.element) { \
457 			fDMAMode = mode; \
458 			modeCount++; \
459 		}
460 
461 	uint32 modeCount = 0;
462 
463 	CHECK_DMA_MODE(multiword_dma_0_selected, 0x00);
464 	CHECK_DMA_MODE(multiword_dma_1_selected, 0x01);
465 	CHECK_DMA_MODE(multiword_dma_2_selected, 0x02);
466 
467 	if (fInfoBlock.word_88_valid) {
468 		CHECK_DMA_MODE(ultra_dma_0_selected, 0x10);
469 		CHECK_DMA_MODE(ultra_dma_1_selected, 0x11);
470 		CHECK_DMA_MODE(ultra_dma_2_selected, 0x12);
471 		CHECK_DMA_MODE(ultra_dma_3_selected, 0x13);
472 		CHECK_DMA_MODE(ultra_dma_4_selected, 0x14);
473 		CHECK_DMA_MODE(ultra_dma_5_selected, 0x15);
474 		CHECK_DMA_MODE(ultra_dma_6_selected, 0x16);
475 	}
476 
477 	#undef CHECK_DMA_MODE
478 
479 	if (modeCount != 1) {
480 		TRACE_ERROR("more than on DMA mode selected, not using DMA\n");
481 		fUseDMA = false;
482 		return B_OK;
483 	}
484 
485 	TRACE_ALWAYS("using DMA mode 0x%02x\n", fDMAMode);
486 	return B_OK;
487 }
488 
489 
490 status_t
491 ATADevice::Configure()
492 {
493 	// warning: ata == 0 means "this is ata"...
494 	if (fInfoBlock.word_0.ata.ata_device != ATA_WORD_0_ATA_DEVICE) {
495 		// CF has either magic header or CFA bit set
496 		// we merge it to "CFA bit set" for easier (later) testing
497 		if (fInfoBlock.word_0.raw == ATA_WORD_0_CFA_MAGIC)
498 			fInfoBlock.compact_flash_assoc_supported = true;
499 		else {
500 			TRACE_ERROR("infoblock indicates non-ata device\n");
501 			return B_ERROR;
502 		}
503 	}
504 
505 	if (!fInfoBlock.lba_supported || (fInfoBlock.lba_sector_count == 0
506 		&& fInfoBlock.lba48_sector_count == 0)) {
507 		TRACE_ERROR("non-lba devices not supported\n");
508 		return B_ERROR;
509 	}
510 
511 	fTotalSectors = fInfoBlock.lba_sector_count;
512 
513 	if (fInfoBlock.word_106_bit_14_one && !fInfoBlock.word_106_bit_15_zero) {
514 		// contains a valid block size configuration
515 		if (fInfoBlock.logical_sector_not_512_bytes)
516 			fBlockSize = fInfoBlock.logical_sector_size * 2;
517 
518 		if (fInfoBlock.multiple_logical_per_physical_sectors) {
519 			fPhysicalBlockSize
520 				= fBlockSize << fInfoBlock.logical_sectors_per_physical_sector;
521 		} else
522 			fPhysicalBlockSize = fBlockSize;
523 	}
524 	if (fInfoBlock.word_209_bit_14_one && !fInfoBlock.word_209_bit_15_zero) {
525 		// contains a valid logical block offset configuration
526 		fBlockOffset = fInfoBlock.logical_sector_offset;
527 	}
528 
529 	fTaskFile.lba.mode = ATA_MODE_LBA;
530 	fTaskFile.lba.device = fIndex;
531 
532 	if (fInfoBlock.lba48_supported
533 		&& fInfoBlock.lba48_sector_count >= fInfoBlock.lba_sector_count) {
534 		fUse48Bits = true;
535 		fTotalSectors = fInfoBlock.lba48_sector_count;
536 	}
537 
538 	status_t result = ConfigureDMA();
539 	if (result != B_OK)
540 		return result;
541 
542 	result = DisableCommandQueueing();
543 	if (result != B_OK)
544 		return result;
545 
546 	return B_OK;
547 }
548 
549 
550 status_t
551 ATADevice::Identify()
552 {
553 	snprintf(fDebugContext, sizeof(fDebugContext), "%s %lu-%u",
554 		IsATAPI() ? "pi" : "", fChannel->ChannelID(), fIndex);
555 
556 	ATARequest request(false);
557 	request.SetDevice(this);
558 	request.SetTimeout(20 * 1000 * 1000);
559 
560 	fRegisterMask = 0;
561 	fTaskFile.write.command = IsATAPI() ? ATA_COMMAND_IDENTIFY_PACKET_DEVICE
562 		: ATA_COMMAND_IDENTIFY_DEVICE;
563 
564 	if (fChannel->SendRequest(&request,
565 			IsATAPI() ? 0 : ATA_DEVICE_READY_REQUIRED) != B_OK) {
566 		TRACE_ERROR("sending identify request failed\n");
567 		return B_ERROR;
568 	}
569 
570 	if (fChannel->Wait(ATA_STATUS_BUSY | ATA_STATUS_DATA_REQUEST, 0,
571 			ATA_WAIT_ANY_BIT, 100 * 1000) != B_OK) {
572 		TRACE_ALWAYS("no data request and not busy within 100ms, assuming "
573 			"no device present\n");
574 		return B_TIMED_OUT;
575 	}
576 
577 	if (fChannel->Wait(ATA_STATUS_DATA_REQUEST, ATA_STATUS_BUSY,
578 			ATA_CHECK_ERROR_BIT | ATA_CHECK_DEVICE_FAULT,
579 			IsATAPI() ? 20 * 1000 * 1000 : 500 * 1000) != B_OK) {
580 		TRACE_ERROR("timeout waiting for identify request\n");
581 		return B_TIMED_OUT;
582 	}
583 
584 	// get the infoblock
585 	fChannel->ReadPIO((uint8 *)&fInfoBlock, sizeof(fInfoBlock));
586 
587 	if (fChannel->WaitDataRequest(false) != B_OK) {
588 		TRACE_ERROR("device disagrees on info block length\n");
589 		return B_ERROR;
590 	}
591 
592 	if (fChannel->FinishRequest(&request,
593 			ATA_WAIT_FINISH | (IsATAPI() ? 0 : ATA_DEVICE_READY_REQUIRED),
594 			ATA_ERROR_ABORTED) != B_OK) {
595 		TRACE_ERROR("failed to finish identify request\n");
596 		return B_ERROR;
597 	}
598 
599 	if (1) {
600 		// print device information
601 		char modelNumber[sizeof(fInfoBlock.model_number) + 1];
602 		char serialNumber[sizeof(fInfoBlock.serial_number) + 1];
603 		char firmwareRev[sizeof(fInfoBlock.firmware_revision) + 1];
604 		strlcpy(modelNumber, fInfoBlock.model_number, sizeof(modelNumber));
605 		strlcpy(serialNumber, fInfoBlock.serial_number, sizeof(serialNumber));
606 		strlcpy(firmwareRev, fInfoBlock.firmware_revision, sizeof(firmwareRev));
607 		swap_words(modelNumber, sizeof(modelNumber) - 1);
608 		swap_words(serialNumber, sizeof(serialNumber) - 1);
609 		swap_words(firmwareRev, sizeof(firmwareRev) - 1);
610 		TRACE_ALWAYS("model number: %s\n", modelNumber);
611 		TRACE_ALWAYS("serial number: %s\n", serialNumber);
612   		TRACE_ALWAYS("firmware rev.: %s\n", firmwareRev);
613 	}
614 
615 	return B_OK;
616 }
617 
618 
619 status_t
620 ATADevice::ExecuteReadWrite(ATARequest *request, uint64 address,
621 	uint32 sectorCount)
622 {
623 	request->SetUseDMA(fUseDMA && fChannel->PrepareDMA(request) == B_OK);
624 	if (!request->UseDMA())
625 		request->PrepareSGInfo();
626 
627 	request->SetBytesLeft(sectorCount * fBlockSize);
628 	if (_FillTaskFile(request, address) != B_OK) {
629 		TRACE_ERROR("failed to setup transfer request\n");
630 		if (request->UseDMA())
631 			fChannel->FinishDMA();
632 		return B_ERROR;
633 	}
634 
635 	status_t result = fChannel->SendRequest(request,
636 		IsATAPI() ? 0 : ATA_DEVICE_READY_REQUIRED);
637 	if (result != B_OK) {
638 		TRACE_ERROR("failed to send transfer request\n");
639 		if (request->UseDMA())
640 			fChannel->FinishDMA();
641 		return result;
642 	}
643 
644 	if (request->UseDMA()) {
645 		fChannel->PrepareWaitingForInterrupt();
646 		fChannel->StartDMA();
647 
648 		result = fChannel->WaitForInterrupt(request->Timeout());
649 		status_t dmaResult = fChannel->FinishDMA();
650 		if (result == B_OK && dmaResult == B_OK) {
651 			fDMAFailures = 0;
652 			request->CCB()->data_resid = 0;
653 		} else {
654 			if (dmaResult != B_OK) {
655 				request->SetSense(SCSIS_KEY_HARDWARE_ERROR,
656 					SCSIS_ASC_LUN_COM_FAILURE);
657 				fDMAFailures++;
658 				if (fDMAFailures >= ATA_MAX_DMA_FAILURES) {
659 					TRACE_ALWAYS("disabling DMA after %u failures\n",
660 						fDMAFailures);
661 					fUseDMA = false;
662 				}
663 			} else {
664 				// timeout
665 				request->SetStatus(SCSI_CMD_TIMEOUT);
666 			}
667 		}
668 	} else {
669 		if (fChannel->Wait(ATA_STATUS_DATA_REQUEST, 0, ATA_CHECK_ERROR_BIT
670 				| ATA_CHECK_DEVICE_FAULT, request->Timeout()) != B_OK) {
671 			TRACE_ERROR("timeout waiting for device to request data\n");
672 			request->SetStatus(SCSI_CMD_TIMEOUT);
673 			return B_TIMED_OUT;
674 		}
675 
676 		if (fChannel->ExecutePIOTransfer(request) != B_OK) {
677 			TRACE_ERROR("executing pio transfer failed\n");
678 			request->SetStatus(SCSI_SEQUENCE_FAIL);
679 		}
680 	}
681 
682 	return fChannel->FinishRequest(request, ATA_WAIT_FINISH
683 		| ATA_DEVICE_READY_REQUIRED, ATA_ERROR_ALL);
684 }
685 
686 
687 status_t
688 ATADevice::_FillTaskFile(ATARequest *request, uint64 address)
689 {
690 	// list of LBA48 opcodes
691 	static const uint8 s48BitCommands[2][2] = {
692 		{ ATA_COMMAND_READ_SECTORS_EXT, ATA_COMMAND_WRITE_SECTORS_EXT },
693 		{ ATA_COMMAND_READ_DMA_EXT, ATA_COMMAND_WRITE_DMA_EXT }
694 	};
695 
696 	// list of normal LBA opcodes
697 	static const uint8 s28BitCommands[2][2] = {
698 		{ ATA_COMMAND_READ_SECTORS, ATA_COMMAND_WRITE_SECTORS },
699 		{ ATA_COMMAND_READ_DMA, ATA_COMMAND_WRITE_DMA }
700 	};
701 
702 	uint32 sectorCount = *request->BytesLeft() / fBlockSize;
703 	TRACE("about to transfer %lu sectors\n", sectorCount);
704 
705 	if (fUse48Bits
706 		&& (address + sectorCount > 0xfffffff || sectorCount > 0x100)) {
707 		// use LBA48 only if necessary
708 		if (sectorCount > 0xffff) {
709 			TRACE_ERROR("invalid sector count %lu\n", sectorCount);
710 			request->SetSense(SCSIS_KEY_ILLEGAL_REQUEST,
711 				SCSIS_ASC_INV_CDB_FIELD);
712 			return B_ERROR;
713 		}
714 
715 		fRegisterMask = ATA_MASK_SECTOR_COUNT_48
716 			| ATA_MASK_LBA_LOW_48
717 			| ATA_MASK_LBA_MID_48
718 			| ATA_MASK_LBA_HIGH_48;
719 
720 		fTaskFile.lba48.sector_count_0_7 = sectorCount & 0xff;
721 		fTaskFile.lba48.sector_count_8_15 = (sectorCount >> 8) & 0xff;
722 		fTaskFile.lba48.lba_0_7 = address & 0xff;
723 		fTaskFile.lba48.lba_8_15 = (address >> 8) & 0xff;
724 		fTaskFile.lba48.lba_16_23 = (address >> 16) & 0xff;
725 		fTaskFile.lba48.lba_24_31 = (address >> 24) & 0xff;
726 		fTaskFile.lba48.lba_32_39 = (address >> 32) & 0xff;
727 		fTaskFile.lba48.lba_40_47 = (address >> 40) & 0xff;
728 		fTaskFile.lba48.command = s48BitCommands[request->UseDMA()
729 			? 1 : 0][request->IsWrite() ? 1 : 0];
730 	} else {
731 		// normal LBA
732 		if (sectorCount > 0x100) {
733 			TRACE_ERROR("invalid sector count %lu\n", sectorCount);
734 			request->SetSense(SCSIS_KEY_ILLEGAL_REQUEST,
735 				SCSIS_ASC_INV_CDB_FIELD);
736 			return B_ERROR;
737 		}
738 
739 		fRegisterMask = ATA_MASK_SECTOR_COUNT
740 			| ATA_MASK_LBA_LOW
741 			| ATA_MASK_LBA_MID
742 			| ATA_MASK_LBA_HIGH
743 			| ATA_MASK_DEVICE_HEAD;
744 
745 		fTaskFile.lba.sector_count = sectorCount & 0xff;
746 		fTaskFile.lba.lba_0_7 = address & 0xff;
747 		fTaskFile.lba.lba_8_15 = (address >> 8) & 0xff;
748 		fTaskFile.lba.lba_16_23 = (address >> 16) & 0xff;
749 		fTaskFile.lba.lba_24_27 = (address >> 24) & 0xf;
750 		fTaskFile.lba.command = s28BitCommands[request->UseDMA()
751 			? 1 : 0][request->IsWrite() ? 1 : 0];
752 	}
753 
754 	return B_OK;
755 }
756