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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
32 #include <ipxe/list.h>
33 #include <ipxe/interface.h>
34 #include <ipxe/blockdev.h>
44 /******************************************************************************
48 ******************************************************************************
54 * @v control ATA control interface
55 * @v data ATA data interface
56 * @v command ATA command
57 * @ret tag Command tag, or negative error
59 int ata_command ( struct interface
*control
, struct interface
*data
,
60 struct ata_cmd
*command
) {
61 struct interface
*dest
;
62 ata_command_TYPE ( void * ) *op
=
63 intf_get_dest_op ( control
, ata_command
, &dest
);
64 void *object
= intf_object ( dest
);
68 tag
= op ( object
, data
, command
);
70 /* Default is to fail to issue the command */
78 /******************************************************************************
80 * ATA devices and commands
82 ******************************************************************************
85 /** List of all ATA commands */
86 static LIST_HEAD ( ata_commands
);
90 /** Reference count */
92 /** Block control interface */
93 struct interface block
;
94 /** ATA control interface */
99 * Must be ATA_DEV_MASTER or ATA_DEV_SLAVE.
102 /** Maximum number of blocks per single transfer */
103 unsigned int max_count
;
104 /** Device uses LBA48 extended addressing */
108 /** An ATA command */
110 /** Reference count */
111 struct refcnt refcnt
;
113 struct ata_device
*atadev
;
114 /** List of ATA commands */
115 struct list_head list
;
117 /** Block data interface */
118 struct interface block
;
119 /** ATA data interface */
120 struct interface ata
;
123 struct ata_command_type
*type
;
131 /** An ATA command type */
132 struct ata_command_type
{
135 /** Additional working space */
137 /** Command for non-LBA48-capable devices */
139 /** Command for LBA48-capable devices */
142 * Calculate data-in buffer
144 * @v atacmd ATA command
145 * @v buffer Available buffer
146 * @v len Available buffer length
147 * @ret data_in Data-in buffer
148 * @ret data_in_len Data-in buffer length
150 void ( * data_in
) ( struct ata_command
*atacmd
, userptr_t buffer
,
151 size_t len
, userptr_t
*data_in
,
152 size_t *data_in_len
);
154 * Calculate data-out buffer
157 * @v atacmd ATA command
158 * @v buffer Available buffer
159 * @v len Available buffer length
160 * @ret data_out Data-out buffer
161 * @ret data_out_len Data-out buffer length
163 void ( * data_out
) ( struct ata_command
*atacmd
, userptr_t buffer
,
164 size_t len
, userptr_t
*data_out
,
165 size_t *data_out_len
);
167 * Handle ATA command completion
169 * @v atacmd ATA command
170 * @v rc Reason for completion
172 void ( * done
) ( struct ata_command
*atacmd
, int rc
);
176 * Get reference to ATA device
178 * @v atadev ATA device
179 * @ret atadev ATA device
181 static inline __attribute__ (( always_inline
)) struct ata_device
*
182 atadev_get ( struct ata_device
*atadev
) {
183 ref_get ( &atadev
->refcnt
);
188 * Drop reference to ATA device
190 * @v atadev ATA device
192 static inline __attribute__ (( always_inline
)) void
193 atadev_put ( struct ata_device
*atadev
) {
194 ref_put ( &atadev
->refcnt
);
198 * Get reference to ATA command
200 * @v atacmd ATA command
201 * @ret atacmd ATA command
203 static inline __attribute__ (( always_inline
)) struct ata_command
*
204 atacmd_get ( struct ata_command
*atacmd
) {
205 ref_get ( &atacmd
->refcnt
);
210 * Drop reference to ATA command
212 * @v atacmd ATA command
214 static inline __attribute__ (( always_inline
)) void
215 atacmd_put ( struct ata_command
*atacmd
) {
216 ref_put ( &atacmd
->refcnt
);
220 * Get ATA command private data
222 * @v atacmd ATA command
223 * @ret priv Private data
225 static inline __attribute__ (( always_inline
)) void *
226 atacmd_priv ( struct ata_command
*atacmd
) {
233 * @v refcnt Reference count
235 static void atacmd_free ( struct refcnt
*refcnt
) {
236 struct ata_command
*atacmd
=
237 container_of ( refcnt
, struct ata_command
, refcnt
);
239 /* Remove from list of commands */
240 list_del ( &atacmd
->list
);
241 atadev_put ( atacmd
->atadev
);
250 * @v atacmd ATA command
251 * @v rc Reason for close
253 static void atacmd_close ( struct ata_command
*atacmd
, int rc
) {
254 struct ata_device
*atadev
= atacmd
->atadev
;
257 DBGC ( atadev
, "ATA %p tag %08x closed: %s\n",
258 atadev
, atacmd
->tag
, strerror ( rc
) );
261 /* Shut down interfaces */
262 intf_shutdown ( &atacmd
->ata
, rc
);
263 intf_shutdown ( &atacmd
->block
, rc
);
267 * Handle ATA command completion
269 * @v atacmd ATA command
270 * @v rc Reason for close
272 static void atacmd_done ( struct ata_command
*atacmd
, int rc
) {
274 /* Hand over to the command completion handler */
275 atacmd
->type
->done ( atacmd
, rc
);
279 * Use provided data buffer for ATA command
281 * @v atacmd ATA command
282 * @v buffer Available buffer
283 * @v len Available buffer length
284 * @ret data Data buffer
285 * @ret data_len Data buffer length
287 static void atacmd_data_buffer ( struct ata_command
*atacmd __unused
,
288 userptr_t buffer
, size_t len
,
289 userptr_t
*data
, size_t *data_len
) {
295 * Use no data buffer for ATA command
297 * @v atacmd ATA command
298 * @v buffer Available buffer
299 * @v len Available buffer length
300 * @ret data Data buffer
301 * @ret data_len Data buffer length
303 static void atacmd_data_none ( struct ata_command
*atacmd __unused
,
304 userptr_t buffer __unused
, size_t len __unused
,
305 userptr_t
*data __unused
,
306 size_t *data_len __unused
) {
311 * Use private data buffer for ATA command
313 * @v atacmd ATA command
314 * @v buffer Available buffer
315 * @v len Available buffer length
316 * @ret data Data buffer
317 * @ret data_len Data buffer length
319 static void atacmd_data_priv ( struct ata_command
*atacmd
,
320 userptr_t buffer __unused
, size_t len __unused
,
321 userptr_t
*data
, size_t *data_len
) {
322 *data
= virt_to_user ( atacmd_priv ( atacmd
) );
323 *data_len
= atacmd
->type
->priv_len
;
326 /** ATA READ command type */
327 static struct ata_command_type atacmd_read
= {
329 .cmd_lba
= ATA_CMD_READ
,
330 .cmd_lba48
= ATA_CMD_READ_EXT
,
331 .data_in
= atacmd_data_buffer
,
332 .data_out
= atacmd_data_none
,
333 .done
= atacmd_close
,
336 /** ATA WRITE command type */
337 static struct ata_command_type atacmd_write
= {
339 .cmd_lba
= ATA_CMD_WRITE
,
340 .cmd_lba48
= ATA_CMD_WRITE_EXT
,
341 .data_in
= atacmd_data_none
,
342 .data_out
= atacmd_data_buffer
,
343 .done
= atacmd_close
,
346 /** ATA IDENTIFY private data */
347 struct ata_identify_private
{
349 struct ata_identity identity
;
353 * Return ATA model string (for debugging)
355 * @v identify ATA identity data
356 * @ret model Model string
358 static const char * ata_model ( struct ata_identity
*identity
) {
360 uint16_t words
[ sizeof ( identity
->model
) / 2 ];
361 char text
[ sizeof ( identity
->model
) + 1 /* NUL */ ];
365 for ( i
= 0 ; i
< ( sizeof ( identity
->model
) / 2 ) ; i
++ )
366 buf
.words
[i
] = bswap_16 ( identity
->model
[i
] );
372 * Handle ATA IDENTIFY command completion
374 * @v atacmd ATA command
375 * @v rc Reason for completion
377 static void atacmd_identify_done ( struct ata_command
*atacmd
, int rc
) {
378 struct ata_device
*atadev
= atacmd
->atadev
;
379 struct ata_identify_private
*priv
= atacmd_priv ( atacmd
);
380 struct ata_identity
*identity
= &priv
->identity
;
381 struct block_device_capacity capacity
;
383 /* Close if command failed */
385 atacmd_close ( atacmd
, rc
);
389 /* Extract capacity */
390 if ( identity
->supports_lba48
& cpu_to_le16 ( ATA_SUPPORTS_LBA48
) ) {
392 capacity
.blocks
= le64_to_cpu ( identity
->lba48_sectors
);
394 capacity
.blocks
= le32_to_cpu ( identity
->lba_sectors
);
396 capacity
.blksize
= ATA_SECTOR_SIZE
;
397 capacity
.max_count
= atadev
->max_count
;
398 DBGC ( atadev
, "ATA %p is a %s\n", atadev
, ata_model ( identity
) );
399 DBGC ( atadev
, "ATA %p has %#llx blocks (%ld MB) and uses %s\n",
400 atadev
, capacity
.blocks
,
401 ( ( signed long ) ( capacity
.blocks
>> 11 ) ),
402 ( atadev
->lba48 ?
"LBA48" : "LBA" ) );
404 /* Return capacity to caller */
405 block_capacity ( &atacmd
->block
, &capacity
);
408 atacmd_close ( atacmd
, 0 );
411 /** ATA IDENTITY command type */
412 static struct ata_command_type atacmd_identify
= {
414 .priv_len
= sizeof ( struct ata_identify_private
),
415 .cmd_lba
= ATA_CMD_IDENTIFY
,
416 .cmd_lba48
= ATA_CMD_IDENTIFY
,
417 .data_in
= atacmd_data_priv
,
418 .data_out
= atacmd_data_none
,
419 .done
= atacmd_identify_done
,
422 /** ATA command block interface operations */
423 static struct interface_operation atacmd_block_op
[] = {
424 INTF_OP ( intf_close
, struct ata_command
*, atacmd_close
),
427 /** ATA command block interface descriptor */
428 static struct interface_descriptor atacmd_block_desc
=
429 INTF_DESC_PASSTHRU ( struct ata_command
, block
,
430 atacmd_block_op
, ata
);
432 /** ATA command ATA interface operations */
433 static struct interface_operation atacmd_ata_op
[] = {
434 INTF_OP ( intf_close
, struct ata_command
*, atacmd_done
),
437 /** ATA command ATA interface descriptor */
438 static struct interface_descriptor atacmd_ata_desc
=
439 INTF_DESC_PASSTHRU ( struct ata_command
, ata
,
440 atacmd_ata_op
, block
);
445 * @v atadev ATA device
446 * @v block Block data interface
447 * @v type ATA command type
448 * @v lba Starting logical block address
449 * @v count Number of blocks to transfer
450 * @v buffer Data buffer
451 * @v len Length of data buffer
452 * @ret rc Return status code
454 static int atadev_command ( struct ata_device
*atadev
,
455 struct interface
*block
,
456 struct ata_command_type
*type
,
457 uint64_t lba
, unsigned int count
,
458 userptr_t buffer
, size_t len
) {
459 struct ata_command
*atacmd
;
460 struct ata_cmd command
;
464 /* Allocate and initialise structure */
465 atacmd
= zalloc ( sizeof ( *atacmd
) + type
->priv_len
);
470 ref_init ( &atacmd
->refcnt
, atacmd_free
);
471 intf_init ( &atacmd
->block
, &atacmd_block_desc
, &atacmd
->refcnt
);
472 intf_init ( &atacmd
->ata
, &atacmd_ata_desc
,
474 atacmd
->atadev
= atadev_get ( atadev
);
475 list_add ( &atacmd
->list
, &ata_commands
);
479 if ( len
!= ( count
* ATA_SECTOR_SIZE
) ) {
480 DBGC ( atadev
, "ATA %p tag %08x buffer length mismatch (count "
481 "%d len %zd)\n", atadev
, atacmd
->tag
, count
, len
);
486 /* Construct command */
487 memset ( &command
, 0, sizeof ( command
) );
488 command
.cb
.lba
.native
= lba
;
489 command
.cb
.count
.native
= count
;
490 command
.cb
.device
= ( atadev
->device
| ATA_DEV_OBSOLETE
| ATA_DEV_LBA
);
491 command
.cb
.lba48
= atadev
->lba48
;
492 if ( ! atadev
->lba48
)
493 command
.cb
.device
|= command
.cb
.lba
.bytes
.low_prev
;
494 command
.cb
.cmd_stat
=
495 ( atadev
->lba48 ? type
->cmd_lba48
: type
->cmd_lba
);
496 type
->data_in ( atacmd
, buffer
, len
,
497 &command
.data_in
, &command
.data_in_len
);
498 type
->data_out ( atacmd
, buffer
, len
,
499 &command
.data_out
, &command
.data_out_len
);
502 if ( ( tag
= ata_command ( &atadev
->ata
, &atacmd
->ata
,
505 DBGC ( atadev
, "ATA %p tag %08x could not issue command: %s\n",
506 atadev
, atacmd
->tag
, strerror ( rc
) );
511 DBGC2 ( atadev
, "ATA %p tag %08x %s cmd %02x dev %02x LBA%s %08llx "
512 "count %04x\n", atadev
, atacmd
->tag
, atacmd
->type
->name
,
513 command
.cb
.cmd_stat
, command
.cb
.device
,
514 ( command
.cb
.lba48 ?
"48" : "" ),
515 ( unsigned long long ) command
.cb
.lba
.native
,
516 command
.cb
.count
.native
);
518 /* Attach to parent interface, mortalise self, and return */
519 intf_plug_plug ( &atacmd
->block
, block
);
520 ref_put ( &atacmd
->refcnt
);
525 atacmd_close ( atacmd
, rc
);
526 ref_put ( &atacmd
->refcnt
);
532 * Issue ATA block read
534 * @v atadev ATA device
535 * @v block Block data interface
536 * @v lba Starting logical block address
537 * @v count Number of blocks to transfer
538 * @v buffer Data buffer
539 * @v len Length of data buffer
540 * @ret rc Return status code
543 static int atadev_read ( struct ata_device
*atadev
,
544 struct interface
*block
,
545 uint64_t lba
, unsigned int count
,
546 userptr_t buffer
, size_t len
) {
547 return atadev_command ( atadev
, block
, &atacmd_read
,
548 lba
, count
, buffer
, len
);
552 * Issue ATA block write
554 * @v atadev ATA device
555 * @v block Block data interface
556 * @v lba Starting logical block address
557 * @v count Number of blocks to transfer
558 * @v buffer Data buffer
559 * @v len Length of data buffer
560 * @ret rc Return status code
562 static int atadev_write ( struct ata_device
*atadev
,
563 struct interface
*block
,
564 uint64_t lba
, unsigned int count
,
565 userptr_t buffer
, size_t len
) {
566 return atadev_command ( atadev
, block
, &atacmd_write
,
567 lba
, count
, buffer
, len
);
571 * Read ATA device capacity
573 * @v atadev ATA device
574 * @v block Block data interface
575 * @ret rc Return status code
577 static int atadev_read_capacity ( struct ata_device
*atadev
,
578 struct interface
*block
) {
579 struct ata_identity
*identity
;
581 assert ( atacmd_identify
.priv_len
== sizeof ( *identity
) );
582 assert ( atacmd_identify
.priv_len
== ATA_SECTOR_SIZE
);
583 return atadev_command ( atadev
, block
, &atacmd_identify
,
584 0, 1, UNULL
, ATA_SECTOR_SIZE
);
590 * @v atadev ATA device
591 * @v rc Reason for close
593 static void atadev_close ( struct ata_device
*atadev
, int rc
) {
594 struct ata_command
*atacmd
;
595 struct ata_command
*tmp
;
597 /* Shut down interfaces */
598 intf_shutdown ( &atadev
->block
, rc
);
599 intf_shutdown ( &atadev
->ata
, rc
);
601 /* Shut down any remaining commands */
602 list_for_each_entry_safe ( atacmd
, tmp
, &ata_commands
, list
) {
603 if ( atacmd
->atadev
!= atadev
)
605 atacmd_get ( atacmd
);
606 atacmd_close ( atacmd
, rc
);
607 atacmd_put ( atacmd
);
612 * Describe ATA device using EDD
614 * @v atadev ATA device
615 * @v type EDD interface type
616 * @v path EDD device path
617 * @ret rc Return status code
619 static int atadev_edd_describe ( struct ata_device
*atadev
,
620 struct edd_interface_type
*type
,
621 union edd_device_path
*path
) {
623 type
->type
= cpu_to_le64 ( EDD_INTF_TYPE_ATA
);
624 path
->ata
.slave
= ( ( atadev
->device
== ATA_DEV_SLAVE
) ?
0x01 : 0x00 );
628 /** ATA device block interface operations */
629 static struct interface_operation atadev_block_op
[] = {
630 INTF_OP ( block_read
, struct ata_device
*, atadev_read
),
631 INTF_OP ( block_write
, struct ata_device
*, atadev_write
),
632 INTF_OP ( block_read_capacity
, struct ata_device
*,
633 atadev_read_capacity
),
634 INTF_OP ( intf_close
, struct ata_device
*, atadev_close
),
635 INTF_OP ( edd_describe
, struct ata_device
*, atadev_edd_describe
),
638 /** ATA device block interface descriptor */
639 static struct interface_descriptor atadev_block_desc
=
640 INTF_DESC_PASSTHRU ( struct ata_device
, block
,
641 atadev_block_op
, ata
);
643 /** ATA device ATA interface operations */
644 static struct interface_operation atadev_ata_op
[] = {
645 INTF_OP ( intf_close
, struct ata_device
*, atadev_close
),
648 /** ATA device ATA interface descriptor */
649 static struct interface_descriptor atadev_ata_desc
=
650 INTF_DESC_PASSTHRU ( struct ata_device
, ata
,
651 atadev_ata_op
, block
);
656 * @v block Block control interface
657 * @v ata ATA control interface
658 * @v device ATA device number
659 * @v max_count Maximum number of blocks per single transfer
660 * @ret rc Return status code
662 int ata_open ( struct interface
*block
, struct interface
*ata
,
663 unsigned int device
, unsigned int max_count
) {
664 struct ata_device
*atadev
;
666 /* Allocate and initialise structure */
667 atadev
= zalloc ( sizeof ( *atadev
) );
670 ref_init ( &atadev
->refcnt
, NULL
);
671 intf_init ( &atadev
->block
, &atadev_block_desc
, &atadev
->refcnt
);
672 intf_init ( &atadev
->ata
, &atadev_ata_desc
, &atadev
->refcnt
);
673 atadev
->device
= device
;
674 atadev
->max_count
= max_count
;
676 /* Attach to ATA and parent and interfaces, mortalise self,
679 intf_plug_plug ( &atadev
->ata
, ata
);
680 intf_plug_plug ( &atadev
->block
, block
);
681 ref_put ( &atadev
->refcnt
);