Skip to contentSkip to conversation

misc: pci_endpoint_test: Refactor dma_set_mask_and_coherent() logic

PATCH Superseded Frank Li 3 participants 3 messages Mar 28, 2024 – Apr 2, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Frank Li

author
#0 lore raw

PATCH 1/1 misc: pci_endpoint_test: Refactor dma_set_mask_and_coherent() logic

dma_set_mask_and_coherent() will never return failure when mask >= 32bit. So needn't fall back to set dma_set_mask_and_coherent(32).

Even if dma_set_mask_and_coherent(48) failure, dma_set_mask_and_coherent(32) will be failure according to the same reason.

The function dma_set_mask_and_coherent() defines the device DMA access address width. If it's capable of accessing 48 bits, it inherently supports 32-bit space as well.

---

Notes:
Ref: https://lore.kernel.org/linux-pci/[email protected]/T/#u

for document change patch. DMA document sample code is miss leading.

 drivers/misc/pci_endpoint_test.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/misc/pci_endpoint_test.c +1 -5
index c38a6083f0a73..56ac6969a8f59 100644
@@ -824,11 +824,7 @@ static int pci_endpoint_test_probe(struct pci_dev *pdev,
 	init_completion(&test->irq_raised);
 	mutex_init(&test->mutex);
 
-	if ((dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48)) != 0) &&
-	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
-		dev_err(dev, "Cannot set DMA mask\n");
-		return -EINVAL;
-	}
+	dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
 
 	err = pci_enable_device(pdev);
 	if (err) {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Frank Li

dma_set_mask_and_coherent() will never return failure when mask >= 32bit. So needn't fall back to set dma_set_mask_and_coherent(32).

dma_set_mask_and_coherent() should never fail when the mask is >= 32bit, unless the architecture has no DMA support. So no need check for the error and also no need to set dma_set_mask_and_coherent(32) as a fallback.

Quoted 4 lines

Even if dma_set_mask_and_coherent(48) failure, dma_set_mask_and_coherent(32) will be failure according to the same reason.

Even if dma_set_mask_and_coherent(48) fails due to the lack of DMA support (theoretically), then dma_set_mask_and_coherent(32) will also fail for the same reason. So the fallback doesn't make sense.

Quoted 4 lines

The function dma_set_mask_and_coherent() defines the device DMA access address width. If it's capable of accessing 48 bits, it inherently supports 32-bit space as well.

Due to the above reasons, let's simplify the code by setting the streaming and coherent DMA mask to 48 bits.

Quoted 1 line

- Mani

Quoted 30 lines

---

Notes:
Ref: https://lore.kernel.org/linux-pci/[email protected]/T/#u

for document change patch. DMA document sample code is miss leading.

 drivers/misc/pci_endpoint_test.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/misc/pci_endpoint_test.c +1 -5
index c38a6083f0a73..56ac6969a8f59 100644
@@ -824,11 +824,7 @@ static int pci_endpoint_test_probe(struct pci_dev *pdev,
 	init_completion(&test->irq_raised);
 	mutex_init(&test->mutex);
 
-	if ((dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48)) != 0) &&
-	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
-		dev_err(dev, "Cannot set DMA mask\n");
-		return -EINVAL;
-	}
+	dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
 
 	err = pci_enable_device(pdev);
 	if (err) {
Signature · 1 line
2.34.1
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#2 lore raw
In reply to Frank Li
Quoted 6 lines · Frank Li

dma_set_mask_and_coherent() will never return failure when mask >= 32bit. So needn't fall back to set dma_set_mask_and_coherent(32).

Even if dma_set_mask_and_coherent(48) failure, dma_set_mask_and_coherent(32) will be failure according to the same reason.

Quoted 6 lines

The function dma_set_mask_and_coherent() defines the device DMA access address width. If it's capable of accessing 48 bits, it inherently supports 32-bit space as well.

---

Kishon,

do you remember why you set the DMA mask to 48-bits in the first place? It seems a bit random.

Kind regards,
Niklas