Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is subject to the terms and conditions of the GNU General Public
0003  * License.  See the file "COPYING" in the main directory of this archive
0004  * for more details.
0005  *
0006  * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
0007  * Copyright (C) 1999 Andrew R. Baker (andrewb@uab.edu)
0008  * Copyright (C) 2001 Florian Lohoff (flo@rfc822.org)
0009  * Copyright (C) 2003, 07 Ralf Baechle (ralf@linux-mips.org)
0010  * 
0011  * (In all truth, Jed Schimmel wrote all this code.)
0012  */
0013 
0014 #undef DEBUG
0015 
0016 #include <linux/delay.h>
0017 #include <linux/dma-mapping.h>
0018 #include <linux/gfp.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/init.h>
0021 #include <linux/kernel.h>
0022 #include <linux/types.h>
0023 #include <linux/module.h>
0024 #include <linux/platform_device.h>
0025 #include <linux/spinlock.h>
0026 
0027 #include <asm/sgi/hpc3.h>
0028 #include <asm/sgi/ip22.h>
0029 #include <asm/sgi/wd.h>
0030 
0031 #include <scsi/scsi.h>
0032 #include <scsi/scsi_cmnd.h>
0033 #include <scsi/scsi_device.h>
0034 #include <scsi/scsi_eh.h>
0035 #include <scsi/scsi_tcq.h>
0036 #include "wd33c93.h"
0037 
0038 struct ip22_hostdata {
0039     struct WD33C93_hostdata wh;
0040     dma_addr_t dma;
0041     void *cpu;
0042     struct device *dev;
0043 };
0044 
0045 #define host_to_hostdata(host) ((struct ip22_hostdata *)((host)->hostdata))
0046 
0047 struct hpc_chunk {
0048     struct hpc_dma_desc desc;
0049     u32 _padding;   /* align to quadword boundary */
0050 };
0051 
0052 /* space for hpc dma descriptors */
0053 #define HPC_DMA_SIZE   PAGE_SIZE
0054 
0055 #define DMA_DIR(d)   ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
0056 
0057 static irqreturn_t sgiwd93_intr(int irq, void *dev_id)
0058 {
0059     struct Scsi_Host * host = dev_id;
0060     unsigned long flags;
0061 
0062     spin_lock_irqsave(host->host_lock, flags);
0063     wd33c93_intr(host);
0064     spin_unlock_irqrestore(host->host_lock, flags);
0065 
0066     return IRQ_HANDLED;
0067 }
0068 
0069 static inline
0070 void fill_hpc_entries(struct ip22_hostdata *hd, struct scsi_cmnd *cmd, int din)
0071 {
0072     struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
0073     unsigned long len = scsi_pointer->this_residual;
0074     void *addr = scsi_pointer->ptr;
0075     dma_addr_t physaddr;
0076     unsigned long count;
0077     struct hpc_chunk *hcp;
0078 
0079     physaddr = dma_map_single(hd->dev, addr, len, DMA_DIR(din));
0080     scsi_pointer->dma_handle = physaddr;
0081     hcp = hd->cpu;
0082 
0083     while (len) {
0084         /*
0085          * even cntinfo could be up to 16383, without
0086          * magic only 8192 works correctly
0087          */
0088         count = len > 8192 ? 8192 : len;
0089         hcp->desc.pbuf = physaddr;
0090         hcp->desc.cntinfo = count;
0091         hcp++;
0092         len -= count;
0093         physaddr += count;
0094     }
0095 
0096     /*
0097      * To make sure, if we trip an HPC bug, that we transfer every single
0098      * byte, we tag on an extra zero length dma descriptor at the end of
0099      * the chain.
0100      */
0101     hcp->desc.pbuf = 0;
0102     hcp->desc.cntinfo = HPCDMA_EOX;
0103     dma_sync_single_for_device(hd->dev, hd->dma,
0104                (unsigned long)(hcp + 1) - (unsigned long)hd->cpu,
0105                DMA_TO_DEVICE);
0106 }
0107 
0108 static int dma_setup(struct scsi_cmnd *cmd, int datainp)
0109 {
0110     struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
0111     struct ip22_hostdata *hdata = host_to_hostdata(cmd->device->host);
0112     struct hpc3_scsiregs *hregs =
0113         (struct hpc3_scsiregs *) cmd->device->host->base;
0114 
0115     pr_debug("dma_setup: datainp<%d> hcp<%p> ", datainp, hdata->cpu);
0116 
0117     hdata->wh.dma_dir = datainp;
0118 
0119     /*
0120      * wd33c93 shouldn't pass us bogus dma_setups, but it does:-(  The
0121      * other wd33c93 drivers deal with it the same way (which isn't that
0122      * obvious).  IMHO a better fix would be, not to do these dma setups
0123      * in the first place.
0124      */
0125     if (scsi_pointer->ptr == NULL || scsi_pointer->this_residual == 0)
0126         return 1;
0127 
0128     fill_hpc_entries(hdata, cmd, datainp);
0129 
0130     pr_debug(" HPCGO\n");
0131 
0132     /* Start up the HPC. */
0133     hregs->ndptr = hdata->dma;
0134     if (datainp)
0135         hregs->ctrl = HPC3_SCTRL_ACTIVE;
0136     else
0137         hregs->ctrl = HPC3_SCTRL_ACTIVE | HPC3_SCTRL_DIR;
0138 
0139     return 0;
0140 }
0141 
0142 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
0143              int status)
0144 {
0145     struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
0146     struct ip22_hostdata *hdata = host_to_hostdata(instance);
0147     struct hpc3_scsiregs *hregs;
0148 
0149     if (!SCpnt)
0150         return;
0151 
0152     if (scsi_pointer->ptr == NULL || scsi_pointer->this_residual == 0)
0153         return;
0154 
0155     hregs = (struct hpc3_scsiregs *) SCpnt->device->host->base;
0156 
0157     pr_debug("dma_stop: status<%d> ", status);
0158 
0159     /* First stop the HPC and flush it's FIFO. */
0160     if (hdata->wh.dma_dir) {
0161         hregs->ctrl |= HPC3_SCTRL_FLUSH;
0162         while (hregs->ctrl & HPC3_SCTRL_ACTIVE)
0163             barrier();
0164     }
0165     hregs->ctrl = 0;
0166     dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
0167              scsi_pointer->this_residual,
0168              DMA_DIR(hdata->wh.dma_dir));
0169 
0170     pr_debug("\n");
0171 }
0172 
0173 void sgiwd93_reset(unsigned long base)
0174 {
0175     struct hpc3_scsiregs *hregs = (struct hpc3_scsiregs *) base;
0176 
0177     hregs->ctrl = HPC3_SCTRL_CRESET;
0178     udelay(50);
0179     hregs->ctrl = 0;
0180 }
0181 EXPORT_SYMBOL_GPL(sgiwd93_reset);
0182 
0183 static inline void init_hpc_chain(struct ip22_hostdata *hdata)
0184 {
0185     struct hpc_chunk *hcp = (struct hpc_chunk *)hdata->cpu;
0186     dma_addr_t dma = hdata->dma;
0187     unsigned long start, end;
0188 
0189     start = (unsigned long) hcp;
0190     end = start + HPC_DMA_SIZE;
0191     while (start < end) {
0192         hcp->desc.pnext = (u32) (dma + sizeof(struct hpc_chunk));
0193         hcp->desc.cntinfo = HPCDMA_EOX;
0194         hcp++;
0195         dma += sizeof(struct hpc_chunk);
0196         start += sizeof(struct hpc_chunk);
0197     }
0198     hcp--;
0199     hcp->desc.pnext = hdata->dma;
0200 }
0201 
0202 /*
0203  * Kludge alert - the SCSI code calls the abort and reset method with int
0204  * arguments not with pointers.  So this is going to blow up beautyfully
0205  * on 64-bit systems with memory outside the compat address spaces.
0206  */
0207 static struct scsi_host_template sgiwd93_template = {
0208     .module         = THIS_MODULE,
0209     .proc_name      = "SGIWD93",
0210     .name           = "SGI WD93",
0211     .queuecommand       = wd33c93_queuecommand,
0212     .eh_abort_handler   = wd33c93_abort,
0213     .eh_host_reset_handler  = wd33c93_host_reset,
0214     .can_queue      = 16,
0215     .this_id        = 7,
0216     .sg_tablesize       = SG_ALL,
0217     .cmd_per_lun        = 8,
0218     .dma_boundary       = PAGE_SIZE - 1,
0219     .cmd_size       = sizeof(struct scsi_pointer),
0220 };
0221 
0222 static int sgiwd93_probe(struct platform_device *pdev)
0223 {
0224     struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
0225     unsigned char *wdregs = pd->wdregs;
0226     struct hpc3_scsiregs *hregs = pd->hregs;
0227     struct ip22_hostdata *hdata;
0228     struct Scsi_Host *host;
0229     wd33c93_regs regs;
0230     unsigned int unit = pd->unit;
0231     unsigned int irq = pd->irq;
0232     int err;
0233 
0234     host = scsi_host_alloc(&sgiwd93_template, sizeof(struct ip22_hostdata));
0235     if (!host) {
0236         err = -ENOMEM;
0237         goto out;
0238     }
0239 
0240     host->base = (unsigned long) hregs;
0241     host->irq = irq;
0242 
0243     hdata = host_to_hostdata(host);
0244     hdata->dev = &pdev->dev;
0245     hdata->cpu = dma_alloc_noncoherent(&pdev->dev, HPC_DMA_SIZE,
0246                 &hdata->dma, DMA_TO_DEVICE, GFP_KERNEL);
0247     if (!hdata->cpu) {
0248         printk(KERN_WARNING "sgiwd93: Could not allocate memory for "
0249                "host %d buffer.\n", unit);
0250         err = -ENOMEM;
0251         goto out_put;
0252     }
0253 
0254     init_hpc_chain(hdata);
0255 
0256     regs.SASR = wdregs + 3;
0257     regs.SCMD = wdregs + 7;
0258 
0259     hdata->wh.no_sync = 0;
0260     hdata->wh.fast = 1;
0261     hdata->wh.dma_mode = CTRL_BURST;
0262 
0263     wd33c93_init(host, regs, dma_setup, dma_stop, WD33C93_FS_MHZ(20));
0264 
0265     err = request_irq(irq, sgiwd93_intr, 0, "SGI WD93", host);
0266     if (err) {
0267         printk(KERN_WARNING "sgiwd93: Could not register irq %d "
0268                "for host %d.\n", irq, unit);
0269         goto out_free;
0270     }
0271 
0272     platform_set_drvdata(pdev, host);
0273 
0274     err = scsi_add_host(host, NULL);
0275     if (err)
0276         goto out_irq;
0277 
0278     scsi_scan_host(host);
0279 
0280     return 0;
0281 
0282 out_irq:
0283     free_irq(irq, host);
0284 out_free:
0285     dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma,
0286             DMA_TO_DEVICE);
0287 out_put:
0288     scsi_host_put(host);
0289 out:
0290 
0291     return err;
0292 }
0293 
0294 static int sgiwd93_remove(struct platform_device *pdev)
0295 {
0296     struct Scsi_Host *host = platform_get_drvdata(pdev);
0297     struct ip22_hostdata *hdata = (struct ip22_hostdata *) host->hostdata;
0298     struct sgiwd93_platform_data *pd = pdev->dev.platform_data;
0299 
0300     scsi_remove_host(host);
0301     free_irq(pd->irq, host);
0302     dma_free_noncoherent(&pdev->dev, HPC_DMA_SIZE, hdata->cpu, hdata->dma,
0303             DMA_TO_DEVICE);
0304     scsi_host_put(host);
0305     return 0;
0306 }
0307 
0308 static struct platform_driver sgiwd93_driver = {
0309     .probe  = sgiwd93_probe,
0310     .remove = sgiwd93_remove,
0311     .driver = {
0312         .name   = "sgiwd93",
0313     }
0314 };
0315 
0316 static int __init sgiwd93_module_init(void)
0317 {
0318     return platform_driver_register(&sgiwd93_driver);
0319 }
0320 
0321 static void __exit sgiwd93_module_exit(void)
0322 {
0323     return platform_driver_unregister(&sgiwd93_driver);
0324 }
0325 
0326 module_init(sgiwd93_module_init);
0327 module_exit(sgiwd93_module_exit);
0328 
0329 MODULE_DESCRIPTION("SGI WD33C93 driver");
0330 MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
0331 MODULE_LICENSE("GPL");
0332 MODULE_ALIAS("platform:sgiwd93");