| 1 | /* |
| 2 | * DMA Engine test module |
| 3 | * |
| 4 | * Copyright (C) 2007 Atmel Corporation |
| 5 | * Copyright (C) 2013 Intel Corporation |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/dma-mapping.h> |
| 13 | #include <linux/dmaengine.h> |
| 14 | #include <linux/freezer.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kthread.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/moduleparam.h> |
| 19 | #include <linux/random.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/wait.h> |
| 22 | #include <linux/ctype.h> |
| 23 | #include <linux/debugfs.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/seq_file.h> |
| 26 | |
| 27 | static unsigned int test_buf_size = 16384; |
| 28 | module_param(test_buf_size, uint, S_IRUGO); |
| 29 | MODULE_PARM_DESC(test_buf_size, "Size of the memcpy test buffer"); |
| 30 | |
| 31 | static char test_channel[20]; |
| 32 | module_param_string(channel, test_channel, sizeof(test_channel), S_IRUGO); |
| 33 | MODULE_PARM_DESC(channel, "Bus ID of the channel to test (default: any)"); |
| 34 | |
| 35 | static char test_device[20]; |
| 36 | module_param_string(device, test_device, sizeof(test_device), S_IRUGO); |
| 37 | MODULE_PARM_DESC(device, "Bus ID of the DMA Engine to test (default: any)"); |
| 38 | |
| 39 | static unsigned int threads_per_chan = 1; |
| 40 | module_param(threads_per_chan, uint, S_IRUGO); |
| 41 | MODULE_PARM_DESC(threads_per_chan, |
| 42 | "Number of threads to start per channel (default: 1)"); |
| 43 | |
| 44 | static unsigned int max_channels; |
| 45 | module_param(max_channels, uint, S_IRUGO); |
| 46 | MODULE_PARM_DESC(max_channels, |
| 47 | "Maximum number of channels to use (default: all)"); |
| 48 | |
| 49 | static unsigned int iterations; |
| 50 | module_param(iterations, uint, S_IRUGO); |
| 51 | MODULE_PARM_DESC(iterations, |
| 52 | "Iterations before stopping test (default: infinite)"); |
| 53 | |
| 54 | static unsigned int xor_sources = 3; |
| 55 | module_param(xor_sources, uint, S_IRUGO); |
| 56 | MODULE_PARM_DESC(xor_sources, |
| 57 | "Number of xor source buffers (default: 3)"); |
| 58 | |
| 59 | static unsigned int pq_sources = 3; |
| 60 | module_param(pq_sources, uint, S_IRUGO); |
| 61 | MODULE_PARM_DESC(pq_sources, |
| 62 | "Number of p+q source buffers (default: 3)"); |
| 63 | |
| 64 | static int timeout = 3000; |
| 65 | module_param(timeout, uint, S_IRUGO); |
| 66 | MODULE_PARM_DESC(timeout, "Transfer Timeout in msec (default: 3000), " |
| 67 | "Pass -1 for infinite timeout"); |
| 68 | |
| 69 | /* Maximum amount of mismatched bytes in buffer to print */ |
| 70 | #define MAX_ERROR_COUNT 32 |
| 71 | |
| 72 | /* |
| 73 | * Initialization patterns. All bytes in the source buffer has bit 7 |
| 74 | * set, all bytes in the destination buffer has bit 7 cleared. |
| 75 | * |
| 76 | * Bit 6 is set for all bytes which are to be copied by the DMA |
| 77 | * engine. Bit 5 is set for all bytes which are to be overwritten by |
| 78 | * the DMA engine. |
| 79 | * |
| 80 | * The remaining bits are the inverse of a counter which increments by |
| 81 | * one for each byte address. |
| 82 | */ |
| 83 | #define PATTERN_SRC 0x80 |
| 84 | #define PATTERN_DST 0x00 |
| 85 | #define PATTERN_COPY 0x40 |
| 86 | #define PATTERN_OVERWRITE 0x20 |
| 87 | #define PATTERN_COUNT_MASK 0x1f |
| 88 | |
| 89 | enum dmatest_error_type { |
| 90 | DMATEST_ET_OK, |
| 91 | DMATEST_ET_MAP_SRC, |
| 92 | DMATEST_ET_MAP_DST, |
| 93 | DMATEST_ET_PREP, |
| 94 | DMATEST_ET_SUBMIT, |
| 95 | DMATEST_ET_TIMEOUT, |
| 96 | DMATEST_ET_DMA_ERROR, |
| 97 | DMATEST_ET_DMA_IN_PROGRESS, |
| 98 | DMATEST_ET_VERIFY, |
| 99 | }; |
| 100 | |
| 101 | struct dmatest_thread_result { |
| 102 | struct list_head node; |
| 103 | unsigned int n; |
| 104 | unsigned int src_off; |
| 105 | unsigned int dst_off; |
| 106 | unsigned int len; |
| 107 | enum dmatest_error_type type; |
| 108 | union { |
| 109 | unsigned long data; |
| 110 | dma_cookie_t cookie; |
| 111 | enum dma_status status; |
| 112 | int error; |
| 113 | }; |
| 114 | }; |
| 115 | |
| 116 | struct dmatest_result { |
| 117 | struct list_head node; |
| 118 | char *name; |
| 119 | struct list_head results; |
| 120 | }; |
| 121 | |
| 122 | struct dmatest_info; |
| 123 | |
| 124 | struct dmatest_thread { |
| 125 | struct list_head node; |
| 126 | struct dmatest_info *info; |
| 127 | struct task_struct *task; |
| 128 | struct dma_chan *chan; |
| 129 | u8 **srcs; |
| 130 | u8 **dsts; |
| 131 | enum dma_transaction_type type; |
| 132 | bool done; |
| 133 | }; |
| 134 | |
| 135 | struct dmatest_chan { |
| 136 | struct list_head node; |
| 137 | struct dma_chan *chan; |
| 138 | struct list_head threads; |
| 139 | }; |
| 140 | |
| 141 | /** |
| 142 | * struct dmatest_params - test parameters. |
| 143 | * @buf_size: size of the memcpy test buffer |
| 144 | * @channel: bus ID of the channel to test |
| 145 | * @device: bus ID of the DMA Engine to test |
| 146 | * @threads_per_chan: number of threads to start per channel |
| 147 | * @max_channels: maximum number of channels to use |
| 148 | * @iterations: iterations before stopping test |
| 149 | * @xor_sources: number of xor source buffers |
| 150 | * @pq_sources: number of p+q source buffers |
| 151 | * @timeout: transfer timeout in msec, -1 for infinite timeout |
| 152 | */ |
| 153 | struct dmatest_params { |
| 154 | unsigned int buf_size; |
| 155 | char channel[20]; |
| 156 | char device[20]; |
| 157 | unsigned int threads_per_chan; |
| 158 | unsigned int max_channels; |
| 159 | unsigned int iterations; |
| 160 | unsigned int xor_sources; |
| 161 | unsigned int pq_sources; |
| 162 | int timeout; |
| 163 | }; |
| 164 | |
| 165 | /** |
| 166 | * struct dmatest_info - test information. |
| 167 | * @params: test parameters |
| 168 | * @lock: access protection to the fields of this structure |
| 169 | */ |
| 170 | struct dmatest_info { |
| 171 | /* Test parameters */ |
| 172 | struct dmatest_params params; |
| 173 | |
| 174 | /* Internal state */ |
| 175 | struct list_head channels; |
| 176 | unsigned int nr_channels; |
| 177 | struct mutex lock; |
| 178 | |
| 179 | /* debugfs related stuff */ |
| 180 | struct dentry *root; |
| 181 | struct dmatest_params dbgfs_params; |
| 182 | |
| 183 | /* Test results */ |
| 184 | struct list_head results; |
| 185 | struct mutex results_lock; |
| 186 | }; |
| 187 | |
| 188 | static struct dmatest_info test_info; |
| 189 | |
| 190 | static bool dmatest_match_channel(struct dmatest_params *params, |
| 191 | struct dma_chan *chan) |
| 192 | { |
| 193 | if (params->channel[0] == '\0') |
| 194 | return true; |
| 195 | return strcmp(dma_chan_name(chan), params->channel) == 0; |
| 196 | } |
| 197 | |
| 198 | static bool dmatest_match_device(struct dmatest_params *params, |
| 199 | struct dma_device *device) |
| 200 | { |
| 201 | if (params->device[0] == '\0') |
| 202 | return true; |
| 203 | return strcmp(dev_name(device->dev), params->device) == 0; |
| 204 | } |
| 205 | |
| 206 | static unsigned long dmatest_random(void) |
| 207 | { |
| 208 | unsigned long buf; |
| 209 | |
| 210 | get_random_bytes(&buf, sizeof(buf)); |
| 211 | return buf; |
| 212 | } |
| 213 | |
| 214 | static void dmatest_init_srcs(u8 **bufs, unsigned int start, unsigned int len, |
| 215 | unsigned int buf_size) |
| 216 | { |
| 217 | unsigned int i; |
| 218 | u8 *buf; |
| 219 | |
| 220 | for (; (buf = *bufs); bufs++) { |
| 221 | for (i = 0; i < start; i++) |
| 222 | buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK); |
| 223 | for ( ; i < start + len; i++) |
| 224 | buf[i] = PATTERN_SRC | PATTERN_COPY |
| 225 | | (~i & PATTERN_COUNT_MASK); |
| 226 | for ( ; i < buf_size; i++) |
| 227 | buf[i] = PATTERN_SRC | (~i & PATTERN_COUNT_MASK); |
| 228 | buf++; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static void dmatest_init_dsts(u8 **bufs, unsigned int start, unsigned int len, |
| 233 | unsigned int buf_size) |
| 234 | { |
| 235 | unsigned int i; |
| 236 | u8 *buf; |
| 237 | |
| 238 | for (; (buf = *bufs); bufs++) { |
| 239 | for (i = 0; i < start; i++) |
| 240 | buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK); |
| 241 | for ( ; i < start + len; i++) |
| 242 | buf[i] = PATTERN_DST | PATTERN_OVERWRITE |
| 243 | | (~i & PATTERN_COUNT_MASK); |
| 244 | for ( ; i < buf_size; i++) |
| 245 | buf[i] = PATTERN_DST | (~i & PATTERN_COUNT_MASK); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static void dmatest_mismatch(u8 actual, u8 pattern, unsigned int index, |
| 250 | unsigned int counter, bool is_srcbuf) |
| 251 | { |
| 252 | u8 diff = actual ^ pattern; |
| 253 | u8 expected = pattern | (~counter & PATTERN_COUNT_MASK); |
| 254 | const char *thread_name = current->comm; |
| 255 | |
| 256 | if (is_srcbuf) |
| 257 | pr_warning("%s: srcbuf[0x%x] overwritten!" |
| 258 | " Expected %02x, got %02x\n", |
| 259 | thread_name, index, expected, actual); |
| 260 | else if ((pattern & PATTERN_COPY) |
| 261 | && (diff & (PATTERN_COPY | PATTERN_OVERWRITE))) |
| 262 | pr_warning("%s: dstbuf[0x%x] not copied!" |
| 263 | " Expected %02x, got %02x\n", |
| 264 | thread_name, index, expected, actual); |
| 265 | else if (diff & PATTERN_SRC) |
| 266 | pr_warning("%s: dstbuf[0x%x] was copied!" |
| 267 | " Expected %02x, got %02x\n", |
| 268 | thread_name, index, expected, actual); |
| 269 | else |
| 270 | pr_warning("%s: dstbuf[0x%x] mismatch!" |
| 271 | " Expected %02x, got %02x\n", |
| 272 | thread_name, index, expected, actual); |
| 273 | } |
| 274 | |
| 275 | static unsigned int dmatest_verify(u8 **bufs, unsigned int start, |
| 276 | unsigned int end, unsigned int counter, u8 pattern, |
| 277 | bool is_srcbuf) |
| 278 | { |
| 279 | unsigned int i; |
| 280 | unsigned int error_count = 0; |
| 281 | u8 actual; |
| 282 | u8 expected; |
| 283 | u8 *buf; |
| 284 | unsigned int counter_orig = counter; |
| 285 | |
| 286 | for (; (buf = *bufs); bufs++) { |
| 287 | counter = counter_orig; |
| 288 | for (i = start; i < end; i++) { |
| 289 | actual = buf[i]; |
| 290 | expected = pattern | (~counter & PATTERN_COUNT_MASK); |
| 291 | if (actual != expected) { |
| 292 | if (error_count < MAX_ERROR_COUNT) |
| 293 | dmatest_mismatch(actual, pattern, i, |
| 294 | counter, is_srcbuf); |
| 295 | error_count++; |
| 296 | } |
| 297 | counter++; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (error_count > MAX_ERROR_COUNT) |
| 302 | pr_warning("%s: %u errors suppressed\n", |
| 303 | current->comm, error_count - MAX_ERROR_COUNT); |
| 304 | |
| 305 | return error_count; |
| 306 | } |
| 307 | |
| 308 | /* poor man's completion - we want to use wait_event_freezable() on it */ |
| 309 | struct dmatest_done { |
| 310 | bool done; |
| 311 | wait_queue_head_t *wait; |
| 312 | }; |
| 313 | |
| 314 | static void dmatest_callback(void *arg) |
| 315 | { |
| 316 | struct dmatest_done *done = arg; |
| 317 | |
| 318 | done->done = true; |
| 319 | wake_up_all(done->wait); |
| 320 | } |
| 321 | |
| 322 | static inline void unmap_src(struct device *dev, dma_addr_t *addr, size_t len, |
| 323 | unsigned int count) |
| 324 | { |
| 325 | while (count--) |
| 326 | dma_unmap_single(dev, addr[count], len, DMA_TO_DEVICE); |
| 327 | } |
| 328 | |
| 329 | static inline void unmap_dst(struct device *dev, dma_addr_t *addr, size_t len, |
| 330 | unsigned int count) |
| 331 | { |
| 332 | while (count--) |
| 333 | dma_unmap_single(dev, addr[count], len, DMA_BIDIRECTIONAL); |
| 334 | } |
| 335 | |
| 336 | static unsigned int min_odd(unsigned int x, unsigned int y) |
| 337 | { |
| 338 | unsigned int val = min(x, y); |
| 339 | |
| 340 | return val % 2 ? val : val - 1; |
| 341 | } |
| 342 | |
| 343 | static char *thread_result_get(const char *name, |
| 344 | struct dmatest_thread_result *tr) |
| 345 | { |
| 346 | static const char * const messages[] = { |
| 347 | [DMATEST_ET_OK] = "No errors", |
| 348 | [DMATEST_ET_MAP_SRC] = "src mapping error", |
| 349 | [DMATEST_ET_MAP_DST] = "dst mapping error", |
| 350 | [DMATEST_ET_PREP] = "prep error", |
| 351 | [DMATEST_ET_SUBMIT] = "submit error", |
| 352 | [DMATEST_ET_TIMEOUT] = "test timed out", |
| 353 | [DMATEST_ET_DMA_ERROR] = |
| 354 | "got completion callback (DMA_ERROR)", |
| 355 | [DMATEST_ET_DMA_IN_PROGRESS] = |
| 356 | "got completion callback (DMA_IN_PROGRESS)", |
| 357 | [DMATEST_ET_VERIFY] = "errors", |
| 358 | }; |
| 359 | static char buf[512]; |
| 360 | |
| 361 | snprintf(buf, sizeof(buf) - 1, |
| 362 | "%s: #%u: %s with src_off=0x%x ""dst_off=0x%x len=0x%x (%lu)", |
| 363 | name, tr->n, messages[tr->type], tr->src_off, tr->dst_off, |
| 364 | tr->len, tr->data); |
| 365 | |
| 366 | return buf; |
| 367 | } |
| 368 | |
| 369 | static int thread_result_add(struct dmatest_info *info, |
| 370 | struct dmatest_result *r, enum dmatest_error_type type, |
| 371 | unsigned int n, unsigned int src_off, unsigned int dst_off, |
| 372 | unsigned int len, unsigned long data) |
| 373 | { |
| 374 | struct dmatest_thread_result *tr; |
| 375 | |
| 376 | tr = kzalloc(sizeof(*tr), GFP_KERNEL); |
| 377 | if (!tr) |
| 378 | return -ENOMEM; |
| 379 | |
| 380 | tr->type = type; |
| 381 | tr->n = n; |
| 382 | tr->src_off = src_off; |
| 383 | tr->dst_off = dst_off; |
| 384 | tr->len = len; |
| 385 | tr->data = data; |
| 386 | |
| 387 | mutex_lock(&info->results_lock); |
| 388 | list_add_tail(&tr->node, &r->results); |
| 389 | mutex_unlock(&info->results_lock); |
| 390 | |
| 391 | pr_warn("%s\n", thread_result_get(r->name, tr)); |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static void result_free(struct dmatest_info *info, const char *name) |
| 396 | { |
| 397 | struct dmatest_result *r, *_r; |
| 398 | |
| 399 | mutex_lock(&info->results_lock); |
| 400 | list_for_each_entry_safe(r, _r, &info->results, node) { |
| 401 | struct dmatest_thread_result *tr, *_tr; |
| 402 | |
| 403 | if (name && strcmp(r->name, name)) |
| 404 | continue; |
| 405 | |
| 406 | list_for_each_entry_safe(tr, _tr, &r->results, node) { |
| 407 | list_del(&tr->node); |
| 408 | kfree(tr); |
| 409 | } |
| 410 | |
| 411 | kfree(r->name); |
| 412 | list_del(&r->node); |
| 413 | kfree(r); |
| 414 | } |
| 415 | |
| 416 | mutex_unlock(&info->results_lock); |
| 417 | } |
| 418 | |
| 419 | static struct dmatest_result *result_init(struct dmatest_info *info, |
| 420 | const char *name) |
| 421 | { |
| 422 | struct dmatest_result *r; |
| 423 | |
| 424 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 425 | if (r) { |
| 426 | r->name = kstrdup(name, GFP_KERNEL); |
| 427 | INIT_LIST_HEAD(&r->results); |
| 428 | mutex_lock(&info->results_lock); |
| 429 | list_add_tail(&r->node, &info->results); |
| 430 | mutex_unlock(&info->results_lock); |
| 431 | } |
| 432 | return r; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * This function repeatedly tests DMA transfers of various lengths and |
| 437 | * offsets for a given operation type until it is told to exit by |
| 438 | * kthread_stop(). There may be multiple threads running this function |
| 439 | * in parallel for a single channel, and there may be multiple channels |
| 440 | * being tested in parallel. |
| 441 | * |
| 442 | * Before each test, the source and destination buffer is initialized |
| 443 | * with a known pattern. This pattern is different depending on |
| 444 | * whether it's in an area which is supposed to be copied or |
| 445 | * overwritten, and different in the source and destination buffers. |
| 446 | * So if the DMA engine doesn't copy exactly what we tell it to copy, |
| 447 | * we'll notice. |
| 448 | */ |
| 449 | static int dmatest_func(void *data) |
| 450 | { |
| 451 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_wait); |
| 452 | struct dmatest_thread *thread = data; |
| 453 | struct dmatest_done done = { .wait = &done_wait }; |
| 454 | struct dmatest_info *info; |
| 455 | struct dmatest_params *params; |
| 456 | struct dma_chan *chan; |
| 457 | struct dma_device *dev; |
| 458 | const char *thread_name; |
| 459 | unsigned int src_off, dst_off, len; |
| 460 | unsigned int error_count; |
| 461 | unsigned int failed_tests = 0; |
| 462 | unsigned int total_tests = 0; |
| 463 | dma_cookie_t cookie; |
| 464 | enum dma_status status; |
| 465 | enum dma_ctrl_flags flags; |
| 466 | u8 *pq_coefs = NULL; |
| 467 | int ret; |
| 468 | int src_cnt; |
| 469 | int dst_cnt; |
| 470 | int i; |
| 471 | struct dmatest_result *result; |
| 472 | |
| 473 | thread_name = current->comm; |
| 474 | set_freezable(); |
| 475 | |
| 476 | ret = -ENOMEM; |
| 477 | |
| 478 | smp_rmb(); |
| 479 | info = thread->info; |
| 480 | params = &info->params; |
| 481 | chan = thread->chan; |
| 482 | dev = chan->device; |
| 483 | if (thread->type == DMA_MEMCPY) |
| 484 | src_cnt = dst_cnt = 1; |
| 485 | else if (thread->type == DMA_XOR) { |
| 486 | /* force odd to ensure dst = src */ |
| 487 | src_cnt = min_odd(params->xor_sources | 1, dev->max_xor); |
| 488 | dst_cnt = 1; |
| 489 | } else if (thread->type == DMA_PQ) { |
| 490 | /* force odd to ensure dst = src */ |
| 491 | src_cnt = min_odd(params->pq_sources | 1, dma_maxpq(dev, 0)); |
| 492 | dst_cnt = 2; |
| 493 | |
| 494 | pq_coefs = kmalloc(params->pq_sources+1, GFP_KERNEL); |
| 495 | if (!pq_coefs) |
| 496 | goto err_thread_type; |
| 497 | |
| 498 | for (i = 0; i < src_cnt; i++) |
| 499 | pq_coefs[i] = 1; |
| 500 | } else |
| 501 | goto err_thread_type; |
| 502 | |
| 503 | result = result_init(info, thread_name); |
| 504 | if (!result) |
| 505 | goto err_srcs; |
| 506 | |
| 507 | thread->srcs = kcalloc(src_cnt+1, sizeof(u8 *), GFP_KERNEL); |
| 508 | if (!thread->srcs) |
| 509 | goto err_srcs; |
| 510 | for (i = 0; i < src_cnt; i++) { |
| 511 | thread->srcs[i] = kmalloc(params->buf_size, GFP_KERNEL); |
| 512 | if (!thread->srcs[i]) |
| 513 | goto err_srcbuf; |
| 514 | } |
| 515 | thread->srcs[i] = NULL; |
| 516 | |
| 517 | thread->dsts = kcalloc(dst_cnt+1, sizeof(u8 *), GFP_KERNEL); |
| 518 | if (!thread->dsts) |
| 519 | goto err_dsts; |
| 520 | for (i = 0; i < dst_cnt; i++) { |
| 521 | thread->dsts[i] = kmalloc(params->buf_size, GFP_KERNEL); |
| 522 | if (!thread->dsts[i]) |
| 523 | goto err_dstbuf; |
| 524 | } |
| 525 | thread->dsts[i] = NULL; |
| 526 | |
| 527 | set_user_nice(current, 10); |
| 528 | |
| 529 | /* |
| 530 | * src buffers are freed by the DMAEngine code with dma_unmap_single() |
| 531 | * dst buffers are freed by ourselves below |
| 532 | */ |
| 533 | flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT |
| 534 | | DMA_COMPL_SKIP_DEST_UNMAP | DMA_COMPL_SRC_UNMAP_SINGLE; |
| 535 | |
| 536 | while (!kthread_should_stop() |
| 537 | && !(params->iterations && total_tests >= params->iterations)) { |
| 538 | struct dma_async_tx_descriptor *tx = NULL; |
| 539 | dma_addr_t dma_srcs[src_cnt]; |
| 540 | dma_addr_t dma_dsts[dst_cnt]; |
| 541 | u8 align = 0; |
| 542 | |
| 543 | total_tests++; |
| 544 | |
| 545 | /* honor alignment restrictions */ |
| 546 | if (thread->type == DMA_MEMCPY) |
| 547 | align = dev->copy_align; |
| 548 | else if (thread->type == DMA_XOR) |
| 549 | align = dev->xor_align; |
| 550 | else if (thread->type == DMA_PQ) |
| 551 | align = dev->pq_align; |
| 552 | |
| 553 | if (1 << align > params->buf_size) { |
| 554 | pr_err("%u-byte buffer too small for %d-byte alignment\n", |
| 555 | params->buf_size, 1 << align); |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | len = dmatest_random() % params->buf_size + 1; |
| 560 | len = (len >> align) << align; |
| 561 | if (!len) |
| 562 | len = 1 << align; |
| 563 | src_off = dmatest_random() % (params->buf_size - len + 1); |
| 564 | dst_off = dmatest_random() % (params->buf_size - len + 1); |
| 565 | |
| 566 | src_off = (src_off >> align) << align; |
| 567 | dst_off = (dst_off >> align) << align; |
| 568 | |
| 569 | dmatest_init_srcs(thread->srcs, src_off, len, params->buf_size); |
| 570 | dmatest_init_dsts(thread->dsts, dst_off, len, params->buf_size); |
| 571 | |
| 572 | for (i = 0; i < src_cnt; i++) { |
| 573 | u8 *buf = thread->srcs[i] + src_off; |
| 574 | |
| 575 | dma_srcs[i] = dma_map_single(dev->dev, buf, len, |
| 576 | DMA_TO_DEVICE); |
| 577 | ret = dma_mapping_error(dev->dev, dma_srcs[i]); |
| 578 | if (ret) { |
| 579 | unmap_src(dev->dev, dma_srcs, len, i); |
| 580 | thread_result_add(info, result, |
| 581 | DMATEST_ET_MAP_SRC, |
| 582 | total_tests, src_off, dst_off, |
| 583 | len, ret); |
| 584 | failed_tests++; |
| 585 | continue; |
| 586 | } |
| 587 | } |
| 588 | /* map with DMA_BIDIRECTIONAL to force writeback/invalidate */ |
| 589 | for (i = 0; i < dst_cnt; i++) { |
| 590 | dma_dsts[i] = dma_map_single(dev->dev, thread->dsts[i], |
| 591 | params->buf_size, |
| 592 | DMA_BIDIRECTIONAL); |
| 593 | ret = dma_mapping_error(dev->dev, dma_dsts[i]); |
| 594 | if (ret) { |
| 595 | unmap_src(dev->dev, dma_srcs, len, src_cnt); |
| 596 | unmap_dst(dev->dev, dma_dsts, params->buf_size, |
| 597 | i); |
| 598 | thread_result_add(info, result, |
| 599 | DMATEST_ET_MAP_DST, |
| 600 | total_tests, src_off, dst_off, |
| 601 | len, ret); |
| 602 | failed_tests++; |
| 603 | continue; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (thread->type == DMA_MEMCPY) |
| 608 | tx = dev->device_prep_dma_memcpy(chan, |
| 609 | dma_dsts[0] + dst_off, |
| 610 | dma_srcs[0], len, |
| 611 | flags); |
| 612 | else if (thread->type == DMA_XOR) |
| 613 | tx = dev->device_prep_dma_xor(chan, |
| 614 | dma_dsts[0] + dst_off, |
| 615 | dma_srcs, src_cnt, |
| 616 | len, flags); |
| 617 | else if (thread->type == DMA_PQ) { |
| 618 | dma_addr_t dma_pq[dst_cnt]; |
| 619 | |
| 620 | for (i = 0; i < dst_cnt; i++) |
| 621 | dma_pq[i] = dma_dsts[i] + dst_off; |
| 622 | tx = dev->device_prep_dma_pq(chan, dma_pq, dma_srcs, |
| 623 | src_cnt, pq_coefs, |
| 624 | len, flags); |
| 625 | } |
| 626 | |
| 627 | if (!tx) { |
| 628 | unmap_src(dev->dev, dma_srcs, len, src_cnt); |
| 629 | unmap_dst(dev->dev, dma_dsts, params->buf_size, |
| 630 | dst_cnt); |
| 631 | thread_result_add(info, result, DMATEST_ET_PREP, |
| 632 | total_tests, src_off, dst_off, |
| 633 | len, 0); |
| 634 | msleep(100); |
| 635 | failed_tests++; |
| 636 | continue; |
| 637 | } |
| 638 | |
| 639 | done.done = false; |
| 640 | tx->callback = dmatest_callback; |
| 641 | tx->callback_param = &done; |
| 642 | cookie = tx->tx_submit(tx); |
| 643 | |
| 644 | if (dma_submit_error(cookie)) { |
| 645 | thread_result_add(info, result, DMATEST_ET_SUBMIT, |
| 646 | total_tests, src_off, dst_off, |
| 647 | len, cookie); |
| 648 | msleep(100); |
| 649 | failed_tests++; |
| 650 | continue; |
| 651 | } |
| 652 | dma_async_issue_pending(chan); |
| 653 | |
| 654 | wait_event_freezable_timeout(done_wait, |
| 655 | done.done || kthread_should_stop(), |
| 656 | msecs_to_jiffies(params->timeout)); |
| 657 | |
| 658 | status = dma_async_is_tx_complete(chan, cookie, NULL, NULL); |
| 659 | |
| 660 | if (!done.done) { |
| 661 | /* |
| 662 | * We're leaving the timed out dma operation with |
| 663 | * dangling pointer to done_wait. To make this |
| 664 | * correct, we'll need to allocate wait_done for |
| 665 | * each test iteration and perform "who's gonna |
| 666 | * free it this time?" dancing. For now, just |
| 667 | * leave it dangling. |
| 668 | */ |
| 669 | thread_result_add(info, result, DMATEST_ET_TIMEOUT, |
| 670 | total_tests, src_off, dst_off, |
| 671 | len, 0); |
| 672 | failed_tests++; |
| 673 | continue; |
| 674 | } else if (status != DMA_SUCCESS) { |
| 675 | enum dmatest_error_type type = (status == DMA_ERROR) ? |
| 676 | DMATEST_ET_DMA_ERROR : DMATEST_ET_DMA_IN_PROGRESS; |
| 677 | thread_result_add(info, result, type, |
| 678 | total_tests, src_off, dst_off, |
| 679 | len, status); |
| 680 | failed_tests++; |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | /* Unmap by myself (see DMA_COMPL_SKIP_DEST_UNMAP above) */ |
| 685 | unmap_dst(dev->dev, dma_dsts, params->buf_size, dst_cnt); |
| 686 | |
| 687 | error_count = 0; |
| 688 | |
| 689 | pr_debug("%s: verifying source buffer...\n", thread_name); |
| 690 | error_count += dmatest_verify(thread->srcs, 0, src_off, |
| 691 | 0, PATTERN_SRC, true); |
| 692 | error_count += dmatest_verify(thread->srcs, src_off, |
| 693 | src_off + len, src_off, |
| 694 | PATTERN_SRC | PATTERN_COPY, true); |
| 695 | error_count += dmatest_verify(thread->srcs, src_off + len, |
| 696 | params->buf_size, src_off + len, |
| 697 | PATTERN_SRC, true); |
| 698 | |
| 699 | pr_debug("%s: verifying dest buffer...\n", |
| 700 | thread->task->comm); |
| 701 | error_count += dmatest_verify(thread->dsts, 0, dst_off, |
| 702 | 0, PATTERN_DST, false); |
| 703 | error_count += dmatest_verify(thread->dsts, dst_off, |
| 704 | dst_off + len, src_off, |
| 705 | PATTERN_SRC | PATTERN_COPY, false); |
| 706 | error_count += dmatest_verify(thread->dsts, dst_off + len, |
| 707 | params->buf_size, dst_off + len, |
| 708 | PATTERN_DST, false); |
| 709 | |
| 710 | if (error_count) { |
| 711 | thread_result_add(info, result, DMATEST_ET_VERIFY, |
| 712 | total_tests, src_off, dst_off, |
| 713 | len, error_count); |
| 714 | failed_tests++; |
| 715 | } else { |
| 716 | thread_result_add(info, result, DMATEST_ET_OK, |
| 717 | total_tests, src_off, dst_off, |
| 718 | len, 0); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | ret = 0; |
| 723 | for (i = 0; thread->dsts[i]; i++) |
| 724 | kfree(thread->dsts[i]); |
| 725 | err_dstbuf: |
| 726 | kfree(thread->dsts); |
| 727 | err_dsts: |
| 728 | for (i = 0; thread->srcs[i]; i++) |
| 729 | kfree(thread->srcs[i]); |
| 730 | err_srcbuf: |
| 731 | kfree(thread->srcs); |
| 732 | err_srcs: |
| 733 | kfree(pq_coefs); |
| 734 | err_thread_type: |
| 735 | pr_notice("%s: terminating after %u tests, %u failures (status %d)\n", |
| 736 | thread_name, total_tests, failed_tests, ret); |
| 737 | |
| 738 | /* terminate all transfers on specified channels */ |
| 739 | if (ret) |
| 740 | dmaengine_terminate_all(chan); |
| 741 | |
| 742 | thread->done = true; |
| 743 | |
| 744 | if (params->iterations > 0) |
| 745 | while (!kthread_should_stop()) { |
| 746 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wait_dmatest_exit); |
| 747 | interruptible_sleep_on(&wait_dmatest_exit); |
| 748 | } |
| 749 | |
| 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | static void dmatest_cleanup_channel(struct dmatest_chan *dtc) |
| 754 | { |
| 755 | struct dmatest_thread *thread; |
| 756 | struct dmatest_thread *_thread; |
| 757 | int ret; |
| 758 | |
| 759 | list_for_each_entry_safe(thread, _thread, &dtc->threads, node) { |
| 760 | ret = kthread_stop(thread->task); |
| 761 | pr_debug("dmatest: thread %s exited with status %d\n", |
| 762 | thread->task->comm, ret); |
| 763 | list_del(&thread->node); |
| 764 | kfree(thread); |
| 765 | } |
| 766 | |
| 767 | /* terminate all transfers on specified channels */ |
| 768 | dmaengine_terminate_all(dtc->chan); |
| 769 | |
| 770 | kfree(dtc); |
| 771 | } |
| 772 | |
| 773 | static int dmatest_add_threads(struct dmatest_info *info, |
| 774 | struct dmatest_chan *dtc, enum dma_transaction_type type) |
| 775 | { |
| 776 | struct dmatest_params *params = &info->params; |
| 777 | struct dmatest_thread *thread; |
| 778 | struct dma_chan *chan = dtc->chan; |
| 779 | char *op; |
| 780 | unsigned int i; |
| 781 | |
| 782 | if (type == DMA_MEMCPY) |
| 783 | op = "copy"; |
| 784 | else if (type == DMA_XOR) |
| 785 | op = "xor"; |
| 786 | else if (type == DMA_PQ) |
| 787 | op = "pq"; |
| 788 | else |
| 789 | return -EINVAL; |
| 790 | |
| 791 | for (i = 0; i < params->threads_per_chan; i++) { |
| 792 | thread = kzalloc(sizeof(struct dmatest_thread), GFP_KERNEL); |
| 793 | if (!thread) { |
| 794 | pr_warning("dmatest: No memory for %s-%s%u\n", |
| 795 | dma_chan_name(chan), op, i); |
| 796 | |
| 797 | break; |
| 798 | } |
| 799 | thread->info = info; |
| 800 | thread->chan = dtc->chan; |
| 801 | thread->type = type; |
| 802 | smp_wmb(); |
| 803 | thread->task = kthread_run(dmatest_func, thread, "%s-%s%u", |
| 804 | dma_chan_name(chan), op, i); |
| 805 | if (IS_ERR(thread->task)) { |
| 806 | pr_warning("dmatest: Failed to run thread %s-%s%u\n", |
| 807 | dma_chan_name(chan), op, i); |
| 808 | kfree(thread); |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | /* srcbuf and dstbuf are allocated by the thread itself */ |
| 813 | |
| 814 | list_add_tail(&thread->node, &dtc->threads); |
| 815 | } |
| 816 | |
| 817 | return i; |
| 818 | } |
| 819 | |
| 820 | static int dmatest_add_channel(struct dmatest_info *info, |
| 821 | struct dma_chan *chan) |
| 822 | { |
| 823 | struct dmatest_chan *dtc; |
| 824 | struct dma_device *dma_dev = chan->device; |
| 825 | unsigned int thread_count = 0; |
| 826 | int cnt; |
| 827 | |
| 828 | dtc = kmalloc(sizeof(struct dmatest_chan), GFP_KERNEL); |
| 829 | if (!dtc) { |
| 830 | pr_warning("dmatest: No memory for %s\n", dma_chan_name(chan)); |
| 831 | return -ENOMEM; |
| 832 | } |
| 833 | |
| 834 | dtc->chan = chan; |
| 835 | INIT_LIST_HEAD(&dtc->threads); |
| 836 | |
| 837 | if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) { |
| 838 | cnt = dmatest_add_threads(info, dtc, DMA_MEMCPY); |
| 839 | thread_count += cnt > 0 ? cnt : 0; |
| 840 | } |
| 841 | if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) { |
| 842 | cnt = dmatest_add_threads(info, dtc, DMA_XOR); |
| 843 | thread_count += cnt > 0 ? cnt : 0; |
| 844 | } |
| 845 | if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) { |
| 846 | cnt = dmatest_add_threads(info, dtc, DMA_PQ); |
| 847 | thread_count += cnt > 0 ? cnt : 0; |
| 848 | } |
| 849 | |
| 850 | pr_info("dmatest: Started %u threads using %s\n", |
| 851 | thread_count, dma_chan_name(chan)); |
| 852 | |
| 853 | list_add_tail(&dtc->node, &info->channels); |
| 854 | info->nr_channels++; |
| 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | static bool filter(struct dma_chan *chan, void *param) |
| 860 | { |
| 861 | struct dmatest_params *params = param; |
| 862 | |
| 863 | if (!dmatest_match_channel(params, chan) || |
| 864 | !dmatest_match_device(params, chan->device)) |
| 865 | return false; |
| 866 | else |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | static int __run_threaded_test(struct dmatest_info *info) |
| 871 | { |
| 872 | dma_cap_mask_t mask; |
| 873 | struct dma_chan *chan; |
| 874 | struct dmatest_params *params = &info->params; |
| 875 | int err = 0; |
| 876 | |
| 877 | dma_cap_zero(mask); |
| 878 | dma_cap_set(DMA_MEMCPY, mask); |
| 879 | for (;;) { |
| 880 | chan = dma_request_channel(mask, filter, params); |
| 881 | if (chan) { |
| 882 | err = dmatest_add_channel(info, chan); |
| 883 | if (err) { |
| 884 | dma_release_channel(chan); |
| 885 | break; /* add_channel failed, punt */ |
| 886 | } |
| 887 | } else |
| 888 | break; /* no more channels available */ |
| 889 | if (params->max_channels && |
| 890 | info->nr_channels >= params->max_channels) |
| 891 | break; /* we have all we need */ |
| 892 | } |
| 893 | return err; |
| 894 | } |
| 895 | |
| 896 | #ifndef MODULE |
| 897 | static int run_threaded_test(struct dmatest_info *info) |
| 898 | { |
| 899 | int ret; |
| 900 | |
| 901 | mutex_lock(&info->lock); |
| 902 | ret = __run_threaded_test(info); |
| 903 | mutex_unlock(&info->lock); |
| 904 | return ret; |
| 905 | } |
| 906 | #endif |
| 907 | |
| 908 | static void __stop_threaded_test(struct dmatest_info *info) |
| 909 | { |
| 910 | struct dmatest_chan *dtc, *_dtc; |
| 911 | struct dma_chan *chan; |
| 912 | |
| 913 | list_for_each_entry_safe(dtc, _dtc, &info->channels, node) { |
| 914 | list_del(&dtc->node); |
| 915 | chan = dtc->chan; |
| 916 | dmatest_cleanup_channel(dtc); |
| 917 | pr_debug("dmatest: dropped channel %s\n", dma_chan_name(chan)); |
| 918 | dma_release_channel(chan); |
| 919 | } |
| 920 | |
| 921 | info->nr_channels = 0; |
| 922 | } |
| 923 | |
| 924 | static void stop_threaded_test(struct dmatest_info *info) |
| 925 | { |
| 926 | mutex_lock(&info->lock); |
| 927 | __stop_threaded_test(info); |
| 928 | mutex_unlock(&info->lock); |
| 929 | } |
| 930 | |
| 931 | static int __restart_threaded_test(struct dmatest_info *info, bool run) |
| 932 | { |
| 933 | struct dmatest_params *params = &info->params; |
| 934 | int ret; |
| 935 | |
| 936 | /* Stop any running test first */ |
| 937 | __stop_threaded_test(info); |
| 938 | |
| 939 | if (run == false) |
| 940 | return 0; |
| 941 | |
| 942 | /* Clear results from previous run */ |
| 943 | result_free(info, NULL); |
| 944 | |
| 945 | /* Copy test parameters */ |
| 946 | memcpy(params, &info->dbgfs_params, sizeof(*params)); |
| 947 | |
| 948 | /* Run test with new parameters */ |
| 949 | ret = __run_threaded_test(info); |
| 950 | if (ret) { |
| 951 | __stop_threaded_test(info); |
| 952 | pr_err("dmatest: Can't run test\n"); |
| 953 | } |
| 954 | |
| 955 | return ret; |
| 956 | } |
| 957 | |
| 958 | static ssize_t dtf_write_string(void *to, size_t available, loff_t *ppos, |
| 959 | const void __user *from, size_t count) |
| 960 | { |
| 961 | char tmp[20]; |
| 962 | ssize_t len; |
| 963 | |
| 964 | len = simple_write_to_buffer(tmp, sizeof(tmp) - 1, ppos, from, count); |
| 965 | if (len >= 0) { |
| 966 | tmp[len] = '\0'; |
| 967 | strlcpy(to, strim(tmp), available); |
| 968 | } |
| 969 | |
| 970 | return len; |
| 971 | } |
| 972 | |
| 973 | static ssize_t dtf_read_channel(struct file *file, char __user *buf, |
| 974 | size_t count, loff_t *ppos) |
| 975 | { |
| 976 | struct dmatest_info *info = file->private_data; |
| 977 | return simple_read_from_buffer(buf, count, ppos, |
| 978 | info->dbgfs_params.channel, |
| 979 | strlen(info->dbgfs_params.channel)); |
| 980 | } |
| 981 | |
| 982 | static ssize_t dtf_write_channel(struct file *file, const char __user *buf, |
| 983 | size_t size, loff_t *ppos) |
| 984 | { |
| 985 | struct dmatest_info *info = file->private_data; |
| 986 | return dtf_write_string(info->dbgfs_params.channel, |
| 987 | sizeof(info->dbgfs_params.channel), |
| 988 | ppos, buf, size); |
| 989 | } |
| 990 | |
| 991 | static const struct file_operations dtf_channel_fops = { |
| 992 | .read = dtf_read_channel, |
| 993 | .write = dtf_write_channel, |
| 994 | .open = simple_open, |
| 995 | .llseek = default_llseek, |
| 996 | }; |
| 997 | |
| 998 | static ssize_t dtf_read_device(struct file *file, char __user *buf, |
| 999 | size_t count, loff_t *ppos) |
| 1000 | { |
| 1001 | struct dmatest_info *info = file->private_data; |
| 1002 | return simple_read_from_buffer(buf, count, ppos, |
| 1003 | info->dbgfs_params.device, |
| 1004 | strlen(info->dbgfs_params.device)); |
| 1005 | } |
| 1006 | |
| 1007 | static ssize_t dtf_write_device(struct file *file, const char __user *buf, |
| 1008 | size_t size, loff_t *ppos) |
| 1009 | { |
| 1010 | struct dmatest_info *info = file->private_data; |
| 1011 | return dtf_write_string(info->dbgfs_params.device, |
| 1012 | sizeof(info->dbgfs_params.device), |
| 1013 | ppos, buf, size); |
| 1014 | } |
| 1015 | |
| 1016 | static const struct file_operations dtf_device_fops = { |
| 1017 | .read = dtf_read_device, |
| 1018 | .write = dtf_write_device, |
| 1019 | .open = simple_open, |
| 1020 | .llseek = default_llseek, |
| 1021 | }; |
| 1022 | |
| 1023 | static ssize_t dtf_read_run(struct file *file, char __user *user_buf, |
| 1024 | size_t count, loff_t *ppos) |
| 1025 | { |
| 1026 | struct dmatest_info *info = file->private_data; |
| 1027 | char buf[3]; |
| 1028 | struct dmatest_chan *dtc; |
| 1029 | bool alive = false; |
| 1030 | |
| 1031 | mutex_lock(&info->lock); |
| 1032 | list_for_each_entry(dtc, &info->channels, node) { |
| 1033 | struct dmatest_thread *thread; |
| 1034 | |
| 1035 | list_for_each_entry(thread, &dtc->threads, node) { |
| 1036 | if (!thread->done) { |
| 1037 | alive = true; |
| 1038 | break; |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | if (alive) { |
| 1044 | buf[0] = 'Y'; |
| 1045 | } else { |
| 1046 | __stop_threaded_test(info); |
| 1047 | buf[0] = 'N'; |
| 1048 | } |
| 1049 | |
| 1050 | mutex_unlock(&info->lock); |
| 1051 | buf[1] = '\n'; |
| 1052 | buf[2] = 0x00; |
| 1053 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 1054 | } |
| 1055 | |
| 1056 | static ssize_t dtf_write_run(struct file *file, const char __user *user_buf, |
| 1057 | size_t count, loff_t *ppos) |
| 1058 | { |
| 1059 | struct dmatest_info *info = file->private_data; |
| 1060 | char buf[16]; |
| 1061 | bool bv; |
| 1062 | int ret = 0; |
| 1063 | |
| 1064 | if (copy_from_user(buf, user_buf, min(count, (sizeof(buf) - 1)))) |
| 1065 | return -EFAULT; |
| 1066 | |
| 1067 | if (strtobool(buf, &bv) == 0) { |
| 1068 | mutex_lock(&info->lock); |
| 1069 | ret = __restart_threaded_test(info, bv); |
| 1070 | mutex_unlock(&info->lock); |
| 1071 | } |
| 1072 | |
| 1073 | return ret ? ret : count; |
| 1074 | } |
| 1075 | |
| 1076 | static const struct file_operations dtf_run_fops = { |
| 1077 | .read = dtf_read_run, |
| 1078 | .write = dtf_write_run, |
| 1079 | .open = simple_open, |
| 1080 | .llseek = default_llseek, |
| 1081 | }; |
| 1082 | |
| 1083 | static int dtf_results_show(struct seq_file *sf, void *data) |
| 1084 | { |
| 1085 | struct dmatest_info *info = sf->private; |
| 1086 | struct dmatest_result *result; |
| 1087 | struct dmatest_thread_result *tr; |
| 1088 | |
| 1089 | mutex_lock(&info->results_lock); |
| 1090 | list_for_each_entry(result, &info->results, node) { |
| 1091 | list_for_each_entry(tr, &result->results, node) |
| 1092 | seq_printf(sf, "%s\n", |
| 1093 | thread_result_get(result->name, tr)); |
| 1094 | } |
| 1095 | |
| 1096 | mutex_unlock(&info->results_lock); |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | static int dtf_results_open(struct inode *inode, struct file *file) |
| 1101 | { |
| 1102 | return single_open(file, dtf_results_show, inode->i_private); |
| 1103 | } |
| 1104 | |
| 1105 | static const struct file_operations dtf_results_fops = { |
| 1106 | .open = dtf_results_open, |
| 1107 | .read = seq_read, |
| 1108 | .llseek = seq_lseek, |
| 1109 | .release = single_release, |
| 1110 | }; |
| 1111 | |
| 1112 | static int dmatest_register_dbgfs(struct dmatest_info *info) |
| 1113 | { |
| 1114 | struct dentry *d; |
| 1115 | struct dmatest_params *params = &info->dbgfs_params; |
| 1116 | int ret = -ENOMEM; |
| 1117 | |
| 1118 | d = debugfs_create_dir("dmatest", NULL); |
| 1119 | if (IS_ERR(d)) |
| 1120 | return PTR_ERR(d); |
| 1121 | if (!d) |
| 1122 | goto err_root; |
| 1123 | |
| 1124 | info->root = d; |
| 1125 | |
| 1126 | /* Copy initial values */ |
| 1127 | memcpy(params, &info->params, sizeof(*params)); |
| 1128 | |
| 1129 | /* Test parameters */ |
| 1130 | |
| 1131 | d = debugfs_create_u32("test_buf_size", S_IWUSR | S_IRUGO, info->root, |
| 1132 | (u32 *)¶ms->buf_size); |
| 1133 | if (IS_ERR_OR_NULL(d)) |
| 1134 | goto err_node; |
| 1135 | |
| 1136 | d = debugfs_create_file("channel", S_IRUGO | S_IWUSR, info->root, |
| 1137 | info, &dtf_channel_fops); |
| 1138 | if (IS_ERR_OR_NULL(d)) |
| 1139 | goto err_node; |
| 1140 | |
| 1141 | d = debugfs_create_file("device", S_IRUGO | S_IWUSR, info->root, |
| 1142 | info, &dtf_device_fops); |
| 1143 | if (IS_ERR_OR_NULL(d)) |
| 1144 | goto err_node; |
| 1145 | |
| 1146 | d = debugfs_create_u32("threads_per_chan", S_IWUSR | S_IRUGO, info->root, |
| 1147 | (u32 *)¶ms->threads_per_chan); |
| 1148 | if (IS_ERR_OR_NULL(d)) |
| 1149 | goto err_node; |
| 1150 | |
| 1151 | d = debugfs_create_u32("max_channels", S_IWUSR | S_IRUGO, info->root, |
| 1152 | (u32 *)¶ms->max_channels); |
| 1153 | if (IS_ERR_OR_NULL(d)) |
| 1154 | goto err_node; |
| 1155 | |
| 1156 | d = debugfs_create_u32("iterations", S_IWUSR | S_IRUGO, info->root, |
| 1157 | (u32 *)¶ms->iterations); |
| 1158 | if (IS_ERR_OR_NULL(d)) |
| 1159 | goto err_node; |
| 1160 | |
| 1161 | d = debugfs_create_u32("xor_sources", S_IWUSR | S_IRUGO, info->root, |
| 1162 | (u32 *)¶ms->xor_sources); |
| 1163 | if (IS_ERR_OR_NULL(d)) |
| 1164 | goto err_node; |
| 1165 | |
| 1166 | d = debugfs_create_u32("pq_sources", S_IWUSR | S_IRUGO, info->root, |
| 1167 | (u32 *)¶ms->pq_sources); |
| 1168 | if (IS_ERR_OR_NULL(d)) |
| 1169 | goto err_node; |
| 1170 | |
| 1171 | d = debugfs_create_u32("timeout", S_IWUSR | S_IRUGO, info->root, |
| 1172 | (u32 *)¶ms->timeout); |
| 1173 | if (IS_ERR_OR_NULL(d)) |
| 1174 | goto err_node; |
| 1175 | |
| 1176 | /* Run or stop threaded test */ |
| 1177 | d = debugfs_create_file("run", S_IWUSR | S_IRUGO, info->root, |
| 1178 | info, &dtf_run_fops); |
| 1179 | if (IS_ERR_OR_NULL(d)) |
| 1180 | goto err_node; |
| 1181 | |
| 1182 | /* Results of test in progress */ |
| 1183 | d = debugfs_create_file("results", S_IRUGO, info->root, info, |
| 1184 | &dtf_results_fops); |
| 1185 | if (IS_ERR_OR_NULL(d)) |
| 1186 | goto err_node; |
| 1187 | |
| 1188 | return 0; |
| 1189 | |
| 1190 | err_node: |
| 1191 | debugfs_remove_recursive(info->root); |
| 1192 | err_root: |
| 1193 | pr_err("dmatest: Failed to initialize debugfs\n"); |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | static int __init dmatest_init(void) |
| 1198 | { |
| 1199 | struct dmatest_info *info = &test_info; |
| 1200 | struct dmatest_params *params = &info->params; |
| 1201 | int ret; |
| 1202 | |
| 1203 | memset(info, 0, sizeof(*info)); |
| 1204 | |
| 1205 | mutex_init(&info->lock); |
| 1206 | INIT_LIST_HEAD(&info->channels); |
| 1207 | |
| 1208 | mutex_init(&info->results_lock); |
| 1209 | INIT_LIST_HEAD(&info->results); |
| 1210 | |
| 1211 | /* Set default parameters */ |
| 1212 | params->buf_size = test_buf_size; |
| 1213 | strlcpy(params->channel, test_channel, sizeof(params->channel)); |
| 1214 | strlcpy(params->device, test_device, sizeof(params->device)); |
| 1215 | params->threads_per_chan = threads_per_chan; |
| 1216 | params->max_channels = max_channels; |
| 1217 | params->iterations = iterations; |
| 1218 | params->xor_sources = xor_sources; |
| 1219 | params->pq_sources = pq_sources; |
| 1220 | params->timeout = timeout; |
| 1221 | |
| 1222 | ret = dmatest_register_dbgfs(info); |
| 1223 | if (ret) |
| 1224 | return ret; |
| 1225 | |
| 1226 | #ifdef MODULE |
| 1227 | return 0; |
| 1228 | #else |
| 1229 | return run_threaded_test(info); |
| 1230 | #endif |
| 1231 | } |
| 1232 | /* when compiled-in wait for drivers to load first */ |
| 1233 | late_initcall(dmatest_init); |
| 1234 | |
| 1235 | static void __exit dmatest_exit(void) |
| 1236 | { |
| 1237 | struct dmatest_info *info = &test_info; |
| 1238 | |
| 1239 | debugfs_remove_recursive(info->root); |
| 1240 | stop_threaded_test(info); |
| 1241 | result_free(info, NULL); |
| 1242 | } |
| 1243 | module_exit(dmatest_exit); |
| 1244 | |
| 1245 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
| 1246 | MODULE_LICENSE("GPL v2"); |