libata-acpi.c 28 KB

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