Skip to content

Commit ffd32a9

Browse files
author
Christoph Hellwig
committed
net: fec: use dma_alloc_noncoherent for data cache enabled coldfire
Coldfire platforms with data caches can't properly implement dma_alloc_coherent and currently just return noncoherent memory from dma_alloc_coherent. The fec driver than works around this with a flush of all caches in the receive path. Make this hack a little less bad by using the explicit dma_alloc_noncoherent API and documenting the hacky cache flushes so that the DMA API level hack can be removed. Also replace the check for CONFIG_M532x for said hack with a check for COLDFIRE && !COLDFIRE_COHERENT_DMA. While m532x is the only such platform with a fec module, this makes the code more consistent and easier to follow. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Greg Ungerer <gerg@linux-m68k.org> Tested-by: Greg Ungerer <gerg@linux-m68k.org>
1 parent 9e28bf8 commit ffd32a9

1 file changed

Lines changed: 76 additions & 10 deletions

File tree

drivers/net/ethernet/freescale/fec_main.c

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,70 @@ static void fec_dump(struct net_device *ndev)
406406
} while (bdp != txq->bd.base);
407407
}
408408

409+
/*
410+
* Coldfire does not support DMA coherent allocations, and has historically used
411+
* a band-aid with a manual flush in fec_enet_rx_queue.
412+
*/
413+
#if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
414+
static void *fec_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
415+
gfp_t gfp)
416+
{
417+
return dma_alloc_noncoherent(dev, size, handle, DMA_BIDIRECTIONAL, gfp);
418+
}
419+
420+
static void fec_dma_free(struct device *dev, size_t size, void *cpu_addr,
421+
dma_addr_t handle)
422+
{
423+
dma_free_noncoherent(dev, size, cpu_addr, handle, DMA_BIDIRECTIONAL);
424+
}
425+
#else /* !CONFIG_COLDFIRE || CONFIG_COLDFIRE_COHERENT_DMA */
426+
static void *fec_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
427+
gfp_t gfp)
428+
{
429+
return dma_alloc_coherent(dev, size, handle, gfp);
430+
}
431+
432+
static void fec_dma_free(struct device *dev, size_t size, void *cpu_addr,
433+
dma_addr_t handle)
434+
{
435+
dma_free_coherent(dev, size, cpu_addr, handle);
436+
}
437+
#endif /* !CONFIG_COLDFIRE || CONFIG_COLDFIRE_COHERENT_DMA */
438+
439+
struct fec_dma_devres {
440+
size_t size;
441+
void *vaddr;
442+
dma_addr_t dma_handle;
443+
};
444+
445+
static void fec_dmam_release(struct device *dev, void *res)
446+
{
447+
struct fec_dma_devres *this = res;
448+
449+
fec_dma_free(dev, this->size, this->vaddr, this->dma_handle);
450+
}
451+
452+
static void *fec_dmam_alloc(struct device *dev, size_t size, dma_addr_t *handle,
453+
gfp_t gfp)
454+
{
455+
struct fec_dma_devres *dr;
456+
void *vaddr;
457+
458+
dr = devres_alloc(fec_dmam_release, sizeof(*dr), gfp);
459+
if (!dr)
460+
return NULL;
461+
vaddr = fec_dma_alloc(dev, size, handle, gfp);
462+
if (!vaddr) {
463+
devres_free(dr);
464+
return NULL;
465+
}
466+
dr->vaddr = vaddr;
467+
dr->dma_handle = *handle;
468+
dr->size = size;
469+
devres_add(dev, dr);
470+
return vaddr;
471+
}
472+
409473
static inline bool is_ipv4_pkt(struct sk_buff *skb)
410474
{
411475
return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
@@ -1660,7 +1724,11 @@ fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id)
16601724
}
16611725
#endif
16621726

1663-
#ifdef CONFIG_M532x
1727+
#if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
1728+
/*
1729+
* Hacky flush of all caches instead of using the DMA API for the TSO
1730+
* headers.
1731+
*/
16641732
flush_cache_all();
16651733
#endif
16661734
rxq = fep->rx_queue[queue_id];
@@ -3288,10 +3356,9 @@ static void fec_enet_free_queue(struct net_device *ndev)
32883356
for (i = 0; i < fep->num_tx_queues; i++)
32893357
if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
32903358
txq = fep->tx_queue[i];
3291-
dma_free_coherent(&fep->pdev->dev,
3292-
txq->bd.ring_size * TSO_HEADER_SIZE,
3293-
txq->tso_hdrs,
3294-
txq->tso_hdrs_dma);
3359+
fec_dma_free(&fep->pdev->dev,
3360+
txq->bd.ring_size * TSO_HEADER_SIZE,
3361+
txq->tso_hdrs, txq->tso_hdrs_dma);
32953362
}
32963363

32973364
for (i = 0; i < fep->num_rx_queues; i++)
@@ -3321,10 +3388,9 @@ static int fec_enet_alloc_queue(struct net_device *ndev)
33213388
txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
33223389
txq->tx_wake_threshold = FEC_MAX_SKB_DESCS + 2 * MAX_SKB_FRAGS;
33233390

3324-
txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev,
3391+
txq->tso_hdrs = fec_dma_alloc(&fep->pdev->dev,
33253392
txq->bd.ring_size * TSO_HEADER_SIZE,
3326-
&txq->tso_hdrs_dma,
3327-
GFP_KERNEL);
3393+
&txq->tso_hdrs_dma, GFP_KERNEL);
33283394
if (!txq->tso_hdrs) {
33293395
ret = -ENOMEM;
33303396
goto alloc_failed;
@@ -4043,8 +4109,8 @@ static int fec_enet_init(struct net_device *ndev)
40434109
bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
40444110

40454111
/* Allocate memory for buffer descriptors. */
4046-
cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma,
4047-
GFP_KERNEL);
4112+
cbd_base = fec_dmam_alloc(&fep->pdev->dev, bd_size, &bd_dma,
4113+
GFP_KERNEL);
40484114
if (!cbd_base) {
40494115
ret = -ENOMEM;
40504116
goto free_queue_mem;

0 commit comments

Comments
 (0)