ata.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. /*
  2. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  3. * Copyright 2003-2004 Jeff Garzik
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; see the file COPYING. If not, write to
  18. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. *
  21. * libata documentation is available via 'make {ps|pdf}docs',
  22. * as Documentation/DocBook/libata.*
  23. *
  24. * Hardware documentation available from http://www.t13.org/
  25. *
  26. */
  27. #ifndef __LINUX_ATA_H__
  28. #define __LINUX_ATA_H__
  29. #include <linux/kernel.h>
  30. #include <linux/string.h>
  31. #include <linux/types.h>
  32. #include <asm/byteorder.h>
  33. /* defines only for the constants which don't work well as enums */
  34. #define ATA_DMA_BOUNDARY 0xffffUL
  35. #define ATA_DMA_MASK 0xffffffffULL
  36. enum {
  37. /* various global constants */
  38. ATA_MAX_DEVICES = 2, /* per bus/port */
  39. ATA_MAX_PRD = 256, /* we could make these 256/256 */
  40. ATA_SECT_SIZE = 512,
  41. ATA_MAX_SECTORS_128 = 128,
  42. ATA_MAX_SECTORS = 256,
  43. ATA_MAX_SECTORS_LBA48 = 65535,/* TODO: 65536? */
  44. ATA_MAX_SECTORS_TAPE = 65535,
  45. ATA_ID_WORDS = 256,
  46. ATA_ID_CONFIG = 0,
  47. ATA_ID_CYLS = 1,
  48. ATA_ID_HEADS = 3,
  49. ATA_ID_SECTORS = 6,
  50. ATA_ID_SERNO = 10,
  51. ATA_ID_BUF_SIZE = 21,
  52. ATA_ID_FW_REV = 23,
  53. ATA_ID_PROD = 27,
  54. ATA_ID_MAX_MULTSECT = 47,
  55. ATA_ID_DWORD_IO = 48,
  56. ATA_ID_CAPABILITY = 49,
  57. ATA_ID_OLD_PIO_MODES = 51,
  58. ATA_ID_OLD_DMA_MODES = 52,
  59. ATA_ID_FIELD_VALID = 53,
  60. ATA_ID_CUR_CYLS = 54,
  61. ATA_ID_CUR_HEADS = 55,
  62. ATA_ID_CUR_SECTORS = 56,
  63. ATA_ID_MULTSECT = 59,
  64. ATA_ID_LBA_CAPACITY = 60,
  65. ATA_ID_SWDMA_MODES = 62,
  66. ATA_ID_MWDMA_MODES = 63,
  67. ATA_ID_PIO_MODES = 64,
  68. ATA_ID_EIDE_DMA_MIN = 65,
  69. ATA_ID_EIDE_DMA_TIME = 66,
  70. ATA_ID_EIDE_PIO = 67,
  71. ATA_ID_EIDE_PIO_IORDY = 68,
  72. ATA_ID_ADDITIONAL_SUPP = 69,
  73. ATA_ID_QUEUE_DEPTH = 75,
  74. ATA_ID_MAJOR_VER = 80,
  75. ATA_ID_COMMAND_SET_1 = 82,
  76. ATA_ID_COMMAND_SET_2 = 83,
  77. ATA_ID_CFSSE = 84,
  78. ATA_ID_CFS_ENABLE_1 = 85,
  79. ATA_ID_CFS_ENABLE_2 = 86,
  80. ATA_ID_CSF_DEFAULT = 87,
  81. ATA_ID_UDMA_MODES = 88,
  82. ATA_ID_HW_CONFIG = 93,
  83. ATA_ID_SPG = 98,
  84. ATA_ID_LBA_CAPACITY_2 = 100,
  85. ATA_ID_SECTOR_SIZE = 106,
  86. ATA_ID_WWN = 108,
  87. ATA_ID_LOGICAL_SECTOR_SIZE = 117, /* and 118 */
  88. ATA_ID_LAST_LUN = 126,
  89. ATA_ID_DLF = 128,
  90. ATA_ID_CSFO = 129,
  91. ATA_ID_CFA_POWER = 160,
  92. ATA_ID_CFA_KEY_MGMT = 162,
  93. ATA_ID_CFA_MODES = 163,
  94. ATA_ID_DATA_SET_MGMT = 169,
  95. ATA_ID_ROT_SPEED = 217,
  96. ATA_ID_PIO4 = (1 << 1),
  97. ATA_ID_SERNO_LEN = 20,
  98. ATA_ID_FW_REV_LEN = 8,
  99. ATA_ID_PROD_LEN = 40,
  100. ATA_ID_WWN_LEN = 8,
  101. ATA_PCI_CTL_OFS = 2,
  102. ATA_PIO0 = (1 << 0),
  103. ATA_PIO1 = ATA_PIO0 | (1 << 1),
  104. ATA_PIO2 = ATA_PIO1 | (1 << 2),
  105. ATA_PIO3 = ATA_PIO2 | (1 << 3),
  106. ATA_PIO4 = ATA_PIO3 | (1 << 4),
  107. ATA_PIO5 = ATA_PIO4 | (1 << 5),
  108. ATA_PIO6 = ATA_PIO5 | (1 << 6),
  109. ATA_PIO4_ONLY = (1 << 4),
  110. ATA_SWDMA0 = (1 << 0),
  111. ATA_SWDMA1 = ATA_SWDMA0 | (1 << 1),
  112. ATA_SWDMA2 = ATA_SWDMA1 | (1 << 2),
  113. ATA_SWDMA2_ONLY = (1 << 2),
  114. ATA_MWDMA0 = (1 << 0),
  115. ATA_MWDMA1 = ATA_MWDMA0 | (1 << 1),
  116. ATA_MWDMA2 = ATA_MWDMA1 | (1 << 2),
  117. ATA_MWDMA3 = ATA_MWDMA2 | (1 << 3),
  118. ATA_MWDMA4 = ATA_MWDMA3 | (1 << 4),
  119. ATA_MWDMA12_ONLY = (1 << 1) | (1 << 2),
  120. ATA_MWDMA2_ONLY = (1 << 2),
  121. ATA_UDMA0 = (1 << 0),
  122. ATA_UDMA1 = ATA_UDMA0 | (1 << 1),
  123. ATA_UDMA2 = ATA_UDMA1 | (1 << 2),
  124. ATA_UDMA3 = ATA_UDMA2 | (1 << 3),
  125. ATA_UDMA4 = ATA_UDMA3 | (1 << 4),
  126. ATA_UDMA5 = ATA_UDMA4 | (1 << 5),
  127. ATA_UDMA6 = ATA_UDMA5 | (1 << 6),
  128. ATA_UDMA7 = ATA_UDMA6 | (1 << 7),
  129. /* ATA_UDMA7 is just for completeness... doesn't exist (yet?). */
  130. ATA_UDMA24_ONLY = (1 << 2) | (1 << 4),
  131. ATA_UDMA_MASK_40C = ATA_UDMA2, /* udma0-2 */
  132. /* DMA-related */
  133. ATA_PRD_SZ = 8,
  134. ATA_PRD_TBL_SZ = (ATA_MAX_PRD * ATA_PRD_SZ),
  135. ATA_PRD_EOT = (1 << 31), /* end-of-table flag */
  136. ATA_DMA_TABLE_OFS = 4,
  137. ATA_DMA_STATUS = 2,
  138. ATA_DMA_CMD = 0,
  139. ATA_DMA_WR = (1 << 3),
  140. ATA_DMA_START = (1 << 0),
  141. ATA_DMA_INTR = (1 << 2),
  142. ATA_DMA_ERR = (1 << 1),
  143. ATA_DMA_ACTIVE = (1 << 0),
  144. /* bits in ATA command block registers */
  145. ATA_HOB = (1 << 7), /* LBA48 selector */
  146. ATA_NIEN = (1 << 1), /* disable-irq flag */
  147. ATA_LBA = (1 << 6), /* LBA28 selector */
  148. ATA_DEV1 = (1 << 4), /* Select Device 1 (slave) */
  149. ATA_DEVICE_OBS = (1 << 7) | (1 << 5), /* obs bits in dev reg */
  150. ATA_DEVCTL_OBS = (1 << 3), /* obsolete bit in devctl reg */
  151. ATA_BUSY = (1 << 7), /* BSY status bit */
  152. ATA_DRDY = (1 << 6), /* device ready */
  153. ATA_DF = (1 << 5), /* device fault */
  154. ATA_DSC = (1 << 4), /* drive seek complete */
  155. ATA_DRQ = (1 << 3), /* data request i/o */
  156. ATA_CORR = (1 << 2), /* corrected data error */
  157. ATA_IDX = (1 << 1), /* index */
  158. ATA_ERR = (1 << 0), /* have an error */
  159. ATA_SRST = (1 << 2), /* software reset */
  160. ATA_ICRC = (1 << 7), /* interface CRC error */
  161. ATA_BBK = ATA_ICRC, /* pre-EIDE: block marked bad */
  162. ATA_UNC = (1 << 6), /* uncorrectable media error */
  163. ATA_MC = (1 << 5), /* media changed */
  164. ATA_IDNF = (1 << 4), /* ID not found */
  165. ATA_MCR = (1 << 3), /* media change requested */
  166. ATA_ABORTED = (1 << 2), /* command aborted */
  167. ATA_TRK0NF = (1 << 1), /* track 0 not found */
  168. ATA_AMNF = (1 << 0), /* address mark not found */
  169. ATAPI_LFS = 0xF0, /* last failed sense */
  170. ATAPI_EOM = ATA_TRK0NF, /* end of media */
  171. ATAPI_ILI = ATA_AMNF, /* illegal length indication */
  172. ATAPI_IO = (1 << 1),
  173. ATAPI_COD = (1 << 0),
  174. /* ATA command block registers */
  175. ATA_REG_DATA = 0x00,
  176. ATA_REG_ERR = 0x01,
  177. ATA_REG_NSECT = 0x02,
  178. ATA_REG_LBAL = 0x03,
  179. ATA_REG_LBAM = 0x04,
  180. ATA_REG_LBAH = 0x05,
  181. ATA_REG_DEVICE = 0x06,
  182. ATA_REG_STATUS = 0x07,
  183. ATA_REG_FEATURE = ATA_REG_ERR, /* and their aliases */
  184. ATA_REG_CMD = ATA_REG_STATUS,
  185. ATA_REG_BYTEL = ATA_REG_LBAM,
  186. ATA_REG_BYTEH = ATA_REG_LBAH,
  187. ATA_REG_DEVSEL = ATA_REG_DEVICE,
  188. ATA_REG_IRQ = ATA_REG_NSECT,
  189. /* ATA device commands */
  190. ATA_CMD_DEV_RESET = 0x08, /* ATAPI device reset */
  191. ATA_CMD_CHK_POWER = 0xE5, /* check power mode */
  192. ATA_CMD_STANDBY = 0xE2, /* place in standby power mode */
  193. ATA_CMD_IDLE = 0xE3, /* place in idle power mode */
  194. ATA_CMD_EDD = 0x90, /* execute device diagnostic */
  195. ATA_CMD_DOWNLOAD_MICRO = 0x92,
  196. ATA_CMD_NOP = 0x00,
  197. ATA_CMD_FLUSH = 0xE7,
  198. ATA_CMD_FLUSH_EXT = 0xEA,
  199. ATA_CMD_ID_ATA = 0xEC,
  200. ATA_CMD_ID_ATAPI = 0xA1,
  201. ATA_CMD_SERVICE = 0xA2,
  202. ATA_CMD_READ = 0xC8,
  203. ATA_CMD_READ_EXT = 0x25,
  204. ATA_CMD_READ_QUEUED = 0x26,
  205. ATA_CMD_READ_STREAM_EXT = 0x2B,
  206. ATA_CMD_READ_STREAM_DMA_EXT = 0x2A,
  207. ATA_CMD_WRITE = 0xCA,
  208. ATA_CMD_WRITE_EXT = 0x35,
  209. ATA_CMD_WRITE_QUEUED = 0x36,
  210. ATA_CMD_WRITE_STREAM_EXT = 0x3B,
  211. ATA_CMD_WRITE_STREAM_DMA_EXT = 0x3A,
  212. ATA_CMD_WRITE_FUA_EXT = 0x3D,
  213. ATA_CMD_WRITE_QUEUED_FUA_EXT = 0x3E,
  214. ATA_CMD_FPDMA_READ = 0x60,
  215. ATA_CMD_FPDMA_WRITE = 0x61,
  216. ATA_CMD_PIO_READ = 0x20,
  217. ATA_CMD_PIO_READ_EXT = 0x24,
  218. ATA_CMD_PIO_WRITE = 0x30,
  219. ATA_CMD_PIO_WRITE_EXT = 0x34,
  220. ATA_CMD_READ_MULTI = 0xC4,
  221. ATA_CMD_READ_MULTI_EXT = 0x29,
  222. ATA_CMD_WRITE_MULTI = 0xC5,
  223. ATA_CMD_WRITE_MULTI_EXT = 0x39,
  224. ATA_CMD_WRITE_MULTI_FUA_EXT = 0xCE,
  225. ATA_CMD_SET_FEATURES = 0xEF,
  226. ATA_CMD_SET_MULTI = 0xC6,
  227. ATA_CMD_PACKET = 0xA0,
  228. ATA_CMD_VERIFY = 0x40,
  229. ATA_CMD_VERIFY_EXT = 0x42,
  230. ATA_CMD_WRITE_UNCORR_EXT = 0x45,
  231. ATA_CMD_STANDBYNOW1 = 0xE0,
  232. ATA_CMD_IDLEIMMEDIATE = 0xE1,
  233. ATA_CMD_SLEEP = 0xE6,
  234. ATA_CMD_INIT_DEV_PARAMS = 0x91,
  235. ATA_CMD_READ_NATIVE_MAX = 0xF8,
  236. ATA_CMD_READ_NATIVE_MAX_EXT = 0x27,
  237. ATA_CMD_SET_MAX = 0xF9,
  238. ATA_CMD_SET_MAX_EXT = 0x37,
  239. ATA_CMD_READ_LOG_EXT = 0x2F,
  240. ATA_CMD_WRITE_LOG_EXT = 0x3F,
  241. ATA_CMD_READ_LOG_DMA_EXT = 0x47,
  242. ATA_CMD_WRITE_LOG_DMA_EXT = 0x57,
  243. ATA_CMD_TRUSTED_RCV = 0x5C,
  244. ATA_CMD_TRUSTED_RCV_DMA = 0x5D,
  245. ATA_CMD_TRUSTED_SND = 0x5E,
  246. ATA_CMD_TRUSTED_SND_DMA = 0x5F,
  247. ATA_CMD_PMP_READ = 0xE4,
  248. ATA_CMD_PMP_WRITE = 0xE8,
  249. ATA_CMD_CONF_OVERLAY = 0xB1,
  250. ATA_CMD_SEC_SET_PASS = 0xF1,
  251. ATA_CMD_SEC_UNLOCK = 0xF2,
  252. ATA_CMD_SEC_ERASE_PREP = 0xF3,
  253. ATA_CMD_SEC_ERASE_UNIT = 0xF4,
  254. ATA_CMD_SEC_FREEZE_LOCK = 0xF5,
  255. ATA_CMD_SEC_DISABLE_PASS = 0xF6,
  256. ATA_CMD_CONFIG_STREAM = 0x51,
  257. ATA_CMD_SMART = 0xB0,
  258. ATA_CMD_MEDIA_LOCK = 0xDE,
  259. ATA_CMD_MEDIA_UNLOCK = 0xDF,
  260. ATA_CMD_DSM = 0x06,
  261. ATA_CMD_CHK_MED_CRD_TYP = 0xD1,
  262. ATA_CMD_CFA_REQ_EXT_ERR = 0x03,
  263. ATA_CMD_CFA_WRITE_NE = 0x38,
  264. ATA_CMD_CFA_TRANS_SECT = 0x87,
  265. ATA_CMD_CFA_ERASE = 0xC0,
  266. ATA_CMD_CFA_WRITE_MULT_NE = 0xCD,
  267. /* marked obsolete in the ATA/ATAPI-7 spec */
  268. ATA_CMD_RESTORE = 0x10,
  269. /* READ_LOG_EXT pages */
  270. ATA_LOG_SATA_NCQ = 0x10,
  271. /* READ/WRITE LONG (obsolete) */
  272. ATA_CMD_READ_LONG = 0x22,
  273. ATA_CMD_READ_LONG_ONCE = 0x23,
  274. ATA_CMD_WRITE_LONG = 0x32,
  275. ATA_CMD_WRITE_LONG_ONCE = 0x33,
  276. /* SETFEATURES stuff */
  277. SETFEATURES_XFER = 0x03,
  278. XFER_UDMA_7 = 0x47,
  279. XFER_UDMA_6 = 0x46,
  280. XFER_UDMA_5 = 0x45,
  281. XFER_UDMA_4 = 0x44,
  282. XFER_UDMA_3 = 0x43,
  283. XFER_UDMA_2 = 0x42,
  284. XFER_UDMA_1 = 0x41,
  285. XFER_UDMA_0 = 0x40,
  286. XFER_MW_DMA_4 = 0x24, /* CFA only */
  287. XFER_MW_DMA_3 = 0x23, /* CFA only */
  288. XFER_MW_DMA_2 = 0x22,
  289. XFER_MW_DMA_1 = 0x21,
  290. XFER_MW_DMA_0 = 0x20,
  291. XFER_SW_DMA_2 = 0x12,
  292. XFER_SW_DMA_1 = 0x11,
  293. XFER_SW_DMA_0 = 0x10,
  294. XFER_PIO_6 = 0x0E, /* CFA only */
  295. XFER_PIO_5 = 0x0D, /* CFA only */
  296. XFER_PIO_4 = 0x0C,
  297. XFER_PIO_3 = 0x0B,
  298. XFER_PIO_2 = 0x0A,
  299. XFER_PIO_1 = 0x09,
  300. XFER_PIO_0 = 0x08,
  301. XFER_PIO_SLOW = 0x00,
  302. SETFEATURES_WC_ON = 0x02, /* Enable write cache */
  303. SETFEATURES_WC_OFF = 0x82, /* Disable write cache */
  304. /* Enable/Disable Automatic Acoustic Management */
  305. SETFEATURES_AAM_ON = 0x42,
  306. SETFEATURES_AAM_OFF = 0xC2,
  307. SETFEATURES_SPINUP = 0x07, /* Spin-up drive */
  308. SETFEATURES_SATA_ENABLE = 0x10, /* Enable use of SATA feature */
  309. SETFEATURES_SATA_DISABLE = 0x90, /* Disable use of SATA feature */
  310. /* SETFEATURE Sector counts for SATA features */
  311. SATA_FPDMA_OFFSET = 0x01, /* FPDMA non-zero buffer offsets */
  312. SATA_FPDMA_AA = 0x02, /* FPDMA Setup FIS Auto-Activate */
  313. SATA_DIPM = 0x03, /* Device Initiated Power Management */
  314. SATA_FPDMA_IN_ORDER = 0x04, /* FPDMA in-order data delivery */
  315. SATA_AN = 0x05, /* Asynchronous Notification */
  316. SATA_SSP = 0x06, /* Software Settings Preservation */
  317. /* feature values for SET_MAX */
  318. ATA_SET_MAX_ADDR = 0x00,
  319. ATA_SET_MAX_PASSWD = 0x01,
  320. ATA_SET_MAX_LOCK = 0x02,
  321. ATA_SET_MAX_UNLOCK = 0x03,
  322. ATA_SET_MAX_FREEZE_LOCK = 0x04,
  323. /* feature values for DEVICE CONFIGURATION OVERLAY */
  324. ATA_DCO_RESTORE = 0xC0,
  325. ATA_DCO_FREEZE_LOCK = 0xC1,
  326. ATA_DCO_IDENTIFY = 0xC2,
  327. ATA_DCO_SET = 0xC3,
  328. /* feature values for SMART */
  329. ATA_SMART_ENABLE = 0xD8,
  330. ATA_SMART_READ_VALUES = 0xD0,
  331. ATA_SMART_READ_THRESHOLDS = 0xD1,
  332. /* feature values for Data Set Management */
  333. ATA_DSM_TRIM = 0x01,
  334. /* password used in LBA Mid / LBA High for executing SMART commands */
  335. ATA_SMART_LBAM_PASS = 0x4F,
  336. ATA_SMART_LBAH_PASS = 0xC2,
  337. /* ATAPI stuff */
  338. ATAPI_PKT_DMA = (1 << 0),
  339. ATAPI_DMADIR = (1 << 2), /* ATAPI data dir:
  340. 0=to device, 1=to host */
  341. ATAPI_CDB_LEN = 16,
  342. /* PMP stuff */
  343. SATA_PMP_MAX_PORTS = 15,
  344. SATA_PMP_CTRL_PORT = 15,
  345. SATA_PMP_GSCR_DWORDS = 128,
  346. SATA_PMP_GSCR_PROD_ID = 0,
  347. SATA_PMP_GSCR_REV = 1,
  348. SATA_PMP_GSCR_PORT_INFO = 2,
  349. SATA_PMP_GSCR_ERROR = 32,
  350. SATA_PMP_GSCR_ERROR_EN = 33,
  351. SATA_PMP_GSCR_FEAT = 64,
  352. SATA_PMP_GSCR_FEAT_EN = 96,
  353. SATA_PMP_PSCR_STATUS = 0,
  354. SATA_PMP_PSCR_ERROR = 1,
  355. SATA_PMP_PSCR_CONTROL = 2,
  356. SATA_PMP_FEAT_BIST = (1 << 0),
  357. SATA_PMP_FEAT_PMREQ = (1 << 1),
  358. SATA_PMP_FEAT_DYNSSC = (1 << 2),
  359. SATA_PMP_FEAT_NOTIFY = (1 << 3),
  360. /* cable types */
  361. ATA_CBL_NONE = 0,
  362. ATA_CBL_PATA40 = 1,
  363. ATA_CBL_PATA80 = 2,
  364. ATA_CBL_PATA40_SHORT = 3, /* 40 wire cable to high UDMA spec */
  365. ATA_CBL_PATA_UNK = 4, /* don't know, maybe 80c? */
  366. ATA_CBL_PATA_IGN = 5, /* don't know, ignore cable handling */
  367. ATA_CBL_SATA = 6,
  368. /* SATA Status and Control Registers */
  369. SCR_STATUS = 0,
  370. SCR_ERROR = 1,
  371. SCR_CONTROL = 2,
  372. SCR_ACTIVE = 3,
  373. SCR_NOTIFICATION = 4,
  374. /* SError bits */
  375. SERR_DATA_RECOVERED = (1 << 0), /* recovered data error */
  376. SERR_COMM_RECOVERED = (1 << 1), /* recovered comm failure */
  377. SERR_DATA = (1 << 8), /* unrecovered data error */
  378. SERR_PERSISTENT = (1 << 9), /* persistent data/comm error */
  379. SERR_PROTOCOL = (1 << 10), /* protocol violation */
  380. SERR_INTERNAL = (1 << 11), /* host internal error */
  381. SERR_PHYRDY_CHG = (1 << 16), /* PHY RDY changed */
  382. SERR_PHY_INT_ERR = (1 << 17), /* PHY internal error */
  383. SERR_COMM_WAKE = (1 << 18), /* Comm wake */
  384. SERR_10B_8B_ERR = (1 << 19), /* 10b to 8b decode error */
  385. SERR_DISPARITY = (1 << 20), /* Disparity */
  386. SERR_CRC = (1 << 21), /* CRC error */
  387. SERR_HANDSHAKE = (1 << 22), /* Handshake error */
  388. SERR_LINK_SEQ_ERR = (1 << 23), /* Link sequence error */
  389. SERR_TRANS_ST_ERROR = (1 << 24), /* Transport state trans. error */
  390. SERR_UNRECOG_FIS = (1 << 25), /* Unrecognized FIS */
  391. SERR_DEV_XCHG = (1 << 26), /* device exchanged */
  392. /* struct ata_taskfile flags */
  393. ATA_TFLAG_LBA48 = (1 << 0), /* enable 48-bit LBA and "HOB" */
  394. ATA_TFLAG_ISADDR = (1 << 1), /* enable r/w to nsect/lba regs */
  395. ATA_TFLAG_DEVICE = (1 << 2), /* enable r/w to device reg */
  396. ATA_TFLAG_WRITE = (1 << 3), /* data dir: host->dev==1 (write) */
  397. ATA_TFLAG_LBA = (1 << 4), /* enable LBA */
  398. ATA_TFLAG_FUA = (1 << 5), /* enable FUA */
  399. ATA_TFLAG_POLLING = (1 << 6), /* set nIEN to 1 and use polling */
  400. /* protocol flags */
  401. ATA_PROT_FLAG_PIO = (1 << 0), /* is PIO */
  402. ATA_PROT_FLAG_DMA = (1 << 1), /* is DMA */
  403. ATA_PROT_FLAG_DATA = ATA_PROT_FLAG_PIO | ATA_PROT_FLAG_DMA,
  404. ATA_PROT_FLAG_NCQ = (1 << 2), /* is NCQ */
  405. ATA_PROT_FLAG_ATAPI = (1 << 3), /* is ATAPI */
  406. };
  407. enum ata_tf_protocols {
  408. /* ATA taskfile protocols */
  409. ATA_PROT_UNKNOWN, /* unknown/invalid */
  410. ATA_PROT_NODATA, /* no data */
  411. ATA_PROT_PIO, /* PIO data xfer */
  412. ATA_PROT_DMA, /* DMA */
  413. ATA_PROT_NCQ, /* NCQ */
  414. ATAPI_PROT_NODATA, /* packet command, no data */
  415. ATAPI_PROT_PIO, /* packet command, PIO data xfer*/
  416. ATAPI_PROT_DMA, /* packet command with special DMA sauce */
  417. };
  418. enum ata_ioctls {
  419. ATA_IOC_GET_IO32 = 0x309,
  420. ATA_IOC_SET_IO32 = 0x324,
  421. };
  422. /* core structures */
  423. struct ata_bmdma_prd {
  424. __le32 addr;
  425. __le32 flags_len;
  426. };
  427. struct ata_taskfile {
  428. unsigned long flags; /* ATA_TFLAG_xxx */
  429. u8 protocol; /* ATA_PROT_xxx */
  430. u8 ctl; /* control reg */
  431. u8 hob_feature; /* additional data */
  432. u8 hob_nsect; /* to support LBA48 */
  433. u8 hob_lbal;
  434. u8 hob_lbam;
  435. u8 hob_lbah;
  436. u8 feature;
  437. u8 nsect;
  438. u8 lbal;
  439. u8 lbam;
  440. u8 lbah;
  441. u8 device;
  442. u8 command; /* IO operation */
  443. };
  444. /*
  445. * protocol tests
  446. */
  447. static inline unsigned int ata_prot_flags(u8 prot)
  448. {
  449. switch (prot) {
  450. case ATA_PROT_NODATA:
  451. return 0;
  452. case ATA_PROT_PIO:
  453. return ATA_PROT_FLAG_PIO;
  454. case ATA_PROT_DMA:
  455. return ATA_PROT_FLAG_DMA;
  456. case ATA_PROT_NCQ:
  457. return ATA_PROT_FLAG_DMA | ATA_PROT_FLAG_NCQ;
  458. case ATAPI_PROT_NODATA:
  459. return ATA_PROT_FLAG_ATAPI;
  460. case ATAPI_PROT_PIO:
  461. return ATA_PROT_FLAG_ATAPI | ATA_PROT_FLAG_PIO;
  462. case ATAPI_PROT_DMA:
  463. return ATA_PROT_FLAG_ATAPI | ATA_PROT_FLAG_DMA;
  464. }
  465. return 0;
  466. }
  467. static inline int ata_is_atapi(u8 prot)
  468. {
  469. return ata_prot_flags(prot) & ATA_PROT_FLAG_ATAPI;
  470. }
  471. static inline int ata_is_nodata(u8 prot)
  472. {
  473. return !(ata_prot_flags(prot) & ATA_PROT_FLAG_DATA);
  474. }
  475. static inline int ata_is_pio(u8 prot)
  476. {
  477. return ata_prot_flags(prot) & ATA_PROT_FLAG_PIO;
  478. }
  479. static inline int ata_is_dma(u8 prot)
  480. {
  481. return ata_prot_flags(prot) & ATA_PROT_FLAG_DMA;
  482. }
  483. static inline int ata_is_ncq(u8 prot)
  484. {
  485. return ata_prot_flags(prot) & ATA_PROT_FLAG_NCQ;
  486. }
  487. static inline int ata_is_data(u8 prot)
  488. {
  489. return ata_prot_flags(prot) & ATA_PROT_FLAG_DATA;
  490. }
  491. /*
  492. * id tests
  493. */
  494. #define ata_id_is_ata(id) (((id)[ATA_ID_CONFIG] & (1 << 15)) == 0)
  495. #define ata_id_has_lba(id) ((id)[ATA_ID_CAPABILITY] & (1 << 9))
  496. #define ata_id_has_dma(id) ((id)[ATA_ID_CAPABILITY] & (1 << 8))
  497. #define ata_id_has_ncq(id) ((id)[76] & (1 << 8))
  498. #define ata_id_queue_depth(id) (((id)[ATA_ID_QUEUE_DEPTH] & 0x1f) + 1)
  499. #define ata_id_removeable(id) ((id)[ATA_ID_CONFIG] & (1 << 7))
  500. #define ata_id_has_atapi_AN(id) \
  501. ( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \
  502. ((id)[78] & (1 << 5)) )
  503. #define ata_id_has_fpdma_aa(id) \
  504. ( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \
  505. ((id)[78] & (1 << 2)) )
  506. #define ata_id_iordy_disable(id) ((id)[ATA_ID_CAPABILITY] & (1 << 10))
  507. #define ata_id_has_iordy(id) ((id)[ATA_ID_CAPABILITY] & (1 << 11))
  508. #define ata_id_u32(id,n) \
  509. (((u32) (id)[(n) + 1] << 16) | ((u32) (id)[(n)]))
  510. #define ata_id_u64(id,n) \
  511. ( ((u64) (id)[(n) + 3] << 48) | \
  512. ((u64) (id)[(n) + 2] << 32) | \
  513. ((u64) (id)[(n) + 1] << 16) | \
  514. ((u64) (id)[(n) + 0]) )
  515. #define ata_id_cdb_intr(id) (((id)[ATA_ID_CONFIG] & 0x60) == 0x20)
  516. static inline bool ata_id_has_hipm(const u16 *id)
  517. {
  518. u16 val = id[76];
  519. if (val == 0 || val == 0xffff)
  520. return false;
  521. return val & (1 << 9);
  522. }
  523. static inline bool ata_id_has_dipm(const u16 *id)
  524. {
  525. u16 val = id[78];
  526. if (val == 0 || val == 0xffff)
  527. return false;
  528. return val & (1 << 3);
  529. }
  530. static inline bool ata_id_has_fua(const u16 *id)
  531. {
  532. if ((id[ATA_ID_CFSSE] & 0xC000) != 0x4000)
  533. return false;
  534. return id[ATA_ID_CFSSE] & (1 << 6);
  535. }
  536. static inline bool ata_id_has_flush(const u16 *id)
  537. {
  538. if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
  539. return false;
  540. return id[ATA_ID_COMMAND_SET_2] & (1 << 12);
  541. }
  542. static inline bool ata_id_flush_enabled(const u16 *id)
  543. {
  544. if (ata_id_has_flush(id) == 0)
  545. return false;
  546. if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
  547. return false;
  548. return id[ATA_ID_CFS_ENABLE_2] & (1 << 12);
  549. }
  550. static inline bool ata_id_has_flush_ext(const u16 *id)
  551. {
  552. if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
  553. return false;
  554. return id[ATA_ID_COMMAND_SET_2] & (1 << 13);
  555. }
  556. static inline bool ata_id_flush_ext_enabled(const u16 *id)
  557. {
  558. if (ata_id_has_flush_ext(id) == 0)
  559. return false;
  560. if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
  561. return false;
  562. /*
  563. * some Maxtor disks have bit 13 defined incorrectly
  564. * so check bit 10 too
  565. */
  566. return (id[ATA_ID_CFS_ENABLE_2] & 0x2400) == 0x2400;
  567. }
  568. static inline u32 ata_id_logical_sector_size(const u16 *id)
  569. {
  570. /* T13/1699-D Revision 6a, Sep 6, 2008. Page 128.
  571. * IDENTIFY DEVICE data, word 117-118.
  572. * 0xd000 ignores bit 13 (logical:physical > 1)
  573. */
  574. if ((id[ATA_ID_SECTOR_SIZE] & 0xd000) == 0x5000)
  575. return (((id[ATA_ID_LOGICAL_SECTOR_SIZE+1] << 16)
  576. + id[ATA_ID_LOGICAL_SECTOR_SIZE]) * sizeof(u16)) ;
  577. return ATA_SECT_SIZE;
  578. }
  579. static inline u8 ata_id_log2_per_physical_sector(const u16 *id)
  580. {
  581. /* T13/1699-D Revision 6a, Sep 6, 2008. Page 128.
  582. * IDENTIFY DEVICE data, word 106.
  583. * 0xe000 ignores bit 12 (logical sector > 512 bytes)
  584. */
  585. if ((id[ATA_ID_SECTOR_SIZE] & 0xe000) == 0x6000)
  586. return (id[ATA_ID_SECTOR_SIZE] & 0xf);
  587. return 0;
  588. }
  589. /* Offset of logical sectors relative to physical sectors.
  590. *
  591. * If device has more than one logical sector per physical sector
  592. * (aka 512 byte emulation), vendors might offset the "sector 0" address
  593. * so sector 63 is "naturally aligned" - e.g. FAT partition table.
  594. * This avoids Read/Mod/Write penalties when using FAT partition table
  595. * and updating "well aligned" (FS perspective) physical sectors on every
  596. * transaction.
  597. */
  598. static inline u16 ata_id_logical_sector_offset(const u16 *id,
  599. u8 log2_per_phys)
  600. {
  601. u16 word_209 = id[209];
  602. if ((log2_per_phys > 1) && (word_209 & 0xc000) == 0x4000) {
  603. u16 first = word_209 & 0x3fff;
  604. if (first > 0)
  605. return (1 << log2_per_phys) - first;
  606. }
  607. return 0;
  608. }
  609. static inline bool ata_id_has_lba48(const u16 *id)
  610. {
  611. if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
  612. return false;
  613. if (!ata_id_u64(id, ATA_ID_LBA_CAPACITY_2))
  614. return false;
  615. return id[ATA_ID_COMMAND_SET_2] & (1 << 10);
  616. }
  617. static inline bool ata_id_lba48_enabled(const u16 *id)
  618. {
  619. if (ata_id_has_lba48(id) == 0)
  620. return false;
  621. if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
  622. return false;
  623. return id[ATA_ID_CFS_ENABLE_2] & (1 << 10);
  624. }
  625. static inline bool ata_id_hpa_enabled(const u16 *id)
  626. {
  627. /* Yes children, word 83 valid bits cover word 82 data */
  628. if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
  629. return false;
  630. /* And 87 covers 85-87 */
  631. if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
  632. return false;
  633. /* Check command sets enabled as well as supported */
  634. if ((id[ATA_ID_CFS_ENABLE_1] & (1 << 10)) == 0)
  635. return false;
  636. return id[ATA_ID_COMMAND_SET_1] & (1 << 10);
  637. }
  638. static inline bool ata_id_has_wcache(const u16 *id)
  639. {
  640. /* Yes children, word 83 valid bits cover word 82 data */
  641. if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
  642. return false;
  643. return id[ATA_ID_COMMAND_SET_1] & (1 << 5);
  644. }
  645. static inline bool ata_id_has_pm(const u16 *id)
  646. {
  647. if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
  648. return false;
  649. return id[ATA_ID_COMMAND_SET_1] & (1 << 3);
  650. }
  651. static inline bool ata_id_rahead_enabled(const u16 *id)
  652. {
  653. if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
  654. return false;
  655. return id[ATA_ID_CFS_ENABLE_1] & (1 << 6);
  656. }
  657. static inline bool ata_id_wcache_enabled(const u16 *id)
  658. {
  659. if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
  660. return false;
  661. return id[ATA_ID_CFS_ENABLE_1] & (1 << 5);
  662. }
  663. /**
  664. * ata_id_major_version - get ATA level of drive
  665. * @id: Identify data
  666. *
  667. * Caveats:
  668. * ATA-1 considers identify optional
  669. * ATA-2 introduces mandatory identify
  670. * ATA-3 introduces word 80 and accurate reporting
  671. *
  672. * The practical impact of this is that ata_id_major_version cannot
  673. * reliably report on drives below ATA3.
  674. */
  675. static inline unsigned int ata_id_major_version(const u16 *id)
  676. {
  677. unsigned int mver;
  678. if (id[ATA_ID_MAJOR_VER] == 0xFFFF)
  679. return 0;
  680. for (mver = 14; mver >= 1; mver--)
  681. if (id[ATA_ID_MAJOR_VER] & (1 << mver))
  682. break;
  683. return mver;
  684. }
  685. static inline bool ata_id_is_sata(const u16 *id)
  686. {
  687. /*
  688. * See if word 93 is 0 AND drive is at least ATA-5 compatible
  689. * verifying that word 80 by casting it to a signed type --
  690. * this trick allows us to filter out the reserved values of
  691. * 0x0000 and 0xffff along with the earlier ATA revisions...
  692. */
  693. if (id[ATA_ID_HW_CONFIG] == 0 && (short)id[ATA_ID_MAJOR_VER] >= 0x0020)
  694. return true;
  695. return false;
  696. }
  697. static inline bool ata_id_has_tpm(const u16 *id)
  698. {
  699. /* The TPM bits are only valid on ATA8 */
  700. if (ata_id_major_version(id) < 8)
  701. return false;
  702. if ((id[48] & 0xC000) != 0x4000)
  703. return false;
  704. return id[48] & (1 << 0);
  705. }
  706. static inline bool ata_id_has_dword_io(const u16 *id)
  707. {
  708. /* ATA 8 reuses this flag for "trusted" computing */
  709. if (ata_id_major_version(id) > 7)
  710. return false;
  711. return id[ATA_ID_DWORD_IO] & (1 << 0);
  712. }
  713. static inline bool ata_id_has_unload(const u16 *id)
  714. {
  715. if (ata_id_major_version(id) >= 7 &&
  716. (id[ATA_ID_CFSSE] & 0xC000) == 0x4000 &&
  717. id[ATA_ID_CFSSE] & (1 << 13))
  718. return true;
  719. return false;
  720. }
  721. static inline bool ata_id_has_wwn(const u16 *id)
  722. {
  723. return (id[ATA_ID_CSF_DEFAULT] & 0xC100) == 0x4100;
  724. }
  725. static inline int ata_id_form_factor(const u16 *id)
  726. {
  727. u16 val = id[168];
  728. if (ata_id_major_version(id) < 7 || val == 0 || val == 0xffff)
  729. return 0;
  730. val &= 0xf;
  731. if (val > 5)
  732. return 0;
  733. return val;
  734. }
  735. static inline int ata_id_rotation_rate(const u16 *id)
  736. {
  737. u16 val = id[217];
  738. if (ata_id_major_version(id) < 7 || val == 0 || val == 0xffff)
  739. return 0;
  740. if (val > 1 && val < 0x401)
  741. return 0;
  742. return val;
  743. }
  744. static inline bool ata_id_has_trim(const u16 *id)
  745. {
  746. if (ata_id_major_version(id) >= 7 &&
  747. (id[ATA_ID_DATA_SET_MGMT] & 1))
  748. return true;
  749. return false;
  750. }
  751. static inline bool ata_id_has_zero_after_trim(const u16 *id)
  752. {
  753. /* DSM supported, deterministic read, and read zero after trim set */
  754. if (ata_id_has_trim(id) &&
  755. (id[ATA_ID_ADDITIONAL_SUPP] & 0x4020) == 0x4020)
  756. return true;
  757. return false;
  758. }
  759. static inline bool ata_id_current_chs_valid(const u16 *id)
  760. {
  761. /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command
  762. has not been issued to the device then the values of
  763. id[ATA_ID_CUR_CYLS] to id[ATA_ID_CUR_SECTORS] are vendor specific. */
  764. return (id[ATA_ID_FIELD_VALID] & 1) && /* Current translation valid */
  765. id[ATA_ID_CUR_CYLS] && /* cylinders in current translation */
  766. id[ATA_ID_CUR_HEADS] && /* heads in current translation */
  767. id[ATA_ID_CUR_HEADS] <= 16 &&
  768. id[ATA_ID_CUR_SECTORS]; /* sectors in current translation */
  769. }
  770. static inline bool ata_id_is_cfa(const u16 *id)
  771. {
  772. if ((id[ATA_ID_CONFIG] == 0x848A) || /* Traditional CF */
  773. (id[ATA_ID_CONFIG] == 0x844A)) /* Delkin Devices CF */
  774. return true;
  775. /*
  776. * CF specs don't require specific value in the word 0 anymore and yet
  777. * they forbid to report the ATA version in the word 80 and require the
  778. * CFA feature set support to be indicated in the word 83 in this case.
  779. * Unfortunately, some cards only follow either of this requirements,
  780. * and while those that don't indicate CFA feature support need some
  781. * sort of quirk list, it seems impractical for the ones that do...
  782. */
  783. return (id[ATA_ID_COMMAND_SET_2] & 0xC004) == 0x4004;
  784. }
  785. static inline bool ata_id_is_ssd(const u16 *id)
  786. {
  787. return id[ATA_ID_ROT_SPEED] == 0x01;
  788. }
  789. static inline bool ata_id_pio_need_iordy(const u16 *id, const u8 pio)
  790. {
  791. /* CF spec. r4.1 Table 22 says no IORDY on PIO5 and PIO6. */
  792. if (pio > 4 && ata_id_is_cfa(id))
  793. return false;
  794. /* For PIO3 and higher it is mandatory. */
  795. if (pio > 2)
  796. return true;
  797. /* Turn it on when possible. */
  798. return ata_id_has_iordy(id);
  799. }
  800. static inline bool ata_drive_40wire(const u16 *dev_id)
  801. {
  802. if (ata_id_is_sata(dev_id))
  803. return false; /* SATA */
  804. if ((dev_id[ATA_ID_HW_CONFIG] & 0xE000) == 0x6000)
  805. return false; /* 80 wire */
  806. return true;
  807. }
  808. static inline bool ata_drive_40wire_relaxed(const u16 *dev_id)
  809. {
  810. if ((dev_id[ATA_ID_HW_CONFIG] & 0x2000) == 0x2000)
  811. return false; /* 80 wire */
  812. return true;
  813. }
  814. static inline int atapi_cdb_len(const u16 *dev_id)
  815. {
  816. u16 tmp = dev_id[ATA_ID_CONFIG] & 0x3;
  817. switch (tmp) {
  818. case 0: return 12;
  819. case 1: return 16;
  820. default: return -1;
  821. }
  822. }
  823. static inline int atapi_command_packet_set(const u16 *dev_id)
  824. {
  825. return (dev_id[ATA_ID_CONFIG] >> 8) & 0x1f;
  826. }
  827. static inline bool atapi_id_dmadir(const u16 *dev_id)
  828. {
  829. return ata_id_major_version(dev_id) >= 7 && (dev_id[62] & 0x8000);
  830. }
  831. /*
  832. * ata_id_is_lba_capacity_ok() performs a sanity check on
  833. * the claimed LBA capacity value for the device.
  834. *
  835. * Returns 1 if LBA capacity looks sensible, 0 otherwise.
  836. *
  837. * It is called only once for each device.
  838. */
  839. static inline bool ata_id_is_lba_capacity_ok(u16 *id)
  840. {
  841. unsigned long lba_sects, chs_sects, head, tail;
  842. /* No non-LBA info .. so valid! */
  843. if (id[ATA_ID_CYLS] == 0)
  844. return true;
  845. lba_sects = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
  846. /*
  847. * The ATA spec tells large drives to return
  848. * C/H/S = 16383/16/63 independent of their size.
  849. * Some drives can be jumpered to use 15 heads instead of 16.
  850. * Some drives can be jumpered to use 4092 cyls instead of 16383.
  851. */
  852. if ((id[ATA_ID_CYLS] == 16383 ||
  853. (id[ATA_ID_CYLS] == 4092 && id[ATA_ID_CUR_CYLS] == 16383)) &&
  854. id[ATA_ID_SECTORS] == 63 &&
  855. (id[ATA_ID_HEADS] == 15 || id[ATA_ID_HEADS] == 16) &&
  856. (lba_sects >= 16383 * 63 * id[ATA_ID_HEADS]))
  857. return true;
  858. chs_sects = id[ATA_ID_CYLS] * id[ATA_ID_HEADS] * id[ATA_ID_SECTORS];
  859. /* perform a rough sanity check on lba_sects: within 10% is OK */
  860. if (lba_sects - chs_sects < chs_sects/10)
  861. return true;
  862. /* some drives have the word order reversed */
  863. head = (lba_sects >> 16) & 0xffff;
  864. tail = lba_sects & 0xffff;
  865. lba_sects = head | (tail << 16);
  866. if (lba_sects - chs_sects < chs_sects/10) {
  867. *(__le32 *)&id[ATA_ID_LBA_CAPACITY] = __cpu_to_le32(lba_sects);
  868. return true; /* LBA capacity is (now) good */
  869. }
  870. return false; /* LBA capacity value may be bad */
  871. }
  872. static inline void ata_id_to_hd_driveid(u16 *id)
  873. {
  874. #ifdef __BIG_ENDIAN
  875. /* accessed in struct hd_driveid as 8-bit values */
  876. id[ATA_ID_MAX_MULTSECT] = __cpu_to_le16(id[ATA_ID_MAX_MULTSECT]);
  877. id[ATA_ID_CAPABILITY] = __cpu_to_le16(id[ATA_ID_CAPABILITY]);
  878. id[ATA_ID_OLD_PIO_MODES] = __cpu_to_le16(id[ATA_ID_OLD_PIO_MODES]);
  879. id[ATA_ID_OLD_DMA_MODES] = __cpu_to_le16(id[ATA_ID_OLD_DMA_MODES]);
  880. id[ATA_ID_MULTSECT] = __cpu_to_le16(id[ATA_ID_MULTSECT]);
  881. /* as 32-bit values */
  882. *(u32 *)&id[ATA_ID_LBA_CAPACITY] = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
  883. *(u32 *)&id[ATA_ID_SPG] = ata_id_u32(id, ATA_ID_SPG);
  884. /* as 64-bit value */
  885. *(u64 *)&id[ATA_ID_LBA_CAPACITY_2] =
  886. ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
  887. #endif
  888. }
  889. /*
  890. * Write LBA Range Entries to the buffer that will cover the extent from
  891. * sector to sector + count. This is used for TRIM and for ADD LBA(S)
  892. * TO NV CACHE PINNED SET.
  893. */
  894. static inline unsigned ata_set_lba_range_entries(void *_buffer,
  895. unsigned buf_size, u64 sector, unsigned long count)
  896. {
  897. __le64 *buffer = _buffer;
  898. unsigned i = 0, used_bytes;
  899. while (i < buf_size / 8 ) { /* 6-byte LBA + 2-byte range per entry */
  900. u64 entry = sector |
  901. ((u64)(count > 0xffff ? 0xffff : count) << 48);
  902. buffer[i++] = __cpu_to_le64(entry);
  903. if (count <= 0xffff)
  904. break;
  905. count -= 0xffff;
  906. sector += 0xffff;
  907. }
  908. used_bytes = ALIGN(i * 8, 512);
  909. memset(buffer + i, 0, used_bytes - i * 8);
  910. return used_bytes;
  911. }
  912. static inline int is_multi_taskfile(struct ata_taskfile *tf)
  913. {
  914. return (tf->command == ATA_CMD_READ_MULTI) ||
  915. (tf->command == ATA_CMD_WRITE_MULTI) ||
  916. (tf->command == ATA_CMD_READ_MULTI_EXT) ||
  917. (tf->command == ATA_CMD_WRITE_MULTI_EXT) ||
  918. (tf->command == ATA_CMD_WRITE_MULTI_FUA_EXT);
  919. }
  920. static inline bool ata_ok(u8 status)
  921. {
  922. return ((status & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ | ATA_ERR))
  923. == ATA_DRDY);
  924. }
  925. static inline bool lba_28_ok(u64 block, u32 n_block)
  926. {
  927. /* check the ending block number: must be LESS THAN 0x0fffffff */
  928. return ((block + n_block) < ((1 << 28) - 1)) && (n_block <= 256);
  929. }
  930. static inline bool lba_48_ok(u64 block, u32 n_block)
  931. {
  932. /* check the ending block number */
  933. return ((block + n_block - 1) < ((u64)1 << 48)) && (n_block <= 65536);
  934. }
  935. #define sata_pmp_gscr_vendor(gscr) ((gscr)[SATA_PMP_GSCR_PROD_ID] & 0xffff)
  936. #define sata_pmp_gscr_devid(gscr) ((gscr)[SATA_PMP_GSCR_PROD_ID] >> 16)
  937. #define sata_pmp_gscr_rev(gscr) (((gscr)[SATA_PMP_GSCR_REV] >> 8) & 0xff)
  938. #define sata_pmp_gscr_ports(gscr) ((gscr)[SATA_PMP_GSCR_PORT_INFO] & 0xf)
  939. #endif /* __LINUX_ATA_H__ */