Re: [PATCH] forcedeth: switch from 'pci_' to 'dma_' API

From: Christophe JAILLET
Date: Mon Aug 23 2021 - 00:33:17 EST


Le 23/08/2021 à 04:39, Zhu Yanjun a écrit :
On Sun, Aug 22, 2021 at 3:09 PM Christophe JAILLET
<christophe.jaillet@xxxxxxxxxx> wrote:

The wrappers in include/linux/pci-dma-compat.h should go away.

The patch has been generated with the coccinelle script below.

It has been hand modified to use 'dma_set_mask_and_coherent()' instead of
'pci_set_dma_mask()/pci_set_consistent_dma_mask()' when applicable.
This is less verbose.

It has been compile tested.


@@
@@
- PCI_DMA_BIDIRECTIONAL
+ DMA_BIDIRECTIONAL

@@
@@
- PCI_DMA_TODEVICE
+ DMA_TO_DEVICE

@@
@@
- PCI_DMA_FROMDEVICE
+ DMA_FROM_DEVICE

@@
@@
- PCI_DMA_NONE
+ DMA_NONE

@@
expression e1, e2, e3;
@@
- pci_alloc_consistent(e1, e2, e3)
+ dma_alloc_coherent(&e1->dev, e2, e3, GFP_)

@@
expression e1, e2, e3;
@@
- pci_zalloc_consistent(e1, e2, e3)
+ dma_alloc_coherent(&e1->dev, e2, e3, GFP_)

@@
expression e1, e2, e3, e4;
@@
- pci_free_consistent(e1, e2, e3, e4)
+ dma_free_coherent(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4;
@@
- pci_map_single(e1, e2, e3, e4)
+ dma_map_single(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4;
@@
- pci_unmap_single(e1, e2, e3, e4)
+ dma_unmap_single(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4, e5;
@@
- pci_map_page(e1, e2, e3, e4, e5)
+ dma_map_page(&e1->dev, e2, e3, e4, e5)

@@
expression e1, e2, e3, e4;
@@
- pci_unmap_page(e1, e2, e3, e4)
+ dma_unmap_page(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4;
@@
- pci_map_sg(e1, e2, e3, e4)
+ dma_map_sg(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4;
@@
- pci_unmap_sg(e1, e2, e3, e4)
+ dma_unmap_sg(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4;
@@
- pci_dma_sync_single_for_cpu(e1, e2, e3, e4)
+ dma_sync_single_for_cpu(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4;
@@
- pci_dma_sync_single_for_device(e1, e2, e3, e4)
+ dma_sync_single_for_device(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4;
@@
- pci_dma_sync_sg_for_cpu(e1, e2, e3, e4)
+ dma_sync_sg_for_cpu(&e1->dev, e2, e3, e4)

@@
expression e1, e2, e3, e4;
@@
- pci_dma_sync_sg_for_device(e1, e2, e3, e4)
+ dma_sync_sg_for_device(&e1->dev, e2, e3, e4)

@@
expression e1, e2;
@@
- pci_dma_mapping_error(e1, e2)
+ dma_mapping_error(&e1->dev, e2)

@@
expression e1, e2;
@@
- pci_set_dma_mask(e1, e2)
+ dma_set_mask(&e1->dev, e2)

@@
expression e1, e2;
@@
- pci_set_consistent_dma_mask(e1, e2)
+ dma_set_coherent_mask(&e1->dev, e2)

Signed-off-by: Christophe JAILLET <christophe.jaillet@xxxxxxxxxx>
---
If needed, see post from Christoph Hellwig on the kernel-janitors ML:
https://marc.info/?l=kernel-janitors&m=158745678307186&w=4
---
drivers/net/ethernet/nvidia/forcedeth.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/nvidia/forcedeth.c b/drivers/net/ethernet/nvidia/forcedeth.c
index 8724d6a9ed02..ef3fb4cc90af 100644
--- a/drivers/net/ethernet/nvidia/forcedeth.c
+++ b/drivers/net/ethernet/nvidia/forcedeth.c
@@ -5782,15 +5782,11 @@ static int nv_probe(struct pci_dev *pci_dev, const struct pci_device_id *id)
np->desc_ver = DESC_VER_3;
np->txrxctl_bits = NVREG_TXRXCTL_DESC_3;
if (dma_64bit) {
- if (pci_set_dma_mask(pci_dev, DMA_BIT_MASK(39)))
+ if (dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(39)))
dev_info(&pci_dev->dev,
"64-bit DMA failed, using 32-bit addressing\n");
else
dev->features |= NETIF_F_HIGHDMA;
- if (pci_set_consistent_dma_mask(pci_dev, DMA_BIT_MASK(39))) {
- dev_info(&pci_dev->dev,
- "64-bit DMA (consistent) failed, using 32-bit ring buffers\n");
- }

From the commit log, "pci_set_consistent_dma_mask(e1, e2)" should be
replaced by "dma_set_coherent_mask(&e1->dev, e2)".
But in this snippet, "pci_set_consistent_dma_mask(e1, e2)" is not
replaced by "dma_set_coherent_mask(&e1->dev, e2)".

Why?

Hi,
in the commit log I said that:
The patch has been generated with the coccinelle script below.

It has been hand modified to use 'dma_set_mask_and_coherent()'
instead of 'pci_set_dma_mask()/pci_set_consistent_dma_mask()' when
applicable.
This is less verbose.

When I started this task proposed by Christoph Hellwig ([1]), their were 150 files using using 'pci_set_dma_mask()' ([2]). Many of them were good candidate for using 'dma_set_mask_and_coherent()' but this transformation can not easily be done by coccinelle because it depends on the way the code has been written.

So, I decided to hand modify and include the transformation in the many patches have sent to remove this deprecated API.
Up to now, it has never been an issue.

I *DO* know that it should have been a 2 steps process but this clean-up was too big for me (i.e. 150 files) and doing the job twice was discouraging.

My first motivation was to remove the deprecated API. Simplifying code and using 'dma_set_mask_and_coherent()' when applicable was just a bonus.

So, if desired, I can send a v2 without the additional transformation but I won't send 2 patches for that. The 'dma_set_mask_and_coherent()' transformation would be left apart, for whoever feels like cleaning it.

CJ


[1]: https://marc.info/?l=kernel-janitors&m=158745678307186&w=4
[2]: https://elixir.bootlin.com/linux/v5.8/A/ident/pci_set_dma_mask



Zhu Yanjun


}
} else if (id->driver_data & DEV_HAS_LARGEDESC) {
/* packet format 2: supports jumbo frames */
--
2.30.2