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.
22 #include <gpxe/blockdev.h>
31 static inline __attribute__ (( always_inline )) struct ata_device *
32 block_to_ata ( struct block_device *blockdev ) {
33 return container_of ( blockdev, struct ata_device, blockdev );
40 * @v command ATA command
41 * @ret rc Return status code
43 static inline __attribute__ (( always_inline )) int
44 ata_command ( struct ata_device *ata, struct ata_command *command ) {
45 DBG ( "ATA cmd %02x dev %02x fl %02x LBA %llx count %04x\n",
46 command->cb.cmd_stat, command->cb.device, command->cb.flags,
47 ( unsigned long long ) command->cb.lba.native,
48 command->cb.count.native );
50 return ata->command ( ata, command );
54 * Read block from ATA device
56 * @v blockdev Block device
57 * @v block LBA block number
58 * @v count Block count
59 * @v buffer Data buffer
60 * @ret rc Return status code
62 static int ata_read ( struct block_device *blockdev, uint64_t block,
63 unsigned long count, userptr_t buffer ) {
64 struct ata_device *ata = block_to_ata ( blockdev );
65 struct ata_command command;
67 memset ( &command, 0, sizeof ( command ) );
68 command.cb.lba.native = block;
69 command.cb.count.native = count;
70 command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
71 command.cb.lba48 = ata->lba48;
73 command.cb.device |= command.cb.lba.bytes.low_prev;
74 command.cb.cmd_stat = ( ata->lba48 ? ATA_CMD_READ_EXT : ATA_CMD_READ );
75 command.data_in = buffer;
76 command.data_in_len = ( count * blockdev->blksize );
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 command.data_out_len = ( count * blockdev->blksize );
105 return ata_command ( ata, &command );
109 * Identify ATA device
111 * @v blockdev Block device
112 * @ret rc Return status code
114 static int ata_identify ( struct block_device *blockdev ) {
115 struct ata_device *ata = block_to_ata ( blockdev );
116 struct ata_command command;
117 struct ata_identity identity;
121 memset ( &command, 0, sizeof ( command ) );
122 command.cb.count.native = 1; /* n/a according to spec, but at least
123 * AoE vblade devices require it. */
124 command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
125 command.cb.cmd_stat = ATA_CMD_IDENTIFY;
126 command.data_in = virt_to_user ( &identity );
127 command.data_in_len = sizeof ( identity );
128 if ( ( rc = ata_command ( ata, &command ) ) != 0 )
131 /* Fill in block device parameters */
132 blockdev->blksize = ATA_SECTOR_SIZE;
133 if ( identity.supports_lba48 & cpu_to_le16 ( ATA_SUPPORTS_LBA48 ) ) {
135 blockdev->blocks = le64_to_cpu ( identity.lba48_sectors );
137 blockdev->blocks = le32_to_cpu ( identity.lba_sectors );
143 * Initialise ATA device
146 * @ret rc Return status code
148 * Initialises an ATA device. The ata_device::command field and the
149 * @c ATA_FL_SLAVE portion of the ata_device::flags field must already
150 * be filled in. This function will configure ata_device::blockdev,
151 * including issuing an IDENTIFY DEVICE call to determine the block
152 * size and total device size.
154 int init_atadev ( struct ata_device *ata ) {
155 /** Fill in read and write methods, and get device capacity */
156 ata->blockdev.read = ata_read;
157 ata->blockdev.write = ata_write;
158 return ata_identify ( &ata->blockdev );