libata-acpi.c 24 KB

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