libata-acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 (ata_msg_probe(ap))
  118. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  119. __FUNCTION__, ap->port_no);
  120. /* _GTF has no input parameters */
  121. status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
  122. if (ACPI_FAILURE(status)) {
  123. if (status != AE_NOT_FOUND) {
  124. ata_dev_printk(dev, KERN_WARNING,
  125. "_GTF evaluation failed (AE 0x%x)\n",
  126. status);
  127. rc = -EIO;
  128. }
  129. goto out_free;
  130. }
  131. if (!output.length || !output.pointer) {
  132. if (ata_msg_probe(ap))
  133. ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
  134. "length or ptr is NULL (0x%llx, 0x%p)\n",
  135. __FUNCTION__,
  136. (unsigned long long)output.length,
  137. output.pointer);
  138. goto out_free;
  139. }
  140. out_obj = output.pointer;
  141. if (out_obj->type != ACPI_TYPE_BUFFER) {
  142. ata_dev_printk(dev, KERN_WARNING,
  143. "_GTF unexpected object type 0x%x\n",
  144. out_obj->type);
  145. rc = -EINVAL;
  146. goto out_free;
  147. }
  148. if (out_obj->buffer.length % REGS_PER_GTF) {
  149. ata_dev_printk(dev, KERN_WARNING,
  150. "unexpected _GTF length (%d)\n",
  151. out_obj->buffer.length);
  152. rc = -EINVAL;
  153. goto out_free;
  154. }
  155. *ptr_to_free = out_obj;
  156. *gtf = (void *)out_obj->buffer.pointer;
  157. rc = out_obj->buffer.length / REGS_PER_GTF;
  158. if (ata_msg_probe(ap))
  159. ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
  160. "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
  161. __FUNCTION__, *gtf, rc, *ptr_to_free);
  162. return rc;
  163. out_free:
  164. kfree(output.pointer);
  165. return rc;
  166. }
  167. /**
  168. * taskfile_load_raw - send taskfile registers to host controller
  169. * @dev: target ATA device
  170. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  171. *
  172. * Outputs ATA taskfile to standard ATA host controller using MMIO
  173. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  174. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  175. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  176. * hob_lbal, hob_lbam, and hob_lbah.
  177. *
  178. * This function waits for idle (!BUSY and !DRQ) after writing
  179. * registers. If the control register has a new value, this
  180. * function also waits for idle after writing control and before
  181. * writing the remaining registers.
  182. *
  183. * LOCKING:
  184. * EH context.
  185. *
  186. * RETURNS:
  187. * 0 on success, -errno on failure.
  188. */
  189. static int taskfile_load_raw(struct ata_device *dev,
  190. const struct ata_acpi_gtf *gtf)
  191. {
  192. struct ata_port *ap = dev->ap;
  193. struct ata_taskfile tf, rtf;
  194. unsigned int err_mask;
  195. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  196. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  197. && (gtf->tf[6] == 0))
  198. return 0;
  199. ata_tf_init(dev, &tf);
  200. /* convert gtf to tf */
  201. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
  202. tf.protocol = ATA_PROT_NODATA;
  203. tf.feature = gtf->tf[0]; /* 0x1f1 */
  204. tf.nsect = gtf->tf[1]; /* 0x1f2 */
  205. tf.lbal = gtf->tf[2]; /* 0x1f3 */
  206. tf.lbam = gtf->tf[3]; /* 0x1f4 */
  207. tf.lbah = gtf->tf[4]; /* 0x1f5 */
  208. tf.device = gtf->tf[5]; /* 0x1f6 */
  209. tf.command = gtf->tf[6]; /* 0x1f7 */
  210. if (ata_msg_probe(ap))
  211. ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
  212. "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
  213. tf.command, tf.feature, tf.nsect,
  214. tf.lbal, tf.lbam, tf.lbah, tf.device);
  215. rtf = tf;
  216. err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
  217. if (err_mask) {
  218. ata_dev_printk(dev, KERN_ERR,
  219. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
  220. "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
  221. tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
  222. tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
  223. return -EIO;
  224. }
  225. return 0;
  226. }
  227. /**
  228. * ata_acpi_exec_tfs - get then write drive taskfile settings
  229. * @dev: target ATA device
  230. *
  231. * Evaluate _GTF and excute returned taskfiles.
  232. *
  233. * LOCKING:
  234. * EH context.
  235. *
  236. * RETURNS:
  237. * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
  238. * doesn't contain valid data. -errno on other errors.
  239. */
  240. static int ata_acpi_exec_tfs(struct ata_device *dev)
  241. {
  242. struct ata_acpi_gtf *gtf = NULL;
  243. void *ptr_to_free = NULL;
  244. int gtf_count, i, rc;
  245. /* get taskfiles */
  246. rc = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
  247. if (rc < 0)
  248. return rc;
  249. gtf_count = rc;
  250. /* execute them */
  251. for (i = 0, rc = 0; i < gtf_count; i++) {
  252. int tmp;
  253. /* ACPI errors are eventually ignored. Run till the
  254. * end even after errors.
  255. */
  256. tmp = taskfile_load_raw(dev, gtf++);
  257. if (!rc)
  258. rc = tmp;
  259. }
  260. kfree(ptr_to_free);
  261. if (rc == 0)
  262. return gtf_count;
  263. return rc;
  264. }
  265. /**
  266. * ata_acpi_push_id - send Identify data to drive
  267. * @dev: target ATA device
  268. *
  269. * _SDD ACPI object: for SATA mode only
  270. * Must be after Identify (Packet) Device -- uses its data
  271. * ATM this function never returns a failure. It is an optional
  272. * method and if it fails for whatever reason, we should still
  273. * just keep going.
  274. *
  275. * LOCKING:
  276. * EH context.
  277. *
  278. * RETURNS:
  279. * 0 on success, -errno on failure.
  280. */
  281. static int ata_acpi_push_id(struct ata_device *dev)
  282. {
  283. struct ata_port *ap = dev->ap;
  284. int err;
  285. acpi_status status;
  286. struct acpi_object_list input;
  287. union acpi_object in_params[1];
  288. if (ata_msg_probe(ap))
  289. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  290. __FUNCTION__, dev->devno, ap->port_no);
  291. /* Give the drive Identify data to the drive via the _SDD method */
  292. /* _SDD: set up input parameters */
  293. input.count = 1;
  294. input.pointer = in_params;
  295. in_params[0].type = ACPI_TYPE_BUFFER;
  296. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  297. in_params[0].buffer.pointer = (u8 *)dev->id;
  298. /* Output buffer: _SDD has no output */
  299. /* It's OK for _SDD to be missing too. */
  300. swap_buf_le16(dev->id, ATA_ID_WORDS);
  301. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  302. swap_buf_le16(dev->id, ATA_ID_WORDS);
  303. err = ACPI_FAILURE(status) ? -EIO : 0;
  304. if (err < 0)
  305. ata_dev_printk(dev, KERN_WARNING,
  306. "ACPI _SDD failed (AE 0x%x)\n", status);
  307. return err;
  308. }
  309. /**
  310. * ata_acpi_on_resume - ATA ACPI hook called on resume
  311. * @ap: target ATA port
  312. *
  313. * This function is called when @ap is resumed - right after port
  314. * itself is resumed but before any EH action is taken.
  315. *
  316. * LOCKING:
  317. * EH context.
  318. */
  319. void ata_acpi_on_resume(struct ata_port *ap)
  320. {
  321. int i;
  322. /* schedule _GTF */
  323. for (i = 0; i < ATA_MAX_DEVICES; i++)
  324. ap->device[i].flags |= ATA_DFLAG_ACPI_PENDING;
  325. }
  326. /**
  327. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  328. * @dev: target ATA device
  329. *
  330. * This function is called when @dev is about to be configured.
  331. * IDENTIFY data might have been modified after this hook is run.
  332. *
  333. * LOCKING:
  334. * EH context.
  335. *
  336. * RETURNS:
  337. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  338. * -errno on failure.
  339. */
  340. int ata_acpi_on_devcfg(struct ata_device *dev)
  341. {
  342. struct ata_port *ap = dev->ap;
  343. struct ata_eh_context *ehc = &ap->eh_context;
  344. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  345. int rc;
  346. /* XXX: _STM isn't implemented yet, skip if IDE for now */
  347. if (!acpi_sata)
  348. return 0;
  349. if (!dev->acpi_handle)
  350. return 0;
  351. /* do we need to do _GTF? */
  352. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  353. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  354. return 0;
  355. /* do _SDD if SATA */
  356. if (acpi_sata) {
  357. rc = ata_acpi_push_id(dev);
  358. if (rc)
  359. goto acpi_err;
  360. }
  361. /* do _GTF */
  362. rc = ata_acpi_exec_tfs(dev);
  363. if (rc < 0)
  364. goto acpi_err;
  365. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  366. /* refresh IDENTIFY page if any _GTF command has been executed */
  367. if (rc > 0) {
  368. rc = ata_dev_reread_id(dev, 0);
  369. if (rc < 0) {
  370. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  371. "after ACPI commands\n");
  372. return rc;
  373. }
  374. }
  375. return 0;
  376. acpi_err:
  377. /* let EH retry on the first failure, disable ACPI on the second */
  378. if (dev->flags & ATA_DFLAG_ACPI_FAILED) {
  379. ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the "
  380. "second time, disabling (errno=%d)\n", rc);
  381. dev->acpi_handle = NULL;
  382. /* if port is working, request IDENTIFY reload and continue */
  383. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  384. rc = 1;
  385. }
  386. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  387. return rc;
  388. }