libata-zpodd.c 7.1 KB

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