Purge warnings from prism2 drivers
[gpxe.git] / src / drivers / block / ata.c
blob555a5f6ec836b0fda745029a841413977d8d3bb5
1 /*
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.
19 #include <stddef.h>
20 #include <string.h>
21 #include <assert.h>
22 #include <byteswap.h>
23 #include <gpxe/blockdev.h>
24 #include <gpxe/ata.h>
26 /** @file
28 * ATA block device
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 );
37 /**
38 * Issue ATA command
40 * @v ata ATA device
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 LBA%s %llx count %04x\n",
47 command->cb.cmd_stat, command->cb.device,
48 ( command->cb.lba48 ? "48" : "" ),
49 ( unsigned long long ) command->cb.lba.native,
50 command->cb.count.native );
52 return ata->command ( ata, command );
55 /**
56 * Read block from ATA device
58 * @v blockdev Block device
59 * @v block LBA block number
60 * @v count Block count
61 * @v buffer Data buffer
62 * @ret rc Return status code
64 static int ata_read ( struct block_device *blockdev, uint64_t block,
65 unsigned long count, userptr_t buffer ) {
66 struct ata_device *ata = block_to_ata ( blockdev );
67 struct ata_command command;
69 memset ( &command, 0, sizeof ( command ) );
70 command.cb.lba.native = block;
71 command.cb.count.native = count;
72 command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
73 command.cb.lba48 = ata->lba48;
74 if ( ! ata->lba48 )
75 command.cb.device |= command.cb.lba.bytes.low_prev;
76 command.cb.cmd_stat = ( ata->lba48 ? ATA_CMD_READ_EXT : ATA_CMD_READ );
77 command.data_in = buffer;
78 return ata_command ( ata, &command );
81 /**
82 * Write block to ATA device
84 * @v blockdev Block device
85 * @v block LBA block number
86 * @v count Block count
87 * @v buffer Data buffer
88 * @ret rc Return status code
90 static int ata_write ( struct block_device *blockdev, uint64_t block,
91 unsigned long count, userptr_t buffer ) {
92 struct ata_device *ata = block_to_ata ( blockdev );
93 struct ata_command command;
95 memset ( &command, 0, sizeof ( command ) );
96 command.cb.lba.native = block;
97 command.cb.count.native = count;
98 command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
99 command.cb.lba48 = ata->lba48;
100 if ( ! ata->lba48 )
101 command.cb.device |= command.cb.lba.bytes.low_prev;
102 command.cb.cmd_stat = ( ata->lba48 ?
103 ATA_CMD_WRITE_EXT : ATA_CMD_WRITE );
104 command.data_out = buffer;
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;
118 int rc;
120 /* Issue IDENTIFY */
121 memset ( &command, 0, sizeof ( command ) );
122 command.cb.count.native = 1;
123 command.cb.device = ( ata->device | ATA_DEV_OBSOLETE | ATA_DEV_LBA );
124 command.cb.cmd_stat = ATA_CMD_IDENTIFY;
125 command.data_in = virt_to_user ( &identity );
126 linker_assert ( sizeof ( identity ) == ATA_SECTOR_SIZE,
127 __ata_identity_bad_size__ );
128 if ( ( rc = ata_command ( ata, &command ) ) != 0 )
129 return rc;
131 /* Fill in block device parameters */
132 blockdev->blksize = ATA_SECTOR_SIZE;
133 if ( identity.supports_lba48 & cpu_to_le16 ( ATA_SUPPORTS_LBA48 ) ) {
134 ata->lba48 = 1;
135 blockdev->blocks = le64_to_cpu ( identity.lba48_sectors );
136 } else {
137 blockdev->blocks = le32_to_cpu ( identity.lba_sectors );
139 return 0;
143 * Initialise ATA device
145 * @v ata 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 );