2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
28 #include <ipxe/list.h>
29 #include <ipxe/interface.h>
30 #include <ipxe/blockdev.h>
40 /******************************************************************************
44 ******************************************************************************
50 * @v control ATA control interface
51 * @v data ATA data interface
52 * @v command ATA command
53 * @ret tag Command tag, or negative error
55 int ata_command ( struct interface *control, struct interface *data,
56 struct ata_cmd *command ) {
57 struct interface *dest;
58 ata_command_TYPE ( void * ) *op =
59 intf_get_dest_op ( control, ata_command, &dest );
60 void *object = intf_object ( dest );
64 tag = op ( object, data, command );
66 /* Default is to fail to issue the command */
74 /******************************************************************************
76 * ATA devices and commands
78 ******************************************************************************
81 /** List of all ATA commands */
82 static LIST_HEAD ( ata_commands );
86 /** Reference count */
88 /** Block control interface */
89 struct interface block;
90 /** ATA control interface */
95 * Must be ATA_DEV_MASTER or ATA_DEV_SLAVE.
98 /** Maximum number of blocks per single transfer */
99 unsigned int max_count;
100 /** Device uses LBA48 extended addressing */
104 /** An ATA command */
106 /** Reference count */
107 struct refcnt refcnt;
109 struct ata_device *atadev;
110 /** List of ATA commands */
111 struct list_head list;
113 /** Block data interface */
114 struct interface block;
115 /** ATA data interface */
116 struct interface ata;
119 struct ata_command_type *type;
127 /** An ATA command type */
128 struct ata_command_type {
131 /** Additional working space */
133 /** Command for non-LBA48-capable devices */
135 /** Command for LBA48-capable devices */
138 * Calculate data-in buffer
140 * @v atacmd ATA command
141 * @v buffer Available buffer
142 * @v len Available buffer length
143 * @ret data_in Data-in buffer
144 * @ret data_in_len Data-in buffer length
146 void ( * data_in ) ( struct ata_command *atacmd, userptr_t buffer,
147 size_t len, userptr_t *data_in,
148 size_t *data_in_len );
150 * Calculate data-out buffer
153 * @v atacmd ATA command
154 * @v buffer Available buffer
155 * @v len Available buffer length
156 * @ret data_out Data-out buffer
157 * @ret data_out_len Data-out buffer length
159 void ( * data_out ) ( struct ata_command *atacmd, userptr_t buffer,
160 size_t len, userptr_t *data_out,
161 size_t *data_out_len );
163 * Handle ATA command completion
165 * @v atacmd ATA command
166 * @v rc Reason for completion
168 void ( * done ) ( struct ata_command *atacmd, int rc );
172 * Get reference to ATA device
174 * @v atadev ATA device
175 * @ret atadev ATA device
177 static inline __attribute__ (( always_inline )) struct ata_device *
178 atadev_get ( struct ata_device *atadev ) {
179 ref_get ( &atadev->refcnt );
184 * Drop reference to ATA device
186 * @v atadev ATA device
188 static inline __attribute__ (( always_inline )) void
189 atadev_put ( struct ata_device *atadev ) {
190 ref_put ( &atadev->refcnt );
194 * Get reference to ATA command
196 * @v atacmd ATA command
197 * @ret atacmd ATA command
199 static inline __attribute__ (( always_inline )) struct ata_command *
200 atacmd_get ( struct ata_command *atacmd ) {
201 ref_get ( &atacmd->refcnt );
206 * Drop reference to ATA command
208 * @v atacmd ATA command
210 static inline __attribute__ (( always_inline )) void
211 atacmd_put ( struct ata_command *atacmd ) {
212 ref_put ( &atacmd->refcnt );
216 * Get ATA command private data
218 * @v atacmd ATA command
219 * @ret priv Private data
221 static inline __attribute__ (( always_inline )) void *
222 atacmd_priv ( struct ata_command *atacmd ) {
229 * @v refcnt Reference count
231 static void atacmd_free ( struct refcnt *refcnt ) {
232 struct ata_command *atacmd =
233 container_of ( refcnt, struct ata_command, refcnt );
235 /* Remove from list of commands */
236 list_del ( &atacmd->list );
237 atadev_put ( atacmd->atadev );
246 * @v atacmd ATA command
247 * @v rc Reason for close
249 static void atacmd_close ( struct ata_command *atacmd, int rc ) {
250 struct ata_device *atadev = atacmd->atadev;
253 DBGC ( atadev, "ATA %p tag %08x closed: %s\n",
254 atadev, atacmd->tag, strerror ( rc ) );
257 /* Shut down interfaces */
258 intf_shutdown ( &atacmd->ata, rc );
259 intf_shutdown ( &atacmd->block, rc );
263 * Handle ATA command completion
265 * @v atacmd ATA command
266 * @v rc Reason for close
268 static void atacmd_done ( struct ata_command *atacmd, int rc ) {
270 /* Hand over to the command completion handler */
271 atacmd->type->done ( atacmd, rc );
275 * Use provided data buffer for ATA command
277 * @v atacmd ATA command
278 * @v buffer Available buffer
279 * @v len Available buffer length
280 * @ret data Data buffer
281 * @ret data_len Data buffer length
283 static void atacmd_data_buffer ( struct ata_command *atacmd __unused,
284 userptr_t buffer, size_t len,
285 userptr_t *data, size_t *data_len ) {
291 * Use no data buffer for ATA command
293 * @v atacmd ATA command
294 * @v buffer Available buffer
295 * @v len Available buffer length
296 * @ret data Data buffer
297 * @ret data_len Data buffer length
299 static void atacmd_data_none ( struct ata_command *atacmd __unused,
300 userptr_t buffer __unused, size_t len __unused,
301 userptr_t *data __unused,
302 size_t *data_len __unused ) {
307 * Use private data buffer for ATA command
309 * @v atacmd ATA command
310 * @v buffer Available buffer
311 * @v len Available buffer length
312 * @ret data Data buffer
313 * @ret data_len Data buffer length
315 static void atacmd_data_priv ( struct ata_command *atacmd,
316 userptr_t buffer __unused, size_t len __unused,
317 userptr_t *data, size_t *data_len ) {
318 *data = virt_to_user ( atacmd_priv ( atacmd ) );
319 *data_len = atacmd->type->priv_len;
322 /** ATA READ command type */
323 static struct ata_command_type atacmd_read = {
325 .cmd_lba = ATA_CMD_READ,
326 .cmd_lba48 = ATA_CMD_READ_EXT,
327 .data_in = atacmd_data_buffer,
328 .data_out = atacmd_data_none,
329 .done = atacmd_close,
332 /** ATA WRITE command type */
333 static struct ata_command_type atacmd_write = {
335 .cmd_lba = ATA_CMD_WRITE,
336 .cmd_lba48 = ATA_CMD_WRITE_EXT,
337 .data_in = atacmd_data_none,
338 .data_out = atacmd_data_buffer,
339 .done = atacmd_close,
342 /** ATA IDENTIFY private data */
343 struct ata_identify_private {
345 struct ata_identity identity;
349 * Return ATA model string (for debugging)
351 * @v identify ATA identity data
352 * @ret model Model string
354 static const char * ata_model ( struct ata_identity *identity ) {
356 uint16_t words[ sizeof ( identity->model ) / 2 ];
357 char text[ sizeof ( identity->model ) + 1 /* NUL */ ];
361 for ( i = 0 ; i < ( sizeof ( identity->model ) / 2 ) ; i++ )
362 buf.words[i] = bswap_16 ( identity->model[i] );
368 * Handle ATA IDENTIFY command completion
370 * @v atacmd ATA command
371 * @v rc Reason for completion
373 static void atacmd_identify_done ( struct ata_command *atacmd, int rc ) {
374 struct ata_device *atadev = atacmd->atadev;
375 struct ata_identify_private *priv = atacmd_priv ( atacmd );
376 struct ata_identity *identity = &priv->identity;
377 struct block_device_capacity capacity;
379 /* Close if command failed */
381 atacmd_close ( atacmd, rc );
385 /* Extract capacity */
386 if ( identity->supports_lba48 & cpu_to_le16 ( ATA_SUPPORTS_LBA48 ) ) {
388 capacity.blocks = le64_to_cpu ( identity->lba48_sectors );
390 capacity.blocks = le32_to_cpu ( identity->lba_sectors );
392 capacity.blksize = ATA_SECTOR_SIZE;
393 capacity.max_count = atadev->max_count;
394 DBGC ( atadev, "ATA %p is a %s\n", atadev, ata_model ( identity ) );
395 DBGC ( atadev, "ATA %p has %#llx blocks (%ld MB) and uses %s\n",
396 atadev, capacity.blocks,
397 ( ( signed long ) ( capacity.blocks >> 11 ) ),
398 ( atadev->lba48 ? "LBA48" : "LBA" ) );
400 /* Return capacity to caller */
401 block_capacity ( &atacmd->block, &capacity );
404 atacmd_close ( atacmd, 0 );
407 /** ATA IDENTITY command type */
408 static struct ata_command_type atacmd_identify = {
410 .priv_len = sizeof ( struct ata_identify_private ),
411 .cmd_lba = ATA_CMD_IDENTIFY,
412 .cmd_lba48 = ATA_CMD_IDENTIFY,
413 .data_in = atacmd_data_priv,
414 .data_out = atacmd_data_none,
415 .done = atacmd_identify_done,
418 /** ATA command block interface operations */
419 static struct interface_operation atacmd_block_op[] = {
420 INTF_OP ( intf_close, struct ata_command *, atacmd_close ),
423 /** ATA command block interface descriptor */
424 static struct interface_descriptor atacmd_block_desc =
425 INTF_DESC_PASSTHRU ( struct ata_command, block,
426 atacmd_block_op, ata );
428 /** ATA command ATA interface operations */
429 static struct interface_operation atacmd_ata_op[] = {
430 INTF_OP ( intf_close, struct ata_command *, atacmd_done ),
433 /** ATA command ATA interface descriptor */
434 static struct interface_descriptor atacmd_ata_desc =
435 INTF_DESC_PASSTHRU ( struct ata_command, ata,
436 atacmd_ata_op, block );
441 * @v atadev ATA device
442 * @v block Block data interface
443 * @v type ATA command type
444 * @v lba Starting logical block address
445 * @v count Number of blocks to transfer
446 * @v buffer Data buffer
447 * @v len Length of data buffer
448 * @ret rc Return status code
450 static int atadev_command ( struct ata_device *atadev,
451 struct interface *block,
452 struct ata_command_type *type,
453 uint64_t lba, unsigned int count,
454 userptr_t buffer, size_t len ) {
455 struct ata_command *atacmd;
456 struct ata_cmd command;
460 /* Allocate and initialise structure */
461 atacmd = zalloc ( sizeof ( *atacmd ) + type->priv_len );
466 ref_init ( &atacmd->refcnt, atacmd_free );
467 intf_init ( &atacmd->block, &atacmd_block_desc, &atacmd->refcnt );
468 intf_init ( &atacmd->ata, &atacmd_ata_desc,
470 atacmd->atadev = atadev_get ( atadev );
471 list_add ( &atacmd->list, &ata_commands );
475 if ( len != ( count * ATA_SECTOR_SIZE ) ) {
476 DBGC ( atadev, "ATA %p tag %08x buffer length mismatch (count "
477 "%d len %zd)\n", atadev, atacmd->tag, count, len );
482 /* Construct command */
483 memset ( &command, 0, sizeof ( command ) );
484 command.cb.lba.native = lba;
485 command.cb.count.native = count;
486 command.cb.device = ( atadev->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
487 command.cb.lba48 = atadev->lba48;
488 if ( ! atadev->lba48 )
489 command.cb.device |= command.cb.lba.bytes.low_prev;
490 command.cb.cmd_stat =
491 ( atadev->lba48 ? type->cmd_lba48 : type->cmd_lba );
492 type->data_in ( atacmd, buffer, len,
493 &command.data_in, &command.data_in_len );
494 type->data_out ( atacmd, buffer, len,
495 &command.data_out, &command.data_out_len );
498 if ( ( tag = ata_command ( &atadev->ata, &atacmd->ata,
501 DBGC ( atadev, "ATA %p tag %08x could not issue command: %s\n",
502 atadev, atacmd->tag, strerror ( rc ) );
507 DBGC2 ( atadev, "ATA %p tag %08x %s cmd %02x dev %02x LBA%s %08llx "
508 "count %04x\n", atadev, atacmd->tag, atacmd->type->name,
509 command.cb.cmd_stat, command.cb.device,
510 ( command.cb.lba48 ? "48" : "" ),
511 ( unsigned long long ) command.cb.lba.native,
512 command.cb.count.native );
514 /* Attach to parent interface, mortalise self, and return */
515 intf_plug_plug ( &atacmd->block, block );
516 ref_put ( &atacmd->refcnt );
521 atacmd_close ( atacmd, rc );
522 ref_put ( &atacmd->refcnt );
528 * Issue ATA block read
530 * @v atadev ATA device
531 * @v block Block data interface
532 * @v lba Starting logical block address
533 * @v count Number of blocks to transfer
534 * @v buffer Data buffer
535 * @v len Length of data buffer
536 * @ret rc Return status code
539 static int atadev_read ( struct ata_device *atadev,
540 struct interface *block,
541 uint64_t lba, unsigned int count,
542 userptr_t buffer, size_t len ) {
543 return atadev_command ( atadev, block, &atacmd_read,
544 lba, count, buffer, len );
548 * Issue ATA block write
550 * @v atadev ATA device
551 * @v block Block data interface
552 * @v lba Starting logical block address
553 * @v count Number of blocks to transfer
554 * @v buffer Data buffer
555 * @v len Length of data buffer
556 * @ret rc Return status code
558 static int atadev_write ( struct ata_device *atadev,
559 struct interface *block,
560 uint64_t lba, unsigned int count,
561 userptr_t buffer, size_t len ) {
562 return atadev_command ( atadev, block, &atacmd_write,
563 lba, count, buffer, len );
567 * Read ATA device capacity
569 * @v atadev ATA device
570 * @v block Block data interface
571 * @ret rc Return status code
573 static int atadev_read_capacity ( struct ata_device *atadev,
574 struct interface *block ) {
575 struct ata_identity *identity;
577 assert ( atacmd_identify.priv_len == sizeof ( *identity ) );
578 assert ( atacmd_identify.priv_len == ATA_SECTOR_SIZE );
579 return atadev_command ( atadev, block, &atacmd_identify,
580 0, 1, UNULL, ATA_SECTOR_SIZE );
586 * @v atadev ATA device
587 * @v rc Reason for close
589 static void atadev_close ( struct ata_device *atadev, int rc ) {
590 struct ata_command *atacmd;
591 struct ata_command *tmp;
593 /* Shut down interfaces */
594 intf_shutdown ( &atadev->block, rc );
595 intf_shutdown ( &atadev->ata, rc );
597 /* Shut down any remaining commands */
598 list_for_each_entry_safe ( atacmd, tmp, &ata_commands, list ) {
599 if ( atacmd->atadev != atadev )
601 atacmd_get ( atacmd );
602 atacmd_close ( atacmd, rc );
603 atacmd_put ( atacmd );
608 * Describe ATA device using EDD
610 * @v atadev ATA device
611 * @v type EDD interface type
612 * @v path EDD device path
613 * @ret rc Return status code
615 static int atadev_edd_describe ( struct ata_device *atadev,
616 struct edd_interface_type *type,
617 union edd_device_path *path ) {
619 type->type = cpu_to_le64 ( EDD_INTF_TYPE_ATA );
620 path->ata.slave = ( ( atadev->device == ATA_DEV_SLAVE ) ? 0x01 : 0x00 );
624 /** ATA device block interface operations */
625 static struct interface_operation atadev_block_op[] = {
626 INTF_OP ( block_read, struct ata_device *, atadev_read ),
627 INTF_OP ( block_write, struct ata_device *, atadev_write ),
628 INTF_OP ( block_read_capacity, struct ata_device *,
629 atadev_read_capacity ),
630 INTF_OP ( intf_close, struct ata_device *, atadev_close ),
631 INTF_OP ( edd_describe, struct ata_device *, atadev_edd_describe ),
634 /** ATA device block interface descriptor */
635 static struct interface_descriptor atadev_block_desc =
636 INTF_DESC_PASSTHRU ( struct ata_device, block,
637 atadev_block_op, ata );
639 /** ATA device ATA interface operations */
640 static struct interface_operation atadev_ata_op[] = {
641 INTF_OP ( intf_close, struct ata_device *, atadev_close ),
644 /** ATA device ATA interface descriptor */
645 static struct interface_descriptor atadev_ata_desc =
646 INTF_DESC_PASSTHRU ( struct ata_device, ata,
647 atadev_ata_op, block );
652 * @v block Block control interface
653 * @v ata ATA control interface
654 * @v device ATA device number
655 * @v max_count Maximum number of blocks per single transfer
656 * @ret rc Return status code
658 int ata_open ( struct interface *block, struct interface *ata,
659 unsigned int device, unsigned int max_count ) {
660 struct ata_device *atadev;
662 /* Allocate and initialise structure */
663 atadev = zalloc ( sizeof ( *atadev ) );
666 ref_init ( &atadev->refcnt, NULL );
667 intf_init ( &atadev->block, &atadev_block_desc, &atadev->refcnt );
668 intf_init ( &atadev->ata, &atadev_ata_desc, &atadev->refcnt );
669 atadev->device = device;
670 atadev->max_count = max_count;
672 /* Attach to ATA and parent and interfaces, mortalise self,
675 intf_plug_plug ( &atadev->ata, ata );
676 intf_plug_plug ( &atadev->block, block );
677 ref_put ( &atadev->refcnt );