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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <gpxe/blockdev.h>
32 static inline __attribute__ (( always_inline )) struct ata_device *
33 block_to_ata ( struct block_device *blockdev ) {
34 return container_of ( blockdev, struct ata_device, blockdev );
41 * @v command ATA command
42 * @ret rc Return status code
44 static inline __attribute__ (( always_inline )) int
45 ata_command ( struct ata_device *ata, struct ata_command *command ) {
46 DBG ( "ATA cmd %02x dev %02x fl %02x LBA %llx count %04x\n",
47 command->cb.cmd_stat, command->cb.device, command->cb.flags,
48 ( unsigned long long ) command->cb.lba.native,
49 command->cb.count.native );
51 return ata->command ( ata, command );
55 * Read block from ATA device
57 * @v blockdev Block device
58 * @v block LBA block number
59 * @v count Block count
60 * @v buffer Data buffer
61 * @ret rc Return status code
63 static int ata_read ( struct block_device *blockdev, uint64_t block,
64 unsigned long count, userptr_t buffer ) {
65 struct ata_device *ata = block_to_ata ( blockdev );
66 struct ata_command command;
68 memset ( &command, 0, sizeof ( command ) );
69 command.cb.lba.native = block;
70 command.cb.count.native = count;
71 command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
72 command.cb.lba48 = ata->lba48;
74 command.cb.device |= command.cb.lba.bytes.low_prev;
75 command.cb.cmd_stat = ( ata->lba48 ? ATA_CMD_READ_EXT : ATA_CMD_READ );
76 command.data_in = buffer;
77 return ata_command ( ata, &command );
81 * Write block to ATA device
83 * @v blockdev Block device
84 * @v block LBA block number
85 * @v count Block count
86 * @v buffer Data buffer
87 * @ret rc Return status code
89 static int ata_write ( struct block_device *blockdev, uint64_t block,
90 unsigned long count, userptr_t buffer ) {
91 struct ata_device *ata = block_to_ata ( blockdev );
92 struct ata_command command;
94 memset ( &command, 0, sizeof ( command ) );
95 command.cb.lba.native = block;
96 command.cb.count.native = count;
97 command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
98 command.cb.lba48 = ata->lba48;
100 command.cb.device |= command.cb.lba.bytes.low_prev;
101 command.cb.cmd_stat = ( ata->lba48 ?
102 ATA_CMD_WRITE_EXT : ATA_CMD_WRITE );
103 command.data_out = buffer;
104 return ata_command ( ata, &command );
108 * Identify ATA device
110 * @v blockdev Block device
111 * @ret rc Return status code
113 static int ata_identify ( struct block_device *blockdev ) {
114 struct ata_device *ata = block_to_ata ( blockdev );
115 struct ata_command command;
116 struct ata_identity identity;
120 memset ( &command, 0, sizeof ( command ) );
121 command.cb.count.native = 1;
122 command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
123 command.cb.cmd_stat = ATA_CMD_IDENTIFY;
124 command.data_in = virt_to_user ( &identity );
125 linker_assert ( sizeof ( identity ) == ATA_SECTOR_SIZE,
126 __ata_identity_bad_size__ );
127 if ( ( rc = ata_command ( ata, &command ) ) != 0 )
130 /* Fill in block device parameters */
131 blockdev->blksize = ATA_SECTOR_SIZE;
132 if ( identity.supports_lba48 & cpu_to_le16 ( ATA_SUPPORTS_LBA48 ) ) {
134 blockdev->blocks = le64_to_cpu ( identity.lba48_sectors );
136 blockdev->blocks = le32_to_cpu ( identity.lba_sectors );
142 * Initialise ATA device
145 * @ret rc Return status code
147 * Initialises an ATA device. The ata_device::command field and the
148 * @c ATA_FL_SLAVE portion of the ata_device::flags field must already
149 * be filled in. This function will configure ata_device::blockdev,
150 * including issuing an IDENTIFY DEVICE call to determine the block
151 * size and total device size.
153 int init_atadev ( struct ata_device *ata ) {
154 /** Fill in read and write methods, and get device capacity */
155 ata->blockdev.read = ata_read;
156 ata->blockdev.write = ata_write;
157 return ata_identify ( &ata->blockdev );