libata-acpi.c 24 KB

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