libata-acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * libata-acpi.c
  3. * Provides ACPI support for PATA/SATA.
  4. *
  5. * Copyright (C) 2006 Intel Corp.
  6. * Copyright (C) 2006 Randy Dunlap
  7. */
  8. #include <linux/ata.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/acpi.h>
  14. #include <linux/libata.h>
  15. #include <linux/pci.h>
  16. #include "libata.h"
  17. #include <acpi/acpi_bus.h>
  18. #include <acpi/acnames.h>
  19. #include <acpi/acnamesp.h>
  20. #include <acpi/acparser.h>
  21. #include <acpi/acexcep.h>
  22. #include <acpi/acmacros.h>
  23. #include <acpi/actypes.h>
  24. #define NO_PORT_MULT 0xffff
  25. #define SATA_ADR(root,pmp) (((root) << 16) | (pmp))
  26. #define REGS_PER_GTF 7
  27. struct ata_acpi_gtf {
  28. u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
  29. } __packed;
  30. /*
  31. * Helper - belongs in the PCI layer somewhere eventually
  32. */
  33. static int is_pci_dev(struct device *dev)
  34. {
  35. return (dev->bus == &pci_bus_type);
  36. }
  37. static void ata_acpi_associate_sata_port(struct ata_port *ap)
  38. {
  39. acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
  40. ap->device->acpi_handle = acpi_get_child(ap->host->acpi_handle, adr);
  41. }
  42. static void ata_acpi_associate_ide_port(struct ata_port *ap)
  43. {
  44. int max_devices, i;
  45. ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
  46. if (!ap->acpi_handle)
  47. return;
  48. max_devices = 1;
  49. if (ap->flags & ATA_FLAG_SLAVE_POSS)
  50. max_devices++;
  51. for (i = 0; i < max_devices; i++) {
  52. struct ata_device *dev = &ap->device[i];
  53. dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
  54. }
  55. }
  56. /**
  57. * ata_acpi_associate - associate ATA host with ACPI objects
  58. * @host: target ATA host
  59. *
  60. * Look up ACPI objects associated with @host and initialize
  61. * acpi_handle fields of @host, its ports and devices accordingly.
  62. *
  63. * LOCKING:
  64. * EH context.
  65. *
  66. * RETURNS:
  67. * 0 on success, -errno on failure.
  68. */
  69. void ata_acpi_associate(struct ata_host *host)
  70. {
  71. int i;
  72. if (!is_pci_dev(host->dev) || libata_noacpi)
  73. return;
  74. host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
  75. if (!host->acpi_handle)
  76. return;
  77. for (i = 0; i < host->n_ports; i++) {
  78. struct ata_port *ap = host->ports[i];
  79. if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
  80. ata_acpi_associate_sata_port(ap);
  81. else
  82. ata_acpi_associate_ide_port(ap);
  83. }
  84. }
  85. /**
  86. * ata_dev_get_GTF - get the drive bootup default taskfile settings
  87. * @dev: target ATA device
  88. * @gtf: output parameter for buffer containing _GTF taskfile arrays
  89. * @ptr_to_free: pointer which should be freed
  90. *
  91. * This applies to both PATA and SATA drives.
  92. *
  93. * The _GTF method has no input parameters.
  94. * It returns a variable number of register set values (registers
  95. * hex 1F1..1F7, taskfiles).
  96. * The <variable number> is not known in advance, so have ACPI-CA
  97. * allocate the buffer as needed and return it, then free it later.
  98. *
  99. * LOCKING:
  100. * EH context.
  101. *
  102. * RETURNS:
  103. * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
  104. * contain valid data. -errno on other errors.
  105. */
  106. static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
  107. void **ptr_to_free)
  108. {
  109. struct ata_port *ap = dev->ap;
  110. acpi_status status;
  111. struct acpi_buffer output;
  112. union acpi_object *out_obj;
  113. int rc = 0;
  114. /* set up output buffer */
  115. output.length = ACPI_ALLOCATE_BUFFER;
  116. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  117. if (!dev->acpi_handle)
  118. goto out_free;
  119. if (ata_msg_probe(ap))
  120. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  121. __FUNCTION__, ap->port_no);
  122. if (!ata_dev_enabled(dev) || (ap->flags & ATA_FLAG_DISABLED)) {
  123. if (ata_msg_probe(ap))
  124. ata_dev_printk(dev, KERN_DEBUG, "%s: ERR: "
  125. "ata_dev_present: %d, PORT_DISABLED: %lu\n",
  126. __FUNCTION__, ata_dev_enabled(dev),
  127. ap->flags & ATA_FLAG_DISABLED);
  128. goto out_free;
  129. }
  130. /* _GTF has no input parameters */
  131. status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
  132. if (ACPI_FAILURE(status)) {
  133. if (status != AE_NOT_FOUND) {
  134. ata_dev_printk(dev, KERN_WARNING,
  135. "_GTF evaluation failed (AE 0x%x)\n",
  136. status);
  137. rc = -EIO;
  138. }
  139. goto out_free;
  140. }
  141. if (!output.length || !output.pointer) {
  142. if (ata_msg_probe(ap))
  143. ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
  144. "length or ptr is NULL (0x%llx, 0x%p)\n",
  145. __FUNCTION__,
  146. (unsigned long long)output.length,
  147. output.pointer);
  148. goto out_free;
  149. }
  150. out_obj = output.pointer;
  151. if (out_obj->type != ACPI_TYPE_BUFFER) {
  152. ata_dev_printk(dev, KERN_WARNING,
  153. "_GTF unexpected object type 0x%x\n",
  154. out_obj->type);
  155. rc = -EINVAL;
  156. goto out_free;
  157. }
  158. if (out_obj->buffer.length % REGS_PER_GTF) {
  159. ata_dev_printk(dev, KERN_WARNING,
  160. "unexpected _GTF length (%d)\n",
  161. out_obj->buffer.length);
  162. rc = -EINVAL;
  163. goto out_free;
  164. }
  165. *ptr_to_free = out_obj;
  166. *gtf = (void *)out_obj->buffer.pointer;
  167. rc = out_obj->buffer.length / REGS_PER_GTF;
  168. if (ata_msg_probe(ap))
  169. ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
  170. "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
  171. __FUNCTION__, *gtf, rc, *ptr_to_free);
  172. return rc;
  173. out_free:
  174. kfree(output.pointer);
  175. return rc;
  176. }
  177. /**
  178. * taskfile_load_raw - send taskfile registers to host controller
  179. * @dev: target ATA device
  180. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  181. *
  182. * Outputs ATA taskfile to standard ATA host controller using MMIO
  183. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  184. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  185. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  186. * hob_lbal, hob_lbam, and hob_lbah.
  187. *
  188. * This function waits for idle (!BUSY and !DRQ) after writing
  189. * registers. If the control register has a new value, this
  190. * function also waits for idle after writing control and before
  191. * writing the remaining registers.
  192. *
  193. * LOCKING:
  194. * EH context.
  195. *
  196. * RETURNS:
  197. * 0 on success, -errno on failure.
  198. */
  199. static int taskfile_load_raw(struct ata_device *dev,
  200. const struct ata_acpi_gtf *gtf)
  201. {
  202. struct ata_port *ap = dev->ap;
  203. struct ata_taskfile tf, rtf;
  204. unsigned int err_mask;
  205. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  206. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  207. && (gtf->tf[6] == 0))
  208. return 0;
  209. ata_tf_init(dev, &tf);
  210. /* convert gtf to tf */
  211. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
  212. tf.protocol = ATA_PROT_NODATA;
  213. tf.feature = gtf->tf[0]; /* 0x1f1 */
  214. tf.nsect = gtf->tf[1]; /* 0x1f2 */
  215. tf.lbal = gtf->tf[2]; /* 0x1f3 */
  216. tf.lbam = gtf->tf[3]; /* 0x1f4 */
  217. tf.lbah = gtf->tf[4]; /* 0x1f5 */
  218. tf.device = gtf->tf[5]; /* 0x1f6 */
  219. tf.command = gtf->tf[6]; /* 0x1f7 */
  220. if (ata_msg_probe(ap))
  221. ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
  222. "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
  223. tf.command, tf.feature, tf.nsect,
  224. tf.lbal, tf.lbam, tf.lbah, tf.device);
  225. rtf = tf;
  226. err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
  227. if (err_mask) {
  228. ata_dev_printk(dev, KERN_ERR,
  229. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
  230. "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
  231. tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
  232. tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
  233. return -EIO;
  234. }
  235. return 0;
  236. }
  237. /**
  238. * ata_dev_set_taskfiles - write the drive taskfile settings from _GTF
  239. * @dev: target ATA device
  240. * @gtf: pointer to array of _GTF taskfiles to execute
  241. * @gtf_count: number of taskfiles
  242. *
  243. * This applies to both PATA and SATA drives.
  244. *
  245. * Execute taskfiles in @gtf.
  246. *
  247. * LOCKING:
  248. * EH context.
  249. *
  250. * RETURNS:
  251. * 0 on success, -errno on failure.
  252. */
  253. static int ata_dev_set_taskfiles(struct ata_device *dev,
  254. struct ata_acpi_gtf *gtf, int gtf_count)
  255. {
  256. struct ata_port *ap = dev->ap;
  257. int ix;
  258. if (ata_msg_probe(ap))
  259. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  260. __FUNCTION__, ap->port_no);
  261. if (!(ap->flags & ATA_FLAG_ACPI_SATA))
  262. return 0;
  263. if (!ata_dev_enabled(dev) || (ap->flags & ATA_FLAG_DISABLED))
  264. return -ENODEV;
  265. /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
  266. for (ix = 0; ix < gtf_count; ix++)
  267. taskfile_load_raw(dev, gtf++);
  268. return 0;
  269. }
  270. /**
  271. * ata_acpi_exec_tfs - get then write drive taskfile settings
  272. * @ap: the ata_port for the drive
  273. *
  274. * This applies to both PATA and SATA drives.
  275. *
  276. * LOCKING:
  277. * EH context.
  278. *
  279. * RETURNS:
  280. * 0 on success, -errno on failure.
  281. */
  282. int ata_acpi_exec_tfs(struct ata_port *ap)
  283. {
  284. int ix, ret = 0;
  285. /*
  286. * TBD - implement PATA support. For now,
  287. * we should not run GTF on PATA devices since some
  288. * PATA require execution of GTM/STM before GTF.
  289. */
  290. if (!(ap->flags & ATA_FLAG_ACPI_SATA))
  291. return 0;
  292. for (ix = 0; ix < ATA_MAX_DEVICES; ix++) {
  293. struct ata_device *dev = &ap->device[ix];
  294. struct ata_acpi_gtf *gtf = NULL;
  295. int gtf_count;
  296. void *ptr_to_free = NULL;
  297. if (!ata_dev_enabled(dev))
  298. continue;
  299. ret = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
  300. if (ret == 0)
  301. continue;
  302. if (ret < 0)
  303. break;
  304. gtf_count = ret;
  305. ret = ata_dev_set_taskfiles(dev, gtf, gtf_count);
  306. kfree(ptr_to_free);
  307. if (ret < 0)
  308. break;
  309. }
  310. return ret;
  311. }
  312. /**
  313. * ata_acpi_push_id - send Identify data to drive
  314. * @dev: target ATA device
  315. *
  316. * _SDD ACPI object: for SATA mode only
  317. * Must be after Identify (Packet) Device -- uses its data
  318. * ATM this function never returns a failure. It is an optional
  319. * method and if it fails for whatever reason, we should still
  320. * just keep going.
  321. *
  322. * LOCKING:
  323. * EH context.
  324. *
  325. * RETURNS:
  326. * 0 on success, -errno on failure.
  327. */
  328. int ata_acpi_push_id(struct ata_device *dev)
  329. {
  330. struct ata_port *ap = dev->ap;
  331. int err;
  332. acpi_status status;
  333. struct acpi_object_list input;
  334. union acpi_object in_params[1];
  335. if (!dev->acpi_handle)
  336. return 0;
  337. if (ata_msg_probe(ap))
  338. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  339. __FUNCTION__, dev->devno, ap->port_no);
  340. /* Don't continue if not a SATA device. */
  341. if (!(ap->flags & ATA_FLAG_ACPI_SATA)) {
  342. if (ata_msg_probe(ap))
  343. ata_dev_printk(dev, KERN_DEBUG,
  344. "%s: Not a SATA device\n", __FUNCTION__);
  345. goto out;
  346. }
  347. /* Give the drive Identify data to the drive via the _SDD method */
  348. /* _SDD: set up input parameters */
  349. input.count = 1;
  350. input.pointer = in_params;
  351. in_params[0].type = ACPI_TYPE_BUFFER;
  352. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  353. in_params[0].buffer.pointer = (u8 *)dev->id;
  354. /* Output buffer: _SDD has no output */
  355. /* It's OK for _SDD to be missing too. */
  356. swap_buf_le16(dev->id, ATA_ID_WORDS);
  357. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  358. swap_buf_le16(dev->id, ATA_ID_WORDS);
  359. err = ACPI_FAILURE(status) ? -EIO : 0;
  360. if (err < 0)
  361. ata_dev_printk(dev, KERN_WARNING,
  362. "ACPI _SDD failed (AE 0x%x)\n", status);
  363. /* always return success */
  364. out:
  365. return 0;
  366. }