blob: f2392d59568d25906691b9b18eaa01db94eb2db5 [file] [log] [blame]
Dan Williamsc2110922007-01-02 13:52:26 -07001/*
2 * offload engine driver for the Intel Xscale series of i/o processors
3 * Copyright © 2006, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20/*
21 * This driver supports the asynchrounous DMA copy and RAID engines available
22 * on the Intel Xscale(R) family of I/O Processors (IOP 32x, 33x, 134x)
23 */
24
25#include <linux/init.h>
26#include <linux/module.h>
Dan Williamsc2110922007-01-02 13:52:26 -070027#include <linux/delay.h>
28#include <linux/dma-mapping.h>
29#include <linux/spinlock.h>
30#include <linux/interrupt.h>
31#include <linux/platform_device.h>
32#include <linux/memory.h>
33#include <linux/ioport.h>
Dan Williamsf6dbf652009-08-29 19:12:40 -070034#include <linux/raid/pq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Dan Williamsc2110922007-01-02 13:52:26 -070036
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/adma.h>
Dan Williamsc2110922007-01-02 13:52:26 -070038
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000039#include "dmaengine.h"
40
Dan Williamsc2110922007-01-02 13:52:26 -070041#define to_iop_adma_chan(chan) container_of(chan, struct iop_adma_chan, common)
42#define to_iop_adma_device(dev) \
43 container_of(dev, struct iop_adma_device, common)
44#define tx_to_iop_adma_slot(tx) \
45 container_of(tx, struct iop_adma_desc_slot, async_tx)
46
47/**
48 * iop_adma_free_slots - flags descriptor slots for reuse
49 * @slot: Slot to free
50 * Caller must hold &iop_chan->lock while calling this function
51 */
52static void iop_adma_free_slots(struct iop_adma_desc_slot *slot)
53{
54 int stride = slot->slots_per_op;
55
56 while (stride--) {
57 slot->slots_per_op = 0;
58 slot = list_entry(slot->slot_node.next,
59 struct iop_adma_desc_slot,
60 slot_node);
61 }
62}
63
Dan Williams7bf649a2009-08-28 14:32:04 -070064static void
65iop_desc_unmap(struct iop_adma_chan *iop_chan, struct iop_adma_desc_slot *desc)
66{
67 struct dma_async_tx_descriptor *tx = &desc->async_tx;
68 struct iop_adma_desc_slot *unmap = desc->group_head;
69 struct device *dev = &iop_chan->device->pdev->dev;
70 u32 len = unmap->unmap_len;
71 enum dma_ctrl_flags flags = tx->flags;
72 u32 src_cnt;
73 dma_addr_t addr;
74 dma_addr_t dest;
75
76 src_cnt = unmap->unmap_src_cnt;
77 dest = iop_desc_get_dest_addr(unmap, iop_chan);
78 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
79 enum dma_data_direction dir;
80
81 if (src_cnt > 1) /* is xor? */
82 dir = DMA_BIDIRECTIONAL;
83 else
84 dir = DMA_FROM_DEVICE;
85
86 dma_unmap_page(dev, dest, len, dir);
87 }
88
89 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
90 while (src_cnt--) {
91 addr = iop_desc_get_src_addr(unmap, iop_chan, src_cnt);
92 if (addr == dest)
93 continue;
94 dma_unmap_page(dev, addr, len, DMA_TO_DEVICE);
95 }
96 }
97 desc->group_head = NULL;
98}
99
100static void
101iop_desc_unmap_pq(struct iop_adma_chan *iop_chan, struct iop_adma_desc_slot *desc)
102{
103 struct dma_async_tx_descriptor *tx = &desc->async_tx;
104 struct iop_adma_desc_slot *unmap = desc->group_head;
105 struct device *dev = &iop_chan->device->pdev->dev;
106 u32 len = unmap->unmap_len;
107 enum dma_ctrl_flags flags = tx->flags;
108 u32 src_cnt = unmap->unmap_src_cnt;
109 dma_addr_t pdest = iop_desc_get_dest_addr(unmap, iop_chan);
110 dma_addr_t qdest = iop_desc_get_qdest_addr(unmap, iop_chan);
111 int i;
112
113 if (tx->flags & DMA_PREP_CONTINUE)
114 src_cnt -= 3;
115
116 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP) && !desc->pq_check_result) {
117 dma_unmap_page(dev, pdest, len, DMA_BIDIRECTIONAL);
118 dma_unmap_page(dev, qdest, len, DMA_BIDIRECTIONAL);
119 }
120
121 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
122 dma_addr_t addr;
123
124 for (i = 0; i < src_cnt; i++) {
125 addr = iop_desc_get_src_addr(unmap, iop_chan, i);
126 dma_unmap_page(dev, addr, len, DMA_TO_DEVICE);
127 }
128 if (desc->pq_check_result) {
129 dma_unmap_page(dev, pdest, len, DMA_TO_DEVICE);
130 dma_unmap_page(dev, qdest, len, DMA_TO_DEVICE);
131 }
132 }
133
134 desc->group_head = NULL;
135}
136
137
Dan Williamsc2110922007-01-02 13:52:26 -0700138static dma_cookie_t
139iop_adma_run_tx_complete_actions(struct iop_adma_desc_slot *desc,
140 struct iop_adma_chan *iop_chan, dma_cookie_t cookie)
141{
Dan Williams507fbec2009-08-29 19:12:39 -0700142 struct dma_async_tx_descriptor *tx = &desc->async_tx;
143
144 BUG_ON(tx->cookie < 0);
145 if (tx->cookie > 0) {
146 cookie = tx->cookie;
147 tx->cookie = 0;
Dan Williamsc2110922007-01-02 13:52:26 -0700148
149 /* call the callback (must not sleep or submit new
150 * operations to this channel)
151 */
Dan Williams507fbec2009-08-29 19:12:39 -0700152 if (tx->callback)
153 tx->callback(tx->callback_param);
Dan Williamsc2110922007-01-02 13:52:26 -0700154
155 /* unmap dma addresses
156 * (unmap_single vs unmap_page?)
157 */
158 if (desc->group_head && desc->unmap_len) {
Dan Williams7bf649a2009-08-28 14:32:04 -0700159 if (iop_desc_is_pq(desc))
160 iop_desc_unmap_pq(iop_chan, desc);
161 else
162 iop_desc_unmap(iop_chan, desc);
Dan Williamsc2110922007-01-02 13:52:26 -0700163 }
164 }
165
166 /* run dependent operations */
Dan Williams507fbec2009-08-29 19:12:39 -0700167 dma_run_dependencies(tx);
Dan Williamsc2110922007-01-02 13:52:26 -0700168
169 return cookie;
170}
171
172static int
173iop_adma_clean_slot(struct iop_adma_desc_slot *desc,
174 struct iop_adma_chan *iop_chan)
175{
176 /* the client is allowed to attach dependent operations
177 * until 'ack' is set
178 */
Dan Williams636bdea2008-04-17 20:17:26 -0700179 if (!async_tx_test_ack(&desc->async_tx))
Dan Williamsc2110922007-01-02 13:52:26 -0700180 return 0;
181
182 /* leave the last descriptor in the chain
183 * so we can append to it
184 */
185 if (desc->chain_node.next == &iop_chan->chain)
186 return 1;
187
188 dev_dbg(iop_chan->device->common.dev,
189 "\tfree slot: %d slots_per_op: %d\n",
190 desc->idx, desc->slots_per_op);
191
192 list_del(&desc->chain_node);
193 iop_adma_free_slots(desc);
194
195 return 0;
196}
197
198static void __iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
199{
200 struct iop_adma_desc_slot *iter, *_iter, *grp_start = NULL;
201 dma_cookie_t cookie = 0;
202 u32 current_desc = iop_chan_get_current_descriptor(iop_chan);
203 int busy = iop_chan_is_busy(iop_chan);
204 int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
205
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700206 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700207 /* free completed slots from the chain starting with
208 * the oldest descriptor
209 */
210 list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
211 chain_node) {
212 pr_debug("\tcookie: %d slot: %d busy: %d "
213 "this_desc: %#x next_desc: %#x ack: %d\n",
214 iter->async_tx.cookie, iter->idx, busy,
215 iter->async_tx.phys, iop_desc_get_next_desc(iter),
Dan Williams636bdea2008-04-17 20:17:26 -0700216 async_tx_test_ack(&iter->async_tx));
Dan Williamsc2110922007-01-02 13:52:26 -0700217 prefetch(_iter);
218 prefetch(&_iter->async_tx);
219
220 /* do not advance past the current descriptor loaded into the
221 * hardware channel, subsequent descriptors are either in
222 * process or have not been submitted
223 */
224 if (seen_current)
225 break;
226
227 /* stop the search if we reach the current descriptor and the
228 * channel is busy, or if it appears that the current descriptor
229 * needs to be re-read (i.e. has been appended to)
230 */
231 if (iter->async_tx.phys == current_desc) {
232 BUG_ON(seen_current++);
233 if (busy || iop_desc_get_next_desc(iter))
234 break;
235 }
236
237 /* detect the start of a group transaction */
238 if (!slot_cnt && !slots_per_op) {
239 slot_cnt = iter->slot_cnt;
240 slots_per_op = iter->slots_per_op;
241 if (slot_cnt <= slots_per_op) {
242 slot_cnt = 0;
243 slots_per_op = 0;
244 }
245 }
246
247 if (slot_cnt) {
248 pr_debug("\tgroup++\n");
249 if (!grp_start)
250 grp_start = iter;
251 slot_cnt -= slots_per_op;
252 }
253
254 /* all the members of a group are complete */
255 if (slots_per_op != 0 && slot_cnt == 0) {
256 struct iop_adma_desc_slot *grp_iter, *_grp_iter;
257 int end_of_chain = 0;
258 pr_debug("\tgroup end\n");
259
260 /* collect the total results */
261 if (grp_start->xor_check_result) {
262 u32 zero_sum_result = 0;
263 slot_cnt = grp_start->slot_cnt;
264 grp_iter = grp_start;
265
266 list_for_each_entry_from(grp_iter,
267 &iop_chan->chain, chain_node) {
268 zero_sum_result |=
269 iop_desc_get_zero_result(grp_iter);
270 pr_debug("\titer%d result: %d\n",
271 grp_iter->idx, zero_sum_result);
272 slot_cnt -= slots_per_op;
273 if (slot_cnt == 0)
274 break;
275 }
276 pr_debug("\tgrp_start->xor_check_result: %p\n",
277 grp_start->xor_check_result);
278 *grp_start->xor_check_result = zero_sum_result;
279 }
280
281 /* clean up the group */
282 slot_cnt = grp_start->slot_cnt;
283 grp_iter = grp_start;
284 list_for_each_entry_safe_from(grp_iter, _grp_iter,
285 &iop_chan->chain, chain_node) {
286 cookie = iop_adma_run_tx_complete_actions(
287 grp_iter, iop_chan, cookie);
288
289 slot_cnt -= slots_per_op;
290 end_of_chain = iop_adma_clean_slot(grp_iter,
291 iop_chan);
292
293 if (slot_cnt == 0 || end_of_chain)
294 break;
295 }
296
297 /* the group should be complete at this point */
298 BUG_ON(slot_cnt);
299
300 slots_per_op = 0;
301 grp_start = NULL;
302 if (end_of_chain)
303 break;
304 else
305 continue;
306 } else if (slots_per_op) /* wait for group completion */
307 continue;
308
309 /* write back zero sum results (single descriptor case) */
310 if (iter->xor_check_result && iter->async_tx.cookie)
311 *iter->xor_check_result =
312 iop_desc_get_zero_result(iter);
313
314 cookie = iop_adma_run_tx_complete_actions(
315 iter, iop_chan, cookie);
316
317 if (iop_adma_clean_slot(iter, iop_chan))
318 break;
319 }
320
Dan Williamsc2110922007-01-02 13:52:26 -0700321 if (cookie > 0) {
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000322 iop_chan->common.completed_cookie = cookie;
Dan Williamsc2110922007-01-02 13:52:26 -0700323 pr_debug("\tcompleted cookie %d\n", cookie);
324 }
325}
326
327static void
328iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
329{
330 spin_lock_bh(&iop_chan->lock);
331 __iop_adma_slot_cleanup(iop_chan);
332 spin_unlock_bh(&iop_chan->lock);
333}
334
335static void iop_adma_tasklet(unsigned long data)
336{
Dan Williams19242d72008-04-17 20:17:25 -0700337 struct iop_adma_chan *iop_chan = (struct iop_adma_chan *) data;
338
Dan Williams72be12f2009-07-14 13:38:29 -0700339 /* lockdep will flag depedency submissions as potentially
340 * recursive locking, this is not the case as a dependency
341 * submission will never recurse a channels submit routine.
342 * There are checks in async_tx.c to prevent this.
343 */
344 spin_lock_nested(&iop_chan->lock, SINGLE_DEPTH_NESTING);
Dan Williams19242d72008-04-17 20:17:25 -0700345 __iop_adma_slot_cleanup(iop_chan);
346 spin_unlock(&iop_chan->lock);
Dan Williamsc2110922007-01-02 13:52:26 -0700347}
348
349static struct iop_adma_desc_slot *
350iop_adma_alloc_slots(struct iop_adma_chan *iop_chan, int num_slots,
351 int slots_per_op)
352{
353 struct iop_adma_desc_slot *iter, *_iter, *alloc_start = NULL;
Denis Chenge73ef9a2008-02-02 19:30:01 -0700354 LIST_HEAD(chain);
Dan Williamsc2110922007-01-02 13:52:26 -0700355 int slots_found, retry = 0;
356
357 /* start search from the last allocated descrtiptor
358 * if a contiguous allocation can not be found start searching
359 * from the beginning of the list
360 */
361retry:
362 slots_found = 0;
363 if (retry == 0)
364 iter = iop_chan->last_used;
365 else
366 iter = list_entry(&iop_chan->all_slots,
367 struct iop_adma_desc_slot,
368 slot_node);
369
370 list_for_each_entry_safe_continue(
371 iter, _iter, &iop_chan->all_slots, slot_node) {
372 prefetch(_iter);
373 prefetch(&_iter->async_tx);
374 if (iter->slots_per_op) {
375 /* give up after finding the first busy slot
376 * on the second pass through the list
377 */
378 if (retry)
379 break;
380
381 slots_found = 0;
382 continue;
383 }
384
385 /* start the allocation if the slot is correctly aligned */
386 if (!slots_found++) {
387 if (iop_desc_is_aligned(iter, slots_per_op))
388 alloc_start = iter;
389 else {
390 slots_found = 0;
391 continue;
392 }
393 }
394
395 if (slots_found == num_slots) {
396 struct iop_adma_desc_slot *alloc_tail = NULL;
397 struct iop_adma_desc_slot *last_used = NULL;
398 iter = alloc_start;
399 while (num_slots) {
400 int i;
401 dev_dbg(iop_chan->device->common.dev,
402 "allocated slot: %d "
403 "(desc %p phys: %#x) slots_per_op %d\n",
404 iter->idx, iter->hw_desc,
405 iter->async_tx.phys, slots_per_op);
406
407 /* pre-ack all but the last descriptor */
408 if (num_slots != slots_per_op)
Dan Williams636bdea2008-04-17 20:17:26 -0700409 async_tx_ack(&iter->async_tx);
Dan Williamsc2110922007-01-02 13:52:26 -0700410
411 list_add_tail(&iter->chain_node, &chain);
412 alloc_tail = iter;
413 iter->async_tx.cookie = 0;
414 iter->slot_cnt = num_slots;
415 iter->xor_check_result = NULL;
416 for (i = 0; i < slots_per_op; i++) {
417 iter->slots_per_op = slots_per_op - i;
418 last_used = iter;
419 iter = list_entry(iter->slot_node.next,
420 struct iop_adma_desc_slot,
421 slot_node);
422 }
423 num_slots -= slots_per_op;
424 }
425 alloc_tail->group_head = alloc_start;
426 alloc_tail->async_tx.cookie = -EBUSY;
Dan Williams308136d2009-09-08 17:53:02 -0700427 list_splice(&chain, &alloc_tail->tx_list);
Dan Williamsc2110922007-01-02 13:52:26 -0700428 iop_chan->last_used = last_used;
429 iop_desc_clear_next_desc(alloc_start);
430 iop_desc_clear_next_desc(alloc_tail);
431 return alloc_tail;
432 }
433 }
434 if (!retry++)
435 goto retry;
436
Dan Williamsc7141d02008-07-17 17:59:56 -0700437 /* perform direct reclaim if the allocation fails */
438 __iop_adma_slot_cleanup(iop_chan);
Dan Williamsc2110922007-01-02 13:52:26 -0700439
440 return NULL;
441}
442
Dan Williamsc2110922007-01-02 13:52:26 -0700443static void iop_adma_check_threshold(struct iop_adma_chan *iop_chan)
444{
445 dev_dbg(iop_chan->device->common.dev, "pending: %d\n",
446 iop_chan->pending);
447
448 if (iop_chan->pending >= IOP_ADMA_THRESHOLD) {
449 iop_chan->pending = 0;
450 iop_chan_append(iop_chan);
451 }
452}
453
454static dma_cookie_t
455iop_adma_tx_submit(struct dma_async_tx_descriptor *tx)
456{
457 struct iop_adma_desc_slot *sw_desc = tx_to_iop_adma_slot(tx);
458 struct iop_adma_chan *iop_chan = to_iop_adma_chan(tx->chan);
459 struct iop_adma_desc_slot *grp_start, *old_chain_tail;
460 int slot_cnt;
461 int slots_per_op;
462 dma_cookie_t cookie;
Dan Williams137cb552008-11-11 13:12:33 -0700463 dma_addr_t next_dma;
Dan Williamsc2110922007-01-02 13:52:26 -0700464
465 grp_start = sw_desc->group_head;
466 slot_cnt = grp_start->slot_cnt;
467 slots_per_op = grp_start->slots_per_op;
468
469 spin_lock_bh(&iop_chan->lock);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000470 cookie = dma_cookie_assign(tx);
Dan Williamsc2110922007-01-02 13:52:26 -0700471
472 old_chain_tail = list_entry(iop_chan->chain.prev,
473 struct iop_adma_desc_slot, chain_node);
Dan Williams308136d2009-09-08 17:53:02 -0700474 list_splice_init(&sw_desc->tx_list,
Dan Williamsc2110922007-01-02 13:52:26 -0700475 &old_chain_tail->chain_node);
476
477 /* fix up the hardware chain */
Dan Williams137cb552008-11-11 13:12:33 -0700478 next_dma = grp_start->async_tx.phys;
479 iop_desc_set_next_desc(old_chain_tail, next_dma);
480 BUG_ON(iop_desc_get_next_desc(old_chain_tail) != next_dma); /* flush */
Dan Williamsc2110922007-01-02 13:52:26 -0700481
Dan Williams137cb552008-11-11 13:12:33 -0700482 /* check for pre-chained descriptors */
Dan Williams65e50382008-11-11 13:12:33 -0700483 iop_paranoia(iop_desc_get_next_desc(sw_desc));
Dan Williamsc2110922007-01-02 13:52:26 -0700484
485 /* increment the pending count by the number of slots
486 * memcpy operations have a 1:1 (slot:operation) relation
487 * other operations are heavier and will pop the threshold
488 * more often.
489 */
490 iop_chan->pending += slot_cnt;
491 iop_adma_check_threshold(iop_chan);
492 spin_unlock_bh(&iop_chan->lock);
493
494 dev_dbg(iop_chan->device->common.dev, "%s cookie: %d slot: %d\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700495 __func__, sw_desc->async_tx.cookie, sw_desc->idx);
Dan Williamsc2110922007-01-02 13:52:26 -0700496
497 return cookie;
498}
499
Dan Williamsc2110922007-01-02 13:52:26 -0700500static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan);
501static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan);
502
Dan Williams5eb907a2008-07-17 17:59:56 -0700503/**
504 * iop_adma_alloc_chan_resources - returns the number of allocated descriptors
505 * @chan - allocate descriptor resources for this channel
506 * @client - current client requesting the channel be ready for requests
507 *
508 * Note: We keep the slots for 1 operation on iop_chan->chain at all times. To
509 * avoid deadlock, via async_xor, num_descs_in_pool must at a minimum be
510 * greater than 2x the number slots needed to satisfy a device->max_xor
511 * request.
512 * */
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700513static int iop_adma_alloc_chan_resources(struct dma_chan *chan)
Dan Williamsc2110922007-01-02 13:52:26 -0700514{
515 char *hw_desc;
516 int idx;
517 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
518 struct iop_adma_desc_slot *slot = NULL;
519 int init = iop_chan->slots_allocated ? 0 : 1;
520 struct iop_adma_platform_data *plat_data =
521 iop_chan->device->pdev->dev.platform_data;
522 int num_descs_in_pool = plat_data->pool_size/IOP_ADMA_SLOT_SIZE;
523
524 /* Allocate descriptor slots */
525 do {
526 idx = iop_chan->slots_allocated;
527 if (idx == num_descs_in_pool)
528 break;
529
530 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
531 if (!slot) {
532 printk(KERN_INFO "IOP ADMA Channel only initialized"
533 " %d descriptor slots", idx);
534 break;
535 }
536 hw_desc = (char *) iop_chan->device->dma_desc_pool_virt;
537 slot->hw_desc = (void *) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
538
539 dma_async_tx_descriptor_init(&slot->async_tx, chan);
540 slot->async_tx.tx_submit = iop_adma_tx_submit;
Dan Williams308136d2009-09-08 17:53:02 -0700541 INIT_LIST_HEAD(&slot->tx_list);
Dan Williamsc2110922007-01-02 13:52:26 -0700542 INIT_LIST_HEAD(&slot->chain_node);
543 INIT_LIST_HEAD(&slot->slot_node);
Dan Williamsc2110922007-01-02 13:52:26 -0700544 hw_desc = (char *) iop_chan->device->dma_desc_pool;
545 slot->async_tx.phys =
546 (dma_addr_t) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
547 slot->idx = idx;
548
549 spin_lock_bh(&iop_chan->lock);
550 iop_chan->slots_allocated++;
551 list_add_tail(&slot->slot_node, &iop_chan->all_slots);
552 spin_unlock_bh(&iop_chan->lock);
553 } while (iop_chan->slots_allocated < num_descs_in_pool);
554
555 if (idx && !iop_chan->last_used)
556 iop_chan->last_used = list_entry(iop_chan->all_slots.next,
557 struct iop_adma_desc_slot,
558 slot_node);
559
560 dev_dbg(iop_chan->device->common.dev,
561 "allocated %d descriptor slots last_used: %p\n",
562 iop_chan->slots_allocated, iop_chan->last_used);
563
564 /* initialize the channel and the chain with a null operation */
565 if (init) {
566 if (dma_has_cap(DMA_MEMCPY,
567 iop_chan->device->common.cap_mask))
568 iop_chan_start_null_memcpy(iop_chan);
569 else if (dma_has_cap(DMA_XOR,
570 iop_chan->device->common.cap_mask))
571 iop_chan_start_null_xor(iop_chan);
572 else
573 BUG();
574 }
575
576 return (idx > 0) ? idx : -ENOMEM;
577}
578
579static struct dma_async_tx_descriptor *
Dan Williams636bdea2008-04-17 20:17:26 -0700580iop_adma_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700581{
582 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
583 struct iop_adma_desc_slot *sw_desc, *grp_start;
584 int slot_cnt, slots_per_op;
585
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700586 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700587
588 spin_lock_bh(&iop_chan->lock);
589 slot_cnt = iop_chan_interrupt_slot_count(&slots_per_op, iop_chan);
590 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
591 if (sw_desc) {
592 grp_start = sw_desc->group_head;
593 iop_desc_init_interrupt(grp_start, iop_chan);
594 grp_start->unmap_len = 0;
Dan Williams636bdea2008-04-17 20:17:26 -0700595 sw_desc->async_tx.flags = flags;
Dan Williamsc2110922007-01-02 13:52:26 -0700596 }
597 spin_unlock_bh(&iop_chan->lock);
598
599 return sw_desc ? &sw_desc->async_tx : NULL;
600}
601
Dan Williamsc2110922007-01-02 13:52:26 -0700602static struct dma_async_tx_descriptor *
Dan Williams00367312008-02-02 19:49:57 -0700603iop_adma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700604 dma_addr_t dma_src, size_t len, unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700605{
606 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
607 struct iop_adma_desc_slot *sw_desc, *grp_start;
608 int slot_cnt, slots_per_op;
609
610 if (unlikely(!len))
611 return NULL;
Coly Lie2ec7712011-03-27 01:26:52 +0800612 BUG_ON(len > IOP_ADMA_MAX_BYTE_COUNT);
Dan Williamsc2110922007-01-02 13:52:26 -0700613
614 dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700615 __func__, len);
Dan Williamsc2110922007-01-02 13:52:26 -0700616
617 spin_lock_bh(&iop_chan->lock);
618 slot_cnt = iop_chan_memcpy_slot_count(len, &slots_per_op);
619 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
620 if (sw_desc) {
621 grp_start = sw_desc->group_head;
Dan Williamsd4c56f92008-02-02 19:49:58 -0700622 iop_desc_init_memcpy(grp_start, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700623 iop_desc_set_byte_count(grp_start, iop_chan, len);
Dan Williams00367312008-02-02 19:49:57 -0700624 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
625 iop_desc_set_memcpy_src_addr(grp_start, dma_src);
Dan Williamsc2110922007-01-02 13:52:26 -0700626 sw_desc->unmap_src_cnt = 1;
627 sw_desc->unmap_len = len;
Dan Williams636bdea2008-04-17 20:17:26 -0700628 sw_desc->async_tx.flags = flags;
Dan Williamsc2110922007-01-02 13:52:26 -0700629 }
630 spin_unlock_bh(&iop_chan->lock);
631
632 return sw_desc ? &sw_desc->async_tx : NULL;
633}
634
635static struct dma_async_tx_descriptor *
Dan Williams00367312008-02-02 19:49:57 -0700636iop_adma_prep_dma_memset(struct dma_chan *chan, dma_addr_t dma_dest,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700637 int value, size_t len, unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700638{
639 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
640 struct iop_adma_desc_slot *sw_desc, *grp_start;
641 int slot_cnt, slots_per_op;
642
643 if (unlikely(!len))
644 return NULL;
Coly Lie2ec7712011-03-27 01:26:52 +0800645 BUG_ON(len > IOP_ADMA_MAX_BYTE_COUNT);
Dan Williamsc2110922007-01-02 13:52:26 -0700646
647 dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700648 __func__, len);
Dan Williamsc2110922007-01-02 13:52:26 -0700649
650 spin_lock_bh(&iop_chan->lock);
651 slot_cnt = iop_chan_memset_slot_count(len, &slots_per_op);
652 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
653 if (sw_desc) {
654 grp_start = sw_desc->group_head;
Dan Williamsd4c56f92008-02-02 19:49:58 -0700655 iop_desc_init_memset(grp_start, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700656 iop_desc_set_byte_count(grp_start, iop_chan, len);
657 iop_desc_set_block_fill_val(grp_start, value);
Dan Williams00367312008-02-02 19:49:57 -0700658 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
Dan Williamsc2110922007-01-02 13:52:26 -0700659 sw_desc->unmap_src_cnt = 1;
660 sw_desc->unmap_len = len;
Dan Williams636bdea2008-04-17 20:17:26 -0700661 sw_desc->async_tx.flags = flags;
Dan Williamsc2110922007-01-02 13:52:26 -0700662 }
663 spin_unlock_bh(&iop_chan->lock);
664
665 return sw_desc ? &sw_desc->async_tx : NULL;
666}
667
Dan Williamsc2110922007-01-02 13:52:26 -0700668static struct dma_async_tx_descriptor *
Dan Williams00367312008-02-02 19:49:57 -0700669iop_adma_prep_dma_xor(struct dma_chan *chan, dma_addr_t dma_dest,
670 dma_addr_t *dma_src, unsigned int src_cnt, size_t len,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700671 unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700672{
673 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
674 struct iop_adma_desc_slot *sw_desc, *grp_start;
675 int slot_cnt, slots_per_op;
676
677 if (unlikely(!len))
678 return NULL;
Coly Lie2ec7712011-03-27 01:26:52 +0800679 BUG_ON(len > IOP_ADMA_XOR_MAX_BYTE_COUNT);
Dan Williamsc2110922007-01-02 13:52:26 -0700680
681 dev_dbg(iop_chan->device->common.dev,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700682 "%s src_cnt: %d len: %u flags: %lx\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700683 __func__, src_cnt, len, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700684
685 spin_lock_bh(&iop_chan->lock);
686 slot_cnt = iop_chan_xor_slot_count(len, src_cnt, &slots_per_op);
687 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
688 if (sw_desc) {
689 grp_start = sw_desc->group_head;
Dan Williamsd4c56f92008-02-02 19:49:58 -0700690 iop_desc_init_xor(grp_start, src_cnt, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700691 iop_desc_set_byte_count(grp_start, iop_chan, len);
Dan Williams00367312008-02-02 19:49:57 -0700692 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
Dan Williamsc2110922007-01-02 13:52:26 -0700693 sw_desc->unmap_src_cnt = src_cnt;
694 sw_desc->unmap_len = len;
Dan Williams636bdea2008-04-17 20:17:26 -0700695 sw_desc->async_tx.flags = flags;
Dan Williams00367312008-02-02 19:49:57 -0700696 while (src_cnt--)
697 iop_desc_set_xor_src_addr(grp_start, src_cnt,
698 dma_src[src_cnt]);
Dan Williamsc2110922007-01-02 13:52:26 -0700699 }
700 spin_unlock_bh(&iop_chan->lock);
701
702 return sw_desc ? &sw_desc->async_tx : NULL;
703}
704
Dan Williamsc2110922007-01-02 13:52:26 -0700705static struct dma_async_tx_descriptor *
Dan Williams099f53c2009-04-08 14:28:37 -0700706iop_adma_prep_dma_xor_val(struct dma_chan *chan, dma_addr_t *dma_src,
707 unsigned int src_cnt, size_t len, u32 *result,
708 unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700709{
710 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
711 struct iop_adma_desc_slot *sw_desc, *grp_start;
712 int slot_cnt, slots_per_op;
713
714 if (unlikely(!len))
715 return NULL;
716
717 dev_dbg(iop_chan->device->common.dev, "%s src_cnt: %d len: %u\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700718 __func__, src_cnt, len);
Dan Williamsc2110922007-01-02 13:52:26 -0700719
720 spin_lock_bh(&iop_chan->lock);
721 slot_cnt = iop_chan_zero_sum_slot_count(len, src_cnt, &slots_per_op);
722 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
723 if (sw_desc) {
724 grp_start = sw_desc->group_head;
Dan Williamsd4c56f92008-02-02 19:49:58 -0700725 iop_desc_init_zero_sum(grp_start, src_cnt, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700726 iop_desc_set_zero_sum_byte_count(grp_start, len);
727 grp_start->xor_check_result = result;
728 pr_debug("\t%s: grp_start->xor_check_result: %p\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700729 __func__, grp_start->xor_check_result);
Dan Williamsc2110922007-01-02 13:52:26 -0700730 sw_desc->unmap_src_cnt = src_cnt;
731 sw_desc->unmap_len = len;
Dan Williams636bdea2008-04-17 20:17:26 -0700732 sw_desc->async_tx.flags = flags;
Dan Williams00367312008-02-02 19:49:57 -0700733 while (src_cnt--)
734 iop_desc_set_zero_sum_src_addr(grp_start, src_cnt,
735 dma_src[src_cnt]);
Dan Williamsc2110922007-01-02 13:52:26 -0700736 }
737 spin_unlock_bh(&iop_chan->lock);
738
739 return sw_desc ? &sw_desc->async_tx : NULL;
740}
741
Dan Williams7bf649a2009-08-28 14:32:04 -0700742static struct dma_async_tx_descriptor *
743iop_adma_prep_dma_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
744 unsigned int src_cnt, const unsigned char *scf, size_t len,
745 unsigned long flags)
746{
747 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
748 struct iop_adma_desc_slot *sw_desc, *g;
749 int slot_cnt, slots_per_op;
750 int continue_srcs;
751
752 if (unlikely(!len))
753 return NULL;
754 BUG_ON(len > IOP_ADMA_XOR_MAX_BYTE_COUNT);
755
756 dev_dbg(iop_chan->device->common.dev,
757 "%s src_cnt: %d len: %u flags: %lx\n",
758 __func__, src_cnt, len, flags);
759
760 if (dmaf_p_disabled_continue(flags))
761 continue_srcs = 1+src_cnt;
762 else if (dmaf_continue(flags))
763 continue_srcs = 3+src_cnt;
764 else
765 continue_srcs = 0+src_cnt;
766
767 spin_lock_bh(&iop_chan->lock);
768 slot_cnt = iop_chan_pq_slot_count(len, continue_srcs, &slots_per_op);
769 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
770 if (sw_desc) {
771 int i;
772
773 g = sw_desc->group_head;
774 iop_desc_set_byte_count(g, iop_chan, len);
775
776 /* even if P is disabled its destination address (bits
777 * [3:0]) must match Q. It is ok if P points to an
778 * invalid address, it won't be written.
779 */
780 if (flags & DMA_PREP_PQ_DISABLE_P)
781 dst[0] = dst[1] & 0x7;
782
783 iop_desc_set_pq_addr(g, dst);
784 sw_desc->unmap_src_cnt = src_cnt;
785 sw_desc->unmap_len = len;
786 sw_desc->async_tx.flags = flags;
787 for (i = 0; i < src_cnt; i++)
788 iop_desc_set_pq_src_addr(g, i, src[i], scf[i]);
789
790 /* if we are continuing a previous operation factor in
791 * the old p and q values, see the comment for dma_maxpq
792 * in include/linux/dmaengine.h
793 */
794 if (dmaf_p_disabled_continue(flags))
795 iop_desc_set_pq_src_addr(g, i++, dst[1], 1);
796 else if (dmaf_continue(flags)) {
797 iop_desc_set_pq_src_addr(g, i++, dst[0], 0);
798 iop_desc_set_pq_src_addr(g, i++, dst[1], 1);
799 iop_desc_set_pq_src_addr(g, i++, dst[1], 0);
800 }
801 iop_desc_init_pq(g, i, flags);
802 }
803 spin_unlock_bh(&iop_chan->lock);
804
805 return sw_desc ? &sw_desc->async_tx : NULL;
806}
807
808static struct dma_async_tx_descriptor *
809iop_adma_prep_dma_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
810 unsigned int src_cnt, const unsigned char *scf,
811 size_t len, enum sum_check_flags *pqres,
812 unsigned long flags)
813{
814 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
815 struct iop_adma_desc_slot *sw_desc, *g;
816 int slot_cnt, slots_per_op;
817
818 if (unlikely(!len))
819 return NULL;
820 BUG_ON(len > IOP_ADMA_XOR_MAX_BYTE_COUNT);
821
822 dev_dbg(iop_chan->device->common.dev, "%s src_cnt: %d len: %u\n",
823 __func__, src_cnt, len);
824
825 spin_lock_bh(&iop_chan->lock);
826 slot_cnt = iop_chan_pq_zero_sum_slot_count(len, src_cnt + 2, &slots_per_op);
827 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
828 if (sw_desc) {
829 /* for validate operations p and q are tagged onto the
830 * end of the source list
831 */
832 int pq_idx = src_cnt;
833
834 g = sw_desc->group_head;
835 iop_desc_init_pq_zero_sum(g, src_cnt+2, flags);
836 iop_desc_set_pq_zero_sum_byte_count(g, len);
837 g->pq_check_result = pqres;
838 pr_debug("\t%s: g->pq_check_result: %p\n",
839 __func__, g->pq_check_result);
840 sw_desc->unmap_src_cnt = src_cnt+2;
841 sw_desc->unmap_len = len;
842 sw_desc->async_tx.flags = flags;
843 while (src_cnt--)
844 iop_desc_set_pq_zero_sum_src_addr(g, src_cnt,
845 src[src_cnt],
846 scf[src_cnt]);
847 iop_desc_set_pq_zero_sum_addr(g, pq_idx, src);
848 }
849 spin_unlock_bh(&iop_chan->lock);
850
851 return sw_desc ? &sw_desc->async_tx : NULL;
852}
853
Dan Williamsc2110922007-01-02 13:52:26 -0700854static void iop_adma_free_chan_resources(struct dma_chan *chan)
855{
856 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
857 struct iop_adma_desc_slot *iter, *_iter;
858 int in_use_descs = 0;
859
860 iop_adma_slot_cleanup(iop_chan);
861
862 spin_lock_bh(&iop_chan->lock);
863 list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
864 chain_node) {
865 in_use_descs++;
866 list_del(&iter->chain_node);
867 }
868 list_for_each_entry_safe_reverse(
869 iter, _iter, &iop_chan->all_slots, slot_node) {
870 list_del(&iter->slot_node);
871 kfree(iter);
872 iop_chan->slots_allocated--;
873 }
874 iop_chan->last_used = NULL;
875
876 dev_dbg(iop_chan->device->common.dev, "%s slots_allocated %d\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700877 __func__, iop_chan->slots_allocated);
Dan Williamsc2110922007-01-02 13:52:26 -0700878 spin_unlock_bh(&iop_chan->lock);
879
880 /* one is ok since we left it on there on purpose */
881 if (in_use_descs > 1)
882 printk(KERN_ERR "IOP: Freeing %d in use descriptors!\n",
883 in_use_descs - 1);
884}
885
886/**
Linus Walleij07934482010-03-26 16:50:49 -0700887 * iop_adma_status - poll the status of an ADMA transaction
Dan Williamsc2110922007-01-02 13:52:26 -0700888 * @chan: ADMA channel handle
889 * @cookie: ADMA transaction identifier
Linus Walleij07934482010-03-26 16:50:49 -0700890 * @txstate: a holder for the current state of the channel or NULL
Dan Williamsc2110922007-01-02 13:52:26 -0700891 */
Linus Walleij07934482010-03-26 16:50:49 -0700892static enum dma_status iop_adma_status(struct dma_chan *chan,
Dan Williamsc2110922007-01-02 13:52:26 -0700893 dma_cookie_t cookie,
Linus Walleij07934482010-03-26 16:50:49 -0700894 struct dma_tx_state *txstate)
Dan Williamsc2110922007-01-02 13:52:26 -0700895{
896 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
897 dma_cookie_t last_used;
898 dma_cookie_t last_complete;
899 enum dma_status ret;
900
901 last_used = chan->cookie;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000902 last_complete = chan->completed_cookie;
Dan Williamsbca34692010-03-26 16:52:10 -0700903 dma_set_tx_state(txstate, last_complete, last_used, 0);
Dan Williamsc2110922007-01-02 13:52:26 -0700904 ret = dma_async_is_complete(cookie, last_complete, last_used);
905 if (ret == DMA_SUCCESS)
906 return ret;
907
908 iop_adma_slot_cleanup(iop_chan);
909
910 last_used = chan->cookie;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000911 last_complete = chan->completed_cookie;
Dan Williamsbca34692010-03-26 16:52:10 -0700912 dma_set_tx_state(txstate, last_complete, last_used, 0);
Dan Williamsc2110922007-01-02 13:52:26 -0700913
914 return dma_async_is_complete(cookie, last_complete, last_used);
915}
916
917static irqreturn_t iop_adma_eot_handler(int irq, void *data)
918{
919 struct iop_adma_chan *chan = data;
920
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700921 dev_dbg(chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700922
923 tasklet_schedule(&chan->irq_tasklet);
924
925 iop_adma_device_clear_eot_status(chan);
926
927 return IRQ_HANDLED;
928}
929
930static irqreturn_t iop_adma_eoc_handler(int irq, void *data)
931{
932 struct iop_adma_chan *chan = data;
933
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700934 dev_dbg(chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700935
936 tasklet_schedule(&chan->irq_tasklet);
937
938 iop_adma_device_clear_eoc_status(chan);
939
940 return IRQ_HANDLED;
941}
942
943static irqreturn_t iop_adma_err_handler(int irq, void *data)
944{
945 struct iop_adma_chan *chan = data;
946 unsigned long status = iop_chan_get_status(chan);
947
948 dev_printk(KERN_ERR, chan->device->common.dev,
949 "error ( %s%s%s%s%s%s%s)\n",
950 iop_is_err_int_parity(status, chan) ? "int_parity " : "",
951 iop_is_err_mcu_abort(status, chan) ? "mcu_abort " : "",
952 iop_is_err_int_tabort(status, chan) ? "int_tabort " : "",
953 iop_is_err_int_mabort(status, chan) ? "int_mabort " : "",
954 iop_is_err_pci_tabort(status, chan) ? "pci_tabort " : "",
955 iop_is_err_pci_mabort(status, chan) ? "pci_mabort " : "",
956 iop_is_err_split_tx(status, chan) ? "split_tx " : "");
957
958 iop_adma_device_clear_err_status(chan);
959
960 BUG();
961
962 return IRQ_HANDLED;
963}
964
965static void iop_adma_issue_pending(struct dma_chan *chan)
966{
967 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
968
969 if (iop_chan->pending) {
970 iop_chan->pending = 0;
971 iop_chan_append(iop_chan);
972 }
973}
974
975/*
976 * Perform a transaction to verify the HW works.
977 */
978#define IOP_ADMA_TEST_SIZE 2000
979
980static int __devinit iop_adma_memcpy_self_test(struct iop_adma_device *device)
981{
982 int i;
983 void *src, *dest;
984 dma_addr_t src_dma, dest_dma;
985 struct dma_chan *dma_chan;
986 dma_cookie_t cookie;
987 struct dma_async_tx_descriptor *tx;
988 int err = 0;
989 struct iop_adma_chan *iop_chan;
990
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700991 dev_dbg(device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700992
Christophe Jailleteccf2142008-05-20 16:33:06 -0700993 src = kmalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
Dan Williamsc2110922007-01-02 13:52:26 -0700994 if (!src)
995 return -ENOMEM;
Christophe Jailleteccf2142008-05-20 16:33:06 -0700996 dest = kzalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
Dan Williamsc2110922007-01-02 13:52:26 -0700997 if (!dest) {
998 kfree(src);
999 return -ENOMEM;
1000 }
1001
1002 /* Fill in src buffer */
1003 for (i = 0; i < IOP_ADMA_TEST_SIZE; i++)
1004 ((u8 *) src)[i] = (u8)i;
1005
Dan Williamsc2110922007-01-02 13:52:26 -07001006 /* Start copy, using first DMA channel */
1007 dma_chan = container_of(device->common.channels.next,
1008 struct dma_chan,
1009 device_node);
Dan Williamsaa1e6f12009-01-06 11:38:17 -07001010 if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
Dan Williamsc2110922007-01-02 13:52:26 -07001011 err = -ENODEV;
1012 goto out;
1013 }
1014
Dan Williamsc2110922007-01-02 13:52:26 -07001015 dest_dma = dma_map_single(dma_chan->device->dev, dest,
1016 IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
Dan Williamsc2110922007-01-02 13:52:26 -07001017 src_dma = dma_map_single(dma_chan->device->dev, src,
1018 IOP_ADMA_TEST_SIZE, DMA_TO_DEVICE);
Dan Williams00367312008-02-02 19:49:57 -07001019 tx = iop_adma_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
Dan Williams636bdea2008-04-17 20:17:26 -07001020 IOP_ADMA_TEST_SIZE,
1021 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -07001022
1023 cookie = iop_adma_tx_submit(tx);
1024 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -07001025 msleep(1);
1026
Linus Walleij07934482010-03-26 16:50:49 -07001027 if (iop_adma_status(dma_chan, cookie, NULL) !=
Dan Williamsc2110922007-01-02 13:52:26 -07001028 DMA_SUCCESS) {
1029 dev_printk(KERN_ERR, dma_chan->device->dev,
1030 "Self-test copy timed out, disabling\n");
1031 err = -ENODEV;
1032 goto free_resources;
1033 }
1034
1035 iop_chan = to_iop_adma_chan(dma_chan);
1036 dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
1037 IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
1038 if (memcmp(src, dest, IOP_ADMA_TEST_SIZE)) {
1039 dev_printk(KERN_ERR, dma_chan->device->dev,
1040 "Self-test copy failed compare, disabling\n");
1041 err = -ENODEV;
1042 goto free_resources;
1043 }
1044
1045free_resources:
1046 iop_adma_free_chan_resources(dma_chan);
1047out:
1048 kfree(src);
1049 kfree(dest);
1050 return err;
1051}
1052
1053#define IOP_ADMA_NUM_SRC_TEST 4 /* must be <= 15 */
1054static int __devinit
Dan Williams099f53c2009-04-08 14:28:37 -07001055iop_adma_xor_val_self_test(struct iop_adma_device *device)
Dan Williamsc2110922007-01-02 13:52:26 -07001056{
1057 int i, src_idx;
1058 struct page *dest;
1059 struct page *xor_srcs[IOP_ADMA_NUM_SRC_TEST];
1060 struct page *zero_sum_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
Dan Williams00367312008-02-02 19:49:57 -07001061 dma_addr_t dma_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
Dan Williamsc2110922007-01-02 13:52:26 -07001062 dma_addr_t dma_addr, dest_dma;
1063 struct dma_async_tx_descriptor *tx;
1064 struct dma_chan *dma_chan;
1065 dma_cookie_t cookie;
1066 u8 cmp_byte = 0;
1067 u32 cmp_word;
1068 u32 zero_sum_result;
1069 int err = 0;
1070 struct iop_adma_chan *iop_chan;
1071
Harvey Harrison3d9b5252008-03-13 17:45:28 -07001072 dev_dbg(device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -07001073
1074 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
1075 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +01001076 if (!xor_srcs[src_idx]) {
1077 while (src_idx--)
Dan Williamsc2110922007-01-02 13:52:26 -07001078 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +01001079 return -ENOMEM;
1080 }
Dan Williamsc2110922007-01-02 13:52:26 -07001081 }
1082
1083 dest = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +01001084 if (!dest) {
1085 while (src_idx--)
Dan Williamsc2110922007-01-02 13:52:26 -07001086 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +01001087 return -ENOMEM;
1088 }
Dan Williamsc2110922007-01-02 13:52:26 -07001089
1090 /* Fill in src buffers */
1091 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
1092 u8 *ptr = page_address(xor_srcs[src_idx]);
1093 for (i = 0; i < PAGE_SIZE; i++)
1094 ptr[i] = (1 << src_idx);
1095 }
1096
1097 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++)
1098 cmp_byte ^= (u8) (1 << src_idx);
1099
1100 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
1101 (cmp_byte << 8) | cmp_byte;
1102
1103 memset(page_address(dest), 0, PAGE_SIZE);
1104
1105 dma_chan = container_of(device->common.channels.next,
1106 struct dma_chan,
1107 device_node);
Dan Williamsaa1e6f12009-01-06 11:38:17 -07001108 if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
Dan Williamsc2110922007-01-02 13:52:26 -07001109 err = -ENODEV;
1110 goto out;
1111 }
1112
1113 /* test xor */
Dan Williamsc2110922007-01-02 13:52:26 -07001114 dest_dma = dma_map_page(dma_chan->device->dev, dest, 0,
1115 PAGE_SIZE, DMA_FROM_DEVICE);
Dan Williams00367312008-02-02 19:49:57 -07001116 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
1117 dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
1118 0, PAGE_SIZE, DMA_TO_DEVICE);
1119 tx = iop_adma_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
Dan Williams636bdea2008-04-17 20:17:26 -07001120 IOP_ADMA_NUM_SRC_TEST, PAGE_SIZE,
1121 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -07001122
1123 cookie = iop_adma_tx_submit(tx);
1124 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -07001125 msleep(8);
1126
Linus Walleij07934482010-03-26 16:50:49 -07001127 if (iop_adma_status(dma_chan, cookie, NULL) !=
Dan Williamsc2110922007-01-02 13:52:26 -07001128 DMA_SUCCESS) {
1129 dev_printk(KERN_ERR, dma_chan->device->dev,
1130 "Self-test xor timed out, disabling\n");
1131 err = -ENODEV;
1132 goto free_resources;
1133 }
1134
1135 iop_chan = to_iop_adma_chan(dma_chan);
1136 dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
1137 PAGE_SIZE, DMA_FROM_DEVICE);
1138 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
1139 u32 *ptr = page_address(dest);
1140 if (ptr[i] != cmp_word) {
1141 dev_printk(KERN_ERR, dma_chan->device->dev,
1142 "Self-test xor failed compare, disabling\n");
1143 err = -ENODEV;
1144 goto free_resources;
1145 }
1146 }
1147 dma_sync_single_for_device(&iop_chan->device->pdev->dev, dest_dma,
1148 PAGE_SIZE, DMA_TO_DEVICE);
1149
1150 /* skip zero sum if the capability is not present */
Dan Williams099f53c2009-04-08 14:28:37 -07001151 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
Dan Williamsc2110922007-01-02 13:52:26 -07001152 goto free_resources;
1153
1154 /* zero sum the sources with the destintation page */
1155 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
1156 zero_sum_srcs[i] = xor_srcs[i];
1157 zero_sum_srcs[i] = dest;
1158
1159 zero_sum_result = 1;
1160
Dan Williams00367312008-02-02 19:49:57 -07001161 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
1162 dma_srcs[i] = dma_map_page(dma_chan->device->dev,
1163 zero_sum_srcs[i], 0, PAGE_SIZE,
1164 DMA_TO_DEVICE);
Dan Williams099f53c2009-04-08 14:28:37 -07001165 tx = iop_adma_prep_dma_xor_val(dma_chan, dma_srcs,
1166 IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
1167 &zero_sum_result,
1168 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -07001169
1170 cookie = iop_adma_tx_submit(tx);
1171 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -07001172 msleep(8);
1173
Linus Walleij07934482010-03-26 16:50:49 -07001174 if (iop_adma_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
Dan Williamsc2110922007-01-02 13:52:26 -07001175 dev_printk(KERN_ERR, dma_chan->device->dev,
1176 "Self-test zero sum timed out, disabling\n");
1177 err = -ENODEV;
1178 goto free_resources;
1179 }
1180
1181 if (zero_sum_result != 0) {
1182 dev_printk(KERN_ERR, dma_chan->device->dev,
1183 "Self-test zero sum failed compare, disabling\n");
1184 err = -ENODEV;
1185 goto free_resources;
1186 }
1187
1188 /* test memset */
Dan Williamsc2110922007-01-02 13:52:26 -07001189 dma_addr = dma_map_page(dma_chan->device->dev, dest, 0,
1190 PAGE_SIZE, DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -07001191 tx = iop_adma_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
1192 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -07001193
1194 cookie = iop_adma_tx_submit(tx);
1195 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -07001196 msleep(8);
1197
Linus Walleij07934482010-03-26 16:50:49 -07001198 if (iop_adma_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
Dan Williamsc2110922007-01-02 13:52:26 -07001199 dev_printk(KERN_ERR, dma_chan->device->dev,
1200 "Self-test memset timed out, disabling\n");
1201 err = -ENODEV;
1202 goto free_resources;
1203 }
1204
1205 for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
1206 u32 *ptr = page_address(dest);
1207 if (ptr[i]) {
1208 dev_printk(KERN_ERR, dma_chan->device->dev,
1209 "Self-test memset failed compare, disabling\n");
1210 err = -ENODEV;
1211 goto free_resources;
1212 }
1213 }
1214
1215 /* test for non-zero parity sum */
1216 zero_sum_result = 0;
Dan Williams00367312008-02-02 19:49:57 -07001217 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
1218 dma_srcs[i] = dma_map_page(dma_chan->device->dev,
1219 zero_sum_srcs[i], 0, PAGE_SIZE,
1220 DMA_TO_DEVICE);
Dan Williams099f53c2009-04-08 14:28:37 -07001221 tx = iop_adma_prep_dma_xor_val(dma_chan, dma_srcs,
1222 IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
1223 &zero_sum_result,
1224 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -07001225
1226 cookie = iop_adma_tx_submit(tx);
1227 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -07001228 msleep(8);
1229
Linus Walleij07934482010-03-26 16:50:49 -07001230 if (iop_adma_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
Dan Williamsc2110922007-01-02 13:52:26 -07001231 dev_printk(KERN_ERR, dma_chan->device->dev,
1232 "Self-test non-zero sum timed out, disabling\n");
1233 err = -ENODEV;
1234 goto free_resources;
1235 }
1236
1237 if (zero_sum_result != 1) {
1238 dev_printk(KERN_ERR, dma_chan->device->dev,
1239 "Self-test non-zero sum failed compare, disabling\n");
1240 err = -ENODEV;
1241 goto free_resources;
1242 }
1243
1244free_resources:
1245 iop_adma_free_chan_resources(dma_chan);
1246out:
1247 src_idx = IOP_ADMA_NUM_SRC_TEST;
1248 while (src_idx--)
1249 __free_page(xor_srcs[src_idx]);
1250 __free_page(dest);
1251 return err;
1252}
1253
Wei Yongquan0261f742010-12-29 20:30:55 +08001254#ifdef CONFIG_RAID6_PQ
Dan Williamsf6dbf652009-08-29 19:12:40 -07001255static int __devinit
1256iop_adma_pq_zero_sum_self_test(struct iop_adma_device *device)
1257{
1258 /* combined sources, software pq results, and extra hw pq results */
1259 struct page *pq[IOP_ADMA_NUM_SRC_TEST+2+2];
1260 /* ptr to the extra hw pq buffers defined above */
1261 struct page **pq_hw = &pq[IOP_ADMA_NUM_SRC_TEST+2];
1262 /* address conversion buffers (dma_map / page_address) */
1263 void *pq_sw[IOP_ADMA_NUM_SRC_TEST+2];
1264 dma_addr_t pq_src[IOP_ADMA_NUM_SRC_TEST];
1265 dma_addr_t pq_dest[2];
1266
1267 int i;
1268 struct dma_async_tx_descriptor *tx;
1269 struct dma_chan *dma_chan;
1270 dma_cookie_t cookie;
1271 u32 zero_sum_result;
1272 int err = 0;
1273 struct device *dev;
1274
1275 dev_dbg(device->common.dev, "%s\n", __func__);
1276
1277 for (i = 0; i < ARRAY_SIZE(pq); i++) {
1278 pq[i] = alloc_page(GFP_KERNEL);
1279 if (!pq[i]) {
1280 while (i--)
1281 __free_page(pq[i]);
1282 return -ENOMEM;
1283 }
1284 }
1285
1286 /* Fill in src buffers */
1287 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++) {
1288 pq_sw[i] = page_address(pq[i]);
1289 memset(pq_sw[i], 0x11111111 * (1<<i), PAGE_SIZE);
1290 }
1291 pq_sw[i] = page_address(pq[i]);
1292 pq_sw[i+1] = page_address(pq[i+1]);
1293
1294 dma_chan = container_of(device->common.channels.next,
1295 struct dma_chan,
1296 device_node);
1297 if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
1298 err = -ENODEV;
1299 goto out;
1300 }
1301
1302 dev = dma_chan->device->dev;
1303
1304 /* initialize the dests */
1305 memset(page_address(pq_hw[0]), 0 , PAGE_SIZE);
1306 memset(page_address(pq_hw[1]), 0 , PAGE_SIZE);
1307
1308 /* test pq */
1309 pq_dest[0] = dma_map_page(dev, pq_hw[0], 0, PAGE_SIZE, DMA_FROM_DEVICE);
1310 pq_dest[1] = dma_map_page(dev, pq_hw[1], 0, PAGE_SIZE, DMA_FROM_DEVICE);
1311 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
1312 pq_src[i] = dma_map_page(dev, pq[i], 0, PAGE_SIZE,
1313 DMA_TO_DEVICE);
1314
1315 tx = iop_adma_prep_dma_pq(dma_chan, pq_dest, pq_src,
1316 IOP_ADMA_NUM_SRC_TEST, (u8 *)raid6_gfexp,
1317 PAGE_SIZE,
1318 DMA_PREP_INTERRUPT |
1319 DMA_CTRL_ACK);
1320
1321 cookie = iop_adma_tx_submit(tx);
1322 iop_adma_issue_pending(dma_chan);
1323 msleep(8);
1324
Linus Walleij07934482010-03-26 16:50:49 -07001325 if (iop_adma_status(dma_chan, cookie, NULL) !=
Dan Williamsf6dbf652009-08-29 19:12:40 -07001326 DMA_SUCCESS) {
1327 dev_err(dev, "Self-test pq timed out, disabling\n");
1328 err = -ENODEV;
1329 goto free_resources;
1330 }
1331
1332 raid6_call.gen_syndrome(IOP_ADMA_NUM_SRC_TEST+2, PAGE_SIZE, pq_sw);
1333
1334 if (memcmp(pq_sw[IOP_ADMA_NUM_SRC_TEST],
1335 page_address(pq_hw[0]), PAGE_SIZE) != 0) {
1336 dev_err(dev, "Self-test p failed compare, disabling\n");
1337 err = -ENODEV;
1338 goto free_resources;
1339 }
1340 if (memcmp(pq_sw[IOP_ADMA_NUM_SRC_TEST+1],
1341 page_address(pq_hw[1]), PAGE_SIZE) != 0) {
1342 dev_err(dev, "Self-test q failed compare, disabling\n");
1343 err = -ENODEV;
1344 goto free_resources;
1345 }
1346
1347 /* test correct zero sum using the software generated pq values */
1348 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 2; i++)
1349 pq_src[i] = dma_map_page(dev, pq[i], 0, PAGE_SIZE,
1350 DMA_TO_DEVICE);
1351
1352 zero_sum_result = ~0;
1353 tx = iop_adma_prep_dma_pq_val(dma_chan, &pq_src[IOP_ADMA_NUM_SRC_TEST],
1354 pq_src, IOP_ADMA_NUM_SRC_TEST,
1355 raid6_gfexp, PAGE_SIZE, &zero_sum_result,
1356 DMA_PREP_INTERRUPT|DMA_CTRL_ACK);
1357
1358 cookie = iop_adma_tx_submit(tx);
1359 iop_adma_issue_pending(dma_chan);
1360 msleep(8);
1361
Linus Walleij07934482010-03-26 16:50:49 -07001362 if (iop_adma_status(dma_chan, cookie, NULL) !=
Dan Williamsf6dbf652009-08-29 19:12:40 -07001363 DMA_SUCCESS) {
1364 dev_err(dev, "Self-test pq-zero-sum timed out, disabling\n");
1365 err = -ENODEV;
1366 goto free_resources;
1367 }
1368
1369 if (zero_sum_result != 0) {
1370 dev_err(dev, "Self-test pq-zero-sum failed to validate: %x\n",
1371 zero_sum_result);
1372 err = -ENODEV;
1373 goto free_resources;
1374 }
1375
1376 /* test incorrect zero sum */
1377 i = IOP_ADMA_NUM_SRC_TEST;
1378 memset(pq_sw[i] + 100, 0, 100);
1379 memset(pq_sw[i+1] + 200, 0, 200);
1380 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 2; i++)
1381 pq_src[i] = dma_map_page(dev, pq[i], 0, PAGE_SIZE,
1382 DMA_TO_DEVICE);
1383
1384 zero_sum_result = 0;
1385 tx = iop_adma_prep_dma_pq_val(dma_chan, &pq_src[IOP_ADMA_NUM_SRC_TEST],
1386 pq_src, IOP_ADMA_NUM_SRC_TEST,
1387 raid6_gfexp, PAGE_SIZE, &zero_sum_result,
1388 DMA_PREP_INTERRUPT|DMA_CTRL_ACK);
1389
1390 cookie = iop_adma_tx_submit(tx);
1391 iop_adma_issue_pending(dma_chan);
1392 msleep(8);
1393
Linus Walleij07934482010-03-26 16:50:49 -07001394 if (iop_adma_status(dma_chan, cookie, NULL) !=
Dan Williamsf6dbf652009-08-29 19:12:40 -07001395 DMA_SUCCESS) {
1396 dev_err(dev, "Self-test !pq-zero-sum timed out, disabling\n");
1397 err = -ENODEV;
1398 goto free_resources;
1399 }
1400
1401 if (zero_sum_result != (SUM_CHECK_P_RESULT | SUM_CHECK_Q_RESULT)) {
1402 dev_err(dev, "Self-test !pq-zero-sum failed to validate: %x\n",
1403 zero_sum_result);
1404 err = -ENODEV;
1405 goto free_resources;
1406 }
1407
1408free_resources:
1409 iop_adma_free_chan_resources(dma_chan);
1410out:
1411 i = ARRAY_SIZE(pq);
1412 while (i--)
1413 __free_page(pq[i]);
1414 return err;
1415}
1416#endif
1417
Dan Williamsc2110922007-01-02 13:52:26 -07001418static int __devexit iop_adma_remove(struct platform_device *dev)
1419{
1420 struct iop_adma_device *device = platform_get_drvdata(dev);
1421 struct dma_chan *chan, *_chan;
1422 struct iop_adma_chan *iop_chan;
Dan Williamsc2110922007-01-02 13:52:26 -07001423 struct iop_adma_platform_data *plat_data = dev->dev.platform_data;
1424
1425 dma_async_device_unregister(&device->common);
1426
Dan Williamsc2110922007-01-02 13:52:26 -07001427 dma_free_coherent(&dev->dev, plat_data->pool_size,
1428 device->dma_desc_pool_virt, device->dma_desc_pool);
1429
Dan Williamsc2110922007-01-02 13:52:26 -07001430 list_for_each_entry_safe(chan, _chan, &device->common.channels,
1431 device_node) {
1432 iop_chan = to_iop_adma_chan(chan);
1433 list_del(&chan->device_node);
1434 kfree(iop_chan);
1435 }
1436 kfree(device);
1437
1438 return 0;
1439}
1440
1441static int __devinit iop_adma_probe(struct platform_device *pdev)
1442{
1443 struct resource *res;
1444 int ret = 0, i;
1445 struct iop_adma_device *adev;
1446 struct iop_adma_chan *iop_chan;
1447 struct dma_device *dma_dev;
1448 struct iop_adma_platform_data *plat_data = pdev->dev.platform_data;
1449
1450 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1451 if (!res)
1452 return -ENODEV;
1453
1454 if (!devm_request_mem_region(&pdev->dev, res->start,
H Hartley Sweeten2e032b62009-12-11 21:24:33 -07001455 resource_size(res), pdev->name))
Dan Williamsc2110922007-01-02 13:52:26 -07001456 return -EBUSY;
1457
1458 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
1459 if (!adev)
1460 return -ENOMEM;
1461 dma_dev = &adev->common;
1462
1463 /* allocate coherent memory for hardware descriptors
1464 * note: writecombine gives slightly better performance, but
1465 * requires that we explicitly flush the writes
1466 */
1467 if ((adev->dma_desc_pool_virt = dma_alloc_writecombine(&pdev->dev,
1468 plat_data->pool_size,
1469 &adev->dma_desc_pool,
1470 GFP_KERNEL)) == NULL) {
1471 ret = -ENOMEM;
1472 goto err_free_adev;
1473 }
1474
1475 dev_dbg(&pdev->dev, "%s: allocted descriptor pool virt %p phys %p\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -07001476 __func__, adev->dma_desc_pool_virt,
Dan Williamsc2110922007-01-02 13:52:26 -07001477 (void *) adev->dma_desc_pool);
1478
1479 adev->id = plat_data->hw_id;
1480
1481 /* discover transaction capabilites from the platform data */
1482 dma_dev->cap_mask = plat_data->cap_mask;
1483
1484 adev->pdev = pdev;
1485 platform_set_drvdata(pdev, adev);
1486
1487 INIT_LIST_HEAD(&dma_dev->channels);
1488
1489 /* set base routines */
1490 dma_dev->device_alloc_chan_resources = iop_adma_alloc_chan_resources;
1491 dma_dev->device_free_chan_resources = iop_adma_free_chan_resources;
Linus Walleij07934482010-03-26 16:50:49 -07001492 dma_dev->device_tx_status = iop_adma_status;
Dan Williamsc2110922007-01-02 13:52:26 -07001493 dma_dev->device_issue_pending = iop_adma_issue_pending;
Dan Williamsc2110922007-01-02 13:52:26 -07001494 dma_dev->dev = &pdev->dev;
1495
1496 /* set prep routines based on capability */
1497 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
1498 dma_dev->device_prep_dma_memcpy = iop_adma_prep_dma_memcpy;
1499 if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
1500 dma_dev->device_prep_dma_memset = iop_adma_prep_dma_memset;
1501 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1502 dma_dev->max_xor = iop_adma_get_max_xor();
1503 dma_dev->device_prep_dma_xor = iop_adma_prep_dma_xor;
1504 }
Dan Williams099f53c2009-04-08 14:28:37 -07001505 if (dma_has_cap(DMA_XOR_VAL, dma_dev->cap_mask))
1506 dma_dev->device_prep_dma_xor_val =
1507 iop_adma_prep_dma_xor_val;
Dan Williams7bf649a2009-08-28 14:32:04 -07001508 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask)) {
1509 dma_set_maxpq(dma_dev, iop_adma_get_max_pq(), 0);
1510 dma_dev->device_prep_dma_pq = iop_adma_prep_dma_pq;
1511 }
1512 if (dma_has_cap(DMA_PQ_VAL, dma_dev->cap_mask))
1513 dma_dev->device_prep_dma_pq_val =
1514 iop_adma_prep_dma_pq_val;
Dan Williamsc2110922007-01-02 13:52:26 -07001515 if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
1516 dma_dev->device_prep_dma_interrupt =
1517 iop_adma_prep_dma_interrupt;
1518
1519 iop_chan = kzalloc(sizeof(*iop_chan), GFP_KERNEL);
1520 if (!iop_chan) {
1521 ret = -ENOMEM;
1522 goto err_free_dma;
1523 }
1524 iop_chan->device = adev;
1525
1526 iop_chan->mmr_base = devm_ioremap(&pdev->dev, res->start,
H Hartley Sweeten2e032b62009-12-11 21:24:33 -07001527 resource_size(res));
Dan Williamsc2110922007-01-02 13:52:26 -07001528 if (!iop_chan->mmr_base) {
1529 ret = -ENOMEM;
1530 goto err_free_iop_chan;
1531 }
1532 tasklet_init(&iop_chan->irq_tasklet, iop_adma_tasklet, (unsigned long)
1533 iop_chan);
1534
1535 /* clear errors before enabling interrupts */
1536 iop_adma_device_clear_err_status(iop_chan);
1537
1538 for (i = 0; i < 3; i++) {
1539 irq_handler_t handler[] = { iop_adma_eot_handler,
1540 iop_adma_eoc_handler,
1541 iop_adma_err_handler };
1542 int irq = platform_get_irq(pdev, i);
1543 if (irq < 0) {
1544 ret = -ENXIO;
1545 goto err_free_iop_chan;
1546 } else {
1547 ret = devm_request_irq(&pdev->dev, irq,
1548 handler[i], 0, pdev->name, iop_chan);
1549 if (ret)
1550 goto err_free_iop_chan;
1551 }
1552 }
1553
1554 spin_lock_init(&iop_chan->lock);
Dan Williamsc2110922007-01-02 13:52:26 -07001555 INIT_LIST_HEAD(&iop_chan->chain);
1556 INIT_LIST_HEAD(&iop_chan->all_slots);
Dan Williamsc2110922007-01-02 13:52:26 -07001557 iop_chan->common.device = dma_dev;
1558 list_add_tail(&iop_chan->common.device_node, &dma_dev->channels);
1559
1560 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
1561 ret = iop_adma_memcpy_self_test(adev);
1562 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1563 if (ret)
1564 goto err_free_iop_chan;
1565 }
1566
1567 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask) ||
Dan Williamsf6dbf652009-08-29 19:12:40 -07001568 dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
Dan Williams099f53c2009-04-08 14:28:37 -07001569 ret = iop_adma_xor_val_self_test(adev);
Dan Williamsc2110922007-01-02 13:52:26 -07001570 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1571 if (ret)
1572 goto err_free_iop_chan;
1573 }
1574
Dan Williamsf6dbf652009-08-29 19:12:40 -07001575 if (dma_has_cap(DMA_PQ, dma_dev->cap_mask) &&
1576 dma_has_cap(DMA_PQ_VAL, dma_dev->cap_mask)) {
Wei Yongquan0261f742010-12-29 20:30:55 +08001577 #ifdef CONFIG_RAID6_PQ
Dan Williamsf6dbf652009-08-29 19:12:40 -07001578 ret = iop_adma_pq_zero_sum_self_test(adev);
1579 dev_dbg(&pdev->dev, "pq self test returned %d\n", ret);
1580 #else
1581 /* can not test raid6, so do not publish capability */
1582 dma_cap_clear(DMA_PQ, dma_dev->cap_mask);
1583 dma_cap_clear(DMA_PQ_VAL, dma_dev->cap_mask);
1584 ret = 0;
1585 #endif
1586 if (ret)
1587 goto err_free_iop_chan;
1588 }
1589
Dan Williamsc2110922007-01-02 13:52:26 -07001590 dev_printk(KERN_INFO, &pdev->dev, "Intel(R) IOP: "
Dan Williams9308add2009-09-08 17:42:52 -07001591 "( %s%s%s%s%s%s%s)\n",
Dan Williamsb2f46fd2009-07-14 12:20:36 -07001592 dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "pq " : "",
Dan Williams099f53c2009-04-08 14:28:37 -07001593 dma_has_cap(DMA_PQ_VAL, dma_dev->cap_mask) ? "pq_val " : "",
Dan Williamsc2110922007-01-02 13:52:26 -07001594 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
Dan Williams099f53c2009-04-08 14:28:37 -07001595 dma_has_cap(DMA_XOR_VAL, dma_dev->cap_mask) ? "xor_val " : "",
Dan Williamsc2110922007-01-02 13:52:26 -07001596 dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
Dan Williamsc2110922007-01-02 13:52:26 -07001597 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1598 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
1599
1600 dma_async_device_register(dma_dev);
1601 goto out;
1602
1603 err_free_iop_chan:
1604 kfree(iop_chan);
1605 err_free_dma:
1606 dma_free_coherent(&adev->pdev->dev, plat_data->pool_size,
1607 adev->dma_desc_pool_virt, adev->dma_desc_pool);
1608 err_free_adev:
1609 kfree(adev);
1610 out:
1611 return ret;
1612}
1613
1614static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan)
1615{
1616 struct iop_adma_desc_slot *sw_desc, *grp_start;
1617 dma_cookie_t cookie;
1618 int slot_cnt, slots_per_op;
1619
Harvey Harrison3d9b5252008-03-13 17:45:28 -07001620 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -07001621
1622 spin_lock_bh(&iop_chan->lock);
1623 slot_cnt = iop_chan_memcpy_slot_count(0, &slots_per_op);
1624 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
1625 if (sw_desc) {
1626 grp_start = sw_desc->group_head;
1627
Dan Williams308136d2009-09-08 17:53:02 -07001628 list_splice_init(&sw_desc->tx_list, &iop_chan->chain);
Dan Williams636bdea2008-04-17 20:17:26 -07001629 async_tx_ack(&sw_desc->async_tx);
Dan Williamsc2110922007-01-02 13:52:26 -07001630 iop_desc_init_memcpy(grp_start, 0);
1631 iop_desc_set_byte_count(grp_start, iop_chan, 0);
1632 iop_desc_set_dest_addr(grp_start, iop_chan, 0);
1633 iop_desc_set_memcpy_src_addr(grp_start, 0);
1634
1635 cookie = iop_chan->common.cookie;
1636 cookie++;
1637 if (cookie <= 1)
1638 cookie = 2;
1639
1640 /* initialize the completed cookie to be less than
1641 * the most recently used cookie
1642 */
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001643 iop_chan->common.completed_cookie = cookie - 1;
Dan Williamsc2110922007-01-02 13:52:26 -07001644 iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
1645
1646 /* channel should not be busy */
1647 BUG_ON(iop_chan_is_busy(iop_chan));
1648
1649 /* clear any prior error-status bits */
1650 iop_adma_device_clear_err_status(iop_chan);
1651
1652 /* disable operation */
1653 iop_chan_disable(iop_chan);
1654
1655 /* set the descriptor address */
1656 iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
1657
1658 /* 1/ don't add pre-chained descriptors
1659 * 2/ dummy read to flush next_desc write
1660 */
1661 BUG_ON(iop_desc_get_next_desc(sw_desc));
1662
1663 /* run the descriptor */
1664 iop_chan_enable(iop_chan);
1665 } else
1666 dev_printk(KERN_ERR, iop_chan->device->common.dev,
1667 "failed to allocate null descriptor\n");
1668 spin_unlock_bh(&iop_chan->lock);
1669}
1670
1671static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan)
1672{
1673 struct iop_adma_desc_slot *sw_desc, *grp_start;
1674 dma_cookie_t cookie;
1675 int slot_cnt, slots_per_op;
1676
Harvey Harrison3d9b5252008-03-13 17:45:28 -07001677 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -07001678
1679 spin_lock_bh(&iop_chan->lock);
1680 slot_cnt = iop_chan_xor_slot_count(0, 2, &slots_per_op);
1681 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
1682 if (sw_desc) {
1683 grp_start = sw_desc->group_head;
Dan Williams308136d2009-09-08 17:53:02 -07001684 list_splice_init(&sw_desc->tx_list, &iop_chan->chain);
Dan Williams636bdea2008-04-17 20:17:26 -07001685 async_tx_ack(&sw_desc->async_tx);
Dan Williamsc2110922007-01-02 13:52:26 -07001686 iop_desc_init_null_xor(grp_start, 2, 0);
1687 iop_desc_set_byte_count(grp_start, iop_chan, 0);
1688 iop_desc_set_dest_addr(grp_start, iop_chan, 0);
1689 iop_desc_set_xor_src_addr(grp_start, 0, 0);
1690 iop_desc_set_xor_src_addr(grp_start, 1, 0);
1691
1692 cookie = iop_chan->common.cookie;
1693 cookie++;
1694 if (cookie <= 1)
1695 cookie = 2;
1696
1697 /* initialize the completed cookie to be less than
1698 * the most recently used cookie
1699 */
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001700 iop_chan->common.completed_cookie = cookie - 1;
Dan Williamsc2110922007-01-02 13:52:26 -07001701 iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
1702
1703 /* channel should not be busy */
1704 BUG_ON(iop_chan_is_busy(iop_chan));
1705
1706 /* clear any prior error-status bits */
1707 iop_adma_device_clear_err_status(iop_chan);
1708
1709 /* disable operation */
1710 iop_chan_disable(iop_chan);
1711
1712 /* set the descriptor address */
1713 iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
1714
1715 /* 1/ don't add pre-chained descriptors
1716 * 2/ dummy read to flush next_desc write
1717 */
1718 BUG_ON(iop_desc_get_next_desc(sw_desc));
1719
1720 /* run the descriptor */
1721 iop_chan_enable(iop_chan);
1722 } else
1723 dev_printk(KERN_ERR, iop_chan->device->common.dev,
1724 "failed to allocate null descriptor\n");
1725 spin_unlock_bh(&iop_chan->lock);
1726}
1727
1728static struct platform_driver iop_adma_driver = {
1729 .probe = iop_adma_probe,
Russell Kingbdf602b2009-03-03 13:43:47 +00001730 .remove = __devexit_p(iop_adma_remove),
Dan Williamsc2110922007-01-02 13:52:26 -07001731 .driver = {
1732 .owner = THIS_MODULE,
1733 .name = "iop-adma",
1734 },
1735};
1736
Axel Linc94e9102011-11-26 15:11:12 +08001737module_platform_driver(iop_adma_driver);
Dan Williamsc2110922007-01-02 13:52:26 -07001738
1739MODULE_AUTHOR("Intel Corporation");
1740MODULE_DESCRIPTION("IOP ADMA Engine Driver");
1741MODULE_LICENSE("GPL");
Axel Linc94e9102011-11-26 15:11:12 +08001742MODULE_ALIAS("platform:iop-adma");