libata-acpi.c 26 KB

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