libata-acpi.c 26 KB

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