libata-acpi.c 29 KB

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