xref: /linux/crypto/async_tx/raid6test.c (revision d39d0ed196aa1685bb24771e92f78633c66ac9cb)
1 /*
2  * asynchronous raid6 recovery self test
3  * Copyright (c) 2009, Intel Corporation.
4  *
5  * based on drivers/md/raid6test/test.c:
6  * 	Copyright 2002-2007 H. Peter Anvin
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22 #include <linux/async_tx.h>
23 #include <linux/gfp.h>
24 #include <linux/random.h>
25 
26 #undef pr
27 #define pr(fmt, args...) pr_info("raid6test: " fmt, ##args)
28 
29 #define NDISKS 16 /* Including P and Q */
30 
31 static struct page *dataptrs[NDISKS];
32 static addr_conv_t addr_conv[NDISKS];
33 static struct page *data[NDISKS+3];
34 static struct page *spare;
35 static struct page *recovi;
36 static struct page *recovj;
37 
38 static void callback(void *param)
39 {
40 	struct completion *cmp = param;
41 
42 	complete(cmp);
43 }
44 
45 static void makedata(int disks)
46 {
47 	int i, j;
48 
49 	for (i = 0; i < disks; i++) {
50 		for (j = 0; j < PAGE_SIZE/sizeof(u32); j += sizeof(u32)) {
51 			u32 *p = page_address(data[i]) + j;
52 
53 			*p = random32();
54 		}
55 
56 		dataptrs[i] = data[i];
57 	}
58 }
59 
60 static char disk_type(int d, int disks)
61 {
62 	if (d == disks - 2)
63 		return 'P';
64 	else if (d == disks - 1)
65 		return 'Q';
66 	else
67 		return 'D';
68 }
69 
70 /* Recover two failed blocks. */
71 static void raid6_dual_recov(int disks, size_t bytes, int faila, int failb, struct page **ptrs)
72 {
73 	struct async_submit_ctl submit;
74 	struct completion cmp;
75 	struct dma_async_tx_descriptor *tx = NULL;
76 	enum sum_check_flags result = ~0;
77 
78 	if (faila > failb)
79 		swap(faila, failb);
80 
81 	if (failb == disks-1) {
82 		if (faila == disks-2) {
83 			/* P+Q failure.  Just rebuild the syndrome. */
84 			init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
85 			tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
86 		} else {
87 			struct page *blocks[disks];
88 			struct page *dest;
89 			int count = 0;
90 			int i;
91 
92 			/* data+Q failure.  Reconstruct data from P,
93 			 * then rebuild syndrome
94 			 */
95 			for (i = disks; i-- ; ) {
96 				if (i == faila || i == failb)
97 					continue;
98 				blocks[count++] = ptrs[i];
99 			}
100 			dest = ptrs[faila];
101 			init_async_submit(&submit, ASYNC_TX_XOR_ZERO_DST, NULL,
102 					  NULL, NULL, addr_conv);
103 			tx = async_xor(dest, blocks, 0, count, bytes, &submit);
104 
105 			init_async_submit(&submit, 0, tx, NULL, NULL, addr_conv);
106 			tx = async_gen_syndrome(ptrs, 0, disks, bytes, &submit);
107 		}
108 	} else {
109 		if (failb == disks-2) {
110 			/* data+P failure. */
111 			init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
112 			tx = async_raid6_datap_recov(disks, bytes, faila, ptrs, &submit);
113 		} else {
114 			/* data+data failure. */
115 			init_async_submit(&submit, 0, NULL, NULL, NULL, addr_conv);
116 			tx = async_raid6_2data_recov(disks, bytes, faila, failb, ptrs, &submit);
117 		}
118 	}
119 	init_completion(&cmp);
120 	init_async_submit(&submit, ASYNC_TX_ACK, tx, callback, &cmp, addr_conv);
121 	tx = async_syndrome_val(ptrs, 0, disks, bytes, &result, spare, &submit);
122 	async_tx_issue_pending(tx);
123 
124 	if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0)
125 		pr("%s: timeout! (faila: %d failb: %d disks: %d)\n",
126 		   __func__, faila, failb, disks);
127 
128 	if (result != 0)
129 		pr("%s: validation failure! faila: %d failb: %d sum_check_flags: %x\n",
130 		   __func__, faila, failb, result);
131 }
132 
133 static int test_disks(int i, int j, int disks)
134 {
135 	int erra, errb;
136 
137 	memset(page_address(recovi), 0xf0, PAGE_SIZE);
138 	memset(page_address(recovj), 0xba, PAGE_SIZE);
139 
140 	dataptrs[i] = recovi;
141 	dataptrs[j] = recovj;
142 
143 	raid6_dual_recov(disks, PAGE_SIZE, i, j, dataptrs);
144 
145 	erra = memcmp(page_address(data[i]), page_address(recovi), PAGE_SIZE);
146 	errb = memcmp(page_address(data[j]), page_address(recovj), PAGE_SIZE);
147 
148 	pr("%s(%d, %d): faila=%3d(%c)  failb=%3d(%c)  %s\n",
149 	   __func__, i, j, i, disk_type(i, disks), j, disk_type(j, disks),
150 	   (!erra && !errb) ? "OK" : !erra ? "ERRB" : !errb ? "ERRA" : "ERRAB");
151 
152 	dataptrs[i] = data[i];
153 	dataptrs[j] = data[j];
154 
155 	return erra || errb;
156 }
157 
158 static int test(int disks, int *tests)
159 {
160 	struct dma_async_tx_descriptor *tx;
161 	struct async_submit_ctl submit;
162 	struct completion cmp;
163 	int err = 0;
164 	int i, j;
165 
166 	recovi = data[disks];
167 	recovj = data[disks+1];
168 	spare  = data[disks+2];
169 
170 	makedata(disks);
171 
172 	/* Nuke syndromes */
173 	memset(page_address(data[disks-2]), 0xee, PAGE_SIZE);
174 	memset(page_address(data[disks-1]), 0xee, PAGE_SIZE);
175 
176 	/* Generate assumed good syndrome */
177 	init_completion(&cmp);
178 	init_async_submit(&submit, ASYNC_TX_ACK, NULL, callback, &cmp, addr_conv);
179 	tx = async_gen_syndrome(dataptrs, 0, disks, PAGE_SIZE, &submit);
180 	async_tx_issue_pending(tx);
181 
182 	if (wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000)) == 0) {
183 		pr("error: initial gen_syndrome(%d) timed out\n", disks);
184 		return 1;
185 	}
186 
187 	pr("testing the %d-disk case...\n", disks);
188 	for (i = 0; i < disks-1; i++)
189 		for (j = i+1; j < disks; j++) {
190 			(*tests)++;
191 			err += test_disks(i, j, disks);
192 		}
193 
194 	return err;
195 }
196 
197 
198 static int raid6_test(void)
199 {
200 	int err = 0;
201 	int tests = 0;
202 	int i;
203 
204 	for (i = 0; i < NDISKS+3; i++) {
205 		data[i] = alloc_page(GFP_KERNEL);
206 		if (!data[i]) {
207 			while (i--)
208 				put_page(data[i]);
209 			return -ENOMEM;
210 		}
211 	}
212 
213 	/* the 4-disk and 5-disk cases are special for the recovery code */
214 	if (NDISKS > 4)
215 		err += test(4, &tests);
216 	if (NDISKS > 5)
217 		err += test(5, &tests);
218 	/* the 11 and 12 disk cases are special for ioatdma (p-disabled
219 	 * q-continuation without extended descriptor)
220 	 */
221 	if (NDISKS > 12) {
222 		err += test(11, &tests);
223 		err += test(12, &tests);
224 	}
225 	err += test(NDISKS, &tests);
226 
227 	pr("\n");
228 	pr("complete (%d tests, %d failure%s)\n",
229 	   tests, err, err == 1 ? "" : "s");
230 
231 	for (i = 0; i < NDISKS+3; i++)
232 		put_page(data[i]);
233 
234 	return 0;
235 }
236 
237 static void raid6_test_exit(void)
238 {
239 }
240 
241 /* when compiled-in wait for drivers to load first (assumes dma drivers
242  * are also compliled-in)
243  */
244 late_initcall(raid6_test);
245 module_exit(raid6_test_exit);
246 MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
247 MODULE_DESCRIPTION("asynchronous RAID-6 recovery self tests");
248 MODULE_LICENSE("GPL");
249