linux/drivers/dma/ppc4xx/adma.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2006-2009 DENX Software Engineering.
   3 *
   4 * Author: Yuri Tikhonov <yur@emcraft.com>
   5 *
   6 * Further porting to arch/powerpc by
   7 *      Anatolij Gustschin <agust@denx.de>
   8 *
   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)
  12 * any later version.
  13 *
  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
  17 * more details.
  18 *
  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.
  22 *
  23 * The full GNU General Public License is included in this distribution in the
  24 * file called COPYING.
  25 */
  26
  27/*
  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.
  32 */
  33
  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>
  44#include <linux/of.h>
  45#include <linux/of_address.h>
  46#include <linux/of_irq.h>
  47#include <linux/of_platform.h>
  48#include <asm/dcr.h>
  49#include <asm/dcr-regs.h>
  50#include "adma.h"
  51#include "../dmaengine.h"
  52
  53enum ppc_adma_init_code {
  54        PPC_ADMA_INIT_OK = 0,
  55        PPC_ADMA_INIT_MEMRES,
  56        PPC_ADMA_INIT_MEMREG,
  57        PPC_ADMA_INIT_ALLOC,
  58        PPC_ADMA_INIT_COHERENT,
  59        PPC_ADMA_INIT_CHANNEL,
  60        PPC_ADMA_INIT_IRQ1,
  61        PPC_ADMA_INIT_IRQ2,
  62        PPC_ADMA_INIT_REGISTER
  63};
  64
  65static 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 "
  70                                "structure",
  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",
  77};
  78
  79static enum ppc_adma_init_code
  80ppc440spe_adma_devices[PPC440SPE_ADMA_ENGINES_NUM];
  81
  82struct ppc_dma_chan_ref {
  83        struct dma_chan *chan;
  84        struct list_head node;
  85};
  86
  87/* The list of channels exported by ppc440spe ADMA */
  88struct list_head
  89ppc440spe_adma_chan_list = LIST_HEAD_INIT(ppc440spe_adma_chan_list);
  90
  91/* This flag is set when want to refetch the xor chain in the interrupt
  92 * handler
  93 */
  94static u32 do_xor_refetch;
  95
  96/* Pointer to DMA0, DMA1 CP/CS FIFO */
  97static void *ppc440spe_dma_fifo_buf;
  98
  99/* Pointers to last submitted to DMA0, DMA1 CDBs */
 100static struct ppc440spe_adma_desc_slot *chan_last_sub[3];
 101static struct ppc440spe_adma_desc_slot *chan_first_cdb[3];
 102
 103/* Pointer to last linked and submitted xor CB */
 104static struct ppc440spe_adma_desc_slot *xor_last_linked;
 105static struct ppc440spe_adma_desc_slot *xor_last_submit;
 106
 107/* This array is used in data-check operations for storing a pattern */
 108static char ppc440spe_qword[16];
 109
 110static atomic_t ppc440spe_adma_err_irq_ref;
 111static dcr_host_t ppc440spe_mq_dcr_host;
 112static unsigned int ppc440spe_mq_dcr_len;
 113
 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).
 119 */
 120static unsigned long ppc440spe_rxor_state;
 121
 122/* These are used in enable & check routines
 123 */
 124static u32 ppc440spe_r6_enabled;
 125static struct ppc440spe_adma_chan *ppc440spe_r6_tchan;
 126static struct completion ppc440spe_r6_test_comp;
 127
 128static 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);
 132static void ppc440spe_adma_dma2rxor_set_src(
 133                struct ppc440spe_adma_desc_slot *desc,
 134                int index, dma_addr_t addr);
 135static void ppc440spe_adma_dma2rxor_set_mult(
 136                struct ppc440spe_adma_desc_slot *desc,
 137                int index, u8 mult);
 138
 139#ifdef ADMA_LL_DEBUG
 140#define ADMA_LL_DBG(x) ({ if (1) x; 0; })
 141#else
 142#define ADMA_LL_DBG(x) ({ if (0) x; 0; })
 143#endif
 144
 145static void print_cb(struct ppc440spe_adma_chan *chan, void *block)
 146{
 147        struct dma_cdb *cdb;
 148        struct xor_cb *cb;
 149        int i;
 150
 151        switch (chan->device->id) {
 152        case 0:
 153        case 1:
 154                cdb = block;
 155
 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)
 166                );
 167                break;
 168        case 2:
 169                cb = block;
 170
 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)
 181                                continue;
 182                        pr_debug("\t ops[%2d]: h 0x%08x l 0x%08x\n",
 183                                i, cb->ops[i].h, cb->ops[i].l);
 184                }
 185                break;
 186        }
 187}
 188
 189static void print_cb_list(struct ppc440spe_adma_chan *chan,
 190                          struct ppc440spe_adma_desc_slot *iter)
 191{
 192        for (; iter; iter = iter->hw_next)
 193                print_cb(chan, iter->hw_desc);
 194}
 195
 196static void prep_dma_xor_dbg(int id, dma_addr_t dst, dma_addr_t *src,
 197                             unsigned int src_cnt)
 198{
 199        int i;
 200
 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);
 205}
 206
 207static void prep_dma_pq_dbg(int id, dma_addr_t *dst, dma_addr_t *src,
 208                            unsigned int src_cnt)
 209{
 210        int i;
 211
 212        pr_debug("\n%s(%d):\nsrc: ", __func__, id);
 213        for (i = 0; i < src_cnt; i++)
 214                pr_debug("\t0x%016llx ", src[i]);
 215        pr_debug("dst: ");
 216        for (i = 0; i < 2; i++)
 217                pr_debug("\t0x%016llx ", dst[i]);
 218}
 219
 220static void prep_dma_pqzero_sum_dbg(int id, dma_addr_t *src,
 221                                    unsigned int src_cnt,
 222                                    const unsigned char *scf)
 223{
 224        int i;
 225
 226        pr_debug("\n%s(%d):\nsrc(coef): ", __func__, id);
 227        if (scf) {
 228                for (i = 0; i < src_cnt; i++)
 229                        pr_debug("\t0x%016llx(0x%02x) ", src[i], scf[i]);
 230        } else {
 231                for (i = 0; i < src_cnt; i++)
 232                        pr_debug("\t0x%016llx(no) ", src[i]);
 233        }
 234
 235        pr_debug("dst: ");
 236        for (i = 0; i < 2; i++)
 237                pr_debug("\t0x%016llx ", src[src_cnt + i]);
 238}
 239
 240/******************************************************************************
 241 * Command (Descriptor) Blocks low-level routines
 242 ******************************************************************************/
 243/**
 244 * ppc440spe_desc_init_interrupt - initialize the descriptor for INTERRUPT
 245 * pseudo operation
 246 */
 247static void ppc440spe_desc_init_interrupt(struct ppc440spe_adma_desc_slot *desc,
 248                                          struct ppc440spe_adma_chan *chan)
 249{
 250        struct xor_cb *p;
 251
 252        switch (chan->device->id) {
 253        case PPC440SPE_XOR_ID:
 254                p = desc->hw_desc;
 255                memset(desc->hw_desc, 0, sizeof(struct xor_cb));
 256                /* NOP with Command Block Complete Enable */
 257                p->cbc = XOR_CBCR_CBCE_BIT;
 258                break;
 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);
 264                break;
 265        default:
 266                printk(KERN_ERR "Unsupported id %d in %s\n", chan->device->id,
 267                                __func__);
 268                break;
 269        }
 270}
 271
 272/**
 273 * ppc440spe_desc_init_null_xor - initialize the descriptor for NULL XOR
 274 * pseudo operation
 275 */
 276static void ppc440spe_desc_init_null_xor(struct ppc440spe_adma_desc_slot *desc)
 277{
 278        memset(desc->hw_desc, 0, sizeof(struct xor_cb));
 279        desc->hw_next = NULL;
 280        desc->src_cnt = 0;
 281        desc->dst_cnt = 1;
 282}
 283
 284/**
 285 * ppc440spe_desc_init_xor - initialize the descriptor for XOR operation
 286 */
 287static void ppc440spe_desc_init_xor(struct ppc440spe_adma_desc_slot *desc,
 288                                         int src_cnt, unsigned long flags)
 289{
 290        struct xor_cb *hw_desc = desc->hw_desc;
 291
 292        memset(desc->hw_desc, 0, sizeof(struct xor_cb));
 293        desc->hw_next = NULL;
 294        desc->src_cnt = src_cnt;
 295        desc->dst_cnt = 1;
 296
 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;
 301}
 302
 303/**
 304 * ppc440spe_desc_init_dma2pq - initialize the descriptor for PQ
 305 * operation in DMA2 controller
 306 */
 307static void ppc440spe_desc_init_dma2pq(struct ppc440spe_adma_desc_slot *desc,
 308                int dst_cnt, int src_cnt, unsigned long flags)
 309{
 310        struct xor_cb *hw_desc = desc->hw_desc;
 311
 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;
 318
 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;
 323}
 324
 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)
 328
 329/**
 330 * ppc440spe_desc_init_dma01pq - initialize the descriptors for PQ operation
 331 * with DMA0/1
 332 */
 333static void ppc440spe_desc_init_dma01pq(struct ppc440spe_adma_desc_slot *desc,
 334                                int dst_cnt, int src_cnt, unsigned long flags,
 335                                unsigned long op)
 336{
 337        struct dma_cdb *hw_desc;
 338        struct ppc440spe_adma_desc_slot *iter;
 339        u8 dopc;
 340
 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;
 345
 346        /* WXOR MULTICAST if both P and Q are being computed
 347         * MV_SG1_SG2 if Q only
 348         */
 349        dopc = (desc->dst_cnt == DMA_DEST_MAX_NUM) ?
 350                DMA_CDB_OPC_MULTICAST : DMA_CDB_OPC_MV_SG1_SG2;
 351
 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));
 355
 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);
 362                } else {
 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, ...)
 367                         */
 368                        iter->hw_next = NULL;
 369                        if (flags & DMA_PREP_INTERRUPT)
 370                                set_bit(PPC440SPE_DESC_INT, &iter->flags);
 371                        else
 372                                clear_bit(PPC440SPE_DESC_INT, &iter->flags);
 373                }
 374        }
 375
 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.
 382                 */
 383                iter = list_first_entry(&desc->group_list,
 384                                        struct ppc440spe_adma_desc_slot,
 385                                        chain_node);
 386
 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,
 392                                        chain_node);
 393                }
 394
 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,
 400                                        chain_node);
 401                }
 402
 403                list_for_each_entry_from(iter, &desc->group_list, chain_node) {
 404                        hw_desc = iter->hw_desc;
 405                        hw_desc->opc = dopc;
 406                }
 407        } else {
 408                /* This is either RXOR-only or mixed RXOR/WXOR */
 409
 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
 413                 */
 414                iter = list_first_entry(&desc->group_list,
 415                                        struct ppc440spe_adma_desc_slot,
 416                                        chain_node);
 417                hw_desc = iter->hw_desc;
 418                hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
 419
 420                if (desc->dst_cnt == DMA_DEST_MAX_NUM) {
 421                        iter = list_first_entry(&iter->chain_node,
 422                                                struct ppc440spe_adma_desc_slot,
 423                                                chain_node);
 424                        hw_desc = iter->hw_desc;
 425                        hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
 426                }
 427
 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,
 432                                                chain_node);
 433                        list_for_each_entry_from(iter, &desc->group_list,
 434                                                chain_node) {
 435                                hw_desc = iter->hw_desc;
 436                                hw_desc->opc = dopc;
 437                        }
 438                }
 439        }
 440}
 441
 442/**
 443 * ppc440spe_desc_init_dma01pqzero_sum - initialize the descriptor
 444 * for PQ_ZERO_SUM operation
 445 */
 446static void ppc440spe_desc_init_dma01pqzero_sum(
 447                                struct ppc440spe_adma_desc_slot *desc,
 448                                int dst_cnt, int src_cnt)
 449{
 450        struct dma_cdb *hw_desc;
 451        struct ppc440spe_adma_desc_slot *iter;
 452        int i = 0;
 453        u8 dopc = (dst_cnt == 2) ? DMA_CDB_OPC_MULTICAST :
 454                                   DMA_CDB_OPC_MV_SG1_SG2;
 455        /*
 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.
 460         */
 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);
 465
 466        if (dst_cnt > 1) {
 467                iter = list_entry(iter->chain_node.next,
 468                                  struct ppc440spe_adma_desc_slot, chain_node);
 469        }
 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));
 474                iter->src_cnt = 0;
 475                iter->dst_cnt = 0;
 476
 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
 481                 */
 482                if (i++ < src_cnt)
 483                        /* MV_SG1_SG2 if only Q is being verified
 484                         * MULTICAST if both P and Q are being verified
 485                         */
 486                        hw_desc->opc = dopc;
 487                else
 488                        /* DMA_CDB_OPC_DCHECK128 operation */
 489                        hw_desc->opc = DMA_CDB_OPC_DCHECK128;
 490
 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,
 496                                                chain_node);
 497                } else {
 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, ...)
 502                         */
 503                        iter->hw_next = NULL;
 504                        /* always enable interrupt generation since we get
 505                         * the status of pqzero from the handler
 506                         */
 507                        set_bit(PPC440SPE_DESC_INT, &iter->flags);
 508                }
 509        }
 510        desc->src_cnt = src_cnt;
 511        desc->dst_cnt = dst_cnt;
 512}
 513
 514/**
 515 * ppc440spe_desc_init_memcpy - initialize the descriptor for MEMCPY operation
 516 */
 517static void ppc440spe_desc_init_memcpy(struct ppc440spe_adma_desc_slot *desc,
 518                                        unsigned long flags)
 519{
 520        struct dma_cdb *hw_desc = desc->hw_desc;
 521
 522        memset(desc->hw_desc, 0, sizeof(struct dma_cdb));
 523        desc->hw_next = NULL;
 524        desc->src_cnt = 1;
 525        desc->dst_cnt = 1;
 526
 527        if (flags & DMA_PREP_INTERRUPT)
 528                set_bit(PPC440SPE_DESC_INT, &desc->flags);
 529        else
 530                clear_bit(PPC440SPE_DESC_INT, &desc->flags);
 531
 532        hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
 533}
 534
 535/**
 536 * ppc440spe_desc_init_memset - initialize the descriptor for MEMSET operation
 537 */
 538static void ppc440spe_desc_init_memset(struct ppc440spe_adma_desc_slot *desc,
 539                                        int value, unsigned long flags)
 540{
 541        struct dma_cdb *hw_desc = desc->hw_desc;
 542
 543        memset(desc->hw_desc, 0, sizeof(struct dma_cdb));
 544        desc->hw_next = NULL;
 545        desc->src_cnt = 1;
 546        desc->dst_cnt = 1;
 547
 548        if (flags & DMA_PREP_INTERRUPT)
 549                set_bit(PPC440SPE_DESC_INT, &desc->flags);
 550        else
 551                clear_bit(PPC440SPE_DESC_INT, &desc->flags);
 552
 553        hw_desc->sg1u = hw_desc->sg1l = cpu_to_le32((u32)value);
 554        hw_desc->sg3u = hw_desc->sg3l = cpu_to_le32((u32)value);
 555        hw_desc->opc = DMA_CDB_OPC_DFILL128;
 556}
 557
 558/**
 559 * ppc440spe_desc_set_src_addr - set source address into the descriptor
 560 */
 561static void ppc440spe_desc_set_src_addr(struct ppc440spe_adma_desc_slot *desc,
 562                                        struct ppc440spe_adma_chan *chan,
 563                                        int src_idx, dma_addr_t addrh,
 564                                        dma_addr_t addrl)
 565{
 566        struct dma_cdb *dma_hw_desc;
 567        struct xor_cb *xor_hw_desc;
 568        phys_addr_t addr64, tmplow, tmphi;
 569
 570        switch (chan->device->id) {
 571        case PPC440SPE_DMA0_ID:
 572        case PPC440SPE_DMA1_ID:
 573                if (!addrh) {
 574                        addr64 = addrl;
 575                        tmphi = (addr64 >> 32);
 576                        tmplow = (addr64 & 0xFFFFFFFF);
 577                } else {
 578                        tmphi = addrh;
 579                        tmplow = addrl;
 580                }
 581                dma_hw_desc = desc->hw_desc;
 582                dma_hw_desc->sg1l = cpu_to_le32((u32)tmplow);
 583                dma_hw_desc->sg1u |= cpu_to_le32((u32)tmphi);
 584                break;
 585        case PPC440SPE_XOR_ID:
 586                xor_hw_desc = desc->hw_desc;
 587                xor_hw_desc->ops[src_idx].l = addrl;
 588                xor_hw_desc->ops[src_idx].h |= addrh;
 589                break;
 590        }
 591}
 592
 593/**
 594 * ppc440spe_desc_set_src_mult - set source address mult into the descriptor
 595 */
 596static void ppc440spe_desc_set_src_mult(struct ppc440spe_adma_desc_slot *desc,
 597                        struct ppc440spe_adma_chan *chan, u32 mult_index,
 598                        int sg_index, unsigned char mult_value)
 599{
 600        struct dma_cdb *dma_hw_desc;
 601        struct xor_cb *xor_hw_desc;
 602        u32 *psgu;
 603
 604        switch (chan->device->id) {
 605        case PPC440SPE_DMA0_ID:
 606        case PPC440SPE_DMA1_ID:
 607                dma_hw_desc = desc->hw_desc;
 608
 609                switch (sg_index) {
 610                /* for RXOR operations set multiplier
 611                 * into source cued address
 612                 */
 613                case DMA_CDB_SG_SRC:
 614                        psgu = &dma_hw_desc->sg1u;
 615                        break;
 616                /* for WXOR operations set multiplier
 617                 * into destination cued address(es)
 618                 */
 619                case DMA_CDB_SG_DST1:
 620                        psgu = &dma_hw_desc->sg2u;
 621                        break;
 622                case DMA_CDB_SG_DST2:
 623                        psgu = &dma_hw_desc->sg3u;
 624                        break;
 625                default:
 626                        BUG();
 627                }
 628
 629                *psgu |= cpu_to_le32(mult_value << mult_index);
 630                break;
 631        case PPC440SPE_XOR_ID:
 632                xor_hw_desc = desc->hw_desc;
 633                break;
 634        default:
 635                BUG();
 636        }
 637}
 638
 639/**
 640 * ppc440spe_desc_set_dest_addr - set destination address into the descriptor
 641 */
 642static void ppc440spe_desc_set_dest_addr(struct ppc440spe_adma_desc_slot *desc,
 643                                struct ppc440spe_adma_chan *chan,
 644                                dma_addr_t addrh, dma_addr_t addrl,
 645                                u32 dst_idx)
 646{
 647        struct dma_cdb *dma_hw_desc;
 648        struct xor_cb *xor_hw_desc;
 649        phys_addr_t addr64, tmphi, tmplow;
 650        u32 *psgu, *psgl;
 651
 652        switch (chan->device->id) {
 653        case PPC440SPE_DMA0_ID:
 654        case PPC440SPE_DMA1_ID:
 655                if (!addrh) {
 656                        addr64 = addrl;
 657                        tmphi = (addr64 >> 32);
 658                        tmplow = (addr64 & 0xFFFFFFFF);
 659                } else {
 660                        tmphi = addrh;
 661                        tmplow = addrl;
 662                }
 663                dma_hw_desc = desc->hw_desc;
 664
 665                psgu = dst_idx ? &dma_hw_desc->sg3u : &dma_hw_desc->sg2u;
 666                psgl = dst_idx ? &dma_hw_desc->sg3l : &dma_hw_desc->sg2l;
 667
 668                *psgl = cpu_to_le32((u32)tmplow);
 669                *psgu |= cpu_to_le32((u32)tmphi);
 670                break;
 671        case PPC440SPE_XOR_ID:
 672                xor_hw_desc = desc->hw_desc;
 673                xor_hw_desc->cbtal = addrl;
 674                xor_hw_desc->cbtah |= addrh;
 675                break;
 676        }
 677}
 678
 679/**
 680 * ppc440spe_desc_set_byte_count - set number of data bytes involved
 681 * into the operation
 682 */
 683static void ppc440spe_desc_set_byte_count(struct ppc440spe_adma_desc_slot *desc,
 684                                struct ppc440spe_adma_chan *chan,
 685                                u32 byte_count)
 686{
 687        struct dma_cdb *dma_hw_desc;
 688        struct xor_cb *xor_hw_desc;
 689
 690        switch (chan->device->id) {
 691        case PPC440SPE_DMA0_ID:
 692        case PPC440SPE_DMA1_ID:
 693                dma_hw_desc = desc->hw_desc;
 694                dma_hw_desc->cnt = cpu_to_le32(byte_count);
 695                break;
 696        case PPC440SPE_XOR_ID:
 697                xor_hw_desc = desc->hw_desc;
 698                xor_hw_desc->cbbc = byte_count;
 699                break;
 700        }
 701}
 702
 703/**
 704 * ppc440spe_desc_set_rxor_block_size - set RXOR block size
 705 */
 706static inline void ppc440spe_desc_set_rxor_block_size(u32 byte_count)
 707{
 708        /* assume that byte_count is aligned on the 512-boundary;
 709         * thus write it directly to the register (bits 23:31 are
 710         * reserved there).
 711         */
 712        dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CF2H, byte_count);
 713}
 714
 715/**
 716 * ppc440spe_desc_set_dcheck - set CHECK pattern
 717 */
 718static void ppc440spe_desc_set_dcheck(struct ppc440spe_adma_desc_slot *desc,
 719                                struct ppc440spe_adma_chan *chan, u8 *qword)
 720{
 721        struct dma_cdb *dma_hw_desc;
 722
 723        switch (chan->device->id) {
 724        case PPC440SPE_DMA0_ID:
 725        case PPC440SPE_DMA1_ID:
 726                dma_hw_desc = desc->hw_desc;
 727                iowrite32(qword[0], &dma_hw_desc->sg3l);
 728                iowrite32(qword[4], &dma_hw_desc->sg3u);
 729                iowrite32(qword[8], &dma_hw_desc->sg2l);
 730                iowrite32(qword[12], &dma_hw_desc->sg2u);
 731                break;
 732        default:
 733                BUG();
 734        }
 735}
 736
 737/**
 738 * ppc440spe_xor_set_link - set link address in xor CB
 739 */
 740static void ppc440spe_xor_set_link(struct ppc440spe_adma_desc_slot *prev_desc,
 741                                struct ppc440spe_adma_desc_slot *next_desc)
 742{
 743        struct xor_cb *xor_hw_desc = prev_desc->hw_desc;
 744
 745        if (unlikely(!next_desc || !(next_desc->phys))) {
 746                printk(KERN_ERR "%s: next_desc=0x%p; next_desc->phys=0x%llx\n",
 747                        __func__, next_desc,
 748                        next_desc ? next_desc->phys : 0);
 749                BUG();
 750        }
 751
 752        xor_hw_desc->cbs = 0;
 753        xor_hw_desc->cblal = next_desc->phys;
 754        xor_hw_desc->cblah = 0;
 755        xor_hw_desc->cbc |= XOR_CBCR_LNK_BIT;
 756}
 757
 758/**
 759 * ppc440spe_desc_set_link - set the address of descriptor following this
 760 * descriptor in chain
 761 */
 762static void ppc440spe_desc_set_link(struct ppc440spe_adma_chan *chan,
 763                                struct ppc440spe_adma_desc_slot *prev_desc,
 764                                struct ppc440spe_adma_desc_slot *next_desc)
 765{
 766        unsigned long flags;
 767        struct ppc440spe_adma_desc_slot *tail = next_desc;
 768
 769        if (unlikely(!prev_desc || !next_desc ||
 770                (prev_desc->hw_next && prev_desc->hw_next != next_desc))) {
 771                /* If previous next is overwritten something is wrong.
 772                 * though we may refetch from append to initiate list
 773                 * processing; in this case - it's ok.
 774                 */
 775                printk(KERN_ERR "%s: prev_desc=0x%p; next_desc=0x%p; "
 776                        "prev->hw_next=0x%p\n", __func__, prev_desc,
 777                        next_desc, prev_desc ? prev_desc->hw_next : 0);
 778                BUG();
 779        }
 780
 781        local_irq_save(flags);
 782
 783        /* do s/w chaining both for DMA and XOR descriptors */
 784        prev_desc->hw_next = next_desc;
 785
 786        switch (chan->device->id) {
 787        case PPC440SPE_DMA0_ID:
 788        case PPC440SPE_DMA1_ID:
 789                break;
 790        case PPC440SPE_XOR_ID:
 791                /* bind descriptor to the chain */
 792                while (tail->hw_next)
 793                        tail = tail->hw_next;
 794                xor_last_linked = tail;
 795
 796                if (prev_desc == xor_last_submit)
 797                        /* do not link to the last submitted CB */
 798                        break;
 799                ppc440spe_xor_set_link(prev_desc, next_desc);
 800                break;
 801        }
 802
 803        local_irq_restore(flags);
 804}
 805
 806/**
 807 * ppc440spe_desc_get_src_addr - extract the source address from the descriptor
 808 */
 809static u32 ppc440spe_desc_get_src_addr(struct ppc440spe_adma_desc_slot *desc,
 810                                struct ppc440spe_adma_chan *chan, int src_idx)
 811{
 812        struct dma_cdb *dma_hw_desc;
 813        struct xor_cb *xor_hw_desc;
 814
 815        switch (chan->device->id) {
 816        case PPC440SPE_DMA0_ID:
 817        case PPC440SPE_DMA1_ID:
 818                dma_hw_desc = desc->hw_desc;
 819                /* May have 0, 1, 2, or 3 sources */
 820                switch (dma_hw_desc->opc) {
 821                case DMA_CDB_OPC_NO_OP:
 822                case DMA_CDB_OPC_DFILL128:
 823                        return 0;
 824                case DMA_CDB_OPC_DCHECK128:
 825                        if (unlikely(src_idx)) {
 826                                printk(KERN_ERR "%s: try to get %d source for"
 827                                    " DCHECK128\n", __func__, src_idx);
 828                                BUG();
 829                        }
 830                        return le32_to_cpu(dma_hw_desc->sg1l);
 831                case DMA_CDB_OPC_MULTICAST:
 832                case DMA_CDB_OPC_MV_SG1_SG2:
 833                        if (unlikely(src_idx > 2)) {
 834                                printk(KERN_ERR "%s: try to get %d source from"
 835                                    " DMA descr\n", __func__, src_idx);
 836                                BUG();
 837                        }
 838                        if (src_idx) {
 839                                if (le32_to_cpu(dma_hw_desc->sg1u) &
 840                                    DMA_CUED_XOR_WIN_MSK) {
 841                                        u8 region;
 842
 843                                        if (src_idx == 1)
 844                                                return le32_to_cpu(
 845                                                    dma_hw_desc->sg1l) +
 846                                                        desc->unmap_len;
 847
 848                                        region = (le32_to_cpu(
 849                                            dma_hw_desc->sg1u)) >>
 850                                                DMA_CUED_REGION_OFF;
 851
 852                                        region &= DMA_CUED_REGION_MSK;
 853                                        switch (region) {
 854                                        case DMA_RXOR123:
 855                                                return le32_to_cpu(
 856                                                    dma_hw_desc->sg1l) +
 857                                                        (desc->unmap_len << 1);
 858                                        case DMA_RXOR124:
 859                                                return le32_to_cpu(
 860                                                    dma_hw_desc->sg1l) +
 861                                                        (desc->unmap_len * 3);
 862                                        case DMA_RXOR125:
 863                                                return le32_to_cpu(
 864                                                    dma_hw_desc->sg1l) +
 865                                                        (desc->unmap_len << 2);
 866                                        default:
 867                                                printk(KERN_ERR
 868                                                    "%s: try to"
 869                                                    " get src3 for region %02x"
 870                                                    "PPC440SPE_DESC_RXOR12?\n",
 871                                                    __func__, region);
 872                                                BUG();
 873                                        }
 874                                } else {
 875                                        printk(KERN_ERR
 876                                                "%s: try to get %d"
 877                                                " source for non-cued descr\n",
 878                                                __func__, src_idx);
 879                                        BUG();
 880                                }
 881                        }
 882                        return le32_to_cpu(dma_hw_desc->sg1l);
 883                default:
 884                        printk(KERN_ERR "%s: unknown OPC 0x%02x\n",
 885                                __func__, dma_hw_desc->opc);
 886                        BUG();
 887                }
 888                return le32_to_cpu(dma_hw_desc->sg1l);
 889        case PPC440SPE_XOR_ID:
 890                /* May have up to 16 sources */
 891                xor_hw_desc = desc->hw_desc;
 892                return xor_hw_desc->ops[src_idx].l;
 893        }
 894        return 0;
 895}
 896
 897/**
 898 * ppc440spe_desc_get_dest_addr - extract the destination address from the
 899 * descriptor
 900 */
 901static u32 ppc440spe_desc_get_dest_addr(struct ppc440spe_adma_desc_slot *desc,
 902                                struct ppc440spe_adma_chan *chan, int idx)
 903{
 904        struct dma_cdb *dma_hw_desc;
 905        struct xor_cb *xor_hw_desc;
 906
 907        switch (chan->device->id) {
 908        case PPC440SPE_DMA0_ID:
 909        case PPC440SPE_DMA1_ID:
 910                dma_hw_desc = desc->hw_desc;
 911
 912                if (likely(!idx))
 913                        return le32_to_cpu(dma_hw_desc->sg2l);
 914                return le32_to_cpu(dma_hw_desc->sg3l);
 915        case PPC440SPE_XOR_ID:
 916                xor_hw_desc = desc->hw_desc;
 917                return xor_hw_desc->cbtal;
 918        }
 919        return 0;
 920}
 921
 922/**
 923 * ppc440spe_desc_get_src_num - extract the number of source addresses from
 924 * the descriptor
 925 */
 926static u32 ppc440spe_desc_get_src_num(struct ppc440spe_adma_desc_slot *desc,
 927                                struct ppc440spe_adma_chan *chan)
 928{
 929        struct dma_cdb *dma_hw_desc;
 930        struct xor_cb *xor_hw_desc;
 931
 932        switch (chan->device->id) {
 933        case PPC440SPE_DMA0_ID:
 934        case PPC440SPE_DMA1_ID:
 935                dma_hw_desc = desc->hw_desc;
 936
 937                switch (dma_hw_desc->opc) {
 938                case DMA_CDB_OPC_NO_OP:
 939                case DMA_CDB_OPC_DFILL128:
 940                        return 0;
 941                case DMA_CDB_OPC_DCHECK128:
 942                        return 1;
 943                case DMA_CDB_OPC_MV_SG1_SG2:
 944                case DMA_CDB_OPC_MULTICAST:
 945                        /*
 946                         * Only for RXOR operations we have more than
 947                         * one source
 948                         */
 949                        if (le32_to_cpu(dma_hw_desc->sg1u) &
 950                            DMA_CUED_XOR_WIN_MSK) {
 951                                /* RXOR op, there are 2 or 3 sources */
 952                                if (((le32_to_cpu(dma_hw_desc->sg1u) >>
 953                                    DMA_CUED_REGION_OFF) &
 954                                      DMA_CUED_REGION_MSK) == DMA_RXOR12) {
 955                                        /* RXOR 1-2 */
 956                                        return 2;
 957                                } else {
 958                                        /* RXOR 1-2-3/1-2-4/1-2-5 */
 959                                        return 3;
 960                                }
 961                        }
 962                        return 1;
 963                default:
 964                        printk(KERN_ERR "%s: unknown OPC 0x%02x\n",
 965                                __func__, dma_hw_desc->opc);
 966                        BUG();
 967                }
 968        case PPC440SPE_XOR_ID:
 969                /* up to 16 sources */
 970                xor_hw_desc = desc->hw_desc;
 971                return xor_hw_desc->cbc & XOR_CDCR_OAC_MSK;
 972        default:
 973                BUG();
 974        }
 975        return 0;
 976}
 977
 978/**
 979 * ppc440spe_desc_get_dst_num - get the number of destination addresses in
 980 * this descriptor
 981 */
 982static u32 ppc440spe_desc_get_dst_num(struct ppc440spe_adma_desc_slot *desc,
 983                                struct ppc440spe_adma_chan *chan)
 984{
 985        struct dma_cdb *dma_hw_desc;
 986
 987        switch (chan->device->id) {
 988        case PPC440SPE_DMA0_ID:
 989        case PPC440SPE_DMA1_ID:
 990                /* May be 1 or 2 destinations */
 991                dma_hw_desc = desc->hw_desc;
 992                switch (dma_hw_desc->opc) {
 993                case DMA_CDB_OPC_NO_OP:
 994                case DMA_CDB_OPC_DCHECK128:
 995                        return 0;
 996                case DMA_CDB_OPC_MV_SG1_SG2:
 997                case DMA_CDB_OPC_DFILL128:
 998                        return 1;
 999                case DMA_CDB_OPC_MULTICAST:
1000                        if (desc->dst_cnt == 2)
1001                                return 2;
1002                        else
1003                                return 1;
1004                default:
1005                        printk(KERN_ERR "%s: unknown OPC 0x%02x\n",
1006                                __func__, dma_hw_desc->opc);
1007                        BUG();
1008                }
1009        case PPC440SPE_XOR_ID:
1010                /* Always only 1 destination */
1011                return 1;
1012        default:
1013                BUG();
1014        }
1015        return 0;
1016}
1017
1018/**
1019 * ppc440spe_desc_get_link - get the address of the descriptor that
1020 * follows this one
1021 */
1022static inline u32 ppc440spe_desc_get_link(struct ppc440spe_adma_desc_slot *desc,
1023                                        struct ppc440spe_adma_chan *chan)
1024{
1025        if (!desc->hw_next)
1026                return 0;
1027
1028        return desc->hw_next->phys;
1029}
1030
1031/**
1032 * ppc440spe_desc_is_aligned - check alignment
1033 */
1034static inline int ppc440spe_desc_is_aligned(
1035        struct ppc440spe_adma_desc_slot *desc, int num_slots)
1036{
1037        return (desc->idx & (num_slots - 1)) ? 0 : 1;
1038}
1039
1040/**
1041 * ppc440spe_chan_xor_slot_count - get the number of slots necessary for
1042 * XOR operation
1043 */
1044static int ppc440spe_chan_xor_slot_count(size_t len, int src_cnt,
1045                        int *slots_per_op)
1046{
1047        int slot_cnt;
1048
1049        /* each XOR descriptor provides up to 16 source operands */
1050        slot_cnt = *slots_per_op = (src_cnt + XOR_MAX_OPS - 1)/XOR_MAX_OPS;
1051
1052        if (likely(len <= PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT))
1053                return slot_cnt;
1054
1055        printk(KERN_ERR "%s: len %d > max %d !!\n",
1056                __func__, len, PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
1057        BUG();
1058        return slot_cnt;
1059}
1060
1061/**
1062 * ppc440spe_dma2_pq_slot_count - get the number of slots necessary for
1063 * DMA2 PQ operation
1064 */
1065static int ppc440spe_dma2_pq_slot_count(dma_addr_t *srcs,
1066                int src_cnt, size_t len)
1067{
1068        signed long long order = 0;
1069        int state = 0;
1070        int addr_count = 0;
1071        int i;
1072        for (i = 1; i < src_cnt; i++) {
1073                dma_addr_t cur_addr = srcs[i];
1074                dma_addr_t old_addr = srcs[i-1];
1075                switch (state) {
1076                case 0:
1077                        if (cur_addr == old_addr + len) {
1078                                /* direct RXOR */
1079                                order = 1;
1080                                state = 1;
1081                                if (i == src_cnt-1)
1082                                        addr_count++;
1083                        } else if (old_addr == cur_addr + len) {
1084                                /* reverse RXOR */
1085                                order = -1;
1086                                state = 1;
1087                                if (i == src_cnt-1)
1088                                        addr_count++;
1089                        } else {
1090                                state = 3;
1091                        }
1092                        break;
1093                case 1:
1094                        if (i == src_cnt-2 || (order == -1
1095                                && cur_addr != old_addr - len)) {
1096                                order = 0;
1097                                state = 0;
1098                                addr_count++;
1099                        } else if (cur_addr == old_addr + len*order) {
1100                                state = 2;
1101                                if (i == src_cnt-1)
1102                                        addr_count++;
1103                        } else if (cur_addr == old_addr + 2*len) {
1104                                state = 2;
1105                                if (i == src_cnt-1)
1106                                        addr_count++;
1107                        } else if (cur_addr == old_addr + 3*len) {
1108                                state = 2;
1109                                if (i == src_cnt-1)
1110                                        addr_count++;
1111                        } else {
1112                                order = 0;
1113                                state = 0;
1114                                addr_count++;
1115                        }
1116                        break;
1117                case 2:
1118                        order = 0;
1119                        state = 0;
1120                        addr_count++;
1121                                break;
1122                }
1123                if (state == 3)
1124                        break;
1125        }
1126        if (src_cnt <= 1 || (state != 1 && state != 2)) {
1127                pr_err("%s: src_cnt=%d, state=%d, addr_count=%d, order=%lld\n",
1128                        __func__, src_cnt, state, addr_count, order);
1129                for (i = 0; i < src_cnt; i++)
1130                        pr_err("\t[%d] 0x%llx \n", i, srcs[i]);
1131                BUG();
1132        }
1133
1134        return (addr_count + XOR_MAX_OPS - 1) / XOR_MAX_OPS;
1135}
1136
1137
1138/******************************************************************************
1139 * ADMA channel low-level routines
1140 ******************************************************************************/
1141
1142static u32
1143ppc440spe_chan_get_current_descriptor(struct ppc440spe_adma_chan *chan);
1144static void ppc440spe_chan_append(struct ppc440spe_adma_chan *chan);
1145
1146/**
1147 * ppc440spe_adma_device_clear_eot_status - interrupt ack to XOR or DMA engine
1148 */
1149static void ppc440spe_adma_device_clear_eot_status(
1150                                        struct ppc440spe_adma_chan *chan)
1151{
1152        struct dma_regs *dma_reg;
1153        struct xor_regs *xor_reg;
1154        u8 *p = chan->device->dma_desc_pool_virt;
1155        struct dma_cdb *cdb;
1156        u32 rv, i;
1157
1158        switch (chan->device->id) {
1159        case PPC440SPE_DMA0_ID:
1160        case PPC440SPE_DMA1_ID:
1161                /* read FIFO to ack */
1162                dma_reg = chan->device->dma_reg;
1163                while ((rv = ioread32(&dma_reg->csfpl))) {
1164                        i = rv & DMA_CDB_ADDR_MSK;
1165                        cdb = (struct dma_cdb *)&p[i -
1166                            (u32)chan->device->dma_desc_pool];
1167
1168                        /* Clear opcode to ack. This is necessary for
1169                         * ZeroSum operations only
1170                         */
1171                        cdb->opc = 0;
1172
1173                        if (test_bit(PPC440SPE_RXOR_RUN,
1174                            &ppc440spe_rxor_state)) {
1175                                /* probably this is a completed RXOR op,
1176                                 * get pointer to CDB using the fact that
1177                                 * physical and virtual addresses of CDB
1178                                 * in pools have the same offsets
1179                                 */
1180                                if (le32_to_cpu(cdb->sg1u) &
1181                                    DMA_CUED_XOR_BASE) {
1182                                        /* this is a RXOR */
1183                                        clear_bit(PPC440SPE_RXOR_RUN,
1184                                                  &ppc440spe_rxor_state);
1185                                }
1186                        }
1187
1188                        if (rv & DMA_CDB_STATUS_MSK) {
1189                                /* ZeroSum check failed
1190                                 */
1191                                struct ppc440spe_adma_desc_slot *iter;
1192                                dma_addr_t phys = rv & ~DMA_CDB_MSK;
1193
1194                                /*
1195                                 * Update the status of corresponding
1196                                 * descriptor.
1197                                 */
1198                                list_for_each_entry(iter, &chan->chain,
1199                                    chain_node) {
1200                                        if (iter->phys == phys)
1201                                                break;
1202                                }
1203                                /*
1204                                 * if cannot find the corresponding
1205                                 * slot it's a bug
1206                                 */
1207                                BUG_ON(&iter->chain_node == &chan->chain);
1208
1209                                if (iter->xor_check_result) {
1210                                        if (test_bit(PPC440SPE_DESC_PCHECK,
1211                                                     &iter->flags)) {
1212                                                *iter->xor_check_result |=
1213                                                        SUM_CHECK_P_RESULT;
1214                                        } else
1215                                        if (test_bit(PPC440SPE_DESC_QCHECK,
1216                                                     &iter->flags)) {
1217                                                *iter->xor_check_result |=
1218                                                        SUM_CHECK_Q_RESULT;
1219                                        } else
1220                                                BUG();
1221                                }
1222                        }
1223                }
1224
1225                rv = ioread32(&dma_reg->dsts);
1226                if (rv) {
1227                        pr_err("DMA%d err status: 0x%x\n",
1228                               chan->device->id, rv);
1229                        /* write back to clear */
1230                        iowrite32(rv, &dma_reg->dsts);
1231                }
1232                break;
1233        case PPC440SPE_XOR_ID:
1234                /* reset status bits to ack */
1235                xor_reg = chan->device->xor_reg;
1236                rv = ioread32be(&xor_reg->sr);
1237                iowrite32be(rv, &xor_reg->sr);
1238
1239                if (rv & (XOR_IE_ICBIE_BIT|XOR_IE_ICIE_BIT|XOR_IE_RPTIE_BIT)) {
1240                        if (rv & XOR_IE_RPTIE_BIT) {
1241                                /* Read PLB Timeout Error.
1242                                 * Try to resubmit the CB
1243                                 */
1244                                u32 val = ioread32be(&xor_reg->ccbalr);
1245
1246                                iowrite32be(val, &xor_reg->cblalr);
1247
1248                                val = ioread32be(&xor_reg->crsr);
1249                                iowrite32be(val | XOR_CRSR_XAE_BIT,
1250                                            &xor_reg->crsr);
1251                        } else
1252                                pr_err("XOR ERR 0x%x status\n", rv);
1253                        break;
1254                }
1255
1256                /*  if the XORcore is idle, but there are unprocessed CBs
1257                 * then refetch the s/w chain here
1258                 */
1259                if (!(ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT) &&
1260                    do_xor_refetch)
1261                        ppc440spe_chan_append(chan);
1262                break;
1263        }
1264}
1265
1266/**
1267 * ppc440spe_chan_is_busy - get the channel status
1268 */
1269static int ppc440spe_chan_is_busy(struct ppc440spe_adma_chan *chan)
1270{
1271        struct dma_regs *dma_reg;
1272        struct xor_regs *xor_reg;
1273        int busy = 0;
1274
1275        switch (chan->device->id) {
1276        case PPC440SPE_DMA0_ID:
1277        case PPC440SPE_DMA1_ID:
1278                dma_reg = chan->device->dma_reg;
1279                /*  if command FIFO's head and tail pointers are equal and
1280                 * status tail is the same as command, then channel is free
1281                 */
1282                if (ioread16(&dma_reg->cpfhp) != ioread16(&dma_reg->cpftp) ||
1283                    ioread16(&dma_reg->cpftp) != ioread16(&dma_reg->csftp))
1284                        busy = 1;
1285                break;
1286        case PPC440SPE_XOR_ID:
1287                /* use the special status bit for the XORcore
1288                 */
1289                xor_reg = chan->device->xor_reg;
1290                busy = (ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT) ? 1 : 0;
1291                break;
1292        }
1293
1294        return busy;
1295}
1296
1297/**
1298 * ppc440spe_chan_set_first_xor_descriptor -  init XORcore chain
1299 */
1300static void ppc440spe_chan_set_first_xor_descriptor(
1301                                struct ppc440spe_adma_chan *chan,
1302                                struct ppc440spe_adma_desc_slot *next_desc)
1303{
1304        struct xor_regs *xor_reg = chan->device->xor_reg;
1305
1306        if (ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT)
1307                printk(KERN_INFO "%s: Warn: XORcore is running "
1308                        "when try to set the first CDB!\n",
1309                        __func__);
1310
1311        xor_last_submit = xor_last_linked = next_desc;
1312
1313        iowrite32be(XOR_CRSR_64BA_BIT, &xor_reg->crsr);
1314
1315        iowrite32be(next_desc->phys, &xor_reg->cblalr);
1316        iowrite32be(0, &xor_reg->cblahr);
1317        iowrite32be(ioread32be(&xor_reg->cbcr) | XOR_CBCR_LNK_BIT,
1318                    &xor_reg->cbcr);
1319
1320        chan->hw_chain_inited = 1;
1321}
1322
1323/**
1324 * ppc440spe_dma_put_desc - put DMA0,1 descriptor to FIFO.
1325 * called with irqs disabled
1326 */
1327static void ppc440spe_dma_put_desc(struct ppc440spe_adma_chan *chan,
1328                struct ppc440spe_adma_desc_slot *desc)
1329{
1330        u32 pcdb;
1331        struct dma_regs *dma_reg = chan->device->dma_reg;
1332
1333        pcdb = desc->phys;
1334        if (!test_bit(PPC440SPE_DESC_INT, &desc->flags))
1335                pcdb |= DMA_CDB_NO_INT;
1336
1337        chan_last_sub[chan->device->id] = desc;
1338
1339        ADMA_LL_DBG(print_cb(chan, desc->hw_desc));
1340
1341        iowrite32(pcdb, &dma_reg->cpfpl);
1342}
1343
1344/**
1345 * ppc440spe_chan_append - update the h/w chain in the channel
1346 */
1347static void ppc440spe_chan_append(struct ppc440spe_adma_chan *chan)
1348{
1349        struct xor_regs *xor_reg;
1350        struct ppc440spe_adma_desc_slot *iter;
1351        struct xor_cb *xcb;
1352        u32 cur_desc;
1353        unsigned long flags;
1354
1355        local_irq_save(flags);
1356
1357        switch (chan->device->id) {
1358        case PPC440SPE_DMA0_ID:
1359        case PPC440SPE_DMA1_ID:
1360                cur_desc = ppc440spe_chan_get_current_descriptor(chan);
1361
1362                if (likely(cur_desc)) {
1363                        iter = chan_last_sub[chan->device->id];
1364                        BUG_ON(!iter);
1365                } else {
1366                        /* first peer */
1367                        iter = chan_first_cdb[chan->device->id];
1368                        BUG_ON(!iter);
1369                        ppc440spe_dma_put_desc(chan, iter);
1370                        chan->hw_chain_inited = 1;
1371                }
1372
1373                /* is there something new to append */
1374                if (!iter->hw_next)
1375                        break;
1376
1377                /* flush descriptors from the s/w queue to fifo */
1378                list_for_each_entry_continue(iter, &chan->chain, chain_node) {
1379                        ppc440spe_dma_put_desc(chan, iter);
1380                        if (!iter->hw_next)
1381                                break;
1382                }
1383                break;
1384        case PPC440SPE_XOR_ID:
1385                /* update h/w links and refetch */
1386                if (!xor_last_submit->hw_next)
1387                        break;
1388
1389                xor_reg = chan->device->xor_reg;
1390                /* the last linked CDB has to generate an interrupt
1391                 * that we'd be able to append the next lists to h/w
1392                 * regardless of the XOR engine state at the moment of
1393                 * appending of these next lists
1394                 */
1395                xcb = xor_last_linked->hw_desc;
1396                xcb->cbc |= XOR_CBCR_CBCE_BIT;
1397
1398                if (!(ioread32be(&xor_reg->sr) & XOR_SR_XCP_BIT)) {
1399                        /* XORcore is idle. Refetch now */
1400                        do_xor_refetch = 0;
1401                        ppc440spe_xor_set_link(xor_last_submit,
1402                                xor_last_submit->hw_next);
1403
1404                        ADMA_LL_DBG(print_cb_list(chan,
1405                                xor_last_submit->hw_next));
1406
1407                        xor_last_submit = xor_last_linked;
1408                        iowrite32be(ioread32be(&xor_reg->crsr) |
1409                                    XOR_CRSR_RCBE_BIT | XOR_CRSR_64BA_BIT,
1410                                    &xor_reg->crsr);
1411                } else {
1412                        /* XORcore is running. Refetch later in the handler */
1413                        do_xor_refetch = 1;
1414                }
1415
1416                break;
1417        }
1418
1419        local_irq_restore(flags);
1420}
1421
1422/**
1423 * ppc440spe_chan_get_current_descriptor - get the currently executed descriptor
1424 */
1425static u32
1426ppc440spe_chan_get_current_descriptor(struct ppc440spe_adma_chan *chan)
1427{
1428        struct dma_regs *dma_reg;
1429        struct xor_regs *xor_reg;
1430
1431        if (unlikely(!chan->hw_chain_inited))
1432                /* h/w descriptor chain is not initialized yet */
1433                return 0;
1434
1435        switch (chan->device->id) {
1436        case PPC440SPE_DMA0_ID:
1437        case PPC440SPE_DMA1_ID:
1438                dma_reg = chan->device->dma_reg;
1439                return ioread32(&dma_reg->acpl) & (~DMA_CDB_MSK);
1440        case PPC440SPE_XOR_ID:
1441                xor_reg = chan->device->xor_reg;
1442                return ioread32be(&xor_reg->ccbalr);
1443        }
1444        return 0;
1445}
1446
1447/**
1448 * ppc440spe_chan_run - enable the channel
1449 */
1450static void ppc440spe_chan_run(struct ppc440spe_adma_chan *chan)
1451{
1452        struct xor_regs *xor_reg;
1453
1454        switch (chan->device->id) {
1455        case PPC440SPE_DMA0_ID:
1456        case PPC440SPE_DMA1_ID:
1457                /* DMAs are always enabled, do nothing */
1458                break;
1459        case PPC440SPE_XOR_ID:
1460                /* drain write buffer */
1461                xor_reg = chan->device->xor_reg;
1462
1463                /* fetch descriptor pointed to in <link> */
1464                iowrite32be(XOR_CRSR_64BA_BIT | XOR_CRSR_XAE_BIT,
1465                            &xor_reg->crsr);
1466                break;
1467        }
1468}
1469
1470/******************************************************************************
1471 * ADMA device level
1472 ******************************************************************************/
1473
1474static void ppc440spe_chan_start_null_xor(struct ppc440spe_adma_chan *chan);
1475static int ppc440spe_adma_alloc_chan_resources(struct dma_chan *chan);
1476
1477static dma_cookie_t
1478ppc440spe_adma_tx_submit(struct dma_async_tx_descriptor *tx);
1479
1480static void ppc440spe_adma_set_dest(struct ppc440spe_adma_desc_slot *tx,
1481                                    dma_addr_t addr, int index);
1482static void
1483ppc440spe_adma_memcpy_xor_set_src(struct ppc440spe_adma_desc_slot *tx,
1484                                  dma_addr_t addr, int index);
1485
1486static void
1487ppc440spe_adma_pq_set_dest(struct ppc440spe_adma_desc_slot *tx,
1488                           dma_addr_t *paddr, unsigned long flags);
1489static void
1490ppc440spe_adma_pq_set_src(struct ppc440spe_adma_desc_slot *tx,
1491                          dma_addr_t addr, int index);
1492static void
1493ppc440spe_adma_pq_set_src_mult(struct ppc440spe_adma_desc_slot *tx,
1494                               unsigned char mult, int index, int dst_pos);
1495static void
1496ppc440spe_adma_pqzero_sum_set_dest(struct ppc440spe_adma_desc_slot *tx,
1497                                   dma_addr_t paddr, dma_addr_t qaddr);
1498
1499static struct page *ppc440spe_rxor_srcs[32];
1500
1501/**
1502 * ppc440spe_can_rxor - check if the operands may be processed with RXOR
1503 */
1504static int ppc440spe_can_rxor(struct page **srcs, int src_cnt, size_t len)
1505{
1506        int i, order = 0, state = 0;
1507        int idx = 0;
1508
1509        if (unlikely(!(src_cnt > 1)))
1510                return 0;
1511
1512        BUG_ON(src_cnt > ARRAY_SIZE(ppc440spe_rxor_srcs));
1513
1514        /* Skip holes in the source list before checking */
1515        for (i = 0; i < src_cnt; i++) {
1516                if (!srcs[i])
1517                        continue;
1518                ppc440spe_rxor_srcs[idx++] = srcs[i];
1519        }
1520        src_cnt = idx;
1521
1522        for (i = 1; i < src_cnt; i++) {
1523                char *cur_addr = page_address(ppc440spe_rxor_srcs[i]);
1524                char *old_addr = page_address(ppc440spe_rxor_srcs[i - 1]);
1525
1526                switch (state) {
1527                case 0:
1528                        if (cur_addr == old_addr + len) {
1529                                /* direct RXOR */
1530                                order = 1;
1531                                state = 1;
1532                        } else if (old_addr == cur_addr + len) {
1533                                /* reverse RXOR */
1534                                order = -1;
1535                                state = 1;
1536                        } else
1537                                goto out;
1538                        break;
1539                case 1:
1540                        if ((i == src_cnt - 2) ||
1541                            (order == -1 && cur_addr != old_addr - len)) {
1542                                order = 0;
1543                                state = 0;
1544                        } else if ((cur_addr == old_addr + len * order) ||
1545                                   (cur_addr == old_addr + 2 * len) ||
1546                                   (cur_addr == old_addr + 3 * len)) {
1547                                state = 2;
1548                        } else {
1549                                order = 0;
1550                                state = 0;
1551                        }
1552                        break;
1553                case 2:
1554                        order = 0;
1555                        state = 0;
1556                        break;
1557                }
1558        }
1559
1560out:
1561        if (state == 1 || state == 2)
1562                return 1;
1563
1564        return 0;
1565}
1566
1567/**
1568 * ppc440spe_adma_device_estimate - estimate the efficiency of processing
1569 *      the operation given on this channel. It's assumed that 'chan' is
1570 *      capable to process 'cap' type of operation.
1571 * @chan: channel to use
1572 * @cap: type of transaction
1573 * @dst_lst: array of destination pointers
1574 * @dst_cnt: number of destination operands
1575 * @src_lst: array of source pointers
1576 * @src_cnt: number of source operands
1577 * @src_sz: size of each source operand
1578 */
1579static int ppc440spe_adma_estimate(struct dma_chan *chan,
1580        enum dma_transaction_type cap, struct page **dst_lst, int dst_cnt,
1581        struct page **src_lst, int src_cnt, size_t src_sz)
1582{
1583        int ef = 1;
1584
1585        if (cap == DMA_PQ || cap == DMA_PQ_VAL) {
1586                /* If RAID-6 capabilities were not activated don't try
1587                 * to use them
1588                 */
1589                if (unlikely(!ppc440spe_r6_enabled))
1590                        return -1;
1591        }
1592        /*  In the current implementation of ppc440spe ADMA driver it
1593         * makes sense to pick out only pq case, because it may be
1594         * processed:
1595         * (1) either using Biskup method on DMA2;
1596         * (2) or on DMA0/1.
1597         *  Thus we give a favour to (1) if the sources are suitable;
1598         * else let it be processed on one of the DMA0/1 engines.
1599         *  In the sum_product case where destination is also the
1600         * source process it on DMA0/1 only.
1601         */
1602        if (cap == DMA_PQ && chan->chan_id == PPC440SPE_XOR_ID) {
1603
1604                if (dst_cnt == 1 && src_cnt == 2 && dst_lst[0] == src_lst[1])
1605                        ef = 0; /* sum_product case, process on DMA0/1 */
1606                else if (ppc440spe_can_rxor(src_lst, src_cnt, src_sz))
1607                        ef = 3; /* override (DMA0/1 + idle) */
1608                else
1609                        ef = 0; /* can't process on DMA2 if !rxor */
1610        }
1611
1612        /* channel idleness increases the priority */
1613        if (likely(ef) &&
1614            !ppc440spe_chan_is_busy(to_ppc440spe_adma_chan(chan)))
1615                ef++;
1616
1617        return ef;
1618}
1619
1620struct dma_chan *
1621ppc440spe_async_tx_find_best_channel(enum dma_transaction_type cap,
1622        struct page **dst_lst, int dst_cnt, struct page **src_lst,
1623        int src_cnt, size_t src_sz)
1624{
1625        struct dma_chan *best_chan = NULL;
1626        struct ppc_dma_chan_ref *ref;
1627        int best_rank = -1;
1628
1629        if (unlikely(!src_sz))
1630                return NULL;
1631        if (src_sz > PAGE_SIZE) {
1632                /*
1633                 * should a user of the api ever pass > PAGE_SIZE requests
1634                 * we sort out cases where temporary page-sized buffers
1635                 * are used.
1636                 */
1637                switch (cap) {
1638                case DMA_PQ:
1639                        if (src_cnt == 1 && dst_lst[1] == src_lst[0])
1640                                return NULL;
1641                        if (src_cnt == 2 && dst_lst[1] == src_lst[1])
1642                                return NULL;
1643                        break;
1644                case DMA_PQ_VAL:
1645                case DMA_XOR_VAL:
1646                        return NULL;
1647                default:
1648                        break;
1649                }
1650        }
1651
1652        list_for_each_entry(ref, &ppc440spe_adma_chan_list, node) {
1653                if (dma_has_cap(cap, ref->chan->device->cap_mask)) {
1654                        int rank;
1655
1656                        rank = ppc440spe_adma_estimate(ref->chan, cap, dst_lst,
1657                                        dst_cnt, src_lst, src_cnt, src_sz);
1658                        if (rank > best_rank) {
1659                                best_rank = rank;
1660                                best_chan = ref->chan;
1661                        }
1662                }
1663        }
1664
1665        return best_chan;
1666}
1667EXPORT_SYMBOL_GPL(ppc440spe_async_tx_find_best_channel);
1668
1669/**
1670 * ppc440spe_get_group_entry - get group entry with index idx
1671 * @tdesc: is the last allocated slot in the group.
1672 */
1673static struct ppc440spe_adma_desc_slot *
1674ppc440spe_get_group_entry(struct ppc440spe_adma_desc_slot *tdesc, u32 entry_idx)
1675{
1676        struct ppc440spe_adma_desc_slot *iter = tdesc->group_head;
1677        int i = 0;
1678
1679        if (entry_idx < 0 || entry_idx >= (tdesc->src_cnt + tdesc->dst_cnt)) {
1680                printk("%s: entry_idx %d, src_cnt %d, dst_cnt %d\n",
1681                        __func__, entry_idx, tdesc->src_cnt, tdesc->dst_cnt);
1682                BUG();
1683        }
1684
1685        list_for_each_entry(iter, &tdesc->group_list, chain_node) {
1686                if (i++ == entry_idx)
1687                        break;
1688        }
1689        return iter;
1690}
1691
1692/**
1693 * ppc440spe_adma_free_slots - flags descriptor slots for reuse
1694 * @slot: Slot to free
1695 * Caller must hold &ppc440spe_chan->lock while calling this function
1696 */
1697static void ppc440spe_adma_free_slots(struct ppc440spe_adma_desc_slot *slot,
1698                                      struct ppc440spe_adma_chan *chan)
1699{
1700        int stride = slot->slots_per_op;
1701
1702        while (stride--) {
1703                slot->slots_per_op = 0;
1704                slot = list_entry(slot->slot_node.next,
1705                                struct ppc440spe_adma_desc_slot,
1706                                slot_node);
1707        }
1708}
1709
1710static void ppc440spe_adma_unmap(struct ppc440spe_adma_chan *chan,
1711                                 struct ppc440spe_adma_desc_slot *desc)
1712{
1713        u32 src_cnt, dst_cnt;
1714        dma_addr_t addr;
1715
1716        /*
1717         * get the number of sources & destination
1718         * included in this descriptor and unmap
1719         * them all
1720         */
1721        src_cnt = ppc440spe_desc_get_src_num(desc, chan);
1722        dst_cnt = ppc440spe_desc_get_dst_num(desc, chan);
1723
1724        /* unmap destinations */
1725        if (!(desc->async_tx.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
1726                while (dst_cnt--) {
1727                        addr = ppc440spe_desc_get_dest_addr(
1728                                desc, chan, dst_cnt);
1729                        dma_unmap_page(chan->device->dev,
1730                                        addr, desc->unmap_len,
1731                                        DMA_FROM_DEVICE);
1732                }
1733        }
1734
1735        /* unmap sources */
1736        if (!(desc->async_tx.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
1737                while (src_cnt--) {
1738                        addr = ppc440spe_desc_get_src_addr(
1739                                desc, chan, src_cnt);
1740                        dma_unmap_page(chan->device->dev,
1741                                        addr, desc->unmap_len,
1742                                        DMA_TO_DEVICE);
1743                }
1744        }
1745}
1746
1747/**
1748 * ppc440spe_adma_run_tx_complete_actions - call functions to be called
1749 * upon completion
1750 */
1751static dma_cookie_t ppc440spe_adma_run_tx_complete_actions(
1752                struct ppc440spe_adma_desc_slot *desc,
1753                struct ppc440spe_adma_chan *chan,
1754                dma_cookie_t cookie)
1755{
1756        int i;
1757
1758        BUG_ON(desc->async_tx.cookie < 0);
1759        if (desc->async_tx.cookie > 0) {
1760                cookie = desc->async_tx.cookie;
1761                desc->async_tx.cookie = 0;
1762
1763                /* call the callback (must not sleep or submit new
1764                 * operations to this channel)
1765                 */
1766                if (desc->async_tx.callback)
1767                        desc->async_tx.callback(
1768                                desc->async_tx.callback_param);
1769
1770                dma_descriptor_unmap(&desc->async_tx);
1771                /* unmap dma addresses
1772                 * (unmap_single vs unmap_page?)
1773                 *
1774                 * actually, ppc's dma_unmap_page() functions are empty, so
1775                 * the following code is just for the sake of completeness
1776                 */
1777                if (chan && chan->needs_unmap && desc->group_head &&
1778                     desc->unmap_len) {
1779                        struct ppc440spe_adma_desc_slot *unmap =
1780                                                        desc->group_head;
1781                        /* assume 1 slot per op always */
1782                        u32 slot_count = unmap->slot_cnt;
1783
1784                        /* Run through the group list and unmap addresses */
1785                        for (i = 0; i < slot_count; i++) {
1786                                BUG_ON(!unmap);
1787                                ppc440spe_adma_unmap(chan, unmap);
1788                                unmap = unmap->hw_next;
1789                        }
1790                }
1791        }
1792
1793        /* run dependent operations */
1794        dma_run_dependencies(&desc->async_tx);
1795
1796        return cookie;
1797}
1798
1799/**
1800 * ppc440spe_adma_clean_slot - clean up CDB slot (if ack is set)
1801 */
1802static int ppc440spe_adma_clean_slot(struct ppc440spe_adma_desc_slot *desc,
1803                struct ppc440spe_adma_chan *chan)
1804{
1805        /* the client is allowed to attach dependent operations
1806         * until 'ack' is set
1807         */
1808        if (!async_tx_test_ack(&desc->async_tx))
1809                return 0;
1810
1811        /* leave the last descriptor in the chain
1812         * so we can append to it
1813         */
1814        if (list_is_last(&desc->chain_node, &chan->chain) ||
1815            desc->phys == ppc440spe_chan_get_current_descriptor(chan))
1816                return 1;
1817
1818        if (chan->device->id != PPC440SPE_XOR_ID) {
1819                /* our DMA interrupt handler clears opc field of
1820                 * each processed descriptor. For all types of
1821                 * operations except for ZeroSum we do not actually
1822                 * need ack from the interrupt handler. ZeroSum is a
1823                 * special case since the result of this operation
1824                 * is available from the handler only, so if we see
1825                 * such type of descriptor (which is unprocessed yet)
1826                 * then leave it in chain.
1827                 */
1828                struct dma_cdb *cdb = desc->hw_desc;
1829                if (cdb->opc == DMA_CDB_OPC_DCHECK128)
1830                        return 1;
1831        }
1832
1833        dev_dbg(chan->device->common.dev, "\tfree slot %llx: %d stride: %d\n",
1834                desc->phys, desc->idx, desc->slots_per_op);
1835
1836        list_del(&desc->chain_node);
1837        ppc440spe_adma_free_slots(desc, chan);
1838        return 0;
1839}
1840
1841/**
1842 * __ppc440spe_adma_slot_cleanup - this is the common clean-up routine
1843 *      which runs through the channel CDBs list until reach the descriptor
1844 *      currently processed. When routine determines that all CDBs of group
1845 *      are completed then corresponding callbacks (if any) are called and slots
1846 *      are freed.
1847 */
1848static void __ppc440spe_adma_slot_cleanup(struct ppc440spe_adma_chan *chan)
1849{
1850        struct ppc440spe_adma_desc_slot *iter, *_iter, *group_start = NULL;
1851        dma_cookie_t cookie = 0;
1852        u32 current_desc = ppc440spe_chan_get_current_descriptor(chan);
1853        int busy = ppc440spe_chan_is_busy(chan);
1854        int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
1855
1856        dev_dbg(chan->device->common.dev, "ppc440spe adma%d: %s\n",
1857                chan->device->id, __func__);
1858
1859        if (!current_desc) {
1860                /*  There were no transactions yet, so
1861                 * nothing to clean
1862                 */
1863                return;
1864        }
1865
1866        /* free completed slots from the chain starting with
1867         * the oldest descriptor
1868         */
1869        list_for_each_entry_safe(iter, _iter, &chan->chain,
1870                                        chain_node) {
1871                dev_dbg(chan->device->common.dev, "\tcookie: %d slot: %d "
1872                    "busy: %d this_desc: %#llx next_desc: %#x "
1873                    "cur: %#x ack: %d\n",
1874                    iter->async_tx.cookie, iter->idx, busy, iter->phys,
1875                    ppc440spe_desc_get_link(iter, chan), current_desc,
1876                    async_tx_test_ack(&iter->async_tx));
1877                prefetch(_iter);
1878                prefetch(&_iter->async_tx);
1879
1880                /* do not advance past the current descriptor loaded into the
1881                 * hardware channel,subsequent descriptors are either in process
1882                 * or have not been submitted
1883                 */
1884                if (seen_current)
1885                        break;
1886
1887                /* stop the search if we reach the current descriptor and the
1888                 * channel is busy, or if it appears that the current descriptor
1889                 * needs to be re-read (i.e. has been appended to)
1890                 */
1891                if (iter->phys == current_desc) {
1892                        BUG_ON(seen_current++);
1893                        if (busy || ppc440spe_desc_get_link(iter, chan)) {
1894                                /* not all descriptors of the group have
1895                                 * been completed; exit.
1896                                 */
1897                                break;
1898                        }
1899                }
1900
1901                /* detect the start of a group transaction */
1902                if (!slot_cnt && !slots_per_op) {
1903                        slot_cnt = iter->slot_cnt;
1904                        slots_per_op = iter->slots_per_op;
1905                        if (slot_cnt <= slots_per_op) {
1906                                slot_cnt = 0;
1907                                slots_per_op = 0;
1908                        }
1909                }
1910
1911                if (slot_cnt) {
1912                        if (!group_start)
1913                                group_start = iter;
1914                        slot_cnt -= slots_per_op;
1915                }
1916
1917                /* all the members of a group are complete */
1918                if (slots_per_op != 0 && slot_cnt == 0) {
1919                        struct ppc440spe_adma_desc_slot *grp_iter, *_grp_iter;
1920                        int end_of_chain = 0;
1921
1922                        /* clean up the group */
1923                        slot_cnt = group_start->slot_cnt;
1924                        grp_iter = group_start;
1925                        list_for_each_entry_safe_from(grp_iter, _grp_iter,
1926                                &chan->chain, chain_node) {
1927
1928                                cookie = ppc440spe_adma_run_tx_complete_actions(
1929                                        grp_iter, chan, cookie);
1930
1931                                slot_cnt -= slots_per_op;
1932                                end_of_chain = ppc440spe_adma_clean_slot(
1933                                    grp_iter, chan);
1934                                if (end_of_chain && slot_cnt) {
1935                                        /* Should wait for ZeroSum completion */
1936                                        if (cookie > 0)
1937                                                chan->common.completed_cookie = cookie;
1938                                        return;
1939                                }
1940
1941                                if (slot_cnt == 0 || end_of_chain)
1942                                        break;
1943                        }
1944
1945                        /* the group should be complete at this point */
1946                        BUG_ON(slot_cnt);
1947
1948                        slots_per_op = 0;
1949                        group_start = NULL;
1950                        if (end_of_chain)
1951                                break;
1952                        else
1953                                continue;
1954                } else if (slots_per_op) /* wait for group completion */
1955                        continue;
1956
1957                cookie = ppc440spe_adma_run_tx_complete_actions(iter, chan,
1958                    cookie);
1959
1960                if (ppc440spe_adma_clean_slot(iter, chan))
1961                        break;
1962        }
1963
1964        BUG_ON(!seen_current);
1965
1966        if (cookie > 0) {
1967                chan->common.completed_cookie = cookie;
1968                pr_debug("\tcompleted cookie %d\n", cookie);
1969        }
1970
1971}
1972
1973/**
1974 * ppc440spe_adma_tasklet - clean up watch-dog initiator
1975 */
1976static void ppc440spe_adma_tasklet(unsigned long data)
1977{
1978        struct ppc440spe_adma_chan *chan = (struct ppc440spe_adma_chan *) data;
1979
1980        spin_lock_nested(&chan->lock, SINGLE_DEPTH_NESTING);
1981        __ppc440spe_adma_slot_cleanup(chan);
1982        spin_unlock(&chan->lock);
1983}
1984
1985/**
1986 * ppc440spe_adma_slot_cleanup - clean up scheduled initiator
1987 */
1988static void ppc440spe_adma_slot_cleanup(struct ppc440spe_adma_chan *chan)
1989{
1990        spin_lock_bh(&chan->lock);
1991        __ppc440spe_adma_slot_cleanup(chan);
1992        spin_unlock_bh(&chan->lock);
1993}
1994
1995/**
1996 * ppc440spe_adma_alloc_slots - allocate free slots (if any)
1997 */
1998static struct ppc440spe_adma_desc_slot *ppc440spe_adma_alloc_slots(
1999                struct ppc440spe_adma_chan *chan, int num_slots,
2000                int slots_per_op)
2001{
2002        struct ppc440spe_adma_desc_slot *iter = NULL, *_iter;
2003        struct ppc440spe_adma_desc_slot *alloc_start = NULL;
2004        struct list_head chain = LIST_HEAD_INIT(chain);
2005        int slots_found, retry = 0;
2006
2007
2008        BUG_ON(!num_slots || !slots_per_op);
2009        /* start search from the last allocated descrtiptor
2010         * if a contiguous allocation can not be found start searching
2011         * from the beginning of the list
2012         */
2013retry:
2014        slots_found = 0;
2015        if (retry == 0)
2016                iter = chan->last_used;
2017        else
2018                iter = list_entry(&chan->all_slots,
2019                                  struct ppc440spe_adma_desc_slot,
2020                                  slot_node);
2021        list_for_each_entry_safe_continue(iter, _iter, &chan->all_slots,
2022            slot_node) {
2023                prefetch(_iter);
2024                prefetch(&_iter->async_tx);
2025                if (iter->slots_per_op) {
2026                        slots_found = 0;
2027                        continue;
2028                }
2029
2030                /* start the allocation if the slot is correctly aligned */
2031                if (!slots_found++)
2032                        alloc_start = iter;
2033
2034                if (slots_found == num_slots) {
2035                        struct ppc440spe_adma_desc_slot *alloc_tail = NULL;
2036                        struct ppc440spe_adma_desc_slot *last_used = NULL;
2037
2038                        iter = alloc_start;
2039                        while (num_slots) {
2040                                int i;
2041                                /* pre-ack all but the last descriptor */
2042                                if (num_slots != slots_per_op)
2043                                        async_tx_ack(&iter->async_tx);
2044
2045                                list_add_tail(&iter->chain_node, &chain);
2046                                alloc_tail = iter;
2047                                iter->async_tx.cookie = 0;
2048                                iter->hw_next = NULL;
2049                                iter->flags = 0;
2050                                iter->slot_cnt = num_slots;
2051                                iter->xor_check_result = NULL;
2052                                for (i = 0; i < slots_per_op; i++) {
2053                                        iter->slots_per_op = slots_per_op - i;
2054                                        last_used = iter;
2055                                        iter = list_entry(iter->slot_node.next,
2056                                                struct ppc440spe_adma_desc_slot,
2057                                                slot_node);
2058                                }
2059                                num_slots -= slots_per_op;
2060                        }
2061                        alloc_tail->group_head = alloc_start;
2062                        alloc_tail->async_tx.cookie = -EBUSY;
2063                        list_splice(&chain, &alloc_tail->group_list);
2064                        chan->last_used = last_used;
2065                        return alloc_tail;
2066                }
2067        }
2068        if (!retry++)
2069                goto retry;
2070
2071        /* try to free some slots if the allocation fails */
2072        tasklet_schedule(&chan->irq_tasklet);
2073        return NULL;
2074}
2075
2076/**
2077 * ppc440spe_adma_alloc_chan_resources -  allocate pools for CDB slots
2078 */
2079static int ppc440spe_adma_alloc_chan_resources(struct dma_chan *chan)
2080{
2081        struct ppc440spe_adma_chan *ppc440spe_chan;
2082        struct ppc440spe_adma_desc_slot *slot = NULL;
2083        char *hw_desc;
2084        int i, db_sz;
2085        int init;
2086
2087        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2088        init = ppc440spe_chan->slots_allocated ? 0 : 1;
2089        chan->chan_id = ppc440spe_chan->device->id;
2090
2091        /* Allocate descriptor slots */
2092        i = ppc440spe_chan->slots_allocated;
2093        if (ppc440spe_chan->device->id != PPC440SPE_XOR_ID)
2094                db_sz = sizeof(struct dma_cdb);
2095        else
2096                db_sz = sizeof(struct xor_cb);
2097
2098        for (; i < (ppc440spe_chan->device->pool_size / db_sz); i++) {
2099                slot = kzalloc(sizeof(struct ppc440spe_adma_desc_slot),
2100                               GFP_KERNEL);
2101                if (!slot) {
2102                        printk(KERN_INFO "SPE ADMA Channel only initialized"
2103                                " %d descriptor slots", i--);
2104                        break;
2105                }
2106
2107                hw_desc = (char *) ppc440spe_chan->device->dma_desc_pool_virt;
2108                slot->hw_desc = (void *) &hw_desc[i * db_sz];
2109                dma_async_tx_descriptor_init(&slot->async_tx, chan);
2110                slot->async_tx.tx_submit = ppc440spe_adma_tx_submit;
2111                INIT_LIST_HEAD(&slot->chain_node);
2112                INIT_LIST_HEAD(&slot->slot_node);
2113                INIT_LIST_HEAD(&slot->group_list);
2114                slot->phys = ppc440spe_chan->device->dma_desc_pool + i * db_sz;
2115                slot->idx = i;
2116
2117                spin_lock_bh(&ppc440spe_chan->lock);
2118                ppc440spe_chan->slots_allocated++;
2119                list_add_tail(&slot->slot_node, &ppc440spe_chan->all_slots);
2120                spin_unlock_bh(&ppc440spe_chan->lock);
2121        }
2122
2123        if (i && !ppc440spe_chan->last_used) {
2124                ppc440spe_chan->last_used =
2125                        list_entry(ppc440spe_chan->all_slots.next,
2126                                struct ppc440spe_adma_desc_slot,
2127                                slot_node);
2128        }
2129
2130        dev_dbg(ppc440spe_chan->device->common.dev,
2131                "ppc440spe adma%d: allocated %d descriptor slots\n",
2132                ppc440spe_chan->device->id, i);
2133
2134        /* initialize the channel and the chain with a null operation */
2135        if (init) {
2136                switch (ppc440spe_chan->device->id) {
2137                case PPC440SPE_DMA0_ID:
2138                case PPC440SPE_DMA1_ID:
2139                        ppc440spe_chan->hw_chain_inited = 0;
2140                        /* Use WXOR for self-testing */
2141                        if (!ppc440spe_r6_tchan)
2142                                ppc440spe_r6_tchan = ppc440spe_chan;
2143                        break;
2144                case PPC440SPE_XOR_ID:
2145                        ppc440spe_chan_start_null_xor(ppc440spe_chan);
2146                        break;
2147                default:
2148                        BUG();
2149                }
2150                ppc440spe_chan->needs_unmap = 1;
2151        }
2152
2153        return (i > 0) ? i : -ENOMEM;
2154}
2155
2156/**
2157 * ppc440spe_rxor_set_region_data -
2158 */
2159static void ppc440spe_rxor_set_region(struct ppc440spe_adma_desc_slot *desc,
2160        u8 xor_arg_no, u32 mask)
2161{
2162        struct xor_cb *xcb = desc->hw_desc;
2163
2164        xcb->ops[xor_arg_no].h |= mask;
2165}
2166
2167/**
2168 * ppc440spe_rxor_set_src -
2169 */
2170static void ppc440spe_rxor_set_src(struct ppc440spe_adma_desc_slot *desc,
2171        u8 xor_arg_no, dma_addr_t addr)
2172{
2173        struct xor_cb *xcb = desc->hw_desc;
2174
2175        xcb->ops[xor_arg_no].h |= DMA_CUED_XOR_BASE;
2176        xcb->ops[xor_arg_no].l = addr;
2177}
2178
2179/**
2180 * ppc440spe_rxor_set_mult -
2181 */
2182static void ppc440spe_rxor_set_mult(struct ppc440spe_adma_desc_slot *desc,
2183        u8 xor_arg_no, u8 idx, u8 mult)
2184{
2185        struct xor_cb *xcb = desc->hw_desc;
2186
2187        xcb->ops[xor_arg_no].h |= mult << (DMA_CUED_MULT1_OFF + idx * 8);
2188}
2189
2190/**
2191 * ppc440spe_adma_check_threshold - append CDBs to h/w chain if threshold
2192 *      has been achieved
2193 */
2194static void ppc440spe_adma_check_threshold(struct ppc440spe_adma_chan *chan)
2195{
2196        dev_dbg(chan->device->common.dev, "ppc440spe adma%d: pending: %d\n",
2197                chan->device->id, chan->pending);
2198
2199        if (chan->pending >= PPC440SPE_ADMA_THRESHOLD) {
2200                chan->pending = 0;
2201                ppc440spe_chan_append(chan);
2202        }
2203}
2204
2205/**
2206 * ppc440spe_adma_tx_submit - submit new descriptor group to the channel
2207 *      (it's not necessary that descriptors will be submitted to the h/w
2208 *      chains too right now)
2209 */
2210static dma_cookie_t ppc440spe_adma_tx_submit(struct dma_async_tx_descriptor *tx)
2211{
2212        struct ppc440spe_adma_desc_slot *sw_desc;
2213        struct ppc440spe_adma_chan *chan = to_ppc440spe_adma_chan(tx->chan);
2214        struct ppc440spe_adma_desc_slot *group_start, *old_chain_tail;
2215        int slot_cnt;
2216        int slots_per_op;
2217        dma_cookie_t cookie;
2218
2219        sw_desc = tx_to_ppc440spe_adma_slot(tx);
2220
2221        group_start = sw_desc->group_head;
2222        slot_cnt = group_start->slot_cnt;
2223        slots_per_op = group_start->slots_per_op;
2224
2225        spin_lock_bh(&chan->lock);
2226        cookie = dma_cookie_assign(tx);
2227
2228        if (unlikely(list_empty(&chan->chain))) {
2229                /* first peer */
2230                list_splice_init(&sw_desc->group_list, &chan->chain);
2231                chan_first_cdb[chan->device->id] = group_start;
2232        } else {
2233                /* isn't first peer, bind CDBs to chain */
2234                old_chain_tail = list_entry(chan->chain.prev,
2235                                        struct ppc440spe_adma_desc_slot,
2236                                        chain_node);
2237                list_splice_init(&sw_desc->group_list,
2238                    &old_chain_tail->chain_node);
2239                /* fix up the hardware chain */
2240                ppc440spe_desc_set_link(chan, old_chain_tail, group_start);
2241        }
2242
2243        /* increment the pending count by the number of operations */
2244        chan->pending += slot_cnt / slots_per_op;
2245        ppc440spe_adma_check_threshold(chan);
2246        spin_unlock_bh(&chan->lock);
2247
2248        dev_dbg(chan->device->common.dev,
2249                "ppc440spe adma%d: %s cookie: %d slot: %d tx %p\n",
2250                chan->device->id, __func__,
2251                sw_desc->async_tx.cookie, sw_desc->idx, sw_desc);
2252
2253        return cookie;
2254}
2255
2256/**
2257 * ppc440spe_adma_prep_dma_interrupt - prepare CDB for a pseudo DMA operation
2258 */
2259static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_interrupt(
2260                struct dma_chan *chan, unsigned long flags)
2261{
2262        struct ppc440spe_adma_chan *ppc440spe_chan;
2263        struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
2264        int slot_cnt, slots_per_op;
2265
2266        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2267
2268        dev_dbg(ppc440spe_chan->device->common.dev,
2269                "ppc440spe adma%d: %s\n", ppc440spe_chan->device->id,
2270                __func__);
2271
2272        spin_lock_bh(&ppc440spe_chan->lock);
2273        slot_cnt = slots_per_op = 1;
2274        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
2275                        slots_per_op);
2276        if (sw_desc) {
2277                group_start = sw_desc->group_head;
2278                ppc440spe_desc_init_interrupt(group_start, ppc440spe_chan);
2279                group_start->unmap_len = 0;
2280                sw_desc->async_tx.flags = flags;
2281        }
2282        spin_unlock_bh(&ppc440spe_chan->lock);
2283
2284        return sw_desc ? &sw_desc->async_tx : NULL;
2285}
2286
2287/**
2288 * ppc440spe_adma_prep_dma_memcpy - prepare CDB for a MEMCPY operation
2289 */
2290static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_memcpy(
2291                struct dma_chan *chan, dma_addr_t dma_dest,
2292                dma_addr_t dma_src, size_t len, unsigned long flags)
2293{
2294        struct ppc440spe_adma_chan *ppc440spe_chan;
2295        struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
2296        int slot_cnt, slots_per_op;
2297
2298        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2299
2300        if (unlikely(!len))
2301                return NULL;
2302
2303        BUG_ON(len > PPC440SPE_ADMA_DMA_MAX_BYTE_COUNT);
2304
2305        spin_lock_bh(&ppc440spe_chan->lock);
2306
2307        dev_dbg(ppc440spe_chan->device->common.dev,
2308                "ppc440spe adma%d: %s len: %u int_en %d\n",
2309                ppc440spe_chan->device->id, __func__, len,
2310                flags & DMA_PREP_INTERRUPT ? 1 : 0);
2311        slot_cnt = slots_per_op = 1;
2312        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
2313                slots_per_op);
2314        if (sw_desc) {
2315                group_start = sw_desc->group_head;
2316                ppc440spe_desc_init_memcpy(group_start, flags);
2317                ppc440spe_adma_set_dest(group_start, dma_dest, 0);
2318                ppc440spe_adma_memcpy_xor_set_src(group_start, dma_src, 0);
2319                ppc440spe_desc_set_byte_count(group_start, ppc440spe_chan, len);
2320                sw_desc->unmap_len = len;
2321                sw_desc->async_tx.flags = flags;
2322        }
2323        spin_unlock_bh(&ppc440spe_chan->lock);
2324
2325        return sw_desc ? &sw_desc->async_tx : NULL;
2326}
2327
2328/**
2329 * ppc440spe_adma_prep_dma_memset - prepare CDB for a MEMSET operation
2330 */
2331static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_memset(
2332                struct dma_chan *chan, dma_addr_t dma_dest, int value,
2333                size_t len, unsigned long flags)
2334{
2335        struct ppc440spe_adma_chan *ppc440spe_chan;
2336        struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
2337        int slot_cnt, slots_per_op;
2338
2339        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2340
2341        if (unlikely(!len))
2342                return NULL;
2343
2344        BUG_ON(len > PPC440SPE_ADMA_DMA_MAX_BYTE_COUNT);
2345
2346        spin_lock_bh(&ppc440spe_chan->lock);
2347
2348        dev_dbg(ppc440spe_chan->device->common.dev,
2349                "ppc440spe adma%d: %s cal: %u len: %u int_en %d\n",
2350                ppc440spe_chan->device->id, __func__, value, len,
2351                flags & DMA_PREP_INTERRUPT ? 1 : 0);
2352
2353        slot_cnt = slots_per_op = 1;
2354        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
2355                slots_per_op);
2356        if (sw_desc) {
2357                group_start = sw_desc->group_head;
2358                ppc440spe_desc_init_memset(group_start, value, flags);
2359                ppc440spe_adma_set_dest(group_start, dma_dest, 0);
2360                ppc440spe_desc_set_byte_count(group_start, ppc440spe_chan, len);
2361                sw_desc->unmap_len = len;
2362                sw_desc->async_tx.flags = flags;
2363        }
2364        spin_unlock_bh(&ppc440spe_chan->lock);
2365
2366        return sw_desc ? &sw_desc->async_tx : NULL;
2367}
2368
2369/**
2370 * ppc440spe_adma_prep_dma_xor - prepare CDB for a XOR operation
2371 */
2372static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_xor(
2373                struct dma_chan *chan, dma_addr_t dma_dest,
2374                dma_addr_t *dma_src, u32 src_cnt, size_t len,
2375                unsigned long flags)
2376{
2377        struct ppc440spe_adma_chan *ppc440spe_chan;
2378        struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
2379        int slot_cnt, slots_per_op;
2380
2381        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2382
2383        ADMA_LL_DBG(prep_dma_xor_dbg(ppc440spe_chan->device->id,
2384                                     dma_dest, dma_src, src_cnt));
2385        if (unlikely(!len))
2386                return NULL;
2387        BUG_ON(len > PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
2388
2389        dev_dbg(ppc440spe_chan->device->common.dev,
2390                "ppc440spe adma%d: %s src_cnt: %d len: %u int_en: %d\n",
2391                ppc440spe_chan->device->id, __func__, src_cnt, len,
2392                flags & DMA_PREP_INTERRUPT ? 1 : 0);
2393
2394        spin_lock_bh(&ppc440spe_chan->lock);
2395        slot_cnt = ppc440spe_chan_xor_slot_count(len, src_cnt, &slots_per_op);
2396        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
2397                        slots_per_op);
2398        if (sw_desc) {
2399                group_start = sw_desc->group_head;
2400                ppc440spe_desc_init_xor(group_start, src_cnt, flags);
2401                ppc440spe_adma_set_dest(group_start, dma_dest, 0);
2402                while (src_cnt--)
2403                        ppc440spe_adma_memcpy_xor_set_src(group_start,
2404                                dma_src[src_cnt], src_cnt);
2405                ppc440spe_desc_set_byte_count(group_start, ppc440spe_chan, len);
2406                sw_desc->unmap_len = len;
2407                sw_desc->async_tx.flags = flags;
2408        }
2409        spin_unlock_bh(&ppc440spe_chan->lock);
2410
2411        return sw_desc ? &sw_desc->async_tx : NULL;
2412}
2413
2414static inline void
2415ppc440spe_desc_set_xor_src_cnt(struct ppc440spe_adma_desc_slot *desc,
2416                                int src_cnt);
2417static void ppc440spe_init_rxor_cursor(struct ppc440spe_rxor *cursor);
2418
2419/**
2420 * ppc440spe_adma_init_dma2rxor_slot -
2421 */
2422static void ppc440spe_adma_init_dma2rxor_slot(
2423                struct ppc440spe_adma_desc_slot *desc,
2424                dma_addr_t *src, int src_cnt)
2425{
2426        int i;
2427
2428        /* initialize CDB */
2429        for (i = 0; i < src_cnt; i++) {
2430                ppc440spe_adma_dma2rxor_prep_src(desc, &desc->rxor_cursor, i,
2431                                                 desc->src_cnt, (u32)src[i]);
2432        }
2433}
2434
2435/**
2436 * ppc440spe_dma01_prep_mult -
2437 * for Q operation where destination is also the source
2438 */
2439static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_mult(
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)
2443{
2444        struct ppc440spe_adma_desc_slot *sw_desc = NULL;
2445        unsigned long op = 0;
2446        int slot_cnt;
2447
2448        set_bit(PPC440SPE_DESC_WXOR, &op);
2449        slot_cnt = 2;
2450
2451        spin_lock_bh(&ppc440spe_chan->lock);
2452
2453        /* use WXOR, each descriptor occupies one slot */
2454        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
2455        if (sw_desc) {
2456                struct ppc440spe_adma_chan *chan;
2457                struct ppc440spe_adma_desc_slot *iter;
2458                struct dma_cdb *hw_desc;
2459
2460                chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
2461                set_bits(op, &sw_desc->flags);
2462                sw_desc->src_cnt = src_cnt;
2463                sw_desc->dst_cnt = dst_cnt;
2464                /* First descriptor, zero data in the destination and copy it
2465                 * to q page using MULTICAST transfer.
2466                 */
2467                iter = list_first_entry(&sw_desc->group_list,
2468                                        struct ppc440spe_adma_desc_slot,
2469                                        chain_node);
2470                memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2471                /* set 'next' pointer */
2472                iter->hw_next = list_entry(iter->chain_node.next,
2473                                           struct ppc440spe_adma_desc_slot,
2474                                           chain_node);
2475                clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2476                hw_desc = iter->hw_desc;
2477                hw_desc->opc = DMA_CDB_OPC_MULTICAST;
2478
2479                ppc440spe_desc_set_dest_addr(iter, chan,
2480                                             DMA_CUED_XOR_BASE, dst[0], 0);
2481                ppc440spe_desc_set_dest_addr(iter, chan, 0, dst[1], 1);
2482                ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
2483                                            src[0]);
2484                ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2485                iter->unmap_len = len;
2486
2487                /*
2488                 * Second descriptor, multiply data from the q page
2489                 * and store the result in real destination.
2490                 */
2491                iter = list_first_entry(&iter->chain_node,
2492                                        struct ppc440spe_adma_desc_slot,
2493                                        chain_node);
2494                memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2495                iter->hw_next = NULL;
2496                if (flags & DMA_PREP_INTERRUPT)
2497                        set_bit(PPC440SPE_DESC_INT, &iter->flags);
2498                else
2499                        clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2500
2501                hw_desc = iter->hw_desc;
2502                hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
2503                ppc440spe_desc_set_src_addr(iter, chan, 0,
2504                                            DMA_CUED_XOR_HB, dst[1]);
2505                ppc440spe_desc_set_dest_addr(iter, chan,
2506                                             DMA_CUED_XOR_BASE, dst[0], 0);
2507
2508                ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
2509                                            DMA_CDB_SG_DST1, scf[0]);
2510                ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2511                iter->unmap_len = len;
2512                sw_desc->async_tx.flags = flags;
2513        }
2514
2515        spin_unlock_bh(&ppc440spe_chan->lock);
2516
2517        return sw_desc;
2518}
2519
2520/**
2521 * ppc440spe_dma01_prep_sum_product -
2522 * Dx = A*(P+Pxy) + B*(Q+Qxy) operation where destination is also
2523 * the source.
2524 */
2525static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_sum_product(
2526                struct ppc440spe_adma_chan *ppc440spe_chan,
2527                dma_addr_t *dst, dma_addr_t *src, int src_cnt,
2528                const unsigned char *scf, size_t len, unsigned long flags)
2529{
2530        struct ppc440spe_adma_desc_slot *sw_desc = NULL;
2531        unsigned long op = 0;
2532        int slot_cnt;
2533
2534        set_bit(PPC440SPE_DESC_WXOR, &op);
2535        slot_cnt = 3;
2536
2537        spin_lock_bh(&ppc440spe_chan->lock);
2538
2539        /* WXOR, each descriptor occupies one slot */
2540        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
2541        if (sw_desc) {
2542                struct ppc440spe_adma_chan *chan;
2543                struct ppc440spe_adma_desc_slot *iter;
2544                struct dma_cdb *hw_desc;
2545
2546                chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
2547                set_bits(op, &sw_desc->flags);
2548                sw_desc->src_cnt = src_cnt;
2549                sw_desc->dst_cnt = 1;
2550                /* 1st descriptor, src[1] data to q page and zero destination */
2551                iter = list_first_entry(&sw_desc->group_list,
2552                                        struct ppc440spe_adma_desc_slot,
2553                                        chain_node);
2554                memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2555                iter->hw_next = list_entry(iter->chain_node.next,
2556                                           struct ppc440spe_adma_desc_slot,
2557                                           chain_node);
2558                clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2559                hw_desc = iter->hw_desc;
2560                hw_desc->opc = DMA_CDB_OPC_MULTICAST;
2561
2562                ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
2563                                             *dst, 0);
2564                ppc440spe_desc_set_dest_addr(iter, chan, 0,
2565                                             ppc440spe_chan->qdest, 1);
2566                ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
2567                                            src[1]);
2568                ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2569                iter->unmap_len = len;
2570
2571                /* 2nd descriptor, multiply src[1] data and store the
2572                 * result in destination */
2573                iter = list_first_entry(&iter->chain_node,
2574                                        struct ppc440spe_adma_desc_slot,
2575                                        chain_node);
2576                memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2577                /* set 'next' pointer */
2578                iter->hw_next = list_entry(iter->chain_node.next,
2579                                           struct ppc440spe_adma_desc_slot,
2580                                           chain_node);
2581                if (flags & DMA_PREP_INTERRUPT)
2582                        set_bit(PPC440SPE_DESC_INT, &iter->flags);
2583                else
2584                        clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2585
2586                hw_desc = iter->hw_desc;
2587                hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
2588                ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
2589                                            ppc440spe_chan->qdest);
2590                ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
2591                                             *dst, 0);
2592                ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
2593                                            DMA_CDB_SG_DST1, scf[1]);
2594                ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2595                iter->unmap_len = len;
2596
2597                /*
2598                 * 3rd descriptor, multiply src[0] data and xor it
2599                 * with destination
2600                 */
2601                iter = list_first_entry(&iter->chain_node,
2602                                        struct ppc440spe_adma_desc_slot,
2603                                        chain_node);
2604                memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2605                iter->hw_next = NULL;
2606                if (flags & DMA_PREP_INTERRUPT)
2607                        set_bit(PPC440SPE_DESC_INT, &iter->flags);
2608                else
2609                        clear_bit(PPC440SPE_DESC_INT, &iter->flags);
2610
2611                hw_desc = iter->hw_desc;
2612                hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
2613                ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB,
2614                                            src[0]);
2615                ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE,
2616                                             *dst, 0);
2617                ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
2618                                            DMA_CDB_SG_DST1, scf[0]);
2619                ppc440spe_desc_set_byte_count(iter, ppc440spe_chan, len);
2620                iter->unmap_len = len;
2621                sw_desc->async_tx.flags = flags;
2622        }
2623
2624        spin_unlock_bh(&ppc440spe_chan->lock);
2625
2626        return sw_desc;
2627}
2628
2629static struct ppc440spe_adma_desc_slot *ppc440spe_dma01_prep_pq(
2630                struct ppc440spe_adma_chan *ppc440spe_chan,
2631                dma_addr_t *dst, int dst_cnt, dma_addr_t *src, int src_cnt,
2632                const unsigned char *scf, size_t len, unsigned long flags)
2633{
2634        int slot_cnt;
2635        struct ppc440spe_adma_desc_slot *sw_desc = NULL, *iter;
2636        unsigned long op = 0;
2637        unsigned char mult = 1;
2638
2639        pr_debug("%s: dst_cnt %d, src_cnt %d, len %d\n",
2640                 __func__, dst_cnt, src_cnt, len);
2641        /*  select operations WXOR/RXOR depending on the
2642         * source addresses of operators and the number
2643         * of destinations (RXOR support only Q-parity calculations)
2644         */
2645        set_bit(PPC440SPE_DESC_WXOR, &op);
2646        if (!test_and_set_bit(PPC440SPE_RXOR_RUN, &ppc440spe_rxor_state)) {
2647                /* no active RXOR;
2648                 * do RXOR if:
2649                 * - there are more than 1 source,
2650                 * - len is aligned on 512-byte boundary,
2651                 * - source addresses fit to one of 4 possible regions.
2652                 */
2653                if (src_cnt > 1 &&
2654                    !(len & MQ0_CF2H_RXOR_BS_MASK) &&
2655                    (src[0] + len) == src[1]) {
2656                        /* may do RXOR R1 R2 */
2657                        set_bit(PPC440SPE_DESC_RXOR, &op);
2658                        if (src_cnt != 2) {
2659                                /* may try to enhance region of RXOR */
2660                                if ((src[1] + len) == src[2]) {
2661                                        /* do RXOR R1 R2 R3 */
2662                                        set_bit(PPC440SPE_DESC_RXOR123,
2663                                                &op);
2664                                } else if ((src[1] + len * 2) == src[2]) {
2665                                        /* do RXOR R1 R2 R4 */
2666                                        set_bit(PPC440SPE_DESC_RXOR124, &op);
2667                                } else if ((src[1] + len * 3) == src[2]) {
2668                                        /* do RXOR R1 R2 R5 */
2669                                        set_bit(PPC440SPE_DESC_RXOR125,
2670                                                &op);
2671                                } else {
2672                                        /* do RXOR R1 R2 */
2673                                        set_bit(PPC440SPE_DESC_RXOR12,
2674                                                &op);
2675                                }
2676                        } else {
2677                                /* do RXOR R1 R2 */
2678                                set_bit(PPC440SPE_DESC_RXOR12, &op);
2679                        }
2680                }
2681
2682                if (!test_bit(PPC440SPE_DESC_RXOR, &op)) {
2683                        /* can not do this operation with RXOR */
2684                        clear_bit(PPC440SPE_RXOR_RUN,
2685                                &ppc440spe_rxor_state);
2686                } else {
2687                        /* can do; set block size right now */
2688                        ppc440spe_desc_set_rxor_block_size(len);
2689                }
2690        }
2691
2692        /* Number of necessary slots depends on operation type selected */
2693        if (!test_bit(PPC440SPE_DESC_RXOR, &op)) {
2694                /*  This is a WXOR only chain. Need descriptors for each
2695                 * source to GF-XOR them with WXOR, and need descriptors
2696                 * for each destination to zero them with WXOR
2697                 */
2698                slot_cnt = src_cnt;
2699
2700                if (flags & DMA_PREP_ZERO_P) {
2701                        slot_cnt++;
2702                        set_bit(PPC440SPE_ZERO_P, &op);
2703                }
2704                if (flags & DMA_PREP_ZERO_Q) {
2705                        slot_cnt++;
2706                        set_bit(PPC440SPE_ZERO_Q, &op);
2707                }
2708        } else {
2709                /*  Need 1/2 descriptor for RXOR operation, and
2710                 * need (src_cnt - (2 or 3)) for WXOR of sources
2711                 * remained (if any)
2712                 */
2713                slot_cnt = dst_cnt;
2714
2715                if (flags & DMA_PREP_ZERO_P)
2716                        set_bit(PPC440SPE_ZERO_P, &op);
2717                if (flags & DMA_PREP_ZERO_Q)
2718                        set_bit(PPC440SPE_ZERO_Q, &op);
2719
2720                if (test_bit(PPC440SPE_DESC_RXOR12, &op))
2721                        slot_cnt += src_cnt - 2;
2722                else
2723                        slot_cnt += src_cnt - 3;
2724
2725                /*  Thus we have either RXOR only chain or
2726                 * mixed RXOR/WXOR
2727                 */
2728                if (slot_cnt == dst_cnt)
2729                        /* RXOR only chain */
2730                        clear_bit(PPC440SPE_DESC_WXOR, &op);
2731        }
2732
2733        spin_lock_bh(&ppc440spe_chan->lock);
2734        /* for both RXOR/WXOR each descriptor occupies one slot */
2735        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
2736        if (sw_desc) {
2737                ppc440spe_desc_init_dma01pq(sw_desc, dst_cnt, src_cnt,
2738                                flags, op);
2739
2740                /* setup dst/src/mult */
2741                pr_debug("%s: set dst descriptor 0, 1: 0x%016llx, 0x%016llx\n",
2742                         __func__, dst[0], dst[1]);
2743                ppc440spe_adma_pq_set_dest(sw_desc, dst, flags);
2744                while (src_cnt--) {
2745                        ppc440spe_adma_pq_set_src(sw_desc, src[src_cnt],
2746                                                  src_cnt);
2747
2748                        /* NOTE: "Multi = 0 is equivalent to = 1" as it
2749                         * stated in 440SPSPe_RAID6_Addendum_UM_1_17.pdf
2750                         * doesn't work for RXOR with DMA0/1! Instead, multi=0
2751                         * leads to zeroing source data after RXOR.
2752                         * So, for P case set-up mult=1 explicitly.
2753                         */
2754                        if (!(flags & DMA_PREP_PQ_DISABLE_Q))
2755                                mult = scf[src_cnt];
2756                        ppc440spe_adma_pq_set_src_mult(sw_desc,
2757                                mult, src_cnt,  dst_cnt - 1);
2758                }
2759
2760                /* Setup byte count foreach slot just allocated */
2761                sw_desc->async_tx.flags = flags;
2762                list_for_each_entry(iter, &sw_desc->group_list,
2763                                chain_node) {
2764                        ppc440spe_desc_set_byte_count(iter,
2765                                ppc440spe_chan, len);
2766                        iter->unmap_len = len;
2767                }
2768        }
2769        spin_unlock_bh(&ppc440spe_chan->lock);
2770
2771        return sw_desc;
2772}
2773
2774static struct ppc440spe_adma_desc_slot *ppc440spe_dma2_prep_pq(
2775                struct ppc440spe_adma_chan *ppc440spe_chan,
2776                dma_addr_t *dst, int dst_cnt, dma_addr_t *src, int src_cnt,
2777                const unsigned char *scf, size_t len, unsigned long flags)
2778{
2779        int slot_cnt, descs_per_op;
2780        struct ppc440spe_adma_desc_slot *sw_desc = NULL, *iter;
2781        unsigned long op = 0;
2782        unsigned char mult = 1;
2783
2784        BUG_ON(!dst_cnt);
2785        /*pr_debug("%s: dst_cnt %d, src_cnt %d, len %d\n",
2786                 __func__, dst_cnt, src_cnt, len);*/
2787
2788        spin_lock_bh(&ppc440spe_chan->lock);
2789        descs_per_op = ppc440spe_dma2_pq_slot_count(src, src_cnt, len);
2790        if (descs_per_op < 0) {
2791                spin_unlock_bh(&ppc440spe_chan->lock);
2792                return NULL;
2793        }
2794
2795        /* depending on number of sources we have 1 or 2 RXOR chains */
2796        slot_cnt = descs_per_op * dst_cnt;
2797
2798        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt, 1);
2799        if (sw_desc) {
2800                op = slot_cnt;
2801                sw_desc->async_tx.flags = flags;
2802                list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
2803                        ppc440spe_desc_init_dma2pq(iter, dst_cnt, src_cnt,
2804                                --op ? 0 : flags);
2805                        ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
2806                                len);
2807                        iter->unmap_len = len;
2808
2809                        ppc440spe_init_rxor_cursor(&(iter->rxor_cursor));
2810                        iter->rxor_cursor.len = len;
2811                        iter->descs_per_op = descs_per_op;
2812                }
2813                op = 0;
2814                list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
2815                        op++;
2816                        if (op % descs_per_op == 0)
2817                                ppc440spe_adma_init_dma2rxor_slot(iter, src,
2818                                                                  src_cnt);
2819                        if (likely(!list_is_last(&iter->chain_node,
2820                                                 &sw_desc->group_list))) {
2821                                /* set 'next' pointer */
2822                                iter->hw_next =
2823                                        list_entry(iter->chain_node.next,
2824                                                struct ppc440spe_adma_desc_slot,
2825                                                chain_node);
2826                                ppc440spe_xor_set_link(iter, iter->hw_next);
2827                        } else {
2828                                /* this is the last descriptor. */
2829                                iter->hw_next = NULL;
2830                        }
2831                }
2832
2833                /* fixup head descriptor */
2834                sw_desc->dst_cnt = dst_cnt;
2835                if (flags & DMA_PREP_ZERO_P)
2836                        set_bit(PPC440SPE_ZERO_P, &sw_desc->flags);
2837                if (flags & DMA_PREP_ZERO_Q)
2838                        set_bit(PPC440SPE_ZERO_Q, &sw_desc->flags);
2839
2840                /* setup dst/src/mult */
2841                ppc440spe_adma_pq_set_dest(sw_desc, dst, flags);
2842
2843                while (src_cnt--) {
2844                        /* handle descriptors (if dst_cnt == 2) inside
2845                         * the ppc440spe_adma_pq_set_srcxxx() functions
2846                         */
2847                        ppc440spe_adma_pq_set_src(sw_desc, src[src_cnt],
2848                                                  src_cnt);
2849                        if (!(flags & DMA_PREP_PQ_DISABLE_Q))
2850                                mult = scf[src_cnt];
2851                        ppc440spe_adma_pq_set_src_mult(sw_desc,
2852                                        mult, src_cnt, dst_cnt - 1);
2853                }
2854        }
2855        spin_unlock_bh(&ppc440spe_chan->lock);
2856        ppc440spe_desc_set_rxor_block_size(len);
2857        return sw_desc;
2858}
2859
2860/**
2861 * ppc440spe_adma_prep_dma_pq - prepare CDB (group) for a GF-XOR operation
2862 */
2863static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_pq(
2864                struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
2865                unsigned int src_cnt, const unsigned char *scf,
2866                size_t len, unsigned long flags)
2867{
2868        struct ppc440spe_adma_chan *ppc440spe_chan;
2869        struct ppc440spe_adma_desc_slot *sw_desc = NULL;
2870        int dst_cnt = 0;
2871
2872        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2873
2874        ADMA_LL_DBG(prep_dma_pq_dbg(ppc440spe_chan->device->id,
2875                                    dst, src, src_cnt));
2876        BUG_ON(!len);
2877        BUG_ON(len > PPC440SPE_ADMA_XOR_MAX_BYTE_COUNT);
2878        BUG_ON(!src_cnt);
2879
2880        if (src_cnt == 1 && dst[1] == src[0]) {
2881                dma_addr_t dest[2];
2882
2883                /* dst[1] is real destination (Q) */
2884                dest[0] = dst[1];
2885                /* this is the page to multicast source data to */
2886                dest[1] = ppc440spe_chan->qdest;
2887                sw_desc = ppc440spe_dma01_prep_mult(ppc440spe_chan,
2888                                dest, 2, src, src_cnt, scf, len, flags);
2889                return sw_desc ? &sw_desc->async_tx : NULL;
2890        }
2891
2892        if (src_cnt == 2 && dst[1] == src[1]) {
2893                sw_desc = ppc440spe_dma01_prep_sum_product(ppc440spe_chan,
2894                                        &dst[1], src, 2, scf, len, flags);
2895                return sw_desc ? &sw_desc->async_tx : NULL;
2896        }
2897
2898        if (!(flags & DMA_PREP_PQ_DISABLE_P)) {
2899                BUG_ON(!dst[0]);
2900                dst_cnt++;
2901                flags |= DMA_PREP_ZERO_P;
2902        }
2903
2904        if (!(flags & DMA_PREP_PQ_DISABLE_Q)) {
2905                BUG_ON(!dst[1]);
2906                dst_cnt++;
2907                flags |= DMA_PREP_ZERO_Q;
2908        }
2909
2910        BUG_ON(!dst_cnt);
2911
2912        dev_dbg(ppc440spe_chan->device->common.dev,
2913                "ppc440spe adma%d: %s src_cnt: %d len: %u int_en: %d\n",
2914                ppc440spe_chan->device->id, __func__, src_cnt, len,
2915                flags & DMA_PREP_INTERRUPT ? 1 : 0);
2916
2917        switch (ppc440spe_chan->device->id) {
2918        case PPC440SPE_DMA0_ID:
2919        case PPC440SPE_DMA1_ID:
2920                sw_desc = ppc440spe_dma01_prep_pq(ppc440spe_chan,
2921                                dst, dst_cnt, src, src_cnt, scf,
2922                                len, flags);
2923                break;
2924
2925        case PPC440SPE_XOR_ID:
2926                sw_desc = ppc440spe_dma2_prep_pq(ppc440spe_chan,
2927                                dst, dst_cnt, src, src_cnt, scf,
2928                                len, flags);
2929                break;
2930        }
2931
2932        return sw_desc ? &sw_desc->async_tx : NULL;
2933}
2934
2935/**
2936 * ppc440spe_adma_prep_dma_pqzero_sum - prepare CDB group for
2937 * a PQ_ZERO_SUM operation
2938 */
2939static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_pqzero_sum(
2940                struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
2941                unsigned int src_cnt, const unsigned char *scf, size_t len,
2942                enum sum_check_flags *pqres, unsigned long flags)
2943{
2944        struct ppc440spe_adma_chan *ppc440spe_chan;
2945        struct ppc440spe_adma_desc_slot *sw_desc, *iter;
2946        dma_addr_t pdest, qdest;
2947        int slot_cnt, slots_per_op, idst, dst_cnt;
2948
2949        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
2950
2951        if (flags & DMA_PREP_PQ_DISABLE_P)
2952                pdest = 0;
2953        else
2954                pdest = pq[0];
2955
2956        if (flags & DMA_PREP_PQ_DISABLE_Q)
2957                qdest = 0;
2958        else
2959                qdest = pq[1];
2960
2961        ADMA_LL_DBG(prep_dma_pqzero_sum_dbg(ppc440spe_chan->device->id,
2962                                            src, src_cnt, scf));
2963
2964        /* Always use WXOR for P/Q calculations (two destinations).
2965         * Need 1 or 2 extra slots to verify results are zero.
2966         */
2967        idst = dst_cnt = (pdest && qdest) ? 2 : 1;
2968
2969        /* One additional slot per destination to clone P/Q
2970         * before calculation (we have to preserve destinations).
2971         */
2972        slot_cnt = src_cnt + dst_cnt * 2;
2973        slots_per_op = 1;
2974
2975        spin_lock_bh(&ppc440spe_chan->lock);
2976        sw_desc = ppc440spe_adma_alloc_slots(ppc440spe_chan, slot_cnt,
2977                                             slots_per_op);
2978        if (sw_desc) {
2979                ppc440spe_desc_init_dma01pqzero_sum(sw_desc, dst_cnt, src_cnt);
2980
2981                /* Setup byte count for each slot just allocated */
2982                sw_desc->async_tx.flags = flags;
2983                list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
2984                        ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
2985                                                      len);
2986                        iter->unmap_len = len;
2987                }
2988
2989                if (pdest) {
2990                        struct dma_cdb *hw_desc;
2991                        struct ppc440spe_adma_chan *chan;
2992
2993                        iter = sw_desc->group_head;
2994                        chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
2995                        memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
2996                        iter->hw_next = list_entry(iter->chain_node.next,
2997                                                struct ppc440spe_adma_desc_slot,
2998                                                chain_node);
2999                        hw_desc = iter->hw_desc;
3000                        hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
3001                        iter->src_cnt = 0;
3002                        iter->dst_cnt = 0;
3003                        ppc440spe_desc_set_dest_addr(iter, chan, 0,
3004                                                     ppc440spe_chan->pdest, 0);
3005                        ppc440spe_desc_set_src_addr(iter, chan, 0, 0, pdest);
3006                        ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
3007                                                      len);
3008                        iter->unmap_len = 0;
3009                        /* override pdest to preserve original P */
3010                        pdest = ppc440spe_chan->pdest;
3011                }
3012                if (qdest) {
3013                        struct dma_cdb *hw_desc;
3014                        struct ppc440spe_adma_chan *chan;
3015
3016                        iter = list_first_entry(&sw_desc->group_list,
3017                                                struct ppc440spe_adma_desc_slot,
3018                                                chain_node);
3019                        chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
3020
3021                        if (pdest) {
3022                                iter = list_entry(iter->chain_node.next,
3023                                                struct ppc440spe_adma_desc_slot,
3024                                                chain_node);
3025                        }
3026
3027                        memset(iter->hw_desc, 0, sizeof(struct dma_cdb));
3028                        iter->hw_next = list_entry(iter->chain_node.next,
3029                                                struct ppc440spe_adma_desc_slot,
3030                                                chain_node);
3031                        hw_desc = iter->hw_desc;
3032                        hw_desc->opc = DMA_CDB_OPC_MV_SG1_SG2;
3033                        iter->src_cnt = 0;
3034                        iter->dst_cnt = 0;
3035                        ppc440spe_desc_set_dest_addr(iter, chan, 0,
3036                                                     ppc440spe_chan->qdest, 0);
3037                        ppc440spe_desc_set_src_addr(iter, chan, 0, 0, qdest);
3038                        ppc440spe_desc_set_byte_count(iter, ppc440spe_chan,
3039                                                      len);
3040                        iter->unmap_len = 0;
3041                        /* override qdest to preserve original Q */
3042                        qdest = ppc440spe_chan->qdest;
3043                }
3044
3045                /* Setup destinations for P/Q ops */
3046                ppc440spe_adma_pqzero_sum_set_dest(sw_desc, pdest, qdest);
3047
3048                /* Setup zero QWORDs into DCHECK CDBs */
3049                idst = dst_cnt;
3050                list_for_each_entry_reverse(iter, &sw_desc->group_list,
3051                                            chain_node) {
3052                        /*
3053                         * The last CDB corresponds to Q-parity check,
3054                         * the one before last CDB corresponds
3055                         * P-parity check
3056                         */
3057                        if (idst == DMA_DEST_MAX_NUM) {
3058                                if (idst == dst_cnt) {
3059                                        set_bit(PPC440SPE_DESC_QCHECK,
3060                                                &iter->flags);
3061                                } else {
3062                                        set_bit(PPC440SPE_DESC_PCHECK,
3063                                                &iter->flags);
3064                                }
3065                        } else {
3066                                if (qdest) {
3067                                        set_bit(PPC440SPE_DESC_QCHECK,
3068                                                &iter->flags);
3069                                } else {
3070                                        set_bit(PPC440SPE_DESC_PCHECK,
3071                                                &iter->flags);
3072                                }
3073                        }
3074                        iter->xor_check_result = pqres;
3075
3076                        /*
3077                         * set it to zero, if check fail then result will
3078                         * be updated
3079                         */
3080                        *iter->xor_check_result = 0;
3081                        ppc440spe_desc_set_dcheck(iter, ppc440spe_chan,
3082                                ppc440spe_qword);
3083
3084                        if (!(--dst_cnt))
3085                                break;
3086                }
3087
3088                /* Setup sources and mults for P/Q ops */
3089                list_for_each_entry_continue_reverse(iter, &sw_desc->group_list,
3090                                                     chain_node) {
3091                        struct ppc440spe_adma_chan *chan;
3092                        u32 mult_dst;
3093
3094                        chan = to_ppc440spe_adma_chan(iter->async_tx.chan);
3095                        ppc440spe_desc_set_src_addr(iter, chan, 0,
3096                                                    DMA_CUED_XOR_HB,
3097                                                    src[src_cnt - 1]);
3098                        if (qdest) {
3099                                mult_dst = (dst_cnt - 1) ? DMA_CDB_SG_DST2 :
3100                                                           DMA_CDB_SG_DST1;
3101                                ppc440spe_desc_set_src_mult(iter, chan,
3102                                                            DMA_CUED_MULT1_OFF,
3103                                                            mult_dst,
3104                                                            scf[src_cnt - 1]);
3105                        }
3106                        if (!(--src_cnt))
3107                                break;
3108                }
3109        }
3110        spin_unlock_bh(&ppc440spe_chan->lock);
3111        return sw_desc ? &sw_desc->async_tx : NULL;
3112}
3113
3114/**
3115 * ppc440spe_adma_prep_dma_xor_zero_sum - prepare CDB group for
3116 * XOR ZERO_SUM operation
3117 */
3118static struct dma_async_tx_descriptor *ppc440spe_adma_prep_dma_xor_zero_sum(
3119                struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
3120                size_t len, enum sum_check_flags *result, unsigned long flags)
3121{
3122        struct dma_async_tx_descriptor *tx;
3123        dma_addr_t pq[2];
3124
3125        /* validate P, disable Q */
3126        pq[0] = src[0];
3127        pq[1] = 0;
3128        flags |= DMA_PREP_PQ_DISABLE_Q;
3129
3130        tx = ppc440spe_adma_prep_dma_pqzero_sum(chan, pq, &src[1],
3131                                                src_cnt - 1, 0, len,
3132                                                result, flags);
3133        return tx;
3134}
3135
3136/**
3137 * ppc440spe_adma_set_dest - set destination address into descriptor
3138 */
3139static void ppc440spe_adma_set_dest(struct ppc440spe_adma_desc_slot *sw_desc,
3140                dma_addr_t addr, int index)
3141{
3142        struct ppc440spe_adma_chan *chan;
3143
3144        BUG_ON(index >= sw_desc->dst_cnt);
3145
3146        chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3147
3148        switch (chan->device->id) {
3149        case PPC440SPE_DMA0_ID:
3150        case PPC440SPE_DMA1_ID:
3151                /* to do: support transfers lengths >
3152                 * PPC440SPE_ADMA_DMA/XOR_MAX_BYTE_COUNT
3153                 */
3154                ppc440spe_desc_set_dest_addr(sw_desc->group_head,
3155                        chan, 0, addr, index);
3156                break;
3157        case PPC440SPE_XOR_ID:
3158                sw_desc = ppc440spe_get_group_entry(sw_desc, index);
3159                ppc440spe_desc_set_dest_addr(sw_desc,
3160                        chan, 0, addr, index);
3161                break;
3162        }
3163}
3164
3165static void ppc440spe_adma_pq_zero_op(struct ppc440spe_adma_desc_slot *iter,
3166                struct ppc440spe_adma_chan *chan, dma_addr_t addr)
3167{
3168        /*  To clear destinations update the descriptor
3169         * (P or Q depending on index) as follows:
3170         * addr is destination (0 corresponds to SG2):
3171         */
3172        ppc440spe_desc_set_dest_addr(iter, chan, DMA_CUED_XOR_BASE, addr, 0);
3173
3174        /* ... and the addr is source: */
3175        ppc440spe_desc_set_src_addr(iter, chan, 0, DMA_CUED_XOR_HB, addr);
3176
3177        /* addr is always SG2 then the mult is always DST1 */
3178        ppc440spe_desc_set_src_mult(iter, chan, DMA_CUED_MULT1_OFF,
3179                                    DMA_CDB_SG_DST1, 1);
3180}
3181
3182/**
3183 * ppc440spe_adma_pq_set_dest - set destination address into descriptor
3184 * for the PQXOR operation
3185 */
3186static void ppc440spe_adma_pq_set_dest(struct ppc440spe_adma_desc_slot *sw_desc,
3187                dma_addr_t *addrs, unsigned long flags)
3188{
3189        struct ppc440spe_adma_desc_slot *iter;
3190        struct ppc440spe_adma_chan *chan;
3191        dma_addr_t paddr, qaddr;
3192        dma_addr_t addr = 0, ppath, qpath;
3193        int index = 0, i;
3194
3195        chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3196
3197        if (flags & DMA_PREP_PQ_DISABLE_P)
3198                paddr = 0;
3199        else
3200                paddr = addrs[0];
3201
3202        if (flags & DMA_PREP_PQ_DISABLE_Q)
3203                qaddr = 0;
3204        else
3205                qaddr = addrs[1];
3206
3207        if (!paddr || !qaddr)
3208                addr = paddr ? paddr : qaddr;
3209
3210        switch (chan->device->id) {
3211        case PPC440SPE_DMA0_ID:
3212        case PPC440SPE_DMA1_ID:
3213                /* walk through the WXOR source list and set P/Q-destinations
3214                 * for each slot:
3215                 */
3216                if (!test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
3217                        /* This is WXOR-only chain; may have 1/2 zero descs */
3218                        if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
3219                                index++;
3220                        if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
3221                                index++;
3222
3223                        iter = ppc440spe_get_group_entry(sw_desc, index);
3224                        if (addr) {
3225                                /* one destination */
3226                                list_for_each_entry_from(iter,
3227                                        &sw_desc->group_list, chain_node)
3228                                        ppc440spe_desc_set_dest_addr(iter, chan,
3229                                                DMA_CUED_XOR_BASE, addr, 0);
3230                        } else {
3231                                /* two destinations */
3232                                list_for_each_entry_from(iter,
3233                                        &sw_desc->group_list, chain_node) {
3234                                        ppc440spe_desc_set_dest_addr(iter, chan,
3235                                                DMA_CUED_XOR_BASE, paddr, 0);
3236                                        ppc440spe_desc_set_dest_addr(iter, chan,
3237                                                DMA_CUED_XOR_BASE, qaddr, 1);
3238                                }
3239                        }
3240
3241                        if (index) {
3242                                /*  To clear destinations update the descriptor
3243                                 * (1st,2nd, or both depending on flags)
3244                                 */
3245                                index = 0;
3246                                if (test_bit(PPC440SPE_ZERO_P,
3247                                                &sw_desc->flags)) {
3248                                        iter = ppc440spe_get_group_entry(
3249                                                        sw_desc, index++);
3250                                        ppc440spe_adma_pq_zero_op(iter, chan,
3251                                                        paddr);
3252                                }
3253
3254                                if (test_bit(PPC440SPE_ZERO_Q,
3255                                                &sw_desc->flags)) {
3256                                        iter = ppc440spe_get_group_entry(
3257                                                        sw_desc, index++);
3258                                        ppc440spe_adma_pq_zero_op(iter, chan,
3259                                                        qaddr);
3260                                }
3261
3262                                return;
3263                        }
3264                } else {
3265                        /* This is RXOR-only or RXOR/WXOR mixed chain */
3266
3267                        /* If we want to include destination into calculations,
3268                         * then make dest addresses cued with mult=1 (XOR).
3269                         */
3270                        ppath = test_bit(PPC440SPE_ZERO_P, &sw_desc->flags) ?
3271                                        DMA_CUED_XOR_HB :
3272                                        DMA_CUED_XOR_BASE |
3273                                                (1 << DMA_CUED_MULT1_OFF);
3274                        qpath = test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags) ?
3275                                        DMA_CUED_XOR_HB :
3276                                        DMA_CUED_XOR_BASE |
3277                                                (1 << DMA_CUED_MULT1_OFF);
3278
3279                        /* Setup destination(s) in RXOR slot(s) */
3280                        iter = ppc440spe_get_group_entry(sw_desc, index++);
3281                        ppc440spe_desc_set_dest_addr(iter, chan,
3282                                                paddr ? ppath : qpath,
3283                                                paddr ? paddr : qaddr, 0);
3284                        if (!addr) {
3285                                /* two destinations */
3286                                iter = ppc440spe_get_group_entry(sw_desc,
3287                                                                 index++);
3288                                ppc440spe_desc_set_dest_addr(iter, chan,
3289                                                qpath, qaddr, 0);
3290                        }
3291
3292                        if (test_bit(PPC440SPE_DESC_WXOR, &sw_desc->flags)) {
3293                                /* Setup destination(s) in remaining WXOR
3294                                 * slots
3295                                 */
3296                                iter = ppc440spe_get_group_entry(sw_desc,
3297                                                                 index);
3298                                if (addr) {
3299                                        /* one destination */
3300                                        list_for_each_entry_from(iter,
3301                                            &sw_desc->group_list,
3302                                            chain_node)
3303                                                ppc440spe_desc_set_dest_addr(
3304                                                        iter, chan,
3305                                                        DMA_CUED_XOR_BASE,
3306                                                        addr, 0);
3307
3308                                } else {
3309                                        /* two destinations */
3310                                        list_for_each_entry_from(iter,
3311                                            &sw_desc->group_list,
3312                                            chain_node) {
3313                                                ppc440spe_desc_set_dest_addr(
3314                                                        iter, chan,
3315                                                        DMA_CUED_XOR_BASE,
3316                                                        paddr, 0);
3317                                                ppc440spe_desc_set_dest_addr(
3318                                                        iter, chan,
3319                                                        DMA_CUED_XOR_BASE,
3320                                                        qaddr, 1);
3321                                        }
3322                                }
3323                        }
3324
3325                }
3326                break;
3327
3328        case PPC440SPE_XOR_ID:
3329                /* DMA2 descriptors have only 1 destination, so there are
3330                 * two chains - one for each dest.
3331                 * If we want to include destination into calculations,
3332                 * then make dest addresses cued with mult=1 (XOR).
3333                 */
3334                ppath = test_bit(PPC440SPE_ZERO_P, &sw_desc->flags) ?
3335                                DMA_CUED_XOR_HB :
3336                                DMA_CUED_XOR_BASE |
3337                                        (1 << DMA_CUED_MULT1_OFF);
3338
3339                qpath = test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags) ?
3340                                DMA_CUED_XOR_HB :
3341                                DMA_CUED_XOR_BASE |
3342                                        (1 << DMA_CUED_MULT1_OFF);
3343
3344                iter = ppc440spe_get_group_entry(sw_desc, 0);
3345                for (i = 0; i < sw_desc->descs_per_op; i++) {
3346                        ppc440spe_desc_set_dest_addr(iter, chan,
3347                                paddr ? ppath : qpath,
3348                                paddr ? paddr : qaddr, 0);
3349                        iter = list_entry(iter->chain_node.next,
3350                                          struct ppc440spe_adma_desc_slot,
3351                                          chain_node);
3352                }
3353
3354                if (!addr) {
3355                        /* Two destinations; setup Q here */
3356                        iter = ppc440spe_get_group_entry(sw_desc,
3357                                sw_desc->descs_per_op);
3358                        for (i = 0; i < sw_desc->descs_per_op; i++) {
3359                                ppc440spe_desc_set_dest_addr(iter,
3360                                        chan, qpath, qaddr, 0);
3361                                iter = list_entry(iter->chain_node.next,
3362                                                struct ppc440spe_adma_desc_slot,
3363                                                chain_node);
3364                        }
3365                }
3366
3367                break;
3368        }
3369}
3370
3371/**
3372 * ppc440spe_adma_pq_zero_sum_set_dest - set destination address into descriptor
3373 * for the PQ_ZERO_SUM operation
3374 */
3375static void ppc440spe_adma_pqzero_sum_set_dest(
3376                struct ppc440spe_adma_desc_slot *sw_desc,
3377                dma_addr_t paddr, dma_addr_t qaddr)
3378{
3379        struct ppc440spe_adma_desc_slot *iter, *end;
3380        struct ppc440spe_adma_chan *chan;
3381        dma_addr_t addr = 0;
3382        int idx;
3383
3384        chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3385
3386        /* walk through the WXOR source list and set P/Q-destinations
3387         * for each slot
3388         */
3389        idx = (paddr && qaddr) ? 2 : 1;
3390        /* set end */
3391        list_for_each_entry_reverse(end, &sw_desc->group_list,
3392                                    chain_node) {
3393                if (!(--idx))
3394                        break;
3395        }
3396        /* set start */
3397        idx = (paddr && qaddr) ? 2 : 1;
3398        iter = ppc440spe_get_group_entry(sw_desc, idx);
3399
3400        if (paddr && qaddr) {
3401                /* two destinations */
3402                list_for_each_entry_from(iter, &sw_desc->group_list,
3403                                         chain_node) {
3404                        if (unlikely(iter == end))
3405                                break;
3406                        ppc440spe_desc_set_dest_addr(iter, chan,
3407                                                DMA_CUED_XOR_BASE, paddr, 0);
3408                        ppc440spe_desc_set_dest_addr(iter, chan,
3409                                                DMA_CUED_XOR_BASE, qaddr, 1);
3410                }
3411        } else {
3412                /* one destination */
3413                addr = paddr ? paddr : qaddr;
3414                list_for_each_entry_from(iter, &sw_desc->group_list,
3415                                         chain_node) {
3416                        if (unlikely(iter == end))
3417                                break;
3418                        ppc440spe_desc_set_dest_addr(iter, chan,
3419                                                DMA_CUED_XOR_BASE, addr, 0);
3420                }
3421        }
3422
3423        /*  The remaining descriptors are DATACHECK. These have no need in
3424         * destination. Actually, these destinations are used there
3425         * as sources for check operation. So, set addr as source.
3426         */
3427        ppc440spe_desc_set_src_addr(end, chan, 0, 0, addr ? addr : paddr);
3428
3429        if (!addr) {
3430                end = list_entry(end->chain_node.next,
3431                                 struct ppc440spe_adma_desc_slot, chain_node);
3432                ppc440spe_desc_set_src_addr(end, chan, 0, 0, qaddr);
3433        }
3434}
3435
3436/**
3437 * ppc440spe_desc_set_xor_src_cnt - set source count into descriptor
3438 */
3439static inline void ppc440spe_desc_set_xor_src_cnt(
3440                        struct ppc440spe_adma_desc_slot *desc,
3441                        int src_cnt)
3442{
3443        struct xor_cb *hw_desc = desc->hw_desc;
3444
3445        hw_desc->cbc &= ~XOR_CDCR_OAC_MSK;
3446        hw_desc->cbc |= src_cnt;
3447}
3448
3449/**
3450 * ppc440spe_adma_pq_set_src - set source address into descriptor
3451 */
3452static void ppc440spe_adma_pq_set_src(struct ppc440spe_adma_desc_slot *sw_desc,
3453                dma_addr_t addr, int index)
3454{
3455        struct ppc440spe_adma_chan *chan;
3456        dma_addr_t haddr = 0;
3457        struct ppc440spe_adma_desc_slot *iter = NULL;
3458
3459        chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3460
3461        switch (chan->device->id) {
3462        case PPC440SPE_DMA0_ID:
3463        case PPC440SPE_DMA1_ID:
3464                /* DMA0,1 may do: WXOR, RXOR, RXOR+WXORs chain
3465                 */
3466                if (test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
3467                        /* RXOR-only or RXOR/WXOR operation */
3468                        int iskip = test_bit(PPC440SPE_DESC_RXOR12,
3469                                &sw_desc->flags) ?  2 : 3;
3470
3471                        if (index == 0) {
3472                                /* 1st slot (RXOR) */
3473                                /* setup sources region (R1-2-3, R1-2-4,
3474                                 * or R1-2-5)
3475                                 */
3476                                if (test_bit(PPC440SPE_DESC_RXOR12,
3477                                                &sw_desc->flags))
3478                                        haddr = DMA_RXOR12 <<
3479                                                DMA_CUED_REGION_OFF;
3480                                else if (test_bit(PPC440SPE_DESC_RXOR123,
3481                                    &sw_desc->flags))
3482                                        haddr = DMA_RXOR123 <<
3483                                                DMA_CUED_REGION_OFF;
3484                                else if (test_bit(PPC440SPE_DESC_RXOR124,
3485                                    &sw_desc->flags))
3486                                        haddr = DMA_RXOR124 <<
3487                                                DMA_CUED_REGION_OFF;
3488                                else if (test_bit(PPC440SPE_DESC_RXOR125,
3489                                    &sw_desc->flags))
3490                                        haddr = DMA_RXOR125 <<
3491                                                DMA_CUED_REGION_OFF;
3492                                else
3493                                        BUG();
3494                                haddr |= DMA_CUED_XOR_BASE;
3495                                iter = ppc440spe_get_group_entry(sw_desc, 0);
3496                        } else if (index < iskip) {
3497                                /* 1st slot (RXOR)
3498                                 * shall actually set source address only once
3499                                 * instead of first <iskip>
3500                                 */
3501                                iter = NULL;
3502                        } else {
3503                                /* 2nd/3d and next slots (WXOR);
3504                                 * skip first slot with RXOR
3505                                 */
3506                                haddr = DMA_CUED_XOR_HB;
3507                                iter = ppc440spe_get_group_entry(sw_desc,
3508                                    index - iskip + sw_desc->dst_cnt);
3509                        }
3510                } else {
3511                        int znum = 0;
3512
3513                        /* WXOR-only operation; skip first slots with
3514                         * zeroing destinations
3515                         */
3516                        if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
3517                                znum++;
3518                        if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
3519                                znum++;
3520
3521                        haddr = DMA_CUED_XOR_HB;
3522                        iter = ppc440spe_get_group_entry(sw_desc,
3523                                        index + znum);
3524                }
3525
3526                if (likely(iter)) {
3527                        ppc440spe_desc_set_src_addr(iter, chan, 0, haddr, addr);
3528
3529                        if (!index &&
3530                            test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags) &&
3531                            sw_desc->dst_cnt == 2) {
3532                                /* if we have two destinations for RXOR, then
3533                                 * setup source in the second descr too
3534                                 */
3535                                iter = ppc440spe_get_group_entry(sw_desc, 1);
3536                                ppc440spe_desc_set_src_addr(iter, chan, 0,
3537                                        haddr, addr);
3538                        }
3539                }
3540                break;
3541
3542        case PPC440SPE_XOR_ID:
3543                /* DMA2 may do Biskup */
3544                iter = sw_desc->group_head;
3545                if (iter->dst_cnt == 2) {
3546                        /* both P & Q calculations required; set P src here */
3547                        ppc440spe_adma_dma2rxor_set_src(iter, index, addr);
3548
3549                        /* this is for Q */
3550                        iter = ppc440spe_get_group_entry(sw_desc,
3551                                sw_desc->descs_per_op);
3552                }
3553                ppc440spe_adma_dma2rxor_set_src(iter, index, addr);
3554                break;
3555        }
3556}
3557
3558/**
3559 * ppc440spe_adma_memcpy_xor_set_src - set source address into descriptor
3560 */
3561static void ppc440spe_adma_memcpy_xor_set_src(
3562                struct ppc440spe_adma_desc_slot *sw_desc,
3563                dma_addr_t addr, int index)
3564{
3565        struct ppc440spe_adma_chan *chan;
3566
3567        chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3568        sw_desc = sw_desc->group_head;
3569
3570        if (likely(sw_desc))
3571                ppc440spe_desc_set_src_addr(sw_desc, chan, index, 0, addr);
3572}
3573
3574/**
3575 * ppc440spe_adma_dma2rxor_inc_addr  -
3576 */
3577static void ppc440spe_adma_dma2rxor_inc_addr(
3578                struct ppc440spe_adma_desc_slot *desc,
3579                struct ppc440spe_rxor *cursor, int index, int src_cnt)
3580{
3581        cursor->addr_count++;
3582        if (index == src_cnt - 1) {
3583                ppc440spe_desc_set_xor_src_cnt(desc, cursor->addr_count);
3584        } else if (cursor->addr_count == XOR_MAX_OPS) {
3585                ppc440spe_desc_set_xor_src_cnt(desc, cursor->addr_count);
3586                cursor->addr_count = 0;
3587                cursor->desc_count++;
3588        }
3589}
3590
3591/**
3592 * ppc440spe_adma_dma2rxor_prep_src - setup RXOR types in DMA2 CDB
3593 */
3594static int ppc440spe_adma_dma2rxor_prep_src(
3595                struct ppc440spe_adma_desc_slot *hdesc,
3596                struct ppc440spe_rxor *cursor, int index,
3597                int src_cnt, u32 addr)
3598{
3599        int rval = 0;
3600        u32 sign;
3601        struct ppc440spe_adma_desc_slot *desc = hdesc;
3602        int i;
3603
3604        for (i = 0; i < cursor->desc_count; i++) {
3605                desc = list_entry(hdesc->chain_node.next,
3606                                  struct ppc440spe_adma_desc_slot,
3607                                  chain_node);
3608        }
3609
3610        switch (cursor->state) {
3611        case 0:
3612                if (addr == cursor->addrl + cursor->len) {
3613                        /* direct RXOR */
3614                        cursor->state = 1;
3615                        cursor->xor_count++;
3616                        if (index == src_cnt-1) {
3617                                ppc440spe_rxor_set_region(desc,
3618                                        cursor->addr_count,
3619                                        DMA_RXOR12 << DMA_CUED_REGION_OFF);
3620                                ppc440spe_adma_dma2rxor_inc_addr(
3621                                        desc, cursor, index, src_cnt);
3622                        }
3623                } else if (cursor->addrl == addr + cursor->len) {
3624                        /* reverse RXOR */
3625                        cursor->state = 1;
3626                        cursor->xor_count++;
3627                        set_bit(cursor->addr_count, &desc->reverse_flags[0]);
3628                        if (index == src_cnt-1) {
3629                                ppc440spe_rxor_set_region(desc,
3630                                        cursor->addr_count,
3631                                        DMA_RXOR12 << DMA_CUED_REGION_OFF);
3632                                ppc440spe_adma_dma2rxor_inc_addr(
3633                                        desc, cursor, index, src_cnt);
3634                        }
3635                } else {
3636                        printk(KERN_ERR "Cannot build "
3637                                "DMA2 RXOR command block.\n");
3638                        BUG();
3639                }
3640                break;
3641        case 1:
3642                sign = test_bit(cursor->addr_count,
3643                                desc->reverse_flags)
3644                        ? -1 : 1;
3645                if (index == src_cnt-2 || (sign == -1
3646                        && addr != cursor->addrl - 2*cursor->len)) {
3647                        cursor->state = 0;
3648                        cursor->xor_count = 1;
3649                        cursor->addrl = addr;
3650                        ppc440spe_rxor_set_region(desc,
3651                                cursor->addr_count,
3652                                DMA_RXOR12 << DMA_CUED_REGION_OFF);
3653                        ppc440spe_adma_dma2rxor_inc_addr(
3654                                desc, cursor, index, src_cnt);
3655                } else if (addr == cursor->addrl + 2*sign*cursor->len) {
3656                        cursor->state = 2;
3657                        cursor->xor_count = 0;
3658                        ppc440spe_rxor_set_region(desc,
3659                                cursor->addr_count,
3660                                DMA_RXOR123 << DMA_CUED_REGION_OFF);
3661                        if (index == src_cnt-1) {
3662                                ppc440spe_adma_dma2rxor_inc_addr(
3663                                        desc, cursor, index, src_cnt);
3664                        }
3665                } else if (addr == cursor->addrl + 3*cursor->len) {
3666                        cursor->state = 2;
3667                        cursor->xor_count = 0;
3668                        ppc440spe_rxor_set_region(desc,
3669                                cursor->addr_count,
3670                                DMA_RXOR124 << DMA_CUED_REGION_OFF);
3671                        if (index == src_cnt-1) {
3672                                ppc440spe_adma_dma2rxor_inc_addr(
3673                                        desc, cursor, index, src_cnt);
3674                        }
3675                } else if (addr == cursor->addrl + 4*cursor->len) {
3676                        cursor->state = 2;
3677                        cursor->xor_count = 0;
3678                        ppc440spe_rxor_set_region(desc,
3679                                cursor->addr_count,
3680                                DMA_RXOR125 << DMA_CUED_REGION_OFF);
3681                        if (index == src_cnt-1) {
3682                                ppc440spe_adma_dma2rxor_inc_addr(
3683                                        desc, cursor, index, src_cnt);
3684                        }
3685                } else {
3686                        cursor->state = 0;
3687                        cursor->xor_count = 1;
3688                        cursor->addrl = addr;
3689                        ppc440spe_rxor_set_region(desc,
3690                                cursor->addr_count,
3691                                DMA_RXOR12 << DMA_CUED_REGION_OFF);
3692                        ppc440spe_adma_dma2rxor_inc_addr(
3693                                desc, cursor, index, src_cnt);
3694                }
3695                break;
3696        case 2:
3697                cursor->state = 0;
3698                cursor->addrl = addr;
3699                cursor->xor_count++;
3700                if (index) {
3701                        ppc440spe_adma_dma2rxor_inc_addr(
3702                                desc, cursor, index, src_cnt);
3703                }
3704                break;
3705        }
3706
3707        return rval;
3708}
3709
3710/**
3711 * ppc440spe_adma_dma2rxor_set_src - set RXOR source address; it's assumed that
3712 *      ppc440spe_adma_dma2rxor_prep_src() has already done prior this call
3713 */
3714static void ppc440spe_adma_dma2rxor_set_src(
3715                struct ppc440spe_adma_desc_slot *desc,
3716                int index, dma_addr_t addr)
3717{
3718        struct xor_cb *xcb = desc->hw_desc;
3719        int k = 0, op = 0, lop = 0;
3720
3721        /* get the RXOR operand which corresponds to index addr */
3722        while (op <= index) {
3723                lop = op;
3724                if (k == XOR_MAX_OPS) {
3725                        k = 0;
3726                        desc = list_entry(desc->chain_node.next,
3727                                struct ppc440spe_adma_desc_slot, chain_node);
3728                        xcb = desc->hw_desc;
3729
3730                }
3731                if ((xcb->ops[k++].h & (DMA_RXOR12 << DMA_CUED_REGION_OFF)) ==
3732                    (DMA_RXOR12 << DMA_CUED_REGION_OFF))
3733                        op += 2;
3734                else
3735                        op += 3;
3736        }
3737
3738        BUG_ON(k < 1);
3739
3740        if (test_bit(k-1, desc->reverse_flags)) {
3741                /* reverse operand order; put last op in RXOR group */
3742                if (index == op - 1)
3743                        ppc440spe_rxor_set_src(desc, k - 1, addr);
3744        } else {
3745                /* direct operand order; put first op in RXOR group */
3746                if (index == lop)
3747                        ppc440spe_rxor_set_src(desc, k - 1, addr);
3748        }
3749}
3750
3751/**
3752 * ppc440spe_adma_dma2rxor_set_mult - set RXOR multipliers; it's assumed that
3753 *      ppc440spe_adma_dma2rxor_prep_src() has already done prior this call
3754 */
3755static void ppc440spe_adma_dma2rxor_set_mult(
3756                struct ppc440spe_adma_desc_slot *desc,
3757                int index, u8 mult)
3758{
3759        struct xor_cb *xcb = desc->hw_desc;
3760        int k = 0, op = 0, lop = 0;
3761
3762        /* get the RXOR operand which corresponds to index mult */
3763        while (op <= index) {
3764                lop = op;
3765                if (k == XOR_MAX_OPS) {
3766                        k = 0;
3767                        desc = list_entry(desc->chain_node.next,
3768                                          struct ppc440spe_adma_desc_slot,
3769                                          chain_node);
3770                        xcb = desc->hw_desc;
3771
3772                }
3773                if ((xcb->ops[k++].h & (DMA_RXOR12 << DMA_CUED_REGION_OFF)) ==
3774                    (DMA_RXOR12 << DMA_CUED_REGION_OFF))
3775                        op += 2;
3776                else
3777                        op += 3;
3778        }
3779
3780        BUG_ON(k < 1);
3781        if (test_bit(k-1, desc->reverse_flags)) {
3782                /* reverse order */
3783                ppc440spe_rxor_set_mult(desc, k - 1, op - index - 1, mult);
3784        } else {
3785                /* direct order */
3786                ppc440spe_rxor_set_mult(desc, k - 1, index - lop, mult);
3787        }
3788}
3789
3790/**
3791 * ppc440spe_init_rxor_cursor -
3792 */
3793static void ppc440spe_init_rxor_cursor(struct ppc440spe_rxor *cursor)
3794{
3795        memset(cursor, 0, sizeof(struct ppc440spe_rxor));
3796        cursor->state = 2;
3797}
3798
3799/**
3800 * ppc440spe_adma_pq_set_src_mult - set multiplication coefficient into
3801 * descriptor for the PQXOR operation
3802 */
3803static void ppc440spe_adma_pq_set_src_mult(
3804                struct ppc440spe_adma_desc_slot *sw_desc,
3805                unsigned char mult, int index, int dst_pos)
3806{
3807        struct ppc440spe_adma_chan *chan;
3808        u32 mult_idx, mult_dst;
3809        struct ppc440spe_adma_desc_slot *iter = NULL, *iter1 = NULL;
3810
3811        chan = to_ppc440spe_adma_chan(sw_desc->async_tx.chan);
3812
3813        switch (chan->device->id) {
3814        case PPC440SPE_DMA0_ID:
3815        case PPC440SPE_DMA1_ID:
3816                if (test_bit(PPC440SPE_DESC_RXOR, &sw_desc->flags)) {
3817                        int region = test_bit(PPC440SPE_DESC_RXOR12,
3818                                        &sw_desc->flags) ? 2 : 3;
3819
3820                        if (index < region) {
3821                                /* RXOR multipliers */
3822                                iter = ppc440spe_get_group_entry(sw_desc,
3823                                        sw_desc->dst_cnt - 1);
3824                                if (sw_desc->dst_cnt == 2)
3825                                        iter1 = ppc440spe_get_group_entry(
3826                                                        sw_desc, 0);
3827
3828                                mult_idx = DMA_CUED_MULT1_OFF + (index << 3);
3829                                mult_dst = DMA_CDB_SG_SRC;
3830                        } else {
3831                                /* WXOR multiplier */
3832                                iter = ppc440spe_get_group_entry(sw_desc,
3833                                                        index - region +
3834                                                        sw_desc->dst_cnt);
3835                                mult_idx = DMA_CUED_MULT1_OFF;
3836                                mult_dst = dst_pos ? DMA_CDB_SG_DST2 :
3837                                                     DMA_CDB_SG_DST1;
3838                        }
3839                } else {
3840                        int znum = 0;
3841
3842                        /* WXOR-only;
3843                         * skip first slots with destinations (if ZERO_DST has
3844                         * place)
3845                         */
3846                        if (test_bit(PPC440SPE_ZERO_P, &sw_desc->flags))
3847                                znum++;
3848                        if (test_bit(PPC440SPE_ZERO_Q, &sw_desc->flags))
3849                                znum++;
3850
3851                        iter = ppc440spe_get_group_entry(sw_desc, index + znum);
3852                        mult_idx = DMA_CUED_MULT1_OFF;
3853                        mult_dst = dst_pos ? DMA_CDB_SG_DST2 : DMA_CDB_SG_DST1;
3854                }
3855
3856                if (likely(iter)) {
3857                        ppc440spe_desc_set_src_mult(iter, chan,
3858                                mult_idx, mult_dst, mult);
3859
3860                        if (unlikely(iter1)) {
3861                                /* if we have two destinations for RXOR, then
3862                                 * we've just set Q mult. Set-up P now.
3863                                 */
3864                                ppc440spe_desc_set_src_mult(iter1, chan,
3865                                        mult_idx, mult_dst, 1);
3866                        }
3867
3868                }
3869                break;
3870
3871        case PPC440SPE_XOR_ID:
3872                iter = sw_desc->group_head;
3873                if (sw_desc->dst_cnt == 2) {
3874                        /* both P & Q calculations required; set P mult here */
3875                        ppc440spe_adma_dma2rxor_set_mult(iter, index, 1);
3876
3877                        /* and then set Q mult */
3878                        iter = ppc440spe_get_group_entry(sw_desc,
3879                               sw_desc->descs_per_op);
3880                }
3881                ppc440spe_adma_dma2rxor_set_mult(iter, index, mult);
3882                break;
3883        }
3884}
3885
3886/**
3887 * ppc440spe_adma_free_chan_resources - free the resources allocated
3888 */
3889static void ppc440spe_adma_free_chan_resources(struct dma_chan *chan)
3890{
3891        struct ppc440spe_adma_chan *ppc440spe_chan;
3892        struct ppc440spe_adma_desc_slot *iter, *_iter;
3893        int in_use_descs = 0;
3894
3895        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
3896        ppc440spe_adma_slot_cleanup(ppc440spe_chan);
3897
3898        spin_lock_bh(&ppc440spe_chan->lock);
3899        list_for_each_entry_safe(iter, _iter, &ppc440spe_chan->chain,
3900                                        chain_node) {
3901                in_use_descs++;
3902                list_del(&iter->chain_node);
3903        }
3904        list_for_each_entry_safe_reverse(iter, _iter,
3905                        &ppc440spe_chan->all_slots, slot_node) {
3906                list_del(&iter->slot_node);
3907                kfree(iter);
3908                ppc440spe_chan->slots_allocated--;
3909        }
3910        ppc440spe_chan->last_used = NULL;
3911
3912        dev_dbg(ppc440spe_chan->device->common.dev,
3913                "ppc440spe adma%d %s slots_allocated %d\n",
3914                ppc440spe_chan->device->id,
3915                __func__, ppc440spe_chan->slots_allocated);
3916        spin_unlock_bh(&ppc440spe_chan->lock);
3917
3918        /* one is ok since we left it on there on purpose */
3919        if (in_use_descs > 1)
3920                printk(KERN_ERR "SPE: Freeing %d in use descriptors!\n",
3921                        in_use_descs - 1);
3922}
3923
3924/**
3925 * ppc440spe_adma_tx_status - poll the status of an ADMA transaction
3926 * @chan: ADMA channel handle
3927 * @cookie: ADMA transaction identifier
3928 * @txstate: a holder for the current state of the channel
3929 */
3930static enum dma_status ppc440spe_adma_tx_status(struct dma_chan *chan,
3931                        dma_cookie_t cookie, struct dma_tx_state *txstate)
3932{
3933        struct ppc440spe_adma_chan *ppc440spe_chan;
3934        enum dma_status ret;
3935
3936        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
3937        ret = dma_cookie_status(chan, cookie, txstate);
3938        if (ret == DMA_SUCCESS)
3939                return ret;
3940
3941        ppc440spe_adma_slot_cleanup(ppc440spe_chan);
3942
3943        return dma_cookie_status(chan, cookie, txstate);
3944}
3945
3946/**
3947 * ppc440spe_adma_eot_handler - end of transfer interrupt handler
3948 */
3949static irqreturn_t ppc440spe_adma_eot_handler(int irq, void *data)
3950{
3951        struct ppc440spe_adma_chan *chan = data;
3952
3953        dev_dbg(chan->device->common.dev,
3954                "ppc440spe adma%d: %s\n", chan->device->id, __func__);
3955
3956        tasklet_schedule(&chan->irq_tasklet);
3957        ppc440spe_adma_device_clear_eot_status(chan);
3958
3959        return IRQ_HANDLED;
3960}
3961
3962/**
3963 * ppc440spe_adma_err_handler - DMA error interrupt handler;
3964 *      do the same things as a eot handler
3965 */
3966static irqreturn_t ppc440spe_adma_err_handler(int irq, void *data)
3967{
3968        struct ppc440spe_adma_chan *chan = data;
3969
3970        dev_dbg(chan->device->common.dev,
3971                "ppc440spe adma%d: %s\n", chan->device->id, __func__);
3972
3973        tasklet_schedule(&chan->irq_tasklet);
3974        ppc440spe_adma_device_clear_eot_status(chan);
3975
3976        return IRQ_HANDLED;
3977}
3978
3979/**
3980 * ppc440spe_test_callback - called when test operation has been done
3981 */
3982static void ppc440spe_test_callback(void *unused)
3983{
3984        complete(&ppc440spe_r6_test_comp);
3985}
3986
3987/**
3988 * ppc440spe_adma_issue_pending - flush all pending descriptors to h/w
3989 */
3990static void ppc440spe_adma_issue_pending(struct dma_chan *chan)
3991{
3992        struct ppc440spe_adma_chan *ppc440spe_chan;
3993
3994        ppc440spe_chan = to_ppc440spe_adma_chan(chan);
3995        dev_dbg(ppc440spe_chan->device->common.dev,
3996                "ppc440spe adma%d: %s %d \n", ppc440spe_chan->device->id,
3997                __func__, ppc440spe_chan->pending);
3998
3999        if (ppc440spe_chan->pending) {
4000                ppc440spe_chan->pending = 0;
4001                ppc440spe_chan_append(ppc440spe_chan);
4002        }
4003}
4004
4005/**
4006 * ppc440spe_chan_start_null_xor - initiate the first XOR operation (DMA engines
4007 *      use FIFOs (as opposite to chains used in XOR) so this is a XOR
4008 *      specific operation)
4009 */
4010static void ppc440spe_chan_start_null_xor(struct ppc440spe_adma_chan *chan)
4011{
4012        struct ppc440spe_adma_desc_slot *sw_desc, *group_start;
4013        dma_cookie_t cookie;
4014        int slot_cnt, slots_per_op;
4015
4016        dev_dbg(chan->device->common.dev,
4017                "ppc440spe adma%d: %s\n", chan->device->id, __func__);
4018
4019        spin_lock_bh(&chan->lock);
4020        slot_cnt = ppc440spe_chan_xor_slot_count(0, 2, &slots_per_op);
4021        sw_desc = ppc440spe_adma_alloc_slots(chan, slot_cnt, slots_per_op);
4022        if (sw_desc) {
4023                group_start = sw_desc->group_head;
4024                list_splice_init(&sw_desc->group_list, &chan->chain);
4025                async_tx_ack(&sw_desc->async_tx);
4026                ppc440spe_desc_init_null_xor(group_start);
4027
4028                cookie = dma_cookie_assign(&sw_desc->async_tx);
4029
4030                /* initialize the completed cookie to be less than
4031                 * the most recently used cookie
4032                 */
4033                chan->common.completed_cookie = cookie - 1;
4034
4035                /* channel should not be busy */
4036                BUG_ON(ppc440spe_chan_is_busy(chan));
4037
4038                /* set the descriptor address */
4039                ppc440spe_chan_set_first_xor_descriptor(chan, sw_desc);
4040
4041                /* run the descriptor */
4042                ppc440spe_chan_run(chan);
4043        } else
4044                printk(KERN_ERR "ppc440spe adma%d"
4045                        " failed to allocate null descriptor\n",
4046                        chan->device->id);
4047        spin_unlock_bh(&chan->lock);
4048}
4049
4050/**
4051 * ppc440spe_test_raid6 - test are RAID-6 capabilities enabled successfully.
4052 *      For this we just perform one WXOR operation with the same source
4053 *      and destination addresses, the GF-multiplier is 1; so if RAID-6
4054 *      capabilities are enabled then we'll get src/dst filled with zero.
4055 */
4056static int ppc440spe_test_raid6(struct ppc440spe_adma_chan *chan)
4057{
4058        struct ppc440spe_adma_desc_slot *sw_desc, *iter;
4059        struct page *pg;
4060        char *a;
4061        dma_addr_t dma_addr, addrs[2];
4062        unsigned long op = 0;
4063        int rval = 0;
4064
4065        set_bit(PPC440SPE_DESC_WXOR, &op);
4066
4067        pg = alloc_page(GFP_KERNEL);
4068        if (!pg)
4069                return -ENOMEM;
4070
4071        spin_lock_bh(&chan->lock);
4072        sw_desc = ppc440spe_adma_alloc_slots(chan, 1, 1);
4073        if (sw_desc) {
4074                /* 1 src, 1 dsr, int_ena, WXOR */
4075                ppc440spe_desc_init_dma01pq(sw_desc, 1, 1, 1, op);
4076                list_for_each_entry(iter, &sw_desc->group_list, chain_node) {
4077                        ppc440spe_desc_set_byte_count(iter, chan, PAGE_SIZE);
4078                        iter->unmap_len = PAGE_SIZE;
4079                }
4080        } else {
4081                rval = -EFAULT;
4082                spin_unlock_bh(&chan->lock);
4083                goto exit;
4084        }
4085        spin_unlock_bh(&chan->lock);
4086
4087        /* Fill the test page with ones */
4088        memset(page_address(pg), 0xFF, PAGE_SIZE);
4089        dma_addr = dma_map_page(chan->device->dev, pg, 0,
4090                                PAGE_SIZE, DMA_BIDIRECTIONAL);
4091
4092        /* Setup addresses */
4093        ppc440spe_adma_pq_set_src(sw_desc, dma_addr, 0);
4094        ppc440spe_adma_pq_set_src_mult(sw_desc, 1, 0, 0);
4095        addrs[0] = dma_addr;
4096        addrs[1] = 0;
4097        ppc440spe_adma_pq_set_dest(sw_desc, addrs, DMA_PREP_PQ_DISABLE_Q);
4098
4099        async_tx_ack(&sw_desc->async_tx);
4100        sw_desc->async_tx.callback = ppc440spe_test_callback;
4101        sw_desc->async_tx.callback_param = NULL;
4102
4103        init_completion(&ppc440spe_r6_test_comp);
4104
4105        ppc440spe_adma_tx_submit(&sw_desc->async_tx);
4106        ppc440spe_adma_issue_pending(&chan->common);
4107
4108        wait_for_completion(&ppc440spe_r6_test_comp);
4109
4110        /* Now check if the test page is zeroed */
4111        a = page_address(pg);
4112        if ((*(u32 *)a) == 0 && memcmp(a, a+4, PAGE_SIZE-4) == 0) {
4113                /* page is zero - RAID-6 enabled */
4114                rval = 0;
4115        } else {
4116                /* RAID-6 was not enabled */
4117                rval = -EINVAL;
4118        }
4119exit:
4120        __free_page(pg);
4121        return rval;
4122}
4123
4124static void ppc440spe_adma_init_capabilities(struct ppc440spe_adma_device *adev)
4125{
4126        switch (adev->id) {
4127        case PPC440SPE_DMA0_ID:
4128        case PPC440SPE_DMA1_ID:
4129                dma_cap_set(DMA_MEMCPY, adev->common.cap_mask);
4130                dma_cap_set(DMA_INTERRUPT, adev->common.cap_mask);
4131                dma_cap_set(DMA_MEMSET, adev->common.cap_mask);
4132                dma_cap_set(DMA_PQ, adev->common.cap_mask);
4133                dma_cap_set(DMA_PQ_VAL, adev->common.cap_mask);
4134                dma_cap_set(DMA_XOR_VAL, adev->common.cap_mask);
4135                break;
4136        case PPC440SPE_XOR_ID:
4137                dma_cap_set(DMA_XOR, adev->common.cap_mask);
4138                dma_cap_set(DMA_PQ, adev->common.cap_mask);
4139                dma_cap_set(DMA_INTERRUPT, adev->common.cap_mask);
4140                adev->common.cap_mask = adev->common.cap_mask;
4141                break;
4142        }
4143
4144        /* Set base routines */
4145        adev->common.device_alloc_chan_resources =
4146                                ppc440spe_adma_alloc_chan_resources;
4147        adev->common.device_free_chan_resources =
4148                                ppc440spe_adma_free_chan_resources;
4149        adev->common.device_tx_status = ppc440spe_adma_tx_status;
4150        adev->common.device_issue_pending = ppc440spe_adma_issue_pending;
4151
4152        /* Set prep routines based on capability */
4153        if (dma_has_cap(DMA_MEMCPY, adev->common.cap_mask)) {
4154                adev->common.device_prep_dma_memcpy =
4155                        ppc440spe_adma_prep_dma_memcpy;
4156        }
4157        if (dma_has_cap(DMA_MEMSET, adev->common.cap_mask)) {
4158                adev->common.device_prep_dma_memset =
4159                        ppc440spe_adma_prep_dma_memset;
4160        }
4161        if (dma_has_cap(DMA_XOR, adev->common.cap_mask)) {
4162                adev->common.max_xor = XOR_MAX_OPS;
4163                adev->common.device_prep_dma_xor =
4164                        ppc440spe_adma_prep_dma_xor;
4165        }
4166        if (dma_has_cap(DMA_PQ, adev->common.cap_mask)) {
4167                switch (adev->id) {
4168                case PPC440SPE_DMA0_ID:
4169                        dma_set_maxpq(&adev->common,
4170                                DMA0_FIFO_SIZE / sizeof(struct dma_cdb), 0);
4171                        break;
4172                case PPC440SPE_DMA1_ID:
4173                        dma_set_maxpq(&adev->common,
4174                                DMA1_FIFO_SIZE / sizeof(struct dma_cdb), 0);
4175                        break;
4176                case PPC440SPE_XOR_ID:
4177                        adev->common.max_pq = XOR_MAX_OPS * 3;
4178                        break;
4179                }
4180                adev->common.device_prep_dma_pq =
4181                        ppc440spe_adma_prep_dma_pq;
4182        }
4183        if (dma_has_cap(DMA_PQ_VAL, adev->common.cap_mask)) {
4184                switch (adev->id) {
4185                case PPC440SPE_DMA0_ID:
4186                        adev->common.max_pq = DMA0_FIFO_SIZE /
4187                                                sizeof(struct dma_cdb);
4188                        break;
4189                case PPC440SPE_DMA1_ID:
4190                        adev->common.max_pq = DMA1_FIFO_SIZE /
4191                                                sizeof(struct dma_cdb);
4192                        break;
4193                }
4194                adev->common.device_prep_dma_pq_val =
4195                        ppc440spe_adma_prep_dma_pqzero_sum;
4196        }
4197        if (dma_has_cap(DMA_XOR_VAL, adev->common.cap_mask)) {
4198                switch (adev->id) {
4199                case PPC440SPE_DMA0_ID:
4200                        adev->common.max_xor = DMA0_FIFO_SIZE /
4201                                                sizeof(struct dma_cdb);
4202                        break;
4203                case PPC440SPE_DMA1_ID:
4204                        adev->common.max_xor = DMA1_FIFO_SIZE /
4205                                                sizeof(struct dma_cdb);
4206                        break;
4207                }
4208                adev->common.device_prep_dma_xor_val =
4209                        ppc440spe_adma_prep_dma_xor_zero_sum;
4210        }
4211        if (dma_has_cap(DMA_INTERRUPT, adev->common.cap_mask)) {
4212                adev->common.device_prep_dma_interrupt =
4213                        ppc440spe_adma_prep_dma_interrupt;
4214        }
4215        pr_info("%s: AMCC(R) PPC440SP(E) ADMA Engine: "
4216          "( %s%s%s%s%s%s%s)\n",
4217          dev_name(adev->dev),
4218          dma_has_cap(DMA_PQ, adev->common.cap_mask) ? "pq " : "",
4219          dma_has_cap(DMA_PQ_VAL, adev->common.cap_mask) ? "pq_val " : "",
4220          dma_has_cap(DMA_XOR, adev->common.cap_mask) ? "xor " : "",
4221          dma_has_cap(DMA_XOR_VAL, adev->common.cap_mask) ? "xor_val " : "",
4222          dma_has_cap(DMA_MEMCPY, adev->common.cap_mask) ? "memcpy " : "",
4223          dma_has_cap(DMA_MEMSET, adev->common.cap_mask)  ? "memset " : "",
4224          dma_has_cap(DMA_INTERRUPT, adev->common.cap_mask) ? "intr " : "");
4225}
4226
4227static int ppc440spe_adma_setup_irqs(struct ppc440spe_adma_device *adev,
4228                                     struct ppc440spe_adma_chan *chan,
4229                                     int *initcode)
4230{
4231        struct platform_device *ofdev;
4232        struct device_node *np;
4233        int ret;
4234
4235        ofdev = container_of(adev->dev, struct platform_device, dev);
4236        np = ofdev->dev.of_node;
4237        if (adev->id != PPC440SPE_XOR_ID) {
4238                adev->err_irq = irq_of_parse_and_map(np, 1);
4239                if (adev->err_irq == NO_IRQ) {
4240                        dev_warn(adev->dev, "no err irq resource?\n");
4241                        *initcode = PPC_ADMA_INIT_IRQ2;
4242                        adev->err_irq = -ENXIO;
4243                } else
4244                        atomic_inc(&ppc440spe_adma_err_irq_ref);
4245        } else {
4246                adev->err_irq = -ENXIO;
4247        }
4248
4249        adev->irq = irq_of_parse_and_map(np, 0);
4250        if (adev->irq == NO_IRQ) {
4251                dev_err(adev->dev, "no irq resource\n");
4252                *initcode = PPC_ADMA_INIT_IRQ1;
4253                ret = -ENXIO;
4254                goto err_irq_map;
4255        }
4256        dev_dbg(adev->dev, "irq %d, err irq %d\n",
4257                adev->irq, adev->err_irq);
4258
4259        ret = request_irq(adev->irq, ppc440spe_adma_eot_handler,
4260                          0, dev_driver_string(adev->dev), chan);
4261        if (ret) {
4262                dev_err(adev->dev, "can't request irq %d\n",
4263                        adev->irq);
4264                *initcode = PPC_ADMA_INIT_IRQ1;
4265                ret = -EIO;
4266                goto err_req1;
4267        }
4268
4269        /* only DMA engines have a separate error IRQ
4270         * so it's Ok if err_irq < 0 in XOR engine case.
4271         */
4272        if (adev->err_irq > 0) {
4273                /* both DMA engines share common error IRQ */
4274                ret = request_irq(adev->err_irq,
4275                                  ppc440spe_adma_err_handler,
4276                                  IRQF_SHARED,
4277                                  dev_driver_string(adev->dev),
4278                                  chan);
4279                if (ret) {
4280                        dev_err(adev->dev, "can't request irq %d\n",
4281                                adev->err_irq);
4282                        *initcode = PPC_ADMA_INIT_IRQ2;
4283                        ret = -EIO;
4284                        goto err_req2;
4285                }
4286        }
4287
4288        if (adev->id == PPC440SPE_XOR_ID) {
4289                /* enable XOR engine interrupts */
4290                iowrite32be(XOR_IE_CBCIE_BIT | XOR_IE_ICBIE_BIT |
4291                            XOR_IE_ICIE_BIT | XOR_IE_RPTIE_BIT,
4292                            &adev->xor_reg->ier);
4293        } else {
4294                u32 mask, enable;
4295
4296                np = of_find_compatible_node(NULL, NULL, "ibm,i2o-440spe");
4297                if (!np) {
4298                        pr_err("%s: can't find I2O device tree node\n",
4299                                __func__);
4300                        ret = -ENODEV;
4301                        goto err_req2;
4302                }
4303                adev->i2o_reg = of_iomap(np, 0);
4304                if (!adev->i2o_reg) {
4305                        pr_err("%s: failed to map I2O registers\n", __func__);
4306                        of_node_put(np);
4307                        ret = -EINVAL;
4308                        goto err_req2;
4309                }
4310                of_node_put(np);
4311                /* Unmask 'CS FIFO Attention' interrupts and
4312                 * enable generating interrupts on errors
4313                 */
4314                enable = (adev->id == PPC440SPE_DMA0_ID) ?
4315                         ~(I2O_IOPIM_P0SNE | I2O_IOPIM_P0EM) :
4316                         ~(I2O_IOPIM_P1SNE | I2O_IOPIM_P1EM);
4317                mask = ioread32(&adev->i2o_reg->iopim) & enable;
4318                iowrite32(mask, &adev->i2o_reg->iopim);
4319        }
4320        return 0;
4321
4322err_req2:
4323        free_irq(adev->irq, chan);
4324err_req1:
4325        irq_dispose_mapping(adev->irq);
4326err_irq_map:
4327        if (adev->err_irq > 0) {
4328                if (atomic_dec_and_test(&ppc440spe_adma_err_irq_ref))
4329                        irq_dispose_mapping(adev->err_irq);
4330        }
4331        return ret;
4332}
4333
4334static void ppc440spe_adma_release_irqs(struct ppc440spe_adma_device *adev,
4335                                        struct ppc440spe_adma_chan *chan)
4336{
4337        u32 mask, disable;
4338
4339        if (adev->id == PPC440SPE_XOR_ID) {
4340                /* disable XOR engine interrupts */
4341                mask = ioread32be(&adev->xor_reg->ier);
4342                mask &= ~(XOR_IE_CBCIE_BIT | XOR_IE_ICBIE_BIT |
4343                          XOR_IE_ICIE_BIT | XOR_IE_RPTIE_BIT);
4344                iowrite32be(mask, &adev->xor_reg->ier);
4345        } else {
4346                /* disable DMAx engine interrupts */
4347                disable = (adev->id == PPC440SPE_DMA0_ID) ?
4348                          (I2O_IOPIM_P0SNE | I2O_IOPIM_P0EM) :
4349                          (I2O_IOPIM_P1SNE | I2O_IOPIM_P1EM);
4350                mask = ioread32(&adev->i2o_reg->iopim) | disable;
4351                iowrite32(mask, &adev->i2o_reg->iopim);
4352        }
4353        free_irq(adev->irq, chan);
4354        irq_dispose_mapping(adev->irq);
4355        if (adev->err_irq > 0) {
4356                free_irq(adev->err_irq, chan);
4357                if (atomic_dec_and_test(&ppc440spe_adma_err_irq_ref)) {
4358                        irq_dispose_mapping(adev->err_irq);
4359                        iounmap(adev->i2o_reg);
4360                }
4361        }
4362}
4363
4364/**
4365 * ppc440spe_adma_probe - probe the asynch device
4366 */
4367static int ppc440spe_adma_probe(struct platform_device *ofdev)
4368{
4369        struct device_node *np = ofdev->dev.of_node;
4370        struct resource res;
4371        struct ppc440spe_adma_device *adev;
4372        struct ppc440spe_adma_chan *chan;
4373        struct ppc_dma_chan_ref *ref, *_ref;
4374        int ret = 0, initcode = PPC_ADMA_INIT_OK;
4375        const u32 *idx;
4376        int len;
4377        void *regs;
4378        u32 id, pool_size;
4379
4380        if (of_device_is_compatible(np, "amcc,xor-accelerator")) {
4381                id = PPC440SPE_XOR_ID;
4382                /* As far as the XOR engine is concerned, it does not
4383                 * use FIFOs but uses linked list. So there is no dependency
4384                 * between pool size to allocate and the engine configuration.
4385                 */
4386                pool_size = PAGE_SIZE << 1;
4387        } else {
4388                /* it is DMA0 or DMA1 */
4389                idx = of_get_property(np, "cell-index", &len);
4390                if (!idx || (len != sizeof(u32))) {
4391                        dev_err(&ofdev->dev, "Device node %s has missing "
4392                                "or invalid cell-index property\n",
4393                                np->full_name);
4394                        return -EINVAL;
4395                }
4396                id = *idx;
4397                /* DMA0,1 engines use FIFO to maintain CDBs, so we
4398                 * should allocate the pool accordingly to size of this
4399                 * FIFO. Thus, the pool size depends on the FIFO depth:
4400                 * how much CDBs pointers the FIFO may contain then so
4401                 * much CDBs we should provide in the pool.
4402                 * That is
4403                 *   CDB size = 32B;
4404                 *   CDBs number = (DMA0_FIFO_SIZE >> 3);
4405                 *   Pool size = CDBs number * CDB size =
4406                 *      = (DMA0_FIFO_SIZE >> 3) << 5 = DMA0_FIFO_SIZE << 2.
4407                 */
4408                pool_size = (id == PPC440SPE_DMA0_ID) ?
4409                            DMA0_FIFO_SIZE : DMA1_FIFO_SIZE;
4410                pool_size <<= 2;
4411        }
4412
4413        if (of_address_to_resource(np, 0, &res)) {
4414                dev_err(&ofdev->dev, "failed to get memory resource\n");
4415                initcode = PPC_ADMA_INIT_MEMRES;
4416                ret = -ENODEV;
4417                goto out;
4418        }
4419
4420        if (!request_mem_region(res.start, resource_size(&res),
4421                                dev_driver_string(&ofdev->dev))) {
4422                dev_err(&ofdev->dev, "failed to request memory region %pR\n",
4423                        &res);
4424                initcode = PPC_ADMA_INIT_MEMREG;
4425                ret = -EBUSY;
4426                goto out;
4427        }
4428
4429        /* create a device */
4430        adev = kzalloc(sizeof(*adev), GFP_KERNEL);
4431        if (!adev) {
4432                dev_err(&ofdev->dev, "failed to allocate device\n");
4433                initcode = PPC_ADMA_INIT_ALLOC;
4434                ret = -ENOMEM;
4435                goto err_adev_alloc;
4436        }
4437
4438        adev->id = id;
4439        adev->pool_size = pool_size;
4440        /* allocate coherent memory for hardware descriptors */
4441        adev->dma_desc_pool_virt = dma_alloc_coherent(&ofdev->dev,
4442                                        adev->pool_size, &adev->dma_desc_pool,
4443                                        GFP_KERNEL);
4444        if (adev->dma_desc_pool_virt == NULL) {
4445                dev_err(&ofdev->dev, "failed to allocate %d bytes of coherent "
4446                        "memory for hardware descriptors\n",
4447                        adev->pool_size);
4448                initcode = PPC_ADMA_INIT_COHERENT;
4449                ret = -ENOMEM;
4450                goto err_dma_alloc;
4451        }
4452        dev_dbg(&ofdev->dev, "allocated descriptor pool virt 0x%p phys 0x%llx\n",
4453                adev->dma_desc_pool_virt, (u64)adev->dma_desc_pool);
4454
4455        regs = ioremap(res.start, resource_size(&res));
4456        if (!regs) {
4457                dev_err(&ofdev->dev, "failed to ioremap regs!\n");
4458                goto err_regs_alloc;
4459        }
4460
4461        if (adev->id == PPC440SPE_XOR_ID) {
4462                adev->xor_reg = regs;
4463                /* Reset XOR */
4464                iowrite32be(XOR_CRSR_XASR_BIT, &adev->xor_reg->crsr);
4465                iowrite32be(XOR_CRSR_64BA_BIT, &adev->xor_reg->crrr);
4466        } else {
4467                size_t fifo_size = (adev->id == PPC440SPE_DMA0_ID) ?
4468                                   DMA0_FIFO_SIZE : DMA1_FIFO_SIZE;
4469                adev->dma_reg = regs;
4470                /* DMAx_FIFO_SIZE is defined in bytes,
4471                 * <fsiz> - is defined in number of CDB pointers (8byte).
4472                 * DMA FIFO Length = CSlength + CPlength, where
4473                 * CSlength = CPlength = (fsiz + 1) * 8.
4474                 */
4475                iowrite32(DMA_FIFO_ENABLE | ((fifo_size >> 3) - 2),
4476                          &adev->dma_reg->fsiz);
4477                /* Configure DMA engine */
4478                iowrite32(DMA_CFG_DXEPR_HP | DMA_CFG_DFMPP_HP | DMA_CFG_FALGN,
4479                          &adev->dma_reg->cfg);
4480                /* Clear Status */
4481                iowrite32(~0, &adev->dma_reg->dsts);
4482        }
4483
4484        adev->dev = &ofdev->dev;
4485        adev->common.dev = &ofdev->dev;
4486        INIT_LIST_HEAD(&adev->common.channels);
4487        dev_set_drvdata(&ofdev->dev, adev);
4488
4489        /* create a channel */
4490        chan = kzalloc(sizeof(*chan), GFP_KERNEL);
4491        if (!chan) {
4492                dev_err(&ofdev->dev, "can't allocate channel structure\n");
4493                initcode = PPC_ADMA_INIT_CHANNEL;
4494                ret = -ENOMEM;
4495                goto err_chan_alloc;
4496        }
4497
4498        spin_lock_init(&chan->lock);
4499        INIT_LIST_HEAD(&chan->chain);
4500        INIT_LIST_HEAD(&chan->all_slots);
4501        chan->device = adev;
4502        chan->common.device = &adev->common;
4503        dma_cookie_init(&chan->common);
4504        list_add_tail(&chan->common.device_node, &adev->common.channels);
4505        tasklet_init(&chan->irq_tasklet, ppc440spe_adma_tasklet,
4506                     (unsigned long)chan);
4507
4508        /* allocate and map helper pages for async validation or
4509         * async_mult/async_sum_product operations on DMA0/1.
4510         */
4511        if (adev->id != PPC440SPE_XOR_ID) {
4512                chan->pdest_page = alloc_page(GFP_KERNEL);
4513                chan->qdest_page = alloc_page(GFP_KERNEL);
4514                if (!chan->pdest_page ||
4515                    !chan->qdest_page) {
4516                        if (chan->pdest_page)
4517                                __free_page(chan->pdest_page);
4518                        if (chan->qdest_page)
4519                                __free_page(chan->qdest_page);
4520                        ret = -ENOMEM;
4521                        goto err_page_alloc;
4522                }
4523                chan->pdest = dma_map_page(&ofdev->dev, chan->pdest_page, 0,
4524                                           PAGE_SIZE, DMA_BIDIRECTIONAL);
4525                chan->qdest = dma_map_page(&ofdev->dev, chan->qdest_page, 0,
4526                                           PAGE_SIZE, DMA_BIDIRECTIONAL);
4527        }
4528
4529        ref = kmalloc(sizeof(*ref), GFP_KERNEL);
4530        if (ref) {
4531                ref->chan = &chan->common;
4532                INIT_LIST_HEAD(&ref->node);
4533                list_add_tail(&ref->node, &ppc440spe_adma_chan_list);
4534        } else {
4535                dev_err(&ofdev->dev, "failed to allocate channel reference!\n");
4536                ret = -ENOMEM;
4537                goto err_ref_alloc;
4538        }
4539
4540        ret = ppc440spe_adma_setup_irqs(adev, chan, &initcode);
4541        if (ret)
4542                goto err_irq;
4543
4544        ppc440spe_adma_init_capabilities(adev);
4545
4546        ret = dma_async_device_register(&adev->common);
4547        if (ret) {
4548                initcode = PPC_ADMA_INIT_REGISTER;
4549                dev_err(&ofdev->dev, "failed to register dma device\n");
4550                goto err_dev_reg;
4551        }
4552
4553        goto out;
4554
4555err_dev_reg:
4556        ppc440spe_adma_release_irqs(adev, chan);
4557err_irq:
4558        list_for_each_entry_safe(ref, _ref, &ppc440spe_adma_chan_list, node) {
4559                if (chan == to_ppc440spe_adma_chan(ref->chan)) {
4560                        list_del(&ref->node);
4561                        kfree(ref);
4562                }
4563        }
4564err_ref_alloc:
4565        if (adev->id != PPC440SPE_XOR_ID) {
4566                dma_unmap_page(&ofdev->dev, chan->pdest,
4567                               PAGE_SIZE, DMA_BIDIRECTIONAL);
4568                dma_unmap_page(&ofdev->dev, chan->qdest,
4569                               PAGE_SIZE, DMA_BIDIRECTIONAL);
4570                __free_page(chan->pdest_page);
4571                __free_page(chan->qdest_page);
4572        }
4573err_page_alloc:
4574        kfree(chan);
4575err_chan_alloc:
4576        if (adev->id == PPC440SPE_XOR_ID)
4577                iounmap(adev->xor_reg);
4578        else
4579                iounmap(adev->dma_reg);
4580err_regs_alloc:
4581        dma_free_coherent(adev->dev, adev->pool_size,
4582                          adev->dma_desc_pool_virt,
4583                          adev->dma_desc_pool);
4584err_dma_alloc:
4585        kfree(adev);
4586err_adev_alloc:
4587        release_mem_region(res.start, resource_size(&res));
4588out:
4589        if (id < PPC440SPE_ADMA_ENGINES_NUM)
4590                ppc440spe_adma_devices[id] = initcode;
4591
4592        return ret;
4593}
4594
4595/**
4596 * ppc440spe_adma_remove - remove the asynch device
4597 */
4598static int ppc440spe_adma_remove(struct platform_device *ofdev)
4599{
4600        struct ppc440spe_adma_device *adev = dev_get_drvdata(&ofdev->dev);
4601        struct device_node *np = ofdev->dev.of_node;
4602        struct resource res;
4603        struct dma_chan *chan, *_chan;
4604        struct ppc_dma_chan_ref *ref, *_ref;
4605        struct ppc440spe_adma_chan *ppc440spe_chan;
4606
4607        dev_set_drvdata(&ofdev->dev, NULL);
4608        if (adev->id < PPC440SPE_ADMA_ENGINES_NUM)
4609                ppc440spe_adma_devices[adev->id] = -1;
4610
4611        dma_async_device_unregister(&adev->common);
4612
4613        list_for_each_entry_safe(chan, _chan, &adev->common.channels,
4614                                 device_node) {
4615                ppc440spe_chan = to_ppc440spe_adma_chan(chan);
4616                ppc440spe_adma_release_irqs(adev, ppc440spe_chan);
4617                tasklet_kill(&ppc440spe_chan->irq_tasklet);
4618                if (adev->id != PPC440SPE_XOR_ID) {
4619                        dma_unmap_page(&ofdev->dev, ppc440spe_chan->pdest,
4620                                        PAGE_SIZE, DMA_BIDIRECTIONAL);
4621                        dma_unmap_page(&ofdev->dev, ppc440spe_chan->qdest,
4622                                        PAGE_SIZE, DMA_BIDIRECTIONAL);
4623                        __free_page(ppc440spe_chan->pdest_page);
4624                        __free_page(ppc440spe_chan->qdest_page);
4625                }
4626                list_for_each_entry_safe(ref, _ref, &ppc440spe_adma_chan_list,
4627                                         node) {
4628                        if (ppc440spe_chan ==
4629                            to_ppc440spe_adma_chan(ref->chan)) {
4630                                list_del(&ref->node);
4631                                kfree(ref);
4632                        }
4633                }
4634                list_del(&chan->device_node);
4635                kfree(ppc440spe_chan);
4636        }
4637
4638        dma_free_coherent(adev->dev, adev->pool_size,
4639                          adev->dma_desc_pool_virt, adev->dma_desc_pool);
4640        if (adev->id == PPC440SPE_XOR_ID)
4641                iounmap(adev->xor_reg);
4642        else
4643                iounmap(adev->dma_reg);
4644        of_address_to_resource(np, 0, &res);
4645        release_mem_region(res.start, resource_size(&res));
4646        kfree(adev);
4647        return 0;
4648}
4649
4650/*
4651 * /sys driver interface to enable h/w RAID-6 capabilities
4652 * Files created in e.g. /sys/devices/plb.0/400100100.dma0/driver/
4653 * directory are "devices", "enable" and "poly".
4654 * "devices" shows available engines.
4655 * "enable" is used to enable RAID-6 capabilities or to check
4656 * whether these has been activated.
4657 * "poly" allows setting/checking used polynomial (for PPC440SPe only).
4658 */
4659
4660static ssize_t show_ppc440spe_devices(struct device_driver *dev, char *buf)
4661{
4662        ssize_t size = 0;
4663        int i;
4664
4665        for (i = 0; i < PPC440SPE_ADMA_ENGINES_NUM; i++) {
4666                if (ppc440spe_adma_devices[i] == -1)
4667                        continue;
4668                size += snprintf(buf + size, PAGE_SIZE - size,
4669                                 "PPC440SP(E)-ADMA.%d: %s\n", i,
4670                                 ppc_adma_errors[ppc440spe_adma_devices[i]]);
4671        }
4672        return size;
4673}
4674
4675static ssize_t show_ppc440spe_r6enable(struct device_driver *dev, char *buf)
4676{
4677        return snprintf(buf, PAGE_SIZE,
4678                        "PPC440SP(e) RAID-6 capabilities are %sABLED.\n",
4679                        ppc440spe_r6_enabled ? "EN" : "DIS");
4680}
4681
4682static ssize_t store_ppc440spe_r6enable(struct device_driver *dev,
4683                                        const char *buf, size_t count)
4684{
4685        unsigned long val;
4686
4687        if (!count || count > 11)
4688                return -EINVAL;
4689
4690        if (!ppc440spe_r6_tchan)
4691                return -EFAULT;
4692
4693        /* Write a key */
4694        sscanf(buf, "%lx", &val);
4695        dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_XORBA, val);
4696        isync();
4697
4698        /* Verify whether it really works now */
4699        if (ppc440spe_test_raid6(ppc440spe_r6_tchan) == 0) {
4700                pr_info("PPC440SP(e) RAID-6 has been activated "
4701                        "successfully\n");
4702                ppc440spe_r6_enabled = 1;
4703        } else {
4704                pr_info("PPC440SP(e) RAID-6 hasn't been activated!"
4705                        " Error key ?\n");
4706                ppc440spe_r6_enabled = 0;
4707        }
4708        return count;
4709}
4710
4711static ssize_t show_ppc440spe_r6poly(struct device_driver *dev, char *buf)
4712{
4713        ssize_t size = 0;
4714        u32 reg;
4715
4716#ifdef CONFIG_440SP
4717        /* 440SP has fixed polynomial */
4718        reg = 0x4d;
4719#else
4720        reg = dcr_read(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL);
4721        reg >>= MQ0_CFBHL_POLY;
4722        reg &= 0xFF;
4723#endif
4724
4725        size = snprintf(buf, PAGE_SIZE, "PPC440SP(e) RAID-6 driver "
4726                        "uses 0x1%02x polynomial.\n", reg);
4727        return size;
4728}
4729
4730static ssize_t store_ppc440spe_r6poly(struct device_driver *dev,
4731                                      const char *buf, size_t count)
4732{
4733        unsigned long reg, val;
4734
4735#ifdef CONFIG_440SP
4736        /* 440SP uses default 0x14D polynomial only */
4737        return -EINVAL;
4738#endif
4739
4740        if (!count || count > 6)
4741                return -EINVAL;
4742
4743        /* e.g., 0x14D or 0x11D */
4744        sscanf(buf, "%lx", &val);
4745
4746        if (val & ~0x1FF)
4747                return -EINVAL;
4748
4749        val &= 0xFF;
4750        reg = dcr_read(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL);
4751        reg &= ~(0xFF << MQ0_CFBHL_POLY);
4752        reg |= val << MQ0_CFBHL_POLY;
4753        dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL, reg);
4754
4755        return count;
4756}
4757
4758static DRIVER_ATTR(devices, S_IRUGO, show_ppc440spe_devices, NULL);
4759static DRIVER_ATTR(enable, S_IRUGO | S_IWUSR, show_ppc440spe_r6enable,
4760                   store_ppc440spe_r6enable);
4761static DRIVER_ATTR(poly, S_IRUGO | S_IWUSR, show_ppc440spe_r6poly,
4762                   store_ppc440spe_r6poly);
4763
4764/*
4765 * Common initialisation for RAID engines; allocate memory for
4766 * DMAx FIFOs, perform configuration common for all DMA engines.
4767 * Further DMA engine specific configuration is done at probe time.
4768 */
4769static int ppc440spe_configure_raid_devices(void)
4770{
4771        struct device_node *np;
4772        struct resource i2o_res;
4773        struct i2o_regs __iomem *i2o_reg;
4774        dcr_host_t i2o_dcr_host;
4775        unsigned int dcr_base, dcr_len;
4776        int i, ret;
4777
4778        np = of_find_compatible_node(NULL, NULL, "ibm,i2o-440spe");
4779        if (!np) {
4780                pr_err("%s: can't find I2O device tree node\n",
4781                        __func__);
4782                return -ENODEV;
4783        }
4784
4785        if (of_address_to_resource(np, 0, &i2o_res)) {
4786                of_node_put(np);
4787                return -EINVAL;
4788        }
4789
4790        i2o_reg = of_iomap(np, 0);
4791        if (!i2o_reg) {
4792                pr_err("%s: failed to map I2O registers\n", __func__);
4793                of_node_put(np);
4794                return -EINVAL;
4795        }
4796
4797        /* Get I2O DCRs base */
4798        dcr_base = dcr_resource_start(np, 0);
4799        dcr_len = dcr_resource_len(np, 0);
4800        if (!dcr_base && !dcr_len) {
4801                pr_err("%s: can't get DCR registers base/len!\n",
4802                        np->full_name);
4803                of_node_put(np);
4804                iounmap(i2o_reg);
4805                return -ENODEV;
4806        }
4807
4808        i2o_dcr_host = dcr_map(np, dcr_base, dcr_len);
4809        if (!DCR_MAP_OK(i2o_dcr_host)) {
4810                pr_err("%s: failed to map DCRs!\n", np->full_name);
4811                of_node_put(np);
4812                iounmap(i2o_reg);
4813                return -ENODEV;
4814        }
4815        of_node_put(np);
4816
4817        /* Provide memory regions for DMA's FIFOs: I2O, DMA0 and DMA1 share
4818         * the base address of FIFO memory space.
4819         * Actually we need twice more physical memory than programmed in the
4820         * <fsiz> register (because there are two FIFOs for each DMA: CP and CS)
4821         */
4822        ppc440spe_dma_fifo_buf = kmalloc((DMA0_FIFO_SIZE + DMA1_FIFO_SIZE) << 1,
4823                                         GFP_KERNEL);
4824        if (!ppc440spe_dma_fifo_buf) {
4825                pr_err("%s: DMA FIFO buffer allocation failed.\n", __func__);
4826                iounmap(i2o_reg);
4827                dcr_unmap(i2o_dcr_host, dcr_len);
4828                return -ENOMEM;
4829        }
4830
4831        /*
4832         * Configure h/w
4833         */
4834        /* Reset I2O/DMA */
4835        mtdcri(SDR0, DCRN_SDR0_SRST, DCRN_SDR0_SRST_I2ODMA);
4836        mtdcri(SDR0, DCRN_SDR0_SRST, 0);
4837
4838        /* Setup the base address of mmaped registers */
4839        dcr_write(i2o_dcr_host, DCRN_I2O0_IBAH, (u32)(i2o_res.start >> 32));
4840        dcr_write(i2o_dcr_host, DCRN_I2O0_IBAL, (u32)(i2o_res.start) |
4841                                                I2O_REG_ENABLE);
4842        dcr_unmap(i2o_dcr_host, dcr_len);
4843
4844        /* Setup FIFO memory space base address */
4845        iowrite32(0, &i2o_reg->ifbah);
4846        iowrite32(((u32)__pa(ppc440spe_dma_fifo_buf)), &i2o_reg->ifbal);
4847
4848        /* set zero FIFO size for I2O, so the whole
4849         * ppc440spe_dma_fifo_buf is used by DMAs.
4850         * DMAx_FIFOs will be configured while probe.
4851         */
4852        iowrite32(0, &i2o_reg->ifsiz);
4853        iounmap(i2o_reg);
4854
4855        /* To prepare WXOR/RXOR functionality we need access to
4856         * Memory Queue Module DCRs (finally it will be enabled
4857         * via /sys interface of the ppc440spe ADMA driver).
4858         */
4859        np = of_find_compatible_node(NULL, NULL, "ibm,mq-440spe");
4860        if (!np) {
4861                pr_err("%s: can't find MQ device tree node\n",
4862                        __func__);
4863                ret = -ENODEV;
4864                goto out_free;
4865        }
4866
4867        /* Get MQ DCRs base */
4868        dcr_base = dcr_resource_start(np, 0);
4869        dcr_len = dcr_resource_len(np, 0);
4870        if (!dcr_base && !dcr_len) {
4871                pr_err("%s: can't get DCR registers base/len!\n",
4872                        np->full_name);
4873                ret = -ENODEV;
4874                goto out_mq;
4875        }
4876
4877        ppc440spe_mq_dcr_host = dcr_map(np, dcr_base, dcr_len);
4878        if (!DCR_MAP_OK(ppc440spe_mq_dcr_host)) {
4879                pr_err("%s: failed to map DCRs!\n", np->full_name);
4880                ret = -ENODEV;
4881                goto out_mq;
4882        }
4883        of_node_put(np);
4884        ppc440spe_mq_dcr_len = dcr_len;
4885
4886        /* Set HB alias */
4887        dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_BAUH, DMA_CUED_XOR_HB);
4888
4889        /* Set:
4890         * - LL transaction passing limit to 1;
4891         * - Memory controller cycle limit to 1;
4892         * - Galois Polynomial to 0x14d (default)
4893         */
4894        dcr_write(ppc440spe_mq_dcr_host, DCRN_MQ0_CFBHL,
4895                  (1 << MQ0_CFBHL_TPLM) | (1 << MQ0_CFBHL_HBCL) |
4896                  (PPC440SPE_DEFAULT_POLY << MQ0_CFBHL_POLY));
4897
4898        atomic_set(&ppc440spe_adma_err_irq_ref, 0);
4899        for (i = 0; i < PPC440SPE_ADMA_ENGINES_NUM; i++)
4900                ppc440spe_adma_devices[i] = -1;
4901
4902        return 0;
4903
4904out_mq:
4905        of_node_put(np);
4906out_free:
4907        kfree(ppc440spe_dma_fifo_buf);
4908        return ret;
4909}
4910
4911static const struct of_device_id ppc440spe_adma_of_match[] = {
4912        { .compatible   = "ibm,dma-440spe", },
4913        { .compatible   = "amcc,xor-accelerator", },
4914        {},
4915};
4916MODULE_DEVICE_TABLE(of, ppc440spe_adma_of_match);
4917
4918static struct platform_driver ppc440spe_adma_driver = {
4919        .probe = ppc440spe_adma_probe,
4920        .remove = ppc440spe_adma_remove,
4921        .driver = {
4922                .name = "PPC440SP(E)-ADMA",
4923                .owner = THIS_MODULE,
4924                .of_match_table = ppc440spe_adma_of_match,
4925        },
4926};
4927
4928static __init int ppc440spe_adma_init(void)
4929{
4930        int ret;
4931
4932        ret = ppc440spe_configure_raid_devices();
4933        if (ret)
4934                return ret;
4935
4936        ret = platform_driver_register(&ppc440spe_adma_driver);
4937        if (ret) {
4938                pr_err("%s: failed to register platform driver\n",
4939                        __func__);
4940                goto out_reg;
4941        }
4942
4943        /* Initialization status */
4944        ret = driver_create_file(&ppc440spe_adma_driver.driver,
4945                                 &driver_attr_devices);
4946        if (ret)
4947                goto out_dev;
4948
4949        /* RAID-6 h/w enable entry */
4950        ret = driver_create_file(&ppc440spe_adma_driver.driver,
4951                                 &driver_attr_enable);
4952        if (ret)
4953                goto out_en;
4954
4955        /* GF polynomial to use */
4956        ret = driver_create_file(&ppc440spe_adma_driver.driver,
4957                                 &driver_attr_poly);
4958        if (!ret)
4959                return ret;
4960
4961        driver_remove_file(&ppc440spe_adma_driver.driver,
4962                           &driver_attr_enable);
4963out_en:
4964        driver_remove_file(&ppc440spe_adma_driver.driver,
4965                           &driver_attr_devices);
4966out_dev:
4967        /* User will not be able to enable h/w RAID-6 */
4968        pr_err("%s: failed to create RAID-6 driver interface\n",
4969                __func__);
4970        platform_driver_unregister(&ppc440spe_adma_driver);
4971out_reg:
4972        dcr_unmap(ppc440spe_mq_dcr_host, ppc440spe_mq_dcr_len);
4973        kfree(ppc440spe_dma_fifo_buf);
4974        return ret;
4975}
4976
4977static void __exit ppc440spe_adma_exit(void)
4978{
4979        driver_remove_file(&ppc440spe_adma_driver.driver,
4980                           &driver_attr_poly);
4981        driver_remove_file(&ppc440spe_adma_driver.driver,
4982                           &driver_attr_enable);
4983        driver_remove_file(&ppc440spe_adma_driver.driver,
4984                           &driver_attr_devices);
4985        platform_driver_unregister(&ppc440spe_adma_driver);
4986        dcr_unmap(ppc440spe_mq_dcr_host, ppc440spe_mq_dcr_len);
4987        kfree(ppc440spe_dma_fifo_buf);
4988}
4989
4990arch_initcall(ppc440spe_adma_init);
4991module_exit(ppc440spe_adma_exit);
4992
4993MODULE_AUTHOR("Yuri Tikhonov <yur@emcraft.com>");
4994MODULE_DESCRIPTION("PPC440SPE ADMA Engine Driver");
4995MODULE_LICENSE("GPL");
4996