libata-acpi.c 18 KB

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