libata-acpi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 <scsi/scsi_device.h>
  17. #include "libata.h"
  18. #include <acpi/acpi_bus.h>
  19. #include <acpi/acnames.h>
  20. #include <acpi/acnamesp.h>
  21. #include <acpi/acparser.h>
  22. #include <acpi/acexcep.h>
  23. #include <acpi/acmacros.h>
  24. #include <acpi/actypes.h>
  25. #define NO_PORT_MULT 0xffff
  26. #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
  27. #define REGS_PER_GTF 7
  28. struct ata_acpi_gtf {
  29. u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
  30. } __packed;
  31. /*
  32. * Helper - belongs in the PCI layer somewhere eventually
  33. */
  34. static int is_pci_dev(struct device *dev)
  35. {
  36. return (dev->bus == &pci_bus_type);
  37. }
  38. static void ata_acpi_clear_gtf(struct ata_device *dev)
  39. {
  40. kfree(dev->gtf_cache);
  41. dev->gtf_cache = NULL;
  42. }
  43. /**
  44. * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
  45. * @ap: target SATA port
  46. *
  47. * Look up ACPI objects associated with @ap and initialize acpi_handle
  48. * fields of @ap, the port and devices accordingly.
  49. *
  50. * LOCKING:
  51. * EH context.
  52. *
  53. * RETURNS:
  54. * 0 on success, -errno on failure.
  55. */
  56. void ata_acpi_associate_sata_port(struct ata_port *ap)
  57. {
  58. WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
  59. if (!ap->nr_pmp_links) {
  60. acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
  61. ap->link.device->acpi_handle =
  62. acpi_get_child(ap->host->acpi_handle, adr);
  63. } else {
  64. struct ata_link *link;
  65. ap->link.device->acpi_handle = NULL;
  66. ata_port_for_each_link(link, ap) {
  67. acpi_integer adr = SATA_ADR(ap->port_no, link->pmp);
  68. link->device->acpi_handle =
  69. acpi_get_child(ap->host->acpi_handle, adr);
  70. }
  71. }
  72. }
  73. static void ata_acpi_associate_ide_port(struct ata_port *ap)
  74. {
  75. int max_devices, i;
  76. ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
  77. if (!ap->acpi_handle)
  78. return;
  79. max_devices = 1;
  80. if (ap->flags & ATA_FLAG_SLAVE_POSS)
  81. max_devices++;
  82. for (i = 0; i < max_devices; i++) {
  83. struct ata_device *dev = &ap->link.device[i];
  84. dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
  85. }
  86. if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
  87. ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
  88. }
  89. static void ata_acpi_handle_hotplug(struct ata_port *ap, struct kobject *kobj,
  90. u32 event)
  91. {
  92. char event_string[12];
  93. char *envp[] = { event_string, NULL };
  94. struct ata_eh_info *ehi = &ap->link.eh_info;
  95. if (event == 0 || event == 1) {
  96. unsigned long flags;
  97. spin_lock_irqsave(ap->lock, flags);
  98. ata_ehi_clear_desc(ehi);
  99. ata_ehi_push_desc(ehi, "ACPI event");
  100. ata_ehi_hotplugged(ehi);
  101. ata_port_freeze(ap);
  102. spin_unlock_irqrestore(ap->lock, flags);
  103. }
  104. if (kobj) {
  105. sprintf(event_string, "BAY_EVENT=%d", event);
  106. kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
  107. }
  108. }
  109. static void ata_acpi_dev_notify(acpi_handle handle, u32 event, void *data)
  110. {
  111. struct ata_device *dev = data;
  112. struct kobject *kobj = NULL;
  113. if (dev->sdev)
  114. kobj = &dev->sdev->sdev_gendev.kobj;
  115. ata_acpi_handle_hotplug(dev->link->ap, kobj, event);
  116. }
  117. static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
  118. {
  119. struct ata_port *ap = data;
  120. ata_acpi_handle_hotplug(ap, &ap->dev->kobj, event);
  121. }
  122. /**
  123. * ata_acpi_associate - associate ATA host with ACPI objects
  124. * @host: target ATA host
  125. *
  126. * Look up ACPI objects associated with @host and initialize
  127. * acpi_handle fields of @host, its ports and devices accordingly.
  128. *
  129. * LOCKING:
  130. * EH context.
  131. *
  132. * RETURNS:
  133. * 0 on success, -errno on failure.
  134. */
  135. void ata_acpi_associate(struct ata_host *host)
  136. {
  137. int i, j;
  138. if (!is_pci_dev(host->dev) || libata_noacpi)
  139. return;
  140. host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
  141. if (!host->acpi_handle)
  142. return;
  143. for (i = 0; i < host->n_ports; i++) {
  144. struct ata_port *ap = host->ports[i];
  145. if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
  146. ata_acpi_associate_sata_port(ap);
  147. else
  148. ata_acpi_associate_ide_port(ap);
  149. if (ap->acpi_handle)
  150. acpi_install_notify_handler (ap->acpi_handle,
  151. ACPI_SYSTEM_NOTIFY,
  152. ata_acpi_ap_notify,
  153. ap);
  154. for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
  155. struct ata_device *dev = &ap->link.device[j];
  156. if (dev->acpi_handle)
  157. acpi_install_notify_handler (dev->acpi_handle,
  158. ACPI_SYSTEM_NOTIFY,
  159. ata_acpi_dev_notify,
  160. dev);
  161. }
  162. }
  163. }
  164. /**
  165. * ata_acpi_dissociate - dissociate ATA host from ACPI objects
  166. * @host: target ATA host
  167. *
  168. * This function is called during driver detach after the whole host
  169. * is shut down.
  170. *
  171. * LOCKING:
  172. * EH context.
  173. */
  174. void ata_acpi_dissociate(struct ata_host *host)
  175. {
  176. int i;
  177. /* Restore initial _GTM values so that driver which attaches
  178. * afterward can use them too.
  179. */
  180. for (i = 0; i < host->n_ports; i++) {
  181. struct ata_port *ap = host->ports[i];
  182. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  183. if (ap->acpi_handle && gtm)
  184. ata_acpi_stm(ap, gtm);
  185. }
  186. }
  187. /**
  188. * ata_acpi_gtm - execute _GTM
  189. * @ap: target ATA port
  190. * @gtm: out parameter for _GTM result
  191. *
  192. * Evaluate _GTM and store the result in @gtm.
  193. *
  194. * LOCKING:
  195. * EH context.
  196. *
  197. * RETURNS:
  198. * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
  199. */
  200. int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
  201. {
  202. struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
  203. union acpi_object *out_obj;
  204. acpi_status status;
  205. int rc = 0;
  206. status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
  207. rc = -ENOENT;
  208. if (status == AE_NOT_FOUND)
  209. goto out_free;
  210. rc = -EINVAL;
  211. if (ACPI_FAILURE(status)) {
  212. ata_port_printk(ap, KERN_ERR,
  213. "ACPI get timing mode failed (AE 0x%x)\n",
  214. status);
  215. goto out_free;
  216. }
  217. out_obj = output.pointer;
  218. if (out_obj->type != ACPI_TYPE_BUFFER) {
  219. ata_port_printk(ap, KERN_WARNING,
  220. "_GTM returned unexpected object type 0x%x\n",
  221. out_obj->type);
  222. goto out_free;
  223. }
  224. if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
  225. ata_port_printk(ap, KERN_ERR,
  226. "_GTM returned invalid length %d\n",
  227. out_obj->buffer.length);
  228. goto out_free;
  229. }
  230. memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
  231. rc = 0;
  232. out_free:
  233. kfree(output.pointer);
  234. return rc;
  235. }
  236. EXPORT_SYMBOL_GPL(ata_acpi_gtm);
  237. /**
  238. * ata_acpi_stm - execute _STM
  239. * @ap: target ATA port
  240. * @stm: timing parameter to _STM
  241. *
  242. * Evaluate _STM with timing parameter @stm.
  243. *
  244. * LOCKING:
  245. * EH context.
  246. *
  247. * RETURNS:
  248. * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
  249. */
  250. int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
  251. {
  252. acpi_status status;
  253. struct ata_acpi_gtm stm_buf = *stm;
  254. struct acpi_object_list input;
  255. union acpi_object in_params[3];
  256. in_params[0].type = ACPI_TYPE_BUFFER;
  257. in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
  258. in_params[0].buffer.pointer = (u8 *)&stm_buf;
  259. /* Buffers for id may need byteswapping ? */
  260. in_params[1].type = ACPI_TYPE_BUFFER;
  261. in_params[1].buffer.length = 512;
  262. in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
  263. in_params[2].type = ACPI_TYPE_BUFFER;
  264. in_params[2].buffer.length = 512;
  265. in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
  266. input.count = 3;
  267. input.pointer = in_params;
  268. status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
  269. if (status == AE_NOT_FOUND)
  270. return -ENOENT;
  271. if (ACPI_FAILURE(status)) {
  272. ata_port_printk(ap, KERN_ERR,
  273. "ACPI set timing mode failed (status=0x%x)\n", status);
  274. return -EINVAL;
  275. }
  276. return 0;
  277. }
  278. EXPORT_SYMBOL_GPL(ata_acpi_stm);
  279. /**
  280. * ata_dev_get_GTF - get the drive bootup default taskfile settings
  281. * @dev: target ATA device
  282. * @gtf: output parameter for buffer containing _GTF taskfile arrays
  283. *
  284. * This applies to both PATA and SATA drives.
  285. *
  286. * The _GTF method has no input parameters.
  287. * It returns a variable number of register set values (registers
  288. * hex 1F1..1F7, taskfiles).
  289. * The <variable number> is not known in advance, so have ACPI-CA
  290. * allocate the buffer as needed and return it, then free it later.
  291. *
  292. * LOCKING:
  293. * EH context.
  294. *
  295. * RETURNS:
  296. * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
  297. * if _GTF is invalid.
  298. */
  299. static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
  300. {
  301. struct ata_port *ap = dev->link->ap;
  302. acpi_status status;
  303. struct acpi_buffer output;
  304. union acpi_object *out_obj;
  305. int rc = 0;
  306. /* if _GTF is cached, use the cached value */
  307. if (dev->gtf_cache) {
  308. out_obj = dev->gtf_cache;
  309. goto done;
  310. }
  311. /* set up output buffer */
  312. output.length = ACPI_ALLOCATE_BUFFER;
  313. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  314. if (ata_msg_probe(ap))
  315. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  316. __FUNCTION__, ap->port_no);
  317. /* _GTF has no input parameters */
  318. status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
  319. out_obj = dev->gtf_cache = output.pointer;
  320. if (ACPI_FAILURE(status)) {
  321. if (status != AE_NOT_FOUND) {
  322. ata_dev_printk(dev, KERN_WARNING,
  323. "_GTF evaluation failed (AE 0x%x)\n",
  324. status);
  325. rc = -EINVAL;
  326. }
  327. goto out_free;
  328. }
  329. if (!output.length || !output.pointer) {
  330. if (ata_msg_probe(ap))
  331. ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
  332. "length or ptr is NULL (0x%llx, 0x%p)\n",
  333. __FUNCTION__,
  334. (unsigned long long)output.length,
  335. output.pointer);
  336. rc = -EINVAL;
  337. goto out_free;
  338. }
  339. if (out_obj->type != ACPI_TYPE_BUFFER) {
  340. ata_dev_printk(dev, KERN_WARNING,
  341. "_GTF unexpected object type 0x%x\n",
  342. out_obj->type);
  343. rc = -EINVAL;
  344. goto out_free;
  345. }
  346. if (out_obj->buffer.length % REGS_PER_GTF) {
  347. ata_dev_printk(dev, KERN_WARNING,
  348. "unexpected _GTF length (%d)\n",
  349. out_obj->buffer.length);
  350. rc = -EINVAL;
  351. goto out_free;
  352. }
  353. done:
  354. rc = out_obj->buffer.length / REGS_PER_GTF;
  355. if (gtf) {
  356. *gtf = (void *)out_obj->buffer.pointer;
  357. if (ata_msg_probe(ap))
  358. ata_dev_printk(dev, KERN_DEBUG,
  359. "%s: returning gtf=%p, gtf_count=%d\n",
  360. __FUNCTION__, *gtf, rc);
  361. }
  362. return rc;
  363. out_free:
  364. ata_acpi_clear_gtf(dev);
  365. return rc;
  366. }
  367. /**
  368. * ata_acpi_cbl_80wire - Check for 80 wire cable
  369. * @ap: Port to check
  370. *
  371. * Return 1 if the ACPI mode data for this port indicates the BIOS selected
  372. * an 80wire mode.
  373. */
  374. int ata_acpi_cbl_80wire(struct ata_port *ap)
  375. {
  376. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  377. int valid = 0;
  378. if (!gtm)
  379. return 0;
  380. /* Split timing, DMA enabled */
  381. if ((gtm->flags & 0x11) == 0x11 && gtm->drive[0].dma < 55)
  382. valid |= 1;
  383. if ((gtm->flags & 0x14) == 0x14 && gtm->drive[1].dma < 55)
  384. valid |= 2;
  385. /* Shared timing, DMA enabled */
  386. if ((gtm->flags & 0x11) == 0x01 && gtm->drive[0].dma < 55)
  387. valid |= 1;
  388. if ((gtm->flags & 0x14) == 0x04 && gtm->drive[0].dma < 55)
  389. valid |= 2;
  390. /* Drive check */
  391. if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
  392. return 1;
  393. if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
  394. return 1;
  395. return 0;
  396. }
  397. EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
  398. /**
  399. * ata_acpi_run_tf - send taskfile registers to host controller
  400. * @dev: target ATA device
  401. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  402. *
  403. * Outputs ATA taskfile to standard ATA host controller using MMIO
  404. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  405. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  406. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  407. * hob_lbal, hob_lbam, and hob_lbah.
  408. *
  409. * This function waits for idle (!BUSY and !DRQ) after writing
  410. * registers. If the control register has a new value, this
  411. * function also waits for idle after writing control and before
  412. * writing the remaining registers.
  413. *
  414. * LOCKING:
  415. * EH context.
  416. *
  417. * RETURNS:
  418. * 1 if command is executed successfully. 0 if ignored or rejected,
  419. * -errno on other errors.
  420. */
  421. static int ata_acpi_run_tf(struct ata_device *dev,
  422. const struct ata_acpi_gtf *gtf)
  423. {
  424. struct ata_taskfile tf, rtf;
  425. unsigned int err_mask;
  426. const char *level;
  427. char msg[60];
  428. int rc;
  429. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  430. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  431. && (gtf->tf[6] == 0))
  432. return 0;
  433. ata_tf_init(dev, &tf);
  434. /* convert gtf to tf */
  435. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
  436. tf.protocol = ATA_PROT_NODATA;
  437. tf.feature = gtf->tf[0]; /* 0x1f1 */
  438. tf.nsect = gtf->tf[1]; /* 0x1f2 */
  439. tf.lbal = gtf->tf[2]; /* 0x1f3 */
  440. tf.lbam = gtf->tf[3]; /* 0x1f4 */
  441. tf.lbah = gtf->tf[4]; /* 0x1f5 */
  442. tf.device = gtf->tf[5]; /* 0x1f6 */
  443. tf.command = gtf->tf[6]; /* 0x1f7 */
  444. rtf = tf;
  445. err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0, 0);
  446. switch (err_mask) {
  447. case 0:
  448. level = KERN_DEBUG;
  449. snprintf(msg, sizeof(msg), "succeeded");
  450. rc = 1;
  451. break;
  452. case AC_ERR_DEV:
  453. level = KERN_INFO;
  454. snprintf(msg, sizeof(msg),
  455. "rejected by device (Stat=0x%02x Err=0x%02x)",
  456. rtf.command, rtf.feature);
  457. rc = 0;
  458. break;
  459. default:
  460. level = KERN_ERR;
  461. snprintf(msg, sizeof(msg),
  462. "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  463. err_mask, rtf.command, rtf.feature);
  464. rc = -EIO;
  465. break;
  466. }
  467. ata_dev_printk(dev, level,
  468. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x %s\n",
  469. tf.command, tf.feature, tf.nsect, tf.lbal,
  470. tf.lbam, tf.lbah, tf.device, msg);
  471. return rc;
  472. }
  473. /**
  474. * ata_acpi_exec_tfs - get then write drive taskfile settings
  475. * @dev: target ATA device
  476. * @nr_executed: out paramter for the number of executed commands
  477. *
  478. * Evaluate _GTF and excute returned taskfiles.
  479. *
  480. * LOCKING:
  481. * EH context.
  482. *
  483. * RETURNS:
  484. * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
  485. * -errno on other errors.
  486. */
  487. static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
  488. {
  489. struct ata_acpi_gtf *gtf = NULL;
  490. int gtf_count, i, rc;
  491. /* get taskfiles */
  492. rc = ata_dev_get_GTF(dev, &gtf);
  493. if (rc < 0)
  494. return rc;
  495. gtf_count = rc;
  496. /* execute them */
  497. for (i = 0; i < gtf_count; i++) {
  498. rc = ata_acpi_run_tf(dev, gtf++);
  499. if (rc < 0)
  500. break;
  501. if (rc)
  502. (*nr_executed)++;
  503. }
  504. ata_acpi_clear_gtf(dev);
  505. if (rc < 0)
  506. return rc;
  507. return 0;
  508. }
  509. /**
  510. * ata_acpi_push_id - send Identify data to drive
  511. * @dev: target ATA device
  512. *
  513. * _SDD ACPI object: for SATA mode only
  514. * Must be after Identify (Packet) Device -- uses its data
  515. * ATM this function never returns a failure. It is an optional
  516. * method and if it fails for whatever reason, we should still
  517. * just keep going.
  518. *
  519. * LOCKING:
  520. * EH context.
  521. *
  522. * RETURNS:
  523. * 0 on success, -errno on failure.
  524. */
  525. static int ata_acpi_push_id(struct ata_device *dev)
  526. {
  527. struct ata_port *ap = dev->link->ap;
  528. int err;
  529. acpi_status status;
  530. struct acpi_object_list input;
  531. union acpi_object in_params[1];
  532. if (ata_msg_probe(ap))
  533. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  534. __FUNCTION__, dev->devno, ap->port_no);
  535. /* Give the drive Identify data to the drive via the _SDD method */
  536. /* _SDD: set up input parameters */
  537. input.count = 1;
  538. input.pointer = in_params;
  539. in_params[0].type = ACPI_TYPE_BUFFER;
  540. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  541. in_params[0].buffer.pointer = (u8 *)dev->id;
  542. /* Output buffer: _SDD has no output */
  543. /* It's OK for _SDD to be missing too. */
  544. swap_buf_le16(dev->id, ATA_ID_WORDS);
  545. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  546. swap_buf_le16(dev->id, ATA_ID_WORDS);
  547. err = ACPI_FAILURE(status) ? -EIO : 0;
  548. if (err < 0)
  549. ata_dev_printk(dev, KERN_WARNING,
  550. "ACPI _SDD failed (AE 0x%x)\n", status);
  551. return err;
  552. }
  553. /**
  554. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  555. * @ap: target ATA port
  556. *
  557. * This function is called when @ap is about to be suspended. All
  558. * devices are already put to sleep but the port_suspend() callback
  559. * hasn't been executed yet. Error return from this function aborts
  560. * suspend.
  561. *
  562. * LOCKING:
  563. * EH context.
  564. *
  565. * RETURNS:
  566. * 0 on success, -errno on failure.
  567. */
  568. int ata_acpi_on_suspend(struct ata_port *ap)
  569. {
  570. /* nada */
  571. return 0;
  572. }
  573. /**
  574. * ata_acpi_on_resume - ATA ACPI hook called on resume
  575. * @ap: target ATA port
  576. *
  577. * This function is called when @ap is resumed - right after port
  578. * itself is resumed but before any EH action is taken.
  579. *
  580. * LOCKING:
  581. * EH context.
  582. */
  583. void ata_acpi_on_resume(struct ata_port *ap)
  584. {
  585. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  586. struct ata_device *dev;
  587. if (ap->acpi_handle && gtm) {
  588. /* _GTM valid */
  589. /* restore timing parameters */
  590. ata_acpi_stm(ap, gtm);
  591. /* _GTF should immediately follow _STM so that it can
  592. * use values set by _STM. Cache _GTF result and
  593. * schedule _GTF.
  594. */
  595. ata_link_for_each_dev(dev, &ap->link) {
  596. ata_acpi_clear_gtf(dev);
  597. if (ata_dev_get_GTF(dev, NULL) >= 0)
  598. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  599. }
  600. } else {
  601. /* SATA _GTF needs to be evaulated after _SDD and
  602. * there's no reason to evaluate IDE _GTF early
  603. * without _STM. Clear cache and schedule _GTF.
  604. */
  605. ata_link_for_each_dev(dev, &ap->link) {
  606. ata_acpi_clear_gtf(dev);
  607. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  608. }
  609. }
  610. }
  611. /**
  612. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  613. * @dev: target ATA device
  614. *
  615. * This function is called when @dev is about to be configured.
  616. * IDENTIFY data might have been modified after this hook is run.
  617. *
  618. * LOCKING:
  619. * EH context.
  620. *
  621. * RETURNS:
  622. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  623. * -errno on failure.
  624. */
  625. int ata_acpi_on_devcfg(struct ata_device *dev)
  626. {
  627. struct ata_port *ap = dev->link->ap;
  628. struct ata_eh_context *ehc = &ap->link.eh_context;
  629. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  630. int nr_executed = 0;
  631. int rc;
  632. if (!dev->acpi_handle)
  633. return 0;
  634. /* do we need to do _GTF? */
  635. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  636. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  637. return 0;
  638. /* do _SDD if SATA */
  639. if (acpi_sata) {
  640. rc = ata_acpi_push_id(dev);
  641. if (rc)
  642. goto acpi_err;
  643. }
  644. /* do _GTF */
  645. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  646. if (rc)
  647. goto acpi_err;
  648. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  649. /* refresh IDENTIFY page if any _GTF command has been executed */
  650. if (nr_executed) {
  651. rc = ata_dev_reread_id(dev, 0);
  652. if (rc < 0) {
  653. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  654. "after ACPI commands\n");
  655. return rc;
  656. }
  657. }
  658. return 0;
  659. acpi_err:
  660. /* ignore evaluation failure if we can continue safely */
  661. if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  662. return 0;
  663. /* fail and let EH retry once more for unknown IO errors */
  664. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  665. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  666. return rc;
  667. }
  668. ata_dev_printk(dev, KERN_WARNING,
  669. "ACPI: failed the second time, disabled\n");
  670. dev->acpi_handle = NULL;
  671. /* We can safely continue if no _GTF command has been executed
  672. * and port is not frozen.
  673. */
  674. if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  675. return 0;
  676. return rc;
  677. }
  678. /**
  679. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  680. * @dev: target ATA device
  681. *
  682. * This function is called when @dev is about to be disabled.
  683. *
  684. * LOCKING:
  685. * EH context.
  686. */
  687. void ata_acpi_on_disable(struct ata_device *dev)
  688. {
  689. ata_acpi_clear_gtf(dev);
  690. }