libata-zpodd.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/libata.h>
  3. #include <linux/cdrom.h>
  4. #include <linux/pm_runtime.h>
  5. #include <linux/module.h>
  6. #include <linux/pm_qos.h>
  7. #include <scsi/scsi_device.h>
  8. #include "libata.h"
  9. static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */
  10. module_param(zpodd_poweroff_delay, int, 0644);
  11. MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds");
  12. enum odd_mech_type {
  13. ODD_MECH_TYPE_SLOT,
  14. ODD_MECH_TYPE_DRAWER,
  15. ODD_MECH_TYPE_UNSUPPORTED,
  16. };
  17. struct zpodd {
  18. enum odd_mech_type mech_type; /* init during probe, RO afterwards */
  19. struct ata_device *dev;
  20. /* The following fields are synchronized by PM core. */
  21. bool from_notify; /* resumed as a result of
  22. * acpi wake notification */
  23. bool zp_ready; /* ZP ready state */
  24. unsigned long last_ready; /* last ZP ready timestamp */
  25. bool zp_sampled; /* ZP ready state sampled */
  26. bool powered_off; /* ODD is powered off
  27. * during suspend */
  28. };
  29. static int eject_tray(struct ata_device *dev)
  30. {
  31. struct ata_taskfile tf;
  32. static const char cdb[ATAPI_CDB_LEN] = { GPCMD_START_STOP_UNIT,
  33. 0, 0, 0,
  34. 0x02, /* LoEj */
  35. 0, 0, 0, 0, 0, 0, 0,
  36. };
  37. ata_tf_init(dev, &tf);
  38. tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  39. tf.command = ATA_CMD_PACKET;
  40. tf.protocol = ATAPI_PROT_NODATA;
  41. return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
  42. }
  43. /* Per the spec, only slot type and drawer type ODD can be supported */
  44. static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev)
  45. {
  46. char *buf;
  47. unsigned int ret;
  48. struct rm_feature_desc *desc;
  49. struct ata_taskfile tf;
  50. static const char cdb[ATAPI_CDB_LEN] = { GPCMD_GET_CONFIGURATION,
  51. 2, /* only 1 feature descriptor requested */
  52. 0, 3, /* 3, removable medium feature */
  53. 0, 0, 0,/* reserved */
  54. 0, 16,
  55. 0, 0, 0,
  56. };
  57. buf = kzalloc(16, GFP_KERNEL);
  58. if (!buf)
  59. return ODD_MECH_TYPE_UNSUPPORTED;
  60. desc = (void *)(buf + 8);
  61. ata_tf_init(dev, &tf);
  62. tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  63. tf.command = ATA_CMD_PACKET;
  64. tf.protocol = ATAPI_PROT_PIO;
  65. tf.lbam = 16;
  66. ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  67. buf, 16, 0);
  68. if (ret) {
  69. kfree(buf);
  70. return ODD_MECH_TYPE_UNSUPPORTED;
  71. }
  72. if (be16_to_cpu(desc->feature_code) != 3) {
  73. kfree(buf);
  74. return ODD_MECH_TYPE_UNSUPPORTED;
  75. }
  76. if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1) {
  77. kfree(buf);
  78. return ODD_MECH_TYPE_SLOT;
  79. } else if (desc->mech_type == 1 && desc->load == 0 &&
  80. desc->eject == 1) {
  81. kfree(buf);
  82. return ODD_MECH_TYPE_DRAWER;
  83. } else {
  84. kfree(buf);
  85. return ODD_MECH_TYPE_UNSUPPORTED;
  86. }
  87. }
  88. /* Test if ODD is zero power ready by sense code */
  89. static bool zpready(struct ata_device *dev)
  90. {
  91. u8 sense_key, *sense_buf;
  92. unsigned int ret, asc, ascq, add_len;
  93. struct zpodd *zpodd = dev->zpodd;
  94. ret = atapi_eh_tur(dev, &sense_key);
  95. if (!ret || sense_key != NOT_READY)
  96. return false;
  97. sense_buf = dev->link->ap->sector_buf;
  98. ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
  99. if (ret)
  100. return false;
  101. /* sense valid */
  102. if ((sense_buf[0] & 0x7f) != 0x70)
  103. return false;
  104. add_len = sense_buf[7];
  105. /* has asc and ascq */
  106. if (add_len < 6)
  107. return false;
  108. asc = sense_buf[12];
  109. ascq = sense_buf[13];
  110. if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
  111. /* no media inside */
  112. return asc == 0x3a;
  113. else
  114. /* no media inside and door closed */
  115. return asc == 0x3a && ascq == 0x01;
  116. }
  117. /*
  118. * Update the zpodd->zp_ready field. This field will only be set
  119. * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
  120. * time, and will be used to decide if power off is allowed. If it
  121. * is set, it will be cleared during resume from powered off state.
  122. */
  123. void zpodd_on_suspend(struct ata_device *dev)
  124. {
  125. struct zpodd *zpodd = dev->zpodd;
  126. unsigned long expires;
  127. if (!zpready(dev)) {
  128. zpodd->zp_sampled = false;
  129. zpodd->zp_ready = false;
  130. return;
  131. }
  132. if (!zpodd->zp_sampled) {
  133. zpodd->zp_sampled = true;
  134. zpodd->last_ready = jiffies;
  135. return;
  136. }
  137. expires = zpodd->last_ready +
  138. msecs_to_jiffies(zpodd_poweroff_delay * 1000);
  139. if (time_before(jiffies, expires))
  140. return;
  141. zpodd->zp_ready = true;
  142. }
  143. bool zpodd_zpready(struct ata_device *dev)
  144. {
  145. struct zpodd *zpodd = dev->zpodd;
  146. return zpodd->zp_ready;
  147. }
  148. /*
  149. * Enable runtime wake capability through ACPI and set the powered_off flag,
  150. * this flag will be used during resume to decide what operations are needed
  151. * to take.
  152. *
  153. * Also, media poll needs to be silenced, so that it doesn't bring the ODD
  154. * back to full power state every few seconds.
  155. */
  156. void zpodd_enable_run_wake(struct ata_device *dev)
  157. {
  158. struct zpodd *zpodd = dev->zpodd;
  159. sdev_disable_disk_events(dev->sdev);
  160. zpodd->powered_off = true;
  161. acpi_pm_set_device_wakeup(&dev->tdev, true);
  162. }
  163. /* Disable runtime wake capability if it is enabled */
  164. void zpodd_disable_run_wake(struct ata_device *dev)
  165. {
  166. struct zpodd *zpodd = dev->zpodd;
  167. if (zpodd->powered_off)
  168. acpi_pm_set_device_wakeup(&dev->tdev, false);
  169. }
  170. /*
  171. * Post power on processing after the ODD has been recovered. If the
  172. * ODD wasn't powered off during suspend, it doesn't do anything.
  173. *
  174. * For drawer type ODD, if it is powered on due to user pressed the
  175. * eject button, the tray needs to be ejected. This can only be done
  176. * after the ODD has been recovered, i.e. link is initialized and
  177. * device is able to process NON_DATA PIO command, as eject needs to
  178. * send command for the ODD to process.
  179. *
  180. * The from_notify flag set in wake notification handler function
  181. * zpodd_wake_dev represents if power on is due to user's action.
  182. *
  183. * For both types of ODD, several fields need to be reset.
  184. */
  185. void zpodd_post_poweron(struct ata_device *dev)
  186. {
  187. struct zpodd *zpodd = dev->zpodd;
  188. if (!zpodd->powered_off)
  189. return;
  190. zpodd->powered_off = false;
  191. if (zpodd->from_notify) {
  192. zpodd->from_notify = false;
  193. if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
  194. eject_tray(dev);
  195. }
  196. zpodd->zp_sampled = false;
  197. zpodd->zp_ready = false;
  198. sdev_enable_disk_events(dev->sdev);
  199. }
  200. static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
  201. {
  202. struct ata_device *ata_dev = context;
  203. struct zpodd *zpodd = ata_dev->zpodd;
  204. struct device *dev = &ata_dev->sdev->sdev_gendev;
  205. if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) {
  206. zpodd->from_notify = true;
  207. pm_runtime_resume(dev);
  208. }
  209. }
  210. static void ata_acpi_add_pm_notifier(struct ata_device *dev)
  211. {
  212. acpi_handle handle = ata_dev_acpi_handle(dev);
  213. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  214. zpodd_wake_dev, dev);
  215. }
  216. static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
  217. {
  218. acpi_handle handle = ata_dev_acpi_handle(dev);
  219. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
  220. }
  221. void zpodd_init(struct ata_device *dev)
  222. {
  223. struct acpi_device *adev = ACPI_COMPANION(&dev->tdev);
  224. enum odd_mech_type mech_type;
  225. struct zpodd *zpodd;
  226. if (dev->zpodd || !adev || !acpi_device_can_poweroff(adev))
  227. return;
  228. mech_type = zpodd_get_mech_type(dev);
  229. if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
  230. return;
  231. zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
  232. if (!zpodd)
  233. return;
  234. zpodd->mech_type = mech_type;
  235. ata_acpi_add_pm_notifier(dev);
  236. zpodd->dev = dev;
  237. dev->zpodd = zpodd;
  238. dev_pm_qos_expose_flags(&dev->tdev, 0);
  239. }
  240. void zpodd_exit(struct ata_device *dev)
  241. {
  242. ata_acpi_remove_pm_notifier(dev);
  243. kfree(dev->zpodd);
  244. dev->zpodd = NULL;
  245. }