libata-acpi.c 14 KB

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