libata-acpi.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. 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, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)");
  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(struct ata_device *dev,
  516. const struct ata_taskfile *tf,
  517. const struct ata_taskfile *ptf)
  518. {
  519. if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
  520. /* libata doesn't use ACPI to configure transfer mode.
  521. * It will only confuse device configuration. Skip.
  522. */
  523. if (tf->command == ATA_CMD_SET_FEATURES &&
  524. tf->feature == SETFEATURES_XFER)
  525. return 1;
  526. }
  527. if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
  528. /* BIOS writers, sorry but we don't wanna lock
  529. * features unless the user explicitly said so.
  530. */
  531. /* DEVICE CONFIGURATION FREEZE LOCK */
  532. if (tf->command == ATA_CMD_CONF_OVERLAY &&
  533. tf->feature == ATA_DCO_FREEZE_LOCK)
  534. return 1;
  535. /* SECURITY FREEZE LOCK */
  536. if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
  537. return 1;
  538. /* SET MAX LOCK and SET MAX FREEZE LOCK */
  539. if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
  540. tf->command == ATA_CMD_SET_MAX &&
  541. (tf->feature == ATA_SET_MAX_LOCK ||
  542. tf->feature == ATA_SET_MAX_FREEZE_LOCK))
  543. return 1;
  544. }
  545. if (tf->command == ATA_CMD_SET_FEATURES &&
  546. tf->feature == SETFEATURES_SATA_ENABLE) {
  547. /* inhibit enabling DIPM */
  548. if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
  549. tf->nsect == SATA_DIPM)
  550. return 1;
  551. /* inhibit FPDMA non-zero offset */
  552. if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
  553. (tf->nsect == SATA_FPDMA_OFFSET ||
  554. tf->nsect == SATA_FPDMA_IN_ORDER))
  555. return 1;
  556. /* inhibit FPDMA auto activation */
  557. if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
  558. tf->nsect == SATA_FPDMA_AA)
  559. return 1;
  560. }
  561. return 0;
  562. }
  563. /**
  564. * ata_acpi_run_tf - send taskfile registers to host controller
  565. * @dev: target ATA device
  566. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  567. *
  568. * Outputs ATA taskfile to standard ATA host controller using MMIO
  569. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  570. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  571. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  572. * hob_lbal, hob_lbam, and hob_lbah.
  573. *
  574. * This function waits for idle (!BUSY and !DRQ) after writing
  575. * registers. If the control register has a new value, this
  576. * function also waits for idle after writing control and before
  577. * writing the remaining registers.
  578. *
  579. * LOCKING:
  580. * EH context.
  581. *
  582. * RETURNS:
  583. * 1 if command is executed successfully. 0 if ignored, rejected or
  584. * filtered out, -errno on other errors.
  585. */
  586. static int ata_acpi_run_tf(struct ata_device *dev,
  587. const struct ata_acpi_gtf *gtf,
  588. const struct ata_acpi_gtf *prev_gtf)
  589. {
  590. struct ata_taskfile *pptf = NULL;
  591. struct ata_taskfile tf, ptf, rtf;
  592. unsigned int err_mask;
  593. const char *level;
  594. const char *descr;
  595. char msg[60];
  596. int rc;
  597. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  598. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  599. && (gtf->tf[6] == 0))
  600. return 0;
  601. ata_acpi_gtf_to_tf(dev, gtf, &tf);
  602. if (prev_gtf) {
  603. ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
  604. pptf = &ptf;
  605. }
  606. if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
  607. rtf = tf;
  608. err_mask = ata_exec_internal(dev, &rtf, NULL,
  609. DMA_NONE, NULL, 0, 0);
  610. switch (err_mask) {
  611. case 0:
  612. level = KERN_DEBUG;
  613. snprintf(msg, sizeof(msg), "succeeded");
  614. rc = 1;
  615. break;
  616. case AC_ERR_DEV:
  617. level = KERN_INFO;
  618. snprintf(msg, sizeof(msg),
  619. "rejected by device (Stat=0x%02x Err=0x%02x)",
  620. rtf.command, rtf.feature);
  621. rc = 0;
  622. break;
  623. default:
  624. level = KERN_ERR;
  625. snprintf(msg, sizeof(msg),
  626. "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  627. err_mask, rtf.command, rtf.feature);
  628. rc = -EIO;
  629. break;
  630. }
  631. } else {
  632. level = KERN_INFO;
  633. snprintf(msg, sizeof(msg), "filtered out");
  634. rc = 0;
  635. }
  636. descr = ata_get_cmd_descript(tf.command);
  637. ata_dev_printk(dev, level,
  638. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
  639. tf.command, tf.feature, tf.nsect, tf.lbal,
  640. tf.lbam, tf.lbah, tf.device,
  641. (descr ? descr : "unknown"), msg);
  642. return rc;
  643. }
  644. /**
  645. * ata_acpi_exec_tfs - get then write drive taskfile settings
  646. * @dev: target ATA device
  647. * @nr_executed: out parameter for the number of executed commands
  648. *
  649. * Evaluate _GTF and execute returned taskfiles.
  650. *
  651. * LOCKING:
  652. * EH context.
  653. *
  654. * RETURNS:
  655. * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
  656. * -errno on other errors.
  657. */
  658. static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
  659. {
  660. struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
  661. int gtf_count, i, rc;
  662. /* get taskfiles */
  663. rc = ata_dev_get_GTF(dev, &gtf);
  664. if (rc < 0)
  665. return rc;
  666. gtf_count = rc;
  667. /* execute them */
  668. for (i = 0; i < gtf_count; i++, gtf++) {
  669. rc = ata_acpi_run_tf(dev, gtf, pgtf);
  670. if (rc < 0)
  671. break;
  672. if (rc) {
  673. (*nr_executed)++;
  674. pgtf = gtf;
  675. }
  676. }
  677. ata_acpi_clear_gtf(dev);
  678. if (rc < 0)
  679. return rc;
  680. return 0;
  681. }
  682. /**
  683. * ata_acpi_push_id - send Identify data to drive
  684. * @dev: target ATA device
  685. *
  686. * _SDD ACPI object: for SATA mode only
  687. * Must be after Identify (Packet) Device -- uses its data
  688. * ATM this function never returns a failure. It is an optional
  689. * method and if it fails for whatever reason, we should still
  690. * just keep going.
  691. *
  692. * LOCKING:
  693. * EH context.
  694. *
  695. * RETURNS:
  696. * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
  697. */
  698. static int ata_acpi_push_id(struct ata_device *dev)
  699. {
  700. struct ata_port *ap = dev->link->ap;
  701. acpi_status status;
  702. struct acpi_object_list input;
  703. union acpi_object in_params[1];
  704. if (ata_msg_probe(ap))
  705. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  706. __func__, dev->devno, ap->port_no);
  707. /* Give the drive Identify data to the drive via the _SDD method */
  708. /* _SDD: set up input parameters */
  709. input.count = 1;
  710. input.pointer = in_params;
  711. in_params[0].type = ACPI_TYPE_BUFFER;
  712. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  713. in_params[0].buffer.pointer = (u8 *)dev->id;
  714. /* Output buffer: _SDD has no output */
  715. /* It's OK for _SDD to be missing too. */
  716. swap_buf_le16(dev->id, ATA_ID_WORDS);
  717. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  718. swap_buf_le16(dev->id, ATA_ID_WORDS);
  719. if (status == AE_NOT_FOUND)
  720. return -ENOENT;
  721. if (ACPI_FAILURE(status)) {
  722. ata_dev_printk(dev, KERN_WARNING,
  723. "ACPI _SDD failed (AE 0x%x)\n", status);
  724. return -EIO;
  725. }
  726. return 0;
  727. }
  728. /**
  729. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  730. * @ap: target ATA port
  731. *
  732. * This function is called when @ap is about to be suspended. All
  733. * devices are already put to sleep but the port_suspend() callback
  734. * hasn't been executed yet. Error return from this function aborts
  735. * suspend.
  736. *
  737. * LOCKING:
  738. * EH context.
  739. *
  740. * RETURNS:
  741. * 0 on success, -errno on failure.
  742. */
  743. int ata_acpi_on_suspend(struct ata_port *ap)
  744. {
  745. /* nada */
  746. return 0;
  747. }
  748. /**
  749. * ata_acpi_on_resume - ATA ACPI hook called on resume
  750. * @ap: target ATA port
  751. *
  752. * This function is called when @ap is resumed - right after port
  753. * itself is resumed but before any EH action is taken.
  754. *
  755. * LOCKING:
  756. * EH context.
  757. */
  758. void ata_acpi_on_resume(struct ata_port *ap)
  759. {
  760. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  761. struct ata_device *dev;
  762. if (ap->acpi_handle && gtm) {
  763. /* _GTM valid */
  764. /* restore timing parameters */
  765. ata_acpi_stm(ap, gtm);
  766. /* _GTF should immediately follow _STM so that it can
  767. * use values set by _STM. Cache _GTF result and
  768. * schedule _GTF.
  769. */
  770. ata_for_each_dev(dev, &ap->link, ALL) {
  771. ata_acpi_clear_gtf(dev);
  772. if (ata_dev_enabled(dev) &&
  773. ata_dev_get_GTF(dev, NULL) >= 0)
  774. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  775. }
  776. } else {
  777. /* SATA _GTF needs to be evaulated after _SDD and
  778. * there's no reason to evaluate IDE _GTF early
  779. * without _STM. Clear cache and schedule _GTF.
  780. */
  781. ata_for_each_dev(dev, &ap->link, ALL) {
  782. ata_acpi_clear_gtf(dev);
  783. if (ata_dev_enabled(dev))
  784. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  785. }
  786. }
  787. }
  788. /**
  789. * ata_acpi_set_state - set the port power state
  790. * @ap: target ATA port
  791. * @state: state, on/off
  792. *
  793. * This function executes the _PS0/_PS3 ACPI method to set the power state.
  794. * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
  795. */
  796. void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  797. {
  798. struct ata_device *dev;
  799. if (!ap->acpi_handle || (ap->flags & ATA_FLAG_ACPI_SATA))
  800. return;
  801. /* channel first and then drives for power on and vica versa
  802. for power off */
  803. if (state.event == PM_EVENT_ON)
  804. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D0);
  805. ata_for_each_dev(dev, &ap->link, ENABLED) {
  806. if (dev->acpi_handle)
  807. acpi_bus_set_power(dev->acpi_handle,
  808. state.event == PM_EVENT_ON ?
  809. ACPI_STATE_D0 : ACPI_STATE_D3);
  810. }
  811. if (state.event != PM_EVENT_ON)
  812. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D3);
  813. }
  814. /**
  815. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  816. * @dev: target ATA device
  817. *
  818. * This function is called when @dev is about to be configured.
  819. * IDENTIFY data might have been modified after this hook is run.
  820. *
  821. * LOCKING:
  822. * EH context.
  823. *
  824. * RETURNS:
  825. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  826. * -errno on failure.
  827. */
  828. int ata_acpi_on_devcfg(struct ata_device *dev)
  829. {
  830. struct ata_port *ap = dev->link->ap;
  831. struct ata_eh_context *ehc = &ap->link.eh_context;
  832. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  833. int nr_executed = 0;
  834. int rc;
  835. if (!dev->acpi_handle)
  836. return 0;
  837. /* do we need to do _GTF? */
  838. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  839. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  840. return 0;
  841. /* do _SDD if SATA */
  842. if (acpi_sata) {
  843. rc = ata_acpi_push_id(dev);
  844. if (rc && rc != -ENOENT)
  845. goto acpi_err;
  846. }
  847. /* do _GTF */
  848. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  849. if (rc)
  850. goto acpi_err;
  851. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  852. /* refresh IDENTIFY page if any _GTF command has been executed */
  853. if (nr_executed) {
  854. rc = ata_dev_reread_id(dev, 0);
  855. if (rc < 0) {
  856. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  857. "after ACPI commands\n");
  858. return rc;
  859. }
  860. }
  861. return 0;
  862. acpi_err:
  863. /* ignore evaluation failure if we can continue safely */
  864. if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  865. return 0;
  866. /* fail and let EH retry once more for unknown IO errors */
  867. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  868. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  869. return rc;
  870. }
  871. ata_dev_printk(dev, KERN_WARNING,
  872. "ACPI: failed the second time, disabled\n");
  873. dev->acpi_handle = NULL;
  874. /* We can safely continue if no _GTF command has been executed
  875. * and port is not frozen.
  876. */
  877. if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  878. return 0;
  879. return rc;
  880. }
  881. /**
  882. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  883. * @dev: target ATA device
  884. *
  885. * This function is called when @dev is about to be disabled.
  886. *
  887. * LOCKING:
  888. * EH context.
  889. */
  890. void ata_acpi_on_disable(struct ata_device *dev)
  891. {
  892. ata_acpi_clear_gtf(dev);
  893. }