aboutsummaryrefslogtreecommitdiff
path: root/net/core/skb_dma_map.c
blob: 7adb623ef6645d8051481d9fd44988976b09b30f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* skb_dma_map.c: DMA mapping helpers for socket buffers.
 *
 * Copyright (C) David S. Miller <davem@davemloft.net>
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/dma-mapping.h>
#include <linux/skbuff.h>

int skb_dma_map(struct device *dev, struct sk_buff *skb,
		enum dma_data_direction dir)
{
	struct skb_shared_info *sp = skb_shinfo(skb);
	dma_addr_t map;
	int i;

	map = dma_map_single(dev, skb->data,
			     skb_headlen(skb), dir);
	if (dma_mapping_error(dev, map))
		goto out_err;

	sp->dma_maps[0] = map;
	for (i = 0; i < sp->nr_frags; i++) {
		skb_frag_t *fp = &sp->frags[i];

		map = dma_map_page(dev, fp->page, fp->page_offset,
				   fp->size, dir);
		if (dma_mapping_error(dev, map))
			goto unwind;
		sp->dma_maps[i + 1] = map;
	}

	return 0;

unwind:
	while (--i >= 0) {
		skb_frag_t *fp = &sp->frags[i];

		dma_unmap_page(dev, sp->dma_maps[i + 1],
			       fp->size, dir);
	}
	dma_unmap_single(dev, sp->dma_maps[0],
			 skb_headlen(skb), dir);
out_err:
	return -ENOMEM;
}
EXPORT_SYMBOL(skb_dma_map);

void skb_dma_unmap(struct device *dev, struct sk_buff *skb,
		   enum dma_data_direction dir)
{
	struct skb_shared_info *sp = skb_shinfo(skb);
	int i;

	dma_unmap_single(dev, sp->dma_maps[0],
			 skb_headlen(skb), dir);
	for (i = 0; i < sp->nr_frags; i++) {
		skb_frag_t *fp = &sp->frags[i];

		dma_unmap_page(dev, sp->dma_maps[i + 1],
			       fp->size, dir);
	}
}
EXPORT_SYMBOL(skb_dma_unmap);