scsi_lib_dma.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SCSI library functions depending on DMA
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/device.h>
  7. #include <linux/export.h>
  8. #include <linux/kernel.h>
  9. #include <scsi/scsi.h>
  10. #include <scsi/scsi_cmnd.h>
  11. #include <scsi/scsi_device.h>
  12. #include <scsi/scsi_host.h>
  13. /**
  14. * scsi_dma_map - perform DMA mapping against command's sg lists
  15. * @cmd: scsi command
  16. *
  17. * Returns the number of sg lists actually used, zero if the sg lists
  18. * is NULL, or -ENOMEM if the mapping failed.
  19. */
  20. int scsi_dma_map(struct scsi_cmnd *cmd)
  21. {
  22. int nseg = 0;
  23. if (scsi_sg_count(cmd)) {
  24. struct device *dev = cmd->device->host->dma_dev;
  25. nseg = dma_map_sg(dev, scsi_sglist(cmd), scsi_sg_count(cmd),
  26. cmd->sc_data_direction);
  27. if (unlikely(!nseg))
  28. return -ENOMEM;
  29. }
  30. return nseg;
  31. }
  32. EXPORT_SYMBOL(scsi_dma_map);
  33. /**
  34. * scsi_dma_unmap - unmap command's sg lists mapped by scsi_dma_map
  35. * @cmd: scsi command
  36. */
  37. void scsi_dma_unmap(struct scsi_cmnd *cmd)
  38. {
  39. if (scsi_sg_count(cmd)) {
  40. struct device *dev = cmd->device->host->dma_dev;
  41. dma_unmap_sg(dev, scsi_sglist(cmd), scsi_sg_count(cmd),
  42. cmd->sc_data_direction);
  43. }
  44. }
  45. EXPORT_SYMBOL(scsi_dma_unmap);