libata-acpi.c 25 KB

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