You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ata.c 5.4KB

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