Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  ACPI PATA driver
0004  *
0005  *  (c) 2007 Red Hat
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/pci.h>
0011 #include <linux/blkdev.h>
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/gfp.h>
0015 #include <linux/acpi.h>
0016 #include <linux/libata.h>
0017 #include <linux/ata.h>
0018 #include <scsi/scsi_host.h>
0019 
0020 #define DRV_NAME    "pata_acpi"
0021 #define DRV_VERSION "0.2.3"
0022 
0023 struct pata_acpi {
0024     struct ata_acpi_gtm gtm;
0025     void *last;
0026     unsigned long mask[2];
0027 };
0028 
0029 /**
0030  *  pacpi_pre_reset -   check for 40/80 pin
0031  *  @link: ATA link
0032  *  @deadline: deadline jiffies for the operation
0033  *
0034  *  Perform the PATA port setup we need.
0035  */
0036 
0037 static int pacpi_pre_reset(struct ata_link *link, unsigned long deadline)
0038 {
0039     struct ata_port *ap = link->ap;
0040     struct pata_acpi *acpi = ap->private_data;
0041     if (ACPI_HANDLE(&ap->tdev) == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0)
0042         return -ENODEV;
0043 
0044     return ata_sff_prereset(link, deadline);
0045 }
0046 
0047 /**
0048  *  pacpi_cable_detect  -   cable type detection
0049  *  @ap: port to detect
0050  *
0051  *  Perform device specific cable detection
0052  */
0053 
0054 static int pacpi_cable_detect(struct ata_port *ap)
0055 {
0056     struct pata_acpi *acpi = ap->private_data;
0057 
0058     if ((acpi->mask[0] | acpi->mask[1]) & (0xF8 << ATA_SHIFT_UDMA))
0059         return ATA_CBL_PATA80;
0060     else
0061         return ATA_CBL_PATA40;
0062 }
0063 
0064 /**
0065  *  pacpi_discover_modes    -   filter non ACPI modes
0066  *  @ap: ATA port
0067  *  @adev: ATA device
0068  *
0069  *  Try the modes available and see which ones the ACPI method will
0070  *  set up sensibly. From this we get a mask of ACPI modes we can use
0071  */
0072 
0073 static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev)
0074 {
0075     struct pata_acpi *acpi = ap->private_data;
0076     struct ata_acpi_gtm probe;
0077     unsigned int xfer_mask;
0078 
0079     probe = acpi->gtm;
0080 
0081     ata_acpi_gtm(ap, &probe);
0082 
0083     xfer_mask = ata_acpi_gtm_xfermask(adev, &probe);
0084 
0085     if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
0086         ap->cbl = ATA_CBL_PATA80;
0087 
0088     return xfer_mask;
0089 }
0090 
0091 /**
0092  *  pacpi_mode_filter   -   mode filter for ACPI
0093  *  @adev: device
0094  *  @mask: mask of valid modes
0095  *
0096  *  Filter the valid mode list according to our own specific rules, in
0097  *  this case the list of discovered valid modes obtained by ACPI probing
0098  */
0099 
0100 static unsigned int pacpi_mode_filter(struct ata_device *adev, unsigned int mask)
0101 {
0102     struct pata_acpi *acpi = adev->link->ap->private_data;
0103     return mask & acpi->mask[adev->devno];
0104 }
0105 
0106 /**
0107  *  pacpi_set_piomode   -   set initial PIO mode data
0108  *  @ap: ATA interface
0109  *  @adev: ATA device
0110  */
0111 
0112 static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev)
0113 {
0114     int unit = adev->devno;
0115     struct pata_acpi *acpi = ap->private_data;
0116     const struct ata_timing *t;
0117 
0118     if (!(acpi->gtm.flags & 0x10))
0119         unit = 0;
0120 
0121     /* Now stuff the nS values into the structure */
0122     t = ata_timing_find_mode(adev->pio_mode);
0123     acpi->gtm.drive[unit].pio = t->cycle;
0124     ata_acpi_stm(ap, &acpi->gtm);
0125     /* See what mode we actually got */
0126     ata_acpi_gtm(ap, &acpi->gtm);
0127 }
0128 
0129 /**
0130  *  pacpi_set_dmamode   -   set initial DMA mode data
0131  *  @ap: ATA interface
0132  *  @adev: ATA device
0133  */
0134 
0135 static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev)
0136 {
0137     int unit = adev->devno;
0138     struct pata_acpi *acpi = ap->private_data;
0139     const struct ata_timing *t;
0140 
0141     if (!(acpi->gtm.flags & 0x10))
0142         unit = 0;
0143 
0144     /* Now stuff the nS values into the structure */
0145     t = ata_timing_find_mode(adev->dma_mode);
0146     if (adev->dma_mode >= XFER_UDMA_0) {
0147         acpi->gtm.drive[unit].dma = t->udma;
0148         acpi->gtm.flags |= (1 << (2 * unit));
0149     } else {
0150         acpi->gtm.drive[unit].dma = t->cycle;
0151         acpi->gtm.flags &= ~(1 << (2 * unit));
0152     }
0153     ata_acpi_stm(ap, &acpi->gtm);
0154     /* See what mode we actually got */
0155     ata_acpi_gtm(ap, &acpi->gtm);
0156 }
0157 
0158 /**
0159  *  pacpi_qc_issue  -   command issue
0160  *  @qc: command pending
0161  *
0162  *  Called when the libata layer is about to issue a command. We wrap
0163  *  this interface so that we can load the correct ATA timings if
0164  *  necessary.
0165  */
0166 
0167 static unsigned int pacpi_qc_issue(struct ata_queued_cmd *qc)
0168 {
0169     struct ata_port *ap = qc->ap;
0170     struct ata_device *adev = qc->dev;
0171     struct pata_acpi *acpi = ap->private_data;
0172 
0173     if (acpi->gtm.flags & 0x10)
0174         return ata_bmdma_qc_issue(qc);
0175 
0176     if (adev != acpi->last) {
0177         pacpi_set_piomode(ap, adev);
0178         if (ata_dma_enabled(adev))
0179             pacpi_set_dmamode(ap, adev);
0180         acpi->last = adev;
0181     }
0182     return ata_bmdma_qc_issue(qc);
0183 }
0184 
0185 /**
0186  *  pacpi_port_start    -   port setup
0187  *  @ap: ATA port being set up
0188  *
0189  *  Use the port_start hook to maintain private control structures
0190  */
0191 
0192 static int pacpi_port_start(struct ata_port *ap)
0193 {
0194     struct pci_dev *pdev = to_pci_dev(ap->host->dev);
0195     struct pata_acpi *acpi;
0196 
0197     if (ACPI_HANDLE(&ap->tdev) == NULL)
0198         return -ENODEV;
0199 
0200     acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL);
0201     if (ap->private_data == NULL)
0202         return -ENOMEM;
0203     acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]);
0204     acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]);
0205     return ata_bmdma_port_start(ap);
0206 }
0207 
0208 static struct scsi_host_template pacpi_sht = {
0209     ATA_BMDMA_SHT(DRV_NAME),
0210 };
0211 
0212 static struct ata_port_operations pacpi_ops = {
0213     .inherits       = &ata_bmdma_port_ops,
0214     .qc_issue       = pacpi_qc_issue,
0215     .cable_detect       = pacpi_cable_detect,
0216     .mode_filter        = pacpi_mode_filter,
0217     .set_piomode        = pacpi_set_piomode,
0218     .set_dmamode        = pacpi_set_dmamode,
0219     .prereset       = pacpi_pre_reset,
0220     .port_start     = pacpi_port_start,
0221 };
0222 
0223 
0224 /**
0225  *  pacpi_init_one - Register ACPI ATA PCI device with kernel services
0226  *  @pdev: PCI device to register
0227  *  @id: PCI device ID
0228  *
0229  *  Called from kernel PCI layer.
0230  *
0231  *  LOCKING:
0232  *  Inherited from PCI layer (may sleep).
0233  *
0234  *  RETURNS:
0235  *  Zero on success, or -ERRNO value.
0236  */
0237 
0238 static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
0239 {
0240     static const struct ata_port_info info = {
0241         .flags      = ATA_FLAG_SLAVE_POSS,
0242 
0243         .pio_mask   = ATA_PIO4,
0244         .mwdma_mask = ATA_MWDMA2,
0245         .udma_mask  = ATA_UDMA6,
0246 
0247         .port_ops   = &pacpi_ops,
0248     };
0249     const struct ata_port_info *ppi[] = { &info, NULL };
0250     if (pdev->vendor == PCI_VENDOR_ID_ATI) {
0251         int rc = pcim_enable_device(pdev);
0252         if (rc < 0)
0253             return rc;
0254         pcim_pin_device(pdev);
0255     }
0256     return ata_pci_bmdma_init_one(pdev, ppi, &pacpi_sht, NULL, 0);
0257 }
0258 
0259 static const struct pci_device_id pacpi_pci_tbl[] = {
0260     { PCI_ANY_ID,       PCI_ANY_ID,            PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1},
0261     { } /* terminate list */
0262 };
0263 
0264 static struct pci_driver pacpi_pci_driver = {
0265     .name           = DRV_NAME,
0266     .id_table       = pacpi_pci_tbl,
0267     .probe          = pacpi_init_one,
0268     .remove         = ata_pci_remove_one,
0269 #ifdef CONFIG_PM_SLEEP
0270     .suspend        = ata_pci_device_suspend,
0271     .resume         = ata_pci_device_resume,
0272 #endif
0273 };
0274 
0275 module_pci_driver(pacpi_pci_driver);
0276 
0277 MODULE_AUTHOR("Alan Cox");
0278 MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode");
0279 MODULE_LICENSE("GPL");
0280 MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl);
0281 MODULE_VERSION(DRV_VERSION);