2 * Copyright (C) 2006-2009 DENX Software Engineering.
4 * Author: Yuri Tikhonov <yur@emcraft.com>
6 * Further porting to arch/powerpc by
7 * Anatolij Gustschin <agust@denx.de>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The full GNU General Public License is included in this distribution in the
24 * file called COPYING.
28 * This driver supports the asynchrounous DMA copy and RAID engines available
29 * on the AMCC PPC440SPe Processors.
30 * Based on the Intel Xscale(R) family of I/O Processors (IOP 32x, 33x, 134x)
31 * ADMA driver written by D.Williams.
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/async_tx.h>
37 #include <linux/delay.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/spinlock.h>
40 #include <linux/interrupt.h>
41 #include <linux/slab.h>
42 #include <linux/uaccess.h>
43 #include <linux/proc_fs.h>
45 #include <linux/of_address.h>
46 #include <linux/of_irq.h>
47 #include <linux/of_platform.h>
49 #include <asm/dcr-regs.h>
51 #include "../dmaengine.h"
53 enum ppc_adma_init_code {
58 PPC_ADMA_INIT_COHERENT,
59 PPC_ADMA_INIT_CHANNEL,
62 PPC_ADMA_INIT_REGISTER
65 static char *ppc_adma_errors[] = {
66 [PPC_ADMA_INIT_OK] = "ok",
67 [PPC_ADMA_INIT_MEMRES] = "failed to get memory resource",
68 [PPC_ADMA_INIT_MEMREG] = "failed to request memory region",
69 [PPC_ADMA_INIT_ALLOC] = "failed to allocate memory for adev "
71 [PPC_ADMA_INIT_COHERENT] = "failed to allocate coherent memory for "
72 "hardware descriptors",
73 [PPC_ADMA_INIT_CHANNEL] = "failed to allocate memory for channel",
74 [PPC_ADMA_INIT_IRQ1] = "failed to request first irq",
75 [PPC_ADMA_INIT_IRQ2] = "failed to request second irq",
76 [PPC_ADMA_INIT_REGISTER] = "failed to register dma async device",
79 static enum ppc_adma_init_code
80 ppc440spe_adma_devices[PPC440SPE_ADMA_ENGINES_NUM];
82 struct ppc_dma_chan_ref {
83 struct dma_chan *chan;
84 struct list_head node;
87 /* The list of channels exported by ppc440spe ADMA */
89 ppc440spe_adma_chan_list = LIST_HEAD_INIT(ppc440spe_adma_chan_list);
91 /* This flag is set when want to refetch the xor chain in the interrupt
94 static u32 do_xor_refetch;
96 /* Pointer to DMA0, DMA1 CP/CS FIFO */
97 static void *ppc440spe_dma_fifo_buf;
99 /* Pointers to last submitted to DMA0, DMA1 CDBs */
100 static struct ppc440spe_adma_desc_slot *chan_last_sub[3];
101 static struct ppc440spe_adma_desc_slot *chan_first_cdb[3];
103 /* Pointer to last linked and submitted xor CB */
104 static struct ppc440spe_adma_desc_slot *xor_last_linked;
105 static struct ppc440spe_adma_desc_slot *xor_last_submit;
107 /* This array is used in data-check operations for storing a pattern */
108 static char ppc440spe_qword[16];
110 static atomic_t ppc440spe_adma_err_irq_ref;
111 static dcr_host_t ppc440spe_mq_dcr_host;
112 static unsigned int ppc440spe_mq_dcr_len;
114 /* Since RXOR operations use the common register (MQ0_CF2H) for setting-up
115 * the block size in transactions, then we do not allow to activate more than
116 * only one RXOR transactions simultaneously. So use this var to store
117 * the information about is RXOR currently active (PPC440SPE_RXOR_RUN bit is
118 * set) or not (PPC440SPE_RXOR_RUN is clear).
120 static unsigned long ppc440spe_rxor_state;
122 /* These are used in enable & check routines
124 static u32 ppc440spe_r6_enabled;
125 static struct ppc440spe_adma_chan *ppc440spe_r6_tchan;
126 static struct completion ppc440spe_r6_test_comp;
128 static int ppc440spe_adma_dma2rxor_prep_src(
129 struct ppc440spe_adma_desc_slot *desc,
130 struct ppc440spe_rxor *cursor, int index,
131 int src_cnt, u32 addr);
132 static void ppc440spe_adma_dma2rxor_set_src(
133 struct ppc440spe_adma_desc_slot *desc,
134 int index, dma_addr_t addr);
135 static void ppc440spe_adma_dma2rxor_set_mult(
136 struct ppc440spe_adma_desc_slot *desc,
140 #define ADMA_LL_DBG(x) ({ if (1) x; 0; })
142 #define ADMA_LL_DBG(x) ({ if (0) x; 0; })
145 static void print_cb(struct ppc440spe_adma_chan *chan, void *block)
151 switch (chan->device->id) {
156 pr_debug("CDB at %p [%d]:\n"
157 "\t attr 0x%02x opc 0x%02x cnt 0x%08x\n"
158 "\t sg1u 0x%08x sg1l 0x%08x\n"
159 "\t sg2u 0x%08x sg2l 0x%08x\n"
160 "\t sg3u 0x%08x sg3l 0x%08x\n",
161 cdb, chan->device->id,
162 cdb->attr, cdb->opc, le32_to_cpu(cdb->cnt),
163 le32_to_cpu(cdb->sg1u), le32_to_cpu(cdb->sg1l),
164 le32_to_cpu(cdb->sg2u), le32_to_cpu(cdb->sg2l),
165 le32_to_cpu(cdb->sg3u), le32_to_cpu(cdb->sg3l)
171 pr_debug("CB at %p [%d]:\n"
172 "\t cbc 0x%08x cbbc 0x%08x cbs 0x%08x\n"
173 "\t cbtah 0x%08x cbtal 0x%08x\n"
174 "\t cblah 0x%08x cblal 0x%08x\n",
175 cb, chan->device->id,
176 cb->cbc, cb->cbbc, cb->cbs,
177 cb->cbtah, cb->cbtal,
178 cb->cblah, cb->cblal);
179 for (i = 0; i < 16; i++) {
180 if (i && !cb->ops[i].h && !cb->ops[i].l)
182 pr_debug("\t ops[%2d]: h 0x%08x l 0x%08x\n",
183 i, cb->ops[i].h, cb->ops[i].l);
189 static void print_cb_list(struct ppc440spe_adma_chan *chan,
190 struct ppc440spe_adma_desc_slot *iter)
192 for (; iter; iter = iter->hw_next)
193 print_cb(chan, iter->hw_desc);
196 static void prep_dma_xor_dbg(int id, dma_addr_t dst, dma_addr_t *src,
197 unsigned int src_cnt)
201 pr_debug("\n%s(%d):\nsrc: ", __func__, id);
202 for (i = 0; i < src_cnt; i++)
203 pr_debug("\t0x%016llx ", src[i]);
204 pr_debug("dst:\n\t0x%016llx\n", dst);
207 static void prep_dma_pq_dbg(int id, dma_addr_t *dst, dma_addr_t *src,
208 unsigned int src_cnt)
212 pr_debug("\n%s(%d):\nsrc: ", __func__, id);
213 for (i = 0; i < src_cnt; i++)
214 pr_debug("\t0x%016llx ", src[i]);
216 for (i = 0; i < 2; i++)
217 pr_debug("\t0x%016llx ", dst[i]);
220 static void prep_dma_pqzero_sum_dbg(int id, dma_addr_t *src,
221 unsigned int src_cnt,
222 const unsigned char *scf)
226 pr_debug("\n%s(%d):\nsrc(coef): ", __func__, id);
228 for (i = 0; i < src_cnt; i++)
229 pr_debug("\t0x%016llx(0x%02x) ", src[i], scf[i]);
231 for (i = 0; i < src_cnt; i++)
232 pr_debug("\t0x%016llx(no) ", src[i]);
236 for (i = 0; i < 2; i++)
237 pr_debug("\t0x%016llx ", src[src_cnt + i]);
240 /******************************************************************************
241 * Command (Descriptor) Blocks low-level routines
242 ******************************************************************************/
244 * ppc440spe_desc_init_interrupt - initialize the descriptor for INTERRUPT
247 static void ppc440spe_desc_init_interrupt(struct ppc440spe_adma_desc_slot *desc,
248 struct ppc440spe_adma_chan *chan)
252 switch (chan->device->id) {
253 case PPC440SPE_XOR_ID:
255 memset(desc->hw_desc, 0, sizeof(struct xor_cb));
256 /* NOP with Command Block Complete Enable */
257 p->cbc = XOR_CBCR_CBCE_BIT;
259 case PPC440SPE_DMA0_ID:
260 case PPC440SPE_DMA1_ID:
261 memset(desc->hw_desc, 0, sizeof(struct dma_cdb));
262 /* NOP with interrupt */
263 set_bit(PPC440SPE_DESC_INT, &desc->flags);
266 printk(KERN_ERR "Unsupported id %d in %s\n", chan->device->id,
273 * ppc440spe_desc_init_null_xor - initialize the descriptor for NULL XOR
276 static void ppc440spe_desc_init_null_xor(struct ppc440spe_adma_desc_slot *desc)
278 memset(desc->hw_desc, 0, sizeof(struct xor_cb));
279 desc->hw_next = NULL;
285 * ppc440spe_desc_init_xor - initialize the descriptor for XOR operation
287 static void ppc440spe_desc_init_xor(struct ppc440spe_adma_desc_slot *desc,
288 int src_cnt, unsigned long flags)
290 struct xor_cb *hw_desc = desc->hw_desc;
292 memset(desc->hw_desc, 0, sizeof(struct xor_cb));
293 desc->hw_next = NULL;
294 desc->src_cnt = src_cnt;
297 hw_desc->cbc = XOR_CBCR_TGT_BIT | src_cnt;
298 if (flags & DMA_PREP_INTERRUPT)
299 /* Enable interrupt on completion */
300 hw_desc->cbc |= XOR_CBCR_CBCE_BIT;
304 * ppc440spe_desc_init_dma2pq - initialize the descriptor for PQ
305 * operation in DMA2 controller
307 static void ppc440spe_desc_init_dma2pq(struct ppc440spe_adma_desc_slot *desc,
308 int dst_cnt, int src_cnt, unsigned long flags)
310 struct xor_cb *hw_desc = desc->hw_desc;
312 memset(desc->hw_desc, 0, sizeof(struct xor_cb));
313 desc->hw_next = NULL;
314 desc->src_cnt = src_cnt;
315 desc->dst_cnt = dst_cnt;
316 memset(desc->reverse_flags, 0, sizeof(desc->reverse_flags));
317 desc->descs_per_op = 0;
319 hw_desc->cbc = XOR_CBCR_TGT_BIT;
320 if (flags & DMA_PREP_INTERRUPT)
321 /* Enable interrupt on completion */
322 hw_desc->cbc |= XOR_CBCR_CBCE_BIT;
325 #define DMA_CTRL_FLAGS_LAST DMA_PREP_FENCE
326 #define DMA_PREP_ZERO_P (DMA_CTRL_FLAGS_LAST << 1)
327 #define DMA_PREP_ZERO_Q (DMA_PREP_ZERO_P << 1)
330 * ppc440spe_desc_init_dma01pq - initialize the descriptors for PQ operation
333 static void ppc440spe_desc_init_dma01pq(struct ppc440spe_adma_desc_slot *desc,
334 int dst_cnt, int src_cnt, unsigned long flags,
337 struct dma_cdb *hw_desc;
338 struct ppc440spe_adma_desc_slot *iter;
341 /* Common initialization of a PQ descriptors chain */
342 set_bits(op, &desc->flags);
343 desc->src_cnt = src_cnt;
344 desc->dst_cnt = dst_cnt;
346 /* WXOR MULTICAST if both P and Q are being computed
347 * MV_SG1_SG2 if Q only
349 dopc = (desc->dst_cnt == DMA_DEST_MAX_NUM) ?
350 DMA_CDB_OPC_MULTICAST : DMA_CDB_OPC_MV_SG1_SG2;
352 list_for_each_entry(iter, &desc->group_list, chain_node) {
353 hw_desc = iter->hw_desc;
354 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
356 if (likely(!list_is_last(&iter->chain_node,
357 &desc->group_list))) {
358 /* set 'next' pointer */
359 iter->hw_next = list_entry(iter->chain_node.next,
360 struct ppc440spe_adma_desc_slot, chain_node);
361 clear_bit(PPC440SPE_DESC_INT, &iter->flags);
363 /* this is the last descriptor.
364 * this slot will be pasted from ADMA level
365 * each time it wants to configure parameters
366 * of the transaction (src, dst, ...)
368 iter->hw_next = NULL;
369 if (flags & DMA_PREP_INTERRUPT)
370 set_bit(PPC440SPE_DESC_INT, &iter->flags);
372 clear_bit(PPC440SPE_DESC_INT, &iter->flags);
376 /* Set OPS depending on WXOR/RXOR type of operation */
377 if (!test_bit(PPC440SPE_DESC_RXOR, &desc->flags)) {
378 /* This is a WXOR only chain:
379 * - first descriptors are for zeroing destinations
380 * if PPC440SPE_ZERO_P/Q set;
381 * - descriptors remained are for GF-XOR operations.
383 iter = list_first_entry(&desc->group_list,
384 struct ppc440spe_adma_desc_slot,
387 if (test_bit(PPC440SPE_ZERO_P, &desc->flags)) {
388 hw_desc = iter->hw_desc;
389 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
390 iter = list_first_entry(&iter->chain_node,
391 struct ppc440spe_adma_desc_slot,
395 if (test_bit(PPC440SPE_ZERO_Q, &desc->flags)) {
396 hw_desc = iter->hw_desc;
397 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
398 iter = list_first_entry(&iter->chain_node,
399 struct ppc440spe_adma_desc_slot,
403 list_for_each_entry_from(iter, &desc->group_list, chain_node) {
404 hw_desc = iter->hw_desc;
408 /* This is either RXOR-only or mixed RXOR/WXOR */
410 /* The first 1 or 2 slots in chain are always RXOR,
411 * if need to calculate P & Q, then there are two
412 * RXOR slots; if only P or only Q, then there is one
414 iter = list_first_entry(&desc->group_list,
415 struct ppc440spe_adma_desc_slot,
417 hw_desc = iter->hw_desc;
418 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
420 if (desc->dst_cnt == DMA_DEST_MAX_NUM) {
421 iter = list_first_entry(&iter->chain_node,
422 struct ppc440spe_adma_desc_slot,
424 hw_desc = iter->hw_desc;
425 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
428 /* The remaining descs (if any) are WXORs */
429 if (test_bit(PPC440SPE_DESC_WXOR, &desc->flags)) {
430 iter = list_first_entry(&iter->chain_node,
431 struct ppc440spe_adma_desc_slot,
433 list_for_each_entry_from(iter, &desc->group_list,
435 hw_desc = iter->hw_desc;
443 * ppc440spe_desc_init_dma01pqzero_sum - initialize the descriptor
444 * for PQ_ZERO_SUM operation
446 static void ppc440spe_desc_init_dma01pqzero_sum(
447 struct ppc440spe_adma_desc_slot *desc,
448 int dst_cnt, int src_cnt)
450 struct dma_cdb *hw_desc;
451 struct ppc440spe_adma_desc_slot *iter;
453 u8 dopc = (dst_cnt == 2) ? DMA_CDB_OPC_MULTICAST :
454 DMA_CDB_OPC_MV_SG1_SG2;
456 * Initialize starting from 2nd or 3rd descriptor dependent
457 * on dst_cnt. First one or two slots are for cloning P
458 * and/or Q to chan->pdest and/or chan->qdest as we have
459 * to preserve original P/Q.
461 iter = list_first_entry(&desc->group_list,
462 struct ppc440spe_adma_desc_slot, chain_node);
463 iter = list_entry(iter->chain_node.next,
464 struct ppc440spe_adma_desc_slot, chain_node);
467 iter = list_entry(iter->chain_node.next,
468 struct ppc440spe_adma_desc_slot, chain_node);
470 /* initialize each source descriptor in chain */
471 list_for_each_entry_from(iter, &desc->group_list, chain_node) {
472 hw_desc = iter->hw_desc;
473 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
477 /* This is a ZERO_SUM operation:
478 * - <src_cnt> descriptors starting from 2nd or 3rd
479 * descriptor are for GF-XOR operations;
480 * - remaining <dst_cnt> descriptors are for checking the result
483 /* MV_SG1_SG2 if only Q is being verified
484 * MULTICAST if both P and Q are being verified
488 /* DMA_CDB_OPC_DCHECK128 operation */
489 hw_desc->opc = DMA_CDB_OPC_DCHECK128;
491 if (likely(!list_is_last(&iter->chain_node,
492 &desc->group_list))) {
493 /* set 'next' pointer */
494 iter->hw_next = list_entry(iter->chain_node.next,
495 struct ppc440spe_adma_desc_slot,
498 /* this is the last descriptor.
499 * this slot will be pasted from ADMA level
500 * each time it wants to configure parameters
501 * of the transaction (src, dst, ...)
503 iter->hw_next = NULL;
504 /* always enable interrupt generation since we get
505 * the status of pqzero from the handler
507 set_bit(PPC440SPE_DESC_INT, &iter->flags);
510 desc->src_cnt = src_cnt;
511 desc->dst_cnt = dst_cnt;
515 * ppc440spe_desc_init_memcpy - initialize the descriptor for MEMCPY operation
517 static void ppc440spe_desc_init_memcpy(struct ppc440spe_adma_desc_slot *desc,
520 struct dma_cdb *hw_desc = desc->hw_desc;
522 memset(desc->hw_desc, 0, sizeof(struct dma_cdb));
523 desc->hw_next = NULL;
527 if (flags & DMA_PREP_INTERRUPT)
528 set_bit(PPC440SPE_DESC_INT, &desc->flags);
530 clear_bit(PPC440SPE_DESC_INT, &desc->flags);
532 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
536 * ppc440spe_desc_set_src_addr - set source address into the descriptor
538 static void ppc440spe_desc_set_src_addr(struct ppc440spe_adma_desc_slot *desc,
539 struct ppc440spe_adma_chan *chan,
540 int src_idx, dma_addr_t addrh,
543 struct dma_cdb *dma_hw_desc;
544 struct xor_cb *xor_hw_desc;
545 phys_addr_t addr64, tmplow, tmphi;
547 switch (chan->device->id) {
548 case PPC440SPE_DMA0_ID:
549 case PPC440SPE_DMA1_ID:
552 tmphi = (addr64 >> 32);
553 tmplow = (addr64 & 0xFFFFFFFF);
558 dma_hw_desc = desc->hw_desc;
559 dma_hw_desc->sg1l = cpu_to_le32((u32)tmplow);
560 dma_hw_desc->sg1u |= cpu_to_le32((u32)tmphi);
562 case PPC440SPE_XOR_ID:
563 xor_hw_desc = desc->hw_desc;
564 xor_hw_desc->ops[src_idx].l = addrl;
565 xor_hw_desc->ops[src_idx].h |= addrh;
571 * ppc440spe_desc_set_src_mult - set source address mult into the descriptor
573 static void ppc440spe_desc_set_src_mult(struct ppc440spe_adma_desc_slot *desc,
574 struct ppc440spe_adma_chan *chan, u32 mult_index,
575 int sg_index, unsigned char mult_value)
577 struct dma_cdb *dma_hw_desc;
578 struct xor_cb *xor_hw_desc;
581 switch (chan->device->id) {
582 case PPC440SPE_DMA0_ID:
583 case PPC440SPE_DMA1_ID:
584 dma_hw_desc = desc->hw_desc;
587 /* for RXOR operations set multiplier
588 * into source cued address
591 psgu = &dma_hw_desc->sg1u;
593 /* for WXOR operations set multiplier
594 * into destination cued address(es)
596 case DMA_CDB_SG_DST1:
597 psgu = &dma_hw_desc->sg2u;
599 case DMA_CDB_SG_DST2:
600 psgu = &dma_hw_desc->sg3u;
606 *psgu |= cpu_to_le32(mult_value << mult_index);
608 case PPC440SPE_XOR_ID:
609 xor_hw_desc = desc->hw_desc;
617 * ppc440spe_desc_set_dest_addr - set destination address into the descriptor
619 static void ppc440spe_desc_set_dest_addr(struct ppc440spe_adma_desc_slot *desc,
620 struct ppc440spe_adma_chan *chan,
621 dma_addr_t addrh, dma_addr_t addrl,
624 struct dma_cdb *dma_hw_desc;
625 struct xor_cb *xor_hw_desc;
626 phys_addr_t addr64, tmphi, tmplow;
629 switch (chan->device->id) {
630 case PPC440SPE_DMA0_ID:
631 case PPC440SPE_DMA1_ID:
634 tmphi = (addr64 >> 32);
635 tmplow = (addr64 & 0xFFFFFFFF);
640 dma_hw_desc = desc->hw_desc;
642 psgu = dst_idx ? &dma_hw_desc->sg3u : &dma_hw_desc->sg2u;
643 psgl = dst_idx ? &dma_hw_desc->sg3l : &dma_hw_desc->sg2l;
645 *psgl = cpu_to_le32((u32)tmplow);
646 *psgu |= cpu_to_le32((u32)tmphi);
648 case PPC440SPE_XOR_ID:
649 xor_hw_desc = desc->hw_desc;
650 xor_hw_desc->cbtal = addrl;
651 xor_hw_desc->cbtah |= addrh;
657 * ppc440spe_desc_set_byte_count - set number of data bytes involved
660 static void ppc440spe_desc_set_byte_count(struct ppc440spe_adma_desc_slot *desc,
661 struct ppc440spe_adma_chan *chan,
664 struct dma_cdb *dma_hw_desc;
665 struct xor_cb *xor_hw_desc;
667 switch (chan->device->id) {
668 case PPC440SPE_DMA0_ID:
669 case PPC440SPE_DMA1_ID:
670 dma_hw_desc = desc->hw_desc;
671 dma_hw_desc->cnt = cpu_to_le32(byte_count);
673 case PPC440SPE_XOR_ID:
674 xor_hw_desc = desc->hw_desc;
675 xor_hw_desc->cbbc = byte_count;
681 * ppc440spe_desc_set_rxor_block_size - set RXOR block size
683 static inline void ppc440spe_desc_set_rxor_block_size(u32 byte_count)
685 /* assume that byte_count is aligned on the 512-boundary;
686 * thus write it directly to the register (bits 23:31 are
689 dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CF2H, byte_count);
693 * ppc440spe_desc_set_dcheck - set CHECK pattern
695 static void ppc440spe_desc_set_dcheck(struct ppc440spe_adma_desc_slot *desc,
696 struct ppc440spe_adma_chan *chan, u8 *qword)
698 struct dma_cdb *dma_hw_desc;
700 switch (chan->device->id) {
701 case PPC440SPE_DMA0_ID:
702 case PPC440SPE_DMA1_ID:
703 dma_hw_desc = desc->hw_desc;
704 iowrite32(qword[0], &dma_hw_desc->sg3l);
705 iowrite32(qword[4], &dma_hw_desc->sg3u);
706 iowrite32(qword[8], &dma_hw_desc->sg2l);
707 iowrite32(qword[12], &dma_hw_desc->sg2u);
715 * ppc440spe_xor_set_link - set link address in xor CB
717 static void ppc440spe_xor_set_link(struct ppc440spe_adma_desc_slot *prev_desc,
718 struct ppc440spe_adma_desc_slot *next_desc)
720 struct xor_cb *xor_hw_desc = prev_desc->hw_desc;
722 if (unlikely(!next_desc || !(next_desc->phys))) {
723 printk(KERN_ERR "%s: next_desc=0x%p; next_desc->phys=0x%llx\n",
725 next_desc ? next_desc->phys : 0);
729 xor_hw_desc->cbs = 0;
730 xor_hw_desc->cblal = next_desc->phys;
731 xor_hw_desc->cblah = 0;
732 xor_hw_desc->cbc |= XOR_CBCR_LNK_BIT;
736 * ppc440spe_desc_set_link - set the address of descriptor following this
737 * descriptor in chain
739 static void ppc440spe_desc_set_link(struct ppc440spe_adma_chan *chan,
740 struct ppc440spe_adma_desc_slot *prev_desc,
741 struct ppc440spe_adma_desc_slot *next_desc)
744 struct ppc440spe_adma_desc_slot *tail = next_desc;
746 if (unlikely(!prev_desc || !next_desc ||
747 (prev_desc->hw_next && prev_desc->hw_next != next_desc))) {
748 /* If previous next is overwritten something is wrong.
749 * though we may refetch from append to initiate list
750 * processing; in this case - it's ok.
752 printk(KERN_ERR "%s: prev_desc=0x%p; next_desc=0x%p; "
753 "prev->hw_next=0x%p\n", __func__, prev_desc,
754 next_desc, prev_desc ? prev_desc->hw_next : 0);
758 local_irq_save(flags);
760 /* do s/w chaining both for DMA and XOR descriptors */
761 prev_desc->hw_next = next_desc;
763 switch (chan->device->id) {
764 case PPC440SPE_DMA0_ID:
765 case PPC440SPE_DMA1_ID:
767 case PPC440SPE_XOR_ID:
768 /* bind descriptor to the chain */
769 while (tail->hw_next)
770 tail = tail->hw_next;
771 xor_last_linked = tail;
773 if (prev_desc == xor_last_submit)
774 /* do not link to the last submitted CB */
776 ppc440spe_xor_set_link(prev_desc, next_desc);
780 local_irq_restore(flags);
784 * ppc440spe_desc_get_link - get the address of the descriptor that
787 static inline u32 ppc440spe_desc_get_link(struct ppc440spe_adma_desc_slot *desc,
788 struct ppc440spe_adma_chan *chan)
793 return desc->hw_next->phys;
797 * ppc440spe_desc_is_aligned - check alignment
799 static inline int ppc440spe_desc_is_aligned(
800 struct ppc440spe_adma_desc_slot *desc, int num_slots)
802 return (desc->idx & (num_slots - 1)) ? 0 : 1;
806 * ppc440spe_chan_xor_slot_count - get the number of slots necessary for
809 static int ppc440spe_chan_xor_slot_count(size_t len, int src_cnt,
814 /* each XOR descriptor provides up to 16 source operands */
815 slot_cnt = *slots_per_op = (src_cnt + XOR_MAX_OPS - 1)/XOR_MAX_OPS;
817 if (likely(len <= PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT))
820 printk(KERN_ERR "%s: len %d > max %d !!\n",
821 __func__, len, PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
827 * ppc440spe_dma2_pq_slot_count - get the number of slots necessary for
830 static int ppc440spe_dma2_pq_slot_count(dma_addr_t *srcs,
831 int src_cnt, size_t len)
833 signed long long order = 0;
837 for (i = 1; i < src_cnt; i++) {
838 dma_addr_t cur_addr = srcs[i];
839 dma_addr_t old_addr = srcs[i-1];
842 if (cur_addr == old_addr + len) {
848 } else if (old_addr == cur_addr + len) {
859 if (i == src_cnt-2 || (order == -1
860 && cur_addr != old_addr - len)) {
864 } else if (cur_addr == old_addr + len*order) {
868 } else if (cur_addr == old_addr + 2*len) {
872 } else if (cur_addr == old_addr + 3*len) {
891 if (src_cnt <= 1 || (state != 1 && state != 2)) {
892 pr_err("%s: src_cnt=%d, state=%d, addr_count=%d, order=%lld\n",
893 __func__, src_cnt, state, addr_count, order);
894 for (i = 0; i < src_cnt; i++)
895 pr_err("\t[%d] 0x%llx \n", i, srcs[i]);
899 return (addr_count + XOR_MAX_OPS - 1) / XOR_MAX_OPS;
903 /******************************************************************************
904 * ADMA channel low-level routines
905 ******************************************************************************/
908 ppc440spe_chan_get_current_descriptor(struct ppc440spe_adma_chan *chan);
909 static void ppc440spe_chan_append(struct ppc440spe_adma_chan *chan);
912 * ppc440spe_adma_device_clear_eot_status - interrupt ack to XOR or DMA engine
914 static void ppc440spe_adma_device_clear_eot_status(
915 struct ppc440spe_adma_chan *chan)
917 struct dma_regs *dma_reg;
918 struct xor_regs *xor_reg;
919 u8 *p = chan->device->dma_desc_pool_virt;
923 switch (chan->device->id) {
924 case PPC440SPE_DMA0_ID:
925 case PPC440SPE_DMA1_ID:
926 /* read FIFO to ack */
927 dma_reg = chan->device->dma_reg;
928 while ((rv = ioread32(&dma_reg->csfpl))) {
929 i = rv & DMA_CDB_ADDR_MSK;
930 cdb = (struct dma_cdb *)&p[i -
931 (u32)chan->device->dma_desc_pool];
933 /* Clear opcode to ack. This is necessary for
934 * ZeroSum operations only
938 if (test_bit(PPC440SPE_RXOR_RUN,
939 &ppc440spe_rxor_state)) {
940 /* probably this is a completed RXOR op,
941 * get pointer to CDB using the fact that
942 * physical and virtual addresses of CDB
943 * in pools have the same offsets
945 if (le32_to_cpu(cdb->sg1u) &
948 clear_bit(PPC440SPE_RXOR_RUN,
949 &ppc440spe_rxor_state);
953 if (rv & DMA_CDB_STATUS_MSK) {
954 /* ZeroSum check failed
956 struct ppc440spe_adma_desc_slot *iter;
957 dma_addr_t phys = rv & ~DMA_CDB_MSK;
960 * Update the status of corresponding
963 list_for_each_entry(iter, &chan->chain,
965 if (iter->phys == phys)
969 * if cannot find the corresponding
972 BUG_ON(&iter->chain_node == &chan->chain);
974 if (iter->xor_check_result) {
975 if (test_bit(PPC440SPE_DESC_PCHECK,
977 *iter->xor_check_result |=
980 if (test_bit(PPC440SPE_DESC_QCHECK,
982 *iter->xor_check_result |=
990 rv = ioread32(&dma_reg->dsts);
992 pr_err("DMA%d err status: 0x%x\n",
993 chan->device->id, rv);
994 /* write back to clear */
995 iowrite32(rv, &dma_reg->dsts);
998 case PPC440SPE_XOR_ID:
999 /* reset status bits to ack */
1000 xor_reg = chan->device->xor_reg;
1001 rv = ioread32be(&xor_reg->sr);
1002 iowrite32be(rv, &xor_reg->sr);
1004 if (rv & (XOR_IE_ICBIE_BIT|XOR_IE_ICIE_BIT|XOR_IE_RPTIE_BIT)) {
1005 if (rv & XOR_IE_RPTIE_BIT) {
1006 /* Read PLB Timeout Error.
1007 * Try to resubmit the CB
1009 u32 val = ioread32be(&xor_reg->ccbalr);
1011 iowrite32be(val, &xor_reg->cblalr);
1013 val = ioread32be(&xor_reg->crsr);
1014 iowrite32be(val | XOR_CRSR_XAE_BIT,
1017 pr_err("XOR ERR 0x%x status\n", rv);
1021 /* if the XORcore is idle, but there are unprocessed CBs
1022 * then refetch the s/w chain here
1024 if (!(ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT) &&
1026 ppc440spe_chan_append(chan);
1032 * ppc440spe_chan_is_busy - get the channel status
1034 static int ppc440spe_chan_is_busy(struct ppc440spe_adma_chan *chan)
1036 struct dma_regs *dma_reg;
1037 struct xor_regs *xor_reg;
1040 switch (chan->device->id) {
1041 case PPC440SPE_DMA0_ID:
1042 case PPC440SPE_DMA1_ID:
1043 dma_reg = chan->device->dma_reg;
1044 /* if command FIFO's head and tail pointers are equal and
1045 * status tail is the same as command, then channel is free
1047 if (ioread16(&dma_reg->cpfhp) != ioread16(&dma_reg->cpftp) ||
1048 ioread16(&dma_reg->cpftp) != ioread16(&dma_reg->csftp))
1051 case PPC440SPE_XOR_ID:
1052 /* use the special status bit for the XORcore
1054 xor_reg = chan->device->xor_reg;
1055 busy = (ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT) ? 1 : 0;
1063 * ppc440spe_chan_set_first_xor_descriptor - init XORcore chain
1065 static void ppc440spe_chan_set_first_xor_descriptor(
1066 struct ppc440spe_adma_chan *chan,
1067 struct ppc440spe_adma_desc_slot *next_desc)
1069 struct xor_regs *xor_reg = chan->device->xor_reg;
1071 if (ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT)
1072 printk(KERN_INFO "%s: Warn: XORcore is running "
1073 "when try to set the first CDB!\n",
1076 xor_last_submit = xor_last_linked = next_desc;
1078 iowrite32be(XOR_CRSR_64BA_BIT, &xor_reg->crsr);
1080 iowrite32be(next_desc->phys, &xor_reg->cblalr);
1081 iowrite32be(0, &xor_reg->cblahr);
1082 iowrite32be(ioread32be(&xor_reg->cbcr) | XOR_CBCR_LNK_BIT,
1085 chan->hw_chain_inited = 1;
1089 * ppc440spe_dma_put_desc - put DMA0,1 descriptor to FIFO.
1090 * called with irqs disabled
1092 static void ppc440spe_dma_put_desc(struct ppc440spe_adma_chan *chan,
1093 struct ppc440spe_adma_desc_slot *desc)
1096 struct dma_regs *dma_reg = chan->device->dma_reg;
1099 if (!test_bit(PPC440SPE_DESC_INT, &desc->flags))
1100 pcdb |= DMA_CDB_NO_INT;
1102 chan_last_sub[chan->device->id] = desc;
1104 ADMA_LL_DBG(print_cb(chan, desc->hw_desc));
1106 iowrite32(pcdb, &dma_reg->cpfpl);
1110 * ppc440spe_chan_append - update the h/w chain in the channel
1112 static void ppc440spe_chan_append(struct ppc440spe_adma_chan *chan)
1114 struct xor_regs *xor_reg;
1115 struct ppc440spe_adma_desc_slot *iter;
1118 unsigned long flags;
1120 local_irq_save(flags);
1122 switch (chan->device->id) {
1123 case PPC440SPE_DMA0_ID:
1124 case PPC440SPE_DMA1_ID:
1125 cur_desc = ppc440spe_chan_get_current_descriptor(chan);
1127 if (likely(cur_desc)) {
1128 iter = chan_last_sub[chan->device->id];
1132 iter = chan_first_cdb[chan->device->id];
1134 ppc440spe_dma_put_desc(chan, iter);
1135 chan->hw_chain_inited = 1;
1138 /* is there something new to append */
1142 /* flush descriptors from the s/w queue to fifo */
1143 list_for_each_entry_continue(iter, &chan->chain, chain_node) {
1144 ppc440spe_dma_put_desc(chan, iter);
1149 case PPC440SPE_XOR_ID:
1150 /* update h/w links and refetch */
1151 if (!xor_last_submit->hw_next)
1154 xor_reg = chan->device->xor_reg;
1155 /* the last linked CDB has to generate an interrupt
1156 * that we'd be able to append the next lists to h/w
1157 * regardless of the XOR engine state at the moment of
1158 * appending of these next lists
1160 xcb = xor_last_linked->hw_desc;
1161 xcb->cbc |= XOR_CBCR_CBCE_BIT;
1163 if (!(ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT)) {
1164 /* XORcore is idle. Refetch now */
1166 ppc440spe_xor_set_link(xor_last_submit,
1167 xor_last_submit->hw_next);
1169 ADMA_LL_DBG(print_cb_list(chan,
1170 xor_last_submit->hw_next));
1172 xor_last_submit = xor_last_linked;
1173 iowrite32be(ioread32be(&xor_reg->crsr) |
1174 XOR_CRSR_RCBE_BIT | XOR_CRSR_64BA_BIT,
1177 /* XORcore is running. Refetch later in the handler */
1184 local_irq_restore(flags);
1188 * ppc440spe_chan_get_current_descriptor - get the currently executed descriptor
1191 ppc440spe_chan_get_current_descriptor(struct ppc440spe_adma_chan *chan)
1193 struct dma_regs *dma_reg;
1194 struct xor_regs *xor_reg;
1196 if (unlikely(!chan->hw_chain_inited))
1197 /* h/w descriptor chain is not initialized yet */
1200 switch (chan->device->id) {
1201 case PPC440SPE_DMA0_ID:
1202 case PPC440SPE_DMA1_ID:
1203 dma_reg = chan->device->dma_reg;
1204 return ioread32(&dma_reg->acpl) & (~DMA_CDB_MSK);
1205 case PPC440SPE_XOR_ID:
1206 xor_reg = chan->device->xor_reg;
1207 return ioread32be(&xor_reg->ccbalr);
1213 * ppc440spe_chan_run - enable the channel
1215 static void ppc440spe_chan_run(struct ppc440spe_adma_chan *chan)
1217 struct xor_regs *xor_reg;
1219 switch (chan->device->id) {
1220 case PPC440SPE_DMA0_ID:
1221 case PPC440SPE_DMA1_ID:
1222 /* DMAs are always enabled, do nothing */
1224 case PPC440SPE_XOR_ID:
1225 /* drain write buffer */
1226 xor_reg = chan->device->xor_reg;
1228 /* fetch descriptor pointed to in <link> */
1229 iowrite32be(XOR_CRSR_64BA_BIT | XOR_CRSR_XAE_BIT,
1235 /******************************************************************************
1237 ******************************************************************************/
1239 static void ppc440spe_chan_start_null_xor(struct ppc440spe_adma_chan *chan);
1240 static int ppc440spe_adma_alloc_chan_resources(struct dma_chan *chan);
1243 ppc440spe_adma_tx_submit(struct dma_async_tx_descriptor *tx);
1245 static void ppc440spe_adma_set_dest(struct ppc440spe_adma_desc_slot *tx,
1246 dma_addr_t addr, int index);
1248 ppc440spe_adma_memcpy_xor_set_src(struct ppc440spe_adma_desc_slot *tx,
1249 dma_addr_t addr, int index);
1252 ppc440spe_adma_pq_set_dest(struct ppc440spe_adma_desc_slot *tx,
1253 dma_addr_t *paddr, unsigned long flags);
1255 ppc440spe_adma_pq_set_src(struct ppc440spe_adma_desc_slot *tx,
1256 dma_addr_t addr, int index);
1258 ppc440spe_adma_pq_set_src_mult(struct ppc440spe_adma_desc_slot *tx,
1259 unsigned char mult, int index, int dst_pos);
1261 ppc440spe_adma_pqzero_sum_set_dest(struct ppc440spe_adma_desc_slot *tx,
1262 dma_addr_t paddr, dma_addr_t qaddr);
1264 static struct page *ppc440spe_rxor_srcs[32];
1267 * ppc440spe_can_rxor - check if the operands may be processed with RXOR
1269 static int ppc440spe_can_rxor(struct page **srcs, int src_cnt, size_t len)
1271 int i, order = 0, state = 0;
1274 if (unlikely(!(src_cnt > 1)))
1277 BUG_ON(src_cnt > ARRAY_SIZE(ppc440spe_rxor_srcs));
1279 /* Skip holes in the source list before checking */
1280 for (i = 0; i < src_cnt; i++) {
1283 ppc440spe_rxor_srcs[idx++] = srcs[i];
1287 for (i = 1; i < src_cnt; i++) {
1288 char *cur_addr = page_address(ppc440spe_rxor_srcs[i]);
1289 char *old_addr = page_address(ppc440spe_rxor_srcs[i - 1]);
1293 if (cur_addr == old_addr + len) {
1297 } else if (old_addr == cur_addr + len) {
1305 if ((i == src_cnt - 2) ||
1306 (order == -1 && cur_addr != old_addr - len)) {
1309 } else if ((cur_addr == old_addr + len * order) ||
1310 (cur_addr == old_addr + 2 * len) ||
1311 (cur_addr == old_addr + 3 * len)) {
1326 if (state == 1 || state == 2)
1333 * ppc440spe_adma_device_estimate - estimate the efficiency of processing
1334 * the operation given on this channel. It's assumed that 'chan' is
1335 * capable to process 'cap' type of operation.
1336 * @chan: channel to use
1337 * @cap: type of transaction
1338 * @dst_lst: array of destination pointers
1339 * @dst_cnt: number of destination operands
1340 * @src_lst: array of source pointers
1341 * @src_cnt: number of source operands
1342 * @src_sz: size of each source operand
1344 static int ppc440spe_adma_estimate(struct dma_chan *chan,
1345 enum dma_transaction_type cap, struct page **dst_lst, int dst_cnt,
1346 struct page **src_lst, int src_cnt, size_t src_sz)
1350 if (cap == DMA_PQ || cap == DMA_PQ_VAL) {
1351 /* If RAID-6 capabilities were not activated don't try
1354 if (unlikely(!ppc440spe_r6_enabled))
1357 /* In the current implementation of ppc440spe ADMA driver it
1358 * makes sense to pick out only pq case, because it may be
1360 * (1) either using Biskup method on DMA2;
1362 * Thus we give a favour to (1) if the sources are suitable;
1363 * else let it be processed on one of the DMA0/1 engines.
1364 * In the sum_product case where destination is also the
1365 * source process it on DMA0/1 only.
1367 if (cap == DMA_PQ && chan->chan_id == PPC440SPE_XOR_ID) {
1369 if (dst_cnt == 1 && src_cnt == 2 && dst_lst[0] == src_lst[1])
1370 ef = 0; /* sum_product case, process on DMA0/1 */
1371 else if (ppc440spe_can_rxor(src_lst, src_cnt, src_sz))
1372 ef = 3; /* override (DMA0/1 + idle) */
1374 ef = 0; /* can't process on DMA2 if !rxor */
1377 /* channel idleness increases the priority */
1379 !ppc440spe_chan_is_busy(to_ppc440spe_adma_chan(chan)))
1386 ppc440spe_async_tx_find_best_channel(enum dma_transaction_type cap,
1387 struct page **dst_lst, int dst_cnt, struct page **src_lst,
1388 int src_cnt, size_t src_sz)
1390 struct dma_chan *best_chan = NULL;
1391 struct ppc_dma_chan_ref *ref;
1394 if (unlikely(!src_sz))
1396 if (src_sz > PAGE_SIZE) {
1398 * should a user of the api ever pass > PAGE_SIZE requests
1399 * we sort out cases where temporary page-sized buffers
1404 if (src_cnt == 1 && dst_lst[1] == src_lst[0])
1406 if (src_cnt == 2 && dst_lst[1] == src_lst[1])
1417 list_for_each_entry(ref, &ppc440spe_adma_chan_list, node) {
1418 if (dma_has_cap(cap, ref->chan->device->cap_mask)) {
1421 rank = ppc440spe_adma_estimate(ref->chan, cap, dst_lst,
1422 dst_cnt, src_lst, src_cnt, src_sz);
1423 if (rank > best_rank) {
1425 best_chan = ref->chan;
1432 EXPORT_SYMBOL_GPL(ppc440spe_async_tx_find_best_channel);
1435 * ppc440spe_get_group_entry - get group entry with index idx
1436 * @tdesc: is the last allocated slot in the group.
1438 static struct ppc440spe_adma_desc_slot *
1439 ppc440spe_get_group_entry(struct ppc440spe_adma_desc_slot *tdesc, u32 entry_idx)
1441 struct ppc440spe_adma_desc_slot *iter = tdesc->group_head;
1444 if (entry_idx < 0 || entry_idx >= (tdesc->src_cnt + tdesc->dst_cnt)) {
1445 printk("%s: entry_idx %d, src_cnt %d, dst_cnt %d\n",
1446 __func__, entry_idx, tdesc->src_cnt, tdesc->dst_cnt);
1450 list_for_each_entry(iter, &tdesc->group_list, chain_node) {
1451 if (i++ == entry_idx)
1458 * ppc440spe_adma_free_slots - flags descriptor slots for reuse
1459 * @slot: Slot to free
1460 * Caller must hold &ppc440spe_chan->lock while calling this function
1462 static void ppc440spe_adma_free_slots(struct ppc440spe_adma_desc_slot *slot,
1463 struct ppc440spe_adma_chan *chan)
1465 int stride = slot->slots_per_op;
1468 slot->slots_per_op = 0;
1469 slot = list_entry(slot->slot_node.next,
1470 struct ppc440spe_adma_desc_slot,
1476 * ppc440spe_adma_run_tx_complete_actions - call functions to be called
1479 static dma_cookie_t ppc440spe_adma_run_tx_complete_actions(
1480 struct ppc440spe_adma_desc_slot *desc,
1481 struct ppc440spe_adma_chan *chan,
1482 dma_cookie_t cookie)
1484 BUG_ON(desc->async_tx.cookie < 0);
1485 if (desc->async_tx.cookie > 0) {
1486 cookie = desc->async_tx.cookie;
1487 desc->async_tx.cookie = 0;
1489 /* call the callback (must not sleep or submit new
1490 * operations to this channel)
1492 if (desc->async_tx.callback)
1493 desc->async_tx.callback(
1494 desc->async_tx.callback_param);
1496 dma_descriptor_unmap(&desc->async_tx);
1499 /* run dependent operations */
1500 dma_run_dependencies(&desc->async_tx);
1506 * ppc440spe_adma_clean_slot - clean up CDB slot (if ack is set)
1508 static int ppc440spe_adma_clean_slot(struct ppc440spe_adma_desc_slot *desc,
1509 struct ppc440spe_adma_chan *chan)
1511 /* the client is allowed to attach dependent operations
1512 * until 'ack' is set
1514 if (!async_tx_test_ack(&desc->async_tx))
1517 /* leave the last descriptor in the chain
1518 * so we can append to it
1520 if (list_is_last(&desc->chain_node, &chan->chain) ||
1521 desc->phys == ppc440spe_chan_get_current_descriptor(chan))
1524 if (chan->device->id != PPC440SPE_XOR_ID) {
1525 /* our DMA interrupt handler clears opc field of
1526 * each processed descriptor. For all types of
1527 * operations except for ZeroSum we do not actually
1528 * need ack from the interrupt handler. ZeroSum is a
1529 * special case since the result of this operation
1530 * is available from the handler only, so if we see
1531 * such type of descriptor (which is unprocessed yet)
1532 * then leave it in chain.
1534 struct dma_cdb *cdb = desc->hw_desc;
1535 if (cdb->opc == DMA_CDB_OPC_DCHECK128)
1539 dev_dbg(chan->device->common.dev, "\tfree slot %llx: %d stride: %d\n",
1540 desc->phys, desc->idx, desc->slots_per_op);
1542 list_del(&desc->chain_node);
1543 ppc440spe_adma_free_slots(desc, chan);
1548 * __ppc440spe_adma_slot_cleanup - this is the common clean-up routine
1549 * which runs through the channel CDBs list until reach the descriptor
1550 * currently processed. When routine determines that all CDBs of group
1551 * are completed then corresponding callbacks (if any) are called and slots
1554 static void __ppc440spe_adma_slot_cleanup(struct ppc440spe_adma_chan *chan)
1556 struct ppc440spe_adma_desc_slot *iter, *_iter, *group_start = NULL;
1557 dma_cookie_t cookie = 0;
1558 u32 current_desc = ppc440spe_chan_get_current_descriptor(chan);
1559 int busy = ppc440spe_chan_is_busy(chan);
1560 int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
1562 dev_dbg(chan->device->common.dev, "ppc440spe adma%d: %s\n",
1563 chan->device->id, __func__);
1565 if (!current_desc) {
1566 /* There were no transactions yet, so
1572 /* free completed slots from the chain starting with
1573 * the oldest descriptor
1575 list_for_each_entry_safe(iter, _iter, &chan->chain,
1577 dev_dbg(chan->device->common.dev, "\tcookie: %d slot: %d "
1578 "busy: %d this_desc: %#llx next_desc: %#x "
1579 "cur: %#x ack: %d\n",
1580 iter->async_tx.cookie, iter->idx, busy, iter->phys,
1581 ppc440spe_desc_get_link(iter, chan), current_desc,
1582 async_tx_test_ack(&iter->async_tx));
1584 prefetch(&_iter->async_tx);
1586 /* do not advance past the current descriptor loaded into the
1587 * hardware channel,subsequent descriptors are either in process
1588 * or have not been submitted
1593 /* stop the search if we reach the current descriptor and the
1594 * channel is busy, or if it appears that the current descriptor
1595 * needs to be re-read (i.e. has been appended to)
1597 if (iter->phys == current_desc) {
1598 BUG_ON(seen_current++);
1599 if (busy || ppc440spe_desc_get_link(iter, chan)) {
1600 /* not all descriptors of the group have
1601 * been completed; exit.
1607 /* detect the start of a group transaction */
1608 if (!slot_cnt && !slots_per_op) {
1609 slot_cnt = iter->slot_cnt;
1610 slots_per_op = iter->slots_per_op;
1611 if (slot_cnt <= slots_per_op) {
1620 slot_cnt -= slots_per_op;
1623 /* all the members of a group are complete */
1624 if (slots_per_op != 0 && slot_cnt == 0) {
1625 struct ppc440spe_adma_desc_slot *grp_iter, *_grp_iter;
1626 int end_of_chain = 0;
1628 /* clean up the group */
1629 slot_cnt = group_start->slot_cnt;
1630 grp_iter = group_start;
1631 list_for_each_entry_safe_from(grp_iter, _grp_iter,
1632 &chan->chain, chain_node) {
1634 cookie = ppc440spe_adma_run_tx_complete_actions(
1635 grp_iter, chan, cookie);
1637 slot_cnt -= slots_per_op;
1638 end_of_chain = ppc440spe_adma_clean_slot(
1640 if (end_of_chain && slot_cnt) {
1641 /* Should wait for ZeroSum completion */
1643 chan->common.completed_cookie = cookie;
1647 if (slot_cnt == 0 || end_of_chain)
1651 /* the group should be complete at this point */
1660 } else if (slots_per_op) /* wait for group completion */
1663 cookie = ppc440spe_adma_run_tx_complete_actions(iter, chan,
1666 if (ppc440spe_adma_clean_slot(iter, chan))
1670 BUG_ON(!seen_current);
1673 chan->common.completed_cookie = cookie;
1674 pr_debug("\tcompleted cookie %d\n", cookie);
1680 * ppc440spe_adma_tasklet - clean up watch-dog initiator
1682 static void ppc440spe_adma_tasklet(unsigned long data)
1684 struct ppc440spe_adma_chan *chan = (struct ppc440spe_adma_chan *) data;
1686 spin_lock_nested(&chan->lock, SINGLE_DEPTH_NESTING);
1687 __ppc440spe_adma_slot_cleanup(chan);
1688 spin_unlock(&chan->lock);
1692 * ppc440spe_adma_slot_cleanup - clean up scheduled initiator
1694 static void ppc440spe_adma_slot_cleanup(struct ppc440spe_adma_chan *chan)
1696 spin_lock_bh(&chan->lock);
1697 __ppc440spe_adma_slot_cleanup(chan);
1698 spin_unlock_bh(&chan->lock);
1702 * ppc440spe_adma_alloc_slots - allocate free slots (if any)
1704 static struct ppc440spe_adma_desc_slot *ppc440spe_adma_alloc_slots(
1705 struct ppc440spe_adma_chan *chan, int num_slots,
1708 struct ppc440spe_adma_desc_slot *iter = NULL, *_iter;
1709 struct ppc440spe_adma_desc_slot *alloc_start = NULL;
1710 struct list_head chain = LIST_HEAD_INIT(chain);
1711 int slots_found, retry = 0;
1714 BUG_ON(!num_slots || !slots_per_op);
1715 /* start search from the last allocated descrtiptor
1716 * if a contiguous allocation can not be found start searching
1717 * from the beginning of the list
1722 iter = chan->last_used;
1724 iter = list_entry(&chan->all_slots,
1725 struct ppc440spe_adma_desc_slot,
1727 list_for_each_entry_safe_continue(iter, _iter, &chan->all_slots,
1730 prefetch(&_iter->async_tx);
1731 if (iter->slots_per_op) {
1736 /* start the allocation if the slot is correctly aligned */
1740 if (slots_found == num_slots) {
1741 struct ppc440spe_adma_desc_slot *alloc_tail = NULL;
1742 struct ppc440spe_adma_desc_slot *last_used = NULL;
1747 /* pre-ack all but the last descriptor */
1748 if (num_slots != slots_per_op)
1749 async_tx_ack(&iter->async_tx);
1751 list_add_tail(&iter->chain_node, &chain);
1753 iter->async_tx.cookie = 0;
1754 iter->hw_next = NULL;
1756 iter->slot_cnt = num_slots;
1757 iter->xor_check_result = NULL;
1758 for (i = 0; i < slots_per_op; i++) {
1759 iter->slots_per_op = slots_per_op - i;
1761 iter = list_entry(iter->slot_node.next,
1762 struct ppc440spe_adma_desc_slot,
1765 num_slots -= slots_per_op;
1767 alloc_tail->group_head = alloc_start;
1768 alloc_tail->async_tx.cookie = -EBUSY;
1769 list_splice(&chain, &alloc_tail->group_list);
1770 chan->last_used = last_used;
1777 /* try to free some slots if the allocation fails */
1778 tasklet_schedule(&chan->irq_tasklet);
1783 * ppc440spe_adma_alloc_chan_resources - allocate pools for CDB slots
1785 static int ppc440spe_adma_alloc_chan_resources(struct dma_chan *chan)
1787 struct ppc440spe_adma_chan *ppc440spe_chan;
1788 struct ppc440spe_adma_desc_slot *slot = NULL;
1793 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
1794 init = ppc440spe_chan->slots_allocated ? 0 : 1;
1795 chan->chan_id = ppc440spe_chan->device->id;
1797 /* Allocate descriptor slots */
1798 i = ppc440spe_chan->slots_allocated;
1799 if (ppc440spe_chan->device->id != PPC440SPE_XOR_ID)
1800 db_sz = sizeof(struct dma_cdb);
1802 db_sz = sizeof(struct xor_cb);
1804 for (; i < (ppc440spe_chan->device->pool_size / db_sz); i++) {
1805 slot = kzalloc(sizeof(struct ppc440spe_adma_desc_slot),
1808 printk(KERN_INFO "SPE ADMA Channel only initialized"
1809 " %d descriptor slots", i--);
1813 hw_desc = (char *) ppc440spe_chan->device->dma_desc_pool_virt;
1814 slot->hw_desc = (void *) &hw_desc[i * db_sz];
1815 dma_async_tx_descriptor_init(&slot->async_tx, chan);
1816 slot->async_tx.tx_submit = ppc440spe_adma_tx_submit;
1817 INIT_LIST_HEAD(&slot->chain_node);
1818 INIT_LIST_HEAD(&slot->slot_node);
1819 INIT_LIST_HEAD(&slot->group_list);
1820 slot->phys = ppc440spe_chan->device->dma_desc_pool + i * db_sz;
1823 spin_lock_bh(&ppc440spe_chan->lock);
1824 ppc440spe_chan->slots_allocated++;
1825 list_add_tail(&slot->slot_node, &ppc440spe_chan->all_slots);
1826 spin_unlock_bh(&ppc440spe_chan->lock);
1829 if (i && !ppc440spe_chan->last_used) {
1830 ppc440spe_chan->last_used =
1831 list_entry(ppc440spe_chan->all_slots.next,
1832 struct ppc440spe_adma_desc_slot,
1836 dev_dbg(ppc440spe_chan->device->common.dev,
1837 "ppc440spe adma%d: allocated %d descriptor slots\n",
1838 ppc440spe_chan->device->id, i);
1840 /* initialize the channel and the chain with a null operation */
1842 switch (ppc440spe_chan->device->id) {
1843 case PPC440SPE_DMA0_ID:
1844 case PPC440SPE_DMA1_ID:
1845 ppc440spe_chan->hw_chain_inited = 0;
1846 /* Use WXOR for self-testing */
1847 if (!ppc440spe_r6_tchan)
1848 ppc440spe_r6_tchan = ppc440spe_chan;
1850 case PPC440SPE_XOR_ID:
1851 ppc440spe_chan_start_null_xor(ppc440spe_chan);
1856 ppc440spe_chan->needs_unmap = 1;
1859 return (i > 0) ? i : -ENOMEM;
1863 * ppc440spe_rxor_set_region_data -
1865 static void ppc440spe_rxor_set_region(struct ppc440spe_adma_desc_slot *desc,
1866 u8 xor_arg_no, u32 mask)
1868 struct xor_cb *xcb = desc->hw_desc;
1870 xcb->ops[xor_arg_no].h |= mask;
1874 * ppc440spe_rxor_set_src -
1876 static void ppc440spe_rxor_set_src(struct ppc440spe_adma_desc_slot *desc,
1877 u8 xor_arg_no, dma_addr_t addr)
1879 struct xor_cb *xcb = desc->hw_desc;
1881 xcb->ops[xor_arg_no].h |= DMA_CUED_XOR_BASE;
1882 xcb->ops[xor_arg_no].l = addr;
1886 * ppc440spe_rxor_set_mult -
1888 static void ppc440spe_rxor_set_mult(struct ppc440spe_adma_desc_slot *desc,
1889 u8 xor_arg_no, u8 idx, u8 mult)
1891 struct xor_cb *xcb = desc->hw_desc;
1893 xcb->ops[xor_arg_no].h |= mult << (DMA_CUED_MULT1_OFF + idx * 8);
1897 * ppc440spe_adma_check_threshold - append CDBs to h/w chain if threshold
1900 static void ppc440spe_adma_check_threshold(struct ppc440spe_adma_chan *chan)
1902 dev_dbg(chan->device->common.dev, "ppc440spe adma%d: pending: %d\n",
1903 chan->device->id, chan->pending);
1905 if (chan->pending >= PPC440SPE_ADMA_THRESHOLD) {
1907 ppc440spe_chan_append(chan);
1912 * ppc440spe_adma_tx_submit - submit new descriptor group to the channel
1913 * (it's not necessary that descriptors will be submitted to the h/w
1914 * chains too right now)
1916 static dma_cookie_t ppc440spe_adma_tx_submit(struct dma_async_tx_descriptor *tx)
1918 struct ppc440spe_adma_desc_slot *sw_desc;
1919 struct ppc440spe_adma_chan *chan = to_ppc440spe_adma_chan(tx->chan);
1920 struct ppc440spe_adma_desc_slot *group_start, *old_chain_tail;
1923 dma_cookie_t cookie;
1925 sw_desc = tx_to_ppc440spe_adma_slot(tx);
1927 group_start = sw_desc->group_head;
1928 slot_cnt = group_start->slot_cnt;
1929 slots_per_op = group_start->slots_per_op;
1931 spin_lock_bh(&chan->lock);
1932 cookie = dma_cookie_assign(tx);
1934 if (unlikely(list_empty(&chan->chain))) {
1936 list_splice_init(&sw_desc->group_list, &chan->chain);
1937 chan_first_cdb[chan->device->id] = group_start;
1939 /* isn't first peer, bind CDBs to chain */
1940 old_chain_tail = list_entry(chan->chain.prev,
1941 struct ppc440spe_adma_desc_slot,
1943 list_splice_init(&sw_desc->group_list,
1944 &old_chain_tail->chain_node);
1945 /* fix up the hardware chain */
1946 ppc440spe_desc_set_link(chan, old_chain_tail, group_start);
1949 /* increment the pending count by the number of operations */
1950 chan->pending += slot_cnt / slots_per_op;
1951 ppc440spe_adma_check_threshold(chan);
1952 spin_unlock_bh(&chan->lock);
1954 dev_dbg(chan->device->common.dev,
1955 "ppc440spe adma%d: %s cookie: %d slot: %d tx %p\n",
1956 chan->device->id, __func__,
1957 sw_desc->async_tx.cookie, sw_desc->idx, sw_desc);
1963 * ppc440spe_adma_prep_dma_interrupt - prepare CDB for a pseudo DMA operation
1965 static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_interrupt(
1966 struct dma_chan *chan, unsigned long flags)
1968 struct ppc440spe_adma_chan *ppc440spe_chan;
1969 struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
1970 int slot_cnt, slots_per_op;
1972 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
1974 dev_dbg(ppc440spe_chan->device->common.dev,
1975 "ppc440spe adma%d: %s\n", ppc440spe_chan->device->id,
1978 spin_lock_bh(&ppc440spe_chan->lock);
1979 slot_cnt = slots_per_op = 1;
1980 sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
1983 group_start = sw_desc->group_head;
1984 ppc440spe_desc_init_interrupt(group_start, ppc440spe_chan);
1985 group_start->unmap_len = 0;
1986 sw_desc->async_tx.flags = flags;
1988 spin_unlock_bh(&ppc440spe_chan->lock);
1990 return sw_desc ? &sw_desc->async_tx : NULL;
1994 * ppc440spe_adma_prep_dma_memcpy - prepare CDB for a MEMCPY operation
1996 static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_memcpy(
1997 struct dma_chan *chan, dma_addr_t dma_dest,
1998 dma_addr_t dma_src, size_t len, unsigned long flags)
2000 struct ppc440spe_adma_chan *ppc440spe_chan;
2001 struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
2002 int slot_cnt, slots_per_op;
2004 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2009 BUG_ON(len > PPC440SPE_ADMA_DMA_MAX_BYTE_COUNT);
2011 spin_lock_bh(&ppc440spe_chan->lock);
2013 dev_dbg(ppc440spe_chan->device->common.dev,
2014 "ppc440spe adma%d: %s len: %u int_en %d\n",
2015 ppc440spe_chan->device->id, __func__, len,
2016 flags & DMA_PREP_INTERRUPT ? 1 : 0);
2017 slot_cnt = slots_per_op = 1;
2018 sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
2021 group_start = sw_desc->group_head;
2022 ppc440spe_desc_init_memcpy(group_start, flags);
2023 ppc440spe_adma_set_dest(group_start, dma_dest, 0);
2024 ppc440spe_adma_memcpy_xor_set_src(group_start, dma_src, 0);
2025 ppc440spe_desc_set_byte_count(group_start, ppc440spe_chan, len);
2026 sw_desc->unmap_len = len;
2027 sw_desc->async_tx.flags = flags;
2029 spin_unlock_bh(&ppc440spe_chan->lock);
2031 return sw_desc ? &sw_desc->async_tx : NULL;
2035 * ppc440spe_adma_prep_dma_xor - prepare CDB for a XOR operation
2037 static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_xor(
2038 struct dma_chan *chan, dma_addr_t dma_dest,
2039 dma_addr_t *dma_src, u32 src_cnt, size_t len,
2040 unsigned long flags)
2042 struct ppc440spe_adma_chan *ppc440spe_chan;
2043 struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
2044 int slot_cnt, slots_per_op;
2046 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2048 ADMA_LL_DBG(prep_dma_xor_dbg(ppc440spe_chan->device->id,
2049 dma_dest, dma_src, src_cnt));
2052 BUG_ON(len > PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
2054 dev_dbg(ppc440spe_chan->device->common.dev,
2055 "ppc440spe adma%d: %s src_cnt: %d len: %u int_en: %d\n",
2056 ppc440spe_chan->device->id, __func__, src_cnt, len,
2057 flags & DMA_PREP_INTERRUPT ? 1 : 0);
2059 spin_lock_bh(&ppc440spe_chan->lock);
2060 slot_cnt = ppc440spe_chan_xor_slot_count(len, src_cnt, &slots_per_op);
2061 sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
2064 group_start = sw_desc->group_head;
2065 ppc440spe_desc_init_xor(group_start, src_cnt, flags);
2066 ppc440spe_adma_set_dest(group_start, dma_dest, 0);
2068 ppc440spe_adma_memcpy_xor_set_src(group_start,
2069 dma_src[src_cnt], src_cnt);
2070 ppc440spe_desc_set_byte_count(group_start, ppc440spe_chan, len);
2071 sw_desc->unmap_len = len;
2072 sw_desc->async_tx.flags = flags;
2074 spin_unlock_bh(&ppc440spe_chan->lock);
2076 return sw_desc ? &sw_desc->async_tx : NULL;
2080 ppc440spe_desc_set_xor_src_cnt(struct ppc440spe_adma_desc_slot *desc,
2082 static void ppc440spe_init_rxor_cursor(struct ppc440spe_rxor *cursor);
2085 * ppc440spe_adma_init_dma2rxor_slot -
2087 static void ppc440spe_adma_init_dma2rxor_slot(
2088 struct ppc440spe_adma_desc_slot *desc,
2089 dma_addr_t *src, int src_cnt)
2093 /* initialize CDB */
2094 for (i = 0; i < src_cnt; i++) {
2095 ppc440spe_adma_dma2rxor_prep_src(desc, &desc->rxor_cursor, i,
2096 desc->src_cnt, (u32)src[i]);
2101 * ppc440spe_dma01_prep_mult -
2102 * for Q operation where destination is also the source
2104 static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_mult(
2105 struct ppc440spe_adma_chan *ppc440spe_chan,
2106 dma_addr_t *dst, int dst_cnt, dma_addr_t *src, int src_cnt,
2107 const unsigned char *scf, size_t len, unsigned long flags)
2109 struct ppc440spe_adma_desc_slot *sw_desc = NULL;
2110 unsigned long op = 0;
2113 set_bit(PPC440SPE_DESC_WXOR, &op);
2116 spin_lock_bh(&ppc440spe_chan->lock);
2118 /* use WXOR, each descriptor occupies one slot */
2119 sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
2121 struct ppc440spe_adma_chan *chan;
2122 struct ppc440spe_adma_desc_slot *iter;
2123 struct dma_cdb *hw_desc;
2125 chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
2126 set_bits(op, &sw_desc->flags);
2127 sw_desc->src_cnt = src_cnt;
2128 sw_desc->dst_cnt = dst_cnt;
2129 /* First descriptor, zero data in the destination and copy it
2130 * to q page using MULTICAST transfer.
2132 iter = list_first_entry(&sw_desc->group_list,
2133 struct ppc440spe_adma_desc_slot,
2135 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2136 /* set 'next' pointer */
2137 iter->hw_next = list_entry(iter->chain_node.next,
2138 struct ppc440spe_adma_desc_slot,
2140 clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2141 hw_desc = iter->hw_desc;
2142 hw_desc->opc = DMA_CDB_OPC_MULTICAST;
2144 ppc440spe_desc_set_dest_addr(iter, chan,
2145 DMA_CUED_XOR_BASE, dst[0], 0);
2146 ppc440spe_desc_set_dest_addr(iter, chan, 0, dst[1], 1);
2147 ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
2149 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2150 iter->unmap_len = len;
2153 * Second descriptor, multiply data from the q page
2154 * and store the result in real destination.
2156 iter = list_first_entry(&iter->chain_node,
2157 struct ppc440spe_adma_desc_slot,
2159 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2160 iter->hw_next = NULL;
2161 if (flags & DMA_PREP_INTERRUPT)
2162 set_bit(PPC440SPE_DESC_INT, &iter->flags);
2164 clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2166 hw_desc = iter->hw_desc;
2167 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
2168 ppc440spe_desc_set_src_addr(iter, chan, 0,
2169 DMA_CUED_XOR_HB, dst[1]);
2170 ppc440spe_desc_set_dest_addr(iter, chan,
2171 DMA_CUED_XOR_BASE, dst[0], 0);
2173 ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
2174 DMA_CDB_SG_DST1, scf[0]);
2175 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2176 iter->unmap_len = len;
2177 sw_desc->async_tx.flags = flags;
2180 spin_unlock_bh(&ppc440spe_chan->lock);
2186 * ppc440spe_dma01_prep_sum_product -
2187 * Dx = A*(P+Pxy) + B*(Q+Qxy) operation where destination is also
2190 static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_sum_product(
2191 struct ppc440spe_adma_chan *ppc440spe_chan,
2192 dma_addr_t *dst, dma_addr_t *src, int src_cnt,
2193 const unsigned char *scf, size_t len, unsigned long flags)
2195 struct ppc440spe_adma_desc_slot *sw_desc = NULL;
2196 unsigned long op = 0;
2199 set_bit(PPC440SPE_DESC_WXOR, &op);
2202 spin_lock_bh(&ppc440spe_chan->lock);
2204 /* WXOR, each descriptor occupies one slot */
2205 sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
2207 struct ppc440spe_adma_chan *chan;
2208 struct ppc440spe_adma_desc_slot *iter;
2209 struct dma_cdb *hw_desc;
2211 chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
2212 set_bits(op, &sw_desc->flags);
2213 sw_desc->src_cnt = src_cnt;
2214 sw_desc->dst_cnt = 1;
2215 /* 1st descriptor, src[1] data to q page and zero destination */
2216 iter = list_first_entry(&sw_desc->group_list,
2217 struct ppc440spe_adma_desc_slot,
2219 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2220 iter->hw_next = list_entry(iter->chain_node.next,
2221 struct ppc440spe_adma_desc_slot,
2223 clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2224 hw_desc = iter->hw_desc;
2225 hw_desc->opc = DMA_CDB_OPC_MULTICAST;
2227 ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
2229 ppc440spe_desc_set_dest_addr(iter, chan, 0,
2230 ppc440spe_chan->qdest, 1);
2231 ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
2233 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2234 iter->unmap_len = len;
2236 /* 2nd descriptor, multiply src[1] data and store the
2237 * result in destination */
2238 iter = list_first_entry(&iter->chain_node,
2239 struct ppc440spe_adma_desc_slot,
2241 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2242 /* set 'next' pointer */
2243 iter->hw_next = list_entry(iter->chain_node.next,
2244 struct ppc440spe_adma_desc_slot,
2246 if (flags & DMA_PREP_INTERRUPT)
2247 set_bit(PPC440SPE_DESC_INT, &iter->flags);
2249 clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2251 hw_desc = iter->hw_desc;
2252 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
2253 ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
2254 ppc440spe_chan->qdest);
2255 ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
2257 ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
2258 DMA_CDB_SG_DST1, scf[1]);
2259 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2260 iter->unmap_len = len;
2263 * 3rd descriptor, multiply src[0] data and xor it
2266 iter = list_first_entry(&iter->chain_node,
2267 struct ppc440spe_adma_desc_slot,
2269 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2270 iter->hw_next = NULL;
2271 if (flags & DMA_PREP_INTERRUPT)
2272 set_bit(PPC440SPE_DESC_INT, &iter->flags);
2274 clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2276 hw_desc = iter->hw_desc;
2277 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
2278 ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
2280 ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
2282 ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
2283 DMA_CDB_SG_DST1, scf[0]);
2284 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2285 iter->unmap_len = len;
2286 sw_desc->async_tx.flags = flags;
2289 spin_unlock_bh(&ppc440spe_chan->lock);
2294 static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_pq(
2295 struct ppc440spe_adma_chan *ppc440spe_chan,
2296 dma_addr_t *dst, int dst_cnt, dma_addr_t *src, int src_cnt,
2297 const unsigned char *scf, size_t len, unsigned long flags)
2300 struct ppc440spe_adma_desc_slot *sw_desc = NULL, *iter;
2301 unsigned long op = 0;
2302 unsigned char mult = 1;
2304 pr_debug("%s: dst_cnt %d, src_cnt %d, len %d\n",
2305 __func__, dst_cnt, src_cnt, len);
2306 /* select operations WXOR/RXOR depending on the
2307 * source addresses of operators and the number
2308 * of destinations (RXOR support only Q-parity calculations)
2310 set_bit(PPC440SPE_DESC_WXOR, &op);
2311 if (!test_and_set_bit(PPC440SPE_RXOR_RUN, &ppc440spe_rxor_state)) {
2314 * - there are more than 1 source,
2315 * - len is aligned on 512-byte boundary,
2316 * - source addresses fit to one of 4 possible regions.
2319 !(len & MQ0_CF2H_RXOR_BS_MASK) &&
2320 (src[0] + len) == src[1]) {
2321 /* may do RXOR R1 R2 */
2322 set_bit(PPC440SPE_DESC_RXOR, &op);
2324 /* may try to enhance region of RXOR */
2325 if ((src[1] + len) == src[2]) {
2326 /* do RXOR R1 R2 R3 */
2327 set_bit(PPC440SPE_DESC_RXOR123,
2329 } else if ((src[1] + len * 2) == src[2]) {
2330 /* do RXOR R1 R2 R4 */
2331 set_bit(PPC440SPE_DESC_RXOR124, &op);
2332 } else if ((src[1] + len * 3) == src[2]) {
2333 /* do RXOR R1 R2 R5 */
2334 set_bit(PPC440SPE_DESC_RXOR125,
2338 set_bit(PPC440SPE_DESC_RXOR12,
2343 set_bit(PPC440SPE_DESC_RXOR12, &op);
2347 if (!test_bit(PPC440SPE_DESC_RXOR, &op)) {
2348 /* can not do this operation with RXOR */
2349 clear_bit(PPC440SPE_RXOR_RUN,
2350 &ppc440spe_rxor_state);
2352 /* can do; set block size right now */
2353 ppc440spe_desc_set_rxor_block_size(len);
2357 /* Number of necessary slots depends on operation type selected */
2358 if (!test_bit(PPC440SPE_DESC_RXOR, &op)) {
2359 /* This is a WXOR only chain. Need descriptors for each
2360 * source to GF-XOR them with WXOR, and need descriptors
2361 * for each destination to zero them with WXOR
2365 if (flags & DMA_PREP_ZERO_P) {
2367 set_bit(PPC440SPE_ZERO_P, &op);
2369 if (flags & DMA_PREP_ZERO_Q) {
2371 set_bit(PPC440SPE_ZERO_Q, &op);
2374 /* Need 1/2 descriptor for RXOR operation, and
2375 * need (src_cnt - (2 or 3)) for WXOR of sources
2380 if (flags & DMA_PREP_ZERO_P)
2381 set_bit(PPC440SPE_ZERO_P, &op);
2382 if (flags & DMA_PREP_ZERO_Q)
2383 set_bit(PPC440SPE_ZERO_Q, &op);
2385 if (test_bit(PPC440SPE_DESC_RXOR12, &op))
2386 slot_cnt += src_cnt - 2;
2388 slot_cnt += src_cnt - 3;
2390 /* Thus we have either RXOR only chain or
2393 if (slot_cnt == dst_cnt)
2394 /* RXOR only chain */
2395 clear_bit(PPC440SPE_DESC_WXOR, &op);
2398 spin_lock_bh(&ppc440spe_chan->lock);
2399 /* for both RXOR/WXOR each descriptor occupies one slot */
2400 sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
2402 ppc440spe_desc_init_dma01pq(sw_desc, dst_cnt, src_cnt,
2405 /* setup dst/src/mult */
2406 pr_debug("%s: set dst descriptor 0, 1: 0x%016llx, 0x%016llx\n",
2407 __func__, dst[0], dst[1]);
2408 ppc440spe_adma_pq_set_dest(sw_desc, dst, flags);
2410 ppc440spe_adma_pq_set_src(sw_desc, src[src_cnt],
2413 /* NOTE: "Multi = 0 is equivalent to = 1" as it
2414 * stated in 440SPSPe_RAID6_Addendum_UM_1_17.pdf
2415 * doesn't work for RXOR with DMA0/1! Instead, multi=0
2416 * leads to zeroing source data after RXOR.
2417 * So, for P case set-up mult=1 explicitly.
2419 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
2420 mult = scf[src_cnt];
2421 ppc440spe_adma_pq_set_src_mult(sw_desc,
2422 mult, src_cnt, dst_cnt - 1);
2425 /* Setup byte count foreach slot just allocated */
2426 sw_desc->async_tx.flags = flags;
2427 list_for_each_entry(iter, &sw_desc->group_list,
2429 ppc440spe_desc_set_byte_count(iter,
2430 ppc440spe_chan, len);
2431 iter->unmap_len = len;
2434 spin_unlock_bh(&ppc440spe_chan->lock);
2439 static struct ppc440spe_adma_desc_slot *ppc440spe_dma2_prep_pq(
2440 struct ppc440spe_adma_chan *ppc440spe_chan,
2441 dma_addr_t *dst, int dst_cnt, dma_addr_t *src, int src_cnt,
2442 const unsigned char *scf, size_t len, unsigned long flags)
2444 int slot_cnt, descs_per_op;
2445 struct ppc440spe_adma_desc_slot *sw_desc = NULL, *iter;
2446 unsigned long op = 0;
2447 unsigned char mult = 1;
2450 /*pr_debug("%s: dst_cnt %d, src_cnt %d, len %d\n",
2451 __func__, dst_cnt, src_cnt, len);*/
2453 spin_lock_bh(&ppc440spe_chan->lock);
2454 descs_per_op = ppc440spe_dma2_pq_slot_count(src, src_cnt, len);
2455 if (descs_per_op < 0) {
2456 spin_unlock_bh(&ppc440spe_chan->lock);
2460 /* depending on number of sources we have 1 or 2 RXOR chains */
2461 slot_cnt = descs_per_op * dst_cnt;
2463 sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
2466 sw_desc->async_tx.flags = flags;
2467 list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
2468 ppc440spe_desc_init_dma2pq(iter, dst_cnt, src_cnt,
2470 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
2472 iter->unmap_len = len;
2474 ppc440spe_init_rxor_cursor(&(iter->rxor_cursor));
2475 iter->rxor_cursor.len = len;
2476 iter->descs_per_op = descs_per_op;
2479 list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
2481 if (op % descs_per_op == 0)
2482 ppc440spe_adma_init_dma2rxor_slot(iter, src,
2484 if (likely(!list_is_last(&iter->chain_node,
2485 &sw_desc->group_list))) {
2486 /* set 'next' pointer */
2488 list_entry(iter->chain_node.next,
2489 struct ppc440spe_adma_desc_slot,
2491 ppc440spe_xor_set_link(iter, iter->hw_next);
2493 /* this is the last descriptor. */
2494 iter->hw_next = NULL;
2498 /* fixup head descriptor */
2499 sw_desc->dst_cnt = dst_cnt;
2500 if (flags & DMA_PREP_ZERO_P)
2501 set_bit(PPC440SPE_ZERO_P, &sw_desc->flags);
2502 if (flags & DMA_PREP_ZERO_Q)
2503 set_bit(PPC440SPE_ZERO_Q, &sw_desc->flags);
2505 /* setup dst/src/mult */
2506 ppc440spe_adma_pq_set_dest(sw_desc, dst, flags);
2509 /* handle descriptors (if dst_cnt == 2) inside
2510 * the ppc440spe_adma_pq_set_srcxxx() functions
2512 ppc440spe_adma_pq_set_src(sw_desc, src[src_cnt],
2514 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
2515 mult = scf[src_cnt];
2516 ppc440spe_adma_pq_set_src_mult(sw_desc,
2517 mult, src_cnt, dst_cnt - 1);
2520 spin_unlock_bh(&ppc440spe_chan->lock);
2521 ppc440spe_desc_set_rxor_block_size(len);
2526 * ppc440spe_adma_prep_dma_pq - prepare CDB (group) for a GF-XOR operation
2528 static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_pq(
2529 struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
2530 unsigned int src_cnt, const unsigned char *scf,
2531 size_t len, unsigned long flags)
2533 struct ppc440spe_adma_chan *ppc440spe_chan;
2534 struct ppc440spe_adma_desc_slot *sw_desc = NULL;
2537 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2539 ADMA_LL_DBG(prep_dma_pq_dbg(ppc440spe_chan->device->id,
2540 dst, src, src_cnt));
2542 BUG_ON(len > PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
2545 if (src_cnt == 1 && dst[1] == src[0]) {
2548 /* dst[1] is real destination (Q) */
2550 /* this is the page to multicast source data to */
2551 dest[1] = ppc440spe_chan->qdest;
2552 sw_desc = ppc440spe_dma01_prep_mult(ppc440spe_chan,
2553 dest, 2, src, src_cnt, scf, len, flags);
2554 return sw_desc ? &sw_desc->async_tx : NULL;
2557 if (src_cnt == 2 && dst[1] == src[1]) {
2558 sw_desc = ppc440spe_dma01_prep_sum_product(ppc440spe_chan,
2559 &dst[1], src, 2, scf, len, flags);
2560 return sw_desc ? &sw_desc->async_tx : NULL;
2563 if (!(flags & DMA_PREP_PQ_DISABLE_P)) {
2566 flags |= DMA_PREP_ZERO_P;
2569 if (!(flags & DMA_PREP_PQ_DISABLE_Q)) {
2572 flags |= DMA_PREP_ZERO_Q;
2577 dev_dbg(ppc440spe_chan->device->common.dev,
2578 "ppc440spe adma%d: %s src_cnt: %d len: %u int_en: %d\n",
2579 ppc440spe_chan->device->id, __func__, src_cnt, len,
2580 flags & DMA_PREP_INTERRUPT ? 1 : 0);
2582 switch (ppc440spe_chan->device->id) {
2583 case PPC440SPE_DMA0_ID:
2584 case PPC440SPE_DMA1_ID:
2585 sw_desc = ppc440spe_dma01_prep_pq(ppc440spe_chan,
2586 dst, dst_cnt, src, src_cnt, scf,
2590 case PPC440SPE_XOR_ID:
2591 sw_desc = ppc440spe_dma2_prep_pq(ppc440spe_chan,
2592 dst, dst_cnt, src, src_cnt, scf,
2597 return sw_desc ? &sw_desc->async_tx : NULL;
2601 * ppc440spe_adma_prep_dma_pqzero_sum - prepare CDB group for
2602 * a PQ_ZERO_SUM operation
2604 static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_pqzero_sum(
2605 struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
2606 unsigned int src_cnt, const unsigned char *scf, size_t len,
2607 enum sum_check_flags *pqres, unsigned long flags)
2609 struct ppc440spe_adma_chan *ppc440spe_chan;
2610 struct ppc440spe_adma_desc_slot *sw_desc, *iter;
2611 dma_addr_t pdest, qdest;
2612 int slot_cnt, slots_per_op, idst, dst_cnt;
2614 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2616 if (flags & DMA_PREP_PQ_DISABLE_P)
2621 if (flags & DMA_PREP_PQ_DISABLE_Q)
2626 ADMA_LL_DBG(prep_dma_pqzero_sum_dbg(ppc440spe_chan->device->id,
2627 src, src_cnt, scf));
2629 /* Always use WXOR for P/Q calculations (two destinations).
2630 * Need 1 or 2 extra slots to verify results are zero.
2632 idst = dst_cnt = (pdest && qdest) ? 2 : 1;
2634 /* One additional slot per destination to clone P/Q
2635 * before calculation (we have to preserve destinations).
2637 slot_cnt = src_cnt + dst_cnt * 2;
2640 spin_lock_bh(&ppc440spe_chan->lock);
2641 sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
2644 ppc440spe_desc_init_dma01pqzero_sum(sw_desc, dst_cnt, src_cnt);
2646 /* Setup byte count for each slot just allocated */
2647 sw_desc->async_tx.flags = flags;
2648 list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
2649 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
2651 iter->unmap_len = len;
2655 struct dma_cdb *hw_desc;
2656 struct ppc440spe_adma_chan *chan;
2658 iter = sw_desc->group_head;
2659 chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
2660 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2661 iter->hw_next = list_entry(iter->chain_node.next,
2662 struct ppc440spe_adma_desc_slot,
2664 hw_desc = iter->hw_desc;
2665 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
2668 ppc440spe_desc_set_dest_addr(iter, chan, 0,
2669 ppc440spe_chan->pdest, 0);
2670 ppc440spe_desc_set_src_addr(iter, chan, 0, 0, pdest);
2671 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
2673 iter->unmap_len = 0;
2674 /* override pdest to preserve original P */
2675 pdest = ppc440spe_chan->pdest;
2678 struct dma_cdb *hw_desc;
2679 struct ppc440spe_adma_chan *chan;
2681 iter = list_first_entry(&sw_desc->group_list,
2682 struct ppc440spe_adma_desc_slot,
2684 chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
2687 iter = list_entry(iter->chain_node.next,
2688 struct ppc440spe_adma_desc_slot,
2692 memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2693 iter->hw_next = list_entry(iter->chain_node.next,
2694 struct ppc440spe_adma_desc_slot,
2696 hw_desc = iter->hw_desc;
2697 hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
2700 ppc440spe_desc_set_dest_addr(iter, chan, 0,
2701 ppc440spe_chan->qdest, 0);
2702 ppc440spe_desc_set_src_addr(iter, chan, 0, 0, qdest);
2703 ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
2705 iter->unmap_len = 0;
2706 /* override qdest to preserve original Q */
2707 qdest = ppc440spe_chan->qdest;
2710 /* Setup destinations for P/Q ops */
2711 ppc440spe_adma_pqzero_sum_set_dest(sw_desc, pdest, qdest);
2713 /* Setup zero QWORDs into DCHECK CDBs */
2715 list_for_each_entry_reverse(iter, &sw_desc->group_list,
2718 * The last CDB corresponds to Q-parity check,
2719 * the one before last CDB corresponds
2722 if (idst == DMA_DEST_MAX_NUM) {
2723 if (idst == dst_cnt) {
2724 set_bit(PPC440SPE_DESC_QCHECK,
2727 set_bit(PPC440SPE_DESC_PCHECK,
2732 set_bit(PPC440SPE_DESC_QCHECK,
2735 set_bit(PPC440SPE_DESC_PCHECK,
2739 iter->xor_check_result = pqres;
2742 * set it to zero, if check fail then result will
2745 *iter->xor_check_result = 0;
2746 ppc440spe_desc_set_dcheck(iter, ppc440spe_chan,
2753 /* Setup sources and mults for P/Q ops */
2754 list_for_each_entry_continue_reverse(iter, &sw_desc->group_list,
2756 struct ppc440spe_adma_chan *chan;
2759 chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
2760 ppc440spe_desc_set_src_addr(iter, chan, 0,
2764 mult_dst = (dst_cnt - 1) ? DMA_CDB_SG_DST2 :
2766 ppc440spe_desc_set_src_mult(iter, chan,
2775 spin_unlock_bh(&ppc440spe_chan->lock);
2776 return sw_desc ? &sw_desc->async_tx : NULL;
2780 * ppc440spe_adma_prep_dma_xor_zero_sum - prepare CDB group for
2781 * XOR ZERO_SUM operation
2783 static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_xor_zero_sum(
2784 struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
2785 size_t len, enum sum_check_flags *result, unsigned long flags)
2787 struct dma_async_tx_descriptor *tx;
2790 /* validate P, disable Q */
2793 flags |= DMA_PREP_PQ_DISABLE_Q;
2795 tx = ppc440spe_adma_prep_dma_pqzero_sum(chan, pq, &src[1],
2796 src_cnt - 1, 0, len,
2802 * ppc440spe_adma_set_dest - set destination address into descriptor
2804 static void ppc440spe_adma_set_dest(struct ppc440spe_adma_desc_slot *sw_desc,
2805 dma_addr_t addr, int index)
2807 struct ppc440spe_adma_chan *chan;
2809 BUG_ON(index >= sw_desc->dst_cnt);
2811 chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
2813 switch (chan->device->id) {
2814 case PPC440SPE_DMA0_ID:
2815 case PPC440SPE_DMA1_ID:
2816 /* to do: support transfers lengths >
2817 * PPC440SPE_ADMA_DMA/XOR_MAX_BYTE_COUNT
2819 ppc440spe_desc_set_dest_addr(sw_desc->group_head,
2820 chan, 0, addr, index);
2822 case PPC440SPE_XOR_ID:
2823 sw_desc = ppc440spe_get_group_entry(sw_desc, index);
2824 ppc440spe_desc_set_dest_addr(sw_desc,
2825 chan, 0, addr, index);
2830 static void ppc440spe_adma_pq_zero_op(struct ppc440spe_adma_desc_slot *iter,
2831 struct ppc440spe_adma_chan *chan, dma_addr_t addr)
2833 /* To clear destinations update the descriptor
2834 * (P or Q depending on index) as follows:
2835 * addr is destination (0 corresponds to SG2):
2837 ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE, addr, 0);
2839 /* ... and the addr is source: */
2840 ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB, addr);
2842 /* addr is always SG2 then the mult is always DST1 */
2843 ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
2844 DMA_CDB_SG_DST1, 1);
2848 * ppc440spe_adma_pq_set_dest - set destination address into descriptor
2849 * for the PQXOR operation
2851 static void ppc440spe_adma_pq_set_dest(struct ppc440spe_adma_desc_slot *sw_desc,
2852 dma_addr_t *addrs, unsigned long flags)
2854 struct ppc440spe_adma_desc_slot *iter;
2855 struct ppc440spe_adma_chan *chan;
2856 dma_addr_t paddr, qaddr;
2857 dma_addr_t addr = 0, ppath, qpath;
2860 chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
2862 if (flags & DMA_PREP_PQ_DISABLE_P)
2867 if (flags & DMA_PREP_PQ_DISABLE_Q)
2872 if (!paddr || !qaddr)
2873 addr = paddr ? paddr : qaddr;
2875 switch (chan->device->id) {
2876 case PPC440SPE_DMA0_ID:
2877 case PPC440SPE_DMA1_ID:
2878 /* walk through the WXOR source list and set P/Q-destinations
2881 if (!test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
2882 /* This is WXOR-only chain; may have 1/2 zero descs */
2883 if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
2885 if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
2888 iter = ppc440spe_get_group_entry(sw_desc, index);
2890 /* one destination */
2891 list_for_each_entry_from(iter,
2892 &sw_desc->group_list, chain_node)
2893 ppc440spe_desc_set_dest_addr(iter, chan,
2894 DMA_CUED_XOR_BASE, addr, 0);
2896 /* two destinations */
2897 list_for_each_entry_from(iter,
2898 &sw_desc->group_list, chain_node) {
2899 ppc440spe_desc_set_dest_addr(iter, chan,
2900 DMA_CUED_XOR_BASE, paddr, 0);
2901 ppc440spe_desc_set_dest_addr(iter, chan,
2902 DMA_CUED_XOR_BASE, qaddr, 1);
2907 /* To clear destinations update the descriptor
2908 * (1st,2nd, or both depending on flags)
2911 if (test_bit(PPC440SPE_ZERO_P,
2913 iter = ppc440spe_get_group_entry(
2915 ppc440spe_adma_pq_zero_op(iter, chan,
2919 if (test_bit(PPC440SPE_ZERO_Q,
2921 iter = ppc440spe_get_group_entry(
2923 ppc440spe_adma_pq_zero_op(iter, chan,
2930 /* This is RXOR-only or RXOR/WXOR mixed chain */
2932 /* If we want to include destination into calculations,
2933 * then make dest addresses cued with mult=1 (XOR).
2935 ppath = test_bit(PPC440SPE_ZERO_P, &sw_desc->flags) ?
2938 (1 << DMA_CUED_MULT1_OFF);
2939 qpath = test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags) ?
2942 (1 << DMA_CUED_MULT1_OFF);
2944 /* Setup destination(s) in RXOR slot(s) */
2945 iter = ppc440spe_get_group_entry(sw_desc, index++);
2946 ppc440spe_desc_set_dest_addr(iter, chan,
2947 paddr ? ppath : qpath,
2948 paddr ? paddr : qaddr, 0);
2950 /* two destinations */
2951 iter = ppc440spe_get_group_entry(sw_desc,
2953 ppc440spe_desc_set_dest_addr(iter, chan,
2957 if (test_bit(PPC440SPE_DESC_WXOR, &sw_desc->flags)) {
2958 /* Setup destination(s) in remaining WXOR
2961 iter = ppc440spe_get_group_entry(sw_desc,
2964 /* one destination */
2965 list_for_each_entry_from(iter,
2966 &sw_desc->group_list,
2968 ppc440spe_desc_set_dest_addr(
2974 /* two destinations */
2975 list_for_each_entry_from(iter,
2976 &sw_desc->group_list,
2978 ppc440spe_desc_set_dest_addr(
2982 ppc440spe_desc_set_dest_addr(
2993 case PPC440SPE_XOR_ID:
2994 /* DMA2 descriptors have only 1 destination, so there are
2995 * two chains - one for each dest.
2996 * If we want to include destination into calculations,
2997 * then make dest addresses cued with mult=1 (XOR).
2999 ppath = test_bit(PPC440SPE_ZERO_P, &sw_desc->flags) ?
3002 (1 << DMA_CUED_MULT1_OFF);
3004 qpath = test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags) ?
3007 (1 << DMA_CUED_MULT1_OFF);
3009 iter = ppc440spe_get_group_entry(sw_desc, 0);
3010 for (i = 0; i < sw_desc->descs_per_op; i++) {
3011 ppc440spe_desc_set_dest_addr(iter, chan,
3012 paddr ? ppath : qpath,
3013 paddr ? paddr : qaddr, 0);
3014 iter = list_entry(iter->chain_node.next,
3015 struct ppc440spe_adma_desc_slot,
3020 /* Two destinations; setup Q here */
3021 iter = ppc440spe_get_group_entry(sw_desc,
3022 sw_desc->descs_per_op);
3023 for (i = 0; i < sw_desc->descs_per_op; i++) {
3024 ppc440spe_desc_set_dest_addr(iter,
3025 chan, qpath, qaddr, 0);
3026 iter = list_entry(iter->chain_node.next,
3027 struct ppc440spe_adma_desc_slot,
3037 * ppc440spe_adma_pq_zero_sum_set_dest - set destination address into descriptor
3038 * for the PQ_ZERO_SUM operation
3040 static void ppc440spe_adma_pqzero_sum_set_dest(
3041 struct ppc440spe_adma_desc_slot *sw_desc,
3042 dma_addr_t paddr, dma_addr_t qaddr)
3044 struct ppc440spe_adma_desc_slot *iter, *end;
3045 struct ppc440spe_adma_chan *chan;
3046 dma_addr_t addr = 0;
3049 chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3051 /* walk through the WXOR source list and set P/Q-destinations
3054 idx = (paddr && qaddr) ? 2 : 1;
3056 list_for_each_entry_reverse(end, &sw_desc->group_list,
3062 idx = (paddr && qaddr) ? 2 : 1;
3063 iter = ppc440spe_get_group_entry(sw_desc, idx);
3065 if (paddr && qaddr) {
3066 /* two destinations */
3067 list_for_each_entry_from(iter, &sw_desc->group_list,
3069 if (unlikely(iter == end))
3071 ppc440spe_desc_set_dest_addr(iter, chan,
3072 DMA_CUED_XOR_BASE, paddr, 0);
3073 ppc440spe_desc_set_dest_addr(iter, chan,
3074 DMA_CUED_XOR_BASE, qaddr, 1);
3077 /* one destination */
3078 addr = paddr ? paddr : qaddr;
3079 list_for_each_entry_from(iter, &sw_desc->group_list,
3081 if (unlikely(iter == end))
3083 ppc440spe_desc_set_dest_addr(iter, chan,
3084 DMA_CUED_XOR_BASE, addr, 0);
3088 /* The remaining descriptors are DATACHECK. These have no need in
3089 * destination. Actually, these destinations are used there
3090 * as sources for check operation. So, set addr as source.
3092 ppc440spe_desc_set_src_addr(end, chan, 0, 0, addr ? addr : paddr);
3095 end = list_entry(end->chain_node.next,
3096 struct ppc440spe_adma_desc_slot, chain_node);
3097 ppc440spe_desc_set_src_addr(end, chan, 0, 0, qaddr);
3102 * ppc440spe_desc_set_xor_src_cnt - set source count into descriptor
3104 static inline void ppc440spe_desc_set_xor_src_cnt(
3105 struct ppc440spe_adma_desc_slot *desc,
3108 struct xor_cb *hw_desc = desc->hw_desc;
3110 hw_desc->cbc &= ~XOR_CDCR_OAC_MSK;
3111 hw_desc->cbc |= src_cnt;
3115 * ppc440spe_adma_pq_set_src - set source address into descriptor
3117 static void ppc440spe_adma_pq_set_src(struct ppc440spe_adma_desc_slot *sw_desc,
3118 dma_addr_t addr, int index)
3120 struct ppc440spe_adma_chan *chan;
3121 dma_addr_t haddr = 0;
3122 struct ppc440spe_adma_desc_slot *iter = NULL;
3124 chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3126 switch (chan->device->id) {
3127 case PPC440SPE_DMA0_ID:
3128 case PPC440SPE_DMA1_ID:
3129 /* DMA0,1 may do: WXOR, RXOR, RXOR+WXORs chain
3131 if (test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
3132 /* RXOR-only or RXOR/WXOR operation */
3133 int iskip = test_bit(PPC440SPE_DESC_RXOR12,
3134 &sw_desc->flags) ? 2 : 3;
3137 /* 1st slot (RXOR) */
3138 /* setup sources region (R1-2-3, R1-2-4,
3141 if (test_bit(PPC440SPE_DESC_RXOR12,
3143 haddr = DMA_RXOR12 <<
3144 DMA_CUED_REGION_OFF;
3145 else if (test_bit(PPC440SPE_DESC_RXOR123,
3147 haddr = DMA_RXOR123 <<
3148 DMA_CUED_REGION_OFF;
3149 else if (test_bit(PPC440SPE_DESC_RXOR124,
3151 haddr = DMA_RXOR124 <<
3152 DMA_CUED_REGION_OFF;
3153 else if (test_bit(PPC440SPE_DESC_RXOR125,
3155 haddr = DMA_RXOR125 <<
3156 DMA_CUED_REGION_OFF;
3159 haddr |= DMA_CUED_XOR_BASE;
3160 iter = ppc440spe_get_group_entry(sw_desc, 0);
3161 } else if (index < iskip) {
3163 * shall actually set source address only once
3164 * instead of first <iskip>
3168 /* 2nd/3d and next slots (WXOR);
3169 * skip first slot with RXOR
3171 haddr = DMA_CUED_XOR_HB;
3172 iter = ppc440spe_get_group_entry(sw_desc,
3173 index - iskip + sw_desc->dst_cnt);
3178 /* WXOR-only operation; skip first slots with
3179 * zeroing destinations
3181 if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
3183 if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
3186 haddr = DMA_CUED_XOR_HB;
3187 iter = ppc440spe_get_group_entry(sw_desc,
3192 ppc440spe_desc_set_src_addr(iter, chan, 0, haddr, addr);
3195 test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags) &&
3196 sw_desc->dst_cnt == 2) {
3197 /* if we have two destinations for RXOR, then
3198 * setup source in the second descr too
3200 iter = ppc440spe_get_group_entry(sw_desc, 1);
3201 ppc440spe_desc_set_src_addr(iter, chan, 0,
3207 case PPC440SPE_XOR_ID:
3208 /* DMA2 may do Biskup */
3209 iter = sw_desc->group_head;
3210 if (iter->dst_cnt == 2) {
3211 /* both P & Q calculations required; set P src here */
3212 ppc440spe_adma_dma2rxor_set_src(iter, index, addr);
3215 iter = ppc440spe_get_group_entry(sw_desc,
3216 sw_desc->descs_per_op);
3218 ppc440spe_adma_dma2rxor_set_src(iter, index, addr);
3224 * ppc440spe_adma_memcpy_xor_set_src - set source address into descriptor
3226 static void ppc440spe_adma_memcpy_xor_set_src(
3227 struct ppc440spe_adma_desc_slot *sw_desc,
3228 dma_addr_t addr, int index)
3230 struct ppc440spe_adma_chan *chan;
3232 chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3233 sw_desc = sw_desc->group_head;
3235 if (likely(sw_desc))
3236 ppc440spe_desc_set_src_addr(sw_desc, chan, index, 0, addr);
3240 * ppc440spe_adma_dma2rxor_inc_addr -
3242 static void ppc440spe_adma_dma2rxor_inc_addr(
3243 struct ppc440spe_adma_desc_slot *desc,
3244 struct ppc440spe_rxor *cursor, int index, int src_cnt)
3246 cursor->addr_count++;
3247 if (index == src_cnt - 1) {
3248 ppc440spe_desc_set_xor_src_cnt(desc, cursor->addr_count);
3249 } else if (cursor->addr_count == XOR_MAX_OPS) {
3250 ppc440spe_desc_set_xor_src_cnt(desc, cursor->addr_count);
3251 cursor->addr_count = 0;
3252 cursor->desc_count++;
3257 * ppc440spe_adma_dma2rxor_prep_src - setup RXOR types in DMA2 CDB
3259 static int ppc440spe_adma_dma2rxor_prep_src(
3260 struct ppc440spe_adma_desc_slot *hdesc,
3261 struct ppc440spe_rxor *cursor, int index,
3262 int src_cnt, u32 addr)
3266 struct ppc440spe_adma_desc_slot *desc = hdesc;
3269 for (i = 0; i < cursor->desc_count; i++) {
3270 desc = list_entry(hdesc->chain_node.next,
3271 struct ppc440spe_adma_desc_slot,
3275 switch (cursor->state) {
3277 if (addr == cursor->addrl + cursor->len) {
3280 cursor->xor_count++;
3281 if (index == src_cnt-1) {
3282 ppc440spe_rxor_set_region(desc,
3284 DMA_RXOR12 << DMA_CUED_REGION_OFF);
3285 ppc440spe_adma_dma2rxor_inc_addr(
3286 desc, cursor, index, src_cnt);
3288 } else if (cursor->addrl == addr + cursor->len) {
3291 cursor->xor_count++;
3292 set_bit(cursor->addr_count, &desc->reverse_flags[0]);
3293 if (index == src_cnt-1) {
3294 ppc440spe_rxor_set_region(desc,
3296 DMA_RXOR12 << DMA_CUED_REGION_OFF);
3297 ppc440spe_adma_dma2rxor_inc_addr(
3298 desc, cursor, index, src_cnt);
3301 printk(KERN_ERR "Cannot build "
3302 "DMA2 RXOR command block.\n");
3307 sign = test_bit(cursor->addr_count,
3308 desc->reverse_flags)
3310 if (index == src_cnt-2 || (sign == -1
3311 && addr != cursor->addrl - 2*cursor->len)) {
3313 cursor->xor_count = 1;
3314 cursor->addrl = addr;
3315 ppc440spe_rxor_set_region(desc,
3317 DMA_RXOR12 << DMA_CUED_REGION_OFF);
3318 ppc440spe_adma_dma2rxor_inc_addr(
3319 desc, cursor, index, src_cnt);
3320 } else if (addr == cursor->addrl + 2*sign*cursor->len) {
3322 cursor->xor_count = 0;
3323 ppc440spe_rxor_set_region(desc,
3325 DMA_RXOR123 << DMA_CUED_REGION_OFF);
3326 if (index == src_cnt-1) {
3327 ppc440spe_adma_dma2rxor_inc_addr(
3328 desc, cursor, index, src_cnt);
3330 } else if (addr == cursor->addrl + 3*cursor->len) {
3332 cursor->xor_count = 0;
3333 ppc440spe_rxor_set_region(desc,
3335 DMA_RXOR124 << DMA_CUED_REGION_OFF);
3336 if (index == src_cnt-1) {
3337 ppc440spe_adma_dma2rxor_inc_addr(
3338 desc, cursor, index, src_cnt);
3340 } else if (addr == cursor->addrl + 4*cursor->len) {
3342 cursor->xor_count = 0;
3343 ppc440spe_rxor_set_region(desc,
3345 DMA_RXOR125 << DMA_CUED_REGION_OFF);
3346 if (index == src_cnt-1) {
3347 ppc440spe_adma_dma2rxor_inc_addr(
3348 desc, cursor, index, src_cnt);
3352 cursor->xor_count = 1;
3353 cursor->addrl = addr;
3354 ppc440spe_rxor_set_region(desc,
3356 DMA_RXOR12 << DMA_CUED_REGION_OFF);
3357 ppc440spe_adma_dma2rxor_inc_addr(
3358 desc, cursor, index, src_cnt);
3363 cursor->addrl = addr;
3364 cursor->xor_count++;
3366 ppc440spe_adma_dma2rxor_inc_addr(
3367 desc, cursor, index, src_cnt);
3376 * ppc440spe_adma_dma2rxor_set_src - set RXOR source address; it's assumed that
3377 * ppc440spe_adma_dma2rxor_prep_src() has already done prior this call
3379 static void ppc440spe_adma_dma2rxor_set_src(
3380 struct ppc440spe_adma_desc_slot *desc,
3381 int index, dma_addr_t addr)
3383 struct xor_cb *xcb = desc->hw_desc;
3384 int k = 0, op = 0, lop = 0;
3386 /* get the RXOR operand which corresponds to index addr */
3387 while (op <= index) {
3389 if (k == XOR_MAX_OPS) {
3391 desc = list_entry(desc->chain_node.next,
3392 struct ppc440spe_adma_desc_slot, chain_node);
3393 xcb = desc->hw_desc;
3396 if ((xcb->ops[k++].h & (DMA_RXOR12 << DMA_CUED_REGION_OFF)) ==
3397 (DMA_RXOR12 << DMA_CUED_REGION_OFF))
3405 if (test_bit(k-1, desc->reverse_flags)) {
3406 /* reverse operand order; put last op in RXOR group */
3407 if (index == op - 1)
3408 ppc440spe_rxor_set_src(desc, k - 1, addr);
3410 /* direct operand order; put first op in RXOR group */
3412 ppc440spe_rxor_set_src(desc, k - 1, addr);
3417 * ppc440spe_adma_dma2rxor_set_mult - set RXOR multipliers; it's assumed that
3418 * ppc440spe_adma_dma2rxor_prep_src() has already done prior this call
3420 static void ppc440spe_adma_dma2rxor_set_mult(
3421 struct ppc440spe_adma_desc_slot *desc,
3424 struct xor_cb *xcb = desc->hw_desc;
3425 int k = 0, op = 0, lop = 0;
3427 /* get the RXOR operand which corresponds to index mult */
3428 while (op <= index) {
3430 if (k == XOR_MAX_OPS) {
3432 desc = list_entry(desc->chain_node.next,
3433 struct ppc440spe_adma_desc_slot,
3435 xcb = desc->hw_desc;
3438 if ((xcb->ops[k++].h & (DMA_RXOR12 << DMA_CUED_REGION_OFF)) ==
3439 (DMA_RXOR12 << DMA_CUED_REGION_OFF))
3446 if (test_bit(k-1, desc->reverse_flags)) {
3448 ppc440spe_rxor_set_mult(desc, k - 1, op - index - 1, mult);
3451 ppc440spe_rxor_set_mult(desc, k - 1, index - lop, mult);
3456 * ppc440spe_init_rxor_cursor -
3458 static void ppc440spe_init_rxor_cursor(struct ppc440spe_rxor *cursor)
3460 memset(cursor, 0, sizeof(struct ppc440spe_rxor));
3465 * ppc440spe_adma_pq_set_src_mult - set multiplication coefficient into
3466 * descriptor for the PQXOR operation
3468 static void ppc440spe_adma_pq_set_src_mult(
3469 struct ppc440spe_adma_desc_slot *sw_desc,
3470 unsigned char mult, int index, int dst_pos)
3472 struct ppc440spe_adma_chan *chan;
3473 u32 mult_idx, mult_dst;
3474 struct ppc440spe_adma_desc_slot *iter = NULL, *iter1 = NULL;
3476 chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3478 switch (chan->device->id) {
3479 case PPC440SPE_DMA0_ID:
3480 case PPC440SPE_DMA1_ID:
3481 if (test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
3482 int region = test_bit(PPC440SPE_DESC_RXOR12,
3483 &sw_desc->flags) ? 2 : 3;
3485 if (index < region) {
3486 /* RXOR multipliers */
3487 iter = ppc440spe_get_group_entry(sw_desc,
3488 sw_desc->dst_cnt - 1);
3489 if (sw_desc->dst_cnt == 2)
3490 iter1 = ppc440spe_get_group_entry(
3493 mult_idx = DMA_CUED_MULT1_OFF + (index << 3);
3494 mult_dst = DMA_CDB_SG_SRC;
3496 /* WXOR multiplier */
3497 iter = ppc440spe_get_group_entry(sw_desc,
3500 mult_idx = DMA_CUED_MULT1_OFF;
3501 mult_dst = dst_pos ? DMA_CDB_SG_DST2 :
3508 * skip first slots with destinations (if ZERO_DST has
3511 if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
3513 if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
3516 iter = ppc440spe_get_group_entry(sw_desc, index + znum);
3517 mult_idx = DMA_CUED_MULT1_OFF;
3518 mult_dst = dst_pos ? DMA_CDB_SG_DST2 : DMA_CDB_SG_DST1;
3522 ppc440spe_desc_set_src_mult(iter, chan,
3523 mult_idx, mult_dst, mult);
3525 if (unlikely(iter1)) {
3526 /* if we have two destinations for RXOR, then
3527 * we've just set Q mult. Set-up P now.
3529 ppc440spe_desc_set_src_mult(iter1, chan,
3530 mult_idx, mult_dst, 1);
3536 case PPC440SPE_XOR_ID:
3537 iter = sw_desc->group_head;
3538 if (sw_desc->dst_cnt == 2) {
3539 /* both P & Q calculations required; set P mult here */
3540 ppc440spe_adma_dma2rxor_set_mult(iter, index, 1);
3542 /* and then set Q mult */
3543 iter = ppc440spe_get_group_entry(sw_desc,
3544 sw_desc->descs_per_op);
3546 ppc440spe_adma_dma2rxor_set_mult(iter, index, mult);
3552 * ppc440spe_adma_free_chan_resources - free the resources allocated
3554 static void ppc440spe_adma_free_chan_resources(struct dma_chan *chan)
3556 struct ppc440spe_adma_chan *ppc440spe_chan;
3557 struct ppc440spe_adma_desc_slot *iter, *_iter;
3558 int in_use_descs = 0;
3560 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
3561 ppc440spe_adma_slot_cleanup(ppc440spe_chan);
3563 spin_lock_bh(&ppc440spe_chan->lock);
3564 list_for_each_entry_safe(iter, _iter, &ppc440spe_chan->chain,
3567 list_del(&iter->chain_node);
3569 list_for_each_entry_safe_reverse(iter, _iter,
3570 &ppc440spe_chan->all_slots, slot_node) {
3571 list_del(&iter->slot_node);
3573 ppc440spe_chan->slots_allocated--;
3575 ppc440spe_chan->last_used = NULL;
3577 dev_dbg(ppc440spe_chan->device->common.dev,
3578 "ppc440spe adma%d %s slots_allocated %d\n",
3579 ppc440spe_chan->device->id,
3580 __func__, ppc440spe_chan->slots_allocated);
3581 spin_unlock_bh(&ppc440spe_chan->lock);
3583 /* one is ok since we left it on there on purpose */
3584 if (in_use_descs > 1)
3585 printk(KERN_ERR "SPE: Freeing %d in use descriptors!\n",
3590 * ppc440spe_adma_tx_status - poll the status of an ADMA transaction
3591 * @chan: ADMA channel handle
3592 * @cookie: ADMA transaction identifier
3593 * @txstate: a holder for the current state of the channel
3595 static enum dma_status ppc440spe_adma_tx_status(struct dma_chan *chan,
3596 dma_cookie_t cookie, struct dma_tx_state *txstate)
3598 struct ppc440spe_adma_chan *ppc440spe_chan;
3599 enum dma_status ret;
3601 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
3602 ret = dma_cookie_status(chan, cookie, txstate);
3603 if (ret == DMA_COMPLETE)
3606 ppc440spe_adma_slot_cleanup(ppc440spe_chan);
3608 return dma_cookie_status(chan, cookie, txstate);
3612 * ppc440spe_adma_eot_handler - end of transfer interrupt handler
3614 static irqreturn_t ppc440spe_adma_eot_handler(int irq, void *data)
3616 struct ppc440spe_adma_chan *chan = data;
3618 dev_dbg(chan->device->common.dev,
3619 "ppc440spe adma%d: %s\n", chan->device->id, __func__);
3621 tasklet_schedule(&chan->irq_tasklet);
3622 ppc440spe_adma_device_clear_eot_status(chan);
3628 * ppc440spe_adma_err_handler - DMA error interrupt handler;
3629 * do the same things as a eot handler
3631 static irqreturn_t ppc440spe_adma_err_handler(int irq, void *data)
3633 struct ppc440spe_adma_chan *chan = data;
3635 dev_dbg(chan->device->common.dev,
3636 "ppc440spe adma%d: %s\n", chan->device->id, __func__);
3638 tasklet_schedule(&chan->irq_tasklet);
3639 ppc440spe_adma_device_clear_eot_status(chan);
3645 * ppc440spe_test_callback - called when test operation has been done
3647 static void ppc440spe_test_callback(void *unused)
3649 complete(&ppc440spe_r6_test_comp);
3653 * ppc440spe_adma_issue_pending - flush all pending descriptors to h/w
3655 static void ppc440spe_adma_issue_pending(struct dma_chan *chan)
3657 struct ppc440spe_adma_chan *ppc440spe_chan;
3659 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
3660 dev_dbg(ppc440spe_chan->device->common.dev,
3661 "ppc440spe adma%d: %s %d \n", ppc440spe_chan->device->id,
3662 __func__, ppc440spe_chan->pending);
3664 if (ppc440spe_chan->pending) {
3665 ppc440spe_chan->pending = 0;
3666 ppc440spe_chan_append(ppc440spe_chan);
3671 * ppc440spe_chan_start_null_xor - initiate the first XOR operation (DMA engines
3672 * use FIFOs (as opposite to chains used in XOR) so this is a XOR
3673 * specific operation)
3675 static void ppc440spe_chan_start_null_xor(struct ppc440spe_adma_chan *chan)
3677 struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
3678 dma_cookie_t cookie;
3679 int slot_cnt, slots_per_op;
3681 dev_dbg(chan->device->common.dev,
3682 "ppc440spe adma%d: %s\n", chan->device->id, __func__);
3684 spin_lock_bh(&chan->lock);
3685 slot_cnt = ppc440spe_chan_xor_slot_count(0, 2, &slots_per_op);
3686 sw_desc = ppc440spe_adma_alloc_slots(chan, slot_cnt, slots_per_op);
3688 group_start = sw_desc->group_head;
3689 list_splice_init(&sw_desc->group_list, &chan->chain);
3690 async_tx_ack(&sw_desc->async_tx);
3691 ppc440spe_desc_init_null_xor(group_start);
3693 cookie = dma_cookie_assign(&sw_desc->async_tx);
3695 /* initialize the completed cookie to be less than
3696 * the most recently used cookie
3698 chan->common.completed_cookie = cookie - 1;
3700 /* channel should not be busy */
3701 BUG_ON(ppc440spe_chan_is_busy(chan));
3703 /* set the descriptor address */
3704 ppc440spe_chan_set_first_xor_descriptor(chan, sw_desc);
3706 /* run the descriptor */
3707 ppc440spe_chan_run(chan);
3709 printk(KERN_ERR "ppc440spe adma%d"
3710 " failed to allocate null descriptor\n",
3712 spin_unlock_bh(&chan->lock);
3716 * ppc440spe_test_raid6 - test are RAID-6 capabilities enabled successfully.
3717 * For this we just perform one WXOR operation with the same source
3718 * and destination addresses, the GF-multiplier is 1; so if RAID-6
3719 * capabilities are enabled then we'll get src/dst filled with zero.
3721 static int ppc440spe_test_raid6(struct ppc440spe_adma_chan *chan)
3723 struct ppc440spe_adma_desc_slot *sw_desc, *iter;
3726 dma_addr_t dma_addr, addrs[2];
3727 unsigned long op = 0;
3730 set_bit(PPC440SPE_DESC_WXOR, &op);
3732 pg = alloc_page(GFP_KERNEL);
3736 spin_lock_bh(&chan->lock);
3737 sw_desc = ppc440spe_adma_alloc_slots(chan, 1, 1);
3739 /* 1 src, 1 dsr, int_ena, WXOR */
3740 ppc440spe_desc_init_dma01pq(sw_desc, 1, 1, 1, op);
3741 list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
3742 ppc440spe_desc_set_byte_count(iter, chan, PAGE_SIZE);
3743 iter->unmap_len = PAGE_SIZE;
3747 spin_unlock_bh(&chan->lock);
3750 spin_unlock_bh(&chan->lock);
3752 /* Fill the test page with ones */
3753 memset(page_address(pg), 0xFF, PAGE_SIZE);
3754 dma_addr = dma_map_page(chan->device->dev, pg, 0,
3755 PAGE_SIZE, DMA_BIDIRECTIONAL);
3757 /* Setup addresses */
3758 ppc440spe_adma_pq_set_src(sw_desc, dma_addr, 0);
3759 ppc440spe_adma_pq_set_src_mult(sw_desc, 1, 0, 0);
3760 addrs[0] = dma_addr;
3762 ppc440spe_adma_pq_set_dest(sw_desc, addrs, DMA_PREP_PQ_DISABLE_Q);
3764 async_tx_ack(&sw_desc->async_tx);
3765 sw_desc->async_tx.callback = ppc440spe_test_callback;
3766 sw_desc->async_tx.callback_param = NULL;
3768 init_completion(&ppc440spe_r6_test_comp);
3770 ppc440spe_adma_tx_submit(&sw_desc->async_tx);
3771 ppc440spe_adma_issue_pending(&chan->common);
3773 wait_for_completion(&ppc440spe_r6_test_comp);
3775 /* Now check if the test page is zeroed */
3776 a = page_address(pg);
3777 if ((*(u32 *)a) == 0 && memcmp(a, a+4, PAGE_SIZE-4) == 0) {
3778 /* page is zero - RAID-6 enabled */
3781 /* RAID-6 was not enabled */
3789 static void ppc440spe_adma_init_capabilities(struct ppc440spe_adma_device *adev)
3792 case PPC440SPE_DMA0_ID:
3793 case PPC440SPE_DMA1_ID:
3794 dma_cap_set(DMA_MEMCPY, adev->common.cap_mask);
3795 dma_cap_set(DMA_INTERRUPT, adev->common.cap_mask);
3796 dma_cap_set(DMA_PQ, adev->common.cap_mask);
3797 dma_cap_set(DMA_PQ_VAL, adev->common.cap_mask);
3798 dma_cap_set(DMA_XOR_VAL, adev->common.cap_mask);
3800 case PPC440SPE_XOR_ID:
3801 dma_cap_set(DMA_XOR, adev->common.cap_mask);
3802 dma_cap_set(DMA_PQ, adev->common.cap_mask);
3803 dma_cap_set(DMA_INTERRUPT, adev->common.cap_mask);
3804 adev->common.cap_mask = adev->common.cap_mask;
3808 /* Set base routines */
3809 adev->common.device_alloc_chan_resources =
3810 ppc440spe_adma_alloc_chan_resources;
3811 adev->common.device_free_chan_resources =
3812 ppc440spe_adma_free_chan_resources;
3813 adev->common.device_tx_status = ppc440spe_adma_tx_status;
3814 adev->common.device_issue_pending = ppc440spe_adma_issue_pending;
3816 /* Set prep routines based on capability */
3817 if (dma_has_cap(DMA_MEMCPY, adev->common.cap_mask)) {
3818 adev->common.device_prep_dma_memcpy =
3819 ppc440spe_adma_prep_dma_memcpy;
3821 if (dma_has_cap(DMA_XOR, adev->common.cap_mask)) {
3822 adev->common.max_xor = XOR_MAX_OPS;
3823 adev->common.device_prep_dma_xor =
3824 ppc440spe_adma_prep_dma_xor;
3826 if (dma_has_cap(DMA_PQ, adev->common.cap_mask)) {
3828 case PPC440SPE_DMA0_ID:
3829 dma_set_maxpq(&adev->common,
3830 DMA0_FIFO_SIZE / sizeof(struct dma_cdb), 0);
3832 case PPC440SPE_DMA1_ID:
3833 dma_set_maxpq(&adev->common,
3834 DMA1_FIFO_SIZE / sizeof(struct dma_cdb), 0);
3836 case PPC440SPE_XOR_ID:
3837 adev->common.max_pq = XOR_MAX_OPS * 3;
3840 adev->common.device_prep_dma_pq =
3841 ppc440spe_adma_prep_dma_pq;
3843 if (dma_has_cap(DMA_PQ_VAL, adev->common.cap_mask)) {
3845 case PPC440SPE_DMA0_ID:
3846 adev->common.max_pq = DMA0_FIFO_SIZE /
3847 sizeof(struct dma_cdb);
3849 case PPC440SPE_DMA1_ID:
3850 adev->common.max_pq = DMA1_FIFO_SIZE /
3851 sizeof(struct dma_cdb);
3854 adev->common.device_prep_dma_pq_val =
3855 ppc440spe_adma_prep_dma_pqzero_sum;
3857 if (dma_has_cap(DMA_XOR_VAL, adev->common.cap_mask)) {
3859 case PPC440SPE_DMA0_ID:
3860 adev->common.max_xor = DMA0_FIFO_SIZE /
3861 sizeof(struct dma_cdb);
3863 case PPC440SPE_DMA1_ID:
3864 adev->common.max_xor = DMA1_FIFO_SIZE /
3865 sizeof(struct dma_cdb);
3868 adev->common.device_prep_dma_xor_val =
3869 ppc440spe_adma_prep_dma_xor_zero_sum;
3871 if (dma_has_cap(DMA_INTERRUPT, adev->common.cap_mask)) {
3872 adev->common.device_prep_dma_interrupt =
3873 ppc440spe_adma_prep_dma_interrupt;
3875 pr_info("%s: AMCC(R) PPC440SP(E) ADMA Engine: "
3876 "( %s%s%s%s%s%s)\n",
3877 dev_name(adev->dev),
3878 dma_has_cap(DMA_PQ, adev->common.cap_mask) ? "pq " : "",
3879 dma_has_cap(DMA_PQ_VAL, adev->common.cap_mask) ? "pq_val " : "",
3880 dma_has_cap(DMA_XOR, adev->common.cap_mask) ? "xor " : "",
3881 dma_has_cap(DMA_XOR_VAL, adev->common.cap_mask) ? "xor_val " : "",
3882 dma_has_cap(DMA_MEMCPY, adev->common.cap_mask) ? "memcpy " : "",
3883 dma_has_cap(DMA_INTERRUPT, adev->common.cap_mask) ? "intr " : "");
3886 static int ppc440spe_adma_setup_irqs(struct ppc440spe_adma_device *adev,
3887 struct ppc440spe_adma_chan *chan,
3890 struct platform_device *ofdev;
3891 struct device_node *np;
3894 ofdev = container_of(adev->dev, struct platform_device, dev);
3895 np = ofdev->dev.of_node;
3896 if (adev->id != PPC440SPE_XOR_ID) {
3897 adev->err_irq = irq_of_parse_and_map(np, 1);
3898 if (adev->err_irq == NO_IRQ) {
3899 dev_warn(adev->dev, "no err irq resource?\n");
3900 *initcode = PPC_ADMA_INIT_IRQ2;
3901 adev->err_irq = -ENXIO;
3903 atomic_inc(&ppc440spe_adma_err_irq_ref);
3905 adev->err_irq = -ENXIO;
3908 adev->irq = irq_of_parse_and_map(np, 0);
3909 if (adev->irq == NO_IRQ) {
3910 dev_err(adev->dev, "no irq resource\n");
3911 *initcode = PPC_ADMA_INIT_IRQ1;
3915 dev_dbg(adev->dev, "irq %d, err irq %d\n",
3916 adev->irq, adev->err_irq);
3918 ret = request_irq(adev->irq, ppc440spe_adma_eot_handler,
3919 0, dev_driver_string(adev->dev), chan);
3921 dev_err(adev->dev, "can't request irq %d\n",
3923 *initcode = PPC_ADMA_INIT_IRQ1;
3928 /* only DMA engines have a separate error IRQ
3929 * so it's Ok if err_irq < 0 in XOR engine case.
3931 if (adev->err_irq > 0) {
3932 /* both DMA engines share common error IRQ */
3933 ret = request_irq(adev->err_irq,
3934 ppc440spe_adma_err_handler,
3936 dev_driver_string(adev->dev),
3939 dev_err(adev->dev, "can't request irq %d\n",
3941 *initcode = PPC_ADMA_INIT_IRQ2;
3947 if (adev->id == PPC440SPE_XOR_ID) {
3948 /* enable XOR engine interrupts */
3949 iowrite32be(XOR_IE_CBCIE_BIT | XOR_IE_ICBIE_BIT |
3950 XOR_IE_ICIE_BIT | XOR_IE_RPTIE_BIT,
3951 &adev->xor_reg->ier);
3955 np = of_find_compatible_node(NULL, NULL, "ibm,i2o-440spe");
3957 pr_err("%s: can't find I2O device tree node\n",
3962 adev->i2o_reg = of_iomap(np, 0);
3963 if (!adev->i2o_reg) {
3964 pr_err("%s: failed to map I2O registers\n", __func__);
3970 /* Unmask 'CS FIFO Attention' interrupts and
3971 * enable generating interrupts on errors
3973 enable = (adev->id == PPC440SPE_DMA0_ID) ?
3974 ~(I2O_IOPIM_P0SNE | I2O_IOPIM_P0EM) :
3975 ~(I2O_IOPIM_P1SNE | I2O_IOPIM_P1EM);
3976 mask = ioread32(&adev->i2o_reg->iopim) & enable;
3977 iowrite32(mask, &adev->i2o_reg->iopim);
3982 free_irq(adev->irq, chan);
3984 irq_dispose_mapping(adev->irq);
3986 if (adev->err_irq > 0) {
3987 if (atomic_dec_and_test(&ppc440spe_adma_err_irq_ref))
3988 irq_dispose_mapping(adev->err_irq);
3993 static void ppc440spe_adma_release_irqs(struct ppc440spe_adma_device *adev,
3994 struct ppc440spe_adma_chan *chan)
3998 if (adev->id == PPC440SPE_XOR_ID) {
3999 /* disable XOR engine interrupts */
4000 mask = ioread32be(&adev->xor_reg->ier);
4001 mask &= ~(XOR_IE_CBCIE_BIT | XOR_IE_ICBIE_BIT |
4002 XOR_IE_ICIE_BIT | XOR_IE_RPTIE_BIT);
4003 iowrite32be(mask, &adev->xor_reg->ier);
4005 /* disable DMAx engine interrupts */
4006 disable = (adev->id == PPC440SPE_DMA0_ID) ?
4007 (I2O_IOPIM_P0SNE | I2O_IOPIM_P0EM) :
4008 (I2O_IOPIM_P1SNE | I2O_IOPIM_P1EM);
4009 mask = ioread32(&adev->i2o_reg->iopim) | disable;
4010 iowrite32(mask, &adev->i2o_reg->iopim);
4012 free_irq(adev->irq, chan);
4013 irq_dispose_mapping(adev->irq);
4014 if (adev->err_irq > 0) {
4015 free_irq(adev->err_irq, chan);
4016 if (atomic_dec_and_test(&ppc440spe_adma_err_irq_ref)) {
4017 irq_dispose_mapping(adev->err_irq);
4018 iounmap(adev->i2o_reg);
4024 * ppc440spe_adma_probe - probe the asynch device
4026 static int ppc440spe_adma_probe(struct platform_device *ofdev)
4028 struct device_node *np = ofdev->dev.of_node;
4029 struct resource res;
4030 struct ppc440spe_adma_device *adev;
4031 struct ppc440spe_adma_chan *chan;
4032 struct ppc_dma_chan_ref *ref, *_ref;
4033 int ret = 0, initcode = PPC_ADMA_INIT_OK;
4039 if (of_device_is_compatible(np, "amcc,xor-accelerator")) {
4040 id = PPC440SPE_XOR_ID;
4041 /* As far as the XOR engine is concerned, it does not
4042 * use FIFOs but uses linked list. So there is no dependency
4043 * between pool size to allocate and the engine configuration.
4045 pool_size = PAGE_SIZE << 1;
4047 /* it is DMA0 or DMA1 */
4048 idx = of_get_property(np, "cell-index", &len);
4049 if (!idx || (len != sizeof(u32))) {
4050 dev_err(&ofdev->dev, "Device node %s has missing "
4051 "or invalid cell-index property\n",
4056 /* DMA0,1 engines use FIFO to maintain CDBs, so we
4057 * should allocate the pool accordingly to size of this
4058 * FIFO. Thus, the pool size depends on the FIFO depth:
4059 * how much CDBs pointers the FIFO may contain then so
4060 * much CDBs we should provide in the pool.
4063 * CDBs number = (DMA0_FIFO_SIZE >> 3);
4064 * Pool size = CDBs number * CDB size =
4065 * = (DMA0_FIFO_SIZE >> 3) << 5 = DMA0_FIFO_SIZE << 2.
4067 pool_size = (id == PPC440SPE_DMA0_ID) ?
4068 DMA0_FIFO_SIZE : DMA1_FIFO_SIZE;
4072 if (of_address_to_resource(np, 0, &res)) {
4073 dev_err(&ofdev->dev, "failed to get memory resource\n");
4074 initcode = PPC_ADMA_INIT_MEMRES;
4079 if (!request_mem_region(res.start, resource_size(&res),
4080 dev_driver_string(&ofdev->dev))) {
4081 dev_err(&ofdev->dev, "failed to request memory region %pR\n",
4083 initcode = PPC_ADMA_INIT_MEMREG;
4088 /* create a device */
4089 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
4091 dev_err(&ofdev->dev, "failed to allocate device\n");
4092 initcode = PPC_ADMA_INIT_ALLOC;
4094 goto err_adev_alloc;
4098 adev->pool_size = pool_size;
4099 /* allocate coherent memory for hardware descriptors */
4100 adev->dma_desc_pool_virt = dma_alloc_coherent(&ofdev->dev,
4101 adev->pool_size, &adev->dma_desc_pool,
4103 if (adev->dma_desc_pool_virt == NULL) {
4104 dev_err(&ofdev->dev, "failed to allocate %d bytes of coherent "
4105 "memory for hardware descriptors\n",
4107 initcode = PPC_ADMA_INIT_COHERENT;
4111 dev_dbg(&ofdev->dev, "allocated descriptor pool virt 0x%p phys 0x%llx\n",
4112 adev->dma_desc_pool_virt, (u64)adev->dma_desc_pool);
4114 regs = ioremap(res.start, resource_size(&res));
4116 dev_err(&ofdev->dev, "failed to ioremap regs!\n");
4117 goto err_regs_alloc;
4120 if (adev->id == PPC440SPE_XOR_ID) {
4121 adev->xor_reg = regs;
4123 iowrite32be(XOR_CRSR_XASR_BIT, &adev->xor_reg->crsr);
4124 iowrite32be(XOR_CRSR_64BA_BIT, &adev->xor_reg->crrr);
4126 size_t fifo_size = (adev->id == PPC440SPE_DMA0_ID) ?
4127 DMA0_FIFO_SIZE : DMA1_FIFO_SIZE;
4128 adev->dma_reg = regs;
4129 /* DMAx_FIFO_SIZE is defined in bytes,
4130 * <fsiz> - is defined in number of CDB pointers (8byte).
4131 * DMA FIFO Length = CSlength + CPlength, where
4132 * CSlength = CPlength = (fsiz + 1) * 8.
4134 iowrite32(DMA_FIFO_ENABLE | ((fifo_size >> 3) - 2),
4135 &adev->dma_reg->fsiz);
4136 /* Configure DMA engine */
4137 iowrite32(DMA_CFG_DXEPR_HP | DMA_CFG_DFMPP_HP | DMA_CFG_FALGN,
4138 &adev->dma_reg->cfg);
4140 iowrite32(~0, &adev->dma_reg->dsts);
4143 adev->dev = &ofdev->dev;
4144 adev->common.dev = &ofdev->dev;
4145 INIT_LIST_HEAD(&adev->common.channels);
4146 platform_set_drvdata(ofdev, adev);
4148 /* create a channel */
4149 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
4151 dev_err(&ofdev->dev, "can't allocate channel structure\n");
4152 initcode = PPC_ADMA_INIT_CHANNEL;
4154 goto err_chan_alloc;
4157 spin_lock_init(&chan->lock);
4158 INIT_LIST_HEAD(&chan->chain);
4159 INIT_LIST_HEAD(&chan->all_slots);
4160 chan->device = adev;
4161 chan->common.device = &adev->common;
4162 dma_cookie_init(&chan->common);
4163 list_add_tail(&chan->common.device_node, &adev->common.channels);
4164 tasklet_init(&chan->irq_tasklet, ppc440spe_adma_tasklet,
4165 (unsigned long)chan);
4167 /* allocate and map helper pages for async validation or
4168 * async_mult/async_sum_product operations on DMA0/1.
4170 if (adev->id != PPC440SPE_XOR_ID) {
4171 chan->pdest_page = alloc_page(GFP_KERNEL);
4172 chan->qdest_page = alloc_page(GFP_KERNEL);
4173 if (!chan->pdest_page ||
4174 !chan->qdest_page) {
4175 if (chan->pdest_page)
4176 __free_page(chan->pdest_page);
4177 if (chan->qdest_page)
4178 __free_page(chan->qdest_page);
4180 goto err_page_alloc;
4182 chan->pdest = dma_map_page(&ofdev->dev, chan->pdest_page, 0,
4183 PAGE_SIZE, DMA_BIDIRECTIONAL);
4184 chan->qdest = dma_map_page(&ofdev->dev, chan->qdest_page, 0,
4185 PAGE_SIZE, DMA_BIDIRECTIONAL);
4188 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
4190 ref->chan = &chan->common;
4191 INIT_LIST_HEAD(&ref->node);
4192 list_add_tail(&ref->node, &ppc440spe_adma_chan_list);
4194 dev_err(&ofdev->dev, "failed to allocate channel reference!\n");
4199 ret = ppc440spe_adma_setup_irqs(adev, chan, &initcode);
4203 ppc440spe_adma_init_capabilities(adev);
4205 ret = dma_async_device_register(&adev->common);
4207 initcode = PPC_ADMA_INIT_REGISTER;
4208 dev_err(&ofdev->dev, "failed to register dma device\n");
4215 ppc440spe_adma_release_irqs(adev, chan);
4217 list_for_each_entry_safe(ref, _ref, &ppc440spe_adma_chan_list, node) {
4218 if (chan == to_ppc440spe_adma_chan(ref->chan)) {
4219 list_del(&ref->node);
4224 if (adev->id != PPC440SPE_XOR_ID) {
4225 dma_unmap_page(&ofdev->dev, chan->pdest,
4226 PAGE_SIZE, DMA_BIDIRECTIONAL);
4227 dma_unmap_page(&ofdev->dev, chan->qdest,
4228 PAGE_SIZE, DMA_BIDIRECTIONAL);
4229 __free_page(chan->pdest_page);
4230 __free_page(chan->qdest_page);
4235 if (adev->id == PPC440SPE_XOR_ID)
4236 iounmap(adev->xor_reg);
4238 iounmap(adev->dma_reg);
4240 dma_free_coherent(adev->dev, adev->pool_size,
4241 adev->dma_desc_pool_virt,
4242 adev->dma_desc_pool);
4246 release_mem_region(res.start, resource_size(&res));
4248 if (id < PPC440SPE_ADMA_ENGINES_NUM)
4249 ppc440spe_adma_devices[id] = initcode;
4255 * ppc440spe_adma_remove - remove the asynch device
4257 static int ppc440spe_adma_remove(struct platform_device *ofdev)
4259 struct ppc440spe_adma_device *adev = platform_get_drvdata(ofdev);
4260 struct device_node *np = ofdev->dev.of_node;
4261 struct resource res;
4262 struct dma_chan *chan, *_chan;
4263 struct ppc_dma_chan_ref *ref, *_ref;
4264 struct ppc440spe_adma_chan *ppc440spe_chan;
4266 if (adev->id < PPC440SPE_ADMA_ENGINES_NUM)
4267 ppc440spe_adma_devices[adev->id] = -1;
4269 dma_async_device_unregister(&adev->common);
4271 list_for_each_entry_safe(chan, _chan, &adev->common.channels,
4273 ppc440spe_chan = to_ppc440spe_adma_chan(chan);
4274 ppc440spe_adma_release_irqs(adev, ppc440spe_chan);
4275 tasklet_kill(&ppc440spe_chan->irq_tasklet);
4276 if (adev->id != PPC440SPE_XOR_ID) {
4277 dma_unmap_page(&ofdev->dev, ppc440spe_chan->pdest,
4278 PAGE_SIZE, DMA_BIDIRECTIONAL);
4279 dma_unmap_page(&ofdev->dev, ppc440spe_chan->qdest,
4280 PAGE_SIZE, DMA_BIDIRECTIONAL);
4281 __free_page(ppc440spe_chan->pdest_page);
4282 __free_page(ppc440spe_chan->qdest_page);
4284 list_for_each_entry_safe(ref, _ref, &ppc440spe_adma_chan_list,
4286 if (ppc440spe_chan ==
4287 to_ppc440spe_adma_chan(ref->chan)) {
4288 list_del(&ref->node);
4292 list_del(&chan->device_node);
4293 kfree(ppc440spe_chan);
4296 dma_free_coherent(adev->dev, adev->pool_size,
4297 adev->dma_desc_pool_virt, adev->dma_desc_pool);
4298 if (adev->id == PPC440SPE_XOR_ID)
4299 iounmap(adev->xor_reg);
4301 iounmap(adev->dma_reg);
4302 of_address_to_resource(np, 0, &res);
4303 release_mem_region(res.start, resource_size(&res));
4309 * /sys driver interface to enable h/w RAID-6 capabilities
4310 * Files created in e.g. /sys/devices/plb.0/400100100.dma0/driver/
4311 * directory are "devices", "enable" and "poly".
4312 * "devices" shows available engines.
4313 * "enable" is used to enable RAID-6 capabilities or to check
4314 * whether these has been activated.
4315 * "poly" allows setting/checking used polynomial (for PPC440SPe only).
4318 static ssize_t show_ppc440spe_devices(struct device_driver *dev, char *buf)
4323 for (i = 0; i < PPC440SPE_ADMA_ENGINES_NUM; i++) {
4324 if (ppc440spe_adma_devices[i] == -1)
4326 size += snprintf(buf + size, PAGE_SIZE - size,
4327 "PPC440SP(E)-ADMA.%d: %s\n", i,
4328 ppc_adma_errors[ppc440spe_adma_devices[i]]);
4333 static ssize_t show_ppc440spe_r6enable(struct device_driver *dev, char *buf)
4335 return snprintf(buf, PAGE_SIZE,
4336 "PPC440SP(e) RAID-6 capabilities are %sABLED.\n",
4337 ppc440spe_r6_enabled ? "EN" : "DIS");
4340 static ssize_t store_ppc440spe_r6enable(struct device_driver *dev,
4341 const char *buf, size_t count)
4345 if (!count || count > 11)
4348 if (!ppc440spe_r6_tchan)
4352 sscanf(buf, "%lx", &val);
4353 dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_XORBA, val);
4356 /* Verify whether it really works now */
4357 if (ppc440spe_test_raid6(ppc440spe_r6_tchan) == 0) {
4358 pr_info("PPC440SP(e) RAID-6 has been activated "
4360 ppc440spe_r6_enabled = 1;
4362 pr_info("PPC440SP(e) RAID-6 hasn't been activated!"
4364 ppc440spe_r6_enabled = 0;
4369 static ssize_t show_ppc440spe_r6poly(struct device_driver *dev, char *buf)
4375 /* 440SP has fixed polynomial */
4378 reg = dcr_read(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL);
4379 reg >>= MQ0_CFBHL_POLY;
4383 size = snprintf(buf, PAGE_SIZE, "PPC440SP(e) RAID-6 driver "
4384 "uses 0x1%02x polynomial.\n", reg);
4388 static ssize_t store_ppc440spe_r6poly(struct device_driver *dev,
4389 const char *buf, size_t count)
4391 unsigned long reg, val;
4394 /* 440SP uses default 0x14D polynomial only */
4398 if (!count || count > 6)
4401 /* e.g., 0x14D or 0x11D */
4402 sscanf(buf, "%lx", &val);
4408 reg = dcr_read(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL);
4409 reg &= ~(0xFF << MQ0_CFBHL_POLY);
4410 reg |= val << MQ0_CFBHL_POLY;
4411 dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL, reg);
4416 static DRIVER_ATTR(devices, S_IRUGO, show_ppc440spe_devices, NULL);
4417 static DRIVER_ATTR(enable, S_IRUGO | S_IWUSR, show_ppc440spe_r6enable,
4418 store_ppc440spe_r6enable);
4419 static DRIVER_ATTR(poly, S_IRUGO | S_IWUSR, show_ppc440spe_r6poly,
4420 store_ppc440spe_r6poly);
4423 * Common initialisation for RAID engines; allocate memory for
4424 * DMAx FIFOs, perform configuration common for all DMA engines.
4425 * Further DMA engine specific configuration is done at probe time.
4427 static int ppc440spe_configure_raid_devices(void)
4429 struct device_node *np;
4430 struct resource i2o_res;
4431 struct i2o_regs __iomem *i2o_reg;
4432 dcr_host_t i2o_dcr_host;
4433 unsigned int dcr_base, dcr_len;
4436 np = of_find_compatible_node(NULL, NULL, "ibm,i2o-440spe");
4438 pr_err("%s: can't find I2O device tree node\n",
4443 if (of_address_to_resource(np, 0, &i2o_res)) {
4448 i2o_reg = of_iomap(np, 0);
4450 pr_err("%s: failed to map I2O registers\n", __func__);
4455 /* Get I2O DCRs base */
4456 dcr_base = dcr_resource_start(np, 0);
4457 dcr_len = dcr_resource_len(np, 0);
4458 if (!dcr_base && !dcr_len) {
4459 pr_err("%s: can't get DCR registers base/len!\n",
4466 i2o_dcr_host = dcr_map(np, dcr_base, dcr_len);
4467 if (!DCR_MAP_OK(i2o_dcr_host)) {
4468 pr_err("%s: failed to map DCRs!\n", np->full_name);
4475 /* Provide memory regions for DMA's FIFOs: I2O, DMA0 and DMA1 share
4476 * the base address of FIFO memory space.
4477 * Actually we need twice more physical memory than programmed in the
4478 * <fsiz> register (because there are two FIFOs for each DMA: CP and CS)
4480 ppc440spe_dma_fifo_buf = kmalloc((DMA0_FIFO_SIZE + DMA1_FIFO_SIZE) << 1,
4482 if (!ppc440spe_dma_fifo_buf) {
4483 pr_err("%s: DMA FIFO buffer allocation failed.\n", __func__);
4485 dcr_unmap(i2o_dcr_host, dcr_len);
4493 mtdcri(SDR0, DCRN_SDR0_SRST, DCRN_SDR0_SRST_I2ODMA);
4494 mtdcri(SDR0, DCRN_SDR0_SRST, 0);
4496 /* Setup the base address of mmaped registers */
4497 dcr_write(i2o_dcr_host, DCRN_I2O0_IBAH, (u32)(i2o_res.start >> 32));
4498 dcr_write(i2o_dcr_host, DCRN_I2O0_IBAL, (u32)(i2o_res.start) |
4500 dcr_unmap(i2o_dcr_host, dcr_len);
4502 /* Setup FIFO memory space base address */
4503 iowrite32(0, &i2o_reg->ifbah);
4504 iowrite32(((u32)__pa(ppc440spe_dma_fifo_buf)), &i2o_reg->ifbal);
4506 /* set zero FIFO size for I2O, so the whole
4507 * ppc440spe_dma_fifo_buf is used by DMAs.
4508 * DMAx_FIFOs will be configured while probe.
4510 iowrite32(0, &i2o_reg->ifsiz);
4513 /* To prepare WXOR/RXOR functionality we need access to
4514 * Memory Queue Module DCRs (finally it will be enabled
4515 * via /sys interface of the ppc440spe ADMA driver).
4517 np = of_find_compatible_node(NULL, NULL, "ibm,mq-440spe");
4519 pr_err("%s: can't find MQ device tree node\n",
4525 /* Get MQ DCRs base */
4526 dcr_base = dcr_resource_start(np, 0);
4527 dcr_len = dcr_resource_len(np, 0);
4528 if (!dcr_base && !dcr_len) {
4529 pr_err("%s: can't get DCR registers base/len!\n",
4535 ppc440spe_mq_dcr_host = dcr_map(np, dcr_base, dcr_len);
4536 if (!DCR_MAP_OK(ppc440spe_mq_dcr_host)) {
4537 pr_err("%s: failed to map DCRs!\n", np->full_name);
4542 ppc440spe_mq_dcr_len = dcr_len;
4545 dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_BAUH, DMA_CUED_XOR_HB);
4548 * - LL transaction passing limit to 1;
4549 * - Memory controller cycle limit to 1;
4550 * - Galois Polynomial to 0x14d (default)
4552 dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL,
4553 (1 << MQ0_CFBHL_TPLM) | (1 << MQ0_CFBHL_HBCL) |
4554 (PPC440SPE_DEFAULT_POLY << MQ0_CFBHL_POLY));
4556 atomic_set(&ppc440spe_adma_err_irq_ref, 0);
4557 for (i = 0; i < PPC440SPE_ADMA_ENGINES_NUM; i++)
4558 ppc440spe_adma_devices[i] = -1;
4565 kfree(ppc440spe_dma_fifo_buf);
4569 static const struct of_device_id ppc440spe_adma_of_match[] = {
4570 { .compatible = "ibm,dma-440spe", },
4571 { .compatible = "amcc,xor-accelerator", },
4574 MODULE_DEVICE_TABLE(of, ppc440spe_adma_of_match);
4576 static struct platform_driver ppc440spe_adma_driver = {
4577 .probe = ppc440spe_adma_probe,
4578 .remove = ppc440spe_adma_remove,
4580 .name = "PPC440SP(E)-ADMA",
4581 .owner = THIS_MODULE,
4582 .of_match_table = ppc440spe_adma_of_match,
4586 static __init int ppc440spe_adma_init(void)
4590 ret = ppc440spe_configure_raid_devices();
4594 ret = platform_driver_register(&ppc440spe_adma_driver);
4596 pr_err("%s: failed to register platform driver\n",
4601 /* Initialization status */
4602 ret = driver_create_file(&ppc440spe_adma_driver.driver,
4603 &driver_attr_devices);
4607 /* RAID-6 h/w enable entry */
4608 ret = driver_create_file(&ppc440spe_adma_driver.driver,
4609 &driver_attr_enable);
4613 /* GF polynomial to use */
4614 ret = driver_create_file(&ppc440spe_adma_driver.driver,
4619 driver_remove_file(&ppc440spe_adma_driver.driver,
4620 &driver_attr_enable);
4622 driver_remove_file(&ppc440spe_adma_driver.driver,
4623 &driver_attr_devices);
4625 /* User will not be able to enable h/w RAID-6 */
4626 pr_err("%s: failed to create RAID-6 driver interface\n",
4628 platform_driver_unregister(&ppc440spe_adma_driver);
4630 dcr_unmap(ppc440spe_mq_dcr_host, ppc440spe_mq_dcr_len);
4631 kfree(ppc440spe_dma_fifo_buf);
4635 static void __exit ppc440spe_adma_exit(void)
4637 driver_remove_file(&ppc440spe_adma_driver.driver,
4639 driver_remove_file(&ppc440spe_adma_driver.driver,
4640 &driver_attr_enable);
4641 driver_remove_file(&ppc440spe_adma_driver.driver,
4642 &driver_attr_devices);
4643 platform_driver_unregister(&ppc440spe_adma_driver);
4644 dcr_unmap(ppc440spe_mq_dcr_host, ppc440spe_mq_dcr_len);
4645 kfree(ppc440spe_dma_fifo_buf);
4648 arch_initcall(ppc440spe_adma_init);
4649 module_exit(ppc440spe_adma_exit);
4651 MODULE_AUTHOR("Yuri Tikhonov <yur@emcraft.com>");
4652 MODULE_DESCRIPTION("PPC440SPE ADMA Engine Driver");
4653 MODULE_LICENSE("GPL");