WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / scatterlist / main.c
blob71c960dcd8a42fae5bbaf3d66418eb2d6d0ef465
1 #include <stdio.h>
2 #include <assert.h>
4 #include <linux/scatterlist.h>
6 #define MAX_PAGES (64)
8 struct test {
9 int alloc_ret;
10 unsigned num_pages;
11 unsigned *pfn;
12 unsigned *pfn_app;
13 unsigned size;
14 unsigned int max_seg;
15 unsigned int expected_segments;
18 static void set_pages(struct page **pages, const unsigned *array, unsigned num)
20 unsigned int i;
22 assert(num < MAX_PAGES);
23 for (i = 0; i < num; i++)
24 pages[i] = (struct page *)(unsigned long)
25 ((1 + array[i]) * PAGE_SIZE);
28 #define pfn(...) (unsigned []){ __VA_ARGS__ }
30 static void fail(struct test *test, struct sg_table *st, const char *cond)
32 unsigned int i;
34 fprintf(stderr, "Failed on '%s'!\n\n", cond);
36 printf("size = %u, max segment = %u, expected nents = %u\nst->nents = %u, st->orig_nents= %u\n",
37 test->size, test->max_seg, test->expected_segments, st->nents,
38 st->orig_nents);
40 printf("%u input PFNs:", test->num_pages);
41 for (i = 0; i < test->num_pages; i++)
42 printf(" %x", test->pfn[i]);
43 printf("\n");
45 exit(1);
48 #define VALIDATE(cond, st, test) \
49 if (!(cond)) \
50 fail((test), (st), #cond);
52 int main(void)
54 const unsigned int sgmax = UINT_MAX;
55 struct test *test, tests[] = {
56 { -EINVAL, 1, pfn(0), NULL, PAGE_SIZE, 0, 1 },
57 { 0, 1, pfn(0), NULL, PAGE_SIZE, PAGE_SIZE + 1, 1 },
58 { 0, 1, pfn(0), NULL, PAGE_SIZE, sgmax + 1, 1 },
59 { 0, 1, pfn(0), NULL, PAGE_SIZE, sgmax, 1 },
60 { 0, 1, pfn(0), NULL, 1, sgmax, 1 },
61 { 0, 2, pfn(0, 1), NULL, 2 * PAGE_SIZE, sgmax, 1 },
62 { 0, 2, pfn(1, 0), NULL, 2 * PAGE_SIZE, sgmax, 2 },
63 { 0, 3, pfn(0, 1, 2), NULL, 3 * PAGE_SIZE, sgmax, 1 },
64 { 0, 3, pfn(0, 1, 2), NULL, 3 * PAGE_SIZE, sgmax, 1 },
65 { 0, 3, pfn(0, 1, 2), pfn(3, 4, 5), 3 * PAGE_SIZE, sgmax, 1 },
66 { 0, 3, pfn(0, 1, 2), pfn(4, 5, 6), 3 * PAGE_SIZE, sgmax, 2 },
67 { 0, 3, pfn(0, 2, 1), NULL, 3 * PAGE_SIZE, sgmax, 3 },
68 { 0, 3, pfn(0, 1, 3), NULL, 3 * PAGE_SIZE, sgmax, 2 },
69 { 0, 3, pfn(1, 2, 4), NULL, 3 * PAGE_SIZE, sgmax, 2 },
70 { 0, 3, pfn(1, 3, 4), NULL, 3 * PAGE_SIZE, sgmax, 2 },
71 { 0, 4, pfn(0, 1, 3, 4), NULL, 4 * PAGE_SIZE, sgmax, 2 },
72 { 0, 5, pfn(0, 1, 3, 4, 5), NULL, 5 * PAGE_SIZE, sgmax, 2 },
73 { 0, 5, pfn(0, 1, 3, 4, 6), NULL, 5 * PAGE_SIZE, sgmax, 3 },
74 { 0, 5, pfn(0, 1, 2, 3, 4), NULL, 5 * PAGE_SIZE, sgmax, 1 },
75 { 0, 5, pfn(0, 1, 2, 3, 4), NULL, 5 * PAGE_SIZE, 2 * PAGE_SIZE,
76 3 },
77 { 0, 6, pfn(0, 1, 2, 3, 4, 5), NULL, 6 * PAGE_SIZE,
78 2 * PAGE_SIZE, 3 },
79 { 0, 6, pfn(0, 2, 3, 4, 5, 6), NULL, 6 * PAGE_SIZE,
80 2 * PAGE_SIZE, 4 },
81 { 0, 6, pfn(0, 1, 3, 4, 5, 6), pfn(7, 8, 9, 10, 11, 12),
82 6 * PAGE_SIZE, 12 * PAGE_SIZE, 2 },
83 { 0, 0, NULL, NULL, 0, 0, 0 },
85 unsigned int i;
87 for (i = 0, test = tests; test->expected_segments; test++, i++) {
88 int left_pages = test->pfn_app ? test->num_pages : 0;
89 struct page *pages[MAX_PAGES];
90 struct sg_table st;
91 struct scatterlist *sg;
93 set_pages(pages, test->pfn, test->num_pages);
95 sg = __sg_alloc_table_from_pages(&st, pages, test->num_pages, 0,
96 test->size, test->max_seg, NULL, left_pages, GFP_KERNEL);
97 assert(PTR_ERR_OR_ZERO(sg) == test->alloc_ret);
99 if (test->alloc_ret)
100 continue;
102 if (test->pfn_app) {
103 set_pages(pages, test->pfn_app, test->num_pages);
104 sg = __sg_alloc_table_from_pages(&st, pages, test->num_pages, 0,
105 test->size, test->max_seg, sg, 0, GFP_KERNEL);
107 assert(PTR_ERR_OR_ZERO(sg) == test->alloc_ret);
110 VALIDATE(st.nents == test->expected_segments, &st, test);
111 if (!test->pfn_app)
112 VALIDATE(st.orig_nents == test->expected_segments, &st, test);
114 sg_free_table(&st);
117 assert(i == (sizeof(tests) / sizeof(tests[0])) - 1);
119 return 0;