libata-acpi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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->link.device->acpi_handle =
  41. acpi_get_child(ap->host->acpi_handle, adr);
  42. }
  43. static void ata_acpi_associate_ide_port(struct ata_port *ap)
  44. {
  45. int max_devices, i;
  46. ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
  47. if (!ap->acpi_handle)
  48. return;
  49. max_devices = 1;
  50. if (ap->flags & ATA_FLAG_SLAVE_POSS)
  51. max_devices++;
  52. for (i = 0; i < max_devices; i++) {
  53. struct ata_device *dev = &ap->link.device[i];
  54. dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
  55. }
  56. }
  57. /**
  58. * ata_acpi_associate - associate ATA host with ACPI objects
  59. * @host: target ATA host
  60. *
  61. * Look up ACPI objects associated with @host and initialize
  62. * acpi_handle fields of @host, its ports and devices accordingly.
  63. *
  64. * LOCKING:
  65. * EH context.
  66. *
  67. * RETURNS:
  68. * 0 on success, -errno on failure.
  69. */
  70. void ata_acpi_associate(struct ata_host *host)
  71. {
  72. int i;
  73. if (!is_pci_dev(host->dev) || libata_noacpi)
  74. return;
  75. host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
  76. if (!host->acpi_handle)
  77. return;
  78. for (i = 0; i < host->n_ports; i++) {
  79. struct ata_port *ap = host->ports[i];
  80. if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
  81. ata_acpi_associate_sata_port(ap);
  82. else
  83. ata_acpi_associate_ide_port(ap);
  84. }
  85. }
  86. /**
  87. * ata_acpi_gtm - execute _GTM
  88. * @ap: target ATA port
  89. * @gtm: out parameter for _GTM result
  90. *
  91. * Evaluate _GTM and store the result in @gtm.
  92. *
  93. * LOCKING:
  94. * EH context.
  95. *
  96. * RETURNS:
  97. * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
  98. */
  99. static int ata_acpi_gtm(const struct ata_port *ap, struct ata_acpi_gtm *gtm)
  100. {
  101. struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
  102. union acpi_object *out_obj;
  103. acpi_status status;
  104. int rc = 0;
  105. status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
  106. rc = -ENOENT;
  107. if (status == AE_NOT_FOUND)
  108. goto out_free;
  109. rc = -EINVAL;
  110. if (ACPI_FAILURE(status)) {
  111. ata_port_printk(ap, KERN_ERR,
  112. "ACPI get timing mode failed (AE 0x%x)\n",
  113. status);
  114. goto out_free;
  115. }
  116. out_obj = output.pointer;
  117. if (out_obj->type != ACPI_TYPE_BUFFER) {
  118. ata_port_printk(ap, KERN_WARNING,
  119. "_GTM returned unexpected object type 0x%x\n",
  120. out_obj->type);
  121. goto out_free;
  122. }
  123. if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
  124. ata_port_printk(ap, KERN_ERR,
  125. "_GTM returned invalid length %d\n",
  126. out_obj->buffer.length);
  127. goto out_free;
  128. }
  129. memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
  130. rc = 0;
  131. out_free:
  132. kfree(output.pointer);
  133. return rc;
  134. }
  135. /**
  136. * ata_acpi_stm - execute _STM
  137. * @ap: target ATA port
  138. * @stm: timing parameter to _STM
  139. *
  140. * Evaluate _STM with timing parameter @stm.
  141. *
  142. * LOCKING:
  143. * EH context.
  144. *
  145. * RETURNS:
  146. * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
  147. */
  148. static int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm)
  149. {
  150. acpi_status status;
  151. struct acpi_object_list input;
  152. union acpi_object in_params[3];
  153. in_params[0].type = ACPI_TYPE_BUFFER;
  154. in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
  155. in_params[0].buffer.pointer = (u8 *)stm;
  156. /* Buffers for id may need byteswapping ? */
  157. in_params[1].type = ACPI_TYPE_BUFFER;
  158. in_params[1].buffer.length = 512;
  159. in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
  160. in_params[2].type = ACPI_TYPE_BUFFER;
  161. in_params[2].buffer.length = 512;
  162. in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
  163. input.count = 3;
  164. input.pointer = in_params;
  165. status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
  166. if (status == AE_NOT_FOUND)
  167. return -ENOENT;
  168. if (ACPI_FAILURE(status)) {
  169. ata_port_printk(ap, KERN_ERR,
  170. "ACPI set timing mode failed (status=0x%x)\n", status);
  171. return -EINVAL;
  172. }
  173. return 0;
  174. }
  175. /**
  176. * ata_dev_get_GTF - get the drive bootup default taskfile settings
  177. * @dev: target ATA device
  178. * @gtf: output parameter for buffer containing _GTF taskfile arrays
  179. * @ptr_to_free: pointer which should be freed
  180. *
  181. * This applies to both PATA and SATA drives.
  182. *
  183. * The _GTF method has no input parameters.
  184. * It returns a variable number of register set values (registers
  185. * hex 1F1..1F7, taskfiles).
  186. * The <variable number> is not known in advance, so have ACPI-CA
  187. * allocate the buffer as needed and return it, then free it later.
  188. *
  189. * LOCKING:
  190. * EH context.
  191. *
  192. * RETURNS:
  193. * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
  194. * contain valid data. -errno on other errors.
  195. */
  196. static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
  197. void **ptr_to_free)
  198. {
  199. struct ata_port *ap = dev->link->ap;
  200. acpi_status status;
  201. struct acpi_buffer output;
  202. union acpi_object *out_obj;
  203. int rc = 0;
  204. /* set up output buffer */
  205. output.length = ACPI_ALLOCATE_BUFFER;
  206. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  207. if (ata_msg_probe(ap))
  208. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  209. __FUNCTION__, ap->port_no);
  210. /* _GTF has no input parameters */
  211. status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
  212. if (ACPI_FAILURE(status)) {
  213. if (status != AE_NOT_FOUND) {
  214. ata_dev_printk(dev, KERN_WARNING,
  215. "_GTF evaluation failed (AE 0x%x)\n",
  216. status);
  217. rc = -EIO;
  218. }
  219. goto out_free;
  220. }
  221. if (!output.length || !output.pointer) {
  222. if (ata_msg_probe(ap))
  223. ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
  224. "length or ptr is NULL (0x%llx, 0x%p)\n",
  225. __FUNCTION__,
  226. (unsigned long long)output.length,
  227. output.pointer);
  228. goto out_free;
  229. }
  230. out_obj = output.pointer;
  231. if (out_obj->type != ACPI_TYPE_BUFFER) {
  232. ata_dev_printk(dev, KERN_WARNING,
  233. "_GTF unexpected object type 0x%x\n",
  234. out_obj->type);
  235. rc = -EINVAL;
  236. goto out_free;
  237. }
  238. if (out_obj->buffer.length % REGS_PER_GTF) {
  239. ata_dev_printk(dev, KERN_WARNING,
  240. "unexpected _GTF length (%d)\n",
  241. out_obj->buffer.length);
  242. rc = -EINVAL;
  243. goto out_free;
  244. }
  245. *ptr_to_free = out_obj;
  246. *gtf = (void *)out_obj->buffer.pointer;
  247. rc = out_obj->buffer.length / REGS_PER_GTF;
  248. if (ata_msg_probe(ap))
  249. ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
  250. "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
  251. __FUNCTION__, *gtf, rc, *ptr_to_free);
  252. return rc;
  253. out_free:
  254. kfree(output.pointer);
  255. return rc;
  256. }
  257. /**
  258. * taskfile_load_raw - send taskfile registers to host controller
  259. * @dev: target ATA device
  260. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  261. *
  262. * Outputs ATA taskfile to standard ATA host controller using MMIO
  263. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  264. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  265. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  266. * hob_lbal, hob_lbam, and hob_lbah.
  267. *
  268. * This function waits for idle (!BUSY and !DRQ) after writing
  269. * registers. If the control register has a new value, this
  270. * function also waits for idle after writing control and before
  271. * writing the remaining registers.
  272. *
  273. * LOCKING:
  274. * EH context.
  275. *
  276. * RETURNS:
  277. * 0 on success, -errno on failure.
  278. */
  279. static int taskfile_load_raw(struct ata_device *dev,
  280. const struct ata_acpi_gtf *gtf)
  281. {
  282. struct ata_port *ap = dev->link->ap;
  283. struct ata_taskfile tf, rtf;
  284. unsigned int err_mask;
  285. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  286. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  287. && (gtf->tf[6] == 0))
  288. return 0;
  289. ata_tf_init(dev, &tf);
  290. /* convert gtf to tf */
  291. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
  292. tf.protocol = ATA_PROT_NODATA;
  293. tf.feature = gtf->tf[0]; /* 0x1f1 */
  294. tf.nsect = gtf->tf[1]; /* 0x1f2 */
  295. tf.lbal = gtf->tf[2]; /* 0x1f3 */
  296. tf.lbam = gtf->tf[3]; /* 0x1f4 */
  297. tf.lbah = gtf->tf[4]; /* 0x1f5 */
  298. tf.device = gtf->tf[5]; /* 0x1f6 */
  299. tf.command = gtf->tf[6]; /* 0x1f7 */
  300. if (ata_msg_probe(ap))
  301. ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
  302. "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
  303. tf.command, tf.feature, tf.nsect,
  304. tf.lbal, tf.lbam, tf.lbah, tf.device);
  305. rtf = tf;
  306. err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
  307. if (err_mask) {
  308. ata_dev_printk(dev, KERN_ERR,
  309. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
  310. "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
  311. tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
  312. tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
  313. return -EIO;
  314. }
  315. return 0;
  316. }
  317. /**
  318. * ata_acpi_exec_tfs - get then write drive taskfile settings
  319. * @dev: target ATA device
  320. *
  321. * Evaluate _GTF and excute returned taskfiles.
  322. *
  323. * LOCKING:
  324. * EH context.
  325. *
  326. * RETURNS:
  327. * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
  328. * doesn't contain valid data. -errno on other errors.
  329. */
  330. static int ata_acpi_exec_tfs(struct ata_device *dev)
  331. {
  332. struct ata_acpi_gtf *gtf = NULL;
  333. void *ptr_to_free = NULL;
  334. int gtf_count, i, rc;
  335. /* get taskfiles */
  336. rc = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
  337. if (rc < 0)
  338. return rc;
  339. gtf_count = rc;
  340. /* execute them */
  341. for (i = 0, rc = 0; i < gtf_count; i++) {
  342. int tmp;
  343. /* ACPI errors are eventually ignored. Run till the
  344. * end even after errors.
  345. */
  346. tmp = taskfile_load_raw(dev, gtf++);
  347. if (!rc)
  348. rc = tmp;
  349. }
  350. kfree(ptr_to_free);
  351. if (rc == 0)
  352. return gtf_count;
  353. return rc;
  354. }
  355. /**
  356. * ata_acpi_push_id - send Identify data to drive
  357. * @dev: target ATA device
  358. *
  359. * _SDD ACPI object: for SATA mode only
  360. * Must be after Identify (Packet) Device -- uses its data
  361. * ATM this function never returns a failure. It is an optional
  362. * method and if it fails for whatever reason, we should still
  363. * just keep going.
  364. *
  365. * LOCKING:
  366. * EH context.
  367. *
  368. * RETURNS:
  369. * 0 on success, -errno on failure.
  370. */
  371. static int ata_acpi_push_id(struct ata_device *dev)
  372. {
  373. struct ata_port *ap = dev->link->ap;
  374. int err;
  375. acpi_status status;
  376. struct acpi_object_list input;
  377. union acpi_object in_params[1];
  378. if (ata_msg_probe(ap))
  379. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  380. __FUNCTION__, dev->devno, ap->port_no);
  381. /* Give the drive Identify data to the drive via the _SDD method */
  382. /* _SDD: set up input parameters */
  383. input.count = 1;
  384. input.pointer = in_params;
  385. in_params[0].type = ACPI_TYPE_BUFFER;
  386. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  387. in_params[0].buffer.pointer = (u8 *)dev->id;
  388. /* Output buffer: _SDD has no output */
  389. /* It's OK for _SDD to be missing too. */
  390. swap_buf_le16(dev->id, ATA_ID_WORDS);
  391. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  392. swap_buf_le16(dev->id, ATA_ID_WORDS);
  393. err = ACPI_FAILURE(status) ? -EIO : 0;
  394. if (err < 0)
  395. ata_dev_printk(dev, KERN_WARNING,
  396. "ACPI _SDD failed (AE 0x%x)\n", status);
  397. return err;
  398. }
  399. /**
  400. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  401. * @ap: target ATA port
  402. *
  403. * This function is called when @ap is about to be suspended. All
  404. * devices are already put to sleep but the port_suspend() callback
  405. * hasn't been executed yet. Error return from this function aborts
  406. * suspend.
  407. *
  408. * LOCKING:
  409. * EH context.
  410. *
  411. * RETURNS:
  412. * 0 on success, -errno on failure.
  413. */
  414. int ata_acpi_on_suspend(struct ata_port *ap)
  415. {
  416. unsigned long flags;
  417. int rc;
  418. /* proceed iff per-port acpi_handle is valid */
  419. if (!ap->acpi_handle)
  420. return 0;
  421. BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
  422. /* store timing parameters */
  423. rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
  424. spin_lock_irqsave(ap->lock, flags);
  425. if (rc == 0)
  426. ap->pflags |= ATA_PFLAG_GTM_VALID;
  427. else
  428. ap->pflags &= ~ATA_PFLAG_GTM_VALID;
  429. spin_unlock_irqrestore(ap->lock, flags);
  430. if (rc == -ENOENT)
  431. rc = 0;
  432. return rc;
  433. }
  434. /**
  435. * ata_acpi_on_resume - ATA ACPI hook called on resume
  436. * @ap: target ATA port
  437. *
  438. * This function is called when @ap is resumed - right after port
  439. * itself is resumed but before any EH action is taken.
  440. *
  441. * LOCKING:
  442. * EH context.
  443. */
  444. void ata_acpi_on_resume(struct ata_port *ap)
  445. {
  446. struct ata_device *dev;
  447. if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
  448. BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
  449. /* restore timing parameters */
  450. ata_acpi_stm(ap, &ap->acpi_gtm);
  451. }
  452. /* schedule _GTF */
  453. ata_link_for_each_dev(dev, &ap->link)
  454. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  455. }
  456. /**
  457. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  458. * @dev: target ATA device
  459. *
  460. * This function is called when @dev is about to be configured.
  461. * IDENTIFY data might have been modified after this hook is run.
  462. *
  463. * LOCKING:
  464. * EH context.
  465. *
  466. * RETURNS:
  467. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  468. * -errno on failure.
  469. */
  470. int ata_acpi_on_devcfg(struct ata_device *dev)
  471. {
  472. struct ata_port *ap = dev->link->ap;
  473. struct ata_eh_context *ehc = &ap->link.eh_context;
  474. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  475. int rc;
  476. if (!dev->acpi_handle)
  477. return 0;
  478. /* do we need to do _GTF? */
  479. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  480. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  481. return 0;
  482. /* do _SDD if SATA */
  483. if (acpi_sata) {
  484. rc = ata_acpi_push_id(dev);
  485. if (rc)
  486. goto acpi_err;
  487. }
  488. /* do _GTF */
  489. rc = ata_acpi_exec_tfs(dev);
  490. if (rc < 0)
  491. goto acpi_err;
  492. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  493. /* refresh IDENTIFY page if any _GTF command has been executed */
  494. if (rc > 0) {
  495. rc = ata_dev_reread_id(dev, 0);
  496. if (rc < 0) {
  497. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  498. "after ACPI commands\n");
  499. return rc;
  500. }
  501. }
  502. return 0;
  503. acpi_err:
  504. /* let EH retry on the first failure, disable ACPI on the second */
  505. if (dev->flags & ATA_DFLAG_ACPI_FAILED) {
  506. ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the "
  507. "second time, disabling (errno=%d)\n", rc);
  508. dev->acpi_handle = NULL;
  509. /* if port is working, request IDENTIFY reload and continue */
  510. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  511. rc = 1;
  512. }
  513. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  514. return rc;
  515. }