libata-acpi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. static 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. /**
  207. * ata_acpi_stm - execute _STM
  208. * @ap: target ATA port
  209. * @stm: timing parameter to _STM
  210. *
  211. * Evaluate _STM with timing parameter @stm.
  212. *
  213. * LOCKING:
  214. * EH context.
  215. *
  216. * RETURNS:
  217. * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
  218. */
  219. static int ata_acpi_stm(const struct ata_port *ap, struct ata_acpi_gtm *stm)
  220. {
  221. acpi_status status;
  222. struct acpi_object_list input;
  223. union acpi_object in_params[3];
  224. in_params[0].type = ACPI_TYPE_BUFFER;
  225. in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
  226. in_params[0].buffer.pointer = (u8 *)stm;
  227. /* Buffers for id may need byteswapping ? */
  228. in_params[1].type = ACPI_TYPE_BUFFER;
  229. in_params[1].buffer.length = 512;
  230. in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
  231. in_params[2].type = ACPI_TYPE_BUFFER;
  232. in_params[2].buffer.length = 512;
  233. in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
  234. input.count = 3;
  235. input.pointer = in_params;
  236. status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
  237. if (status == AE_NOT_FOUND)
  238. return -ENOENT;
  239. if (ACPI_FAILURE(status)) {
  240. ata_port_printk(ap, KERN_ERR,
  241. "ACPI set timing mode failed (status=0x%x)\n", status);
  242. return -EINVAL;
  243. }
  244. return 0;
  245. }
  246. /**
  247. * ata_dev_get_GTF - get the drive bootup default taskfile settings
  248. * @dev: target ATA device
  249. * @gtf: output parameter for buffer containing _GTF taskfile arrays
  250. * @ptr_to_free: pointer which should be freed
  251. *
  252. * This applies to both PATA and SATA drives.
  253. *
  254. * The _GTF method has no input parameters.
  255. * It returns a variable number of register set values (registers
  256. * hex 1F1..1F7, taskfiles).
  257. * The <variable number> is not known in advance, so have ACPI-CA
  258. * allocate the buffer as needed and return it, then free it later.
  259. *
  260. * LOCKING:
  261. * EH context.
  262. *
  263. * RETURNS:
  264. * Number of taskfiles on success, 0 if _GTF doesn't exist or doesn't
  265. * contain valid data. -errno on other errors.
  266. */
  267. static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf,
  268. void **ptr_to_free)
  269. {
  270. struct ata_port *ap = dev->link->ap;
  271. acpi_status status;
  272. struct acpi_buffer output;
  273. union acpi_object *out_obj;
  274. int rc = 0;
  275. /* set up output buffer */
  276. output.length = ACPI_ALLOCATE_BUFFER;
  277. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  278. if (ata_msg_probe(ap))
  279. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  280. __FUNCTION__, ap->port_no);
  281. /* _GTF has no input parameters */
  282. status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
  283. if (ACPI_FAILURE(status)) {
  284. if (status != AE_NOT_FOUND) {
  285. ata_dev_printk(dev, KERN_WARNING,
  286. "_GTF evaluation failed (AE 0x%x)\n",
  287. status);
  288. rc = -EIO;
  289. }
  290. goto out_free;
  291. }
  292. if (!output.length || !output.pointer) {
  293. if (ata_msg_probe(ap))
  294. ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
  295. "length or ptr is NULL (0x%llx, 0x%p)\n",
  296. __FUNCTION__,
  297. (unsigned long long)output.length,
  298. output.pointer);
  299. goto out_free;
  300. }
  301. out_obj = output.pointer;
  302. if (out_obj->type != ACPI_TYPE_BUFFER) {
  303. ata_dev_printk(dev, KERN_WARNING,
  304. "_GTF unexpected object type 0x%x\n",
  305. out_obj->type);
  306. rc = -EINVAL;
  307. goto out_free;
  308. }
  309. if (out_obj->buffer.length % REGS_PER_GTF) {
  310. ata_dev_printk(dev, KERN_WARNING,
  311. "unexpected _GTF length (%d)\n",
  312. out_obj->buffer.length);
  313. rc = -EINVAL;
  314. goto out_free;
  315. }
  316. *ptr_to_free = out_obj;
  317. *gtf = (void *)out_obj->buffer.pointer;
  318. rc = out_obj->buffer.length / REGS_PER_GTF;
  319. if (ata_msg_probe(ap))
  320. ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
  321. "gtf=%p, gtf_count=%d, ptr_to_free=%p\n",
  322. __FUNCTION__, *gtf, rc, *ptr_to_free);
  323. return rc;
  324. out_free:
  325. kfree(output.pointer);
  326. return rc;
  327. }
  328. /**
  329. * ata_acpi_cbl_80wire - Check for 80 wire cable
  330. * @ap: Port to check
  331. *
  332. * Return 1 if the ACPI mode data for this port indicates the BIOS selected
  333. * an 80wire mode.
  334. */
  335. int ata_acpi_cbl_80wire(struct ata_port *ap)
  336. {
  337. struct ata_acpi_gtm gtm;
  338. int valid = 0;
  339. /* No _GTM data, no information */
  340. if (ata_acpi_gtm(ap, &gtm) < 0)
  341. return 0;
  342. /* Split timing, DMA enabled */
  343. if ((gtm.flags & 0x11) == 0x11 && gtm.drive[0].dma < 55)
  344. valid |= 1;
  345. if ((gtm.flags & 0x14) == 0x14 && gtm.drive[1].dma < 55)
  346. valid |= 2;
  347. /* Shared timing, DMA enabled */
  348. if ((gtm.flags & 0x11) == 0x01 && gtm.drive[0].dma < 55)
  349. valid |= 1;
  350. if ((gtm.flags & 0x14) == 0x04 && gtm.drive[0].dma < 55)
  351. valid |= 2;
  352. /* Drive check */
  353. if ((valid & 1) && ata_dev_enabled(&ap->link.device[0]))
  354. return 1;
  355. if ((valid & 2) && ata_dev_enabled(&ap->link.device[1]))
  356. return 1;
  357. return 0;
  358. }
  359. EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
  360. /**
  361. * taskfile_load_raw - send taskfile registers to host controller
  362. * @dev: target ATA device
  363. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  364. *
  365. * Outputs ATA taskfile to standard ATA host controller using MMIO
  366. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  367. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  368. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  369. * hob_lbal, hob_lbam, and hob_lbah.
  370. *
  371. * This function waits for idle (!BUSY and !DRQ) after writing
  372. * registers. If the control register has a new value, this
  373. * function also waits for idle after writing control and before
  374. * writing the remaining registers.
  375. *
  376. * LOCKING:
  377. * EH context.
  378. *
  379. * RETURNS:
  380. * 0 on success, -errno on failure.
  381. */
  382. static int taskfile_load_raw(struct ata_device *dev,
  383. const struct ata_acpi_gtf *gtf)
  384. {
  385. struct ata_port *ap = dev->link->ap;
  386. struct ata_taskfile tf, rtf;
  387. unsigned int err_mask;
  388. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  389. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  390. && (gtf->tf[6] == 0))
  391. return 0;
  392. ata_tf_init(dev, &tf);
  393. /* convert gtf to tf */
  394. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
  395. tf.protocol = ATA_PROT_NODATA;
  396. tf.feature = gtf->tf[0]; /* 0x1f1 */
  397. tf.nsect = gtf->tf[1]; /* 0x1f2 */
  398. tf.lbal = gtf->tf[2]; /* 0x1f3 */
  399. tf.lbam = gtf->tf[3]; /* 0x1f4 */
  400. tf.lbah = gtf->tf[4]; /* 0x1f5 */
  401. tf.device = gtf->tf[5]; /* 0x1f6 */
  402. tf.command = gtf->tf[6]; /* 0x1f7 */
  403. if (ata_msg_probe(ap))
  404. ata_dev_printk(dev, KERN_DEBUG, "executing ACPI cmd "
  405. "%02x/%02x:%02x:%02x:%02x:%02x:%02x\n",
  406. tf.command, tf.feature, tf.nsect,
  407. tf.lbal, tf.lbam, tf.lbah, tf.device);
  408. rtf = tf;
  409. err_mask = ata_exec_internal(dev, &rtf, NULL, DMA_NONE, NULL, 0);
  410. if (err_mask) {
  411. ata_dev_printk(dev, KERN_ERR,
  412. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x failed "
  413. "(Emask=0x%x Stat=0x%02x Err=0x%02x)\n",
  414. tf.command, tf.feature, tf.nsect, tf.lbal, tf.lbam,
  415. tf.lbah, tf.device, err_mask, rtf.command, rtf.feature);
  416. return -EIO;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * ata_acpi_exec_tfs - get then write drive taskfile settings
  422. * @dev: target ATA device
  423. *
  424. * Evaluate _GTF and excute returned taskfiles.
  425. *
  426. * LOCKING:
  427. * EH context.
  428. *
  429. * RETURNS:
  430. * Number of executed taskfiles on success, 0 if _GTF doesn't exist or
  431. * doesn't contain valid data. -errno on other errors.
  432. */
  433. static int ata_acpi_exec_tfs(struct ata_device *dev)
  434. {
  435. struct ata_acpi_gtf *gtf = NULL;
  436. void *ptr_to_free = NULL;
  437. int gtf_count, i, rc;
  438. /* get taskfiles */
  439. rc = ata_dev_get_GTF(dev, &gtf, &ptr_to_free);
  440. if (rc < 0)
  441. return rc;
  442. gtf_count = rc;
  443. /* execute them */
  444. for (i = 0, rc = 0; i < gtf_count; i++) {
  445. int tmp;
  446. /* ACPI errors are eventually ignored. Run till the
  447. * end even after errors.
  448. */
  449. tmp = taskfile_load_raw(dev, gtf++);
  450. if (!rc)
  451. rc = tmp;
  452. }
  453. kfree(ptr_to_free);
  454. if (rc == 0)
  455. return gtf_count;
  456. return rc;
  457. }
  458. /**
  459. * ata_acpi_push_id - send Identify data to drive
  460. * @dev: target ATA device
  461. *
  462. * _SDD ACPI object: for SATA mode only
  463. * Must be after Identify (Packet) Device -- uses its data
  464. * ATM this function never returns a failure. It is an optional
  465. * method and if it fails for whatever reason, we should still
  466. * just keep going.
  467. *
  468. * LOCKING:
  469. * EH context.
  470. *
  471. * RETURNS:
  472. * 0 on success, -errno on failure.
  473. */
  474. static int ata_acpi_push_id(struct ata_device *dev)
  475. {
  476. struct ata_port *ap = dev->link->ap;
  477. int err;
  478. acpi_status status;
  479. struct acpi_object_list input;
  480. union acpi_object in_params[1];
  481. if (ata_msg_probe(ap))
  482. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  483. __FUNCTION__, dev->devno, ap->port_no);
  484. /* Give the drive Identify data to the drive via the _SDD method */
  485. /* _SDD: set up input parameters */
  486. input.count = 1;
  487. input.pointer = in_params;
  488. in_params[0].type = ACPI_TYPE_BUFFER;
  489. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  490. in_params[0].buffer.pointer = (u8 *)dev->id;
  491. /* Output buffer: _SDD has no output */
  492. /* It's OK for _SDD to be missing too. */
  493. swap_buf_le16(dev->id, ATA_ID_WORDS);
  494. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  495. swap_buf_le16(dev->id, ATA_ID_WORDS);
  496. err = ACPI_FAILURE(status) ? -EIO : 0;
  497. if (err < 0)
  498. ata_dev_printk(dev, KERN_WARNING,
  499. "ACPI _SDD failed (AE 0x%x)\n", status);
  500. return err;
  501. }
  502. /**
  503. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  504. * @ap: target ATA port
  505. *
  506. * This function is called when @ap is about to be suspended. All
  507. * devices are already put to sleep but the port_suspend() callback
  508. * hasn't been executed yet. Error return from this function aborts
  509. * suspend.
  510. *
  511. * LOCKING:
  512. * EH context.
  513. *
  514. * RETURNS:
  515. * 0 on success, -errno on failure.
  516. */
  517. int ata_acpi_on_suspend(struct ata_port *ap)
  518. {
  519. unsigned long flags;
  520. int rc;
  521. /* proceed iff per-port acpi_handle is valid */
  522. if (!ap->acpi_handle)
  523. return 0;
  524. BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
  525. /* store timing parameters */
  526. rc = ata_acpi_gtm(ap, &ap->acpi_gtm);
  527. spin_lock_irqsave(ap->lock, flags);
  528. if (rc == 0)
  529. ap->pflags |= ATA_PFLAG_GTM_VALID;
  530. else
  531. ap->pflags &= ~ATA_PFLAG_GTM_VALID;
  532. spin_unlock_irqrestore(ap->lock, flags);
  533. if (rc == -ENOENT)
  534. rc = 0;
  535. return rc;
  536. }
  537. /**
  538. * ata_acpi_on_resume - ATA ACPI hook called on resume
  539. * @ap: target ATA port
  540. *
  541. * This function is called when @ap is resumed - right after port
  542. * itself is resumed but before any EH action is taken.
  543. *
  544. * LOCKING:
  545. * EH context.
  546. */
  547. void ata_acpi_on_resume(struct ata_port *ap)
  548. {
  549. struct ata_device *dev;
  550. if (ap->acpi_handle && (ap->pflags & ATA_PFLAG_GTM_VALID)) {
  551. BUG_ON(ap->flags & ATA_FLAG_ACPI_SATA);
  552. /* restore timing parameters */
  553. ata_acpi_stm(ap, &ap->acpi_gtm);
  554. }
  555. /* schedule _GTF */
  556. ata_link_for_each_dev(dev, &ap->link)
  557. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  558. }
  559. /**
  560. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  561. * @dev: target ATA device
  562. *
  563. * This function is called when @dev is about to be configured.
  564. * IDENTIFY data might have been modified after this hook is run.
  565. *
  566. * LOCKING:
  567. * EH context.
  568. *
  569. * RETURNS:
  570. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  571. * -errno on failure.
  572. */
  573. int ata_acpi_on_devcfg(struct ata_device *dev)
  574. {
  575. struct ata_port *ap = dev->link->ap;
  576. struct ata_eh_context *ehc = &ap->link.eh_context;
  577. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  578. int rc;
  579. if (!dev->acpi_handle)
  580. return 0;
  581. /* do we need to do _GTF? */
  582. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  583. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  584. return 0;
  585. /* do _SDD if SATA */
  586. if (acpi_sata) {
  587. rc = ata_acpi_push_id(dev);
  588. if (rc)
  589. goto acpi_err;
  590. }
  591. /* do _GTF */
  592. rc = ata_acpi_exec_tfs(dev);
  593. if (rc < 0)
  594. goto acpi_err;
  595. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  596. /* refresh IDENTIFY page if any _GTF command has been executed */
  597. if (rc > 0) {
  598. rc = ata_dev_reread_id(dev, 0);
  599. if (rc < 0) {
  600. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  601. "after ACPI commands\n");
  602. return rc;
  603. }
  604. }
  605. return 0;
  606. acpi_err:
  607. /* let EH retry on the first failure, disable ACPI on the second */
  608. if (dev->flags & ATA_DFLAG_ACPI_FAILED) {
  609. ata_dev_printk(dev, KERN_WARNING, "ACPI on devcfg failed the "
  610. "second time, disabling (errno=%d)\n", rc);
  611. dev->acpi_handle = NULL;
  612. /* if port is working, request IDENTIFY reload and continue */
  613. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  614. rc = 1;
  615. }
  616. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  617. return rc;
  618. }