pata_acpi.c revision 7c77fa4d51b1480bcec2e898c94d6912fe063c16
1/* 2 * ACPI PATA driver 3 * 4 * (c) 2007 Red Hat <alan@redhat.com> 5 */ 6 7#include <linux/kernel.h> 8#include <linux/module.h> 9#include <linux/pci.h> 10#include <linux/init.h> 11#include <linux/blkdev.h> 12#include <linux/delay.h> 13#include <linux/device.h> 14#include <scsi/scsi_host.h> 15#include <acpi/acpi_bus.h> 16#include <acpi/acnames.h> 17#include <acpi/acnamesp.h> 18#include <acpi/acparser.h> 19#include <acpi/acexcep.h> 20#include <acpi/acmacros.h> 21#include <acpi/actypes.h> 22 23#include <linux/libata.h> 24#include <linux/ata.h> 25 26#define DRV_NAME "pata_acpi" 27#define DRV_VERSION "0.2.3" 28 29struct pata_acpi { 30 struct ata_acpi_gtm gtm; 31 void *last; 32 unsigned long mask[2]; 33}; 34 35/** 36 * pacpi_pre_reset - check for 40/80 pin 37 * @ap: Port 38 * @deadline: deadline jiffies for the operation 39 * 40 * Perform the PATA port setup we need. 41 */ 42 43static int pacpi_pre_reset(struct ata_link *link, unsigned long deadline) 44{ 45 struct ata_port *ap = link->ap; 46 struct pata_acpi *acpi = ap->private_data; 47 if (ap->acpi_handle == NULL || ata_acpi_gtm(ap, &acpi->gtm) < 0) 48 return -ENODEV; 49 50 return ata_std_prereset(link, deadline); 51} 52 53/** 54 * pacpi_cable_detect - cable type detection 55 * @ap: port to detect 56 * 57 * Perform device specific cable detection 58 */ 59 60static int pacpi_cable_detect(struct ata_port *ap) 61{ 62 struct pata_acpi *acpi = ap->private_data; 63 64 if ((acpi->mask[0] | acpi->mask[1]) & (0xF8 << ATA_SHIFT_UDMA)) 65 return ATA_CBL_PATA80; 66 else 67 return ATA_CBL_PATA40; 68} 69 70/** 71 * pacpi_error_handler - Setup and error handler 72 * @ap: Port to handle 73 * 74 * LOCKING: 75 * None (inherited from caller). 76 */ 77 78static void pacpi_error_handler(struct ata_port *ap) 79{ 80 return ata_bmdma_drive_eh(ap, pacpi_pre_reset, ata_std_softreset, 81 NULL, ata_std_postreset); 82} 83 84/** 85 * pacpi_discover_modes - filter non ACPI modes 86 * @adev: ATA device 87 * @mask: proposed modes 88 * 89 * Try the modes available and see which ones the ACPI method will 90 * set up sensibly. From this we get a mask of ACPI modes we can use 91 */ 92 93static unsigned long pacpi_discover_modes(struct ata_port *ap, struct ata_device *adev) 94{ 95 struct pata_acpi *acpi = ap->private_data; 96 struct ata_acpi_gtm probe; 97 unsigned int xfer_mask; 98 99 probe = acpi->gtm; 100 101 ata_acpi_gtm(ap, &probe); 102 103 xfer_mask = ata_acpi_gtm_xfermask(adev, &probe); 104 105 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA)) 106 ap->cbl = ATA_CBL_PATA80; 107 108 return xfer_mask; 109} 110 111/** 112 * pacpi_mode_filter - mode filter for ACPI 113 * @adev: device 114 * @mask: mask of valid modes 115 * 116 * Filter the valid mode list according to our own specific rules, in 117 * this case the list of discovered valid modes obtained by ACPI probing 118 */ 119 120static unsigned long pacpi_mode_filter(struct ata_device *adev, unsigned long mask) 121{ 122 struct pata_acpi *acpi = adev->link->ap->private_data; 123 return ata_pci_default_filter(adev, mask & acpi->mask[adev->devno]); 124} 125 126/** 127 * pacpi_set_piomode - set initial PIO mode data 128 * @ap: ATA interface 129 * @adev: ATA device 130 */ 131 132static void pacpi_set_piomode(struct ata_port *ap, struct ata_device *adev) 133{ 134 int unit = adev->devno; 135 struct pata_acpi *acpi = ap->private_data; 136 137 if (!(acpi->gtm.flags & 0x10)) 138 unit = 0; 139 140 /* Now stuff the nS values into the structure */ 141 acpi->gtm.drive[unit].pio = 142 ata_acpi_pio_cycle[adev->pio_mode - XFER_PIO_0]; 143 ata_acpi_stm(ap, &acpi->gtm); 144 /* See what mode we actually got */ 145 ata_acpi_gtm(ap, &acpi->gtm); 146} 147 148/** 149 * pacpi_set_dmamode - set initial DMA mode data 150 * @ap: ATA interface 151 * @adev: ATA device 152 */ 153 154static void pacpi_set_dmamode(struct ata_port *ap, struct ata_device *adev) 155{ 156 int unit = adev->devno; 157 struct pata_acpi *acpi = ap->private_data; 158 159 if (!(acpi->gtm.flags & 0x10)) 160 unit = 0; 161 162 /* Now stuff the nS values into the structure */ 163 if (adev->dma_mode >= XFER_UDMA_0) { 164 acpi->gtm.drive[unit].dma = 165 ata_acpi_udma_cycle[adev->dma_mode - XFER_UDMA_0]; 166 acpi->gtm.flags |= (1 << (2 * unit)); 167 } else { 168 acpi->gtm.drive[unit].dma = 169 ata_acpi_mwdma_cycle[adev->dma_mode - XFER_MW_DMA_0]; 170 acpi->gtm.flags &= ~(1 << (2 * unit)); 171 } 172 ata_acpi_stm(ap, &acpi->gtm); 173 /* See what mode we actually got */ 174 ata_acpi_gtm(ap, &acpi->gtm); 175} 176 177/** 178 * pacpi_qc_issue_prot - command issue 179 * @qc: command pending 180 * 181 * Called when the libata layer is about to issue a command. We wrap 182 * this interface so that we can load the correct ATA timings if 183 * neccessary. 184 */ 185 186static unsigned int pacpi_qc_issue_prot(struct ata_queued_cmd *qc) 187{ 188 struct ata_port *ap = qc->ap; 189 struct ata_device *adev = qc->dev; 190 struct pata_acpi *acpi = ap->private_data; 191 192 if (acpi->gtm.flags & 0x10) 193 return ata_qc_issue_prot(qc); 194 195 if (adev != acpi->last) { 196 pacpi_set_piomode(ap, adev); 197 if (adev->dma_mode) 198 pacpi_set_dmamode(ap, adev); 199 acpi->last = adev; 200 } 201 return ata_qc_issue_prot(qc); 202} 203 204/** 205 * pacpi_port_start - port setup 206 * @ap: ATA port being set up 207 * 208 * Use the port_start hook to maintain private control structures 209 */ 210 211static int pacpi_port_start(struct ata_port *ap) 212{ 213 struct pci_dev *pdev = to_pci_dev(ap->host->dev); 214 struct pata_acpi *acpi; 215 216 int ret; 217 218 if (ap->acpi_handle == NULL) 219 return -ENODEV; 220 221 acpi = ap->private_data = devm_kzalloc(&pdev->dev, sizeof(struct pata_acpi), GFP_KERNEL); 222 if (ap->private_data == NULL) 223 return -ENOMEM; 224 acpi->mask[0] = pacpi_discover_modes(ap, &ap->link.device[0]); 225 acpi->mask[1] = pacpi_discover_modes(ap, &ap->link.device[1]); 226 ret = ata_sff_port_start(ap); 227 if (ret < 0) 228 return ret; 229 230 return ret; 231} 232 233static struct scsi_host_template pacpi_sht = { 234 .module = THIS_MODULE, 235 .name = DRV_NAME, 236 .ioctl = ata_scsi_ioctl, 237 .queuecommand = ata_scsi_queuecmd, 238 .can_queue = ATA_DEF_QUEUE, 239 .this_id = ATA_SHT_THIS_ID, 240 .sg_tablesize = LIBATA_MAX_PRD, 241 .cmd_per_lun = ATA_SHT_CMD_PER_LUN, 242 .emulated = ATA_SHT_EMULATED, 243 .use_clustering = ATA_SHT_USE_CLUSTERING, 244 .proc_name = DRV_NAME, 245 .dma_boundary = ATA_DMA_BOUNDARY, 246 .slave_configure = ata_scsi_slave_config, 247 .slave_destroy = ata_scsi_slave_destroy, 248 /* Use standard CHS mapping rules */ 249 .bios_param = ata_std_bios_param, 250}; 251 252static const struct ata_port_operations pacpi_ops = { 253 .set_piomode = pacpi_set_piomode, 254 .set_dmamode = pacpi_set_dmamode, 255 .mode_filter = pacpi_mode_filter, 256 257 /* Task file is PCI ATA format, use helpers */ 258 .tf_load = ata_tf_load, 259 .tf_read = ata_tf_read, 260 .check_status = ata_check_status, 261 .exec_command = ata_exec_command, 262 .dev_select = ata_std_dev_select, 263 264 .freeze = ata_bmdma_freeze, 265 .thaw = ata_bmdma_thaw, 266 .error_handler = pacpi_error_handler, 267 .post_internal_cmd = ata_bmdma_post_internal_cmd, 268 .cable_detect = pacpi_cable_detect, 269 270 /* BMDMA handling is PCI ATA format, use helpers */ 271 .bmdma_setup = ata_bmdma_setup, 272 .bmdma_start = ata_bmdma_start, 273 .bmdma_stop = ata_bmdma_stop, 274 .bmdma_status = ata_bmdma_status, 275 .qc_prep = ata_qc_prep, 276 .qc_issue = pacpi_qc_issue_prot, 277 .data_xfer = ata_data_xfer, 278 279 /* Timeout handling */ 280 .irq_handler = ata_interrupt, 281 .irq_clear = ata_bmdma_irq_clear, 282 .irq_on = ata_irq_on, 283 284 /* Generic PATA PCI ATA helpers */ 285 .port_start = pacpi_port_start, 286}; 287 288 289/** 290 * pacpi_init_one - Register ACPI ATA PCI device with kernel services 291 * @pdev: PCI device to register 292 * @ent: Entry in pacpi_pci_tbl matching with @pdev 293 * 294 * Called from kernel PCI layer. 295 * 296 * LOCKING: 297 * Inherited from PCI layer (may sleep). 298 * 299 * RETURNS: 300 * Zero on success, or -ERRNO value. 301 */ 302 303static int pacpi_init_one (struct pci_dev *pdev, const struct pci_device_id *id) 304{ 305 static const struct ata_port_info info = { 306 .sht = &pacpi_sht, 307 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST, 308 309 .pio_mask = 0x1f, 310 .mwdma_mask = 0x07, 311 .udma_mask = 0x7f, 312 313 .port_ops = &pacpi_ops, 314 }; 315 const struct ata_port_info *ppi[] = { &info, NULL }; 316 return ata_pci_init_one(pdev, ppi); 317} 318 319static const struct pci_device_id pacpi_pci_tbl[] = { 320 { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL, 1}, 321 { } /* terminate list */ 322}; 323 324static struct pci_driver pacpi_pci_driver = { 325 .name = DRV_NAME, 326 .id_table = pacpi_pci_tbl, 327 .probe = pacpi_init_one, 328 .remove = ata_pci_remove_one, 329#ifdef CONFIG_PM 330 .suspend = ata_pci_device_suspend, 331 .resume = ata_pci_device_resume, 332#endif 333}; 334 335static int __init pacpi_init(void) 336{ 337 return pci_register_driver(&pacpi_pci_driver); 338} 339 340static void __exit pacpi_exit(void) 341{ 342 pci_unregister_driver(&pacpi_pci_driver); 343} 344 345module_init(pacpi_init); 346module_exit(pacpi_exit); 347 348MODULE_AUTHOR("Alan Cox"); 349MODULE_DESCRIPTION("SCSI low-level driver for ATA in ACPI mode"); 350MODULE_LICENSE("GPL"); 351MODULE_DEVICE_TABLE(pci, pacpi_pci_tbl); 352MODULE_VERSION(DRV_VERSION); 353 354