libata: Fix a large collection of DMA mode mismatches

Dave Müller sent a diff for the pata_oldpiix that highlighted a problem
where a lot of the ATA drivers assume dma_mode == 0 means "no DMA" while
the core code uses 0xFF.

This turns out to have other consequences such as code doing >= XFER_UDMA_0
also catching 0xFF as UDMAlots. Fortunately it doesn't generally affect
set_dma_mode, although some drivers call back into their own set mode code
from other points.

Having been through the drivers I've added helpers for using_udma/using_mwdma
dma_enabled so that people don't open code ranges that may change (eg if UDMA8
appears somewhere)

Thanks to David for the initial bits
[and added fix for pata_oldpiix from and signed-off-by Dave Mueller
<dave.mueller@gmx.ch> -jg]

Signed-off-by: Alan Cox <alan@redhat.com>
Signed-off-by: Jeff Garzik <jgarzik@redhat.com>

authored by Alan Cox and committed by Jeff Garzik b15b3eba 20e2de4a

+33 -11
+2 -2
drivers/ata/libata-core.c
··· 3288 3288 dev->dma_mode = ata_xfer_mask2mode(dma_mask); 3289 3289 3290 3290 found = 1; 3291 - if (dev->dma_mode != 0xff) 3291 + if (ata_dma_enabled(dev)) 3292 3292 used_dma = 1; 3293 3293 } 3294 3294 if (!found) ··· 3313 3313 3314 3314 /* step 3: set host DMA timings */ 3315 3315 ata_link_for_each_dev(dev, link) { 3316 - if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff) 3316 + if (!ata_dev_enabled(dev) || !ata_dma_enabled(dev)) 3317 3317 continue; 3318 3318 3319 3319 dev->xfer_mode = dev->dma_mode;
+1 -1
drivers/ata/pata_acpi.c
··· 181 181 182 182 if (adev != acpi->last) { 183 183 pacpi_set_piomode(ap, adev); 184 - if (adev->dma_mode) 184 + if (ata_dma_enabled(adev)) 185 185 pacpi_set_dmamode(ap, adev); 186 186 acpi->last = adev; 187 187 }
+1 -1
drivers/ata/pata_atiixp.c
··· 183 183 u16 tmp16; 184 184 185 185 pci_read_config_word(pdev, ATIIXP_IDE_UDMA_CONTROL, &tmp16); 186 - if (adev->dma_mode >= XFER_UDMA_0) 186 + if (ata_using_udma(adev)) 187 187 tmp16 |= (1 << dn); 188 188 else 189 189 tmp16 &= ~(1 << dn);
+3 -3
drivers/ata/pata_cs5530.c
··· 149 149 struct ata_device *prev = ap->private_data; 150 150 151 151 /* See if the DMA settings could be wrong */ 152 - if (adev->dma_mode != 0 && adev != prev && prev != NULL) { 152 + if (ata_dma_enabled(adev) && adev != prev && prev != NULL) { 153 153 /* Maybe, but do the channels match MWDMA/UDMA ? */ 154 - if ((adev->dma_mode >= XFER_UDMA_0 && prev->dma_mode < XFER_UDMA_0) || 155 - (adev->dma_mode < XFER_UDMA_0 && prev->dma_mode >= XFER_UDMA_0)) 154 + if ((ata_using_udma(adev) && !ata_using_udma(prev)) || 155 + (ata_using_udma(prev) && !ata_using_udma(adev))) 156 156 /* Switch the mode bits */ 157 157 cs5530_set_dmamode(ap, adev); 158 158 }
+1 -1
drivers/ata/pata_oldpiix.c
··· 198 198 199 199 if (adev != ap->private_data) { 200 200 oldpiix_set_piomode(ap, adev); 201 - if (adev->dma_mode) 201 + if (ata_dma_enabled(adev)) 202 202 oldpiix_set_dmamode(ap, adev); 203 203 } 204 204 return ata_sff_qc_issue(qc);
+3 -3
drivers/ata/pata_sc1200.c
··· 167 167 struct ata_device *prev = ap->private_data; 168 168 169 169 /* See if the DMA settings could be wrong */ 170 - if (adev->dma_mode != 0 && adev != prev && prev != NULL) { 170 + if (ata_dma_enabled(adev) && adev != prev && prev != NULL) { 171 171 /* Maybe, but do the channels match MWDMA/UDMA ? */ 172 - if ((adev->dma_mode >= XFER_UDMA_0 && prev->dma_mode < XFER_UDMA_0) || 173 - (adev->dma_mode < XFER_UDMA_0 && prev->dma_mode >= XFER_UDMA_0)) 172 + if ((ata_using_udma(adev) && !ata_using_udma(prev)) || 173 + (ata_using_udma(prev) && !ata_using_udma(adev))) 174 174 /* Switch the mode bits */ 175 175 sc1200_set_dmamode(ap, adev); 176 176 }
+22
include/linux/libata.h
··· 1429 1429 return from_jiffies + msecs_to_jiffies(timeout_msecs); 1430 1430 } 1431 1431 1432 + /* Don't open code these in drivers as there are traps. Firstly the range may 1433 + change in future hardware and specs, secondly 0xFF means 'no DMA' but is 1434 + > UDMA_0. Dyma ddreigiau */ 1435 + 1436 + static inline int ata_using_mwdma(struct ata_device *adev) 1437 + { 1438 + if (adev->dma_mode >= XFER_MW_DMA_0 && adev->dma_mode <= XFER_MW_DMA_4) 1439 + return 1; 1440 + return 0; 1441 + } 1442 + 1443 + static inline int ata_using_udma(struct ata_device *adev) 1444 + { 1445 + if (adev->dma_mode >= XFER_UDMA_0 && adev->dma_mode <= XFER_UDMA_7) 1446 + return 1; 1447 + return 0; 1448 + } 1449 + 1450 + static inline int ata_dma_enabled(struct ata_device *adev) 1451 + { 1452 + return (adev->dma_mode == 0xFF ? 0 : 1); 1453 + } 1432 1454 1433 1455 /************************************************************************** 1434 1456 * PMP - drivers/ata/libata-pmp.c