libata-acpi.c 29 KB

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