Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2005-2006 by Texas Instruments
0004  *
0005  * This file implements a DMA  interface using TI's CPPI DMA.
0006  * For now it's DaVinci-only, but CPPI isn't specific to DaVinci or USB.
0007  * The TUSB6020, using VLYNQ, has CPPI that looks much like DaVinci.
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/slab.h>
0013 #include <linux/usb.h>
0014 
0015 #include "musb_core.h"
0016 #include "musb_debug.h"
0017 #include "cppi_dma.h"
0018 #include "davinci.h"
0019 
0020 
0021 /* CPPI DMA status 7-mar-2006:
0022  *
0023  * - See musb_{host,gadget}.c for more info
0024  *
0025  * - Correct RX DMA generally forces the engine into irq-per-packet mode,
0026  *   which can easily saturate the CPU under non-mass-storage loads.
0027  *
0028  * NOTES 24-aug-2006 (2.6.18-rc4):
0029  *
0030  * - peripheral RXDMA wedged in a test with packets of length 512/512/1.
0031  *   evidently after the 1 byte packet was received and acked, the queue
0032  *   of BDs got garbaged so it wouldn't empty the fifo.  (rxcsr 0x2003,
0033  *   and RX DMA0: 4 left, 80000000 8feff880, 8feff860 8feff860; 8f321401
0034  *   004001ff 00000001 .. 8feff860)  Host was just getting NAKed on tx
0035  *   of its next (512 byte) packet.  IRQ issues?
0036  *
0037  * REVISIT:  the "transfer DMA" glue between CPPI and USB fifos will
0038  * evidently also directly update the RX and TX CSRs ... so audit all
0039  * host and peripheral side DMA code to avoid CSR access after DMA has
0040  * been started.
0041  */
0042 
0043 /* REVISIT now we can avoid preallocating these descriptors; or
0044  * more simply, switch to a global freelist not per-channel ones.
0045  * Note: at full speed, 64 descriptors == 4K bulk data.
0046  */
0047 #define NUM_TXCHAN_BD       64
0048 #define NUM_RXCHAN_BD       64
0049 
0050 static inline void cpu_drain_writebuffer(void)
0051 {
0052     wmb();
0053 #ifdef  CONFIG_CPU_ARM926T
0054     /* REVISIT this "should not be needed",
0055      * but lack of it sure seemed to hurt ...
0056      */
0057     asm("mcr p15, 0, r0, c7, c10, 4 @ drain write buffer\n");
0058 #endif
0059 }
0060 
0061 static inline struct cppi_descriptor *cppi_bd_alloc(struct cppi_channel *c)
0062 {
0063     struct cppi_descriptor  *bd = c->freelist;
0064 
0065     if (bd)
0066         c->freelist = bd->next;
0067     return bd;
0068 }
0069 
0070 static inline void
0071 cppi_bd_free(struct cppi_channel *c, struct cppi_descriptor *bd)
0072 {
0073     if (!bd)
0074         return;
0075     bd->next = c->freelist;
0076     c->freelist = bd;
0077 }
0078 
0079 /*
0080  *  Start DMA controller
0081  *
0082  *  Initialize the DMA controller as necessary.
0083  */
0084 
0085 /* zero out entire rx state RAM entry for the channel */
0086 static void cppi_reset_rx(struct cppi_rx_stateram __iomem *rx)
0087 {
0088     musb_writel(&rx->rx_skipbytes, 0, 0);
0089     musb_writel(&rx->rx_head, 0, 0);
0090     musb_writel(&rx->rx_sop, 0, 0);
0091     musb_writel(&rx->rx_current, 0, 0);
0092     musb_writel(&rx->rx_buf_current, 0, 0);
0093     musb_writel(&rx->rx_len_len, 0, 0);
0094     musb_writel(&rx->rx_cnt_cnt, 0, 0);
0095 }
0096 
0097 /* zero out entire tx state RAM entry for the channel */
0098 static void cppi_reset_tx(struct cppi_tx_stateram __iomem *tx, u32 ptr)
0099 {
0100     musb_writel(&tx->tx_head, 0, 0);
0101     musb_writel(&tx->tx_buf, 0, 0);
0102     musb_writel(&tx->tx_current, 0, 0);
0103     musb_writel(&tx->tx_buf_current, 0, 0);
0104     musb_writel(&tx->tx_info, 0, 0);
0105     musb_writel(&tx->tx_rem_len, 0, 0);
0106     /* musb_writel(&tx->tx_dummy, 0, 0); */
0107     musb_writel(&tx->tx_complete, 0, ptr);
0108 }
0109 
0110 static void cppi_pool_init(struct cppi *cppi, struct cppi_channel *c)
0111 {
0112     int j;
0113 
0114     /* initialize channel fields */
0115     c->head = NULL;
0116     c->tail = NULL;
0117     c->last_processed = NULL;
0118     c->channel.status = MUSB_DMA_STATUS_UNKNOWN;
0119     c->controller = cppi;
0120     c->is_rndis = 0;
0121     c->freelist = NULL;
0122 
0123     /* build the BD Free list for the channel */
0124     for (j = 0; j < NUM_TXCHAN_BD + 1; j++) {
0125         struct cppi_descriptor  *bd;
0126         dma_addr_t      dma;
0127 
0128         bd = dma_pool_alloc(cppi->pool, GFP_KERNEL, &dma);
0129         bd->dma = dma;
0130         cppi_bd_free(c, bd);
0131     }
0132 }
0133 
0134 static int cppi_channel_abort(struct dma_channel *);
0135 
0136 static void cppi_pool_free(struct cppi_channel *c)
0137 {
0138     struct cppi     *cppi = c->controller;
0139     struct cppi_descriptor  *bd;
0140 
0141     (void) cppi_channel_abort(&c->channel);
0142     c->channel.status = MUSB_DMA_STATUS_UNKNOWN;
0143     c->controller = NULL;
0144 
0145     /* free all its bds */
0146     bd = c->last_processed;
0147     do {
0148         if (bd)
0149             dma_pool_free(cppi->pool, bd, bd->dma);
0150         bd = cppi_bd_alloc(c);
0151     } while (bd);
0152     c->last_processed = NULL;
0153 }
0154 
0155 static void cppi_controller_start(struct cppi *controller)
0156 {
0157     void __iomem    *tibase;
0158     int     i;
0159 
0160     /* do whatever is necessary to start controller */
0161     for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
0162         controller->tx[i].transmit = true;
0163         controller->tx[i].index = i;
0164     }
0165     for (i = 0; i < ARRAY_SIZE(controller->rx); i++) {
0166         controller->rx[i].transmit = false;
0167         controller->rx[i].index = i;
0168     }
0169 
0170     /* setup BD list on a per channel basis */
0171     for (i = 0; i < ARRAY_SIZE(controller->tx); i++)
0172         cppi_pool_init(controller, controller->tx + i);
0173     for (i = 0; i < ARRAY_SIZE(controller->rx); i++)
0174         cppi_pool_init(controller, controller->rx + i);
0175 
0176     tibase =  controller->tibase;
0177     INIT_LIST_HEAD(&controller->tx_complete);
0178 
0179     /* initialise tx/rx channel head pointers to zero */
0180     for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
0181         struct cppi_channel *tx_ch = controller->tx + i;
0182         struct cppi_tx_stateram __iomem *tx;
0183 
0184         INIT_LIST_HEAD(&tx_ch->tx_complete);
0185 
0186         tx = tibase + DAVINCI_TXCPPI_STATERAM_OFFSET(i);
0187         tx_ch->state_ram = tx;
0188         cppi_reset_tx(tx, 0);
0189     }
0190     for (i = 0; i < ARRAY_SIZE(controller->rx); i++) {
0191         struct cppi_channel *rx_ch = controller->rx + i;
0192         struct cppi_rx_stateram __iomem *rx;
0193 
0194         INIT_LIST_HEAD(&rx_ch->tx_complete);
0195 
0196         rx = tibase + DAVINCI_RXCPPI_STATERAM_OFFSET(i);
0197         rx_ch->state_ram = rx;
0198         cppi_reset_rx(rx);
0199     }
0200 
0201     /* enable individual cppi channels */
0202     musb_writel(tibase, DAVINCI_TXCPPI_INTENAB_REG,
0203             DAVINCI_DMA_ALL_CHANNELS_ENABLE);
0204     musb_writel(tibase, DAVINCI_RXCPPI_INTENAB_REG,
0205             DAVINCI_DMA_ALL_CHANNELS_ENABLE);
0206 
0207     /* enable tx/rx CPPI control */
0208     musb_writel(tibase, DAVINCI_TXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_ENABLE);
0209     musb_writel(tibase, DAVINCI_RXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_ENABLE);
0210 
0211     /* disable RNDIS mode, also host rx RNDIS autorequest */
0212     musb_writel(tibase, DAVINCI_RNDIS_REG, 0);
0213     musb_writel(tibase, DAVINCI_AUTOREQ_REG, 0);
0214 }
0215 
0216 /*
0217  *  Stop DMA controller
0218  *
0219  *  De-Init the DMA controller as necessary.
0220  */
0221 
0222 static void cppi_controller_stop(struct cppi *controller)
0223 {
0224     void __iomem        *tibase;
0225     int         i;
0226     struct musb     *musb;
0227 
0228     musb = controller->controller.musb;
0229 
0230     tibase = controller->tibase;
0231     /* DISABLE INDIVIDUAL CHANNEL Interrupts */
0232     musb_writel(tibase, DAVINCI_TXCPPI_INTCLR_REG,
0233             DAVINCI_DMA_ALL_CHANNELS_ENABLE);
0234     musb_writel(tibase, DAVINCI_RXCPPI_INTCLR_REG,
0235             DAVINCI_DMA_ALL_CHANNELS_ENABLE);
0236 
0237     musb_dbg(musb, "Tearing down RX and TX Channels");
0238     for (i = 0; i < ARRAY_SIZE(controller->tx); i++) {
0239         /* FIXME restructure of txdma to use bds like rxdma */
0240         controller->tx[i].last_processed = NULL;
0241         cppi_pool_free(controller->tx + i);
0242     }
0243     for (i = 0; i < ARRAY_SIZE(controller->rx); i++)
0244         cppi_pool_free(controller->rx + i);
0245 
0246     /* in Tx Case proper teardown is supported. We resort to disabling
0247      * Tx/Rx CPPI after cleanup of Tx channels. Before TX teardown is
0248      * complete TX CPPI cannot be disabled.
0249      */
0250     /*disable tx/rx cppi */
0251     musb_writel(tibase, DAVINCI_TXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_DISABLE);
0252     musb_writel(tibase, DAVINCI_RXCPPI_CTRL_REG, DAVINCI_DMA_CTRL_DISABLE);
0253 }
0254 
0255 /* While dma channel is allocated, we only want the core irqs active
0256  * for fault reports, otherwise we'd get irqs that we don't care about.
0257  * Except for TX irqs, where dma done != fifo empty and reusable ...
0258  *
0259  * NOTE: docs don't say either way, but irq masking **enables** irqs.
0260  *
0261  * REVISIT same issue applies to pure PIO usage too, and non-cppi dma...
0262  */
0263 static inline void core_rxirq_disable(void __iomem *tibase, unsigned epnum)
0264 {
0265     musb_writel(tibase, DAVINCI_USB_INT_MASK_CLR_REG, 1 << (epnum + 8));
0266 }
0267 
0268 static inline void core_rxirq_enable(void __iomem *tibase, unsigned epnum)
0269 {
0270     musb_writel(tibase, DAVINCI_USB_INT_MASK_SET_REG, 1 << (epnum + 8));
0271 }
0272 
0273 
0274 /*
0275  * Allocate a CPPI Channel for DMA.  With CPPI, channels are bound to
0276  * each transfer direction of a non-control endpoint, so allocating
0277  * (and deallocating) is mostly a way to notice bad housekeeping on
0278  * the software side.  We assume the irqs are always active.
0279  */
0280 static struct dma_channel *
0281 cppi_channel_allocate(struct dma_controller *c,
0282         struct musb_hw_ep *ep, u8 transmit)
0283 {
0284     struct cppi     *controller;
0285     u8          index;
0286     struct cppi_channel *cppi_ch;
0287     void __iomem        *tibase;
0288     struct musb     *musb;
0289 
0290     controller = container_of(c, struct cppi, controller);
0291     tibase = controller->tibase;
0292     musb = c->musb;
0293 
0294     /* ep0 doesn't use DMA; remember cppi indices are 0..N-1 */
0295     index = ep->epnum - 1;
0296 
0297     /* return the corresponding CPPI Channel Handle, and
0298      * probably disable the non-CPPI irq until we need it.
0299      */
0300     if (transmit) {
0301         if (index >= ARRAY_SIZE(controller->tx)) {
0302             musb_dbg(musb, "no %cX%d CPPI channel", 'T', index);
0303             return NULL;
0304         }
0305         cppi_ch = controller->tx + index;
0306     } else {
0307         if (index >= ARRAY_SIZE(controller->rx)) {
0308             musb_dbg(musb, "no %cX%d CPPI channel", 'R', index);
0309             return NULL;
0310         }
0311         cppi_ch = controller->rx + index;
0312         core_rxirq_disable(tibase, ep->epnum);
0313     }
0314 
0315     /* REVISIT make this an error later once the same driver code works
0316      * with the other DMA engine too
0317      */
0318     if (cppi_ch->hw_ep)
0319         musb_dbg(musb, "re-allocating DMA%d %cX channel %p",
0320                 index, transmit ? 'T' : 'R', cppi_ch);
0321     cppi_ch->hw_ep = ep;
0322     cppi_ch->channel.status = MUSB_DMA_STATUS_FREE;
0323     cppi_ch->channel.max_len = 0x7fffffff;
0324 
0325     musb_dbg(musb, "Allocate CPPI%d %cX", index, transmit ? 'T' : 'R');
0326     return &cppi_ch->channel;
0327 }
0328 
0329 /* Release a CPPI Channel.  */
0330 static void cppi_channel_release(struct dma_channel *channel)
0331 {
0332     struct cppi_channel *c;
0333     void __iomem        *tibase;
0334 
0335     /* REVISIT:  for paranoia, check state and abort if needed... */
0336 
0337     c = container_of(channel, struct cppi_channel, channel);
0338     tibase = c->controller->tibase;
0339     if (!c->hw_ep)
0340         musb_dbg(c->controller->controller.musb,
0341             "releasing idle DMA channel %p", c);
0342     else if (!c->transmit)
0343         core_rxirq_enable(tibase, c->index + 1);
0344 
0345     /* for now, leave its cppi IRQ enabled (we won't trigger it) */
0346     c->hw_ep = NULL;
0347     channel->status = MUSB_DMA_STATUS_UNKNOWN;
0348 }
0349 
0350 /* Context: controller irqlocked */
0351 static void
0352 cppi_dump_rx(int level, struct cppi_channel *c, const char *tag)
0353 {
0354     void __iomem            *base = c->controller->mregs;
0355     struct cppi_rx_stateram __iomem *rx = c->state_ram;
0356 
0357     musb_ep_select(base, c->index + 1);
0358 
0359     musb_dbg(c->controller->controller.musb,
0360         "RX DMA%d%s: %d left, csr %04x, "
0361         "%08x H%08x S%08x C%08x, "
0362         "B%08x L%08x %08x .. %08x",
0363         c->index, tag,
0364         musb_readl(c->controller->tibase,
0365             DAVINCI_RXCPPI_BUFCNT0_REG + 4 * c->index),
0366         musb_readw(c->hw_ep->regs, MUSB_RXCSR),
0367 
0368         musb_readl(&rx->rx_skipbytes, 0),
0369         musb_readl(&rx->rx_head, 0),
0370         musb_readl(&rx->rx_sop, 0),
0371         musb_readl(&rx->rx_current, 0),
0372 
0373         musb_readl(&rx->rx_buf_current, 0),
0374         musb_readl(&rx->rx_len_len, 0),
0375         musb_readl(&rx->rx_cnt_cnt, 0),
0376         musb_readl(&rx->rx_complete, 0)
0377         );
0378 }
0379 
0380 /* Context: controller irqlocked */
0381 static void
0382 cppi_dump_tx(int level, struct cppi_channel *c, const char *tag)
0383 {
0384     void __iomem            *base = c->controller->mregs;
0385     struct cppi_tx_stateram __iomem *tx = c->state_ram;
0386 
0387     musb_ep_select(base, c->index + 1);
0388 
0389     musb_dbg(c->controller->controller.musb,
0390         "TX DMA%d%s: csr %04x, "
0391         "H%08x S%08x C%08x %08x, "
0392         "F%08x L%08x .. %08x",
0393         c->index, tag,
0394         musb_readw(c->hw_ep->regs, MUSB_TXCSR),
0395 
0396         musb_readl(&tx->tx_head, 0),
0397         musb_readl(&tx->tx_buf, 0),
0398         musb_readl(&tx->tx_current, 0),
0399         musb_readl(&tx->tx_buf_current, 0),
0400 
0401         musb_readl(&tx->tx_info, 0),
0402         musb_readl(&tx->tx_rem_len, 0),
0403         /* dummy/unused word 6 */
0404         musb_readl(&tx->tx_complete, 0)
0405         );
0406 }
0407 
0408 /* Context: controller irqlocked */
0409 static inline void
0410 cppi_rndis_update(struct cppi_channel *c, int is_rx,
0411         void __iomem *tibase, int is_rndis)
0412 {
0413     /* we may need to change the rndis flag for this cppi channel */
0414     if (c->is_rndis != is_rndis) {
0415         u32 value = musb_readl(tibase, DAVINCI_RNDIS_REG);
0416         u32 temp = 1 << (c->index);
0417 
0418         if (is_rx)
0419             temp <<= 16;
0420         if (is_rndis)
0421             value |= temp;
0422         else
0423             value &= ~temp;
0424         musb_writel(tibase, DAVINCI_RNDIS_REG, value);
0425         c->is_rndis = is_rndis;
0426     }
0427 }
0428 
0429 static void cppi_dump_rxbd(const char *tag, struct cppi_descriptor *bd)
0430 {
0431     pr_debug("RXBD/%s %08x: "
0432             "nxt %08x buf %08x off.blen %08x opt.plen %08x\n",
0433             tag, bd->dma,
0434             bd->hw_next, bd->hw_bufp, bd->hw_off_len,
0435             bd->hw_options);
0436 }
0437 
0438 static void cppi_dump_rxq(int level, const char *tag, struct cppi_channel *rx)
0439 {
0440     struct cppi_descriptor  *bd;
0441 
0442     cppi_dump_rx(level, rx, tag);
0443     if (rx->last_processed)
0444         cppi_dump_rxbd("last", rx->last_processed);
0445     for (bd = rx->head; bd; bd = bd->next)
0446         cppi_dump_rxbd("active", bd);
0447 }
0448 
0449 
0450 /* NOTE:  DaVinci autoreq is ignored except for host side "RNDIS" mode RX;
0451  * so we won't ever use it (see "CPPI RX Woes" below).
0452  */
0453 static inline int cppi_autoreq_update(struct cppi_channel *rx,
0454         void __iomem *tibase, int onepacket, unsigned n_bds)
0455 {
0456     u32 val;
0457 
0458 #ifdef  RNDIS_RX_IS_USABLE
0459     u32 tmp;
0460     /* assert(is_host_active(musb)) */
0461 
0462     /* start from "AutoReq never" */
0463     tmp = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
0464     val = tmp & ~((0x3) << (rx->index * 2));
0465 
0466     /* HCD arranged reqpkt for packet #1.  we arrange int
0467      * for all but the last one, maybe in two segments.
0468      */
0469     if (!onepacket) {
0470 #if 0
0471         /* use two segments, autoreq "all" then the last "never" */
0472         val |= ((0x3) << (rx->index * 2));
0473         n_bds--;
0474 #else
0475         /* one segment, autoreq "all-but-last" */
0476         val |= ((0x1) << (rx->index * 2));
0477 #endif
0478     }
0479 
0480     if (val != tmp) {
0481         int n = 100;
0482 
0483         /* make sure that autoreq is updated before continuing */
0484         musb_writel(tibase, DAVINCI_AUTOREQ_REG, val);
0485         do {
0486             tmp = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
0487             if (tmp == val)
0488                 break;
0489             cpu_relax();
0490         } while (n-- > 0);
0491     }
0492 #endif
0493 
0494     /* REQPKT is turned off after each segment */
0495     if (n_bds && rx->channel.actual_len) {
0496         void __iomem    *regs = rx->hw_ep->regs;
0497 
0498         val = musb_readw(regs, MUSB_RXCSR);
0499         if (!(val & MUSB_RXCSR_H_REQPKT)) {
0500             val |= MUSB_RXCSR_H_REQPKT | MUSB_RXCSR_H_WZC_BITS;
0501             musb_writew(regs, MUSB_RXCSR, val);
0502             /* flush writebuffer */
0503             val = musb_readw(regs, MUSB_RXCSR);
0504         }
0505     }
0506     return n_bds;
0507 }
0508 
0509 
0510 /* Buffer enqueuing Logic:
0511  *
0512  *  - RX builds new queues each time, to help handle routine "early
0513  *    termination" cases (faults, including errors and short reads)
0514  *    more correctly.
0515  *
0516  *  - for now, TX reuses the same queue of BDs every time
0517  *
0518  * REVISIT long term, we want a normal dynamic model.
0519  * ... the goal will be to append to the
0520  * existing queue, processing completed "dma buffers" (segments) on the fly.
0521  *
0522  * Otherwise we force an IRQ latency between requests, which slows us a lot
0523  * (especially in "transparent" dma).  Unfortunately that model seems to be
0524  * inherent in the DMA model from the Mentor code, except in the rare case
0525  * of transfers big enough (~128+ KB) that we could append "middle" segments
0526  * in the TX paths.  (RX can't do this, see below.)
0527  *
0528  * That's true even in the CPPI- friendly iso case, where most urbs have
0529  * several small segments provided in a group and where the "packet at a time"
0530  * "transparent" DMA model is always correct, even on the RX side.
0531  */
0532 
0533 /*
0534  * CPPI TX:
0535  * ========
0536  * TX is a lot more reasonable than RX; it doesn't need to run in
0537  * irq-per-packet mode very often.  RNDIS mode seems to behave too
0538  * (except how it handles the exactly-N-packets case).  Building a
0539  * txdma queue with multiple requests (urb or usb_request) looks
0540  * like it would work ... but fault handling would need much testing.
0541  *
0542  * The main issue with TX mode RNDIS relates to transfer lengths that
0543  * are an exact multiple of the packet length.  It appears that there's
0544  * a hiccup in that case (maybe the DMA completes before the ZLP gets
0545  * written?) boiling down to not being able to rely on CPPI writing any
0546  * terminating zero length packet before the next transfer is written.
0547  * So that's punted to PIO; better yet, gadget drivers can avoid it.
0548  *
0549  * Plus, there's allegedly an undocumented constraint that rndis transfer
0550  * length be a multiple of 64 bytes ... but the chip doesn't act that
0551  * way, and we really don't _want_ that behavior anyway.
0552  *
0553  * On TX, "transparent" mode works ... although experiments have shown
0554  * problems trying to use the SOP/EOP bits in different USB packets.
0555  *
0556  * REVISIT try to handle terminating zero length packets using CPPI
0557  * instead of doing it by PIO after an IRQ.  (Meanwhile, make Ethernet
0558  * links avoid that issue by forcing them to avoid zlps.)
0559  */
0560 static void
0561 cppi_next_tx_segment(struct musb *musb, struct cppi_channel *tx)
0562 {
0563     unsigned        maxpacket = tx->maxpacket;
0564     dma_addr_t      addr = tx->buf_dma + tx->offset;
0565     size_t          length = tx->buf_len - tx->offset;
0566     struct cppi_descriptor  *bd;
0567     unsigned        n_bds;
0568     unsigned        i;
0569     struct cppi_tx_stateram __iomem *tx_ram = tx->state_ram;
0570     int         rndis;
0571 
0572     /* TX can use the CPPI "rndis" mode, where we can probably fit this
0573      * transfer in one BD and one IRQ.  The only time we would NOT want
0574      * to use it is when hardware constraints prevent it, or if we'd
0575      * trigger the "send a ZLP?" confusion.
0576      */
0577     rndis = (maxpacket & 0x3f) == 0
0578         && length > maxpacket
0579         && length < 0xffff
0580         && (length % maxpacket) != 0;
0581 
0582     if (rndis) {
0583         maxpacket = length;
0584         n_bds = 1;
0585     } else {
0586         if (length)
0587             n_bds = DIV_ROUND_UP(length, maxpacket);
0588         else
0589             n_bds = 1;
0590         n_bds = min(n_bds, (unsigned) NUM_TXCHAN_BD);
0591         length = min(n_bds * maxpacket, length);
0592     }
0593 
0594     musb_dbg(musb, "TX DMA%d, pktSz %d %s bds %d dma 0x%llx len %u",
0595             tx->index,
0596             maxpacket,
0597             rndis ? "rndis" : "transparent",
0598             n_bds,
0599             (unsigned long long)addr, length);
0600 
0601     cppi_rndis_update(tx, 0, musb->ctrl_base, rndis);
0602 
0603     /* assuming here that channel_program is called during
0604      * transfer initiation ... current code maintains state
0605      * for one outstanding request only (no queues, not even
0606      * the implicit ones of an iso urb).
0607      */
0608 
0609     bd = tx->freelist;
0610     tx->head = bd;
0611     tx->last_processed = NULL;
0612 
0613     /* FIXME use BD pool like RX side does, and just queue
0614      * the minimum number for this request.
0615      */
0616 
0617     /* Prepare queue of BDs first, then hand it to hardware.
0618      * All BDs except maybe the last should be of full packet
0619      * size; for RNDIS there _is_ only that last packet.
0620      */
0621     for (i = 0; i < n_bds; ) {
0622         if (++i < n_bds && bd->next)
0623             bd->hw_next = bd->next->dma;
0624         else
0625             bd->hw_next = 0;
0626 
0627         bd->hw_bufp = tx->buf_dma + tx->offset;
0628 
0629         /* FIXME set EOP only on the last packet,
0630          * SOP only on the first ... avoid IRQs
0631          */
0632         if ((tx->offset + maxpacket) <= tx->buf_len) {
0633             tx->offset += maxpacket;
0634             bd->hw_off_len = maxpacket;
0635             bd->hw_options = CPPI_SOP_SET | CPPI_EOP_SET
0636                 | CPPI_OWN_SET | maxpacket;
0637         } else {
0638             /* only this one may be a partial USB Packet */
0639             u32     partial_len;
0640 
0641             partial_len = tx->buf_len - tx->offset;
0642             tx->offset = tx->buf_len;
0643             bd->hw_off_len = partial_len;
0644 
0645             bd->hw_options = CPPI_SOP_SET | CPPI_EOP_SET
0646                 | CPPI_OWN_SET | partial_len;
0647             if (partial_len == 0)
0648                 bd->hw_options |= CPPI_ZERO_SET;
0649         }
0650 
0651         musb_dbg(musb, "TXBD %p: nxt %08x buf %08x len %04x opt %08x",
0652                 bd, bd->hw_next, bd->hw_bufp,
0653                 bd->hw_off_len, bd->hw_options);
0654 
0655         /* update the last BD enqueued to the list */
0656         tx->tail = bd;
0657         bd = bd->next;
0658     }
0659 
0660     /* BDs live in DMA-coherent memory, but writes might be pending */
0661     cpu_drain_writebuffer();
0662 
0663     /* Write to the HeadPtr in state RAM to trigger */
0664     musb_writel(&tx_ram->tx_head, 0, (u32)tx->freelist->dma);
0665 
0666     cppi_dump_tx(5, tx, "/S");
0667 }
0668 
0669 /*
0670  * CPPI RX Woes:
0671  * =============
0672  * Consider a 1KB bulk RX buffer in two scenarios:  (a) it's fed two 300 byte
0673  * packets back-to-back, and (b) it's fed two 512 byte packets back-to-back.
0674  * (Full speed transfers have similar scenarios.)
0675  *
0676  * The correct behavior for Linux is that (a) fills the buffer with 300 bytes,
0677  * and the next packet goes into a buffer that's queued later; while (b) fills
0678  * the buffer with 1024 bytes.  How to do that with CPPI?
0679  *
0680  * - RX queues in "rndis" mode -- one single BD -- handle (a) correctly, but
0681  *   (b) loses **BADLY** because nothing (!) happens when that second packet
0682  *   fills the buffer, much less when a third one arrives.  (Which makes this
0683  *   not a "true" RNDIS mode.  In the RNDIS protocol short-packet termination
0684  *   is optional, and it's fine if peripherals -- not hosts! -- pad messages
0685  *   out to end-of-buffer.  Standard PCI host controller DMA descriptors
0686  *   implement that mode by default ... which is no accident.)
0687  *
0688  * - RX queues in "transparent" mode -- two BDs with 512 bytes each -- have
0689  *   converse problems:  (b) is handled right, but (a) loses badly.  CPPI RX
0690  *   ignores SOP/EOP markings and processes both of those BDs; so both packets
0691  *   are loaded into the buffer (with a 212 byte gap between them), and the next
0692  *   buffer queued will NOT get its 300 bytes of data. (It seems like SOP/EOP
0693  *   are intended as outputs for RX queues, not inputs...)
0694  *
0695  * - A variant of "transparent" mode -- one BD at a time -- is the only way to
0696  *   reliably make both cases work, with software handling both cases correctly
0697  *   and at the significant penalty of needing an IRQ per packet.  (The lack of
0698  *   I/O overlap can be slightly ameliorated by enabling double buffering.)
0699  *
0700  * So how to get rid of IRQ-per-packet?  The transparent multi-BD case could
0701  * be used in special cases like mass storage, which sets URB_SHORT_NOT_OK
0702  * (or maybe its peripheral side counterpart) to flag (a) scenarios as errors
0703  * with guaranteed driver level fault recovery and scrubbing out what's left
0704  * of that garbaged datastream.
0705  *
0706  * But there seems to be no way to identify the cases where CPPI RNDIS mode
0707  * is appropriate -- which do NOT include RNDIS host drivers, but do include
0708  * the CDC Ethernet driver! -- and the documentation is incomplete/wrong.
0709  * So we can't _ever_ use RX RNDIS mode ... except by using a heuristic
0710  * that applies best on the peripheral side (and which could fail rudely).
0711  *
0712  * Leaving only "transparent" mode; we avoid multi-bd modes in almost all
0713  * cases other than mass storage class.  Otherwise we're correct but slow,
0714  * since CPPI penalizes our need for a "true RNDIS" default mode.
0715  */
0716 
0717 
0718 /* Heuristic, intended to kick in for ethernet/rndis peripheral ONLY
0719  *
0720  * IFF
0721  *  (a) peripheral mode ... since rndis peripherals could pad their
0722  *  writes to hosts, causing i/o failure; or we'd have to cope with
0723  *  a largely unknowable variety of host side protocol variants
0724  *  (b) and short reads are NOT errors ... since full reads would
0725  *  cause those same i/o failures
0726  *  (c) and read length is
0727  *  - less than 64KB (max per cppi descriptor)
0728  *  - not a multiple of 4096 (g_zero default, full reads typical)
0729  *  - N (>1) packets long, ditto (full reads not EXPECTED)
0730  * THEN
0731  *   try rx rndis mode
0732  *
0733  * Cost of heuristic failing:  RXDMA wedges at the end of transfers that
0734  * fill out the whole buffer.  Buggy host side usb network drivers could
0735  * trigger that, but "in the field" such bugs seem to be all but unknown.
0736  *
0737  * So this module parameter lets the heuristic be disabled.  When using
0738  * gadgetfs, the heuristic will probably need to be disabled.
0739  */
0740 static bool cppi_rx_rndis = 1;
0741 
0742 module_param(cppi_rx_rndis, bool, 0);
0743 MODULE_PARM_DESC(cppi_rx_rndis, "enable/disable RX RNDIS heuristic");
0744 
0745 
0746 /**
0747  * cppi_next_rx_segment - dma read for the next chunk of a buffer
0748  * @musb: the controller
0749  * @rx: dma channel
0750  * @onepacket: true unless caller treats short reads as errors, and
0751  *  performs fault recovery above usbcore.
0752  * Context: controller irqlocked
0753  *
0754  * See above notes about why we can't use multi-BD RX queues except in
0755  * rare cases (mass storage class), and can never use the hardware "rndis"
0756  * mode (since it's not a "true" RNDIS mode) with complete safety..
0757  *
0758  * It's ESSENTIAL that callers specify "onepacket" mode unless they kick in
0759  * code to recover from corrupted datastreams after each short transfer.
0760  */
0761 static void
0762 cppi_next_rx_segment(struct musb *musb, struct cppi_channel *rx, int onepacket)
0763 {
0764     unsigned        maxpacket = rx->maxpacket;
0765     dma_addr_t      addr = rx->buf_dma + rx->offset;
0766     size_t          length = rx->buf_len - rx->offset;
0767     struct cppi_descriptor  *bd, *tail;
0768     unsigned        n_bds;
0769     unsigned        i;
0770     void __iomem        *tibase = musb->ctrl_base;
0771     int         is_rndis = 0;
0772     struct cppi_rx_stateram __iomem *rx_ram = rx->state_ram;
0773     struct cppi_descriptor  *d;
0774 
0775     if (onepacket) {
0776         /* almost every USB driver, host or peripheral side */
0777         n_bds = 1;
0778 
0779         /* maybe apply the heuristic above */
0780         if (cppi_rx_rndis
0781                 && is_peripheral_active(musb)
0782                 && length > maxpacket
0783                 && (length & ~0xffff) == 0
0784                 && (length & 0x0fff) != 0
0785                 && (length & (maxpacket - 1)) == 0) {
0786             maxpacket = length;
0787             is_rndis = 1;
0788         }
0789     } else {
0790         /* virtually nothing except mass storage class */
0791         if (length > 0xffff) {
0792             n_bds = 0xffff / maxpacket;
0793             length = n_bds * maxpacket;
0794         } else {
0795             n_bds = DIV_ROUND_UP(length, maxpacket);
0796         }
0797         if (n_bds == 1)
0798             onepacket = 1;
0799         else
0800             n_bds = min(n_bds, (unsigned) NUM_RXCHAN_BD);
0801     }
0802 
0803     /* In host mode, autorequest logic can generate some IN tokens; it's
0804      * tricky since we can't leave REQPKT set in RXCSR after the transfer
0805      * finishes. So:  multipacket transfers involve two or more segments.
0806      * And always at least two IRQs ... RNDIS mode is not an option.
0807      */
0808     if (is_host_active(musb))
0809         n_bds = cppi_autoreq_update(rx, tibase, onepacket, n_bds);
0810 
0811     cppi_rndis_update(rx, 1, musb->ctrl_base, is_rndis);
0812 
0813     length = min(n_bds * maxpacket, length);
0814 
0815     musb_dbg(musb, "RX DMA%d seg, maxp %d %s bds %d (cnt %d) "
0816             "dma 0x%llx len %u %u/%u",
0817             rx->index, maxpacket,
0818             onepacket
0819                 ? (is_rndis ? "rndis" : "onepacket")
0820                 : "multipacket",
0821             n_bds,
0822             musb_readl(tibase,
0823                 DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
0824                     & 0xffff,
0825             (unsigned long long)addr, length,
0826             rx->channel.actual_len, rx->buf_len);
0827 
0828     /* only queue one segment at a time, since the hardware prevents
0829      * correct queue shutdown after unexpected short packets
0830      */
0831     bd = cppi_bd_alloc(rx);
0832     rx->head = bd;
0833 
0834     /* Build BDs for all packets in this segment */
0835     for (i = 0, tail = NULL; bd && i < n_bds; i++, tail = bd) {
0836         u32 bd_len;
0837 
0838         if (i) {
0839             bd = cppi_bd_alloc(rx);
0840             if (!bd)
0841                 break;
0842             tail->next = bd;
0843             tail->hw_next = bd->dma;
0844         }
0845         bd->hw_next = 0;
0846 
0847         /* all but the last packet will be maxpacket size */
0848         if (maxpacket < length)
0849             bd_len = maxpacket;
0850         else
0851             bd_len = length;
0852 
0853         bd->hw_bufp = addr;
0854         addr += bd_len;
0855         rx->offset += bd_len;
0856 
0857         bd->hw_off_len = (0 /*offset*/ << 16) + bd_len;
0858         bd->buflen = bd_len;
0859 
0860         bd->hw_options = CPPI_OWN_SET | (i == 0 ? length : 0);
0861         length -= bd_len;
0862     }
0863 
0864     /* we always expect at least one reusable BD! */
0865     if (!tail) {
0866         WARNING("rx dma%d -- no BDs? need %d\n", rx->index, n_bds);
0867         return;
0868     } else if (i < n_bds)
0869         WARNING("rx dma%d -- only %d of %d BDs\n", rx->index, i, n_bds);
0870 
0871     tail->next = NULL;
0872     tail->hw_next = 0;
0873 
0874     bd = rx->head;
0875     rx->tail = tail;
0876 
0877     /* short reads and other faults should terminate this entire
0878      * dma segment.  we want one "dma packet" per dma segment, not
0879      * one per USB packet, terminating the whole queue at once...
0880      * NOTE that current hardware seems to ignore SOP and EOP.
0881      */
0882     bd->hw_options |= CPPI_SOP_SET;
0883     tail->hw_options |= CPPI_EOP_SET;
0884 
0885     for (d = rx->head; d; d = d->next)
0886         cppi_dump_rxbd("S", d);
0887 
0888     /* in case the preceding transfer left some state... */
0889     tail = rx->last_processed;
0890     if (tail) {
0891         tail->next = bd;
0892         tail->hw_next = bd->dma;
0893     }
0894 
0895     core_rxirq_enable(tibase, rx->index + 1);
0896 
0897     /* BDs live in DMA-coherent memory, but writes might be pending */
0898     cpu_drain_writebuffer();
0899 
0900     /* REVISIT specs say to write this AFTER the BUFCNT register
0901      * below ... but that loses badly.
0902      */
0903     musb_writel(&rx_ram->rx_head, 0, bd->dma);
0904 
0905     /* bufferCount must be at least 3, and zeroes on completion
0906      * unless it underflows below zero, or stops at two, or keeps
0907      * growing ... grr.
0908      */
0909     i = musb_readl(tibase,
0910             DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
0911             & 0xffff;
0912 
0913     if (!i)
0914         musb_writel(tibase,
0915             DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
0916             n_bds + 2);
0917     else if (n_bds > (i - 3))
0918         musb_writel(tibase,
0919             DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
0920             n_bds - (i - 3));
0921 
0922     i = musb_readl(tibase,
0923             DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4))
0924             & 0xffff;
0925     if (i < (2 + n_bds)) {
0926         musb_dbg(musb, "bufcnt%d underrun - %d (for %d)",
0927                     rx->index, i, n_bds);
0928         musb_writel(tibase,
0929             DAVINCI_RXCPPI_BUFCNT0_REG + (rx->index * 4),
0930             n_bds + 2);
0931     }
0932 
0933     cppi_dump_rx(4, rx, "/S");
0934 }
0935 
0936 /**
0937  * cppi_channel_program - program channel for data transfer
0938  * @ch: the channel
0939  * @maxpacket: max packet size
0940  * @mode: For RX, 1 unless the usb protocol driver promised to treat
0941  *  all short reads as errors and kick in high level fault recovery.
0942  *  For TX, ignored because of RNDIS mode races/glitches.
0943  * @dma_addr: dma address of buffer
0944  * @len: length of buffer
0945  * Context: controller irqlocked
0946  */
0947 static int cppi_channel_program(struct dma_channel *ch,
0948         u16 maxpacket, u8 mode,
0949         dma_addr_t dma_addr, u32 len)
0950 {
0951     struct cppi_channel *cppi_ch;
0952     struct cppi     *controller;
0953     struct musb     *musb;
0954 
0955     cppi_ch = container_of(ch, struct cppi_channel, channel);
0956     controller = cppi_ch->controller;
0957     musb = controller->controller.musb;
0958 
0959     switch (ch->status) {
0960     case MUSB_DMA_STATUS_BUS_ABORT:
0961     case MUSB_DMA_STATUS_CORE_ABORT:
0962         /* fault irq handler should have handled cleanup */
0963         WARNING("%cX DMA%d not cleaned up after abort!\n",
0964                 cppi_ch->transmit ? 'T' : 'R',
0965                 cppi_ch->index);
0966         /* WARN_ON(1); */
0967         break;
0968     case MUSB_DMA_STATUS_BUSY:
0969         WARNING("program active channel?  %cX DMA%d\n",
0970                 cppi_ch->transmit ? 'T' : 'R',
0971                 cppi_ch->index);
0972         /* WARN_ON(1); */
0973         break;
0974     case MUSB_DMA_STATUS_UNKNOWN:
0975         musb_dbg(musb, "%cX DMA%d not allocated!",
0976                 cppi_ch->transmit ? 'T' : 'R',
0977                 cppi_ch->index);
0978         fallthrough;
0979     case MUSB_DMA_STATUS_FREE:
0980         break;
0981     }
0982 
0983     ch->status = MUSB_DMA_STATUS_BUSY;
0984 
0985     /* set transfer parameters, then queue up its first segment */
0986     cppi_ch->buf_dma = dma_addr;
0987     cppi_ch->offset = 0;
0988     cppi_ch->maxpacket = maxpacket;
0989     cppi_ch->buf_len = len;
0990     cppi_ch->channel.actual_len = 0;
0991 
0992     /* TX channel? or RX? */
0993     if (cppi_ch->transmit)
0994         cppi_next_tx_segment(musb, cppi_ch);
0995     else
0996         cppi_next_rx_segment(musb, cppi_ch, mode);
0997 
0998     return true;
0999 }
1000 
1001 static bool cppi_rx_scan(struct cppi *cppi, unsigned ch)
1002 {
1003     struct cppi_channel     *rx = &cppi->rx[ch];
1004     struct cppi_rx_stateram __iomem *state = rx->state_ram;
1005     struct cppi_descriptor      *bd;
1006     struct cppi_descriptor      *last = rx->last_processed;
1007     bool                completed = false;
1008     bool                acked = false;
1009     int             i;
1010     dma_addr_t          safe2ack;
1011     void __iomem            *regs = rx->hw_ep->regs;
1012     struct musb         *musb = cppi->controller.musb;
1013 
1014     cppi_dump_rx(6, rx, "/K");
1015 
1016     bd = last ? last->next : rx->head;
1017     if (!bd)
1018         return false;
1019 
1020     /* run through all completed BDs */
1021     for (i = 0, safe2ack = musb_readl(&state->rx_complete, 0);
1022             (safe2ack || completed) && bd && i < NUM_RXCHAN_BD;
1023             i++, bd = bd->next) {
1024         u16 len;
1025 
1026         /* catch latest BD writes from CPPI */
1027         rmb();
1028         if (!completed && (bd->hw_options & CPPI_OWN_SET))
1029             break;
1030 
1031         musb_dbg(musb, "C/RXBD %llx: nxt %08x buf %08x "
1032             "off.len %08x opt.len %08x (%d)",
1033             (unsigned long long)bd->dma, bd->hw_next, bd->hw_bufp,
1034             bd->hw_off_len, bd->hw_options,
1035             rx->channel.actual_len);
1036 
1037         /* actual packet received length */
1038         if ((bd->hw_options & CPPI_SOP_SET) && !completed)
1039             len = bd->hw_off_len & CPPI_RECV_PKTLEN_MASK;
1040         else
1041             len = 0;
1042 
1043         if (bd->hw_options & CPPI_EOQ_MASK)
1044             completed = true;
1045 
1046         if (!completed && len < bd->buflen) {
1047             /* NOTE:  when we get a short packet, RXCSR_H_REQPKT
1048              * must have been cleared, and no more DMA packets may
1049              * active be in the queue... TI docs didn't say, but
1050              * CPPI ignores those BDs even though OWN is still set.
1051              */
1052             completed = true;
1053             musb_dbg(musb, "rx short %d/%d (%d)",
1054                     len, bd->buflen,
1055                     rx->channel.actual_len);
1056         }
1057 
1058         /* If we got here, we expect to ack at least one BD; meanwhile
1059          * CPPI may completing other BDs while we scan this list...
1060          *
1061          * RACE: we can notice OWN cleared before CPPI raises the
1062          * matching irq by writing that BD as the completion pointer.
1063          * In such cases, stop scanning and wait for the irq, avoiding
1064          * lost acks and states where BD ownership is unclear.
1065          */
1066         if (bd->dma == safe2ack) {
1067             musb_writel(&state->rx_complete, 0, safe2ack);
1068             safe2ack = musb_readl(&state->rx_complete, 0);
1069             acked = true;
1070             if (bd->dma == safe2ack)
1071                 safe2ack = 0;
1072         }
1073 
1074         rx->channel.actual_len += len;
1075 
1076         cppi_bd_free(rx, last);
1077         last = bd;
1078 
1079         /* stop scanning on end-of-segment */
1080         if (bd->hw_next == 0)
1081             completed = true;
1082     }
1083     rx->last_processed = last;
1084 
1085     /* dma abort, lost ack, or ... */
1086     if (!acked && last) {
1087         int csr;
1088 
1089         if (safe2ack == 0 || safe2ack == rx->last_processed->dma)
1090             musb_writel(&state->rx_complete, 0, safe2ack);
1091         if (safe2ack == 0) {
1092             cppi_bd_free(rx, last);
1093             rx->last_processed = NULL;
1094 
1095             /* if we land here on the host side, H_REQPKT will
1096              * be clear and we need to restart the queue...
1097              */
1098             WARN_ON(rx->head);
1099         }
1100         musb_ep_select(cppi->mregs, rx->index + 1);
1101         csr = musb_readw(regs, MUSB_RXCSR);
1102         if (csr & MUSB_RXCSR_DMAENAB) {
1103             musb_dbg(musb, "list%d %p/%p, last %llx%s, csr %04x",
1104                 rx->index,
1105                 rx->head, rx->tail,
1106                 rx->last_processed
1107                     ? (unsigned long long)
1108                         rx->last_processed->dma
1109                     : 0,
1110                 completed ? ", completed" : "",
1111                 csr);
1112             cppi_dump_rxq(4, "/what?", rx);
1113         }
1114     }
1115     if (!completed) {
1116         int csr;
1117 
1118         rx->head = bd;
1119 
1120         /* REVISIT seems like "autoreq all but EOP" doesn't...
1121          * setting it here "should" be racey, but seems to work
1122          */
1123         csr = musb_readw(rx->hw_ep->regs, MUSB_RXCSR);
1124         if (is_host_active(cppi->controller.musb)
1125                 && bd
1126                 && !(csr & MUSB_RXCSR_H_REQPKT)) {
1127             csr |= MUSB_RXCSR_H_REQPKT;
1128             musb_writew(regs, MUSB_RXCSR,
1129                     MUSB_RXCSR_H_WZC_BITS | csr);
1130             csr = musb_readw(rx->hw_ep->regs, MUSB_RXCSR);
1131         }
1132     } else {
1133         rx->head = NULL;
1134         rx->tail = NULL;
1135     }
1136 
1137     cppi_dump_rx(6, rx, completed ? "/completed" : "/cleaned");
1138     return completed;
1139 }
1140 
1141 irqreturn_t cppi_interrupt(int irq, void *dev_id)
1142 {
1143     struct musb     *musb = dev_id;
1144     struct cppi     *cppi;
1145     void __iomem        *tibase;
1146     struct musb_hw_ep   *hw_ep = NULL;
1147     u32         rx, tx;
1148     int         i, index;
1149     unsigned long       flags;
1150 
1151     cppi = container_of(musb->dma_controller, struct cppi, controller);
1152     if (cppi->irq)
1153         spin_lock_irqsave(&musb->lock, flags);
1154 
1155     tibase = musb->ctrl_base;
1156 
1157     tx = musb_readl(tibase, DAVINCI_TXCPPI_MASKED_REG);
1158     rx = musb_readl(tibase, DAVINCI_RXCPPI_MASKED_REG);
1159 
1160     if (!tx && !rx) {
1161         if (cppi->irq)
1162             spin_unlock_irqrestore(&musb->lock, flags);
1163         return IRQ_NONE;
1164     }
1165 
1166     musb_dbg(musb, "CPPI IRQ Tx%x Rx%x", tx, rx);
1167 
1168     /* process TX channels */
1169     for (index = 0; tx; tx = tx >> 1, index++) {
1170         struct cppi_channel     *tx_ch;
1171         struct cppi_tx_stateram __iomem *tx_ram;
1172         bool                completed = false;
1173         struct cppi_descriptor      *bd;
1174 
1175         if (!(tx & 1))
1176             continue;
1177 
1178         tx_ch = cppi->tx + index;
1179         tx_ram = tx_ch->state_ram;
1180 
1181         /* FIXME  need a cppi_tx_scan() routine, which
1182          * can also be called from abort code
1183          */
1184 
1185         cppi_dump_tx(5, tx_ch, "/E");
1186 
1187         bd = tx_ch->head;
1188 
1189         /*
1190          * If Head is null then this could mean that a abort interrupt
1191          * that needs to be acknowledged.
1192          */
1193         if (NULL == bd) {
1194             musb_dbg(musb, "null BD");
1195             musb_writel(&tx_ram->tx_complete, 0, 0);
1196             continue;
1197         }
1198 
1199         /* run through all completed BDs */
1200         for (i = 0; !completed && bd && i < NUM_TXCHAN_BD;
1201                 i++, bd = bd->next) {
1202             u16 len;
1203 
1204             /* catch latest BD writes from CPPI */
1205             rmb();
1206             if (bd->hw_options & CPPI_OWN_SET)
1207                 break;
1208 
1209             musb_dbg(musb, "C/TXBD %p n %x b %x off %x opt %x",
1210                     bd, bd->hw_next, bd->hw_bufp,
1211                     bd->hw_off_len, bd->hw_options);
1212 
1213             len = bd->hw_off_len & CPPI_BUFFER_LEN_MASK;
1214             tx_ch->channel.actual_len += len;
1215 
1216             tx_ch->last_processed = bd;
1217 
1218             /* write completion register to acknowledge
1219              * processing of completed BDs, and possibly
1220              * release the IRQ; EOQ might not be set ...
1221              *
1222              * REVISIT use the same ack strategy as rx
1223              *
1224              * REVISIT have observed bit 18 set; huh??
1225              */
1226             /* if ((bd->hw_options & CPPI_EOQ_MASK)) */
1227                 musb_writel(&tx_ram->tx_complete, 0, bd->dma);
1228 
1229             /* stop scanning on end-of-segment */
1230             if (bd->hw_next == 0)
1231                 completed = true;
1232         }
1233 
1234         /* on end of segment, maybe go to next one */
1235         if (completed) {
1236             /* cppi_dump_tx(4, tx_ch, "/complete"); */
1237 
1238             /* transfer more, or report completion */
1239             if (tx_ch->offset >= tx_ch->buf_len) {
1240                 tx_ch->head = NULL;
1241                 tx_ch->tail = NULL;
1242                 tx_ch->channel.status = MUSB_DMA_STATUS_FREE;
1243 
1244                 hw_ep = tx_ch->hw_ep;
1245 
1246                 musb_dma_completion(musb, index + 1, 1);
1247 
1248             } else {
1249                 /* Bigger transfer than we could fit in
1250                  * that first batch of descriptors...
1251                  */
1252                 cppi_next_tx_segment(musb, tx_ch);
1253             }
1254         } else
1255             tx_ch->head = bd;
1256     }
1257 
1258     /* Start processing the RX block */
1259     for (index = 0; rx; rx = rx >> 1, index++) {
1260 
1261         if (rx & 1) {
1262             struct cppi_channel     *rx_ch;
1263 
1264             rx_ch = cppi->rx + index;
1265 
1266             /* let incomplete dma segments finish */
1267             if (!cppi_rx_scan(cppi, index))
1268                 continue;
1269 
1270             /* start another dma segment if needed */
1271             if (rx_ch->channel.actual_len != rx_ch->buf_len
1272                     && rx_ch->channel.actual_len
1273                         == rx_ch->offset) {
1274                 cppi_next_rx_segment(musb, rx_ch, 1);
1275                 continue;
1276             }
1277 
1278             /* all segments completed! */
1279             rx_ch->channel.status = MUSB_DMA_STATUS_FREE;
1280 
1281             hw_ep = rx_ch->hw_ep;
1282 
1283             core_rxirq_disable(tibase, index + 1);
1284             musb_dma_completion(musb, index + 1, 0);
1285         }
1286     }
1287 
1288     /* write to CPPI EOI register to re-enable interrupts */
1289     musb_writel(tibase, DAVINCI_CPPI_EOI_REG, 0);
1290 
1291     if (cppi->irq)
1292         spin_unlock_irqrestore(&musb->lock, flags);
1293 
1294     return IRQ_HANDLED;
1295 }
1296 EXPORT_SYMBOL_GPL(cppi_interrupt);
1297 
1298 /* Instantiate a software object representing a DMA controller. */
1299 struct dma_controller *
1300 cppi_dma_controller_create(struct musb *musb, void __iomem *mregs)
1301 {
1302     struct cppi     *controller;
1303     struct device       *dev = musb->controller;
1304     struct platform_device  *pdev = to_platform_device(dev);
1305     int         irq = platform_get_irq_byname(pdev, "dma");
1306 
1307     controller = kzalloc(sizeof *controller, GFP_KERNEL);
1308     if (!controller)
1309         return NULL;
1310 
1311     controller->mregs = mregs;
1312     controller->tibase = mregs - DAVINCI_BASE_OFFSET;
1313 
1314     controller->controller.musb = musb;
1315     controller->controller.channel_alloc = cppi_channel_allocate;
1316     controller->controller.channel_release = cppi_channel_release;
1317     controller->controller.channel_program = cppi_channel_program;
1318     controller->controller.channel_abort = cppi_channel_abort;
1319 
1320     /* NOTE: allocating from on-chip SRAM would give the least
1321      * contention for memory access, if that ever matters here.
1322      */
1323 
1324     /* setup BufferPool */
1325     controller->pool = dma_pool_create("cppi",
1326             controller->controller.musb->controller,
1327             sizeof(struct cppi_descriptor),
1328             CPPI_DESCRIPTOR_ALIGN, 0);
1329     if (!controller->pool) {
1330         kfree(controller);
1331         return NULL;
1332     }
1333 
1334     if (irq > 0) {
1335         if (request_irq(irq, cppi_interrupt, 0, "cppi-dma", musb)) {
1336             dev_err(dev, "request_irq %d failed!\n", irq);
1337             musb_dma_controller_destroy(&controller->controller);
1338             return NULL;
1339         }
1340         controller->irq = irq;
1341     }
1342 
1343     cppi_controller_start(controller);
1344     return &controller->controller;
1345 }
1346 EXPORT_SYMBOL_GPL(cppi_dma_controller_create);
1347 
1348 /*
1349  *  Destroy a previously-instantiated DMA controller.
1350  */
1351 void cppi_dma_controller_destroy(struct dma_controller *c)
1352 {
1353     struct cppi *cppi;
1354 
1355     cppi = container_of(c, struct cppi, controller);
1356 
1357     cppi_controller_stop(cppi);
1358 
1359     if (cppi->irq)
1360         free_irq(cppi->irq, cppi->controller.musb);
1361 
1362     /* assert:  caller stopped the controller first */
1363     dma_pool_destroy(cppi->pool);
1364 
1365     kfree(cppi);
1366 }
1367 EXPORT_SYMBOL_GPL(cppi_dma_controller_destroy);
1368 
1369 /*
1370  * Context: controller irqlocked, endpoint selected
1371  */
1372 static int cppi_channel_abort(struct dma_channel *channel)
1373 {
1374     struct cppi_channel *cppi_ch;
1375     struct cppi     *controller;
1376     void __iomem        *mbase;
1377     void __iomem        *tibase;
1378     void __iomem        *regs;
1379     u32         value;
1380     struct cppi_descriptor  *queue;
1381 
1382     cppi_ch = container_of(channel, struct cppi_channel, channel);
1383 
1384     controller = cppi_ch->controller;
1385 
1386     switch (channel->status) {
1387     case MUSB_DMA_STATUS_BUS_ABORT:
1388     case MUSB_DMA_STATUS_CORE_ABORT:
1389         /* from RX or TX fault irq handler */
1390     case MUSB_DMA_STATUS_BUSY:
1391         /* the hardware needs shutting down */
1392         regs = cppi_ch->hw_ep->regs;
1393         break;
1394     case MUSB_DMA_STATUS_UNKNOWN:
1395     case MUSB_DMA_STATUS_FREE:
1396         return 0;
1397     default:
1398         return -EINVAL;
1399     }
1400 
1401     if (!cppi_ch->transmit && cppi_ch->head)
1402         cppi_dump_rxq(3, "/abort", cppi_ch);
1403 
1404     mbase = controller->mregs;
1405     tibase = controller->tibase;
1406 
1407     queue = cppi_ch->head;
1408     cppi_ch->head = NULL;
1409     cppi_ch->tail = NULL;
1410 
1411     /* REVISIT should rely on caller having done this,
1412      * and caller should rely on us not changing it.
1413      * peripheral code is safe ... check host too.
1414      */
1415     musb_ep_select(mbase, cppi_ch->index + 1);
1416 
1417     if (cppi_ch->transmit) {
1418         struct cppi_tx_stateram __iomem *tx_ram;
1419         /* REVISIT put timeouts on these controller handshakes */
1420 
1421         cppi_dump_tx(6, cppi_ch, " (teardown)");
1422 
1423         /* teardown DMA engine then usb core */
1424         do {
1425             value = musb_readl(tibase, DAVINCI_TXCPPI_TEAR_REG);
1426         } while (!(value & CPPI_TEAR_READY));
1427         musb_writel(tibase, DAVINCI_TXCPPI_TEAR_REG, cppi_ch->index);
1428 
1429         tx_ram = cppi_ch->state_ram;
1430         do {
1431             value = musb_readl(&tx_ram->tx_complete, 0);
1432         } while (0xFFFFFFFC != value);
1433 
1434         /* FIXME clean up the transfer state ... here?
1435          * the completion routine should get called with
1436          * an appropriate status code.
1437          */
1438 
1439         value = musb_readw(regs, MUSB_TXCSR);
1440         value &= ~MUSB_TXCSR_DMAENAB;
1441         value |= MUSB_TXCSR_FLUSHFIFO;
1442         musb_writew(regs, MUSB_TXCSR, value);
1443         musb_writew(regs, MUSB_TXCSR, value);
1444 
1445         /*
1446          * 1. Write to completion Ptr value 0x1(bit 0 set)
1447          *    (write back mode)
1448          * 2. Wait for abort interrupt and then put the channel in
1449          *    compare mode by writing 1 to the tx_complete register.
1450          */
1451         cppi_reset_tx(tx_ram, 1);
1452         cppi_ch->head = NULL;
1453         musb_writel(&tx_ram->tx_complete, 0, 1);
1454         cppi_dump_tx(5, cppi_ch, " (done teardown)");
1455 
1456         /* REVISIT tx side _should_ clean up the same way
1457          * as the RX side ... this does no cleanup at all!
1458          */
1459 
1460     } else /* RX */ {
1461         u16         csr;
1462 
1463         /* NOTE: docs don't guarantee any of this works ...  we
1464          * expect that if the usb core stops telling the cppi core
1465          * to pull more data from it, then it'll be safe to flush
1466          * current RX DMA state iff any pending fifo transfer is done.
1467          */
1468 
1469         core_rxirq_disable(tibase, cppi_ch->index + 1);
1470 
1471         /* for host, ensure ReqPkt is never set again */
1472         if (is_host_active(cppi_ch->controller->controller.musb)) {
1473             value = musb_readl(tibase, DAVINCI_AUTOREQ_REG);
1474             value &= ~((0x3) << (cppi_ch->index * 2));
1475             musb_writel(tibase, DAVINCI_AUTOREQ_REG, value);
1476         }
1477 
1478         csr = musb_readw(regs, MUSB_RXCSR);
1479 
1480         /* for host, clear (just) ReqPkt at end of current packet(s) */
1481         if (is_host_active(cppi_ch->controller->controller.musb)) {
1482             csr |= MUSB_RXCSR_H_WZC_BITS;
1483             csr &= ~MUSB_RXCSR_H_REQPKT;
1484         } else
1485             csr |= MUSB_RXCSR_P_WZC_BITS;
1486 
1487         /* clear dma enable */
1488         csr &= ~(MUSB_RXCSR_DMAENAB);
1489         musb_writew(regs, MUSB_RXCSR, csr);
1490         csr = musb_readw(regs, MUSB_RXCSR);
1491 
1492         /* Quiesce: wait for current dma to finish (if not cleanup).
1493          * We can't use bit zero of stateram->rx_sop, since that
1494          * refers to an entire "DMA packet" not just emptying the
1495          * current fifo.  Most segments need multiple usb packets.
1496          */
1497         if (channel->status == MUSB_DMA_STATUS_BUSY)
1498             udelay(50);
1499 
1500         /* scan the current list, reporting any data that was
1501          * transferred and acking any IRQ
1502          */
1503         cppi_rx_scan(controller, cppi_ch->index);
1504 
1505         /* clobber the existing state once it's idle
1506          *
1507          * NOTE:  arguably, we should also wait for all the other
1508          * RX channels to quiesce (how??) and then temporarily
1509          * disable RXCPPI_CTRL_REG ... but it seems that we can
1510          * rely on the controller restarting from state ram, with
1511          * only RXCPPI_BUFCNT state being bogus.  BUFCNT will
1512          * correct itself after the next DMA transfer though.
1513          *
1514          * REVISIT does using rndis mode change that?
1515          */
1516         cppi_reset_rx(cppi_ch->state_ram);
1517 
1518         /* next DMA request _should_ load cppi head ptr */
1519 
1520         /* ... we don't "free" that list, only mutate it in place.  */
1521         cppi_dump_rx(5, cppi_ch, " (done abort)");
1522 
1523         /* clean up previously pending bds */
1524         cppi_bd_free(cppi_ch, cppi_ch->last_processed);
1525         cppi_ch->last_processed = NULL;
1526 
1527         while (queue) {
1528             struct cppi_descriptor  *tmp = queue->next;
1529 
1530             cppi_bd_free(cppi_ch, queue);
1531             queue = tmp;
1532         }
1533     }
1534 
1535     channel->status = MUSB_DMA_STATUS_FREE;
1536     cppi_ch->buf_dma = 0;
1537     cppi_ch->offset = 0;
1538     cppi_ch->buf_len = 0;
1539     cppi_ch->maxpacket = 0;
1540     return 0;
1541 }
1542 
1543 /* TBD Queries:
1544  *
1545  * Power Management ... probably turn off cppi during suspend, restart;
1546  * check state ram?  Clocking is presumably shared with usb core.
1547  */