Blame view

drivers/dma/dmatest.c 17.6 KB
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
1
2
3
4
5
6
7
8
9
10
  /*
   * DMA Engine test module
   *
   * Copyright (C) 2007 Atmel Corporation
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   */
  #include <linux/delay.h>
b7f080cfe   Alexey Dobriyan   net: remove mm.h ...
11
  #include <linux/dma-mapping.h>
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
12
  #include <linux/dmaengine.h>
981ed70d8   Guennadi Liakhovetski   dmatest: make dma...
13
  #include <linux/freezer.h>
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
14
15
16
17
18
  #include <linux/init.h>
  #include <linux/kthread.h>
  #include <linux/module.h>
  #include <linux/moduleparam.h>
  #include <linux/random.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/slab.h>
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
20
21
22
23
24
  #include <linux/wait.h>
  
  static unsigned int test_buf_size = 16384;
  module_param(test_buf_size, uint, S_IRUGO);
  MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer");
06190d841   Kay Sievers   dmaengine: struct...
25
  static char test_channel[20];
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
26
27
  module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO);
  MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)");
06190d841   Kay Sievers   dmaengine: struct...
28
  static char test_device[20];
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
29
30
31
32
33
34
35
36
37
38
  module_param_string(device, test_device, sizeof(test_device), S_IRUGO);
  MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)");
  
  static unsigned int threads_per_chan = 1;
  module_param(threads_per_chan, uint, S_IRUGO);
  MODULE_PARM_DESC(threads_per_chan,
  		"Number of threads to start per channel (default: 1)");
  
  static unsigned int max_channels;
  module_param(max_channels, uint, S_IRUGO);
33df8ca06   Dan Williams   dmatest: convert ...
39
  MODULE_PARM_DESC(max_channels,
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
40
  		"Maximum number of channels to use (default: all)");
0a2ff57d6   Nicolas Ferre   dmaengine: dmates...
41
42
43
44
  static unsigned int iterations;
  module_param(iterations, uint, S_IRUGO);
  MODULE_PARM_DESC(iterations,
  		"Iterations before stopping test (default: infinite)");
b54d5cb91   Dan Williams   dmatest: add xor ...
45
46
47
48
  static unsigned int xor_sources = 3;
  module_param(xor_sources, uint, S_IRUGO);
  MODULE_PARM_DESC(xor_sources,
  		"Number of xor source buffers (default: 3)");
58691d64c   Dan Williams   dmatest: add pq s...
49
50
51
52
  static unsigned int pq_sources = 3;
  module_param(pq_sources, uint, S_IRUGO);
  MODULE_PARM_DESC(pq_sources,
  		"Number of p+q source buffers (default: 3)");
d42efe6bf   Viresh Kumar   dmaengine/dmatest...
53
54
  static int timeout = 3000;
  module_param(timeout, uint, S_IRUGO);
85ee7a1d3   Joe Perches   treewide: cleanup...
55
56
  MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), "
  		 "Pass -1 for infinite timeout");
d42efe6bf   Viresh Kumar   dmaengine/dmatest...
57

4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  /*
   * Initialization patterns. All bytes in the source buffer has bit 7
   * set, all bytes in the destination buffer has bit 7 cleared.
   *
   * Bit 6 is set for all bytes which are to be copied by the DMA
   * engine. Bit 5 is set for all bytes which are to be overwritten by
   * the DMA engine.
   *
   * The remaining bits are the inverse of a counter which increments by
   * one for each byte address.
   */
  #define PATTERN_SRC		0x80
  #define PATTERN_DST		0x00
  #define PATTERN_COPY		0x40
  #define PATTERN_OVERWRITE	0x20
  #define PATTERN_COUNT_MASK	0x1f
  
  struct dmatest_thread {
  	struct list_head	node;
  	struct task_struct	*task;
  	struct dma_chan		*chan;
b54d5cb91   Dan Williams   dmatest: add xor ...
79
80
81
  	u8			**srcs;
  	u8			**dsts;
  	enum dma_transaction_type type;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
82
83
84
85
86
87
88
89
90
91
  };
  
  struct dmatest_chan {
  	struct list_head	node;
  	struct dma_chan		*chan;
  	struct list_head	threads;
  };
  
  /*
   * These are protected by dma_list_mutex since they're only used by
33df8ca06   Dan Williams   dmatest: convert ...
92
   * the DMA filter function callback
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
93
94
95
96
97
98
99
100
   */
  static LIST_HEAD(dmatest_channels);
  static unsigned int nr_channels;
  
  static bool dmatest_match_channel(struct dma_chan *chan)
  {
  	if (test_channel[0] == '\0')
  		return true;
41d5e59c1   Dan Williams   dmaengine: add a ...
101
  	return strcmp(dma_chan_name(chan), test_channel) == 0;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
102
103
104
105
106
107
  }
  
  static bool dmatest_match_device(struct dma_device *device)
  {
  	if (test_device[0] == '\0')
  		return true;
06190d841   Kay Sievers   dmaengine: struct...
108
  	return strcmp(dev_name(device->dev), test_device) == 0;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
109
110
111
112
113
114
115
116
117
  }
  
  static unsigned long dmatest_random(void)
  {
  	unsigned long buf;
  
  	get_random_bytes(&buf, sizeof(buf));
  	return buf;
  }
b54d5cb91   Dan Williams   dmatest: add xor ...
118
  static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len)
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
119
120
  {
  	unsigned int i;
b54d5cb91   Dan Williams   dmatest: add xor ...
121
122
123
124
125
126
127
  	u8 *buf;
  
  	for (; (buf = *bufs); bufs++) {
  		for (i = 0; i < start; i++)
  			buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  		for ( ; i < start + len; i++)
  			buf[i] = PATTERN_SRC | PATTERN_COPY
c019894ef   Joe Perches   drivers/dma: Remo...
128
  				| (~i & PATTERN_COUNT_MASK);
b54d5cb91   Dan Williams   dmatest: add xor ...
129
130
131
132
  		for ( ; i < test_buf_size; i++)
  			buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK);
  		buf++;
  	}
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
133
  }
b54d5cb91   Dan Williams   dmatest: add xor ...
134
  static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len)
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
135
136
  {
  	unsigned int i;
b54d5cb91   Dan Williams   dmatest: add xor ...
137
138
139
140
141
142
143
144
145
146
147
  	u8 *buf;
  
  	for (; (buf = *bufs); bufs++) {
  		for (i = 0; i < start; i++)
  			buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  		for ( ; i < start + len; i++)
  			buf[i] = PATTERN_DST | PATTERN_OVERWRITE
  				| (~i & PATTERN_COUNT_MASK);
  		for ( ; i < test_buf_size; i++)
  			buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK);
  	}
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  }
  
  static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index,
  		unsigned int counter, bool is_srcbuf)
  {
  	u8		diff = actual ^ pattern;
  	u8		expected = pattern | (~counter & PATTERN_COUNT_MASK);
  	const char	*thread_name = current->comm;
  
  	if (is_srcbuf)
  		pr_warning("%s: srcbuf[0x%x] overwritten!"
  				" Expected %02x, got %02x
  ",
  				thread_name, index, expected, actual);
  	else if ((pattern & PATTERN_COPY)
  			&& (diff & (PATTERN_COPY | PATTERN_OVERWRITE)))
  		pr_warning("%s: dstbuf[0x%x] not copied!"
  				" Expected %02x, got %02x
  ",
  				thread_name, index, expected, actual);
  	else if (diff & PATTERN_SRC)
  		pr_warning("%s: dstbuf[0x%x] was copied!"
  				" Expected %02x, got %02x
  ",
  				thread_name, index, expected, actual);
  	else
  		pr_warning("%s: dstbuf[0x%x] mismatch!"
  				" Expected %02x, got %02x
  ",
  				thread_name, index, expected, actual);
  }
b54d5cb91   Dan Williams   dmatest: add xor ...
179
  static unsigned int dmatest_verify(u8 **bufs, unsigned int start,
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
180
181
182
183
184
185
  		unsigned int end, unsigned int counter, u8 pattern,
  		bool is_srcbuf)
  {
  	unsigned int i;
  	unsigned int error_count = 0;
  	u8 actual;
b54d5cb91   Dan Williams   dmatest: add xor ...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  	u8 expected;
  	u8 *buf;
  	unsigned int counter_orig = counter;
  
  	for (; (buf = *bufs); bufs++) {
  		counter = counter_orig;
  		for (i = start; i < end; i++) {
  			actual = buf[i];
  			expected = pattern | (~counter & PATTERN_COUNT_MASK);
  			if (actual != expected) {
  				if (error_count < 32)
  					dmatest_mismatch(actual, pattern, i,
  							 counter, is_srcbuf);
  				error_count++;
  			}
  			counter++;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
202
  		}
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
203
204
205
206
207
208
209
210
211
  	}
  
  	if (error_count > 32)
  		pr_warning("%s: %u errors suppressed
  ",
  			current->comm, error_count - 32);
  
  	return error_count;
  }
adfa543e7   Tejun Heo   dmatest: don't us...
212
213
214
215
216
217
218
  /* poor man's completion - we want to use wait_event_freezable() on it */
  struct dmatest_done {
  	bool			done;
  	wait_queue_head_t	*wait;
  };
  
  static void dmatest_callback(void *arg)
e44e0aa3c   Dan Williams   dmatest: add dma ...
219
  {
adfa543e7   Tejun Heo   dmatest: don't us...
220
221
222
223
  	struct dmatest_done *done = arg;
  
  	done->done = true;
  	wake_up_all(done->wait);
e44e0aa3c   Dan Williams   dmatest: add dma ...
224
  }
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
225
226
  /*
   * This function repeatedly tests DMA transfers of various lengths and
b54d5cb91   Dan Williams   dmatest: add xor ...
227
228
229
230
   * offsets for a given operation type until it is told to exit by
   * kthread_stop(). There may be multiple threads running this function
   * in parallel for a single channel, and there may be multiple channels
   * being tested in parallel.
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
231
232
233
234
235
236
237
238
239
240
   *
   * Before each test, the source and destination buffer is initialized
   * with a known pattern. This pattern is different depending on
   * whether it's in an area which is supposed to be copied or
   * overwritten, and different in the source and destination buffers.
   * So if the DMA engine doesn't copy exactly what we tell it to copy,
   * we'll notice.
   */
  static int dmatest_func(void *data)
  {
adfa543e7   Tejun Heo   dmatest: don't us...
241
  	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
242
  	struct dmatest_thread	*thread = data;
adfa543e7   Tejun Heo   dmatest: don't us...
243
  	struct dmatest_done	done = { .wait = &done_wait };
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
244
245
246
247
248
249
250
251
  	struct dma_chan		*chan;
  	const char		*thread_name;
  	unsigned int		src_off, dst_off, len;
  	unsigned int		error_count;
  	unsigned int		failed_tests = 0;
  	unsigned int		total_tests = 0;
  	dma_cookie_t		cookie;
  	enum dma_status		status;
b54d5cb91   Dan Williams   dmatest: add xor ...
252
  	enum dma_ctrl_flags 	flags;
94de648d7   Anatolij Gustschin   dmatest: correct ...
253
  	u8			pq_coefs[pq_sources + 1];
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
254
  	int			ret;
b54d5cb91   Dan Williams   dmatest: add xor ...
255
256
257
  	int			src_cnt;
  	int			dst_cnt;
  	int			i;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
258
259
  
  	thread_name = current->comm;
adfa543e7   Tejun Heo   dmatest: don't us...
260
  	set_freezable();
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
261
262
  
  	ret = -ENOMEM;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
263
264
265
  
  	smp_rmb();
  	chan = thread->chan;
b54d5cb91   Dan Williams   dmatest: add xor ...
266
267
268
269
270
  	if (thread->type == DMA_MEMCPY)
  		src_cnt = dst_cnt = 1;
  	else if (thread->type == DMA_XOR) {
  		src_cnt = xor_sources | 1; /* force odd to ensure dst = src */
  		dst_cnt = 1;
58691d64c   Dan Williams   dmatest: add pq s...
271
272
273
  	} else if (thread->type == DMA_PQ) {
  		src_cnt = pq_sources | 1; /* force odd to ensure dst = src */
  		dst_cnt = 2;
94de648d7   Anatolij Gustschin   dmatest: correct ...
274
  		for (i = 0; i < src_cnt; i++)
58691d64c   Dan Williams   dmatest: add pq s...
275
  			pq_coefs[i] = 1;
b54d5cb91   Dan Williams   dmatest: add xor ...
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  	} else
  		goto err_srcs;
  
  	thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL);
  	if (!thread->srcs)
  		goto err_srcs;
  	for (i = 0; i < src_cnt; i++) {
  		thread->srcs[i] = kmalloc(test_buf_size, GFP_KERNEL);
  		if (!thread->srcs[i])
  			goto err_srcbuf;
  	}
  	thread->srcs[i] = NULL;
  
  	thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL);
  	if (!thread->dsts)
  		goto err_dsts;
  	for (i = 0; i < dst_cnt; i++) {
  		thread->dsts[i] = kmalloc(test_buf_size, GFP_KERNEL);
  		if (!thread->dsts[i])
  			goto err_dstbuf;
  	}
  	thread->dsts[i] = NULL;
e44e0aa3c   Dan Williams   dmatest: add dma ...
298
  	set_user_nice(current, 10);
b203bd3f6   Ira Snyder   dmatest: fix auto...
299
300
301
302
303
304
  	/*
  	 * src buffers are freed by the DMAEngine code with dma_unmap_single()
  	 * dst buffers are freed by ourselves below
  	 */
  	flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT
  	      | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
305

0a2ff57d6   Nicolas Ferre   dmaengine: dmates...
306
307
  	while (!kthread_should_stop()
  	       && !(iterations && total_tests >= iterations)) {
d86be86e9   Atsushi Nemoto   dmatest: Use cust...
308
  		struct dma_device *dev = chan->device;
b54d5cb91   Dan Williams   dmatest: add xor ...
309
310
311
  		struct dma_async_tx_descriptor *tx = NULL;
  		dma_addr_t dma_srcs[src_cnt];
  		dma_addr_t dma_dsts[dst_cnt];
83544ae9f   Dan Williams   dmaengine, async_...
312
  		u8 align = 0;
d86be86e9   Atsushi Nemoto   dmatest: Use cust...
313

4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
314
  		total_tests++;
83544ae9f   Dan Williams   dmaengine, async_...
315
316
317
318
319
320
321
  		/* honor alignment restrictions */
  		if (thread->type == DMA_MEMCPY)
  			align = dev->copy_align;
  		else if (thread->type == DMA_XOR)
  			align = dev->xor_align;
  		else if (thread->type == DMA_PQ)
  			align = dev->pq_align;
cfe4f2751   Guennadi Liakhovetski   dmaengine: fix dm...
322
323
324
325
326
327
328
329
  		if (1 << align > test_buf_size) {
  			pr_err("%u-byte buffer too small for %d-byte alignment
  ",
  			       test_buf_size, 1 << align);
  			break;
  		}
  
  		len = dmatest_random() % test_buf_size + 1;
83544ae9f   Dan Williams   dmaengine, async_...
330
  		len = (len >> align) << align;
cfe4f2751   Guennadi Liakhovetski   dmaengine: fix dm...
331
332
333
334
  		if (!len)
  			len = 1 << align;
  		src_off = dmatest_random() % (test_buf_size - len + 1);
  		dst_off = dmatest_random() % (test_buf_size - len + 1);
83544ae9f   Dan Williams   dmaengine, async_...
335
336
  		src_off = (src_off >> align) << align;
  		dst_off = (dst_off >> align) << align;
b54d5cb91   Dan Williams   dmatest: add xor ...
337
338
  		dmatest_init_srcs(thread->srcs, src_off, len);
  		dmatest_init_dsts(thread->dsts, dst_off, len);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
339

b54d5cb91   Dan Williams   dmatest: add xor ...
340
341
342
343
344
345
  		for (i = 0; i < src_cnt; i++) {
  			u8 *buf = thread->srcs[i] + src_off;
  
  			dma_srcs[i] = dma_map_single(dev->dev, buf, len,
  						     DMA_TO_DEVICE);
  		}
d86be86e9   Atsushi Nemoto   dmatest: Use cust...
346
  		/* map with DMA_BIDIRECTIONAL to force writeback/invalidate */
b54d5cb91   Dan Williams   dmatest: add xor ...
347
348
349
350
351
  		for (i = 0; i < dst_cnt; i++) {
  			dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i],
  						     test_buf_size,
  						     DMA_BIDIRECTIONAL);
  		}
83544ae9f   Dan Williams   dmaengine, async_...
352

b54d5cb91   Dan Williams   dmatest: add xor ...
353
354
355
356
357
358
359
360
  		if (thread->type == DMA_MEMCPY)
  			tx = dev->device_prep_dma_memcpy(chan,
  							 dma_dsts[0] + dst_off,
  							 dma_srcs[0], len,
  							 flags);
  		else if (thread->type == DMA_XOR)
  			tx = dev->device_prep_dma_xor(chan,
  						      dma_dsts[0] + dst_off,
67b9124f7   Dan Williams   dmatest: fix hand...
361
  						      dma_srcs, src_cnt,
b54d5cb91   Dan Williams   dmatest: add xor ...
362
  						      len, flags);
58691d64c   Dan Williams   dmatest: add pq s...
363
364
365
366
367
368
  		else if (thread->type == DMA_PQ) {
  			dma_addr_t dma_pq[dst_cnt];
  
  			for (i = 0; i < dst_cnt; i++)
  				dma_pq[i] = dma_dsts[i] + dst_off;
  			tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs,
94de648d7   Anatolij Gustschin   dmatest: correct ...
369
  						     src_cnt, pq_coefs,
58691d64c   Dan Williams   dmatest: add pq s...
370
371
  						     len, flags);
  		}
d86be86e9   Atsushi Nemoto   dmatest: Use cust...
372

d86be86e9   Atsushi Nemoto   dmatest: Use cust...
373
  		if (!tx) {
b54d5cb91   Dan Williams   dmatest: add xor ...
374
375
376
377
378
379
380
  			for (i = 0; i < src_cnt; i++)
  				dma_unmap_single(dev->dev, dma_srcs[i], len,
  						 DMA_TO_DEVICE);
  			for (i = 0; i < dst_cnt; i++)
  				dma_unmap_single(dev->dev, dma_dsts[i],
  						 test_buf_size,
  						 DMA_BIDIRECTIONAL);
d86be86e9   Atsushi Nemoto   dmatest: Use cust...
381
382
383
384
385
386
387
388
389
  			pr_warning("%s: #%u: prep error with src_off=0x%x "
  					"dst_off=0x%x len=0x%x
  ",
  					thread_name, total_tests - 1,
  					src_off, dst_off, len);
  			msleep(100);
  			failed_tests++;
  			continue;
  		}
e44e0aa3c   Dan Williams   dmatest: add dma ...
390

adfa543e7   Tejun Heo   dmatest: don't us...
391
  		done.done = false;
e44e0aa3c   Dan Williams   dmatest: add dma ...
392
  		tx->callback = dmatest_callback;
adfa543e7   Tejun Heo   dmatest: don't us...
393
  		tx->callback_param = &done;
d86be86e9   Atsushi Nemoto   dmatest: Use cust...
394
  		cookie = tx->tx_submit(tx);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
395
396
397
398
399
400
401
402
403
404
  		if (dma_submit_error(cookie)) {
  			pr_warning("%s: #%u: submit error %d with src_off=0x%x "
  					"dst_off=0x%x len=0x%x
  ",
  					thread_name, total_tests - 1, cookie,
  					src_off, dst_off, len);
  			msleep(100);
  			failed_tests++;
  			continue;
  		}
b54d5cb91   Dan Williams   dmatest: add xor ...
405
  		dma_async_issue_pending(chan);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
406

adfa543e7   Tejun Heo   dmatest: don't us...
407
408
  		wait_event_freezable_timeout(done_wait, done.done,
  					     msecs_to_jiffies(timeout));
981ed70d8   Guennadi Liakhovetski   dmatest: make dma...
409

e44e0aa3c   Dan Williams   dmatest: add dma ...
410
  		status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
411

adfa543e7   Tejun Heo   dmatest: don't us...
412
413
414
415
416
417
418
419
420
  		if (!done.done) {
  			/*
  			 * We're leaving the timed out dma operation with
  			 * dangling pointer to done_wait.  To make this
  			 * correct, we'll need to allocate wait_done for
  			 * each test iteration and perform "who's gonna
  			 * free it this time?" dancing.  For now, just
  			 * leave it dangling.
  			 */
e44e0aa3c   Dan Williams   dmatest: add dma ...
421
422
423
424
425
426
427
428
429
430
431
  			pr_warning("%s: #%u: test timed out
  ",
  				   thread_name, total_tests - 1);
  			failed_tests++;
  			continue;
  		} else if (status != DMA_SUCCESS) {
  			pr_warning("%s: #%u: got completion callback,"
  				   " but status is \'%s\'
  ",
  				   thread_name, total_tests - 1,
  				   status == DMA_ERROR ? "error" : "in progress");
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
432
433
434
  			failed_tests++;
  			continue;
  		}
e44e0aa3c   Dan Williams   dmatest: add dma ...
435

d86be86e9   Atsushi Nemoto   dmatest: Use cust...
436
  		/* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */
b54d5cb91   Dan Williams   dmatest: add xor ...
437
438
439
  		for (i = 0; i < dst_cnt; i++)
  			dma_unmap_single(dev->dev, dma_dsts[i], test_buf_size,
  					 DMA_BIDIRECTIONAL);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
440
441
442
443
444
  
  		error_count = 0;
  
  		pr_debug("%s: verifying source buffer...
  ", thread_name);
b54d5cb91   Dan Williams   dmatest: add xor ...
445
  		error_count += dmatest_verify(thread->srcs, 0, src_off,
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
446
  				0, PATTERN_SRC, true);
b54d5cb91   Dan Williams   dmatest: add xor ...
447
  		error_count += dmatest_verify(thread->srcs, src_off,
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
448
449
  				src_off + len, src_off,
  				PATTERN_SRC | PATTERN_COPY, true);
b54d5cb91   Dan Williams   dmatest: add xor ...
450
  		error_count += dmatest_verify(thread->srcs, src_off + len,
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
451
452
453
454
455
456
  				test_buf_size, src_off + len,
  				PATTERN_SRC, true);
  
  		pr_debug("%s: verifying dest buffer...
  ",
  				thread->task->comm);
b54d5cb91   Dan Williams   dmatest: add xor ...
457
  		error_count += dmatest_verify(thread->dsts, 0, dst_off,
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
458
  				0, PATTERN_DST, false);
b54d5cb91   Dan Williams   dmatest: add xor ...
459
  		error_count += dmatest_verify(thread->dsts, dst_off,
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
460
461
  				dst_off + len, src_off,
  				PATTERN_SRC | PATTERN_COPY, false);
b54d5cb91   Dan Williams   dmatest: add xor ...
462
  		error_count += dmatest_verify(thread->dsts, dst_off + len,
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  				test_buf_size, dst_off + len,
  				PATTERN_DST, false);
  
  		if (error_count) {
  			pr_warning("%s: #%u: %u errors with "
  				"src_off=0x%x dst_off=0x%x len=0x%x
  ",
  				thread_name, total_tests - 1, error_count,
  				src_off, dst_off, len);
  			failed_tests++;
  		} else {
  			pr_debug("%s: #%u: No errors with "
  				"src_off=0x%x dst_off=0x%x len=0x%x
  ",
  				thread_name, total_tests - 1,
  				src_off, dst_off, len);
  		}
  	}
  
  	ret = 0;
b54d5cb91   Dan Williams   dmatest: add xor ...
483
484
  	for (i = 0; thread->dsts[i]; i++)
  		kfree(thread->dsts[i]);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
485
  err_dstbuf:
b54d5cb91   Dan Williams   dmatest: add xor ...
486
487
488
489
  	kfree(thread->dsts);
  err_dsts:
  	for (i = 0; thread->srcs[i]; i++)
  		kfree(thread->srcs[i]);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
490
  err_srcbuf:
b54d5cb91   Dan Williams   dmatest: add xor ...
491
492
  	kfree(thread->srcs);
  err_srcs:
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
493
494
495
  	pr_notice("%s: terminating after %u tests, %u failures (status %d)
  ",
  			thread_name, total_tests, failed_tests, ret);
0a2ff57d6   Nicolas Ferre   dmaengine: dmates...
496

9704efaa5   Viresh Kumar   dmaengine/dmatest...
497
498
  	/* terminate all transfers on specified channels */
  	chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
0a2ff57d6   Nicolas Ferre   dmaengine: dmates...
499
500
  	if (iterations > 0)
  		while (!kthread_should_stop()) {
b953df7c7   Yong Zhang   dmaengine: correc...
501
  			DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit);
0a2ff57d6   Nicolas Ferre   dmaengine: dmates...
502
503
  			interruptible_sleep_on(&wait_dmatest_exit);
  		}
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  	return ret;
  }
  
  static void dmatest_cleanup_channel(struct dmatest_chan *dtc)
  {
  	struct dmatest_thread	*thread;
  	struct dmatest_thread	*_thread;
  	int			ret;
  
  	list_for_each_entry_safe(thread, _thread, &dtc->threads, node) {
  		ret = kthread_stop(thread->task);
  		pr_debug("dmatest: thread %s exited with status %d
  ",
  				thread->task->comm, ret);
  		list_del(&thread->node);
  		kfree(thread);
  	}
9704efaa5   Viresh Kumar   dmaengine/dmatest...
521
522
523
  
  	/* terminate all transfers on specified channels */
  	dtc->chan->device->device_control(dtc->chan, DMA_TERMINATE_ALL, 0);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
524
525
  	kfree(dtc);
  }
b54d5cb91   Dan Williams   dmatest: add xor ...
526
  static int dmatest_add_threads(struct dmatest_chan *dtc, enum dma_transaction_type type)
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
527
  {
b54d5cb91   Dan Williams   dmatest: add xor ...
528
529
530
531
  	struct dmatest_thread *thread;
  	struct dma_chan *chan = dtc->chan;
  	char *op;
  	unsigned int i;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
532

b54d5cb91   Dan Williams   dmatest: add xor ...
533
534
535
536
  	if (type == DMA_MEMCPY)
  		op = "copy";
  	else if (type == DMA_XOR)
  		op = "xor";
58691d64c   Dan Williams   dmatest: add pq s...
537
538
  	else if (type == DMA_PQ)
  		op = "pq";
b54d5cb91   Dan Williams   dmatest: add xor ...
539
540
  	else
  		return -EINVAL;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
541
542
543
544
  
  	for (i = 0; i < threads_per_chan; i++) {
  		thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL);
  		if (!thread) {
b54d5cb91   Dan Williams   dmatest: add xor ...
545
546
547
  			pr_warning("dmatest: No memory for %s-%s%u
  ",
  				   dma_chan_name(chan), op, i);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
548
549
550
  			break;
  		}
  		thread->chan = dtc->chan;
b54d5cb91   Dan Williams   dmatest: add xor ...
551
  		thread->type = type;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
552
  		smp_wmb();
b54d5cb91   Dan Williams   dmatest: add xor ...
553
554
  		thread->task = kthread_run(dmatest_func, thread, "%s-%s%u",
  				dma_chan_name(chan), op, i);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
555
  		if (IS_ERR(thread->task)) {
b54d5cb91   Dan Williams   dmatest: add xor ...
556
557
558
  			pr_warning("dmatest: Failed to run thread %s-%s%u
  ",
  					dma_chan_name(chan), op, i);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
559
560
561
562
563
564
565
566
  			kfree(thread);
  			break;
  		}
  
  		/* srcbuf and dstbuf are allocated by the thread itself */
  
  		list_add_tail(&thread->node, &dtc->threads);
  	}
b54d5cb91   Dan Williams   dmatest: add xor ...
567
568
569
570
571
572
573
574
  	return i;
  }
  
  static int dmatest_add_channel(struct dma_chan *chan)
  {
  	struct dmatest_chan	*dtc;
  	struct dma_device	*dma_dev = chan->device;
  	unsigned int		thread_count = 0;
b9033e682   Kulikov Vasiliy   dma: dmatest: fix...
575
  	int cnt;
b54d5cb91   Dan Williams   dmatest: add xor ...
576
577
578
579
580
581
582
583
584
585
586
587
588
  
  	dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL);
  	if (!dtc) {
  		pr_warning("dmatest: No memory for %s
  ", dma_chan_name(chan));
  		return -ENOMEM;
  	}
  
  	dtc->chan = chan;
  	INIT_LIST_HEAD(&dtc->threads);
  
  	if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  		cnt = dmatest_add_threads(dtc, DMA_MEMCPY);
f1aef8b6e   Nicolas Ferre   dmaengine: dmates...
589
  		thread_count += cnt > 0 ? cnt : 0;
b54d5cb91   Dan Williams   dmatest: add xor ...
590
591
592
  	}
  	if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  		cnt = dmatest_add_threads(dtc, DMA_XOR);
f1aef8b6e   Nicolas Ferre   dmaengine: dmates...
593
  		thread_count += cnt > 0 ? cnt : 0;
b54d5cb91   Dan Williams   dmatest: add xor ...
594
  	}
58691d64c   Dan Williams   dmatest: add pq s...
595
596
597
598
  	if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
  		cnt = dmatest_add_threads(dtc, DMA_PQ);
  		thread_count += cnt > 0 ?: 0;
  	}
b54d5cb91   Dan Williams   dmatest: add xor ...
599
600
601
602
  
  	pr_info("dmatest: Started %u threads using %s
  ",
  		thread_count, dma_chan_name(chan));
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
603
604
605
  
  	list_add_tail(&dtc->node, &dmatest_channels);
  	nr_channels++;
33df8ca06   Dan Williams   dmatest: convert ...
606
  	return 0;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
607
  }
7dd602510   Dan Williams   dmaengine: kill e...
608
  static bool filter(struct dma_chan *chan, void *param)
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
609
  {
33df8ca06   Dan Williams   dmatest: convert ...
610
  	if (!dmatest_match_channel(chan) || !dmatest_match_device(chan->device))
7dd602510   Dan Williams   dmaengine: kill e...
611
  		return false;
33df8ca06   Dan Williams   dmatest: convert ...
612
  	else
7dd602510   Dan Williams   dmaengine: kill e...
613
  		return true;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
614
  }
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
615
616
  static int __init dmatest_init(void)
  {
33df8ca06   Dan Williams   dmatest: convert ...
617
618
619
620
621
622
623
624
625
626
  	dma_cap_mask_t mask;
  	struct dma_chan *chan;
  	int err = 0;
  
  	dma_cap_zero(mask);
  	dma_cap_set(DMA_MEMCPY, mask);
  	for (;;) {
  		chan = dma_request_channel(mask, filter, NULL);
  		if (chan) {
  			err = dmatest_add_channel(chan);
c56c81abe   Dan Williams   dmatest: fix max ...
627
  			if (err) {
33df8ca06   Dan Williams   dmatest: convert ...
628
629
630
631
632
633
634
635
  				dma_release_channel(chan);
  				break; /* add_channel failed, punt */
  			}
  		} else
  			break; /* no more channels available */
  		if (max_channels && nr_channels >= max_channels)
  			break; /* we have all we need */
  	}
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
636

33df8ca06   Dan Williams   dmatest: convert ...
637
  	return err;
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
638
  }
33df8ca06   Dan Williams   dmatest: convert ...
639
640
  /* when compiled-in wait for drivers to load first */
  late_initcall(dmatest_init);
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
641
642
643
  
  static void __exit dmatest_exit(void)
  {
33df8ca06   Dan Williams   dmatest: convert ...
644
  	struct dmatest_chan *dtc, *_dtc;
7cbd4877e   Dan Williams   dmatest: fix use ...
645
  	struct dma_chan *chan;
33df8ca06   Dan Williams   dmatest: convert ...
646
647
648
  
  	list_for_each_entry_safe(dtc, _dtc, &dmatest_channels, node) {
  		list_del(&dtc->node);
7cbd4877e   Dan Williams   dmatest: fix use ...
649
  		chan = dtc->chan;
33df8ca06   Dan Williams   dmatest: convert ...
650
651
652
  		dmatest_cleanup_channel(dtc);
  		pr_debug("dmatest: dropped channel %s
  ",
7cbd4877e   Dan Williams   dmatest: fix use ...
653
654
  			 dma_chan_name(chan));
  		dma_release_channel(chan);
33df8ca06   Dan Williams   dmatest: convert ...
655
  	}
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
656
657
  }
  module_exit(dmatest_exit);
e05503ef1   Jean Delvare   Haavard Skinnemoe...
658
  MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
4a776f0aa   Haavard Skinnemoen   dmatest: Simple D...
659
  MODULE_LICENSE("GPL v2");