libata-acpi.c 26 KB

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