blob: ce45f3fb034335bc46bc31f241c52c186e0da713 [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>
34
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/adma.h>
Dan Williamsc2110922007-01-02 13:52:26 -070036
37#define to_iop_adma_chan(chan) container_of(chan, struct iop_adma_chan, common)
38#define to_iop_adma_device(dev) \
39 container_of(dev, struct iop_adma_device, common)
40#define tx_to_iop_adma_slot(tx) \
41 container_of(tx, struct iop_adma_desc_slot, async_tx)
42
43/**
44 * iop_adma_free_slots - flags descriptor slots for reuse
45 * @slot: Slot to free
46 * Caller must hold &iop_chan->lock while calling this function
47 */
48static void iop_adma_free_slots(struct iop_adma_desc_slot *slot)
49{
50 int stride = slot->slots_per_op;
51
52 while (stride--) {
53 slot->slots_per_op = 0;
54 slot = list_entry(slot->slot_node.next,
55 struct iop_adma_desc_slot,
56 slot_node);
57 }
58}
59
60static dma_cookie_t
61iop_adma_run_tx_complete_actions(struct iop_adma_desc_slot *desc,
62 struct iop_adma_chan *iop_chan, dma_cookie_t cookie)
63{
Dan Williams507fbec2009-08-29 19:12:39 -070064 struct dma_async_tx_descriptor *tx = &desc->async_tx;
65
66 BUG_ON(tx->cookie < 0);
67 if (tx->cookie > 0) {
68 cookie = tx->cookie;
69 tx->cookie = 0;
Dan Williamsc2110922007-01-02 13:52:26 -070070
71 /* call the callback (must not sleep or submit new
72 * operations to this channel)
73 */
Dan Williams507fbec2009-08-29 19:12:39 -070074 if (tx->callback)
75 tx->callback(tx->callback_param);
Dan Williamsc2110922007-01-02 13:52:26 -070076
77 /* unmap dma addresses
78 * (unmap_single vs unmap_page?)
79 */
80 if (desc->group_head && desc->unmap_len) {
81 struct iop_adma_desc_slot *unmap = desc->group_head;
82 struct device *dev =
83 &iop_chan->device->pdev->dev;
84 u32 len = unmap->unmap_len;
Dan Williams507fbec2009-08-29 19:12:39 -070085 enum dma_ctrl_flags flags = tx->flags;
Dan Williamse1d181e2008-07-04 00:13:40 -070086 u32 src_cnt;
87 dma_addr_t addr;
Dan Williamsa06d5682008-12-08 13:46:00 -070088 dma_addr_t dest;
Dan Williamsc2110922007-01-02 13:52:26 -070089
Dan Williamsa06d5682008-12-08 13:46:00 -070090 src_cnt = unmap->unmap_src_cnt;
91 dest = iop_desc_get_dest_addr(unmap, iop_chan);
Dan Williamse1d181e2008-07-04 00:13:40 -070092 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
Dan Williamsa06d5682008-12-08 13:46:00 -070093 enum dma_data_direction dir;
94
95 if (src_cnt > 1) /* is xor? */
96 dir = DMA_BIDIRECTIONAL;
97 else
98 dir = DMA_FROM_DEVICE;
99
100 dma_unmap_page(dev, dest, len, dir);
Dan Williamse1d181e2008-07-04 00:13:40 -0700101 }
102
103 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
Dan Williamse1d181e2008-07-04 00:13:40 -0700104 while (src_cnt--) {
105 addr = iop_desc_get_src_addr(unmap,
106 iop_chan,
107 src_cnt);
Dan Williamsa06d5682008-12-08 13:46:00 -0700108 if (addr == dest)
109 continue;
Dan Williamse1d181e2008-07-04 00:13:40 -0700110 dma_unmap_page(dev, addr, len,
111 DMA_TO_DEVICE);
112 }
Dan Williamsc2110922007-01-02 13:52:26 -0700113 }
114 desc->group_head = NULL;
115 }
116 }
117
118 /* run dependent operations */
Dan Williams507fbec2009-08-29 19:12:39 -0700119 dma_run_dependencies(tx);
Dan Williamsc2110922007-01-02 13:52:26 -0700120
121 return cookie;
122}
123
124static int
125iop_adma_clean_slot(struct iop_adma_desc_slot *desc,
126 struct iop_adma_chan *iop_chan)
127{
128 /* the client is allowed to attach dependent operations
129 * until 'ack' is set
130 */
Dan Williams636bdea2008-04-17 20:17:26 -0700131 if (!async_tx_test_ack(&desc->async_tx))
Dan Williamsc2110922007-01-02 13:52:26 -0700132 return 0;
133
134 /* leave the last descriptor in the chain
135 * so we can append to it
136 */
137 if (desc->chain_node.next == &iop_chan->chain)
138 return 1;
139
140 dev_dbg(iop_chan->device->common.dev,
141 "\tfree slot: %d slots_per_op: %d\n",
142 desc->idx, desc->slots_per_op);
143
144 list_del(&desc->chain_node);
145 iop_adma_free_slots(desc);
146
147 return 0;
148}
149
150static void __iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
151{
152 struct iop_adma_desc_slot *iter, *_iter, *grp_start = NULL;
153 dma_cookie_t cookie = 0;
154 u32 current_desc = iop_chan_get_current_descriptor(iop_chan);
155 int busy = iop_chan_is_busy(iop_chan);
156 int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
157
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700158 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700159 /* free completed slots from the chain starting with
160 * the oldest descriptor
161 */
162 list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
163 chain_node) {
164 pr_debug("\tcookie: %d slot: %d busy: %d "
165 "this_desc: %#x next_desc: %#x ack: %d\n",
166 iter->async_tx.cookie, iter->idx, busy,
167 iter->async_tx.phys, iop_desc_get_next_desc(iter),
Dan Williams636bdea2008-04-17 20:17:26 -0700168 async_tx_test_ack(&iter->async_tx));
Dan Williamsc2110922007-01-02 13:52:26 -0700169 prefetch(_iter);
170 prefetch(&_iter->async_tx);
171
172 /* do not advance past the current descriptor loaded into the
173 * hardware channel, subsequent descriptors are either in
174 * process or have not been submitted
175 */
176 if (seen_current)
177 break;
178
179 /* stop the search if we reach the current descriptor and the
180 * channel is busy, or if it appears that the current descriptor
181 * needs to be re-read (i.e. has been appended to)
182 */
183 if (iter->async_tx.phys == current_desc) {
184 BUG_ON(seen_current++);
185 if (busy || iop_desc_get_next_desc(iter))
186 break;
187 }
188
189 /* detect the start of a group transaction */
190 if (!slot_cnt && !slots_per_op) {
191 slot_cnt = iter->slot_cnt;
192 slots_per_op = iter->slots_per_op;
193 if (slot_cnt <= slots_per_op) {
194 slot_cnt = 0;
195 slots_per_op = 0;
196 }
197 }
198
199 if (slot_cnt) {
200 pr_debug("\tgroup++\n");
201 if (!grp_start)
202 grp_start = iter;
203 slot_cnt -= slots_per_op;
204 }
205
206 /* all the members of a group are complete */
207 if (slots_per_op != 0 && slot_cnt == 0) {
208 struct iop_adma_desc_slot *grp_iter, *_grp_iter;
209 int end_of_chain = 0;
210 pr_debug("\tgroup end\n");
211
212 /* collect the total results */
213 if (grp_start->xor_check_result) {
214 u32 zero_sum_result = 0;
215 slot_cnt = grp_start->slot_cnt;
216 grp_iter = grp_start;
217
218 list_for_each_entry_from(grp_iter,
219 &iop_chan->chain, chain_node) {
220 zero_sum_result |=
221 iop_desc_get_zero_result(grp_iter);
222 pr_debug("\titer%d result: %d\n",
223 grp_iter->idx, zero_sum_result);
224 slot_cnt -= slots_per_op;
225 if (slot_cnt == 0)
226 break;
227 }
228 pr_debug("\tgrp_start->xor_check_result: %p\n",
229 grp_start->xor_check_result);
230 *grp_start->xor_check_result = zero_sum_result;
231 }
232
233 /* clean up the group */
234 slot_cnt = grp_start->slot_cnt;
235 grp_iter = grp_start;
236 list_for_each_entry_safe_from(grp_iter, _grp_iter,
237 &iop_chan->chain, chain_node) {
238 cookie = iop_adma_run_tx_complete_actions(
239 grp_iter, iop_chan, cookie);
240
241 slot_cnt -= slots_per_op;
242 end_of_chain = iop_adma_clean_slot(grp_iter,
243 iop_chan);
244
245 if (slot_cnt == 0 || end_of_chain)
246 break;
247 }
248
249 /* the group should be complete at this point */
250 BUG_ON(slot_cnt);
251
252 slots_per_op = 0;
253 grp_start = NULL;
254 if (end_of_chain)
255 break;
256 else
257 continue;
258 } else if (slots_per_op) /* wait for group completion */
259 continue;
260
261 /* write back zero sum results (single descriptor case) */
262 if (iter->xor_check_result && iter->async_tx.cookie)
263 *iter->xor_check_result =
264 iop_desc_get_zero_result(iter);
265
266 cookie = iop_adma_run_tx_complete_actions(
267 iter, iop_chan, cookie);
268
269 if (iop_adma_clean_slot(iter, iop_chan))
270 break;
271 }
272
Dan Williamsc2110922007-01-02 13:52:26 -0700273 if (cookie > 0) {
274 iop_chan->completed_cookie = cookie;
275 pr_debug("\tcompleted cookie %d\n", cookie);
276 }
277}
278
279static void
280iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
281{
282 spin_lock_bh(&iop_chan->lock);
283 __iop_adma_slot_cleanup(iop_chan);
284 spin_unlock_bh(&iop_chan->lock);
285}
286
287static void iop_adma_tasklet(unsigned long data)
288{
Dan Williams19242d72008-04-17 20:17:25 -0700289 struct iop_adma_chan *iop_chan = (struct iop_adma_chan *) data;
290
291 spin_lock(&iop_chan->lock);
292 __iop_adma_slot_cleanup(iop_chan);
293 spin_unlock(&iop_chan->lock);
Dan Williamsc2110922007-01-02 13:52:26 -0700294}
295
296static struct iop_adma_desc_slot *
297iop_adma_alloc_slots(struct iop_adma_chan *iop_chan, int num_slots,
298 int slots_per_op)
299{
300 struct iop_adma_desc_slot *iter, *_iter, *alloc_start = NULL;
Denis Chenge73ef9a2008-02-02 19:30:01 -0700301 LIST_HEAD(chain);
Dan Williamsc2110922007-01-02 13:52:26 -0700302 int slots_found, retry = 0;
303
304 /* start search from the last allocated descrtiptor
305 * if a contiguous allocation can not be found start searching
306 * from the beginning of the list
307 */
308retry:
309 slots_found = 0;
310 if (retry == 0)
311 iter = iop_chan->last_used;
312 else
313 iter = list_entry(&iop_chan->all_slots,
314 struct iop_adma_desc_slot,
315 slot_node);
316
317 list_for_each_entry_safe_continue(
318 iter, _iter, &iop_chan->all_slots, slot_node) {
319 prefetch(_iter);
320 prefetch(&_iter->async_tx);
321 if (iter->slots_per_op) {
322 /* give up after finding the first busy slot
323 * on the second pass through the list
324 */
325 if (retry)
326 break;
327
328 slots_found = 0;
329 continue;
330 }
331
332 /* start the allocation if the slot is correctly aligned */
333 if (!slots_found++) {
334 if (iop_desc_is_aligned(iter, slots_per_op))
335 alloc_start = iter;
336 else {
337 slots_found = 0;
338 continue;
339 }
340 }
341
342 if (slots_found == num_slots) {
343 struct iop_adma_desc_slot *alloc_tail = NULL;
344 struct iop_adma_desc_slot *last_used = NULL;
345 iter = alloc_start;
346 while (num_slots) {
347 int i;
348 dev_dbg(iop_chan->device->common.dev,
349 "allocated slot: %d "
350 "(desc %p phys: %#x) slots_per_op %d\n",
351 iter->idx, iter->hw_desc,
352 iter->async_tx.phys, slots_per_op);
353
354 /* pre-ack all but the last descriptor */
355 if (num_slots != slots_per_op)
Dan Williams636bdea2008-04-17 20:17:26 -0700356 async_tx_ack(&iter->async_tx);
Dan Williamsc2110922007-01-02 13:52:26 -0700357
358 list_add_tail(&iter->chain_node, &chain);
359 alloc_tail = iter;
360 iter->async_tx.cookie = 0;
361 iter->slot_cnt = num_slots;
362 iter->xor_check_result = NULL;
363 for (i = 0; i < slots_per_op; i++) {
364 iter->slots_per_op = slots_per_op - i;
365 last_used = iter;
366 iter = list_entry(iter->slot_node.next,
367 struct iop_adma_desc_slot,
368 slot_node);
369 }
370 num_slots -= slots_per_op;
371 }
372 alloc_tail->group_head = alloc_start;
373 alloc_tail->async_tx.cookie = -EBUSY;
374 list_splice(&chain, &alloc_tail->async_tx.tx_list);
375 iop_chan->last_used = last_used;
376 iop_desc_clear_next_desc(alloc_start);
377 iop_desc_clear_next_desc(alloc_tail);
378 return alloc_tail;
379 }
380 }
381 if (!retry++)
382 goto retry;
383
Dan Williamsc7141d02008-07-17 17:59:56 -0700384 /* perform direct reclaim if the allocation fails */
385 __iop_adma_slot_cleanup(iop_chan);
Dan Williamsc2110922007-01-02 13:52:26 -0700386
387 return NULL;
388}
389
390static dma_cookie_t
391iop_desc_assign_cookie(struct iop_adma_chan *iop_chan,
392 struct iop_adma_desc_slot *desc)
393{
394 dma_cookie_t cookie = iop_chan->common.cookie;
395 cookie++;
396 if (cookie < 0)
397 cookie = 1;
398 iop_chan->common.cookie = desc->async_tx.cookie = cookie;
399 return cookie;
400}
401
402static void iop_adma_check_threshold(struct iop_adma_chan *iop_chan)
403{
404 dev_dbg(iop_chan->device->common.dev, "pending: %d\n",
405 iop_chan->pending);
406
407 if (iop_chan->pending >= IOP_ADMA_THRESHOLD) {
408 iop_chan->pending = 0;
409 iop_chan_append(iop_chan);
410 }
411}
412
413static dma_cookie_t
414iop_adma_tx_submit(struct dma_async_tx_descriptor *tx)
415{
416 struct iop_adma_desc_slot *sw_desc = tx_to_iop_adma_slot(tx);
417 struct iop_adma_chan *iop_chan = to_iop_adma_chan(tx->chan);
418 struct iop_adma_desc_slot *grp_start, *old_chain_tail;
419 int slot_cnt;
420 int slots_per_op;
421 dma_cookie_t cookie;
Dan Williams137cb552008-11-11 13:12:33 -0700422 dma_addr_t next_dma;
Dan Williamsc2110922007-01-02 13:52:26 -0700423
424 grp_start = sw_desc->group_head;
425 slot_cnt = grp_start->slot_cnt;
426 slots_per_op = grp_start->slots_per_op;
427
428 spin_lock_bh(&iop_chan->lock);
429 cookie = iop_desc_assign_cookie(iop_chan, sw_desc);
430
431 old_chain_tail = list_entry(iop_chan->chain.prev,
432 struct iop_adma_desc_slot, chain_node);
433 list_splice_init(&sw_desc->async_tx.tx_list,
434 &old_chain_tail->chain_node);
435
436 /* fix up the hardware chain */
Dan Williams137cb552008-11-11 13:12:33 -0700437 next_dma = grp_start->async_tx.phys;
438 iop_desc_set_next_desc(old_chain_tail, next_dma);
439 BUG_ON(iop_desc_get_next_desc(old_chain_tail) != next_dma); /* flush */
Dan Williamsc2110922007-01-02 13:52:26 -0700440
Dan Williams137cb552008-11-11 13:12:33 -0700441 /* check for pre-chained descriptors */
Dan Williams65e50382008-11-11 13:12:33 -0700442 iop_paranoia(iop_desc_get_next_desc(sw_desc));
Dan Williamsc2110922007-01-02 13:52:26 -0700443
444 /* increment the pending count by the number of slots
445 * memcpy operations have a 1:1 (slot:operation) relation
446 * other operations are heavier and will pop the threshold
447 * more often.
448 */
449 iop_chan->pending += slot_cnt;
450 iop_adma_check_threshold(iop_chan);
451 spin_unlock_bh(&iop_chan->lock);
452
453 dev_dbg(iop_chan->device->common.dev, "%s cookie: %d slot: %d\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700454 __func__, sw_desc->async_tx.cookie, sw_desc->idx);
Dan Williamsc2110922007-01-02 13:52:26 -0700455
456 return cookie;
457}
458
Dan Williamsc2110922007-01-02 13:52:26 -0700459static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan);
460static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan);
461
Dan Williams5eb907a2008-07-17 17:59:56 -0700462/**
463 * iop_adma_alloc_chan_resources - returns the number of allocated descriptors
464 * @chan - allocate descriptor resources for this channel
465 * @client - current client requesting the channel be ready for requests
466 *
467 * Note: We keep the slots for 1 operation on iop_chan->chain at all times. To
468 * avoid deadlock, via async_xor, num_descs_in_pool must at a minimum be
469 * greater than 2x the number slots needed to satisfy a device->max_xor
470 * request.
471 * */
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700472static int iop_adma_alloc_chan_resources(struct dma_chan *chan)
Dan Williamsc2110922007-01-02 13:52:26 -0700473{
474 char *hw_desc;
475 int idx;
476 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
477 struct iop_adma_desc_slot *slot = NULL;
478 int init = iop_chan->slots_allocated ? 0 : 1;
479 struct iop_adma_platform_data *plat_data =
480 iop_chan->device->pdev->dev.platform_data;
481 int num_descs_in_pool = plat_data->pool_size/IOP_ADMA_SLOT_SIZE;
482
483 /* Allocate descriptor slots */
484 do {
485 idx = iop_chan->slots_allocated;
486 if (idx == num_descs_in_pool)
487 break;
488
489 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
490 if (!slot) {
491 printk(KERN_INFO "IOP ADMA Channel only initialized"
492 " %d descriptor slots", idx);
493 break;
494 }
495 hw_desc = (char *) iop_chan->device->dma_desc_pool_virt;
496 slot->hw_desc = (void *) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
497
498 dma_async_tx_descriptor_init(&slot->async_tx, chan);
499 slot->async_tx.tx_submit = iop_adma_tx_submit;
Dan Williamsc2110922007-01-02 13:52:26 -0700500 INIT_LIST_HEAD(&slot->chain_node);
501 INIT_LIST_HEAD(&slot->slot_node);
Dan Williamsc2110922007-01-02 13:52:26 -0700502 hw_desc = (char *) iop_chan->device->dma_desc_pool;
503 slot->async_tx.phys =
504 (dma_addr_t) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
505 slot->idx = idx;
506
507 spin_lock_bh(&iop_chan->lock);
508 iop_chan->slots_allocated++;
509 list_add_tail(&slot->slot_node, &iop_chan->all_slots);
510 spin_unlock_bh(&iop_chan->lock);
511 } while (iop_chan->slots_allocated < num_descs_in_pool);
512
513 if (idx && !iop_chan->last_used)
514 iop_chan->last_used = list_entry(iop_chan->all_slots.next,
515 struct iop_adma_desc_slot,
516 slot_node);
517
518 dev_dbg(iop_chan->device->common.dev,
519 "allocated %d descriptor slots last_used: %p\n",
520 iop_chan->slots_allocated, iop_chan->last_used);
521
522 /* initialize the channel and the chain with a null operation */
523 if (init) {
524 if (dma_has_cap(DMA_MEMCPY,
525 iop_chan->device->common.cap_mask))
526 iop_chan_start_null_memcpy(iop_chan);
527 else if (dma_has_cap(DMA_XOR,
528 iop_chan->device->common.cap_mask))
529 iop_chan_start_null_xor(iop_chan);
530 else
531 BUG();
532 }
533
534 return (idx > 0) ? idx : -ENOMEM;
535}
536
537static struct dma_async_tx_descriptor *
Dan Williams636bdea2008-04-17 20:17:26 -0700538iop_adma_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700539{
540 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
541 struct iop_adma_desc_slot *sw_desc, *grp_start;
542 int slot_cnt, slots_per_op;
543
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700544 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700545
546 spin_lock_bh(&iop_chan->lock);
547 slot_cnt = iop_chan_interrupt_slot_count(&slots_per_op, iop_chan);
548 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
549 if (sw_desc) {
550 grp_start = sw_desc->group_head;
551 iop_desc_init_interrupt(grp_start, iop_chan);
552 grp_start->unmap_len = 0;
Dan Williams636bdea2008-04-17 20:17:26 -0700553 sw_desc->async_tx.flags = flags;
Dan Williamsc2110922007-01-02 13:52:26 -0700554 }
555 spin_unlock_bh(&iop_chan->lock);
556
557 return sw_desc ? &sw_desc->async_tx : NULL;
558}
559
Dan Williamsc2110922007-01-02 13:52:26 -0700560static struct dma_async_tx_descriptor *
Dan Williams00367312008-02-02 19:49:57 -0700561iop_adma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700562 dma_addr_t dma_src, size_t len, unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700563{
564 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
565 struct iop_adma_desc_slot *sw_desc, *grp_start;
566 int slot_cnt, slots_per_op;
567
568 if (unlikely(!len))
569 return NULL;
570 BUG_ON(unlikely(len > IOP_ADMA_MAX_BYTE_COUNT));
571
572 dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700573 __func__, len);
Dan Williamsc2110922007-01-02 13:52:26 -0700574
575 spin_lock_bh(&iop_chan->lock);
576 slot_cnt = iop_chan_memcpy_slot_count(len, &slots_per_op);
577 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
578 if (sw_desc) {
579 grp_start = sw_desc->group_head;
Dan Williamsd4c56f92008-02-02 19:49:58 -0700580 iop_desc_init_memcpy(grp_start, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700581 iop_desc_set_byte_count(grp_start, iop_chan, len);
Dan Williams00367312008-02-02 19:49:57 -0700582 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
583 iop_desc_set_memcpy_src_addr(grp_start, dma_src);
Dan Williamsc2110922007-01-02 13:52:26 -0700584 sw_desc->unmap_src_cnt = 1;
585 sw_desc->unmap_len = len;
Dan Williams636bdea2008-04-17 20:17:26 -0700586 sw_desc->async_tx.flags = flags;
Dan Williamsc2110922007-01-02 13:52:26 -0700587 }
588 spin_unlock_bh(&iop_chan->lock);
589
590 return sw_desc ? &sw_desc->async_tx : NULL;
591}
592
593static struct dma_async_tx_descriptor *
Dan Williams00367312008-02-02 19:49:57 -0700594iop_adma_prep_dma_memset(struct dma_chan *chan, dma_addr_t dma_dest,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700595 int value, size_t len, unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700596{
597 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
598 struct iop_adma_desc_slot *sw_desc, *grp_start;
599 int slot_cnt, slots_per_op;
600
601 if (unlikely(!len))
602 return NULL;
603 BUG_ON(unlikely(len > IOP_ADMA_MAX_BYTE_COUNT));
604
605 dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700606 __func__, len);
Dan Williamsc2110922007-01-02 13:52:26 -0700607
608 spin_lock_bh(&iop_chan->lock);
609 slot_cnt = iop_chan_memset_slot_count(len, &slots_per_op);
610 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
611 if (sw_desc) {
612 grp_start = sw_desc->group_head;
Dan Williamsd4c56f92008-02-02 19:49:58 -0700613 iop_desc_init_memset(grp_start, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700614 iop_desc_set_byte_count(grp_start, iop_chan, len);
615 iop_desc_set_block_fill_val(grp_start, value);
Dan Williams00367312008-02-02 19:49:57 -0700616 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
Dan Williamsc2110922007-01-02 13:52:26 -0700617 sw_desc->unmap_src_cnt = 1;
618 sw_desc->unmap_len = len;
Dan Williams636bdea2008-04-17 20:17:26 -0700619 sw_desc->async_tx.flags = flags;
Dan Williamsc2110922007-01-02 13:52:26 -0700620 }
621 spin_unlock_bh(&iop_chan->lock);
622
623 return sw_desc ? &sw_desc->async_tx : NULL;
624}
625
Dan Williamsc2110922007-01-02 13:52:26 -0700626static struct dma_async_tx_descriptor *
Dan Williams00367312008-02-02 19:49:57 -0700627iop_adma_prep_dma_xor(struct dma_chan *chan, dma_addr_t dma_dest,
628 dma_addr_t *dma_src, unsigned int src_cnt, size_t len,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700629 unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700630{
631 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
632 struct iop_adma_desc_slot *sw_desc, *grp_start;
633 int slot_cnt, slots_per_op;
634
635 if (unlikely(!len))
636 return NULL;
637 BUG_ON(unlikely(len > IOP_ADMA_XOR_MAX_BYTE_COUNT));
638
639 dev_dbg(iop_chan->device->common.dev,
Dan Williamsd4c56f92008-02-02 19:49:58 -0700640 "%s src_cnt: %d len: %u flags: %lx\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700641 __func__, src_cnt, len, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700642
643 spin_lock_bh(&iop_chan->lock);
644 slot_cnt = iop_chan_xor_slot_count(len, src_cnt, &slots_per_op);
645 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
646 if (sw_desc) {
647 grp_start = sw_desc->group_head;
Dan Williamsd4c56f92008-02-02 19:49:58 -0700648 iop_desc_init_xor(grp_start, src_cnt, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700649 iop_desc_set_byte_count(grp_start, iop_chan, len);
Dan Williams00367312008-02-02 19:49:57 -0700650 iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
Dan Williamsc2110922007-01-02 13:52:26 -0700651 sw_desc->unmap_src_cnt = src_cnt;
652 sw_desc->unmap_len = len;
Dan Williams636bdea2008-04-17 20:17:26 -0700653 sw_desc->async_tx.flags = flags;
Dan Williams00367312008-02-02 19:49:57 -0700654 while (src_cnt--)
655 iop_desc_set_xor_src_addr(grp_start, src_cnt,
656 dma_src[src_cnt]);
Dan Williamsc2110922007-01-02 13:52:26 -0700657 }
658 spin_unlock_bh(&iop_chan->lock);
659
660 return sw_desc ? &sw_desc->async_tx : NULL;
661}
662
Dan Williamsc2110922007-01-02 13:52:26 -0700663static struct dma_async_tx_descriptor *
Dan Williams099f53c2009-04-08 14:28:37 -0700664iop_adma_prep_dma_xor_val(struct dma_chan *chan, dma_addr_t *dma_src,
665 unsigned int src_cnt, size_t len, u32 *result,
666 unsigned long flags)
Dan Williamsc2110922007-01-02 13:52:26 -0700667{
668 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
669 struct iop_adma_desc_slot *sw_desc, *grp_start;
670 int slot_cnt, slots_per_op;
671
672 if (unlikely(!len))
673 return NULL;
674
675 dev_dbg(iop_chan->device->common.dev, "%s src_cnt: %d len: %u\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700676 __func__, src_cnt, len);
Dan Williamsc2110922007-01-02 13:52:26 -0700677
678 spin_lock_bh(&iop_chan->lock);
679 slot_cnt = iop_chan_zero_sum_slot_count(len, src_cnt, &slots_per_op);
680 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
681 if (sw_desc) {
682 grp_start = sw_desc->group_head;
Dan Williamsd4c56f92008-02-02 19:49:58 -0700683 iop_desc_init_zero_sum(grp_start, src_cnt, flags);
Dan Williamsc2110922007-01-02 13:52:26 -0700684 iop_desc_set_zero_sum_byte_count(grp_start, len);
685 grp_start->xor_check_result = result;
686 pr_debug("\t%s: grp_start->xor_check_result: %p\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700687 __func__, grp_start->xor_check_result);
Dan Williamsc2110922007-01-02 13:52:26 -0700688 sw_desc->unmap_src_cnt = src_cnt;
689 sw_desc->unmap_len = len;
Dan Williams636bdea2008-04-17 20:17:26 -0700690 sw_desc->async_tx.flags = flags;
Dan Williams00367312008-02-02 19:49:57 -0700691 while (src_cnt--)
692 iop_desc_set_zero_sum_src_addr(grp_start, src_cnt,
693 dma_src[src_cnt]);
Dan Williamsc2110922007-01-02 13:52:26 -0700694 }
695 spin_unlock_bh(&iop_chan->lock);
696
697 return sw_desc ? &sw_desc->async_tx : NULL;
698}
699
Dan Williamsc2110922007-01-02 13:52:26 -0700700static void iop_adma_free_chan_resources(struct dma_chan *chan)
701{
702 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
703 struct iop_adma_desc_slot *iter, *_iter;
704 int in_use_descs = 0;
705
706 iop_adma_slot_cleanup(iop_chan);
707
708 spin_lock_bh(&iop_chan->lock);
709 list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
710 chain_node) {
711 in_use_descs++;
712 list_del(&iter->chain_node);
713 }
714 list_for_each_entry_safe_reverse(
715 iter, _iter, &iop_chan->all_slots, slot_node) {
716 list_del(&iter->slot_node);
717 kfree(iter);
718 iop_chan->slots_allocated--;
719 }
720 iop_chan->last_used = NULL;
721
722 dev_dbg(iop_chan->device->common.dev, "%s slots_allocated %d\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700723 __func__, iop_chan->slots_allocated);
Dan Williamsc2110922007-01-02 13:52:26 -0700724 spin_unlock_bh(&iop_chan->lock);
725
726 /* one is ok since we left it on there on purpose */
727 if (in_use_descs > 1)
728 printk(KERN_ERR "IOP: Freeing %d in use descriptors!\n",
729 in_use_descs - 1);
730}
731
732/**
733 * iop_adma_is_complete - poll the status of an ADMA transaction
734 * @chan: ADMA channel handle
735 * @cookie: ADMA transaction identifier
736 */
737static enum dma_status iop_adma_is_complete(struct dma_chan *chan,
738 dma_cookie_t cookie,
739 dma_cookie_t *done,
740 dma_cookie_t *used)
741{
742 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
743 dma_cookie_t last_used;
744 dma_cookie_t last_complete;
745 enum dma_status ret;
746
747 last_used = chan->cookie;
748 last_complete = iop_chan->completed_cookie;
749
750 if (done)
751 *done = last_complete;
752 if (used)
753 *used = last_used;
754
755 ret = dma_async_is_complete(cookie, last_complete, last_used);
756 if (ret == DMA_SUCCESS)
757 return ret;
758
759 iop_adma_slot_cleanup(iop_chan);
760
761 last_used = chan->cookie;
762 last_complete = iop_chan->completed_cookie;
763
764 if (done)
765 *done = last_complete;
766 if (used)
767 *used = last_used;
768
769 return dma_async_is_complete(cookie, last_complete, last_used);
770}
771
772static irqreturn_t iop_adma_eot_handler(int irq, void *data)
773{
774 struct iop_adma_chan *chan = data;
775
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700776 dev_dbg(chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700777
778 tasklet_schedule(&chan->irq_tasklet);
779
780 iop_adma_device_clear_eot_status(chan);
781
782 return IRQ_HANDLED;
783}
784
785static irqreturn_t iop_adma_eoc_handler(int irq, void *data)
786{
787 struct iop_adma_chan *chan = data;
788
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700789 dev_dbg(chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700790
791 tasklet_schedule(&chan->irq_tasklet);
792
793 iop_adma_device_clear_eoc_status(chan);
794
795 return IRQ_HANDLED;
796}
797
798static irqreturn_t iop_adma_err_handler(int irq, void *data)
799{
800 struct iop_adma_chan *chan = data;
801 unsigned long status = iop_chan_get_status(chan);
802
803 dev_printk(KERN_ERR, chan->device->common.dev,
804 "error ( %s%s%s%s%s%s%s)\n",
805 iop_is_err_int_parity(status, chan) ? "int_parity " : "",
806 iop_is_err_mcu_abort(status, chan) ? "mcu_abort " : "",
807 iop_is_err_int_tabort(status, chan) ? "int_tabort " : "",
808 iop_is_err_int_mabort(status, chan) ? "int_mabort " : "",
809 iop_is_err_pci_tabort(status, chan) ? "pci_tabort " : "",
810 iop_is_err_pci_mabort(status, chan) ? "pci_mabort " : "",
811 iop_is_err_split_tx(status, chan) ? "split_tx " : "");
812
813 iop_adma_device_clear_err_status(chan);
814
815 BUG();
816
817 return IRQ_HANDLED;
818}
819
820static void iop_adma_issue_pending(struct dma_chan *chan)
821{
822 struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
823
824 if (iop_chan->pending) {
825 iop_chan->pending = 0;
826 iop_chan_append(iop_chan);
827 }
828}
829
830/*
831 * Perform a transaction to verify the HW works.
832 */
833#define IOP_ADMA_TEST_SIZE 2000
834
835static int __devinit iop_adma_memcpy_self_test(struct iop_adma_device *device)
836{
837 int i;
838 void *src, *dest;
839 dma_addr_t src_dma, dest_dma;
840 struct dma_chan *dma_chan;
841 dma_cookie_t cookie;
842 struct dma_async_tx_descriptor *tx;
843 int err = 0;
844 struct iop_adma_chan *iop_chan;
845
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700846 dev_dbg(device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700847
Christophe Jailleteccf2142008-05-20 16:33:06 -0700848 src = kmalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
Dan Williamsc2110922007-01-02 13:52:26 -0700849 if (!src)
850 return -ENOMEM;
Christophe Jailleteccf2142008-05-20 16:33:06 -0700851 dest = kzalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
Dan Williamsc2110922007-01-02 13:52:26 -0700852 if (!dest) {
853 kfree(src);
854 return -ENOMEM;
855 }
856
857 /* Fill in src buffer */
858 for (i = 0; i < IOP_ADMA_TEST_SIZE; i++)
859 ((u8 *) src)[i] = (u8)i;
860
Dan Williamsc2110922007-01-02 13:52:26 -0700861 /* Start copy, using first DMA channel */
862 dma_chan = container_of(device->common.channels.next,
863 struct dma_chan,
864 device_node);
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700865 if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
Dan Williamsc2110922007-01-02 13:52:26 -0700866 err = -ENODEV;
867 goto out;
868 }
869
Dan Williamsc2110922007-01-02 13:52:26 -0700870 dest_dma = dma_map_single(dma_chan->device->dev, dest,
871 IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
Dan Williamsc2110922007-01-02 13:52:26 -0700872 src_dma = dma_map_single(dma_chan->device->dev, src,
873 IOP_ADMA_TEST_SIZE, DMA_TO_DEVICE);
Dan Williams00367312008-02-02 19:49:57 -0700874 tx = iop_adma_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
Dan Williams636bdea2008-04-17 20:17:26 -0700875 IOP_ADMA_TEST_SIZE,
876 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -0700877
878 cookie = iop_adma_tx_submit(tx);
879 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -0700880 msleep(1);
881
882 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
883 DMA_SUCCESS) {
884 dev_printk(KERN_ERR, dma_chan->device->dev,
885 "Self-test copy timed out, disabling\n");
886 err = -ENODEV;
887 goto free_resources;
888 }
889
890 iop_chan = to_iop_adma_chan(dma_chan);
891 dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
892 IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
893 if (memcmp(src, dest, IOP_ADMA_TEST_SIZE)) {
894 dev_printk(KERN_ERR, dma_chan->device->dev,
895 "Self-test copy failed compare, disabling\n");
896 err = -ENODEV;
897 goto free_resources;
898 }
899
900free_resources:
901 iop_adma_free_chan_resources(dma_chan);
902out:
903 kfree(src);
904 kfree(dest);
905 return err;
906}
907
908#define IOP_ADMA_NUM_SRC_TEST 4 /* must be <= 15 */
909static int __devinit
Dan Williams099f53c2009-04-08 14:28:37 -0700910iop_adma_xor_val_self_test(struct iop_adma_device *device)
Dan Williamsc2110922007-01-02 13:52:26 -0700911{
912 int i, src_idx;
913 struct page *dest;
914 struct page *xor_srcs[IOP_ADMA_NUM_SRC_TEST];
915 struct page *zero_sum_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
Dan Williams00367312008-02-02 19:49:57 -0700916 dma_addr_t dma_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
Dan Williamsc2110922007-01-02 13:52:26 -0700917 dma_addr_t dma_addr, dest_dma;
918 struct dma_async_tx_descriptor *tx;
919 struct dma_chan *dma_chan;
920 dma_cookie_t cookie;
921 u8 cmp_byte = 0;
922 u32 cmp_word;
923 u32 zero_sum_result;
924 int err = 0;
925 struct iop_adma_chan *iop_chan;
926
Harvey Harrison3d9b5252008-03-13 17:45:28 -0700927 dev_dbg(device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -0700928
929 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
930 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100931 if (!xor_srcs[src_idx]) {
932 while (src_idx--)
Dan Williamsc2110922007-01-02 13:52:26 -0700933 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100934 return -ENOMEM;
935 }
Dan Williamsc2110922007-01-02 13:52:26 -0700936 }
937
938 dest = alloc_page(GFP_KERNEL);
Roel Kluina09b09a2009-02-25 13:56:21 +0100939 if (!dest) {
940 while (src_idx--)
Dan Williamsc2110922007-01-02 13:52:26 -0700941 __free_page(xor_srcs[src_idx]);
Roel Kluina09b09a2009-02-25 13:56:21 +0100942 return -ENOMEM;
943 }
Dan Williamsc2110922007-01-02 13:52:26 -0700944
945 /* Fill in src buffers */
946 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
947 u8 *ptr = page_address(xor_srcs[src_idx]);
948 for (i = 0; i < PAGE_SIZE; i++)
949 ptr[i] = (1 << src_idx);
950 }
951
952 for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++)
953 cmp_byte ^= (u8) (1 << src_idx);
954
955 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
956 (cmp_byte << 8) | cmp_byte;
957
958 memset(page_address(dest), 0, PAGE_SIZE);
959
960 dma_chan = container_of(device->common.channels.next,
961 struct dma_chan,
962 device_node);
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700963 if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
Dan Williamsc2110922007-01-02 13:52:26 -0700964 err = -ENODEV;
965 goto out;
966 }
967
968 /* test xor */
Dan Williamsc2110922007-01-02 13:52:26 -0700969 dest_dma = dma_map_page(dma_chan->device->dev, dest, 0,
970 PAGE_SIZE, DMA_FROM_DEVICE);
Dan Williams00367312008-02-02 19:49:57 -0700971 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
972 dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
973 0, PAGE_SIZE, DMA_TO_DEVICE);
974 tx = iop_adma_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
Dan Williams636bdea2008-04-17 20:17:26 -0700975 IOP_ADMA_NUM_SRC_TEST, PAGE_SIZE,
976 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -0700977
978 cookie = iop_adma_tx_submit(tx);
979 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -0700980 msleep(8);
981
982 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
983 DMA_SUCCESS) {
984 dev_printk(KERN_ERR, dma_chan->device->dev,
985 "Self-test xor timed out, disabling\n");
986 err = -ENODEV;
987 goto free_resources;
988 }
989
990 iop_chan = to_iop_adma_chan(dma_chan);
991 dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
992 PAGE_SIZE, DMA_FROM_DEVICE);
993 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
994 u32 *ptr = page_address(dest);
995 if (ptr[i] != cmp_word) {
996 dev_printk(KERN_ERR, dma_chan->device->dev,
997 "Self-test xor failed compare, disabling\n");
998 err = -ENODEV;
999 goto free_resources;
1000 }
1001 }
1002 dma_sync_single_for_device(&iop_chan->device->pdev->dev, dest_dma,
1003 PAGE_SIZE, DMA_TO_DEVICE);
1004
1005 /* skip zero sum if the capability is not present */
Dan Williams099f53c2009-04-08 14:28:37 -07001006 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
Dan Williamsc2110922007-01-02 13:52:26 -07001007 goto free_resources;
1008
1009 /* zero sum the sources with the destintation page */
1010 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
1011 zero_sum_srcs[i] = xor_srcs[i];
1012 zero_sum_srcs[i] = dest;
1013
1014 zero_sum_result = 1;
1015
Dan Williams00367312008-02-02 19:49:57 -07001016 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
1017 dma_srcs[i] = dma_map_page(dma_chan->device->dev,
1018 zero_sum_srcs[i], 0, PAGE_SIZE,
1019 DMA_TO_DEVICE);
Dan Williams099f53c2009-04-08 14:28:37 -07001020 tx = iop_adma_prep_dma_xor_val(dma_chan, dma_srcs,
1021 IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
1022 &zero_sum_result,
1023 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -07001024
1025 cookie = iop_adma_tx_submit(tx);
1026 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -07001027 msleep(8);
1028
1029 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1030 dev_printk(KERN_ERR, dma_chan->device->dev,
1031 "Self-test zero sum timed out, disabling\n");
1032 err = -ENODEV;
1033 goto free_resources;
1034 }
1035
1036 if (zero_sum_result != 0) {
1037 dev_printk(KERN_ERR, dma_chan->device->dev,
1038 "Self-test zero sum failed compare, disabling\n");
1039 err = -ENODEV;
1040 goto free_resources;
1041 }
1042
1043 /* test memset */
Dan Williamsc2110922007-01-02 13:52:26 -07001044 dma_addr = dma_map_page(dma_chan->device->dev, dest, 0,
1045 PAGE_SIZE, DMA_FROM_DEVICE);
Dan Williams636bdea2008-04-17 20:17:26 -07001046 tx = iop_adma_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
1047 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -07001048
1049 cookie = iop_adma_tx_submit(tx);
1050 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -07001051 msleep(8);
1052
1053 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1054 dev_printk(KERN_ERR, dma_chan->device->dev,
1055 "Self-test memset timed out, disabling\n");
1056 err = -ENODEV;
1057 goto free_resources;
1058 }
1059
1060 for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
1061 u32 *ptr = page_address(dest);
1062 if (ptr[i]) {
1063 dev_printk(KERN_ERR, dma_chan->device->dev,
1064 "Self-test memset failed compare, disabling\n");
1065 err = -ENODEV;
1066 goto free_resources;
1067 }
1068 }
1069
1070 /* test for non-zero parity sum */
1071 zero_sum_result = 0;
Dan Williams00367312008-02-02 19:49:57 -07001072 for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
1073 dma_srcs[i] = dma_map_page(dma_chan->device->dev,
1074 zero_sum_srcs[i], 0, PAGE_SIZE,
1075 DMA_TO_DEVICE);
Dan Williams099f53c2009-04-08 14:28:37 -07001076 tx = iop_adma_prep_dma_xor_val(dma_chan, dma_srcs,
1077 IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
1078 &zero_sum_result,
1079 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Dan Williamsc2110922007-01-02 13:52:26 -07001080
1081 cookie = iop_adma_tx_submit(tx);
1082 iop_adma_issue_pending(dma_chan);
Dan Williamsc2110922007-01-02 13:52:26 -07001083 msleep(8);
1084
1085 if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
1086 dev_printk(KERN_ERR, dma_chan->device->dev,
1087 "Self-test non-zero sum timed out, disabling\n");
1088 err = -ENODEV;
1089 goto free_resources;
1090 }
1091
1092 if (zero_sum_result != 1) {
1093 dev_printk(KERN_ERR, dma_chan->device->dev,
1094 "Self-test non-zero sum failed compare, disabling\n");
1095 err = -ENODEV;
1096 goto free_resources;
1097 }
1098
1099free_resources:
1100 iop_adma_free_chan_resources(dma_chan);
1101out:
1102 src_idx = IOP_ADMA_NUM_SRC_TEST;
1103 while (src_idx--)
1104 __free_page(xor_srcs[src_idx]);
1105 __free_page(dest);
1106 return err;
1107}
1108
1109static int __devexit iop_adma_remove(struct platform_device *dev)
1110{
1111 struct iop_adma_device *device = platform_get_drvdata(dev);
1112 struct dma_chan *chan, *_chan;
1113 struct iop_adma_chan *iop_chan;
Dan Williamsc2110922007-01-02 13:52:26 -07001114 struct iop_adma_platform_data *plat_data = dev->dev.platform_data;
1115
1116 dma_async_device_unregister(&device->common);
1117
Dan Williamsc2110922007-01-02 13:52:26 -07001118 dma_free_coherent(&dev->dev, plat_data->pool_size,
1119 device->dma_desc_pool_virt, device->dma_desc_pool);
1120
Dan Williamsc2110922007-01-02 13:52:26 -07001121 list_for_each_entry_safe(chan, _chan, &device->common.channels,
1122 device_node) {
1123 iop_chan = to_iop_adma_chan(chan);
1124 list_del(&chan->device_node);
1125 kfree(iop_chan);
1126 }
1127 kfree(device);
1128
1129 return 0;
1130}
1131
1132static int __devinit iop_adma_probe(struct platform_device *pdev)
1133{
1134 struct resource *res;
1135 int ret = 0, i;
1136 struct iop_adma_device *adev;
1137 struct iop_adma_chan *iop_chan;
1138 struct dma_device *dma_dev;
1139 struct iop_adma_platform_data *plat_data = pdev->dev.platform_data;
1140
1141 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1142 if (!res)
1143 return -ENODEV;
1144
1145 if (!devm_request_mem_region(&pdev->dev, res->start,
1146 res->end - res->start, pdev->name))
1147 return -EBUSY;
1148
1149 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
1150 if (!adev)
1151 return -ENOMEM;
1152 dma_dev = &adev->common;
1153
1154 /* allocate coherent memory for hardware descriptors
1155 * note: writecombine gives slightly better performance, but
1156 * requires that we explicitly flush the writes
1157 */
1158 if ((adev->dma_desc_pool_virt = dma_alloc_writecombine(&pdev->dev,
1159 plat_data->pool_size,
1160 &adev->dma_desc_pool,
1161 GFP_KERNEL)) == NULL) {
1162 ret = -ENOMEM;
1163 goto err_free_adev;
1164 }
1165
1166 dev_dbg(&pdev->dev, "%s: allocted descriptor pool virt %p phys %p\n",
Harvey Harrison3d9b5252008-03-13 17:45:28 -07001167 __func__, adev->dma_desc_pool_virt,
Dan Williamsc2110922007-01-02 13:52:26 -07001168 (void *) adev->dma_desc_pool);
1169
1170 adev->id = plat_data->hw_id;
1171
1172 /* discover transaction capabilites from the platform data */
1173 dma_dev->cap_mask = plat_data->cap_mask;
1174
1175 adev->pdev = pdev;
1176 platform_set_drvdata(pdev, adev);
1177
1178 INIT_LIST_HEAD(&dma_dev->channels);
1179
1180 /* set base routines */
1181 dma_dev->device_alloc_chan_resources = iop_adma_alloc_chan_resources;
1182 dma_dev->device_free_chan_resources = iop_adma_free_chan_resources;
1183 dma_dev->device_is_tx_complete = iop_adma_is_complete;
1184 dma_dev->device_issue_pending = iop_adma_issue_pending;
Dan Williamsc2110922007-01-02 13:52:26 -07001185 dma_dev->dev = &pdev->dev;
1186
1187 /* set prep routines based on capability */
1188 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
1189 dma_dev->device_prep_dma_memcpy = iop_adma_prep_dma_memcpy;
1190 if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
1191 dma_dev->device_prep_dma_memset = iop_adma_prep_dma_memset;
1192 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
1193 dma_dev->max_xor = iop_adma_get_max_xor();
1194 dma_dev->device_prep_dma_xor = iop_adma_prep_dma_xor;
1195 }
Dan Williams099f53c2009-04-08 14:28:37 -07001196 if (dma_has_cap(DMA_XOR_VAL, dma_dev->cap_mask))
1197 dma_dev->device_prep_dma_xor_val =
1198 iop_adma_prep_dma_xor_val;
Dan Williamsc2110922007-01-02 13:52:26 -07001199 if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
1200 dma_dev->device_prep_dma_interrupt =
1201 iop_adma_prep_dma_interrupt;
1202
1203 iop_chan = kzalloc(sizeof(*iop_chan), GFP_KERNEL);
1204 if (!iop_chan) {
1205 ret = -ENOMEM;
1206 goto err_free_dma;
1207 }
1208 iop_chan->device = adev;
1209
1210 iop_chan->mmr_base = devm_ioremap(&pdev->dev, res->start,
1211 res->end - res->start);
1212 if (!iop_chan->mmr_base) {
1213 ret = -ENOMEM;
1214 goto err_free_iop_chan;
1215 }
1216 tasklet_init(&iop_chan->irq_tasklet, iop_adma_tasklet, (unsigned long)
1217 iop_chan);
1218
1219 /* clear errors before enabling interrupts */
1220 iop_adma_device_clear_err_status(iop_chan);
1221
1222 for (i = 0; i < 3; i++) {
1223 irq_handler_t handler[] = { iop_adma_eot_handler,
1224 iop_adma_eoc_handler,
1225 iop_adma_err_handler };
1226 int irq = platform_get_irq(pdev, i);
1227 if (irq < 0) {
1228 ret = -ENXIO;
1229 goto err_free_iop_chan;
1230 } else {
1231 ret = devm_request_irq(&pdev->dev, irq,
1232 handler[i], 0, pdev->name, iop_chan);
1233 if (ret)
1234 goto err_free_iop_chan;
1235 }
1236 }
1237
1238 spin_lock_init(&iop_chan->lock);
Dan Williamsc2110922007-01-02 13:52:26 -07001239 INIT_LIST_HEAD(&iop_chan->chain);
1240 INIT_LIST_HEAD(&iop_chan->all_slots);
Dan Williamsc2110922007-01-02 13:52:26 -07001241 iop_chan->common.device = dma_dev;
1242 list_add_tail(&iop_chan->common.device_node, &dma_dev->channels);
1243
1244 if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
1245 ret = iop_adma_memcpy_self_test(adev);
1246 dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
1247 if (ret)
1248 goto err_free_iop_chan;
1249 }
1250
1251 if (dma_has_cap(DMA_XOR, dma_dev->cap_mask) ||
1252 dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
Dan Williams099f53c2009-04-08 14:28:37 -07001253 ret = iop_adma_xor_val_self_test(adev);
Dan Williamsc2110922007-01-02 13:52:26 -07001254 dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
1255 if (ret)
1256 goto err_free_iop_chan;
1257 }
1258
1259 dev_printk(KERN_INFO, &pdev->dev, "Intel(R) IOP: "
1260 "( %s%s%s%s%s%s%s%s%s%s)\n",
Dan Williamsb2f46fd2009-07-14 12:20:36 -07001261 dma_has_cap(DMA_PQ, dma_dev->cap_mask) ? "pq " : "",
Dan Williamsc2110922007-01-02 13:52:26 -07001262 dma_has_cap(DMA_PQ_UPDATE, dma_dev->cap_mask) ? "pq_update " : "",
Dan Williams099f53c2009-04-08 14:28:37 -07001263 dma_has_cap(DMA_PQ_VAL, dma_dev->cap_mask) ? "pq_val " : "",
Dan Williamsc2110922007-01-02 13:52:26 -07001264 dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
1265 dma_has_cap(DMA_DUAL_XOR, dma_dev->cap_mask) ? "dual_xor " : "",
Dan Williams099f53c2009-04-08 14:28:37 -07001266 dma_has_cap(DMA_XOR_VAL, dma_dev->cap_mask) ? "xor_val " : "",
Dan Williamsc2110922007-01-02 13:52:26 -07001267 dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
1268 dma_has_cap(DMA_MEMCPY_CRC32C, dma_dev->cap_mask) ? "cpy+crc " : "",
1269 dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
1270 dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
1271
1272 dma_async_device_register(dma_dev);
1273 goto out;
1274
1275 err_free_iop_chan:
1276 kfree(iop_chan);
1277 err_free_dma:
1278 dma_free_coherent(&adev->pdev->dev, plat_data->pool_size,
1279 adev->dma_desc_pool_virt, adev->dma_desc_pool);
1280 err_free_adev:
1281 kfree(adev);
1282 out:
1283 return ret;
1284}
1285
1286static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan)
1287{
1288 struct iop_adma_desc_slot *sw_desc, *grp_start;
1289 dma_cookie_t cookie;
1290 int slot_cnt, slots_per_op;
1291
Harvey Harrison3d9b5252008-03-13 17:45:28 -07001292 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -07001293
1294 spin_lock_bh(&iop_chan->lock);
1295 slot_cnt = iop_chan_memcpy_slot_count(0, &slots_per_op);
1296 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
1297 if (sw_desc) {
1298 grp_start = sw_desc->group_head;
1299
1300 list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
Dan Williams636bdea2008-04-17 20:17:26 -07001301 async_tx_ack(&sw_desc->async_tx);
Dan Williamsc2110922007-01-02 13:52:26 -07001302 iop_desc_init_memcpy(grp_start, 0);
1303 iop_desc_set_byte_count(grp_start, iop_chan, 0);
1304 iop_desc_set_dest_addr(grp_start, iop_chan, 0);
1305 iop_desc_set_memcpy_src_addr(grp_start, 0);
1306
1307 cookie = iop_chan->common.cookie;
1308 cookie++;
1309 if (cookie <= 1)
1310 cookie = 2;
1311
1312 /* initialize the completed cookie to be less than
1313 * the most recently used cookie
1314 */
1315 iop_chan->completed_cookie = cookie - 1;
1316 iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
1317
1318 /* channel should not be busy */
1319 BUG_ON(iop_chan_is_busy(iop_chan));
1320
1321 /* clear any prior error-status bits */
1322 iop_adma_device_clear_err_status(iop_chan);
1323
1324 /* disable operation */
1325 iop_chan_disable(iop_chan);
1326
1327 /* set the descriptor address */
1328 iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
1329
1330 /* 1/ don't add pre-chained descriptors
1331 * 2/ dummy read to flush next_desc write
1332 */
1333 BUG_ON(iop_desc_get_next_desc(sw_desc));
1334
1335 /* run the descriptor */
1336 iop_chan_enable(iop_chan);
1337 } else
1338 dev_printk(KERN_ERR, iop_chan->device->common.dev,
1339 "failed to allocate null descriptor\n");
1340 spin_unlock_bh(&iop_chan->lock);
1341}
1342
1343static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan)
1344{
1345 struct iop_adma_desc_slot *sw_desc, *grp_start;
1346 dma_cookie_t cookie;
1347 int slot_cnt, slots_per_op;
1348
Harvey Harrison3d9b5252008-03-13 17:45:28 -07001349 dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
Dan Williamsc2110922007-01-02 13:52:26 -07001350
1351 spin_lock_bh(&iop_chan->lock);
1352 slot_cnt = iop_chan_xor_slot_count(0, 2, &slots_per_op);
1353 sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
1354 if (sw_desc) {
1355 grp_start = sw_desc->group_head;
1356 list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
Dan Williams636bdea2008-04-17 20:17:26 -07001357 async_tx_ack(&sw_desc->async_tx);
Dan Williamsc2110922007-01-02 13:52:26 -07001358 iop_desc_init_null_xor(grp_start, 2, 0);
1359 iop_desc_set_byte_count(grp_start, iop_chan, 0);
1360 iop_desc_set_dest_addr(grp_start, iop_chan, 0);
1361 iop_desc_set_xor_src_addr(grp_start, 0, 0);
1362 iop_desc_set_xor_src_addr(grp_start, 1, 0);
1363
1364 cookie = iop_chan->common.cookie;
1365 cookie++;
1366 if (cookie <= 1)
1367 cookie = 2;
1368
1369 /* initialize the completed cookie to be less than
1370 * the most recently used cookie
1371 */
1372 iop_chan->completed_cookie = cookie - 1;
1373 iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
1374
1375 /* channel should not be busy */
1376 BUG_ON(iop_chan_is_busy(iop_chan));
1377
1378 /* clear any prior error-status bits */
1379 iop_adma_device_clear_err_status(iop_chan);
1380
1381 /* disable operation */
1382 iop_chan_disable(iop_chan);
1383
1384 /* set the descriptor address */
1385 iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
1386
1387 /* 1/ don't add pre-chained descriptors
1388 * 2/ dummy read to flush next_desc write
1389 */
1390 BUG_ON(iop_desc_get_next_desc(sw_desc));
1391
1392 /* run the descriptor */
1393 iop_chan_enable(iop_chan);
1394 } else
1395 dev_printk(KERN_ERR, iop_chan->device->common.dev,
1396 "failed to allocate null descriptor\n");
1397 spin_unlock_bh(&iop_chan->lock);
1398}
1399
Kay Sieversebabe272008-07-08 11:58:28 -07001400MODULE_ALIAS("platform:iop-adma");
1401
Dan Williamsc2110922007-01-02 13:52:26 -07001402static struct platform_driver iop_adma_driver = {
1403 .probe = iop_adma_probe,
Russell Kingbdf602b2009-03-03 13:43:47 +00001404 .remove = __devexit_p(iop_adma_remove),
Dan Williamsc2110922007-01-02 13:52:26 -07001405 .driver = {
1406 .owner = THIS_MODULE,
1407 .name = "iop-adma",
1408 },
1409};
1410
1411static int __init iop_adma_init (void)
1412{
Dan Williamsc2110922007-01-02 13:52:26 -07001413 return platform_driver_register(&iop_adma_driver);
1414}
1415
1416static void __exit iop_adma_exit (void)
1417{
1418 platform_driver_unregister(&iop_adma_driver);
1419 return;
1420}
Rusty Russellaf49d922007-10-16 23:26:27 -07001421module_exit(iop_adma_exit);
Dan Williamsc2110922007-01-02 13:52:26 -07001422module_init(iop_adma_init);
Dan Williamsc2110922007-01-02 13:52:26 -07001423
1424MODULE_AUTHOR("Intel Corporation");
1425MODULE_DESCRIPTION("IOP ADMA Engine Driver");
1426MODULE_LICENSE("GPL");