libata-acpi.c 25 KB

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