ide-devsets.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/gfp.h>
  4. #include <linux/ide.h>
  5. DEFINE_MUTEX(ide_setting_mtx);
  6. ide_devset_get(io_32bit, io_32bit);
  7. static int set_io_32bit(ide_drive_t *drive, int arg)
  8. {
  9. if (drive->dev_flags & IDE_DFLAG_NO_IO_32BIT)
  10. return -EPERM;
  11. if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
  12. return -EINVAL;
  13. drive->io_32bit = arg;
  14. return 0;
  15. }
  16. ide_devset_get_flag(ksettings, IDE_DFLAG_KEEP_SETTINGS);
  17. static int set_ksettings(ide_drive_t *drive, int arg)
  18. {
  19. if (arg < 0 || arg > 1)
  20. return -EINVAL;
  21. if (arg)
  22. drive->dev_flags |= IDE_DFLAG_KEEP_SETTINGS;
  23. else
  24. drive->dev_flags &= ~IDE_DFLAG_KEEP_SETTINGS;
  25. return 0;
  26. }
  27. ide_devset_get_flag(using_dma, IDE_DFLAG_USING_DMA);
  28. static int set_using_dma(ide_drive_t *drive, int arg)
  29. {
  30. #ifdef CONFIG_BLK_DEV_IDEDMA
  31. int err = -EPERM;
  32. if (arg < 0 || arg > 1)
  33. return -EINVAL;
  34. if (ata_id_has_dma(drive->id) == 0)
  35. goto out;
  36. if (drive->hwif->dma_ops == NULL)
  37. goto out;
  38. err = 0;
  39. if (arg) {
  40. if (ide_set_dma(drive))
  41. err = -EIO;
  42. } else
  43. ide_dma_off(drive);
  44. out:
  45. return err;
  46. #else
  47. if (arg < 0 || arg > 1)
  48. return -EINVAL;
  49. return -EPERM;
  50. #endif
  51. }
  52. /*
  53. * handle HDIO_SET_PIO_MODE ioctl abusers here, eventually it will go away
  54. */
  55. static int set_pio_mode_abuse(ide_hwif_t *hwif, u8 req_pio)
  56. {
  57. switch (req_pio) {
  58. case 202:
  59. case 201:
  60. case 200:
  61. case 102:
  62. case 101:
  63. case 100:
  64. return (hwif->host_flags & IDE_HFLAG_ABUSE_DMA_MODES) ? 1 : 0;
  65. case 9:
  66. case 8:
  67. return (hwif->host_flags & IDE_HFLAG_ABUSE_PREFETCH) ? 1 : 0;
  68. case 7:
  69. case 6:
  70. return (hwif->host_flags & IDE_HFLAG_ABUSE_FAST_DEVSEL) ? 1 : 0;
  71. default:
  72. return 0;
  73. }
  74. }
  75. static int set_pio_mode(ide_drive_t *drive, int arg)
  76. {
  77. ide_hwif_t *hwif = drive->hwif;
  78. const struct ide_port_ops *port_ops = hwif->port_ops;
  79. if (arg < 0 || arg > 255)
  80. return -EINVAL;
  81. if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  82. (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  83. return -ENOSYS;
  84. if (set_pio_mode_abuse(drive->hwif, arg)) {
  85. drive->pio_mode = arg + XFER_PIO_0;
  86. if (arg == 8 || arg == 9) {
  87. unsigned long flags;
  88. /* take lock for IDE_DFLAG_[NO_]UNMASK/[NO_]IO_32BIT */
  89. spin_lock_irqsave(&hwif->lock, flags);
  90. port_ops->set_pio_mode(hwif, drive);
  91. spin_unlock_irqrestore(&hwif->lock, flags);
  92. } else
  93. port_ops->set_pio_mode(hwif, drive);
  94. } else {
  95. int keep_dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
  96. ide_set_pio(drive, arg);
  97. if (hwif->host_flags & IDE_HFLAG_SET_PIO_MODE_KEEP_DMA) {
  98. if (keep_dma)
  99. ide_dma_on(drive);
  100. }
  101. }
  102. return 0;
  103. }
  104. ide_devset_get_flag(unmaskirq, IDE_DFLAG_UNMASK);
  105. static int set_unmaskirq(ide_drive_t *drive, int arg)
  106. {
  107. if (drive->dev_flags & IDE_DFLAG_NO_UNMASK)
  108. return -EPERM;
  109. if (arg < 0 || arg > 1)
  110. return -EINVAL;
  111. if (arg)
  112. drive->dev_flags |= IDE_DFLAG_UNMASK;
  113. else
  114. drive->dev_flags &= ~IDE_DFLAG_UNMASK;
  115. return 0;
  116. }
  117. ide_ext_devset_rw_sync(io_32bit, io_32bit);
  118. ide_ext_devset_rw_sync(keepsettings, ksettings);
  119. ide_ext_devset_rw_sync(unmaskirq, unmaskirq);
  120. ide_ext_devset_rw_sync(using_dma, using_dma);
  121. __IDE_DEVSET(pio_mode, DS_SYNC, NULL, set_pio_mode);
  122. int ide_devset_execute(ide_drive_t *drive, const struct ide_devset *setting,
  123. int arg)
  124. {
  125. struct request_queue *q = drive->queue;
  126. struct request *rq;
  127. int ret = 0;
  128. if (!(setting->flags & DS_SYNC))
  129. return setting->set(drive, arg);
  130. rq = blk_get_request(q, REQ_OP_DRV_IN, 0);
  131. ide_req(rq)->type = ATA_PRIV_MISC;
  132. scsi_req(rq)->cmd_len = 5;
  133. scsi_req(rq)->cmd[0] = REQ_DEVSET_EXEC;
  134. *(int *)&scsi_req(rq)->cmd[1] = arg;
  135. ide_req(rq)->special = setting->set;
  136. blk_execute_rq(q, NULL, rq, 0);
  137. ret = scsi_req(rq)->result;
  138. blk_put_request(rq);
  139. return ret;
  140. }
  141. ide_startstop_t ide_do_devset(ide_drive_t *drive, struct request *rq)
  142. {
  143. int err, (*setfunc)(ide_drive_t *, int) = ide_req(rq)->special;
  144. err = setfunc(drive, *(int *)&scsi_req(rq)->cmd[1]);
  145. if (err)
  146. scsi_req(rq)->result = err;
  147. ide_complete_rq(drive, 0, blk_rq_bytes(rq));
  148. return ide_stopped;
  149. }