libata-acpi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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/module.h>
  9. #include <linux/ata.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/acpi.h>
  15. #include <linux/libata.h>
  16. #include <linux/pci.h>
  17. #include <scsi/scsi_device.h>
  18. #include "libata.h"
  19. #include <acpi/acpi_bus.h>
  20. #include <acpi/acnames.h>
  21. #include <acpi/acnamesp.h>
  22. #include <acpi/acparser.h>
  23. #include <acpi/acexcep.h>
  24. #include <acpi/acmacros.h>
  25. #include <acpi/actypes.h>
  26. enum {
  27. ATA_ACPI_FILTER_SETXFER = 1 << 0,
  28. ATA_ACPI_FILTER_LOCK = 1 << 1,
  29. ATA_ACPI_FILTER_DEFAULT = ATA_ACPI_FILTER_SETXFER |
  30. ATA_ACPI_FILTER_LOCK,
  31. };
  32. static unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
  33. module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
  34. MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock)");
  35. #define NO_PORT_MULT 0xffff
  36. #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
  37. #define REGS_PER_GTF 7
  38. struct ata_acpi_gtf {
  39. u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
  40. } __packed;
  41. /*
  42. * Helper - belongs in the PCI layer somewhere eventually
  43. */
  44. static int is_pci_dev(struct device *dev)
  45. {
  46. return (dev->bus == &pci_bus_type);
  47. }
  48. static void ata_acpi_clear_gtf(struct ata_device *dev)
  49. {
  50. kfree(dev->gtf_cache);
  51. dev->gtf_cache = NULL;
  52. }
  53. /**
  54. * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
  55. * @ap: target SATA port
  56. *
  57. * Look up ACPI objects associated with @ap and initialize acpi_handle
  58. * fields of @ap, the port and devices accordingly.
  59. *
  60. * LOCKING:
  61. * EH context.
  62. *
  63. * RETURNS:
  64. * 0 on success, -errno on failure.
  65. */
  66. void ata_acpi_associate_sata_port(struct ata_port *ap)
  67. {
  68. WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
  69. if (!sata_pmp_attached(ap)) {
  70. acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
  71. ap->link.device->acpi_handle =
  72. acpi_get_child(ap->host->acpi_handle, adr);
  73. } else {
  74. struct ata_link *link;
  75. ap->link.device->acpi_handle = NULL;
  76. ata_port_for_each_link(link, ap) {
  77. acpi_integer adr = SATA_ADR(ap->port_no, link->pmp);
  78. link->device->acpi_handle =
  79. acpi_get_child(ap->host->acpi_handle, adr);
  80. }
  81. }
  82. }
  83. static void ata_acpi_associate_ide_port(struct ata_port *ap)
  84. {
  85. int max_devices, i;
  86. ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
  87. if (!ap->acpi_handle)
  88. return;
  89. max_devices = 1;
  90. if (ap->flags & ATA_FLAG_SLAVE_POSS)
  91. max_devices++;
  92. for (i = 0; i < max_devices; i++) {
  93. struct ata_device *dev = &ap->link.device[i];
  94. dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
  95. }
  96. if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
  97. ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
  98. }
  99. static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
  100. u32 event)
  101. {
  102. char event_string[12];
  103. char *envp[] = { event_string, NULL };
  104. struct ata_eh_info *ehi;
  105. struct kobject *kobj = NULL;
  106. int wait = 0;
  107. unsigned long flags;
  108. if (!ap)
  109. ap = dev->link->ap;
  110. ehi = &ap->link.eh_info;
  111. spin_lock_irqsave(ap->lock, flags);
  112. switch (event) {
  113. case ACPI_NOTIFY_BUS_CHECK:
  114. case ACPI_NOTIFY_DEVICE_CHECK:
  115. ata_ehi_push_desc(ehi, "ACPI event");
  116. ata_ehi_hotplugged(ehi);
  117. ata_port_freeze(ap);
  118. break;
  119. case ACPI_NOTIFY_EJECT_REQUEST:
  120. ata_ehi_push_desc(ehi, "ACPI event");
  121. if (dev)
  122. dev->flags |= ATA_DFLAG_DETACH;
  123. else {
  124. struct ata_link *tlink;
  125. struct ata_device *tdev;
  126. ata_port_for_each_link(tlink, ap)
  127. ata_link_for_each_dev(tdev, tlink)
  128. tdev->flags |= ATA_DFLAG_DETACH;
  129. }
  130. ata_port_schedule_eh(ap);
  131. wait = 1;
  132. break;
  133. }
  134. if (dev) {
  135. if (dev->sdev)
  136. kobj = &dev->sdev->sdev_gendev.kobj;
  137. } else
  138. kobj = &ap->dev->kobj;
  139. if (kobj) {
  140. sprintf(event_string, "BAY_EVENT=%d", event);
  141. kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
  142. }
  143. spin_unlock_irqrestore(ap->lock, flags);
  144. if (wait)
  145. ata_port_wait_eh(ap);
  146. }
  147. static void ata_acpi_dev_notify(acpi_handle handle, u32 event, void *data)
  148. {
  149. struct ata_device *dev = data;
  150. ata_acpi_handle_hotplug(NULL, dev, event);
  151. }
  152. static void ata_acpi_ap_notify(acpi_handle handle, u32 event, void *data)
  153. {
  154. struct ata_port *ap = data;
  155. ata_acpi_handle_hotplug(ap, NULL, event);
  156. }
  157. /**
  158. * ata_acpi_associate - associate ATA host with ACPI objects
  159. * @host: target ATA host
  160. *
  161. * Look up ACPI objects associated with @host and initialize
  162. * acpi_handle fields of @host, its ports and devices accordingly.
  163. *
  164. * LOCKING:
  165. * EH context.
  166. *
  167. * RETURNS:
  168. * 0 on success, -errno on failure.
  169. */
  170. void ata_acpi_associate(struct ata_host *host)
  171. {
  172. int i, j;
  173. if (!is_pci_dev(host->dev) || libata_noacpi)
  174. return;
  175. host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
  176. if (!host->acpi_handle)
  177. return;
  178. for (i = 0; i < host->n_ports; i++) {
  179. struct ata_port *ap = host->ports[i];
  180. if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
  181. ata_acpi_associate_sata_port(ap);
  182. else
  183. ata_acpi_associate_ide_port(ap);
  184. if (ap->acpi_handle) {
  185. acpi_install_notify_handler(ap->acpi_handle,
  186. ACPI_SYSTEM_NOTIFY,
  187. ata_acpi_ap_notify, ap);
  188. #if defined(CONFIG_ACPI_DOCK) || defined(CONFIG_ACPI_DOCK_MODULE)
  189. /* we might be on a docking station */
  190. register_hotplug_dock_device(ap->acpi_handle,
  191. ata_acpi_ap_notify, ap);
  192. #endif
  193. }
  194. for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
  195. struct ata_device *dev = &ap->link.device[j];
  196. if (dev->acpi_handle) {
  197. acpi_install_notify_handler(dev->acpi_handle,
  198. ACPI_SYSTEM_NOTIFY,
  199. ata_acpi_dev_notify, dev);
  200. #if defined(CONFIG_ACPI_DOCK) || defined(CONFIG_ACPI_DOCK_MODULE)
  201. /* we might be on a docking station */
  202. register_hotplug_dock_device(dev->acpi_handle,
  203. ata_acpi_dev_notify, dev);
  204. #endif
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. * ata_acpi_dissociate - dissociate ATA host from ACPI objects
  211. * @host: target ATA host
  212. *
  213. * This function is called during driver detach after the whole host
  214. * is shut down.
  215. *
  216. * LOCKING:
  217. * EH context.
  218. */
  219. void ata_acpi_dissociate(struct ata_host *host)
  220. {
  221. int i;
  222. /* Restore initial _GTM values so that driver which attaches
  223. * afterward can use them too.
  224. */
  225. for (i = 0; i < host->n_ports; i++) {
  226. struct ata_port *ap = host->ports[i];
  227. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  228. if (ap->acpi_handle && gtm)
  229. ata_acpi_stm(ap, gtm);
  230. }
  231. }
  232. /**
  233. * ata_acpi_gtm - execute _GTM
  234. * @ap: target ATA port
  235. * @gtm: out parameter for _GTM result
  236. *
  237. * Evaluate _GTM and store the result in @gtm.
  238. *
  239. * LOCKING:
  240. * EH context.
  241. *
  242. * RETURNS:
  243. * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
  244. */
  245. int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
  246. {
  247. struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
  248. union acpi_object *out_obj;
  249. acpi_status status;
  250. int rc = 0;
  251. status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
  252. rc = -ENOENT;
  253. if (status == AE_NOT_FOUND)
  254. goto out_free;
  255. rc = -EINVAL;
  256. if (ACPI_FAILURE(status)) {
  257. ata_port_printk(ap, KERN_ERR,
  258. "ACPI get timing mode failed (AE 0x%x)\n",
  259. status);
  260. goto out_free;
  261. }
  262. out_obj = output.pointer;
  263. if (out_obj->type != ACPI_TYPE_BUFFER) {
  264. ata_port_printk(ap, KERN_WARNING,
  265. "_GTM returned unexpected object type 0x%x\n",
  266. out_obj->type);
  267. goto out_free;
  268. }
  269. if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
  270. ata_port_printk(ap, KERN_ERR,
  271. "_GTM returned invalid length %d\n",
  272. out_obj->buffer.length);
  273. goto out_free;
  274. }
  275. memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
  276. rc = 0;
  277. out_free:
  278. kfree(output.pointer);
  279. return rc;
  280. }
  281. EXPORT_SYMBOL_GPL(ata_acpi_gtm);
  282. /**
  283. * ata_acpi_stm - execute _STM
  284. * @ap: target ATA port
  285. * @stm: timing parameter to _STM
  286. *
  287. * Evaluate _STM with timing parameter @stm.
  288. *
  289. * LOCKING:
  290. * EH context.
  291. *
  292. * RETURNS:
  293. * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
  294. */
  295. int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
  296. {
  297. acpi_status status;
  298. struct ata_acpi_gtm stm_buf = *stm;
  299. struct acpi_object_list input;
  300. union acpi_object in_params[3];
  301. in_params[0].type = ACPI_TYPE_BUFFER;
  302. in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
  303. in_params[0].buffer.pointer = (u8 *)&stm_buf;
  304. /* Buffers for id may need byteswapping ? */
  305. in_params[1].type = ACPI_TYPE_BUFFER;
  306. in_params[1].buffer.length = 512;
  307. in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
  308. in_params[2].type = ACPI_TYPE_BUFFER;
  309. in_params[2].buffer.length = 512;
  310. in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
  311. input.count = 3;
  312. input.pointer = in_params;
  313. status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
  314. if (status == AE_NOT_FOUND)
  315. return -ENOENT;
  316. if (ACPI_FAILURE(status)) {
  317. ata_port_printk(ap, KERN_ERR,
  318. "ACPI set timing mode failed (status=0x%x)\n", status);
  319. return -EINVAL;
  320. }
  321. return 0;
  322. }
  323. EXPORT_SYMBOL_GPL(ata_acpi_stm);
  324. /**
  325. * ata_dev_get_GTF - get the drive bootup default taskfile settings
  326. * @dev: target ATA device
  327. * @gtf: output parameter for buffer containing _GTF taskfile arrays
  328. *
  329. * This applies to both PATA and SATA drives.
  330. *
  331. * The _GTF method has no input parameters.
  332. * It returns a variable number of register set values (registers
  333. * hex 1F1..1F7, taskfiles).
  334. * The <variable number> is not known in advance, so have ACPI-CA
  335. * allocate the buffer as needed and return it, then free it later.
  336. *
  337. * LOCKING:
  338. * EH context.
  339. *
  340. * RETURNS:
  341. * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
  342. * if _GTF is invalid.
  343. */
  344. static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
  345. {
  346. struct ata_port *ap = dev->link->ap;
  347. acpi_status status;
  348. struct acpi_buffer output;
  349. union acpi_object *out_obj;
  350. int rc = 0;
  351. /* if _GTF is cached, use the cached value */
  352. if (dev->gtf_cache) {
  353. out_obj = dev->gtf_cache;
  354. goto done;
  355. }
  356. /* set up output buffer */
  357. output.length = ACPI_ALLOCATE_BUFFER;
  358. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  359. if (ata_msg_probe(ap))
  360. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  361. __func__, ap->port_no);
  362. /* _GTF has no input parameters */
  363. status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
  364. out_obj = dev->gtf_cache = output.pointer;
  365. if (ACPI_FAILURE(status)) {
  366. if (status != AE_NOT_FOUND) {
  367. ata_dev_printk(dev, KERN_WARNING,
  368. "_GTF evaluation failed (AE 0x%x)\n",
  369. status);
  370. rc = -EINVAL;
  371. }
  372. goto out_free;
  373. }
  374. if (!output.length || !output.pointer) {
  375. if (ata_msg_probe(ap))
  376. ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
  377. "length or ptr is NULL (0x%llx, 0x%p)\n",
  378. __func__,
  379. (unsigned long long)output.length,
  380. output.pointer);
  381. rc = -EINVAL;
  382. goto out_free;
  383. }
  384. if (out_obj->type != ACPI_TYPE_BUFFER) {
  385. ata_dev_printk(dev, KERN_WARNING,
  386. "_GTF unexpected object type 0x%x\n",
  387. out_obj->type);
  388. rc = -EINVAL;
  389. goto out_free;
  390. }
  391. if (out_obj->buffer.length % REGS_PER_GTF) {
  392. ata_dev_printk(dev, KERN_WARNING,
  393. "unexpected _GTF length (%d)\n",
  394. out_obj->buffer.length);
  395. rc = -EINVAL;
  396. goto out_free;
  397. }
  398. done:
  399. rc = out_obj->buffer.length / REGS_PER_GTF;
  400. if (gtf) {
  401. *gtf = (void *)out_obj->buffer.pointer;
  402. if (ata_msg_probe(ap))
  403. ata_dev_printk(dev, KERN_DEBUG,
  404. "%s: returning gtf=%p, gtf_count=%d\n",
  405. __func__, *gtf, rc);
  406. }
  407. return rc;
  408. out_free:
  409. ata_acpi_clear_gtf(dev);
  410. return rc;
  411. }
  412. /**
  413. * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
  414. * @dev: target device
  415. * @gtm: GTM parameter to use
  416. *
  417. * Determine xfermask for @dev from @gtm.
  418. *
  419. * LOCKING:
  420. * None.
  421. *
  422. * RETURNS:
  423. * Determined xfermask.
  424. */
  425. unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
  426. const struct ata_acpi_gtm *gtm)
  427. {
  428. unsigned long xfer_mask = 0;
  429. unsigned int type;
  430. int unit;
  431. u8 mode;
  432. /* we always use the 0 slot for crap hardware */
  433. unit = dev->devno;
  434. if (!(gtm->flags & 0x10))
  435. unit = 0;
  436. /* PIO */
  437. mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
  438. xfer_mask |= ata_xfer_mode2mask(mode);
  439. /* See if we have MWDMA or UDMA data. We don't bother with
  440. * MWDMA if UDMA is available as this means the BIOS set UDMA
  441. * and our error changedown if it works is UDMA to PIO anyway.
  442. */
  443. if (!(gtm->flags & (1 << (2 * unit))))
  444. type = ATA_SHIFT_MWDMA;
  445. else
  446. type = ATA_SHIFT_UDMA;
  447. mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
  448. xfer_mask |= ata_xfer_mode2mask(mode);
  449. return xfer_mask;
  450. }
  451. EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
  452. /**
  453. * ata_acpi_cbl_80wire - Check for 80 wire cable
  454. * @ap: Port to check
  455. * @gtm: GTM data to use
  456. *
  457. * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
  458. */
  459. int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
  460. {
  461. struct ata_device *dev;
  462. ata_link_for_each_dev(dev, &ap->link) {
  463. unsigned long xfer_mask, udma_mask;
  464. if (!ata_dev_enabled(dev))
  465. continue;
  466. xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
  467. ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
  468. if (udma_mask & ~ATA_UDMA_MASK_40C)
  469. return 1;
  470. }
  471. return 0;
  472. }
  473. EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
  474. static void ata_acpi_gtf_to_tf(struct ata_device *dev,
  475. const struct ata_acpi_gtf *gtf,
  476. struct ata_taskfile *tf)
  477. {
  478. ata_tf_init(dev, tf);
  479. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  480. tf->protocol = ATA_PROT_NODATA;
  481. tf->feature = gtf->tf[0]; /* 0x1f1 */
  482. tf->nsect = gtf->tf[1]; /* 0x1f2 */
  483. tf->lbal = gtf->tf[2]; /* 0x1f3 */
  484. tf->lbam = gtf->tf[3]; /* 0x1f4 */
  485. tf->lbah = gtf->tf[4]; /* 0x1f5 */
  486. tf->device = gtf->tf[5]; /* 0x1f6 */
  487. tf->command = gtf->tf[6]; /* 0x1f7 */
  488. }
  489. static int ata_acpi_filter_tf(const struct ata_taskfile *tf,
  490. const struct ata_taskfile *ptf)
  491. {
  492. if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_SETXFER) {
  493. /* libata doesn't use ACPI to configure transfer mode.
  494. * It will only confuse device configuration. Skip.
  495. */
  496. if (tf->command == ATA_CMD_SET_FEATURES &&
  497. tf->feature == SETFEATURES_XFER)
  498. return 1;
  499. }
  500. if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_LOCK) {
  501. /* BIOS writers, sorry but we don't wanna lock
  502. * features unless the user explicitly said so.
  503. */
  504. /* DEVICE CONFIGURATION FREEZE LOCK */
  505. if (tf->command == ATA_CMD_CONF_OVERLAY &&
  506. tf->feature == ATA_DCO_FREEZE_LOCK)
  507. return 1;
  508. /* SECURITY FREEZE LOCK */
  509. if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
  510. return 1;
  511. /* SET MAX LOCK and SET MAX FREEZE LOCK */
  512. if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
  513. tf->command == ATA_CMD_SET_MAX &&
  514. (tf->feature == ATA_SET_MAX_LOCK ||
  515. tf->feature == ATA_SET_MAX_FREEZE_LOCK))
  516. return 1;
  517. }
  518. return 0;
  519. }
  520. /**
  521. * ata_acpi_run_tf - send taskfile registers to host controller
  522. * @dev: target ATA device
  523. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  524. *
  525. * Outputs ATA taskfile to standard ATA host controller using MMIO
  526. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  527. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  528. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  529. * hob_lbal, hob_lbam, and hob_lbah.
  530. *
  531. * This function waits for idle (!BUSY and !DRQ) after writing
  532. * registers. If the control register has a new value, this
  533. * function also waits for idle after writing control and before
  534. * writing the remaining registers.
  535. *
  536. * LOCKING:
  537. * EH context.
  538. *
  539. * RETURNS:
  540. * 1 if command is executed successfully. 0 if ignored, rejected or
  541. * filtered out, -errno on other errors.
  542. */
  543. static int ata_acpi_run_tf(struct ata_device *dev,
  544. const struct ata_acpi_gtf *gtf,
  545. const struct ata_acpi_gtf *prev_gtf)
  546. {
  547. struct ata_taskfile *pptf = NULL;
  548. struct ata_taskfile tf, ptf, rtf;
  549. unsigned int err_mask;
  550. const char *level;
  551. char msg[60];
  552. int rc;
  553. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  554. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  555. && (gtf->tf[6] == 0))
  556. return 0;
  557. ata_acpi_gtf_to_tf(dev, gtf, &tf);
  558. if (prev_gtf) {
  559. ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
  560. pptf = &ptf;
  561. }
  562. if (!ata_acpi_filter_tf(&tf, pptf)) {
  563. rtf = tf;
  564. err_mask = ata_exec_internal(dev, &rtf, NULL,
  565. DMA_NONE, NULL, 0, 0);
  566. switch (err_mask) {
  567. case 0:
  568. level = KERN_DEBUG;
  569. snprintf(msg, sizeof(msg), "succeeded");
  570. rc = 1;
  571. break;
  572. case AC_ERR_DEV:
  573. level = KERN_INFO;
  574. snprintf(msg, sizeof(msg),
  575. "rejected by device (Stat=0x%02x Err=0x%02x)",
  576. rtf.command, rtf.feature);
  577. rc = 0;
  578. break;
  579. default:
  580. level = KERN_ERR;
  581. snprintf(msg, sizeof(msg),
  582. "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  583. err_mask, rtf.command, rtf.feature);
  584. rc = -EIO;
  585. break;
  586. }
  587. } else {
  588. level = KERN_INFO;
  589. snprintf(msg, sizeof(msg), "filtered out");
  590. rc = 0;
  591. }
  592. ata_dev_printk(dev, level,
  593. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x %s\n",
  594. tf.command, tf.feature, tf.nsect, tf.lbal,
  595. tf.lbam, tf.lbah, tf.device, msg);
  596. return rc;
  597. }
  598. /**
  599. * ata_acpi_exec_tfs - get then write drive taskfile settings
  600. * @dev: target ATA device
  601. * @nr_executed: out paramter for the number of executed commands
  602. *
  603. * Evaluate _GTF and excute returned taskfiles.
  604. *
  605. * LOCKING:
  606. * EH context.
  607. *
  608. * RETURNS:
  609. * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
  610. * -errno on other errors.
  611. */
  612. static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
  613. {
  614. struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
  615. int gtf_count, i, rc;
  616. /* get taskfiles */
  617. rc = ata_dev_get_GTF(dev, &gtf);
  618. if (rc < 0)
  619. return rc;
  620. gtf_count = rc;
  621. /* execute them */
  622. for (i = 0; i < gtf_count; i++, gtf++) {
  623. rc = ata_acpi_run_tf(dev, gtf, pgtf);
  624. if (rc < 0)
  625. break;
  626. if (rc) {
  627. (*nr_executed)++;
  628. pgtf = gtf;
  629. }
  630. }
  631. ata_acpi_clear_gtf(dev);
  632. if (rc < 0)
  633. return rc;
  634. return 0;
  635. }
  636. /**
  637. * ata_acpi_push_id - send Identify data to drive
  638. * @dev: target ATA device
  639. *
  640. * _SDD ACPI object: for SATA mode only
  641. * Must be after Identify (Packet) Device -- uses its data
  642. * ATM this function never returns a failure. It is an optional
  643. * method and if it fails for whatever reason, we should still
  644. * just keep going.
  645. *
  646. * LOCKING:
  647. * EH context.
  648. *
  649. * RETURNS:
  650. * 0 on success, -errno on failure.
  651. */
  652. static int ata_acpi_push_id(struct ata_device *dev)
  653. {
  654. struct ata_port *ap = dev->link->ap;
  655. int err;
  656. acpi_status status;
  657. struct acpi_object_list input;
  658. union acpi_object in_params[1];
  659. if (ata_msg_probe(ap))
  660. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  661. __func__, dev->devno, ap->port_no);
  662. /* Give the drive Identify data to the drive via the _SDD method */
  663. /* _SDD: set up input parameters */
  664. input.count = 1;
  665. input.pointer = in_params;
  666. in_params[0].type = ACPI_TYPE_BUFFER;
  667. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  668. in_params[0].buffer.pointer = (u8 *)dev->id;
  669. /* Output buffer: _SDD has no output */
  670. /* It's OK for _SDD to be missing too. */
  671. swap_buf_le16(dev->id, ATA_ID_WORDS);
  672. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  673. swap_buf_le16(dev->id, ATA_ID_WORDS);
  674. err = ACPI_FAILURE(status) ? -EIO : 0;
  675. if (err < 0)
  676. ata_dev_printk(dev, KERN_WARNING,
  677. "ACPI _SDD failed (AE 0x%x)\n", status);
  678. return err;
  679. }
  680. /**
  681. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  682. * @ap: target ATA port
  683. *
  684. * This function is called when @ap is about to be suspended. All
  685. * devices are already put to sleep but the port_suspend() callback
  686. * hasn't been executed yet. Error return from this function aborts
  687. * suspend.
  688. *
  689. * LOCKING:
  690. * EH context.
  691. *
  692. * RETURNS:
  693. * 0 on success, -errno on failure.
  694. */
  695. int ata_acpi_on_suspend(struct ata_port *ap)
  696. {
  697. /* nada */
  698. return 0;
  699. }
  700. /**
  701. * ata_acpi_on_resume - ATA ACPI hook called on resume
  702. * @ap: target ATA port
  703. *
  704. * This function is called when @ap is resumed - right after port
  705. * itself is resumed but before any EH action is taken.
  706. *
  707. * LOCKING:
  708. * EH context.
  709. */
  710. void ata_acpi_on_resume(struct ata_port *ap)
  711. {
  712. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  713. struct ata_device *dev;
  714. if (ap->acpi_handle && gtm) {
  715. /* _GTM valid */
  716. /* restore timing parameters */
  717. ata_acpi_stm(ap, gtm);
  718. /* _GTF should immediately follow _STM so that it can
  719. * use values set by _STM. Cache _GTF result and
  720. * schedule _GTF.
  721. */
  722. ata_link_for_each_dev(dev, &ap->link) {
  723. ata_acpi_clear_gtf(dev);
  724. if (ata_dev_enabled(dev) &&
  725. ata_dev_get_GTF(dev, NULL) >= 0)
  726. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  727. }
  728. } else {
  729. /* SATA _GTF needs to be evaulated after _SDD and
  730. * there's no reason to evaluate IDE _GTF early
  731. * without _STM. Clear cache and schedule _GTF.
  732. */
  733. ata_link_for_each_dev(dev, &ap->link) {
  734. ata_acpi_clear_gtf(dev);
  735. if (ata_dev_enabled(dev))
  736. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  737. }
  738. }
  739. }
  740. /**
  741. * ata_acpi_set_state - set the port power state
  742. * @ap: target ATA port
  743. * @state: state, on/off
  744. *
  745. * This function executes the _PS0/_PS3 ACPI method to set the power state.
  746. * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
  747. */
  748. void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  749. {
  750. struct ata_device *dev;
  751. if (!ap->acpi_handle || (ap->flags & ATA_FLAG_ACPI_SATA))
  752. return;
  753. /* channel first and then drives for power on and vica versa
  754. for power off */
  755. if (state.event == PM_EVENT_ON)
  756. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D0);
  757. ata_link_for_each_dev(dev, &ap->link) {
  758. if (dev->acpi_handle && ata_dev_enabled(dev))
  759. acpi_bus_set_power(dev->acpi_handle,
  760. state.event == PM_EVENT_ON ?
  761. ACPI_STATE_D0 : ACPI_STATE_D3);
  762. }
  763. if (state.event != PM_EVENT_ON)
  764. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D3);
  765. }
  766. /**
  767. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  768. * @dev: target ATA device
  769. *
  770. * This function is called when @dev is about to be configured.
  771. * IDENTIFY data might have been modified after this hook is run.
  772. *
  773. * LOCKING:
  774. * EH context.
  775. *
  776. * RETURNS:
  777. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  778. * -errno on failure.
  779. */
  780. int ata_acpi_on_devcfg(struct ata_device *dev)
  781. {
  782. struct ata_port *ap = dev->link->ap;
  783. struct ata_eh_context *ehc = &ap->link.eh_context;
  784. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  785. int nr_executed = 0;
  786. int rc;
  787. if (!dev->acpi_handle)
  788. return 0;
  789. /* do we need to do _GTF? */
  790. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  791. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  792. return 0;
  793. /* do _SDD if SATA */
  794. if (acpi_sata) {
  795. rc = ata_acpi_push_id(dev);
  796. if (rc)
  797. goto acpi_err;
  798. }
  799. /* do _GTF */
  800. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  801. if (rc)
  802. goto acpi_err;
  803. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  804. /* refresh IDENTIFY page if any _GTF command has been executed */
  805. if (nr_executed) {
  806. rc = ata_dev_reread_id(dev, 0);
  807. if (rc < 0) {
  808. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  809. "after ACPI commands\n");
  810. return rc;
  811. }
  812. }
  813. return 0;
  814. acpi_err:
  815. /* ignore evaluation failure if we can continue safely */
  816. if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  817. return 0;
  818. /* fail and let EH retry once more for unknown IO errors */
  819. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  820. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  821. return rc;
  822. }
  823. ata_dev_printk(dev, KERN_WARNING,
  824. "ACPI: failed the second time, disabled\n");
  825. dev->acpi_handle = NULL;
  826. /* We can safely continue if no _GTF command has been executed
  827. * and port is not frozen.
  828. */
  829. if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  830. return 0;
  831. return rc;
  832. }
  833. /**
  834. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  835. * @dev: target ATA device
  836. *
  837. * This function is called when @dev is about to be disabled.
  838. *
  839. * LOCKING:
  840. * EH context.
  841. */
  842. void ata_acpi_on_disable(struct ata_device *dev)
  843. {
  844. ata_acpi_clear_gtf(dev);
  845. }