ide-lib.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/string.h>
  4. #include <linux/kernel.h>
  5. #include <linux/export.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/ide.h>
  8. #include <linux/bitops.h>
  9. u64 ide_get_lba_addr(struct ide_cmd *cmd, int lba48)
  10. {
  11. struct ide_taskfile *tf = &cmd->tf;
  12. u32 high, low;
  13. low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
  14. if (lba48) {
  15. tf = &cmd->hob;
  16. high = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal;
  17. } else
  18. high = tf->device & 0xf;
  19. return ((u64)high << 24) | low;
  20. }
  21. EXPORT_SYMBOL_GPL(ide_get_lba_addr);
  22. static void ide_dump_sector(ide_drive_t *drive)
  23. {
  24. struct ide_cmd cmd;
  25. struct ide_taskfile *tf = &cmd.tf;
  26. u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
  27. memset(&cmd, 0, sizeof(cmd));
  28. if (lba48) {
  29. cmd.valid.in.tf = IDE_VALID_LBA;
  30. cmd.valid.in.hob = IDE_VALID_LBA;
  31. cmd.tf_flags = IDE_TFLAG_LBA48;
  32. } else
  33. cmd.valid.in.tf = IDE_VALID_LBA | IDE_VALID_DEVICE;
  34. ide_tf_readback(drive, &cmd);
  35. if (lba48 || (tf->device & ATA_LBA))
  36. printk(KERN_CONT ", LBAsect=%llu",
  37. (unsigned long long)ide_get_lba_addr(&cmd, lba48));
  38. else
  39. printk(KERN_CONT ", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam,
  40. tf->device & 0xf, tf->lbal);
  41. }
  42. static void ide_dump_ata_error(ide_drive_t *drive, u8 err)
  43. {
  44. printk(KERN_CONT "{ ");
  45. if (err & ATA_ABORTED)
  46. printk(KERN_CONT "DriveStatusError ");
  47. if (err & ATA_ICRC)
  48. printk(KERN_CONT "%s",
  49. (err & ATA_ABORTED) ? "BadCRC " : "BadSector ");
  50. if (err & ATA_UNC)
  51. printk(KERN_CONT "UncorrectableError ");
  52. if (err & ATA_IDNF)
  53. printk(KERN_CONT "SectorIdNotFound ");
  54. if (err & ATA_TRK0NF)
  55. printk(KERN_CONT "TrackZeroNotFound ");
  56. if (err & ATA_AMNF)
  57. printk(KERN_CONT "AddrMarkNotFound ");
  58. printk(KERN_CONT "}");
  59. if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK ||
  60. (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) {
  61. struct request *rq = drive->hwif->rq;
  62. ide_dump_sector(drive);
  63. if (rq)
  64. printk(KERN_CONT ", sector=%llu",
  65. (unsigned long long)blk_rq_pos(rq));
  66. }
  67. printk(KERN_CONT "\n");
  68. }
  69. static void ide_dump_atapi_error(ide_drive_t *drive, u8 err)
  70. {
  71. printk(KERN_CONT "{ ");
  72. if (err & ATAPI_ILI)
  73. printk(KERN_CONT "IllegalLengthIndication ");
  74. if (err & ATAPI_EOM)
  75. printk(KERN_CONT "EndOfMedia ");
  76. if (err & ATA_ABORTED)
  77. printk(KERN_CONT "AbortedCommand ");
  78. if (err & ATA_MCR)
  79. printk(KERN_CONT "MediaChangeRequested ");
  80. if (err & ATAPI_LFS)
  81. printk(KERN_CONT "LastFailedSense=0x%02x ",
  82. (err & ATAPI_LFS) >> 4);
  83. printk(KERN_CONT "}\n");
  84. }
  85. /**
  86. * ide_dump_status - translate ATA/ATAPI error
  87. * @drive: drive that status applies to
  88. * @msg: text message to print
  89. * @stat: status byte to decode
  90. *
  91. * Error reporting, in human readable form (luxurious, but a memory hog).
  92. * Combines the drive name, message and status byte to provide a
  93. * user understandable explanation of the device error.
  94. */
  95. u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat)
  96. {
  97. u8 err = 0;
  98. printk(KERN_ERR "%s: %s: status=0x%02x { ", drive->name, msg, stat);
  99. if (stat & ATA_BUSY)
  100. printk(KERN_CONT "Busy ");
  101. else {
  102. if (stat & ATA_DRDY)
  103. printk(KERN_CONT "DriveReady ");
  104. if (stat & ATA_DF)
  105. printk(KERN_CONT "DeviceFault ");
  106. if (stat & ATA_DSC)
  107. printk(KERN_CONT "SeekComplete ");
  108. if (stat & ATA_DRQ)
  109. printk(KERN_CONT "DataRequest ");
  110. if (stat & ATA_CORR)
  111. printk(KERN_CONT "CorrectedError ");
  112. if (stat & ATA_SENSE)
  113. printk(KERN_CONT "Sense ");
  114. if (stat & ATA_ERR)
  115. printk(KERN_CONT "Error ");
  116. }
  117. printk(KERN_CONT "}\n");
  118. if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) {
  119. err = ide_read_error(drive);
  120. printk(KERN_ERR "%s: %s: error=0x%02x ", drive->name, msg, err);
  121. if (drive->media == ide_disk)
  122. ide_dump_ata_error(drive, err);
  123. else
  124. ide_dump_atapi_error(drive, err);
  125. }
  126. printk(KERN_ERR "%s: possibly failed opcode: 0x%02x\n",
  127. drive->name, drive->hwif->cmd.tf.command);
  128. return err;
  129. }
  130. EXPORT_SYMBOL(ide_dump_status);