libata-acpi.c 24 KB

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