xref: /linux/drivers/scsi/scsi_lib_dma.c (revision 498495dba268b20e8eadd7fe93c140c68b6cc9d2)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27689e82eSCornelia Huck /*
37689e82eSCornelia Huck  * SCSI library functions depending on DMA
47689e82eSCornelia Huck  */
57689e82eSCornelia Huck 
67689e82eSCornelia Huck #include <linux/blkdev.h>
77689e82eSCornelia Huck #include <linux/device.h>
809703660SPaul Gortmaker #include <linux/export.h>
97689e82eSCornelia Huck #include <linux/kernel.h>
107689e82eSCornelia Huck 
117689e82eSCornelia Huck #include <scsi/scsi.h>
127689e82eSCornelia Huck #include <scsi/scsi_cmnd.h>
137689e82eSCornelia Huck #include <scsi/scsi_device.h>
147689e82eSCornelia Huck #include <scsi/scsi_host.h>
157689e82eSCornelia Huck 
167689e82eSCornelia Huck /**
177689e82eSCornelia Huck  * scsi_dma_map - perform DMA mapping against command's sg lists
187689e82eSCornelia Huck  * @cmd:	scsi command
197689e82eSCornelia Huck  *
207689e82eSCornelia Huck  * Returns the number of sg lists actually used, zero if the sg lists
217689e82eSCornelia Huck  * is NULL, or -ENOMEM if the mapping failed.
227689e82eSCornelia Huck  */
scsi_dma_map(struct scsi_cmnd * cmd)237689e82eSCornelia Huck int scsi_dma_map(struct scsi_cmnd *cmd)
247689e82eSCornelia Huck {
257689e82eSCornelia Huck 	int nseg = 0;
267689e82eSCornelia Huck 
277689e82eSCornelia Huck 	if (scsi_sg_count(cmd)) {
28d139b9bdSJames Bottomley 		struct device *dev = cmd->device->host->dma_dev;
297689e82eSCornelia Huck 
307689e82eSCornelia Huck 		nseg = dma_map_sg(dev, scsi_sglist(cmd), scsi_sg_count(cmd),
317689e82eSCornelia Huck 				  cmd->sc_data_direction);
327689e82eSCornelia Huck 		if (unlikely(!nseg))
337689e82eSCornelia Huck 			return -ENOMEM;
347689e82eSCornelia Huck 	}
357689e82eSCornelia Huck 	return nseg;
367689e82eSCornelia Huck }
377689e82eSCornelia Huck EXPORT_SYMBOL(scsi_dma_map);
387689e82eSCornelia Huck 
397689e82eSCornelia Huck /**
407689e82eSCornelia Huck  * scsi_dma_unmap - unmap command's sg lists mapped by scsi_dma_map
417689e82eSCornelia Huck  * @cmd:	scsi command
427689e82eSCornelia Huck  */
scsi_dma_unmap(struct scsi_cmnd * cmd)437689e82eSCornelia Huck void scsi_dma_unmap(struct scsi_cmnd *cmd)
447689e82eSCornelia Huck {
457689e82eSCornelia Huck 	if (scsi_sg_count(cmd)) {
46d139b9bdSJames Bottomley 		struct device *dev = cmd->device->host->dma_dev;
477689e82eSCornelia Huck 
487689e82eSCornelia Huck 		dma_unmap_sg(dev, scsi_sglist(cmd), scsi_sg_count(cmd),
497689e82eSCornelia Huck 			     cmd->sc_data_direction);
507689e82eSCornelia Huck 	}
517689e82eSCornelia Huck }
527689e82eSCornelia Huck EXPORT_SYMBOL(scsi_dma_unmap);
53