[e16e8f2] | 1 | /* |
---|
| 2 | * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>. |
---|
| 3 | * |
---|
| 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. |
---|
| 8 | * |
---|
| 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. |
---|
| 13 | * |
---|
| 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. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | FILE_LICENCE ( GPL2_OR_LATER ); |
---|
| 20 | |
---|
| 21 | #include <stddef.h> |
---|
| 22 | #include <string.h> |
---|
| 23 | #include <assert.h> |
---|
| 24 | #include <errno.h> |
---|
| 25 | #include <byteswap.h> |
---|
| 26 | #include <gpxe/blockdev.h> |
---|
| 27 | #include <gpxe/process.h> |
---|
| 28 | #include <gpxe/ata.h> |
---|
| 29 | |
---|
| 30 | /** @file |
---|
| 31 | * |
---|
| 32 | * ATA block device |
---|
| 33 | * |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | static inline __attribute__ (( always_inline )) struct ata_device * |
---|
| 37 | block_to_ata ( struct block_device *blockdev ) { |
---|
| 38 | return container_of ( blockdev, struct ata_device, blockdev ); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Issue ATA command |
---|
| 43 | * |
---|
| 44 | * @v ata ATA device |
---|
| 45 | * @v command ATA command |
---|
| 46 | * @ret rc Return status code |
---|
| 47 | */ |
---|
| 48 | static inline __attribute__ (( always_inline )) int |
---|
| 49 | ata_command ( struct ata_device *ata, struct ata_command *command ) { |
---|
| 50 | int rc; |
---|
| 51 | |
---|
| 52 | DBG ( "ATA cmd %02x dev %02x LBA%s %llx count %04x\n", |
---|
| 53 | command->cb.cmd_stat, command->cb.device, |
---|
| 54 | ( command->cb.lba48 ? "48" : "" ), |
---|
| 55 | ( unsigned long long ) command->cb.lba.native, |
---|
| 56 | command->cb.count.native ); |
---|
| 57 | |
---|
| 58 | /* Flag command as in-progress */ |
---|
| 59 | command->rc = -EINPROGRESS; |
---|
| 60 | |
---|
| 61 | /* Issue ATA command */ |
---|
| 62 | if ( ( rc = ata->command ( ata, command ) ) != 0 ) { |
---|
| 63 | /* Something went wrong with the issuing mechanism */ |
---|
| 64 | DBG ( "ATA could not issue command: %s\n", strerror ( rc ) ); |
---|
| 65 | return rc; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /* Wait for command to complete */ |
---|
| 69 | while ( command->rc == -EINPROGRESS ) |
---|
| 70 | step(); |
---|
| 71 | if ( ( rc = command->rc ) != 0 ) { |
---|
| 72 | /* Something went wrong with the command execution */ |
---|
| 73 | DBG ( "ATA command failed: %s\n", strerror ( rc ) ); |
---|
| 74 | return rc; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | return 0; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Read block from ATA device |
---|
| 82 | * |
---|
| 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 |
---|
| 88 | */ |
---|
| 89 | static int ata_read ( 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; |
---|
| 93 | |
---|
| 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; |
---|
| 99 | if ( ! ata->lba48 ) |
---|
| 100 | command.cb.device |= command.cb.lba.bytes.low_prev; |
---|
| 101 | command.cb.cmd_stat = ( ata->lba48 ? ATA_CMD_READ_EXT : ATA_CMD_READ ); |
---|
| 102 | command.data_in = buffer; |
---|
| 103 | return ata_command ( ata, &command ); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Write block to ATA device |
---|
| 108 | * |
---|
| 109 | * @v blockdev Block device |
---|
| 110 | * @v block LBA block number |
---|
| 111 | * @v count Block count |
---|
| 112 | * @v buffer Data buffer |
---|
| 113 | * @ret rc Return status code |
---|
| 114 | */ |
---|
| 115 | static int ata_write ( struct block_device *blockdev, uint64_t block, |
---|
| 116 | unsigned long count, userptr_t buffer ) { |
---|
| 117 | struct ata_device *ata = block_to_ata ( blockdev ); |
---|
| 118 | struct ata_command command; |
---|
| 119 | |
---|
| 120 | memset ( &command, 0, sizeof ( command ) ); |
---|
| 121 | command.cb.lba.native = block; |
---|
| 122 | command.cb.count.native = count; |
---|
| 123 | command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA ); |
---|
| 124 | command.cb.lba48 = ata->lba48; |
---|
| 125 | if ( ! ata->lba48 ) |
---|
| 126 | command.cb.device |= command.cb.lba.bytes.low_prev; |
---|
| 127 | command.cb.cmd_stat = ( ata->lba48 ? |
---|
| 128 | ATA_CMD_WRITE_EXT : ATA_CMD_WRITE ); |
---|
| 129 | command.data_out = buffer; |
---|
| 130 | return ata_command ( ata, &command ); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * Identify ATA device |
---|
| 135 | * |
---|
| 136 | * @v blockdev Block device |
---|
| 137 | * @ret rc Return status code |
---|
| 138 | */ |
---|
| 139 | static int ata_identify ( struct block_device *blockdev ) { |
---|
| 140 | struct ata_device *ata = block_to_ata ( blockdev ); |
---|
| 141 | struct ata_command command; |
---|
| 142 | struct ata_identity identity; |
---|
| 143 | int rc; |
---|
| 144 | |
---|
| 145 | /* Issue IDENTIFY */ |
---|
| 146 | memset ( &command, 0, sizeof ( command ) ); |
---|
| 147 | command.cb.count.native = 1; |
---|
| 148 | command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA ); |
---|
| 149 | command.cb.cmd_stat = ATA_CMD_IDENTIFY; |
---|
| 150 | command.data_in = virt_to_user ( &identity ); |
---|
| 151 | linker_assert ( sizeof ( identity ) == ATA_SECTOR_SIZE, |
---|
| 152 | __ata_identity_bad_size__ ); |
---|
| 153 | if ( ( rc = ata_command ( ata, &command ) ) != 0 ) |
---|
| 154 | return rc; |
---|
| 155 | |
---|
| 156 | /* Fill in block device parameters */ |
---|
| 157 | blockdev->blksize = ATA_SECTOR_SIZE; |
---|
| 158 | if ( identity.supports_lba48 & cpu_to_le16 ( ATA_SUPPORTS_LBA48 ) ) { |
---|
| 159 | ata->lba48 = 1; |
---|
| 160 | blockdev->blocks = le64_to_cpu ( identity.lba48_sectors ); |
---|
| 161 | } else { |
---|
| 162 | blockdev->blocks = le32_to_cpu ( identity.lba_sectors ); |
---|
| 163 | } |
---|
| 164 | return 0; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | static struct block_device_operations ata_operations = { |
---|
| 168 | .read = ata_read, |
---|
| 169 | .write = ata_write |
---|
| 170 | }; |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * Initialise ATA device |
---|
| 174 | * |
---|
| 175 | * @v ata ATA device |
---|
| 176 | * @ret rc Return status code |
---|
| 177 | * |
---|
| 178 | * Initialises an ATA device. The ata_device::command field and the |
---|
| 179 | * @c ATA_FL_SLAVE portion of the ata_device::flags field must already |
---|
| 180 | * be filled in. This function will configure ata_device::blockdev, |
---|
| 181 | * including issuing an IDENTIFY DEVICE call to determine the block |
---|
| 182 | * size and total device size. |
---|
| 183 | */ |
---|
| 184 | int init_atadev ( struct ata_device *ata ) { |
---|
| 185 | /** Fill in read and write methods, and get device capacity */ |
---|
| 186 | ata->blockdev.op = &ata_operations; |
---|
| 187 | return ata_identify ( &ata->blockdev ); |
---|
| 188 | } |
---|