aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/ABI/testing/sysfs-firmware-acpi127
-rw-r--r--Documentation/filesystems/ubifs.txt164
-rw-r--r--Documentation/ioctl/hdio.txt7
-rw-r--r--Documentation/kernel-parameters.txt14
-rw-r--r--Documentation/laptops/acer-wmi.txt2
-rw-r--r--MAINTAINERS64
-rw-r--r--Makefile1
-rw-r--r--arch/avr32/boards/atngw100/setup.c7
-rw-r--r--arch/avr32/boards/atstk1000/atstk1002.c3
-rw-r--r--arch/avr32/mach-at32ap/at32ap700x.c30
-rw-r--r--arch/frv/mb93090-mb00/pci-frv.c30
-rw-r--r--arch/ia64/kernel/process.c4
-rw-r--r--arch/ia64/kvm/kvm-ia64.c4
-rw-r--r--arch/ia64/kvm/kvm_fw.c2
-rw-r--r--arch/m68knommu/kernel/comempci.c9
-rw-r--r--arch/mips/pmc-sierra/yosemite/ht.c36
-rw-r--r--arch/s390/Kconfig2
-rw-r--r--arch/s390/kernel/stacktrace.c1
-rw-r--r--arch/sh/drivers/pci/pci.c32
-rw-r--r--arch/sparc64/kernel/pci.c2
-rw-r--r--arch/x86/kernel/Makefile3
-rw-r--r--arch/x86/kernel/acpi/processor.c6
-rw-r--r--arch/x86/kernel/acpi/sleep.c2
-rw-r--r--arch/x86/kernel/apm_32.c8
-rw-r--r--arch/x86/kernel/early-quirks.c26
-rw-r--r--arch/x86/kernel/process.c28
-rw-r--r--arch/x86/kernel/setup.c5
-rw-r--r--arch/x86/mm/srat_32.c3
-rw-r--r--arch/x86/pci/common.c30
-rw-r--r--arch/x86/pci/early.c60
-rw-r--r--arch/x86/pci/irq.c120
-rw-r--r--arch/x86/pci/pci.h1
-rw-r--r--drivers/acpi/Kconfig9
-rw-r--r--drivers/acpi/Makefile3
-rw-r--r--drivers/acpi/bay.c3
-rw-r--r--drivers/acpi/bus.c24
-rw-r--r--drivers/acpi/dispatcher/dsinit.c2
-rw-r--r--drivers/acpi/dispatcher/dsmethod.c1
-rw-r--r--drivers/acpi/dispatcher/dsopcode.c22
-rw-r--r--drivers/acpi/dispatcher/dswexec.c8
-rw-r--r--drivers/acpi/dispatcher/dswstate.c20
-rw-r--r--drivers/acpi/dock.c3
-rw-r--r--drivers/acpi/events/evevent.c6
-rw-r--r--drivers/acpi/events/evgpe.c36
-rw-r--r--drivers/acpi/events/evgpeblk.c18
-rw-r--r--drivers/acpi/events/evmisc.c4
-rw-r--r--drivers/acpi/events/evregion.c5
-rw-r--r--drivers/acpi/events/evrgnini.c2
-rw-r--r--drivers/acpi/events/evxfevnt.c18
-rw-r--r--drivers/acpi/executer/exconfig.c17
-rw-r--r--drivers/acpi/executer/exconvrt.c12
-rw-r--r--drivers/acpi/executer/excreate.c2
-rw-r--r--drivers/acpi/executer/exdump.c67
-rw-r--r--drivers/acpi/executer/exfldio.c9
-rw-r--r--drivers/acpi/executer/exmisc.c8
-rw-r--r--drivers/acpi/executer/exprep.c8
-rw-r--r--drivers/acpi/executer/exregion.c2
-rw-r--r--drivers/acpi/executer/exresop.c4
-rw-r--r--drivers/acpi/executer/exstore.c6
-rw-r--r--drivers/acpi/fan.c8
-rw-r--r--drivers/acpi/glue.c5
-rw-r--r--drivers/acpi/hardware/hwgpe.c52
-rw-r--r--drivers/acpi/namespace/nsdump.c6
-rw-r--r--drivers/acpi/namespace/nseval.c35
-rw-r--r--drivers/acpi/namespace/nsinit.c1
-rw-r--r--drivers/acpi/namespace/nsload.c3
-rw-r--r--drivers/acpi/namespace/nsparse.c15
-rw-r--r--drivers/acpi/namespace/nsutils.c50
-rw-r--r--drivers/acpi/namespace/nsxfeval.c3
-rw-r--r--drivers/acpi/numa.c4
-rw-r--r--drivers/acpi/parser/psargs.c4
-rw-r--r--drivers/acpi/parser/psxface.c4
-rw-r--r--drivers/acpi/pci_irq.c38
-rw-r--r--drivers/acpi/pci_slot.c368
-rw-r--r--drivers/acpi/power.c138
-rw-r--r--drivers/acpi/processor_core.c75
-rw-r--r--drivers/acpi/processor_idle.c34
-rw-r--r--drivers/acpi/processor_perflib.c18
-rw-r--r--drivers/acpi/processor_throttling.c38
-rw-r--r--drivers/acpi/reboot.c50
-rw-r--r--drivers/acpi/resources/rscalc.c4
-rw-r--r--drivers/acpi/resources/rscreate.c41
-rw-r--r--drivers/acpi/resources/rsmisc.c2
-rw-r--r--drivers/acpi/resources/rsutils.c13
-rw-r--r--drivers/acpi/scan.c104
-rw-r--r--drivers/acpi/sleep/main.c328
-rw-r--r--drivers/acpi/sleep/wakeup.c13
-rw-r--r--drivers/acpi/system.c169
-rw-r--r--drivers/acpi/tables/tbfadt.c23
-rw-r--r--drivers/acpi/tables/tbfind.c5
-rw-r--r--drivers/acpi/tables/tbinstal.c30
-rw-r--r--drivers/acpi/tables/tbutils.c15
-rw-r--r--drivers/acpi/tables/tbxface.c28
-rw-r--r--drivers/acpi/tables/tbxfroot.c4
-rw-r--r--drivers/acpi/utilities/utalloc.c5
-rw-r--r--drivers/acpi/utilities/utcopy.c4
-rw-r--r--drivers/acpi/utilities/utdebug.c54
-rw-r--r--drivers/acpi/utilities/utdelete.c2
-rw-r--r--drivers/acpi/utilities/uteval.c5
-rw-r--r--drivers/acpi/utilities/utmisc.c39
-rw-r--r--drivers/acpi/utilities/utmutex.c4
-rw-r--r--drivers/acpi/utilities/utobject.c9
-rw-r--r--drivers/acpi/video.c123
-rw-r--r--drivers/base/platform.c296
-rw-r--r--drivers/base/power/main.c675
-rw-r--r--drivers/base/power/power.h2
-rw-r--r--drivers/base/power/sysfs.c3
-rw-r--r--drivers/base/power/trace.c4
-rw-r--r--drivers/block/Kconfig12
-rw-r--r--drivers/block/Makefile1
-rw-r--r--drivers/block/hd.c (renamed from drivers/ide/legacy/hd.c)3
-rw-r--r--drivers/char/apm-emulation.c346
-rw-r--r--drivers/ide/Kconfig88
-rw-r--r--drivers/ide/Makefile10
-rw-r--r--drivers/ide/arm/Makefile1
-rw-r--r--drivers/ide/arm/bast-ide.c90
-rw-r--r--drivers/ide/arm/icside.c105
-rw-r--r--drivers/ide/arm/palm_bk3710.c3
-rw-r--r--drivers/ide/arm/rapide.c33
-rw-r--r--drivers/ide/h8300/ide-h8300.c19
-rw-r--r--drivers/ide/ide-cd.c160
-rw-r--r--drivers/ide/ide-disk.c1
-rw-r--r--drivers/ide/ide-floppy.c32
-rw-r--r--drivers/ide/ide-io.c72
-rw-r--r--drivers/ide/ide-iops.c24
-rw-r--r--drivers/ide/ide-lib.c151
-rw-r--r--drivers/ide/ide-pio-blacklist.c94
-rw-r--r--drivers/ide/ide-pnp.c4
-rw-r--r--drivers/ide/ide-probe.c21
-rw-r--r--drivers/ide/ide-tape.c1
-rw-r--r--drivers/ide/ide-taskfile.c33
-rw-r--r--drivers/ide/ide-timings.c (renamed from drivers/ide/ide-timing.h)204
-rw-r--r--drivers/ide/ide.c76
-rw-r--r--drivers/ide/legacy/ali14xx.c3
-rw-r--r--drivers/ide/legacy/buddha.c1
-rw-r--r--drivers/ide/legacy/falconide.c1
-rw-r--r--drivers/ide/legacy/gayle.c1
-rw-r--r--drivers/ide/legacy/ht6560b.c15
-rw-r--r--drivers/ide/legacy/ide-4drives.c25
-rw-r--r--drivers/ide/legacy/ide-cs.c9
-rw-r--r--drivers/ide/legacy/ide_platform.c9
-rw-r--r--drivers/ide/legacy/macide.c1
-rw-r--r--drivers/ide/legacy/q40ide.c1
-rw-r--r--drivers/ide/legacy/qd65xx.c19
-rw-r--r--drivers/ide/mips/au1xxx-ide.c13
-rw-r--r--drivers/ide/mips/swarm.c28
-rw-r--r--drivers/ide/pci/alim15x3.c6
-rw-r--r--drivers/ide/pci/amd74xx.c2
-rw-r--r--drivers/ide/pci/cmd640.c131
-rw-r--r--drivers/ide/pci/cmd64x.c6
-rw-r--r--drivers/ide/pci/cs5535.c6
-rw-r--r--drivers/ide/pci/cy82c693.c9
-rw-r--r--drivers/ide/pci/delkin_cb.c1
-rw-r--r--drivers/ide/pci/it821x.c6
-rw-r--r--drivers/ide/pci/scc_pata.c8
-rw-r--r--drivers/ide/pci/sgiioc4.c23
-rw-r--r--drivers/ide/pci/siimage.c3
-rw-r--r--drivers/ide/pci/sis5513.c3
-rw-r--r--drivers/ide/pci/sl82c105.c3
-rw-r--r--drivers/ide/pci/via82cxxx.c2
-rw-r--r--drivers/ide/ppc/Makefile1
-rw-r--r--drivers/ide/ppc/mpc8xx.c851
-rw-r--r--drivers/ide/ppc/pmac.c21
-rw-r--r--drivers/ide/setup-pci.c25
-rw-r--r--drivers/misc/Kconfig31
-rw-r--r--drivers/misc/Makefile5
-rw-r--r--drivers/misc/acer-wmi.c145
-rw-r--r--drivers/misc/compal-laptop.c404
-rw-r--r--drivers/misc/eeepc-laptop.c4
-rw-r--r--drivers/misc/fujitsu-laptop.c825
-rw-r--r--drivers/mmc/card/block.c60
-rw-r--r--drivers/mmc/card/mmc_test.c569
-rw-r--r--drivers/mmc/card/sdio_uart.c9
-rw-r--r--drivers/mmc/core/core.c41
-rw-r--r--drivers/mmc/core/mmc.c2
-rw-r--r--drivers/mmc/core/sd.c6
-rw-r--r--drivers/mmc/core/sdio_cis.c6
-rw-r--r--drivers/mmc/core/sdio_io.c167
-rw-r--r--drivers/mmc/host/Kconfig50
-rw-r--r--drivers/mmc/host/Makefile4
-rw-r--r--drivers/mmc/host/at91_mci.c257
-rw-r--r--drivers/mmc/host/atmel-mci-regs.h91
-rw-r--r--drivers/mmc/host/atmel-mci.c981
-rw-r--r--drivers/mmc/host/au1xmmc.c792
-rw-r--r--drivers/mmc/host/au1xmmc.h96
-rw-r--r--drivers/mmc/host/imxmmc.c9
-rw-r--r--drivers/mmc/host/mmc_spi.c33
-rw-r--r--drivers/mmc/host/mmci.c1
-rw-r--r--drivers/mmc/host/omap.c2
-rw-r--r--drivers/mmc/host/pxamci.c9
-rw-r--r--drivers/mmc/host/s3cmci.c1446
-rw-r--r--drivers/mmc/host/s3cmci.h70
-rw-r--r--drivers/mmc/host/sdhci-pci.c732
-rw-r--r--drivers/mmc/host/sdhci.c994
-rw-r--r--drivers/mmc/host/sdhci.h120
-rw-r--r--drivers/mmc/host/sdricoh_cs.c575
-rw-r--r--drivers/mmc/host/tifm_sd.c2
-rw-r--r--drivers/mmc/host/wbsd.c38
-rw-r--r--drivers/net/wireless/libertas/if_sdio.c20
-rw-r--r--drivers/pci/Makefile2
-rw-r--r--drivers/pci/hotplug/acpi_pcihp.c85
-rw-r--r--drivers/pci/hotplug/acpiphp.h1
-rw-r--r--drivers/pci/hotplug/acpiphp_core.c25
-rw-r--r--drivers/pci/hotplug/acpiphp_glue.c23
-rw-r--r--drivers/pci/hotplug/acpiphp_ibm.c6
-rw-r--r--drivers/pci/hotplug/cpci_hotplug_core.c2
-rw-r--r--drivers/pci/hotplug/cpqphp_core.c4
-rw-r--r--drivers/pci/hotplug/fakephp.c86
-rw-r--r--drivers/pci/hotplug/ibmphp_ebda.c3
-rw-r--r--drivers/pci/hotplug/pci_hotplug_core.c284
-rw-r--r--drivers/pci/hotplug/pciehp.h16
-rw-r--r--drivers/pci/hotplug/pciehp_core.c127
-rw-r--r--drivers/pci/hotplug/pciehp_hpc.c318
-rw-r--r--drivers/pci/hotplug/rpadlpar_sysfs.c5
-rw-r--r--drivers/pci/hotplug/rpaphp_slot.c44
-rw-r--r--drivers/pci/hotplug/sgi_hotplug.c12
-rw-r--r--drivers/pci/hotplug/shpchp.h14
-rw-r--r--drivers/pci/hotplug/shpchp_core.c37
-rw-r--r--drivers/pci/hotplug/shpchp_hpc.c1
-rw-r--r--drivers/pci/intel-iommu.c1
-rw-r--r--drivers/pci/msi.c22
-rw-r--r--drivers/pci/pci-acpi.c277
-rw-r--r--drivers/pci/pci-driver.c388
-rw-r--r--drivers/pci/pci.c479
-rw-r--r--drivers/pci/pci.h48
-rw-r--r--drivers/pci/pcie/aer/aerdrv.c9
-rw-r--r--drivers/pci/pcie/aer/aerdrv_acpi.c8
-rw-r--r--drivers/pci/pcie/aer/aerdrv_core.c24
-rw-r--r--drivers/pci/pcie/portdrv_bus.c1
-rw-r--r--drivers/pci/pcie/portdrv_core.c22
-rw-r--r--drivers/pci/pcie/portdrv_pci.c5
-rw-r--r--drivers/pci/probe.c38
-rw-r--r--drivers/pci/proc.c4
-rw-r--r--drivers/pci/quirks.c134
-rw-r--r--drivers/pci/setup-bus.c43
-rw-r--r--drivers/pci/setup-irq.c3
-rw-r--r--drivers/pci/setup-res.c70
-rw-r--r--drivers/pci/slot.c233
-rw-r--r--drivers/pnp/base.h148
-rw-r--r--drivers/pnp/core.c29
-rw-r--r--drivers/pnp/interface.c207
-rw-r--r--drivers/pnp/isapnp/core.c253
-rw-r--r--drivers/pnp/manager.c414
-rw-r--r--drivers/pnp/pnpacpi/core.c4
-rw-r--r--drivers/pnp/pnpacpi/rsparser.c492
-rw-r--r--drivers/pnp/pnpbios/rsparser.c274
-rw-r--r--drivers/pnp/quirks.c307
-rw-r--r--drivers/pnp/resource.c454
-rw-r--r--drivers/pnp/support.c171
-rw-r--r--drivers/pnp/system.c4
-rw-r--r--drivers/s390/block/dasd_diag.c25
-rw-r--r--drivers/s390/block/dasd_eckd.c4
-rw-r--r--drivers/s390/block/dasd_fba.c7
-rw-r--r--drivers/s390/char/vmur.c10
-rw-r--r--drivers/s390/char/zcore.c101
-rw-r--r--drivers/s390/cio/Makefile2
-rw-r--r--drivers/s390/cio/chsc.c9
-rw-r--r--drivers/s390/cio/chsc.h2
-rw-r--r--drivers/s390/cio/qdio.c3929
-rw-r--r--drivers/s390/cio/qdio.h835
-rw-r--r--drivers/s390/cio/qdio_debug.c240
-rw-r--r--drivers/s390/cio/qdio_debug.h91
-rw-r--r--drivers/s390/cio/qdio_main.c1755
-rw-r--r--drivers/s390/cio/qdio_perf.c151
-rw-r--r--drivers/s390/cio/qdio_perf.h54
-rw-r--r--drivers/s390/cio/qdio_setup.c521
-rw-r--r--drivers/s390/cio/qdio_thinint.c380
-rw-r--r--drivers/s390/net/qeth_core.h12
-rw-r--r--drivers/s390/net/qeth_core_main.c87
-rw-r--r--drivers/s390/net/qeth_l2_main.c26
-rw-r--r--drivers/s390/net/qeth_l3_main.c25
-rw-r--r--drivers/s390/scsi/zfcp_dbf.c12
-rw-r--r--drivers/s390/scsi/zfcp_dbf.h2
-rw-r--r--drivers/s390/scsi/zfcp_ext.h5
-rw-r--r--drivers/s390/scsi/zfcp_qdio.c42
-rw-r--r--drivers/scsi/device_handler/scsi_dh_emc.c7
-rw-r--r--drivers/scsi/device_handler/scsi_dh_hp_sw.c7
-rw-r--r--drivers/scsi/device_handler/scsi_dh_rdac.c7
-rw-r--r--drivers/scsi/ide-scsi.c14
-rw-r--r--drivers/serial/8250_pnp.c24
-rw-r--r--fs/Kconfig139
-rw-r--r--fs/Makefile1
-rw-r--r--fs/fs-writeback.c22
-rw-r--r--fs/lockd/clntproc.c8
-rw-r--r--fs/lockd/svc4proc.c2
-rw-r--r--fs/lockd/svclock.c7
-rw-r--r--fs/lockd/svcproc.c2
-rw-r--r--fs/nfs/callback.c34
-rw-r--r--fs/nfs/client.c13
-rw-r--r--fs/nfs/dir.c88
-rw-r--r--fs/nfs/direct.c4
-rw-r--r--fs/nfs/file.c155
-rw-r--r--fs/nfs/inode.c79
-rw-r--r--fs/nfs/internal.h1
-rw-r--r--fs/nfs/iostat.h119
-rw-r--r--fs/nfs/nfs3acl.c9
-rw-r--r--fs/nfs/nfs3proc.c275
-rw-r--r--fs/nfs/nfs4proc.c265
-rw-r--r--fs/nfs/nfs4state.c2
-rw-r--r--fs/nfs/nfsroot.c10
-rw-r--r--fs/nfs/proc.c28
-rw-r--r--fs/nfs/super.c882
-rw-r--r--fs/nfs/write.c322
-rw-r--r--fs/nfsd/nfs4callback.c2
-rw-r--r--fs/ubifs/Kconfig72
-rw-r--r--fs/ubifs/Makefile9
-rw-r--r--fs/ubifs/budget.c731
-rw-r--r--fs/ubifs/commit.c677
-rw-r--r--fs/ubifs/compress.c253
-rw-r--r--fs/ubifs/debug.c2289
-rw-r--r--fs/ubifs/debug.h403
-rw-r--r--fs/ubifs/dir.c1240
-rw-r--r--fs/ubifs/file.c1275
-rw-r--r--fs/ubifs/find.c975
-rw-r--r--fs/ubifs/gc.c773
-rw-r--r--fs/ubifs/io.c914
-rw-r--r--fs/ubifs/ioctl.c204
-rw-r--r--fs/ubifs/journal.c1387
-rw-r--r--fs/ubifs/key.h533
-rw-r--r--fs/ubifs/log.c805
-rw-r--r--fs/ubifs/lprops.c1357
-rw-r--r--fs/ubifs/lpt.c2243
-rw-r--r--fs/ubifs/lpt_commit.c1648
-rw-r--r--fs/ubifs/master.c387
-rw-r--r--fs/ubifs/misc.h342
-rw-r--r--fs/ubifs/orphan.c958
-rw-r--r--fs/ubifs/recovery.c1519
-rw-r--r--fs/ubifs/replay.c1075
-rw-r--r--fs/ubifs/sb.c629
-rw-r--r--fs/ubifs/scan.c362
-rw-r--r--fs/ubifs/shrinker.c322
-rw-r--r--fs/ubifs/super.c1951
-rw-r--r--fs/ubifs/tnc.c2956
-rw-r--r--fs/ubifs/tnc_commit.c1103
-rw-r--r--fs/ubifs/tnc_misc.c494
-rw-r--r--fs/ubifs/ubifs-media.h745
-rw-r--r--fs/ubifs/ubifs.h1649
-rw-r--r--fs/ubifs/xattr.c581
-rw-r--r--include/acpi/acconfig.h2
-rw-r--r--include/acpi/acdisasm.h1
-rw-r--r--include/acpi/acdispat.h2
-rw-r--r--include/acpi/acexcep.h10
-rw-r--r--include/acpi/acglobal.h2
-rw-r--r--include/acpi/achware.h4
-rw-r--r--include/acpi/acinterp.h5
-rw-r--r--include/acpi/aclocal.h6
-rw-r--r--include/acpi/acmacros.h38
-rw-r--r--include/acpi/acnamesp.h30
-rw-r--r--include/acpi/acpi_bus.h12
-rw-r--r--include/acpi/acpi_drivers.h4
-rw-r--r--include/acpi/acpiosxf.h2
-rw-r--r--include/acpi/acpixf.h12
-rw-r--r--include/acpi/acstruct.h10
-rw-r--r--include/acpi/actables.h27
-rw-r--r--include/acpi/actbl1.h23
-rw-r--r--include/acpi/actypes.h16
-rw-r--r--include/acpi/acutils.h70
-rw-r--r--include/acpi/processor.h2
-rw-r--r--include/acpi/reboot.h14
-rw-r--r--include/asm-arm/arch-at91/at91_mci.h4
-rw-r--r--include/asm-arm/arch-s3c2410/regs-sdi.h20
-rw-r--r--include/asm-arm/plat-s3c24xx/mci.h15
-rw-r--r--include/asm-avr32/arch-at32ap/board.h6
-rw-r--r--include/asm-avr32/atmel-mci.h9
-rw-r--r--include/asm-generic/vmlinux.lds.h6
-rw-r--r--include/asm-ia64/processor.h2
-rw-r--r--include/asm-mips/mach-au1x00/au1100_mmc.h18
-rw-r--r--include/asm-s390/Kbuild2
-rw-r--r--include/asm-s390/chpid.h6
-rw-r--r--include/asm-s390/qdio.h646
-rw-r--r--include/asm-s390/schid.h3
-rw-r--r--include/asm-s390/setup.h2
-rw-r--r--include/asm-x86/pci-direct.h4
-rw-r--r--include/asm-x86/processor.h2
-rw-r--r--include/linux/acpi.h3
-rw-r--r--include/linux/blkdev.h3
-rw-r--r--include/linux/device.h9
-rw-r--r--include/linux/freezer.h10
-rw-r--r--include/linux/fs.h2
-rw-r--r--include/linux/ide.h74
-rw-r--r--include/linux/inet.h7
-rw-r--r--include/linux/init_task.h4
-rw-r--r--include/linux/ioport.h5
-rw-r--r--include/linux/mmc/core.h1
-rw-r--r--include/linux/mmc/host.h32
-rw-r--r--include/linux/mmc/mmc.h1
-rw-r--r--include/linux/mmc/sdio_func.h21
-rw-r--r--include/linux/nfs_fs.h10
-rw-r--r--include/linux/nfs_iostat.h119
-rw-r--r--include/linux/nfs_page.h9
-rw-r--r--include/linux/nfs_xdr.h3
-rw-r--r--include/linux/pci.h57
-rw-r--r--include/linux/pci_hotplug.h14
-rw-r--r--include/linux/pci_ids.h1
-rw-r--r--include/linux/pci_regs.h1
-rw-r--r--include/linux/platform_device.h1
-rw-r--r--include/linux/pm.h314
-rw-r--r--include/linux/pm_wakeup.h28
-rw-r--r--include/linux/pnp.h146
-rw-r--r--include/linux/sched.h27
-rw-r--r--include/linux/spi/mmc_spi.h9
-rw-r--r--include/linux/sunrpc/clnt.h7
-rw-r--r--include/linux/sunrpc/sched.h1
-rw-r--r--include/linux/suspend.h14
-rw-r--r--init/do_mounts.c3
-rw-r--r--kernel/Makefile3
-rw-r--r--kernel/cpu.c1
-rw-r--r--kernel/exit.c451
-rw-r--r--kernel/fork.c6
-rw-r--r--kernel/kthread.c2
-rw-r--r--kernel/power/disk.c50
-rw-r--r--kernel/power/main.c16
-rw-r--r--kernel/power/process.c97
-rw-r--r--kernel/power/user.c71
-rw-r--r--kernel/ptrace.c37
-rw-r--r--lib/Makefile14
-rw-r--r--lib/kobject.c1
-rw-r--r--net/sunrpc/auth_gss/auth_gss.c27
-rw-r--r--net/sunrpc/auth_gss/gss_krb5_mech.c4
-rw-r--r--net/sunrpc/auth_gss/gss_spkm3_mech.c4
-rw-r--r--net/sunrpc/auth_gss/gss_spkm3_token.c2
-rw-r--r--net/sunrpc/auth_unix.c2
-rw-r--r--net/sunrpc/clnt.c161
-rw-r--r--net/sunrpc/rpcb_clnt.c356
-rw-r--r--net/sunrpc/sched.c23
-rw-r--r--net/sunrpc/xprt.c9
-rw-r--r--net/sunrpc/xprtsock.c2
-rw-r--r--scripts/Makefile.fwinst25
428 files changed, 54427 insertions, 13805 deletions
diff --git a/Documentation/ABI/testing/sysfs-firmware-acpi b/Documentation/ABI/testing/sysfs-firmware-acpi
index 9470ed9afcc0..f27be7d1a49f 100644
--- a/Documentation/ABI/testing/sysfs-firmware-acpi
+++ b/Documentation/ABI/testing/sysfs-firmware-acpi
@@ -29,46 +29,46 @@ Description:
29 29
30 $ cd /sys/firmware/acpi/interrupts 30 $ cd /sys/firmware/acpi/interrupts
31 $ grep . * 31 $ grep . *
32 error:0 32 error: 0
33 ff_gbl_lock:0 33 ff_gbl_lock: 0 enable
34 ff_pmtimer:0 34 ff_pmtimer: 0 invalid
35 ff_pwr_btn:0 35 ff_pwr_btn: 0 enable
36 ff_rt_clk:0 36 ff_rt_clk: 2 disable
37 ff_slp_btn:0 37 ff_slp_btn: 0 invalid
38 gpe00:0 38 gpe00: 0 invalid
39 gpe01:0 39 gpe01: 0 enable
40 gpe02:0 40 gpe02: 108 enable
41 gpe03:0 41 gpe03: 0 invalid
42 gpe04:0 42 gpe04: 0 invalid
43 gpe05:0 43 gpe05: 0 invalid
44 gpe06:0 44 gpe06: 0 enable
45 gpe07:0 45 gpe07: 0 enable
46 gpe08:0 46 gpe08: 0 invalid
47 gpe09:174 47 gpe09: 0 invalid
48 gpe0A:0 48 gpe0A: 0 invalid
49 gpe0B:0 49 gpe0B: 0 invalid
50 gpe0C:0 50 gpe0C: 0 invalid
51 gpe0D:0 51 gpe0D: 0 invalid
52 gpe0E:0 52 gpe0E: 0 invalid
53 gpe0F:0 53 gpe0F: 0 invalid
54 gpe10:0 54 gpe10: 0 invalid
55 gpe11:60 55 gpe11: 0 invalid
56 gpe12:0 56 gpe12: 0 invalid
57 gpe13:0 57 gpe13: 0 invalid
58 gpe14:0 58 gpe14: 0 invalid
59 gpe15:0 59 gpe15: 0 invalid
60 gpe16:0 60 gpe16: 0 invalid
61 gpe17:0 61 gpe17: 1084 enable
62 gpe18:0 62 gpe18: 0 enable
63 gpe19:7 63 gpe19: 0 invalid
64 gpe1A:0 64 gpe1A: 0 invalid
65 gpe1B:0 65 gpe1B: 0 invalid
66 gpe1C:0 66 gpe1C: 0 invalid
67 gpe1D:0 67 gpe1D: 0 invalid
68 gpe1E:0 68 gpe1E: 0 invalid
69 gpe1F:0 69 gpe1F: 0 invalid
70 gpe_all:241 70 gpe_all: 1192
71 sci:241 71 sci: 1194
72 72
73 sci - The total number of times the ACPI SCI 73 sci - The total number of times the ACPI SCI
74 has claimed an interrupt. 74 has claimed an interrupt.
@@ -89,6 +89,13 @@ Description:
89 89
90 error - an interrupt that can't be accounted for above. 90 error - an interrupt that can't be accounted for above.
91 91
92 invalid: it's either a wakeup GPE or a GPE/Fixed Event that
93 doesn't have an event handler.
94
95 disable: the GPE/Fixed Event is valid but disabled.
96
97 enable: the GPE/Fixed Event is valid and enabled.
98
92 Root has permission to clear any of these counters. Eg. 99 Root has permission to clear any of these counters. Eg.
93 # echo 0 > gpe11 100 # echo 0 > gpe11
94 101
@@ -97,3 +104,43 @@ Description:
97 104
98 None of these counters has an effect on the function 105 None of these counters has an effect on the function
99 of the system, they are simply statistics. 106 of the system, they are simply statistics.
107
108 Besides this, user can also write specific strings to these files
109 to enable/disable/clear ACPI interrupts in user space, which can be
110 used to debug some ACPI interrupt storm issues.
111
112 Note that only writting to VALID GPE/Fixed Event is allowed,
113 i.e. user can only change the status of runtime GPE and
114 Fixed Event with event handler installed.
115
116 Let's take power button fixed event for example, please kill acpid
117 and other user space applications so that the machine won't shutdown
118 when pressing the power button.
119 # cat ff_pwr_btn
120 0
121 # press the power button for 3 times;
122 # cat ff_pwr_btn
123 3
124 # echo disable > ff_pwr_btn
125 # cat ff_pwr_btn
126 disable
127 # press the power button for 3 times;
128 # cat ff_pwr_btn
129 disable
130 # echo enable > ff_pwr_btn
131 # cat ff_pwr_btn
132 4
133 /*
134 * this is because the status bit is set even if the enable bit is cleared,
135 * and it triggers an ACPI fixed event when the enable bit is set again
136 */
137 # press the power button for 3 times;
138 # cat ff_pwr_btn
139 7
140 # echo disable > ff_pwr_btn
141 # press the power button for 3 times;
142 # echo clear > ff_pwr_btn /* clear the status bit */
143 # echo disable > ff_pwr_btn
144 # cat ff_pwr_btn
145 7
146
diff --git a/Documentation/filesystems/ubifs.txt b/Documentation/filesystems/ubifs.txt
new file mode 100644
index 000000000000..540e9e7f59c5
--- /dev/null
+++ b/Documentation/filesystems/ubifs.txt
@@ -0,0 +1,164 @@
1Introduction
2=============
3
4UBIFS file-system stands for UBI File System. UBI stands for "Unsorted
5Block Images". UBIFS is a flash file system, which means it is designed
6to work with flash devices. It is important to understand, that UBIFS
7is completely different to any traditional file-system in Linux, like
8Ext2, XFS, JFS, etc. UBIFS represents a separate class of file-systems
9which work with MTD devices, not block devices. The other Linux
10file-system of this class is JFFS2.
11
12To make it more clear, here is a small comparison of MTD devices and
13block devices.
14
151 MTD devices represent flash devices and they consist of eraseblocks of
16 rather large size, typically about 128KiB. Block devices consist of
17 small blocks, typically 512 bytes.
182 MTD devices support 3 main operations - read from some offset within an
19 eraseblock, write to some offset within an eraseblock, and erase a whole
20 eraseblock. Block devices support 2 main operations - read a whole
21 block and write a whole block.
223 The whole eraseblock has to be erased before it becomes possible to
23 re-write its contents. Blocks may be just re-written.
244 Eraseblocks become worn out after some number of erase cycles -
25 typically 100K-1G for SLC NAND and NOR flashes, and 1K-10K for MLC
26 NAND flashes. Blocks do not have the wear-out property.
275 Eraseblocks may become bad (only on NAND flashes) and software should
28 deal with this. Blocks on hard drives typically do not become bad,
29 because hardware has mechanisms to substitute bad blocks, at least in
30 modern LBA disks.
31
32It should be quite obvious why UBIFS is very different to traditional
33file-systems.
34
35UBIFS works on top of UBI. UBI is a separate software layer which may be
36found in drivers/mtd/ubi. UBI is basically a volume management and
37wear-leveling layer. It provides so called UBI volumes which is a higher
38level abstraction than a MTD device. The programming model of UBI devices
39is very similar to MTD devices - they still consist of large eraseblocks,
40they have read/write/erase operations, but UBI devices are devoid of
41limitations like wear and bad blocks (items 4 and 5 in the above list).
42
43In a sense, UBIFS is a next generation of JFFS2 file-system, but it is
44very different and incompatible to JFFS2. The following are the main
45differences.
46
47* JFFS2 works on top of MTD devices, UBIFS depends on UBI and works on
48 top of UBI volumes.
49* JFFS2 does not have on-media index and has to build it while mounting,
50 which requires full media scan. UBIFS maintains the FS indexing
51 information on the flash media and does not require full media scan,
52 so it mounts many times faster than JFFS2.
53* JFFS2 is a write-through file-system, while UBIFS supports write-back,
54 which makes UBIFS much faster on writes.
55
56Similarly to JFFS2, UBIFS supports on-the-flight compression which makes
57it possible to fit quite a lot of data to the flash.
58
59Similarly to JFFS2, UBIFS is tolerant of unclean reboots and power-cuts.
60It does not need stuff like ckfs.ext2. UBIFS automatically replays its
61journal and recovers from crashes, ensuring that the on-flash data
62structures are consistent.
63
64UBIFS scales logarithmically (most of the data structures it uses are
65trees), so the mount time and memory consumption do not linearly depend
66on the flash size, like in case of JFFS2. This is because UBIFS
67maintains the FS index on the flash media. However, UBIFS depends on
68UBI, which scales linearly. So overall UBI/UBIFS stack scales linearly.
69Nevertheless, UBI/UBIFS scales considerably better than JFFS2.
70
71The authors of UBIFS believe, that it is possible to develop UBI2 which
72would scale logarithmically as well. UBI2 would support the same API as UBI,
73but it would be binary incompatible to UBI. So UBIFS would not need to be
74changed to use UBI2
75
76
77Mount options
78=============
79
80(*) == default.
81
82norm_unmount (*) commit on unmount; the journal is committed
83 when the file-system is unmounted so that the
84 next mount does not have to replay the journal
85 and it becomes very fast;
86fast_unmount do not commit on unmount; this option makes
87 unmount faster, but the next mount slower
88 because of the need to replay the journal.
89
90
91Quick usage instructions
92========================
93
94The UBI volume to mount is specified using "ubiX_Y" or "ubiX:NAME" syntax,
95where "X" is UBI device number, "Y" is UBI volume number, and "NAME" is
96UBI volume name.
97
98Mount volume 0 on UBI device 0 to /mnt/ubifs:
99$ mount -t ubifs ubi0_0 /mnt/ubifs
100
101Mount "rootfs" volume of UBI device 0 to /mnt/ubifs ("rootfs" is volume
102name):
103$ mount -t ubifs ubi0:rootfs /mnt/ubifs
104
105The following is an example of the kernel boot arguments to attach mtd0
106to UBI and mount volume "rootfs":
107ubi.mtd=0 root=ubi0:rootfs rootfstype=ubifs
108
109
110Module Parameters for Debugging
111===============================
112
113When UBIFS has been compiled with debugging enabled, there are 3 module
114parameters that are available to control aspects of testing and debugging.
115The parameters are unsigned integers where each bit controls an option.
116The parameters are:
117
118debug_msgs Selects which debug messages to display, as follows:
119
120 Message Type Flag value
121
122 General messages 1
123 Journal messages 2
124 Mount messages 4
125 Commit messages 8
126 LEB search messages 16
127 Budgeting messages 32
128 Garbage collection messages 64
129 Tree Node Cache (TNC) messages 128
130 LEB properties (lprops) messages 256
131 Input/output messages 512
132 Log messages 1024
133 Scan messages 2048
134 Recovery messages 4096
135
136debug_chks Selects extra checks that UBIFS can do while running:
137
138 Check Flag value
139
140 General checks 1
141 Check Tree Node Cache (TNC) 2
142 Check indexing tree size 4
143 Check orphan area 8
144 Check old indexing tree 16
145 Check LEB properties (lprops) 32
146 Check leaf nodes and inodes 64
147
148debug_tsts Selects a mode of testing, as follows:
149
150 Test mode Flag value
151
152 Force in-the-gaps method 2
153 Failure mode for recovery testing 4
154
155For example, set debug_msgs to 5 to display General messages and Mount
156messages.
157
158
159References
160==========
161
162UBIFS documentation and FAQ/HOWTO at the MTD web site:
163http://www.linux-mtd.infradead.org/doc/ubifs.html
164http://www.linux-mtd.infradead.org/faq/ubifs.html
diff --git a/Documentation/ioctl/hdio.txt b/Documentation/ioctl/hdio.txt
index c19efdeace2c..91a6ecbae0bb 100644
--- a/Documentation/ioctl/hdio.txt
+++ b/Documentation/ioctl/hdio.txt
@@ -508,12 +508,13 @@ HDIO_DRIVE_RESET execute a device reset
508 508
509 error returns: 509 error returns:
510 EACCES Access denied: requires CAP_SYS_ADMIN 510 EACCES Access denied: requires CAP_SYS_ADMIN
511 ENXIO No such device: phy dead or ctl_addr == 0
512 EIO I/O error: reset timed out or hardware error
511 513
512 notes: 514 notes:
513 515
514 Abort any current command, prevent anything else from being 516 Execute a reset on the device as soon as the current IO
515 queued, execute a reset on the device, and issue BLKRRPART 517 operation has completed.
516 ioctl on the block device.
517 518
518 Executes an ATAPI soft reset if applicable, otherwise 519 Executes an ATAPI soft reset if applicable, otherwise
519 executes an ATA soft reset on the controller. 520 executes an ATA soft reset on the controller.
diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
index 312fe77764a4..09ad7450647b 100644
--- a/Documentation/kernel-parameters.txt
+++ b/Documentation/kernel-parameters.txt
@@ -147,10 +147,14 @@ and is between 256 and 4096 characters. It is defined in the file
147 default: 0 147 default: 0
148 148
149 acpi_sleep= [HW,ACPI] Sleep options 149 acpi_sleep= [HW,ACPI] Sleep options
150 Format: { s3_bios, s3_mode, s3_beep } 150 Format: { s3_bios, s3_mode, s3_beep, old_ordering }
151 See Documentation/power/video.txt for s3_bios and s3_mode. 151 See Documentation/power/video.txt for s3_bios and s3_mode.
152 s3_beep is for debugging; it makes the PC's speaker beep 152 s3_beep is for debugging; it makes the PC's speaker beep
153 as soon as the kernel's real-mode entry point is called. 153 as soon as the kernel's real-mode entry point is called.
154 old_ordering causes the ACPI 1.0 ordering of the _PTS
155 control method, wrt putting devices into low power
156 states, to be enforced (the ACPI 2.0 ordering of _PTS is
157 used by default).
154 158
155 acpi_sci= [HW,ACPI] ACPI System Control Interrupt trigger mode 159 acpi_sci= [HW,ACPI] ACPI System Control Interrupt trigger mode
156 Format: { level | edge | high | low } 160 Format: { level | edge | high | low }
@@ -818,7 +822,7 @@ and is between 256 and 4096 characters. It is defined in the file
818 See Documentation/ide/ide.txt. 822 See Documentation/ide/ide.txt.
819 823
820 idle= [X86] 824 idle= [X86]
821 Format: idle=poll or idle=mwait 825 Format: idle=poll or idle=mwait, idle=halt, idle=nomwait
822 Poll forces a polling idle loop that can slightly improves the performance 826 Poll forces a polling idle loop that can slightly improves the performance
823 of waking up a idle CPU, but will use a lot of power and make the system 827 of waking up a idle CPU, but will use a lot of power and make the system
824 run hot. Not recommended. 828 run hot. Not recommended.
@@ -826,6 +830,9 @@ and is between 256 and 4096 characters. It is defined in the file
826 to not use it because it doesn't save as much power as a normal idle 830 to not use it because it doesn't save as much power as a normal idle
827 loop use the MONITOR/MWAIT idle loop anyways. Performance should be the same 831 loop use the MONITOR/MWAIT idle loop anyways. Performance should be the same
828 as idle=poll. 832 as idle=poll.
833 idle=halt. Halt is forced to be used for CPU idle.
834 In such case C2/C3 won't be used again.
835 idle=nomwait. Disable mwait for CPU C-states
829 836
830 ide-pci-generic.all-generic-ide [HW] (E)IDE subsystem 837 ide-pci-generic.all-generic-ide [HW] (E)IDE subsystem
831 Claim all unknown PCI IDE storage controllers. 838 Claim all unknown PCI IDE storage controllers.
@@ -1534,6 +1541,9 @@ and is between 256 and 4096 characters. It is defined in the file
1534 Use with caution as certain devices share 1541 Use with caution as certain devices share
1535 address decoders between ROMs and other 1542 address decoders between ROMs and other
1536 resources. 1543 resources.
1544 norom [X86-32,X86_64] Do not assign address space to
1545 expansion ROMs that do not already have
1546 BIOS assigned address ranges.
1537 irqmask=0xMMMM [X86-32] Set a bit mask of IRQs allowed to be 1547 irqmask=0xMMMM [X86-32] Set a bit mask of IRQs allowed to be
1538 assigned automatically to PCI devices. You can 1548 assigned automatically to PCI devices. You can
1539 make the kernel exclude IRQs of your ISA cards 1549 make the kernel exclude IRQs of your ISA cards
diff --git a/Documentation/laptops/acer-wmi.txt b/Documentation/laptops/acer-wmi.txt
index 79b7dbd22141..69b5dd4e5a59 100644
--- a/Documentation/laptops/acer-wmi.txt
+++ b/Documentation/laptops/acer-wmi.txt
@@ -174,8 +174,6 @@ The LED is exposed through the LED subsystem, and can be found in:
174The mail LED is autodetected, so if you don't have one, the LED device won't 174The mail LED is autodetected, so if you don't have one, the LED device won't
175be registered. 175be registered.
176 176
177If you have a mail LED that is not green, please report this to me.
178
179Backlight 177Backlight
180********* 178*********
181 179
diff --git a/MAINTAINERS b/MAINTAINERS
index ee1c56a20750..93fd6b2efeee 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -216,8 +216,8 @@ W: http://code.google.com/p/aceracpi
216S: Maintained 216S: Maintained
217 217
218ACPI 218ACPI
219P: Len Brown 219P: Andi Kleen
220M: len.brown@intel.com 220M: ak@linux.intel.com
221M: lenb@kernel.org 221M: lenb@kernel.org
222L: linux-acpi@vger.kernel.org 222L: linux-acpi@vger.kernel.org
223W: http://www.lesswatts.org/projects/acpi/ 223W: http://www.lesswatts.org/projects/acpi/
@@ -239,8 +239,8 @@ W: http://www.lesswatts.org/projects/acpi/
239S: Supported 239S: Supported
240 240
241ACPI FAN DRIVER 241ACPI FAN DRIVER
242P: Len Brown 242P: Zhang Rui
243M: len.brown@intel.com 243M: rui.zhang@intel.com
244L: linux-acpi@vger.kernel.org 244L: linux-acpi@vger.kernel.org
245W: http://www.lesswatts.org/projects/acpi/ 245W: http://www.lesswatts.org/projects/acpi/
246S: Supported 246S: Supported
@@ -248,18 +248,18 @@ S: Supported
248ACPI PCI HOTPLUG DRIVER 248ACPI PCI HOTPLUG DRIVER
249P: Kristen Carlson Accardi 249P: Kristen Carlson Accardi
250M: kristen.c.accardi@intel.com 250M: kristen.c.accardi@intel.com
251L: pcihpd-discuss@lists.sourceforge.net 251L: linux-pci@vger.kernel.org
252S: Supported 252S: Supported
253 253
254ACPI THERMAL DRIVER 254ACPI THERMAL DRIVER
255P: Len Brown 255P: Zhang Rui
256M: len.brown@intel.com 256M: rui.zhang@intel.com
257L: linux-acpi@vger.kernel.org 257L: linux-acpi@vger.kernel.org
258W: http://www.lesswatts.org/projects/acpi/ 258W: http://www.lesswatts.org/projects/acpi/
259S: Supported 259S: Supported
260 260
261ACPI VIDEO DRIVER 261ACPI VIDEO DRIVER
262P: Rui Zhang 262P: Zhang Rui
263M: rui.zhang@intel.com 263M: rui.zhang@intel.com
264L: linux-acpi@vger.kernel.org 264L: linux-acpi@vger.kernel.org
265W: http://www.lesswatts.org/projects/acpi/ 265W: http://www.lesswatts.org/projects/acpi/
@@ -348,7 +348,9 @@ W: http://www.linux-usb.org/SpeedTouch/
348S: Maintained 348S: Maintained
349 349
350ALCHEMY AU1XX0 MMC DRIVER 350ALCHEMY AU1XX0 MMC DRIVER
351S: Orphan 351P: Manuel Lauss
352M: manuel.lauss@gmail.com
353S: Maintained
352 354
353ALI1563 I2C DRIVER 355ALI1563 I2C DRIVER
354P: Rudolf Marek 356P: Rudolf Marek
@@ -1143,23 +1145,28 @@ COMPACTPCI HOTPLUG CORE
1143P: Scott Murray 1145P: Scott Murray
1144M: scottm@somanetworks.com 1146M: scottm@somanetworks.com
1145M: scott@spiteful.org 1147M: scott@spiteful.org
1146L: pcihpd-discuss@lists.sourceforge.net 1148L: linux-pci@vger.kernel.org
1147S: Supported 1149S: Supported
1148 1150
1149COMPACTPCI HOTPLUG ZIATECH ZT5550 DRIVER 1151COMPACTPCI HOTPLUG ZIATECH ZT5550 DRIVER
1150P: Scott Murray 1152P: Scott Murray
1151M: scottm@somanetworks.com 1153M: scottm@somanetworks.com
1152M: scott@spiteful.org 1154M: scott@spiteful.org
1153L: pcihpd-discuss@lists.sourceforge.net 1155L: linux-pci@vger.kernel.org
1154S: Supported 1156S: Supported
1155 1157
1156COMPACTPCI HOTPLUG GENERIC DRIVER 1158COMPACTPCI HOTPLUG GENERIC DRIVER
1157P: Scott Murray 1159P: Scott Murray
1158M: scottm@somanetworks.com 1160M: scottm@somanetworks.com
1159M: scott@spiteful.org 1161M: scott@spiteful.org
1160L: pcihpd-discuss@lists.sourceforge.net 1162L: linux-pci@vger.kernel.org
1161S: Supported 1163S: Supported
1162 1164
1165COMPAL LAPTOP SUPPORT
1166P: Cezary Jackiewicz
1167M: cezary.jackiewicz@gmail.com
1168S: Maintained
1169
1163COMPUTONE INTELLIPORT MULTIPORT CARD 1170COMPUTONE INTELLIPORT MULTIPORT CARD
1164P: Michael H. Warfield 1171P: Michael H. Warfield
1165M: mhw@wittsend.com 1172M: mhw@wittsend.com
@@ -1787,6 +1794,12 @@ P: David Howells
1787M: dhowells@redhat.com 1794M: dhowells@redhat.com
1788S: Maintained 1795S: Maintained
1789 1796
1797FUJITSU LAPTOP EXTRAS
1798P: Jonathan Woithe
1799M: jwoithe@physics.adelaide.edu.au
1800L: linux-acpi@vger.kernel.org
1801S: Maintained
1802
1790FUSE: FILESYSTEM IN USERSPACE 1803FUSE: FILESYSTEM IN USERSPACE
1791P: Miklos Szeredi 1804P: Miklos Szeredi
1792M: miklos@szeredi.hu 1805M: miklos@szeredi.hu
@@ -2325,6 +2338,16 @@ L: linux-mtd@lists.infradead.org
2325W: http://www.linux-mtd.infradead.org/doc/jffs2.html 2338W: http://www.linux-mtd.infradead.org/doc/jffs2.html
2326S: Maintained 2339S: Maintained
2327 2340
2341UBI FILE SYSTEM (UBIFS)
2342P: Artem Bityutskiy
2343M: dedekind@infradead.org
2344P: Adrian Hunter
2345M: ext-adrian.hunter@nokia.com
2346L: linux-mtd@lists.infradead.org
2347T: git git://git.infradead.org/~dedekind/ubifs-2.6.git
2348W: http://www.linux-mtd.infradead.org/doc/ubifs.html
2349S: Maintained
2350
2328JFS FILESYSTEM 2351JFS FILESYSTEM
2329P: Dave Kleikamp 2352P: Dave Kleikamp
2330M: shaggy@austin.ibm.com 2353M: shaggy@austin.ibm.com
@@ -3196,7 +3219,7 @@ S: Supported
3196PCIE HOTPLUG DRIVER 3219PCIE HOTPLUG DRIVER
3197P: Kristen Carlson Accardi 3220P: Kristen Carlson Accardi
3198M: kristen.c.accardi@intel.com 3221M: kristen.c.accardi@intel.com
3199L: pcihpd-discuss@lists.sourceforge.net 3222L: linux-pci@vger.kernel.org
3200S: Supported 3223S: Supported
3201 3224
3202PCMCIA SUBSYSTEM 3225PCMCIA SUBSYSTEM
@@ -3538,6 +3561,13 @@ L: linux-s390@vger.kernel.org
3538W: http://www.ibm.com/developerworks/linux/linux390/ 3561W: http://www.ibm.com/developerworks/linux/linux390/
3539S: Supported 3562S: Supported
3540 3563
3564S3C24XX SD/MMC Driver
3565P: Ben Dooks
3566M: ben-linux@fluff.org
3567L: linux-arm-kernel@lists.arm.linux.org.uk (subscribers-only)
3568L: linux-kernel@vger.kernel.org
3569S: Supported
3570
3541SAA7146 VIDEO4LINUX-2 DRIVER 3571SAA7146 VIDEO4LINUX-2 DRIVER
3542P: Michael Hunold 3572P: Michael Hunold
3543M: michael@mihu.de 3573M: michael@mihu.de
@@ -3610,6 +3640,12 @@ P: Jim Cromie
3610M: jim.cromie@gmail.com 3640M: jim.cromie@gmail.com
3611S: Maintained 3641S: Maintained
3612 3642
3643SDRICOH_CS MMC/SD HOST CONTROLLER INTERFACE DRIVER
3644P: Sascha Sommer
3645M: saschasommer@freenet.de
3646L: sdricohcs-devel@lists.sourceforge.net (subscribers-only)
3647S: Maintained
3648
3613SECURITY CONTACT 3649SECURITY CONTACT
3614P: Security Officers 3650P: Security Officers
3615M: security@kernel.org 3651M: security@kernel.org
@@ -3829,7 +3865,7 @@ S: Maintained
3829SHPC HOTPLUG DRIVER 3865SHPC HOTPLUG DRIVER
3830P: Kristen Carlson Accardi 3866P: Kristen Carlson Accardi
3831M: kristen.c.accardi@intel.com 3867M: kristen.c.accardi@intel.com
3832L: pcihpd-discuss@lists.sourceforge.net 3868L: linux-pci@vger.kernel.org
3833S: Supported 3869S: Supported
3834 3870
3835SECURE DIGITAL HOST CONTROLLER INTERFACE DRIVER 3871SECURE DIGITAL HOST CONTROLLER INTERFACE DRIVER
diff --git a/Makefile b/Makefile
index 1564577bdc53..6192922de9c0 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,3 @@
1FRED=42
2VERSION = 2 1VERSION = 2
3PATCHLEVEL = 6 2PATCHLEVEL = 6
4SUBLEVEL = 26 3SUBLEVEL = 26
diff --git a/arch/avr32/boards/atngw100/setup.c b/arch/avr32/boards/atngw100/setup.c
index a51bb9fb3c89..c7fe94d03a1e 100644
--- a/arch/avr32/boards/atngw100/setup.c
+++ b/arch/avr32/boards/atngw100/setup.c
@@ -19,6 +19,7 @@
19#include <linux/leds.h> 19#include <linux/leds.h>
20#include <linux/spi/spi.h> 20#include <linux/spi/spi.h>
21 21
22#include <asm/atmel-mci.h>
22#include <asm/io.h> 23#include <asm/io.h>
23#include <asm/setup.h> 24#include <asm/setup.h>
24 25
@@ -51,6 +52,11 @@ static struct spi_board_info spi0_board_info[] __initdata = {
51 }, 52 },
52}; 53};
53 54
55static struct mci_platform_data __initdata mci0_data = {
56 .detect_pin = GPIO_PIN_PC(25),
57 .wp_pin = GPIO_PIN_PE(0),
58};
59
54/* 60/*
55 * The next two functions should go away as the boot loader is 61 * The next two functions should go away as the boot loader is
56 * supposed to initialize the macb address registers with a valid 62 * supposed to initialize the macb address registers with a valid
@@ -170,6 +176,7 @@ static int __init atngw100_init(void)
170 set_hw_addr(at32_add_device_eth(1, &eth_data[1])); 176 set_hw_addr(at32_add_device_eth(1, &eth_data[1]));
171 177
172 at32_add_device_spi(0, spi0_board_info, ARRAY_SIZE(spi0_board_info)); 178 at32_add_device_spi(0, spi0_board_info, ARRAY_SIZE(spi0_board_info));
179 at32_add_device_mci(0, &mci0_data);
173 at32_add_device_usba(0, NULL); 180 at32_add_device_usba(0, NULL);
174 181
175 for (i = 0; i < ARRAY_SIZE(ngw_leds); i++) { 182 for (i = 0; i < ARRAY_SIZE(ngw_leds); i++) {
diff --git a/arch/avr32/boards/atstk1000/atstk1002.c b/arch/avr32/boards/atstk1000/atstk1002.c
index 86b363c1c25b..e11659b732fa 100644
--- a/arch/avr32/boards/atstk1000/atstk1002.c
+++ b/arch/avr32/boards/atstk1000/atstk1002.c
@@ -234,6 +234,9 @@ static int __init atstk1002_init(void)
234#ifdef CONFIG_BOARD_ATSTK100X_SPI1 234#ifdef CONFIG_BOARD_ATSTK100X_SPI1
235 at32_add_device_spi(1, spi1_board_info, ARRAY_SIZE(spi1_board_info)); 235 at32_add_device_spi(1, spi1_board_info, ARRAY_SIZE(spi1_board_info));
236#endif 236#endif
237#ifndef CONFIG_BOARD_ATSTK1002_SW2_CUSTOM
238 at32_add_device_mci(0, NULL);
239#endif
237#ifdef CONFIG_BOARD_ATSTK1002_SW5_CUSTOM 240#ifdef CONFIG_BOARD_ATSTK1002_SW5_CUSTOM
238 set_hw_addr(at32_add_device_eth(1, &eth_data[1])); 241 set_hw_addr(at32_add_device_eth(1, &eth_data[1]));
239#else 242#else
diff --git a/arch/avr32/mach-at32ap/at32ap700x.c b/arch/avr32/mach-at32ap/at32ap700x.c
index 07b21b121eef..021d51217184 100644
--- a/arch/avr32/mach-at32ap/at32ap700x.c
+++ b/arch/avr32/mach-at32ap/at32ap700x.c
@@ -14,6 +14,7 @@
14#include <linux/spi/spi.h> 14#include <linux/spi/spi.h>
15#include <linux/usb/atmel_usba_udc.h> 15#include <linux/usb/atmel_usba_udc.h>
16 16
17#include <asm/atmel-mci.h>
17#include <asm/io.h> 18#include <asm/io.h>
18#include <asm/irq.h> 19#include <asm/irq.h>
19 20
@@ -1278,20 +1279,32 @@ static struct clk atmel_mci0_pclk = {
1278 .index = 9, 1279 .index = 9,
1279}; 1280};
1280 1281
1281struct platform_device *__init at32_add_device_mci(unsigned int id) 1282struct platform_device *__init
1283at32_add_device_mci(unsigned int id, struct mci_platform_data *data)
1282{ 1284{
1283 struct platform_device *pdev; 1285 struct mci_platform_data _data;
1286 struct platform_device *pdev;
1287 struct dw_dma_slave *dws;
1284 1288
1285 if (id != 0) 1289 if (id != 0)
1286 return NULL; 1290 return NULL;
1287 1291
1288 pdev = platform_device_alloc("atmel_mci", id); 1292 pdev = platform_device_alloc("atmel_mci", id);
1289 if (!pdev) 1293 if (!pdev)
1290 return NULL; 1294 goto fail;
1291 1295
1292 if (platform_device_add_resources(pdev, atmel_mci0_resource, 1296 if (platform_device_add_resources(pdev, atmel_mci0_resource,
1293 ARRAY_SIZE(atmel_mci0_resource))) 1297 ARRAY_SIZE(atmel_mci0_resource)))
1294 goto err_add_resources; 1298 goto fail;
1299
1300 if (!data) {
1301 data = &_data;
1302 memset(data, 0, sizeof(struct mci_platform_data));
1303 }
1304
1305 if (platform_device_add_data(pdev, data,
1306 sizeof(struct mci_platform_data)))
1307 goto fail;
1295 1308
1296 select_peripheral(PA(10), PERIPH_A, 0); /* CLK */ 1309 select_peripheral(PA(10), PERIPH_A, 0); /* CLK */
1297 select_peripheral(PA(11), PERIPH_A, 0); /* CMD */ 1310 select_peripheral(PA(11), PERIPH_A, 0); /* CMD */
@@ -1300,12 +1313,19 @@ struct platform_device *__init at32_add_device_mci(unsigned int id)
1300 select_peripheral(PA(14), PERIPH_A, 0); /* DATA2 */ 1313 select_peripheral(PA(14), PERIPH_A, 0); /* DATA2 */
1301 select_peripheral(PA(15), PERIPH_A, 0); /* DATA3 */ 1314 select_peripheral(PA(15), PERIPH_A, 0); /* DATA3 */
1302 1315
1316 if (data) {
1317 if (data->detect_pin != GPIO_PIN_NONE)
1318 at32_select_gpio(data->detect_pin, 0);
1319 if (data->wp_pin != GPIO_PIN_NONE)
1320 at32_select_gpio(data->wp_pin, 0);
1321 }
1322
1303 atmel_mci0_pclk.dev = &pdev->dev; 1323 atmel_mci0_pclk.dev = &pdev->dev;
1304 1324
1305 platform_device_add(pdev); 1325 platform_device_add(pdev);
1306 return pdev; 1326 return pdev;
1307 1327
1308err_add_resources: 1328fail:
1309 platform_device_put(pdev); 1329 platform_device_put(pdev);
1310 return NULL; 1330 return NULL;
1311} 1331}
diff --git a/arch/frv/mb93090-mb00/pci-frv.c b/arch/frv/mb93090-mb00/pci-frv.c
index 4f165c93be42..edae117fcc2b 100644
--- a/arch/frv/mb93090-mb00/pci-frv.c
+++ b/arch/frv/mb93090-mb00/pci-frv.c
@@ -19,36 +19,6 @@
19 19
20#include "pci-frv.h" 20#include "pci-frv.h"
21 21
22#if 0
23void
24pcibios_update_resource(struct pci_dev *dev, struct resource *root,
25 struct resource *res, int resource)
26{
27 u32 new, check;
28 int reg;
29
30 new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
31 if (resource < 6) {
32 reg = PCI_BASE_ADDRESS_0 + 4*resource;
33 } else if (resource == PCI_ROM_RESOURCE) {
34 res->flags |= IORESOURCE_ROM_ENABLE;
35 new |= PCI_ROM_ADDRESS_ENABLE;
36 reg = dev->rom_base_reg;
37 } else {
38 /* Somebody might have asked allocation of a non-standard resource */
39 return;
40 }
41
42 pci_write_config_dword(dev, reg, new);
43 pci_read_config_dword(dev, reg, &check);
44 if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
45 printk(KERN_ERR "PCI: Error while updating region "
46 "%s/%d (%08x != %08x)\n", pci_name(dev), resource,
47 new, check);
48 }
49}
50#endif
51
52/* 22/*
53 * We need to avoid collisions with `mirrored' VGA ports 23 * We need to avoid collisions with `mirrored' VGA ports
54 * and other strange ISA hardware, so we always want the 24 * and other strange ISA hardware, so we always want the
diff --git a/arch/ia64/kernel/process.c b/arch/ia64/kernel/process.c
index fabaf08d9a69..3ab8373103ec 100644
--- a/arch/ia64/kernel/process.c
+++ b/arch/ia64/kernel/process.c
@@ -55,6 +55,10 @@ void (*ia64_mark_idle)(int);
55 55
56unsigned long boot_option_idle_override = 0; 56unsigned long boot_option_idle_override = 0;
57EXPORT_SYMBOL(boot_option_idle_override); 57EXPORT_SYMBOL(boot_option_idle_override);
58unsigned long idle_halt;
59EXPORT_SYMBOL(idle_halt);
60unsigned long idle_nomwait;
61EXPORT_SYMBOL(idle_nomwait);
58 62
59void 63void
60ia64_do_show_stack (struct unw_frame_info *info, void *arg) 64ia64_do_show_stack (struct unw_frame_info *info, void *arg)
diff --git a/arch/ia64/kvm/kvm-ia64.c b/arch/ia64/kvm/kvm-ia64.c
index 318b81100623..68c978be9a51 100644
--- a/arch/ia64/kvm/kvm-ia64.c
+++ b/arch/ia64/kvm/kvm-ia64.c
@@ -395,7 +395,7 @@ static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
395 if (kvm->vcpus[i]->cpu != -1) { 395 if (kvm->vcpus[i]->cpu != -1) {
396 call_data.vcpu = kvm->vcpus[i]; 396 call_data.vcpu = kvm->vcpus[i];
397 smp_call_function_single(kvm->vcpus[i]->cpu, 397 smp_call_function_single(kvm->vcpus[i]->cpu,
398 vcpu_global_purge, &call_data, 0, 1); 398 vcpu_global_purge, &call_data, 1);
399 } else 399 } else
400 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n"); 400 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
401 401
@@ -1693,7 +1693,7 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1693 wake_up_interruptible(&vcpu->wq); 1693 wake_up_interruptible(&vcpu->wq);
1694 1694
1695 if (vcpu->guest_mode) 1695 if (vcpu->guest_mode)
1696 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0, 0); 1696 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
1697} 1697}
1698 1698
1699int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig) 1699int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
diff --git a/arch/ia64/kvm/kvm_fw.c b/arch/ia64/kvm/kvm_fw.c
index 091f936c4485..0c69d9ec92d4 100644
--- a/arch/ia64/kvm/kvm_fw.c
+++ b/arch/ia64/kvm/kvm_fw.c
@@ -130,7 +130,7 @@ static struct ia64_pal_retval pal_cache_flush(struct kvm_vcpu *vcpu)
130 args.cache_type = gr29; 130 args.cache_type = gr29;
131 args.operation = gr30; 131 args.operation = gr30;
132 smp_call_function(remote_pal_cache_flush, 132 smp_call_function(remote_pal_cache_flush,
133 (void *)&args, 1, 1); 133 (void *)&args, 1);
134 if (args.status != 0) 134 if (args.status != 0)
135 printk(KERN_ERR"pal_cache_flush error!," 135 printk(KERN_ERR"pal_cache_flush error!,"
136 "status:0x%lx\n", args.status); 136 "status:0x%lx\n", args.status);
diff --git a/arch/m68knommu/kernel/comempci.c b/arch/m68knommu/kernel/comempci.c
index 6ee00effbad2..0a68b5a85f86 100644
--- a/arch/m68knommu/kernel/comempci.c
+++ b/arch/m68knommu/kernel/comempci.c
@@ -375,15 +375,6 @@ int pcibios_enable_device(struct pci_dev *dev, int mask)
375 375
376/*****************************************************************************/ 376/*****************************************************************************/
377 377
378void pcibios_update_resource(struct pci_dev *dev, struct resource *root, struct resource *r, int resource)
379{
380 printk(KERN_WARNING "%s(%d): no support for changing PCI resources...\n",
381 __FILE__, __LINE__);
382}
383
384
385/*****************************************************************************/
386
387/* 378/*
388 * Local routines to interrcept the standard I/O and vector handling 379 * Local routines to interrcept the standard I/O and vector handling
389 * code. Don't include this 'till now - initialization code above needs 380 * code. Don't include this 'till now - initialization code above needs
diff --git a/arch/mips/pmc-sierra/yosemite/ht.c b/arch/mips/pmc-sierra/yosemite/ht.c
index 6380662bbf3c..678388fd34b1 100644
--- a/arch/mips/pmc-sierra/yosemite/ht.c
+++ b/arch/mips/pmc-sierra/yosemite/ht.c
@@ -345,42 +345,6 @@ int pcibios_enable_device(struct pci_dev *dev, int mask)
345 return pcibios_enable_resources(dev); 345 return pcibios_enable_resources(dev);
346} 346}
347 347
348
349
350void pcibios_update_resource(struct pci_dev *dev, struct resource *root,
351 struct resource *res, int resource)
352{
353 u32 new, check;
354 int reg;
355
356 return;
357
358 new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
359 if (resource < 6) {
360 reg = PCI_BASE_ADDRESS_0 + 4 * resource;
361 } else if (resource == PCI_ROM_RESOURCE) {
362 res->flags |= IORESOURCE_ROM_ENABLE;
363 reg = dev->rom_base_reg;
364 } else {
365 /*
366 * Somebody might have asked allocation of a non-standard
367 * resource
368 */
369 return;
370 }
371
372 pci_write_config_dword(dev, reg, new);
373 pci_read_config_dword(dev, reg, &check);
374 if ((new ^ check) &
375 ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK :
376 PCI_BASE_ADDRESS_MEM_MASK)) {
377 printk(KERN_ERR "PCI: Error while updating region "
378 "%s/%d (%08x != %08x)\n", pci_name(dev), resource,
379 new, check);
380 }
381}
382
383
384void pcibios_align_resource(void *data, struct resource *res, 348void pcibios_align_resource(void *data, struct resource *res,
385 resource_size_t size, resource_size_t align) 349 resource_size_t size, resource_size_t align)
386{ 350{
diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig
index 5dc8f8028d52..eb530b4128ba 100644
--- a/arch/s390/Kconfig
+++ b/arch/s390/Kconfig
@@ -289,7 +289,7 @@ config WARN_STACK_SIZE
289 int "Maximum frame size considered safe (128-2048)" 289 int "Maximum frame size considered safe (128-2048)"
290 range 128 2048 290 range 128 2048
291 depends on WARN_STACK 291 depends on WARN_STACK
292 default "256" 292 default "2048"
293 help 293 help
294 This allows you to specify the maximum frame size a function may 294 This allows you to specify the maximum frame size a function may
295 have without the compiler complaining about it. 295 have without the compiler complaining about it.
diff --git a/arch/s390/kernel/stacktrace.c b/arch/s390/kernel/stacktrace.c
index 57571f10270c..8841919ef7e6 100644
--- a/arch/s390/kernel/stacktrace.c
+++ b/arch/s390/kernel/stacktrace.c
@@ -10,6 +10,7 @@
10#include <linux/sched.h> 10#include <linux/sched.h>
11#include <linux/stacktrace.h> 11#include <linux/stacktrace.h>
12#include <linux/kallsyms.h> 12#include <linux/kallsyms.h>
13#include <linux/module.h>
13 14
14static unsigned long save_context_stack(struct stack_trace *trace, 15static unsigned long save_context_stack(struct stack_trace *trace,
15 unsigned long sp, 16 unsigned long sp,
diff --git a/arch/sh/drivers/pci/pci.c b/arch/sh/drivers/pci/pci.c
index 08d2e7325252..f57095a2617c 100644
--- a/arch/sh/drivers/pci/pci.c
+++ b/arch/sh/drivers/pci/pci.c
@@ -76,38 +76,6 @@ void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus)
76 pci_read_bridge_bases(bus); 76 pci_read_bridge_bases(bus);
77} 77}
78 78
79void
80pcibios_update_resource(struct pci_dev *dev, struct resource *root,
81 struct resource *res, int resource)
82{
83 u32 new, check;
84 int reg;
85
86 new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
87 if (resource < 6) {
88 reg = PCI_BASE_ADDRESS_0 + 4*resource;
89 } else if (resource == PCI_ROM_RESOURCE) {
90 res->flags |= IORESOURCE_ROM_ENABLE;
91 new |= PCI_ROM_ADDRESS_ENABLE;
92 reg = dev->rom_base_reg;
93 } else {
94 /*
95 * Somebody might have asked allocation of a non-standard
96 * resource
97 */
98 return;
99 }
100
101 pci_write_config_dword(dev, reg, new);
102 pci_read_config_dword(dev, reg, &check);
103 if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ?
104 PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
105 printk(KERN_ERR "PCI: Error while updating region "
106 "%s/%d (%08x != %08x)\n", pci_name(dev), resource,
107 new, check);
108 }
109}
110
111void pcibios_align_resource(void *data, struct resource *res, 79void pcibios_align_resource(void *data, struct resource *res,
112 resource_size_t size, resource_size_t align) 80 resource_size_t size, resource_size_t align)
113 __attribute__ ((weak)); 81 __attribute__ ((weak));
diff --git a/arch/sparc64/kernel/pci.c b/arch/sparc64/kernel/pci.c
index 112b09f16f36..d00a3656c287 100644
--- a/arch/sparc64/kernel/pci.c
+++ b/arch/sparc64/kernel/pci.c
@@ -408,7 +408,7 @@ struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
408 dev->class = class >> 8; 408 dev->class = class >> 8;
409 dev->revision = class & 0xff; 409 dev->revision = class & 0xff;
410 410
411 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus), 411 sprintf(dev->dev.bus_id, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
412 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn)); 412 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
413 413
414 if (ofpci_verbose) 414 if (ofpci_verbose)
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 5112c84f5421..da140611bb57 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -8,8 +8,7 @@ CPPFLAGS_vmlinux.lds += -U$(UTS_MACHINE)
8 8
9ifdef CONFIG_FTRACE 9ifdef CONFIG_FTRACE
10# Do not profile debug utilities 10# Do not profile debug utilities
11CFLAGS_REMOVE_tsc_64.o = -pg 11CFLAGS_REMOVE_tsc.o = -pg
12CFLAGS_REMOVE_tsc_32.o = -pg
13CFLAGS_REMOVE_rtc.o = -pg 12CFLAGS_REMOVE_rtc.o = -pg
14endif 13endif
15 14
diff --git a/arch/x86/kernel/acpi/processor.c b/arch/x86/kernel/acpi/processor.c
index de2d2e4ebad9..7c074eec39fb 100644
--- a/arch/x86/kernel/acpi/processor.c
+++ b/arch/x86/kernel/acpi/processor.c
@@ -56,6 +56,12 @@ static void init_intel_pdc(struct acpi_processor *pr, struct cpuinfo_x86 *c)
56 if (cpu_has(c, X86_FEATURE_ACPI)) 56 if (cpu_has(c, X86_FEATURE_ACPI))
57 buf[2] |= ACPI_PDC_T_FFH; 57 buf[2] |= ACPI_PDC_T_FFH;
58 58
59 /*
60 * If mwait/monitor is unsupported, C2/C3_FFH will be disabled
61 */
62 if (!cpu_has(c, X86_FEATURE_MWAIT))
63 buf[2] &= ~(ACPI_PDC_C_C2C3_FFH);
64
59 obj->type = ACPI_TYPE_BUFFER; 65 obj->type = ACPI_TYPE_BUFFER;
60 obj->buffer.length = 12; 66 obj->buffer.length = 12;
61 obj->buffer.pointer = (u8 *) buf; 67 obj->buffer.pointer = (u8 *) buf;
diff --git a/arch/x86/kernel/acpi/sleep.c b/arch/x86/kernel/acpi/sleep.c
index 793ad2045f58..868de3d5c39d 100644
--- a/arch/x86/kernel/acpi/sleep.c
+++ b/arch/x86/kernel/acpi/sleep.c
@@ -158,6 +158,8 @@ static int __init acpi_sleep_setup(char *str)
158 acpi_realmode_flags |= 2; 158 acpi_realmode_flags |= 2;
159 if (strncmp(str, "s3_beep", 7) == 0) 159 if (strncmp(str, "s3_beep", 7) == 0)
160 acpi_realmode_flags |= 4; 160 acpi_realmode_flags |= 4;
161 if (strncmp(str, "old_ordering", 12) == 0)
162 acpi_old_suspend_ordering();
161 str = strchr(str, ','); 163 str = strchr(str, ',');
162 if (str != NULL) 164 if (str != NULL)
163 str += strspn(str, ", \t"); 165 str += strspn(str, ", \t");
diff --git a/arch/x86/kernel/apm_32.c b/arch/x86/kernel/apm_32.c
index 75cb5da4ea0a..bf9b441331e9 100644
--- a/arch/x86/kernel/apm_32.c
+++ b/arch/x86/kernel/apm_32.c
@@ -1213,9 +1213,9 @@ static int suspend(int vetoable)
1213 if (err != APM_SUCCESS) 1213 if (err != APM_SUCCESS)
1214 apm_error("suspend", err); 1214 apm_error("suspend", err);
1215 err = (err == APM_SUCCESS) ? 0 : -EIO; 1215 err = (err == APM_SUCCESS) ? 0 : -EIO;
1216 device_power_up(); 1216 device_power_up(PMSG_RESUME);
1217 local_irq_enable(); 1217 local_irq_enable();
1218 device_resume(); 1218 device_resume(PMSG_RESUME);
1219 queue_event(APM_NORMAL_RESUME, NULL); 1219 queue_event(APM_NORMAL_RESUME, NULL);
1220 spin_lock(&user_list_lock); 1220 spin_lock(&user_list_lock);
1221 for (as = user_list; as != NULL; as = as->next) { 1221 for (as = user_list; as != NULL; as = as->next) {
@@ -1240,7 +1240,7 @@ static void standby(void)
1240 apm_error("standby", err); 1240 apm_error("standby", err);
1241 1241
1242 local_irq_disable(); 1242 local_irq_disable();
1243 device_power_up(); 1243 device_power_up(PMSG_RESUME);
1244 local_irq_enable(); 1244 local_irq_enable();
1245} 1245}
1246 1246
@@ -1326,7 +1326,7 @@ static void check_events(void)
1326 ignore_bounce = 1; 1326 ignore_bounce = 1;
1327 if ((event != APM_NORMAL_RESUME) 1327 if ((event != APM_NORMAL_RESUME)
1328 || (ignore_normal_resume == 0)) { 1328 || (ignore_normal_resume == 0)) {
1329 device_resume(); 1329 device_resume(PMSG_RESUME);
1330 queue_event(event, NULL); 1330 queue_event(event, NULL);
1331 } 1331 }
1332 ignore_normal_resume = 0; 1332 ignore_normal_resume = 0;
diff --git a/arch/x86/kernel/early-quirks.c b/arch/x86/kernel/early-quirks.c
index a4665f37cfc5..a0e11c0cc872 100644
--- a/arch/x86/kernel/early-quirks.c
+++ b/arch/x86/kernel/early-quirks.c
@@ -120,7 +120,18 @@ static struct chipset early_qrk[] __initdata = {
120 {} 120 {}
121}; 121};
122 122
123static void __init check_dev_quirk(int num, int slot, int func) 123/**
124 * check_dev_quirk - apply early quirks to a given PCI device
125 * @num: bus number
126 * @slot: slot number
127 * @func: PCI function
128 *
129 * Check the vendor & device ID against the early quirks table.
130 *
131 * If the device is single function, let early_quirks() know so we don't
132 * poke at this device again.
133 */
134static int __init check_dev_quirk(int num, int slot, int func)
124{ 135{
125 u16 class; 136 u16 class;
126 u16 vendor; 137 u16 vendor;
@@ -131,7 +142,7 @@ static void __init check_dev_quirk(int num, int slot, int func)
131 class = read_pci_config_16(num, slot, func, PCI_CLASS_DEVICE); 142 class = read_pci_config_16(num, slot, func, PCI_CLASS_DEVICE);
132 143
133 if (class == 0xffff) 144 if (class == 0xffff)
134 return; 145 return -1; /* no class, treat as single function */
135 146
136 vendor = read_pci_config_16(num, slot, func, PCI_VENDOR_ID); 147 vendor = read_pci_config_16(num, slot, func, PCI_VENDOR_ID);
137 148
@@ -154,7 +165,9 @@ static void __init check_dev_quirk(int num, int slot, int func)
154 type = read_pci_config_byte(num, slot, func, 165 type = read_pci_config_byte(num, slot, func,
155 PCI_HEADER_TYPE); 166 PCI_HEADER_TYPE);
156 if (!(type & 0x80)) 167 if (!(type & 0x80))
157 return; 168 return -1;
169
170 return 0;
158} 171}
159 172
160void __init early_quirks(void) 173void __init early_quirks(void)
@@ -167,6 +180,9 @@ void __init early_quirks(void)
167 /* Poor man's PCI discovery */ 180 /* Poor man's PCI discovery */
168 for (num = 0; num < 32; num++) 181 for (num = 0; num < 32; num++)
169 for (slot = 0; slot < 32; slot++) 182 for (slot = 0; slot < 32; slot++)
170 for (func = 0; func < 8; func++) 183 for (func = 0; func < 8; func++) {
171 check_dev_quirk(num, slot, func); 184 /* Only probe function 0 on single fn devices */
185 if (check_dev_quirk(num, slot, func))
186 break;
187 }
172} 188}
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 7dceea947232..4d629c62f4f8 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -7,6 +7,12 @@
7#include <linux/module.h> 7#include <linux/module.h>
8#include <linux/pm.h> 8#include <linux/pm.h>
9#include <linux/clockchips.h> 9#include <linux/clockchips.h>
10#include <asm/system.h>
11
12unsigned long idle_halt;
13EXPORT_SYMBOL(idle_halt);
14unsigned long idle_nomwait;
15EXPORT_SYMBOL(idle_nomwait);
10 16
11struct kmem_cache *task_xstate_cachep; 17struct kmem_cache *task_xstate_cachep;
12 18
@@ -325,7 +331,27 @@ static int __init idle_setup(char *str)
325 pm_idle = poll_idle; 331 pm_idle = poll_idle;
326 } else if (!strcmp(str, "mwait")) 332 } else if (!strcmp(str, "mwait"))
327 force_mwait = 1; 333 force_mwait = 1;
328 else 334 else if (!strcmp(str, "halt")) {
335 /*
336 * When the boot option of idle=halt is added, halt is
337 * forced to be used for CPU idle. In such case CPU C2/C3
338 * won't be used again.
339 * To continue to load the CPU idle driver, don't touch
340 * the boot_option_idle_override.
341 */
342 pm_idle = default_idle;
343 idle_halt = 1;
344 return 0;
345 } else if (!strcmp(str, "nomwait")) {
346 /*
347 * If the boot option of "idle=nomwait" is added,
348 * it means that mwait will be disabled for CPU C2/C3
349 * states. In such case it won't touch the variable
350 * of boot_option_idle_override.
351 */
352 idle_nomwait = 1;
353 return 0;
354 } else
329 return -1; 355 return -1;
330 356
331 boot_option_idle_override = 1; 357 boot_option_idle_override = 1;
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 36c540d4ac4b..531b55b8e81a 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -684,6 +684,11 @@ void __init setup_arch(char **cmdline_p)
684 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC); 684 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
685 } 685 }
686 686
687#ifdef CONFIG_PCI
688 if (pci_early_dump_regs)
689 early_dump_pci_devices();
690#endif
691
687 finish_e820_parsing(); 692 finish_e820_parsing();
688 693
689#ifdef CONFIG_X86_32 694#ifdef CONFIG_X86_32
diff --git a/arch/x86/mm/srat_32.c b/arch/x86/mm/srat_32.c
index f41d67f8f831..1eb2973a301c 100644
--- a/arch/x86/mm/srat_32.c
+++ b/arch/x86/mm/srat_32.c
@@ -156,10 +156,9 @@ acpi_numa_memory_affinity_init(struct acpi_srat_mem_affinity *memory_affinity)
156 156
157 num_memory_chunks++; 157 num_memory_chunks++;
158 158
159 printk(KERN_DEBUG "Memory range %08lx to %08lx (type %x)" 159 printk(KERN_DEBUG "Memory range %08lx to %08lx"
160 " in proximity domain %02x %s\n", 160 " in proximity domain %02x %s\n",
161 start_pfn, end_pfn, 161 start_pfn, end_pfn,
162 memory_affinity->memory_type,
163 pxm, 162 pxm,
164 ((memory_affinity->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) ? 163 ((memory_affinity->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) ?
165 "enabled and removable" : "enabled" ) ); 164 "enabled and removable" : "enabled" ) );
diff --git a/arch/x86/pci/common.c b/arch/x86/pci/common.c
index 20b9f59f95df..b67732bbb85a 100644
--- a/arch/x86/pci/common.c
+++ b/arch/x86/pci/common.c
@@ -20,6 +20,7 @@
20unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 | 20unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |
21 PCI_PROBE_MMCONF; 21 PCI_PROBE_MMCONF;
22 22
23unsigned int pci_early_dump_regs;
23static int pci_bf_sort; 24static int pci_bf_sort;
24int pci_routeirq; 25int pci_routeirq;
25int pcibios_last_bus = -1; 26int pcibios_last_bus = -1;
@@ -31,7 +32,7 @@ struct pci_raw_ops *raw_pci_ext_ops;
31int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn, 32int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
32 int reg, int len, u32 *val) 33 int reg, int len, u32 *val)
33{ 34{
34 if (reg < 256 && raw_pci_ops) 35 if (domain == 0 && reg < 256 && raw_pci_ops)
35 return raw_pci_ops->read(domain, bus, devfn, reg, len, val); 36 return raw_pci_ops->read(domain, bus, devfn, reg, len, val);
36 if (raw_pci_ext_ops) 37 if (raw_pci_ext_ops)
37 return raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val); 38 return raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val);
@@ -41,7 +42,7 @@ int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
41int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn, 42int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
42 int reg, int len, u32 val) 43 int reg, int len, u32 val)
43{ 44{
44 if (reg < 256 && raw_pci_ops) 45 if (domain == 0 && reg < 256 && raw_pci_ops)
45 return raw_pci_ops->write(domain, bus, devfn, reg, len, val); 46 return raw_pci_ops->write(domain, bus, devfn, reg, len, val);
46 if (raw_pci_ext_ops) 47 if (raw_pci_ext_ops)
47 return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val); 48 return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);
@@ -121,6 +122,21 @@ void __init dmi_check_skip_isa_align(void)
121 dmi_check_system(can_skip_pciprobe_dmi_table); 122 dmi_check_system(can_skip_pciprobe_dmi_table);
122} 123}
123 124
125static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
126{
127 struct resource *rom_r = &dev->resource[PCI_ROM_RESOURCE];
128
129 if (pci_probe & PCI_NOASSIGN_ROMS) {
130 if (rom_r->parent)
131 return;
132 if (rom_r->start) {
133 /* we deal with BIOS assigned ROM later */
134 return;
135 }
136 rom_r->start = rom_r->end = rom_r->flags = 0;
137 }
138}
139
124/* 140/*
125 * Called after each bus is probed, but before its children 141 * Called after each bus is probed, but before its children
126 * are examined. 142 * are examined.
@@ -128,7 +144,11 @@ void __init dmi_check_skip_isa_align(void)
128 144
129void __devinit pcibios_fixup_bus(struct pci_bus *b) 145void __devinit pcibios_fixup_bus(struct pci_bus *b)
130{ 146{
147 struct pci_dev *dev;
148
131 pci_read_bridge_bases(b); 149 pci_read_bridge_bases(b);
150 list_for_each_entry(dev, &b->devices, bus_list)
151 pcibios_fixup_device_resources(dev);
132} 152}
133 153
134/* 154/*
@@ -481,12 +501,18 @@ char * __devinit pcibios_setup(char *str)
481 else if (!strcmp(str, "rom")) { 501 else if (!strcmp(str, "rom")) {
482 pci_probe |= PCI_ASSIGN_ROMS; 502 pci_probe |= PCI_ASSIGN_ROMS;
483 return NULL; 503 return NULL;
504 } else if (!strcmp(str, "norom")) {
505 pci_probe |= PCI_NOASSIGN_ROMS;
506 return NULL;
484 } else if (!strcmp(str, "assign-busses")) { 507 } else if (!strcmp(str, "assign-busses")) {
485 pci_probe |= PCI_ASSIGN_ALL_BUSSES; 508 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
486 return NULL; 509 return NULL;
487 } else if (!strcmp(str, "use_crs")) { 510 } else if (!strcmp(str, "use_crs")) {
488 pci_probe |= PCI_USE__CRS; 511 pci_probe |= PCI_USE__CRS;
489 return NULL; 512 return NULL;
513 } else if (!strcmp(str, "earlydump")) {
514 pci_early_dump_regs = 1;
515 return NULL;
490 } else if (!strcmp(str, "routeirq")) { 516 } else if (!strcmp(str, "routeirq")) {
491 pci_routeirq = 1; 517 pci_routeirq = 1;
492 return NULL; 518 return NULL;
diff --git a/arch/x86/pci/early.c b/arch/x86/pci/early.c
index 42df4b6606df..858dbe3399f9 100644
--- a/arch/x86/pci/early.c
+++ b/arch/x86/pci/early.c
@@ -49,7 +49,14 @@ void write_pci_config_byte(u8 bus, u8 slot, u8 func, u8 offset, u8 val)
49{ 49{
50 PDprintk("%x writing to %x: %x\n", slot, offset, val); 50 PDprintk("%x writing to %x: %x\n", slot, offset, val);
51 outl(0x80000000 | (bus<<16) | (slot<<11) | (func<<8) | offset, 0xcf8); 51 outl(0x80000000 | (bus<<16) | (slot<<11) | (func<<8) | offset, 0xcf8);
52 outb(val, 0xcfc); 52 outb(val, 0xcfc + (offset&3));
53}
54
55void write_pci_config_16(u8 bus, u8 slot, u8 func, u8 offset, u16 val)
56{
57 PDprintk("%x writing to %x: %x\n", slot, offset, val);
58 outl(0x80000000 | (bus<<16) | (slot<<11) | (func<<8) | offset, 0xcf8);
59 outw(val, 0xcfc + (offset&2));
53} 60}
54 61
55int early_pci_allowed(void) 62int early_pci_allowed(void)
@@ -57,3 +64,54 @@ int early_pci_allowed(void)
57 return (pci_probe & (PCI_PROBE_CONF1|PCI_PROBE_NOEARLY)) == 64 return (pci_probe & (PCI_PROBE_CONF1|PCI_PROBE_NOEARLY)) ==
58 PCI_PROBE_CONF1; 65 PCI_PROBE_CONF1;
59} 66}
67
68void early_dump_pci_device(u8 bus, u8 slot, u8 func)
69{
70 int i;
71 int j;
72 u32 val;
73
74 printk("PCI: %02x:%02x:%02x", bus, slot, func);
75
76 for (i = 0; i < 256; i += 4) {
77 if (!(i & 0x0f))
78 printk("\n%04x:",i);
79
80 val = read_pci_config(bus, slot, func, i);
81 for (j = 0; j < 4; j++) {
82 printk(" %02x", val & 0xff);
83 val >>= 8;
84 }
85 }
86 printk("\n");
87}
88
89void early_dump_pci_devices(void)
90{
91 unsigned bus, slot, func;
92
93 if (!early_pci_allowed())
94 return;
95
96 for (bus = 0; bus < 256; bus++) {
97 for (slot = 0; slot < 32; slot++) {
98 for (func = 0; func < 8; func++) {
99 u32 class;
100 u8 type;
101 class = read_pci_config(bus, slot, func,
102 PCI_CLASS_REVISION);
103 if (class == 0xffffffff)
104 break;
105
106 early_dump_pci_device(bus, slot, func);
107
108 /* No multi-function device? */
109 type = read_pci_config_byte(bus, slot, func,
110 PCI_HEADER_TYPE);
111 if (!(type & 0x80))
112 break;
113 }
114 }
115 }
116}
117
diff --git a/arch/x86/pci/irq.c b/arch/x86/pci/irq.c
index dc568c6b83f8..6a06a2eb0597 100644
--- a/arch/x86/pci/irq.c
+++ b/arch/x86/pci/irq.c
@@ -45,7 +45,8 @@ struct irq_router {
45 char *name; 45 char *name;
46 u16 vendor, device; 46 u16 vendor, device;
47 int (*get)(struct pci_dev *router, struct pci_dev *dev, int pirq); 47 int (*get)(struct pci_dev *router, struct pci_dev *dev, int pirq);
48 int (*set)(struct pci_dev *router, struct pci_dev *dev, int pirq, int new); 48 int (*set)(struct pci_dev *router, struct pci_dev *dev, int pirq,
49 int new);
49}; 50};
50 51
51struct irq_router_handler { 52struct irq_router_handler {
@@ -77,7 +78,8 @@ static inline struct irq_routing_table *pirq_check_routing_table(u8 *addr)
77 for (i = 0; i < rt->size; i++) 78 for (i = 0; i < rt->size; i++)
78 sum += addr[i]; 79 sum += addr[i];
79 if (!sum) { 80 if (!sum) {
80 DBG(KERN_DEBUG "PCI: Interrupt Routing Table found at 0x%p\n", rt); 81 DBG(KERN_DEBUG "PCI: Interrupt Routing Table found at 0x%p\n",
82 rt);
81 return rt; 83 return rt;
82 } 84 }
83 return NULL; 85 return NULL;
@@ -183,7 +185,8 @@ static unsigned int read_config_nybble(struct pci_dev *router, unsigned offset,
183 return (nr & 1) ? (x >> 4) : (x & 0xf); 185 return (nr & 1) ? (x >> 4) : (x & 0xf);
184} 186}
185 187
186static void write_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr, unsigned int val) 188static void write_config_nybble(struct pci_dev *router, unsigned offset,
189 unsigned nr, unsigned int val)
187{ 190{
188 u8 x; 191 u8 x;
189 unsigned reg = offset + (nr >> 1); 192 unsigned reg = offset + (nr >> 1);
@@ -467,7 +470,8 @@ static int pirq_serverworks_get(struct pci_dev *router, struct pci_dev *dev, int
467 return inb(0xc01) & 0xf; 470 return inb(0xc01) & 0xf;
468} 471}
469 472
470static int pirq_serverworks_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq) 473static int pirq_serverworks_set(struct pci_dev *router, struct pci_dev *dev,
474 int pirq, int irq)
471{ 475{
472 outb(pirq, 0xc00); 476 outb(pirq, 0xc00);
473 outb(irq, 0xc01); 477 outb(irq, 0xc01);
@@ -660,7 +664,8 @@ static __init int vlsi_router_probe(struct irq_router *r, struct pci_dev *router
660} 664}
661 665
662 666
663static __init int serverworks_router_probe(struct irq_router *r, struct pci_dev *router, u16 device) 667static __init int serverworks_router_probe(struct irq_router *r,
668 struct pci_dev *router, u16 device)
664{ 669{
665 switch (device) { 670 switch (device) {
666 case PCI_DEVICE_ID_SERVERWORKS_OSB4: 671 case PCI_DEVICE_ID_SERVERWORKS_OSB4:
@@ -827,10 +832,12 @@ static void __init pirq_find_router(struct irq_router *r)
827 832
828 for (h = pirq_routers; h->vendor; h++) { 833 for (h = pirq_routers; h->vendor; h++) {
829 /* First look for a router match */ 834 /* First look for a router match */
830 if (rt->rtr_vendor == h->vendor && h->probe(r, pirq_router_dev, rt->rtr_device)) 835 if (rt->rtr_vendor == h->vendor &&
836 h->probe(r, pirq_router_dev, rt->rtr_device))
831 break; 837 break;
832 /* Fall back to a device match */ 838 /* Fall back to a device match */
833 if (pirq_router_dev->vendor == h->vendor && h->probe(r, pirq_router_dev, pirq_router_dev->device)) 839 if (pirq_router_dev->vendor == h->vendor &&
840 h->probe(r, pirq_router_dev, pirq_router_dev->device))
834 break; 841 break;
835 } 842 }
836 printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %s\n", 843 printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %s\n",
@@ -845,11 +852,13 @@ static void __init pirq_find_router(struct irq_router *r)
845static struct irq_info *pirq_get_info(struct pci_dev *dev) 852static struct irq_info *pirq_get_info(struct pci_dev *dev)
846{ 853{
847 struct irq_routing_table *rt = pirq_table; 854 struct irq_routing_table *rt = pirq_table;
848 int entries = (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info); 855 int entries = (rt->size - sizeof(struct irq_routing_table)) /
856 sizeof(struct irq_info);
849 struct irq_info *info; 857 struct irq_info *info;
850 858
851 for (info = rt->slots; entries--; info++) 859 for (info = rt->slots; entries--; info++)
852 if (info->bus == dev->bus->number && PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn)) 860 if (info->bus == dev->bus->number &&
861 PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn))
853 return info; 862 return info;
854 return NULL; 863 return NULL;
855} 864}
@@ -890,7 +899,8 @@ static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
890 DBG(" -> not routed\n" KERN_DEBUG); 899 DBG(" -> not routed\n" KERN_DEBUG);
891 return 0; 900 return 0;
892 } 901 }
893 DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask, pirq_table->exclusive_irqs); 902 DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask,
903 pirq_table->exclusive_irqs);
894 mask &= pcibios_irq_mask; 904 mask &= pcibios_irq_mask;
895 905
896 /* Work around broken HP Pavilion Notebooks which assign USB to 906 /* Work around broken HP Pavilion Notebooks which assign USB to
@@ -903,7 +913,8 @@ static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
903 } 913 }
904 914
905 /* same for Acer Travelmate 360, but with CB and irq 11 -> 10 */ 915 /* same for Acer Travelmate 360, but with CB and irq 11 -> 10 */
906 if (acer_tm360_irqrouting && dev->irq == 11 && dev->vendor == PCI_VENDOR_ID_O2) { 916 if (acer_tm360_irqrouting && dev->irq == 11 &&
917 dev->vendor == PCI_VENDOR_ID_O2) {
907 pirq = 0x68; 918 pirq = 0x68;
908 mask = 0x400; 919 mask = 0x400;
909 dev->irq = r->get(pirq_router_dev, dev, pirq); 920 dev->irq = r->get(pirq_router_dev, dev, pirq);
@@ -920,15 +931,16 @@ static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
920 newirq = 0; 931 newirq = 0;
921 else 932 else
922 printk("\n" KERN_WARNING 933 printk("\n" KERN_WARNING
923 "PCI: IRQ %i for device %s doesn't match PIRQ mask " 934 "PCI: IRQ %i for device %s doesn't match PIRQ mask - try pci=usepirqmask\n"
924 "- try pci=usepirqmask\n" KERN_DEBUG, newirq, 935 KERN_DEBUG, newirq,
925 pci_name(dev)); 936 pci_name(dev));
926 } 937 }
927 if (!newirq && assign) { 938 if (!newirq && assign) {
928 for (i = 0; i < 16; i++) { 939 for (i = 0; i < 16; i++) {
929 if (!(mask & (1 << i))) 940 if (!(mask & (1 << i)))
930 continue; 941 continue;
931 if (pirq_penalty[i] < pirq_penalty[newirq] && can_request_irq(i, IRQF_SHARED)) 942 if (pirq_penalty[i] < pirq_penalty[newirq] &&
943 can_request_irq(i, IRQF_SHARED))
932 newirq = i; 944 newirq = i;
933 } 945 }
934 } 946 }
@@ -944,7 +956,8 @@ static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
944 DBG(" -> got IRQ %d\n", irq); 956 DBG(" -> got IRQ %d\n", irq);
945 msg = "Found"; 957 msg = "Found";
946 eisa_set_level_irq(irq); 958 eisa_set_level_irq(irq);
947 } else if (newirq && r->set && (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) { 959 } else if (newirq && r->set &&
960 (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) {
948 DBG(" -> assigning IRQ %d", newirq); 961 DBG(" -> assigning IRQ %d", newirq);
949 if (r->set(pirq_router_dev, dev, pirq, newirq)) { 962 if (r->set(pirq_router_dev, dev, pirq, newirq)) {
950 eisa_set_level_irq(newirq); 963 eisa_set_level_irq(newirq);
@@ -962,7 +975,8 @@ static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
962 } else 975 } else
963 return 0; 976 return 0;
964 } 977 }
965 printk(KERN_INFO "PCI: %s IRQ %d for device %s\n", msg, irq, pci_name(dev)); 978 printk(KERN_INFO "PCI: %s IRQ %d for device %s\n", msg, irq,
979 pci_name(dev));
966 980
967 /* Update IRQ for all devices with the same pirq value */ 981 /* Update IRQ for all devices with the same pirq value */
968 while ((dev2 = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev2)) != NULL) { 982 while ((dev2 = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev2)) != NULL) {
@@ -974,7 +988,10 @@ static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
974 if (!info) 988 if (!info)
975 continue; 989 continue;
976 if (info->irq[pin].link == pirq) { 990 if (info->irq[pin].link == pirq) {
977 /* We refuse to override the dev->irq information. Give a warning! */ 991 /*
992 * We refuse to override the dev->irq
993 * information. Give a warning!
994 */
978 if (dev2->irq && dev2->irq != irq && \ 995 if (dev2->irq && dev2->irq != irq && \
979 (!(pci_probe & PCI_USE_PIRQ_MASK) || \ 996 (!(pci_probe & PCI_USE_PIRQ_MASK) || \
980 ((1 << dev2->irq) & mask))) { 997 ((1 << dev2->irq) & mask))) {
@@ -987,7 +1004,9 @@ static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
987 dev2->irq = irq; 1004 dev2->irq = irq;
988 pirq_penalty[irq]++; 1005 pirq_penalty[irq]++;
989 if (dev != dev2) 1006 if (dev != dev2)
990 printk(KERN_INFO "PCI: Sharing IRQ %d with %s\n", irq, pci_name(dev2)); 1007 printk(KERN_INFO
1008 "PCI: Sharing IRQ %d with %s\n",
1009 irq, pci_name(dev2));
991 } 1010 }
992 } 1011 }
993 return 1; 1012 return 1;
@@ -1001,15 +1020,21 @@ static void __init pcibios_fixup_irqs(void)
1001 DBG(KERN_DEBUG "PCI: IRQ fixup\n"); 1020 DBG(KERN_DEBUG "PCI: IRQ fixup\n");
1002 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { 1021 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
1003 /* 1022 /*
1004 * If the BIOS has set an out of range IRQ number, just ignore it. 1023 * If the BIOS has set an out of range IRQ number, just
1005 * Also keep track of which IRQ's are already in use. 1024 * ignore it. Also keep track of which IRQ's are
1025 * already in use.
1006 */ 1026 */
1007 if (dev->irq >= 16) { 1027 if (dev->irq >= 16) {
1008 DBG(KERN_DEBUG "%s: ignoring bogus IRQ %d\n", pci_name(dev), dev->irq); 1028 DBG(KERN_DEBUG "%s: ignoring bogus IRQ %d\n",
1029 pci_name(dev), dev->irq);
1009 dev->irq = 0; 1030 dev->irq = 0;
1010 } 1031 }
1011 /* If the IRQ is already assigned to a PCI device, ignore its ISA use penalty */ 1032 /*
1012 if (pirq_penalty[dev->irq] >= 100 && pirq_penalty[dev->irq] < 100000) 1033 * If the IRQ is already assigned to a PCI device,
1034 * ignore its ISA use penalty
1035 */
1036 if (pirq_penalty[dev->irq] >= 100 &&
1037 pirq_penalty[dev->irq] < 100000)
1013 pirq_penalty[dev->irq] = 0; 1038 pirq_penalty[dev->irq] = 0;
1014 pirq_penalty[dev->irq]++; 1039 pirq_penalty[dev->irq]++;
1015 } 1040 }
@@ -1025,8 +1050,13 @@ static void __init pcibios_fixup_irqs(void)
1025 int irq; 1050 int irq;
1026 1051
1027 if (pin) { 1052 if (pin) {
1028 pin--; /* interrupt pins are numbered starting from 1 */ 1053 /*
1029 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin); 1054 * interrupt pins are numbered starting
1055 * from 1
1056 */
1057 pin--;
1058 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number,
1059 PCI_SLOT(dev->devfn), pin);
1030 /* 1060 /*
1031 * Busses behind bridges are typically not listed in the MP-table. 1061 * Busses behind bridges are typically not listed in the MP-table.
1032 * In this case we have to look up the IRQ based on the parent bus, 1062 * In this case we have to look up the IRQ based on the parent bus,
@@ -1067,7 +1097,8 @@ static int __init fix_broken_hp_bios_irq9(const struct dmi_system_id *d)
1067{ 1097{
1068 if (!broken_hp_bios_irq9) { 1098 if (!broken_hp_bios_irq9) {
1069 broken_hp_bios_irq9 = 1; 1099 broken_hp_bios_irq9 = 1;
1070 printk(KERN_INFO "%s detected - fixing broken IRQ routing\n", d->ident); 1100 printk(KERN_INFO "%s detected - fixing broken IRQ routing\n",
1101 d->ident);
1071 } 1102 }
1072 return 0; 1103 return 0;
1073} 1104}
@@ -1080,7 +1111,8 @@ static int __init fix_acer_tm360_irqrouting(const struct dmi_system_id *d)
1080{ 1111{
1081 if (!acer_tm360_irqrouting) { 1112 if (!acer_tm360_irqrouting) {
1082 acer_tm360_irqrouting = 1; 1113 acer_tm360_irqrouting = 1;
1083 printk(KERN_INFO "%s detected - fixing broken IRQ routing\n", d->ident); 1114 printk(KERN_INFO "%s detected - fixing broken IRQ routing\n",
1115 d->ident);
1084 } 1116 }
1085 return 0; 1117 return 0;
1086} 1118}
@@ -1092,7 +1124,8 @@ static struct dmi_system_id __initdata pciirq_dmi_table[] = {
1092 .matches = { 1124 .matches = {
1093 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 1125 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1094 DMI_MATCH(DMI_BIOS_VERSION, "GE.M1.03"), 1126 DMI_MATCH(DMI_BIOS_VERSION, "GE.M1.03"),
1095 DMI_MATCH(DMI_PRODUCT_VERSION, "HP Pavilion Notebook Model GE"), 1127 DMI_MATCH(DMI_PRODUCT_VERSION,
1128 "HP Pavilion Notebook Model GE"),
1096 DMI_MATCH(DMI_BOARD_VERSION, "OmniBook N32N-736"), 1129 DMI_MATCH(DMI_BOARD_VERSION, "OmniBook N32N-736"),
1097 }, 1130 },
1098 }, 1131 },
@@ -1131,7 +1164,10 @@ int __init pcibios_irq_init(void)
1131 if (!(pirq_table->exclusive_irqs & (1 << i))) 1164 if (!(pirq_table->exclusive_irqs & (1 << i)))
1132 pirq_penalty[i] += 100; 1165 pirq_penalty[i] += 100;
1133 } 1166 }
1134 /* If we're using the I/O APIC, avoid using the PCI IRQ routing table */ 1167 /*
1168 * If we're using the I/O APIC, avoid using the PCI IRQ
1169 * routing table
1170 */
1135 if (io_apic_assign_pci_irqs) 1171 if (io_apic_assign_pci_irqs)
1136 pirq_table = NULL; 1172 pirq_table = NULL;
1137 } 1173 }
@@ -1175,7 +1211,7 @@ static int pirq_enable_irq(struct pci_dev *dev)
1175 if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) { 1211 if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) {
1176 char *msg = ""; 1212 char *msg = "";
1177 1213
1178 pin--; /* interrupt pins are numbered starting from 1 */ 1214 pin--; /* interrupt pins are numbered starting from 1 */
1179 1215
1180 if (io_apic_assign_pci_irqs) { 1216 if (io_apic_assign_pci_irqs) {
1181 int irq; 1217 int irq;
@@ -1195,13 +1231,16 @@ static int pirq_enable_irq(struct pci_dev *dev)
1195 irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 1231 irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number,
1196 PCI_SLOT(bridge->devfn), pin); 1232 PCI_SLOT(bridge->devfn), pin);
1197 if (irq >= 0) 1233 if (irq >= 0)
1198 printk(KERN_WARNING "PCI: using PPB %s[%c] to get irq %d\n", 1234 printk(KERN_WARNING
1199 pci_name(bridge), 'A' + pin, irq); 1235 "PCI: using PPB %s[%c] to get irq %d\n",
1236 pci_name(bridge),
1237 'A' + pin, irq);
1200 dev = bridge; 1238 dev = bridge;
1201 } 1239 }
1202 dev = temp_dev; 1240 dev = temp_dev;
1203 if (irq >= 0) { 1241 if (irq >= 0) {
1204 printk(KERN_INFO "PCI->APIC IRQ transform: %s[%c] -> IRQ %d\n", 1242 printk(KERN_INFO
1243 "PCI->APIC IRQ transform: %s[%c] -> IRQ %d\n",
1205 pci_name(dev), 'A' + pin, irq); 1244 pci_name(dev), 'A' + pin, irq);
1206 dev->irq = irq; 1245 dev->irq = irq;
1207 return 0; 1246 return 0;
@@ -1212,12 +1251,17 @@ static int pirq_enable_irq(struct pci_dev *dev)
1212 else 1251 else
1213 msg = " Please try using pci=biosirq."; 1252 msg = " Please try using pci=biosirq.";
1214 1253
1215 /* With IDE legacy devices the IRQ lookup failure is not a problem.. */ 1254 /*
1216 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE && !(dev->class & 0x5)) 1255 * With IDE legacy devices the IRQ lookup failure is not
1256 * a problem..
1257 */
1258 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
1259 !(dev->class & 0x5))
1217 return 0; 1260 return 0;
1218 1261
1219 printk(KERN_WARNING "PCI: No IRQ known for interrupt pin %c of device %s.%s\n", 1262 printk(KERN_WARNING
1220 'A' + pin, pci_name(dev), msg); 1263 "PCI: No IRQ known for interrupt pin %c of device %s.%s\n",
1264 'A' + pin, pci_name(dev), msg);
1221 } 1265 }
1222 return 0; 1266 return 0;
1223} 1267}
diff --git a/arch/x86/pci/pci.h b/arch/x86/pci/pci.h
index b2270a55b0cf..3e25deb821ac 100644
--- a/arch/x86/pci/pci.h
+++ b/arch/x86/pci/pci.h
@@ -28,6 +28,7 @@
28#define PCI_USE__CRS 0x10000 28#define PCI_USE__CRS 0x10000
29#define PCI_CHECK_ENABLE_AMD_MMCONF 0x20000 29#define PCI_CHECK_ENABLE_AMD_MMCONF 0x20000
30#define PCI_HAS_IO_ECS 0x40000 30#define PCI_HAS_IO_ECS 0x40000
31#define PCI_NOASSIGN_ROMS 0x80000
31 32
32extern unsigned int pci_probe; 33extern unsigned int pci_probe;
33extern unsigned long pirq_table_addr; 34extern unsigned long pirq_table_addr;
diff --git a/drivers/acpi/Kconfig b/drivers/acpi/Kconfig
index bba867391a85..735f5ea17473 100644
--- a/drivers/acpi/Kconfig
+++ b/drivers/acpi/Kconfig
@@ -336,6 +336,15 @@ config ACPI_EC
336 the battery and thermal drivers. If you are compiling for a 336 the battery and thermal drivers. If you are compiling for a
337 mobile system, say Y. 337 mobile system, say Y.
338 338
339config ACPI_PCI_SLOT
340 tristate "PCI slot detection driver"
341 default n
342 help
343 This driver will attempt to discover all PCI slots in your system,
344 and creates entries in /sys/bus/pci/slots/. This feature can
345 help you correlate PCI bus addresses with the physical geography
346 of your slots. If you are unsure, say N.
347
339config ACPI_POWER 348config ACPI_POWER
340 bool 349 bool
341 default y 350 default y
diff --git a/drivers/acpi/Makefile b/drivers/acpi/Makefile
index 40b0fcae4c78..52a4cd4b81d0 100644
--- a/drivers/acpi/Makefile
+++ b/drivers/acpi/Makefile
@@ -21,7 +21,7 @@ obj-$(CONFIG_X86) += blacklist.o
21# 21#
22# ACPI Core Subsystem (Interpreter) 22# ACPI Core Subsystem (Interpreter)
23# 23#
24obj-y += osl.o utils.o \ 24obj-y += osl.o utils.o reboot.o\
25 dispatcher/ events/ executer/ hardware/ \ 25 dispatcher/ events/ executer/ hardware/ \
26 namespace/ parser/ resources/ tables/ \ 26 namespace/ parser/ resources/ tables/ \
27 utilities/ 27 utilities/
@@ -48,6 +48,7 @@ obj-$(CONFIG_ACPI_DOCK) += dock.o
48obj-$(CONFIG_ACPI_BAY) += bay.o 48obj-$(CONFIG_ACPI_BAY) += bay.o
49obj-$(CONFIG_ACPI_VIDEO) += video.o 49obj-$(CONFIG_ACPI_VIDEO) += video.o
50obj-y += pci_root.o pci_link.o pci_irq.o pci_bind.o 50obj-y += pci_root.o pci_link.o pci_irq.o pci_bind.o
51obj-$(CONFIG_ACPI_PCI_SLOT) += pci_slot.o
51obj-$(CONFIG_ACPI_POWER) += power.o 52obj-$(CONFIG_ACPI_POWER) += power.o
52obj-$(CONFIG_ACPI_PROCESSOR) += processor.o 53obj-$(CONFIG_ACPI_PROCESSOR) += processor.o
53obj-$(CONFIG_ACPI_CONTAINER) += container.o 54obj-$(CONFIG_ACPI_CONTAINER) += container.o
diff --git a/drivers/acpi/bay.c b/drivers/acpi/bay.c
index 61b6c5beb2d3..e6caf5d42e0e 100644
--- a/drivers/acpi/bay.c
+++ b/drivers/acpi/bay.c
@@ -380,6 +380,9 @@ static int __init bay_init(void)
380 if (acpi_disabled) 380 if (acpi_disabled)
381 return -ENODEV; 381 return -ENODEV;
382 382
383 if (acpi_disabled)
384 return -ENODEV;
385
383 /* look for dockable drive bays */ 386 /* look for dockable drive bays */
384 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 387 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
385 ACPI_UINT32_MAX, find_bay, &bays, NULL); 388 ACPI_UINT32_MAX, find_bay, &bays, NULL);
diff --git a/drivers/acpi/bus.c b/drivers/acpi/bus.c
index a6dbcf4d9ef5..ccae305ee55d 100644
--- a/drivers/acpi/bus.c
+++ b/drivers/acpi/bus.c
@@ -295,6 +295,28 @@ int acpi_bus_set_power(acpi_handle handle, int state)
295 295
296EXPORT_SYMBOL(acpi_bus_set_power); 296EXPORT_SYMBOL(acpi_bus_set_power);
297 297
298bool acpi_bus_power_manageable(acpi_handle handle)
299{
300 struct acpi_device *device;
301 int result;
302
303 result = acpi_bus_get_device(handle, &device);
304 return result ? false : device->flags.power_manageable;
305}
306
307EXPORT_SYMBOL(acpi_bus_power_manageable);
308
309bool acpi_bus_can_wakeup(acpi_handle handle)
310{
311 struct acpi_device *device;
312 int result;
313
314 result = acpi_bus_get_device(handle, &device);
315 return result ? false : device->wakeup.flags.valid;
316}
317
318EXPORT_SYMBOL(acpi_bus_can_wakeup);
319
298/* -------------------------------------------------------------------------- 320/* --------------------------------------------------------------------------
299 Event Management 321 Event Management
300 -------------------------------------------------------------------------- */ 322 -------------------------------------------------------------------------- */
@@ -612,7 +634,7 @@ static int __init acpi_bus_init_irq(void)
612 return 0; 634 return 0;
613} 635}
614 636
615acpi_native_uint acpi_gbl_permanent_mmap; 637u8 acpi_gbl_permanent_mmap;
616 638
617 639
618void __init acpi_early_init(void) 640void __init acpi_early_init(void)
diff --git a/drivers/acpi/dispatcher/dsinit.c b/drivers/acpi/dispatcher/dsinit.c
index 610b1ee102b0..949f7c75029e 100644
--- a/drivers/acpi/dispatcher/dsinit.c
+++ b/drivers/acpi/dispatcher/dsinit.c
@@ -151,7 +151,7 @@ acpi_ds_init_one_object(acpi_handle obj_handle,
151 ******************************************************************************/ 151 ******************************************************************************/
152 152
153acpi_status 153acpi_status
154acpi_ds_initialize_objects(acpi_native_uint table_index, 154acpi_ds_initialize_objects(u32 table_index,
155 struct acpi_namespace_node * start_node) 155 struct acpi_namespace_node * start_node)
156{ 156{
157 acpi_status status; 157 acpi_status status;
diff --git a/drivers/acpi/dispatcher/dsmethod.c b/drivers/acpi/dispatcher/dsmethod.c
index 2509809a36cf..4613b9ca5792 100644
--- a/drivers/acpi/dispatcher/dsmethod.c
+++ b/drivers/acpi/dispatcher/dsmethod.c
@@ -377,7 +377,6 @@ acpi_ds_call_control_method(struct acpi_thread_state *thread,
377 } 377 }
378 378
379 info->parameters = &this_walk_state->operands[0]; 379 info->parameters = &this_walk_state->operands[0];
380 info->parameter_type = ACPI_PARAM_ARGS;
381 380
382 status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node, 381 status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node,
383 obj_desc->method.aml_start, 382 obj_desc->method.aml_start,
diff --git a/drivers/acpi/dispatcher/dsopcode.c b/drivers/acpi/dispatcher/dsopcode.c
index a818e0ddb996..6a81c4400edf 100644
--- a/drivers/acpi/dispatcher/dsopcode.c
+++ b/drivers/acpi/dispatcher/dsopcode.c
@@ -691,12 +691,6 @@ acpi_ds_eval_buffer_field_operands(struct acpi_walk_state *walk_state,
691 691
692 status = acpi_ex_resolve_operands(op->common.aml_opcode, 692 status = acpi_ex_resolve_operands(op->common.aml_opcode,
693 ACPI_WALK_OPERANDS, walk_state); 693 ACPI_WALK_OPERANDS, walk_state);
694
695 ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS, ACPI_IMODE_EXECUTE,
696 acpi_ps_get_opcode_name(op->common.aml_opcode),
697 walk_state->num_operands,
698 "after AcpiExResolveOperands");
699
700 if (ACPI_FAILURE(status)) { 694 if (ACPI_FAILURE(status)) {
701 ACPI_ERROR((AE_INFO, "(%s) bad operand(s) (%X)", 695 ACPI_ERROR((AE_INFO, "(%s) bad operand(s) (%X)",
702 acpi_ps_get_opcode_name(op->common.aml_opcode), 696 acpi_ps_get_opcode_name(op->common.aml_opcode),
@@ -785,10 +779,6 @@ acpi_ds_eval_region_operands(struct acpi_walk_state *walk_state,
785 return_ACPI_STATUS(status); 779 return_ACPI_STATUS(status);
786 } 780 }
787 781
788 ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS, ACPI_IMODE_EXECUTE,
789 acpi_ps_get_opcode_name(op->common.aml_opcode),
790 1, "after AcpiExResolveOperands");
791
792 obj_desc = acpi_ns_get_attached_object(node); 782 obj_desc = acpi_ns_get_attached_object(node);
793 if (!obj_desc) { 783 if (!obj_desc) {
794 return_ACPI_STATUS(AE_NOT_EXIST); 784 return_ACPI_STATUS(AE_NOT_EXIST);
@@ -848,7 +838,7 @@ acpi_ds_eval_table_region_operands(struct acpi_walk_state *walk_state,
848 union acpi_operand_object **operand; 838 union acpi_operand_object **operand;
849 struct acpi_namespace_node *node; 839 struct acpi_namespace_node *node;
850 union acpi_parse_object *next_op; 840 union acpi_parse_object *next_op;
851 acpi_native_uint table_index; 841 u32 table_index;
852 struct acpi_table_header *table; 842 struct acpi_table_header *table;
853 843
854 ACPI_FUNCTION_TRACE_PTR(ds_eval_table_region_operands, op); 844 ACPI_FUNCTION_TRACE_PTR(ds_eval_table_region_operands, op);
@@ -882,10 +872,6 @@ acpi_ds_eval_table_region_operands(struct acpi_walk_state *walk_state,
882 return_ACPI_STATUS(status); 872 return_ACPI_STATUS(status);
883 } 873 }
884 874
885 ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS, ACPI_IMODE_EXECUTE,
886 acpi_ps_get_opcode_name(op->common.aml_opcode),
887 1, "after AcpiExResolveOperands");
888
889 operand = &walk_state->operands[0]; 875 operand = &walk_state->operands[0];
890 876
891 /* Find the ACPI table */ 877 /* Find the ACPI table */
@@ -1091,10 +1077,8 @@ acpi_ds_eval_bank_field_operands(struct acpi_walk_state *walk_state,
1091 return_ACPI_STATUS(status); 1077 return_ACPI_STATUS(status);
1092 } 1078 }
1093 1079
1094 ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS, ACPI_IMODE_EXECUTE, 1080 ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS,
1095 acpi_ps_get_opcode_name(op->common.aml_opcode), 1081 acpi_ps_get_opcode_name(op->common.aml_opcode), 1);
1096 1, "after AcpiExResolveOperands");
1097
1098 /* 1082 /*
1099 * Get the bank_value operand and save it 1083 * Get the bank_value operand and save it
1100 * (at Top of stack) 1084 * (at Top of stack)
diff --git a/drivers/acpi/dispatcher/dswexec.c b/drivers/acpi/dispatcher/dswexec.c
index b246b9657ead..b5072fa9c920 100644
--- a/drivers/acpi/dispatcher/dswexec.c
+++ b/drivers/acpi/dispatcher/dswexec.c
@@ -408,14 +408,6 @@ acpi_status acpi_ds_exec_end_op(struct acpi_walk_state *walk_state)
408 [walk_state-> 408 [walk_state->
409 num_operands - 1]), 409 num_operands - 1]),
410 walk_state); 410 walk_state);
411 if (ACPI_SUCCESS(status)) {
412 ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS,
413 ACPI_IMODE_EXECUTE,
414 acpi_ps_get_opcode_name
415 (walk_state->opcode),
416 walk_state->num_operands,
417 "after ExResolveOperands");
418 }
419 } 411 }
420 412
421 if (ACPI_SUCCESS(status)) { 413 if (ACPI_SUCCESS(status)) {
diff --git a/drivers/acpi/dispatcher/dswstate.c b/drivers/acpi/dispatcher/dswstate.c
index 1386ced332ec..b00d4af791aa 100644
--- a/drivers/acpi/dispatcher/dswstate.c
+++ b/drivers/acpi/dispatcher/dswstate.c
@@ -70,7 +70,7 @@ acpi_status
70acpi_ds_result_pop(union acpi_operand_object **object, 70acpi_ds_result_pop(union acpi_operand_object **object,
71 struct acpi_walk_state *walk_state) 71 struct acpi_walk_state *walk_state)
72{ 72{
73 acpi_native_uint index; 73 u32 index;
74 union acpi_generic_state *state; 74 union acpi_generic_state *state;
75 acpi_status status; 75 acpi_status status;
76 76
@@ -122,7 +122,7 @@ acpi_ds_result_pop(union acpi_operand_object **object,
122 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, 122 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
123 "Obj=%p [%s] Index=%X State=%p Num=%X\n", *object, 123 "Obj=%p [%s] Index=%X State=%p Num=%X\n", *object,
124 acpi_ut_get_object_type_name(*object), 124 acpi_ut_get_object_type_name(*object),
125 (u32) index, walk_state, walk_state->result_count)); 125 index, walk_state, walk_state->result_count));
126 126
127 return (AE_OK); 127 return (AE_OK);
128} 128}
@@ -146,7 +146,7 @@ acpi_ds_result_push(union acpi_operand_object * object,
146{ 146{
147 union acpi_generic_state *state; 147 union acpi_generic_state *state;
148 acpi_status status; 148 acpi_status status;
149 acpi_native_uint index; 149 u32 index;
150 150
151 ACPI_FUNCTION_NAME(ds_result_push); 151 ACPI_FUNCTION_NAME(ds_result_push);
152 152
@@ -400,7 +400,7 @@ void
400acpi_ds_obj_stack_pop_and_delete(u32 pop_count, 400acpi_ds_obj_stack_pop_and_delete(u32 pop_count,
401 struct acpi_walk_state *walk_state) 401 struct acpi_walk_state *walk_state)
402{ 402{
403 acpi_native_int i; 403 s32 i;
404 union acpi_operand_object *obj_desc; 404 union acpi_operand_object *obj_desc;
405 405
406 ACPI_FUNCTION_NAME(ds_obj_stack_pop_and_delete); 406 ACPI_FUNCTION_NAME(ds_obj_stack_pop_and_delete);
@@ -409,7 +409,7 @@ acpi_ds_obj_stack_pop_and_delete(u32 pop_count,
409 return; 409 return;
410 } 410 }
411 411
412 for (i = (acpi_native_int) (pop_count - 1); i >= 0; i--) { 412 for (i = (s32) pop_count - 1; i >= 0; i--) {
413 if (walk_state->num_operands == 0) { 413 if (walk_state->num_operands == 0) {
414 return; 414 return;
415 } 415 }
@@ -615,14 +615,8 @@ acpi_ds_init_aml_walk(struct acpi_walk_state *walk_state,
615 walk_state->pass_number = pass_number; 615 walk_state->pass_number = pass_number;
616 616
617 if (info) { 617 if (info) {
618 if (info->parameter_type == ACPI_PARAM_GPE) { 618 walk_state->params = info->parameters;
619 walk_state->gpe_event_info = 619 walk_state->caller_return_desc = &info->return_object;
620 ACPI_CAST_PTR(struct acpi_gpe_event_info,
621 info->parameters);
622 } else {
623 walk_state->params = info->parameters;
624 walk_state->caller_return_desc = &info->return_object;
625 }
626 } 620 }
627 621
628 status = acpi_ps_init_scope(&walk_state->parser_state, op); 622 status = acpi_ps_init_scope(&walk_state->parser_state, op);
diff --git a/drivers/acpi/dock.c b/drivers/acpi/dock.c
index bb7c51f712bd..1e872e79db33 100644
--- a/drivers/acpi/dock.c
+++ b/drivers/acpi/dock.c
@@ -920,6 +920,9 @@ static int __init dock_init(void)
920 if (acpi_disabled) 920 if (acpi_disabled)
921 return 0; 921 return 0;
922 922
923 if (acpi_disabled)
924 return 0;
925
923 /* look for a dock station */ 926 /* look for a dock station */
924 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 927 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
925 ACPI_UINT32_MAX, find_dock, &num, NULL); 928 ACPI_UINT32_MAX, find_dock, &num, NULL);
diff --git a/drivers/acpi/events/evevent.c b/drivers/acpi/events/evevent.c
index 5d30e5be1b1c..c56c5c6ea77b 100644
--- a/drivers/acpi/events/evevent.c
+++ b/drivers/acpi/events/evevent.c
@@ -188,7 +188,7 @@ acpi_status acpi_ev_install_xrupt_handlers(void)
188 188
189static acpi_status acpi_ev_fixed_event_initialize(void) 189static acpi_status acpi_ev_fixed_event_initialize(void)
190{ 190{
191 acpi_native_uint i; 191 u32 i;
192 acpi_status status; 192 acpi_status status;
193 193
194 /* 194 /*
@@ -231,7 +231,7 @@ u32 acpi_ev_fixed_event_detect(void)
231 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED; 231 u32 int_status = ACPI_INTERRUPT_NOT_HANDLED;
232 u32 fixed_status; 232 u32 fixed_status;
233 u32 fixed_enable; 233 u32 fixed_enable;
234 acpi_native_uint i; 234 u32 i;
235 235
236 ACPI_FUNCTION_NAME(ev_fixed_event_detect); 236 ACPI_FUNCTION_NAME(ev_fixed_event_detect);
237 237
@@ -260,7 +260,7 @@ u32 acpi_ev_fixed_event_detect(void)
260 260
261 /* Found an active (signalled) event */ 261 /* Found an active (signalled) event */
262 acpi_os_fixed_event_count(i); 262 acpi_os_fixed_event_count(i);
263 int_status |= acpi_ev_fixed_event_dispatch((u32) i); 263 int_status |= acpi_ev_fixed_event_dispatch(i);
264 } 264 }
265 } 265 }
266 266
diff --git a/drivers/acpi/events/evgpe.c b/drivers/acpi/events/evgpe.c
index 5354be44f876..c5e53aae86f7 100644
--- a/drivers/acpi/events/evgpe.c
+++ b/drivers/acpi/events/evgpe.c
@@ -256,7 +256,7 @@ acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
256 return_ACPI_STATUS(status); 256 return_ACPI_STATUS(status);
257 } 257 }
258 258
259 /* Mark wake-disabled or HW disable, or both */ 259 /* Clear the appropriate enabled flags for this GPE */
260 260
261 switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) { 261 switch (gpe_event_info->flags & ACPI_GPE_TYPE_MASK) {
262 case ACPI_GPE_TYPE_WAKE: 262 case ACPI_GPE_TYPE_WAKE:
@@ -273,13 +273,23 @@ acpi_status acpi_ev_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
273 /* Disable the requested runtime GPE */ 273 /* Disable the requested runtime GPE */
274 274
275 ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED); 275 ACPI_CLEAR_BIT(gpe_event_info->flags, ACPI_GPE_RUN_ENABLED);
276 276 break;
277 /* fallthrough */
278 277
279 default: 278 default:
280 acpi_hw_write_gpe_enable_reg(gpe_event_info); 279 break;
281 } 280 }
282 281
282 /*
283 * Even if we don't know the GPE type, make sure that we always
284 * disable it. low_disable_gpe will just clear the enable bit for this
285 * GPE and write it. It will not write out the current GPE enable mask,
286 * since this may inadvertently enable GPEs too early, if a rogue GPE has
287 * come in during ACPICA initialization - possibly as a result of AML or
288 * other code that has enabled the GPE.
289 */
290 status = acpi_hw_low_disable_gpe(gpe_event_info);
291 return_ACPI_STATUS(status);
292
283 return_ACPI_STATUS(AE_OK); 293 return_ACPI_STATUS(AE_OK);
284} 294}
285 295
@@ -305,7 +315,7 @@ struct acpi_gpe_event_info *acpi_ev_get_gpe_event_info(acpi_handle gpe_device,
305{ 315{
306 union acpi_operand_object *obj_desc; 316 union acpi_operand_object *obj_desc;
307 struct acpi_gpe_block_info *gpe_block; 317 struct acpi_gpe_block_info *gpe_block;
308 acpi_native_uint i; 318 u32 i;
309 319
310 ACPI_FUNCTION_ENTRY(); 320 ACPI_FUNCTION_ENTRY();
311 321
@@ -379,8 +389,8 @@ u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
379 u32 status_reg; 389 u32 status_reg;
380 u32 enable_reg; 390 u32 enable_reg;
381 acpi_cpu_flags flags; 391 acpi_cpu_flags flags;
382 acpi_native_uint i; 392 u32 i;
383 acpi_native_uint j; 393 u32 j;
384 394
385 ACPI_FUNCTION_NAME(ev_gpe_detect); 395 ACPI_FUNCTION_NAME(ev_gpe_detect);
386 396
@@ -462,13 +472,7 @@ u32 acpi_ev_gpe_detect(struct acpi_gpe_xrupt_info * gpe_xrupt_list)
462 */ 472 */
463 int_status |= 473 int_status |=
464 acpi_ev_gpe_dispatch(&gpe_block-> 474 acpi_ev_gpe_dispatch(&gpe_block->
465 event_info[(i * 475 event_info[((acpi_size) i * ACPI_GPE_REGISTER_WIDTH) + j], j + gpe_register_info->base_gpe_number);
466 ACPI_GPE_REGISTER_WIDTH)
467 +
468 j],
469 (u32) j +
470 gpe_register_info->
471 base_gpe_number);
472 } 476 }
473 } 477 }
474 } 478 }
@@ -555,10 +559,6 @@ static void ACPI_SYSTEM_XFACE acpi_ev_asynch_execute_gpe_method(void *context)
555 */ 559 */
556 info->prefix_node = 560 info->prefix_node =
557 local_gpe_event_info.dispatch.method_node; 561 local_gpe_event_info.dispatch.method_node;
558 info->parameters =
559 ACPI_CAST_PTR(union acpi_operand_object *,
560 gpe_event_info);
561 info->parameter_type = ACPI_PARAM_GPE;
562 info->flags = ACPI_IGNORE_RETURN_VALUE; 562 info->flags = ACPI_IGNORE_RETURN_VALUE;
563 563
564 status = acpi_ns_evaluate(info); 564 status = acpi_ns_evaluate(info);
diff --git a/drivers/acpi/events/evgpeblk.c b/drivers/acpi/events/evgpeblk.c
index e6c4d4c49e79..73c058e2f5c2 100644
--- a/drivers/acpi/events/evgpeblk.c
+++ b/drivers/acpi/events/evgpeblk.c
@@ -189,8 +189,8 @@ acpi_ev_delete_gpe_handlers(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
189 struct acpi_gpe_block_info *gpe_block) 189 struct acpi_gpe_block_info *gpe_block)
190{ 190{
191 struct acpi_gpe_event_info *gpe_event_info; 191 struct acpi_gpe_event_info *gpe_event_info;
192 acpi_native_uint i; 192 u32 i;
193 acpi_native_uint j; 193 u32 j;
194 194
195 ACPI_FUNCTION_TRACE(ev_delete_gpe_handlers); 195 ACPI_FUNCTION_TRACE(ev_delete_gpe_handlers);
196 196
@@ -203,7 +203,8 @@ acpi_ev_delete_gpe_handlers(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
203 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) { 203 for (j = 0; j < ACPI_GPE_REGISTER_WIDTH; j++) {
204 gpe_event_info = 204 gpe_event_info =
205 &gpe_block-> 205 &gpe_block->
206 event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j]; 206 event_info[((acpi_size) i *
207 ACPI_GPE_REGISTER_WIDTH) + j];
207 208
208 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) == 209 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
209 ACPI_GPE_DISPATCH_HANDLER) { 210 ACPI_GPE_DISPATCH_HANDLER) {
@@ -744,8 +745,8 @@ acpi_ev_create_gpe_info_blocks(struct acpi_gpe_block_info *gpe_block)
744 struct acpi_gpe_event_info *gpe_event_info = NULL; 745 struct acpi_gpe_event_info *gpe_event_info = NULL;
745 struct acpi_gpe_event_info *this_event; 746 struct acpi_gpe_event_info *this_event;
746 struct acpi_gpe_register_info *this_register; 747 struct acpi_gpe_register_info *this_register;
747 acpi_native_uint i; 748 u32 i;
748 acpi_native_uint j; 749 u32 j;
749 acpi_status status; 750 acpi_status status;
750 751
751 ACPI_FUNCTION_TRACE(ev_create_gpe_info_blocks); 752 ACPI_FUNCTION_TRACE(ev_create_gpe_info_blocks);
@@ -983,8 +984,8 @@ acpi_ev_initialize_gpe_block(struct acpi_namespace_node *gpe_device,
983 struct acpi_gpe_walk_info gpe_info; 984 struct acpi_gpe_walk_info gpe_info;
984 u32 wake_gpe_count; 985 u32 wake_gpe_count;
985 u32 gpe_enabled_count; 986 u32 gpe_enabled_count;
986 acpi_native_uint i; 987 u32 i;
987 acpi_native_uint j; 988 u32 j;
988 989
989 ACPI_FUNCTION_TRACE(ev_initialize_gpe_block); 990 ACPI_FUNCTION_TRACE(ev_initialize_gpe_block);
990 991
@@ -1033,7 +1034,8 @@ acpi_ev_initialize_gpe_block(struct acpi_namespace_node *gpe_device,
1033 1034
1034 gpe_event_info = 1035 gpe_event_info =
1035 &gpe_block-> 1036 &gpe_block->
1036 event_info[(i * ACPI_GPE_REGISTER_WIDTH) + j]; 1037 event_info[((acpi_size) i *
1038 ACPI_GPE_REGISTER_WIDTH) + j];
1037 1039
1038 if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) == 1040 if (((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
1039 ACPI_GPE_DISPATCH_METHOD) 1041 ACPI_GPE_DISPATCH_METHOD)
diff --git a/drivers/acpi/events/evmisc.c b/drivers/acpi/events/evmisc.c
index 2113e58e2221..1d5670be729a 100644
--- a/drivers/acpi/events/evmisc.c
+++ b/drivers/acpi/events/evmisc.c
@@ -575,7 +575,7 @@ acpi_status acpi_ev_release_global_lock(void)
575 575
576void acpi_ev_terminate(void) 576void acpi_ev_terminate(void)
577{ 577{
578 acpi_native_uint i; 578 u32 i;
579 acpi_status status; 579 acpi_status status;
580 580
581 ACPI_FUNCTION_TRACE(ev_terminate); 581 ACPI_FUNCTION_TRACE(ev_terminate);
@@ -589,7 +589,7 @@ void acpi_ev_terminate(void)
589 /* Disable all fixed events */ 589 /* Disable all fixed events */
590 590
591 for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++) { 591 for (i = 0; i < ACPI_NUM_FIXED_EVENTS; i++) {
592 status = acpi_disable_event((u32) i, 0); 592 status = acpi_disable_event(i, 0);
593 if (ACPI_FAILURE(status)) { 593 if (ACPI_FAILURE(status)) {
594 ACPI_ERROR((AE_INFO, 594 ACPI_ERROR((AE_INFO,
595 "Could not disable fixed event %d", 595 "Could not disable fixed event %d",
diff --git a/drivers/acpi/events/evregion.c b/drivers/acpi/events/evregion.c
index 1628f5934752..236fbd1ca438 100644
--- a/drivers/acpi/events/evregion.c
+++ b/drivers/acpi/events/evregion.c
@@ -81,7 +81,7 @@ acpi_ev_install_handler(acpi_handle obj_handle,
81acpi_status acpi_ev_install_region_handlers(void) 81acpi_status acpi_ev_install_region_handlers(void)
82{ 82{
83 acpi_status status; 83 acpi_status status;
84 acpi_native_uint i; 84 u32 i;
85 85
86 ACPI_FUNCTION_TRACE(ev_install_region_handlers); 86 ACPI_FUNCTION_TRACE(ev_install_region_handlers);
87 87
@@ -151,7 +151,7 @@ acpi_status acpi_ev_install_region_handlers(void)
151acpi_status acpi_ev_initialize_op_regions(void) 151acpi_status acpi_ev_initialize_op_regions(void)
152{ 152{
153 acpi_status status; 153 acpi_status status;
154 acpi_native_uint i; 154 u32 i;
155 155
156 ACPI_FUNCTION_TRACE(ev_initialize_op_regions); 156 ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
157 157
@@ -219,7 +219,6 @@ acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
219 info->prefix_node = region_obj2->extra.method_REG; 219 info->prefix_node = region_obj2->extra.method_REG;
220 info->pathname = NULL; 220 info->pathname = NULL;
221 info->parameters = args; 221 info->parameters = args;
222 info->parameter_type = ACPI_PARAM_ARGS;
223 info->flags = ACPI_IGNORE_RETURN_VALUE; 222 info->flags = ACPI_IGNORE_RETURN_VALUE;
224 223
225 /* 224 /*
diff --git a/drivers/acpi/events/evrgnini.c b/drivers/acpi/events/evrgnini.c
index 2e3d2c5e4f4d..6b94b38df07d 100644
--- a/drivers/acpi/events/evrgnini.c
+++ b/drivers/acpi/events/evrgnini.c
@@ -380,7 +380,7 @@ static u8 acpi_ev_is_pci_root_bridge(struct acpi_namespace_node *node)
380 acpi_status status; 380 acpi_status status;
381 struct acpica_device_id hid; 381 struct acpica_device_id hid;
382 struct acpi_compatible_id_list *cid; 382 struct acpi_compatible_id_list *cid;
383 acpi_native_uint i; 383 u32 i;
384 384
385 /* 385 /*
386 * Get the _HID and check for a PCI Root Bridge 386 * Get the _HID and check for a PCI Root Bridge
diff --git a/drivers/acpi/events/evxfevnt.c b/drivers/acpi/events/evxfevnt.c
index 99a7502e6a87..73bfd6bf962f 100644
--- a/drivers/acpi/events/evxfevnt.c
+++ b/drivers/acpi/events/evxfevnt.c
@@ -472,7 +472,6 @@ acpi_status acpi_clear_gpe(acpi_handle gpe_device, u32 gpe_number, u32 flags)
472} 472}
473 473
474ACPI_EXPORT_SYMBOL(acpi_clear_gpe) 474ACPI_EXPORT_SYMBOL(acpi_clear_gpe)
475#ifdef ACPI_FUTURE_USAGE
476/******************************************************************************* 475/*******************************************************************************
477 * 476 *
478 * FUNCTION: acpi_get_event_status 477 * FUNCTION: acpi_get_event_status
@@ -489,6 +488,7 @@ ACPI_EXPORT_SYMBOL(acpi_clear_gpe)
489acpi_status acpi_get_event_status(u32 event, acpi_event_status * event_status) 488acpi_status acpi_get_event_status(u32 event, acpi_event_status * event_status)
490{ 489{
491 acpi_status status = AE_OK; 490 acpi_status status = AE_OK;
491 u32 value;
492 492
493 ACPI_FUNCTION_TRACE(acpi_get_event_status); 493 ACPI_FUNCTION_TRACE(acpi_get_event_status);
494 494
@@ -506,7 +506,20 @@ acpi_status acpi_get_event_status(u32 event, acpi_event_status * event_status)
506 506
507 status = 507 status =
508 acpi_get_register(acpi_gbl_fixed_event_info[event]. 508 acpi_get_register(acpi_gbl_fixed_event_info[event].
509 status_register_id, event_status); 509 enable_register_id, &value);
510 if (ACPI_FAILURE(status))
511 return_ACPI_STATUS(status);
512
513 *event_status = value;
514
515 status =
516 acpi_get_register(acpi_gbl_fixed_event_info[event].
517 status_register_id, &value);
518 if (ACPI_FAILURE(status))
519 return_ACPI_STATUS(status);
520
521 if (value)
522 *event_status |= ACPI_EVENT_FLAG_SET;
510 523
511 return_ACPI_STATUS(status); 524 return_ACPI_STATUS(status);
512} 525}
@@ -566,7 +579,6 @@ acpi_get_gpe_status(acpi_handle gpe_device,
566} 579}
567 580
568ACPI_EXPORT_SYMBOL(acpi_get_gpe_status) 581ACPI_EXPORT_SYMBOL(acpi_get_gpe_status)
569#endif /* ACPI_FUTURE_USAGE */
570/******************************************************************************* 582/*******************************************************************************
571 * 583 *
572 * FUNCTION: acpi_install_gpe_block 584 * FUNCTION: acpi_install_gpe_block
diff --git a/drivers/acpi/executer/exconfig.c b/drivers/acpi/executer/exconfig.c
index 39d742190584..2a32c843cb4a 100644
--- a/drivers/acpi/executer/exconfig.c
+++ b/drivers/acpi/executer/exconfig.c
@@ -53,7 +53,7 @@ ACPI_MODULE_NAME("exconfig")
53 53
54/* Local prototypes */ 54/* Local prototypes */
55static acpi_status 55static acpi_status
56acpi_ex_add_table(acpi_native_uint table_index, 56acpi_ex_add_table(u32 table_index,
57 struct acpi_namespace_node *parent_node, 57 struct acpi_namespace_node *parent_node,
58 union acpi_operand_object **ddb_handle); 58 union acpi_operand_object **ddb_handle);
59 59
@@ -73,7 +73,7 @@ acpi_ex_add_table(acpi_native_uint table_index,
73 ******************************************************************************/ 73 ******************************************************************************/
74 74
75static acpi_status 75static acpi_status
76acpi_ex_add_table(acpi_native_uint table_index, 76acpi_ex_add_table(u32 table_index,
77 struct acpi_namespace_node *parent_node, 77 struct acpi_namespace_node *parent_node,
78 union acpi_operand_object **ddb_handle) 78 union acpi_operand_object **ddb_handle)
79{ 79{
@@ -96,7 +96,8 @@ acpi_ex_add_table(acpi_native_uint table_index,
96 96
97 /* Install the new table into the local data structures */ 97 /* Install the new table into the local data structures */
98 98
99 obj_desc->reference.object = ACPI_CAST_PTR(void, table_index); 99 obj_desc->reference.object = ACPI_CAST_PTR(void,
100 (unsigned long)table_index);
100 101
101 /* Add the table to the namespace */ 102 /* Add the table to the namespace */
102 103
@@ -128,12 +129,12 @@ acpi_ex_load_table_op(struct acpi_walk_state *walk_state,
128{ 129{
129 acpi_status status; 130 acpi_status status;
130 union acpi_operand_object **operand = &walk_state->operands[0]; 131 union acpi_operand_object **operand = &walk_state->operands[0];
131 acpi_native_uint table_index;
132 struct acpi_namespace_node *parent_node; 132 struct acpi_namespace_node *parent_node;
133 struct acpi_namespace_node *start_node; 133 struct acpi_namespace_node *start_node;
134 struct acpi_namespace_node *parameter_node = NULL; 134 struct acpi_namespace_node *parameter_node = NULL;
135 union acpi_operand_object *ddb_handle; 135 union acpi_operand_object *ddb_handle;
136 struct acpi_table_header *table; 136 struct acpi_table_header *table;
137 u32 table_index;
137 138
138 ACPI_FUNCTION_TRACE(ex_load_table_op); 139 ACPI_FUNCTION_TRACE(ex_load_table_op);
139 140
@@ -280,7 +281,7 @@ acpi_ex_load_op(union acpi_operand_object *obj_desc,
280{ 281{
281 union acpi_operand_object *ddb_handle; 282 union acpi_operand_object *ddb_handle;
282 struct acpi_table_desc table_desc; 283 struct acpi_table_desc table_desc;
283 acpi_native_uint table_index; 284 u32 table_index;
284 acpi_status status; 285 acpi_status status;
285 u32 length; 286 u32 length;
286 287
@@ -437,7 +438,7 @@ acpi_status acpi_ex_unload_table(union acpi_operand_object *ddb_handle)
437{ 438{
438 acpi_status status = AE_OK; 439 acpi_status status = AE_OK;
439 union acpi_operand_object *table_desc = ddb_handle; 440 union acpi_operand_object *table_desc = ddb_handle;
440 acpi_native_uint table_index; 441 u32 table_index;
441 struct acpi_table_header *table; 442 struct acpi_table_header *table;
442 443
443 ACPI_FUNCTION_TRACE(ex_unload_table); 444 ACPI_FUNCTION_TRACE(ex_unload_table);
@@ -454,9 +455,9 @@ acpi_status acpi_ex_unload_table(union acpi_operand_object *ddb_handle)
454 return_ACPI_STATUS(AE_BAD_PARAMETER); 455 return_ACPI_STATUS(AE_BAD_PARAMETER);
455 } 456 }
456 457
457 /* Get the table index from the ddb_handle */ 458 /* Get the table index from the ddb_handle (acpi_size for 64-bit case) */
458 459
459 table_index = (acpi_native_uint) table_desc->reference.object; 460 table_index = (u32) (acpi_size) table_desc->reference.object;
460 461
461 /* Invoke table handler if present */ 462 /* Invoke table handler if present */
462 463
diff --git a/drivers/acpi/executer/exconvrt.c b/drivers/acpi/executer/exconvrt.c
index fd954b4ed83d..261d97516d9b 100644
--- a/drivers/acpi/executer/exconvrt.c
+++ b/drivers/acpi/executer/exconvrt.c
@@ -288,11 +288,11 @@ acpi_ex_convert_to_ascii(acpi_integer integer,
288 u16 base, u8 * string, u8 data_width) 288 u16 base, u8 * string, u8 data_width)
289{ 289{
290 acpi_integer digit; 290 acpi_integer digit;
291 acpi_native_uint i; 291 u32 i;
292 acpi_native_uint j; 292 u32 j;
293 acpi_native_uint k = 0; 293 u32 k = 0;
294 acpi_native_uint hex_length; 294 u32 hex_length;
295 acpi_native_uint decimal_length; 295 u32 decimal_length;
296 u32 remainder; 296 u32 remainder;
297 u8 supress_zeros; 297 u8 supress_zeros;
298 298
@@ -348,7 +348,7 @@ acpi_ex_convert_to_ascii(acpi_integer integer,
348 348
349 /* hex_length: 2 ascii hex chars per data byte */ 349 /* hex_length: 2 ascii hex chars per data byte */
350 350
351 hex_length = (acpi_native_uint) ACPI_MUL_2(data_width); 351 hex_length = ACPI_MUL_2(data_width);
352 for (i = 0, j = (hex_length - 1); i < hex_length; i++, j--) { 352 for (i = 0, j = (hex_length - 1); i < hex_length; i++, j--) {
353 353
354 /* Get one hex digit, most significant digits first */ 354 /* Get one hex digit, most significant digits first */
diff --git a/drivers/acpi/executer/excreate.c b/drivers/acpi/executer/excreate.c
index 60e62c4f0577..ad09696d5069 100644
--- a/drivers/acpi/executer/excreate.c
+++ b/drivers/acpi/executer/excreate.c
@@ -45,8 +45,6 @@
45#include <acpi/acinterp.h> 45#include <acpi/acinterp.h>
46#include <acpi/amlcode.h> 46#include <acpi/amlcode.h>
47#include <acpi/acnamesp.h> 47#include <acpi/acnamesp.h>
48#include <acpi/acevents.h>
49#include <acpi/actables.h>
50 48
51#define _COMPONENT ACPI_EXECUTER 49#define _COMPONENT ACPI_EXECUTER
52ACPI_MODULE_NAME("excreate") 50ACPI_MODULE_NAME("excreate")
diff --git a/drivers/acpi/executer/exdump.c b/drivers/acpi/executer/exdump.c
index 74f1b22601b3..2be2e2bf95bf 100644
--- a/drivers/acpi/executer/exdump.c
+++ b/drivers/acpi/executer/exdump.c
@@ -580,25 +580,22 @@ void acpi_ex_dump_operand(union acpi_operand_object *obj_desc, u32 depth)
580 580
581 case ACPI_TYPE_BUFFER: 581 case ACPI_TYPE_BUFFER:
582 582
583 acpi_os_printf("Buffer len %X @ %p\n", 583 acpi_os_printf("Buffer length %.2X @ %p\n",
584 obj_desc->buffer.length, 584 obj_desc->buffer.length,
585 obj_desc->buffer.pointer); 585 obj_desc->buffer.pointer);
586 586
587 length = obj_desc->buffer.length;
588 if (length > 64) {
589 length = 64;
590 }
591
592 /* Debug only -- dump the buffer contents */ 587 /* Debug only -- dump the buffer contents */
593 588
594 if (obj_desc->buffer.pointer) { 589 if (obj_desc->buffer.pointer) {
595 acpi_os_printf("Buffer Contents: "); 590 length = obj_desc->buffer.length;
596 591 if (length > 128) {
597 for (index = 0; index < length; index++) { 592 length = 128;
598 acpi_os_printf(" %02x",
599 obj_desc->buffer.pointer[index]);
600 } 593 }
601 acpi_os_printf("\n"); 594
595 acpi_os_printf
596 ("Buffer Contents: (displaying length 0x%.2X)\n",
597 length);
598 ACPI_DUMP_BUFFER(obj_desc->buffer.pointer, length);
602 } 599 }
603 break; 600 break;
604 601
@@ -756,54 +753,42 @@ void acpi_ex_dump_operand(union acpi_operand_object *obj_desc, u32 depth)
756 * 753 *
757 * FUNCTION: acpi_ex_dump_operands 754 * FUNCTION: acpi_ex_dump_operands
758 * 755 *
759 * PARAMETERS: Operands - Operand list 756 * PARAMETERS: Operands - A list of Operand objects
760 * interpreter_mode - Load or Exec 757 * opcode_name - AML opcode name
761 * Ident - Identification 758 * num_operands - Operand count for this opcode
762 * num_levels - # of stack entries to dump above line
763 * Note - Output notation
764 * module_name - Caller's module name
765 * line_number - Caller's invocation line number
766 * 759 *
767 * DESCRIPTION: Dump the object stack 760 * DESCRIPTION: Dump the operands associated with the opcode
768 * 761 *
769 ******************************************************************************/ 762 ******************************************************************************/
770 763
771void 764void
772acpi_ex_dump_operands(union acpi_operand_object **operands, 765acpi_ex_dump_operands(union acpi_operand_object **operands,
773 acpi_interpreter_mode interpreter_mode, 766 const char *opcode_name, u32 num_operands)
774 char *ident,
775 u32 num_levels,
776 char *note, char *module_name, u32 line_number)
777{ 767{
778 acpi_native_uint i;
779
780 ACPI_FUNCTION_NAME(ex_dump_operands); 768 ACPI_FUNCTION_NAME(ex_dump_operands);
781 769
782 if (!ident) { 770 if (!opcode_name) {
783 ident = "?"; 771 opcode_name = "UNKNOWN";
784 }
785
786 if (!note) {
787 note = "?";
788 } 772 }
789 773
790 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, 774 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
791 "************* Operand Stack Contents (Opcode [%s], %d Operands)\n", 775 "**** Start operand dump for opcode [%s], %d operands\n",
792 ident, num_levels)); 776 opcode_name, num_operands));
793 777
794 if (num_levels == 0) { 778 if (num_operands == 0) {
795 num_levels = 1; 779 num_operands = 1;
796 } 780 }
797 781
798 /* Dump the operand stack starting at the top */ 782 /* Dump the individual operands */
799 783
800 for (i = 0; num_levels > 0; i--, num_levels--) { 784 while (num_operands) {
801 acpi_ex_dump_operand(operands[i], 0); 785 acpi_ex_dump_operand(*operands, 0);
786 operands++;
787 num_operands--;
802 } 788 }
803 789
804 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, 790 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
805 "************* Operand Stack dump from %s(%d), %s\n", 791 "**** End operand dump for [%s]\n", opcode_name));
806 module_name, line_number, note));
807 return; 792 return;
808} 793}
809 794
diff --git a/drivers/acpi/executer/exfldio.c b/drivers/acpi/executer/exfldio.c
index e336b5dc7a50..9ff9d1f4615d 100644
--- a/drivers/acpi/executer/exfldio.c
+++ b/drivers/acpi/executer/exfldio.c
@@ -153,14 +153,15 @@ acpi_ex_setup_region(union acpi_operand_object *obj_desc,
153 /* 153 /*
154 * Slack mode only: We will go ahead and allow access to this 154 * Slack mode only: We will go ahead and allow access to this
155 * field if it is within the region length rounded up to the next 155 * field if it is within the region length rounded up to the next
156 * access width boundary. 156 * access width boundary. acpi_size cast for 64-bit compile.
157 */ 157 */
158 if (ACPI_ROUND_UP(rgn_desc->region.length, 158 if (ACPI_ROUND_UP(rgn_desc->region.length,
159 obj_desc->common_field. 159 obj_desc->common_field.
160 access_byte_width) >= 160 access_byte_width) >=
161 (obj_desc->common_field.base_byte_offset + 161 ((acpi_size) obj_desc->common_field.
162 (acpi_native_uint) obj_desc->common_field. 162 base_byte_offset +
163 access_byte_width + field_datum_byte_offset)) { 163 obj_desc->common_field.access_byte_width +
164 field_datum_byte_offset)) {
164 return_ACPI_STATUS(AE_OK); 165 return_ACPI_STATUS(AE_OK);
165 } 166 }
166 } 167 }
diff --git a/drivers/acpi/executer/exmisc.c b/drivers/acpi/executer/exmisc.c
index cc956a5b5267..731414a581a6 100644
--- a/drivers/acpi/executer/exmisc.c
+++ b/drivers/acpi/executer/exmisc.c
@@ -329,8 +329,8 @@ acpi_ex_do_concatenate(union acpi_operand_object *operand0,
329 329
330 /* Result of two Strings is a String */ 330 /* Result of two Strings is a String */
331 331
332 return_desc = acpi_ut_create_string_object((acpi_size) 332 return_desc = acpi_ut_create_string_object(((acpi_size)
333 (operand0->string. 333 operand0->string.
334 length + 334 length +
335 local_operand1-> 335 local_operand1->
336 string.length)); 336 string.length));
@@ -352,8 +352,8 @@ acpi_ex_do_concatenate(union acpi_operand_object *operand0,
352 352
353 /* Result of two Buffers is a Buffer */ 353 /* Result of two Buffers is a Buffer */
354 354
355 return_desc = acpi_ut_create_buffer_object((acpi_size) 355 return_desc = acpi_ut_create_buffer_object(((acpi_size)
356 (operand0->buffer. 356 operand0->buffer.
357 length + 357 length +
358 local_operand1-> 358 local_operand1->
359 buffer.length)); 359 buffer.length));
diff --git a/drivers/acpi/executer/exprep.c b/drivers/acpi/executer/exprep.c
index 3a2f8cd4c62a..5d438c32989d 100644
--- a/drivers/acpi/executer/exprep.c
+++ b/drivers/acpi/executer/exprep.c
@@ -503,11 +503,11 @@ acpi_status acpi_ex_prep_field_value(struct acpi_create_field_info *info)
503 */ 503 */
504 second_desc = obj_desc->common.next_object; 504 second_desc = obj_desc->common.next_object;
505 second_desc->extra.aml_start = 505 second_desc->extra.aml_start =
506 ((union acpi_parse_object *)(info->data_register_node))-> 506 ACPI_CAST_PTR(union acpi_parse_object,
507 named.data; 507 info->data_register_node)->named.data;
508 second_desc->extra.aml_length = 508 second_desc->extra.aml_length =
509 ((union acpi_parse_object *)(info->data_register_node))-> 509 ACPI_CAST_PTR(union acpi_parse_object,
510 named.length; 510 info->data_register_node)->named.length;
511 511
512 break; 512 break;
513 513
diff --git a/drivers/acpi/executer/exregion.c b/drivers/acpi/executer/exregion.c
index 7cd8bb54fa01..7a41c409ae4d 100644
--- a/drivers/acpi/executer/exregion.c
+++ b/drivers/acpi/executer/exregion.c
@@ -156,7 +156,7 @@ acpi_ex_system_memory_space_handler(u32 function,
156 /* Create a new mapping starting at the address given */ 156 /* Create a new mapping starting at the address given */
157 157
158 mem_info->mapped_logical_address = 158 mem_info->mapped_logical_address =
159 acpi_os_map_memory((acpi_native_uint) address, window_size); 159 acpi_os_map_memory((acpi_physical_address) address, window_size);
160 if (!mem_info->mapped_logical_address) { 160 if (!mem_info->mapped_logical_address) {
161 ACPI_ERROR((AE_INFO, 161 ACPI_ERROR((AE_INFO,
162 "Could not map memory at %8.8X%8.8X, size %X", 162 "Could not map memory at %8.8X%8.8X, size %X",
diff --git a/drivers/acpi/executer/exresop.c b/drivers/acpi/executer/exresop.c
index 73e29e566a70..54085f16ec28 100644
--- a/drivers/acpi/executer/exresop.c
+++ b/drivers/acpi/executer/exresop.c
@@ -698,5 +698,9 @@ acpi_ex_resolve_operands(u16 opcode,
698 } 698 }
699 } 699 }
700 700
701 ACPI_DUMP_OPERANDS(walk_state->operands,
702 acpi_ps_get_opcode_name(opcode),
703 walk_state->num_operands);
704
701 return_ACPI_STATUS(status); 705 return_ACPI_STATUS(status);
702} 706}
diff --git a/drivers/acpi/executer/exstore.c b/drivers/acpi/executer/exstore.c
index 76c875bc3154..38b55e352495 100644
--- a/drivers/acpi/executer/exstore.c
+++ b/drivers/acpi/executer/exstore.c
@@ -343,12 +343,6 @@ acpi_ex_store(union acpi_operand_object *source_desc,
343 acpi_ut_get_object_type_name(dest_desc), 343 acpi_ut_get_object_type_name(dest_desc),
344 dest_desc)); 344 dest_desc));
345 345
346 ACPI_DUMP_STACK_ENTRY(source_desc);
347 ACPI_DUMP_STACK_ENTRY(dest_desc);
348 ACPI_DUMP_OPERANDS(&dest_desc, ACPI_IMODE_EXECUTE, "ExStore",
349 2,
350 "Target is not a Reference or Constant object");
351
352 return_ACPI_STATUS(AE_AML_OPERAND_TYPE); 346 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
353 } 347 }
354 348
diff --git a/drivers/acpi/fan.c b/drivers/acpi/fan.c
index 6cf10cbc1eee..55c17afbe669 100644
--- a/drivers/acpi/fan.c
+++ b/drivers/acpi/fan.c
@@ -148,7 +148,7 @@ acpi_fan_write_state(struct file *file, const char __user * buffer,
148 int result = 0; 148 int result = 0;
149 struct seq_file *m = file->private_data; 149 struct seq_file *m = file->private_data;
150 struct acpi_device *device = m->private; 150 struct acpi_device *device = m->private;
151 char state_string[12] = { '\0' }; 151 char state_string[3] = { '\0' };
152 152
153 if (count > sizeof(state_string) - 1) 153 if (count > sizeof(state_string) - 1)
154 return -EINVAL; 154 return -EINVAL;
@@ -157,6 +157,12 @@ acpi_fan_write_state(struct file *file, const char __user * buffer,
157 return -EFAULT; 157 return -EFAULT;
158 158
159 state_string[count] = '\0'; 159 state_string[count] = '\0';
160 if ((state_string[0] < '0') || (state_string[0] > '3'))
161 return -EINVAL;
162 if (state_string[1] == '\n')
163 state_string[1] = '\0';
164 if (state_string[1] != '\0')
165 return -EINVAL;
160 166
161 result = acpi_bus_set_power(device->handle, 167 result = acpi_bus_set_power(device->handle,
162 simple_strtoul(state_string, NULL, 0)); 168 simple_strtoul(state_string, NULL, 0));
diff --git a/drivers/acpi/glue.c b/drivers/acpi/glue.c
index 9b227d4dc9c9..0f2dd81736bd 100644
--- a/drivers/acpi/glue.c
+++ b/drivers/acpi/glue.c
@@ -166,6 +166,8 @@ static int acpi_bind_one(struct device *dev, acpi_handle handle)
166 "firmware_node"); 166 "firmware_node");
167 ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj, 167 ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
168 "physical_node"); 168 "physical_node");
169 if (acpi_dev->wakeup.flags.valid)
170 device_set_wakeup_capable(dev, true);
169 } 171 }
170 172
171 return 0; 173 return 0;
@@ -336,6 +338,9 @@ static int __init acpi_rtc_init(void)
336 if (acpi_disabled) 338 if (acpi_disabled)
337 return 0; 339 return 0;
338 340
341 if (acpi_disabled)
342 return 0;
343
339 if (dev) { 344 if (dev) {
340 rtc_wake_setup(); 345 rtc_wake_setup();
341 rtc_info.wake_on = rtc_wake_on; 346 rtc_info.wake_on = rtc_wake_on;
diff --git a/drivers/acpi/hardware/hwgpe.c b/drivers/acpi/hardware/hwgpe.c
index 14bc4f456ae8..0b80db9d9197 100644
--- a/drivers/acpi/hardware/hwgpe.c
+++ b/drivers/acpi/hardware/hwgpe.c
@@ -55,6 +55,54 @@ acpi_hw_enable_wakeup_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
55 55
56/****************************************************************************** 56/******************************************************************************
57 * 57 *
58 * FUNCTION: acpi_hw_low_disable_gpe
59 *
60 * PARAMETERS: gpe_event_info - Info block for the GPE to be disabled
61 *
62 * RETURN: Status
63 *
64 * DESCRIPTION: Disable a single GPE in the enable register.
65 *
66 ******************************************************************************/
67
68acpi_status acpi_hw_low_disable_gpe(struct acpi_gpe_event_info *gpe_event_info)
69{
70 struct acpi_gpe_register_info *gpe_register_info;
71 acpi_status status;
72 u32 enable_mask;
73
74 /* Get the info block for the entire GPE register */
75
76 gpe_register_info = gpe_event_info->register_info;
77 if (!gpe_register_info) {
78 return (AE_NOT_EXIST);
79 }
80
81 /* Get current value of the enable register that contains this GPE */
82
83 status = acpi_hw_low_level_read(ACPI_GPE_REGISTER_WIDTH, &enable_mask,
84 &gpe_register_info->enable_address);
85 if (ACPI_FAILURE(status)) {
86 return (status);
87 }
88
89 /* Clear just the bit that corresponds to this GPE */
90
91 ACPI_CLEAR_BIT(enable_mask,
92 ((u32) 1 <<
93 (gpe_event_info->gpe_number -
94 gpe_register_info->base_gpe_number)));
95
96 /* Write the updated enable mask */
97
98 status = acpi_hw_low_level_write(ACPI_GPE_REGISTER_WIDTH, enable_mask,
99 &gpe_register_info->enable_address);
100
101 return (status);
102}
103
104/******************************************************************************
105 *
58 * FUNCTION: acpi_hw_write_gpe_enable_reg 106 * FUNCTION: acpi_hw_write_gpe_enable_reg
59 * 107 *
60 * PARAMETERS: gpe_event_info - Info block for the GPE to be enabled 108 * PARAMETERS: gpe_event_info - Info block for the GPE to be enabled
@@ -68,7 +116,7 @@ acpi_hw_enable_wakeup_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
68 ******************************************************************************/ 116 ******************************************************************************/
69 117
70acpi_status 118acpi_status
71acpi_hw_write_gpe_enable_reg(struct acpi_gpe_event_info *gpe_event_info) 119acpi_hw_write_gpe_enable_reg(struct acpi_gpe_event_info * gpe_event_info)
72{ 120{
73 struct acpi_gpe_register_info *gpe_register_info; 121 struct acpi_gpe_register_info *gpe_register_info;
74 acpi_status status; 122 acpi_status status;
@@ -138,7 +186,6 @@ acpi_status acpi_hw_clear_gpe(struct acpi_gpe_event_info * gpe_event_info)
138 * 186 *
139 ******************************************************************************/ 187 ******************************************************************************/
140 188
141#ifdef ACPI_FUTURE_USAGE
142acpi_status 189acpi_status
143acpi_hw_get_gpe_status(struct acpi_gpe_event_info * gpe_event_info, 190acpi_hw_get_gpe_status(struct acpi_gpe_event_info * gpe_event_info,
144 acpi_event_status * event_status) 191 acpi_event_status * event_status)
@@ -198,7 +245,6 @@ acpi_hw_get_gpe_status(struct acpi_gpe_event_info * gpe_event_info,
198 unlock_and_exit: 245 unlock_and_exit:
199 return (status); 246 return (status);
200} 247}
201#endif /* ACPI_FUTURE_USAGE */
202 248
203/****************************************************************************** 249/******************************************************************************
204 * 250 *
diff --git a/drivers/acpi/namespace/nsdump.c b/drivers/acpi/namespace/nsdump.c
index 5445751b8a3e..0ab22004728a 100644
--- a/drivers/acpi/namespace/nsdump.c
+++ b/drivers/acpi/namespace/nsdump.c
@@ -73,7 +73,7 @@ acpi_ns_dump_one_device(acpi_handle obj_handle,
73 73
74void acpi_ns_print_pathname(u32 num_segments, char *pathname) 74void acpi_ns_print_pathname(u32 num_segments, char *pathname)
75{ 75{
76 acpi_native_uint i; 76 u32 i;
77 77
78 ACPI_FUNCTION_NAME(ns_print_pathname); 78 ACPI_FUNCTION_NAME(ns_print_pathname);
79 79
@@ -515,12 +515,12 @@ acpi_ns_dump_one_object(acpi_handle obj_handle,
515 515
516 if (obj_type > ACPI_TYPE_LOCAL_MAX) { 516 if (obj_type > ACPI_TYPE_LOCAL_MAX) {
517 acpi_os_printf 517 acpi_os_printf
518 ("(Ptr to ACPI Object type %X [UNKNOWN])\n", 518 ("(Pointer to ACPI Object type %.2X [UNKNOWN])\n",
519 obj_type); 519 obj_type);
520 bytes_to_dump = 32; 520 bytes_to_dump = 32;
521 } else { 521 } else {
522 acpi_os_printf 522 acpi_os_printf
523 ("(Ptr to ACPI Object type %X [%s])\n", 523 ("(Pointer to ACPI Object type %.2X [%s])\n",
524 obj_type, acpi_ut_get_type_name(obj_type)); 524 obj_type, acpi_ut_get_type_name(obj_type));
525 bytes_to_dump = 525 bytes_to_dump =
526 sizeof(union acpi_operand_object); 526 sizeof(union acpi_operand_object);
diff --git a/drivers/acpi/namespace/nseval.c b/drivers/acpi/namespace/nseval.c
index 14bdfa92bea0..d369164e00b0 100644
--- a/drivers/acpi/namespace/nseval.c
+++ b/drivers/acpi/namespace/nseval.c
@@ -138,6 +138,41 @@ acpi_status acpi_ns_evaluate(struct acpi_evaluate_info * info)
138 return_ACPI_STATUS(AE_NULL_OBJECT); 138 return_ACPI_STATUS(AE_NULL_OBJECT);
139 } 139 }
140 140
141 /*
142 * Calculate the number of arguments being passed to the method
143 */
144
145 info->param_count = 0;
146 if (info->parameters) {
147 while (info->parameters[info->param_count])
148 info->param_count++;
149 }
150
151 /* Error if too few arguments were passed in */
152
153 if (info->param_count < info->obj_desc->method.param_count) {
154 ACPI_ERROR((AE_INFO,
155 "Insufficient arguments - "
156 "method [%4.4s] needs %d, found %d",
157 acpi_ut_get_node_name(info->resolved_node),
158 info->obj_desc->method.param_count,
159 info->param_count));
160 return_ACPI_STATUS(AE_MISSING_ARGUMENTS);
161 }
162
163 /* Just a warning if too many arguments */
164
165 else if (info->param_count >
166 info->obj_desc->method.param_count) {
167 ACPI_WARNING((AE_INFO,
168 "Excess arguments - "
169 "method [%4.4s] needs %d, found %d",
170 acpi_ut_get_node_name(info->
171 resolved_node),
172 info->obj_desc->method.param_count,
173 info->param_count));
174 }
175
141 ACPI_DUMP_PATHNAME(info->resolved_node, "Execute Method:", 176 ACPI_DUMP_PATHNAME(info->resolved_node, "Execute Method:",
142 ACPI_LV_INFO, _COMPONENT); 177 ACPI_LV_INFO, _COMPONENT);
143 178
diff --git a/drivers/acpi/namespace/nsinit.c b/drivers/acpi/namespace/nsinit.c
index 6d6d930c8e18..e4c57510d798 100644
--- a/drivers/acpi/namespace/nsinit.c
+++ b/drivers/acpi/namespace/nsinit.c
@@ -542,7 +542,6 @@ acpi_ns_init_one_device(acpi_handle obj_handle,
542 info->prefix_node = device_node; 542 info->prefix_node = device_node;
543 info->pathname = METHOD_NAME__INI; 543 info->pathname = METHOD_NAME__INI;
544 info->parameters = NULL; 544 info->parameters = NULL;
545 info->parameter_type = ACPI_PARAM_ARGS;
546 info->flags = ACPI_IGNORE_RETURN_VALUE; 545 info->flags = ACPI_IGNORE_RETURN_VALUE;
547 546
548 /* 547 /*
diff --git a/drivers/acpi/namespace/nsload.c b/drivers/acpi/namespace/nsload.c
index 2c92f6cf5ce1..a4a412b7c029 100644
--- a/drivers/acpi/namespace/nsload.c
+++ b/drivers/acpi/namespace/nsload.c
@@ -71,8 +71,7 @@ static acpi_status acpi_ns_delete_subtree(acpi_handle start_handle);
71 ******************************************************************************/ 71 ******************************************************************************/
72 72
73acpi_status 73acpi_status
74acpi_ns_load_table(acpi_native_uint table_index, 74acpi_ns_load_table(u32 table_index, struct acpi_namespace_node *node)
75 struct acpi_namespace_node *node)
76{ 75{
77 acpi_status status; 76 acpi_status status;
78 77
diff --git a/drivers/acpi/namespace/nsparse.c b/drivers/acpi/namespace/nsparse.c
index 46a79b0103b6..a82271a9dbb3 100644
--- a/drivers/acpi/namespace/nsparse.c
+++ b/drivers/acpi/namespace/nsparse.c
@@ -63,13 +63,13 @@ ACPI_MODULE_NAME("nsparse")
63 * 63 *
64 ******************************************************************************/ 64 ******************************************************************************/
65acpi_status 65acpi_status
66acpi_ns_one_complete_parse(acpi_native_uint pass_number, 66acpi_ns_one_complete_parse(u32 pass_number,
67 acpi_native_uint table_index, 67 u32 table_index,
68 struct acpi_namespace_node * start_node) 68 struct acpi_namespace_node *start_node)
69{ 69{
70 union acpi_parse_object *parse_root; 70 union acpi_parse_object *parse_root;
71 acpi_status status; 71 acpi_status status;
72 acpi_native_uint aml_length; 72 u32 aml_length;
73 u8 *aml_start; 73 u8 *aml_start;
74 struct acpi_walk_state *walk_state; 74 struct acpi_walk_state *walk_state;
75 struct acpi_table_header *table; 75 struct acpi_table_header *table;
@@ -112,8 +112,8 @@ acpi_ns_one_complete_parse(acpi_native_uint pass_number,
112 aml_start = (u8 *) table + sizeof(struct acpi_table_header); 112 aml_start = (u8 *) table + sizeof(struct acpi_table_header);
113 aml_length = table->length - sizeof(struct acpi_table_header); 113 aml_length = table->length - sizeof(struct acpi_table_header);
114 status = acpi_ds_init_aml_walk(walk_state, parse_root, NULL, 114 status = acpi_ds_init_aml_walk(walk_state, parse_root, NULL,
115 aml_start, (u32) aml_length, 115 aml_start, aml_length, NULL,
116 NULL, (u8) pass_number); 116 (u8) pass_number);
117 } 117 }
118 118
119 if (ACPI_FAILURE(status)) { 119 if (ACPI_FAILURE(status)) {
@@ -158,8 +158,7 @@ acpi_ns_one_complete_parse(acpi_native_uint pass_number,
158 ******************************************************************************/ 158 ******************************************************************************/
159 159
160acpi_status 160acpi_status
161acpi_ns_parse_table(acpi_native_uint table_index, 161acpi_ns_parse_table(u32 table_index, struct acpi_namespace_node *start_node)
162 struct acpi_namespace_node *start_node)
163{ 162{
164 acpi_status status; 163 acpi_status status;
165 164
diff --git a/drivers/acpi/namespace/nsutils.c b/drivers/acpi/namespace/nsutils.c
index 64c039843ed2..b0817e1127b1 100644
--- a/drivers/acpi/namespace/nsutils.c
+++ b/drivers/acpi/namespace/nsutils.c
@@ -73,9 +73,9 @@ acpi_name acpi_ns_find_parent_name(struct acpi_namespace_node *node_to_search);
73 ******************************************************************************/ 73 ******************************************************************************/
74 74
75void 75void
76acpi_ns_report_error(char *module_name, 76acpi_ns_report_error(const char *module_name,
77 u32 line_number, 77 u32 line_number,
78 char *internal_name, acpi_status lookup_status) 78 const char *internal_name, acpi_status lookup_status)
79{ 79{
80 acpi_status status; 80 acpi_status status;
81 u32 bad_name; 81 u32 bad_name;
@@ -130,11 +130,11 @@ acpi_ns_report_error(char *module_name,
130 ******************************************************************************/ 130 ******************************************************************************/
131 131
132void 132void
133acpi_ns_report_method_error(char *module_name, 133acpi_ns_report_method_error(const char *module_name,
134 u32 line_number, 134 u32 line_number,
135 char *message, 135 const char *message,
136 struct acpi_namespace_node *prefix_node, 136 struct acpi_namespace_node *prefix_node,
137 char *path, acpi_status method_status) 137 const char *path, acpi_status method_status)
138{ 138{
139 acpi_status status; 139 acpi_status status;
140 struct acpi_namespace_node *node = prefix_node; 140 struct acpi_namespace_node *node = prefix_node;
@@ -167,7 +167,8 @@ acpi_ns_report_method_error(char *module_name,
167 ******************************************************************************/ 167 ******************************************************************************/
168 168
169void 169void
170acpi_ns_print_node_pathname(struct acpi_namespace_node *node, char *message) 170acpi_ns_print_node_pathname(struct acpi_namespace_node *node,
171 const char *message)
171{ 172{
172 struct acpi_buffer buffer; 173 struct acpi_buffer buffer;
173 acpi_status status; 174 acpi_status status;
@@ -296,7 +297,7 @@ u32 acpi_ns_local(acpi_object_type type)
296 297
297void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info) 298void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info)
298{ 299{
299 char *next_external_char; 300 const char *next_external_char;
300 u32 i; 301 u32 i;
301 302
302 ACPI_FUNCTION_ENTRY(); 303 ACPI_FUNCTION_ENTRY();
@@ -363,9 +364,9 @@ acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info)
363{ 364{
364 u32 num_segments = info->num_segments; 365 u32 num_segments = info->num_segments;
365 char *internal_name = info->internal_name; 366 char *internal_name = info->internal_name;
366 char *external_name = info->next_external_char; 367 const char *external_name = info->next_external_char;
367 char *result = NULL; 368 char *result = NULL;
368 acpi_native_uint i; 369 u32 i;
369 370
370 ACPI_FUNCTION_TRACE(ns_build_internal_name); 371 ACPI_FUNCTION_TRACE(ns_build_internal_name);
371 372
@@ -400,12 +401,11 @@ acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info)
400 result = &internal_name[i]; 401 result = &internal_name[i];
401 } else if (num_segments == 2) { 402 } else if (num_segments == 2) {
402 internal_name[i] = AML_DUAL_NAME_PREFIX; 403 internal_name[i] = AML_DUAL_NAME_PREFIX;
403 result = &internal_name[(acpi_native_uint) (i + 1)]; 404 result = &internal_name[(acpi_size) i + 1];
404 } else { 405 } else {
405 internal_name[i] = AML_MULTI_NAME_PREFIX_OP; 406 internal_name[i] = AML_MULTI_NAME_PREFIX_OP;
406 internal_name[(acpi_native_uint) (i + 1)] = 407 internal_name[(acpi_size) i + 1] = (char)num_segments;
407 (char)num_segments; 408 result = &internal_name[(acpi_size) i + 2];
408 result = &internal_name[(acpi_native_uint) (i + 2)];
409 } 409 }
410 } 410 }
411 411
@@ -472,7 +472,8 @@ acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info)
472 * 472 *
473 *******************************************************************************/ 473 *******************************************************************************/
474 474
475acpi_status acpi_ns_internalize_name(char *external_name, char **converted_name) 475acpi_status
476acpi_ns_internalize_name(const char *external_name, char **converted_name)
476{ 477{
477 char *internal_name; 478 char *internal_name;
478 struct acpi_namestring_info info; 479 struct acpi_namestring_info info;
@@ -528,15 +529,15 @@ acpi_status acpi_ns_internalize_name(char *external_name, char **converted_name)
528 529
529acpi_status 530acpi_status
530acpi_ns_externalize_name(u32 internal_name_length, 531acpi_ns_externalize_name(u32 internal_name_length,
531 char *internal_name, 532 const char *internal_name,
532 u32 * converted_name_length, char **converted_name) 533 u32 * converted_name_length, char **converted_name)
533{ 534{
534 acpi_native_uint names_index = 0; 535 u32 names_index = 0;
535 acpi_native_uint num_segments = 0; 536 u32 num_segments = 0;
536 acpi_native_uint required_length; 537 u32 required_length;
537 acpi_native_uint prefix_length = 0; 538 u32 prefix_length = 0;
538 acpi_native_uint i = 0; 539 u32 i = 0;
539 acpi_native_uint j = 0; 540 u32 j = 0;
540 541
541 ACPI_FUNCTION_TRACE(ns_externalize_name); 542 ACPI_FUNCTION_TRACE(ns_externalize_name);
542 543
@@ -582,9 +583,8 @@ acpi_ns_externalize_name(u32 internal_name_length,
582 /* <count> 4-byte names */ 583 /* <count> 4-byte names */
583 584
584 names_index = prefix_length + 2; 585 names_index = prefix_length + 2;
585 num_segments = (acpi_native_uint) (u8) 586 num_segments = (u8)
586 internal_name[(acpi_native_uint) 587 internal_name[(acpi_size) prefix_length + 1];
587 (prefix_length + 1)];
588 break; 588 break;
589 589
590 case AML_DUAL_NAME_PREFIX: 590 case AML_DUAL_NAME_PREFIX:
@@ -823,7 +823,7 @@ u32 acpi_ns_opens_scope(acpi_object_type type)
823 823
824acpi_status 824acpi_status
825acpi_ns_get_node(struct acpi_namespace_node *prefix_node, 825acpi_ns_get_node(struct acpi_namespace_node *prefix_node,
826 char *pathname, 826 const char *pathname,
827 u32 flags, struct acpi_namespace_node **return_node) 827 u32 flags, struct acpi_namespace_node **return_node)
828{ 828{
829 union acpi_generic_state scope_info; 829 union acpi_generic_state scope_info;
diff --git a/drivers/acpi/namespace/nsxfeval.c b/drivers/acpi/namespace/nsxfeval.c
index a8d549187c84..38be5865d95d 100644
--- a/drivers/acpi/namespace/nsxfeval.c
+++ b/drivers/acpi/namespace/nsxfeval.c
@@ -182,7 +182,6 @@ acpi_evaluate_object(acpi_handle handle,
182 } 182 }
183 183
184 info->pathname = pathname; 184 info->pathname = pathname;
185 info->parameter_type = ACPI_PARAM_ARGS;
186 185
187 /* Convert and validate the device handle */ 186 /* Convert and validate the device handle */
188 187
@@ -442,7 +441,7 @@ acpi_ns_get_device_callback(acpi_handle obj_handle,
442 u32 flags; 441 u32 flags;
443 struct acpica_device_id hid; 442 struct acpica_device_id hid;
444 struct acpi_compatible_id_list *cid; 443 struct acpi_compatible_id_list *cid;
445 acpi_native_uint i; 444 u32 i;
446 int found; 445 int found;
447 446
448 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 447 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
diff --git a/drivers/acpi/numa.c b/drivers/acpi/numa.c
index 658e5f3abae0..cb9864e39bae 100644
--- a/drivers/acpi/numa.c
+++ b/drivers/acpi/numa.c
@@ -120,10 +120,10 @@ acpi_table_print_srat_entry(struct acpi_subtable_header *header)
120 struct acpi_srat_mem_affinity *p = 120 struct acpi_srat_mem_affinity *p =
121 (struct acpi_srat_mem_affinity *)header; 121 (struct acpi_srat_mem_affinity *)header;
122 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 122 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
123 "SRAT Memory (0x%lx length 0x%lx type 0x%x) in proximity domain %d %s%s\n", 123 "SRAT Memory (0x%lx length 0x%lx) in proximity domain %d %s%s\n",
124 (unsigned long)p->base_address, 124 (unsigned long)p->base_address,
125 (unsigned long)p->length, 125 (unsigned long)p->length,
126 p->memory_type, p->proximity_domain, 126 p->proximity_domain,
127 (p->flags & ACPI_SRAT_MEM_ENABLED)? 127 (p->flags & ACPI_SRAT_MEM_ENABLED)?
128 "enabled" : "disabled", 128 "enabled" : "disabled",
129 (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)? 129 (p->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)?
diff --git a/drivers/acpi/parser/psargs.c b/drivers/acpi/parser/psargs.c
index e94463778845..d830b29b85b1 100644
--- a/drivers/acpi/parser/psargs.c
+++ b/drivers/acpi/parser/psargs.c
@@ -76,7 +76,7 @@ acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
76{ 76{
77 u8 *aml = parser_state->aml; 77 u8 *aml = parser_state->aml;
78 u32 package_length = 0; 78 u32 package_length = 0;
79 acpi_native_uint byte_count; 79 u32 byte_count;
80 u8 byte_zero_mask = 0x3F; /* Default [0:5] */ 80 u8 byte_zero_mask = 0x3F; /* Default [0:5] */
81 81
82 ACPI_FUNCTION_TRACE(ps_get_next_package_length); 82 ACPI_FUNCTION_TRACE(ps_get_next_package_length);
@@ -86,7 +86,7 @@ acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
86 * used to encode the package length, either 0,1,2, or 3 86 * used to encode the package length, either 0,1,2, or 3
87 */ 87 */
88 byte_count = (aml[0] >> 6); 88 byte_count = (aml[0] >> 6);
89 parser_state->aml += (byte_count + 1); 89 parser_state->aml += ((acpi_size) byte_count + 1);
90 90
91 /* Get bytes 3, 2, 1 as needed */ 91 /* Get bytes 3, 2, 1 as needed */
92 92
diff --git a/drivers/acpi/parser/psxface.c b/drivers/acpi/parser/psxface.c
index 52581454c47c..270469aae842 100644
--- a/drivers/acpi/parser/psxface.c
+++ b/drivers/acpi/parser/psxface.c
@@ -333,9 +333,9 @@ acpi_status acpi_ps_execute_method(struct acpi_evaluate_info *info)
333static void 333static void
334acpi_ps_update_parameter_list(struct acpi_evaluate_info *info, u16 action) 334acpi_ps_update_parameter_list(struct acpi_evaluate_info *info, u16 action)
335{ 335{
336 acpi_native_uint i; 336 u32 i;
337 337
338 if ((info->parameter_type == ACPI_PARAM_ARGS) && (info->parameters)) { 338 if (info->parameters) {
339 339
340 /* Update reference count for each parameter */ 340 /* Update reference count for each parameter */
341 341
diff --git a/drivers/acpi/pci_irq.c b/drivers/acpi/pci_irq.c
index 89022a74faee..11acaee14d66 100644
--- a/drivers/acpi/pci_irq.c
+++ b/drivers/acpi/pci_irq.c
@@ -162,7 +162,7 @@ do_prt_fixups(struct acpi_prt_entry *entry, struct acpi_pci_routing_table *prt)
162 !strcmp(prt->source, quirk->source) && 162 !strcmp(prt->source, quirk->source) &&
163 strlen(prt->source) >= strlen(quirk->actual_source)) { 163 strlen(prt->source) >= strlen(quirk->actual_source)) {
164 printk(KERN_WARNING PREFIX "firmware reports " 164 printk(KERN_WARNING PREFIX "firmware reports "
165 "%04x:%02x:%02x[%c] connected to %s; " 165 "%04x:%02x:%02x PCI INT %c connected to %s; "
166 "changing to %s\n", 166 "changing to %s\n",
167 entry->id.segment, entry->id.bus, 167 entry->id.segment, entry->id.bus,
168 entry->id.device, 'A' + entry->pin, 168 entry->id.device, 'A' + entry->pin,
@@ -429,7 +429,7 @@ acpi_pci_irq_derive(struct pci_dev *dev,
429{ 429{
430 struct pci_dev *bridge = dev; 430 struct pci_dev *bridge = dev;
431 int irq = -1; 431 int irq = -1;
432 u8 bridge_pin = 0; 432 u8 bridge_pin = 0, orig_pin = pin;
433 433
434 434
435 if (!dev) 435 if (!dev)
@@ -463,8 +463,8 @@ acpi_pci_irq_derive(struct pci_dev *dev,
463 } 463 }
464 464
465 if (irq < 0) { 465 if (irq < 0) {
466 printk(KERN_WARNING PREFIX "Unable to derive IRQ for device %s\n", 466 dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
467 pci_name(dev)); 467 'A' + orig_pin);
468 return -1; 468 return -1;
469 } 469 }
470 470
@@ -487,6 +487,7 @@ int acpi_pci_irq_enable(struct pci_dev *dev)
487 int triggering = ACPI_LEVEL_SENSITIVE; 487 int triggering = ACPI_LEVEL_SENSITIVE;
488 int polarity = ACPI_ACTIVE_LOW; 488 int polarity = ACPI_ACTIVE_LOW;
489 char *link = NULL; 489 char *link = NULL;
490 char link_desc[16];
490 int rc; 491 int rc;
491 492
492 493
@@ -503,7 +504,7 @@ int acpi_pci_irq_enable(struct pci_dev *dev)
503 pin--; 504 pin--;
504 505
505 if (!dev->bus) { 506 if (!dev->bus) {
506 printk(KERN_ERR PREFIX "Invalid (NULL) 'bus' field\n"); 507 dev_err(&dev->dev, "invalid (NULL) 'bus' field\n");
507 return -ENODEV; 508 return -ENODEV;
508 } 509 }
509 510
@@ -538,8 +539,7 @@ int acpi_pci_irq_enable(struct pci_dev *dev)
538 * driver reported one, then use it. Exit in any case. 539 * driver reported one, then use it. Exit in any case.
539 */ 540 */
540 if (irq < 0) { 541 if (irq < 0) {
541 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: no GSI", 542 dev_warn(&dev->dev, "PCI INT %c: no GSI", 'A' + pin);
542 pci_name(dev), ('A' + pin));
543 /* Interrupt Line values above 0xF are forbidden */ 543 /* Interrupt Line values above 0xF are forbidden */
544 if (dev->irq > 0 && (dev->irq <= 0xF)) { 544 if (dev->irq > 0 && (dev->irq <= 0xF)) {
545 printk(" - using IRQ %d\n", dev->irq); 545 printk(" - using IRQ %d\n", dev->irq);
@@ -554,21 +554,21 @@ int acpi_pci_irq_enable(struct pci_dev *dev)
554 554
555 rc = acpi_register_gsi(irq, triggering, polarity); 555 rc = acpi_register_gsi(irq, triggering, polarity);
556 if (rc < 0) { 556 if (rc < 0) {
557 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: failed " 557 dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
558 "to register GSI\n", pci_name(dev), ('A' + pin)); 558 'A' + pin);
559 return rc; 559 return rc;
560 } 560 }
561 dev->irq = rc; 561 dev->irq = rc;
562 562
563 printk(KERN_INFO PREFIX "PCI Interrupt %s[%c] -> ",
564 pci_name(dev), 'A' + pin);
565
566 if (link) 563 if (link)
567 printk("Link [%s] -> ", link); 564 snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
565 else
566 link_desc[0] = '\0';
568 567
569 printk("GSI %u (%s, %s) -> IRQ %d\n", irq, 568 dev_info(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
570 (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge", 569 'A' + pin, link_desc, irq,
571 (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq); 570 (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
571 (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
572 572
573 return 0; 573 return 0;
574} 574}
@@ -616,10 +616,6 @@ void acpi_pci_irq_disable(struct pci_dev *dev)
616 * (e.g. PCI_UNDEFINED_IRQ). 616 * (e.g. PCI_UNDEFINED_IRQ).
617 */ 617 */
618 618
619 printk(KERN_INFO PREFIX "PCI interrupt for device %s disabled\n", 619 dev_info(&dev->dev, "PCI INT %c disabled\n", 'A' + pin);
620 pci_name(dev));
621
622 acpi_unregister_gsi(gsi); 620 acpi_unregister_gsi(gsi);
623
624 return;
625} 621}
diff --git a/drivers/acpi/pci_slot.c b/drivers/acpi/pci_slot.c
new file mode 100644
index 000000000000..b9ab030a52d5
--- /dev/null
+++ b/drivers/acpi/pci_slot.c
@@ -0,0 +1,368 @@
1/*
2 * pci_slot.c - ACPI PCI Slot Driver
3 *
4 * The code here is heavily leveraged from the acpiphp module.
5 * Thanks to Matthew Wilcox <matthew@wil.cx> for much guidance.
6 * Thanks to Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com> for code
7 * review and fixes.
8 *
9 * Copyright (C) 2007 Alex Chiang <achiang@hp.com>
10 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms and conditions of the GNU General Public License,
14 * version 2, as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/pci.h>
31#include <linux/acpi.h>
32#include <acpi/acpi_bus.h>
33#include <acpi/acpi_drivers.h>
34
35static int debug;
36static int check_sta_before_sun;
37
38#define DRIVER_VERSION "0.1"
39#define DRIVER_AUTHOR "Alex Chiang <achiang@hp.com>"
40#define DRIVER_DESC "ACPI PCI Slot Detection Driver"
41MODULE_AUTHOR(DRIVER_AUTHOR);
42MODULE_DESCRIPTION(DRIVER_DESC);
43MODULE_LICENSE("GPL");
44MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
45module_param(debug, bool, 0644);
46
47#define _COMPONENT ACPI_PCI_COMPONENT
48ACPI_MODULE_NAME("pci_slot");
49
50#define MY_NAME "pci_slot"
51#define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
52#define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
53#define dbg(format, arg...) \
54 do { \
55 if (debug) \
56 printk(KERN_DEBUG "%s: " format, \
57 MY_NAME , ## arg); \
58 } while (0)
59
60#define SLOT_NAME_SIZE 20 /* Inspired by #define in acpiphp.h */
61
62struct acpi_pci_slot {
63 acpi_handle root_handle; /* handle of the root bridge */
64 struct pci_slot *pci_slot; /* corresponding pci_slot */
65 struct list_head list; /* node in the list of slots */
66};
67
68static int acpi_pci_slot_add(acpi_handle handle);
69static void acpi_pci_slot_remove(acpi_handle handle);
70
71static LIST_HEAD(slot_list);
72static DEFINE_MUTEX(slot_list_lock);
73static struct acpi_pci_driver acpi_pci_slot_driver = {
74 .add = acpi_pci_slot_add,
75 .remove = acpi_pci_slot_remove,
76};
77
78static int
79check_slot(acpi_handle handle, int *device, unsigned long *sun)
80{
81 int retval = 0;
82 unsigned long adr, sta;
83 acpi_status status;
84 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
85
86 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
87 dbg("Checking slot on path: %s\n", (char *)buffer.pointer);
88
89 if (check_sta_before_sun) {
90 /* If SxFy doesn't have _STA, we just assume it's there */
91 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
92 if (ACPI_SUCCESS(status) && !(sta & ACPI_STA_DEVICE_PRESENT)) {
93 retval = -1;
94 goto out;
95 }
96 }
97
98 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
99 if (ACPI_FAILURE(status)) {
100 dbg("_ADR returned %d on %s\n", status, (char *)buffer.pointer);
101 retval = -1;
102 goto out;
103 }
104
105 *device = (adr >> 16) & 0xffff;
106
107 /* No _SUN == not a slot == bail */
108 status = acpi_evaluate_integer(handle, "_SUN", NULL, sun);
109 if (ACPI_FAILURE(status)) {
110 dbg("_SUN returned %d on %s\n", status, (char *)buffer.pointer);
111 retval = -1;
112 goto out;
113 }
114
115out:
116 kfree(buffer.pointer);
117 return retval;
118}
119
120struct callback_args {
121 acpi_walk_callback user_function; /* only for walk_p2p_bridge */
122 struct pci_bus *pci_bus;
123 acpi_handle root_handle;
124};
125
126/*
127 * register_slot
128 *
129 * Called once for each SxFy object in the namespace. Don't worry about
130 * calling pci_create_slot multiple times for the same pci_bus:device,
131 * since each subsequent call simply bumps the refcount on the pci_slot.
132 *
133 * The number of calls to pci_destroy_slot from unregister_slot is
134 * symmetrical.
135 */
136static acpi_status
137register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
138{
139 int device;
140 unsigned long sun;
141 char name[SLOT_NAME_SIZE];
142 struct acpi_pci_slot *slot;
143 struct pci_slot *pci_slot;
144 struct callback_args *parent_context = context;
145 struct pci_bus *pci_bus = parent_context->pci_bus;
146
147 if (check_slot(handle, &device, &sun))
148 return AE_OK;
149
150 slot = kmalloc(sizeof(*slot), GFP_KERNEL);
151 if (!slot) {
152 err("%s: cannot allocate memory\n", __func__);
153 return AE_OK;
154 }
155
156 snprintf(name, sizeof(name), "%u", (u32)sun);
157 pci_slot = pci_create_slot(pci_bus, device, name);
158 if (IS_ERR(pci_slot)) {
159 err("pci_create_slot returned %ld\n", PTR_ERR(pci_slot));
160 kfree(slot);
161 }
162
163 slot->root_handle = parent_context->root_handle;
164 slot->pci_slot = pci_slot;
165 INIT_LIST_HEAD(&slot->list);
166 mutex_lock(&slot_list_lock);
167 list_add(&slot->list, &slot_list);
168 mutex_unlock(&slot_list_lock);
169
170 dbg("pci_slot: %p, pci_bus: %x, device: %d, name: %s\n",
171 pci_slot, pci_bus->number, device, name);
172
173 return AE_OK;
174}
175
176/*
177 * walk_p2p_bridge - discover and walk p2p bridges
178 * @handle: points to an acpi_pci_root
179 * @context: p2p_bridge_context pointer
180 *
181 * Note that when we call ourselves recursively, we pass a different
182 * value of pci_bus in the child_context.
183 */
184static acpi_status
185walk_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
186{
187 int device, function;
188 unsigned long adr;
189 acpi_status status;
190 acpi_handle dummy_handle;
191 acpi_walk_callback user_function;
192
193 struct pci_dev *dev;
194 struct pci_bus *pci_bus;
195 struct callback_args child_context;
196 struct callback_args *parent_context = context;
197
198 pci_bus = parent_context->pci_bus;
199 user_function = parent_context->user_function;
200
201 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
202 if (ACPI_FAILURE(status))
203 return AE_OK;
204
205 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
206 if (ACPI_FAILURE(status))
207 return AE_OK;
208
209 device = (adr >> 16) & 0xffff;
210 function = adr & 0xffff;
211
212 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
213 if (!dev || !dev->subordinate)
214 goto out;
215
216 child_context.pci_bus = dev->subordinate;
217 child_context.user_function = user_function;
218 child_context.root_handle = parent_context->root_handle;
219
220 dbg("p2p bridge walk, pci_bus = %x\n", dev->subordinate->number);
221 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
222 user_function, &child_context, NULL);
223 if (ACPI_FAILURE(status))
224 goto out;
225
226 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
227 walk_p2p_bridge, &child_context, NULL);
228out:
229 pci_dev_put(dev);
230 return AE_OK;
231}
232
233/*
234 * walk_root_bridge - generic root bridge walker
235 * @handle: points to an acpi_pci_root
236 * @user_function: user callback for slot objects
237 *
238 * Call user_function for all objects underneath this root bridge.
239 * Walk p2p bridges underneath us and call user_function on those too.
240 */
241static int
242walk_root_bridge(acpi_handle handle, acpi_walk_callback user_function)
243{
244 int seg, bus;
245 unsigned long tmp;
246 acpi_status status;
247 acpi_handle dummy_handle;
248 struct pci_bus *pci_bus;
249 struct callback_args context;
250
251 /* If the bridge doesn't have _STA, we assume it is always there */
252 status = acpi_get_handle(handle, "_STA", &dummy_handle);
253 if (ACPI_SUCCESS(status)) {
254 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
255 if (ACPI_FAILURE(status)) {
256 info("%s: _STA evaluation failure\n", __func__);
257 return 0;
258 }
259 if ((tmp & ACPI_STA_DEVICE_FUNCTIONING) == 0)
260 /* don't register this object */
261 return 0;
262 }
263
264 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
265 seg = ACPI_SUCCESS(status) ? tmp : 0;
266
267 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
268 bus = ACPI_SUCCESS(status) ? tmp : 0;
269
270 pci_bus = pci_find_bus(seg, bus);
271 if (!pci_bus)
272 return 0;
273
274 context.pci_bus = pci_bus;
275 context.user_function = user_function;
276 context.root_handle = handle;
277
278 dbg("root bridge walk, pci_bus = %x\n", pci_bus->number);
279 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
280 user_function, &context, NULL);
281 if (ACPI_FAILURE(status))
282 return status;
283
284 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
285 walk_p2p_bridge, &context, NULL);
286 if (ACPI_FAILURE(status))
287 err("%s: walk_p2p_bridge failure - %d\n", __func__, status);
288
289 return status;
290}
291
292/*
293 * acpi_pci_slot_add
294 * @handle: points to an acpi_pci_root
295 */
296static int
297acpi_pci_slot_add(acpi_handle handle)
298{
299 acpi_status status;
300
301 status = walk_root_bridge(handle, register_slot);
302 if (ACPI_FAILURE(status))
303 err("%s: register_slot failure - %d\n", __func__, status);
304
305 return status;
306}
307
308/*
309 * acpi_pci_slot_remove
310 * @handle: points to an acpi_pci_root
311 */
312static void
313acpi_pci_slot_remove(acpi_handle handle)
314{
315 struct acpi_pci_slot *slot, *tmp;
316
317 mutex_lock(&slot_list_lock);
318 list_for_each_entry_safe(slot, tmp, &slot_list, list) {
319 if (slot->root_handle == handle) {
320 list_del(&slot->list);
321 pci_destroy_slot(slot->pci_slot);
322 kfree(slot);
323 }
324 }
325 mutex_unlock(&slot_list_lock);
326}
327
328static int do_sta_before_sun(const struct dmi_system_id *d)
329{
330 info("%s detected: will evaluate _STA before calling _SUN\n", d->ident);
331 check_sta_before_sun = 1;
332 return 0;
333}
334
335static struct dmi_system_id acpi_pci_slot_dmi_table[] __initdata = {
336 /*
337 * Fujitsu Primequest machines will return 1023 to indicate an
338 * error if the _SUN method is evaluated on SxFy objects that
339 * are not present (as indicated by _STA), so for those machines,
340 * we want to check _STA before evaluating _SUN.
341 */
342 {
343 .callback = do_sta_before_sun,
344 .ident = "Fujitsu PRIMEQUEST",
345 .matches = {
346 DMI_MATCH(DMI_BIOS_VENDOR, "FUJITSU LIMITED"),
347 DMI_MATCH(DMI_BIOS_VERSION, "PRIMEQUEST"),
348 },
349 },
350 {}
351};
352
353static int __init
354acpi_pci_slot_init(void)
355{
356 dmi_check_system(acpi_pci_slot_dmi_table);
357 acpi_pci_register_driver(&acpi_pci_slot_driver);
358 return 0;
359}
360
361static void __exit
362acpi_pci_slot_exit(void)
363{
364 acpi_pci_unregister_driver(&acpi_pci_slot_driver);
365}
366
367module_init(acpi_pci_slot_init);
368module_exit(acpi_pci_slot_exit);
diff --git a/drivers/acpi/power.c b/drivers/acpi/power.c
index 81e4f081a4ae..4ab21cb1c8c7 100644
--- a/drivers/acpi/power.c
+++ b/drivers/acpi/power.c
@@ -292,69 +292,135 @@ static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev)
292 return 0; 292 return 0;
293} 293}
294 294
295/**
296 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
297 * ACPI 3.0) _PSW (Power State Wake)
298 * @dev: Device to handle.
299 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
300 * @sleep_state: Target sleep state of the system.
301 * @dev_state: Target power state of the device.
302 *
303 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
304 * State Wake) for the device, if present. On failure reset the device's
305 * wakeup.flags.valid flag.
306 *
307 * RETURN VALUE:
308 * 0 if either _DSW or _PSW has been successfully executed
309 * 0 if neither _DSW nor _PSW has been found
310 * -ENODEV if the execution of either _DSW or _PSW has failed
311 */
312int acpi_device_sleep_wake(struct acpi_device *dev,
313 int enable, int sleep_state, int dev_state)
314{
315 union acpi_object in_arg[3];
316 struct acpi_object_list arg_list = { 3, in_arg };
317 acpi_status status = AE_OK;
318
319 /*
320 * Try to execute _DSW first.
321 *
322 * Three agruments are needed for the _DSW object:
323 * Argument 0: enable/disable the wake capabilities
324 * Argument 1: target system state
325 * Argument 2: target device state
326 * When _DSW object is called to disable the wake capabilities, maybe
327 * the first argument is filled. The values of the other two agruments
328 * are meaningless.
329 */
330 in_arg[0].type = ACPI_TYPE_INTEGER;
331 in_arg[0].integer.value = enable;
332 in_arg[1].type = ACPI_TYPE_INTEGER;
333 in_arg[1].integer.value = sleep_state;
334 in_arg[2].type = ACPI_TYPE_INTEGER;
335 in_arg[2].integer.value = dev_state;
336 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
337 if (ACPI_SUCCESS(status)) {
338 return 0;
339 } else if (status != AE_NOT_FOUND) {
340 printk(KERN_ERR PREFIX "_DSW execution failed\n");
341 dev->wakeup.flags.valid = 0;
342 return -ENODEV;
343 }
344
345 /* Execute _PSW */
346 arg_list.count = 1;
347 in_arg[0].integer.value = enable;
348 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
349 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
350 printk(KERN_ERR PREFIX "_PSW execution failed\n");
351 dev->wakeup.flags.valid = 0;
352 return -ENODEV;
353 }
354
355 return 0;
356}
357
295/* 358/*
296 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229): 359 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
297 * 1. Power on the power resources required for the wakeup device 360 * 1. Power on the power resources required for the wakeup device
298 * 2. Enable _PSW (power state wake) for the device if present 361 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
362 * State Wake) for the device, if present
299 */ 363 */
300int acpi_enable_wakeup_device_power(struct acpi_device *dev) 364int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
301{ 365{
302 union acpi_object arg = { ACPI_TYPE_INTEGER }; 366 int i, err;
303 struct acpi_object_list arg_list = { 1, &arg };
304 acpi_status status = AE_OK;
305 int i;
306 int ret = 0;
307 367
308 if (!dev || !dev->wakeup.flags.valid) 368 if (!dev || !dev->wakeup.flags.valid)
309 return -1; 369 return -EINVAL;
370
371 /*
372 * Do not execute the code below twice in a row without calling
373 * acpi_disable_wakeup_device_power() in between for the same device
374 */
375 if (dev->wakeup.flags.prepared)
376 return 0;
310 377
311 arg.integer.value = 1;
312 /* Open power resource */ 378 /* Open power resource */
313 for (i = 0; i < dev->wakeup.resources.count; i++) { 379 for (i = 0; i < dev->wakeup.resources.count; i++) {
314 ret = acpi_power_on(dev->wakeup.resources.handles[i], dev); 380 int ret = acpi_power_on(dev->wakeup.resources.handles[i], dev);
315 if (ret) { 381 if (ret) {
316 printk(KERN_ERR PREFIX "Transition power state\n"); 382 printk(KERN_ERR PREFIX "Transition power state\n");
317 dev->wakeup.flags.valid = 0; 383 dev->wakeup.flags.valid = 0;
318 return -1; 384 return -ENODEV;
319 } 385 }
320 } 386 }
321 387
322 /* Execute PSW */ 388 /*
323 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL); 389 * Passing 3 as the third argument below means the device may be placed
324 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { 390 * in arbitrary power state afterwards.
325 printk(KERN_ERR PREFIX "Evaluate _PSW\n"); 391 */
326 dev->wakeup.flags.valid = 0; 392 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
327 ret = -1; 393 if (!err)
328 } 394 dev->wakeup.flags.prepared = 1;
329 395
330 return ret; 396 return err;
331} 397}
332 398
333/* 399/*
334 * Shutdown a wakeup device, counterpart of above method 400 * Shutdown a wakeup device, counterpart of above method
335 * 1. Disable _PSW (power state wake) 401 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
402 * State Wake) for the device, if present
336 * 2. Shutdown down the power resources 403 * 2. Shutdown down the power resources
337 */ 404 */
338int acpi_disable_wakeup_device_power(struct acpi_device *dev) 405int acpi_disable_wakeup_device_power(struct acpi_device *dev)
339{ 406{
340 union acpi_object arg = { ACPI_TYPE_INTEGER }; 407 int i, ret;
341 struct acpi_object_list arg_list = { 1, &arg };
342 acpi_status status = AE_OK;
343 int i;
344 int ret = 0;
345
346 408
347 if (!dev || !dev->wakeup.flags.valid) 409 if (!dev || !dev->wakeup.flags.valid)
348 return -1; 410 return -EINVAL;
349 411
350 arg.integer.value = 0; 412 /*
351 /* Execute PSW */ 413 * Do not execute the code below twice in a row without calling
352 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL); 414 * acpi_enable_wakeup_device_power() in between for the same device
353 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { 415 */
354 printk(KERN_ERR PREFIX "Evaluate _PSW\n"); 416 if (!dev->wakeup.flags.prepared)
355 dev->wakeup.flags.valid = 0; 417 return 0;
356 return -1; 418
357 } 419 dev->wakeup.flags.prepared = 0;
420
421 ret = acpi_device_sleep_wake(dev, 0, 0, 0);
422 if (ret)
423 return ret;
358 424
359 /* Close power resource */ 425 /* Close power resource */
360 for (i = 0; i < dev->wakeup.resources.count; i++) { 426 for (i = 0; i < dev->wakeup.resources.count; i++) {
@@ -362,7 +428,7 @@ int acpi_disable_wakeup_device_power(struct acpi_device *dev)
362 if (ret) { 428 if (ret) {
363 printk(KERN_ERR PREFIX "Transition power state\n"); 429 printk(KERN_ERR PREFIX "Transition power state\n");
364 dev->wakeup.flags.valid = 0; 430 dev->wakeup.flags.valid = 0;
365 return -1; 431 return -ENODEV;
366 } 432 }
367 } 433 }
368 434
diff --git a/drivers/acpi/processor_core.c b/drivers/acpi/processor_core.c
index 9dd0fa93b9e1..ec0f2d581ece 100644
--- a/drivers/acpi/processor_core.c
+++ b/drivers/acpi/processor_core.c
@@ -118,8 +118,31 @@ static const struct file_operations acpi_processor_info_fops = {
118 .release = single_release, 118 .release = single_release,
119}; 119};
120 120
121struct acpi_processor *processors[NR_CPUS]; 121DEFINE_PER_CPU(struct acpi_processor *, processors);
122struct acpi_processor_errata errata __read_mostly; 122struct acpi_processor_errata errata __read_mostly;
123static int set_no_mwait(const struct dmi_system_id *id)
124{
125 printk(KERN_NOTICE PREFIX "%s detected - "
126 "disable mwait for CPU C-stetes\n", id->ident);
127 idle_nomwait = 1;
128 return 0;
129}
130
131static struct dmi_system_id __cpuinitdata processor_idle_dmi_table[] = {
132 {
133 set_no_mwait, "IFL91 board", {
134 DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
135 DMI_MATCH(DMI_SYS_VENDOR, "ZEPTO"),
136 DMI_MATCH(DMI_PRODUCT_VERSION, "3215W"),
137 DMI_MATCH(DMI_BOARD_NAME, "IFL91") }, NULL},
138 {
139 set_no_mwait, "Extensa 5220", {
140 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
141 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
142 DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
143 DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
144 {},
145};
123 146
124/* -------------------------------------------------------------------------- 147/* --------------------------------------------------------------------------
125 Errata Handling 148 Errata Handling
@@ -265,7 +288,20 @@ static int acpi_processor_set_pdc(struct acpi_processor *pr)
265 288
266 if (!pdc_in) 289 if (!pdc_in)
267 return status; 290 return status;
291 if (idle_nomwait) {
292 /*
293 * If mwait is disabled for CPU C-states, the C2C3_FFH access
294 * mode will be disabled in the parameter of _PDC object.
295 * Of course C1_FFH access mode will also be disabled.
296 */
297 union acpi_object *obj;
298 u32 *buffer = NULL;
268 299
300 obj = pdc_in->pointer;
301 buffer = (u32 *)(obj->buffer.pointer);
302 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
303
304 }
269 status = acpi_evaluate_object(pr->handle, "_PDC", pdc_in, NULL); 305 status = acpi_evaluate_object(pr->handle, "_PDC", pdc_in, NULL);
270 306
271 if (ACPI_FAILURE(status)) 307 if (ACPI_FAILURE(status))
@@ -614,14 +650,14 @@ static int acpi_processor_get_info(struct acpi_processor *pr, unsigned has_uid)
614 return 0; 650 return 0;
615} 651}
616 652
617static void *processor_device_array[NR_CPUS]; 653static DEFINE_PER_CPU(void *, processor_device_array);
618 654
619static int __cpuinit acpi_processor_start(struct acpi_device *device) 655static int __cpuinit acpi_processor_start(struct acpi_device *device)
620{ 656{
621 int result = 0; 657 int result = 0;
622 acpi_status status = AE_OK; 658 acpi_status status = AE_OK;
623 struct acpi_processor *pr; 659 struct acpi_processor *pr;
624 660 struct sys_device *sysdev;
625 661
626 pr = acpi_driver_data(device); 662 pr = acpi_driver_data(device);
627 663
@@ -638,20 +674,24 @@ static int __cpuinit acpi_processor_start(struct acpi_device *device)
638 * ACPI id of processors can be reported wrongly by the BIOS. 674 * ACPI id of processors can be reported wrongly by the BIOS.
639 * Don't trust it blindly 675 * Don't trust it blindly
640 */ 676 */
641 if (processor_device_array[pr->id] != NULL && 677 if (per_cpu(processor_device_array, pr->id) != NULL &&
642 processor_device_array[pr->id] != device) { 678 per_cpu(processor_device_array, pr->id) != device) {
643 printk(KERN_WARNING "BIOS reported wrong ACPI id " 679 printk(KERN_WARNING "BIOS reported wrong ACPI id "
644 "for the processor\n"); 680 "for the processor\n");
645 return -ENODEV; 681 return -ENODEV;
646 } 682 }
647 processor_device_array[pr->id] = device; 683 per_cpu(processor_device_array, pr->id) = device;
648 684
649 processors[pr->id] = pr; 685 per_cpu(processors, pr->id) = pr;
650 686
651 result = acpi_processor_add_fs(device); 687 result = acpi_processor_add_fs(device);
652 if (result) 688 if (result)
653 goto end; 689 goto end;
654 690
691 sysdev = get_cpu_sysdev(pr->id);
692 if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev"))
693 return -EFAULT;
694
655 status = acpi_install_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY, 695 status = acpi_install_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY,
656 acpi_processor_notify, pr); 696 acpi_processor_notify, pr);
657 697
@@ -749,7 +789,7 @@ static int acpi_cpu_soft_notify(struct notifier_block *nfb,
749 unsigned long action, void *hcpu) 789 unsigned long action, void *hcpu)
750{ 790{
751 unsigned int cpu = (unsigned long)hcpu; 791 unsigned int cpu = (unsigned long)hcpu;
752 struct acpi_processor *pr = processors[cpu]; 792 struct acpi_processor *pr = per_cpu(processors, cpu);
753 793
754 if (action == CPU_ONLINE && pr) { 794 if (action == CPU_ONLINE && pr) {
755 acpi_processor_ppc_has_changed(pr); 795 acpi_processor_ppc_has_changed(pr);
@@ -810,6 +850,8 @@ static int acpi_processor_remove(struct acpi_device *device, int type)
810 status = acpi_remove_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY, 850 status = acpi_remove_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY,
811 acpi_processor_notify); 851 acpi_processor_notify);
812 852
853 sysfs_remove_link(&device->dev.kobj, "sysdev");
854
813 acpi_processor_remove_fs(device); 855 acpi_processor_remove_fs(device);
814 856
815 if (pr->cdev) { 857 if (pr->cdev) {
@@ -819,8 +861,8 @@ static int acpi_processor_remove(struct acpi_device *device, int type)
819 pr->cdev = NULL; 861 pr->cdev = NULL;
820 } 862 }
821 863
822 processors[pr->id] = NULL; 864 per_cpu(processors, pr->id) = NULL;
823 processor_device_array[pr->id] = NULL; 865 per_cpu(processor_device_array, pr->id) = NULL;
824 kfree(pr); 866 kfree(pr);
825 867
826 return 0; 868 return 0;
@@ -1014,9 +1056,9 @@ static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
1014 1056
1015static int acpi_processor_handle_eject(struct acpi_processor *pr) 1057static int acpi_processor_handle_eject(struct acpi_processor *pr)
1016{ 1058{
1017 if (cpu_online(pr->id)) { 1059 if (cpu_online(pr->id))
1018 return (-EINVAL); 1060 cpu_down(pr->id);
1019 } 1061
1020 arch_unregister_cpu(pr->id); 1062 arch_unregister_cpu(pr->id);
1021 acpi_unmap_lsapic(pr->id); 1063 acpi_unmap_lsapic(pr->id);
1022 return (0); 1064 return (0);
@@ -1068,8 +1110,6 @@ static int __init acpi_processor_init(void)
1068{ 1110{
1069 int result = 0; 1111 int result = 0;
1070 1112
1071
1072 memset(&processors, 0, sizeof(processors));
1073 memset(&errata, 0, sizeof(errata)); 1113 memset(&errata, 0, sizeof(errata));
1074 1114
1075#ifdef CONFIG_SMP 1115#ifdef CONFIG_SMP
@@ -1083,6 +1123,11 @@ static int __init acpi_processor_init(void)
1083 return -ENOMEM; 1123 return -ENOMEM;
1084 acpi_processor_dir->owner = THIS_MODULE; 1124 acpi_processor_dir->owner = THIS_MODULE;
1085 1125
1126 /*
1127 * Check whether the system is DMI table. If yes, OSPM
1128 * should not use mwait for CPU-states.
1129 */
1130 dmi_check_system(processor_idle_dmi_table);
1086 result = cpuidle_register_driver(&acpi_idle_driver); 1131 result = cpuidle_register_driver(&acpi_idle_driver);
1087 if (result < 0) 1132 if (result < 0)
1088 goto out_proc; 1133 goto out_proc;
diff --git a/drivers/acpi/processor_idle.c b/drivers/acpi/processor_idle.c
index 4976e5db2b3f..d592dbb1d12a 100644
--- a/drivers/acpi/processor_idle.c
+++ b/drivers/acpi/processor_idle.c
@@ -41,6 +41,7 @@
41#include <linux/pm_qos_params.h> 41#include <linux/pm_qos_params.h>
42#include <linux/clockchips.h> 42#include <linux/clockchips.h>
43#include <linux/cpuidle.h> 43#include <linux/cpuidle.h>
44#include <linux/cpuidle.h>
44 45
45/* 46/*
46 * Include the apic definitions for x86 to have the APIC timer related defines 47 * Include the apic definitions for x86 to have the APIC timer related defines
@@ -57,6 +58,7 @@
57 58
58#include <acpi/acpi_bus.h> 59#include <acpi/acpi_bus.h>
59#include <acpi/processor.h> 60#include <acpi/processor.h>
61#include <asm/processor.h>
60 62
61#define ACPI_PROCESSOR_COMPONENT 0x01000000 63#define ACPI_PROCESSOR_COMPONENT 0x01000000
62#define ACPI_PROCESSOR_CLASS "processor" 64#define ACPI_PROCESSOR_CLASS "processor"
@@ -401,7 +403,7 @@ static void acpi_processor_idle(void)
401 */ 403 */
402 local_irq_disable(); 404 local_irq_disable();
403 405
404 pr = processors[smp_processor_id()]; 406 pr = __get_cpu_var(processors);
405 if (!pr) { 407 if (!pr) {
406 local_irq_enable(); 408 local_irq_enable();
407 return; 409 return;
@@ -955,6 +957,21 @@ static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
955 } else { 957 } else {
956 continue; 958 continue;
957 } 959 }
960 if (cx.type == ACPI_STATE_C1 &&
961 (idle_halt || idle_nomwait)) {
962 /*
963 * In most cases the C1 space_id obtained from
964 * _CST object is FIXED_HARDWARE access mode.
965 * But when the option of idle=halt is added,
966 * the entry_method type should be changed from
967 * CSTATE_FFH to CSTATE_HALT.
968 * When the option of idle=nomwait is added,
969 * the C1 entry_method type should be
970 * CSTATE_HALT.
971 */
972 cx.entry_method = ACPI_CSTATE_HALT;
973 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
974 }
958 } else { 975 } else {
959 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x", 976 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
960 cx.address); 977 cx.address);
@@ -1431,7 +1448,7 @@ static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1431 struct acpi_processor *pr; 1448 struct acpi_processor *pr;
1432 struct acpi_processor_cx *cx = cpuidle_get_statedata(state); 1449 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1433 1450
1434 pr = processors[smp_processor_id()]; 1451 pr = __get_cpu_var(processors);
1435 1452
1436 if (unlikely(!pr)) 1453 if (unlikely(!pr))
1437 return 0; 1454 return 0;
@@ -1471,7 +1488,7 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1471 u32 t1, t2; 1488 u32 t1, t2;
1472 int sleep_ticks = 0; 1489 int sleep_ticks = 0;
1473 1490
1474 pr = processors[smp_processor_id()]; 1491 pr = __get_cpu_var(processors);
1475 1492
1476 if (unlikely(!pr)) 1493 if (unlikely(!pr))
1477 return 0; 1494 return 0;
@@ -1549,7 +1566,7 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1549 u32 t1, t2; 1566 u32 t1, t2;
1550 int sleep_ticks = 0; 1567 int sleep_ticks = 0;
1551 1568
1552 pr = processors[smp_processor_id()]; 1569 pr = __get_cpu_var(processors);
1553 1570
1554 if (unlikely(!pr)) 1571 if (unlikely(!pr))
1555 return 0; 1572 return 0;
@@ -1780,6 +1797,15 @@ int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
1780 return 0; 1797 return 0;
1781 1798
1782 if (!first_run) { 1799 if (!first_run) {
1800 if (idle_halt) {
1801 /*
1802 * When the boot option of "idle=halt" is added, halt
1803 * is used for CPU IDLE.
1804 * In such case C2/C3 is meaningless. So the max_cstate
1805 * is set to one.
1806 */
1807 max_cstate = 1;
1808 }
1783 dmi_check_system(processor_power_dmi_table); 1809 dmi_check_system(processor_power_dmi_table);
1784 max_cstate = acpi_processor_cstate_check(max_cstate); 1810 max_cstate = acpi_processor_cstate_check(max_cstate);
1785 if (max_cstate < ACPI_C_STATES_MAX) 1811 if (max_cstate < ACPI_C_STATES_MAX)
diff --git a/drivers/acpi/processor_perflib.c b/drivers/acpi/processor_perflib.c
index d80b2d1441af..b4749969c6b4 100644
--- a/drivers/acpi/processor_perflib.c
+++ b/drivers/acpi/processor_perflib.c
@@ -89,7 +89,7 @@ static int acpi_processor_ppc_notifier(struct notifier_block *nb,
89 if (event != CPUFREQ_INCOMPATIBLE) 89 if (event != CPUFREQ_INCOMPATIBLE)
90 goto out; 90 goto out;
91 91
92 pr = processors[policy->cpu]; 92 pr = per_cpu(processors, policy->cpu);
93 if (!pr || !pr->performance) 93 if (!pr || !pr->performance)
94 goto out; 94 goto out;
95 95
@@ -572,7 +572,7 @@ int acpi_processor_preregister_performance(
572 572
573 /* Call _PSD for all CPUs */ 573 /* Call _PSD for all CPUs */
574 for_each_possible_cpu(i) { 574 for_each_possible_cpu(i) {
575 pr = processors[i]; 575 pr = per_cpu(processors, i);
576 if (!pr) { 576 if (!pr) {
577 /* Look only at processors in ACPI namespace */ 577 /* Look only at processors in ACPI namespace */
578 continue; 578 continue;
@@ -603,7 +603,7 @@ int acpi_processor_preregister_performance(
603 * domain info. 603 * domain info.
604 */ 604 */
605 for_each_possible_cpu(i) { 605 for_each_possible_cpu(i) {
606 pr = processors[i]; 606 pr = per_cpu(processors, i);
607 if (!pr) 607 if (!pr)
608 continue; 608 continue;
609 609
@@ -624,7 +624,7 @@ int acpi_processor_preregister_performance(
624 624
625 cpus_clear(covered_cpus); 625 cpus_clear(covered_cpus);
626 for_each_possible_cpu(i) { 626 for_each_possible_cpu(i) {
627 pr = processors[i]; 627 pr = per_cpu(processors, i);
628 if (!pr) 628 if (!pr)
629 continue; 629 continue;
630 630
@@ -651,7 +651,7 @@ int acpi_processor_preregister_performance(
651 if (i == j) 651 if (i == j)
652 continue; 652 continue;
653 653
654 match_pr = processors[j]; 654 match_pr = per_cpu(processors, j);
655 if (!match_pr) 655 if (!match_pr)
656 continue; 656 continue;
657 657
@@ -680,7 +680,7 @@ int acpi_processor_preregister_performance(
680 if (i == j) 680 if (i == j)
681 continue; 681 continue;
682 682
683 match_pr = processors[j]; 683 match_pr = per_cpu(processors, j);
684 if (!match_pr) 684 if (!match_pr)
685 continue; 685 continue;
686 686
@@ -697,7 +697,7 @@ int acpi_processor_preregister_performance(
697 697
698err_ret: 698err_ret:
699 for_each_possible_cpu(i) { 699 for_each_possible_cpu(i) {
700 pr = processors[i]; 700 pr = per_cpu(processors, i);
701 if (!pr || !pr->performance) 701 if (!pr || !pr->performance)
702 continue; 702 continue;
703 703
@@ -728,7 +728,7 @@ acpi_processor_register_performance(struct acpi_processor_performance
728 728
729 mutex_lock(&performance_mutex); 729 mutex_lock(&performance_mutex);
730 730
731 pr = processors[cpu]; 731 pr = per_cpu(processors, cpu);
732 if (!pr) { 732 if (!pr) {
733 mutex_unlock(&performance_mutex); 733 mutex_unlock(&performance_mutex);
734 return -ENODEV; 734 return -ENODEV;
@@ -766,7 +766,7 @@ acpi_processor_unregister_performance(struct acpi_processor_performance
766 766
767 mutex_lock(&performance_mutex); 767 mutex_lock(&performance_mutex);
768 768
769 pr = processors[cpu]; 769 pr = per_cpu(processors, cpu);
770 if (!pr) { 770 if (!pr) {
771 mutex_unlock(&performance_mutex); 771 mutex_unlock(&performance_mutex);
772 return; 772 return;
diff --git a/drivers/acpi/processor_throttling.c b/drivers/acpi/processor_throttling.c
index bb06738860c4..0622ace05220 100644
--- a/drivers/acpi/processor_throttling.c
+++ b/drivers/acpi/processor_throttling.c
@@ -71,7 +71,7 @@ static int acpi_processor_update_tsd_coord(void)
71 * coordination between all CPUs. 71 * coordination between all CPUs.
72 */ 72 */
73 for_each_possible_cpu(i) { 73 for_each_possible_cpu(i) {
74 pr = processors[i]; 74 pr = per_cpu(processors, i);
75 if (!pr) 75 if (!pr)
76 continue; 76 continue;
77 77
@@ -93,7 +93,7 @@ static int acpi_processor_update_tsd_coord(void)
93 93
94 cpus_clear(covered_cpus); 94 cpus_clear(covered_cpus);
95 for_each_possible_cpu(i) { 95 for_each_possible_cpu(i) {
96 pr = processors[i]; 96 pr = per_cpu(processors, i);
97 if (!pr) 97 if (!pr)
98 continue; 98 continue;
99 99
@@ -119,7 +119,7 @@ static int acpi_processor_update_tsd_coord(void)
119 if (i == j) 119 if (i == j)
120 continue; 120 continue;
121 121
122 match_pr = processors[j]; 122 match_pr = per_cpu(processors, j);
123 if (!match_pr) 123 if (!match_pr)
124 continue; 124 continue;
125 125
@@ -152,7 +152,7 @@ static int acpi_processor_update_tsd_coord(void)
152 if (i == j) 152 if (i == j)
153 continue; 153 continue;
154 154
155 match_pr = processors[j]; 155 match_pr = per_cpu(processors, j);
156 if (!match_pr) 156 if (!match_pr)
157 continue; 157 continue;
158 158
@@ -172,7 +172,7 @@ static int acpi_processor_update_tsd_coord(void)
172 172
173err_ret: 173err_ret:
174 for_each_possible_cpu(i) { 174 for_each_possible_cpu(i) {
175 pr = processors[i]; 175 pr = per_cpu(processors, i);
176 if (!pr) 176 if (!pr)
177 continue; 177 continue;
178 178
@@ -214,7 +214,7 @@ static int acpi_processor_throttling_notifier(unsigned long event, void *data)
214 struct acpi_processor_throttling *p_throttling; 214 struct acpi_processor_throttling *p_throttling;
215 215
216 cpu = p_tstate->cpu; 216 cpu = p_tstate->cpu;
217 pr = processors[cpu]; 217 pr = per_cpu(processors, cpu);
218 if (!pr) { 218 if (!pr) {
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n")); 219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n"));
220 return 0; 220 return 0;
@@ -1035,7 +1035,7 @@ int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
1035 * cpus. 1035 * cpus.
1036 */ 1036 */
1037 for_each_cpu_mask(i, online_throttling_cpus) { 1037 for_each_cpu_mask(i, online_throttling_cpus) {
1038 match_pr = processors[i]; 1038 match_pr = per_cpu(processors, i);
1039 /* 1039 /*
1040 * If the pointer is invalid, we will report the 1040 * If the pointer is invalid, we will report the
1041 * error message and continue. 1041 * error message and continue.
@@ -1232,7 +1232,10 @@ static ssize_t acpi_processor_write_throttling(struct file *file,
1232 int result = 0; 1232 int result = 0;
1233 struct seq_file *m = file->private_data; 1233 struct seq_file *m = file->private_data;
1234 struct acpi_processor *pr = m->private; 1234 struct acpi_processor *pr = m->private;
1235 char state_string[12] = { '\0' }; 1235 char state_string[5] = "";
1236 char *charp = NULL;
1237 size_t state_val = 0;
1238 char tmpbuf[5] = "";
1236 1239
1237 if (!pr || (count > sizeof(state_string) - 1)) 1240 if (!pr || (count > sizeof(state_string) - 1))
1238 return -EINVAL; 1241 return -EINVAL;
@@ -1241,10 +1244,23 @@ static ssize_t acpi_processor_write_throttling(struct file *file,
1241 return -EFAULT; 1244 return -EFAULT;
1242 1245
1243 state_string[count] = '\0'; 1246 state_string[count] = '\0';
1247 if ((count > 0) && (state_string[count-1] == '\n'))
1248 state_string[count-1] = '\0';
1244 1249
1245 result = acpi_processor_set_throttling(pr, 1250 charp = state_string;
1246 simple_strtoul(state_string, 1251 if ((state_string[0] == 't') || (state_string[0] == 'T'))
1247 NULL, 0)); 1252 charp++;
1253
1254 state_val = simple_strtoul(charp, NULL, 0);
1255 if (state_val >= pr->throttling.state_count)
1256 return -EINVAL;
1257
1258 snprintf(tmpbuf, 5, "%zu", state_val);
1259
1260 if (strcmp(tmpbuf, charp) != 0)
1261 return -EINVAL;
1262
1263 result = acpi_processor_set_throttling(pr, state_val);
1248 if (result) 1264 if (result)
1249 return result; 1265 return result;
1250 1266
diff --git a/drivers/acpi/reboot.c b/drivers/acpi/reboot.c
new file mode 100644
index 000000000000..a6b662c00b67
--- /dev/null
+++ b/drivers/acpi/reboot.c
@@ -0,0 +1,50 @@
1
2#include <linux/pci.h>
3#include <linux/acpi.h>
4#include <acpi/reboot.h>
5
6void acpi_reboot(void)
7{
8 struct acpi_generic_address *rr;
9 struct pci_bus *bus0;
10 u8 reset_value;
11 unsigned int devfn;
12
13 if (acpi_disabled)
14 return;
15
16 rr = &acpi_gbl_FADT.reset_register;
17
18 /* Is the reset register supported? */
19 if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) ||
20 rr->bit_width != 8 || rr->bit_offset != 0)
21 return;
22
23 reset_value = acpi_gbl_FADT.reset_value;
24
25 /* The reset register can only exist in I/O, Memory or PCI config space
26 * on a device on bus 0. */
27 switch (rr->space_id) {
28 case ACPI_ADR_SPACE_PCI_CONFIG:
29 /* The reset register can only live on bus 0. */
30 bus0 = pci_find_bus(0, 0);
31 if (!bus0)
32 return;
33 /* Form PCI device/function pair. */
34 devfn = PCI_DEVFN((rr->address >> 32) & 0xffff,
35 (rr->address >> 16) & 0xffff);
36 printk(KERN_DEBUG "Resetting with ACPI PCI RESET_REG.");
37 /* Write the value that resets us. */
38 pci_bus_write_config_byte(bus0, devfn,
39 (rr->address & 0xffff), reset_value);
40 break;
41
42 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
43 case ACPI_ADR_SPACE_SYSTEM_IO:
44 printk(KERN_DEBUG "ACPI MEMORY or I/O RESET_REG.\n");
45 acpi_hw_low_level_write(8, reset_value, rr);
46 break;
47 }
48 /* Wait ten seconds */
49 acpi_os_stall(10000000);
50}
diff --git a/drivers/acpi/resources/rscalc.c b/drivers/acpi/resources/rscalc.c
index 8a112d11d491..f61ebc679e66 100644
--- a/drivers/acpi/resources/rscalc.c
+++ b/drivers/acpi/resources/rscalc.c
@@ -73,7 +73,7 @@ acpi_rs_stream_option_length(u32 resource_length, u32 minimum_total_length);
73 73
74static u8 acpi_rs_count_set_bits(u16 bit_field) 74static u8 acpi_rs_count_set_bits(u16 bit_field)
75{ 75{
76 acpi_native_uint bits_set; 76 u8 bits_set;
77 77
78 ACPI_FUNCTION_ENTRY(); 78 ACPI_FUNCTION_ENTRY();
79 79
@@ -84,7 +84,7 @@ static u8 acpi_rs_count_set_bits(u16 bit_field)
84 bit_field &= (u16) (bit_field - 1); 84 bit_field &= (u16) (bit_field - 1);
85 } 85 }
86 86
87 return ((u8) bits_set); 87 return bits_set;
88} 88}
89 89
90/******************************************************************************* 90/*******************************************************************************
diff --git a/drivers/acpi/resources/rscreate.c b/drivers/acpi/resources/rscreate.c
index faddaee1bc07..7804a8c40e7a 100644
--- a/drivers/acpi/resources/rscreate.c
+++ b/drivers/acpi/resources/rscreate.c
@@ -181,9 +181,9 @@ acpi_rs_create_pci_routing_table(union acpi_operand_object *package_object,
181 } 181 }
182 182
183 /* 183 /*
184 * Loop through the ACPI_INTERNAL_OBJECTS - Each object 184 * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
185 * should be a package that in turn contains an 185 * package that in turn contains an acpi_integer Address, a u8 Pin,
186 * acpi_integer Address, a u8 Pin, a Name and a u8 source_index. 186 * a Name, and a u8 source_index.
187 */ 187 */
188 top_object_list = package_object->package.elements; 188 top_object_list = package_object->package.elements;
189 number_of_elements = package_object->package.count; 189 number_of_elements = package_object->package.count;
@@ -240,9 +240,7 @@ acpi_rs_create_pci_routing_table(union acpi_operand_object *package_object,
240 /* 1) First subobject: Dereference the PRT.Address */ 240 /* 1) First subobject: Dereference the PRT.Address */
241 241
242 obj_desc = sub_object_list[0]; 242 obj_desc = sub_object_list[0];
243 if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_INTEGER) { 243 if (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER) {
244 user_prt->address = obj_desc->integer.value;
245 } else {
246 ACPI_ERROR((AE_INFO, 244 ACPI_ERROR((AE_INFO,
247 "(PRT[%X].Address) Need Integer, found %s", 245 "(PRT[%X].Address) Need Integer, found %s",
248 index, 246 index,
@@ -250,12 +248,12 @@ acpi_rs_create_pci_routing_table(union acpi_operand_object *package_object,
250 return_ACPI_STATUS(AE_BAD_DATA); 248 return_ACPI_STATUS(AE_BAD_DATA);
251 } 249 }
252 250
251 user_prt->address = obj_desc->integer.value;
252
253 /* 2) Second subobject: Dereference the PRT.Pin */ 253 /* 2) Second subobject: Dereference the PRT.Pin */
254 254
255 obj_desc = sub_object_list[1]; 255 obj_desc = sub_object_list[1];
256 if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_INTEGER) { 256 if (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER) {
257 user_prt->pin = (u32) obj_desc->integer.value;
258 } else {
259 ACPI_ERROR((AE_INFO, 257 ACPI_ERROR((AE_INFO,
260 "(PRT[%X].Pin) Need Integer, found %s", 258 "(PRT[%X].Pin) Need Integer, found %s",
261 index, 259 index,
@@ -284,6 +282,25 @@ acpi_rs_create_pci_routing_table(union acpi_operand_object *package_object,
284 } 282 }
285 } 283 }
286 284
285 user_prt->pin = (u32) obj_desc->integer.value;
286
287 /*
288 * If the BIOS has erroneously reversed the _PRT source_name (index 2)
289 * and the source_index (index 3), fix it. _PRT is important enough to
290 * workaround this BIOS error. This also provides compatibility with
291 * other ACPI implementations.
292 */
293 obj_desc = sub_object_list[3];
294 if (!obj_desc
295 || (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER)) {
296 sub_object_list[3] = sub_object_list[2];
297 sub_object_list[2] = obj_desc;
298
299 ACPI_WARNING((AE_INFO,
300 "(PRT[%X].Source) SourceName and SourceIndex are reversed, fixed",
301 index));
302 }
303
287 /* 304 /*
288 * 3) Third subobject: Dereference the PRT.source_name 305 * 3) Third subobject: Dereference the PRT.source_name
289 * The name may be unresolved (slack mode), so allow a null object 306 * The name may be unresolved (slack mode), so allow a null object
@@ -364,9 +381,7 @@ acpi_rs_create_pci_routing_table(union acpi_operand_object *package_object,
364 /* 4) Fourth subobject: Dereference the PRT.source_index */ 381 /* 4) Fourth subobject: Dereference the PRT.source_index */
365 382
366 obj_desc = sub_object_list[source_index_index]; 383 obj_desc = sub_object_list[source_index_index];
367 if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_INTEGER) { 384 if (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER) {
368 user_prt->source_index = (u32) obj_desc->integer.value;
369 } else {
370 ACPI_ERROR((AE_INFO, 385 ACPI_ERROR((AE_INFO,
371 "(PRT[%X].SourceIndex) Need Integer, found %s", 386 "(PRT[%X].SourceIndex) Need Integer, found %s",
372 index, 387 index,
@@ -374,6 +389,8 @@ acpi_rs_create_pci_routing_table(union acpi_operand_object *package_object,
374 return_ACPI_STATUS(AE_BAD_DATA); 389 return_ACPI_STATUS(AE_BAD_DATA);
375 } 390 }
376 391
392 user_prt->source_index = (u32) obj_desc->integer.value;
393
377 /* Point to the next union acpi_operand_object in the top level package */ 394 /* Point to the next union acpi_operand_object in the top level package */
378 395
379 top_object_list++; 396 top_object_list++;
diff --git a/drivers/acpi/resources/rsmisc.c b/drivers/acpi/resources/rsmisc.c
index de1ac3881b22..96a6c0353255 100644
--- a/drivers/acpi/resources/rsmisc.c
+++ b/drivers/acpi/resources/rsmisc.c
@@ -82,7 +82,7 @@ acpi_rs_convert_aml_to_resource(struct acpi_resource *resource,
82 82
83 ACPI_FUNCTION_TRACE(rs_convert_aml_to_resource); 83 ACPI_FUNCTION_TRACE(rs_convert_aml_to_resource);
84 84
85 if (((acpi_native_uint) resource) & 0x3) { 85 if (((acpi_size) resource) & 0x3) {
86 86
87 /* Each internal resource struct is expected to be 32-bit aligned */ 87 /* Each internal resource struct is expected to be 32-bit aligned */
88 88
diff --git a/drivers/acpi/resources/rsutils.c b/drivers/acpi/resources/rsutils.c
index befe2302f41b..f7b3bcd59ba7 100644
--- a/drivers/acpi/resources/rsutils.c
+++ b/drivers/acpi/resources/rsutils.c
@@ -62,7 +62,7 @@ ACPI_MODULE_NAME("rsutils")
62 ******************************************************************************/ 62 ******************************************************************************/
63u8 acpi_rs_decode_bitmask(u16 mask, u8 * list) 63u8 acpi_rs_decode_bitmask(u16 mask, u8 * list)
64{ 64{
65 acpi_native_uint i; 65 u8 i;
66 u8 bit_count; 66 u8 bit_count;
67 67
68 ACPI_FUNCTION_ENTRY(); 68 ACPI_FUNCTION_ENTRY();
@@ -71,7 +71,7 @@ u8 acpi_rs_decode_bitmask(u16 mask, u8 * list)
71 71
72 for (i = 0, bit_count = 0; mask; i++) { 72 for (i = 0, bit_count = 0; mask; i++) {
73 if (mask & 0x0001) { 73 if (mask & 0x0001) {
74 list[bit_count] = (u8) i; 74 list[bit_count] = i;
75 bit_count++; 75 bit_count++;
76 } 76 }
77 77
@@ -96,8 +96,8 @@ u8 acpi_rs_decode_bitmask(u16 mask, u8 * list)
96 96
97u16 acpi_rs_encode_bitmask(u8 * list, u8 count) 97u16 acpi_rs_encode_bitmask(u8 * list, u8 count)
98{ 98{
99 acpi_native_uint i; 99 u32 i;
100 acpi_native_uint mask; 100 u16 mask;
101 101
102 ACPI_FUNCTION_ENTRY(); 102 ACPI_FUNCTION_ENTRY();
103 103
@@ -107,7 +107,7 @@ u16 acpi_rs_encode_bitmask(u8 * list, u8 count)
107 mask |= (0x1 << list[i]); 107 mask |= (0x1 << list[i]);
108 } 108 }
109 109
110 return ((u16) mask); 110 return mask;
111} 111}
112 112
113/******************************************************************************* 113/*******************************************************************************
@@ -130,7 +130,7 @@ u16 acpi_rs_encode_bitmask(u8 * list, u8 count)
130void 130void
131acpi_rs_move_data(void *destination, void *source, u16 item_count, u8 move_type) 131acpi_rs_move_data(void *destination, void *source, u16 item_count, u8 move_type)
132{ 132{
133 acpi_native_uint i; 133 u32 i;
134 134
135 ACPI_FUNCTION_ENTRY(); 135 ACPI_FUNCTION_ENTRY();
136 136
@@ -679,7 +679,6 @@ acpi_rs_set_srs_method_data(struct acpi_namespace_node *node,
679 info->prefix_node = node; 679 info->prefix_node = node;
680 info->pathname = METHOD_NAME__SRS; 680 info->pathname = METHOD_NAME__SRS;
681 info->parameters = args; 681 info->parameters = args;
682 info->parameter_type = ACPI_PARAM_ARGS;
683 info->flags = ACPI_IGNORE_RETURN_VALUE; 682 info->flags = ACPI_IGNORE_RETURN_VALUE;
684 683
685 /* 684 /*
diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c
index 6d85289f1c12..f3132aa47a69 100644
--- a/drivers/acpi/scan.c
+++ b/drivers/acpi/scan.c
@@ -6,6 +6,8 @@
6#include <linux/init.h> 6#include <linux/init.h>
7#include <linux/kernel.h> 7#include <linux/kernel.h>
8#include <linux/acpi.h> 8#include <linux/acpi.h>
9#include <linux/signal.h>
10#include <linux/kthread.h>
9 11
10#include <acpi/acpi_drivers.h> 12#include <acpi/acpi_drivers.h>
11#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */ 13#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
@@ -92,17 +94,37 @@ acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, cha
92} 94}
93static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); 95static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
94 96
95static int acpi_eject_operation(acpi_handle handle, int lockable) 97static int acpi_bus_hot_remove_device(void *context)
96{ 98{
99 struct acpi_device *device;
100 acpi_handle handle = context;
97 struct acpi_object_list arg_list; 101 struct acpi_object_list arg_list;
98 union acpi_object arg; 102 union acpi_object arg;
99 acpi_status status = AE_OK; 103 acpi_status status = AE_OK;
100 104
101 /* 105 if (acpi_bus_get_device(handle, &device))
102 * TBD: evaluate _PS3? 106 return 0;
103 */ 107
108 if (!device)
109 return 0;
110
111 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
112 "Hot-removing device %s...\n", device->dev.bus_id));
113
104 114
105 if (lockable) { 115 if (acpi_bus_trim(device, 1)) {
116 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
117 "Removing device failed\n"));
118 return -1;
119 }
120
121 /* power off device */
122 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
123 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
124 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
125 "Power-off device failed\n"));
126
127 if (device->flags.lockable) {
106 arg_list.count = 1; 128 arg_list.count = 1;
107 arg_list.pointer = &arg; 129 arg_list.pointer = &arg;
108 arg.type = ACPI_TYPE_INTEGER; 130 arg.type = ACPI_TYPE_INTEGER;
@@ -118,26 +140,22 @@ static int acpi_eject_operation(acpi_handle handle, int lockable)
118 /* 140 /*
119 * TBD: _EJD support. 141 * TBD: _EJD support.
120 */ 142 */
121
122 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); 143 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
123 if (ACPI_FAILURE(status)) { 144 if (ACPI_FAILURE(status))
124 return (-ENODEV); 145 return -ENODEV;
125 }
126 146
127 return (0); 147 return 0;
128} 148}
129 149
130static ssize_t 150static ssize_t
131acpi_eject_store(struct device *d, struct device_attribute *attr, 151acpi_eject_store(struct device *d, struct device_attribute *attr,
132 const char *buf, size_t count) 152 const char *buf, size_t count)
133{ 153{
134 int result;
135 int ret = count; 154 int ret = count;
136 int islockable;
137 acpi_status status; 155 acpi_status status;
138 acpi_handle handle;
139 acpi_object_type type = 0; 156 acpi_object_type type = 0;
140 struct acpi_device *acpi_device = to_acpi_device(d); 157 struct acpi_device *acpi_device = to_acpi_device(d);
158 struct task_struct *task;
141 159
142 if ((!count) || (buf[0] != '1')) { 160 if ((!count) || (buf[0] != '1')) {
143 return -EINVAL; 161 return -EINVAL;
@@ -154,18 +172,12 @@ acpi_eject_store(struct device *d, struct device_attribute *attr,
154 goto err; 172 goto err;
155 } 173 }
156 174
157 islockable = acpi_device->flags.lockable; 175 /* remove the device in another thread to fix the deadlock issue */
158 handle = acpi_device->handle; 176 task = kthread_run(acpi_bus_hot_remove_device,
159 177 acpi_device->handle, "acpi_hot_remove_device");
160 result = acpi_bus_trim(acpi_device, 1); 178 if (IS_ERR(task))
161 179 ret = PTR_ERR(task);
162 if (!result) 180err:
163 result = acpi_eject_operation(handle, islockable);
164
165 if (result) {
166 ret = -EBUSY;
167 }
168 err:
169 return ret; 181 return ret;
170} 182}
171 183
@@ -691,9 +703,7 @@ static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
691 acpi_status status = 0; 703 acpi_status status = 0;
692 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 704 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
693 union acpi_object *package = NULL; 705 union acpi_object *package = NULL;
694 union acpi_object in_arg[3]; 706 int psw_error;
695 struct acpi_object_list arg_list = { 3, in_arg };
696 acpi_status psw_status = AE_OK;
697 707
698 struct acpi_device_id button_device_ids[] = { 708 struct acpi_device_id button_device_ids[] = {
699 {"PNP0C0D", 0}, 709 {"PNP0C0D", 0},
@@ -725,39 +735,11 @@ static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
725 * So it is necessary to call _DSW object first. Only when it is not 735 * So it is necessary to call _DSW object first. Only when it is not
726 * present will the _PSW object used. 736 * present will the _PSW object used.
727 */ 737 */
728 /* 738 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
729 * Three agruments are needed for the _DSW object. 739 if (psw_error)
730 * Argument 0: enable/disable the wake capabilities 740 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
731 * When _DSW object is called to disable the wake capabilities, maybe 741 "error in _DSW or _PSW evaluation\n"));
732 * the first argument is filled. The value of the other two agruments 742
733 * is meaningless.
734 */
735 in_arg[0].type = ACPI_TYPE_INTEGER;
736 in_arg[0].integer.value = 0;
737 in_arg[1].type = ACPI_TYPE_INTEGER;
738 in_arg[1].integer.value = 0;
739 in_arg[2].type = ACPI_TYPE_INTEGER;
740 in_arg[2].integer.value = 0;
741 psw_status = acpi_evaluate_object(device->handle, "_DSW",
742 &arg_list, NULL);
743 if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND))
744 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in evaluate _DSW\n"));
745 /*
746 * When the _DSW object is not present, OSPM will call _PSW object.
747 */
748 if (psw_status == AE_NOT_FOUND) {
749 /*
750 * Only one agruments is required for the _PSW object.
751 * agrument 0: enable/disable the wake capabilities
752 */
753 arg_list.count = 1;
754 in_arg[0].integer.value = 0;
755 psw_status = acpi_evaluate_object(device->handle, "_PSW",
756 &arg_list, NULL);
757 if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND))
758 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in "
759 "evaluate _PSW\n"));
760 }
761 /* Power button, Lid switch always enable wakeup */ 743 /* Power button, Lid switch always enable wakeup */
762 if (!acpi_match_device_ids(device, button_device_ids)) 744 if (!acpi_match_device_ids(device, button_device_ids))
763 device->wakeup.flags.run_wake = 1; 745 device->wakeup.flags.run_wake = 1;
diff --git a/drivers/acpi/sleep/main.c b/drivers/acpi/sleep/main.c
index 495c63a3e0af..0489a7d1d42c 100644
--- a/drivers/acpi/sleep/main.c
+++ b/drivers/acpi/sleep/main.c
@@ -24,10 +24,6 @@
24 24
25u8 sleep_states[ACPI_S_STATE_COUNT]; 25u8 sleep_states[ACPI_S_STATE_COUNT];
26 26
27#ifdef CONFIG_PM_SLEEP
28static u32 acpi_target_sleep_state = ACPI_STATE_S0;
29#endif
30
31static int acpi_sleep_prepare(u32 acpi_state) 27static int acpi_sleep_prepare(u32 acpi_state)
32{ 28{
33#ifdef CONFIG_ACPI_SLEEP 29#ifdef CONFIG_ACPI_SLEEP
@@ -49,9 +45,96 @@ static int acpi_sleep_prepare(u32 acpi_state)
49 return 0; 45 return 0;
50} 46}
51 47
52#ifdef CONFIG_SUSPEND 48#ifdef CONFIG_PM_SLEEP
53static struct platform_suspend_ops acpi_suspend_ops; 49static u32 acpi_target_sleep_state = ACPI_STATE_S0;
50
51/*
52 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
53 * user to request that behavior by using the 'acpi_old_suspend_ordering'
54 * kernel command line option that causes the following variable to be set.
55 */
56static bool old_suspend_ordering;
54 57
58void __init acpi_old_suspend_ordering(void)
59{
60 old_suspend_ordering = true;
61}
62
63/**
64 * acpi_pm_disable_gpes - Disable the GPEs.
65 */
66static int acpi_pm_disable_gpes(void)
67{
68 acpi_hw_disable_all_gpes();
69 return 0;
70}
71
72/**
73 * __acpi_pm_prepare - Prepare the platform to enter the target state.
74 *
75 * If necessary, set the firmware waking vector and do arch-specific
76 * nastiness to get the wakeup code to the waking vector.
77 */
78static int __acpi_pm_prepare(void)
79{
80 int error = acpi_sleep_prepare(acpi_target_sleep_state);
81
82 if (error)
83 acpi_target_sleep_state = ACPI_STATE_S0;
84 return error;
85}
86
87/**
88 * acpi_pm_prepare - Prepare the platform to enter the target sleep
89 * state and disable the GPEs.
90 */
91static int acpi_pm_prepare(void)
92{
93 int error = __acpi_pm_prepare();
94
95 if (!error)
96 acpi_hw_disable_all_gpes();
97 return error;
98}
99
100/**
101 * acpi_pm_finish - Instruct the platform to leave a sleep state.
102 *
103 * This is called after we wake back up (or if entering the sleep state
104 * failed).
105 */
106static void acpi_pm_finish(void)
107{
108 u32 acpi_state = acpi_target_sleep_state;
109
110 if (acpi_state == ACPI_STATE_S0)
111 return;
112
113 printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
114 acpi_state);
115 acpi_disable_wakeup_device(acpi_state);
116 acpi_leave_sleep_state(acpi_state);
117
118 /* reset firmware waking vector */
119 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
120
121 acpi_target_sleep_state = ACPI_STATE_S0;
122}
123
124/**
125 * acpi_pm_end - Finish up suspend sequence.
126 */
127static void acpi_pm_end(void)
128{
129 /*
130 * This is necessary in case acpi_pm_finish() is not called during a
131 * failing transition to a sleep state.
132 */
133 acpi_target_sleep_state = ACPI_STATE_S0;
134}
135#endif /* CONFIG_PM_SLEEP */
136
137#ifdef CONFIG_SUSPEND
55extern void do_suspend_lowlevel(void); 138extern void do_suspend_lowlevel(void);
56 139
57static u32 acpi_suspend_states[] = { 140static u32 acpi_suspend_states[] = {
@@ -61,13 +144,10 @@ static u32 acpi_suspend_states[] = {
61 [PM_SUSPEND_MAX] = ACPI_STATE_S5 144 [PM_SUSPEND_MAX] = ACPI_STATE_S5
62}; 145};
63 146
64static int init_8259A_after_S1;
65
66/** 147/**
67 * acpi_suspend_begin - Set the target system sleep state to the state 148 * acpi_suspend_begin - Set the target system sleep state to the state
68 * associated with given @pm_state, if supported. 149 * associated with given @pm_state, if supported.
69 */ 150 */
70
71static int acpi_suspend_begin(suspend_state_t pm_state) 151static int acpi_suspend_begin(suspend_state_t pm_state)
72{ 152{
73 u32 acpi_state = acpi_suspend_states[pm_state]; 153 u32 acpi_state = acpi_suspend_states[pm_state];
@@ -84,25 +164,6 @@ static int acpi_suspend_begin(suspend_state_t pm_state)
84} 164}
85 165
86/** 166/**
87 * acpi_suspend_prepare - Do preliminary suspend work.
88 *
89 * If necessary, set the firmware waking vector and do arch-specific
90 * nastiness to get the wakeup code to the waking vector.
91 */
92
93static int acpi_suspend_prepare(void)
94{
95 int error = acpi_sleep_prepare(acpi_target_sleep_state);
96
97 if (error) {
98 acpi_target_sleep_state = ACPI_STATE_S0;
99 return error;
100 }
101
102 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
103}
104
105/**
106 * acpi_suspend_enter - Actually enter a sleep state. 167 * acpi_suspend_enter - Actually enter a sleep state.
107 * @pm_state: ignored 168 * @pm_state: ignored
108 * 169 *
@@ -110,7 +171,6 @@ static int acpi_suspend_prepare(void)
110 * assembly, which in turn call acpi_enter_sleep_state(). 171 * assembly, which in turn call acpi_enter_sleep_state().
111 * It's unfortunate, but it works. Please fix if you're feeling frisky. 172 * It's unfortunate, but it works. Please fix if you're feeling frisky.
112 */ 173 */
113
114static int acpi_suspend_enter(suspend_state_t pm_state) 174static int acpi_suspend_enter(suspend_state_t pm_state)
115{ 175{
116 acpi_status status = AE_OK; 176 acpi_status status = AE_OK;
@@ -167,46 +227,6 @@ static int acpi_suspend_enter(suspend_state_t pm_state)
167 return ACPI_SUCCESS(status) ? 0 : -EFAULT; 227 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
168} 228}
169 229
170/**
171 * acpi_suspend_finish - Instruct the platform to leave a sleep state.
172 *
173 * This is called after we wake back up (or if entering the sleep state
174 * failed).
175 */
176
177static void acpi_suspend_finish(void)
178{
179 u32 acpi_state = acpi_target_sleep_state;
180
181 acpi_disable_wakeup_device(acpi_state);
182 acpi_leave_sleep_state(acpi_state);
183
184 /* reset firmware waking vector */
185 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
186
187 acpi_target_sleep_state = ACPI_STATE_S0;
188
189#ifdef CONFIG_X86
190 if (init_8259A_after_S1) {
191 printk("Broken toshiba laptop -> kicking interrupts\n");
192 init_8259A(0);
193 }
194#endif
195}
196
197/**
198 * acpi_suspend_end - Finish up suspend sequence.
199 */
200
201static void acpi_suspend_end(void)
202{
203 /*
204 * This is necessary in case acpi_suspend_finish() is not called during a
205 * failing transition to a sleep state.
206 */
207 acpi_target_sleep_state = ACPI_STATE_S0;
208}
209
210static int acpi_suspend_state_valid(suspend_state_t pm_state) 230static int acpi_suspend_state_valid(suspend_state_t pm_state)
211{ 231{
212 u32 acpi_state; 232 u32 acpi_state;
@@ -226,30 +246,39 @@ static int acpi_suspend_state_valid(suspend_state_t pm_state)
226static struct platform_suspend_ops acpi_suspend_ops = { 246static struct platform_suspend_ops acpi_suspend_ops = {
227 .valid = acpi_suspend_state_valid, 247 .valid = acpi_suspend_state_valid,
228 .begin = acpi_suspend_begin, 248 .begin = acpi_suspend_begin,
229 .prepare = acpi_suspend_prepare, 249 .prepare = acpi_pm_prepare,
230 .enter = acpi_suspend_enter, 250 .enter = acpi_suspend_enter,
231 .finish = acpi_suspend_finish, 251 .finish = acpi_pm_finish,
232 .end = acpi_suspend_end, 252 .end = acpi_pm_end,
233}; 253};
234 254
235/* 255/**
236 * Toshiba fails to preserve interrupts over S1, reinitialization 256 * acpi_suspend_begin_old - Set the target system sleep state to the
237 * of 8259 is needed after S1 resume. 257 * state associated with given @pm_state, if supported, and
258 * execute the _PTS control method. This function is used if the
259 * pre-ACPI 2.0 suspend ordering has been requested.
238 */ 260 */
239static int __init init_ints_after_s1(const struct dmi_system_id *d) 261static int acpi_suspend_begin_old(suspend_state_t pm_state)
240{ 262{
241 printk(KERN_WARNING "%s with broken S1 detected.\n", d->ident); 263 int error = acpi_suspend_begin(pm_state);
242 init_8259A_after_S1 = 1; 264
243 return 0; 265 if (!error)
266 error = __acpi_pm_prepare();
267 return error;
244} 268}
245 269
246static struct dmi_system_id __initdata acpisleep_dmi_table[] = { 270/*
247 { 271 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
248 .callback = init_ints_after_s1, 272 * been requested.
249 .ident = "Toshiba Satellite 4030cdt", 273 */
250 .matches = {DMI_MATCH(DMI_PRODUCT_NAME, "S4030CDT/4.3"),}, 274static struct platform_suspend_ops acpi_suspend_ops_old = {
251 }, 275 .valid = acpi_suspend_state_valid,
252 {}, 276 .begin = acpi_suspend_begin_old,
277 .prepare = acpi_pm_disable_gpes,
278 .enter = acpi_suspend_enter,
279 .finish = acpi_pm_finish,
280 .end = acpi_pm_end,
281 .recover = acpi_pm_finish,
253}; 282};
254#endif /* CONFIG_SUSPEND */ 283#endif /* CONFIG_SUSPEND */
255 284
@@ -257,22 +286,9 @@ static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
257static int acpi_hibernation_begin(void) 286static int acpi_hibernation_begin(void)
258{ 287{
259 acpi_target_sleep_state = ACPI_STATE_S4; 288 acpi_target_sleep_state = ACPI_STATE_S4;
260
261 return 0; 289 return 0;
262} 290}
263 291
264static int acpi_hibernation_prepare(void)
265{
266 int error = acpi_sleep_prepare(ACPI_STATE_S4);
267
268 if (error) {
269 acpi_target_sleep_state = ACPI_STATE_S0;
270 return error;
271 }
272
273 return ACPI_SUCCESS(acpi_hw_disable_all_gpes()) ? 0 : -EFAULT;
274}
275
276static int acpi_hibernation_enter(void) 292static int acpi_hibernation_enter(void)
277{ 293{
278 acpi_status status = AE_OK; 294 acpi_status status = AE_OK;
@@ -302,52 +318,55 @@ static void acpi_hibernation_leave(void)
302 acpi_leave_sleep_state_prep(ACPI_STATE_S4); 318 acpi_leave_sleep_state_prep(ACPI_STATE_S4);
303} 319}
304 320
305static void acpi_hibernation_finish(void) 321static void acpi_pm_enable_gpes(void)
306{ 322{
307 acpi_disable_wakeup_device(ACPI_STATE_S4); 323 acpi_hw_enable_all_runtime_gpes();
308 acpi_leave_sleep_state(ACPI_STATE_S4);
309
310 /* reset firmware waking vector */
311 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
312
313 acpi_target_sleep_state = ACPI_STATE_S0;
314} 324}
315 325
316static void acpi_hibernation_end(void) 326static struct platform_hibernation_ops acpi_hibernation_ops = {
317{ 327 .begin = acpi_hibernation_begin,
318 /* 328 .end = acpi_pm_end,
319 * This is necessary in case acpi_hibernation_finish() is not called 329 .pre_snapshot = acpi_pm_prepare,
320 * during a failing transition to the sleep state. 330 .finish = acpi_pm_finish,
321 */ 331 .prepare = acpi_pm_prepare,
322 acpi_target_sleep_state = ACPI_STATE_S0; 332 .enter = acpi_hibernation_enter,
323} 333 .leave = acpi_hibernation_leave,
334 .pre_restore = acpi_pm_disable_gpes,
335 .restore_cleanup = acpi_pm_enable_gpes,
336};
324 337
325static int acpi_hibernation_pre_restore(void) 338/**
339 * acpi_hibernation_begin_old - Set the target system sleep state to
340 * ACPI_STATE_S4 and execute the _PTS control method. This
341 * function is used if the pre-ACPI 2.0 suspend ordering has been
342 * requested.
343 */
344static int acpi_hibernation_begin_old(void)
326{ 345{
327 acpi_status status; 346 int error = acpi_sleep_prepare(ACPI_STATE_S4);
328
329 status = acpi_hw_disable_all_gpes();
330
331 return ACPI_SUCCESS(status) ? 0 : -EFAULT;
332}
333 347
334static void acpi_hibernation_restore_cleanup(void) 348 if (!error)
335{ 349 acpi_target_sleep_state = ACPI_STATE_S4;
336 acpi_hw_enable_all_runtime_gpes(); 350 return error;
337} 351}
338 352
339static struct platform_hibernation_ops acpi_hibernation_ops = { 353/*
340 .begin = acpi_hibernation_begin, 354 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
341 .end = acpi_hibernation_end, 355 * been requested.
342 .pre_snapshot = acpi_hibernation_prepare, 356 */
343 .finish = acpi_hibernation_finish, 357static struct platform_hibernation_ops acpi_hibernation_ops_old = {
344 .prepare = acpi_hibernation_prepare, 358 .begin = acpi_hibernation_begin_old,
359 .end = acpi_pm_end,
360 .pre_snapshot = acpi_pm_disable_gpes,
361 .finish = acpi_pm_finish,
362 .prepare = acpi_pm_disable_gpes,
345 .enter = acpi_hibernation_enter, 363 .enter = acpi_hibernation_enter,
346 .leave = acpi_hibernation_leave, 364 .leave = acpi_hibernation_leave,
347 .pre_restore = acpi_hibernation_pre_restore, 365 .pre_restore = acpi_pm_disable_gpes,
348 .restore_cleanup = acpi_hibernation_restore_cleanup, 366 .restore_cleanup = acpi_pm_enable_gpes,
367 .recover = acpi_pm_finish,
349}; 368};
350#endif /* CONFIG_HIBERNATION */ 369#endif /* CONFIG_HIBERNATION */
351 370
352int acpi_suspend(u32 acpi_state) 371int acpi_suspend(u32 acpi_state)
353{ 372{
@@ -368,8 +387,8 @@ int acpi_suspend(u32 acpi_state)
368/** 387/**
369 * acpi_pm_device_sleep_state - return preferred power state of ACPI device 388 * acpi_pm_device_sleep_state - return preferred power state of ACPI device
370 * in the system sleep state given by %acpi_target_sleep_state 389 * in the system sleep state given by %acpi_target_sleep_state
371 * @dev: device to examine 390 * @dev: device to examine; its driver model wakeup flags control
372 * @wake: if set, the device should be able to wake up the system 391 * whether it should be able to wake up the system
373 * @d_min_p: used to store the upper limit of allowed states range 392 * @d_min_p: used to store the upper limit of allowed states range
374 * Return value: preferred power state of the device on success, -ENODEV on 393 * Return value: preferred power state of the device on success, -ENODEV on
375 * failure (ie. if there's no 'struct acpi_device' for @dev) 394 * failure (ie. if there's no 'struct acpi_device' for @dev)
@@ -387,7 +406,7 @@ int acpi_suspend(u32 acpi_state)
387 * via @wake. 406 * via @wake.
388 */ 407 */
389 408
390int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p) 409int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
391{ 410{
392 acpi_handle handle = DEVICE_ACPI_HANDLE(dev); 411 acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
393 struct acpi_device *adev; 412 struct acpi_device *adev;
@@ -426,7 +445,7 @@ int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
426 * can wake the system. _S0W may be valid, too. 445 * can wake the system. _S0W may be valid, too.
427 */ 446 */
428 if (acpi_target_sleep_state == ACPI_STATE_S0 || 447 if (acpi_target_sleep_state == ACPI_STATE_S0 ||
429 (wake && adev->wakeup.state.enabled && 448 (device_may_wakeup(dev) && adev->wakeup.state.enabled &&
430 adev->wakeup.sleep_state <= acpi_target_sleep_state)) { 449 adev->wakeup.sleep_state <= acpi_target_sleep_state)) {
431 acpi_status status; 450 acpi_status status;
432 451
@@ -448,6 +467,31 @@ int acpi_pm_device_sleep_state(struct device *dev, int wake, int *d_min_p)
448 *d_min_p = d_min; 467 *d_min_p = d_min;
449 return d_max; 468 return d_max;
450} 469}
470
471/**
472 * acpi_pm_device_sleep_wake - enable or disable the system wake-up
473 * capability of given device
474 * @dev: device to handle
475 * @enable: 'true' - enable, 'false' - disable the wake-up capability
476 */
477int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
478{
479 acpi_handle handle;
480 struct acpi_device *adev;
481
482 if (!device_may_wakeup(dev))
483 return -EINVAL;
484
485 handle = DEVICE_ACPI_HANDLE(dev);
486 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
487 printk(KERN_DEBUG "ACPI handle has no context!\n");
488 return -ENODEV;
489 }
490
491 return enable ?
492 acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
493 acpi_disable_wakeup_device_power(adev);
494}
451#endif 495#endif
452 496
453static void acpi_power_off_prepare(void) 497static void acpi_power_off_prepare(void)
@@ -472,8 +516,6 @@ int __init acpi_sleep_init(void)
472 u8 type_a, type_b; 516 u8 type_a, type_b;
473#ifdef CONFIG_SUSPEND 517#ifdef CONFIG_SUSPEND
474 int i = 0; 518 int i = 0;
475
476 dmi_check_system(acpisleep_dmi_table);
477#endif 519#endif
478 520
479 if (acpi_disabled) 521 if (acpi_disabled)
@@ -491,13 +533,15 @@ int __init acpi_sleep_init(void)
491 } 533 }
492 } 534 }
493 535
494 suspend_set_ops(&acpi_suspend_ops); 536 suspend_set_ops(old_suspend_ordering ?
537 &acpi_suspend_ops_old : &acpi_suspend_ops);
495#endif 538#endif
496 539
497#ifdef CONFIG_HIBERNATION 540#ifdef CONFIG_HIBERNATION
498 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b); 541 status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
499 if (ACPI_SUCCESS(status)) { 542 if (ACPI_SUCCESS(status)) {
500 hibernation_set_ops(&acpi_hibernation_ops); 543 hibernation_set_ops(old_suspend_ordering ?
544 &acpi_hibernation_ops_old : &acpi_hibernation_ops);
501 sleep_states[ACPI_STATE_S4] = 1; 545 sleep_states[ACPI_STATE_S4] = 1;
502 printk(" S4"); 546 printk(" S4");
503 } 547 }
diff --git a/drivers/acpi/sleep/wakeup.c b/drivers/acpi/sleep/wakeup.c
index ed8e41becf0c..38655eb132dc 100644
--- a/drivers/acpi/sleep/wakeup.c
+++ b/drivers/acpi/sleep/wakeup.c
@@ -42,7 +42,7 @@ void acpi_enable_wakeup_device_prep(u8 sleep_state)
42 continue; 42 continue;
43 43
44 spin_unlock(&acpi_device_lock); 44 spin_unlock(&acpi_device_lock);
45 acpi_enable_wakeup_device_power(dev); 45 acpi_enable_wakeup_device_power(dev, sleep_state);
46 spin_lock(&acpi_device_lock); 46 spin_lock(&acpi_device_lock);
47 } 47 }
48 spin_unlock(&acpi_device_lock); 48 spin_unlock(&acpi_device_lock);
@@ -66,13 +66,15 @@ void acpi_enable_wakeup_device(u8 sleep_state)
66 list_for_each_safe(node, next, &acpi_wakeup_device_list) { 66 list_for_each_safe(node, next, &acpi_wakeup_device_list) {
67 struct acpi_device *dev = 67 struct acpi_device *dev =
68 container_of(node, struct acpi_device, wakeup_list); 68 container_of(node, struct acpi_device, wakeup_list);
69
69 if (!dev->wakeup.flags.valid) 70 if (!dev->wakeup.flags.valid)
70 continue; 71 continue;
72
71 /* If users want to disable run-wake GPE, 73 /* If users want to disable run-wake GPE,
72 * we only disable it for wake and leave it for runtime 74 * we only disable it for wake and leave it for runtime
73 */ 75 */
74 if (!dev->wakeup.state.enabled || 76 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
75 sleep_state > (u32) dev->wakeup.sleep_state) { 77 || sleep_state > (u32) dev->wakeup.sleep_state) {
76 if (dev->wakeup.flags.run_wake) { 78 if (dev->wakeup.flags.run_wake) {
77 spin_unlock(&acpi_device_lock); 79 spin_unlock(&acpi_device_lock);
78 /* set_gpe_type will disable GPE, leave it like that */ 80 /* set_gpe_type will disable GPE, leave it like that */
@@ -110,8 +112,9 @@ void acpi_disable_wakeup_device(u8 sleep_state)
110 112
111 if (!dev->wakeup.flags.valid) 113 if (!dev->wakeup.flags.valid)
112 continue; 114 continue;
113 if (!dev->wakeup.state.enabled || 115
114 sleep_state > (u32) dev->wakeup.sleep_state) { 116 if ((!dev->wakeup.state.enabled && !dev->wakeup.flags.prepared)
117 || sleep_state > (u32) dev->wakeup.sleep_state) {
115 if (dev->wakeup.flags.run_wake) { 118 if (dev->wakeup.flags.run_wake) {
116 spin_unlock(&acpi_device_lock); 119 spin_unlock(&acpi_device_lock);
117 acpi_set_gpe_type(dev->wakeup.gpe_device, 120 acpi_set_gpe_type(dev->wakeup.gpe_device,
diff --git a/drivers/acpi/system.c b/drivers/acpi/system.c
index 5bd2dec9a7ac..d8e3f153b295 100644
--- a/drivers/acpi/system.c
+++ b/drivers/acpi/system.c
@@ -167,7 +167,13 @@ static int acpi_system_sysfs_init(void)
167#define COUNT_ERROR 2 /* other */ 167#define COUNT_ERROR 2 /* other */
168#define NUM_COUNTERS_EXTRA 3 168#define NUM_COUNTERS_EXTRA 3
169 169
170static u32 *all_counters; 170#define ACPI_EVENT_VALID 0x01
171struct event_counter {
172 u32 count;
173 u32 flags;
174};
175
176static struct event_counter *all_counters;
171static u32 num_gpes; 177static u32 num_gpes;
172static u32 num_counters; 178static u32 num_counters;
173static struct attribute **all_attrs; 179static struct attribute **all_attrs;
@@ -202,9 +208,44 @@ static int count_num_gpes(void)
202 return count; 208 return count;
203} 209}
204 210
211static int get_gpe_device(int index, acpi_handle *handle)
212{
213 struct acpi_gpe_xrupt_info *gpe_xrupt_info;
214 struct acpi_gpe_block_info *gpe_block;
215 acpi_cpu_flags flags;
216 struct acpi_namespace_node *node;
217
218 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
219
220 gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
221 while (gpe_xrupt_info) {
222 gpe_block = gpe_xrupt_info->gpe_block_list_head;
223 node = gpe_block->node;
224 while (gpe_block) {
225 index -= gpe_block->register_count *
226 ACPI_GPE_REGISTER_WIDTH;
227 if (index < 0) {
228 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
229 /* return NULL if it's FADT GPE */
230 if (node->type != ACPI_TYPE_DEVICE)
231 *handle = NULL;
232 else
233 *handle = node;
234 return 0;
235 }
236 node = gpe_block->node;
237 gpe_block = gpe_block->next;
238 }
239 gpe_xrupt_info = gpe_xrupt_info->next;
240 }
241 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
242
243 return -ENODEV;
244}
245
205static void delete_gpe_attr_array(void) 246static void delete_gpe_attr_array(void)
206{ 247{
207 u32 *tmp = all_counters; 248 struct event_counter *tmp = all_counters;
208 249
209 all_counters = NULL; 250 all_counters = NULL;
210 kfree(tmp); 251 kfree(tmp);
@@ -230,9 +271,10 @@ void acpi_os_gpe_count(u32 gpe_number)
230 return; 271 return;
231 272
232 if (gpe_number < num_gpes) 273 if (gpe_number < num_gpes)
233 all_counters[gpe_number]++; 274 all_counters[gpe_number].count++;
234 else 275 else
235 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR]++; 276 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
277 count++;
236 278
237 return; 279 return;
238} 280}
@@ -243,44 +285,144 @@ void acpi_os_fixed_event_count(u32 event_number)
243 return; 285 return;
244 286
245 if (event_number < ACPI_NUM_FIXED_EVENTS) 287 if (event_number < ACPI_NUM_FIXED_EVENTS)
246 all_counters[num_gpes + event_number]++; 288 all_counters[num_gpes + event_number].count++;
247 else 289 else
248 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR]++; 290 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
291 count++;
249 292
250 return; 293 return;
251} 294}
252 295
296static int get_status(u32 index, acpi_event_status *status, acpi_handle *handle)
297{
298 int result = 0;
299
300 if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
301 goto end;
302
303 if (index < num_gpes) {
304 result = get_gpe_device(index, handle);
305 if (result) {
306 ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
307 "Invalid GPE 0x%x\n", index));
308 goto end;
309 }
310 result = acpi_get_gpe_status(*handle, index,
311 ACPI_NOT_ISR, status);
312 } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
313 result = acpi_get_event_status(index - num_gpes, status);
314
315 /*
316 * sleep/power button GPE/Fixed Event is enabled after acpi_system_init,
317 * check the status at runtime and mark it as valid once it's enabled
318 */
319 if (!result && (*status & ACPI_EVENT_FLAG_ENABLED))
320 all_counters[index].flags |= ACPI_EVENT_VALID;
321end:
322 return result;
323}
324
253static ssize_t counter_show(struct kobject *kobj, 325static ssize_t counter_show(struct kobject *kobj,
254 struct kobj_attribute *attr, char *buf) 326 struct kobj_attribute *attr, char *buf)
255{ 327{
256 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI] = 328 int index = attr - counter_attrs;
329 int size;
330 acpi_handle handle;
331 acpi_event_status status;
332 int result = 0;
333
334 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
257 acpi_irq_handled; 335 acpi_irq_handled;
258 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE] = 336 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
259 acpi_gpe_count; 337 acpi_gpe_count;
260 338
261 return sprintf(buf, "%d\n", all_counters[attr - counter_attrs]); 339 size = sprintf(buf, "%8d", all_counters[index].count);
340
341 /* "gpe_all" or "sci" */
342 if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
343 goto end;
344
345 result = get_status(index, &status, &handle);
346 if (result)
347 goto end;
348
349 if (!(all_counters[index].flags & ACPI_EVENT_VALID))
350 size += sprintf(buf + size, " invalid");
351 else if (status & ACPI_EVENT_FLAG_ENABLED)
352 size += sprintf(buf + size, " enable");
353 else
354 size += sprintf(buf + size, " disable");
355
356end:
357 size += sprintf(buf + size, "\n");
358 return result ? result : size;
262} 359}
263 360
264/* 361/*
265 * counter_set() sets the specified counter. 362 * counter_set() sets the specified counter.
266 * setting the total "sci" file to any value clears all counters. 363 * setting the total "sci" file to any value clears all counters.
364 * enable/disable/clear a gpe/fixed event in user space.
267 */ 365 */
268static ssize_t counter_set(struct kobject *kobj, 366static ssize_t counter_set(struct kobject *kobj,
269 struct kobj_attribute *attr, const char *buf, size_t size) 367 struct kobj_attribute *attr, const char *buf, size_t size)
270{ 368{
271 int index = attr - counter_attrs; 369 int index = attr - counter_attrs;
370 acpi_event_status status;
371 acpi_handle handle;
372 int result = 0;
272 373
273 if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) { 374 if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
274 int i; 375 int i;
275 for (i = 0; i < num_counters; ++i) 376 for (i = 0; i < num_counters; ++i)
276 all_counters[i] = 0; 377 all_counters[i].count = 0;
277 acpi_gpe_count = 0; 378 acpi_gpe_count = 0;
278 acpi_irq_handled = 0; 379 acpi_irq_handled = 0;
380 goto end;
381 }
279 382
383 /* show the event status for both GPEs and Fixed Events */
384 result = get_status(index, &status, &handle);
385 if (result)
386 goto end;
387
388 if (!(all_counters[index].flags & ACPI_EVENT_VALID)) {
389 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
390 "Can not change Invalid GPE/Fixed Event status\n"));
391 return -EINVAL;
392 }
393
394 if (index < num_gpes) {
395 if (!strcmp(buf, "disable\n") &&
396 (status & ACPI_EVENT_FLAG_ENABLED))
397 result = acpi_disable_gpe(handle, index, ACPI_NOT_ISR);
398 else if (!strcmp(buf, "enable\n") &&
399 !(status & ACPI_EVENT_FLAG_ENABLED))
400 result = acpi_enable_gpe(handle, index, ACPI_NOT_ISR);
401 else if (!strcmp(buf, "clear\n") &&
402 (status & ACPI_EVENT_FLAG_SET))
403 result = acpi_clear_gpe(handle, index, ACPI_NOT_ISR);
404 else
405 all_counters[index].count = strtoul(buf, NULL, 0);
406 } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
407 int event = index - num_gpes;
408 if (!strcmp(buf, "disable\n") &&
409 (status & ACPI_EVENT_FLAG_ENABLED))
410 result = acpi_disable_event(event, ACPI_NOT_ISR);
411 else if (!strcmp(buf, "enable\n") &&
412 !(status & ACPI_EVENT_FLAG_ENABLED))
413 result = acpi_enable_event(event, ACPI_NOT_ISR);
414 else if (!strcmp(buf, "clear\n") &&
415 (status & ACPI_EVENT_FLAG_SET))
416 result = acpi_clear_event(event);
417 else
418 all_counters[index].count = strtoul(buf, NULL, 0);
280 } else 419 } else
281 all_counters[index] = strtoul(buf, NULL, 0); 420 all_counters[index].count = strtoul(buf, NULL, 0);
282 421
283 return size; 422 if (ACPI_FAILURE(result))
423 result = -EINVAL;
424end:
425 return result ? result : size;
284} 426}
285 427
286void acpi_irq_stats_init(void) 428void acpi_irq_stats_init(void)
@@ -298,7 +440,8 @@ void acpi_irq_stats_init(void)
298 if (all_attrs == NULL) 440 if (all_attrs == NULL)
299 return; 441 return;
300 442
301 all_counters = kzalloc(sizeof(u32) * (num_counters), GFP_KERNEL); 443 all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
444 GFP_KERNEL);
302 if (all_counters == NULL) 445 if (all_counters == NULL)
303 goto fail; 446 goto fail;
304 447
diff --git a/drivers/acpi/tables/tbfadt.c b/drivers/acpi/tables/tbfadt.c
index 949d4114eb9f..ccb5b64bbef3 100644
--- a/drivers/acpi/tables/tbfadt.c
+++ b/drivers/acpi/tables/tbfadt.c
@@ -124,7 +124,7 @@ static struct acpi_fadt_info fadt_info_table[] = {
124 124
125static void inline 125static void inline
126acpi_tb_init_generic_address(struct acpi_generic_address *generic_address, 126acpi_tb_init_generic_address(struct acpi_generic_address *generic_address,
127 u8 bit_width, u64 address) 127 u8 byte_width, u64 address)
128{ 128{
129 129
130 /* 130 /*
@@ -136,7 +136,7 @@ acpi_tb_init_generic_address(struct acpi_generic_address *generic_address,
136 /* All other fields are byte-wide */ 136 /* All other fields are byte-wide */
137 137
138 generic_address->space_id = ACPI_ADR_SPACE_SYSTEM_IO; 138 generic_address->space_id = ACPI_ADR_SPACE_SYSTEM_IO;
139 generic_address->bit_width = bit_width; 139 generic_address->bit_width = byte_width << 3;
140 generic_address->bit_offset = 0; 140 generic_address->bit_offset = 0;
141 generic_address->access_width = 0; 141 generic_address->access_width = 0;
142} 142}
@@ -155,7 +155,7 @@ acpi_tb_init_generic_address(struct acpi_generic_address *generic_address,
155 * 155 *
156 ******************************************************************************/ 156 ******************************************************************************/
157 157
158void acpi_tb_parse_fadt(acpi_native_uint table_index, u8 flags) 158void acpi_tb_parse_fadt(u32 table_index, u8 flags)
159{ 159{
160 u32 length; 160 u32 length;
161 struct acpi_table_header *table; 161 struct acpi_table_header *table;
@@ -280,7 +280,7 @@ static void acpi_tb_convert_fadt(void)
280{ 280{
281 u8 pm1_register_length; 281 u8 pm1_register_length;
282 struct acpi_generic_address *target; 282 struct acpi_generic_address *target;
283 acpi_native_uint i; 283 u32 i;
284 284
285 /* Update the local FADT table header length */ 285 /* Update the local FADT table header length */
286 286
@@ -343,9 +343,11 @@ static void acpi_tb_convert_fadt(void)
343 * 343 *
344 * The PM event blocks are split into two register blocks, first is the 344 * The PM event blocks are split into two register blocks, first is the
345 * PM Status Register block, followed immediately by the PM Enable Register 345 * PM Status Register block, followed immediately by the PM Enable Register
346 * block. Each is of length (pm1_event_length/2) 346 * block. Each is of length (xpm1x_event_block.bit_width/2)
347 */ 347 */
348 pm1_register_length = (u8) ACPI_DIV_2(acpi_gbl_FADT.pm1_event_length); 348 WARN_ON(ACPI_MOD_16(acpi_gbl_FADT.xpm1a_event_block.bit_width));
349 pm1_register_length = (u8) ACPI_DIV_16(acpi_gbl_FADT
350 .xpm1a_event_block.bit_width);
349 351
350 /* The PM1A register block is required */ 352 /* The PM1A register block is required */
351 353
@@ -360,14 +362,17 @@ static void acpi_tb_convert_fadt(void)
360 /* The PM1B register block is optional, ignore if not present */ 362 /* The PM1B register block is optional, ignore if not present */
361 363
362 if (acpi_gbl_FADT.xpm1b_event_block.address) { 364 if (acpi_gbl_FADT.xpm1b_event_block.address) {
365 WARN_ON(ACPI_MOD_16(acpi_gbl_FADT.xpm1b_event_block.bit_width));
366 pm1_register_length = (u8) ACPI_DIV_16(acpi_gbl_FADT
367 .xpm1b_event_block
368 .bit_width);
363 acpi_tb_init_generic_address(&acpi_gbl_xpm1b_enable, 369 acpi_tb_init_generic_address(&acpi_gbl_xpm1b_enable,
364 pm1_register_length, 370 pm1_register_length,
365 (acpi_gbl_FADT.xpm1b_event_block. 371 (acpi_gbl_FADT.xpm1b_event_block.
366 address + pm1_register_length)); 372 address + pm1_register_length));
367 /* Don't forget to copy space_id of the GAS */ 373 /* Don't forget to copy space_id of the GAS */
368 acpi_gbl_xpm1b_enable.space_id = 374 acpi_gbl_xpm1b_enable.space_id =
369 acpi_gbl_FADT.xpm1a_event_block.space_id; 375 acpi_gbl_FADT.xpm1b_event_block.space_id;
370
371 } 376 }
372} 377}
373 378
@@ -396,7 +401,7 @@ static void acpi_tb_validate_fadt(void)
396 u32 *address32; 401 u32 *address32;
397 struct acpi_generic_address *address64; 402 struct acpi_generic_address *address64;
398 u8 length; 403 u8 length;
399 acpi_native_uint i; 404 u32 i;
400 405
401 /* Examine all of the 64-bit extended address fields (X fields) */ 406 /* Examine all of the 64-bit extended address fields (X fields) */
402 407
diff --git a/drivers/acpi/tables/tbfind.c b/drivers/acpi/tables/tbfind.c
index 9ca3afc98c80..531584defbb8 100644
--- a/drivers/acpi/tables/tbfind.c
+++ b/drivers/acpi/tables/tbfind.c
@@ -65,10 +65,9 @@ ACPI_MODULE_NAME("tbfind")
65 ******************************************************************************/ 65 ******************************************************************************/
66acpi_status 66acpi_status
67acpi_tb_find_table(char *signature, 67acpi_tb_find_table(char *signature,
68 char *oem_id, 68 char *oem_id, char *oem_table_id, u32 *table_index)
69 char *oem_table_id, acpi_native_uint * table_index)
70{ 69{
71 acpi_native_uint i; 70 u32 i;
72 acpi_status status; 71 acpi_status status;
73 struct acpi_table_header header; 72 struct acpi_table_header header;
74 73
diff --git a/drivers/acpi/tables/tbinstal.c b/drivers/acpi/tables/tbinstal.c
index 5336ce88f89f..b22185f55a16 100644
--- a/drivers/acpi/tables/tbinstal.c
+++ b/drivers/acpi/tables/tbinstal.c
@@ -107,11 +107,10 @@ acpi_status acpi_tb_verify_table(struct acpi_table_desc *table_desc)
107 ******************************************************************************/ 107 ******************************************************************************/
108 108
109acpi_status 109acpi_status
110acpi_tb_add_table(struct acpi_table_desc *table_desc, 110acpi_tb_add_table(struct acpi_table_desc *table_desc, u32 *table_index)
111 acpi_native_uint * table_index)
112{ 111{
113 acpi_native_uint i; 112 u32 i;
114 acpi_native_uint length; 113 u32 length;
115 acpi_status status = AE_OK; 114 acpi_status status = AE_OK;
116 115
117 ACPI_FUNCTION_TRACE(tb_add_table); 116 ACPI_FUNCTION_TRACE(tb_add_table);
@@ -207,8 +206,8 @@ acpi_status acpi_tb_resize_root_table_list(void)
207 206
208 /* Increase the Table Array size */ 207 /* Increase the Table Array size */
209 208
210 tables = ACPI_ALLOCATE_ZEROED((acpi_gbl_root_table_list.size + 209 tables = ACPI_ALLOCATE_ZEROED(((acpi_size) acpi_gbl_root_table_list.
211 ACPI_ROOT_TABLE_SIZE_INCREMENT) 210 size + ACPI_ROOT_TABLE_SIZE_INCREMENT)
212 * sizeof(struct acpi_table_desc)); 211 * sizeof(struct acpi_table_desc));
213 if (!tables) { 212 if (!tables) {
214 ACPI_ERROR((AE_INFO, 213 ACPI_ERROR((AE_INFO,
@@ -220,7 +219,7 @@ acpi_status acpi_tb_resize_root_table_list(void)
220 219
221 if (acpi_gbl_root_table_list.tables) { 220 if (acpi_gbl_root_table_list.tables) {
222 ACPI_MEMCPY(tables, acpi_gbl_root_table_list.tables, 221 ACPI_MEMCPY(tables, acpi_gbl_root_table_list.tables,
223 acpi_gbl_root_table_list.size * 222 (acpi_size) acpi_gbl_root_table_list.size *
224 sizeof(struct acpi_table_desc)); 223 sizeof(struct acpi_table_desc));
225 224
226 if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) { 225 if (acpi_gbl_root_table_list.flags & ACPI_ROOT_ORIGIN_ALLOCATED) {
@@ -253,7 +252,7 @@ acpi_status acpi_tb_resize_root_table_list(void)
253acpi_status 252acpi_status
254acpi_tb_store_table(acpi_physical_address address, 253acpi_tb_store_table(acpi_physical_address address,
255 struct acpi_table_header *table, 254 struct acpi_table_header *table,
256 u32 length, u8 flags, acpi_native_uint * table_index) 255 u32 length, u8 flags, u32 *table_index)
257{ 256{
258 acpi_status status = AE_OK; 257 acpi_status status = AE_OK;
259 258
@@ -334,7 +333,7 @@ void acpi_tb_delete_table(struct acpi_table_desc *table_desc)
334 333
335void acpi_tb_terminate(void) 334void acpi_tb_terminate(void)
336{ 335{
337 acpi_native_uint i; 336 u32 i;
338 337
339 ACPI_FUNCTION_TRACE(tb_terminate); 338 ACPI_FUNCTION_TRACE(tb_terminate);
340 339
@@ -374,7 +373,7 @@ void acpi_tb_terminate(void)
374 * 373 *
375 ******************************************************************************/ 374 ******************************************************************************/
376 375
377void acpi_tb_delete_namespace_by_owner(acpi_native_uint table_index) 376void acpi_tb_delete_namespace_by_owner(u32 table_index)
378{ 377{
379 acpi_owner_id owner_id; 378 acpi_owner_id owner_id;
380 379
@@ -403,7 +402,7 @@ void acpi_tb_delete_namespace_by_owner(acpi_native_uint table_index)
403 * 402 *
404 ******************************************************************************/ 403 ******************************************************************************/
405 404
406acpi_status acpi_tb_allocate_owner_id(acpi_native_uint table_index) 405acpi_status acpi_tb_allocate_owner_id(u32 table_index)
407{ 406{
408 acpi_status status = AE_BAD_PARAMETER; 407 acpi_status status = AE_BAD_PARAMETER;
409 408
@@ -431,7 +430,7 @@ acpi_status acpi_tb_allocate_owner_id(acpi_native_uint table_index)
431 * 430 *
432 ******************************************************************************/ 431 ******************************************************************************/
433 432
434acpi_status acpi_tb_release_owner_id(acpi_native_uint table_index) 433acpi_status acpi_tb_release_owner_id(u32 table_index)
435{ 434{
436 acpi_status status = AE_BAD_PARAMETER; 435 acpi_status status = AE_BAD_PARAMETER;
437 436
@@ -462,8 +461,7 @@ acpi_status acpi_tb_release_owner_id(acpi_native_uint table_index)
462 * 461 *
463 ******************************************************************************/ 462 ******************************************************************************/
464 463
465acpi_status 464acpi_status acpi_tb_get_owner_id(u32 table_index, acpi_owner_id *owner_id)
466acpi_tb_get_owner_id(acpi_native_uint table_index, acpi_owner_id * owner_id)
467{ 465{
468 acpi_status status = AE_BAD_PARAMETER; 466 acpi_status status = AE_BAD_PARAMETER;
469 467
@@ -490,7 +488,7 @@ acpi_tb_get_owner_id(acpi_native_uint table_index, acpi_owner_id * owner_id)
490 * 488 *
491 ******************************************************************************/ 489 ******************************************************************************/
492 490
493u8 acpi_tb_is_table_loaded(acpi_native_uint table_index) 491u8 acpi_tb_is_table_loaded(u32 table_index)
494{ 492{
495 u8 is_loaded = FALSE; 493 u8 is_loaded = FALSE;
496 494
@@ -518,7 +516,7 @@ u8 acpi_tb_is_table_loaded(acpi_native_uint table_index)
518 * 516 *
519 ******************************************************************************/ 517 ******************************************************************************/
520 518
521void acpi_tb_set_table_loaded_flag(acpi_native_uint table_index, u8 is_loaded) 519void acpi_tb_set_table_loaded_flag(u32 table_index, u8 is_loaded)
522{ 520{
523 521
524 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES); 522 (void)acpi_ut_acquire_mutex(ACPI_MTX_TABLES);
diff --git a/drivers/acpi/tables/tbutils.c b/drivers/acpi/tables/tbutils.c
index bc019b9b6a68..0cc92ef5236f 100644
--- a/drivers/acpi/tables/tbutils.c
+++ b/drivers/acpi/tables/tbutils.c
@@ -49,8 +49,8 @@ ACPI_MODULE_NAME("tbutils")
49 49
50/* Local prototypes */ 50/* Local prototypes */
51static acpi_physical_address 51static acpi_physical_address
52acpi_tb_get_root_table_entry(u8 * table_entry, 52acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
53 acpi_native_uint table_entry_size); 53
54/******************************************************************************* 54/*******************************************************************************
55 * 55 *
56 * FUNCTION: acpi_tb_check_xsdt 56 * FUNCTION: acpi_tb_check_xsdt
@@ -238,7 +238,7 @@ acpi_status acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
238 * 238 *
239 ******************************************************************************/ 239 ******************************************************************************/
240 240
241u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length) 241u8 acpi_tb_checksum(u8 *buffer, u32 length)
242{ 242{
243 u8 sum = 0; 243 u8 sum = 0;
244 u8 *end = buffer + length; 244 u8 *end = buffer + length;
@@ -268,7 +268,7 @@ u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length)
268 268
269void 269void
270acpi_tb_install_table(acpi_physical_address address, 270acpi_tb_install_table(acpi_physical_address address,
271 u8 flags, char *signature, acpi_native_uint table_index) 271 u8 flags, char *signature, u32 table_index)
272{ 272{
273 struct acpi_table_header *table; 273 struct acpi_table_header *table;
274 274
@@ -336,8 +336,7 @@ acpi_tb_install_table(acpi_physical_address address,
336 ******************************************************************************/ 336 ******************************************************************************/
337 337
338static acpi_physical_address 338static acpi_physical_address
339acpi_tb_get_root_table_entry(u8 * table_entry, 339acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
340 acpi_native_uint table_entry_size)
341{ 340{
342 u64 address64; 341 u64 address64;
343 342
@@ -395,8 +394,8 @@ acpi_status __init
395acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags) 394acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags)
396{ 395{
397 struct acpi_table_rsdp *rsdp; 396 struct acpi_table_rsdp *rsdp;
398 acpi_native_uint table_entry_size; 397 u32 table_entry_size;
399 acpi_native_uint i; 398 u32 i;
400 u32 table_count; 399 u32 table_count;
401 struct acpi_table_header *table; 400 struct acpi_table_header *table;
402 acpi_physical_address address; 401 acpi_physical_address address;
diff --git a/drivers/acpi/tables/tbxface.c b/drivers/acpi/tables/tbxface.c
index 0e319604d3e7..fd7770aa1061 100644
--- a/drivers/acpi/tables/tbxface.c
+++ b/drivers/acpi/tables/tbxface.c
@@ -125,7 +125,7 @@ acpi_initialize_tables(struct acpi_table_desc * initial_table_array,
125 /* Root Table Array has been statically allocated by the host */ 125 /* Root Table Array has been statically allocated by the host */
126 126
127 ACPI_MEMSET(initial_table_array, 0, 127 ACPI_MEMSET(initial_table_array, 0,
128 initial_table_count * 128 (acpi_size) initial_table_count *
129 sizeof(struct acpi_table_desc)); 129 sizeof(struct acpi_table_desc));
130 130
131 acpi_gbl_root_table_list.tables = initial_table_array; 131 acpi_gbl_root_table_list.tables = initial_table_array;
@@ -183,9 +183,9 @@ acpi_status acpi_reallocate_root_table(void)
183 return_ACPI_STATUS(AE_SUPPORT); 183 return_ACPI_STATUS(AE_SUPPORT);
184 } 184 }
185 185
186 new_size = 186 new_size = ((acpi_size) acpi_gbl_root_table_list.count +
187 (acpi_gbl_root_table_list.count + 187 ACPI_ROOT_TABLE_SIZE_INCREMENT) *
188 ACPI_ROOT_TABLE_SIZE_INCREMENT) * sizeof(struct acpi_table_desc); 188 sizeof(struct acpi_table_desc);
189 189
190 /* Create new array and copy the old array */ 190 /* Create new array and copy the old array */
191 191
@@ -222,7 +222,7 @@ acpi_status acpi_reallocate_root_table(void)
222acpi_status acpi_load_table(struct acpi_table_header *table_ptr) 222acpi_status acpi_load_table(struct acpi_table_header *table_ptr)
223{ 223{
224 acpi_status status; 224 acpi_status status;
225 acpi_native_uint table_index; 225 u32 table_index;
226 struct acpi_table_desc table_desc; 226 struct acpi_table_desc table_desc;
227 227
228 if (!table_ptr) 228 if (!table_ptr)
@@ -264,11 +264,10 @@ ACPI_EXPORT_SYMBOL(acpi_load_table)
264 *****************************************************************************/ 264 *****************************************************************************/
265acpi_status 265acpi_status
266acpi_get_table_header(char *signature, 266acpi_get_table_header(char *signature,
267 acpi_native_uint instance, 267 u32 instance, struct acpi_table_header *out_table_header)
268 struct acpi_table_header * out_table_header)
269{ 268{
270 acpi_native_uint i; 269 u32 i;
271 acpi_native_uint j; 270 u32 j;
272 struct acpi_table_header *header; 271 struct acpi_table_header *header;
273 272
274 /* Parameter validation */ 273 /* Parameter validation */
@@ -378,10 +377,10 @@ ACPI_EXPORT_SYMBOL(acpi_unload_table_id)
378 *****************************************************************************/ 377 *****************************************************************************/
379acpi_status 378acpi_status
380acpi_get_table(char *signature, 379acpi_get_table(char *signature,
381 acpi_native_uint instance, struct acpi_table_header **out_table) 380 u32 instance, struct acpi_table_header **out_table)
382{ 381{
383 acpi_native_uint i; 382 u32 i;
384 acpi_native_uint j; 383 u32 j;
385 acpi_status status; 384 acpi_status status;
386 385
387 /* Parameter validation */ 386 /* Parameter validation */
@@ -435,8 +434,7 @@ ACPI_EXPORT_SYMBOL(acpi_get_table)
435 * 434 *
436 ******************************************************************************/ 435 ******************************************************************************/
437acpi_status 436acpi_status
438acpi_get_table_by_index(acpi_native_uint table_index, 437acpi_get_table_by_index(u32 table_index, struct acpi_table_header **table)
439 struct acpi_table_header ** table)
440{ 438{
441 acpi_status status; 439 acpi_status status;
442 440
@@ -493,7 +491,7 @@ static acpi_status acpi_tb_load_namespace(void)
493{ 491{
494 acpi_status status; 492 acpi_status status;
495 struct acpi_table_header *table; 493 struct acpi_table_header *table;
496 acpi_native_uint i; 494 u32 i;
497 495
498 ACPI_FUNCTION_TRACE(tb_load_namespace); 496 ACPI_FUNCTION_TRACE(tb_load_namespace);
499 497
diff --git a/drivers/acpi/tables/tbxfroot.c b/drivers/acpi/tables/tbxfroot.c
index b8c0dfa084f6..2d157e0f98d2 100644
--- a/drivers/acpi/tables/tbxfroot.c
+++ b/drivers/acpi/tables/tbxfroot.c
@@ -118,7 +118,7 @@ static acpi_status acpi_tb_validate_rsdp(struct acpi_table_rsdp *rsdp)
118 * 118 *
119 ******************************************************************************/ 119 ******************************************************************************/
120 120
121acpi_status acpi_find_root_pointer(acpi_native_uint * table_address) 121acpi_status acpi_find_root_pointer(acpi_size *table_address)
122{ 122{
123 u8 *table_ptr; 123 u8 *table_ptr;
124 u8 *mem_rover; 124 u8 *mem_rover;
@@ -153,7 +153,7 @@ acpi_status acpi_find_root_pointer(acpi_native_uint * table_address)
153 * 1b) Search EBDA paragraphs (EBDA is required to be a 153 * 1b) Search EBDA paragraphs (EBDA is required to be a
154 * minimum of 1_k length) 154 * minimum of 1_k length)
155 */ 155 */
156 table_ptr = acpi_os_map_memory((acpi_native_uint) 156 table_ptr = acpi_os_map_memory((acpi_physical_address)
157 physical_address, 157 physical_address,
158 ACPI_EBDA_WINDOW_SIZE); 158 ACPI_EBDA_WINDOW_SIZE);
159 if (!table_ptr) { 159 if (!table_ptr) {
diff --git a/drivers/acpi/utilities/utalloc.c b/drivers/acpi/utilities/utalloc.c
index ede084829a70..3dfb8a442b26 100644
--- a/drivers/acpi/utilities/utalloc.c
+++ b/drivers/acpi/utilities/utalloc.c
@@ -309,7 +309,8 @@ acpi_ut_initialize_buffer(struct acpi_buffer * buffer,
309 * 309 *
310 ******************************************************************************/ 310 ******************************************************************************/
311 311
312void *acpi_ut_allocate(acpi_size size, u32 component, char *module, u32 line) 312void *acpi_ut_allocate(acpi_size size,
313 u32 component, const char *module, u32 line)
313{ 314{
314 void *allocation; 315 void *allocation;
315 316
@@ -353,7 +354,7 @@ void *acpi_ut_allocate(acpi_size size, u32 component, char *module, u32 line)
353 ******************************************************************************/ 354 ******************************************************************************/
354 355
355void *acpi_ut_allocate_zeroed(acpi_size size, 356void *acpi_ut_allocate_zeroed(acpi_size size,
356 u32 component, char *module, u32 line) 357 u32 component, const char *module, u32 line)
357{ 358{
358 void *allocation; 359 void *allocation;
359 360
diff --git a/drivers/acpi/utilities/utcopy.c b/drivers/acpi/utilities/utcopy.c
index 655c290aca7b..53499ac90988 100644
--- a/drivers/acpi/utilities/utcopy.c
+++ b/drivers/acpi/utilities/utcopy.c
@@ -572,7 +572,7 @@ acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
572 acpi_status status = AE_OK; 572 acpi_status status = AE_OK;
573 union acpi_operand_object *package_object; 573 union acpi_operand_object *package_object;
574 union acpi_operand_object **package_elements; 574 union acpi_operand_object **package_elements;
575 acpi_native_uint i; 575 u32 i;
576 576
577 ACPI_FUNCTION_TRACE(ut_copy_epackage_to_ipackage); 577 ACPI_FUNCTION_TRACE(ut_copy_epackage_to_ipackage);
578 578
@@ -599,7 +599,7 @@ acpi_ut_copy_epackage_to_ipackage(union acpi_object *external_object,
599 599
600 /* Truncate package and delete it */ 600 /* Truncate package and delete it */
601 601
602 package_object->package.count = (u32) i; 602 package_object->package.count = i;
603 package_elements[i] = NULL; 603 package_elements[i] = NULL;
604 acpi_ut_remove_reference(package_object); 604 acpi_ut_remove_reference(package_object);
605 return_ACPI_STATUS(status); 605 return_ACPI_STATUS(status);
diff --git a/drivers/acpi/utilities/utdebug.c b/drivers/acpi/utilities/utdebug.c
index f938f465efa4..fd66ecb6741e 100644
--- a/drivers/acpi/utilities/utdebug.c
+++ b/drivers/acpi/utilities/utdebug.c
@@ -157,7 +157,8 @@ void ACPI_INTERNAL_VAR_XFACE
157acpi_ut_debug_print(u32 requested_debug_level, 157acpi_ut_debug_print(u32 requested_debug_level,
158 u32 line_number, 158 u32 line_number,
159 const char *function_name, 159 const char *function_name,
160 char *module_name, u32 component_id, char *format, ...) 160 const char *module_name,
161 u32 component_id, const char *format, ...)
161{ 162{
162 acpi_thread_id thread_id; 163 acpi_thread_id thread_id;
163 va_list args; 164 va_list args;
@@ -228,7 +229,8 @@ void ACPI_INTERNAL_VAR_XFACE
228acpi_ut_debug_print_raw(u32 requested_debug_level, 229acpi_ut_debug_print_raw(u32 requested_debug_level,
229 u32 line_number, 230 u32 line_number,
230 const char *function_name, 231 const char *function_name,
231 char *module_name, u32 component_id, char *format, ...) 232 const char *module_name,
233 u32 component_id, const char *format, ...)
232{ 234{
233 va_list args; 235 va_list args;
234 236
@@ -261,7 +263,8 @@ ACPI_EXPORT_SYMBOL(acpi_ut_debug_print_raw)
261 ******************************************************************************/ 263 ******************************************************************************/
262void 264void
263acpi_ut_trace(u32 line_number, 265acpi_ut_trace(u32 line_number,
264 const char *function_name, char *module_name, u32 component_id) 266 const char *function_name,
267 const char *module_name, u32 component_id)
265{ 268{
266 269
267 acpi_gbl_nesting_level++; 270 acpi_gbl_nesting_level++;
@@ -293,7 +296,7 @@ ACPI_EXPORT_SYMBOL(acpi_ut_trace)
293void 296void
294acpi_ut_trace_ptr(u32 line_number, 297acpi_ut_trace_ptr(u32 line_number,
295 const char *function_name, 298 const char *function_name,
296 char *module_name, u32 component_id, void *pointer) 299 const char *module_name, u32 component_id, void *pointer)
297{ 300{
298 acpi_gbl_nesting_level++; 301 acpi_gbl_nesting_level++;
299 acpi_ut_track_stack_ptr(); 302 acpi_ut_track_stack_ptr();
@@ -324,7 +327,7 @@ acpi_ut_trace_ptr(u32 line_number,
324void 327void
325acpi_ut_trace_str(u32 line_number, 328acpi_ut_trace_str(u32 line_number,
326 const char *function_name, 329 const char *function_name,
327 char *module_name, u32 component_id, char *string) 330 const char *module_name, u32 component_id, char *string)
328{ 331{
329 332
330 acpi_gbl_nesting_level++; 333 acpi_gbl_nesting_level++;
@@ -356,7 +359,7 @@ acpi_ut_trace_str(u32 line_number,
356void 359void
357acpi_ut_trace_u32(u32 line_number, 360acpi_ut_trace_u32(u32 line_number,
358 const char *function_name, 361 const char *function_name,
359 char *module_name, u32 component_id, u32 integer) 362 const char *module_name, u32 component_id, u32 integer)
360{ 363{
361 364
362 acpi_gbl_nesting_level++; 365 acpi_gbl_nesting_level++;
@@ -386,7 +389,8 @@ acpi_ut_trace_u32(u32 line_number,
386 389
387void 390void
388acpi_ut_exit(u32 line_number, 391acpi_ut_exit(u32 line_number,
389 const char *function_name, char *module_name, u32 component_id) 392 const char *function_name,
393 const char *module_name, u32 component_id)
390{ 394{
391 395
392 acpi_ut_debug_print(ACPI_LV_FUNCTIONS, 396 acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
@@ -417,7 +421,8 @@ ACPI_EXPORT_SYMBOL(acpi_ut_exit)
417void 421void
418acpi_ut_status_exit(u32 line_number, 422acpi_ut_status_exit(u32 line_number,
419 const char *function_name, 423 const char *function_name,
420 char *module_name, u32 component_id, acpi_status status) 424 const char *module_name,
425 u32 component_id, acpi_status status)
421{ 426{
422 427
423 if (ACPI_SUCCESS(status)) { 428 if (ACPI_SUCCESS(status)) {
@@ -458,7 +463,8 @@ ACPI_EXPORT_SYMBOL(acpi_ut_status_exit)
458void 463void
459acpi_ut_value_exit(u32 line_number, 464acpi_ut_value_exit(u32 line_number,
460 const char *function_name, 465 const char *function_name,
461 char *module_name, u32 component_id, acpi_integer value) 466 const char *module_name,
467 u32 component_id, acpi_integer value)
462{ 468{
463 469
464 acpi_ut_debug_print(ACPI_LV_FUNCTIONS, 470 acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
@@ -490,7 +496,7 @@ ACPI_EXPORT_SYMBOL(acpi_ut_value_exit)
490void 496void
491acpi_ut_ptr_exit(u32 line_number, 497acpi_ut_ptr_exit(u32 line_number,
492 const char *function_name, 498 const char *function_name,
493 char *module_name, u32 component_id, u8 * ptr) 499 const char *module_name, u32 component_id, u8 *ptr)
494{ 500{
495 501
496 acpi_ut_debug_print(ACPI_LV_FUNCTIONS, 502 acpi_ut_debug_print(ACPI_LV_FUNCTIONS,
@@ -519,8 +525,8 @@ acpi_ut_ptr_exit(u32 line_number,
519 525
520void acpi_ut_dump_buffer2(u8 * buffer, u32 count, u32 display) 526void acpi_ut_dump_buffer2(u8 * buffer, u32 count, u32 display)
521{ 527{
522 acpi_native_uint i = 0; 528 u32 i = 0;
523 acpi_native_uint j; 529 u32 j;
524 u32 temp32; 530 u32 temp32;
525 u8 buf_char; 531 u8 buf_char;
526 532
@@ -539,7 +545,7 @@ void acpi_ut_dump_buffer2(u8 * buffer, u32 count, u32 display)
539 545
540 /* Print current offset */ 546 /* Print current offset */
541 547
542 acpi_os_printf("%6.4X: ", (u32) i); 548 acpi_os_printf("%6.4X: ", i);
543 549
544 /* Print 16 hex chars */ 550 /* Print 16 hex chars */
545 551
@@ -549,7 +555,7 @@ void acpi_ut_dump_buffer2(u8 * buffer, u32 count, u32 display)
549 /* Dump fill spaces */ 555 /* Dump fill spaces */
550 556
551 acpi_os_printf("%*s", ((display * 2) + 1), " "); 557 acpi_os_printf("%*s", ((display * 2) + 1), " ");
552 j += (acpi_native_uint) display; 558 j += display;
553 continue; 559 continue;
554 } 560 }
555 561
@@ -557,32 +563,38 @@ void acpi_ut_dump_buffer2(u8 * buffer, u32 count, u32 display)
557 case DB_BYTE_DISPLAY: 563 case DB_BYTE_DISPLAY:
558 default: /* Default is BYTE display */ 564 default: /* Default is BYTE display */
559 565
560 acpi_os_printf("%02X ", buffer[i + j]); 566 acpi_os_printf("%02X ",
567 buffer[(acpi_size) i + j]);
561 break; 568 break;
562 569
563 case DB_WORD_DISPLAY: 570 case DB_WORD_DISPLAY:
564 571
565 ACPI_MOVE_16_TO_32(&temp32, &buffer[i + j]); 572 ACPI_MOVE_16_TO_32(&temp32,
573 &buffer[(acpi_size) i + j]);
566 acpi_os_printf("%04X ", temp32); 574 acpi_os_printf("%04X ", temp32);
567 break; 575 break;
568 576
569 case DB_DWORD_DISPLAY: 577 case DB_DWORD_DISPLAY:
570 578
571 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j]); 579 ACPI_MOVE_32_TO_32(&temp32,
580 &buffer[(acpi_size) i + j]);
572 acpi_os_printf("%08X ", temp32); 581 acpi_os_printf("%08X ", temp32);
573 break; 582 break;
574 583
575 case DB_QWORD_DISPLAY: 584 case DB_QWORD_DISPLAY:
576 585
577 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j]); 586 ACPI_MOVE_32_TO_32(&temp32,
587 &buffer[(acpi_size) i + j]);
578 acpi_os_printf("%08X", temp32); 588 acpi_os_printf("%08X", temp32);
579 589
580 ACPI_MOVE_32_TO_32(&temp32, &buffer[i + j + 4]); 590 ACPI_MOVE_32_TO_32(&temp32,
591 &buffer[(acpi_size) i + j +
592 4]);
581 acpi_os_printf("%08X ", temp32); 593 acpi_os_printf("%08X ", temp32);
582 break; 594 break;
583 } 595 }
584 596
585 j += (acpi_native_uint) display; 597 j += display;
586 } 598 }
587 599
588 /* 600 /*
@@ -596,7 +608,7 @@ void acpi_ut_dump_buffer2(u8 * buffer, u32 count, u32 display)
596 return; 608 return;
597 } 609 }
598 610
599 buf_char = buffer[i + j]; 611 buf_char = buffer[(acpi_size) i + j];
600 if (ACPI_IS_PRINT(buf_char)) { 612 if (ACPI_IS_PRINT(buf_char)) {
601 acpi_os_printf("%c", buf_char); 613 acpi_os_printf("%c", buf_char);
602 } else { 614 } else {
diff --git a/drivers/acpi/utilities/utdelete.c b/drivers/acpi/utilities/utdelete.c
index 1fbc35139e84..c5c791a575c9 100644
--- a/drivers/acpi/utilities/utdelete.c
+++ b/drivers/acpi/utilities/utdelete.c
@@ -442,7 +442,7 @@ acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
442 union acpi_generic_state *state_list = NULL; 442 union acpi_generic_state *state_list = NULL;
443 union acpi_operand_object *next_object = NULL; 443 union acpi_operand_object *next_object = NULL;
444 union acpi_generic_state *state; 444 union acpi_generic_state *state;
445 acpi_native_uint i; 445 u32 i;
446 446
447 ACPI_FUNCTION_TRACE_PTR(ut_update_object_reference, object); 447 ACPI_FUNCTION_TRACE_PTR(ut_update_object_reference, object);
448 448
diff --git a/drivers/acpi/utilities/uteval.c b/drivers/acpi/utilities/uteval.c
index 05e61be267d5..352747e49c7a 100644
--- a/drivers/acpi/utilities/uteval.c
+++ b/drivers/acpi/utilities/uteval.c
@@ -97,7 +97,7 @@ acpi_status acpi_ut_osi_implementation(struct acpi_walk_state *walk_state)
97 acpi_status status; 97 acpi_status status;
98 union acpi_operand_object *string_desc; 98 union acpi_operand_object *string_desc;
99 union acpi_operand_object *return_desc; 99 union acpi_operand_object *return_desc;
100 acpi_native_uint i; 100 u32 i;
101 101
102 ACPI_FUNCTION_TRACE(ut_osi_implementation); 102 ACPI_FUNCTION_TRACE(ut_osi_implementation);
103 103
@@ -217,7 +217,6 @@ acpi_ut_evaluate_object(struct acpi_namespace_node *prefix_node,
217 217
218 info->prefix_node = prefix_node; 218 info->prefix_node = prefix_node;
219 info->pathname = path; 219 info->pathname = path;
220 info->parameter_type = ACPI_PARAM_ARGS;
221 220
222 /* Evaluate the object/method */ 221 /* Evaluate the object/method */
223 222
@@ -514,7 +513,7 @@ acpi_ut_execute_CID(struct acpi_namespace_node * device_node,
514 u32 count; 513 u32 count;
515 u32 size; 514 u32 size;
516 struct acpi_compatible_id_list *cid_list; 515 struct acpi_compatible_id_list *cid_list;
517 acpi_native_uint i; 516 u32 i;
518 517
519 ACPI_FUNCTION_TRACE(ut_execute_CID); 518 ACPI_FUNCTION_TRACE(ut_execute_CID);
520 519
diff --git a/drivers/acpi/utilities/utmisc.c b/drivers/acpi/utilities/utmisc.c
index 1f057b71db1a..f34be6773556 100644
--- a/drivers/acpi/utilities/utmisc.c
+++ b/drivers/acpi/utilities/utmisc.c
@@ -64,7 +64,7 @@ ACPI_MODULE_NAME("utmisc")
64 ******************************************************************************/ 64 ******************************************************************************/
65const char *acpi_ut_validate_exception(acpi_status status) 65const char *acpi_ut_validate_exception(acpi_status status)
66{ 66{
67 acpi_status sub_status; 67 u32 sub_status;
68 const char *exception = NULL; 68 const char *exception = NULL;
69 69
70 ACPI_FUNCTION_ENTRY(); 70 ACPI_FUNCTION_ENTRY();
@@ -85,32 +85,28 @@ const char *acpi_ut_validate_exception(acpi_status status)
85 case AE_CODE_PROGRAMMER: 85 case AE_CODE_PROGRAMMER:
86 86
87 if (sub_status <= AE_CODE_PGM_MAX) { 87 if (sub_status <= AE_CODE_PGM_MAX) {
88 exception = 88 exception = acpi_gbl_exception_names_pgm[sub_status];
89 acpi_gbl_exception_names_pgm[sub_status - 1];
90 } 89 }
91 break; 90 break;
92 91
93 case AE_CODE_ACPI_TABLES: 92 case AE_CODE_ACPI_TABLES:
94 93
95 if (sub_status <= AE_CODE_TBL_MAX) { 94 if (sub_status <= AE_CODE_TBL_MAX) {
96 exception = 95 exception = acpi_gbl_exception_names_tbl[sub_status];
97 acpi_gbl_exception_names_tbl[sub_status - 1];
98 } 96 }
99 break; 97 break;
100 98
101 case AE_CODE_AML: 99 case AE_CODE_AML:
102 100
103 if (sub_status <= AE_CODE_AML_MAX) { 101 if (sub_status <= AE_CODE_AML_MAX) {
104 exception = 102 exception = acpi_gbl_exception_names_aml[sub_status];
105 acpi_gbl_exception_names_aml[sub_status - 1];
106 } 103 }
107 break; 104 break;
108 105
109 case AE_CODE_CONTROL: 106 case AE_CODE_CONTROL:
110 107
111 if (sub_status <= AE_CODE_CTRL_MAX) { 108 if (sub_status <= AE_CODE_CTRL_MAX) {
112 exception = 109 exception = acpi_gbl_exception_names_ctrl[sub_status];
113 acpi_gbl_exception_names_ctrl[sub_status - 1];
114 } 110 }
115 break; 111 break;
116 112
@@ -165,9 +161,9 @@ u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
165 161
166acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id) 162acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
167{ 163{
168 acpi_native_uint i; 164 u32 i;
169 acpi_native_uint j; 165 u32 j;
170 acpi_native_uint k; 166 u32 k;
171 acpi_status status; 167 acpi_status status;
172 168
173 ACPI_FUNCTION_TRACE(ut_allocate_owner_id); 169 ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
@@ -273,7 +269,7 @@ void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
273{ 269{
274 acpi_owner_id owner_id = *owner_id_ptr; 270 acpi_owner_id owner_id = *owner_id_ptr;
275 acpi_status status; 271 acpi_status status;
276 acpi_native_uint index; 272 u32 index;
277 u32 bit; 273 u32 bit;
278 274
279 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id); 275 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
@@ -593,7 +589,7 @@ acpi_ut_display_init_pathname(u8 type,
593 * 589 *
594 ******************************************************************************/ 590 ******************************************************************************/
595 591
596u8 acpi_ut_valid_acpi_char(char character, acpi_native_uint position) 592u8 acpi_ut_valid_acpi_char(char character, u32 position)
597{ 593{
598 594
599 if (!((character >= 'A' && character <= 'Z') || 595 if (!((character >= 'A' && character <= 'Z') ||
@@ -628,7 +624,7 @@ u8 acpi_ut_valid_acpi_char(char character, acpi_native_uint position)
628 624
629u8 acpi_ut_valid_acpi_name(u32 name) 625u8 acpi_ut_valid_acpi_name(u32 name)
630{ 626{
631 acpi_native_uint i; 627 u32 i;
632 628
633 ACPI_FUNCTION_ENTRY(); 629 ACPI_FUNCTION_ENTRY();
634 630
@@ -657,7 +653,7 @@ u8 acpi_ut_valid_acpi_name(u32 name)
657 653
658acpi_name acpi_ut_repair_name(char *name) 654acpi_name acpi_ut_repair_name(char *name)
659{ 655{
660 acpi_native_uint i; 656 u32 i;
661 char new_name[ACPI_NAME_SIZE]; 657 char new_name[ACPI_NAME_SIZE];
662 658
663 for (i = 0; i < ACPI_NAME_SIZE; i++) { 659 for (i = 0; i < ACPI_NAME_SIZE; i++) {
@@ -1024,7 +1020,7 @@ acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
1024 ******************************************************************************/ 1020 ******************************************************************************/
1025 1021
1026void ACPI_INTERNAL_VAR_XFACE 1022void ACPI_INTERNAL_VAR_XFACE
1027acpi_ut_error(char *module_name, u32 line_number, char *format, ...) 1023acpi_ut_error(const char *module_name, u32 line_number, const char *format, ...)
1028{ 1024{
1029 va_list args; 1025 va_list args;
1030 1026
@@ -1037,8 +1033,8 @@ acpi_ut_error(char *module_name, u32 line_number, char *format, ...)
1037} 1033}
1038 1034
1039void ACPI_INTERNAL_VAR_XFACE 1035void ACPI_INTERNAL_VAR_XFACE
1040acpi_ut_exception(char *module_name, 1036acpi_ut_exception(const char *module_name,
1041 u32 line_number, acpi_status status, char *format, ...) 1037 u32 line_number, acpi_status status, const char *format, ...)
1042{ 1038{
1043 va_list args; 1039 va_list args;
1044 1040
@@ -1054,7 +1050,8 @@ acpi_ut_exception(char *module_name,
1054EXPORT_SYMBOL(acpi_ut_exception); 1050EXPORT_SYMBOL(acpi_ut_exception);
1055 1051
1056void ACPI_INTERNAL_VAR_XFACE 1052void ACPI_INTERNAL_VAR_XFACE
1057acpi_ut_warning(char *module_name, u32 line_number, char *format, ...) 1053acpi_ut_warning(const char *module_name,
1054 u32 line_number, const char *format, ...)
1058{ 1055{
1059 va_list args; 1056 va_list args;
1060 1057
@@ -1067,7 +1064,7 @@ acpi_ut_warning(char *module_name, u32 line_number, char *format, ...)
1067} 1064}
1068 1065
1069void ACPI_INTERNAL_VAR_XFACE 1066void ACPI_INTERNAL_VAR_XFACE
1070acpi_ut_info(char *module_name, u32 line_number, char *format, ...) 1067acpi_ut_info(const char *module_name, u32 line_number, const char *format, ...)
1071{ 1068{
1072 va_list args; 1069 va_list args;
1073 1070
diff --git a/drivers/acpi/utilities/utmutex.c b/drivers/acpi/utilities/utmutex.c
index f7d602b1a894..7331dde9e1b3 100644
--- a/drivers/acpi/utilities/utmutex.c
+++ b/drivers/acpi/utilities/utmutex.c
@@ -218,7 +218,7 @@ acpi_status acpi_ut_acquire_mutex(acpi_mutex_handle mutex_id)
218 * the mutex ordering rule. This indicates a coding error somewhere in 218 * the mutex ordering rule. This indicates a coding error somewhere in
219 * the ACPI subsystem code. 219 * the ACPI subsystem code.
220 */ 220 */
221 for (i = mutex_id; i < ACPI_MAX_MUTEX; i++) { 221 for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
222 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) { 222 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
223 if (i == mutex_id) { 223 if (i == mutex_id) {
224 ACPI_ERROR((AE_INFO, 224 ACPI_ERROR((AE_INFO,
@@ -315,7 +315,7 @@ acpi_status acpi_ut_release_mutex(acpi_mutex_handle mutex_id)
315 * ordering rule. This indicates a coding error somewhere in 315 * ordering rule. This indicates a coding error somewhere in
316 * the ACPI subsystem code. 316 * the ACPI subsystem code.
317 */ 317 */
318 for (i = mutex_id; i < ACPI_MAX_MUTEX; i++) { 318 for (i = mutex_id; i < ACPI_NUM_MUTEX; i++) {
319 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) { 319 if (acpi_gbl_mutex_info[i].thread_id == this_thread_id) {
320 if (i == mutex_id) { 320 if (i == mutex_id) {
321 continue; 321 continue;
diff --git a/drivers/acpi/utilities/utobject.c b/drivers/acpi/utilities/utobject.c
index e68466de8044..e25484495e65 100644
--- a/drivers/acpi/utilities/utobject.c
+++ b/drivers/acpi/utilities/utobject.c
@@ -83,7 +83,8 @@ acpi_ut_get_element_length(u8 object_type,
83 * 83 *
84 ******************************************************************************/ 84 ******************************************************************************/
85 85
86union acpi_operand_object *acpi_ut_create_internal_object_dbg(char *module_name, 86union acpi_operand_object *acpi_ut_create_internal_object_dbg(const char
87 *module_name,
87 u32 line_number, 88 u32 line_number,
88 u32 component_id, 89 u32 component_id,
89 acpi_object_type 90 acpi_object_type
@@ -175,8 +176,8 @@ union acpi_operand_object *acpi_ut_create_package_object(u32 count)
175 * Create the element array. Count+1 allows the array to be null 176 * Create the element array. Count+1 allows the array to be null
176 * terminated. 177 * terminated.
177 */ 178 */
178 package_elements = ACPI_ALLOCATE_ZEROED((acpi_size) 179 package_elements = ACPI_ALLOCATE_ZEROED(((acpi_size) count +
179 (count + 1) * sizeof(void *)); 180 1) * sizeof(void *));
180 if (!package_elements) { 181 if (!package_elements) {
181 acpi_ut_remove_reference(package_desc); 182 acpi_ut_remove_reference(package_desc);
182 return_PTR(NULL); 183 return_PTR(NULL);
@@ -347,7 +348,7 @@ u8 acpi_ut_valid_internal_object(void *object)
347 * 348 *
348 ******************************************************************************/ 349 ******************************************************************************/
349 350
350void *acpi_ut_allocate_object_desc_dbg(char *module_name, 351void *acpi_ut_allocate_object_desc_dbg(const char *module_name,
351 u32 line_number, u32 component_id) 352 u32 line_number, u32 component_id)
352{ 353{
353 union acpi_operand_object *object; 354 union acpi_operand_object *object;
diff --git a/drivers/acpi/video.c b/drivers/acpi/video.c
index d089c4519d45..64c889331f3b 100644
--- a/drivers/acpi/video.c
+++ b/drivers/acpi/video.c
@@ -631,6 +631,76 @@ acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
631 * device : video output device (LCD, CRT, ..) 631 * device : video output device (LCD, CRT, ..)
632 * 632 *
633 * Return Value: 633 * Return Value:
634 * Maximum brightness level
635 *
636 * Allocate and initialize device->brightness.
637 */
638
639static int
640acpi_video_init_brightness(struct acpi_video_device *device)
641{
642 union acpi_object *obj = NULL;
643 int i, max_level = 0, count = 0;
644 union acpi_object *o;
645 struct acpi_video_device_brightness *br = NULL;
646
647 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
648 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
649 "LCD brightness level\n"));
650 goto out;
651 }
652
653 if (obj->package.count < 2)
654 goto out;
655
656 br = kzalloc(sizeof(*br), GFP_KERNEL);
657 if (!br) {
658 printk(KERN_ERR "can't allocate memory\n");
659 goto out;
660 }
661
662 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
663 GFP_KERNEL);
664 if (!br->levels)
665 goto out_free;
666
667 for (i = 0; i < obj->package.count; i++) {
668 o = (union acpi_object *)&obj->package.elements[i];
669 if (o->type != ACPI_TYPE_INTEGER) {
670 printk(KERN_ERR PREFIX "Invalid data\n");
671 continue;
672 }
673 br->levels[count] = (u32) o->integer.value;
674
675 if (br->levels[count] > max_level)
676 max_level = br->levels[count];
677 count++;
678 }
679
680 if (count < 2)
681 goto out_free_levels;
682
683 br->count = count;
684 device->brightness = br;
685 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
686 kfree(obj);
687 return max_level;
688
689out_free_levels:
690 kfree(br->levels);
691out_free:
692 kfree(br);
693out:
694 device->brightness = NULL;
695 kfree(obj);
696 return 0;
697}
698
699/*
700 * Arg:
701 * device : video output device (LCD, CRT, ..)
702 *
703 * Return Value:
634 * None 704 * None
635 * 705 *
636 * Find out all required AML methods defined under the output 706 * Find out all required AML methods defined under the output
@@ -640,10 +710,7 @@ acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
640static void acpi_video_device_find_cap(struct acpi_video_device *device) 710static void acpi_video_device_find_cap(struct acpi_video_device *device)
641{ 711{
642 acpi_handle h_dummy1; 712 acpi_handle h_dummy1;
643 int i;
644 u32 max_level = 0; 713 u32 max_level = 0;
645 union acpi_object *obj = NULL;
646 struct acpi_video_device_brightness *br = NULL;
647 714
648 715
649 memset(&device->cap, 0, sizeof(device->cap)); 716 memset(&device->cap, 0, sizeof(device->cap));
@@ -672,53 +739,7 @@ static void acpi_video_device_find_cap(struct acpi_video_device *device)
672 device->cap._DSS = 1; 739 device->cap._DSS = 1;
673 } 740 }
674 741
675 if (ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) { 742 max_level = acpi_video_init_brightness(device);
676
677 if (obj->package.count >= 2) {
678 int count = 0;
679 union acpi_object *o;
680
681 br = kzalloc(sizeof(*br), GFP_KERNEL);
682 if (!br) {
683 printk(KERN_ERR "can't allocate memory\n");
684 } else {
685 br->levels = kmalloc(obj->package.count *
686 sizeof *(br->levels), GFP_KERNEL);
687 if (!br->levels)
688 goto out;
689
690 for (i = 0; i < obj->package.count; i++) {
691 o = (union acpi_object *)&obj->package.
692 elements[i];
693 if (o->type != ACPI_TYPE_INTEGER) {
694 printk(KERN_ERR PREFIX "Invalid data\n");
695 continue;
696 }
697 br->levels[count] = (u32) o->integer.value;
698
699 if (br->levels[count] > max_level)
700 max_level = br->levels[count];
701 count++;
702 }
703 out:
704 if (count < 2) {
705 kfree(br->levels);
706 kfree(br);
707 } else {
708 br->count = count;
709 device->brightness = br;
710 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
711 "found %d brightness levels\n",
712 count));
713 }
714 }
715 }
716
717 } else {
718 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available LCD brightness level\n"));
719 }
720
721 kfree(obj);
722 743
723 if (device->cap._BCL && device->cap._BCM && device->cap._BQC && max_level > 0){ 744 if (device->cap._BCL && device->cap._BCM && device->cap._BQC && max_level > 0){
724 int result; 745 int result;
@@ -1695,6 +1716,8 @@ static void
1695acpi_video_switch_brightness(struct acpi_video_device *device, int event) 1716acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1696{ 1717{
1697 unsigned long level_current, level_next; 1718 unsigned long level_current, level_next;
1719 if (!device->brightness)
1720 return;
1698 acpi_video_device_lcd_get_level_current(device, &level_current); 1721 acpi_video_device_lcd_get_level_current(device, &level_current);
1699 level_next = acpi_video_get_next_level(device, level_current, event); 1722 level_next = acpi_video_get_next_level(device, level_current, event);
1700 acpi_video_device_lcd_set_level(device, level_next); 1723 acpi_video_device_lcd_set_level(device, level_next);
diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 911ec600fe71..3f940393d6c7 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -453,6 +453,8 @@ int platform_driver_register(struct platform_driver *drv)
453 drv->driver.suspend = platform_drv_suspend; 453 drv->driver.suspend = platform_drv_suspend;
454 if (drv->resume) 454 if (drv->resume)
455 drv->driver.resume = platform_drv_resume; 455 drv->driver.resume = platform_drv_resume;
456 if (drv->pm)
457 drv->driver.pm = &drv->pm->base;
456 return driver_register(&drv->driver); 458 return driver_register(&drv->driver);
457} 459}
458EXPORT_SYMBOL_GPL(platform_driver_register); 460EXPORT_SYMBOL_GPL(platform_driver_register);
@@ -560,7 +562,9 @@ static int platform_match(struct device *dev, struct device_driver *drv)
560 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); 562 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
561} 563}
562 564
563static int platform_suspend(struct device *dev, pm_message_t mesg) 565#ifdef CONFIG_PM_SLEEP
566
567static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
564{ 568{
565 int ret = 0; 569 int ret = 0;
566 570
@@ -570,7 +574,7 @@ static int platform_suspend(struct device *dev, pm_message_t mesg)
570 return ret; 574 return ret;
571} 575}
572 576
573static int platform_suspend_late(struct device *dev, pm_message_t mesg) 577static int platform_legacy_suspend_late(struct device *dev, pm_message_t mesg)
574{ 578{
575 struct platform_driver *drv = to_platform_driver(dev->driver); 579 struct platform_driver *drv = to_platform_driver(dev->driver);
576 struct platform_device *pdev; 580 struct platform_device *pdev;
@@ -583,7 +587,7 @@ static int platform_suspend_late(struct device *dev, pm_message_t mesg)
583 return ret; 587 return ret;
584} 588}
585 589
586static int platform_resume_early(struct device *dev) 590static int platform_legacy_resume_early(struct device *dev)
587{ 591{
588 struct platform_driver *drv = to_platform_driver(dev->driver); 592 struct platform_driver *drv = to_platform_driver(dev->driver);
589 struct platform_device *pdev; 593 struct platform_device *pdev;
@@ -596,7 +600,7 @@ static int platform_resume_early(struct device *dev)
596 return ret; 600 return ret;
597} 601}
598 602
599static int platform_resume(struct device *dev) 603static int platform_legacy_resume(struct device *dev)
600{ 604{
601 int ret = 0; 605 int ret = 0;
602 606
@@ -606,15 +610,291 @@ static int platform_resume(struct device *dev)
606 return ret; 610 return ret;
607} 611}
608 612
613static int platform_pm_prepare(struct device *dev)
614{
615 struct device_driver *drv = dev->driver;
616 int ret = 0;
617
618 if (drv && drv->pm && drv->pm->prepare)
619 ret = drv->pm->prepare(dev);
620
621 return ret;
622}
623
624static void platform_pm_complete(struct device *dev)
625{
626 struct device_driver *drv = dev->driver;
627
628 if (drv && drv->pm && drv->pm->complete)
629 drv->pm->complete(dev);
630}
631
632#ifdef CONFIG_SUSPEND
633
634static int platform_pm_suspend(struct device *dev)
635{
636 struct device_driver *drv = dev->driver;
637 int ret = 0;
638
639 if (drv && drv->pm) {
640 if (drv->pm->suspend)
641 ret = drv->pm->suspend(dev);
642 } else {
643 ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
644 }
645
646 return ret;
647}
648
649static int platform_pm_suspend_noirq(struct device *dev)
650{
651 struct platform_driver *pdrv;
652 int ret = 0;
653
654 if (!dev->driver)
655 return 0;
656
657 pdrv = to_platform_driver(dev->driver);
658 if (pdrv->pm) {
659 if (pdrv->pm->suspend_noirq)
660 ret = pdrv->pm->suspend_noirq(dev);
661 } else {
662 ret = platform_legacy_suspend_late(dev, PMSG_SUSPEND);
663 }
664
665 return ret;
666}
667
668static int platform_pm_resume(struct device *dev)
669{
670 struct device_driver *drv = dev->driver;
671 int ret = 0;
672
673 if (drv && drv->pm) {
674 if (drv->pm->resume)
675 ret = drv->pm->resume(dev);
676 } else {
677 ret = platform_legacy_resume(dev);
678 }
679
680 return ret;
681}
682
683static int platform_pm_resume_noirq(struct device *dev)
684{
685 struct platform_driver *pdrv;
686 int ret = 0;
687
688 if (!dev->driver)
689 return 0;
690
691 pdrv = to_platform_driver(dev->driver);
692 if (pdrv->pm) {
693 if (pdrv->pm->resume_noirq)
694 ret = pdrv->pm->resume_noirq(dev);
695 } else {
696 ret = platform_legacy_resume_early(dev);
697 }
698
699 return ret;
700}
701
702#else /* !CONFIG_SUSPEND */
703
704#define platform_pm_suspend NULL
705#define platform_pm_resume NULL
706#define platform_pm_suspend_noirq NULL
707#define platform_pm_resume_noirq NULL
708
709#endif /* !CONFIG_SUSPEND */
710
711#ifdef CONFIG_HIBERNATION
712
713static int platform_pm_freeze(struct device *dev)
714{
715 struct device_driver *drv = dev->driver;
716 int ret = 0;
717
718 if (!drv)
719 return 0;
720
721 if (drv->pm) {
722 if (drv->pm->freeze)
723 ret = drv->pm->freeze(dev);
724 } else {
725 ret = platform_legacy_suspend(dev, PMSG_FREEZE);
726 }
727
728 return ret;
729}
730
731static int platform_pm_freeze_noirq(struct device *dev)
732{
733 struct platform_driver *pdrv;
734 int ret = 0;
735
736 if (!dev->driver)
737 return 0;
738
739 pdrv = to_platform_driver(dev->driver);
740 if (pdrv->pm) {
741 if (pdrv->pm->freeze_noirq)
742 ret = pdrv->pm->freeze_noirq(dev);
743 } else {
744 ret = platform_legacy_suspend_late(dev, PMSG_FREEZE);
745 }
746
747 return ret;
748}
749
750static int platform_pm_thaw(struct device *dev)
751{
752 struct device_driver *drv = dev->driver;
753 int ret = 0;
754
755 if (drv && drv->pm) {
756 if (drv->pm->thaw)
757 ret = drv->pm->thaw(dev);
758 } else {
759 ret = platform_legacy_resume(dev);
760 }
761
762 return ret;
763}
764
765static int platform_pm_thaw_noirq(struct device *dev)
766{
767 struct platform_driver *pdrv;
768 int ret = 0;
769
770 if (!dev->driver)
771 return 0;
772
773 pdrv = to_platform_driver(dev->driver);
774 if (pdrv->pm) {
775 if (pdrv->pm->thaw_noirq)
776 ret = pdrv->pm->thaw_noirq(dev);
777 } else {
778 ret = platform_legacy_resume_early(dev);
779 }
780
781 return ret;
782}
783
784static int platform_pm_poweroff(struct device *dev)
785{
786 struct device_driver *drv = dev->driver;
787 int ret = 0;
788
789 if (drv && drv->pm) {
790 if (drv->pm->poweroff)
791 ret = drv->pm->poweroff(dev);
792 } else {
793 ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
794 }
795
796 return ret;
797}
798
799static int platform_pm_poweroff_noirq(struct device *dev)
800{
801 struct platform_driver *pdrv;
802 int ret = 0;
803
804 if (!dev->driver)
805 return 0;
806
807 pdrv = to_platform_driver(dev->driver);
808 if (pdrv->pm) {
809 if (pdrv->pm->poweroff_noirq)
810 ret = pdrv->pm->poweroff_noirq(dev);
811 } else {
812 ret = platform_legacy_suspend_late(dev, PMSG_HIBERNATE);
813 }
814
815 return ret;
816}
817
818static int platform_pm_restore(struct device *dev)
819{
820 struct device_driver *drv = dev->driver;
821 int ret = 0;
822
823 if (drv && drv->pm) {
824 if (drv->pm->restore)
825 ret = drv->pm->restore(dev);
826 } else {
827 ret = platform_legacy_resume(dev);
828 }
829
830 return ret;
831}
832
833static int platform_pm_restore_noirq(struct device *dev)
834{
835 struct platform_driver *pdrv;
836 int ret = 0;
837
838 if (!dev->driver)
839 return 0;
840
841 pdrv = to_platform_driver(dev->driver);
842 if (pdrv->pm) {
843 if (pdrv->pm->restore_noirq)
844 ret = pdrv->pm->restore_noirq(dev);
845 } else {
846 ret = platform_legacy_resume_early(dev);
847 }
848
849 return ret;
850}
851
852#else /* !CONFIG_HIBERNATION */
853
854#define platform_pm_freeze NULL
855#define platform_pm_thaw NULL
856#define platform_pm_poweroff NULL
857#define platform_pm_restore NULL
858#define platform_pm_freeze_noirq NULL
859#define platform_pm_thaw_noirq NULL
860#define platform_pm_poweroff_noirq NULL
861#define platform_pm_restore_noirq NULL
862
863#endif /* !CONFIG_HIBERNATION */
864
865struct pm_ext_ops platform_pm_ops = {
866 .base = {
867 .prepare = platform_pm_prepare,
868 .complete = platform_pm_complete,
869 .suspend = platform_pm_suspend,
870 .resume = platform_pm_resume,
871 .freeze = platform_pm_freeze,
872 .thaw = platform_pm_thaw,
873 .poweroff = platform_pm_poweroff,
874 .restore = platform_pm_restore,
875 },
876 .suspend_noirq = platform_pm_suspend_noirq,
877 .resume_noirq = platform_pm_resume_noirq,
878 .freeze_noirq = platform_pm_freeze_noirq,
879 .thaw_noirq = platform_pm_thaw_noirq,
880 .poweroff_noirq = platform_pm_poweroff_noirq,
881 .restore_noirq = platform_pm_restore_noirq,
882};
883
884#define PLATFORM_PM_OPS_PTR &platform_pm_ops
885
886#else /* !CONFIG_PM_SLEEP */
887
888#define PLATFORM_PM_OPS_PTR NULL
889
890#endif /* !CONFIG_PM_SLEEP */
891
609struct bus_type platform_bus_type = { 892struct bus_type platform_bus_type = {
610 .name = "platform", 893 .name = "platform",
611 .dev_attrs = platform_dev_attrs, 894 .dev_attrs = platform_dev_attrs,
612 .match = platform_match, 895 .match = platform_match,
613 .uevent = platform_uevent, 896 .uevent = platform_uevent,
614 .suspend = platform_suspend, 897 .pm = PLATFORM_PM_OPS_PTR,
615 .suspend_late = platform_suspend_late,
616 .resume_early = platform_resume_early,
617 .resume = platform_resume,
618}; 898};
619EXPORT_SYMBOL_GPL(platform_bus_type); 899EXPORT_SYMBOL_GPL(platform_bus_type);
620 900
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index 45cc3d9eacb8..3250c5257b74 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -12,11 +12,9 @@
12 * and add it to the list of power-controlled devices. sysfs entries for 12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added. 13 * controlling device power management will also be added.
14 * 14 *
15 * A different set of lists than the global subsystem list are used to 15 * A separate list is used for keeping track of power info, because the power
16 * keep track of power info because we use different lists to hold 16 * domain dependencies may differ from the ancestral dependencies that the
17 * devices based on what stage of the power management process they 17 * subsystem list maintains.
18 * are in. The power domain dependencies may also differ from the
19 * ancestral dependencies that the subsystem list maintains.
20 */ 18 */
21 19
22#include <linux/device.h> 20#include <linux/device.h>
@@ -30,31 +28,40 @@
30#include "power.h" 28#include "power.h"
31 29
32/* 30/*
33 * The entries in the dpm_active list are in a depth first order, simply 31 * The entries in the dpm_list list are in a depth first order, simply
34 * because children are guaranteed to be discovered after parents, and 32 * because children are guaranteed to be discovered after parents, and
35 * are inserted at the back of the list on discovery. 33 * are inserted at the back of the list on discovery.
36 * 34 *
37 * All the other lists are kept in the same order, for consistency.
38 * However the lists aren't always traversed in the same order.
39 * Semaphores must be acquired from the top (i.e., front) down
40 * and released in the opposite order. Devices must be suspended
41 * from the bottom (i.e., end) up and resumed in the opposite order.
42 * That way no parent will be suspended while it still has an active
43 * child.
44 *
45 * Since device_pm_add() may be called with a device semaphore held, 35 * Since device_pm_add() may be called with a device semaphore held,
46 * we must never try to acquire a device semaphore while holding 36 * we must never try to acquire a device semaphore while holding
47 * dpm_list_mutex. 37 * dpm_list_mutex.
48 */ 38 */
49 39
50LIST_HEAD(dpm_active); 40LIST_HEAD(dpm_list);
51static LIST_HEAD(dpm_off);
52static LIST_HEAD(dpm_off_irq);
53 41
54static DEFINE_MUTEX(dpm_list_mtx); 42static DEFINE_MUTEX(dpm_list_mtx);
55 43
56/* 'true' if all devices have been suspended, protected by dpm_list_mtx */ 44/*
57static bool all_sleeping; 45 * Set once the preparation of devices for a PM transition has started, reset
46 * before starting to resume devices. Protected by dpm_list_mtx.
47 */
48static bool transition_started;
49
50/**
51 * device_pm_lock - lock the list of active devices used by the PM core
52 */
53void device_pm_lock(void)
54{
55 mutex_lock(&dpm_list_mtx);
56}
57
58/**
59 * device_pm_unlock - unlock the list of active devices used by the PM core
60 */
61void device_pm_unlock(void)
62{
63 mutex_unlock(&dpm_list_mtx);
64}
58 65
59/** 66/**
60 * device_pm_add - add a device to the list of active devices 67 * device_pm_add - add a device to the list of active devices
@@ -68,17 +75,25 @@ int device_pm_add(struct device *dev)
68 dev->bus ? dev->bus->name : "No Bus", 75 dev->bus ? dev->bus->name : "No Bus",
69 kobject_name(&dev->kobj)); 76 kobject_name(&dev->kobj));
70 mutex_lock(&dpm_list_mtx); 77 mutex_lock(&dpm_list_mtx);
71 if ((dev->parent && dev->parent->power.sleeping) || all_sleeping) { 78 if (dev->parent) {
72 if (dev->parent->power.sleeping) 79 if (dev->parent->power.status >= DPM_SUSPENDING) {
73 dev_warn(dev, "parent %s is sleeping\n", 80 dev_warn(dev, "parent %s is sleeping, will not add\n",
74 dev->parent->bus_id); 81 dev->parent->bus_id);
75 else 82 WARN_ON(true);
76 dev_warn(dev, "all devices are sleeping\n"); 83 }
84 } else if (transition_started) {
85 /*
86 * We refuse to register parentless devices while a PM
87 * transition is in progress in order to avoid leaving them
88 * unhandled down the road
89 */
77 WARN_ON(true); 90 WARN_ON(true);
78 } 91 }
79 error = dpm_sysfs_add(dev); 92 error = dpm_sysfs_add(dev);
80 if (!error) 93 if (!error) {
81 list_add_tail(&dev->power.entry, &dpm_active); 94 dev->power.status = DPM_ON;
95 list_add_tail(&dev->power.entry, &dpm_list);
96 }
82 mutex_unlock(&dpm_list_mtx); 97 mutex_unlock(&dpm_list_mtx);
83 return error; 98 return error;
84} 99}
@@ -100,73 +115,243 @@ void device_pm_remove(struct device *dev)
100 mutex_unlock(&dpm_list_mtx); 115 mutex_unlock(&dpm_list_mtx);
101} 116}
102 117
118/**
119 * pm_op - execute the PM operation appropiate for given PM event
120 * @dev: Device.
121 * @ops: PM operations to choose from.
122 * @state: PM transition of the system being carried out.
123 */
124static int pm_op(struct device *dev, struct pm_ops *ops, pm_message_t state)
125{
126 int error = 0;
127
128 switch (state.event) {
129#ifdef CONFIG_SUSPEND
130 case PM_EVENT_SUSPEND:
131 if (ops->suspend) {
132 error = ops->suspend(dev);
133 suspend_report_result(ops->suspend, error);
134 }
135 break;
136 case PM_EVENT_RESUME:
137 if (ops->resume) {
138 error = ops->resume(dev);
139 suspend_report_result(ops->resume, error);
140 }
141 break;
142#endif /* CONFIG_SUSPEND */
143#ifdef CONFIG_HIBERNATION
144 case PM_EVENT_FREEZE:
145 case PM_EVENT_QUIESCE:
146 if (ops->freeze) {
147 error = ops->freeze(dev);
148 suspend_report_result(ops->freeze, error);
149 }
150 break;
151 case PM_EVENT_HIBERNATE:
152 if (ops->poweroff) {
153 error = ops->poweroff(dev);
154 suspend_report_result(ops->poweroff, error);
155 }
156 break;
157 case PM_EVENT_THAW:
158 case PM_EVENT_RECOVER:
159 if (ops->thaw) {
160 error = ops->thaw(dev);
161 suspend_report_result(ops->thaw, error);
162 }
163 break;
164 case PM_EVENT_RESTORE:
165 if (ops->restore) {
166 error = ops->restore(dev);
167 suspend_report_result(ops->restore, error);
168 }
169 break;
170#endif /* CONFIG_HIBERNATION */
171 default:
172 error = -EINVAL;
173 }
174 return error;
175}
176
177/**
178 * pm_noirq_op - execute the PM operation appropiate for given PM event
179 * @dev: Device.
180 * @ops: PM operations to choose from.
181 * @state: PM transition of the system being carried out.
182 *
183 * The operation is executed with interrupts disabled by the only remaining
184 * functional CPU in the system.
185 */
186static int pm_noirq_op(struct device *dev, struct pm_ext_ops *ops,
187 pm_message_t state)
188{
189 int error = 0;
190
191 switch (state.event) {
192#ifdef CONFIG_SUSPEND
193 case PM_EVENT_SUSPEND:
194 if (ops->suspend_noirq) {
195 error = ops->suspend_noirq(dev);
196 suspend_report_result(ops->suspend_noirq, error);
197 }
198 break;
199 case PM_EVENT_RESUME:
200 if (ops->resume_noirq) {
201 error = ops->resume_noirq(dev);
202 suspend_report_result(ops->resume_noirq, error);
203 }
204 break;
205#endif /* CONFIG_SUSPEND */
206#ifdef CONFIG_HIBERNATION
207 case PM_EVENT_FREEZE:
208 case PM_EVENT_QUIESCE:
209 if (ops->freeze_noirq) {
210 error = ops->freeze_noirq(dev);
211 suspend_report_result(ops->freeze_noirq, error);
212 }
213 break;
214 case PM_EVENT_HIBERNATE:
215 if (ops->poweroff_noirq) {
216 error = ops->poweroff_noirq(dev);
217 suspend_report_result(ops->poweroff_noirq, error);
218 }
219 break;
220 case PM_EVENT_THAW:
221 case PM_EVENT_RECOVER:
222 if (ops->thaw_noirq) {
223 error = ops->thaw_noirq(dev);
224 suspend_report_result(ops->thaw_noirq, error);
225 }
226 break;
227 case PM_EVENT_RESTORE:
228 if (ops->restore_noirq) {
229 error = ops->restore_noirq(dev);
230 suspend_report_result(ops->restore_noirq, error);
231 }
232 break;
233#endif /* CONFIG_HIBERNATION */
234 default:
235 error = -EINVAL;
236 }
237 return error;
238}
239
240static char *pm_verb(int event)
241{
242 switch (event) {
243 case PM_EVENT_SUSPEND:
244 return "suspend";
245 case PM_EVENT_RESUME:
246 return "resume";
247 case PM_EVENT_FREEZE:
248 return "freeze";
249 case PM_EVENT_QUIESCE:
250 return "quiesce";
251 case PM_EVENT_HIBERNATE:
252 return "hibernate";
253 case PM_EVENT_THAW:
254 return "thaw";
255 case PM_EVENT_RESTORE:
256 return "restore";
257 case PM_EVENT_RECOVER:
258 return "recover";
259 default:
260 return "(unknown PM event)";
261 }
262}
263
264static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
265{
266 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
267 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
268 ", may wakeup" : "");
269}
270
271static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
272 int error)
273{
274 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
275 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
276}
277
103/*------------------------- Resume routines -------------------------*/ 278/*------------------------- Resume routines -------------------------*/
104 279
105/** 280/**
106 * resume_device_early - Power on one device (early resume). 281 * resume_device_noirq - Power on one device (early resume).
107 * @dev: Device. 282 * @dev: Device.
283 * @state: PM transition of the system being carried out.
108 * 284 *
109 * Must be called with interrupts disabled. 285 * Must be called with interrupts disabled.
110 */ 286 */
111static int resume_device_early(struct device *dev) 287static int resume_device_noirq(struct device *dev, pm_message_t state)
112{ 288{
113 int error = 0; 289 int error = 0;
114 290
115 TRACE_DEVICE(dev); 291 TRACE_DEVICE(dev);
116 TRACE_RESUME(0); 292 TRACE_RESUME(0);
117 293
118 if (dev->bus && dev->bus->resume_early) { 294 if (!dev->bus)
119 dev_dbg(dev, "EARLY resume\n"); 295 goto End;
296
297 if (dev->bus->pm) {
298 pm_dev_dbg(dev, state, "EARLY ");
299 error = pm_noirq_op(dev, dev->bus->pm, state);
300 } else if (dev->bus->resume_early) {
301 pm_dev_dbg(dev, state, "legacy EARLY ");
120 error = dev->bus->resume_early(dev); 302 error = dev->bus->resume_early(dev);
121 } 303 }
122 304 End:
123 TRACE_RESUME(error); 305 TRACE_RESUME(error);
124 return error; 306 return error;
125} 307}
126 308
127/** 309/**
128 * dpm_power_up - Power on all regular (non-sysdev) devices. 310 * dpm_power_up - Power on all regular (non-sysdev) devices.
311 * @state: PM transition of the system being carried out.
129 * 312 *
130 * Walk the dpm_off_irq list and power each device up. This 313 * Execute the appropriate "noirq resume" callback for all devices marked
131 * is used for devices that required they be powered down with 314 * as DPM_OFF_IRQ.
132 * interrupts disabled. As devices are powered on, they are moved
133 * to the dpm_off list.
134 * 315 *
135 * Must be called with interrupts disabled and only one CPU running. 316 * Must be called with interrupts disabled and only one CPU running.
136 */ 317 */
137static void dpm_power_up(void) 318static void dpm_power_up(pm_message_t state)
138{ 319{
320 struct device *dev;
139 321
140 while (!list_empty(&dpm_off_irq)) { 322 list_for_each_entry(dev, &dpm_list, power.entry)
141 struct list_head *entry = dpm_off_irq.next; 323 if (dev->power.status > DPM_OFF) {
142 struct device *dev = to_device(entry); 324 int error;
143 325
144 list_move_tail(entry, &dpm_off); 326 dev->power.status = DPM_OFF;
145 resume_device_early(dev); 327 error = resume_device_noirq(dev, state);
146 } 328 if (error)
329 pm_dev_err(dev, state, " early", error);
330 }
147} 331}
148 332
149/** 333/**
150 * device_power_up - Turn on all devices that need special attention. 334 * device_power_up - Turn on all devices that need special attention.
335 * @state: PM transition of the system being carried out.
151 * 336 *
152 * Power on system devices, then devices that required we shut them down 337 * Power on system devices, then devices that required we shut them down
153 * with interrupts disabled. 338 * with interrupts disabled.
154 * 339 *
155 * Must be called with interrupts disabled. 340 * Must be called with interrupts disabled.
156 */ 341 */
157void device_power_up(void) 342void device_power_up(pm_message_t state)
158{ 343{
159 sysdev_resume(); 344 sysdev_resume();
160 dpm_power_up(); 345 dpm_power_up(state);
161} 346}
162EXPORT_SYMBOL_GPL(device_power_up); 347EXPORT_SYMBOL_GPL(device_power_up);
163 348
164/** 349/**
165 * resume_device - Restore state for one device. 350 * resume_device - Restore state for one device.
166 * @dev: Device. 351 * @dev: Device.
167 * 352 * @state: PM transition of the system being carried out.
168 */ 353 */
169static int resume_device(struct device *dev) 354static int resume_device(struct device *dev, pm_message_t state)
170{ 355{
171 int error = 0; 356 int error = 0;
172 357
@@ -175,21 +360,40 @@ static int resume_device(struct device *dev)
175 360
176 down(&dev->sem); 361 down(&dev->sem);
177 362
178 if (dev->bus && dev->bus->resume) { 363 if (dev->bus) {
179 dev_dbg(dev,"resuming\n"); 364 if (dev->bus->pm) {
180 error = dev->bus->resume(dev); 365 pm_dev_dbg(dev, state, "");
366 error = pm_op(dev, &dev->bus->pm->base, state);
367 } else if (dev->bus->resume) {
368 pm_dev_dbg(dev, state, "legacy ");
369 error = dev->bus->resume(dev);
370 }
371 if (error)
372 goto End;
181 } 373 }
182 374
183 if (!error && dev->type && dev->type->resume) { 375 if (dev->type) {
184 dev_dbg(dev,"resuming\n"); 376 if (dev->type->pm) {
185 error = dev->type->resume(dev); 377 pm_dev_dbg(dev, state, "type ");
378 error = pm_op(dev, dev->type->pm, state);
379 } else if (dev->type->resume) {
380 pm_dev_dbg(dev, state, "legacy type ");
381 error = dev->type->resume(dev);
382 }
383 if (error)
384 goto End;
186 } 385 }
187 386
188 if (!error && dev->class && dev->class->resume) { 387 if (dev->class) {
189 dev_dbg(dev,"class resume\n"); 388 if (dev->class->pm) {
190 error = dev->class->resume(dev); 389 pm_dev_dbg(dev, state, "class ");
390 error = pm_op(dev, dev->class->pm, state);
391 } else if (dev->class->resume) {
392 pm_dev_dbg(dev, state, "legacy class ");
393 error = dev->class->resume(dev);
394 }
191 } 395 }
192 396 End:
193 up(&dev->sem); 397 up(&dev->sem);
194 398
195 TRACE_RESUME(error); 399 TRACE_RESUME(error);
@@ -198,78 +402,161 @@ static int resume_device(struct device *dev)
198 402
199/** 403/**
200 * dpm_resume - Resume every device. 404 * dpm_resume - Resume every device.
405 * @state: PM transition of the system being carried out.
201 * 406 *
202 * Resume the devices that have either not gone through 407 * Execute the appropriate "resume" callback for all devices the status of
203 * the late suspend, or that did go through it but also 408 * which indicates that they are inactive.
204 * went through the early resume. 409 */
410static void dpm_resume(pm_message_t state)
411{
412 struct list_head list;
413
414 INIT_LIST_HEAD(&list);
415 mutex_lock(&dpm_list_mtx);
416 transition_started = false;
417 while (!list_empty(&dpm_list)) {
418 struct device *dev = to_device(dpm_list.next);
419
420 get_device(dev);
421 if (dev->power.status >= DPM_OFF) {
422 int error;
423
424 dev->power.status = DPM_RESUMING;
425 mutex_unlock(&dpm_list_mtx);
426
427 error = resume_device(dev, state);
428
429 mutex_lock(&dpm_list_mtx);
430 if (error)
431 pm_dev_err(dev, state, "", error);
432 } else if (dev->power.status == DPM_SUSPENDING) {
433 /* Allow new children of the device to be registered */
434 dev->power.status = DPM_RESUMING;
435 }
436 if (!list_empty(&dev->power.entry))
437 list_move_tail(&dev->power.entry, &list);
438 put_device(dev);
439 }
440 list_splice(&list, &dpm_list);
441 mutex_unlock(&dpm_list_mtx);
442}
443
444/**
445 * complete_device - Complete a PM transition for given device
446 * @dev: Device.
447 * @state: PM transition of the system being carried out.
448 */
449static void complete_device(struct device *dev, pm_message_t state)
450{
451 down(&dev->sem);
452
453 if (dev->class && dev->class->pm && dev->class->pm->complete) {
454 pm_dev_dbg(dev, state, "completing class ");
455 dev->class->pm->complete(dev);
456 }
457
458 if (dev->type && dev->type->pm && dev->type->pm->complete) {
459 pm_dev_dbg(dev, state, "completing type ");
460 dev->type->pm->complete(dev);
461 }
462
463 if (dev->bus && dev->bus->pm && dev->bus->pm->base.complete) {
464 pm_dev_dbg(dev, state, "completing ");
465 dev->bus->pm->base.complete(dev);
466 }
467
468 up(&dev->sem);
469}
470
471/**
472 * dpm_complete - Complete a PM transition for all devices.
473 * @state: PM transition of the system being carried out.
205 * 474 *
206 * Take devices from the dpm_off_list, resume them, 475 * Execute the ->complete() callbacks for all devices that are not marked
207 * and put them on the dpm_locked list. 476 * as DPM_ON.
208 */ 477 */
209static void dpm_resume(void) 478static void dpm_complete(pm_message_t state)
210{ 479{
480 struct list_head list;
481
482 INIT_LIST_HEAD(&list);
211 mutex_lock(&dpm_list_mtx); 483 mutex_lock(&dpm_list_mtx);
212 all_sleeping = false; 484 while (!list_empty(&dpm_list)) {
213 while(!list_empty(&dpm_off)) { 485 struct device *dev = to_device(dpm_list.prev);
214 struct list_head *entry = dpm_off.next;
215 struct device *dev = to_device(entry);
216 486
217 list_move_tail(entry, &dpm_active); 487 get_device(dev);
218 dev->power.sleeping = false; 488 if (dev->power.status > DPM_ON) {
219 mutex_unlock(&dpm_list_mtx); 489 dev->power.status = DPM_ON;
220 resume_device(dev); 490 mutex_unlock(&dpm_list_mtx);
221 mutex_lock(&dpm_list_mtx); 491
492 complete_device(dev, state);
493
494 mutex_lock(&dpm_list_mtx);
495 }
496 if (!list_empty(&dev->power.entry))
497 list_move(&dev->power.entry, &list);
498 put_device(dev);
222 } 499 }
500 list_splice(&list, &dpm_list);
223 mutex_unlock(&dpm_list_mtx); 501 mutex_unlock(&dpm_list_mtx);
224} 502}
225 503
226/** 504/**
227 * device_resume - Restore state of each device in system. 505 * device_resume - Restore state of each device in system.
506 * @state: PM transition of the system being carried out.
228 * 507 *
229 * Resume all the devices, unlock them all, and allow new 508 * Resume all the devices, unlock them all, and allow new
230 * devices to be registered once again. 509 * devices to be registered once again.
231 */ 510 */
232void device_resume(void) 511void device_resume(pm_message_t state)
233{ 512{
234 might_sleep(); 513 might_sleep();
235 dpm_resume(); 514 dpm_resume(state);
515 dpm_complete(state);
236} 516}
237EXPORT_SYMBOL_GPL(device_resume); 517EXPORT_SYMBOL_GPL(device_resume);
238 518
239 519
240/*------------------------- Suspend routines -------------------------*/ 520/*------------------------- Suspend routines -------------------------*/
241 521
242static inline char *suspend_verb(u32 event) 522/**
523 * resume_event - return a PM message representing the resume event
524 * corresponding to given sleep state.
525 * @sleep_state: PM message representing a sleep state.
526 */
527static pm_message_t resume_event(pm_message_t sleep_state)
243{ 528{
244 switch (event) { 529 switch (sleep_state.event) {
245 case PM_EVENT_SUSPEND: return "suspend"; 530 case PM_EVENT_SUSPEND:
246 case PM_EVENT_FREEZE: return "freeze"; 531 return PMSG_RESUME;
247 case PM_EVENT_PRETHAW: return "prethaw"; 532 case PM_EVENT_FREEZE:
248 default: return "(unknown suspend event)"; 533 case PM_EVENT_QUIESCE:
534 return PMSG_RECOVER;
535 case PM_EVENT_HIBERNATE:
536 return PMSG_RESTORE;
249 } 537 }
250} 538 return PMSG_ON;
251
252static void
253suspend_device_dbg(struct device *dev, pm_message_t state, char *info)
254{
255 dev_dbg(dev, "%s%s%s\n", info, suspend_verb(state.event),
256 ((state.event == PM_EVENT_SUSPEND) && device_may_wakeup(dev)) ?
257 ", may wakeup" : "");
258} 539}
259 540
260/** 541/**
261 * suspend_device_late - Shut down one device (late suspend). 542 * suspend_device_noirq - Shut down one device (late suspend).
262 * @dev: Device. 543 * @dev: Device.
263 * @state: Power state device is entering. 544 * @state: PM transition of the system being carried out.
264 * 545 *
265 * This is called with interrupts off and only a single CPU running. 546 * This is called with interrupts off and only a single CPU running.
266 */ 547 */
267static int suspend_device_late(struct device *dev, pm_message_t state) 548static int suspend_device_noirq(struct device *dev, pm_message_t state)
268{ 549{
269 int error = 0; 550 int error = 0;
270 551
271 if (dev->bus && dev->bus->suspend_late) { 552 if (!dev->bus)
272 suspend_device_dbg(dev, state, "LATE "); 553 return 0;
554
555 if (dev->bus->pm) {
556 pm_dev_dbg(dev, state, "LATE ");
557 error = pm_noirq_op(dev, dev->bus->pm, state);
558 } else if (dev->bus->suspend_late) {
559 pm_dev_dbg(dev, state, "legacy LATE ");
273 error = dev->bus->suspend_late(dev, state); 560 error = dev->bus->suspend_late(dev, state);
274 suspend_report_result(dev->bus->suspend_late, error); 561 suspend_report_result(dev->bus->suspend_late, error);
275 } 562 }
@@ -278,37 +565,30 @@ static int suspend_device_late(struct device *dev, pm_message_t state)
278 565
279/** 566/**
280 * device_power_down - Shut down special devices. 567 * device_power_down - Shut down special devices.
281 * @state: Power state to enter. 568 * @state: PM transition of the system being carried out.
282 * 569 *
283 * Power down devices that require interrupts to be disabled 570 * Power down devices that require interrupts to be disabled.
284 * and move them from the dpm_off list to the dpm_off_irq list.
285 * Then power down system devices. 571 * Then power down system devices.
286 * 572 *
287 * Must be called with interrupts disabled and only one CPU running. 573 * Must be called with interrupts disabled and only one CPU running.
288 */ 574 */
289int device_power_down(pm_message_t state) 575int device_power_down(pm_message_t state)
290{ 576{
577 struct device *dev;
291 int error = 0; 578 int error = 0;
292 579
293 while (!list_empty(&dpm_off)) { 580 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
294 struct list_head *entry = dpm_off.prev; 581 error = suspend_device_noirq(dev, state);
295 struct device *dev = to_device(entry);
296
297 error = suspend_device_late(dev, state);
298 if (error) { 582 if (error) {
299 printk(KERN_ERR "Could not power down device %s: " 583 pm_dev_err(dev, state, " late", error);
300 "error %d\n",
301 kobject_name(&dev->kobj), error);
302 break; 584 break;
303 } 585 }
304 if (!list_empty(&dev->power.entry)) 586 dev->power.status = DPM_OFF_IRQ;
305 list_move(&dev->power.entry, &dpm_off_irq);
306 } 587 }
307
308 if (!error) 588 if (!error)
309 error = sysdev_suspend(state); 589 error = sysdev_suspend(state);
310 if (error) 590 if (error)
311 dpm_power_up(); 591 dpm_power_up(resume_event(state));
312 return error; 592 return error;
313} 593}
314EXPORT_SYMBOL_GPL(device_power_down); 594EXPORT_SYMBOL_GPL(device_power_down);
@@ -316,7 +596,7 @@ EXPORT_SYMBOL_GPL(device_power_down);
316/** 596/**
317 * suspend_device - Save state of one device. 597 * suspend_device - Save state of one device.
318 * @dev: Device. 598 * @dev: Device.
319 * @state: Power state device is entering. 599 * @state: PM transition of the system being carried out.
320 */ 600 */
321static int suspend_device(struct device *dev, pm_message_t state) 601static int suspend_device(struct device *dev, pm_message_t state)
322{ 602{
@@ -324,24 +604,43 @@ static int suspend_device(struct device *dev, pm_message_t state)
324 604
325 down(&dev->sem); 605 down(&dev->sem);
326 606
327 if (dev->class && dev->class->suspend) { 607 if (dev->class) {
328 suspend_device_dbg(dev, state, "class "); 608 if (dev->class->pm) {
329 error = dev->class->suspend(dev, state); 609 pm_dev_dbg(dev, state, "class ");
330 suspend_report_result(dev->class->suspend, error); 610 error = pm_op(dev, dev->class->pm, state);
611 } else if (dev->class->suspend) {
612 pm_dev_dbg(dev, state, "legacy class ");
613 error = dev->class->suspend(dev, state);
614 suspend_report_result(dev->class->suspend, error);
615 }
616 if (error)
617 goto End;
331 } 618 }
332 619
333 if (!error && dev->type && dev->type->suspend) { 620 if (dev->type) {
334 suspend_device_dbg(dev, state, "type "); 621 if (dev->type->pm) {
335 error = dev->type->suspend(dev, state); 622 pm_dev_dbg(dev, state, "type ");
336 suspend_report_result(dev->type->suspend, error); 623 error = pm_op(dev, dev->type->pm, state);
624 } else if (dev->type->suspend) {
625 pm_dev_dbg(dev, state, "legacy type ");
626 error = dev->type->suspend(dev, state);
627 suspend_report_result(dev->type->suspend, error);
628 }
629 if (error)
630 goto End;
337 } 631 }
338 632
339 if (!error && dev->bus && dev->bus->suspend) { 633 if (dev->bus) {
340 suspend_device_dbg(dev, state, ""); 634 if (dev->bus->pm) {
341 error = dev->bus->suspend(dev, state); 635 pm_dev_dbg(dev, state, "");
342 suspend_report_result(dev->bus->suspend, error); 636 error = pm_op(dev, &dev->bus->pm->base, state);
637 } else if (dev->bus->suspend) {
638 pm_dev_dbg(dev, state, "legacy ");
639 error = dev->bus->suspend(dev, state);
640 suspend_report_result(dev->bus->suspend, error);
641 }
343 } 642 }
344 643 End:
345 up(&dev->sem); 644 up(&dev->sem);
346 645
347 return error; 646 return error;
@@ -349,67 +648,139 @@ static int suspend_device(struct device *dev, pm_message_t state)
349 648
350/** 649/**
351 * dpm_suspend - Suspend every device. 650 * dpm_suspend - Suspend every device.
352 * @state: Power state to put each device in. 651 * @state: PM transition of the system being carried out.
353 *
354 * Walk the dpm_locked list. Suspend each device and move it
355 * to the dpm_off list.
356 * 652 *
357 * (For historical reasons, if it returns -EAGAIN, that used to mean 653 * Execute the appropriate "suspend" callbacks for all devices.
358 * that the device would be called again with interrupts disabled.
359 * These days, we use the "suspend_late()" callback for that, so we
360 * print a warning and consider it an error).
361 */ 654 */
362static int dpm_suspend(pm_message_t state) 655static int dpm_suspend(pm_message_t state)
363{ 656{
657 struct list_head list;
364 int error = 0; 658 int error = 0;
365 659
660 INIT_LIST_HEAD(&list);
366 mutex_lock(&dpm_list_mtx); 661 mutex_lock(&dpm_list_mtx);
367 while (!list_empty(&dpm_active)) { 662 while (!list_empty(&dpm_list)) {
368 struct list_head *entry = dpm_active.prev; 663 struct device *dev = to_device(dpm_list.prev);
369 struct device *dev = to_device(entry);
370 664
371 WARN_ON(dev->parent && dev->parent->power.sleeping); 665 get_device(dev);
372
373 dev->power.sleeping = true;
374 mutex_unlock(&dpm_list_mtx); 666 mutex_unlock(&dpm_list_mtx);
667
375 error = suspend_device(dev, state); 668 error = suspend_device(dev, state);
669
376 mutex_lock(&dpm_list_mtx); 670 mutex_lock(&dpm_list_mtx);
377 if (error) { 671 if (error) {
378 printk(KERN_ERR "Could not suspend device %s: " 672 pm_dev_err(dev, state, "", error);
379 "error %d%s\n", 673 put_device(dev);
380 kobject_name(&dev->kobj),
381 error,
382 (error == -EAGAIN ?
383 " (please convert to suspend_late)" :
384 ""));
385 dev->power.sleeping = false;
386 break; 674 break;
387 } 675 }
676 dev->power.status = DPM_OFF;
388 if (!list_empty(&dev->power.entry)) 677 if (!list_empty(&dev->power.entry))
389 list_move(&dev->power.entry, &dpm_off); 678 list_move(&dev->power.entry, &list);
679 put_device(dev);
390 } 680 }
391 if (!error) 681 list_splice(&list, dpm_list.prev);
392 all_sleeping = true;
393 mutex_unlock(&dpm_list_mtx); 682 mutex_unlock(&dpm_list_mtx);
683 return error;
684}
685
686/**
687 * prepare_device - Execute the ->prepare() callback(s) for given device.
688 * @dev: Device.
689 * @state: PM transition of the system being carried out.
690 */
691static int prepare_device(struct device *dev, pm_message_t state)
692{
693 int error = 0;
694
695 down(&dev->sem);
696
697 if (dev->bus && dev->bus->pm && dev->bus->pm->base.prepare) {
698 pm_dev_dbg(dev, state, "preparing ");
699 error = dev->bus->pm->base.prepare(dev);
700 suspend_report_result(dev->bus->pm->base.prepare, error);
701 if (error)
702 goto End;
703 }
704
705 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
706 pm_dev_dbg(dev, state, "preparing type ");
707 error = dev->type->pm->prepare(dev);
708 suspend_report_result(dev->type->pm->prepare, error);
709 if (error)
710 goto End;
711 }
712
713 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
714 pm_dev_dbg(dev, state, "preparing class ");
715 error = dev->class->pm->prepare(dev);
716 suspend_report_result(dev->class->pm->prepare, error);
717 }
718 End:
719 up(&dev->sem);
720
721 return error;
722}
723
724/**
725 * dpm_prepare - Prepare all devices for a PM transition.
726 * @state: PM transition of the system being carried out.
727 *
728 * Execute the ->prepare() callback for all devices.
729 */
730static int dpm_prepare(pm_message_t state)
731{
732 struct list_head list;
733 int error = 0;
734
735 INIT_LIST_HEAD(&list);
736 mutex_lock(&dpm_list_mtx);
737 transition_started = true;
738 while (!list_empty(&dpm_list)) {
739 struct device *dev = to_device(dpm_list.next);
740
741 get_device(dev);
742 dev->power.status = DPM_PREPARING;
743 mutex_unlock(&dpm_list_mtx);
394 744
745 error = prepare_device(dev, state);
746
747 mutex_lock(&dpm_list_mtx);
748 if (error) {
749 dev->power.status = DPM_ON;
750 if (error == -EAGAIN) {
751 put_device(dev);
752 continue;
753 }
754 printk(KERN_ERR "PM: Failed to prepare device %s "
755 "for power transition: error %d\n",
756 kobject_name(&dev->kobj), error);
757 put_device(dev);
758 break;
759 }
760 dev->power.status = DPM_SUSPENDING;
761 if (!list_empty(&dev->power.entry))
762 list_move_tail(&dev->power.entry, &list);
763 put_device(dev);
764 }
765 list_splice(&list, &dpm_list);
766 mutex_unlock(&dpm_list_mtx);
395 return error; 767 return error;
396} 768}
397 769
398/** 770/**
399 * device_suspend - Save state and stop all devices in system. 771 * device_suspend - Save state and stop all devices in system.
400 * @state: new power management state 772 * @state: PM transition of the system being carried out.
401 * 773 *
402 * Prevent new devices from being registered, then lock all devices 774 * Prepare and suspend all devices.
403 * and suspend them.
404 */ 775 */
405int device_suspend(pm_message_t state) 776int device_suspend(pm_message_t state)
406{ 777{
407 int error; 778 int error;
408 779
409 might_sleep(); 780 might_sleep();
410 error = dpm_suspend(state); 781 error = dpm_prepare(state);
411 if (error) 782 if (!error)
412 device_resume(); 783 error = dpm_suspend(state);
413 return error; 784 return error;
414} 785}
415EXPORT_SYMBOL_GPL(device_suspend); 786EXPORT_SYMBOL_GPL(device_suspend);
diff --git a/drivers/base/power/power.h b/drivers/base/power/power.h
index a6894f2a4b99..a3252c0e2887 100644
--- a/drivers/base/power/power.h
+++ b/drivers/base/power/power.h
@@ -4,7 +4,7 @@
4 * main.c 4 * main.c
5 */ 5 */
6 6
7extern struct list_head dpm_active; /* The active device list */ 7extern struct list_head dpm_list; /* The active device list */
8 8
9static inline struct device *to_device(struct list_head *entry) 9static inline struct device *to_device(struct list_head *entry)
10{ 10{
diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
index d11f74b038db..596aeecfdffe 100644
--- a/drivers/base/power/sysfs.c
+++ b/drivers/base/power/sysfs.c
@@ -6,9 +6,6 @@
6#include <linux/string.h> 6#include <linux/string.h>
7#include "power.h" 7#include "power.h"
8 8
9int (*platform_enable_wakeup)(struct device *dev, int is_on);
10
11
12/* 9/*
13 * wakeup - Report/change current wakeup option for device 10 * wakeup - Report/change current wakeup option for device
14 * 11 *
diff --git a/drivers/base/power/trace.c b/drivers/base/power/trace.c
index 87a7f1d02578..9b1b20b59e0a 100644
--- a/drivers/base/power/trace.c
+++ b/drivers/base/power/trace.c
@@ -188,9 +188,9 @@ static int show_file_hash(unsigned int value)
188static int show_dev_hash(unsigned int value) 188static int show_dev_hash(unsigned int value)
189{ 189{
190 int match = 0; 190 int match = 0;
191 struct list_head * entry = dpm_active.prev; 191 struct list_head *entry = dpm_list.prev;
192 192
193 while (entry != &dpm_active) { 193 while (entry != &dpm_list) {
194 struct device * dev = to_device(entry); 194 struct device * dev = to_device(entry);
195 unsigned int hash = hash_string(DEVSEED, dev->bus_id, DEVHASH); 195 unsigned int hash = hash_string(DEVSEED, dev->bus_id, DEVHASH);
196 if (hash == value) { 196 if (hash == value) {
diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig
index 0d1d2133d9bc..61ad8d639ba3 100644
--- a/drivers/block/Kconfig
+++ b/drivers/block/Kconfig
@@ -433,4 +433,16 @@ config VIRTIO_BLK
433 This is the virtual block driver for virtio. It can be used with 433 This is the virtual block driver for virtio. It can be used with
434 lguest or QEMU based VMMs (like KVM or Xen). Say Y or M. 434 lguest or QEMU based VMMs (like KVM or Xen). Say Y or M.
435 435
436config BLK_DEV_HD
437 bool "Very old hard disk (MFM/RLL/IDE) driver"
438 depends on HAVE_IDE
439 depends on !ARM || ARCH_RPC || ARCH_SHARK || BROKEN
440 help
441 This is a very old hard disk driver that lacks the enhanced
442 functionality of the newer ones.
443
444 It is required for systems with ancient MFM/RLL/ESDI drives.
445
446 If unsure, say N.
447
436endif # BLK_DEV 448endif # BLK_DEV
diff --git a/drivers/block/Makefile b/drivers/block/Makefile
index 5e584306be99..204332b29578 100644
--- a/drivers/block/Makefile
+++ b/drivers/block/Makefile
@@ -29,5 +29,6 @@ obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o
29obj-$(CONFIG_VIODASD) += viodasd.o 29obj-$(CONFIG_VIODASD) += viodasd.o
30obj-$(CONFIG_BLK_DEV_SX8) += sx8.o 30obj-$(CONFIG_BLK_DEV_SX8) += sx8.o
31obj-$(CONFIG_BLK_DEV_UB) += ub.o 31obj-$(CONFIG_BLK_DEV_UB) += ub.o
32obj-$(CONFIG_BLK_DEV_HD) += hd.o
32 33
33obj-$(CONFIG_XEN_BLKDEV_FRONTEND) += xen-blkfront.o 34obj-$(CONFIG_XEN_BLKDEV_FRONTEND) += xen-blkfront.o
diff --git a/drivers/ide/legacy/hd.c b/drivers/block/hd.c
index abdedf56643e..682243bf2e46 100644
--- a/drivers/ide/legacy/hd.c
+++ b/drivers/block/hd.c
@@ -37,7 +37,6 @@
37#include <linux/slab.h> 37#include <linux/slab.h>
38#include <linux/string.h> 38#include <linux/string.h>
39#include <linux/ioport.h> 39#include <linux/ioport.h>
40#include <linux/mc146818rtc.h> /* CMOS defines */
41#include <linux/init.h> 40#include <linux/init.h>
42#include <linux/blkpg.h> 41#include <linux/blkpg.h>
43#include <linux/hdreg.h> 42#include <linux/hdreg.h>
@@ -812,4 +811,4 @@ static int __init parse_hd_setup(char *line)
812} 811}
813__setup("hd=", parse_hd_setup); 812__setup("hd=", parse_hd_setup);
814 813
815module_init(hd_init); 814late_initcall(hd_init);
diff --git a/drivers/char/apm-emulation.c b/drivers/char/apm-emulation.c
index da8a1658a273..aaca40283be9 100644
--- a/drivers/char/apm-emulation.c
+++ b/drivers/char/apm-emulation.c
@@ -59,6 +59,55 @@ struct apm_queue {
59}; 59};
60 60
61/* 61/*
62 * thread states (for threads using a writable /dev/apm_bios fd):
63 *
64 * SUSPEND_NONE: nothing happening
65 * SUSPEND_PENDING: suspend event queued for thread and pending to be read
66 * SUSPEND_READ: suspend event read, pending acknowledgement
67 * SUSPEND_ACKED: acknowledgement received from thread (via ioctl),
68 * waiting for resume
69 * SUSPEND_ACKTO: acknowledgement timeout
70 * SUSPEND_DONE: thread had acked suspend and is now notified of
71 * resume
72 *
73 * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume
74 *
75 * A thread migrates in one of three paths:
76 * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE
77 * -6-> ACKTO -7-> NONE
78 * NONE -8-> WAIT -9-> NONE
79 *
80 * While in PENDING or READ, the thread is accounted for in the
81 * suspend_acks_pending counter.
82 *
83 * The transitions are invoked as follows:
84 * 1: suspend event is signalled from the core PM code
85 * 2: the suspend event is read from the fd by the userspace thread
86 * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack)
87 * 4: core PM code signals that we have resumed
88 * 5: APM_IOC_SUSPEND ioctl returns
89 *
90 * 6: the notifier invoked from the core PM code timed out waiting
91 * for all relevant threds to enter ACKED state and puts those
92 * that haven't into ACKTO
93 * 7: those threads issue APM_IOC_SUSPEND ioctl too late,
94 * get an error
95 *
96 * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend),
97 * ioctl code invokes pm_suspend()
98 * 9: pm_suspend() returns indicating resume
99 */
100enum apm_suspend_state {
101 SUSPEND_NONE,
102 SUSPEND_PENDING,
103 SUSPEND_READ,
104 SUSPEND_ACKED,
105 SUSPEND_ACKTO,
106 SUSPEND_WAIT,
107 SUSPEND_DONE,
108};
109
110/*
62 * The per-file APM data 111 * The per-file APM data
63 */ 112 */
64struct apm_user { 113struct apm_user {
@@ -69,13 +118,7 @@ struct apm_user {
69 unsigned int reader: 1; 118 unsigned int reader: 1;
70 119
71 int suspend_result; 120 int suspend_result;
72 unsigned int suspend_state; 121 enum apm_suspend_state suspend_state;
73#define SUSPEND_NONE 0 /* no suspend pending */
74#define SUSPEND_PENDING 1 /* suspend pending read */
75#define SUSPEND_READ 2 /* suspend read, pending ack */
76#define SUSPEND_ACKED 3 /* suspend acked */
77#define SUSPEND_WAIT 4 /* waiting for suspend */
78#define SUSPEND_DONE 5 /* suspend completed */
79 122
80 struct apm_queue queue; 123 struct apm_queue queue;
81}; 124};
@@ -83,7 +126,8 @@ struct apm_user {
83/* 126/*
84 * Local variables 127 * Local variables
85 */ 128 */
86static int suspends_pending; 129static atomic_t suspend_acks_pending = ATOMIC_INIT(0);
130static atomic_t userspace_notification_inhibit = ATOMIC_INIT(0);
87static int apm_disabled; 131static int apm_disabled;
88static struct task_struct *kapmd_tsk; 132static struct task_struct *kapmd_tsk;
89 133
@@ -166,78 +210,6 @@ static void queue_event(apm_event_t event)
166 wake_up_interruptible(&apm_waitqueue); 210 wake_up_interruptible(&apm_waitqueue);
167} 211}
168 212
169/*
170 * queue_suspend_event - queue an APM suspend event.
171 *
172 * Check that we're in a state where we can suspend. If not,
173 * return -EBUSY. Otherwise, queue an event to all "writer"
174 * users. If there are no "writer" users, return '1' to
175 * indicate that we can immediately suspend.
176 */
177static int queue_suspend_event(apm_event_t event, struct apm_user *sender)
178{
179 struct apm_user *as;
180 int ret = 1;
181
182 mutex_lock(&state_lock);
183 down_read(&user_list_lock);
184
185 /*
186 * If a thread is still processing, we can't suspend, so reject
187 * the request.
188 */
189 list_for_each_entry(as, &apm_user_list, list) {
190 if (as != sender && as->reader && as->writer && as->suser &&
191 as->suspend_state != SUSPEND_NONE) {
192 ret = -EBUSY;
193 goto out;
194 }
195 }
196
197 list_for_each_entry(as, &apm_user_list, list) {
198 if (as != sender && as->reader && as->writer && as->suser) {
199 as->suspend_state = SUSPEND_PENDING;
200 suspends_pending++;
201 queue_add_event(&as->queue, event);
202 ret = 0;
203 }
204 }
205 out:
206 up_read(&user_list_lock);
207 mutex_unlock(&state_lock);
208 wake_up_interruptible(&apm_waitqueue);
209 return ret;
210}
211
212static void apm_suspend(void)
213{
214 struct apm_user *as;
215 int err = pm_suspend(PM_SUSPEND_MEM);
216
217 /*
218 * Anyone on the APM queues will think we're still suspended.
219 * Send a message so everyone knows we're now awake again.
220 */
221 queue_event(APM_NORMAL_RESUME);
222
223 /*
224 * Finally, wake up anyone who is sleeping on the suspend.
225 */
226 mutex_lock(&state_lock);
227 down_read(&user_list_lock);
228 list_for_each_entry(as, &apm_user_list, list) {
229 if (as->suspend_state == SUSPEND_WAIT ||
230 as->suspend_state == SUSPEND_ACKED) {
231 as->suspend_result = err;
232 as->suspend_state = SUSPEND_DONE;
233 }
234 }
235 up_read(&user_list_lock);
236 mutex_unlock(&state_lock);
237
238 wake_up(&apm_suspend_waitqueue);
239}
240
241static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos) 213static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
242{ 214{
243 struct apm_user *as = fp->private_data; 215 struct apm_user *as = fp->private_data;
@@ -308,25 +280,22 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
308 280
309 as->suspend_result = -EINTR; 281 as->suspend_result = -EINTR;
310 282
311 if (as->suspend_state == SUSPEND_READ) { 283 switch (as->suspend_state) {
312 int pending; 284 case SUSPEND_READ:
313
314 /* 285 /*
315 * If we read a suspend command from /dev/apm_bios, 286 * If we read a suspend command from /dev/apm_bios,
316 * then the corresponding APM_IOC_SUSPEND ioctl is 287 * then the corresponding APM_IOC_SUSPEND ioctl is
317 * interpreted as an acknowledge. 288 * interpreted as an acknowledge.
318 */ 289 */
319 as->suspend_state = SUSPEND_ACKED; 290 as->suspend_state = SUSPEND_ACKED;
320 suspends_pending--; 291 atomic_dec(&suspend_acks_pending);
321 pending = suspends_pending == 0;
322 mutex_unlock(&state_lock); 292 mutex_unlock(&state_lock);
323 293
324 /* 294 /*
325 * If there are no further acknowledges required, 295 * suspend_acks_pending changed, the notifier needs to
326 * suspend the system. 296 * be woken up for this
327 */ 297 */
328 if (pending) 298 wake_up(&apm_suspend_waitqueue);
329 apm_suspend();
330 299
331 /* 300 /*
332 * Wait for the suspend/resume to complete. If there 301 * Wait for the suspend/resume to complete. If there
@@ -342,35 +311,21 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
342 * try_to_freeze() in freezer_count() will not trigger 311 * try_to_freeze() in freezer_count() will not trigger
343 */ 312 */
344 freezer_count(); 313 freezer_count();
345 } else { 314 break;
315 case SUSPEND_ACKTO:
316 as->suspend_result = -ETIMEDOUT;
317 mutex_unlock(&state_lock);
318 break;
319 default:
346 as->suspend_state = SUSPEND_WAIT; 320 as->suspend_state = SUSPEND_WAIT;
347 mutex_unlock(&state_lock); 321 mutex_unlock(&state_lock);
348 322
349 /* 323 /*
350 * Otherwise it is a request to suspend the system. 324 * Otherwise it is a request to suspend the system.
351 * Queue an event for all readers, and expect an 325 * Just invoke pm_suspend(), we'll handle it from
352 * acknowledge from all writers who haven't already 326 * there via the notifier.
353 * acknowledged.
354 */
355 err = queue_suspend_event(APM_USER_SUSPEND, as);
356 if (err < 0) {
357 /*
358 * Avoid taking the lock here - this
359 * should be fine.
360 */
361 as->suspend_state = SUSPEND_NONE;
362 break;
363 }
364
365 if (err > 0)
366 apm_suspend();
367
368 /*
369 * Wait for the suspend/resume to complete. If there
370 * are pending acknowledges, we wait here for them.
371 */ 327 */
372 wait_event_freezable(apm_suspend_waitqueue, 328 as->suspend_result = pm_suspend(PM_SUSPEND_MEM);
373 as->suspend_state == SUSPEND_DONE);
374 } 329 }
375 330
376 mutex_lock(&state_lock); 331 mutex_lock(&state_lock);
@@ -386,7 +341,6 @@ apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
386static int apm_release(struct inode * inode, struct file * filp) 341static int apm_release(struct inode * inode, struct file * filp)
387{ 342{
388 struct apm_user *as = filp->private_data; 343 struct apm_user *as = filp->private_data;
389 int pending = 0;
390 344
391 filp->private_data = NULL; 345 filp->private_data = NULL;
392 346
@@ -396,18 +350,15 @@ static int apm_release(struct inode * inode, struct file * filp)
396 350
397 /* 351 /*
398 * We are now unhooked from the chain. As far as new 352 * We are now unhooked from the chain. As far as new
399 * events are concerned, we no longer exist. However, we 353 * events are concerned, we no longer exist.
400 * need to balance suspends_pending, which means the
401 * possibility of sleeping.
402 */ 354 */
403 mutex_lock(&state_lock); 355 mutex_lock(&state_lock);
404 if (as->suspend_state != SUSPEND_NONE) { 356 if (as->suspend_state == SUSPEND_PENDING ||
405 suspends_pending -= 1; 357 as->suspend_state == SUSPEND_READ)
406 pending = suspends_pending == 0; 358 atomic_dec(&suspend_acks_pending);
407 }
408 mutex_unlock(&state_lock); 359 mutex_unlock(&state_lock);
409 if (pending) 360
410 apm_suspend(); 361 wake_up(&apm_suspend_waitqueue);
411 362
412 kfree(as); 363 kfree(as);
413 return 0; 364 return 0;
@@ -545,7 +496,6 @@ static int kapmd(void *arg)
545{ 496{
546 do { 497 do {
547 apm_event_t event; 498 apm_event_t event;
548 int ret;
549 499
550 wait_event_interruptible(kapmd_wait, 500 wait_event_interruptible(kapmd_wait,
551 !queue_empty(&kapmd_queue) || kthread_should_stop()); 501 !queue_empty(&kapmd_queue) || kthread_should_stop());
@@ -570,20 +520,13 @@ static int kapmd(void *arg)
570 520
571 case APM_USER_SUSPEND: 521 case APM_USER_SUSPEND:
572 case APM_SYS_SUSPEND: 522 case APM_SYS_SUSPEND:
573 ret = queue_suspend_event(event, NULL); 523 pm_suspend(PM_SUSPEND_MEM);
574 if (ret < 0) {
575 /*
576 * We were busy. Try again in 50ms.
577 */
578 queue_add_event(&kapmd_queue, event);
579 msleep(50);
580 }
581 if (ret > 0)
582 apm_suspend();
583 break; 524 break;
584 525
585 case APM_CRITICAL_SUSPEND: 526 case APM_CRITICAL_SUSPEND:
586 apm_suspend(); 527 atomic_inc(&userspace_notification_inhibit);
528 pm_suspend(PM_SUSPEND_MEM);
529 atomic_dec(&userspace_notification_inhibit);
587 break; 530 break;
588 } 531 }
589 } while (1); 532 } while (1);
@@ -591,6 +534,120 @@ static int kapmd(void *arg)
591 return 0; 534 return 0;
592} 535}
593 536
537static int apm_suspend_notifier(struct notifier_block *nb,
538 unsigned long event,
539 void *dummy)
540{
541 struct apm_user *as;
542 int err;
543
544 /* short-cut emergency suspends */
545 if (atomic_read(&userspace_notification_inhibit))
546 return NOTIFY_DONE;
547
548 switch (event) {
549 case PM_SUSPEND_PREPARE:
550 /*
551 * Queue an event to all "writer" users that we want
552 * to suspend and need their ack.
553 */
554 mutex_lock(&state_lock);
555 down_read(&user_list_lock);
556
557 list_for_each_entry(as, &apm_user_list, list) {
558 if (as->suspend_state != SUSPEND_WAIT && as->reader &&
559 as->writer && as->suser) {
560 as->suspend_state = SUSPEND_PENDING;
561 atomic_inc(&suspend_acks_pending);
562 queue_add_event(&as->queue, APM_USER_SUSPEND);
563 }
564 }
565
566 up_read(&user_list_lock);
567 mutex_unlock(&state_lock);
568 wake_up_interruptible(&apm_waitqueue);
569
570 /*
571 * Wait for the the suspend_acks_pending variable to drop to
572 * zero, meaning everybody acked the suspend event (or the
573 * process was killed.)
574 *
575 * If the app won't answer within a short while we assume it
576 * locked up and ignore it.
577 */
578 err = wait_event_interruptible_timeout(
579 apm_suspend_waitqueue,
580 atomic_read(&suspend_acks_pending) == 0,
581 5*HZ);
582
583 /* timed out */
584 if (err == 0) {
585 /*
586 * Move anybody who timed out to "ack timeout" state.
587 *
588 * We could time out and the userspace does the ACK
589 * right after we time out but before we enter the
590 * locked section here, but that's fine.
591 */
592 mutex_lock(&state_lock);
593 down_read(&user_list_lock);
594 list_for_each_entry(as, &apm_user_list, list) {
595 if (as->suspend_state == SUSPEND_PENDING ||
596 as->suspend_state == SUSPEND_READ) {
597 as->suspend_state = SUSPEND_ACKTO;
598 atomic_dec(&suspend_acks_pending);
599 }
600 }
601 up_read(&user_list_lock);
602 mutex_unlock(&state_lock);
603 }
604
605 /* let suspend proceed */
606 if (err >= 0)
607 return NOTIFY_OK;
608
609 /* interrupted by signal */
610 return NOTIFY_BAD;
611
612 case PM_POST_SUSPEND:
613 /*
614 * Anyone on the APM queues will think we're still suspended.
615 * Send a message so everyone knows we're now awake again.
616 */
617 queue_event(APM_NORMAL_RESUME);
618
619 /*
620 * Finally, wake up anyone who is sleeping on the suspend.
621 */
622 mutex_lock(&state_lock);
623 down_read(&user_list_lock);
624 list_for_each_entry(as, &apm_user_list, list) {
625 if (as->suspend_state == SUSPEND_ACKED) {
626 /*
627 * TODO: maybe grab error code, needs core
628 * changes to push the error to the notifier
629 * chain (could use the second parameter if
630 * implemented)
631 */
632 as->suspend_result = 0;
633 as->suspend_state = SUSPEND_DONE;
634 }
635 }
636 up_read(&user_list_lock);
637 mutex_unlock(&state_lock);
638
639 wake_up(&apm_suspend_waitqueue);
640 return NOTIFY_OK;
641
642 default:
643 return NOTIFY_DONE;
644 }
645}
646
647static struct notifier_block apm_notif_block = {
648 .notifier_call = apm_suspend_notifier,
649};
650
594static int __init apm_init(void) 651static int __init apm_init(void)
595{ 652{
596 int ret; 653 int ret;
@@ -604,7 +661,7 @@ static int __init apm_init(void)
604 if (IS_ERR(kapmd_tsk)) { 661 if (IS_ERR(kapmd_tsk)) {
605 ret = PTR_ERR(kapmd_tsk); 662 ret = PTR_ERR(kapmd_tsk);
606 kapmd_tsk = NULL; 663 kapmd_tsk = NULL;
607 return ret; 664 goto out;
608 } 665 }
609 wake_up_process(kapmd_tsk); 666 wake_up_process(kapmd_tsk);
610 667
@@ -613,16 +670,27 @@ static int __init apm_init(void)
613#endif 670#endif
614 671
615 ret = misc_register(&apm_device); 672 ret = misc_register(&apm_device);
616 if (ret != 0) { 673 if (ret)
617 remove_proc_entry("apm", NULL); 674 goto out_stop;
618 kthread_stop(kapmd_tsk);
619 }
620 675
676 ret = register_pm_notifier(&apm_notif_block);
677 if (ret)
678 goto out_unregister;
679
680 return 0;
681
682 out_unregister:
683 misc_deregister(&apm_device);
684 out_stop:
685 remove_proc_entry("apm", NULL);
686 kthread_stop(kapmd_tsk);
687 out:
621 return ret; 688 return ret;
622} 689}
623 690
624static void __exit apm_exit(void) 691static void __exit apm_exit(void)
625{ 692{
693 unregister_pm_notifier(&apm_notif_block);
626 misc_deregister(&apm_device); 694 misc_deregister(&apm_device);
627 remove_proc_entry("apm", NULL); 695 remove_proc_entry("apm", NULL);
628 696
diff --git a/drivers/ide/Kconfig b/drivers/ide/Kconfig
index cf707c8f08d4..15b09b89588a 100644
--- a/drivers/ide/Kconfig
+++ b/drivers/ide/Kconfig
@@ -98,6 +98,9 @@ if BLK_DEV_IDE
98 98
99comment "Please see Documentation/ide/ide.txt for help/info on IDE drives" 99comment "Please see Documentation/ide/ide.txt for help/info on IDE drives"
100 100
101config IDE_TIMINGS
102 bool
103
101config IDE_ATAPI 104config IDE_ATAPI
102 bool 105 bool
103 106
@@ -326,6 +329,7 @@ config BLK_DEV_PLATFORM
326config BLK_DEV_CMD640 329config BLK_DEV_CMD640
327 tristate "CMD640 chipset bugfix/support" 330 tristate "CMD640 chipset bugfix/support"
328 depends on X86 331 depends on X86
332 select IDE_TIMINGS
329 ---help--- 333 ---help---
330 The CMD-Technologies CMD640 IDE chip is used on many common 486 and 334 The CMD-Technologies CMD640 IDE chip is used on many common 486 and
331 Pentium motherboards, usually in combination with a "Neptune" or 335 Pentium motherboards, usually in combination with a "Neptune" or
@@ -455,6 +459,7 @@ config BLK_DEV_AEC62XX
455 459
456config BLK_DEV_ALI15X3 460config BLK_DEV_ALI15X3
457 tristate "ALI M15x3 chipset support" 461 tristate "ALI M15x3 chipset support"
462 select IDE_TIMINGS
458 select BLK_DEV_IDEDMA_PCI 463 select BLK_DEV_IDEDMA_PCI
459 help 464 help
460 This driver ensures (U)DMA support for ALI 1533, 1543 and 1543C 465 This driver ensures (U)DMA support for ALI 1533, 1543 and 1543C
@@ -469,6 +474,7 @@ config BLK_DEV_ALI15X3
469config BLK_DEV_AMD74XX 474config BLK_DEV_AMD74XX
470 tristate "AMD and nVidia IDE support" 475 tristate "AMD and nVidia IDE support"
471 depends on !ARM 476 depends on !ARM
477 select IDE_TIMINGS
472 select BLK_DEV_IDEDMA_PCI 478 select BLK_DEV_IDEDMA_PCI
473 help 479 help
474 This driver adds explicit support for AMD-7xx and AMD-8111 chips 480 This driver adds explicit support for AMD-7xx and AMD-8111 chips
@@ -489,6 +495,7 @@ config BLK_DEV_ATIIXP
489 495
490config BLK_DEV_CMD64X 496config BLK_DEV_CMD64X
491 tristate "CMD64{3|6|8|9} chipset support" 497 tristate "CMD64{3|6|8|9} chipset support"
498 select IDE_TIMINGS
492 select BLK_DEV_IDEDMA_PCI 499 select BLK_DEV_IDEDMA_PCI
493 help 500 help
494 Say Y here if you have an IDE controller which uses any of these 501 Say Y here if you have an IDE controller which uses any of these
@@ -503,6 +510,7 @@ config BLK_DEV_TRIFLEX
503 510
504config BLK_DEV_CY82C693 511config BLK_DEV_CY82C693
505 tristate "CY82C693 chipset support" 512 tristate "CY82C693 chipset support"
513 select IDE_TIMINGS
506 select BLK_DEV_IDEDMA_PCI 514 select BLK_DEV_IDEDMA_PCI
507 help 515 help
508 This driver adds detection and support for the CY82C693 chipset 516 This driver adds detection and support for the CY82C693 chipset
@@ -695,6 +703,7 @@ config BLK_DEV_SIS5513
695config BLK_DEV_SL82C105 703config BLK_DEV_SL82C105
696 tristate "Winbond SL82c105 support" 704 tristate "Winbond SL82c105 support"
697 depends on (PPC || ARM) 705 depends on (PPC || ARM)
706 select IDE_TIMINGS
698 select BLK_DEV_IDEDMA_PCI 707 select BLK_DEV_IDEDMA_PCI
699 help 708 help
700 If you have a Winbond SL82c105 IDE controller, say Y here to enable 709 If you have a Winbond SL82c105 IDE controller, say Y here to enable
@@ -725,6 +734,7 @@ config BLK_DEV_TRM290
725 734
726config BLK_DEV_VIA82CXXX 735config BLK_DEV_VIA82CXXX
727 tristate "VIA82CXXX chipset support" 736 tristate "VIA82CXXX chipset support"
737 select IDE_TIMINGS
728 select BLK_DEV_IDEDMA_PCI 738 select BLK_DEV_IDEDMA_PCI
729 help 739 help
730 This driver adds explicit support for VIA BusMastering IDE chips. 740 This driver adds explicit support for VIA BusMastering IDE chips.
@@ -751,6 +761,7 @@ endif
751config BLK_DEV_IDE_PMAC 761config BLK_DEV_IDE_PMAC
752 tristate "PowerMac on-board IDE support" 762 tristate "PowerMac on-board IDE support"
753 depends on PPC_PMAC && IDE=y && BLK_DEV_IDE=y 763 depends on PPC_PMAC && IDE=y && BLK_DEV_IDE=y
764 select IDE_TIMINGS
754 help 765 help
755 This driver provides support for the on-board IDE controller on 766 This driver provides support for the on-board IDE controller on
756 most of the recent Apple Power Macintoshes and PowerBooks. 767 most of the recent Apple Power Macintoshes and PowerBooks.
@@ -829,13 +840,6 @@ config BLK_DEV_IDE_RAPIDE
829 Say Y here if you want to support the Yellowstone RapIDE controller 840 Say Y here if you want to support the Yellowstone RapIDE controller
830 manufactured for use with Acorn computers. 841 manufactured for use with Acorn computers.
831 842
832config BLK_DEV_IDE_BAST
833 tristate "Simtec BAST / Thorcom VR1000 IDE support"
834 depends on ARM && (ARCH_BAST || MACH_VR1000)
835 help
836 Say Y here if you want to support the onboard IDE channels on the
837 Simtec BAST or the Thorcom VR1000
838
839config IDE_H8300 843config IDE_H8300
840 tristate "H8300 IDE support" 844 tristate "H8300 IDE support"
841 depends on H8300 845 depends on H8300
@@ -919,51 +923,12 @@ config BLK_DEV_Q40IDE
919config BLK_DEV_PALMCHIP_BK3710 923config BLK_DEV_PALMCHIP_BK3710
920 tristate "Palmchip bk3710 IDE controller support" 924 tristate "Palmchip bk3710 IDE controller support"
921 depends on ARCH_DAVINCI 925 depends on ARCH_DAVINCI
926 select IDE_TIMINGS
922 select BLK_DEV_IDEDMA_SFF 927 select BLK_DEV_IDEDMA_SFF
923 help 928 help
924 Say Y here if you want to support the onchip IDE controller on the 929 Say Y here if you want to support the onchip IDE controller on the
925 TI DaVinci SoC 930 TI DaVinci SoC
926 931
927
928config BLK_DEV_MPC8xx_IDE
929 tristate "MPC8xx IDE support"
930 depends on 8xx && (LWMON || IVMS8 || IVML24 || TQM8xxL) && IDE=y && BLK_DEV_IDE=y && !PPC_MERGE
931 help
932 This option provides support for IDE on Motorola MPC8xx Systems.
933 Please see 'Type of MPC8xx IDE interface' for details.
934
935 If unsure, say N.
936
937choice
938 prompt "Type of MPC8xx IDE interface"
939 depends on BLK_DEV_MPC8xx_IDE
940 default IDE_8xx_PCCARD
941
942config IDE_8xx_PCCARD
943 bool "8xx_PCCARD"
944 ---help---
945 Select how the IDE devices are connected to the MPC8xx system:
946
947 8xx_PCCARD uses the 8xx internal PCMCIA interface in combination
948 with a PC Card (e.g. ARGOSY portable Hard Disk Adapter),
949 ATA PC Card HDDs or ATA PC Flash Cards (example: TQM8xxL
950 systems)
951
952 8xx_DIRECT is used for directly connected IDE devices using the 8xx
953 internal PCMCIA interface (example: IVMS8 systems)
954
955 EXT_DIRECT is used for IDE devices directly connected to the 8xx
956 bus using some glue logic, but _not_ the 8xx internal
957 PCMCIA interface (example: IDIF860 systems)
958
959config IDE_8xx_DIRECT
960 bool "8xx_DIRECT"
961
962config IDE_EXT_DIRECT
963 bool "EXT_DIRECT"
964
965endchoice
966
967# no isa -> no vlb 932# no isa -> no vlb
968if ISA && (ALPHA || X86 || MIPS) 933if ISA && (ALPHA || X86 || MIPS)
969 934
@@ -981,6 +946,7 @@ config BLK_DEV_4DRIVES
981 946
982config BLK_DEV_ALI14XX 947config BLK_DEV_ALI14XX
983 tristate "ALI M14xx support" 948 tristate "ALI M14xx support"
949 select IDE_TIMINGS
984 help 950 help
985 This driver is enabled at runtime using the "ali14xx.probe" kernel 951 This driver is enabled at runtime using the "ali14xx.probe" kernel
986 boot parameter. It enables support for the secondary IDE interface 952 boot parameter. It enables support for the secondary IDE interface
@@ -1000,6 +966,7 @@ config BLK_DEV_DTC2278
1000 966
1001config BLK_DEV_HT6560B 967config BLK_DEV_HT6560B
1002 tristate "Holtek HT6560B support" 968 tristate "Holtek HT6560B support"
969 select IDE_TIMINGS
1003 help 970 help
1004 This driver is enabled at runtime using the "ht6560b.probe" kernel 971 This driver is enabled at runtime using the "ht6560b.probe" kernel
1005 boot parameter. It enables support for the secondary IDE interface 972 boot parameter. It enables support for the secondary IDE interface
@@ -1009,6 +976,7 @@ config BLK_DEV_HT6560B
1009 976
1010config BLK_DEV_QD65XX 977config BLK_DEV_QD65XX
1011 tristate "QDI QD65xx support" 978 tristate "QDI QD65xx support"
979 select IDE_TIMINGS
1012 help 980 help
1013 This driver is enabled at runtime using the "qd65xx.probe" kernel 981 This driver is enabled at runtime using the "qd65xx.probe" kernel
1014 boot parameter. It permits faster I/O speeds to be set. See the 982 boot parameter. It permits faster I/O speeds to be set. See the
@@ -1032,30 +1000,4 @@ config BLK_DEV_IDEDMA
1032 1000
1033endif 1001endif
1034 1002
1035config BLK_DEV_HD_ONLY
1036 bool "Old hard disk (MFM/RLL/IDE) driver"
1037 depends on !ARM || ARCH_RPC || ARCH_SHARK || BROKEN
1038 help
1039 There are two drivers for MFM/RLL/IDE hard disks. Most people use
1040 the newer enhanced driver, but this old one is still around for two
1041 reasons. Some older systems have strange timing problems and seem to
1042 work only with the old driver (which itself does not work with some
1043 newer systems). The other reason is that the old driver is smaller,
1044 since it lacks the enhanced functionality of the new one. This makes
1045 it a good choice for systems with very tight memory restrictions, or
1046 for systems with only older MFM/RLL/ESDI drives. Choosing the old
1047 driver can save 13 KB or so of kernel memory.
1048
1049 If you want to use this driver together with the new one you have
1050 to use "hda=noprobe hdb=noprobe" kernel parameters to prevent the new
1051 driver from probing the primary interface.
1052
1053 If you are unsure, then just choose the Enhanced IDE/MFM/RLL driver
1054 instead of this one. For more detailed information, read the
1055 Disk-HOWTO, available from
1056 <http://www.tldp.org/docs.html#howto>.
1057
1058config BLK_DEV_HD
1059 def_bool BLK_DEV_HD_ONLY
1060
1061endif # IDE 1003endif # IDE
diff --git a/drivers/ide/Makefile b/drivers/ide/Makefile
index a2b3f84d710d..5d414e301a5a 100644
--- a/drivers/ide/Makefile
+++ b/drivers/ide/Makefile
@@ -11,9 +11,11 @@
11 11
12EXTRA_CFLAGS += -Idrivers/ide 12EXTRA_CFLAGS += -Idrivers/ide
13 13
14ide-core-y += ide.o ide-io.o ide-iops.o ide-lib.o ide-probe.o ide-taskfile.o 14ide-core-y += ide.o ide-io.o ide-iops.o ide-lib.o ide-probe.o ide-taskfile.o \
15 ide-pio-blacklist.o
15 16
16# core IDE code 17# core IDE code
18ide-core-$(CONFIG_IDE_TIMINGS) += ide-timings.o
17ide-core-$(CONFIG_IDE_ATAPI) += ide-atapi.o 19ide-core-$(CONFIG_IDE_ATAPI) += ide-atapi.o
18ide-core-$(CONFIG_BLK_DEV_IDEPCI) += setup-pci.o 20ide-core-$(CONFIG_BLK_DEV_IDEPCI) += setup-pci.o
19ide-core-$(CONFIG_BLK_DEV_IDEDMA) += ide-dma.o 21ide-core-$(CONFIG_BLK_DEV_IDEDMA) += ide-dma.o
@@ -59,9 +61,3 @@ ifeq ($(CONFIG_BLK_DEV_PLATFORM), y)
59endif 61endif
60 62
61obj-$(CONFIG_BLK_DEV_IDE) += arm/ mips/ 63obj-$(CONFIG_BLK_DEV_IDE) += arm/ mips/
62
63# old hd driver must be last
64ifeq ($(CONFIG_BLK_DEV_HD), y)
65 hd-core-y += legacy/hd.o
66 obj-y += hd-core.o
67endif
diff --git a/drivers/ide/arm/Makefile b/drivers/ide/arm/Makefile
index 936e7b0237f5..5bc26053afa6 100644
--- a/drivers/ide/arm/Makefile
+++ b/drivers/ide/arm/Makefile
@@ -1,7 +1,6 @@
1 1
2obj-$(CONFIG_BLK_DEV_IDE_ICSIDE) += icside.o 2obj-$(CONFIG_BLK_DEV_IDE_ICSIDE) += icside.o
3obj-$(CONFIG_BLK_DEV_IDE_RAPIDE) += rapide.o 3obj-$(CONFIG_BLK_DEV_IDE_RAPIDE) += rapide.o
4obj-$(CONFIG_BLK_DEV_IDE_BAST) += bast-ide.o
5obj-$(CONFIG_BLK_DEV_PALMCHIP_BK3710) += palm_bk3710.o 4obj-$(CONFIG_BLK_DEV_PALMCHIP_BK3710) += palm_bk3710.o
6 5
7ifeq ($(CONFIG_IDE_ARM), m) 6ifeq ($(CONFIG_IDE_ARM), m)
diff --git a/drivers/ide/arm/bast-ide.c b/drivers/ide/arm/bast-ide.c
deleted file mode 100644
index 8e8c28104b45..000000000000
--- a/drivers/ide/arm/bast-ide.c
+++ /dev/null
@@ -1,90 +0,0 @@
1/*
2 * Copyright (c) 2003-2004 Simtec Electronics
3 * Ben Dooks <ben@simtec.co.uk>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9*/
10
11#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/ide.h>
14#include <linux/init.h>
15
16#include <asm/mach-types.h>
17
18#include <asm/io.h>
19#include <asm/irq.h>
20#include <asm/arch/map.h>
21#include <asm/arch/bast-map.h>
22#include <asm/arch/bast-irq.h>
23
24#define DRV_NAME "bast-ide"
25
26static int __init bastide_register(unsigned int base, unsigned int aux, int irq)
27{
28 ide_hwif_t *hwif;
29 hw_regs_t hw;
30 int i;
31 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
32
33 memset(&hw, 0, sizeof(hw));
34
35 base += BAST_IDE_CS;
36 aux += BAST_IDE_CS;
37
38 for (i = 0; i <= 7; i++) {
39 hw.io_ports_array[i] = (unsigned long)base;
40 base += 0x20;
41 }
42
43 hw.io_ports.ctl_addr = aux + (6 * 0x20);
44 hw.irq = irq;
45 hw.chipset = ide_generic;
46
47 hwif = ide_find_port();
48 if (hwif == NULL)
49 goto out;
50
51 i = hwif->index;
52
53 ide_init_port_data(hwif, i);
54 ide_init_port_hw(hwif, &hw);
55 hwif->port_ops = NULL;
56
57 idx[0] = i;
58
59 ide_device_add(idx, NULL);
60out:
61 return 0;
62}
63
64static int __init bastide_init(void)
65{
66 unsigned long base = BAST_VA_IDEPRI + BAST_IDE_CS;
67
68 /* we can treat the VR1000 and the BAST the same */
69
70 if (!(machine_is_bast() || machine_is_vr1000()))
71 return 0;
72
73 printk("BAST: IDE driver, (c) 2003-2004 Simtec Electronics\n");
74
75 if (!request_mem_region(base, 0x400000, DRV_NAME)) {
76 printk(KERN_ERR "%s: resources busy\n", DRV_NAME);
77 return -EBUSY;
78 }
79
80 bastide_register(BAST_VA_IDEPRI, BAST_VA_IDEPRIAUX, IRQ_IDE0);
81 bastide_register(BAST_VA_IDESEC, BAST_VA_IDESECAUX, IRQ_IDE1);
82
83 return 0;
84}
85
86module_init(bastide_init);
87
88MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
89MODULE_LICENSE("GPL");
90MODULE_DESCRIPTION("Simtec BAST / Thorcom VR1000 IDE driver");
diff --git a/drivers/ide/arm/icside.c b/drivers/ide/arm/icside.c
index 061456914ca3..52f58c885783 100644
--- a/drivers/ide/arm/icside.c
+++ b/drivers/ide/arm/icside.c
@@ -21,6 +21,8 @@
21#include <asm/dma.h> 21#include <asm/dma.h>
22#include <asm/ecard.h> 22#include <asm/ecard.h>
23 23
24#define DRV_NAME "icside"
25
24#define ICS_IDENT_OFFSET 0x2280 26#define ICS_IDENT_OFFSET 0x2280
25 27
26#define ICS_ARCIN_V5_INTRSTAT 0x0000 28#define ICS_ARCIN_V5_INTRSTAT 0x0000
@@ -68,6 +70,7 @@ struct icside_state {
68 unsigned int enabled; 70 unsigned int enabled;
69 void __iomem *irq_port; 71 void __iomem *irq_port;
70 void __iomem *ioc_base; 72 void __iomem *ioc_base;
73 unsigned int sel;
71 unsigned int type; 74 unsigned int type;
72 ide_hwif_t *hwif[2]; 75 ide_hwif_t *hwif[2];
73}; 76};
@@ -165,7 +168,8 @@ static const expansioncard_ops_t icside_ops_arcin_v6 = {
165static void icside_maskproc(ide_drive_t *drive, int mask) 168static void icside_maskproc(ide_drive_t *drive, int mask)
166{ 169{
167 ide_hwif_t *hwif = HWIF(drive); 170 ide_hwif_t *hwif = HWIF(drive);
168 struct icside_state *state = hwif->hwif_data; 171 struct expansion_card *ec = ECARD_DEV(hwif->dev);
172 struct icside_state *state = ecard_get_drvdata(ec);
169 unsigned long flags; 173 unsigned long flags;
170 174
171 local_irq_save(flags); 175 local_irq_save(flags);
@@ -308,6 +312,7 @@ static int icside_dma_setup(ide_drive_t *drive)
308{ 312{
309 ide_hwif_t *hwif = HWIF(drive); 313 ide_hwif_t *hwif = HWIF(drive);
310 struct expansion_card *ec = ECARD_DEV(hwif->dev); 314 struct expansion_card *ec = ECARD_DEV(hwif->dev);
315 struct icside_state *state = ecard_get_drvdata(ec);
311 struct request *rq = hwif->hwgroup->rq; 316 struct request *rq = hwif->hwgroup->rq;
312 unsigned int dma_mode; 317 unsigned int dma_mode;
313 318
@@ -331,7 +336,7 @@ static int icside_dma_setup(ide_drive_t *drive)
331 /* 336 /*
332 * Route the DMA signals to the correct interface. 337 * Route the DMA signals to the correct interface.
333 */ 338 */
334 writeb(hwif->select_data, hwif->config_data); 339 writeb(state->sel | hwif->channel, state->ioc_base);
335 340
336 /* 341 /*
337 * Select the correct timing for this drive. 342 * Select the correct timing for this drive.
@@ -359,7 +364,8 @@ static void icside_dma_exec_cmd(ide_drive_t *drive, u8 cmd)
359static int icside_dma_test_irq(ide_drive_t *drive) 364static int icside_dma_test_irq(ide_drive_t *drive)
360{ 365{
361 ide_hwif_t *hwif = HWIF(drive); 366 ide_hwif_t *hwif = HWIF(drive);
362 struct icside_state *state = hwif->hwif_data; 367 struct expansion_card *ec = ECARD_DEV(hwif->dev);
368 struct icside_state *state = ecard_get_drvdata(ec);
363 369
364 return readb(state->irq_port + 370 return readb(state->irq_port +
365 (hwif->channel ? 371 (hwif->channel ?
@@ -411,36 +417,24 @@ static int icside_dma_off_init(ide_hwif_t *hwif, const struct ide_port_info *d)
411 return -EOPNOTSUPP; 417 return -EOPNOTSUPP;
412} 418}
413 419
414static ide_hwif_t * 420static void icside_setup_ports(hw_regs_t *hw, void __iomem *base,
415icside_setup(void __iomem *base, struct cardinfo *info, struct expansion_card *ec) 421 struct cardinfo *info, struct expansion_card *ec)
416{ 422{
417 unsigned long port = (unsigned long)base + info->dataoffset; 423 unsigned long port = (unsigned long)base + info->dataoffset;
418 ide_hwif_t *hwif;
419 424
420 hwif = ide_find_port(); 425 hw->io_ports.data_addr = port;
421 if (hwif) { 426 hw->io_ports.error_addr = port + (1 << info->stepping);
422 /* 427 hw->io_ports.nsect_addr = port + (2 << info->stepping);
423 * Ensure we're using MMIO 428 hw->io_ports.lbal_addr = port + (3 << info->stepping);
424 */ 429 hw->io_ports.lbam_addr = port + (4 << info->stepping);
425 default_hwif_mmiops(hwif); 430 hw->io_ports.lbah_addr = port + (5 << info->stepping);
426 431 hw->io_ports.device_addr = port + (6 << info->stepping);
427 hwif->io_ports.data_addr = port; 432 hw->io_ports.status_addr = port + (7 << info->stepping);
428 hwif->io_ports.error_addr = port + (1 << info->stepping); 433 hw->io_ports.ctl_addr = (unsigned long)base + info->ctrloffset;
429 hwif->io_ports.nsect_addr = port + (2 << info->stepping); 434
430 hwif->io_ports.lbal_addr = port + (3 << info->stepping); 435 hw->irq = ec->irq;
431 hwif->io_ports.lbam_addr = port + (4 << info->stepping); 436 hw->dev = &ec->dev;
432 hwif->io_ports.lbah_addr = port + (5 << info->stepping); 437 hw->chipset = ide_acorn;
433 hwif->io_ports.device_addr = port + (6 << info->stepping);
434 hwif->io_ports.status_addr = port + (7 << info->stepping);
435 hwif->io_ports.ctl_addr =
436 (unsigned long)base + info->ctrloffset;
437 hwif->irq = ec->irq;
438 hwif->chipset = ide_acorn;
439 hwif->gendev.parent = &ec->dev;
440 hwif->dev = &ec->dev;
441 }
442
443 return hwif;
444} 438}
445 439
446static int __init 440static int __init
@@ -449,6 +443,7 @@ icside_register_v5(struct icside_state *state, struct expansion_card *ec)
449 ide_hwif_t *hwif; 443 ide_hwif_t *hwif;
450 void __iomem *base; 444 void __iomem *base;
451 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff }; 445 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
446 hw_regs_t hw;
452 447
453 base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0); 448 base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
454 if (!base) 449 if (!base)
@@ -466,12 +461,19 @@ icside_register_v5(struct icside_state *state, struct expansion_card *ec)
466 */ 461 */
467 icside_irqdisable_arcin_v5(ec, 0); 462 icside_irqdisable_arcin_v5(ec, 0);
468 463
469 hwif = icside_setup(base, &icside_cardinfo_v5, ec); 464 icside_setup_ports(&hw, base, &icside_cardinfo_v5, ec);
465
466 hwif = ide_find_port();
470 if (!hwif) 467 if (!hwif)
471 return -ENODEV; 468 return -ENODEV;
472 469
470 ide_init_port_hw(hwif, &hw);
471 default_hwif_mmiops(hwif);
472
473 state->hwif[0] = hwif; 473 state->hwif[0] = hwif;
474 474
475 ecard_set_drvdata(ec, state);
476
475 idx[0] = hwif->index; 477 idx[0] = hwif->index;
476 478
477 ide_device_add(idx, NULL); 479 ide_device_add(idx, NULL);
@@ -497,6 +499,7 @@ icside_register_v6(struct icside_state *state, struct expansion_card *ec)
497 int ret; 499 int ret;
498 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff }; 500 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
499 struct ide_port_info d = icside_v6_port_info; 501 struct ide_port_info d = icside_v6_port_info;
502 hw_regs_t hw[2];
500 503
501 ioc_base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0); 504 ioc_base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
502 if (!ioc_base) { 505 if (!ioc_base) {
@@ -525,43 +528,47 @@ icside_register_v6(struct icside_state *state, struct expansion_card *ec)
525 528
526 state->irq_port = easi_base; 529 state->irq_port = easi_base;
527 state->ioc_base = ioc_base; 530 state->ioc_base = ioc_base;
531 state->sel = sel;
528 532
529 /* 533 /*
530 * Be on the safe side - disable interrupts 534 * Be on the safe side - disable interrupts
531 */ 535 */
532 icside_irqdisable_arcin_v6(ec, 0); 536 icside_irqdisable_arcin_v6(ec, 0);
533 537
538 icside_setup_ports(&hw[0], easi_base, &icside_cardinfo_v6_1, ec);
539 icside_setup_ports(&hw[1], easi_base, &icside_cardinfo_v6_2, ec);
540
534 /* 541 /*
535 * Find and register the interfaces. 542 * Find and register the interfaces.
536 */ 543 */
537 hwif = icside_setup(easi_base, &icside_cardinfo_v6_1, ec); 544 hwif = ide_find_port();
538 mate = icside_setup(easi_base, &icside_cardinfo_v6_2, ec); 545 if (hwif == NULL)
546 return -ENODEV;
539 547
540 if (!hwif || !mate) { 548 ide_init_port_hw(hwif, &hw[0]);
541 ret = -ENODEV; 549 default_hwif_mmiops(hwif);
542 goto out; 550
551 idx[0] = hwif->index;
552
553 mate = ide_find_port();
554 if (mate) {
555 ide_init_port_hw(mate, &hw[1]);
556 default_hwif_mmiops(mate);
557
558 idx[1] = mate->index;
543 } 559 }
544 560
545 state->hwif[0] = hwif; 561 state->hwif[0] = hwif;
546 state->hwif[1] = mate; 562 state->hwif[1] = mate;
547 563
548 hwif->hwif_data = state; 564 ecard_set_drvdata(ec, state);
549 hwif->config_data = (unsigned long)ioc_base;
550 hwif->select_data = sel;
551
552 mate->hwif_data = state;
553 mate->config_data = (unsigned long)ioc_base;
554 mate->select_data = sel | 1;
555 565
556 if (ec->dma != NO_DMA && !request_dma(ec->dma, hwif->name)) { 566 if (ec->dma != NO_DMA && !request_dma(ec->dma, DRV_NAME)) {
557 d.init_dma = icside_dma_init; 567 d.init_dma = icside_dma_init;
558 d.port_ops = &icside_v6_port_ops; 568 d.port_ops = &icside_v6_port_ops;
559 d.dma_ops = NULL; 569 d.dma_ops = NULL;
560 } 570 }
561 571
562 idx[0] = hwif->index;
563 idx[1] = mate->index;
564
565 ide_device_add(idx, &d); 572 ide_device_add(idx, &d);
566 573
567 return 0; 574 return 0;
@@ -627,10 +634,8 @@ icside_probe(struct expansion_card *ec, const struct ecard_id *id)
627 break; 634 break;
628 } 635 }
629 636
630 if (ret == 0) { 637 if (ret == 0)
631 ecard_set_drvdata(ec, state);
632 goto out; 638 goto out;
633 }
634 639
635 kfree(state); 640 kfree(state);
636 release: 641 release:
diff --git a/drivers/ide/arm/palm_bk3710.c b/drivers/ide/arm/palm_bk3710.c
index 3839f5722985..c79b85b6e4a3 100644
--- a/drivers/ide/arm/palm_bk3710.c
+++ b/drivers/ide/arm/palm_bk3710.c
@@ -74,8 +74,6 @@ struct palm_bk3710_udmatiming {
74#define BK3710_IORDYTMP 0x78 74#define BK3710_IORDYTMP 0x78
75#define BK3710_IORDYTMS 0x7C 75#define BK3710_IORDYTMS 0x7C
76 76
77#include "../ide-timing.h"
78
79static unsigned ideclk_period; /* in nanoseconds */ 77static unsigned ideclk_period; /* in nanoseconds */
80 78
81static const struct palm_bk3710_udmatiming palm_bk3710_udmatimings[6] = { 79static const struct palm_bk3710_udmatiming palm_bk3710_udmatimings[6] = {
@@ -402,7 +400,6 @@ static int __devinit palm_bk3710_probe(struct platform_device *pdev)
402 400
403 i = hwif->index; 401 i = hwif->index;
404 402
405 ide_init_port_data(hwif, i);
406 ide_init_port_hw(hwif, &hw); 403 ide_init_port_hw(hwif, &hw);
407 404
408 default_hwif_mmiops(hwif); 405 default_hwif_mmiops(hwif);
diff --git a/drivers/ide/arm/rapide.c b/drivers/ide/arm/rapide.c
index 1747b2358775..43057e0303c8 100644
--- a/drivers/ide/arm/rapide.c
+++ b/drivers/ide/arm/rapide.c
@@ -11,6 +11,10 @@
11 11
12#include <asm/ecard.h> 12#include <asm/ecard.h>
13 13
14static struct const ide_port_info rapide_port_info = {
15 .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
16};
17
14static void rapide_setup_ports(hw_regs_t *hw, void __iomem *base, 18static void rapide_setup_ports(hw_regs_t *hw, void __iomem *base,
15 void __iomem *ctrl, unsigned int sz, int irq) 19 void __iomem *ctrl, unsigned int sz, int irq)
16{ 20{
@@ -44,25 +48,26 @@ rapide_probe(struct expansion_card *ec, const struct ecard_id *id)
44 goto release; 48 goto release;
45 } 49 }
46 50
47 hwif = ide_find_port(); 51 memset(&hw, 0, sizeof(hw));
48 if (hwif) { 52 rapide_setup_ports(&hw, base, base + 0x818, 1 << 6, ec->irq);
49 memset(&hw, 0, sizeof(hw)); 53 hw.chipset = ide_generic;
50 rapide_setup_ports(&hw, base, base + 0x818, 1 << 6, ec->irq); 54 hw.dev = &ec->dev;
51 hw.chipset = ide_generic;
52 hw.dev = &ec->dev;
53 55
54 ide_init_port_hw(hwif, &hw); 56 hwif = ide_find_port();
57 if (hwif == NULL) {
58 ret = -ENOENT;
59 goto release;
60 }
55 61
56 hwif->host_flags = IDE_HFLAG_MMIO; 62 ide_init_port_hw(hwif, &hw);
57 default_hwif_mmiops(hwif); 63 default_hwif_mmiops(hwif);
58 64
59 idx[0] = hwif->index; 65 idx[0] = hwif->index;
60 66
61 ide_device_add(idx, NULL); 67 ide_device_add(idx, &rapide_port_info);
62 68
63 ecard_set_drvdata(ec, hwif); 69 ecard_set_drvdata(ec, hwif);
64 goto out; 70 goto out;
65 }
66 71
67 release: 72 release:
68 ecard_release_resources(ec); 73 ecard_release_resources(ec);
diff --git a/drivers/ide/h8300/ide-h8300.c b/drivers/ide/h8300/ide-h8300.c
index ae37ee58bae2..20fad6d542cc 100644
--- a/drivers/ide/h8300/ide-h8300.c
+++ b/drivers/ide/h8300/ide-h8300.c
@@ -8,6 +8,8 @@
8#include <asm/io.h> 8#include <asm/io.h>
9#include <asm/irq.h> 9#include <asm/irq.h>
10 10
11#define DRV_NAME "ide-h8300"
12
11#define bswap(d) \ 13#define bswap(d) \
12({ \ 14({ \
13 u16 r; \ 15 u16 r; \
@@ -176,6 +178,10 @@ static inline void hwif_setup(ide_hwif_t *hwif)
176 hwif->output_data = h8300_output_data; 178 hwif->output_data = h8300_output_data;
177} 179}
178 180
181static const struct ide_port_info h8300_port_info = {
182 .host_flags = IDE_HFLAG_NO_IO_32BIT | IDE_HFLAG_NO_DMA,
183};
184
179static int __init h8300_ide_init(void) 185static int __init h8300_ide_init(void)
180{ 186{
181 hw_regs_t hw; 187 hw_regs_t hw;
@@ -183,6 +189,8 @@ static int __init h8300_ide_init(void)
183 int index; 189 int index;
184 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff }; 190 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
185 191
192 printk(KERN_INFO DRV_NAME ": H8/300 generic IDE interface\n");
193
186 if (!request_region(CONFIG_H8300_IDE_BASE, H8300_IDE_GAP*8, "ide-h8300")) 194 if (!request_region(CONFIG_H8300_IDE_BASE, H8300_IDE_GAP*8, "ide-h8300"))
187 goto out_busy; 195 goto out_busy;
188 if (!request_region(CONFIG_H8300_IDE_ALT, H8300_IDE_GAP, "ide-h8300")) { 196 if (!request_region(CONFIG_H8300_IDE_ALT, H8300_IDE_GAP, "ide-h8300")) {
@@ -192,22 +200,17 @@ static int __init h8300_ide_init(void)
192 200
193 hw_setup(&hw); 201 hw_setup(&hw);
194 202
195 hwif = ide_find_port(); 203 hwif = ide_find_port_slot(&h8300_port_info);
196 if (hwif == NULL) { 204 if (hwif == NULL)
197 printk(KERN_ERR "ide-h8300: IDE I/F register failed\n");
198 return -ENOENT; 205 return -ENOENT;
199 }
200 206
201 index = hwif->index; 207 index = hwif->index;
202 ide_init_port_data(hwif, index);
203 ide_init_port_hw(hwif, &hw); 208 ide_init_port_hw(hwif, &hw);
204 hwif_setup(hwif); 209 hwif_setup(hwif);
205 hwif->host_flags = IDE_HFLAG_NO_IO_32BIT;
206 printk(KERN_INFO "ide%d: H8/300 generic IDE interface\n", index);
207 210
208 idx[0] = index; 211 idx[0] = index;
209 212
210 ide_device_add(idx, NULL); 213 ide_device_add(idx, &h8300_port_info);
211 214
212 return 0; 215 return 0;
213 216
diff --git a/drivers/ide/ide-cd.c b/drivers/ide/ide-cd.c
index d99847157186..6e29dd532090 100644
--- a/drivers/ide/ide-cd.c
+++ b/drivers/ide/ide-cd.c
@@ -517,14 +517,9 @@ static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
517 int xferlen, 517 int xferlen,
518 ide_handler_t *handler) 518 ide_handler_t *handler)
519{ 519{
520 ide_startstop_t startstop;
521 struct cdrom_info *info = drive->driver_data; 520 struct cdrom_info *info = drive->driver_data;
522 ide_hwif_t *hwif = drive->hwif; 521 ide_hwif_t *hwif = drive->hwif;
523 522
524 /* wait for the controller to be idle */
525 if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
526 return startstop;
527
528 /* FIXME: for Virtual DMA we must check harder */ 523 /* FIXME: for Virtual DMA we must check harder */
529 if (info->dma) 524 if (info->dma)
530 info->dma = !hwif->dma_ops->dma_setup(drive); 525 info->dma = !hwif->dma_ops->dma_setup(drive);
@@ -604,28 +599,6 @@ static ide_startstop_t cdrom_transfer_packet_command(ide_drive_t *drive,
604} 599}
605 600
606/* 601/*
607 * Block read functions.
608 */
609static void ide_cd_pad_transfer(ide_drive_t *drive, xfer_func_t *xf, int len)
610{
611 while (len > 0) {
612 int dum = 0;
613 xf(drive, NULL, &dum, sizeof(dum));
614 len -= sizeof(dum);
615 }
616}
617
618static void ide_cd_drain_data(ide_drive_t *drive, int nsects)
619{
620 while (nsects > 0) {
621 static char dum[SECTOR_SIZE];
622
623 drive->hwif->input_data(drive, NULL, dum, sizeof(dum));
624 nsects--;
625 }
626}
627
628/*
629 * Check the contents of the interrupt reason register from the cdrom 602 * Check the contents of the interrupt reason register from the cdrom
630 * and attempt to recover if there are problems. Returns 0 if everything's 603 * and attempt to recover if there are problems. Returns 0 if everything's
631 * ok; nonzero if the request has been terminated. 604 * ok; nonzero if the request has been terminated.
@@ -640,15 +613,12 @@ static int ide_cd_check_ireason(ide_drive_t *drive, struct request *rq,
640 if (ireason == (!rw << 1)) 613 if (ireason == (!rw << 1))
641 return 0; 614 return 0;
642 else if (ireason == (rw << 1)) { 615 else if (ireason == (rw << 1)) {
643 ide_hwif_t *hwif = drive->hwif;
644 xfer_func_t *xf;
645 616
646 /* whoops... */ 617 /* whoops... */
647 printk(KERN_ERR "%s: %s: wrong transfer direction!\n", 618 printk(KERN_ERR "%s: %s: wrong transfer direction!\n",
648 drive->name, __func__); 619 drive->name, __func__);
649 620
650 xf = rw ? hwif->output_data : hwif->input_data; 621 ide_pad_transfer(drive, rw, len);
651 ide_cd_pad_transfer(drive, xf, len);
652 } else if (rw == 0 && ireason == 1) { 622 } else if (rw == 0 && ireason == 1) {
653 /* 623 /*
654 * Some drives (ASUS) seem to tell us that status info is 624 * Some drives (ASUS) seem to tell us that status info is
@@ -696,16 +666,9 @@ static int ide_cd_check_transfer_size(ide_drive_t *drive, int len)
696 666
697static ide_startstop_t cdrom_newpc_intr(ide_drive_t *); 667static ide_startstop_t cdrom_newpc_intr(ide_drive_t *);
698 668
699/* 669static ide_startstop_t ide_cd_prepare_rw_request(ide_drive_t *drive,
700 * Routine to send a read/write packet command to the drive. This is usually 670 struct request *rq)
701 * called directly from cdrom_start_{read,write}(). However, for drq_interrupt
702 * devices, it is called from an interrupt when the drive is ready to accept
703 * the command.
704 */
705static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
706{ 671{
707 struct request *rq = HWGROUP(drive)->rq;
708
709 if (rq_data_dir(rq) == READ) { 672 if (rq_data_dir(rq) == READ) {
710 unsigned short sectors_per_frame = 673 unsigned short sectors_per_frame =
711 queue_hardsect_size(drive->queue) >> SECTOR_BITS; 674 queue_hardsect_size(drive->queue) >> SECTOR_BITS;
@@ -742,6 +705,19 @@ static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
742 /* set up the command */ 705 /* set up the command */
743 rq->timeout = ATAPI_WAIT_PC; 706 rq->timeout = ATAPI_WAIT_PC;
744 707
708 return ide_started;
709}
710
711/*
712 * Routine to send a read/write packet command to the drive. This is usually
713 * called directly from cdrom_start_{read,write}(). However, for drq_interrupt
714 * devices, it is called from an interrupt when the drive is ready to accept
715 * the command.
716 */
717static ide_startstop_t cdrom_start_rw_cont(ide_drive_t *drive)
718{
719 struct request *rq = drive->hwif->hwgroup->rq;
720
745 /* send the command to the drive and return */ 721 /* send the command to the drive and return */
746 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr); 722 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
747} 723}
@@ -768,9 +744,8 @@ static ide_startstop_t cdrom_seek_intr(ide_drive_t *drive)
768 return ide_stopped; 744 return ide_stopped;
769} 745}
770 746
771static ide_startstop_t cdrom_start_seek_continuation(ide_drive_t *drive) 747static void ide_cd_prepare_seek_request(ide_drive_t *drive, struct request *rq)
772{ 748{
773 struct request *rq = HWGROUP(drive)->rq;
774 sector_t frame = rq->sector; 749 sector_t frame = rq->sector;
775 750
776 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS); 751 sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
@@ -780,17 +755,13 @@ static ide_startstop_t cdrom_start_seek_continuation(ide_drive_t *drive)
780 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]); 755 put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
781 756
782 rq->timeout = ATAPI_WAIT_PC; 757 rq->timeout = ATAPI_WAIT_PC;
783 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
784} 758}
785 759
786static ide_startstop_t cdrom_start_seek(ide_drive_t *drive, unsigned int block) 760static ide_startstop_t cdrom_start_seek_continuation(ide_drive_t *drive)
787{ 761{
788 struct cdrom_info *info = drive->driver_data; 762 struct request *rq = drive->hwif->hwgroup->rq;
789 763
790 info->dma = 0; 764 return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
791 info->start_seek = jiffies;
792 return cdrom_start_packet_command(drive, 0,
793 cdrom_start_seek_continuation);
794} 765}
795 766
796/* 767/*
@@ -1011,7 +982,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1011 - bio_cur_sectors(rq->bio), 982 - bio_cur_sectors(rq->bio),
1012 thislen >> 9); 983 thislen >> 9);
1013 if (nskip > 0) { 984 if (nskip > 0) {
1014 ide_cd_drain_data(drive, nskip); 985 ide_pad_transfer(drive, write, nskip << 9);
1015 rq->current_nr_sectors -= nskip; 986 rq->current_nr_sectors -= nskip;
1016 thislen -= (nskip << 9); 987 thislen -= (nskip << 9);
1017 } 988 }
@@ -1048,7 +1019,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1048 * If the buffers are full, pipe the rest into 1019 * If the buffers are full, pipe the rest into
1049 * oblivion. 1020 * oblivion.
1050 */ 1021 */
1051 ide_cd_drain_data(drive, thislen >> 9); 1022 ide_pad_transfer(drive, 0, thislen);
1052 else { 1023 else {
1053 printk(KERN_ERR "%s: confused, missing data\n", 1024 printk(KERN_ERR "%s: confused, missing data\n",
1054 drive->name); 1025 drive->name);
@@ -1096,7 +1067,7 @@ static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
1096 1067
1097 /* pad, if necessary */ 1068 /* pad, if necessary */
1098 if (!blk_fs_request(rq) && len > 0) 1069 if (!blk_fs_request(rq) && len > 0)
1099 ide_cd_pad_transfer(drive, xferfunc, len); 1070 ide_pad_transfer(drive, write, len);
1100 1071
1101 if (blk_pc_request(rq)) { 1072 if (blk_pc_request(rq)) {
1102 timeout = rq->timeout; 1073 timeout = rq->timeout;
@@ -1165,21 +1136,17 @@ static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
1165 if (write) 1136 if (write)
1166 cd->devinfo.media_written = 1; 1137 cd->devinfo.media_written = 1;
1167 1138
1168 /* start sending the read/write request to the drive */ 1139 return ide_started;
1169 return cdrom_start_packet_command(drive, 32768, cdrom_start_rw_cont);
1170} 1140}
1171 1141
1172static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive) 1142static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
1173{ 1143{
1174 struct request *rq = HWGROUP(drive)->rq; 1144 struct request *rq = HWGROUP(drive)->rq;
1175 1145
1176 if (!rq->timeout)
1177 rq->timeout = ATAPI_WAIT_PC;
1178
1179 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr); 1146 return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
1180} 1147}
1181 1148
1182static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq) 1149static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1183{ 1150{
1184 struct cdrom_info *info = drive->driver_data; 1151 struct cdrom_info *info = drive->driver_data;
1185 1152
@@ -1191,10 +1158,16 @@ static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1191 info->dma = 0; 1158 info->dma = 0;
1192 1159
1193 /* sg request */ 1160 /* sg request */
1194 if (rq->bio) { 1161 if (rq->bio || ((rq->cmd_type == REQ_TYPE_ATA_PC) && rq->data_len)) {
1195 int mask = drive->queue->dma_alignment; 1162 struct request_queue *q = drive->queue;
1196 unsigned long addr = 1163 unsigned int alignment;
1197 (unsigned long)page_address(bio_page(rq->bio)); 1164 unsigned long addr;
1165 unsigned long stack_mask = ~(THREAD_SIZE - 1);
1166
1167 if (rq->bio)
1168 addr = (unsigned long)bio_data(rq->bio);
1169 else
1170 addr = (unsigned long)rq->data;
1198 1171
1199 info->dma = drive->using_dma; 1172 info->dma = drive->using_dma;
1200 1173
@@ -1204,23 +1177,25 @@ static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
1204 * NOTE! The "len" and "addr" checks should possibly have 1177 * NOTE! The "len" and "addr" checks should possibly have
1205 * separate masks. 1178 * separate masks.
1206 */ 1179 */
1207 if ((rq->data_len & 15) || (addr & mask)) 1180 alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1181 if (addr & alignment || rq->data_len & alignment)
1208 info->dma = 0; 1182 info->dma = 0;
1209 }
1210 1183
1211 /* start sending the command to the drive */ 1184 if (!((addr & stack_mask) ^
1212 return cdrom_start_packet_command(drive, rq->data_len, 1185 ((unsigned long)current->stack & stack_mask)))
1213 cdrom_do_newpc_cont); 1186 info->dma = 0;
1187 }
1214} 1188}
1215 1189
1216/* 1190/*
1217 * cdrom driver request routine. 1191 * cdrom driver request routine.
1218 */ 1192 */
1219static ide_startstop_t ide_do_rw_cdrom(ide_drive_t *drive, struct request *rq, 1193static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
1220 sector_t block) 1194 sector_t block)
1221{ 1195{
1222 ide_startstop_t action;
1223 struct cdrom_info *info = drive->driver_data; 1196 struct cdrom_info *info = drive->driver_data;
1197 ide_handler_t *fn;
1198 int xferlen;
1224 1199
1225 if (blk_fs_request(rq)) { 1200 if (blk_fs_request(rq)) {
1226 if (info->cd_flags & IDE_CD_FLAG_SEEKING) { 1201 if (info->cd_flags & IDE_CD_FLAG_SEEKING) {
@@ -1240,29 +1215,48 @@ static ide_startstop_t ide_do_rw_cdrom(ide_drive_t *drive, struct request *rq,
1240 } 1215 }
1241 if (rq_data_dir(rq) == READ && 1216 if (rq_data_dir(rq) == READ &&
1242 IDE_LARGE_SEEK(info->last_block, block, 1217 IDE_LARGE_SEEK(info->last_block, block,
1243 IDECD_SEEK_THRESHOLD) && 1218 IDECD_SEEK_THRESHOLD) &&
1244 drive->dsc_overlap) 1219 drive->dsc_overlap) {
1245 action = cdrom_start_seek(drive, block); 1220 xferlen = 0;
1246 else 1221 fn = cdrom_start_seek_continuation;
1247 action = cdrom_start_rw(drive, rq); 1222
1223 info->dma = 0;
1224 info->start_seek = jiffies;
1225
1226 ide_cd_prepare_seek_request(drive, rq);
1227 } else {
1228 xferlen = 32768;
1229 fn = cdrom_start_rw_cont;
1230
1231 if (cdrom_start_rw(drive, rq) == ide_stopped)
1232 return ide_stopped;
1233
1234 if (ide_cd_prepare_rw_request(drive, rq) == ide_stopped)
1235 return ide_stopped;
1236 }
1248 info->last_block = block; 1237 info->last_block = block;
1249 return action;
1250 } else if (blk_sense_request(rq) || blk_pc_request(rq) || 1238 } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
1251 rq->cmd_type == REQ_TYPE_ATA_PC) { 1239 rq->cmd_type == REQ_TYPE_ATA_PC) {
1252 return cdrom_do_block_pc(drive, rq); 1240 xferlen = rq->data_len;
1241 fn = cdrom_do_newpc_cont;
1242
1243 if (!rq->timeout)
1244 rq->timeout = ATAPI_WAIT_PC;
1245
1246 cdrom_do_block_pc(drive, rq);
1253 } else if (blk_special_request(rq)) { 1247 } else if (blk_special_request(rq)) {
1254 /* right now this can only be a reset... */ 1248 /* right now this can only be a reset... */
1255 cdrom_end_request(drive, 1); 1249 cdrom_end_request(drive, 1);
1256 return ide_stopped; 1250 return ide_stopped;
1251 } else {
1252 blk_dump_rq_flags(rq, "ide-cd bad flags");
1253 cdrom_end_request(drive, 0);
1254 return ide_stopped;
1257 } 1255 }
1258 1256
1259 blk_dump_rq_flags(rq, "ide-cd bad flags"); 1257 return cdrom_start_packet_command(drive, xferlen, fn);
1260 cdrom_end_request(drive, 0);
1261 return ide_stopped;
1262} 1258}
1263 1259
1264
1265
1266/* 1260/*
1267 * Ioctl handling. 1261 * Ioctl handling.
1268 * 1262 *
@@ -1872,6 +1866,7 @@ static int ide_cdrom_setup(ide_drive_t *drive)
1872 1866
1873 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn); 1867 blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
1874 blk_queue_dma_alignment(drive->queue, 31); 1868 blk_queue_dma_alignment(drive->queue, 31);
1869 blk_queue_update_dma_pad(drive->queue, 15);
1875 drive->queue->unplug_delay = (1 * HZ) / 1000; 1870 drive->queue->unplug_delay = (1 * HZ) / 1000;
1876 if (!drive->queue->unplug_delay) 1871 if (!drive->queue->unplug_delay)
1877 drive->queue->unplug_delay = 1; 1872 drive->queue->unplug_delay = 1;
@@ -1954,10 +1949,9 @@ static ide_driver_t ide_cdrom_driver = {
1954 .version = IDECD_VERSION, 1949 .version = IDECD_VERSION,
1955 .media = ide_cdrom, 1950 .media = ide_cdrom,
1956 .supports_dsc_overlap = 1, 1951 .supports_dsc_overlap = 1,
1957 .do_request = ide_do_rw_cdrom, 1952 .do_request = ide_cd_do_request,
1958 .end_request = ide_end_request, 1953 .end_request = ide_end_request,
1959 .error = __ide_error, 1954 .error = __ide_error,
1960 .abort = __ide_abort,
1961#ifdef CONFIG_IDE_PROC_FS 1955#ifdef CONFIG_IDE_PROC_FS
1962 .proc = idecd_proc, 1956 .proc = idecd_proc,
1963#endif 1957#endif
diff --git a/drivers/ide/ide-disk.c b/drivers/ide/ide-disk.c
index 5f49a4ae9dd8..3a2e80237c10 100644
--- a/drivers/ide/ide-disk.c
+++ b/drivers/ide/ide-disk.c
@@ -985,7 +985,6 @@ static ide_driver_t idedisk_driver = {
985 .do_request = ide_do_rw_disk, 985 .do_request = ide_do_rw_disk,
986 .end_request = ide_end_request, 986 .end_request = ide_end_request,
987 .error = __ide_error, 987 .error = __ide_error,
988 .abort = __ide_abort,
989#ifdef CONFIG_IDE_PROC_FS 988#ifdef CONFIG_IDE_PROC_FS
990 .proc = idedisk_proc, 989 .proc = idedisk_proc,
991#endif 990#endif
diff --git a/drivers/ide/ide-floppy.c b/drivers/ide/ide-floppy.c
index b3689437269f..011d72011cc4 100644
--- a/drivers/ide/ide-floppy.c
+++ b/drivers/ide/ide-floppy.c
@@ -351,10 +351,7 @@ static void ide_floppy_callback(ide_drive_t *drive)
351 351
352static void idefloppy_init_pc(struct ide_atapi_pc *pc) 352static void idefloppy_init_pc(struct ide_atapi_pc *pc)
353{ 353{
354 memset(pc->c, 0, 12); 354 memset(pc, 0, sizeof(*pc));
355 pc->retries = 0;
356 pc->flags = 0;
357 pc->req_xfer = 0;
358 pc->buf = pc->pc_buf; 355 pc->buf = pc->pc_buf;
359 pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE; 356 pc->buf_size = IDEFLOPPY_PC_BUFFER_SIZE;
360 pc->callback = ide_floppy_callback; 357 pc->callback = ide_floppy_callback;
@@ -561,12 +558,6 @@ static void idefloppy_create_start_stop_cmd(struct ide_atapi_pc *pc, int start)
561 pc->c[4] = start; 558 pc->c[4] = start;
562} 559}
563 560
564static void idefloppy_create_test_unit_ready_cmd(struct ide_atapi_pc *pc)
565{
566 idefloppy_init_pc(pc);
567 pc->c[0] = GPCMD_TEST_UNIT_READY;
568}
569
570static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy, 561static void idefloppy_create_rw_cmd(idefloppy_floppy_t *floppy,
571 struct ide_atapi_pc *pc, struct request *rq, 562 struct ide_atapi_pc *pc, struct request *rq,
572 unsigned long sector) 563 unsigned long sector)
@@ -711,10 +702,10 @@ static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive)
711 set_disk_ro(floppy->disk, floppy->wp); 702 set_disk_ro(floppy->disk, floppy->wp);
712 page = &pc.buf[8]; 703 page = &pc.buf[8];
713 704
714 transfer_rate = be16_to_cpu(*(u16 *)&pc.buf[8 + 2]); 705 transfer_rate = be16_to_cpup((__be16 *)&pc.buf[8 + 2]);
715 sector_size = be16_to_cpu(*(u16 *)&pc.buf[8 + 6]); 706 sector_size = be16_to_cpup((__be16 *)&pc.buf[8 + 6]);
716 cyls = be16_to_cpu(*(u16 *)&pc.buf[8 + 8]); 707 cyls = be16_to_cpup((__be16 *)&pc.buf[8 + 8]);
717 rpm = be16_to_cpu(*(u16 *)&pc.buf[8 + 28]); 708 rpm = be16_to_cpup((__be16 *)&pc.buf[8 + 28]);
718 heads = pc.buf[8 + 4]; 709 heads = pc.buf[8 + 4];
719 sectors = pc.buf[8 + 5]; 710 sectors = pc.buf[8 + 5];
720 711
@@ -789,8 +780,8 @@ static int ide_floppy_get_capacity(ide_drive_t *drive)
789 for (i = 0; i < desc_cnt; i++) { 780 for (i = 0; i < desc_cnt; i++) {
790 unsigned int desc_start = 4 + i*8; 781 unsigned int desc_start = 4 + i*8;
791 782
792 blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]); 783 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
793 length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]); 784 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
794 785
795 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n", 786 debug_log("Descriptor %d: %dkB, %d blocks, %d sector size\n",
796 i, blocks * length / 1024, blocks, length); 787 i, blocks * length / 1024, blocks, length);
@@ -911,8 +902,8 @@ static int ide_floppy_get_format_capacities(ide_drive_t *drive, int __user *arg)
911 if (u_index >= u_array_size) 902 if (u_index >= u_array_size)
912 break; /* User-supplied buffer too small */ 903 break; /* User-supplied buffer too small */
913 904
914 blocks = be32_to_cpu(*(u32 *)&pc.buf[desc_start]); 905 blocks = be32_to_cpup((__be32 *)&pc.buf[desc_start]);
915 length = be16_to_cpu(*(u16 *)&pc.buf[desc_start + 6]); 906 length = be16_to_cpup((__be16 *)&pc.buf[desc_start + 6]);
916 907
917 if (put_user(blocks, argp)) 908 if (put_user(blocks, argp))
918 return(-EFAULT); 909 return(-EFAULT);
@@ -1138,7 +1129,6 @@ static ide_driver_t idefloppy_driver = {
1138 .do_request = idefloppy_do_request, 1129 .do_request = idefloppy_do_request,
1139 .end_request = idefloppy_end_request, 1130 .end_request = idefloppy_end_request,
1140 .error = __ide_error, 1131 .error = __ide_error,
1141 .abort = __ide_abort,
1142#ifdef CONFIG_IDE_PROC_FS 1132#ifdef CONFIG_IDE_PROC_FS
1143 .proc = idefloppy_proc, 1133 .proc = idefloppy_proc,
1144#endif 1134#endif
@@ -1166,7 +1156,9 @@ static int idefloppy_open(struct inode *inode, struct file *filp)
1166 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS; 1156 floppy->flags &= ~IDEFLOPPY_FLAG_FORMAT_IN_PROGRESS;
1167 /* Just in case */ 1157 /* Just in case */
1168 1158
1169 idefloppy_create_test_unit_ready_cmd(&pc); 1159 idefloppy_init_pc(&pc);
1160 pc.c[0] = GPCMD_TEST_UNIT_READY;
1161
1170 if (idefloppy_queue_pc_tail(drive, &pc)) { 1162 if (idefloppy_queue_pc_tail(drive, &pc)) {
1171 idefloppy_create_start_stop_cmd(&pc, 1); 1163 idefloppy_create_start_stop_cmd(&pc, 1);
1172 (void) idefloppy_queue_pc_tail(drive, &pc); 1164 (void) idefloppy_queue_pc_tail(drive, &pc);
diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
index 28057747c1f8..661b75a89d4d 100644
--- a/drivers/ide/ide-io.c
+++ b/drivers/ide/ide-io.c
@@ -504,55 +504,6 @@ ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
504 504
505EXPORT_SYMBOL_GPL(ide_error); 505EXPORT_SYMBOL_GPL(ide_error);
506 506
507ide_startstop_t __ide_abort(ide_drive_t *drive, struct request *rq)
508{
509 if (drive->media != ide_disk)
510 rq->errors |= ERROR_RESET;
511
512 ide_kill_rq(drive, rq);
513
514 return ide_stopped;
515}
516
517EXPORT_SYMBOL_GPL(__ide_abort);
518
519/**
520 * ide_abort - abort pending IDE operations
521 * @drive: drive the error occurred on
522 * @msg: message to report
523 *
524 * ide_abort kills and cleans up when we are about to do a
525 * host initiated reset on active commands. Longer term we
526 * want handlers to have sensible abort handling themselves
527 *
528 * This differs fundamentally from ide_error because in
529 * this case the command is doing just fine when we
530 * blow it away.
531 */
532
533ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
534{
535 struct request *rq;
536
537 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
538 return ide_stopped;
539
540 /* retry only "normal" I/O: */
541 if (!blk_fs_request(rq)) {
542 rq->errors = 1;
543 ide_end_drive_cmd(drive, BUSY_STAT, 0);
544 return ide_stopped;
545 }
546
547 if (rq->rq_disk) {
548 ide_driver_t *drv;
549
550 drv = *(ide_driver_t **)rq->rq_disk->private_data;
551 return drv->abort(drive, rq);
552 } else
553 return __ide_abort(drive, rq);
554}
555
556static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf) 507static void ide_tf_set_specify_cmd(ide_drive_t *drive, struct ide_taskfile *tf)
557{ 508{
558 tf->nsect = drive->sect; 509 tf->nsect = drive->sect;
@@ -766,6 +717,18 @@ static ide_startstop_t execute_drive_cmd (ide_drive_t *drive,
766 return ide_stopped; 717 return ide_stopped;
767} 718}
768 719
720static ide_startstop_t ide_special_rq(ide_drive_t *drive, struct request *rq)
721{
722 switch (rq->cmd[0]) {
723 case REQ_DRIVE_RESET:
724 return ide_do_reset(drive);
725 default:
726 blk_dump_rq_flags(rq, "ide_special_rq - bad request");
727 ide_end_request(drive, 0, 0);
728 return ide_stopped;
729 }
730}
731
769static void ide_check_pm_state(ide_drive_t *drive, struct request *rq) 732static void ide_check_pm_state(ide_drive_t *drive, struct request *rq)
770{ 733{
771 struct request_pm_state *pm = rq->data; 734 struct request_pm_state *pm = rq->data;
@@ -869,7 +832,16 @@ static ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
869 pm->pm_step == ide_pm_state_completed) 832 pm->pm_step == ide_pm_state_completed)
870 ide_complete_pm_request(drive, rq); 833 ide_complete_pm_request(drive, rq);
871 return startstop; 834 return startstop;
872 } 835 } else if (!rq->rq_disk && blk_special_request(rq))
836 /*
837 * TODO: Once all ULDs have been modified to
838 * check for specific op codes rather than
839 * blindly accepting any special request, the
840 * check for ->rq_disk above may be replaced
841 * by a more suitable mechanism or even
842 * dropped entirely.
843 */
844 return ide_special_rq(drive, rq);
873 845
874 drv = *(ide_driver_t **)rq->rq_disk->private_data; 846 drv = *(ide_driver_t **)rq->rq_disk->private_data;
875 return drv->do_request(drive, rq, block); 847 return drv->do_request(drive, rq, block);
diff --git a/drivers/ide/ide-iops.c b/drivers/ide/ide-iops.c
index 80ad4f234f3f..44aaec256a30 100644
--- a/drivers/ide/ide-iops.c
+++ b/drivers/ide/ide-iops.c
@@ -905,6 +905,14 @@ void ide_execute_pkt_cmd(ide_drive_t *drive)
905} 905}
906EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd); 906EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
907 907
908static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
909{
910 struct request *rq = drive->hwif->hwgroup->rq;
911
912 if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
913 ide_end_request(drive, err ? err : 1, 0);
914}
915
908/* needed below */ 916/* needed below */
909static ide_startstop_t do_reset1 (ide_drive_t *, int); 917static ide_startstop_t do_reset1 (ide_drive_t *, int);
910 918
@@ -940,7 +948,7 @@ static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
940 } 948 }
941 /* done polling */ 949 /* done polling */
942 hwgroup->polling = 0; 950 hwgroup->polling = 0;
943 hwgroup->resetting = 0; 951 ide_complete_drive_reset(drive, 0);
944 return ide_stopped; 952 return ide_stopped;
945} 953}
946 954
@@ -956,12 +964,14 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
956 ide_hwif_t *hwif = HWIF(drive); 964 ide_hwif_t *hwif = HWIF(drive);
957 const struct ide_port_ops *port_ops = hwif->port_ops; 965 const struct ide_port_ops *port_ops = hwif->port_ops;
958 u8 tmp; 966 u8 tmp;
967 int err = 0;
959 968
960 if (port_ops && port_ops->reset_poll) { 969 if (port_ops && port_ops->reset_poll) {
961 if (port_ops->reset_poll(drive)) { 970 err = port_ops->reset_poll(drive);
971 if (err) {
962 printk(KERN_ERR "%s: host reset_poll failure for %s.\n", 972 printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
963 hwif->name, drive->name); 973 hwif->name, drive->name);
964 return ide_stopped; 974 goto out;
965 } 975 }
966 } 976 }
967 977
@@ -975,6 +985,7 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
975 } 985 }
976 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp); 986 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
977 drive->failures++; 987 drive->failures++;
988 err = -EIO;
978 } else { 989 } else {
979 printk("%s: reset: ", hwif->name); 990 printk("%s: reset: ", hwif->name);
980 tmp = ide_read_error(drive); 991 tmp = ide_read_error(drive);
@@ -1001,10 +1012,12 @@ static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
1001 if (tmp & 0x80) 1012 if (tmp & 0x80)
1002 printk("; slave: failed"); 1013 printk("; slave: failed");
1003 printk("\n"); 1014 printk("\n");
1015 err = -EIO;
1004 } 1016 }
1005 } 1017 }
1018out:
1006 hwgroup->polling = 0; /* done polling */ 1019 hwgroup->polling = 0; /* done polling */
1007 hwgroup->resetting = 0; /* done reset attempt */ 1020 ide_complete_drive_reset(drive, err);
1008 return ide_stopped; 1021 return ide_stopped;
1009} 1022}
1010 1023
@@ -1090,7 +1103,6 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1090 1103
1091 /* For an ATAPI device, first try an ATAPI SRST. */ 1104 /* For an ATAPI device, first try an ATAPI SRST. */
1092 if (drive->media != ide_disk && !do_not_try_atapi) { 1105 if (drive->media != ide_disk && !do_not_try_atapi) {
1093 hwgroup->resetting = 1;
1094 pre_reset(drive); 1106 pre_reset(drive);
1095 SELECT_DRIVE(drive); 1107 SELECT_DRIVE(drive);
1096 udelay (20); 1108 udelay (20);
@@ -1112,10 +1124,10 @@ static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
1112 1124
1113 if (io_ports->ctl_addr == 0) { 1125 if (io_ports->ctl_addr == 0) {
1114 spin_unlock_irqrestore(&ide_lock, flags); 1126 spin_unlock_irqrestore(&ide_lock, flags);
1127 ide_complete_drive_reset(drive, -ENXIO);
1115 return ide_stopped; 1128 return ide_stopped;
1116 } 1129 }
1117 1130
1118 hwgroup->resetting = 1;
1119 /* 1131 /*
1120 * Note that we also set nIEN while resetting the device, 1132 * Note that we also set nIEN while resetting the device,
1121 * to mask unwanted interrupts from the interface during the reset. 1133 * to mask unwanted interrupts from the interface during the reset.
diff --git a/drivers/ide/ide-lib.c b/drivers/ide/ide-lib.c
index 47af80df6872..13af72f09ec4 100644
--- a/drivers/ide/ide-lib.c
+++ b/drivers/ide/ide-lib.c
@@ -1,26 +1,11 @@
1#include <linux/module.h>
2#include <linux/types.h> 1#include <linux/types.h>
3#include <linux/string.h> 2#include <linux/string.h>
4#include <linux/kernel.h> 3#include <linux/kernel.h>
5#include <linux/timer.h>
6#include <linux/mm.h>
7#include <linux/interrupt.h> 4#include <linux/interrupt.h>
8#include <linux/major.h>
9#include <linux/errno.h>
10#include <linux/genhd.h>
11#include <linux/blkpg.h>
12#include <linux/slab.h>
13#include <linux/pci.h>
14#include <linux/delay.h>
15#include <linux/hdreg.h> 5#include <linux/hdreg.h>
16#include <linux/ide.h> 6#include <linux/ide.h>
17#include <linux/bitops.h> 7#include <linux/bitops.h>
18 8
19#include <asm/byteorder.h>
20#include <asm/irq.h>
21#include <asm/uaccess.h>
22#include <asm/io.h>
23
24static const char *udma_str[] = 9static const char *udma_str[] =
25 { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44", 10 { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44",
26 "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" }; 11 "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
@@ -90,142 +75,6 @@ static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
90 return min(speed, mode); 75 return min(speed, mode);
91} 76}
92 77
93/*
94 * Standard (generic) timings for PIO modes, from ATA2 specification.
95 * These timings are for access to the IDE data port register *only*.
96 * Some drives may specify a mode, while also specifying a different
97 * value for cycle_time (from drive identification data).
98 */
99const ide_pio_timings_t ide_pio_timings[6] = {
100 { 70, 165, 600 }, /* PIO Mode 0 */
101 { 50, 125, 383 }, /* PIO Mode 1 */
102 { 30, 100, 240 }, /* PIO Mode 2 */
103 { 30, 80, 180 }, /* PIO Mode 3 with IORDY */
104 { 25, 70, 120 }, /* PIO Mode 4 with IORDY */
105 { 20, 50, 100 } /* PIO Mode 5 with IORDY (nonstandard) */
106};
107
108EXPORT_SYMBOL_GPL(ide_pio_timings);
109
110/*
111 * Shared data/functions for determining best PIO mode for an IDE drive.
112 * Most of this stuff originally lived in cmd640.c, and changes to the
113 * ide_pio_blacklist[] table should be made with EXTREME CAUTION to avoid
114 * breaking the fragile cmd640.c support.
115 */
116
117/*
118 * Black list. Some drives incorrectly report their maximal PIO mode,
119 * at least in respect to CMD640. Here we keep info on some known drives.
120 */
121static struct ide_pio_info {
122 const char *name;
123 int pio;
124} ide_pio_blacklist [] = {
125 { "Conner Peripherals 540MB - CFS540A", 3 },
126
127 { "WDC AC2700", 3 },
128 { "WDC AC2540", 3 },
129 { "WDC AC2420", 3 },
130 { "WDC AC2340", 3 },
131 { "WDC AC2250", 0 },
132 { "WDC AC2200", 0 },
133 { "WDC AC21200", 4 },
134 { "WDC AC2120", 0 },
135 { "WDC AC2850", 3 },
136 { "WDC AC1270", 3 },
137 { "WDC AC1170", 1 },
138 { "WDC AC1210", 1 },
139 { "WDC AC280", 0 },
140 { "WDC AC31000", 3 },
141 { "WDC AC31200", 3 },
142
143 { "Maxtor 7131 AT", 1 },
144 { "Maxtor 7171 AT", 1 },
145 { "Maxtor 7213 AT", 1 },
146 { "Maxtor 7245 AT", 1 },
147 { "Maxtor 7345 AT", 1 },
148 { "Maxtor 7546 AT", 3 },
149 { "Maxtor 7540 AV", 3 },
150
151 { "SAMSUNG SHD-3121A", 1 },
152 { "SAMSUNG SHD-3122A", 1 },
153 { "SAMSUNG SHD-3172A", 1 },
154
155 { "ST5660A", 3 },
156 { "ST3660A", 3 },
157 { "ST3630A", 3 },
158 { "ST3655A", 3 },
159 { "ST3391A", 3 },
160 { "ST3390A", 1 },
161 { "ST3600A", 1 },
162 { "ST3290A", 0 },
163 { "ST3144A", 0 },
164 { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on */
165 /* drive) according to Seagates FIND-ATA program */
166
167 { "QUANTUM ELS127A", 0 },
168 { "QUANTUM ELS170A", 0 },
169 { "QUANTUM LPS240A", 0 },
170 { "QUANTUM LPS210A", 3 },
171 { "QUANTUM LPS270A", 3 },
172 { "QUANTUM LPS365A", 3 },
173 { "QUANTUM LPS540A", 3 },
174 { "QUANTUM LIGHTNING 540A", 3 },
175 { "QUANTUM LIGHTNING 730A", 3 },
176
177 { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
178 { "QUANTUM FIREBALL_640", 3 },
179 { "QUANTUM FIREBALL_1080", 3 },
180 { "QUANTUM FIREBALL_1280", 3 },
181 { NULL, 0 }
182};
183
184/**
185 * ide_scan_pio_blacklist - check for a blacklisted drive
186 * @model: Drive model string
187 *
188 * This routine searches the ide_pio_blacklist for an entry
189 * matching the start/whole of the supplied model name.
190 *
191 * Returns -1 if no match found.
192 * Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
193 */
194
195static int ide_scan_pio_blacklist (char *model)
196{
197 struct ide_pio_info *p;
198
199 for (p = ide_pio_blacklist; p->name != NULL; p++) {
200 if (strncmp(p->name, model, strlen(p->name)) == 0)
201 return p->pio;
202 }
203 return -1;
204}
205
206unsigned int ide_pio_cycle_time(ide_drive_t *drive, u8 pio)
207{
208 struct hd_driveid *id = drive->id;
209 int cycle_time = 0;
210
211 if (id->field_valid & 2) {
212 if (id->capability & 8)
213 cycle_time = id->eide_pio_iordy;
214 else
215 cycle_time = id->eide_pio;
216 }
217
218 /* conservative "downgrade" for all pre-ATA2 drives */
219 if (pio < 3) {
220 if (cycle_time && cycle_time < ide_pio_timings[pio].cycle_time)
221 cycle_time = 0; /* use standard timing */
222 }
223
224 return cycle_time ? cycle_time : ide_pio_timings[pio].cycle_time;
225}
226
227EXPORT_SYMBOL_GPL(ide_pio_cycle_time);
228
229/** 78/**
230 * ide_get_best_pio_mode - get PIO mode from drive 79 * ide_get_best_pio_mode - get PIO mode from drive
231 * @drive: drive to consider 80 * @drive: drive to consider
diff --git a/drivers/ide/ide-pio-blacklist.c b/drivers/ide/ide-pio-blacklist.c
new file mode 100644
index 000000000000..a8c2c8f8660a
--- /dev/null
+++ b/drivers/ide/ide-pio-blacklist.c
@@ -0,0 +1,94 @@
1/*
2 * PIO blacklist. Some drives incorrectly report their maximal PIO mode,
3 * at least in respect to CMD640. Here we keep info on some known drives.
4 *
5 * Changes to the ide_pio_blacklist[] should be made with EXTREME CAUTION
6 * to avoid breaking the fragile cmd640.c support.
7 */
8
9#include <linux/string.h>
10
11static struct ide_pio_info {
12 const char *name;
13 int pio;
14} ide_pio_blacklist [] = {
15 { "Conner Peripherals 540MB - CFS540A", 3 },
16
17 { "WDC AC2700", 3 },
18 { "WDC AC2540", 3 },
19 { "WDC AC2420", 3 },
20 { "WDC AC2340", 3 },
21 { "WDC AC2250", 0 },
22 { "WDC AC2200", 0 },
23 { "WDC AC21200", 4 },
24 { "WDC AC2120", 0 },
25 { "WDC AC2850", 3 },
26 { "WDC AC1270", 3 },
27 { "WDC AC1170", 1 },
28 { "WDC AC1210", 1 },
29 { "WDC AC280", 0 },
30 { "WDC AC31000", 3 },
31 { "WDC AC31200", 3 },
32
33 { "Maxtor 7131 AT", 1 },
34 { "Maxtor 7171 AT", 1 },
35 { "Maxtor 7213 AT", 1 },
36 { "Maxtor 7245 AT", 1 },
37 { "Maxtor 7345 AT", 1 },
38 { "Maxtor 7546 AT", 3 },
39 { "Maxtor 7540 AV", 3 },
40
41 { "SAMSUNG SHD-3121A", 1 },
42 { "SAMSUNG SHD-3122A", 1 },
43 { "SAMSUNG SHD-3172A", 1 },
44
45 { "ST5660A", 3 },
46 { "ST3660A", 3 },
47 { "ST3630A", 3 },
48 { "ST3655A", 3 },
49 { "ST3391A", 3 },
50 { "ST3390A", 1 },
51 { "ST3600A", 1 },
52 { "ST3290A", 0 },
53 { "ST3144A", 0 },
54 { "ST3491A", 1 }, /* reports 3, should be 1 or 2 (depending on drive)
55 according to Seagate's FIND-ATA program */
56
57 { "QUANTUM ELS127A", 0 },
58 { "QUANTUM ELS170A", 0 },
59 { "QUANTUM LPS240A", 0 },
60 { "QUANTUM LPS210A", 3 },
61 { "QUANTUM LPS270A", 3 },
62 { "QUANTUM LPS365A", 3 },
63 { "QUANTUM LPS540A", 3 },
64 { "QUANTUM LIGHTNING 540A", 3 },
65 { "QUANTUM LIGHTNING 730A", 3 },
66
67 { "QUANTUM FIREBALL_540", 3 }, /* Older Quantum Fireballs don't work */
68 { "QUANTUM FIREBALL_640", 3 },
69 { "QUANTUM FIREBALL_1080", 3 },
70 { "QUANTUM FIREBALL_1280", 3 },
71 { NULL, 0 }
72};
73
74/**
75 * ide_scan_pio_blacklist - check for a blacklisted drive
76 * @model: Drive model string
77 *
78 * This routine searches the ide_pio_blacklist for an entry
79 * matching the start/whole of the supplied model name.
80 *
81 * Returns -1 if no match found.
82 * Otherwise returns the recommended PIO mode from ide_pio_blacklist[].
83 */
84
85int ide_scan_pio_blacklist(char *model)
86{
87 struct ide_pio_info *p;
88
89 for (p = ide_pio_blacklist; p->name != NULL; p++) {
90 if (strncmp(p->name, model, strlen(p->name)) == 0)
91 return p->pio;
92 }
93 return -1;
94}
diff --git a/drivers/ide/ide-pnp.c b/drivers/ide/ide-pnp.c
index adbd01784162..03f2ef5470a3 100644
--- a/drivers/ide/ide-pnp.c
+++ b/drivers/ide/ide-pnp.c
@@ -33,6 +33,8 @@ static int idepnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
33 ide_hwif_t *hwif; 33 ide_hwif_t *hwif;
34 unsigned long base, ctl; 34 unsigned long base, ctl;
35 35
36 printk(KERN_INFO DRV_NAME ": generic PnP IDE interface\n");
37
36 if (!(pnp_port_valid(dev, 0) && pnp_port_valid(dev, 1) && pnp_irq_valid(dev, 0))) 38 if (!(pnp_port_valid(dev, 0) && pnp_port_valid(dev, 1) && pnp_irq_valid(dev, 0)))
37 return -1; 39 return -1;
38 40
@@ -62,10 +64,8 @@ static int idepnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
62 u8 index = hwif->index; 64 u8 index = hwif->index;
63 u8 idx[4] = { index, 0xff, 0xff, 0xff }; 65 u8 idx[4] = { index, 0xff, 0xff, 0xff };
64 66
65 ide_init_port_data(hwif, index);
66 ide_init_port_hw(hwif, &hw); 67 ide_init_port_hw(hwif, &hw);
67 68
68 printk(KERN_INFO "ide%d: generic PnP IDE interface\n", index);
69 pnp_set_drvdata(dev, hwif); 69 pnp_set_drvdata(dev, hwif);
70 70
71 ide_device_add(idx, NULL); 71 ide_device_add(idx, NULL);
diff --git a/drivers/ide/ide-probe.c b/drivers/ide/ide-probe.c
index d21e51a02c3e..235ebdb29b28 100644
--- a/drivers/ide/ide-probe.c
+++ b/drivers/ide/ide-probe.c
@@ -39,6 +39,8 @@
39#include <asm/uaccess.h> 39#include <asm/uaccess.h>
40#include <asm/io.h> 40#include <asm/io.h>
41 41
42static ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
43
42/** 44/**
43 * generic_id - add a generic drive id 45 * generic_id - add a generic drive id
44 * @drive: drive to make an ID block for 46 * @drive: drive to make an ID block for
@@ -1318,10 +1320,10 @@ static void ide_port_init_devices(ide_hwif_t *hwif)
1318 drive->unmask = 1; 1320 drive->unmask = 1;
1319 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS) 1321 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1320 drive->no_unmask = 1; 1322 drive->no_unmask = 1;
1321 }
1322 1323
1323 if (port_ops && port_ops->port_init_devs) 1324 if (port_ops && port_ops->init_dev)
1324 port_ops->port_init_devs(hwif); 1325 port_ops->init_dev(drive);
1326 }
1325} 1327}
1326 1328
1327static void ide_init_port(ide_hwif_t *hwif, unsigned int port, 1329static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
@@ -1473,22 +1475,29 @@ ide_hwif_t *ide_find_port_slot(const struct ide_port_info *d)
1473 for (; i < MAX_HWIFS; i++) { 1475 for (; i < MAX_HWIFS; i++) {
1474 hwif = &ide_hwifs[i]; 1476 hwif = &ide_hwifs[i];
1475 if (hwif->chipset == ide_unknown) 1477 if (hwif->chipset == ide_unknown)
1476 return hwif; 1478 goto out_found;
1477 } 1479 }
1478 } else { 1480 } else {
1479 for (i = 2; i < MAX_HWIFS; i++) { 1481 for (i = 2; i < MAX_HWIFS; i++) {
1480 hwif = &ide_hwifs[i]; 1482 hwif = &ide_hwifs[i];
1481 if (hwif->chipset == ide_unknown) 1483 if (hwif->chipset == ide_unknown)
1482 return hwif; 1484 goto out_found;
1483 } 1485 }
1484 for (i = 0; i < 2 && i < MAX_HWIFS; i++) { 1486 for (i = 0; i < 2 && i < MAX_HWIFS; i++) {
1485 hwif = &ide_hwifs[i]; 1487 hwif = &ide_hwifs[i];
1486 if (hwif->chipset == ide_unknown) 1488 if (hwif->chipset == ide_unknown)
1487 return hwif; 1489 goto out_found;
1488 } 1490 }
1489 } 1491 }
1490 1492
1493 printk(KERN_ERR "%s: no free slot for interface\n",
1494 d ? d->name : "ide");
1495
1491 return NULL; 1496 return NULL;
1497
1498out_found:
1499 ide_init_port_data(hwif, i);
1500 return hwif;
1492} 1501}
1493EXPORT_SYMBOL_GPL(ide_find_port_slot); 1502EXPORT_SYMBOL_GPL(ide_find_port_slot);
1494 1503
diff --git a/drivers/ide/ide-tape.c b/drivers/ide/ide-tape.c
index f9cf1670e4e1..b711ab96e287 100644
--- a/drivers/ide/ide-tape.c
+++ b/drivers/ide/ide-tape.c
@@ -2591,7 +2591,6 @@ static ide_driver_t idetape_driver = {
2591 .do_request = idetape_do_request, 2591 .do_request = idetape_do_request,
2592 .end_request = idetape_end_request, 2592 .end_request = idetape_end_request,
2593 .error = __ide_error, 2593 .error = __ide_error,
2594 .abort = __ide_abort,
2595#ifdef CONFIG_IDE_PROC_FS 2594#ifdef CONFIG_IDE_PROC_FS
2596 .proc = idetape_proc, 2595 .proc = idetape_proc,
2597#endif 2596#endif
diff --git a/drivers/ide/ide-taskfile.c b/drivers/ide/ide-taskfile.c
index cf55a48a7dd2..1fbdb746dc88 100644
--- a/drivers/ide/ide-taskfile.c
+++ b/drivers/ide/ide-taskfile.c
@@ -8,28 +8,18 @@
8 * The big the bad and the ugly. 8 * The big the bad and the ugly.
9 */ 9 */
10 10
11#include <linux/module.h>
12#include <linux/types.h> 11#include <linux/types.h>
13#include <linux/string.h> 12#include <linux/string.h>
14#include <linux/kernel.h> 13#include <linux/kernel.h>
15#include <linux/timer.h>
16#include <linux/mm.h>
17#include <linux/sched.h> 14#include <linux/sched.h>
18#include <linux/interrupt.h> 15#include <linux/interrupt.h>
19#include <linux/major.h>
20#include <linux/errno.h> 16#include <linux/errno.h>
21#include <linux/genhd.h>
22#include <linux/blkpg.h>
23#include <linux/slab.h> 17#include <linux/slab.h>
24#include <linux/pci.h>
25#include <linux/delay.h> 18#include <linux/delay.h>
26#include <linux/hdreg.h> 19#include <linux/hdreg.h>
27#include <linux/ide.h> 20#include <linux/ide.h>
28#include <linux/bitops.h>
29#include <linux/scatterlist.h> 21#include <linux/scatterlist.h>
30 22
31#include <asm/byteorder.h>
32#include <asm/irq.h>
33#include <asm/uaccess.h> 23#include <asm/uaccess.h>
34#include <asm/io.h> 24#include <asm/io.h>
35 25
@@ -62,25 +52,6 @@ int taskfile_lib_get_identify (ide_drive_t *drive, u8 *buf)
62 return ide_raw_taskfile(drive, &args, buf, 1); 52 return ide_raw_taskfile(drive, &args, buf, 1);
63} 53}
64 54
65static int inline task_dma_ok(ide_task_t *task)
66{
67 if (blk_fs_request(task->rq) || (task->tf_flags & IDE_TFLAG_FLAGGED))
68 return 1;
69
70 switch (task->tf.command) {
71 case WIN_WRITEDMA_ONCE:
72 case WIN_WRITEDMA:
73 case WIN_WRITEDMA_EXT:
74 case WIN_READDMA_ONCE:
75 case WIN_READDMA:
76 case WIN_READDMA_EXT:
77 case WIN_IDENTIFY_DMA:
78 return 1;
79 }
80
81 return 0;
82}
83
84static ide_startstop_t task_no_data_intr(ide_drive_t *); 55static ide_startstop_t task_no_data_intr(ide_drive_t *);
85static ide_startstop_t set_geometry_intr(ide_drive_t *); 56static ide_startstop_t set_geometry_intr(ide_drive_t *);
86static ide_startstop_t recal_intr(ide_drive_t *); 57static ide_startstop_t recal_intr(ide_drive_t *);
@@ -139,8 +110,7 @@ ide_startstop_t do_rw_taskfile (ide_drive_t *drive, ide_task_t *task)
139 WAIT_WORSTCASE, NULL); 110 WAIT_WORSTCASE, NULL);
140 return ide_started; 111 return ide_started;
141 default: 112 default:
142 if (task_dma_ok(task) == 0 || drive->using_dma == 0 || 113 if (drive->using_dma == 0 || dma_ops->dma_setup(drive))
143 dma_ops->dma_setup(drive))
144 return ide_stopped; 114 return ide_stopped;
145 dma_ops->dma_exec_cmd(drive, tf->command); 115 dma_ops->dma_exec_cmd(drive, tf->command);
146 dma_ops->dma_start(drive); 116 dma_ops->dma_start(drive);
@@ -183,7 +153,6 @@ static ide_startstop_t set_geometry_intr(ide_drive_t *drive)
183 if (stat & (ERR_STAT|DRQ_STAT)) 153 if (stat & (ERR_STAT|DRQ_STAT))
184 return ide_error(drive, "set_geometry_intr", stat); 154 return ide_error(drive, "set_geometry_intr", stat);
185 155
186 BUG_ON(HWGROUP(drive)->handler != NULL);
187 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL); 156 ide_set_handler(drive, &set_geometry_intr, WAIT_WORSTCASE, NULL);
188 return ide_started; 157 return ide_started;
189} 158}
diff --git a/drivers/ide/ide-timing.h b/drivers/ide/ide-timings.c
index 2e91c5870b4c..8c2f8327f487 100644
--- a/drivers/ide/ide-timing.h
+++ b/drivers/ide/ide-timings.c
@@ -1,11 +1,7 @@
1#ifndef _IDE_TIMING_H
2#define _IDE_TIMING_H
3
4/* 1/*
5 * Copyright (c) 1999-2001 Vojtech Pavlik 2 * Copyright (c) 1999-2001 Vojtech Pavlik
6 */ 3 * Copyright (c) 2007-2008 Bartlomiej Zolnierkiewicz
7 4 *
8/*
9 * This program is free software; you can redistribute it and/or modify 5 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or 7 * the Free Software Foundation; either version 2 of the License, or
@@ -27,27 +23,14 @@
27 23
28#include <linux/kernel.h> 24#include <linux/kernel.h>
29#include <linux/hdreg.h> 25#include <linux/hdreg.h>
30 26#include <linux/ide.h>
31#define XFER_PIO_5 0x0d 27#include <linux/module.h>
32#define XFER_UDMA_SLOW 0x4f
33
34struct ide_timing {
35 short mode;
36 short setup; /* t1 */
37 short act8b; /* t2 for 8-bit io */
38 short rec8b; /* t2i for 8-bit io */
39 short cyc8b; /* t0 for 8-bit io */
40 short active; /* t2 or tD */
41 short recover; /* t2i or tK */
42 short cycle; /* t0 */
43 short udma; /* t2CYCTYP/2 */
44};
45 28
46/* 29/*
47 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds). 30 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
48 * These were taken from ATA/ATAPI-6 standard, rev 0a, except 31 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
49 * for PIO 5, which is a nonstandard extension and UDMA6, which 32 * for PIO 5, which is a nonstandard extension and UDMA6, which
50 * is currently supported only by Maxtor drives. 33 * is currently supported only by Maxtor drives.
51 */ 34 */
52 35
53static struct ide_timing ide_timing[] = { 36static struct ide_timing ide_timing[] = {
@@ -61,12 +44,10 @@ static struct ide_timing ide_timing[] = {
61 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 }, 44 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
62 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 }, 45 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
63 46
64 { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 },
65
66 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 }, 47 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
67 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 }, 48 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
68 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 }, 49 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
69 50
70 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 }, 51 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
71 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 }, 52 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
72 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 }, 53 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
@@ -81,29 +62,46 @@ static struct ide_timing ide_timing[] = {
81 62
82 { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, 63 { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 },
83 64
84 { -1 } 65 { 0xff }
85}; 66};
86 67
87#define IDE_TIMING_SETUP 0x01 68struct ide_timing *ide_timing_find_mode(u8 speed)
88#define IDE_TIMING_ACT8B 0x02 69{
89#define IDE_TIMING_REC8B 0x04 70 struct ide_timing *t;
90#define IDE_TIMING_CYC8B 0x08 71
91#define IDE_TIMING_8BIT 0x0e 72 for (t = ide_timing; t->mode != speed; t++)
92#define IDE_TIMING_ACTIVE 0x10 73 if (t->mode == 0xff)
93#define IDE_TIMING_RECOVER 0x20 74 return NULL;
94#define IDE_TIMING_CYCLE 0x40 75 return t;
95#define IDE_TIMING_UDMA 0x80 76}
96#define IDE_TIMING_ALL 0xff 77EXPORT_SYMBOL_GPL(ide_timing_find_mode);
97 78
98#define ENOUGH(v,unit) (((v)-1)/(unit)+1) 79u16 ide_pio_cycle_time(ide_drive_t *drive, u8 pio)
99#define EZ(v,unit) ((v)?ENOUGH(v,unit):0) 80{
100 81 struct hd_driveid *id = drive->id;
101#define XFER_MODE 0xf0 82 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
102#define XFER_MWDMA 0x20 83 u16 cycle = 0;
103#define XFER_EPIO 0x01 84
104#define XFER_PIO 0x00 85 if (id->field_valid & 2) {
105 86 if (id->capability & 8)
106static void ide_timing_quantize(struct ide_timing *t, struct ide_timing *q, int T, int UT) 87 cycle = id->eide_pio_iordy;
88 else
89 cycle = id->eide_pio;
90
91 /* conservative "downgrade" for all pre-ATA2 drives */
92 if (pio < 3 && cycle < t->cycle)
93 cycle = 0; /* use standard timing */
94 }
95
96 return cycle ? cycle : t->cycle;
97}
98EXPORT_SYMBOL_GPL(ide_pio_cycle_time);
99
100#define ENOUGH(v, unit) (((v) - 1) / (unit) + 1)
101#define EZ(v, unit) ((v) ? ENOUGH(v, unit) : 0)
102
103static void ide_timing_quantize(struct ide_timing *t, struct ide_timing *q,
104 int T, int UT)
107{ 105{
108 q->setup = EZ(t->setup * 1000, T); 106 q->setup = EZ(t->setup * 1000, T);
109 q->act8b = EZ(t->act8b * 1000, T); 107 q->act8b = EZ(t->act8b * 1000, T);
@@ -115,92 +113,83 @@ static void ide_timing_quantize(struct ide_timing *t, struct ide_timing *q, int
115 q->udma = EZ(t->udma * 1000, UT); 113 q->udma = EZ(t->udma * 1000, UT);
116} 114}
117 115
118static void ide_timing_merge(struct ide_timing *a, struct ide_timing *b, struct ide_timing *m, unsigned int what) 116void ide_timing_merge(struct ide_timing *a, struct ide_timing *b,
119{ 117 struct ide_timing *m, unsigned int what)
120 if (what & IDE_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
121 if (what & IDE_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
122 if (what & IDE_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
123 if (what & IDE_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
124 if (what & IDE_TIMING_ACTIVE ) m->active = max(a->active, b->active);
125 if (what & IDE_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
126 if (what & IDE_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
127 if (what & IDE_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
128}
129
130static struct ide_timing* ide_timing_find_mode(short speed)
131{ 118{
132 struct ide_timing *t; 119 if (what & IDE_TIMING_SETUP)
133 120 m->setup = max(a->setup, b->setup);
134 for (t = ide_timing; t->mode != speed; t++) 121 if (what & IDE_TIMING_ACT8B)
135 if (t->mode < 0) 122 m->act8b = max(a->act8b, b->act8b);
136 return NULL; 123 if (what & IDE_TIMING_REC8B)
137 return t; 124 m->rec8b = max(a->rec8b, b->rec8b);
125 if (what & IDE_TIMING_CYC8B)
126 m->cyc8b = max(a->cyc8b, b->cyc8b);
127 if (what & IDE_TIMING_ACTIVE)
128 m->active = max(a->active, b->active);
129 if (what & IDE_TIMING_RECOVER)
130 m->recover = max(a->recover, b->recover);
131 if (what & IDE_TIMING_CYCLE)
132 m->cycle = max(a->cycle, b->cycle);
133 if (what & IDE_TIMING_UDMA)
134 m->udma = max(a->udma, b->udma);
138} 135}
136EXPORT_SYMBOL_GPL(ide_timing_merge);
139 137
140static int ide_timing_compute(ide_drive_t *drive, short speed, struct ide_timing *t, int T, int UT) 138int ide_timing_compute(ide_drive_t *drive, u8 speed,
139 struct ide_timing *t, int T, int UT)
141{ 140{
142 struct hd_driveid *id = drive->id; 141 struct hd_driveid *id = drive->id;
143 struct ide_timing *s, p; 142 struct ide_timing *s, p;
144 143
145/* 144 /*
146 * Find the mode. 145 * Find the mode.
147 */ 146 */
148 147 s = ide_timing_find_mode(speed);
149 if (!(s = ide_timing_find_mode(speed))) 148 if (s == NULL)
150 return -EINVAL; 149 return -EINVAL;
151 150
152/* 151 /*
153 * Copy the timing from the table. 152 * Copy the timing from the table.
154 */ 153 */
155
156 *t = *s; 154 *t = *s;
157 155
158/* 156 /*
159 * If the drive is an EIDE drive, it can tell us it needs extended 157 * If the drive is an EIDE drive, it can tell us it needs extended
160 * PIO/MWDMA cycle timing. 158 * PIO/MWDMA cycle timing.
161 */ 159 */
162
163 if (id && id->field_valid & 2) { /* EIDE drive */ 160 if (id && id->field_valid & 2) { /* EIDE drive */
164 161
165 memset(&p, 0, sizeof(p)); 162 memset(&p, 0, sizeof(p));
166 163
167 switch (speed & XFER_MODE) { 164 if (speed <= XFER_PIO_2)
168 165 p.cycle = p.cyc8b = id->eide_pio;
169 case XFER_PIO: 166 else if (speed <= XFER_PIO_5)
170 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = id->eide_pio; 167 p.cycle = p.cyc8b = id->eide_pio_iordy;
171 else p.cycle = p.cyc8b = id->eide_pio_iordy; 168 else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
172 break; 169 p.cycle = id->eide_dma_min;
173
174 case XFER_MWDMA:
175 p.cycle = id->eide_dma_min;
176 break;
177 }
178 170
179 ide_timing_merge(&p, t, t, IDE_TIMING_CYCLE | IDE_TIMING_CYC8B); 171 ide_timing_merge(&p, t, t, IDE_TIMING_CYCLE | IDE_TIMING_CYC8B);
180 } 172 }
181 173
182/* 174 /*
183 * Convert the timing to bus clock counts. 175 * Convert the timing to bus clock counts.
184 */ 176 */
185
186 ide_timing_quantize(t, t, T, UT); 177 ide_timing_quantize(t, t, T, UT);
187 178
188/* 179 /*
189 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T 180 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
190 * and some other commands. We have to ensure that the DMA cycle timing is 181 * S.M.A.R.T and some other commands. We have to ensure that the
191 * slower/equal than the fastest PIO timing. 182 * DMA cycle timing is slower/equal than the fastest PIO timing.
192 */ 183 */
193 184 if (speed >= XFER_SW_DMA_0) {
194 if ((speed & XFER_MODE) != XFER_PIO) {
195 u8 pio = ide_get_best_pio_mode(drive, 255, 5); 185 u8 pio = ide_get_best_pio_mode(drive, 255, 5);
196 ide_timing_compute(drive, XFER_PIO_0 + pio, &p, T, UT); 186 ide_timing_compute(drive, XFER_PIO_0 + pio, &p, T, UT);
197 ide_timing_merge(&p, t, t, IDE_TIMING_ALL); 187 ide_timing_merge(&p, t, t, IDE_TIMING_ALL);
198 } 188 }
199 189
200/* 190 /*
201 * Lengthen active & recovery time so that cycle time is correct. 191 * Lengthen active & recovery time so that cycle time is correct.
202 */ 192 */
203
204 if (t->act8b + t->rec8b < t->cyc8b) { 193 if (t->act8b + t->rec8b < t->cyc8b) {
205 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2; 194 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
206 t->rec8b = t->cyc8b - t->act8b; 195 t->rec8b = t->cyc8b - t->act8b;
@@ -213,5 +202,4 @@ static int ide_timing_compute(ide_drive_t *drive, short speed, struct ide_timing
213 202
214 return 0; 203 return 0;
215} 204}
216 205EXPORT_SYMBOL_GPL(ide_timing_compute);
217#endif
diff --git a/drivers/ide/ide.c b/drivers/ide/ide.c
index 2b8453510e09..d4a6b102a772 100644
--- a/drivers/ide/ide.c
+++ b/drivers/ide/ide.c
@@ -50,29 +50,16 @@
50#include <linux/types.h> 50#include <linux/types.h>
51#include <linux/string.h> 51#include <linux/string.h>
52#include <linux/kernel.h> 52#include <linux/kernel.h>
53#include <linux/timer.h>
54#include <linux/mm.h>
55#include <linux/interrupt.h> 53#include <linux/interrupt.h>
56#include <linux/major.h> 54#include <linux/major.h>
57#include <linux/errno.h> 55#include <linux/errno.h>
58#include <linux/genhd.h> 56#include <linux/genhd.h>
59#include <linux/blkpg.h>
60#include <linux/slab.h> 57#include <linux/slab.h>
61#include <linux/init.h> 58#include <linux/init.h>
62#include <linux/pci.h> 59#include <linux/pci.h>
63#include <linux/delay.h>
64#include <linux/ide.h> 60#include <linux/ide.h>
65#include <linux/completion.h> 61#include <linux/completion.h>
66#include <linux/reboot.h>
67#include <linux/cdrom.h>
68#include <linux/seq_file.h>
69#include <linux/device.h> 62#include <linux/device.h>
70#include <linux/bitops.h>
71
72#include <asm/byteorder.h>
73#include <asm/irq.h>
74#include <asm/uaccess.h>
75#include <asm/io.h>
76 63
77 64
78/* default maximum number of failures */ 65/* default maximum number of failures */
@@ -91,8 +78,6 @@ DEFINE_MUTEX(ide_cfg_mtx);
91__cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock); 78__cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
92EXPORT_SYMBOL(ide_lock); 79EXPORT_SYMBOL(ide_lock);
93 80
94ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
95
96static void ide_port_init_devices_data(ide_hwif_t *); 81static void ide_port_init_devices_data(ide_hwif_t *);
97 82
98/* 83/*
@@ -121,7 +106,6 @@ void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
121 106
122 ide_port_init_devices_data(hwif); 107 ide_port_init_devices_data(hwif);
123} 108}
124EXPORT_SYMBOL_GPL(ide_init_port_data);
125 109
126static void ide_port_init_devices_data(ide_hwif_t *hwif) 110static void ide_port_init_devices_data(ide_hwif_t *hwif)
127{ 111{
@@ -150,18 +134,6 @@ static void ide_port_init_devices_data(ide_hwif_t *hwif)
150 } 134 }
151} 135}
152 136
153static void __init init_ide_data (void)
154{
155 unsigned int index;
156
157 /* Initialise all interface structures */
158 for (index = 0; index < MAX_HWIFS; ++index) {
159 ide_hwif_t *hwif = &ide_hwifs[index];
160
161 ide_init_port_data(hwif, index);
162 }
163}
164
165void ide_remove_port_from_hwgroup(ide_hwif_t *hwif) 137void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
166{ 138{
167 ide_hwgroup_t *hwgroup = hwif->hwgroup; 139 ide_hwgroup_t *hwgroup = hwif->hwgroup;
@@ -312,7 +284,8 @@ void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
312 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports)); 284 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
313 hwif->irq = hw->irq; 285 hwif->irq = hw->irq;
314 hwif->chipset = hw->chipset; 286 hwif->chipset = hw->chipset;
315 hwif->gendev.parent = hw->dev; 287 hwif->dev = hw->dev;
288 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
316 hwif->ack_intr = hw->ack_intr; 289 hwif->ack_intr = hw->ack_intr;
317} 290}
318EXPORT_SYMBOL_GPL(ide_init_port_hw); 291EXPORT_SYMBOL_GPL(ide_init_port_hw);
@@ -556,6 +529,22 @@ static int generic_ide_resume(struct device *dev)
556 return err; 529 return err;
557} 530}
558 531
532static int generic_drive_reset(ide_drive_t *drive)
533{
534 struct request *rq;
535 int ret = 0;
536
537 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
538 rq->cmd_type = REQ_TYPE_SPECIAL;
539 rq->cmd_len = 1;
540 rq->cmd[0] = REQ_DRIVE_RESET;
541 rq->cmd_flags |= REQ_SOFTBARRIER;
542 if (blk_execute_rq(drive->queue, NULL, rq, 1))
543 ret = rq->errors;
544 blk_put_request(rq);
545 return ret;
546}
547
559int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev, 548int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
560 unsigned int cmd, unsigned long arg) 549 unsigned int cmd, unsigned long arg)
561{ 550{
@@ -630,33 +619,8 @@ int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device
630 if (!capable(CAP_SYS_ADMIN)) 619 if (!capable(CAP_SYS_ADMIN))
631 return -EACCES; 620 return -EACCES;
632 621
633 /* 622 return generic_drive_reset(drive);
634 * Abort the current command on the
635 * group if there is one, taking
636 * care not to allow anything else
637 * to be queued and to die on the
638 * spot if we miss one somehow
639 */
640
641 spin_lock_irqsave(&ide_lock, flags);
642
643 if (HWGROUP(drive)->resetting) {
644 spin_unlock_irqrestore(&ide_lock, flags);
645 return -EBUSY;
646 }
647 623
648 ide_abort(drive, "drive reset");
649
650 BUG_ON(HWGROUP(drive)->handler);
651
652 /* Ensure nothing gets queued after we
653 drop the lock. Reset will clear the busy */
654
655 HWGROUP(drive)->busy = 1;
656 spin_unlock_irqrestore(&ide_lock, flags);
657 (void) ide_do_reset(drive);
658
659 return 0;
660 case HDIO_GET_BUSSTATE: 624 case HDIO_GET_BUSSTATE:
661 if (!capable(CAP_SYS_ADMIN)) 625 if (!capable(CAP_SYS_ADMIN))
662 return -EACCES; 626 return -EACCES;
@@ -1021,8 +985,6 @@ static int __init ide_init(void)
1021 goto out_port_class; 985 goto out_port_class;
1022 } 986 }
1023 987
1024 init_ide_data();
1025
1026 proc_ide_create(); 988 proc_ide_create();
1027 989
1028 return 0; 990 return 0;
diff --git a/drivers/ide/legacy/ali14xx.c b/drivers/ide/legacy/ali14xx.c
index 052125fafcfa..4ec19737f3c5 100644
--- a/drivers/ide/legacy/ali14xx.c
+++ b/drivers/ide/legacy/ali14xx.c
@@ -117,10 +117,11 @@ static void ali14xx_set_pio_mode(ide_drive_t *drive, const u8 pio)
117 u8 param1, param2, param3, param4; 117 u8 param1, param2, param3, param4;
118 unsigned long flags; 118 unsigned long flags;
119 int bus_speed = ide_vlb_clk ? ide_vlb_clk : 50; 119 int bus_speed = ide_vlb_clk ? ide_vlb_clk : 50;
120 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
120 121
121 /* calculate timing, according to PIO mode */ 122 /* calculate timing, according to PIO mode */
122 time1 = ide_pio_cycle_time(drive, pio); 123 time1 = ide_pio_cycle_time(drive, pio);
123 time2 = ide_pio_timings[pio].active_time; 124 time2 = t->active;
124 param3 = param1 = (time2 * bus_speed + 999) / 1000; 125 param3 = param1 = (time2 * bus_speed + 999) / 1000;
125 param4 = param2 = (time1 * bus_speed + 999) / 1000 - param1; 126 param4 = param2 = (time1 * bus_speed + 999) / 1000 - param1;
126 if (pio < 3) { 127 if (pio < 3) {
diff --git a/drivers/ide/legacy/buddha.c b/drivers/ide/legacy/buddha.c
index 9a1d27ef3f8a..0497e7f85b09 100644
--- a/drivers/ide/legacy/buddha.c
+++ b/drivers/ide/legacy/buddha.c
@@ -227,7 +227,6 @@ fail_base2:
227 if (hwif) { 227 if (hwif) {
228 u8 index = hwif->index; 228 u8 index = hwif->index;
229 229
230 ide_init_port_data(hwif, index);
231 ide_init_port_hw(hwif, &hw); 230 ide_init_port_hw(hwif, &hw);
232 231
233 idx[i] = index; 232 idx[i] = index;
diff --git a/drivers/ide/legacy/falconide.c b/drivers/ide/legacy/falconide.c
index af11028b4794..129a812bb57f 100644
--- a/drivers/ide/legacy/falconide.c
+++ b/drivers/ide/legacy/falconide.c
@@ -111,7 +111,6 @@ static int __init falconide_init(void)
111 u8 index = hwif->index; 111 u8 index = hwif->index;
112 u8 idx[4] = { index, 0xff, 0xff, 0xff }; 112 u8 idx[4] = { index, 0xff, 0xff, 0xff };
113 113
114 ide_init_port_data(hwif, index);
115 ide_init_port_hw(hwif, &hw); 114 ide_init_port_hw(hwif, &hw);
116 115
117 /* Atari has a byte-swapped IDE interface */ 116 /* Atari has a byte-swapped IDE interface */
diff --git a/drivers/ide/legacy/gayle.c b/drivers/ide/legacy/gayle.c
index b78941680c32..7e74b20202df 100644
--- a/drivers/ide/legacy/gayle.c
+++ b/drivers/ide/legacy/gayle.c
@@ -185,7 +185,6 @@ found:
185 if (hwif) { 185 if (hwif) {
186 u8 index = hwif->index; 186 u8 index = hwif->index;
187 187
188 ide_init_port_data(hwif, index);
189 ide_init_port_hw(hwif, &hw); 188 ide_init_port_hw(hwif, &hw);
190 189
191 idx[i] = index; 190 idx[i] = index;
diff --git a/drivers/ide/legacy/ht6560b.c b/drivers/ide/legacy/ht6560b.c
index dd6dfb32e853..7bc8fd59ea9e 100644
--- a/drivers/ide/legacy/ht6560b.c
+++ b/drivers/ide/legacy/ht6560b.c
@@ -216,6 +216,7 @@ static u8 ht_pio2timings(ide_drive_t *drive, const u8 pio)
216 216
217 if (pio) { 217 if (pio) {
218 unsigned int cycle_time; 218 unsigned int cycle_time;
219 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
219 220
220 cycle_time = ide_pio_cycle_time(drive, pio); 221 cycle_time = ide_pio_cycle_time(drive, pio);
221 222
@@ -224,10 +225,8 @@ static u8 ht_pio2timings(ide_drive_t *drive, const u8 pio)
224 * actual cycle time for recovery and activity 225 * actual cycle time for recovery and activity
225 * according system bus speed. 226 * according system bus speed.
226 */ 227 */
227 active_time = ide_pio_timings[pio].active_time; 228 active_time = t->active;
228 recovery_time = cycle_time 229 recovery_time = cycle_time - active_time - t->setup;
229 - active_time
230 - ide_pio_timings[pio].setup_time;
231 /* 230 /*
232 * Cycle times should be Vesa bus cycles 231 * Cycle times should be Vesa bus cycles
233 */ 232 */
@@ -311,16 +310,16 @@ static void ht6560b_set_pio_mode(ide_drive_t *drive, const u8 pio)
311#endif 310#endif
312} 311}
313 312
314static void __init ht6560b_port_init_devs(ide_hwif_t *hwif) 313static void __init ht6560b_init_dev(ide_drive_t *drive)
315{ 314{
315 ide_hwif_t *hwif = drive->hwif;
316 /* Setting default configurations for drives. */ 316 /* Setting default configurations for drives. */
317 int t = (HT_CONFIG_DEFAULT << 8) | HT_TIMING_DEFAULT; 317 int t = (HT_CONFIG_DEFAULT << 8) | HT_TIMING_DEFAULT;
318 318
319 if (hwif->channel) 319 if (hwif->channel)
320 t |= (HT_SECONDARY_IF << 8); 320 t |= (HT_SECONDARY_IF << 8);
321 321
322 hwif->drives[0].drive_data = t; 322 drive->drive_data = t;
323 hwif->drives[1].drive_data = t;
324} 323}
325 324
326static int probe_ht6560b; 325static int probe_ht6560b;
@@ -329,7 +328,7 @@ module_param_named(probe, probe_ht6560b, bool, 0);
329MODULE_PARM_DESC(probe, "probe for HT6560B chipset"); 328MODULE_PARM_DESC(probe, "probe for HT6560B chipset");
330 329
331static const struct ide_port_ops ht6560b_port_ops = { 330static const struct ide_port_ops ht6560b_port_ops = {
332 .port_init_devs = ht6560b_port_init_devs, 331 .init_dev = ht6560b_init_dev,
333 .set_pio_mode = ht6560b_set_pio_mode, 332 .set_pio_mode = ht6560b_set_pio_mode,
334 .selectproc = ht6560b_selectproc, 333 .selectproc = ht6560b_selectproc,
335}; 334};
diff --git a/drivers/ide/legacy/ide-4drives.c b/drivers/ide/legacy/ide-4drives.c
index ecae916a3385..89c8ff0a4d08 100644
--- a/drivers/ide/legacy/ide-4drives.c
+++ b/drivers/ide/legacy/ide-4drives.c
@@ -11,6 +11,21 @@ static int probe_4drives;
11module_param_named(probe, probe_4drives, bool, 0); 11module_param_named(probe, probe_4drives, bool, 0);
12MODULE_PARM_DESC(probe, "probe for generic IDE chipset with 4 drives/port"); 12MODULE_PARM_DESC(probe, "probe for generic IDE chipset with 4 drives/port");
13 13
14static void ide_4drives_init_dev(ide_drive_t *drive)
15{
16 if (drive->hwif->channel)
17 drive->select.all ^= 0x20;
18}
19
20static const struct ide_port_ops ide_4drives_port_ops = {
21 .init_dev = ide_4drives_init_dev,
22};
23
24static const struct ide_port_info ide_4drives_port_info = {
25 .port_ops = &ide_4drives_port_ops,
26 .host_flags = IDE_HFLAG_SERIALIZE | IDE_HFLAG_NO_DMA,
27};
28
14static int __init ide_4drives_init(void) 29static int __init ide_4drives_init(void)
15{ 30{
16 ide_hwif_t *hwif, *mate; 31 ide_hwif_t *hwif, *mate;
@@ -49,18 +64,10 @@ static int __init ide_4drives_init(void)
49 mate = ide_find_port(); 64 mate = ide_find_port();
50 if (mate) { 65 if (mate) {
51 ide_init_port_hw(mate, &hw); 66 ide_init_port_hw(mate, &hw);
52 mate->drives[0].select.all ^= 0x20;
53 mate->drives[1].select.all ^= 0x20;
54 idx[1] = mate->index; 67 idx[1] = mate->index;
55
56 if (hwif) {
57 hwif->mate = mate;
58 mate->mate = hwif;
59 hwif->serialized = mate->serialized = 1;
60 }
61 } 68 }
62 69
63 ide_device_add(idx, NULL); 70 ide_device_add(idx, &ide_4drives_port_info);
64 71
65 return 0; 72 return 0;
66} 73}
diff --git a/drivers/ide/legacy/ide-cs.c b/drivers/ide/legacy/ide-cs.c
index 8dbf4d9b6447..fc53dcfbfe38 100644
--- a/drivers/ide/legacy/ide-cs.c
+++ b/drivers/ide/legacy/ide-cs.c
@@ -154,6 +154,11 @@ static const struct ide_port_ops idecs_port_ops = {
154 .quirkproc = ide_undecoded_slave, 154 .quirkproc = ide_undecoded_slave,
155}; 155};
156 156
157static const struct ide_port_info idecs_port_info = {
158 .port_ops = &idecs_port_ops,
159 .host_flags = IDE_HFLAG_NO_DMA,
160};
161
157static ide_hwif_t *idecs_register(unsigned long io, unsigned long ctl, 162static ide_hwif_t *idecs_register(unsigned long io, unsigned long ctl,
158 unsigned long irq, struct pcmcia_device *handle) 163 unsigned long irq, struct pcmcia_device *handle)
159{ 164{
@@ -187,13 +192,11 @@ static ide_hwif_t *idecs_register(unsigned long io, unsigned long ctl,
187 192
188 i = hwif->index; 193 i = hwif->index;
189 194
190 ide_init_port_data(hwif, i);
191 ide_init_port_hw(hwif, &hw); 195 ide_init_port_hw(hwif, &hw);
192 hwif->port_ops = &idecs_port_ops;
193 196
194 idx[0] = i; 197 idx[0] = i;
195 198
196 ide_device_add(idx, NULL); 199 ide_device_add(idx, &idecs_port_info);
197 200
198 if (hwif->present) 201 if (hwif->present)
199 return hwif; 202 return hwif;
diff --git a/drivers/ide/legacy/ide_platform.c b/drivers/ide/legacy/ide_platform.c
index d3bc3f24e05d..a249562b34b5 100644
--- a/drivers/ide/legacy/ide_platform.c
+++ b/drivers/ide/legacy/ide_platform.c
@@ -44,6 +44,10 @@ static void __devinit plat_ide_setup_ports(hw_regs_t *hw,
44 hw->chipset = ide_generic; 44 hw->chipset = ide_generic;
45} 45}
46 46
47static const struct ide_port_info platform_ide_port_info = {
48 .host_flags = IDE_HFLAG_NO_DMA,
49};
50
47static int __devinit plat_ide_probe(struct platform_device *pdev) 51static int __devinit plat_ide_probe(struct platform_device *pdev)
48{ 52{
49 struct resource *res_base, *res_alt, *res_irq; 53 struct resource *res_base, *res_alt, *res_irq;
@@ -54,6 +58,7 @@ static int __devinit plat_ide_probe(struct platform_device *pdev)
54 int ret = 0; 58 int ret = 0;
55 int mmio = 0; 59 int mmio = 0;
56 hw_regs_t hw; 60 hw_regs_t hw;
61 struct ide_port_info d = platform_ide_port_info;
57 62
58 pdata = pdev->dev.platform_data; 63 pdata = pdev->dev.platform_data;
59 64
@@ -102,13 +107,13 @@ static int __devinit plat_ide_probe(struct platform_device *pdev)
102 ide_init_port_hw(hwif, &hw); 107 ide_init_port_hw(hwif, &hw);
103 108
104 if (mmio) { 109 if (mmio) {
105 hwif->host_flags = IDE_HFLAG_MMIO; 110 d.host_flags |= IDE_HFLAG_MMIO;
106 default_hwif_mmiops(hwif); 111 default_hwif_mmiops(hwif);
107 } 112 }
108 113
109 idx[0] = hwif->index; 114 idx[0] = hwif->index;
110 115
111 ide_device_add(idx, NULL); 116 ide_device_add(idx, &d);
112 117
113 platform_set_drvdata(pdev, hwif); 118 platform_set_drvdata(pdev, hwif);
114 119
diff --git a/drivers/ide/legacy/macide.c b/drivers/ide/legacy/macide.c
index 2e84290d0bcc..0a6195bcfeda 100644
--- a/drivers/ide/legacy/macide.c
+++ b/drivers/ide/legacy/macide.c
@@ -130,7 +130,6 @@ static int __init macide_init(void)
130 u8 index = hwif->index; 130 u8 index = hwif->index;
131 u8 idx[4] = { index, 0xff, 0xff, 0xff }; 131 u8 idx[4] = { index, 0xff, 0xff, 0xff };
132 132
133 ide_init_port_data(hwif, index);
134 ide_init_port_hw(hwif, &hw); 133 ide_init_port_hw(hwif, &hw);
135 134
136 ide_device_add(idx, NULL); 135 ide_device_add(idx, NULL);
diff --git a/drivers/ide/legacy/q40ide.c b/drivers/ide/legacy/q40ide.c
index 8ff6e2d20834..9c2b9d078f69 100644
--- a/drivers/ide/legacy/q40ide.c
+++ b/drivers/ide/legacy/q40ide.c
@@ -142,7 +142,6 @@ static int __init q40ide_init(void)
142 142
143 hwif = ide_find_port(); 143 hwif = ide_find_port();
144 if (hwif) { 144 if (hwif) {
145 ide_init_port_data(hwif, hwif->index);
146 ide_init_port_hw(hwif, &hw); 145 ide_init_port_hw(hwif, &hw);
147 146
148 /* Q40 has a byte-swapped IDE interface */ 147 /* Q40 has a byte-swapped IDE interface */
diff --git a/drivers/ide/legacy/qd65xx.c b/drivers/ide/legacy/qd65xx.c
index 51dba82f8812..2338f344ea24 100644
--- a/drivers/ide/legacy/qd65xx.c
+++ b/drivers/ide/legacy/qd65xx.c
@@ -207,6 +207,7 @@ static void qd6500_set_pio_mode(ide_drive_t *drive, const u8 pio)
207static void qd6580_set_pio_mode(ide_drive_t *drive, const u8 pio) 207static void qd6580_set_pio_mode(ide_drive_t *drive, const u8 pio)
208{ 208{
209 ide_hwif_t *hwif = drive->hwif; 209 ide_hwif_t *hwif = drive->hwif;
210 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
210 unsigned int cycle_time; 211 unsigned int cycle_time;
211 int active_time = 175; 212 int active_time = 175;
212 int recovery_time = 415; /* worst case values from the dos driver */ 213 int recovery_time = 415; /* worst case values from the dos driver */
@@ -236,7 +237,7 @@ static void qd6580_set_pio_mode(ide_drive_t *drive, const u8 pio)
236 active_time = 110; 237 active_time = 110;
237 recovery_time = cycle_time - 120; 238 recovery_time = cycle_time - 120;
238 } else { 239 } else {
239 active_time = ide_pio_timings[pio].active_time; 240 active_time = t->active;
240 recovery_time = cycle_time - active_time; 241 recovery_time = cycle_time - active_time;
241 } 242 }
242 } 243 }
@@ -281,17 +282,18 @@ static int __init qd_testreg(int port)
281 return (readreg != QD_TESTVAL); 282 return (readreg != QD_TESTVAL);
282} 283}
283 284
284static void __init qd6500_port_init_devs(ide_hwif_t *hwif) 285static void __init qd6500_init_dev(ide_drive_t *drive)
285{ 286{
287 ide_hwif_t *hwif = drive->hwif;
286 u8 base = (hwif->config_data & 0xff00) >> 8; 288 u8 base = (hwif->config_data & 0xff00) >> 8;
287 u8 config = QD_CONFIG(hwif); 289 u8 config = QD_CONFIG(hwif);
288 290
289 hwif->drives[0].drive_data = QD6500_DEF_DATA; 291 drive->drive_data = QD6500_DEF_DATA;
290 hwif->drives[1].drive_data = QD6500_DEF_DATA;
291} 292}
292 293
293static void __init qd6580_port_init_devs(ide_hwif_t *hwif) 294static void __init qd6580_init_dev(ide_drive_t *drive)
294{ 295{
296 ide_hwif_t *hwif = drive->hwif;
295 u16 t1, t2; 297 u16 t1, t2;
296 u8 base = (hwif->config_data & 0xff00) >> 8; 298 u8 base = (hwif->config_data & 0xff00) >> 8;
297 u8 config = QD_CONFIG(hwif); 299 u8 config = QD_CONFIG(hwif);
@@ -302,18 +304,17 @@ static void __init qd6580_port_init_devs(ide_hwif_t *hwif)
302 } else 304 } else
303 t2 = t1 = hwif->channel ? QD6580_DEF_DATA2 : QD6580_DEF_DATA; 305 t2 = t1 = hwif->channel ? QD6580_DEF_DATA2 : QD6580_DEF_DATA;
304 306
305 hwif->drives[0].drive_data = t1; 307 drive->drive_data = drive->select.b.unit ? t2 : t1;
306 hwif->drives[1].drive_data = t2;
307} 308}
308 309
309static const struct ide_port_ops qd6500_port_ops = { 310static const struct ide_port_ops qd6500_port_ops = {
310 .port_init_devs = qd6500_port_init_devs, 311 .init_dev = qd6500_init_dev,
311 .set_pio_mode = qd6500_set_pio_mode, 312 .set_pio_mode = qd6500_set_pio_mode,
312 .selectproc = qd65xx_select, 313 .selectproc = qd65xx_select,
313}; 314};
314 315
315static const struct ide_port_ops qd6580_port_ops = { 316static const struct ide_port_ops qd6580_port_ops = {
316 .port_init_devs = qd6580_port_init_devs, 317 .init_dev = qd6580_init_dev,
317 .set_pio_mode = qd6580_set_pio_mode, 318 .set_pio_mode = qd6580_set_pio_mode,
318 .selectproc = qd65xx_select, 319 .selectproc = qd65xx_select,
319}; 320};
diff --git a/drivers/ide/mips/au1xxx-ide.c b/drivers/ide/mips/au1xxx-ide.c
index 1a6c27b32498..48d57cae63c6 100644
--- a/drivers/ide/mips/au1xxx-ide.c
+++ b/drivers/ide/mips/au1xxx-ide.c
@@ -213,10 +213,8 @@ static int auide_build_dmatable(ide_drive_t *drive)
213{ 213{
214 int i, iswrite, count = 0; 214 int i, iswrite, count = 0;
215 ide_hwif_t *hwif = HWIF(drive); 215 ide_hwif_t *hwif = HWIF(drive);
216
217 struct request *rq = HWGROUP(drive)->rq; 216 struct request *rq = HWGROUP(drive)->rq;
218 217 _auide_hwif *ahwif = &auide_hwif;
219 _auide_hwif *ahwif = (_auide_hwif*)hwif->hwif_data;
220 struct scatterlist *sg; 218 struct scatterlist *sg;
221 219
222 iswrite = (rq_data_dir(rq) == WRITE); 220 iswrite = (rq_data_dir(rq) == WRITE);
@@ -402,7 +400,7 @@ static const struct ide_dma_ops au1xxx_dma_ops = {
402 400
403static int auide_ddma_init(ide_hwif_t *hwif, const struct ide_port_info *d) 401static int auide_ddma_init(ide_hwif_t *hwif, const struct ide_port_info *d)
404{ 402{
405 _auide_hwif *auide = (_auide_hwif *)hwif->hwif_data; 403 _auide_hwif *auide = &auide_hwif;
406 dbdev_tab_t source_dev_tab, target_dev_tab; 404 dbdev_tab_t source_dev_tab, target_dev_tab;
407 u32 dev_id, tsize, devwidth, flags; 405 u32 dev_id, tsize, devwidth, flags;
408 406
@@ -463,7 +461,7 @@ static int auide_ddma_init(ide_hwif_t *hwif, const struct ide_port_info *d)
463#else 461#else
464static int auide_ddma_init(ide_hwif_t *hwif, const struct ide_port_info *d) 462static int auide_ddma_init(ide_hwif_t *hwif, const struct ide_port_info *d)
465{ 463{
466 _auide_hwif *auide = (_auide_hwif *)hwif->hwif_data; 464 _auide_hwif *auide = &auide_hwif;
467 dbdev_tab_t source_dev_tab; 465 dbdev_tab_t source_dev_tab;
468 int flags; 466 int flags;
469 467
@@ -600,8 +598,6 @@ static int au_ide_probe(struct device *dev)
600 598
601 ide_init_port_hw(hwif, &hw); 599 ide_init_port_hw(hwif, &hw);
602 600
603 hwif->dev = dev;
604
605 /* If the user has selected DDMA assisted copies, 601 /* If the user has selected DDMA assisted copies,
606 then set up a few local I/O function entry points 602 then set up a few local I/O function entry points
607 */ 603 */
@@ -610,11 +606,8 @@ static int au_ide_probe(struct device *dev)
610 hwif->input_data = au1xxx_input_data; 606 hwif->input_data = au1xxx_input_data;
611 hwif->output_data = au1xxx_output_data; 607 hwif->output_data = au1xxx_output_data;
612#endif 608#endif
613 hwif->select_data = 0; /* no chipset-specific code */
614 hwif->config_data = 0; /* no chipset-specific code */
615 609
616 auide_hwif.hwif = hwif; 610 auide_hwif.hwif = hwif;
617 hwif->hwif_data = &auide_hwif;
618 611
619 idx[0] = hwif->index; 612 idx[0] = hwif->index;
620 613
diff --git a/drivers/ide/mips/swarm.c b/drivers/ide/mips/swarm.c
index 52fee3d2771a..9f1212cc4aed 100644
--- a/drivers/ide/mips/swarm.c
+++ b/drivers/ide/mips/swarm.c
@@ -61,6 +61,11 @@ static struct resource swarm_ide_resource = {
61 61
62static struct platform_device *swarm_ide_dev; 62static struct platform_device *swarm_ide_dev;
63 63
64static const struct ide_port_info swarm_port_info = {
65 .name = DRV_NAME,
66 .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
67};
68
64/* 69/*
65 * swarm_ide_probe - if the board header indicates the existence of 70 * swarm_ide_probe - if the board header indicates the existence of
66 * Generic Bus IDE, allocate a HWIF for it. 71 * Generic Bus IDE, allocate a HWIF for it.
@@ -77,12 +82,6 @@ static int __devinit swarm_ide_probe(struct device *dev)
77 if (!SIBYTE_HAVE_IDE) 82 if (!SIBYTE_HAVE_IDE)
78 return -ENODEV; 83 return -ENODEV;
79 84
80 hwif = ide_find_port();
81 if (hwif == NULL) {
82 printk(KERN_ERR DRV_NAME ": no free slot for interface\n");
83 return -ENOMEM;
84 }
85
86 base = ioremap(A_IO_EXT_BASE, 0x800); 85 base = ioremap(A_IO_EXT_BASE, 0x800);
87 offset = __raw_readq(base + R_IO_EXT_REG(R_IO_EXT_START_ADDR, IDE_CS)); 86 offset = __raw_readq(base + R_IO_EXT_REG(R_IO_EXT_START_ADDR, IDE_CS));
88 size = __raw_readq(base + R_IO_EXT_REG(R_IO_EXT_MULT_SIZE, IDE_CS)); 87 size = __raw_readq(base + R_IO_EXT_REG(R_IO_EXT_MULT_SIZE, IDE_CS));
@@ -109,10 +108,6 @@ static int __devinit swarm_ide_probe(struct device *dev)
109 108
110 base = ioremap(offset, size); 109 base = ioremap(offset, size);
111 110
112 /* Setup MMIO ops. */
113 hwif->host_flags = IDE_HFLAG_MMIO;
114 default_hwif_mmiops(hwif);
115
116 for (i = 0; i <= 7; i++) 111 for (i = 0; i <= 7; i++)
117 hw.io_ports_array[i] = 112 hw.io_ports_array[i] =
118 (unsigned long)(base + ((0x1f0 + i) << 5)); 113 (unsigned long)(base + ((0x1f0 + i) << 5));
@@ -121,15 +116,26 @@ static int __devinit swarm_ide_probe(struct device *dev)
121 hw.irq = K_INT_GB_IDE; 116 hw.irq = K_INT_GB_IDE;
122 hw.chipset = ide_generic; 117 hw.chipset = ide_generic;
123 118
119 hwif = ide_find_port_slot(&swarm_port_info);
120 if (hwif == NULL)
121 goto err;
122
124 ide_init_port_hw(hwif, &hw); 123 ide_init_port_hw(hwif, &hw);
125 124
125 /* Setup MMIO ops. */
126 default_hwif_mmiops(hwif);
127
126 idx[0] = hwif->index; 128 idx[0] = hwif->index;
127 129
128 ide_device_add(idx, NULL); 130 ide_device_add(idx, &swarm_port_info);
129 131
130 dev_set_drvdata(dev, hwif); 132 dev_set_drvdata(dev, hwif);
131 133
132 return 0; 134 return 0;
135err:
136 release_resource(&swarm_ide_resource);
137 iounmap(base);
138 return -ENOMEM;
133} 139}
134 140
135static struct device_driver swarm_ide_driver = { 141static struct device_driver swarm_ide_driver = {
diff --git a/drivers/ide/pci/alim15x3.c b/drivers/ide/pci/alim15x3.c
index f2de00adf147..80d19c0eb780 100644
--- a/drivers/ide/pci/alim15x3.c
+++ b/drivers/ide/pci/alim15x3.c
@@ -69,7 +69,8 @@ static void ali_set_pio_mode(ide_drive_t *drive, const u8 pio)
69{ 69{
70 ide_hwif_t *hwif = HWIF(drive); 70 ide_hwif_t *hwif = HWIF(drive);
71 struct pci_dev *dev = to_pci_dev(hwif->dev); 71 struct pci_dev *dev = to_pci_dev(hwif->dev);
72 int s_time, a_time, c_time; 72 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
73 int s_time = t->setup, a_time = t->active, c_time = t->cycle;
73 u8 s_clc, a_clc, r_clc; 74 u8 s_clc, a_clc, r_clc;
74 unsigned long flags; 75 unsigned long flags;
75 int bus_speed = ide_pci_clk ? ide_pci_clk : 33; 76 int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
@@ -78,13 +79,10 @@ static void ali_set_pio_mode(ide_drive_t *drive, const u8 pio)
78 u8 cd_dma_fifo = 0; 79 u8 cd_dma_fifo = 0;
79 int unit = drive->select.b.unit & 1; 80 int unit = drive->select.b.unit & 1;
80 81
81 s_time = ide_pio_timings[pio].setup_time;
82 a_time = ide_pio_timings[pio].active_time;
83 if ((s_clc = (s_time * bus_speed + 999) / 1000) >= 8) 82 if ((s_clc = (s_time * bus_speed + 999) / 1000) >= 8)
84 s_clc = 0; 83 s_clc = 0;
85 if ((a_clc = (a_time * bus_speed + 999) / 1000) >= 8) 84 if ((a_clc = (a_time * bus_speed + 999) / 1000) >= 8)
86 a_clc = 0; 85 a_clc = 0;
87 c_time = ide_pio_timings[pio].cycle_time;
88 86
89 if (!(r_clc = (c_time * bus_speed + 999) / 1000 - a_clc - s_clc)) { 87 if (!(r_clc = (c_time * bus_speed + 999) / 1000 - a_clc - s_clc)) {
90 r_clc = 1; 88 r_clc = 1;
diff --git a/drivers/ide/pci/amd74xx.c b/drivers/ide/pci/amd74xx.c
index ad222206a429..0bfcdd0e77b3 100644
--- a/drivers/ide/pci/amd74xx.c
+++ b/drivers/ide/pci/amd74xx.c
@@ -21,8 +21,6 @@
21#include <linux/init.h> 21#include <linux/init.h>
22#include <linux/ide.h> 22#include <linux/ide.h>
23 23
24#include "ide-timing.h"
25
26enum { 24enum {
27 AMD_IDE_CONFIG = 0x41, 25 AMD_IDE_CONFIG = 0x41,
28 AMD_CABLE_DETECT = 0x42, 26 AMD_CABLE_DETECT = 0x42,
diff --git a/drivers/ide/pci/cmd640.c b/drivers/ide/pci/cmd640.c
index cd1ba14984ab..1ad1e23e3105 100644
--- a/drivers/ide/pci/cmd640.c
+++ b/drivers/ide/pci/cmd640.c
@@ -521,6 +521,7 @@ static void program_drive_counts(ide_drive_t *drive, unsigned int index)
521static void cmd640_set_mode(ide_drive_t *drive, unsigned int index, 521static void cmd640_set_mode(ide_drive_t *drive, unsigned int index,
522 u8 pio_mode, unsigned int cycle_time) 522 u8 pio_mode, unsigned int cycle_time)
523{ 523{
524 struct ide_timing *t;
524 int setup_time, active_time, recovery_time, clock_time; 525 int setup_time, active_time, recovery_time, clock_time;
525 u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count; 526 u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
526 int bus_speed; 527 int bus_speed;
@@ -532,8 +533,11 @@ static void cmd640_set_mode(ide_drive_t *drive, unsigned int index,
532 533
533 if (pio_mode > 5) 534 if (pio_mode > 5)
534 pio_mode = 5; 535 pio_mode = 5;
535 setup_time = ide_pio_timings[pio_mode].setup_time; 536
536 active_time = ide_pio_timings[pio_mode].active_time; 537 t = ide_timing_find_mode(XFER_PIO_0 + pio_mode);
538 setup_time = t->setup;
539 active_time = t->active;
540
537 recovery_time = cycle_time - (setup_time + active_time); 541 recovery_time = cycle_time - (setup_time + active_time);
538 clock_time = 1000 / bus_speed; 542 clock_time = 1000 / bus_speed;
539 cycle_count = DIV_ROUND_UP(cycle_time, clock_time); 543 cycle_count = DIV_ROUND_UP(cycle_time, clock_time);
@@ -607,11 +611,40 @@ static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio)
607 611
608 display_clocks(index); 612 display_clocks(index);
609} 613}
614#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
615
616static void cmd640_init_dev(ide_drive_t *drive)
617{
618 unsigned int i = drive->hwif->channel * 2 + drive->select.b.unit;
619
620#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
621 /*
622 * Reset timing to the slowest speed and turn off prefetch.
623 * This way, the drive identify code has a better chance.
624 */
625 setup_counts[i] = 4; /* max possible */
626 active_counts[i] = 16; /* max possible */
627 recovery_counts[i] = 16; /* max possible */
628 program_drive_counts(drive, i);
629 set_prefetch_mode(drive, i, 0);
630 printk(KERN_INFO DRV_NAME ": drive%d timings/prefetch cleared\n", i);
631#else
632 /*
633 * Set the drive unmask flags to match the prefetch setting.
634 */
635 check_prefetch(drive, i);
636 printk(KERN_INFO DRV_NAME ": drive%d timings/prefetch(%s) preserved\n",
637 i, drive->no_io_32bit ? "off" : "on");
638#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
639}
640
610 641
611static const struct ide_port_ops cmd640_port_ops = { 642static const struct ide_port_ops cmd640_port_ops = {
643 .init_dev = cmd640_init_dev,
644#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
612 .set_pio_mode = cmd640_set_pio_mode, 645 .set_pio_mode = cmd640_set_pio_mode,
646#endif
613}; 647};
614#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
615 648
616static int pci_conf1(void) 649static int pci_conf1(void)
617{ 650{
@@ -654,10 +687,8 @@ static const struct ide_port_info cmd640_port_info __initdata = {
654 IDE_HFLAG_NO_DMA | 687 IDE_HFLAG_NO_DMA |
655 IDE_HFLAG_ABUSE_PREFETCH | 688 IDE_HFLAG_ABUSE_PREFETCH |
656 IDE_HFLAG_ABUSE_FAST_DEVSEL, 689 IDE_HFLAG_ABUSE_FAST_DEVSEL,
657#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
658 .port_ops = &cmd640_port_ops, 690 .port_ops = &cmd640_port_ops,
659 .pio_mask = ATA_PIO5, 691 .pio_mask = ATA_PIO5,
660#endif
661}; 692};
662 693
663static int cmd640x_init_one(unsigned long base, unsigned long ctl) 694static int cmd640x_init_one(unsigned long base, unsigned long ctl)
@@ -683,12 +714,8 @@ static int cmd640x_init_one(unsigned long base, unsigned long ctl)
683 */ 714 */
684static int __init cmd640x_init(void) 715static int __init cmd640x_init(void)
685{ 716{
686#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
687 int second_port_toggled = 0;
688#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
689 int second_port_cmd640 = 0, rc; 717 int second_port_cmd640 = 0, rc;
690 const char *bus_type, *port2; 718 const char *bus_type, *port2;
691 unsigned int index;
692 u8 b, cfr; 719 u8 b, cfr;
693 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff }; 720 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
694 hw_regs_t hw[2]; 721 hw_regs_t hw[2];
@@ -774,88 +801,44 @@ static int __init cmd640x_init(void)
774 put_cmd640_reg(CMDTIM, 0); 801 put_cmd640_reg(CMDTIM, 0);
775 put_cmd640_reg(BRST, 0x40); 802 put_cmd640_reg(BRST, 0x40);
776 803
777 cmd_hwif1 = ide_find_port(); 804 b = get_cmd640_reg(CNTRL);
778 805
779 /* 806 /*
780 * Try to enable the secondary interface, if not already enabled 807 * Try to enable the secondary interface, if not already enabled
781 */ 808 */
782 if (cmd_hwif1 && 809 if (secondary_port_responding()) {
783 cmd_hwif1->drives[0].noprobe && cmd_hwif1->drives[1].noprobe) { 810 if ((b & CNTRL_ENA_2ND)) {
784 port2 = "not probed"; 811 second_port_cmd640 = 1;
812 port2 = "okay";
813 } else if (cmd640_vlb) {
814 second_port_cmd640 = 1;
815 port2 = "alive";
816 } else
817 port2 = "not cmd640";
785 } else { 818 } else {
786 b = get_cmd640_reg(CNTRL); 819 put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */
787 if (secondary_port_responding()) { 820 if (secondary_port_responding()) {
788 if ((b & CNTRL_ENA_2ND)) { 821 second_port_cmd640 = 1;
789 second_port_cmd640 = 1; 822 port2 = "enabled";
790 port2 = "okay";
791 } else if (cmd640_vlb) {
792 second_port_cmd640 = 1;
793 port2 = "alive";
794 } else
795 port2 = "not cmd640";
796 } else { 823 } else {
797 put_cmd640_reg(CNTRL, b ^ CNTRL_ENA_2ND); /* toggle the bit */ 824 put_cmd640_reg(CNTRL, b); /* restore original setting */
798 if (secondary_port_responding()) { 825 port2 = "not responding";
799 second_port_cmd640 = 1;
800#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
801 second_port_toggled = 1;
802#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
803 port2 = "enabled";
804 } else {
805 put_cmd640_reg(CNTRL, b); /* restore original setting */
806 port2 = "not responding";
807 }
808 } 826 }
809 } 827 }
810 828
811 /* 829 /*
812 * Initialize data for secondary cmd640 port, if enabled 830 * Initialize data for secondary cmd640 port, if enabled
813 */ 831 */
814 if (second_port_cmd640 && cmd_hwif1) { 832 if (second_port_cmd640) {
815 ide_init_port_hw(cmd_hwif1, &hw[1]); 833 cmd_hwif1 = ide_find_port();
816 idx[1] = cmd_hwif1->index; 834 if (cmd_hwif1) {
835 ide_init_port_hw(cmd_hwif1, &hw[1]);
836 idx[1] = cmd_hwif1->index;
837 }
817 } 838 }
818 printk(KERN_INFO "cmd640: %sserialized, secondary interface %s\n", 839 printk(KERN_INFO "cmd640: %sserialized, secondary interface %s\n",
819 second_port_cmd640 ? "" : "not ", port2); 840 second_port_cmd640 ? "" : "not ", port2);
820 841
821 /*
822 * Establish initial timings/prefetch for all drives.
823 * Do not unnecessarily disturb any prior BIOS setup of these.
824 */
825 for (index = 0; index < (2 + (second_port_cmd640 << 1)); index++) {
826 ide_drive_t *drive;
827
828 if (index > 1) {
829 if (cmd_hwif1 == NULL)
830 continue;
831 drive = &cmd_hwif1->drives[index & 1];
832 } else {
833 if (cmd_hwif0 == NULL)
834 continue;
835 drive = &cmd_hwif0->drives[index & 1];
836 }
837
838#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
839 /*
840 * Reset timing to the slowest speed and turn off prefetch.
841 * This way, the drive identify code has a better chance.
842 */
843 setup_counts [index] = 4; /* max possible */
844 active_counts [index] = 16; /* max possible */
845 recovery_counts [index] = 16; /* max possible */
846 program_drive_counts(drive, index);
847 set_prefetch_mode(drive, index, 0);
848 printk("cmd640: drive%d timings/prefetch cleared\n", index);
849#else
850 /*
851 * Set the drive unmask flags to match the prefetch setting
852 */
853 check_prefetch(drive, index);
854 printk("cmd640: drive%d timings/prefetch(%s) preserved\n",
855 index, drive->no_io_32bit ? "off" : "on");
856#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
857 }
858
859#ifdef CMD640_DUMP_REGS 842#ifdef CMD640_DUMP_REGS
860 cmd640_dump_regs(); 843 cmd640_dump_regs();
861#endif 844#endif
diff --git a/drivers/ide/pci/cmd64x.c b/drivers/ide/pci/cmd64x.c
index ca4774aa27ee..cfa784bacf48 100644
--- a/drivers/ide/pci/cmd64x.c
+++ b/drivers/ide/pci/cmd64x.c
@@ -116,6 +116,7 @@ static void cmd64x_tune_pio(ide_drive_t *drive, const u8 pio)
116{ 116{
117 ide_hwif_t *hwif = HWIF(drive); 117 ide_hwif_t *hwif = HWIF(drive);
118 struct pci_dev *dev = to_pci_dev(hwif->dev); 118 struct pci_dev *dev = to_pci_dev(hwif->dev);
119 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
119 unsigned int cycle_time; 120 unsigned int cycle_time;
120 u8 setup_count, arttim = 0; 121 u8 setup_count, arttim = 0;
121 122
@@ -124,10 +125,9 @@ static void cmd64x_tune_pio(ide_drive_t *drive, const u8 pio)
124 125
125 cycle_time = ide_pio_cycle_time(drive, pio); 126 cycle_time = ide_pio_cycle_time(drive, pio);
126 127
127 program_cycle_times(drive, cycle_time, 128 program_cycle_times(drive, cycle_time, t->active);
128 ide_pio_timings[pio].active_time);
129 129
130 setup_count = quantize_timing(ide_pio_timings[pio].setup_time, 130 setup_count = quantize_timing(t->setup,
131 1000 / (ide_pci_clk ? ide_pci_clk : 33)); 131 1000 / (ide_pci_clk ? ide_pci_clk : 33));
132 132
133 /* 133 /*
diff --git a/drivers/ide/pci/cs5535.c b/drivers/ide/pci/cs5535.c
index 99fe91a191b8..dc97c48623f3 100644
--- a/drivers/ide/pci/cs5535.c
+++ b/drivers/ide/pci/cs5535.c
@@ -26,8 +26,6 @@
26#include <linux/pci.h> 26#include <linux/pci.h>
27#include <linux/ide.h> 27#include <linux/ide.h>
28 28
29#include "ide-timing.h"
30
31#define MSR_ATAC_BASE 0x51300000 29#define MSR_ATAC_BASE 0x51300000
32#define ATAC_GLD_MSR_CAP (MSR_ATAC_BASE+0) 30#define ATAC_GLD_MSR_CAP (MSR_ATAC_BASE+0)
33#define ATAC_GLD_MSR_CONFIG (MSR_ATAC_BASE+0x01) 31#define ATAC_GLD_MSR_CONFIG (MSR_ATAC_BASE+0x01)
@@ -75,13 +73,11 @@ static unsigned int cs5535_udma_timings[5] =
75 */ 73 */
76static void cs5535_set_speed(ide_drive_t *drive, const u8 speed) 74static void cs5535_set_speed(ide_drive_t *drive, const u8 speed)
77{ 75{
78
79 u32 reg = 0, dummy; 76 u32 reg = 0, dummy;
80 int unit = drive->select.b.unit; 77 int unit = drive->select.b.unit;
81 78
82
83 /* Set the PIO timings */ 79 /* Set the PIO timings */
84 if ((speed & XFER_MODE) == XFER_PIO) { 80 if (speed < XFER_SW_DMA_0) {
85 ide_drive_t *pair = ide_get_paired_drive(drive); 81 ide_drive_t *pair = ide_get_paired_drive(drive);
86 u8 cmd, pioa; 82 u8 cmd, pioa;
87 83
diff --git a/drivers/ide/pci/cy82c693.c b/drivers/ide/pci/cy82c693.c
index 8c534afcb6c8..e14ad5530fa4 100644
--- a/drivers/ide/pci/cy82c693.c
+++ b/drivers/ide/pci/cy82c693.c
@@ -133,6 +133,7 @@ static int calc_clk(int time, int bus_speed)
133 */ 133 */
134static void compute_clocks(u8 pio, pio_clocks_t *p_pclk) 134static void compute_clocks(u8 pio, pio_clocks_t *p_pclk)
135{ 135{
136 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
136 int clk1, clk2; 137 int clk1, clk2;
137 int bus_speed = ide_pci_clk ? ide_pci_clk : 33; 138 int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
138 139
@@ -141,15 +142,13 @@ static void compute_clocks(u8 pio, pio_clocks_t *p_pclk)
141 */ 142 */
142 143
143 /* let's calc the address setup time clocks */ 144 /* let's calc the address setup time clocks */
144 p_pclk->address_time = (u8)calc_clk(ide_pio_timings[pio].setup_time, bus_speed); 145 p_pclk->address_time = (u8)calc_clk(t->setup, bus_speed);
145 146
146 /* let's calc the active and recovery time clocks */ 147 /* let's calc the active and recovery time clocks */
147 clk1 = calc_clk(ide_pio_timings[pio].active_time, bus_speed); 148 clk1 = calc_clk(t->active, bus_speed);
148 149
149 /* calc recovery timing */ 150 /* calc recovery timing */
150 clk2 = ide_pio_timings[pio].cycle_time - 151 clk2 = t->cycle - t->active - t->setup;
151 ide_pio_timings[pio].active_time -
152 ide_pio_timings[pio].setup_time;
153 152
154 clk2 = calc_clk(clk2, bus_speed); 153 clk2 = calc_clk(clk2, bus_speed);
155 154
diff --git a/drivers/ide/pci/delkin_cb.c b/drivers/ide/pci/delkin_cb.c
index af0f30051d5a..0106e2a2df77 100644
--- a/drivers/ide/pci/delkin_cb.c
+++ b/drivers/ide/pci/delkin_cb.c
@@ -93,7 +93,6 @@ delkin_cb_probe (struct pci_dev *dev, const struct pci_device_id *id)
93 93
94 i = hwif->index; 94 i = hwif->index;
95 95
96 ide_init_port_data(hwif, i);
97 ide_init_port_hw(hwif, &hw); 96 ide_init_port_hw(hwif, &hw);
98 97
99 idx[0] = i; 98 idx[0] = i;
diff --git a/drivers/ide/pci/it821x.c b/drivers/ide/pci/it821x.c
index 6ab04115286b..cbf647202994 100644
--- a/drivers/ide/pci/it821x.c
+++ b/drivers/ide/pci/it821x.c
@@ -512,8 +512,14 @@ static void __devinit it821x_quirkproc(ide_drive_t *drive)
512} 512}
513 513
514static struct ide_dma_ops it821x_pass_through_dma_ops = { 514static struct ide_dma_ops it821x_pass_through_dma_ops = {
515 .dma_host_set = ide_dma_host_set,
516 .dma_setup = ide_dma_setup,
517 .dma_exec_cmd = ide_dma_exec_cmd,
515 .dma_start = it821x_dma_start, 518 .dma_start = it821x_dma_start,
516 .dma_end = it821x_dma_end, 519 .dma_end = it821x_dma_end,
520 .dma_test_irq = ide_dma_test_irq,
521 .dma_timeout = ide_dma_timeout,
522 .dma_lost_irq = ide_dma_lost_irq,
517}; 523};
518 524
519/** 525/**
diff --git a/drivers/ide/pci/scc_pata.c b/drivers/ide/pci/scc_pata.c
index 1584ebb6a185..789c66dfbde5 100644
--- a/drivers/ide/pci/scc_pata.c
+++ b/drivers/ide/pci/scc_pata.c
@@ -558,12 +558,9 @@ static int scc_ide_setup_pci_device(struct pci_dev *dev,
558 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff }; 558 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
559 int i; 559 int i;
560 560
561 hwif = ide_find_port(); 561 hwif = ide_find_port_slot(d);
562 if (hwif == NULL) { 562 if (hwif == NULL)
563 printk(KERN_ERR "%s: too many IDE interfaces, "
564 "no room in table\n", SCC_PATA_NAME);
565 return -ENOMEM; 563 return -ENOMEM;
566 }
567 564
568 memset(&hw, 0, sizeof(hw)); 565 memset(&hw, 0, sizeof(hw));
569 for (i = 0; i <= 8; i++) 566 for (i = 0; i <= 8; i++)
@@ -572,7 +569,6 @@ static int scc_ide_setup_pci_device(struct pci_dev *dev,
572 hw.dev = &dev->dev; 569 hw.dev = &dev->dev;
573 hw.chipset = ide_pci; 570 hw.chipset = ide_pci;
574 ide_init_port_hw(hwif, &hw); 571 ide_init_port_hw(hwif, &hw);
575 hwif->dev = &dev->dev;
576 572
577 idx[0] = hwif->index; 573 idx[0] = hwif->index;
578 574
diff --git a/drivers/ide/pci/sgiioc4.c b/drivers/ide/pci/sgiioc4.c
index 24513e3dcd6b..c79ff5b41088 100644
--- a/drivers/ide/pci/sgiioc4.c
+++ b/drivers/ide/pci/sgiioc4.c
@@ -568,6 +568,7 @@ static const struct ide_dma_ops sgiioc4_dma_ops = {
568}; 568};
569 569
570static const struct ide_port_info sgiioc4_port_info __devinitdata = { 570static const struct ide_port_info sgiioc4_port_info __devinitdata = {
571 .name = DRV_NAME,
571 .chipset = ide_pci, 572 .chipset = ide_pci,
572 .init_dma = ide_dma_sgiioc4, 573 .init_dma = ide_dma_sgiioc4,
573 .port_ops = &sgiioc4_port_ops, 574 .port_ops = &sgiioc4_port_ops,
@@ -587,13 +588,6 @@ sgiioc4_ide_setup_pci_device(struct pci_dev *dev)
587 hw_regs_t hw; 588 hw_regs_t hw;
588 struct ide_port_info d = sgiioc4_port_info; 589 struct ide_port_info d = sgiioc4_port_info;
589 590
590 hwif = ide_find_port();
591 if (hwif == NULL) {
592 printk(KERN_ERR "%s: too many IDE interfaces, no room in table\n",
593 DRV_NAME);
594 return -ENOMEM;
595 }
596
597 /* Get the CmdBlk and CtrlBlk Base Registers */ 591 /* Get the CmdBlk and CtrlBlk Base Registers */
598 bar0 = pci_resource_start(dev, 0); 592 bar0 = pci_resource_start(dev, 0);
599 virt_base = ioremap(bar0, pci_resource_len(dev, 0)); 593 virt_base = ioremap(bar0, pci_resource_len(dev, 0));
@@ -608,11 +602,11 @@ sgiioc4_ide_setup_pci_device(struct pci_dev *dev)
608 602
609 cmd_phys_base = bar0 + IOC4_CMD_OFFSET; 603 cmd_phys_base = bar0 + IOC4_CMD_OFFSET;
610 if (!request_mem_region(cmd_phys_base, IOC4_CMD_CTL_BLK_SIZE, 604 if (!request_mem_region(cmd_phys_base, IOC4_CMD_CTL_BLK_SIZE,
611 hwif->name)) { 605 DRV_NAME)) {
612 printk(KERN_ERR 606 printk(KERN_ERR
613 "%s : %s -- ERROR, Addresses " 607 "%s : %s -- ERROR, Addresses "
614 "0x%p to 0x%p ALREADY in use\n", 608 "0x%p to 0x%p ALREADY in use\n",
615 __func__, hwif->name, (void *) cmd_phys_base, 609 __func__, DRV_NAME, (void *) cmd_phys_base,
616 (void *) cmd_phys_base + IOC4_CMD_CTL_BLK_SIZE); 610 (void *) cmd_phys_base + IOC4_CMD_CTL_BLK_SIZE);
617 return -ENOMEM; 611 return -ENOMEM;
618 } 612 }
@@ -623,9 +617,12 @@ sgiioc4_ide_setup_pci_device(struct pci_dev *dev)
623 hw.irq = dev->irq; 617 hw.irq = dev->irq;
624 hw.chipset = ide_pci; 618 hw.chipset = ide_pci;
625 hw.dev = &dev->dev; 619 hw.dev = &dev->dev;
626 ide_init_port_hw(hwif, &hw);
627 620
628 hwif->dev = &dev->dev; 621 hwif = ide_find_port_slot(&d);
622 if (hwif == NULL)
623 goto err;
624
625 ide_init_port_hw(hwif, &hw);
629 626
630 /* The IOC4 uses MMIO rather than Port IO. */ 627 /* The IOC4 uses MMIO rather than Port IO. */
631 default_hwif_mmiops(hwif); 628 default_hwif_mmiops(hwif);
@@ -641,6 +638,10 @@ sgiioc4_ide_setup_pci_device(struct pci_dev *dev)
641 return -EIO; 638 return -EIO;
642 639
643 return 0; 640 return 0;
641err:
642 release_mem_region(cmd_phys_base, IOC4_CMD_CTL_BLK_SIZE);
643 iounmap(virt_base);
644 return -ENOMEM;
644} 645}
645 646
646static unsigned int __devinit 647static unsigned int __devinit
diff --git a/drivers/ide/pci/siimage.c b/drivers/ide/pci/siimage.c
index b75e9bb390a7..6e9d7655d89c 100644
--- a/drivers/ide/pci/siimage.c
+++ b/drivers/ide/pci/siimage.c
@@ -421,8 +421,7 @@ static int sil_sata_reset_poll(ide_drive_t *drive)
421 if ((sata_stat & 0x03) != 0x03) { 421 if ((sata_stat & 0x03) != 0x03) {
422 printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n", 422 printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
423 hwif->name, sata_stat); 423 hwif->name, sata_stat);
424 HWGROUP(drive)->polling = 0; 424 return -ENXIO;
425 return ide_started;
426 } 425 }
427 } 426 }
428 427
diff --git a/drivers/ide/pci/sis5513.c b/drivers/ide/pci/sis5513.c
index e127eb25ab63..2389945ca95d 100644
--- a/drivers/ide/pci/sis5513.c
+++ b/drivers/ide/pci/sis5513.c
@@ -52,8 +52,6 @@
52#include <linux/init.h> 52#include <linux/init.h>
53#include <linux/ide.h> 53#include <linux/ide.h>
54 54
55#include "ide-timing.h"
56
57/* registers layout and init values are chipset family dependant */ 55/* registers layout and init values are chipset family dependant */
58 56
59#define ATA_16 0x01 57#define ATA_16 0x01
@@ -616,7 +614,6 @@ MODULE_LICENSE("GPL");
616/* 614/*
617 * TODO: 615 * TODO:
618 * - CLEANUP 616 * - CLEANUP
619 * - Use drivers/ide/ide-timing.h !
620 * - More checks in the config registers (force values instead of 617 * - More checks in the config registers (force values instead of
621 * relying on the BIOS setting them correctly). 618 * relying on the BIOS setting them correctly).
622 * - Further optimisations ? 619 * - Further optimisations ?
diff --git a/drivers/ide/pci/sl82c105.c b/drivers/ide/pci/sl82c105.c
index ce84fa045d39..6efbde297174 100644
--- a/drivers/ide/pci/sl82c105.c
+++ b/drivers/ide/pci/sl82c105.c
@@ -47,10 +47,11 @@
47 */ 47 */
48static unsigned int get_pio_timings(ide_drive_t *drive, u8 pio) 48static unsigned int get_pio_timings(ide_drive_t *drive, u8 pio)
49{ 49{
50 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
50 unsigned int cmd_on, cmd_off; 51 unsigned int cmd_on, cmd_off;
51 u8 iordy = 0; 52 u8 iordy = 0;
52 53
53 cmd_on = (ide_pio_timings[pio].active_time + 29) / 30; 54 cmd_on = (t->active + 29) / 30;
54 cmd_off = (ide_pio_cycle_time(drive, pio) - 30 * cmd_on + 29) / 30; 55 cmd_off = (ide_pio_cycle_time(drive, pio) - 30 * cmd_on + 29) / 30;
55 56
56 if (cmd_on == 0) 57 if (cmd_on == 0)
diff --git a/drivers/ide/pci/via82cxxx.c b/drivers/ide/pci/via82cxxx.c
index 3ed9728abd24..e47384c70c40 100644
--- a/drivers/ide/pci/via82cxxx.c
+++ b/drivers/ide/pci/via82cxxx.c
@@ -35,8 +35,6 @@
35#include <asm/processor.h> 35#include <asm/processor.h>
36#endif 36#endif
37 37
38#include "ide-timing.h"
39
40#define VIA_IDE_ENABLE 0x40 38#define VIA_IDE_ENABLE 0x40
41#define VIA_IDE_CONFIG 0x41 39#define VIA_IDE_CONFIG 0x41
42#define VIA_FIFO_CONFIG 0x43 40#define VIA_FIFO_CONFIG 0x43
diff --git a/drivers/ide/ppc/Makefile b/drivers/ide/ppc/Makefile
index 65af5848b28c..74e52adcdf4b 100644
--- a/drivers/ide/ppc/Makefile
+++ b/drivers/ide/ppc/Makefile
@@ -1,3 +1,2 @@
1 1
2obj-$(CONFIG_BLK_DEV_IDE_PMAC) += pmac.o 2obj-$(CONFIG_BLK_DEV_IDE_PMAC) += pmac.o
3obj-$(CONFIG_BLK_DEV_MPC8xx_IDE) += mpc8xx.o
diff --git a/drivers/ide/ppc/mpc8xx.c b/drivers/ide/ppc/mpc8xx.c
deleted file mode 100644
index 236f9c38e519..000000000000
--- a/drivers/ide/ppc/mpc8xx.c
+++ /dev/null
@@ -1,851 +0,0 @@
1/*
2 * Copyright (C) 2000, 2001 Wolfgang Denk, wd@denx.de
3 * Modified for direct IDE interface
4 * by Thomas Lange, thomas@corelatus.com
5 * Modified for direct IDE interface on 8xx without using the PCMCIA
6 * controller
7 * by Steven.Scholz@imc-berlin.de
8 * Moved out of arch/ppc/kernel/m8xx_setup.c, other minor cleanups
9 * by Mathew Locke <mattl@mvista.com>
10 */
11
12#include <linux/errno.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/stddef.h>
16#include <linux/unistd.h>
17#include <linux/ptrace.h>
18#include <linux/slab.h>
19#include <linux/user.h>
20#include <linux/tty.h>
21#include <linux/major.h>
22#include <linux/interrupt.h>
23#include <linux/reboot.h>
24#include <linux/init.h>
25#include <linux/ioport.h>
26#include <linux/ide.h>
27#include <linux/bootmem.h>
28
29#include <asm/mpc8xx.h>
30#include <asm/mmu.h>
31#include <asm/processor.h>
32#include <asm/io.h>
33#include <asm/pgtable.h>
34#include <asm/ide.h>
35#include <asm/8xx_immap.h>
36#include <asm/machdep.h>
37#include <asm/irq.h>
38
39#define DRV_NAME "ide-mpc8xx"
40
41static int identify (volatile u8 *p);
42static void print_fixed (volatile u8 *p);
43static void print_funcid (int func);
44static int check_ide_device (unsigned long base);
45
46static void ide_interrupt_ack (void *dev);
47static void m8xx_ide_set_pio_mode(ide_drive_t *drive, const u8 pio);
48
49typedef struct ide_ioport_desc {
50 unsigned long base_off; /* Offset to PCMCIA memory */
51 unsigned long reg_off[IDE_NR_PORTS]; /* controller register offsets */
52 int irq; /* IRQ */
53} ide_ioport_desc_t;
54
55ide_ioport_desc_t ioport_dsc[MAX_HWIFS] = {
56#ifdef IDE0_BASE_OFFSET
57 { IDE0_BASE_OFFSET,
58 {
59 IDE0_DATA_REG_OFFSET,
60 IDE0_ERROR_REG_OFFSET,
61 IDE0_NSECTOR_REG_OFFSET,
62 IDE0_SECTOR_REG_OFFSET,
63 IDE0_LCYL_REG_OFFSET,
64 IDE0_HCYL_REG_OFFSET,
65 IDE0_SELECT_REG_OFFSET,
66 IDE0_STATUS_REG_OFFSET,
67 IDE0_CONTROL_REG_OFFSET,
68 IDE0_IRQ_REG_OFFSET,
69 },
70 IDE0_INTERRUPT,
71 },
72#ifdef IDE1_BASE_OFFSET
73 { IDE1_BASE_OFFSET,
74 {
75 IDE1_DATA_REG_OFFSET,
76 IDE1_ERROR_REG_OFFSET,
77 IDE1_NSECTOR_REG_OFFSET,
78 IDE1_SECTOR_REG_OFFSET,
79 IDE1_LCYL_REG_OFFSET,
80 IDE1_HCYL_REG_OFFSET,
81 IDE1_SELECT_REG_OFFSET,
82 IDE1_STATUS_REG_OFFSET,
83 IDE1_CONTROL_REG_OFFSET,
84 IDE1_IRQ_REG_OFFSET,
85 },
86 IDE1_INTERRUPT,
87 },
88#endif /* IDE1_BASE_OFFSET */
89#endif /* IDE0_BASE_OFFSET */
90};
91
92ide_pio_timings_t ide_pio_clocks[6];
93int hold_time[6] = {30, 20, 15, 10, 10, 10 }; /* PIO Mode 5 with IORDY (nonstandard) */
94
95/*
96 * Warning: only 1 (ONE) PCMCIA slot supported here,
97 * which must be correctly initialized by the firmware (PPCBoot).
98 */
99static int _slot_ = -1; /* will be read from PCMCIA registers */
100
101/* Make clock cycles and always round up */
102#define PCMCIA_MK_CLKS( t, T ) (( (t) * ((T)/1000000) + 999U ) / 1000U )
103
104#define M8XX_PCMCIA_CD2(slot) (0x10000000 >> (slot << 4))
105#define M8XX_PCMCIA_CD1(slot) (0x08000000 >> (slot << 4))
106
107/*
108 * The TQM850L hardware has two pins swapped! Grrrrgh!
109 */
110#ifdef CONFIG_TQM850L
111#define __MY_PCMCIA_GCRX_CXRESET PCMCIA_GCRX_CXOE
112#define __MY_PCMCIA_GCRX_CXOE PCMCIA_GCRX_CXRESET
113#else
114#define __MY_PCMCIA_GCRX_CXRESET PCMCIA_GCRX_CXRESET
115#define __MY_PCMCIA_GCRX_CXOE PCMCIA_GCRX_CXOE
116#endif
117
118#if defined(CONFIG_BLK_DEV_MPC8xx_IDE) && defined(CONFIG_IDE_8xx_PCCARD)
119#define PCMCIA_SCHLVL IDE0_INTERRUPT /* Status Change Interrupt Level */
120static int pcmcia_schlvl = PCMCIA_SCHLVL;
121#endif
122
123/*
124 * See include/linux/ide.h for definition of hw_regs_t (p, base)
125 */
126
127/*
128 * m8xx_ide_init_ports() for a direct IDE interface _using_
129 * MPC8xx's internal PCMCIA interface
130 */
131#if defined(CONFIG_IDE_8xx_PCCARD) || defined(CONFIG_IDE_8xx_DIRECT)
132static int __init m8xx_ide_init_ports(hw_regs_t *hw, unsigned long data_port)
133{
134 unsigned long *p = hw->io_ports_array;
135 int i;
136
137 typedef struct {
138 ulong br;
139 ulong or;
140 } pcmcia_win_t;
141 volatile pcmcia_win_t *win;
142 volatile pcmconf8xx_t *pcmp;
143
144 uint *pgcrx;
145 u32 pcmcia_phy_base;
146 u32 pcmcia_phy_end;
147 static unsigned long pcmcia_base = 0;
148 unsigned long base;
149
150 *p = 0;
151
152 pcmp = (pcmconf8xx_t *)(&(((immap_t *)IMAP_ADDR)->im_pcmcia));
153
154 if (!pcmcia_base) {
155 /*
156 * Read out PCMCIA registers. Since the reset values
157 * are undefined, we sure hope that they have been
158 * set up by firmware
159 */
160
161 /* Scan all registers for valid settings */
162 pcmcia_phy_base = 0xFFFFFFFF;
163 pcmcia_phy_end = 0;
164 /* br0 is start of brX and orX regs */
165 win = (pcmcia_win_t *) \
166 (&(((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pbr0));
167 for (i = 0; i < 8; i++) {
168 if (win->or & 1) { /* This bank is marked as valid */
169 if (win->br < pcmcia_phy_base) {
170 pcmcia_phy_base = win->br;
171 }
172 if ((win->br + PCMCIA_MEM_SIZE) > pcmcia_phy_end) {
173 pcmcia_phy_end = win->br + PCMCIA_MEM_SIZE;
174 }
175 /* Check which slot that has been defined */
176 _slot_ = (win->or >> 2) & 1;
177
178 } /* Valid bank */
179 win++;
180 } /* for */
181
182 printk ("PCMCIA slot %c: phys mem %08x...%08x (size %08x)\n",
183 'A' + _slot_,
184 pcmcia_phy_base, pcmcia_phy_end,
185 pcmcia_phy_end - pcmcia_phy_base);
186
187 if (!request_mem_region(pcmcia_phy_base,
188 pcmcia_phy_end - pcmcia_phy_base,
189 DRV_NAME)) {
190 printk(KERN_ERR "%s: resources busy\n", DRV_NAME);
191 return -EBUSY;
192 }
193
194 pcmcia_base=(unsigned long)ioremap(pcmcia_phy_base,
195 pcmcia_phy_end-pcmcia_phy_base);
196
197#ifdef DEBUG
198 printk ("PCMCIA virt base: %08lx\n", pcmcia_base);
199#endif
200 /* Compute clock cycles for PIO timings */
201 for (i=0; i<6; ++i) {
202 bd_t *binfo = (bd_t *)__res;
203
204 hold_time[i] =
205 PCMCIA_MK_CLKS (hold_time[i],
206 binfo->bi_busfreq);
207 ide_pio_clocks[i].setup_time =
208 PCMCIA_MK_CLKS (ide_pio_timings[i].setup_time,
209 binfo->bi_busfreq);
210 ide_pio_clocks[i].active_time =
211 PCMCIA_MK_CLKS (ide_pio_timings[i].active_time,
212 binfo->bi_busfreq);
213 ide_pio_clocks[i].cycle_time =
214 PCMCIA_MK_CLKS (ide_pio_timings[i].cycle_time,
215 binfo->bi_busfreq);
216#if 0
217 printk ("PIO mode %d timings: %d/%d/%d => %d/%d/%d\n",
218 i,
219 ide_pio_clocks[i].setup_time,
220 ide_pio_clocks[i].active_time,
221 ide_pio_clocks[i].hold_time,
222 ide_pio_clocks[i].cycle_time,
223 ide_pio_timings[i].setup_time,
224 ide_pio_timings[i].active_time,
225 ide_pio_timings[i].hold_time,
226 ide_pio_timings[i].cycle_time);
227#endif
228 }
229 }
230
231 if (_slot_ == -1) {
232 printk ("PCMCIA slot has not been defined! Using A as default\n");
233 _slot_ = 0;
234 }
235
236#ifdef CONFIG_IDE_8xx_PCCARD
237
238#ifdef DEBUG
239 printk ("PIPR = 0x%08X slot %c ==> mask = 0x%X\n",
240 pcmp->pcmc_pipr,
241 'A' + _slot_,
242 M8XX_PCMCIA_CD1(_slot_) | M8XX_PCMCIA_CD2(_slot_) );
243#endif /* DEBUG */
244
245 if (pcmp->pcmc_pipr & (M8XX_PCMCIA_CD1(_slot_)|M8XX_PCMCIA_CD2(_slot_))) {
246 printk ("No card in slot %c: PIPR=%08x\n",
247 'A' + _slot_, (u32) pcmp->pcmc_pipr);
248 return -ENODEV; /* No card in slot */
249 }
250
251 check_ide_device (pcmcia_base);
252
253#endif /* CONFIG_IDE_8xx_PCCARD */
254
255 base = pcmcia_base + ioport_dsc[data_port].base_off;
256#ifdef DEBUG
257 printk ("base: %08x + %08x = %08x\n",
258 pcmcia_base, ioport_dsc[data_port].base_off, base);
259#endif
260
261 for (i = 0; i < IDE_NR_PORTS; ++i) {
262#ifdef DEBUG
263 printk ("port[%d]: %08x + %08x = %08x\n",
264 i,
265 base,
266 ioport_dsc[data_port].reg_off[i],
267 i, base + ioport_dsc[data_port].reg_off[i]);
268#endif
269 *p++ = base + ioport_dsc[data_port].reg_off[i];
270 }
271
272 hw->irq = ioport_dsc[data_port].irq;
273 hw->ack_intr = (ide_ack_intr_t *)ide_interrupt_ack;
274
275#ifdef CONFIG_IDE_8xx_PCCARD
276 {
277 unsigned int reg;
278
279 if (_slot_)
280 pgcrx = &((immap_t *) IMAP_ADDR)->im_pcmcia.pcmc_pgcrb;
281 else
282 pgcrx = &((immap_t *) IMAP_ADDR)->im_pcmcia.pcmc_pgcra;
283
284 reg = *pgcrx;
285 reg |= mk_int_int_mask (pcmcia_schlvl) << 24;
286 reg |= mk_int_int_mask (pcmcia_schlvl) << 16;
287 *pgcrx = reg;
288 }
289#endif /* CONFIG_IDE_8xx_PCCARD */
290
291 /* Enable Harddisk Interrupt,
292 * and make it edge sensitive
293 */
294 /* (11-18) Set edge detect for irq, no wakeup from low power mode */
295 ((immap_t *)IMAP_ADDR)->im_siu_conf.sc_siel |=
296 (0x80000000 >> ioport_dsc[data_port].irq);
297
298#ifdef CONFIG_IDE_8xx_PCCARD
299 /* Make sure we don't get garbage irq */
300 ((immap_t *) IMAP_ADDR)->im_pcmcia.pcmc_pscr = 0xFFFF;
301
302 /* Enable falling edge irq */
303 pcmp->pcmc_per = 0x100000 >> (16 * _slot_);
304#endif /* CONFIG_IDE_8xx_PCCARD */
305
306 hw->chipset = ide_generic;
307
308 return 0;
309}
310#endif /* CONFIG_IDE_8xx_PCCARD || CONFIG_IDE_8xx_DIRECT */
311
312/*
313 * m8xx_ide_init_ports() for a direct IDE interface _not_ using
314 * MPC8xx's internal PCMCIA interface
315 */
316#if defined(CONFIG_IDE_EXT_DIRECT)
317static int __init m8xx_ide_init_ports(hw_regs_t *hw, unsigned long data_port)
318{
319 unsigned long *p = hw->io_ports_array;
320 int i;
321
322 u32 ide_phy_base;
323 u32 ide_phy_end;
324 static unsigned long ide_base = 0;
325 unsigned long base;
326
327 *p = 0;
328
329 if (!ide_base) {
330
331 /* TODO:
332 * - add code to read ORx, BRx
333 */
334 ide_phy_base = CFG_ATA_BASE_ADDR;
335 ide_phy_end = CFG_ATA_BASE_ADDR + 0x200;
336
337 printk ("IDE phys mem : %08x...%08x (size %08x)\n",
338 ide_phy_base, ide_phy_end,
339 ide_phy_end - ide_phy_base);
340
341 if (!request_mem_region(ide_phy_base, 0x200, DRV_NAME)) {
342 printk(KERN_ERR "%s: resources busy\n", DRV_NAME);
343 return -EBUSY;
344 }
345
346 ide_base=(unsigned long)ioremap(ide_phy_base,
347 ide_phy_end-ide_phy_base);
348
349#ifdef DEBUG
350 printk ("IDE virt base: %08lx\n", ide_base);
351#endif
352 }
353
354 base = ide_base + ioport_dsc[data_port].base_off;
355#ifdef DEBUG
356 printk ("base: %08x + %08x = %08x\n",
357 ide_base, ioport_dsc[data_port].base_off, base);
358#endif
359
360 for (i = 0; i < IDE_NR_PORTS; ++i) {
361#ifdef DEBUG
362 printk ("port[%d]: %08x + %08x = %08x\n",
363 i,
364 base,
365 ioport_dsc[data_port].reg_off[i],
366 i, base + ioport_dsc[data_port].reg_off[i]);
367#endif
368 *p++ = base + ioport_dsc[data_port].reg_off[i];
369 }
370
371 /* direct connected IDE drive, i.e. external IRQ */
372 hw->irq = ioport_dsc[data_port].irq;
373 hw->ack_intr = (ide_ack_intr_t *)ide_interrupt_ack;
374
375 /* Enable Harddisk Interrupt,
376 * and make it edge sensitive
377 */
378 /* (11-18) Set edge detect for irq, no wakeup from low power mode */
379 ((immap_t *) IMAP_ADDR)->im_siu_conf.sc_siel |=
380 (0x80000000 >> ioport_dsc[data_port].irq);
381
382 hw->chipset = ide_generic;
383
384 return 0;
385}
386#endif /* CONFIG_IDE_8xx_DIRECT */
387
388
389/* -------------------------------------------------------------------- */
390
391
392/* PCMCIA Timing */
393#ifndef PCMCIA_SHT
394#define PCMCIA_SHT(t) ((t & 0x0F)<<16) /* Strobe Hold Time */
395#define PCMCIA_SST(t) ((t & 0x0F)<<12) /* Strobe Setup Time */
396#define PCMCIA_SL(t) ((t==32) ? 0 : ((t & 0x1F)<<7)) /* Strobe Length */
397#endif
398
399/* Calculate PIO timings */
400static void m8xx_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
401{
402#if defined(CONFIG_IDE_8xx_PCCARD) || defined(CONFIG_IDE_8xx_DIRECT)
403 volatile pcmconf8xx_t *pcmp;
404 ulong timing, mask, reg;
405
406 pcmp = (pcmconf8xx_t *)(&(((immap_t *)IMAP_ADDR)->im_pcmcia));
407
408 mask = ~(PCMCIA_SHT(0xFF) | PCMCIA_SST(0xFF) | PCMCIA_SL(0xFF));
409
410 timing = PCMCIA_SHT(hold_time[pio] )
411 | PCMCIA_SST(ide_pio_clocks[pio].setup_time )
412 | PCMCIA_SL (ide_pio_clocks[pio].active_time)
413 ;
414
415#if 1
416 printk ("Setting timing bits 0x%08lx in PCMCIA controller\n", timing);
417#endif
418 if ((reg = pcmp->pcmc_por0 & mask) != 0)
419 pcmp->pcmc_por0 = reg | timing;
420
421 if ((reg = pcmp->pcmc_por1 & mask) != 0)
422 pcmp->pcmc_por1 = reg | timing;
423
424 if ((reg = pcmp->pcmc_por2 & mask) != 0)
425 pcmp->pcmc_por2 = reg | timing;
426
427 if ((reg = pcmp->pcmc_por3 & mask) != 0)
428 pcmp->pcmc_por3 = reg | timing;
429
430 if ((reg = pcmp->pcmc_por4 & mask) != 0)
431 pcmp->pcmc_por4 = reg | timing;
432
433 if ((reg = pcmp->pcmc_por5 & mask) != 0)
434 pcmp->pcmc_por5 = reg | timing;
435
436 if ((reg = pcmp->pcmc_por6 & mask) != 0)
437 pcmp->pcmc_por6 = reg | timing;
438
439 if ((reg = pcmp->pcmc_por7 & mask) != 0)
440 pcmp->pcmc_por7 = reg | timing;
441
442#elif defined(CONFIG_IDE_EXT_DIRECT)
443
444 printk("%s[%d] %s: not implemented yet!\n",
445 __FILE__, __LINE__, __func__);
446#endif /* defined(CONFIG_IDE_8xx_PCCARD) || defined(CONFIG_IDE_8xx_PCMCIA */
447}
448
449static const struct ide_port_ops m8xx_port_ops = {
450 .set_pio_mode = m8xx_ide_set_pio_mode,
451};
452
453static void
454ide_interrupt_ack (void *dev)
455{
456#ifdef CONFIG_IDE_8xx_PCCARD
457 u_int pscr, pipr;
458
459#if (PCMCIA_SOCKETS_NO == 2)
460 u_int _slot_;
461#endif
462
463 /* get interrupt sources */
464
465 pscr = ((volatile immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pscr;
466 pipr = ((volatile immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pipr;
467
468 /*
469 * report only if both card detect signals are the same
470 * not too nice done,
471 * we depend on that CD2 is the bit to the left of CD1...
472 */
473
474 if(_slot_==-1){
475 printk("PCMCIA slot has not been defined! Using A as default\n");
476 _slot_=0;
477 }
478
479 if(((pipr & M8XX_PCMCIA_CD2(_slot_)) >> 1) ^
480 (pipr & M8XX_PCMCIA_CD1(_slot_)) ) {
481 printk ("card detect interrupt\n");
482 }
483 /* clear the interrupt sources */
484 ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pscr = pscr;
485
486#else /* ! CONFIG_IDE_8xx_PCCARD */
487 /*
488 * Only CONFIG_IDE_8xx_PCCARD is using the interrupt of the
489 * MPC8xx's PCMCIA controller, so there is nothing to be done here
490 * for CONFIG_IDE_8xx_DIRECT and CONFIG_IDE_EXT_DIRECT.
491 * The interrupt is handled somewhere else. -- Steven
492 */
493#endif /* CONFIG_IDE_8xx_PCCARD */
494}
495
496
497
498/*
499 * CIS Tupel codes
500 */
501#define CISTPL_NULL 0x00
502#define CISTPL_DEVICE 0x01
503#define CISTPL_LONGLINK_CB 0x02
504#define CISTPL_INDIRECT 0x03
505#define CISTPL_CONFIG_CB 0x04
506#define CISTPL_CFTABLE_ENTRY_CB 0x05
507#define CISTPL_LONGLINK_MFC 0x06
508#define CISTPL_BAR 0x07
509#define CISTPL_PWR_MGMNT 0x08
510#define CISTPL_EXTDEVICE 0x09
511#define CISTPL_CHECKSUM 0x10
512#define CISTPL_LONGLINK_A 0x11
513#define CISTPL_LONGLINK_C 0x12
514#define CISTPL_LINKTARGET 0x13
515#define CISTPL_NO_LINK 0x14
516#define CISTPL_VERS_1 0x15
517#define CISTPL_ALTSTR 0x16
518#define CISTPL_DEVICE_A 0x17
519#define CISTPL_JEDEC_C 0x18
520#define CISTPL_JEDEC_A 0x19
521#define CISTPL_CONFIG 0x1a
522#define CISTPL_CFTABLE_ENTRY 0x1b
523#define CISTPL_DEVICE_OC 0x1c
524#define CISTPL_DEVICE_OA 0x1d
525#define CISTPL_DEVICE_GEO 0x1e
526#define CISTPL_DEVICE_GEO_A 0x1f
527#define CISTPL_MANFID 0x20
528#define CISTPL_FUNCID 0x21
529#define CISTPL_FUNCE 0x22
530#define CISTPL_SWIL 0x23
531#define CISTPL_END 0xff
532
533/*
534 * CIS Function ID codes
535 */
536#define CISTPL_FUNCID_MULTI 0x00
537#define CISTPL_FUNCID_MEMORY 0x01
538#define CISTPL_FUNCID_SERIAL 0x02
539#define CISTPL_FUNCID_PARALLEL 0x03
540#define CISTPL_FUNCID_FIXED 0x04
541#define CISTPL_FUNCID_VIDEO 0x05
542#define CISTPL_FUNCID_NETWORK 0x06
543#define CISTPL_FUNCID_AIMS 0x07
544#define CISTPL_FUNCID_SCSI 0x08
545
546/*
547 * Fixed Disk FUNCE codes
548 */
549#define CISTPL_IDE_INTERFACE 0x01
550
551#define CISTPL_FUNCE_IDE_IFACE 0x01
552#define CISTPL_FUNCE_IDE_MASTER 0x02
553#define CISTPL_FUNCE_IDE_SLAVE 0x03
554
555/* First feature byte */
556#define CISTPL_IDE_SILICON 0x04
557#define CISTPL_IDE_UNIQUE 0x08
558#define CISTPL_IDE_DUAL 0x10
559
560/* Second feature byte */
561#define CISTPL_IDE_HAS_SLEEP 0x01
562#define CISTPL_IDE_HAS_STANDBY 0x02
563#define CISTPL_IDE_HAS_IDLE 0x04
564#define CISTPL_IDE_LOW_POWER 0x08
565#define CISTPL_IDE_REG_INHIBIT 0x10
566#define CISTPL_IDE_HAS_INDEX 0x20
567#define CISTPL_IDE_IOIS16 0x40
568
569
570/* -------------------------------------------------------------------- */
571
572
573#define MAX_TUPEL_SZ 512
574#define MAX_FEATURES 4
575
576static int check_ide_device (unsigned long base)
577{
578 volatile u8 *ident = NULL;
579 volatile u8 *feature_p[MAX_FEATURES];
580 volatile u8 *p, *start;
581 int n_features = 0;
582 u8 func_id = ~0;
583 u8 code, len;
584 unsigned short config_base = 0;
585 int found = 0;
586 int i;
587
588#ifdef DEBUG
589 printk ("PCMCIA MEM: %08lX\n", base);
590#endif
591 start = p = (volatile u8 *) base;
592
593 while ((p - start) < MAX_TUPEL_SZ) {
594
595 code = *p; p += 2;
596
597 if (code == 0xFF) { /* End of chain */
598 break;
599 }
600
601 len = *p; p += 2;
602#ifdef DEBUG_PCMCIA
603 { volatile u8 *q = p;
604 printk ("\nTuple code %02x length %d\n\tData:",
605 code, len);
606
607 for (i = 0; i < len; ++i) {
608 printk (" %02x", *q);
609 q+= 2;
610 }
611 }
612#endif /* DEBUG_PCMCIA */
613 switch (code) {
614 case CISTPL_VERS_1:
615 ident = p + 4;
616 break;
617 case CISTPL_FUNCID:
618 func_id = *p;
619 break;
620 case CISTPL_FUNCE:
621 if (n_features < MAX_FEATURES)
622 feature_p[n_features++] = p;
623 break;
624 case CISTPL_CONFIG:
625 config_base = (*(p+6) << 8) + (*(p+4));
626 default:
627 break;
628 }
629 p += 2 * len;
630 }
631
632 found = identify (ident);
633
634 if (func_id != ((u8)~0)) {
635 print_funcid (func_id);
636
637 if (func_id == CISTPL_FUNCID_FIXED)
638 found = 1;
639 else
640 return (1); /* no disk drive */
641 }
642
643 for (i=0; i<n_features; ++i) {
644 print_fixed (feature_p[i]);
645 }
646
647 if (!found) {
648 printk ("unknown card type\n");
649 return (1);
650 }
651
652 /* set level mode irq and I/O mapped device in config reg*/
653 *((u8 *)(base + config_base)) = 0x41;
654
655 return (0);
656}
657
658/* ------------------------------------------------------------------------- */
659
660static void print_funcid (int func)
661{
662 switch (func) {
663 case CISTPL_FUNCID_MULTI:
664 printk (" Multi-Function");
665 break;
666 case CISTPL_FUNCID_MEMORY:
667 printk (" Memory");
668 break;
669 case CISTPL_FUNCID_SERIAL:
670 printk (" Serial Port");
671 break;
672 case CISTPL_FUNCID_PARALLEL:
673 printk (" Parallel Port");
674 break;
675 case CISTPL_FUNCID_FIXED:
676 printk (" Fixed Disk");
677 break;
678 case CISTPL_FUNCID_VIDEO:
679 printk (" Video Adapter");
680 break;
681 case CISTPL_FUNCID_NETWORK:
682 printk (" Network Adapter");
683 break;
684 case CISTPL_FUNCID_AIMS:
685 printk (" AIMS Card");
686 break;
687 case CISTPL_FUNCID_SCSI:
688 printk (" SCSI Adapter");
689 break;
690 default:
691 printk (" Unknown");
692 break;
693 }
694 printk (" Card\n");
695}
696
697/* ------------------------------------------------------------------------- */
698
699static void print_fixed (volatile u8 *p)
700{
701 if (p == NULL)
702 return;
703
704 switch (*p) {
705 case CISTPL_FUNCE_IDE_IFACE:
706 { u8 iface = *(p+2);
707
708 printk ((iface == CISTPL_IDE_INTERFACE) ? " IDE" : " unknown");
709 printk (" interface ");
710 break;
711 }
712 case CISTPL_FUNCE_IDE_MASTER:
713 case CISTPL_FUNCE_IDE_SLAVE:
714 { u8 f1 = *(p+2);
715 u8 f2 = *(p+4);
716
717 printk ((f1 & CISTPL_IDE_SILICON) ? " [silicon]" : " [rotating]");
718
719 if (f1 & CISTPL_IDE_UNIQUE)
720 printk (" [unique]");
721
722 printk ((f1 & CISTPL_IDE_DUAL) ? " [dual]" : " [single]");
723
724 if (f2 & CISTPL_IDE_HAS_SLEEP)
725 printk (" [sleep]");
726
727 if (f2 & CISTPL_IDE_HAS_STANDBY)
728 printk (" [standby]");
729
730 if (f2 & CISTPL_IDE_HAS_IDLE)
731 printk (" [idle]");
732
733 if (f2 & CISTPL_IDE_LOW_POWER)
734 printk (" [low power]");
735
736 if (f2 & CISTPL_IDE_REG_INHIBIT)
737 printk (" [reg inhibit]");
738
739 if (f2 & CISTPL_IDE_HAS_INDEX)
740 printk (" [index]");
741
742 if (f2 & CISTPL_IDE_IOIS16)
743 printk (" [IOis16]");
744
745 break;
746 }
747 }
748 printk ("\n");
749}
750
751/* ------------------------------------------------------------------------- */
752
753
754#define MAX_IDENT_CHARS 64
755#define MAX_IDENT_FIELDS 4
756
757static u8 *known_cards[] = {
758 "ARGOSY PnPIDE D5",
759 NULL
760};
761
762static int identify (volatile u8 *p)
763{
764 u8 id_str[MAX_IDENT_CHARS];
765 u8 data;
766 u8 *t;
767 u8 **card;
768 int i, done;
769
770 if (p == NULL)
771 return (0); /* Don't know */
772
773 t = id_str;
774 done =0;
775
776 for (i=0; i<=4 && !done; ++i, p+=2) {
777 while ((data = *p) != '\0') {
778 if (data == 0xFF) {
779 done = 1;
780 break;
781 }
782 *t++ = data;
783 if (t == &id_str[MAX_IDENT_CHARS-1]) {
784 done = 1;
785 break;
786 }
787 p += 2;
788 }
789 if (!done)
790 *t++ = ' ';
791 }
792 *t = '\0';
793 while (--t > id_str) {
794 if (*t == ' ')
795 *t = '\0';
796 else
797 break;
798 }
799 printk ("Card ID: %s\n", id_str);
800
801 for (card=known_cards; *card; ++card) {
802 if (strcmp(*card, id_str) == 0) { /* found! */
803 return (1);
804 }
805 }
806
807 return (0); /* don't know */
808}
809
810static int __init mpc8xx_ide_probe(void)
811{
812 hw_regs_t hw;
813 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
814
815#ifdef IDE0_BASE_OFFSET
816 memset(&hw, 0, sizeof(hw));
817 if (!m8xx_ide_init_ports(&hw, 0)) {
818 ide_hwif_t *hwif = ide_find_port();
819
820 if (hwif) {
821 ide_init_port_hw(hwif, &hw);
822 hwif->pio_mask = ATA_PIO4;
823 hwif->port_ops = &m8xx_port_ops;
824
825 idx[0] = hwif->index;
826 }
827 }
828#ifdef IDE1_BASE_OFFSET
829 memset(&hw, 0, sizeof(hw));
830 if (!m8xx_ide_init_ports(&hw, 1)) {
831 ide_hwif_t *mate = ide_find_port();
832
833 if (mate) {
834 ide_init_port_hw(mate, &hw);
835 mate->pio_mask = ATA_PIO4;
836 mate->port_ops = &m8xx_port_ops;
837
838 idx[1] = mate->index;
839 }
840 }
841#endif
842#endif
843
844 ide_device_add(idx, NULL);
845
846 return 0;
847}
848
849module_init(mpc8xx_ide_probe);
850
851MODULE_LICENSE("GPL");
diff --git a/drivers/ide/ppc/pmac.c b/drivers/ide/ppc/pmac.c
index dcb2c466bb97..93fb9067c043 100644
--- a/drivers/ide/ppc/pmac.c
+++ b/drivers/ide/ppc/pmac.c
@@ -5,7 +5,7 @@
5 * for doing DMA. 5 * for doing DMA.
6 * 6 *
7 * Copyright (C) 1998-2003 Paul Mackerras & Ben. Herrenschmidt 7 * Copyright (C) 1998-2003 Paul Mackerras & Ben. Herrenschmidt
8 * Copyright (C) 2007 Bartlomiej Zolnierkiewicz 8 * Copyright (C) 2007-2008 Bartlomiej Zolnierkiewicz
9 * 9 *
10 * This program is free software; you can redistribute it and/or 10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License 11 * modify it under the terms of the GNU General Public License
@@ -48,8 +48,6 @@
48#include <asm/mediabay.h> 48#include <asm/mediabay.h>
49#endif 49#endif
50 50
51#include "../ide-timing.h"
52
53#undef IDE_PMAC_DEBUG 51#undef IDE_PMAC_DEBUG
54 52
55#define DMA_WAIT_TIMEOUT 50 53#define DMA_WAIT_TIMEOUT 50
@@ -495,6 +493,7 @@ static void pmac_outbsync(ide_hwif_t *hwif, u8 value, unsigned long port)
495static void 493static void
496pmac_ide_set_pio_mode(ide_drive_t *drive, const u8 pio) 494pmac_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
497{ 495{
496 struct ide_timing *tim = ide_timing_find_mode(XFER_PIO_0 + pio);
498 u32 *timings, t; 497 u32 *timings, t;
499 unsigned accessTicks, recTicks; 498 unsigned accessTicks, recTicks;
500 unsigned accessTime, recTime; 499 unsigned accessTime, recTime;
@@ -526,10 +525,9 @@ pmac_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
526 } 525 }
527 case controller_kl_ata4: 526 case controller_kl_ata4:
528 /* 66Mhz cell */ 527 /* 66Mhz cell */
529 recTime = cycle_time - ide_pio_timings[pio].active_time 528 recTime = cycle_time - tim->active - tim->setup;
530 - ide_pio_timings[pio].setup_time;
531 recTime = max(recTime, 150U); 529 recTime = max(recTime, 150U);
532 accessTime = ide_pio_timings[pio].active_time; 530 accessTime = tim->active;
533 accessTime = max(accessTime, 150U); 531 accessTime = max(accessTime, 150U);
534 accessTicks = SYSCLK_TICKS_66(accessTime); 532 accessTicks = SYSCLK_TICKS_66(accessTime);
535 accessTicks = min(accessTicks, 0x1fU); 533 accessTicks = min(accessTicks, 0x1fU);
@@ -542,10 +540,9 @@ pmac_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
542 default: { 540 default: {
543 /* 33Mhz cell */ 541 /* 33Mhz cell */
544 int ebit = 0; 542 int ebit = 0;
545 recTime = cycle_time - ide_pio_timings[pio].active_time 543 recTime = cycle_time - tim->active - tim->setup;
546 - ide_pio_timings[pio].setup_time;
547 recTime = max(recTime, 150U); 544 recTime = max(recTime, 150U);
548 accessTime = ide_pio_timings[pio].active_time; 545 accessTime = tim->active;
549 accessTime = max(accessTime, 150U); 546 accessTime = max(accessTime, 150U);
550 accessTicks = SYSCLK_TICKS(accessTime); 547 accessTicks = SYSCLK_TICKS(accessTime);
551 accessTicks = min(accessTicks, 0x1fU); 548 accessTicks = min(accessTicks, 0x1fU);
@@ -1151,8 +1148,6 @@ pmac_ide_macio_attach(struct macio_dev *mdev, const struct of_device_id *match)
1151 base = ioremap(macio_resource_start(mdev, 0), 0x400); 1148 base = ioremap(macio_resource_start(mdev, 0), 0x400);
1152 regbase = (unsigned long) base; 1149 regbase = (unsigned long) base;
1153 1150
1154 hwif->dev = &mdev->bus->pdev->dev;
1155
1156 pmif->mdev = mdev; 1151 pmif->mdev = mdev;
1157 pmif->node = mdev->ofdev.node; 1152 pmif->node = mdev->ofdev.node;
1158 pmif->regbase = regbase; 1153 pmif->regbase = regbase;
@@ -1174,7 +1169,8 @@ pmac_ide_macio_attach(struct macio_dev *mdev, const struct of_device_id *match)
1174 memset(&hw, 0, sizeof(hw)); 1169 memset(&hw, 0, sizeof(hw));
1175 pmac_ide_init_ports(&hw, pmif->regbase); 1170 pmac_ide_init_ports(&hw, pmif->regbase);
1176 hw.irq = irq; 1171 hw.irq = irq;
1177 hw.dev = &mdev->ofdev.dev; 1172 hw.dev = &mdev->bus->pdev->dev;
1173 hw.parent = &mdev->ofdev.dev;
1178 1174
1179 rc = pmac_ide_setup_device(pmif, hwif, &hw); 1175 rc = pmac_ide_setup_device(pmif, hwif, &hw);
1180 if (rc != 0) { 1176 if (rc != 0) {
@@ -1274,7 +1270,6 @@ pmac_ide_pci_attach(struct pci_dev *pdev, const struct pci_device_id *id)
1274 goto out_free_pmif; 1270 goto out_free_pmif;
1275 } 1271 }
1276 1272
1277 hwif->dev = &pdev->dev;
1278 pmif->mdev = NULL; 1273 pmif->mdev = NULL;
1279 pmif->node = np; 1274 pmif->node = np;
1280 1275
diff --git a/drivers/ide/setup-pci.c b/drivers/ide/setup-pci.c
index abcfb1739d4d..65fc08b6b6d0 100644
--- a/drivers/ide/setup-pci.c
+++ b/drivers/ide/setup-pci.c
@@ -6,19 +6,15 @@
6 * May be copied or modified under the terms of the GNU General Public License 6 * May be copied or modified under the terms of the GNU General Public License
7 */ 7 */
8 8
9#include <linux/module.h>
10#include <linux/types.h> 9#include <linux/types.h>
11#include <linux/kernel.h> 10#include <linux/kernel.h>
12#include <linux/pci.h> 11#include <linux/pci.h>
13#include <linux/init.h> 12#include <linux/init.h>
14#include <linux/timer.h>
15#include <linux/mm.h>
16#include <linux/interrupt.h> 13#include <linux/interrupt.h>
17#include <linux/ide.h> 14#include <linux/ide.h>
18#include <linux/dma-mapping.h> 15#include <linux/dma-mapping.h>
19 16
20#include <asm/io.h> 17#include <asm/io.h>
21#include <asm/irq.h>
22 18
23/** 19/**
24 * ide_setup_pci_baseregs - place a PCI IDE controller native 20 * ide_setup_pci_baseregs - place a PCI IDE controller native
@@ -319,25 +315,22 @@ static ide_hwif_t *ide_hwif_configure(struct pci_dev *dev,
319 315
320 ctl = pci_resource_start(dev, 2*port+1); 316 ctl = pci_resource_start(dev, 2*port+1);
321 base = pci_resource_start(dev, 2*port); 317 base = pci_resource_start(dev, 2*port);
322 if ((ctl && !base) || (base && !ctl)) { 318 } else {
323 printk(KERN_ERR "%s: inconsistent baseregs (BIOS) "
324 "for port %d, skipping\n", d->name, port);
325 return NULL;
326 }
327 }
328 if (!ctl) {
329 /* Use default values */ 319 /* Use default values */
330 ctl = port ? 0x374 : 0x3f4; 320 ctl = port ? 0x374 : 0x3f4;
331 base = port ? 0x170 : 0x1f0; 321 base = port ? 0x170 : 0x1f0;
332 } 322 }
333 323
334 hwif = ide_find_port_slot(d); 324 if (!base || !ctl) {
335 if (hwif == NULL) { 325 printk(KERN_ERR "%s: bad PCI BARs for port %d, skipping\n",
336 printk(KERN_ERR "%s: too many IDE interfaces, no room in " 326 d->name, port);
337 "table\n", d->name);
338 return NULL; 327 return NULL;
339 } 328 }
340 329
330 hwif = ide_find_port_slot(d);
331 if (hwif == NULL)
332 return NULL;
333
341 memset(&hw, 0, sizeof(hw)); 334 memset(&hw, 0, sizeof(hw));
342 hw.irq = irq; 335 hw.irq = irq;
343 hw.dev = &dev->dev; 336 hw.dev = &dev->dev;
@@ -346,8 +339,6 @@ static ide_hwif_t *ide_hwif_configure(struct pci_dev *dev,
346 339
347 ide_init_port_hw(hwif, &hw); 340 ide_init_port_hw(hwif, &hw);
348 341
349 hwif->dev = &dev->dev;
350
351 return hwif; 342 return hwif;
352} 343}
353 344
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index 636af2862308..1921b8dbb242 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -179,17 +179,29 @@ config FUJITSU_LAPTOP
179 tristate "Fujitsu Laptop Extras" 179 tristate "Fujitsu Laptop Extras"
180 depends on X86 180 depends on X86
181 depends on ACPI 181 depends on ACPI
182 depends on INPUT
182 depends on BACKLIGHT_CLASS_DEVICE 183 depends on BACKLIGHT_CLASS_DEVICE
183 ---help--- 184 ---help---
184 This is a driver for laptops built by Fujitsu: 185 This is a driver for laptops built by Fujitsu:
185 186
186 * P2xxx/P5xxx/S6xxx/S7xxx series Lifebooks 187 * P2xxx/P5xxx/S6xxx/S7xxx series Lifebooks
187 * Possibly other Fujitsu laptop models 188 * Possibly other Fujitsu laptop models
189 * Tested with S6410 and S7020
188 190
189 It adds support for LCD brightness control. 191 It adds support for LCD brightness control and some hotkeys.
190 192
191 If you have a Fujitsu laptop, say Y or M here. 193 If you have a Fujitsu laptop, say Y or M here.
192 194
195config FUJITSU_LAPTOP_DEBUG
196 bool "Verbose debug mode for Fujitsu Laptop Extras"
197 depends on FUJITSU_LAPTOP
198 default n
199 ---help---
200 Enables extra debug output from the fujitsu extras driver, at the
201 expense of a slight increase in driver size.
202
203 If you are not sure, say N here.
204
193config TC1100_WMI 205config TC1100_WMI
194 tristate "HP Compaq TC1100 Tablet WMI Extras (EXPERIMENTAL)" 206 tristate "HP Compaq TC1100 Tablet WMI Extras (EXPERIMENTAL)"
195 depends on X86 && !X86_64 207 depends on X86 && !X86_64
@@ -219,6 +231,23 @@ config MSI_LAPTOP
219 231
220 If you have an MSI S270 laptop, say Y or M here. 232 If you have an MSI S270 laptop, say Y or M here.
221 233
234config COMPAL_LAPTOP
235 tristate "Compal Laptop Extras"
236 depends on X86
237 depends on ACPI_EC
238 depends on BACKLIGHT_CLASS_DEVICE
239 ---help---
240 This is a driver for laptops built by Compal:
241
242 Compal FL90/IFL90
243 Compal FL91/IFL91
244 Compal FL92/JFL92
245 Compal FT00/IFT00
246
247 It adds support for Bluetooth, WLAN and LCD brightness control.
248
249 If you have an Compal FL9x/IFL9x/FT00 laptop, say Y or M here.
250
222config SONY_LAPTOP 251config SONY_LAPTOP
223 tristate "Sony Laptop Extras" 252 tristate "Sony Laptop Extras"
224 depends on X86 && ACPI 253 depends on X86 && ACPI
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 1952875a272e..a6dac6a2e7e5 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -5,10 +5,11 @@ obj- := misc.o # Dummy rule to force built-in.o to be made
5 5
6obj-$(CONFIG_IBM_ASM) += ibmasm/ 6obj-$(CONFIG_IBM_ASM) += ibmasm/
7obj-$(CONFIG_HDPU_FEATURES) += hdpuftrs/ 7obj-$(CONFIG_HDPU_FEATURES) += hdpuftrs/
8obj-$(CONFIG_MSI_LAPTOP) += msi-laptop.o
9obj-$(CONFIG_ACER_WMI) += acer-wmi.o
10obj-$(CONFIG_ASUS_LAPTOP) += asus-laptop.o 8obj-$(CONFIG_ASUS_LAPTOP) += asus-laptop.o
11obj-$(CONFIG_EEEPC_LAPTOP) += eeepc-laptop.o 9obj-$(CONFIG_EEEPC_LAPTOP) += eeepc-laptop.o
10obj-$(CONFIG_MSI_LAPTOP) += msi-laptop.o
11obj-$(CONFIG_COMPAL_LAPTOP) += compal-laptop.o
12obj-$(CONFIG_ACER_WMI) += acer-wmi.o
12obj-$(CONFIG_ATMEL_PWM) += atmel_pwm.o 13obj-$(CONFIG_ATMEL_PWM) += atmel_pwm.o
13obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o 14obj-$(CONFIG_ATMEL_SSC) += atmel-ssc.o
14obj-$(CONFIG_ATMEL_TCLIB) += atmel_tclib.o 15obj-$(CONFIG_ATMEL_TCLIB) += atmel_tclib.o
diff --git a/drivers/misc/acer-wmi.c b/drivers/misc/acer-wmi.c
index dd13a3749927..e7a3fe508dff 100644
--- a/drivers/misc/acer-wmi.c
+++ b/drivers/misc/acer-wmi.c
@@ -22,18 +22,18 @@
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */ 23 */
24 24
25#define ACER_WMI_VERSION "0.1"
26
27#include <linux/kernel.h> 25#include <linux/kernel.h>
28#include <linux/module.h> 26#include <linux/module.h>
29#include <linux/init.h> 27#include <linux/init.h>
30#include <linux/types.h> 28#include <linux/types.h>
31#include <linux/dmi.h> 29#include <linux/dmi.h>
30#include <linux/fb.h>
32#include <linux/backlight.h> 31#include <linux/backlight.h>
33#include <linux/leds.h> 32#include <linux/leds.h>
34#include <linux/platform_device.h> 33#include <linux/platform_device.h>
35#include <linux/acpi.h> 34#include <linux/acpi.h>
36#include <linux/i8042.h> 35#include <linux/i8042.h>
36#include <linux/debugfs.h>
37 37
38#include <acpi/acpi_drivers.h> 38#include <acpi/acpi_drivers.h>
39 39
@@ -87,6 +87,7 @@ struct acer_quirks {
87 * Acer ACPI method GUIDs 87 * Acer ACPI method GUIDs
88 */ 88 */
89#define AMW0_GUID1 "67C3371D-95A3-4C37-BB61-DD47B491DAAB" 89#define AMW0_GUID1 "67C3371D-95A3-4C37-BB61-DD47B491DAAB"
90#define AMW0_GUID2 "431F16ED-0C2B-444C-B267-27DEB140CF9C"
90#define WMID_GUID1 "6AF4F258-B401-42fd-BE91-3D4AC2D7C0D3" 91#define WMID_GUID1 "6AF4F258-B401-42fd-BE91-3D4AC2D7C0D3"
91#define WMID_GUID2 "95764E09-FB56-4e83-B31A-37761F60994A" 92#define WMID_GUID2 "95764E09-FB56-4e83-B31A-37761F60994A"
92 93
@@ -150,6 +151,12 @@ struct acer_data {
150 int brightness; 151 int brightness;
151}; 152};
152 153
154struct acer_debug {
155 struct dentry *root;
156 struct dentry *devices;
157 u32 wmid_devices;
158};
159
153/* Each low-level interface must define at least some of the following */ 160/* Each low-level interface must define at least some of the following */
154struct wmi_interface { 161struct wmi_interface {
155 /* The WMI device type */ 162 /* The WMI device type */
@@ -160,6 +167,9 @@ struct wmi_interface {
160 167
161 /* Private data for the current interface */ 168 /* Private data for the current interface */
162 struct acer_data data; 169 struct acer_data data;
170
171 /* debugfs entries associated with this interface */
172 struct acer_debug debug;
163}; 173};
164 174
165/* The static interface pointer, points to the currently detected interface */ 175/* The static interface pointer, points to the currently detected interface */
@@ -174,7 +184,7 @@ static struct wmi_interface *interface;
174struct quirk_entry { 184struct quirk_entry {
175 u8 wireless; 185 u8 wireless;
176 u8 mailled; 186 u8 mailled;
177 u8 brightness; 187 s8 brightness;
178 u8 bluetooth; 188 u8 bluetooth;
179}; 189};
180 190
@@ -198,6 +208,10 @@ static int dmi_matched(const struct dmi_system_id *dmi)
198static struct quirk_entry quirk_unknown = { 208static struct quirk_entry quirk_unknown = {
199}; 209};
200 210
211static struct quirk_entry quirk_acer_aspire_1520 = {
212 .brightness = -1,
213};
214
201static struct quirk_entry quirk_acer_travelmate_2490 = { 215static struct quirk_entry quirk_acer_travelmate_2490 = {
202 .mailled = 1, 216 .mailled = 1,
203}; 217};
@@ -207,9 +221,31 @@ static struct quirk_entry quirk_medion_md_98300 = {
207 .wireless = 1, 221 .wireless = 1,
208}; 222};
209 223
224static struct quirk_entry quirk_fujitsu_amilo_li_1718 = {
225 .wireless = 2,
226};
227
210static struct dmi_system_id acer_quirks[] = { 228static struct dmi_system_id acer_quirks[] = {
211 { 229 {
212 .callback = dmi_matched, 230 .callback = dmi_matched,
231 .ident = "Acer Aspire 1360",
232 .matches = {
233 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
234 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1360"),
235 },
236 .driver_data = &quirk_acer_aspire_1520,
237 },
238 {
239 .callback = dmi_matched,
240 .ident = "Acer Aspire 1520",
241 .matches = {
242 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
243 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1520"),
244 },
245 .driver_data = &quirk_acer_aspire_1520,
246 },
247 {
248 .callback = dmi_matched,
213 .ident = "Acer Aspire 3100", 249 .ident = "Acer Aspire 3100",
214 .matches = { 250 .matches = {
215 DMI_MATCH(DMI_SYS_VENDOR, "Acer"), 251 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
@@ -300,6 +336,15 @@ static struct dmi_system_id acer_quirks[] = {
300 }, 336 },
301 { 337 {
302 .callback = dmi_matched, 338 .callback = dmi_matched,
339 .ident = "Fujitsu Siemens Amilo Li 1718",
340 .matches = {
341 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
342 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Li 1718"),
343 },
344 .driver_data = &quirk_fujitsu_amilo_li_1718,
345 },
346 {
347 .callback = dmi_matched,
303 .ident = "Medion MD 98300", 348 .ident = "Medion MD 98300",
304 .matches = { 349 .matches = {
305 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"), 350 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
@@ -393,6 +438,12 @@ struct wmi_interface *iface)
393 return AE_ERROR; 438 return AE_ERROR;
394 *value = result & 0x1; 439 *value = result & 0x1;
395 return AE_OK; 440 return AE_OK;
441 case 2:
442 err = ec_read(0x71, &result);
443 if (err)
444 return AE_ERROR;
445 *value = result & 0x1;
446 return AE_OK;
396 default: 447 default:
397 err = ec_read(0xA, &result); 448 err = ec_read(0xA, &result);
398 if (err) 449 if (err)
@@ -506,6 +557,15 @@ static acpi_status AMW0_set_capabilities(void)
506 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; 557 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
507 union acpi_object *obj; 558 union acpi_object *obj;
508 559
560 /*
561 * On laptops with this strange GUID (non Acer), normal probing doesn't
562 * work.
563 */
564 if (wmi_has_guid(AMW0_GUID2)) {
565 interface->capability |= ACER_CAP_WIRELESS;
566 return AE_OK;
567 }
568
509 args.eax = ACER_AMW0_WRITE; 569 args.eax = ACER_AMW0_WRITE;
510 args.ecx = args.edx = 0; 570 args.ecx = args.edx = 0;
511 571
@@ -552,7 +612,8 @@ static acpi_status AMW0_set_capabilities(void)
552 * appear to use the same EC register for brightness, even if they 612 * appear to use the same EC register for brightness, even if they
553 * differ for wireless, etc 613 * differ for wireless, etc
554 */ 614 */
555 interface->capability |= ACER_CAP_BRIGHTNESS; 615 if (quirks->brightness >= 0)
616 interface->capability |= ACER_CAP_BRIGHTNESS;
556 617
557 return AE_OK; 618 return AE_OK;
558} 619}
@@ -807,7 +868,15 @@ static int read_brightness(struct backlight_device *bd)
807 868
808static int update_bl_status(struct backlight_device *bd) 869static int update_bl_status(struct backlight_device *bd)
809{ 870{
810 set_u32(bd->props.brightness, ACER_CAP_BRIGHTNESS); 871 int intensity = bd->props.brightness;
872
873 if (bd->props.power != FB_BLANK_UNBLANK)
874 intensity = 0;
875 if (bd->props.fb_blank != FB_BLANK_UNBLANK)
876 intensity = 0;
877
878 set_u32(intensity, ACER_CAP_BRIGHTNESS);
879
811 return 0; 880 return 0;
812} 881}
813 882
@@ -829,8 +898,9 @@ static int __devinit acer_backlight_init(struct device *dev)
829 898
830 acer_backlight_device = bd; 899 acer_backlight_device = bd;
831 900
901 bd->props.power = FB_BLANK_UNBLANK;
902 bd->props.brightness = max_brightness;
832 bd->props.max_brightness = max_brightness; 903 bd->props.max_brightness = max_brightness;
833 bd->props.brightness = read_brightness(NULL);
834 backlight_update_status(bd); 904 backlight_update_status(bd);
835 return 0; 905 return 0;
836} 906}
@@ -894,6 +964,28 @@ static DEVICE_ATTR(interface, S_IWUGO | S_IRUGO | S_IWUSR,
894 show_interface, NULL); 964 show_interface, NULL);
895 965
896/* 966/*
967 * debugfs functions
968 */
969static u32 get_wmid_devices(void)
970{
971 struct acpi_buffer out = {ACPI_ALLOCATE_BUFFER, NULL};
972 union acpi_object *obj;
973 acpi_status status;
974
975 status = wmi_query_block(WMID_GUID2, 1, &out);
976 if (ACPI_FAILURE(status))
977 return 0;
978
979 obj = (union acpi_object *) out.pointer;
980 if (obj && obj->type == ACPI_TYPE_BUFFER &&
981 obj->buffer.length == sizeof(u32)) {
982 return *((u32 *) obj->buffer.pointer);
983 } else {
984 return 0;
985 }
986}
987
988/*
897 * Platform device 989 * Platform device
898 */ 990 */
899static int __devinit acer_platform_probe(struct platform_device *device) 991static int __devinit acer_platform_probe(struct platform_device *device)
@@ -1052,12 +1144,40 @@ error_sysfs:
1052 return retval; 1144 return retval;
1053} 1145}
1054 1146
1147static void remove_debugfs(void)
1148{
1149 debugfs_remove(interface->debug.devices);
1150 debugfs_remove(interface->debug.root);
1151}
1152
1153static int create_debugfs(void)
1154{
1155 interface->debug.root = debugfs_create_dir("acer-wmi", NULL);
1156 if (!interface->debug.root) {
1157 printk(ACER_ERR "Failed to create debugfs directory");
1158 return -ENOMEM;
1159 }
1160
1161 interface->debug.devices = debugfs_create_u32("devices", S_IRUGO,
1162 interface->debug.root,
1163 &interface->debug.wmid_devices);
1164 if (!interface->debug.devices)
1165 goto error_debugfs;
1166
1167 return 0;
1168
1169error_debugfs:
1170 remove_debugfs();
1171 return -ENOMEM;
1172}
1173
1055static int __init acer_wmi_init(void) 1174static int __init acer_wmi_init(void)
1056{ 1175{
1057 int err; 1176 int err;
1058 1177
1059 printk(ACER_INFO "Acer Laptop ACPI-WMI Extras version %s\n", 1178 printk(ACER_INFO "Acer Laptop ACPI-WMI Extras\n");
1060 ACER_WMI_VERSION); 1179
1180 find_quirks();
1061 1181
1062 /* 1182 /*
1063 * Detect which ACPI-WMI interface we're using. 1183 * Detect which ACPI-WMI interface we're using.
@@ -1092,8 +1212,6 @@ static int __init acer_wmi_init(void)
1092 if (wmi_has_guid(AMW0_GUID1)) 1212 if (wmi_has_guid(AMW0_GUID1))
1093 AMW0_find_mailled(); 1213 AMW0_find_mailled();
1094 1214
1095 find_quirks();
1096
1097 if (!interface) { 1215 if (!interface) {
1098 printk(ACER_ERR "No or unsupported WMI interface, unable to " 1216 printk(ACER_ERR "No or unsupported WMI interface, unable to "
1099 "load\n"); 1217 "load\n");
@@ -1111,6 +1229,13 @@ static int __init acer_wmi_init(void)
1111 if (err) 1229 if (err)
1112 return err; 1230 return err;
1113 1231
1232 if (wmi_has_guid(WMID_GUID2)) {
1233 interface->debug.wmid_devices = get_wmid_devices();
1234 err = create_debugfs();
1235 if (err)
1236 return err;
1237 }
1238
1114 /* Override any initial settings with values from the commandline */ 1239 /* Override any initial settings with values from the commandline */
1115 acer_commandline_init(); 1240 acer_commandline_init();
1116 1241
diff --git a/drivers/misc/compal-laptop.c b/drivers/misc/compal-laptop.c
new file mode 100644
index 000000000000..344b790a6253
--- /dev/null
+++ b/drivers/misc/compal-laptop.c
@@ -0,0 +1,404 @@
1/*-*-linux-c-*-*/
2
3/*
4 Copyright (C) 2008 Cezary Jackiewicz <cezary.jackiewicz (at) gmail.com>
5
6 based on MSI driver
7
8 Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA.
24 */
25
26/*
27 * comapl-laptop.c - Compal laptop support.
28 *
29 * This driver exports a few files in /sys/devices/platform/compal-laptop/:
30 *
31 * wlan - wlan subsystem state: contains 0 or 1 (rw)
32 *
33 * bluetooth - Bluetooth subsystem state: contains 0 or 1 (rw)
34 *
35 * raw - raw value taken from embedded controller register (ro)
36 *
37 * In addition to these platform device attributes the driver
38 * registers itself in the Linux backlight control subsystem and is
39 * available to userspace under /sys/class/backlight/compal-laptop/.
40 *
41 * This driver might work on other laptops produced by Compal. If you
42 * want to try it you can pass force=1 as argument to the module which
43 * will force it to load even when the DMI data doesn't identify the
44 * laptop as FL9x.
45 */
46
47#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/init.h>
50#include <linux/acpi.h>
51#include <linux/dmi.h>
52#include <linux/backlight.h>
53#include <linux/platform_device.h>
54#include <linux/autoconf.h>
55
56#define COMPAL_DRIVER_VERSION "0.2.6"
57
58#define COMPAL_LCD_LEVEL_MAX 8
59
60#define COMPAL_EC_COMMAND_WIRELESS 0xBB
61#define COMPAL_EC_COMMAND_LCD_LEVEL 0xB9
62
63#define KILLSWITCH_MASK 0x10
64#define WLAN_MASK 0x01
65#define BT_MASK 0x02
66
67static int force;
68module_param(force, bool, 0);
69MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
70
71/* Hardware access */
72
73static int set_lcd_level(int level)
74{
75 if (level < 0 || level >= COMPAL_LCD_LEVEL_MAX)
76 return -EINVAL;
77
78 ec_write(COMPAL_EC_COMMAND_LCD_LEVEL, level);
79
80 return 0;
81}
82
83static int get_lcd_level(void)
84{
85 u8 result;
86
87 ec_read(COMPAL_EC_COMMAND_LCD_LEVEL, &result);
88
89 return (int) result;
90}
91
92static int set_wlan_state(int state)
93{
94 u8 result, value;
95
96 ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
97
98 if ((result & KILLSWITCH_MASK) == 0)
99 return -EINVAL;
100 else {
101 if (state)
102 value = (u8) (result | WLAN_MASK);
103 else
104 value = (u8) (result & ~WLAN_MASK);
105 ec_write(COMPAL_EC_COMMAND_WIRELESS, value);
106 }
107
108 return 0;
109}
110
111static int set_bluetooth_state(int state)
112{
113 u8 result, value;
114
115 ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
116
117 if ((result & KILLSWITCH_MASK) == 0)
118 return -EINVAL;
119 else {
120 if (state)
121 value = (u8) (result | BT_MASK);
122 else
123 value = (u8) (result & ~BT_MASK);
124 ec_write(COMPAL_EC_COMMAND_WIRELESS, value);
125 }
126
127 return 0;
128}
129
130static int get_wireless_state(int *wlan, int *bluetooth)
131{
132 u8 result;
133
134 ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
135
136 if (wlan) {
137 if ((result & KILLSWITCH_MASK) == 0)
138 *wlan = 0;
139 else
140 *wlan = result & WLAN_MASK;
141 }
142
143 if (bluetooth) {
144 if ((result & KILLSWITCH_MASK) == 0)
145 *bluetooth = 0;
146 else
147 *bluetooth = (result & BT_MASK) >> 1;
148 }
149
150 return 0;
151}
152
153/* Backlight device stuff */
154
155static int bl_get_brightness(struct backlight_device *b)
156{
157 return get_lcd_level();
158}
159
160
161static int bl_update_status(struct backlight_device *b)
162{
163 return set_lcd_level(b->props.brightness);
164}
165
166static struct backlight_ops compalbl_ops = {
167 .get_brightness = bl_get_brightness,
168 .update_status = bl_update_status,
169};
170
171static struct backlight_device *compalbl_device;
172
173/* Platform device */
174
175static ssize_t show_wlan(struct device *dev,
176 struct device_attribute *attr, char *buf)
177{
178 int ret, enabled;
179
180 ret = get_wireless_state(&enabled, NULL);
181 if (ret < 0)
182 return ret;
183
184 return sprintf(buf, "%i\n", enabled);
185}
186
187static ssize_t show_raw(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
190 u8 result;
191
192 ec_read(COMPAL_EC_COMMAND_WIRELESS, &result);
193
194 return sprintf(buf, "%i\n", result);
195}
196
197static ssize_t show_bluetooth(struct device *dev,
198 struct device_attribute *attr, char *buf)
199{
200 int ret, enabled;
201
202 ret = get_wireless_state(NULL, &enabled);
203 if (ret < 0)
204 return ret;
205
206 return sprintf(buf, "%i\n", enabled);
207}
208
209static ssize_t store_wlan_state(struct device *dev,
210 struct device_attribute *attr, const char *buf, size_t count)
211{
212 int state, ret;
213
214 if (sscanf(buf, "%i", &state) != 1 || (state < 0 || state > 1))
215 return -EINVAL;
216
217 ret = set_wlan_state(state);
218 if (ret < 0)
219 return ret;
220
221 return count;
222}
223
224static ssize_t store_bluetooth_state(struct device *dev,
225 struct device_attribute *attr, const char *buf, size_t count)
226{
227 int state, ret;
228
229 if (sscanf(buf, "%i", &state) != 1 || (state < 0 || state > 1))
230 return -EINVAL;
231
232 ret = set_bluetooth_state(state);
233 if (ret < 0)
234 return ret;
235
236 return count;
237}
238
239static DEVICE_ATTR(bluetooth, 0644, show_bluetooth, store_bluetooth_state);
240static DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan_state);
241static DEVICE_ATTR(raw, 0444, show_raw, NULL);
242
243static struct attribute *compal_attributes[] = {
244 &dev_attr_bluetooth.attr,
245 &dev_attr_wlan.attr,
246 &dev_attr_raw.attr,
247 NULL
248};
249
250static struct attribute_group compal_attribute_group = {
251 .attrs = compal_attributes
252};
253
254static struct platform_driver compal_driver = {
255 .driver = {
256 .name = "compal-laptop",
257 .owner = THIS_MODULE,
258 }
259};
260
261static struct platform_device *compal_device;
262
263/* Initialization */
264
265static int dmi_check_cb(const struct dmi_system_id *id)
266{
267 printk(KERN_INFO "compal-laptop: Identified laptop model '%s'.\n",
268 id->ident);
269
270 return 0;
271}
272
273static struct dmi_system_id __initdata compal_dmi_table[] = {
274 {
275 .ident = "FL90/IFL90",
276 .matches = {
277 DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
278 DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
279 },
280 .callback = dmi_check_cb
281 },
282 {
283 .ident = "FL90/IFL90",
284 .matches = {
285 DMI_MATCH(DMI_BOARD_NAME, "IFL90"),
286 DMI_MATCH(DMI_BOARD_VERSION, "REFERENCE"),
287 },
288 .callback = dmi_check_cb
289 },
290 {
291 .ident = "FL91/IFL91",
292 .matches = {
293 DMI_MATCH(DMI_BOARD_NAME, "IFL91"),
294 DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
295 },
296 .callback = dmi_check_cb
297 },
298 {
299 .ident = "FL92/JFL92",
300 .matches = {
301 DMI_MATCH(DMI_BOARD_NAME, "JFL92"),
302 DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
303 },
304 .callback = dmi_check_cb
305 },
306 {
307 .ident = "FT00/IFT00",
308 .matches = {
309 DMI_MATCH(DMI_BOARD_NAME, "IFT00"),
310 DMI_MATCH(DMI_BOARD_VERSION, "IFT00"),
311 },
312 .callback = dmi_check_cb
313 },
314 { }
315};
316
317static int __init compal_init(void)
318{
319 int ret;
320
321 if (acpi_disabled)
322 return -ENODEV;
323
324 if (!force && !dmi_check_system(compal_dmi_table))
325 return -ENODEV;
326
327 /* Register backlight stuff */
328
329 compalbl_device = backlight_device_register("compal-laptop", NULL, NULL,
330 &compalbl_ops);
331 if (IS_ERR(compalbl_device))
332 return PTR_ERR(compalbl_device);
333
334 compalbl_device->props.max_brightness = COMPAL_LCD_LEVEL_MAX-1;
335
336 ret = platform_driver_register(&compal_driver);
337 if (ret)
338 goto fail_backlight;
339
340 /* Register platform stuff */
341
342 compal_device = platform_device_alloc("compal-laptop", -1);
343 if (!compal_device) {
344 ret = -ENOMEM;
345 goto fail_platform_driver;
346 }
347
348 ret = platform_device_add(compal_device);
349 if (ret)
350 goto fail_platform_device1;
351
352 ret = sysfs_create_group(&compal_device->dev.kobj,
353 &compal_attribute_group);
354 if (ret)
355 goto fail_platform_device2;
356
357 printk(KERN_INFO "compal-laptop: driver "COMPAL_DRIVER_VERSION
358 " successfully loaded.\n");
359
360 return 0;
361
362fail_platform_device2:
363
364 platform_device_del(compal_device);
365
366fail_platform_device1:
367
368 platform_device_put(compal_device);
369
370fail_platform_driver:
371
372 platform_driver_unregister(&compal_driver);
373
374fail_backlight:
375
376 backlight_device_unregister(compalbl_device);
377
378 return ret;
379}
380
381static void __exit compal_cleanup(void)
382{
383
384 sysfs_remove_group(&compal_device->dev.kobj, &compal_attribute_group);
385 platform_device_unregister(compal_device);
386 platform_driver_unregister(&compal_driver);
387 backlight_device_unregister(compalbl_device);
388
389 printk(KERN_INFO "compal-laptop: driver unloaded.\n");
390}
391
392module_init(compal_init);
393module_exit(compal_cleanup);
394
395MODULE_AUTHOR("Cezary Jackiewicz");
396MODULE_DESCRIPTION("Compal Laptop Support");
397MODULE_VERSION(COMPAL_DRIVER_VERSION);
398MODULE_LICENSE("GPL");
399
400MODULE_ALIAS("dmi:*:rnIFL90:rvrIFT00:*");
401MODULE_ALIAS("dmi:*:rnIFL90:rvrREFERENCE:*");
402MODULE_ALIAS("dmi:*:rnIFL91:rvrIFT00:*");
403MODULE_ALIAS("dmi:*:rnJFL92:rvrIFT00:*");
404MODULE_ALIAS("dmi:*:rnIFT00:rvrIFT00:*");
diff --git a/drivers/misc/eeepc-laptop.c b/drivers/misc/eeepc-laptop.c
index 6d727609097f..9e8d79e7e9f4 100644
--- a/drivers/misc/eeepc-laptop.c
+++ b/drivers/misc/eeepc-laptop.c
@@ -87,7 +87,7 @@ enum {
87 CM_ASL_LID 87 CM_ASL_LID
88}; 88};
89 89
90const char *cm_getv[] = { 90static const char *cm_getv[] = {
91 "WLDG", NULL, NULL, NULL, 91 "WLDG", NULL, NULL, NULL,
92 "CAMG", NULL, NULL, NULL, 92 "CAMG", NULL, NULL, NULL,
93 NULL, "PBLG", NULL, NULL, 93 NULL, "PBLG", NULL, NULL,
@@ -96,7 +96,7 @@ const char *cm_getv[] = {
96 "CRDG", "LIDG" 96 "CRDG", "LIDG"
97}; 97};
98 98
99const char *cm_setv[] = { 99static const char *cm_setv[] = {
100 "WLDS", NULL, NULL, NULL, 100 "WLDS", NULL, NULL, NULL,
101 "CAMS", NULL, NULL, NULL, 101 "CAMS", NULL, NULL, NULL,
102 "SDSP", "PBLS", "HDPS", NULL, 102 "SDSP", "PBLS", "HDPS", NULL,
diff --git a/drivers/misc/fujitsu-laptop.c b/drivers/misc/fujitsu-laptop.c
index 6d14e8fe1537..7a1ef6c262de 100644
--- a/drivers/misc/fujitsu-laptop.c
+++ b/drivers/misc/fujitsu-laptop.c
@@ -1,12 +1,14 @@
1/*-*-linux-c-*-*/ 1/*-*-linux-c-*-*/
2 2
3/* 3/*
4 Copyright (C) 2007 Jonathan Woithe <jwoithe@physics.adelaide.edu.au> 4 Copyright (C) 2007,2008 Jonathan Woithe <jwoithe@physics.adelaide.edu.au>
5 Copyright (C) 2008 Peter Gruber <nokos@gmx.net>
5 Based on earlier work: 6 Based on earlier work:
6 Copyright (C) 2003 Shane Spencer <shane@bogomip.com> 7 Copyright (C) 2003 Shane Spencer <shane@bogomip.com>
7 Adrian Yee <brewt-fujitsu@brewt.org> 8 Adrian Yee <brewt-fujitsu@brewt.org>
8 9
9 Templated from msi-laptop.c which is copyright by its respective authors. 10 Templated from msi-laptop.c and thinkpad_acpi.c which is copyright
11 by its respective authors.
10 12
11 This program is free software; you can redistribute it and/or modify 13 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by 14 it under the terms of the GNU General Public License as published by
@@ -39,8 +41,17 @@
39 * registers itself in the Linux backlight control subsystem and is 41 * registers itself in the Linux backlight control subsystem and is
40 * available to userspace under /sys/class/backlight/fujitsu-laptop/. 42 * available to userspace under /sys/class/backlight/fujitsu-laptop/.
41 * 43 *
42 * This driver has been tested on a Fujitsu Lifebook S7020. It should 44 * Hotkeys present on certain Fujitsu laptops (eg: the S6xxx series) are
43 * work on most P-series and S-series Lifebooks, but YMMV. 45 * also supported by this driver.
46 *
47 * This driver has been tested on a Fujitsu Lifebook S6410 and S7020. It
48 * should work on most P-series and S-series Lifebooks, but YMMV.
49 *
50 * The module parameter use_alt_lcd_levels switches between different ACPI
51 * brightness controls which are used by different Fujitsu laptops. In most
52 * cases the correct method is automatically detected. "use_alt_lcd_levels=1"
53 * is applicable for a Fujitsu Lifebook S6410 if autodetection fails.
54 *
44 */ 55 */
45 56
46#include <linux/module.h> 57#include <linux/module.h>
@@ -49,30 +60,105 @@
49#include <linux/acpi.h> 60#include <linux/acpi.h>
50#include <linux/dmi.h> 61#include <linux/dmi.h>
51#include <linux/backlight.h> 62#include <linux/backlight.h>
63#include <linux/input.h>
64#include <linux/kfifo.h>
65#include <linux/video_output.h>
52#include <linux/platform_device.h> 66#include <linux/platform_device.h>
53 67
54#define FUJITSU_DRIVER_VERSION "0.3" 68#define FUJITSU_DRIVER_VERSION "0.4.2"
55 69
56#define FUJITSU_LCD_N_LEVELS 8 70#define FUJITSU_LCD_N_LEVELS 8
57 71
58#define ACPI_FUJITSU_CLASS "fujitsu" 72#define ACPI_FUJITSU_CLASS "fujitsu"
59#define ACPI_FUJITSU_HID "FUJ02B1" 73#define ACPI_FUJITSU_HID "FUJ02B1"
60#define ACPI_FUJITSU_DRIVER_NAME "Fujitsu laptop FUJ02B1 ACPI extras driver" 74#define ACPI_FUJITSU_DRIVER_NAME "Fujitsu laptop FUJ02B1 ACPI brightness driver"
61#define ACPI_FUJITSU_DEVICE_NAME "Fujitsu FUJ02B1" 75#define ACPI_FUJITSU_DEVICE_NAME "Fujitsu FUJ02B1"
62 76#define ACPI_FUJITSU_HOTKEY_HID "FUJ02E3"
77#define ACPI_FUJITSU_HOTKEY_DRIVER_NAME "Fujitsu laptop FUJ02E3 ACPI hotkeys driver"
78#define ACPI_FUJITSU_HOTKEY_DEVICE_NAME "Fujitsu FUJ02E3"
79
80#define ACPI_FUJITSU_NOTIFY_CODE1 0x80
81
82#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
83#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
84
85/* Hotkey details */
86#define LOCK_KEY 0x410 /* codes for the keys in the GIRB register */
87#define DISPLAY_KEY 0x411 /* keys are mapped to KEY_SCREENLOCK (the key with the key symbol) */
88#define ENERGY_KEY 0x412 /* KEY_MEDIA (the key with the laptop symbol, KEY_EMAIL (E key)) */
89#define REST_KEY 0x413 /* KEY_SUSPEND (R key) */
90
91#define MAX_HOTKEY_RINGBUFFER_SIZE 100
92#define RINGBUFFERSIZE 40
93
94/* Debugging */
95#define FUJLAPTOP_LOG ACPI_FUJITSU_HID ": "
96#define FUJLAPTOP_ERR KERN_ERR FUJLAPTOP_LOG
97#define FUJLAPTOP_NOTICE KERN_NOTICE FUJLAPTOP_LOG
98#define FUJLAPTOP_INFO KERN_INFO FUJLAPTOP_LOG
99#define FUJLAPTOP_DEBUG KERN_DEBUG FUJLAPTOP_LOG
100
101#define FUJLAPTOP_DBG_ALL 0xffff
102#define FUJLAPTOP_DBG_ERROR 0x0001
103#define FUJLAPTOP_DBG_WARN 0x0002
104#define FUJLAPTOP_DBG_INFO 0x0004
105#define FUJLAPTOP_DBG_TRACE 0x0008
106
107#define dbg_printk(a_dbg_level, format, arg...) \
108 do { if (dbg_level & a_dbg_level) \
109 printk(FUJLAPTOP_DEBUG "%s: " format, __func__ , ## arg); \
110 } while (0)
111#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
112#define vdbg_printk(a_dbg_level, format, arg...) \
113 dbg_printk(a_dbg_level, format, ## arg)
114#else
115#define vdbg_printk(a_dbg_level, format, arg...)
116#endif
117
118/* Device controlling the backlight and associated keys */
63struct fujitsu_t { 119struct fujitsu_t {
64 acpi_handle acpi_handle; 120 acpi_handle acpi_handle;
121 struct acpi_device *dev;
122 struct input_dev *input;
123 char phys[32];
65 struct backlight_device *bl_device; 124 struct backlight_device *bl_device;
66 struct platform_device *pf_device; 125 struct platform_device *pf_device;
67 126
68 unsigned long fuj02b1_state; 127 unsigned int max_brightness;
69 unsigned int brightness_changed; 128 unsigned int brightness_changed;
70 unsigned int brightness_level; 129 unsigned int brightness_level;
71}; 130};
72 131
73static struct fujitsu_t *fujitsu; 132static struct fujitsu_t *fujitsu;
133static int use_alt_lcd_levels = -1;
134static int disable_brightness_keys = -1;
135static int disable_brightness_adjust = -1;
136
137/* Device used to access other hotkeys on the laptop */
138struct fujitsu_hotkey_t {
139 acpi_handle acpi_handle;
140 struct acpi_device *dev;
141 struct input_dev *input;
142 char phys[32];
143 struct platform_device *pf_device;
144 struct kfifo *fifo;
145 spinlock_t fifo_lock;
146
147 unsigned int irb; /* info about the pressed buttons */
148};
74 149
75/* Hardware access */ 150static struct fujitsu_hotkey_t *fujitsu_hotkey;
151
152static void acpi_fujitsu_hotkey_notify(acpi_handle handle, u32 event,
153 void *data);
154
155#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
156static u32 dbg_level = 0x03;
157#endif
158
159static void acpi_fujitsu_notify(acpi_handle handle, u32 event, void *data);
160
161/* Hardware access for LCD brightness control */
76 162
77static int set_lcd_level(int level) 163static int set_lcd_level(int level)
78{ 164{
@@ -81,7 +167,10 @@ static int set_lcd_level(int level)
81 struct acpi_object_list arg_list = { 1, &arg0 }; 167 struct acpi_object_list arg_list = { 1, &arg0 };
82 acpi_handle handle = NULL; 168 acpi_handle handle = NULL;
83 169
84 if (level < 0 || level >= FUJITSU_LCD_N_LEVELS) 170 vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via SBLL [%d]\n",
171 level);
172
173 if (level < 0 || level >= fujitsu->max_brightness)
85 return -EINVAL; 174 return -EINVAL;
86 175
87 if (!fujitsu) 176 if (!fujitsu)
@@ -89,7 +178,38 @@ static int set_lcd_level(int level)
89 178
90 status = acpi_get_handle(fujitsu->acpi_handle, "SBLL", &handle); 179 status = acpi_get_handle(fujitsu->acpi_handle, "SBLL", &handle);
91 if (ACPI_FAILURE(status)) { 180 if (ACPI_FAILURE(status)) {
92 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "SBLL not present\n")); 181 vdbg_printk(FUJLAPTOP_DBG_ERROR, "SBLL not present\n");
182 return -ENODEV;
183 }
184
185 arg0.integer.value = level;
186
187 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
188 if (ACPI_FAILURE(status))
189 return -ENODEV;
190
191 return 0;
192}
193
194static int set_lcd_level_alt(int level)
195{
196 acpi_status status = AE_OK;
197 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
198 struct acpi_object_list arg_list = { 1, &arg0 };
199 acpi_handle handle = NULL;
200
201 vdbg_printk(FUJLAPTOP_DBG_TRACE, "set lcd level via SBL2 [%d]\n",
202 level);
203
204 if (level < 0 || level >= fujitsu->max_brightness)
205 return -EINVAL;
206
207 if (!fujitsu)
208 return -EINVAL;
209
210 status = acpi_get_handle(fujitsu->acpi_handle, "SBL2", &handle);
211 if (ACPI_FAILURE(status)) {
212 vdbg_printk(FUJLAPTOP_DBG_ERROR, "SBL2 not present\n");
93 return -ENODEV; 213 return -ENODEV;
94 } 214 }
95 215
@@ -107,13 +227,52 @@ static int get_lcd_level(void)
107 unsigned long state = 0; 227 unsigned long state = 0;
108 acpi_status status = AE_OK; 228 acpi_status status = AE_OK;
109 229
110 // Get the Brightness 230 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLL\n");
231
111 status = 232 status =
112 acpi_evaluate_integer(fujitsu->acpi_handle, "GBLL", NULL, &state); 233 acpi_evaluate_integer(fujitsu->acpi_handle, "GBLL", NULL, &state);
113 if (status < 0) 234 if (status < 0)
114 return status; 235 return status;
115 236
116 fujitsu->fuj02b1_state = state; 237 fujitsu->brightness_level = state & 0x0fffffff;
238
239 if (state & 0x80000000)
240 fujitsu->brightness_changed = 1;
241 else
242 fujitsu->brightness_changed = 0;
243
244 return fujitsu->brightness_level;
245}
246
247static int get_max_brightness(void)
248{
249 unsigned long state = 0;
250 acpi_status status = AE_OK;
251
252 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get max lcd level via RBLL\n");
253
254 status =
255 acpi_evaluate_integer(fujitsu->acpi_handle, "RBLL", NULL, &state);
256 if (status < 0)
257 return status;
258
259 fujitsu->max_brightness = state;
260
261 return fujitsu->max_brightness;
262}
263
264static int get_lcd_level_alt(void)
265{
266 unsigned long state = 0;
267 acpi_status status = AE_OK;
268
269 vdbg_printk(FUJLAPTOP_DBG_TRACE, "get lcd level via GBLS\n");
270
271 status =
272 acpi_evaluate_integer(fujitsu->acpi_handle, "GBLS", NULL, &state);
273 if (status < 0)
274 return status;
275
117 fujitsu->brightness_level = state & 0x0fffffff; 276 fujitsu->brightness_level = state & 0x0fffffff;
118 277
119 if (state & 0x80000000) 278 if (state & 0x80000000)
@@ -128,12 +287,18 @@ static int get_lcd_level(void)
128 287
129static int bl_get_brightness(struct backlight_device *b) 288static int bl_get_brightness(struct backlight_device *b)
130{ 289{
131 return get_lcd_level(); 290 if (use_alt_lcd_levels)
291 return get_lcd_level_alt();
292 else
293 return get_lcd_level();
132} 294}
133 295
134static int bl_update_status(struct backlight_device *b) 296static int bl_update_status(struct backlight_device *b)
135{ 297{
136 return set_lcd_level(b->props.brightness); 298 if (use_alt_lcd_levels)
299 return set_lcd_level_alt(b->props.brightness);
300 else
301 return set_lcd_level(b->props.brightness);
137} 302}
138 303
139static struct backlight_ops fujitsubl_ops = { 304static struct backlight_ops fujitsubl_ops = {
@@ -141,7 +306,35 @@ static struct backlight_ops fujitsubl_ops = {
141 .update_status = bl_update_status, 306 .update_status = bl_update_status,
142}; 307};
143 308
144/* Platform device */ 309/* Platform LCD brightness device */
310
311static ssize_t
312show_max_brightness(struct device *dev,
313 struct device_attribute *attr, char *buf)
314{
315
316 int ret;
317
318 ret = get_max_brightness();
319 if (ret < 0)
320 return ret;
321
322 return sprintf(buf, "%i\n", ret);
323}
324
325static ssize_t
326show_brightness_changed(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329
330 int ret;
331
332 ret = fujitsu->brightness_changed;
333 if (ret < 0)
334 return ret;
335
336 return sprintf(buf, "%i\n", ret);
337}
145 338
146static ssize_t show_lcd_level(struct device *dev, 339static ssize_t show_lcd_level(struct device *dev,
147 struct device_attribute *attr, char *buf) 340 struct device_attribute *attr, char *buf)
@@ -149,7 +342,10 @@ static ssize_t show_lcd_level(struct device *dev,
149 342
150 int ret; 343 int ret;
151 344
152 ret = get_lcd_level(); 345 if (use_alt_lcd_levels)
346 ret = get_lcd_level_alt();
347 else
348 ret = get_lcd_level();
153 if (ret < 0) 349 if (ret < 0)
154 return ret; 350 return ret;
155 351
@@ -164,19 +360,61 @@ static ssize_t store_lcd_level(struct device *dev,
164 int level, ret; 360 int level, ret;
165 361
166 if (sscanf(buf, "%i", &level) != 1 362 if (sscanf(buf, "%i", &level) != 1
167 || (level < 0 || level >= FUJITSU_LCD_N_LEVELS)) 363 || (level < 0 || level >= fujitsu->max_brightness))
168 return -EINVAL; 364 return -EINVAL;
169 365
170 ret = set_lcd_level(level); 366 if (use_alt_lcd_levels)
367 ret = set_lcd_level_alt(level);
368 else
369 ret = set_lcd_level(level);
370 if (ret < 0)
371 return ret;
372
373 if (use_alt_lcd_levels)
374 ret = get_lcd_level_alt();
375 else
376 ret = get_lcd_level();
171 if (ret < 0) 377 if (ret < 0)
172 return ret; 378 return ret;
173 379
174 return count; 380 return count;
175} 381}
176 382
383/* Hardware access for hotkey device */
384
385static int get_irb(void)
386{
387 unsigned long state = 0;
388 acpi_status status = AE_OK;
389
390 vdbg_printk(FUJLAPTOP_DBG_TRACE, "Get irb\n");
391
392 status =
393 acpi_evaluate_integer(fujitsu_hotkey->acpi_handle, "GIRB", NULL,
394 &state);
395 if (status < 0)
396 return status;
397
398 fujitsu_hotkey->irb = state;
399
400 return fujitsu_hotkey->irb;
401}
402
403static ssize_t
404ignore_store(struct device *dev,
405 struct device_attribute *attr, const char *buf, size_t count)
406{
407 return count;
408}
409
410static DEVICE_ATTR(max_brightness, 0444, show_max_brightness, ignore_store);
411static DEVICE_ATTR(brightness_changed, 0444, show_brightness_changed,
412 ignore_store);
177static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level); 413static DEVICE_ATTR(lcd_level, 0644, show_lcd_level, store_lcd_level);
178 414
179static struct attribute *fujitsupf_attributes[] = { 415static struct attribute *fujitsupf_attributes[] = {
416 &dev_attr_brightness_changed.attr,
417 &dev_attr_max_brightness.attr,
180 &dev_attr_lcd_level.attr, 418 &dev_attr_lcd_level.attr,
181 NULL 419 NULL
182}; 420};
@@ -192,14 +430,52 @@ static struct platform_driver fujitsupf_driver = {
192 } 430 }
193}; 431};
194 432
195/* ACPI device */ 433static int dmi_check_cb_s6410(const struct dmi_system_id *id)
434{
435 acpi_handle handle;
436 int have_blnf;
437 printk(KERN_INFO "fujitsu-laptop: Identified laptop model '%s'.\n",
438 id->ident);
439 have_blnf = ACPI_SUCCESS
440 (acpi_get_handle(NULL, "\\_SB.PCI0.GFX0.LCD.BLNF", &handle));
441 if (use_alt_lcd_levels == -1) {
442 vdbg_printk(FUJLAPTOP_DBG_TRACE, "auto-detecting usealt\n");
443 use_alt_lcd_levels = 1;
444 }
445 if (disable_brightness_keys == -1) {
446 vdbg_printk(FUJLAPTOP_DBG_TRACE,
447 "auto-detecting disable_keys\n");
448 disable_brightness_keys = have_blnf ? 1 : 0;
449 }
450 if (disable_brightness_adjust == -1) {
451 vdbg_printk(FUJLAPTOP_DBG_TRACE,
452 "auto-detecting disable_adjust\n");
453 disable_brightness_adjust = have_blnf ? 0 : 1;
454 }
455 return 0;
456}
457
458static struct dmi_system_id __initdata fujitsu_dmi_table[] = {
459 {
460 .ident = "Fujitsu Siemens",
461 .matches = {
462 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
463 DMI_MATCH(DMI_PRODUCT_NAME, "LIFEBOOK S6410"),
464 },
465 .callback = dmi_check_cb_s6410},
466 {}
467};
468
469/* ACPI device for LCD brightness control */
196 470
197static int acpi_fujitsu_add(struct acpi_device *device) 471static int acpi_fujitsu_add(struct acpi_device *device)
198{ 472{
473 acpi_status status;
474 acpi_handle handle;
199 int result = 0; 475 int result = 0;
200 int state = 0; 476 int state = 0;
201 477 struct input_dev *input;
202 ACPI_FUNCTION_TRACE("acpi_fujitsu_add"); 478 int error;
203 479
204 if (!device) 480 if (!device)
205 return -EINVAL; 481 return -EINVAL;
@@ -209,10 +485,42 @@ static int acpi_fujitsu_add(struct acpi_device *device)
209 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS); 485 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
210 acpi_driver_data(device) = fujitsu; 486 acpi_driver_data(device) = fujitsu;
211 487
488 status = acpi_install_notify_handler(device->handle,
489 ACPI_DEVICE_NOTIFY,
490 acpi_fujitsu_notify, fujitsu);
491
492 if (ACPI_FAILURE(status)) {
493 printk(KERN_ERR "Error installing notify handler\n");
494 error = -ENODEV;
495 goto err_stop;
496 }
497
498 fujitsu->input = input = input_allocate_device();
499 if (!input) {
500 error = -ENOMEM;
501 goto err_uninstall_notify;
502 }
503
504 snprintf(fujitsu->phys, sizeof(fujitsu->phys),
505 "%s/video/input0", acpi_device_hid(device));
506
507 input->name = acpi_device_name(device);
508 input->phys = fujitsu->phys;
509 input->id.bustype = BUS_HOST;
510 input->id.product = 0x06;
511 input->dev.parent = &device->dev;
512 input->evbit[0] = BIT(EV_KEY);
513 set_bit(KEY_BRIGHTNESSUP, input->keybit);
514 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
515 set_bit(KEY_UNKNOWN, input->keybit);
516
517 error = input_register_device(input);
518 if (error)
519 goto err_free_input_dev;
520
212 result = acpi_bus_get_power(fujitsu->acpi_handle, &state); 521 result = acpi_bus_get_power(fujitsu->acpi_handle, &state);
213 if (result) { 522 if (result) {
214 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 523 printk(KERN_ERR "Error reading power state\n");
215 "Error reading power state\n"));
216 goto end; 524 goto end;
217 } 525 }
218 526
@@ -220,22 +528,373 @@ static int acpi_fujitsu_add(struct acpi_device *device)
220 acpi_device_name(device), acpi_device_bid(device), 528 acpi_device_name(device), acpi_device_bid(device),
221 !device->power.state ? "on" : "off"); 529 !device->power.state ? "on" : "off");
222 530
223 end: 531 fujitsu->dev = device;
532
533 if (ACPI_SUCCESS
534 (acpi_get_handle(device->handle, METHOD_NAME__INI, &handle))) {
535 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
536 if (ACPI_FAILURE
537 (acpi_evaluate_object
538 (device->handle, METHOD_NAME__INI, NULL, NULL)))
539 printk(KERN_ERR "_INI Method failed\n");
540 }
541
542 /* do config (detect defaults) */
543 dmi_check_system(fujitsu_dmi_table);
544 use_alt_lcd_levels = use_alt_lcd_levels == 1 ? 1 : 0;
545 disable_brightness_keys = disable_brightness_keys == 1 ? 1 : 0;
546 disable_brightness_adjust = disable_brightness_adjust == 1 ? 1 : 0;
547 vdbg_printk(FUJLAPTOP_DBG_INFO,
548 "config: [alt interface: %d], [key disable: %d], [adjust disable: %d]\n",
549 use_alt_lcd_levels, disable_brightness_keys,
550 disable_brightness_adjust);
551
552 if (get_max_brightness() <= 0)
553 fujitsu->max_brightness = FUJITSU_LCD_N_LEVELS;
554 if (use_alt_lcd_levels)
555 get_lcd_level_alt();
556 else
557 get_lcd_level();
558
559 return result;
560
561end:
562err_free_input_dev:
563 input_free_device(input);
564err_uninstall_notify:
565 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
566 acpi_fujitsu_notify);
567err_stop:
224 568
225 return result; 569 return result;
226} 570}
227 571
228static int acpi_fujitsu_remove(struct acpi_device *device, int type) 572static int acpi_fujitsu_remove(struct acpi_device *device, int type)
229{ 573{
230 ACPI_FUNCTION_TRACE("acpi_fujitsu_remove"); 574 acpi_status status;
575 struct fujitsu_t *fujitsu = NULL;
231 576
232 if (!device || !acpi_driver_data(device)) 577 if (!device || !acpi_driver_data(device))
233 return -EINVAL; 578 return -EINVAL;
579
580 fujitsu = acpi_driver_data(device);
581
582 status = acpi_remove_notify_handler(fujitsu->acpi_handle,
583 ACPI_DEVICE_NOTIFY,
584 acpi_fujitsu_notify);
585
586 if (!device || !acpi_driver_data(device))
587 return -EINVAL;
588
234 fujitsu->acpi_handle = NULL; 589 fujitsu->acpi_handle = NULL;
235 590
236 return 0; 591 return 0;
237} 592}
238 593
594/* Brightness notify */
595
596static void acpi_fujitsu_notify(acpi_handle handle, u32 event, void *data)
597{
598 struct input_dev *input;
599 int keycode;
600 int oldb, newb;
601
602 input = fujitsu->input;
603
604 switch (event) {
605 case ACPI_FUJITSU_NOTIFY_CODE1:
606 keycode = 0;
607 oldb = fujitsu->brightness_level;
608 get_lcd_level(); /* the alt version always yields changed */
609 newb = fujitsu->brightness_level;
610
611 vdbg_printk(FUJLAPTOP_DBG_TRACE,
612 "brightness button event [%i -> %i (%i)]\n",
613 oldb, newb, fujitsu->brightness_changed);
614
615 if (oldb == newb && fujitsu->brightness_changed) {
616 keycode = 0;
617 if (disable_brightness_keys != 1) {
618 if (oldb == 0) {
619 acpi_bus_generate_proc_event(fujitsu->
620 dev,
621 ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS,
622 0);
623 keycode = KEY_BRIGHTNESSDOWN;
624 } else if (oldb ==
625 (fujitsu->max_brightness) - 1) {
626 acpi_bus_generate_proc_event(fujitsu->
627 dev,
628 ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS,
629 0);
630 keycode = KEY_BRIGHTNESSUP;
631 }
632 }
633 } else if (oldb < newb) {
634 if (disable_brightness_adjust != 1) {
635 if (use_alt_lcd_levels)
636 set_lcd_level_alt(newb);
637 else
638 set_lcd_level(newb);
639 }
640 if (disable_brightness_keys != 1) {
641 acpi_bus_generate_proc_event(fujitsu->dev,
642 ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS,
643 0);
644 keycode = KEY_BRIGHTNESSUP;
645 }
646 } else if (oldb > newb) {
647 if (disable_brightness_adjust != 1) {
648 if (use_alt_lcd_levels)
649 set_lcd_level_alt(newb);
650 else
651 set_lcd_level(newb);
652 }
653 if (disable_brightness_keys != 1) {
654 acpi_bus_generate_proc_event(fujitsu->dev,
655 ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS,
656 0);
657 keycode = KEY_BRIGHTNESSDOWN;
658 }
659 } else {
660 keycode = KEY_UNKNOWN;
661 }
662 break;
663 default:
664 keycode = KEY_UNKNOWN;
665 vdbg_printk(FUJLAPTOP_DBG_WARN,
666 "unsupported event [0x%x]\n", event);
667 break;
668 }
669
670 if (keycode != 0) {
671 input_report_key(input, keycode, 1);
672 input_sync(input);
673 input_report_key(input, keycode, 0);
674 input_sync(input);
675 }
676
677 return;
678}
679
680/* ACPI device for hotkey handling */
681
682static int acpi_fujitsu_hotkey_add(struct acpi_device *device)
683{
684 acpi_status status;
685 acpi_handle handle;
686 int result = 0;
687 int state = 0;
688 struct input_dev *input;
689 int error;
690 int i;
691
692 if (!device)
693 return -EINVAL;
694
695 fujitsu_hotkey->acpi_handle = device->handle;
696 sprintf(acpi_device_name(device), "%s",
697 ACPI_FUJITSU_HOTKEY_DEVICE_NAME);
698 sprintf(acpi_device_class(device), "%s", ACPI_FUJITSU_CLASS);
699 acpi_driver_data(device) = fujitsu_hotkey;
700
701 status = acpi_install_notify_handler(device->handle,
702 ACPI_DEVICE_NOTIFY,
703 acpi_fujitsu_hotkey_notify,
704 fujitsu_hotkey);
705
706 if (ACPI_FAILURE(status)) {
707 printk(KERN_ERR "Error installing notify handler\n");
708 error = -ENODEV;
709 goto err_stop;
710 }
711
712 /* kfifo */
713 spin_lock_init(&fujitsu_hotkey->fifo_lock);
714 fujitsu_hotkey->fifo =
715 kfifo_alloc(RINGBUFFERSIZE * sizeof(int), GFP_KERNEL,
716 &fujitsu_hotkey->fifo_lock);
717 if (IS_ERR(fujitsu_hotkey->fifo)) {
718 printk(KERN_ERR "kfifo_alloc failed\n");
719 error = PTR_ERR(fujitsu_hotkey->fifo);
720 goto err_stop;
721 }
722
723 fujitsu_hotkey->input = input = input_allocate_device();
724 if (!input) {
725 error = -ENOMEM;
726 goto err_uninstall_notify;
727 }
728
729 snprintf(fujitsu_hotkey->phys, sizeof(fujitsu_hotkey->phys),
730 "%s/video/input0", acpi_device_hid(device));
731
732 input->name = acpi_device_name(device);
733 input->phys = fujitsu_hotkey->phys;
734 input->id.bustype = BUS_HOST;
735 input->id.product = 0x06;
736 input->dev.parent = &device->dev;
737 input->evbit[0] = BIT(EV_KEY);
738 set_bit(KEY_SCREENLOCK, input->keybit);
739 set_bit(KEY_MEDIA, input->keybit);
740 set_bit(KEY_EMAIL, input->keybit);
741 set_bit(KEY_SUSPEND, input->keybit);
742 set_bit(KEY_UNKNOWN, input->keybit);
743
744 error = input_register_device(input);
745 if (error)
746 goto err_free_input_dev;
747
748 result = acpi_bus_get_power(fujitsu_hotkey->acpi_handle, &state);
749 if (result) {
750 printk(KERN_ERR "Error reading power state\n");
751 goto end;
752 }
753
754 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
755 acpi_device_name(device), acpi_device_bid(device),
756 !device->power.state ? "on" : "off");
757
758 fujitsu_hotkey->dev = device;
759
760 if (ACPI_SUCCESS
761 (acpi_get_handle(device->handle, METHOD_NAME__INI, &handle))) {
762 vdbg_printk(FUJLAPTOP_DBG_INFO, "Invoking _INI\n");
763 if (ACPI_FAILURE
764 (acpi_evaluate_object
765 (device->handle, METHOD_NAME__INI, NULL, NULL)))
766 printk(KERN_ERR "_INI Method failed\n");
767 }
768
769 i = 0; /* Discard hotkey ringbuffer */
770 while (get_irb() != 0 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) ;
771 vdbg_printk(FUJLAPTOP_DBG_INFO, "Discarded %i ringbuffer entries\n", i);
772
773 return result;
774
775end:
776err_free_input_dev:
777 input_free_device(input);
778err_uninstall_notify:
779 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
780 acpi_fujitsu_hotkey_notify);
781 kfifo_free(fujitsu_hotkey->fifo);
782err_stop:
783
784 return result;
785}
786
787static int acpi_fujitsu_hotkey_remove(struct acpi_device *device, int type)
788{
789 acpi_status status;
790 struct fujitsu_hotkey_t *fujitsu_hotkey = NULL;
791
792 if (!device || !acpi_driver_data(device))
793 return -EINVAL;
794
795 fujitsu_hotkey = acpi_driver_data(device);
796
797 status = acpi_remove_notify_handler(fujitsu_hotkey->acpi_handle,
798 ACPI_DEVICE_NOTIFY,
799 acpi_fujitsu_hotkey_notify);
800
801 fujitsu_hotkey->acpi_handle = NULL;
802
803 kfifo_free(fujitsu_hotkey->fifo);
804
805 return 0;
806}
807
808static void acpi_fujitsu_hotkey_notify(acpi_handle handle, u32 event,
809 void *data)
810{
811 struct input_dev *input;
812 int keycode, keycode_r;
813 unsigned int irb = 1;
814 int i, status;
815
816 input = fujitsu_hotkey->input;
817
818 vdbg_printk(FUJLAPTOP_DBG_TRACE, "Hotkey event\n");
819
820 switch (event) {
821 case ACPI_FUJITSU_NOTIFY_CODE1:
822 i = 0;
823 while ((irb = get_irb()) != 0
824 && (i++) < MAX_HOTKEY_RINGBUFFER_SIZE) {
825 vdbg_printk(FUJLAPTOP_DBG_TRACE, "GIRB result [%x]\n",
826 irb);
827
828 switch (irb & 0x4ff) {
829 case LOCK_KEY:
830 keycode = KEY_SCREENLOCK;
831 break;
832 case DISPLAY_KEY:
833 keycode = KEY_MEDIA;
834 break;
835 case ENERGY_KEY:
836 keycode = KEY_EMAIL;
837 break;
838 case REST_KEY:
839 keycode = KEY_SUSPEND;
840 break;
841 case 0:
842 keycode = 0;
843 break;
844 default:
845 vdbg_printk(FUJLAPTOP_DBG_WARN,
846 "Unknown GIRB result [%x]\n", irb);
847 keycode = -1;
848 break;
849 }
850 if (keycode > 0) {
851 vdbg_printk(FUJLAPTOP_DBG_TRACE,
852 "Push keycode into ringbuffer [%d]\n",
853 keycode);
854 status = kfifo_put(fujitsu_hotkey->fifo,
855 (unsigned char *)&keycode,
856 sizeof(keycode));
857 if (status != sizeof(keycode)) {
858 vdbg_printk(FUJLAPTOP_DBG_WARN,
859 "Could not push keycode [0x%x]\n",
860 keycode);
861 } else {
862 input_report_key(input, keycode, 1);
863 input_sync(input);
864 }
865 } else if (keycode == 0) {
866 while ((status =
867 kfifo_get
868 (fujitsu_hotkey->fifo, (unsigned char *)
869 &keycode_r,
870 sizeof
871 (keycode_r))) == sizeof(keycode_r)) {
872 input_report_key(input, keycode_r, 0);
873 input_sync(input);
874 vdbg_printk(FUJLAPTOP_DBG_TRACE,
875 "Pop keycode from ringbuffer [%d]\n",
876 keycode_r);
877 }
878 }
879 }
880
881 break;
882 default:
883 keycode = KEY_UNKNOWN;
884 vdbg_printk(FUJLAPTOP_DBG_WARN,
885 "Unsupported event [0x%x]\n", event);
886 input_report_key(input, keycode, 1);
887 input_sync(input);
888 input_report_key(input, keycode, 0);
889 input_sync(input);
890 break;
891 }
892
893 return;
894}
895
896/* Initialization */
897
239static const struct acpi_device_id fujitsu_device_ids[] = { 898static const struct acpi_device_id fujitsu_device_ids[] = {
240 {ACPI_FUJITSU_HID, 0}, 899 {ACPI_FUJITSU_HID, 0},
241 {"", 0}, 900 {"", 0},
@@ -251,11 +910,24 @@ static struct acpi_driver acpi_fujitsu_driver = {
251 }, 910 },
252}; 911};
253 912
254/* Initialization */ 913static const struct acpi_device_id fujitsu_hotkey_device_ids[] = {
914 {ACPI_FUJITSU_HOTKEY_HID, 0},
915 {"", 0},
916};
917
918static struct acpi_driver acpi_fujitsu_hotkey_driver = {
919 .name = ACPI_FUJITSU_HOTKEY_DRIVER_NAME,
920 .class = ACPI_FUJITSU_CLASS,
921 .ids = fujitsu_hotkey_device_ids,
922 .ops = {
923 .add = acpi_fujitsu_hotkey_add,
924 .remove = acpi_fujitsu_hotkey_remove,
925 },
926};
255 927
256static int __init fujitsu_init(void) 928static int __init fujitsu_init(void)
257{ 929{
258 int ret, result; 930 int ret, result, max_brightness;
259 931
260 if (acpi_disabled) 932 if (acpi_disabled)
261 return -ENODEV; 933 return -ENODEV;
@@ -271,19 +943,6 @@ static int __init fujitsu_init(void)
271 goto fail_acpi; 943 goto fail_acpi;
272 } 944 }
273 945
274 /* Register backlight stuff */
275
276 fujitsu->bl_device =
277 backlight_device_register("fujitsu-laptop", NULL, NULL,
278 &fujitsubl_ops);
279 if (IS_ERR(fujitsu->bl_device))
280 return PTR_ERR(fujitsu->bl_device);
281
282 fujitsu->bl_device->props.max_brightness = FUJITSU_LCD_N_LEVELS - 1;
283 ret = platform_driver_register(&fujitsupf_driver);
284 if (ret)
285 goto fail_backlight;
286
287 /* Register platform stuff */ 946 /* Register platform stuff */
288 947
289 fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1); 948 fujitsu->pf_device = platform_device_alloc("fujitsu-laptop", -1);
@@ -302,28 +961,68 @@ static int __init fujitsu_init(void)
302 if (ret) 961 if (ret)
303 goto fail_platform_device2; 962 goto fail_platform_device2;
304 963
964 /* Register backlight stuff */
965
966 fujitsu->bl_device =
967 backlight_device_register("fujitsu-laptop", NULL, NULL,
968 &fujitsubl_ops);
969 if (IS_ERR(fujitsu->bl_device))
970 return PTR_ERR(fujitsu->bl_device);
971
972 max_brightness = fujitsu->max_brightness;
973
974 fujitsu->bl_device->props.max_brightness = max_brightness - 1;
975 fujitsu->bl_device->props.brightness = fujitsu->brightness_level;
976
977 ret = platform_driver_register(&fujitsupf_driver);
978 if (ret)
979 goto fail_backlight;
980
981 /* Register hotkey driver */
982
983 fujitsu_hotkey = kmalloc(sizeof(struct fujitsu_hotkey_t), GFP_KERNEL);
984 if (!fujitsu_hotkey) {
985 ret = -ENOMEM;
986 goto fail_hotkey;
987 }
988 memset(fujitsu_hotkey, 0, sizeof(struct fujitsu_hotkey_t));
989
990 result = acpi_bus_register_driver(&acpi_fujitsu_hotkey_driver);
991 if (result < 0) {
992 ret = -ENODEV;
993 goto fail_hotkey1;
994 }
995
305 printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION 996 printk(KERN_INFO "fujitsu-laptop: driver " FUJITSU_DRIVER_VERSION
306 " successfully loaded.\n"); 997 " successfully loaded.\n");
307 998
308 return 0; 999 return 0;
309 1000
310 fail_platform_device2: 1001fail_hotkey1:
311 1002
312 platform_device_del(fujitsu->pf_device); 1003 kfree(fujitsu_hotkey);
313
314 fail_platform_device1:
315
316 platform_device_put(fujitsu->pf_device);
317 1004
318 fail_platform_driver: 1005fail_hotkey:
319 1006
320 platform_driver_unregister(&fujitsupf_driver); 1007 platform_driver_unregister(&fujitsupf_driver);
321 1008
322 fail_backlight: 1009fail_backlight:
323 1010
324 backlight_device_unregister(fujitsu->bl_device); 1011 backlight_device_unregister(fujitsu->bl_device);
325 1012
326 fail_acpi: 1013fail_platform_device2:
1014
1015 platform_device_del(fujitsu->pf_device);
1016
1017fail_platform_device1:
1018
1019 platform_device_put(fujitsu->pf_device);
1020
1021fail_platform_driver:
1022
1023 acpi_bus_unregister_driver(&acpi_fujitsu_driver);
1024
1025fail_acpi:
327 1026
328 kfree(fujitsu); 1027 kfree(fujitsu);
329 1028
@@ -342,19 +1041,43 @@ static void __exit fujitsu_cleanup(void)
342 1041
343 kfree(fujitsu); 1042 kfree(fujitsu);
344 1043
1044 acpi_bus_unregister_driver(&acpi_fujitsu_hotkey_driver);
1045
1046 kfree(fujitsu_hotkey);
1047
345 printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n"); 1048 printk(KERN_INFO "fujitsu-laptop: driver unloaded.\n");
346} 1049}
347 1050
348module_init(fujitsu_init); 1051module_init(fujitsu_init);
349module_exit(fujitsu_cleanup); 1052module_exit(fujitsu_cleanup);
350 1053
351MODULE_AUTHOR("Jonathan Woithe"); 1054module_param(use_alt_lcd_levels, uint, 0644);
1055MODULE_PARM_DESC(use_alt_lcd_levels,
1056 "Use alternative interface for lcd_levels (needed for Lifebook s6410).");
1057module_param(disable_brightness_keys, uint, 0644);
1058MODULE_PARM_DESC(disable_brightness_keys,
1059 "Disable brightness keys (eg. if they are already handled by the generic ACPI_VIDEO device).");
1060module_param(disable_brightness_adjust, uint, 0644);
1061MODULE_PARM_DESC(disable_brightness_adjust, "Disable brightness adjustment .");
1062#ifdef CONFIG_FUJITSU_LAPTOP_DEBUG
1063module_param_named(debug, dbg_level, uint, 0644);
1064MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
1065#endif
1066
1067MODULE_AUTHOR("Jonathan Woithe, Peter Gruber");
352MODULE_DESCRIPTION("Fujitsu laptop extras support"); 1068MODULE_DESCRIPTION("Fujitsu laptop extras support");
353MODULE_VERSION(FUJITSU_DRIVER_VERSION); 1069MODULE_VERSION(FUJITSU_DRIVER_VERSION);
354MODULE_LICENSE("GPL"); 1070MODULE_LICENSE("GPL");
355 1071
1072MODULE_ALIAS
1073 ("dmi:*:svnFUJITSUSIEMENS:*:pvr:rvnFUJITSU:rnFJNB1D3:*:cvrS6410:*");
1074MODULE_ALIAS
1075 ("dmi:*:svnFUJITSU:*:pvr:rvnFUJITSU:rnFJNB19C:*:cvrS7020:*");
1076
356static struct pnp_device_id pnp_ids[] = { 1077static struct pnp_device_id pnp_ids[] = {
357 { .id = "FUJ02bf" }, 1078 { .id = "FUJ02bf" },
1079 { .id = "FUJ02B1" },
1080 { .id = "FUJ02E3" },
358 { .id = "" } 1081 { .id = "" }
359}; 1082};
360MODULE_DEVICE_TABLE(pnp, pnp_ids); 1083MODULE_DEVICE_TABLE(pnp, pnp_ids);
diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index f9ad960d7c1a..66e5a5487c20 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -2,7 +2,7 @@
2 * Block driver for media (i.e., flash cards) 2 * Block driver for media (i.e., flash cards)
3 * 3 *
4 * Copyright 2002 Hewlett-Packard Company 4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2007 Pierre Ossman 5 * Copyright 2005-2008 Pierre Ossman
6 * 6 *
7 * Use consistent with the GNU GPL is permitted, 7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is 8 * provided that this copyright notice is
@@ -237,17 +237,6 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
237 if (brq.data.blocks > card->host->max_blk_count) 237 if (brq.data.blocks > card->host->max_blk_count)
238 brq.data.blocks = card->host->max_blk_count; 238 brq.data.blocks = card->host->max_blk_count;
239 239
240 /*
241 * If the host doesn't support multiple block writes, force
242 * block writes to single block. SD cards are excepted from
243 * this rule as they support querying the number of
244 * successfully written sectors.
245 */
246 if (rq_data_dir(req) != READ &&
247 !(card->host->caps & MMC_CAP_MULTIWRITE) &&
248 !mmc_card_sd(card))
249 brq.data.blocks = 1;
250
251 if (brq.data.blocks > 1) { 240 if (brq.data.blocks > 1) {
252 /* SPI multiblock writes terminate using a special 241 /* SPI multiblock writes terminate using a special
253 * token, not a STOP_TRANSMISSION request. 242 * token, not a STOP_TRANSMISSION request.
@@ -296,22 +285,24 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
296 285
297 mmc_queue_bounce_post(mq); 286 mmc_queue_bounce_post(mq);
298 287
288 /*
289 * Check for errors here, but don't jump to cmd_err
290 * until later as we need to wait for the card to leave
291 * programming mode even when things go wrong.
292 */
299 if (brq.cmd.error) { 293 if (brq.cmd.error) {
300 printk(KERN_ERR "%s: error %d sending read/write command\n", 294 printk(KERN_ERR "%s: error %d sending read/write command\n",
301 req->rq_disk->disk_name, brq.cmd.error); 295 req->rq_disk->disk_name, brq.cmd.error);
302 goto cmd_err;
303 } 296 }
304 297
305 if (brq.data.error) { 298 if (brq.data.error) {
306 printk(KERN_ERR "%s: error %d transferring data\n", 299 printk(KERN_ERR "%s: error %d transferring data\n",
307 req->rq_disk->disk_name, brq.data.error); 300 req->rq_disk->disk_name, brq.data.error);
308 goto cmd_err;
309 } 301 }
310 302
311 if (brq.stop.error) { 303 if (brq.stop.error) {
312 printk(KERN_ERR "%s: error %d sending stop command\n", 304 printk(KERN_ERR "%s: error %d sending stop command\n",
313 req->rq_disk->disk_name, brq.stop.error); 305 req->rq_disk->disk_name, brq.stop.error);
314 goto cmd_err;
315 } 306 }
316 307
317 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { 308 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
@@ -344,6 +335,9 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
344#endif 335#endif
345 } 336 }
346 337
338 if (brq.cmd.error || brq.data.error || brq.stop.error)
339 goto cmd_err;
340
347 /* 341 /*
348 * A block was successfully transferred. 342 * A block was successfully transferred.
349 */ 343 */
@@ -362,30 +356,32 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
362 * mark the known good sectors as ok. 356 * mark the known good sectors as ok.
363 * 357 *
364 * If the card is not SD, we can still ok written sectors 358 * If the card is not SD, we can still ok written sectors
365 * if the controller can do proper error reporting. 359 * as reported by the controller (which might be less than
360 * the real number of written sectors, but never more).
366 * 361 *
367 * For reads we just fail the entire chunk as that should 362 * For reads we just fail the entire chunk as that should
368 * be safe in all cases. 363 * be safe in all cases.
369 */ 364 */
370 if (rq_data_dir(req) != READ && mmc_card_sd(card)) { 365 if (rq_data_dir(req) != READ) {
371 u32 blocks; 366 if (mmc_card_sd(card)) {
372 unsigned int bytes; 367 u32 blocks;
373 368 unsigned int bytes;
374 blocks = mmc_sd_num_wr_blocks(card); 369
375 if (blocks != (u32)-1) { 370 blocks = mmc_sd_num_wr_blocks(card);
376 if (card->csd.write_partial) 371 if (blocks != (u32)-1) {
377 bytes = blocks << md->block_bits; 372 if (card->csd.write_partial)
378 else 373 bytes = blocks << md->block_bits;
379 bytes = blocks << 9; 374 else
375 bytes = blocks << 9;
376 spin_lock_irq(&md->lock);
377 ret = __blk_end_request(req, 0, bytes);
378 spin_unlock_irq(&md->lock);
379 }
380 } else {
380 spin_lock_irq(&md->lock); 381 spin_lock_irq(&md->lock);
381 ret = __blk_end_request(req, 0, bytes); 382 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
382 spin_unlock_irq(&md->lock); 383 spin_unlock_irq(&md->lock);
383 } 384 }
384 } else if (rq_data_dir(req) != READ &&
385 (card->host->caps & MMC_CAP_MULTIWRITE)) {
386 spin_lock_irq(&md->lock);
387 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
388 spin_unlock_irq(&md->lock);
389 } 385 }
390 386
391 mmc_release_host(card->host); 387 mmc_release_host(card->host);
diff --git a/drivers/mmc/card/mmc_test.c b/drivers/mmc/card/mmc_test.c
index ffadee549a41..d6b9b486417c 100644
--- a/drivers/mmc/card/mmc_test.c
+++ b/drivers/mmc/card/mmc_test.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * linux/drivers/mmc/card/mmc_test.c 2 * linux/drivers/mmc/card/mmc_test.c
3 * 3 *
4 * Copyright 2007 Pierre Ossman 4 * Copyright 2007-2008 Pierre Ossman
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
@@ -26,13 +26,17 @@
26struct mmc_test_card { 26struct mmc_test_card {
27 struct mmc_card *card; 27 struct mmc_card *card;
28 28
29 u8 scratch[BUFFER_SIZE];
29 u8 *buffer; 30 u8 *buffer;
30}; 31};
31 32
32/*******************************************************************/ 33/*******************************************************************/
33/* Helper functions */ 34/* General helper functions */
34/*******************************************************************/ 35/*******************************************************************/
35 36
37/*
38 * Configure correct block size in card
39 */
36static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size) 40static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
37{ 41{
38 struct mmc_command cmd; 42 struct mmc_command cmd;
@@ -48,117 +52,61 @@ static int mmc_test_set_blksize(struct mmc_test_card *test, unsigned size)
48 return 0; 52 return 0;
49} 53}
50 54
51static int __mmc_test_transfer(struct mmc_test_card *test, int write, 55/*
52 unsigned broken_xfer, u8 *buffer, unsigned addr, 56 * Fill in the mmc_request structure given a set of transfer parameters.
53 unsigned blocks, unsigned blksz) 57 */
58static void mmc_test_prepare_mrq(struct mmc_test_card *test,
59 struct mmc_request *mrq, struct scatterlist *sg, unsigned sg_len,
60 unsigned dev_addr, unsigned blocks, unsigned blksz, int write)
54{ 61{
55 int ret, busy; 62 BUG_ON(!mrq || !mrq->cmd || !mrq->data || !mrq->stop);
56
57 struct mmc_request mrq;
58 struct mmc_command cmd;
59 struct mmc_command stop;
60 struct mmc_data data;
61
62 struct scatterlist sg;
63
64 memset(&mrq, 0, sizeof(struct mmc_request));
65
66 mrq.cmd = &cmd;
67 mrq.data = &data;
68
69 memset(&cmd, 0, sizeof(struct mmc_command));
70 63
71 if (broken_xfer) { 64 if (blocks > 1) {
72 if (blocks > 1) { 65 mrq->cmd->opcode = write ?
73 cmd.opcode = write ? 66 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
74 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
75 } else {
76 cmd.opcode = MMC_SEND_STATUS;
77 }
78 } else { 67 } else {
79 if (blocks > 1) { 68 mrq->cmd->opcode = write ?
80 cmd.opcode = write ? 69 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
81 MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
82 } else {
83 cmd.opcode = write ?
84 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
85 }
86 } 70 }
87 71
88 if (broken_xfer && blocks == 1) 72 mrq->cmd->arg = dev_addr;
89 cmd.arg = test->card->rca << 16; 73 mrq->cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC;
90 else
91 cmd.arg = addr;
92 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
93 74
94 memset(&stop, 0, sizeof(struct mmc_command)); 75 if (blocks == 1)
95 76 mrq->stop = NULL;
96 if (!broken_xfer && (blocks > 1)) { 77 else {
97 stop.opcode = MMC_STOP_TRANSMISSION; 78 mrq->stop->opcode = MMC_STOP_TRANSMISSION;
98 stop.arg = 0; 79 mrq->stop->arg = 0;
99 stop.flags = MMC_RSP_R1B | MMC_CMD_AC; 80 mrq->stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
100
101 mrq.stop = &stop;
102 } 81 }
103 82
104 memset(&data, 0, sizeof(struct mmc_data)); 83 mrq->data->blksz = blksz;
105 84 mrq->data->blocks = blocks;
106 data.blksz = blksz; 85 mrq->data->flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
107 data.blocks = blocks; 86 mrq->data->sg = sg;
108 data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ; 87 mrq->data->sg_len = sg_len;
109 data.sg = &sg;
110 data.sg_len = 1;
111
112 sg_init_one(&sg, buffer, blocks * blksz);
113
114 mmc_set_data_timeout(&data, test->card);
115 88
116 mmc_wait_for_req(test->card->host, &mrq); 89 mmc_set_data_timeout(mrq->data, test->card);
117 90}
118 ret = 0;
119
120 if (broken_xfer) {
121 if (!ret && cmd.error)
122 ret = cmd.error;
123 if (!ret && data.error == 0)
124 ret = RESULT_FAIL;
125 if (!ret && data.error != -ETIMEDOUT)
126 ret = data.error;
127 if (!ret && stop.error)
128 ret = stop.error;
129 if (blocks > 1) {
130 if (!ret && data.bytes_xfered > blksz)
131 ret = RESULT_FAIL;
132 } else {
133 if (!ret && data.bytes_xfered > 0)
134 ret = RESULT_FAIL;
135 }
136 } else {
137 if (!ret && cmd.error)
138 ret = cmd.error;
139 if (!ret && data.error)
140 ret = data.error;
141 if (!ret && stop.error)
142 ret = stop.error;
143 if (!ret && data.bytes_xfered != blocks * blksz)
144 ret = RESULT_FAIL;
145 }
146 91
147 if (ret == -EINVAL) 92/*
148 ret = RESULT_UNSUP_HOST; 93 * Wait for the card to finish the busy state
94 */
95static int mmc_test_wait_busy(struct mmc_test_card *test)
96{
97 int ret, busy;
98 struct mmc_command cmd;
149 99
150 busy = 0; 100 busy = 0;
151 do { 101 do {
152 int ret2;
153
154 memset(&cmd, 0, sizeof(struct mmc_command)); 102 memset(&cmd, 0, sizeof(struct mmc_command));
155 103
156 cmd.opcode = MMC_SEND_STATUS; 104 cmd.opcode = MMC_SEND_STATUS;
157 cmd.arg = test->card->rca << 16; 105 cmd.arg = test->card->rca << 16;
158 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 106 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
159 107
160 ret2 = mmc_wait_for_cmd(test->card->host, &cmd, 0); 108 ret = mmc_wait_for_cmd(test->card->host, &cmd, 0);
161 if (ret2) 109 if (ret)
162 break; 110 break;
163 111
164 if (!busy && !(cmd.resp[0] & R1_READY_FOR_DATA)) { 112 if (!busy && !(cmd.resp[0] & R1_READY_FOR_DATA)) {
@@ -172,14 +120,57 @@ static int __mmc_test_transfer(struct mmc_test_card *test, int write,
172 return ret; 120 return ret;
173} 121}
174 122
175static int mmc_test_transfer(struct mmc_test_card *test, int write, 123/*
176 u8 *buffer, unsigned addr, unsigned blocks, unsigned blksz) 124 * Transfer a single sector of kernel addressable data
125 */
126static int mmc_test_buffer_transfer(struct mmc_test_card *test,
127 u8 *buffer, unsigned addr, unsigned blksz, int write)
177{ 128{
178 return __mmc_test_transfer(test, write, 0, buffer, 129 int ret;
179 addr, blocks, blksz); 130
131 struct mmc_request mrq;
132 struct mmc_command cmd;
133 struct mmc_command stop;
134 struct mmc_data data;
135
136 struct scatterlist sg;
137
138 memset(&mrq, 0, sizeof(struct mmc_request));
139 memset(&cmd, 0, sizeof(struct mmc_command));
140 memset(&data, 0, sizeof(struct mmc_data));
141 memset(&stop, 0, sizeof(struct mmc_command));
142
143 mrq.cmd = &cmd;
144 mrq.data = &data;
145 mrq.stop = &stop;
146
147 sg_init_one(&sg, buffer, blksz);
148
149 mmc_test_prepare_mrq(test, &mrq, &sg, 1, addr, 1, blksz, write);
150
151 mmc_wait_for_req(test->card->host, &mrq);
152
153 if (cmd.error)
154 return cmd.error;
155 if (data.error)
156 return data.error;
157
158 ret = mmc_test_wait_busy(test);
159 if (ret)
160 return ret;
161
162 return 0;
180} 163}
181 164
182static int mmc_test_prepare_verify(struct mmc_test_card *test, int write) 165/*******************************************************************/
166/* Test preparation and cleanup */
167/*******************************************************************/
168
169/*
170 * Fill the first couple of sectors of the card with known data
171 * so that bad reads/writes can be detected
172 */
173static int __mmc_test_prepare(struct mmc_test_card *test, int write)
183{ 174{
184 int ret, i; 175 int ret, i;
185 176
@@ -188,15 +179,14 @@ static int mmc_test_prepare_verify(struct mmc_test_card *test, int write)
188 return ret; 179 return ret;
189 180
190 if (write) 181 if (write)
191 memset(test->buffer, 0xDF, BUFFER_SIZE); 182 memset(test->buffer, 0xDF, 512);
192 else { 183 else {
193 for (i = 0;i < BUFFER_SIZE;i++) 184 for (i = 0;i < 512;i++)
194 test->buffer[i] = i; 185 test->buffer[i] = i;
195 } 186 }
196 187
197 for (i = 0;i < BUFFER_SIZE / 512;i++) { 188 for (i = 0;i < BUFFER_SIZE / 512;i++) {
198 ret = mmc_test_transfer(test, 1, test->buffer + i * 512, 189 ret = mmc_test_buffer_transfer(test, test->buffer, i * 512, 512, 1);
199 i * 512, 1, 512);
200 if (ret) 190 if (ret)
201 return ret; 191 return ret;
202 } 192 }
@@ -204,41 +194,218 @@ static int mmc_test_prepare_verify(struct mmc_test_card *test, int write)
204 return 0; 194 return 0;
205} 195}
206 196
207static int mmc_test_prepare_verify_write(struct mmc_test_card *test) 197static int mmc_test_prepare_write(struct mmc_test_card *test)
198{
199 return __mmc_test_prepare(test, 1);
200}
201
202static int mmc_test_prepare_read(struct mmc_test_card *test)
203{
204 return __mmc_test_prepare(test, 0);
205}
206
207static int mmc_test_cleanup(struct mmc_test_card *test)
208{ 208{
209 return mmc_test_prepare_verify(test, 1); 209 int ret, i;
210
211 ret = mmc_test_set_blksize(test, 512);
212 if (ret)
213 return ret;
214
215 memset(test->buffer, 0, 512);
216
217 for (i = 0;i < BUFFER_SIZE / 512;i++) {
218 ret = mmc_test_buffer_transfer(test, test->buffer, i * 512, 512, 1);
219 if (ret)
220 return ret;
221 }
222
223 return 0;
210} 224}
211 225
212static int mmc_test_prepare_verify_read(struct mmc_test_card *test) 226/*******************************************************************/
227/* Test execution helpers */
228/*******************************************************************/
229
230/*
231 * Modifies the mmc_request to perform the "short transfer" tests
232 */
233static void mmc_test_prepare_broken_mrq(struct mmc_test_card *test,
234 struct mmc_request *mrq, int write)
213{ 235{
214 return mmc_test_prepare_verify(test, 0); 236 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
237
238 if (mrq->data->blocks > 1) {
239 mrq->cmd->opcode = write ?
240 MMC_WRITE_BLOCK : MMC_READ_SINGLE_BLOCK;
241 mrq->stop = NULL;
242 } else {
243 mrq->cmd->opcode = MMC_SEND_STATUS;
244 mrq->cmd->arg = test->card->rca << 16;
245 }
215} 246}
216 247
217static int mmc_test_verified_transfer(struct mmc_test_card *test, int write, 248/*
218 u8 *buffer, unsigned addr, unsigned blocks, unsigned blksz) 249 * Checks that a normal transfer didn't have any errors
250 */
251static int mmc_test_check_result(struct mmc_test_card *test,
252 struct mmc_request *mrq)
219{ 253{
220 int ret, i, sectors; 254 int ret;
221 255
222 /* 256 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
223 * It is assumed that the above preparation has been done. 257
224 */ 258 ret = 0;
225 259
226 memset(test->buffer, 0, BUFFER_SIZE); 260 if (!ret && mrq->cmd->error)
261 ret = mrq->cmd->error;
262 if (!ret && mrq->data->error)
263 ret = mrq->data->error;
264 if (!ret && mrq->stop && mrq->stop->error)
265 ret = mrq->stop->error;
266 if (!ret && mrq->data->bytes_xfered !=
267 mrq->data->blocks * mrq->data->blksz)
268 ret = RESULT_FAIL;
269
270 if (ret == -EINVAL)
271 ret = RESULT_UNSUP_HOST;
272
273 return ret;
274}
275
276/*
277 * Checks that a "short transfer" behaved as expected
278 */
279static int mmc_test_check_broken_result(struct mmc_test_card *test,
280 struct mmc_request *mrq)
281{
282 int ret;
283
284 BUG_ON(!mrq || !mrq->cmd || !mrq->data);
285
286 ret = 0;
287
288 if (!ret && mrq->cmd->error)
289 ret = mrq->cmd->error;
290 if (!ret && mrq->data->error == 0)
291 ret = RESULT_FAIL;
292 if (!ret && mrq->data->error != -ETIMEDOUT)
293 ret = mrq->data->error;
294 if (!ret && mrq->stop && mrq->stop->error)
295 ret = mrq->stop->error;
296 if (mrq->data->blocks > 1) {
297 if (!ret && mrq->data->bytes_xfered > mrq->data->blksz)
298 ret = RESULT_FAIL;
299 } else {
300 if (!ret && mrq->data->bytes_xfered > 0)
301 ret = RESULT_FAIL;
302 }
303
304 if (ret == -EINVAL)
305 ret = RESULT_UNSUP_HOST;
306
307 return ret;
308}
309
310/*
311 * Tests a basic transfer with certain parameters
312 */
313static int mmc_test_simple_transfer(struct mmc_test_card *test,
314 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
315 unsigned blocks, unsigned blksz, int write)
316{
317 struct mmc_request mrq;
318 struct mmc_command cmd;
319 struct mmc_command stop;
320 struct mmc_data data;
321
322 memset(&mrq, 0, sizeof(struct mmc_request));
323 memset(&cmd, 0, sizeof(struct mmc_command));
324 memset(&data, 0, sizeof(struct mmc_data));
325 memset(&stop, 0, sizeof(struct mmc_command));
326
327 mrq.cmd = &cmd;
328 mrq.data = &data;
329 mrq.stop = &stop;
330
331 mmc_test_prepare_mrq(test, &mrq, sg, sg_len, dev_addr,
332 blocks, blksz, write);
333
334 mmc_wait_for_req(test->card->host, &mrq);
335
336 mmc_test_wait_busy(test);
337
338 return mmc_test_check_result(test, &mrq);
339}
340
341/*
342 * Tests a transfer where the card will fail completely or partly
343 */
344static int mmc_test_broken_transfer(struct mmc_test_card *test,
345 unsigned blocks, unsigned blksz, int write)
346{
347 struct mmc_request mrq;
348 struct mmc_command cmd;
349 struct mmc_command stop;
350 struct mmc_data data;
351
352 struct scatterlist sg;
353
354 memset(&mrq, 0, sizeof(struct mmc_request));
355 memset(&cmd, 0, sizeof(struct mmc_command));
356 memset(&data, 0, sizeof(struct mmc_data));
357 memset(&stop, 0, sizeof(struct mmc_command));
358
359 mrq.cmd = &cmd;
360 mrq.data = &data;
361 mrq.stop = &stop;
362
363 sg_init_one(&sg, test->buffer, blocks * blksz);
364
365 mmc_test_prepare_mrq(test, &mrq, &sg, 1, 0, blocks, blksz, write);
366 mmc_test_prepare_broken_mrq(test, &mrq, write);
367
368 mmc_wait_for_req(test->card->host, &mrq);
369
370 mmc_test_wait_busy(test);
371
372 return mmc_test_check_broken_result(test, &mrq);
373}
374
375/*
376 * Does a complete transfer test where data is also validated
377 *
378 * Note: mmc_test_prepare() must have been done before this call
379 */
380static int mmc_test_transfer(struct mmc_test_card *test,
381 struct scatterlist *sg, unsigned sg_len, unsigned dev_addr,
382 unsigned blocks, unsigned blksz, int write)
383{
384 int ret, i;
385 unsigned long flags;
227 386
228 if (write) { 387 if (write) {
229 for (i = 0;i < blocks * blksz;i++) 388 for (i = 0;i < blocks * blksz;i++)
230 buffer[i] = i; 389 test->scratch[i] = i;
390 } else {
391 memset(test->scratch, 0, BUFFER_SIZE);
231 } 392 }
393 local_irq_save(flags);
394 sg_copy_from_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
395 local_irq_restore(flags);
232 396
233 ret = mmc_test_set_blksize(test, blksz); 397 ret = mmc_test_set_blksize(test, blksz);
234 if (ret) 398 if (ret)
235 return ret; 399 return ret;
236 400
237 ret = mmc_test_transfer(test, write, buffer, addr, blocks, blksz); 401 ret = mmc_test_simple_transfer(test, sg, sg_len, dev_addr,
402 blocks, blksz, write);
238 if (ret) 403 if (ret)
239 return ret; 404 return ret;
240 405
241 if (write) { 406 if (write) {
407 int sectors;
408
242 ret = mmc_test_set_blksize(test, 512); 409 ret = mmc_test_set_blksize(test, 512);
243 if (ret) 410 if (ret)
244 return ret; 411 return ret;
@@ -253,9 +420,9 @@ static int mmc_test_verified_transfer(struct mmc_test_card *test, int write,
253 memset(test->buffer, 0, sectors * 512); 420 memset(test->buffer, 0, sectors * 512);
254 421
255 for (i = 0;i < sectors;i++) { 422 for (i = 0;i < sectors;i++) {
256 ret = mmc_test_transfer(test, 0, 423 ret = mmc_test_buffer_transfer(test,
257 test->buffer + i * 512, 424 test->buffer + i * 512,
258 addr + i * 512, 1, 512); 425 dev_addr + i * 512, 512, 0);
259 if (ret) 426 if (ret)
260 return ret; 427 return ret;
261 } 428 }
@@ -270,8 +437,11 @@ static int mmc_test_verified_transfer(struct mmc_test_card *test, int write,
270 return RESULT_FAIL; 437 return RESULT_FAIL;
271 } 438 }
272 } else { 439 } else {
440 local_irq_save(flags);
441 sg_copy_to_buffer(sg, sg_len, test->scratch, BUFFER_SIZE);
442 local_irq_restore(flags);
273 for (i = 0;i < blocks * blksz;i++) { 443 for (i = 0;i < blocks * blksz;i++) {
274 if (buffer[i] != (u8)i) 444 if (test->scratch[i] != (u8)i)
275 return RESULT_FAIL; 445 return RESULT_FAIL;
276 } 446 }
277 } 447 }
@@ -279,26 +449,6 @@ static int mmc_test_verified_transfer(struct mmc_test_card *test, int write,
279 return 0; 449 return 0;
280} 450}
281 451
282static int mmc_test_cleanup_verify(struct mmc_test_card *test)
283{
284 int ret, i;
285
286 ret = mmc_test_set_blksize(test, 512);
287 if (ret)
288 return ret;
289
290 memset(test->buffer, 0, BUFFER_SIZE);
291
292 for (i = 0;i < BUFFER_SIZE / 512;i++) {
293 ret = mmc_test_transfer(test, 1, test->buffer + i * 512,
294 i * 512, 1, 512);
295 if (ret)
296 return ret;
297 }
298
299 return 0;
300}
301
302/*******************************************************************/ 452/*******************************************************************/
303/* Tests */ 453/* Tests */
304/*******************************************************************/ 454/*******************************************************************/
@@ -314,12 +464,15 @@ struct mmc_test_case {
314static int mmc_test_basic_write(struct mmc_test_card *test) 464static int mmc_test_basic_write(struct mmc_test_card *test)
315{ 465{
316 int ret; 466 int ret;
467 struct scatterlist sg;
317 468
318 ret = mmc_test_set_blksize(test, 512); 469 ret = mmc_test_set_blksize(test, 512);
319 if (ret) 470 if (ret)
320 return ret; 471 return ret;
321 472
322 ret = mmc_test_transfer(test, 1, test->buffer, 0, 1, 512); 473 sg_init_one(&sg, test->buffer, 512);
474
475 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
323 if (ret) 476 if (ret)
324 return ret; 477 return ret;
325 478
@@ -329,12 +482,15 @@ static int mmc_test_basic_write(struct mmc_test_card *test)
329static int mmc_test_basic_read(struct mmc_test_card *test) 482static int mmc_test_basic_read(struct mmc_test_card *test)
330{ 483{
331 int ret; 484 int ret;
485 struct scatterlist sg;
332 486
333 ret = mmc_test_set_blksize(test, 512); 487 ret = mmc_test_set_blksize(test, 512);
334 if (ret) 488 if (ret)
335 return ret; 489 return ret;
336 490
337 ret = mmc_test_transfer(test, 0, test->buffer, 0, 1, 512); 491 sg_init_one(&sg, test->buffer, 512);
492
493 ret = mmc_test_simple_transfer(test, &sg, 1, 0, 1, 512, 1);
338 if (ret) 494 if (ret)
339 return ret; 495 return ret;
340 496
@@ -344,8 +500,11 @@ static int mmc_test_basic_read(struct mmc_test_card *test)
344static int mmc_test_verify_write(struct mmc_test_card *test) 500static int mmc_test_verify_write(struct mmc_test_card *test)
345{ 501{
346 int ret; 502 int ret;
503 struct scatterlist sg;
504
505 sg_init_one(&sg, test->buffer, 512);
347 506
348 ret = mmc_test_verified_transfer(test, 1, test->buffer, 0, 1, 512); 507 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
349 if (ret) 508 if (ret)
350 return ret; 509 return ret;
351 510
@@ -355,8 +514,11 @@ static int mmc_test_verify_write(struct mmc_test_card *test)
355static int mmc_test_verify_read(struct mmc_test_card *test) 514static int mmc_test_verify_read(struct mmc_test_card *test)
356{ 515{
357 int ret; 516 int ret;
517 struct scatterlist sg;
518
519 sg_init_one(&sg, test->buffer, 512);
358 520
359 ret = mmc_test_verified_transfer(test, 0, test->buffer, 0, 1, 512); 521 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
360 if (ret) 522 if (ret)
361 return ret; 523 return ret;
362 524
@@ -367,6 +529,7 @@ static int mmc_test_multi_write(struct mmc_test_card *test)
367{ 529{
368 int ret; 530 int ret;
369 unsigned int size; 531 unsigned int size;
532 struct scatterlist sg;
370 533
371 if (test->card->host->max_blk_count == 1) 534 if (test->card->host->max_blk_count == 1)
372 return RESULT_UNSUP_HOST; 535 return RESULT_UNSUP_HOST;
@@ -379,8 +542,9 @@ static int mmc_test_multi_write(struct mmc_test_card *test)
379 if (size < 1024) 542 if (size < 1024)
380 return RESULT_UNSUP_HOST; 543 return RESULT_UNSUP_HOST;
381 544
382 ret = mmc_test_verified_transfer(test, 1, test->buffer, 0, 545 sg_init_one(&sg, test->buffer, size);
383 size / 512, 512); 546
547 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
384 if (ret) 548 if (ret)
385 return ret; 549 return ret;
386 550
@@ -391,6 +555,7 @@ static int mmc_test_multi_read(struct mmc_test_card *test)
391{ 555{
392 int ret; 556 int ret;
393 unsigned int size; 557 unsigned int size;
558 struct scatterlist sg;
394 559
395 if (test->card->host->max_blk_count == 1) 560 if (test->card->host->max_blk_count == 1)
396 return RESULT_UNSUP_HOST; 561 return RESULT_UNSUP_HOST;
@@ -403,8 +568,9 @@ static int mmc_test_multi_read(struct mmc_test_card *test)
403 if (size < 1024) 568 if (size < 1024)
404 return RESULT_UNSUP_HOST; 569 return RESULT_UNSUP_HOST;
405 570
406 ret = mmc_test_verified_transfer(test, 0, test->buffer, 0, 571 sg_init_one(&sg, test->buffer, size);
407 size / 512, 512); 572
573 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
408 if (ret) 574 if (ret)
409 return ret; 575 return ret;
410 576
@@ -414,13 +580,14 @@ static int mmc_test_multi_read(struct mmc_test_card *test)
414static int mmc_test_pow2_write(struct mmc_test_card *test) 580static int mmc_test_pow2_write(struct mmc_test_card *test)
415{ 581{
416 int ret, i; 582 int ret, i;
583 struct scatterlist sg;
417 584
418 if (!test->card->csd.write_partial) 585 if (!test->card->csd.write_partial)
419 return RESULT_UNSUP_CARD; 586 return RESULT_UNSUP_CARD;
420 587
421 for (i = 1; i < 512;i <<= 1) { 588 for (i = 1; i < 512;i <<= 1) {
422 ret = mmc_test_verified_transfer(test, 1, 589 sg_init_one(&sg, test->buffer, i);
423 test->buffer, 0, 1, i); 590 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
424 if (ret) 591 if (ret)
425 return ret; 592 return ret;
426 } 593 }
@@ -431,13 +598,14 @@ static int mmc_test_pow2_write(struct mmc_test_card *test)
431static int mmc_test_pow2_read(struct mmc_test_card *test) 598static int mmc_test_pow2_read(struct mmc_test_card *test)
432{ 599{
433 int ret, i; 600 int ret, i;
601 struct scatterlist sg;
434 602
435 if (!test->card->csd.read_partial) 603 if (!test->card->csd.read_partial)
436 return RESULT_UNSUP_CARD; 604 return RESULT_UNSUP_CARD;
437 605
438 for (i = 1; i < 512;i <<= 1) { 606 for (i = 1; i < 512;i <<= 1) {
439 ret = mmc_test_verified_transfer(test, 0, 607 sg_init_one(&sg, test->buffer, i);
440 test->buffer, 0, 1, i); 608 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
441 if (ret) 609 if (ret)
442 return ret; 610 return ret;
443 } 611 }
@@ -448,13 +616,14 @@ static int mmc_test_pow2_read(struct mmc_test_card *test)
448static int mmc_test_weird_write(struct mmc_test_card *test) 616static int mmc_test_weird_write(struct mmc_test_card *test)
449{ 617{
450 int ret, i; 618 int ret, i;
619 struct scatterlist sg;
451 620
452 if (!test->card->csd.write_partial) 621 if (!test->card->csd.write_partial)
453 return RESULT_UNSUP_CARD; 622 return RESULT_UNSUP_CARD;
454 623
455 for (i = 3; i < 512;i += 7) { 624 for (i = 3; i < 512;i += 7) {
456 ret = mmc_test_verified_transfer(test, 1, 625 sg_init_one(&sg, test->buffer, i);
457 test->buffer, 0, 1, i); 626 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 1);
458 if (ret) 627 if (ret)
459 return ret; 628 return ret;
460 } 629 }
@@ -465,13 +634,14 @@ static int mmc_test_weird_write(struct mmc_test_card *test)
465static int mmc_test_weird_read(struct mmc_test_card *test) 634static int mmc_test_weird_read(struct mmc_test_card *test)
466{ 635{
467 int ret, i; 636 int ret, i;
637 struct scatterlist sg;
468 638
469 if (!test->card->csd.read_partial) 639 if (!test->card->csd.read_partial)
470 return RESULT_UNSUP_CARD; 640 return RESULT_UNSUP_CARD;
471 641
472 for (i = 3; i < 512;i += 7) { 642 for (i = 3; i < 512;i += 7) {
473 ret = mmc_test_verified_transfer(test, 0, 643 sg_init_one(&sg, test->buffer, i);
474 test->buffer, 0, 1, i); 644 ret = mmc_test_transfer(test, &sg, 1, 0, 1, i, 0);
475 if (ret) 645 if (ret)
476 return ret; 646 return ret;
477 } 647 }
@@ -482,10 +652,11 @@ static int mmc_test_weird_read(struct mmc_test_card *test)
482static int mmc_test_align_write(struct mmc_test_card *test) 652static int mmc_test_align_write(struct mmc_test_card *test)
483{ 653{
484 int ret, i; 654 int ret, i;
655 struct scatterlist sg;
485 656
486 for (i = 1;i < 4;i++) { 657 for (i = 1;i < 4;i++) {
487 ret = mmc_test_verified_transfer(test, 1, test->buffer + i, 658 sg_init_one(&sg, test->buffer + i, 512);
488 0, 1, 512); 659 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 1);
489 if (ret) 660 if (ret)
490 return ret; 661 return ret;
491 } 662 }
@@ -496,10 +667,11 @@ static int mmc_test_align_write(struct mmc_test_card *test)
496static int mmc_test_align_read(struct mmc_test_card *test) 667static int mmc_test_align_read(struct mmc_test_card *test)
497{ 668{
498 int ret, i; 669 int ret, i;
670 struct scatterlist sg;
499 671
500 for (i = 1;i < 4;i++) { 672 for (i = 1;i < 4;i++) {
501 ret = mmc_test_verified_transfer(test, 0, test->buffer + i, 673 sg_init_one(&sg, test->buffer + i, 512);
502 0, 1, 512); 674 ret = mmc_test_transfer(test, &sg, 1, 0, 1, 512, 0);
503 if (ret) 675 if (ret)
504 return ret; 676 return ret;
505 } 677 }
@@ -511,6 +683,7 @@ static int mmc_test_align_multi_write(struct mmc_test_card *test)
511{ 683{
512 int ret, i; 684 int ret, i;
513 unsigned int size; 685 unsigned int size;
686 struct scatterlist sg;
514 687
515 if (test->card->host->max_blk_count == 1) 688 if (test->card->host->max_blk_count == 1)
516 return RESULT_UNSUP_HOST; 689 return RESULT_UNSUP_HOST;
@@ -524,8 +697,8 @@ static int mmc_test_align_multi_write(struct mmc_test_card *test)
524 return RESULT_UNSUP_HOST; 697 return RESULT_UNSUP_HOST;
525 698
526 for (i = 1;i < 4;i++) { 699 for (i = 1;i < 4;i++) {
527 ret = mmc_test_verified_transfer(test, 1, test->buffer + i, 700 sg_init_one(&sg, test->buffer + i, size);
528 0, size / 512, 512); 701 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 1);
529 if (ret) 702 if (ret)
530 return ret; 703 return ret;
531 } 704 }
@@ -537,6 +710,7 @@ static int mmc_test_align_multi_read(struct mmc_test_card *test)
537{ 710{
538 int ret, i; 711 int ret, i;
539 unsigned int size; 712 unsigned int size;
713 struct scatterlist sg;
540 714
541 if (test->card->host->max_blk_count == 1) 715 if (test->card->host->max_blk_count == 1)
542 return RESULT_UNSUP_HOST; 716 return RESULT_UNSUP_HOST;
@@ -550,8 +724,8 @@ static int mmc_test_align_multi_read(struct mmc_test_card *test)
550 return RESULT_UNSUP_HOST; 724 return RESULT_UNSUP_HOST;
551 725
552 for (i = 1;i < 4;i++) { 726 for (i = 1;i < 4;i++) {
553 ret = mmc_test_verified_transfer(test, 0, test->buffer + i, 727 sg_init_one(&sg, test->buffer + i, size);
554 0, size / 512, 512); 728 ret = mmc_test_transfer(test, &sg, 1, 0, size/512, 512, 0);
555 if (ret) 729 if (ret)
556 return ret; 730 return ret;
557 } 731 }
@@ -567,7 +741,7 @@ static int mmc_test_xfersize_write(struct mmc_test_card *test)
567 if (ret) 741 if (ret)
568 return ret; 742 return ret;
569 743
570 ret = __mmc_test_transfer(test, 1, 1, test->buffer, 0, 1, 512); 744 ret = mmc_test_broken_transfer(test, 1, 512, 1);
571 if (ret) 745 if (ret)
572 return ret; 746 return ret;
573 747
@@ -582,7 +756,7 @@ static int mmc_test_xfersize_read(struct mmc_test_card *test)
582 if (ret) 756 if (ret)
583 return ret; 757 return ret;
584 758
585 ret = __mmc_test_transfer(test, 0, 1, test->buffer, 0, 1, 512); 759 ret = mmc_test_broken_transfer(test, 1, 512, 0);
586 if (ret) 760 if (ret)
587 return ret; 761 return ret;
588 762
@@ -600,7 +774,7 @@ static int mmc_test_multi_xfersize_write(struct mmc_test_card *test)
600 if (ret) 774 if (ret)
601 return ret; 775 return ret;
602 776
603 ret = __mmc_test_transfer(test, 1, 1, test->buffer, 0, 2, 512); 777 ret = mmc_test_broken_transfer(test, 2, 512, 1);
604 if (ret) 778 if (ret)
605 return ret; 779 return ret;
606 780
@@ -618,7 +792,7 @@ static int mmc_test_multi_xfersize_read(struct mmc_test_card *test)
618 if (ret) 792 if (ret)
619 return ret; 793 return ret;
620 794
621 ret = __mmc_test_transfer(test, 0, 1, test->buffer, 0, 2, 512); 795 ret = mmc_test_broken_transfer(test, 2, 512, 0);
622 if (ret) 796 if (ret)
623 return ret; 797 return ret;
624 798
@@ -638,86 +812,86 @@ static const struct mmc_test_case mmc_test_cases[] = {
638 812
639 { 813 {
640 .name = "Basic write (with data verification)", 814 .name = "Basic write (with data verification)",
641 .prepare = mmc_test_prepare_verify_write, 815 .prepare = mmc_test_prepare_write,
642 .run = mmc_test_verify_write, 816 .run = mmc_test_verify_write,
643 .cleanup = mmc_test_cleanup_verify, 817 .cleanup = mmc_test_cleanup,
644 }, 818 },
645 819
646 { 820 {
647 .name = "Basic read (with data verification)", 821 .name = "Basic read (with data verification)",
648 .prepare = mmc_test_prepare_verify_read, 822 .prepare = mmc_test_prepare_read,
649 .run = mmc_test_verify_read, 823 .run = mmc_test_verify_read,
650 .cleanup = mmc_test_cleanup_verify, 824 .cleanup = mmc_test_cleanup,
651 }, 825 },
652 826
653 { 827 {
654 .name = "Multi-block write", 828 .name = "Multi-block write",
655 .prepare = mmc_test_prepare_verify_write, 829 .prepare = mmc_test_prepare_write,
656 .run = mmc_test_multi_write, 830 .run = mmc_test_multi_write,
657 .cleanup = mmc_test_cleanup_verify, 831 .cleanup = mmc_test_cleanup,
658 }, 832 },
659 833
660 { 834 {
661 .name = "Multi-block read", 835 .name = "Multi-block read",
662 .prepare = mmc_test_prepare_verify_read, 836 .prepare = mmc_test_prepare_read,
663 .run = mmc_test_multi_read, 837 .run = mmc_test_multi_read,
664 .cleanup = mmc_test_cleanup_verify, 838 .cleanup = mmc_test_cleanup,
665 }, 839 },
666 840
667 { 841 {
668 .name = "Power of two block writes", 842 .name = "Power of two block writes",
669 .prepare = mmc_test_prepare_verify_write, 843 .prepare = mmc_test_prepare_write,
670 .run = mmc_test_pow2_write, 844 .run = mmc_test_pow2_write,
671 .cleanup = mmc_test_cleanup_verify, 845 .cleanup = mmc_test_cleanup,
672 }, 846 },
673 847
674 { 848 {
675 .name = "Power of two block reads", 849 .name = "Power of two block reads",
676 .prepare = mmc_test_prepare_verify_read, 850 .prepare = mmc_test_prepare_read,
677 .run = mmc_test_pow2_read, 851 .run = mmc_test_pow2_read,
678 .cleanup = mmc_test_cleanup_verify, 852 .cleanup = mmc_test_cleanup,
679 }, 853 },
680 854
681 { 855 {
682 .name = "Weird sized block writes", 856 .name = "Weird sized block writes",
683 .prepare = mmc_test_prepare_verify_write, 857 .prepare = mmc_test_prepare_write,
684 .run = mmc_test_weird_write, 858 .run = mmc_test_weird_write,
685 .cleanup = mmc_test_cleanup_verify, 859 .cleanup = mmc_test_cleanup,
686 }, 860 },
687 861
688 { 862 {
689 .name = "Weird sized block reads", 863 .name = "Weird sized block reads",
690 .prepare = mmc_test_prepare_verify_read, 864 .prepare = mmc_test_prepare_read,
691 .run = mmc_test_weird_read, 865 .run = mmc_test_weird_read,
692 .cleanup = mmc_test_cleanup_verify, 866 .cleanup = mmc_test_cleanup,
693 }, 867 },
694 868
695 { 869 {
696 .name = "Badly aligned write", 870 .name = "Badly aligned write",
697 .prepare = mmc_test_prepare_verify_write, 871 .prepare = mmc_test_prepare_write,
698 .run = mmc_test_align_write, 872 .run = mmc_test_align_write,
699 .cleanup = mmc_test_cleanup_verify, 873 .cleanup = mmc_test_cleanup,
700 }, 874 },
701 875
702 { 876 {
703 .name = "Badly aligned read", 877 .name = "Badly aligned read",
704 .prepare = mmc_test_prepare_verify_read, 878 .prepare = mmc_test_prepare_read,
705 .run = mmc_test_align_read, 879 .run = mmc_test_align_read,
706 .cleanup = mmc_test_cleanup_verify, 880 .cleanup = mmc_test_cleanup,
707 }, 881 },
708 882
709 { 883 {
710 .name = "Badly aligned multi-block write", 884 .name = "Badly aligned multi-block write",
711 .prepare = mmc_test_prepare_verify_write, 885 .prepare = mmc_test_prepare_write,
712 .run = mmc_test_align_multi_write, 886 .run = mmc_test_align_multi_write,
713 .cleanup = mmc_test_cleanup_verify, 887 .cleanup = mmc_test_cleanup,
714 }, 888 },
715 889
716 { 890 {
717 .name = "Badly aligned multi-block read", 891 .name = "Badly aligned multi-block read",
718 .prepare = mmc_test_prepare_verify_read, 892 .prepare = mmc_test_prepare_read,
719 .run = mmc_test_align_multi_read, 893 .run = mmc_test_align_multi_read,
720 .cleanup = mmc_test_cleanup_verify, 894 .cleanup = mmc_test_cleanup,
721 }, 895 },
722 896
723 { 897 {
@@ -743,7 +917,7 @@ static const struct mmc_test_case mmc_test_cases[] = {
743 917
744static struct mutex mmc_test_lock; 918static struct mutex mmc_test_lock;
745 919
746static void mmc_test_run(struct mmc_test_card *test) 920static void mmc_test_run(struct mmc_test_card *test, int testcase)
747{ 921{
748 int i, ret; 922 int i, ret;
749 923
@@ -753,6 +927,9 @@ static void mmc_test_run(struct mmc_test_card *test)
753 mmc_claim_host(test->card->host); 927 mmc_claim_host(test->card->host);
754 928
755 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) { 929 for (i = 0;i < ARRAY_SIZE(mmc_test_cases);i++) {
930 if (testcase && ((i + 1) != testcase))
931 continue;
932
756 printk(KERN_INFO "%s: Test case %d. %s...\n", 933 printk(KERN_INFO "%s: Test case %d. %s...\n",
757 mmc_hostname(test->card->host), i + 1, 934 mmc_hostname(test->card->host), i + 1,
758 mmc_test_cases[i].name); 935 mmc_test_cases[i].name);
@@ -824,9 +1001,12 @@ static ssize_t mmc_test_store(struct device *dev,
824{ 1001{
825 struct mmc_card *card; 1002 struct mmc_card *card;
826 struct mmc_test_card *test; 1003 struct mmc_test_card *test;
1004 int testcase;
827 1005
828 card = container_of(dev, struct mmc_card, dev); 1006 card = container_of(dev, struct mmc_card, dev);
829 1007
1008 testcase = simple_strtol(buf, NULL, 10);
1009
830 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL); 1010 test = kzalloc(sizeof(struct mmc_test_card), GFP_KERNEL);
831 if (!test) 1011 if (!test)
832 return -ENOMEM; 1012 return -ENOMEM;
@@ -836,7 +1016,7 @@ static ssize_t mmc_test_store(struct device *dev,
836 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL); 1016 test->buffer = kzalloc(BUFFER_SIZE, GFP_KERNEL);
837 if (test->buffer) { 1017 if (test->buffer) {
838 mutex_lock(&mmc_test_lock); 1018 mutex_lock(&mmc_test_lock);
839 mmc_test_run(test); 1019 mmc_test_run(test, testcase);
840 mutex_unlock(&mmc_test_lock); 1020 mutex_unlock(&mmc_test_lock);
841 } 1021 }
842 1022
@@ -852,6 +1032,9 @@ static int mmc_test_probe(struct mmc_card *card)
852{ 1032{
853 int ret; 1033 int ret;
854 1034
1035 if ((card->type != MMC_TYPE_MMC) && (card->type != MMC_TYPE_SD))
1036 return -ENODEV;
1037
855 mutex_init(&mmc_test_lock); 1038 mutex_init(&mmc_test_lock);
856 1039
857 ret = device_create_file(&card->dev, &dev_attr_test); 1040 ret = device_create_file(&card->dev, &dev_attr_test);
diff --git a/drivers/mmc/card/sdio_uart.c b/drivers/mmc/card/sdio_uart.c
index eeea84c309e6..78ad48718ab0 100644
--- a/drivers/mmc/card/sdio_uart.c
+++ b/drivers/mmc/card/sdio_uart.c
@@ -885,12 +885,14 @@ static void sdio_uart_set_termios(struct tty_struct *tty, struct ktermios *old_t
885 sdio_uart_release_func(port); 885 sdio_uart_release_func(port);
886} 886}
887 887
888static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state) 888static int sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
889{ 889{
890 struct sdio_uart_port *port = tty->driver_data; 890 struct sdio_uart_port *port = tty->driver_data;
891 int result;
891 892
892 if (sdio_uart_claim_func(port) != 0) 893 result = sdio_uart_claim_func(port);
893 return; 894 if (result != 0)
895 return result;
894 896
895 if (break_state == -1) 897 if (break_state == -1)
896 port->lcr |= UART_LCR_SBC; 898 port->lcr |= UART_LCR_SBC;
@@ -899,6 +901,7 @@ static void sdio_uart_break_ctl(struct tty_struct *tty, int break_state)
899 sdio_out(port, UART_LCR, port->lcr); 901 sdio_out(port, UART_LCR, port->lcr);
900 902
901 sdio_uart_release_func(port); 903 sdio_uart_release_func(port);
904 return 0;
902} 905}
903 906
904static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file) 907static int sdio_uart_tiocmget(struct tty_struct *tty, struct file *file)
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index 01ced4c5a61d..3ee5b8c3b5ce 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -3,7 +3,7 @@
3 * 3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. 6 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
@@ -295,6 +295,33 @@ void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
295EXPORT_SYMBOL(mmc_set_data_timeout); 295EXPORT_SYMBOL(mmc_set_data_timeout);
296 296
297/** 297/**
298 * mmc_align_data_size - pads a transfer size to a more optimal value
299 * @card: the MMC card associated with the data transfer
300 * @sz: original transfer size
301 *
302 * Pads the original data size with a number of extra bytes in
303 * order to avoid controller bugs and/or performance hits
304 * (e.g. some controllers revert to PIO for certain sizes).
305 *
306 * Returns the improved size, which might be unmodified.
307 *
308 * Note that this function is only relevant when issuing a
309 * single scatter gather entry.
310 */
311unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
312{
313 /*
314 * FIXME: We don't have a system for the controller to tell
315 * the core about its problems yet, so for now we just 32-bit
316 * align the size.
317 */
318 sz = ((sz + 3) / 4) * 4;
319
320 return sz;
321}
322EXPORT_SYMBOL(mmc_align_data_size);
323
324/**
298 * __mmc_claim_host - exclusively claim a host 325 * __mmc_claim_host - exclusively claim a host
299 * @host: mmc host to claim 326 * @host: mmc host to claim
300 * @abort: whether or not the operation should be aborted 327 * @abort: whether or not the operation should be aborted
@@ -638,6 +665,9 @@ void mmc_rescan(struct work_struct *work)
638 */ 665 */
639 mmc_bus_put(host); 666 mmc_bus_put(host);
640 667
668 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
669 goto out;
670
641 mmc_claim_host(host); 671 mmc_claim_host(host);
642 672
643 mmc_power_up(host); 673 mmc_power_up(host);
@@ -652,7 +682,7 @@ void mmc_rescan(struct work_struct *work)
652 if (!err) { 682 if (!err) {
653 if (mmc_attach_sdio(host, ocr)) 683 if (mmc_attach_sdio(host, ocr))
654 mmc_power_off(host); 684 mmc_power_off(host);
655 return; 685 goto out;
656 } 686 }
657 687
658 /* 688 /*
@@ -662,7 +692,7 @@ void mmc_rescan(struct work_struct *work)
662 if (!err) { 692 if (!err) {
663 if (mmc_attach_sd(host, ocr)) 693 if (mmc_attach_sd(host, ocr))
664 mmc_power_off(host); 694 mmc_power_off(host);
665 return; 695 goto out;
666 } 696 }
667 697
668 /* 698 /*
@@ -672,7 +702,7 @@ void mmc_rescan(struct work_struct *work)
672 if (!err) { 702 if (!err) {
673 if (mmc_attach_mmc(host, ocr)) 703 if (mmc_attach_mmc(host, ocr))
674 mmc_power_off(host); 704 mmc_power_off(host);
675 return; 705 goto out;
676 } 706 }
677 707
678 mmc_release_host(host); 708 mmc_release_host(host);
@@ -683,6 +713,9 @@ void mmc_rescan(struct work_struct *work)
683 713
684 mmc_bus_put(host); 714 mmc_bus_put(host);
685 } 715 }
716out:
717 if (host->caps & MMC_CAP_NEEDS_POLL)
718 mmc_schedule_delayed_work(&host->detect, HZ);
686} 719}
687 720
688void mmc_start_host(struct mmc_host *host) 721void mmc_start_host(struct mmc_host *host)
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index 3da29eef8f7d..fdd7c760be8c 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -288,7 +288,7 @@ static struct device_type mmc_type = {
288/* 288/*
289 * Handle the detection and initialisation of a card. 289 * Handle the detection and initialisation of a card.
290 * 290 *
291 * In the case of a resume, "curcard" will contain the card 291 * In the case of a resume, "oldcard" will contain the card
292 * we're trying to reinitialise. 292 * we're trying to reinitialise.
293 */ 293 */
294static int mmc_init_card(struct mmc_host *host, u32 ocr, 294static int mmc_init_card(struct mmc_host *host, u32 ocr,
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index 7ef3b15c5e3d..26fc098d77cd 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -326,7 +326,7 @@ static struct device_type sd_type = {
326/* 326/*
327 * Handle the detection and initialisation of a card. 327 * Handle the detection and initialisation of a card.
328 * 328 *
329 * In the case of a resume, "curcard" will contain the card 329 * In the case of a resume, "oldcard" will contain the card
330 * we're trying to reinitialise. 330 * we're trying to reinitialise.
331 */ 331 */
332static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, 332static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
@@ -494,13 +494,13 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
494 * Check if read-only switch is active. 494 * Check if read-only switch is active.
495 */ 495 */
496 if (!oldcard) { 496 if (!oldcard) {
497 if (!host->ops->get_ro) { 497 if (!host->ops->get_ro || host->ops->get_ro(host) < 0) {
498 printk(KERN_WARNING "%s: host does not " 498 printk(KERN_WARNING "%s: host does not "
499 "support reading read-only " 499 "support reading read-only "
500 "switch. assuming write-enable.\n", 500 "switch. assuming write-enable.\n",
501 mmc_hostname(host)); 501 mmc_hostname(host));
502 } else { 502 } else {
503 if (host->ops->get_ro(host)) 503 if (host->ops->get_ro(host) > 0)
504 mmc_card_set_readonly(card); 504 mmc_card_set_readonly(card);
505 } 505 }
506 } 506 }
diff --git a/drivers/mmc/core/sdio_cis.c b/drivers/mmc/core/sdio_cis.c
index d5e51b1c7b3f..956bd7677502 100644
--- a/drivers/mmc/core/sdio_cis.c
+++ b/drivers/mmc/core/sdio_cis.c
@@ -129,6 +129,12 @@ static int cistpl_funce_func(struct sdio_func *func,
129 /* TPLFE_MAX_BLK_SIZE */ 129 /* TPLFE_MAX_BLK_SIZE */
130 func->max_blksize = buf[12] | (buf[13] << 8); 130 func->max_blksize = buf[12] | (buf[13] << 8);
131 131
132 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
133 if (vsn > SDIO_SDIO_REV_1_00)
134 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
135 else
136 func->enable_timeout = jiffies_to_msecs(HZ);
137
132 return 0; 138 return 0;
133} 139}
134 140
diff --git a/drivers/mmc/core/sdio_io.c b/drivers/mmc/core/sdio_io.c
index 625b92ce9cef..f61fc2d4cd0a 100644
--- a/drivers/mmc/core/sdio_io.c
+++ b/drivers/mmc/core/sdio_io.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * linux/drivers/mmc/core/sdio_io.c 2 * linux/drivers/mmc/core/sdio_io.c
3 * 3 *
4 * Copyright 2007 Pierre Ossman 4 * Copyright 2007-2008 Pierre Ossman
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
@@ -76,11 +76,7 @@ int sdio_enable_func(struct sdio_func *func)
76 if (ret) 76 if (ret)
77 goto err; 77 goto err;
78 78
79 /* 79 timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
80 * FIXME: This should timeout based on information in the CIS,
81 * but we don't have card to parse that yet.
82 */
83 timeout = jiffies + HZ;
84 80
85 while (1) { 81 while (1) {
86 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg); 82 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
@@ -167,10 +163,8 @@ int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
167 return -EINVAL; 163 return -EINVAL;
168 164
169 if (blksz == 0) { 165 if (blksz == 0) {
170 blksz = min(min( 166 blksz = min(func->max_blksize, func->card->host->max_blk_size);
171 func->max_blksize, 167 blksz = min(blksz, 512u);
172 func->card->host->max_blk_size),
173 512u);
174 } 168 }
175 169
176 ret = mmc_io_rw_direct(func->card, 1, 0, 170 ret = mmc_io_rw_direct(func->card, 1, 0,
@@ -186,9 +180,116 @@ int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
186 func->cur_blksize = blksz; 180 func->cur_blksize = blksz;
187 return 0; 181 return 0;
188} 182}
189
190EXPORT_SYMBOL_GPL(sdio_set_block_size); 183EXPORT_SYMBOL_GPL(sdio_set_block_size);
191 184
185/*
186 * Calculate the maximum byte mode transfer size
187 */
188static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
189{
190 unsigned mval = min(func->card->host->max_seg_size,
191 func->card->host->max_blk_size);
192 mval = min(mval, func->max_blksize);
193 return min(mval, 512u); /* maximum size for byte mode */
194}
195
196/**
197 * sdio_align_size - pads a transfer size to a more optimal value
198 * @func: SDIO function
199 * @sz: original transfer size
200 *
201 * Pads the original data size with a number of extra bytes in
202 * order to avoid controller bugs and/or performance hits
203 * (e.g. some controllers revert to PIO for certain sizes).
204 *
205 * If possible, it will also adjust the size so that it can be
206 * handled in just a single request.
207 *
208 * Returns the improved size, which might be unmodified.
209 */
210unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
211{
212 unsigned int orig_sz;
213 unsigned int blk_sz, byte_sz;
214 unsigned chunk_sz;
215
216 orig_sz = sz;
217
218 /*
219 * Do a first check with the controller, in case it
220 * wants to increase the size up to a point where it
221 * might need more than one block.
222 */
223 sz = mmc_align_data_size(func->card, sz);
224
225 /*
226 * If we can still do this with just a byte transfer, then
227 * we're done.
228 */
229 if (sz <= sdio_max_byte_size(func))
230 return sz;
231
232 if (func->card->cccr.multi_block) {
233 /*
234 * Check if the transfer is already block aligned
235 */
236 if ((sz % func->cur_blksize) == 0)
237 return sz;
238
239 /*
240 * Realign it so that it can be done with one request,
241 * and recheck if the controller still likes it.
242 */
243 blk_sz = ((sz + func->cur_blksize - 1) /
244 func->cur_blksize) * func->cur_blksize;
245 blk_sz = mmc_align_data_size(func->card, blk_sz);
246
247 /*
248 * This value is only good if it is still just
249 * one request.
250 */
251 if ((blk_sz % func->cur_blksize) == 0)
252 return blk_sz;
253
254 /*
255 * We failed to do one request, but at least try to
256 * pad the remainder properly.
257 */
258 byte_sz = mmc_align_data_size(func->card,
259 sz % func->cur_blksize);
260 if (byte_sz <= sdio_max_byte_size(func)) {
261 blk_sz = sz / func->cur_blksize;
262 return blk_sz * func->cur_blksize + byte_sz;
263 }
264 } else {
265 /*
266 * We need multiple requests, so first check that the
267 * controller can handle the chunk size;
268 */
269 chunk_sz = mmc_align_data_size(func->card,
270 sdio_max_byte_size(func));
271 if (chunk_sz == sdio_max_byte_size(func)) {
272 /*
273 * Fix up the size of the remainder (if any)
274 */
275 byte_sz = orig_sz % chunk_sz;
276 if (byte_sz) {
277 byte_sz = mmc_align_data_size(func->card,
278 byte_sz);
279 }
280
281 return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
282 }
283 }
284
285 /*
286 * The controller is simply incapable of transferring the size
287 * we want in decent manner, so just return the original size.
288 */
289 return orig_sz;
290}
291EXPORT_SYMBOL_GPL(sdio_align_size);
292
192/* Split an arbitrarily sized data transfer into several 293/* Split an arbitrarily sized data transfer into several
193 * IO_RW_EXTENDED commands. */ 294 * IO_RW_EXTENDED commands. */
194static int sdio_io_rw_ext_helper(struct sdio_func *func, int write, 295static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
@@ -199,14 +300,13 @@ static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
199 int ret; 300 int ret;
200 301
201 /* Do the bulk of the transfer using block mode (if supported). */ 302 /* Do the bulk of the transfer using block mode (if supported). */
202 if (func->card->cccr.multi_block) { 303 if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
203 /* Blocks per command is limited by host count, host transfer 304 /* Blocks per command is limited by host count, host transfer
204 * size (we only use a single sg entry) and the maximum for 305 * size (we only use a single sg entry) and the maximum for
205 * IO_RW_EXTENDED of 511 blocks. */ 306 * IO_RW_EXTENDED of 511 blocks. */
206 max_blocks = min(min( 307 max_blocks = min(func->card->host->max_blk_count,
207 func->card->host->max_blk_count, 308 func->card->host->max_seg_size / func->cur_blksize);
208 func->card->host->max_seg_size / func->cur_blksize), 309 max_blocks = min(max_blocks, 511u);
209 511u);
210 310
211 while (remainder > func->cur_blksize) { 311 while (remainder > func->cur_blksize) {
212 unsigned blocks; 312 unsigned blocks;
@@ -231,11 +331,7 @@ static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
231 331
232 /* Write the remainder using byte mode. */ 332 /* Write the remainder using byte mode. */
233 while (remainder > 0) { 333 while (remainder > 0) {
234 size = remainder; 334 size = min(remainder, sdio_max_byte_size(func));
235 if (size > func->cur_blksize)
236 size = func->cur_blksize;
237 if (size > 512)
238 size = 512; /* maximum size for byte mode */
239 335
240 ret = mmc_io_rw_extended(func->card, write, func->num, addr, 336 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
241 incr_addr, buf, 1, size); 337 incr_addr, buf, 1, size);
@@ -260,11 +356,10 @@ static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
260 * function. If there is a problem reading the address, 0xff 356 * function. If there is a problem reading the address, 0xff
261 * is returned and @err_ret will contain the error code. 357 * is returned and @err_ret will contain the error code.
262 */ 358 */
263unsigned char sdio_readb(struct sdio_func *func, unsigned int addr, 359u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
264 int *err_ret)
265{ 360{
266 int ret; 361 int ret;
267 unsigned char val; 362 u8 val;
268 363
269 BUG_ON(!func); 364 BUG_ON(!func);
270 365
@@ -293,8 +388,7 @@ EXPORT_SYMBOL_GPL(sdio_readb);
293 * function. @err_ret will contain the status of the actual 388 * function. @err_ret will contain the status of the actual
294 * transfer. 389 * transfer.
295 */ 390 */
296void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr, 391void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
297 int *err_ret)
298{ 392{
299 int ret; 393 int ret;
300 394
@@ -355,7 +449,6 @@ int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
355{ 449{
356 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count); 450 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
357} 451}
358
359EXPORT_SYMBOL_GPL(sdio_readsb); 452EXPORT_SYMBOL_GPL(sdio_readsb);
360 453
361/** 454/**
@@ -385,8 +478,7 @@ EXPORT_SYMBOL_GPL(sdio_writesb);
385 * function. If there is a problem reading the address, 0xffff 478 * function. If there is a problem reading the address, 0xffff
386 * is returned and @err_ret will contain the error code. 479 * is returned and @err_ret will contain the error code.
387 */ 480 */
388unsigned short sdio_readw(struct sdio_func *func, unsigned int addr, 481u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
389 int *err_ret)
390{ 482{
391 int ret; 483 int ret;
392 484
@@ -400,7 +492,7 @@ unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
400 return 0xFFFF; 492 return 0xFFFF;
401 } 493 }
402 494
403 return le16_to_cpu(*(u16*)func->tmpbuf); 495 return le16_to_cpup((__le16 *)func->tmpbuf);
404} 496}
405EXPORT_SYMBOL_GPL(sdio_readw); 497EXPORT_SYMBOL_GPL(sdio_readw);
406 498
@@ -415,12 +507,11 @@ EXPORT_SYMBOL_GPL(sdio_readw);
415 * function. @err_ret will contain the status of the actual 507 * function. @err_ret will contain the status of the actual
416 * transfer. 508 * transfer.
417 */ 509 */
418void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr, 510void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
419 int *err_ret)
420{ 511{
421 int ret; 512 int ret;
422 513
423 *(u16*)func->tmpbuf = cpu_to_le16(b); 514 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
424 515
425 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2); 516 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
426 if (err_ret) 517 if (err_ret)
@@ -439,8 +530,7 @@ EXPORT_SYMBOL_GPL(sdio_writew);
439 * 0xffffffff is returned and @err_ret will contain the error 530 * 0xffffffff is returned and @err_ret will contain the error
440 * code. 531 * code.
441 */ 532 */
442unsigned long sdio_readl(struct sdio_func *func, unsigned int addr, 533u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
443 int *err_ret)
444{ 534{
445 int ret; 535 int ret;
446 536
@@ -454,7 +544,7 @@ unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
454 return 0xFFFFFFFF; 544 return 0xFFFFFFFF;
455 } 545 }
456 546
457 return le32_to_cpu(*(u32*)func->tmpbuf); 547 return le32_to_cpup((__le32 *)func->tmpbuf);
458} 548}
459EXPORT_SYMBOL_GPL(sdio_readl); 549EXPORT_SYMBOL_GPL(sdio_readl);
460 550
@@ -469,12 +559,11 @@ EXPORT_SYMBOL_GPL(sdio_readl);
469 * function. @err_ret will contain the status of the actual 559 * function. @err_ret will contain the status of the actual
470 * transfer. 560 * transfer.
471 */ 561 */
472void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr, 562void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
473 int *err_ret)
474{ 563{
475 int ret; 564 int ret;
476 565
477 *(u32*)func->tmpbuf = cpu_to_le32(b); 566 *(__le32 *)func->tmpbuf = cpu_to_le32(b);
478 567
479 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4); 568 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
480 if (err_ret) 569 if (err_ret)
diff --git a/drivers/mmc/host/Kconfig b/drivers/mmc/host/Kconfig
index dead61754ad7..dc6f2579f85c 100644
--- a/drivers/mmc/host/Kconfig
+++ b/drivers/mmc/host/Kconfig
@@ -26,18 +26,31 @@ config MMC_PXA
26 26
27config MMC_SDHCI 27config MMC_SDHCI
28 tristate "Secure Digital Host Controller Interface support" 28 tristate "Secure Digital Host Controller Interface support"
29 depends on PCI 29 depends on HAS_DMA
30 help 30 help
31 This select the generic Secure Digital Host Controller Interface. 31 This selects the generic Secure Digital Host Controller Interface.
32 It is used by manufacturers such as Texas Instruments(R), Ricoh(R) 32 It is used by manufacturers such as Texas Instruments(R), Ricoh(R)
33 and Toshiba(R). Most controllers found in laptops are of this type. 33 and Toshiba(R). Most controllers found in laptops are of this type.
34
35 If you have a controller with this interface, say Y or M here. You
36 also need to enable an appropriate bus interface.
37
38 If unsure, say N.
39
40config MMC_SDHCI_PCI
41 tristate "SDHCI support on PCI bus"
42 depends on MMC_SDHCI && PCI
43 help
44 This selects the PCI Secure Digital Host Controller Interface.
45 Most controllers found today are PCI devices.
46
34 If you have a controller with this interface, say Y or M here. 47 If you have a controller with this interface, say Y or M here.
35 48
36 If unsure, say N. 49 If unsure, say N.
37 50
38config MMC_RICOH_MMC 51config MMC_RICOH_MMC
39 tristate "Ricoh MMC Controller Disabler (EXPERIMENTAL)" 52 tristate "Ricoh MMC Controller Disabler (EXPERIMENTAL)"
40 depends on PCI && EXPERIMENTAL && MMC_SDHCI 53 depends on MMC_SDHCI_PCI
41 help 54 help
42 This selects the disabler for the Ricoh MMC Controller. This 55 This selects the disabler for the Ricoh MMC Controller. This
43 proprietary controller is unnecessary because the SDHCI driver 56 proprietary controller is unnecessary because the SDHCI driver
@@ -91,6 +104,16 @@ config MMC_AT91
91 104
92 If unsure, say N. 105 If unsure, say N.
93 106
107config MMC_ATMELMCI
108 tristate "Atmel Multimedia Card Interface support"
109 depends on AVR32
110 help
111 This selects the Atmel Multimedia Card Interface driver. If
112 you have an AT32 (AVR32) platform with a Multimedia Card
113 slot, say Y or M here.
114
115 If unsure, say N.
116
94config MMC_IMX 117config MMC_IMX
95 tristate "Motorola i.MX Multimedia Card Interface support" 118 tristate "Motorola i.MX Multimedia Card Interface support"
96 depends on ARCH_IMX 119 depends on ARCH_IMX
@@ -130,3 +153,24 @@ config MMC_SPI
130 153
131 If unsure, or if your system has no SPI master driver, say N. 154 If unsure, or if your system has no SPI master driver, say N.
132 155
156config MMC_S3C
157 tristate "Samsung S3C SD/MMC Card Interface support"
158 depends on ARCH_S3C2410 && MMC
159 help
160 This selects a driver for the MCI interface found in
161 Samsung's S3C2410, S3C2412, S3C2440, S3C2442 CPUs.
162 If you have a board based on one of those and a MMC/SD
163 slot, say Y or M here.
164
165 If unsure, say N.
166
167config MMC_SDRICOH_CS
168 tristate "MMC/SD driver for Ricoh Bay1Controllers (EXPERIMENTAL)"
169 depends on EXPERIMENTAL && MMC && PCI && PCMCIA
170 help
171 Say Y here if your Notebook reports a Ricoh Bay1Controller PCMCIA
172 card whenever you insert a MMC or SD card into the card slot.
173
174 To compile this driver as a module, choose M here: the
175 module will be called sdricoh_cs.
176
diff --git a/drivers/mmc/host/Makefile b/drivers/mmc/host/Makefile
index 3877c87e6da2..db52eebfb50e 100644
--- a/drivers/mmc/host/Makefile
+++ b/drivers/mmc/host/Makefile
@@ -10,11 +10,15 @@ obj-$(CONFIG_MMC_ARMMMCI) += mmci.o
10obj-$(CONFIG_MMC_PXA) += pxamci.o 10obj-$(CONFIG_MMC_PXA) += pxamci.o
11obj-$(CONFIG_MMC_IMX) += imxmmc.o 11obj-$(CONFIG_MMC_IMX) += imxmmc.o
12obj-$(CONFIG_MMC_SDHCI) += sdhci.o 12obj-$(CONFIG_MMC_SDHCI) += sdhci.o
13obj-$(CONFIG_MMC_SDHCI_PCI) += sdhci-pci.o
13obj-$(CONFIG_MMC_RICOH_MMC) += ricoh_mmc.o 14obj-$(CONFIG_MMC_RICOH_MMC) += ricoh_mmc.o
14obj-$(CONFIG_MMC_WBSD) += wbsd.o 15obj-$(CONFIG_MMC_WBSD) += wbsd.o
15obj-$(CONFIG_MMC_AU1X) += au1xmmc.o 16obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
16obj-$(CONFIG_MMC_OMAP) += omap.o 17obj-$(CONFIG_MMC_OMAP) += omap.o
17obj-$(CONFIG_MMC_AT91) += at91_mci.o 18obj-$(CONFIG_MMC_AT91) += at91_mci.o
19obj-$(CONFIG_MMC_ATMELMCI) += atmel-mci.o
18obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o 20obj-$(CONFIG_MMC_TIFM_SD) += tifm_sd.o
19obj-$(CONFIG_MMC_SPI) += mmc_spi.o 21obj-$(CONFIG_MMC_SPI) += mmc_spi.o
22obj-$(CONFIG_MMC_S3C) += s3cmci.o
23obj-$(CONFIG_MMC_SDRICOH_CS) += sdricoh_cs.o
20 24
diff --git a/drivers/mmc/host/at91_mci.c b/drivers/mmc/host/at91_mci.c
index 8979ad330a4d..f15e2064305c 100644
--- a/drivers/mmc/host/at91_mci.c
+++ b/drivers/mmc/host/at91_mci.c
@@ -125,9 +125,72 @@ struct at91mci_host
125 125
126 /* Latest in the scatterlist that has been enabled for transfer */ 126 /* Latest in the scatterlist that has been enabled for transfer */
127 int transfer_index; 127 int transfer_index;
128
129 /* Timer for timeouts */
130 struct timer_list timer;
128}; 131};
129 132
130/* 133/*
134 * Reset the controller and restore most of the state
135 */
136static void at91_reset_host(struct at91mci_host *host)
137{
138 unsigned long flags;
139 u32 mr;
140 u32 sdcr;
141 u32 dtor;
142 u32 imr;
143
144 local_irq_save(flags);
145 imr = at91_mci_read(host, AT91_MCI_IMR);
146
147 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
148
149 /* save current state */
150 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
151 sdcr = at91_mci_read(host, AT91_MCI_SDCR);
152 dtor = at91_mci_read(host, AT91_MCI_DTOR);
153
154 /* reset the controller */
155 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
156
157 /* restore state */
158 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
159 at91_mci_write(host, AT91_MCI_MR, mr);
160 at91_mci_write(host, AT91_MCI_SDCR, sdcr);
161 at91_mci_write(host, AT91_MCI_DTOR, dtor);
162 at91_mci_write(host, AT91_MCI_IER, imr);
163
164 /* make sure sdio interrupts will fire */
165 at91_mci_read(host, AT91_MCI_SR);
166
167 local_irq_restore(flags);
168}
169
170static void at91_timeout_timer(unsigned long data)
171{
172 struct at91mci_host *host;
173
174 host = (struct at91mci_host *)data;
175
176 if (host->request) {
177 dev_err(host->mmc->parent, "Timeout waiting end of packet\n");
178
179 if (host->cmd && host->cmd->data) {
180 host->cmd->data->error = -ETIMEDOUT;
181 } else {
182 if (host->cmd)
183 host->cmd->error = -ETIMEDOUT;
184 else
185 host->request->cmd->error = -ETIMEDOUT;
186 }
187
188 at91_reset_host(host);
189 mmc_request_done(host->mmc, host->request);
190 }
191}
192
193/*
131 * Copy from sg to a dma block - used for transfers 194 * Copy from sg to a dma block - used for transfers
132 */ 195 */
133static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data) 196static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
@@ -135,9 +198,14 @@ static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data
135 unsigned int len, i, size; 198 unsigned int len, i, size;
136 unsigned *dmabuf = host->buffer; 199 unsigned *dmabuf = host->buffer;
137 200
138 size = host->total_length; 201 size = data->blksz * data->blocks;
139 len = data->sg_len; 202 len = data->sg_len;
140 203
204 /* AT91SAM926[0/3] Data Write Operation and number of bytes erratum */
205 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
206 if (host->total_length == 12)
207 memset(dmabuf, 0, 12);
208
141 /* 209 /*
142 * Just loop through all entries. Size might not 210 * Just loop through all entries. Size might not
143 * be the entire list though so make sure that 211 * be the entire list though so make sure that
@@ -159,9 +227,10 @@ static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data
159 227
160 for (index = 0; index < (amount / 4); index++) 228 for (index = 0; index < (amount / 4); index++)
161 *dmabuf++ = swab32(sgbuffer[index]); 229 *dmabuf++ = swab32(sgbuffer[index]);
162 } 230 } else {
163 else
164 memcpy(dmabuf, sgbuffer, amount); 231 memcpy(dmabuf, sgbuffer, amount);
232 dmabuf += amount;
233 }
165 234
166 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ); 235 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
167 236
@@ -233,11 +302,11 @@ static void at91_mci_pre_dma_read(struct at91mci_host *host)
233 302
234 if (i == 0) { 303 if (i == 0) {
235 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address); 304 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
236 at91_mci_write(host, ATMEL_PDC_RCR, sg->length / 4); 305 at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
237 } 306 }
238 else { 307 else {
239 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address); 308 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
240 at91_mci_write(host, ATMEL_PDC_RNCR, sg->length / 4); 309 at91_mci_write(host, ATMEL_PDC_RNCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
241 } 310 }
242 } 311 }
243 312
@@ -277,8 +346,6 @@ static void at91_mci_post_dma_read(struct at91mci_host *host)
277 346
278 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE); 347 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
279 348
280 data->bytes_xfered += sg->length;
281
282 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */ 349 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
283 unsigned int *buffer; 350 unsigned int *buffer;
284 int index; 351 int index;
@@ -294,6 +361,8 @@ static void at91_mci_post_dma_read(struct at91mci_host *host)
294 } 361 }
295 362
296 flush_dcache_page(sg_page(sg)); 363 flush_dcache_page(sg_page(sg));
364
365 data->bytes_xfered += sg->length;
297 } 366 }
298 367
299 /* Is there another transfer to trigger? */ 368 /* Is there another transfer to trigger? */
@@ -334,10 +403,32 @@ static void at91_mci_handle_transmitted(struct at91mci_host *host)
334 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE); 403 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
335 } else 404 } else
336 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY); 405 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
406}
407
408/*
409 * Update bytes tranfered count during a write operation
410 */
411static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
412{
413 struct mmc_data *data;
414
415 /* always deal with the effective request (and not the current cmd) */
416
417 if (host->request->cmd && host->request->cmd->error != 0)
418 return;
337 419
338 data->bytes_xfered = host->total_length; 420 if (host->request->data) {
421 data = host->request->data;
422 if (data->flags & MMC_DATA_WRITE) {
423 /* card is in IDLE mode now */
424 pr_debug("-> bytes_xfered %d, total_length = %d\n",
425 data->bytes_xfered, host->total_length);
426 data->bytes_xfered = data->blksz * data->blocks;
427 }
428 }
339} 429}
340 430
431
341/*Handle after command sent ready*/ 432/*Handle after command sent ready*/
342static int at91_mci_handle_cmdrdy(struct at91mci_host *host) 433static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
343{ 434{
@@ -350,8 +441,7 @@ static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
350 } else return 1; 441 } else return 1;
351 } else if (host->cmd->data->flags & MMC_DATA_WRITE) { 442 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
352 /*After sendding multi-block-write command, start DMA transfer*/ 443 /*After sendding multi-block-write command, start DMA transfer*/
353 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE); 444 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
354 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
355 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 445 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
356 } 446 }
357 447
@@ -430,11 +520,19 @@ static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command
430 520
431 if (data) { 521 if (data) {
432 522
433 if ( data->blksz & 0x3 ) { 523 if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
434 pr_debug("Unsupported block size\n"); 524 if (data->blksz & 0x3) {
435 cmd->error = -EINVAL; 525 pr_debug("Unsupported block size\n");
436 mmc_request_done(host->mmc, host->request); 526 cmd->error = -EINVAL;
437 return; 527 mmc_request_done(host->mmc, host->request);
528 return;
529 }
530 if (data->flags & MMC_DATA_STREAM) {
531 pr_debug("Stream commands not supported\n");
532 cmd->error = -EINVAL;
533 mmc_request_done(host->mmc, host->request);
534 return;
535 }
438 } 536 }
439 537
440 block_length = data->blksz; 538 block_length = data->blksz;
@@ -481,8 +579,16 @@ static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command
481 ier = AT91_MCI_CMDRDY; 579 ier = AT91_MCI_CMDRDY;
482 } else { 580 } else {
483 /* zero block length and PDC mode */ 581 /* zero block length and PDC mode */
484 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff; 582 mr = at91_mci_read(host, AT91_MCI_MR) & 0x5fff;
485 at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE); 583 mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
584 mr |= (block_length << 16);
585 mr |= AT91_MCI_PDCMODE;
586 at91_mci_write(host, AT91_MCI_MR, mr);
587
588 if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
589 at91_mci_write(host, AT91_MCI_BLKR,
590 AT91_MCI_BLKR_BCNT(blocks) |
591 AT91_MCI_BLKR_BLKLEN(block_length));
486 592
487 /* 593 /*
488 * Disable the PDC controller 594 * Disable the PDC controller
@@ -508,6 +614,13 @@ static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command
508 * Handle a write 614 * Handle a write
509 */ 615 */
510 host->total_length = block_length * blocks; 616 host->total_length = block_length * blocks;
617 /*
618 * AT91SAM926[0/3] Data Write Operation and
619 * number of bytes erratum
620 */
621 if (cpu_is_at91sam9260 () || cpu_is_at91sam9263())
622 if (host->total_length < 12)
623 host->total_length = 12;
511 host->buffer = dma_alloc_coherent(NULL, 624 host->buffer = dma_alloc_coherent(NULL,
512 host->total_length, 625 host->total_length,
513 &host->physical_address, GFP_KERNEL); 626 &host->physical_address, GFP_KERNEL);
@@ -517,7 +630,9 @@ static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command
517 pr_debug("Transmitting %d bytes\n", host->total_length); 630 pr_debug("Transmitting %d bytes\n", host->total_length);
518 631
519 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address); 632 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
520 at91_mci_write(host, ATMEL_PDC_TCR, host->total_length / 4); 633 at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
634 host->total_length : host->total_length / 4);
635
521 ier = AT91_MCI_CMDRDY; 636 ier = AT91_MCI_CMDRDY;
522 } 637 }
523 } 638 }
@@ -552,20 +667,26 @@ static void at91_mci_process_next(struct at91mci_host *host)
552 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) { 667 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
553 host->flags |= FL_SENT_STOP; 668 host->flags |= FL_SENT_STOP;
554 at91_mci_send_command(host, host->request->stop); 669 at91_mci_send_command(host, host->request->stop);
555 } 670 } else {
556 else 671 del_timer(&host->timer);
672 /* the at91rm9200 mci controller hangs after some transfers,
673 * and the workaround is to reset it after each transfer.
674 */
675 if (cpu_is_at91rm9200())
676 at91_reset_host(host);
557 mmc_request_done(host->mmc, host->request); 677 mmc_request_done(host->mmc, host->request);
678 }
558} 679}
559 680
560/* 681/*
561 * Handle a command that has been completed 682 * Handle a command that has been completed
562 */ 683 */
563static void at91_mci_completed_command(struct at91mci_host *host) 684static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
564{ 685{
565 struct mmc_command *cmd = host->cmd; 686 struct mmc_command *cmd = host->cmd;
566 unsigned int status; 687 struct mmc_data *data = cmd->data;
567 688
568 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff); 689 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
569 690
570 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0)); 691 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
571 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1)); 692 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
@@ -577,25 +698,34 @@ static void at91_mci_completed_command(struct at91mci_host *host)
577 host->buffer = NULL; 698 host->buffer = NULL;
578 } 699 }
579 700
580 status = at91_mci_read(host, AT91_MCI_SR); 701 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
581 702 status, at91_mci_read(host, AT91_MCI_SR),
582 pr_debug("Status = %08X [%08X %08X %08X %08X]\n", 703 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
583 status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
584 704
585 if (status & AT91_MCI_ERRORS) { 705 if (status & AT91_MCI_ERRORS) {
586 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) { 706 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
587 cmd->error = 0; 707 cmd->error = 0;
588 } 708 }
589 else { 709 else {
590 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE)) 710 if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE)) {
591 cmd->error = -ETIMEDOUT; 711 if (data) {
592 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE)) 712 if (status & AT91_MCI_DTOE)
593 cmd->error = -EILSEQ; 713 data->error = -ETIMEDOUT;
594 else 714 else if (status & AT91_MCI_DCRCE)
595 cmd->error = -EIO; 715 data->error = -EILSEQ;
716 }
717 } else {
718 if (status & AT91_MCI_RTOE)
719 cmd->error = -ETIMEDOUT;
720 else if (status & AT91_MCI_RCRCE)
721 cmd->error = -EILSEQ;
722 else
723 cmd->error = -EIO;
724 }
596 725
597 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n", 726 pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
598 cmd->error, cmd->opcode, cmd->retries); 727 cmd->error, data ? data->error : 0,
728 cmd->opcode, cmd->retries);
599 } 729 }
600 } 730 }
601 else 731 else
@@ -613,6 +743,8 @@ static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
613 host->request = mrq; 743 host->request = mrq;
614 host->flags = 0; 744 host->flags = 0;
615 745
746 mod_timer(&host->timer, jiffies + HZ);
747
616 at91_mci_process_next(host); 748 at91_mci_process_next(host);
617} 749}
618 750
@@ -736,6 +868,7 @@ static irqreturn_t at91_mci_irq(int irq, void *devid)
736 868
737 if (int_status & AT91_MCI_NOTBUSY) { 869 if (int_status & AT91_MCI_NOTBUSY) {
738 pr_debug("Card is ready\n"); 870 pr_debug("Card is ready\n");
871 at91_mci_update_bytes_xfered(host);
739 completed = 1; 872 completed = 1;
740 } 873 }
741 874
@@ -744,9 +877,21 @@ static irqreturn_t at91_mci_irq(int irq, void *devid)
744 877
745 if (int_status & AT91_MCI_BLKE) { 878 if (int_status & AT91_MCI_BLKE) {
746 pr_debug("Block transfer has ended\n"); 879 pr_debug("Block transfer has ended\n");
747 completed = 1; 880 if (host->request->data && host->request->data->blocks > 1) {
881 /* multi block write : complete multi write
882 * command and send stop */
883 completed = 1;
884 } else {
885 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
886 }
748 } 887 }
749 888
889 if (int_status & AT91_MCI_SDIOIRQA)
890 mmc_signal_sdio_irq(host->mmc);
891
892 if (int_status & AT91_MCI_SDIOIRQB)
893 mmc_signal_sdio_irq(host->mmc);
894
750 if (int_status & AT91_MCI_TXRDY) 895 if (int_status & AT91_MCI_TXRDY)
751 pr_debug("Ready to transmit\n"); 896 pr_debug("Ready to transmit\n");
752 897
@@ -761,10 +906,10 @@ static irqreturn_t at91_mci_irq(int irq, void *devid)
761 906
762 if (completed) { 907 if (completed) {
763 pr_debug("Completed command\n"); 908 pr_debug("Completed command\n");
764 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff); 909 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
765 at91_mci_completed_command(host); 910 at91_mci_completed_command(host, int_status);
766 } else 911 } else
767 at91_mci_write(host, AT91_MCI_IDR, int_status); 912 at91_mci_write(host, AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
768 913
769 return IRQ_HANDLED; 914 return IRQ_HANDLED;
770} 915}
@@ -793,25 +938,33 @@ static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
793 938
794static int at91_mci_get_ro(struct mmc_host *mmc) 939static int at91_mci_get_ro(struct mmc_host *mmc)
795{ 940{
796 int read_only = 0;
797 struct at91mci_host *host = mmc_priv(mmc); 941 struct at91mci_host *host = mmc_priv(mmc);
798 942
799 if (host->board->wp_pin) { 943 if (host->board->wp_pin)
800 read_only = gpio_get_value(host->board->wp_pin); 944 return !!gpio_get_value(host->board->wp_pin);
801 printk(KERN_WARNING "%s: card is %s\n", mmc_hostname(mmc), 945 /*
802 (read_only ? "read-only" : "read-write") ); 946 * Board doesn't support read only detection; let the mmc core
803 } 947 * decide what to do.
804 else { 948 */
805 printk(KERN_WARNING "%s: host does not support reading read-only " 949 return -ENOSYS;
806 "switch. Assuming write-enable.\n", mmc_hostname(mmc)); 950}
807 } 951
808 return read_only; 952static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
953{
954 struct at91mci_host *host = mmc_priv(mmc);
955
956 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
957 host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
958 at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
959 host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
960
809} 961}
810 962
811static const struct mmc_host_ops at91_mci_ops = { 963static const struct mmc_host_ops at91_mci_ops = {
812 .request = at91_mci_request, 964 .request = at91_mci_request,
813 .set_ios = at91_mci_set_ios, 965 .set_ios = at91_mci_set_ios,
814 .get_ro = at91_mci_get_ro, 966 .get_ro = at91_mci_get_ro,
967 .enable_sdio_irq = at91_mci_enable_sdio_irq,
815}; 968};
816 969
817/* 970/*
@@ -842,6 +995,7 @@ static int __init at91_mci_probe(struct platform_device *pdev)
842 mmc->f_min = 375000; 995 mmc->f_min = 375000;
843 mmc->f_max = 25000000; 996 mmc->f_max = 25000000;
844 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 997 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
998 mmc->caps = MMC_CAP_SDIO_IRQ;
845 999
846 mmc->max_blk_size = 4095; 1000 mmc->max_blk_size = 4095;
847 mmc->max_blk_count = mmc->max_req_size; 1001 mmc->max_blk_count = mmc->max_req_size;
@@ -935,6 +1089,8 @@ static int __init at91_mci_probe(struct platform_device *pdev)
935 1089
936 mmc_add_host(mmc); 1090 mmc_add_host(mmc);
937 1091
1092 setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
1093
938 /* 1094 /*
939 * monitor card insertion/removal if we can 1095 * monitor card insertion/removal if we can
940 */ 1096 */
@@ -995,6 +1151,7 @@ static int __exit at91_mci_remove(struct platform_device *pdev)
995 } 1151 }
996 1152
997 at91_mci_disable(host); 1153 at91_mci_disable(host);
1154 del_timer_sync(&host->timer);
998 mmc_remove_host(mmc); 1155 mmc_remove_host(mmc);
999 free_irq(host->irq, host); 1156 free_irq(host->irq, host);
1000 1157
diff --git a/drivers/mmc/host/atmel-mci-regs.h b/drivers/mmc/host/atmel-mci-regs.h
new file mode 100644
index 000000000000..a9a5657706c6
--- /dev/null
+++ b/drivers/mmc/host/atmel-mci-regs.h
@@ -0,0 +1,91 @@
1/*
2 * Atmel MultiMedia Card Interface driver
3 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#ifndef __DRIVERS_MMC_ATMEL_MCI_H__
11#define __DRIVERS_MMC_ATMEL_MCI_H__
12
13/* MCI Register Definitions */
14#define MCI_CR 0x0000 /* Control */
15# define MCI_CR_MCIEN ( 1 << 0) /* MCI Enable */
16# define MCI_CR_MCIDIS ( 1 << 1) /* MCI Disable */
17# define MCI_CR_SWRST ( 1 << 7) /* Software Reset */
18#define MCI_MR 0x0004 /* Mode */
19# define MCI_MR_CLKDIV(x) ((x) << 0) /* Clock Divider */
20# define MCI_MR_RDPROOF ( 1 << 11) /* Read Proof */
21# define MCI_MR_WRPROOF ( 1 << 12) /* Write Proof */
22#define MCI_DTOR 0x0008 /* Data Timeout */
23# define MCI_DTOCYC(x) ((x) << 0) /* Data Timeout Cycles */
24# define MCI_DTOMUL(x) ((x) << 4) /* Data Timeout Multiplier */
25#define MCI_SDCR 0x000c /* SD Card / SDIO */
26# define MCI_SDCSEL_SLOT_A ( 0 << 0) /* Select SD slot A */
27# define MCI_SDCSEL_SLOT_B ( 1 << 0) /* Select SD slot A */
28# define MCI_SDCBUS_1BIT ( 0 << 7) /* 1-bit data bus */
29# define MCI_SDCBUS_4BIT ( 1 << 7) /* 4-bit data bus */
30#define MCI_ARGR 0x0010 /* Command Argument */
31#define MCI_CMDR 0x0014 /* Command */
32# define MCI_CMDR_CMDNB(x) ((x) << 0) /* Command Opcode */
33# define MCI_CMDR_RSPTYP_NONE ( 0 << 6) /* No response */
34# define MCI_CMDR_RSPTYP_48BIT ( 1 << 6) /* 48-bit response */
35# define MCI_CMDR_RSPTYP_136BIT ( 2 << 6) /* 136-bit response */
36# define MCI_CMDR_SPCMD_INIT ( 1 << 8) /* Initialization command */
37# define MCI_CMDR_SPCMD_SYNC ( 2 << 8) /* Synchronized command */
38# define MCI_CMDR_SPCMD_INT ( 4 << 8) /* Interrupt command */
39# define MCI_CMDR_SPCMD_INTRESP ( 5 << 8) /* Interrupt response */
40# define MCI_CMDR_OPDCMD ( 1 << 11) /* Open Drain */
41# define MCI_CMDR_MAXLAT_5CYC ( 0 << 12) /* Max latency 5 cycles */
42# define MCI_CMDR_MAXLAT_64CYC ( 1 << 12) /* Max latency 64 cycles */
43# define MCI_CMDR_START_XFER ( 1 << 16) /* Start data transfer */
44# define MCI_CMDR_STOP_XFER ( 2 << 16) /* Stop data transfer */
45# define MCI_CMDR_TRDIR_WRITE ( 0 << 18) /* Write data */
46# define MCI_CMDR_TRDIR_READ ( 1 << 18) /* Read data */
47# define MCI_CMDR_BLOCK ( 0 << 19) /* Single-block transfer */
48# define MCI_CMDR_MULTI_BLOCK ( 1 << 19) /* Multi-block transfer */
49# define MCI_CMDR_STREAM ( 2 << 19) /* MMC Stream transfer */
50# define MCI_CMDR_SDIO_BYTE ( 4 << 19) /* SDIO Byte transfer */
51# define MCI_CMDR_SDIO_BLOCK ( 5 << 19) /* SDIO Block transfer */
52# define MCI_CMDR_SDIO_SUSPEND ( 1 << 24) /* SDIO Suspend Command */
53# define MCI_CMDR_SDIO_RESUME ( 2 << 24) /* SDIO Resume Command */
54#define MCI_BLKR 0x0018 /* Block */
55# define MCI_BCNT(x) ((x) << 0) /* Data Block Count */
56# define MCI_BLKLEN(x) ((x) << 16) /* Data Block Length */
57#define MCI_RSPR 0x0020 /* Response 0 */
58#define MCI_RSPR1 0x0024 /* Response 1 */
59#define MCI_RSPR2 0x0028 /* Response 2 */
60#define MCI_RSPR3 0x002c /* Response 3 */
61#define MCI_RDR 0x0030 /* Receive Data */
62#define MCI_TDR 0x0034 /* Transmit Data */
63#define MCI_SR 0x0040 /* Status */
64#define MCI_IER 0x0044 /* Interrupt Enable */
65#define MCI_IDR 0x0048 /* Interrupt Disable */
66#define MCI_IMR 0x004c /* Interrupt Mask */
67# define MCI_CMDRDY ( 1 << 0) /* Command Ready */
68# define MCI_RXRDY ( 1 << 1) /* Receiver Ready */
69# define MCI_TXRDY ( 1 << 2) /* Transmitter Ready */
70# define MCI_BLKE ( 1 << 3) /* Data Block Ended */
71# define MCI_DTIP ( 1 << 4) /* Data Transfer In Progress */
72# define MCI_NOTBUSY ( 1 << 5) /* Data Not Busy */
73# define MCI_SDIOIRQA ( 1 << 8) /* SDIO IRQ in slot A */
74# define MCI_SDIOIRQB ( 1 << 9) /* SDIO IRQ in slot B */
75# define MCI_RINDE ( 1 << 16) /* Response Index Error */
76# define MCI_RDIRE ( 1 << 17) /* Response Direction Error */
77# define MCI_RCRCE ( 1 << 18) /* Response CRC Error */
78# define MCI_RENDE ( 1 << 19) /* Response End Bit Error */
79# define MCI_RTOE ( 1 << 20) /* Response Time-Out Error */
80# define MCI_DCRCE ( 1 << 21) /* Data CRC Error */
81# define MCI_DTOE ( 1 << 22) /* Data Time-Out Error */
82# define MCI_OVRE ( 1 << 30) /* RX Overrun Error */
83# define MCI_UNRE ( 1 << 31) /* TX Underrun Error */
84
85/* Register access macros */
86#define mci_readl(port,reg) \
87 __raw_readl((port)->regs + MCI_##reg)
88#define mci_writel(port,reg,value) \
89 __raw_writel((value), (port)->regs + MCI_##reg)
90
91#endif /* __DRIVERS_MMC_ATMEL_MCI_H__ */
diff --git a/drivers/mmc/host/atmel-mci.c b/drivers/mmc/host/atmel-mci.c
new file mode 100644
index 000000000000..cce873c5a149
--- /dev/null
+++ b/drivers/mmc/host/atmel-mci.c
@@ -0,0 +1,981 @@
1/*
2 * Atmel MultiMedia Card Interface driver
3 *
4 * Copyright (C) 2004-2008 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10#include <linux/blkdev.h>
11#include <linux/clk.h>
12#include <linux/device.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/ioport.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/scatterlist.h>
19
20#include <linux/mmc/host.h>
21
22#include <asm/atmel-mci.h>
23#include <asm/io.h>
24#include <asm/unaligned.h>
25
26#include <asm/arch/board.h>
27#include <asm/arch/gpio.h>
28
29#include "atmel-mci-regs.h"
30
31#define ATMCI_DATA_ERROR_FLAGS (MCI_DCRCE | MCI_DTOE | MCI_OVRE | MCI_UNRE)
32
33enum {
34 EVENT_CMD_COMPLETE = 0,
35 EVENT_DATA_ERROR,
36 EVENT_DATA_COMPLETE,
37 EVENT_STOP_SENT,
38 EVENT_STOP_COMPLETE,
39 EVENT_XFER_COMPLETE,
40};
41
42struct atmel_mci {
43 struct mmc_host *mmc;
44 void __iomem *regs;
45
46 struct scatterlist *sg;
47 unsigned int pio_offset;
48
49 struct mmc_request *mrq;
50 struct mmc_command *cmd;
51 struct mmc_data *data;
52
53 u32 cmd_status;
54 u32 data_status;
55 u32 stop_status;
56 u32 stop_cmdr;
57
58 u32 mode_reg;
59 u32 sdc_reg;
60
61 struct tasklet_struct tasklet;
62 unsigned long pending_events;
63 unsigned long completed_events;
64
65 int present;
66 int detect_pin;
67 int wp_pin;
68
69 /* For detect pin debouncing */
70 struct timer_list detect_timer;
71
72 unsigned long bus_hz;
73 unsigned long mapbase;
74 struct clk *mck;
75 struct platform_device *pdev;
76};
77
78#define atmci_is_completed(host, event) \
79 test_bit(event, &host->completed_events)
80#define atmci_test_and_clear_pending(host, event) \
81 test_and_clear_bit(event, &host->pending_events)
82#define atmci_test_and_set_completed(host, event) \
83 test_and_set_bit(event, &host->completed_events)
84#define atmci_set_completed(host, event) \
85 set_bit(event, &host->completed_events)
86#define atmci_set_pending(host, event) \
87 set_bit(event, &host->pending_events)
88#define atmci_clear_pending(host, event) \
89 clear_bit(event, &host->pending_events)
90
91
92static void atmci_enable(struct atmel_mci *host)
93{
94 clk_enable(host->mck);
95 mci_writel(host, CR, MCI_CR_MCIEN);
96 mci_writel(host, MR, host->mode_reg);
97 mci_writel(host, SDCR, host->sdc_reg);
98}
99
100static void atmci_disable(struct atmel_mci *host)
101{
102 mci_writel(host, CR, MCI_CR_SWRST);
103
104 /* Stall until write is complete, then disable the bus clock */
105 mci_readl(host, SR);
106 clk_disable(host->mck);
107}
108
109static inline unsigned int ns_to_clocks(struct atmel_mci *host,
110 unsigned int ns)
111{
112 return (ns * (host->bus_hz / 1000000) + 999) / 1000;
113}
114
115static void atmci_set_timeout(struct atmel_mci *host,
116 struct mmc_data *data)
117{
118 static unsigned dtomul_to_shift[] = {
119 0, 4, 7, 8, 10, 12, 16, 20
120 };
121 unsigned timeout;
122 unsigned dtocyc;
123 unsigned dtomul;
124
125 timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
126
127 for (dtomul = 0; dtomul < 8; dtomul++) {
128 unsigned shift = dtomul_to_shift[dtomul];
129 dtocyc = (timeout + (1 << shift) - 1) >> shift;
130 if (dtocyc < 15)
131 break;
132 }
133
134 if (dtomul >= 8) {
135 dtomul = 7;
136 dtocyc = 15;
137 }
138
139 dev_vdbg(&host->mmc->class_dev, "setting timeout to %u cycles\n",
140 dtocyc << dtomul_to_shift[dtomul]);
141 mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
142}
143
144/*
145 * Return mask with command flags to be enabled for this command.
146 */
147static u32 atmci_prepare_command(struct mmc_host *mmc,
148 struct mmc_command *cmd)
149{
150 struct mmc_data *data;
151 u32 cmdr;
152
153 cmd->error = -EINPROGRESS;
154
155 cmdr = MCI_CMDR_CMDNB(cmd->opcode);
156
157 if (cmd->flags & MMC_RSP_PRESENT) {
158 if (cmd->flags & MMC_RSP_136)
159 cmdr |= MCI_CMDR_RSPTYP_136BIT;
160 else
161 cmdr |= MCI_CMDR_RSPTYP_48BIT;
162 }
163
164 /*
165 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
166 * it's too difficult to determine whether this is an ACMD or
167 * not. Better make it 64.
168 */
169 cmdr |= MCI_CMDR_MAXLAT_64CYC;
170
171 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
172 cmdr |= MCI_CMDR_OPDCMD;
173
174 data = cmd->data;
175 if (data) {
176 cmdr |= MCI_CMDR_START_XFER;
177 if (data->flags & MMC_DATA_STREAM)
178 cmdr |= MCI_CMDR_STREAM;
179 else if (data->blocks > 1)
180 cmdr |= MCI_CMDR_MULTI_BLOCK;
181 else
182 cmdr |= MCI_CMDR_BLOCK;
183
184 if (data->flags & MMC_DATA_READ)
185 cmdr |= MCI_CMDR_TRDIR_READ;
186 }
187
188 return cmdr;
189}
190
191static void atmci_start_command(struct atmel_mci *host,
192 struct mmc_command *cmd,
193 u32 cmd_flags)
194{
195 /* Must read host->cmd after testing event flags */
196 smp_rmb();
197 WARN_ON(host->cmd);
198 host->cmd = cmd;
199
200 dev_vdbg(&host->mmc->class_dev,
201 "start command: ARGR=0x%08x CMDR=0x%08x\n",
202 cmd->arg, cmd_flags);
203
204 mci_writel(host, ARGR, cmd->arg);
205 mci_writel(host, CMDR, cmd_flags);
206}
207
208static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data)
209{
210 struct atmel_mci *host = mmc_priv(mmc);
211
212 atmci_start_command(host, data->stop, host->stop_cmdr);
213 mci_writel(host, IER, MCI_CMDRDY);
214}
215
216static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
217{
218 struct atmel_mci *host = mmc_priv(mmc);
219
220 WARN_ON(host->cmd || host->data);
221 host->mrq = NULL;
222
223 atmci_disable(host);
224
225 mmc_request_done(mmc, mrq);
226}
227
228/*
229 * Returns a mask of interrupt flags to be enabled after the whole
230 * request has been prepared.
231 */
232static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
233{
234 struct atmel_mci *host = mmc_priv(mmc);
235 u32 iflags;
236
237 data->error = -EINPROGRESS;
238
239 WARN_ON(host->data);
240 host->sg = NULL;
241 host->data = data;
242
243 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
244 | MCI_BLKLEN(data->blksz));
245 dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
246 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
247
248 iflags = ATMCI_DATA_ERROR_FLAGS;
249 host->sg = data->sg;
250 host->pio_offset = 0;
251 if (data->flags & MMC_DATA_READ)
252 iflags |= MCI_RXRDY;
253 else
254 iflags |= MCI_TXRDY;
255
256 return iflags;
257}
258
259static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
260{
261 struct atmel_mci *host = mmc_priv(mmc);
262 struct mmc_data *data;
263 struct mmc_command *cmd;
264 u32 iflags;
265 u32 cmdflags = 0;
266
267 iflags = mci_readl(host, IMR);
268 if (iflags)
269 dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
270 mci_readl(host, IMR));
271
272 WARN_ON(host->mrq != NULL);
273
274 /*
275 * We may "know" the card is gone even though there's still an
276 * electrical connection. If so, we really need to communicate
277 * this to the MMC core since there won't be any more
278 * interrupts as the card is completely removed. Otherwise,
279 * the MMC core might believe the card is still there even
280 * though the card was just removed very slowly.
281 */
282 if (!host->present) {
283 mrq->cmd->error = -ENOMEDIUM;
284 mmc_request_done(mmc, mrq);
285 return;
286 }
287
288 host->mrq = mrq;
289 host->pending_events = 0;
290 host->completed_events = 0;
291
292 atmci_enable(host);
293
294 /* We don't support multiple blocks of weird lengths. */
295 data = mrq->data;
296 if (data) {
297 if (data->blocks > 1 && data->blksz & 3)
298 goto fail;
299 atmci_set_timeout(host, data);
300 }
301
302 iflags = MCI_CMDRDY;
303 cmd = mrq->cmd;
304 cmdflags = atmci_prepare_command(mmc, cmd);
305 atmci_start_command(host, cmd, cmdflags);
306
307 if (data)
308 iflags |= atmci_submit_data(mmc, data);
309
310 if (mrq->stop) {
311 host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
312 host->stop_cmdr |= MCI_CMDR_STOP_XFER;
313 if (!(data->flags & MMC_DATA_WRITE))
314 host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
315 if (data->flags & MMC_DATA_STREAM)
316 host->stop_cmdr |= MCI_CMDR_STREAM;
317 else
318 host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
319 }
320
321 /*
322 * We could have enabled interrupts earlier, but I suspect
323 * that would open up a nice can of interesting race
324 * conditions (e.g. command and data complete, but stop not
325 * prepared yet.)
326 */
327 mci_writel(host, IER, iflags);
328
329 return;
330
331fail:
332 atmci_disable(host);
333 host->mrq = NULL;
334 mrq->cmd->error = -EINVAL;
335 mmc_request_done(mmc, mrq);
336}
337
338static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
339{
340 struct atmel_mci *host = mmc_priv(mmc);
341
342 if (ios->clock) {
343 u32 clkdiv;
344
345 /* Set clock rate */
346 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
347 if (clkdiv > 255) {
348 dev_warn(&mmc->class_dev,
349 "clock %u too slow; using %lu\n",
350 ios->clock, host->bus_hz / (2 * 256));
351 clkdiv = 255;
352 }
353
354 host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
355 | MCI_MR_RDPROOF;
356 }
357
358 switch (ios->bus_width) {
359 case MMC_BUS_WIDTH_1:
360 host->sdc_reg = 0;
361 break;
362 case MMC_BUS_WIDTH_4:
363 host->sdc_reg = MCI_SDCBUS_4BIT;
364 break;
365 }
366
367 switch (ios->power_mode) {
368 case MMC_POWER_ON:
369 /* Send init sequence (74 clock cycles) */
370 atmci_enable(host);
371 mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
372 while (!(mci_readl(host, SR) & MCI_CMDRDY))
373 cpu_relax();
374 atmci_disable(host);
375 break;
376 default:
377 /*
378 * TODO: None of the currently available AVR32-based
379 * boards allow MMC power to be turned off. Implement
380 * power control when this can be tested properly.
381 */
382 break;
383 }
384}
385
386static int atmci_get_ro(struct mmc_host *mmc)
387{
388 int read_only = 0;
389 struct atmel_mci *host = mmc_priv(mmc);
390
391 if (host->wp_pin >= 0) {
392 read_only = gpio_get_value(host->wp_pin);
393 dev_dbg(&mmc->class_dev, "card is %s\n",
394 read_only ? "read-only" : "read-write");
395 } else {
396 dev_dbg(&mmc->class_dev,
397 "no pin for checking read-only switch."
398 " Assuming write-enable.\n");
399 }
400
401 return read_only;
402}
403
404static struct mmc_host_ops atmci_ops = {
405 .request = atmci_request,
406 .set_ios = atmci_set_ios,
407 .get_ro = atmci_get_ro,
408};
409
410static void atmci_command_complete(struct atmel_mci *host,
411 struct mmc_command *cmd, u32 status)
412{
413 /* Read the response from the card (up to 16 bytes) */
414 cmd->resp[0] = mci_readl(host, RSPR);
415 cmd->resp[1] = mci_readl(host, RSPR);
416 cmd->resp[2] = mci_readl(host, RSPR);
417 cmd->resp[3] = mci_readl(host, RSPR);
418
419 if (status & MCI_RTOE)
420 cmd->error = -ETIMEDOUT;
421 else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
422 cmd->error = -EILSEQ;
423 else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
424 cmd->error = -EIO;
425 else
426 cmd->error = 0;
427
428 if (cmd->error) {
429 dev_dbg(&host->mmc->class_dev,
430 "command error: status=0x%08x\n", status);
431
432 if (cmd->data) {
433 host->data = NULL;
434 mci_writel(host, IDR, MCI_NOTBUSY
435 | MCI_TXRDY | MCI_RXRDY
436 | ATMCI_DATA_ERROR_FLAGS);
437 }
438 }
439}
440
441static void atmci_detect_change(unsigned long data)
442{
443 struct atmel_mci *host = (struct atmel_mci *)data;
444 struct mmc_request *mrq = host->mrq;
445 int present;
446
447 /*
448 * atmci_remove() sets detect_pin to -1 before freeing the
449 * interrupt. We must not re-enable the interrupt if it has
450 * been freed.
451 */
452 smp_rmb();
453 if (host->detect_pin < 0)
454 return;
455
456 enable_irq(gpio_to_irq(host->detect_pin));
457 present = !gpio_get_value(host->detect_pin);
458
459 dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
460 present, host->present);
461
462 if (present != host->present) {
463 dev_dbg(&host->mmc->class_dev, "card %s\n",
464 present ? "inserted" : "removed");
465 host->present = present;
466
467 /* Reset controller if card is gone */
468 if (!present) {
469 mci_writel(host, CR, MCI_CR_SWRST);
470 mci_writel(host, IDR, ~0UL);
471 mci_writel(host, CR, MCI_CR_MCIEN);
472 }
473
474 /* Clean up queue if present */
475 if (mrq) {
476 /*
477 * Reset controller to terminate any ongoing
478 * commands or data transfers.
479 */
480 mci_writel(host, CR, MCI_CR_SWRST);
481
482 if (!atmci_is_completed(host, EVENT_CMD_COMPLETE))
483 mrq->cmd->error = -ENOMEDIUM;
484
485 if (mrq->data && !atmci_is_completed(host,
486 EVENT_DATA_COMPLETE)) {
487 host->data = NULL;
488 mrq->data->error = -ENOMEDIUM;
489 }
490 if (mrq->stop && !atmci_is_completed(host,
491 EVENT_STOP_COMPLETE))
492 mrq->stop->error = -ENOMEDIUM;
493
494 host->cmd = NULL;
495 atmci_request_end(host->mmc, mrq);
496 }
497
498 mmc_detect_change(host->mmc, 0);
499 }
500}
501
502static void atmci_tasklet_func(unsigned long priv)
503{
504 struct mmc_host *mmc = (struct mmc_host *)priv;
505 struct atmel_mci *host = mmc_priv(mmc);
506 struct mmc_request *mrq = host->mrq;
507 struct mmc_data *data = host->data;
508
509 dev_vdbg(&mmc->class_dev,
510 "tasklet: pending/completed/mask %lx/%lx/%x\n",
511 host->pending_events, host->completed_events,
512 mci_readl(host, IMR));
513
514 if (atmci_test_and_clear_pending(host, EVENT_CMD_COMPLETE)) {
515 /*
516 * host->cmd must be set to NULL before the interrupt
517 * handler sees EVENT_CMD_COMPLETE
518 */
519 host->cmd = NULL;
520 smp_wmb();
521 atmci_set_completed(host, EVENT_CMD_COMPLETE);
522 atmci_command_complete(host, mrq->cmd, host->cmd_status);
523
524 if (!mrq->cmd->error && mrq->stop
525 && atmci_is_completed(host, EVENT_XFER_COMPLETE)
526 && !atmci_test_and_set_completed(host,
527 EVENT_STOP_SENT))
528 send_stop_cmd(host->mmc, mrq->data);
529 }
530 if (atmci_test_and_clear_pending(host, EVENT_STOP_COMPLETE)) {
531 /*
532 * host->cmd must be set to NULL before the interrupt
533 * handler sees EVENT_STOP_COMPLETE
534 */
535 host->cmd = NULL;
536 smp_wmb();
537 atmci_set_completed(host, EVENT_STOP_COMPLETE);
538 atmci_command_complete(host, mrq->stop, host->stop_status);
539 }
540 if (atmci_test_and_clear_pending(host, EVENT_DATA_ERROR)) {
541 u32 status = host->data_status;
542
543 dev_vdbg(&mmc->class_dev, "data error: status=%08x\n", status);
544
545 atmci_set_completed(host, EVENT_DATA_ERROR);
546 atmci_set_completed(host, EVENT_DATA_COMPLETE);
547
548 if (status & MCI_DTOE) {
549 dev_dbg(&mmc->class_dev,
550 "data timeout error\n");
551 data->error = -ETIMEDOUT;
552 } else if (status & MCI_DCRCE) {
553 dev_dbg(&mmc->class_dev, "data CRC error\n");
554 data->error = -EILSEQ;
555 } else {
556 dev_dbg(&mmc->class_dev,
557 "data FIFO error (status=%08x)\n",
558 status);
559 data->error = -EIO;
560 }
561
562 if (host->present && data->stop
563 && atmci_is_completed(host, EVENT_CMD_COMPLETE)
564 && !atmci_test_and_set_completed(
565 host, EVENT_STOP_SENT))
566 send_stop_cmd(host->mmc, data);
567
568 host->data = NULL;
569 }
570 if (atmci_test_and_clear_pending(host, EVENT_DATA_COMPLETE)) {
571 atmci_set_completed(host, EVENT_DATA_COMPLETE);
572
573 if (!atmci_is_completed(host, EVENT_DATA_ERROR)) {
574 data->bytes_xfered = data->blocks * data->blksz;
575 data->error = 0;
576 }
577
578 host->data = NULL;
579 }
580
581 if (host->mrq && !host->cmd && !host->data)
582 atmci_request_end(mmc, host->mrq);
583}
584
585static void atmci_read_data_pio(struct atmel_mci *host)
586{
587 struct scatterlist *sg = host->sg;
588 void *buf = sg_virt(sg);
589 unsigned int offset = host->pio_offset;
590 struct mmc_data *data = host->data;
591 u32 value;
592 u32 status;
593 unsigned int nbytes = 0;
594
595 do {
596 value = mci_readl(host, RDR);
597 if (likely(offset + 4 <= sg->length)) {
598 put_unaligned(value, (u32 *)(buf + offset));
599
600 offset += 4;
601 nbytes += 4;
602
603 if (offset == sg->length) {
604 host->sg = sg = sg_next(sg);
605 if (!sg)
606 goto done;
607
608 offset = 0;
609 buf = sg_virt(sg);
610 }
611 } else {
612 unsigned int remaining = sg->length - offset;
613 memcpy(buf + offset, &value, remaining);
614 nbytes += remaining;
615
616 flush_dcache_page(sg_page(sg));
617 host->sg = sg = sg_next(sg);
618 if (!sg)
619 goto done;
620
621 offset = 4 - remaining;
622 buf = sg_virt(sg);
623 memcpy(buf, (u8 *)&value + remaining, offset);
624 nbytes += offset;
625 }
626
627 status = mci_readl(host, SR);
628 if (status & ATMCI_DATA_ERROR_FLAGS) {
629 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
630 | ATMCI_DATA_ERROR_FLAGS));
631 host->data_status = status;
632 atmci_set_pending(host, EVENT_DATA_ERROR);
633 tasklet_schedule(&host->tasklet);
634 break;
635 }
636 } while (status & MCI_RXRDY);
637
638 host->pio_offset = offset;
639 data->bytes_xfered += nbytes;
640
641 return;
642
643done:
644 mci_writel(host, IDR, MCI_RXRDY);
645 mci_writel(host, IER, MCI_NOTBUSY);
646 data->bytes_xfered += nbytes;
647 atmci_set_completed(host, EVENT_XFER_COMPLETE);
648 if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
649 && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
650 send_stop_cmd(host->mmc, data);
651}
652
653static void atmci_write_data_pio(struct atmel_mci *host)
654{
655 struct scatterlist *sg = host->sg;
656 void *buf = sg_virt(sg);
657 unsigned int offset = host->pio_offset;
658 struct mmc_data *data = host->data;
659 u32 value;
660 u32 status;
661 unsigned int nbytes = 0;
662
663 do {
664 if (likely(offset + 4 <= sg->length)) {
665 value = get_unaligned((u32 *)(buf + offset));
666 mci_writel(host, TDR, value);
667
668 offset += 4;
669 nbytes += 4;
670 if (offset == sg->length) {
671 host->sg = sg = sg_next(sg);
672 if (!sg)
673 goto done;
674
675 offset = 0;
676 buf = sg_virt(sg);
677 }
678 } else {
679 unsigned int remaining = sg->length - offset;
680
681 value = 0;
682 memcpy(&value, buf + offset, remaining);
683 nbytes += remaining;
684
685 host->sg = sg = sg_next(sg);
686 if (!sg) {
687 mci_writel(host, TDR, value);
688 goto done;
689 }
690
691 offset = 4 - remaining;
692 buf = sg_virt(sg);
693 memcpy((u8 *)&value + remaining, buf, offset);
694 mci_writel(host, TDR, value);
695 nbytes += offset;
696 }
697
698 status = mci_readl(host, SR);
699 if (status & ATMCI_DATA_ERROR_FLAGS) {
700 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
701 | ATMCI_DATA_ERROR_FLAGS));
702 host->data_status = status;
703 atmci_set_pending(host, EVENT_DATA_ERROR);
704 tasklet_schedule(&host->tasklet);
705 break;
706 }
707 } while (status & MCI_TXRDY);
708
709 host->pio_offset = offset;
710 data->bytes_xfered += nbytes;
711
712 return;
713
714done:
715 mci_writel(host, IDR, MCI_TXRDY);
716 mci_writel(host, IER, MCI_NOTBUSY);
717 data->bytes_xfered += nbytes;
718 atmci_set_completed(host, EVENT_XFER_COMPLETE);
719 if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
720 && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
721 send_stop_cmd(host->mmc, data);
722}
723
724static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
725{
726 struct atmel_mci *host = mmc_priv(mmc);
727
728 mci_writel(host, IDR, MCI_CMDRDY);
729
730 if (atmci_is_completed(host, EVENT_STOP_SENT)) {
731 host->stop_status = status;
732 atmci_set_pending(host, EVENT_STOP_COMPLETE);
733 } else {
734 host->cmd_status = status;
735 atmci_set_pending(host, EVENT_CMD_COMPLETE);
736 }
737
738 tasklet_schedule(&host->tasklet);
739}
740
741static irqreturn_t atmci_interrupt(int irq, void *dev_id)
742{
743 struct mmc_host *mmc = dev_id;
744 struct atmel_mci *host = mmc_priv(mmc);
745 u32 status, mask, pending;
746 unsigned int pass_count = 0;
747
748 spin_lock(&mmc->lock);
749
750 do {
751 status = mci_readl(host, SR);
752 mask = mci_readl(host, IMR);
753 pending = status & mask;
754 if (!pending)
755 break;
756
757 if (pending & ATMCI_DATA_ERROR_FLAGS) {
758 mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
759 | MCI_RXRDY | MCI_TXRDY);
760 pending &= mci_readl(host, IMR);
761 host->data_status = status;
762 atmci_set_pending(host, EVENT_DATA_ERROR);
763 tasklet_schedule(&host->tasklet);
764 }
765 if (pending & MCI_NOTBUSY) {
766 mci_writel(host, IDR, (MCI_NOTBUSY
767 | ATMCI_DATA_ERROR_FLAGS));
768 atmci_set_pending(host, EVENT_DATA_COMPLETE);
769 tasklet_schedule(&host->tasklet);
770 }
771 if (pending & MCI_RXRDY)
772 atmci_read_data_pio(host);
773 if (pending & MCI_TXRDY)
774 atmci_write_data_pio(host);
775
776 if (pending & MCI_CMDRDY)
777 atmci_cmd_interrupt(mmc, status);
778 } while (pass_count++ < 5);
779
780 spin_unlock(&mmc->lock);
781
782 return pass_count ? IRQ_HANDLED : IRQ_NONE;
783}
784
785static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
786{
787 struct mmc_host *mmc = dev_id;
788 struct atmel_mci *host = mmc_priv(mmc);
789
790 /*
791 * Disable interrupts until the pin has stabilized and check
792 * the state then. Use mod_timer() since we may be in the
793 * middle of the timer routine when this interrupt triggers.
794 */
795 disable_irq_nosync(irq);
796 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
797
798 return IRQ_HANDLED;
799}
800
801static int __init atmci_probe(struct platform_device *pdev)
802{
803 struct mci_platform_data *pdata;
804 struct atmel_mci *host;
805 struct mmc_host *mmc;
806 struct resource *regs;
807 int irq;
808 int ret;
809
810 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
811 if (!regs)
812 return -ENXIO;
813 pdata = pdev->dev.platform_data;
814 if (!pdata)
815 return -ENXIO;
816 irq = platform_get_irq(pdev, 0);
817 if (irq < 0)
818 return irq;
819
820 mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
821 if (!mmc)
822 return -ENOMEM;
823
824 host = mmc_priv(mmc);
825 host->pdev = pdev;
826 host->mmc = mmc;
827 host->detect_pin = pdata->detect_pin;
828 host->wp_pin = pdata->wp_pin;
829
830 host->mck = clk_get(&pdev->dev, "mci_clk");
831 if (IS_ERR(host->mck)) {
832 ret = PTR_ERR(host->mck);
833 goto err_clk_get;
834 }
835
836 ret = -ENOMEM;
837 host->regs = ioremap(regs->start, regs->end - regs->start + 1);
838 if (!host->regs)
839 goto err_ioremap;
840
841 clk_enable(host->mck);
842 mci_writel(host, CR, MCI_CR_SWRST);
843 host->bus_hz = clk_get_rate(host->mck);
844 clk_disable(host->mck);
845
846 host->mapbase = regs->start;
847
848 mmc->ops = &atmci_ops;
849 mmc->f_min = (host->bus_hz + 511) / 512;
850 mmc->f_max = host->bus_hz / 2;
851 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
852 mmc->caps |= MMC_CAP_4_BIT_DATA;
853
854 mmc->max_hw_segs = 64;
855 mmc->max_phys_segs = 64;
856 mmc->max_req_size = 32768 * 512;
857 mmc->max_blk_size = 32768;
858 mmc->max_blk_count = 512;
859
860 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
861
862 ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc);
863 if (ret)
864 goto err_request_irq;
865
866 /* Assume card is present if we don't have a detect pin */
867 host->present = 1;
868 if (host->detect_pin >= 0) {
869 if (gpio_request(host->detect_pin, "mmc_detect")) {
870 dev_dbg(&mmc->class_dev, "no detect pin available\n");
871 host->detect_pin = -1;
872 } else {
873 host->present = !gpio_get_value(host->detect_pin);
874 }
875 }
876 if (host->wp_pin >= 0) {
877 if (gpio_request(host->wp_pin, "mmc_wp")) {
878 dev_dbg(&mmc->class_dev, "no WP pin available\n");
879 host->wp_pin = -1;
880 }
881 }
882
883 platform_set_drvdata(pdev, host);
884
885 mmc_add_host(mmc);
886
887 if (host->detect_pin >= 0) {
888 setup_timer(&host->detect_timer, atmci_detect_change,
889 (unsigned long)host);
890
891 ret = request_irq(gpio_to_irq(host->detect_pin),
892 atmci_detect_interrupt,
893 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
894 "mmc-detect", mmc);
895 if (ret) {
896 dev_dbg(&mmc->class_dev,
897 "could not request IRQ %d for detect pin\n",
898 gpio_to_irq(host->detect_pin));
899 gpio_free(host->detect_pin);
900 host->detect_pin = -1;
901 }
902 }
903
904 dev_info(&mmc->class_dev,
905 "Atmel MCI controller at 0x%08lx irq %d\n",
906 host->mapbase, irq);
907
908 return 0;
909
910err_request_irq:
911 iounmap(host->regs);
912err_ioremap:
913 clk_put(host->mck);
914err_clk_get:
915 mmc_free_host(mmc);
916 return ret;
917}
918
919static int __exit atmci_remove(struct platform_device *pdev)
920{
921 struct atmel_mci *host = platform_get_drvdata(pdev);
922
923 platform_set_drvdata(pdev, NULL);
924
925 if (host) {
926 if (host->detect_pin >= 0) {
927 int pin = host->detect_pin;
928
929 /* Make sure the timer doesn't enable the interrupt */
930 host->detect_pin = -1;
931 smp_wmb();
932
933 free_irq(gpio_to_irq(pin), host->mmc);
934 del_timer_sync(&host->detect_timer);
935 gpio_free(pin);
936 }
937
938 mmc_remove_host(host->mmc);
939
940 clk_enable(host->mck);
941 mci_writel(host, IDR, ~0UL);
942 mci_writel(host, CR, MCI_CR_MCIDIS);
943 mci_readl(host, SR);
944 clk_disable(host->mck);
945
946 if (host->wp_pin >= 0)
947 gpio_free(host->wp_pin);
948
949 free_irq(platform_get_irq(pdev, 0), host->mmc);
950 iounmap(host->regs);
951
952 clk_put(host->mck);
953
954 mmc_free_host(host->mmc);
955 }
956 return 0;
957}
958
959static struct platform_driver atmci_driver = {
960 .remove = __exit_p(atmci_remove),
961 .driver = {
962 .name = "atmel_mci",
963 },
964};
965
966static int __init atmci_init(void)
967{
968 return platform_driver_probe(&atmci_driver, atmci_probe);
969}
970
971static void __exit atmci_exit(void)
972{
973 platform_driver_unregister(&atmci_driver);
974}
975
976module_init(atmci_init);
977module_exit(atmci_exit);
978
979MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
980MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
981MODULE_LICENSE("GPL v2");
diff --git a/drivers/mmc/host/au1xmmc.c b/drivers/mmc/host/au1xmmc.c
index cc5f7bc546af..3f15eb204895 100644
--- a/drivers/mmc/host/au1xmmc.c
+++ b/drivers/mmc/host/au1xmmc.c
@@ -21,7 +21,7 @@
21 * published by the Free Software Foundation. 21 * published by the Free Software Foundation.
22 */ 22 */
23 23
24/* Why is a timer used to detect insert events? 24/* Why don't we use the SD controllers' carddetect feature?
25 * 25 *
26 * From the AU1100 MMC application guide: 26 * From the AU1100 MMC application guide:
27 * If the Au1100-based design is intended to support both MultiMediaCards 27 * If the Au1100-based design is intended to support both MultiMediaCards
@@ -30,8 +30,6 @@
30 * In doing so, a MMC card never enters SPI-mode communications, 30 * In doing so, a MMC card never enters SPI-mode communications,
31 * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective 31 * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
32 * (the low to high transition will not occur). 32 * (the low to high transition will not occur).
33 *
34 * So we use the timer to check the status manually.
35 */ 33 */
36 34
37#include <linux/module.h> 35#include <linux/module.h>
@@ -41,51 +39,110 @@
41#include <linux/interrupt.h> 39#include <linux/interrupt.h>
42#include <linux/dma-mapping.h> 40#include <linux/dma-mapping.h>
43#include <linux/scatterlist.h> 41#include <linux/scatterlist.h>
44 42#include <linux/leds.h>
45#include <linux/mmc/host.h> 43#include <linux/mmc/host.h>
44
46#include <asm/io.h> 45#include <asm/io.h>
47#include <asm/mach-au1x00/au1000.h> 46#include <asm/mach-au1x00/au1000.h>
48#include <asm/mach-au1x00/au1xxx_dbdma.h> 47#include <asm/mach-au1x00/au1xxx_dbdma.h>
49#include <asm/mach-au1x00/au1100_mmc.h> 48#include <asm/mach-au1x00/au1100_mmc.h>
50 49
51#include <au1xxx.h>
52#include "au1xmmc.h"
53
54#define DRIVER_NAME "au1xxx-mmc" 50#define DRIVER_NAME "au1xxx-mmc"
55 51
56/* Set this to enable special debugging macros */ 52/* Set this to enable special debugging macros */
53/* #define DEBUG */
57 54
58#ifdef DEBUG 55#ifdef DEBUG
59#define DBG(fmt, idx, args...) printk("au1xx(%d): DEBUG: " fmt, idx, ##args) 56#define DBG(fmt, idx, args...) \
57 printk(KERN_DEBUG "au1xmmc(%d): DEBUG: " fmt, idx, ##args)
60#else 58#else
61#define DBG(fmt, idx, args...) 59#define DBG(fmt, idx, args...) do {} while (0)
62#endif 60#endif
63 61
64const struct { 62/* Hardware definitions */
63#define AU1XMMC_DESCRIPTOR_COUNT 1
64#define AU1XMMC_DESCRIPTOR_SIZE 2048
65
66#define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
67 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
68 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
69
70/* This gives us a hard value for the stop command that we can write directly
71 * to the command register.
72 */
73#define STOP_CMD \
74 (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
75
76/* This is the set of interrupts that we configure by default. */
77#define AU1XMMC_INTERRUPTS \
78 (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
79 SD_CONFIG_CR | SD_CONFIG_I)
80
81/* The poll event (looking for insert/remove events runs twice a second. */
82#define AU1XMMC_DETECT_TIMEOUT (HZ/2)
83
84struct au1xmmc_host {
85 struct mmc_host *mmc;
86 struct mmc_request *mrq;
87
88 u32 flags;
65 u32 iobase; 89 u32 iobase;
66 u32 tx_devid, rx_devid; 90 u32 clock;
67 u16 bcsrpwr; 91 u32 bus_width;
68 u16 bcsrstatus; 92 u32 power_mode;
69 u16 wpstatus;
70} au1xmmc_card_table[] = {
71 { SD0_BASE, DSCR_CMD0_SDMS_TX0, DSCR_CMD0_SDMS_RX0,
72 BCSR_BOARD_SD0PWR, BCSR_INT_SD0INSERT, BCSR_STATUS_SD0WP },
73#ifndef CONFIG_MIPS_DB1200
74 { SD1_BASE, DSCR_CMD0_SDMS_TX1, DSCR_CMD0_SDMS_RX1,
75 BCSR_BOARD_DS1PWR, BCSR_INT_SD1INSERT, BCSR_STATUS_SD1WP }
76#endif
77};
78 93
79#define AU1XMMC_CONTROLLER_COUNT (ARRAY_SIZE(au1xmmc_card_table)) 94 int status;
80 95
81/* This array stores pointers for the hosts (used by the IRQ handler) */ 96 struct {
82struct au1xmmc_host *au1xmmc_hosts[AU1XMMC_CONTROLLER_COUNT]; 97 int len;
83static int dma = 1; 98 int dir;
99 } dma;
84 100
85#ifdef MODULE 101 struct {
86module_param(dma, bool, 0); 102 int index;
87MODULE_PARM_DESC(dma, "Use DMA engine for data transfers (0 = disabled)"); 103 int offset;
88#endif 104 int len;
105 } pio;
106
107 u32 tx_chan;
108 u32 rx_chan;
109
110 int irq;
111
112 struct tasklet_struct finish_task;
113 struct tasklet_struct data_task;
114 struct au1xmmc_platform_data *platdata;
115 struct platform_device *pdev;
116 struct resource *ioarea;
117};
118
119/* Status flags used by the host structure */
120#define HOST_F_XMIT 0x0001
121#define HOST_F_RECV 0x0002
122#define HOST_F_DMA 0x0010
123#define HOST_F_ACTIVE 0x0100
124#define HOST_F_STOP 0x1000
125
126#define HOST_S_IDLE 0x0001
127#define HOST_S_CMD 0x0002
128#define HOST_S_DATA 0x0003
129#define HOST_S_STOP 0x0004
130
131/* Easy access macros */
132#define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
133#define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
134#define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
135#define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
136#define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
137#define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
138#define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
139#define HOST_CMD(h) ((h)->iobase + SD_CMD)
140#define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
141#define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
142#define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
143
144#define DMA_CHANNEL(h) \
145 (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
89 146
90static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask) 147static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
91{ 148{
@@ -119,14 +176,13 @@ static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
119 176
120static inline void SEND_STOP(struct au1xmmc_host *host) 177static inline void SEND_STOP(struct au1xmmc_host *host)
121{ 178{
122 179 u32 config2;
123 /* We know the value of CONFIG2, so avoid a read we don't need */
124 u32 mask = SD_CONFIG2_EN;
125 180
126 WARN_ON(host->status != HOST_S_DATA); 181 WARN_ON(host->status != HOST_S_DATA);
127 host->status = HOST_S_STOP; 182 host->status = HOST_S_STOP;
128 183
129 au_writel(mask | SD_CONFIG2_DF, HOST_CONFIG2(host)); 184 config2 = au_readl(HOST_CONFIG2(host));
185 au_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
130 au_sync(); 186 au_sync();
131 187
132 /* Send the stop commmand */ 188 /* Send the stop commmand */
@@ -135,35 +191,36 @@ static inline void SEND_STOP(struct au1xmmc_host *host)
135 191
136static void au1xmmc_set_power(struct au1xmmc_host *host, int state) 192static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
137{ 193{
138 194 if (host->platdata && host->platdata->set_power)
139 u32 val = au1xmmc_card_table[host->id].bcsrpwr; 195 host->platdata->set_power(host->mmc, state);
140
141 bcsr->board &= ~val;
142 if (state) bcsr->board |= val;
143
144 au_sync_delay(1);
145} 196}
146 197
147static inline int au1xmmc_card_inserted(struct au1xmmc_host *host) 198static int au1xmmc_card_inserted(struct mmc_host *mmc)
148{ 199{
149 return (bcsr->sig_status & au1xmmc_card_table[host->id].bcsrstatus) 200 struct au1xmmc_host *host = mmc_priv(mmc);
150 ? 1 : 0; 201
202 if (host->platdata && host->platdata->card_inserted)
203 return !!host->platdata->card_inserted(host->mmc);
204
205 return -ENOSYS;
151} 206}
152 207
153static int au1xmmc_card_readonly(struct mmc_host *mmc) 208static int au1xmmc_card_readonly(struct mmc_host *mmc)
154{ 209{
155 struct au1xmmc_host *host = mmc_priv(mmc); 210 struct au1xmmc_host *host = mmc_priv(mmc);
156 return (bcsr->status & au1xmmc_card_table[host->id].wpstatus) 211
157 ? 1 : 0; 212 if (host->platdata && host->platdata->card_readonly)
213 return !!host->platdata->card_readonly(mmc);
214
215 return -ENOSYS;
158} 216}
159 217
160static void au1xmmc_finish_request(struct au1xmmc_host *host) 218static void au1xmmc_finish_request(struct au1xmmc_host *host)
161{ 219{
162
163 struct mmc_request *mrq = host->mrq; 220 struct mmc_request *mrq = host->mrq;
164 221
165 host->mrq = NULL; 222 host->mrq = NULL;
166 host->flags &= HOST_F_ACTIVE; 223 host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
167 224
168 host->dma.len = 0; 225 host->dma.len = 0;
169 host->dma.dir = 0; 226 host->dma.dir = 0;
@@ -174,8 +231,6 @@ static void au1xmmc_finish_request(struct au1xmmc_host *host)
174 231
175 host->status = HOST_S_IDLE; 232 host->status = HOST_S_IDLE;
176 233
177 bcsr->disk_leds |= (1 << 8);
178
179 mmc_request_done(host->mmc, mrq); 234 mmc_request_done(host->mmc, mrq);
180} 235}
181 236
@@ -235,18 +290,14 @@ static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
235 au_sync(); 290 au_sync();
236 291
237 /* Wait for the command to go on the line */ 292 /* Wait for the command to go on the line */
238 293 while (au_readl(HOST_CMD(host)) & SD_CMD_GO)
239 while(1) { 294 /* nop */;
240 if (!(au_readl(HOST_CMD(host)) & SD_CMD_GO))
241 break;
242 }
243 295
244 /* Wait for the command to come back */ 296 /* Wait for the command to come back */
245
246 if (wait) { 297 if (wait) {
247 u32 status = au_readl(HOST_STATUS(host)); 298 u32 status = au_readl(HOST_STATUS(host));
248 299
249 while(!(status & SD_STATUS_CR)) 300 while (!(status & SD_STATUS_CR))
250 status = au_readl(HOST_STATUS(host)); 301 status = au_readl(HOST_STATUS(host));
251 302
252 /* Clear the CR status */ 303 /* Clear the CR status */
@@ -260,12 +311,11 @@ static int au1xmmc_send_command(struct au1xmmc_host *host, int wait,
260 311
261static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status) 312static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
262{ 313{
263
264 struct mmc_request *mrq = host->mrq; 314 struct mmc_request *mrq = host->mrq;
265 struct mmc_data *data; 315 struct mmc_data *data;
266 u32 crc; 316 u32 crc;
267 317
268 WARN_ON(host->status != HOST_S_DATA && host->status != HOST_S_STOP); 318 WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
269 319
270 if (host->mrq == NULL) 320 if (host->mrq == NULL)
271 return; 321 return;
@@ -276,15 +326,13 @@ static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
276 status = au_readl(HOST_STATUS(host)); 326 status = au_readl(HOST_STATUS(host));
277 327
278 /* The transaction is really over when the SD_STATUS_DB bit is clear */ 328 /* The transaction is really over when the SD_STATUS_DB bit is clear */
279 329 while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
280 while((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
281 status = au_readl(HOST_STATUS(host)); 330 status = au_readl(HOST_STATUS(host));
282 331
283 data->error = 0; 332 data->error = 0;
284 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir); 333 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
285 334
286 /* Process any errors */ 335 /* Process any errors */
287
288 crc = (status & (SD_STATUS_WC | SD_STATUS_RC)); 336 crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
289 if (host->flags & HOST_F_XMIT) 337 if (host->flags & HOST_F_XMIT)
290 crc |= ((status & 0x07) == 0x02) ? 0 : 1; 338 crc |= ((status & 0x07) == 0x02) ? 0 : 1;
@@ -299,16 +347,16 @@ static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
299 347
300 if (!data->error) { 348 if (!data->error) {
301 if (host->flags & HOST_F_DMA) { 349 if (host->flags & HOST_F_DMA) {
350#ifdef CONFIG_SOC_AU1200 /* DBDMA */
302 u32 chan = DMA_CHANNEL(host); 351 u32 chan = DMA_CHANNEL(host);
303 352
304 chan_tab_t *c = *((chan_tab_t **) chan); 353 chan_tab_t *c = *((chan_tab_t **)chan);
305 au1x_dma_chan_t *cp = c->chan_ptr; 354 au1x_dma_chan_t *cp = c->chan_ptr;
306 data->bytes_xfered = cp->ddma_bytecnt; 355 data->bytes_xfered = cp->ddma_bytecnt;
307 } 356#endif
308 else 357 } else
309 data->bytes_xfered = 358 data->bytes_xfered =
310 (data->blocks * data->blksz) - 359 (data->blocks * data->blksz) - host->pio.len;
311 host->pio.len;
312 } 360 }
313 361
314 au1xmmc_finish_request(host); 362 au1xmmc_finish_request(host);
@@ -316,7 +364,7 @@ static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
316 364
317static void au1xmmc_tasklet_data(unsigned long param) 365static void au1xmmc_tasklet_data(unsigned long param)
318{ 366{
319 struct au1xmmc_host *host = (struct au1xmmc_host *) param; 367 struct au1xmmc_host *host = (struct au1xmmc_host *)param;
320 368
321 u32 status = au_readl(HOST_STATUS(host)); 369 u32 status = au_readl(HOST_STATUS(host));
322 au1xmmc_data_complete(host, status); 370 au1xmmc_data_complete(host, status);
@@ -326,11 +374,10 @@ static void au1xmmc_tasklet_data(unsigned long param)
326 374
327static void au1xmmc_send_pio(struct au1xmmc_host *host) 375static void au1xmmc_send_pio(struct au1xmmc_host *host)
328{ 376{
329 377 struct mmc_data *data;
330 struct mmc_data *data = 0; 378 int sg_len, max, count;
331 int sg_len, max, count = 0; 379 unsigned char *sg_ptr, val;
332 unsigned char *sg_ptr; 380 u32 status;
333 u32 status = 0;
334 struct scatterlist *sg; 381 struct scatterlist *sg;
335 382
336 data = host->mrq->data; 383 data = host->mrq->data;
@@ -345,14 +392,12 @@ static void au1xmmc_send_pio(struct au1xmmc_host *host)
345 /* This is the space left inside the buffer */ 392 /* This is the space left inside the buffer */
346 sg_len = data->sg[host->pio.index].length - host->pio.offset; 393 sg_len = data->sg[host->pio.index].length - host->pio.offset;
347 394
348 /* Check to if we need less then the size of the sg_buffer */ 395 /* Check if we need less than the size of the sg_buffer */
349
350 max = (sg_len > host->pio.len) ? host->pio.len : sg_len; 396 max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
351 if (max > AU1XMMC_MAX_TRANSFER) max = AU1XMMC_MAX_TRANSFER; 397 if (max > AU1XMMC_MAX_TRANSFER)
352 398 max = AU1XMMC_MAX_TRANSFER;
353 for(count = 0; count < max; count++ ) {
354 unsigned char val;
355 399
400 for (count = 0; count < max; count++) {
356 status = au_readl(HOST_STATUS(host)); 401 status = au_readl(HOST_STATUS(host));
357 402
358 if (!(status & SD_STATUS_TH)) 403 if (!(status & SD_STATUS_TH))
@@ -360,7 +405,7 @@ static void au1xmmc_send_pio(struct au1xmmc_host *host)
360 405
361 val = *sg_ptr++; 406 val = *sg_ptr++;
362 407
363 au_writel((unsigned long) val, HOST_TXPORT(host)); 408 au_writel((unsigned long)val, HOST_TXPORT(host));
364 au_sync(); 409 au_sync();
365 } 410 }
366 411
@@ -384,11 +429,10 @@ static void au1xmmc_send_pio(struct au1xmmc_host *host)
384 429
385static void au1xmmc_receive_pio(struct au1xmmc_host *host) 430static void au1xmmc_receive_pio(struct au1xmmc_host *host)
386{ 431{
387 432 struct mmc_data *data;
388 struct mmc_data *data = 0; 433 int max, count, sg_len = 0;
389 int sg_len = 0, max = 0, count = 0; 434 unsigned char *sg_ptr = NULL;
390 unsigned char *sg_ptr = 0; 435 u32 status, val;
391 u32 status = 0;
392 struct scatterlist *sg; 436 struct scatterlist *sg;
393 437
394 data = host->mrq->data; 438 data = host->mrq->data;
@@ -405,33 +449,33 @@ static void au1xmmc_receive_pio(struct au1xmmc_host *host)
405 /* This is the space left inside the buffer */ 449 /* This is the space left inside the buffer */
406 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset; 450 sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
407 451
408 /* Check to if we need less then the size of the sg_buffer */ 452 /* Check if we need less than the size of the sg_buffer */
409 if (sg_len < max) max = sg_len; 453 if (sg_len < max)
454 max = sg_len;
410 } 455 }
411 456
412 if (max > AU1XMMC_MAX_TRANSFER) 457 if (max > AU1XMMC_MAX_TRANSFER)
413 max = AU1XMMC_MAX_TRANSFER; 458 max = AU1XMMC_MAX_TRANSFER;
414 459
415 for(count = 0; count < max; count++ ) { 460 for (count = 0; count < max; count++) {
416 u32 val;
417 status = au_readl(HOST_STATUS(host)); 461 status = au_readl(HOST_STATUS(host));
418 462
419 if (!(status & SD_STATUS_NE)) 463 if (!(status & SD_STATUS_NE))
420 break; 464 break;
421 465
422 if (status & SD_STATUS_RC) { 466 if (status & SD_STATUS_RC) {
423 DBG("RX CRC Error [%d + %d].\n", host->id, 467 DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
424 host->pio.len, count); 468 host->pio.len, count);
425 break; 469 break;
426 } 470 }
427 471
428 if (status & SD_STATUS_RO) { 472 if (status & SD_STATUS_RO) {
429 DBG("RX Overrun [%d + %d]\n", host->id, 473 DBG("RX Overrun [%d + %d]\n", host->pdev->id,
430 host->pio.len, count); 474 host->pio.len, count);
431 break; 475 break;
432 } 476 }
433 else if (status & SD_STATUS_RU) { 477 else if (status & SD_STATUS_RU) {
434 DBG("RX Underrun [%d + %d]\n", host->id, 478 DBG("RX Underrun [%d + %d]\n", host->pdev->id,
435 host->pio.len, count); 479 host->pio.len, count);
436 break; 480 break;
437 } 481 }
@@ -439,7 +483,7 @@ static void au1xmmc_receive_pio(struct au1xmmc_host *host)
439 val = au_readl(HOST_RXPORT(host)); 483 val = au_readl(HOST_RXPORT(host));
440 484
441 if (sg_ptr) 485 if (sg_ptr)
442 *sg_ptr++ = (unsigned char) (val & 0xFF); 486 *sg_ptr++ = (unsigned char)(val & 0xFF);
443 } 487 }
444 488
445 host->pio.len -= count; 489 host->pio.len -= count;
@@ -451,7 +495,7 @@ static void au1xmmc_receive_pio(struct au1xmmc_host *host)
451 } 495 }
452 496
453 if (host->pio.len == 0) { 497 if (host->pio.len == 0) {
454 //IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); 498 /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
455 IRQ_OFF(host, SD_CONFIG_NE); 499 IRQ_OFF(host, SD_CONFIG_NE);
456 500
457 if (host->flags & HOST_F_STOP) 501 if (host->flags & HOST_F_STOP)
@@ -461,17 +505,15 @@ static void au1xmmc_receive_pio(struct au1xmmc_host *host)
461 } 505 }
462} 506}
463 507
464/* static void au1xmmc_cmd_complete 508/* This is called when a command has been completed - grab the response
465 This is called when a command has been completed - grab the response 509 * and check for errors. Then start the data transfer if it is indicated.
466 and check for errors. Then start the data transfer if it is indicated. 510 */
467*/
468
469static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status) 511static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
470{ 512{
471
472 struct mmc_request *mrq = host->mrq; 513 struct mmc_request *mrq = host->mrq;
473 struct mmc_command *cmd; 514 struct mmc_command *cmd;
474 int trans; 515 u32 r[4];
516 int i, trans;
475 517
476 if (!host->mrq) 518 if (!host->mrq)
477 return; 519 return;
@@ -481,9 +523,6 @@ static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
481 523
482 if (cmd->flags & MMC_RSP_PRESENT) { 524 if (cmd->flags & MMC_RSP_PRESENT) {
483 if (cmd->flags & MMC_RSP_136) { 525 if (cmd->flags & MMC_RSP_136) {
484 u32 r[4];
485 int i;
486
487 r[0] = au_readl(host->iobase + SD_RESP3); 526 r[0] = au_readl(host->iobase + SD_RESP3);
488 r[1] = au_readl(host->iobase + SD_RESP2); 527 r[1] = au_readl(host->iobase + SD_RESP2);
489 r[2] = au_readl(host->iobase + SD_RESP1); 528 r[2] = au_readl(host->iobase + SD_RESP1);
@@ -491,10 +530,9 @@ static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
491 530
492 /* The CRC is omitted from the response, so really 531 /* The CRC is omitted from the response, so really
493 * we only got 120 bytes, but the engine expects 532 * we only got 120 bytes, but the engine expects
494 * 128 bits, so we have to shift things up 533 * 128 bits, so we have to shift things up.
495 */ 534 */
496 535 for (i = 0; i < 4; i++) {
497 for(i = 0; i < 4; i++) {
498 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8; 536 cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
499 if (i != 3) 537 if (i != 3)
500 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24; 538 cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
@@ -505,22 +543,20 @@ static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
505 * our response omits the CRC, our data ends up 543 * our response omits the CRC, our data ends up
506 * being shifted 8 bits to the right. In this case, 544 * being shifted 8 bits to the right. In this case,
507 * that means that the OSR data starts at bit 31, 545 * that means that the OSR data starts at bit 31,
508 * so we can just read RESP0 and return that 546 * so we can just read RESP0 and return that.
509 */ 547 */
510 cmd->resp[0] = au_readl(host->iobase + SD_RESP0); 548 cmd->resp[0] = au_readl(host->iobase + SD_RESP0);
511 } 549 }
512 } 550 }
513 551
514 /* Figure out errors */ 552 /* Figure out errors */
515
516 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC)) 553 if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
517 cmd->error = -EILSEQ; 554 cmd->error = -EILSEQ;
518 555
519 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV); 556 trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
520 557
521 if (!trans || cmd->error) { 558 if (!trans || cmd->error) {
522 559 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
523 IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA|SD_CONFIG_RF);
524 tasklet_schedule(&host->finish_task); 560 tasklet_schedule(&host->finish_task);
525 return; 561 return;
526 } 562 }
@@ -528,6 +564,7 @@ static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
528 host->status = HOST_S_DATA; 564 host->status = HOST_S_DATA;
529 565
530 if (host->flags & HOST_F_DMA) { 566 if (host->flags & HOST_F_DMA) {
567#ifdef CONFIG_SOC_AU1200 /* DBDMA */
531 u32 channel = DMA_CHANNEL(host); 568 u32 channel = DMA_CHANNEL(host);
532 569
533 /* Start the DMA as soon as the buffer gets something in it */ 570 /* Start the DMA as soon as the buffer gets something in it */
@@ -540,23 +577,21 @@ static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
540 } 577 }
541 578
542 au1xxx_dbdma_start(channel); 579 au1xxx_dbdma_start(channel);
580#endif
543 } 581 }
544} 582}
545 583
546static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate) 584static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
547{ 585{
548
549 unsigned int pbus = get_au1x00_speed(); 586 unsigned int pbus = get_au1x00_speed();
550 unsigned int divisor; 587 unsigned int divisor;
551 u32 config; 588 u32 config;
552 589
553 /* From databook: 590 /* From databook:
554 divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1 591 * divisor = ((((cpuclock / sbus_divisor) / 2) / mmcclock) / 2) - 1
555 */ 592 */
556
557 pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2); 593 pbus /= ((au_readl(SYS_POWERCTRL) & 0x3) + 2);
558 pbus /= 2; 594 pbus /= 2;
559
560 divisor = ((pbus / rate) / 2) - 1; 595 divisor = ((pbus / rate) / 2) - 1;
561 596
562 config = au_readl(HOST_CONFIG(host)); 597 config = au_readl(HOST_CONFIG(host));
@@ -568,15 +603,11 @@ static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
568 au_sync(); 603 au_sync();
569} 604}
570 605
571static int 606static int au1xmmc_prepare_data(struct au1xmmc_host *host,
572au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data) 607 struct mmc_data *data)
573{ 608{
574
575 int datalen = data->blocks * data->blksz; 609 int datalen = data->blocks * data->blksz;
576 610
577 if (dma != 0)
578 host->flags |= HOST_F_DMA;
579
580 if (data->flags & MMC_DATA_READ) 611 if (data->flags & MMC_DATA_READ)
581 host->flags |= HOST_F_RECV; 612 host->flags |= HOST_F_RECV;
582 else 613 else
@@ -596,12 +627,13 @@ au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
596 au_writel(data->blksz - 1, HOST_BLKSIZE(host)); 627 au_writel(data->blksz - 1, HOST_BLKSIZE(host));
597 628
598 if (host->flags & HOST_F_DMA) { 629 if (host->flags & HOST_F_DMA) {
630#ifdef CONFIG_SOC_AU1200 /* DBDMA */
599 int i; 631 int i;
600 u32 channel = DMA_CHANNEL(host); 632 u32 channel = DMA_CHANNEL(host);
601 633
602 au1xxx_dbdma_stop(channel); 634 au1xxx_dbdma_stop(channel);
603 635
604 for(i = 0; i < host->dma.len; i++) { 636 for (i = 0; i < host->dma.len; i++) {
605 u32 ret = 0, flags = DDMA_FLAGS_NOIE; 637 u32 ret = 0, flags = DDMA_FLAGS_NOIE;
606 struct scatterlist *sg = &data->sg[i]; 638 struct scatterlist *sg = &data->sg[i];
607 int sg_len = sg->length; 639 int sg_len = sg->length;
@@ -611,23 +643,21 @@ au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
611 if (i == host->dma.len - 1) 643 if (i == host->dma.len - 1)
612 flags = DDMA_FLAGS_IE; 644 flags = DDMA_FLAGS_IE;
613 645
614 if (host->flags & HOST_F_XMIT){ 646 if (host->flags & HOST_F_XMIT) {
615 ret = au1xxx_dbdma_put_source_flags(channel, 647 ret = au1xxx_dbdma_put_source_flags(channel,
616 (void *) sg_virt(sg), len, flags); 648 (void *)sg_virt(sg), len, flags);
617 } 649 } else {
618 else { 650 ret = au1xxx_dbdma_put_dest_flags(channel,
619 ret = au1xxx_dbdma_put_dest_flags(channel, 651 (void *)sg_virt(sg), len, flags);
620 (void *) sg_virt(sg),
621 len, flags);
622 } 652 }
623 653
624 if (!ret) 654 if (!ret)
625 goto dataerr; 655 goto dataerr;
626 656
627 datalen -= len; 657 datalen -= len;
628 } 658 }
629 } 659#endif
630 else { 660 } else {
631 host->pio.index = 0; 661 host->pio.index = 0;
632 host->pio.offset = 0; 662 host->pio.offset = 0;
633 host->pio.len = datalen; 663 host->pio.len = datalen;
@@ -636,25 +666,21 @@ au1xmmc_prepare_data(struct au1xmmc_host *host, struct mmc_data *data)
636 IRQ_ON(host, SD_CONFIG_TH); 666 IRQ_ON(host, SD_CONFIG_TH);
637 else 667 else
638 IRQ_ON(host, SD_CONFIG_NE); 668 IRQ_ON(host, SD_CONFIG_NE);
639 //IRQ_ON(host, SD_CONFIG_RA|SD_CONFIG_RF); 669 /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
640 } 670 }
641 671
642 return 0; 672 return 0;
643 673
644 dataerr: 674dataerr:
645 dma_unmap_sg(mmc_dev(host->mmc),data->sg,data->sg_len,host->dma.dir); 675 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
676 host->dma.dir);
646 return -ETIMEDOUT; 677 return -ETIMEDOUT;
647} 678}
648 679
649/* static void au1xmmc_request 680/* This actually starts a command or data transaction */
650 This actually starts a command or data transaction
651*/
652
653static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq) 681static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
654{ 682{
655
656 struct au1xmmc_host *host = mmc_priv(mmc); 683 struct au1xmmc_host *host = mmc_priv(mmc);
657 unsigned int flags = 0;
658 int ret = 0; 684 int ret = 0;
659 685
660 WARN_ON(irqs_disabled()); 686 WARN_ON(irqs_disabled());
@@ -663,11 +689,15 @@ static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
663 host->mrq = mrq; 689 host->mrq = mrq;
664 host->status = HOST_S_CMD; 690 host->status = HOST_S_CMD;
665 691
666 bcsr->disk_leds &= ~(1 << 8); 692 /* fail request immediately if no card is present */
693 if (0 == au1xmmc_card_inserted(mmc)) {
694 mrq->cmd->error = -ENOMEDIUM;
695 au1xmmc_finish_request(host);
696 return;
697 }
667 698
668 if (mrq->data) { 699 if (mrq->data) {
669 FLUSH_FIFO(host); 700 FLUSH_FIFO(host);
670 flags = mrq->data->flags;
671 ret = au1xmmc_prepare_data(host, mrq->data); 701 ret = au1xmmc_prepare_data(host, mrq->data);
672 } 702 }
673 703
@@ -682,7 +712,6 @@ static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
682 712
683static void au1xmmc_reset_controller(struct au1xmmc_host *host) 713static void au1xmmc_reset_controller(struct au1xmmc_host *host)
684{ 714{
685
686 /* Apply the clock */ 715 /* Apply the clock */
687 au_writel(SD_ENABLE_CE, HOST_ENABLE(host)); 716 au_writel(SD_ENABLE_CE, HOST_ENABLE(host));
688 au_sync_delay(1); 717 au_sync_delay(1);
@@ -712,9 +741,10 @@ static void au1xmmc_reset_controller(struct au1xmmc_host *host)
712} 741}
713 742
714 743
715static void au1xmmc_set_ios(struct mmc_host* mmc, struct mmc_ios* ios) 744static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
716{ 745{
717 struct au1xmmc_host *host = mmc_priv(mmc); 746 struct au1xmmc_host *host = mmc_priv(mmc);
747 u32 config2;
718 748
719 if (ios->power_mode == MMC_POWER_OFF) 749 if (ios->power_mode == MMC_POWER_OFF)
720 au1xmmc_set_power(host, 0); 750 au1xmmc_set_power(host, 0);
@@ -726,21 +756,18 @@ static void au1xmmc_set_ios(struct mmc_host* mmc, struct mmc_ios* ios)
726 au1xmmc_set_clock(host, ios->clock); 756 au1xmmc_set_clock(host, ios->clock);
727 host->clock = ios->clock; 757 host->clock = ios->clock;
728 } 758 }
729}
730
731static void au1xmmc_dma_callback(int irq, void *dev_id)
732{
733 struct au1xmmc_host *host = (struct au1xmmc_host *) dev_id;
734
735 /* Avoid spurious interrupts */
736 759
737 if (!host->mrq) 760 config2 = au_readl(HOST_CONFIG2(host));
738 return; 761 switch (ios->bus_width) {
739 762 case MMC_BUS_WIDTH_4:
740 if (host->flags & HOST_F_STOP) 763 config2 |= SD_CONFIG2_WB;
741 SEND_STOP(host); 764 break;
742 765 case MMC_BUS_WIDTH_1:
743 tasklet_schedule(&host->data_task); 766 config2 &= ~SD_CONFIG2_WB;
767 break;
768 }
769 au_writel(config2, HOST_CONFIG2(host));
770 au_sync();
744} 771}
745 772
746#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT) 773#define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
@@ -749,245 +776,354 @@ static void au1xmmc_dma_callback(int irq, void *dev_id)
749 776
750static irqreturn_t au1xmmc_irq(int irq, void *dev_id) 777static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
751{ 778{
752 779 struct au1xmmc_host *host = dev_id;
753 u32 status; 780 u32 status;
754 int i, ret = 0;
755
756 disable_irq(AU1100_SD_IRQ);
757 781
758 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) { 782 status = au_readl(HOST_STATUS(host));
759 struct au1xmmc_host * host = au1xmmc_hosts[i];
760 u32 handled = 1;
761 783
762 status = au_readl(HOST_STATUS(host)); 784 if (!(status & SD_STATUS_I))
785 return IRQ_NONE; /* not ours */
763 786
764 if (host->mrq && (status & STATUS_TIMEOUT)) { 787 if (status & SD_STATUS_SI) /* SDIO */
765 if (status & SD_STATUS_RAT) 788 mmc_signal_sdio_irq(host->mmc);
766 host->mrq->cmd->error = -ETIMEDOUT;
767 789
768 else if (status & SD_STATUS_DT) 790 if (host->mrq && (status & STATUS_TIMEOUT)) {
769 host->mrq->data->error = -ETIMEDOUT; 791 if (status & SD_STATUS_RAT)
792 host->mrq->cmd->error = -ETIMEDOUT;
793 else if (status & SD_STATUS_DT)
794 host->mrq->data->error = -ETIMEDOUT;
770 795
771 /* In PIO mode, interrupts might still be enabled */ 796 /* In PIO mode, interrupts might still be enabled */
772 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH); 797 IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
773 798
774 //IRQ_OFF(host, SD_CONFIG_TH|SD_CONFIG_RA|SD_CONFIG_RF); 799 /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
775 tasklet_schedule(&host->finish_task); 800 tasklet_schedule(&host->finish_task);
776 } 801 }
777#if 0 802#if 0
778 else if (status & SD_STATUS_DD) { 803 else if (status & SD_STATUS_DD) {
779 804 /* Sometimes we get a DD before a NE in PIO mode */
780 /* Sometimes we get a DD before a NE in PIO mode */ 805 if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
781 806 au1xmmc_receive_pio(host);
782 if (!(host->flags & HOST_F_DMA) && 807 else {
783 (status & SD_STATUS_NE)) 808 au1xmmc_data_complete(host, status);
784 au1xmmc_receive_pio(host); 809 /* tasklet_schedule(&host->data_task); */
785 else {
786 au1xmmc_data_complete(host, status);
787 //tasklet_schedule(&host->data_task);
788 }
789 } 810 }
811 }
790#endif 812#endif
791 else if (status & (SD_STATUS_CR)) { 813 else if (status & SD_STATUS_CR) {
792 if (host->status == HOST_S_CMD) 814 if (host->status == HOST_S_CMD)
793 au1xmmc_cmd_complete(host,status); 815 au1xmmc_cmd_complete(host, status);
794 } 816
795 else if (!(host->flags & HOST_F_DMA)) { 817 } else if (!(host->flags & HOST_F_DMA)) {
796 if ((host->flags & HOST_F_XMIT) && 818 if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
797 (status & STATUS_DATA_OUT)) 819 au1xmmc_send_pio(host);
798 au1xmmc_send_pio(host); 820 else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
799 else if ((host->flags & HOST_F_RECV) && 821 au1xmmc_receive_pio(host);
800 (status & STATUS_DATA_IN)) 822
801 au1xmmc_receive_pio(host); 823 } else if (status & 0x203F3C70) {
802 } 824 DBG("Unhandled status %8.8x\n", host->pdev->id,
803 else if (status & 0x203FBC70) { 825 status);
804 DBG("Unhandled status %8.8x\n", host->id, status);
805 handled = 0;
806 }
807
808 au_writel(status, HOST_STATUS(host));
809 au_sync();
810
811 ret |= handled;
812 } 826 }
813 827
814 enable_irq(AU1100_SD_IRQ); 828 au_writel(status, HOST_STATUS(host));
815 return ret; 829 au_sync();
830
831 return IRQ_HANDLED;
816} 832}
817 833
818static void au1xmmc_poll_event(unsigned long arg) 834#ifdef CONFIG_SOC_AU1200
819{ 835/* 8bit memory DMA device */
820 struct au1xmmc_host *host = (struct au1xmmc_host *) arg; 836static dbdev_tab_t au1xmmc_mem_dbdev = {
837 .dev_id = DSCR_CMD0_ALWAYS,
838 .dev_flags = DEV_FLAGS_ANYUSE,
839 .dev_tsize = 0,
840 .dev_devwidth = 8,
841 .dev_physaddr = 0x00000000,
842 .dev_intlevel = 0,
843 .dev_intpolarity = 0,
844};
845static int memid;
821 846
822 int card = au1xmmc_card_inserted(host); 847static void au1xmmc_dbdma_callback(int irq, void *dev_id)
823 int controller = (host->flags & HOST_F_ACTIVE) ? 1 : 0; 848{
849 struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
824 850
825 if (card != controller) { 851 /* Avoid spurious interrupts */
826 host->flags &= ~HOST_F_ACTIVE; 852 if (!host->mrq)
827 if (card) host->flags |= HOST_F_ACTIVE; 853 return;
828 mmc_detect_change(host->mmc, 0);
829 }
830 854
831 if (host->mrq != NULL) { 855 if (host->flags & HOST_F_STOP)
832 u32 status = au_readl(HOST_STATUS(host)); 856 SEND_STOP(host);
833 DBG("PENDING - %8.8x\n", host->id, status);
834 }
835 857
836 mod_timer(&host->timer, jiffies + AU1XMMC_DETECT_TIMEOUT); 858 tasklet_schedule(&host->data_task);
837} 859}
838 860
839static dbdev_tab_t au1xmmc_mem_dbdev = 861static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
840{
841 DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 8, 0x00000000, 0, 0
842};
843
844static void au1xmmc_init_dma(struct au1xmmc_host *host)
845{ 862{
863 struct resource *res;
864 int txid, rxid;
865
866 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
867 if (!res)
868 return -ENODEV;
869 txid = res->start;
870
871 res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
872 if (!res)
873 return -ENODEV;
874 rxid = res->start;
875
876 if (!memid)
877 return -ENODEV;
878
879 host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
880 au1xmmc_dbdma_callback, (void *)host);
881 if (!host->tx_chan) {
882 dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
883 return -ENODEV;
884 }
846 885
847 u32 rxchan, txchan; 886 host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
848 887 au1xmmc_dbdma_callback, (void *)host);
849 int txid = au1xmmc_card_table[host->id].tx_devid; 888 if (!host->rx_chan) {
850 int rxid = au1xmmc_card_table[host->id].rx_devid; 889 dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
890 au1xxx_dbdma_chan_free(host->tx_chan);
891 return -ENODEV;
892 }
851 893
852 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride 894 au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
853 of 8 bits. And since devices are shared, we need to create 895 au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
854 our own to avoid freaking out other devices
855 */
856 896
857 int memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev); 897 au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
898 au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
858 899
859 txchan = au1xxx_dbdma_chan_alloc(memid, txid, 900 /* DBDMA is good to go */
860 au1xmmc_dma_callback, (void *) host); 901 host->flags |= HOST_F_DMA;
861 902
862 rxchan = au1xxx_dbdma_chan_alloc(rxid, memid, 903 return 0;
863 au1xmmc_dma_callback, (void *) host); 904}
864 905
865 au1xxx_dbdma_set_devwidth(txchan, 8); 906static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
866 au1xxx_dbdma_set_devwidth(rxchan, 8); 907{
908 if (host->flags & HOST_F_DMA) {
909 host->flags &= ~HOST_F_DMA;
910 au1xxx_dbdma_chan_free(host->tx_chan);
911 au1xxx_dbdma_chan_free(host->rx_chan);
912 }
913}
914#endif
867 915
868 au1xxx_dbdma_ring_alloc(txchan, AU1XMMC_DESCRIPTOR_COUNT); 916static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
869 au1xxx_dbdma_ring_alloc(rxchan, AU1XMMC_DESCRIPTOR_COUNT); 917{
918 struct au1xmmc_host *host = mmc_priv(mmc);
870 919
871 host->tx_chan = txchan; 920 if (en)
872 host->rx_chan = rxchan; 921 IRQ_ON(host, SD_CONFIG_SI);
922 else
923 IRQ_OFF(host, SD_CONFIG_SI);
873} 924}
874 925
875static const struct mmc_host_ops au1xmmc_ops = { 926static const struct mmc_host_ops au1xmmc_ops = {
876 .request = au1xmmc_request, 927 .request = au1xmmc_request,
877 .set_ios = au1xmmc_set_ios, 928 .set_ios = au1xmmc_set_ios,
878 .get_ro = au1xmmc_card_readonly, 929 .get_ro = au1xmmc_card_readonly,
930 .get_cd = au1xmmc_card_inserted,
931 .enable_sdio_irq = au1xmmc_enable_sdio_irq,
879}; 932};
880 933
881static int __devinit au1xmmc_probe(struct platform_device *pdev) 934static int __devinit au1xmmc_probe(struct platform_device *pdev)
882{ 935{
936 struct mmc_host *mmc;
937 struct au1xmmc_host *host;
938 struct resource *r;
939 int ret;
940
941 mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
942 if (!mmc) {
943 dev_err(&pdev->dev, "no memory for mmc_host\n");
944 ret = -ENOMEM;
945 goto out0;
946 }
883 947
884 int i, ret = 0; 948 host = mmc_priv(mmc);
885 949 host->mmc = mmc;
886 /* THe interrupt is shared among all controllers */ 950 host->platdata = pdev->dev.platform_data;
887 ret = request_irq(AU1100_SD_IRQ, au1xmmc_irq, IRQF_DISABLED, "MMC", 0); 951 host->pdev = pdev;
888 952
889 if (ret) { 953 ret = -ENODEV;
890 printk(DRIVER_NAME "ERROR: Couldn't get int %d: %d\n", 954 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
891 AU1100_SD_IRQ, ret); 955 if (!r) {
892 return -ENXIO; 956 dev_err(&pdev->dev, "no mmio defined\n");
957 goto out1;
893 } 958 }
894 959
895 disable_irq(AU1100_SD_IRQ); 960 host->ioarea = request_mem_region(r->start, r->end - r->start + 1,
961 pdev->name);
962 if (!host->ioarea) {
963 dev_err(&pdev->dev, "mmio already in use\n");
964 goto out1;
965 }
896 966
897 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) { 967 host->iobase = (unsigned long)ioremap(r->start, 0x3c);
898 struct mmc_host *mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev); 968 if (!host->iobase) {
899 struct au1xmmc_host *host = 0; 969 dev_err(&pdev->dev, "cannot remap mmio\n");
970 goto out2;
971 }
900 972
901 if (!mmc) { 973 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
902 printk(DRIVER_NAME "ERROR: no mem for host %d\n", i); 974 if (!r) {
903 au1xmmc_hosts[i] = 0; 975 dev_err(&pdev->dev, "no IRQ defined\n");
904 continue; 976 goto out3;
905 } 977 }
906 978
907 mmc->ops = &au1xmmc_ops; 979 host->irq = r->start;
980 /* IRQ is shared among both SD controllers */
981 ret = request_irq(host->irq, au1xmmc_irq, IRQF_SHARED,
982 DRIVER_NAME, host);
983 if (ret) {
984 dev_err(&pdev->dev, "cannot grab IRQ\n");
985 goto out3;
986 }
908 987
909 mmc->f_min = 450000; 988 mmc->ops = &au1xmmc_ops;
910 mmc->f_max = 24000000;
911 989
912 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE; 990 mmc->f_min = 450000;
913 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT; 991 mmc->f_max = 24000000;
914 992
915 mmc->max_blk_size = 2048; 993 mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
916 mmc->max_blk_count = 512; 994 mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
917 995
918 mmc->ocr_avail = AU1XMMC_OCR; 996 mmc->max_blk_size = 2048;
997 mmc->max_blk_count = 512;
919 998
920 host = mmc_priv(mmc); 999 mmc->ocr_avail = AU1XMMC_OCR;
921 host->mmc = mmc; 1000 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
922 1001
923 host->id = i; 1002 host->status = HOST_S_IDLE;
924 host->iobase = au1xmmc_card_table[host->id].iobase;
925 host->clock = 0;
926 host->power_mode = MMC_POWER_OFF;
927 1003
928 host->flags = au1xmmc_card_inserted(host) ? HOST_F_ACTIVE : 0; 1004 /* board-specific carddetect setup, if any */
929 host->status = HOST_S_IDLE; 1005 if (host->platdata && host->platdata->cd_setup) {
1006 ret = host->platdata->cd_setup(mmc, 1);
1007 if (ret) {
1008 dev_warn(&pdev->dev, "board CD setup failed\n");
1009 mmc->caps |= MMC_CAP_NEEDS_POLL;
1010 }
1011 } else
1012 mmc->caps |= MMC_CAP_NEEDS_POLL;
930 1013
931 init_timer(&host->timer); 1014 tasklet_init(&host->data_task, au1xmmc_tasklet_data,
1015 (unsigned long)host);
932 1016
933 host->timer.function = au1xmmc_poll_event; 1017 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish,
934 host->timer.data = (unsigned long) host; 1018 (unsigned long)host);
935 host->timer.expires = jiffies + AU1XMMC_DETECT_TIMEOUT;
936 1019
937 tasklet_init(&host->data_task, au1xmmc_tasklet_data, 1020#ifdef CONFIG_SOC_AU1200
938 (unsigned long) host); 1021 ret = au1xmmc_dbdma_init(host);
1022 if (ret)
1023 printk(KERN_INFO DRIVER_NAME ": DBDMA init failed; using PIO\n");
1024#endif
939 1025
940 tasklet_init(&host->finish_task, au1xmmc_tasklet_finish, 1026#ifdef CONFIG_LEDS_CLASS
941 (unsigned long) host); 1027 if (host->platdata && host->platdata->led) {
1028 struct led_classdev *led = host->platdata->led;
1029 led->name = mmc_hostname(mmc);
1030 led->brightness = LED_OFF;
1031 led->default_trigger = mmc_hostname(mmc);
1032 ret = led_classdev_register(mmc_dev(mmc), led);
1033 if (ret)
1034 goto out5;
1035 }
1036#endif
942 1037
943 spin_lock_init(&host->lock); 1038 au1xmmc_reset_controller(host);
944 1039
945 if (dma != 0) 1040 ret = mmc_add_host(mmc);
946 au1xmmc_init_dma(host); 1041 if (ret) {
1042 dev_err(&pdev->dev, "cannot add mmc host\n");
1043 goto out6;
1044 }
947 1045
948 au1xmmc_reset_controller(host); 1046 platform_set_drvdata(pdev, mmc);
949 1047
950 mmc_add_host(mmc); 1048 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X"
951 au1xmmc_hosts[i] = host; 1049 " (mode=%s)\n", pdev->id, host->iobase,
1050 host->flags & HOST_F_DMA ? "dma" : "pio");
952 1051
953 add_timer(&host->timer); 1052 return 0; /* all ok */
954 1053
955 printk(KERN_INFO DRIVER_NAME ": MMC Controller %d set up at %8.8X (mode=%s)\n", 1054out6:
956 host->id, host->iobase, dma ? "dma" : "pio"); 1055#ifdef CONFIG_LEDS_CLASS
957 } 1056 if (host->platdata && host->platdata->led)
1057 led_classdev_unregister(host->platdata->led);
1058out5:
1059#endif
1060 au_writel(0, HOST_ENABLE(host));
1061 au_writel(0, HOST_CONFIG(host));
1062 au_writel(0, HOST_CONFIG2(host));
1063 au_sync();
958 1064
959 enable_irq(AU1100_SD_IRQ); 1065#ifdef CONFIG_SOC_AU1200
1066 au1xmmc_dbdma_shutdown(host);
1067#endif
960 1068
961 return 0; 1069 tasklet_kill(&host->data_task);
1070 tasklet_kill(&host->finish_task);
1071
1072 if (host->platdata && host->platdata->cd_setup &&
1073 !(mmc->caps & MMC_CAP_NEEDS_POLL))
1074 host->platdata->cd_setup(mmc, 0);
1075
1076 free_irq(host->irq, host);
1077out3:
1078 iounmap((void *)host->iobase);
1079out2:
1080 release_resource(host->ioarea);
1081 kfree(host->ioarea);
1082out1:
1083 mmc_free_host(mmc);
1084out0:
1085 return ret;
962} 1086}
963 1087
964static int __devexit au1xmmc_remove(struct platform_device *pdev) 1088static int __devexit au1xmmc_remove(struct platform_device *pdev)
965{ 1089{
1090 struct mmc_host *mmc = platform_get_drvdata(pdev);
1091 struct au1xmmc_host *host;
1092
1093 if (mmc) {
1094 host = mmc_priv(mmc);
966 1095
967 int i; 1096 mmc_remove_host(mmc);
968 1097
969 disable_irq(AU1100_SD_IRQ); 1098#ifdef CONFIG_LEDS_CLASS
1099 if (host->platdata && host->platdata->led)
1100 led_classdev_unregister(host->platdata->led);
1101#endif
970 1102
971 for(i = 0; i < AU1XMMC_CONTROLLER_COUNT; i++) { 1103 if (host->platdata && host->platdata->cd_setup &&
972 struct au1xmmc_host *host = au1xmmc_hosts[i]; 1104 !(mmc->caps & MMC_CAP_NEEDS_POLL))
973 if (!host) continue; 1105 host->platdata->cd_setup(mmc, 0);
1106
1107 au_writel(0, HOST_ENABLE(host));
1108 au_writel(0, HOST_CONFIG(host));
1109 au_writel(0, HOST_CONFIG2(host));
1110 au_sync();
974 1111
975 tasklet_kill(&host->data_task); 1112 tasklet_kill(&host->data_task);
976 tasklet_kill(&host->finish_task); 1113 tasklet_kill(&host->finish_task);
977 1114
978 del_timer_sync(&host->timer); 1115#ifdef CONFIG_SOC_AU1200
1116 au1xmmc_dbdma_shutdown(host);
1117#endif
979 au1xmmc_set_power(host, 0); 1118 au1xmmc_set_power(host, 0);
980 1119
981 mmc_remove_host(host->mmc); 1120 free_irq(host->irq, host);
982 1121 iounmap((void *)host->iobase);
983 au1xxx_dbdma_chan_free(host->tx_chan); 1122 release_resource(host->ioarea);
984 au1xxx_dbdma_chan_free(host->rx_chan); 1123 kfree(host->ioarea);
985 1124
986 au_writel(0x0, HOST_ENABLE(host)); 1125 mmc_free_host(mmc);
987 au_sync();
988 } 1126 }
989
990 free_irq(AU1100_SD_IRQ, 0);
991 return 0; 1127 return 0;
992} 1128}
993 1129
@@ -1004,21 +1140,31 @@ static struct platform_driver au1xmmc_driver = {
1004 1140
1005static int __init au1xmmc_init(void) 1141static int __init au1xmmc_init(void)
1006{ 1142{
1143#ifdef CONFIG_SOC_AU1200
1144 /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
1145 * of 8 bits. And since devices are shared, we need to create
1146 * our own to avoid freaking out other devices.
1147 */
1148 memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
1149 if (!memid)
1150 printk(KERN_ERR "au1xmmc: cannot add memory dbdma dev\n");
1151#endif
1007 return platform_driver_register(&au1xmmc_driver); 1152 return platform_driver_register(&au1xmmc_driver);
1008} 1153}
1009 1154
1010static void __exit au1xmmc_exit(void) 1155static void __exit au1xmmc_exit(void)
1011{ 1156{
1157#ifdef CONFIG_SOC_AU1200
1158 if (memid)
1159 au1xxx_ddma_del_device(memid);
1160#endif
1012 platform_driver_unregister(&au1xmmc_driver); 1161 platform_driver_unregister(&au1xmmc_driver);
1013} 1162}
1014 1163
1015module_init(au1xmmc_init); 1164module_init(au1xmmc_init);
1016module_exit(au1xmmc_exit); 1165module_exit(au1xmmc_exit);
1017 1166
1018#ifdef MODULE
1019MODULE_AUTHOR("Advanced Micro Devices, Inc"); 1167MODULE_AUTHOR("Advanced Micro Devices, Inc");
1020MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX"); 1168MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
1021MODULE_LICENSE("GPL"); 1169MODULE_LICENSE("GPL");
1022MODULE_ALIAS("platform:au1xxx-mmc"); 1170MODULE_ALIAS("platform:au1xxx-mmc");
1023#endif
1024
diff --git a/drivers/mmc/host/au1xmmc.h b/drivers/mmc/host/au1xmmc.h
deleted file mode 100644
index 341cbdf0baca..000000000000
--- a/drivers/mmc/host/au1xmmc.h
+++ /dev/null
@@ -1,96 +0,0 @@
1#ifndef _AU1XMMC_H_
2#define _AU1XMMC_H_
3
4/* Hardware definitions */
5
6#define AU1XMMC_DESCRIPTOR_COUNT 1
7#define AU1XMMC_DESCRIPTOR_SIZE 2048
8
9#define AU1XMMC_OCR ( MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
10 MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
11 MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
12
13/* Easy access macros */
14
15#define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
16#define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
17#define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
18#define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
19#define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
20#define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
21#define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
22#define HOST_CMD(h) ((h)->iobase + SD_CMD)
23#define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
24#define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
25#define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
26
27#define DMA_CHANNEL(h) \
28 ( ((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
29
30/* This gives us a hard value for the stop command that we can write directly
31 * to the command register
32 */
33
34#define STOP_CMD (SD_CMD_RT_1B|SD_CMD_CT_7|(0xC << SD_CMD_CI_SHIFT)|SD_CMD_GO)
35
36/* This is the set of interrupts that we configure by default */
37
38#if 0
39#define AU1XMMC_INTERRUPTS (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_DD | \
40 SD_CONFIG_RAT | SD_CONFIG_CR | SD_CONFIG_I)
41#endif
42
43#define AU1XMMC_INTERRUPTS (SD_CONFIG_SC | SD_CONFIG_DT | \
44 SD_CONFIG_RAT | SD_CONFIG_CR | SD_CONFIG_I)
45/* The poll event (looking for insert/remove events runs twice a second */
46#define AU1XMMC_DETECT_TIMEOUT (HZ/2)
47
48struct au1xmmc_host {
49 struct mmc_host *mmc;
50 struct mmc_request *mrq;
51
52 u32 id;
53
54 u32 flags;
55 u32 iobase;
56 u32 clock;
57 u32 bus_width;
58 u32 power_mode;
59
60 int status;
61
62 struct {
63 int len;
64 int dir;
65 } dma;
66
67 struct {
68 int index;
69 int offset;
70 int len;
71 } pio;
72
73 u32 tx_chan;
74 u32 rx_chan;
75
76 struct timer_list timer;
77 struct tasklet_struct finish_task;
78 struct tasklet_struct data_task;
79
80 spinlock_t lock;
81};
82
83/* Status flags used by the host structure */
84
85#define HOST_F_XMIT 0x0001
86#define HOST_F_RECV 0x0002
87#define HOST_F_DMA 0x0010
88#define HOST_F_ACTIVE 0x0100
89#define HOST_F_STOP 0x1000
90
91#define HOST_S_IDLE 0x0001
92#define HOST_S_CMD 0x0002
93#define HOST_S_DATA 0x0003
94#define HOST_S_STOP 0x0004
95
96#endif
diff --git a/drivers/mmc/host/imxmmc.c b/drivers/mmc/host/imxmmc.c
index eed211b2ac70..5e880c0f1349 100644
--- a/drivers/mmc/host/imxmmc.c
+++ b/drivers/mmc/host/imxmmc.c
@@ -892,9 +892,12 @@ static int imxmci_get_ro(struct mmc_host *mmc)
892 struct imxmci_host *host = mmc_priv(mmc); 892 struct imxmci_host *host = mmc_priv(mmc);
893 893
894 if (host->pdata && host->pdata->get_ro) 894 if (host->pdata && host->pdata->get_ro)
895 return host->pdata->get_ro(mmc_dev(mmc)); 895 return !!host->pdata->get_ro(mmc_dev(mmc));
896 /* Host doesn't support read only detection so assume writeable */ 896 /*
897 return 0; 897 * Board doesn't support read only detection; let the mmc core
898 * decide what to do.
899 */
900 return -ENOSYS;
898} 901}
899 902
900 903
diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
index 35508584ac2a..41cc63360e43 100644
--- a/drivers/mmc/host/mmc_spi.c
+++ b/drivers/mmc/host/mmc_spi.c
@@ -1126,16 +1126,28 @@ static int mmc_spi_get_ro(struct mmc_host *mmc)
1126 struct mmc_spi_host *host = mmc_priv(mmc); 1126 struct mmc_spi_host *host = mmc_priv(mmc);
1127 1127
1128 if (host->pdata && host->pdata->get_ro) 1128 if (host->pdata && host->pdata->get_ro)
1129 return host->pdata->get_ro(mmc->parent); 1129 return !!host->pdata->get_ro(mmc->parent);
1130 /* board doesn't support read only detection; assume writeable */ 1130 /*
1131 return 0; 1131 * Board doesn't support read only detection; let the mmc core
1132 * decide what to do.
1133 */
1134 return -ENOSYS;
1132} 1135}
1133 1136
1137static int mmc_spi_get_cd(struct mmc_host *mmc)
1138{
1139 struct mmc_spi_host *host = mmc_priv(mmc);
1140
1141 if (host->pdata && host->pdata->get_cd)
1142 return !!host->pdata->get_cd(mmc->parent);
1143 return -ENOSYS;
1144}
1134 1145
1135static const struct mmc_host_ops mmc_spi_ops = { 1146static const struct mmc_host_ops mmc_spi_ops = {
1136 .request = mmc_spi_request, 1147 .request = mmc_spi_request,
1137 .set_ios = mmc_spi_set_ios, 1148 .set_ios = mmc_spi_set_ios,
1138 .get_ro = mmc_spi_get_ro, 1149 .get_ro = mmc_spi_get_ro,
1150 .get_cd = mmc_spi_get_cd,
1139}; 1151};
1140 1152
1141 1153
@@ -1240,10 +1252,7 @@ static int mmc_spi_probe(struct spi_device *spi)
1240 mmc->ops = &mmc_spi_ops; 1252 mmc->ops = &mmc_spi_ops;
1241 mmc->max_blk_size = MMC_SPI_BLOCKSIZE; 1253 mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
1242 1254
1243 /* As long as we keep track of the number of successfully 1255 mmc->caps = MMC_CAP_SPI;
1244 * transmitted blocks, we're good for multiwrite.
1245 */
1246 mmc->caps = MMC_CAP_SPI | MMC_CAP_MULTIWRITE;
1247 1256
1248 /* SPI doesn't need the lowspeed device identification thing for 1257 /* SPI doesn't need the lowspeed device identification thing for
1249 * MMC or SD cards, since it never comes up in open drain mode. 1258 * MMC or SD cards, since it never comes up in open drain mode.
@@ -1319,17 +1328,23 @@ static int mmc_spi_probe(struct spi_device *spi)
1319 goto fail_glue_init; 1328 goto fail_glue_init;
1320 } 1329 }
1321 1330
1331 /* pass platform capabilities, if any */
1332 if (host->pdata)
1333 mmc->caps |= host->pdata->caps;
1334
1322 status = mmc_add_host(mmc); 1335 status = mmc_add_host(mmc);
1323 if (status != 0) 1336 if (status != 0)
1324 goto fail_add_host; 1337 goto fail_add_host;
1325 1338
1326 dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n", 1339 dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n",
1327 mmc->class_dev.bus_id, 1340 mmc->class_dev.bus_id,
1328 host->dma_dev ? "" : ", no DMA", 1341 host->dma_dev ? "" : ", no DMA",
1329 (host->pdata && host->pdata->get_ro) 1342 (host->pdata && host->pdata->get_ro)
1330 ? "" : ", no WP", 1343 ? "" : ", no WP",
1331 (host->pdata && host->pdata->setpower) 1344 (host->pdata && host->pdata->setpower)
1332 ? "" : ", no poweroff"); 1345 ? "" : ", no poweroff",
1346 (mmc->caps & MMC_CAP_NEEDS_POLL)
1347 ? ", cd polling" : "");
1333 return 0; 1348 return 0;
1334 1349
1335fail_add_host: 1350fail_add_host:
diff --git a/drivers/mmc/host/mmci.c b/drivers/mmc/host/mmci.c
index da5fecad74d9..696cf3647ceb 100644
--- a/drivers/mmc/host/mmci.c
+++ b/drivers/mmc/host/mmci.c
@@ -535,7 +535,6 @@ static int mmci_probe(struct amba_device *dev, void *id)
535 mmc->f_min = (host->mclk + 511) / 512; 535 mmc->f_min = (host->mclk + 511) / 512;
536 mmc->f_max = min(host->mclk, fmax); 536 mmc->f_max = min(host->mclk, fmax);
537 mmc->ocr_avail = plat->ocr_mask; 537 mmc->ocr_avail = plat->ocr_mask;
538 mmc->caps = MMC_CAP_MULTIWRITE;
539 538
540 /* 539 /*
541 * We can do SGIO 540 * We can do SGIO
diff --git a/drivers/mmc/host/omap.c b/drivers/mmc/host/omap.c
index 549517c35675..dbc26eb6a89e 100644
--- a/drivers/mmc/host/omap.c
+++ b/drivers/mmc/host/omap.c
@@ -1317,7 +1317,7 @@ static int __init mmc_omap_new_slot(struct mmc_omap_host *host, int id)
1317 1317
1318 host->slots[id] = slot; 1318 host->slots[id] = slot;
1319 1319
1320 mmc->caps = MMC_CAP_MULTIWRITE; 1320 mmc->caps = 0;
1321 if (host->pdata->conf.wire4) 1321 if (host->pdata->conf.wire4)
1322 mmc->caps |= MMC_CAP_4_BIT_DATA; 1322 mmc->caps |= MMC_CAP_4_BIT_DATA;
1323 1323
diff --git a/drivers/mmc/host/pxamci.c b/drivers/mmc/host/pxamci.c
index d89475d36988..d39f59738866 100644
--- a/drivers/mmc/host/pxamci.c
+++ b/drivers/mmc/host/pxamci.c
@@ -374,9 +374,12 @@ static int pxamci_get_ro(struct mmc_host *mmc)
374 struct pxamci_host *host = mmc_priv(mmc); 374 struct pxamci_host *host = mmc_priv(mmc);
375 375
376 if (host->pdata && host->pdata->get_ro) 376 if (host->pdata && host->pdata->get_ro)
377 return host->pdata->get_ro(mmc_dev(mmc)); 377 return !!host->pdata->get_ro(mmc_dev(mmc));
378 /* Host doesn't support read only detection so assume writeable */ 378 /*
379 return 0; 379 * Board doesn't support read only detection; let the mmc core
380 * decide what to do.
381 */
382 return -ENOSYS;
380} 383}
381 384
382static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 385static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
diff --git a/drivers/mmc/host/s3cmci.c b/drivers/mmc/host/s3cmci.c
new file mode 100644
index 000000000000..6a1e4994b724
--- /dev/null
+++ b/drivers/mmc/host/s3cmci.c
@@ -0,0 +1,1446 @@
1/*
2 * linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
3 *
4 * Copyright (C) 2004-2006 maintech GmbH, Thomas Kleffel <tk@maintech.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/dma-mapping.h>
13#include <linux/clk.h>
14#include <linux/mmc/host.h>
15#include <linux/platform_device.h>
16#include <linux/irq.h>
17#include <linux/io.h>
18
19#include <asm/dma.h>
20
21#include <asm/arch/regs-sdi.h>
22#include <asm/arch/regs-gpio.h>
23
24#include <asm/plat-s3c24xx/mci.h>
25
26#include "s3cmci.h"
27
28#define DRIVER_NAME "s3c-mci"
29
30enum dbg_channels {
31 dbg_err = (1 << 0),
32 dbg_debug = (1 << 1),
33 dbg_info = (1 << 2),
34 dbg_irq = (1 << 3),
35 dbg_sg = (1 << 4),
36 dbg_dma = (1 << 5),
37 dbg_pio = (1 << 6),
38 dbg_fail = (1 << 7),
39 dbg_conf = (1 << 8),
40};
41
42static const int dbgmap_err = dbg_err | dbg_fail;
43static const int dbgmap_info = dbg_info | dbg_conf;
44static const int dbgmap_debug = dbg_debug;
45
46#define dbg(host, channels, args...) \
47 do { \
48 if (dbgmap_err & channels) \
49 dev_err(&host->pdev->dev, args); \
50 else if (dbgmap_info & channels) \
51 dev_info(&host->pdev->dev, args); \
52 else if (dbgmap_debug & channels) \
53 dev_dbg(&host->pdev->dev, args); \
54 } while (0)
55
56#define RESSIZE(ressource) (((ressource)->end - (ressource)->start)+1)
57
58static struct s3c2410_dma_client s3cmci_dma_client = {
59 .name = "s3c-mci",
60};
61
62static void finalize_request(struct s3cmci_host *host);
63static void s3cmci_send_request(struct mmc_host *mmc);
64static void s3cmci_reset(struct s3cmci_host *host);
65
66#ifdef CONFIG_MMC_DEBUG
67
68static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
69{
70 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
71 u32 datcon, datcnt, datsta, fsta, imask;
72
73 con = readl(host->base + S3C2410_SDICON);
74 pre = readl(host->base + S3C2410_SDIPRE);
75 cmdarg = readl(host->base + S3C2410_SDICMDARG);
76 cmdcon = readl(host->base + S3C2410_SDICMDCON);
77 cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
78 r0 = readl(host->base + S3C2410_SDIRSP0);
79 r1 = readl(host->base + S3C2410_SDIRSP1);
80 r2 = readl(host->base + S3C2410_SDIRSP2);
81 r3 = readl(host->base + S3C2410_SDIRSP3);
82 timer = readl(host->base + S3C2410_SDITIMER);
83 bsize = readl(host->base + S3C2410_SDIBSIZE);
84 datcon = readl(host->base + S3C2410_SDIDCON);
85 datcnt = readl(host->base + S3C2410_SDIDCNT);
86 datsta = readl(host->base + S3C2410_SDIDSTA);
87 fsta = readl(host->base + S3C2410_SDIFSTA);
88 imask = readl(host->base + host->sdiimsk);
89
90 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
91 prefix, con, pre, timer);
92
93 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
94 prefix, cmdcon, cmdarg, cmdsta);
95
96 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
97 " DSTA:[%08x] DCNT:[%08x]\n",
98 prefix, datcon, fsta, datsta, datcnt);
99
100 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
101 " R2:[%08x] R3:[%08x]\n",
102 prefix, r0, r1, r2, r3);
103}
104
105static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
106 int stop)
107{
108 snprintf(host->dbgmsg_cmd, 300,
109 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
110 host->ccnt, (stop ? " (STOP)" : ""),
111 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
112
113 if (cmd->data) {
114 snprintf(host->dbgmsg_dat, 300,
115 "#%u bsize:%u blocks:%u bytes:%u",
116 host->dcnt, cmd->data->blksz,
117 cmd->data->blocks,
118 cmd->data->blocks * cmd->data->blksz);
119 } else {
120 host->dbgmsg_dat[0] = '\0';
121 }
122}
123
124static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
125 int fail)
126{
127 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
128
129 if (!cmd)
130 return;
131
132 if (cmd->error == 0) {
133 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
134 host->dbgmsg_cmd, cmd->resp[0]);
135 } else {
136 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
137 cmd->error, host->dbgmsg_cmd, host->status);
138 }
139
140 if (!cmd->data)
141 return;
142
143 if (cmd->data->error == 0) {
144 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
145 } else {
146 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
147 cmd->data->error, host->dbgmsg_dat,
148 readl(host->base + S3C2410_SDIDCNT));
149 }
150}
151#else
152static void dbg_dumpcmd(struct s3cmci_host *host,
153 struct mmc_command *cmd, int fail) { }
154
155static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
156 int stop) { }
157
158static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
159
160#endif /* CONFIG_MMC_DEBUG */
161
162static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
163{
164 u32 newmask;
165
166 newmask = readl(host->base + host->sdiimsk);
167 newmask |= imask;
168
169 writel(newmask, host->base + host->sdiimsk);
170
171 return newmask;
172}
173
174static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
175{
176 u32 newmask;
177
178 newmask = readl(host->base + host->sdiimsk);
179 newmask &= ~imask;
180
181 writel(newmask, host->base + host->sdiimsk);
182
183 return newmask;
184}
185
186static inline void clear_imask(struct s3cmci_host *host)
187{
188 writel(0, host->base + host->sdiimsk);
189}
190
191static inline int get_data_buffer(struct s3cmci_host *host,
192 u32 *words, u32 **pointer)
193{
194 struct scatterlist *sg;
195
196 if (host->pio_active == XFER_NONE)
197 return -EINVAL;
198
199 if ((!host->mrq) || (!host->mrq->data))
200 return -EINVAL;
201
202 if (host->pio_sgptr >= host->mrq->data->sg_len) {
203 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
204 host->pio_sgptr, host->mrq->data->sg_len);
205 return -EBUSY;
206 }
207 sg = &host->mrq->data->sg[host->pio_sgptr];
208
209 *words = sg->length >> 2;
210 *pointer = sg_virt(sg);
211
212 host->pio_sgptr++;
213
214 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
215 host->pio_sgptr, host->mrq->data->sg_len);
216
217 return 0;
218}
219
220static inline u32 fifo_count(struct s3cmci_host *host)
221{
222 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
223
224 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
225 return fifostat >> 2;
226}
227
228static inline u32 fifo_free(struct s3cmci_host *host)
229{
230 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
231
232 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
233 return (63 - fifostat) >> 2;
234}
235
236static void do_pio_read(struct s3cmci_host *host)
237{
238 int res;
239 u32 fifo;
240 void __iomem *from_ptr;
241
242 /* write real prescaler to host, it might be set slow to fix */
243 writel(host->prescaler, host->base + S3C2410_SDIPRE);
244
245 from_ptr = host->base + host->sdidata;
246
247 while ((fifo = fifo_count(host))) {
248 if (!host->pio_words) {
249 res = get_data_buffer(host, &host->pio_words,
250 &host->pio_ptr);
251 if (res) {
252 host->pio_active = XFER_NONE;
253 host->complete_what = COMPLETION_FINALIZE;
254
255 dbg(host, dbg_pio, "pio_read(): "
256 "complete (no more data).\n");
257 return;
258 }
259
260 dbg(host, dbg_pio,
261 "pio_read(): new target: [%i]@[%p]\n",
262 host->pio_words, host->pio_ptr);
263 }
264
265 dbg(host, dbg_pio,
266 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
267 fifo, host->pio_words,
268 readl(host->base + S3C2410_SDIDCNT));
269
270 if (fifo > host->pio_words)
271 fifo = host->pio_words;
272
273 host->pio_words -= fifo;
274 host->pio_count += fifo;
275
276 while (fifo--)
277 *(host->pio_ptr++) = readl(from_ptr);
278 }
279
280 if (!host->pio_words) {
281 res = get_data_buffer(host, &host->pio_words, &host->pio_ptr);
282 if (res) {
283 dbg(host, dbg_pio,
284 "pio_read(): complete (no more buffers).\n");
285 host->pio_active = XFER_NONE;
286 host->complete_what = COMPLETION_FINALIZE;
287
288 return;
289 }
290 }
291
292 enable_imask(host,
293 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
294}
295
296static void do_pio_write(struct s3cmci_host *host)
297{
298 void __iomem *to_ptr;
299 int res;
300 u32 fifo;
301
302 to_ptr = host->base + host->sdidata;
303
304 while ((fifo = fifo_free(host))) {
305 if (!host->pio_words) {
306 res = get_data_buffer(host, &host->pio_words,
307 &host->pio_ptr);
308 if (res) {
309 dbg(host, dbg_pio,
310 "pio_write(): complete (no more data).\n");
311 host->pio_active = XFER_NONE;
312
313 return;
314 }
315
316 dbg(host, dbg_pio,
317 "pio_write(): new source: [%i]@[%p]\n",
318 host->pio_words, host->pio_ptr);
319
320 }
321
322 if (fifo > host->pio_words)
323 fifo = host->pio_words;
324
325 host->pio_words -= fifo;
326 host->pio_count += fifo;
327
328 while (fifo--)
329 writel(*(host->pio_ptr++), to_ptr);
330 }
331
332 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
333}
334
335static void pio_tasklet(unsigned long data)
336{
337 struct s3cmci_host *host = (struct s3cmci_host *) data;
338
339
340 disable_irq(host->irq);
341
342 if (host->pio_active == XFER_WRITE)
343 do_pio_write(host);
344
345 if (host->pio_active == XFER_READ)
346 do_pio_read(host);
347
348 if (host->complete_what == COMPLETION_FINALIZE) {
349 clear_imask(host);
350 if (host->pio_active != XFER_NONE) {
351 dbg(host, dbg_err, "unfinished %s "
352 "- pio_count:[%u] pio_words:[%u]\n",
353 (host->pio_active == XFER_READ) ? "read" : "write",
354 host->pio_count, host->pio_words);
355
356 if (host->mrq->data)
357 host->mrq->data->error = -EINVAL;
358 }
359
360 finalize_request(host);
361 } else
362 enable_irq(host->irq);
363}
364
365/*
366 * ISR for SDI Interface IRQ
367 * Communication between driver and ISR works as follows:
368 * host->mrq points to current request
369 * host->complete_what Indicates when the request is considered done
370 * COMPLETION_CMDSENT when the command was sent
371 * COMPLETION_RSPFIN when a response was received
372 * COMPLETION_XFERFINISH when the data transfer is finished
373 * COMPLETION_XFERFINISH_RSPFIN both of the above.
374 * host->complete_request is the completion-object the driver waits for
375 *
376 * 1) Driver sets up host->mrq and host->complete_what
377 * 2) Driver prepares the transfer
378 * 3) Driver enables interrupts
379 * 4) Driver starts transfer
380 * 5) Driver waits for host->complete_rquest
381 * 6) ISR checks for request status (errors and success)
382 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
383 * 7) ISR completes host->complete_request
384 * 8) ISR disables interrupts
385 * 9) Driver wakes up and takes care of the request
386 *
387 * Note: "->error"-fields are expected to be set to 0 before the request
388 * was issued by mmc.c - therefore they are only set, when an error
389 * contition comes up
390 */
391
392static irqreturn_t s3cmci_irq(int irq, void *dev_id)
393{
394 struct s3cmci_host *host = dev_id;
395 struct mmc_command *cmd;
396 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
397 u32 mci_cclear, mci_dclear;
398 unsigned long iflags;
399
400 spin_lock_irqsave(&host->complete_lock, iflags);
401
402 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
403 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
404 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
405 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
406 mci_imsk = readl(host->base + host->sdiimsk);
407 mci_cclear = 0;
408 mci_dclear = 0;
409
410 if ((host->complete_what == COMPLETION_NONE) ||
411 (host->complete_what == COMPLETION_FINALIZE)) {
412 host->status = "nothing to complete";
413 clear_imask(host);
414 goto irq_out;
415 }
416
417 if (!host->mrq) {
418 host->status = "no active mrq";
419 clear_imask(host);
420 goto irq_out;
421 }
422
423 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
424
425 if (!cmd) {
426 host->status = "no active cmd";
427 clear_imask(host);
428 goto irq_out;
429 }
430
431 if (!host->dodma) {
432 if ((host->pio_active == XFER_WRITE) &&
433 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
434
435 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
436 tasklet_schedule(&host->pio_tasklet);
437 host->status = "pio tx";
438 }
439
440 if ((host->pio_active == XFER_READ) &&
441 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
442
443 disable_imask(host,
444 S3C2410_SDIIMSK_RXFIFOHALF |
445 S3C2410_SDIIMSK_RXFIFOLAST);
446
447 tasklet_schedule(&host->pio_tasklet);
448 host->status = "pio rx";
449 }
450 }
451
452 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
453 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
454 cmd->error = -ETIMEDOUT;
455 host->status = "error: command timeout";
456 goto fail_transfer;
457 }
458
459 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
460 if (host->complete_what == COMPLETION_CMDSENT) {
461 host->status = "ok: command sent";
462 goto close_transfer;
463 }
464
465 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
466 }
467
468 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
469 if (cmd->flags & MMC_RSP_CRC) {
470 if (host->mrq->cmd->flags & MMC_RSP_136) {
471 dbg(host, dbg_irq,
472 "fixup: ignore CRC fail with long rsp\n");
473 } else {
474 /* note, we used to fail the transfer
475 * here, but it seems that this is just
476 * the hardware getting it wrong.
477 *
478 * cmd->error = -EILSEQ;
479 * host->status = "error: bad command crc";
480 * goto fail_transfer;
481 */
482 }
483 }
484
485 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
486 }
487
488 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
489 if (host->complete_what == COMPLETION_RSPFIN) {
490 host->status = "ok: command response received";
491 goto close_transfer;
492 }
493
494 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
495 host->complete_what = COMPLETION_XFERFINISH;
496
497 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
498 }
499
500 /* errors handled after this point are only relevant
501 when a data transfer is in progress */
502
503 if (!cmd->data)
504 goto clear_status_bits;
505
506 /* Check for FIFO failure */
507 if (host->is2440) {
508 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
509 dbg(host, dbg_err, "FIFO failure\n");
510 host->mrq->data->error = -EILSEQ;
511 host->status = "error: 2440 fifo failure";
512 goto fail_transfer;
513 }
514 } else {
515 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
516 dbg(host, dbg_err, "FIFO failure\n");
517 cmd->data->error = -EILSEQ;
518 host->status = "error: fifo failure";
519 goto fail_transfer;
520 }
521 }
522
523 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
524 dbg(host, dbg_err, "bad data crc (outgoing)\n");
525 cmd->data->error = -EILSEQ;
526 host->status = "error: bad data crc (outgoing)";
527 goto fail_transfer;
528 }
529
530 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
531 dbg(host, dbg_err, "bad data crc (incoming)\n");
532 cmd->data->error = -EILSEQ;
533 host->status = "error: bad data crc (incoming)";
534 goto fail_transfer;
535 }
536
537 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
538 dbg(host, dbg_err, "data timeout\n");
539 cmd->data->error = -ETIMEDOUT;
540 host->status = "error: data timeout";
541 goto fail_transfer;
542 }
543
544 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
545 if (host->complete_what == COMPLETION_XFERFINISH) {
546 host->status = "ok: data transfer completed";
547 goto close_transfer;
548 }
549
550 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
551 host->complete_what = COMPLETION_RSPFIN;
552
553 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
554 }
555
556clear_status_bits:
557 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
558 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
559
560 goto irq_out;
561
562fail_transfer:
563 host->pio_active = XFER_NONE;
564
565close_transfer:
566 host->complete_what = COMPLETION_FINALIZE;
567
568 clear_imask(host);
569 tasklet_schedule(&host->pio_tasklet);
570
571 goto irq_out;
572
573irq_out:
574 dbg(host, dbg_irq,
575 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
576 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
577
578 spin_unlock_irqrestore(&host->complete_lock, iflags);
579 return IRQ_HANDLED;
580
581}
582
583/*
584 * ISR for the CardDetect Pin
585*/
586
587static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
588{
589 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
590
591 dbg(host, dbg_irq, "card detect\n");
592
593 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
594
595 return IRQ_HANDLED;
596}
597
598void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch, void *buf_id,
599 int size, enum s3c2410_dma_buffresult result)
600{
601 struct s3cmci_host *host = buf_id;
602 unsigned long iflags;
603 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
604
605 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
606 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
607 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
608 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
609
610 BUG_ON(!host->mrq);
611 BUG_ON(!host->mrq->data);
612 BUG_ON(!host->dmatogo);
613
614 spin_lock_irqsave(&host->complete_lock, iflags);
615
616 if (result != S3C2410_RES_OK) {
617 dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
618 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
619 mci_csta, mci_dsta, mci_fsta,
620 mci_dcnt, result, host->dmatogo);
621
622 goto fail_request;
623 }
624
625 host->dmatogo--;
626 if (host->dmatogo) {
627 dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
628 "DCNT:[%08x] toGo:%u\n",
629 size, mci_dsta, mci_dcnt, host->dmatogo);
630
631 goto out;
632 }
633
634 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
635 size, mci_dsta, mci_dcnt);
636
637 host->complete_what = COMPLETION_FINALIZE;
638
639out:
640 tasklet_schedule(&host->pio_tasklet);
641 spin_unlock_irqrestore(&host->complete_lock, iflags);
642 return;
643
644fail_request:
645 host->mrq->data->error = -EINVAL;
646 host->complete_what = COMPLETION_FINALIZE;
647 writel(0, host->base + host->sdiimsk);
648 goto out;
649
650}
651
652static void finalize_request(struct s3cmci_host *host)
653{
654 struct mmc_request *mrq = host->mrq;
655 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
656 int debug_as_failure = 0;
657
658 if (host->complete_what != COMPLETION_FINALIZE)
659 return;
660
661 if (!mrq)
662 return;
663
664 if (cmd->data && (cmd->error == 0) &&
665 (cmd->data->error == 0)) {
666 if (host->dodma && (!host->dma_complete)) {
667 dbg(host, dbg_dma, "DMA Missing!\n");
668 return;
669 }
670 }
671
672 /* Read response from controller. */
673 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
674 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
675 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
676 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
677
678 writel(host->prescaler, host->base + S3C2410_SDIPRE);
679
680 if (cmd->error)
681 debug_as_failure = 1;
682
683 if (cmd->data && cmd->data->error)
684 debug_as_failure = 1;
685
686 dbg_dumpcmd(host, cmd, debug_as_failure);
687
688 /* Cleanup controller */
689 writel(0, host->base + S3C2410_SDICMDARG);
690 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
691 writel(0, host->base + S3C2410_SDICMDCON);
692 writel(0, host->base + host->sdiimsk);
693
694 if (cmd->data && cmd->error)
695 cmd->data->error = cmd->error;
696
697 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
698 host->cmd_is_stop = 1;
699 s3cmci_send_request(host->mmc);
700 return;
701 }
702
703 /* If we have no data transfer we are finished here */
704 if (!mrq->data)
705 goto request_done;
706
707 /* Calulate the amout of bytes transfer if there was no error */
708 if (mrq->data->error == 0) {
709 mrq->data->bytes_xfered =
710 (mrq->data->blocks * mrq->data->blksz);
711 } else {
712 mrq->data->bytes_xfered = 0;
713 }
714
715 /* If we had an error while transfering data we flush the
716 * DMA channel and the fifo to clear out any garbage. */
717 if (mrq->data->error != 0) {
718 if (host->dodma)
719 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
720
721 if (host->is2440) {
722 /* Clear failure register and reset fifo. */
723 writel(S3C2440_SDIFSTA_FIFORESET |
724 S3C2440_SDIFSTA_FIFOFAIL,
725 host->base + S3C2410_SDIFSTA);
726 } else {
727 u32 mci_con;
728
729 /* reset fifo */
730 mci_con = readl(host->base + S3C2410_SDICON);
731 mci_con |= S3C2410_SDICON_FIFORESET;
732
733 writel(mci_con, host->base + S3C2410_SDICON);
734 }
735 }
736
737request_done:
738 host->complete_what = COMPLETION_NONE;
739 host->mrq = NULL;
740 mmc_request_done(host->mmc, mrq);
741}
742
743
744void s3cmci_dma_setup(struct s3cmci_host *host, enum s3c2410_dmasrc source)
745{
746 static enum s3c2410_dmasrc last_source = -1;
747 static int setup_ok;
748
749 if (last_source == source)
750 return;
751
752 last_source = source;
753
754 s3c2410_dma_devconfig(host->dma, source, 3,
755 host->mem->start + host->sdidata);
756
757 if (!setup_ok) {
758 s3c2410_dma_config(host->dma, 4,
759 (S3C2410_DCON_HWTRIG | S3C2410_DCON_CH0_SDI));
760 s3c2410_dma_set_buffdone_fn(host->dma,
761 s3cmci_dma_done_callback);
762 s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
763 setup_ok = 1;
764 }
765}
766
767static void s3cmci_send_command(struct s3cmci_host *host,
768 struct mmc_command *cmd)
769{
770 u32 ccon, imsk;
771
772 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
773 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
774 S3C2410_SDIIMSK_RESPONSECRC;
775
776 enable_imask(host, imsk);
777
778 if (cmd->data)
779 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
780 else if (cmd->flags & MMC_RSP_PRESENT)
781 host->complete_what = COMPLETION_RSPFIN;
782 else
783 host->complete_what = COMPLETION_CMDSENT;
784
785 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
786
787 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
788 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
789
790 if (cmd->flags & MMC_RSP_PRESENT)
791 ccon |= S3C2410_SDICMDCON_WAITRSP;
792
793 if (cmd->flags & MMC_RSP_136)
794 ccon |= S3C2410_SDICMDCON_LONGRSP;
795
796 writel(ccon, host->base + S3C2410_SDICMDCON);
797}
798
799static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
800{
801 u32 dcon, imsk, stoptries = 3;
802
803 /* write DCON register */
804
805 if (!data) {
806 writel(0, host->base + S3C2410_SDIDCON);
807 return 0;
808 }
809
810 if ((data->blksz & 3) != 0) {
811 /* We cannot deal with unaligned blocks with more than
812 * one block being transfered. */
813
814 if (data->blocks > 1)
815 return -EINVAL;
816
817 /* No support yet for non-word block transfers. */
818 return -EINVAL;
819 }
820
821 while (readl(host->base + S3C2410_SDIDSTA) &
822 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
823
824 dbg(host, dbg_err,
825 "mci_setup_data() transfer stillin progress.\n");
826
827 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
828 s3cmci_reset(host);
829
830 if ((stoptries--) == 0) {
831 dbg_dumpregs(host, "DRF");
832 return -EINVAL;
833 }
834 }
835
836 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
837
838 if (host->dodma)
839 dcon |= S3C2410_SDIDCON_DMAEN;
840
841 if (host->bus_width == MMC_BUS_WIDTH_4)
842 dcon |= S3C2410_SDIDCON_WIDEBUS;
843
844 if (!(data->flags & MMC_DATA_STREAM))
845 dcon |= S3C2410_SDIDCON_BLOCKMODE;
846
847 if (data->flags & MMC_DATA_WRITE) {
848 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
849 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
850 }
851
852 if (data->flags & MMC_DATA_READ) {
853 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
854 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
855 }
856
857 if (host->is2440) {
858 dcon |= S3C2440_SDIDCON_DS_WORD;
859 dcon |= S3C2440_SDIDCON_DATSTART;
860 }
861
862 writel(dcon, host->base + S3C2410_SDIDCON);
863
864 /* write BSIZE register */
865
866 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
867
868 /* add to IMASK register */
869 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
870 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
871
872 enable_imask(host, imsk);
873
874 /* write TIMER register */
875
876 if (host->is2440) {
877 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
878 } else {
879 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
880
881 /* FIX: set slow clock to prevent timeouts on read */
882 if (data->flags & MMC_DATA_READ)
883 writel(0xFF, host->base + S3C2410_SDIPRE);
884 }
885
886 return 0;
887}
888
889#define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
890
891static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
892{
893 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
894
895 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
896
897 host->pio_sgptr = 0;
898 host->pio_words = 0;
899 host->pio_count = 0;
900 host->pio_active = rw ? XFER_WRITE : XFER_READ;
901
902 if (rw) {
903 do_pio_write(host);
904 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
905 } else {
906 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
907 | S3C2410_SDIIMSK_RXFIFOLAST);
908 }
909
910 return 0;
911}
912
913static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
914{
915 int dma_len, i;
916 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
917
918 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
919
920 s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
921 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
922
923 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
924 (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
925
926 if (dma_len == 0)
927 return -ENOMEM;
928
929 host->dma_complete = 0;
930 host->dmatogo = dma_len;
931
932 for (i = 0; i < dma_len; i++) {
933 int res;
934
935 dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
936 sg_dma_address(&data->sg[i]),
937 sg_dma_len(&data->sg[i]));
938
939 res = s3c2410_dma_enqueue(host->dma, (void *) host,
940 sg_dma_address(&data->sg[i]),
941 sg_dma_len(&data->sg[i]));
942
943 if (res) {
944 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
945 return -EBUSY;
946 }
947 }
948
949 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
950
951 return 0;
952}
953
954static void s3cmci_send_request(struct mmc_host *mmc)
955{
956 struct s3cmci_host *host = mmc_priv(mmc);
957 struct mmc_request *mrq = host->mrq;
958 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
959
960 host->ccnt++;
961 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
962
963 /* Clear command, data and fifo status registers
964 Fifo clear only necessary on 2440, but doesn't hurt on 2410
965 */
966 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
967 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
968 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
969
970 if (cmd->data) {
971 int res = s3cmci_setup_data(host, cmd->data);
972
973 host->dcnt++;
974
975 if (res) {
976 dbg(host, dbg_err, "setup data error %d\n", res);
977 cmd->error = res;
978 cmd->data->error = res;
979
980 mmc_request_done(mmc, mrq);
981 return;
982 }
983
984 if (host->dodma)
985 res = s3cmci_prepare_dma(host, cmd->data);
986 else
987 res = s3cmci_prepare_pio(host, cmd->data);
988
989 if (res) {
990 dbg(host, dbg_err, "data prepare error %d\n", res);
991 cmd->error = res;
992 cmd->data->error = res;
993
994 mmc_request_done(mmc, mrq);
995 return;
996 }
997 }
998
999 /* Send command */
1000 s3cmci_send_command(host, cmd);
1001
1002 /* Enable Interrupt */
1003 enable_irq(host->irq);
1004}
1005
1006static int s3cmci_card_present(struct s3cmci_host *host)
1007{
1008 struct s3c24xx_mci_pdata *pdata = host->pdata;
1009 int ret;
1010
1011 if (pdata->gpio_detect == 0)
1012 return -ENOSYS;
1013
1014 ret = s3c2410_gpio_getpin(pdata->gpio_detect) ? 0 : 1;
1015 return ret ^ pdata->detect_invert;
1016}
1017
1018static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1019{
1020 struct s3cmci_host *host = mmc_priv(mmc);
1021
1022 host->status = "mmc request";
1023 host->cmd_is_stop = 0;
1024 host->mrq = mrq;
1025
1026 if (s3cmci_card_present(host) == 0) {
1027 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1028 host->mrq->cmd->error = -ENOMEDIUM;
1029 mmc_request_done(mmc, mrq);
1030 } else
1031 s3cmci_send_request(mmc);
1032}
1033
1034static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1035{
1036 struct s3cmci_host *host = mmc_priv(mmc);
1037 u32 mci_psc, mci_con;
1038
1039 /* Set the power state */
1040
1041 mci_con = readl(host->base + S3C2410_SDICON);
1042
1043 switch (ios->power_mode) {
1044 case MMC_POWER_ON:
1045 case MMC_POWER_UP:
1046 s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_SDCLK);
1047 s3c2410_gpio_cfgpin(S3C2410_GPE6, S3C2410_GPE6_SDCMD);
1048 s3c2410_gpio_cfgpin(S3C2410_GPE7, S3C2410_GPE7_SDDAT0);
1049 s3c2410_gpio_cfgpin(S3C2410_GPE8, S3C2410_GPE8_SDDAT1);
1050 s3c2410_gpio_cfgpin(S3C2410_GPE9, S3C2410_GPE9_SDDAT2);
1051 s3c2410_gpio_cfgpin(S3C2410_GPE10, S3C2410_GPE10_SDDAT3);
1052
1053 if (host->pdata->set_power)
1054 host->pdata->set_power(ios->power_mode, ios->vdd);
1055
1056 if (!host->is2440)
1057 mci_con |= S3C2410_SDICON_FIFORESET;
1058
1059 break;
1060
1061 case MMC_POWER_OFF:
1062 default:
1063 s3c2410_gpio_setpin(S3C2410_GPE5, 0);
1064 s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_OUTP);
1065
1066 if (host->is2440)
1067 mci_con |= S3C2440_SDICON_SDRESET;
1068
1069 if (host->pdata->set_power)
1070 host->pdata->set_power(ios->power_mode, ios->vdd);
1071
1072 break;
1073 }
1074
1075 /* Set clock */
1076 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1077 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1078
1079 if (host->real_rate <= ios->clock)
1080 break;
1081 }
1082
1083 if (mci_psc > 255)
1084 mci_psc = 255;
1085
1086 host->prescaler = mci_psc;
1087 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1088
1089 /* If requested clock is 0, real_rate will be 0, too */
1090 if (ios->clock == 0)
1091 host->real_rate = 0;
1092
1093 /* Set CLOCK_ENABLE */
1094 if (ios->clock)
1095 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1096 else
1097 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1098
1099 writel(mci_con, host->base + S3C2410_SDICON);
1100
1101 if ((ios->power_mode == MMC_POWER_ON) ||
1102 (ios->power_mode == MMC_POWER_UP)) {
1103 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1104 host->real_rate/1000, ios->clock/1000);
1105 } else {
1106 dbg(host, dbg_conf, "powered down.\n");
1107 }
1108
1109 host->bus_width = ios->bus_width;
1110}
1111
1112static void s3cmci_reset(struct s3cmci_host *host)
1113{
1114 u32 con = readl(host->base + S3C2410_SDICON);
1115
1116 con |= S3C2440_SDICON_SDRESET;
1117 writel(con, host->base + S3C2410_SDICON);
1118}
1119
1120static int s3cmci_get_ro(struct mmc_host *mmc)
1121{
1122 struct s3cmci_host *host = mmc_priv(mmc);
1123 struct s3c24xx_mci_pdata *pdata = host->pdata;
1124 int ret;
1125
1126 if (pdata->gpio_wprotect == 0)
1127 return 0;
1128
1129 ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
1130
1131 if (pdata->wprotect_invert)
1132 ret = !ret;
1133
1134 return ret;
1135}
1136
1137static struct mmc_host_ops s3cmci_ops = {
1138 .request = s3cmci_request,
1139 .set_ios = s3cmci_set_ios,
1140 .get_ro = s3cmci_get_ro,
1141};
1142
1143static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1144 /* This is currently here to avoid a number of if (host->pdata)
1145 * checks. Any zero fields to ensure reaonable defaults are picked. */
1146};
1147
1148static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
1149{
1150 struct s3cmci_host *host;
1151 struct mmc_host *mmc;
1152 int ret;
1153
1154 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1155 if (!mmc) {
1156 ret = -ENOMEM;
1157 goto probe_out;
1158 }
1159
1160 host = mmc_priv(mmc);
1161 host->mmc = mmc;
1162 host->pdev = pdev;
1163 host->is2440 = is2440;
1164
1165 host->pdata = pdev->dev.platform_data;
1166 if (!host->pdata) {
1167 pdev->dev.platform_data = &s3cmci_def_pdata;
1168 host->pdata = &s3cmci_def_pdata;
1169 }
1170
1171 spin_lock_init(&host->complete_lock);
1172 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1173
1174 if (is2440) {
1175 host->sdiimsk = S3C2440_SDIIMSK;
1176 host->sdidata = S3C2440_SDIDATA;
1177 host->clk_div = 1;
1178 } else {
1179 host->sdiimsk = S3C2410_SDIIMSK;
1180 host->sdidata = S3C2410_SDIDATA;
1181 host->clk_div = 2;
1182 }
1183
1184 host->dodma = 0;
1185 host->complete_what = COMPLETION_NONE;
1186 host->pio_active = XFER_NONE;
1187
1188 host->dma = S3CMCI_DMA;
1189
1190 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1191 if (!host->mem) {
1192 dev_err(&pdev->dev,
1193 "failed to get io memory region resouce.\n");
1194
1195 ret = -ENOENT;
1196 goto probe_free_host;
1197 }
1198
1199 host->mem = request_mem_region(host->mem->start,
1200 RESSIZE(host->mem), pdev->name);
1201
1202 if (!host->mem) {
1203 dev_err(&pdev->dev, "failed to request io memory region.\n");
1204 ret = -ENOENT;
1205 goto probe_free_host;
1206 }
1207
1208 host->base = ioremap(host->mem->start, RESSIZE(host->mem));
1209 if (host->base == 0) {
1210 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1211 ret = -EINVAL;
1212 goto probe_free_mem_region;
1213 }
1214
1215 host->irq = platform_get_irq(pdev, 0);
1216 if (host->irq == 0) {
1217 dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
1218 ret = -EINVAL;
1219 goto probe_iounmap;
1220 }
1221
1222 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1223 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1224 ret = -ENOENT;
1225 goto probe_iounmap;
1226 }
1227
1228 /* We get spurious interrupts even when we have set the IMSK
1229 * register to ignore everything, so use disable_irq() to make
1230 * ensure we don't lock the system with un-serviceable requests. */
1231
1232 disable_irq(host->irq);
1233
1234 host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
1235
1236 if (host->irq_cd >= 0) {
1237 if (request_irq(host->irq_cd, s3cmci_irq_cd,
1238 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1239 DRIVER_NAME, host)) {
1240 dev_err(&pdev->dev, "can't get card detect irq.\n");
1241 ret = -ENOENT;
1242 goto probe_free_irq;
1243 }
1244 } else {
1245 dev_warn(&pdev->dev, "host detect has no irq available\n");
1246 s3c2410_gpio_cfgpin(host->pdata->gpio_detect,
1247 S3C2410_GPIO_INPUT);
1248 }
1249
1250 if (host->pdata->gpio_wprotect)
1251 s3c2410_gpio_cfgpin(host->pdata->gpio_wprotect,
1252 S3C2410_GPIO_INPUT);
1253
1254 if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
1255 dev_err(&pdev->dev, "unable to get DMA channel.\n");
1256 ret = -EBUSY;
1257 goto probe_free_irq_cd;
1258 }
1259
1260 host->clk = clk_get(&pdev->dev, "sdi");
1261 if (IS_ERR(host->clk)) {
1262 dev_err(&pdev->dev, "failed to find clock source.\n");
1263 ret = PTR_ERR(host->clk);
1264 host->clk = NULL;
1265 goto probe_free_host;
1266 }
1267
1268 ret = clk_enable(host->clk);
1269 if (ret) {
1270 dev_err(&pdev->dev, "failed to enable clock source.\n");
1271 goto clk_free;
1272 }
1273
1274 host->clk_rate = clk_get_rate(host->clk);
1275
1276 mmc->ops = &s3cmci_ops;
1277 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1278 mmc->caps = MMC_CAP_4_BIT_DATA;
1279 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1280 mmc->f_max = host->clk_rate / host->clk_div;
1281
1282 if (host->pdata->ocr_avail)
1283 mmc->ocr_avail = host->pdata->ocr_avail;
1284
1285 mmc->max_blk_count = 4095;
1286 mmc->max_blk_size = 4095;
1287 mmc->max_req_size = 4095 * 512;
1288 mmc->max_seg_size = mmc->max_req_size;
1289
1290 mmc->max_phys_segs = 128;
1291 mmc->max_hw_segs = 128;
1292
1293 dbg(host, dbg_debug,
1294 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1295 (host->is2440?"2440":""),
1296 host->base, host->irq, host->irq_cd, host->dma);
1297
1298 ret = mmc_add_host(mmc);
1299 if (ret) {
1300 dev_err(&pdev->dev, "failed to add mmc host.\n");
1301 goto free_dmabuf;
1302 }
1303
1304 platform_set_drvdata(pdev, mmc);
1305 dev_info(&pdev->dev, "initialisation done.\n");
1306
1307 return 0;
1308
1309 free_dmabuf:
1310 clk_disable(host->clk);
1311
1312 clk_free:
1313 clk_put(host->clk);
1314
1315 probe_free_irq_cd:
1316 if (host->irq_cd >= 0)
1317 free_irq(host->irq_cd, host);
1318
1319 probe_free_irq:
1320 free_irq(host->irq, host);
1321
1322 probe_iounmap:
1323 iounmap(host->base);
1324
1325 probe_free_mem_region:
1326 release_mem_region(host->mem->start, RESSIZE(host->mem));
1327
1328 probe_free_host:
1329 mmc_free_host(mmc);
1330 probe_out:
1331 return ret;
1332}
1333
1334static int __devexit s3cmci_remove(struct platform_device *pdev)
1335{
1336 struct mmc_host *mmc = platform_get_drvdata(pdev);
1337 struct s3cmci_host *host = mmc_priv(mmc);
1338
1339 mmc_remove_host(mmc);
1340
1341 clk_disable(host->clk);
1342 clk_put(host->clk);
1343
1344 tasklet_disable(&host->pio_tasklet);
1345 s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);
1346
1347 if (host->irq_cd >= 0)
1348 free_irq(host->irq_cd, host);
1349 free_irq(host->irq, host);
1350
1351 iounmap(host->base);
1352 release_mem_region(host->mem->start, RESSIZE(host->mem));
1353
1354 mmc_free_host(mmc);
1355 return 0;
1356}
1357
1358static int __devinit s3cmci_probe_2410(struct platform_device *dev)
1359{
1360 return s3cmci_probe(dev, 0);
1361}
1362
1363static int __devinit s3cmci_probe_2412(struct platform_device *dev)
1364{
1365 return s3cmci_probe(dev, 1);
1366}
1367
1368static int __devinit s3cmci_probe_2440(struct platform_device *dev)
1369{
1370 return s3cmci_probe(dev, 1);
1371}
1372
1373#ifdef CONFIG_PM
1374
1375static int s3cmci_suspend(struct platform_device *dev, pm_message_t state)
1376{
1377 struct mmc_host *mmc = platform_get_drvdata(dev);
1378
1379 return mmc_suspend_host(mmc, state);
1380}
1381
1382static int s3cmci_resume(struct platform_device *dev)
1383{
1384 struct mmc_host *mmc = platform_get_drvdata(dev);
1385
1386 return mmc_resume_host(mmc);
1387}
1388
1389#else /* CONFIG_PM */
1390#define s3cmci_suspend NULL
1391#define s3cmci_resume NULL
1392#endif /* CONFIG_PM */
1393
1394
1395static struct platform_driver s3cmci_driver_2410 = {
1396 .driver.name = "s3c2410-sdi",
1397 .driver.owner = THIS_MODULE,
1398 .probe = s3cmci_probe_2410,
1399 .remove = __devexit_p(s3cmci_remove),
1400 .suspend = s3cmci_suspend,
1401 .resume = s3cmci_resume,
1402};
1403
1404static struct platform_driver s3cmci_driver_2412 = {
1405 .driver.name = "s3c2412-sdi",
1406 .driver.owner = THIS_MODULE,
1407 .probe = s3cmci_probe_2412,
1408 .remove = __devexit_p(s3cmci_remove),
1409 .suspend = s3cmci_suspend,
1410 .resume = s3cmci_resume,
1411};
1412
1413static struct platform_driver s3cmci_driver_2440 = {
1414 .driver.name = "s3c2440-sdi",
1415 .driver.owner = THIS_MODULE,
1416 .probe = s3cmci_probe_2440,
1417 .remove = __devexit_p(s3cmci_remove),
1418 .suspend = s3cmci_suspend,
1419 .resume = s3cmci_resume,
1420};
1421
1422
1423static int __init s3cmci_init(void)
1424{
1425 platform_driver_register(&s3cmci_driver_2410);
1426 platform_driver_register(&s3cmci_driver_2412);
1427 platform_driver_register(&s3cmci_driver_2440);
1428 return 0;
1429}
1430
1431static void __exit s3cmci_exit(void)
1432{
1433 platform_driver_unregister(&s3cmci_driver_2410);
1434 platform_driver_unregister(&s3cmci_driver_2412);
1435 platform_driver_unregister(&s3cmci_driver_2440);
1436}
1437
1438module_init(s3cmci_init);
1439module_exit(s3cmci_exit);
1440
1441MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1442MODULE_LICENSE("GPL v2");
1443MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>");
1444MODULE_ALIAS("platform:s3c2410-sdi");
1445MODULE_ALIAS("platform:s3c2412-sdi");
1446MODULE_ALIAS("platform:s3c2440-sdi");
diff --git a/drivers/mmc/host/s3cmci.h b/drivers/mmc/host/s3cmci.h
new file mode 100644
index 000000000000..37d9c60010c9
--- /dev/null
+++ b/drivers/mmc/host/s3cmci.h
@@ -0,0 +1,70 @@
1/*
2 * linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
3 *
4 * Copyright (C) 2004-2006 Thomas Kleffel, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11/* FIXME: DMA Resource management ?! */
12#define S3CMCI_DMA 0
13
14enum s3cmci_waitfor {
15 COMPLETION_NONE,
16 COMPLETION_FINALIZE,
17 COMPLETION_CMDSENT,
18 COMPLETION_RSPFIN,
19 COMPLETION_XFERFINISH,
20 COMPLETION_XFERFINISH_RSPFIN,
21};
22
23struct s3cmci_host {
24 struct platform_device *pdev;
25 struct s3c24xx_mci_pdata *pdata;
26 struct mmc_host *mmc;
27 struct resource *mem;
28 struct clk *clk;
29 void __iomem *base;
30 int irq;
31 int irq_cd;
32 int dma;
33
34 unsigned long clk_rate;
35 unsigned long clk_div;
36 unsigned long real_rate;
37 u8 prescaler;
38
39 int is2440;
40 unsigned sdiimsk;
41 unsigned sdidata;
42 int dodma;
43 int dmatogo;
44
45 struct mmc_request *mrq;
46 int cmd_is_stop;
47
48 spinlock_t complete_lock;
49 enum s3cmci_waitfor complete_what;
50
51 int dma_complete;
52
53 u32 pio_sgptr;
54 u32 pio_words;
55 u32 pio_count;
56 u32 *pio_ptr;
57#define XFER_NONE 0
58#define XFER_READ 1
59#define XFER_WRITE 2
60 u32 pio_active;
61
62 int bus_width;
63
64 char dbgmsg_cmd[301];
65 char dbgmsg_dat[301];
66 char *status;
67
68 unsigned int ccnt, dcnt;
69 struct tasklet_struct pio_tasklet;
70};
diff --git a/drivers/mmc/host/sdhci-pci.c b/drivers/mmc/host/sdhci-pci.c
new file mode 100644
index 000000000000..deb607c52c0d
--- /dev/null
+++ b/drivers/mmc/host/sdhci-pci.c
@@ -0,0 +1,732 @@
1/* linux/drivers/mmc/host/sdhci-pci.c - SDHCI on PCI bus interface
2 *
3 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * Thanks to the following companies for their support:
11 *
12 * - JMicron (hardware and technical support)
13 */
14
15#include <linux/delay.h>
16#include <linux/highmem.h>
17#include <linux/pci.h>
18#include <linux/dma-mapping.h>
19
20#include <linux/mmc/host.h>
21
22#include <asm/scatterlist.h>
23#include <asm/io.h>
24
25#include "sdhci.h"
26
27/*
28 * PCI registers
29 */
30
31#define PCI_SDHCI_IFPIO 0x00
32#define PCI_SDHCI_IFDMA 0x01
33#define PCI_SDHCI_IFVENDOR 0x02
34
35#define PCI_SLOT_INFO 0x40 /* 8 bits */
36#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
37#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
38
39#define MAX_SLOTS 8
40
41struct sdhci_pci_chip;
42struct sdhci_pci_slot;
43
44struct sdhci_pci_fixes {
45 unsigned int quirks;
46
47 int (*probe)(struct sdhci_pci_chip*);
48
49 int (*probe_slot)(struct sdhci_pci_slot*);
50 void (*remove_slot)(struct sdhci_pci_slot*, int);
51
52 int (*suspend)(struct sdhci_pci_chip*,
53 pm_message_t);
54 int (*resume)(struct sdhci_pci_chip*);
55};
56
57struct sdhci_pci_slot {
58 struct sdhci_pci_chip *chip;
59 struct sdhci_host *host;
60
61 int pci_bar;
62};
63
64struct sdhci_pci_chip {
65 struct pci_dev *pdev;
66
67 unsigned int quirks;
68 const struct sdhci_pci_fixes *fixes;
69
70 int num_slots; /* Slots on controller */
71 struct sdhci_pci_slot *slots[MAX_SLOTS]; /* Pointers to host slots */
72};
73
74
75/*****************************************************************************\
76 * *
77 * Hardware specific quirk handling *
78 * *
79\*****************************************************************************/
80
81static int ricoh_probe(struct sdhci_pci_chip *chip)
82{
83 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
84 chip->quirks |= SDHCI_QUIRK_CLOCK_BEFORE_RESET;
85
86 if (chip->pdev->subsystem_vendor == PCI_VENDOR_ID_SAMSUNG)
87 chip->quirks |= SDHCI_QUIRK_NO_CARD_NO_RESET;
88
89 return 0;
90}
91
92static const struct sdhci_pci_fixes sdhci_ricoh = {
93 .probe = ricoh_probe,
94 .quirks = SDHCI_QUIRK_32BIT_DMA_ADDR,
95};
96
97static const struct sdhci_pci_fixes sdhci_ene_712 = {
98 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
99 SDHCI_QUIRK_BROKEN_DMA,
100};
101
102static const struct sdhci_pci_fixes sdhci_ene_714 = {
103 .quirks = SDHCI_QUIRK_SINGLE_POWER_WRITE |
104 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
105 SDHCI_QUIRK_BROKEN_DMA,
106};
107
108static const struct sdhci_pci_fixes sdhci_cafe = {
109 .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
110 SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
111};
112
113static int jmicron_pmos(struct sdhci_pci_chip *chip, int on)
114{
115 u8 scratch;
116 int ret;
117
118 ret = pci_read_config_byte(chip->pdev, 0xAE, &scratch);
119 if (ret)
120 return ret;
121
122 /*
123 * Turn PMOS on [bit 0], set over current detection to 2.4 V
124 * [bit 1:2] and enable over current debouncing [bit 6].
125 */
126 if (on)
127 scratch |= 0x47;
128 else
129 scratch &= ~0x47;
130
131 ret = pci_write_config_byte(chip->pdev, 0xAE, scratch);
132 if (ret)
133 return ret;
134
135 return 0;
136}
137
138static int jmicron_probe(struct sdhci_pci_chip *chip)
139{
140 int ret;
141
142 if (chip->pdev->revision == 0) {
143 chip->quirks |= SDHCI_QUIRK_32BIT_DMA_ADDR |
144 SDHCI_QUIRK_32BIT_DMA_SIZE |
145 SDHCI_QUIRK_32BIT_ADMA_SIZE |
146 SDHCI_QUIRK_RESET_AFTER_REQUEST;
147 }
148
149 /*
150 * JMicron chips can have two interfaces to the same hardware
151 * in order to work around limitations in Microsoft's driver.
152 * We need to make sure we only bind to one of them.
153 *
154 * This code assumes two things:
155 *
156 * 1. The PCI code adds subfunctions in order.
157 *
158 * 2. The MMC interface has a lower subfunction number
159 * than the SD interface.
160 */
161 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_SD) {
162 struct pci_dev *sd_dev;
163
164 sd_dev = NULL;
165 while ((sd_dev = pci_get_device(PCI_VENDOR_ID_JMICRON,
166 PCI_DEVICE_ID_JMICRON_JMB38X_MMC, sd_dev)) != NULL) {
167 if ((PCI_SLOT(chip->pdev->devfn) ==
168 PCI_SLOT(sd_dev->devfn)) &&
169 (chip->pdev->bus == sd_dev->bus))
170 break;
171 }
172
173 if (sd_dev) {
174 pci_dev_put(sd_dev);
175 dev_info(&chip->pdev->dev, "Refusing to bind to "
176 "secondary interface.\n");
177 return -ENODEV;
178 }
179 }
180
181 /*
182 * JMicron chips need a bit of a nudge to enable the power
183 * output pins.
184 */
185 ret = jmicron_pmos(chip, 1);
186 if (ret) {
187 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
188 return ret;
189 }
190
191 return 0;
192}
193
194static void jmicron_enable_mmc(struct sdhci_host *host, int on)
195{
196 u8 scratch;
197
198 scratch = readb(host->ioaddr + 0xC0);
199
200 if (on)
201 scratch |= 0x01;
202 else
203 scratch &= ~0x01;
204
205 writeb(scratch, host->ioaddr + 0xC0);
206}
207
208static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
209{
210 if (slot->chip->pdev->revision == 0) {
211 u16 version;
212
213 version = readl(slot->host->ioaddr + SDHCI_HOST_VERSION);
214 version = (version & SDHCI_VENDOR_VER_MASK) >>
215 SDHCI_VENDOR_VER_SHIFT;
216
217 /*
218 * Older versions of the chip have lots of nasty glitches
219 * in the ADMA engine. It's best just to avoid it
220 * completely.
221 */
222 if (version < 0xAC)
223 slot->host->quirks |= SDHCI_QUIRK_BROKEN_ADMA;
224 }
225
226 /*
227 * The secondary interface requires a bit set to get the
228 * interrupts.
229 */
230 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
231 jmicron_enable_mmc(slot->host, 1);
232
233 return 0;
234}
235
236static void jmicron_remove_slot(struct sdhci_pci_slot *slot, int dead)
237{
238 if (dead)
239 return;
240
241 if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC)
242 jmicron_enable_mmc(slot->host, 0);
243}
244
245static int jmicron_suspend(struct sdhci_pci_chip *chip, pm_message_t state)
246{
247 int i;
248
249 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
250 for (i = 0;i < chip->num_slots;i++)
251 jmicron_enable_mmc(chip->slots[i]->host, 0);
252 }
253
254 return 0;
255}
256
257static int jmicron_resume(struct sdhci_pci_chip *chip)
258{
259 int ret, i;
260
261 if (chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB38X_MMC) {
262 for (i = 0;i < chip->num_slots;i++)
263 jmicron_enable_mmc(chip->slots[i]->host, 1);
264 }
265
266 ret = jmicron_pmos(chip, 1);
267 if (ret) {
268 dev_err(&chip->pdev->dev, "Failure enabling card power\n");
269 return ret;
270 }
271
272 return 0;
273}
274
275static const struct sdhci_pci_fixes sdhci_jmicron = {
276 .probe = jmicron_probe,
277
278 .probe_slot = jmicron_probe_slot,
279 .remove_slot = jmicron_remove_slot,
280
281 .suspend = jmicron_suspend,
282 .resume = jmicron_resume,
283};
284
285static const struct pci_device_id pci_ids[] __devinitdata = {
286 {
287 .vendor = PCI_VENDOR_ID_RICOH,
288 .device = PCI_DEVICE_ID_RICOH_R5C822,
289 .subvendor = PCI_ANY_ID,
290 .subdevice = PCI_ANY_ID,
291 .driver_data = (kernel_ulong_t)&sdhci_ricoh,
292 },
293
294 {
295 .vendor = PCI_VENDOR_ID_ENE,
296 .device = PCI_DEVICE_ID_ENE_CB712_SD,
297 .subvendor = PCI_ANY_ID,
298 .subdevice = PCI_ANY_ID,
299 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
300 },
301
302 {
303 .vendor = PCI_VENDOR_ID_ENE,
304 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
305 .subvendor = PCI_ANY_ID,
306 .subdevice = PCI_ANY_ID,
307 .driver_data = (kernel_ulong_t)&sdhci_ene_712,
308 },
309
310 {
311 .vendor = PCI_VENDOR_ID_ENE,
312 .device = PCI_DEVICE_ID_ENE_CB714_SD,
313 .subvendor = PCI_ANY_ID,
314 .subdevice = PCI_ANY_ID,
315 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
316 },
317
318 {
319 .vendor = PCI_VENDOR_ID_ENE,
320 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
321 .subvendor = PCI_ANY_ID,
322 .subdevice = PCI_ANY_ID,
323 .driver_data = (kernel_ulong_t)&sdhci_ene_714,
324 },
325
326 {
327 .vendor = PCI_VENDOR_ID_MARVELL,
328 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
329 .subvendor = PCI_ANY_ID,
330 .subdevice = PCI_ANY_ID,
331 .driver_data = (kernel_ulong_t)&sdhci_cafe,
332 },
333
334 {
335 .vendor = PCI_VENDOR_ID_JMICRON,
336 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
337 .subvendor = PCI_ANY_ID,
338 .subdevice = PCI_ANY_ID,
339 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
340 },
341
342 {
343 .vendor = PCI_VENDOR_ID_JMICRON,
344 .device = PCI_DEVICE_ID_JMICRON_JMB38X_MMC,
345 .subvendor = PCI_ANY_ID,
346 .subdevice = PCI_ANY_ID,
347 .driver_data = (kernel_ulong_t)&sdhci_jmicron,
348 },
349
350 { /* Generic SD host controller */
351 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
352 },
353
354 { /* end: all zeroes */ },
355};
356
357MODULE_DEVICE_TABLE(pci, pci_ids);
358
359/*****************************************************************************\
360 * *
361 * SDHCI core callbacks *
362 * *
363\*****************************************************************************/
364
365static int sdhci_pci_enable_dma(struct sdhci_host *host)
366{
367 struct sdhci_pci_slot *slot;
368 struct pci_dev *pdev;
369 int ret;
370
371 slot = sdhci_priv(host);
372 pdev = slot->chip->pdev;
373
374 if (((pdev->class & 0xFFFF00) == (PCI_CLASS_SYSTEM_SDHCI << 8)) &&
375 ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
376 (host->flags & SDHCI_USE_DMA)) {
377 dev_warn(&pdev->dev, "Will use DMA mode even though HW "
378 "doesn't fully claim to support it.\n");
379 }
380
381 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
382 if (ret)
383 return ret;
384
385 pci_set_master(pdev);
386
387 return 0;
388}
389
390static struct sdhci_ops sdhci_pci_ops = {
391 .enable_dma = sdhci_pci_enable_dma,
392};
393
394/*****************************************************************************\
395 * *
396 * Suspend/resume *
397 * *
398\*****************************************************************************/
399
400#ifdef CONFIG_PM
401
402static int sdhci_pci_suspend (struct pci_dev *pdev, pm_message_t state)
403{
404 struct sdhci_pci_chip *chip;
405 struct sdhci_pci_slot *slot;
406 int i, ret;
407
408 chip = pci_get_drvdata(pdev);
409 if (!chip)
410 return 0;
411
412 for (i = 0;i < chip->num_slots;i++) {
413 slot = chip->slots[i];
414 if (!slot)
415 continue;
416
417 ret = sdhci_suspend_host(slot->host, state);
418
419 if (ret) {
420 for (i--;i >= 0;i--)
421 sdhci_resume_host(chip->slots[i]->host);
422 return ret;
423 }
424 }
425
426 if (chip->fixes && chip->fixes->suspend) {
427 ret = chip->fixes->suspend(chip, state);
428 if (ret) {
429 for (i = chip->num_slots - 1;i >= 0;i--)
430 sdhci_resume_host(chip->slots[i]->host);
431 return ret;
432 }
433 }
434
435 pci_save_state(pdev);
436 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
437 pci_disable_device(pdev);
438 pci_set_power_state(pdev, pci_choose_state(pdev, state));
439
440 return 0;
441}
442
443static int sdhci_pci_resume (struct pci_dev *pdev)
444{
445 struct sdhci_pci_chip *chip;
446 struct sdhci_pci_slot *slot;
447 int i, ret;
448
449 chip = pci_get_drvdata(pdev);
450 if (!chip)
451 return 0;
452
453 pci_set_power_state(pdev, PCI_D0);
454 pci_restore_state(pdev);
455 ret = pci_enable_device(pdev);
456 if (ret)
457 return ret;
458
459 if (chip->fixes && chip->fixes->resume) {
460 ret = chip->fixes->resume(chip);
461 if (ret)
462 return ret;
463 }
464
465 for (i = 0;i < chip->num_slots;i++) {
466 slot = chip->slots[i];
467 if (!slot)
468 continue;
469
470 ret = sdhci_resume_host(slot->host);
471 if (ret)
472 return ret;
473 }
474
475 return 0;
476}
477
478#else /* CONFIG_PM */
479
480#define sdhci_pci_suspend NULL
481#define sdhci_pci_resume NULL
482
483#endif /* CONFIG_PM */
484
485/*****************************************************************************\
486 * *
487 * Device probing/removal *
488 * *
489\*****************************************************************************/
490
491static struct sdhci_pci_slot * __devinit sdhci_pci_probe_slot(
492 struct pci_dev *pdev, struct sdhci_pci_chip *chip, int bar)
493{
494 struct sdhci_pci_slot *slot;
495 struct sdhci_host *host;
496
497 resource_size_t addr;
498
499 int ret;
500
501 if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
502 dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
503 return ERR_PTR(-ENODEV);
504 }
505
506 if (pci_resource_len(pdev, bar) != 0x100) {
507 dev_err(&pdev->dev, "Invalid iomem size. You may "
508 "experience problems.\n");
509 }
510
511 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
512 dev_err(&pdev->dev, "Vendor specific interface. Aborting.\n");
513 return ERR_PTR(-ENODEV);
514 }
515
516 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
517 dev_err(&pdev->dev, "Unknown interface. Aborting.\n");
518 return ERR_PTR(-ENODEV);
519 }
520
521 host = sdhci_alloc_host(&pdev->dev, sizeof(struct sdhci_pci_slot));
522 if (IS_ERR(host)) {
523 ret = PTR_ERR(host);
524 goto unmap;
525 }
526
527 slot = sdhci_priv(host);
528
529 slot->chip = chip;
530 slot->host = host;
531 slot->pci_bar = bar;
532
533 host->hw_name = "PCI";
534 host->ops = &sdhci_pci_ops;
535 host->quirks = chip->quirks;
536
537 host->irq = pdev->irq;
538
539 ret = pci_request_region(pdev, bar, mmc_hostname(host->mmc));
540 if (ret) {
541 dev_err(&pdev->dev, "cannot request region\n");
542 return ERR_PTR(ret);
543 }
544
545 addr = pci_resource_start(pdev, bar);
546 host->ioaddr = ioremap_nocache(addr, pci_resource_len(pdev, bar));
547 if (!host->ioaddr) {
548 dev_err(&pdev->dev, "failed to remap registers\n");
549 goto release;
550 }
551
552 if (chip->fixes && chip->fixes->probe_slot) {
553 ret = chip->fixes->probe_slot(slot);
554 if (ret)
555 goto unmap;
556 }
557
558 ret = sdhci_add_host(host);
559 if (ret)
560 goto remove;
561
562 return slot;
563
564remove:
565 if (chip->fixes && chip->fixes->remove_slot)
566 chip->fixes->remove_slot(slot, 0);
567
568unmap:
569 iounmap(host->ioaddr);
570
571release:
572 pci_release_region(pdev, bar);
573 sdhci_free_host(host);
574
575 return ERR_PTR(ret);
576}
577
578static void sdhci_pci_remove_slot(struct sdhci_pci_slot *slot)
579{
580 int dead;
581 u32 scratch;
582
583 dead = 0;
584 scratch = readl(slot->host->ioaddr + SDHCI_INT_STATUS);
585 if (scratch == (u32)-1)
586 dead = 1;
587
588 sdhci_remove_host(slot->host, dead);
589
590 if (slot->chip->fixes && slot->chip->fixes->remove_slot)
591 slot->chip->fixes->remove_slot(slot, dead);
592
593 pci_release_region(slot->chip->pdev, slot->pci_bar);
594
595 sdhci_free_host(slot->host);
596}
597
598static int __devinit sdhci_pci_probe(struct pci_dev *pdev,
599 const struct pci_device_id *ent)
600{
601 struct sdhci_pci_chip *chip;
602 struct sdhci_pci_slot *slot;
603
604 u8 slots, rev, first_bar;
605 int ret, i;
606
607 BUG_ON(pdev == NULL);
608 BUG_ON(ent == NULL);
609
610 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
611
612 dev_info(&pdev->dev, "SDHCI controller found [%04x:%04x] (rev %x)\n",
613 (int)pdev->vendor, (int)pdev->device, (int)rev);
614
615 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
616 if (ret)
617 return ret;
618
619 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
620 dev_dbg(&pdev->dev, "found %d slot(s)\n", slots);
621 if (slots == 0)
622 return -ENODEV;
623
624 BUG_ON(slots > MAX_SLOTS);
625
626 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
627 if (ret)
628 return ret;
629
630 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
631
632 if (first_bar > 5) {
633 dev_err(&pdev->dev, "Invalid first BAR. Aborting.\n");
634 return -ENODEV;
635 }
636
637 ret = pci_enable_device(pdev);
638 if (ret)
639 return ret;
640
641 chip = kzalloc(sizeof(struct sdhci_pci_chip), GFP_KERNEL);
642 if (!chip) {
643 ret = -ENOMEM;
644 goto err;
645 }
646
647 chip->pdev = pdev;
648 chip->fixes = (const struct sdhci_pci_fixes*)ent->driver_data;
649 if (chip->fixes)
650 chip->quirks = chip->fixes->quirks;
651 chip->num_slots = slots;
652
653 pci_set_drvdata(pdev, chip);
654
655 if (chip->fixes && chip->fixes->probe) {
656 ret = chip->fixes->probe(chip);
657 if (ret)
658 goto free;
659 }
660
661 for (i = 0;i < slots;i++) {
662 slot = sdhci_pci_probe_slot(pdev, chip, first_bar + i);
663 if (IS_ERR(slot)) {
664 for (i--;i >= 0;i--)
665 sdhci_pci_remove_slot(chip->slots[i]);
666 ret = PTR_ERR(slot);
667 goto free;
668 }
669
670 chip->slots[i] = slot;
671 }
672
673 return 0;
674
675free:
676 pci_set_drvdata(pdev, NULL);
677 kfree(chip);
678
679err:
680 pci_disable_device(pdev);
681 return ret;
682}
683
684static void __devexit sdhci_pci_remove(struct pci_dev *pdev)
685{
686 int i;
687 struct sdhci_pci_chip *chip;
688
689 chip = pci_get_drvdata(pdev);
690
691 if (chip) {
692 for (i = 0;i < chip->num_slots; i++)
693 sdhci_pci_remove_slot(chip->slots[i]);
694
695 pci_set_drvdata(pdev, NULL);
696 kfree(chip);
697 }
698
699 pci_disable_device(pdev);
700}
701
702static struct pci_driver sdhci_driver = {
703 .name = "sdhci-pci",
704 .id_table = pci_ids,
705 .probe = sdhci_pci_probe,
706 .remove = __devexit_p(sdhci_pci_remove),
707 .suspend = sdhci_pci_suspend,
708 .resume = sdhci_pci_resume,
709};
710
711/*****************************************************************************\
712 * *
713 * Driver init/exit *
714 * *
715\*****************************************************************************/
716
717static int __init sdhci_drv_init(void)
718{
719 return pci_register_driver(&sdhci_driver);
720}
721
722static void __exit sdhci_drv_exit(void)
723{
724 pci_unregister_driver(&sdhci_driver);
725}
726
727module_init(sdhci_drv_init);
728module_exit(sdhci_drv_exit);
729
730MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
731MODULE_DESCRIPTION("Secure Digital Host Controller Interface PCI driver");
732MODULE_LICENSE("GPL");
diff --git a/drivers/mmc/host/sdhci.c b/drivers/mmc/host/sdhci.c
index b413aa6c246b..17701c3da733 100644
--- a/drivers/mmc/host/sdhci.c
+++ b/drivers/mmc/host/sdhci.c
@@ -15,7 +15,7 @@
15 15
16#include <linux/delay.h> 16#include <linux/delay.h>
17#include <linux/highmem.h> 17#include <linux/highmem.h>
18#include <linux/pci.h> 18#include <linux/io.h>
19#include <linux/dma-mapping.h> 19#include <linux/dma-mapping.h>
20#include <linux/scatterlist.h> 20#include <linux/scatterlist.h>
21 21
@@ -32,135 +32,6 @@
32 32
33static unsigned int debug_quirks = 0; 33static unsigned int debug_quirks = 0;
34 34
35/*
36 * Different quirks to handle when the hardware deviates from a strict
37 * interpretation of the SDHCI specification.
38 */
39
40/* Controller doesn't honor resets unless we touch the clock register */
41#define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
42/* Controller has bad caps bits, but really supports DMA */
43#define SDHCI_QUIRK_FORCE_DMA (1<<1)
44/* Controller doesn't like to be reset when there is no card inserted. */
45#define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
46/* Controller doesn't like clearing the power reg before a change */
47#define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
48/* Controller has flaky internal state so reset it on each ios change */
49#define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4)
50/* Controller has an unusable DMA engine */
51#define SDHCI_QUIRK_BROKEN_DMA (1<<5)
52/* Controller can only DMA from 32-bit aligned addresses */
53#define SDHCI_QUIRK_32BIT_DMA_ADDR (1<<6)
54/* Controller can only DMA chunk sizes that are a multiple of 32 bits */
55#define SDHCI_QUIRK_32BIT_DMA_SIZE (1<<7)
56/* Controller needs to be reset after each request to stay stable */
57#define SDHCI_QUIRK_RESET_AFTER_REQUEST (1<<8)
58/* Controller needs voltage and power writes to happen separately */
59#define SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER (1<<9)
60/* Controller has an off-by-one issue with timeout value */
61#define SDHCI_QUIRK_INCR_TIMEOUT_CONTROL (1<<10)
62
63static const struct pci_device_id pci_ids[] __devinitdata = {
64 {
65 .vendor = PCI_VENDOR_ID_RICOH,
66 .device = PCI_DEVICE_ID_RICOH_R5C822,
67 .subvendor = PCI_VENDOR_ID_IBM,
68 .subdevice = PCI_ANY_ID,
69 .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET |
70 SDHCI_QUIRK_FORCE_DMA,
71 },
72
73 {
74 .vendor = PCI_VENDOR_ID_RICOH,
75 .device = PCI_DEVICE_ID_RICOH_R5C822,
76 .subvendor = PCI_VENDOR_ID_SAMSUNG,
77 .subdevice = PCI_ANY_ID,
78 .driver_data = SDHCI_QUIRK_FORCE_DMA |
79 SDHCI_QUIRK_NO_CARD_NO_RESET,
80 },
81
82 {
83 .vendor = PCI_VENDOR_ID_RICOH,
84 .device = PCI_DEVICE_ID_RICOH_R5C822,
85 .subvendor = PCI_ANY_ID,
86 .subdevice = PCI_ANY_ID,
87 .driver_data = SDHCI_QUIRK_FORCE_DMA,
88 },
89
90 {
91 .vendor = PCI_VENDOR_ID_TI,
92 .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
93 .subvendor = PCI_ANY_ID,
94 .subdevice = PCI_ANY_ID,
95 .driver_data = SDHCI_QUIRK_FORCE_DMA,
96 },
97
98 {
99 .vendor = PCI_VENDOR_ID_ENE,
100 .device = PCI_DEVICE_ID_ENE_CB712_SD,
101 .subvendor = PCI_ANY_ID,
102 .subdevice = PCI_ANY_ID,
103 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
104 SDHCI_QUIRK_BROKEN_DMA,
105 },
106
107 {
108 .vendor = PCI_VENDOR_ID_ENE,
109 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
110 .subvendor = PCI_ANY_ID,
111 .subdevice = PCI_ANY_ID,
112 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
113 SDHCI_QUIRK_BROKEN_DMA,
114 },
115
116 {
117 .vendor = PCI_VENDOR_ID_ENE,
118 .device = PCI_DEVICE_ID_ENE_CB714_SD,
119 .subvendor = PCI_ANY_ID,
120 .subdevice = PCI_ANY_ID,
121 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
122 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
123 SDHCI_QUIRK_BROKEN_DMA,
124 },
125
126 {
127 .vendor = PCI_VENDOR_ID_ENE,
128 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
129 .subvendor = PCI_ANY_ID,
130 .subdevice = PCI_ANY_ID,
131 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
132 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS |
133 SDHCI_QUIRK_BROKEN_DMA,
134 },
135
136 {
137 .vendor = PCI_VENDOR_ID_MARVELL,
138 .device = PCI_DEVICE_ID_MARVELL_CAFE_SD,
139 .subvendor = PCI_ANY_ID,
140 .subdevice = PCI_ANY_ID,
141 .driver_data = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
142 SDHCI_QUIRK_INCR_TIMEOUT_CONTROL,
143 },
144
145 {
146 .vendor = PCI_VENDOR_ID_JMICRON,
147 .device = PCI_DEVICE_ID_JMICRON_JMB38X_SD,
148 .subvendor = PCI_ANY_ID,
149 .subdevice = PCI_ANY_ID,
150 .driver_data = SDHCI_QUIRK_32BIT_DMA_ADDR |
151 SDHCI_QUIRK_32BIT_DMA_SIZE |
152 SDHCI_QUIRK_RESET_AFTER_REQUEST,
153 },
154
155 { /* Generic SD host controller */
156 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
157 },
158
159 { /* end: all zeroes */ },
160};
161
162MODULE_DEVICE_TABLE(pci, pci_ids);
163
164static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *); 35static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
165static void sdhci_finish_data(struct sdhci_host *); 36static void sdhci_finish_data(struct sdhci_host *);
166 37
@@ -215,7 +86,7 @@ static void sdhci_reset(struct sdhci_host *host, u8 mask)
215{ 86{
216 unsigned long timeout; 87 unsigned long timeout;
217 88
218 if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 89 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
219 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & 90 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
220 SDHCI_CARD_PRESENT)) 91 SDHCI_CARD_PRESENT))
221 return; 92 return;
@@ -253,7 +124,8 @@ static void sdhci_init(struct sdhci_host *host)
253 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 124 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
254 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT | 125 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
255 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 126 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
256 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE; 127 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
128 SDHCI_INT_ADMA_ERROR;
257 129
258 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE); 130 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
259 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE); 131 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
@@ -443,23 +315,226 @@ static void sdhci_transfer_pio(struct sdhci_host *host)
443 DBG("PIO transfer complete.\n"); 315 DBG("PIO transfer complete.\n");
444} 316}
445 317
446static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data) 318static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
447{ 319{
448 u8 count; 320 local_irq_save(*flags);
449 unsigned target_timeout, current_timeout; 321 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
322}
450 323
451 WARN_ON(host->data); 324static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
325{
326 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
327 local_irq_restore(*flags);
328}
452 329
453 if (data == NULL) 330static int sdhci_adma_table_pre(struct sdhci_host *host,
454 return; 331 struct mmc_data *data)
332{
333 int direction;
455 334
456 /* Sanity checks */ 335 u8 *desc;
457 BUG_ON(data->blksz * data->blocks > 524288); 336 u8 *align;
458 BUG_ON(data->blksz > host->mmc->max_blk_size); 337 dma_addr_t addr;
459 BUG_ON(data->blocks > 65535); 338 dma_addr_t align_addr;
339 int len, offset;
460 340
461 host->data = data; 341 struct scatterlist *sg;
462 host->data_early = 0; 342 int i;
343 char *buffer;
344 unsigned long flags;
345
346 /*
347 * The spec does not specify endianness of descriptor table.
348 * We currently guess that it is LE.
349 */
350
351 if (data->flags & MMC_DATA_READ)
352 direction = DMA_FROM_DEVICE;
353 else
354 direction = DMA_TO_DEVICE;
355
356 /*
357 * The ADMA descriptor table is mapped further down as we
358 * need to fill it with data first.
359 */
360
361 host->align_addr = dma_map_single(mmc_dev(host->mmc),
362 host->align_buffer, 128 * 4, direction);
363 if (dma_mapping_error(host->align_addr))
364 goto fail;
365 BUG_ON(host->align_addr & 0x3);
366
367 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
368 data->sg, data->sg_len, direction);
369 if (host->sg_count == 0)
370 goto unmap_align;
371
372 desc = host->adma_desc;
373 align = host->align_buffer;
374
375 align_addr = host->align_addr;
376
377 for_each_sg(data->sg, sg, host->sg_count, i) {
378 addr = sg_dma_address(sg);
379 len = sg_dma_len(sg);
380
381 /*
382 * The SDHCI specification states that ADMA
383 * addresses must be 32-bit aligned. If they
384 * aren't, then we use a bounce buffer for
385 * the (up to three) bytes that screw up the
386 * alignment.
387 */
388 offset = (4 - (addr & 0x3)) & 0x3;
389 if (offset) {
390 if (data->flags & MMC_DATA_WRITE) {
391 buffer = sdhci_kmap_atomic(sg, &flags);
392 memcpy(align, buffer, offset);
393 sdhci_kunmap_atomic(buffer, &flags);
394 }
395
396 desc[7] = (align_addr >> 24) & 0xff;
397 desc[6] = (align_addr >> 16) & 0xff;
398 desc[5] = (align_addr >> 8) & 0xff;
399 desc[4] = (align_addr >> 0) & 0xff;
400
401 BUG_ON(offset > 65536);
402
403 desc[3] = (offset >> 8) & 0xff;
404 desc[2] = (offset >> 0) & 0xff;
405
406 desc[1] = 0x00;
407 desc[0] = 0x21; /* tran, valid */
408
409 align += 4;
410 align_addr += 4;
411
412 desc += 8;
413
414 addr += offset;
415 len -= offset;
416 }
417
418 desc[7] = (addr >> 24) & 0xff;
419 desc[6] = (addr >> 16) & 0xff;
420 desc[5] = (addr >> 8) & 0xff;
421 desc[4] = (addr >> 0) & 0xff;
422
423 BUG_ON(len > 65536);
424
425 desc[3] = (len >> 8) & 0xff;
426 desc[2] = (len >> 0) & 0xff;
427
428 desc[1] = 0x00;
429 desc[0] = 0x21; /* tran, valid */
430
431 desc += 8;
432
433 /*
434 * If this triggers then we have a calculation bug
435 * somewhere. :/
436 */
437 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
438 }
439
440 /*
441 * Add a terminating entry.
442 */
443 desc[7] = 0;
444 desc[6] = 0;
445 desc[5] = 0;
446 desc[4] = 0;
447
448 desc[3] = 0;
449 desc[2] = 0;
450
451 desc[1] = 0x00;
452 desc[0] = 0x03; /* nop, end, valid */
453
454 /*
455 * Resync align buffer as we might have changed it.
456 */
457 if (data->flags & MMC_DATA_WRITE) {
458 dma_sync_single_for_device(mmc_dev(host->mmc),
459 host->align_addr, 128 * 4, direction);
460 }
461
462 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
463 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
464 if (dma_mapping_error(host->align_addr))
465 goto unmap_entries;
466 BUG_ON(host->adma_addr & 0x3);
467
468 return 0;
469
470unmap_entries:
471 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
472 data->sg_len, direction);
473unmap_align:
474 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
475 128 * 4, direction);
476fail:
477 return -EINVAL;
478}
479
480static void sdhci_adma_table_post(struct sdhci_host *host,
481 struct mmc_data *data)
482{
483 int direction;
484
485 struct scatterlist *sg;
486 int i, size;
487 u8 *align;
488 char *buffer;
489 unsigned long flags;
490
491 if (data->flags & MMC_DATA_READ)
492 direction = DMA_FROM_DEVICE;
493 else
494 direction = DMA_TO_DEVICE;
495
496 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
497 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
498
499 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
500 128 * 4, direction);
501
502 if (data->flags & MMC_DATA_READ) {
503 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
504 data->sg_len, direction);
505
506 align = host->align_buffer;
507
508 for_each_sg(data->sg, sg, host->sg_count, i) {
509 if (sg_dma_address(sg) & 0x3) {
510 size = 4 - (sg_dma_address(sg) & 0x3);
511
512 buffer = sdhci_kmap_atomic(sg, &flags);
513 memcpy(buffer, align, size);
514 sdhci_kunmap_atomic(buffer, &flags);
515
516 align += 4;
517 }
518 }
519 }
520
521 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
522 data->sg_len, direction);
523}
524
525static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
526{
527 u8 count;
528 unsigned target_timeout, current_timeout;
529
530 /*
531 * If the host controller provides us with an incorrect timeout
532 * value, just skip the check and use 0xE. The hardware may take
533 * longer to time out, but that's much better than having a too-short
534 * timeout value.
535 */
536 if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL))
537 return 0xE;
463 538
464 /* timeout in us */ 539 /* timeout in us */
465 target_timeout = data->timeout_ns / 1000 + 540 target_timeout = data->timeout_ns / 1000 +
@@ -484,52 +559,158 @@ static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
484 break; 559 break;
485 } 560 }
486 561
487 /*
488 * Compensate for an off-by-one error in the CaFe hardware; otherwise,
489 * a too-small count gives us interrupt timeouts.
490 */
491 if ((host->chip->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL))
492 count++;
493
494 if (count >= 0xF) { 562 if (count >= 0xF) {
495 printk(KERN_WARNING "%s: Too large timeout requested!\n", 563 printk(KERN_WARNING "%s: Too large timeout requested!\n",
496 mmc_hostname(host->mmc)); 564 mmc_hostname(host->mmc));
497 count = 0xE; 565 count = 0xE;
498 } 566 }
499 567
568 return count;
569}
570
571static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
572{
573 u8 count;
574 u8 ctrl;
575 int ret;
576
577 WARN_ON(host->data);
578
579 if (data == NULL)
580 return;
581
582 /* Sanity checks */
583 BUG_ON(data->blksz * data->blocks > 524288);
584 BUG_ON(data->blksz > host->mmc->max_blk_size);
585 BUG_ON(data->blocks > 65535);
586
587 host->data = data;
588 host->data_early = 0;
589
590 count = sdhci_calc_timeout(host, data);
500 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL); 591 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
501 592
502 if (host->flags & SDHCI_USE_DMA) 593 if (host->flags & SDHCI_USE_DMA)
503 host->flags |= SDHCI_REQ_USE_DMA; 594 host->flags |= SDHCI_REQ_USE_DMA;
504 595
505 if (unlikely((host->flags & SDHCI_REQ_USE_DMA) && 596 /*
506 (host->chip->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 597 * FIXME: This doesn't account for merging when mapping the
507 ((data->blksz * data->blocks) & 0x3))) { 598 * scatterlist.
508 DBG("Reverting to PIO because of transfer size (%d)\n", 599 */
509 data->blksz * data->blocks); 600 if (host->flags & SDHCI_REQ_USE_DMA) {
510 host->flags &= ~SDHCI_REQ_USE_DMA; 601 int broken, i;
602 struct scatterlist *sg;
603
604 broken = 0;
605 if (host->flags & SDHCI_USE_ADMA) {
606 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
607 broken = 1;
608 } else {
609 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
610 broken = 1;
611 }
612
613 if (unlikely(broken)) {
614 for_each_sg(data->sg, sg, data->sg_len, i) {
615 if (sg->length & 0x3) {
616 DBG("Reverting to PIO because of "
617 "transfer size (%d)\n",
618 sg->length);
619 host->flags &= ~SDHCI_REQ_USE_DMA;
620 break;
621 }
622 }
623 }
511 } 624 }
512 625
513 /* 626 /*
514 * The assumption here being that alignment is the same after 627 * The assumption here being that alignment is the same after
515 * translation to device address space. 628 * translation to device address space.
516 */ 629 */
517 if (unlikely((host->flags & SDHCI_REQ_USE_DMA) && 630 if (host->flags & SDHCI_REQ_USE_DMA) {
518 (host->chip->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && 631 int broken, i;
519 (data->sg->offset & 0x3))) { 632 struct scatterlist *sg;
520 DBG("Reverting to PIO because of bad alignment\n"); 633
521 host->flags &= ~SDHCI_REQ_USE_DMA; 634 broken = 0;
635 if (host->flags & SDHCI_USE_ADMA) {
636 /*
637 * As we use 3 byte chunks to work around
638 * alignment problems, we need to check this
639 * quirk.
640 */
641 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
642 broken = 1;
643 } else {
644 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
645 broken = 1;
646 }
647
648 if (unlikely(broken)) {
649 for_each_sg(data->sg, sg, data->sg_len, i) {
650 if (sg->offset & 0x3) {
651 DBG("Reverting to PIO because of "
652 "bad alignment\n");
653 host->flags &= ~SDHCI_REQ_USE_DMA;
654 break;
655 }
656 }
657 }
522 } 658 }
523 659
524 if (host->flags & SDHCI_REQ_USE_DMA) { 660 if (host->flags & SDHCI_REQ_USE_DMA) {
525 int count; 661 if (host->flags & SDHCI_USE_ADMA) {
662 ret = sdhci_adma_table_pre(host, data);
663 if (ret) {
664 /*
665 * This only happens when someone fed
666 * us an invalid request.
667 */
668 WARN_ON(1);
669 host->flags &= ~SDHCI_USE_DMA;
670 } else {
671 writel(host->adma_addr,
672 host->ioaddr + SDHCI_ADMA_ADDRESS);
673 }
674 } else {
675 int sg_cnt;
676
677 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
678 data->sg, data->sg_len,
679 (data->flags & MMC_DATA_READ) ?
680 DMA_FROM_DEVICE :
681 DMA_TO_DEVICE);
682 if (sg_cnt == 0) {
683 /*
684 * This only happens when someone fed
685 * us an invalid request.
686 */
687 WARN_ON(1);
688 host->flags &= ~SDHCI_USE_DMA;
689 } else {
690 WARN_ON(count != 1);
691 writel(sg_dma_address(data->sg),
692 host->ioaddr + SDHCI_DMA_ADDRESS);
693 }
694 }
695 }
526 696
527 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len, 697 /*
528 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE); 698 * Always adjust the DMA selection as some controllers
529 BUG_ON(count != 1); 699 * (e.g. JMicron) can't do PIO properly when the selection
700 * is ADMA.
701 */
702 if (host->version >= SDHCI_SPEC_200) {
703 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
704 ctrl &= ~SDHCI_CTRL_DMA_MASK;
705 if ((host->flags & SDHCI_REQ_USE_DMA) &&
706 (host->flags & SDHCI_USE_ADMA))
707 ctrl |= SDHCI_CTRL_ADMA32;
708 else
709 ctrl |= SDHCI_CTRL_SDMA;
710 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
711 }
530 712
531 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS); 713 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
532 } else {
533 host->cur_sg = data->sg; 714 host->cur_sg = data->sg;
534 host->num_sg = data->sg_len; 715 host->num_sg = data->sg_len;
535 716
@@ -567,7 +748,6 @@ static void sdhci_set_transfer_mode(struct sdhci_host *host,
567static void sdhci_finish_data(struct sdhci_host *host) 748static void sdhci_finish_data(struct sdhci_host *host)
568{ 749{
569 struct mmc_data *data; 750 struct mmc_data *data;
570 u16 blocks;
571 751
572 BUG_ON(!host->data); 752 BUG_ON(!host->data);
573 753
@@ -575,25 +755,26 @@ static void sdhci_finish_data(struct sdhci_host *host)
575 host->data = NULL; 755 host->data = NULL;
576 756
577 if (host->flags & SDHCI_REQ_USE_DMA) { 757 if (host->flags & SDHCI_REQ_USE_DMA) {
578 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len, 758 if (host->flags & SDHCI_USE_ADMA)
579 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE); 759 sdhci_adma_table_post(host, data);
760 else {
761 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
762 data->sg_len, (data->flags & MMC_DATA_READ) ?
763 DMA_FROM_DEVICE : DMA_TO_DEVICE);
764 }
580 } 765 }
581 766
582 /* 767 /*
583 * Controller doesn't count down when in single block mode. 768 * The specification states that the block count register must
769 * be updated, but it does not specify at what point in the
770 * data flow. That makes the register entirely useless to read
771 * back so we have to assume that nothing made it to the card
772 * in the event of an error.
584 */ 773 */
585 if (data->blocks == 1) 774 if (data->error)
586 blocks = (data->error == 0) ? 0 : 1; 775 data->bytes_xfered = 0;
587 else 776 else
588 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT); 777 data->bytes_xfered = data->blksz * data->blocks;
589 data->bytes_xfered = data->blksz * (data->blocks - blocks);
590
591 if (!data->error && blocks) {
592 printk(KERN_ERR "%s: Controller signalled completion even "
593 "though there were blocks left.\n",
594 mmc_hostname(host->mmc));
595 data->error = -EIO;
596 }
597 778
598 if (data->stop) { 779 if (data->stop) {
599 /* 780 /*
@@ -775,7 +956,7 @@ static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
775 * Spec says that we should clear the power reg before setting 956 * Spec says that we should clear the power reg before setting
776 * a new value. Some controllers don't seem to like this though. 957 * a new value. Some controllers don't seem to like this though.
777 */ 958 */
778 if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) 959 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
779 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL); 960 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
780 961
781 pwr = SDHCI_POWER_ON; 962 pwr = SDHCI_POWER_ON;
@@ -797,10 +978,10 @@ static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
797 } 978 }
798 979
799 /* 980 /*
800 * At least the CaFe chip gets confused if we set the voltage 981 * At least the Marvell CaFe chip gets confused if we set the voltage
801 * and set turn on power at the same time, so set the voltage first. 982 * and set turn on power at the same time, so set the voltage first.
802 */ 983 */
803 if ((host->chip->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)) 984 if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
804 writeb(pwr & ~SDHCI_POWER_ON, 985 writeb(pwr & ~SDHCI_POWER_ON,
805 host->ioaddr + SDHCI_POWER_CONTROL); 986 host->ioaddr + SDHCI_POWER_CONTROL);
806 987
@@ -833,7 +1014,8 @@ static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
833 1014
834 host->mrq = mrq; 1015 host->mrq = mrq;
835 1016
836 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) { 1017 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)
1018 || (host->flags & SDHCI_DEVICE_DEAD)) {
837 host->mrq->cmd->error = -ENOMEDIUM; 1019 host->mrq->cmd->error = -ENOMEDIUM;
838 tasklet_schedule(&host->finish_tasklet); 1020 tasklet_schedule(&host->finish_tasklet);
839 } else 1021 } else
@@ -853,6 +1035,9 @@ static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
853 1035
854 spin_lock_irqsave(&host->lock, flags); 1036 spin_lock_irqsave(&host->lock, flags);
855 1037
1038 if (host->flags & SDHCI_DEVICE_DEAD)
1039 goto out;
1040
856 /* 1041 /*
857 * Reset the chip on each power off. 1042 * Reset the chip on each power off.
858 * Should clear out any weird states. 1043 * Should clear out any weird states.
@@ -888,9 +1073,10 @@ static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
888 * signalling timeout and CRC errors even on CMD0. Resetting 1073 * signalling timeout and CRC errors even on CMD0. Resetting
889 * it on each ios seems to solve the problem. 1074 * it on each ios seems to solve the problem.
890 */ 1075 */
891 if(host->chip->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS) 1076 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
892 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1077 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
893 1078
1079out:
894 mmiowb(); 1080 mmiowb();
895 spin_unlock_irqrestore(&host->lock, flags); 1081 spin_unlock_irqrestore(&host->lock, flags);
896} 1082}
@@ -905,7 +1091,10 @@ static int sdhci_get_ro(struct mmc_host *mmc)
905 1091
906 spin_lock_irqsave(&host->lock, flags); 1092 spin_lock_irqsave(&host->lock, flags);
907 1093
908 present = readl(host->ioaddr + SDHCI_PRESENT_STATE); 1094 if (host->flags & SDHCI_DEVICE_DEAD)
1095 present = 0;
1096 else
1097 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
909 1098
910 spin_unlock_irqrestore(&host->lock, flags); 1099 spin_unlock_irqrestore(&host->lock, flags);
911 1100
@@ -922,6 +1111,9 @@ static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
922 1111
923 spin_lock_irqsave(&host->lock, flags); 1112 spin_lock_irqsave(&host->lock, flags);
924 1113
1114 if (host->flags & SDHCI_DEVICE_DEAD)
1115 goto out;
1116
925 ier = readl(host->ioaddr + SDHCI_INT_ENABLE); 1117 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
926 1118
927 ier &= ~SDHCI_INT_CARD_INT; 1119 ier &= ~SDHCI_INT_CARD_INT;
@@ -931,6 +1123,7 @@ static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
931 writel(ier, host->ioaddr + SDHCI_INT_ENABLE); 1123 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
932 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE); 1124 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
933 1125
1126out:
934 mmiowb(); 1127 mmiowb();
935 1128
936 spin_unlock_irqrestore(&host->lock, flags); 1129 spin_unlock_irqrestore(&host->lock, flags);
@@ -996,13 +1189,14 @@ static void sdhci_tasklet_finish(unsigned long param)
996 * The controller needs a reset of internal state machines 1189 * The controller needs a reset of internal state machines
997 * upon error conditions. 1190 * upon error conditions.
998 */ 1191 */
999 if (mrq->cmd->error || 1192 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1000 (mrq->data && (mrq->data->error || 1193 (mrq->cmd->error ||
1001 (mrq->data->stop && mrq->data->stop->error))) || 1194 (mrq->data && (mrq->data->error ||
1002 (host->chip->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1195 (mrq->data->stop && mrq->data->stop->error))) ||
1196 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1003 1197
1004 /* Some controllers need this kick or reset won't work here */ 1198 /* Some controllers need this kick or reset won't work here */
1005 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) { 1199 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
1006 unsigned int clock; 1200 unsigned int clock;
1007 1201
1008 /* This is to force an update */ 1202 /* This is to force an update */
@@ -1116,6 +1310,8 @@ static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1116 host->data->error = -ETIMEDOUT; 1310 host->data->error = -ETIMEDOUT;
1117 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 1311 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1118 host->data->error = -EILSEQ; 1312 host->data->error = -EILSEQ;
1313 else if (intmask & SDHCI_INT_ADMA_ERROR)
1314 host->data->error = -EIO;
1119 1315
1120 if (host->data->error) 1316 if (host->data->error)
1121 sdhci_finish_data(host); 1317 sdhci_finish_data(host);
@@ -1234,218 +1430,167 @@ out:
1234 1430
1235#ifdef CONFIG_PM 1431#ifdef CONFIG_PM
1236 1432
1237static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state) 1433int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
1238{ 1434{
1239 struct sdhci_chip *chip; 1435 int ret;
1240 int i, ret;
1241
1242 chip = pci_get_drvdata(pdev);
1243 if (!chip)
1244 return 0;
1245
1246 DBG("Suspending...\n");
1247
1248 for (i = 0;i < chip->num_slots;i++) {
1249 if (!chip->hosts[i])
1250 continue;
1251 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1252 if (ret) {
1253 for (i--;i >= 0;i--)
1254 mmc_resume_host(chip->hosts[i]->mmc);
1255 return ret;
1256 }
1257 }
1258
1259 pci_save_state(pdev);
1260 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1261 1436
1262 for (i = 0;i < chip->num_slots;i++) { 1437 ret = mmc_suspend_host(host->mmc, state);
1263 if (!chip->hosts[i]) 1438 if (ret)
1264 continue; 1439 return ret;
1265 free_irq(chip->hosts[i]->irq, chip->hosts[i]);
1266 }
1267 1440
1268 pci_disable_device(pdev); 1441 free_irq(host->irq, host);
1269 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1270 1442
1271 return 0; 1443 return 0;
1272} 1444}
1273 1445
1274static int sdhci_resume (struct pci_dev *pdev) 1446EXPORT_SYMBOL_GPL(sdhci_suspend_host);
1275{
1276 struct sdhci_chip *chip;
1277 int i, ret;
1278 1447
1279 chip = pci_get_drvdata(pdev); 1448int sdhci_resume_host(struct sdhci_host *host)
1280 if (!chip) 1449{
1281 return 0; 1450 int ret;
1282 1451
1283 DBG("Resuming...\n"); 1452 if (host->flags & SDHCI_USE_DMA) {
1453 if (host->ops->enable_dma)
1454 host->ops->enable_dma(host);
1455 }
1284 1456
1285 pci_set_power_state(pdev, PCI_D0); 1457 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1286 pci_restore_state(pdev); 1458 mmc_hostname(host->mmc), host);
1287 ret = pci_enable_device(pdev);
1288 if (ret) 1459 if (ret)
1289 return ret; 1460 return ret;
1290 1461
1291 for (i = 0;i < chip->num_slots;i++) { 1462 sdhci_init(host);
1292 if (!chip->hosts[i]) 1463 mmiowb();
1293 continue; 1464
1294 if (chip->hosts[i]->flags & SDHCI_USE_DMA) 1465 ret = mmc_resume_host(host->mmc);
1295 pci_set_master(pdev); 1466 if (ret)
1296 ret = request_irq(chip->hosts[i]->irq, sdhci_irq, 1467 return ret;
1297 IRQF_SHARED, mmc_hostname(chip->hosts[i]->mmc),
1298 chip->hosts[i]);
1299 if (ret)
1300 return ret;
1301 sdhci_init(chip->hosts[i]);
1302 mmiowb();
1303 ret = mmc_resume_host(chip->hosts[i]->mmc);
1304 if (ret)
1305 return ret;
1306 }
1307 1468
1308 return 0; 1469 return 0;
1309} 1470}
1310 1471
1311#else /* CONFIG_PM */ 1472EXPORT_SYMBOL_GPL(sdhci_resume_host);
1312
1313#define sdhci_suspend NULL
1314#define sdhci_resume NULL
1315 1473
1316#endif /* CONFIG_PM */ 1474#endif /* CONFIG_PM */
1317 1475
1318/*****************************************************************************\ 1476/*****************************************************************************\
1319 * * 1477 * *
1320 * Device probing/removal * 1478 * Device allocation/registration *
1321 * * 1479 * *
1322\*****************************************************************************/ 1480\*****************************************************************************/
1323 1481
1324static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot) 1482struct sdhci_host *sdhci_alloc_host(struct device *dev,
1483 size_t priv_size)
1325{ 1484{
1326 int ret;
1327 unsigned int version;
1328 struct sdhci_chip *chip;
1329 struct mmc_host *mmc; 1485 struct mmc_host *mmc;
1330 struct sdhci_host *host; 1486 struct sdhci_host *host;
1331 1487
1332 u8 first_bar; 1488 WARN_ON(dev == NULL);
1333 unsigned int caps;
1334
1335 chip = pci_get_drvdata(pdev);
1336 BUG_ON(!chip);
1337
1338 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1339 if (ret)
1340 return ret;
1341
1342 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1343
1344 if (first_bar > 5) {
1345 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1346 return -ENODEV;
1347 }
1348
1349 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1350 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1351 return -ENODEV;
1352 }
1353
1354 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1355 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. "
1356 "You may experience problems.\n");
1357 }
1358
1359 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1360 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1361 return -ENODEV;
1362 }
1363
1364 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1365 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1366 return -ENODEV;
1367 }
1368 1489
1369 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev); 1490 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
1370 if (!mmc) 1491 if (!mmc)
1371 return -ENOMEM; 1492 return ERR_PTR(-ENOMEM);
1372 1493
1373 host = mmc_priv(mmc); 1494 host = mmc_priv(mmc);
1374 host->mmc = mmc; 1495 host->mmc = mmc;
1375 1496
1376 host->chip = chip; 1497 return host;
1377 chip->hosts[slot] = host; 1498}
1378 1499
1379 host->bar = first_bar + slot; 1500EXPORT_SYMBOL_GPL(sdhci_alloc_host);
1380 1501
1381 host->addr = pci_resource_start(pdev, host->bar); 1502int sdhci_add_host(struct sdhci_host *host)
1382 host->irq = pdev->irq; 1503{
1504 struct mmc_host *mmc;
1505 unsigned int caps;
1506 int ret;
1383 1507
1384 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq); 1508 WARN_ON(host == NULL);
1509 if (host == NULL)
1510 return -EINVAL;
1385 1511
1386 ret = pci_request_region(pdev, host->bar, mmc_hostname(mmc)); 1512 mmc = host->mmc;
1387 if (ret)
1388 goto free;
1389 1513
1390 host->ioaddr = ioremap_nocache(host->addr, 1514 if (debug_quirks)
1391 pci_resource_len(pdev, host->bar)); 1515 host->quirks = debug_quirks;
1392 if (!host->ioaddr) {
1393 ret = -ENOMEM;
1394 goto release;
1395 }
1396 1516
1397 sdhci_reset(host, SDHCI_RESET_ALL); 1517 sdhci_reset(host, SDHCI_RESET_ALL);
1398 1518
1399 version = readw(host->ioaddr + SDHCI_HOST_VERSION); 1519 host->version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1400 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT; 1520 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1401 if (version > 1) { 1521 >> SDHCI_SPEC_VER_SHIFT;
1522 if (host->version > SDHCI_SPEC_200) {
1402 printk(KERN_ERR "%s: Unknown controller version (%d). " 1523 printk(KERN_ERR "%s: Unknown controller version (%d). "
1403 "You may experience problems.\n", mmc_hostname(mmc), 1524 "You may experience problems.\n", mmc_hostname(mmc),
1404 version); 1525 host->version);
1405 } 1526 }
1406 1527
1407 caps = readl(host->ioaddr + SDHCI_CAPABILITIES); 1528 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1408 1529
1409 if (chip->quirks & SDHCI_QUIRK_FORCE_DMA) 1530 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
1410 host->flags |= SDHCI_USE_DMA; 1531 host->flags |= SDHCI_USE_DMA;
1411 else if (!(caps & SDHCI_CAN_DO_DMA)) 1532 else if (!(caps & SDHCI_CAN_DO_DMA))
1412 DBG("Controller doesn't have DMA capability\n"); 1533 DBG("Controller doesn't have DMA capability\n");
1413 else 1534 else
1414 host->flags |= SDHCI_USE_DMA; 1535 host->flags |= SDHCI_USE_DMA;
1415 1536
1416 if ((chip->quirks & SDHCI_QUIRK_BROKEN_DMA) && 1537 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1417 (host->flags & SDHCI_USE_DMA)) { 1538 (host->flags & SDHCI_USE_DMA)) {
1418 DBG("Disabling DMA as it is marked broken\n"); 1539 DBG("Disabling DMA as it is marked broken\n");
1419 host->flags &= ~SDHCI_USE_DMA; 1540 host->flags &= ~SDHCI_USE_DMA;
1420 } 1541 }
1421 1542
1422 if (((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) && 1543 if (host->flags & SDHCI_USE_DMA) {
1423 (host->flags & SDHCI_USE_DMA)) { 1544 if ((host->version >= SDHCI_SPEC_200) &&
1424 printk(KERN_WARNING "%s: Will use DMA " 1545 (caps & SDHCI_CAN_DO_ADMA2))
1425 "mode even though HW doesn't fully " 1546 host->flags |= SDHCI_USE_ADMA;
1426 "claim to support it.\n", mmc_hostname(mmc)); 1547 }
1548
1549 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1550 (host->flags & SDHCI_USE_ADMA)) {
1551 DBG("Disabling ADMA as it is marked broken\n");
1552 host->flags &= ~SDHCI_USE_ADMA;
1427 } 1553 }
1428 1554
1429 if (host->flags & SDHCI_USE_DMA) { 1555 if (host->flags & SDHCI_USE_DMA) {
1430 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) { 1556 if (host->ops->enable_dma) {
1431 printk(KERN_WARNING "%s: No suitable DMA available. " 1557 if (host->ops->enable_dma(host)) {
1432 "Falling back to PIO.\n", mmc_hostname(mmc)); 1558 printk(KERN_WARNING "%s: No suitable DMA "
1433 host->flags &= ~SDHCI_USE_DMA; 1559 "available. Falling back to PIO.\n",
1560 mmc_hostname(mmc));
1561 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
1562 }
1434 } 1563 }
1435 } 1564 }
1436 1565
1437 if (host->flags & SDHCI_USE_DMA) 1566 if (host->flags & SDHCI_USE_ADMA) {
1438 pci_set_master(pdev); 1567 /*
1439 else /* XXX: Hack to get MMC layer to avoid highmem */ 1568 * We need to allocate descriptors for all sg entries
1440 pdev->dma_mask = 0; 1569 * (128) and potentially one alignment transfer for
1570 * each of those entries.
1571 */
1572 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1573 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1574 if (!host->adma_desc || !host->align_buffer) {
1575 kfree(host->adma_desc);
1576 kfree(host->align_buffer);
1577 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1578 "buffers. Falling back to standard DMA.\n",
1579 mmc_hostname(mmc));
1580 host->flags &= ~SDHCI_USE_ADMA;
1581 }
1582 }
1583
1584 /* XXX: Hack to get MMC layer to avoid highmem */
1585 if (!(host->flags & SDHCI_USE_DMA))
1586 mmc_dev(host->mmc)->dma_mask = NULL;
1441 1587
1442 host->max_clk = 1588 host->max_clk =
1443 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT; 1589 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1444 if (host->max_clk == 0) { 1590 if (host->max_clk == 0) {
1445 printk(KERN_ERR "%s: Hardware doesn't specify base clock " 1591 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1446 "frequency.\n", mmc_hostname(mmc)); 1592 "frequency.\n", mmc_hostname(mmc));
1447 ret = -ENODEV; 1593 return -ENODEV;
1448 goto unmap;
1449 } 1594 }
1450 host->max_clk *= 1000000; 1595 host->max_clk *= 1000000;
1451 1596
@@ -1454,8 +1599,7 @@ static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1454 if (host->timeout_clk == 0) { 1599 if (host->timeout_clk == 0) {
1455 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock " 1600 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1456 "frequency.\n", mmc_hostname(mmc)); 1601 "frequency.\n", mmc_hostname(mmc));
1457 ret = -ENODEV; 1602 return -ENODEV;
1458 goto unmap;
1459 } 1603 }
1460 if (caps & SDHCI_TIMEOUT_CLK_UNIT) 1604 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1461 host->timeout_clk *= 1000; 1605 host->timeout_clk *= 1000;
@@ -1466,7 +1610,7 @@ static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1466 mmc->ops = &sdhci_ops; 1610 mmc->ops = &sdhci_ops;
1467 mmc->f_min = host->max_clk / 256; 1611 mmc->f_min = host->max_clk / 256;
1468 mmc->f_max = host->max_clk; 1612 mmc->f_max = host->max_clk;
1469 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ; 1613 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
1470 1614
1471 if (caps & SDHCI_CAN_DO_HISPD) 1615 if (caps & SDHCI_CAN_DO_HISPD)
1472 mmc->caps |= MMC_CAP_SD_HIGHSPEED; 1616 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
@@ -1482,20 +1626,22 @@ static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1482 if (mmc->ocr_avail == 0) { 1626 if (mmc->ocr_avail == 0) {
1483 printk(KERN_ERR "%s: Hardware doesn't report any " 1627 printk(KERN_ERR "%s: Hardware doesn't report any "
1484 "support voltages.\n", mmc_hostname(mmc)); 1628 "support voltages.\n", mmc_hostname(mmc));
1485 ret = -ENODEV; 1629 return -ENODEV;
1486 goto unmap;
1487 } 1630 }
1488 1631
1489 spin_lock_init(&host->lock); 1632 spin_lock_init(&host->lock);
1490 1633
1491 /* 1634 /*
1492 * Maximum number of segments. Hardware cannot do scatter lists. 1635 * Maximum number of segments. Depends on if the hardware
1636 * can do scatter/gather or not.
1493 */ 1637 */
1494 if (host->flags & SDHCI_USE_DMA) 1638 if (host->flags & SDHCI_USE_ADMA)
1639 mmc->max_hw_segs = 128;
1640 else if (host->flags & SDHCI_USE_DMA)
1495 mmc->max_hw_segs = 1; 1641 mmc->max_hw_segs = 1;
1496 else 1642 else /* PIO */
1497 mmc->max_hw_segs = 16; 1643 mmc->max_hw_segs = 128;
1498 mmc->max_phys_segs = 16; 1644 mmc->max_phys_segs = 128;
1499 1645
1500 /* 1646 /*
1501 * Maximum number of sectors in one transfer. Limited by DMA boundary 1647 * Maximum number of sectors in one transfer. Limited by DMA boundary
@@ -1505,9 +1651,13 @@ static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1505 1651
1506 /* 1652 /*
1507 * Maximum segment size. Could be one segment with the maximum number 1653 * Maximum segment size. Could be one segment with the maximum number
1508 * of bytes. 1654 * of bytes. When doing hardware scatter/gather, each entry cannot
1655 * be larger than 64 KiB though.
1509 */ 1656 */
1510 mmc->max_seg_size = mmc->max_req_size; 1657 if (host->flags & SDHCI_USE_ADMA)
1658 mmc->max_seg_size = 65536;
1659 else
1660 mmc->max_seg_size = mmc->max_req_size;
1511 1661
1512 /* 1662 /*
1513 * Maximum block size. This varies from controller to controller and 1663 * Maximum block size. This varies from controller to controller and
@@ -1553,7 +1703,7 @@ static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1553 host->led.default_trigger = mmc_hostname(mmc); 1703 host->led.default_trigger = mmc_hostname(mmc);
1554 host->led.brightness_set = sdhci_led_control; 1704 host->led.brightness_set = sdhci_led_control;
1555 1705
1556 ret = led_classdev_register(&pdev->dev, &host->led); 1706 ret = led_classdev_register(mmc_dev(mmc), &host->led);
1557 if (ret) 1707 if (ret)
1558 goto reset; 1708 goto reset;
1559#endif 1709#endif
@@ -1562,8 +1712,9 @@ static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1562 1712
1563 mmc_add_host(mmc); 1713 mmc_add_host(mmc);
1564 1714
1565 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", 1715 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
1566 mmc_hostname(mmc), host->addr, host->irq, 1716 mmc_hostname(mmc), host->hw_name, mmc_dev(mmc)->bus_id,
1717 (host->flags & SDHCI_USE_ADMA)?"A":"",
1567 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO"); 1718 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1568 1719
1569 return 0; 1720 return 0;
@@ -1576,35 +1727,40 @@ reset:
1576untasklet: 1727untasklet:
1577 tasklet_kill(&host->card_tasklet); 1728 tasklet_kill(&host->card_tasklet);
1578 tasklet_kill(&host->finish_tasklet); 1729 tasklet_kill(&host->finish_tasklet);
1579unmap:
1580 iounmap(host->ioaddr);
1581release:
1582 pci_release_region(pdev, host->bar);
1583free:
1584 mmc_free_host(mmc);
1585 1730
1586 return ret; 1731 return ret;
1587} 1732}
1588 1733
1589static void sdhci_remove_slot(struct pci_dev *pdev, int slot) 1734EXPORT_SYMBOL_GPL(sdhci_add_host);
1735
1736void sdhci_remove_host(struct sdhci_host *host, int dead)
1590{ 1737{
1591 struct sdhci_chip *chip; 1738 unsigned long flags;
1592 struct mmc_host *mmc;
1593 struct sdhci_host *host;
1594 1739
1595 chip = pci_get_drvdata(pdev); 1740 if (dead) {
1596 host = chip->hosts[slot]; 1741 spin_lock_irqsave(&host->lock, flags);
1597 mmc = host->mmc; 1742
1743 host->flags |= SDHCI_DEVICE_DEAD;
1744
1745 if (host->mrq) {
1746 printk(KERN_ERR "%s: Controller removed during "
1747 " transfer!\n", mmc_hostname(host->mmc));
1598 1748
1599 chip->hosts[slot] = NULL; 1749 host->mrq->cmd->error = -ENOMEDIUM;
1750 tasklet_schedule(&host->finish_tasklet);
1751 }
1752
1753 spin_unlock_irqrestore(&host->lock, flags);
1754 }
1600 1755
1601 mmc_remove_host(mmc); 1756 mmc_remove_host(host->mmc);
1602 1757
1603#ifdef CONFIG_LEDS_CLASS 1758#ifdef CONFIG_LEDS_CLASS
1604 led_classdev_unregister(&host->led); 1759 led_classdev_unregister(&host->led);
1605#endif 1760#endif
1606 1761
1607 sdhci_reset(host, SDHCI_RESET_ALL); 1762 if (!dead)
1763 sdhci_reset(host, SDHCI_RESET_ALL);
1608 1764
1609 free_irq(host->irq, host); 1765 free_irq(host->irq, host);
1610 1766
@@ -1613,106 +1769,21 @@ static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1613 tasklet_kill(&host->card_tasklet); 1769 tasklet_kill(&host->card_tasklet);
1614 tasklet_kill(&host->finish_tasklet); 1770 tasklet_kill(&host->finish_tasklet);
1615 1771
1616 iounmap(host->ioaddr); 1772 kfree(host->adma_desc);
1617 1773 kfree(host->align_buffer);
1618 pci_release_region(pdev, host->bar);
1619 1774
1620 mmc_free_host(mmc); 1775 host->adma_desc = NULL;
1776 host->align_buffer = NULL;
1621} 1777}
1622 1778
1623static int __devinit sdhci_probe(struct pci_dev *pdev, 1779EXPORT_SYMBOL_GPL(sdhci_remove_host);
1624 const struct pci_device_id *ent)
1625{
1626 int ret, i;
1627 u8 slots, rev;
1628 struct sdhci_chip *chip;
1629
1630 BUG_ON(pdev == NULL);
1631 BUG_ON(ent == NULL);
1632 1780
1633 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev); 1781void sdhci_free_host(struct sdhci_host *host)
1634
1635 printk(KERN_INFO DRIVER_NAME
1636 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1637 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1638 (int)rev);
1639
1640 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1641 if (ret)
1642 return ret;
1643
1644 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1645 DBG("found %d slot(s)\n", slots);
1646 if (slots == 0)
1647 return -ENODEV;
1648
1649 ret = pci_enable_device(pdev);
1650 if (ret)
1651 return ret;
1652
1653 chip = kzalloc(sizeof(struct sdhci_chip) +
1654 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1655 if (!chip) {
1656 ret = -ENOMEM;
1657 goto err;
1658 }
1659
1660 chip->pdev = pdev;
1661 chip->quirks = ent->driver_data;
1662
1663 if (debug_quirks)
1664 chip->quirks = debug_quirks;
1665
1666 chip->num_slots = slots;
1667 pci_set_drvdata(pdev, chip);
1668
1669 for (i = 0;i < slots;i++) {
1670 ret = sdhci_probe_slot(pdev, i);
1671 if (ret) {
1672 for (i--;i >= 0;i--)
1673 sdhci_remove_slot(pdev, i);
1674 goto free;
1675 }
1676 }
1677
1678 return 0;
1679
1680free:
1681 pci_set_drvdata(pdev, NULL);
1682 kfree(chip);
1683
1684err:
1685 pci_disable_device(pdev);
1686 return ret;
1687}
1688
1689static void __devexit sdhci_remove(struct pci_dev *pdev)
1690{ 1782{
1691 int i; 1783 mmc_free_host(host->mmc);
1692 struct sdhci_chip *chip;
1693
1694 chip = pci_get_drvdata(pdev);
1695
1696 if (chip) {
1697 for (i = 0;i < chip->num_slots;i++)
1698 sdhci_remove_slot(pdev, i);
1699
1700 pci_set_drvdata(pdev, NULL);
1701
1702 kfree(chip);
1703 }
1704
1705 pci_disable_device(pdev);
1706} 1784}
1707 1785
1708static struct pci_driver sdhci_driver = { 1786EXPORT_SYMBOL_GPL(sdhci_free_host);
1709 .name = DRIVER_NAME,
1710 .id_table = pci_ids,
1711 .probe = sdhci_probe,
1712 .remove = __devexit_p(sdhci_remove),
1713 .suspend = sdhci_suspend,
1714 .resume = sdhci_resume,
1715};
1716 1787
1717/*****************************************************************************\ 1788/*****************************************************************************\
1718 * * 1789 * *
@@ -1726,14 +1797,11 @@ static int __init sdhci_drv_init(void)
1726 ": Secure Digital Host Controller Interface driver\n"); 1797 ": Secure Digital Host Controller Interface driver\n");
1727 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n"); 1798 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1728 1799
1729 return pci_register_driver(&sdhci_driver); 1800 return 0;
1730} 1801}
1731 1802
1732static void __exit sdhci_drv_exit(void) 1803static void __exit sdhci_drv_exit(void)
1733{ 1804{
1734 DBG("Exiting\n");
1735
1736 pci_unregister_driver(&sdhci_driver);
1737} 1805}
1738 1806
1739module_init(sdhci_drv_init); 1807module_init(sdhci_drv_init);
@@ -1742,7 +1810,7 @@ module_exit(sdhci_drv_exit);
1742module_param(debug_quirks, uint, 0444); 1810module_param(debug_quirks, uint, 0444);
1743 1811
1744MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>"); 1812MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1745MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver"); 1813MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
1746MODULE_LICENSE("GPL"); 1814MODULE_LICENSE("GPL");
1747 1815
1748MODULE_PARM_DESC(debug_quirks, "Force certain quirks."); 1816MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
diff --git a/drivers/mmc/host/sdhci.h b/drivers/mmc/host/sdhci.h
index 299118de8933..5bb355281765 100644
--- a/drivers/mmc/host/sdhci.h
+++ b/drivers/mmc/host/sdhci.h
@@ -10,18 +10,6 @@
10 */ 10 */
11 11
12/* 12/*
13 * PCI registers
14 */
15
16#define PCI_SDHCI_IFPIO 0x00
17#define PCI_SDHCI_IFDMA 0x01
18#define PCI_SDHCI_IFVENDOR 0x02
19
20#define PCI_SLOT_INFO 0x40 /* 8 bits */
21#define PCI_SLOT_INFO_SLOTS(x) ((x >> 4) & 7)
22#define PCI_SLOT_INFO_FIRST_BAR_MASK 0x07
23
24/*
25 * Controller registers 13 * Controller registers
26 */ 14 */
27 15
@@ -72,6 +60,11 @@
72#define SDHCI_CTRL_LED 0x01 60#define SDHCI_CTRL_LED 0x01
73#define SDHCI_CTRL_4BITBUS 0x02 61#define SDHCI_CTRL_4BITBUS 0x02
74#define SDHCI_CTRL_HISPD 0x04 62#define SDHCI_CTRL_HISPD 0x04
63#define SDHCI_CTRL_DMA_MASK 0x18
64#define SDHCI_CTRL_SDMA 0x00
65#define SDHCI_CTRL_ADMA1 0x08
66#define SDHCI_CTRL_ADMA32 0x10
67#define SDHCI_CTRL_ADMA64 0x18
75 68
76#define SDHCI_POWER_CONTROL 0x29 69#define SDHCI_POWER_CONTROL 0x29
77#define SDHCI_POWER_ON 0x01 70#define SDHCI_POWER_ON 0x01
@@ -117,6 +110,7 @@
117#define SDHCI_INT_DATA_END_BIT 0x00400000 110#define SDHCI_INT_DATA_END_BIT 0x00400000
118#define SDHCI_INT_BUS_POWER 0x00800000 111#define SDHCI_INT_BUS_POWER 0x00800000
119#define SDHCI_INT_ACMD12ERR 0x01000000 112#define SDHCI_INT_ACMD12ERR 0x01000000
113#define SDHCI_INT_ADMA_ERROR 0x02000000
120 114
121#define SDHCI_INT_NORMAL_MASK 0x00007FFF 115#define SDHCI_INT_NORMAL_MASK 0x00007FFF
122#define SDHCI_INT_ERROR_MASK 0xFFFF8000 116#define SDHCI_INT_ERROR_MASK 0xFFFF8000
@@ -140,11 +134,14 @@
140#define SDHCI_CLOCK_BASE_SHIFT 8 134#define SDHCI_CLOCK_BASE_SHIFT 8
141#define SDHCI_MAX_BLOCK_MASK 0x00030000 135#define SDHCI_MAX_BLOCK_MASK 0x00030000
142#define SDHCI_MAX_BLOCK_SHIFT 16 136#define SDHCI_MAX_BLOCK_SHIFT 16
137#define SDHCI_CAN_DO_ADMA2 0x00080000
138#define SDHCI_CAN_DO_ADMA1 0x00100000
143#define SDHCI_CAN_DO_HISPD 0x00200000 139#define SDHCI_CAN_DO_HISPD 0x00200000
144#define SDHCI_CAN_DO_DMA 0x00400000 140#define SDHCI_CAN_DO_DMA 0x00400000
145#define SDHCI_CAN_VDD_330 0x01000000 141#define SDHCI_CAN_VDD_330 0x01000000
146#define SDHCI_CAN_VDD_300 0x02000000 142#define SDHCI_CAN_VDD_300 0x02000000
147#define SDHCI_CAN_VDD_180 0x04000000 143#define SDHCI_CAN_VDD_180 0x04000000
144#define SDHCI_CAN_64BIT 0x10000000
148 145
149/* 44-47 reserved for more caps */ 146/* 44-47 reserved for more caps */
150 147
@@ -152,7 +149,16 @@
152 149
153/* 4C-4F reserved for more max current */ 150/* 4C-4F reserved for more max current */
154 151
155/* 50-FB reserved */ 152#define SDHCI_SET_ACMD12_ERROR 0x50
153#define SDHCI_SET_INT_ERROR 0x52
154
155#define SDHCI_ADMA_ERROR 0x54
156
157/* 55-57 reserved */
158
159#define SDHCI_ADMA_ADDRESS 0x58
160
161/* 60-FB reserved */
156 162
157#define SDHCI_SLOT_INT_STATUS 0xFC 163#define SDHCI_SLOT_INT_STATUS 0xFC
158 164
@@ -161,11 +167,50 @@
161#define SDHCI_VENDOR_VER_SHIFT 8 167#define SDHCI_VENDOR_VER_SHIFT 8
162#define SDHCI_SPEC_VER_MASK 0x00FF 168#define SDHCI_SPEC_VER_MASK 0x00FF
163#define SDHCI_SPEC_VER_SHIFT 0 169#define SDHCI_SPEC_VER_SHIFT 0
170#define SDHCI_SPEC_100 0
171#define SDHCI_SPEC_200 1
164 172
165struct sdhci_chip; 173struct sdhci_ops;
166 174
167struct sdhci_host { 175struct sdhci_host {
168 struct sdhci_chip *chip; 176 /* Data set by hardware interface driver */
177 const char *hw_name; /* Hardware bus name */
178
179 unsigned int quirks; /* Deviations from spec. */
180
181/* Controller doesn't honor resets unless we touch the clock register */
182#define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
183/* Controller has bad caps bits, but really supports DMA */
184#define SDHCI_QUIRK_FORCE_DMA (1<<1)
185/* Controller doesn't like to be reset when there is no card inserted. */
186#define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
187/* Controller doesn't like clearing the power reg before a change */
188#define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
189/* Controller has flaky internal state so reset it on each ios change */
190#define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4)
191/* Controller has an unusable DMA engine */
192#define SDHCI_QUIRK_BROKEN_DMA (1<<5)
193/* Controller has an unusable ADMA engine */
194#define SDHCI_QUIRK_BROKEN_ADMA (1<<6)
195/* Controller can only DMA from 32-bit aligned addresses */
196#define SDHCI_QUIRK_32BIT_DMA_ADDR (1<<7)
197/* Controller can only DMA chunk sizes that are a multiple of 32 bits */
198#define SDHCI_QUIRK_32BIT_DMA_SIZE (1<<8)
199/* Controller can only ADMA chunks that are a multiple of 32 bits */
200#define SDHCI_QUIRK_32BIT_ADMA_SIZE (1<<9)
201/* Controller needs to be reset after each request to stay stable */
202#define SDHCI_QUIRK_RESET_AFTER_REQUEST (1<<10)
203/* Controller needs voltage and power writes to happen separately */
204#define SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER (1<<11)
205/* Controller provides an incorrect timeout value for transfers */
206#define SDHCI_QUIRK_BROKEN_TIMEOUT_VAL (1<<12)
207
208 int irq; /* Device IRQ */
209 void __iomem * ioaddr; /* Mapped address */
210
211 const struct sdhci_ops *ops; /* Low level hw interface */
212
213 /* Internal data */
169 struct mmc_host *mmc; /* MMC structure */ 214 struct mmc_host *mmc; /* MMC structure */
170 215
171#ifdef CONFIG_LEDS_CLASS 216#ifdef CONFIG_LEDS_CLASS
@@ -176,7 +221,11 @@ struct sdhci_host {
176 221
177 int flags; /* Host attributes */ 222 int flags; /* Host attributes */
178#define SDHCI_USE_DMA (1<<0) /* Host is DMA capable */ 223#define SDHCI_USE_DMA (1<<0) /* Host is DMA capable */
179#define SDHCI_REQ_USE_DMA (1<<1) /* Use DMA for this req. */ 224#define SDHCI_USE_ADMA (1<<1) /* Host is ADMA capable */
225#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
226#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
227
228 unsigned int version; /* SDHCI spec. version */
180 229
181 unsigned int max_clk; /* Max possible freq (MHz) */ 230 unsigned int max_clk; /* Max possible freq (MHz) */
182 unsigned int timeout_clk; /* Timeout freq (KHz) */ 231 unsigned int timeout_clk; /* Timeout freq (KHz) */
@@ -194,22 +243,41 @@ struct sdhci_host {
194 int offset; /* Offset into current sg */ 243 int offset; /* Offset into current sg */
195 int remain; /* Bytes left in current */ 244 int remain; /* Bytes left in current */
196 245
197 int irq; /* Device IRQ */ 246 int sg_count; /* Mapped sg entries */
198 int bar; /* PCI BAR index */ 247
199 unsigned long addr; /* Bus address */ 248 u8 *adma_desc; /* ADMA descriptor table */
200 void __iomem * ioaddr; /* Mapped address */ 249 u8 *align_buffer; /* Bounce buffer */
250
251 dma_addr_t adma_addr; /* Mapped ADMA descr. table */
252 dma_addr_t align_addr; /* Mapped bounce buffer */
201 253
202 struct tasklet_struct card_tasklet; /* Tasklet structures */ 254 struct tasklet_struct card_tasklet; /* Tasklet structures */
203 struct tasklet_struct finish_tasklet; 255 struct tasklet_struct finish_tasklet;
204 256
205 struct timer_list timer; /* Timer for timeouts */ 257 struct timer_list timer; /* Timer for timeouts */
206};
207 258
208struct sdhci_chip { 259 unsigned long private[0] ____cacheline_aligned;
209 struct pci_dev *pdev; 260};
210 261
211 unsigned long quirks;
212 262
213 int num_slots; /* Slots on controller */ 263struct sdhci_ops {
214 struct sdhci_host *hosts[0]; /* Pointers to hosts */ 264 int (*enable_dma)(struct sdhci_host *host);
215}; 265};
266
267
268extern struct sdhci_host *sdhci_alloc_host(struct device *dev,
269 size_t priv_size);
270extern void sdhci_free_host(struct sdhci_host *host);
271
272static inline void *sdhci_priv(struct sdhci_host *host)
273{
274 return (void *)host->private;
275}
276
277extern int sdhci_add_host(struct sdhci_host *host);
278extern void sdhci_remove_host(struct sdhci_host *host, int dead);
279
280#ifdef CONFIG_PM
281extern int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state);
282extern int sdhci_resume_host(struct sdhci_host *host);
283#endif
diff --git a/drivers/mmc/host/sdricoh_cs.c b/drivers/mmc/host/sdricoh_cs.c
new file mode 100644
index 000000000000..f99e9f721629
--- /dev/null
+++ b/drivers/mmc/host/sdricoh_cs.c
@@ -0,0 +1,575 @@
1/*
2 * sdricoh_cs.c - driver for Ricoh Secure Digital Card Readers that can be
3 * found on some Ricoh RL5c476 II cardbus bridge
4 *
5 * Copyright (C) 2006 - 2008 Sascha Sommer <saschasommer@freenet.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23/*
24#define DEBUG
25#define VERBOSE_DEBUG
26*/
27#include <linux/delay.h>
28#include <linux/highmem.h>
29#include <linux/pci.h>
30#include <linux/ioport.h>
31#include <linux/scatterlist.h>
32#include <linux/version.h>
33
34#include <pcmcia/cs_types.h>
35#include <pcmcia/cs.h>
36#include <pcmcia/cistpl.h>
37#include <pcmcia/ds.h>
38#include <linux/io.h>
39
40#include <linux/mmc/host.h>
41
42#define DRIVER_NAME "sdricoh_cs"
43
44static unsigned int switchlocked;
45
46/* i/o region */
47#define SDRICOH_PCI_REGION 0
48#define SDRICOH_PCI_REGION_SIZE 0x1000
49
50/* registers */
51#define R104_VERSION 0x104
52#define R200_CMD 0x200
53#define R204_CMD_ARG 0x204
54#define R208_DATAIO 0x208
55#define R20C_RESP 0x20c
56#define R21C_STATUS 0x21c
57#define R2E0_INIT 0x2e0
58#define R2E4_STATUS_RESP 0x2e4
59#define R2F0_RESET 0x2f0
60#define R224_MODE 0x224
61#define R226_BLOCKSIZE 0x226
62#define R228_POWER 0x228
63#define R230_DATA 0x230
64
65/* flags for the R21C_STATUS register */
66#define STATUS_CMD_FINISHED 0x00000001
67#define STATUS_TRANSFER_FINISHED 0x00000004
68#define STATUS_CARD_INSERTED 0x00000020
69#define STATUS_CARD_LOCKED 0x00000080
70#define STATUS_CMD_TIMEOUT 0x00400000
71#define STATUS_READY_TO_READ 0x01000000
72#define STATUS_READY_TO_WRITE 0x02000000
73#define STATUS_BUSY 0x40000000
74
75/* timeouts */
76#define INIT_TIMEOUT 100
77#define CMD_TIMEOUT 100000
78#define TRANSFER_TIMEOUT 100000
79#define BUSY_TIMEOUT 32767
80
81/* list of supported pcmcia devices */
82static struct pcmcia_device_id pcmcia_ids[] = {
83 /* vendor and device strings followed by their crc32 hashes */
84 PCMCIA_DEVICE_PROD_ID12("RICOH", "Bay1Controller", 0xd9f522ed,
85 0xc3901202),
86 PCMCIA_DEVICE_NULL,
87};
88
89MODULE_DEVICE_TABLE(pcmcia, pcmcia_ids);
90
91/* mmc privdata */
92struct sdricoh_host {
93 struct device *dev;
94 struct mmc_host *mmc; /* MMC structure */
95 unsigned char __iomem *iobase;
96 struct pci_dev *pci_dev;
97 int app_cmd;
98};
99
100/***************** register i/o helper functions *****************************/
101
102static inline unsigned int sdricoh_readl(struct sdricoh_host *host,
103 unsigned int reg)
104{
105 unsigned int value = readl(host->iobase + reg);
106 dev_vdbg(host->dev, "rl %x 0x%x\n", reg, value);
107 return value;
108}
109
110static inline void sdricoh_writel(struct sdricoh_host *host, unsigned int reg,
111 unsigned int value)
112{
113 writel(value, host->iobase + reg);
114 dev_vdbg(host->dev, "wl %x 0x%x\n", reg, value);
115
116}
117
118static inline unsigned int sdricoh_readw(struct sdricoh_host *host,
119 unsigned int reg)
120{
121 unsigned int value = readw(host->iobase + reg);
122 dev_vdbg(host->dev, "rb %x 0x%x\n", reg, value);
123 return value;
124}
125
126static inline void sdricoh_writew(struct sdricoh_host *host, unsigned int reg,
127 unsigned short value)
128{
129 writew(value, host->iobase + reg);
130 dev_vdbg(host->dev, "ww %x 0x%x\n", reg, value);
131}
132
133static inline unsigned int sdricoh_readb(struct sdricoh_host *host,
134 unsigned int reg)
135{
136 unsigned int value = readb(host->iobase + reg);
137 dev_vdbg(host->dev, "rb %x 0x%x\n", reg, value);
138 return value;
139}
140
141static int sdricoh_query_status(struct sdricoh_host *host, unsigned int wanted,
142 unsigned int timeout){
143 unsigned int loop;
144 unsigned int status = 0;
145 struct device *dev = host->dev;
146 for (loop = 0; loop < timeout; loop++) {
147 status = sdricoh_readl(host, R21C_STATUS);
148 sdricoh_writel(host, R2E4_STATUS_RESP, status);
149 if (status & wanted)
150 break;
151 }
152
153 if (loop == timeout) {
154 dev_err(dev, "query_status: timeout waiting for %x\n", wanted);
155 return -ETIMEDOUT;
156 }
157
158 /* do not do this check in the loop as some commands fail otherwise */
159 if (status & 0x7F0000) {
160 dev_err(dev, "waiting for status bit %x failed\n", wanted);
161 return -EINVAL;
162 }
163 return 0;
164
165}
166
167static int sdricoh_mmc_cmd(struct sdricoh_host *host, unsigned char opcode,
168 unsigned int arg)
169{
170 unsigned int status;
171 int result = 0;
172 unsigned int loop = 0;
173 /* reset status reg? */
174 sdricoh_writel(host, R21C_STATUS, 0x18);
175 /* fill parameters */
176 sdricoh_writel(host, R204_CMD_ARG, arg);
177 sdricoh_writel(host, R200_CMD, (0x10000 << 8) | opcode);
178 /* wait for command completion */
179 if (opcode) {
180 for (loop = 0; loop < CMD_TIMEOUT; loop++) {
181 status = sdricoh_readl(host, R21C_STATUS);
182 sdricoh_writel(host, R2E4_STATUS_RESP, status);
183 if (status & STATUS_CMD_FINISHED)
184 break;
185 }
186 /* don't check for timeout in the loop it is not always
187 reset correctly
188 */
189 if (loop == CMD_TIMEOUT || status & STATUS_CMD_TIMEOUT)
190 result = -ETIMEDOUT;
191
192 }
193
194 return result;
195
196}
197
198static int sdricoh_reset(struct sdricoh_host *host)
199{
200 dev_dbg(host->dev, "reset\n");
201 sdricoh_writel(host, R2F0_RESET, 0x10001);
202 sdricoh_writel(host, R2E0_INIT, 0x10000);
203 if (sdricoh_readl(host, R2E0_INIT) != 0x10000)
204 return -EIO;
205 sdricoh_writel(host, R2E0_INIT, 0x10007);
206
207 sdricoh_writel(host, R224_MODE, 0x2000000);
208 sdricoh_writel(host, R228_POWER, 0xe0);
209
210
211 /* status register ? */
212 sdricoh_writel(host, R21C_STATUS, 0x18);
213
214 return 0;
215}
216
217static int sdricoh_blockio(struct sdricoh_host *host, int read,
218 u8 *buf, int len)
219{
220 int size;
221 u32 data = 0;
222 /* wait until the data is available */
223 if (read) {
224 if (sdricoh_query_status(host, STATUS_READY_TO_READ,
225 TRANSFER_TIMEOUT))
226 return -ETIMEDOUT;
227 sdricoh_writel(host, R21C_STATUS, 0x18);
228 /* read data */
229 while (len) {
230 data = sdricoh_readl(host, R230_DATA);
231 size = min(len, 4);
232 len -= size;
233 while (size) {
234 *buf = data & 0xFF;
235 buf++;
236 data >>= 8;
237 size--;
238 }
239 }
240 } else {
241 if (sdricoh_query_status(host, STATUS_READY_TO_WRITE,
242 TRANSFER_TIMEOUT))
243 return -ETIMEDOUT;
244 sdricoh_writel(host, R21C_STATUS, 0x18);
245 /* write data */
246 while (len) {
247 size = min(len, 4);
248 len -= size;
249 while (size) {
250 data >>= 8;
251 data |= (u32)*buf << 24;
252 buf++;
253 size--;
254 }
255 sdricoh_writel(host, R230_DATA, data);
256 }
257 }
258
259 if (len)
260 return -EIO;
261
262 return 0;
263}
264
265static void sdricoh_request(struct mmc_host *mmc, struct mmc_request *mrq)
266{
267 struct sdricoh_host *host = mmc_priv(mmc);
268 struct mmc_command *cmd = mrq->cmd;
269 struct mmc_data *data = cmd->data;
270 struct device *dev = host->dev;
271 unsigned char opcode = cmd->opcode;
272 int i;
273
274 dev_dbg(dev, "=============================\n");
275 dev_dbg(dev, "sdricoh_request opcode=%i\n", opcode);
276
277 sdricoh_writel(host, R21C_STATUS, 0x18);
278
279 /* MMC_APP_CMDs need some special handling */
280 if (host->app_cmd) {
281 opcode |= 64;
282 host->app_cmd = 0;
283 } else if (opcode == 55)
284 host->app_cmd = 1;
285
286 /* read/write commands seem to require this */
287 if (data) {
288 sdricoh_writew(host, R226_BLOCKSIZE, data->blksz);
289 sdricoh_writel(host, R208_DATAIO, 0);
290 }
291
292 cmd->error = sdricoh_mmc_cmd(host, opcode, cmd->arg);
293
294 /* read response buffer */
295 if (cmd->flags & MMC_RSP_PRESENT) {
296 if (cmd->flags & MMC_RSP_136) {
297 /* CRC is stripped so we need to do some shifting. */
298 for (i = 0; i < 4; i++) {
299 cmd->resp[i] =
300 sdricoh_readl(host,
301 R20C_RESP + (3 - i) * 4) << 8;
302 if (i != 3)
303 cmd->resp[i] |=
304 sdricoh_readb(host, R20C_RESP +
305 (3 - i) * 4 - 1);
306 }
307 } else
308 cmd->resp[0] = sdricoh_readl(host, R20C_RESP);
309 }
310
311 /* transfer data */
312 if (data && cmd->error == 0) {
313 dev_dbg(dev, "transfer: blksz %i blocks %i sg_len %i "
314 "sg length %i\n", data->blksz, data->blocks,
315 data->sg_len, data->sg->length);
316
317 /* enter data reading mode */
318 sdricoh_writel(host, R21C_STATUS, 0x837f031e);
319 for (i = 0; i < data->blocks; i++) {
320 size_t len = data->blksz;
321 u8 *buf;
322 struct page *page;
323 int result;
324 page = sg_page(data->sg);
325
326 buf = kmap(page) + data->sg->offset + (len * i);
327 result =
328 sdricoh_blockio(host,
329 data->flags & MMC_DATA_READ, buf, len);
330 kunmap(page);
331 flush_dcache_page(page);
332 if (result) {
333 dev_err(dev, "sdricoh_request: cmd %i "
334 "block transfer failed\n", cmd->opcode);
335 cmd->error = result;
336 break;
337 } else
338 data->bytes_xfered += len;
339 }
340
341 sdricoh_writel(host, R208_DATAIO, 1);
342
343 if (sdricoh_query_status(host, STATUS_TRANSFER_FINISHED,
344 TRANSFER_TIMEOUT)) {
345 dev_err(dev, "sdricoh_request: transfer end error\n");
346 cmd->error = -EINVAL;
347 }
348 }
349 /* FIXME check busy flag */
350
351 mmc_request_done(mmc, mrq);
352 dev_dbg(dev, "=============================\n");
353}
354
355static void sdricoh_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
356{
357 struct sdricoh_host *host = mmc_priv(mmc);
358 dev_dbg(host->dev, "set_ios\n");
359
360 if (ios->power_mode == MMC_POWER_ON) {
361 sdricoh_writel(host, R228_POWER, 0xc0e0);
362
363 if (ios->bus_width == MMC_BUS_WIDTH_4) {
364 sdricoh_writel(host, R224_MODE, 0x2000300);
365 sdricoh_writel(host, R228_POWER, 0x40e0);
366 } else {
367 sdricoh_writel(host, R224_MODE, 0x2000340);
368 }
369
370 } else if (ios->power_mode == MMC_POWER_UP) {
371 sdricoh_writel(host, R224_MODE, 0x2000320);
372 sdricoh_writel(host, R228_POWER, 0xe0);
373 }
374}
375
376static int sdricoh_get_ro(struct mmc_host *mmc)
377{
378 struct sdricoh_host *host = mmc_priv(mmc);
379 unsigned int status;
380
381 status = sdricoh_readl(host, R21C_STATUS);
382 sdricoh_writel(host, R2E4_STATUS_RESP, status);
383
384 /* some notebooks seem to have the locked flag switched */
385 if (switchlocked)
386 return !(status & STATUS_CARD_LOCKED);
387
388 return (status & STATUS_CARD_LOCKED);
389}
390
391static struct mmc_host_ops sdricoh_ops = {
392 .request = sdricoh_request,
393 .set_ios = sdricoh_set_ios,
394 .get_ro = sdricoh_get_ro,
395};
396
397/* initialize the control and register it to the mmc framework */
398static int sdricoh_init_mmc(struct pci_dev *pci_dev,
399 struct pcmcia_device *pcmcia_dev)
400{
401 int result = 0;
402 void __iomem *iobase = NULL;
403 struct mmc_host *mmc = NULL;
404 struct sdricoh_host *host = NULL;
405 struct device *dev = &pcmcia_dev->dev;
406 /* map iomem */
407 if (pci_resource_len(pci_dev, SDRICOH_PCI_REGION) !=
408 SDRICOH_PCI_REGION_SIZE) {
409 dev_dbg(dev, "unexpected pci resource len\n");
410 return -ENODEV;
411 }
412 iobase =
413 pci_iomap(pci_dev, SDRICOH_PCI_REGION, SDRICOH_PCI_REGION_SIZE);
414 if (!iobase) {
415 dev_err(dev, "unable to map iobase\n");
416 return -ENODEV;
417 }
418 /* check version? */
419 if (readl(iobase + R104_VERSION) != 0x4000) {
420 dev_dbg(dev, "no supported mmc controller found\n");
421 result = -ENODEV;
422 goto err;
423 }
424 /* allocate privdata */
425 mmc = pcmcia_dev->priv =
426 mmc_alloc_host(sizeof(struct sdricoh_host), &pcmcia_dev->dev);
427 if (!mmc) {
428 dev_err(dev, "mmc_alloc_host failed\n");
429 result = -ENOMEM;
430 goto err;
431 }
432 host = mmc_priv(mmc);
433
434 host->iobase = iobase;
435 host->dev = dev;
436 host->pci_dev = pci_dev;
437
438 mmc->ops = &sdricoh_ops;
439
440 /* FIXME: frequency and voltage handling is done by the controller
441 */
442 mmc->f_min = 450000;
443 mmc->f_max = 24000000;
444 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
445 mmc->caps |= MMC_CAP_4_BIT_DATA;
446
447 mmc->max_seg_size = 1024 * 512;
448 mmc->max_blk_size = 512;
449
450 /* reset the controler */
451 if (sdricoh_reset(host)) {
452 dev_dbg(dev, "could not reset\n");
453 result = -EIO;
454 goto err;
455
456 }
457
458 result = mmc_add_host(mmc);
459
460 if (!result) {
461 dev_dbg(dev, "mmc host registered\n");
462 return 0;
463 }
464
465err:
466 if (iobase)
467 iounmap(iobase);
468 if (mmc)
469 mmc_free_host(mmc);
470
471 return result;
472}
473
474/* search for supported mmc controllers */
475static int sdricoh_pcmcia_probe(struct pcmcia_device *pcmcia_dev)
476{
477 struct pci_dev *pci_dev = NULL;
478
479 dev_info(&pcmcia_dev->dev, "Searching MMC controller for pcmcia device"
480 " %s %s ...\n", pcmcia_dev->prod_id[0], pcmcia_dev->prod_id[1]);
481
482 /* search pci cardbus bridge that contains the mmc controler */
483 /* the io region is already claimed by yenta_socket... */
484 while ((pci_dev =
485 pci_get_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_RL5C476,
486 pci_dev))) {
487 /* try to init the device */
488 if (!sdricoh_init_mmc(pci_dev, pcmcia_dev)) {
489 dev_info(&pcmcia_dev->dev, "MMC controller found\n");
490 return 0;
491 }
492
493 }
494 dev_err(&pcmcia_dev->dev, "No MMC controller was found.\n");
495 return -ENODEV;
496}
497
498static void sdricoh_pcmcia_detach(struct pcmcia_device *link)
499{
500 struct mmc_host *mmc = link->priv;
501
502 dev_dbg(&link->dev, "detach\n");
503
504 /* remove mmc host */
505 if (mmc) {
506 struct sdricoh_host *host = mmc_priv(mmc);
507 mmc_remove_host(mmc);
508 pci_iounmap(host->pci_dev, host->iobase);
509 pci_dev_put(host->pci_dev);
510 mmc_free_host(mmc);
511 }
512 pcmcia_disable_device(link);
513
514}
515
516#ifdef CONFIG_PM
517static int sdricoh_pcmcia_suspend(struct pcmcia_device *link)
518{
519 struct mmc_host *mmc = link->priv;
520 dev_dbg(&link->dev, "suspend\n");
521 mmc_suspend_host(mmc, PMSG_SUSPEND);
522 return 0;
523}
524
525static int sdricoh_pcmcia_resume(struct pcmcia_device *link)
526{
527 struct mmc_host *mmc = link->priv;
528 dev_dbg(&link->dev, "resume\n");
529 sdricoh_reset(mmc_priv(mmc));
530 mmc_resume_host(mmc);
531 return 0;
532}
533#else
534#define sdricoh_pcmcia_suspend NULL
535#define sdricoh_pcmcia_resume NULL
536#endif
537
538static struct pcmcia_driver sdricoh_driver = {
539 .drv = {
540 .name = DRIVER_NAME,
541 },
542 .probe = sdricoh_pcmcia_probe,
543 .remove = sdricoh_pcmcia_detach,
544 .id_table = pcmcia_ids,
545 .suspend = sdricoh_pcmcia_suspend,
546 .resume = sdricoh_pcmcia_resume,
547};
548
549/*****************************************************************************\
550 * *
551 * Driver init/exit *
552 * *
553\*****************************************************************************/
554
555static int __init sdricoh_drv_init(void)
556{
557 return pcmcia_register_driver(&sdricoh_driver);
558}
559
560static void __exit sdricoh_drv_exit(void)
561{
562 pcmcia_unregister_driver(&sdricoh_driver);
563}
564
565module_init(sdricoh_drv_init);
566module_exit(sdricoh_drv_exit);
567
568module_param(switchlocked, uint, 0444);
569
570MODULE_AUTHOR("Sascha Sommer <saschasommer@freenet.de>");
571MODULE_DESCRIPTION("Ricoh PCMCIA Secure Digital Interface driver");
572MODULE_LICENSE("GPL");
573
574MODULE_PARM_DESC(switchlocked, "Switch the cards locked status."
575 "Use this when unlocked cards are shown readonly (default 0)");
diff --git a/drivers/mmc/host/tifm_sd.c b/drivers/mmc/host/tifm_sd.c
index 1c14a186f000..13844843e8de 100644
--- a/drivers/mmc/host/tifm_sd.c
+++ b/drivers/mmc/host/tifm_sd.c
@@ -973,7 +973,7 @@ static int tifm_sd_probe(struct tifm_dev *sock)
973 973
974 mmc->ops = &tifm_sd_ops; 974 mmc->ops = &tifm_sd_ops;
975 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 975 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
976 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE; 976 mmc->caps = MMC_CAP_4_BIT_DATA;
977 mmc->f_min = 20000000 / 60; 977 mmc->f_min = 20000000 / 60;
978 mmc->f_max = 24000000; 978 mmc->f_max = 24000000;
979 979
diff --git a/drivers/mmc/host/wbsd.c b/drivers/mmc/host/wbsd.c
index c303e7f57ab4..adda37952032 100644
--- a/drivers/mmc/host/wbsd.c
+++ b/drivers/mmc/host/wbsd.c
@@ -68,16 +68,16 @@ static const int unlock_codes[] = { 0x83, 0x87 };
68 68
69static const int valid_ids[] = { 69static const int valid_ids[] = {
70 0x7112, 70 0x7112,
71 }; 71};
72 72
73#ifdef CONFIG_PNP 73#ifdef CONFIG_PNP
74static unsigned int nopnp = 0; 74static unsigned int param_nopnp = 0;
75#else 75#else
76static const unsigned int nopnp = 1; 76static const unsigned int param_nopnp = 1;
77#endif 77#endif
78static unsigned int io = 0x248; 78static unsigned int param_io = 0x248;
79static unsigned int irq = 6; 79static unsigned int param_irq = 6;
80static int dma = 2; 80static int param_dma = 2;
81 81
82/* 82/*
83 * Basic functions 83 * Basic functions
@@ -939,7 +939,7 @@ static int wbsd_get_ro(struct mmc_host *mmc)
939 939
940 spin_unlock_bh(&host->lock); 940 spin_unlock_bh(&host->lock);
941 941
942 return csr & WBSD_WRPT; 942 return !!(csr & WBSD_WRPT);
943} 943}
944 944
945static const struct mmc_host_ops wbsd_ops = { 945static const struct mmc_host_ops wbsd_ops = {
@@ -1219,7 +1219,7 @@ static int __devinit wbsd_alloc_mmc(struct device *dev)
1219 mmc->f_min = 375000; 1219 mmc->f_min = 375000;
1220 mmc->f_max = 24000000; 1220 mmc->f_max = 24000000;
1221 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; 1221 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1222 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE; 1222 mmc->caps = MMC_CAP_4_BIT_DATA;
1223 1223
1224 spin_lock_init(&host->lock); 1224 spin_lock_init(&host->lock);
1225 1225
@@ -1420,7 +1420,7 @@ kfree:
1420 1420
1421 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr, 1421 dma_unmap_single(mmc_dev(host->mmc), host->dma_addr,
1422 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL); 1422 WBSD_DMA_SIZE, DMA_BIDIRECTIONAL);
1423 host->dma_addr = (dma_addr_t)NULL; 1423 host->dma_addr = 0;
1424 1424
1425 kfree(host->dma_buffer); 1425 kfree(host->dma_buffer);
1426 host->dma_buffer = NULL; 1426 host->dma_buffer = NULL;
@@ -1445,7 +1445,7 @@ static void wbsd_release_dma(struct wbsd_host *host)
1445 1445
1446 host->dma = -1; 1446 host->dma = -1;
1447 host->dma_buffer = NULL; 1447 host->dma_buffer = NULL;
1448 host->dma_addr = (dma_addr_t)NULL; 1448 host->dma_addr = 0;
1449} 1449}
1450 1450
1451/* 1451/*
@@ -1765,7 +1765,7 @@ static void __devexit wbsd_shutdown(struct device *dev, int pnp)
1765static int __devinit wbsd_probe(struct platform_device *dev) 1765static int __devinit wbsd_probe(struct platform_device *dev)
1766{ 1766{
1767 /* Use the module parameters for resources */ 1767 /* Use the module parameters for resources */
1768 return wbsd_init(&dev->dev, io, irq, dma, 0); 1768 return wbsd_init(&dev->dev, param_io, param_irq, param_dma, 0);
1769} 1769}
1770 1770
1771static int __devexit wbsd_remove(struct platform_device *dev) 1771static int __devexit wbsd_remove(struct platform_device *dev)
@@ -1979,14 +1979,14 @@ static int __init wbsd_drv_init(void)
1979 1979
1980#ifdef CONFIG_PNP 1980#ifdef CONFIG_PNP
1981 1981
1982 if (!nopnp) { 1982 if (!param_nopnp) {
1983 result = pnp_register_driver(&wbsd_pnp_driver); 1983 result = pnp_register_driver(&wbsd_pnp_driver);
1984 if (result < 0) 1984 if (result < 0)
1985 return result; 1985 return result;
1986 } 1986 }
1987#endif /* CONFIG_PNP */ 1987#endif /* CONFIG_PNP */
1988 1988
1989 if (nopnp) { 1989 if (param_nopnp) {
1990 result = platform_driver_register(&wbsd_driver); 1990 result = platform_driver_register(&wbsd_driver);
1991 if (result < 0) 1991 if (result < 0)
1992 return result; 1992 return result;
@@ -2012,12 +2012,12 @@ static void __exit wbsd_drv_exit(void)
2012{ 2012{
2013#ifdef CONFIG_PNP 2013#ifdef CONFIG_PNP
2014 2014
2015 if (!nopnp) 2015 if (!param_nopnp)
2016 pnp_unregister_driver(&wbsd_pnp_driver); 2016 pnp_unregister_driver(&wbsd_pnp_driver);
2017 2017
2018#endif /* CONFIG_PNP */ 2018#endif /* CONFIG_PNP */
2019 2019
2020 if (nopnp) { 2020 if (param_nopnp) {
2021 platform_device_unregister(wbsd_device); 2021 platform_device_unregister(wbsd_device);
2022 2022
2023 platform_driver_unregister(&wbsd_driver); 2023 platform_driver_unregister(&wbsd_driver);
@@ -2029,11 +2029,11 @@ static void __exit wbsd_drv_exit(void)
2029module_init(wbsd_drv_init); 2029module_init(wbsd_drv_init);
2030module_exit(wbsd_drv_exit); 2030module_exit(wbsd_drv_exit);
2031#ifdef CONFIG_PNP 2031#ifdef CONFIG_PNP
2032module_param(nopnp, uint, 0444); 2032module_param_named(nopnp, param_nopnp, uint, 0444);
2033#endif 2033#endif
2034module_param(io, uint, 0444); 2034module_param_named(io, param_io, uint, 0444);
2035module_param(irq, uint, 0444); 2035module_param_named(irq, param_irq, uint, 0444);
2036module_param(dma, int, 0444); 2036module_param_named(dma, param_dma, int, 0444);
2037 2037
2038MODULE_LICENSE("GPL"); 2038MODULE_LICENSE("GPL");
2039MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>"); 2039MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
diff --git a/drivers/net/wireless/libertas/if_sdio.c b/drivers/net/wireless/libertas/if_sdio.c
index 3dd537be87d8..b54e2ea8346b 100644
--- a/drivers/net/wireless/libertas/if_sdio.c
+++ b/drivers/net/wireless/libertas/if_sdio.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * linux/drivers/net/wireless/libertas/if_sdio.c 2 * linux/drivers/net/wireless/libertas/if_sdio.c
3 * 3 *
4 * Copyright 2007 Pierre Ossman 4 * Copyright 2007-2008 Pierre Ossman
5 * 5 *
6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig 6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
7 * 7 *
@@ -266,13 +266,10 @@ static int if_sdio_card_to_host(struct if_sdio_card *card)
266 266
267 /* 267 /*
268 * The transfer must be in one transaction or the firmware 268 * The transfer must be in one transaction or the firmware
269 * goes suicidal. 269 * goes suicidal. There's no way to guarantee that for all
270 * controllers, but we can at least try.
270 */ 271 */
271 chunk = size; 272 chunk = sdio_align_size(card->func, size);
272 if ((chunk > card->func->cur_blksize) || (chunk > 512)) {
273 chunk = (chunk + card->func->cur_blksize - 1) /
274 card->func->cur_blksize * card->func->cur_blksize;
275 }
276 273
277 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk); 274 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
278 if (ret) 275 if (ret)
@@ -696,13 +693,10 @@ static int if_sdio_host_to_card(struct lbs_private *priv,
696 693
697 /* 694 /*
698 * The transfer must be in one transaction or the firmware 695 * The transfer must be in one transaction or the firmware
699 * goes suicidal. 696 * goes suicidal. There's no way to guarantee that for all
697 * controllers, but we can at least try.
700 */ 698 */
701 size = nb + 4; 699 size = sdio_align_size(card->func, nb + 4);
702 if ((size > card->func->cur_blksize) || (size > 512)) {
703 size = (size + card->func->cur_blksize - 1) /
704 card->func->cur_blksize * card->func->cur_blksize;
705 }
706 700
707 packet = kzalloc(sizeof(struct if_sdio_packet) + size, 701 packet = kzalloc(sizeof(struct if_sdio_packet) + size,
708 GFP_ATOMIC); 702 GFP_ATOMIC);
diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
index 4d1ce2e7361e..7d63f8ced24b 100644
--- a/drivers/pci/Makefile
+++ b/drivers/pci/Makefile
@@ -2,7 +2,7 @@
2# Makefile for the PCI bus specific drivers. 2# Makefile for the PCI bus specific drivers.
3# 3#
4 4
5obj-y += access.o bus.o probe.o remove.o pci.o quirks.o \ 5obj-y += access.o bus.o probe.o remove.o pci.o quirks.o slot.o \
6 pci-driver.o search.o pci-sysfs.o rom.o setup-res.o 6 pci-driver.o search.o pci-sysfs.o rom.o setup-res.o
7obj-$(CONFIG_PROC_FS) += proc.o 7obj-$(CONFIG_PROC_FS) += proc.o
8 8
diff --git a/drivers/pci/hotplug/acpi_pcihp.c b/drivers/pci/hotplug/acpi_pcihp.c
index f8c187a763bd..93e37f0666ab 100644
--- a/drivers/pci/hotplug/acpi_pcihp.c
+++ b/drivers/pci/hotplug/acpi_pcihp.c
@@ -30,6 +30,7 @@
30#include <linux/types.h> 30#include <linux/types.h>
31#include <linux/pci.h> 31#include <linux/pci.h>
32#include <linux/pci_hotplug.h> 32#include <linux/pci_hotplug.h>
33#include <linux/pci-acpi.h>
33#include <acpi/acpi.h> 34#include <acpi/acpi.h>
34#include <acpi/acpi_bus.h> 35#include <acpi/acpi_bus.h>
35#include <acpi/actypes.h> 36#include <acpi/actypes.h>
@@ -299,7 +300,7 @@ free_and_return:
299 * 300 *
300 * @handle - the handle of the hotplug controller. 301 * @handle - the handle of the hotplug controller.
301 */ 302 */
302acpi_status acpi_run_oshp(acpi_handle handle) 303static acpi_status acpi_run_oshp(acpi_handle handle)
303{ 304{
304 acpi_status status; 305 acpi_status status;
305 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL }; 306 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
@@ -322,9 +323,6 @@ acpi_status acpi_run_oshp(acpi_handle handle)
322 kfree(string.pointer); 323 kfree(string.pointer);
323 return status; 324 return status;
324} 325}
325EXPORT_SYMBOL_GPL(acpi_run_oshp);
326
327
328 326
329/* acpi_get_hp_params_from_firmware 327/* acpi_get_hp_params_from_firmware
330 * 328 *
@@ -374,6 +372,85 @@ acpi_status acpi_get_hp_params_from_firmware(struct pci_bus *bus,
374} 372}
375EXPORT_SYMBOL_GPL(acpi_get_hp_params_from_firmware); 373EXPORT_SYMBOL_GPL(acpi_get_hp_params_from_firmware);
376 374
375/**
376 * acpi_get_hp_hw_control_from_firmware
377 * @dev: the pci_dev of the bridge that has a hotplug controller
378 * @flags: requested control bits for _OSC
379 *
380 * Attempt to take hotplug control from firmware.
381 */
382int acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev, u32 flags)
383{
384 acpi_status status;
385 acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
386 struct pci_dev *pdev = dev;
387 struct pci_bus *parent;
388 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
389
390 flags &= (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL |
391 OSC_SHPC_NATIVE_HP_CONTROL |
392 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
393 if (!flags) {
394 err("Invalid flags %u specified!\n", flags);
395 return -EINVAL;
396 }
397
398 /*
399 * Per PCI firmware specification, we should run the ACPI _OSC
400 * method to get control of hotplug hardware before using it. If
401 * an _OSC is missing, we look for an OSHP to do the same thing.
402 * To handle different BIOS behavior, we look for _OSC and OSHP
403 * within the scope of the hotplug controller and its parents,
404 * upto the host bridge under which this controller exists.
405 */
406 while (!handle) {
407 /*
408 * This hotplug controller was not listed in the ACPI name
409 * space at all. Try to get acpi handle of parent pci bus.
410 */
411 if (!pdev || !pdev->bus->parent)
412 break;
413 parent = pdev->bus->parent;
414 dbg("Could not find %s in acpi namespace, trying parent\n",
415 pci_name(pdev));
416 if (!parent->self)
417 /* Parent must be a host bridge */
418 handle = acpi_get_pci_rootbridge_handle(
419 pci_domain_nr(parent),
420 parent->number);
421 else
422 handle = DEVICE_ACPI_HANDLE(&(parent->self->dev));
423 pdev = parent->self;
424 }
425
426 while (handle) {
427 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
428 dbg("Trying to get hotplug control for %s \n",
429 (char *)string.pointer);
430 status = pci_osc_control_set(handle, flags);
431 if (status == AE_NOT_FOUND)
432 status = acpi_run_oshp(handle);
433 if (ACPI_SUCCESS(status)) {
434 dbg("Gained control for hotplug HW for pci %s (%s)\n",
435 pci_name(dev), (char *)string.pointer);
436 kfree(string.pointer);
437 return 0;
438 }
439 if (acpi_root_bridge(handle))
440 break;
441 chandle = handle;
442 status = acpi_get_parent(chandle, &handle);
443 if (ACPI_FAILURE(status))
444 break;
445 }
446
447 dbg("Cannot get control of hotplug hardware for pci %s\n",
448 pci_name(dev));
449
450 kfree(string.pointer);
451 return -ENODEV;
452}
453EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
377 454
378/* acpi_root_bridge - check to see if this acpi object is a root bridge 455/* acpi_root_bridge - check to see if this acpi object is a root bridge
379 * 456 *
diff --git a/drivers/pci/hotplug/acpiphp.h b/drivers/pci/hotplug/acpiphp.h
index 7a29164d4b32..eecf7cbf4139 100644
--- a/drivers/pci/hotplug/acpiphp.h
+++ b/drivers/pci/hotplug/acpiphp.h
@@ -215,7 +215,6 @@ extern u8 acpiphp_get_power_status (struct acpiphp_slot *slot);
215extern u8 acpiphp_get_attention_status (struct acpiphp_slot *slot); 215extern u8 acpiphp_get_attention_status (struct acpiphp_slot *slot);
216extern u8 acpiphp_get_latch_status (struct acpiphp_slot *slot); 216extern u8 acpiphp_get_latch_status (struct acpiphp_slot *slot);
217extern u8 acpiphp_get_adapter_status (struct acpiphp_slot *slot); 217extern u8 acpiphp_get_adapter_status (struct acpiphp_slot *slot);
218extern u32 acpiphp_get_address (struct acpiphp_slot *slot);
219 218
220/* variables */ 219/* variables */
221extern int acpiphp_debug; 220extern int acpiphp_debug;
diff --git a/drivers/pci/hotplug/acpiphp_core.c b/drivers/pci/hotplug/acpiphp_core.c
index 7af68ba27903..0e496e866a84 100644
--- a/drivers/pci/hotplug/acpiphp_core.c
+++ b/drivers/pci/hotplug/acpiphp_core.c
@@ -70,7 +70,6 @@ static int disable_slot (struct hotplug_slot *slot);
70static int set_attention_status (struct hotplug_slot *slot, u8 value); 70static int set_attention_status (struct hotplug_slot *slot, u8 value);
71static int get_power_status (struct hotplug_slot *slot, u8 *value); 71static int get_power_status (struct hotplug_slot *slot, u8 *value);
72static int get_attention_status (struct hotplug_slot *slot, u8 *value); 72static int get_attention_status (struct hotplug_slot *slot, u8 *value);
73static int get_address (struct hotplug_slot *slot, u32 *value);
74static int get_latch_status (struct hotplug_slot *slot, u8 *value); 73static int get_latch_status (struct hotplug_slot *slot, u8 *value);
75static int get_adapter_status (struct hotplug_slot *slot, u8 *value); 74static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
76 75
@@ -83,7 +82,6 @@ static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
83 .get_attention_status = get_attention_status, 82 .get_attention_status = get_attention_status,
84 .get_latch_status = get_latch_status, 83 .get_latch_status = get_latch_status,
85 .get_adapter_status = get_adapter_status, 84 .get_adapter_status = get_adapter_status,
86 .get_address = get_address,
87}; 85};
88 86
89 87
@@ -274,23 +272,6 @@ static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
274 return 0; 272 return 0;
275} 273}
276 274
277
278/**
279 * get_address - get pci address of a slot
280 * @hotplug_slot: slot to get status
281 * @value: pointer to struct pci_busdev (seg, bus, dev)
282 */
283static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
284{
285 struct slot *slot = hotplug_slot->private;
286
287 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
288
289 *value = acpiphp_get_address(slot->acpi_slot);
290
291 return 0;
292}
293
294static int __init init_acpi(void) 275static int __init init_acpi(void)
295{ 276{
296 int retval; 277 int retval;
@@ -357,7 +338,11 @@ int acpiphp_register_hotplug_slot(struct acpiphp_slot *acpiphp_slot)
357 acpiphp_slot->slot = slot; 338 acpiphp_slot->slot = slot;
358 snprintf(slot->name, sizeof(slot->name), "%u", slot->acpi_slot->sun); 339 snprintf(slot->name, sizeof(slot->name), "%u", slot->acpi_slot->sun);
359 340
360 retval = pci_hp_register(slot->hotplug_slot); 341 retval = pci_hp_register(slot->hotplug_slot,
342 acpiphp_slot->bridge->pci_bus,
343 acpiphp_slot->device);
344 if (retval == -EBUSY)
345 goto error_hpslot;
361 if (retval) { 346 if (retval) {
362 err("pci_hp_register failed with error %d\n", retval); 347 err("pci_hp_register failed with error %d\n", retval);
363 goto error_hpslot; 348 goto error_hpslot;
diff --git a/drivers/pci/hotplug/acpiphp_glue.c b/drivers/pci/hotplug/acpiphp_glue.c
index 91156f85a926..a3e4705dd8f0 100644
--- a/drivers/pci/hotplug/acpiphp_glue.c
+++ b/drivers/pci/hotplug/acpiphp_glue.c
@@ -258,7 +258,12 @@ register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
258 bridge->pci_bus->number, slot->device); 258 bridge->pci_bus->number, slot->device);
259 retval = acpiphp_register_hotplug_slot(slot); 259 retval = acpiphp_register_hotplug_slot(slot);
260 if (retval) { 260 if (retval) {
261 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval); 261 if (retval == -EBUSY)
262 warn("Slot %d already registered by another "
263 "hotplug driver\n", slot->sun);
264 else
265 warn("acpiphp_register_hotplug_slot failed "
266 "(err code = 0x%x)\n", retval);
262 goto err_exit; 267 goto err_exit;
263 } 268 }
264 } 269 }
@@ -1878,19 +1883,3 @@ u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1878 1883
1879 return (sta == 0) ? 0 : 1; 1884 return (sta == 0) ? 0 : 1;
1880} 1885}
1881
1882
1883/*
1884 * pci address (seg/bus/dev)
1885 */
1886u32 acpiphp_get_address(struct acpiphp_slot *slot)
1887{
1888 u32 address;
1889 struct pci_bus *pci_bus = slot->bridge->pci_bus;
1890
1891 address = (pci_domain_nr(pci_bus) << 16) |
1892 (pci_bus->number << 8) |
1893 slot->device;
1894
1895 return address;
1896}
diff --git a/drivers/pci/hotplug/acpiphp_ibm.c b/drivers/pci/hotplug/acpiphp_ibm.c
index ede9051fdb5d..2b7c45e39370 100644
--- a/drivers/pci/hotplug/acpiphp_ibm.c
+++ b/drivers/pci/hotplug/acpiphp_ibm.c
@@ -33,8 +33,10 @@
33#include <linux/kobject.h> 33#include <linux/kobject.h>
34#include <asm/uaccess.h> 34#include <asm/uaccess.h>
35#include <linux/moduleparam.h> 35#include <linux/moduleparam.h>
36#include <linux/pci.h>
36 37
37#include "acpiphp.h" 38#include "acpiphp.h"
39#include "../pci.h"
38 40
39#define DRIVER_VERSION "1.0.1" 41#define DRIVER_VERSION "1.0.1"
40#define DRIVER_AUTHOR "Irene Zubarev <zubarev@us.ibm.com>, Vernon Mauery <vernux@us.ibm.com>" 42#define DRIVER_AUTHOR "Irene Zubarev <zubarev@us.ibm.com>, Vernon Mauery <vernux@us.ibm.com>"
@@ -430,7 +432,7 @@ static int __init ibm_acpiphp_init(void)
430 int retval = 0; 432 int retval = 0;
431 acpi_status status; 433 acpi_status status;
432 struct acpi_device *device; 434 struct acpi_device *device;
433 struct kobject *sysdir = &pci_hotplug_slots_kset->kobj; 435 struct kobject *sysdir = &pci_slots_kset->kobj;
434 436
435 dbg("%s\n", __func__); 437 dbg("%s\n", __func__);
436 438
@@ -477,7 +479,7 @@ init_return:
477static void __exit ibm_acpiphp_exit(void) 479static void __exit ibm_acpiphp_exit(void)
478{ 480{
479 acpi_status status; 481 acpi_status status;
480 struct kobject *sysdir = &pci_hotplug_slots_kset->kobj; 482 struct kobject *sysdir = &pci_slots_kset->kobj;
481 483
482 dbg("%s\n", __func__); 484 dbg("%s\n", __func__);
483 485
diff --git a/drivers/pci/hotplug/cpci_hotplug_core.c b/drivers/pci/hotplug/cpci_hotplug_core.c
index d8a6b80ab42a..935947991dc9 100644
--- a/drivers/pci/hotplug/cpci_hotplug_core.c
+++ b/drivers/pci/hotplug/cpci_hotplug_core.c
@@ -285,7 +285,7 @@ cpci_hp_register_bus(struct pci_bus *bus, u8 first, u8 last)
285 info->attention_status = cpci_get_attention_status(slot); 285 info->attention_status = cpci_get_attention_status(slot);
286 286
287 dbg("registering slot %s", slot->hotplug_slot->name); 287 dbg("registering slot %s", slot->hotplug_slot->name);
288 status = pci_hp_register(slot->hotplug_slot); 288 status = pci_hp_register(slot->hotplug_slot, bus, i);
289 if (status) { 289 if (status) {
290 err("pci_hp_register failed with error %d", status); 290 err("pci_hp_register failed with error %d", status);
291 goto error_name; 291 goto error_name;
diff --git a/drivers/pci/hotplug/cpqphp_core.c b/drivers/pci/hotplug/cpqphp_core.c
index 36b115b27b0b..54defec51d08 100644
--- a/drivers/pci/hotplug/cpqphp_core.c
+++ b/drivers/pci/hotplug/cpqphp_core.c
@@ -434,7 +434,9 @@ static int ctrl_slot_setup(struct controller *ctrl,
434 slot->bus, slot->device, 434 slot->bus, slot->device,
435 slot->number, ctrl->slot_device_offset, 435 slot->number, ctrl->slot_device_offset,
436 slot_number); 436 slot_number);
437 result = pci_hp_register(hotplug_slot); 437 result = pci_hp_register(hotplug_slot,
438 ctrl->pci_dev->subordinate,
439 slot->device);
438 if (result) { 440 if (result) {
439 err("pci_hp_register failed with error %d\n", result); 441 err("pci_hp_register failed with error %d\n", result);
440 goto error_name; 442 goto error_name;
diff --git a/drivers/pci/hotplug/fakephp.c b/drivers/pci/hotplug/fakephp.c
index 7e9a827c2687..40337a06c18a 100644
--- a/drivers/pci/hotplug/fakephp.c
+++ b/drivers/pci/hotplug/fakephp.c
@@ -66,6 +66,7 @@ struct dummy_slot {
66 struct pci_dev *dev; 66 struct pci_dev *dev;
67 struct work_struct remove_work; 67 struct work_struct remove_work;
68 unsigned long removed; 68 unsigned long removed;
69 char name[8];
69}; 70};
70 71
71static int debug; 72static int debug;
@@ -100,6 +101,7 @@ static int add_slot(struct pci_dev *dev)
100 struct dummy_slot *dslot; 101 struct dummy_slot *dslot;
101 struct hotplug_slot *slot; 102 struct hotplug_slot *slot;
102 int retval = -ENOMEM; 103 int retval = -ENOMEM;
104 static int count = 1;
103 105
104 slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL); 106 slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
105 if (!slot) 107 if (!slot)
@@ -113,18 +115,18 @@ static int add_slot(struct pci_dev *dev)
113 slot->info->max_bus_speed = PCI_SPEED_UNKNOWN; 115 slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
114 slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN; 116 slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
115 117
116 slot->name = &dev->dev.bus_id[0];
117 dbg("slot->name = %s\n", slot->name);
118
119 dslot = kzalloc(sizeof(struct dummy_slot), GFP_KERNEL); 118 dslot = kzalloc(sizeof(struct dummy_slot), GFP_KERNEL);
120 if (!dslot) 119 if (!dslot)
121 goto error_info; 120 goto error_info;
122 121
122 slot->name = dslot->name;
123 snprintf(slot->name, sizeof(dslot->name), "fake%d", count++);
124 dbg("slot->name = %s\n", slot->name);
123 slot->ops = &dummy_hotplug_slot_ops; 125 slot->ops = &dummy_hotplug_slot_ops;
124 slot->release = &dummy_release; 126 slot->release = &dummy_release;
125 slot->private = dslot; 127 slot->private = dslot;
126 128
127 retval = pci_hp_register(slot); 129 retval = pci_hp_register(slot, dev->bus, PCI_SLOT(dev->devfn));
128 if (retval) { 130 if (retval) {
129 err("pci_hp_register failed with error %d\n", retval); 131 err("pci_hp_register failed with error %d\n", retval);
130 goto error_dslot; 132 goto error_dslot;
@@ -148,17 +150,17 @@ error:
148static int __init pci_scan_buses(void) 150static int __init pci_scan_buses(void)
149{ 151{
150 struct pci_dev *dev = NULL; 152 struct pci_dev *dev = NULL;
151 int retval = 0; 153 int lastslot = 0;
152 154
153 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { 155 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
154 retval = add_slot(dev); 156 if (PCI_FUNC(dev->devfn) > 0 &&
155 if (retval) { 157 lastslot == PCI_SLOT(dev->devfn))
156 pci_dev_put(dev); 158 continue;
157 break; 159 lastslot = PCI_SLOT(dev->devfn);
158 } 160 add_slot(dev);
159 } 161 }
160 162
161 return retval; 163 return 0;
162} 164}
163 165
164static void remove_slot(struct dummy_slot *dslot) 166static void remove_slot(struct dummy_slot *dslot)
@@ -296,23 +298,9 @@ static int enable_slot(struct hotplug_slot *hotplug_slot)
296 return 0; 298 return 0;
297} 299}
298 300
299/* find the hotplug_slot for the pci_dev */
300static struct hotplug_slot *get_slot_from_dev(struct pci_dev *dev)
301{
302 struct dummy_slot *dslot;
303
304 list_for_each_entry(dslot, &slot_list, node) {
305 if (dslot->dev == dev)
306 return dslot->slot;
307 }
308 return NULL;
309}
310
311
312static int disable_slot(struct hotplug_slot *slot) 301static int disable_slot(struct hotplug_slot *slot)
313{ 302{
314 struct dummy_slot *dslot; 303 struct dummy_slot *dslot;
315 struct hotplug_slot *hslot;
316 struct pci_dev *dev; 304 struct pci_dev *dev;
317 int func; 305 int func;
318 306
@@ -322,41 +310,27 @@ static int disable_slot(struct hotplug_slot *slot)
322 310
323 dbg("%s - physical_slot = %s\n", __func__, slot->name); 311 dbg("%s - physical_slot = %s\n", __func__, slot->name);
324 312
325 /* don't disable bridged devices just yet, we can't handle them easily... */ 313 for (func = 7; func >= 0; func--) {
326 if (dslot->dev->subordinate) { 314 dev = pci_get_slot(dslot->dev->bus, dslot->dev->devfn + func);
327 err("Can't remove PCI devices with other PCI devices behind it yet.\n"); 315 if (!dev)
328 return -ENODEV; 316 continue;
329 } 317
330 if (test_and_set_bit(0, &dslot->removed)) { 318 if (test_and_set_bit(0, &dslot->removed)) {
331 dbg("Slot already scheduled for removal\n"); 319 dbg("Slot already scheduled for removal\n");
332 return -ENODEV; 320 return -ENODEV;
333 }
334 /* search for subfunctions and disable them first */
335 if (!(dslot->dev->devfn & 7)) {
336 for (func = 1; func < 8; func++) {
337 dev = pci_get_slot(dslot->dev->bus,
338 dslot->dev->devfn + func);
339 if (dev) {
340 hslot = get_slot_from_dev(dev);
341 if (hslot)
342 disable_slot(hslot);
343 else {
344 err("Hotplug slot not found for subfunction of PCI device\n");
345 return -ENODEV;
346 }
347 pci_dev_put(dev);
348 } else
349 dbg("No device in slot found\n");
350 } 321 }
351 }
352 322
353 /* remove the device from the pci core */ 323 /* queue work item to blow away this sysfs entry and other
354 pci_remove_bus_device(dslot->dev); 324 * parts.
325 */
326 INIT_WORK(&dslot->remove_work, remove_slot_worker);
327 queue_work(dummyphp_wq, &dslot->remove_work);
355 328
356 /* queue work item to blow away this sysfs entry and other parts. */ 329 /* blow away this sysfs entry and other parts. */
357 INIT_WORK(&dslot->remove_work, remove_slot_worker); 330 remove_slot(dslot);
358 queue_work(dummyphp_wq, &dslot->remove_work);
359 331
332 pci_dev_put(dev);
333 }
360 return 0; 334 return 0;
361} 335}
362 336
diff --git a/drivers/pci/hotplug/ibmphp_ebda.c b/drivers/pci/hotplug/ibmphp_ebda.c
index dca7efc14be2..8467d0287325 100644
--- a/drivers/pci/hotplug/ibmphp_ebda.c
+++ b/drivers/pci/hotplug/ibmphp_ebda.c
@@ -1001,7 +1001,8 @@ static int __init ebda_rsrc_controller (void)
1001 tmp_slot = list_entry (list, struct slot, ibm_slot_list); 1001 tmp_slot = list_entry (list, struct slot, ibm_slot_list);
1002 1002
1003 snprintf (tmp_slot->hotplug_slot->name, 30, "%s", create_file_name (tmp_slot)); 1003 snprintf (tmp_slot->hotplug_slot->name, 30, "%s", create_file_name (tmp_slot));
1004 pci_hp_register (tmp_slot->hotplug_slot); 1004 pci_hp_register(tmp_slot->hotplug_slot,
1005 pci_find_bus(0, tmp_slot->bus), tmp_slot->device);
1005 } 1006 }
1006 1007
1007 print_ebda_hpc (); 1008 print_ebda_hpc ();
diff --git a/drivers/pci/hotplug/pci_hotplug_core.c b/drivers/pci/hotplug/pci_hotplug_core.c
index a11021e8ce37..5f85b1b120e3 100644
--- a/drivers/pci/hotplug/pci_hotplug_core.c
+++ b/drivers/pci/hotplug/pci_hotplug_core.c
@@ -40,6 +40,7 @@
40#include <linux/pci.h> 40#include <linux/pci.h>
41#include <linux/pci_hotplug.h> 41#include <linux/pci_hotplug.h>
42#include <asm/uaccess.h> 42#include <asm/uaccess.h>
43#include "../pci.h"
43 44
44#define MY_NAME "pci_hotplug" 45#define MY_NAME "pci_hotplug"
45 46
@@ -60,41 +61,7 @@ static int debug;
60////////////////////////////////////////////////////////////////// 61//////////////////////////////////////////////////////////////////
61 62
62static LIST_HEAD(pci_hotplug_slot_list); 63static LIST_HEAD(pci_hotplug_slot_list);
63 64static DEFINE_SPINLOCK(pci_hotplug_slot_list_lock);
64struct kset *pci_hotplug_slots_kset;
65
66static ssize_t hotplug_slot_attr_show(struct kobject *kobj,
67 struct attribute *attr, char *buf)
68{
69 struct hotplug_slot *slot = to_hotplug_slot(kobj);
70 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
71 return attribute->show ? attribute->show(slot, buf) : -EIO;
72}
73
74static ssize_t hotplug_slot_attr_store(struct kobject *kobj,
75 struct attribute *attr, const char *buf, size_t len)
76{
77 struct hotplug_slot *slot = to_hotplug_slot(kobj);
78 struct hotplug_slot_attribute *attribute = to_hotplug_attr(attr);
79 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
80}
81
82static struct sysfs_ops hotplug_slot_sysfs_ops = {
83 .show = hotplug_slot_attr_show,
84 .store = hotplug_slot_attr_store,
85};
86
87static void hotplug_slot_release(struct kobject *kobj)
88{
89 struct hotplug_slot *slot = to_hotplug_slot(kobj);
90 if (slot->release)
91 slot->release(slot);
92}
93
94static struct kobj_type hotplug_slot_ktype = {
95 .sysfs_ops = &hotplug_slot_sysfs_ops,
96 .release = &hotplug_slot_release,
97};
98 65
99/* these strings match up with the values in pci_bus_speed */ 66/* these strings match up with the values in pci_bus_speed */
100static char *pci_bus_speed_strings[] = { 67static char *pci_bus_speed_strings[] = {
@@ -149,16 +116,15 @@ GET_STATUS(power_status, u8)
149GET_STATUS(attention_status, u8) 116GET_STATUS(attention_status, u8)
150GET_STATUS(latch_status, u8) 117GET_STATUS(latch_status, u8)
151GET_STATUS(adapter_status, u8) 118GET_STATUS(adapter_status, u8)
152GET_STATUS(address, u32)
153GET_STATUS(max_bus_speed, enum pci_bus_speed) 119GET_STATUS(max_bus_speed, enum pci_bus_speed)
154GET_STATUS(cur_bus_speed, enum pci_bus_speed) 120GET_STATUS(cur_bus_speed, enum pci_bus_speed)
155 121
156static ssize_t power_read_file (struct hotplug_slot *slot, char *buf) 122static ssize_t power_read_file(struct pci_slot *slot, char *buf)
157{ 123{
158 int retval; 124 int retval;
159 u8 value; 125 u8 value;
160 126
161 retval = get_power_status (slot, &value); 127 retval = get_power_status(slot->hotplug, &value);
162 if (retval) 128 if (retval)
163 goto exit; 129 goto exit;
164 retval = sprintf (buf, "%d\n", value); 130 retval = sprintf (buf, "%d\n", value);
@@ -166,9 +132,10 @@ exit:
166 return retval; 132 return retval;
167} 133}
168 134
169static ssize_t power_write_file (struct hotplug_slot *slot, const char *buf, 135static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
170 size_t count) 136 size_t count)
171{ 137{
138 struct hotplug_slot *slot = pci_slot->hotplug;
172 unsigned long lpower; 139 unsigned long lpower;
173 u8 power; 140 u8 power;
174 int retval = 0; 141 int retval = 0;
@@ -204,29 +171,30 @@ exit:
204 return count; 171 return count;
205} 172}
206 173
207static struct hotplug_slot_attribute hotplug_slot_attr_power = { 174static struct pci_slot_attribute hotplug_slot_attr_power = {
208 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR}, 175 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
209 .show = power_read_file, 176 .show = power_read_file,
210 .store = power_write_file 177 .store = power_write_file
211}; 178};
212 179
213static ssize_t attention_read_file (struct hotplug_slot *slot, char *buf) 180static ssize_t attention_read_file(struct pci_slot *slot, char *buf)
214{ 181{
215 int retval; 182 int retval;
216 u8 value; 183 u8 value;
217 184
218 retval = get_attention_status (slot, &value); 185 retval = get_attention_status(slot->hotplug, &value);
219 if (retval) 186 if (retval)
220 goto exit; 187 goto exit;
221 retval = sprintf (buf, "%d\n", value); 188 retval = sprintf(buf, "%d\n", value);
222 189
223exit: 190exit:
224 return retval; 191 return retval;
225} 192}
226 193
227static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf, 194static ssize_t attention_write_file(struct pci_slot *slot, const char *buf,
228 size_t count) 195 size_t count)
229{ 196{
197 struct hotplug_slot_ops *ops = slot->hotplug->ops;
230 unsigned long lattention; 198 unsigned long lattention;
231 u8 attention; 199 u8 attention;
232 int retval = 0; 200 int retval = 0;
@@ -235,13 +203,13 @@ static ssize_t attention_write_file (struct hotplug_slot *slot, const char *buf,
235 attention = (u8)(lattention & 0xff); 203 attention = (u8)(lattention & 0xff);
236 dbg (" - attention = %d\n", attention); 204 dbg (" - attention = %d\n", attention);
237 205
238 if (!try_module_get(slot->ops->owner)) { 206 if (!try_module_get(ops->owner)) {
239 retval = -ENODEV; 207 retval = -ENODEV;
240 goto exit; 208 goto exit;
241 } 209 }
242 if (slot->ops->set_attention_status) 210 if (ops->set_attention_status)
243 retval = slot->ops->set_attention_status(slot, attention); 211 retval = ops->set_attention_status(slot->hotplug, attention);
244 module_put(slot->ops->owner); 212 module_put(ops->owner);
245 213
246exit: 214exit:
247 if (retval) 215 if (retval)
@@ -249,18 +217,18 @@ exit:
249 return count; 217 return count;
250} 218}
251 219
252static struct hotplug_slot_attribute hotplug_slot_attr_attention = { 220static struct pci_slot_attribute hotplug_slot_attr_attention = {
253 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR}, 221 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
254 .show = attention_read_file, 222 .show = attention_read_file,
255 .store = attention_write_file 223 .store = attention_write_file
256}; 224};
257 225
258static ssize_t latch_read_file (struct hotplug_slot *slot, char *buf) 226static ssize_t latch_read_file(struct pci_slot *slot, char *buf)
259{ 227{
260 int retval; 228 int retval;
261 u8 value; 229 u8 value;
262 230
263 retval = get_latch_status (slot, &value); 231 retval = get_latch_status(slot->hotplug, &value);
264 if (retval) 232 if (retval)
265 goto exit; 233 goto exit;
266 retval = sprintf (buf, "%d\n", value); 234 retval = sprintf (buf, "%d\n", value);
@@ -269,17 +237,17 @@ exit:
269 return retval; 237 return retval;
270} 238}
271 239
272static struct hotplug_slot_attribute hotplug_slot_attr_latch = { 240static struct pci_slot_attribute hotplug_slot_attr_latch = {
273 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO}, 241 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
274 .show = latch_read_file, 242 .show = latch_read_file,
275}; 243};
276 244
277static ssize_t presence_read_file (struct hotplug_slot *slot, char *buf) 245static ssize_t presence_read_file(struct pci_slot *slot, char *buf)
278{ 246{
279 int retval; 247 int retval;
280 u8 value; 248 u8 value;
281 249
282 retval = get_adapter_status (slot, &value); 250 retval = get_adapter_status(slot->hotplug, &value);
283 if (retval) 251 if (retval)
284 goto exit; 252 goto exit;
285 retval = sprintf (buf, "%d\n", value); 253 retval = sprintf (buf, "%d\n", value);
@@ -288,42 +256,20 @@ exit:
288 return retval; 256 return retval;
289} 257}
290 258
291static struct hotplug_slot_attribute hotplug_slot_attr_presence = { 259static struct pci_slot_attribute hotplug_slot_attr_presence = {
292 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO}, 260 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
293 .show = presence_read_file, 261 .show = presence_read_file,
294}; 262};
295 263
296static ssize_t address_read_file (struct hotplug_slot *slot, char *buf)
297{
298 int retval;
299 u32 address;
300
301 retval = get_address (slot, &address);
302 if (retval)
303 goto exit;
304 retval = sprintf (buf, "%04x:%02x:%02x\n",
305 (address >> 16) & 0xffff,
306 (address >> 8) & 0xff,
307 address & 0xff);
308
309exit:
310 return retval;
311}
312
313static struct hotplug_slot_attribute hotplug_slot_attr_address = {
314 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
315 .show = address_read_file,
316};
317
318static char *unknown_speed = "Unknown bus speed"; 264static char *unknown_speed = "Unknown bus speed";
319 265
320static ssize_t max_bus_speed_read_file (struct hotplug_slot *slot, char *buf) 266static ssize_t max_bus_speed_read_file(struct pci_slot *slot, char *buf)
321{ 267{
322 char *speed_string; 268 char *speed_string;
323 int retval; 269 int retval;
324 enum pci_bus_speed value; 270 enum pci_bus_speed value;
325 271
326 retval = get_max_bus_speed (slot, &value); 272 retval = get_max_bus_speed(slot->hotplug, &value);
327 if (retval) 273 if (retval)
328 goto exit; 274 goto exit;
329 275
@@ -338,18 +284,18 @@ exit:
338 return retval; 284 return retval;
339} 285}
340 286
341static struct hotplug_slot_attribute hotplug_slot_attr_max_bus_speed = { 287static struct pci_slot_attribute hotplug_slot_attr_max_bus_speed = {
342 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO}, 288 .attr = {.name = "max_bus_speed", .mode = S_IFREG | S_IRUGO},
343 .show = max_bus_speed_read_file, 289 .show = max_bus_speed_read_file,
344}; 290};
345 291
346static ssize_t cur_bus_speed_read_file (struct hotplug_slot *slot, char *buf) 292static ssize_t cur_bus_speed_read_file(struct pci_slot *slot, char *buf)
347{ 293{
348 char *speed_string; 294 char *speed_string;
349 int retval; 295 int retval;
350 enum pci_bus_speed value; 296 enum pci_bus_speed value;
351 297
352 retval = get_cur_bus_speed (slot, &value); 298 retval = get_cur_bus_speed(slot->hotplug, &value);
353 if (retval) 299 if (retval)
354 goto exit; 300 goto exit;
355 301
@@ -364,14 +310,15 @@ exit:
364 return retval; 310 return retval;
365} 311}
366 312
367static struct hotplug_slot_attribute hotplug_slot_attr_cur_bus_speed = { 313static struct pci_slot_attribute hotplug_slot_attr_cur_bus_speed = {
368 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO}, 314 .attr = {.name = "cur_bus_speed", .mode = S_IFREG | S_IRUGO},
369 .show = cur_bus_speed_read_file, 315 .show = cur_bus_speed_read_file,
370}; 316};
371 317
372static ssize_t test_write_file (struct hotplug_slot *slot, const char *buf, 318static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
373 size_t count) 319 size_t count)
374{ 320{
321 struct hotplug_slot *slot = pci_slot->hotplug;
375 unsigned long ltest; 322 unsigned long ltest;
376 u32 test; 323 u32 test;
377 int retval = 0; 324 int retval = 0;
@@ -394,13 +341,14 @@ exit:
394 return count; 341 return count;
395} 342}
396 343
397static struct hotplug_slot_attribute hotplug_slot_attr_test = { 344static struct pci_slot_attribute hotplug_slot_attr_test = {
398 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR}, 345 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
399 .store = test_write_file 346 .store = test_write_file
400}; 347};
401 348
402static int has_power_file (struct hotplug_slot *slot) 349static int has_power_file(struct pci_slot *pci_slot)
403{ 350{
351 struct hotplug_slot *slot = pci_slot->hotplug;
404 if ((!slot) || (!slot->ops)) 352 if ((!slot) || (!slot->ops))
405 return -ENODEV; 353 return -ENODEV;
406 if ((slot->ops->enable_slot) || 354 if ((slot->ops->enable_slot) ||
@@ -410,8 +358,9 @@ static int has_power_file (struct hotplug_slot *slot)
410 return -ENOENT; 358 return -ENOENT;
411} 359}
412 360
413static int has_attention_file (struct hotplug_slot *slot) 361static int has_attention_file(struct pci_slot *pci_slot)
414{ 362{
363 struct hotplug_slot *slot = pci_slot->hotplug;
415 if ((!slot) || (!slot->ops)) 364 if ((!slot) || (!slot->ops))
416 return -ENODEV; 365 return -ENODEV;
417 if ((slot->ops->set_attention_status) || 366 if ((slot->ops->set_attention_status) ||
@@ -420,8 +369,9 @@ static int has_attention_file (struct hotplug_slot *slot)
420 return -ENOENT; 369 return -ENOENT;
421} 370}
422 371
423static int has_latch_file (struct hotplug_slot *slot) 372static int has_latch_file(struct pci_slot *pci_slot)
424{ 373{
374 struct hotplug_slot *slot = pci_slot->hotplug;
425 if ((!slot) || (!slot->ops)) 375 if ((!slot) || (!slot->ops))
426 return -ENODEV; 376 return -ENODEV;
427 if (slot->ops->get_latch_status) 377 if (slot->ops->get_latch_status)
@@ -429,8 +379,9 @@ static int has_latch_file (struct hotplug_slot *slot)
429 return -ENOENT; 379 return -ENOENT;
430} 380}
431 381
432static int has_adapter_file (struct hotplug_slot *slot) 382static int has_adapter_file(struct pci_slot *pci_slot)
433{ 383{
384 struct hotplug_slot *slot = pci_slot->hotplug;
434 if ((!slot) || (!slot->ops)) 385 if ((!slot) || (!slot->ops))
435 return -ENODEV; 386 return -ENODEV;
436 if (slot->ops->get_adapter_status) 387 if (slot->ops->get_adapter_status)
@@ -438,17 +389,9 @@ static int has_adapter_file (struct hotplug_slot *slot)
438 return -ENOENT; 389 return -ENOENT;
439} 390}
440 391
441static int has_address_file (struct hotplug_slot *slot) 392static int has_max_bus_speed_file(struct pci_slot *pci_slot)
442{
443 if ((!slot) || (!slot->ops))
444 return -ENODEV;
445 if (slot->ops->get_address)
446 return 0;
447 return -ENOENT;
448}
449
450static int has_max_bus_speed_file (struct hotplug_slot *slot)
451{ 393{
394 struct hotplug_slot *slot = pci_slot->hotplug;
452 if ((!slot) || (!slot->ops)) 395 if ((!slot) || (!slot->ops))
453 return -ENODEV; 396 return -ENODEV;
454 if (slot->ops->get_max_bus_speed) 397 if (slot->ops->get_max_bus_speed)
@@ -456,8 +399,9 @@ static int has_max_bus_speed_file (struct hotplug_slot *slot)
456 return -ENOENT; 399 return -ENOENT;
457} 400}
458 401
459static int has_cur_bus_speed_file (struct hotplug_slot *slot) 402static int has_cur_bus_speed_file(struct pci_slot *pci_slot)
460{ 403{
404 struct hotplug_slot *slot = pci_slot->hotplug;
461 if ((!slot) || (!slot->ops)) 405 if ((!slot) || (!slot->ops))
462 return -ENODEV; 406 return -ENODEV;
463 if (slot->ops->get_cur_bus_speed) 407 if (slot->ops->get_cur_bus_speed)
@@ -465,8 +409,9 @@ static int has_cur_bus_speed_file (struct hotplug_slot *slot)
465 return -ENOENT; 409 return -ENOENT;
466} 410}
467 411
468static int has_test_file (struct hotplug_slot *slot) 412static int has_test_file(struct pci_slot *pci_slot)
469{ 413{
414 struct hotplug_slot *slot = pci_slot->hotplug;
470 if ((!slot) || (!slot->ops)) 415 if ((!slot) || (!slot->ops))
471 return -ENODEV; 416 return -ENODEV;
472 if (slot->ops->hardware_test) 417 if (slot->ops->hardware_test)
@@ -474,7 +419,7 @@ static int has_test_file (struct hotplug_slot *slot)
474 return -ENOENT; 419 return -ENOENT;
475} 420}
476 421
477static int fs_add_slot (struct hotplug_slot *slot) 422static int fs_add_slot(struct pci_slot *slot)
478{ 423{
479 int retval = 0; 424 int retval = 0;
480 425
@@ -505,13 +450,6 @@ static int fs_add_slot (struct hotplug_slot *slot)
505 goto exit_adapter; 450 goto exit_adapter;
506 } 451 }
507 452
508 if (has_address_file(slot) == 0) {
509 retval = sysfs_create_file(&slot->kobj,
510 &hotplug_slot_attr_address.attr);
511 if (retval)
512 goto exit_address;
513 }
514
515 if (has_max_bus_speed_file(slot) == 0) { 453 if (has_max_bus_speed_file(slot) == 0) {
516 retval = sysfs_create_file(&slot->kobj, 454 retval = sysfs_create_file(&slot->kobj,
517 &hotplug_slot_attr_max_bus_speed.attr); 455 &hotplug_slot_attr_max_bus_speed.attr);
@@ -544,10 +482,6 @@ exit_cur_speed:
544 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr); 482 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
545 483
546exit_max_speed: 484exit_max_speed:
547 if (has_address_file(slot) == 0)
548 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
549
550exit_address:
551 if (has_adapter_file(slot) == 0) 485 if (has_adapter_file(slot) == 0)
552 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr); 486 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
553 487
@@ -567,7 +501,7 @@ exit:
567 return retval; 501 return retval;
568} 502}
569 503
570static void fs_remove_slot (struct hotplug_slot *slot) 504static void fs_remove_slot(struct pci_slot *slot)
571{ 505{
572 if (has_power_file(slot) == 0) 506 if (has_power_file(slot) == 0)
573 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr); 507 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
@@ -581,9 +515,6 @@ static void fs_remove_slot (struct hotplug_slot *slot)
581 if (has_adapter_file(slot) == 0) 515 if (has_adapter_file(slot) == 0)
582 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr); 516 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_presence.attr);
583 517
584 if (has_address_file(slot) == 0)
585 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_address.attr);
586
587 if (has_max_bus_speed_file(slot) == 0) 518 if (has_max_bus_speed_file(slot) == 0)
588 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr); 519 sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_max_bus_speed.attr);
589 520
@@ -599,27 +530,33 @@ static struct hotplug_slot *get_slot_from_name (const char *name)
599 struct hotplug_slot *slot; 530 struct hotplug_slot *slot;
600 struct list_head *tmp; 531 struct list_head *tmp;
601 532
533 spin_lock(&pci_hotplug_slot_list_lock);
602 list_for_each (tmp, &pci_hotplug_slot_list) { 534 list_for_each (tmp, &pci_hotplug_slot_list) {
603 slot = list_entry (tmp, struct hotplug_slot, slot_list); 535 slot = list_entry (tmp, struct hotplug_slot, slot_list);
604 if (strcmp(slot->name, name) == 0) 536 if (strcmp(slot->name, name) == 0)
605 return slot; 537 goto out;
606 } 538 }
607 return NULL; 539 slot = NULL;
540out:
541 spin_unlock(&pci_hotplug_slot_list_lock);
542 return slot;
608} 543}
609 544
610/** 545/**
611 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem 546 * pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
547 * @bus: bus this slot is on
612 * @slot: pointer to the &struct hotplug_slot to register 548 * @slot: pointer to the &struct hotplug_slot to register
549 * @slot_nr: slot number
613 * 550 *
614 * Registers a hotplug slot with the pci hotplug subsystem, which will allow 551 * Registers a hotplug slot with the pci hotplug subsystem, which will allow
615 * userspace interaction to the slot. 552 * userspace interaction to the slot.
616 * 553 *
617 * Returns 0 if successful, anything else for an error. 554 * Returns 0 if successful, anything else for an error.
618 */ 555 */
619int pci_hp_register (struct hotplug_slot *slot) 556int pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus, int slot_nr)
620{ 557{
621 int result; 558 int result;
622 struct hotplug_slot *tmp; 559 struct pci_slot *pci_slot;
623 560
624 if (slot == NULL) 561 if (slot == NULL)
625 return -ENODEV; 562 return -ENODEV;
@@ -632,57 +569,89 @@ int pci_hp_register (struct hotplug_slot *slot)
632 } 569 }
633 570
634 /* Check if we have already registered a slot with the same name. */ 571 /* Check if we have already registered a slot with the same name. */
635 tmp = get_slot_from_name(slot->name); 572 if (get_slot_from_name(slot->name))
636 if (tmp)
637 return -EEXIST; 573 return -EEXIST;
638 574
639 slot->kobj.kset = pci_hotplug_slots_kset; 575 /*
640 result = kobject_init_and_add(&slot->kobj, &hotplug_slot_ktype, NULL, 576 * No problems if we call this interface from both ACPI_PCI_SLOT
641 "%s", slot->name); 577 * driver and call it here again. If we've already created the
642 if (result) { 578 * pci_slot, the interface will simply bump the refcount.
643 err("Unable to register kobject '%s'", slot->name); 579 */
644 return -EINVAL; 580 pci_slot = pci_create_slot(bus, slot_nr, slot->name);
581 if (IS_ERR(pci_slot))
582 return PTR_ERR(pci_slot);
583
584 if (pci_slot->hotplug) {
585 dbg("%s: already claimed\n", __func__);
586 pci_destroy_slot(pci_slot);
587 return -EBUSY;
645 } 588 }
646 589
647 list_add (&slot->slot_list, &pci_hotplug_slot_list); 590 slot->pci_slot = pci_slot;
591 pci_slot->hotplug = slot;
592
593 /*
594 * Allow pcihp drivers to override the ACPI_PCI_SLOT name.
595 */
596 if (strcmp(kobject_name(&pci_slot->kobj), slot->name)) {
597 result = kobject_rename(&pci_slot->kobj, slot->name);
598 if (result) {
599 pci_destroy_slot(pci_slot);
600 return result;
601 }
602 }
603
604 spin_lock(&pci_hotplug_slot_list_lock);
605 list_add(&slot->slot_list, &pci_hotplug_slot_list);
606 spin_unlock(&pci_hotplug_slot_list_lock);
607
608 result = fs_add_slot(pci_slot);
609 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
610 dbg("Added slot %s to the list\n", slot->name);
611
648 612
649 result = fs_add_slot (slot);
650 kobject_uevent(&slot->kobj, KOBJ_ADD);
651 dbg ("Added slot %s to the list\n", slot->name);
652 return result; 613 return result;
653} 614}
654 615
655/** 616/**
656 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem 617 * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
657 * @slot: pointer to the &struct hotplug_slot to deregister 618 * @hotplug: pointer to the &struct hotplug_slot to deregister
658 * 619 *
659 * The @slot must have been registered with the pci hotplug subsystem 620 * The @slot must have been registered with the pci hotplug subsystem
660 * previously with a call to pci_hp_register(). 621 * previously with a call to pci_hp_register().
661 * 622 *
662 * Returns 0 if successful, anything else for an error. 623 * Returns 0 if successful, anything else for an error.
663 */ 624 */
664int pci_hp_deregister (struct hotplug_slot *slot) 625int pci_hp_deregister(struct hotplug_slot *hotplug)
665{ 626{
666 struct hotplug_slot *temp; 627 struct hotplug_slot *temp;
628 struct pci_slot *slot;
667 629
668 if (slot == NULL) 630 if (!hotplug)
669 return -ENODEV; 631 return -ENODEV;
670 632
671 temp = get_slot_from_name (slot->name); 633 temp = get_slot_from_name(hotplug->name);
672 if (temp != slot) { 634 if (temp != hotplug)
673 return -ENODEV; 635 return -ENODEV;
674 }
675 list_del (&slot->slot_list);
676 636
677 fs_remove_slot (slot); 637 spin_lock(&pci_hotplug_slot_list_lock);
678 dbg ("Removed slot %s from the list\n", slot->name); 638 list_del(&hotplug->slot_list);
679 kobject_put(&slot->kobj); 639 spin_unlock(&pci_hotplug_slot_list_lock);
640
641 slot = hotplug->pci_slot;
642 fs_remove_slot(slot);
643 dbg("Removed slot %s from the list\n", hotplug->name);
644
645 hotplug->release(hotplug);
646 slot->hotplug = NULL;
647 pci_destroy_slot(slot);
648
680 return 0; 649 return 0;
681} 650}
682 651
683/** 652/**
684 * pci_hp_change_slot_info - changes the slot's information structure in the core 653 * pci_hp_change_slot_info - changes the slot's information structure in the core
685 * @slot: pointer to the slot whose info has changed 654 * @hotplug: pointer to the slot whose info has changed
686 * @info: pointer to the info copy into the slot's info structure 655 * @info: pointer to the info copy into the slot's info structure
687 * 656 *
688 * @slot must have been registered with the pci 657 * @slot must have been registered with the pci
@@ -690,13 +659,15 @@ int pci_hp_deregister (struct hotplug_slot *slot)
690 * 659 *
691 * Returns 0 if successful, anything else for an error. 660 * Returns 0 if successful, anything else for an error.
692 */ 661 */
693int __must_check pci_hp_change_slot_info(struct hotplug_slot *slot, 662int __must_check pci_hp_change_slot_info(struct hotplug_slot *hotplug,
694 struct hotplug_slot_info *info) 663 struct hotplug_slot_info *info)
695{ 664{
696 if ((slot == NULL) || (info == NULL)) 665 struct pci_slot *slot;
666 if (!hotplug || !info)
697 return -ENODEV; 667 return -ENODEV;
668 slot = hotplug->pci_slot;
698 669
699 memcpy (slot->info, info, sizeof (struct hotplug_slot_info)); 670 memcpy(hotplug->info, info, sizeof(struct hotplug_slot_info));
700 671
701 return 0; 672 return 0;
702} 673}
@@ -704,36 +675,22 @@ int __must_check pci_hp_change_slot_info(struct hotplug_slot *slot,
704static int __init pci_hotplug_init (void) 675static int __init pci_hotplug_init (void)
705{ 676{
706 int result; 677 int result;
707 struct kset *pci_bus_kset;
708 678
709 pci_bus_kset = bus_get_kset(&pci_bus_type);
710
711 pci_hotplug_slots_kset = kset_create_and_add("slots", NULL,
712 &pci_bus_kset->kobj);
713 if (!pci_hotplug_slots_kset) {
714 result = -ENOMEM;
715 err("Register subsys error\n");
716 goto exit;
717 }
718 result = cpci_hotplug_init(debug); 679 result = cpci_hotplug_init(debug);
719 if (result) { 680 if (result) {
720 err ("cpci_hotplug_init with error %d\n", result); 681 err ("cpci_hotplug_init with error %d\n", result);
721 goto err_subsys; 682 goto err_cpci;
722 } 683 }
723 684
724 info (DRIVER_DESC " version: " DRIVER_VERSION "\n"); 685 info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
725 goto exit;
726 686
727err_subsys: 687err_cpci:
728 kset_unregister(pci_hotplug_slots_kset);
729exit:
730 return result; 688 return result;
731} 689}
732 690
733static void __exit pci_hotplug_exit (void) 691static void __exit pci_hotplug_exit (void)
734{ 692{
735 cpci_hotplug_exit(); 693 cpci_hotplug_exit();
736 kset_unregister(pci_hotplug_slots_kset);
737} 694}
738 695
739module_init(pci_hotplug_init); 696module_init(pci_hotplug_init);
@@ -745,7 +702,6 @@ MODULE_LICENSE("GPL");
745module_param(debug, bool, 0644); 702module_param(debug, bool, 0644);
746MODULE_PARM_DESC(debug, "Debugging mode enabled or not"); 703MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
747 704
748EXPORT_SYMBOL_GPL(pci_hotplug_slots_kset);
749EXPORT_SYMBOL_GPL(pci_hp_register); 705EXPORT_SYMBOL_GPL(pci_hp_register);
750EXPORT_SYMBOL_GPL(pci_hp_deregister); 706EXPORT_SYMBOL_GPL(pci_hp_deregister);
751EXPORT_SYMBOL_GPL(pci_hp_change_slot_info); 707EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);
diff --git a/drivers/pci/hotplug/pciehp.h b/drivers/pci/hotplug/pciehp.h
index 79c9ddaad3fb..e3a1e7e7dba2 100644
--- a/drivers/pci/hotplug/pciehp.h
+++ b/drivers/pci/hotplug/pciehp.h
@@ -43,6 +43,7 @@ extern int pciehp_poll_mode;
43extern int pciehp_poll_time; 43extern int pciehp_poll_time;
44extern int pciehp_debug; 44extern int pciehp_debug;
45extern int pciehp_force; 45extern int pciehp_force;
46extern int pciehp_slot_with_bus;
46extern struct workqueue_struct *pciehp_wq; 47extern struct workqueue_struct *pciehp_wq;
47 48
48#define dbg(format, arg...) \ 49#define dbg(format, arg...) \
@@ -96,7 +97,7 @@ struct controller {
96 u32 slot_cap; 97 u32 slot_cap;
97 u8 cap_base; 98 u8 cap_base;
98 struct timer_list poll_timer; 99 struct timer_list poll_timer;
99 volatile int cmd_busy; 100 int cmd_busy;
100 unsigned int no_cmd_complete:1; 101 unsigned int no_cmd_complete:1;
101}; 102};
102 103
@@ -156,10 +157,10 @@ extern u8 pciehp_handle_power_fault(struct slot *p_slot);
156extern int pciehp_configure_device(struct slot *p_slot); 157extern int pciehp_configure_device(struct slot *p_slot);
157extern int pciehp_unconfigure_device(struct slot *p_slot); 158extern int pciehp_unconfigure_device(struct slot *p_slot);
158extern void pciehp_queue_pushbutton_work(struct work_struct *work); 159extern void pciehp_queue_pushbutton_work(struct work_struct *work);
159int pcie_init(struct controller *ctrl, struct pcie_device *dev); 160struct controller *pcie_init(struct pcie_device *dev);
160int pciehp_enable_slot(struct slot *p_slot); 161int pciehp_enable_slot(struct slot *p_slot);
161int pciehp_disable_slot(struct slot *p_slot); 162int pciehp_disable_slot(struct slot *p_slot);
162int pcie_init_hardware_part2(struct controller *ctrl, struct pcie_device *dev); 163int pcie_enable_notification(struct controller *ctrl);
163 164
164static inline struct slot *pciehp_find_slot(struct controller *ctrl, u8 device) 165static inline struct slot *pciehp_find_slot(struct controller *ctrl, u8 device)
165{ 166{
@@ -202,8 +203,13 @@ struct hpc_ops {
202#include <acpi/actypes.h> 203#include <acpi/actypes.h>
203#include <linux/pci-acpi.h> 204#include <linux/pci-acpi.h>
204 205
205#define pciehp_get_hp_hw_control_from_firmware(dev) \ 206static inline int pciehp_get_hp_hw_control_from_firmware(struct pci_dev *dev)
206 pciehp_acpi_get_hp_hw_control_from_firmware(dev) 207{
208 u32 flags = (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL |
209 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
210 return acpi_get_hp_hw_control_from_firmware(dev, flags);
211}
212
207static inline int pciehp_get_hp_params_from_firmware(struct pci_dev *dev, 213static inline int pciehp_get_hp_params_from_firmware(struct pci_dev *dev,
208 struct hotplug_params *hpp) 214 struct hotplug_params *hpp)
209{ 215{
diff --git a/drivers/pci/hotplug/pciehp_core.c b/drivers/pci/hotplug/pciehp_core.c
index 48a2ed378914..3677495c4f91 100644
--- a/drivers/pci/hotplug/pciehp_core.c
+++ b/drivers/pci/hotplug/pciehp_core.c
@@ -72,7 +72,6 @@ static int get_power_status (struct hotplug_slot *slot, u8 *value);
72static int get_attention_status (struct hotplug_slot *slot, u8 *value); 72static int get_attention_status (struct hotplug_slot *slot, u8 *value);
73static int get_latch_status (struct hotplug_slot *slot, u8 *value); 73static int get_latch_status (struct hotplug_slot *slot, u8 *value);
74static int get_adapter_status (struct hotplug_slot *slot, u8 *value); 74static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
75static int get_address (struct hotplug_slot *slot, u32 *value);
76static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value); 75static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
77static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value); 76static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
78 77
@@ -85,7 +84,6 @@ static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
85 .get_attention_status = get_attention_status, 84 .get_attention_status = get_attention_status,
86 .get_latch_status = get_latch_status, 85 .get_latch_status = get_latch_status,
87 .get_adapter_status = get_adapter_status, 86 .get_adapter_status = get_adapter_status,
88 .get_address = get_address,
89 .get_max_bus_speed = get_max_bus_speed, 87 .get_max_bus_speed = get_max_bus_speed,
90 .get_cur_bus_speed = get_cur_bus_speed, 88 .get_cur_bus_speed = get_cur_bus_speed,
91}; 89};
@@ -185,23 +183,10 @@ static struct hotplug_slot_attribute hotplug_slot_attr_lock = {
185 */ 183 */
186static void release_slot(struct hotplug_slot *hotplug_slot) 184static void release_slot(struct hotplug_slot *hotplug_slot)
187{ 185{
188 struct slot *slot = hotplug_slot->private;
189
190 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name); 186 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
191 187
192 kfree(slot->hotplug_slot->info); 188 kfree(hotplug_slot->info);
193 kfree(slot->hotplug_slot); 189 kfree(hotplug_slot);
194 kfree(slot);
195}
196
197static void make_slot_name(struct slot *slot)
198{
199 if (pciehp_slot_with_bus)
200 snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
201 slot->bus, slot->number);
202 else
203 snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%d",
204 slot->number);
205} 190}
206 191
207static int init_slots(struct controller *ctrl) 192static int init_slots(struct controller *ctrl)
@@ -210,49 +195,34 @@ static int init_slots(struct controller *ctrl)
210 struct hotplug_slot *hotplug_slot; 195 struct hotplug_slot *hotplug_slot;
211 struct hotplug_slot_info *info; 196 struct hotplug_slot_info *info;
212 int retval = -ENOMEM; 197 int retval = -ENOMEM;
213 int i;
214
215 for (i = 0; i < ctrl->num_slots; i++) {
216 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
217 if (!slot)
218 goto error;
219 198
199 list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
220 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL); 200 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
221 if (!hotplug_slot) 201 if (!hotplug_slot)
222 goto error_slot; 202 goto error;
223 slot->hotplug_slot = hotplug_slot;
224 203
225 info = kzalloc(sizeof(*info), GFP_KERNEL); 204 info = kzalloc(sizeof(*info), GFP_KERNEL);
226 if (!info) 205 if (!info)
227 goto error_hpslot; 206 goto error_hpslot;
228 hotplug_slot->info = info;
229
230 hotplug_slot->name = slot->name;
231
232 slot->hp_slot = i;
233 slot->ctrl = ctrl;
234 slot->bus = ctrl->pci_dev->subordinate->number;
235 slot->device = ctrl->slot_device_offset + i;
236 slot->hpc_ops = ctrl->hpc_ops;
237 slot->number = ctrl->first_slot;
238 mutex_init(&slot->lock);
239 INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
240 207
241 /* register this slot with the hotplug pci core */ 208 /* register this slot with the hotplug pci core */
209 hotplug_slot->info = info;
210 hotplug_slot->name = slot->name;
242 hotplug_slot->private = slot; 211 hotplug_slot->private = slot;
243 hotplug_slot->release = &release_slot; 212 hotplug_slot->release = &release_slot;
244 make_slot_name(slot);
245 hotplug_slot->ops = &pciehp_hotplug_slot_ops; 213 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
246
247 get_power_status(hotplug_slot, &info->power_status); 214 get_power_status(hotplug_slot, &info->power_status);
248 get_attention_status(hotplug_slot, &info->attention_status); 215 get_attention_status(hotplug_slot, &info->attention_status);
249 get_latch_status(hotplug_slot, &info->latch_status); 216 get_latch_status(hotplug_slot, &info->latch_status);
250 get_adapter_status(hotplug_slot, &info->adapter_status); 217 get_adapter_status(hotplug_slot, &info->adapter_status);
218 slot->hotplug_slot = hotplug_slot;
251 219
252 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x " 220 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
253 "slot_device_offset=%x\n", slot->bus, slot->device, 221 "slot_device_offset=%x\n", slot->bus, slot->device,
254 slot->hp_slot, slot->number, ctrl->slot_device_offset); 222 slot->hp_slot, slot->number, ctrl->slot_device_offset);
255 retval = pci_hp_register(hotplug_slot); 223 retval = pci_hp_register(hotplug_slot,
224 ctrl->pci_dev->subordinate,
225 slot->device);
256 if (retval) { 226 if (retval) {
257 err("pci_hp_register failed with error %d\n", retval); 227 err("pci_hp_register failed with error %d\n", retval);
258 if (retval == -EEXIST) 228 if (retval == -EEXIST)
@@ -263,7 +233,7 @@ static int init_slots(struct controller *ctrl)
263 } 233 }
264 /* create additional sysfs entries */ 234 /* create additional sysfs entries */
265 if (EMI(ctrl)) { 235 if (EMI(ctrl)) {
266 retval = sysfs_create_file(&hotplug_slot->kobj, 236 retval = sysfs_create_file(&hotplug_slot->pci_slot->kobj,
267 &hotplug_slot_attr_lock.attr); 237 &hotplug_slot_attr_lock.attr);
268 if (retval) { 238 if (retval) {
269 pci_hp_deregister(hotplug_slot); 239 pci_hp_deregister(hotplug_slot);
@@ -271,8 +241,6 @@ static int init_slots(struct controller *ctrl)
271 goto error_info; 241 goto error_info;
272 } 242 }
273 } 243 }
274
275 list_add(&slot->slot_list, &ctrl->slot_list);
276 } 244 }
277 245
278 return 0; 246 return 0;
@@ -280,27 +248,18 @@ error_info:
280 kfree(info); 248 kfree(info);
281error_hpslot: 249error_hpslot:
282 kfree(hotplug_slot); 250 kfree(hotplug_slot);
283error_slot:
284 kfree(slot);
285error: 251error:
286 return retval; 252 return retval;
287} 253}
288 254
289static void cleanup_slots(struct controller *ctrl) 255static void cleanup_slots(struct controller *ctrl)
290{ 256{
291 struct list_head *tmp;
292 struct list_head *next;
293 struct slot *slot; 257 struct slot *slot;
294 258
295 list_for_each_safe(tmp, next, &ctrl->slot_list) { 259 list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
296 slot = list_entry(tmp, struct slot, slot_list);
297 list_del(&slot->slot_list);
298 if (EMI(ctrl)) 260 if (EMI(ctrl))
299 sysfs_remove_file(&slot->hotplug_slot->kobj, 261 sysfs_remove_file(&slot->hotplug_slot->pci_slot->kobj,
300 &hotplug_slot_attr_lock.attr); 262 &hotplug_slot_attr_lock.attr);
301 cancel_delayed_work(&slot->work);
302 flush_scheduled_work();
303 flush_workqueue(pciehp_wq);
304 pci_hp_deregister(slot->hotplug_slot); 263 pci_hp_deregister(slot->hotplug_slot);
305 } 264 }
306} 265}
@@ -398,19 +357,8 @@ static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
398 return 0; 357 return 0;
399} 358}
400 359
401static int get_address(struct hotplug_slot *hotplug_slot, u32 *value) 360static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
402{ 361 enum pci_bus_speed *value)
403 struct slot *slot = hotplug_slot->private;
404 struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
405
406 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
407
408 *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
409
410 return 0;
411}
412
413static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
414{ 362{
415 struct slot *slot = hotplug_slot->private; 363 struct slot *slot = hotplug_slot->private;
416 int retval; 364 int retval;
@@ -444,34 +392,30 @@ static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_
444 struct controller *ctrl; 392 struct controller *ctrl;
445 struct slot *t_slot; 393 struct slot *t_slot;
446 u8 value; 394 u8 value;
447 struct pci_dev *pdev; 395 struct pci_dev *pdev = dev->port;
448 396
449 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 397 if (pciehp_force)
450 if (!ctrl) { 398 dbg("Bypassing BIOS check for pciehp use on %s\n",
451 err("%s : out of memory\n", __func__); 399 pci_name(pdev));
400 else if (pciehp_get_hp_hw_control_from_firmware(pdev))
452 goto err_out_none; 401 goto err_out_none;
453 }
454 INIT_LIST_HEAD(&ctrl->slot_list);
455
456 pdev = dev->port;
457 ctrl->pci_dev = pdev;
458 402
459 rc = pcie_init(ctrl, dev); 403 ctrl = pcie_init(dev);
460 if (rc) { 404 if (!ctrl) {
461 dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME); 405 dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
462 goto err_out_free_ctrl; 406 goto err_out_none;
463 } 407 }
464 408 set_service_data(dev, ctrl);
465 pci_set_drvdata(pdev, ctrl);
466
467 dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n",
468 __func__, pdev->bus->number, PCI_SLOT(pdev->devfn),
469 PCI_FUNC(pdev->devfn), pdev->irq);
470 409
471 /* Setup the slot information structures */ 410 /* Setup the slot information structures */
472 rc = init_slots(ctrl); 411 rc = init_slots(ctrl);
473 if (rc) { 412 if (rc) {
474 err("%s: slot initialization failed\n", PCIE_MODULE_NAME); 413 if (rc == -EBUSY)
414 warn("%s: slot already registered by another "
415 "hotplug driver\n", PCIE_MODULE_NAME);
416 else
417 err("%s: slot initialization failed\n",
418 PCIE_MODULE_NAME);
475 goto err_out_release_ctlr; 419 goto err_out_release_ctlr;
476 } 420 }
477 421
@@ -495,20 +439,16 @@ err_out_free_ctrl_slot:
495 cleanup_slots(ctrl); 439 cleanup_slots(ctrl);
496err_out_release_ctlr: 440err_out_release_ctlr:
497 ctrl->hpc_ops->release_ctlr(ctrl); 441 ctrl->hpc_ops->release_ctlr(ctrl);
498err_out_free_ctrl:
499 kfree(ctrl);
500err_out_none: 442err_out_none:
501 return -ENODEV; 443 return -ENODEV;
502} 444}
503 445
504static void pciehp_remove (struct pcie_device *dev) 446static void pciehp_remove (struct pcie_device *dev)
505{ 447{
506 struct pci_dev *pdev = dev->port; 448 struct controller *ctrl = get_service_data(dev);
507 struct controller *ctrl = pci_get_drvdata(pdev);
508 449
509 cleanup_slots(ctrl); 450 cleanup_slots(ctrl);
510 ctrl->hpc_ops->release_ctlr(ctrl); 451 ctrl->hpc_ops->release_ctlr(ctrl);
511 kfree(ctrl);
512} 452}
513 453
514#ifdef CONFIG_PM 454#ifdef CONFIG_PM
@@ -522,13 +462,12 @@ static int pciehp_resume (struct pcie_device *dev)
522{ 462{
523 printk("%s ENTRY\n", __func__); 463 printk("%s ENTRY\n", __func__);
524 if (pciehp_force) { 464 if (pciehp_force) {
525 struct pci_dev *pdev = dev->port; 465 struct controller *ctrl = get_service_data(dev);
526 struct controller *ctrl = pci_get_drvdata(pdev);
527 struct slot *t_slot; 466 struct slot *t_slot;
528 u8 status; 467 u8 status;
529 468
530 /* reinitialize the chipset's event detection logic */ 469 /* reinitialize the chipset's event detection logic */
531 pcie_init_hardware_part2(ctrl, dev); 470 pcie_enable_notification(ctrl);
532 471
533 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset); 472 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
534 473
diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
index 79f104963166..1323a43285d7 100644
--- a/drivers/pci/hotplug/pciehp_hpc.c
+++ b/drivers/pci/hotplug/pciehp_hpc.c
@@ -247,30 +247,32 @@ static inline void pciehp_free_irq(struct controller *ctrl)
247 free_irq(ctrl->pci_dev->irq, ctrl); 247 free_irq(ctrl->pci_dev->irq, ctrl);
248} 248}
249 249
250static inline int pcie_poll_cmd(struct controller *ctrl) 250static int pcie_poll_cmd(struct controller *ctrl)
251{ 251{
252 u16 slot_status; 252 u16 slot_status;
253 int timeout = 1000; 253 int timeout = 1000;
254 254
255 if (!pciehp_readw(ctrl, SLOTSTATUS, &slot_status)) 255 if (!pciehp_readw(ctrl, SLOTSTATUS, &slot_status)) {
256 if (slot_status & CMD_COMPLETED) 256 if (slot_status & CMD_COMPLETED) {
257 goto completed; 257 pciehp_writew(ctrl, SLOTSTATUS, CMD_COMPLETED);
258 for (timeout = 1000; timeout > 0; timeout -= 100) { 258 return 1;
259 msleep(100); 259 }
260 if (!pciehp_readw(ctrl, SLOTSTATUS, &slot_status)) 260 }
261 if (slot_status & CMD_COMPLETED) 261 while (timeout > 1000) {
262 goto completed; 262 msleep(10);
263 timeout -= 10;
264 if (!pciehp_readw(ctrl, SLOTSTATUS, &slot_status)) {
265 if (slot_status & CMD_COMPLETED) {
266 pciehp_writew(ctrl, SLOTSTATUS, CMD_COMPLETED);
267 return 1;
268 }
269 }
263 } 270 }
264 return 0; /* timeout */ 271 return 0; /* timeout */
265
266completed:
267 pciehp_writew(ctrl, SLOTSTATUS, CMD_COMPLETED);
268 return timeout;
269} 272}
270 273
271static inline int pcie_wait_cmd(struct controller *ctrl, int poll) 274static void pcie_wait_cmd(struct controller *ctrl, int poll)
272{ 275{
273 int retval = 0;
274 unsigned int msecs = pciehp_poll_mode ? 2500 : 1000; 276 unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
275 unsigned long timeout = msecs_to_jiffies(msecs); 277 unsigned long timeout = msecs_to_jiffies(msecs);
276 int rc; 278 int rc;
@@ -278,16 +280,9 @@ static inline int pcie_wait_cmd(struct controller *ctrl, int poll)
278 if (poll) 280 if (poll)
279 rc = pcie_poll_cmd(ctrl); 281 rc = pcie_poll_cmd(ctrl);
280 else 282 else
281 rc = wait_event_interruptible_timeout(ctrl->queue, 283 rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
282 !ctrl->cmd_busy, timeout);
283 if (!rc) 284 if (!rc)
284 dbg("Command not completed in 1000 msec\n"); 285 dbg("Command not completed in 1000 msec\n");
285 else if (rc < 0) {
286 retval = -EINTR;
287 info("Command was interrupted by a signal\n");
288 }
289
290 return retval;
291} 286}
292 287
293/** 288/**
@@ -342,10 +337,6 @@ static int pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
342 337
343 slot_ctrl &= ~mask; 338 slot_ctrl &= ~mask;
344 slot_ctrl |= (cmd & mask); 339 slot_ctrl |= (cmd & mask);
345 /* Don't enable command completed if caller is changing it. */
346 if (!(mask & CMD_CMPL_INTR_ENABLE))
347 slot_ctrl |= CMD_CMPL_INTR_ENABLE;
348
349 ctrl->cmd_busy = 1; 340 ctrl->cmd_busy = 1;
350 smp_mb(); 341 smp_mb();
351 retval = pciehp_writew(ctrl, SLOTCTRL, slot_ctrl); 342 retval = pciehp_writew(ctrl, SLOTCTRL, slot_ctrl);
@@ -365,7 +356,7 @@ static int pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
365 if (!(slot_ctrl & HP_INTR_ENABLE) || 356 if (!(slot_ctrl & HP_INTR_ENABLE) ||
366 !(slot_ctrl & CMD_CMPL_INTR_ENABLE)) 357 !(slot_ctrl & CMD_CMPL_INTR_ENABLE))
367 poll = 1; 358 poll = 1;
368 retval = pcie_wait_cmd(ctrl, poll); 359 pcie_wait_cmd(ctrl, poll);
369 } 360 }
370 out: 361 out:
371 mutex_unlock(&ctrl->ctrl_lock); 362 mutex_unlock(&ctrl->ctrl_lock);
@@ -614,23 +605,6 @@ static void hpc_set_green_led_blink(struct slot *slot)
614 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd); 605 __func__, ctrl->cap_base + SLOTCTRL, slot_cmd);
615} 606}
616 607
617static void hpc_release_ctlr(struct controller *ctrl)
618{
619 /* Mask Hot-plug Interrupt Enable */
620 if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE))
621 err("%s: Cannot mask hotplut interrupt enable\n", __func__);
622
623 /* Free interrupt handler or interrupt polling timer */
624 pciehp_free_irq(ctrl);
625
626 /*
627 * If this is the last controller to be released, destroy the
628 * pciehp work queue
629 */
630 if (atomic_dec_and_test(&pciehp_num_controllers))
631 destroy_workqueue(pciehp_wq);
632}
633
634static int hpc_power_on_slot(struct slot * slot) 608static int hpc_power_on_slot(struct slot * slot)
635{ 609{
636 struct controller *ctrl = slot->ctrl; 610 struct controller *ctrl = slot->ctrl;
@@ -785,7 +759,7 @@ static irqreturn_t pcie_isr(int irq, void *dev_id)
785 intr_loc |= detected; 759 intr_loc |= detected;
786 if (!intr_loc) 760 if (!intr_loc)
787 return IRQ_NONE; 761 return IRQ_NONE;
788 if (pciehp_writew(ctrl, SLOTSTATUS, detected)) { 762 if (detected && pciehp_writew(ctrl, SLOTSTATUS, detected)) {
789 err("%s: Cannot write to SLOTSTATUS\n", __func__); 763 err("%s: Cannot write to SLOTSTATUS\n", __func__);
790 return IRQ_NONE; 764 return IRQ_NONE;
791 } 765 }
@@ -797,25 +771,13 @@ static irqreturn_t pcie_isr(int irq, void *dev_id)
797 if (intr_loc & CMD_COMPLETED) { 771 if (intr_loc & CMD_COMPLETED) {
798 ctrl->cmd_busy = 0; 772 ctrl->cmd_busy = 0;
799 smp_mb(); 773 smp_mb();
800 wake_up_interruptible(&ctrl->queue); 774 wake_up(&ctrl->queue);
801 } 775 }
802 776
803 if (!(intr_loc & ~CMD_COMPLETED)) 777 if (!(intr_loc & ~CMD_COMPLETED))
804 return IRQ_HANDLED; 778 return IRQ_HANDLED;
805 779
806 /*
807 * Return without handling events if this handler routine is
808 * called before controller initialization is done. This may
809 * happen if hotplug event or another interrupt that shares
810 * the IRQ with pciehp arrives before slot initialization is
811 * done after interrupt handler is registered.
812 *
813 * FIXME - Need more structural fixes. We need to be ready to
814 * handle the event before installing interrupt handler.
815 */
816 p_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset); 780 p_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
817 if (!p_slot || !p_slot->hpc_ops)
818 return IRQ_HANDLED;
819 781
820 /* Check MRL Sensor Changed */ 782 /* Check MRL Sensor Changed */
821 if (intr_loc & MRL_SENS_CHANGED) 783 if (intr_loc & MRL_SENS_CHANGED)
@@ -992,6 +954,7 @@ static int hpc_get_cur_lnk_width(struct slot *slot,
992 return retval; 954 return retval;
993} 955}
994 956
957static void pcie_release_ctrl(struct controller *ctrl);
995static struct hpc_ops pciehp_hpc_ops = { 958static struct hpc_ops pciehp_hpc_ops = {
996 .power_on_slot = hpc_power_on_slot, 959 .power_on_slot = hpc_power_on_slot,
997 .power_off_slot = hpc_power_off_slot, 960 .power_off_slot = hpc_power_off_slot,
@@ -1013,97 +976,11 @@ static struct hpc_ops pciehp_hpc_ops = {
1013 .green_led_off = hpc_set_green_led_off, 976 .green_led_off = hpc_set_green_led_off,
1014 .green_led_blink = hpc_set_green_led_blink, 977 .green_led_blink = hpc_set_green_led_blink,
1015 978
1016 .release_ctlr = hpc_release_ctlr, 979 .release_ctlr = pcie_release_ctrl,
1017 .check_lnk_status = hpc_check_lnk_status, 980 .check_lnk_status = hpc_check_lnk_status,
1018}; 981};
1019 982
1020#ifdef CONFIG_ACPI 983int pcie_enable_notification(struct controller *ctrl)
1021static int pciehp_acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev)
1022{
1023 acpi_status status;
1024 acpi_handle chandle, handle = DEVICE_ACPI_HANDLE(&(dev->dev));
1025 struct pci_dev *pdev = dev;
1026 struct pci_bus *parent;
1027 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
1028
1029 /*
1030 * Per PCI firmware specification, we should run the ACPI _OSC
1031 * method to get control of hotplug hardware before using it.
1032 * If an _OSC is missing, we look for an OSHP to do the same thing.
1033 * To handle different BIOS behavior, we look for _OSC and OSHP
1034 * within the scope of the hotplug controller and its parents, upto
1035 * the host bridge under which this controller exists.
1036 */
1037 while (!handle) {
1038 /*
1039 * This hotplug controller was not listed in the ACPI name
1040 * space at all. Try to get acpi handle of parent pci bus.
1041 */
1042 if (!pdev || !pdev->bus->parent)
1043 break;
1044 parent = pdev->bus->parent;
1045 dbg("Could not find %s in acpi namespace, trying parent\n",
1046 pci_name(pdev));
1047 if (!parent->self)
1048 /* Parent must be a host bridge */
1049 handle = acpi_get_pci_rootbridge_handle(
1050 pci_domain_nr(parent),
1051 parent->number);
1052 else
1053 handle = DEVICE_ACPI_HANDLE(
1054 &(parent->self->dev));
1055 pdev = parent->self;
1056 }
1057
1058 while (handle) {
1059 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1060 dbg("Trying to get hotplug control for %s \n",
1061 (char *)string.pointer);
1062 status = pci_osc_control_set(handle,
1063 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL |
1064 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL);
1065 if (status == AE_NOT_FOUND)
1066 status = acpi_run_oshp(handle);
1067 if (ACPI_SUCCESS(status)) {
1068 dbg("Gained control for hotplug HW for pci %s (%s)\n",
1069 pci_name(dev), (char *)string.pointer);
1070 kfree(string.pointer);
1071 return 0;
1072 }
1073 if (acpi_root_bridge(handle))
1074 break;
1075 chandle = handle;
1076 status = acpi_get_parent(chandle, &handle);
1077 if (ACPI_FAILURE(status))
1078 break;
1079 }
1080
1081 dbg("Cannot get control of hotplug hardware for pci %s\n",
1082 pci_name(dev));
1083
1084 kfree(string.pointer);
1085 return -1;
1086}
1087#endif
1088
1089static int pcie_init_hardware_part1(struct controller *ctrl,
1090 struct pcie_device *dev)
1091{
1092 /* Clear all remaining event bits in Slot Status register */
1093 if (pciehp_writew(ctrl, SLOTSTATUS, 0x1f)) {
1094 err("%s: Cannot write to SLOTSTATUS register\n", __func__);
1095 return -1;
1096 }
1097
1098 /* Mask Hot-plug Interrupt Enable */
1099 if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE)) {
1100 err("%s: Cannot mask hotplug interrupt enable\n", __func__);
1101 return -1;
1102 }
1103 return 0;
1104}
1105
1106int pcie_init_hardware_part2(struct controller *ctrl, struct pcie_device *dev)
1107{ 984{
1108 u16 cmd, mask; 985 u16 cmd, mask;
1109 986
@@ -1115,30 +992,83 @@ int pcie_init_hardware_part2(struct controller *ctrl, struct pcie_device *dev)
1115 if (MRL_SENS(ctrl)) 992 if (MRL_SENS(ctrl))
1116 cmd |= MRL_DETECT_ENABLE; 993 cmd |= MRL_DETECT_ENABLE;
1117 if (!pciehp_poll_mode) 994 if (!pciehp_poll_mode)
1118 cmd |= HP_INTR_ENABLE; 995 cmd |= HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE;
1119 996
1120 mask = PRSN_DETECT_ENABLE | ATTN_BUTTN_ENABLE | 997 mask = PRSN_DETECT_ENABLE | ATTN_BUTTN_ENABLE | MRL_DETECT_ENABLE |
1121 PWR_FAULT_DETECT_ENABLE | MRL_DETECT_ENABLE | HP_INTR_ENABLE; 998 PWR_FAULT_DETECT_ENABLE | HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE;
1122 999
1123 if (pcie_write_cmd(ctrl, cmd, mask)) { 1000 if (pcie_write_cmd(ctrl, cmd, mask)) {
1124 err("%s: Cannot enable software notification\n", __func__); 1001 err("%s: Cannot enable software notification\n", __func__);
1125 goto abort; 1002 return -1;
1126 } 1003 }
1004 return 0;
1005}
1127 1006
1128 if (pciehp_force) 1007static void pcie_disable_notification(struct controller *ctrl)
1129 dbg("Bypassing BIOS check for pciehp use on %s\n", 1008{
1130 pci_name(ctrl->pci_dev)); 1009 u16 mask;
1131 else if (pciehp_get_hp_hw_control_from_firmware(ctrl->pci_dev)) 1010 mask = PRSN_DETECT_ENABLE | ATTN_BUTTN_ENABLE | MRL_DETECT_ENABLE |
1132 goto abort_disable_intr; 1011 PWR_FAULT_DETECT_ENABLE | HP_INTR_ENABLE | CMD_CMPL_INTR_ENABLE;
1012 if (pcie_write_cmd(ctrl, 0, mask))
1013 warn("%s: Cannot disable software notification\n", __func__);
1014}
1133 1015
1016static int pcie_init_notification(struct controller *ctrl)
1017{
1018 if (pciehp_request_irq(ctrl))
1019 return -1;
1020 if (pcie_enable_notification(ctrl)) {
1021 pciehp_free_irq(ctrl);
1022 return -1;
1023 }
1134 return 0; 1024 return 0;
1025}
1135 1026
1136 /* We end up here for the many possible ways to fail this API. */ 1027static void pcie_shutdown_notification(struct controller *ctrl)
1137abort_disable_intr: 1028{
1138 if (pcie_write_cmd(ctrl, 0, HP_INTR_ENABLE)) 1029 pcie_disable_notification(ctrl);
1139 err("%s : disabling interrupts failed\n", __func__); 1030 pciehp_free_irq(ctrl);
1140abort: 1031}
1141 return -1; 1032
1033static void make_slot_name(struct slot *slot)
1034{
1035 if (pciehp_slot_with_bus)
1036 snprintf(slot->name, SLOT_NAME_SIZE, "%04d_%04d",
1037 slot->bus, slot->number);
1038 else
1039 snprintf(slot->name, SLOT_NAME_SIZE, "%d", slot->number);
1040}
1041
1042static int pcie_init_slot(struct controller *ctrl)
1043{
1044 struct slot *slot;
1045
1046 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
1047 if (!slot)
1048 return -ENOMEM;
1049
1050 slot->hp_slot = 0;
1051 slot->ctrl = ctrl;
1052 slot->bus = ctrl->pci_dev->subordinate->number;
1053 slot->device = ctrl->slot_device_offset + slot->hp_slot;
1054 slot->hpc_ops = ctrl->hpc_ops;
1055 slot->number = ctrl->first_slot;
1056 make_slot_name(slot);
1057 mutex_init(&slot->lock);
1058 INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
1059 list_add(&slot->slot_list, &ctrl->slot_list);
1060 return 0;
1061}
1062
1063static void pcie_cleanup_slot(struct controller *ctrl)
1064{
1065 struct slot *slot;
1066 slot = list_first_entry(&ctrl->slot_list, struct slot, slot_list);
1067 list_del(&slot->slot_list);
1068 cancel_delayed_work(&slot->work);
1069 flush_scheduled_work();
1070 flush_workqueue(pciehp_wq);
1071 kfree(slot);
1142} 1072}
1143 1073
1144static inline void dbg_ctrl(struct controller *ctrl) 1074static inline void dbg_ctrl(struct controller *ctrl)
@@ -1176,15 +1106,23 @@ static inline void dbg_ctrl(struct controller *ctrl)
1176 dbg(" Comamnd Completed : %3s\n", NO_CMD_CMPL(ctrl)? "no" : "yes"); 1106 dbg(" Comamnd Completed : %3s\n", NO_CMD_CMPL(ctrl)? "no" : "yes");
1177 pciehp_readw(ctrl, SLOTSTATUS, &reg16); 1107 pciehp_readw(ctrl, SLOTSTATUS, &reg16);
1178 dbg("Slot Status : 0x%04x\n", reg16); 1108 dbg("Slot Status : 0x%04x\n", reg16);
1179 pciehp_readw(ctrl, SLOTSTATUS, &reg16); 1109 pciehp_readw(ctrl, SLOTCTRL, &reg16);
1180 dbg("Slot Control : 0x%04x\n", reg16); 1110 dbg("Slot Control : 0x%04x\n", reg16);
1181} 1111}
1182 1112
1183int pcie_init(struct controller *ctrl, struct pcie_device *dev) 1113struct controller *pcie_init(struct pcie_device *dev)
1184{ 1114{
1115 struct controller *ctrl;
1185 u32 slot_cap; 1116 u32 slot_cap;
1186 struct pci_dev *pdev = dev->port; 1117 struct pci_dev *pdev = dev->port;
1187 1118
1119 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1120 if (!ctrl) {
1121 err("%s : out of memory\n", __func__);
1122 goto abort;
1123 }
1124 INIT_LIST_HEAD(&ctrl->slot_list);
1125
1188 ctrl->pci_dev = pdev; 1126 ctrl->pci_dev = pdev;
1189 ctrl->cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP); 1127 ctrl->cap_base = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1190 if (!ctrl->cap_base) { 1128 if (!ctrl->cap_base) {
@@ -1215,15 +1153,12 @@ int pcie_init(struct controller *ctrl, struct pcie_device *dev)
1215 !(POWER_CTRL(ctrl) | ATTN_LED(ctrl) | PWR_LED(ctrl) | EMI(ctrl))) 1153 !(POWER_CTRL(ctrl) | ATTN_LED(ctrl) | PWR_LED(ctrl) | EMI(ctrl)))
1216 ctrl->no_cmd_complete = 1; 1154 ctrl->no_cmd_complete = 1;
1217 1155
1218 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", 1156 /* Clear all remaining event bits in Slot Status register */
1219 pdev->vendor, pdev->device, 1157 if (pciehp_writew(ctrl, SLOTSTATUS, 0x1f))
1220 pdev->subsystem_vendor, pdev->subsystem_device); 1158 goto abort_ctrl;
1221 1159
1222 if (pcie_init_hardware_part1(ctrl, dev)) 1160 /* Disable sotfware notification */
1223 goto abort; 1161 pcie_disable_notification(ctrl);
1224
1225 if (pciehp_request_irq(ctrl))
1226 goto abort;
1227 1162
1228 /* 1163 /*
1229 * If this is the first controller to be initialized, 1164 * If this is the first controller to be initialized,
@@ -1231,18 +1166,39 @@ int pcie_init(struct controller *ctrl, struct pcie_device *dev)
1231 */ 1166 */
1232 if (atomic_add_return(1, &pciehp_num_controllers) == 1) { 1167 if (atomic_add_return(1, &pciehp_num_controllers) == 1) {
1233 pciehp_wq = create_singlethread_workqueue("pciehpd"); 1168 pciehp_wq = create_singlethread_workqueue("pciehpd");
1234 if (!pciehp_wq) { 1169 if (!pciehp_wq)
1235 goto abort_free_irq; 1170 goto abort_ctrl;
1236 }
1237 } 1171 }
1238 1172
1239 if (pcie_init_hardware_part2(ctrl, dev)) 1173 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
1240 goto abort_free_irq; 1174 pdev->vendor, pdev->device,
1175 pdev->subsystem_vendor, pdev->subsystem_device);
1176
1177 if (pcie_init_slot(ctrl))
1178 goto abort_ctrl;
1241 1179
1242 return 0; 1180 if (pcie_init_notification(ctrl))
1181 goto abort_slot;
1243 1182
1244abort_free_irq: 1183 return ctrl;
1245 pciehp_free_irq(ctrl); 1184
1185abort_slot:
1186 pcie_cleanup_slot(ctrl);
1187abort_ctrl:
1188 kfree(ctrl);
1246abort: 1189abort:
1247 return -1; 1190 return NULL;
1191}
1192
1193void pcie_release_ctrl(struct controller *ctrl)
1194{
1195 pcie_shutdown_notification(ctrl);
1196 pcie_cleanup_slot(ctrl);
1197 /*
1198 * If this is the last controller to be released, destroy the
1199 * pciehp work queue
1200 */
1201 if (atomic_dec_and_test(&pciehp_num_controllers))
1202 destroy_workqueue(pciehp_wq);
1203 kfree(ctrl);
1248} 1204}
diff --git a/drivers/pci/hotplug/rpadlpar_sysfs.c b/drivers/pci/hotplug/rpadlpar_sysfs.c
index 779c5db71be4..a796301ea03f 100644
--- a/drivers/pci/hotplug/rpadlpar_sysfs.c
+++ b/drivers/pci/hotplug/rpadlpar_sysfs.c
@@ -14,8 +14,10 @@
14 */ 14 */
15#include <linux/kobject.h> 15#include <linux/kobject.h>
16#include <linux/string.h> 16#include <linux/string.h>
17#include <linux/pci.h>
17#include <linux/pci_hotplug.h> 18#include <linux/pci_hotplug.h>
18#include "rpadlpar.h" 19#include "rpadlpar.h"
20#include "../pci.h"
19 21
20#define DLPAR_KOBJ_NAME "control" 22#define DLPAR_KOBJ_NAME "control"
21 23
@@ -27,7 +29,6 @@
27 29
28#define MAX_DRC_NAME_LEN 64 30#define MAX_DRC_NAME_LEN 64
29 31
30
31static ssize_t add_slot_store(struct kobject *kobj, struct kobj_attribute *attr, 32static ssize_t add_slot_store(struct kobject *kobj, struct kobj_attribute *attr,
32 const char *buf, size_t nbytes) 33 const char *buf, size_t nbytes)
33{ 34{
@@ -112,7 +113,7 @@ int dlpar_sysfs_init(void)
112 int error; 113 int error;
113 114
114 dlpar_kobj = kobject_create_and_add(DLPAR_KOBJ_NAME, 115 dlpar_kobj = kobject_create_and_add(DLPAR_KOBJ_NAME,
115 &pci_hotplug_slots_kset->kobj); 116 &pci_slots_kset->kobj);
116 if (!dlpar_kobj) 117 if (!dlpar_kobj)
117 return -EINVAL; 118 return -EINVAL;
118 119
diff --git a/drivers/pci/hotplug/rpaphp_slot.c b/drivers/pci/hotplug/rpaphp_slot.c
index 56197b600d36..9b714ea93d20 100644
--- a/drivers/pci/hotplug/rpaphp_slot.c
+++ b/drivers/pci/hotplug/rpaphp_slot.c
@@ -33,33 +33,6 @@
33#include <asm/rtas.h> 33#include <asm/rtas.h>
34#include "rpaphp.h" 34#include "rpaphp.h"
35 35
36static ssize_t address_read_file (struct hotplug_slot *php_slot, char *buf)
37{
38 int retval;
39 struct slot *slot = (struct slot *)php_slot->private;
40 struct pci_bus *bus;
41
42 if (!slot)
43 return -ENOENT;
44
45 bus = slot->bus;
46 if (!bus)
47 return -ENOENT;
48
49 if (bus->self)
50 retval = sprintf(buf, pci_name(bus->self));
51 else
52 retval = sprintf(buf, "%04x:%02x:00.0",
53 pci_domain_nr(bus), bus->number);
54
55 return retval;
56}
57
58static struct hotplug_slot_attribute php_attr_address = {
59 .attr = {.name = "address", .mode = S_IFREG | S_IRUGO},
60 .show = address_read_file,
61};
62
63/* free up the memory used by a slot */ 36/* free up the memory used by a slot */
64static void rpaphp_release_slot(struct hotplug_slot *hotplug_slot) 37static void rpaphp_release_slot(struct hotplug_slot *hotplug_slot)
65{ 38{
@@ -135,9 +108,6 @@ int rpaphp_deregister_slot(struct slot *slot)
135 108
136 list_del(&slot->rpaphp_slot_list); 109 list_del(&slot->rpaphp_slot_list);
137 110
138 /* remove "address" file */
139 sysfs_remove_file(&php_slot->kobj, &php_attr_address.attr);
140
141 retval = pci_hp_deregister(php_slot); 111 retval = pci_hp_deregister(php_slot);
142 if (retval) 112 if (retval)
143 err("Problem unregistering a slot %s\n", slot->name); 113 err("Problem unregistering a slot %s\n", slot->name);
@@ -151,6 +121,7 @@ int rpaphp_register_slot(struct slot *slot)
151{ 121{
152 struct hotplug_slot *php_slot = slot->hotplug_slot; 122 struct hotplug_slot *php_slot = slot->hotplug_slot;
153 int retval; 123 int retval;
124 int slotno;
154 125
155 dbg("%s registering slot:path[%s] index[%x], name[%s] pdomain[%x] type[%d]\n", 126 dbg("%s registering slot:path[%s] index[%x], name[%s] pdomain[%x] type[%d]\n",
156 __func__, slot->dn->full_name, slot->index, slot->name, 127 __func__, slot->dn->full_name, slot->index, slot->name,
@@ -162,19 +133,16 @@ int rpaphp_register_slot(struct slot *slot)
162 return -EAGAIN; 133 return -EAGAIN;
163 } 134 }
164 135
165 retval = pci_hp_register(php_slot); 136 if (slot->dn->child)
137 slotno = PCI_SLOT(PCI_DN(slot->dn->child)->devfn);
138 else
139 slotno = -1;
140 retval = pci_hp_register(php_slot, slot->bus, slotno);
166 if (retval) { 141 if (retval) {
167 err("pci_hp_register failed with error %d\n", retval); 142 err("pci_hp_register failed with error %d\n", retval);
168 return retval; 143 return retval;
169 } 144 }
170 145
171 /* create "address" file */
172 retval = sysfs_create_file(&php_slot->kobj, &php_attr_address.attr);
173 if (retval) {
174 err("sysfs_create_file failed with error %d\n", retval);
175 goto sysfs_fail;
176 }
177
178 /* add slot to our internal list */ 146 /* add slot to our internal list */
179 list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head); 147 list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
180 info("Slot [%s] registered\n", slot->name); 148 info("Slot [%s] registered\n", slot->name);
diff --git a/drivers/pci/hotplug/sgi_hotplug.c b/drivers/pci/hotplug/sgi_hotplug.c
index 2fe37cd85b69..410fe0394a8e 100644
--- a/drivers/pci/hotplug/sgi_hotplug.c
+++ b/drivers/pci/hotplug/sgi_hotplug.c
@@ -197,13 +197,15 @@ static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
197static struct hotplug_slot * sn_hp_destroy(void) 197static struct hotplug_slot * sn_hp_destroy(void)
198{ 198{
199 struct slot *slot; 199 struct slot *slot;
200 struct pci_slot *pci_slot;
200 struct hotplug_slot *bss_hotplug_slot = NULL; 201 struct hotplug_slot *bss_hotplug_slot = NULL;
201 202
202 list_for_each_entry(slot, &sn_hp_list, hp_list) { 203 list_for_each_entry(slot, &sn_hp_list, hp_list) {
203 bss_hotplug_slot = slot->hotplug_slot; 204 bss_hotplug_slot = slot->hotplug_slot;
205 pci_slot = bss_hotplug_slot->pci_slot;
204 list_del(&((struct slot *)bss_hotplug_slot->private)-> 206 list_del(&((struct slot *)bss_hotplug_slot->private)->
205 hp_list); 207 hp_list);
206 sysfs_remove_file(&bss_hotplug_slot->kobj, 208 sysfs_remove_file(&pci_slot->kobj,
207 &sn_slot_path_attr.attr); 209 &sn_slot_path_attr.attr);
208 break; 210 break;
209 } 211 }
@@ -614,6 +616,7 @@ static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
614static int sn_hotplug_slot_register(struct pci_bus *pci_bus) 616static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
615{ 617{
616 int device; 618 int device;
619 struct pci_slot *pci_slot;
617 struct hotplug_slot *bss_hotplug_slot; 620 struct hotplug_slot *bss_hotplug_slot;
618 int rc = 0; 621 int rc = 0;
619 622
@@ -650,11 +653,12 @@ static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
650 bss_hotplug_slot->ops = &sn_hotplug_slot_ops; 653 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
651 bss_hotplug_slot->release = &sn_release_slot; 654 bss_hotplug_slot->release = &sn_release_slot;
652 655
653 rc = pci_hp_register(bss_hotplug_slot); 656 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device);
654 if (rc) 657 if (rc)
655 goto register_err; 658 goto register_err;
656 659
657 rc = sysfs_create_file(&bss_hotplug_slot->kobj, 660 pci_slot = bss_hotplug_slot->pci_slot;
661 rc = sysfs_create_file(&pci_slot->kobj,
658 &sn_slot_path_attr.attr); 662 &sn_slot_path_attr.attr);
659 if (rc) 663 if (rc)
660 goto register_err; 664 goto register_err;
@@ -664,7 +668,7 @@ static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
664 668
665register_err: 669register_err:
666 dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n", 670 dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n",
667 rc); 671 rc);
668 672
669alloc_err: 673alloc_err:
670 if (rc == -ENOMEM) 674 if (rc == -ENOMEM)
diff --git a/drivers/pci/hotplug/shpchp.h b/drivers/pci/hotplug/shpchp.h
index f66e8d6315ab..8a026f750deb 100644
--- a/drivers/pci/hotplug/shpchp.h
+++ b/drivers/pci/hotplug/shpchp.h
@@ -170,6 +170,7 @@ extern void shpchp_queue_pushbutton_work(struct work_struct *work);
170extern int shpc_init( struct controller *ctrl, struct pci_dev *pdev); 170extern int shpc_init( struct controller *ctrl, struct pci_dev *pdev);
171 171
172#ifdef CONFIG_ACPI 172#ifdef CONFIG_ACPI
173#include <linux/pci-acpi.h>
173static inline int get_hp_params_from_firmware(struct pci_dev *dev, 174static inline int get_hp_params_from_firmware(struct pci_dev *dev,
174 struct hotplug_params *hpp) 175 struct hotplug_params *hpp)
175{ 176{
@@ -177,14 +178,15 @@ static inline int get_hp_params_from_firmware(struct pci_dev *dev,
177 return -ENODEV; 178 return -ENODEV;
178 return 0; 179 return 0;
179} 180}
180#define get_hp_hw_control_from_firmware(pdev) \ 181
181 do { \ 182static inline int get_hp_hw_control_from_firmware(struct pci_dev *dev)
182 if (DEVICE_ACPI_HANDLE(&(pdev->dev))) \ 183{
183 acpi_run_oshp(DEVICE_ACPI_HANDLE(&(pdev->dev)));\ 184 u32 flags = OSC_SHPC_NATIVE_HP_CONTROL;
184 } while (0) 185 return acpi_get_hp_hw_control_from_firmware(dev, flags);
186}
185#else 187#else
186#define get_hp_params_from_firmware(dev, hpp) (-ENODEV) 188#define get_hp_params_from_firmware(dev, hpp) (-ENODEV)
187#define get_hp_hw_control_from_firmware(dev) do { } while (0) 189#define get_hp_hw_control_from_firmware(dev) (0)
188#endif 190#endif
189 191
190struct ctrl_reg { 192struct ctrl_reg {
diff --git a/drivers/pci/hotplug/shpchp_core.c b/drivers/pci/hotplug/shpchp_core.c
index 97848654652a..a8cbd039b85b 100644
--- a/drivers/pci/hotplug/shpchp_core.c
+++ b/drivers/pci/hotplug/shpchp_core.c
@@ -39,7 +39,7 @@
39int shpchp_debug; 39int shpchp_debug;
40int shpchp_poll_mode; 40int shpchp_poll_mode;
41int shpchp_poll_time; 41int shpchp_poll_time;
42int shpchp_slot_with_bus; 42static int shpchp_slot_with_bus;
43struct workqueue_struct *shpchp_wq; 43struct workqueue_struct *shpchp_wq;
44 44
45#define DRIVER_VERSION "0.4" 45#define DRIVER_VERSION "0.4"
@@ -68,7 +68,6 @@ static int get_power_status (struct hotplug_slot *slot, u8 *value);
68static int get_attention_status (struct hotplug_slot *slot, u8 *value); 68static int get_attention_status (struct hotplug_slot *slot, u8 *value);
69static int get_latch_status (struct hotplug_slot *slot, u8 *value); 69static int get_latch_status (struct hotplug_slot *slot, u8 *value);
70static int get_adapter_status (struct hotplug_slot *slot, u8 *value); 70static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
71static int get_address (struct hotplug_slot *slot, u32 *value);
72static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value); 71static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
73static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value); 72static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
74 73
@@ -81,7 +80,6 @@ static struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
81 .get_attention_status = get_attention_status, 80 .get_attention_status = get_attention_status,
82 .get_latch_status = get_latch_status, 81 .get_latch_status = get_latch_status,
83 .get_adapter_status = get_adapter_status, 82 .get_adapter_status = get_adapter_status,
84 .get_address = get_address,
85 .get_max_bus_speed = get_max_bus_speed, 83 .get_max_bus_speed = get_max_bus_speed,
86 .get_cur_bus_speed = get_cur_bus_speed, 84 .get_cur_bus_speed = get_cur_bus_speed,
87}; 85};
@@ -159,7 +157,8 @@ static int init_slots(struct controller *ctrl)
159 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x " 157 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
160 "slot_device_offset=%x\n", slot->bus, slot->device, 158 "slot_device_offset=%x\n", slot->bus, slot->device,
161 slot->hp_slot, slot->number, ctrl->slot_device_offset); 159 slot->hp_slot, slot->number, ctrl->slot_device_offset);
162 retval = pci_hp_register(slot->hotplug_slot); 160 retval = pci_hp_register(slot->hotplug_slot,
161 ctrl->pci_dev->subordinate, slot->device);
163 if (retval) { 162 if (retval) {
164 err("pci_hp_register failed with error %d\n", retval); 163 err("pci_hp_register failed with error %d\n", retval);
165 if (retval == -EEXIST) 164 if (retval == -EEXIST)
@@ -288,19 +287,8 @@ static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
288 return 0; 287 return 0;
289} 288}
290 289
291static int get_address (struct hotplug_slot *hotplug_slot, u32 *value) 290static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
292{ 291 enum pci_bus_speed *value)
293 struct slot *slot = get_slot(hotplug_slot);
294 struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
295
296 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
297
298 *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
299
300 return 0;
301}
302
303static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
304{ 292{
305 struct slot *slot = get_slot(hotplug_slot); 293 struct slot *slot = get_slot(hotplug_slot);
306 int retval; 294 int retval;
@@ -330,13 +318,14 @@ static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_sp
330 318
331static int is_shpc_capable(struct pci_dev *dev) 319static int is_shpc_capable(struct pci_dev *dev)
332{ 320{
333 if ((dev->vendor == PCI_VENDOR_ID_AMD) || (dev->device == 321 if ((dev->vendor == PCI_VENDOR_ID_AMD) || (dev->device ==
334 PCI_DEVICE_ID_AMD_GOLAM_7450)) 322 PCI_DEVICE_ID_AMD_GOLAM_7450))
335 return 1; 323 return 1;
336 if (pci_find_capability(dev, PCI_CAP_ID_SHPC)) 324 if (!pci_find_capability(dev, PCI_CAP_ID_SHPC))
337 return 1; 325 return 0;
338 326 if (get_hp_hw_control_from_firmware(dev))
339 return 0; 327 return 0;
328 return 1;
340} 329}
341 330
342static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 331static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
diff --git a/drivers/pci/hotplug/shpchp_hpc.c b/drivers/pci/hotplug/shpchp_hpc.c
index 7d770b2cd889..7a0bff364cd4 100644
--- a/drivers/pci/hotplug/shpchp_hpc.c
+++ b/drivers/pci/hotplug/shpchp_hpc.c
@@ -1084,7 +1084,6 @@ int shpc_init(struct controller *ctrl, struct pci_dev *pdev)
1084 dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __func__, 1084 dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __func__,
1085 pdev->bus->number, PCI_SLOT(pdev->devfn), 1085 pdev->bus->number, PCI_SLOT(pdev->devfn),
1086 PCI_FUNC(pdev->devfn), pdev->irq); 1086 PCI_FUNC(pdev->devfn), pdev->irq);
1087 get_hp_hw_control_from_firmware(pdev);
1088 1087
1089 /* 1088 /*
1090 * If this is the first controller to be initialized, 1089 * If this is the first controller to be initialized,
diff --git a/drivers/pci/intel-iommu.c b/drivers/pci/intel-iommu.c
index bb0642318a95..3f7b81c065d2 100644
--- a/drivers/pci/intel-iommu.c
+++ b/drivers/pci/intel-iommu.c
@@ -1748,7 +1748,6 @@ int __init init_dmars(void)
1748 deferred_flush = kzalloc(g_num_of_iommus * 1748 deferred_flush = kzalloc(g_num_of_iommus *
1749 sizeof(struct deferred_flush_tables), GFP_KERNEL); 1749 sizeof(struct deferred_flush_tables), GFP_KERNEL);
1750 if (!deferred_flush) { 1750 if (!deferred_flush) {
1751 kfree(g_iommus);
1752 ret = -ENOMEM; 1751 ret = -ENOMEM;
1753 goto error; 1752 goto error;
1754 } 1753 }
diff --git a/drivers/pci/msi.c b/drivers/pci/msi.c
index 8c61304cbb37..15af618d36e2 100644
--- a/drivers/pci/msi.c
+++ b/drivers/pci/msi.c
@@ -70,12 +70,10 @@ arch_teardown_msi_irqs(struct pci_dev *dev)
70 } 70 }
71} 71}
72 72
73static void msi_set_enable(struct pci_dev *dev, int enable) 73static void __msi_set_enable(struct pci_dev *dev, int pos, int enable)
74{ 74{
75 int pos;
76 u16 control; 75 u16 control;
77 76
78 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
79 if (pos) { 77 if (pos) {
80 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control); 78 pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
81 control &= ~PCI_MSI_FLAGS_ENABLE; 79 control &= ~PCI_MSI_FLAGS_ENABLE;
@@ -85,6 +83,11 @@ static void msi_set_enable(struct pci_dev *dev, int enable)
85 } 83 }
86} 84}
87 85
86static void msi_set_enable(struct pci_dev *dev, int enable)
87{
88 __msi_set_enable(dev, pci_find_capability(dev, PCI_CAP_ID_MSI), enable);
89}
90
88static void msix_set_enable(struct pci_dev *dev, int enable) 91static void msix_set_enable(struct pci_dev *dev, int enable)
89{ 92{
90 int pos; 93 int pos;
@@ -141,7 +144,8 @@ static void msi_set_mask_bits(unsigned int irq, u32 mask, u32 flag)
141 mask_bits |= flag & mask; 144 mask_bits |= flag & mask;
142 pci_write_config_dword(entry->dev, pos, mask_bits); 145 pci_write_config_dword(entry->dev, pos, mask_bits);
143 } else { 146 } else {
144 msi_set_enable(entry->dev, !flag); 147 __msi_set_enable(entry->dev, entry->msi_attrib.pos,
148 !flag);
145 } 149 }
146 break; 150 break;
147 case PCI_CAP_ID_MSIX: 151 case PCI_CAP_ID_MSIX:
@@ -561,9 +565,8 @@ int pci_enable_msi(struct pci_dev* dev)
561 565
562 /* Check whether driver already requested for MSI-X irqs */ 566 /* Check whether driver already requested for MSI-X irqs */
563 if (dev->msix_enabled) { 567 if (dev->msix_enabled) {
564 printk(KERN_INFO "PCI: %s: Can't enable MSI. " 568 dev_info(&dev->dev, "can't enable MSI "
565 "Device already has MSI-X enabled\n", 569 "(MSI-X already enabled)\n");
566 pci_name(dev));
567 return -EINVAL; 570 return -EINVAL;
568 } 571 }
569 status = msi_capability_init(dev); 572 status = msi_capability_init(dev);
@@ -686,9 +689,8 @@ int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
686 689
687 /* Check whether driver already requested for MSI irq */ 690 /* Check whether driver already requested for MSI irq */
688 if (dev->msi_enabled) { 691 if (dev->msi_enabled) {
689 printk(KERN_INFO "PCI: %s: Can't enable MSI-X. " 692 dev_info(&dev->dev, "can't enable MSI-X "
690 "Device already has an MSI irq assigned\n", 693 "(MSI IRQ already assigned)\n");
691 pci_name(dev));
692 return -EINVAL; 694 return -EINVAL;
693 } 695 }
694 status = msix_capability_init(dev, entries, nvec); 696 status = msix_capability_init(dev, entries, nvec);
diff --git a/drivers/pci/pci-acpi.c b/drivers/pci/pci-acpi.c
index 9d6fc8e6285d..7764768b6a0e 100644
--- a/drivers/pci/pci-acpi.c
+++ b/drivers/pci/pci-acpi.c
@@ -21,12 +21,19 @@
21 21
22struct acpi_osc_data { 22struct acpi_osc_data {
23 acpi_handle handle; 23 acpi_handle handle;
24 u32 ctrlset_buf[3]; 24 u32 support_set;
25 u32 global_ctrlsets; 25 u32 control_set;
26 int is_queried;
27 u32 query_result;
26 struct list_head sibiling; 28 struct list_head sibiling;
27}; 29};
28static LIST_HEAD(acpi_osc_data_list); 30static LIST_HEAD(acpi_osc_data_list);
29 31
32struct acpi_osc_args {
33 u32 capbuf[3];
34 u32 query_result;
35};
36
30static struct acpi_osc_data *acpi_get_osc_data(acpi_handle handle) 37static struct acpi_osc_data *acpi_get_osc_data(acpi_handle handle)
31{ 38{
32 struct acpi_osc_data *data; 39 struct acpi_osc_data *data;
@@ -44,42 +51,18 @@ static struct acpi_osc_data *acpi_get_osc_data(acpi_handle handle)
44 return data; 51 return data;
45} 52}
46 53
47static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66}; 54static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
55 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
48 56
49static acpi_status 57static acpi_status acpi_run_osc(acpi_handle handle,
50acpi_query_osc ( 58 struct acpi_osc_args *osc_args)
51 acpi_handle handle,
52 u32 level,
53 void *context,
54 void **retval )
55{ 59{
56 acpi_status status; 60 acpi_status status;
57 struct acpi_object_list input; 61 struct acpi_object_list input;
58 union acpi_object in_params[4]; 62 union acpi_object in_params[4];
59 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL}; 63 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
60 union acpi_object *out_obj; 64 union acpi_object *out_obj;
61 u32 osc_dw0; 65 u32 osc_dw0, flags = osc_args->capbuf[OSC_QUERY_TYPE];
62 acpi_status *ret_status = (acpi_status *)retval;
63 struct acpi_osc_data *osc_data;
64 u32 flags = (unsigned long)context, temp;
65 acpi_handle tmp;
66
67 status = acpi_get_handle(handle, "_OSC", &tmp);
68 if (ACPI_FAILURE(status))
69 return status;
70
71 osc_data = acpi_get_osc_data(handle);
72 if (!osc_data) {
73 printk(KERN_ERR "acpi osc data array is full\n");
74 return AE_ERROR;
75 }
76
77 osc_data->ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
78
79 /* do _OSC query for all possible controls */
80 temp = osc_data->ctrlset_buf[OSC_CONTROL_TYPE];
81 osc_data->ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
82 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
83 66
84 /* Setting up input parameters */ 67 /* Setting up input parameters */
85 input.count = 4; 68 input.count = 4;
@@ -93,20 +76,19 @@ acpi_query_osc (
93 in_params[2].integer.value = 3; 76 in_params[2].integer.value = 3;
94 in_params[3].type = ACPI_TYPE_BUFFER; 77 in_params[3].type = ACPI_TYPE_BUFFER;
95 in_params[3].buffer.length = 12; 78 in_params[3].buffer.length = 12;
96 in_params[3].buffer.pointer = (u8 *)osc_data->ctrlset_buf; 79 in_params[3].buffer.pointer = (u8 *)osc_args->capbuf;
97 80
98 status = acpi_evaluate_object(handle, "_OSC", &input, &output); 81 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
99 if (ACPI_FAILURE(status)) 82 if (ACPI_FAILURE(status))
100 goto out_nofree; 83 return status;
101 out_obj = output.pointer;
102 84
85 out_obj = output.pointer;
103 if (out_obj->type != ACPI_TYPE_BUFFER) { 86 if (out_obj->type != ACPI_TYPE_BUFFER) {
104 printk(KERN_DEBUG 87 printk(KERN_DEBUG "Evaluate _OSC returns wrong type\n");
105 "Evaluate _OSC returns wrong type\n");
106 status = AE_TYPE; 88 status = AE_TYPE;
107 goto query_osc_out; 89 goto out_kfree;
108 } 90 }
109 osc_dw0 = *((u32 *) out_obj->buffer.pointer); 91 osc_dw0 = *((u32 *)out_obj->buffer.pointer);
110 if (osc_dw0) { 92 if (osc_dw0) {
111 if (osc_dw0 & OSC_REQUEST_ERROR) 93 if (osc_dw0 & OSC_REQUEST_ERROR)
112 printk(KERN_DEBUG "_OSC request fails\n"); 94 printk(KERN_DEBUG "_OSC request fails\n");
@@ -115,93 +97,58 @@ acpi_query_osc (
115 if (osc_dw0 & OSC_INVALID_REVISION_ERROR) 97 if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
116 printk(KERN_DEBUG "_OSC invalid revision\n"); 98 printk(KERN_DEBUG "_OSC invalid revision\n");
117 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) { 99 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
118 /* Update Global Control Set */ 100 if (flags & OSC_QUERY_ENABLE)
119 osc_data->global_ctrlsets = 101 goto out_success;
120 *((u32 *)(out_obj->buffer.pointer + 8)); 102 printk(KERN_DEBUG "_OSC FW not grant req. control\n");
121 status = AE_OK; 103 status = AE_SUPPORT;
122 goto query_osc_out; 104 goto out_kfree;
123 } 105 }
124 status = AE_ERROR; 106 status = AE_ERROR;
125 goto query_osc_out; 107 goto out_kfree;
126 } 108 }
127 109out_success:
128 /* Update Global Control Set */ 110 if (flags & OSC_QUERY_ENABLE)
129 osc_data->global_ctrlsets = *((u32 *)(out_obj->buffer.pointer + 8)); 111 osc_args->query_result =
112 *((u32 *)(out_obj->buffer.pointer + 8));
130 status = AE_OK; 113 status = AE_OK;
131 114
132query_osc_out: 115out_kfree:
133 kfree(output.pointer); 116 kfree(output.pointer);
134out_nofree:
135 *ret_status = status;
136
137 osc_data->ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
138 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] = temp;
139 if (ACPI_FAILURE(status)) {
140 /* no osc support at all */
141 osc_data->ctrlset_buf[OSC_SUPPORT_TYPE] = 0;
142 }
143
144 return status; 117 return status;
145} 118}
146 119
147 120static acpi_status acpi_query_osc(acpi_handle handle,
148static acpi_status 121 u32 level, void *context, void **retval)
149acpi_run_osc (
150 acpi_handle handle,
151 void *context)
152{ 122{
153 acpi_status status; 123 acpi_status status;
154 struct acpi_object_list input; 124 struct acpi_osc_data *osc_data;
155 union acpi_object in_params[4]; 125 u32 flags = (unsigned long)context, support_set;
156 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL}; 126 acpi_handle tmp;
157 union acpi_object *out_obj; 127 struct acpi_osc_args osc_args;
158 u32 osc_dw0;
159
160 /* Setting up input parameters */
161 input.count = 4;
162 input.pointer = in_params;
163 in_params[0].type = ACPI_TYPE_BUFFER;
164 in_params[0].buffer.length = 16;
165 in_params[0].buffer.pointer = OSC_UUID;
166 in_params[1].type = ACPI_TYPE_INTEGER;
167 in_params[1].integer.value = 1;
168 in_params[2].type = ACPI_TYPE_INTEGER;
169 in_params[2].integer.value = 3;
170 in_params[3].type = ACPI_TYPE_BUFFER;
171 in_params[3].buffer.length = 12;
172 in_params[3].buffer.pointer = (u8 *)context;
173 128
174 status = acpi_evaluate_object(handle, "_OSC", &input, &output); 129 status = acpi_get_handle(handle, "_OSC", &tmp);
175 if (ACPI_FAILURE (status)) 130 if (ACPI_FAILURE(status))
176 return status; 131 return status;
177 132
178 out_obj = output.pointer; 133 osc_data = acpi_get_osc_data(handle);
179 if (out_obj->type != ACPI_TYPE_BUFFER) { 134 if (!osc_data) {
180 printk(KERN_DEBUG 135 printk(KERN_ERR "acpi osc data array is full\n");
181 "Evaluate _OSC returns wrong type\n"); 136 return AE_ERROR;
182 status = AE_TYPE;
183 goto run_osc_out;
184 } 137 }
185 osc_dw0 = *((u32 *) out_obj->buffer.pointer); 138
186 if (osc_dw0) { 139 /* do _OSC query for all possible controls */
187 if (osc_dw0 & OSC_REQUEST_ERROR) 140 support_set = osc_data->support_set | (flags & OSC_SUPPORT_MASKS);
188 printk(KERN_DEBUG "_OSC request fails\n"); 141 osc_args.capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
189 if (osc_dw0 & OSC_INVALID_UUID_ERROR) 142 osc_args.capbuf[OSC_SUPPORT_TYPE] = support_set;
190 printk(KERN_DEBUG "_OSC invalid UUID\n"); 143 osc_args.capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
191 if (osc_dw0 & OSC_INVALID_REVISION_ERROR) 144
192 printk(KERN_DEBUG "_OSC invalid revision\n"); 145 status = acpi_run_osc(handle, &osc_args);
193 if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) { 146 if (ACPI_SUCCESS(status)) {
194 printk(KERN_DEBUG "_OSC FW not grant req. control\n"); 147 osc_data->support_set = support_set;
195 status = AE_SUPPORT; 148 osc_data->query_result = osc_args.query_result;
196 goto run_osc_out; 149 osc_data->is_queried = 1;
197 }
198 status = AE_ERROR;
199 goto run_osc_out;
200 } 150 }
201 status = AE_OK;
202 151
203run_osc_out:
204 kfree(output.pointer);
205 return status; 152 return status;
206} 153}
207 154
@@ -215,15 +162,11 @@ run_osc_out:
215 **/ 162 **/
216acpi_status __pci_osc_support_set(u32 flags, const char *hid) 163acpi_status __pci_osc_support_set(u32 flags, const char *hid)
217{ 164{
218 acpi_status retval = AE_NOT_FOUND; 165 if (!(flags & OSC_SUPPORT_MASKS))
219
220 if (!(flags & OSC_SUPPORT_MASKS)) {
221 return AE_TYPE; 166 return AE_TYPE;
222 } 167
223 acpi_get_devices(hid, 168 acpi_get_devices(hid, acpi_query_osc,
224 acpi_query_osc, 169 (void *)(unsigned long)flags, NULL);
225 (void *)(unsigned long)flags,
226 (void **) &retval );
227 return AE_OK; 170 return AE_OK;
228} 171}
229 172
@@ -236,10 +179,11 @@ acpi_status __pci_osc_support_set(u32 flags, const char *hid)
236 **/ 179 **/
237acpi_status pci_osc_control_set(acpi_handle handle, u32 flags) 180acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
238{ 181{
239 acpi_status status; 182 acpi_status status;
240 u32 ctrlset; 183 u32 ctrlset, control_set;
241 acpi_handle tmp; 184 acpi_handle tmp;
242 struct acpi_osc_data *osc_data; 185 struct acpi_osc_data *osc_data;
186 struct acpi_osc_args osc_args;
243 187
244 status = acpi_get_handle(handle, "_OSC", &tmp); 188 status = acpi_get_handle(handle, "_OSC", &tmp);
245 if (ACPI_FAILURE(status)) 189 if (ACPI_FAILURE(status))
@@ -252,24 +196,25 @@ acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
252 } 196 }
253 197
254 ctrlset = (flags & OSC_CONTROL_MASKS); 198 ctrlset = (flags & OSC_CONTROL_MASKS);
255 if (!ctrlset) { 199 if (!ctrlset)
256 return AE_TYPE; 200 return AE_TYPE;
257 } 201
258 if (osc_data->ctrlset_buf[OSC_SUPPORT_TYPE] && 202 if (osc_data->is_queried &&
259 ((osc_data->global_ctrlsets & ctrlset) != ctrlset)) { 203 ((osc_data->query_result & ctrlset) != ctrlset))
260 return AE_SUPPORT; 204 return AE_SUPPORT;
261 } 205
262 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset; 206 control_set = osc_data->control_set | ctrlset;
263 status = acpi_run_osc(handle, osc_data->ctrlset_buf); 207 osc_args.capbuf[OSC_QUERY_TYPE] = 0;
264 if (ACPI_FAILURE (status)) { 208 osc_args.capbuf[OSC_SUPPORT_TYPE] = osc_data->support_set;
265 osc_data->ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset; 209 osc_args.capbuf[OSC_CONTROL_TYPE] = control_set;
266 } 210 status = acpi_run_osc(handle, &osc_args);
267 211 if (ACPI_SUCCESS(status))
212 osc_data->control_set = control_set;
213
268 return status; 214 return status;
269} 215}
270EXPORT_SYMBOL(pci_osc_control_set); 216EXPORT_SYMBOL(pci_osc_control_set);
271 217
272#ifdef CONFIG_ACPI_SLEEP
273/* 218/*
274 * _SxD returns the D-state with the highest power 219 * _SxD returns the D-state with the highest power
275 * (lowest D-state number) supported in the S-state "x". 220 * (lowest D-state number) supported in the S-state "x".
@@ -293,13 +238,11 @@ EXPORT_SYMBOL(pci_osc_control_set);
293 * choose highest power _SxD or any lower power 238 * choose highest power _SxD or any lower power
294 */ 239 */
295 240
296static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev, 241static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
297 pm_message_t state)
298{ 242{
299 int acpi_state; 243 int acpi_state;
300 244
301 acpi_state = acpi_pm_device_sleep_state(&pdev->dev, 245 acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL);
302 device_may_wakeup(&pdev->dev), NULL);
303 if (acpi_state < 0) 246 if (acpi_state < 0)
304 return PCI_POWER_ERROR; 247 return PCI_POWER_ERROR;
305 248
@@ -315,7 +258,13 @@ static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev,
315 } 258 }
316 return PCI_POWER_ERROR; 259 return PCI_POWER_ERROR;
317} 260}
318#endif 261
262static bool acpi_pci_power_manageable(struct pci_dev *dev)
263{
264 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
265
266 return handle ? acpi_bus_power_manageable(handle) : false;
267}
319 268
320static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state) 269static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
321{ 270{
@@ -328,12 +277,11 @@ static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
328 [PCI_D3hot] = ACPI_STATE_D3, 277 [PCI_D3hot] = ACPI_STATE_D3,
329 [PCI_D3cold] = ACPI_STATE_D3 278 [PCI_D3cold] = ACPI_STATE_D3
330 }; 279 };
280 int error = -EINVAL;
331 281
332 if (!handle)
333 return -ENODEV;
334 /* If the ACPI device has _EJ0, ignore the device */ 282 /* If the ACPI device has _EJ0, ignore the device */
335 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp))) 283 if (!handle || ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
336 return 0; 284 return -ENODEV;
337 285
338 switch (state) { 286 switch (state) {
339 case PCI_D0: 287 case PCI_D0:
@@ -341,11 +289,41 @@ static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
341 case PCI_D2: 289 case PCI_D2:
342 case PCI_D3hot: 290 case PCI_D3hot:
343 case PCI_D3cold: 291 case PCI_D3cold:
344 return acpi_bus_set_power(handle, state_conv[state]); 292 error = acpi_bus_set_power(handle, state_conv[state]);
345 } 293 }
346 return -EINVAL; 294
295 if (!error)
296 dev_printk(KERN_INFO, &dev->dev,
297 "power state changed by ACPI to D%d\n", state);
298
299 return error;
300}
301
302static bool acpi_pci_can_wakeup(struct pci_dev *dev)
303{
304 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
305
306 return handle ? acpi_bus_can_wakeup(handle) : false;
307}
308
309static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
310{
311 int error = acpi_pm_device_sleep_wake(&dev->dev, enable);
312
313 if (!error)
314 dev_printk(KERN_INFO, &dev->dev,
315 "wake-up capability %s by ACPI\n",
316 enable ? "enabled" : "disabled");
317 return error;
347} 318}
348 319
320static struct pci_platform_pm_ops acpi_pci_platform_pm = {
321 .is_manageable = acpi_pci_power_manageable,
322 .set_state = acpi_pci_set_power_state,
323 .choose_state = acpi_pci_choose_state,
324 .can_wakeup = acpi_pci_can_wakeup,
325 .sleep_wake = acpi_pci_sleep_wake,
326};
349 327
350/* ACPI bus type */ 328/* ACPI bus type */
351static int acpi_pci_find_device(struct device *dev, acpi_handle *handle) 329static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
@@ -397,10 +375,7 @@ static int __init acpi_pci_init(void)
397 ret = register_acpi_bus_type(&acpi_pci_bus); 375 ret = register_acpi_bus_type(&acpi_pci_bus);
398 if (ret) 376 if (ret)
399 return 0; 377 return 0;
400#ifdef CONFIG_ACPI_SLEEP 378 pci_set_platform_pm(&acpi_pci_platform_pm);
401 platform_pci_choose_state = acpi_pci_choose_state;
402#endif
403 platform_pci_set_power_state = acpi_pci_set_power_state;
404 return 0; 379 return 0;
405} 380}
406arch_initcall(acpi_pci_init); 381arch_initcall(acpi_pci_init);
diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c
index e1637bd82b8e..a13f53486114 100644
--- a/drivers/pci/pci-driver.c
+++ b/drivers/pci/pci-driver.c
@@ -274,7 +274,57 @@ static int pci_device_remove(struct device * dev)
274 return 0; 274 return 0;
275} 275}
276 276
277static int pci_device_suspend(struct device * dev, pm_message_t state) 277static void pci_device_shutdown(struct device *dev)
278{
279 struct pci_dev *pci_dev = to_pci_dev(dev);
280 struct pci_driver *drv = pci_dev->driver;
281
282 if (drv && drv->shutdown)
283 drv->shutdown(pci_dev);
284 pci_msi_shutdown(pci_dev);
285 pci_msix_shutdown(pci_dev);
286}
287
288#ifdef CONFIG_PM_SLEEP
289
290/*
291 * Default "suspend" method for devices that have no driver provided suspend,
292 * or not even a driver at all.
293 */
294static void pci_default_pm_suspend(struct pci_dev *pci_dev)
295{
296 pci_save_state(pci_dev);
297 /*
298 * mark its power state as "unknown", since we don't know if
299 * e.g. the BIOS will change its device state when we suspend.
300 */
301 if (pci_dev->current_state == PCI_D0)
302 pci_dev->current_state = PCI_UNKNOWN;
303}
304
305/*
306 * Default "resume" method for devices that have no driver provided resume,
307 * or not even a driver at all.
308 */
309static int pci_default_pm_resume(struct pci_dev *pci_dev)
310{
311 int retval = 0;
312
313 /* restore the PCI config space */
314 pci_restore_state(pci_dev);
315 /* if the device was enabled before suspend, reenable */
316 retval = pci_reenable_device(pci_dev);
317 /*
318 * if the device was busmaster before the suspend, make it busmaster
319 * again
320 */
321 if (pci_dev->is_busmaster)
322 pci_set_master(pci_dev);
323
324 return retval;
325}
326
327static int pci_legacy_suspend(struct device *dev, pm_message_t state)
278{ 328{
279 struct pci_dev * pci_dev = to_pci_dev(dev); 329 struct pci_dev * pci_dev = to_pci_dev(dev);
280 struct pci_driver * drv = pci_dev->driver; 330 struct pci_driver * drv = pci_dev->driver;
@@ -284,18 +334,12 @@ static int pci_device_suspend(struct device * dev, pm_message_t state)
284 i = drv->suspend(pci_dev, state); 334 i = drv->suspend(pci_dev, state);
285 suspend_report_result(drv->suspend, i); 335 suspend_report_result(drv->suspend, i);
286 } else { 336 } else {
287 pci_save_state(pci_dev); 337 pci_default_pm_suspend(pci_dev);
288 /*
289 * mark its power state as "unknown", since we don't know if
290 * e.g. the BIOS will change its device state when we suspend.
291 */
292 if (pci_dev->current_state == PCI_D0)
293 pci_dev->current_state = PCI_UNKNOWN;
294 } 338 }
295 return i; 339 return i;
296} 340}
297 341
298static int pci_device_suspend_late(struct device * dev, pm_message_t state) 342static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
299{ 343{
300 struct pci_dev * pci_dev = to_pci_dev(dev); 344 struct pci_dev * pci_dev = to_pci_dev(dev);
301 struct pci_driver * drv = pci_dev->driver; 345 struct pci_driver * drv = pci_dev->driver;
@@ -308,26 +352,7 @@ static int pci_device_suspend_late(struct device * dev, pm_message_t state)
308 return i; 352 return i;
309} 353}
310 354
311/* 355static int pci_legacy_resume(struct device *dev)
312 * Default resume method for devices that have no driver provided resume,
313 * or not even a driver at all.
314 */
315static int pci_default_resume(struct pci_dev *pci_dev)
316{
317 int retval = 0;
318
319 /* restore the PCI config space */
320 pci_restore_state(pci_dev);
321 /* if the device was enabled before suspend, reenable */
322 retval = pci_reenable_device(pci_dev);
323 /* if the device was busmaster before the suspend, make it busmaster again */
324 if (pci_dev->is_busmaster)
325 pci_set_master(pci_dev);
326
327 return retval;
328}
329
330static int pci_device_resume(struct device * dev)
331{ 356{
332 int error; 357 int error;
333 struct pci_dev * pci_dev = to_pci_dev(dev); 358 struct pci_dev * pci_dev = to_pci_dev(dev);
@@ -336,34 +361,313 @@ static int pci_device_resume(struct device * dev)
336 if (drv && drv->resume) 361 if (drv && drv->resume)
337 error = drv->resume(pci_dev); 362 error = drv->resume(pci_dev);
338 else 363 else
339 error = pci_default_resume(pci_dev); 364 error = pci_default_pm_resume(pci_dev);
340 return error; 365 return error;
341} 366}
342 367
343static int pci_device_resume_early(struct device * dev) 368static int pci_legacy_resume_early(struct device *dev)
344{ 369{
345 int error = 0; 370 int error = 0;
346 struct pci_dev * pci_dev = to_pci_dev(dev); 371 struct pci_dev * pci_dev = to_pci_dev(dev);
347 struct pci_driver * drv = pci_dev->driver; 372 struct pci_driver * drv = pci_dev->driver;
348 373
349 pci_fixup_device(pci_fixup_resume, pci_dev);
350
351 if (drv && drv->resume_early) 374 if (drv && drv->resume_early)
352 error = drv->resume_early(pci_dev); 375 error = drv->resume_early(pci_dev);
353 return error; 376 return error;
354} 377}
355 378
356static void pci_device_shutdown(struct device *dev) 379static int pci_pm_prepare(struct device *dev)
380{
381 struct device_driver *drv = dev->driver;
382 int error = 0;
383
384 if (drv && drv->pm && drv->pm->prepare)
385 error = drv->pm->prepare(dev);
386
387 return error;
388}
389
390static void pci_pm_complete(struct device *dev)
391{
392 struct device_driver *drv = dev->driver;
393
394 if (drv && drv->pm && drv->pm->complete)
395 drv->pm->complete(dev);
396}
397
398#ifdef CONFIG_SUSPEND
399
400static int pci_pm_suspend(struct device *dev)
401{
402 struct pci_dev *pci_dev = to_pci_dev(dev);
403 struct device_driver *drv = dev->driver;
404 int error = 0;
405
406 if (drv && drv->pm) {
407 if (drv->pm->suspend) {
408 error = drv->pm->suspend(dev);
409 suspend_report_result(drv->pm->suspend, error);
410 } else {
411 pci_default_pm_suspend(pci_dev);
412 }
413 } else {
414 error = pci_legacy_suspend(dev, PMSG_SUSPEND);
415 }
416 pci_fixup_device(pci_fixup_suspend, pci_dev);
417
418 return error;
419}
420
421static int pci_pm_suspend_noirq(struct device *dev)
357{ 422{
358 struct pci_dev *pci_dev = to_pci_dev(dev); 423 struct pci_dev *pci_dev = to_pci_dev(dev);
359 struct pci_driver *drv = pci_dev->driver; 424 struct pci_driver *drv = pci_dev->driver;
425 int error = 0;
360 426
361 if (drv && drv->shutdown) 427 if (drv && drv->pm) {
362 drv->shutdown(pci_dev); 428 if (drv->pm->suspend_noirq) {
363 pci_msi_shutdown(pci_dev); 429 error = drv->pm->suspend_noirq(dev);
364 pci_msix_shutdown(pci_dev); 430 suspend_report_result(drv->pm->suspend_noirq, error);
431 }
432 } else {
433 error = pci_legacy_suspend_late(dev, PMSG_SUSPEND);
434 }
435
436 return error;
365} 437}
366 438
439static int pci_pm_resume(struct device *dev)
440{
441 struct pci_dev *pci_dev = to_pci_dev(dev);
442 struct device_driver *drv = dev->driver;
443 int error;
444
445 pci_fixup_device(pci_fixup_resume, pci_dev);
446
447 if (drv && drv->pm) {
448 error = drv->pm->resume ? drv->pm->resume(dev) :
449 pci_default_pm_resume(pci_dev);
450 } else {
451 error = pci_legacy_resume(dev);
452 }
453
454 return error;
455}
456
457static int pci_pm_resume_noirq(struct device *dev)
458{
459 struct pci_dev *pci_dev = to_pci_dev(dev);
460 struct pci_driver *drv = pci_dev->driver;
461 int error = 0;
462
463 pci_fixup_device(pci_fixup_resume_early, pci_dev);
464
465 if (drv && drv->pm) {
466 if (drv->pm->resume_noirq)
467 error = drv->pm->resume_noirq(dev);
468 } else {
469 error = pci_legacy_resume_early(dev);
470 }
471
472 return error;
473}
474
475#else /* !CONFIG_SUSPEND */
476
477#define pci_pm_suspend NULL
478#define pci_pm_suspend_noirq NULL
479#define pci_pm_resume NULL
480#define pci_pm_resume_noirq NULL
481
482#endif /* !CONFIG_SUSPEND */
483
484#ifdef CONFIG_HIBERNATION
485
486static int pci_pm_freeze(struct device *dev)
487{
488 struct pci_dev *pci_dev = to_pci_dev(dev);
489 struct device_driver *drv = dev->driver;
490 int error = 0;
491
492 if (drv && drv->pm) {
493 if (drv->pm->freeze) {
494 error = drv->pm->freeze(dev);
495 suspend_report_result(drv->pm->freeze, error);
496 } else {
497 pci_default_pm_suspend(pci_dev);
498 }
499 } else {
500 error = pci_legacy_suspend(dev, PMSG_FREEZE);
501 pci_fixup_device(pci_fixup_suspend, pci_dev);
502 }
503
504 return error;
505}
506
507static int pci_pm_freeze_noirq(struct device *dev)
508{
509 struct pci_dev *pci_dev = to_pci_dev(dev);
510 struct pci_driver *drv = pci_dev->driver;
511 int error = 0;
512
513 if (drv && drv->pm) {
514 if (drv->pm->freeze_noirq) {
515 error = drv->pm->freeze_noirq(dev);
516 suspend_report_result(drv->pm->freeze_noirq, error);
517 }
518 } else {
519 error = pci_legacy_suspend_late(dev, PMSG_FREEZE);
520 }
521
522 return error;
523}
524
525static int pci_pm_thaw(struct device *dev)
526{
527 struct device_driver *drv = dev->driver;
528 int error = 0;
529
530 if (drv && drv->pm) {
531 if (drv->pm->thaw)
532 error = drv->pm->thaw(dev);
533 } else {
534 pci_fixup_device(pci_fixup_resume, to_pci_dev(dev));
535 error = pci_legacy_resume(dev);
536 }
537
538 return error;
539}
540
541static int pci_pm_thaw_noirq(struct device *dev)
542{
543 struct pci_dev *pci_dev = to_pci_dev(dev);
544 struct pci_driver *drv = pci_dev->driver;
545 int error = 0;
546
547 if (drv && drv->pm) {
548 if (drv->pm->thaw_noirq)
549 error = drv->pm->thaw_noirq(dev);
550 } else {
551 pci_fixup_device(pci_fixup_resume_early, pci_dev);
552 error = pci_legacy_resume_early(dev);
553 }
554
555 return error;
556}
557
558static int pci_pm_poweroff(struct device *dev)
559{
560 struct device_driver *drv = dev->driver;
561 int error = 0;
562
563 pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
564
565 if (drv && drv->pm) {
566 if (drv->pm->poweroff) {
567 error = drv->pm->poweroff(dev);
568 suspend_report_result(drv->pm->poweroff, error);
569 }
570 } else {
571 error = pci_legacy_suspend(dev, PMSG_HIBERNATE);
572 }
573
574 return error;
575}
576
577static int pci_pm_poweroff_noirq(struct device *dev)
578{
579 struct pci_dev *pci_dev = to_pci_dev(dev);
580 struct pci_driver *drv = pci_dev->driver;
581 int error = 0;
582
583 if (drv && drv->pm) {
584 if (drv->pm->poweroff_noirq) {
585 error = drv->pm->poweroff_noirq(dev);
586 suspend_report_result(drv->pm->poweroff_noirq, error);
587 }
588 } else {
589 error = pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
590 }
591
592 return error;
593}
594
595static int pci_pm_restore(struct device *dev)
596{
597 struct pci_dev *pci_dev = to_pci_dev(dev);
598 struct device_driver *drv = dev->driver;
599 int error;
600
601 if (drv && drv->pm) {
602 error = drv->pm->restore ? drv->pm->restore(dev) :
603 pci_default_pm_resume(pci_dev);
604 } else {
605 error = pci_legacy_resume(dev);
606 }
607 pci_fixup_device(pci_fixup_resume, pci_dev);
608
609 return error;
610}
611
612static int pci_pm_restore_noirq(struct device *dev)
613{
614 struct pci_dev *pci_dev = to_pci_dev(dev);
615 struct pci_driver *drv = pci_dev->driver;
616 int error = 0;
617
618 pci_fixup_device(pci_fixup_resume, pci_dev);
619
620 if (drv && drv->pm) {
621 if (drv->pm->restore_noirq)
622 error = drv->pm->restore_noirq(dev);
623 } else {
624 error = pci_legacy_resume_early(dev);
625 }
626 pci_fixup_device(pci_fixup_resume_early, pci_dev);
627
628 return error;
629}
630
631#else /* !CONFIG_HIBERNATION */
632
633#define pci_pm_freeze NULL
634#define pci_pm_freeze_noirq NULL
635#define pci_pm_thaw NULL
636#define pci_pm_thaw_noirq NULL
637#define pci_pm_poweroff NULL
638#define pci_pm_poweroff_noirq NULL
639#define pci_pm_restore NULL
640#define pci_pm_restore_noirq NULL
641
642#endif /* !CONFIG_HIBERNATION */
643
644struct pm_ext_ops pci_pm_ops = {
645 .base = {
646 .prepare = pci_pm_prepare,
647 .complete = pci_pm_complete,
648 .suspend = pci_pm_suspend,
649 .resume = pci_pm_resume,
650 .freeze = pci_pm_freeze,
651 .thaw = pci_pm_thaw,
652 .poweroff = pci_pm_poweroff,
653 .restore = pci_pm_restore,
654 },
655 .suspend_noirq = pci_pm_suspend_noirq,
656 .resume_noirq = pci_pm_resume_noirq,
657 .freeze_noirq = pci_pm_freeze_noirq,
658 .thaw_noirq = pci_pm_thaw_noirq,
659 .poweroff_noirq = pci_pm_poweroff_noirq,
660 .restore_noirq = pci_pm_restore_noirq,
661};
662
663#define PCI_PM_OPS_PTR &pci_pm_ops
664
665#else /* !CONFIG_PM_SLEEP */
666
667#define PCI_PM_OPS_PTR NULL
668
669#endif /* !CONFIG_PM_SLEEP */
670
367/** 671/**
368 * __pci_register_driver - register a new pci driver 672 * __pci_register_driver - register a new pci driver
369 * @drv: the driver structure to register 673 * @drv: the driver structure to register
@@ -386,6 +690,9 @@ int __pci_register_driver(struct pci_driver *drv, struct module *owner,
386 drv->driver.owner = owner; 690 drv->driver.owner = owner;
387 drv->driver.mod_name = mod_name; 691 drv->driver.mod_name = mod_name;
388 692
693 if (drv->pm)
694 drv->driver.pm = &drv->pm->base;
695
389 spin_lock_init(&drv->dynids.lock); 696 spin_lock_init(&drv->dynids.lock);
390 INIT_LIST_HEAD(&drv->dynids.list); 697 INIT_LIST_HEAD(&drv->dynids.list);
391 698
@@ -511,12 +818,9 @@ struct bus_type pci_bus_type = {
511 .uevent = pci_uevent, 818 .uevent = pci_uevent,
512 .probe = pci_device_probe, 819 .probe = pci_device_probe,
513 .remove = pci_device_remove, 820 .remove = pci_device_remove,
514 .suspend = pci_device_suspend,
515 .suspend_late = pci_device_suspend_late,
516 .resume_early = pci_device_resume_early,
517 .resume = pci_device_resume,
518 .shutdown = pci_device_shutdown, 821 .shutdown = pci_device_shutdown,
519 .dev_attrs = pci_dev_attrs, 822 .dev_attrs = pci_dev_attrs,
823 .pm = PCI_PM_OPS_PTR,
520}; 824};
521 825
522static int __init pci_driver_init(void) 826static int __init pci_driver_init(void)
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index e4548ab2a93c..44a46c92b721 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -1,6 +1,4 @@
1/* 1/*
2 * $Id: pci.c,v 1.91 1999/01/21 13:34:01 davem Exp $
3 *
4 * PCI Bus Services, see include/linux/pci.h for further explanation. 2 * PCI Bus Services, see include/linux/pci.h for further explanation.
5 * 3 *
6 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter, 4 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
@@ -19,6 +17,7 @@
19#include <linux/string.h> 17#include <linux/string.h>
20#include <linux/log2.h> 18#include <linux/log2.h>
21#include <linux/pci-aspm.h> 19#include <linux/pci-aspm.h>
20#include <linux/pm_wakeup.h>
22#include <asm/dma.h> /* isa_dma_bridge_buggy */ 21#include <asm/dma.h> /* isa_dma_bridge_buggy */
23#include "pci.h" 22#include "pci.h"
24 23
@@ -378,74 +377,90 @@ pci_restore_bars(struct pci_dev *dev)
378 pci_update_resource(dev, &dev->resource[i], i); 377 pci_update_resource(dev, &dev->resource[i], i);
379} 378}
380 379
381int (*platform_pci_set_power_state)(struct pci_dev *dev, pci_power_t t); 380static struct pci_platform_pm_ops *pci_platform_pm;
382 381
383/** 382int pci_set_platform_pm(struct pci_platform_pm_ops *ops)
384 * pci_set_power_state - Set the power state of a PCI device
385 * @dev: PCI device to be suspended
386 * @state: PCI power state (D0, D1, D2, D3hot, D3cold) we're entering
387 *
388 * Transition a device to a new power state, using the Power Management
389 * Capabilities in the device's config space.
390 *
391 * RETURN VALUE:
392 * -EINVAL if trying to enter a lower state than we're already in.
393 * 0 if we're already in the requested state.
394 * -EIO if device does not support PCI PM.
395 * 0 if we can successfully change the power state.
396 */
397int
398pci_set_power_state(struct pci_dev *dev, pci_power_t state)
399{ 383{
400 int pm, need_restore = 0; 384 if (!ops->is_manageable || !ops->set_state || !ops->choose_state
401 u16 pmcsr, pmc; 385 || !ops->sleep_wake || !ops->can_wakeup)
386 return -EINVAL;
387 pci_platform_pm = ops;
388 return 0;
389}
402 390
403 /* bound the state we're entering */ 391static inline bool platform_pci_power_manageable(struct pci_dev *dev)
404 if (state > PCI_D3hot) 392{
405 state = PCI_D3hot; 393 return pci_platform_pm ? pci_platform_pm->is_manageable(dev) : false;
394}
406 395
407 /* 396static inline int platform_pci_set_power_state(struct pci_dev *dev,
408 * If the device or the parent bridge can't support PCI PM, ignore 397 pci_power_t t)
409 * the request if we're doing anything besides putting it into D0 398{
410 * (which would only happen on boot). 399 return pci_platform_pm ? pci_platform_pm->set_state(dev, t) : -ENOSYS;
411 */ 400}
412 if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
413 return 0;
414 401
415 /* find PCI PM capability in list */ 402static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev)
416 pm = pci_find_capability(dev, PCI_CAP_ID_PM); 403{
404 return pci_platform_pm ?
405 pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR;
406}
417 407
418 /* abort if the device doesn't support PM capabilities */ 408static inline bool platform_pci_can_wakeup(struct pci_dev *dev)
419 if (!pm) 409{
410 return pci_platform_pm ? pci_platform_pm->can_wakeup(dev) : false;
411}
412
413static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable)
414{
415 return pci_platform_pm ?
416 pci_platform_pm->sleep_wake(dev, enable) : -ENODEV;
417}
418
419/**
420 * pci_raw_set_power_state - Use PCI PM registers to set the power state of
421 * given PCI device
422 * @dev: PCI device to handle.
423 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
424 *
425 * RETURN VALUE:
426 * -EINVAL if the requested state is invalid.
427 * -EIO if device does not support PCI PM or its PM capabilities register has a
428 * wrong version, or device doesn't support the requested state.
429 * 0 if device already is in the requested state.
430 * 0 if device's power state has been successfully changed.
431 */
432static int
433pci_raw_set_power_state(struct pci_dev *dev, pci_power_t state)
434{
435 u16 pmcsr;
436 bool need_restore = false;
437
438 if (!dev->pm_cap)
420 return -EIO; 439 return -EIO;
421 440
441 if (state < PCI_D0 || state > PCI_D3hot)
442 return -EINVAL;
443
422 /* Validate current state: 444 /* Validate current state:
423 * Can enter D0 from any state, but if we can only go deeper 445 * Can enter D0 from any state, but if we can only go deeper
424 * to sleep if we're already in a low power state 446 * to sleep if we're already in a low power state
425 */ 447 */
426 if (state != PCI_D0 && dev->current_state > state) { 448 if (dev->current_state == state) {
427 printk(KERN_ERR "%s(): %s: state=%d, current state=%d\n", 449 /* we're already there */
428 __func__, pci_name(dev), state, dev->current_state); 450 return 0;
451 } else if (state != PCI_D0 && dev->current_state <= PCI_D3cold
452 && dev->current_state > state) {
453 dev_err(&dev->dev, "invalid power transition "
454 "(from state %d to %d)\n", dev->current_state, state);
429 return -EINVAL; 455 return -EINVAL;
430 } else if (dev->current_state == state)
431 return 0; /* we're already there */
432
433
434 pci_read_config_word(dev,pm + PCI_PM_PMC,&pmc);
435 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
436 printk(KERN_DEBUG
437 "PCI: %s has unsupported PM cap regs version (%u)\n",
438 pci_name(dev), pmc & PCI_PM_CAP_VER_MASK);
439 return -EIO;
440 } 456 }
441 457
442 /* check if this device supports the desired state */ 458 /* check if this device supports the desired state */
443 if (state == PCI_D1 && !(pmc & PCI_PM_CAP_D1)) 459 if ((state == PCI_D1 && !dev->d1_support)
444 return -EIO; 460 || (state == PCI_D2 && !dev->d2_support))
445 else if (state == PCI_D2 && !(pmc & PCI_PM_CAP_D2))
446 return -EIO; 461 return -EIO;
447 462
448 pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr); 463 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
449 464
450 /* If we're (effectively) in D3, force entire word to 0. 465 /* If we're (effectively) in D3, force entire word to 0.
451 * This doesn't affect PME_Status, disables PME_En, and 466 * This doesn't affect PME_Status, disables PME_En, and
@@ -461,7 +476,7 @@ pci_set_power_state(struct pci_dev *dev, pci_power_t state)
461 case PCI_UNKNOWN: /* Boot-up */ 476 case PCI_UNKNOWN: /* Boot-up */
462 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot 477 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
463 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET)) 478 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
464 need_restore = 1; 479 need_restore = true;
465 /* Fall-through: force to D0 */ 480 /* Fall-through: force to D0 */
466 default: 481 default:
467 pmcsr = 0; 482 pmcsr = 0;
@@ -469,7 +484,7 @@ pci_set_power_state(struct pci_dev *dev, pci_power_t state)
469 } 484 }
470 485
471 /* enter specified state */ 486 /* enter specified state */
472 pci_write_config_word(dev, pm + PCI_PM_CTRL, pmcsr); 487 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
473 488
474 /* Mandatory power management transition delays */ 489 /* Mandatory power management transition delays */
475 /* see PCI PM 1.1 5.6.1 table 18 */ 490 /* see PCI PM 1.1 5.6.1 table 18 */
@@ -478,13 +493,6 @@ pci_set_power_state(struct pci_dev *dev, pci_power_t state)
478 else if (state == PCI_D2 || dev->current_state == PCI_D2) 493 else if (state == PCI_D2 || dev->current_state == PCI_D2)
479 udelay(200); 494 udelay(200);
480 495
481 /*
482 * Give firmware a chance to be called, such as ACPI _PRx, _PSx
483 * Firmware method after native method ?
484 */
485 if (platform_pci_set_power_state)
486 platform_pci_set_power_state(dev, state);
487
488 dev->current_state = state; 496 dev->current_state = state;
489 497
490 /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT 498 /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
@@ -508,8 +516,77 @@ pci_set_power_state(struct pci_dev *dev, pci_power_t state)
508 return 0; 516 return 0;
509} 517}
510 518
511pci_power_t (*platform_pci_choose_state)(struct pci_dev *dev, pm_message_t state); 519/**
512 520 * pci_update_current_state - Read PCI power state of given device from its
521 * PCI PM registers and cache it
522 * @dev: PCI device to handle.
523 */
524static void pci_update_current_state(struct pci_dev *dev)
525{
526 if (dev->pm_cap) {
527 u16 pmcsr;
528
529 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
530 dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
531 }
532}
533
534/**
535 * pci_set_power_state - Set the power state of a PCI device
536 * @dev: PCI device to handle.
537 * @state: PCI power state (D0, D1, D2, D3hot) to put the device into.
538 *
539 * Transition a device to a new power state, using the platform formware and/or
540 * the device's PCI PM registers.
541 *
542 * RETURN VALUE:
543 * -EINVAL if the requested state is invalid.
544 * -EIO if device does not support PCI PM or its PM capabilities register has a
545 * wrong version, or device doesn't support the requested state.
546 * 0 if device already is in the requested state.
547 * 0 if device's power state has been successfully changed.
548 */
549int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
550{
551 int error;
552
553 /* bound the state we're entering */
554 if (state > PCI_D3hot)
555 state = PCI_D3hot;
556 else if (state < PCI_D0)
557 state = PCI_D0;
558 else if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
559 /*
560 * If the device or the parent bridge do not support PCI PM,
561 * ignore the request if we're doing anything other than putting
562 * it into D0 (which would only happen on boot).
563 */
564 return 0;
565
566 if (state == PCI_D0 && platform_pci_power_manageable(dev)) {
567 /*
568 * Allow the platform to change the state, for example via ACPI
569 * _PR0, _PS0 and some such, but do not trust it.
570 */
571 int ret = platform_pci_set_power_state(dev, PCI_D0);
572 if (!ret)
573 pci_update_current_state(dev);
574 }
575
576 error = pci_raw_set_power_state(dev, state);
577
578 if (state > PCI_D0 && platform_pci_power_manageable(dev)) {
579 /* Allow the platform to finalize the transition */
580 int ret = platform_pci_set_power_state(dev, state);
581 if (!ret) {
582 pci_update_current_state(dev);
583 error = 0;
584 }
585 }
586
587 return error;
588}
589
513/** 590/**
514 * pci_choose_state - Choose the power state of a PCI device 591 * pci_choose_state - Choose the power state of a PCI device
515 * @dev: PCI device to be suspended 592 * @dev: PCI device to be suspended
@@ -527,11 +604,9 @@ pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
527 if (!pci_find_capability(dev, PCI_CAP_ID_PM)) 604 if (!pci_find_capability(dev, PCI_CAP_ID_PM))
528 return PCI_D0; 605 return PCI_D0;
529 606
530 if (platform_pci_choose_state) { 607 ret = platform_pci_choose_state(dev);
531 ret = platform_pci_choose_state(dev, state); 608 if (ret != PCI_POWER_ERROR)
532 if (ret != PCI_POWER_ERROR) 609 return ret;
533 return ret;
534 }
535 610
536 switch (state.event) { 611 switch (state.event) {
537 case PM_EVENT_ON: 612 case PM_EVENT_ON:
@@ -543,7 +618,8 @@ pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
543 case PM_EVENT_HIBERNATE: 618 case PM_EVENT_HIBERNATE:
544 return PCI_D3hot; 619 return PCI_D3hot;
545 default: 620 default:
546 printk("Unrecognized suspend event %d\n", state.event); 621 dev_info(&dev->dev, "unrecognized suspend event %d\n",
622 state.event);
547 BUG(); 623 BUG();
548 } 624 }
549 return PCI_D0; 625 return PCI_D0;
@@ -568,7 +644,7 @@ static int pci_save_pcie_state(struct pci_dev *dev)
568 else 644 else
569 found = 1; 645 found = 1;
570 if (!save_state) { 646 if (!save_state) {
571 dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n"); 647 dev_err(&dev->dev, "out of memory in pci_save_pcie_state\n");
572 return -ENOMEM; 648 return -ENOMEM;
573 } 649 }
574 cap = (u16 *)&save_state->data[0]; 650 cap = (u16 *)&save_state->data[0];
@@ -619,7 +695,7 @@ static int pci_save_pcix_state(struct pci_dev *dev)
619 else 695 else
620 found = 1; 696 found = 1;
621 if (!save_state) { 697 if (!save_state) {
622 dev_err(&dev->dev, "Out of memory in pci_save_pcie_state\n"); 698 dev_err(&dev->dev, "out of memory in pci_save_pcie_state\n");
623 return -ENOMEM; 699 return -ENOMEM;
624 } 700 }
625 cap = (u16 *)&save_state->data[0]; 701 cap = (u16 *)&save_state->data[0];
@@ -685,10 +761,9 @@ pci_restore_state(struct pci_dev *dev)
685 for (i = 15; i >= 0; i--) { 761 for (i = 15; i >= 0; i--) {
686 pci_read_config_dword(dev, i * 4, &val); 762 pci_read_config_dword(dev, i * 4, &val);
687 if (val != dev->saved_config_space[i]) { 763 if (val != dev->saved_config_space[i]) {
688 printk(KERN_DEBUG "PM: Writing back config space on " 764 dev_printk(KERN_DEBUG, &dev->dev, "restoring config "
689 "device %s at offset %x (was %x, writing %x)\n", 765 "space at offset %#x (was %#x, writing %#x)\n",
690 pci_name(dev), i, 766 i, val, (int)dev->saved_config_space[i]);
691 val, (int)dev->saved_config_space[i]);
692 pci_write_config_dword(dev,i * 4, 767 pci_write_config_dword(dev,i * 4,
693 dev->saved_config_space[i]); 768 dev->saved_config_space[i]);
694 } 769 }
@@ -961,6 +1036,46 @@ int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
961} 1036}
962 1037
963/** 1038/**
1039 * pci_pme_capable - check the capability of PCI device to generate PME#
1040 * @dev: PCI device to handle.
1041 * @state: PCI state from which device will issue PME#.
1042 */
1043static bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
1044{
1045 if (!dev->pm_cap)
1046 return false;
1047
1048 return !!(dev->pme_support & (1 << state));
1049}
1050
1051/**
1052 * pci_pme_active - enable or disable PCI device's PME# function
1053 * @dev: PCI device to handle.
1054 * @enable: 'true' to enable PME# generation; 'false' to disable it.
1055 *
1056 * The caller must verify that the device is capable of generating PME# before
1057 * calling this function with @enable equal to 'true'.
1058 */
1059static void pci_pme_active(struct pci_dev *dev, bool enable)
1060{
1061 u16 pmcsr;
1062
1063 if (!dev->pm_cap)
1064 return;
1065
1066 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
1067 /* Clear PME_Status by writing 1 to it and enable PME# */
1068 pmcsr |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1069 if (!enable)
1070 pmcsr &= ~PCI_PM_CTRL_PME_ENABLE;
1071
1072 pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr);
1073
1074 dev_printk(KERN_INFO, &dev->dev, "PME# %s\n",
1075 enable ? "enabled" : "disabled");
1076}
1077
1078/**
964 * pci_enable_wake - enable PCI device as wakeup event source 1079 * pci_enable_wake - enable PCI device as wakeup event source
965 * @dev: PCI device affected 1080 * @dev: PCI device affected
966 * @state: PCI state from which device will issue wakeup events 1081 * @state: PCI state from which device will issue wakeup events
@@ -971,66 +1086,173 @@ int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
971 * called automatically by this routine. 1086 * called automatically by this routine.
972 * 1087 *
973 * Devices with legacy power management (no standard PCI PM capabilities) 1088 * Devices with legacy power management (no standard PCI PM capabilities)
974 * always require such platform hooks. Depending on the platform, devices 1089 * always require such platform hooks.
975 * supporting the standard PCI PME# signal may require such platform hooks;
976 * they always update bits in config space to allow PME# generation.
977 * 1090 *
978 * -EIO is returned if the device can't ever be a wakeup event source. 1091 * RETURN VALUE:
979 * -EINVAL is returned if the device can't generate wakeup events from 1092 * 0 is returned on success
980 * the specified PCI state. Returns zero if the operation is successful. 1093 * -EINVAL is returned if device is not supposed to wake up the system
1094 * Error code depending on the platform is returned if both the platform and
1095 * the native mechanism fail to enable the generation of wake-up events
981 */ 1096 */
982int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable) 1097int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable)
983{ 1098{
984 int pm; 1099 int error = 0;
985 int status; 1100 bool pme_done = false;
986 u16 value; 1101
987 1102 if (!device_may_wakeup(&dev->dev))
988 /* Note that drivers should verify device_may_wakeup(&dev->dev) 1103 return -EINVAL;
989 * before calling this function. Platform code should report 1104
990 * errors when drivers try to enable wakeup on devices that 1105 /*
991 * can't issue wakeups, or on which wakeups were disabled by 1106 * According to "PCI System Architecture" 4th ed. by Tom Shanley & Don
992 * userspace updating the /sys/devices.../power/wakeup file. 1107 * Anderson we should be doing PME# wake enable followed by ACPI wake
1108 * enable. To disable wake-up we call the platform first, for symmetry.
993 */ 1109 */
994 1110
995 status = call_platform_enable_wakeup(&dev->dev, enable); 1111 if (!enable && platform_pci_can_wakeup(dev))
1112 error = platform_pci_sleep_wake(dev, false);
996 1113
997 /* find PCI PM capability in list */ 1114 if (!enable || pci_pme_capable(dev, state)) {
998 pm = pci_find_capability(dev, PCI_CAP_ID_PM); 1115 pci_pme_active(dev, enable);
1116 pme_done = true;
1117 }
999 1118
1000 /* If device doesn't support PM Capabilities, but caller wants to 1119 if (enable && platform_pci_can_wakeup(dev))
1001 * disable wake events, it's a NOP. Otherwise fail unless the 1120 error = platform_pci_sleep_wake(dev, true);
1002 * platform hooks handled this legacy device already.
1003 */
1004 if (!pm)
1005 return enable ? status : 0;
1006 1121
1007 /* Check device's ability to generate PME# */ 1122 return pme_done ? 0 : error;
1008 pci_read_config_word(dev,pm+PCI_PM_PMC,&value); 1123}
1009 1124
1010 value &= PCI_PM_CAP_PME_MASK; 1125/**
1011 value >>= ffs(PCI_PM_CAP_PME_MASK) - 1; /* First bit of mask */ 1126 * pci_prepare_to_sleep - prepare PCI device for system-wide transition into
1127 * a sleep state
1128 * @dev: Device to handle.
1129 *
1130 * Choose the power state appropriate for the device depending on whether
1131 * it can wake up the system and/or is power manageable by the platform
1132 * (PCI_D3hot is the default) and put the device into that state.
1133 */
1134int pci_prepare_to_sleep(struct pci_dev *dev)
1135{
1136 pci_power_t target_state = PCI_D3hot;
1137 int error;
1012 1138
1013 /* Check if it can generate PME# from requested state. */ 1139 if (platform_pci_power_manageable(dev)) {
1014 if (!value || !(value & (1 << state))) { 1140 /*
1015 /* if it can't, revert what the platform hook changed, 1141 * Call the platform to choose the target state of the device
1016 * always reporting the base "EINVAL, can't PME#" error 1142 * and enable wake-up from this state if supported.
1017 */ 1143 */
1018 if (enable) 1144 pci_power_t state = platform_pci_choose_state(dev);
1019 call_platform_enable_wakeup(&dev->dev, 0); 1145
1020 return enable ? -EINVAL : 0; 1146 switch (state) {
1147 case PCI_POWER_ERROR:
1148 case PCI_UNKNOWN:
1149 break;
1150 case PCI_D1:
1151 case PCI_D2:
1152 if (pci_no_d1d2(dev))
1153 break;
1154 default:
1155 target_state = state;
1156 }
1157 } else if (device_may_wakeup(&dev->dev)) {
1158 /*
1159 * Find the deepest state from which the device can generate
1160 * wake-up events, make it the target state and enable device
1161 * to generate PME#.
1162 */
1163 if (!dev->pm_cap)
1164 return -EIO;
1165
1166 if (dev->pme_support) {
1167 while (target_state
1168 && !(dev->pme_support & (1 << target_state)))
1169 target_state--;
1170 }
1021 } 1171 }
1022 1172
1023 pci_read_config_word(dev, pm + PCI_PM_CTRL, &value); 1173 pci_enable_wake(dev, target_state, true);
1024 1174
1025 /* Clear PME_Status by writing 1 to it and enable PME# */ 1175 error = pci_set_power_state(dev, target_state);
1026 value |= PCI_PM_CTRL_PME_STATUS | PCI_PM_CTRL_PME_ENABLE;
1027 1176
1028 if (!enable) 1177 if (error)
1029 value &= ~PCI_PM_CTRL_PME_ENABLE; 1178 pci_enable_wake(dev, target_state, false);
1030 1179
1031 pci_write_config_word(dev, pm + PCI_PM_CTRL, value); 1180 return error;
1181}
1032 1182
1033 return 0; 1183/**
1184 * pci_back_from_sleep - turn PCI device on during system-wide transition into
1185 * the working state a sleep state
1186 * @dev: Device to handle.
1187 *
1188 * Disable device's sytem wake-up capability and put it into D0.
1189 */
1190int pci_back_from_sleep(struct pci_dev *dev)
1191{
1192 pci_enable_wake(dev, PCI_D0, false);
1193 return pci_set_power_state(dev, PCI_D0);
1194}
1195
1196/**
1197 * pci_pm_init - Initialize PM functions of given PCI device
1198 * @dev: PCI device to handle.
1199 */
1200void pci_pm_init(struct pci_dev *dev)
1201{
1202 int pm;
1203 u16 pmc;
1204
1205 dev->pm_cap = 0;
1206
1207 /* find PCI PM capability in list */
1208 pm = pci_find_capability(dev, PCI_CAP_ID_PM);
1209 if (!pm)
1210 return;
1211 /* Check device's ability to generate PME# */
1212 pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
1213
1214 if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
1215 dev_err(&dev->dev, "unsupported PM cap regs version (%u)\n",
1216 pmc & PCI_PM_CAP_VER_MASK);
1217 return;
1218 }
1219
1220 dev->pm_cap = pm;
1221
1222 dev->d1_support = false;
1223 dev->d2_support = false;
1224 if (!pci_no_d1d2(dev)) {
1225 if (pmc & PCI_PM_CAP_D1) {
1226 dev_printk(KERN_DEBUG, &dev->dev, "supports D1\n");
1227 dev->d1_support = true;
1228 }
1229 if (pmc & PCI_PM_CAP_D2) {
1230 dev_printk(KERN_DEBUG, &dev->dev, "supports D2\n");
1231 dev->d2_support = true;
1232 }
1233 }
1234
1235 pmc &= PCI_PM_CAP_PME_MASK;
1236 if (pmc) {
1237 dev_printk(KERN_INFO, &dev->dev,
1238 "PME# supported from%s%s%s%s%s\n",
1239 (pmc & PCI_PM_CAP_PME_D0) ? " D0" : "",
1240 (pmc & PCI_PM_CAP_PME_D1) ? " D1" : "",
1241 (pmc & PCI_PM_CAP_PME_D2) ? " D2" : "",
1242 (pmc & PCI_PM_CAP_PME_D3) ? " D3hot" : "",
1243 (pmc & PCI_PM_CAP_PME_D3cold) ? " D3cold" : "");
1244 dev->pme_support = pmc >> PCI_PM_CAP_PME_SHIFT;
1245 /*
1246 * Make device's PM flags reflect the wake-up capability, but
1247 * let the user space enable it to wake up the system as needed.
1248 */
1249 device_set_wakeup_capable(&dev->dev, true);
1250 device_set_wakeup_enable(&dev->dev, false);
1251 /* Disable the PME# generation functionality */
1252 pci_pme_active(dev, false);
1253 } else {
1254 dev->pme_support = 0;
1255 }
1034} 1256}
1035 1257
1036int 1258int
@@ -1116,13 +1338,11 @@ int pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
1116 return 0; 1338 return 0;
1117 1339
1118err_out: 1340err_out:
1119 printk (KERN_WARNING "PCI: Unable to reserve %s region #%d:%llx@%llx " 1341 dev_warn(&pdev->dev, "BAR %d: can't reserve %s region [%#llx-%#llx]\n",
1120 "for device %s\n", 1342 bar,
1121 pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem", 1343 pci_resource_flags(pdev, bar) & IORESOURCE_IO ? "I/O" : "mem",
1122 bar + 1, /* PCI BAR # */ 1344 (unsigned long long)pci_resource_start(pdev, bar),
1123 (unsigned long long)pci_resource_len(pdev, bar), 1345 (unsigned long long)pci_resource_end(pdev, bar));
1124 (unsigned long long)pci_resource_start(pdev, bar),
1125 pci_name(pdev));
1126 return -EBUSY; 1346 return -EBUSY;
1127} 1347}
1128 1348
@@ -1214,7 +1434,7 @@ pci_set_master(struct pci_dev *dev)
1214 1434
1215 pci_read_config_word(dev, PCI_COMMAND, &cmd); 1435 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1216 if (! (cmd & PCI_COMMAND_MASTER)) { 1436 if (! (cmd & PCI_COMMAND_MASTER)) {
1217 pr_debug("PCI: Enabling bus mastering for device %s\n", pci_name(dev)); 1437 dev_dbg(&dev->dev, "enabling bus mastering\n");
1218 cmd |= PCI_COMMAND_MASTER; 1438 cmd |= PCI_COMMAND_MASTER;
1219 pci_write_config_word(dev, PCI_COMMAND, cmd); 1439 pci_write_config_word(dev, PCI_COMMAND, cmd);
1220 } 1440 }
@@ -1279,8 +1499,8 @@ pci_set_cacheline_size(struct pci_dev *dev)
1279 if (cacheline_size == pci_cache_line_size) 1499 if (cacheline_size == pci_cache_line_size)
1280 return 0; 1500 return 0;
1281 1501
1282 printk(KERN_DEBUG "PCI: cache line size of %d is not supported " 1502 dev_printk(KERN_DEBUG, &dev->dev, "cache line size of %d is not "
1283 "by device %s\n", pci_cache_line_size << 2, pci_name(dev)); 1503 "supported\n", pci_cache_line_size << 2);
1284 1504
1285 return -EINVAL; 1505 return -EINVAL;
1286} 1506}
@@ -1305,8 +1525,7 @@ pci_set_mwi(struct pci_dev *dev)
1305 1525
1306 pci_read_config_word(dev, PCI_COMMAND, &cmd); 1526 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1307 if (! (cmd & PCI_COMMAND_INVALIDATE)) { 1527 if (! (cmd & PCI_COMMAND_INVALIDATE)) {
1308 pr_debug("PCI: Enabling Mem-Wr-Inval for device %s\n", 1528 dev_dbg(&dev->dev, "enabling Mem-Wr-Inval\n");
1309 pci_name(dev));
1310 cmd |= PCI_COMMAND_INVALIDATE; 1529 cmd |= PCI_COMMAND_INVALIDATE;
1311 pci_write_config_word(dev, PCI_COMMAND, cmd); 1530 pci_write_config_word(dev, PCI_COMMAND, cmd);
1312 } 1531 }
@@ -1702,5 +1921,7 @@ EXPORT_SYMBOL(pci_set_power_state);
1702EXPORT_SYMBOL(pci_save_state); 1921EXPORT_SYMBOL(pci_save_state);
1703EXPORT_SYMBOL(pci_restore_state); 1922EXPORT_SYMBOL(pci_restore_state);
1704EXPORT_SYMBOL(pci_enable_wake); 1923EXPORT_SYMBOL(pci_enable_wake);
1924EXPORT_SYMBOL(pci_prepare_to_sleep);
1925EXPORT_SYMBOL(pci_back_from_sleep);
1705EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state); 1926EXPORT_SYMBOL_GPL(pci_set_pcie_reset_state);
1706 1927
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 00408c97e5fc..d807cd786f20 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -5,11 +5,36 @@ extern int pci_create_sysfs_dev_files(struct pci_dev *pdev);
5extern void pci_remove_sysfs_dev_files(struct pci_dev *pdev); 5extern void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
6extern void pci_cleanup_rom(struct pci_dev *dev); 6extern void pci_cleanup_rom(struct pci_dev *dev);
7 7
8/* Firmware callbacks */ 8/**
9extern pci_power_t (*platform_pci_choose_state)(struct pci_dev *dev, 9 * Firmware PM callbacks
10 pm_message_t state); 10 *
11extern int (*platform_pci_set_power_state)(struct pci_dev *dev, 11 * @is_manageable - returns 'true' if given device is power manageable by the
12 pci_power_t state); 12 * platform firmware
13 *
14 * @set_state - invokes the platform firmware to set the device's power state
15 *
16 * @choose_state - returns PCI power state of given device preferred by the
17 * platform; to be used during system-wide transitions from a
18 * sleeping state to the working state and vice versa
19 *
20 * @can_wakeup - returns 'true' if given device is capable of waking up the
21 * system from a sleeping state
22 *
23 * @sleep_wake - enables/disables the system wake up capability of given device
24 *
25 * If given platform is generally capable of power managing PCI devices, all of
26 * these callbacks are mandatory.
27 */
28struct pci_platform_pm_ops {
29 bool (*is_manageable)(struct pci_dev *dev);
30 int (*set_state)(struct pci_dev *dev, pci_power_t state);
31 pci_power_t (*choose_state)(struct pci_dev *dev);
32 bool (*can_wakeup)(struct pci_dev *dev);
33 int (*sleep_wake)(struct pci_dev *dev, bool enable);
34};
35
36extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
37extern void pci_pm_init(struct pci_dev *dev);
13 38
14extern int pci_user_read_config_byte(struct pci_dev *dev, int where, u8 *val); 39extern int pci_user_read_config_byte(struct pci_dev *dev, int where, u8 *val);
15extern int pci_user_read_config_word(struct pci_dev *dev, int where, u16 *val); 40extern int pci_user_read_config_word(struct pci_dev *dev, int where, u16 *val);
@@ -106,3 +131,16 @@ pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
106} 131}
107 132
108struct pci_dev *pci_find_upstream_pcie_bridge(struct pci_dev *pdev); 133struct pci_dev *pci_find_upstream_pcie_bridge(struct pci_dev *pdev);
134
135/* PCI slot sysfs helper code */
136#define to_pci_slot(s) container_of(s, struct pci_slot, kobj)
137
138extern struct kset *pci_slots_kset;
139
140struct pci_slot_attribute {
141 struct attribute attr;
142 ssize_t (*show)(struct pci_slot *, char *);
143 ssize_t (*store)(struct pci_slot *, const char *, size_t);
144};
145#define to_pci_slot_attr(s) container_of(s, struct pci_slot_attribute, attr)
146
diff --git a/drivers/pci/pcie/aer/aerdrv.c b/drivers/pci/pcie/aer/aerdrv.c
index 07c3bdb6edc2..77036f46acfe 100644
--- a/drivers/pci/pcie/aer/aerdrv.c
+++ b/drivers/pci/pcie/aer/aerdrv.c
@@ -26,6 +26,7 @@
26#include <linux/pcieport_if.h> 26#include <linux/pcieport_if.h>
27 27
28#include "aerdrv.h" 28#include "aerdrv.h"
29#include "../../pci.h"
29 30
30/* 31/*
31 * Version Information 32 * Version Information
@@ -219,8 +220,7 @@ static int __devinit aer_probe (struct pcie_device *dev,
219 220
220 /* Alloc rpc data structure */ 221 /* Alloc rpc data structure */
221 if (!(rpc = aer_alloc_rpc(dev))) { 222 if (!(rpc = aer_alloc_rpc(dev))) {
222 printk(KERN_DEBUG "%s: Alloc rpc fails on PCIE device[%s]\n", 223 dev_printk(KERN_DEBUG, device, "alloc rpc failed\n");
223 __func__, device->bus_id);
224 aer_remove(dev); 224 aer_remove(dev);
225 return -ENOMEM; 225 return -ENOMEM;
226 } 226 }
@@ -228,8 +228,7 @@ static int __devinit aer_probe (struct pcie_device *dev,
228 /* Request IRQ ISR */ 228 /* Request IRQ ISR */
229 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv", 229 if ((status = request_irq(dev->irq, aer_irq, IRQF_SHARED, "aerdrv",
230 dev))) { 230 dev))) {
231 printk(KERN_DEBUG "%s: Request ISR fails on PCIE device[%s]\n", 231 dev_printk(KERN_DEBUG, device, "request IRQ failed\n");
232 __func__, device->bus_id);
233 aer_remove(dev); 232 aer_remove(dev);
234 return status; 233 return status;
235 } 234 }
@@ -273,7 +272,7 @@ static pci_ers_result_t aer_root_reset(struct pci_dev *dev)
273 * to issue Configuration Requests to those devices. 272 * to issue Configuration Requests to those devices.
274 */ 273 */
275 msleep(200); 274 msleep(200);
276 printk(KERN_DEBUG "Complete link reset at Root[%s]\n", dev->dev.bus_id); 275 dev_printk(KERN_DEBUG, &dev->dev, "Root Port link has been reset\n");
277 276
278 /* Enable Root Port's interrupt in response to error messages */ 277 /* Enable Root Port's interrupt in response to error messages */
279 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status); 278 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS, &status);
diff --git a/drivers/pci/pcie/aer/aerdrv_acpi.c b/drivers/pci/pcie/aer/aerdrv_acpi.c
index d39a78dbd026..30f581b8791f 100644
--- a/drivers/pci/pcie/aer/aerdrv_acpi.c
+++ b/drivers/pci/pcie/aer/aerdrv_acpi.c
@@ -50,10 +50,10 @@ int aer_osc_setup(struct pcie_device *pciedev)
50 } 50 }
51 51
52 if (ACPI_FAILURE(status)) { 52 if (ACPI_FAILURE(status)) {
53 printk(KERN_DEBUG "AER service couldn't init device %s - %s\n", 53 dev_printk(KERN_DEBUG, &pciedev->device, "AER service couldn't "
54 pciedev->device.bus_id, 54 "init device: %s\n",
55 (status == AE_SUPPORT || status == AE_NOT_FOUND) ? 55 (status == AE_SUPPORT || status == AE_NOT_FOUND) ?
56 "no _OSC support" : "Run ACPI _OSC fails"); 56 "no _OSC support" : "_OSC failed");
57 return -1; 57 return -1;
58 } 58 }
59 59
diff --git a/drivers/pci/pcie/aer/aerdrv_core.c b/drivers/pci/pcie/aer/aerdrv_core.c
index aaa82392d1dc..ee5e7b5176d0 100644
--- a/drivers/pci/pcie/aer/aerdrv_core.c
+++ b/drivers/pci/pcie/aer/aerdrv_core.c
@@ -221,9 +221,9 @@ static void report_error_detected(struct pci_dev *dev, void *data)
221 * of a driver for this device is unaware of 221 * of a driver for this device is unaware of
222 * its hw state. 222 * its hw state.
223 */ 223 */
224 printk(KERN_DEBUG "Device ID[%s] has %s\n", 224 dev_printk(KERN_DEBUG, &dev->dev, "device has %s\n",
225 dev->dev.bus_id, (dev->driver) ? 225 dev->driver ?
226 "no AER-aware driver" : "no driver"); 226 "no AER-aware driver" : "no driver");
227 } 227 }
228 return; 228 return;
229 } 229 }
@@ -304,7 +304,7 @@ static pci_ers_result_t broadcast_error_message(struct pci_dev *dev,
304{ 304{
305 struct aer_broadcast_data result_data; 305 struct aer_broadcast_data result_data;
306 306
307 printk(KERN_DEBUG "Broadcast %s message\n", error_mesg); 307 dev_printk(KERN_DEBUG, &dev->dev, "broadcast %s message\n", error_mesg);
308 result_data.state = state; 308 result_data.state = state;
309 if (cb == report_error_detected) 309 if (cb == report_error_detected)
310 result_data.result = PCI_ERS_RESULT_CAN_RECOVER; 310 result_data.result = PCI_ERS_RESULT_CAN_RECOVER;
@@ -404,18 +404,16 @@ static pci_ers_result_t reset_link(struct pcie_device *aerdev,
404 data.aer_driver = 404 data.aer_driver =
405 to_service_driver(aerdev->device.driver); 405 to_service_driver(aerdev->device.driver);
406 } else { 406 } else {
407 printk(KERN_DEBUG "No link-reset support to Device ID" 407 dev_printk(KERN_DEBUG, &dev->dev, "no link-reset "
408 "[%s]\n", 408 "support\n");
409 dev->dev.bus_id);
410 return PCI_ERS_RESULT_DISCONNECT; 409 return PCI_ERS_RESULT_DISCONNECT;
411 } 410 }
412 } 411 }
413 412
414 status = data.aer_driver->reset_link(udev); 413 status = data.aer_driver->reset_link(udev);
415 if (status != PCI_ERS_RESULT_RECOVERED) { 414 if (status != PCI_ERS_RESULT_RECOVERED) {
416 printk(KERN_DEBUG "Link reset at upstream Device ID" 415 dev_printk(KERN_DEBUG, &dev->dev, "link reset at upstream "
417 "[%s] failed\n", 416 "device %s failed\n", pci_name(udev));
418 udev->dev.bus_id);
419 return PCI_ERS_RESULT_DISCONNECT; 417 return PCI_ERS_RESULT_DISCONNECT;
420 } 418 }
421 419
@@ -511,10 +509,12 @@ static void handle_error_source(struct pcie_device * aerdev,
511 } else { 509 } else {
512 status = do_recovery(aerdev, dev, info.severity); 510 status = do_recovery(aerdev, dev, info.severity);
513 if (status == PCI_ERS_RESULT_RECOVERED) { 511 if (status == PCI_ERS_RESULT_RECOVERED) {
514 printk(KERN_DEBUG "AER driver successfully recovered\n"); 512 dev_printk(KERN_DEBUG, &dev->dev, "AER driver "
513 "successfully recovered\n");
515 } else { 514 } else {
516 /* TODO: Should kernel panic here? */ 515 /* TODO: Should kernel panic here? */
517 printk(KERN_DEBUG "AER driver didn't recover\n"); 516 dev_printk(KERN_DEBUG, &dev->dev, "AER driver didn't "
517 "recover\n");
518 } 518 }
519 } 519 }
520} 520}
diff --git a/drivers/pci/pcie/portdrv_bus.c b/drivers/pci/pcie/portdrv_bus.c
index 3f0976868eda..359fe5568df1 100644
--- a/drivers/pci/pcie/portdrv_bus.c
+++ b/drivers/pci/pcie/portdrv_bus.c
@@ -13,6 +13,7 @@
13#include <linux/pm.h> 13#include <linux/pm.h>
14 14
15#include <linux/pcieport_if.h> 15#include <linux/pcieport_if.h>
16#include "portdrv.h"
16 17
17static int pcie_port_bus_match(struct device *dev, struct device_driver *drv); 18static int pcie_port_bus_match(struct device *dev, struct device_driver *drv);
18static int pcie_port_bus_suspend(struct device *dev, pm_message_t state); 19static int pcie_port_bus_suspend(struct device *dev, pm_message_t state);
diff --git a/drivers/pci/pcie/portdrv_core.c b/drivers/pci/pcie/portdrv_core.c
index fb0abfa508dc..890f0d2b370a 100644
--- a/drivers/pci/pcie/portdrv_core.c
+++ b/drivers/pci/pcie/portdrv_core.c
@@ -23,20 +23,20 @@ static int pcie_port_probe_service(struct device *dev)
23{ 23{
24 struct pcie_device *pciedev; 24 struct pcie_device *pciedev;
25 struct pcie_port_service_driver *driver; 25 struct pcie_port_service_driver *driver;
26 int status = -ENODEV; 26 int status;
27 27
28 if (!dev || !dev->driver) 28 if (!dev || !dev->driver)
29 return status; 29 return -ENODEV;
30 30
31 driver = to_service_driver(dev->driver); 31 driver = to_service_driver(dev->driver);
32 if (!driver || !driver->probe) 32 if (!driver || !driver->probe)
33 return status; 33 return -ENODEV;
34 34
35 pciedev = to_pcie_device(dev); 35 pciedev = to_pcie_device(dev);
36 status = driver->probe(pciedev, driver->id_table); 36 status = driver->probe(pciedev, driver->id_table);
37 if (!status) { 37 if (!status) {
38 printk(KERN_DEBUG "Load service driver %s on pcie device %s\n", 38 dev_printk(KERN_DEBUG, dev, "service driver %s loaded\n",
39 driver->name, dev->bus_id); 39 driver->name);
40 get_device(dev); 40 get_device(dev);
41 } 41 }
42 return status; 42 return status;
@@ -53,8 +53,8 @@ static int pcie_port_remove_service(struct device *dev)
53 pciedev = to_pcie_device(dev); 53 pciedev = to_pcie_device(dev);
54 driver = to_service_driver(dev->driver); 54 driver = to_service_driver(dev->driver);
55 if (driver && driver->remove) { 55 if (driver && driver->remove) {
56 printk(KERN_DEBUG "Unload service driver %s on pcie device %s\n", 56 dev_printk(KERN_DEBUG, dev, "unloading service driver %s\n",
57 driver->name, dev->bus_id); 57 driver->name);
58 driver->remove(pciedev); 58 driver->remove(pciedev);
59 put_device(dev); 59 put_device(dev);
60 } 60 }
@@ -103,7 +103,7 @@ static int pcie_port_resume_service(struct device *dev)
103 */ 103 */
104static void release_pcie_device(struct device *dev) 104static void release_pcie_device(struct device *dev)
105{ 105{
106 printk(KERN_DEBUG "Free Port Service[%s]\n", dev->bus_id); 106 dev_printk(KERN_DEBUG, dev, "free port service\n");
107 kfree(to_pcie_device(dev)); 107 kfree(to_pcie_device(dev));
108} 108}
109 109
@@ -150,7 +150,7 @@ static int assign_interrupt_mode(struct pci_dev *dev, int *vectors, int mask)
150 if (pos) { 150 if (pos) {
151 struct msix_entry msix_entries[PCIE_PORT_DEVICE_MAXSERVICES] = 151 struct msix_entry msix_entries[PCIE_PORT_DEVICE_MAXSERVICES] =
152 {{0, 0}, {0, 1}, {0, 2}, {0, 3}}; 152 {{0, 0}, {0, 1}, {0, 2}, {0, 3}};
153 printk("%s Found MSIX capability\n", __func__); 153 dev_info(&dev->dev, "found MSI-X capability\n");
154 status = pci_enable_msix(dev, msix_entries, nvec); 154 status = pci_enable_msix(dev, msix_entries, nvec);
155 if (!status) { 155 if (!status) {
156 int j = 0; 156 int j = 0;
@@ -165,7 +165,7 @@ static int assign_interrupt_mode(struct pci_dev *dev, int *vectors, int mask)
165 if (status) { 165 if (status) {
166 pos = pci_find_capability(dev, PCI_CAP_ID_MSI); 166 pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
167 if (pos) { 167 if (pos) {
168 printk("%s Found MSI capability\n", __func__); 168 dev_info(&dev->dev, "found MSI capability\n");
169 status = pci_enable_msi(dev); 169 status = pci_enable_msi(dev);
170 if (!status) { 170 if (!status) {
171 interrupt_mode = PCIE_PORT_MSI_MODE; 171 interrupt_mode = PCIE_PORT_MSI_MODE;
@@ -252,7 +252,7 @@ static struct pcie_device* alloc_pcie_device(struct pci_dev *parent,
252 return NULL; 252 return NULL;
253 253
254 pcie_device_init(parent, device, port_type, service_type, irq,irq_mode); 254 pcie_device_init(parent, device, port_type, service_type, irq,irq_mode);
255 printk(KERN_DEBUG "Allocate Port Service[%s]\n", device->device.bus_id); 255 dev_printk(KERN_DEBUG, &device->device, "allocate port service\n");
256 return device; 256 return device;
257} 257}
258 258
diff --git a/drivers/pci/pcie/portdrv_pci.c b/drivers/pci/pcie/portdrv_pci.c
index 51d163238d93..367c9c20000d 100644
--- a/drivers/pci/pcie/portdrv_pci.c
+++ b/drivers/pci/pcie/portdrv_pci.c
@@ -91,9 +91,8 @@ static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
91 91
92 pci_set_master(dev); 92 pci_set_master(dev);
93 if (!dev->irq && dev->pin) { 93 if (!dev->irq && dev->pin) {
94 printk(KERN_WARNING 94 dev_warn(&dev->dev, "device [%04x/%04x] has invalid IRQ; "
95 "%s->Dev[%04x:%04x] has invalid IRQ. Check vendor BIOS\n", 95 "check vendor BIOS\n", dev->vendor, dev->device);
96 __func__, dev->vendor, dev->device);
97 } 96 }
98 if (pcie_port_device_register(dev)) { 97 if (pcie_port_device_register(dev)) {
99 pci_disable_device(dev); 98 pci_disable_device(dev);
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 3706ce7972dd..b1724cf31b66 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -277,8 +277,8 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
277 res->end = res->start + sz64; 277 res->end = res->start + sz64;
278#else 278#else
279 if (sz64 > 0x100000000ULL) { 279 if (sz64 > 0x100000000ULL) {
280 printk(KERN_ERR "PCI: Unable to handle 64-bit " 280 dev_err(&dev->dev, "BAR %d: can't handle 64-bit"
281 "BAR for device %s\n", pci_name(dev)); 281 " BAR\n", pos);
282 res->start = 0; 282 res->start = 0;
283 res->flags = 0; 283 res->flags = 0;
284 } else if (lhi) { 284 } else if (lhi) {
@@ -329,7 +329,7 @@ void __devinit pci_read_bridge_bases(struct pci_bus *child)
329 return; 329 return;
330 330
331 if (dev->transparent) { 331 if (dev->transparent) {
332 printk(KERN_INFO "PCI: Transparent bridge - %s\n", pci_name(dev)); 332 dev_info(&dev->dev, "transparent bridge\n");
333 for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++) 333 for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
334 child->resource[i] = child->parent->resource[i - 3]; 334 child->resource[i] = child->parent->resource[i - 3];
335 } 335 }
@@ -392,7 +392,8 @@ void __devinit pci_read_bridge_bases(struct pci_bus *child)
392 limit |= ((long) mem_limit_hi) << 32; 392 limit |= ((long) mem_limit_hi) << 32;
393#else 393#else
394 if (mem_base_hi || mem_limit_hi) { 394 if (mem_base_hi || mem_limit_hi) {
395 printk(KERN_ERR "PCI: Unable to handle 64-bit address space for bridge %s\n", pci_name(dev)); 395 dev_err(&dev->dev, "can't handle 64-bit "
396 "address space for bridge\n");
396 return; 397 return;
397 } 398 }
398#endif 399#endif
@@ -414,6 +415,7 @@ static struct pci_bus * pci_alloc_bus(void)
414 INIT_LIST_HEAD(&b->node); 415 INIT_LIST_HEAD(&b->node);
415 INIT_LIST_HEAD(&b->children); 416 INIT_LIST_HEAD(&b->children);
416 INIT_LIST_HEAD(&b->devices); 417 INIT_LIST_HEAD(&b->devices);
418 INIT_LIST_HEAD(&b->slots);
417 } 419 }
418 return b; 420 return b;
419} 421}
@@ -511,8 +513,8 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max,
511 513
512 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses); 514 pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
513 515
514 pr_debug("PCI: Scanning behind PCI bridge %s, config %06x, pass %d\n", 516 dev_dbg(&dev->dev, "scanning behind bridge, config %06x, pass %d\n",
515 pci_name(dev), buses & 0xffffff, pass); 517 buses & 0xffffff, pass);
516 518
517 /* Disable MasterAbortMode during probing to avoid reporting 519 /* Disable MasterAbortMode during probing to avoid reporting
518 of bus errors (in some architectures) */ 520 of bus errors (in some architectures) */
@@ -535,8 +537,8 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max,
535 * ignore it. This can happen with the i450NX chipset. 537 * ignore it. This can happen with the i450NX chipset.
536 */ 538 */
537 if (pci_find_bus(pci_domain_nr(bus), busnr)) { 539 if (pci_find_bus(pci_domain_nr(bus), busnr)) {
538 printk(KERN_INFO "PCI: Bus %04x:%02x already known\n", 540 dev_info(&dev->dev, "bus %04x:%02x already known\n",
539 pci_domain_nr(bus), busnr); 541 pci_domain_nr(bus), busnr);
540 goto out; 542 goto out;
541 } 543 }
542 544
@@ -711,8 +713,9 @@ static int pci_setup_device(struct pci_dev * dev)
711{ 713{
712 u32 class; 714 u32 class;
713 715
714 sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus), 716 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
715 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn)); 717 dev->bus->number, PCI_SLOT(dev->devfn),
718 PCI_FUNC(dev->devfn));
716 719
717 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class); 720 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
718 dev->revision = class & 0xff; 721 dev->revision = class & 0xff;
@@ -720,7 +723,7 @@ static int pci_setup_device(struct pci_dev * dev)
720 dev->class = class; 723 dev->class = class;
721 class >>= 8; 724 class >>= 8;
722 725
723 pr_debug("PCI: Found %s [%04x/%04x] %06x %02x\n", pci_name(dev), 726 dev_dbg(&dev->dev, "found [%04x/%04x] class %06x header type %02x\n",
724 dev->vendor, dev->device, class, dev->hdr_type); 727 dev->vendor, dev->device, class, dev->hdr_type);
725 728
726 /* "Unknown power state" */ 729 /* "Unknown power state" */
@@ -788,13 +791,13 @@ static int pci_setup_device(struct pci_dev * dev)
788 break; 791 break;
789 792
790 default: /* unknown header */ 793 default: /* unknown header */
791 printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n", 794 dev_err(&dev->dev, "unknown header type %02x, "
792 pci_name(dev), dev->hdr_type); 795 "ignoring device\n", dev->hdr_type);
793 return -1; 796 return -1;
794 797
795 bad: 798 bad:
796 printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n", 799 dev_err(&dev->dev, "ignoring class %02x (doesn't match header "
797 pci_name(dev), class, dev->hdr_type); 800 "type %02x)\n", class, dev->hdr_type);
798 dev->class = PCI_CLASS_NOT_DEFINED; 801 dev->class = PCI_CLASS_NOT_DEFINED;
799 } 802 }
800 803
@@ -927,7 +930,7 @@ static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
927 return NULL; 930 return NULL;
928 /* Card hasn't responded in 60 seconds? Must be stuck. */ 931 /* Card hasn't responded in 60 seconds? Must be stuck. */
929 if (delay > 60 * 1000) { 932 if (delay > 60 * 1000) {
930 printk(KERN_WARNING "Device %04x:%02x:%02x.%d not " 933 printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
931 "responding\n", pci_domain_nr(bus), 934 "responding\n", pci_domain_nr(bus),
932 bus->number, PCI_SLOT(devfn), 935 bus->number, PCI_SLOT(devfn),
933 PCI_FUNC(devfn)); 936 PCI_FUNC(devfn));
@@ -984,6 +987,9 @@ void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
984 /* Fix up broken headers */ 987 /* Fix up broken headers */
985 pci_fixup_device(pci_fixup_header, dev); 988 pci_fixup_device(pci_fixup_header, dev);
986 989
990 /* Initialize power management of the device */
991 pci_pm_init(dev);
992
987 /* 993 /*
988 * Add the device to our list of discovered devices 994 * Add the device to our list of discovered devices
989 * and the bus list for fixup functions, etc. 995 * and the bus list for fixup functions, etc.
diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c
index 963a97642ae9..4400dffbd93a 100644
--- a/drivers/pci/proc.c
+++ b/drivers/pci/proc.c
@@ -1,6 +1,4 @@
1/* 1/*
2 * $Id: proc.c,v 1.13 1998/05/12 07:36:07 mj Exp $
3 *
4 * Procfs interface for the PCI bus. 2 * Procfs interface for the PCI bus.
5 * 3 *
6 * Copyright (c) 1997--1999 Martin Mares <mj@ucw.cz> 4 * Copyright (c) 1997--1999 Martin Mares <mj@ucw.cz>
@@ -482,5 +480,5 @@ static int __init pci_proc_init(void)
482 return 0; 480 return 0;
483} 481}
484 482
485__initcall(pci_proc_init); 483device_initcall(pci_proc_init);
486 484
diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
index 338a3f94b4d4..12d489395fad 100644
--- a/drivers/pci/quirks.c
+++ b/drivers/pci/quirks.c
@@ -556,7 +556,7 @@ static void quirk_via_ioapic(struct pci_dev *dev)
556 pci_write_config_byte (dev, 0x58, tmp); 556 pci_write_config_byte (dev, 0x58, tmp);
557} 557}
558DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686, quirk_via_ioapic); 558DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686, quirk_via_ioapic);
559DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686, quirk_via_ioapic); 559DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686, quirk_via_ioapic);
560 560
561/* 561/*
562 * VIA 8237: Some BIOSs don't set the 'Bypass APIC De-Assert Message' Bit. 562 * VIA 8237: Some BIOSs don't set the 'Bypass APIC De-Assert Message' Bit.
@@ -576,7 +576,7 @@ static void quirk_via_vt8237_bypass_apic_deassert(struct pci_dev *dev)
576 } 576 }
577} 577}
578DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237, quirk_via_vt8237_bypass_apic_deassert); 578DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237, quirk_via_vt8237_bypass_apic_deassert);
579DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237, quirk_via_vt8237_bypass_apic_deassert); 579DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237, quirk_via_vt8237_bypass_apic_deassert);
580 580
581/* 581/*
582 * The AMD io apic can hang the box when an apic irq is masked. 582 * The AMD io apic can hang the box when an apic irq is masked.
@@ -622,7 +622,7 @@ static void quirk_amd_8131_ioapic(struct pci_dev *dev)
622 } 622 }
623} 623}
624DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE, quirk_amd_8131_ioapic); 624DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE, quirk_amd_8131_ioapic);
625DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE, quirk_amd_8131_ioapic); 625DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE, quirk_amd_8131_ioapic);
626#endif /* CONFIG_X86_IO_APIC */ 626#endif /* CONFIG_X86_IO_APIC */
627 627
628/* 628/*
@@ -774,7 +774,7 @@ static void quirk_cardbus_legacy(struct pci_dev *dev)
774 pci_write_config_dword(dev, PCI_CB_LEGACY_MODE_BASE, 0); 774 pci_write_config_dword(dev, PCI_CB_LEGACY_MODE_BASE, 0);
775} 775}
776DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_cardbus_legacy); 776DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_cardbus_legacy);
777DECLARE_PCI_FIXUP_RESUME(PCI_ANY_ID, PCI_ANY_ID, quirk_cardbus_legacy); 777DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_ANY_ID, PCI_ANY_ID, quirk_cardbus_legacy);
778 778
779/* 779/*
780 * Following the PCI ordering rules is optional on the AMD762. I'm not 780 * Following the PCI ordering rules is optional on the AMD762. I'm not
@@ -797,7 +797,7 @@ static void quirk_amd_ordering(struct pci_dev *dev)
797 } 797 }
798} 798}
799DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C, quirk_amd_ordering); 799DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C, quirk_amd_ordering);
800DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C, quirk_amd_ordering); 800DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C, quirk_amd_ordering);
801 801
802/* 802/*
803 * DreamWorks provided workaround for Dunord I-3000 problem 803 * DreamWorks provided workaround for Dunord I-3000 problem
@@ -865,7 +865,7 @@ static void quirk_disable_pxb(struct pci_dev *pdev)
865 } 865 }
866} 866}
867DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82454NX, quirk_disable_pxb); 867DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82454NX, quirk_disable_pxb);
868DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82454NX, quirk_disable_pxb); 868DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82454NX, quirk_disable_pxb);
869 869
870static void __devinit quirk_amd_ide_mode(struct pci_dev *pdev) 870static void __devinit quirk_amd_ide_mode(struct pci_dev *pdev)
871{ 871{
@@ -885,9 +885,9 @@ static void __devinit quirk_amd_ide_mode(struct pci_dev *pdev)
885 } 885 }
886} 886}
887DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_SATA, quirk_amd_ide_mode); 887DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_SATA, quirk_amd_ide_mode);
888DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_SATA, quirk_amd_ide_mode); 888DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP600_SATA, quirk_amd_ide_mode);
889DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP700_SATA, quirk_amd_ide_mode); 889DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP700_SATA, quirk_amd_ide_mode);
890DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP700_SATA, quirk_amd_ide_mode); 890DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_IXP700_SATA, quirk_amd_ide_mode);
891 891
892/* 892/*
893 * Serverworks CSB5 IDE does not fully support native mode 893 * Serverworks CSB5 IDE does not fully support native mode
@@ -1054,6 +1054,20 @@ static void __init asus_hides_smbus_hostbridge(struct pci_dev *dev)
1054 * its on-board VGA controller */ 1054 * its on-board VGA controller */
1055 asus_hides_smbus = 1; 1055 asus_hides_smbus = 1;
1056 } 1056 }
1057 else if (dev->device == PCI_DEVICE_ID_INTEL_82845G_IG)
1058 switch(dev->subsystem_device) {
1059 case 0x00b8: /* Compaq Evo D510 CMT */
1060 case 0x00b9: /* Compaq Evo D510 SFF */
1061 asus_hides_smbus = 1;
1062 }
1063 else if (dev->device == PCI_DEVICE_ID_INTEL_82815_CGC)
1064 switch (dev->subsystem_device) {
1065 case 0x001A: /* Compaq Deskpro EN SSF P667 815E */
1066 /* Motherboard doesn't have host bridge
1067 * subvendor/subdevice IDs, therefore checking
1068 * its on-board VGA controller */
1069 asus_hides_smbus = 1;
1070 }
1057 } 1071 }
1058} 1072}
1059DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82845_HB, asus_hides_smbus_hostbridge); 1073DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82845_HB, asus_hides_smbus_hostbridge);
@@ -1068,6 +1082,8 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82855GM_HB, as
1068DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82915GM_HB, asus_hides_smbus_hostbridge); 1082DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82915GM_HB, asus_hides_smbus_hostbridge);
1069 1083
1070DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810_IG3, asus_hides_smbus_hostbridge); 1084DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82810_IG3, asus_hides_smbus_hostbridge);
1085DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82845G_IG, asus_hides_smbus_hostbridge);
1086DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82815_CGC, asus_hides_smbus_hostbridge);
1071 1087
1072static void asus_hides_smbus_lpc(struct pci_dev *dev) 1088static void asus_hides_smbus_lpc(struct pci_dev *dev)
1073{ 1089{
@@ -1093,31 +1109,61 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, asu
1093DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, asus_hides_smbus_lpc); 1109DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, asus_hides_smbus_lpc);
1094DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, asus_hides_smbus_lpc); 1110DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, asus_hides_smbus_lpc);
1095DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, asus_hides_smbus_lpc); 1111DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, asus_hides_smbus_lpc);
1096DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, asus_hides_smbus_lpc); 1112DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, asus_hides_smbus_lpc);
1097DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, asus_hides_smbus_lpc); 1113DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, asus_hides_smbus_lpc);
1098DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, asus_hides_smbus_lpc); 1114DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, asus_hides_smbus_lpc);
1099DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, asus_hides_smbus_lpc); 1115DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, asus_hides_smbus_lpc);
1100DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, asus_hides_smbus_lpc); 1116DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, asus_hides_smbus_lpc);
1101DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, asus_hides_smbus_lpc); 1117DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_12, asus_hides_smbus_lpc);
1102DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, asus_hides_smbus_lpc); 1118DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801EB_0, asus_hides_smbus_lpc);
1103 1119
1104static void asus_hides_smbus_lpc_ich6(struct pci_dev *dev) 1120/* It appears we just have one such device. If not, we have a warning */
1121static void __iomem *asus_rcba_base;
1122static void asus_hides_smbus_lpc_ich6_suspend(struct pci_dev *dev)
1105{ 1123{
1106 u32 val, rcba; 1124 u32 rcba;
1107 void __iomem *base;
1108 1125
1109 if (likely(!asus_hides_smbus)) 1126 if (likely(!asus_hides_smbus))
1110 return; 1127 return;
1128 WARN_ON(asus_rcba_base);
1129
1111 pci_read_config_dword(dev, 0xF0, &rcba); 1130 pci_read_config_dword(dev, 0xF0, &rcba);
1112 base = ioremap_nocache(rcba & 0xFFFFC000, 0x4000); /* use bits 31:14, 16 kB aligned */ 1131 /* use bits 31:14, 16 kB aligned */
1113 if (base == NULL) return; 1132 asus_rcba_base = ioremap_nocache(rcba & 0xFFFFC000, 0x4000);
1114 val=readl(base + 0x3418); /* read the Function Disable register, dword mode only */ 1133 if (asus_rcba_base == NULL)
1115 writel(val & 0xFFFFFFF7, base + 0x3418); /* enable the SMBus device */ 1134 return;
1116 iounmap(base); 1135}
1136
1137static void asus_hides_smbus_lpc_ich6_resume_early(struct pci_dev *dev)
1138{
1139 u32 val;
1140
1141 if (likely(!asus_hides_smbus || !asus_rcba_base))
1142 return;
1143 /* read the Function Disable register, dword mode only */
1144 val = readl(asus_rcba_base + 0x3418);
1145 writel(val & 0xFFFFFFF7, asus_rcba_base + 0x3418); /* enable the SMBus device */
1146}
1147
1148static void asus_hides_smbus_lpc_ich6_resume(struct pci_dev *dev)
1149{
1150 if (likely(!asus_hides_smbus || !asus_rcba_base))
1151 return;
1152 iounmap(asus_rcba_base);
1153 asus_rcba_base = NULL;
1117 dev_info(&dev->dev, "Enabled ICH6/i801 SMBus device\n"); 1154 dev_info(&dev->dev, "Enabled ICH6/i801 SMBus device\n");
1118} 1155}
1156
1157static void asus_hides_smbus_lpc_ich6(struct pci_dev *dev)
1158{
1159 asus_hides_smbus_lpc_ich6_suspend(dev);
1160 asus_hides_smbus_lpc_ich6_resume_early(dev);
1161 asus_hides_smbus_lpc_ich6_resume(dev);
1162}
1119DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, asus_hides_smbus_lpc_ich6); 1163DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, asus_hides_smbus_lpc_ich6);
1120DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, asus_hides_smbus_lpc_ich6); 1164DECLARE_PCI_FIXUP_SUSPEND(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, asus_hides_smbus_lpc_ich6_suspend);
1165DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, asus_hides_smbus_lpc_ich6_resume);
1166DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH6_1, asus_hides_smbus_lpc_ich6_resume_early);
1121 1167
1122/* 1168/*
1123 * SiS 96x south bridge: BIOS typically hides SMBus device... 1169 * SiS 96x south bridge: BIOS typically hides SMBus device...
@@ -1135,10 +1181,10 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_961, quirk_sis_96x_
1135DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_962, quirk_sis_96x_smbus); 1181DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_962, quirk_sis_96x_smbus);
1136DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_963, quirk_sis_96x_smbus); 1182DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_963, quirk_sis_96x_smbus);
1137DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC, quirk_sis_96x_smbus); 1183DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC, quirk_sis_96x_smbus);
1138DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_961, quirk_sis_96x_smbus); 1184DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_961, quirk_sis_96x_smbus);
1139DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_962, quirk_sis_96x_smbus); 1185DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_962, quirk_sis_96x_smbus);
1140DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_963, quirk_sis_96x_smbus); 1186DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_963, quirk_sis_96x_smbus);
1141DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC, quirk_sis_96x_smbus); 1187DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC, quirk_sis_96x_smbus);
1142 1188
1143/* 1189/*
1144 * ... This is further complicated by the fact that some SiS96x south 1190 * ... This is further complicated by the fact that some SiS96x south
@@ -1172,7 +1218,7 @@ static void quirk_sis_503(struct pci_dev *dev)
1172 quirk_sis_96x_smbus(dev); 1218 quirk_sis_96x_smbus(dev);
1173} 1219}
1174DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503, quirk_sis_503); 1220DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503, quirk_sis_503);
1175DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503, quirk_sis_503); 1221DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503, quirk_sis_503);
1176 1222
1177 1223
1178/* 1224/*
@@ -1205,7 +1251,7 @@ static void asus_hides_ac97_lpc(struct pci_dev *dev)
1205 } 1251 }
1206} 1252}
1207DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237, asus_hides_ac97_lpc); 1253DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237, asus_hides_ac97_lpc);
1208DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237, asus_hides_ac97_lpc); 1254DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8237, asus_hides_ac97_lpc);
1209 1255
1210#if defined(CONFIG_ATA) || defined(CONFIG_ATA_MODULE) 1256#if defined(CONFIG_ATA) || defined(CONFIG_ATA_MODULE)
1211 1257
@@ -1270,12 +1316,12 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363, qui
1270DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365, quirk_jmicron_ata); 1316DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365, quirk_jmicron_ata);
1271DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366, quirk_jmicron_ata); 1317DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366, quirk_jmicron_ata);
1272DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368, quirk_jmicron_ata); 1318DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368, quirk_jmicron_ata);
1273DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB360, quirk_jmicron_ata); 1319DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB360, quirk_jmicron_ata);
1274DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361, quirk_jmicron_ata); 1320DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361, quirk_jmicron_ata);
1275DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363, quirk_jmicron_ata); 1321DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363, quirk_jmicron_ata);
1276DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365, quirk_jmicron_ata); 1322DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365, quirk_jmicron_ata);
1277DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366, quirk_jmicron_ata); 1323DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366, quirk_jmicron_ata);
1278DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368, quirk_jmicron_ata); 1324DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368, quirk_jmicron_ata);
1279 1325
1280#endif 1326#endif
1281 1327
@@ -1521,6 +1567,10 @@ extern struct pci_fixup __start_pci_fixups_enable[];
1521extern struct pci_fixup __end_pci_fixups_enable[]; 1567extern struct pci_fixup __end_pci_fixups_enable[];
1522extern struct pci_fixup __start_pci_fixups_resume[]; 1568extern struct pci_fixup __start_pci_fixups_resume[];
1523extern struct pci_fixup __end_pci_fixups_resume[]; 1569extern struct pci_fixup __end_pci_fixups_resume[];
1570extern struct pci_fixup __start_pci_fixups_resume_early[];
1571extern struct pci_fixup __end_pci_fixups_resume_early[];
1572extern struct pci_fixup __start_pci_fixups_suspend[];
1573extern struct pci_fixup __end_pci_fixups_suspend[];
1524 1574
1525 1575
1526void pci_fixup_device(enum pci_fixup_pass pass, struct pci_dev *dev) 1576void pci_fixup_device(enum pci_fixup_pass pass, struct pci_dev *dev)
@@ -1553,6 +1603,16 @@ void pci_fixup_device(enum pci_fixup_pass pass, struct pci_dev *dev)
1553 end = __end_pci_fixups_resume; 1603 end = __end_pci_fixups_resume;
1554 break; 1604 break;
1555 1605
1606 case pci_fixup_resume_early:
1607 start = __start_pci_fixups_resume_early;
1608 end = __end_pci_fixups_resume_early;
1609 break;
1610
1611 case pci_fixup_suspend:
1612 start = __start_pci_fixups_suspend;
1613 end = __end_pci_fixups_suspend;
1614 break;
1615
1556 default: 1616 default:
1557 /* stupid compiler warning, you would think with an enum... */ 1617 /* stupid compiler warning, you would think with an enum... */
1558 return; 1618 return;
@@ -1629,7 +1689,7 @@ static void quirk_nvidia_ck804_pcie_aer_ext_cap(struct pci_dev *dev)
1629} 1689}
1630DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_CK804_PCIE, 1690DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_CK804_PCIE,
1631 quirk_nvidia_ck804_pcie_aer_ext_cap); 1691 quirk_nvidia_ck804_pcie_aer_ext_cap);
1632DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_CK804_PCIE, 1692DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_CK804_PCIE,
1633 quirk_nvidia_ck804_pcie_aer_ext_cap); 1693 quirk_nvidia_ck804_pcie_aer_ext_cap);
1634 1694
1635static void __devinit quirk_via_cx700_pci_parking_caching(struct pci_dev *dev) 1695static void __devinit quirk_via_cx700_pci_parking_caching(struct pci_dev *dev)
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 8ddb918f5f57..827c0a520e2b 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -27,13 +27,6 @@
27#include <linux/slab.h> 27#include <linux/slab.h>
28 28
29 29
30#define DEBUG_CONFIG 1
31#if DEBUG_CONFIG
32#define DBG(x...) printk(x)
33#else
34#define DBG(x...)
35#endif
36
37static void pbus_assign_resources_sorted(struct pci_bus *bus) 30static void pbus_assign_resources_sorted(struct pci_bus *bus)
38{ 31{
39 struct pci_dev *dev; 32 struct pci_dev *dev;
@@ -81,8 +74,8 @@ void pci_setup_cardbus(struct pci_bus *bus)
81 struct pci_dev *bridge = bus->self; 74 struct pci_dev *bridge = bus->self;
82 struct pci_bus_region region; 75 struct pci_bus_region region;
83 76
84 printk("PCI: Bus %d, cardbus bridge: %s\n", 77 dev_info(&bridge->dev, "CardBus bridge, secondary bus %04x:%02x\n",
85 bus->number, pci_name(bridge)); 78 pci_domain_nr(bus), bus->number);
86 79
87 pcibios_resource_to_bus(bridge, &region, bus->resource[0]); 80 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
88 if (bus->resource[0]->flags & IORESOURCE_IO) { 81 if (bus->resource[0]->flags & IORESOURCE_IO) {
@@ -90,7 +83,7 @@ void pci_setup_cardbus(struct pci_bus *bus)
90 * The IO resource is allocated a range twice as large as it 83 * The IO resource is allocated a range twice as large as it
91 * would normally need. This allows us to set both IO regs. 84 * would normally need. This allows us to set both IO regs.
92 */ 85 */
93 printk(KERN_INFO " IO window: 0x%08lx-0x%08lx\n", 86 dev_info(&bridge->dev, " IO window: %#08lx-%#08lx\n",
94 (unsigned long)region.start, 87 (unsigned long)region.start,
95 (unsigned long)region.end); 88 (unsigned long)region.end);
96 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0, 89 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
@@ -101,7 +94,7 @@ void pci_setup_cardbus(struct pci_bus *bus)
101 94
102 pcibios_resource_to_bus(bridge, &region, bus->resource[1]); 95 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
103 if (bus->resource[1]->flags & IORESOURCE_IO) { 96 if (bus->resource[1]->flags & IORESOURCE_IO) {
104 printk(KERN_INFO " IO window: 0x%08lx-0x%08lx\n", 97 dev_info(&bridge->dev, " IO window: %#08lx-%#08lx\n",
105 (unsigned long)region.start, 98 (unsigned long)region.start,
106 (unsigned long)region.end); 99 (unsigned long)region.end);
107 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1, 100 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
@@ -112,7 +105,7 @@ void pci_setup_cardbus(struct pci_bus *bus)
112 105
113 pcibios_resource_to_bus(bridge, &region, bus->resource[2]); 106 pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
114 if (bus->resource[2]->flags & IORESOURCE_MEM) { 107 if (bus->resource[2]->flags & IORESOURCE_MEM) {
115 printk(KERN_INFO " PREFETCH window: 0x%08lx-0x%08lx\n", 108 dev_info(&bridge->dev, " PREFETCH window: %#08lx-%#08lx\n",
116 (unsigned long)region.start, 109 (unsigned long)region.start,
117 (unsigned long)region.end); 110 (unsigned long)region.end);
118 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0, 111 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
@@ -123,7 +116,7 @@ void pci_setup_cardbus(struct pci_bus *bus)
123 116
124 pcibios_resource_to_bus(bridge, &region, bus->resource[3]); 117 pcibios_resource_to_bus(bridge, &region, bus->resource[3]);
125 if (bus->resource[3]->flags & IORESOURCE_MEM) { 118 if (bus->resource[3]->flags & IORESOURCE_MEM) {
126 printk(KERN_INFO " MEM window: 0x%08lx-0x%08lx\n", 119 dev_info(&bridge->dev, " MEM window: %#08lx-%#08lx\n",
127 (unsigned long)region.start, 120 (unsigned long)region.start,
128 (unsigned long)region.end); 121 (unsigned long)region.end);
129 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1, 122 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
@@ -151,7 +144,8 @@ static void pci_setup_bridge(struct pci_bus *bus)
151 struct pci_bus_region region; 144 struct pci_bus_region region;
152 u32 l, bu, lu, io_upper16; 145 u32 l, bu, lu, io_upper16;
153 146
154 DBG(KERN_INFO "PCI: Bridge: %s\n", pci_name(bridge)); 147 dev_info(&bridge->dev, "PCI bridge, secondary bus %04x:%02x\n",
148 pci_domain_nr(bus), bus->number);
155 149
156 /* Set up the top and bottom of the PCI I/O segment for this bus. */ 150 /* Set up the top and bottom of the PCI I/O segment for this bus. */
157 pcibios_resource_to_bus(bridge, &region, bus->resource[0]); 151 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
@@ -162,7 +156,7 @@ static void pci_setup_bridge(struct pci_bus *bus)
162 l |= region.end & 0xf000; 156 l |= region.end & 0xf000;
163 /* Set up upper 16 bits of I/O base/limit. */ 157 /* Set up upper 16 bits of I/O base/limit. */
164 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16); 158 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
165 DBG(KERN_INFO " IO window: %04lx-%04lx\n", 159 dev_info(&bridge->dev, " IO window: %#04lx-%#04lx\n",
166 (unsigned long)region.start, 160 (unsigned long)region.start,
167 (unsigned long)region.end); 161 (unsigned long)region.end);
168 } 162 }
@@ -170,7 +164,7 @@ static void pci_setup_bridge(struct pci_bus *bus)
170 /* Clear upper 16 bits of I/O base/limit. */ 164 /* Clear upper 16 bits of I/O base/limit. */
171 io_upper16 = 0; 165 io_upper16 = 0;
172 l = 0x00f0; 166 l = 0x00f0;
173 DBG(KERN_INFO " IO window: disabled.\n"); 167 dev_info(&bridge->dev, " IO window: disabled\n");
174 } 168 }
175 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */ 169 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
176 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff); 170 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
@@ -185,13 +179,13 @@ static void pci_setup_bridge(struct pci_bus *bus)
185 if (bus->resource[1]->flags & IORESOURCE_MEM) { 179 if (bus->resource[1]->flags & IORESOURCE_MEM) {
186 l = (region.start >> 16) & 0xfff0; 180 l = (region.start >> 16) & 0xfff0;
187 l |= region.end & 0xfff00000; 181 l |= region.end & 0xfff00000;
188 DBG(KERN_INFO " MEM window: 0x%08lx-0x%08lx\n", 182 dev_info(&bridge->dev, " MEM window: %#08lx-%#08lx\n",
189 (unsigned long)region.start, 183 (unsigned long)region.start,
190 (unsigned long)region.end); 184 (unsigned long)region.end);
191 } 185 }
192 else { 186 else {
193 l = 0x0000fff0; 187 l = 0x0000fff0;
194 DBG(KERN_INFO " MEM window: disabled.\n"); 188 dev_info(&bridge->dev, " MEM window: disabled\n");
195 } 189 }
196 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l); 190 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
197 191
@@ -208,13 +202,13 @@ static void pci_setup_bridge(struct pci_bus *bus)
208 l |= region.end & 0xfff00000; 202 l |= region.end & 0xfff00000;
209 bu = upper_32_bits(region.start); 203 bu = upper_32_bits(region.start);
210 lu = upper_32_bits(region.end); 204 lu = upper_32_bits(region.end);
211 DBG(KERN_INFO " PREFETCH window: 0x%016llx-0x%016llx\n", 205 dev_info(&bridge->dev, " PREFETCH window: %#016llx-%#016llx\n",
212 (unsigned long long)region.start, 206 (unsigned long long)region.start,
213 (unsigned long long)region.end); 207 (unsigned long long)region.end);
214 } 208 }
215 else { 209 else {
216 l = 0x0000fff0; 210 l = 0x0000fff0;
217 DBG(KERN_INFO " PREFETCH window: disabled.\n"); 211 dev_info(&bridge->dev, " PREFETCH window: disabled\n");
218 } 212 }
219 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l); 213 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
220 214
@@ -361,9 +355,8 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long
361 align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start; 355 align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start;
362 order = __ffs(align) - 20; 356 order = __ffs(align) - 20;
363 if (order > 11) { 357 if (order > 11) {
364 printk(KERN_WARNING "PCI: region %s/%d " 358 dev_warn(&dev->dev, "BAR %d too large: "
365 "too large: 0x%016llx-0x%016llx\n", 359 "%#016llx-%#016llx\n", i,
366 pci_name(dev), i,
367 (unsigned long long)r->start, 360 (unsigned long long)r->start,
368 (unsigned long long)r->end); 361 (unsigned long long)r->end);
369 r->flags = 0; 362 r->flags = 0;
@@ -529,8 +522,8 @@ void __ref pci_bus_assign_resources(struct pci_bus *bus)
529 break; 522 break;
530 523
531 default: 524 default:
532 printk(KERN_INFO "PCI: not setting up bridge %s " 525 dev_info(&dev->dev, "not setting up bridge for bus "
533 "for bus %d\n", pci_name(dev), b->number); 526 "%04x:%02x\n", pci_domain_nr(b), b->number);
534 break; 527 break;
535 } 528 }
536 } 529 }
diff --git a/drivers/pci/setup-irq.c b/drivers/pci/setup-irq.c
index 05ca2ed9eb51..aa795fd428de 100644
--- a/drivers/pci/setup-irq.c
+++ b/drivers/pci/setup-irq.c
@@ -47,8 +47,7 @@ pdev_fixup_irq(struct pci_dev *dev,
47 } 47 }
48 dev->irq = irq; 48 dev->irq = irq;
49 49
50 pr_debug("PCI: fixup irq: (%s) got %d\n", 50 dev_dbg(&dev->dev, "fixup irq: got %d\n", dev->irq);
51 kobject_name(&dev->dev.kobj), dev->irq);
52 51
53 /* Always tell the device, so the driver knows what is 52 /* Always tell the device, so the driver knows what is
54 the real IRQ to use; the device does not use it. */ 53 the real IRQ to use; the device does not use it. */
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index 7d35cdf4579f..1a5fc83c71b3 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -26,8 +26,7 @@
26#include "pci.h" 26#include "pci.h"
27 27
28 28
29void 29void pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
30pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
31{ 30{
32 struct pci_bus_region region; 31 struct pci_bus_region region;
33 u32 new, check, mask; 32 u32 new, check, mask;
@@ -43,20 +42,20 @@ pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
43 /* 42 /*
44 * Ignore non-moveable resources. This might be legacy resources for 43 * Ignore non-moveable resources. This might be legacy resources for
45 * which no functional BAR register exists or another important 44 * which no functional BAR register exists or another important
46 * system resource we should better not move around in system address 45 * system resource we shouldn't move around.
47 * space.
48 */ 46 */
49 if (res->flags & IORESOURCE_PCI_FIXED) 47 if (res->flags & IORESOURCE_PCI_FIXED)
50 return; 48 return;
51 49
52 pcibios_resource_to_bus(dev, &region, res); 50 pcibios_resource_to_bus(dev, &region, res);
53 51
54 pr_debug(" got res [%llx:%llx] bus [%llx:%llx] flags %lx for " 52 dev_dbg(&dev->dev, "BAR %d: got res [%#llx-%#llx] bus [%#llx-%#llx] "
55 "BAR %d of %s\n", (unsigned long long)res->start, 53 "flags %#lx\n", resno,
54 (unsigned long long)res->start,
56 (unsigned long long)res->end, 55 (unsigned long long)res->end,
57 (unsigned long long)region.start, 56 (unsigned long long)region.start,
58 (unsigned long long)region.end, 57 (unsigned long long)region.end,
59 (unsigned long)res->flags, resno, pci_name(dev)); 58 (unsigned long)res->flags);
60 59
61 new = region.start | (res->flags & PCI_REGION_FLAG_MASK); 60 new = region.start | (res->flags & PCI_REGION_FLAG_MASK);
62 if (res->flags & IORESOURCE_IO) 61 if (res->flags & IORESOURCE_IO)
@@ -81,9 +80,8 @@ pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
81 pci_read_config_dword(dev, reg, &check); 80 pci_read_config_dword(dev, reg, &check);
82 81
83 if ((new ^ check) & mask) { 82 if ((new ^ check) & mask) {
84 printk(KERN_ERR "PCI: Error while updating region " 83 dev_err(&dev->dev, "BAR %d: error updating (%#08x != %#08x)\n",
85 "%s/%d (%08x != %08x)\n", pci_name(dev), resno, 84 resno, new, check);
86 new, check);
87 } 85 }
88 86
89 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) == 87 if ((new & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
@@ -92,15 +90,14 @@ pci_update_resource(struct pci_dev *dev, struct resource *res, int resno)
92 pci_write_config_dword(dev, reg + 4, new); 90 pci_write_config_dword(dev, reg + 4, new);
93 pci_read_config_dword(dev, reg + 4, &check); 91 pci_read_config_dword(dev, reg + 4, &check);
94 if (check != new) { 92 if (check != new) {
95 printk(KERN_ERR "PCI: Error updating region " 93 dev_err(&dev->dev, "BAR %d: error updating "
96 "%s/%d (high %08x != %08x)\n", 94 "(high %#08x != %#08x)\n", resno, new, check);
97 pci_name(dev), resno, new, check);
98 } 95 }
99 } 96 }
100 res->flags &= ~IORESOURCE_UNSET; 97 res->flags &= ~IORESOURCE_UNSET;
101 pr_debug("PCI: moved device %s resource %d (%lx) to %x\n", 98 dev_dbg(&dev->dev, "BAR %d: moved to bus [%#llx-%#llx] flags %#lx\n",
102 pci_name(dev), resno, res->flags, 99 resno, (unsigned long long)region.start,
103 new & ~PCI_REGION_FLAG_MASK); 100 (unsigned long long)region.end, res->flags);
104} 101}
105 102
106int pci_claim_resource(struct pci_dev *dev, int resource) 103int pci_claim_resource(struct pci_dev *dev, int resource)
@@ -117,10 +114,11 @@ int pci_claim_resource(struct pci_dev *dev, int resource)
117 err = insert_resource(root, res); 114 err = insert_resource(root, res);
118 115
119 if (err) { 116 if (err) {
120 printk(KERN_ERR "PCI: %s region %d of %s %s [%llx:%llx]\n", 117 dev_err(&dev->dev, "BAR %d: %s of %s [%#llx-%#llx]\n",
121 root ? "Address space collision on" : 118 resource,
122 "No parent found for", 119 root ? "address space collision on" :
123 resource, dtype, pci_name(dev), 120 "no parent found for",
121 dtype,
124 (unsigned long long)res->start, 122 (unsigned long long)res->start,
125 (unsigned long long)res->end); 123 (unsigned long long)res->end);
126 } 124 }
@@ -140,11 +138,10 @@ int pci_assign_resource(struct pci_dev *dev, int resno)
140 138
141 align = resource_alignment(res); 139 align = resource_alignment(res);
142 if (!align) { 140 if (!align) {
143 printk(KERN_ERR "PCI: Cannot allocate resource (bogus " 141 dev_err(&dev->dev, "BAR %d: can't allocate resource (bogus "
144 "alignment) %d [%llx:%llx] (flags %lx) of %s\n", 142 "alignment) [%#llx-%#llx] flags %#lx\n",
145 resno, (unsigned long long)res->start, 143 resno, (unsigned long long)res->start,
146 (unsigned long long)res->end, res->flags, 144 (unsigned long long)res->end, res->flags);
147 pci_name(dev));
148 return -EINVAL; 145 return -EINVAL;
149 } 146 }
150 147
@@ -165,11 +162,11 @@ int pci_assign_resource(struct pci_dev *dev, int resno)
165 } 162 }
166 163
167 if (ret) { 164 if (ret) {
168 printk(KERN_ERR "PCI: Failed to allocate %s resource " 165 dev_err(&dev->dev, "BAR %d: can't allocate %s resource "
169 "#%d:%llx@%llx for %s\n", 166 "[%#llx-%#llx]\n", resno,
170 res->flags & IORESOURCE_IO ? "I/O" : "mem", 167 res->flags & IORESOURCE_IO ? "I/O" : "mem",
171 resno, (unsigned long long)size, 168 (unsigned long long)res->start,
172 (unsigned long long)res->start, pci_name(dev)); 169 (unsigned long long)res->end);
173 } else { 170 } else {
174 res->flags &= ~IORESOURCE_STARTALIGN; 171 res->flags &= ~IORESOURCE_STARTALIGN;
175 if (resno < PCI_BRIDGE_RESOURCES) 172 if (resno < PCI_BRIDGE_RESOURCES)
@@ -205,11 +202,11 @@ int pci_assign_resource_fixed(struct pci_dev *dev, int resno)
205 } 202 }
206 203
207 if (ret) { 204 if (ret) {
208 printk(KERN_ERR "PCI: Failed to allocate %s resource " 205 dev_err(&dev->dev, "BAR %d: can't allocate %s resource "
209 "#%d:%llx@%llx for %s\n", 206 "[%#llx-%#llx\n]", resno,
210 res->flags & IORESOURCE_IO ? "I/O" : "mem", 207 res->flags & IORESOURCE_IO ? "I/O" : "mem",
211 resno, (unsigned long long)(res->end - res->start + 1), 208 (unsigned long long)res->start,
212 (unsigned long long)res->start, pci_name(dev)); 209 (unsigned long long)res->end);
213 } else if (resno < PCI_BRIDGE_RESOURCES) { 210 } else if (resno < PCI_BRIDGE_RESOURCES) {
214 pci_update_resource(dev, res, resno); 211 pci_update_resource(dev, res, resno);
215 } 212 }
@@ -239,11 +236,10 @@ void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
239 236
240 r_align = resource_alignment(r); 237 r_align = resource_alignment(r);
241 if (!r_align) { 238 if (!r_align) {
242 printk(KERN_WARNING "PCI: bogus alignment of resource " 239 dev_warn(&dev->dev, "BAR %d: bogus alignment "
243 "%d [%llx:%llx] (flags %lx) of %s\n", 240 "[%#llx-%#llx] flags %#lx\n",
244 i, (unsigned long long)r->start, 241 i, (unsigned long long)r->start,
245 (unsigned long long)r->end, r->flags, 242 (unsigned long long)r->end, r->flags);
246 pci_name(dev));
247 continue; 243 continue;
248 } 244 }
249 for (list = head; ; list = list->next) { 245 for (list = head; ; list = list->next) {
@@ -291,7 +287,7 @@ int pci_enable_resources(struct pci_dev *dev, int mask)
291 287
292 if (!r->parent) { 288 if (!r->parent) {
293 dev_err(&dev->dev, "device not available because of " 289 dev_err(&dev->dev, "device not available because of "
294 "BAR %d [%llx:%llx] collisions\n", i, 290 "BAR %d [%#llx-%#llx] collisions\n", i,
295 (unsigned long long) r->start, 291 (unsigned long long) r->start,
296 (unsigned long long) r->end); 292 (unsigned long long) r->end);
297 return -EINVAL; 293 return -EINVAL;
diff --git a/drivers/pci/slot.c b/drivers/pci/slot.c
new file mode 100644
index 000000000000..7e5b85cbd948
--- /dev/null
+++ b/drivers/pci/slot.c
@@ -0,0 +1,233 @@
1/*
2 * drivers/pci/slot.c
3 * Copyright (C) 2006 Matthew Wilcox <matthew@wil.cx>
4 * Copyright (C) 2006-2008 Hewlett-Packard Development Company, L.P.
5 * Alex Chiang <achiang@hp.com>
6 */
7
8#include <linux/kobject.h>
9#include <linux/pci.h>
10#include <linux/err.h>
11#include "pci.h"
12
13struct kset *pci_slots_kset;
14EXPORT_SYMBOL_GPL(pci_slots_kset);
15
16static ssize_t pci_slot_attr_show(struct kobject *kobj,
17 struct attribute *attr, char *buf)
18{
19 struct pci_slot *slot = to_pci_slot(kobj);
20 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
21 return attribute->show ? attribute->show(slot, buf) : -EIO;
22}
23
24static ssize_t pci_slot_attr_store(struct kobject *kobj,
25 struct attribute *attr, const char *buf, size_t len)
26{
27 struct pci_slot *slot = to_pci_slot(kobj);
28 struct pci_slot_attribute *attribute = to_pci_slot_attr(attr);
29 return attribute->store ? attribute->store(slot, buf, len) : -EIO;
30}
31
32static struct sysfs_ops pci_slot_sysfs_ops = {
33 .show = pci_slot_attr_show,
34 .store = pci_slot_attr_store,
35};
36
37static ssize_t address_read_file(struct pci_slot *slot, char *buf)
38{
39 if (slot->number == 0xff)
40 return sprintf(buf, "%04x:%02x\n",
41 pci_domain_nr(slot->bus),
42 slot->bus->number);
43 else
44 return sprintf(buf, "%04x:%02x:%02x\n",
45 pci_domain_nr(slot->bus),
46 slot->bus->number,
47 slot->number);
48}
49
50static void pci_slot_release(struct kobject *kobj)
51{
52 struct pci_slot *slot = to_pci_slot(kobj);
53
54 pr_debug("%s: releasing pci_slot on %x:%d\n", __func__,
55 slot->bus->number, slot->number);
56
57 list_del(&slot->list);
58
59 kfree(slot);
60}
61
62static struct pci_slot_attribute pci_slot_attr_address =
63 __ATTR(address, (S_IFREG | S_IRUGO), address_read_file, NULL);
64
65static struct attribute *pci_slot_default_attrs[] = {
66 &pci_slot_attr_address.attr,
67 NULL,
68};
69
70static struct kobj_type pci_slot_ktype = {
71 .sysfs_ops = &pci_slot_sysfs_ops,
72 .release = &pci_slot_release,
73 .default_attrs = pci_slot_default_attrs,
74};
75
76/**
77 * pci_create_slot - create or increment refcount for physical PCI slot
78 * @parent: struct pci_bus of parent bridge
79 * @slot_nr: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
80 * @name: user visible string presented in /sys/bus/pci/slots/<name>
81 *
82 * PCI slots have first class attributes such as address, speed, width,
83 * and a &struct pci_slot is used to manage them. This interface will
84 * either return a new &struct pci_slot to the caller, or if the pci_slot
85 * already exists, its refcount will be incremented.
86 *
87 * Slots are uniquely identified by a @pci_bus, @slot_nr, @name tuple.
88 *
89 * Placeholder slots:
90 * In most cases, @pci_bus, @slot_nr will be sufficient to uniquely identify
91 * a slot. There is one notable exception - pSeries (rpaphp), where the
92 * @slot_nr cannot be determined until a device is actually inserted into
93 * the slot. In this scenario, the caller may pass -1 for @slot_nr.
94 *
95 * The following semantics are imposed when the caller passes @slot_nr ==
96 * -1. First, the check for existing %struct pci_slot is skipped, as the
97 * caller may know about several unpopulated slots on a given %struct
98 * pci_bus, and each slot would have a @slot_nr of -1. Uniqueness for
99 * these slots is then determined by the @name parameter. We expect
100 * kobject_init_and_add() to warn us if the caller attempts to create
101 * multiple slots with the same name. The other change in semantics is
102 * user-visible, which is the 'address' parameter presented in sysfs will
103 * consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
104 * %struct pci_bus and bb is the bus number. In other words, the devfn of
105 * the 'placeholder' slot will not be displayed.
106 */
107
108struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
109 const char *name)
110{
111 struct pci_slot *slot;
112 int err;
113
114 down_write(&pci_bus_sem);
115
116 if (slot_nr == -1)
117 goto placeholder;
118
119 /* If we've already created this slot, bump refcount and return. */
120 list_for_each_entry(slot, &parent->slots, list) {
121 if (slot->number == slot_nr) {
122 kobject_get(&slot->kobj);
123 pr_debug("%s: inc refcount to %d on %04x:%02x:%02x\n",
124 __func__,
125 atomic_read(&slot->kobj.kref.refcount),
126 pci_domain_nr(parent), parent->number,
127 slot_nr);
128 goto out;
129 }
130 }
131
132placeholder:
133 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
134 if (!slot) {
135 slot = ERR_PTR(-ENOMEM);
136 goto out;
137 }
138
139 slot->bus = parent;
140 slot->number = slot_nr;
141
142 slot->kobj.kset = pci_slots_kset;
143 err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL,
144 "%s", name);
145 if (err) {
146 printk(KERN_ERR "Unable to register kobject %s\n", name);
147 goto err;
148 }
149
150 INIT_LIST_HEAD(&slot->list);
151 list_add(&slot->list, &parent->slots);
152
153 /* Don't care if debug printk has a -1 for slot_nr */
154 pr_debug("%s: created pci_slot on %04x:%02x:%02x\n",
155 __func__, pci_domain_nr(parent), parent->number, slot_nr);
156
157 out:
158 up_write(&pci_bus_sem);
159 return slot;
160 err:
161 kfree(slot);
162 slot = ERR_PTR(err);
163 goto out;
164}
165EXPORT_SYMBOL_GPL(pci_create_slot);
166
167/**
168 * pci_update_slot_number - update %struct pci_slot -> number
169 * @slot - %struct pci_slot to update
170 * @slot_nr - new number for slot
171 *
172 * The primary purpose of this interface is to allow callers who earlier
173 * created a placeholder slot in pci_create_slot() by passing a -1 as
174 * slot_nr, to update their %struct pci_slot with the correct @slot_nr.
175 */
176
177void pci_update_slot_number(struct pci_slot *slot, int slot_nr)
178{
179 int name_count = 0;
180 struct pci_slot *tmp;
181
182 down_write(&pci_bus_sem);
183
184 list_for_each_entry(tmp, &slot->bus->slots, list) {
185 WARN_ON(tmp->number == slot_nr);
186 if (!strcmp(kobject_name(&tmp->kobj), kobject_name(&slot->kobj)))
187 name_count++;
188 }
189
190 if (name_count > 1)
191 printk(KERN_WARNING "pci_update_slot_number found %d slots with the same name: %s\n", name_count, kobject_name(&slot->kobj));
192
193 slot->number = slot_nr;
194 up_write(&pci_bus_sem);
195}
196EXPORT_SYMBOL_GPL(pci_update_slot_number);
197
198/**
199 * pci_destroy_slot - decrement refcount for physical PCI slot
200 * @slot: struct pci_slot to decrement
201 *
202 * %struct pci_slot is refcounted, so destroying them is really easy; we
203 * just call kobject_put on its kobj and let our release methods do the
204 * rest.
205 */
206
207void pci_destroy_slot(struct pci_slot *slot)
208{
209 pr_debug("%s: dec refcount to %d on %04x:%02x:%02x\n", __func__,
210 atomic_read(&slot->kobj.kref.refcount) - 1,
211 pci_domain_nr(slot->bus), slot->bus->number, slot->number);
212
213 down_write(&pci_bus_sem);
214 kobject_put(&slot->kobj);
215 up_write(&pci_bus_sem);
216}
217EXPORT_SYMBOL_GPL(pci_destroy_slot);
218
219static int pci_slot_init(void)
220{
221 struct kset *pci_bus_kset;
222
223 pci_bus_kset = bus_get_kset(&pci_bus_type);
224 pci_slots_kset = kset_create_and_add("slots", NULL,
225 &pci_bus_kset->kobj);
226 if (!pci_slots_kset) {
227 printk(KERN_ERR "PCI: Slot initialization failure\n");
228 return -ENOMEM;
229 }
230 return 0;
231}
232
233subsys_initcall(pci_slot_init);
diff --git a/drivers/pnp/base.h b/drivers/pnp/base.h
index 886dac823ed6..e3fa9a2d9a3d 100644
--- a/drivers/pnp/base.h
+++ b/drivers/pnp/base.h
@@ -1,3 +1,8 @@
1/*
2 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
3 * Bjorn Helgaas <bjorn.helgaas@hp.com>
4 */
5
1extern spinlock_t pnp_lock; 6extern spinlock_t pnp_lock;
2void *pnp_alloc(long size); 7void *pnp_alloc(long size);
3 8
@@ -19,22 +24,118 @@ void pnp_remove_card(struct pnp_card *card);
19int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev); 24int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev);
20void pnp_remove_card_device(struct pnp_dev *dev); 25void pnp_remove_card_device(struct pnp_dev *dev);
21 26
22struct pnp_option *pnp_build_option(int priority); 27struct pnp_port {
23struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev); 28 resource_size_t min; /* min base number */
24struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev, 29 resource_size_t max; /* max base number */
25 int priority); 30 resource_size_t align; /* align boundary */
26int pnp_register_irq_resource(struct pnp_dev *dev, struct pnp_option *option, 31 resource_size_t size; /* size of range */
27 struct pnp_irq *data); 32 unsigned char flags; /* port flags */
28int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option, 33};
29 struct pnp_dma *data); 34
30int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option, 35#define PNP_IRQ_NR 256
31 struct pnp_port *data); 36typedef struct { DECLARE_BITMAP(bits, PNP_IRQ_NR); } pnp_irq_mask_t;
32int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option, 37
33 struct pnp_mem *data); 38struct pnp_irq {
39 pnp_irq_mask_t map; /* bitmap for IRQ lines */
40 unsigned char flags; /* IRQ flags */
41};
42
43struct pnp_dma {
44 unsigned char map; /* bitmask for DMA channels */
45 unsigned char flags; /* DMA flags */
46};
47
48struct pnp_mem {
49 resource_size_t min; /* min base number */
50 resource_size_t max; /* max base number */
51 resource_size_t align; /* align boundary */
52 resource_size_t size; /* size of range */
53 unsigned char flags; /* memory flags */
54};
55
56#define PNP_OPTION_DEPENDENT 0x80000000
57#define PNP_OPTION_SET_MASK 0xffff
58#define PNP_OPTION_SET_SHIFT 12
59#define PNP_OPTION_PRIORITY_MASK 0xfff
60#define PNP_OPTION_PRIORITY_SHIFT 0
61
62#define PNP_RES_PRIORITY_PREFERRED 0
63#define PNP_RES_PRIORITY_ACCEPTABLE 1
64#define PNP_RES_PRIORITY_FUNCTIONAL 2
65#define PNP_RES_PRIORITY_INVALID PNP_OPTION_PRIORITY_MASK
66
67struct pnp_option {
68 struct list_head list;
69 unsigned int flags; /* independent/dependent, set, priority */
70
71 unsigned long type; /* IORESOURCE_{IO,MEM,IRQ,DMA} */
72 union {
73 struct pnp_port port;
74 struct pnp_irq irq;
75 struct pnp_dma dma;
76 struct pnp_mem mem;
77 } u;
78};
79
80int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
81 pnp_irq_mask_t *map, unsigned char flags);
82int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
83 unsigned char map, unsigned char flags);
84int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
85 resource_size_t min, resource_size_t max,
86 resource_size_t align, resource_size_t size,
87 unsigned char flags);
88int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
89 resource_size_t min, resource_size_t max,
90 resource_size_t align, resource_size_t size,
91 unsigned char flags);
92
93static inline int pnp_option_is_dependent(struct pnp_option *option)
94{
95 return option->flags & PNP_OPTION_DEPENDENT ? 1 : 0;
96}
97
98static inline unsigned int pnp_option_set(struct pnp_option *option)
99{
100 return (option->flags >> PNP_OPTION_SET_SHIFT) & PNP_OPTION_SET_MASK;
101}
102
103static inline unsigned int pnp_option_priority(struct pnp_option *option)
104{
105 return (option->flags >> PNP_OPTION_PRIORITY_SHIFT) &
106 PNP_OPTION_PRIORITY_MASK;
107}
108
109static inline unsigned int pnp_new_dependent_set(struct pnp_dev *dev,
110 int priority)
111{
112 unsigned int flags;
113
114 if (priority > PNP_RES_PRIORITY_FUNCTIONAL) {
115 dev_warn(&dev->dev, "invalid dependent option priority %d "
116 "clipped to %d", priority,
117 PNP_RES_PRIORITY_INVALID);
118 priority = PNP_RES_PRIORITY_INVALID;
119 }
120
121 flags = PNP_OPTION_DEPENDENT |
122 ((dev->num_dependent_sets & PNP_OPTION_SET_MASK) <<
123 PNP_OPTION_SET_SHIFT) |
124 ((priority & PNP_OPTION_PRIORITY_MASK) <<
125 PNP_OPTION_PRIORITY_SHIFT);
126
127 dev->num_dependent_sets++;
128
129 return flags;
130}
131
132char *pnp_option_priority_name(struct pnp_option *option);
133void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option);
134
34void pnp_init_resources(struct pnp_dev *dev); 135void pnp_init_resources(struct pnp_dev *dev);
35 136
36void pnp_fixup_device(struct pnp_dev *dev); 137void pnp_fixup_device(struct pnp_dev *dev);
37void pnp_free_option(struct pnp_option *option); 138void pnp_free_options(struct pnp_dev *dev);
38int __pnp_add_device(struct pnp_dev *dev); 139int __pnp_add_device(struct pnp_dev *dev);
39void __pnp_remove_device(struct pnp_dev *dev); 140void __pnp_remove_device(struct pnp_dev *dev);
40 141
@@ -43,29 +144,18 @@ int pnp_check_mem(struct pnp_dev *dev, struct resource *res);
43int pnp_check_irq(struct pnp_dev *dev, struct resource *res); 144int pnp_check_irq(struct pnp_dev *dev, struct resource *res);
44int pnp_check_dma(struct pnp_dev *dev, struct resource *res); 145int pnp_check_dma(struct pnp_dev *dev, struct resource *res);
45 146
147char *pnp_resource_type_name(struct resource *res);
46void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc); 148void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc);
47 149
48void pnp_init_resource(struct resource *res); 150void pnp_free_resources(struct pnp_dev *dev);
49 151int pnp_resource_type(struct resource *res);
50struct pnp_resource *pnp_get_pnp_resource(struct pnp_dev *dev,
51 unsigned int type, unsigned int num);
52
53#define PNP_MAX_PORT 40
54#define PNP_MAX_MEM 24
55#define PNP_MAX_IRQ 2
56#define PNP_MAX_DMA 2
57 152
58struct pnp_resource { 153struct pnp_resource {
154 struct list_head list;
59 struct resource res; 155 struct resource res;
60 unsigned int index; /* ISAPNP config register index */
61}; 156};
62 157
63struct pnp_resource_table { 158void pnp_free_resource(struct pnp_resource *pnp_res);
64 struct pnp_resource port[PNP_MAX_PORT];
65 struct pnp_resource mem[PNP_MAX_MEM];
66 struct pnp_resource dma[PNP_MAX_DMA];
67 struct pnp_resource irq[PNP_MAX_IRQ];
68};
69 159
70struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq, 160struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
71 int flags); 161 int flags);
diff --git a/drivers/pnp/core.c b/drivers/pnp/core.c
index 20771b7d4482..a411582bcd72 100644
--- a/drivers/pnp/core.c
+++ b/drivers/pnp/core.c
@@ -99,14 +99,28 @@ static void pnp_free_ids(struct pnp_dev *dev)
99 } 99 }
100} 100}
101 101
102void pnp_free_resource(struct pnp_resource *pnp_res)
103{
104 list_del(&pnp_res->list);
105 kfree(pnp_res);
106}
107
108void pnp_free_resources(struct pnp_dev *dev)
109{
110 struct pnp_resource *pnp_res, *tmp;
111
112 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
113 pnp_free_resource(pnp_res);
114 }
115}
116
102static void pnp_release_device(struct device *dmdev) 117static void pnp_release_device(struct device *dmdev)
103{ 118{
104 struct pnp_dev *dev = to_pnp_dev(dmdev); 119 struct pnp_dev *dev = to_pnp_dev(dmdev);
105 120
106 pnp_free_option(dev->independent);
107 pnp_free_option(dev->dependent);
108 pnp_free_ids(dev); 121 pnp_free_ids(dev);
109 kfree(dev->res); 122 pnp_free_resources(dev);
123 pnp_free_options(dev);
110 kfree(dev); 124 kfree(dev);
111} 125}
112 126
@@ -119,12 +133,8 @@ struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id, char *pnpid
119 if (!dev) 133 if (!dev)
120 return NULL; 134 return NULL;
121 135
122 dev->res = kzalloc(sizeof(struct pnp_resource_table), GFP_KERNEL); 136 INIT_LIST_HEAD(&dev->resources);
123 if (!dev->res) { 137 INIT_LIST_HEAD(&dev->options);
124 kfree(dev);
125 return NULL;
126 }
127
128 dev->protocol = protocol; 138 dev->protocol = protocol;
129 dev->number = id; 139 dev->number = id;
130 dev->dma_mask = DMA_24BIT_MASK; 140 dev->dma_mask = DMA_24BIT_MASK;
@@ -140,7 +150,6 @@ struct pnp_dev *pnp_alloc_dev(struct pnp_protocol *protocol, int id, char *pnpid
140 150
141 dev_id = pnp_add_id(dev, pnpid); 151 dev_id = pnp_add_id(dev, pnpid);
142 if (!dev_id) { 152 if (!dev_id) {
143 kfree(dev->res);
144 kfree(dev); 153 kfree(dev);
145 return NULL; 154 return NULL;
146 } 155 }
diff --git a/drivers/pnp/interface.c b/drivers/pnp/interface.c
index 5695a79f3a52..a876ecf7028c 100644
--- a/drivers/pnp/interface.c
+++ b/drivers/pnp/interface.c
@@ -3,6 +3,8 @@
3 * 3 *
4 * Some code, especially possible resource dumping is based on isapnp_proc.c (c) Jaroslav Kysela <perex@perex.cz> 4 * Some code, especially possible resource dumping is based on isapnp_proc.c (c) Jaroslav Kysela <perex@perex.cz>
5 * Copyright 2002 Adam Belay <ambx1@neo.rr.com> 5 * Copyright 2002 Adam Belay <ambx1@neo.rr.com>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 */ 8 */
7 9
8#include <linux/pnp.h> 10#include <linux/pnp.h>
@@ -53,11 +55,13 @@ static int pnp_printf(pnp_info_buffer_t * buffer, char *fmt, ...)
53static void pnp_print_port(pnp_info_buffer_t * buffer, char *space, 55static void pnp_print_port(pnp_info_buffer_t * buffer, char *space,
54 struct pnp_port *port) 56 struct pnp_port *port)
55{ 57{
56 pnp_printf(buffer, 58 pnp_printf(buffer, "%sport %#llx-%#llx, align %#llx, size %#llx, "
57 "%sport 0x%x-0x%x, align 0x%x, size 0x%x, %i-bit address decoding\n", 59 "%i-bit address decoding\n", space,
58 space, port->min, port->max, 60 (unsigned long long) port->min,
59 port->align ? (port->align - 1) : 0, port->size, 61 (unsigned long long) port->max,
60 port->flags & PNP_PORT_FLAG_16BITADDR ? 16 : 10); 62 port->align ? ((unsigned long long) port->align - 1) : 0,
63 (unsigned long long) port->size,
64 port->flags & IORESOURCE_IO_16BIT_ADDR ? 16 : 10);
61} 65}
62 66
63static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space, 67static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space,
@@ -67,7 +71,7 @@ static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space,
67 71
68 pnp_printf(buffer, "%sirq ", space); 72 pnp_printf(buffer, "%sirq ", space);
69 for (i = 0; i < PNP_IRQ_NR; i++) 73 for (i = 0; i < PNP_IRQ_NR; i++)
70 if (test_bit(i, irq->map)) { 74 if (test_bit(i, irq->map.bits)) {
71 if (!first) { 75 if (!first) {
72 pnp_printf(buffer, ","); 76 pnp_printf(buffer, ",");
73 } else { 77 } else {
@@ -78,7 +82,7 @@ static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space,
78 else 82 else
79 pnp_printf(buffer, "%i", i); 83 pnp_printf(buffer, "%i", i);
80 } 84 }
81 if (bitmap_empty(irq->map, PNP_IRQ_NR)) 85 if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
82 pnp_printf(buffer, "<none>"); 86 pnp_printf(buffer, "<none>");
83 if (irq->flags & IORESOURCE_IRQ_HIGHEDGE) 87 if (irq->flags & IORESOURCE_IRQ_HIGHEDGE)
84 pnp_printf(buffer, " High-Edge"); 88 pnp_printf(buffer, " High-Edge");
@@ -88,6 +92,8 @@ static void pnp_print_irq(pnp_info_buffer_t * buffer, char *space,
88 pnp_printf(buffer, " High-Level"); 92 pnp_printf(buffer, " High-Level");
89 if (irq->flags & IORESOURCE_IRQ_LOWLEVEL) 93 if (irq->flags & IORESOURCE_IRQ_LOWLEVEL)
90 pnp_printf(buffer, " Low-Level"); 94 pnp_printf(buffer, " Low-Level");
95 if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
96 pnp_printf(buffer, " (optional)");
91 pnp_printf(buffer, "\n"); 97 pnp_printf(buffer, "\n");
92} 98}
93 99
@@ -148,8 +154,11 @@ static void pnp_print_mem(pnp_info_buffer_t * buffer, char *space,
148{ 154{
149 char *s; 155 char *s;
150 156
151 pnp_printf(buffer, "%sMemory 0x%x-0x%x, align 0x%x, size 0x%x", 157 pnp_printf(buffer, "%sMemory %#llx-%#llx, align %#llx, size %#llx",
152 space, mem->min, mem->max, mem->align, mem->size); 158 space, (unsigned long long) mem->min,
159 (unsigned long long) mem->max,
160 (unsigned long long) mem->align,
161 (unsigned long long) mem->size);
153 if (mem->flags & IORESOURCE_MEM_WRITEABLE) 162 if (mem->flags & IORESOURCE_MEM_WRITEABLE)
154 pnp_printf(buffer, ", writeable"); 163 pnp_printf(buffer, ", writeable");
155 if (mem->flags & IORESOURCE_MEM_CACHEABLE) 164 if (mem->flags & IORESOURCE_MEM_CACHEABLE)
@@ -177,65 +186,58 @@ static void pnp_print_mem(pnp_info_buffer_t * buffer, char *space,
177} 186}
178 187
179static void pnp_print_option(pnp_info_buffer_t * buffer, char *space, 188static void pnp_print_option(pnp_info_buffer_t * buffer, char *space,
180 struct pnp_option *option, int dep) 189 struct pnp_option *option)
181{ 190{
182 char *s; 191 switch (option->type) {
183 struct pnp_port *port; 192 case IORESOURCE_IO:
184 struct pnp_irq *irq; 193 pnp_print_port(buffer, space, &option->u.port);
185 struct pnp_dma *dma; 194 break;
186 struct pnp_mem *mem; 195 case IORESOURCE_MEM:
187 196 pnp_print_mem(buffer, space, &option->u.mem);
188 if (dep) { 197 break;
189 switch (option->priority) { 198 case IORESOURCE_IRQ:
190 case PNP_RES_PRIORITY_PREFERRED: 199 pnp_print_irq(buffer, space, &option->u.irq);
191 s = "preferred"; 200 break;
192 break; 201 case IORESOURCE_DMA:
193 case PNP_RES_PRIORITY_ACCEPTABLE: 202 pnp_print_dma(buffer, space, &option->u.dma);
194 s = "acceptable"; 203 break;
195 break;
196 case PNP_RES_PRIORITY_FUNCTIONAL:
197 s = "functional";
198 break;
199 default:
200 s = "invalid";
201 }
202 pnp_printf(buffer, "Dependent: %02i - Priority %s\n", dep, s);
203 } 204 }
204
205 for (port = option->port; port; port = port->next)
206 pnp_print_port(buffer, space, port);
207 for (irq = option->irq; irq; irq = irq->next)
208 pnp_print_irq(buffer, space, irq);
209 for (dma = option->dma; dma; dma = dma->next)
210 pnp_print_dma(buffer, space, dma);
211 for (mem = option->mem; mem; mem = mem->next)
212 pnp_print_mem(buffer, space, mem);
213} 205}
214 206
215static ssize_t pnp_show_options(struct device *dmdev, 207static ssize_t pnp_show_options(struct device *dmdev,
216 struct device_attribute *attr, char *buf) 208 struct device_attribute *attr, char *buf)
217{ 209{
218 struct pnp_dev *dev = to_pnp_dev(dmdev); 210 struct pnp_dev *dev = to_pnp_dev(dmdev);
219 struct pnp_option *independent = dev->independent; 211 pnp_info_buffer_t *buffer;
220 struct pnp_option *dependent = dev->dependent; 212 struct pnp_option *option;
221 int ret, dep = 1; 213 int ret, dep = 0, set = 0;
214 char *indent;
222 215
223 pnp_info_buffer_t *buffer = (pnp_info_buffer_t *) 216 buffer = pnp_alloc(sizeof(pnp_info_buffer_t));
224 pnp_alloc(sizeof(pnp_info_buffer_t));
225 if (!buffer) 217 if (!buffer)
226 return -ENOMEM; 218 return -ENOMEM;
227 219
228 buffer->len = PAGE_SIZE; 220 buffer->len = PAGE_SIZE;
229 buffer->buffer = buf; 221 buffer->buffer = buf;
230 buffer->curr = buffer->buffer; 222 buffer->curr = buffer->buffer;
231 if (independent)
232 pnp_print_option(buffer, "", independent, 0);
233 223
234 while (dependent) { 224 list_for_each_entry(option, &dev->options, list) {
235 pnp_print_option(buffer, " ", dependent, dep); 225 if (pnp_option_is_dependent(option)) {
236 dependent = dependent->next; 226 indent = " ";
237 dep++; 227 if (!dep || pnp_option_set(option) != set) {
228 set = pnp_option_set(option);
229 dep = 1;
230 pnp_printf(buffer, "Dependent: %02i - "
231 "Priority %s\n", set,
232 pnp_option_priority_name(option));
233 }
234 } else {
235 dep = 0;
236 indent = "";
237 }
238 pnp_print_option(buffer, indent, option);
238 } 239 }
240
239 ret = (buffer->curr - buf); 241 ret = (buffer->curr - buf);
240 kfree(buffer); 242 kfree(buffer);
241 return ret; 243 return ret;
@@ -248,79 +250,59 @@ static ssize_t pnp_show_current_resources(struct device *dmdev,
248 char *buf) 250 char *buf)
249{ 251{
250 struct pnp_dev *dev = to_pnp_dev(dmdev); 252 struct pnp_dev *dev = to_pnp_dev(dmdev);
251 struct resource *res;
252 int i, ret;
253 pnp_info_buffer_t *buffer; 253 pnp_info_buffer_t *buffer;
254 struct pnp_resource *pnp_res;
255 struct resource *res;
256 int ret;
254 257
255 if (!dev) 258 if (!dev)
256 return -EINVAL; 259 return -EINVAL;
257 260
258 buffer = (pnp_info_buffer_t *) pnp_alloc(sizeof(pnp_info_buffer_t)); 261 buffer = pnp_alloc(sizeof(pnp_info_buffer_t));
259 if (!buffer) 262 if (!buffer)
260 return -ENOMEM; 263 return -ENOMEM;
264
261 buffer->len = PAGE_SIZE; 265 buffer->len = PAGE_SIZE;
262 buffer->buffer = buf; 266 buffer->buffer = buf;
263 buffer->curr = buffer->buffer; 267 buffer->curr = buffer->buffer;
264 268
265 pnp_printf(buffer, "state = "); 269 pnp_printf(buffer, "state = %s\n", dev->active ? "active" : "disabled");
266 if (dev->active) 270
267 pnp_printf(buffer, "active\n"); 271 list_for_each_entry(pnp_res, &dev->resources, list) {
268 else 272 res = &pnp_res->res;
269 pnp_printf(buffer, "disabled\n"); 273
270 274 pnp_printf(buffer, pnp_resource_type_name(res));
271 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) { 275
272 if (pnp_resource_valid(res)) { 276 if (res->flags & IORESOURCE_DISABLED) {
273 pnp_printf(buffer, "io"); 277 pnp_printf(buffer, " disabled\n");
274 if (res->flags & IORESOURCE_DISABLED) 278 continue;
275 pnp_printf(buffer, " disabled\n");
276 else
277 pnp_printf(buffer, " 0x%llx-0x%llx\n",
278 (unsigned long long) res->start,
279 (unsigned long long) res->end);
280 }
281 }
282 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
283 if (pnp_resource_valid(res)) {
284 pnp_printf(buffer, "mem");
285 if (res->flags & IORESOURCE_DISABLED)
286 pnp_printf(buffer, " disabled\n");
287 else
288 pnp_printf(buffer, " 0x%llx-0x%llx\n",
289 (unsigned long long) res->start,
290 (unsigned long long) res->end);
291 }
292 }
293 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IRQ, i)); i++) {
294 if (pnp_resource_valid(res)) {
295 pnp_printf(buffer, "irq");
296 if (res->flags & IORESOURCE_DISABLED)
297 pnp_printf(buffer, " disabled\n");
298 else
299 pnp_printf(buffer, " %lld\n",
300 (unsigned long long) res->start);
301 } 279 }
302 } 280
303 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_DMA, i)); i++) { 281 switch (pnp_resource_type(res)) {
304 if (pnp_resource_valid(res)) { 282 case IORESOURCE_IO:
305 pnp_printf(buffer, "dma"); 283 case IORESOURCE_MEM:
306 if (res->flags & IORESOURCE_DISABLED) 284 pnp_printf(buffer, " %#llx-%#llx\n",
307 pnp_printf(buffer, " disabled\n"); 285 (unsigned long long) res->start,
308 else 286 (unsigned long long) res->end);
309 pnp_printf(buffer, " %lld\n", 287 break;
310 (unsigned long long) res->start); 288 case IORESOURCE_IRQ:
289 case IORESOURCE_DMA:
290 pnp_printf(buffer, " %lld\n",
291 (unsigned long long) res->start);
292 break;
311 } 293 }
312 } 294 }
295
313 ret = (buffer->curr - buf); 296 ret = (buffer->curr - buf);
314 kfree(buffer); 297 kfree(buffer);
315 return ret; 298 return ret;
316} 299}
317 300
318static ssize_t 301static ssize_t pnp_set_current_resources(struct device *dmdev,
319pnp_set_current_resources(struct device *dmdev, struct device_attribute *attr, 302 struct device_attribute *attr,
320 const char *ubuf, size_t count) 303 const char *ubuf, size_t count)
321{ 304{
322 struct pnp_dev *dev = to_pnp_dev(dmdev); 305 struct pnp_dev *dev = to_pnp_dev(dmdev);
323 struct pnp_resource *pnp_res;
324 char *buf = (void *)ubuf; 306 char *buf = (void *)ubuf;
325 int retval = 0; 307 int retval = 0;
326 resource_size_t start, end; 308 resource_size_t start, end;
@@ -368,7 +350,6 @@ pnp_set_current_resources(struct device *dmdev, struct device_attribute *attr,
368 goto done; 350 goto done;
369 } 351 }
370 if (!strnicmp(buf, "set", 3)) { 352 if (!strnicmp(buf, "set", 3)) {
371 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
372 if (dev->active) 353 if (dev->active)
373 goto done; 354 goto done;
374 buf += 3; 355 buf += 3;
@@ -391,10 +372,7 @@ pnp_set_current_resources(struct device *dmdev, struct device_attribute *attr,
391 end = simple_strtoul(buf, &buf, 0); 372 end = simple_strtoul(buf, &buf, 0);
392 } else 373 } else
393 end = start; 374 end = start;
394 pnp_res = pnp_add_io_resource(dev, start, end, 375 pnp_add_io_resource(dev, start, end, 0);
395 0);
396 if (pnp_res)
397 pnp_res->index = nport++;
398 continue; 376 continue;
399 } 377 }
400 if (!strnicmp(buf, "mem", 3)) { 378 if (!strnicmp(buf, "mem", 3)) {
@@ -411,10 +389,7 @@ pnp_set_current_resources(struct device *dmdev, struct device_attribute *attr,
411 end = simple_strtoul(buf, &buf, 0); 389 end = simple_strtoul(buf, &buf, 0);
412 } else 390 } else
413 end = start; 391 end = start;
414 pnp_res = pnp_add_mem_resource(dev, start, end, 392 pnp_add_mem_resource(dev, start, end, 0);
415 0);
416 if (pnp_res)
417 pnp_res->index = nmem++;
418 continue; 393 continue;
419 } 394 }
420 if (!strnicmp(buf, "irq", 3)) { 395 if (!strnicmp(buf, "irq", 3)) {
@@ -422,9 +397,7 @@ pnp_set_current_resources(struct device *dmdev, struct device_attribute *attr,
422 while (isspace(*buf)) 397 while (isspace(*buf))
423 ++buf; 398 ++buf;
424 start = simple_strtoul(buf, &buf, 0); 399 start = simple_strtoul(buf, &buf, 0);
425 pnp_res = pnp_add_irq_resource(dev, start, 0); 400 pnp_add_irq_resource(dev, start, 0);
426 if (pnp_res)
427 pnp_res->index = nirq++;
428 continue; 401 continue;
429 } 402 }
430 if (!strnicmp(buf, "dma", 3)) { 403 if (!strnicmp(buf, "dma", 3)) {
@@ -432,9 +405,7 @@ pnp_set_current_resources(struct device *dmdev, struct device_attribute *attr,
432 while (isspace(*buf)) 405 while (isspace(*buf))
433 ++buf; 406 ++buf;
434 start = simple_strtoul(buf, &buf, 0); 407 start = simple_strtoul(buf, &buf, 0);
435 pnp_res = pnp_add_dma_resource(dev, start, 0); 408 pnp_add_dma_resource(dev, start, 0);
436 if (pnp_res)
437 pnp_res->index = ndma++;
438 continue; 409 continue;
439 } 410 }
440 break; 411 break;
diff --git a/drivers/pnp/isapnp/core.c b/drivers/pnp/isapnp/core.c
index f1bccdbdeb08..101a835e8759 100644
--- a/drivers/pnp/isapnp/core.c
+++ b/drivers/pnp/isapnp/core.c
@@ -429,154 +429,135 @@ static struct pnp_dev *__init isapnp_parse_device(struct pnp_card *card,
429 * Add IRQ resource to resources list. 429 * Add IRQ resource to resources list.
430 */ 430 */
431static void __init isapnp_parse_irq_resource(struct pnp_dev *dev, 431static void __init isapnp_parse_irq_resource(struct pnp_dev *dev,
432 struct pnp_option *option, 432 unsigned int option_flags,
433 int size) 433 int size)
434{ 434{
435 unsigned char tmp[3]; 435 unsigned char tmp[3];
436 struct pnp_irq *irq;
437 unsigned long bits; 436 unsigned long bits;
437 pnp_irq_mask_t map;
438 unsigned char flags = IORESOURCE_IRQ_HIGHEDGE;
438 439
439 isapnp_peek(tmp, size); 440 isapnp_peek(tmp, size);
440 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
441 if (!irq)
442 return;
443 bits = (tmp[1] << 8) | tmp[0]; 441 bits = (tmp[1] << 8) | tmp[0];
444 bitmap_copy(irq->map, &bits, 16); 442
443 bitmap_zero(map.bits, PNP_IRQ_NR);
444 bitmap_copy(map.bits, &bits, 16);
445
445 if (size > 2) 446 if (size > 2)
446 irq->flags = tmp[2]; 447 flags = tmp[2];
447 else 448
448 irq->flags = IORESOURCE_IRQ_HIGHEDGE; 449 pnp_register_irq_resource(dev, option_flags, &map, flags);
449 pnp_register_irq_resource(dev, option, irq);
450} 450}
451 451
452/* 452/*
453 * Add DMA resource to resources list. 453 * Add DMA resource to resources list.
454 */ 454 */
455static void __init isapnp_parse_dma_resource(struct pnp_dev *dev, 455static void __init isapnp_parse_dma_resource(struct pnp_dev *dev,
456 struct pnp_option *option, 456 unsigned int option_flags,
457 int size) 457 int size)
458{ 458{
459 unsigned char tmp[2]; 459 unsigned char tmp[2];
460 struct pnp_dma *dma;
461 460
462 isapnp_peek(tmp, size); 461 isapnp_peek(tmp, size);
463 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL); 462 pnp_register_dma_resource(dev, option_flags, tmp[0], tmp[1]);
464 if (!dma)
465 return;
466 dma->map = tmp[0];
467 dma->flags = tmp[1];
468 pnp_register_dma_resource(dev, option, dma);
469} 463}
470 464
471/* 465/*
472 * Add port resource to resources list. 466 * Add port resource to resources list.
473 */ 467 */
474static void __init isapnp_parse_port_resource(struct pnp_dev *dev, 468static void __init isapnp_parse_port_resource(struct pnp_dev *dev,
475 struct pnp_option *option, 469 unsigned int option_flags,
476 int size) 470 int size)
477{ 471{
478 unsigned char tmp[7]; 472 unsigned char tmp[7];
479 struct pnp_port *port; 473 resource_size_t min, max, align, len;
474 unsigned char flags;
480 475
481 isapnp_peek(tmp, size); 476 isapnp_peek(tmp, size);
482 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL); 477 min = (tmp[2] << 8) | tmp[1];
483 if (!port) 478 max = (tmp[4] << 8) | tmp[3];
484 return; 479 align = tmp[5];
485 port->min = (tmp[2] << 8) | tmp[1]; 480 len = tmp[6];
486 port->max = (tmp[4] << 8) | tmp[3]; 481 flags = tmp[0] ? IORESOURCE_IO_16BIT_ADDR : 0;
487 port->align = tmp[5]; 482 pnp_register_port_resource(dev, option_flags,
488 port->size = tmp[6]; 483 min, max, align, len, flags);
489 port->flags = tmp[0] ? PNP_PORT_FLAG_16BITADDR : 0;
490 pnp_register_port_resource(dev, option, port);
491} 484}
492 485
493/* 486/*
494 * Add fixed port resource to resources list. 487 * Add fixed port resource to resources list.
495 */ 488 */
496static void __init isapnp_parse_fixed_port_resource(struct pnp_dev *dev, 489static void __init isapnp_parse_fixed_port_resource(struct pnp_dev *dev,
497 struct pnp_option *option, 490 unsigned int option_flags,
498 int size) 491 int size)
499{ 492{
500 unsigned char tmp[3]; 493 unsigned char tmp[3];
501 struct pnp_port *port; 494 resource_size_t base, len;
502 495
503 isapnp_peek(tmp, size); 496 isapnp_peek(tmp, size);
504 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL); 497 base = (tmp[1] << 8) | tmp[0];
505 if (!port) 498 len = tmp[2];
506 return; 499 pnp_register_port_resource(dev, option_flags, base, base, 0, len,
507 port->min = port->max = (tmp[1] << 8) | tmp[0]; 500 IORESOURCE_IO_FIXED);
508 port->size = tmp[2];
509 port->align = 0;
510 port->flags = PNP_PORT_FLAG_FIXED;
511 pnp_register_port_resource(dev, option, port);
512} 501}
513 502
514/* 503/*
515 * Add memory resource to resources list. 504 * Add memory resource to resources list.
516 */ 505 */
517static void __init isapnp_parse_mem_resource(struct pnp_dev *dev, 506static void __init isapnp_parse_mem_resource(struct pnp_dev *dev,
518 struct pnp_option *option, 507 unsigned int option_flags,
519 int size) 508 int size)
520{ 509{
521 unsigned char tmp[9]; 510 unsigned char tmp[9];
522 struct pnp_mem *mem; 511 resource_size_t min, max, align, len;
512 unsigned char flags;
523 513
524 isapnp_peek(tmp, size); 514 isapnp_peek(tmp, size);
525 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); 515 min = ((tmp[2] << 8) | tmp[1]) << 8;
526 if (!mem) 516 max = ((tmp[4] << 8) | tmp[3]) << 8;
527 return; 517 align = (tmp[6] << 8) | tmp[5];
528 mem->min = ((tmp[2] << 8) | tmp[1]) << 8; 518 len = ((tmp[8] << 8) | tmp[7]) << 8;
529 mem->max = ((tmp[4] << 8) | tmp[3]) << 8; 519 flags = tmp[0];
530 mem->align = (tmp[6] << 8) | tmp[5]; 520 pnp_register_mem_resource(dev, option_flags,
531 mem->size = ((tmp[8] << 8) | tmp[7]) << 8; 521 min, max, align, len, flags);
532 mem->flags = tmp[0];
533 pnp_register_mem_resource(dev, option, mem);
534} 522}
535 523
536/* 524/*
537 * Add 32-bit memory resource to resources list. 525 * Add 32-bit memory resource to resources list.
538 */ 526 */
539static void __init isapnp_parse_mem32_resource(struct pnp_dev *dev, 527static void __init isapnp_parse_mem32_resource(struct pnp_dev *dev,
540 struct pnp_option *option, 528 unsigned int option_flags,
541 int size) 529 int size)
542{ 530{
543 unsigned char tmp[17]; 531 unsigned char tmp[17];
544 struct pnp_mem *mem; 532 resource_size_t min, max, align, len;
533 unsigned char flags;
545 534
546 isapnp_peek(tmp, size); 535 isapnp_peek(tmp, size);
547 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); 536 min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
548 if (!mem) 537 max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
549 return; 538 align = (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
550 mem->min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1]; 539 len = (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
551 mem->max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5]; 540 flags = tmp[0];
552 mem->align = 541 pnp_register_mem_resource(dev, option_flags,
553 (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9]; 542 min, max, align, len, flags);
554 mem->size =
555 (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
556 mem->flags = tmp[0];
557 pnp_register_mem_resource(dev, option, mem);
558} 543}
559 544
560/* 545/*
561 * Add 32-bit fixed memory resource to resources list. 546 * Add 32-bit fixed memory resource to resources list.
562 */ 547 */
563static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev, 548static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev,
564 struct pnp_option *option, 549 unsigned int option_flags,
565 int size) 550 int size)
566{ 551{
567 unsigned char tmp[9]; 552 unsigned char tmp[9];
568 struct pnp_mem *mem; 553 resource_size_t base, len;
554 unsigned char flags;
569 555
570 isapnp_peek(tmp, size); 556 isapnp_peek(tmp, size);
571 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); 557 base = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
572 if (!mem) 558 len = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
573 return; 559 flags = tmp[0];
574 mem->min = mem->max = 560 pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
575 (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
576 mem->size = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
577 mem->align = 0;
578 mem->flags = tmp[0];
579 pnp_register_mem_resource(dev, option, mem);
580} 561}
581 562
582/* 563/*
@@ -604,20 +585,16 @@ isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
604static int __init isapnp_create_device(struct pnp_card *card, 585static int __init isapnp_create_device(struct pnp_card *card,
605 unsigned short size) 586 unsigned short size)
606{ 587{
607 int number = 0, skip = 0, priority = 0, compat = 0; 588 int number = 0, skip = 0, priority, compat = 0;
608 unsigned char type, tmp[17]; 589 unsigned char type, tmp[17];
609 struct pnp_option *option; 590 unsigned int option_flags;
610 struct pnp_dev *dev; 591 struct pnp_dev *dev;
611 u32 eisa_id; 592 u32 eisa_id;
612 char id[8]; 593 char id[8];
613 594
614 if ((dev = isapnp_parse_device(card, size, number++)) == NULL) 595 if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
615 return 1; 596 return 1;
616 option = pnp_register_independent_option(dev); 597 option_flags = 0;
617 if (!option) {
618 kfree(dev);
619 return 1;
620 }
621 pnp_add_card_device(card, dev); 598 pnp_add_card_device(card, dev);
622 599
623 while (1) { 600 while (1) {
@@ -634,16 +611,11 @@ static int __init isapnp_create_device(struct pnp_card *card,
634 return 1; 611 return 1;
635 size = 0; 612 size = 0;
636 skip = 0; 613 skip = 0;
637 option = pnp_register_independent_option(dev); 614 option_flags = 0;
638 if (!option) {
639 kfree(dev);
640 return 1;
641 }
642 pnp_add_card_device(card, dev); 615 pnp_add_card_device(card, dev);
643 } else { 616 } else {
644 skip = 1; 617 skip = 1;
645 } 618 }
646 priority = 0;
647 compat = 0; 619 compat = 0;
648 break; 620 break;
649 case _STAG_COMPATDEVID: 621 case _STAG_COMPATDEVID:
@@ -660,44 +632,42 @@ static int __init isapnp_create_device(struct pnp_card *card,
660 case _STAG_IRQ: 632 case _STAG_IRQ:
661 if (size < 2 || size > 3) 633 if (size < 2 || size > 3)
662 goto __skip; 634 goto __skip;
663 isapnp_parse_irq_resource(dev, option, size); 635 isapnp_parse_irq_resource(dev, option_flags, size);
664 size = 0; 636 size = 0;
665 break; 637 break;
666 case _STAG_DMA: 638 case _STAG_DMA:
667 if (size != 2) 639 if (size != 2)
668 goto __skip; 640 goto __skip;
669 isapnp_parse_dma_resource(dev, option, size); 641 isapnp_parse_dma_resource(dev, option_flags, size);
670 size = 0; 642 size = 0;
671 break; 643 break;
672 case _STAG_STARTDEP: 644 case _STAG_STARTDEP:
673 if (size > 1) 645 if (size > 1)
674 goto __skip; 646 goto __skip;
675 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE; 647 priority = PNP_RES_PRIORITY_ACCEPTABLE;
676 if (size > 0) { 648 if (size > 0) {
677 isapnp_peek(tmp, size); 649 isapnp_peek(tmp, size);
678 priority = 0x100 | tmp[0]; 650 priority = tmp[0];
679 size = 0; 651 size = 0;
680 } 652 }
681 option = pnp_register_dependent_option(dev, priority); 653 option_flags = pnp_new_dependent_set(dev, priority);
682 if (!option)
683 return 1;
684 break; 654 break;
685 case _STAG_ENDDEP: 655 case _STAG_ENDDEP:
686 if (size != 0) 656 if (size != 0)
687 goto __skip; 657 goto __skip;
688 priority = 0; 658 option_flags = 0;
689 dev_dbg(&dev->dev, "end dependent options\n");
690 break; 659 break;
691 case _STAG_IOPORT: 660 case _STAG_IOPORT:
692 if (size != 7) 661 if (size != 7)
693 goto __skip; 662 goto __skip;
694 isapnp_parse_port_resource(dev, option, size); 663 isapnp_parse_port_resource(dev, option_flags, size);
695 size = 0; 664 size = 0;
696 break; 665 break;
697 case _STAG_FIXEDIO: 666 case _STAG_FIXEDIO:
698 if (size != 3) 667 if (size != 3)
699 goto __skip; 668 goto __skip;
700 isapnp_parse_fixed_port_resource(dev, option, size); 669 isapnp_parse_fixed_port_resource(dev, option_flags,
670 size);
701 size = 0; 671 size = 0;
702 break; 672 break;
703 case _STAG_VENDOR: 673 case _STAG_VENDOR:
@@ -705,7 +675,7 @@ static int __init isapnp_create_device(struct pnp_card *card,
705 case _LTAG_MEMRANGE: 675 case _LTAG_MEMRANGE:
706 if (size != 9) 676 if (size != 9)
707 goto __skip; 677 goto __skip;
708 isapnp_parse_mem_resource(dev, option, size); 678 isapnp_parse_mem_resource(dev, option_flags, size);
709 size = 0; 679 size = 0;
710 break; 680 break;
711 case _LTAG_ANSISTR: 681 case _LTAG_ANSISTR:
@@ -720,13 +690,14 @@ static int __init isapnp_create_device(struct pnp_card *card,
720 case _LTAG_MEM32RANGE: 690 case _LTAG_MEM32RANGE:
721 if (size != 17) 691 if (size != 17)
722 goto __skip; 692 goto __skip;
723 isapnp_parse_mem32_resource(dev, option, size); 693 isapnp_parse_mem32_resource(dev, option_flags, size);
724 size = 0; 694 size = 0;
725 break; 695 break;
726 case _LTAG_FIXEDMEM32RANGE: 696 case _LTAG_FIXEDMEM32RANGE:
727 if (size != 9) 697 if (size != 9)
728 goto __skip; 698 goto __skip;
729 isapnp_parse_fixed_mem32_resource(dev, option, size); 699 isapnp_parse_fixed_mem32_resource(dev, option_flags,
700 size);
730 size = 0; 701 size = 0;
731 break; 702 break;
732 case _STAG_END: 703 case _STAG_END:
@@ -928,7 +899,6 @@ EXPORT_SYMBOL(isapnp_write_byte);
928 899
929static int isapnp_get_resources(struct pnp_dev *dev) 900static int isapnp_get_resources(struct pnp_dev *dev)
930{ 901{
931 struct pnp_resource *pnp_res;
932 int i, ret; 902 int i, ret;
933 903
934 dev_dbg(&dev->dev, "get resources\n"); 904 dev_dbg(&dev->dev, "get resources\n");
@@ -940,35 +910,23 @@ static int isapnp_get_resources(struct pnp_dev *dev)
940 910
941 for (i = 0; i < ISAPNP_MAX_PORT; i++) { 911 for (i = 0; i < ISAPNP_MAX_PORT; i++) {
942 ret = isapnp_read_word(ISAPNP_CFG_PORT + (i << 1)); 912 ret = isapnp_read_word(ISAPNP_CFG_PORT + (i << 1));
943 if (ret) { 913 pnp_add_io_resource(dev, ret, ret,
944 pnp_res = pnp_add_io_resource(dev, ret, ret, 0); 914 ret == 0 ? IORESOURCE_DISABLED : 0);
945 if (pnp_res)
946 pnp_res->index = i;
947 }
948 } 915 }
949 for (i = 0; i < ISAPNP_MAX_MEM; i++) { 916 for (i = 0; i < ISAPNP_MAX_MEM; i++) {
950 ret = isapnp_read_word(ISAPNP_CFG_MEM + (i << 3)) << 8; 917 ret = isapnp_read_word(ISAPNP_CFG_MEM + (i << 3)) << 8;
951 if (ret) { 918 pnp_add_mem_resource(dev, ret, ret,
952 pnp_res = pnp_add_mem_resource(dev, ret, ret, 0); 919 ret == 0 ? IORESOURCE_DISABLED : 0);
953 if (pnp_res)
954 pnp_res->index = i;
955 }
956 } 920 }
957 for (i = 0; i < ISAPNP_MAX_IRQ; i++) { 921 for (i = 0; i < ISAPNP_MAX_IRQ; i++) {
958 ret = isapnp_read_word(ISAPNP_CFG_IRQ + (i << 1)) >> 8; 922 ret = isapnp_read_word(ISAPNP_CFG_IRQ + (i << 1)) >> 8;
959 if (ret) { 923 pnp_add_irq_resource(dev, ret,
960 pnp_res = pnp_add_irq_resource(dev, ret, 0); 924 ret == 0 ? IORESOURCE_DISABLED : 0);
961 if (pnp_res)
962 pnp_res->index = i;
963 }
964 } 925 }
965 for (i = 0; i < ISAPNP_MAX_DMA; i++) { 926 for (i = 0; i < ISAPNP_MAX_DMA; i++) {
966 ret = isapnp_read_byte(ISAPNP_CFG_DMA + i); 927 ret = isapnp_read_byte(ISAPNP_CFG_DMA + i);
967 if (ret != 4) { 928 pnp_add_dma_resource(dev, ret,
968 pnp_res = pnp_add_dma_resource(dev, ret, 0); 929 ret == 4 ? IORESOURCE_DISABLED : 0);
969 if (pnp_res)
970 pnp_res->index = i;
971 }
972 } 930 }
973 931
974__end: 932__end:
@@ -978,62 +936,45 @@ __end:
978 936
979static int isapnp_set_resources(struct pnp_dev *dev) 937static int isapnp_set_resources(struct pnp_dev *dev)
980{ 938{
981 struct pnp_resource *pnp_res;
982 struct resource *res; 939 struct resource *res;
983 int tmp, index; 940 int tmp;
984 941
985 dev_dbg(&dev->dev, "set resources\n"); 942 dev_dbg(&dev->dev, "set resources\n");
986 isapnp_cfg_begin(dev->card->number, dev->number); 943 isapnp_cfg_begin(dev->card->number, dev->number);
987 dev->active = 1; 944 dev->active = 1;
988 for (tmp = 0; tmp < ISAPNP_MAX_PORT; tmp++) { 945 for (tmp = 0; tmp < ISAPNP_MAX_PORT; tmp++) {
989 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IO, tmp); 946 res = pnp_get_resource(dev, IORESOURCE_IO, tmp);
990 if (!pnp_res) 947 if (pnp_resource_enabled(res)) {
991 continue;
992 res = &pnp_res->res;
993 if (pnp_resource_valid(res)) {
994 index = pnp_res->index;
995 dev_dbg(&dev->dev, " set io %d to %#llx\n", 948 dev_dbg(&dev->dev, " set io %d to %#llx\n",
996 index, (unsigned long long) res->start); 949 tmp, (unsigned long long) res->start);
997 isapnp_write_word(ISAPNP_CFG_PORT + (index << 1), 950 isapnp_write_word(ISAPNP_CFG_PORT + (tmp << 1),
998 res->start); 951 res->start);
999 } 952 }
1000 } 953 }
1001 for (tmp = 0; tmp < ISAPNP_MAX_IRQ; tmp++) { 954 for (tmp = 0; tmp < ISAPNP_MAX_IRQ; tmp++) {
1002 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IRQ, tmp); 955 res = pnp_get_resource(dev, IORESOURCE_IRQ, tmp);
1003 if (!pnp_res) 956 if (pnp_resource_enabled(res)) {
1004 continue;
1005 res = &pnp_res->res;
1006 if (pnp_resource_valid(res)) {
1007 int irq = res->start; 957 int irq = res->start;
1008 if (irq == 2) 958 if (irq == 2)
1009 irq = 9; 959 irq = 9;
1010 index = pnp_res->index; 960 dev_dbg(&dev->dev, " set irq %d to %d\n", tmp, irq);
1011 dev_dbg(&dev->dev, " set irq %d to %d\n", index, irq); 961 isapnp_write_byte(ISAPNP_CFG_IRQ + (tmp << 1), irq);
1012 isapnp_write_byte(ISAPNP_CFG_IRQ + (index << 1), irq);
1013 } 962 }
1014 } 963 }
1015 for (tmp = 0; tmp < ISAPNP_MAX_DMA; tmp++) { 964 for (tmp = 0; tmp < ISAPNP_MAX_DMA; tmp++) {
1016 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_DMA, tmp); 965 res = pnp_get_resource(dev, IORESOURCE_DMA, tmp);
1017 if (!pnp_res) 966 if (pnp_resource_enabled(res)) {
1018 continue;
1019 res = &pnp_res->res;
1020 if (pnp_resource_valid(res)) {
1021 index = pnp_res->index;
1022 dev_dbg(&dev->dev, " set dma %d to %lld\n", 967 dev_dbg(&dev->dev, " set dma %d to %lld\n",
1023 index, (unsigned long long) res->start); 968 tmp, (unsigned long long) res->start);
1024 isapnp_write_byte(ISAPNP_CFG_DMA + index, res->start); 969 isapnp_write_byte(ISAPNP_CFG_DMA + tmp, res->start);
1025 } 970 }
1026 } 971 }
1027 for (tmp = 0; tmp < ISAPNP_MAX_MEM; tmp++) { 972 for (tmp = 0; tmp < ISAPNP_MAX_MEM; tmp++) {
1028 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_MEM, tmp); 973 res = pnp_get_resource(dev, IORESOURCE_MEM, tmp);
1029 if (!pnp_res) 974 if (pnp_resource_enabled(res)) {
1030 continue;
1031 res = &pnp_res->res;
1032 if (pnp_resource_valid(res)) {
1033 index = pnp_res->index;
1034 dev_dbg(&dev->dev, " set mem %d to %#llx\n", 975 dev_dbg(&dev->dev, " set mem %d to %#llx\n",
1035 index, (unsigned long long) res->start); 976 tmp, (unsigned long long) res->start);
1036 isapnp_write_word(ISAPNP_CFG_MEM + (index << 3), 977 isapnp_write_word(ISAPNP_CFG_MEM + (tmp << 3),
1037 (res->start >> 8) & 0xffff); 978 (res->start >> 8) & 0xffff);
1038 } 979 }
1039 } 980 }
diff --git a/drivers/pnp/manager.c b/drivers/pnp/manager.c
index bea0914ff947..b526eaad3f6c 100644
--- a/drivers/pnp/manager.c
+++ b/drivers/pnp/manager.c
@@ -3,6 +3,8 @@
3 * 3 *
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz> 4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com> 5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 */ 8 */
7 9
8#include <linux/errno.h> 10#include <linux/errno.h>
@@ -19,82 +21,64 @@ DEFINE_MUTEX(pnp_res_mutex);
19 21
20static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx) 22static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
21{ 23{
22 struct pnp_resource *pnp_res; 24 struct resource *res, local_res;
23 struct resource *res;
24
25 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IO, idx);
26 if (!pnp_res) {
27 dev_err(&dev->dev, "too many I/O port resources\n");
28 /* pretend we were successful so at least the manager won't try again */
29 return 1;
30 }
31
32 res = &pnp_res->res;
33 25
34 /* check if this resource has been manually set, if so skip */ 26 res = pnp_get_resource(dev, IORESOURCE_IO, idx);
35 if (!(res->flags & IORESOURCE_AUTO)) { 27 if (res) {
36 dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx " 28 dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
37 "flags %#lx\n", idx, (unsigned long long) res->start, 29 "flags %#lx\n", idx, (unsigned long long) res->start,
38 (unsigned long long) res->end, res->flags); 30 (unsigned long long) res->end, res->flags);
39 return 1; 31 return 0;
40 } 32 }
41 33
42 /* set the initial values */ 34 res = &local_res;
43 pnp_res->index = idx; 35 res->flags = rule->flags | IORESOURCE_AUTO;
44 res->flags |= rule->flags | IORESOURCE_IO; 36 res->start = 0;
45 res->flags &= ~IORESOURCE_UNSET; 37 res->end = 0;
46 38
47 if (!rule->size) { 39 if (!rule->size) {
48 res->flags |= IORESOURCE_DISABLED; 40 res->flags |= IORESOURCE_DISABLED;
49 dev_dbg(&dev->dev, " io %d disabled\n", idx); 41 dev_dbg(&dev->dev, " io %d disabled\n", idx);
50 return 1; /* skip disabled resource requests */ 42 goto __add;
51 } 43 }
52 44
53 res->start = rule->min; 45 res->start = rule->min;
54 res->end = res->start + rule->size - 1; 46 res->end = res->start + rule->size - 1;
55 47
56 /* run through until pnp_check_port is happy */
57 while (!pnp_check_port(dev, res)) { 48 while (!pnp_check_port(dev, res)) {
58 res->start += rule->align; 49 res->start += rule->align;
59 res->end = res->start + rule->size - 1; 50 res->end = res->start + rule->size - 1;
60 if (res->start > rule->max || !rule->align) { 51 if (res->start > rule->max || !rule->align) {
61 dev_dbg(&dev->dev, " couldn't assign io %d\n", idx); 52 dev_dbg(&dev->dev, " couldn't assign io %d "
62 return 0; 53 "(min %#llx max %#llx)\n", idx,
54 (unsigned long long) rule->min,
55 (unsigned long long) rule->max);
56 return -EBUSY;
63 } 57 }
64 } 58 }
65 dev_dbg(&dev->dev, " assign io %d %#llx-%#llx\n", idx, 59
66 (unsigned long long) res->start, (unsigned long long) res->end); 60__add:
67 return 1; 61 pnp_add_io_resource(dev, res->start, res->end, res->flags);
62 return 0;
68} 63}
69 64
70static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx) 65static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
71{ 66{
72 struct pnp_resource *pnp_res; 67 struct resource *res, local_res;
73 struct resource *res;
74
75 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_MEM, idx);
76 if (!pnp_res) {
77 dev_err(&dev->dev, "too many memory resources\n");
78 /* pretend we were successful so at least the manager won't try again */
79 return 1;
80 }
81 68
82 res = &pnp_res->res; 69 res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
83 70 if (res) {
84 /* check if this resource has been manually set, if so skip */
85 if (!(res->flags & IORESOURCE_AUTO)) {
86 dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx " 71 dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
87 "flags %#lx\n", idx, (unsigned long long) res->start, 72 "flags %#lx\n", idx, (unsigned long long) res->start,
88 (unsigned long long) res->end, res->flags); 73 (unsigned long long) res->end, res->flags);
89 return 1; 74 return 0;
90 } 75 }
91 76
92 /* set the initial values */ 77 res = &local_res;
93 pnp_res->index = idx; 78 res->flags = rule->flags | IORESOURCE_AUTO;
94 res->flags |= rule->flags | IORESOURCE_MEM; 79 res->start = 0;
95 res->flags &= ~IORESOURCE_UNSET; 80 res->end = 0;
96 81
97 /* convert pnp flags to standard Linux flags */
98 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE)) 82 if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
99 res->flags |= IORESOURCE_READONLY; 83 res->flags |= IORESOURCE_READONLY;
100 if (rule->flags & IORESOURCE_MEM_CACHEABLE) 84 if (rule->flags & IORESOURCE_MEM_CACHEABLE)
@@ -107,30 +91,32 @@ static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
107 if (!rule->size) { 91 if (!rule->size) {
108 res->flags |= IORESOURCE_DISABLED; 92 res->flags |= IORESOURCE_DISABLED;
109 dev_dbg(&dev->dev, " mem %d disabled\n", idx); 93 dev_dbg(&dev->dev, " mem %d disabled\n", idx);
110 return 1; /* skip disabled resource requests */ 94 goto __add;
111 } 95 }
112 96
113 res->start = rule->min; 97 res->start = rule->min;
114 res->end = res->start + rule->size - 1; 98 res->end = res->start + rule->size - 1;
115 99
116 /* run through until pnp_check_mem is happy */
117 while (!pnp_check_mem(dev, res)) { 100 while (!pnp_check_mem(dev, res)) {
118 res->start += rule->align; 101 res->start += rule->align;
119 res->end = res->start + rule->size - 1; 102 res->end = res->start + rule->size - 1;
120 if (res->start > rule->max || !rule->align) { 103 if (res->start > rule->max || !rule->align) {
121 dev_dbg(&dev->dev, " couldn't assign mem %d\n", idx); 104 dev_dbg(&dev->dev, " couldn't assign mem %d "
122 return 0; 105 "(min %#llx max %#llx)\n", idx,
106 (unsigned long long) rule->min,
107 (unsigned long long) rule->max);
108 return -EBUSY;
123 } 109 }
124 } 110 }
125 dev_dbg(&dev->dev, " assign mem %d %#llx-%#llx\n", idx, 111
126 (unsigned long long) res->start, (unsigned long long) res->end); 112__add:
127 return 1; 113 pnp_add_mem_resource(dev, res->start, res->end, res->flags);
114 return 0;
128} 115}
129 116
130static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx) 117static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
131{ 118{
132 struct pnp_resource *pnp_res; 119 struct resource *res, local_res;
133 struct resource *res;
134 int i; 120 int i;
135 121
136 /* IRQ priority: this table is good for i386 */ 122 /* IRQ priority: this table is good for i386 */
@@ -138,59 +124,57 @@ static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
138 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2 124 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
139 }; 125 };
140 126
141 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IRQ, idx); 127 res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
142 if (!pnp_res) { 128 if (res) {
143 dev_err(&dev->dev, "too many IRQ resources\n");
144 /* pretend we were successful so at least the manager won't try again */
145 return 1;
146 }
147
148 res = &pnp_res->res;
149
150 /* check if this resource has been manually set, if so skip */
151 if (!(res->flags & IORESOURCE_AUTO)) {
152 dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n", 129 dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
153 idx, (int) res->start, res->flags); 130 idx, (int) res->start, res->flags);
154 return 1; 131 return 0;
155 } 132 }
156 133
157 /* set the initial values */ 134 res = &local_res;
158 pnp_res->index = idx; 135 res->flags = rule->flags | IORESOURCE_AUTO;
159 res->flags |= rule->flags | IORESOURCE_IRQ; 136 res->start = -1;
160 res->flags &= ~IORESOURCE_UNSET; 137 res->end = -1;
161 138
162 if (bitmap_empty(rule->map, PNP_IRQ_NR)) { 139 if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
163 res->flags |= IORESOURCE_DISABLED; 140 res->flags |= IORESOURCE_DISABLED;
164 dev_dbg(&dev->dev, " irq %d disabled\n", idx); 141 dev_dbg(&dev->dev, " irq %d disabled\n", idx);
165 return 1; /* skip disabled resource requests */ 142 goto __add;
166 } 143 }
167 144
168 /* TBD: need check for >16 IRQ */ 145 /* TBD: need check for >16 IRQ */
169 res->start = find_next_bit(rule->map, PNP_IRQ_NR, 16); 146 res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
170 if (res->start < PNP_IRQ_NR) { 147 if (res->start < PNP_IRQ_NR) {
171 res->end = res->start; 148 res->end = res->start;
172 dev_dbg(&dev->dev, " assign irq %d %d\n", idx, 149 goto __add;
173 (int) res->start);
174 return 1;
175 } 150 }
176 for (i = 0; i < 16; i++) { 151 for (i = 0; i < 16; i++) {
177 if (test_bit(xtab[i], rule->map)) { 152 if (test_bit(xtab[i], rule->map.bits)) {
178 res->start = res->end = xtab[i]; 153 res->start = res->end = xtab[i];
179 if (pnp_check_irq(dev, res)) { 154 if (pnp_check_irq(dev, res))
180 dev_dbg(&dev->dev, " assign irq %d %d\n", idx, 155 goto __add;
181 (int) res->start);
182 return 1;
183 }
184 } 156 }
185 } 157 }
158
159 if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
160 res->start = -1;
161 res->end = -1;
162 res->flags |= IORESOURCE_DISABLED;
163 dev_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
164 goto __add;
165 }
166
186 dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx); 167 dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
168 return -EBUSY;
169
170__add:
171 pnp_add_irq_resource(dev, res->start, res->flags);
187 return 0; 172 return 0;
188} 173}
189 174
190static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx) 175static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
191{ 176{
192 struct pnp_resource *pnp_res; 177 struct resource *res, local_res;
193 struct resource *res;
194 int i; 178 int i;
195 179
196 /* DMA priority: this table is good for i386 */ 180 /* DMA priority: this table is good for i386 */
@@ -198,231 +182,99 @@ static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
198 1, 3, 5, 6, 7, 0, 2, 4 182 1, 3, 5, 6, 7, 0, 2, 4
199 }; 183 };
200 184
201 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_DMA, idx); 185 res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
202 if (!pnp_res) { 186 if (res) {
203 dev_err(&dev->dev, "too many DMA resources\n");
204 return;
205 }
206
207 res = &pnp_res->res;
208
209 /* check if this resource has been manually set, if so skip */
210 if (!(res->flags & IORESOURCE_AUTO)) {
211 dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n", 187 dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
212 idx, (int) res->start, res->flags); 188 idx, (int) res->start, res->flags);
213 return; 189 return 0;
214 } 190 }
215 191
216 /* set the initial values */ 192 res = &local_res;
217 pnp_res->index = idx; 193 res->flags = rule->flags | IORESOURCE_AUTO;
218 res->flags |= rule->flags | IORESOURCE_DMA; 194 res->start = -1;
219 res->flags &= ~IORESOURCE_UNSET; 195 res->end = -1;
220 196
221 for (i = 0; i < 8; i++) { 197 for (i = 0; i < 8; i++) {
222 if (rule->map & (1 << xtab[i])) { 198 if (rule->map & (1 << xtab[i])) {
223 res->start = res->end = xtab[i]; 199 res->start = res->end = xtab[i];
224 if (pnp_check_dma(dev, res)) { 200 if (pnp_check_dma(dev, res))
225 dev_dbg(&dev->dev, " assign dma %d %d\n", idx, 201 goto __add;
226 (int) res->start);
227 return;
228 }
229 } 202 }
230 } 203 }
231#ifdef MAX_DMA_CHANNELS 204#ifdef MAX_DMA_CHANNELS
232 res->start = res->end = MAX_DMA_CHANNELS; 205 res->start = res->end = MAX_DMA_CHANNELS;
233#endif 206#endif
234 res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED; 207 res->flags |= IORESOURCE_DISABLED;
235 dev_dbg(&dev->dev, " disable dma %d\n", idx); 208 dev_dbg(&dev->dev, " disable dma %d\n", idx);
236}
237
238void pnp_init_resource(struct resource *res)
239{
240 unsigned long type;
241
242 type = res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
243 IORESOURCE_IRQ | IORESOURCE_DMA);
244 209
245 res->name = NULL; 210__add:
246 res->flags = type | IORESOURCE_AUTO | IORESOURCE_UNSET; 211 pnp_add_dma_resource(dev, res->start, res->flags);
247 if (type == IORESOURCE_IRQ || type == IORESOURCE_DMA) { 212 return 0;
248 res->start = -1;
249 res->end = -1;
250 } else {
251 res->start = 0;
252 res->end = 0;
253 }
254} 213}
255 214
256/**
257 * pnp_init_resources - Resets a resource table to default values.
258 * @table: pointer to the desired resource table
259 */
260void pnp_init_resources(struct pnp_dev *dev) 215void pnp_init_resources(struct pnp_dev *dev)
261{ 216{
262 struct resource *res; 217 pnp_free_resources(dev);
263 int idx;
264
265 for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
266 res = &dev->res->irq[idx].res;
267 res->flags = IORESOURCE_IRQ;
268 pnp_init_resource(res);
269 }
270 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
271 res = &dev->res->dma[idx].res;
272 res->flags = IORESOURCE_DMA;
273 pnp_init_resource(res);
274 }
275 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
276 res = &dev->res->port[idx].res;
277 res->flags = IORESOURCE_IO;
278 pnp_init_resource(res);
279 }
280 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
281 res = &dev->res->mem[idx].res;
282 res->flags = IORESOURCE_MEM;
283 pnp_init_resource(res);
284 }
285} 218}
286 219
287/**
288 * pnp_clean_resources - clears resources that were not manually set
289 * @res: the resources to clean
290 */
291static void pnp_clean_resource_table(struct pnp_dev *dev) 220static void pnp_clean_resource_table(struct pnp_dev *dev)
292{ 221{
293 struct resource *res; 222 struct pnp_resource *pnp_res, *tmp;
294 int idx; 223
295 224 list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
296 for (idx = 0; idx < PNP_MAX_IRQ; idx++) { 225 if (pnp_res->res.flags & IORESOURCE_AUTO)
297 res = &dev->res->irq[idx].res; 226 pnp_free_resource(pnp_res);
298 if (res->flags & IORESOURCE_AUTO) {
299 res->flags = IORESOURCE_IRQ;
300 pnp_init_resource(res);
301 }
302 }
303 for (idx = 0; idx < PNP_MAX_DMA; idx++) {
304 res = &dev->res->dma[idx].res;
305 if (res->flags & IORESOURCE_AUTO) {
306 res->flags = IORESOURCE_DMA;
307 pnp_init_resource(res);
308 }
309 }
310 for (idx = 0; idx < PNP_MAX_PORT; idx++) {
311 res = &dev->res->port[idx].res;
312 if (res->flags & IORESOURCE_AUTO) {
313 res->flags = IORESOURCE_IO;
314 pnp_init_resource(res);
315 }
316 }
317 for (idx = 0; idx < PNP_MAX_MEM; idx++) {
318 res = &dev->res->mem[idx].res;
319 if (res->flags & IORESOURCE_AUTO) {
320 res->flags = IORESOURCE_MEM;
321 pnp_init_resource(res);
322 }
323 } 227 }
324} 228}
325 229
326/** 230/**
327 * pnp_assign_resources - assigns resources to the device based on the specified dependent number 231 * pnp_assign_resources - assigns resources to the device based on the specified dependent number
328 * @dev: pointer to the desired device 232 * @dev: pointer to the desired device
329 * @depnum: the dependent function number 233 * @set: the dependent function number
330 *
331 * Only set depnum to 0 if the device does not have dependent options.
332 */ 234 */
333static int pnp_assign_resources(struct pnp_dev *dev, int depnum) 235static int pnp_assign_resources(struct pnp_dev *dev, int set)
334{ 236{
335 struct pnp_port *port; 237 struct pnp_option *option;
336 struct pnp_mem *mem;
337 struct pnp_irq *irq;
338 struct pnp_dma *dma;
339 int nport = 0, nmem = 0, nirq = 0, ndma = 0; 238 int nport = 0, nmem = 0, nirq = 0, ndma = 0;
239 int ret = 0;
340 240
341 if (!pnp_can_configure(dev)) 241 dev_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
342 return -ENODEV;
343
344 dbg_pnp_show_resources(dev, "before pnp_assign_resources");
345 mutex_lock(&pnp_res_mutex); 242 mutex_lock(&pnp_res_mutex);
346 pnp_clean_resource_table(dev); 243 pnp_clean_resource_table(dev);
347 if (dev->independent) {
348 dev_dbg(&dev->dev, "assigning independent options\n");
349 port = dev->independent->port;
350 mem = dev->independent->mem;
351 irq = dev->independent->irq;
352 dma = dev->independent->dma;
353 while (port) {
354 if (!pnp_assign_port(dev, port, nport))
355 goto fail;
356 nport++;
357 port = port->next;
358 }
359 while (mem) {
360 if (!pnp_assign_mem(dev, mem, nmem))
361 goto fail;
362 nmem++;
363 mem = mem->next;
364 }
365 while (irq) {
366 if (!pnp_assign_irq(dev, irq, nirq))
367 goto fail;
368 nirq++;
369 irq = irq->next;
370 }
371 while (dma) {
372 pnp_assign_dma(dev, dma, ndma);
373 ndma++;
374 dma = dma->next;
375 }
376 }
377 244
378 if (depnum) { 245 list_for_each_entry(option, &dev->options, list) {
379 struct pnp_option *dep; 246 if (pnp_option_is_dependent(option) &&
380 int i; 247 pnp_option_set(option) != set)
381 248 continue;
382 dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum); 249
383 for (i = 1, dep = dev->dependent; i < depnum; 250 switch (option->type) {
384 i++, dep = dep->next) 251 case IORESOURCE_IO:
385 if (!dep) 252 ret = pnp_assign_port(dev, &option->u.port, nport++);
386 goto fail; 253 break;
387 port = dep->port; 254 case IORESOURCE_MEM:
388 mem = dep->mem; 255 ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
389 irq = dep->irq; 256 break;
390 dma = dep->dma; 257 case IORESOURCE_IRQ:
391 while (port) { 258 ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
392 if (!pnp_assign_port(dev, port, nport)) 259 break;
393 goto fail; 260 case IORESOURCE_DMA:
394 nport++; 261 ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
395 port = port->next; 262 break;
396 } 263 default:
397 while (mem) { 264 ret = -EINVAL;
398 if (!pnp_assign_mem(dev, mem, nmem)) 265 break;
399 goto fail;
400 nmem++;
401 mem = mem->next;
402 }
403 while (irq) {
404 if (!pnp_assign_irq(dev, irq, nirq))
405 goto fail;
406 nirq++;
407 irq = irq->next;
408 } 266 }
409 while (dma) { 267 if (ret < 0)
410 pnp_assign_dma(dev, dma, ndma); 268 break;
411 ndma++; 269 }
412 dma = dma->next;
413 }
414 } else if (dev->dependent)
415 goto fail;
416
417 mutex_unlock(&pnp_res_mutex);
418 dbg_pnp_show_resources(dev, "after pnp_assign_resources");
419 return 1;
420 270
421fail:
422 pnp_clean_resource_table(dev);
423 mutex_unlock(&pnp_res_mutex); 271 mutex_unlock(&pnp_res_mutex);
424 dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)"); 272 if (ret < 0) {
425 return 0; 273 dev_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
274 pnp_clean_resource_table(dev);
275 } else
276 dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
277 return ret;
426} 278}
427 279
428/** 280/**
@@ -431,29 +283,25 @@ fail:
431 */ 283 */
432int pnp_auto_config_dev(struct pnp_dev *dev) 284int pnp_auto_config_dev(struct pnp_dev *dev)
433{ 285{
434 struct pnp_option *dep; 286 int i, ret;
435 int i = 1;
436 287
437 if (!pnp_can_configure(dev)) { 288 if (!pnp_can_configure(dev)) {
438 dev_dbg(&dev->dev, "configuration not supported\n"); 289 dev_dbg(&dev->dev, "configuration not supported\n");
439 return -ENODEV; 290 return -ENODEV;
440 } 291 }
441 292
442 if (!dev->dependent) { 293 ret = pnp_assign_resources(dev, 0);
443 if (pnp_assign_resources(dev, 0)) 294 if (ret == 0)
295 return 0;
296
297 for (i = 1; i < dev->num_dependent_sets; i++) {
298 ret = pnp_assign_resources(dev, i);
299 if (ret == 0)
444 return 0; 300 return 0;
445 } else {
446 dep = dev->dependent;
447 do {
448 if (pnp_assign_resources(dev, i))
449 return 0;
450 dep = dep->next;
451 i++;
452 } while (dep);
453 } 301 }
454 302
455 dev_err(&dev->dev, "unable to assign resources\n"); 303 dev_err(&dev->dev, "unable to assign resources\n");
456 return -EBUSY; 304 return ret;
457} 305}
458 306
459/** 307/**
diff --git a/drivers/pnp/pnpacpi/core.c b/drivers/pnp/pnpacpi/core.c
index 50902773beaf..c1b9ea34977b 100644
--- a/drivers/pnp/pnpacpi/core.c
+++ b/drivers/pnp/pnpacpi/core.c
@@ -117,9 +117,7 @@ static int pnpacpi_suspend(struct pnp_dev *dev, pm_message_t state)
117{ 117{
118 int power_state; 118 int power_state;
119 119
120 power_state = acpi_pm_device_sleep_state(&dev->dev, 120 power_state = acpi_pm_device_sleep_state(&dev->dev, NULL);
121 device_may_wakeup(&dev->dev),
122 NULL);
123 if (power_state < 0) 121 if (power_state < 0)
124 power_state = (state.event == PM_EVENT_ON) ? 122 power_state = (state.event == PM_EVENT_ON) ?
125 ACPI_STATE_D0 : ACPI_STATE_D3; 123 ACPI_STATE_D0 : ACPI_STATE_D3;
diff --git a/drivers/pnp/pnpacpi/rsparser.c b/drivers/pnp/pnpacpi/rsparser.c
index 46c791adb894..d7e9f2152df0 100644
--- a/drivers/pnp/pnpacpi/rsparser.c
+++ b/drivers/pnp/pnpacpi/rsparser.c
@@ -3,6 +3,8 @@
3 * 3 *
4 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr> 4 * Copyright (c) 2004 Matthieu Castet <castet.matthieu@free.fr>
5 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com> 5 * Copyright (c) 2004 Li Shaohua <shaohua.li@intel.com>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 * 8 *
7 * This program is free software; you can redistribute it and/or modify it 9 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the 10 * under the terms of the GNU General Public License as published by the
@@ -98,8 +100,10 @@ static void pnpacpi_parse_allocated_irqresource(struct pnp_dev *dev,
98 int irq, flags; 100 int irq, flags;
99 int p, t; 101 int p, t;
100 102
101 if (!valid_IRQ(gsi)) 103 if (!valid_IRQ(gsi)) {
104 pnp_add_irq_resource(dev, gsi, IORESOURCE_DISABLED);
102 return; 105 return;
106 }
103 107
104 /* 108 /*
105 * in IO-APIC mode, use overrided attribute. Two reasons: 109 * in IO-APIC mode, use overrided attribute. Two reasons:
@@ -178,13 +182,68 @@ static void pnpacpi_parse_allocated_ioresource(struct pnp_dev *dev, u64 start,
178 u64 end = start + len - 1; 182 u64 end = start + len - 1;
179 183
180 if (io_decode == ACPI_DECODE_16) 184 if (io_decode == ACPI_DECODE_16)
181 flags |= PNP_PORT_FLAG_16BITADDR; 185 flags |= IORESOURCE_IO_16BIT_ADDR;
182 if (len == 0 || end >= 0x10003) 186 if (len == 0 || end >= 0x10003)
183 flags |= IORESOURCE_DISABLED; 187 flags |= IORESOURCE_DISABLED;
184 188
185 pnp_add_io_resource(dev, start, end, flags); 189 pnp_add_io_resource(dev, start, end, flags);
186} 190}
187 191
192/*
193 * Device CSRs that do not appear in PCI config space should be described
194 * via ACPI. This would normally be done with Address Space Descriptors
195 * marked as "consumer-only," but old versions of Windows and Linux ignore
196 * the producer/consumer flag, so HP invented a vendor-defined resource to
197 * describe the location and size of CSR space.
198 */
199static struct acpi_vendor_uuid hp_ccsr_uuid = {
200 .subtype = 2,
201 .data = { 0xf9, 0xad, 0xe9, 0x69, 0x4f, 0x92, 0x5f, 0xab, 0xf6, 0x4a,
202 0x24, 0xd2, 0x01, 0x37, 0x0e, 0xad },
203};
204
205static int vendor_resource_matches(struct pnp_dev *dev,
206 struct acpi_resource_vendor_typed *vendor,
207 struct acpi_vendor_uuid *match,
208 int expected_len)
209{
210 int uuid_len = sizeof(vendor->uuid);
211 u8 uuid_subtype = vendor->uuid_subtype;
212 u8 *uuid = vendor->uuid;
213 int actual_len;
214
215 /* byte_length includes uuid_subtype and uuid */
216 actual_len = vendor->byte_length - uuid_len - 1;
217
218 if (uuid_subtype == match->subtype &&
219 uuid_len == sizeof(match->data) &&
220 memcmp(uuid, match->data, uuid_len) == 0) {
221 if (expected_len && expected_len != actual_len) {
222 dev_err(&dev->dev, "wrong vendor descriptor size; "
223 "expected %d, found %d bytes\n",
224 expected_len, actual_len);
225 return 0;
226 }
227
228 return 1;
229 }
230
231 return 0;
232}
233
234static void pnpacpi_parse_allocated_vendor(struct pnp_dev *dev,
235 struct acpi_resource_vendor_typed *vendor)
236{
237 if (vendor_resource_matches(dev, vendor, &hp_ccsr_uuid, 16)) {
238 u64 start, length;
239
240 memcpy(&start, vendor->byte_data, sizeof(start));
241 memcpy(&length, vendor->byte_data + 8, sizeof(length));
242
243 pnp_add_mem_resource(dev, start, start + length - 1, 0);
244 }
245}
246
188static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev, 247static void pnpacpi_parse_allocated_memresource(struct pnp_dev *dev,
189 u64 start, u64 len, 248 u64 start, u64 len,
190 int write_protect) 249 int write_protect)
@@ -235,6 +294,7 @@ static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
235 struct acpi_resource_dma *dma; 294 struct acpi_resource_dma *dma;
236 struct acpi_resource_io *io; 295 struct acpi_resource_io *io;
237 struct acpi_resource_fixed_io *fixed_io; 296 struct acpi_resource_fixed_io *fixed_io;
297 struct acpi_resource_vendor_typed *vendor_typed;
238 struct acpi_resource_memory24 *memory24; 298 struct acpi_resource_memory24 *memory24;
239 struct acpi_resource_memory32 *memory32; 299 struct acpi_resource_memory32 *memory32;
240 struct acpi_resource_fixed_memory32 *fixed_memory32; 300 struct acpi_resource_fixed_memory32 *fixed_memory32;
@@ -248,24 +308,39 @@ static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
248 * _CRS, but some firmware violates this, so parse them all. 308 * _CRS, but some firmware violates this, so parse them all.
249 */ 309 */
250 irq = &res->data.irq; 310 irq = &res->data.irq;
251 for (i = 0; i < irq->interrupt_count; i++) { 311 if (irq->interrupt_count == 0)
252 pnpacpi_parse_allocated_irqresource(dev, 312 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
253 irq->interrupts[i], 313 else {
254 irq->triggering, 314 for (i = 0; i < irq->interrupt_count; i++) {
255 irq->polarity, 315 pnpacpi_parse_allocated_irqresource(dev,
256 irq->sharable); 316 irq->interrupts[i],
317 irq->triggering,
318 irq->polarity,
319 irq->sharable);
320 }
321
322 /*
323 * The IRQ encoder puts a single interrupt in each
324 * descriptor, so if a _CRS descriptor has more than
325 * one interrupt, we won't be able to re-encode it.
326 */
327 if (pnp_can_write(dev) && irq->interrupt_count > 1) {
328 dev_warn(&dev->dev, "multiple interrupts in "
329 "_CRS descriptor; configuration can't "
330 "be changed\n");
331 dev->capabilities &= ~PNP_WRITE;
332 }
257 } 333 }
258 break; 334 break;
259 335
260 case ACPI_RESOURCE_TYPE_DMA: 336 case ACPI_RESOURCE_TYPE_DMA:
261 dma = &res->data.dma; 337 dma = &res->data.dma;
262 if (dma->channel_count > 0) { 338 if (dma->channel_count > 0 && dma->channels[0] != (u8) -1)
263 flags = dma_flags(dma->type, dma->bus_master, 339 flags = dma_flags(dma->type, dma->bus_master,
264 dma->transfer); 340 dma->transfer);
265 if (dma->channels[0] == (u8) -1) 341 else
266 flags |= IORESOURCE_DISABLED; 342 flags = IORESOURCE_DISABLED;
267 pnp_add_dma_resource(dev, dma->channels[0], flags); 343 pnp_add_dma_resource(dev, dma->channels[0], flags);
268 }
269 break; 344 break;
270 345
271 case ACPI_RESOURCE_TYPE_IO: 346 case ACPI_RESOURCE_TYPE_IO:
@@ -289,6 +364,8 @@ static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
289 break; 364 break;
290 365
291 case ACPI_RESOURCE_TYPE_VENDOR: 366 case ACPI_RESOURCE_TYPE_VENDOR:
367 vendor_typed = &res->data.vendor_typed;
368 pnpacpi_parse_allocated_vendor(dev, vendor_typed);
292 break; 369 break;
293 370
294 case ACPI_RESOURCE_TYPE_END_TAG: 371 case ACPI_RESOURCE_TYPE_END_TAG:
@@ -331,12 +408,29 @@ static acpi_status pnpacpi_allocated_resource(struct acpi_resource *res,
331 if (extended_irq->producer_consumer == ACPI_PRODUCER) 408 if (extended_irq->producer_consumer == ACPI_PRODUCER)
332 return AE_OK; 409 return AE_OK;
333 410
334 for (i = 0; i < extended_irq->interrupt_count; i++) { 411 if (extended_irq->interrupt_count == 0)
335 pnpacpi_parse_allocated_irqresource(dev, 412 pnp_add_irq_resource(dev, 0, IORESOURCE_DISABLED);
336 extended_irq->interrupts[i], 413 else {
337 extended_irq->triggering, 414 for (i = 0; i < extended_irq->interrupt_count; i++) {
338 extended_irq->polarity, 415 pnpacpi_parse_allocated_irqresource(dev,
339 extended_irq->sharable); 416 extended_irq->interrupts[i],
417 extended_irq->triggering,
418 extended_irq->polarity,
419 extended_irq->sharable);
420 }
421
422 /*
423 * The IRQ encoder puts a single interrupt in each
424 * descriptor, so if a _CRS descriptor has more than
425 * one interrupt, we won't be able to re-encode it.
426 */
427 if (pnp_can_write(dev) &&
428 extended_irq->interrupt_count > 1) {
429 dev_warn(&dev->dev, "multiple interrupts in "
430 "_CRS descriptor; configuration can't "
431 "be changed\n");
432 dev->capabilities &= ~PNP_WRITE;
433 }
340 } 434 }
341 break; 435 break;
342 436
@@ -373,179 +467,147 @@ int pnpacpi_parse_allocated_resource(struct pnp_dev *dev)
373} 467}
374 468
375static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev, 469static __init void pnpacpi_parse_dma_option(struct pnp_dev *dev,
376 struct pnp_option *option, 470 unsigned int option_flags,
377 struct acpi_resource_dma *p) 471 struct acpi_resource_dma *p)
378{ 472{
379 int i; 473 int i;
380 struct pnp_dma *dma; 474 unsigned char map = 0, flags;
381 475
382 if (p->channel_count == 0) 476 if (p->channel_count == 0)
383 return; 477 return;
384 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
385 if (!dma)
386 return;
387 478
388 for (i = 0; i < p->channel_count; i++) 479 for (i = 0; i < p->channel_count; i++)
389 dma->map |= 1 << p->channels[i]; 480 map |= 1 << p->channels[i];
390
391 dma->flags = dma_flags(p->type, p->bus_master, p->transfer);
392 481
393 pnp_register_dma_resource(dev, option, dma); 482 flags = dma_flags(p->type, p->bus_master, p->transfer);
483 pnp_register_dma_resource(dev, option_flags, map, flags);
394} 484}
395 485
396static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev, 486static __init void pnpacpi_parse_irq_option(struct pnp_dev *dev,
397 struct pnp_option *option, 487 unsigned int option_flags,
398 struct acpi_resource_irq *p) 488 struct acpi_resource_irq *p)
399{ 489{
400 int i; 490 int i;
401 struct pnp_irq *irq; 491 pnp_irq_mask_t map;
492 unsigned char flags;
402 493
403 if (p->interrupt_count == 0) 494 if (p->interrupt_count == 0)
404 return; 495 return;
405 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
406 if (!irq)
407 return;
408 496
497 bitmap_zero(map.bits, PNP_IRQ_NR);
409 for (i = 0; i < p->interrupt_count; i++) 498 for (i = 0; i < p->interrupt_count; i++)
410 if (p->interrupts[i]) 499 if (p->interrupts[i])
411 __set_bit(p->interrupts[i], irq->map); 500 __set_bit(p->interrupts[i], map.bits);
412 irq->flags = irq_flags(p->triggering, p->polarity, p->sharable);
413 501
414 pnp_register_irq_resource(dev, option, irq); 502 flags = irq_flags(p->triggering, p->polarity, p->sharable);
503 pnp_register_irq_resource(dev, option_flags, &map, flags);
415} 504}
416 505
417static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev, 506static __init void pnpacpi_parse_ext_irq_option(struct pnp_dev *dev,
418 struct pnp_option *option, 507 unsigned int option_flags,
419 struct acpi_resource_extended_irq *p) 508 struct acpi_resource_extended_irq *p)
420{ 509{
421 int i; 510 int i;
422 struct pnp_irq *irq; 511 pnp_irq_mask_t map;
512 unsigned char flags;
423 513
424 if (p->interrupt_count == 0) 514 if (p->interrupt_count == 0)
425 return; 515 return;
426 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
427 if (!irq)
428 return;
429 516
430 for (i = 0; i < p->interrupt_count; i++) 517 bitmap_zero(map.bits, PNP_IRQ_NR);
431 if (p->interrupts[i]) 518 for (i = 0; i < p->interrupt_count; i++) {
432 __set_bit(p->interrupts[i], irq->map); 519 if (p->interrupts[i]) {
433 irq->flags = irq_flags(p->triggering, p->polarity, p->sharable); 520 if (p->interrupts[i] < PNP_IRQ_NR)
521 __set_bit(p->interrupts[i], map.bits);
522 else
523 dev_err(&dev->dev, "ignoring IRQ %d option "
524 "(too large for %d entry bitmap)\n",
525 p->interrupts[i], PNP_IRQ_NR);
526 }
527 }
434 528
435 pnp_register_irq_resource(dev, option, irq); 529 flags = irq_flags(p->triggering, p->polarity, p->sharable);
530 pnp_register_irq_resource(dev, option_flags, &map, flags);
436} 531}
437 532
438static __init void pnpacpi_parse_port_option(struct pnp_dev *dev, 533static __init void pnpacpi_parse_port_option(struct pnp_dev *dev,
439 struct pnp_option *option, 534 unsigned int option_flags,
440 struct acpi_resource_io *io) 535 struct acpi_resource_io *io)
441{ 536{
442 struct pnp_port *port; 537 unsigned char flags = 0;
443 538
444 if (io->address_length == 0) 539 if (io->address_length == 0)
445 return; 540 return;
446 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL); 541
447 if (!port) 542 if (io->io_decode == ACPI_DECODE_16)
448 return; 543 flags = IORESOURCE_IO_16BIT_ADDR;
449 port->min = io->minimum; 544 pnp_register_port_resource(dev, option_flags, io->minimum, io->maximum,
450 port->max = io->maximum; 545 io->alignment, io->address_length, flags);
451 port->align = io->alignment;
452 port->size = io->address_length;
453 port->flags = ACPI_DECODE_16 == io->io_decode ?
454 PNP_PORT_FLAG_16BITADDR : 0;
455 pnp_register_port_resource(dev, option, port);
456} 546}
457 547
458static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev, 548static __init void pnpacpi_parse_fixed_port_option(struct pnp_dev *dev,
459 struct pnp_option *option, 549 unsigned int option_flags,
460 struct acpi_resource_fixed_io *io) 550 struct acpi_resource_fixed_io *io)
461{ 551{
462 struct pnp_port *port;
463
464 if (io->address_length == 0) 552 if (io->address_length == 0)
465 return; 553 return;
466 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL); 554
467 if (!port) 555 pnp_register_port_resource(dev, option_flags, io->address, io->address,
468 return; 556 0, io->address_length, IORESOURCE_IO_FIXED);
469 port->min = port->max = io->address;
470 port->size = io->address_length;
471 port->align = 0;
472 port->flags = PNP_PORT_FLAG_FIXED;
473 pnp_register_port_resource(dev, option, port);
474} 557}
475 558
476static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev, 559static __init void pnpacpi_parse_mem24_option(struct pnp_dev *dev,
477 struct pnp_option *option, 560 unsigned int option_flags,
478 struct acpi_resource_memory24 *p) 561 struct acpi_resource_memory24 *p)
479{ 562{
480 struct pnp_mem *mem; 563 unsigned char flags = 0;
481 564
482 if (p->address_length == 0) 565 if (p->address_length == 0)
483 return; 566 return;
484 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
485 if (!mem)
486 return;
487 mem->min = p->minimum;
488 mem->max = p->maximum;
489 mem->align = p->alignment;
490 mem->size = p->address_length;
491
492 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
493 IORESOURCE_MEM_WRITEABLE : 0;
494 567
495 pnp_register_mem_resource(dev, option, mem); 568 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
569 flags = IORESOURCE_MEM_WRITEABLE;
570 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
571 p->alignment, p->address_length, flags);
496} 572}
497 573
498static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev, 574static __init void pnpacpi_parse_mem32_option(struct pnp_dev *dev,
499 struct pnp_option *option, 575 unsigned int option_flags,
500 struct acpi_resource_memory32 *p) 576 struct acpi_resource_memory32 *p)
501{ 577{
502 struct pnp_mem *mem; 578 unsigned char flags = 0;
503 579
504 if (p->address_length == 0) 580 if (p->address_length == 0)
505 return; 581 return;
506 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
507 if (!mem)
508 return;
509 mem->min = p->minimum;
510 mem->max = p->maximum;
511 mem->align = p->alignment;
512 mem->size = p->address_length;
513
514 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
515 IORESOURCE_MEM_WRITEABLE : 0;
516 582
517 pnp_register_mem_resource(dev, option, mem); 583 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
584 flags = IORESOURCE_MEM_WRITEABLE;
585 pnp_register_mem_resource(dev, option_flags, p->minimum, p->maximum,
586 p->alignment, p->address_length, flags);
518} 587}
519 588
520static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev, 589static __init void pnpacpi_parse_fixed_mem32_option(struct pnp_dev *dev,
521 struct pnp_option *option, 590 unsigned int option_flags,
522 struct acpi_resource_fixed_memory32 *p) 591 struct acpi_resource_fixed_memory32 *p)
523{ 592{
524 struct pnp_mem *mem; 593 unsigned char flags = 0;
525 594
526 if (p->address_length == 0) 595 if (p->address_length == 0)
527 return; 596 return;
528 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
529 if (!mem)
530 return;
531 mem->min = mem->max = p->address;
532 mem->size = p->address_length;
533 mem->align = 0;
534
535 mem->flags = (ACPI_READ_WRITE_MEMORY == p->write_protect) ?
536 IORESOURCE_MEM_WRITEABLE : 0;
537 597
538 pnp_register_mem_resource(dev, option, mem); 598 if (p->write_protect == ACPI_READ_WRITE_MEMORY)
599 flags = IORESOURCE_MEM_WRITEABLE;
600 pnp_register_mem_resource(dev, option_flags, p->address, p->address,
601 0, p->address_length, flags);
539} 602}
540 603
541static __init void pnpacpi_parse_address_option(struct pnp_dev *dev, 604static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
542 struct pnp_option *option, 605 unsigned int option_flags,
543 struct acpi_resource *r) 606 struct acpi_resource *r)
544{ 607{
545 struct acpi_resource_address64 addr, *p = &addr; 608 struct acpi_resource_address64 addr, *p = &addr;
546 acpi_status status; 609 acpi_status status;
547 struct pnp_mem *mem; 610 unsigned char flags = 0;
548 struct pnp_port *port;
549 611
550 status = acpi_resource_to_address64(r, p); 612 status = acpi_resource_to_address64(r, p);
551 if (!ACPI_SUCCESS(status)) { 613 if (!ACPI_SUCCESS(status)) {
@@ -558,49 +620,37 @@ static __init void pnpacpi_parse_address_option(struct pnp_dev *dev,
558 return; 620 return;
559 621
560 if (p->resource_type == ACPI_MEMORY_RANGE) { 622 if (p->resource_type == ACPI_MEMORY_RANGE) {
561 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); 623 if (p->info.mem.write_protect == ACPI_READ_WRITE_MEMORY)
562 if (!mem) 624 flags = IORESOURCE_MEM_WRITEABLE;
563 return; 625 pnp_register_mem_resource(dev, option_flags, p->minimum,
564 mem->min = mem->max = p->minimum; 626 p->minimum, 0, p->address_length,
565 mem->size = p->address_length; 627 flags);
566 mem->align = 0; 628 } else if (p->resource_type == ACPI_IO_RANGE)
567 mem->flags = (p->info.mem.write_protect == 629 pnp_register_port_resource(dev, option_flags, p->minimum,
568 ACPI_READ_WRITE_MEMORY) ? IORESOURCE_MEM_WRITEABLE 630 p->minimum, 0, p->address_length,
569 : 0; 631 IORESOURCE_IO_FIXED);
570 pnp_register_mem_resource(dev, option, mem);
571 } else if (p->resource_type == ACPI_IO_RANGE) {
572 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
573 if (!port)
574 return;
575 port->min = port->max = p->minimum;
576 port->size = p->address_length;
577 port->align = 0;
578 port->flags = PNP_PORT_FLAG_FIXED;
579 pnp_register_port_resource(dev, option, port);
580 }
581} 632}
582 633
583struct acpipnp_parse_option_s { 634struct acpipnp_parse_option_s {
584 struct pnp_option *option;
585 struct pnp_option *option_independent;
586 struct pnp_dev *dev; 635 struct pnp_dev *dev;
636 unsigned int option_flags;
587}; 637};
588 638
589static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res, 639static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
590 void *data) 640 void *data)
591{ 641{
592 int priority = 0; 642 int priority;
593 struct acpipnp_parse_option_s *parse_data = data; 643 struct acpipnp_parse_option_s *parse_data = data;
594 struct pnp_dev *dev = parse_data->dev; 644 struct pnp_dev *dev = parse_data->dev;
595 struct pnp_option *option = parse_data->option; 645 unsigned int option_flags = parse_data->option_flags;
596 646
597 switch (res->type) { 647 switch (res->type) {
598 case ACPI_RESOURCE_TYPE_IRQ: 648 case ACPI_RESOURCE_TYPE_IRQ:
599 pnpacpi_parse_irq_option(dev, option, &res->data.irq); 649 pnpacpi_parse_irq_option(dev, option_flags, &res->data.irq);
600 break; 650 break;
601 651
602 case ACPI_RESOURCE_TYPE_DMA: 652 case ACPI_RESOURCE_TYPE_DMA:
603 pnpacpi_parse_dma_option(dev, option, &res->data.dma); 653 pnpacpi_parse_dma_option(dev, option_flags, &res->data.dma);
604 break; 654 break;
605 655
606 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 656 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
@@ -620,31 +670,19 @@ static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
620 priority = PNP_RES_PRIORITY_INVALID; 670 priority = PNP_RES_PRIORITY_INVALID;
621 break; 671 break;
622 } 672 }
623 /* TBD: Consider performance/robustness bits */ 673 parse_data->option_flags = pnp_new_dependent_set(dev, priority);
624 option = pnp_register_dependent_option(dev, priority);
625 if (!option)
626 return AE_ERROR;
627 parse_data->option = option;
628 break; 674 break;
629 675
630 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 676 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
631 /*only one EndDependentFn is allowed */ 677 parse_data->option_flags = 0;
632 if (!parse_data->option_independent) {
633 dev_warn(&dev->dev, "more than one EndDependentFn "
634 "in _PRS\n");
635 return AE_ERROR;
636 }
637 parse_data->option = parse_data->option_independent;
638 parse_data->option_independent = NULL;
639 dev_dbg(&dev->dev, "end dependent options\n");
640 break; 678 break;
641 679
642 case ACPI_RESOURCE_TYPE_IO: 680 case ACPI_RESOURCE_TYPE_IO:
643 pnpacpi_parse_port_option(dev, option, &res->data.io); 681 pnpacpi_parse_port_option(dev, option_flags, &res->data.io);
644 break; 682 break;
645 683
646 case ACPI_RESOURCE_TYPE_FIXED_IO: 684 case ACPI_RESOURCE_TYPE_FIXED_IO:
647 pnpacpi_parse_fixed_port_option(dev, option, 685 pnpacpi_parse_fixed_port_option(dev, option_flags,
648 &res->data.fixed_io); 686 &res->data.fixed_io);
649 break; 687 break;
650 688
@@ -653,29 +691,31 @@ static __init acpi_status pnpacpi_option_resource(struct acpi_resource *res,
653 break; 691 break;
654 692
655 case ACPI_RESOURCE_TYPE_MEMORY24: 693 case ACPI_RESOURCE_TYPE_MEMORY24:
656 pnpacpi_parse_mem24_option(dev, option, &res->data.memory24); 694 pnpacpi_parse_mem24_option(dev, option_flags,
695 &res->data.memory24);
657 break; 696 break;
658 697
659 case ACPI_RESOURCE_TYPE_MEMORY32: 698 case ACPI_RESOURCE_TYPE_MEMORY32:
660 pnpacpi_parse_mem32_option(dev, option, &res->data.memory32); 699 pnpacpi_parse_mem32_option(dev, option_flags,
700 &res->data.memory32);
661 break; 701 break;
662 702
663 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: 703 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
664 pnpacpi_parse_fixed_mem32_option(dev, option, 704 pnpacpi_parse_fixed_mem32_option(dev, option_flags,
665 &res->data.fixed_memory32); 705 &res->data.fixed_memory32);
666 break; 706 break;
667 707
668 case ACPI_RESOURCE_TYPE_ADDRESS16: 708 case ACPI_RESOURCE_TYPE_ADDRESS16:
669 case ACPI_RESOURCE_TYPE_ADDRESS32: 709 case ACPI_RESOURCE_TYPE_ADDRESS32:
670 case ACPI_RESOURCE_TYPE_ADDRESS64: 710 case ACPI_RESOURCE_TYPE_ADDRESS64:
671 pnpacpi_parse_address_option(dev, option, res); 711 pnpacpi_parse_address_option(dev, option_flags, res);
672 break; 712 break;
673 713
674 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64: 714 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
675 break; 715 break;
676 716
677 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 717 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
678 pnpacpi_parse_ext_irq_option(dev, option, 718 pnpacpi_parse_ext_irq_option(dev, option_flags,
679 &res->data.extended_irq); 719 &res->data.extended_irq);
680 break; 720 break;
681 721
@@ -699,12 +739,9 @@ int __init pnpacpi_parse_resource_option_data(struct pnp_dev *dev)
699 739
700 dev_dbg(&dev->dev, "parse resource options\n"); 740 dev_dbg(&dev->dev, "parse resource options\n");
701 741
702 parse_data.option = pnp_register_independent_option(dev);
703 if (!parse_data.option)
704 return -ENOMEM;
705
706 parse_data.option_independent = parse_data.option;
707 parse_data.dev = dev; 742 parse_data.dev = dev;
743 parse_data.option_flags = 0;
744
708 status = acpi_walk_resources(handle, METHOD_NAME__PRS, 745 status = acpi_walk_resources(handle, METHOD_NAME__PRS,
709 pnpacpi_option_resource, &parse_data); 746 pnpacpi_option_resource, &parse_data);
710 747
@@ -806,6 +843,13 @@ static void pnpacpi_encode_irq(struct pnp_dev *dev,
806 struct acpi_resource_irq *irq = &resource->data.irq; 843 struct acpi_resource_irq *irq = &resource->data.irq;
807 int triggering, polarity, shareable; 844 int triggering, polarity, shareable;
808 845
846 if (!pnp_resource_enabled(p)) {
847 irq->interrupt_count = 0;
848 dev_dbg(&dev->dev, " encode irq (%s)\n",
849 p ? "disabled" : "missing");
850 return;
851 }
852
809 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable); 853 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
810 irq->triggering = triggering; 854 irq->triggering = triggering;
811 irq->polarity = polarity; 855 irq->polarity = polarity;
@@ -828,6 +872,13 @@ static void pnpacpi_encode_ext_irq(struct pnp_dev *dev,
828 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq; 872 struct acpi_resource_extended_irq *extended_irq = &resource->data.extended_irq;
829 int triggering, polarity, shareable; 873 int triggering, polarity, shareable;
830 874
875 if (!pnp_resource_enabled(p)) {
876 extended_irq->interrupt_count = 0;
877 dev_dbg(&dev->dev, " encode extended irq (%s)\n",
878 p ? "disabled" : "missing");
879 return;
880 }
881
831 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable); 882 decode_irq_flags(dev, p->flags, &triggering, &polarity, &shareable);
832 extended_irq->producer_consumer = ACPI_CONSUMER; 883 extended_irq->producer_consumer = ACPI_CONSUMER;
833 extended_irq->triggering = triggering; 884 extended_irq->triggering = triggering;
@@ -848,6 +899,13 @@ static void pnpacpi_encode_dma(struct pnp_dev *dev,
848{ 899{
849 struct acpi_resource_dma *dma = &resource->data.dma; 900 struct acpi_resource_dma *dma = &resource->data.dma;
850 901
902 if (!pnp_resource_enabled(p)) {
903 dma->channel_count = 0;
904 dev_dbg(&dev->dev, " encode dma (%s)\n",
905 p ? "disabled" : "missing");
906 return;
907 }
908
851 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */ 909 /* Note: pnp_assign_dma will copy pnp_dma->flags into p->flags */
852 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) { 910 switch (p->flags & IORESOURCE_DMA_SPEED_MASK) {
853 case IORESOURCE_DMA_TYPEA: 911 case IORESOURCE_DMA_TYPEA:
@@ -889,17 +947,21 @@ static void pnpacpi_encode_io(struct pnp_dev *dev,
889{ 947{
890 struct acpi_resource_io *io = &resource->data.io; 948 struct acpi_resource_io *io = &resource->data.io;
891 949
892 /* Note: pnp_assign_port will copy pnp_port->flags into p->flags */ 950 if (pnp_resource_enabled(p)) {
893 io->io_decode = (p->flags & PNP_PORT_FLAG_16BITADDR) ? 951 /* Note: pnp_assign_port copies pnp_port->flags into p->flags */
894 ACPI_DECODE_16 : ACPI_DECODE_10; 952 io->io_decode = (p->flags & IORESOURCE_IO_16BIT_ADDR) ?
895 io->minimum = p->start; 953 ACPI_DECODE_16 : ACPI_DECODE_10;
896 io->maximum = p->end; 954 io->minimum = p->start;
897 io->alignment = 0; /* Correct? */ 955 io->maximum = p->end;
898 io->address_length = p->end - p->start + 1; 956 io->alignment = 0; /* Correct? */
899 957 io->address_length = p->end - p->start + 1;
900 dev_dbg(&dev->dev, " encode io %#llx-%#llx decode %#x\n", 958 } else {
901 (unsigned long long) p->start, (unsigned long long) p->end, 959 io->minimum = 0;
902 io->io_decode); 960 io->address_length = 0;
961 }
962
963 dev_dbg(&dev->dev, " encode io %#x-%#x decode %#x\n", io->minimum,
964 io->minimum + io->address_length - 1, io->io_decode);
903} 965}
904 966
905static void pnpacpi_encode_fixed_io(struct pnp_dev *dev, 967static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
@@ -908,11 +970,16 @@ static void pnpacpi_encode_fixed_io(struct pnp_dev *dev,
908{ 970{
909 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io; 971 struct acpi_resource_fixed_io *fixed_io = &resource->data.fixed_io;
910 972
911 fixed_io->address = p->start; 973 if (pnp_resource_enabled(p)) {
912 fixed_io->address_length = p->end - p->start + 1; 974 fixed_io->address = p->start;
975 fixed_io->address_length = p->end - p->start + 1;
976 } else {
977 fixed_io->address = 0;
978 fixed_io->address_length = 0;
979 }
913 980
914 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n", 981 dev_dbg(&dev->dev, " encode fixed_io %#x-%#x\n", fixed_io->address,
915 (unsigned long long) p->start, (unsigned long long) p->end); 982 fixed_io->address + fixed_io->address_length - 1);
916} 983}
917 984
918static void pnpacpi_encode_mem24(struct pnp_dev *dev, 985static void pnpacpi_encode_mem24(struct pnp_dev *dev,
@@ -921,17 +988,22 @@ static void pnpacpi_encode_mem24(struct pnp_dev *dev,
921{ 988{
922 struct acpi_resource_memory24 *memory24 = &resource->data.memory24; 989 struct acpi_resource_memory24 *memory24 = &resource->data.memory24;
923 990
924 /* Note: pnp_assign_mem will copy pnp_mem->flags into p->flags */ 991 if (pnp_resource_enabled(p)) {
925 memory24->write_protect = 992 /* Note: pnp_assign_mem copies pnp_mem->flags into p->flags */
926 (p->flags & IORESOURCE_MEM_WRITEABLE) ? 993 memory24->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
927 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY; 994 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
928 memory24->minimum = p->start; 995 memory24->minimum = p->start;
929 memory24->maximum = p->end; 996 memory24->maximum = p->end;
930 memory24->alignment = 0; 997 memory24->alignment = 0;
931 memory24->address_length = p->end - p->start + 1; 998 memory24->address_length = p->end - p->start + 1;
932 999 } else {
933 dev_dbg(&dev->dev, " encode mem24 %#llx-%#llx write_protect %#x\n", 1000 memory24->minimum = 0;
934 (unsigned long long) p->start, (unsigned long long) p->end, 1001 memory24->address_length = 0;
1002 }
1003
1004 dev_dbg(&dev->dev, " encode mem24 %#x-%#x write_protect %#x\n",
1005 memory24->minimum,
1006 memory24->minimum + memory24->address_length - 1,
935 memory24->write_protect); 1007 memory24->write_protect);
936} 1008}
937 1009
@@ -941,16 +1013,21 @@ static void pnpacpi_encode_mem32(struct pnp_dev *dev,
941{ 1013{
942 struct acpi_resource_memory32 *memory32 = &resource->data.memory32; 1014 struct acpi_resource_memory32 *memory32 = &resource->data.memory32;
943 1015
944 memory32->write_protect = 1016 if (pnp_resource_enabled(p)) {
945 (p->flags & IORESOURCE_MEM_WRITEABLE) ? 1017 memory32->write_protect = p->flags & IORESOURCE_MEM_WRITEABLE ?
946 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY; 1018 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
947 memory32->minimum = p->start; 1019 memory32->minimum = p->start;
948 memory32->maximum = p->end; 1020 memory32->maximum = p->end;
949 memory32->alignment = 0; 1021 memory32->alignment = 0;
950 memory32->address_length = p->end - p->start + 1; 1022 memory32->address_length = p->end - p->start + 1;
1023 } else {
1024 memory32->minimum = 0;
1025 memory32->alignment = 0;
1026 }
951 1027
952 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx write_protect %#x\n", 1028 dev_dbg(&dev->dev, " encode mem32 %#x-%#x write_protect %#x\n",
953 (unsigned long long) p->start, (unsigned long long) p->end, 1029 memory32->minimum,
1030 memory32->minimum + memory32->address_length - 1,
954 memory32->write_protect); 1031 memory32->write_protect);
955} 1032}
956 1033
@@ -960,15 +1037,20 @@ static void pnpacpi_encode_fixed_mem32(struct pnp_dev *dev,
960{ 1037{
961 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32; 1038 struct acpi_resource_fixed_memory32 *fixed_memory32 = &resource->data.fixed_memory32;
962 1039
963 fixed_memory32->write_protect = 1040 if (pnp_resource_enabled(p)) {
964 (p->flags & IORESOURCE_MEM_WRITEABLE) ? 1041 fixed_memory32->write_protect =
965 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY; 1042 p->flags & IORESOURCE_MEM_WRITEABLE ?
966 fixed_memory32->address = p->start; 1043 ACPI_READ_WRITE_MEMORY : ACPI_READ_ONLY_MEMORY;
967 fixed_memory32->address_length = p->end - p->start + 1; 1044 fixed_memory32->address = p->start;
1045 fixed_memory32->address_length = p->end - p->start + 1;
1046 } else {
1047 fixed_memory32->address = 0;
1048 fixed_memory32->address_length = 0;
1049 }
968 1050
969 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx " 1051 dev_dbg(&dev->dev, " encode fixed_mem32 %#x-%#x write_protect %#x\n",
970 "write_protect %#x\n", 1052 fixed_memory32->address,
971 (unsigned long long) p->start, (unsigned long long) p->end, 1053 fixed_memory32->address + fixed_memory32->address_length - 1,
972 fixed_memory32->write_protect); 1054 fixed_memory32->write_protect);
973} 1055}
974 1056
diff --git a/drivers/pnp/pnpbios/rsparser.c b/drivers/pnp/pnpbios/rsparser.c
index 5ff9a4c0447e..ca567671379e 100644
--- a/drivers/pnp/pnpbios/rsparser.c
+++ b/drivers/pnp/pnpbios/rsparser.c
@@ -216,137 +216,116 @@ len_err:
216 216
217static __init void pnpbios_parse_mem_option(struct pnp_dev *dev, 217static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
218 unsigned char *p, int size, 218 unsigned char *p, int size,
219 struct pnp_option *option) 219 unsigned int option_flags)
220{ 220{
221 struct pnp_mem *mem; 221 resource_size_t min, max, align, len;
222 222 unsigned char flags;
223 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); 223
224 if (!mem) 224 min = ((p[5] << 8) | p[4]) << 8;
225 return; 225 max = ((p[7] << 8) | p[6]) << 8;
226 mem->min = ((p[5] << 8) | p[4]) << 8; 226 align = (p[9] << 8) | p[8];
227 mem->max = ((p[7] << 8) | p[6]) << 8; 227 len = ((p[11] << 8) | p[10]) << 8;
228 mem->align = (p[9] << 8) | p[8]; 228 flags = p[3];
229 mem->size = ((p[11] << 8) | p[10]) << 8; 229 pnp_register_mem_resource(dev, option_flags, min, max, align, len,
230 mem->flags = p[3]; 230 flags);
231 pnp_register_mem_resource(dev, option, mem);
232} 231}
233 232
234static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev, 233static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
235 unsigned char *p, int size, 234 unsigned char *p, int size,
236 struct pnp_option *option) 235 unsigned int option_flags)
237{ 236{
238 struct pnp_mem *mem; 237 resource_size_t min, max, align, len;
239 238 unsigned char flags;
240 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); 239
241 if (!mem) 240 min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
242 return; 241 max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
243 mem->min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4]; 242 align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
244 mem->max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8]; 243 len = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
245 mem->align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12]; 244 flags = p[3];
246 mem->size = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16]; 245 pnp_register_mem_resource(dev, option_flags, min, max, align, len,
247 mem->flags = p[3]; 246 flags);
248 pnp_register_mem_resource(dev, option, mem);
249} 247}
250 248
251static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev, 249static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
252 unsigned char *p, int size, 250 unsigned char *p, int size,
253 struct pnp_option *option) 251 unsigned int option_flags)
254{ 252{
255 struct pnp_mem *mem; 253 resource_size_t base, len;
256 254 unsigned char flags;
257 mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL); 255
258 if (!mem) 256 base = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
259 return; 257 len = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
260 mem->min = mem->max = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4]; 258 flags = p[3];
261 mem->size = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8]; 259 pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
262 mem->align = 0;
263 mem->flags = p[3];
264 pnp_register_mem_resource(dev, option, mem);
265} 260}
266 261
267static __init void pnpbios_parse_irq_option(struct pnp_dev *dev, 262static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
268 unsigned char *p, int size, 263 unsigned char *p, int size,
269 struct pnp_option *option) 264 unsigned int option_flags)
270{ 265{
271 struct pnp_irq *irq;
272 unsigned long bits; 266 unsigned long bits;
267 pnp_irq_mask_t map;
268 unsigned char flags = IORESOURCE_IRQ_HIGHEDGE;
273 269
274 irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
275 if (!irq)
276 return;
277 bits = (p[2] << 8) | p[1]; 270 bits = (p[2] << 8) | p[1];
278 bitmap_copy(irq->map, &bits, 16); 271
272 bitmap_zero(map.bits, PNP_IRQ_NR);
273 bitmap_copy(map.bits, &bits, 16);
274
279 if (size > 2) 275 if (size > 2)
280 irq->flags = p[3]; 276 flags = p[3];
281 else 277
282 irq->flags = IORESOURCE_IRQ_HIGHEDGE; 278 pnp_register_irq_resource(dev, option_flags, &map, flags);
283 pnp_register_irq_resource(dev, option, irq);
284} 279}
285 280
286static __init void pnpbios_parse_dma_option(struct pnp_dev *dev, 281static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
287 unsigned char *p, int size, 282 unsigned char *p, int size,
288 struct pnp_option *option) 283 unsigned int option_flags)
289{ 284{
290 struct pnp_dma *dma; 285 pnp_register_dma_resource(dev, option_flags, p[1], p[2]);
291
292 dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
293 if (!dma)
294 return;
295 dma->map = p[1];
296 dma->flags = p[2];
297 pnp_register_dma_resource(dev, option, dma);
298} 286}
299 287
300static __init void pnpbios_parse_port_option(struct pnp_dev *dev, 288static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
301 unsigned char *p, int size, 289 unsigned char *p, int size,
302 struct pnp_option *option) 290 unsigned int option_flags)
303{ 291{
304 struct pnp_port *port; 292 resource_size_t min, max, align, len;
305 293 unsigned char flags;
306 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL); 294
307 if (!port) 295 min = (p[3] << 8) | p[2];
308 return; 296 max = (p[5] << 8) | p[4];
309 port->min = (p[3] << 8) | p[2]; 297 align = p[6];
310 port->max = (p[5] << 8) | p[4]; 298 len = p[7];
311 port->align = p[6]; 299 flags = p[1] ? IORESOURCE_IO_16BIT_ADDR : 0;
312 port->size = p[7]; 300 pnp_register_port_resource(dev, option_flags, min, max, align, len,
313 port->flags = p[1] ? PNP_PORT_FLAG_16BITADDR : 0; 301 flags);
314 pnp_register_port_resource(dev, option, port);
315} 302}
316 303
317static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev, 304static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
318 unsigned char *p, int size, 305 unsigned char *p, int size,
319 struct pnp_option *option) 306 unsigned int option_flags)
320{ 307{
321 struct pnp_port *port; 308 resource_size_t base, len;
322 309
323 port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL); 310 base = (p[2] << 8) | p[1];
324 if (!port) 311 len = p[3];
325 return; 312 pnp_register_port_resource(dev, option_flags, base, base, 0, len,
326 port->min = port->max = (p[2] << 8) | p[1]; 313 IORESOURCE_IO_FIXED);
327 port->size = p[3];
328 port->align = 0;
329 port->flags = PNP_PORT_FLAG_FIXED;
330 pnp_register_port_resource(dev, option, port);
331} 314}
332 315
333static __init unsigned char * 316static __init unsigned char *
334pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end, 317pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
335 struct pnp_dev *dev) 318 struct pnp_dev *dev)
336{ 319{
337 unsigned int len, tag; 320 unsigned int len, tag;
338 int priority = 0; 321 int priority;
339 struct pnp_option *option, *option_independent; 322 unsigned int option_flags;
340 323
341 if (!p) 324 if (!p)
342 return NULL; 325 return NULL;
343 326
344 dev_dbg(&dev->dev, "parse resource options\n"); 327 dev_dbg(&dev->dev, "parse resource options\n");
345 328 option_flags = 0;
346 option_independent = option = pnp_register_independent_option(dev);
347 if (!option)
348 return NULL;
349
350 while ((char *)p < (char *)end) { 329 while ((char *)p < (char *)end) {
351 330
352 /* determine the type of tag */ 331 /* determine the type of tag */
@@ -363,37 +342,38 @@ pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
363 case LARGE_TAG_MEM: 342 case LARGE_TAG_MEM:
364 if (len != 9) 343 if (len != 9)
365 goto len_err; 344 goto len_err;
366 pnpbios_parse_mem_option(dev, p, len, option); 345 pnpbios_parse_mem_option(dev, p, len, option_flags);
367 break; 346 break;
368 347
369 case LARGE_TAG_MEM32: 348 case LARGE_TAG_MEM32:
370 if (len != 17) 349 if (len != 17)
371 goto len_err; 350 goto len_err;
372 pnpbios_parse_mem32_option(dev, p, len, option); 351 pnpbios_parse_mem32_option(dev, p, len, option_flags);
373 break; 352 break;
374 353
375 case LARGE_TAG_FIXEDMEM32: 354 case LARGE_TAG_FIXEDMEM32:
376 if (len != 9) 355 if (len != 9)
377 goto len_err; 356 goto len_err;
378 pnpbios_parse_fixed_mem32_option(dev, p, len, option); 357 pnpbios_parse_fixed_mem32_option(dev, p, len,
358 option_flags);
379 break; 359 break;
380 360
381 case SMALL_TAG_IRQ: 361 case SMALL_TAG_IRQ:
382 if (len < 2 || len > 3) 362 if (len < 2 || len > 3)
383 goto len_err; 363 goto len_err;
384 pnpbios_parse_irq_option(dev, p, len, option); 364 pnpbios_parse_irq_option(dev, p, len, option_flags);
385 break; 365 break;
386 366
387 case SMALL_TAG_DMA: 367 case SMALL_TAG_DMA:
388 if (len != 2) 368 if (len != 2)
389 goto len_err; 369 goto len_err;
390 pnpbios_parse_dma_option(dev, p, len, option); 370 pnpbios_parse_dma_option(dev, p, len, option_flags);
391 break; 371 break;
392 372
393 case SMALL_TAG_PORT: 373 case SMALL_TAG_PORT:
394 if (len != 7) 374 if (len != 7)
395 goto len_err; 375 goto len_err;
396 pnpbios_parse_port_option(dev, p, len, option); 376 pnpbios_parse_port_option(dev, p, len, option_flags);
397 break; 377 break;
398 378
399 case SMALL_TAG_VENDOR: 379 case SMALL_TAG_VENDOR:
@@ -403,28 +383,23 @@ pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
403 case SMALL_TAG_FIXEDPORT: 383 case SMALL_TAG_FIXEDPORT:
404 if (len != 3) 384 if (len != 3)
405 goto len_err; 385 goto len_err;
406 pnpbios_parse_fixed_port_option(dev, p, len, option); 386 pnpbios_parse_fixed_port_option(dev, p, len,
387 option_flags);
407 break; 388 break;
408 389
409 case SMALL_TAG_STARTDEP: 390 case SMALL_TAG_STARTDEP:
410 if (len > 1) 391 if (len > 1)
411 goto len_err; 392 goto len_err;
412 priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE; 393 priority = PNP_RES_PRIORITY_ACCEPTABLE;
413 if (len > 0) 394 if (len > 0)
414 priority = 0x100 | p[1]; 395 priority = p[1];
415 option = pnp_register_dependent_option(dev, priority); 396 option_flags = pnp_new_dependent_set(dev, priority);
416 if (!option)
417 return NULL;
418 break; 397 break;
419 398
420 case SMALL_TAG_ENDDEP: 399 case SMALL_TAG_ENDDEP:
421 if (len != 0) 400 if (len != 0)
422 goto len_err; 401 goto len_err;
423 if (option_independent == option) 402 option_flags = 0;
424 dev_warn(&dev->dev, "missing "
425 "SMALL_TAG_STARTDEP tag\n");
426 option = option_independent;
427 dev_dbg(&dev->dev, "end dependent options\n");
428 break; 403 break;
429 404
430 case SMALL_TAG_END: 405 case SMALL_TAG_END:
@@ -526,8 +501,16 @@ len_err:
526static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p, 501static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
527 struct resource *res) 502 struct resource *res)
528{ 503{
529 unsigned long base = res->start; 504 unsigned long base;
530 unsigned long len = res->end - res->start + 1; 505 unsigned long len;
506
507 if (pnp_resource_enabled(res)) {
508 base = res->start;
509 len = res->end - res->start + 1;
510 } else {
511 base = 0;
512 len = 0;
513 }
531 514
532 p[4] = (base >> 8) & 0xff; 515 p[4] = (base >> 8) & 0xff;
533 p[5] = ((base >> 8) >> 8) & 0xff; 516 p[5] = ((base >> 8) >> 8) & 0xff;
@@ -536,15 +519,22 @@ static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
536 p[10] = (len >> 8) & 0xff; 519 p[10] = (len >> 8) & 0xff;
537 p[11] = ((len >> 8) >> 8) & 0xff; 520 p[11] = ((len >> 8) >> 8) & 0xff;
538 521
539 dev_dbg(&dev->dev, " encode mem %#llx-%#llx\n", 522 dev_dbg(&dev->dev, " encode mem %#lx-%#lx\n", base, base + len - 1);
540 (unsigned long long) res->start, (unsigned long long) res->end);
541} 523}
542 524
543static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p, 525static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
544 struct resource *res) 526 struct resource *res)
545{ 527{
546 unsigned long base = res->start; 528 unsigned long base;
547 unsigned long len = res->end - res->start + 1; 529 unsigned long len;
530
531 if (pnp_resource_enabled(res)) {
532 base = res->start;
533 len = res->end - res->start + 1;
534 } else {
535 base = 0;
536 len = 0;
537 }
548 538
549 p[4] = base & 0xff; 539 p[4] = base & 0xff;
550 p[5] = (base >> 8) & 0xff; 540 p[5] = (base >> 8) & 0xff;
@@ -559,15 +549,22 @@ static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
559 p[18] = (len >> 16) & 0xff; 549 p[18] = (len >> 16) & 0xff;
560 p[19] = (len >> 24) & 0xff; 550 p[19] = (len >> 24) & 0xff;
561 551
562 dev_dbg(&dev->dev, " encode mem32 %#llx-%#llx\n", 552 dev_dbg(&dev->dev, " encode mem32 %#lx-%#lx\n", base, base + len - 1);
563 (unsigned long long) res->start, (unsigned long long) res->end);
564} 553}
565 554
566static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p, 555static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
567 struct resource *res) 556 struct resource *res)
568{ 557{
569 unsigned long base = res->start; 558 unsigned long base;
570 unsigned long len = res->end - res->start + 1; 559 unsigned long len;
560
561 if (pnp_resource_enabled(res)) {
562 base = res->start;
563 len = res->end - res->start + 1;
564 } else {
565 base = 0;
566 len = 0;
567 }
571 568
572 p[4] = base & 0xff; 569 p[4] = base & 0xff;
573 p[5] = (base >> 8) & 0xff; 570 p[5] = (base >> 8) & 0xff;
@@ -578,40 +575,54 @@ static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
578 p[10] = (len >> 16) & 0xff; 575 p[10] = (len >> 16) & 0xff;
579 p[11] = (len >> 24) & 0xff; 576 p[11] = (len >> 24) & 0xff;
580 577
581 dev_dbg(&dev->dev, " encode fixed_mem32 %#llx-%#llx\n", 578 dev_dbg(&dev->dev, " encode fixed_mem32 %#lx-%#lx\n", base,
582 (unsigned long long) res->start, (unsigned long long) res->end); 579 base + len - 1);
583} 580}
584 581
585static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p, 582static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
586 struct resource *res) 583 struct resource *res)
587{ 584{
588 unsigned long map = 0; 585 unsigned long map;
586
587 if (pnp_resource_enabled(res))
588 map = 1 << res->start;
589 else
590 map = 0;
589 591
590 map = 1 << res->start;
591 p[1] = map & 0xff; 592 p[1] = map & 0xff;
592 p[2] = (map >> 8) & 0xff; 593 p[2] = (map >> 8) & 0xff;
593 594
594 dev_dbg(&dev->dev, " encode irq %llu\n", 595 dev_dbg(&dev->dev, " encode irq mask %#lx\n", map);
595 (unsigned long long)res->start);
596} 596}
597 597
598static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p, 598static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
599 struct resource *res) 599 struct resource *res)
600{ 600{
601 unsigned long map = 0; 601 unsigned long map;
602
603 if (pnp_resource_enabled(res))
604 map = 1 << res->start;
605 else
606 map = 0;
602 607
603 map = 1 << res->start;
604 p[1] = map & 0xff; 608 p[1] = map & 0xff;
605 609
606 dev_dbg(&dev->dev, " encode dma %llu\n", 610 dev_dbg(&dev->dev, " encode dma mask %#lx\n", map);
607 (unsigned long long)res->start);
608} 611}
609 612
610static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p, 613static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
611 struct resource *res) 614 struct resource *res)
612{ 615{
613 unsigned long base = res->start; 616 unsigned long base;
614 unsigned long len = res->end - res->start + 1; 617 unsigned long len;
618
619 if (pnp_resource_enabled(res)) {
620 base = res->start;
621 len = res->end - res->start + 1;
622 } else {
623 base = 0;
624 len = 0;
625 }
615 626
616 p[2] = base & 0xff; 627 p[2] = base & 0xff;
617 p[3] = (base >> 8) & 0xff; 628 p[3] = (base >> 8) & 0xff;
@@ -619,8 +630,7 @@ static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
619 p[5] = (base >> 8) & 0xff; 630 p[5] = (base >> 8) & 0xff;
620 p[7] = len & 0xff; 631 p[7] = len & 0xff;
621 632
622 dev_dbg(&dev->dev, " encode io %#llx-%#llx\n", 633 dev_dbg(&dev->dev, " encode io %#lx-%#lx\n", base, base + len - 1);
623 (unsigned long long) res->start, (unsigned long long) res->end);
624} 634}
625 635
626static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p, 636static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
@@ -629,12 +639,20 @@ static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
629 unsigned long base = res->start; 639 unsigned long base = res->start;
630 unsigned long len = res->end - res->start + 1; 640 unsigned long len = res->end - res->start + 1;
631 641
642 if (pnp_resource_enabled(res)) {
643 base = res->start;
644 len = res->end - res->start + 1;
645 } else {
646 base = 0;
647 len = 0;
648 }
649
632 p[1] = base & 0xff; 650 p[1] = base & 0xff;
633 p[2] = (base >> 8) & 0xff; 651 p[2] = (base >> 8) & 0xff;
634 p[3] = len & 0xff; 652 p[3] = len & 0xff;
635 653
636 dev_dbg(&dev->dev, " encode fixed_io %#llx-%#llx\n", 654 dev_dbg(&dev->dev, " encode fixed_io %#lx-%#lx\n", base,
637 (unsigned long long) res->start, (unsigned long long) res->end); 655 base + len - 1);
638} 656}
639 657
640static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev 658static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
diff --git a/drivers/pnp/quirks.c b/drivers/pnp/quirks.c
index 1ff3bb585ab2..55f55ed72dc7 100644
--- a/drivers/pnp/quirks.c
+++ b/drivers/pnp/quirks.c
@@ -5,6 +5,8 @@
5 * when building up the resource structure for the first time. 5 * when building up the resource structure for the first time.
6 * 6 *
7 * Copyright (c) 2000 Peter Denison <peterd@pnd-pc.demon.co.uk> 7 * Copyright (c) 2000 Peter Denison <peterd@pnd-pc.demon.co.uk>
8 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
9 * Bjorn Helgaas <bjorn.helgaas@hp.com>
8 * 10 *
9 * Heavily based on PCI quirks handling which is 11 * Heavily based on PCI quirks handling which is
10 * 12 *
@@ -20,203 +22,207 @@
20#include <linux/kallsyms.h> 22#include <linux/kallsyms.h>
21#include "base.h" 23#include "base.h"
22 24
25static void quirk_awe32_add_ports(struct pnp_dev *dev,
26 struct pnp_option *option,
27 unsigned int offset)
28{
29 struct pnp_option *new_option;
30
31 new_option = kmalloc(sizeof(struct pnp_option), GFP_KERNEL);
32 if (!new_option) {
33 dev_err(&dev->dev, "couldn't add ioport region to option set "
34 "%d\n", pnp_option_set(option));
35 return;
36 }
37
38 *new_option = *option;
39 new_option->u.port.min += offset;
40 new_option->u.port.max += offset;
41 list_add(&new_option->list, &option->list);
42
43 dev_info(&dev->dev, "added ioport region %#llx-%#llx to set %d\n",
44 (unsigned long long) new_option->u.port.min,
45 (unsigned long long) new_option->u.port.max,
46 pnp_option_set(option));
47}
48
23static void quirk_awe32_resources(struct pnp_dev *dev) 49static void quirk_awe32_resources(struct pnp_dev *dev)
24{ 50{
25 struct pnp_port *port, *port2, *port3; 51 struct pnp_option *option;
26 struct pnp_option *res = dev->dependent; 52 unsigned int set = ~0;
27 53
28 /* 54 /*
29 * Unfortunately the isapnp_add_port_resource is too tightly bound 55 * Add two extra ioport regions (at offset 0x400 and 0x800 from the
30 * into the PnP discovery sequence, and cannot be used. Link in the 56 * one given) to every dependent option set.
31 * two extra ports (at offset 0x400 and 0x800 from the one given) by
32 * hand.
33 */ 57 */
34 for (; res; res = res->next) { 58 list_for_each_entry(option, &dev->options, list) {
35 port2 = pnp_alloc(sizeof(struct pnp_port)); 59 if (pnp_option_is_dependent(option) &&
36 if (!port2) 60 pnp_option_set(option) != set) {
37 return; 61 set = pnp_option_set(option);
38 port3 = pnp_alloc(sizeof(struct pnp_port)); 62 quirk_awe32_add_ports(dev, option, 0x800);
39 if (!port3) { 63 quirk_awe32_add_ports(dev, option, 0x400);
40 kfree(port2);
41 return;
42 } 64 }
43 port = res->port;
44 memcpy(port2, port, sizeof(struct pnp_port));
45 memcpy(port3, port, sizeof(struct pnp_port));
46 port->next = port2;
47 port2->next = port3;
48 port2->min += 0x400;
49 port2->max += 0x400;
50 port3->min += 0x800;
51 port3->max += 0x800;
52 dev_info(&dev->dev,
53 "AWE32 quirk - added ioports 0x%lx and 0x%lx\n",
54 (unsigned long)port2->min,
55 (unsigned long)port3->min);
56 } 65 }
57} 66}
58 67
59static void quirk_cmi8330_resources(struct pnp_dev *dev) 68static void quirk_cmi8330_resources(struct pnp_dev *dev)
60{ 69{
61 struct pnp_option *res = dev->dependent; 70 struct pnp_option *option;
62 unsigned long tmp; 71 struct pnp_irq *irq;
63 72 struct pnp_dma *dma;
64 for (; res; res = res->next) {
65
66 struct pnp_irq *irq;
67 struct pnp_dma *dma;
68 73
69 for (irq = res->irq; irq; irq = irq->next) { // Valid irqs are 5, 7, 10 74 list_for_each_entry(option, &dev->options, list) {
70 tmp = 0x04A0; 75 if (!pnp_option_is_dependent(option))
71 bitmap_copy(irq->map, &tmp, 16); // 0000 0100 1010 0000 76 continue;
72 }
73 77
74 for (dma = res->dma; dma; dma = dma->next) // Valid 8bit dma channels are 1,3 78 if (option->type == IORESOURCE_IRQ) {
79 irq = &option->u.irq;
80 bitmap_zero(irq->map.bits, PNP_IRQ_NR);
81 __set_bit(5, irq->map.bits);
82 __set_bit(7, irq->map.bits);
83 __set_bit(10, irq->map.bits);
84 dev_info(&dev->dev, "set possible IRQs in "
85 "option set %d to 5, 7, 10\n",
86 pnp_option_set(option));
87 } else if (option->type == IORESOURCE_DMA) {
88 dma = &option->u.dma;
75 if ((dma->flags & IORESOURCE_DMA_TYPE_MASK) == 89 if ((dma->flags & IORESOURCE_DMA_TYPE_MASK) ==
76 IORESOURCE_DMA_8BIT) 90 IORESOURCE_DMA_8BIT &&
77 dma->map = 0x000A; 91 dma->map != 0x0A) {
92 dev_info(&dev->dev, "changing possible "
93 "DMA channel mask in option set %d "
94 "from %#02x to 0x0A (1, 3)\n",
95 pnp_option_set(option), dma->map);
96 dma->map = 0x0A;
97 }
98 }
78 } 99 }
79 dev_info(&dev->dev, "CMI8330 quirk - forced possible IRQs to 5, 7, 10 "
80 "and DMA channels to 1, 3\n");
81} 100}
82 101
83static void quirk_sb16audio_resources(struct pnp_dev *dev) 102static void quirk_sb16audio_resources(struct pnp_dev *dev)
84{ 103{
104 struct pnp_option *option;
105 unsigned int prev_option_flags = ~0, n = 0;
85 struct pnp_port *port; 106 struct pnp_port *port;
86 struct pnp_option *res = dev->dependent;
87 int changed = 0;
88 107
89 /* 108 /*
90 * The default range on the mpu port for these devices is 0x388-0x388. 109 * The default range on the OPL port for these devices is 0x388-0x388.
91 * Here we increase that range so that two such cards can be 110 * Here we increase that range so that two such cards can be
92 * auto-configured. 111 * auto-configured.
93 */ 112 */
113 list_for_each_entry(option, &dev->options, list) {
114 if (prev_option_flags != option->flags) {
115 prev_option_flags = option->flags;
116 n = 0;
117 }
94 118
95 for (; res; res = res->next) { 119 if (pnp_option_is_dependent(option) &&
96 port = res->port; 120 option->type == IORESOURCE_IO) {
97 if (!port) 121 n++;
98 continue; 122 port = &option->u.port;
99 port = port->next; 123 if (n == 3 && port->min == port->max) {
100 if (!port) 124 port->max += 0x70;
101 continue; 125 dev_info(&dev->dev, "increased option port "
102 port = port->next; 126 "range from %#llx-%#llx to "
103 if (!port) 127 "%#llx-%#llx\n",
104 continue; 128 (unsigned long long) port->min,
105 if (port->min != port->max) 129 (unsigned long long) port->min,
106 continue; 130 (unsigned long long) port->min,
107 port->max += 0x70; 131 (unsigned long long) port->max);
108 changed = 1; 132 }
133 }
109 } 134 }
110 if (changed)
111 dev_info(&dev->dev, "SB audio device quirk - increased port range\n");
112} 135}
113 136
114static struct pnp_option *quirk_isapnp_mpu_options(struct pnp_dev *dev) 137static struct pnp_option *pnp_clone_dependent_set(struct pnp_dev *dev,
138 unsigned int set)
115{ 139{
116 struct pnp_option *head = NULL; 140 struct pnp_option *tail = NULL, *first_new_option = NULL;
117 struct pnp_option *prev = NULL; 141 struct pnp_option *option, *new_option;
118 struct pnp_option *res; 142 unsigned int flags;
119
120 /*
121 * Build a functional IRQ-less variant of each MPU option.
122 */
123
124 for (res = dev->dependent; res; res = res->next) {
125 struct pnp_option *curr;
126 struct pnp_port *port;
127 struct pnp_port *copy;
128 143
129 port = res->port; 144 list_for_each_entry(option, &dev->options, list) {
130 if (!port || !res->irq) 145 if (pnp_option_is_dependent(option))
131 continue; 146 tail = option;
147 }
148 if (!tail) {
149 dev_err(&dev->dev, "no dependent option sets\n");
150 return NULL;
151 }
132 152
133 copy = pnp_alloc(sizeof *copy); 153 flags = pnp_new_dependent_set(dev, PNP_RES_PRIORITY_FUNCTIONAL);
134 if (!copy) 154 list_for_each_entry(option, &dev->options, list) {
135 break; 155 if (pnp_option_is_dependent(option) &&
156 pnp_option_set(option) == set) {
157 new_option = kmalloc(sizeof(struct pnp_option),
158 GFP_KERNEL);
159 if (!new_option) {
160 dev_err(&dev->dev, "couldn't clone dependent "
161 "set %d\n", set);
162 return NULL;
163 }
136 164
137 copy->min = port->min; 165 *new_option = *option;
138 copy->max = port->max; 166 new_option->flags = flags;
139 copy->align = port->align; 167 if (!first_new_option)
140 copy->size = port->size; 168 first_new_option = new_option;
141 copy->flags = port->flags;
142 169
143 curr = pnp_build_option(PNP_RES_PRIORITY_FUNCTIONAL); 170 list_add(&new_option->list, &tail->list);
144 if (!curr) { 171 tail = new_option;
145 kfree(copy);
146 break;
147 } 172 }
148 curr->port = copy;
149
150 if (prev)
151 prev->next = curr;
152 else
153 head = curr;
154 prev = curr;
155 } 173 }
156 if (head)
157 dev_info(&dev->dev, "adding IRQ-less MPU options\n");
158 174
159 return head; 175 return first_new_option;
160} 176}
161 177
162static void quirk_ad1815_mpu_resources(struct pnp_dev *dev) 178
179static void quirk_add_irq_optional_dependent_sets(struct pnp_dev *dev)
163{ 180{
164 struct pnp_option *res; 181 struct pnp_option *new_option;
182 unsigned int num_sets, i, set;
165 struct pnp_irq *irq; 183 struct pnp_irq *irq;
166 184
167 /* 185 num_sets = dev->num_dependent_sets;
168 * Distribute the independent IRQ over the dependent options 186 for (i = 0; i < num_sets; i++) {
169 */ 187 new_option = pnp_clone_dependent_set(dev, i);
170 188 if (!new_option)
171 res = dev->independent; 189 return;
172 if (!res)
173 return;
174
175 irq = res->irq;
176 if (!irq || irq->next)
177 return;
178
179 res = dev->dependent;
180 if (!res)
181 return;
182
183 while (1) {
184 struct pnp_irq *copy;
185
186 copy = pnp_alloc(sizeof *copy);
187 if (!copy)
188 break;
189
190 memcpy(copy->map, irq->map, sizeof copy->map);
191 copy->flags = irq->flags;
192 190
193 copy->next = res->irq; /* Yes, this is NULL */ 191 set = pnp_option_set(new_option);
194 res->irq = copy; 192 while (new_option && pnp_option_set(new_option) == set) {
193 if (new_option->type == IORESOURCE_IRQ) {
194 irq = &new_option->u.irq;
195 irq->flags |= IORESOURCE_IRQ_OPTIONAL;
196 }
197 dbg_pnp_show_option(dev, new_option);
198 new_option = list_entry(new_option->list.next,
199 struct pnp_option, list);
200 }
195 201
196 if (!res->next) 202 dev_info(&dev->dev, "added dependent option set %d (same as "
197 break; 203 "set %d except IRQ optional)\n", set, i);
198 res = res->next;
199 } 204 }
200 kfree(irq);
201
202 res->next = quirk_isapnp_mpu_options(dev);
203
204 res = dev->independent;
205 res->irq = NULL;
206} 205}
207 206
208static void quirk_isapnp_mpu_resources(struct pnp_dev *dev) 207static void quirk_ad1815_mpu_resources(struct pnp_dev *dev)
209{ 208{
210 struct pnp_option *res; 209 struct pnp_option *option;
210 struct pnp_irq *irq = NULL;
211 unsigned int independent_irqs = 0;
212
213 list_for_each_entry(option, &dev->options, list) {
214 if (option->type == IORESOURCE_IRQ &&
215 !pnp_option_is_dependent(option)) {
216 independent_irqs++;
217 irq = &option->u.irq;
218 }
219 }
211 220
212 res = dev->dependent; 221 if (independent_irqs != 1)
213 if (!res)
214 return; 222 return;
215 223
216 while (res->next) 224 irq->flags |= IORESOURCE_IRQ_OPTIONAL;
217 res = res->next; 225 dev_info(&dev->dev, "made independent IRQ optional\n");
218
219 res->next = quirk_isapnp_mpu_options(dev);
220} 226}
221 227
222#include <linux/pci.h> 228#include <linux/pci.h>
@@ -248,8 +254,7 @@ static void quirk_system_pci_resources(struct pnp_dev *dev)
248 for (j = 0; 254 for (j = 0;
249 (res = pnp_get_resource(dev, IORESOURCE_MEM, j)); 255 (res = pnp_get_resource(dev, IORESOURCE_MEM, j));
250 j++) { 256 j++) {
251 if (res->flags & IORESOURCE_UNSET || 257 if (res->start == 0 && res->end == 0)
252 (res->start == 0 && res->end == 0))
253 continue; 258 continue;
254 259
255 pnp_start = res->start; 260 pnp_start = res->start;
@@ -312,10 +317,10 @@ static struct pnp_fixup pnp_fixups[] = {
312 {"CTL0043", quirk_sb16audio_resources}, 317 {"CTL0043", quirk_sb16audio_resources},
313 {"CTL0044", quirk_sb16audio_resources}, 318 {"CTL0044", quirk_sb16audio_resources},
314 {"CTL0045", quirk_sb16audio_resources}, 319 {"CTL0045", quirk_sb16audio_resources},
315 /* Add IRQ-less MPU options */ 320 /* Add IRQ-optional MPU options */
316 {"ADS7151", quirk_ad1815_mpu_resources}, 321 {"ADS7151", quirk_ad1815_mpu_resources},
317 {"ADS7181", quirk_isapnp_mpu_resources}, 322 {"ADS7181", quirk_add_irq_optional_dependent_sets},
318 {"AZT0002", quirk_isapnp_mpu_resources}, 323 {"AZT0002", quirk_add_irq_optional_dependent_sets},
319 /* PnP resources that might overlap PCI BARs */ 324 /* PnP resources that might overlap PCI BARs */
320 {"PNP0c01", quirk_system_pci_resources}, 325 {"PNP0c01", quirk_system_pci_resources},
321 {"PNP0c02", quirk_system_pci_resources}, 326 {"PNP0c02", quirk_system_pci_resources},
diff --git a/drivers/pnp/resource.c b/drivers/pnp/resource.c
index 390b50096e30..4cfe3a1efdfb 100644
--- a/drivers/pnp/resource.c
+++ b/drivers/pnp/resource.c
@@ -3,6 +3,8 @@
3 * 3 *
4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz> 4 * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com> 5 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
6 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
7 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 */ 8 */
7 9
8#include <linux/module.h> 10#include <linux/module.h>
@@ -28,201 +30,121 @@ static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some
28 * option registration 30 * option registration
29 */ 31 */
30 32
31struct pnp_option *pnp_build_option(int priority) 33struct pnp_option *pnp_build_option(struct pnp_dev *dev, unsigned long type,
34 unsigned int option_flags)
32{ 35{
33 struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option)); 36 struct pnp_option *option;
34 37
38 option = kzalloc(sizeof(struct pnp_option), GFP_KERNEL);
35 if (!option) 39 if (!option)
36 return NULL; 40 return NULL;
37 41
38 option->priority = priority & 0xff; 42 option->flags = option_flags;
39 /* make sure the priority is valid */ 43 option->type = type;
40 if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
41 option->priority = PNP_RES_PRIORITY_INVALID;
42
43 return option;
44}
45
46struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
47{
48 struct pnp_option *option;
49
50 option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
51
52 /* this should never happen but if it does we'll try to continue */
53 if (dev->independent)
54 dev_err(&dev->dev, "independent resource already registered\n");
55 dev->independent = option;
56 44
57 dev_dbg(&dev->dev, "new independent option\n"); 45 list_add_tail(&option->list, &dev->options);
58 return option; 46 return option;
59} 47}
60 48
61struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev, 49int pnp_register_irq_resource(struct pnp_dev *dev, unsigned int option_flags,
62 int priority) 50 pnp_irq_mask_t *map, unsigned char flags)
63{ 51{
64 struct pnp_option *option; 52 struct pnp_option *option;
53 struct pnp_irq *irq;
65 54
66 option = pnp_build_option(priority); 55 option = pnp_build_option(dev, IORESOURCE_IRQ, option_flags);
67 56 if (!option)
68 if (dev->dependent) { 57 return -ENOMEM;
69 struct pnp_option *parent = dev->dependent;
70 while (parent->next)
71 parent = parent->next;
72 parent->next = option;
73 } else
74 dev->dependent = option;
75
76 dev_dbg(&dev->dev, "new dependent option (priority %#x)\n", priority);
77 return option;
78}
79
80int pnp_register_irq_resource(struct pnp_dev *dev, struct pnp_option *option,
81 struct pnp_irq *data)
82{
83 struct pnp_irq *ptr;
84#ifdef DEBUG
85 char buf[PNP_IRQ_NR]; /* hex-encoded, so this is overkill but safe */
86#endif
87 58
88 ptr = option->irq; 59 irq = &option->u.irq;
89 while (ptr && ptr->next) 60 irq->map = *map;
90 ptr = ptr->next; 61 irq->flags = flags;
91 if (ptr)
92 ptr->next = data;
93 else
94 option->irq = data;
95 62
96#ifdef CONFIG_PCI 63#ifdef CONFIG_PCI
97 { 64 {
98 int i; 65 int i;
99 66
100 for (i = 0; i < 16; i++) 67 for (i = 0; i < 16; i++)
101 if (test_bit(i, data->map)) 68 if (test_bit(i, irq->map.bits))
102 pcibios_penalize_isa_irq(i, 0); 69 pcibios_penalize_isa_irq(i, 0);
103 } 70 }
104#endif 71#endif
105 72
106#ifdef DEBUG 73 dbg_pnp_show_option(dev, option);
107 bitmap_scnprintf(buf, sizeof(buf), data->map, PNP_IRQ_NR);
108 dev_dbg(&dev->dev, " irq bitmask %s flags %#x\n", buf,
109 data->flags);
110#endif
111 return 0; 74 return 0;
112} 75}
113 76
114int pnp_register_dma_resource(struct pnp_dev *dev, struct pnp_option *option, 77int pnp_register_dma_resource(struct pnp_dev *dev, unsigned int option_flags,
115 struct pnp_dma *data) 78 unsigned char map, unsigned char flags)
116{ 79{
117 struct pnp_dma *ptr; 80 struct pnp_option *option;
118 81 struct pnp_dma *dma;
119 ptr = option->dma;
120 while (ptr && ptr->next)
121 ptr = ptr->next;
122 if (ptr)
123 ptr->next = data;
124 else
125 option->dma = data;
126
127 dev_dbg(&dev->dev, " dma bitmask %#x flags %#x\n", data->map,
128 data->flags);
129 return 0;
130}
131 82
132int pnp_register_port_resource(struct pnp_dev *dev, struct pnp_option *option, 83 option = pnp_build_option(dev, IORESOURCE_DMA, option_flags);
133 struct pnp_port *data) 84 if (!option)
134{ 85 return -ENOMEM;
135 struct pnp_port *ptr;
136
137 ptr = option->port;
138 while (ptr && ptr->next)
139 ptr = ptr->next;
140 if (ptr)
141 ptr->next = data;
142 else
143 option->port = data;
144
145 dev_dbg(&dev->dev, " io "
146 "min %#x max %#x align %d size %d flags %#x\n",
147 data->min, data->max, data->align, data->size, data->flags);
148 return 0;
149}
150 86
151int pnp_register_mem_resource(struct pnp_dev *dev, struct pnp_option *option, 87 dma = &option->u.dma;
152 struct pnp_mem *data) 88 dma->map = map;
153{ 89 dma->flags = flags;
154 struct pnp_mem *ptr; 90
155 91 dbg_pnp_show_option(dev, option);
156 ptr = option->mem;
157 while (ptr && ptr->next)
158 ptr = ptr->next;
159 if (ptr)
160 ptr->next = data;
161 else
162 option->mem = data;
163
164 dev_dbg(&dev->dev, " mem "
165 "min %#x max %#x align %d size %d flags %#x\n",
166 data->min, data->max, data->align, data->size, data->flags);
167 return 0; 92 return 0;
168} 93}
169 94
170static void pnp_free_port(struct pnp_port *port) 95int pnp_register_port_resource(struct pnp_dev *dev, unsigned int option_flags,
96 resource_size_t min, resource_size_t max,
97 resource_size_t align, resource_size_t size,
98 unsigned char flags)
171{ 99{
172 struct pnp_port *next; 100 struct pnp_option *option;
101 struct pnp_port *port;
173 102
174 while (port) { 103 option = pnp_build_option(dev, IORESOURCE_IO, option_flags);
175 next = port->next; 104 if (!option)
176 kfree(port); 105 return -ENOMEM;
177 port = next;
178 }
179}
180 106
181static void pnp_free_irq(struct pnp_irq *irq) 107 port = &option->u.port;
182{ 108 port->min = min;
183 struct pnp_irq *next; 109 port->max = max;
110 port->align = align;
111 port->size = size;
112 port->flags = flags;
184 113
185 while (irq) { 114 dbg_pnp_show_option(dev, option);
186 next = irq->next; 115 return 0;
187 kfree(irq);
188 irq = next;
189 }
190} 116}
191 117
192static void pnp_free_dma(struct pnp_dma *dma) 118int pnp_register_mem_resource(struct pnp_dev *dev, unsigned int option_flags,
119 resource_size_t min, resource_size_t max,
120 resource_size_t align, resource_size_t size,
121 unsigned char flags)
193{ 122{
194 struct pnp_dma *next; 123 struct pnp_option *option;
124 struct pnp_mem *mem;
195 125
196 while (dma) { 126 option = pnp_build_option(dev, IORESOURCE_MEM, option_flags);
197 next = dma->next; 127 if (!option)
198 kfree(dma); 128 return -ENOMEM;
199 dma = next;
200 }
201}
202 129
203static void pnp_free_mem(struct pnp_mem *mem) 130 mem = &option->u.mem;
204{ 131 mem->min = min;
205 struct pnp_mem *next; 132 mem->max = max;
133 mem->align = align;
134 mem->size = size;
135 mem->flags = flags;
206 136
207 while (mem) { 137 dbg_pnp_show_option(dev, option);
208 next = mem->next; 138 return 0;
209 kfree(mem);
210 mem = next;
211 }
212} 139}
213 140
214void pnp_free_option(struct pnp_option *option) 141void pnp_free_options(struct pnp_dev *dev)
215{ 142{
216 struct pnp_option *next; 143 struct pnp_option *option, *tmp;
217 144
218 while (option) { 145 list_for_each_entry_safe(option, tmp, &dev->options, list) {
219 next = option->next; 146 list_del(&option->list);
220 pnp_free_port(option->port);
221 pnp_free_irq(option->irq);
222 pnp_free_dma(option->dma);
223 pnp_free_mem(option->mem);
224 kfree(option); 147 kfree(option);
225 option = next;
226 } 148 }
227} 149}
228 150
@@ -237,7 +159,7 @@ void pnp_free_option(struct pnp_option *option)
237 !((*(enda) < *(startb)) || (*(endb) < *(starta))) 159 !((*(enda) < *(startb)) || (*(endb) < *(starta)))
238 160
239#define cannot_compare(flags) \ 161#define cannot_compare(flags) \
240((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED)) 162((flags) & IORESOURCE_DISABLED)
241 163
242int pnp_check_port(struct pnp_dev *dev, struct resource *res) 164int pnp_check_port(struct pnp_dev *dev, struct resource *res)
243{ 165{
@@ -364,6 +286,61 @@ static irqreturn_t pnp_test_handler(int irq, void *dev_id)
364 return IRQ_HANDLED; 286 return IRQ_HANDLED;
365} 287}
366 288
289#ifdef CONFIG_PCI
290static int pci_dev_uses_irq(struct pnp_dev *pnp, struct pci_dev *pci,
291 unsigned int irq)
292{
293 u32 class;
294 u8 progif;
295
296 if (pci->irq == irq) {
297 dev_dbg(&pnp->dev, "device %s using irq %d\n",
298 pci_name(pci), irq);
299 return 1;
300 }
301
302 /*
303 * See pci_setup_device() and ata_pci_sff_activate_host() for
304 * similar IDE legacy detection.
305 */
306 pci_read_config_dword(pci, PCI_CLASS_REVISION, &class);
307 class >>= 8; /* discard revision ID */
308 progif = class & 0xff;
309 class >>= 8;
310
311 if (class == PCI_CLASS_STORAGE_IDE) {
312 /*
313 * Unless both channels are native-PCI mode only,
314 * treat the compatibility IRQs as busy.
315 */
316 if ((progif & 0x5) != 0x5)
317 if (pci_get_legacy_ide_irq(pci, 0) == irq ||
318 pci_get_legacy_ide_irq(pci, 1) == irq) {
319 dev_dbg(&pnp->dev, "legacy IDE device %s "
320 "using irq %d\n", pci_name(pci), irq);
321 return 1;
322 }
323 }
324
325 return 0;
326}
327#endif
328
329static int pci_uses_irq(struct pnp_dev *pnp, unsigned int irq)
330{
331#ifdef CONFIG_PCI
332 struct pci_dev *pci = NULL;
333
334 for_each_pci_dev(pci) {
335 if (pci_dev_uses_irq(pnp, pci, irq)) {
336 pci_dev_put(pci);
337 return 1;
338 }
339 }
340#endif
341 return 0;
342}
343
367int pnp_check_irq(struct pnp_dev *dev, struct resource *res) 344int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
368{ 345{
369 int i; 346 int i;
@@ -395,18 +372,9 @@ int pnp_check_irq(struct pnp_dev *dev, struct resource *res)
395 } 372 }
396 } 373 }
397 374
398#ifdef CONFIG_PCI
399 /* check if the resource is being used by a pci device */ 375 /* check if the resource is being used by a pci device */
400 { 376 if (pci_uses_irq(dev, *irq))
401 struct pci_dev *pci = NULL; 377 return 0;
402 for_each_pci_dev(pci) {
403 if (pci->irq == *irq) {
404 pci_dev_put(pci);
405 return 0;
406 }
407 }
408 }
409#endif
410 378
411 /* check if the resource is already in use, skip if the 379 /* check if the resource is already in use, skip if the
412 * device is active because it itself may be in use */ 380 * device is active because it itself may be in use */
@@ -499,81 +467,37 @@ int pnp_check_dma(struct pnp_dev *dev, struct resource *res)
499#endif 467#endif
500} 468}
501 469
502struct pnp_resource *pnp_get_pnp_resource(struct pnp_dev *dev, 470int pnp_resource_type(struct resource *res)
503 unsigned int type, unsigned int num)
504{ 471{
505 struct pnp_resource_table *res = dev->res; 472 return res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
506 473 IORESOURCE_IRQ | IORESOURCE_DMA);
507 switch (type) {
508 case IORESOURCE_IO:
509 if (num >= PNP_MAX_PORT)
510 return NULL;
511 return &res->port[num];
512 case IORESOURCE_MEM:
513 if (num >= PNP_MAX_MEM)
514 return NULL;
515 return &res->mem[num];
516 case IORESOURCE_IRQ:
517 if (num >= PNP_MAX_IRQ)
518 return NULL;
519 return &res->irq[num];
520 case IORESOURCE_DMA:
521 if (num >= PNP_MAX_DMA)
522 return NULL;
523 return &res->dma[num];
524 }
525 return NULL;
526} 474}
527 475
528struct resource *pnp_get_resource(struct pnp_dev *dev, 476struct resource *pnp_get_resource(struct pnp_dev *dev,
529 unsigned int type, unsigned int num) 477 unsigned int type, unsigned int num)
530{ 478{
531 struct pnp_resource *pnp_res; 479 struct pnp_resource *pnp_res;
480 struct resource *res;
532 481
533 pnp_res = pnp_get_pnp_resource(dev, type, num); 482 list_for_each_entry(pnp_res, &dev->resources, list) {
534 if (pnp_res) 483 res = &pnp_res->res;
535 return &pnp_res->res; 484 if (pnp_resource_type(res) == type && num-- == 0)
536 485 return res;
486 }
537 return NULL; 487 return NULL;
538} 488}
539EXPORT_SYMBOL(pnp_get_resource); 489EXPORT_SYMBOL(pnp_get_resource);
540 490
541static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev, int type) 491static struct pnp_resource *pnp_new_resource(struct pnp_dev *dev)
542{ 492{
543 struct pnp_resource *pnp_res; 493 struct pnp_resource *pnp_res;
544 int i;
545 494
546 switch (type) { 495 pnp_res = kzalloc(sizeof(struct pnp_resource), GFP_KERNEL);
547 case IORESOURCE_IO: 496 if (!pnp_res)
548 for (i = 0; i < PNP_MAX_PORT; i++) { 497 return NULL;
549 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IO, i); 498
550 if (pnp_res && !pnp_resource_valid(&pnp_res->res)) 499 list_add_tail(&pnp_res->list, &dev->resources);
551 return pnp_res; 500 return pnp_res;
552 }
553 break;
554 case IORESOURCE_MEM:
555 for (i = 0; i < PNP_MAX_MEM; i++) {
556 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_MEM, i);
557 if (pnp_res && !pnp_resource_valid(&pnp_res->res))
558 return pnp_res;
559 }
560 break;
561 case IORESOURCE_IRQ:
562 for (i = 0; i < PNP_MAX_IRQ; i++) {
563 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IRQ, i);
564 if (pnp_res && !pnp_resource_valid(&pnp_res->res))
565 return pnp_res;
566 }
567 break;
568 case IORESOURCE_DMA:
569 for (i = 0; i < PNP_MAX_DMA; i++) {
570 pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_DMA, i);
571 if (pnp_res && !pnp_resource_valid(&pnp_res->res))
572 return pnp_res;
573 }
574 break;
575 }
576 return NULL;
577} 501}
578 502
579struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq, 503struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
@@ -581,15 +505,10 @@ struct pnp_resource *pnp_add_irq_resource(struct pnp_dev *dev, int irq,
581{ 505{
582 struct pnp_resource *pnp_res; 506 struct pnp_resource *pnp_res;
583 struct resource *res; 507 struct resource *res;
584 static unsigned char warned;
585 508
586 pnp_res = pnp_new_resource(dev, IORESOURCE_IRQ); 509 pnp_res = pnp_new_resource(dev);
587 if (!pnp_res) { 510 if (!pnp_res) {
588 if (!warned) { 511 dev_err(&dev->dev, "can't add resource for IRQ %d\n", irq);
589 dev_err(&dev->dev, "can't add resource for IRQ %d\n",
590 irq);
591 warned = 1;
592 }
593 return NULL; 512 return NULL;
594 } 513 }
595 514
@@ -607,15 +526,10 @@ struct pnp_resource *pnp_add_dma_resource(struct pnp_dev *dev, int dma,
607{ 526{
608 struct pnp_resource *pnp_res; 527 struct pnp_resource *pnp_res;
609 struct resource *res; 528 struct resource *res;
610 static unsigned char warned;
611 529
612 pnp_res = pnp_new_resource(dev, IORESOURCE_DMA); 530 pnp_res = pnp_new_resource(dev);
613 if (!pnp_res) { 531 if (!pnp_res) {
614 if (!warned) { 532 dev_err(&dev->dev, "can't add resource for DMA %d\n", dma);
615 dev_err(&dev->dev, "can't add resource for DMA %d\n",
616 dma);
617 warned = 1;
618 }
619 return NULL; 533 return NULL;
620 } 534 }
621 535
@@ -634,16 +548,12 @@ struct pnp_resource *pnp_add_io_resource(struct pnp_dev *dev,
634{ 548{
635 struct pnp_resource *pnp_res; 549 struct pnp_resource *pnp_res;
636 struct resource *res; 550 struct resource *res;
637 static unsigned char warned;
638 551
639 pnp_res = pnp_new_resource(dev, IORESOURCE_IO); 552 pnp_res = pnp_new_resource(dev);
640 if (!pnp_res) { 553 if (!pnp_res) {
641 if (!warned) { 554 dev_err(&dev->dev, "can't add resource for IO %#llx-%#llx\n",
642 dev_err(&dev->dev, "can't add resource for IO " 555 (unsigned long long) start,
643 "%#llx-%#llx\n",(unsigned long long) start, 556 (unsigned long long) end);
644 (unsigned long long) end);
645 warned = 1;
646 }
647 return NULL; 557 return NULL;
648 } 558 }
649 559
@@ -663,16 +573,12 @@ struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
663{ 573{
664 struct pnp_resource *pnp_res; 574 struct pnp_resource *pnp_res;
665 struct resource *res; 575 struct resource *res;
666 static unsigned char warned;
667 576
668 pnp_res = pnp_new_resource(dev, IORESOURCE_MEM); 577 pnp_res = pnp_new_resource(dev);
669 if (!pnp_res) { 578 if (!pnp_res) {
670 if (!warned) { 579 dev_err(&dev->dev, "can't add resource for MEM %#llx-%#llx\n",
671 dev_err(&dev->dev, "can't add resource for MEM " 580 (unsigned long long) start,
672 "%#llx-%#llx\n",(unsigned long long) start, 581 (unsigned long long) end);
673 (unsigned long long) end);
674 warned = 1;
675 }
676 return NULL; 582 return NULL;
677 } 583 }
678 584
@@ -686,6 +592,52 @@ struct pnp_resource *pnp_add_mem_resource(struct pnp_dev *dev,
686 return pnp_res; 592 return pnp_res;
687} 593}
688 594
595/*
596 * Determine whether the specified resource is a possible configuration
597 * for this device.
598 */
599int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t start,
600 resource_size_t size)
601{
602 struct pnp_option *option;
603 struct pnp_port *port;
604 struct pnp_mem *mem;
605 struct pnp_irq *irq;
606 struct pnp_dma *dma;
607
608 list_for_each_entry(option, &dev->options, list) {
609 if (option->type != type)
610 continue;
611
612 switch (option->type) {
613 case IORESOURCE_IO:
614 port = &option->u.port;
615 if (port->min == start && port->size == size)
616 return 1;
617 break;
618 case IORESOURCE_MEM:
619 mem = &option->u.mem;
620 if (mem->min == start && mem->size == size)
621 return 1;
622 break;
623 case IORESOURCE_IRQ:
624 irq = &option->u.irq;
625 if (start < PNP_IRQ_NR &&
626 test_bit(start, irq->map.bits))
627 return 1;
628 break;
629 case IORESOURCE_DMA:
630 dma = &option->u.dma;
631 if (dma->map & (1 << start))
632 return 1;
633 break;
634 }
635 }
636
637 return 0;
638}
639EXPORT_SYMBOL(pnp_possible_config);
640
689/* format is: pnp_reserve_irq=irq1[,irq2] .... */ 641/* format is: pnp_reserve_irq=irq1[,irq2] .... */
690static int __init pnp_setup_reserve_irq(char *str) 642static int __init pnp_setup_reserve_irq(char *str)
691{ 643{
diff --git a/drivers/pnp/support.c b/drivers/pnp/support.c
index 95b076c18c07..bbf78ef4ba02 100644
--- a/drivers/pnp/support.c
+++ b/drivers/pnp/support.c
@@ -2,6 +2,8 @@
2 * support.c - standard functions for the use of pnp protocol drivers 2 * support.c - standard functions for the use of pnp protocol drivers
3 * 3 *
4 * Copyright 2003 Adam Belay <ambx1@neo.rr.com> 4 * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
5 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
6 * Bjorn Helgaas <bjorn.helgaas@hp.com>
5 */ 7 */
6 8
7#include <linux/module.h> 9#include <linux/module.h>
@@ -16,6 +18,10 @@
16 */ 18 */
17int pnp_is_active(struct pnp_dev *dev) 19int pnp_is_active(struct pnp_dev *dev)
18{ 20{
21 /*
22 * I don't think this is very reliable because pnp_disable_dev()
23 * only clears out auto-assigned resources.
24 */
19 if (!pnp_port_start(dev, 0) && pnp_port_len(dev, 0) <= 1 && 25 if (!pnp_port_start(dev, 0) && pnp_port_len(dev, 0) <= 1 &&
20 !pnp_mem_start(dev, 0) && pnp_mem_len(dev, 0) <= 1 && 26 !pnp_mem_start(dev, 0) && pnp_mem_len(dev, 0) <= 1 &&
21 pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1) 27 pnp_irq(dev, 0) == -1 && pnp_dma(dev, 0) == -1)
@@ -52,39 +58,154 @@ void pnp_eisa_id_to_string(u32 id, char *str)
52 str[7] = '\0'; 58 str[7] = '\0';
53} 59}
54 60
61char *pnp_resource_type_name(struct resource *res)
62{
63 switch (pnp_resource_type(res)) {
64 case IORESOURCE_IO:
65 return "io";
66 case IORESOURCE_MEM:
67 return "mem";
68 case IORESOURCE_IRQ:
69 return "irq";
70 case IORESOURCE_DMA:
71 return "dma";
72 }
73 return NULL;
74}
75
55void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc) 76void dbg_pnp_show_resources(struct pnp_dev *dev, char *desc)
56{ 77{
57#ifdef DEBUG 78#ifdef DEBUG
79 char buf[128];
80 int len = 0;
81 struct pnp_resource *pnp_res;
58 struct resource *res; 82 struct resource *res;
59 int i;
60 83
61 dev_dbg(&dev->dev, "current resources: %s\n", desc); 84 if (list_empty(&dev->resources)) {
62 85 dev_dbg(&dev->dev, "%s: no current resources\n", desc);
63 for (i = 0; i < PNP_MAX_IRQ; i++) { 86 return;
64 res = pnp_get_resource(dev, IORESOURCE_IRQ, i);
65 if (res && !(res->flags & IORESOURCE_UNSET))
66 dev_dbg(&dev->dev, " irq %lld flags %#lx\n",
67 (unsigned long long) res->start, res->flags);
68 } 87 }
69 for (i = 0; i < PNP_MAX_DMA; i++) { 88
70 res = pnp_get_resource(dev, IORESOURCE_DMA, i); 89 dev_dbg(&dev->dev, "%s: current resources:\n", desc);
71 if (res && !(res->flags & IORESOURCE_UNSET)) 90 list_for_each_entry(pnp_res, &dev->resources, list) {
72 dev_dbg(&dev->dev, " dma %lld flags %#lx\n", 91 res = &pnp_res->res;
73 (unsigned long long) res->start, res->flags); 92
93 len += snprintf(buf + len, sizeof(buf) - len, " %-3s ",
94 pnp_resource_type_name(res));
95
96 if (res->flags & IORESOURCE_DISABLED) {
97 dev_dbg(&dev->dev, "%sdisabled\n", buf);
98 continue;
99 }
100
101 switch (pnp_resource_type(res)) {
102 case IORESOURCE_IO:
103 case IORESOURCE_MEM:
104 len += snprintf(buf + len, sizeof(buf) - len,
105 "%#llx-%#llx flags %#lx",
106 (unsigned long long) res->start,
107 (unsigned long long) res->end,
108 res->flags);
109 break;
110 case IORESOURCE_IRQ:
111 case IORESOURCE_DMA:
112 len += snprintf(buf + len, sizeof(buf) - len,
113 "%lld flags %#lx",
114 (unsigned long long) res->start,
115 res->flags);
116 break;
117 }
118 dev_dbg(&dev->dev, "%s\n", buf);
74 } 119 }
75 for (i = 0; i < PNP_MAX_PORT; i++) { 120#endif
76 res = pnp_get_resource(dev, IORESOURCE_IO, i); 121}
77 if (res && !(res->flags & IORESOURCE_UNSET)) 122
78 dev_dbg(&dev->dev, " io %#llx-%#llx flags %#lx\n", 123char *pnp_option_priority_name(struct pnp_option *option)
79 (unsigned long long) res->start, 124{
80 (unsigned long long) res->end, res->flags); 125 switch (pnp_option_priority(option)) {
126 case PNP_RES_PRIORITY_PREFERRED:
127 return "preferred";
128 case PNP_RES_PRIORITY_ACCEPTABLE:
129 return "acceptable";
130 case PNP_RES_PRIORITY_FUNCTIONAL:
131 return "functional";
81 } 132 }
82 for (i = 0; i < PNP_MAX_MEM; i++) { 133 return "invalid";
83 res = pnp_get_resource(dev, IORESOURCE_MEM, i); 134}
84 if (res && !(res->flags & IORESOURCE_UNSET)) 135
85 dev_dbg(&dev->dev, " mem %#llx-%#llx flags %#lx\n", 136void dbg_pnp_show_option(struct pnp_dev *dev, struct pnp_option *option)
86 (unsigned long long) res->start, 137{
87 (unsigned long long) res->end, res->flags); 138#ifdef DEBUG
139 char buf[128];
140 int len = 0, i;
141 struct pnp_port *port;
142 struct pnp_mem *mem;
143 struct pnp_irq *irq;
144 struct pnp_dma *dma;
145
146 if (pnp_option_is_dependent(option))
147 len += snprintf(buf + len, sizeof(buf) - len,
148 " dependent set %d (%s) ",
149 pnp_option_set(option),
150 pnp_option_priority_name(option));
151 else
152 len += snprintf(buf + len, sizeof(buf) - len, " independent ");
153
154 switch (option->type) {
155 case IORESOURCE_IO:
156 port = &option->u.port;
157 len += snprintf(buf + len, sizeof(buf) - len, "io min %#llx "
158 "max %#llx align %lld size %lld flags %#x",
159 (unsigned long long) port->min,
160 (unsigned long long) port->max,
161 (unsigned long long) port->align,
162 (unsigned long long) port->size, port->flags);
163 break;
164 case IORESOURCE_MEM:
165 mem = &option->u.mem;
166 len += snprintf(buf + len, sizeof(buf) - len, "mem min %#llx "
167 "max %#llx align %lld size %lld flags %#x",
168 (unsigned long long) mem->min,
169 (unsigned long long) mem->max,
170 (unsigned long long) mem->align,
171 (unsigned long long) mem->size, mem->flags);
172 break;
173 case IORESOURCE_IRQ:
174 irq = &option->u.irq;
175 len += snprintf(buf + len, sizeof(buf) - len, "irq");
176 if (bitmap_empty(irq->map.bits, PNP_IRQ_NR))
177 len += snprintf(buf + len, sizeof(buf) - len,
178 " <none>");
179 else {
180 for (i = 0; i < PNP_IRQ_NR; i++)
181 if (test_bit(i, irq->map.bits))
182 len += snprintf(buf + len,
183 sizeof(buf) - len,
184 " %d", i);
185 }
186 len += snprintf(buf + len, sizeof(buf) - len, " flags %#x",
187 irq->flags);
188 if (irq->flags & IORESOURCE_IRQ_OPTIONAL)
189 len += snprintf(buf + len, sizeof(buf) - len,
190 " (optional)");
191 break;
192 case IORESOURCE_DMA:
193 dma = &option->u.dma;
194 len += snprintf(buf + len, sizeof(buf) - len, "dma");
195 if (!dma->map)
196 len += snprintf(buf + len, sizeof(buf) - len,
197 " <none>");
198 else {
199 for (i = 0; i < 8; i++)
200 if (dma->map & (1 << i))
201 len += snprintf(buf + len,
202 sizeof(buf) - len,
203 " %d", i);
204 }
205 len += snprintf(buf + len, sizeof(buf) - len, " (bitmask %#x) "
206 "flags %#x", dma->map, dma->flags);
207 break;
88 } 208 }
209 dev_dbg(&dev->dev, "%s\n", buf);
89#endif 210#endif
90} 211}
diff --git a/drivers/pnp/system.c b/drivers/pnp/system.c
index cf4e07b01d48..764f3a310685 100644
--- a/drivers/pnp/system.c
+++ b/drivers/pnp/system.c
@@ -60,7 +60,7 @@ static void reserve_resources_of_dev(struct pnp_dev *dev)
60 int i; 60 int i;
61 61
62 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) { 62 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_IO, i)); i++) {
63 if (res->flags & IORESOURCE_UNSET) 63 if (res->flags & IORESOURCE_DISABLED)
64 continue; 64 continue;
65 if (res->start == 0) 65 if (res->start == 0)
66 continue; /* disabled */ 66 continue; /* disabled */
@@ -81,7 +81,7 @@ static void reserve_resources_of_dev(struct pnp_dev *dev)
81 } 81 }
82 82
83 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) { 83 for (i = 0; (res = pnp_get_resource(dev, IORESOURCE_MEM, i)); i++) {
84 if (res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED)) 84 if (res->flags & IORESOURCE_DISABLED)
85 continue; 85 continue;
86 86
87 reserve_range(dev, res->start, res->end, 0); 87 reserve_range(dev, res->start, res->end, 0);
diff --git a/drivers/s390/block/dasd_diag.c b/drivers/s390/block/dasd_diag.c
index d91df38ee4f7..85fcb4371054 100644
--- a/drivers/s390/block/dasd_diag.c
+++ b/drivers/s390/block/dasd_diag.c
@@ -333,7 +333,8 @@ dasd_diag_check_device(struct dasd_device *device)
333 if (IS_ERR(block)) { 333 if (IS_ERR(block)) {
334 DEV_MESSAGE(KERN_WARNING, device, "%s", 334 DEV_MESSAGE(KERN_WARNING, device, "%s",
335 "could not allocate dasd block structure"); 335 "could not allocate dasd block structure");
336 kfree(device->private); 336 device->private = NULL;
337 kfree(private);
337 return PTR_ERR(block); 338 return PTR_ERR(block);
338 } 339 }
339 device->block = block; 340 device->block = block;
@@ -348,7 +349,8 @@ dasd_diag_check_device(struct dasd_device *device)
348 if (rc) { 349 if (rc) {
349 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device " 350 DEV_MESSAGE(KERN_WARNING, device, "failed to retrieve device "
350 "information (rc=%d)", rc); 351 "information (rc=%d)", rc);
351 return -ENOTSUPP; 352 rc = -EOPNOTSUPP;
353 goto out;
352 } 354 }
353 355
354 /* Figure out position of label block */ 356 /* Figure out position of label block */
@@ -362,7 +364,8 @@ dasd_diag_check_device(struct dasd_device *device)
362 default: 364 default:
363 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class " 365 DEV_MESSAGE(KERN_WARNING, device, "unsupported device class "
364 "(class=%d)", private->rdc_data.vdev_class); 366 "(class=%d)", private->rdc_data.vdev_class);
365 return -ENOTSUPP; 367 rc = -EOPNOTSUPP;
368 goto out;
366 } 369 }
367 370
368 DBF_DEV_EVENT(DBF_INFO, device, 371 DBF_DEV_EVENT(DBF_INFO, device,
@@ -379,7 +382,8 @@ dasd_diag_check_device(struct dasd_device *device)
379 if (label == NULL) { 382 if (label == NULL) {
380 DEV_MESSAGE(KERN_WARNING, device, "%s", 383 DEV_MESSAGE(KERN_WARNING, device, "%s",
381 "No memory to allocate initialization request"); 384 "No memory to allocate initialization request");
382 return -ENOMEM; 385 rc = -ENOMEM;
386 goto out;
383 } 387 }
384 rc = 0; 388 rc = 0;
385 end_block = 0; 389 end_block = 0;
@@ -403,7 +407,7 @@ dasd_diag_check_device(struct dasd_device *device)
403 DEV_MESSAGE(KERN_WARNING, device, "%s", 407 DEV_MESSAGE(KERN_WARNING, device, "%s",
404 "DIAG call failed"); 408 "DIAG call failed");
405 rc = -EOPNOTSUPP; 409 rc = -EOPNOTSUPP;
406 goto out; 410 goto out_label;
407 } 411 }
408 mdsk_term_io(device); 412 mdsk_term_io(device);
409 if (rc == 0) 413 if (rc == 0)
@@ -413,7 +417,7 @@ dasd_diag_check_device(struct dasd_device *device)
413 DEV_MESSAGE(KERN_WARNING, device, "device access failed " 417 DEV_MESSAGE(KERN_WARNING, device, "device access failed "
414 "(rc=%d)", rc); 418 "(rc=%d)", rc);
415 rc = -EIO; 419 rc = -EIO;
416 goto out; 420 goto out_label;
417 } 421 }
418 /* check for label block */ 422 /* check for label block */
419 if (memcmp(label->label_id, DASD_DIAG_CMS1, 423 if (memcmp(label->label_id, DASD_DIAG_CMS1,
@@ -439,8 +443,15 @@ dasd_diag_check_device(struct dasd_device *device)
439 (unsigned long) (block->blocks << 443 (unsigned long) (block->blocks <<
440 block->s2b_shift) >> 1); 444 block->s2b_shift) >> 1);
441 } 445 }
442out: 446out_label:
443 free_page((long) label); 447 free_page((long) label);
448out:
449 if (rc) {
450 device->block = NULL;
451 dasd_free_block(block);
452 device->private = NULL;
453 kfree(private);
454 }
444 return rc; 455 return rc;
445} 456}
446 457
diff --git a/drivers/s390/block/dasd_eckd.c b/drivers/s390/block/dasd_eckd.c
index e0b77210d37a..3590fdb5b2fd 100644
--- a/drivers/s390/block/dasd_eckd.c
+++ b/drivers/s390/block/dasd_eckd.c
@@ -1418,8 +1418,10 @@ static void dasd_eckd_handle_unsolicited_interrupt(struct dasd_device *device,
1418 1418
1419 1419
1420 /* service information message SIM */ 1420 /* service information message SIM */
1421 if ((irb->ecw[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE) { 1421 if (irb->esw.esw0.erw.cons && (irb->ecw[27] & DASD_SENSE_BIT_0) &&
1422 ((irb->ecw[6] & DASD_SIM_SENSE) == DASD_SIM_SENSE)) {
1422 dasd_3990_erp_handle_sim(device, irb->ecw); 1423 dasd_3990_erp_handle_sim(device, irb->ecw);
1424 dasd_schedule_device_bh(device);
1423 return; 1425 return;
1424 } 1426 }
1425 1427
diff --git a/drivers/s390/block/dasd_fba.c b/drivers/s390/block/dasd_fba.c
index aee4656127f7..aa0c533423a5 100644
--- a/drivers/s390/block/dasd_fba.c
+++ b/drivers/s390/block/dasd_fba.c
@@ -139,7 +139,8 @@ dasd_fba_check_characteristics(struct dasd_device *device)
139 if (IS_ERR(block)) { 139 if (IS_ERR(block)) {
140 DEV_MESSAGE(KERN_WARNING, device, "%s", 140 DEV_MESSAGE(KERN_WARNING, device, "%s",
141 "could not allocate dasd block structure"); 141 "could not allocate dasd block structure");
142 kfree(device->private); 142 device->private = NULL;
143 kfree(private);
143 return PTR_ERR(block); 144 return PTR_ERR(block);
144 } 145 }
145 device->block = block; 146 device->block = block;
@@ -152,6 +153,10 @@ dasd_fba_check_characteristics(struct dasd_device *device)
152 DEV_MESSAGE(KERN_WARNING, device, 153 DEV_MESSAGE(KERN_WARNING, device,
153 "Read device characteristics returned error %d", 154 "Read device characteristics returned error %d",
154 rc); 155 rc);
156 device->block = NULL;
157 dasd_free_block(block);
158 device->private = NULL;
159 kfree(private);
155 return rc; 160 return rc;
156 } 161 }
157 162
diff --git a/drivers/s390/char/vmur.c b/drivers/s390/char/vmur.c
index 0a9f1cccbe58..b0ac44b27127 100644
--- a/drivers/s390/char/vmur.c
+++ b/drivers/s390/char/vmur.c
@@ -345,7 +345,7 @@ static int get_urd_class(struct urdev *urd)
345 cc = diag210(&ur_diag210); 345 cc = diag210(&ur_diag210);
346 switch (cc) { 346 switch (cc) {
347 case 0: 347 case 0:
348 return -ENOTSUPP; 348 return -EOPNOTSUPP;
349 case 2: 349 case 2:
350 return ur_diag210.vrdcvcla; /* virtual device class */ 350 return ur_diag210.vrdcvcla; /* virtual device class */
351 case 3: 351 case 3:
@@ -621,7 +621,7 @@ static int verify_device(struct urdev *urd)
621 case DEV_CLASS_UR_I: 621 case DEV_CLASS_UR_I:
622 return verify_uri_device(urd); 622 return verify_uri_device(urd);
623 default: 623 default:
624 return -ENOTSUPP; 624 return -EOPNOTSUPP;
625 } 625 }
626} 626}
627 627
@@ -654,7 +654,7 @@ static int get_file_reclen(struct urdev *urd)
654 case DEV_CLASS_UR_I: 654 case DEV_CLASS_UR_I:
655 return get_uri_file_reclen(urd); 655 return get_uri_file_reclen(urd);
656 default: 656 default:
657 return -ENOTSUPP; 657 return -EOPNOTSUPP;
658 } 658 }
659} 659}
660 660
@@ -827,7 +827,7 @@ static int ur_probe(struct ccw_device *cdev)
827 goto fail_remove_attr; 827 goto fail_remove_attr;
828 } 828 }
829 if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) { 829 if ((urd->class != DEV_CLASS_UR_I) && (urd->class != DEV_CLASS_UR_O)) {
830 rc = -ENOTSUPP; 830 rc = -EOPNOTSUPP;
831 goto fail_remove_attr; 831 goto fail_remove_attr;
832 } 832 }
833 spin_lock_irq(get_ccwdev_lock(cdev)); 833 spin_lock_irq(get_ccwdev_lock(cdev));
@@ -892,7 +892,7 @@ static int ur_set_online(struct ccw_device *cdev)
892 } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) { 892 } else if (urd->cdev->id.cu_type == PRINTER_DEVTYPE) {
893 sprintf(node_id, "vmprt-%s", cdev->dev.bus_id); 893 sprintf(node_id, "vmprt-%s", cdev->dev.bus_id);
894 } else { 894 } else {
895 rc = -ENOTSUPP; 895 rc = -EOPNOTSUPP;
896 goto fail_free_cdev; 896 goto fail_free_cdev;
897 } 897 }
898 898
diff --git a/drivers/s390/char/zcore.c b/drivers/s390/char/zcore.c
index 047dd92ae804..7fd84be11931 100644
--- a/drivers/s390/char/zcore.c
+++ b/drivers/s390/char/zcore.c
@@ -29,6 +29,7 @@
29 29
30#define TO_USER 0 30#define TO_USER 0
31#define TO_KERNEL 1 31#define TO_KERNEL 1
32#define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
32 33
33enum arch_id { 34enum arch_id {
34 ARCH_S390 = 0, 35 ARCH_S390 = 0,
@@ -51,6 +52,7 @@ static struct debug_info *zcore_dbf;
51static int hsa_available; 52static int hsa_available;
52static struct dentry *zcore_dir; 53static struct dentry *zcore_dir;
53static struct dentry *zcore_file; 54static struct dentry *zcore_file;
55static struct dentry *zcore_memmap_file;
54 56
55/* 57/*
56 * Copy memory from HSA to kernel or user memory (not reentrant): 58 * Copy memory from HSA to kernel or user memory (not reentrant):
@@ -476,6 +478,54 @@ static const struct file_operations zcore_fops = {
476 .release = zcore_release, 478 .release = zcore_release,
477}; 479};
478 480
481static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
482 size_t count, loff_t *ppos)
483{
484 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
485 MEMORY_CHUNKS * CHUNK_INFO_SIZE);
486}
487
488static int zcore_memmap_open(struct inode *inode, struct file *filp)
489{
490 int i;
491 char *buf;
492 struct mem_chunk *chunk_array;
493
494 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
495 GFP_KERNEL);
496 if (!chunk_array)
497 return -ENOMEM;
498 detect_memory_layout(chunk_array);
499 buf = kzalloc(MEMORY_CHUNKS * CHUNK_INFO_SIZE, GFP_KERNEL);
500 if (!buf) {
501 kfree(chunk_array);
502 return -ENOMEM;
503 }
504 for (i = 0; i < MEMORY_CHUNKS; i++) {
505 sprintf(buf + (i * CHUNK_INFO_SIZE), "%016llx %016llx ",
506 (unsigned long long) chunk_array[i].addr,
507 (unsigned long long) chunk_array[i].size);
508 if (chunk_array[i].size == 0)
509 break;
510 }
511 kfree(chunk_array);
512 filp->private_data = buf;
513 return 0;
514}
515
516static int zcore_memmap_release(struct inode *inode, struct file *filp)
517{
518 kfree(filp->private_data);
519 return 0;
520}
521
522static const struct file_operations zcore_memmap_fops = {
523 .owner = THIS_MODULE,
524 .read = zcore_memmap_read,
525 .open = zcore_memmap_open,
526 .release = zcore_memmap_release,
527};
528
479 529
480static void __init set_s390_lc_mask(union save_area *map) 530static void __init set_s390_lc_mask(union save_area *map)
481{ 531{
@@ -554,18 +604,44 @@ static int __init check_sdias(void)
554 return 0; 604 return 0;
555} 605}
556 606
557static void __init zcore_header_init(int arch, struct zcore_header *hdr) 607static int __init get_mem_size(unsigned long *mem)
608{
609 int i;
610 struct mem_chunk *chunk_array;
611
612 chunk_array = kzalloc(MEMORY_CHUNKS * sizeof(struct mem_chunk),
613 GFP_KERNEL);
614 if (!chunk_array)
615 return -ENOMEM;
616 detect_memory_layout(chunk_array);
617 for (i = 0; i < MEMORY_CHUNKS; i++) {
618 if (chunk_array[i].size == 0)
619 break;
620 *mem += chunk_array[i].size;
621 }
622 kfree(chunk_array);
623 return 0;
624}
625
626static int __init zcore_header_init(int arch, struct zcore_header *hdr)
558{ 627{
628 int rc;
629 unsigned long memory = 0;
630
559 if (arch == ARCH_S390X) 631 if (arch == ARCH_S390X)
560 hdr->arch_id = DUMP_ARCH_S390X; 632 hdr->arch_id = DUMP_ARCH_S390X;
561 else 633 else
562 hdr->arch_id = DUMP_ARCH_S390; 634 hdr->arch_id = DUMP_ARCH_S390;
563 hdr->mem_size = sys_info.mem_size; 635 rc = get_mem_size(&memory);
564 hdr->rmem_size = sys_info.mem_size; 636 if (rc)
637 return rc;
638 hdr->mem_size = memory;
639 hdr->rmem_size = memory;
565 hdr->mem_end = sys_info.mem_size; 640 hdr->mem_end = sys_info.mem_size;
566 hdr->num_pages = sys_info.mem_size / PAGE_SIZE; 641 hdr->num_pages = memory / PAGE_SIZE;
567 hdr->tod = get_clock(); 642 hdr->tod = get_clock();
568 get_cpu_id(&hdr->cpu_id); 643 get_cpu_id(&hdr->cpu_id);
644 return 0;
569} 645}
570 646
571static int __init zcore_init(void) 647static int __init zcore_init(void)
@@ -608,7 +684,9 @@ static int __init zcore_init(void)
608 if (rc) 684 if (rc)
609 goto fail; 685 goto fail;
610 686
611 zcore_header_init(arch, &zcore_header); 687 rc = zcore_header_init(arch, &zcore_header);
688 if (rc)
689 goto fail;
612 690
613 zcore_dir = debugfs_create_dir("zcore" , NULL); 691 zcore_dir = debugfs_create_dir("zcore" , NULL);
614 if (!zcore_dir) { 692 if (!zcore_dir) {
@@ -618,13 +696,22 @@ static int __init zcore_init(void)
618 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL, 696 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
619 &zcore_fops); 697 &zcore_fops);
620 if (!zcore_file) { 698 if (!zcore_file) {
621 debugfs_remove(zcore_dir);
622 rc = -ENOMEM; 699 rc = -ENOMEM;
623 goto fail; 700 goto fail_dir;
701 }
702 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
703 NULL, &zcore_memmap_fops);
704 if (!zcore_memmap_file) {
705 rc = -ENOMEM;
706 goto fail_file;
624 } 707 }
625 hsa_available = 1; 708 hsa_available = 1;
626 return 0; 709 return 0;
627 710
711fail_file:
712 debugfs_remove(zcore_file);
713fail_dir:
714 debugfs_remove(zcore_dir);
628fail: 715fail:
629 diag308(DIAG308_REL_HSA, NULL); 716 diag308(DIAG308_REL_HSA, NULL);
630 return rc; 717 return rc;
diff --git a/drivers/s390/cio/Makefile b/drivers/s390/cio/Makefile
index 91e9e3f3073a..bd79bd165396 100644
--- a/drivers/s390/cio/Makefile
+++ b/drivers/s390/cio/Makefile
@@ -9,4 +9,6 @@ ccw_device-objs += device_id.o device_pgid.o device_status.o
9obj-y += ccw_device.o cmf.o 9obj-y += ccw_device.o cmf.o
10obj-$(CONFIG_CHSC_SCH) += chsc_sch.o 10obj-$(CONFIG_CHSC_SCH) += chsc_sch.o
11obj-$(CONFIG_CCWGROUP) += ccwgroup.o 11obj-$(CONFIG_CCWGROUP) += ccwgroup.o
12
13qdio-objs := qdio_main.o qdio_thinint.o qdio_debug.o qdio_perf.o qdio_setup.o
12obj-$(CONFIG_QDIO) += qdio.o 14obj-$(CONFIG_QDIO) += qdio.o
diff --git a/drivers/s390/cio/chsc.c b/drivers/s390/cio/chsc.c
index 65264a38057d..29826fdd47b8 100644
--- a/drivers/s390/cio/chsc.c
+++ b/drivers/s390/cio/chsc.c
@@ -27,7 +27,13 @@
27 27
28static void *sei_page; 28static void *sei_page;
29 29
30static int chsc_error_from_response(int response) 30/**
31 * chsc_error_from_response() - convert a chsc response to an error
32 * @response: chsc response code
33 *
34 * Returns an appropriate Linux error code for @response.
35 */
36int chsc_error_from_response(int response)
31{ 37{
32 switch (response) { 38 switch (response) {
33 case 0x0001: 39 case 0x0001:
@@ -45,6 +51,7 @@ static int chsc_error_from_response(int response)
45 return -EIO; 51 return -EIO;
46 } 52 }
47} 53}
54EXPORT_SYMBOL_GPL(chsc_error_from_response);
48 55
49struct chsc_ssd_area { 56struct chsc_ssd_area {
50 struct chsc_header request; 57 struct chsc_header request;
diff --git a/drivers/s390/cio/chsc.h b/drivers/s390/cio/chsc.h
index fb6c4d6c45b4..ba59bceace98 100644
--- a/drivers/s390/cio/chsc.h
+++ b/drivers/s390/cio/chsc.h
@@ -101,4 +101,6 @@ void chsc_chp_online(struct chp_id chpid);
101void chsc_chp_offline(struct chp_id chpid); 101void chsc_chp_offline(struct chp_id chpid);
102int chsc_get_channel_measurement_chars(struct channel_path *chp); 102int chsc_get_channel_measurement_chars(struct channel_path *chp);
103 103
104int chsc_error_from_response(int response);
105
104#endif 106#endif
diff --git a/drivers/s390/cio/qdio.c b/drivers/s390/cio/qdio.c
deleted file mode 100644
index 2bf36e14b102..000000000000
--- a/drivers/s390/cio/qdio.c
+++ /dev/null
@@ -1,3929 +0,0 @@
1/*
2 *
3 * linux/drivers/s390/cio/qdio.c
4 *
5 * Linux for S/390 QDIO base support, Hipersocket base support
6 * version 2
7 *
8 * Copyright 2000,2002 IBM Corporation
9 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
10 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
11 *
12 * Restriction: only 63 iqdio subchannels would have its own indicator,
13 * after that, subsequent subchannels share one indicator
14 *
15 *
16 *
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2, or (at your option)
21 * any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 */
32
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/delay.h>
36#include <linux/slab.h>
37#include <linux/kernel.h>
38#include <linux/proc_fs.h>
39#include <linux/timer.h>
40#include <linux/mempool.h>
41#include <linux/semaphore.h>
42
43#include <asm/ccwdev.h>
44#include <asm/io.h>
45#include <asm/atomic.h>
46#include <asm/timex.h>
47
48#include <asm/debug.h>
49#include <asm/s390_rdev.h>
50#include <asm/qdio.h>
51#include <asm/airq.h>
52
53#include "cio.h"
54#include "css.h"
55#include "device.h"
56#include "qdio.h"
57#include "ioasm.h"
58#include "chsc.h"
59
60/****************** MODULE PARAMETER VARIABLES ********************/
61MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
62MODULE_DESCRIPTION("QDIO base support version 2, " \
63 "Copyright 2000 IBM Corporation");
64MODULE_LICENSE("GPL");
65
66/******************** HERE WE GO ***********************************/
67
68static const char version[] = "QDIO base support version 2";
69
70static int qdio_performance_stats = 0;
71static int proc_perf_file_registration;
72static struct qdio_perf_stats perf_stats;
73
74static int hydra_thinints;
75static int is_passthrough = 0;
76static int omit_svs;
77
78static int indicator_used[INDICATORS_PER_CACHELINE];
79static __u32 * volatile indicators;
80static __u32 volatile spare_indicator;
81static atomic_t spare_indicator_usecount;
82#define QDIO_MEMPOOL_SCSSC_ELEMENTS 2
83static mempool_t *qdio_mempool_scssc;
84static struct kmem_cache *qdio_q_cache;
85
86static debug_info_t *qdio_dbf_setup;
87static debug_info_t *qdio_dbf_sbal;
88static debug_info_t *qdio_dbf_trace;
89static debug_info_t *qdio_dbf_sense;
90#ifdef CONFIG_QDIO_DEBUG
91static debug_info_t *qdio_dbf_slsb_out;
92static debug_info_t *qdio_dbf_slsb_in;
93#endif /* CONFIG_QDIO_DEBUG */
94
95/* iQDIO stuff: */
96static volatile struct qdio_q *tiq_list=NULL; /* volatile as it could change
97 during a while loop */
98static DEFINE_SPINLOCK(ttiq_list_lock);
99static void *tiqdio_ind;
100static void tiqdio_tl(unsigned long);
101static DECLARE_TASKLET(tiqdio_tasklet,tiqdio_tl,0);
102
103/* not a macro, as one of the arguments is atomic_read */
104static inline int
105qdio_min(int a,int b)
106{
107 if (a<b)
108 return a;
109 else
110 return b;
111}
112
113/***************** SCRUBBER HELPER ROUTINES **********************/
114#ifdef CONFIG_64BIT
115static inline void qdio_perf_stat_inc(atomic64_t *count)
116{
117 if (qdio_performance_stats)
118 atomic64_inc(count);
119}
120
121static inline void qdio_perf_stat_dec(atomic64_t *count)
122{
123 if (qdio_performance_stats)
124 atomic64_dec(count);
125}
126#else /* CONFIG_64BIT */
127static inline void qdio_perf_stat_inc(atomic_t *count)
128{
129 if (qdio_performance_stats)
130 atomic_inc(count);
131}
132
133static inline void qdio_perf_stat_dec(atomic_t *count)
134{
135 if (qdio_performance_stats)
136 atomic_dec(count);
137}
138#endif /* CONFIG_64BIT */
139
140static inline __u64
141qdio_get_micros(void)
142{
143 return (get_clock() >> 12); /* time>>12 is microseconds */
144}
145
146/*
147 * unfortunately, we can't just xchg the values; in do_QDIO we want to reserve
148 * the q in any case, so that we'll not be interrupted when we are in
149 * qdio_mark_tiq... shouldn't have a really bad impact, as reserving almost
150 * ever works (last famous words)
151 */
152static inline int
153qdio_reserve_q(struct qdio_q *q)
154{
155 return atomic_add_return(1,&q->use_count) - 1;
156}
157
158static inline void
159qdio_release_q(struct qdio_q *q)
160{
161 atomic_dec(&q->use_count);
162}
163
164/*check ccq */
165static int
166qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
167{
168 char dbf_text[15];
169
170 if (ccq == 0 || ccq == 32)
171 return 0;
172 if (ccq == 96 || ccq == 97)
173 return 1;
174 /*notify devices immediately*/
175 sprintf(dbf_text,"%d", ccq);
176 QDIO_DBF_TEXT2(1,trace,dbf_text);
177 return -EIO;
178}
179/* EQBS: extract buffer states */
180static int
181qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
182 unsigned int *start, unsigned int *cnt)
183{
184 struct qdio_irq *irq;
185 unsigned int tmp_cnt, q_no, ccq;
186 int rc ;
187 char dbf_text[15];
188
189 ccq = 0;
190 tmp_cnt = *cnt;
191 irq = (struct qdio_irq*)q->irq_ptr;
192 q_no = q->q_no;
193 if(!q->is_input_q)
194 q_no += irq->no_input_qs;
195again:
196 ccq = do_eqbs(irq->sch_token, state, q_no, start, cnt);
197 rc = qdio_check_ccq(q, ccq);
198 if ((ccq == 96) && (tmp_cnt != *cnt))
199 rc = 0;
200 if (rc == 1) {
201 QDIO_DBF_TEXT5(1,trace,"eqAGAIN");
202 goto again;
203 }
204 if (rc < 0) {
205 QDIO_DBF_TEXT2(1,trace,"eqberr");
206 sprintf(dbf_text,"%2x,%2x,%d,%d",tmp_cnt, *cnt, ccq, q_no);
207 QDIO_DBF_TEXT2(1,trace,dbf_text);
208 q->handler(q->cdev,QDIO_STATUS_ACTIVATE_CHECK_CONDITION|
209 QDIO_STATUS_LOOK_FOR_ERROR,
210 0, 0, 0, -1, -1, q->int_parm);
211 return 0;
212 }
213 return (tmp_cnt - *cnt);
214}
215
216/* SQBS: set buffer states */
217static int
218qdio_do_sqbs(struct qdio_q *q, unsigned char state,
219 unsigned int *start, unsigned int *cnt)
220{
221 struct qdio_irq *irq;
222 unsigned int tmp_cnt, q_no, ccq;
223 int rc;
224 char dbf_text[15];
225
226 ccq = 0;
227 tmp_cnt = *cnt;
228 irq = (struct qdio_irq*)q->irq_ptr;
229 q_no = q->q_no;
230 if(!q->is_input_q)
231 q_no += irq->no_input_qs;
232again:
233 ccq = do_sqbs(irq->sch_token, state, q_no, start, cnt);
234 rc = qdio_check_ccq(q, ccq);
235 if (rc == 1) {
236 QDIO_DBF_TEXT5(1,trace,"sqAGAIN");
237 goto again;
238 }
239 if (rc < 0) {
240 QDIO_DBF_TEXT3(1,trace,"sqberr");
241 sprintf(dbf_text,"%2x,%2x",tmp_cnt,*cnt);
242 QDIO_DBF_TEXT3(1,trace,dbf_text);
243 sprintf(dbf_text,"%d,%d",ccq,q_no);
244 QDIO_DBF_TEXT3(1,trace,dbf_text);
245 q->handler(q->cdev,QDIO_STATUS_ACTIVATE_CHECK_CONDITION|
246 QDIO_STATUS_LOOK_FOR_ERROR,
247 0, 0, 0, -1, -1, q->int_parm);
248 return 0;
249 }
250 return (tmp_cnt - *cnt);
251}
252
253static inline int
254qdio_set_slsb(struct qdio_q *q, unsigned int *bufno,
255 unsigned char state, unsigned int *count)
256{
257 volatile char *slsb;
258 struct qdio_irq *irq;
259
260 irq = (struct qdio_irq*)q->irq_ptr;
261 if (!irq->is_qebsm) {
262 slsb = (char *)&q->slsb.acc.val[(*bufno)];
263 xchg(slsb, state);
264 return 1;
265 }
266 return qdio_do_sqbs(q, state, bufno, count);
267}
268
269#ifdef CONFIG_QDIO_DEBUG
270static inline void
271qdio_trace_slsb(struct qdio_q *q)
272{
273 if (q->queue_type==QDIO_TRACE_QTYPE) {
274 if (q->is_input_q)
275 QDIO_DBF_HEX2(0,slsb_in,&q->slsb,
276 QDIO_MAX_BUFFERS_PER_Q);
277 else
278 QDIO_DBF_HEX2(0,slsb_out,&q->slsb,
279 QDIO_MAX_BUFFERS_PER_Q);
280 }
281}
282#endif
283
284static inline int
285set_slsb(struct qdio_q *q, unsigned int *bufno,
286 unsigned char state, unsigned int *count)
287{
288 int rc;
289#ifdef CONFIG_QDIO_DEBUG
290 qdio_trace_slsb(q);
291#endif
292 rc = qdio_set_slsb(q, bufno, state, count);
293#ifdef CONFIG_QDIO_DEBUG
294 qdio_trace_slsb(q);
295#endif
296 return rc;
297}
298static inline int
299qdio_siga_sync(struct qdio_q *q, unsigned int gpr2,
300 unsigned int gpr3)
301{
302 int cc;
303
304 QDIO_DBF_TEXT4(0,trace,"sigasync");
305 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
306
307 qdio_perf_stat_inc(&perf_stats.siga_syncs);
308
309 cc = do_siga_sync(q->schid, gpr2, gpr3);
310 if (cc)
311 QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));
312
313 return cc;
314}
315
316static inline int
317qdio_siga_sync_q(struct qdio_q *q)
318{
319 if (q->is_input_q)
320 return qdio_siga_sync(q, 0, q->mask);
321 return qdio_siga_sync(q, q->mask, 0);
322}
323
324static int
325__do_siga_output(struct qdio_q *q, unsigned int *busy_bit)
326{
327 struct qdio_irq *irq;
328 unsigned int fc = 0;
329 unsigned long schid;
330
331 irq = (struct qdio_irq *) q->irq_ptr;
332 if (!irq->is_qebsm)
333 schid = *((u32 *)&q->schid);
334 else {
335 schid = irq->sch_token;
336 fc |= 0x80;
337 }
338 return do_siga_output(schid, q->mask, busy_bit, fc);
339}
340
341/*
342 * returns QDIO_SIGA_ERROR_ACCESS_EXCEPTION as cc, when SIGA returns
343 * an access exception
344 */
345static int
346qdio_siga_output(struct qdio_q *q)
347{
348 int cc;
349 __u32 busy_bit;
350 __u64 start_time=0;
351
352 qdio_perf_stat_inc(&perf_stats.siga_outs);
353
354 QDIO_DBF_TEXT4(0,trace,"sigaout");
355 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
356
357 for (;;) {
358 cc = __do_siga_output(q, &busy_bit);
359//QDIO_PRINT_ERR("cc=%x, busy=%x\n",cc,busy_bit);
360 if ((cc==2) && (busy_bit) && (q->is_iqdio_q)) {
361 if (!start_time)
362 start_time=NOW;
363 if ((NOW-start_time)>QDIO_BUSY_BIT_PATIENCE)
364 break;
365 } else
366 break;
367 }
368
369 if ((cc==2) && (busy_bit))
370 cc |= QDIO_SIGA_ERROR_B_BIT_SET;
371
372 if (cc)
373 QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));
374
375 return cc;
376}
377
378static int
379qdio_siga_input(struct qdio_q *q)
380{
381 int cc;
382
383 QDIO_DBF_TEXT4(0,trace,"sigain");
384 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
385
386 qdio_perf_stat_inc(&perf_stats.siga_ins);
387
388 cc = do_siga_input(q->schid, q->mask);
389
390 if (cc)
391 QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));
392
393 return cc;
394}
395
396/* locked by the locks in qdio_activate and qdio_cleanup */
397static __u32 *
398qdio_get_indicator(void)
399{
400 int i;
401
402 for (i = 0; i < INDICATORS_PER_CACHELINE; i++)
403 if (!indicator_used[i]) {
404 indicator_used[i]=1;
405 return indicators+i;
406 }
407 atomic_inc(&spare_indicator_usecount);
408 return (__u32 * volatile) &spare_indicator;
409}
410
411/* locked by the locks in qdio_activate and qdio_cleanup */
412static void
413qdio_put_indicator(__u32 *addr)
414{
415 int i;
416
417 if ( (addr) && (addr!=&spare_indicator) ) {
418 i=addr-indicators;
419 indicator_used[i]=0;
420 }
421 if (addr == &spare_indicator)
422 atomic_dec(&spare_indicator_usecount);
423}
424
425static inline void
426tiqdio_clear_summary_bit(__u32 *location)
427{
428 QDIO_DBF_TEXT5(0,trace,"clrsummb");
429 QDIO_DBF_HEX5(0,trace,&location,sizeof(void*));
430
431 xchg(location,0);
432}
433
434static inline void
435tiqdio_set_summary_bit(__u32 *location)
436{
437 QDIO_DBF_TEXT5(0,trace,"setsummb");
438 QDIO_DBF_HEX5(0,trace,&location,sizeof(void*));
439
440 xchg(location,-1);
441}
442
443static inline void
444tiqdio_sched_tl(void)
445{
446 tasklet_hi_schedule(&tiqdio_tasklet);
447}
448
449static void
450qdio_mark_tiq(struct qdio_q *q)
451{
452 unsigned long flags;
453
454 QDIO_DBF_TEXT4(0,trace,"mark iq");
455 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
456
457 spin_lock_irqsave(&ttiq_list_lock,flags);
458 if (unlikely(atomic_read(&q->is_in_shutdown)))
459 goto out_unlock;
460
461 if (!q->is_input_q)
462 goto out_unlock;
463
464 if ((q->list_prev) || (q->list_next))
465 goto out_unlock;
466
467 if (!tiq_list) {
468 tiq_list=q;
469 q->list_prev=q;
470 q->list_next=q;
471 } else {
472 q->list_next=tiq_list;
473 q->list_prev=tiq_list->list_prev;
474 tiq_list->list_prev->list_next=q;
475 tiq_list->list_prev=q;
476 }
477 spin_unlock_irqrestore(&ttiq_list_lock,flags);
478
479 tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
480 tiqdio_sched_tl();
481 return;
482out_unlock:
483 spin_unlock_irqrestore(&ttiq_list_lock,flags);
484 return;
485}
486
487static inline void
488qdio_mark_q(struct qdio_q *q)
489{
490 QDIO_DBF_TEXT4(0,trace,"mark q");
491 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
492
493 if (unlikely(atomic_read(&q->is_in_shutdown)))
494 return;
495
496 tasklet_schedule(&q->tasklet);
497}
498
499static int
500qdio_stop_polling(struct qdio_q *q)
501{
502#ifdef QDIO_USE_PROCESSING_STATE
503 unsigned int tmp, gsf, count = 1;
504 unsigned char state = 0;
505 struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
506
507 if (!atomic_xchg(&q->polling,0))
508 return 1;
509
510 QDIO_DBF_TEXT4(0,trace,"stoppoll");
511 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
512
513 /* show the card that we are not polling anymore */
514 if (!q->is_input_q)
515 return 1;
516
517 tmp = gsf = GET_SAVED_FRONTIER(q);
518 tmp = ((tmp + QDIO_MAX_BUFFERS_PER_Q-1) & (QDIO_MAX_BUFFERS_PER_Q-1) );
519 set_slsb(q, &tmp, SLSB_P_INPUT_NOT_INIT, &count);
520
521 /*
522 * we don't issue this SYNC_MEMORY, as we trust Rick T and
523 * moreover will not use the PROCESSING state under VM, so
524 * q->polling was 0 anyway
525 */
526 /*SYNC_MEMORY;*/
527 if (irq->is_qebsm) {
528 count = 1;
529 qdio_do_eqbs(q, &state, &gsf, &count);
530 } else
531 state = q->slsb.acc.val[gsf];
532 if (state != SLSB_P_INPUT_PRIMED)
533 return 1;
534 /*
535 * set our summary bit again, as otherwise there is a
536 * small window we can miss between resetting it and
537 * checking for PRIMED state
538 */
539 if (q->is_thinint_q)
540 tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
541 return 0;
542
543#else /* QDIO_USE_PROCESSING_STATE */
544 return 1;
545#endif /* QDIO_USE_PROCESSING_STATE */
546}
547
548/*
549 * see the comment in do_QDIO and before qdio_reserve_q about the
550 * sophisticated locking outside of unmark_q, so that we don't need to
551 * disable the interrupts :-)
552*/
553static void
554qdio_unmark_q(struct qdio_q *q)
555{
556 unsigned long flags;
557
558 QDIO_DBF_TEXT4(0,trace,"unmark q");
559 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
560
561 if ((!q->list_prev)||(!q->list_next))
562 return;
563
564 if ((q->is_thinint_q)&&(q->is_input_q)) {
565 /* iQDIO */
566 spin_lock_irqsave(&ttiq_list_lock,flags);
567 /* in case cleanup has done this already and simultanously
568 * qdio_unmark_q is called from the interrupt handler, we've
569 * got to check this in this specific case again */
570 if ((!q->list_prev)||(!q->list_next))
571 goto out;
572 if (q->list_next==q) {
573 /* q was the only interesting q */
574 tiq_list=NULL;
575 q->list_next=NULL;
576 q->list_prev=NULL;
577 } else {
578 q->list_next->list_prev=q->list_prev;
579 q->list_prev->list_next=q->list_next;
580 tiq_list=q->list_next;
581 q->list_next=NULL;
582 q->list_prev=NULL;
583 }
584out:
585 spin_unlock_irqrestore(&ttiq_list_lock,flags);
586 }
587}
588
589static inline unsigned long
590tiqdio_clear_global_summary(void)
591{
592 unsigned long time;
593
594 QDIO_DBF_TEXT5(0,trace,"clrglobl");
595
596 time = do_clear_global_summary();
597
598 QDIO_DBF_HEX5(0,trace,&time,sizeof(unsigned long));
599
600 return time;
601}
602
603
604/************************* OUTBOUND ROUTINES *******************************/
605static int
606qdio_qebsm_get_outbound_buffer_frontier(struct qdio_q *q)
607{
608 struct qdio_irq *irq;
609 unsigned char state;
610 unsigned int cnt, count, ftc;
611
612 irq = (struct qdio_irq *) q->irq_ptr;
613 if ((!q->is_iqdio_q) && (!q->hydra_gives_outbound_pcis))
614 SYNC_MEMORY;
615
616 ftc = q->first_to_check;
617 count = qdio_min(atomic_read(&q->number_of_buffers_used),
618 (QDIO_MAX_BUFFERS_PER_Q-1));
619 if (count == 0)
620 return q->first_to_check;
621 cnt = qdio_do_eqbs(q, &state, &ftc, &count);
622 if (cnt == 0)
623 return q->first_to_check;
624 switch (state) {
625 case SLSB_P_OUTPUT_ERROR:
626 QDIO_DBF_TEXT3(0,trace,"outperr");
627 atomic_sub(cnt , &q->number_of_buffers_used);
628 if (q->qdio_error)
629 q->error_status_flags |=
630 QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
631 q->qdio_error = SLSB_P_OUTPUT_ERROR;
632 q->error_status_flags |= QDIO_STATUS_LOOK_FOR_ERROR;
633 q->first_to_check = ftc;
634 break;
635 case SLSB_P_OUTPUT_EMPTY:
636 QDIO_DBF_TEXT5(0,trace,"outpempt");
637 atomic_sub(cnt, &q->number_of_buffers_used);
638 q->first_to_check = ftc;
639 break;
640 case SLSB_CU_OUTPUT_PRIMED:
641 /* all buffers primed */
642 QDIO_DBF_TEXT5(0,trace,"outpprim");
643 break;
644 default:
645 break;
646 }
647 QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));
648 return q->first_to_check;
649}
650
651static int
652qdio_qebsm_get_inbound_buffer_frontier(struct qdio_q *q)
653{
654 struct qdio_irq *irq;
655 unsigned char state;
656 int tmp, ftc, count, cnt;
657 char dbf_text[15];
658
659
660 irq = (struct qdio_irq *) q->irq_ptr;
661 ftc = q->first_to_check;
662 count = qdio_min(atomic_read(&q->number_of_buffers_used),
663 (QDIO_MAX_BUFFERS_PER_Q-1));
664 if (count == 0)
665 return q->first_to_check;
666 cnt = qdio_do_eqbs(q, &state, &ftc, &count);
667 if (cnt == 0)
668 return q->first_to_check;
669 switch (state) {
670 case SLSB_P_INPUT_ERROR :
671#ifdef CONFIG_QDIO_DEBUG
672 QDIO_DBF_TEXT3(1,trace,"inperr");
673 sprintf(dbf_text,"%2x,%2x",ftc,count);
674 QDIO_DBF_TEXT3(1,trace,dbf_text);
675#endif /* CONFIG_QDIO_DEBUG */
676 if (q->qdio_error)
677 q->error_status_flags |=
678 QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
679 q->qdio_error = SLSB_P_INPUT_ERROR;
680 q->error_status_flags |= QDIO_STATUS_LOOK_FOR_ERROR;
681 atomic_sub(cnt, &q->number_of_buffers_used);
682 q->first_to_check = ftc;
683 break;
684 case SLSB_P_INPUT_PRIMED :
685 QDIO_DBF_TEXT3(0,trace,"inptprim");
686 sprintf(dbf_text,"%2x,%2x",ftc,count);
687 QDIO_DBF_TEXT3(1,trace,dbf_text);
688 tmp = 0;
689 ftc = q->first_to_check;
690#ifdef QDIO_USE_PROCESSING_STATE
691 if (cnt > 1) {
692 cnt -= 1;
693 tmp = set_slsb(q, &ftc, SLSB_P_INPUT_NOT_INIT, &cnt);
694 if (!tmp)
695 break;
696 }
697 cnt = 1;
698 tmp += set_slsb(q, &ftc,
699 SLSB_P_INPUT_PROCESSING, &cnt);
700 atomic_set(&q->polling, 1);
701#else
702 tmp = set_slsb(q, &ftc, SLSB_P_INPUT_NOT_INIT, &cnt);
703#endif
704 atomic_sub(tmp, &q->number_of_buffers_used);
705 q->first_to_check = ftc;
706 break;
707 case SLSB_CU_INPUT_EMPTY:
708 case SLSB_P_INPUT_NOT_INIT:
709 case SLSB_P_INPUT_PROCESSING:
710 QDIO_DBF_TEXT5(0,trace,"inpnipro");
711 break;
712 default:
713 break;
714 }
715 QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));
716 return q->first_to_check;
717}
718
719static int
720qdio_get_outbound_buffer_frontier(struct qdio_q *q)
721{
722 struct qdio_irq *irq;
723 volatile char *slsb;
724 unsigned int count = 1;
725 int first_not_to_check, f, f_mod_no;
726 char dbf_text[15];
727
728 QDIO_DBF_TEXT4(0,trace,"getobfro");
729 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
730
731 irq = (struct qdio_irq *) q->irq_ptr;
732 if (irq->is_qebsm)
733 return qdio_qebsm_get_outbound_buffer_frontier(q);
734
735 slsb=&q->slsb.acc.val[0];
736 f_mod_no=f=q->first_to_check;
737 /*
738 * f points to already processed elements, so f+no_used is correct...
739 * ... but: we don't check 128 buffers, as otherwise
740 * qdio_has_outbound_q_moved would return 0
741 */
742 first_not_to_check=f+qdio_min(atomic_read(&q->number_of_buffers_used),
743 (QDIO_MAX_BUFFERS_PER_Q-1));
744
745 if (((!q->is_iqdio_q) && (!q->hydra_gives_outbound_pcis)) ||
746 (q->queue_type == QDIO_IQDIO_QFMT_ASYNCH))
747 SYNC_MEMORY;
748
749check_next:
750 if (f==first_not_to_check)
751 goto out;
752
753 switch(slsb[f_mod_no]) {
754
755 /* the adapter has not fetched the output yet */
756 case SLSB_CU_OUTPUT_PRIMED:
757 QDIO_DBF_TEXT5(0,trace,"outpprim");
758 break;
759
760 /* the adapter got it */
761 case SLSB_P_OUTPUT_EMPTY:
762 atomic_dec(&q->number_of_buffers_used);
763 f++;
764 f_mod_no=f&(QDIO_MAX_BUFFERS_PER_Q-1);
765 QDIO_DBF_TEXT5(0,trace,"outpempt");
766 goto check_next;
767
768 case SLSB_P_OUTPUT_ERROR:
769 QDIO_DBF_TEXT3(0,trace,"outperr");
770 sprintf(dbf_text,"%x-%x-%x",f_mod_no,
771 q->sbal[f_mod_no]->element[14].sbalf.value,
772 q->sbal[f_mod_no]->element[15].sbalf.value);
773 QDIO_DBF_TEXT3(1,trace,dbf_text);
774 QDIO_DBF_HEX2(1,sbal,q->sbal[f_mod_no],256);
775
776 /* kind of process the buffer */
777 set_slsb(q, &f_mod_no, SLSB_P_OUTPUT_NOT_INIT, &count);
778
779 /*
780 * we increment the frontier, as this buffer
781 * was processed obviously
782 */
783 atomic_dec(&q->number_of_buffers_used);
784 f_mod_no=(f_mod_no+1)&(QDIO_MAX_BUFFERS_PER_Q-1);
785
786 if (q->qdio_error)
787 q->error_status_flags|=
788 QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
789 q->qdio_error=SLSB_P_OUTPUT_ERROR;
790 q->error_status_flags|=QDIO_STATUS_LOOK_FOR_ERROR;
791
792 break;
793
794 /* no new buffers */
795 default:
796 QDIO_DBF_TEXT5(0,trace,"outpni");
797 }
798out:
799 return (q->first_to_check=f_mod_no);
800}
801
802/* all buffers are processed */
803static int
804qdio_is_outbound_q_done(struct qdio_q *q)
805{
806 int no_used;
807#ifdef CONFIG_QDIO_DEBUG
808 char dbf_text[15];
809#endif
810
811 no_used=atomic_read(&q->number_of_buffers_used);
812
813#ifdef CONFIG_QDIO_DEBUG
814 if (no_used) {
815 sprintf(dbf_text,"oqisnt%02x",no_used);
816 QDIO_DBF_TEXT4(0,trace,dbf_text);
817 } else {
818 QDIO_DBF_TEXT4(0,trace,"oqisdone");
819 }
820 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
821#endif /* CONFIG_QDIO_DEBUG */
822 return (no_used==0);
823}
824
825static int
826qdio_has_outbound_q_moved(struct qdio_q *q)
827{
828 int i;
829
830 i=qdio_get_outbound_buffer_frontier(q);
831
832 if ( (i!=GET_SAVED_FRONTIER(q)) ||
833 (q->error_status_flags&QDIO_STATUS_LOOK_FOR_ERROR) ) {
834 SAVE_FRONTIER(q,i);
835 QDIO_DBF_TEXT4(0,trace,"oqhasmvd");
836 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
837 return 1;
838 } else {
839 QDIO_DBF_TEXT4(0,trace,"oqhsntmv");
840 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
841 return 0;
842 }
843}
844
845static void
846qdio_kick_outbound_q(struct qdio_q *q)
847{
848 int result;
849#ifdef CONFIG_QDIO_DEBUG
850 char dbf_text[15];
851
852 QDIO_DBF_TEXT4(0,trace,"kickoutq");
853 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
854#endif /* CONFIG_QDIO_DEBUG */
855
856 if (!q->siga_out)
857 return;
858
859 /* here's the story with cc=2 and busy bit set (thanks, Rick):
860 * VM's CP could present us cc=2 and busy bit set on SIGA-write
861 * during reconfiguration of their Guest LAN (only in HIPERS mode,
862 * QDIO mode is asynchronous -- cc=2 and busy bit there will take
863 * the queues down immediately; and not being under VM we have a
864 * problem on cc=2 and busy bit set right away).
865 *
866 * Therefore qdio_siga_output will try for a short time constantly,
867 * if such a condition occurs. If it doesn't change, it will
868 * increase the busy_siga_counter and save the timestamp, and
869 * schedule the queue for later processing (via mark_q, using the
870 * queue tasklet). __qdio_outbound_processing will check out the
871 * counter. If non-zero, it will call qdio_kick_outbound_q as often
872 * as the value of the counter. This will attempt further SIGA
873 * instructions. For each successful SIGA, the counter is
874 * decreased, for failing SIGAs the counter remains the same, after
875 * all.
876 * After some time of no movement, qdio_kick_outbound_q will
877 * finally fail and reflect corresponding error codes to call
878 * the upper layer module and have it take the queues down.
879 *
880 * Note that this is a change from the original HiperSockets design
881 * (saying cc=2 and busy bit means take the queues down), but in
882 * these days Guest LAN didn't exist... excessive cc=2 with busy bit
883 * conditions will still take the queues down, but the threshold is
884 * higher due to the Guest LAN environment.
885 */
886
887
888 result=qdio_siga_output(q);
889
890 switch (result) {
891 case 0:
892 /* went smooth this time, reset timestamp */
893#ifdef CONFIG_QDIO_DEBUG
894 QDIO_DBF_TEXT3(0,trace,"cc2reslv");
895 sprintf(dbf_text,"%4x%2x%2x",q->schid.sch_no,q->q_no,
896 atomic_read(&q->busy_siga_counter));
897 QDIO_DBF_TEXT3(0,trace,dbf_text);
898#endif /* CONFIG_QDIO_DEBUG */
899 q->timing.busy_start=0;
900 break;
901 case (2|QDIO_SIGA_ERROR_B_BIT_SET):
902 /* cc=2 and busy bit: */
903 atomic_inc(&q->busy_siga_counter);
904
905 /* if the last siga was successful, save
906 * timestamp here */
907 if (!q->timing.busy_start)
908 q->timing.busy_start=NOW;
909
910 /* if we're in time, don't touch error_status_flags
911 * and siga_error */
912 if (NOW-q->timing.busy_start<QDIO_BUSY_BIT_GIVE_UP) {
913 qdio_mark_q(q);
914 break;
915 }
916 QDIO_DBF_TEXT2(0,trace,"cc2REPRT");
917#ifdef CONFIG_QDIO_DEBUG
918 sprintf(dbf_text,"%4x%2x%2x",q->schid.sch_no,q->q_no,
919 atomic_read(&q->busy_siga_counter));
920 QDIO_DBF_TEXT3(0,trace,dbf_text);
921#endif /* CONFIG_QDIO_DEBUG */
922 /* else fallthrough and report error */
923 default:
924 /* for plain cc=1, 2 or 3: */
925 if (q->siga_error)
926 q->error_status_flags|=
927 QDIO_STATUS_MORE_THAN_ONE_SIGA_ERROR;
928 q->error_status_flags|=
929 QDIO_STATUS_LOOK_FOR_ERROR;
930 q->siga_error=result;
931 }
932}
933
934static void
935qdio_kick_outbound_handler(struct qdio_q *q)
936{
937 int start, end, real_end, count;
938#ifdef CONFIG_QDIO_DEBUG
939 char dbf_text[15];
940#endif
941
942 start = q->first_element_to_kick;
943 /* last_move_ftc was just updated */
944 real_end = GET_SAVED_FRONTIER(q);
945 end = (real_end+QDIO_MAX_BUFFERS_PER_Q-1)&
946 (QDIO_MAX_BUFFERS_PER_Q-1);
947 count = (end+QDIO_MAX_BUFFERS_PER_Q+1-start)&
948 (QDIO_MAX_BUFFERS_PER_Q-1);
949
950#ifdef CONFIG_QDIO_DEBUG
951 QDIO_DBF_TEXT4(0,trace,"kickouth");
952 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
953
954 sprintf(dbf_text,"s=%2xc=%2x",start,count);
955 QDIO_DBF_TEXT4(0,trace,dbf_text);
956#endif /* CONFIG_QDIO_DEBUG */
957
958 if (q->state==QDIO_IRQ_STATE_ACTIVE)
959 q->handler(q->cdev,QDIO_STATUS_OUTBOUND_INT|
960 q->error_status_flags,
961 q->qdio_error,q->siga_error,q->q_no,start,count,
962 q->int_parm);
963
964 /* for the next time: */
965 q->first_element_to_kick=real_end;
966 q->qdio_error=0;
967 q->siga_error=0;
968 q->error_status_flags=0;
969}
970
971static void
972__qdio_outbound_processing(struct qdio_q *q)
973{
974 int siga_attempts;
975
976 QDIO_DBF_TEXT4(0,trace,"qoutproc");
977 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
978
979 if (unlikely(qdio_reserve_q(q))) {
980 qdio_release_q(q);
981 qdio_perf_stat_inc(&perf_stats.outbound_tl_runs_resched);
982 /* as we're sissies, we'll check next time */
983 if (likely(!atomic_read(&q->is_in_shutdown))) {
984 qdio_mark_q(q);
985 QDIO_DBF_TEXT4(0,trace,"busy,agn");
986 }
987 return;
988 }
989 qdio_perf_stat_inc(&perf_stats.outbound_tl_runs);
990 qdio_perf_stat_inc(&perf_stats.tl_runs);
991
992 /* see comment in qdio_kick_outbound_q */
993 siga_attempts=atomic_read(&q->busy_siga_counter);
994 while (siga_attempts) {
995 atomic_dec(&q->busy_siga_counter);
996 qdio_kick_outbound_q(q);
997 siga_attempts--;
998 }
999
1000 if (qdio_has_outbound_q_moved(q))
1001 qdio_kick_outbound_handler(q);
1002
1003 if (q->queue_type == QDIO_ZFCP_QFMT) {
1004 if ((!q->hydra_gives_outbound_pcis) &&
1005 (!qdio_is_outbound_q_done(q)))
1006 qdio_mark_q(q);
1007 }
1008 else if (((!q->is_iqdio_q) && (!q->is_pci_out)) ||
1009 (q->queue_type == QDIO_IQDIO_QFMT_ASYNCH)) {
1010 /*
1011 * make sure buffer switch from PRIMED to EMPTY is noticed
1012 * and outbound_handler is called
1013 */
1014 if (qdio_is_outbound_q_done(q)) {
1015 del_timer(&q->timer);
1016 } else {
1017 if (!timer_pending(&q->timer))
1018 mod_timer(&q->timer, jiffies +
1019 QDIO_FORCE_CHECK_TIMEOUT);
1020 }
1021 }
1022
1023 qdio_release_q(q);
1024}
1025
1026static void
1027qdio_outbound_processing(unsigned long q)
1028{
1029 __qdio_outbound_processing((struct qdio_q *) q);
1030}
1031
1032/************************* INBOUND ROUTINES *******************************/
1033
1034
1035static int
1036qdio_get_inbound_buffer_frontier(struct qdio_q *q)
1037{
1038 struct qdio_irq *irq;
1039 int f,f_mod_no;
1040 volatile char *slsb;
1041 unsigned int count = 1;
1042 int first_not_to_check;
1043#ifdef CONFIG_QDIO_DEBUG
1044 char dbf_text[15];
1045#endif /* CONFIG_QDIO_DEBUG */
1046#ifdef QDIO_USE_PROCESSING_STATE
1047 int last_position=-1;
1048#endif /* QDIO_USE_PROCESSING_STATE */
1049
1050 QDIO_DBF_TEXT4(0,trace,"getibfro");
1051 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1052
1053 irq = (struct qdio_irq *) q->irq_ptr;
1054 if (irq->is_qebsm)
1055 return qdio_qebsm_get_inbound_buffer_frontier(q);
1056
1057 slsb=&q->slsb.acc.val[0];
1058 f_mod_no=f=q->first_to_check;
1059 /*
1060 * we don't check 128 buffers, as otherwise qdio_has_inbound_q_moved
1061 * would return 0
1062 */
1063 first_not_to_check=f+qdio_min(atomic_read(&q->number_of_buffers_used),
1064 (QDIO_MAX_BUFFERS_PER_Q-1));
1065
1066 /*
1067 * we don't use this one, as a PCI or we after a thin interrupt
1068 * will sync the queues
1069 */
1070 /* SYNC_MEMORY;*/
1071
1072check_next:
1073 f_mod_no=f&(QDIO_MAX_BUFFERS_PER_Q-1);
1074 if (f==first_not_to_check)
1075 goto out;
1076 switch (slsb[f_mod_no]) {
1077
1078 /* CU_EMPTY means frontier is reached */
1079 case SLSB_CU_INPUT_EMPTY:
1080 QDIO_DBF_TEXT5(0,trace,"inptempt");
1081 break;
1082
1083 /* P_PRIMED means set slsb to P_PROCESSING and move on */
1084 case SLSB_P_INPUT_PRIMED:
1085 QDIO_DBF_TEXT5(0,trace,"inptprim");
1086
1087#ifdef QDIO_USE_PROCESSING_STATE
1088 /*
1089 * as soon as running under VM, polling the input queues will
1090 * kill VM in terms of CP overhead
1091 */
1092 if (q->siga_sync) {
1093 set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
1094 } else {
1095 /* set the previous buffer to NOT_INIT. The current
1096 * buffer will be set to PROCESSING at the end of
1097 * this function to avoid further interrupts. */
1098 if (last_position>=0)
1099 set_slsb(q, &last_position,
1100 SLSB_P_INPUT_NOT_INIT, &count);
1101 atomic_set(&q->polling,1);
1102 last_position=f_mod_no;
1103 }
1104#else /* QDIO_USE_PROCESSING_STATE */
1105 set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
1106#endif /* QDIO_USE_PROCESSING_STATE */
1107 /*
1108 * not needed, as the inbound queue will be synced on the next
1109 * siga-r, resp. tiqdio_is_inbound_q_done will do the siga-s
1110 */
1111 /*SYNC_MEMORY;*/
1112 f++;
1113 atomic_dec(&q->number_of_buffers_used);
1114 goto check_next;
1115
1116 case SLSB_P_INPUT_NOT_INIT:
1117 case SLSB_P_INPUT_PROCESSING:
1118 QDIO_DBF_TEXT5(0,trace,"inpnipro");
1119 break;
1120
1121 /* P_ERROR means frontier is reached, break and report error */
1122 case SLSB_P_INPUT_ERROR:
1123#ifdef CONFIG_QDIO_DEBUG
1124 sprintf(dbf_text,"inperr%2x",f_mod_no);
1125 QDIO_DBF_TEXT3(1,trace,dbf_text);
1126#endif /* CONFIG_QDIO_DEBUG */
1127 QDIO_DBF_HEX2(1,sbal,q->sbal[f_mod_no],256);
1128
1129 /* kind of process the buffer */
1130 set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
1131
1132 if (q->qdio_error)
1133 q->error_status_flags|=
1134 QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
1135 q->qdio_error=SLSB_P_INPUT_ERROR;
1136 q->error_status_flags|=QDIO_STATUS_LOOK_FOR_ERROR;
1137
1138 /* we increment the frontier, as this buffer
1139 * was processed obviously */
1140 f_mod_no=(f_mod_no+1)&(QDIO_MAX_BUFFERS_PER_Q-1);
1141 atomic_dec(&q->number_of_buffers_used);
1142
1143#ifdef QDIO_USE_PROCESSING_STATE
1144 last_position=-1;
1145#endif /* QDIO_USE_PROCESSING_STATE */
1146
1147 break;
1148
1149 /* everything else means frontier not changed (HALTED or so) */
1150 default:
1151 break;
1152 }
1153out:
1154 q->first_to_check=f_mod_no;
1155
1156#ifdef QDIO_USE_PROCESSING_STATE
1157 if (last_position>=0)
1158 set_slsb(q, &last_position, SLSB_P_INPUT_PROCESSING, &count);
1159#endif /* QDIO_USE_PROCESSING_STATE */
1160
1161 QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));
1162
1163 return q->first_to_check;
1164}
1165
1166static int
1167qdio_has_inbound_q_moved(struct qdio_q *q)
1168{
1169 int i;
1170
1171 i=qdio_get_inbound_buffer_frontier(q);
1172 if ( (i!=GET_SAVED_FRONTIER(q)) ||
1173 (q->error_status_flags&QDIO_STATUS_LOOK_FOR_ERROR) ) {
1174 SAVE_FRONTIER(q,i);
1175 if ((!q->siga_sync)&&(!q->hydra_gives_outbound_pcis))
1176 SAVE_TIMESTAMP(q);
1177
1178 QDIO_DBF_TEXT4(0,trace,"inhasmvd");
1179 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1180 return 1;
1181 } else {
1182 QDIO_DBF_TEXT4(0,trace,"inhsntmv");
1183 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1184 return 0;
1185 }
1186}
1187
1188/* means, no more buffers to be filled */
1189static int
1190tiqdio_is_inbound_q_done(struct qdio_q *q)
1191{
1192 int no_used;
1193 unsigned int start_buf, count;
1194 unsigned char state = 0;
1195 struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
1196
1197#ifdef CONFIG_QDIO_DEBUG
1198 char dbf_text[15];
1199#endif
1200
1201 no_used=atomic_read(&q->number_of_buffers_used);
1202
1203 /* propagate the change from 82 to 80 through VM */
1204 SYNC_MEMORY;
1205
1206#ifdef CONFIG_QDIO_DEBUG
1207 if (no_used) {
1208 sprintf(dbf_text,"iqisnt%02x",no_used);
1209 QDIO_DBF_TEXT4(0,trace,dbf_text);
1210 } else {
1211 QDIO_DBF_TEXT4(0,trace,"iniqisdo");
1212 }
1213 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1214#endif /* CONFIG_QDIO_DEBUG */
1215
1216 if (!no_used)
1217 return 1;
1218 if (irq->is_qebsm) {
1219 count = 1;
1220 start_buf = q->first_to_check;
1221 qdio_do_eqbs(q, &state, &start_buf, &count);
1222 } else
1223 state = q->slsb.acc.val[q->first_to_check];
1224 if (state != SLSB_P_INPUT_PRIMED)
1225 /*
1226 * nothing more to do, if next buffer is not PRIMED.
1227 * note that we did a SYNC_MEMORY before, that there
1228 * has been a sychnronization.
1229 * we will return 0 below, as there is nothing to do
1230 * (stop_polling not necessary, as we have not been
1231 * using the PROCESSING state
1232 */
1233 return 0;
1234
1235 /*
1236 * ok, the next input buffer is primed. that means, that device state
1237 * change indicator and adapter local summary are set, so we will find
1238 * it next time.
1239 * we will return 0 below, as there is nothing to do, except scheduling
1240 * ourselves for the next time.
1241 */
1242 tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
1243 tiqdio_sched_tl();
1244 return 0;
1245}
1246
1247static int
1248qdio_is_inbound_q_done(struct qdio_q *q)
1249{
1250 int no_used;
1251 unsigned int start_buf, count;
1252 unsigned char state = 0;
1253 struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
1254
1255#ifdef CONFIG_QDIO_DEBUG
1256 char dbf_text[15];
1257#endif
1258
1259 no_used=atomic_read(&q->number_of_buffers_used);
1260
1261 /*
1262 * we need that one for synchronization with the adapter, as it
1263 * does a kind of PCI avoidance
1264 */
1265 SYNC_MEMORY;
1266
1267 if (!no_used) {
1268 QDIO_DBF_TEXT4(0,trace,"inqisdnA");
1269 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1270 return 1;
1271 }
1272 if (irq->is_qebsm) {
1273 count = 1;
1274 start_buf = q->first_to_check;
1275 qdio_do_eqbs(q, &state, &start_buf, &count);
1276 } else
1277 state = q->slsb.acc.val[q->first_to_check];
1278 if (state == SLSB_P_INPUT_PRIMED) {
1279 /* we got something to do */
1280 QDIO_DBF_TEXT4(0,trace,"inqisntA");
1281 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1282 return 0;
1283 }
1284
1285 /* on VM, we don't poll, so the q is always done here */
1286 if (q->siga_sync)
1287 return 1;
1288 if (q->hydra_gives_outbound_pcis)
1289 return 1;
1290
1291 /*
1292 * at this point we know, that inbound first_to_check
1293 * has (probably) not moved (see qdio_inbound_processing)
1294 */
1295 if (NOW>GET_SAVED_TIMESTAMP(q)+q->timing.threshold) {
1296#ifdef CONFIG_QDIO_DEBUG
1297 QDIO_DBF_TEXT4(0,trace,"inqisdon");
1298 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1299 sprintf(dbf_text,"pf%02xcn%02x",q->first_to_check,no_used);
1300 QDIO_DBF_TEXT4(0,trace,dbf_text);
1301#endif /* CONFIG_QDIO_DEBUG */
1302 return 1;
1303 } else {
1304#ifdef CONFIG_QDIO_DEBUG
1305 QDIO_DBF_TEXT4(0,trace,"inqisntd");
1306 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1307 sprintf(dbf_text,"pf%02xcn%02x",q->first_to_check,no_used);
1308 QDIO_DBF_TEXT4(0,trace,dbf_text);
1309#endif /* CONFIG_QDIO_DEBUG */
1310 return 0;
1311 }
1312}
1313
1314static void
1315qdio_kick_inbound_handler(struct qdio_q *q)
1316{
1317 int count, start, end, real_end, i;
1318#ifdef CONFIG_QDIO_DEBUG
1319 char dbf_text[15];
1320#endif
1321
1322 QDIO_DBF_TEXT4(0,trace,"kickinh");
1323 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1324
1325 start=q->first_element_to_kick;
1326 real_end=q->first_to_check;
1327 end=(real_end+QDIO_MAX_BUFFERS_PER_Q-1)&(QDIO_MAX_BUFFERS_PER_Q-1);
1328
1329 i=start;
1330 count=0;
1331 while (1) {
1332 count++;
1333 if (i==end)
1334 break;
1335 i=(i+1)&(QDIO_MAX_BUFFERS_PER_Q-1);
1336 }
1337
1338#ifdef CONFIG_QDIO_DEBUG
1339 sprintf(dbf_text,"s=%2xc=%2x",start,count);
1340 QDIO_DBF_TEXT4(0,trace,dbf_text);
1341#endif /* CONFIG_QDIO_DEBUG */
1342
1343 if (likely(q->state==QDIO_IRQ_STATE_ACTIVE))
1344 q->handler(q->cdev,
1345 QDIO_STATUS_INBOUND_INT|q->error_status_flags,
1346 q->qdio_error,q->siga_error,q->q_no,start,count,
1347 q->int_parm);
1348
1349 /* for the next time: */
1350 q->first_element_to_kick=real_end;
1351 q->qdio_error=0;
1352 q->siga_error=0;
1353 q->error_status_flags=0;
1354
1355 qdio_perf_stat_inc(&perf_stats.inbound_cnt);
1356}
1357
1358static void
1359__tiqdio_inbound_processing(struct qdio_q *q, int spare_ind_was_set)
1360{
1361 struct qdio_irq *irq_ptr;
1362 struct qdio_q *oq;
1363 int i;
1364
1365 QDIO_DBF_TEXT4(0,trace,"iqinproc");
1366 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1367
1368 /*
1369 * we first want to reserve the q, so that we know, that we don't
1370 * interrupt ourselves and call qdio_unmark_q, as is_in_shutdown might
1371 * be set
1372 */
1373 if (unlikely(qdio_reserve_q(q))) {
1374 qdio_release_q(q);
1375 qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs_resched);
1376 /*
1377 * as we might just be about to stop polling, we make
1378 * sure that we check again at least once more
1379 */
1380 tiqdio_sched_tl();
1381 return;
1382 }
1383 qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs);
1384 if (unlikely(atomic_read(&q->is_in_shutdown))) {
1385 qdio_unmark_q(q);
1386 goto out;
1387 }
1388
1389 /*
1390 * we reset spare_ind_was_set, when the queue does not use the
1391 * spare indicator
1392 */
1393 if (spare_ind_was_set)
1394 spare_ind_was_set = (q->dev_st_chg_ind == &spare_indicator);
1395
1396 if (!(*(q->dev_st_chg_ind)) && !spare_ind_was_set)
1397 goto out;
1398 /*
1399 * q->dev_st_chg_ind is the indicator, be it shared or not.
1400 * only clear it, if indicator is non-shared
1401 */
1402 if (q->dev_st_chg_ind != &spare_indicator)
1403 tiqdio_clear_summary_bit((__u32*)q->dev_st_chg_ind);
1404
1405 if (q->hydra_gives_outbound_pcis) {
1406 if (!q->siga_sync_done_on_thinints) {
1407 SYNC_MEMORY_ALL;
1408 } else if (!q->siga_sync_done_on_outb_tis) {
1409 SYNC_MEMORY_ALL_OUTB;
1410 }
1411 } else {
1412 SYNC_MEMORY;
1413 }
1414 /*
1415 * maybe we have to do work on our outbound queues... at least
1416 * we have to check the outbound-int-capable thinint-capable
1417 * queues
1418 */
1419 if (q->hydra_gives_outbound_pcis) {
1420 irq_ptr = (struct qdio_irq*)q->irq_ptr;
1421 for (i=0;i<irq_ptr->no_output_qs;i++) {
1422 oq = irq_ptr->output_qs[i];
1423 if (!qdio_is_outbound_q_done(oq)) {
1424 qdio_perf_stat_dec(&perf_stats.tl_runs);
1425 __qdio_outbound_processing(oq);
1426 }
1427 }
1428 }
1429
1430 if (!qdio_has_inbound_q_moved(q))
1431 goto out;
1432
1433 qdio_kick_inbound_handler(q);
1434 if (tiqdio_is_inbound_q_done(q))
1435 if (!qdio_stop_polling(q)) {
1436 /*
1437 * we set the flags to get into the stuff next time,
1438 * see also comment in qdio_stop_polling
1439 */
1440 tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
1441 tiqdio_sched_tl();
1442 }
1443out:
1444 qdio_release_q(q);
1445}
1446
1447static void
1448tiqdio_inbound_processing(unsigned long q)
1449{
1450 __tiqdio_inbound_processing((struct qdio_q *) q,
1451 atomic_read(&spare_indicator_usecount));
1452}
1453
1454static void
1455__qdio_inbound_processing(struct qdio_q *q)
1456{
1457 int q_laps=0;
1458
1459 QDIO_DBF_TEXT4(0,trace,"qinproc");
1460 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1461
1462 if (unlikely(qdio_reserve_q(q))) {
1463 qdio_release_q(q);
1464 qdio_perf_stat_inc(&perf_stats.inbound_tl_runs_resched);
1465 /* as we're sissies, we'll check next time */
1466 if (likely(!atomic_read(&q->is_in_shutdown))) {
1467 qdio_mark_q(q);
1468 QDIO_DBF_TEXT4(0,trace,"busy,agn");
1469 }
1470 return;
1471 }
1472 qdio_perf_stat_inc(&perf_stats.inbound_tl_runs);
1473 qdio_perf_stat_inc(&perf_stats.tl_runs);
1474
1475again:
1476 if (qdio_has_inbound_q_moved(q)) {
1477 qdio_kick_inbound_handler(q);
1478 if (!qdio_stop_polling(q)) {
1479 q_laps++;
1480 if (q_laps<QDIO_Q_LAPS)
1481 goto again;
1482 }
1483 qdio_mark_q(q);
1484 } else {
1485 if (!qdio_is_inbound_q_done(q))
1486 /* means poll time is not yet over */
1487 qdio_mark_q(q);
1488 }
1489
1490 qdio_release_q(q);
1491}
1492
1493static void
1494qdio_inbound_processing(unsigned long q)
1495{
1496 __qdio_inbound_processing((struct qdio_q *) q);
1497}
1498
1499/************************* MAIN ROUTINES *******************************/
1500
1501#ifdef QDIO_USE_PROCESSING_STATE
1502static int
1503tiqdio_reset_processing_state(struct qdio_q *q, int q_laps)
1504{
1505 if (!q) {
1506 tiqdio_sched_tl();
1507 return 0;
1508 }
1509
1510 /*
1511 * under VM, we have not used the PROCESSING state, so no
1512 * need to stop polling
1513 */
1514 if (q->siga_sync)
1515 return 2;
1516
1517 if (unlikely(qdio_reserve_q(q))) {
1518 qdio_release_q(q);
1519 qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs_resched);
1520 /*
1521 * as we might just be about to stop polling, we make
1522 * sure that we check again at least once more
1523 */
1524
1525 /*
1526 * sanity -- we'd get here without setting the
1527 * dev st chg ind
1528 */
1529 tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
1530 tiqdio_sched_tl();
1531 return 0;
1532 }
1533 if (qdio_stop_polling(q)) {
1534 qdio_release_q(q);
1535 return 2;
1536 }
1537 if (q_laps<QDIO_Q_LAPS-1) {
1538 qdio_release_q(q);
1539 return 3;
1540 }
1541 /*
1542 * we set the flags to get into the stuff
1543 * next time, see also comment in qdio_stop_polling
1544 */
1545 tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
1546 tiqdio_sched_tl();
1547 qdio_release_q(q);
1548 return 1;
1549
1550}
1551#endif /* QDIO_USE_PROCESSING_STATE */
1552
1553static void
1554tiqdio_inbound_checks(void)
1555{
1556 struct qdio_q *q;
1557 int spare_ind_was_set=0;
1558#ifdef QDIO_USE_PROCESSING_STATE
1559 int q_laps=0;
1560#endif /* QDIO_USE_PROCESSING_STATE */
1561
1562 QDIO_DBF_TEXT4(0,trace,"iqdinbck");
1563 QDIO_DBF_TEXT5(0,trace,"iqlocsum");
1564
1565#ifdef QDIO_USE_PROCESSING_STATE
1566again:
1567#endif /* QDIO_USE_PROCESSING_STATE */
1568
1569 /* when the spare indicator is used and set, save that and clear it */
1570 if ((atomic_read(&spare_indicator_usecount)) && spare_indicator) {
1571 spare_ind_was_set = 1;
1572 tiqdio_clear_summary_bit((__u32*)&spare_indicator);
1573 }
1574
1575 q=(struct qdio_q*)tiq_list;
1576 do {
1577 if (!q)
1578 break;
1579 __tiqdio_inbound_processing(q, spare_ind_was_set);
1580 q=(struct qdio_q*)q->list_next;
1581 } while (q!=(struct qdio_q*)tiq_list);
1582
1583#ifdef QDIO_USE_PROCESSING_STATE
1584 q=(struct qdio_q*)tiq_list;
1585 do {
1586 int ret;
1587
1588 ret = tiqdio_reset_processing_state(q, q_laps);
1589 switch (ret) {
1590 case 0:
1591 return;
1592 case 1:
1593 q_laps++;
1594 case 2:
1595 q = (struct qdio_q*)q->list_next;
1596 break;
1597 default:
1598 q_laps++;
1599 goto again;
1600 }
1601 } while (q!=(struct qdio_q*)tiq_list);
1602#endif /* QDIO_USE_PROCESSING_STATE */
1603}
1604
1605static void
1606tiqdio_tl(unsigned long data)
1607{
1608 QDIO_DBF_TEXT4(0,trace,"iqdio_tl");
1609
1610 qdio_perf_stat_inc(&perf_stats.tl_runs);
1611
1612 tiqdio_inbound_checks();
1613}
1614
1615/********************* GENERAL HELPER_ROUTINES ***********************/
1616
1617static void
1618qdio_release_irq_memory(struct qdio_irq *irq_ptr)
1619{
1620 int i;
1621 struct qdio_q *q;
1622
1623 for (i = 0; i < QDIO_MAX_QUEUES_PER_IRQ; i++) {
1624 q = irq_ptr->input_qs[i];
1625 if (q) {
1626 free_page((unsigned long) q->slib);
1627 kmem_cache_free(qdio_q_cache, q);
1628 }
1629 q = irq_ptr->output_qs[i];
1630 if (q) {
1631 free_page((unsigned long) q->slib);
1632 kmem_cache_free(qdio_q_cache, q);
1633 }
1634 }
1635 free_page((unsigned long) irq_ptr->qdr);
1636 free_page((unsigned long) irq_ptr);
1637}
1638
1639static void
1640qdio_set_impl_params(struct qdio_irq *irq_ptr,
1641 unsigned int qib_param_field_format,
1642 /* pointer to 128 bytes or NULL, if no param field */
1643 unsigned char *qib_param_field,
1644 /* pointer to no_queues*128 words of data or NULL */
1645 unsigned int no_input_qs,
1646 unsigned int no_output_qs,
1647 unsigned long *input_slib_elements,
1648 unsigned long *output_slib_elements)
1649{
1650 int i,j;
1651
1652 if (!irq_ptr)
1653 return;
1654
1655 irq_ptr->qib.pfmt=qib_param_field_format;
1656 if (qib_param_field)
1657 memcpy(irq_ptr->qib.parm,qib_param_field,
1658 QDIO_MAX_BUFFERS_PER_Q);
1659
1660 if (input_slib_elements)
1661 for (i=0;i<no_input_qs;i++) {
1662 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1663 irq_ptr->input_qs[i]->slib->slibe[j].parms=
1664 input_slib_elements[
1665 i*QDIO_MAX_BUFFERS_PER_Q+j];
1666 }
1667 if (output_slib_elements)
1668 for (i=0;i<no_output_qs;i++) {
1669 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1670 irq_ptr->output_qs[i]->slib->slibe[j].parms=
1671 output_slib_elements[
1672 i*QDIO_MAX_BUFFERS_PER_Q+j];
1673 }
1674}
1675
1676static int
1677qdio_alloc_qs(struct qdio_irq *irq_ptr,
1678 int no_input_qs, int no_output_qs)
1679{
1680 int i;
1681 struct qdio_q *q;
1682
1683 for (i = 0; i < no_input_qs; i++) {
1684 q = kmem_cache_alloc(qdio_q_cache, GFP_KERNEL);
1685 if (!q)
1686 return -ENOMEM;
1687 memset(q, 0, sizeof(*q));
1688
1689 q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
1690 if (!q->slib) {
1691 kmem_cache_free(qdio_q_cache, q);
1692 return -ENOMEM;
1693 }
1694 irq_ptr->input_qs[i]=q;
1695 }
1696
1697 for (i = 0; i < no_output_qs; i++) {
1698 q = kmem_cache_alloc(qdio_q_cache, GFP_KERNEL);
1699 if (!q)
1700 return -ENOMEM;
1701 memset(q, 0, sizeof(*q));
1702
1703 q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
1704 if (!q->slib) {
1705 kmem_cache_free(qdio_q_cache, q);
1706 return -ENOMEM;
1707 }
1708 irq_ptr->output_qs[i]=q;
1709 }
1710 return 0;
1711}
1712
1713static void
1714qdio_fill_qs(struct qdio_irq *irq_ptr, struct ccw_device *cdev,
1715 int no_input_qs, int no_output_qs,
1716 qdio_handler_t *input_handler,
1717 qdio_handler_t *output_handler,
1718 unsigned long int_parm,int q_format,
1719 unsigned long flags,
1720 void **inbound_sbals_array,
1721 void **outbound_sbals_array)
1722{
1723 struct qdio_q *q;
1724 int i,j;
1725 char dbf_text[20]; /* see qdio_initialize */
1726 void *ptr;
1727 int available;
1728
1729 sprintf(dbf_text,"qfqs%4x",cdev->private->schid.sch_no);
1730 QDIO_DBF_TEXT0(0,setup,dbf_text);
1731 for (i=0;i<no_input_qs;i++) {
1732 q=irq_ptr->input_qs[i];
1733
1734 memset(q,0,((char*)&q->slib)-((char*)q));
1735 sprintf(dbf_text,"in-q%4x",i);
1736 QDIO_DBF_TEXT0(0,setup,dbf_text);
1737 QDIO_DBF_HEX0(0,setup,&q,sizeof(void*));
1738
1739 memset(q->slib,0,PAGE_SIZE);
1740 q->sl=(struct sl*)(((char*)q->slib)+PAGE_SIZE/2);
1741
1742 available=0;
1743
1744 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1745 q->sbal[j]=*(inbound_sbals_array++);
1746
1747 q->queue_type=q_format;
1748 q->int_parm=int_parm;
1749 q->schid = irq_ptr->schid;
1750 q->irq_ptr = irq_ptr;
1751 q->cdev = cdev;
1752 q->mask=1<<(31-i);
1753 q->q_no=i;
1754 q->is_input_q=1;
1755 q->first_to_check=0;
1756 q->last_move_ftc=0;
1757 q->handler=input_handler;
1758 q->dev_st_chg_ind=irq_ptr->dev_st_chg_ind;
1759
1760 /* q->is_thinint_q isn't valid at this time, but
1761 * irq_ptr->is_thinint_irq is
1762 */
1763 if (irq_ptr->is_thinint_irq)
1764 tasklet_init(&q->tasklet, tiqdio_inbound_processing,
1765 (unsigned long) q);
1766 else
1767 tasklet_init(&q->tasklet, qdio_inbound_processing,
1768 (unsigned long) q);
1769
1770 /* actually this is not used for inbound queues. yet. */
1771 atomic_set(&q->busy_siga_counter,0);
1772 q->timing.busy_start=0;
1773
1774/* for (j=0;j<QDIO_STATS_NUMBER;j++)
1775 q->timing.last_transfer_times[j]=(qdio_get_micros()/
1776 QDIO_STATS_NUMBER)*j;
1777 q->timing.last_transfer_index=QDIO_STATS_NUMBER-1;
1778*/
1779
1780 /* fill in slib */
1781 if (i>0) irq_ptr->input_qs[i-1]->slib->nsliba=
1782 (unsigned long)(q->slib);
1783 q->slib->sla=(unsigned long)(q->sl);
1784 q->slib->slsba=(unsigned long)(&q->slsb.acc.val[0]);
1785
1786 /* fill in sl */
1787 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1788 q->sl->element[j].sbal=(unsigned long)(q->sbal[j]);
1789
1790 QDIO_DBF_TEXT2(0,setup,"sl-sb-b0");
1791 ptr=(void*)q->sl;
1792 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1793 ptr=(void*)&q->slsb;
1794 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1795 ptr=(void*)q->sbal[0];
1796 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1797
1798 /* fill in slsb */
1799 if (!irq_ptr->is_qebsm) {
1800 unsigned int count = 1;
1801 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
1802 set_slsb(q, &j, SLSB_P_INPUT_NOT_INIT, &count);
1803 }
1804 }
1805
1806 for (i=0;i<no_output_qs;i++) {
1807 q=irq_ptr->output_qs[i];
1808 memset(q,0,((char*)&q->slib)-((char*)q));
1809
1810 sprintf(dbf_text,"outq%4x",i);
1811 QDIO_DBF_TEXT0(0,setup,dbf_text);
1812 QDIO_DBF_HEX0(0,setup,&q,sizeof(void*));
1813
1814 memset(q->slib,0,PAGE_SIZE);
1815 q->sl=(struct sl*)(((char*)q->slib)+PAGE_SIZE/2);
1816
1817 available=0;
1818
1819 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1820 q->sbal[j]=*(outbound_sbals_array++);
1821
1822 q->queue_type=q_format;
1823 if ((q->queue_type == QDIO_IQDIO_QFMT) &&
1824 (no_output_qs > 1) &&
1825 (i == no_output_qs-1))
1826 q->queue_type = QDIO_IQDIO_QFMT_ASYNCH;
1827 q->int_parm=int_parm;
1828 q->is_input_q=0;
1829 q->is_pci_out = 0;
1830 q->schid = irq_ptr->schid;
1831 q->cdev = cdev;
1832 q->irq_ptr = irq_ptr;
1833 q->mask=1<<(31-i);
1834 q->q_no=i;
1835 q->first_to_check=0;
1836 q->last_move_ftc=0;
1837 q->handler=output_handler;
1838
1839 tasklet_init(&q->tasklet, qdio_outbound_processing,
1840 (unsigned long) q);
1841 setup_timer(&q->timer, qdio_outbound_processing,
1842 (unsigned long) q);
1843
1844 atomic_set(&q->busy_siga_counter,0);
1845 q->timing.busy_start=0;
1846
1847 /* fill in slib */
1848 if (i>0) irq_ptr->output_qs[i-1]->slib->nsliba=
1849 (unsigned long)(q->slib);
1850 q->slib->sla=(unsigned long)(q->sl);
1851 q->slib->slsba=(unsigned long)(&q->slsb.acc.val[0]);
1852
1853 /* fill in sl */
1854 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1855 q->sl->element[j].sbal=(unsigned long)(q->sbal[j]);
1856
1857 QDIO_DBF_TEXT2(0,setup,"sl-sb-b0");
1858 ptr=(void*)q->sl;
1859 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1860 ptr=(void*)&q->slsb;
1861 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1862 ptr=(void*)q->sbal[0];
1863 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1864
1865 /* fill in slsb */
1866 if (!irq_ptr->is_qebsm) {
1867 unsigned int count = 1;
1868 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
1869 set_slsb(q, &j, SLSB_P_OUTPUT_NOT_INIT, &count);
1870 }
1871 }
1872}
1873
1874static void
1875qdio_fill_thresholds(struct qdio_irq *irq_ptr,
1876 unsigned int no_input_qs,
1877 unsigned int no_output_qs,
1878 unsigned int min_input_threshold,
1879 unsigned int max_input_threshold,
1880 unsigned int min_output_threshold,
1881 unsigned int max_output_threshold)
1882{
1883 int i;
1884 struct qdio_q *q;
1885
1886 for (i=0;i<no_input_qs;i++) {
1887 q=irq_ptr->input_qs[i];
1888 q->timing.threshold=max_input_threshold;
1889/* for (j=0;j<QDIO_STATS_CLASSES;j++) {
1890 q->threshold_classes[j].threshold=
1891 min_input_threshold+
1892 (max_input_threshold-min_input_threshold)/
1893 QDIO_STATS_CLASSES;
1894 }
1895 qdio_use_thresholds(q,QDIO_STATS_CLASSES/2);*/
1896 }
1897 for (i=0;i<no_output_qs;i++) {
1898 q=irq_ptr->output_qs[i];
1899 q->timing.threshold=max_output_threshold;
1900/* for (j=0;j<QDIO_STATS_CLASSES;j++) {
1901 q->threshold_classes[j].threshold=
1902 min_output_threshold+
1903 (max_output_threshold-min_output_threshold)/
1904 QDIO_STATS_CLASSES;
1905 }
1906 qdio_use_thresholds(q,QDIO_STATS_CLASSES/2);*/
1907 }
1908}
1909
1910static void tiqdio_thinint_handler(void *ind, void *drv_data)
1911{
1912 QDIO_DBF_TEXT4(0,trace,"thin_int");
1913
1914 qdio_perf_stat_inc(&perf_stats.thinints);
1915
1916 /* SVS only when needed:
1917 * issue SVS to benefit from iqdio interrupt avoidance
1918 * (SVS clears AISOI)*/
1919 if (!omit_svs)
1920 tiqdio_clear_global_summary();
1921
1922 tiqdio_inbound_checks();
1923}
1924
1925static void
1926qdio_set_state(struct qdio_irq *irq_ptr, enum qdio_irq_states state)
1927{
1928 int i;
1929#ifdef CONFIG_QDIO_DEBUG
1930 char dbf_text[15];
1931
1932 QDIO_DBF_TEXT5(0,trace,"newstate");
1933 sprintf(dbf_text,"%4x%4x",irq_ptr->schid.sch_no,state);
1934 QDIO_DBF_TEXT5(0,trace,dbf_text);
1935#endif /* CONFIG_QDIO_DEBUG */
1936
1937 irq_ptr->state=state;
1938 for (i=0;i<irq_ptr->no_input_qs;i++)
1939 irq_ptr->input_qs[i]->state=state;
1940 for (i=0;i<irq_ptr->no_output_qs;i++)
1941 irq_ptr->output_qs[i]->state=state;
1942 mb();
1943}
1944
1945static void
1946qdio_irq_check_sense(struct subchannel_id schid, struct irb *irb)
1947{
1948 char dbf_text[15];
1949
1950 if (irb->esw.esw0.erw.cons) {
1951 sprintf(dbf_text,"sens%4x",schid.sch_no);
1952 QDIO_DBF_TEXT2(1,trace,dbf_text);
1953 QDIO_DBF_HEX0(0,sense,irb,QDIO_DBF_SENSE_LEN);
1954
1955 QDIO_PRINT_WARN("sense data available on qdio channel.\n");
1956 QDIO_HEXDUMP16(WARN,"irb: ",irb);
1957 QDIO_HEXDUMP16(WARN,"sense data: ",irb->ecw);
1958 }
1959
1960}
1961
1962static void
1963qdio_handle_pci(struct qdio_irq *irq_ptr)
1964{
1965 int i;
1966 struct qdio_q *q;
1967
1968 qdio_perf_stat_inc(&perf_stats.pcis);
1969 for (i=0;i<irq_ptr->no_input_qs;i++) {
1970 q=irq_ptr->input_qs[i];
1971 if (q->is_input_q&QDIO_FLAG_NO_INPUT_INTERRUPT_CONTEXT)
1972 qdio_mark_q(q);
1973 else {
1974 qdio_perf_stat_dec(&perf_stats.tl_runs);
1975 __qdio_inbound_processing(q);
1976 }
1977 }
1978 if (!irq_ptr->hydra_gives_outbound_pcis)
1979 return;
1980 for (i=0;i<irq_ptr->no_output_qs;i++) {
1981 q=irq_ptr->output_qs[i];
1982 if (qdio_is_outbound_q_done(q))
1983 continue;
1984 qdio_perf_stat_dec(&perf_stats.tl_runs);
1985 if (!irq_ptr->sync_done_on_outb_pcis)
1986 SYNC_MEMORY;
1987 __qdio_outbound_processing(q);
1988 }
1989}
1990
1991static void qdio_establish_handle_irq(struct ccw_device*, int, int);
1992
1993static void
1994qdio_handle_activate_check(struct ccw_device *cdev, unsigned long intparm,
1995 int cstat, int dstat)
1996{
1997 struct qdio_irq *irq_ptr;
1998 struct qdio_q *q;
1999 char dbf_text[15];
2000
2001 irq_ptr = cdev->private->qdio_data;
2002
2003 QDIO_DBF_TEXT2(1, trace, "ick2");
2004 sprintf(dbf_text,"%s", cdev->dev.bus_id);
2005 QDIO_DBF_TEXT2(1,trace,dbf_text);
2006 QDIO_DBF_HEX2(0,trace,&intparm,sizeof(int));
2007 QDIO_DBF_HEX2(0,trace,&dstat,sizeof(int));
2008 QDIO_DBF_HEX2(0,trace,&cstat,sizeof(int));
2009 QDIO_PRINT_ERR("received check condition on activate " \
2010 "queues on device %s (cs=x%x, ds=x%x).\n",
2011 cdev->dev.bus_id, cstat, dstat);
2012 if (irq_ptr->no_input_qs) {
2013 q=irq_ptr->input_qs[0];
2014 } else if (irq_ptr->no_output_qs) {
2015 q=irq_ptr->output_qs[0];
2016 } else {
2017 QDIO_PRINT_ERR("oops... no queue registered for device %s!?\n",
2018 cdev->dev.bus_id);
2019 goto omit_handler_call;
2020 }
2021 q->handler(q->cdev,QDIO_STATUS_ACTIVATE_CHECK_CONDITION|
2022 QDIO_STATUS_LOOK_FOR_ERROR,
2023 0,0,0,-1,-1,q->int_parm);
2024omit_handler_call:
2025 qdio_set_state(irq_ptr,QDIO_IRQ_STATE_STOPPED);
2026
2027}
2028
2029static void
2030qdio_call_shutdown(struct work_struct *work)
2031{
2032 struct ccw_device_private *priv;
2033 struct ccw_device *cdev;
2034
2035 priv = container_of(work, struct ccw_device_private, kick_work);
2036 cdev = priv->cdev;
2037 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
2038 put_device(&cdev->dev);
2039}
2040
2041static void
2042qdio_timeout_handler(struct ccw_device *cdev)
2043{
2044 struct qdio_irq *irq_ptr;
2045 char dbf_text[15];
2046
2047 QDIO_DBF_TEXT2(0, trace, "qtoh");
2048 sprintf(dbf_text, "%s", cdev->dev.bus_id);
2049 QDIO_DBF_TEXT2(0, trace, dbf_text);
2050
2051 irq_ptr = cdev->private->qdio_data;
2052 sprintf(dbf_text, "state:%d", irq_ptr->state);
2053 QDIO_DBF_TEXT2(0, trace, dbf_text);
2054
2055 switch (irq_ptr->state) {
2056 case QDIO_IRQ_STATE_INACTIVE:
2057 QDIO_PRINT_ERR("establish queues on irq 0.%x.%04x: timed out\n",
2058 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2059 QDIO_DBF_TEXT2(1,setup,"eq:timeo");
2060 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2061 break;
2062 case QDIO_IRQ_STATE_CLEANUP:
2063 QDIO_PRINT_INFO("Did not get interrupt on cleanup, "
2064 "irq=0.%x.%x.\n",
2065 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2066 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2067 break;
2068 case QDIO_IRQ_STATE_ESTABLISHED:
2069 case QDIO_IRQ_STATE_ACTIVE:
2070 /* I/O has been terminated by common I/O layer. */
2071 QDIO_PRINT_INFO("Queues on irq 0.%x.%04x killed by cio.\n",
2072 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2073 QDIO_DBF_TEXT2(1, trace, "cio:term");
2074 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
2075 if (get_device(&cdev->dev)) {
2076 /* Can't call shutdown from interrupt context. */
2077 PREPARE_WORK(&cdev->private->kick_work,
2078 qdio_call_shutdown);
2079 queue_work(ccw_device_work, &cdev->private->kick_work);
2080 }
2081 break;
2082 default:
2083 BUG();
2084 }
2085 wake_up(&cdev->private->wait_q);
2086}
2087
2088static void
2089qdio_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
2090{
2091 struct qdio_irq *irq_ptr;
2092 int cstat,dstat;
2093 char dbf_text[15];
2094
2095#ifdef CONFIG_QDIO_DEBUG
2096 QDIO_DBF_TEXT4(0, trace, "qint");
2097 sprintf(dbf_text, "%s", cdev->dev.bus_id);
2098 QDIO_DBF_TEXT4(0, trace, dbf_text);
2099#endif /* CONFIG_QDIO_DEBUG */
2100
2101 if (!intparm) {
2102 QDIO_PRINT_ERR("got unsolicited interrupt in qdio " \
2103 "handler, device %s\n", cdev->dev.bus_id);
2104 return;
2105 }
2106
2107 irq_ptr = cdev->private->qdio_data;
2108 if (!irq_ptr) {
2109 QDIO_DBF_TEXT2(1, trace, "uint");
2110 sprintf(dbf_text,"%s", cdev->dev.bus_id);
2111 QDIO_DBF_TEXT2(1,trace,dbf_text);
2112 QDIO_PRINT_ERR("received interrupt on unused device %s!\n",
2113 cdev->dev.bus_id);
2114 return;
2115 }
2116
2117 if (IS_ERR(irb)) {
2118 /* Currently running i/o is in error. */
2119 switch (PTR_ERR(irb)) {
2120 case -EIO:
2121 QDIO_PRINT_ERR("i/o error on device %s\n",
2122 cdev->dev.bus_id);
2123 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2124 wake_up(&cdev->private->wait_q);
2125 return;
2126 case -ETIMEDOUT:
2127 qdio_timeout_handler(cdev);
2128 return;
2129 default:
2130 QDIO_PRINT_ERR("unknown error state %ld on device %s\n",
2131 PTR_ERR(irb), cdev->dev.bus_id);
2132 return;
2133 }
2134 }
2135
2136 qdio_irq_check_sense(irq_ptr->schid, irb);
2137
2138#ifdef CONFIG_QDIO_DEBUG
2139 sprintf(dbf_text, "state:%d", irq_ptr->state);
2140 QDIO_DBF_TEXT4(0, trace, dbf_text);
2141#endif /* CONFIG_QDIO_DEBUG */
2142
2143 cstat = irb->scsw.cmd.cstat;
2144 dstat = irb->scsw.cmd.dstat;
2145
2146 switch (irq_ptr->state) {
2147 case QDIO_IRQ_STATE_INACTIVE:
2148 qdio_establish_handle_irq(cdev, cstat, dstat);
2149 break;
2150
2151 case QDIO_IRQ_STATE_CLEANUP:
2152 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
2153 break;
2154
2155 case QDIO_IRQ_STATE_ESTABLISHED:
2156 case QDIO_IRQ_STATE_ACTIVE:
2157 if (cstat & SCHN_STAT_PCI) {
2158 qdio_handle_pci(irq_ptr);
2159 break;
2160 }
2161
2162 if ((cstat&~SCHN_STAT_PCI)||dstat) {
2163 qdio_handle_activate_check(cdev, intparm, cstat, dstat);
2164 break;
2165 }
2166 default:
2167 QDIO_PRINT_ERR("got interrupt for queues in state %d on " \
2168 "device %s?!\n",
2169 irq_ptr->state, cdev->dev.bus_id);
2170 }
2171 wake_up(&cdev->private->wait_q);
2172
2173}
2174
2175int
2176qdio_synchronize(struct ccw_device *cdev, unsigned int flags,
2177 unsigned int queue_number)
2178{
2179 int cc = 0;
2180 struct qdio_q *q;
2181 struct qdio_irq *irq_ptr;
2182 void *ptr;
2183#ifdef CONFIG_QDIO_DEBUG
2184 char dbf_text[15]="SyncXXXX";
2185#endif
2186
2187 irq_ptr = cdev->private->qdio_data;
2188 if (!irq_ptr)
2189 return -ENODEV;
2190
2191#ifdef CONFIG_QDIO_DEBUG
2192 *((int*)(&dbf_text[4])) = irq_ptr->schid.sch_no;
2193 QDIO_DBF_HEX4(0,trace,dbf_text,QDIO_DBF_TRACE_LEN);
2194 *((int*)(&dbf_text[0]))=flags;
2195 *((int*)(&dbf_text[4]))=queue_number;
2196 QDIO_DBF_HEX4(0,trace,dbf_text,QDIO_DBF_TRACE_LEN);
2197#endif /* CONFIG_QDIO_DEBUG */
2198
2199 if (flags&QDIO_FLAG_SYNC_INPUT) {
2200 q=irq_ptr->input_qs[queue_number];
2201 if (!q)
2202 return -EINVAL;
2203 if (!(irq_ptr->is_qebsm))
2204 cc = do_siga_sync(q->schid, 0, q->mask);
2205 } else if (flags&QDIO_FLAG_SYNC_OUTPUT) {
2206 q=irq_ptr->output_qs[queue_number];
2207 if (!q)
2208 return -EINVAL;
2209 if (!(irq_ptr->is_qebsm))
2210 cc = do_siga_sync(q->schid, q->mask, 0);
2211 } else
2212 return -EINVAL;
2213
2214 ptr=&cc;
2215 if (cc)
2216 QDIO_DBF_HEX3(0,trace,&ptr,sizeof(int));
2217
2218 return cc;
2219}
2220
2221static int
2222qdio_get_ssqd_information(struct subchannel_id *schid,
2223 struct qdio_chsc_ssqd **ssqd_area)
2224{
2225 int result;
2226
2227 QDIO_DBF_TEXT0(0, setup, "getssqd");
2228 *ssqd_area = mempool_alloc(qdio_mempool_scssc, GFP_ATOMIC);
2229 if (!ssqd_area) {
2230 QDIO_PRINT_WARN("Could not get memory for chsc on sch x%x.\n",
2231 schid->sch_no);
2232 return -ENOMEM;
2233 }
2234
2235 (*ssqd_area)->request = (struct chsc_header) {
2236 .length = 0x0010,
2237 .code = 0x0024,
2238 };
2239 (*ssqd_area)->first_sch = schid->sch_no;
2240 (*ssqd_area)->last_sch = schid->sch_no;
2241 (*ssqd_area)->ssid = schid->ssid;
2242 result = chsc(*ssqd_area);
2243
2244 if (result) {
2245 QDIO_PRINT_WARN("CHSC returned cc %i on sch 0.%x.%x.\n",
2246 result, schid->ssid, schid->sch_no);
2247 goto out;
2248 }
2249
2250 if ((*ssqd_area)->response.code != QDIO_CHSC_RESPONSE_CODE_OK) {
2251 QDIO_PRINT_WARN("CHSC response is 0x%x on sch 0.%x.%x.\n",
2252 (*ssqd_area)->response.code,
2253 schid->ssid, schid->sch_no);
2254 goto out;
2255 }
2256 if (!((*ssqd_area)->flags & CHSC_FLAG_QDIO_CAPABILITY) ||
2257 !((*ssqd_area)->flags & CHSC_FLAG_VALIDITY) ||
2258 ((*ssqd_area)->sch != schid->sch_no)) {
2259 QDIO_PRINT_WARN("huh? problems checking out sch 0.%x.%x... " \
2260 "using all SIGAs.\n",
2261 schid->ssid, schid->sch_no);
2262 goto out;
2263 }
2264 return 0;
2265out:
2266 return -EINVAL;
2267}
2268
2269int
2270qdio_get_ssqd_pct(struct ccw_device *cdev)
2271{
2272 struct qdio_chsc_ssqd *ssqd_area;
2273 struct subchannel_id schid;
2274 char dbf_text[15];
2275 int rc;
2276 int pct = 0;
2277
2278 QDIO_DBF_TEXT0(0, setup, "getpct");
2279 schid = ccw_device_get_subchannel_id(cdev);
2280 rc = qdio_get_ssqd_information(&schid, &ssqd_area);
2281 if (!rc)
2282 pct = (int)ssqd_area->pct;
2283 if (rc != -ENOMEM)
2284 mempool_free(ssqd_area, qdio_mempool_scssc);
2285 sprintf(dbf_text, "pct: %d", pct);
2286 QDIO_DBF_TEXT2(0, setup, dbf_text);
2287 return pct;
2288}
2289EXPORT_SYMBOL(qdio_get_ssqd_pct);
2290
2291static void
2292qdio_check_subchannel_qebsm(struct qdio_irq *irq_ptr, unsigned long token)
2293{
2294 struct qdio_q *q;
2295 int i;
2296 unsigned int count, start_buf;
2297 char dbf_text[15];
2298
2299 /*check if QEBSM is disabled */
2300 if (!(irq_ptr->is_qebsm) || !(irq_ptr->qdioac & 0x01)) {
2301 irq_ptr->is_qebsm = 0;
2302 irq_ptr->sch_token = 0;
2303 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
2304 QDIO_DBF_TEXT0(0,setup,"noV=V");
2305 return;
2306 }
2307 irq_ptr->sch_token = token;
2308 /*input queue*/
2309 for (i = 0; i < irq_ptr->no_input_qs;i++) {
2310 q = irq_ptr->input_qs[i];
2311 count = QDIO_MAX_BUFFERS_PER_Q;
2312 start_buf = 0;
2313 set_slsb(q, &start_buf, SLSB_P_INPUT_NOT_INIT, &count);
2314 }
2315 sprintf(dbf_text,"V=V:%2x",irq_ptr->is_qebsm);
2316 QDIO_DBF_TEXT0(0,setup,dbf_text);
2317 sprintf(dbf_text,"%8lx",irq_ptr->sch_token);
2318 QDIO_DBF_TEXT0(0,setup,dbf_text);
2319 /*output queue*/
2320 for (i = 0; i < irq_ptr->no_output_qs; i++) {
2321 q = irq_ptr->output_qs[i];
2322 count = QDIO_MAX_BUFFERS_PER_Q;
2323 start_buf = 0;
2324 set_slsb(q, &start_buf, SLSB_P_OUTPUT_NOT_INIT, &count);
2325 }
2326}
2327
2328static void
2329qdio_get_ssqd_siga(struct qdio_irq *irq_ptr)
2330{
2331 int rc;
2332 struct qdio_chsc_ssqd *ssqd_area;
2333
2334 QDIO_DBF_TEXT0(0,setup,"getssqd");
2335 irq_ptr->qdioac = 0;
2336 rc = qdio_get_ssqd_information(&irq_ptr->schid, &ssqd_area);
2337 if (rc) {
2338 QDIO_PRINT_WARN("using all SIGAs for sch x%x.n",
2339 irq_ptr->schid.sch_no);
2340 irq_ptr->qdioac = CHSC_FLAG_SIGA_INPUT_NECESSARY |
2341 CHSC_FLAG_SIGA_OUTPUT_NECESSARY |
2342 CHSC_FLAG_SIGA_SYNC_NECESSARY; /* all flags set */
2343 irq_ptr->is_qebsm = 0;
2344 } else
2345 irq_ptr->qdioac = ssqd_area->qdioac1;
2346
2347 qdio_check_subchannel_qebsm(irq_ptr, ssqd_area->sch_token);
2348 if (rc != -ENOMEM)
2349 mempool_free(ssqd_area, qdio_mempool_scssc);
2350}
2351
2352static unsigned int
2353tiqdio_check_chsc_availability(void)
2354{
2355 char dbf_text[15];
2356
2357 /* Check for bit 41. */
2358 if (!css_general_characteristics.aif) {
2359 QDIO_PRINT_WARN("Adapter interruption facility not " \
2360 "installed.\n");
2361 return -ENOENT;
2362 }
2363
2364 /* Check for bits 107 and 108. */
2365 if (!css_chsc_characteristics.scssc ||
2366 !css_chsc_characteristics.scsscf) {
2367 QDIO_PRINT_WARN("Set Chan Subsys. Char. & Fast-CHSCs " \
2368 "not available.\n");
2369 return -ENOENT;
2370 }
2371
2372 /* Check for OSA/FCP thin interrupts (bit 67). */
2373 hydra_thinints = css_general_characteristics.aif_osa;
2374 sprintf(dbf_text,"hydrati%1x", hydra_thinints);
2375 QDIO_DBF_TEXT0(0,setup,dbf_text);
2376
2377#ifdef CONFIG_64BIT
2378 /* Check for QEBSM support in general (bit 58). */
2379 is_passthrough = css_general_characteristics.qebsm;
2380#endif
2381 sprintf(dbf_text,"cssQBS:%1x", is_passthrough);
2382 QDIO_DBF_TEXT0(0,setup,dbf_text);
2383
2384 /* Check for aif time delay disablement fac (bit 56). If installed,
2385 * omit svs even under lpar (good point by rick again) */
2386 omit_svs = css_general_characteristics.aif_tdd;
2387 sprintf(dbf_text,"omitsvs%1x", omit_svs);
2388 QDIO_DBF_TEXT0(0,setup,dbf_text);
2389 return 0;
2390}
2391
2392
2393static unsigned int
2394tiqdio_set_subchannel_ind(struct qdio_irq *irq_ptr, int reset_to_zero)
2395{
2396 unsigned long real_addr_local_summary_bit;
2397 unsigned long real_addr_dev_st_chg_ind;
2398 void *ptr;
2399 char dbf_text[15];
2400
2401 unsigned int resp_code;
2402 int result;
2403
2404 struct {
2405 struct chsc_header request;
2406 u16 operation_code;
2407 u16 reserved1;
2408 u32 reserved2;
2409 u32 reserved3;
2410 u64 summary_indicator_addr;
2411 u64 subchannel_indicator_addr;
2412 u32 ks:4;
2413 u32 kc:4;
2414 u32 reserved4:21;
2415 u32 isc:3;
2416 u32 word_with_d_bit;
2417 /* set to 0x10000000 to enable
2418 * time delay disablement facility */
2419 u32 reserved5;
2420 struct subchannel_id schid;
2421 u32 reserved6[1004];
2422 struct chsc_header response;
2423 u32 reserved7;
2424 } *scssc_area;
2425
2426 if (!irq_ptr->is_thinint_irq)
2427 return -ENODEV;
2428
2429 if (reset_to_zero) {
2430 real_addr_local_summary_bit=0;
2431 real_addr_dev_st_chg_ind=0;
2432 } else {
2433 real_addr_local_summary_bit=
2434 virt_to_phys((volatile void *)tiqdio_ind);
2435 real_addr_dev_st_chg_ind=
2436 virt_to_phys((volatile void *)irq_ptr->dev_st_chg_ind);
2437 }
2438
2439 scssc_area = mempool_alloc(qdio_mempool_scssc, GFP_ATOMIC);
2440 if (!scssc_area) {
2441 QDIO_PRINT_WARN("No memory for setting indicators on " \
2442 "subchannel 0.%x.%x.\n",
2443 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2444 return -ENOMEM;
2445 }
2446 scssc_area->request = (struct chsc_header) {
2447 .length = 0x0fe0,
2448 .code = 0x0021,
2449 };
2450 scssc_area->operation_code = 0;
2451
2452 scssc_area->summary_indicator_addr = real_addr_local_summary_bit;
2453 scssc_area->subchannel_indicator_addr = real_addr_dev_st_chg_ind;
2454 scssc_area->ks = QDIO_STORAGE_KEY;
2455 scssc_area->kc = QDIO_STORAGE_KEY;
2456 scssc_area->isc = TIQDIO_THININT_ISC;
2457 scssc_area->schid = irq_ptr->schid;
2458 /* enables the time delay disablement facility. Don't care
2459 * whether it is really there (i.e. we haven't checked for
2460 * it) */
2461 if (css_general_characteristics.aif_tdd)
2462 scssc_area->word_with_d_bit = 0x10000000;
2463 else
2464 QDIO_PRINT_WARN("Time delay disablement facility " \
2465 "not available\n");
2466
2467 result = chsc(scssc_area);
2468 if (result) {
2469 QDIO_PRINT_WARN("could not set indicators on irq 0.%x.%x, " \
2470 "cc=%i.\n",
2471 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,result);
2472 result = -EIO;
2473 goto out;
2474 }
2475
2476 resp_code = scssc_area->response.code;
2477 if (resp_code!=QDIO_CHSC_RESPONSE_CODE_OK) {
2478 QDIO_PRINT_WARN("response upon setting indicators " \
2479 "is 0x%x.\n",resp_code);
2480 sprintf(dbf_text,"sidR%4x",resp_code);
2481 QDIO_DBF_TEXT1(0,trace,dbf_text);
2482 QDIO_DBF_TEXT1(0,setup,dbf_text);
2483 ptr=&scssc_area->response;
2484 QDIO_DBF_HEX2(1,setup,&ptr,QDIO_DBF_SETUP_LEN);
2485 result = -EIO;
2486 goto out;
2487 }
2488
2489 QDIO_DBF_TEXT2(0,setup,"setscind");
2490 QDIO_DBF_HEX2(0,setup,&real_addr_local_summary_bit,
2491 sizeof(unsigned long));
2492 QDIO_DBF_HEX2(0,setup,&real_addr_dev_st_chg_ind,sizeof(unsigned long));
2493 result = 0;
2494out:
2495 mempool_free(scssc_area, qdio_mempool_scssc);
2496 return result;
2497
2498}
2499
2500static unsigned int
2501tiqdio_set_delay_target(struct qdio_irq *irq_ptr, unsigned long delay_target)
2502{
2503 unsigned int resp_code;
2504 int result;
2505 void *ptr;
2506 char dbf_text[15];
2507
2508 struct {
2509 struct chsc_header request;
2510 u16 operation_code;
2511 u16 reserved1;
2512 u32 reserved2;
2513 u32 reserved3;
2514 u32 reserved4[2];
2515 u32 delay_target;
2516 u32 reserved5[1009];
2517 struct chsc_header response;
2518 u32 reserved6;
2519 } *scsscf_area;
2520
2521 if (!irq_ptr->is_thinint_irq)
2522 return -ENODEV;
2523
2524 scsscf_area = mempool_alloc(qdio_mempool_scssc, GFP_ATOMIC);
2525 if (!scsscf_area) {
2526 QDIO_PRINT_WARN("No memory for setting delay target on " \
2527 "subchannel 0.%x.%x.\n",
2528 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2529 return -ENOMEM;
2530 }
2531 scsscf_area->request = (struct chsc_header) {
2532 .length = 0x0fe0,
2533 .code = 0x1027,
2534 };
2535
2536 scsscf_area->delay_target = delay_target<<16;
2537
2538 result=chsc(scsscf_area);
2539 if (result) {
2540 QDIO_PRINT_WARN("could not set delay target on irq 0.%x.%x, " \
2541 "cc=%i. Continuing.\n",
2542 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
2543 result);
2544 result = -EIO;
2545 goto out;
2546 }
2547
2548 resp_code = scsscf_area->response.code;
2549 if (resp_code!=QDIO_CHSC_RESPONSE_CODE_OK) {
2550 QDIO_PRINT_WARN("response upon setting delay target " \
2551 "is 0x%x. Continuing.\n",resp_code);
2552 sprintf(dbf_text,"sdtR%4x",resp_code);
2553 QDIO_DBF_TEXT1(0,trace,dbf_text);
2554 QDIO_DBF_TEXT1(0,setup,dbf_text);
2555 ptr=&scsscf_area->response;
2556 QDIO_DBF_HEX2(1,trace,&ptr,QDIO_DBF_TRACE_LEN);
2557 }
2558 QDIO_DBF_TEXT2(0,trace,"delytrgt");
2559 QDIO_DBF_HEX2(0,trace,&delay_target,sizeof(unsigned long));
2560 result = 0; /* not critical */
2561out:
2562 mempool_free(scsscf_area, qdio_mempool_scssc);
2563 return result;
2564}
2565
2566int
2567qdio_cleanup(struct ccw_device *cdev, int how)
2568{
2569 struct qdio_irq *irq_ptr;
2570 char dbf_text[15];
2571 int rc;
2572
2573 irq_ptr = cdev->private->qdio_data;
2574 if (!irq_ptr)
2575 return -ENODEV;
2576
2577 sprintf(dbf_text,"qcln%4x",irq_ptr->schid.sch_no);
2578 QDIO_DBF_TEXT1(0,trace,dbf_text);
2579 QDIO_DBF_TEXT0(0,setup,dbf_text);
2580
2581 rc = qdio_shutdown(cdev, how);
2582 if ((rc == 0) || (rc == -EINPROGRESS))
2583 rc = qdio_free(cdev);
2584 return rc;
2585}
2586
2587int
2588qdio_shutdown(struct ccw_device *cdev, int how)
2589{
2590 struct qdio_irq *irq_ptr;
2591 int i;
2592 int result = 0;
2593 int rc;
2594 unsigned long flags;
2595 int timeout;
2596 char dbf_text[15];
2597
2598 irq_ptr = cdev->private->qdio_data;
2599 if (!irq_ptr)
2600 return -ENODEV;
2601
2602 down(&irq_ptr->setting_up_sema);
2603
2604 sprintf(dbf_text,"qsqs%4x",irq_ptr->schid.sch_no);
2605 QDIO_DBF_TEXT1(0,trace,dbf_text);
2606 QDIO_DBF_TEXT0(0,setup,dbf_text);
2607
2608 /* mark all qs as uninteresting */
2609 for (i=0;i<irq_ptr->no_input_qs;i++)
2610 atomic_set(&irq_ptr->input_qs[i]->is_in_shutdown,1);
2611
2612 for (i=0;i<irq_ptr->no_output_qs;i++)
2613 atomic_set(&irq_ptr->output_qs[i]->is_in_shutdown,1);
2614
2615 tasklet_kill(&tiqdio_tasklet);
2616
2617 for (i=0;i<irq_ptr->no_input_qs;i++) {
2618 qdio_unmark_q(irq_ptr->input_qs[i]);
2619 tasklet_kill(&irq_ptr->input_qs[i]->tasklet);
2620 wait_event_interruptible_timeout(cdev->private->wait_q,
2621 !atomic_read(&irq_ptr->
2622 input_qs[i]->
2623 use_count),
2624 QDIO_NO_USE_COUNT_TIMEOUT);
2625 if (atomic_read(&irq_ptr->input_qs[i]->use_count))
2626 result=-EINPROGRESS;
2627 }
2628
2629 for (i=0;i<irq_ptr->no_output_qs;i++) {
2630 tasklet_kill(&irq_ptr->output_qs[i]->tasklet);
2631 del_timer(&irq_ptr->output_qs[i]->timer);
2632 wait_event_interruptible_timeout(cdev->private->wait_q,
2633 !atomic_read(&irq_ptr->
2634 output_qs[i]->
2635 use_count),
2636 QDIO_NO_USE_COUNT_TIMEOUT);
2637 if (atomic_read(&irq_ptr->output_qs[i]->use_count))
2638 result=-EINPROGRESS;
2639 }
2640
2641 /* cleanup subchannel */
2642 spin_lock_irqsave(get_ccwdev_lock(cdev),flags);
2643 if (how&QDIO_FLAG_CLEANUP_USING_CLEAR) {
2644 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
2645 timeout=QDIO_CLEANUP_CLEAR_TIMEOUT;
2646 } else if (how&QDIO_FLAG_CLEANUP_USING_HALT) {
2647 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
2648 timeout=QDIO_CLEANUP_HALT_TIMEOUT;
2649 } else { /* default behaviour */
2650 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
2651 timeout=QDIO_CLEANUP_HALT_TIMEOUT;
2652 }
2653 if (rc == -ENODEV) {
2654 /* No need to wait for device no longer present. */
2655 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
2656 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2657 } else if (((void *)cdev->handler != (void *)qdio_handler) && rc == 0) {
2658 /*
2659 * Whoever put another handler there, has to cope with the
2660 * interrupt theirself. Might happen if qdio_shutdown was
2661 * called on already shutdown queues, but this shouldn't have
2662 * bad side effects.
2663 */
2664 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
2665 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2666 } else if (rc == 0) {
2667 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
2668 spin_unlock_irqrestore(get_ccwdev_lock(cdev),flags);
2669
2670 wait_event_interruptible_timeout(cdev->private->wait_q,
2671 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
2672 irq_ptr->state == QDIO_IRQ_STATE_ERR,
2673 timeout);
2674 } else {
2675 QDIO_PRINT_INFO("ccw_device_{halt,clear} returned %d for "
2676 "device %s\n", result, cdev->dev.bus_id);
2677 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2678 result = rc;
2679 goto out;
2680 }
2681 if (irq_ptr->is_thinint_irq) {
2682 qdio_put_indicator((__u32*)irq_ptr->dev_st_chg_ind);
2683 tiqdio_set_subchannel_ind(irq_ptr,1);
2684 /* reset adapter interrupt indicators */
2685 }
2686
2687 /* exchange int handlers, if necessary */
2688 if ((void*)cdev->handler == (void*)qdio_handler)
2689 cdev->handler=irq_ptr->original_int_handler;
2690
2691 /* Ignore errors. */
2692 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
2693out:
2694 up(&irq_ptr->setting_up_sema);
2695 return result;
2696}
2697
2698int
2699qdio_free(struct ccw_device *cdev)
2700{
2701 struct qdio_irq *irq_ptr;
2702 char dbf_text[15];
2703
2704 irq_ptr = cdev->private->qdio_data;
2705 if (!irq_ptr)
2706 return -ENODEV;
2707
2708 down(&irq_ptr->setting_up_sema);
2709
2710 sprintf(dbf_text,"qfqs%4x",irq_ptr->schid.sch_no);
2711 QDIO_DBF_TEXT1(0,trace,dbf_text);
2712 QDIO_DBF_TEXT0(0,setup,dbf_text);
2713
2714 cdev->private->qdio_data = NULL;
2715
2716 up(&irq_ptr->setting_up_sema);
2717
2718 qdio_release_irq_memory(irq_ptr);
2719 module_put(THIS_MODULE);
2720 return 0;
2721}
2722
2723static void
2724qdio_allocate_do_dbf(struct qdio_initialize *init_data)
2725{
2726 char dbf_text[20]; /* if a printf printed out more than 8 chars */
2727
2728 sprintf(dbf_text,"qfmt:%x",init_data->q_format);
2729 QDIO_DBF_TEXT0(0,setup,dbf_text);
2730 QDIO_DBF_HEX0(0,setup,init_data->adapter_name,8);
2731 sprintf(dbf_text,"qpff%4x",init_data->qib_param_field_format);
2732 QDIO_DBF_TEXT0(0,setup,dbf_text);
2733 QDIO_DBF_HEX0(0,setup,&init_data->qib_param_field,sizeof(char*));
2734 QDIO_DBF_HEX0(0,setup,&init_data->input_slib_elements,sizeof(long*));
2735 QDIO_DBF_HEX0(0,setup,&init_data->output_slib_elements,sizeof(long*));
2736 sprintf(dbf_text,"miit%4x",init_data->min_input_threshold);
2737 QDIO_DBF_TEXT0(0,setup,dbf_text);
2738 sprintf(dbf_text,"mait%4x",init_data->max_input_threshold);
2739 QDIO_DBF_TEXT0(0,setup,dbf_text);
2740 sprintf(dbf_text,"miot%4x",init_data->min_output_threshold);
2741 QDIO_DBF_TEXT0(0,setup,dbf_text);
2742 sprintf(dbf_text,"maot%4x",init_data->max_output_threshold);
2743 QDIO_DBF_TEXT0(0,setup,dbf_text);
2744 sprintf(dbf_text,"niq:%4x",init_data->no_input_qs);
2745 QDIO_DBF_TEXT0(0,setup,dbf_text);
2746 sprintf(dbf_text,"noq:%4x",init_data->no_output_qs);
2747 QDIO_DBF_TEXT0(0,setup,dbf_text);
2748 QDIO_DBF_HEX0(0,setup,&init_data->input_handler,sizeof(void*));
2749 QDIO_DBF_HEX0(0,setup,&init_data->output_handler,sizeof(void*));
2750 QDIO_DBF_HEX0(0,setup,&init_data->int_parm,sizeof(long));
2751 QDIO_DBF_HEX0(0,setup,&init_data->flags,sizeof(long));
2752 QDIO_DBF_HEX0(0,setup,&init_data->input_sbal_addr_array,sizeof(void*));
2753 QDIO_DBF_HEX0(0,setup,&init_data->output_sbal_addr_array,sizeof(void*));
2754}
2755
2756static void
2757qdio_allocate_fill_input_desc(struct qdio_irq *irq_ptr, int i, int iqfmt)
2758{
2759 irq_ptr->input_qs[i]->is_iqdio_q = iqfmt;
2760 irq_ptr->input_qs[i]->is_thinint_q = irq_ptr->is_thinint_irq;
2761
2762 irq_ptr->qdr->qdf0[i].sliba=(unsigned long)(irq_ptr->input_qs[i]->slib);
2763
2764 irq_ptr->qdr->qdf0[i].sla=(unsigned long)(irq_ptr->input_qs[i]->sl);
2765
2766 irq_ptr->qdr->qdf0[i].slsba=
2767 (unsigned long)(&irq_ptr->input_qs[i]->slsb.acc.val[0]);
2768
2769 irq_ptr->qdr->qdf0[i].akey=QDIO_STORAGE_KEY;
2770 irq_ptr->qdr->qdf0[i].bkey=QDIO_STORAGE_KEY;
2771 irq_ptr->qdr->qdf0[i].ckey=QDIO_STORAGE_KEY;
2772 irq_ptr->qdr->qdf0[i].dkey=QDIO_STORAGE_KEY;
2773}
2774
2775static void
2776qdio_allocate_fill_output_desc(struct qdio_irq *irq_ptr, int i,
2777 int j, int iqfmt)
2778{
2779 irq_ptr->output_qs[i]->is_iqdio_q = iqfmt;
2780 irq_ptr->output_qs[i]->is_thinint_q = irq_ptr->is_thinint_irq;
2781
2782 irq_ptr->qdr->qdf0[i+j].sliba=(unsigned long)(irq_ptr->output_qs[i]->slib);
2783
2784 irq_ptr->qdr->qdf0[i+j].sla=(unsigned long)(irq_ptr->output_qs[i]->sl);
2785
2786 irq_ptr->qdr->qdf0[i+j].slsba=
2787 (unsigned long)(&irq_ptr->output_qs[i]->slsb.acc.val[0]);
2788
2789 irq_ptr->qdr->qdf0[i+j].akey=QDIO_STORAGE_KEY;
2790 irq_ptr->qdr->qdf0[i+j].bkey=QDIO_STORAGE_KEY;
2791 irq_ptr->qdr->qdf0[i+j].ckey=QDIO_STORAGE_KEY;
2792 irq_ptr->qdr->qdf0[i+j].dkey=QDIO_STORAGE_KEY;
2793}
2794
2795
2796static void
2797qdio_initialize_set_siga_flags_input(struct qdio_irq *irq_ptr)
2798{
2799 int i;
2800
2801 for (i=0;i<irq_ptr->no_input_qs;i++) {
2802 irq_ptr->input_qs[i]->siga_sync=
2803 irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_NECESSARY;
2804 irq_ptr->input_qs[i]->siga_in=
2805 irq_ptr->qdioac&CHSC_FLAG_SIGA_INPUT_NECESSARY;
2806 irq_ptr->input_qs[i]->siga_out=
2807 irq_ptr->qdioac&CHSC_FLAG_SIGA_OUTPUT_NECESSARY;
2808 irq_ptr->input_qs[i]->siga_sync_done_on_thinints=
2809 irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS;
2810 irq_ptr->input_qs[i]->hydra_gives_outbound_pcis=
2811 irq_ptr->hydra_gives_outbound_pcis;
2812 irq_ptr->input_qs[i]->siga_sync_done_on_outb_tis=
2813 ((irq_ptr->qdioac&
2814 (CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS|
2815 CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS))==
2816 (CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS|
2817 CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS));
2818
2819 }
2820}
2821
2822static void
2823qdio_initialize_set_siga_flags_output(struct qdio_irq *irq_ptr)
2824{
2825 int i;
2826
2827 for (i=0;i<irq_ptr->no_output_qs;i++) {
2828 irq_ptr->output_qs[i]->siga_sync=
2829 irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_NECESSARY;
2830 irq_ptr->output_qs[i]->siga_in=
2831 irq_ptr->qdioac&CHSC_FLAG_SIGA_INPUT_NECESSARY;
2832 irq_ptr->output_qs[i]->siga_out=
2833 irq_ptr->qdioac&CHSC_FLAG_SIGA_OUTPUT_NECESSARY;
2834 irq_ptr->output_qs[i]->siga_sync_done_on_thinints=
2835 irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS;
2836 irq_ptr->output_qs[i]->hydra_gives_outbound_pcis=
2837 irq_ptr->hydra_gives_outbound_pcis;
2838 irq_ptr->output_qs[i]->siga_sync_done_on_outb_tis=
2839 ((irq_ptr->qdioac&
2840 (CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS|
2841 CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS))==
2842 (CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS|
2843 CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS));
2844
2845 }
2846}
2847
2848static int
2849qdio_establish_irq_check_for_errors(struct ccw_device *cdev, int cstat,
2850 int dstat)
2851{
2852 char dbf_text[15];
2853 struct qdio_irq *irq_ptr;
2854
2855 irq_ptr = cdev->private->qdio_data;
2856
2857 if (cstat || (dstat & ~(DEV_STAT_CHN_END|DEV_STAT_DEV_END))) {
2858 sprintf(dbf_text,"ick1%4x",irq_ptr->schid.sch_no);
2859 QDIO_DBF_TEXT2(1,trace,dbf_text);
2860 QDIO_DBF_HEX2(0,trace,&dstat,sizeof(int));
2861 QDIO_DBF_HEX2(0,trace,&cstat,sizeof(int));
2862 QDIO_PRINT_ERR("received check condition on establish " \
2863 "queues on irq 0.%x.%x (cs=x%x, ds=x%x).\n",
2864 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
2865 cstat,dstat);
2866 qdio_set_state(irq_ptr,QDIO_IRQ_STATE_ERR);
2867 }
2868
2869 if (!(dstat & DEV_STAT_DEV_END)) {
2870 QDIO_DBF_TEXT2(1,setup,"eq:no de");
2871 QDIO_DBF_HEX2(0,setup,&dstat, sizeof(dstat));
2872 QDIO_DBF_HEX2(0,setup,&cstat, sizeof(cstat));
2873 QDIO_PRINT_ERR("establish queues on irq 0.%x.%04x: didn't get "
2874 "device end: dstat=%02x, cstat=%02x\n",
2875 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
2876 dstat, cstat);
2877 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2878 return 1;
2879 }
2880
2881 if (dstat & ~(DEV_STAT_CHN_END|DEV_STAT_DEV_END)) {
2882 QDIO_DBF_TEXT2(1,setup,"eq:badio");
2883 QDIO_DBF_HEX2(0,setup,&dstat, sizeof(dstat));
2884 QDIO_DBF_HEX2(0,setup,&cstat, sizeof(cstat));
2885 QDIO_PRINT_ERR("establish queues on irq 0.%x.%04x: got "
2886 "the following devstat: dstat=%02x, "
2887 "cstat=%02x\n", irq_ptr->schid.ssid,
2888 irq_ptr->schid.sch_no, dstat, cstat);
2889 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2890 return 1;
2891 }
2892 return 0;
2893}
2894
2895static void
2896qdio_establish_handle_irq(struct ccw_device *cdev, int cstat, int dstat)
2897{
2898 struct qdio_irq *irq_ptr;
2899 char dbf_text[15];
2900
2901 irq_ptr = cdev->private->qdio_data;
2902
2903 sprintf(dbf_text,"qehi%4x",cdev->private->schid.sch_no);
2904 QDIO_DBF_TEXT0(0,setup,dbf_text);
2905 QDIO_DBF_TEXT0(0,trace,dbf_text);
2906
2907 if (qdio_establish_irq_check_for_errors(cdev, cstat, dstat))
2908 return;
2909
2910 qdio_set_state(irq_ptr,QDIO_IRQ_STATE_ESTABLISHED);
2911}
2912
2913int
2914qdio_initialize(struct qdio_initialize *init_data)
2915{
2916 int rc;
2917 char dbf_text[15];
2918
2919 sprintf(dbf_text,"qini%4x",init_data->cdev->private->schid.sch_no);
2920 QDIO_DBF_TEXT0(0,setup,dbf_text);
2921 QDIO_DBF_TEXT0(0,trace,dbf_text);
2922
2923 rc = qdio_allocate(init_data);
2924 if (rc == 0) {
2925 rc = qdio_establish(init_data);
2926 if (rc != 0)
2927 qdio_free(init_data->cdev);
2928 }
2929
2930 return rc;
2931}
2932
2933
2934int
2935qdio_allocate(struct qdio_initialize *init_data)
2936{
2937 struct qdio_irq *irq_ptr;
2938 char dbf_text[15];
2939
2940 sprintf(dbf_text,"qalc%4x",init_data->cdev->private->schid.sch_no);
2941 QDIO_DBF_TEXT0(0,setup,dbf_text);
2942 QDIO_DBF_TEXT0(0,trace,dbf_text);
2943 if ( (init_data->no_input_qs>QDIO_MAX_QUEUES_PER_IRQ) ||
2944 (init_data->no_output_qs>QDIO_MAX_QUEUES_PER_IRQ) ||
2945 ((init_data->no_input_qs) && (!init_data->input_handler)) ||
2946 ((init_data->no_output_qs) && (!init_data->output_handler)) )
2947 return -EINVAL;
2948
2949 if (!init_data->input_sbal_addr_array)
2950 return -EINVAL;
2951
2952 if (!init_data->output_sbal_addr_array)
2953 return -EINVAL;
2954
2955 qdio_allocate_do_dbf(init_data);
2956
2957 /* create irq */
2958 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
2959
2960 QDIO_DBF_TEXT0(0,setup,"irq_ptr:");
2961 QDIO_DBF_HEX0(0,setup,&irq_ptr,sizeof(void*));
2962
2963 if (!irq_ptr) {
2964 QDIO_PRINT_ERR("allocation of irq_ptr failed!\n");
2965 return -ENOMEM;
2966 }
2967
2968 init_MUTEX(&irq_ptr->setting_up_sema);
2969
2970 /* QDR must be in DMA area since CCW data address is only 32 bit */
2971 irq_ptr->qdr = (struct qdr *) __get_free_page(GFP_KERNEL | GFP_DMA);
2972 if (!(irq_ptr->qdr)) {
2973 free_page((unsigned long) irq_ptr);
2974 QDIO_PRINT_ERR("allocation of irq_ptr->qdr failed!\n");
2975 return -ENOMEM;
2976 }
2977 QDIO_DBF_TEXT0(0,setup,"qdr:");
2978 QDIO_DBF_HEX0(0,setup,&irq_ptr->qdr,sizeof(void*));
2979
2980 if (qdio_alloc_qs(irq_ptr,
2981 init_data->no_input_qs,
2982 init_data->no_output_qs)) {
2983 QDIO_PRINT_ERR("queue allocation failed!\n");
2984 qdio_release_irq_memory(irq_ptr);
2985 return -ENOMEM;
2986 }
2987
2988 init_data->cdev->private->qdio_data = irq_ptr;
2989
2990 qdio_set_state(irq_ptr,QDIO_IRQ_STATE_INACTIVE);
2991
2992 return 0;
2993}
2994
2995static int qdio_fill_irq(struct qdio_initialize *init_data)
2996{
2997 int i;
2998 char dbf_text[15];
2999 struct ciw *ciw;
3000 int is_iqdio;
3001 struct qdio_irq *irq_ptr;
3002
3003 irq_ptr = init_data->cdev->private->qdio_data;
3004
3005 memset(irq_ptr,0,((char*)&irq_ptr->qdr)-((char*)irq_ptr));
3006
3007 /* wipes qib.ac, required by ar7063 */
3008 memset(irq_ptr->qdr,0,sizeof(struct qdr));
3009
3010 irq_ptr->int_parm=init_data->int_parm;
3011
3012 irq_ptr->schid = ccw_device_get_subchannel_id(init_data->cdev);
3013 irq_ptr->no_input_qs=init_data->no_input_qs;
3014 irq_ptr->no_output_qs=init_data->no_output_qs;
3015
3016 if (init_data->q_format==QDIO_IQDIO_QFMT) {
3017 irq_ptr->is_iqdio_irq=1;
3018 irq_ptr->is_thinint_irq=1;
3019 } else {
3020 irq_ptr->is_iqdio_irq=0;
3021 irq_ptr->is_thinint_irq=hydra_thinints;
3022 }
3023 sprintf(dbf_text,"is_i_t%1x%1x",
3024 irq_ptr->is_iqdio_irq,irq_ptr->is_thinint_irq);
3025 QDIO_DBF_TEXT2(0,setup,dbf_text);
3026
3027 if (irq_ptr->is_thinint_irq) {
3028 irq_ptr->dev_st_chg_ind = qdio_get_indicator();
3029 QDIO_DBF_HEX1(0,setup,&irq_ptr->dev_st_chg_ind,sizeof(void*));
3030 if (!irq_ptr->dev_st_chg_ind) {
3031 QDIO_PRINT_WARN("no indicator location available " \
3032 "for irq 0.%x.%x\n",
3033 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
3034 qdio_release_irq_memory(irq_ptr);
3035 return -ENOBUFS;
3036 }
3037 }
3038
3039 /* defaults */
3040 irq_ptr->equeue.cmd=DEFAULT_ESTABLISH_QS_CMD;
3041 irq_ptr->equeue.count=DEFAULT_ESTABLISH_QS_COUNT;
3042 irq_ptr->aqueue.cmd=DEFAULT_ACTIVATE_QS_CMD;
3043 irq_ptr->aqueue.count=DEFAULT_ACTIVATE_QS_COUNT;
3044
3045 qdio_fill_qs(irq_ptr, init_data->cdev,
3046 init_data->no_input_qs,
3047 init_data->no_output_qs,
3048 init_data->input_handler,
3049 init_data->output_handler,init_data->int_parm,
3050 init_data->q_format,init_data->flags,
3051 init_data->input_sbal_addr_array,
3052 init_data->output_sbal_addr_array);
3053
3054 if (!try_module_get(THIS_MODULE)) {
3055 QDIO_PRINT_CRIT("try_module_get() failed!\n");
3056 qdio_release_irq_memory(irq_ptr);
3057 return -EINVAL;
3058 }
3059
3060 qdio_fill_thresholds(irq_ptr,init_data->no_input_qs,
3061 init_data->no_output_qs,
3062 init_data->min_input_threshold,
3063 init_data->max_input_threshold,
3064 init_data->min_output_threshold,
3065 init_data->max_output_threshold);
3066
3067 /* fill in qdr */
3068 irq_ptr->qdr->qfmt=init_data->q_format;
3069 irq_ptr->qdr->iqdcnt=init_data->no_input_qs;
3070 irq_ptr->qdr->oqdcnt=init_data->no_output_qs;
3071 irq_ptr->qdr->iqdsz=sizeof(struct qdesfmt0)/4; /* size in words */
3072 irq_ptr->qdr->oqdsz=sizeof(struct qdesfmt0)/4;
3073
3074 irq_ptr->qdr->qiba=(unsigned long)&irq_ptr->qib;
3075 irq_ptr->qdr->qkey=QDIO_STORAGE_KEY;
3076
3077 /* fill in qib */
3078 irq_ptr->is_qebsm = is_passthrough;
3079 if (irq_ptr->is_qebsm)
3080 irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM;
3081
3082 irq_ptr->qib.qfmt=init_data->q_format;
3083 if (init_data->no_input_qs)
3084 irq_ptr->qib.isliba=(unsigned long)(irq_ptr->input_qs[0]->slib);
3085 if (init_data->no_output_qs)
3086 irq_ptr->qib.osliba=(unsigned long)(irq_ptr->output_qs[0]->slib);
3087 memcpy(irq_ptr->qib.ebcnam,init_data->adapter_name,8);
3088
3089 qdio_set_impl_params(irq_ptr,init_data->qib_param_field_format,
3090 init_data->qib_param_field,
3091 init_data->no_input_qs,
3092 init_data->no_output_qs,
3093 init_data->input_slib_elements,
3094 init_data->output_slib_elements);
3095
3096 /* first input descriptors, then output descriptors */
3097 is_iqdio = (init_data->q_format == QDIO_IQDIO_QFMT) ? 1 : 0;
3098 for (i=0;i<init_data->no_input_qs;i++)
3099 qdio_allocate_fill_input_desc(irq_ptr, i, is_iqdio);
3100
3101 for (i=0;i<init_data->no_output_qs;i++)
3102 qdio_allocate_fill_output_desc(irq_ptr, i,
3103 init_data->no_input_qs,
3104 is_iqdio);
3105
3106 /* qdr, qib, sls, slsbs, slibs, sbales filled. */
3107
3108 /* get qdio commands */
3109 ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_EQUEUE);
3110 if (!ciw) {
3111 QDIO_DBF_TEXT2(1,setup,"no eq");
3112 QDIO_PRINT_INFO("No equeue CIW found for QDIO commands. "
3113 "Trying to use default.\n");
3114 } else
3115 irq_ptr->equeue = *ciw;
3116 ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_AQUEUE);
3117 if (!ciw) {
3118 QDIO_DBF_TEXT2(1,setup,"no aq");
3119 QDIO_PRINT_INFO("No aqueue CIW found for QDIO commands. "
3120 "Trying to use default.\n");
3121 } else
3122 irq_ptr->aqueue = *ciw;
3123
3124 /* Set new interrupt handler. */
3125 irq_ptr->original_int_handler = init_data->cdev->handler;
3126 init_data->cdev->handler = qdio_handler;
3127
3128 return 0;
3129}
3130
3131int
3132qdio_establish(struct qdio_initialize *init_data)
3133{
3134 struct qdio_irq *irq_ptr;
3135 unsigned long saveflags;
3136 int result, result2;
3137 struct ccw_device *cdev;
3138 char dbf_text[20];
3139
3140 cdev=init_data->cdev;
3141 irq_ptr = cdev->private->qdio_data;
3142 if (!irq_ptr)
3143 return -EINVAL;
3144
3145 if (cdev->private->state != DEV_STATE_ONLINE)
3146 return -EINVAL;
3147
3148 down(&irq_ptr->setting_up_sema);
3149
3150 qdio_fill_irq(init_data);
3151
3152 /* the thinint CHSC stuff */
3153 if (irq_ptr->is_thinint_irq) {
3154
3155 result = tiqdio_set_subchannel_ind(irq_ptr,0);
3156 if (result) {
3157 up(&irq_ptr->setting_up_sema);
3158 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
3159 return result;
3160 }
3161 tiqdio_set_delay_target(irq_ptr,TIQDIO_DELAY_TARGET);
3162 }
3163
3164 sprintf(dbf_text,"qest%4x",cdev->private->schid.sch_no);
3165 QDIO_DBF_TEXT0(0,setup,dbf_text);
3166 QDIO_DBF_TEXT0(0,trace,dbf_text);
3167
3168 /* establish q */
3169 irq_ptr->ccw.cmd_code=irq_ptr->equeue.cmd;
3170 irq_ptr->ccw.flags=CCW_FLAG_SLI;
3171 irq_ptr->ccw.count=irq_ptr->equeue.count;
3172 irq_ptr->ccw.cda=QDIO_GET_ADDR(irq_ptr->qdr);
3173
3174 spin_lock_irqsave(get_ccwdev_lock(cdev),saveflags);
3175
3176 ccw_device_set_options_mask(cdev, 0);
3177 result = ccw_device_start(cdev, &irq_ptr->ccw,
3178 QDIO_DOING_ESTABLISH, 0, 0);
3179 if (result) {
3180 result2 = ccw_device_start(cdev, &irq_ptr->ccw,
3181 QDIO_DOING_ESTABLISH, 0, 0);
3182 sprintf(dbf_text,"eq:io%4x",result);
3183 QDIO_DBF_TEXT2(1,setup,dbf_text);
3184 if (result2) {
3185 sprintf(dbf_text,"eq:io%4x",result);
3186 QDIO_DBF_TEXT2(1,setup,dbf_text);
3187 }
3188 QDIO_PRINT_WARN("establish queues on irq 0.%x.%04x: do_IO " \
3189 "returned %i, next try returned %i\n",
3190 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
3191 result, result2);
3192 result=result2;
3193 }
3194
3195 spin_unlock_irqrestore(get_ccwdev_lock(cdev),saveflags);
3196
3197 if (result) {
3198 up(&irq_ptr->setting_up_sema);
3199 qdio_shutdown(cdev,QDIO_FLAG_CLEANUP_USING_CLEAR);
3200 return result;
3201 }
3202
3203 wait_event_interruptible_timeout(cdev->private->wait_q,
3204 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
3205 irq_ptr->state == QDIO_IRQ_STATE_ERR,
3206 QDIO_ESTABLISH_TIMEOUT);
3207
3208 if (irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED)
3209 result = 0;
3210 else {
3211 up(&irq_ptr->setting_up_sema);
3212 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
3213 return -EIO;
3214 }
3215
3216 qdio_get_ssqd_siga(irq_ptr);
3217 /* if this gets set once, we're running under VM and can omit SVSes */
3218 if (irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_NECESSARY)
3219 omit_svs=1;
3220
3221 sprintf(dbf_text,"qdioac%2x",irq_ptr->qdioac);
3222 QDIO_DBF_TEXT2(0,setup,dbf_text);
3223
3224 sprintf(dbf_text,"qib ac%2x",irq_ptr->qib.ac);
3225 QDIO_DBF_TEXT2(0,setup,dbf_text);
3226
3227 irq_ptr->hydra_gives_outbound_pcis=
3228 irq_ptr->qib.ac&QIB_AC_OUTBOUND_PCI_SUPPORTED;
3229 irq_ptr->sync_done_on_outb_pcis=
3230 irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS;
3231
3232 qdio_initialize_set_siga_flags_input(irq_ptr);
3233 qdio_initialize_set_siga_flags_output(irq_ptr);
3234
3235 up(&irq_ptr->setting_up_sema);
3236
3237 return result;
3238
3239}
3240
3241int
3242qdio_activate(struct ccw_device *cdev, int flags)
3243{
3244 struct qdio_irq *irq_ptr;
3245 int i,result=0,result2;
3246 unsigned long saveflags;
3247 char dbf_text[20]; /* see qdio_initialize */
3248
3249 irq_ptr = cdev->private->qdio_data;
3250 if (!irq_ptr)
3251 return -ENODEV;
3252
3253 if (cdev->private->state != DEV_STATE_ONLINE)
3254 return -EINVAL;
3255
3256 down(&irq_ptr->setting_up_sema);
3257 if (irq_ptr->state==QDIO_IRQ_STATE_INACTIVE) {
3258 result=-EBUSY;
3259 goto out;
3260 }
3261
3262 sprintf(dbf_text,"qact%4x", irq_ptr->schid.sch_no);
3263 QDIO_DBF_TEXT2(0,setup,dbf_text);
3264 QDIO_DBF_TEXT2(0,trace,dbf_text);
3265
3266 /* activate q */
3267 irq_ptr->ccw.cmd_code=irq_ptr->aqueue.cmd;
3268 irq_ptr->ccw.flags=CCW_FLAG_SLI;
3269 irq_ptr->ccw.count=irq_ptr->aqueue.count;
3270 irq_ptr->ccw.cda=QDIO_GET_ADDR(0);
3271
3272 spin_lock_irqsave(get_ccwdev_lock(cdev),saveflags);
3273
3274 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
3275 result=ccw_device_start(cdev,&irq_ptr->ccw,QDIO_DOING_ACTIVATE,
3276 0, DOIO_DENY_PREFETCH);
3277 if (result) {
3278 result2=ccw_device_start(cdev,&irq_ptr->ccw,
3279 QDIO_DOING_ACTIVATE,0,0);
3280 sprintf(dbf_text,"aq:io%4x",result);
3281 QDIO_DBF_TEXT2(1,setup,dbf_text);
3282 if (result2) {
3283 sprintf(dbf_text,"aq:io%4x",result);
3284 QDIO_DBF_TEXT2(1,setup,dbf_text);
3285 }
3286 QDIO_PRINT_WARN("activate queues on irq 0.%x.%04x: do_IO " \
3287 "returned %i, next try returned %i\n",
3288 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
3289 result, result2);
3290 result=result2;
3291 }
3292
3293 spin_unlock_irqrestore(get_ccwdev_lock(cdev),saveflags);
3294 if (result)
3295 goto out;
3296
3297 for (i=0;i<irq_ptr->no_input_qs;i++) {
3298 if (irq_ptr->is_thinint_irq) {
3299 /*
3300 * that way we know, that, if we will get interrupted
3301 * by tiqdio_inbound_processing, qdio_unmark_q will
3302 * not be called
3303 */
3304 qdio_reserve_q(irq_ptr->input_qs[i]);
3305 qdio_mark_tiq(irq_ptr->input_qs[i]);
3306 qdio_release_q(irq_ptr->input_qs[i]);
3307 }
3308 }
3309
3310 if (flags&QDIO_FLAG_NO_INPUT_INTERRUPT_CONTEXT) {
3311 for (i=0;i<irq_ptr->no_input_qs;i++) {
3312 irq_ptr->input_qs[i]->is_input_q|=
3313 QDIO_FLAG_NO_INPUT_INTERRUPT_CONTEXT;
3314 }
3315 }
3316
3317 msleep(QDIO_ACTIVATE_TIMEOUT);
3318 switch (irq_ptr->state) {
3319 case QDIO_IRQ_STATE_STOPPED:
3320 case QDIO_IRQ_STATE_ERR:
3321 up(&irq_ptr->setting_up_sema);
3322 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
3323 down(&irq_ptr->setting_up_sema);
3324 result = -EIO;
3325 break;
3326 default:
3327 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
3328 result = 0;
3329 }
3330 out:
3331 up(&irq_ptr->setting_up_sema);
3332
3333 return result;
3334}
3335
3336/* buffers filled forwards again to make Rick happy */
3337static void
3338qdio_do_qdio_fill_input(struct qdio_q *q, unsigned int qidx,
3339 unsigned int count, struct qdio_buffer *buffers)
3340{
3341 struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
3342 int tmp = 0;
3343
3344 qidx &= (QDIO_MAX_BUFFERS_PER_Q - 1);
3345 if (irq->is_qebsm) {
3346 while (count) {
3347 tmp = set_slsb(q, &qidx, SLSB_CU_INPUT_EMPTY, &count);
3348 if (!tmp)
3349 return;
3350 }
3351 return;
3352 }
3353 for (;;) {
3354 set_slsb(q, &qidx, SLSB_CU_INPUT_EMPTY, &count);
3355 count--;
3356 if (!count) break;
3357 qidx = (qidx + 1) & (QDIO_MAX_BUFFERS_PER_Q - 1);
3358 }
3359}
3360
3361static void
3362qdio_do_qdio_fill_output(struct qdio_q *q, unsigned int qidx,
3363 unsigned int count, struct qdio_buffer *buffers)
3364{
3365 struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
3366 int tmp = 0;
3367
3368 qidx &= (QDIO_MAX_BUFFERS_PER_Q - 1);
3369 if (irq->is_qebsm) {
3370 while (count) {
3371 tmp = set_slsb(q, &qidx, SLSB_CU_OUTPUT_PRIMED, &count);
3372 if (!tmp)
3373 return;
3374 }
3375 return;
3376 }
3377
3378 for (;;) {
3379 set_slsb(q, &qidx, SLSB_CU_OUTPUT_PRIMED, &count);
3380 count--;
3381 if (!count) break;
3382 qidx = (qidx + 1) & (QDIO_MAX_BUFFERS_PER_Q - 1);
3383 }
3384}
3385
3386static void
3387do_qdio_handle_inbound(struct qdio_q *q, unsigned int callflags,
3388 unsigned int qidx, unsigned int count,
3389 struct qdio_buffer *buffers)
3390{
3391 int used_elements;
3392
3393 /* This is the inbound handling of queues */
3394 used_elements=atomic_add_return(count, &q->number_of_buffers_used) - count;
3395
3396 qdio_do_qdio_fill_input(q,qidx,count,buffers);
3397
3398 if ((used_elements+count==QDIO_MAX_BUFFERS_PER_Q)&&
3399 (callflags&QDIO_FLAG_UNDER_INTERRUPT))
3400 atomic_xchg(&q->polling,0);
3401
3402 if (used_elements)
3403 return;
3404 if (callflags&QDIO_FLAG_DONT_SIGA)
3405 return;
3406 if (q->siga_in) {
3407 int result;
3408
3409 result=qdio_siga_input(q);
3410 if (result) {
3411 if (q->siga_error)
3412 q->error_status_flags|=
3413 QDIO_STATUS_MORE_THAN_ONE_SIGA_ERROR;
3414 q->error_status_flags|=QDIO_STATUS_LOOK_FOR_ERROR;
3415 q->siga_error=result;
3416 }
3417 }
3418
3419 qdio_mark_q(q);
3420}
3421
3422static void
3423do_qdio_handle_outbound(struct qdio_q *q, unsigned int callflags,
3424 unsigned int qidx, unsigned int count,
3425 struct qdio_buffer *buffers)
3426{
3427 int used_elements;
3428 unsigned int cnt, start_buf;
3429 unsigned char state = 0;
3430 struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
3431
3432 /* This is the outbound handling of queues */
3433 qdio_do_qdio_fill_output(q,qidx,count,buffers);
3434
3435 used_elements=atomic_add_return(count, &q->number_of_buffers_used) - count;
3436
3437 if (callflags&QDIO_FLAG_DONT_SIGA) {
3438 qdio_perf_stat_inc(&perf_stats.outbound_cnt);
3439 return;
3440 }
3441 if (callflags & QDIO_FLAG_PCI_OUT)
3442 q->is_pci_out = 1;
3443 else
3444 q->is_pci_out = 0;
3445 if (q->is_iqdio_q) {
3446 /* one siga for every sbal */
3447 while (count--)
3448 qdio_kick_outbound_q(q);
3449
3450 __qdio_outbound_processing(q);
3451 } else {
3452 /* under VM, we do a SIGA sync unconditionally */
3453 SYNC_MEMORY;
3454 else {
3455 /*
3456 * w/o shadow queues (else branch of
3457 * SYNC_MEMORY :-/ ), we try to
3458 * fast-requeue buffers
3459 */
3460 if (irq->is_qebsm) {
3461 cnt = 1;
3462 start_buf = ((qidx+QDIO_MAX_BUFFERS_PER_Q-1) &
3463 (QDIO_MAX_BUFFERS_PER_Q-1));
3464 qdio_do_eqbs(q, &state, &start_buf, &cnt);
3465 } else
3466 state = q->slsb.acc.val[(qidx+QDIO_MAX_BUFFERS_PER_Q-1)
3467 &(QDIO_MAX_BUFFERS_PER_Q-1) ];
3468 if (state != SLSB_CU_OUTPUT_PRIMED) {
3469 qdio_kick_outbound_q(q);
3470 } else {
3471 QDIO_DBF_TEXT3(0,trace, "fast-req");
3472 qdio_perf_stat_inc(&perf_stats.fast_reqs);
3473 }
3474 }
3475 /*
3476 * only marking the q could take too long,
3477 * the upper layer module could do a lot of
3478 * traffic in that time
3479 */
3480 __qdio_outbound_processing(q);
3481 }
3482
3483 qdio_perf_stat_inc(&perf_stats.outbound_cnt);
3484}
3485
3486/* count must be 1 in iqdio */
3487int
3488do_QDIO(struct ccw_device *cdev,unsigned int callflags,
3489 unsigned int queue_number, unsigned int qidx,
3490 unsigned int count,struct qdio_buffer *buffers)
3491{
3492 struct qdio_irq *irq_ptr;
3493#ifdef CONFIG_QDIO_DEBUG
3494 char dbf_text[20];
3495
3496 sprintf(dbf_text,"doQD%04x",cdev->private->schid.sch_no);
3497 QDIO_DBF_TEXT3(0,trace,dbf_text);
3498#endif /* CONFIG_QDIO_DEBUG */
3499
3500 if ( (qidx>QDIO_MAX_BUFFERS_PER_Q) ||
3501 (count>QDIO_MAX_BUFFERS_PER_Q) ||
3502 (queue_number>QDIO_MAX_QUEUES_PER_IRQ) )
3503 return -EINVAL;
3504
3505 if (count==0)
3506 return 0;
3507
3508 irq_ptr = cdev->private->qdio_data;
3509 if (!irq_ptr)
3510 return -ENODEV;
3511
3512#ifdef CONFIG_QDIO_DEBUG
3513 if (callflags&QDIO_FLAG_SYNC_INPUT)
3514 QDIO_DBF_HEX3(0,trace,&irq_ptr->input_qs[queue_number],
3515 sizeof(void*));
3516 else
3517 QDIO_DBF_HEX3(0,trace,&irq_ptr->output_qs[queue_number],
3518 sizeof(void*));
3519 sprintf(dbf_text,"flag%04x",callflags);
3520 QDIO_DBF_TEXT3(0,trace,dbf_text);
3521 sprintf(dbf_text,"qi%02xct%02x",qidx,count);
3522 QDIO_DBF_TEXT3(0,trace,dbf_text);
3523#endif /* CONFIG_QDIO_DEBUG */
3524
3525 if (irq_ptr->state!=QDIO_IRQ_STATE_ACTIVE)
3526 return -EBUSY;
3527
3528 if (callflags&QDIO_FLAG_SYNC_INPUT)
3529 do_qdio_handle_inbound(irq_ptr->input_qs[queue_number],
3530 callflags, qidx, count, buffers);
3531 else if (callflags&QDIO_FLAG_SYNC_OUTPUT)
3532 do_qdio_handle_outbound(irq_ptr->output_qs[queue_number],
3533 callflags, qidx, count, buffers);
3534 else {
3535 QDIO_DBF_TEXT3(1,trace,"doQD:inv");
3536 return -EINVAL;
3537 }
3538 return 0;
3539}
3540
3541static int
3542qdio_perf_procfile_read(char *buffer, char **buffer_location, off_t offset,
3543 int buffer_length, int *eof, void *data)
3544{
3545 int c=0;
3546
3547 /* we are always called with buffer_length=4k, so we all
3548 deliver on the first read */
3549 if (offset>0)
3550 return 0;
3551
3552#define _OUTP_IT(x...) c+=sprintf(buffer+c,x)
3553#ifdef CONFIG_64BIT
3554 _OUTP_IT("Number of tasklet runs (total) : %li\n",
3555 (long)atomic64_read(&perf_stats.tl_runs));
3556 _OUTP_IT("Inbound tasklet runs tried/retried : %li/%li\n",
3557 (long)atomic64_read(&perf_stats.inbound_tl_runs),
3558 (long)atomic64_read(&perf_stats.inbound_tl_runs_resched));
3559 _OUTP_IT("Inbound-thin tasklet runs tried/retried : %li/%li\n",
3560 (long)atomic64_read(&perf_stats.inbound_thin_tl_runs),
3561 (long)atomic64_read(&perf_stats.inbound_thin_tl_runs_resched));
3562 _OUTP_IT("Outbound tasklet runs tried/retried : %li/%li\n",
3563 (long)atomic64_read(&perf_stats.outbound_tl_runs),
3564 (long)atomic64_read(&perf_stats.outbound_tl_runs_resched));
3565 _OUTP_IT("\n");
3566 _OUTP_IT("Number of SIGA sync's issued : %li\n",
3567 (long)atomic64_read(&perf_stats.siga_syncs));
3568 _OUTP_IT("Number of SIGA in's issued : %li\n",
3569 (long)atomic64_read(&perf_stats.siga_ins));
3570 _OUTP_IT("Number of SIGA out's issued : %li\n",
3571 (long)atomic64_read(&perf_stats.siga_outs));
3572 _OUTP_IT("Number of PCIs caught : %li\n",
3573 (long)atomic64_read(&perf_stats.pcis));
3574 _OUTP_IT("Number of adapter interrupts caught : %li\n",
3575 (long)atomic64_read(&perf_stats.thinints));
3576 _OUTP_IT("Number of fast requeues (outg. SBALs w/o SIGA) : %li\n",
3577 (long)atomic64_read(&perf_stats.fast_reqs));
3578 _OUTP_IT("\n");
3579 _OUTP_IT("Number of inbound transfers : %li\n",
3580 (long)atomic64_read(&perf_stats.inbound_cnt));
3581 _OUTP_IT("Number of do_QDIOs outbound : %li\n",
3582 (long)atomic64_read(&perf_stats.outbound_cnt));
3583#else /* CONFIG_64BIT */
3584 _OUTP_IT("Number of tasklet runs (total) : %i\n",
3585 atomic_read(&perf_stats.tl_runs));
3586 _OUTP_IT("Inbound tasklet runs tried/retried : %i/%i\n",
3587 atomic_read(&perf_stats.inbound_tl_runs),
3588 atomic_read(&perf_stats.inbound_tl_runs_resched));
3589 _OUTP_IT("Inbound-thin tasklet runs tried/retried : %i/%i\n",
3590 atomic_read(&perf_stats.inbound_thin_tl_runs),
3591 atomic_read(&perf_stats.inbound_thin_tl_runs_resched));
3592 _OUTP_IT("Outbound tasklet runs tried/retried : %i/%i\n",
3593 atomic_read(&perf_stats.outbound_tl_runs),
3594 atomic_read(&perf_stats.outbound_tl_runs_resched));
3595 _OUTP_IT("\n");
3596 _OUTP_IT("Number of SIGA sync's issued : %i\n",
3597 atomic_read(&perf_stats.siga_syncs));
3598 _OUTP_IT("Number of SIGA in's issued : %i\n",
3599 atomic_read(&perf_stats.siga_ins));
3600 _OUTP_IT("Number of SIGA out's issued : %i\n",
3601 atomic_read(&perf_stats.siga_outs));
3602 _OUTP_IT("Number of PCIs caught : %i\n",
3603 atomic_read(&perf_stats.pcis));
3604 _OUTP_IT("Number of adapter interrupts caught : %i\n",
3605 atomic_read(&perf_stats.thinints));
3606 _OUTP_IT("Number of fast requeues (outg. SBALs w/o SIGA) : %i\n",
3607 atomic_read(&perf_stats.fast_reqs));
3608 _OUTP_IT("\n");
3609 _OUTP_IT("Number of inbound transfers : %i\n",
3610 atomic_read(&perf_stats.inbound_cnt));
3611 _OUTP_IT("Number of do_QDIOs outbound : %i\n",
3612 atomic_read(&perf_stats.outbound_cnt));
3613#endif /* CONFIG_64BIT */
3614 _OUTP_IT("\n");
3615
3616 return c;
3617}
3618
3619static struct proc_dir_entry *qdio_perf_proc_file;
3620
3621static void
3622qdio_add_procfs_entry(void)
3623{
3624 proc_perf_file_registration=0;
3625 qdio_perf_proc_file=create_proc_entry(QDIO_PERF,
3626 S_IFREG|0444,NULL);
3627 if (qdio_perf_proc_file) {
3628 qdio_perf_proc_file->read_proc=&qdio_perf_procfile_read;
3629 } else proc_perf_file_registration=-1;
3630
3631 if (proc_perf_file_registration)
3632 QDIO_PRINT_WARN("was not able to register perf. " \
3633 "proc-file (%i).\n",
3634 proc_perf_file_registration);
3635}
3636
3637static void
3638qdio_remove_procfs_entry(void)
3639{
3640 if (!proc_perf_file_registration) /* means if it went ok earlier */
3641 remove_proc_entry(QDIO_PERF,NULL);
3642}
3643
3644/**
3645 * attributes in sysfs
3646 *****************************************************************************/
3647
3648static ssize_t
3649qdio_performance_stats_show(struct bus_type *bus, char *buf)
3650{
3651 return sprintf(buf, "%i\n", qdio_performance_stats ? 1 : 0);
3652}
3653
3654static ssize_t
3655qdio_performance_stats_store(struct bus_type *bus, const char *buf, size_t count)
3656{
3657 unsigned long i;
3658 int ret;
3659
3660 ret = strict_strtoul(buf, 16, &i);
3661 if (!ret && ((i == 0) || (i == 1))) {
3662 if (i == qdio_performance_stats)
3663 return count;
3664 qdio_performance_stats = i;
3665 if (i==0) {
3666 /* reset perf. stat. info */
3667#ifdef CONFIG_64BIT
3668 atomic64_set(&perf_stats.tl_runs, 0);
3669 atomic64_set(&perf_stats.outbound_tl_runs, 0);
3670 atomic64_set(&perf_stats.inbound_tl_runs, 0);
3671 atomic64_set(&perf_stats.inbound_tl_runs_resched, 0);
3672 atomic64_set(&perf_stats.inbound_thin_tl_runs, 0);
3673 atomic64_set(&perf_stats.inbound_thin_tl_runs_resched,
3674 0);
3675 atomic64_set(&perf_stats.siga_outs, 0);
3676 atomic64_set(&perf_stats.siga_ins, 0);
3677 atomic64_set(&perf_stats.siga_syncs, 0);
3678 atomic64_set(&perf_stats.pcis, 0);
3679 atomic64_set(&perf_stats.thinints, 0);
3680 atomic64_set(&perf_stats.fast_reqs, 0);
3681 atomic64_set(&perf_stats.outbound_cnt, 0);
3682 atomic64_set(&perf_stats.inbound_cnt, 0);
3683#else /* CONFIG_64BIT */
3684 atomic_set(&perf_stats.tl_runs, 0);
3685 atomic_set(&perf_stats.outbound_tl_runs, 0);
3686 atomic_set(&perf_stats.inbound_tl_runs, 0);
3687 atomic_set(&perf_stats.inbound_tl_runs_resched, 0);
3688 atomic_set(&perf_stats.inbound_thin_tl_runs, 0);
3689 atomic_set(&perf_stats.inbound_thin_tl_runs_resched, 0);
3690 atomic_set(&perf_stats.siga_outs, 0);
3691 atomic_set(&perf_stats.siga_ins, 0);
3692 atomic_set(&perf_stats.siga_syncs, 0);
3693 atomic_set(&perf_stats.pcis, 0);
3694 atomic_set(&perf_stats.thinints, 0);
3695 atomic_set(&perf_stats.fast_reqs, 0);
3696 atomic_set(&perf_stats.outbound_cnt, 0);
3697 atomic_set(&perf_stats.inbound_cnt, 0);
3698#endif /* CONFIG_64BIT */
3699 }
3700 } else {
3701 QDIO_PRINT_ERR("QDIO performance_stats: write 0 or 1 to this file!\n");
3702 return -EINVAL;
3703 }
3704 return count;
3705}
3706
3707static BUS_ATTR(qdio_performance_stats, 0644, qdio_performance_stats_show,
3708 qdio_performance_stats_store);
3709
3710static void
3711tiqdio_register_thinints(void)
3712{
3713 char dbf_text[20];
3714
3715 tiqdio_ind =
3716 s390_register_adapter_interrupt(&tiqdio_thinint_handler, NULL,
3717 TIQDIO_THININT_ISC);
3718 if (IS_ERR(tiqdio_ind)) {
3719 sprintf(dbf_text, "regthn%lx", PTR_ERR(tiqdio_ind));
3720 QDIO_DBF_TEXT0(0,setup,dbf_text);
3721 QDIO_PRINT_ERR("failed to register adapter handler " \
3722 "(rc=%li).\nAdapter interrupts might " \
3723 "not work. Continuing.\n",
3724 PTR_ERR(tiqdio_ind));
3725 tiqdio_ind = NULL;
3726 }
3727}
3728
3729static void
3730tiqdio_unregister_thinints(void)
3731{
3732 if (tiqdio_ind)
3733 s390_unregister_adapter_interrupt(tiqdio_ind,
3734 TIQDIO_THININT_ISC);
3735}
3736
3737static int
3738qdio_get_qdio_memory(void)
3739{
3740 int i;
3741 indicator_used[0]=1;
3742
3743 for (i=1;i<INDICATORS_PER_CACHELINE;i++)
3744 indicator_used[i]=0;
3745 indicators = kzalloc(sizeof(__u32)*(INDICATORS_PER_CACHELINE),
3746 GFP_KERNEL);
3747 if (!indicators)
3748 return -ENOMEM;
3749 return 0;
3750}
3751
3752static void
3753qdio_release_qdio_memory(void)
3754{
3755 kfree(indicators);
3756}
3757
3758static void
3759qdio_unregister_dbf_views(void)
3760{
3761 if (qdio_dbf_setup)
3762 debug_unregister(qdio_dbf_setup);
3763 if (qdio_dbf_sbal)
3764 debug_unregister(qdio_dbf_sbal);
3765 if (qdio_dbf_sense)
3766 debug_unregister(qdio_dbf_sense);
3767 if (qdio_dbf_trace)
3768 debug_unregister(qdio_dbf_trace);
3769#ifdef CONFIG_QDIO_DEBUG
3770 if (qdio_dbf_slsb_out)
3771 debug_unregister(qdio_dbf_slsb_out);
3772 if (qdio_dbf_slsb_in)
3773 debug_unregister(qdio_dbf_slsb_in);
3774#endif /* CONFIG_QDIO_DEBUG */
3775}
3776
3777static int
3778qdio_register_dbf_views(void)
3779{
3780 qdio_dbf_setup=debug_register(QDIO_DBF_SETUP_NAME,
3781 QDIO_DBF_SETUP_PAGES,
3782 QDIO_DBF_SETUP_NR_AREAS,
3783 QDIO_DBF_SETUP_LEN);
3784 if (!qdio_dbf_setup)
3785 goto oom;
3786 debug_register_view(qdio_dbf_setup,&debug_hex_ascii_view);
3787 debug_set_level(qdio_dbf_setup,QDIO_DBF_SETUP_LEVEL);
3788
3789 qdio_dbf_sbal=debug_register(QDIO_DBF_SBAL_NAME,
3790 QDIO_DBF_SBAL_PAGES,
3791 QDIO_DBF_SBAL_NR_AREAS,
3792 QDIO_DBF_SBAL_LEN);
3793 if (!qdio_dbf_sbal)
3794 goto oom;
3795
3796 debug_register_view(qdio_dbf_sbal,&debug_hex_ascii_view);
3797 debug_set_level(qdio_dbf_sbal,QDIO_DBF_SBAL_LEVEL);
3798
3799 qdio_dbf_sense=debug_register(QDIO_DBF_SENSE_NAME,
3800 QDIO_DBF_SENSE_PAGES,
3801 QDIO_DBF_SENSE_NR_AREAS,
3802 QDIO_DBF_SENSE_LEN);
3803 if (!qdio_dbf_sense)
3804 goto oom;
3805
3806 debug_register_view(qdio_dbf_sense,&debug_hex_ascii_view);
3807 debug_set_level(qdio_dbf_sense,QDIO_DBF_SENSE_LEVEL);
3808
3809 qdio_dbf_trace=debug_register(QDIO_DBF_TRACE_NAME,
3810 QDIO_DBF_TRACE_PAGES,
3811 QDIO_DBF_TRACE_NR_AREAS,
3812 QDIO_DBF_TRACE_LEN);
3813 if (!qdio_dbf_trace)
3814 goto oom;
3815
3816 debug_register_view(qdio_dbf_trace,&debug_hex_ascii_view);
3817 debug_set_level(qdio_dbf_trace,QDIO_DBF_TRACE_LEVEL);
3818
3819#ifdef CONFIG_QDIO_DEBUG
3820 qdio_dbf_slsb_out=debug_register(QDIO_DBF_SLSB_OUT_NAME,
3821 QDIO_DBF_SLSB_OUT_PAGES,
3822 QDIO_DBF_SLSB_OUT_NR_AREAS,
3823 QDIO_DBF_SLSB_OUT_LEN);
3824 if (!qdio_dbf_slsb_out)
3825 goto oom;
3826 debug_register_view(qdio_dbf_slsb_out,&debug_hex_ascii_view);
3827 debug_set_level(qdio_dbf_slsb_out,QDIO_DBF_SLSB_OUT_LEVEL);
3828
3829 qdio_dbf_slsb_in=debug_register(QDIO_DBF_SLSB_IN_NAME,
3830 QDIO_DBF_SLSB_IN_PAGES,
3831 QDIO_DBF_SLSB_IN_NR_AREAS,
3832 QDIO_DBF_SLSB_IN_LEN);
3833 if (!qdio_dbf_slsb_in)
3834 goto oom;
3835 debug_register_view(qdio_dbf_slsb_in,&debug_hex_ascii_view);
3836 debug_set_level(qdio_dbf_slsb_in,QDIO_DBF_SLSB_IN_LEVEL);
3837#endif /* CONFIG_QDIO_DEBUG */
3838 return 0;
3839oom:
3840 QDIO_PRINT_ERR("not enough memory for dbf.\n");
3841 qdio_unregister_dbf_views();
3842 return -ENOMEM;
3843}
3844
3845static void *qdio_mempool_alloc(gfp_t gfp_mask, void *size)
3846{
3847 return (void *) get_zeroed_page(gfp_mask|GFP_DMA);
3848}
3849
3850static void qdio_mempool_free(void *element, void *size)
3851{
3852 free_page((unsigned long) element);
3853}
3854
3855static int __init
3856init_QDIO(void)
3857{
3858 int res;
3859 void *ptr;
3860
3861 printk("qdio: loading %s\n",version);
3862
3863 res=qdio_get_qdio_memory();
3864 if (res)
3865 return res;
3866
3867 qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q),
3868 256, 0, NULL);
3869 if (!qdio_q_cache) {
3870 qdio_release_qdio_memory();
3871 return -ENOMEM;
3872 }
3873
3874 res = qdio_register_dbf_views();
3875 if (res) {
3876 kmem_cache_destroy(qdio_q_cache);
3877 qdio_release_qdio_memory();
3878 return res;
3879 }
3880
3881 QDIO_DBF_TEXT0(0,setup,"initQDIO");
3882 res = bus_create_file(&ccw_bus_type, &bus_attr_qdio_performance_stats);
3883
3884 memset((void*)&perf_stats,0,sizeof(perf_stats));
3885 QDIO_DBF_TEXT0(0,setup,"perfstat");
3886 ptr=&perf_stats;
3887 QDIO_DBF_HEX0(0,setup,&ptr,sizeof(void*));
3888
3889 qdio_add_procfs_entry();
3890
3891 qdio_mempool_scssc = mempool_create(QDIO_MEMPOOL_SCSSC_ELEMENTS,
3892 qdio_mempool_alloc,
3893 qdio_mempool_free, NULL);
3894
3895 isc_register(QDIO_AIRQ_ISC);
3896 if (tiqdio_check_chsc_availability())
3897 QDIO_PRINT_ERR("Not all CHSCs supported. Continuing.\n");
3898
3899 tiqdio_register_thinints();
3900
3901 return 0;
3902 }
3903
3904static void __exit
3905cleanup_QDIO(void)
3906{
3907 tiqdio_unregister_thinints();
3908 isc_unregister(QDIO_AIRQ_ISC);
3909 qdio_remove_procfs_entry();
3910 qdio_release_qdio_memory();
3911 qdio_unregister_dbf_views();
3912 mempool_destroy(qdio_mempool_scssc);
3913 kmem_cache_destroy(qdio_q_cache);
3914 bus_remove_file(&ccw_bus_type, &bus_attr_qdio_performance_stats);
3915 printk("qdio: %s: module removed\n",version);
3916}
3917
3918module_init(init_QDIO);
3919module_exit(cleanup_QDIO);
3920
3921EXPORT_SYMBOL(qdio_allocate);
3922EXPORT_SYMBOL(qdio_establish);
3923EXPORT_SYMBOL(qdio_initialize);
3924EXPORT_SYMBOL(qdio_activate);
3925EXPORT_SYMBOL(do_QDIO);
3926EXPORT_SYMBOL(qdio_shutdown);
3927EXPORT_SYMBOL(qdio_free);
3928EXPORT_SYMBOL(qdio_cleanup);
3929EXPORT_SYMBOL(qdio_synchronize);
diff --git a/drivers/s390/cio/qdio.h b/drivers/s390/cio/qdio.h
index 7656081a24d2..c1a70985abfa 100644
--- a/drivers/s390/cio/qdio.h
+++ b/drivers/s390/cio/qdio.h
@@ -1,66 +1,20 @@
1/*
2 * linux/drivers/s390/cio/qdio.h
3 *
4 * Copyright 2000,2008 IBM Corp.
5 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
6 * Jan Glauber <jang@linux.vnet.ibm.com>
7 */
1#ifndef _CIO_QDIO_H 8#ifndef _CIO_QDIO_H
2#define _CIO_QDIO_H 9#define _CIO_QDIO_H
3 10
4#include <asm/page.h> 11#include <asm/page.h>
5#include <asm/isc.h>
6#include <asm/schid.h> 12#include <asm/schid.h>
13#include "chsc.h"
7 14
8#ifdef CONFIG_QDIO_DEBUG 15#define QDIO_BUSY_BIT_PATIENCE 100 /* 100 microseconds */
9#define QDIO_VERBOSE_LEVEL 9 16#define QDIO_BUSY_BIT_GIVE_UP 2000000 /* 2 seconds = eternity */
10#else /* CONFIG_QDIO_DEBUG */ 17#define QDIO_INPUT_THRESHOLD 500 /* 500 microseconds */
11#define QDIO_VERBOSE_LEVEL 5
12#endif /* CONFIG_QDIO_DEBUG */
13#define QDIO_USE_PROCESSING_STATE
14
15#define QDIO_MINIMAL_BH_RELIEF_TIME 16
16#define QDIO_TIMER_POLL_VALUE 1
17#define IQDIO_TIMER_POLL_VALUE 1
18
19/*
20 * unfortunately this can't be (QDIO_MAX_BUFFERS_PER_Q*4/3) or so -- as
21 * we never know, whether we'll get initiative again, e.g. to give the
22 * transmit skb's back to the stack, however the stack may be waiting for
23 * them... therefore we define 4 as threshold to start polling (which
24 * will stop as soon as the asynchronous queue catches up)
25 * btw, this only applies to the asynchronous HiperSockets queue
26 */
27#define IQDIO_FILL_LEVEL_TO_POLL 4
28
29#define TIQDIO_THININT_ISC QDIO_AIRQ_ISC
30#define TIQDIO_DELAY_TARGET 0
31#define QDIO_BUSY_BIT_PATIENCE 100 /* in microsecs */
32#define QDIO_BUSY_BIT_GIVE_UP 10000000 /* 10 seconds */
33#define IQDIO_GLOBAL_LAPS 2 /* GLOBAL_LAPS are not used as we */
34#define IQDIO_GLOBAL_LAPS_INT 1 /* don't global summary */
35#define IQDIO_LOCAL_LAPS 4
36#define IQDIO_LOCAL_LAPS_INT 1
37#define IQDIO_GLOBAL_SUMMARY_CC_MASK 2
38/*#define IQDIO_IQDC_INT_PARM 0x1234*/
39
40#define QDIO_Q_LAPS 5
41
42#define QDIO_STORAGE_KEY PAGE_DEFAULT_KEY
43
44#define L2_CACHELINE_SIZE 256
45#define INDICATORS_PER_CACHELINE (L2_CACHELINE_SIZE/sizeof(__u32))
46
47#define QDIO_PERF "qdio_perf"
48
49/* must be a power of 2 */
50/*#define QDIO_STATS_NUMBER 4
51
52#define QDIO_STATS_CLASSES 2
53#define QDIO_STATS_COUNT_NEEDED 2*/
54
55#define QDIO_NO_USE_COUNT_TIMEOUT (1*HZ) /* wait for 1 sec on each q before
56 exiting without having use_count
57 of the queue to 0 */
58
59#define QDIO_ESTABLISH_TIMEOUT (1*HZ)
60#define QDIO_CLEANUP_CLEAR_TIMEOUT (20*HZ)
61#define QDIO_CLEANUP_HALT_TIMEOUT (10*HZ)
62#define QDIO_FORCE_CHECK_TIMEOUT (10*HZ)
63#define QDIO_ACTIVATE_TIMEOUT (5) /* 5 ms */
64 18
65enum qdio_irq_states { 19enum qdio_irq_states {
66 QDIO_IRQ_STATE_INACTIVE, 20 QDIO_IRQ_STATE_INACTIVE,
@@ -72,565 +26,352 @@ enum qdio_irq_states {
72 NR_QDIO_IRQ_STATES, 26 NR_QDIO_IRQ_STATES,
73}; 27};
74 28
75/* used as intparm in do_IO: */ 29/* used as intparm in do_IO */
76#define QDIO_DOING_SENSEID 0 30#define QDIO_DOING_ESTABLISH 1
77#define QDIO_DOING_ESTABLISH 1 31#define QDIO_DOING_ACTIVATE 2
78#define QDIO_DOING_ACTIVATE 2 32#define QDIO_DOING_CLEANUP 3
79#define QDIO_DOING_CLEANUP 3 33
80 34#define SLSB_STATE_NOT_INIT 0x0
81/************************* DEBUG FACILITY STUFF *********************/ 35#define SLSB_STATE_EMPTY 0x1
82 36#define SLSB_STATE_PRIMED 0x2
83#define QDIO_DBF_HEX(ex,name,level,addr,len) \ 37#define SLSB_STATE_HALTED 0xe
84 do { \ 38#define SLSB_STATE_ERROR 0xf
85 if (ex) \ 39#define SLSB_TYPE_INPUT 0x0
86 debug_exception(qdio_dbf_##name,level,(void*)(addr),len); \ 40#define SLSB_TYPE_OUTPUT 0x20
87 else \ 41#define SLSB_OWNER_PROG 0x80
88 debug_event(qdio_dbf_##name,level,(void*)(addr),len); \ 42#define SLSB_OWNER_CU 0x40
89 } while (0) 43
90#define QDIO_DBF_TEXT(ex,name,level,text) \ 44#define SLSB_P_INPUT_NOT_INIT \
91 do { \ 45 (SLSB_OWNER_PROG | SLSB_TYPE_INPUT | SLSB_STATE_NOT_INIT) /* 0x80 */
92 if (ex) \ 46#define SLSB_P_INPUT_ACK \
93 debug_text_exception(qdio_dbf_##name,level,text); \ 47 (SLSB_OWNER_PROG | SLSB_TYPE_INPUT | SLSB_STATE_EMPTY) /* 0x81 */
94 else \ 48#define SLSB_CU_INPUT_EMPTY \
95 debug_text_event(qdio_dbf_##name,level,text); \ 49 (SLSB_OWNER_CU | SLSB_TYPE_INPUT | SLSB_STATE_EMPTY) /* 0x41 */
96 } while (0) 50#define SLSB_P_INPUT_PRIMED \
97 51 (SLSB_OWNER_PROG | SLSB_TYPE_INPUT | SLSB_STATE_PRIMED) /* 0x82 */
98 52#define SLSB_P_INPUT_HALTED \
99#define QDIO_DBF_HEX0(ex,name,addr,len) QDIO_DBF_HEX(ex,name,0,addr,len) 53 (SLSB_OWNER_PROG | SLSB_TYPE_INPUT | SLSB_STATE_HALTED) /* 0x8e */
100#define QDIO_DBF_HEX1(ex,name,addr,len) QDIO_DBF_HEX(ex,name,1,addr,len) 54#define SLSB_P_INPUT_ERROR \
101#define QDIO_DBF_HEX2(ex,name,addr,len) QDIO_DBF_HEX(ex,name,2,addr,len) 55 (SLSB_OWNER_PROG | SLSB_TYPE_INPUT | SLSB_STATE_ERROR) /* 0x8f */
102#ifdef CONFIG_QDIO_DEBUG 56#define SLSB_P_OUTPUT_NOT_INIT \
103#define QDIO_DBF_HEX3(ex,name,addr,len) QDIO_DBF_HEX(ex,name,3,addr,len) 57 (SLSB_OWNER_PROG | SLSB_TYPE_OUTPUT | SLSB_STATE_NOT_INIT) /* 0xa0 */
104#define QDIO_DBF_HEX4(ex,name,addr,len) QDIO_DBF_HEX(ex,name,4,addr,len) 58#define SLSB_P_OUTPUT_EMPTY \
105#define QDIO_DBF_HEX5(ex,name,addr,len) QDIO_DBF_HEX(ex,name,5,addr,len) 59 (SLSB_OWNER_PROG | SLSB_TYPE_OUTPUT | SLSB_STATE_EMPTY) /* 0xa1 */
106#define QDIO_DBF_HEX6(ex,name,addr,len) QDIO_DBF_HEX(ex,name,6,addr,len) 60#define SLSB_CU_OUTPUT_PRIMED \
107#else /* CONFIG_QDIO_DEBUG */ 61 (SLSB_OWNER_CU | SLSB_TYPE_OUTPUT | SLSB_STATE_PRIMED) /* 0x62 */
108#define QDIO_DBF_HEX3(ex,name,addr,len) do {} while (0) 62#define SLSB_P_OUTPUT_HALTED \
109#define QDIO_DBF_HEX4(ex,name,addr,len) do {} while (0) 63 (SLSB_OWNER_PROG | SLSB_TYPE_OUTPUT | SLSB_STATE_HALTED) /* 0xae */
110#define QDIO_DBF_HEX5(ex,name,addr,len) do {} while (0) 64#define SLSB_P_OUTPUT_ERROR \
111#define QDIO_DBF_HEX6(ex,name,addr,len) do {} while (0) 65 (SLSB_OWNER_PROG | SLSB_TYPE_OUTPUT | SLSB_STATE_ERROR) /* 0xaf */
112#endif /* CONFIG_QDIO_DEBUG */ 66
113 67#define SLSB_ERROR_DURING_LOOKUP 0xff
114#define QDIO_DBF_TEXT0(ex,name,text) QDIO_DBF_TEXT(ex,name,0,text) 68
115#define QDIO_DBF_TEXT1(ex,name,text) QDIO_DBF_TEXT(ex,name,1,text) 69/* additional CIWs returned by extended Sense-ID */
116#define QDIO_DBF_TEXT2(ex,name,text) QDIO_DBF_TEXT(ex,name,2,text) 70#define CIW_TYPE_EQUEUE 0x3 /* establish QDIO queues */
117#ifdef CONFIG_QDIO_DEBUG 71#define CIW_TYPE_AQUEUE 0x4 /* activate QDIO queues */
118#define QDIO_DBF_TEXT3(ex,name,text) QDIO_DBF_TEXT(ex,name,3,text)
119#define QDIO_DBF_TEXT4(ex,name,text) QDIO_DBF_TEXT(ex,name,4,text)
120#define QDIO_DBF_TEXT5(ex,name,text) QDIO_DBF_TEXT(ex,name,5,text)
121#define QDIO_DBF_TEXT6(ex,name,text) QDIO_DBF_TEXT(ex,name,6,text)
122#else /* CONFIG_QDIO_DEBUG */
123#define QDIO_DBF_TEXT3(ex,name,text) do {} while (0)
124#define QDIO_DBF_TEXT4(ex,name,text) do {} while (0)
125#define QDIO_DBF_TEXT5(ex,name,text) do {} while (0)
126#define QDIO_DBF_TEXT6(ex,name,text) do {} while (0)
127#endif /* CONFIG_QDIO_DEBUG */
128
129#define QDIO_DBF_SETUP_NAME "qdio_setup"
130#define QDIO_DBF_SETUP_LEN 8
131#define QDIO_DBF_SETUP_PAGES 4
132#define QDIO_DBF_SETUP_NR_AREAS 1
133#ifdef CONFIG_QDIO_DEBUG
134#define QDIO_DBF_SETUP_LEVEL 6
135#else /* CONFIG_QDIO_DEBUG */
136#define QDIO_DBF_SETUP_LEVEL 2
137#endif /* CONFIG_QDIO_DEBUG */
138
139#define QDIO_DBF_SBAL_NAME "qdio_labs" /* sbal */
140#define QDIO_DBF_SBAL_LEN 256
141#define QDIO_DBF_SBAL_PAGES 4
142#define QDIO_DBF_SBAL_NR_AREAS 2
143#ifdef CONFIG_QDIO_DEBUG
144#define QDIO_DBF_SBAL_LEVEL 6
145#else /* CONFIG_QDIO_DEBUG */
146#define QDIO_DBF_SBAL_LEVEL 2
147#endif /* CONFIG_QDIO_DEBUG */
148
149#define QDIO_DBF_TRACE_NAME "qdio_trace"
150#define QDIO_DBF_TRACE_LEN 8
151#define QDIO_DBF_TRACE_NR_AREAS 2
152#ifdef CONFIG_QDIO_DEBUG
153#define QDIO_DBF_TRACE_PAGES 16
154#define QDIO_DBF_TRACE_LEVEL 4 /* -------- could be even more verbose here */
155#else /* CONFIG_QDIO_DEBUG */
156#define QDIO_DBF_TRACE_PAGES 4
157#define QDIO_DBF_TRACE_LEVEL 2
158#endif /* CONFIG_QDIO_DEBUG */
159
160#define QDIO_DBF_SENSE_NAME "qdio_sense"
161#define QDIO_DBF_SENSE_LEN 64
162#define QDIO_DBF_SENSE_PAGES 2
163#define QDIO_DBF_SENSE_NR_AREAS 1
164#ifdef CONFIG_QDIO_DEBUG
165#define QDIO_DBF_SENSE_LEVEL 6
166#else /* CONFIG_QDIO_DEBUG */
167#define QDIO_DBF_SENSE_LEVEL 2
168#endif /* CONFIG_QDIO_DEBUG */
169
170#ifdef CONFIG_QDIO_DEBUG
171#define QDIO_TRACE_QTYPE QDIO_ZFCP_QFMT
172
173#define QDIO_DBF_SLSB_OUT_NAME "qdio_slsb_out"
174#define QDIO_DBF_SLSB_OUT_LEN QDIO_MAX_BUFFERS_PER_Q
175#define QDIO_DBF_SLSB_OUT_PAGES 256
176#define QDIO_DBF_SLSB_OUT_NR_AREAS 1
177#define QDIO_DBF_SLSB_OUT_LEVEL 6
178
179#define QDIO_DBF_SLSB_IN_NAME "qdio_slsb_in"
180#define QDIO_DBF_SLSB_IN_LEN QDIO_MAX_BUFFERS_PER_Q
181#define QDIO_DBF_SLSB_IN_PAGES 256
182#define QDIO_DBF_SLSB_IN_NR_AREAS 1
183#define QDIO_DBF_SLSB_IN_LEVEL 6
184#endif /* CONFIG_QDIO_DEBUG */
185
186#define QDIO_PRINTK_HEADER QDIO_NAME ": "
187
188#if QDIO_VERBOSE_LEVEL>8
189#define QDIO_PRINT_STUPID(x...) printk( KERN_DEBUG QDIO_PRINTK_HEADER x)
190#else
191#define QDIO_PRINT_STUPID(x...) do { } while (0)
192#endif
193 72
194#if QDIO_VERBOSE_LEVEL>7 73/* flags for st qdio sch data */
195#define QDIO_PRINT_ALL(x...) printk( QDIO_PRINTK_HEADER x) 74#define CHSC_FLAG_QDIO_CAPABILITY 0x80
196#else 75#define CHSC_FLAG_VALIDITY 0x40
197#define QDIO_PRINT_ALL(x...) do { } while (0) 76
198#endif 77/* qdio adapter-characteristics-1 flag */
199 78#define AC1_SIGA_INPUT_NEEDED 0x40 /* process input queues */
200#if QDIO_VERBOSE_LEVEL>6 79#define AC1_SIGA_OUTPUT_NEEDED 0x20 /* process output queues */
201#define QDIO_PRINT_INFO(x...) printk( QDIO_PRINTK_HEADER x) 80#define AC1_SIGA_SYNC_NEEDED 0x10 /* ask hypervisor to sync */
202#else 81#define AC1_AUTOMATIC_SYNC_ON_THININT 0x08 /* set by hypervisor */
203#define QDIO_PRINT_INFO(x...) do { } while (0) 82#define AC1_AUTOMATIC_SYNC_ON_OUT_PCI 0x04 /* set by hypervisor */
204#endif 83#define AC1_SC_QEBSM_AVAILABLE 0x02 /* available for subchannel */
205 84#define AC1_SC_QEBSM_ENABLED 0x01 /* enabled for subchannel */
206#if QDIO_VERBOSE_LEVEL>5
207#define QDIO_PRINT_WARN(x...) printk( QDIO_PRINTK_HEADER x)
208#else
209#define QDIO_PRINT_WARN(x...) do { } while (0)
210#endif
211
212#if QDIO_VERBOSE_LEVEL>4
213#define QDIO_PRINT_ERR(x...) printk( QDIO_PRINTK_HEADER x)
214#else
215#define QDIO_PRINT_ERR(x...) do { } while (0)
216#endif
217
218#if QDIO_VERBOSE_LEVEL>3
219#define QDIO_PRINT_CRIT(x...) printk( QDIO_PRINTK_HEADER x)
220#else
221#define QDIO_PRINT_CRIT(x...) do { } while (0)
222#endif
223
224#if QDIO_VERBOSE_LEVEL>2
225#define QDIO_PRINT_ALERT(x...) printk( QDIO_PRINTK_HEADER x)
226#else
227#define QDIO_PRINT_ALERT(x...) do { } while (0)
228#endif
229 85
230#if QDIO_VERBOSE_LEVEL>1 86#ifdef CONFIG_64BIT
231#define QDIO_PRINT_EMERG(x...) printk( QDIO_PRINTK_HEADER x) 87static inline int do_sqbs(u64 token, unsigned char state, int queue,
232#else 88 int *start, int *count)
233#define QDIO_PRINT_EMERG(x...) do { } while (0) 89{
234#endif 90 register unsigned long _ccq asm ("0") = *count;
235 91 register unsigned long _token asm ("1") = token;
236#define QDIO_HEXDUMP16(importance,header,ptr) \ 92 unsigned long _queuestart = ((unsigned long)queue << 32) | *start;
237QDIO_PRINT_##importance(header "%02x %02x %02x %02x " \
238 "%02x %02x %02x %02x %02x %02x %02x %02x " \
239 "%02x %02x %02x %02x\n",*(((char*)ptr)), \
240 *(((char*)ptr)+1),*(((char*)ptr)+2), \
241 *(((char*)ptr)+3),*(((char*)ptr)+4), \
242 *(((char*)ptr)+5),*(((char*)ptr)+6), \
243 *(((char*)ptr)+7),*(((char*)ptr)+8), \
244 *(((char*)ptr)+9),*(((char*)ptr)+10), \
245 *(((char*)ptr)+11),*(((char*)ptr)+12), \
246 *(((char*)ptr)+13),*(((char*)ptr)+14), \
247 *(((char*)ptr)+15)); \
248QDIO_PRINT_##importance(header "%02x %02x %02x %02x %02x %02x %02x %02x " \
249 "%02x %02x %02x %02x %02x %02x %02x %02x\n", \
250 *(((char*)ptr)+16),*(((char*)ptr)+17), \
251 *(((char*)ptr)+18),*(((char*)ptr)+19), \
252 *(((char*)ptr)+20),*(((char*)ptr)+21), \
253 *(((char*)ptr)+22),*(((char*)ptr)+23), \
254 *(((char*)ptr)+24),*(((char*)ptr)+25), \
255 *(((char*)ptr)+26),*(((char*)ptr)+27), \
256 *(((char*)ptr)+28),*(((char*)ptr)+29), \
257 *(((char*)ptr)+30),*(((char*)ptr)+31));
258
259/****************** END OF DEBUG FACILITY STUFF *********************/
260 93
261/* 94 asm volatile(
262 * Some instructions as assembly 95 " .insn rsy,0xeb000000008A,%1,0,0(%2)"
263 */ 96 : "+d" (_ccq), "+d" (_queuestart)
97 : "d" ((unsigned long)state), "d" (_token)
98 : "memory", "cc");
99 *count = _ccq & 0xff;
100 *start = _queuestart & 0xff;
264 101
265static inline int 102 return (_ccq >> 32) & 0xff;
266do_sqbs(unsigned long sch, unsigned char state, int queue,
267 unsigned int *start, unsigned int *count)
268{
269#ifdef CONFIG_64BIT
270 register unsigned long _ccq asm ("0") = *count;
271 register unsigned long _sch asm ("1") = sch;
272 unsigned long _queuestart = ((unsigned long)queue << 32) | *start;
273
274 asm volatile(
275 " .insn rsy,0xeb000000008A,%1,0,0(%2)"
276 : "+d" (_ccq), "+d" (_queuestart)
277 : "d" ((unsigned long)state), "d" (_sch)
278 : "memory", "cc");
279 *count = _ccq & 0xff;
280 *start = _queuestart & 0xff;
281
282 return (_ccq >> 32) & 0xff;
283#else
284 return 0;
285#endif
286} 103}
287 104
288static inline int 105static inline int do_eqbs(u64 token, unsigned char *state, int queue,
289do_eqbs(unsigned long sch, unsigned char *state, int queue, 106 int *start, int *count)
290 unsigned int *start, unsigned int *count)
291{ 107{
292#ifdef CONFIG_64BIT
293 register unsigned long _ccq asm ("0") = *count; 108 register unsigned long _ccq asm ("0") = *count;
294 register unsigned long _sch asm ("1") = sch; 109 register unsigned long _token asm ("1") = token;
295 unsigned long _queuestart = ((unsigned long)queue << 32) | *start; 110 unsigned long _queuestart = ((unsigned long)queue << 32) | *start;
296 unsigned long _state = 0; 111 unsigned long _state = 0;
297 112
298 asm volatile( 113 asm volatile(
299 " .insn rrf,0xB99c0000,%1,%2,0,0" 114 " .insn rrf,0xB99c0000,%1,%2,0,0"
300 : "+d" (_ccq), "+d" (_queuestart), "+d" (_state) 115 : "+d" (_ccq), "+d" (_queuestart), "+d" (_state)
301 : "d" (_sch) 116 : "d" (_token)
302 : "memory", "cc" ); 117 : "memory", "cc");
303 *count = _ccq & 0xff; 118 *count = _ccq & 0xff;
304 *start = _queuestart & 0xff; 119 *start = _queuestart & 0xff;
305 *state = _state & 0xff; 120 *state = _state & 0xff;
306 121
307 return (_ccq >> 32) & 0xff; 122 return (_ccq >> 32) & 0xff;
308#else
309 return 0;
310#endif
311}
312
313
314static inline int
315do_siga_sync(struct subchannel_id schid, unsigned int mask1, unsigned int mask2)
316{
317 register unsigned long reg0 asm ("0") = 2;
318 register struct subchannel_id reg1 asm ("1") = schid;
319 register unsigned long reg2 asm ("2") = mask1;
320 register unsigned long reg3 asm ("3") = mask2;
321 int cc;
322
323 asm volatile(
324 " siga 0\n"
325 " ipm %0\n"
326 " srl %0,28\n"
327 : "=d" (cc)
328 : "d" (reg0), "d" (reg1), "d" (reg2), "d" (reg3) : "cc");
329 return cc;
330}
331
332static inline int
333do_siga_input(struct subchannel_id schid, unsigned int mask)
334{
335 register unsigned long reg0 asm ("0") = 1;
336 register struct subchannel_id reg1 asm ("1") = schid;
337 register unsigned long reg2 asm ("2") = mask;
338 int cc;
339
340 asm volatile(
341 " siga 0\n"
342 " ipm %0\n"
343 " srl %0,28\n"
344 : "=d" (cc)
345 : "d" (reg0), "d" (reg1), "d" (reg2) : "cc", "memory");
346 return cc;
347}
348
349static inline int
350do_siga_output(unsigned long schid, unsigned long mask, __u32 *bb,
351 unsigned int fc)
352{
353 register unsigned long __fc asm("0") = fc;
354 register unsigned long __schid asm("1") = schid;
355 register unsigned long __mask asm("2") = mask;
356 int cc;
357
358 asm volatile(
359 " siga 0\n"
360 "0: ipm %0\n"
361 " srl %0,28\n"
362 "1:\n"
363 EX_TABLE(0b,1b)
364 : "=d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
365 : "0" (QDIO_SIGA_ERROR_ACCESS_EXCEPTION)
366 : "cc", "memory");
367 (*bb) = ((unsigned int) __fc) >> 31;
368 return cc;
369}
370
371static inline unsigned long
372do_clear_global_summary(void)
373{
374 register unsigned long __fn asm("1") = 3;
375 register unsigned long __tmp asm("2");
376 register unsigned long __time asm("3");
377
378 asm volatile(
379 " .insn rre,0xb2650000,2,0"
380 : "+d" (__fn), "=d" (__tmp), "=d" (__time));
381 return __time;
382} 123}
383 124#else
384/* 125static inline int do_sqbs(u64 token, unsigned char state, int queue,
385 * QDIO device commands returned by extended Sense-ID 126 int *start, int *count) { return 0; }
386 */ 127static inline int do_eqbs(u64 token, unsigned char *state, int queue,
387#define DEFAULT_ESTABLISH_QS_CMD 0x1b 128 int *start, int *count) { return 0; }
388#define DEFAULT_ESTABLISH_QS_COUNT 0x1000 129#endif /* CONFIG_64BIT */
389#define DEFAULT_ACTIVATE_QS_CMD 0x1f
390#define DEFAULT_ACTIVATE_QS_COUNT 0
391
392/*
393 * additional CIWs returned by extended Sense-ID
394 */
395#define CIW_TYPE_EQUEUE 0x3 /* establish QDIO queues */
396#define CIW_TYPE_AQUEUE 0x4 /* activate QDIO queues */
397 130
398#define QDIO_CHSC_RESPONSE_CODE_OK 1 131struct qdio_irq;
399/* flags for st qdio sch data */
400#define CHSC_FLAG_QDIO_CAPABILITY 0x80
401#define CHSC_FLAG_VALIDITY 0x40
402 132
403#define CHSC_FLAG_SIGA_INPUT_NECESSARY 0x40 133struct siga_flag {
404#define CHSC_FLAG_SIGA_OUTPUT_NECESSARY 0x20 134 u8 input:1;
405#define CHSC_FLAG_SIGA_SYNC_NECESSARY 0x10 135 u8 output:1;
406#define CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS 0x08 136 u8 sync:1;
407#define CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS 0x04 137 u8 no_sync_ti:1;
138 u8 no_sync_out_ti:1;
139 u8 no_sync_out_pci:1;
140 u8:2;
141} __attribute__ ((packed));
408 142
409struct qdio_chsc_ssqd { 143struct chsc_ssqd_area {
410 struct chsc_header request; 144 struct chsc_header request;
411 u16 reserved1:10; 145 u16:10;
412 u16 ssid:2; 146 u8 ssid:2;
413 u16 fmt:4; 147 u8 fmt:4;
414 u16 first_sch; 148 u16 first_sch;
415 u16 reserved2; 149 u16:16;
416 u16 last_sch; 150 u16 last_sch;
417 u32 reserved3; 151 u32:32;
418 struct chsc_header response; 152 struct chsc_header response;
419 u32 reserved4; 153 u32:32;
420 u8 flags; 154 struct qdio_ssqd_desc qdio_ssqd;
421 u8 reserved5; 155} __attribute__ ((packed));
422 u16 sch;
423 u8 qfmt;
424 u8 parm;
425 u8 qdioac1;
426 u8 sch_class;
427 u8 pct;
428 u8 icnt;
429 u8 reserved7;
430 u8 ocnt;
431 u8 reserved8;
432 u8 mbccnt;
433 u16 qdioac2;
434 u64 sch_token;
435};
436 156
437struct qdio_perf_stats { 157struct scssc_area {
438#ifdef CONFIG_64BIT 158 struct chsc_header request;
439 atomic64_t tl_runs; 159 u16 operation_code;
440 atomic64_t outbound_tl_runs; 160 u16:16;
441 atomic64_t outbound_tl_runs_resched; 161 u32:32;
442 atomic64_t inbound_tl_runs; 162 u32:32;
443 atomic64_t inbound_tl_runs_resched; 163 u64 summary_indicator_addr;
444 atomic64_t inbound_thin_tl_runs; 164 u64 subchannel_indicator_addr;
445 atomic64_t inbound_thin_tl_runs_resched; 165 u32 ks:4;
446 166 u32 kc:4;
447 atomic64_t siga_outs; 167 u32:21;
448 atomic64_t siga_ins; 168 u32 isc:3;
449 atomic64_t siga_syncs; 169 u32 word_with_d_bit;
450 atomic64_t pcis; 170 u32:32;
451 atomic64_t thinints; 171 struct subchannel_id schid;
452 atomic64_t fast_reqs; 172 u32 reserved[1004];
453 173 struct chsc_header response;
454 atomic64_t outbound_cnt; 174 u32:32;
455 atomic64_t inbound_cnt; 175} __attribute__ ((packed));
456#else /* CONFIG_64BIT */ 176
457 atomic_t tl_runs; 177struct qdio_input_q {
458 atomic_t outbound_tl_runs; 178 /* input buffer acknowledgement flag */
459 atomic_t outbound_tl_runs_resched; 179 int polling;
460 atomic_t inbound_tl_runs; 180
461 atomic_t inbound_tl_runs_resched; 181 /* last time of noticing incoming data */
462 atomic_t inbound_thin_tl_runs; 182 u64 timestamp;
463 atomic_t inbound_thin_tl_runs_resched; 183
464 184 /* lock for clearing the acknowledgement */
465 atomic_t siga_outs; 185 spinlock_t lock;
466 atomic_t siga_ins;
467 atomic_t siga_syncs;
468 atomic_t pcis;
469 atomic_t thinints;
470 atomic_t fast_reqs;
471
472 atomic_t outbound_cnt;
473 atomic_t inbound_cnt;
474#endif /* CONFIG_64BIT */
475}; 186};
476 187
477/* unlikely as the later the better */ 188struct qdio_output_q {
478#define SYNC_MEMORY if (unlikely(q->siga_sync)) qdio_siga_sync_q(q) 189 /* failed siga-w attempts*/
479#define SYNC_MEMORY_ALL if (unlikely(q->siga_sync)) \ 190 atomic_t busy_siga_counter;
480 qdio_siga_sync(q,~0U,~0U)
481#define SYNC_MEMORY_ALL_OUTB if (unlikely(q->siga_sync)) \
482 qdio_siga_sync(q,~0U,0)
483 191
484#define NOW qdio_get_micros() 192 /* start time of busy condition */
485#define SAVE_TIMESTAMP(q) q->timing.last_transfer_time=NOW 193 u64 timestamp;
486#define GET_SAVED_TIMESTAMP(q) (q->timing.last_transfer_time)
487#define SAVE_FRONTIER(q,val) q->last_move_ftc=val
488#define GET_SAVED_FRONTIER(q) (q->last_move_ftc)
489 194
490#define MY_MODULE_STRING(x) #x 195 /* PCIs are enabled for the queue */
196 int pci_out_enabled;
491 197
492#ifdef CONFIG_64BIT 198 /* timer to check for more outbound work */
493#define QDIO_GET_ADDR(x) ((__u32)(unsigned long)x) 199 struct timer_list timer;
494#else /* CONFIG_64BIT */ 200};
495#define QDIO_GET_ADDR(x) ((__u32)(long)x)
496#endif /* CONFIG_64BIT */
497 201
498struct qdio_q { 202struct qdio_q {
499 volatile struct slsb slsb; 203 struct slsb slsb;
204 union {
205 struct qdio_input_q in;
206 struct qdio_output_q out;
207 } u;
500 208
501 char unused[QDIO_MAX_BUFFERS_PER_Q]; 209 /* queue number */
210 int nr;
502 211
503 __u32 * dev_st_chg_ind; 212 /* bitmask of queue number */
213 int mask;
504 214
215 /* input or output queue */
505 int is_input_q; 216 int is_input_q;
506 struct subchannel_id schid;
507 struct ccw_device *cdev;
508
509 unsigned int is_iqdio_q;
510 unsigned int is_thinint_q;
511 217
512 /* bit 0 means queue 0, bit 1 means queue 1, ... */ 218 /* list of thinint input queues */
513 unsigned int mask; 219 struct list_head entry;
514 unsigned int q_no;
515 220
221 /* upper-layer program handler */
516 qdio_handler_t (*handler); 222 qdio_handler_t (*handler);
517 223
518 /* points to the next buffer to be checked for having 224 /*
519 * been processed by the card (outbound) 225 * inbound: next buffer the program should check for
520 * or to the next buffer the program should check for (inbound) */ 226 * outbound: next buffer to check for having been processed
521 volatile int first_to_check; 227 * by the card
522 /* and the last time it was: */ 228 */
523 volatile int last_move_ftc; 229 int first_to_check;
524 230
525 atomic_t number_of_buffers_used; 231 /* first_to_check of the last time */
526 atomic_t polling; 232 int last_move_ftc;
527 233
528 unsigned int siga_in; 234 /* beginning position for calling the program */
529 unsigned int siga_out; 235 int first_to_kick;
530 unsigned int siga_sync;
531 unsigned int siga_sync_done_on_thinints;
532 unsigned int siga_sync_done_on_outb_tis;
533 unsigned int hydra_gives_outbound_pcis;
534 236
535 /* used to save beginning position when calling dd_handlers */ 237 /* number of buffers in use by the adapter */
536 int first_element_to_kick; 238 atomic_t nr_buf_used;
537 239
538 atomic_t use_count; 240 struct qdio_irq *irq_ptr;
539 atomic_t is_in_shutdown;
540
541 void *irq_ptr;
542
543 struct timer_list timer;
544#ifdef QDIO_USE_TIMERS_FOR_POLLING
545 atomic_t timer_already_set;
546 spinlock_t timer_lock;
547#else /* QDIO_USE_TIMERS_FOR_POLLING */
548 struct tasklet_struct tasklet; 241 struct tasklet_struct tasklet;
549#endif /* QDIO_USE_TIMERS_FOR_POLLING */
550 242
551 243 /* error condition during a data transfer */
552 enum qdio_irq_states state;
553
554 /* used to store the error condition during a data transfer */
555 unsigned int qdio_error; 244 unsigned int qdio_error;
556 unsigned int siga_error;
557 unsigned int error_status_flags;
558
559 /* list of interesting queues */
560 volatile struct qdio_q *list_next;
561 volatile struct qdio_q *list_prev;
562 245
563 struct sl *sl; 246 struct sl *sl;
564 volatile struct sbal *sbal[QDIO_MAX_BUFFERS_PER_Q]; 247 struct qdio_buffer *sbal[QDIO_MAX_BUFFERS_PER_Q];
565 248
566 struct qdio_buffer *qdio_buffers[QDIO_MAX_BUFFERS_PER_Q]; 249 /*
567 250 * Warning: Leave this member at the end so it won't be cleared in
568 unsigned long int_parm; 251 * qdio_fill_qs. A page is allocated under this pointer and used for
569 252 * slib and sl. slib is 2048 bytes big and sl points to offset
570 /*struct { 253 * PAGE_SIZE / 2.
571 int in_bh_check_limit; 254 */
572 int threshold; 255 struct slib *slib;
573 } threshold_classes[QDIO_STATS_CLASSES];*/
574
575 struct {
576 /* inbound: the time to stop polling
577 outbound: the time to kick peer */
578 int threshold; /* the real value */
579
580 /* outbound: last time of do_QDIO
581 inbound: last time of noticing incoming data */
582 /*__u64 last_transfer_times[QDIO_STATS_NUMBER];
583 int last_transfer_index; */
584
585 __u64 last_transfer_time;
586 __u64 busy_start;
587 } timing;
588 atomic_t busy_siga_counter;
589 unsigned int queue_type;
590 unsigned int is_pci_out;
591
592 /* leave this member at the end. won't be cleared in qdio_fill_qs */
593 struct slib *slib; /* a page is allocated under this pointer,
594 sl points into this page, offset PAGE_SIZE/2
595 (after slib) */
596} __attribute__ ((aligned(256))); 256} __attribute__ ((aligned(256)));
597 257
598struct qdio_irq { 258struct qdio_irq {
599 __u32 * volatile dev_st_chg_ind; 259 struct qib qib;
260 u32 *dsci; /* address of device state change indicator */
261 struct ccw_device *cdev;
600 262
601 unsigned long int_parm; 263 unsigned long int_parm;
602 struct subchannel_id schid; 264 struct subchannel_id schid;
603 265 unsigned long sch_token; /* QEBSM facility */
604 unsigned int is_iqdio_irq;
605 unsigned int is_thinint_irq;
606 unsigned int hydra_gives_outbound_pcis;
607 unsigned int sync_done_on_outb_pcis;
608
609 /* QEBSM facility */
610 unsigned int is_qebsm;
611 unsigned long sch_token;
612 266
613 enum qdio_irq_states state; 267 enum qdio_irq_states state;
614 268
615 unsigned int no_input_qs; 269 struct siga_flag siga_flag; /* siga sync information from qdioac */
616 unsigned int no_output_qs;
617 270
618 unsigned char qdioac; 271 int nr_input_qs;
272 int nr_output_qs;
619 273
620 struct ccw1 ccw; 274 struct ccw1 ccw;
621
622 struct ciw equeue; 275 struct ciw equeue;
623 struct ciw aqueue; 276 struct ciw aqueue;
624 277
625 struct qib qib; 278 struct qdio_ssqd_desc ssqd_desc;
626 279
627 void (*original_int_handler) (struct ccw_device *, 280 void (*orig_handler) (struct ccw_device *, unsigned long, struct irb *);
628 unsigned long, struct irb *);
629 281
630 /* leave these four members together at the end. won't be cleared in qdio_fill_irq */ 282 /*
283 * Warning: Leave these members together at the end so they won't be
284 * cleared in qdio_setup_irq.
285 */
631 struct qdr *qdr; 286 struct qdr *qdr;
287 unsigned long chsc_page;
288
632 struct qdio_q *input_qs[QDIO_MAX_QUEUES_PER_IRQ]; 289 struct qdio_q *input_qs[QDIO_MAX_QUEUES_PER_IRQ];
633 struct qdio_q *output_qs[QDIO_MAX_QUEUES_PER_IRQ]; 290 struct qdio_q *output_qs[QDIO_MAX_QUEUES_PER_IRQ];
634 struct semaphore setting_up_sema; 291
292 struct mutex setup_mutex;
635}; 293};
636#endif 294
295/* helper functions */
296#define queue_type(q) q->irq_ptr->qib.qfmt
297
298#define is_thinint_irq(irq) \
299 (irq->qib.qfmt == QDIO_IQDIO_QFMT || \
300 css_general_characteristics.aif_osa)
301
302/* the highest iqdio queue is used for multicast */
303static inline int multicast_outbound(struct qdio_q *q)
304{
305 return (q->irq_ptr->nr_output_qs > 1) &&
306 (q->nr == q->irq_ptr->nr_output_qs - 1);
307}
308
309static inline unsigned long long get_usecs(void)
310{
311 return monotonic_clock() >> 12;
312}
313
314#define pci_out_supported(q) \
315 (q->irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED)
316#define is_qebsm(q) (q->irq_ptr->sch_token != 0)
317
318#define need_siga_sync_thinint(q) (!q->irq_ptr->siga_flag.no_sync_ti)
319#define need_siga_sync_out_thinint(q) (!q->irq_ptr->siga_flag.no_sync_out_ti)
320#define need_siga_in(q) (q->irq_ptr->siga_flag.input)
321#define need_siga_out(q) (q->irq_ptr->siga_flag.output)
322#define need_siga_sync(q) (q->irq_ptr->siga_flag.sync)
323#define siga_syncs_out_pci(q) (q->irq_ptr->siga_flag.no_sync_out_pci)
324
325#define for_each_input_queue(irq_ptr, q, i) \
326 for (i = 0, q = irq_ptr->input_qs[0]; \
327 i < irq_ptr->nr_input_qs; \
328 q = irq_ptr->input_qs[++i])
329#define for_each_output_queue(irq_ptr, q, i) \
330 for (i = 0, q = irq_ptr->output_qs[0]; \
331 i < irq_ptr->nr_output_qs; \
332 q = irq_ptr->output_qs[++i])
333
334#define prev_buf(bufnr) \
335 ((bufnr + QDIO_MAX_BUFFERS_MASK) & QDIO_MAX_BUFFERS_MASK)
336#define next_buf(bufnr) \
337 ((bufnr + 1) & QDIO_MAX_BUFFERS_MASK)
338#define add_buf(bufnr, inc) \
339 ((bufnr + inc) & QDIO_MAX_BUFFERS_MASK)
340
341/* prototypes for thin interrupt */
342void qdio_sync_after_thinint(struct qdio_q *q);
343int get_buf_state(struct qdio_q *q, unsigned int bufnr, unsigned char *state);
344void qdio_check_outbound_after_thinint(struct qdio_q *q);
345int qdio_inbound_q_moved(struct qdio_q *q);
346void qdio_kick_inbound_handler(struct qdio_q *q);
347void qdio_stop_polling(struct qdio_q *q);
348int qdio_siga_sync_q(struct qdio_q *q);
349
350void qdio_setup_thinint(struct qdio_irq *irq_ptr);
351int qdio_establish_thinint(struct qdio_irq *irq_ptr);
352void qdio_shutdown_thinint(struct qdio_irq *irq_ptr);
353void tiqdio_add_input_queues(struct qdio_irq *irq_ptr);
354void tiqdio_remove_input_queues(struct qdio_irq *irq_ptr);
355void tiqdio_inbound_processing(unsigned long q);
356int tiqdio_allocate_memory(void);
357void tiqdio_free_memory(void);
358int tiqdio_register_thinints(void);
359void tiqdio_unregister_thinints(void);
360
361/* prototypes for setup */
362void qdio_inbound_processing(unsigned long data);
363void qdio_outbound_processing(unsigned long data);
364void qdio_outbound_timer(unsigned long data);
365void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
366 struct irb *irb);
367int qdio_allocate_qs(struct qdio_irq *irq_ptr, int nr_input_qs,
368 int nr_output_qs);
369void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr);
370int qdio_setup_irq(struct qdio_initialize *init_data);
371void qdio_print_subchannel_info(struct qdio_irq *irq_ptr,
372 struct ccw_device *cdev);
373void qdio_release_memory(struct qdio_irq *irq_ptr);
374int qdio_setup_init(void);
375void qdio_setup_exit(void);
376
377#endif /* _CIO_QDIO_H */
diff --git a/drivers/s390/cio/qdio_debug.c b/drivers/s390/cio/qdio_debug.c
new file mode 100644
index 000000000000..337aa3087a78
--- /dev/null
+++ b/drivers/s390/cio/qdio_debug.c
@@ -0,0 +1,240 @@
1/*
2 * drivers/s390/cio/qdio_debug.c
3 *
4 * Copyright IBM Corp. 2008
5 *
6 * Author: Jan Glauber (jang@linux.vnet.ibm.com)
7 */
8#include <linux/proc_fs.h>
9#include <linux/seq_file.h>
10#include <linux/debugfs.h>
11#include <asm/qdio.h>
12#include <asm/debug.h>
13#include "qdio_debug.h"
14#include "qdio.h"
15
16debug_info_t *qdio_dbf_setup;
17debug_info_t *qdio_dbf_trace;
18
19static struct dentry *debugfs_root;
20#define MAX_DEBUGFS_QUEUES 32
21static struct dentry *debugfs_queues[MAX_DEBUGFS_QUEUES] = { NULL };
22static DEFINE_MUTEX(debugfs_mutex);
23
24void qdio_allocate_do_dbf(struct qdio_initialize *init_data)
25{
26 char dbf_text[20];
27
28 sprintf(dbf_text, "qfmt:%x", init_data->q_format);
29 QDIO_DBF_TEXT0(0, setup, dbf_text);
30 QDIO_DBF_HEX0(0, setup, init_data->adapter_name, 8);
31 sprintf(dbf_text, "qpff%4x", init_data->qib_param_field_format);
32 QDIO_DBF_TEXT0(0, setup, dbf_text);
33 QDIO_DBF_HEX0(0, setup, &init_data->qib_param_field, sizeof(void *));
34 QDIO_DBF_HEX0(0, setup, &init_data->input_slib_elements, sizeof(void *));
35 QDIO_DBF_HEX0(0, setup, &init_data->output_slib_elements, sizeof(void *));
36 sprintf(dbf_text, "niq:%4x", init_data->no_input_qs);
37 QDIO_DBF_TEXT0(0, setup, dbf_text);
38 sprintf(dbf_text, "noq:%4x", init_data->no_output_qs);
39 QDIO_DBF_TEXT0(0, setup, dbf_text);
40 QDIO_DBF_HEX0(0, setup, &init_data->input_handler, sizeof(void *));
41 QDIO_DBF_HEX0(0, setup, &init_data->output_handler, sizeof(void *));
42 QDIO_DBF_HEX0(0, setup, &init_data->int_parm, sizeof(long));
43 QDIO_DBF_HEX0(0, setup, &init_data->flags, sizeof(long));
44 QDIO_DBF_HEX0(0, setup, &init_data->input_sbal_addr_array, sizeof(void *));
45 QDIO_DBF_HEX0(0, setup, &init_data->output_sbal_addr_array, sizeof(void *));
46}
47
48static void qdio_unregister_dbf_views(void)
49{
50 if (qdio_dbf_setup)
51 debug_unregister(qdio_dbf_setup);
52 if (qdio_dbf_trace)
53 debug_unregister(qdio_dbf_trace);
54}
55
56static int qdio_register_dbf_views(void)
57{
58 qdio_dbf_setup = debug_register("qdio_setup", QDIO_DBF_SETUP_PAGES,
59 QDIO_DBF_SETUP_NR_AREAS,
60 QDIO_DBF_SETUP_LEN);
61 if (!qdio_dbf_setup)
62 goto oom;
63 debug_register_view(qdio_dbf_setup, &debug_hex_ascii_view);
64 debug_set_level(qdio_dbf_setup, QDIO_DBF_SETUP_LEVEL);
65
66 qdio_dbf_trace = debug_register("qdio_trace", QDIO_DBF_TRACE_PAGES,
67 QDIO_DBF_TRACE_NR_AREAS,
68 QDIO_DBF_TRACE_LEN);
69 if (!qdio_dbf_trace)
70 goto oom;
71 debug_register_view(qdio_dbf_trace, &debug_hex_ascii_view);
72 debug_set_level(qdio_dbf_trace, QDIO_DBF_TRACE_LEVEL);
73 return 0;
74oom:
75 qdio_unregister_dbf_views();
76 return -ENOMEM;
77}
78
79static int qstat_show(struct seq_file *m, void *v)
80{
81 unsigned char state;
82 struct qdio_q *q = m->private;
83 int i;
84
85 if (!q)
86 return 0;
87
88 seq_printf(m, "device state indicator: %d\n", *q->irq_ptr->dsci);
89 seq_printf(m, "nr_used: %d\n", atomic_read(&q->nr_buf_used));
90 seq_printf(m, "ftc: %d\n", q->first_to_check);
91 seq_printf(m, "last_move_ftc: %d\n", q->last_move_ftc);
92 seq_printf(m, "polling: %d\n", q->u.in.polling);
93 seq_printf(m, "slsb buffer states:\n");
94
95 qdio_siga_sync_q(q);
96 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++) {
97 get_buf_state(q, i, &state);
98 switch (state) {
99 case SLSB_P_INPUT_NOT_INIT:
100 case SLSB_P_OUTPUT_NOT_INIT:
101 seq_printf(m, "N");
102 break;
103 case SLSB_P_INPUT_PRIMED:
104 case SLSB_CU_OUTPUT_PRIMED:
105 seq_printf(m, "+");
106 break;
107 case SLSB_P_INPUT_ACK:
108 seq_printf(m, "A");
109 break;
110 case SLSB_P_INPUT_ERROR:
111 case SLSB_P_OUTPUT_ERROR:
112 seq_printf(m, "x");
113 break;
114 case SLSB_CU_INPUT_EMPTY:
115 case SLSB_P_OUTPUT_EMPTY:
116 seq_printf(m, "-");
117 break;
118 case SLSB_P_INPUT_HALTED:
119 case SLSB_P_OUTPUT_HALTED:
120 seq_printf(m, ".");
121 break;
122 default:
123 seq_printf(m, "?");
124 }
125 if (i == 63)
126 seq_printf(m, "\n");
127 }
128 seq_printf(m, "\n");
129 return 0;
130}
131
132static ssize_t qstat_seq_write(struct file *file, const char __user *buf,
133 size_t count, loff_t *off)
134{
135 struct seq_file *seq = file->private_data;
136 struct qdio_q *q = seq->private;
137
138 if (!q)
139 return 0;
140
141 if (q->is_input_q)
142 xchg(q->irq_ptr->dsci, 1);
143 local_bh_disable();
144 tasklet_schedule(&q->tasklet);
145 local_bh_enable();
146 return count;
147}
148
149static int qstat_seq_open(struct inode *inode, struct file *filp)
150{
151 return single_open(filp, qstat_show,
152 filp->f_path.dentry->d_inode->i_private);
153}
154
155static void get_queue_name(struct qdio_q *q, struct ccw_device *cdev, char *name)
156{
157 memset(name, 0, sizeof(name));
158 sprintf(name, "%s", cdev->dev.bus_id);
159 if (q->is_input_q)
160 sprintf(name + strlen(name), "_input");
161 else
162 sprintf(name + strlen(name), "_output");
163 sprintf(name + strlen(name), "_%d", q->nr);
164}
165
166static void remove_debugfs_entry(struct qdio_q *q)
167{
168 int i;
169
170 for (i = 0; i < MAX_DEBUGFS_QUEUES; i++) {
171 if (!debugfs_queues[i])
172 continue;
173 if (debugfs_queues[i]->d_inode->i_private == q) {
174 debugfs_remove(debugfs_queues[i]);
175 debugfs_queues[i] = NULL;
176 }
177 }
178}
179
180static struct file_operations debugfs_fops = {
181 .owner = THIS_MODULE,
182 .open = qstat_seq_open,
183 .read = seq_read,
184 .write = qstat_seq_write,
185 .llseek = seq_lseek,
186 .release = single_release,
187};
188
189static void setup_debugfs_entry(struct qdio_q *q, struct ccw_device *cdev)
190{
191 int i = 0;
192 char name[40];
193
194 while (debugfs_queues[i] != NULL) {
195 i++;
196 if (i >= MAX_DEBUGFS_QUEUES)
197 return;
198 }
199 get_queue_name(q, cdev, name);
200 debugfs_queues[i] = debugfs_create_file(name, S_IFREG | S_IRUGO | S_IWUSR,
201 debugfs_root, q, &debugfs_fops);
202}
203
204void qdio_setup_debug_entries(struct qdio_irq *irq_ptr, struct ccw_device *cdev)
205{
206 struct qdio_q *q;
207 int i;
208
209 mutex_lock(&debugfs_mutex);
210 for_each_input_queue(irq_ptr, q, i)
211 setup_debugfs_entry(q, cdev);
212 for_each_output_queue(irq_ptr, q, i)
213 setup_debugfs_entry(q, cdev);
214 mutex_unlock(&debugfs_mutex);
215}
216
217void qdio_shutdown_debug_entries(struct qdio_irq *irq_ptr, struct ccw_device *cdev)
218{
219 struct qdio_q *q;
220 int i;
221
222 mutex_lock(&debugfs_mutex);
223 for_each_input_queue(irq_ptr, q, i)
224 remove_debugfs_entry(q);
225 for_each_output_queue(irq_ptr, q, i)
226 remove_debugfs_entry(q);
227 mutex_unlock(&debugfs_mutex);
228}
229
230int __init qdio_debug_init(void)
231{
232 debugfs_root = debugfs_create_dir("qdio_queues", NULL);
233 return qdio_register_dbf_views();
234}
235
236void qdio_debug_exit(void)
237{
238 debugfs_remove(debugfs_root);
239 qdio_unregister_dbf_views();
240}
diff --git a/drivers/s390/cio/qdio_debug.h b/drivers/s390/cio/qdio_debug.h
new file mode 100644
index 000000000000..8484b83698e1
--- /dev/null
+++ b/drivers/s390/cio/qdio_debug.h
@@ -0,0 +1,91 @@
1/*
2 * drivers/s390/cio/qdio_debug.h
3 *
4 * Copyright IBM Corp. 2008
5 *
6 * Author: Jan Glauber (jang@linux.vnet.ibm.com)
7 */
8#ifndef QDIO_DEBUG_H
9#define QDIO_DEBUG_H
10
11#include <asm/debug.h>
12#include <asm/qdio.h>
13#include "qdio.h"
14
15#define QDIO_DBF_HEX(ex, name, level, addr, len) \
16 do { \
17 if (ex) \
18 debug_exception(qdio_dbf_##name, level, (void *)(addr), len); \
19 else \
20 debug_event(qdio_dbf_##name, level, (void *)(addr), len); \
21 } while (0)
22#define QDIO_DBF_TEXT(ex, name, level, text) \
23 do { \
24 if (ex) \
25 debug_text_exception(qdio_dbf_##name, level, text); \
26 else \
27 debug_text_event(qdio_dbf_##name, level, text); \
28 } while (0)
29
30#define QDIO_DBF_HEX0(ex, name, addr, len) QDIO_DBF_HEX(ex, name, 0, addr, len)
31#define QDIO_DBF_HEX1(ex, name, addr, len) QDIO_DBF_HEX(ex, name, 1, addr, len)
32#define QDIO_DBF_HEX2(ex, name, addr, len) QDIO_DBF_HEX(ex, name, 2, addr, len)
33
34#ifdef CONFIG_QDIO_DEBUG
35#define QDIO_DBF_HEX3(ex, name, addr, len) QDIO_DBF_HEX(ex, name, 3, addr, len)
36#define QDIO_DBF_HEX4(ex, name, addr, len) QDIO_DBF_HEX(ex, name, 4, addr, len)
37#define QDIO_DBF_HEX5(ex, name, addr, len) QDIO_DBF_HEX(ex, name, 5, addr, len)
38#define QDIO_DBF_HEX6(ex, name, addr, len) QDIO_DBF_HEX(ex, name, 6, addr, len)
39#else
40#define QDIO_DBF_HEX3(ex, name, addr, len) do {} while (0)
41#define QDIO_DBF_HEX4(ex, name, addr, len) do {} while (0)
42#define QDIO_DBF_HEX5(ex, name, addr, len) do {} while (0)
43#define QDIO_DBF_HEX6(ex, name, addr, len) do {} while (0)
44#endif /* CONFIG_QDIO_DEBUG */
45
46#define QDIO_DBF_TEXT0(ex, name, text) QDIO_DBF_TEXT(ex, name, 0, text)
47#define QDIO_DBF_TEXT1(ex, name, text) QDIO_DBF_TEXT(ex, name, 1, text)
48#define QDIO_DBF_TEXT2(ex, name, text) QDIO_DBF_TEXT(ex, name, 2, text)
49
50#ifdef CONFIG_QDIO_DEBUG
51#define QDIO_DBF_TEXT3(ex, name, text) QDIO_DBF_TEXT(ex, name, 3, text)
52#define QDIO_DBF_TEXT4(ex, name, text) QDIO_DBF_TEXT(ex, name, 4, text)
53#define QDIO_DBF_TEXT5(ex, name, text) QDIO_DBF_TEXT(ex, name, 5, text)
54#define QDIO_DBF_TEXT6(ex, name, text) QDIO_DBF_TEXT(ex, name, 6, text)
55#else
56#define QDIO_DBF_TEXT3(ex, name, text) do {} while (0)
57#define QDIO_DBF_TEXT4(ex, name, text) do {} while (0)
58#define QDIO_DBF_TEXT5(ex, name, text) do {} while (0)
59#define QDIO_DBF_TEXT6(ex, name, text) do {} while (0)
60#endif /* CONFIG_QDIO_DEBUG */
61
62/* s390dbf views */
63#define QDIO_DBF_SETUP_LEN 8
64#define QDIO_DBF_SETUP_PAGES 4
65#define QDIO_DBF_SETUP_NR_AREAS 1
66
67#define QDIO_DBF_TRACE_LEN 8
68#define QDIO_DBF_TRACE_NR_AREAS 2
69
70#ifdef CONFIG_QDIO_DEBUG
71#define QDIO_DBF_TRACE_PAGES 16
72#define QDIO_DBF_SETUP_LEVEL 6
73#define QDIO_DBF_TRACE_LEVEL 4
74#else /* !CONFIG_QDIO_DEBUG */
75#define QDIO_DBF_TRACE_PAGES 4
76#define QDIO_DBF_SETUP_LEVEL 2
77#define QDIO_DBF_TRACE_LEVEL 2
78#endif /* CONFIG_QDIO_DEBUG */
79
80extern debug_info_t *qdio_dbf_setup;
81extern debug_info_t *qdio_dbf_trace;
82
83void qdio_allocate_do_dbf(struct qdio_initialize *init_data);
84void debug_print_bstat(struct qdio_q *q);
85void qdio_setup_debug_entries(struct qdio_irq *irq_ptr,
86 struct ccw_device *cdev);
87void qdio_shutdown_debug_entries(struct qdio_irq *irq_ptr,
88 struct ccw_device *cdev);
89int qdio_debug_init(void);
90void qdio_debug_exit(void);
91#endif
diff --git a/drivers/s390/cio/qdio_main.c b/drivers/s390/cio/qdio_main.c
new file mode 100644
index 000000000000..d10c73cc1688
--- /dev/null
+++ b/drivers/s390/cio/qdio_main.c
@@ -0,0 +1,1755 @@
1/*
2 * linux/drivers/s390/cio/qdio_main.c
3 *
4 * Linux for s390 qdio support, buffer handling, qdio API and module support.
5 *
6 * Copyright 2000,2008 IBM Corp.
7 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
8 * Jan Glauber <jang@linux.vnet.ibm.com>
9 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
10 */
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/timer.h>
15#include <linux/delay.h>
16#include <asm/atomic.h>
17#include <asm/debug.h>
18#include <asm/qdio.h>
19
20#include "cio.h"
21#include "css.h"
22#include "device.h"
23#include "qdio.h"
24#include "qdio_debug.h"
25#include "qdio_perf.h"
26
27MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
28 "Jan Glauber <jang@linux.vnet.ibm.com>");
29MODULE_DESCRIPTION("QDIO base support");
30MODULE_LICENSE("GPL");
31
32static inline int do_siga_sync(struct subchannel_id schid,
33 unsigned int out_mask, unsigned int in_mask)
34{
35 register unsigned long __fc asm ("0") = 2;
36 register struct subchannel_id __schid asm ("1") = schid;
37 register unsigned long out asm ("2") = out_mask;
38 register unsigned long in asm ("3") = in_mask;
39 int cc;
40
41 asm volatile(
42 " siga 0\n"
43 " ipm %0\n"
44 " srl %0,28\n"
45 : "=d" (cc)
46 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
47 return cc;
48}
49
50static inline int do_siga_input(struct subchannel_id schid, unsigned int mask)
51{
52 register unsigned long __fc asm ("0") = 1;
53 register struct subchannel_id __schid asm ("1") = schid;
54 register unsigned long __mask asm ("2") = mask;
55 int cc;
56
57 asm volatile(
58 " siga 0\n"
59 " ipm %0\n"
60 " srl %0,28\n"
61 : "=d" (cc)
62 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
63 return cc;
64}
65
66/**
67 * do_siga_output - perform SIGA-w/wt function
68 * @schid: subchannel id or in case of QEBSM the subchannel token
69 * @mask: which output queues to process
70 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
71 * @fc: function code to perform
72 *
73 * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
74 * Note: For IQDC unicast queues only the highest priority queue is processed.
75 */
76static inline int do_siga_output(unsigned long schid, unsigned long mask,
77 u32 *bb, unsigned int fc)
78{
79 register unsigned long __fc asm("0") = fc;
80 register unsigned long __schid asm("1") = schid;
81 register unsigned long __mask asm("2") = mask;
82 int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
83
84 asm volatile(
85 " siga 0\n"
86 "0: ipm %0\n"
87 " srl %0,28\n"
88 "1:\n"
89 EX_TABLE(0b, 1b)
90 : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
91 : : "cc", "memory");
92 *bb = ((unsigned int) __fc) >> 31;
93 return cc;
94}
95
96static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
97{
98 char dbf_text[15];
99
100 /* all done or next buffer state different */
101 if (ccq == 0 || ccq == 32)
102 return 0;
103 /* not all buffers processed */
104 if (ccq == 96 || ccq == 97)
105 return 1;
106 /* notify devices immediately */
107 sprintf(dbf_text, "%d", ccq);
108 QDIO_DBF_TEXT2(1, trace, dbf_text);
109 return -EIO;
110}
111
112/**
113 * qdio_do_eqbs - extract buffer states for QEBSM
114 * @q: queue to manipulate
115 * @state: state of the extracted buffers
116 * @start: buffer number to start at
117 * @count: count of buffers to examine
118 *
119 * Returns the number of successfull extracted equal buffer states.
120 * Stops processing if a state is different from the last buffers state.
121 */
122static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
123 int start, int count)
124{
125 unsigned int ccq = 0;
126 int tmp_count = count, tmp_start = start;
127 int nr = q->nr;
128 int rc;
129 char dbf_text[15];
130
131 BUG_ON(!q->irq_ptr->sch_token);
132
133 if (!q->is_input_q)
134 nr += q->irq_ptr->nr_input_qs;
135again:
136 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
137 rc = qdio_check_ccq(q, ccq);
138
139 /* At least one buffer was processed, return and extract the remaining
140 * buffers later.
141 */
142 if ((ccq == 96) && (count != tmp_count))
143 return (count - tmp_count);
144 if (rc == 1) {
145 QDIO_DBF_TEXT5(1, trace, "eqAGAIN");
146 goto again;
147 }
148
149 if (rc < 0) {
150 QDIO_DBF_TEXT2(1, trace, "eqberr");
151 sprintf(dbf_text, "%2x,%2x,%d,%d", count, tmp_count, ccq, nr);
152 QDIO_DBF_TEXT2(1, trace, dbf_text);
153 q->handler(q->irq_ptr->cdev,
154 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
155 0, -1, -1, q->irq_ptr->int_parm);
156 return 0;
157 }
158 return count - tmp_count;
159}
160
161/**
162 * qdio_do_sqbs - set buffer states for QEBSM
163 * @q: queue to manipulate
164 * @state: new state of the buffers
165 * @start: first buffer number to change
166 * @count: how many buffers to change
167 *
168 * Returns the number of successfully changed buffers.
169 * Does retrying until the specified count of buffer states is set or an
170 * error occurs.
171 */
172static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
173 int count)
174{
175 unsigned int ccq = 0;
176 int tmp_count = count, tmp_start = start;
177 int nr = q->nr;
178 int rc;
179 char dbf_text[15];
180
181 BUG_ON(!q->irq_ptr->sch_token);
182
183 if (!q->is_input_q)
184 nr += q->irq_ptr->nr_input_qs;
185again:
186 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
187 rc = qdio_check_ccq(q, ccq);
188 if (rc == 1) {
189 QDIO_DBF_TEXT5(1, trace, "sqAGAIN");
190 goto again;
191 }
192 if (rc < 0) {
193 QDIO_DBF_TEXT3(1, trace, "sqberr");
194 sprintf(dbf_text, "%2x,%2x", count, tmp_count);
195 QDIO_DBF_TEXT3(1, trace, dbf_text);
196 sprintf(dbf_text, "%d,%d", ccq, nr);
197 QDIO_DBF_TEXT3(1, trace, dbf_text);
198
199 q->handler(q->irq_ptr->cdev,
200 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
201 0, -1, -1, q->irq_ptr->int_parm);
202 return 0;
203 }
204 WARN_ON(tmp_count);
205 return count - tmp_count;
206}
207
208/* returns number of examined buffers and their common state in *state */
209static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
210 unsigned char *state, unsigned int count)
211{
212 unsigned char __state = 0;
213 int i;
214
215 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
216 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
217
218 if (is_qebsm(q))
219 return qdio_do_eqbs(q, state, bufnr, count);
220
221 for (i = 0; i < count; i++) {
222 if (!__state)
223 __state = q->slsb.val[bufnr];
224 else if (q->slsb.val[bufnr] != __state)
225 break;
226 bufnr = next_buf(bufnr);
227 }
228 *state = __state;
229 return i;
230}
231
232inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
233 unsigned char *state)
234{
235 return get_buf_states(q, bufnr, state, 1);
236}
237
238/* wrap-around safe setting of slsb states, returns number of changed buffers */
239static inline int set_buf_states(struct qdio_q *q, int bufnr,
240 unsigned char state, int count)
241{
242 int i;
243
244 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
245 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
246
247 if (is_qebsm(q))
248 return qdio_do_sqbs(q, state, bufnr, count);
249
250 for (i = 0; i < count; i++) {
251 xchg(&q->slsb.val[bufnr], state);
252 bufnr = next_buf(bufnr);
253 }
254 return count;
255}
256
257static inline int set_buf_state(struct qdio_q *q, int bufnr,
258 unsigned char state)
259{
260 return set_buf_states(q, bufnr, state, 1);
261}
262
263/* set slsb states to initial state */
264void qdio_init_buf_states(struct qdio_irq *irq_ptr)
265{
266 struct qdio_q *q;
267 int i;
268
269 for_each_input_queue(irq_ptr, q, i)
270 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
271 QDIO_MAX_BUFFERS_PER_Q);
272 for_each_output_queue(irq_ptr, q, i)
273 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
274 QDIO_MAX_BUFFERS_PER_Q);
275}
276
277static int qdio_siga_sync(struct qdio_q *q, unsigned int output,
278 unsigned int input)
279{
280 int cc;
281
282 if (!need_siga_sync(q))
283 return 0;
284
285 qdio_perf_stat_inc(&perf_stats.siga_sync);
286
287 cc = do_siga_sync(q->irq_ptr->schid, output, input);
288 if (cc) {
289 QDIO_DBF_TEXT4(0, trace, "sigasync");
290 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
291 QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
292 }
293 return cc;
294}
295
296inline int qdio_siga_sync_q(struct qdio_q *q)
297{
298 if (q->is_input_q)
299 return qdio_siga_sync(q, 0, q->mask);
300 else
301 return qdio_siga_sync(q, q->mask, 0);
302}
303
304static inline int qdio_siga_sync_out(struct qdio_q *q)
305{
306 return qdio_siga_sync(q, ~0U, 0);
307}
308
309static inline int qdio_siga_sync_all(struct qdio_q *q)
310{
311 return qdio_siga_sync(q, ~0U, ~0U);
312}
313
314static inline int qdio_do_siga_output(struct qdio_q *q, unsigned int *busy_bit)
315{
316 unsigned int fc = 0;
317 unsigned long schid;
318
319 if (!is_qebsm(q))
320 schid = *((u32 *)&q->irq_ptr->schid);
321 else {
322 schid = q->irq_ptr->sch_token;
323 fc |= 0x80;
324 }
325 return do_siga_output(schid, q->mask, busy_bit, fc);
326}
327
328static int qdio_siga_output(struct qdio_q *q)
329{
330 int cc;
331 u32 busy_bit;
332 u64 start_time = 0;
333
334 QDIO_DBF_TEXT5(0, trace, "sigaout");
335 QDIO_DBF_HEX5(0, trace, &q, sizeof(void *));
336
337 qdio_perf_stat_inc(&perf_stats.siga_out);
338again:
339 cc = qdio_do_siga_output(q, &busy_bit);
340 if (queue_type(q) == QDIO_IQDIO_QFMT && cc == 2 && busy_bit) {
341 if (!start_time)
342 start_time = get_usecs();
343 else if ((get_usecs() - start_time) < QDIO_BUSY_BIT_PATIENCE)
344 goto again;
345 }
346
347 if (cc == 2 && busy_bit)
348 cc |= QDIO_ERROR_SIGA_BUSY;
349 if (cc)
350 QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
351 return cc;
352}
353
354static inline int qdio_siga_input(struct qdio_q *q)
355{
356 int cc;
357
358 QDIO_DBF_TEXT4(0, trace, "sigain");
359 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
360
361 qdio_perf_stat_inc(&perf_stats.siga_in);
362
363 cc = do_siga_input(q->irq_ptr->schid, q->mask);
364 if (cc)
365 QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
366 return cc;
367}
368
369/* called from thinint inbound handler */
370void qdio_sync_after_thinint(struct qdio_q *q)
371{
372 if (pci_out_supported(q)) {
373 if (need_siga_sync_thinint(q))
374 qdio_siga_sync_all(q);
375 else if (need_siga_sync_out_thinint(q))
376 qdio_siga_sync_out(q);
377 } else
378 qdio_siga_sync_q(q);
379}
380
381inline void qdio_stop_polling(struct qdio_q *q)
382{
383 spin_lock_bh(&q->u.in.lock);
384 if (!q->u.in.polling) {
385 spin_unlock_bh(&q->u.in.lock);
386 return;
387 }
388 q->u.in.polling = 0;
389 qdio_perf_stat_inc(&perf_stats.debug_stop_polling);
390
391 /* show the card that we are not polling anymore */
392 set_buf_state(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT);
393 spin_unlock_bh(&q->u.in.lock);
394}
395
396static void announce_buffer_error(struct qdio_q *q)
397{
398 char dbf_text[15];
399
400 if (q->is_input_q)
401 QDIO_DBF_TEXT3(1, trace, "inperr");
402 else
403 QDIO_DBF_TEXT3(0, trace, "outperr");
404
405 sprintf(dbf_text, "%x-%x-%x", q->first_to_check,
406 q->sbal[q->first_to_check]->element[14].flags,
407 q->sbal[q->first_to_check]->element[15].flags);
408 QDIO_DBF_TEXT3(1, trace, dbf_text);
409 QDIO_DBF_HEX2(1, trace, q->sbal[q->first_to_check], 256);
410
411 q->qdio_error = QDIO_ERROR_SLSB_STATE;
412}
413
414static int get_inbound_buffer_frontier(struct qdio_q *q)
415{
416 int count, stop;
417 unsigned char state;
418
419 /*
420 * If we still poll don't update last_move_ftc, keep the
421 * previously ACK buffer there.
422 */
423 if (!q->u.in.polling)
424 q->last_move_ftc = q->first_to_check;
425
426 /*
427 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
428 * would return 0.
429 */
430 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
431 stop = add_buf(q->first_to_check, count);
432
433 /*
434 * No siga sync here, as a PCI or we after a thin interrupt
435 * will sync the queues.
436 */
437
438 /* need to set count to 1 for non-qebsm */
439 if (!is_qebsm(q))
440 count = 1;
441
442check_next:
443 if (q->first_to_check == stop)
444 goto out;
445
446 count = get_buf_states(q, q->first_to_check, &state, count);
447 if (!count)
448 goto out;
449
450 switch (state) {
451 case SLSB_P_INPUT_PRIMED:
452 QDIO_DBF_TEXT5(0, trace, "inptprim");
453
454 /*
455 * Only ACK the first buffer. The ACK will be removed in
456 * qdio_stop_polling.
457 */
458 if (q->u.in.polling)
459 state = SLSB_P_INPUT_NOT_INIT;
460 else {
461 q->u.in.polling = 1;
462 state = SLSB_P_INPUT_ACK;
463 }
464 set_buf_state(q, q->first_to_check, state);
465
466 /*
467 * Need to change all PRIMED buffers to NOT_INIT, otherwise
468 * we're loosing initiative in the thinint code.
469 */
470 if (count > 1)
471 set_buf_states(q, next_buf(q->first_to_check),
472 SLSB_P_INPUT_NOT_INIT, count - 1);
473
474 /*
475 * No siga-sync needed for non-qebsm here, as the inbound queue
476 * will be synced on the next siga-r, resp.
477 * tiqdio_is_inbound_q_done will do the siga-sync.
478 */
479 q->first_to_check = add_buf(q->first_to_check, count);
480 atomic_sub(count, &q->nr_buf_used);
481 goto check_next;
482 case SLSB_P_INPUT_ERROR:
483 announce_buffer_error(q);
484 /* process the buffer, the upper layer will take care of it */
485 q->first_to_check = add_buf(q->first_to_check, count);
486 atomic_sub(count, &q->nr_buf_used);
487 break;
488 case SLSB_CU_INPUT_EMPTY:
489 case SLSB_P_INPUT_NOT_INIT:
490 case SLSB_P_INPUT_ACK:
491 QDIO_DBF_TEXT5(0, trace, "inpnipro");
492 break;
493 default:
494 BUG();
495 }
496out:
497 QDIO_DBF_HEX4(0, trace, &q->first_to_check, sizeof(int));
498 return q->first_to_check;
499}
500
501int qdio_inbound_q_moved(struct qdio_q *q)
502{
503 int bufnr;
504
505 bufnr = get_inbound_buffer_frontier(q);
506
507 if ((bufnr != q->last_move_ftc) || q->qdio_error) {
508 if (!need_siga_sync(q) && !pci_out_supported(q))
509 q->u.in.timestamp = get_usecs();
510
511 QDIO_DBF_TEXT4(0, trace, "inhasmvd");
512 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
513 return 1;
514 } else
515 return 0;
516}
517
518static int qdio_inbound_q_done(struct qdio_q *q)
519{
520 unsigned char state;
521#ifdef CONFIG_QDIO_DEBUG
522 char dbf_text[15];
523#endif
524
525 if (!atomic_read(&q->nr_buf_used))
526 return 1;
527
528 /*
529 * We need that one for synchronization with the adapter, as it
530 * does a kind of PCI avoidance.
531 */
532 qdio_siga_sync_q(q);
533
534 get_buf_state(q, q->first_to_check, &state);
535 if (state == SLSB_P_INPUT_PRIMED)
536 /* we got something to do */
537 return 0;
538
539 /* on VM, we don't poll, so the q is always done here */
540 if (need_siga_sync(q) || pci_out_supported(q))
541 return 1;
542
543 /*
544 * At this point we know, that inbound first_to_check
545 * has (probably) not moved (see qdio_inbound_processing).
546 */
547 if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
548#ifdef CONFIG_QDIO_DEBUG
549 QDIO_DBF_TEXT4(0, trace, "inqisdon");
550 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
551 sprintf(dbf_text, "pf%02x", q->first_to_check);
552 QDIO_DBF_TEXT4(0, trace, dbf_text);
553#endif /* CONFIG_QDIO_DEBUG */
554 return 1;
555 } else {
556#ifdef CONFIG_QDIO_DEBUG
557 QDIO_DBF_TEXT4(0, trace, "inqisntd");
558 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
559 sprintf(dbf_text, "pf%02x", q->first_to_check);
560 QDIO_DBF_TEXT4(0, trace, dbf_text);
561#endif /* CONFIG_QDIO_DEBUG */
562 return 0;
563 }
564}
565
566void qdio_kick_inbound_handler(struct qdio_q *q)
567{
568 int count, start, end;
569#ifdef CONFIG_QDIO_DEBUG
570 char dbf_text[15];
571#endif
572
573 qdio_perf_stat_inc(&perf_stats.inbound_handler);
574
575 start = q->first_to_kick;
576 end = q->first_to_check;
577 if (end >= start)
578 count = end - start;
579 else
580 count = end + QDIO_MAX_BUFFERS_PER_Q - start;
581
582#ifdef CONFIG_QDIO_DEBUG
583 sprintf(dbf_text, "s=%2xc=%2x", start, count);
584 QDIO_DBF_TEXT4(0, trace, dbf_text);
585#endif /* CONFIG_QDIO_DEBUG */
586
587 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
588 return;
589
590 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr,
591 start, count, q->irq_ptr->int_parm);
592
593 /* for the next time */
594 q->first_to_kick = q->first_to_check;
595 q->qdio_error = 0;
596}
597
598static void __qdio_inbound_processing(struct qdio_q *q)
599{
600 qdio_perf_stat_inc(&perf_stats.tasklet_inbound);
601again:
602 if (!qdio_inbound_q_moved(q))
603 return;
604
605 qdio_kick_inbound_handler(q);
606
607 if (!qdio_inbound_q_done(q))
608 /* means poll time is not yet over */
609 goto again;
610
611 qdio_stop_polling(q);
612 /*
613 * We need to check again to not lose initiative after
614 * resetting the ACK state.
615 */
616 if (!qdio_inbound_q_done(q))
617 goto again;
618}
619
620/* inbound tasklet */
621void qdio_inbound_processing(unsigned long data)
622{
623 struct qdio_q *q = (struct qdio_q *)data;
624 __qdio_inbound_processing(q);
625}
626
627static int get_outbound_buffer_frontier(struct qdio_q *q)
628{
629 int count, stop;
630 unsigned char state;
631
632 if (((queue_type(q) != QDIO_IQDIO_QFMT) && !pci_out_supported(q)) ||
633 (queue_type(q) == QDIO_IQDIO_QFMT && multicast_outbound(q)))
634 qdio_siga_sync_q(q);
635
636 /*
637 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
638 * would return 0.
639 */
640 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
641 stop = add_buf(q->first_to_check, count);
642
643 /* need to set count to 1 for non-qebsm */
644 if (!is_qebsm(q))
645 count = 1;
646
647check_next:
648 if (q->first_to_check == stop)
649 return q->first_to_check;
650
651 count = get_buf_states(q, q->first_to_check, &state, count);
652 if (!count)
653 return q->first_to_check;
654
655 switch (state) {
656 case SLSB_P_OUTPUT_EMPTY:
657 /* the adapter got it */
658 QDIO_DBF_TEXT5(0, trace, "outpempt");
659
660 atomic_sub(count, &q->nr_buf_used);
661 q->first_to_check = add_buf(q->first_to_check, count);
662 /*
663 * We fetch all buffer states at once. get_buf_states may
664 * return count < stop. For QEBSM we do not loop.
665 */
666 if (is_qebsm(q))
667 break;
668 goto check_next;
669 case SLSB_P_OUTPUT_ERROR:
670 announce_buffer_error(q);
671 /* process the buffer, the upper layer will take care of it */
672 q->first_to_check = add_buf(q->first_to_check, count);
673 atomic_sub(count, &q->nr_buf_used);
674 break;
675 case SLSB_CU_OUTPUT_PRIMED:
676 /* the adapter has not fetched the output yet */
677 QDIO_DBF_TEXT5(0, trace, "outpprim");
678 break;
679 case SLSB_P_OUTPUT_NOT_INIT:
680 case SLSB_P_OUTPUT_HALTED:
681 break;
682 default:
683 BUG();
684 }
685 return q->first_to_check;
686}
687
688/* all buffers processed? */
689static inline int qdio_outbound_q_done(struct qdio_q *q)
690{
691 return atomic_read(&q->nr_buf_used) == 0;
692}
693
694static inline int qdio_outbound_q_moved(struct qdio_q *q)
695{
696 int bufnr;
697
698 bufnr = get_outbound_buffer_frontier(q);
699
700 if ((bufnr != q->last_move_ftc) || q->qdio_error) {
701 q->last_move_ftc = bufnr;
702 QDIO_DBF_TEXT4(0, trace, "oqhasmvd");
703 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
704 return 1;
705 } else
706 return 0;
707}
708
709/*
710 * VM could present us cc=2 and busy bit set on SIGA-write
711 * during reconfiguration of their Guest LAN (only in iqdio mode,
712 * otherwise qdio is asynchronous and cc=2 and busy bit there will take
713 * the queues down immediately).
714 *
715 * Therefore qdio_siga_output will try for a short time constantly,
716 * if such a condition occurs. If it doesn't change, it will
717 * increase the busy_siga_counter and save the timestamp, and
718 * schedule the queue for later processing. qdio_outbound_processing
719 * will check out the counter. If non-zero, it will call qdio_kick_outbound_q
720 * as often as the value of the counter. This will attempt further SIGA
721 * instructions. For each successful SIGA, the counter is
722 * decreased, for failing SIGAs the counter remains the same, after
723 * all. After some time of no movement, qdio_kick_outbound_q will
724 * finally fail and reflect corresponding error codes to call
725 * the upper layer module and have it take the queues down.
726 *
727 * Note that this is a change from the original HiperSockets design
728 * (saying cc=2 and busy bit means take the queues down), but in
729 * these days Guest LAN didn't exist... excessive cc=2 with busy bit
730 * conditions will still take the queues down, but the threshold is
731 * higher due to the Guest LAN environment.
732 *
733 * Called from outbound tasklet and do_QDIO handler.
734 */
735static void qdio_kick_outbound_q(struct qdio_q *q)
736{
737 int rc;
738#ifdef CONFIG_QDIO_DEBUG
739 char dbf_text[15];
740
741 QDIO_DBF_TEXT5(0, trace, "kickoutq");
742 QDIO_DBF_HEX5(0, trace, &q, sizeof(void *));
743#endif /* CONFIG_QDIO_DEBUG */
744
745 if (!need_siga_out(q))
746 return;
747
748 rc = qdio_siga_output(q);
749 switch (rc) {
750 case 0:
751 /* went smooth this time, reset timestamp */
752 q->u.out.timestamp = 0;
753
754 /* TODO: improve error handling for CC=0 case */
755#ifdef CONFIG_QDIO_DEBUG
756 QDIO_DBF_TEXT3(0, trace, "cc2reslv");
757 sprintf(dbf_text, "%4x%2x%2x", q->irq_ptr->schid.sch_no, q->nr,
758 atomic_read(&q->u.out.busy_siga_counter));
759 QDIO_DBF_TEXT3(0, trace, dbf_text);
760#endif /* CONFIG_QDIO_DEBUG */
761 break;
762 /* cc=2 and busy bit */
763 case (2 | QDIO_ERROR_SIGA_BUSY):
764 atomic_inc(&q->u.out.busy_siga_counter);
765
766 /* if the last siga was successful, save timestamp here */
767 if (!q->u.out.timestamp)
768 q->u.out.timestamp = get_usecs();
769
770 /* if we're in time, don't touch qdio_error */
771 if (get_usecs() - q->u.out.timestamp < QDIO_BUSY_BIT_GIVE_UP) {
772 tasklet_schedule(&q->tasklet);
773 break;
774 }
775 QDIO_DBF_TEXT2(0, trace, "cc2REPRT");
776#ifdef CONFIG_QDIO_DEBUG
777 sprintf(dbf_text, "%4x%2x%2x", q->irq_ptr->schid.sch_no, q->nr,
778 atomic_read(&q->u.out.busy_siga_counter));
779 QDIO_DBF_TEXT3(0, trace, dbf_text);
780#endif /* CONFIG_QDIO_DEBUG */
781 default:
782 /* for plain cc=1, 2 or 3 */
783 q->qdio_error = rc;
784 }
785}
786
787static void qdio_kick_outbound_handler(struct qdio_q *q)
788{
789 int start, end, count;
790#ifdef CONFIG_QDIO_DEBUG
791 char dbf_text[15];
792#endif
793
794 start = q->first_to_kick;
795 end = q->last_move_ftc;
796 if (end >= start)
797 count = end - start;
798 else
799 count = end + QDIO_MAX_BUFFERS_PER_Q - start;
800
801#ifdef CONFIG_QDIO_DEBUG
802 QDIO_DBF_TEXT4(0, trace, "kickouth");
803 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
804
805 sprintf(dbf_text, "s=%2xc=%2x", start, count);
806 QDIO_DBF_TEXT4(0, trace, dbf_text);
807#endif /* CONFIG_QDIO_DEBUG */
808
809 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
810 return;
811
812 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
813 q->irq_ptr->int_parm);
814
815 /* for the next time: */
816 q->first_to_kick = q->last_move_ftc;
817 q->qdio_error = 0;
818}
819
820static void __qdio_outbound_processing(struct qdio_q *q)
821{
822 int siga_attempts;
823
824 qdio_perf_stat_inc(&perf_stats.tasklet_outbound);
825
826 /* see comment in qdio_kick_outbound_q */
827 siga_attempts = atomic_read(&q->u.out.busy_siga_counter);
828 while (siga_attempts--) {
829 atomic_dec(&q->u.out.busy_siga_counter);
830 qdio_kick_outbound_q(q);
831 }
832
833 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
834
835 if (qdio_outbound_q_moved(q))
836 qdio_kick_outbound_handler(q);
837
838 if (queue_type(q) == QDIO_ZFCP_QFMT) {
839 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
840 tasklet_schedule(&q->tasklet);
841 return;
842 }
843
844 /* bail out for HiperSockets unicast queues */
845 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
846 return;
847
848 if (q->u.out.pci_out_enabled)
849 return;
850
851 /*
852 * Now we know that queue type is either qeth without pci enabled
853 * or HiperSockets multicast. Make sure buffer switch from PRIMED to
854 * EMPTY is noticed and outbound_handler is called after some time.
855 */
856 if (qdio_outbound_q_done(q))
857 del_timer(&q->u.out.timer);
858 else {
859 if (!timer_pending(&q->u.out.timer)) {
860 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
861 qdio_perf_stat_inc(&perf_stats.debug_tl_out_timer);
862 }
863 }
864}
865
866/* outbound tasklet */
867void qdio_outbound_processing(unsigned long data)
868{
869 struct qdio_q *q = (struct qdio_q *)data;
870 __qdio_outbound_processing(q);
871}
872
873void qdio_outbound_timer(unsigned long data)
874{
875 struct qdio_q *q = (struct qdio_q *)data;
876 tasklet_schedule(&q->tasklet);
877}
878
879/* called from thinint inbound tasklet */
880void qdio_check_outbound_after_thinint(struct qdio_q *q)
881{
882 struct qdio_q *out;
883 int i;
884
885 if (!pci_out_supported(q))
886 return;
887
888 for_each_output_queue(q->irq_ptr, out, i)
889 if (!qdio_outbound_q_done(out))
890 tasklet_schedule(&out->tasklet);
891}
892
893static inline void qdio_set_state(struct qdio_irq *irq_ptr,
894 enum qdio_irq_states state)
895{
896#ifdef CONFIG_QDIO_DEBUG
897 char dbf_text[15];
898
899 QDIO_DBF_TEXT5(0, trace, "newstate");
900 sprintf(dbf_text, "%4x%4x", irq_ptr->schid.sch_no, state);
901 QDIO_DBF_TEXT5(0, trace, dbf_text);
902#endif /* CONFIG_QDIO_DEBUG */
903
904 irq_ptr->state = state;
905 mb();
906}
907
908static void qdio_irq_check_sense(struct subchannel_id schid, struct irb *irb)
909{
910 char dbf_text[15];
911
912 if (irb->esw.esw0.erw.cons) {
913 sprintf(dbf_text, "sens%4x", schid.sch_no);
914 QDIO_DBF_TEXT2(1, trace, dbf_text);
915 QDIO_DBF_HEX0(0, trace, irb, 64);
916 QDIO_DBF_HEX0(0, trace, irb->ecw, 64);
917 }
918}
919
920/* PCI interrupt handler */
921static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
922{
923 int i;
924 struct qdio_q *q;
925
926 qdio_perf_stat_inc(&perf_stats.pci_int);
927
928 for_each_input_queue(irq_ptr, q, i)
929 tasklet_schedule(&q->tasklet);
930
931 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
932 return;
933
934 for_each_output_queue(irq_ptr, q, i) {
935 if (qdio_outbound_q_done(q))
936 continue;
937
938 if (!siga_syncs_out_pci(q))
939 qdio_siga_sync_q(q);
940
941 tasklet_schedule(&q->tasklet);
942 }
943}
944
945static void qdio_handle_activate_check(struct ccw_device *cdev,
946 unsigned long intparm, int cstat, int dstat)
947{
948 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
949 struct qdio_q *q;
950 char dbf_text[15];
951
952 QDIO_DBF_TEXT2(1, trace, "ick2");
953 sprintf(dbf_text, "%s", cdev->dev.bus_id);
954 QDIO_DBF_TEXT2(1, trace, dbf_text);
955 QDIO_DBF_HEX2(0, trace, &intparm, sizeof(int));
956 QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
957 QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
958
959 if (irq_ptr->nr_input_qs) {
960 q = irq_ptr->input_qs[0];
961 } else if (irq_ptr->nr_output_qs) {
962 q = irq_ptr->output_qs[0];
963 } else {
964 dump_stack();
965 goto no_handler;
966 }
967 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
968 0, -1, -1, irq_ptr->int_parm);
969no_handler:
970 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
971}
972
973static void qdio_call_shutdown(struct work_struct *work)
974{
975 struct ccw_device_private *priv;
976 struct ccw_device *cdev;
977
978 priv = container_of(work, struct ccw_device_private, kick_work);
979 cdev = priv->cdev;
980 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
981 put_device(&cdev->dev);
982}
983
984static void qdio_int_error(struct ccw_device *cdev)
985{
986 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
987
988 switch (irq_ptr->state) {
989 case QDIO_IRQ_STATE_INACTIVE:
990 case QDIO_IRQ_STATE_CLEANUP:
991 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
992 break;
993 case QDIO_IRQ_STATE_ESTABLISHED:
994 case QDIO_IRQ_STATE_ACTIVE:
995 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
996 if (get_device(&cdev->dev)) {
997 /* Can't call shutdown from interrupt context. */
998 PREPARE_WORK(&cdev->private->kick_work,
999 qdio_call_shutdown);
1000 queue_work(ccw_device_work, &cdev->private->kick_work);
1001 }
1002 break;
1003 default:
1004 WARN_ON(1);
1005 }
1006 wake_up(&cdev->private->wait_q);
1007}
1008
1009static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
1010 int dstat)
1011{
1012 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1013
1014 if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
1015 QDIO_DBF_TEXT2(1, setup, "eq:ckcon");
1016 goto error;
1017 }
1018
1019 if (!(dstat & DEV_STAT_DEV_END)) {
1020 QDIO_DBF_TEXT2(1, setup, "eq:no de");
1021 goto error;
1022 }
1023
1024 if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
1025 QDIO_DBF_TEXT2(1, setup, "eq:badio");
1026 goto error;
1027 }
1028 return 0;
1029error:
1030 QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
1031 QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
1032 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1033 return 1;
1034}
1035
1036static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1037 int dstat)
1038{
1039 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1040 char dbf_text[15];
1041
1042 sprintf(dbf_text, "qehi%4x", cdev->private->schid.sch_no);
1043 QDIO_DBF_TEXT0(0, setup, dbf_text);
1044 QDIO_DBF_TEXT0(0, trace, dbf_text);
1045
1046 if (!qdio_establish_check_errors(cdev, cstat, dstat))
1047 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1048}
1049
1050/* qdio interrupt handler */
1051void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1052 struct irb *irb)
1053{
1054 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1055 int cstat, dstat;
1056 char dbf_text[15];
1057
1058 qdio_perf_stat_inc(&perf_stats.qdio_int);
1059
1060 if (!intparm || !irq_ptr) {
1061 sprintf(dbf_text, "qihd%4x", cdev->private->schid.sch_no);
1062 QDIO_DBF_TEXT2(1, setup, dbf_text);
1063 return;
1064 }
1065
1066 if (IS_ERR(irb)) {
1067 switch (PTR_ERR(irb)) {
1068 case -EIO:
1069 sprintf(dbf_text, "ierr%4x",
1070 cdev->private->schid.sch_no);
1071 QDIO_DBF_TEXT2(1, setup, dbf_text);
1072 qdio_int_error(cdev);
1073 return;
1074 case -ETIMEDOUT:
1075 sprintf(dbf_text, "qtoh%4x",
1076 cdev->private->schid.sch_no);
1077 QDIO_DBF_TEXT2(1, setup, dbf_text);
1078 qdio_int_error(cdev);
1079 return;
1080 default:
1081 WARN_ON(1);
1082 return;
1083 }
1084 }
1085 qdio_irq_check_sense(irq_ptr->schid, irb);
1086
1087 cstat = irb->scsw.cmd.cstat;
1088 dstat = irb->scsw.cmd.dstat;
1089
1090 switch (irq_ptr->state) {
1091 case QDIO_IRQ_STATE_INACTIVE:
1092 qdio_establish_handle_irq(cdev, cstat, dstat);
1093 break;
1094
1095 case QDIO_IRQ_STATE_CLEANUP:
1096 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1097 break;
1098
1099 case QDIO_IRQ_STATE_ESTABLISHED:
1100 case QDIO_IRQ_STATE_ACTIVE:
1101 if (cstat & SCHN_STAT_PCI) {
1102 qdio_int_handler_pci(irq_ptr);
1103 /* no state change so no need to wake up wait_q */
1104 return;
1105 }
1106 if ((cstat & ~SCHN_STAT_PCI) || dstat) {
1107 qdio_handle_activate_check(cdev, intparm, cstat,
1108 dstat);
1109 break;
1110 }
1111 default:
1112 WARN_ON(1);
1113 }
1114 wake_up(&cdev->private->wait_q);
1115}
1116
1117/**
1118 * qdio_get_ssqd_desc - get qdio subchannel description
1119 * @cdev: ccw device to get description for
1120 *
1121 * Returns a pointer to the saved qdio subchannel description,
1122 * or NULL for not setup qdio devices.
1123 */
1124struct qdio_ssqd_desc *qdio_get_ssqd_desc(struct ccw_device *cdev)
1125{
1126 struct qdio_irq *irq_ptr;
1127
1128 QDIO_DBF_TEXT0(0, setup, "getssqd");
1129
1130 irq_ptr = cdev->private->qdio_data;
1131 if (!irq_ptr)
1132 return NULL;
1133
1134 return &irq_ptr->ssqd_desc;
1135}
1136EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1137
1138/**
1139 * qdio_cleanup - shutdown queues and free data structures
1140 * @cdev: associated ccw device
1141 * @how: use halt or clear to shutdown
1142 *
1143 * This function calls qdio_shutdown() for @cdev with method @how
1144 * and on success qdio_free() for @cdev.
1145 */
1146int qdio_cleanup(struct ccw_device *cdev, int how)
1147{
1148 struct qdio_irq *irq_ptr;
1149 char dbf_text[15];
1150 int rc;
1151
1152 irq_ptr = cdev->private->qdio_data;
1153 if (!irq_ptr)
1154 return -ENODEV;
1155
1156 sprintf(dbf_text, "qcln%4x", irq_ptr->schid.sch_no);
1157 QDIO_DBF_TEXT1(0, trace, dbf_text);
1158 QDIO_DBF_TEXT0(0, setup, dbf_text);
1159
1160 rc = qdio_shutdown(cdev, how);
1161 if (rc == 0)
1162 rc = qdio_free(cdev);
1163 return rc;
1164}
1165EXPORT_SYMBOL_GPL(qdio_cleanup);
1166
1167static void qdio_shutdown_queues(struct ccw_device *cdev)
1168{
1169 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1170 struct qdio_q *q;
1171 int i;
1172
1173 for_each_input_queue(irq_ptr, q, i)
1174 tasklet_disable(&q->tasklet);
1175
1176 for_each_output_queue(irq_ptr, q, i) {
1177 tasklet_disable(&q->tasklet);
1178 del_timer(&q->u.out.timer);
1179 }
1180}
1181
1182/**
1183 * qdio_shutdown - shut down a qdio subchannel
1184 * @cdev: associated ccw device
1185 * @how: use halt or clear to shutdown
1186 */
1187int qdio_shutdown(struct ccw_device *cdev, int how)
1188{
1189 struct qdio_irq *irq_ptr;
1190 int rc;
1191 unsigned long flags;
1192 char dbf_text[15];
1193
1194 irq_ptr = cdev->private->qdio_data;
1195 if (!irq_ptr)
1196 return -ENODEV;
1197
1198 mutex_lock(&irq_ptr->setup_mutex);
1199 /*
1200 * Subchannel was already shot down. We cannot prevent being called
1201 * twice since cio may trigger a shutdown asynchronously.
1202 */
1203 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1204 mutex_unlock(&irq_ptr->setup_mutex);
1205 return 0;
1206 }
1207
1208 sprintf(dbf_text, "qsqs%4x", irq_ptr->schid.sch_no);
1209 QDIO_DBF_TEXT1(0, trace, dbf_text);
1210 QDIO_DBF_TEXT0(0, setup, dbf_text);
1211
1212 tiqdio_remove_input_queues(irq_ptr);
1213 qdio_shutdown_queues(cdev);
1214 qdio_shutdown_debug_entries(irq_ptr, cdev);
1215
1216 /* cleanup subchannel */
1217 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1218
1219 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1220 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1221 else
1222 /* default behaviour is halt */
1223 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1224 if (rc) {
1225 sprintf(dbf_text, "sher%4x", irq_ptr->schid.sch_no);
1226 QDIO_DBF_TEXT0(0, setup, dbf_text);
1227 sprintf(dbf_text, "rc=%d", rc);
1228 QDIO_DBF_TEXT0(0, setup, dbf_text);
1229 goto no_cleanup;
1230 }
1231
1232 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1233 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1234 wait_event_interruptible_timeout(cdev->private->wait_q,
1235 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1236 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1237 10 * HZ);
1238 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1239
1240no_cleanup:
1241 qdio_shutdown_thinint(irq_ptr);
1242
1243 /* restore interrupt handler */
1244 if ((void *)cdev->handler == (void *)qdio_int_handler)
1245 cdev->handler = irq_ptr->orig_handler;
1246 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1247
1248 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1249 mutex_unlock(&irq_ptr->setup_mutex);
1250 module_put(THIS_MODULE);
1251 if (rc)
1252 return rc;
1253 return 0;
1254}
1255EXPORT_SYMBOL_GPL(qdio_shutdown);
1256
1257/**
1258 * qdio_free - free data structures for a qdio subchannel
1259 * @cdev: associated ccw device
1260 */
1261int qdio_free(struct ccw_device *cdev)
1262{
1263 struct qdio_irq *irq_ptr;
1264 char dbf_text[15];
1265
1266 irq_ptr = cdev->private->qdio_data;
1267 if (!irq_ptr)
1268 return -ENODEV;
1269
1270 mutex_lock(&irq_ptr->setup_mutex);
1271
1272 sprintf(dbf_text, "qfqs%4x", irq_ptr->schid.sch_no);
1273 QDIO_DBF_TEXT1(0, trace, dbf_text);
1274 QDIO_DBF_TEXT0(0, setup, dbf_text);
1275
1276 cdev->private->qdio_data = NULL;
1277 mutex_unlock(&irq_ptr->setup_mutex);
1278
1279 qdio_release_memory(irq_ptr);
1280 return 0;
1281}
1282EXPORT_SYMBOL_GPL(qdio_free);
1283
1284/**
1285 * qdio_initialize - allocate and establish queues for a qdio subchannel
1286 * @init_data: initialization data
1287 *
1288 * This function first allocates queues via qdio_allocate() and on success
1289 * establishes them via qdio_establish().
1290 */
1291int qdio_initialize(struct qdio_initialize *init_data)
1292{
1293 int rc;
1294 char dbf_text[15];
1295
1296 sprintf(dbf_text, "qini%4x", init_data->cdev->private->schid.sch_no);
1297 QDIO_DBF_TEXT0(0, setup, dbf_text);
1298 QDIO_DBF_TEXT0(0, trace, dbf_text);
1299
1300 rc = qdio_allocate(init_data);
1301 if (rc)
1302 return rc;
1303
1304 rc = qdio_establish(init_data);
1305 if (rc)
1306 qdio_free(init_data->cdev);
1307 return rc;
1308}
1309EXPORT_SYMBOL_GPL(qdio_initialize);
1310
1311/**
1312 * qdio_allocate - allocate qdio queues and associated data
1313 * @init_data: initialization data
1314 */
1315int qdio_allocate(struct qdio_initialize *init_data)
1316{
1317 struct qdio_irq *irq_ptr;
1318 char dbf_text[15];
1319
1320 sprintf(dbf_text, "qalc%4x", init_data->cdev->private->schid.sch_no);
1321 QDIO_DBF_TEXT0(0, setup, dbf_text);
1322 QDIO_DBF_TEXT0(0, trace, dbf_text);
1323
1324 if ((init_data->no_input_qs && !init_data->input_handler) ||
1325 (init_data->no_output_qs && !init_data->output_handler))
1326 return -EINVAL;
1327
1328 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1329 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1330 return -EINVAL;
1331
1332 if ((!init_data->input_sbal_addr_array) ||
1333 (!init_data->output_sbal_addr_array))
1334 return -EINVAL;
1335
1336 qdio_allocate_do_dbf(init_data);
1337
1338 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1339 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1340 if (!irq_ptr)
1341 goto out_err;
1342 QDIO_DBF_TEXT0(0, setup, "irq_ptr:");
1343 QDIO_DBF_HEX0(0, setup, &irq_ptr, sizeof(void *));
1344
1345 mutex_init(&irq_ptr->setup_mutex);
1346
1347 /*
1348 * Allocate a page for the chsc calls in qdio_establish.
1349 * Must be pre-allocated since a zfcp recovery will call
1350 * qdio_establish. In case of low memory and swap on a zfcp disk
1351 * we may not be able to allocate memory otherwise.
1352 */
1353 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1354 if (!irq_ptr->chsc_page)
1355 goto out_rel;
1356
1357 /* qdr is used in ccw1.cda which is u32 */
1358 irq_ptr->qdr = kzalloc(sizeof(struct qdr), GFP_KERNEL | GFP_DMA);
1359 if (!irq_ptr->qdr)
1360 goto out_rel;
1361 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1362
1363 QDIO_DBF_TEXT0(0, setup, "qdr:");
1364 QDIO_DBF_HEX0(0, setup, &irq_ptr->qdr, sizeof(void *));
1365
1366 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1367 init_data->no_output_qs))
1368 goto out_rel;
1369
1370 init_data->cdev->private->qdio_data = irq_ptr;
1371 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1372 return 0;
1373out_rel:
1374 qdio_release_memory(irq_ptr);
1375out_err:
1376 return -ENOMEM;
1377}
1378EXPORT_SYMBOL_GPL(qdio_allocate);
1379
1380/**
1381 * qdio_establish - establish queues on a qdio subchannel
1382 * @init_data: initialization data
1383 */
1384int qdio_establish(struct qdio_initialize *init_data)
1385{
1386 char dbf_text[20];
1387 struct qdio_irq *irq_ptr;
1388 struct ccw_device *cdev = init_data->cdev;
1389 unsigned long saveflags;
1390 int rc;
1391
1392 irq_ptr = cdev->private->qdio_data;
1393 if (!irq_ptr)
1394 return -ENODEV;
1395
1396 if (cdev->private->state != DEV_STATE_ONLINE)
1397 return -EINVAL;
1398
1399 if (!try_module_get(THIS_MODULE))
1400 return -EINVAL;
1401
1402 sprintf(dbf_text, "qest%4x", cdev->private->schid.sch_no);
1403 QDIO_DBF_TEXT0(0, setup, dbf_text);
1404 QDIO_DBF_TEXT0(0, trace, dbf_text);
1405
1406 mutex_lock(&irq_ptr->setup_mutex);
1407 qdio_setup_irq(init_data);
1408
1409 rc = qdio_establish_thinint(irq_ptr);
1410 if (rc) {
1411 mutex_unlock(&irq_ptr->setup_mutex);
1412 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1413 return rc;
1414 }
1415
1416 /* establish q */
1417 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1418 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1419 irq_ptr->ccw.count = irq_ptr->equeue.count;
1420 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1421
1422 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1423 ccw_device_set_options_mask(cdev, 0);
1424
1425 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1426 if (rc) {
1427 sprintf(dbf_text, "eq:io%4x", irq_ptr->schid.sch_no);
1428 QDIO_DBF_TEXT2(1, setup, dbf_text);
1429 sprintf(dbf_text, "eq:rc%4x", rc);
1430 QDIO_DBF_TEXT2(1, setup, dbf_text);
1431 }
1432 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1433
1434 if (rc) {
1435 mutex_unlock(&irq_ptr->setup_mutex);
1436 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1437 return rc;
1438 }
1439
1440 wait_event_interruptible_timeout(cdev->private->wait_q,
1441 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1442 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1443
1444 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1445 mutex_unlock(&irq_ptr->setup_mutex);
1446 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1447 return -EIO;
1448 }
1449
1450 qdio_setup_ssqd_info(irq_ptr);
1451 sprintf(dbf_text, "qib ac%2x", irq_ptr->qib.ac);
1452 QDIO_DBF_TEXT2(0, setup, dbf_text);
1453
1454 /* qebsm is now setup if available, initialize buffer states */
1455 qdio_init_buf_states(irq_ptr);
1456
1457 mutex_unlock(&irq_ptr->setup_mutex);
1458 qdio_print_subchannel_info(irq_ptr, cdev);
1459 qdio_setup_debug_entries(irq_ptr, cdev);
1460 return 0;
1461}
1462EXPORT_SYMBOL_GPL(qdio_establish);
1463
1464/**
1465 * qdio_activate - activate queues on a qdio subchannel
1466 * @cdev: associated cdev
1467 */
1468int qdio_activate(struct ccw_device *cdev)
1469{
1470 struct qdio_irq *irq_ptr;
1471 int rc;
1472 unsigned long saveflags;
1473 char dbf_text[20];
1474
1475 irq_ptr = cdev->private->qdio_data;
1476 if (!irq_ptr)
1477 return -ENODEV;
1478
1479 if (cdev->private->state != DEV_STATE_ONLINE)
1480 return -EINVAL;
1481
1482 mutex_lock(&irq_ptr->setup_mutex);
1483 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1484 rc = -EBUSY;
1485 goto out;
1486 }
1487
1488 sprintf(dbf_text, "qact%4x", irq_ptr->schid.sch_no);
1489 QDIO_DBF_TEXT2(0, setup, dbf_text);
1490 QDIO_DBF_TEXT2(0, trace, dbf_text);
1491
1492 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1493 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1494 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1495 irq_ptr->ccw.cda = 0;
1496
1497 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1498 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1499
1500 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1501 0, DOIO_DENY_PREFETCH);
1502 if (rc) {
1503 sprintf(dbf_text, "aq:io%4x", irq_ptr->schid.sch_no);
1504 QDIO_DBF_TEXT2(1, setup, dbf_text);
1505 sprintf(dbf_text, "aq:rc%4x", rc);
1506 QDIO_DBF_TEXT2(1, setup, dbf_text);
1507 }
1508 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1509
1510 if (rc)
1511 goto out;
1512
1513 if (is_thinint_irq(irq_ptr))
1514 tiqdio_add_input_queues(irq_ptr);
1515
1516 /* wait for subchannel to become active */
1517 msleep(5);
1518
1519 switch (irq_ptr->state) {
1520 case QDIO_IRQ_STATE_STOPPED:
1521 case QDIO_IRQ_STATE_ERR:
1522 mutex_unlock(&irq_ptr->setup_mutex);
1523 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1524 return -EIO;
1525 default:
1526 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1527 rc = 0;
1528 }
1529out:
1530 mutex_unlock(&irq_ptr->setup_mutex);
1531 return rc;
1532}
1533EXPORT_SYMBOL_GPL(qdio_activate);
1534
1535static inline int buf_in_between(int bufnr, int start, int count)
1536{
1537 int end = add_buf(start, count);
1538
1539 if (end > start) {
1540 if (bufnr >= start && bufnr < end)
1541 return 1;
1542 else
1543 return 0;
1544 }
1545
1546 /* wrap-around case */
1547 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1548 (bufnr < end))
1549 return 1;
1550 else
1551 return 0;
1552}
1553
1554/**
1555 * handle_inbound - reset processed input buffers
1556 * @q: queue containing the buffers
1557 * @callflags: flags
1558 * @bufnr: first buffer to process
1559 * @count: how many buffers are emptied
1560 */
1561static void handle_inbound(struct qdio_q *q, unsigned int callflags,
1562 int bufnr, int count)
1563{
1564 unsigned long flags;
1565 int used, rc;
1566
1567 /*
1568 * do_QDIO could run in parallel with the queue tasklet so the
1569 * upper-layer programm could empty the ACK'ed buffer here.
1570 * If that happens we must clear the polling flag, otherwise
1571 * qdio_stop_polling() could set the buffer to NOT_INIT after
1572 * it was set to EMPTY which would kill us.
1573 */
1574 spin_lock_irqsave(&q->u.in.lock, flags);
1575 if (q->u.in.polling)
1576 if (buf_in_between(q->last_move_ftc, bufnr, count))
1577 q->u.in.polling = 0;
1578
1579 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1580 spin_unlock_irqrestore(&q->u.in.lock, flags);
1581
1582 used = atomic_add_return(count, &q->nr_buf_used) - count;
1583 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1584
1585 /* no need to signal as long as the adapter had free buffers */
1586 if (used)
1587 return;
1588
1589 if (need_siga_in(q)) {
1590 rc = qdio_siga_input(q);
1591 if (rc)
1592 q->qdio_error = rc;
1593 }
1594}
1595
1596/**
1597 * handle_outbound - process filled outbound buffers
1598 * @q: queue containing the buffers
1599 * @callflags: flags
1600 * @bufnr: first buffer to process
1601 * @count: how many buffers are filled
1602 */
1603static void handle_outbound(struct qdio_q *q, unsigned int callflags,
1604 int bufnr, int count)
1605{
1606 unsigned char state;
1607 int used;
1608
1609 qdio_perf_stat_inc(&perf_stats.outbound_handler);
1610
1611 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1612 used = atomic_add_return(count, &q->nr_buf_used);
1613 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1614
1615 if (callflags & QDIO_FLAG_PCI_OUT)
1616 q->u.out.pci_out_enabled = 1;
1617 else
1618 q->u.out.pci_out_enabled = 0;
1619
1620 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1621 if (multicast_outbound(q))
1622 qdio_kick_outbound_q(q);
1623 else
1624 /*
1625 * One siga-w per buffer required for unicast
1626 * HiperSockets.
1627 */
1628 while (count--)
1629 qdio_kick_outbound_q(q);
1630 goto out;
1631 }
1632
1633 if (need_siga_sync(q)) {
1634 qdio_siga_sync_q(q);
1635 goto out;
1636 }
1637
1638 /* try to fast requeue buffers */
1639 get_buf_state(q, prev_buf(bufnr), &state);
1640 if (state != SLSB_CU_OUTPUT_PRIMED)
1641 qdio_kick_outbound_q(q);
1642 else {
1643 QDIO_DBF_TEXT5(0, trace, "fast-req");
1644 qdio_perf_stat_inc(&perf_stats.fast_requeue);
1645 }
1646out:
1647 /* Fixme: could wait forever if called from process context */
1648 tasklet_schedule(&q->tasklet);
1649}
1650
1651/**
1652 * do_QDIO - process input or output buffers
1653 * @cdev: associated ccw_device for the qdio subchannel
1654 * @callflags: input or output and special flags from the program
1655 * @q_nr: queue number
1656 * @bufnr: buffer number
1657 * @count: how many buffers to process
1658 */
1659int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1660 int q_nr, int bufnr, int count)
1661{
1662 struct qdio_irq *irq_ptr;
1663#ifdef CONFIG_QDIO_DEBUG
1664 char dbf_text[20];
1665
1666 sprintf(dbf_text, "doQD%04x", cdev->private->schid.sch_no);
1667 QDIO_DBF_TEXT3(0, trace, dbf_text);
1668#endif /* CONFIG_QDIO_DEBUG */
1669
1670 if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
1671 (count > QDIO_MAX_BUFFERS_PER_Q) ||
1672 (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
1673 return -EINVAL;
1674
1675 if (!count)
1676 return 0;
1677
1678 irq_ptr = cdev->private->qdio_data;
1679 if (!irq_ptr)
1680 return -ENODEV;
1681
1682#ifdef CONFIG_QDIO_DEBUG
1683 if (callflags & QDIO_FLAG_SYNC_INPUT)
1684 QDIO_DBF_HEX3(0, trace, &irq_ptr->input_qs[q_nr],
1685 sizeof(void *));
1686 else
1687 QDIO_DBF_HEX3(0, trace, &irq_ptr->output_qs[q_nr],
1688 sizeof(void *));
1689
1690 sprintf(dbf_text, "flag%04x", callflags);
1691 QDIO_DBF_TEXT3(0, trace, dbf_text);
1692 sprintf(dbf_text, "qi%02xct%02x", bufnr, count);
1693 QDIO_DBF_TEXT3(0, trace, dbf_text);
1694#endif /* CONFIG_QDIO_DEBUG */
1695
1696 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1697 return -EBUSY;
1698
1699 if (callflags & QDIO_FLAG_SYNC_INPUT)
1700 handle_inbound(irq_ptr->input_qs[q_nr],
1701 callflags, bufnr, count);
1702 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1703 handle_outbound(irq_ptr->output_qs[q_nr],
1704 callflags, bufnr, count);
1705 else {
1706 QDIO_DBF_TEXT3(1, trace, "doQD:inv");
1707 return -EINVAL;
1708 }
1709 return 0;
1710}
1711EXPORT_SYMBOL_GPL(do_QDIO);
1712
1713static int __init init_QDIO(void)
1714{
1715 int rc;
1716
1717 rc = qdio_setup_init();
1718 if (rc)
1719 return rc;
1720 rc = tiqdio_allocate_memory();
1721 if (rc)
1722 goto out_cache;
1723 rc = qdio_debug_init();
1724 if (rc)
1725 goto out_ti;
1726 rc = qdio_setup_perf_stats();
1727 if (rc)
1728 goto out_debug;
1729 rc = tiqdio_register_thinints();
1730 if (rc)
1731 goto out_perf;
1732 return 0;
1733
1734out_perf:
1735 qdio_remove_perf_stats();
1736out_debug:
1737 qdio_debug_exit();
1738out_ti:
1739 tiqdio_free_memory();
1740out_cache:
1741 qdio_setup_exit();
1742 return rc;
1743}
1744
1745static void __exit exit_QDIO(void)
1746{
1747 tiqdio_unregister_thinints();
1748 tiqdio_free_memory();
1749 qdio_remove_perf_stats();
1750 qdio_debug_exit();
1751 qdio_setup_exit();
1752}
1753
1754module_init(init_QDIO);
1755module_exit(exit_QDIO);
diff --git a/drivers/s390/cio/qdio_perf.c b/drivers/s390/cio/qdio_perf.c
new file mode 100644
index 000000000000..ea01b85b1cc9
--- /dev/null
+++ b/drivers/s390/cio/qdio_perf.c
@@ -0,0 +1,151 @@
1/*
2 * drivers/s390/cio/qdio_perf.c
3 *
4 * Copyright IBM Corp. 2008
5 *
6 * Author: Jan Glauber (jang@linux.vnet.ibm.com)
7 */
8#include <linux/kernel.h>
9#include <linux/proc_fs.h>
10#include <linux/seq_file.h>
11#include <asm/ccwdev.h>
12
13#include "cio.h"
14#include "css.h"
15#include "device.h"
16#include "ioasm.h"
17#include "chsc.h"
18#include "qdio_debug.h"
19#include "qdio_perf.h"
20
21int qdio_performance_stats;
22struct qdio_perf_stats perf_stats;
23
24#ifdef CONFIG_PROC_FS
25static struct proc_dir_entry *qdio_perf_pde;
26#endif
27
28inline void qdio_perf_stat_inc(atomic_long_t *count)
29{
30 if (qdio_performance_stats)
31 atomic_long_inc(count);
32}
33
34inline void qdio_perf_stat_dec(atomic_long_t *count)
35{
36 if (qdio_performance_stats)
37 atomic_long_dec(count);
38}
39
40/*
41 * procfs functions
42 */
43static int qdio_perf_proc_show(struct seq_file *m, void *v)
44{
45 seq_printf(m, "Number of qdio interrupts\t\t\t: %li\n",
46 (long)atomic_long_read(&perf_stats.qdio_int));
47 seq_printf(m, "Number of PCI interrupts\t\t\t: %li\n",
48 (long)atomic_long_read(&perf_stats.pci_int));
49 seq_printf(m, "Number of adapter interrupts\t\t\t: %li\n",
50 (long)atomic_long_read(&perf_stats.thin_int));
51 seq_printf(m, "\n");
52 seq_printf(m, "Inbound tasklet runs\t\t\t\t: %li\n",
53 (long)atomic_long_read(&perf_stats.tasklet_inbound));
54 seq_printf(m, "Outbound tasklet runs\t\t\t\t: %li\n",
55 (long)atomic_long_read(&perf_stats.tasklet_outbound));
56 seq_printf(m, "Adapter interrupt tasklet runs/loops\t\t: %li/%li\n",
57 (long)atomic_long_read(&perf_stats.tasklet_thinint),
58 (long)atomic_long_read(&perf_stats.tasklet_thinint_loop));
59 seq_printf(m, "Adapter interrupt inbound tasklet runs/loops\t: %li/%li\n",
60 (long)atomic_long_read(&perf_stats.thinint_inbound),
61 (long)atomic_long_read(&perf_stats.thinint_inbound_loop));
62 seq_printf(m, "\n");
63 seq_printf(m, "Number of SIGA In issued\t\t\t: %li\n",
64 (long)atomic_long_read(&perf_stats.siga_in));
65 seq_printf(m, "Number of SIGA Out issued\t\t\t: %li\n",
66 (long)atomic_long_read(&perf_stats.siga_out));
67 seq_printf(m, "Number of SIGA Sync issued\t\t\t: %li\n",
68 (long)atomic_long_read(&perf_stats.siga_sync));
69 seq_printf(m, "\n");
70 seq_printf(m, "Number of inbound transfers\t\t\t: %li\n",
71 (long)atomic_long_read(&perf_stats.inbound_handler));
72 seq_printf(m, "Number of outbound transfers\t\t\t: %li\n",
73 (long)atomic_long_read(&perf_stats.outbound_handler));
74 seq_printf(m, "\n");
75 seq_printf(m, "Number of fast requeues (outg. SBAL w/o SIGA)\t: %li\n",
76 (long)atomic_long_read(&perf_stats.fast_requeue));
77 seq_printf(m, "Number of outbound tasklet mod_timer calls\t: %li\n",
78 (long)atomic_long_read(&perf_stats.debug_tl_out_timer));
79 seq_printf(m, "Number of stop polling calls\t\t\t: %li\n",
80 (long)atomic_long_read(&perf_stats.debug_stop_polling));
81 seq_printf(m, "AI inbound tasklet loops after stop polling\t: %li\n",
82 (long)atomic_long_read(&perf_stats.thinint_inbound_loop2));
83 seq_printf(m, "\n");
84 return 0;
85}
86static int qdio_perf_seq_open(struct inode *inode, struct file *filp)
87{
88 return single_open(filp, qdio_perf_proc_show, NULL);
89}
90
91static struct file_operations qdio_perf_proc_fops = {
92 .owner = THIS_MODULE,
93 .open = qdio_perf_seq_open,
94 .read = seq_read,
95 .llseek = seq_lseek,
96 .release = single_release,
97};
98
99/*
100 * sysfs functions
101 */
102static ssize_t qdio_perf_stats_show(struct bus_type *bus, char *buf)
103{
104 return sprintf(buf, "%i\n", qdio_performance_stats ? 1 : 0);
105}
106
107static ssize_t qdio_perf_stats_store(struct bus_type *bus,
108 const char *buf, size_t count)
109{
110 unsigned long i;
111
112 if (strict_strtoul(buf, 16, &i) != 0)
113 return -EINVAL;
114 if ((i != 0) && (i != 1))
115 return -EINVAL;
116 if (i == qdio_performance_stats)
117 return count;
118
119 qdio_performance_stats = i;
120 /* reset performance statistics */
121 if (i == 0)
122 memset(&perf_stats, 0, sizeof(struct qdio_perf_stats));
123 return count;
124}
125
126static BUS_ATTR(qdio_performance_stats, 0644, qdio_perf_stats_show,
127 qdio_perf_stats_store);
128
129int __init qdio_setup_perf_stats(void)
130{
131 int rc;
132
133 rc = bus_create_file(&ccw_bus_type, &bus_attr_qdio_performance_stats);
134 if (rc)
135 return rc;
136
137#ifdef CONFIG_PROC_FS
138 memset(&perf_stats, 0, sizeof(struct qdio_perf_stats));
139 qdio_perf_pde = proc_create("qdio_perf", S_IFREG | S_IRUGO,
140 NULL, &qdio_perf_proc_fops);
141#endif
142 return 0;
143}
144
145void __exit qdio_remove_perf_stats(void)
146{
147#ifdef CONFIG_PROC_FS
148 remove_proc_entry("qdio_perf", NULL);
149#endif
150 bus_remove_file(&ccw_bus_type, &bus_attr_qdio_performance_stats);
151}
diff --git a/drivers/s390/cio/qdio_perf.h b/drivers/s390/cio/qdio_perf.h
new file mode 100644
index 000000000000..5c406a8b7387
--- /dev/null
+++ b/drivers/s390/cio/qdio_perf.h
@@ -0,0 +1,54 @@
1/*
2 * drivers/s390/cio/qdio_perf.h
3 *
4 * Copyright IBM Corp. 2008
5 *
6 * Author: Jan Glauber (jang@linux.vnet.ibm.com)
7 */
8#ifndef QDIO_PERF_H
9#define QDIO_PERF_H
10
11#include <linux/types.h>
12#include <linux/device.h>
13#include <asm/atomic.h>
14
15struct qdio_perf_stats {
16 /* interrupt handler calls */
17 atomic_long_t qdio_int;
18 atomic_long_t pci_int;
19 atomic_long_t thin_int;
20
21 /* tasklet runs */
22 atomic_long_t tasklet_inbound;
23 atomic_long_t tasklet_outbound;
24 atomic_long_t tasklet_thinint;
25 atomic_long_t tasklet_thinint_loop;
26 atomic_long_t thinint_inbound;
27 atomic_long_t thinint_inbound_loop;
28 atomic_long_t thinint_inbound_loop2;
29
30 /* signal adapter calls */
31 atomic_long_t siga_out;
32 atomic_long_t siga_in;
33 atomic_long_t siga_sync;
34
35 /* misc */
36 atomic_long_t inbound_handler;
37 atomic_long_t outbound_handler;
38 atomic_long_t fast_requeue;
39
40 /* for debugging */
41 atomic_long_t debug_tl_out_timer;
42 atomic_long_t debug_stop_polling;
43};
44
45extern struct qdio_perf_stats perf_stats;
46extern int qdio_performance_stats;
47
48int qdio_setup_perf_stats(void);
49void qdio_remove_perf_stats(void);
50
51extern void qdio_perf_stat_inc(atomic_long_t *count);
52extern void qdio_perf_stat_dec(atomic_long_t *count);
53
54#endif
diff --git a/drivers/s390/cio/qdio_setup.c b/drivers/s390/cio/qdio_setup.c
new file mode 100644
index 000000000000..f0923a8aceda
--- /dev/null
+++ b/drivers/s390/cio/qdio_setup.c
@@ -0,0 +1,521 @@
1/*
2 * driver/s390/cio/qdio_setup.c
3 *
4 * qdio queue initialization
5 *
6 * Copyright (C) IBM Corp. 2008
7 * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
8 */
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <asm/qdio.h>
12
13#include "cio.h"
14#include "css.h"
15#include "device.h"
16#include "ioasm.h"
17#include "chsc.h"
18#include "qdio.h"
19#include "qdio_debug.h"
20
21static struct kmem_cache *qdio_q_cache;
22
23/*
24 * qebsm is only available under 64bit but the adapter sets the feature
25 * flag anyway, so we manually override it.
26 */
27static inline int qebsm_possible(void)
28{
29#ifdef CONFIG_64BIT
30 return css_general_characteristics.qebsm;
31#endif
32 return 0;
33}
34
35/*
36 * qib_param_field: pointer to 128 bytes or NULL, if no param field
37 * nr_input_qs: pointer to nr_queues*128 words of data or NULL
38 */
39static void set_impl_params(struct qdio_irq *irq_ptr,
40 unsigned int qib_param_field_format,
41 unsigned char *qib_param_field,
42 unsigned long *input_slib_elements,
43 unsigned long *output_slib_elements)
44{
45 struct qdio_q *q;
46 int i, j;
47
48 if (!irq_ptr)
49 return;
50
51 WARN_ON((unsigned long)&irq_ptr->qib & 0xff);
52 irq_ptr->qib.pfmt = qib_param_field_format;
53 if (qib_param_field)
54 memcpy(irq_ptr->qib.parm, qib_param_field,
55 QDIO_MAX_BUFFERS_PER_Q);
56
57 if (!input_slib_elements)
58 goto output;
59
60 for_each_input_queue(irq_ptr, q, i) {
61 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
62 q->slib->slibe[j].parms =
63 input_slib_elements[i * QDIO_MAX_BUFFERS_PER_Q + j];
64 }
65output:
66 if (!output_slib_elements)
67 return;
68
69 for_each_output_queue(irq_ptr, q, i) {
70 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
71 q->slib->slibe[j].parms =
72 output_slib_elements[i * QDIO_MAX_BUFFERS_PER_Q + j];
73 }
74}
75
76static int __qdio_allocate_qs(struct qdio_q **irq_ptr_qs, int nr_queues)
77{
78 struct qdio_q *q;
79 int i;
80
81 for (i = 0; i < nr_queues; i++) {
82 q = kmem_cache_alloc(qdio_q_cache, GFP_KERNEL);
83 if (!q)
84 return -ENOMEM;
85 WARN_ON((unsigned long)q & 0xff);
86
87 q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
88 if (!q->slib) {
89 kmem_cache_free(qdio_q_cache, q);
90 return -ENOMEM;
91 }
92 WARN_ON((unsigned long)q->slib & 0x7ff);
93 irq_ptr_qs[i] = q;
94 }
95 return 0;
96}
97
98int qdio_allocate_qs(struct qdio_irq *irq_ptr, int nr_input_qs, int nr_output_qs)
99{
100 int rc;
101
102 rc = __qdio_allocate_qs(irq_ptr->input_qs, nr_input_qs);
103 if (rc)
104 return rc;
105 rc = __qdio_allocate_qs(irq_ptr->output_qs, nr_output_qs);
106 return rc;
107}
108
109static void setup_queues_misc(struct qdio_q *q, struct qdio_irq *irq_ptr,
110 qdio_handler_t *handler, int i)
111{
112 /* must be cleared by every qdio_establish */
113 memset(q, 0, ((char *)&q->slib) - ((char *)q));
114 memset(q->slib, 0, PAGE_SIZE);
115
116 q->irq_ptr = irq_ptr;
117 q->mask = 1 << (31 - i);
118 q->nr = i;
119 q->handler = handler;
120}
121
122static void setup_storage_lists(struct qdio_q *q, struct qdio_irq *irq_ptr,
123 void **sbals_array, char *dbf_text, int i)
124{
125 struct qdio_q *prev;
126 int j;
127
128 QDIO_DBF_TEXT0(0, setup, dbf_text);
129 QDIO_DBF_HEX0(0, setup, &q, sizeof(void *));
130
131 q->sl = (struct sl *)((char *)q->slib + PAGE_SIZE / 2);
132
133 /* fill in sbal */
134 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++) {
135 q->sbal[j] = *sbals_array++;
136 WARN_ON((unsigned long)q->sbal[j] & 0xff);
137 }
138
139 /* fill in slib */
140 if (i > 0) {
141 prev = (q->is_input_q) ? irq_ptr->input_qs[i - 1]
142 : irq_ptr->output_qs[i - 1];
143 prev->slib->nsliba = (unsigned long)q->slib;
144 }
145
146 q->slib->sla = (unsigned long)q->sl;
147 q->slib->slsba = (unsigned long)&q->slsb.val[0];
148
149 /* fill in sl */
150 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
151 q->sl->element[j].sbal = (unsigned long)q->sbal[j];
152
153 QDIO_DBF_TEXT2(0, setup, "sl-sb-b0");
154 QDIO_DBF_HEX2(0, setup, q->sl, sizeof(void *));
155 QDIO_DBF_HEX2(0, setup, &q->slsb, sizeof(void *));
156 QDIO_DBF_HEX2(0, setup, q->sbal, sizeof(void *));
157}
158
159static void setup_queues(struct qdio_irq *irq_ptr,
160 struct qdio_initialize *qdio_init)
161{
162 char dbf_text[20];
163 struct qdio_q *q;
164 void **input_sbal_array = qdio_init->input_sbal_addr_array;
165 void **output_sbal_array = qdio_init->output_sbal_addr_array;
166 int i;
167
168 sprintf(dbf_text, "qfqs%4x", qdio_init->cdev->private->schid.sch_no);
169 QDIO_DBF_TEXT0(0, setup, dbf_text);
170
171 for_each_input_queue(irq_ptr, q, i) {
172 sprintf(dbf_text, "in-q%4x", i);
173 setup_queues_misc(q, irq_ptr, qdio_init->input_handler, i);
174
175 q->is_input_q = 1;
176 spin_lock_init(&q->u.in.lock);
177 setup_storage_lists(q, irq_ptr, input_sbal_array, dbf_text, i);
178 input_sbal_array += QDIO_MAX_BUFFERS_PER_Q;
179
180 if (is_thinint_irq(irq_ptr))
181 tasklet_init(&q->tasklet, tiqdio_inbound_processing,
182 (unsigned long) q);
183 else
184 tasklet_init(&q->tasklet, qdio_inbound_processing,
185 (unsigned long) q);
186 }
187
188 for_each_output_queue(irq_ptr, q, i) {
189 sprintf(dbf_text, "outq%4x", i);
190 setup_queues_misc(q, irq_ptr, qdio_init->output_handler, i);
191
192 q->is_input_q = 0;
193 setup_storage_lists(q, irq_ptr, output_sbal_array,
194 dbf_text, i);
195 output_sbal_array += QDIO_MAX_BUFFERS_PER_Q;
196
197 tasklet_init(&q->tasklet, qdio_outbound_processing,
198 (unsigned long) q);
199 setup_timer(&q->u.out.timer, (void(*)(unsigned long))
200 &qdio_outbound_timer, (unsigned long)q);
201 }
202}
203
204static void process_ac_flags(struct qdio_irq *irq_ptr, unsigned char qdioac)
205{
206 if (qdioac & AC1_SIGA_INPUT_NEEDED)
207 irq_ptr->siga_flag.input = 1;
208 if (qdioac & AC1_SIGA_OUTPUT_NEEDED)
209 irq_ptr->siga_flag.output = 1;
210 if (qdioac & AC1_SIGA_SYNC_NEEDED)
211 irq_ptr->siga_flag.sync = 1;
212 if (qdioac & AC1_AUTOMATIC_SYNC_ON_THININT)
213 irq_ptr->siga_flag.no_sync_ti = 1;
214 if (qdioac & AC1_AUTOMATIC_SYNC_ON_OUT_PCI)
215 irq_ptr->siga_flag.no_sync_out_pci = 1;
216
217 if (irq_ptr->siga_flag.no_sync_out_pci &&
218 irq_ptr->siga_flag.no_sync_ti)
219 irq_ptr->siga_flag.no_sync_out_ti = 1;
220}
221
222static void check_and_setup_qebsm(struct qdio_irq *irq_ptr,
223 unsigned char qdioac, unsigned long token)
224{
225 char dbf_text[15];
226
227 if (!(irq_ptr->qib.rflags & QIB_RFLAGS_ENABLE_QEBSM))
228 goto no_qebsm;
229 if (!(qdioac & AC1_SC_QEBSM_AVAILABLE) ||
230 (!(qdioac & AC1_SC_QEBSM_ENABLED)))
231 goto no_qebsm;
232
233 irq_ptr->sch_token = token;
234
235 QDIO_DBF_TEXT0(0, setup, "V=V:1");
236 sprintf(dbf_text, "%8lx", irq_ptr->sch_token);
237 QDIO_DBF_TEXT0(0, setup, dbf_text);
238 return;
239
240no_qebsm:
241 irq_ptr->sch_token = 0;
242 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
243 QDIO_DBF_TEXT0(0, setup, "noV=V");
244}
245
246static int __get_ssqd_info(struct qdio_irq *irq_ptr)
247{
248 struct chsc_ssqd_area *ssqd;
249 int rc;
250
251 QDIO_DBF_TEXT0(0, setup, "getssqd");
252 ssqd = (struct chsc_ssqd_area *)irq_ptr->chsc_page;
253 memset(ssqd, 0, PAGE_SIZE);
254
255 ssqd->request = (struct chsc_header) {
256 .length = 0x0010,
257 .code = 0x0024,
258 };
259 ssqd->first_sch = irq_ptr->schid.sch_no;
260 ssqd->last_sch = irq_ptr->schid.sch_no;
261 ssqd->ssid = irq_ptr->schid.ssid;
262
263 if (chsc(ssqd))
264 return -EIO;
265 rc = chsc_error_from_response(ssqd->response.code);
266 if (rc)
267 return rc;
268
269 if (!(ssqd->qdio_ssqd.flags & CHSC_FLAG_QDIO_CAPABILITY) ||
270 !(ssqd->qdio_ssqd.flags & CHSC_FLAG_VALIDITY) ||
271 (ssqd->qdio_ssqd.sch != irq_ptr->schid.sch_no))
272 return -EINVAL;
273
274 memcpy(&irq_ptr->ssqd_desc, &ssqd->qdio_ssqd,
275 sizeof(struct qdio_ssqd_desc));
276 return 0;
277}
278
279void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr)
280{
281 unsigned char qdioac;
282 char dbf_text[15];
283 int rc;
284
285 rc = __get_ssqd_info(irq_ptr);
286 if (rc) {
287 QDIO_DBF_TEXT2(0, setup, "ssqdasig");
288 sprintf(dbf_text, "schno%x", irq_ptr->schid.sch_no);
289 QDIO_DBF_TEXT2(0, setup, dbf_text);
290 sprintf(dbf_text, "rc:%d", rc);
291 QDIO_DBF_TEXT2(0, setup, dbf_text);
292 /* all flags set, worst case */
293 qdioac = AC1_SIGA_INPUT_NEEDED | AC1_SIGA_OUTPUT_NEEDED |
294 AC1_SIGA_SYNC_NEEDED;
295 } else
296 qdioac = irq_ptr->ssqd_desc.qdioac1;
297
298 check_and_setup_qebsm(irq_ptr, qdioac, irq_ptr->ssqd_desc.sch_token);
299 process_ac_flags(irq_ptr, qdioac);
300
301 sprintf(dbf_text, "qdioac%2x", qdioac);
302 QDIO_DBF_TEXT2(0, setup, dbf_text);
303}
304
305void qdio_release_memory(struct qdio_irq *irq_ptr)
306{
307 struct qdio_q *q;
308 int i;
309
310 /*
311 * Must check queue array manually since irq_ptr->nr_input_queues /
312 * irq_ptr->nr_input_queues may not yet be set.
313 */
314 for (i = 0; i < QDIO_MAX_QUEUES_PER_IRQ; i++) {
315 q = irq_ptr->input_qs[i];
316 if (q) {
317 free_page((unsigned long) q->slib);
318 kmem_cache_free(qdio_q_cache, q);
319 }
320 }
321 for (i = 0; i < QDIO_MAX_QUEUES_PER_IRQ; i++) {
322 q = irq_ptr->output_qs[i];
323 if (q) {
324 free_page((unsigned long) q->slib);
325 kmem_cache_free(qdio_q_cache, q);
326 }
327 }
328 kfree(irq_ptr->qdr);
329 free_page(irq_ptr->chsc_page);
330 free_page((unsigned long) irq_ptr);
331}
332
333static void __qdio_allocate_fill_qdr(struct qdio_irq *irq_ptr,
334 struct qdio_q **irq_ptr_qs,
335 int i, int nr)
336{
337 irq_ptr->qdr->qdf0[i + nr].sliba =
338 (unsigned long)irq_ptr_qs[i]->slib;
339
340 irq_ptr->qdr->qdf0[i + nr].sla =
341 (unsigned long)irq_ptr_qs[i]->sl;
342
343 irq_ptr->qdr->qdf0[i + nr].slsba =
344 (unsigned long)&irq_ptr_qs[i]->slsb.val[0];
345
346 irq_ptr->qdr->qdf0[i + nr].akey = PAGE_DEFAULT_KEY;
347 irq_ptr->qdr->qdf0[i + nr].bkey = PAGE_DEFAULT_KEY;
348 irq_ptr->qdr->qdf0[i + nr].ckey = PAGE_DEFAULT_KEY;
349 irq_ptr->qdr->qdf0[i + nr].dkey = PAGE_DEFAULT_KEY;
350}
351
352static void setup_qdr(struct qdio_irq *irq_ptr,
353 struct qdio_initialize *qdio_init)
354{
355 int i;
356
357 irq_ptr->qdr->qfmt = qdio_init->q_format;
358 irq_ptr->qdr->iqdcnt = qdio_init->no_input_qs;
359 irq_ptr->qdr->oqdcnt = qdio_init->no_output_qs;
360 irq_ptr->qdr->iqdsz = sizeof(struct qdesfmt0) / 4; /* size in words */
361 irq_ptr->qdr->oqdsz = sizeof(struct qdesfmt0) / 4;
362 irq_ptr->qdr->qiba = (unsigned long)&irq_ptr->qib;
363 irq_ptr->qdr->qkey = PAGE_DEFAULT_KEY;
364
365 for (i = 0; i < qdio_init->no_input_qs; i++)
366 __qdio_allocate_fill_qdr(irq_ptr, irq_ptr->input_qs, i, 0);
367
368 for (i = 0; i < qdio_init->no_output_qs; i++)
369 __qdio_allocate_fill_qdr(irq_ptr, irq_ptr->output_qs, i,
370 qdio_init->no_input_qs);
371}
372
373static void setup_qib(struct qdio_irq *irq_ptr,
374 struct qdio_initialize *init_data)
375{
376 if (qebsm_possible())
377 irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM;
378
379 irq_ptr->qib.qfmt = init_data->q_format;
380 if (init_data->no_input_qs)
381 irq_ptr->qib.isliba =
382 (unsigned long)(irq_ptr->input_qs[0]->slib);
383 if (init_data->no_output_qs)
384 irq_ptr->qib.osliba =
385 (unsigned long)(irq_ptr->output_qs[0]->slib);
386 memcpy(irq_ptr->qib.ebcnam, init_data->adapter_name, 8);
387}
388
389int qdio_setup_irq(struct qdio_initialize *init_data)
390{
391 struct ciw *ciw;
392 struct qdio_irq *irq_ptr = init_data->cdev->private->qdio_data;
393 int rc;
394
395 memset(irq_ptr, 0, ((char *)&irq_ptr->qdr) - ((char *)irq_ptr));
396 /* wipes qib.ac, required by ar7063 */
397 memset(irq_ptr->qdr, 0, sizeof(struct qdr));
398
399 irq_ptr->int_parm = init_data->int_parm;
400 irq_ptr->nr_input_qs = init_data->no_input_qs;
401 irq_ptr->nr_output_qs = init_data->no_output_qs;
402
403 irq_ptr->schid = ccw_device_get_subchannel_id(init_data->cdev);
404 irq_ptr->cdev = init_data->cdev;
405 setup_queues(irq_ptr, init_data);
406
407 setup_qib(irq_ptr, init_data);
408 qdio_setup_thinint(irq_ptr);
409 set_impl_params(irq_ptr, init_data->qib_param_field_format,
410 init_data->qib_param_field,
411 init_data->input_slib_elements,
412 init_data->output_slib_elements);
413
414 /* fill input and output descriptors */
415 setup_qdr(irq_ptr, init_data);
416
417 /* qdr, qib, sls, slsbs, slibs, sbales are filled now */
418
419 /* get qdio commands */
420 ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_EQUEUE);
421 if (!ciw) {
422 QDIO_DBF_TEXT2(1, setup, "no eq");
423 rc = -EINVAL;
424 goto out_err;
425 }
426 irq_ptr->equeue = *ciw;
427
428 ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_AQUEUE);
429 if (!ciw) {
430 QDIO_DBF_TEXT2(1, setup, "no aq");
431 rc = -EINVAL;
432 goto out_err;
433 }
434 irq_ptr->aqueue = *ciw;
435
436 /* set new interrupt handler */
437 irq_ptr->orig_handler = init_data->cdev->handler;
438 init_data->cdev->handler = qdio_int_handler;
439 return 0;
440out_err:
441 qdio_release_memory(irq_ptr);
442 return rc;
443}
444
445void qdio_print_subchannel_info(struct qdio_irq *irq_ptr,
446 struct ccw_device *cdev)
447{
448 char s[80];
449
450 sprintf(s, "%s ", cdev->dev.bus_id);
451
452 switch (irq_ptr->qib.qfmt) {
453 case QDIO_QETH_QFMT:
454 sprintf(s + strlen(s), "OSADE ");
455 break;
456 case QDIO_ZFCP_QFMT:
457 sprintf(s + strlen(s), "ZFCP ");
458 break;
459 case QDIO_IQDIO_QFMT:
460 sprintf(s + strlen(s), "HiperSockets ");
461 break;
462 }
463 sprintf(s + strlen(s), "using: ");
464
465 if (!is_thinint_irq(irq_ptr))
466 sprintf(s + strlen(s), "no");
467 sprintf(s + strlen(s), "AdapterInterrupts ");
468 if (!(irq_ptr->sch_token != 0))
469 sprintf(s + strlen(s), "no");
470 sprintf(s + strlen(s), "QEBSM ");
471 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
472 sprintf(s + strlen(s), "no");
473 sprintf(s + strlen(s), "OutboundPCI ");
474 if (!css_general_characteristics.aif_tdd)
475 sprintf(s + strlen(s), "no");
476 sprintf(s + strlen(s), "TDD\n");
477 printk(KERN_INFO "qdio: %s", s);
478
479 memset(s, 0, sizeof(s));
480 sprintf(s, "%s SIGA required: ", cdev->dev.bus_id);
481 if (irq_ptr->siga_flag.input)
482 sprintf(s + strlen(s), "Read ");
483 if (irq_ptr->siga_flag.output)
484 sprintf(s + strlen(s), "Write ");
485 if (irq_ptr->siga_flag.sync)
486 sprintf(s + strlen(s), "Sync ");
487 if (!irq_ptr->siga_flag.no_sync_ti)
488 sprintf(s + strlen(s), "SyncAI ");
489 if (!irq_ptr->siga_flag.no_sync_out_ti)
490 sprintf(s + strlen(s), "SyncOutAI ");
491 if (!irq_ptr->siga_flag.no_sync_out_pci)
492 sprintf(s + strlen(s), "SyncOutPCI");
493 sprintf(s + strlen(s), "\n");
494 printk(KERN_INFO "qdio: %s", s);
495}
496
497int __init qdio_setup_init(void)
498{
499 char dbf_text[15];
500
501 qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q),
502 256, 0, NULL);
503 if (!qdio_q_cache)
504 return -ENOMEM;
505
506 /* Check for OSA/FCP thin interrupts (bit 67). */
507 sprintf(dbf_text, "thini%1x",
508 (css_general_characteristics.aif_osa) ? 1 : 0);
509 QDIO_DBF_TEXT0(0, setup, dbf_text);
510
511 /* Check for QEBSM support in general (bit 58). */
512 sprintf(dbf_text, "cssQBS:%1x",
513 (qebsm_possible()) ? 1 : 0);
514 QDIO_DBF_TEXT0(0, setup, dbf_text);
515 return 0;
516}
517
518void __exit qdio_setup_exit(void)
519{
520 kmem_cache_destroy(qdio_q_cache);
521}
diff --git a/drivers/s390/cio/qdio_thinint.c b/drivers/s390/cio/qdio_thinint.c
new file mode 100644
index 000000000000..9291a771d812
--- /dev/null
+++ b/drivers/s390/cio/qdio_thinint.c
@@ -0,0 +1,380 @@
1/*
2 * linux/drivers/s390/cio/thinint_qdio.c
3 *
4 * thin interrupt support for qdio
5 *
6 * Copyright 2000-2008 IBM Corp.
7 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
8 * Cornelia Huck <cornelia.huck@de.ibm.com>
9 * Jan Glauber <jang@linux.vnet.ibm.com>
10 */
11#include <linux/io.h>
12#include <asm/atomic.h>
13#include <asm/debug.h>
14#include <asm/qdio.h>
15#include <asm/airq.h>
16#include <asm/isc.h>
17
18#include "cio.h"
19#include "ioasm.h"
20#include "qdio.h"
21#include "qdio_debug.h"
22#include "qdio_perf.h"
23
24/*
25 * Restriction: only 63 iqdio subchannels would have its own indicator,
26 * after that, subsequent subchannels share one indicator
27 */
28#define TIQDIO_NR_NONSHARED_IND 63
29#define TIQDIO_NR_INDICATORS (TIQDIO_NR_NONSHARED_IND + 1)
30#define TIQDIO_SHARED_IND 63
31
32/* list of thin interrupt input queues */
33static LIST_HEAD(tiq_list);
34
35/* adapter local summary indicator */
36static unsigned char *tiqdio_alsi;
37
38/* device state change indicators */
39struct indicator_t {
40 u32 ind; /* u32 because of compare-and-swap performance */
41 atomic_t count; /* use count, 0 or 1 for non-shared indicators */
42};
43static struct indicator_t *q_indicators;
44
45static void tiqdio_tasklet_fn(unsigned long data);
46static DECLARE_TASKLET(tiqdio_tasklet, tiqdio_tasklet_fn, 0);
47
48static int css_qdio_omit_svs;
49
50static inline unsigned long do_clear_global_summary(void)
51{
52 register unsigned long __fn asm("1") = 3;
53 register unsigned long __tmp asm("2");
54 register unsigned long __time asm("3");
55
56 asm volatile(
57 " .insn rre,0xb2650000,2,0"
58 : "+d" (__fn), "=d" (__tmp), "=d" (__time));
59 return __time;
60}
61
62/* returns addr for the device state change indicator */
63static u32 *get_indicator(void)
64{
65 int i;
66
67 for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++)
68 if (!atomic_read(&q_indicators[i].count)) {
69 atomic_set(&q_indicators[i].count, 1);
70 return &q_indicators[i].ind;
71 }
72
73 /* use the shared indicator */
74 atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count);
75 return &q_indicators[TIQDIO_SHARED_IND].ind;
76}
77
78static void put_indicator(u32 *addr)
79{
80 int i;
81
82 if (!addr)
83 return;
84 i = ((unsigned long)addr - (unsigned long)q_indicators) /
85 sizeof(struct indicator_t);
86 atomic_dec(&q_indicators[i].count);
87}
88
89void tiqdio_add_input_queues(struct qdio_irq *irq_ptr)
90{
91 struct qdio_q *q;
92 int i;
93
94 /* No TDD facility? If we must use SIGA-s we can also omit SVS. */
95 if (!css_qdio_omit_svs && irq_ptr->siga_flag.sync)
96 css_qdio_omit_svs = 1;
97
98 for_each_input_queue(irq_ptr, q, i) {
99 list_add_rcu(&q->entry, &tiq_list);
100 synchronize_rcu();
101 }
102 xchg(irq_ptr->dsci, 1);
103 tasklet_schedule(&tiqdio_tasklet);
104}
105
106/*
107 * we cannot stop the tiqdio tasklet here since it is for all
108 * thinint qdio devices and it must run as long as there is a
109 * thinint device left
110 */
111void tiqdio_remove_input_queues(struct qdio_irq *irq_ptr)
112{
113 struct qdio_q *q;
114 int i;
115
116 for_each_input_queue(irq_ptr, q, i) {
117 list_del_rcu(&q->entry);
118 synchronize_rcu();
119 }
120}
121
122static inline int tiqdio_inbound_q_done(struct qdio_q *q)
123{
124 unsigned char state;
125
126 if (!atomic_read(&q->nr_buf_used))
127 return 1;
128
129 qdio_siga_sync_q(q);
130 get_buf_state(q, q->first_to_check, &state);
131
132 if (state == SLSB_P_INPUT_PRIMED)
133 /* more work coming */
134 return 0;
135 return 1;
136}
137
138static inline int shared_ind(struct qdio_irq *irq_ptr)
139{
140 return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind;
141}
142
143static void __tiqdio_inbound_processing(struct qdio_q *q)
144{
145 qdio_perf_stat_inc(&perf_stats.thinint_inbound);
146 qdio_sync_after_thinint(q);
147
148 /*
149 * Maybe we have work on our outbound queues... at least
150 * we have to check the PCI capable queues.
151 */
152 qdio_check_outbound_after_thinint(q);
153
154again:
155 if (!qdio_inbound_q_moved(q))
156 return;
157
158 qdio_kick_inbound_handler(q);
159
160 if (!tiqdio_inbound_q_done(q)) {
161 qdio_perf_stat_inc(&perf_stats.thinint_inbound_loop);
162 goto again;
163 }
164
165 qdio_stop_polling(q);
166 /*
167 * We need to check again to not lose initiative after
168 * resetting the ACK state.
169 */
170 if (!tiqdio_inbound_q_done(q)) {
171 qdio_perf_stat_inc(&perf_stats.thinint_inbound_loop2);
172 goto again;
173 }
174}
175
176void tiqdio_inbound_processing(unsigned long data)
177{
178 struct qdio_q *q = (struct qdio_q *)data;
179
180 __tiqdio_inbound_processing(q);
181}
182
183/* check for work on all inbound thinint queues */
184static void tiqdio_tasklet_fn(unsigned long data)
185{
186 struct qdio_q *q;
187
188 qdio_perf_stat_inc(&perf_stats.tasklet_thinint);
189again:
190
191 /* protect tiq_list entries, only changed in activate or shutdown */
192 rcu_read_lock();
193
194 list_for_each_entry_rcu(q, &tiq_list, entry)
195 /* only process queues from changed sets */
196 if (*q->irq_ptr->dsci) {
197
198 /* only clear it if the indicator is non-shared */
199 if (!shared_ind(q->irq_ptr))
200 xchg(q->irq_ptr->dsci, 0);
201 /*
202 * don't call inbound processing directly since
203 * that could starve other thinint queues
204 */
205 tasklet_schedule(&q->tasklet);
206 }
207
208 rcu_read_unlock();
209
210 /*
211 * if we used the shared indicator clear it now after all queues
212 * were processed
213 */
214 if (atomic_read(&q_indicators[TIQDIO_SHARED_IND].count)) {
215 xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0);
216
217 /* prevent racing */
218 if (*tiqdio_alsi)
219 xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 1);
220 }
221
222 /* check for more work */
223 if (*tiqdio_alsi) {
224 xchg(tiqdio_alsi, 0);
225 qdio_perf_stat_inc(&perf_stats.tasklet_thinint_loop);
226 goto again;
227 }
228}
229
230/**
231 * tiqdio_thinint_handler - thin interrupt handler for qdio
232 * @ind: pointer to adapter local summary indicator
233 * @drv_data: NULL
234 */
235static void tiqdio_thinint_handler(void *ind, void *drv_data)
236{
237 qdio_perf_stat_inc(&perf_stats.thin_int);
238
239 /*
240 * SVS only when needed: issue SVS to benefit from iqdio interrupt
241 * avoidance (SVS clears adapter interrupt suppression overwrite)
242 */
243 if (!css_qdio_omit_svs)
244 do_clear_global_summary();
245
246 /*
247 * reset local summary indicator (tiqdio_alsi) to stop adapter
248 * interrupts for now, the tasklet will clean all dsci's
249 */
250 xchg((u8 *)ind, 0);
251 tasklet_hi_schedule(&tiqdio_tasklet);
252}
253
254static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset)
255{
256 struct scssc_area *scssc_area;
257 char dbf_text[15];
258 void *ptr;
259 int rc;
260
261 scssc_area = (struct scssc_area *)irq_ptr->chsc_page;
262 memset(scssc_area, 0, PAGE_SIZE);
263
264 if (reset) {
265 scssc_area->summary_indicator_addr = 0;
266 scssc_area->subchannel_indicator_addr = 0;
267 } else {
268 scssc_area->summary_indicator_addr = virt_to_phys(tiqdio_alsi);
269 scssc_area->subchannel_indicator_addr =
270 virt_to_phys(irq_ptr->dsci);
271 }
272
273 scssc_area->request = (struct chsc_header) {
274 .length = 0x0fe0,
275 .code = 0x0021,
276 };
277 scssc_area->operation_code = 0;
278 scssc_area->ks = PAGE_DEFAULT_KEY;
279 scssc_area->kc = PAGE_DEFAULT_KEY;
280 scssc_area->isc = QDIO_AIRQ_ISC;
281 scssc_area->schid = irq_ptr->schid;
282
283 /* enable the time delay disablement facility */
284 if (css_general_characteristics.aif_tdd)
285 scssc_area->word_with_d_bit = 0x10000000;
286
287 rc = chsc(scssc_area);
288 if (rc)
289 return -EIO;
290
291 rc = chsc_error_from_response(scssc_area->response.code);
292 if (rc) {
293 sprintf(dbf_text, "sidR%4x", scssc_area->response.code);
294 QDIO_DBF_TEXT1(0, trace, dbf_text);
295 QDIO_DBF_TEXT1(0, setup, dbf_text);
296 ptr = &scssc_area->response;
297 QDIO_DBF_HEX2(1, setup, &ptr, QDIO_DBF_SETUP_LEN);
298 return rc;
299 }
300
301 QDIO_DBF_TEXT2(0, setup, "setscind");
302 QDIO_DBF_HEX2(0, setup, &scssc_area->summary_indicator_addr,
303 sizeof(unsigned long));
304 QDIO_DBF_HEX2(0, setup, &scssc_area->subchannel_indicator_addr,
305 sizeof(unsigned long));
306 return 0;
307}
308
309/* allocate non-shared indicators and shared indicator */
310int __init tiqdio_allocate_memory(void)
311{
312 q_indicators = kzalloc(sizeof(struct indicator_t) * TIQDIO_NR_INDICATORS,
313 GFP_KERNEL);
314 if (!q_indicators)
315 return -ENOMEM;
316 return 0;
317}
318
319void tiqdio_free_memory(void)
320{
321 kfree(q_indicators);
322}
323
324int __init tiqdio_register_thinints(void)
325{
326 char dbf_text[20];
327
328 isc_register(QDIO_AIRQ_ISC);
329 tiqdio_alsi = s390_register_adapter_interrupt(&tiqdio_thinint_handler,
330 NULL, QDIO_AIRQ_ISC);
331 if (IS_ERR(tiqdio_alsi)) {
332 sprintf(dbf_text, "regthn%lx", PTR_ERR(tiqdio_alsi));
333 QDIO_DBF_TEXT0(0, setup, dbf_text);
334 tiqdio_alsi = NULL;
335 isc_unregister(QDIO_AIRQ_ISC);
336 return -ENOMEM;
337 }
338 return 0;
339}
340
341int qdio_establish_thinint(struct qdio_irq *irq_ptr)
342{
343 if (!is_thinint_irq(irq_ptr))
344 return 0;
345
346 /* Check for aif time delay disablement. If installed,
347 * omit SVS even under LPAR
348 */
349 if (css_general_characteristics.aif_tdd)
350 css_qdio_omit_svs = 1;
351 return set_subchannel_ind(irq_ptr, 0);
352}
353
354void qdio_setup_thinint(struct qdio_irq *irq_ptr)
355{
356 if (!is_thinint_irq(irq_ptr))
357 return;
358 irq_ptr->dsci = get_indicator();
359 QDIO_DBF_HEX1(0, setup, &irq_ptr->dsci, sizeof(void *));
360}
361
362void qdio_shutdown_thinint(struct qdio_irq *irq_ptr)
363{
364 if (!is_thinint_irq(irq_ptr))
365 return;
366
367 /* reset adapter interrupt indicators */
368 put_indicator(irq_ptr->dsci);
369 set_subchannel_ind(irq_ptr, 1);
370}
371
372void __exit tiqdio_unregister_thinints(void)
373{
374 tasklet_disable(&tiqdio_tasklet);
375
376 if (tiqdio_alsi) {
377 s390_unregister_adapter_interrupt(tiqdio_alsi, QDIO_AIRQ_ISC);
378 isc_unregister(QDIO_AIRQ_ISC);
379 }
380}
diff --git a/drivers/s390/net/qeth_core.h b/drivers/s390/net/qeth_core.h
index 699ac11debd8..1895dbb553cd 100644
--- a/drivers/s390/net/qeth_core.h
+++ b/drivers/s390/net/qeth_core.h
@@ -239,11 +239,6 @@ static inline int qeth_is_ipa_enabled(struct qeth_ipa_info *ipa,
239/*not used unless the microcode gets patched*/ 239/*not used unless the microcode gets patched*/
240#define QETH_PCI_TIMER_VALUE(card) 3 240#define QETH_PCI_TIMER_VALUE(card) 3
241 241
242#define QETH_MIN_INPUT_THRESHOLD 1
243#define QETH_MAX_INPUT_THRESHOLD 500
244#define QETH_MIN_OUTPUT_THRESHOLD 1
245#define QETH_MAX_OUTPUT_THRESHOLD 300
246
247/* priority queing */ 242/* priority queing */
248#define QETH_PRIOQ_DEFAULT QETH_NO_PRIO_QUEUEING 243#define QETH_PRIOQ_DEFAULT QETH_NO_PRIO_QUEUEING
249#define QETH_DEFAULT_QUEUE 2 244#define QETH_DEFAULT_QUEUE 2
@@ -811,17 +806,14 @@ int qeth_send_ipa_cmd(struct qeth_card *, struct qeth_cmd_buffer *,
811struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *, 806struct qeth_cmd_buffer *qeth_get_ipacmd_buffer(struct qeth_card *,
812 enum qeth_ipa_cmds, enum qeth_prot_versions); 807 enum qeth_ipa_cmds, enum qeth_prot_versions);
813int qeth_query_setadapterparms(struct qeth_card *); 808int qeth_query_setadapterparms(struct qeth_card *);
814int qeth_check_qdio_errors(struct qdio_buffer *, unsigned int, 809int qeth_check_qdio_errors(struct qdio_buffer *, unsigned int, const char *);
815 unsigned int, const char *);
816void qeth_queue_input_buffer(struct qeth_card *, int); 810void qeth_queue_input_buffer(struct qeth_card *, int);
817struct sk_buff *qeth_core_get_next_skb(struct qeth_card *, 811struct sk_buff *qeth_core_get_next_skb(struct qeth_card *,
818 struct qdio_buffer *, struct qdio_buffer_element **, int *, 812 struct qdio_buffer *, struct qdio_buffer_element **, int *,
819 struct qeth_hdr **); 813 struct qeth_hdr **);
820void qeth_schedule_recovery(struct qeth_card *); 814void qeth_schedule_recovery(struct qeth_card *);
821void qeth_qdio_output_handler(struct ccw_device *, unsigned int, 815void qeth_qdio_output_handler(struct ccw_device *, unsigned int,
822 unsigned int, unsigned int, 816 int, int, int, unsigned long);
823 unsigned int, int, int,
824 unsigned long);
825void qeth_clear_ipacmd_list(struct qeth_card *); 817void qeth_clear_ipacmd_list(struct qeth_card *);
826int qeth_qdio_clear_card(struct qeth_card *, int); 818int qeth_qdio_clear_card(struct qeth_card *, int);
827void qeth_clear_working_pool_list(struct qeth_card *); 819void qeth_clear_working_pool_list(struct qeth_card *);
diff --git a/drivers/s390/net/qeth_core_main.c b/drivers/s390/net/qeth_core_main.c
index 0ac54dc638c2..c3ad89e302bd 100644
--- a/drivers/s390/net/qeth_core_main.c
+++ b/drivers/s390/net/qeth_core_main.c
@@ -2073,7 +2073,7 @@ static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2073static int qeth_qdio_activate(struct qeth_card *card) 2073static int qeth_qdio_activate(struct qeth_card *card)
2074{ 2074{
2075 QETH_DBF_TEXT(SETUP, 3, "qdioact"); 2075 QETH_DBF_TEXT(SETUP, 3, "qdioact");
2076 return qdio_activate(CARD_DDEV(card), 0); 2076 return qdio_activate(CARD_DDEV(card));
2077} 2077}
2078 2078
2079static int qeth_dm_act(struct qeth_card *card) 2079static int qeth_dm_act(struct qeth_card *card)
@@ -2349,16 +2349,11 @@ int qeth_init_qdio_queues(struct qeth_card *card)
2349 card->qdio.in_q->next_buf_to_init = 2349 card->qdio.in_q->next_buf_to_init =
2350 card->qdio.in_buf_pool.buf_count - 1; 2350 card->qdio.in_buf_pool.buf_count - 1;
2351 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0, 2351 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2352 card->qdio.in_buf_pool.buf_count - 1, NULL); 2352 card->qdio.in_buf_pool.buf_count - 1);
2353 if (rc) { 2353 if (rc) {
2354 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc); 2354 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
2355 return rc; 2355 return rc;
2356 } 2356 }
2357 rc = qdio_synchronize(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0);
2358 if (rc) {
2359 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
2360 return rc;
2361 }
2362 /* outbound queue */ 2357 /* outbound queue */
2363 for (i = 0; i < card->qdio.no_out_queues; ++i) { 2358 for (i = 0; i < card->qdio.no_out_queues; ++i) {
2364 memset(card->qdio.out_qs[i]->qdio_bufs, 0, 2359 memset(card->qdio.out_qs[i]->qdio_bufs, 0,
@@ -2559,9 +2554,9 @@ int qeth_query_setadapterparms(struct qeth_card *card)
2559EXPORT_SYMBOL_GPL(qeth_query_setadapterparms); 2554EXPORT_SYMBOL_GPL(qeth_query_setadapterparms);
2560 2555
2561int qeth_check_qdio_errors(struct qdio_buffer *buf, unsigned int qdio_error, 2556int qeth_check_qdio_errors(struct qdio_buffer *buf, unsigned int qdio_error,
2562 unsigned int siga_error, const char *dbftext) 2557 const char *dbftext)
2563{ 2558{
2564 if (qdio_error || siga_error) { 2559 if (qdio_error) {
2565 QETH_DBF_TEXT(TRACE, 2, dbftext); 2560 QETH_DBF_TEXT(TRACE, 2, dbftext);
2566 QETH_DBF_TEXT(QERR, 2, dbftext); 2561 QETH_DBF_TEXT(QERR, 2, dbftext);
2567 QETH_DBF_TEXT_(QERR, 2, " F15=%02X", 2562 QETH_DBF_TEXT_(QERR, 2, " F15=%02X",
@@ -2569,7 +2564,6 @@ int qeth_check_qdio_errors(struct qdio_buffer *buf, unsigned int qdio_error,
2569 QETH_DBF_TEXT_(QERR, 2, " F14=%02X", 2564 QETH_DBF_TEXT_(QERR, 2, " F14=%02X",
2570 buf->element[14].flags & 0xff); 2565 buf->element[14].flags & 0xff);
2571 QETH_DBF_TEXT_(QERR, 2, " qerr=%X", qdio_error); 2566 QETH_DBF_TEXT_(QERR, 2, " qerr=%X", qdio_error);
2572 QETH_DBF_TEXT_(QERR, 2, " serr=%X", siga_error);
2573 return 1; 2567 return 1;
2574 } 2568 }
2575 return 0; 2569 return 0;
@@ -2622,9 +2616,8 @@ void qeth_queue_input_buffer(struct qeth_card *card, int index)
2622 card->perf_stats.inbound_do_qdio_start_time = 2616 card->perf_stats.inbound_do_qdio_start_time =
2623 qeth_get_micros(); 2617 qeth_get_micros();
2624 } 2618 }
2625 rc = do_QDIO(CARD_DDEV(card), 2619 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
2626 QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT, 2620 queue->next_buf_to_init, count);
2627 0, queue->next_buf_to_init, count, NULL);
2628 if (card->options.performance_stats) 2621 if (card->options.performance_stats)
2629 card->perf_stats.inbound_do_qdio_time += 2622 card->perf_stats.inbound_do_qdio_time +=
2630 qeth_get_micros() - 2623 qeth_get_micros() -
@@ -2643,14 +2636,13 @@ void qeth_queue_input_buffer(struct qeth_card *card, int index)
2643EXPORT_SYMBOL_GPL(qeth_queue_input_buffer); 2636EXPORT_SYMBOL_GPL(qeth_queue_input_buffer);
2644 2637
2645static int qeth_handle_send_error(struct qeth_card *card, 2638static int qeth_handle_send_error(struct qeth_card *card,
2646 struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err, 2639 struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
2647 unsigned int siga_err)
2648{ 2640{
2649 int sbalf15 = buffer->buffer->element[15].flags & 0xff; 2641 int sbalf15 = buffer->buffer->element[15].flags & 0xff;
2650 int cc = siga_err & 3; 2642 int cc = qdio_err & 3;
2651 2643
2652 QETH_DBF_TEXT(TRACE, 6, "hdsnderr"); 2644 QETH_DBF_TEXT(TRACE, 6, "hdsnderr");
2653 qeth_check_qdio_errors(buffer->buffer, qdio_err, siga_err, "qouterr"); 2645 qeth_check_qdio_errors(buffer->buffer, qdio_err, "qouterr");
2654 switch (cc) { 2646 switch (cc) {
2655 case 0: 2647 case 0:
2656 if (qdio_err) { 2648 if (qdio_err) {
@@ -2662,7 +2654,7 @@ static int qeth_handle_send_error(struct qeth_card *card,
2662 } 2654 }
2663 return QETH_SEND_ERROR_NONE; 2655 return QETH_SEND_ERROR_NONE;
2664 case 2: 2656 case 2:
2665 if (siga_err & QDIO_SIGA_ERROR_B_BIT_SET) { 2657 if (qdio_err & QDIO_ERROR_SIGA_BUSY) {
2666 QETH_DBF_TEXT(TRACE, 1, "SIGAcc2B"); 2658 QETH_DBF_TEXT(TRACE, 1, "SIGAcc2B");
2667 QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card)); 2659 QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card));
2668 return QETH_SEND_ERROR_KICK_IT; 2660 return QETH_SEND_ERROR_KICK_IT;
@@ -2758,8 +2750,8 @@ static int qeth_flush_buffers_on_no_pci(struct qeth_qdio_out_q *queue)
2758 return 0; 2750 return 0;
2759} 2751}
2760 2752
2761static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int under_int, 2753static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
2762 int index, int count) 2754 int count)
2763{ 2755{
2764 struct qeth_qdio_out_buffer *buf; 2756 struct qeth_qdio_out_buffer *buf;
2765 int rc; 2757 int rc;
@@ -2807,12 +2799,10 @@ static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int under_int,
2807 qeth_get_micros(); 2799 qeth_get_micros();
2808 } 2800 }
2809 qdio_flags = QDIO_FLAG_SYNC_OUTPUT; 2801 qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
2810 if (under_int)
2811 qdio_flags |= QDIO_FLAG_UNDER_INTERRUPT;
2812 if (atomic_read(&queue->set_pci_flags_count)) 2802 if (atomic_read(&queue->set_pci_flags_count))
2813 qdio_flags |= QDIO_FLAG_PCI_OUT; 2803 qdio_flags |= QDIO_FLAG_PCI_OUT;
2814 rc = do_QDIO(CARD_DDEV(queue->card), qdio_flags, 2804 rc = do_QDIO(CARD_DDEV(queue->card), qdio_flags,
2815 queue->queue_no, index, count, NULL); 2805 queue->queue_no, index, count);
2816 if (queue->card->options.performance_stats) 2806 if (queue->card->options.performance_stats)
2817 queue->card->perf_stats.outbound_do_qdio_time += 2807 queue->card->perf_stats.outbound_do_qdio_time +=
2818 qeth_get_micros() - 2808 qeth_get_micros() -
@@ -2866,16 +2856,15 @@ static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
2866 queue->card->perf_stats.bufs_sent_pack += 2856 queue->card->perf_stats.bufs_sent_pack +=
2867 flush_cnt; 2857 flush_cnt;
2868 if (flush_cnt) 2858 if (flush_cnt)
2869 qeth_flush_buffers(queue, 1, index, flush_cnt); 2859 qeth_flush_buffers(queue, index, flush_cnt);
2870 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED); 2860 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
2871 } 2861 }
2872 } 2862 }
2873} 2863}
2874 2864
2875void qeth_qdio_output_handler(struct ccw_device *ccwdev, unsigned int status, 2865void qeth_qdio_output_handler(struct ccw_device *ccwdev,
2876 unsigned int qdio_error, unsigned int siga_error, 2866 unsigned int qdio_error, int __queue, int first_element,
2877 unsigned int __queue, int first_element, int count, 2867 int count, unsigned long card_ptr)
2878 unsigned long card_ptr)
2879{ 2868{
2880 struct qeth_card *card = (struct qeth_card *) card_ptr; 2869 struct qeth_card *card = (struct qeth_card *) card_ptr;
2881 struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue]; 2870 struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
@@ -2883,15 +2872,12 @@ void qeth_qdio_output_handler(struct ccw_device *ccwdev, unsigned int status,
2883 int i; 2872 int i;
2884 2873
2885 QETH_DBF_TEXT(TRACE, 6, "qdouhdl"); 2874 QETH_DBF_TEXT(TRACE, 6, "qdouhdl");
2886 if (status & QDIO_STATUS_LOOK_FOR_ERROR) { 2875 if (qdio_error & QDIO_ERROR_ACTIVATE_CHECK_CONDITION) {
2887 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION) { 2876 QETH_DBF_TEXT(TRACE, 2, "achkcond");
2888 QETH_DBF_TEXT(TRACE, 2, "achkcond"); 2877 QETH_DBF_TEXT_(TRACE, 2, "%s", CARD_BUS_ID(card));
2889 QETH_DBF_TEXT_(TRACE, 2, "%s", CARD_BUS_ID(card)); 2878 netif_stop_queue(card->dev);
2890 QETH_DBF_TEXT_(TRACE, 2, "%08x", status); 2879 qeth_schedule_recovery(card);
2891 netif_stop_queue(card->dev); 2880 return;
2892 qeth_schedule_recovery(card);
2893 return;
2894 }
2895 } 2881 }
2896 if (card->options.performance_stats) { 2882 if (card->options.performance_stats) {
2897 card->perf_stats.outbound_handler_cnt++; 2883 card->perf_stats.outbound_handler_cnt++;
@@ -2901,8 +2887,7 @@ void qeth_qdio_output_handler(struct ccw_device *ccwdev, unsigned int status,
2901 for (i = first_element; i < (first_element + count); ++i) { 2887 for (i = first_element; i < (first_element + count); ++i) {
2902 buffer = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q]; 2888 buffer = &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q];
2903 /*we only handle the KICK_IT error by doing a recovery */ 2889 /*we only handle the KICK_IT error by doing a recovery */
2904 if (qeth_handle_send_error(card, buffer, 2890 if (qeth_handle_send_error(card, buffer, qdio_error)
2905 qdio_error, siga_error)
2906 == QETH_SEND_ERROR_KICK_IT){ 2891 == QETH_SEND_ERROR_KICK_IT){
2907 netif_stop_queue(card->dev); 2892 netif_stop_queue(card->dev);
2908 qeth_schedule_recovery(card); 2893 qeth_schedule_recovery(card);
@@ -3164,11 +3149,11 @@ int qeth_do_send_packet_fast(struct qeth_card *card,
3164 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED); 3149 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3165 if (ctx == NULL) { 3150 if (ctx == NULL) {
3166 qeth_fill_buffer(queue, buffer, skb); 3151 qeth_fill_buffer(queue, buffer, skb);
3167 qeth_flush_buffers(queue, 0, index, 1); 3152 qeth_flush_buffers(queue, index, 1);
3168 } else { 3153 } else {
3169 flush_cnt = qeth_eddp_fill_buffer(queue, ctx, index); 3154 flush_cnt = qeth_eddp_fill_buffer(queue, ctx, index);
3170 WARN_ON(buffers_needed != flush_cnt); 3155 WARN_ON(buffers_needed != flush_cnt);
3171 qeth_flush_buffers(queue, 0, index, flush_cnt); 3156 qeth_flush_buffers(queue, index, flush_cnt);
3172 } 3157 }
3173 return 0; 3158 return 0;
3174out: 3159out:
@@ -3221,8 +3206,8 @@ int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
3221 * again */ 3206 * again */
3222 if (atomic_read(&buffer->state) != 3207 if (atomic_read(&buffer->state) !=
3223 QETH_QDIO_BUF_EMPTY){ 3208 QETH_QDIO_BUF_EMPTY){
3224 qeth_flush_buffers(queue, 0, 3209 qeth_flush_buffers(queue, start_index,
3225 start_index, flush_count); 3210 flush_count);
3226 atomic_set(&queue->state, 3211 atomic_set(&queue->state,
3227 QETH_OUT_Q_UNLOCKED); 3212 QETH_OUT_Q_UNLOCKED);
3228 return -EBUSY; 3213 return -EBUSY;
@@ -3253,7 +3238,7 @@ int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
3253 flush_count += tmp; 3238 flush_count += tmp;
3254out: 3239out:
3255 if (flush_count) 3240 if (flush_count)
3256 qeth_flush_buffers(queue, 0, start_index, flush_count); 3241 qeth_flush_buffers(queue, start_index, flush_count);
3257 else if (!atomic_read(&queue->set_pci_flags_count)) 3242 else if (!atomic_read(&queue->set_pci_flags_count))
3258 atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH); 3243 atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH);
3259 /* 3244 /*
@@ -3274,7 +3259,7 @@ out:
3274 if (!flush_count && !atomic_read(&queue->set_pci_flags_count)) 3259 if (!flush_count && !atomic_read(&queue->set_pci_flags_count))
3275 flush_count += qeth_flush_buffers_on_no_pci(queue); 3260 flush_count += qeth_flush_buffers_on_no_pci(queue);
3276 if (flush_count) 3261 if (flush_count)
3277 qeth_flush_buffers(queue, 0, start_index, flush_count); 3262 qeth_flush_buffers(queue, start_index, flush_count);
3278 } 3263 }
3279 /* at this point the queue is UNLOCKED again */ 3264 /* at this point the queue is UNLOCKED again */
3280 if (queue->card->options.performance_stats && do_pack) 3265 if (queue->card->options.performance_stats && do_pack)
@@ -3686,10 +3671,6 @@ static int qeth_qdio_establish(struct qeth_card *card)
3686 init_data.q_format = qeth_get_qdio_q_format(card); 3671 init_data.q_format = qeth_get_qdio_q_format(card);
3687 init_data.qib_param_field_format = 0; 3672 init_data.qib_param_field_format = 0;
3688 init_data.qib_param_field = qib_param_field; 3673 init_data.qib_param_field = qib_param_field;
3689 init_data.min_input_threshold = QETH_MIN_INPUT_THRESHOLD;
3690 init_data.max_input_threshold = QETH_MAX_INPUT_THRESHOLD;
3691 init_data.min_output_threshold = QETH_MIN_OUTPUT_THRESHOLD;
3692 init_data.max_output_threshold = QETH_MAX_OUTPUT_THRESHOLD;
3693 init_data.no_input_qs = 1; 3674 init_data.no_input_qs = 1;
3694 init_data.no_output_qs = card->qdio.no_out_queues; 3675 init_data.no_output_qs = card->qdio.no_out_queues;
3695 init_data.input_handler = card->discipline.input_handler; 3676 init_data.input_handler = card->discipline.input_handler;
@@ -3751,8 +3732,9 @@ static int qeth_core_driver_group(const char *buf, struct device *root_dev,
3751 3732
3752int qeth_core_hardsetup_card(struct qeth_card *card) 3733int qeth_core_hardsetup_card(struct qeth_card *card)
3753{ 3734{
3735 struct qdio_ssqd_desc *qdio_ssqd;
3754 int retries = 3; 3736 int retries = 3;
3755 int mpno; 3737 int mpno = 0;
3756 int rc; 3738 int rc;
3757 3739
3758 QETH_DBF_TEXT(SETUP, 2, "hrdsetup"); 3740 QETH_DBF_TEXT(SETUP, 2, "hrdsetup");
@@ -3784,7 +3766,10 @@ retry:
3784 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc); 3766 QETH_DBF_TEXT_(SETUP, 2, "2err%d", rc);
3785 return rc; 3767 return rc;
3786 } 3768 }
3787 mpno = qdio_get_ssqd_pct(CARD_DDEV(card)); 3769
3770 qdio_ssqd = qdio_get_ssqd_desc(CARD_DDEV(card));
3771 if (qdio_ssqd)
3772 mpno = qdio_ssqd->pcnt;
3788 if (mpno) 3773 if (mpno)
3789 mpno = min(mpno - 1, QETH_MAX_PORTNO); 3774 mpno = min(mpno - 1, QETH_MAX_PORTNO);
3790 if (card->info.portno > mpno) { 3775 if (card->info.portno > mpno) {
diff --git a/drivers/s390/net/qeth_l2_main.c b/drivers/s390/net/qeth_l2_main.c
index f682f7b14480..3fbc3bdec0c5 100644
--- a/drivers/s390/net/qeth_l2_main.c
+++ b/drivers/s390/net/qeth_l2_main.c
@@ -726,8 +726,7 @@ tx_drop:
726} 726}
727 727
728static void qeth_l2_qdio_input_handler(struct ccw_device *ccwdev, 728static void qeth_l2_qdio_input_handler(struct ccw_device *ccwdev,
729 unsigned int status, unsigned int qdio_err, 729 unsigned int qdio_err, unsigned int queue,
730 unsigned int siga_err, unsigned int queue,
731 int first_element, int count, unsigned long card_ptr) 730 int first_element, int count, unsigned long card_ptr)
732{ 731{
733 struct net_device *net_dev; 732 struct net_device *net_dev;
@@ -742,23 +741,20 @@ static void qeth_l2_qdio_input_handler(struct ccw_device *ccwdev,
742 card->perf_stats.inbound_cnt++; 741 card->perf_stats.inbound_cnt++;
743 card->perf_stats.inbound_start_time = qeth_get_micros(); 742 card->perf_stats.inbound_start_time = qeth_get_micros();
744 } 743 }
745 if (status & QDIO_STATUS_LOOK_FOR_ERROR) { 744 if (qdio_err & QDIO_ERROR_ACTIVATE_CHECK_CONDITION) {
746 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION) { 745 QETH_DBF_TEXT(TRACE, 1, "qdinchk");
747 QETH_DBF_TEXT(TRACE, 1, "qdinchk"); 746 QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card));
748 QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card)); 747 QETH_DBF_TEXT_(TRACE, 1, "%04X%04X", first_element,
749 QETH_DBF_TEXT_(TRACE, 1, "%04X%04X", first_element, 748 count);
750 count); 749 QETH_DBF_TEXT_(TRACE, 1, "%04X", queue);
751 QETH_DBF_TEXT_(TRACE, 1, "%04X%04X", queue, status); 750 qeth_schedule_recovery(card);
752 qeth_schedule_recovery(card); 751 return;
753 return;
754 }
755 } 752 }
756 for (i = first_element; i < (first_element + count); ++i) { 753 for (i = first_element; i < (first_element + count); ++i) {
757 index = i % QDIO_MAX_BUFFERS_PER_Q; 754 index = i % QDIO_MAX_BUFFERS_PER_Q;
758 buffer = &card->qdio.in_q->bufs[index]; 755 buffer = &card->qdio.in_q->bufs[index];
759 if (!((status & QDIO_STATUS_LOOK_FOR_ERROR) && 756 if (!(qdio_err &&
760 qeth_check_qdio_errors(buffer->buffer, 757 qeth_check_qdio_errors(buffer->buffer, qdio_err, "qinerr")))
761 qdio_err, siga_err, "qinerr")))
762 qeth_l2_process_inbound_buffer(card, buffer, index); 758 qeth_l2_process_inbound_buffer(card, buffer, index);
763 /* clear buffer and give back to hardware */ 759 /* clear buffer and give back to hardware */
764 qeth_put_buffer_pool_entry(card, buffer->pool_entry); 760 qeth_put_buffer_pool_entry(card, buffer->pool_entry);
diff --git a/drivers/s390/net/qeth_l3_main.c b/drivers/s390/net/qeth_l3_main.c
index 06deaee50f6d..22f64aa6dd1f 100644
--- a/drivers/s390/net/qeth_l3_main.c
+++ b/drivers/s390/net/qeth_l3_main.c
@@ -2939,8 +2939,7 @@ static int qeth_l3_setup_netdev(struct qeth_card *card)
2939} 2939}
2940 2940
2941static void qeth_l3_qdio_input_handler(struct ccw_device *ccwdev, 2941static void qeth_l3_qdio_input_handler(struct ccw_device *ccwdev,
2942 unsigned int status, unsigned int qdio_err, 2942 unsigned int qdio_err, unsigned int queue, int first_element,
2943 unsigned int siga_err, unsigned int queue, int first_element,
2944 int count, unsigned long card_ptr) 2943 int count, unsigned long card_ptr)
2945{ 2944{
2946 struct net_device *net_dev; 2945 struct net_device *net_dev;
@@ -2955,23 +2954,21 @@ static void qeth_l3_qdio_input_handler(struct ccw_device *ccwdev,
2955 card->perf_stats.inbound_cnt++; 2954 card->perf_stats.inbound_cnt++;
2956 card->perf_stats.inbound_start_time = qeth_get_micros(); 2955 card->perf_stats.inbound_start_time = qeth_get_micros();
2957 } 2956 }
2958 if (status & QDIO_STATUS_LOOK_FOR_ERROR) { 2957 if (qdio_err & QDIO_ERROR_ACTIVATE_CHECK_CONDITION) {
2959 if (status & QDIO_STATUS_ACTIVATE_CHECK_CONDITION) { 2958 QETH_DBF_TEXT(TRACE, 1, "qdinchk");
2960 QETH_DBF_TEXT(TRACE, 1, "qdinchk"); 2959 QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card));
2961 QETH_DBF_TEXT_(TRACE, 1, "%s", CARD_BUS_ID(card)); 2960 QETH_DBF_TEXT_(TRACE, 1, "%04X%04X",
2962 QETH_DBF_TEXT_(TRACE, 1, "%04X%04X", 2961 first_element, count);
2963 first_element, count); 2962 QETH_DBF_TEXT_(TRACE, 1, "%04X", queue);
2964 QETH_DBF_TEXT_(TRACE, 1, "%04X%04X", queue, status); 2963 qeth_schedule_recovery(card);
2965 qeth_schedule_recovery(card); 2964 return;
2966 return;
2967 }
2968 } 2965 }
2969 for (i = first_element; i < (first_element + count); ++i) { 2966 for (i = first_element; i < (first_element + count); ++i) {
2970 index = i % QDIO_MAX_BUFFERS_PER_Q; 2967 index = i % QDIO_MAX_BUFFERS_PER_Q;
2971 buffer = &card->qdio.in_q->bufs[index]; 2968 buffer = &card->qdio.in_q->bufs[index];
2972 if (!((status & QDIO_STATUS_LOOK_FOR_ERROR) && 2969 if (!(qdio_err &&
2973 qeth_check_qdio_errors(buffer->buffer, 2970 qeth_check_qdio_errors(buffer->buffer,
2974 qdio_err, siga_err, "qinerr"))) 2971 qdio_err, "qinerr")))
2975 qeth_l3_process_inbound_buffer(card, buffer, index); 2972 qeth_l3_process_inbound_buffer(card, buffer, index);
2976 /* clear buffer and give back to hardware */ 2973 /* clear buffer and give back to hardware */
2977 qeth_put_buffer_pool_entry(card, buffer->pool_entry); 2974 qeth_put_buffer_pool_entry(card, buffer->pool_entry);
diff --git a/drivers/s390/scsi/zfcp_dbf.c b/drivers/s390/scsi/zfcp_dbf.c
index 36169c6944fd..fca48b88fc53 100644
--- a/drivers/s390/scsi/zfcp_dbf.c
+++ b/drivers/s390/scsi/zfcp_dbf.c
@@ -297,15 +297,13 @@ void zfcp_hba_dbf_event_fsf_unsol(const char *tag, struct zfcp_adapter *adapter,
297/** 297/**
298 * zfcp_hba_dbf_event_qdio - trace event for QDIO related failure 298 * zfcp_hba_dbf_event_qdio - trace event for QDIO related failure
299 * @adapter: adapter affected by this QDIO related event 299 * @adapter: adapter affected by this QDIO related event
300 * @status: as passed by qdio module
301 * @qdio_error: as passed by qdio module 300 * @qdio_error: as passed by qdio module
302 * @siga_error: as passed by qdio module
303 * @sbal_index: first buffer with error condition, as passed by qdio module 301 * @sbal_index: first buffer with error condition, as passed by qdio module
304 * @sbal_count: number of buffers affected, as passed by qdio module 302 * @sbal_count: number of buffers affected, as passed by qdio module
305 */ 303 */
306void zfcp_hba_dbf_event_qdio(struct zfcp_adapter *adapter, unsigned int status, 304void zfcp_hba_dbf_event_qdio(struct zfcp_adapter *adapter,
307 unsigned int qdio_error, unsigned int siga_error, 305 unsigned int qdio_error, int sbal_index,
308 int sbal_index, int sbal_count) 306 int sbal_count)
309{ 307{
310 struct zfcp_hba_dbf_record *r = &adapter->hba_dbf_buf; 308 struct zfcp_hba_dbf_record *r = &adapter->hba_dbf_buf;
311 unsigned long flags; 309 unsigned long flags;
@@ -313,9 +311,7 @@ void zfcp_hba_dbf_event_qdio(struct zfcp_adapter *adapter, unsigned int status,
313 spin_lock_irqsave(&adapter->hba_dbf_lock, flags); 311 spin_lock_irqsave(&adapter->hba_dbf_lock, flags);
314 memset(r, 0, sizeof(*r)); 312 memset(r, 0, sizeof(*r));
315 strncpy(r->tag, "qdio", ZFCP_DBF_TAG_SIZE); 313 strncpy(r->tag, "qdio", ZFCP_DBF_TAG_SIZE);
316 r->u.qdio.status = status;
317 r->u.qdio.qdio_error = qdio_error; 314 r->u.qdio.qdio_error = qdio_error;
318 r->u.qdio.siga_error = siga_error;
319 r->u.qdio.sbal_index = sbal_index; 315 r->u.qdio.sbal_index = sbal_index;
320 r->u.qdio.sbal_count = sbal_count; 316 r->u.qdio.sbal_count = sbal_count;
321 debug_event(adapter->hba_dbf, 0, r, sizeof(*r)); 317 debug_event(adapter->hba_dbf, 0, r, sizeof(*r));
@@ -398,9 +394,7 @@ static void zfcp_hba_dbf_view_status(char **p,
398 394
399static void zfcp_hba_dbf_view_qdio(char **p, struct zfcp_hba_dbf_record_qdio *r) 395static void zfcp_hba_dbf_view_qdio(char **p, struct zfcp_hba_dbf_record_qdio *r)
400{ 396{
401 zfcp_dbf_out(p, "status", "0x%08x", r->status);
402 zfcp_dbf_out(p, "qdio_error", "0x%08x", r->qdio_error); 397 zfcp_dbf_out(p, "qdio_error", "0x%08x", r->qdio_error);
403 zfcp_dbf_out(p, "siga_error", "0x%08x", r->siga_error);
404 zfcp_dbf_out(p, "sbal_index", "0x%02x", r->sbal_index); 398 zfcp_dbf_out(p, "sbal_index", "0x%02x", r->sbal_index);
405 zfcp_dbf_out(p, "sbal_count", "0x%02x", r->sbal_count); 399 zfcp_dbf_out(p, "sbal_count", "0x%02x", r->sbal_count);
406} 400}
diff --git a/drivers/s390/scsi/zfcp_dbf.h b/drivers/s390/scsi/zfcp_dbf.h
index d04aea604974..0ddb18449d11 100644
--- a/drivers/s390/scsi/zfcp_dbf.h
+++ b/drivers/s390/scsi/zfcp_dbf.h
@@ -139,9 +139,7 @@ struct zfcp_hba_dbf_record_status {
139} __attribute__ ((packed)); 139} __attribute__ ((packed));
140 140
141struct zfcp_hba_dbf_record_qdio { 141struct zfcp_hba_dbf_record_qdio {
142 u32 status;
143 u32 qdio_error; 142 u32 qdio_error;
144 u32 siga_error;
145 u8 sbal_index; 143 u8 sbal_index;
146 u8 sbal_count; 144 u8 sbal_count;
147} __attribute__ ((packed)); 145} __attribute__ ((packed));
diff --git a/drivers/s390/scsi/zfcp_ext.h b/drivers/s390/scsi/zfcp_ext.h
index 8065b2b224b7..edfdb21591f3 100644
--- a/drivers/s390/scsi/zfcp_ext.h
+++ b/drivers/s390/scsi/zfcp_ext.h
@@ -48,9 +48,8 @@ extern void zfcp_rec_dbf_event_action(u8, struct zfcp_erp_action *);
48extern void zfcp_hba_dbf_event_fsf_response(struct zfcp_fsf_req *); 48extern void zfcp_hba_dbf_event_fsf_response(struct zfcp_fsf_req *);
49extern void zfcp_hba_dbf_event_fsf_unsol(const char *, struct zfcp_adapter *, 49extern void zfcp_hba_dbf_event_fsf_unsol(const char *, struct zfcp_adapter *,
50 struct fsf_status_read_buffer *); 50 struct fsf_status_read_buffer *);
51extern void zfcp_hba_dbf_event_qdio(struct zfcp_adapter *, 51extern void zfcp_hba_dbf_event_qdio(struct zfcp_adapter *, unsigned int, int,
52 unsigned int, unsigned int, unsigned int, 52 int);
53 int, int);
54extern void zfcp_san_dbf_event_ct_request(struct zfcp_fsf_req *); 53extern void zfcp_san_dbf_event_ct_request(struct zfcp_fsf_req *);
55extern void zfcp_san_dbf_event_ct_response(struct zfcp_fsf_req *); 54extern void zfcp_san_dbf_event_ct_response(struct zfcp_fsf_req *);
56extern void zfcp_san_dbf_event_els_request(struct zfcp_fsf_req *); 55extern void zfcp_san_dbf_event_els_request(struct zfcp_fsf_req *);
diff --git a/drivers/s390/scsi/zfcp_qdio.c b/drivers/s390/scsi/zfcp_qdio.c
index 72e3094796d4..d6dbd653fde9 100644
--- a/drivers/s390/scsi/zfcp_qdio.c
+++ b/drivers/s390/scsi/zfcp_qdio.c
@@ -74,17 +74,15 @@ static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
74 } 74 }
75} 75}
76 76
77static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int status, 77static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
78 unsigned int qdio_err, unsigned int siga_err, 78 int queue_no, int first, int count,
79 unsigned int queue_no, int first, int count,
80 unsigned long parm) 79 unsigned long parm)
81{ 80{
82 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm; 81 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
83 struct zfcp_qdio_queue *queue = &adapter->req_q; 82 struct zfcp_qdio_queue *queue = &adapter->req_q;
84 83
85 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) { 84 if (unlikely(qdio_err)) {
86 zfcp_hba_dbf_event_qdio(adapter, status, qdio_err, siga_err, 85 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
87 first, count);
88 zfcp_qdio_handler_error(adapter, 140); 86 zfcp_qdio_handler_error(adapter, 140);
89 return; 87 return;
90 } 88 }
@@ -129,8 +127,7 @@ static void zfcp_qdio_resp_put_back(struct zfcp_adapter *adapter, int processed)
129 127
130 count = atomic_read(&queue->count) + processed; 128 count = atomic_read(&queue->count) + processed;
131 129
132 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT | QDIO_FLAG_UNDER_INTERRUPT, 130 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
133 0, start, count, NULL);
134 131
135 if (unlikely(retval)) { 132 if (unlikely(retval)) {
136 atomic_set(&queue->count, count); 133 atomic_set(&queue->count, count);
@@ -142,9 +139,8 @@ static void zfcp_qdio_resp_put_back(struct zfcp_adapter *adapter, int processed)
142 } 139 }
143} 140}
144 141
145static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int status, 142static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
146 unsigned int qdio_err, unsigned int siga_err, 143 int queue_no, int first, int count,
147 unsigned int queue_no, int first, int count,
148 unsigned long parm) 144 unsigned long parm)
149{ 145{
150 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm; 146 struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm;
@@ -152,9 +148,8 @@ static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int status,
152 volatile struct qdio_buffer_element *sbale; 148 volatile struct qdio_buffer_element *sbale;
153 int sbal_idx, sbale_idx, sbal_no; 149 int sbal_idx, sbale_idx, sbal_no;
154 150
155 if (unlikely(status & QDIO_STATUS_LOOK_FOR_ERROR)) { 151 if (unlikely(qdio_err)) {
156 zfcp_hba_dbf_event_qdio(adapter, status, qdio_err, siga_err, 152 zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count);
157 first, count);
158 zfcp_qdio_handler_error(adapter, 147); 153 zfcp_qdio_handler_error(adapter, 147);
159 return; 154 return;
160 } 155 }
@@ -362,7 +357,7 @@ int zfcp_qdio_send(struct zfcp_fsf_req *fsf_req)
362 } 357 }
363 358
364 retval = do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0, first, 359 retval = do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_OUTPUT, 0, first,
365 count, NULL); 360 count);
366 if (unlikely(retval)) { 361 if (unlikely(retval)) {
367 zfcp_qdio_zero_sbals(req_q->sbal, first, count); 362 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
368 return retval; 363 return retval;
@@ -400,10 +395,6 @@ int zfcp_qdio_allocate(struct zfcp_adapter *adapter)
400 init_data->qib_param_field = NULL; 395 init_data->qib_param_field = NULL;
401 init_data->input_slib_elements = NULL; 396 init_data->input_slib_elements = NULL;
402 init_data->output_slib_elements = NULL; 397 init_data->output_slib_elements = NULL;
403 init_data->min_input_threshold = 1;
404 init_data->max_input_threshold = 5000;
405 init_data->min_output_threshold = 1;
406 init_data->max_output_threshold = 1000;
407 init_data->no_input_qs = 1; 398 init_data->no_input_qs = 1;
408 init_data->no_output_qs = 1; 399 init_data->no_output_qs = 1;
409 init_data->input_handler = zfcp_qdio_int_resp; 400 init_data->input_handler = zfcp_qdio_int_resp;
@@ -436,9 +427,7 @@ void zfcp_qdio_close(struct zfcp_adapter *adapter)
436 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status); 427 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status);
437 spin_unlock(&req_q->lock); 428 spin_unlock(&req_q->lock);
438 429
439 while (qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR) 430 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
440 == -EINPROGRESS)
441 ssleep(1);
442 431
443 /* cleanup used outbound sbals */ 432 /* cleanup used outbound sbals */
444 count = atomic_read(&req_q->count); 433 count = atomic_read(&req_q->count);
@@ -473,7 +462,7 @@ int zfcp_qdio_open(struct zfcp_adapter *adapter)
473 return -EIO; 462 return -EIO;
474 } 463 }
475 464
476 if (qdio_activate(adapter->ccw_device, 0)) { 465 if (qdio_activate(adapter->ccw_device)) {
477 dev_err(&adapter->ccw_device->dev, 466 dev_err(&adapter->ccw_device->dev,
478 "Activate of QDIO queues failed.\n"); 467 "Activate of QDIO queues failed.\n");
479 goto failed_qdio; 468 goto failed_qdio;
@@ -487,7 +476,7 @@ int zfcp_qdio_open(struct zfcp_adapter *adapter)
487 } 476 }
488 477
489 if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0, 478 if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0,
490 QDIO_MAX_BUFFERS_PER_Q, NULL)) { 479 QDIO_MAX_BUFFERS_PER_Q)) {
491 dev_err(&adapter->ccw_device->dev, 480 dev_err(&adapter->ccw_device->dev,
492 "Init of QDIO response queue failed.\n"); 481 "Init of QDIO response queue failed.\n");
493 goto failed_qdio; 482 goto failed_qdio;
@@ -501,9 +490,6 @@ int zfcp_qdio_open(struct zfcp_adapter *adapter)
501 return 0; 490 return 0;
502 491
503failed_qdio: 492failed_qdio:
504 while (qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR) 493 qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR);
505 == -EINPROGRESS)
506 ssleep(1);
507
508 return -EIO; 494 return -EIO;
509} 495}
diff --git a/drivers/scsi/device_handler/scsi_dh_emc.c b/drivers/scsi/device_handler/scsi_dh_emc.c
index ed53f14007a2..f2467e936e55 100644
--- a/drivers/scsi/device_handler/scsi_dh_emc.c
+++ b/drivers/scsi/device_handler/scsi_dh_emc.c
@@ -416,12 +416,17 @@ static int clariion_bus_notify(struct notifier_block *nb,
416 unsigned long action, void *data) 416 unsigned long action, void *data)
417{ 417{
418 struct device *dev = data; 418 struct device *dev = data;
419 struct scsi_device *sdev = to_scsi_device(dev); 419 struct scsi_device *sdev;
420 struct scsi_dh_data *scsi_dh_data; 420 struct scsi_dh_data *scsi_dh_data;
421 struct clariion_dh_data *h; 421 struct clariion_dh_data *h;
422 int i, found = 0; 422 int i, found = 0;
423 unsigned long flags; 423 unsigned long flags;
424 424
425 if (!scsi_is_sdev_device(dev))
426 return 0;
427
428 sdev = to_scsi_device(dev);
429
425 if (action == BUS_NOTIFY_ADD_DEVICE) { 430 if (action == BUS_NOTIFY_ADD_DEVICE) {
426 for (i = 0; clariion_dev_list[i].vendor; i++) { 431 for (i = 0; clariion_dev_list[i].vendor; i++) {
427 if (!strncmp(sdev->vendor, clariion_dev_list[i].vendor, 432 if (!strncmp(sdev->vendor, clariion_dev_list[i].vendor,
diff --git a/drivers/scsi/device_handler/scsi_dh_hp_sw.c b/drivers/scsi/device_handler/scsi_dh_hp_sw.c
index 12ceab7b3662..ae6be87d6a83 100644
--- a/drivers/scsi/device_handler/scsi_dh_hp_sw.c
+++ b/drivers/scsi/device_handler/scsi_dh_hp_sw.c
@@ -131,11 +131,16 @@ static int hp_sw_bus_notify(struct notifier_block *nb,
131 unsigned long action, void *data) 131 unsigned long action, void *data)
132{ 132{
133 struct device *dev = data; 133 struct device *dev = data;
134 struct scsi_device *sdev = to_scsi_device(dev); 134 struct scsi_device *sdev;
135 struct scsi_dh_data *scsi_dh_data; 135 struct scsi_dh_data *scsi_dh_data;
136 int i, found = 0; 136 int i, found = 0;
137 unsigned long flags; 137 unsigned long flags;
138 138
139 if (!scsi_is_sdev_device(dev))
140 return 0;
141
142 sdev = to_scsi_device(dev);
143
139 if (action == BUS_NOTIFY_ADD_DEVICE) { 144 if (action == BUS_NOTIFY_ADD_DEVICE) {
140 for (i = 0; hp_sw_dh_data_list[i].vendor; i++) { 145 for (i = 0; hp_sw_dh_data_list[i].vendor; i++) {
141 if (!strncmp(sdev->vendor, hp_sw_dh_data_list[i].vendor, 146 if (!strncmp(sdev->vendor, hp_sw_dh_data_list[i].vendor,
diff --git a/drivers/scsi/device_handler/scsi_dh_rdac.c b/drivers/scsi/device_handler/scsi_dh_rdac.c
index 6fff077a888d..fdf34b0ec6e1 100644
--- a/drivers/scsi/device_handler/scsi_dh_rdac.c
+++ b/drivers/scsi/device_handler/scsi_dh_rdac.c
@@ -608,12 +608,17 @@ static int rdac_bus_notify(struct notifier_block *nb,
608 unsigned long action, void *data) 608 unsigned long action, void *data)
609{ 609{
610 struct device *dev = data; 610 struct device *dev = data;
611 struct scsi_device *sdev = to_scsi_device(dev); 611 struct scsi_device *sdev;
612 struct scsi_dh_data *scsi_dh_data; 612 struct scsi_dh_data *scsi_dh_data;
613 struct rdac_dh_data *h; 613 struct rdac_dh_data *h;
614 int i, found = 0; 614 int i, found = 0;
615 unsigned long flags; 615 unsigned long flags;
616 616
617 if (!scsi_is_sdev_device(dev))
618 return 0;
619
620 sdev = to_scsi_device(dev);
621
617 if (action == BUS_NOTIFY_ADD_DEVICE) { 622 if (action == BUS_NOTIFY_ADD_DEVICE) {
618 for (i = 0; rdac_dev_list[i].vendor; i++) { 623 for (i = 0; rdac_dev_list[i].vendor; i++) {
619 if (!strncmp(sdev->vendor, rdac_dev_list[i].vendor, 624 if (!strncmp(sdev->vendor, rdac_dev_list[i].vendor,
diff --git a/drivers/scsi/ide-scsi.c b/drivers/scsi/ide-scsi.c
index 683bce375c74..f843c1383a4b 100644
--- a/drivers/scsi/ide-scsi.c
+++ b/drivers/scsi/ide-scsi.c
@@ -258,19 +258,6 @@ idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
258 return ide_stopped; 258 return ide_stopped;
259} 259}
260 260
261static ide_startstop_t
262idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
263{
264 debug_log("%s called for %lu\n", __func__,
265 ((struct ide_atapi_pc *) rq->special)->scsi_cmd->serial_number);
266
267 rq->errors |= ERROR_MAX;
268
269 idescsi_end_request(drive, 0, 0);
270
271 return ide_stopped;
272}
273
274static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs) 261static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
275{ 262{
276 idescsi_scsi_t *scsi = drive_to_idescsi(drive); 263 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
@@ -524,7 +511,6 @@ static ide_driver_t idescsi_driver = {
524 .do_request = idescsi_do_request, 511 .do_request = idescsi_do_request,
525 .end_request = idescsi_end_request, 512 .end_request = idescsi_end_request,
526 .error = idescsi_atapi_error, 513 .error = idescsi_atapi_error,
527 .abort = idescsi_atapi_abort,
528#ifdef CONFIG_IDE_PROC_FS 514#ifdef CONFIG_IDE_PROC_FS
529 .proc = idescsi_proc, 515 .proc = idescsi_proc,
530#endif 516#endif
diff --git a/drivers/serial/8250_pnp.c b/drivers/serial/8250_pnp.c
index 97c68d021d28..638b68649e79 100644
--- a/drivers/serial/8250_pnp.c
+++ b/drivers/serial/8250_pnp.c
@@ -383,21 +383,14 @@ static int __devinit check_name(char *name)
383 return 0; 383 return 0;
384} 384}
385 385
386static int __devinit check_resources(struct pnp_option *option) 386static int __devinit check_resources(struct pnp_dev *dev)
387{ 387{
388 struct pnp_option *tmp; 388 resource_size_t base[] = {0x2f8, 0x3f8, 0x2e8, 0x3e8};
389 if (!option) 389 int i;
390 return 0;
391 390
392 for (tmp = option; tmp; tmp = tmp->next) { 391 for (i = 0; i < ARRAY_SIZE(base); i++) {
393 struct pnp_port *port; 392 if (pnp_possible_config(dev, IORESOURCE_IO, base[i], 8))
394 for (port = tmp->port; port; port = port->next) 393 return 1;
395 if ((port->size == 8) &&
396 ((port->min == 0x2f8) ||
397 (port->min == 0x3f8) ||
398 (port->min == 0x2e8) ||
399 (port->min == 0x3e8)))
400 return 1;
401 } 394 }
402 395
403 return 0; 396 return 0;
@@ -420,10 +413,7 @@ static int __devinit serial_pnp_guess_board(struct pnp_dev *dev, int *flags)
420 (dev->card && check_name(dev->card->name)))) 413 (dev->card && check_name(dev->card->name))))
421 return -ENODEV; 414 return -ENODEV;
422 415
423 if (check_resources(dev->independent)) 416 if (check_resources(dev))
424 return 0;
425
426 if (check_resources(dev->dependent))
427 return 0; 417 return 0;
428 418
429 return -ENODEV; 419 return -ENODEV;
diff --git a/fs/Kconfig b/fs/Kconfig
index 313b2e06ded5..17216ba99c85 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -1375,6 +1375,9 @@ config JFFS2_CMODE_FAVOURLZO
1375 1375
1376endchoice 1376endchoice
1377 1377
1378# UBIFS File system configuration
1379source "fs/ubifs/Kconfig"
1380
1378config CRAMFS 1381config CRAMFS
1379 tristate "Compressed ROM file system support (cramfs)" 1382 tristate "Compressed ROM file system support (cramfs)"
1380 depends on BLOCK 1383 depends on BLOCK
@@ -1544,10 +1547,6 @@ config UFS_FS
1544 The recently released UFS2 variant (used in FreeBSD 5.x) is 1547 The recently released UFS2 variant (used in FreeBSD 5.x) is
1545 READ-ONLY supported. 1548 READ-ONLY supported.
1546 1549
1547 If you only intend to mount files from some other Unix over the
1548 network using NFS, you don't need the UFS file system support (but
1549 you need NFS file system support obviously).
1550
1551 Note that this option is generally not needed for floppies, since a 1550 Note that this option is generally not needed for floppies, since a
1552 good portable way to transport files and directories between unixes 1551 good portable way to transport files and directories between unixes
1553 (and even other operating systems) is given by the tar program ("man 1552 (and even other operating systems) is given by the tar program ("man
@@ -1587,6 +1586,7 @@ menuconfig NETWORK_FILESYSTEMS
1587 Say Y here to get to see options for network filesystems and 1586 Say Y here to get to see options for network filesystems and
1588 filesystem-related networking code, such as NFS daemon and 1587 filesystem-related networking code, such as NFS daemon and
1589 RPCSEC security modules. 1588 RPCSEC security modules.
1589
1590 This option alone does not add any kernel code. 1590 This option alone does not add any kernel code.
1591 1591
1592 If you say N, all options in this submenu will be skipped and 1592 If you say N, all options in this submenu will be skipped and
@@ -1595,76 +1595,92 @@ menuconfig NETWORK_FILESYSTEMS
1595if NETWORK_FILESYSTEMS 1595if NETWORK_FILESYSTEMS
1596 1596
1597config NFS_FS 1597config NFS_FS
1598 tristate "NFS file system support" 1598 tristate "NFS client support"
1599 depends on INET 1599 depends on INET
1600 select LOCKD 1600 select LOCKD
1601 select SUNRPC 1601 select SUNRPC
1602 select NFS_ACL_SUPPORT if NFS_V3_ACL 1602 select NFS_ACL_SUPPORT if NFS_V3_ACL
1603 help 1603 help
1604 If you are connected to some other (usually local) Unix computer 1604 Choose Y here if you want to access files residing on other
1605 (using SLIP, PLIP, PPP or Ethernet) and want to mount files residing 1605 computers using Sun's Network File System protocol. To compile
1606 on that computer (the NFS server) using the Network File Sharing 1606 this file system support as a module, choose M here: the module
1607 protocol, say Y. "Mounting files" means that the client can access 1607 will be called nfs.
1608 the files with usual UNIX commands as if they were sitting on the
1609 client's hard disk. For this to work, the server must run the
1610 programs nfsd and mountd (but does not need to have NFS file system
1611 support enabled in its kernel). NFS is explained in the Network
1612 Administrator's Guide, available from
1613 <http://www.tldp.org/docs.html#guide>, on its man page: "man
1614 nfs", and in the NFS-HOWTO.
1615 1608
1616 A superior but less widely used alternative to NFS is provided by 1609 To mount file systems exported by NFS servers, you also need to
1617 the Coda file system; see "Coda file system support" below. 1610 install the user space mount.nfs command which can be found in
1611 the Linux nfs-utils package, available from http://linux-nfs.org/.
1612 Information about using the mount command is available in the
1613 mount(8) man page. More detail about the Linux NFS client
1614 implementation is available via the nfs(5) man page.
1618 1615
1619 If you say Y here, you should have said Y to TCP/IP networking also. 1616 Below you can choose which versions of the NFS protocol are
1620 This option would enlarge your kernel by about 27 KB. 1617 available in the kernel to mount NFS servers. Support for NFS
1621 1618 version 2 (RFC 1094) is always available when NFS_FS is selected.
1622 To compile this file system support as a module, choose M here: the
1623 module will be called nfs.
1624 1619
1625 If you are configuring a diskless machine which will mount its root 1620 To configure a system which mounts its root file system via NFS
1626 file system over NFS at boot time, say Y here and to "Kernel 1621 at boot time, say Y here, select "Kernel level IP
1627 level IP autoconfiguration" above and to "Root file system on NFS" 1622 autoconfiguration" in the NETWORK menu, and select "Root file
1628 below. You cannot compile this driver as a module in this case. 1623 system on NFS" below. You cannot compile this file system as a
1629 There are two packages designed for booting diskless machines over 1624 module in this case.
1630 the net: netboot, available from
1631 <http://ftp1.sourceforge.net/netboot/>, and Etherboot,
1632 available from <http://ftp1.sourceforge.net/etherboot/>.
1633 1625
1634 If you don't know what all this is about, say N. 1626 If unsure, say N.
1635 1627
1636config NFS_V3 1628config NFS_V3
1637 bool "Provide NFSv3 client support" 1629 bool "NFS client support for NFS version 3"
1638 depends on NFS_FS 1630 depends on NFS_FS
1639 help 1631 help
1640 Say Y here if you want your NFS client to be able to speak version 1632 This option enables support for version 3 of the NFS protocol
1641 3 of the NFS protocol. 1633 (RFC 1813) in the kernel's NFS client.
1642 1634
1643 If unsure, say Y. 1635 If unsure, say Y.
1644 1636
1645config NFS_V3_ACL 1637config NFS_V3_ACL
1646 bool "Provide client support for the NFSv3 ACL protocol extension" 1638 bool "NFS client support for the NFSv3 ACL protocol extension"
1647 depends on NFS_V3 1639 depends on NFS_V3
1648 help 1640 help
1649 Implement the NFSv3 ACL protocol extension for manipulating POSIX 1641 Some NFS servers support an auxiliary NFSv3 ACL protocol that
1650 Access Control Lists. The server should also be compiled with 1642 Sun added to Solaris but never became an official part of the
1651 the NFSv3 ACL protocol extension; see the CONFIG_NFSD_V3_ACL option. 1643 NFS version 3 protocol. This protocol extension allows
1644 applications on NFS clients to manipulate POSIX Access Control
1645 Lists on files residing on NFS servers. NFS servers enforce
1646 ACLs on local files whether this protocol is available or not.
1647
1648 Choose Y here if your NFS server supports the Solaris NFSv3 ACL
1649 protocol extension and you want your NFS client to allow
1650 applications to access and modify ACLs on files on the server.
1651
1652 Most NFS servers don't support the Solaris NFSv3 ACL protocol
1653 extension. You can choose N here or specify the "noacl" mount
1654 option to prevent your NFS client from trying to use the NFSv3
1655 ACL protocol.
1652 1656
1653 If unsure, say N. 1657 If unsure, say N.
1654 1658
1655config NFS_V4 1659config NFS_V4
1656 bool "Provide NFSv4 client support (EXPERIMENTAL)" 1660 bool "NFS client support for NFS version 4 (EXPERIMENTAL)"
1657 depends on NFS_FS && EXPERIMENTAL 1661 depends on NFS_FS && EXPERIMENTAL
1658 select RPCSEC_GSS_KRB5 1662 select RPCSEC_GSS_KRB5
1659 help 1663 help
1660 Say Y here if you want your NFS client to be able to speak the newer 1664 This option enables support for version 4 of the NFS protocol
1661 version 4 of the NFS protocol. 1665 (RFC 3530) in the kernel's NFS client.
1662 1666
1663 Note: Requires auxiliary userspace daemons which may be found on 1667 To mount NFS servers using NFSv4, you also need to install user
1664 http://www.citi.umich.edu/projects/nfsv4/ 1668 space programs which can be found in the Linux nfs-utils package,
1669 available from http://linux-nfs.org/.
1665 1670
1666 If unsure, say N. 1671 If unsure, say N.
1667 1672
1673config ROOT_NFS
1674 bool "Root file system on NFS"
1675 depends on NFS_FS=y && IP_PNP
1676 help
1677 If you want your system to mount its root file system via NFS,
1678 choose Y here. This is common practice for managing systems
1679 without local permanent storage. For details, read
1680 <file:Documentation/filesystems/nfsroot.txt>.
1681
1682 Most people say N here.
1683
1668config NFSD 1684config NFSD
1669 tristate "NFS server support" 1685 tristate "NFS server support"
1670 depends on INET 1686 depends on INET
@@ -1746,20 +1762,6 @@ config NFSD_V4
1746 1762
1747 If unsure, say N. 1763 If unsure, say N.
1748 1764
1749config ROOT_NFS
1750 bool "Root file system on NFS"
1751 depends on NFS_FS=y && IP_PNP
1752 help
1753 If you want your Linux box to mount its whole root file system (the
1754 one containing the directory /) from some other computer over the
1755 net via NFS (presumably because your box doesn't have a hard disk),
1756 say Y. Read <file:Documentation/filesystems/nfsroot.txt> for
1757 details. It is likely that in this case, you also want to say Y to
1758 "Kernel level IP autoconfiguration" so that your box can discover
1759 its network address at boot time.
1760
1761 Most people say N here.
1762
1763config LOCKD 1765config LOCKD
1764 tristate 1766 tristate
1765 1767
@@ -1800,27 +1802,6 @@ config SUNRPC_XPRT_RDMA
1800 1802
1801 If unsure, say N. 1803 If unsure, say N.
1802 1804
1803config SUNRPC_BIND34
1804 bool "Support for rpcbind versions 3 & 4 (EXPERIMENTAL)"
1805 depends on SUNRPC && EXPERIMENTAL
1806 default n
1807 help
1808 RPC requests over IPv6 networks require support for larger
1809 addresses when performing an RPC bind. Sun added support for
1810 IPv6 addressing by creating two new versions of the rpcbind
1811 protocol (RFC 1833).
1812
1813 This option enables support in the kernel RPC client for
1814 querying rpcbind servers via versions 3 and 4 of the rpcbind
1815 protocol. The kernel automatically falls back to version 2
1816 if a remote rpcbind service does not support versions 3 or 4.
1817 By themselves, these new versions do not provide support for
1818 RPC over IPv6, but the new protocol versions are necessary to
1819 support it.
1820
1821 If unsure, say N to get traditional behavior (version 2 rpcbind
1822 requests only).
1823
1824config RPCSEC_GSS_KRB5 1805config RPCSEC_GSS_KRB5
1825 tristate "Secure RPC: Kerberos V mechanism (EXPERIMENTAL)" 1806 tristate "Secure RPC: Kerberos V mechanism (EXPERIMENTAL)"
1826 depends on SUNRPC && EXPERIMENTAL 1807 depends on SUNRPC && EXPERIMENTAL
diff --git a/fs/Makefile b/fs/Makefile
index 277b079dec9e..3b2178b4bb66 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -101,6 +101,7 @@ obj-$(CONFIG_NTFS_FS) += ntfs/
101obj-$(CONFIG_UFS_FS) += ufs/ 101obj-$(CONFIG_UFS_FS) += ufs/
102obj-$(CONFIG_EFS_FS) += efs/ 102obj-$(CONFIG_EFS_FS) += efs/
103obj-$(CONFIG_JFFS2_FS) += jffs2/ 103obj-$(CONFIG_JFFS2_FS) += jffs2/
104obj-$(CONFIG_UBIFS_FS) += ubifs/
104obj-$(CONFIG_AFFS_FS) += affs/ 105obj-$(CONFIG_AFFS_FS) += affs/
105obj-$(CONFIG_ROMFS_FS) += romfs/ 106obj-$(CONFIG_ROMFS_FS) += romfs/
106obj-$(CONFIG_QNX4FS_FS) += qnx4/ 107obj-$(CONFIG_QNX4FS_FS) += qnx4/
diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index ae45f77765c0..25adfc3c693a 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -424,8 +424,6 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
424 * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so 424 * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so
425 * that it can be located for waiting on in __writeback_single_inode(). 425 * that it can be located for waiting on in __writeback_single_inode().
426 * 426 *
427 * Called under inode_lock.
428 *
429 * If `bdi' is non-zero then we're being asked to writeback a specific queue. 427 * If `bdi' is non-zero then we're being asked to writeback a specific queue.
430 * This function assumes that the blockdev superblock's inodes are backed by 428 * This function assumes that the blockdev superblock's inodes are backed by
431 * a variety of queues, so all inodes are searched. For other superblocks, 429 * a variety of queues, so all inodes are searched. For other superblocks,
@@ -441,11 +439,12 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
441 * on the writer throttling path, and we get decent balancing between many 439 * on the writer throttling path, and we get decent balancing between many
442 * throttled threads: we don't want them all piling up on inode_sync_wait. 440 * throttled threads: we don't want them all piling up on inode_sync_wait.
443 */ 441 */
444static void 442void generic_sync_sb_inodes(struct super_block *sb,
445sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc) 443 struct writeback_control *wbc)
446{ 444{
447 const unsigned long start = jiffies; /* livelock avoidance */ 445 const unsigned long start = jiffies; /* livelock avoidance */
448 446
447 spin_lock(&inode_lock);
449 if (!wbc->for_kupdate || list_empty(&sb->s_io)) 448 if (!wbc->for_kupdate || list_empty(&sb->s_io))
450 queue_io(sb, wbc->older_than_this); 449 queue_io(sb, wbc->older_than_this);
451 450
@@ -524,8 +523,16 @@ sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc)
524 if (!list_empty(&sb->s_more_io)) 523 if (!list_empty(&sb->s_more_io))
525 wbc->more_io = 1; 524 wbc->more_io = 1;
526 } 525 }
526 spin_unlock(&inode_lock);
527 return; /* Leave any unwritten inodes on s_io */ 527 return; /* Leave any unwritten inodes on s_io */
528} 528}
529EXPORT_SYMBOL_GPL(generic_sync_sb_inodes);
530
531static void sync_sb_inodes(struct super_block *sb,
532 struct writeback_control *wbc)
533{
534 generic_sync_sb_inodes(sb, wbc);
535}
529 536
530/* 537/*
531 * Start writeback of dirty pagecache data against all unlocked inodes. 538 * Start writeback of dirty pagecache data against all unlocked inodes.
@@ -565,11 +572,8 @@ restart:
565 * be unmounted by the time it is released. 572 * be unmounted by the time it is released.
566 */ 573 */
567 if (down_read_trylock(&sb->s_umount)) { 574 if (down_read_trylock(&sb->s_umount)) {
568 if (sb->s_root) { 575 if (sb->s_root)
569 spin_lock(&inode_lock);
570 sync_sb_inodes(sb, wbc); 576 sync_sb_inodes(sb, wbc);
571 spin_unlock(&inode_lock);
572 }
573 up_read(&sb->s_umount); 577 up_read(&sb->s_umount);
574 } 578 }
575 spin_lock(&sb_lock); 579 spin_lock(&sb_lock);
@@ -607,9 +611,7 @@ void sync_inodes_sb(struct super_block *sb, int wait)
607 (inodes_stat.nr_inodes - inodes_stat.nr_unused) + 611 (inodes_stat.nr_inodes - inodes_stat.nr_unused) +
608 nr_dirty + nr_unstable; 612 nr_dirty + nr_unstable;
609 wbc.nr_to_write += wbc.nr_to_write / 2; /* Bit more for luck */ 613 wbc.nr_to_write += wbc.nr_to_write / 2; /* Bit more for luck */
610 spin_lock(&inode_lock);
611 sync_sb_inodes(sb, &wbc); 614 sync_sb_inodes(sb, &wbc);
612 spin_unlock(&inode_lock);
613} 615}
614 616
615/* 617/*
diff --git a/fs/lockd/clntproc.c b/fs/lockd/clntproc.c
index 5df517b81f3f..1f6dc518505c 100644
--- a/fs/lockd/clntproc.c
+++ b/fs/lockd/clntproc.c
@@ -224,7 +224,9 @@ void nlm_release_call(struct nlm_rqst *call)
224 224
225static void nlmclnt_rpc_release(void *data) 225static void nlmclnt_rpc_release(void *data)
226{ 226{
227 lock_kernel();
227 nlm_release_call(data); 228 nlm_release_call(data);
229 unlock_kernel();
228} 230}
229 231
230static int nlm_wait_on_grace(wait_queue_head_t *queue) 232static int nlm_wait_on_grace(wait_queue_head_t *queue)
@@ -430,7 +432,7 @@ nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
430 * Report the conflicting lock back to the application. 432 * Report the conflicting lock back to the application.
431 */ 433 */
432 fl->fl_start = req->a_res.lock.fl.fl_start; 434 fl->fl_start = req->a_res.lock.fl.fl_start;
433 fl->fl_end = req->a_res.lock.fl.fl_start; 435 fl->fl_end = req->a_res.lock.fl.fl_end;
434 fl->fl_type = req->a_res.lock.fl.fl_type; 436 fl->fl_type = req->a_res.lock.fl.fl_type;
435 fl->fl_pid = 0; 437 fl->fl_pid = 0;
436 break; 438 break;
@@ -710,7 +712,9 @@ static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
710die: 712die:
711 return; 713 return;
712 retry_rebind: 714 retry_rebind:
715 lock_kernel();
713 nlm_rebind_host(req->a_host); 716 nlm_rebind_host(req->a_host);
717 unlock_kernel();
714 retry_unlock: 718 retry_unlock:
715 rpc_restart_call(task); 719 rpc_restart_call(task);
716} 720}
@@ -788,7 +792,9 @@ retry_cancel:
788 /* Don't ever retry more than 3 times */ 792 /* Don't ever retry more than 3 times */
789 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES) 793 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
790 goto die; 794 goto die;
795 lock_kernel();
791 nlm_rebind_host(req->a_host); 796 nlm_rebind_host(req->a_host);
797 unlock_kernel();
792 rpc_restart_call(task); 798 rpc_restart_call(task);
793 rpc_delay(task, 30 * HZ); 799 rpc_delay(task, 30 * HZ);
794} 800}
diff --git a/fs/lockd/svc4proc.c b/fs/lockd/svc4proc.c
index 385437e3387d..2e27176ff42f 100644
--- a/fs/lockd/svc4proc.c
+++ b/fs/lockd/svc4proc.c
@@ -248,7 +248,9 @@ static void nlm4svc_callback_exit(struct rpc_task *task, void *data)
248 248
249static void nlm4svc_callback_release(void *data) 249static void nlm4svc_callback_release(void *data)
250{ 250{
251 lock_kernel();
251 nlm_release_call(data); 252 nlm_release_call(data);
253 unlock_kernel();
252} 254}
253 255
254static const struct rpc_call_ops nlm4svc_callback_ops = { 256static const struct rpc_call_ops nlm4svc_callback_ops = {
diff --git a/fs/lockd/svclock.c b/fs/lockd/svclock.c
index 81aca859bfde..56a08ab9a4cb 100644
--- a/fs/lockd/svclock.c
+++ b/fs/lockd/svclock.c
@@ -795,6 +795,7 @@ static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
795 795
796 dprintk("lockd: GRANT_MSG RPC callback\n"); 796 dprintk("lockd: GRANT_MSG RPC callback\n");
797 797
798 lock_kernel();
798 /* if the block is not on a list at this point then it has 799 /* if the block is not on a list at this point then it has
799 * been invalidated. Don't try to requeue it. 800 * been invalidated. Don't try to requeue it.
800 * 801 *
@@ -804,7 +805,7 @@ static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
804 * for nlm_blocked? 805 * for nlm_blocked?
805 */ 806 */
806 if (list_empty(&block->b_list)) 807 if (list_empty(&block->b_list))
807 return; 808 goto out;
808 809
809 /* Technically, we should down the file semaphore here. Since we 810 /* Technically, we should down the file semaphore here. Since we
810 * move the block towards the head of the queue only, no harm 811 * move the block towards the head of the queue only, no harm
@@ -818,13 +819,17 @@ static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
818 } 819 }
819 nlmsvc_insert_block(block, timeout); 820 nlmsvc_insert_block(block, timeout);
820 svc_wake_up(block->b_daemon); 821 svc_wake_up(block->b_daemon);
822out:
823 unlock_kernel();
821} 824}
822 825
823static void nlmsvc_grant_release(void *data) 826static void nlmsvc_grant_release(void *data)
824{ 827{
825 struct nlm_rqst *call = data; 828 struct nlm_rqst *call = data;
826 829
830 lock_kernel();
827 nlmsvc_release_block(call->a_block); 831 nlmsvc_release_block(call->a_block);
832 unlock_kernel();
828} 833}
829 834
830static const struct rpc_call_ops nlmsvc_grant_ops = { 835static const struct rpc_call_ops nlmsvc_grant_ops = {
diff --git a/fs/lockd/svcproc.c b/fs/lockd/svcproc.c
index 88379cc6e0b1..ce6952b50a75 100644
--- a/fs/lockd/svcproc.c
+++ b/fs/lockd/svcproc.c
@@ -278,7 +278,9 @@ static void nlmsvc_callback_exit(struct rpc_task *task, void *data)
278 278
279static void nlmsvc_callback_release(void *data) 279static void nlmsvc_callback_release(void *data)
280{ 280{
281 lock_kernel();
281 nlm_release_call(data); 282 nlm_release_call(data);
283 unlock_kernel();
282} 284}
283 285
284static const struct rpc_call_ops nlmsvc_callback_ops = { 286static const struct rpc_call_ops nlmsvc_callback_ops = {
diff --git a/fs/nfs/callback.c b/fs/nfs/callback.c
index c1e7c8300629..f447f4b4476c 100644
--- a/fs/nfs/callback.c
+++ b/fs/nfs/callback.c
@@ -27,7 +27,7 @@
27 27
28struct nfs_callback_data { 28struct nfs_callback_data {
29 unsigned int users; 29 unsigned int users;
30 struct svc_serv *serv; 30 struct svc_rqst *rqst;
31 struct task_struct *task; 31 struct task_struct *task;
32}; 32};
33 33
@@ -91,21 +91,17 @@ nfs_callback_svc(void *vrqstp)
91 svc_process(rqstp); 91 svc_process(rqstp);
92 } 92 }
93 unlock_kernel(); 93 unlock_kernel();
94 nfs_callback_info.task = NULL;
95 svc_exit_thread(rqstp);
96 return 0; 94 return 0;
97} 95}
98 96
99/* 97/*
100 * Bring up the server process if it is not already up. 98 * Bring up the callback thread if it is not already up.
101 */ 99 */
102int nfs_callback_up(void) 100int nfs_callback_up(void)
103{ 101{
104 struct svc_serv *serv = NULL; 102 struct svc_serv *serv = NULL;
105 struct svc_rqst *rqstp;
106 int ret = 0; 103 int ret = 0;
107 104
108 lock_kernel();
109 mutex_lock(&nfs_callback_mutex); 105 mutex_lock(&nfs_callback_mutex);
110 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL) 106 if (nfs_callback_info.users++ || nfs_callback_info.task != NULL)
111 goto out; 107 goto out;
@@ -121,22 +117,23 @@ int nfs_callback_up(void)
121 nfs_callback_tcpport = ret; 117 nfs_callback_tcpport = ret;
122 dprintk("Callback port = 0x%x\n", nfs_callback_tcpport); 118 dprintk("Callback port = 0x%x\n", nfs_callback_tcpport);
123 119
124 rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]); 120 nfs_callback_info.rqst = svc_prepare_thread(serv, &serv->sv_pools[0]);
125 if (IS_ERR(rqstp)) { 121 if (IS_ERR(nfs_callback_info.rqst)) {
126 ret = PTR_ERR(rqstp); 122 ret = PTR_ERR(nfs_callback_info.rqst);
123 nfs_callback_info.rqst = NULL;
127 goto out_err; 124 goto out_err;
128 } 125 }
129 126
130 svc_sock_update_bufs(serv); 127 svc_sock_update_bufs(serv);
131 nfs_callback_info.serv = serv;
132 128
133 nfs_callback_info.task = kthread_run(nfs_callback_svc, rqstp, 129 nfs_callback_info.task = kthread_run(nfs_callback_svc,
130 nfs_callback_info.rqst,
134 "nfsv4-svc"); 131 "nfsv4-svc");
135 if (IS_ERR(nfs_callback_info.task)) { 132 if (IS_ERR(nfs_callback_info.task)) {
136 ret = PTR_ERR(nfs_callback_info.task); 133 ret = PTR_ERR(nfs_callback_info.task);
137 nfs_callback_info.serv = NULL; 134 svc_exit_thread(nfs_callback_info.rqst);
135 nfs_callback_info.rqst = NULL;
138 nfs_callback_info.task = NULL; 136 nfs_callback_info.task = NULL;
139 svc_exit_thread(rqstp);
140 goto out_err; 137 goto out_err;
141 } 138 }
142out: 139out:
@@ -149,7 +146,6 @@ out:
149 if (serv) 146 if (serv)
150 svc_destroy(serv); 147 svc_destroy(serv);
151 mutex_unlock(&nfs_callback_mutex); 148 mutex_unlock(&nfs_callback_mutex);
152 unlock_kernel();
153 return ret; 149 return ret;
154out_err: 150out_err:
155 dprintk("Couldn't create callback socket or server thread; err = %d\n", 151 dprintk("Couldn't create callback socket or server thread; err = %d\n",
@@ -159,17 +155,19 @@ out_err:
159} 155}
160 156
161/* 157/*
162 * Kill the server process if it is not already down. 158 * Kill the callback thread if it's no longer being used.
163 */ 159 */
164void nfs_callback_down(void) 160void nfs_callback_down(void)
165{ 161{
166 lock_kernel();
167 mutex_lock(&nfs_callback_mutex); 162 mutex_lock(&nfs_callback_mutex);
168 nfs_callback_info.users--; 163 nfs_callback_info.users--;
169 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) 164 if (nfs_callback_info.users == 0 && nfs_callback_info.task != NULL) {
170 kthread_stop(nfs_callback_info.task); 165 kthread_stop(nfs_callback_info.task);
166 svc_exit_thread(nfs_callback_info.rqst);
167 nfs_callback_info.rqst = NULL;
168 nfs_callback_info.task = NULL;
169 }
171 mutex_unlock(&nfs_callback_mutex); 170 mutex_unlock(&nfs_callback_mutex);
172 unlock_kernel();
173} 171}
174 172
175static int nfs_callback_authenticate(struct svc_rqst *rqstp) 173static int nfs_callback_authenticate(struct svc_rqst *rqstp)
diff --git a/fs/nfs/client.c b/fs/nfs/client.c
index f2a092ca69b5..5ee23e7058b3 100644
--- a/fs/nfs/client.c
+++ b/fs/nfs/client.c
@@ -431,14 +431,14 @@ static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
431{ 431{
432 to->to_initval = timeo * HZ / 10; 432 to->to_initval = timeo * HZ / 10;
433 to->to_retries = retrans; 433 to->to_retries = retrans;
434 if (!to->to_retries)
435 to->to_retries = 2;
436 434
437 switch (proto) { 435 switch (proto) {
438 case XPRT_TRANSPORT_TCP: 436 case XPRT_TRANSPORT_TCP:
439 case XPRT_TRANSPORT_RDMA: 437 case XPRT_TRANSPORT_RDMA:
438 if (to->to_retries == 0)
439 to->to_retries = NFS_DEF_TCP_RETRANS;
440 if (to->to_initval == 0) 440 if (to->to_initval == 0)
441 to->to_initval = 60 * HZ; 441 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
442 if (to->to_initval > NFS_MAX_TCP_TIMEOUT) 442 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
443 to->to_initval = NFS_MAX_TCP_TIMEOUT; 443 to->to_initval = NFS_MAX_TCP_TIMEOUT;
444 to->to_increment = to->to_initval; 444 to->to_increment = to->to_initval;
@@ -450,14 +450,17 @@ static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
450 to->to_exponential = 0; 450 to->to_exponential = 0;
451 break; 451 break;
452 case XPRT_TRANSPORT_UDP: 452 case XPRT_TRANSPORT_UDP:
453 default: 453 if (to->to_retries == 0)
454 to->to_retries = NFS_DEF_UDP_RETRANS;
454 if (!to->to_initval) 455 if (!to->to_initval)
455 to->to_initval = 11 * HZ / 10; 456 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
456 if (to->to_initval > NFS_MAX_UDP_TIMEOUT) 457 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
457 to->to_initval = NFS_MAX_UDP_TIMEOUT; 458 to->to_initval = NFS_MAX_UDP_TIMEOUT;
458 to->to_maxval = NFS_MAX_UDP_TIMEOUT; 459 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
459 to->to_exponential = 1; 460 to->to_exponential = 1;
460 break; 461 break;
462 default:
463 BUG();
461 } 464 }
462} 465}
463 466
diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
index 982a2064fe4c..28a238dab23a 100644
--- a/fs/nfs/dir.c
+++ b/fs/nfs/dir.c
@@ -133,13 +133,14 @@ nfs_opendir(struct inode *inode, struct file *filp)
133{ 133{
134 int res; 134 int res;
135 135
136 dfprintk(VFS, "NFS: opendir(%s/%ld)\n", 136 dfprintk(FILE, "NFS: open dir(%s/%s)\n",
137 inode->i_sb->s_id, inode->i_ino); 137 filp->f_path.dentry->d_parent->d_name.name,
138 filp->f_path.dentry->d_name.name);
139
140 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
138 141
139 lock_kernel();
140 /* Call generic open code in order to cache credentials */ 142 /* Call generic open code in order to cache credentials */
141 res = nfs_open(inode, filp); 143 res = nfs_open(inode, filp);
142 unlock_kernel();
143 return res; 144 return res;
144} 145}
145 146
@@ -528,13 +529,11 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
528 struct nfs_fattr fattr; 529 struct nfs_fattr fattr;
529 long res; 530 long res;
530 531
531 dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n", 532 dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
532 dentry->d_parent->d_name.name, dentry->d_name.name, 533 dentry->d_parent->d_name.name, dentry->d_name.name,
533 (long long)filp->f_pos); 534 (long long)filp->f_pos);
534 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); 535 nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
535 536
536 lock_kernel();
537
538 /* 537 /*
539 * filp->f_pos points to the dirent entry number. 538 * filp->f_pos points to the dirent entry number.
540 * *desc->dir_cookie has the cookie for the next entry. We have 539 * *desc->dir_cookie has the cookie for the next entry. We have
@@ -592,10 +591,9 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
592 } 591 }
593out: 592out:
594 nfs_unblock_sillyrename(dentry); 593 nfs_unblock_sillyrename(dentry);
595 unlock_kernel();
596 if (res > 0) 594 if (res > 0)
597 res = 0; 595 res = 0;
598 dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n", 596 dfprintk(FILE, "NFS: readdir(%s/%s) returns %ld\n",
599 dentry->d_parent->d_name.name, dentry->d_name.name, 597 dentry->d_parent->d_name.name, dentry->d_name.name,
600 res); 598 res);
601 return res; 599 return res;
@@ -603,7 +601,15 @@ out:
603 601
604static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin) 602static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
605{ 603{
606 mutex_lock(&filp->f_path.dentry->d_inode->i_mutex); 604 struct dentry *dentry = filp->f_path.dentry;
605 struct inode *inode = dentry->d_inode;
606
607 dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
608 dentry->d_parent->d_name.name,
609 dentry->d_name.name,
610 offset, origin);
611
612 mutex_lock(&inode->i_mutex);
607 switch (origin) { 613 switch (origin) {
608 case 1: 614 case 1:
609 offset += filp->f_pos; 615 offset += filp->f_pos;
@@ -619,7 +625,7 @@ static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
619 nfs_file_open_context(filp)->dir_cookie = 0; 625 nfs_file_open_context(filp)->dir_cookie = 0;
620 } 626 }
621out: 627out:
622 mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex); 628 mutex_unlock(&inode->i_mutex);
623 return offset; 629 return offset;
624} 630}
625 631
@@ -629,10 +635,11 @@ out:
629 */ 635 */
630static int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync) 636static int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
631{ 637{
632 dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n", 638 dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
633 dentry->d_parent->d_name.name, dentry->d_name.name, 639 dentry->d_parent->d_name.name, dentry->d_name.name,
634 datasync); 640 datasync);
635 641
642 nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
636 return 0; 643 return 0;
637} 644}
638 645
@@ -767,7 +774,6 @@ static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
767 struct nfs_fattr fattr; 774 struct nfs_fattr fattr;
768 775
769 parent = dget_parent(dentry); 776 parent = dget_parent(dentry);
770 lock_kernel();
771 dir = parent->d_inode; 777 dir = parent->d_inode;
772 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); 778 nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
773 inode = dentry->d_inode; 779 inode = dentry->d_inode;
@@ -805,7 +811,6 @@ static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
805 811
806 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 812 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
807 out_valid: 813 out_valid:
808 unlock_kernel();
809 dput(parent); 814 dput(parent);
810 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n", 815 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
811 __func__, dentry->d_parent->d_name.name, 816 __func__, dentry->d_parent->d_name.name,
@@ -824,7 +829,6 @@ out_zap_parent:
824 shrink_dcache_parent(dentry); 829 shrink_dcache_parent(dentry);
825 } 830 }
826 d_drop(dentry); 831 d_drop(dentry);
827 unlock_kernel();
828 dput(parent); 832 dput(parent);
829 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n", 833 dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
830 __func__, dentry->d_parent->d_name.name, 834 __func__, dentry->d_parent->d_name.name,
@@ -858,6 +862,14 @@ static int nfs_dentry_delete(struct dentry *dentry)
858 862
859} 863}
860 864
865static void nfs_drop_nlink(struct inode *inode)
866{
867 spin_lock(&inode->i_lock);
868 if (inode->i_nlink > 0)
869 drop_nlink(inode);
870 spin_unlock(&inode->i_lock);
871}
872
861/* 873/*
862 * Called when the dentry loses inode. 874 * Called when the dentry loses inode.
863 * We use it to clean up silly-renamed files. 875 * We use it to clean up silly-renamed files.
@@ -869,10 +881,8 @@ static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
869 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; 881 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
870 882
871 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 883 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
872 lock_kernel();
873 drop_nlink(inode); 884 drop_nlink(inode);
874 nfs_complete_unlink(dentry, inode); 885 nfs_complete_unlink(dentry, inode);
875 unlock_kernel();
876 } 886 }
877 iput(inode); 887 iput(inode);
878} 888}
@@ -903,8 +913,6 @@ static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, stru
903 res = ERR_PTR(-ENOMEM); 913 res = ERR_PTR(-ENOMEM);
904 dentry->d_op = NFS_PROTO(dir)->dentry_ops; 914 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
905 915
906 lock_kernel();
907
908 /* 916 /*
909 * If we're doing an exclusive create, optimize away the lookup 917 * If we're doing an exclusive create, optimize away the lookup
910 * but don't hash the dentry. 918 * but don't hash the dentry.
@@ -912,7 +920,7 @@ static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, stru
912 if (nfs_is_exclusive_create(dir, nd)) { 920 if (nfs_is_exclusive_create(dir, nd)) {
913 d_instantiate(dentry, NULL); 921 d_instantiate(dentry, NULL);
914 res = NULL; 922 res = NULL;
915 goto out_unlock; 923 goto out;
916 } 924 }
917 925
918 parent = dentry->d_parent; 926 parent = dentry->d_parent;
@@ -940,8 +948,6 @@ no_entry:
940 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 948 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
941out_unblock_sillyrename: 949out_unblock_sillyrename:
942 nfs_unblock_sillyrename(parent); 950 nfs_unblock_sillyrename(parent);
943out_unlock:
944 unlock_kernel();
945out: 951out:
946 return res; 952 return res;
947} 953}
@@ -999,9 +1005,7 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
999 } 1005 }
1000 1006
1001 /* Open the file on the server */ 1007 /* Open the file on the server */
1002 lock_kernel();
1003 res = nfs4_atomic_open(dir, dentry, nd); 1008 res = nfs4_atomic_open(dir, dentry, nd);
1004 unlock_kernel();
1005 if (IS_ERR(res)) { 1009 if (IS_ERR(res)) {
1006 error = PTR_ERR(res); 1010 error = PTR_ERR(res);
1007 switch (error) { 1011 switch (error) {
@@ -1063,9 +1067,7 @@ static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1063 * operations that change the directory. We therefore save the 1067 * operations that change the directory. We therefore save the
1064 * change attribute *before* we do the RPC call. 1068 * change attribute *before* we do the RPC call.
1065 */ 1069 */
1066 lock_kernel();
1067 ret = nfs4_open_revalidate(dir, dentry, openflags, nd); 1070 ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
1068 unlock_kernel();
1069out: 1071out:
1070 dput(parent); 1072 dput(parent);
1071 if (!ret) 1073 if (!ret)
@@ -1218,14 +1220,11 @@ static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1218 if ((nd->flags & LOOKUP_CREATE) != 0) 1220 if ((nd->flags & LOOKUP_CREATE) != 0)
1219 open_flags = nd->intent.open.flags; 1221 open_flags = nd->intent.open.flags;
1220 1222
1221 lock_kernel();
1222 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd); 1223 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
1223 if (error != 0) 1224 if (error != 0)
1224 goto out_err; 1225 goto out_err;
1225 unlock_kernel();
1226 return 0; 1226 return 0;
1227out_err: 1227out_err:
1228 unlock_kernel();
1229 d_drop(dentry); 1228 d_drop(dentry);
1230 return error; 1229 return error;
1231} 1230}
@@ -1248,14 +1247,11 @@ nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
1248 attr.ia_mode = mode; 1247 attr.ia_mode = mode;
1249 attr.ia_valid = ATTR_MODE; 1248 attr.ia_valid = ATTR_MODE;
1250 1249
1251 lock_kernel();
1252 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); 1250 status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
1253 if (status != 0) 1251 if (status != 0)
1254 goto out_err; 1252 goto out_err;
1255 unlock_kernel();
1256 return 0; 1253 return 0;
1257out_err: 1254out_err:
1258 unlock_kernel();
1259 d_drop(dentry); 1255 d_drop(dentry);
1260 return status; 1256 return status;
1261} 1257}
@@ -1274,15 +1270,12 @@ static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1274 attr.ia_valid = ATTR_MODE; 1270 attr.ia_valid = ATTR_MODE;
1275 attr.ia_mode = mode | S_IFDIR; 1271 attr.ia_mode = mode | S_IFDIR;
1276 1272
1277 lock_kernel();
1278 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); 1273 error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
1279 if (error != 0) 1274 if (error != 0)
1280 goto out_err; 1275 goto out_err;
1281 unlock_kernel();
1282 return 0; 1276 return 0;
1283out_err: 1277out_err:
1284 d_drop(dentry); 1278 d_drop(dentry);
1285 unlock_kernel();
1286 return error; 1279 return error;
1287} 1280}
1288 1281
@@ -1299,14 +1292,12 @@ static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
1299 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n", 1292 dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
1300 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name); 1293 dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
1301 1294
1302 lock_kernel();
1303 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); 1295 error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
1304 /* Ensure the VFS deletes this inode */ 1296 /* Ensure the VFS deletes this inode */
1305 if (error == 0 && dentry->d_inode != NULL) 1297 if (error == 0 && dentry->d_inode != NULL)
1306 clear_nlink(dentry->d_inode); 1298 clear_nlink(dentry->d_inode);
1307 else if (error == -ENOENT) 1299 else if (error == -ENOENT)
1308 nfs_dentry_handle_enoent(dentry); 1300 nfs_dentry_handle_enoent(dentry);
1309 unlock_kernel();
1310 1301
1311 return error; 1302 return error;
1312} 1303}
@@ -1408,7 +1399,7 @@ static int nfs_safe_remove(struct dentry *dentry)
1408 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1399 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1409 /* The VFS may want to delete this inode */ 1400 /* The VFS may want to delete this inode */
1410 if (error == 0) 1401 if (error == 0)
1411 drop_nlink(inode); 1402 nfs_drop_nlink(inode);
1412 nfs_mark_for_revalidate(inode); 1403 nfs_mark_for_revalidate(inode);
1413 } else 1404 } else
1414 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1405 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
@@ -1431,7 +1422,6 @@ static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1431 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id, 1422 dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
1432 dir->i_ino, dentry->d_name.name); 1423 dir->i_ino, dentry->d_name.name);
1433 1424
1434 lock_kernel();
1435 spin_lock(&dcache_lock); 1425 spin_lock(&dcache_lock);
1436 spin_lock(&dentry->d_lock); 1426 spin_lock(&dentry->d_lock);
1437 if (atomic_read(&dentry->d_count) > 1) { 1427 if (atomic_read(&dentry->d_count) > 1) {
@@ -1440,7 +1430,6 @@ static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1440 /* Start asynchronous writeout of the inode */ 1430 /* Start asynchronous writeout of the inode */
1441 write_inode_now(dentry->d_inode, 0); 1431 write_inode_now(dentry->d_inode, 0);
1442 error = nfs_sillyrename(dir, dentry); 1432 error = nfs_sillyrename(dir, dentry);
1443 unlock_kernel();
1444 return error; 1433 return error;
1445 } 1434 }
1446 if (!d_unhashed(dentry)) { 1435 if (!d_unhashed(dentry)) {
@@ -1454,7 +1443,6 @@ static int nfs_unlink(struct inode *dir, struct dentry *dentry)
1454 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1443 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1455 } else if (need_rehash) 1444 } else if (need_rehash)
1456 d_rehash(dentry); 1445 d_rehash(dentry);
1457 unlock_kernel();
1458 return error; 1446 return error;
1459} 1447}
1460 1448
@@ -1491,13 +1479,9 @@ static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *sym
1491 attr.ia_mode = S_IFLNK | S_IRWXUGO; 1479 attr.ia_mode = S_IFLNK | S_IRWXUGO;
1492 attr.ia_valid = ATTR_MODE; 1480 attr.ia_valid = ATTR_MODE;
1493 1481
1494 lock_kernel();
1495
1496 page = alloc_page(GFP_HIGHUSER); 1482 page = alloc_page(GFP_HIGHUSER);
1497 if (!page) { 1483 if (!page)
1498 unlock_kernel();
1499 return -ENOMEM; 1484 return -ENOMEM;
1500 }
1501 1485
1502 kaddr = kmap_atomic(page, KM_USER0); 1486 kaddr = kmap_atomic(page, KM_USER0);
1503 memcpy(kaddr, symname, pathlen); 1487 memcpy(kaddr, symname, pathlen);
@@ -1512,7 +1496,6 @@ static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *sym
1512 dentry->d_name.name, symname, error); 1496 dentry->d_name.name, symname, error);
1513 d_drop(dentry); 1497 d_drop(dentry);
1514 __free_page(page); 1498 __free_page(page);
1515 unlock_kernel();
1516 return error; 1499 return error;
1517 } 1500 }
1518 1501
@@ -1530,7 +1513,6 @@ static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *sym
1530 } else 1513 } else
1531 __free_page(page); 1514 __free_page(page);
1532 1515
1533 unlock_kernel();
1534 return 0; 1516 return 0;
1535} 1517}
1536 1518
@@ -1544,14 +1526,12 @@ nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1544 old_dentry->d_parent->d_name.name, old_dentry->d_name.name, 1526 old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
1545 dentry->d_parent->d_name.name, dentry->d_name.name); 1527 dentry->d_parent->d_name.name, dentry->d_name.name);
1546 1528
1547 lock_kernel();
1548 d_drop(dentry); 1529 d_drop(dentry);
1549 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); 1530 error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
1550 if (error == 0) { 1531 if (error == 0) {
1551 atomic_inc(&inode->i_count); 1532 atomic_inc(&inode->i_count);
1552 d_add(dentry, inode); 1533 d_add(dentry, inode);
1553 } 1534 }
1554 unlock_kernel();
1555 return error; 1535 return error;
1556} 1536}
1557 1537
@@ -1591,7 +1571,6 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1591 * To prevent any new references to the target during the rename, 1571 * To prevent any new references to the target during the rename,
1592 * we unhash the dentry and free the inode in advance. 1572 * we unhash the dentry and free the inode in advance.
1593 */ 1573 */
1594 lock_kernel();
1595 if (!d_unhashed(new_dentry)) { 1574 if (!d_unhashed(new_dentry)) {
1596 d_drop(new_dentry); 1575 d_drop(new_dentry);
1597 rehash = new_dentry; 1576 rehash = new_dentry;
@@ -1635,7 +1614,7 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1635 /* dentry still busy? */ 1614 /* dentry still busy? */
1636 goto out; 1615 goto out;
1637 } else 1616 } else
1638 drop_nlink(new_inode); 1617 nfs_drop_nlink(new_inode);
1639 1618
1640go_ahead: 1619go_ahead:
1641 /* 1620 /*
@@ -1669,7 +1648,6 @@ out:
1669 /* new dentry created? */ 1648 /* new dentry created? */
1670 if (dentry) 1649 if (dentry)
1671 dput(dentry); 1650 dput(dentry);
1672 unlock_kernel();
1673 return error; 1651 return error;
1674} 1652}
1675 1653
@@ -1962,8 +1940,6 @@ int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
1962 } 1940 }
1963 1941
1964force_lookup: 1942force_lookup:
1965 lock_kernel();
1966
1967 if (!NFS_PROTO(inode)->access) 1943 if (!NFS_PROTO(inode)->access)
1968 goto out_notsup; 1944 goto out_notsup;
1969 1945
@@ -1973,7 +1949,6 @@ force_lookup:
1973 put_rpccred(cred); 1949 put_rpccred(cred);
1974 } else 1950 } else
1975 res = PTR_ERR(cred); 1951 res = PTR_ERR(cred);
1976 unlock_kernel();
1977out: 1952out:
1978 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n", 1953 dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
1979 inode->i_sb->s_id, inode->i_ino, mask, res); 1954 inode->i_sb->s_id, inode->i_ino, mask, res);
@@ -1982,7 +1957,6 @@ out_notsup:
1982 res = nfs_revalidate_inode(NFS_SERVER(inode), inode); 1957 res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
1983 if (res == 0) 1958 if (res == 0)
1984 res = generic_permission(inode, mask, NULL); 1959 res = generic_permission(inode, mask, NULL);
1985 unlock_kernel();
1986 goto out; 1960 goto out;
1987} 1961}
1988 1962
diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c
index 4757a2b326a1..08f6b040d289 100644
--- a/fs/nfs/direct.c
+++ b/fs/nfs/direct.c
@@ -890,7 +890,7 @@ ssize_t nfs_file_direct_read(struct kiocb *iocb, const struct iovec *iov,
890 count = iov_length(iov, nr_segs); 890 count = iov_length(iov, nr_segs);
891 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count); 891 nfs_add_stats(mapping->host, NFSIOS_DIRECTREADBYTES, count);
892 892
893 dprintk("nfs: direct read(%s/%s, %zd@%Ld)\n", 893 dfprintk(FILE, "NFS: direct read(%s/%s, %zd@%Ld)\n",
894 file->f_path.dentry->d_parent->d_name.name, 894 file->f_path.dentry->d_parent->d_name.name,
895 file->f_path.dentry->d_name.name, 895 file->f_path.dentry->d_name.name,
896 count, (long long) pos); 896 count, (long long) pos);
@@ -947,7 +947,7 @@ ssize_t nfs_file_direct_write(struct kiocb *iocb, const struct iovec *iov,
947 count = iov_length(iov, nr_segs); 947 count = iov_length(iov, nr_segs);
948 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count); 948 nfs_add_stats(mapping->host, NFSIOS_DIRECTWRITTENBYTES, count);
949 949
950 dfprintk(VFS, "nfs: direct write(%s/%s, %zd@%Ld)\n", 950 dfprintk(FILE, "NFS: direct write(%s/%s, %zd@%Ld)\n",
951 file->f_path.dentry->d_parent->d_name.name, 951 file->f_path.dentry->d_parent->d_name.name,
952 file->f_path.dentry->d_name.name, 952 file->f_path.dentry->d_name.name,
953 count, (long long) pos); 953 count, (long long) pos);
diff --git a/fs/nfs/file.c b/fs/nfs/file.c
index 4e98a56a1777..78460657f5cb 100644
--- a/fs/nfs/file.c
+++ b/fs/nfs/file.c
@@ -50,7 +50,7 @@ static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov,
50static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov, 50static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
51 unsigned long nr_segs, loff_t pos); 51 unsigned long nr_segs, loff_t pos);
52static int nfs_file_flush(struct file *, fl_owner_t id); 52static int nfs_file_flush(struct file *, fl_owner_t id);
53static int nfs_fsync(struct file *, struct dentry *dentry, int datasync); 53static int nfs_file_fsync(struct file *, struct dentry *dentry, int datasync);
54static int nfs_check_flags(int flags); 54static int nfs_check_flags(int flags);
55static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl); 55static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
56static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl); 56static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
@@ -72,7 +72,7 @@ const struct file_operations nfs_file_operations = {
72 .open = nfs_file_open, 72 .open = nfs_file_open,
73 .flush = nfs_file_flush, 73 .flush = nfs_file_flush,
74 .release = nfs_file_release, 74 .release = nfs_file_release,
75 .fsync = nfs_fsync, 75 .fsync = nfs_file_fsync,
76 .lock = nfs_lock, 76 .lock = nfs_lock,
77 .flock = nfs_flock, 77 .flock = nfs_flock,
78 .splice_read = nfs_file_splice_read, 78 .splice_read = nfs_file_splice_read,
@@ -119,25 +119,33 @@ nfs_file_open(struct inode *inode, struct file *filp)
119{ 119{
120 int res; 120 int res;
121 121
122 dprintk("NFS: open file(%s/%s)\n",
123 filp->f_path.dentry->d_parent->d_name.name,
124 filp->f_path.dentry->d_name.name);
125
122 res = nfs_check_flags(filp->f_flags); 126 res = nfs_check_flags(filp->f_flags);
123 if (res) 127 if (res)
124 return res; 128 return res;
125 129
126 nfs_inc_stats(inode, NFSIOS_VFSOPEN); 130 nfs_inc_stats(inode, NFSIOS_VFSOPEN);
127 lock_kernel(); 131 res = nfs_open(inode, filp);
128 res = NFS_PROTO(inode)->file_open(inode, filp);
129 unlock_kernel();
130 return res; 132 return res;
131} 133}
132 134
133static int 135static int
134nfs_file_release(struct inode *inode, struct file *filp) 136nfs_file_release(struct inode *inode, struct file *filp)
135{ 137{
138 struct dentry *dentry = filp->f_path.dentry;
139
140 dprintk("NFS: release(%s/%s)\n",
141 dentry->d_parent->d_name.name,
142 dentry->d_name.name);
143
136 /* Ensure that dirty pages are flushed out with the right creds */ 144 /* Ensure that dirty pages are flushed out with the right creds */
137 if (filp->f_mode & FMODE_WRITE) 145 if (filp->f_mode & FMODE_WRITE)
138 nfs_wb_all(filp->f_path.dentry->d_inode); 146 nfs_wb_all(dentry->d_inode);
139 nfs_inc_stats(inode, NFSIOS_VFSRELEASE); 147 nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
140 return NFS_PROTO(inode)->file_release(inode, filp); 148 return nfs_release(inode, filp);
141} 149}
142 150
143/** 151/**
@@ -171,6 +179,12 @@ force_reval:
171static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin) 179static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
172{ 180{
173 loff_t loff; 181 loff_t loff;
182
183 dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
184 filp->f_path.dentry->d_parent->d_name.name,
185 filp->f_path.dentry->d_name.name,
186 offset, origin);
187
174 /* origin == SEEK_END => we must revalidate the cached file length */ 188 /* origin == SEEK_END => we must revalidate the cached file length */
175 if (origin == SEEK_END) { 189 if (origin == SEEK_END) {
176 struct inode *inode = filp->f_mapping->host; 190 struct inode *inode = filp->f_mapping->host;
@@ -185,7 +199,7 @@ static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
185} 199}
186 200
187/* 201/*
188 * Helper for nfs_file_flush() and nfs_fsync() 202 * Helper for nfs_file_flush() and nfs_file_fsync()
189 * 203 *
190 * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to 204 * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
191 * disk, but it retrieves and clears ctx->error after synching, despite 205 * disk, but it retrieves and clears ctx->error after synching, despite
@@ -211,16 +225,18 @@ static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
211 225
212/* 226/*
213 * Flush all dirty pages, and check for write errors. 227 * Flush all dirty pages, and check for write errors.
214 *
215 */ 228 */
216static int 229static int
217nfs_file_flush(struct file *file, fl_owner_t id) 230nfs_file_flush(struct file *file, fl_owner_t id)
218{ 231{
219 struct nfs_open_context *ctx = nfs_file_open_context(file); 232 struct nfs_open_context *ctx = nfs_file_open_context(file);
220 struct inode *inode = file->f_path.dentry->d_inode; 233 struct dentry *dentry = file->f_path.dentry;
234 struct inode *inode = dentry->d_inode;
221 int status; 235 int status;
222 236
223 dfprintk(VFS, "nfs: flush(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 237 dprintk("NFS: flush(%s/%s)\n",
238 dentry->d_parent->d_name.name,
239 dentry->d_name.name);
224 240
225 if ((file->f_mode & FMODE_WRITE) == 0) 241 if ((file->f_mode & FMODE_WRITE) == 0)
226 return 0; 242 return 0;
@@ -245,7 +261,7 @@ nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
245 if (iocb->ki_filp->f_flags & O_DIRECT) 261 if (iocb->ki_filp->f_flags & O_DIRECT)
246 return nfs_file_direct_read(iocb, iov, nr_segs, pos); 262 return nfs_file_direct_read(iocb, iov, nr_segs, pos);
247 263
248 dfprintk(VFS, "nfs: read(%s/%s, %lu@%lu)\n", 264 dprintk("NFS: read(%s/%s, %lu@%lu)\n",
249 dentry->d_parent->d_name.name, dentry->d_name.name, 265 dentry->d_parent->d_name.name, dentry->d_name.name,
250 (unsigned long) count, (unsigned long) pos); 266 (unsigned long) count, (unsigned long) pos);
251 267
@@ -265,7 +281,7 @@ nfs_file_splice_read(struct file *filp, loff_t *ppos,
265 struct inode *inode = dentry->d_inode; 281 struct inode *inode = dentry->d_inode;
266 ssize_t res; 282 ssize_t res;
267 283
268 dfprintk(VFS, "nfs: splice_read(%s/%s, %lu@%Lu)\n", 284 dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
269 dentry->d_parent->d_name.name, dentry->d_name.name, 285 dentry->d_parent->d_name.name, dentry->d_name.name,
270 (unsigned long) count, (unsigned long long) *ppos); 286 (unsigned long) count, (unsigned long long) *ppos);
271 287
@@ -282,7 +298,7 @@ nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
282 struct inode *inode = dentry->d_inode; 298 struct inode *inode = dentry->d_inode;
283 int status; 299 int status;
284 300
285 dfprintk(VFS, "nfs: mmap(%s/%s)\n", 301 dprintk("NFS: mmap(%s/%s)\n",
286 dentry->d_parent->d_name.name, dentry->d_name.name); 302 dentry->d_parent->d_name.name, dentry->d_name.name);
287 303
288 status = nfs_revalidate_mapping(inode, file->f_mapping); 304 status = nfs_revalidate_mapping(inode, file->f_mapping);
@@ -300,12 +316,14 @@ nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
300 * whether any write errors occurred for this process. 316 * whether any write errors occurred for this process.
301 */ 317 */
302static int 318static int
303nfs_fsync(struct file *file, struct dentry *dentry, int datasync) 319nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
304{ 320{
305 struct nfs_open_context *ctx = nfs_file_open_context(file); 321 struct nfs_open_context *ctx = nfs_file_open_context(file);
306 struct inode *inode = dentry->d_inode; 322 struct inode *inode = dentry->d_inode;
307 323
308 dfprintk(VFS, "nfs: fsync(%s/%ld)\n", inode->i_sb->s_id, inode->i_ino); 324 dprintk("NFS: fsync file(%s/%s) datasync %d\n",
325 dentry->d_parent->d_name.name, dentry->d_name.name,
326 datasync);
309 327
310 nfs_inc_stats(inode, NFSIOS_VFSFSYNC); 328 nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
311 return nfs_do_fsync(ctx, inode); 329 return nfs_do_fsync(ctx, inode);
@@ -328,6 +346,11 @@ static int nfs_write_begin(struct file *file, struct address_space *mapping,
328 struct page *page; 346 struct page *page;
329 index = pos >> PAGE_CACHE_SHIFT; 347 index = pos >> PAGE_CACHE_SHIFT;
330 348
349 dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
350 file->f_path.dentry->d_parent->d_name.name,
351 file->f_path.dentry->d_name.name,
352 mapping->host->i_ino, len, (long long) pos);
353
331 page = __grab_cache_page(mapping, index); 354 page = __grab_cache_page(mapping, index);
332 if (!page) 355 if (!page)
333 return -ENOMEM; 356 return -ENOMEM;
@@ -348,9 +371,32 @@ static int nfs_write_end(struct file *file, struct address_space *mapping,
348 unsigned offset = pos & (PAGE_CACHE_SIZE - 1); 371 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
349 int status; 372 int status;
350 373
351 lock_kernel(); 374 dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
375 file->f_path.dentry->d_parent->d_name.name,
376 file->f_path.dentry->d_name.name,
377 mapping->host->i_ino, len, (long long) pos);
378
379 /*
380 * Zero any uninitialised parts of the page, and then mark the page
381 * as up to date if it turns out that we're extending the file.
382 */
383 if (!PageUptodate(page)) {
384 unsigned pglen = nfs_page_length(page);
385 unsigned end = offset + len;
386
387 if (pglen == 0) {
388 zero_user_segments(page, 0, offset,
389 end, PAGE_CACHE_SIZE);
390 SetPageUptodate(page);
391 } else if (end >= pglen) {
392 zero_user_segment(page, end, PAGE_CACHE_SIZE);
393 if (offset == 0)
394 SetPageUptodate(page);
395 } else
396 zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
397 }
398
352 status = nfs_updatepage(file, page, offset, copied); 399 status = nfs_updatepage(file, page, offset, copied);
353 unlock_kernel();
354 400
355 unlock_page(page); 401 unlock_page(page);
356 page_cache_release(page); 402 page_cache_release(page);
@@ -362,6 +408,8 @@ static int nfs_write_end(struct file *file, struct address_space *mapping,
362 408
363static void nfs_invalidate_page(struct page *page, unsigned long offset) 409static void nfs_invalidate_page(struct page *page, unsigned long offset)
364{ 410{
411 dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
412
365 if (offset != 0) 413 if (offset != 0)
366 return; 414 return;
367 /* Cancel any unstarted writes on this page */ 415 /* Cancel any unstarted writes on this page */
@@ -370,13 +418,20 @@ static void nfs_invalidate_page(struct page *page, unsigned long offset)
370 418
371static int nfs_release_page(struct page *page, gfp_t gfp) 419static int nfs_release_page(struct page *page, gfp_t gfp)
372{ 420{
421 dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
422
373 /* If PagePrivate() is set, then the page is not freeable */ 423 /* If PagePrivate() is set, then the page is not freeable */
374 return 0; 424 return 0;
375} 425}
376 426
377static int nfs_launder_page(struct page *page) 427static int nfs_launder_page(struct page *page)
378{ 428{
379 return nfs_wb_page(page->mapping->host, page); 429 struct inode *inode = page->mapping->host;
430
431 dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
432 inode->i_ino, (long long)page_offset(page));
433
434 return nfs_wb_page(inode, page);
380} 435}
381 436
382const struct address_space_operations nfs_file_aops = { 437const struct address_space_operations nfs_file_aops = {
@@ -396,13 +451,19 @@ const struct address_space_operations nfs_file_aops = {
396static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page) 451static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page)
397{ 452{
398 struct file *filp = vma->vm_file; 453 struct file *filp = vma->vm_file;
454 struct dentry *dentry = filp->f_path.dentry;
399 unsigned pagelen; 455 unsigned pagelen;
400 int ret = -EINVAL; 456 int ret = -EINVAL;
401 struct address_space *mapping; 457 struct address_space *mapping;
402 458
459 dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
460 dentry->d_parent->d_name.name, dentry->d_name.name,
461 filp->f_mapping->host->i_ino,
462 (long long)page_offset(page));
463
403 lock_page(page); 464 lock_page(page);
404 mapping = page->mapping; 465 mapping = page->mapping;
405 if (mapping != vma->vm_file->f_path.dentry->d_inode->i_mapping) 466 if (mapping != dentry->d_inode->i_mapping)
406 goto out_unlock; 467 goto out_unlock;
407 468
408 ret = 0; 469 ret = 0;
@@ -450,9 +511,9 @@ static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
450 if (iocb->ki_filp->f_flags & O_DIRECT) 511 if (iocb->ki_filp->f_flags & O_DIRECT)
451 return nfs_file_direct_write(iocb, iov, nr_segs, pos); 512 return nfs_file_direct_write(iocb, iov, nr_segs, pos);
452 513
453 dfprintk(VFS, "nfs: write(%s/%s(%ld), %lu@%Ld)\n", 514 dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
454 dentry->d_parent->d_name.name, dentry->d_name.name, 515 dentry->d_parent->d_name.name, dentry->d_name.name,
455 inode->i_ino, (unsigned long) count, (long long) pos); 516 (unsigned long) count, (long long) pos);
456 517
457 result = -EBUSY; 518 result = -EBUSY;
458 if (IS_SWAPFILE(inode)) 519 if (IS_SWAPFILE(inode))
@@ -586,7 +647,8 @@ static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
586 * This makes locking act as a cache coherency point. 647 * This makes locking act as a cache coherency point.
587 */ 648 */
588 nfs_sync_mapping(filp->f_mapping); 649 nfs_sync_mapping(filp->f_mapping);
589 nfs_zap_caches(inode); 650 if (!nfs_have_delegation(inode, FMODE_READ))
651 nfs_zap_caches(inode);
590out: 652out:
591 return status; 653 return status;
592} 654}
@@ -596,23 +658,35 @@ out:
596 */ 658 */
597static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl) 659static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
598{ 660{
599 struct inode * inode = filp->f_mapping->host; 661 struct inode *inode = filp->f_mapping->host;
662 int ret = -ENOLCK;
600 663
601 dprintk("NFS: nfs_lock(f=%s/%ld, t=%x, fl=%x, r=%Ld:%Ld)\n", 664 dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
602 inode->i_sb->s_id, inode->i_ino, 665 filp->f_path.dentry->d_parent->d_name.name,
666 filp->f_path.dentry->d_name.name,
603 fl->fl_type, fl->fl_flags, 667 fl->fl_type, fl->fl_flags,
604 (long long)fl->fl_start, (long long)fl->fl_end); 668 (long long)fl->fl_start, (long long)fl->fl_end);
669
605 nfs_inc_stats(inode, NFSIOS_VFSLOCK); 670 nfs_inc_stats(inode, NFSIOS_VFSLOCK);
606 671
607 /* No mandatory locks over NFS */ 672 /* No mandatory locks over NFS */
608 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 673 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
609 return -ENOLCK; 674 goto out_err;
675
676 if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
677 ret = NFS_PROTO(inode)->lock_check_bounds(fl);
678 if (ret < 0)
679 goto out_err;
680 }
610 681
611 if (IS_GETLK(cmd)) 682 if (IS_GETLK(cmd))
612 return do_getlk(filp, cmd, fl); 683 ret = do_getlk(filp, cmd, fl);
613 if (fl->fl_type == F_UNLCK) 684 else if (fl->fl_type == F_UNLCK)
614 return do_unlk(filp, cmd, fl); 685 ret = do_unlk(filp, cmd, fl);
615 return do_setlk(filp, cmd, fl); 686 else
687 ret = do_setlk(filp, cmd, fl);
688out_err:
689 return ret;
616} 690}
617 691
618/* 692/*
@@ -620,9 +694,9 @@ static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
620 */ 694 */
621static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl) 695static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
622{ 696{
623 dprintk("NFS: nfs_flock(f=%s/%ld, t=%x, fl=%x)\n", 697 dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
624 filp->f_path.dentry->d_inode->i_sb->s_id, 698 filp->f_path.dentry->d_parent->d_name.name,
625 filp->f_path.dentry->d_inode->i_ino, 699 filp->f_path.dentry->d_name.name,
626 fl->fl_type, fl->fl_flags); 700 fl->fl_type, fl->fl_flags);
627 701
628 /* 702 /*
@@ -645,12 +719,15 @@ static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
645 return do_setlk(filp, cmd, fl); 719 return do_setlk(filp, cmd, fl);
646} 720}
647 721
722/*
723 * There is no protocol support for leases, so we have no way to implement
724 * them correctly in the face of opens by other clients.
725 */
648static int nfs_setlease(struct file *file, long arg, struct file_lock **fl) 726static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
649{ 727{
650 /* 728 dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
651 * There is no protocol support for leases, so we have no way 729 file->f_path.dentry->d_parent->d_name.name,
652 * to implement them correctly in the face of opens by other 730 file->f_path.dentry->d_name.name, arg);
653 * clients. 731
654 */
655 return -EINVAL; 732 return -EINVAL;
656} 733}
diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c
index 596c5d8e86f4..df23f987da6b 100644
--- a/fs/nfs/inode.c
+++ b/fs/nfs/inode.c
@@ -57,8 +57,6 @@ static int enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
57static void nfs_invalidate_inode(struct inode *); 57static void nfs_invalidate_inode(struct inode *);
58static int nfs_update_inode(struct inode *, struct nfs_fattr *); 58static int nfs_update_inode(struct inode *, struct nfs_fattr *);
59 59
60static void nfs_zap_acl_cache(struct inode *);
61
62static struct kmem_cache * nfs_inode_cachep; 60static struct kmem_cache * nfs_inode_cachep;
63 61
64static inline unsigned long 62static inline unsigned long
@@ -167,7 +165,7 @@ void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
167 } 165 }
168} 166}
169 167
170static void nfs_zap_acl_cache(struct inode *inode) 168void nfs_zap_acl_cache(struct inode *inode)
171{ 169{
172 void (*clear_acl_cache)(struct inode *); 170 void (*clear_acl_cache)(struct inode *);
173 171
@@ -347,7 +345,7 @@ out_no_inode:
347 goto out; 345 goto out;
348} 346}
349 347
350#define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET) 348#define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE)
351 349
352int 350int
353nfs_setattr(struct dentry *dentry, struct iattr *attr) 351nfs_setattr(struct dentry *dentry, struct iattr *attr)
@@ -369,10 +367,9 @@ nfs_setattr(struct dentry *dentry, struct iattr *attr)
369 367
370 /* Optimization: if the end result is no change, don't RPC */ 368 /* Optimization: if the end result is no change, don't RPC */
371 attr->ia_valid &= NFS_VALID_ATTRS; 369 attr->ia_valid &= NFS_VALID_ATTRS;
372 if (attr->ia_valid == 0) 370 if ((attr->ia_valid & ~ATTR_FILE) == 0)
373 return 0; 371 return 0;
374 372
375 lock_kernel();
376 /* Write all dirty data */ 373 /* Write all dirty data */
377 if (S_ISREG(inode->i_mode)) { 374 if (S_ISREG(inode->i_mode)) {
378 filemap_write_and_wait(inode->i_mapping); 375 filemap_write_and_wait(inode->i_mapping);
@@ -386,11 +383,66 @@ nfs_setattr(struct dentry *dentry, struct iattr *attr)
386 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr); 383 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
387 if (error == 0) 384 if (error == 0)
388 nfs_refresh_inode(inode, &fattr); 385 nfs_refresh_inode(inode, &fattr);
389 unlock_kernel();
390 return error; 386 return error;
391} 387}
392 388
393/** 389/**
390 * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
391 * @inode: inode of the file used
392 * @offset: file offset to start truncating
393 *
394 * This is a copy of the common vmtruncate, but with the locking
395 * corrected to take into account the fact that NFS requires
396 * inode->i_size to be updated under the inode->i_lock.
397 */
398static int nfs_vmtruncate(struct inode * inode, loff_t offset)
399{
400 if (i_size_read(inode) < offset) {
401 unsigned long limit;
402
403 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
404 if (limit != RLIM_INFINITY && offset > limit)
405 goto out_sig;
406 if (offset > inode->i_sb->s_maxbytes)
407 goto out_big;
408 spin_lock(&inode->i_lock);
409 i_size_write(inode, offset);
410 spin_unlock(&inode->i_lock);
411 } else {
412 struct address_space *mapping = inode->i_mapping;
413
414 /*
415 * truncation of in-use swapfiles is disallowed - it would
416 * cause subsequent swapout to scribble on the now-freed
417 * blocks.
418 */
419 if (IS_SWAPFILE(inode))
420 return -ETXTBSY;
421 spin_lock(&inode->i_lock);
422 i_size_write(inode, offset);
423 spin_unlock(&inode->i_lock);
424
425 /*
426 * unmap_mapping_range is called twice, first simply for
427 * efficiency so that truncate_inode_pages does fewer
428 * single-page unmaps. However after this first call, and
429 * before truncate_inode_pages finishes, it is possible for
430 * private pages to be COWed, which remain after
431 * truncate_inode_pages finishes, hence the second
432 * unmap_mapping_range call must be made for correctness.
433 */
434 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
435 truncate_inode_pages(mapping, offset);
436 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
437 }
438 return 0;
439out_sig:
440 send_sig(SIGXFSZ, current, 0);
441out_big:
442 return -EFBIG;
443}
444
445/**
394 * nfs_setattr_update_inode - Update inode metadata after a setattr call. 446 * nfs_setattr_update_inode - Update inode metadata after a setattr call.
395 * @inode: pointer to struct inode 447 * @inode: pointer to struct inode
396 * @attr: pointer to struct iattr 448 * @attr: pointer to struct iattr
@@ -416,8 +468,7 @@ void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
416 } 468 }
417 if ((attr->ia_valid & ATTR_SIZE) != 0) { 469 if ((attr->ia_valid & ATTR_SIZE) != 0) {
418 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC); 470 nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
419 inode->i_size = attr->ia_size; 471 nfs_vmtruncate(inode, attr->ia_size);
420 vmtruncate(inode, attr->ia_size);
421 } 472 }
422} 473}
423 474
@@ -647,7 +698,6 @@ __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
647 inode->i_sb->s_id, (long long)NFS_FILEID(inode)); 698 inode->i_sb->s_id, (long long)NFS_FILEID(inode));
648 699
649 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE); 700 nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
650 lock_kernel();
651 if (is_bad_inode(inode)) 701 if (is_bad_inode(inode))
652 goto out_nowait; 702 goto out_nowait;
653 if (NFS_STALE(inode)) 703 if (NFS_STALE(inode))
@@ -696,7 +746,6 @@ __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
696 nfs_wake_up_inode(inode); 746 nfs_wake_up_inode(inode);
697 747
698 out_nowait: 748 out_nowait:
699 unlock_kernel();
700 return status; 749 return status;
701} 750}
702 751
@@ -831,9 +880,9 @@ static void nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
831 if (S_ISDIR(inode->i_mode)) 880 if (S_ISDIR(inode->i_mode))
832 nfsi->cache_validity |= NFS_INO_INVALID_DATA; 881 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
833 } 882 }
834 if (inode->i_size == nfs_size_to_loff_t(fattr->pre_size) && 883 if (i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size) &&
835 nfsi->npages == 0) 884 nfsi->npages == 0)
836 inode->i_size = nfs_size_to_loff_t(fattr->size); 885 i_size_write(inode, nfs_size_to_loff_t(fattr->size));
837 } 886 }
838} 887}
839 888
@@ -974,7 +1023,7 @@ int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fa
974 (fattr->valid & NFS_ATTR_WCC) == 0) { 1023 (fattr->valid & NFS_ATTR_WCC) == 0) {
975 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime)); 1024 memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
976 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime)); 1025 memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
977 fattr->pre_size = inode->i_size; 1026 fattr->pre_size = i_size_read(inode);
978 fattr->valid |= NFS_ATTR_WCC; 1027 fattr->valid |= NFS_ATTR_WCC;
979 } 1028 }
980 return nfs_post_op_update_inode(inode, fattr); 1029 return nfs_post_op_update_inode(inode, fattr);
@@ -1059,7 +1108,7 @@ static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
1059 /* Do we perhaps have any outstanding writes, or has 1108 /* Do we perhaps have any outstanding writes, or has
1060 * the file grown beyond our last write? */ 1109 * the file grown beyond our last write? */
1061 if (nfsi->npages == 0 || new_isize > cur_isize) { 1110 if (nfsi->npages == 0 || new_isize > cur_isize) {
1062 inode->i_size = new_isize; 1111 i_size_write(inode, new_isize);
1063 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA; 1112 invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1064 } 1113 }
1065 dprintk("NFS: isize change on server for file %s/%ld\n", 1114 dprintk("NFS: isize change on server for file %s/%ld\n",
diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h
index 04ae867dddba..24241fcbb98d 100644
--- a/fs/nfs/internal.h
+++ b/fs/nfs/internal.h
@@ -150,6 +150,7 @@ extern void nfs_clear_inode(struct inode *);
150#ifdef CONFIG_NFS_V4 150#ifdef CONFIG_NFS_V4
151extern void nfs4_clear_inode(struct inode *); 151extern void nfs4_clear_inode(struct inode *);
152#endif 152#endif
153void nfs_zap_acl_cache(struct inode *inode);
153 154
154/* super.c */ 155/* super.c */
155extern struct file_system_type nfs_xdev_fs_type; 156extern struct file_system_type nfs_xdev_fs_type;
diff --git a/fs/nfs/iostat.h b/fs/nfs/iostat.h
index 6350ecbde589..a36952810032 100644
--- a/fs/nfs/iostat.h
+++ b/fs/nfs/iostat.h
@@ -5,135 +5,41 @@
5 * 5 *
6 * Copyright (C) 2005, 2006 Chuck Lever <cel@netapp.com> 6 * Copyright (C) 2005, 2006 Chuck Lever <cel@netapp.com>
7 * 7 *
8 * NFS client per-mount statistics provide information about the health of
9 * the NFS client and the health of each NFS mount point. Generally these
10 * are not for detailed problem diagnosis, but simply to indicate that there
11 * is a problem.
12 *
13 * These counters are not meant to be human-readable, but are meant to be
14 * integrated into system monitoring tools such as "sar" and "iostat". As
15 * such, the counters are sampled by the tools over time, and are never
16 * zeroed after a file system is mounted. Moving averages can be computed
17 * by the tools by taking the difference between two instantaneous samples
18 * and dividing that by the time between the samples.
19 */ 8 */
20 9
21#ifndef _NFS_IOSTAT 10#ifndef _NFS_IOSTAT
22#define _NFS_IOSTAT 11#define _NFS_IOSTAT
23 12
24#define NFS_IOSTAT_VERS "1.0"
25
26/*
27 * NFS byte counters
28 *
29 * 1. SERVER - the number of payload bytes read from or written to the
30 * server by the NFS client via an NFS READ or WRITE request.
31 *
32 * 2. NORMAL - the number of bytes read or written by applications via
33 * the read(2) and write(2) system call interfaces.
34 *
35 * 3. DIRECT - the number of bytes read or written from files opened
36 * with the O_DIRECT flag.
37 *
38 * These counters give a view of the data throughput into and out of the NFS
39 * client. Comparing the number of bytes requested by an application with the
40 * number of bytes the client requests from the server can provide an
41 * indication of client efficiency (per-op, cache hits, etc).
42 *
43 * These counters can also help characterize which access methods are in
44 * use. DIRECT by itself shows whether there is any O_DIRECT traffic.
45 * NORMAL + DIRECT shows how much data is going through the system call
46 * interface. A large amount of SERVER traffic without much NORMAL or
47 * DIRECT traffic shows that applications are using mapped files.
48 *
49 * NFS page counters
50 *
51 * These count the number of pages read or written via nfs_readpage(),
52 * nfs_readpages(), or their write equivalents.
53 */
54enum nfs_stat_bytecounters {
55 NFSIOS_NORMALREADBYTES = 0,
56 NFSIOS_NORMALWRITTENBYTES,
57 NFSIOS_DIRECTREADBYTES,
58 NFSIOS_DIRECTWRITTENBYTES,
59 NFSIOS_SERVERREADBYTES,
60 NFSIOS_SERVERWRITTENBYTES,
61 NFSIOS_READPAGES,
62 NFSIOS_WRITEPAGES,
63 __NFSIOS_BYTESMAX,
64};
65
66/*
67 * NFS event counters
68 *
69 * These counters provide a low-overhead way of monitoring client activity
70 * without enabling NFS trace debugging. The counters show the rate at
71 * which VFS requests are made, and how often the client invalidates its
72 * data and attribute caches. This allows system administrators to monitor
73 * such things as how close-to-open is working, and answer questions such
74 * as "why are there so many GETATTR requests on the wire?"
75 *
76 * They also count anamolous events such as short reads and writes, silly
77 * renames due to close-after-delete, and operations that change the size
78 * of a file (such operations can often be the source of data corruption
79 * if applications aren't using file locking properly).
80 */
81enum nfs_stat_eventcounters {
82 NFSIOS_INODEREVALIDATE = 0,
83 NFSIOS_DENTRYREVALIDATE,
84 NFSIOS_DATAINVALIDATE,
85 NFSIOS_ATTRINVALIDATE,
86 NFSIOS_VFSOPEN,
87 NFSIOS_VFSLOOKUP,
88 NFSIOS_VFSACCESS,
89 NFSIOS_VFSUPDATEPAGE,
90 NFSIOS_VFSREADPAGE,
91 NFSIOS_VFSREADPAGES,
92 NFSIOS_VFSWRITEPAGE,
93 NFSIOS_VFSWRITEPAGES,
94 NFSIOS_VFSGETDENTS,
95 NFSIOS_VFSSETATTR,
96 NFSIOS_VFSFLUSH,
97 NFSIOS_VFSFSYNC,
98 NFSIOS_VFSLOCK,
99 NFSIOS_VFSRELEASE,
100 NFSIOS_CONGESTIONWAIT,
101 NFSIOS_SETATTRTRUNC,
102 NFSIOS_EXTENDWRITE,
103 NFSIOS_SILLYRENAME,
104 NFSIOS_SHORTREAD,
105 NFSIOS_SHORTWRITE,
106 NFSIOS_DELAY,
107 __NFSIOS_COUNTSMAX,
108};
109
110#ifdef __KERNEL__
111
112#include <linux/percpu.h> 13#include <linux/percpu.h>
113#include <linux/cache.h> 14#include <linux/cache.h>
15#include <linux/nfs_iostat.h>
114 16
115struct nfs_iostats { 17struct nfs_iostats {
116 unsigned long long bytes[__NFSIOS_BYTESMAX]; 18 unsigned long long bytes[__NFSIOS_BYTESMAX];
117 unsigned long events[__NFSIOS_COUNTSMAX]; 19 unsigned long events[__NFSIOS_COUNTSMAX];
118} ____cacheline_aligned; 20} ____cacheline_aligned;
119 21
120static inline void nfs_inc_server_stats(struct nfs_server *server, enum nfs_stat_eventcounters stat) 22static inline void nfs_inc_server_stats(const struct nfs_server *server,
23 enum nfs_stat_eventcounters stat)
121{ 24{
122 struct nfs_iostats *iostats; 25 struct nfs_iostats *iostats;
123 int cpu; 26 int cpu;
124 27
125 cpu = get_cpu(); 28 cpu = get_cpu();
126 iostats = per_cpu_ptr(server->io_stats, cpu); 29 iostats = per_cpu_ptr(server->io_stats, cpu);
127 iostats->events[stat] ++; 30 iostats->events[stat]++;
128 put_cpu_no_resched(); 31 put_cpu_no_resched();
129} 32}
130 33
131static inline void nfs_inc_stats(struct inode *inode, enum nfs_stat_eventcounters stat) 34static inline void nfs_inc_stats(const struct inode *inode,
35 enum nfs_stat_eventcounters stat)
132{ 36{
133 nfs_inc_server_stats(NFS_SERVER(inode), stat); 37 nfs_inc_server_stats(NFS_SERVER(inode), stat);
134} 38}
135 39
136static inline void nfs_add_server_stats(struct nfs_server *server, enum nfs_stat_bytecounters stat, unsigned long addend) 40static inline void nfs_add_server_stats(const struct nfs_server *server,
41 enum nfs_stat_bytecounters stat,
42 unsigned long addend)
137{ 43{
138 struct nfs_iostats *iostats; 44 struct nfs_iostats *iostats;
139 int cpu; 45 int cpu;
@@ -144,7 +50,9 @@ static inline void nfs_add_server_stats(struct nfs_server *server, enum nfs_stat
144 put_cpu_no_resched(); 50 put_cpu_no_resched();
145} 51}
146 52
147static inline void nfs_add_stats(struct inode *inode, enum nfs_stat_bytecounters stat, unsigned long addend) 53static inline void nfs_add_stats(const struct inode *inode,
54 enum nfs_stat_bytecounters stat,
55 unsigned long addend)
148{ 56{
149 nfs_add_server_stats(NFS_SERVER(inode), stat, addend); 57 nfs_add_server_stats(NFS_SERVER(inode), stat, addend);
150} 58}
@@ -160,5 +68,4 @@ static inline void nfs_free_iostats(struct nfs_iostats *stats)
160 free_percpu(stats); 68 free_percpu(stats);
161} 69}
162 70
163#endif 71#endif /* _NFS_IOSTAT */
164#endif
diff --git a/fs/nfs/nfs3acl.c b/fs/nfs/nfs3acl.c
index 9b7362565c0c..423842f51ac9 100644
--- a/fs/nfs/nfs3acl.c
+++ b/fs/nfs/nfs3acl.c
@@ -5,6 +5,8 @@
5#include <linux/posix_acl_xattr.h> 5#include <linux/posix_acl_xattr.h>
6#include <linux/nfsacl.h> 6#include <linux/nfsacl.h>
7 7
8#include "internal.h"
9
8#define NFSDBG_FACILITY NFSDBG_PROC 10#define NFSDBG_FACILITY NFSDBG_PROC
9 11
10ssize_t nfs3_listxattr(struct dentry *dentry, char *buffer, size_t size) 12ssize_t nfs3_listxattr(struct dentry *dentry, char *buffer, size_t size)
@@ -205,6 +207,8 @@ struct posix_acl *nfs3_proc_getacl(struct inode *inode, int type)
205 status = nfs_revalidate_inode(server, inode); 207 status = nfs_revalidate_inode(server, inode);
206 if (status < 0) 208 if (status < 0)
207 return ERR_PTR(status); 209 return ERR_PTR(status);
210 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
211 nfs_zap_acl_cache(inode);
208 acl = nfs3_get_cached_acl(inode, type); 212 acl = nfs3_get_cached_acl(inode, type);
209 if (acl != ERR_PTR(-EAGAIN)) 213 if (acl != ERR_PTR(-EAGAIN))
210 return acl; 214 return acl;
@@ -319,9 +323,8 @@ static int nfs3_proc_setacls(struct inode *inode, struct posix_acl *acl,
319 dprintk("NFS call setacl\n"); 323 dprintk("NFS call setacl\n");
320 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL]; 324 msg.rpc_proc = &server->client_acl->cl_procinfo[ACLPROC3_SETACL];
321 status = rpc_call_sync(server->client_acl, &msg, 0); 325 status = rpc_call_sync(server->client_acl, &msg, 0);
322 spin_lock(&inode->i_lock); 326 nfs_access_zap_cache(inode);
323 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS; 327 nfs_zap_acl_cache(inode);
324 spin_unlock(&inode->i_lock);
325 dprintk("NFS reply setacl: %d\n", status); 328 dprintk("NFS reply setacl: %d\n", status);
326 329
327 /* pages may have been allocated at the xdr layer. */ 330 /* pages may have been allocated at the xdr layer. */
diff --git a/fs/nfs/nfs3proc.c b/fs/nfs/nfs3proc.c
index c3523ad03ed1..1e750e4574a9 100644
--- a/fs/nfs/nfs3proc.c
+++ b/fs/nfs/nfs3proc.c
@@ -129,6 +129,8 @@ nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
129 int status; 129 int status;
130 130
131 dprintk("NFS call setattr\n"); 131 dprintk("NFS call setattr\n");
132 if (sattr->ia_valid & ATTR_FILE)
133 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
132 nfs_fattr_init(fattr); 134 nfs_fattr_init(fattr);
133 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); 135 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
134 if (status == 0) 136 if (status == 0)
@@ -248,6 +250,53 @@ static int nfs3_proc_readlink(struct inode *inode, struct page *page,
248 return status; 250 return status;
249} 251}
250 252
253struct nfs3_createdata {
254 struct rpc_message msg;
255 union {
256 struct nfs3_createargs create;
257 struct nfs3_mkdirargs mkdir;
258 struct nfs3_symlinkargs symlink;
259 struct nfs3_mknodargs mknod;
260 } arg;
261 struct nfs3_diropres res;
262 struct nfs_fh fh;
263 struct nfs_fattr fattr;
264 struct nfs_fattr dir_attr;
265};
266
267static struct nfs3_createdata *nfs3_alloc_createdata(void)
268{
269 struct nfs3_createdata *data;
270
271 data = kzalloc(sizeof(*data), GFP_KERNEL);
272 if (data != NULL) {
273 data->msg.rpc_argp = &data->arg;
274 data->msg.rpc_resp = &data->res;
275 data->res.fh = &data->fh;
276 data->res.fattr = &data->fattr;
277 data->res.dir_attr = &data->dir_attr;
278 nfs_fattr_init(data->res.fattr);
279 nfs_fattr_init(data->res.dir_attr);
280 }
281 return data;
282}
283
284static int nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
285{
286 int status;
287
288 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
289 nfs_post_op_update_inode(dir, data->res.dir_attr);
290 if (status == 0)
291 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
292 return status;
293}
294
295static void nfs3_free_createdata(struct nfs3_createdata *data)
296{
297 kfree(data);
298}
299
251/* 300/*
252 * Create a regular file. 301 * Create a regular file.
253 * For now, we don't implement O_EXCL. 302 * For now, we don't implement O_EXCL.
@@ -256,70 +305,60 @@ static int
256nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 305nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
257 int flags, struct nameidata *nd) 306 int flags, struct nameidata *nd)
258{ 307{
259 struct nfs_fh fhandle; 308 struct nfs3_createdata *data;
260 struct nfs_fattr fattr;
261 struct nfs_fattr dir_attr;
262 struct nfs3_createargs arg = {
263 .fh = NFS_FH(dir),
264 .name = dentry->d_name.name,
265 .len = dentry->d_name.len,
266 .sattr = sattr,
267 };
268 struct nfs3_diropres res = {
269 .dir_attr = &dir_attr,
270 .fh = &fhandle,
271 .fattr = &fattr
272 };
273 struct rpc_message msg = {
274 .rpc_proc = &nfs3_procedures[NFS3PROC_CREATE],
275 .rpc_argp = &arg,
276 .rpc_resp = &res,
277 };
278 mode_t mode = sattr->ia_mode; 309 mode_t mode = sattr->ia_mode;
279 int status; 310 int status = -ENOMEM;
280 311
281 dprintk("NFS call create %s\n", dentry->d_name.name); 312 dprintk("NFS call create %s\n", dentry->d_name.name);
282 arg.createmode = NFS3_CREATE_UNCHECKED; 313
314 data = nfs3_alloc_createdata();
315 if (data == NULL)
316 goto out;
317
318 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
319 data->arg.create.fh = NFS_FH(dir);
320 data->arg.create.name = dentry->d_name.name;
321 data->arg.create.len = dentry->d_name.len;
322 data->arg.create.sattr = sattr;
323
324 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
283 if (flags & O_EXCL) { 325 if (flags & O_EXCL) {
284 arg.createmode = NFS3_CREATE_EXCLUSIVE; 326 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
285 arg.verifier[0] = jiffies; 327 data->arg.create.verifier[0] = jiffies;
286 arg.verifier[1] = current->pid; 328 data->arg.create.verifier[1] = current->pid;
287 } 329 }
288 330
289 sattr->ia_mode &= ~current->fs->umask; 331 sattr->ia_mode &= ~current->fs->umask;
290 332
291again: 333 for (;;) {
292 nfs_fattr_init(&dir_attr); 334 status = nfs3_do_create(dir, dentry, data);
293 nfs_fattr_init(&fattr);
294 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
295 nfs_refresh_inode(dir, &dir_attr);
296 335
297 /* If the server doesn't support the exclusive creation semantics, 336 if (status != -ENOTSUPP)
298 * try again with simple 'guarded' mode. */ 337 break;
299 if (status == -ENOTSUPP) { 338 /* If the server doesn't support the exclusive creation
300 switch (arg.createmode) { 339 * semantics, try again with simple 'guarded' mode. */
340 switch (data->arg.create.createmode) {
301 case NFS3_CREATE_EXCLUSIVE: 341 case NFS3_CREATE_EXCLUSIVE:
302 arg.createmode = NFS3_CREATE_GUARDED; 342 data->arg.create.createmode = NFS3_CREATE_GUARDED;
303 break; 343 break;
304 344
305 case NFS3_CREATE_GUARDED: 345 case NFS3_CREATE_GUARDED:
306 arg.createmode = NFS3_CREATE_UNCHECKED; 346 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
307 break; 347 break;
308 348
309 case NFS3_CREATE_UNCHECKED: 349 case NFS3_CREATE_UNCHECKED:
310 goto out; 350 goto out;
311 } 351 }
312 goto again; 352 nfs_fattr_init(data->res.dir_attr);
353 nfs_fattr_init(data->res.fattr);
313 } 354 }
314 355
315 if (status == 0)
316 status = nfs_instantiate(dentry, &fhandle, &fattr);
317 if (status != 0) 356 if (status != 0)
318 goto out; 357 goto out;
319 358
320 /* When we created the file with exclusive semantics, make 359 /* When we created the file with exclusive semantics, make
321 * sure we set the attributes afterwards. */ 360 * sure we set the attributes afterwards. */
322 if (arg.createmode == NFS3_CREATE_EXCLUSIVE) { 361 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
323 dprintk("NFS call setattr (post-create)\n"); 362 dprintk("NFS call setattr (post-create)\n");
324 363
325 if (!(sattr->ia_valid & ATTR_ATIME_SET)) 364 if (!(sattr->ia_valid & ATTR_ATIME_SET))
@@ -330,14 +369,15 @@ again:
330 /* Note: we could use a guarded setattr here, but I'm 369 /* Note: we could use a guarded setattr here, but I'm
331 * not sure this buys us anything (and I'd have 370 * not sure this buys us anything (and I'd have
332 * to revamp the NFSv3 XDR code) */ 371 * to revamp the NFSv3 XDR code) */
333 status = nfs3_proc_setattr(dentry, &fattr, sattr); 372 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
334 nfs_post_op_update_inode(dentry->d_inode, &fattr); 373 nfs_post_op_update_inode(dentry->d_inode, data->res.fattr);
335 dprintk("NFS reply setattr (post-create): %d\n", status); 374 dprintk("NFS reply setattr (post-create): %d\n", status);
375 if (status != 0)
376 goto out;
336 } 377 }
337 if (status != 0)
338 goto out;
339 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode); 378 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
340out: 379out:
380 nfs3_free_createdata(data);
341 dprintk("NFS reply create: %d\n", status); 381 dprintk("NFS reply create: %d\n", status);
342 return status; 382 return status;
343} 383}
@@ -452,40 +492,28 @@ static int
452nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page, 492nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
453 unsigned int len, struct iattr *sattr) 493 unsigned int len, struct iattr *sattr)
454{ 494{
455 struct nfs_fh fhandle; 495 struct nfs3_createdata *data;
456 struct nfs_fattr fattr, dir_attr; 496 int status = -ENOMEM;
457 struct nfs3_symlinkargs arg = {
458 .fromfh = NFS_FH(dir),
459 .fromname = dentry->d_name.name,
460 .fromlen = dentry->d_name.len,
461 .pages = &page,
462 .pathlen = len,
463 .sattr = sattr
464 };
465 struct nfs3_diropres res = {
466 .dir_attr = &dir_attr,
467 .fh = &fhandle,
468 .fattr = &fattr
469 };
470 struct rpc_message msg = {
471 .rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK],
472 .rpc_argp = &arg,
473 .rpc_resp = &res,
474 };
475 int status;
476 497
477 if (len > NFS3_MAXPATHLEN) 498 if (len > NFS3_MAXPATHLEN)
478 return -ENAMETOOLONG; 499 return -ENAMETOOLONG;
479 500
480 dprintk("NFS call symlink %s\n", dentry->d_name.name); 501 dprintk("NFS call symlink %s\n", dentry->d_name.name);
481 502
482 nfs_fattr_init(&dir_attr); 503 data = nfs3_alloc_createdata();
483 nfs_fattr_init(&fattr); 504 if (data == NULL)
484 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
485 nfs_post_op_update_inode(dir, &dir_attr);
486 if (status != 0)
487 goto out; 505 goto out;
488 status = nfs_instantiate(dentry, &fhandle, &fattr); 506 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
507 data->arg.symlink.fromfh = NFS_FH(dir);
508 data->arg.symlink.fromname = dentry->d_name.name;
509 data->arg.symlink.fromlen = dentry->d_name.len;
510 data->arg.symlink.pages = &page;
511 data->arg.symlink.pathlen = len;
512 data->arg.symlink.sattr = sattr;
513
514 status = nfs3_do_create(dir, dentry, data);
515
516 nfs3_free_createdata(data);
489out: 517out:
490 dprintk("NFS reply symlink: %d\n", status); 518 dprintk("NFS reply symlink: %d\n", status);
491 return status; 519 return status;
@@ -494,42 +522,31 @@ out:
494static int 522static int
495nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr) 523nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
496{ 524{
497 struct nfs_fh fhandle; 525 struct nfs3_createdata *data;
498 struct nfs_fattr fattr, dir_attr;
499 struct nfs3_mkdirargs arg = {
500 .fh = NFS_FH(dir),
501 .name = dentry->d_name.name,
502 .len = dentry->d_name.len,
503 .sattr = sattr
504 };
505 struct nfs3_diropres res = {
506 .dir_attr = &dir_attr,
507 .fh = &fhandle,
508 .fattr = &fattr
509 };
510 struct rpc_message msg = {
511 .rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR],
512 .rpc_argp = &arg,
513 .rpc_resp = &res,
514 };
515 int mode = sattr->ia_mode; 526 int mode = sattr->ia_mode;
516 int status; 527 int status = -ENOMEM;
517 528
518 dprintk("NFS call mkdir %s\n", dentry->d_name.name); 529 dprintk("NFS call mkdir %s\n", dentry->d_name.name);
519 530
520 sattr->ia_mode &= ~current->fs->umask; 531 sattr->ia_mode &= ~current->fs->umask;
521 532
522 nfs_fattr_init(&dir_attr); 533 data = nfs3_alloc_createdata();
523 nfs_fattr_init(&fattr); 534 if (data == NULL)
524 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
525 nfs_post_op_update_inode(dir, &dir_attr);
526 if (status != 0)
527 goto out; 535 goto out;
528 status = nfs_instantiate(dentry, &fhandle, &fattr); 536
537 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
538 data->arg.mkdir.fh = NFS_FH(dir);
539 data->arg.mkdir.name = dentry->d_name.name;
540 data->arg.mkdir.len = dentry->d_name.len;
541 data->arg.mkdir.sattr = sattr;
542
543 status = nfs3_do_create(dir, dentry, data);
529 if (status != 0) 544 if (status != 0)
530 goto out; 545 goto out;
546
531 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode); 547 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
532out: 548out:
549 nfs3_free_createdata(data);
533 dprintk("NFS reply mkdir: %d\n", status); 550 dprintk("NFS reply mkdir: %d\n", status);
534 return status; 551 return status;
535} 552}
@@ -615,52 +632,50 @@ static int
615nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 632nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
616 dev_t rdev) 633 dev_t rdev)
617{ 634{
618 struct nfs_fh fh; 635 struct nfs3_createdata *data;
619 struct nfs_fattr fattr, dir_attr;
620 struct nfs3_mknodargs arg = {
621 .fh = NFS_FH(dir),
622 .name = dentry->d_name.name,
623 .len = dentry->d_name.len,
624 .sattr = sattr,
625 .rdev = rdev
626 };
627 struct nfs3_diropres res = {
628 .dir_attr = &dir_attr,
629 .fh = &fh,
630 .fattr = &fattr
631 };
632 struct rpc_message msg = {
633 .rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD],
634 .rpc_argp = &arg,
635 .rpc_resp = &res,
636 };
637 mode_t mode = sattr->ia_mode; 636 mode_t mode = sattr->ia_mode;
638 int status; 637 int status = -ENOMEM;
639
640 switch (sattr->ia_mode & S_IFMT) {
641 case S_IFBLK: arg.type = NF3BLK; break;
642 case S_IFCHR: arg.type = NF3CHR; break;
643 case S_IFIFO: arg.type = NF3FIFO; break;
644 case S_IFSOCK: arg.type = NF3SOCK; break;
645 default: return -EINVAL;
646 }
647 638
648 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name, 639 dprintk("NFS call mknod %s %u:%u\n", dentry->d_name.name,
649 MAJOR(rdev), MINOR(rdev)); 640 MAJOR(rdev), MINOR(rdev));
650 641
651 sattr->ia_mode &= ~current->fs->umask; 642 sattr->ia_mode &= ~current->fs->umask;
652 643
653 nfs_fattr_init(&dir_attr); 644 data = nfs3_alloc_createdata();
654 nfs_fattr_init(&fattr); 645 if (data == NULL)
655 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
656 nfs_post_op_update_inode(dir, &dir_attr);
657 if (status != 0)
658 goto out; 646 goto out;
659 status = nfs_instantiate(dentry, &fh, &fattr); 647
648 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
649 data->arg.mknod.fh = NFS_FH(dir);
650 data->arg.mknod.name = dentry->d_name.name;
651 data->arg.mknod.len = dentry->d_name.len;
652 data->arg.mknod.sattr = sattr;
653 data->arg.mknod.rdev = rdev;
654
655 switch (sattr->ia_mode & S_IFMT) {
656 case S_IFBLK:
657 data->arg.mknod.type = NF3BLK;
658 break;
659 case S_IFCHR:
660 data->arg.mknod.type = NF3CHR;
661 break;
662 case S_IFIFO:
663 data->arg.mknod.type = NF3FIFO;
664 break;
665 case S_IFSOCK:
666 data->arg.mknod.type = NF3SOCK;
667 break;
668 default:
669 status = -EINVAL;
670 goto out;
671 }
672
673 status = nfs3_do_create(dir, dentry, data);
660 if (status != 0) 674 if (status != 0)
661 goto out; 675 goto out;
662 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode); 676 status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
663out: 677out:
678 nfs3_free_createdata(data);
664 dprintk("NFS reply mknod: %d\n", status); 679 dprintk("NFS reply mknod: %d\n", status);
665 return status; 680 return status;
666} 681}
@@ -801,8 +816,6 @@ const struct nfs_rpc_ops nfs_v3_clientops = {
801 .write_done = nfs3_write_done, 816 .write_done = nfs3_write_done,
802 .commit_setup = nfs3_proc_commit_setup, 817 .commit_setup = nfs3_proc_commit_setup,
803 .commit_done = nfs3_commit_done, 818 .commit_done = nfs3_commit_done,
804 .file_open = nfs_open,
805 .file_release = nfs_release,
806 .lock = nfs3_proc_lock, 819 .lock = nfs3_proc_lock,
807 .clear_acl_cache = nfs3_forget_cached_acls, 820 .clear_acl_cache = nfs3_forget_cached_acls,
808}; 821};
diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
index 1293e0acd82b..c910413eaeca 100644
--- a/fs/nfs/nfs4proc.c
+++ b/fs/nfs/nfs4proc.c
@@ -451,9 +451,7 @@ static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
451 /* Save the delegation */ 451 /* Save the delegation */
452 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data)); 452 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
453 rcu_read_unlock(); 453 rcu_read_unlock();
454 lock_kernel();
455 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); 454 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
456 unlock_kernel();
457 if (ret != 0) 455 if (ret != 0)
458 goto out; 456 goto out;
459 ret = -EAGAIN; 457 ret = -EAGAIN;
@@ -1139,8 +1137,9 @@ static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int
1139 return res; 1137 return res;
1140} 1138}
1141 1139
1142static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr, 1140static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1143 struct iattr *sattr, struct nfs4_state *state) 1141 struct nfs_fattr *fattr, struct iattr *sattr,
1142 struct nfs4_state *state)
1144{ 1143{
1145 struct nfs_server *server = NFS_SERVER(inode); 1144 struct nfs_server *server = NFS_SERVER(inode);
1146 struct nfs_setattrargs arg = { 1145 struct nfs_setattrargs arg = {
@@ -1154,9 +1153,10 @@ static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1154 .server = server, 1153 .server = server,
1155 }; 1154 };
1156 struct rpc_message msg = { 1155 struct rpc_message msg = {
1157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], 1156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1158 .rpc_argp = &arg, 1157 .rpc_argp = &arg,
1159 .rpc_resp = &res, 1158 .rpc_resp = &res,
1159 .rpc_cred = cred,
1160 }; 1160 };
1161 unsigned long timestamp = jiffies; 1161 unsigned long timestamp = jiffies;
1162 int status; 1162 int status;
@@ -1166,7 +1166,6 @@ static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1166 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) { 1166 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1167 /* Use that stateid */ 1167 /* Use that stateid */
1168 } else if (state != NULL) { 1168 } else if (state != NULL) {
1169 msg.rpc_cred = state->owner->so_cred;
1170 nfs4_copy_stateid(&arg.stateid, state, current->files); 1169 nfs4_copy_stateid(&arg.stateid, state, current->files);
1171 } else 1170 } else
1172 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid)); 1171 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
@@ -1177,15 +1176,16 @@ static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr,
1177 return status; 1176 return status;
1178} 1177}
1179 1178
1180static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr, 1179static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1181 struct iattr *sattr, struct nfs4_state *state) 1180 struct nfs_fattr *fattr, struct iattr *sattr,
1181 struct nfs4_state *state)
1182{ 1182{
1183 struct nfs_server *server = NFS_SERVER(inode); 1183 struct nfs_server *server = NFS_SERVER(inode);
1184 struct nfs4_exception exception = { }; 1184 struct nfs4_exception exception = { };
1185 int err; 1185 int err;
1186 do { 1186 do {
1187 err = nfs4_handle_exception(server, 1187 err = nfs4_handle_exception(server,
1188 _nfs4_do_setattr(inode, fattr, sattr, state), 1188 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1189 &exception); 1189 &exception);
1190 } while (exception.retry); 1190 } while (exception.retry);
1191 return err; 1191 return err;
@@ -1647,29 +1647,25 @@ static int
1647nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, 1647nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1648 struct iattr *sattr) 1648 struct iattr *sattr)
1649{ 1649{
1650 struct rpc_cred *cred;
1651 struct inode *inode = dentry->d_inode; 1650 struct inode *inode = dentry->d_inode;
1652 struct nfs_open_context *ctx; 1651 struct rpc_cred *cred = NULL;
1653 struct nfs4_state *state = NULL; 1652 struct nfs4_state *state = NULL;
1654 int status; 1653 int status;
1655 1654
1656 nfs_fattr_init(fattr); 1655 nfs_fattr_init(fattr);
1657 1656
1658 cred = rpc_lookup_cred();
1659 if (IS_ERR(cred))
1660 return PTR_ERR(cred);
1661
1662 /* Search for an existing open(O_WRITE) file */ 1657 /* Search for an existing open(O_WRITE) file */
1663 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE); 1658 if (sattr->ia_valid & ATTR_FILE) {
1664 if (ctx != NULL) 1659 struct nfs_open_context *ctx;
1660
1661 ctx = nfs_file_open_context(sattr->ia_file);
1662 cred = ctx->cred;
1665 state = ctx->state; 1663 state = ctx->state;
1664 }
1666 1665
1667 status = nfs4_do_setattr(inode, fattr, sattr, state); 1666 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
1668 if (status == 0) 1667 if (status == 0)
1669 nfs_setattr_update_inode(inode, sattr); 1668 nfs_setattr_update_inode(inode, sattr);
1670 if (ctx != NULL)
1671 put_nfs_open_context(ctx);
1672 put_rpccred(cred);
1673 return status; 1669 return status;
1674} 1670}
1675 1671
@@ -1897,17 +1893,16 @@ nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1897 goto out; 1893 goto out;
1898 } 1894 }
1899 state = nfs4_do_open(dir, &path, flags, sattr, cred); 1895 state = nfs4_do_open(dir, &path, flags, sattr, cred);
1900 put_rpccred(cred);
1901 d_drop(dentry); 1896 d_drop(dentry);
1902 if (IS_ERR(state)) { 1897 if (IS_ERR(state)) {
1903 status = PTR_ERR(state); 1898 status = PTR_ERR(state);
1904 goto out; 1899 goto out_putcred;
1905 } 1900 }
1906 d_add(dentry, igrab(state->inode)); 1901 d_add(dentry, igrab(state->inode));
1907 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 1902 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1908 if (flags & O_EXCL) { 1903 if (flags & O_EXCL) {
1909 struct nfs_fattr fattr; 1904 struct nfs_fattr fattr;
1910 status = nfs4_do_setattr(state->inode, &fattr, sattr, state); 1905 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
1911 if (status == 0) 1906 if (status == 0)
1912 nfs_setattr_update_inode(state->inode, sattr); 1907 nfs_setattr_update_inode(state->inode, sattr);
1913 nfs_post_op_update_inode(state->inode, &fattr); 1908 nfs_post_op_update_inode(state->inode, &fattr);
@@ -1916,6 +1911,8 @@ nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1916 status = nfs4_intent_set_file(nd, &path, state); 1911 status = nfs4_intent_set_file(nd, &path, state);
1917 else 1912 else
1918 nfs4_close_sync(&path, state, flags); 1913 nfs4_close_sync(&path, state, flags);
1914out_putcred:
1915 put_rpccred(cred);
1919out: 1916out:
1920 return status; 1917 return status;
1921} 1918}
@@ -2079,47 +2076,81 @@ static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *n
2079 return err; 2076 return err;
2080} 2077}
2081 2078
2079struct nfs4_createdata {
2080 struct rpc_message msg;
2081 struct nfs4_create_arg arg;
2082 struct nfs4_create_res res;
2083 struct nfs_fh fh;
2084 struct nfs_fattr fattr;
2085 struct nfs_fattr dir_fattr;
2086};
2087
2088static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2089 struct qstr *name, struct iattr *sattr, u32 ftype)
2090{
2091 struct nfs4_createdata *data;
2092
2093 data = kzalloc(sizeof(*data), GFP_KERNEL);
2094 if (data != NULL) {
2095 struct nfs_server *server = NFS_SERVER(dir);
2096
2097 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2098 data->msg.rpc_argp = &data->arg;
2099 data->msg.rpc_resp = &data->res;
2100 data->arg.dir_fh = NFS_FH(dir);
2101 data->arg.server = server;
2102 data->arg.name = name;
2103 data->arg.attrs = sattr;
2104 data->arg.ftype = ftype;
2105 data->arg.bitmask = server->attr_bitmask;
2106 data->res.server = server;
2107 data->res.fh = &data->fh;
2108 data->res.fattr = &data->fattr;
2109 data->res.dir_fattr = &data->dir_fattr;
2110 nfs_fattr_init(data->res.fattr);
2111 nfs_fattr_init(data->res.dir_fattr);
2112 }
2113 return data;
2114}
2115
2116static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2117{
2118 int status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
2119 if (status == 0) {
2120 update_changeattr(dir, &data->res.dir_cinfo);
2121 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2122 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2123 }
2124 return status;
2125}
2126
2127static void nfs4_free_createdata(struct nfs4_createdata *data)
2128{
2129 kfree(data);
2130}
2131
2082static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, 2132static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2083 struct page *page, unsigned int len, struct iattr *sattr) 2133 struct page *page, unsigned int len, struct iattr *sattr)
2084{ 2134{
2085 struct nfs_server *server = NFS_SERVER(dir); 2135 struct nfs4_createdata *data;
2086 struct nfs_fh fhandle; 2136 int status = -ENAMETOOLONG;
2087 struct nfs_fattr fattr, dir_fattr;
2088 struct nfs4_create_arg arg = {
2089 .dir_fh = NFS_FH(dir),
2090 .server = server,
2091 .name = &dentry->d_name,
2092 .attrs = sattr,
2093 .ftype = NF4LNK,
2094 .bitmask = server->attr_bitmask,
2095 };
2096 struct nfs4_create_res res = {
2097 .server = server,
2098 .fh = &fhandle,
2099 .fattr = &fattr,
2100 .dir_fattr = &dir_fattr,
2101 };
2102 struct rpc_message msg = {
2103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
2104 .rpc_argp = &arg,
2105 .rpc_resp = &res,
2106 };
2107 int status;
2108 2137
2109 if (len > NFS4_MAXPATHLEN) 2138 if (len > NFS4_MAXPATHLEN)
2110 return -ENAMETOOLONG; 2139 goto out;
2111 2140
2112 arg.u.symlink.pages = &page; 2141 status = -ENOMEM;
2113 arg.u.symlink.len = len; 2142 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2114 nfs_fattr_init(&fattr); 2143 if (data == NULL)
2115 nfs_fattr_init(&dir_fattr); 2144 goto out;
2145
2146 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2147 data->arg.u.symlink.pages = &page;
2148 data->arg.u.symlink.len = len;
2116 2149
2117 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 2150 status = nfs4_do_create(dir, dentry, data);
2118 if (!status) { 2151
2119 update_changeattr(dir, &res.dir_cinfo); 2152 nfs4_free_createdata(data);
2120 nfs_post_op_update_inode(dir, res.dir_fattr); 2153out:
2121 status = nfs_instantiate(dentry, &fhandle, &fattr);
2122 }
2123 return status; 2154 return status;
2124} 2155}
2125 2156
@@ -2140,39 +2171,17 @@ static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2140static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, 2171static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2141 struct iattr *sattr) 2172 struct iattr *sattr)
2142{ 2173{
2143 struct nfs_server *server = NFS_SERVER(dir); 2174 struct nfs4_createdata *data;
2144 struct nfs_fh fhandle; 2175 int status = -ENOMEM;
2145 struct nfs_fattr fattr, dir_fattr;
2146 struct nfs4_create_arg arg = {
2147 .dir_fh = NFS_FH(dir),
2148 .server = server,
2149 .name = &dentry->d_name,
2150 .attrs = sattr,
2151 .ftype = NF4DIR,
2152 .bitmask = server->attr_bitmask,
2153 };
2154 struct nfs4_create_res res = {
2155 .server = server,
2156 .fh = &fhandle,
2157 .fattr = &fattr,
2158 .dir_fattr = &dir_fattr,
2159 };
2160 struct rpc_message msg = {
2161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2162 .rpc_argp = &arg,
2163 .rpc_resp = &res,
2164 };
2165 int status;
2166 2176
2167 nfs_fattr_init(&fattr); 2177 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2168 nfs_fattr_init(&dir_fattr); 2178 if (data == NULL)
2169 2179 goto out;
2170 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 2180
2171 if (!status) { 2181 status = nfs4_do_create(dir, dentry, data);
2172 update_changeattr(dir, &res.dir_cinfo); 2182
2173 nfs_post_op_update_inode(dir, res.dir_fattr); 2183 nfs4_free_createdata(data);
2174 status = nfs_instantiate(dentry, &fhandle, &fattr); 2184out:
2175 }
2176 return status; 2185 return status;
2177} 2186}
2178 2187
@@ -2242,56 +2251,34 @@ static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2242static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, 2251static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2243 struct iattr *sattr, dev_t rdev) 2252 struct iattr *sattr, dev_t rdev)
2244{ 2253{
2245 struct nfs_server *server = NFS_SERVER(dir); 2254 struct nfs4_createdata *data;
2246 struct nfs_fh fh; 2255 int mode = sattr->ia_mode;
2247 struct nfs_fattr fattr, dir_fattr; 2256 int status = -ENOMEM;
2248 struct nfs4_create_arg arg = {
2249 .dir_fh = NFS_FH(dir),
2250 .server = server,
2251 .name = &dentry->d_name,
2252 .attrs = sattr,
2253 .bitmask = server->attr_bitmask,
2254 };
2255 struct nfs4_create_res res = {
2256 .server = server,
2257 .fh = &fh,
2258 .fattr = &fattr,
2259 .dir_fattr = &dir_fattr,
2260 };
2261 struct rpc_message msg = {
2262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2263 .rpc_argp = &arg,
2264 .rpc_resp = &res,
2265 };
2266 int status;
2267 int mode = sattr->ia_mode;
2268
2269 nfs_fattr_init(&fattr);
2270 nfs_fattr_init(&dir_fattr);
2271 2257
2272 BUG_ON(!(sattr->ia_valid & ATTR_MODE)); 2258 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2273 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode)); 2259 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2260
2261 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2262 if (data == NULL)
2263 goto out;
2264
2274 if (S_ISFIFO(mode)) 2265 if (S_ISFIFO(mode))
2275 arg.ftype = NF4FIFO; 2266 data->arg.ftype = NF4FIFO;
2276 else if (S_ISBLK(mode)) { 2267 else if (S_ISBLK(mode)) {
2277 arg.ftype = NF4BLK; 2268 data->arg.ftype = NF4BLK;
2278 arg.u.device.specdata1 = MAJOR(rdev); 2269 data->arg.u.device.specdata1 = MAJOR(rdev);
2279 arg.u.device.specdata2 = MINOR(rdev); 2270 data->arg.u.device.specdata2 = MINOR(rdev);
2280 } 2271 }
2281 else if (S_ISCHR(mode)) { 2272 else if (S_ISCHR(mode)) {
2282 arg.ftype = NF4CHR; 2273 data->arg.ftype = NF4CHR;
2283 arg.u.device.specdata1 = MAJOR(rdev); 2274 data->arg.u.device.specdata1 = MAJOR(rdev);
2284 arg.u.device.specdata2 = MINOR(rdev); 2275 data->arg.u.device.specdata2 = MINOR(rdev);
2285 } 2276 }
2286 else
2287 arg.ftype = NF4SOCK;
2288 2277
2289 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 2278 status = nfs4_do_create(dir, dentry, data);
2290 if (status == 0) { 2279
2291 update_changeattr(dir, &res.dir_cinfo); 2280 nfs4_free_createdata(data);
2292 nfs_post_op_update_inode(dir, res.dir_fattr); 2281out:
2293 status = nfs_instantiate(dentry, &fh, &fattr);
2294 }
2295 return status; 2282 return status;
2296} 2283}
2297 2284
@@ -2706,6 +2693,8 @@ static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2706 ret = nfs_revalidate_inode(server, inode); 2693 ret = nfs_revalidate_inode(server, inode);
2707 if (ret < 0) 2694 if (ret < 0)
2708 return ret; 2695 return ret;
2696 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
2697 nfs_zap_acl_cache(inode);
2709 ret = nfs4_read_cached_acl(inode, buf, buflen); 2698 ret = nfs4_read_cached_acl(inode, buf, buflen);
2710 if (ret != -ENOENT) 2699 if (ret != -ENOENT)
2711 return ret; 2700 return ret;
@@ -2733,7 +2722,8 @@ static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t bufl
2733 nfs_inode_return_delegation(inode); 2722 nfs_inode_return_delegation(inode);
2734 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase); 2723 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2735 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); 2724 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2736 nfs_zap_caches(inode); 2725 nfs_access_zap_cache(inode);
2726 nfs_zap_acl_cache(inode);
2737 return ret; 2727 return ret;
2738} 2728}
2739 2729
@@ -2767,8 +2757,7 @@ nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2767 task->tk_status = 0; 2757 task->tk_status = 0;
2768 return -EAGAIN; 2758 return -EAGAIN;
2769 case -NFS4ERR_DELAY: 2759 case -NFS4ERR_DELAY:
2770 nfs_inc_server_stats((struct nfs_server *) server, 2760 nfs_inc_server_stats(server, NFSIOS_DELAY);
2771 NFSIOS_DELAY);
2772 case -NFS4ERR_GRACE: 2761 case -NFS4ERR_GRACE:
2773 rpc_delay(task, NFS4_POLL_RETRY_MAX); 2762 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2774 task->tk_status = 0; 2763 task->tk_status = 0;
@@ -2933,7 +2922,7 @@ static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cre
2933 2922
2934int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred) 2923int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
2935{ 2924{
2936 long timeout; 2925 long timeout = 0;
2937 int err; 2926 int err;
2938 do { 2927 do {
2939 err = _nfs4_proc_setclientid_confirm(clp, cred); 2928 err = _nfs4_proc_setclientid_confirm(clp, cred);
@@ -3725,8 +3714,6 @@ const struct nfs_rpc_ops nfs_v4_clientops = {
3725 .write_done = nfs4_write_done, 3714 .write_done = nfs4_write_done,
3726 .commit_setup = nfs4_proc_commit_setup, 3715 .commit_setup = nfs4_proc_commit_setup,
3727 .commit_done = nfs4_commit_done, 3716 .commit_done = nfs4_commit_done,
3728 .file_open = nfs_open,
3729 .file_release = nfs_release,
3730 .lock = nfs4_proc_lock, 3717 .lock = nfs4_proc_lock,
3731 .clear_acl_cache = nfs4_zap_acl_attr, 3718 .clear_acl_cache = nfs4_zap_acl_attr,
3732}; 3719};
diff --git a/fs/nfs/nfs4state.c b/fs/nfs/nfs4state.c
index 856a8934f610..401ef8b28f97 100644
--- a/fs/nfs/nfs4state.c
+++ b/fs/nfs/nfs4state.c
@@ -940,7 +940,6 @@ static int reclaimer(void *ptr)
940 allow_signal(SIGKILL); 940 allow_signal(SIGKILL);
941 941
942 /* Ensure exclusive access to NFSv4 state */ 942 /* Ensure exclusive access to NFSv4 state */
943 lock_kernel();
944 down_write(&clp->cl_sem); 943 down_write(&clp->cl_sem);
945 /* Are there any NFS mounts out there? */ 944 /* Are there any NFS mounts out there? */
946 if (list_empty(&clp->cl_superblocks)) 945 if (list_empty(&clp->cl_superblocks))
@@ -1000,7 +999,6 @@ restart_loop:
1000 nfs_delegation_reap_unclaimed(clp); 999 nfs_delegation_reap_unclaimed(clp);
1001out: 1000out:
1002 up_write(&clp->cl_sem); 1001 up_write(&clp->cl_sem);
1003 unlock_kernel();
1004 if (status == -NFS4ERR_CB_PATH_DOWN) 1002 if (status == -NFS4ERR_CB_PATH_DOWN)
1005 nfs_handle_cb_pathdown(clp); 1003 nfs_handle_cb_pathdown(clp);
1006 nfs4_clear_recover_bit(clp); 1004 nfs4_clear_recover_bit(clp);
diff --git a/fs/nfs/nfsroot.c b/fs/nfs/nfsroot.c
index 531379d36823..46763d1cd397 100644
--- a/fs/nfs/nfsroot.c
+++ b/fs/nfs/nfsroot.c
@@ -1,6 +1,4 @@
1/* 1/*
2 * $Id: nfsroot.c,v 1.45 1998/03/07 10:44:46 mj Exp $
3 *
4 * Copyright (C) 1995, 1996 Gero Kuhlmann <gero@gkminix.han.de> 2 * Copyright (C) 1995, 1996 Gero Kuhlmann <gero@gkminix.han.de>
5 * 3 *
6 * Allow an NFS filesystem to be mounted as root. The way this works is: 4 * Allow an NFS filesystem to be mounted as root. The way this works is:
@@ -297,10 +295,10 @@ static int __init root_nfs_name(char *name)
297 nfs_data.flags = NFS_MOUNT_NONLM; /* No lockd in nfs root yet */ 295 nfs_data.flags = NFS_MOUNT_NONLM; /* No lockd in nfs root yet */
298 nfs_data.rsize = NFS_DEF_FILE_IO_SIZE; 296 nfs_data.rsize = NFS_DEF_FILE_IO_SIZE;
299 nfs_data.wsize = NFS_DEF_FILE_IO_SIZE; 297 nfs_data.wsize = NFS_DEF_FILE_IO_SIZE;
300 nfs_data.acregmin = 3; 298 nfs_data.acregmin = NFS_DEF_ACREGMIN;
301 nfs_data.acregmax = 60; 299 nfs_data.acregmax = NFS_DEF_ACREGMAX;
302 nfs_data.acdirmin = 30; 300 nfs_data.acdirmin = NFS_DEF_ACDIRMIN;
303 nfs_data.acdirmax = 60; 301 nfs_data.acdirmax = NFS_DEF_ACDIRMAX;
304 strcpy(buf, NFS_ROOT); 302 strcpy(buf, NFS_ROOT);
305 303
306 /* Process options received from the remote server */ 304 /* Process options received from the remote server */
diff --git a/fs/nfs/proc.c b/fs/nfs/proc.c
index 03599bfe81cf..4dbb84df1b68 100644
--- a/fs/nfs/proc.c
+++ b/fs/nfs/proc.c
@@ -129,6 +129,8 @@ nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
129 sattr->ia_mode &= S_IALLUGO; 129 sattr->ia_mode &= S_IALLUGO;
130 130
131 dprintk("NFS call setattr\n"); 131 dprintk("NFS call setattr\n");
132 if (sattr->ia_valid & ATTR_FILE)
133 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
132 nfs_fattr_init(fattr); 134 nfs_fattr_init(fattr);
133 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); 135 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
134 if (status == 0) 136 if (status == 0)
@@ -598,6 +600,29 @@ nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
598 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl); 600 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl);
599} 601}
600 602
603/* Helper functions for NFS lock bounds checking */
604#define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
605static int nfs_lock_check_bounds(const struct file_lock *fl)
606{
607 __s32 start, end;
608
609 start = (__s32)fl->fl_start;
610 if ((loff_t)start != fl->fl_start)
611 goto out_einval;
612
613 if (fl->fl_end != OFFSET_MAX) {
614 end = (__s32)fl->fl_end;
615 if ((loff_t)end != fl->fl_end)
616 goto out_einval;
617 } else
618 end = NFS_LOCK32_OFFSET_MAX;
619
620 if (start < 0 || start > end)
621 goto out_einval;
622 return 0;
623out_einval:
624 return -EINVAL;
625}
601 626
602const struct nfs_rpc_ops nfs_v2_clientops = { 627const struct nfs_rpc_ops nfs_v2_clientops = {
603 .version = 2, /* protocol version */ 628 .version = 2, /* protocol version */
@@ -630,7 +655,6 @@ const struct nfs_rpc_ops nfs_v2_clientops = {
630 .write_setup = nfs_proc_write_setup, 655 .write_setup = nfs_proc_write_setup,
631 .write_done = nfs_write_done, 656 .write_done = nfs_write_done,
632 .commit_setup = nfs_proc_commit_setup, 657 .commit_setup = nfs_proc_commit_setup,
633 .file_open = nfs_open,
634 .file_release = nfs_release,
635 .lock = nfs_proc_lock, 658 .lock = nfs_proc_lock,
659 .lock_check_bounds = nfs_lock_check_bounds,
636}; 660};
diff --git a/fs/nfs/super.c b/fs/nfs/super.c
index 614efeed5437..1b94e3650f5c 100644
--- a/fs/nfs/super.c
+++ b/fs/nfs/super.c
@@ -47,6 +47,7 @@
47#include <linux/inet.h> 47#include <linux/inet.h>
48#include <linux/in6.h> 48#include <linux/in6.h>
49#include <net/ipv6.h> 49#include <net/ipv6.h>
50#include <linux/netdevice.h>
50#include <linux/nfs_xdr.h> 51#include <linux/nfs_xdr.h>
51#include <linux/magic.h> 52#include <linux/magic.h>
52#include <linux/parser.h> 53#include <linux/parser.h>
@@ -65,7 +66,6 @@
65enum { 66enum {
66 /* Mount options that take no arguments */ 67 /* Mount options that take no arguments */
67 Opt_soft, Opt_hard, 68 Opt_soft, Opt_hard,
68 Opt_intr, Opt_nointr,
69 Opt_posix, Opt_noposix, 69 Opt_posix, Opt_noposix,
70 Opt_cto, Opt_nocto, 70 Opt_cto, Opt_nocto,
71 Opt_ac, Opt_noac, 71 Opt_ac, Opt_noac,
@@ -92,8 +92,8 @@ enum {
92 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost, 92 Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
93 Opt_addr, Opt_mountaddr, Opt_clientaddr, 93 Opt_addr, Opt_mountaddr, Opt_clientaddr,
94 94
95 /* Mount options that are ignored */ 95 /* Special mount options */
96 Opt_userspace, Opt_deprecated, 96 Opt_userspace, Opt_deprecated, Opt_sloppy,
97 97
98 Opt_err 98 Opt_err
99}; 99};
@@ -101,10 +101,14 @@ enum {
101static match_table_t nfs_mount_option_tokens = { 101static match_table_t nfs_mount_option_tokens = {
102 { Opt_userspace, "bg" }, 102 { Opt_userspace, "bg" },
103 { Opt_userspace, "fg" }, 103 { Opt_userspace, "fg" },
104 { Opt_userspace, "retry=%s" },
105
106 { Opt_sloppy, "sloppy" },
107
104 { Opt_soft, "soft" }, 108 { Opt_soft, "soft" },
105 { Opt_hard, "hard" }, 109 { Opt_hard, "hard" },
106 { Opt_intr, "intr" }, 110 { Opt_deprecated, "intr" },
107 { Opt_nointr, "nointr" }, 111 { Opt_deprecated, "nointr" },
108 { Opt_posix, "posix" }, 112 { Opt_posix, "posix" },
109 { Opt_noposix, "noposix" }, 113 { Opt_noposix, "noposix" },
110 { Opt_cto, "cto" }, 114 { Opt_cto, "cto" },
@@ -136,7 +140,6 @@ static match_table_t nfs_mount_option_tokens = {
136 { Opt_acdirmin, "acdirmin=%u" }, 140 { Opt_acdirmin, "acdirmin=%u" },
137 { Opt_acdirmax, "acdirmax=%u" }, 141 { Opt_acdirmax, "acdirmax=%u" },
138 { Opt_actimeo, "actimeo=%u" }, 142 { Opt_actimeo, "actimeo=%u" },
139 { Opt_userspace, "retry=%u" },
140 { Opt_namelen, "namlen=%u" }, 143 { Opt_namelen, "namlen=%u" },
141 { Opt_mountport, "mountport=%u" }, 144 { Opt_mountport, "mountport=%u" },
142 { Opt_mountvers, "mountvers=%u" }, 145 { Opt_mountvers, "mountvers=%u" },
@@ -207,6 +210,7 @@ static int nfs_xdev_get_sb(struct file_system_type *fs_type,
207 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt); 210 int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
208static void nfs_kill_super(struct super_block *); 211static void nfs_kill_super(struct super_block *);
209static void nfs_put_super(struct super_block *); 212static void nfs_put_super(struct super_block *);
213static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
210 214
211static struct file_system_type nfs_fs_type = { 215static struct file_system_type nfs_fs_type = {
212 .owner = THIS_MODULE, 216 .owner = THIS_MODULE,
@@ -234,6 +238,7 @@ static const struct super_operations nfs_sops = {
234 .umount_begin = nfs_umount_begin, 238 .umount_begin = nfs_umount_begin,
235 .show_options = nfs_show_options, 239 .show_options = nfs_show_options,
236 .show_stats = nfs_show_stats, 240 .show_stats = nfs_show_stats,
241 .remount_fs = nfs_remount,
237}; 242};
238 243
239#ifdef CONFIG_NFS_V4 244#ifdef CONFIG_NFS_V4
@@ -278,6 +283,7 @@ static const struct super_operations nfs4_sops = {
278 .umount_begin = nfs_umount_begin, 283 .umount_begin = nfs_umount_begin,
279 .show_options = nfs_show_options, 284 .show_options = nfs_show_options,
280 .show_stats = nfs_show_stats, 285 .show_stats = nfs_show_stats,
286 .remount_fs = nfs_remount,
281}; 287};
282#endif 288#endif
283 289
@@ -368,8 +374,6 @@ static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
368 }; 374 };
369 int error; 375 int error;
370 376
371 lock_kernel();
372
373 error = server->nfs_client->rpc_ops->statfs(server, fh, &res); 377 error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
374 if (error < 0) 378 if (error < 0)
375 goto out_err; 379 goto out_err;
@@ -401,12 +405,10 @@ static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
401 405
402 buf->f_namelen = server->namelen; 406 buf->f_namelen = server->namelen;
403 407
404 unlock_kernel();
405 return 0; 408 return 0;
406 409
407 out_err: 410 out_err:
408 dprintk("%s: statfs error = %d\n", __func__, -error); 411 dprintk("%s: statfs error = %d\n", __func__, -error);
409 unlock_kernel();
410 return error; 412 return error;
411} 413}
412 414
@@ -514,13 +516,13 @@ static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
514 if (nfss->bsize != 0) 516 if (nfss->bsize != 0)
515 seq_printf(m, ",bsize=%u", nfss->bsize); 517 seq_printf(m, ",bsize=%u", nfss->bsize);
516 seq_printf(m, ",namlen=%u", nfss->namelen); 518 seq_printf(m, ",namlen=%u", nfss->namelen);
517 if (nfss->acregmin != 3*HZ || showdefaults) 519 if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
518 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ); 520 seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
519 if (nfss->acregmax != 60*HZ || showdefaults) 521 if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
520 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ); 522 seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
521 if (nfss->acdirmin != 30*HZ || showdefaults) 523 if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
522 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ); 524 seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
523 if (nfss->acdirmax != 60*HZ || showdefaults) 525 if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
524 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ); 526 seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
525 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) { 527 for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
526 if (nfss->flags & nfs_infop->flag) 528 if (nfss->flags & nfs_infop->flag)
@@ -702,49 +704,233 @@ static int nfs_verify_server_address(struct sockaddr *addr)
702 return 0; 704 return 0;
703} 705}
704 706
707static void nfs_parse_ipv4_address(char *string, size_t str_len,
708 struct sockaddr *sap, size_t *addr_len)
709{
710 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
711 u8 *addr = (u8 *)&sin->sin_addr.s_addr;
712
713 if (str_len <= INET_ADDRSTRLEN) {
714 dfprintk(MOUNT, "NFS: parsing IPv4 address %*s\n",
715 (int)str_len, string);
716
717 sin->sin_family = AF_INET;
718 *addr_len = sizeof(*sin);
719 if (in4_pton(string, str_len, addr, '\0', NULL))
720 return;
721 }
722
723 sap->sa_family = AF_UNSPEC;
724 *addr_len = 0;
725}
726
727#define IPV6_SCOPE_DELIMITER '%'
728
729#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
730static void nfs_parse_ipv6_scope_id(const char *string, const size_t str_len,
731 const char *delim,
732 struct sockaddr_in6 *sin6)
733{
734 char *p;
735 size_t len;
736
737 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
738 return ;
739 if (*delim != IPV6_SCOPE_DELIMITER)
740 return;
741
742 len = (string + str_len) - delim - 1;
743 p = kstrndup(delim + 1, len, GFP_KERNEL);
744 if (p) {
745 unsigned long scope_id = 0;
746 struct net_device *dev;
747
748 dev = dev_get_by_name(&init_net, p);
749 if (dev != NULL) {
750 scope_id = dev->ifindex;
751 dev_put(dev);
752 } else {
753 /* scope_id is set to zero on error */
754 strict_strtoul(p, 10, &scope_id);
755 }
756
757 kfree(p);
758 sin6->sin6_scope_id = scope_id;
759 dfprintk(MOUNT, "NFS: IPv6 scope ID = %lu\n", scope_id);
760 }
761}
762
763static void nfs_parse_ipv6_address(char *string, size_t str_len,
764 struct sockaddr *sap, size_t *addr_len)
765{
766 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
767 u8 *addr = (u8 *)&sin6->sin6_addr.in6_u;
768 const char *delim;
769
770 if (str_len <= INET6_ADDRSTRLEN) {
771 dfprintk(MOUNT, "NFS: parsing IPv6 address %*s\n",
772 (int)str_len, string);
773
774 sin6->sin6_family = AF_INET6;
775 *addr_len = sizeof(*sin6);
776 if (in6_pton(string, str_len, addr, IPV6_SCOPE_DELIMITER, &delim)) {
777 nfs_parse_ipv6_scope_id(string, str_len, delim, sin6);
778 return;
779 }
780 }
781
782 sap->sa_family = AF_UNSPEC;
783 *addr_len = 0;
784}
785#else
786static void nfs_parse_ipv6_address(char *string, size_t str_len,
787 struct sockaddr *sap, size_t *addr_len)
788{
789 sap->sa_family = AF_UNSPEC;
790 *addr_len = 0;
791}
792#endif
793
705/* 794/*
706 * Parse string addresses passed in via a mount option, 795 * Construct a sockaddr based on the contents of a string that contains
707 * and construct a sockaddr based on the result. 796 * an IP address in presentation format.
708 * 797 *
709 * If address parsing fails, set the sockaddr's address 798 * If there is a problem constructing the new sockaddr, set the address
710 * family to AF_UNSPEC to force nfs_verify_server_address() 799 * family to AF_UNSPEC.
711 * to punt the mount.
712 */ 800 */
713static void nfs_parse_server_address(char *value, 801static void nfs_parse_ip_address(char *string, size_t str_len,
714 struct sockaddr *sap, 802 struct sockaddr *sap, size_t *addr_len)
715 size_t *len)
716{ 803{
717 if (strchr(value, ':')) { 804 unsigned int i, colons;
718 struct sockaddr_in6 *ap = (struct sockaddr_in6 *)sap;
719 u8 *addr = (u8 *)&ap->sin6_addr.in6_u;
720 805
721 ap->sin6_family = AF_INET6; 806 colons = 0;
722 *len = sizeof(*ap); 807 for (i = 0; i < str_len; i++)
723 if (in6_pton(value, -1, addr, '\0', NULL)) 808 if (string[i] == ':')
724 return; 809 colons++;
725 } else { 810
726 struct sockaddr_in *ap = (struct sockaddr_in *)sap; 811 if (colons >= 2)
727 u8 *addr = (u8 *)&ap->sin_addr.s_addr; 812 nfs_parse_ipv6_address(string, str_len, sap, addr_len);
813 else
814 nfs_parse_ipv4_address(string, str_len, sap, addr_len);
815}
816
817/*
818 * Sanity check the NFS transport protocol.
819 *
820 */
821static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
822{
823 switch (mnt->nfs_server.protocol) {
824 case XPRT_TRANSPORT_UDP:
825 case XPRT_TRANSPORT_TCP:
826 case XPRT_TRANSPORT_RDMA:
827 break;
828 default:
829 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
830 }
831}
832
833/*
834 * For text based NFSv2/v3 mounts, the mount protocol transport default
835 * settings should depend upon the specified NFS transport.
836 */
837static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
838{
839 nfs_validate_transport_protocol(mnt);
728 840
729 ap->sin_family = AF_INET; 841 if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
730 *len = sizeof(*ap); 842 mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
731 if (in4_pton(value, -1, addr, '\0', NULL))
732 return; 843 return;
844 switch (mnt->nfs_server.protocol) {
845 case XPRT_TRANSPORT_UDP:
846 mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
847 break;
848 case XPRT_TRANSPORT_TCP:
849 case XPRT_TRANSPORT_RDMA:
850 mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
733 } 851 }
852}
734 853
735 sap->sa_family = AF_UNSPEC; 854/*
736 *len = 0; 855 * Parse the value of the 'sec=' option.
856 *
857 * The flavor_len setting is for v4 mounts.
858 */
859static int nfs_parse_security_flavors(char *value,
860 struct nfs_parsed_mount_data *mnt)
861{
862 substring_t args[MAX_OPT_ARGS];
863
864 dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
865
866 switch (match_token(value, nfs_secflavor_tokens, args)) {
867 case Opt_sec_none:
868 mnt->auth_flavor_len = 0;
869 mnt->auth_flavors[0] = RPC_AUTH_NULL;
870 break;
871 case Opt_sec_sys:
872 mnt->auth_flavor_len = 0;
873 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
874 break;
875 case Opt_sec_krb5:
876 mnt->auth_flavor_len = 1;
877 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
878 break;
879 case Opt_sec_krb5i:
880 mnt->auth_flavor_len = 1;
881 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
882 break;
883 case Opt_sec_krb5p:
884 mnt->auth_flavor_len = 1;
885 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
886 break;
887 case Opt_sec_lkey:
888 mnt->auth_flavor_len = 1;
889 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
890 break;
891 case Opt_sec_lkeyi:
892 mnt->auth_flavor_len = 1;
893 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
894 break;
895 case Opt_sec_lkeyp:
896 mnt->auth_flavor_len = 1;
897 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
898 break;
899 case Opt_sec_spkm:
900 mnt->auth_flavor_len = 1;
901 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
902 break;
903 case Opt_sec_spkmi:
904 mnt->auth_flavor_len = 1;
905 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
906 break;
907 case Opt_sec_spkmp:
908 mnt->auth_flavor_len = 1;
909 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
910 break;
911 default:
912 return 0;
913 }
914
915 return 1;
916}
917
918static void nfs_parse_invalid_value(const char *option)
919{
920 dfprintk(MOUNT, "NFS: bad value specified for %s option\n", option);
737} 921}
738 922
739/* 923/*
740 * Error-check and convert a string of mount options from user space into 924 * Error-check and convert a string of mount options from user space into
741 * a data structure 925 * a data structure. The whole mount string is processed; bad options are
926 * skipped as they are encountered. If there were no errors, return 1;
927 * otherwise return 0 (zero).
742 */ 928 */
743static int nfs_parse_mount_options(char *raw, 929static int nfs_parse_mount_options(char *raw,
744 struct nfs_parsed_mount_data *mnt) 930 struct nfs_parsed_mount_data *mnt)
745{ 931{
746 char *p, *string, *secdata; 932 char *p, *string, *secdata;
747 int rc; 933 int rc, sloppy = 0, errors = 0;
748 934
749 if (!raw) { 935 if (!raw) {
750 dfprintk(MOUNT, "NFS: mount options string was NULL.\n"); 936 dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
@@ -777,15 +963,16 @@ static int nfs_parse_mount_options(char *raw,
777 963
778 token = match_token(p, nfs_mount_option_tokens, args); 964 token = match_token(p, nfs_mount_option_tokens, args);
779 switch (token) { 965 switch (token) {
966
967 /*
968 * boolean options: foo/nofoo
969 */
780 case Opt_soft: 970 case Opt_soft:
781 mnt->flags |= NFS_MOUNT_SOFT; 971 mnt->flags |= NFS_MOUNT_SOFT;
782 break; 972 break;
783 case Opt_hard: 973 case Opt_hard:
784 mnt->flags &= ~NFS_MOUNT_SOFT; 974 mnt->flags &= ~NFS_MOUNT_SOFT;
785 break; 975 break;
786 case Opt_intr:
787 case Opt_nointr:
788 break;
789 case Opt_posix: 976 case Opt_posix:
790 mnt->flags |= NFS_MOUNT_POSIX; 977 mnt->flags |= NFS_MOUNT_POSIX;
791 break; 978 break;
@@ -819,20 +1006,14 @@ static int nfs_parse_mount_options(char *raw,
819 case Opt_udp: 1006 case Opt_udp:
820 mnt->flags &= ~NFS_MOUNT_TCP; 1007 mnt->flags &= ~NFS_MOUNT_TCP;
821 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP; 1008 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
822 mnt->timeo = 7;
823 mnt->retrans = 5;
824 break; 1009 break;
825 case Opt_tcp: 1010 case Opt_tcp:
826 mnt->flags |= NFS_MOUNT_TCP; 1011 mnt->flags |= NFS_MOUNT_TCP;
827 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP; 1012 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
828 mnt->timeo = 600;
829 mnt->retrans = 2;
830 break; 1013 break;
831 case Opt_rdma: 1014 case Opt_rdma:
832 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */ 1015 mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
833 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA; 1016 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
834 mnt->timeo = 600;
835 mnt->retrans = 2;
836 break; 1017 break;
837 case Opt_acl: 1018 case Opt_acl:
838 mnt->flags &= ~NFS_MOUNT_NOACL; 1019 mnt->flags &= ~NFS_MOUNT_NOACL;
@@ -853,165 +1034,144 @@ static int nfs_parse_mount_options(char *raw,
853 mnt->flags |= NFS_MOUNT_UNSHARED; 1034 mnt->flags |= NFS_MOUNT_UNSHARED;
854 break; 1035 break;
855 1036
1037 /*
1038 * options that take numeric values
1039 */
856 case Opt_port: 1040 case Opt_port:
857 if (match_int(args, &option)) 1041 if (match_int(args, &option) ||
858 return 0; 1042 option < 0 || option > USHORT_MAX) {
859 if (option < 0 || option > 65535) 1043 errors++;
860 return 0; 1044 nfs_parse_invalid_value("port");
861 mnt->nfs_server.port = option; 1045 } else
1046 mnt->nfs_server.port = option;
862 break; 1047 break;
863 case Opt_rsize: 1048 case Opt_rsize:
864 if (match_int(args, &mnt->rsize)) 1049 if (match_int(args, &option) || option < 0) {
865 return 0; 1050 errors++;
1051 nfs_parse_invalid_value("rsize");
1052 } else
1053 mnt->rsize = option;
866 break; 1054 break;
867 case Opt_wsize: 1055 case Opt_wsize:
868 if (match_int(args, &mnt->wsize)) 1056 if (match_int(args, &option) || option < 0) {
869 return 0; 1057 errors++;
1058 nfs_parse_invalid_value("wsize");
1059 } else
1060 mnt->wsize = option;
870 break; 1061 break;
871 case Opt_bsize: 1062 case Opt_bsize:
872 if (match_int(args, &option)) 1063 if (match_int(args, &option) || option < 0) {
873 return 0; 1064 errors++;
874 if (option < 0) 1065 nfs_parse_invalid_value("bsize");
875 return 0; 1066 } else
876 mnt->bsize = option; 1067 mnt->bsize = option;
877 break; 1068 break;
878 case Opt_timeo: 1069 case Opt_timeo:
879 if (match_int(args, &mnt->timeo)) 1070 if (match_int(args, &option) || option <= 0) {
880 return 0; 1071 errors++;
1072 nfs_parse_invalid_value("timeo");
1073 } else
1074 mnt->timeo = option;
881 break; 1075 break;
882 case Opt_retrans: 1076 case Opt_retrans:
883 if (match_int(args, &mnt->retrans)) 1077 if (match_int(args, &option) || option <= 0) {
884 return 0; 1078 errors++;
1079 nfs_parse_invalid_value("retrans");
1080 } else
1081 mnt->retrans = option;
885 break; 1082 break;
886 case Opt_acregmin: 1083 case Opt_acregmin:
887 if (match_int(args, &mnt->acregmin)) 1084 if (match_int(args, &option) || option < 0) {
888 return 0; 1085 errors++;
1086 nfs_parse_invalid_value("acregmin");
1087 } else
1088 mnt->acregmin = option;
889 break; 1089 break;
890 case Opt_acregmax: 1090 case Opt_acregmax:
891 if (match_int(args, &mnt->acregmax)) 1091 if (match_int(args, &option) || option < 0) {
892 return 0; 1092 errors++;
1093 nfs_parse_invalid_value("acregmax");
1094 } else
1095 mnt->acregmax = option;
893 break; 1096 break;
894 case Opt_acdirmin: 1097 case Opt_acdirmin:
895 if (match_int(args, &mnt->acdirmin)) 1098 if (match_int(args, &option) || option < 0) {
896 return 0; 1099 errors++;
1100 nfs_parse_invalid_value("acdirmin");
1101 } else
1102 mnt->acdirmin = option;
897 break; 1103 break;
898 case Opt_acdirmax: 1104 case Opt_acdirmax:
899 if (match_int(args, &mnt->acdirmax)) 1105 if (match_int(args, &option) || option < 0) {
900 return 0; 1106 errors++;
1107 nfs_parse_invalid_value("acdirmax");
1108 } else
1109 mnt->acdirmax = option;
901 break; 1110 break;
902 case Opt_actimeo: 1111 case Opt_actimeo:
903 if (match_int(args, &option)) 1112 if (match_int(args, &option) || option < 0) {
904 return 0; 1113 errors++;
905 if (option < 0) 1114 nfs_parse_invalid_value("actimeo");
906 return 0; 1115 } else
907 mnt->acregmin = 1116 mnt->acregmin = mnt->acregmax =
908 mnt->acregmax = 1117 mnt->acdirmin = mnt->acdirmax = option;
909 mnt->acdirmin =
910 mnt->acdirmax = option;
911 break; 1118 break;
912 case Opt_namelen: 1119 case Opt_namelen:
913 if (match_int(args, &mnt->namlen)) 1120 if (match_int(args, &option) || option < 0) {
914 return 0; 1121 errors++;
1122 nfs_parse_invalid_value("namlen");
1123 } else
1124 mnt->namlen = option;
915 break; 1125 break;
916 case Opt_mountport: 1126 case Opt_mountport:
917 if (match_int(args, &option)) 1127 if (match_int(args, &option) ||
918 return 0; 1128 option < 0 || option > USHORT_MAX) {
919 if (option < 0 || option > 65535) 1129 errors++;
920 return 0; 1130 nfs_parse_invalid_value("mountport");
921 mnt->mount_server.port = option; 1131 } else
1132 mnt->mount_server.port = option;
922 break; 1133 break;
923 case Opt_mountvers: 1134 case Opt_mountvers:
924 if (match_int(args, &option)) 1135 if (match_int(args, &option) ||
925 return 0; 1136 option < NFS_MNT_VERSION ||
926 if (option < 0) 1137 option > NFS_MNT3_VERSION) {
927 return 0; 1138 errors++;
928 mnt->mount_server.version = option; 1139 nfs_parse_invalid_value("mountvers");
1140 } else
1141 mnt->mount_server.version = option;
929 break; 1142 break;
930 case Opt_nfsvers: 1143 case Opt_nfsvers:
931 if (match_int(args, &option)) 1144 if (match_int(args, &option)) {
932 return 0; 1145 errors++;
1146 nfs_parse_invalid_value("nfsvers");
1147 break;
1148 }
933 switch (option) { 1149 switch (option) {
934 case 2: 1150 case NFS2_VERSION:
935 mnt->flags &= ~NFS_MOUNT_VER3; 1151 mnt->flags &= ~NFS_MOUNT_VER3;
936 break; 1152 break;
937 case 3: 1153 case NFS3_VERSION:
938 mnt->flags |= NFS_MOUNT_VER3; 1154 mnt->flags |= NFS_MOUNT_VER3;
939 break; 1155 break;
940 default: 1156 default:
941 goto out_unrec_vers; 1157 errors++;
1158 nfs_parse_invalid_value("nfsvers");
942 } 1159 }
943 break; 1160 break;
944 1161
1162 /*
1163 * options that take text values
1164 */
945 case Opt_sec: 1165 case Opt_sec:
946 string = match_strdup(args); 1166 string = match_strdup(args);
947 if (string == NULL) 1167 if (string == NULL)
948 goto out_nomem; 1168 goto out_nomem;
949 token = match_token(string, nfs_secflavor_tokens, args); 1169 rc = nfs_parse_security_flavors(string, mnt);
950 kfree(string); 1170 kfree(string);
951 1171 if (!rc) {
952 /* 1172 errors++;
953 * The flags setting is for v2/v3. The flavor_len 1173 dfprintk(MOUNT, "NFS: unrecognized "
954 * setting is for v4. v2/v3 also need to know the 1174 "security flavor\n");
955 * difference between NULL and UNIX.
956 */
957 switch (token) {
958 case Opt_sec_none:
959 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
960 mnt->auth_flavor_len = 0;
961 mnt->auth_flavors[0] = RPC_AUTH_NULL;
962 break;
963 case Opt_sec_sys:
964 mnt->flags &= ~NFS_MOUNT_SECFLAVOUR;
965 mnt->auth_flavor_len = 0;
966 mnt->auth_flavors[0] = RPC_AUTH_UNIX;
967 break;
968 case Opt_sec_krb5:
969 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
970 mnt->auth_flavor_len = 1;
971 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
972 break;
973 case Opt_sec_krb5i:
974 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
975 mnt->auth_flavor_len = 1;
976 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
977 break;
978 case Opt_sec_krb5p:
979 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
980 mnt->auth_flavor_len = 1;
981 mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
982 break;
983 case Opt_sec_lkey:
984 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
985 mnt->auth_flavor_len = 1;
986 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
987 break;
988 case Opt_sec_lkeyi:
989 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
990 mnt->auth_flavor_len = 1;
991 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
992 break;
993 case Opt_sec_lkeyp:
994 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
995 mnt->auth_flavor_len = 1;
996 mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
997 break;
998 case Opt_sec_spkm:
999 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1000 mnt->auth_flavor_len = 1;
1001 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
1002 break;
1003 case Opt_sec_spkmi:
1004 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1005 mnt->auth_flavor_len = 1;
1006 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
1007 break;
1008 case Opt_sec_spkmp:
1009 mnt->flags |= NFS_MOUNT_SECFLAVOUR;
1010 mnt->auth_flavor_len = 1;
1011 mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
1012 break;
1013 default:
1014 goto out_unrec_sec;
1015 } 1175 }
1016 break; 1176 break;
1017 case Opt_proto: 1177 case Opt_proto:
@@ -1026,24 +1186,20 @@ static int nfs_parse_mount_options(char *raw,
1026 case Opt_xprt_udp: 1186 case Opt_xprt_udp:
1027 mnt->flags &= ~NFS_MOUNT_TCP; 1187 mnt->flags &= ~NFS_MOUNT_TCP;
1028 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP; 1188 mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1029 mnt->timeo = 7;
1030 mnt->retrans = 5;
1031 break; 1189 break;
1032 case Opt_xprt_tcp: 1190 case Opt_xprt_tcp:
1033 mnt->flags |= NFS_MOUNT_TCP; 1191 mnt->flags |= NFS_MOUNT_TCP;
1034 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP; 1192 mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1035 mnt->timeo = 600;
1036 mnt->retrans = 2;
1037 break; 1193 break;
1038 case Opt_xprt_rdma: 1194 case Opt_xprt_rdma:
1039 /* vector side protocols to TCP */ 1195 /* vector side protocols to TCP */
1040 mnt->flags |= NFS_MOUNT_TCP; 1196 mnt->flags |= NFS_MOUNT_TCP;
1041 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA; 1197 mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1042 mnt->timeo = 600;
1043 mnt->retrans = 2;
1044 break; 1198 break;
1045 default: 1199 default:
1046 goto out_unrec_xprt; 1200 errors++;
1201 dfprintk(MOUNT, "NFS: unrecognized "
1202 "transport protocol\n");
1047 } 1203 }
1048 break; 1204 break;
1049 case Opt_mountproto: 1205 case Opt_mountproto:
@@ -1063,16 +1219,19 @@ static int nfs_parse_mount_options(char *raw,
1063 break; 1219 break;
1064 case Opt_xprt_rdma: /* not used for side protocols */ 1220 case Opt_xprt_rdma: /* not used for side protocols */
1065 default: 1221 default:
1066 goto out_unrec_xprt; 1222 errors++;
1223 dfprintk(MOUNT, "NFS: unrecognized "
1224 "transport protocol\n");
1067 } 1225 }
1068 break; 1226 break;
1069 case Opt_addr: 1227 case Opt_addr:
1070 string = match_strdup(args); 1228 string = match_strdup(args);
1071 if (string == NULL) 1229 if (string == NULL)
1072 goto out_nomem; 1230 goto out_nomem;
1073 nfs_parse_server_address(string, (struct sockaddr *) 1231 nfs_parse_ip_address(string, strlen(string),
1074 &mnt->nfs_server.address, 1232 (struct sockaddr *)
1075 &mnt->nfs_server.addrlen); 1233 &mnt->nfs_server.address,
1234 &mnt->nfs_server.addrlen);
1076 kfree(string); 1235 kfree(string);
1077 break; 1236 break;
1078 case Opt_clientaddr: 1237 case Opt_clientaddr:
@@ -1093,24 +1252,33 @@ static int nfs_parse_mount_options(char *raw,
1093 string = match_strdup(args); 1252 string = match_strdup(args);
1094 if (string == NULL) 1253 if (string == NULL)
1095 goto out_nomem; 1254 goto out_nomem;
1096 nfs_parse_server_address(string, (struct sockaddr *) 1255 nfs_parse_ip_address(string, strlen(string),
1097 &mnt->mount_server.address, 1256 (struct sockaddr *)
1098 &mnt->mount_server.addrlen); 1257 &mnt->mount_server.address,
1258 &mnt->mount_server.addrlen);
1099 kfree(string); 1259 kfree(string);
1100 break; 1260 break;
1101 1261
1262 /*
1263 * Special options
1264 */
1265 case Opt_sloppy:
1266 sloppy = 1;
1267 dfprintk(MOUNT, "NFS: relaxing parsing rules\n");
1268 break;
1102 case Opt_userspace: 1269 case Opt_userspace:
1103 case Opt_deprecated: 1270 case Opt_deprecated:
1271 dfprintk(MOUNT, "NFS: ignoring mount option "
1272 "'%s'\n", p);
1104 break; 1273 break;
1105 1274
1106 default: 1275 default:
1107 goto out_unknown; 1276 errors++;
1277 dfprintk(MOUNT, "NFS: unrecognized mount option "
1278 "'%s'\n", p);
1108 } 1279 }
1109 } 1280 }
1110 1281
1111 nfs_set_port((struct sockaddr *)&mnt->nfs_server.address,
1112 mnt->nfs_server.port);
1113
1114 return 1; 1282 return 1;
1115 1283
1116out_nomem: 1284out_nomem:
@@ -1120,21 +1288,6 @@ out_security_failure:
1120 free_secdata(secdata); 1288 free_secdata(secdata);
1121 printk(KERN_INFO "NFS: security options invalid: %d\n", rc); 1289 printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1122 return 0; 1290 return 0;
1123out_unrec_vers:
1124 printk(KERN_INFO "NFS: unrecognized NFS version number\n");
1125 return 0;
1126
1127out_unrec_xprt:
1128 printk(KERN_INFO "NFS: unrecognized transport protocol\n");
1129 return 0;
1130
1131out_unrec_sec:
1132 printk(KERN_INFO "NFS: unrecognized security flavor\n");
1133 return 0;
1134
1135out_unknown:
1136 printk(KERN_INFO "NFS: unknown mount option: %s\n", p);
1137 return 0;
1138} 1291}
1139 1292
1140/* 1293/*
@@ -1188,11 +1341,146 @@ static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1188 if (status == 0) 1341 if (status == 0)
1189 return 0; 1342 return 0;
1190 1343
1191 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d", 1344 dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1192 hostname, status); 1345 hostname, status);
1193 return status; 1346 return status;
1194} 1347}
1195 1348
1349static int nfs_parse_simple_hostname(const char *dev_name,
1350 char **hostname, size_t maxnamlen,
1351 char **export_path, size_t maxpathlen)
1352{
1353 size_t len;
1354 char *colon, *comma;
1355
1356 colon = strchr(dev_name, ':');
1357 if (colon == NULL)
1358 goto out_bad_devname;
1359
1360 len = colon - dev_name;
1361 if (len > maxnamlen)
1362 goto out_hostname;
1363
1364 /* N.B. caller will free nfs_server.hostname in all cases */
1365 *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1366 if (!*hostname)
1367 goto out_nomem;
1368
1369 /* kill possible hostname list: not supported */
1370 comma = strchr(*hostname, ',');
1371 if (comma != NULL) {
1372 if (comma == *hostname)
1373 goto out_bad_devname;
1374 *comma = '\0';
1375 }
1376
1377 colon++;
1378 len = strlen(colon);
1379 if (len > maxpathlen)
1380 goto out_path;
1381 *export_path = kstrndup(colon, len, GFP_KERNEL);
1382 if (!*export_path)
1383 goto out_nomem;
1384
1385 dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1386 return 0;
1387
1388out_bad_devname:
1389 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1390 return -EINVAL;
1391
1392out_nomem:
1393 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1394 return -ENOMEM;
1395
1396out_hostname:
1397 dfprintk(MOUNT, "NFS: server hostname too long\n");
1398 return -ENAMETOOLONG;
1399
1400out_path:
1401 dfprintk(MOUNT, "NFS: export pathname too long\n");
1402 return -ENAMETOOLONG;
1403}
1404
1405/*
1406 * Hostname has square brackets around it because it contains one or
1407 * more colons. We look for the first closing square bracket, and a
1408 * colon must follow it.
1409 */
1410static int nfs_parse_protected_hostname(const char *dev_name,
1411 char **hostname, size_t maxnamlen,
1412 char **export_path, size_t maxpathlen)
1413{
1414 size_t len;
1415 char *start, *end;
1416
1417 start = (char *)(dev_name + 1);
1418
1419 end = strchr(start, ']');
1420 if (end == NULL)
1421 goto out_bad_devname;
1422 if (*(end + 1) != ':')
1423 goto out_bad_devname;
1424
1425 len = end - start;
1426 if (len > maxnamlen)
1427 goto out_hostname;
1428
1429 /* N.B. caller will free nfs_server.hostname in all cases */
1430 *hostname = kstrndup(start, len, GFP_KERNEL);
1431 if (*hostname == NULL)
1432 goto out_nomem;
1433
1434 end += 2;
1435 len = strlen(end);
1436 if (len > maxpathlen)
1437 goto out_path;
1438 *export_path = kstrndup(end, len, GFP_KERNEL);
1439 if (!*export_path)
1440 goto out_nomem;
1441
1442 return 0;
1443
1444out_bad_devname:
1445 dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1446 return -EINVAL;
1447
1448out_nomem:
1449 dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1450 return -ENOMEM;
1451
1452out_hostname:
1453 dfprintk(MOUNT, "NFS: server hostname too long\n");
1454 return -ENAMETOOLONG;
1455
1456out_path:
1457 dfprintk(MOUNT, "NFS: export pathname too long\n");
1458 return -ENAMETOOLONG;
1459}
1460
1461/*
1462 * Split "dev_name" into "hostname:export_path".
1463 *
1464 * The leftmost colon demarks the split between the server's hostname
1465 * and the export path. If the hostname starts with a left square
1466 * bracket, then it may contain colons.
1467 *
1468 * Note: caller frees hostname and export path, even on error.
1469 */
1470static int nfs_parse_devname(const char *dev_name,
1471 char **hostname, size_t maxnamlen,
1472 char **export_path, size_t maxpathlen)
1473{
1474 if (*dev_name == '[')
1475 return nfs_parse_protected_hostname(dev_name,
1476 hostname, maxnamlen,
1477 export_path, maxpathlen);
1478
1479 return nfs_parse_simple_hostname(dev_name,
1480 hostname, maxnamlen,
1481 export_path, maxpathlen);
1482}
1483
1196/* 1484/*
1197 * Validate the NFS2/NFS3 mount data 1485 * Validate the NFS2/NFS3 mount data
1198 * - fills in the mount root filehandle 1486 * - fills in the mount root filehandle
@@ -1222,16 +1510,14 @@ static int nfs_validate_mount_data(void *options,
1222 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP); 1510 args->flags = (NFS_MOUNT_VER3 | NFS_MOUNT_TCP);
1223 args->rsize = NFS_MAX_FILE_IO_SIZE; 1511 args->rsize = NFS_MAX_FILE_IO_SIZE;
1224 args->wsize = NFS_MAX_FILE_IO_SIZE; 1512 args->wsize = NFS_MAX_FILE_IO_SIZE;
1225 args->timeo = 600; 1513 args->acregmin = NFS_DEF_ACREGMIN;
1226 args->retrans = 2; 1514 args->acregmax = NFS_DEF_ACREGMAX;
1227 args->acregmin = 3; 1515 args->acdirmin = NFS_DEF_ACDIRMIN;
1228 args->acregmax = 60; 1516 args->acdirmax = NFS_DEF_ACDIRMAX;
1229 args->acdirmin = 30;
1230 args->acdirmax = 60;
1231 args->mount_server.port = 0; /* autobind unless user sets port */ 1517 args->mount_server.port = 0; /* autobind unless user sets port */
1232 args->mount_server.protocol = XPRT_TRANSPORT_UDP;
1233 args->nfs_server.port = 0; /* autobind unless user sets port */ 1518 args->nfs_server.port = 0; /* autobind unless user sets port */
1234 args->nfs_server.protocol = XPRT_TRANSPORT_TCP; 1519 args->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1520 args->auth_flavors[0] = RPC_AUTH_UNIX;
1235 1521
1236 switch (data->version) { 1522 switch (data->version) {
1237 case 1: 1523 case 1:
@@ -1289,7 +1575,9 @@ static int nfs_validate_mount_data(void *options,
1289 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL); 1575 args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1290 args->namlen = data->namlen; 1576 args->namlen = data->namlen;
1291 args->bsize = data->bsize; 1577 args->bsize = data->bsize;
1292 args->auth_flavors[0] = data->pseudoflavor; 1578
1579 if (data->flags & NFS_MOUNT_SECFLAVOUR)
1580 args->auth_flavors[0] = data->pseudoflavor;
1293 if (!args->nfs_server.hostname) 1581 if (!args->nfs_server.hostname)
1294 goto out_nomem; 1582 goto out_nomem;
1295 1583
@@ -1321,8 +1609,6 @@ static int nfs_validate_mount_data(void *options,
1321 1609
1322 break; 1610 break;
1323 default: { 1611 default: {
1324 unsigned int len;
1325 char *c;
1326 int status; 1612 int status;
1327 1613
1328 if (nfs_parse_mount_options((char *)options, args) == 0) 1614 if (nfs_parse_mount_options((char *)options, args) == 0)
@@ -1332,21 +1618,22 @@ static int nfs_validate_mount_data(void *options,
1332 &args->nfs_server.address)) 1618 &args->nfs_server.address))
1333 goto out_no_address; 1619 goto out_no_address;
1334 1620
1335 c = strchr(dev_name, ':'); 1621 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1336 if (c == NULL) 1622 args->nfs_server.port);
1337 return -EINVAL;
1338 len = c - dev_name;
1339 /* N.B. caller will free nfs_server.hostname in all cases */
1340 args->nfs_server.hostname = kstrndup(dev_name, len, GFP_KERNEL);
1341 if (!args->nfs_server.hostname)
1342 goto out_nomem;
1343 1623
1344 c++; 1624 nfs_set_mount_transport_protocol(args);
1345 if (strlen(c) > NFS_MAXPATHLEN) 1625
1346 return -ENAMETOOLONG; 1626 status = nfs_parse_devname(dev_name,
1347 args->nfs_server.export_path = c; 1627 &args->nfs_server.hostname,
1628 PAGE_SIZE,
1629 &args->nfs_server.export_path,
1630 NFS_MAXPATHLEN);
1631 if (!status)
1632 status = nfs_try_mount(args, mntfh);
1633
1634 kfree(args->nfs_server.export_path);
1635 args->nfs_server.export_path = NULL;
1348 1636
1349 status = nfs_try_mount(args, mntfh);
1350 if (status) 1637 if (status)
1351 return status; 1638 return status;
1352 1639
@@ -1354,9 +1641,6 @@ static int nfs_validate_mount_data(void *options,
1354 } 1641 }
1355 } 1642 }
1356 1643
1357 if (!(args->flags & NFS_MOUNT_SECFLAVOUR))
1358 args->auth_flavors[0] = RPC_AUTH_UNIX;
1359
1360#ifndef CONFIG_NFS_V3 1644#ifndef CONFIG_NFS_V3
1361 if (args->flags & NFS_MOUNT_VER3) 1645 if (args->flags & NFS_MOUNT_VER3)
1362 goto out_v3_not_compiled; 1646 goto out_v3_not_compiled;
@@ -1396,6 +1680,80 @@ out_invalid_fh:
1396 return -EINVAL; 1680 return -EINVAL;
1397} 1681}
1398 1682
1683static int
1684nfs_compare_remount_data(struct nfs_server *nfss,
1685 struct nfs_parsed_mount_data *data)
1686{
1687 if (data->flags != nfss->flags ||
1688 data->rsize != nfss->rsize ||
1689 data->wsize != nfss->wsize ||
1690 data->retrans != nfss->client->cl_timeout->to_retries ||
1691 data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1692 data->acregmin != nfss->acregmin / HZ ||
1693 data->acregmax != nfss->acregmax / HZ ||
1694 data->acdirmin != nfss->acdirmin / HZ ||
1695 data->acdirmax != nfss->acdirmax / HZ ||
1696 data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1697 data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1698 memcmp(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1699 data->nfs_server.addrlen) != 0)
1700 return -EINVAL;
1701
1702 return 0;
1703}
1704
1705static int
1706nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1707{
1708 int error;
1709 struct nfs_server *nfss = sb->s_fs_info;
1710 struct nfs_parsed_mount_data *data;
1711 struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1712 struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
1713 u32 nfsvers = nfss->nfs_client->rpc_ops->version;
1714
1715 /*
1716 * Userspace mount programs that send binary options generally send
1717 * them populated with default values. We have no way to know which
1718 * ones were explicitly specified. Fall back to legacy behavior and
1719 * just return success.
1720 */
1721 if ((nfsvers == 4 && options4->version == 1) ||
1722 (nfsvers <= 3 && options->version >= 1 &&
1723 options->version <= 6))
1724 return 0;
1725
1726 data = kzalloc(sizeof(*data), GFP_KERNEL);
1727 if (data == NULL)
1728 return -ENOMEM;
1729
1730 /* fill out struct with values from existing mount */
1731 data->flags = nfss->flags;
1732 data->rsize = nfss->rsize;
1733 data->wsize = nfss->wsize;
1734 data->retrans = nfss->client->cl_timeout->to_retries;
1735 data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1736 data->acregmin = nfss->acregmin / HZ;
1737 data->acregmax = nfss->acregmax / HZ;
1738 data->acdirmin = nfss->acdirmin / HZ;
1739 data->acdirmax = nfss->acdirmax / HZ;
1740 data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1741 data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1742 memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1743 data->nfs_server.addrlen);
1744
1745 /* overwrite those values with any that were specified */
1746 error = nfs_parse_mount_options((char *)options, data);
1747 if (error < 0)
1748 goto out;
1749
1750 /* compare new mount options with old ones */
1751 error = nfs_compare_remount_data(nfss, data);
1752out:
1753 kfree(data);
1754 return error;
1755}
1756
1399/* 1757/*
1400 * Initialise the common bits of the superblock 1758 * Initialise the common bits of the superblock
1401 */ 1759 */
@@ -1811,14 +2169,13 @@ static int nfs4_validate_mount_data(void *options,
1811 2169
1812 args->rsize = NFS_MAX_FILE_IO_SIZE; 2170 args->rsize = NFS_MAX_FILE_IO_SIZE;
1813 args->wsize = NFS_MAX_FILE_IO_SIZE; 2171 args->wsize = NFS_MAX_FILE_IO_SIZE;
1814 args->timeo = 600; 2172 args->acregmin = NFS_DEF_ACREGMIN;
1815 args->retrans = 2; 2173 args->acregmax = NFS_DEF_ACREGMAX;
1816 args->acregmin = 3; 2174 args->acdirmin = NFS_DEF_ACDIRMIN;
1817 args->acregmax = 60; 2175 args->acdirmax = NFS_DEF_ACDIRMAX;
1818 args->acdirmin = 30;
1819 args->acdirmax = 60;
1820 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */ 2176 args->nfs_server.port = NFS_PORT; /* 2049 unless user set port= */
1821 args->nfs_server.protocol = XPRT_TRANSPORT_TCP; 2177 args->auth_flavors[0] = RPC_AUTH_UNIX;
2178 args->auth_flavor_len = 0;
1822 2179
1823 switch (data->version) { 2180 switch (data->version) {
1824 case 1: 2181 case 1:
@@ -1834,18 +2191,13 @@ static int nfs4_validate_mount_data(void *options,
1834 &args->nfs_server.address)) 2191 &args->nfs_server.address))
1835 goto out_no_address; 2192 goto out_no_address;
1836 2193
1837 switch (data->auth_flavourlen) { 2194 if (data->auth_flavourlen) {
1838 case 0: 2195 if (data->auth_flavourlen > 1)
1839 args->auth_flavors[0] = RPC_AUTH_UNIX; 2196 goto out_inval_auth;
1840 break;
1841 case 1:
1842 if (copy_from_user(&args->auth_flavors[0], 2197 if (copy_from_user(&args->auth_flavors[0],
1843 data->auth_flavours, 2198 data->auth_flavours,
1844 sizeof(args->auth_flavors[0]))) 2199 sizeof(args->auth_flavors[0])))
1845 return -EFAULT; 2200 return -EFAULT;
1846 break;
1847 default:
1848 goto out_inval_auth;
1849 } 2201 }
1850 2202
1851 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN); 2203 c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
@@ -1879,10 +2231,11 @@ static int nfs4_validate_mount_data(void *options,
1879 args->acdirmin = data->acdirmin; 2231 args->acdirmin = data->acdirmin;
1880 args->acdirmax = data->acdirmax; 2232 args->acdirmax = data->acdirmax;
1881 args->nfs_server.protocol = data->proto; 2233 args->nfs_server.protocol = data->proto;
2234 nfs_validate_transport_protocol(args);
1882 2235
1883 break; 2236 break;
1884 default: { 2237 default: {
1885 unsigned int len; 2238 int status;
1886 2239
1887 if (nfs_parse_mount_options((char *)options, args) == 0) 2240 if (nfs_parse_mount_options((char *)options, args) == 0)
1888 return -EINVAL; 2241 return -EINVAL;
@@ -1891,44 +2244,25 @@ static int nfs4_validate_mount_data(void *options,
1891 &args->nfs_server.address)) 2244 &args->nfs_server.address))
1892 return -EINVAL; 2245 return -EINVAL;
1893 2246
1894 switch (args->auth_flavor_len) { 2247 nfs_set_port((struct sockaddr *)&args->nfs_server.address,
1895 case 0: 2248 args->nfs_server.port);
1896 args->auth_flavors[0] = RPC_AUTH_UNIX;
1897 break;
1898 case 1:
1899 break;
1900 default:
1901 goto out_inval_auth;
1902 }
1903 2249
1904 /* 2250 nfs_validate_transport_protocol(args);
1905 * Split "dev_name" into "hostname:mntpath".
1906 */
1907 c = strchr(dev_name, ':');
1908 if (c == NULL)
1909 return -EINVAL;
1910 /* while calculating len, pretend ':' is '\0' */
1911 len = c - dev_name;
1912 if (len > NFS4_MAXNAMLEN)
1913 return -ENAMETOOLONG;
1914 /* N.B. caller will free nfs_server.hostname in all cases */
1915 args->nfs_server.hostname = kstrndup(dev_name, len, GFP_KERNEL);
1916 if (!args->nfs_server.hostname)
1917 goto out_nomem;
1918
1919 c++; /* step over the ':' */
1920 len = strlen(c);
1921 if (len > NFS4_MAXPATHLEN)
1922 return -ENAMETOOLONG;
1923 args->nfs_server.export_path = kstrndup(c, len, GFP_KERNEL);
1924 if (!args->nfs_server.export_path)
1925 goto out_nomem;
1926 2251
1927 dprintk("NFS: MNTPATH: '%s'\n", args->nfs_server.export_path); 2252 if (args->auth_flavor_len > 1)
2253 goto out_inval_auth;
1928 2254
1929 if (args->client_address == NULL) 2255 if (args->client_address == NULL)
1930 goto out_no_client_address; 2256 goto out_no_client_address;
1931 2257
2258 status = nfs_parse_devname(dev_name,
2259 &args->nfs_server.hostname,
2260 NFS4_MAXNAMLEN,
2261 &args->nfs_server.export_path,
2262 NFS4_MAXPATHLEN);
2263 if (status < 0)
2264 return status;
2265
1932 break; 2266 break;
1933 } 2267 }
1934 } 2268 }
@@ -1944,10 +2278,6 @@ out_inval_auth:
1944 data->auth_flavourlen); 2278 data->auth_flavourlen);
1945 return -EINVAL; 2279 return -EINVAL;
1946 2280
1947out_nomem:
1948 dfprintk(MOUNT, "NFS4: not enough memory to handle mount options\n");
1949 return -ENOMEM;
1950
1951out_no_address: 2281out_no_address:
1952 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n"); 2282 dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
1953 return -EINVAL; 2283 return -EINVAL;
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index f333848fd3be..3229e217c773 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -34,9 +34,6 @@
34/* 34/*
35 * Local function declarations 35 * Local function declarations
36 */ 36 */
37static struct nfs_page * nfs_update_request(struct nfs_open_context*,
38 struct page *,
39 unsigned int, unsigned int);
40static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, 37static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc,
41 struct inode *inode, int ioflags); 38 struct inode *inode, int ioflags);
42static void nfs_redirty_request(struct nfs_page *req); 39static void nfs_redirty_request(struct nfs_page *req);
@@ -136,16 +133,21 @@ static struct nfs_page *nfs_page_find_request(struct page *page)
136static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 133static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
137{ 134{
138 struct inode *inode = page->mapping->host; 135 struct inode *inode = page->mapping->host;
139 loff_t end, i_size = i_size_read(inode); 136 loff_t end, i_size;
140 pgoff_t end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 137 pgoff_t end_index;
141 138
139 spin_lock(&inode->i_lock);
140 i_size = i_size_read(inode);
141 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
142 if (i_size > 0 && page->index < end_index) 142 if (i_size > 0 && page->index < end_index)
143 return; 143 goto out;
144 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 144 end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
145 if (i_size >= end) 145 if (i_size >= end)
146 return; 146 goto out;
147 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
148 i_size_write(inode, end); 147 i_size_write(inode, end);
148 nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
149out:
150 spin_unlock(&inode->i_lock);
149} 151}
150 152
151/* A writeback failed: mark the page as bad, and invalidate the page cache */ 153/* A writeback failed: mark the page as bad, and invalidate the page cache */
@@ -169,29 +171,6 @@ static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int
169 SetPageUptodate(page); 171 SetPageUptodate(page);
170} 172}
171 173
172static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
173 unsigned int offset, unsigned int count)
174{
175 struct nfs_page *req;
176 int ret;
177
178 for (;;) {
179 req = nfs_update_request(ctx, page, offset, count);
180 if (!IS_ERR(req))
181 break;
182 ret = PTR_ERR(req);
183 if (ret != -EBUSY)
184 return ret;
185 ret = nfs_wb_page(page->mapping->host, page);
186 if (ret != 0)
187 return ret;
188 }
189 /* Update file length */
190 nfs_grow_file(page, offset, count);
191 nfs_clear_page_tag_locked(req);
192 return 0;
193}
194
195static int wb_priority(struct writeback_control *wbc) 174static int wb_priority(struct writeback_control *wbc)
196{ 175{
197 if (wbc->for_reclaim) 176 if (wbc->for_reclaim)
@@ -268,12 +247,9 @@ static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
268 return ret; 247 return ret;
269 spin_lock(&inode->i_lock); 248 spin_lock(&inode->i_lock);
270 } 249 }
271 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { 250 if (test_bit(PG_CLEAN, &req->wb_flags)) {
272 /* This request is marked for commit */
273 spin_unlock(&inode->i_lock); 251 spin_unlock(&inode->i_lock);
274 nfs_clear_page_tag_locked(req); 252 BUG();
275 nfs_pageio_complete(pgio);
276 return 0;
277 } 253 }
278 if (nfs_set_page_writeback(page) != 0) { 254 if (nfs_set_page_writeback(page) != 0) {
279 spin_unlock(&inode->i_lock); 255 spin_unlock(&inode->i_lock);
@@ -355,11 +331,19 @@ int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
355/* 331/*
356 * Insert a write request into an inode 332 * Insert a write request into an inode
357 */ 333 */
358static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 334static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
359{ 335{
360 struct nfs_inode *nfsi = NFS_I(inode); 336 struct nfs_inode *nfsi = NFS_I(inode);
361 int error; 337 int error;
362 338
339 error = radix_tree_preload(GFP_NOFS);
340 if (error != 0)
341 goto out;
342
343 /* Lock the request! */
344 nfs_lock_request_dontget(req);
345
346 spin_lock(&inode->i_lock);
363 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); 347 error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
364 BUG_ON(error); 348 BUG_ON(error);
365 if (!nfsi->npages) { 349 if (!nfsi->npages) {
@@ -373,6 +357,10 @@ static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
373 kref_get(&req->wb_kref); 357 kref_get(&req->wb_kref);
374 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, 358 radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index,
375 NFS_PAGE_TAG_LOCKED); 359 NFS_PAGE_TAG_LOCKED);
360 spin_unlock(&inode->i_lock);
361 radix_tree_preload_end();
362out:
363 return error;
376} 364}
377 365
378/* 366/*
@@ -405,19 +393,6 @@ nfs_mark_request_dirty(struct nfs_page *req)
405 __set_page_dirty_nobuffers(req->wb_page); 393 __set_page_dirty_nobuffers(req->wb_page);
406} 394}
407 395
408/*
409 * Check if a request is dirty
410 */
411static inline int
412nfs_dirty_request(struct nfs_page *req)
413{
414 struct page *page = req->wb_page;
415
416 if (page == NULL || test_bit(PG_NEED_COMMIT, &req->wb_flags))
417 return 0;
418 return !PageWriteback(page);
419}
420
421#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 396#if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
422/* 397/*
423 * Add a request to the inode's commit list. 398 * Add a request to the inode's commit list.
@@ -430,7 +405,7 @@ nfs_mark_request_commit(struct nfs_page *req)
430 405
431 spin_lock(&inode->i_lock); 406 spin_lock(&inode->i_lock);
432 nfsi->ncommit++; 407 nfsi->ncommit++;
433 set_bit(PG_NEED_COMMIT, &(req)->wb_flags); 408 set_bit(PG_CLEAN, &(req)->wb_flags);
434 radix_tree_tag_set(&nfsi->nfs_page_tree, 409 radix_tree_tag_set(&nfsi->nfs_page_tree,
435 req->wb_index, 410 req->wb_index,
436 NFS_PAGE_TAG_COMMIT); 411 NFS_PAGE_TAG_COMMIT);
@@ -440,6 +415,19 @@ nfs_mark_request_commit(struct nfs_page *req)
440 __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 415 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
441} 416}
442 417
418static int
419nfs_clear_request_commit(struct nfs_page *req)
420{
421 struct page *page = req->wb_page;
422
423 if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) {
424 dec_zone_page_state(page, NR_UNSTABLE_NFS);
425 dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE);
426 return 1;
427 }
428 return 0;
429}
430
443static inline 431static inline
444int nfs_write_need_commit(struct nfs_write_data *data) 432int nfs_write_need_commit(struct nfs_write_data *data)
445{ 433{
@@ -449,7 +437,7 @@ int nfs_write_need_commit(struct nfs_write_data *data)
449static inline 437static inline
450int nfs_reschedule_unstable_write(struct nfs_page *req) 438int nfs_reschedule_unstable_write(struct nfs_page *req)
451{ 439{
452 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { 440 if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) {
453 nfs_mark_request_commit(req); 441 nfs_mark_request_commit(req);
454 return 1; 442 return 1;
455 } 443 }
@@ -465,6 +453,12 @@ nfs_mark_request_commit(struct nfs_page *req)
465{ 453{
466} 454}
467 455
456static inline int
457nfs_clear_request_commit(struct nfs_page *req)
458{
459 return 0;
460}
461
468static inline 462static inline
469int nfs_write_need_commit(struct nfs_write_data *data) 463int nfs_write_need_commit(struct nfs_write_data *data)
470{ 464{
@@ -522,11 +516,8 @@ static void nfs_cancel_commit_list(struct list_head *head)
522 516
523 while(!list_empty(head)) { 517 while(!list_empty(head)) {
524 req = nfs_list_entry(head->next); 518 req = nfs_list_entry(head->next);
525 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
526 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
527 BDI_RECLAIMABLE);
528 nfs_list_remove_request(req); 519 nfs_list_remove_request(req);
529 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags); 520 nfs_clear_request_commit(req);
530 nfs_inode_remove_request(req); 521 nfs_inode_remove_request(req);
531 nfs_unlock_request(req); 522 nfs_unlock_request(req);
532 } 523 }
@@ -564,110 +555,124 @@ static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pg
564#endif 555#endif
565 556
566/* 557/*
567 * Try to update any existing write request, or create one if there is none. 558 * Search for an existing write request, and attempt to update
568 * In order to match, the request's credentials must match those of 559 * it to reflect a new dirty region on a given page.
569 * the calling process.
570 * 560 *
571 * Note: Should always be called with the Page Lock held! 561 * If the attempt fails, then the existing request is flushed out
562 * to disk.
572 */ 563 */
573static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx, 564static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
574 struct page *page, unsigned int offset, unsigned int bytes) 565 struct page *page,
566 unsigned int offset,
567 unsigned int bytes)
575{ 568{
576 struct address_space *mapping = page->mapping; 569 struct nfs_page *req;
577 struct inode *inode = mapping->host; 570 unsigned int rqend;
578 struct nfs_page *req, *new = NULL; 571 unsigned int end;
579 pgoff_t rqend, end; 572 int error;
573
574 if (!PagePrivate(page))
575 return NULL;
580 576
581 end = offset + bytes; 577 end = offset + bytes;
578 spin_lock(&inode->i_lock);
582 579
583 for (;;) { 580 for (;;) {
584 /* Loop over all inode entries and see if we find 581 req = nfs_page_find_request_locked(page);
585 * A request for the page we wish to update 582 if (req == NULL)
583 goto out_unlock;
584
585 rqend = req->wb_offset + req->wb_bytes;
586 /*
587 * Tell the caller to flush out the request if
588 * the offsets are non-contiguous.
589 * Note: nfs_flush_incompatible() will already
590 * have flushed out requests having wrong owners.
586 */ 591 */
587 if (new) { 592 if (offset > rqend
588 if (radix_tree_preload(GFP_NOFS)) { 593 || end < req->wb_offset)
589 nfs_release_request(new); 594 goto out_flushme;
590 return ERR_PTR(-ENOMEM);
591 }
592 }
593 595
594 spin_lock(&inode->i_lock); 596 if (nfs_set_page_tag_locked(req))
595 req = nfs_page_find_request_locked(page);
596 if (req) {
597 if (!nfs_set_page_tag_locked(req)) {
598 int error;
599
600 spin_unlock(&inode->i_lock);
601 error = nfs_wait_on_request(req);
602 nfs_release_request(req);
603 if (error < 0) {
604 if (new) {
605 radix_tree_preload_end();
606 nfs_release_request(new);
607 }
608 return ERR_PTR(error);
609 }
610 continue;
611 }
612 spin_unlock(&inode->i_lock);
613 if (new) {
614 radix_tree_preload_end();
615 nfs_release_request(new);
616 }
617 break; 597 break;
618 }
619 598
620 if (new) { 599 /* The request is locked, so wait and then retry */
621 nfs_lock_request_dontget(new);
622 nfs_inode_add_request(inode, new);
623 spin_unlock(&inode->i_lock);
624 radix_tree_preload_end();
625 req = new;
626 goto zero_page;
627 }
628 spin_unlock(&inode->i_lock); 600 spin_unlock(&inode->i_lock);
629 601 error = nfs_wait_on_request(req);
630 new = nfs_create_request(ctx, inode, page, offset, bytes); 602 nfs_release_request(req);
631 if (IS_ERR(new)) 603 if (error != 0)
632 return new; 604 goto out_err;
605 spin_lock(&inode->i_lock);
633 } 606 }
634 607
635 /* We have a request for our page. 608 if (nfs_clear_request_commit(req))
636 * If the creds don't match, or the 609 radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree,
637 * page addresses don't match, 610 req->wb_index, NFS_PAGE_TAG_COMMIT);
638 * tell the caller to wait on the conflicting
639 * request.
640 */
641 rqend = req->wb_offset + req->wb_bytes;
642 if (req->wb_context != ctx
643 || req->wb_page != page
644 || !nfs_dirty_request(req)
645 || offset > rqend || end < req->wb_offset) {
646 nfs_clear_page_tag_locked(req);
647 return ERR_PTR(-EBUSY);
648 }
649 611
650 /* Okay, the request matches. Update the region */ 612 /* Okay, the request matches. Update the region */
651 if (offset < req->wb_offset) { 613 if (offset < req->wb_offset) {
652 req->wb_offset = offset; 614 req->wb_offset = offset;
653 req->wb_pgbase = offset; 615 req->wb_pgbase = offset;
654 req->wb_bytes = max(end, rqend) - req->wb_offset;
655 goto zero_page;
656 } 616 }
657
658 if (end > rqend) 617 if (end > rqend)
659 req->wb_bytes = end - req->wb_offset; 618 req->wb_bytes = end - req->wb_offset;
660 619 else
620 req->wb_bytes = rqend - req->wb_offset;
621out_unlock:
622 spin_unlock(&inode->i_lock);
661 return req; 623 return req;
662zero_page: 624out_flushme:
663 /* If this page might potentially be marked as up to date, 625 spin_unlock(&inode->i_lock);
664 * then we need to zero any uninitalised data. */ 626 nfs_release_request(req);
665 if (req->wb_pgbase == 0 && req->wb_bytes != PAGE_CACHE_SIZE 627 error = nfs_wb_page(inode, page);
666 && !PageUptodate(req->wb_page)) 628out_err:
667 zero_user_segment(req->wb_page, req->wb_bytes, PAGE_CACHE_SIZE); 629 return ERR_PTR(error);
630}
631
632/*
633 * Try to update an existing write request, or create one if there is none.
634 *
635 * Note: Should always be called with the Page Lock held to prevent races
636 * if we have to add a new request. Also assumes that the caller has
637 * already called nfs_flush_incompatible() if necessary.
638 */
639static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
640 struct page *page, unsigned int offset, unsigned int bytes)
641{
642 struct inode *inode = page->mapping->host;
643 struct nfs_page *req;
644 int error;
645
646 req = nfs_try_to_update_request(inode, page, offset, bytes);
647 if (req != NULL)
648 goto out;
649 req = nfs_create_request(ctx, inode, page, offset, bytes);
650 if (IS_ERR(req))
651 goto out;
652 error = nfs_inode_add_request(inode, req);
653 if (error != 0) {
654 nfs_release_request(req);
655 req = ERR_PTR(error);
656 }
657out:
668 return req; 658 return req;
669} 659}
670 660
661static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
662 unsigned int offset, unsigned int count)
663{
664 struct nfs_page *req;
665
666 req = nfs_setup_write_request(ctx, page, offset, count);
667 if (IS_ERR(req))
668 return PTR_ERR(req);
669 /* Update file length */
670 nfs_grow_file(page, offset, count);
671 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
672 nfs_clear_page_tag_locked(req);
673 return 0;
674}
675
671int nfs_flush_incompatible(struct file *file, struct page *page) 676int nfs_flush_incompatible(struct file *file, struct page *page)
672{ 677{
673 struct nfs_open_context *ctx = nfs_file_open_context(file); 678 struct nfs_open_context *ctx = nfs_file_open_context(file);
@@ -685,8 +690,7 @@ int nfs_flush_incompatible(struct file *file, struct page *page)
685 req = nfs_page_find_request(page); 690 req = nfs_page_find_request(page);
686 if (req == NULL) 691 if (req == NULL)
687 return 0; 692 return 0;
688 do_flush = req->wb_page != page || req->wb_context != ctx 693 do_flush = req->wb_page != page || req->wb_context != ctx;
689 || !nfs_dirty_request(req);
690 nfs_release_request(req); 694 nfs_release_request(req);
691 if (!do_flush) 695 if (!do_flush)
692 return 0; 696 return 0;
@@ -721,10 +725,10 @@ int nfs_updatepage(struct file *file, struct page *page,
721 725
722 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 726 nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
723 727
724 dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n", 728 dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n",
725 file->f_path.dentry->d_parent->d_name.name, 729 file->f_path.dentry->d_parent->d_name.name,
726 file->f_path.dentry->d_name.name, count, 730 file->f_path.dentry->d_name.name, count,
727 (long long)(page_offset(page) +offset)); 731 (long long)(page_offset(page) + offset));
728 732
729 /* If we're not using byte range locks, and we know the page 733 /* If we're not using byte range locks, and we know the page
730 * is up to date, it may be more efficient to extend the write 734 * is up to date, it may be more efficient to extend the write
@@ -744,7 +748,7 @@ int nfs_updatepage(struct file *file, struct page *page,
744 else 748 else
745 __set_page_dirty_nobuffers(page); 749 __set_page_dirty_nobuffers(page);
746 750
747 dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n", 751 dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
748 status, (long long)i_size_read(inode)); 752 status, (long long)i_size_read(inode));
749 return status; 753 return status;
750} 754}
@@ -752,12 +756,7 @@ int nfs_updatepage(struct file *file, struct page *page,
752static void nfs_writepage_release(struct nfs_page *req) 756static void nfs_writepage_release(struct nfs_page *req)
753{ 757{
754 758
755 if (PageError(req->wb_page)) { 759 if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) {
756 nfs_end_page_writeback(req->wb_page);
757 nfs_inode_remove_request(req);
758 } else if (!nfs_reschedule_unstable_write(req)) {
759 /* Set the PG_uptodate flag */
760 nfs_mark_uptodate(req->wb_page, req->wb_pgbase, req->wb_bytes);
761 nfs_end_page_writeback(req->wb_page); 760 nfs_end_page_writeback(req->wb_page);
762 nfs_inode_remove_request(req); 761 nfs_inode_remove_request(req);
763 } else 762 } else
@@ -834,7 +833,7 @@ static int nfs_write_rpcsetup(struct nfs_page *req,
834 NFS_PROTO(inode)->write_setup(data, &msg); 833 NFS_PROTO(inode)->write_setup(data, &msg);
835 834
836 dprintk("NFS: %5u initiated write call " 835 dprintk("NFS: %5u initiated write call "
837 "(req %s/%Ld, %u bytes @ offset %Lu)\n", 836 "(req %s/%lld, %u bytes @ offset %llu)\n",
838 data->task.tk_pid, 837 data->task.tk_pid,
839 inode->i_sb->s_id, 838 inode->i_sb->s_id,
840 (long long)NFS_FILEID(inode), 839 (long long)NFS_FILEID(inode),
@@ -978,13 +977,13 @@ static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
978static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 977static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
979{ 978{
980 struct nfs_write_data *data = calldata; 979 struct nfs_write_data *data = calldata;
981 struct nfs_page *req = data->req;
982 980
983 dprintk("NFS: write (%s/%Ld %d@%Ld)", 981 dprintk("NFS: %5u write(%s/%lld %d@%lld)",
984 req->wb_context->path.dentry->d_inode->i_sb->s_id, 982 task->tk_pid,
985 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 983 data->req->wb_context->path.dentry->d_inode->i_sb->s_id,
986 req->wb_bytes, 984 (long long)
987 (long long)req_offset(req)); 985 NFS_FILEID(data->req->wb_context->path.dentry->d_inode),
986 data->req->wb_bytes, (long long)req_offset(data->req));
988 987
989 nfs_writeback_done(task, data); 988 nfs_writeback_done(task, data);
990} 989}
@@ -1058,7 +1057,8 @@ static void nfs_writeback_release_full(void *calldata)
1058 1057
1059 nfs_list_remove_request(req); 1058 nfs_list_remove_request(req);
1060 1059
1061 dprintk("NFS: write (%s/%Ld %d@%Ld)", 1060 dprintk("NFS: %5u write (%s/%lld %d@%lld)",
1061 data->task.tk_pid,
1062 req->wb_context->path.dentry->d_inode->i_sb->s_id, 1062 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1063 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 1063 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1064 req->wb_bytes, 1064 req->wb_bytes,
@@ -1078,8 +1078,6 @@ static void nfs_writeback_release_full(void *calldata)
1078 dprintk(" marked for commit\n"); 1078 dprintk(" marked for commit\n");
1079 goto next; 1079 goto next;
1080 } 1080 }
1081 /* Set the PG_uptodate flag? */
1082 nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes);
1083 dprintk(" OK\n"); 1081 dprintk(" OK\n");
1084remove_request: 1082remove_request:
1085 nfs_end_page_writeback(page); 1083 nfs_end_page_writeback(page);
@@ -1133,7 +1131,7 @@ int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
1133 static unsigned long complain; 1131 static unsigned long complain;
1134 1132
1135 if (time_before(complain, jiffies)) { 1133 if (time_before(complain, jiffies)) {
1136 dprintk("NFS: faulty NFS server %s:" 1134 dprintk("NFS: faulty NFS server %s:"
1137 " (committed = %d) != (stable = %d)\n", 1135 " (committed = %d) != (stable = %d)\n",
1138 NFS_SERVER(data->inode)->nfs_client->cl_hostname, 1136 NFS_SERVER(data->inode)->nfs_client->cl_hostname,
1139 resp->verf->committed, argp->stable); 1137 resp->verf->committed, argp->stable);
@@ -1297,12 +1295,9 @@ static void nfs_commit_release(void *calldata)
1297 while (!list_empty(&data->pages)) { 1295 while (!list_empty(&data->pages)) {
1298 req = nfs_list_entry(data->pages.next); 1296 req = nfs_list_entry(data->pages.next);
1299 nfs_list_remove_request(req); 1297 nfs_list_remove_request(req);
1300 clear_bit(PG_NEED_COMMIT, &(req)->wb_flags); 1298 nfs_clear_request_commit(req);
1301 dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
1302 dec_bdi_stat(req->wb_page->mapping->backing_dev_info,
1303 BDI_RECLAIMABLE);
1304 1299
1305 dprintk("NFS: commit (%s/%Ld %d@%Ld)", 1300 dprintk("NFS: commit (%s/%lld %d@%lld)",
1306 req->wb_context->path.dentry->d_inode->i_sb->s_id, 1301 req->wb_context->path.dentry->d_inode->i_sb->s_id,
1307 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), 1302 (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode),
1308 req->wb_bytes, 1303 req->wb_bytes,
@@ -1318,9 +1313,6 @@ static void nfs_commit_release(void *calldata)
1318 * returned by the server against all stored verfs. */ 1313 * returned by the server against all stored verfs. */
1319 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 1314 if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
1320 /* We have a match */ 1315 /* We have a match */
1321 /* Set the PG_uptodate flag */
1322 nfs_mark_uptodate(req->wb_page, req->wb_pgbase,
1323 req->wb_bytes);
1324 nfs_inode_remove_request(req); 1316 nfs_inode_remove_request(req);
1325 dprintk(" OK\n"); 1317 dprintk(" OK\n");
1326 goto next; 1318 goto next;
@@ -1479,7 +1471,7 @@ int nfs_wb_page_cancel(struct inode *inode, struct page *page)
1479 req = nfs_page_find_request(page); 1471 req = nfs_page_find_request(page);
1480 if (req == NULL) 1472 if (req == NULL)
1481 goto out; 1473 goto out;
1482 if (test_bit(PG_NEED_COMMIT, &req->wb_flags)) { 1474 if (test_bit(PG_CLEAN, &req->wb_flags)) {
1483 nfs_release_request(req); 1475 nfs_release_request(req);
1484 break; 1476 break;
1485 } 1477 }
diff --git a/fs/nfsd/nfs4callback.c b/fs/nfsd/nfs4callback.c
index 4d4760e687c3..702fa577aa6e 100644
--- a/fs/nfsd/nfs4callback.c
+++ b/fs/nfsd/nfs4callback.c
@@ -381,7 +381,7 @@ static int do_probe_callback(void *data)
381 .program = &cb_program, 381 .program = &cb_program,
382 .version = nfs_cb_version[1]->number, 382 .version = nfs_cb_version[1]->number,
383 .authflavor = RPC_AUTH_UNIX, /* XXX: need AUTH_GSS... */ 383 .authflavor = RPC_AUTH_UNIX, /* XXX: need AUTH_GSS... */
384 .flags = (RPC_CLNT_CREATE_NOPING), 384 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
385 }; 385 };
386 struct rpc_message msg = { 386 struct rpc_message msg = {
387 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL], 387 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
diff --git a/fs/ubifs/Kconfig b/fs/ubifs/Kconfig
new file mode 100644
index 000000000000..91ceeda7e5bf
--- /dev/null
+++ b/fs/ubifs/Kconfig
@@ -0,0 +1,72 @@
1config UBIFS_FS
2 tristate "UBIFS file system support"
3 select CRC16
4 select CRC32
5 select CRYPTO if UBIFS_FS_ADVANCED_COMPR
6 select CRYPTO if UBIFS_FS_LZO
7 select CRYPTO if UBIFS_FS_ZLIB
8 select CRYPTO_LZO if UBIFS_FS_LZO
9 select CRYPTO_DEFLATE if UBIFS_FS_ZLIB
10 depends on MTD_UBI
11 help
12 UBIFS is a file system for flash devices which works on top of UBI.
13
14config UBIFS_FS_XATTR
15 bool "Extended attributes support"
16 depends on UBIFS_FS
17 help
18 This option enables support of extended attributes.
19
20config UBIFS_FS_ADVANCED_COMPR
21 bool "Advanced compression options"
22 depends on UBIFS_FS
23 help
24 This option allows to explicitly choose which compressions, if any,
25 are enabled in UBIFS. Removing compressors means inbility to read
26 existing file systems.
27
28 If unsure, say 'N'.
29
30config UBIFS_FS_LZO
31 bool "LZO compression support" if UBIFS_FS_ADVANCED_COMPR
32 depends on UBIFS_FS
33 default y
34 help
35 LZO compressor is generally faster then zlib but compresses worse.
36 Say 'Y' if unsure.
37
38config UBIFS_FS_ZLIB
39 bool "ZLIB compression support" if UBIFS_FS_ADVANCED_COMPR
40 depends on UBIFS_FS
41 default y
42 help
43 Zlib copresses better then LZO but it is slower. Say 'Y' if unsure.
44
45# Debugging-related stuff
46config UBIFS_FS_DEBUG
47 bool "Enable debugging"
48 depends on UBIFS_FS
49 select DEBUG_FS
50 select KALLSYMS_ALL
51 help
52 This option enables UBIFS debugging.
53
54config UBIFS_FS_DEBUG_MSG_LVL
55 int "Default message level (0 = no extra messages, 3 = lots)"
56 depends on UBIFS_FS_DEBUG
57 default "0"
58 help
59 This controls the amount of debugging messages produced by UBIFS.
60 If reporting bugs, please try to have available a full dump of the
61 messages at level 1 while the misbehaviour was occurring. Level 2
62 may become necessary if level 1 messages were not enough to find the
63 bug. Generally Level 3 should be avoided.
64
65config UBIFS_FS_DEBUG_CHKS
66 bool "Enable extra checks"
67 depends on UBIFS_FS_DEBUG
68 help
69 If extra checks are enabled UBIFS will check the consistency of its
70 internal data structures during operation. However, UBIFS performance
71 is dramatically slower when this option is selected especially if the
72 file system is large.
diff --git a/fs/ubifs/Makefile b/fs/ubifs/Makefile
new file mode 100644
index 000000000000..80e93c35e496
--- /dev/null
+++ b/fs/ubifs/Makefile
@@ -0,0 +1,9 @@
1obj-$(CONFIG_UBIFS_FS) += ubifs.o
2
3ubifs-y += shrinker.o journal.o file.o dir.o super.o sb.o io.o
4ubifs-y += tnc.o master.o scan.o replay.o log.o commit.o gc.o orphan.o
5ubifs-y += budget.o find.o tnc_commit.o compress.o lpt.o lprops.o
6ubifs-y += recovery.o ioctl.o lpt_commit.o tnc_misc.o
7
8ubifs-$(CONFIG_UBIFS_FS_DEBUG) += debug.o
9ubifs-$(CONFIG_UBIFS_FS_XATTR) += xattr.o
diff --git a/fs/ubifs/budget.c b/fs/ubifs/budget.c
new file mode 100644
index 000000000000..d81fb9ed2b8e
--- /dev/null
+++ b/fs/ubifs/budget.c
@@ -0,0 +1,731 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements the budgeting sub-system which is responsible for UBIFS
25 * space management.
26 *
27 * Factors such as compression, wasted space at the ends of LEBs, space in other
28 * journal heads, the effect of updates on the index, and so on, make it
29 * impossible to accurately predict the amount of space needed. Consequently
30 * approximations are used.
31 */
32
33#include "ubifs.h"
34#include <linux/writeback.h>
35#include <asm/div64.h>
36
37/*
38 * When pessimistic budget calculations say that there is no enough space,
39 * UBIFS starts writing back dirty inodes and pages, doing garbage collection,
40 * or committing. The below constants define maximum number of times UBIFS
41 * repeats the operations.
42 */
43#define MAX_SHRINK_RETRIES 8
44#define MAX_GC_RETRIES 4
45#define MAX_CMT_RETRIES 2
46#define MAX_NOSPC_RETRIES 1
47
48/*
49 * The below constant defines amount of dirty pages which should be written
50 * back at when trying to shrink the liability.
51 */
52#define NR_TO_WRITE 16
53
54/**
55 * struct retries_info - information about re-tries while making free space.
56 * @prev_liability: previous liability
57 * @shrink_cnt: how many times the liability was shrinked
58 * @shrink_retries: count of liability shrink re-tries (increased when
59 * liability does not shrink)
60 * @try_gc: GC should be tried first
61 * @gc_retries: how many times GC was run
62 * @cmt_retries: how many times commit has been done
63 * @nospc_retries: how many times GC returned %-ENOSPC
64 *
65 * Since we consider budgeting to be the fast-path, and this structure has to
66 * be allocated on stack and zeroed out, we make it smaller using bit-fields.
67 */
68struct retries_info {
69 long long prev_liability;
70 unsigned int shrink_cnt;
71 unsigned int shrink_retries:5;
72 unsigned int try_gc:1;
73 unsigned int gc_retries:4;
74 unsigned int cmt_retries:3;
75 unsigned int nospc_retries:1;
76};
77
78/**
79 * shrink_liability - write-back some dirty pages/inodes.
80 * @c: UBIFS file-system description object
81 * @nr_to_write: how many dirty pages to write-back
82 *
83 * This function shrinks UBIFS liability by means of writing back some amount
84 * of dirty inodes and their pages. Returns the amount of pages which were
85 * written back. The returned value does not include dirty inodes which were
86 * synchronized.
87 *
88 * Note, this function synchronizes even VFS inodes which are locked
89 * (@i_mutex) by the caller of the budgeting function, because write-back does
90 * not touch @i_mutex.
91 */
92static int shrink_liability(struct ubifs_info *c, int nr_to_write)
93{
94 int nr_written;
95 struct writeback_control wbc = {
96 .sync_mode = WB_SYNC_NONE,
97 .range_end = LLONG_MAX,
98 .nr_to_write = nr_to_write,
99 };
100
101 generic_sync_sb_inodes(c->vfs_sb, &wbc);
102 nr_written = nr_to_write - wbc.nr_to_write;
103
104 if (!nr_written) {
105 /*
106 * Re-try again but wait on pages/inodes which are being
107 * written-back concurrently (e.g., by pdflush).
108 */
109 memset(&wbc, 0, sizeof(struct writeback_control));
110 wbc.sync_mode = WB_SYNC_ALL;
111 wbc.range_end = LLONG_MAX;
112 wbc.nr_to_write = nr_to_write;
113 generic_sync_sb_inodes(c->vfs_sb, &wbc);
114 nr_written = nr_to_write - wbc.nr_to_write;
115 }
116
117 dbg_budg("%d pages were written back", nr_written);
118 return nr_written;
119}
120
121
122/**
123 * run_gc - run garbage collector.
124 * @c: UBIFS file-system description object
125 *
126 * This function runs garbage collector to make some more free space. Returns
127 * zero if a free LEB has been produced, %-EAGAIN if commit is required, and a
128 * negative error code in case of failure.
129 */
130static int run_gc(struct ubifs_info *c)
131{
132 int err, lnum;
133
134 /* Make some free space by garbage-collecting dirty space */
135 down_read(&c->commit_sem);
136 lnum = ubifs_garbage_collect(c, 1);
137 up_read(&c->commit_sem);
138 if (lnum < 0)
139 return lnum;
140
141 /* GC freed one LEB, return it to lprops */
142 dbg_budg("GC freed LEB %d", lnum);
143 err = ubifs_return_leb(c, lnum);
144 if (err)
145 return err;
146 return 0;
147}
148
149/**
150 * make_free_space - make more free space on the file-system.
151 * @c: UBIFS file-system description object
152 * @ri: information about previous invocations of this function
153 *
154 * This function is called when an operation cannot be budgeted because there
155 * is supposedly no free space. But in most cases there is some free space:
156 * o budgeting is pessimistic, so it always budgets more then it is actually
157 * needed, so shrinking the liability is one way to make free space - the
158 * cached data will take less space then it was budgeted for;
159 * o GC may turn some dark space into free space (budgeting treats dark space
160 * as not available);
161 * o commit may free some LEB, i.e., turn freeable LEBs into free LEBs.
162 *
163 * So this function tries to do the above. Returns %-EAGAIN if some free space
164 * was presumably made and the caller has to re-try budgeting the operation.
165 * Returns %-ENOSPC if it couldn't do more free space, and other negative error
166 * codes on failures.
167 */
168static int make_free_space(struct ubifs_info *c, struct retries_info *ri)
169{
170 int err;
171
172 /*
173 * If we have some dirty pages and inodes (liability), try to write
174 * them back unless this was tried too many times without effect
175 * already.
176 */
177 if (ri->shrink_retries < MAX_SHRINK_RETRIES && !ri->try_gc) {
178 long long liability;
179
180 spin_lock(&c->space_lock);
181 liability = c->budg_idx_growth + c->budg_data_growth +
182 c->budg_dd_growth;
183 spin_unlock(&c->space_lock);
184
185 if (ri->prev_liability >= liability) {
186 /* Liability does not shrink, next time try GC then */
187 ri->shrink_retries += 1;
188 if (ri->gc_retries < MAX_GC_RETRIES)
189 ri->try_gc = 1;
190 dbg_budg("liability did not shrink: retries %d of %d",
191 ri->shrink_retries, MAX_SHRINK_RETRIES);
192 }
193
194 dbg_budg("force write-back (count %d)", ri->shrink_cnt);
195 shrink_liability(c, NR_TO_WRITE + ri->shrink_cnt);
196
197 ri->prev_liability = liability;
198 ri->shrink_cnt += 1;
199 return -EAGAIN;
200 }
201
202 /*
203 * Try to run garbage collector unless it was already tried too many
204 * times.
205 */
206 if (ri->gc_retries < MAX_GC_RETRIES) {
207 ri->gc_retries += 1;
208 dbg_budg("run GC, retries %d of %d",
209 ri->gc_retries, MAX_GC_RETRIES);
210
211 ri->try_gc = 0;
212 err = run_gc(c);
213 if (!err)
214 return -EAGAIN;
215
216 if (err == -EAGAIN) {
217 dbg_budg("GC asked to commit");
218 err = ubifs_run_commit(c);
219 if (err)
220 return err;
221 return -EAGAIN;
222 }
223
224 if (err != -ENOSPC)
225 return err;
226
227 /*
228 * GC could not make any progress. If this is the first time,
229 * then it makes sense to try to commit, because it might make
230 * some dirty space.
231 */
232 dbg_budg("GC returned -ENOSPC, retries %d",
233 ri->nospc_retries);
234 if (ri->nospc_retries >= MAX_NOSPC_RETRIES)
235 return err;
236 ri->nospc_retries += 1;
237 }
238
239 /* Neither GC nor write-back helped, try to commit */
240 if (ri->cmt_retries < MAX_CMT_RETRIES) {
241 ri->cmt_retries += 1;
242 dbg_budg("run commit, retries %d of %d",
243 ri->cmt_retries, MAX_CMT_RETRIES);
244 err = ubifs_run_commit(c);
245 if (err)
246 return err;
247 return -EAGAIN;
248 }
249 return -ENOSPC;
250}
251
252/**
253 * ubifs_calc_min_idx_lebs - calculate amount of eraseblocks for the index.
254 * @c: UBIFS file-system description object
255 *
256 * This function calculates and returns the number of eraseblocks which should
257 * be kept for index usage.
258 */
259int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
260{
261 int ret;
262 uint64_t idx_size;
263
264 idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx;
265
266 /* And make sure we have twice the index size of space reserved */
267 idx_size <<= 1;
268
269 /*
270 * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes'
271 * pair, nor similarly the two variables for the new index size, so we
272 * have to do this costly 64-bit division on fast-path.
273 */
274 if (do_div(idx_size, c->leb_size - c->max_idx_node_sz))
275 ret = idx_size + 1;
276 else
277 ret = idx_size;
278 /*
279 * The index head is not available for the in-the-gaps method, so add an
280 * extra LEB to compensate.
281 */
282 ret += 1;
283 /*
284 * At present the index needs at least 2 LEBs: one for the index head
285 * and one for in-the-gaps method (which currently does not cater for
286 * the index head and so excludes it from consideration).
287 */
288 if (ret < 2)
289 ret = 2;
290 return ret;
291}
292
293/**
294 * ubifs_calc_available - calculate available FS space.
295 * @c: UBIFS file-system description object
296 * @min_idx_lebs: minimum number of LEBs reserved for the index
297 *
298 * This function calculates and returns amount of FS space available for use.
299 */
300long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs)
301{
302 int subtract_lebs;
303 long long available;
304
305 /*
306 * Force the amount available to the total size reported if the used
307 * space is zero.
308 */
309 if (c->lst.total_used <= UBIFS_INO_NODE_SZ &&
310 c->budg_data_growth + c->budg_dd_growth == 0) {
311 /* Do the same calculation as for c->block_cnt */
312 available = c->main_lebs - 2;
313 available *= c->leb_size - c->dark_wm;
314 return available;
315 }
316
317 available = c->main_bytes - c->lst.total_used;
318
319 /*
320 * Now 'available' contains theoretically available flash space
321 * assuming there is no index, so we have to subtract the space which
322 * is reserved for the index.
323 */
324 subtract_lebs = min_idx_lebs;
325
326 /* Take into account that GC reserves one LEB for its own needs */
327 subtract_lebs += 1;
328
329 /*
330 * The GC journal head LEB is not really accessible. And since
331 * different write types go to different heads, we may count only on
332 * one head's space.
333 */
334 subtract_lebs += c->jhead_cnt - 1;
335
336 /* We also reserve one LEB for deletions, which bypass budgeting */
337 subtract_lebs += 1;
338
339 available -= (long long)subtract_lebs * c->leb_size;
340
341 /* Subtract the dead space which is not available for use */
342 available -= c->lst.total_dead;
343
344 /*
345 * Subtract dark space, which might or might not be usable - it depends
346 * on the data which we have on the media and which will be written. If
347 * this is a lot of uncompressed or not-compressible data, the dark
348 * space cannot be used.
349 */
350 available -= c->lst.total_dark;
351
352 /*
353 * However, there is more dark space. The index may be bigger than
354 * @min_idx_lebs. Those extra LEBs are assumed to be available, but
355 * their dark space is not included in total_dark, so it is subtracted
356 * here.
357 */
358 if (c->lst.idx_lebs > min_idx_lebs) {
359 subtract_lebs = c->lst.idx_lebs - min_idx_lebs;
360 available -= subtract_lebs * c->dark_wm;
361 }
362
363 /* The calculations are rough and may end up with a negative number */
364 return available > 0 ? available : 0;
365}
366
367/**
368 * can_use_rp - check whether the user is allowed to use reserved pool.
369 * @c: UBIFS file-system description object
370 *
371 * UBIFS has so-called "reserved pool" which is flash space reserved
372 * for the superuser and for uses whose UID/GID is recorded in UBIFS superblock.
373 * This function checks whether current user is allowed to use reserved pool.
374 * Returns %1 current user is allowed to use reserved pool and %0 otherwise.
375 */
376static int can_use_rp(struct ubifs_info *c)
377{
378 if (current->fsuid == c->rp_uid || capable(CAP_SYS_RESOURCE) ||
379 (c->rp_gid != 0 && in_group_p(c->rp_gid)))
380 return 1;
381 return 0;
382}
383
384/**
385 * do_budget_space - reserve flash space for index and data growth.
386 * @c: UBIFS file-system description object
387 *
388 * This function makes sure UBIFS has enough free eraseblocks for index growth
389 * and data.
390 *
391 * When budgeting index space, UBIFS reserves twice as more LEBs as the index
392 * would take if it was consolidated and written to the flash. This guarantees
393 * that the "in-the-gaps" commit method always succeeds and UBIFS will always
394 * be able to commit dirty index. So this function basically adds amount of
395 * budgeted index space to the size of the current index, multiplies this by 2,
396 * and makes sure this does not exceed the amount of free eraseblocks.
397 *
398 * Notes about @c->min_idx_lebs and @c->lst.idx_lebs variables:
399 * o @c->lst.idx_lebs is the number of LEBs the index currently uses. It might
400 * be large, because UBIFS does not do any index consolidation as long as
401 * there is free space. IOW, the index may take a lot of LEBs, but the LEBs
402 * will contain a lot of dirt.
403 * o @c->min_idx_lebs is the the index presumably takes. IOW, the index may be
404 * consolidated to take up to @c->min_idx_lebs LEBs.
405 *
406 * This function returns zero in case of success, and %-ENOSPC in case of
407 * failure.
408 */
409static int do_budget_space(struct ubifs_info *c)
410{
411 long long outstanding, available;
412 int lebs, rsvd_idx_lebs, min_idx_lebs;
413
414 /* First budget index space */
415 min_idx_lebs = ubifs_calc_min_idx_lebs(c);
416
417 /* Now 'min_idx_lebs' contains number of LEBs to reserve */
418 if (min_idx_lebs > c->lst.idx_lebs)
419 rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
420 else
421 rsvd_idx_lebs = 0;
422
423 /*
424 * The number of LEBs that are available to be used by the index is:
425 *
426 * @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt -
427 * @c->lst.taken_empty_lebs
428 *
429 * @empty_lebs are available because they are empty. @freeable_cnt are
430 * available because they contain only free and dirty space and the
431 * index allocation always occurs after wbufs are synch'ed.
432 * @idx_gc_cnt are available because they are index LEBs that have been
433 * garbage collected (including trivial GC) and are awaiting the commit
434 * before they can be unmapped - note that the in-the-gaps method will
435 * grab these if it needs them. @taken_empty_lebs are empty_lebs that
436 * have already been allocated for some purpose (also includes those
437 * LEBs on the @idx_gc list).
438 *
439 * Note, @taken_empty_lebs may temporarily be higher by one because of
440 * the way we serialize LEB allocations and budgeting. See a comment in
441 * 'ubifs_find_free_space()'.
442 */
443 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
444 c->lst.taken_empty_lebs;
445 if (unlikely(rsvd_idx_lebs > lebs)) {
446 dbg_budg("out of indexing space: min_idx_lebs %d (old %d), "
447 "rsvd_idx_lebs %d", min_idx_lebs, c->min_idx_lebs,
448 rsvd_idx_lebs);
449 return -ENOSPC;
450 }
451
452 available = ubifs_calc_available(c, min_idx_lebs);
453 outstanding = c->budg_data_growth + c->budg_dd_growth;
454
455 if (unlikely(available < outstanding)) {
456 dbg_budg("out of data space: available %lld, outstanding %lld",
457 available, outstanding);
458 return -ENOSPC;
459 }
460
461 if (available - outstanding <= c->rp_size && !can_use_rp(c))
462 return -ENOSPC;
463
464 c->min_idx_lebs = min_idx_lebs;
465 return 0;
466}
467
468/**
469 * calc_idx_growth - calculate approximate index growth from budgeting request.
470 * @c: UBIFS file-system description object
471 * @req: budgeting request
472 *
473 * For now we assume each new node adds one znode. But this is rather poor
474 * approximation, though.
475 */
476static int calc_idx_growth(const struct ubifs_info *c,
477 const struct ubifs_budget_req *req)
478{
479 int znodes;
480
481 znodes = req->new_ino + (req->new_page << UBIFS_BLOCKS_PER_PAGE_SHIFT) +
482 req->new_dent;
483 return znodes * c->max_idx_node_sz;
484}
485
486/**
487 * calc_data_growth - calculate approximate amount of new data from budgeting
488 * request.
489 * @c: UBIFS file-system description object
490 * @req: budgeting request
491 */
492static int calc_data_growth(const struct ubifs_info *c,
493 const struct ubifs_budget_req *req)
494{
495 int data_growth;
496
497 data_growth = req->new_ino ? c->inode_budget : 0;
498 if (req->new_page)
499 data_growth += c->page_budget;
500 if (req->new_dent)
501 data_growth += c->dent_budget;
502 data_growth += req->new_ino_d;
503 return data_growth;
504}
505
506/**
507 * calc_dd_growth - calculate approximate amount of data which makes other data
508 * dirty from budgeting request.
509 * @c: UBIFS file-system description object
510 * @req: budgeting request
511 */
512static int calc_dd_growth(const struct ubifs_info *c,
513 const struct ubifs_budget_req *req)
514{
515 int dd_growth;
516
517 dd_growth = req->dirtied_page ? c->page_budget : 0;
518
519 if (req->dirtied_ino)
520 dd_growth += c->inode_budget << (req->dirtied_ino - 1);
521 if (req->mod_dent)
522 dd_growth += c->dent_budget;
523 dd_growth += req->dirtied_ino_d;
524 return dd_growth;
525}
526
527/**
528 * ubifs_budget_space - ensure there is enough space to complete an operation.
529 * @c: UBIFS file-system description object
530 * @req: budget request
531 *
532 * This function allocates budget for an operation. It uses pessimistic
533 * approximation of how much flash space the operation needs. The goal of this
534 * function is to make sure UBIFS always has flash space to flush all dirty
535 * pages, dirty inodes, and dirty znodes (liability). This function may force
536 * commit, garbage-collection or write-back. Returns zero in case of success,
537 * %-ENOSPC if there is no free space and other negative error codes in case of
538 * failures.
539 */
540int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
541{
542 int uninitialized_var(cmt_retries), uninitialized_var(wb_retries);
543 int err, idx_growth, data_growth, dd_growth;
544 struct retries_info ri;
545
546 ubifs_assert(req->dirtied_ino <= 4);
547 ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
548
549 data_growth = calc_data_growth(c, req);
550 dd_growth = calc_dd_growth(c, req);
551 if (!data_growth && !dd_growth)
552 return 0;
553 idx_growth = calc_idx_growth(c, req);
554 memset(&ri, 0, sizeof(struct retries_info));
555
556again:
557 spin_lock(&c->space_lock);
558 ubifs_assert(c->budg_idx_growth >= 0);
559 ubifs_assert(c->budg_data_growth >= 0);
560 ubifs_assert(c->budg_dd_growth >= 0);
561
562 if (unlikely(c->nospace) && (c->nospace_rp || !can_use_rp(c))) {
563 dbg_budg("no space");
564 spin_unlock(&c->space_lock);
565 return -ENOSPC;
566 }
567
568 c->budg_idx_growth += idx_growth;
569 c->budg_data_growth += data_growth;
570 c->budg_dd_growth += dd_growth;
571
572 err = do_budget_space(c);
573 if (likely(!err)) {
574 req->idx_growth = idx_growth;
575 req->data_growth = data_growth;
576 req->dd_growth = dd_growth;
577 spin_unlock(&c->space_lock);
578 return 0;
579 }
580
581 /* Restore the old values */
582 c->budg_idx_growth -= idx_growth;
583 c->budg_data_growth -= data_growth;
584 c->budg_dd_growth -= dd_growth;
585 spin_unlock(&c->space_lock);
586
587 if (req->fast) {
588 dbg_budg("no space for fast budgeting");
589 return err;
590 }
591
592 err = make_free_space(c, &ri);
593 if (err == -EAGAIN) {
594 dbg_budg("try again");
595 cond_resched();
596 goto again;
597 } else if (err == -ENOSPC) {
598 dbg_budg("FS is full, -ENOSPC");
599 c->nospace = 1;
600 if (can_use_rp(c) || c->rp_size == 0)
601 c->nospace_rp = 1;
602 smp_wmb();
603 } else
604 ubifs_err("cannot budget space, error %d", err);
605 return err;
606}
607
608/**
609 * ubifs_release_budget - release budgeted free space.
610 * @c: UBIFS file-system description object
611 * @req: budget request
612 *
613 * This function releases the space budgeted by 'ubifs_budget_space()'. Note,
614 * since the index changes (which were budgeted for in @req->idx_growth) will
615 * only be written to the media on commit, this function moves the index budget
616 * from @c->budg_idx_growth to @c->budg_uncommitted_idx. The latter will be
617 * zeroed by the commit operation.
618 */
619void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
620{
621 ubifs_assert(req->dirtied_ino <= 4);
622 ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
623 if (!req->recalculate) {
624 ubifs_assert(req->idx_growth >= 0);
625 ubifs_assert(req->data_growth >= 0);
626 ubifs_assert(req->dd_growth >= 0);
627 }
628
629 if (req->recalculate) {
630 req->data_growth = calc_data_growth(c, req);
631 req->dd_growth = calc_dd_growth(c, req);
632 req->idx_growth = calc_idx_growth(c, req);
633 }
634
635 if (!req->data_growth && !req->dd_growth)
636 return;
637
638 c->nospace = c->nospace_rp = 0;
639 smp_wmb();
640
641 spin_lock(&c->space_lock);
642 c->budg_idx_growth -= req->idx_growth;
643 c->budg_uncommitted_idx += req->idx_growth;
644 c->budg_data_growth -= req->data_growth;
645 c->budg_dd_growth -= req->dd_growth;
646 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
647
648 ubifs_assert(c->budg_idx_growth >= 0);
649 ubifs_assert(c->budg_data_growth >= 0);
650 ubifs_assert(c->min_idx_lebs < c->main_lebs);
651 spin_unlock(&c->space_lock);
652}
653
654/**
655 * ubifs_convert_page_budget - convert budget of a new page.
656 * @c: UBIFS file-system description object
657 *
658 * This function converts budget which was allocated for a new page of data to
659 * the budget of changing an existing page of data. The latter is smaller then
660 * the former, so this function only does simple re-calculation and does not
661 * involve any write-back.
662 */
663void ubifs_convert_page_budget(struct ubifs_info *c)
664{
665 spin_lock(&c->space_lock);
666 /* Release the index growth reservation */
667 c->budg_idx_growth -= c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT;
668 /* Release the data growth reservation */
669 c->budg_data_growth -= c->page_budget;
670 /* Increase the dirty data growth reservation instead */
671 c->budg_dd_growth += c->page_budget;
672 /* And re-calculate the indexing space reservation */
673 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
674 spin_unlock(&c->space_lock);
675}
676
677/**
678 * ubifs_release_dirty_inode_budget - release dirty inode budget.
679 * @c: UBIFS file-system description object
680 * @ui: UBIFS inode to release the budget for
681 *
682 * This function releases budget corresponding to a dirty inode. It is usually
683 * called when after the inode has been written to the media and marked as
684 * clean.
685 */
686void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
687 struct ubifs_inode *ui)
688{
689 struct ubifs_budget_req req = {.dd_growth = c->inode_budget,
690 .dirtied_ino_d = ui->data_len};
691
692 ubifs_release_budget(c, &req);
693}
694
695/**
696 * ubifs_budg_get_free_space - return amount of free space.
697 * @c: UBIFS file-system description object
698 *
699 * This function returns amount of free space on the file-system.
700 */
701long long ubifs_budg_get_free_space(struct ubifs_info *c)
702{
703 int min_idx_lebs, rsvd_idx_lebs;
704 long long available, outstanding, free;
705
706 /* Do exactly the same calculations as in 'do_budget_space()' */
707 spin_lock(&c->space_lock);
708 min_idx_lebs = ubifs_calc_min_idx_lebs(c);
709
710 if (min_idx_lebs > c->lst.idx_lebs)
711 rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
712 else
713 rsvd_idx_lebs = 0;
714
715 if (rsvd_idx_lebs > c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt
716 - c->lst.taken_empty_lebs) {
717 spin_unlock(&c->space_lock);
718 return 0;
719 }
720
721 available = ubifs_calc_available(c, min_idx_lebs);
722 outstanding = c->budg_data_growth + c->budg_dd_growth;
723 c->min_idx_lebs = min_idx_lebs;
724 spin_unlock(&c->space_lock);
725
726 if (available > outstanding)
727 free = ubifs_reported_space(c, available - outstanding);
728 else
729 free = 0;
730 return free;
731}
diff --git a/fs/ubifs/commit.c b/fs/ubifs/commit.c
new file mode 100644
index 000000000000..3b516316c9b3
--- /dev/null
+++ b/fs/ubifs/commit.c
@@ -0,0 +1,677 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements functions that manage the running of the commit process.
25 * Each affected module has its own functions to accomplish their part in the
26 * commit and those functions are called here.
27 *
28 * The commit is the process whereby all updates to the index and LEB properties
29 * are written out together and the journal becomes empty. This keeps the
30 * file system consistent - at all times the state can be recreated by reading
31 * the index and LEB properties and then replaying the journal.
32 *
33 * The commit is split into two parts named "commit start" and "commit end".
34 * During commit start, the commit process has exclusive access to the journal
35 * by holding the commit semaphore down for writing. As few I/O operations as
36 * possible are performed during commit start, instead the nodes that are to be
37 * written are merely identified. During commit end, the commit semaphore is no
38 * longer held and the journal is again in operation, allowing users to continue
39 * to use the file system while the bulk of the commit I/O is performed. The
40 * purpose of this two-step approach is to prevent the commit from causing any
41 * latency blips. Note that in any case, the commit does not prevent lookups
42 * (as permitted by the TNC mutex), or access to VFS data structures e.g. page
43 * cache.
44 */
45
46#include <linux/freezer.h>
47#include <linux/kthread.h>
48#include "ubifs.h"
49
50/**
51 * do_commit - commit the journal.
52 * @c: UBIFS file-system description object
53 *
54 * This function implements UBIFS commit. It has to be called with commit lock
55 * locked. Returns zero in case of success and a negative error code in case of
56 * failure.
57 */
58static int do_commit(struct ubifs_info *c)
59{
60 int err, new_ltail_lnum, old_ltail_lnum, i;
61 struct ubifs_zbranch zroot;
62 struct ubifs_lp_stats lst;
63
64 dbg_cmt("start");
65 if (c->ro_media) {
66 err = -EROFS;
67 goto out_up;
68 }
69
70 /* Sync all write buffers (necessary for recovery) */
71 for (i = 0; i < c->jhead_cnt; i++) {
72 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
73 if (err)
74 goto out_up;
75 }
76
77 err = ubifs_gc_start_commit(c);
78 if (err)
79 goto out_up;
80 err = dbg_check_lprops(c);
81 if (err)
82 goto out_up;
83 err = ubifs_log_start_commit(c, &new_ltail_lnum);
84 if (err)
85 goto out_up;
86 err = ubifs_tnc_start_commit(c, &zroot);
87 if (err)
88 goto out_up;
89 err = ubifs_lpt_start_commit(c);
90 if (err)
91 goto out_up;
92 err = ubifs_orphan_start_commit(c);
93 if (err)
94 goto out_up;
95
96 ubifs_get_lp_stats(c, &lst);
97
98 up_write(&c->commit_sem);
99
100 err = ubifs_tnc_end_commit(c);
101 if (err)
102 goto out;
103 err = ubifs_lpt_end_commit(c);
104 if (err)
105 goto out;
106 err = ubifs_orphan_end_commit(c);
107 if (err)
108 goto out;
109 old_ltail_lnum = c->ltail_lnum;
110 err = ubifs_log_end_commit(c, new_ltail_lnum);
111 if (err)
112 goto out;
113 err = dbg_check_old_index(c, &zroot);
114 if (err)
115 goto out;
116
117 mutex_lock(&c->mst_mutex);
118 c->mst_node->cmt_no = cpu_to_le64(++c->cmt_no);
119 c->mst_node->log_lnum = cpu_to_le32(new_ltail_lnum);
120 c->mst_node->root_lnum = cpu_to_le32(zroot.lnum);
121 c->mst_node->root_offs = cpu_to_le32(zroot.offs);
122 c->mst_node->root_len = cpu_to_le32(zroot.len);
123 c->mst_node->ihead_lnum = cpu_to_le32(c->ihead_lnum);
124 c->mst_node->ihead_offs = cpu_to_le32(c->ihead_offs);
125 c->mst_node->index_size = cpu_to_le64(c->old_idx_sz);
126 c->mst_node->lpt_lnum = cpu_to_le32(c->lpt_lnum);
127 c->mst_node->lpt_offs = cpu_to_le32(c->lpt_offs);
128 c->mst_node->nhead_lnum = cpu_to_le32(c->nhead_lnum);
129 c->mst_node->nhead_offs = cpu_to_le32(c->nhead_offs);
130 c->mst_node->ltab_lnum = cpu_to_le32(c->ltab_lnum);
131 c->mst_node->ltab_offs = cpu_to_le32(c->ltab_offs);
132 c->mst_node->lsave_lnum = cpu_to_le32(c->lsave_lnum);
133 c->mst_node->lsave_offs = cpu_to_le32(c->lsave_offs);
134 c->mst_node->lscan_lnum = cpu_to_le32(c->lscan_lnum);
135 c->mst_node->empty_lebs = cpu_to_le32(lst.empty_lebs);
136 c->mst_node->idx_lebs = cpu_to_le32(lst.idx_lebs);
137 c->mst_node->total_free = cpu_to_le64(lst.total_free);
138 c->mst_node->total_dirty = cpu_to_le64(lst.total_dirty);
139 c->mst_node->total_used = cpu_to_le64(lst.total_used);
140 c->mst_node->total_dead = cpu_to_le64(lst.total_dead);
141 c->mst_node->total_dark = cpu_to_le64(lst.total_dark);
142 if (c->no_orphs)
143 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
144 else
145 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_NO_ORPHS);
146 err = ubifs_write_master(c);
147 mutex_unlock(&c->mst_mutex);
148 if (err)
149 goto out;
150
151 err = ubifs_log_post_commit(c, old_ltail_lnum);
152 if (err)
153 goto out;
154 err = ubifs_gc_end_commit(c);
155 if (err)
156 goto out;
157 err = ubifs_lpt_post_commit(c);
158 if (err)
159 goto out;
160
161 spin_lock(&c->cs_lock);
162 c->cmt_state = COMMIT_RESTING;
163 wake_up(&c->cmt_wq);
164 dbg_cmt("commit end");
165 spin_unlock(&c->cs_lock);
166
167 return 0;
168
169out_up:
170 up_write(&c->commit_sem);
171out:
172 ubifs_err("commit failed, error %d", err);
173 spin_lock(&c->cs_lock);
174 c->cmt_state = COMMIT_BROKEN;
175 wake_up(&c->cmt_wq);
176 spin_unlock(&c->cs_lock);
177 ubifs_ro_mode(c, err);
178 return err;
179}
180
181/**
182 * run_bg_commit - run background commit if it is needed.
183 * @c: UBIFS file-system description object
184 *
185 * This function runs background commit if it is needed. Returns zero in case
186 * of success and a negative error code in case of failure.
187 */
188static int run_bg_commit(struct ubifs_info *c)
189{
190 spin_lock(&c->cs_lock);
191 /*
192 * Run background commit only if background commit was requested or if
193 * commit is required.
194 */
195 if (c->cmt_state != COMMIT_BACKGROUND &&
196 c->cmt_state != COMMIT_REQUIRED)
197 goto out;
198 spin_unlock(&c->cs_lock);
199
200 down_write(&c->commit_sem);
201 spin_lock(&c->cs_lock);
202 if (c->cmt_state == COMMIT_REQUIRED)
203 c->cmt_state = COMMIT_RUNNING_REQUIRED;
204 else if (c->cmt_state == COMMIT_BACKGROUND)
205 c->cmt_state = COMMIT_RUNNING_BACKGROUND;
206 else
207 goto out_cmt_unlock;
208 spin_unlock(&c->cs_lock);
209
210 return do_commit(c);
211
212out_cmt_unlock:
213 up_write(&c->commit_sem);
214out:
215 spin_unlock(&c->cs_lock);
216 return 0;
217}
218
219/**
220 * ubifs_bg_thread - UBIFS background thread function.
221 * @info: points to the file-system description object
222 *
223 * This function implements various file-system background activities:
224 * o when a write-buffer timer expires it synchronizes the appropriate
225 * write-buffer;
226 * o when the journal is about to be full, it starts in-advance commit.
227 *
228 * Note, other stuff like background garbage collection may be added here in
229 * future.
230 */
231int ubifs_bg_thread(void *info)
232{
233 int err;
234 struct ubifs_info *c = info;
235
236 ubifs_msg("background thread \"%s\" started, PID %d",
237 c->bgt_name, current->pid);
238 set_freezable();
239
240 while (1) {
241 if (kthread_should_stop())
242 break;
243
244 if (try_to_freeze())
245 continue;
246
247 set_current_state(TASK_INTERRUPTIBLE);
248 /* Check if there is something to do */
249 if (!c->need_bgt) {
250 /*
251 * Nothing prevents us from going sleep now and
252 * be never woken up and block the task which
253 * could wait in 'kthread_stop()' forever.
254 */
255 if (kthread_should_stop())
256 break;
257 schedule();
258 continue;
259 } else
260 __set_current_state(TASK_RUNNING);
261
262 c->need_bgt = 0;
263 err = ubifs_bg_wbufs_sync(c);
264 if (err)
265 ubifs_ro_mode(c, err);
266
267 run_bg_commit(c);
268 cond_resched();
269 }
270
271 dbg_msg("background thread \"%s\" stops", c->bgt_name);
272 return 0;
273}
274
275/**
276 * ubifs_commit_required - set commit state to "required".
277 * @c: UBIFS file-system description object
278 *
279 * This function is called if a commit is required but cannot be done from the
280 * calling function, so it is just flagged instead.
281 */
282void ubifs_commit_required(struct ubifs_info *c)
283{
284 spin_lock(&c->cs_lock);
285 switch (c->cmt_state) {
286 case COMMIT_RESTING:
287 case COMMIT_BACKGROUND:
288 dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
289 dbg_cstate(COMMIT_REQUIRED));
290 c->cmt_state = COMMIT_REQUIRED;
291 break;
292 case COMMIT_RUNNING_BACKGROUND:
293 dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
294 dbg_cstate(COMMIT_RUNNING_REQUIRED));
295 c->cmt_state = COMMIT_RUNNING_REQUIRED;
296 break;
297 case COMMIT_REQUIRED:
298 case COMMIT_RUNNING_REQUIRED:
299 case COMMIT_BROKEN:
300 break;
301 }
302 spin_unlock(&c->cs_lock);
303}
304
305/**
306 * ubifs_request_bg_commit - notify the background thread to do a commit.
307 * @c: UBIFS file-system description object
308 *
309 * This function is called if the journal is full enough to make a commit
310 * worthwhile, so background thread is kicked to start it.
311 */
312void ubifs_request_bg_commit(struct ubifs_info *c)
313{
314 spin_lock(&c->cs_lock);
315 if (c->cmt_state == COMMIT_RESTING) {
316 dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
317 dbg_cstate(COMMIT_BACKGROUND));
318 c->cmt_state = COMMIT_BACKGROUND;
319 spin_unlock(&c->cs_lock);
320 ubifs_wake_up_bgt(c);
321 } else
322 spin_unlock(&c->cs_lock);
323}
324
325/**
326 * wait_for_commit - wait for commit.
327 * @c: UBIFS file-system description object
328 *
329 * This function sleeps until the commit operation is no longer running.
330 */
331static int wait_for_commit(struct ubifs_info *c)
332{
333 dbg_cmt("pid %d goes sleep", current->pid);
334
335 /*
336 * The following sleeps if the condition is false, and will be woken
337 * when the commit ends. It is possible, although very unlikely, that we
338 * will wake up and see the subsequent commit running, rather than the
339 * one we were waiting for, and go back to sleep. However, we will be
340 * woken again, so there is no danger of sleeping forever.
341 */
342 wait_event(c->cmt_wq, c->cmt_state != COMMIT_RUNNING_BACKGROUND &&
343 c->cmt_state != COMMIT_RUNNING_REQUIRED);
344 dbg_cmt("commit finished, pid %d woke up", current->pid);
345 return 0;
346}
347
348/**
349 * ubifs_run_commit - run or wait for commit.
350 * @c: UBIFS file-system description object
351 *
352 * This function runs commit and returns zero in case of success and a negative
353 * error code in case of failure.
354 */
355int ubifs_run_commit(struct ubifs_info *c)
356{
357 int err = 0;
358
359 spin_lock(&c->cs_lock);
360 if (c->cmt_state == COMMIT_BROKEN) {
361 err = -EINVAL;
362 goto out;
363 }
364
365 if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
366 /*
367 * We set the commit state to 'running required' to indicate
368 * that we want it to complete as quickly as possible.
369 */
370 c->cmt_state = COMMIT_RUNNING_REQUIRED;
371
372 if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
373 spin_unlock(&c->cs_lock);
374 return wait_for_commit(c);
375 }
376 spin_unlock(&c->cs_lock);
377
378 /* Ok, the commit is indeed needed */
379
380 down_write(&c->commit_sem);
381 spin_lock(&c->cs_lock);
382 /*
383 * Since we unlocked 'c->cs_lock', the state may have changed, so
384 * re-check it.
385 */
386 if (c->cmt_state == COMMIT_BROKEN) {
387 err = -EINVAL;
388 goto out_cmt_unlock;
389 }
390
391 if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
392 c->cmt_state = COMMIT_RUNNING_REQUIRED;
393
394 if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
395 up_write(&c->commit_sem);
396 spin_unlock(&c->cs_lock);
397 return wait_for_commit(c);
398 }
399 c->cmt_state = COMMIT_RUNNING_REQUIRED;
400 spin_unlock(&c->cs_lock);
401
402 err = do_commit(c);
403 return err;
404
405out_cmt_unlock:
406 up_write(&c->commit_sem);
407out:
408 spin_unlock(&c->cs_lock);
409 return err;
410}
411
412/**
413 * ubifs_gc_should_commit - determine if it is time for GC to run commit.
414 * @c: UBIFS file-system description object
415 *
416 * This function is called by garbage collection to determine if commit should
417 * be run. If commit state is @COMMIT_BACKGROUND, which means that the journal
418 * is full enough to start commit, this function returns true. It is not
419 * absolutely necessary to commit yet, but it feels like this should be better
420 * then to keep doing GC. This function returns %1 if GC has to initiate commit
421 * and %0 if not.
422 */
423int ubifs_gc_should_commit(struct ubifs_info *c)
424{
425 int ret = 0;
426
427 spin_lock(&c->cs_lock);
428 if (c->cmt_state == COMMIT_BACKGROUND) {
429 dbg_cmt("commit required now");
430 c->cmt_state = COMMIT_REQUIRED;
431 } else
432 dbg_cmt("commit not requested");
433 if (c->cmt_state == COMMIT_REQUIRED)
434 ret = 1;
435 spin_unlock(&c->cs_lock);
436 return ret;
437}
438
439#ifdef CONFIG_UBIFS_FS_DEBUG
440
441/**
442 * struct idx_node - hold index nodes during index tree traversal.
443 * @list: list
444 * @iip: index in parent (slot number of this indexing node in the parent
445 * indexing node)
446 * @upper_key: all keys in this indexing node have to be less or equivalent to
447 * this key
448 * @idx: index node (8-byte aligned because all node structures must be 8-byte
449 * aligned)
450 */
451struct idx_node {
452 struct list_head list;
453 int iip;
454 union ubifs_key upper_key;
455 struct ubifs_idx_node idx __attribute__((aligned(8)));
456};
457
458/**
459 * dbg_old_index_check_init - get information for the next old index check.
460 * @c: UBIFS file-system description object
461 * @zroot: root of the index
462 *
463 * This function records information about the index that will be needed for the
464 * next old index check i.e. 'dbg_check_old_index()'.
465 *
466 * This function returns %0 on success and a negative error code on failure.
467 */
468int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot)
469{
470 struct ubifs_idx_node *idx;
471 int lnum, offs, len, err = 0;
472
473 c->old_zroot = *zroot;
474
475 lnum = c->old_zroot.lnum;
476 offs = c->old_zroot.offs;
477 len = c->old_zroot.len;
478
479 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
480 if (!idx)
481 return -ENOMEM;
482
483 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
484 if (err)
485 goto out;
486
487 c->old_zroot_level = le16_to_cpu(idx->level);
488 c->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum);
489out:
490 kfree(idx);
491 return err;
492}
493
494/**
495 * dbg_check_old_index - check the old copy of the index.
496 * @c: UBIFS file-system description object
497 * @zroot: root of the new index
498 *
499 * In order to be able to recover from an unclean unmount, a complete copy of
500 * the index must exist on flash. This is the "old" index. The commit process
501 * must write the "new" index to flash without overwriting or destroying any
502 * part of the old index. This function is run at commit end in order to check
503 * that the old index does indeed exist completely intact.
504 *
505 * This function returns %0 on success and a negative error code on failure.
506 */
507int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot)
508{
509 int lnum, offs, len, err = 0, uninitialized_var(last_level), child_cnt;
510 int first = 1, iip;
511 union ubifs_key lower_key, upper_key, l_key, u_key;
512 unsigned long long uninitialized_var(last_sqnum);
513 struct ubifs_idx_node *idx;
514 struct list_head list;
515 struct idx_node *i;
516 size_t sz;
517
518 if (!(ubifs_chk_flags & UBIFS_CHK_OLD_IDX))
519 goto out;
520
521 INIT_LIST_HEAD(&list);
522
523 sz = sizeof(struct idx_node) + ubifs_idx_node_sz(c, c->fanout) -
524 UBIFS_IDX_NODE_SZ;
525
526 /* Start at the old zroot */
527 lnum = c->old_zroot.lnum;
528 offs = c->old_zroot.offs;
529 len = c->old_zroot.len;
530 iip = 0;
531
532 /*
533 * Traverse the index tree preorder depth-first i.e. do a node and then
534 * its subtrees from left to right.
535 */
536 while (1) {
537 struct ubifs_branch *br;
538
539 /* Get the next index node */
540 i = kmalloc(sz, GFP_NOFS);
541 if (!i) {
542 err = -ENOMEM;
543 goto out_free;
544 }
545 i->iip = iip;
546 /* Keep the index nodes on our path in a linked list */
547 list_add_tail(&i->list, &list);
548 /* Read the index node */
549 idx = &i->idx;
550 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
551 if (err)
552 goto out_free;
553 /* Validate index node */
554 child_cnt = le16_to_cpu(idx->child_cnt);
555 if (child_cnt < 1 || child_cnt > c->fanout) {
556 err = 1;
557 goto out_dump;
558 }
559 if (first) {
560 first = 0;
561 /* Check root level and sqnum */
562 if (le16_to_cpu(idx->level) != c->old_zroot_level) {
563 err = 2;
564 goto out_dump;
565 }
566 if (le64_to_cpu(idx->ch.sqnum) != c->old_zroot_sqnum) {
567 err = 3;
568 goto out_dump;
569 }
570 /* Set last values as though root had a parent */
571 last_level = le16_to_cpu(idx->level) + 1;
572 last_sqnum = le64_to_cpu(idx->ch.sqnum) + 1;
573 key_read(c, ubifs_idx_key(c, idx), &lower_key);
574 highest_ino_key(c, &upper_key, INUM_WATERMARK);
575 }
576 key_copy(c, &upper_key, &i->upper_key);
577 if (le16_to_cpu(idx->level) != last_level - 1) {
578 err = 3;
579 goto out_dump;
580 }
581 /*
582 * The index is always written bottom up hence a child's sqnum
583 * is always less than the parents.
584 */
585 if (le64_to_cpu(idx->ch.sqnum) >= last_sqnum) {
586 err = 4;
587 goto out_dump;
588 }
589 /* Check key range */
590 key_read(c, ubifs_idx_key(c, idx), &l_key);
591 br = ubifs_idx_branch(c, idx, child_cnt - 1);
592 key_read(c, &br->key, &u_key);
593 if (keys_cmp(c, &lower_key, &l_key) > 0) {
594 err = 5;
595 goto out_dump;
596 }
597 if (keys_cmp(c, &upper_key, &u_key) < 0) {
598 err = 6;
599 goto out_dump;
600 }
601 if (keys_cmp(c, &upper_key, &u_key) == 0)
602 if (!is_hash_key(c, &u_key)) {
603 err = 7;
604 goto out_dump;
605 }
606 /* Go to next index node */
607 if (le16_to_cpu(idx->level) == 0) {
608 /* At the bottom, so go up until can go right */
609 while (1) {
610 /* Drop the bottom of the list */
611 list_del(&i->list);
612 kfree(i);
613 /* No more list means we are done */
614 if (list_empty(&list))
615 goto out;
616 /* Look at the new bottom */
617 i = list_entry(list.prev, struct idx_node,
618 list);
619 idx = &i->idx;
620 /* Can we go right */
621 if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
622 iip = iip + 1;
623 break;
624 } else
625 /* Nope, so go up again */
626 iip = i->iip;
627 }
628 } else
629 /* Go down left */
630 iip = 0;
631 /*
632 * We have the parent in 'idx' and now we set up for reading the
633 * child pointed to by slot 'iip'.
634 */
635 last_level = le16_to_cpu(idx->level);
636 last_sqnum = le64_to_cpu(idx->ch.sqnum);
637 br = ubifs_idx_branch(c, idx, iip);
638 lnum = le32_to_cpu(br->lnum);
639 offs = le32_to_cpu(br->offs);
640 len = le32_to_cpu(br->len);
641 key_read(c, &br->key, &lower_key);
642 if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
643 br = ubifs_idx_branch(c, idx, iip + 1);
644 key_read(c, &br->key, &upper_key);
645 } else
646 key_copy(c, &i->upper_key, &upper_key);
647 }
648out:
649 err = dbg_old_index_check_init(c, zroot);
650 if (err)
651 goto out_free;
652
653 return 0;
654
655out_dump:
656 dbg_err("dumping index node (iip=%d)", i->iip);
657 dbg_dump_node(c, idx);
658 list_del(&i->list);
659 kfree(i);
660 if (!list_empty(&list)) {
661 i = list_entry(list.prev, struct idx_node, list);
662 dbg_err("dumping parent index node");
663 dbg_dump_node(c, &i->idx);
664 }
665out_free:
666 while (!list_empty(&list)) {
667 i = list_entry(list.next, struct idx_node, list);
668 list_del(&i->list);
669 kfree(i);
670 }
671 ubifs_err("failed, error %d", err);
672 if (err > 0)
673 err = -EINVAL;
674 return err;
675}
676
677#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/compress.c b/fs/ubifs/compress.c
new file mode 100644
index 000000000000..5bb51dac3c16
--- /dev/null
+++ b/fs/ubifs/compress.c
@@ -0,0 +1,253 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors: Adrian Hunter
21 * Artem Bityutskiy (Битюцкий Артём)
22 * Zoltan Sogor
23 */
24
25/*
26 * This file provides a single place to access to compression and
27 * decompression.
28 */
29
30#include <linux/crypto.h>
31#include "ubifs.h"
32
33/* Fake description object for the "none" compressor */
34static struct ubifs_compressor none_compr = {
35 .compr_type = UBIFS_COMPR_NONE,
36 .name = "no compression",
37 .capi_name = "",
38};
39
40#ifdef CONFIG_UBIFS_FS_LZO
41static DEFINE_MUTEX(lzo_mutex);
42
43static struct ubifs_compressor lzo_compr = {
44 .compr_type = UBIFS_COMPR_LZO,
45 .comp_mutex = &lzo_mutex,
46 .name = "LZO",
47 .capi_name = "lzo",
48};
49#else
50static struct ubifs_compressor lzo_compr = {
51 .compr_type = UBIFS_COMPR_LZO,
52 .name = "LZO",
53};
54#endif
55
56#ifdef CONFIG_UBIFS_FS_ZLIB
57static DEFINE_MUTEX(deflate_mutex);
58static DEFINE_MUTEX(inflate_mutex);
59
60static struct ubifs_compressor zlib_compr = {
61 .compr_type = UBIFS_COMPR_ZLIB,
62 .comp_mutex = &deflate_mutex,
63 .decomp_mutex = &inflate_mutex,
64 .name = "zlib",
65 .capi_name = "deflate",
66};
67#else
68static struct ubifs_compressor zlib_compr = {
69 .compr_type = UBIFS_COMPR_ZLIB,
70 .name = "zlib",
71};
72#endif
73
74/* All UBIFS compressors */
75struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
76
77/**
78 * ubifs_compress - compress data.
79 * @in_buf: data to compress
80 * @in_len: length of the data to compress
81 * @out_buf: output buffer where compressed data should be stored
82 * @out_len: output buffer length is returned here
83 * @compr_type: type of compression to use on enter, actually used compression
84 * type on exit
85 *
86 * This function compresses input buffer @in_buf of length @in_len and stores
87 * the result in the output buffer @out_buf and the resulting length in
88 * @out_len. If the input buffer does not compress, it is just copied to the
89 * @out_buf. The same happens if @compr_type is %UBIFS_COMPR_NONE or if
90 * compression error occurred.
91 *
92 * Note, if the input buffer was not compressed, it is copied to the output
93 * buffer and %UBIFS_COMPR_NONE is returned in @compr_type.
94 *
95 * This functions returns %0 on success or a negative error code on failure.
96 */
97void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
98 int *compr_type)
99{
100 int err;
101 struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
102
103 if (*compr_type == UBIFS_COMPR_NONE)
104 goto no_compr;
105
106 /* If the input data is small, do not even try to compress it */
107 if (in_len < UBIFS_MIN_COMPR_LEN)
108 goto no_compr;
109
110 if (compr->comp_mutex)
111 mutex_lock(compr->comp_mutex);
112 err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
113 out_len);
114 if (compr->comp_mutex)
115 mutex_unlock(compr->comp_mutex);
116 if (unlikely(err)) {
117 ubifs_warn("cannot compress %d bytes, compressor %s, "
118 "error %d, leave data uncompressed",
119 in_len, compr->name, err);
120 goto no_compr;
121 }
122
123 /*
124 * Presently, we just require that compression results in less data,
125 * rather than any defined minimum compression ratio or amount.
126 */
127 if (ALIGN(*out_len, 8) >= ALIGN(in_len, 8))
128 goto no_compr;
129
130 return;
131
132no_compr:
133 memcpy(out_buf, in_buf, in_len);
134 *out_len = in_len;
135 *compr_type = UBIFS_COMPR_NONE;
136}
137
138/**
139 * ubifs_decompress - decompress data.
140 * @in_buf: data to decompress
141 * @in_len: length of the data to decompress
142 * @out_buf: output buffer where decompressed data should
143 * @out_len: output length is returned here
144 * @compr_type: type of compression
145 *
146 * This function decompresses data from buffer @in_buf into buffer @out_buf.
147 * The length of the uncompressed data is returned in @out_len. This functions
148 * returns %0 on success or a negative error code on failure.
149 */
150int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
151 int *out_len, int compr_type)
152{
153 int err;
154 struct ubifs_compressor *compr;
155
156 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
157 ubifs_err("invalid compression type %d", compr_type);
158 return -EINVAL;
159 }
160
161 compr = ubifs_compressors[compr_type];
162
163 if (unlikely(!compr->capi_name)) {
164 ubifs_err("%s compression is not compiled in", compr->name);
165 return -EINVAL;
166 }
167
168 if (compr_type == UBIFS_COMPR_NONE) {
169 memcpy(out_buf, in_buf, in_len);
170 *out_len = in_len;
171 return 0;
172 }
173
174 if (compr->decomp_mutex)
175 mutex_lock(compr->decomp_mutex);
176 err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
177 out_len);
178 if (compr->decomp_mutex)
179 mutex_unlock(compr->decomp_mutex);
180 if (err)
181 ubifs_err("cannot decompress %d bytes, compressor %s, "
182 "error %d", in_len, compr->name, err);
183
184 return err;
185}
186
187/**
188 * compr_init - initialize a compressor.
189 * @compr: compressor description object
190 *
191 * This function initializes the requested compressor and returns zero in case
192 * of success or a negative error code in case of failure.
193 */
194static int __init compr_init(struct ubifs_compressor *compr)
195{
196 if (compr->capi_name) {
197 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
198 if (IS_ERR(compr->cc)) {
199 ubifs_err("cannot initialize compressor %s, error %ld",
200 compr->name, PTR_ERR(compr->cc));
201 return PTR_ERR(compr->cc);
202 }
203 }
204
205 ubifs_compressors[compr->compr_type] = compr;
206 return 0;
207}
208
209/**
210 * compr_exit - de-initialize a compressor.
211 * @compr: compressor description object
212 */
213static void compr_exit(struct ubifs_compressor *compr)
214{
215 if (compr->capi_name)
216 crypto_free_comp(compr->cc);
217 return;
218}
219
220/**
221 * ubifs_compressors_init - initialize UBIFS compressors.
222 *
223 * This function initializes the compressor which were compiled in. Returns
224 * zero in case of success and a negative error code in case of failure.
225 */
226int __init ubifs_compressors_init(void)
227{
228 int err;
229
230 err = compr_init(&lzo_compr);
231 if (err)
232 return err;
233
234 err = compr_init(&zlib_compr);
235 if (err)
236 goto out_lzo;
237
238 ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
239 return 0;
240
241out_lzo:
242 compr_exit(&lzo_compr);
243 return err;
244}
245
246/**
247 * ubifs_compressors_exit - de-initialize UBIFS compressors.
248 */
249void __exit ubifs_compressors_exit(void)
250{
251 compr_exit(&lzo_compr);
252 compr_exit(&zlib_compr);
253}
diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c
new file mode 100644
index 000000000000..4e3aaeba4eca
--- /dev/null
+++ b/fs/ubifs/debug.c
@@ -0,0 +1,2289 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements most of the debugging stuff which is compiled in only
25 * when it is enabled. But some debugging check functions are implemented in
26 * corresponding subsystem, just because they are closely related and utilize
27 * various local functions of those subsystems.
28 */
29
30#define UBIFS_DBG_PRESERVE_UBI
31
32#include "ubifs.h"
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35
36#ifdef CONFIG_UBIFS_FS_DEBUG
37
38DEFINE_SPINLOCK(dbg_lock);
39
40static char dbg_key_buf0[128];
41static char dbg_key_buf1[128];
42
43unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
44unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
45unsigned int ubifs_tst_flags;
46
47module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
48module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
49module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
50
51MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
52MODULE_PARM_DESC(debug_chks, "Debug check flags");
53MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
54
55static const char *get_key_fmt(int fmt)
56{
57 switch (fmt) {
58 case UBIFS_SIMPLE_KEY_FMT:
59 return "simple";
60 default:
61 return "unknown/invalid format";
62 }
63}
64
65static const char *get_key_hash(int hash)
66{
67 switch (hash) {
68 case UBIFS_KEY_HASH_R5:
69 return "R5";
70 case UBIFS_KEY_HASH_TEST:
71 return "test";
72 default:
73 return "unknown/invalid name hash";
74 }
75}
76
77static const char *get_key_type(int type)
78{
79 switch (type) {
80 case UBIFS_INO_KEY:
81 return "inode";
82 case UBIFS_DENT_KEY:
83 return "direntry";
84 case UBIFS_XENT_KEY:
85 return "xentry";
86 case UBIFS_DATA_KEY:
87 return "data";
88 case UBIFS_TRUN_KEY:
89 return "truncate";
90 default:
91 return "unknown/invalid key";
92 }
93}
94
95static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
96 char *buffer)
97{
98 char *p = buffer;
99 int type = key_type(c, key);
100
101 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
102 switch (type) {
103 case UBIFS_INO_KEY:
104 sprintf(p, "(%lu, %s)", key_inum(c, key),
105 get_key_type(type));
106 break;
107 case UBIFS_DENT_KEY:
108 case UBIFS_XENT_KEY:
109 sprintf(p, "(%lu, %s, %#08x)", key_inum(c, key),
110 get_key_type(type), key_hash(c, key));
111 break;
112 case UBIFS_DATA_KEY:
113 sprintf(p, "(%lu, %s, %u)", key_inum(c, key),
114 get_key_type(type), key_block(c, key));
115 break;
116 case UBIFS_TRUN_KEY:
117 sprintf(p, "(%lu, %s)",
118 key_inum(c, key), get_key_type(type));
119 break;
120 default:
121 sprintf(p, "(bad key type: %#08x, %#08x)",
122 key->u32[0], key->u32[1]);
123 }
124 } else
125 sprintf(p, "bad key format %d", c->key_fmt);
126}
127
128const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
129{
130 /* dbg_lock must be held */
131 sprintf_key(c, key, dbg_key_buf0);
132 return dbg_key_buf0;
133}
134
135const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
136{
137 /* dbg_lock must be held */
138 sprintf_key(c, key, dbg_key_buf1);
139 return dbg_key_buf1;
140}
141
142const char *dbg_ntype(int type)
143{
144 switch (type) {
145 case UBIFS_PAD_NODE:
146 return "padding node";
147 case UBIFS_SB_NODE:
148 return "superblock node";
149 case UBIFS_MST_NODE:
150 return "master node";
151 case UBIFS_REF_NODE:
152 return "reference node";
153 case UBIFS_INO_NODE:
154 return "inode node";
155 case UBIFS_DENT_NODE:
156 return "direntry node";
157 case UBIFS_XENT_NODE:
158 return "xentry node";
159 case UBIFS_DATA_NODE:
160 return "data node";
161 case UBIFS_TRUN_NODE:
162 return "truncate node";
163 case UBIFS_IDX_NODE:
164 return "indexing node";
165 case UBIFS_CS_NODE:
166 return "commit start node";
167 case UBIFS_ORPH_NODE:
168 return "orphan node";
169 default:
170 return "unknown node";
171 }
172}
173
174static const char *dbg_gtype(int type)
175{
176 switch (type) {
177 case UBIFS_NO_NODE_GROUP:
178 return "no node group";
179 case UBIFS_IN_NODE_GROUP:
180 return "in node group";
181 case UBIFS_LAST_OF_NODE_GROUP:
182 return "last of node group";
183 default:
184 return "unknown";
185 }
186}
187
188const char *dbg_cstate(int cmt_state)
189{
190 switch (cmt_state) {
191 case COMMIT_RESTING:
192 return "commit resting";
193 case COMMIT_BACKGROUND:
194 return "background commit requested";
195 case COMMIT_REQUIRED:
196 return "commit required";
197 case COMMIT_RUNNING_BACKGROUND:
198 return "BACKGROUND commit running";
199 case COMMIT_RUNNING_REQUIRED:
200 return "commit running and required";
201 case COMMIT_BROKEN:
202 return "broken commit";
203 default:
204 return "unknown commit state";
205 }
206}
207
208static void dump_ch(const struct ubifs_ch *ch)
209{
210 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
211 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
212 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
213 dbg_ntype(ch->node_type));
214 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
215 dbg_gtype(ch->group_type));
216 printk(KERN_DEBUG "\tsqnum %llu\n",
217 (unsigned long long)le64_to_cpu(ch->sqnum));
218 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
219}
220
221void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
222{
223 const struct ubifs_inode *ui = ubifs_inode(inode);
224
225 printk(KERN_DEBUG "inode %lu\n", inode->i_ino);
226 printk(KERN_DEBUG "size %llu\n",
227 (unsigned long long)i_size_read(inode));
228 printk(KERN_DEBUG "nlink %u\n", inode->i_nlink);
229 printk(KERN_DEBUG "uid %u\n", (unsigned int)inode->i_uid);
230 printk(KERN_DEBUG "gid %u\n", (unsigned int)inode->i_gid);
231 printk(KERN_DEBUG "atime %u.%u\n",
232 (unsigned int)inode->i_atime.tv_sec,
233 (unsigned int)inode->i_atime.tv_nsec);
234 printk(KERN_DEBUG "mtime %u.%u\n",
235 (unsigned int)inode->i_mtime.tv_sec,
236 (unsigned int)inode->i_mtime.tv_nsec);
237 printk(KERN_DEBUG "ctime %u.%u\n",
238 (unsigned int)inode->i_ctime.tv_sec,
239 (unsigned int)inode->i_ctime.tv_nsec);
240 printk(KERN_DEBUG "creat_sqnum %llu\n", ui->creat_sqnum);
241 printk(KERN_DEBUG "xattr_size %u\n", ui->xattr_size);
242 printk(KERN_DEBUG "xattr_cnt %u\n", ui->xattr_cnt);
243 printk(KERN_DEBUG "xattr_names %u\n", ui->xattr_names);
244 printk(KERN_DEBUG "dirty %u\n", ui->dirty);
245 printk(KERN_DEBUG "xattr %u\n", ui->xattr);
246 printk(KERN_DEBUG "flags %d\n", ui->flags);
247 printk(KERN_DEBUG "compr_type %d\n", ui->compr_type);
248 printk(KERN_DEBUG "data_len %d\n", ui->data_len);
249}
250
251void dbg_dump_node(const struct ubifs_info *c, const void *node)
252{
253 int i, n;
254 union ubifs_key key;
255 const struct ubifs_ch *ch = node;
256
257 if (dbg_failure_mode)
258 return;
259
260 /* If the magic is incorrect, just hexdump the first bytes */
261 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
262 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
263 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
264 (void *)node, UBIFS_CH_SZ, 1);
265 return;
266 }
267
268 spin_lock(&dbg_lock);
269 dump_ch(node);
270
271 switch (ch->node_type) {
272 case UBIFS_PAD_NODE:
273 {
274 const struct ubifs_pad_node *pad = node;
275
276 printk(KERN_DEBUG "\tpad_len %u\n",
277 le32_to_cpu(pad->pad_len));
278 break;
279 }
280 case UBIFS_SB_NODE:
281 {
282 const struct ubifs_sb_node *sup = node;
283 unsigned int sup_flags = le32_to_cpu(sup->flags);
284
285 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
286 (int)sup->key_hash, get_key_hash(sup->key_hash));
287 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
288 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
289 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
290 printk(KERN_DEBUG "\t big_lpt %u\n",
291 !!(sup_flags & UBIFS_FLG_BIGLPT));
292 printk(KERN_DEBUG "\tmin_io_size %u\n",
293 le32_to_cpu(sup->min_io_size));
294 printk(KERN_DEBUG "\tleb_size %u\n",
295 le32_to_cpu(sup->leb_size));
296 printk(KERN_DEBUG "\tleb_cnt %u\n",
297 le32_to_cpu(sup->leb_cnt));
298 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
299 le32_to_cpu(sup->max_leb_cnt));
300 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
301 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
302 printk(KERN_DEBUG "\tlog_lebs %u\n",
303 le32_to_cpu(sup->log_lebs));
304 printk(KERN_DEBUG "\tlpt_lebs %u\n",
305 le32_to_cpu(sup->lpt_lebs));
306 printk(KERN_DEBUG "\torph_lebs %u\n",
307 le32_to_cpu(sup->orph_lebs));
308 printk(KERN_DEBUG "\tjhead_cnt %u\n",
309 le32_to_cpu(sup->jhead_cnt));
310 printk(KERN_DEBUG "\tfanout %u\n",
311 le32_to_cpu(sup->fanout));
312 printk(KERN_DEBUG "\tlsave_cnt %u\n",
313 le32_to_cpu(sup->lsave_cnt));
314 printk(KERN_DEBUG "\tdefault_compr %u\n",
315 (int)le16_to_cpu(sup->default_compr));
316 printk(KERN_DEBUG "\trp_size %llu\n",
317 (unsigned long long)le64_to_cpu(sup->rp_size));
318 printk(KERN_DEBUG "\trp_uid %u\n",
319 le32_to_cpu(sup->rp_uid));
320 printk(KERN_DEBUG "\trp_gid %u\n",
321 le32_to_cpu(sup->rp_gid));
322 printk(KERN_DEBUG "\tfmt_version %u\n",
323 le32_to_cpu(sup->fmt_version));
324 printk(KERN_DEBUG "\ttime_gran %u\n",
325 le32_to_cpu(sup->time_gran));
326 printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X"
327 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
328 sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
329 sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
330 sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
331 sup->uuid[12], sup->uuid[13], sup->uuid[14],
332 sup->uuid[15]);
333 break;
334 }
335 case UBIFS_MST_NODE:
336 {
337 const struct ubifs_mst_node *mst = node;
338
339 printk(KERN_DEBUG "\thighest_inum %llu\n",
340 (unsigned long long)le64_to_cpu(mst->highest_inum));
341 printk(KERN_DEBUG "\tcommit number %llu\n",
342 (unsigned long long)le64_to_cpu(mst->cmt_no));
343 printk(KERN_DEBUG "\tflags %#x\n",
344 le32_to_cpu(mst->flags));
345 printk(KERN_DEBUG "\tlog_lnum %u\n",
346 le32_to_cpu(mst->log_lnum));
347 printk(KERN_DEBUG "\troot_lnum %u\n",
348 le32_to_cpu(mst->root_lnum));
349 printk(KERN_DEBUG "\troot_offs %u\n",
350 le32_to_cpu(mst->root_offs));
351 printk(KERN_DEBUG "\troot_len %u\n",
352 le32_to_cpu(mst->root_len));
353 printk(KERN_DEBUG "\tgc_lnum %u\n",
354 le32_to_cpu(mst->gc_lnum));
355 printk(KERN_DEBUG "\tihead_lnum %u\n",
356 le32_to_cpu(mst->ihead_lnum));
357 printk(KERN_DEBUG "\tihead_offs %u\n",
358 le32_to_cpu(mst->ihead_offs));
359 printk(KERN_DEBUG "\tindex_size %u\n",
360 le32_to_cpu(mst->index_size));
361 printk(KERN_DEBUG "\tlpt_lnum %u\n",
362 le32_to_cpu(mst->lpt_lnum));
363 printk(KERN_DEBUG "\tlpt_offs %u\n",
364 le32_to_cpu(mst->lpt_offs));
365 printk(KERN_DEBUG "\tnhead_lnum %u\n",
366 le32_to_cpu(mst->nhead_lnum));
367 printk(KERN_DEBUG "\tnhead_offs %u\n",
368 le32_to_cpu(mst->nhead_offs));
369 printk(KERN_DEBUG "\tltab_lnum %u\n",
370 le32_to_cpu(mst->ltab_lnum));
371 printk(KERN_DEBUG "\tltab_offs %u\n",
372 le32_to_cpu(mst->ltab_offs));
373 printk(KERN_DEBUG "\tlsave_lnum %u\n",
374 le32_to_cpu(mst->lsave_lnum));
375 printk(KERN_DEBUG "\tlsave_offs %u\n",
376 le32_to_cpu(mst->lsave_offs));
377 printk(KERN_DEBUG "\tlscan_lnum %u\n",
378 le32_to_cpu(mst->lscan_lnum));
379 printk(KERN_DEBUG "\tleb_cnt %u\n",
380 le32_to_cpu(mst->leb_cnt));
381 printk(KERN_DEBUG "\tempty_lebs %u\n",
382 le32_to_cpu(mst->empty_lebs));
383 printk(KERN_DEBUG "\tidx_lebs %u\n",
384 le32_to_cpu(mst->idx_lebs));
385 printk(KERN_DEBUG "\ttotal_free %llu\n",
386 (unsigned long long)le64_to_cpu(mst->total_free));
387 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
388 (unsigned long long)le64_to_cpu(mst->total_dirty));
389 printk(KERN_DEBUG "\ttotal_used %llu\n",
390 (unsigned long long)le64_to_cpu(mst->total_used));
391 printk(KERN_DEBUG "\ttotal_dead %llu\n",
392 (unsigned long long)le64_to_cpu(mst->total_dead));
393 printk(KERN_DEBUG "\ttotal_dark %llu\n",
394 (unsigned long long)le64_to_cpu(mst->total_dark));
395 break;
396 }
397 case UBIFS_REF_NODE:
398 {
399 const struct ubifs_ref_node *ref = node;
400
401 printk(KERN_DEBUG "\tlnum %u\n",
402 le32_to_cpu(ref->lnum));
403 printk(KERN_DEBUG "\toffs %u\n",
404 le32_to_cpu(ref->offs));
405 printk(KERN_DEBUG "\tjhead %u\n",
406 le32_to_cpu(ref->jhead));
407 break;
408 }
409 case UBIFS_INO_NODE:
410 {
411 const struct ubifs_ino_node *ino = node;
412
413 key_read(c, &ino->key, &key);
414 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
415 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
416 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
417 printk(KERN_DEBUG "\tsize %llu\n",
418 (unsigned long long)le64_to_cpu(ino->size));
419 printk(KERN_DEBUG "\tnlink %u\n",
420 le32_to_cpu(ino->nlink));
421 printk(KERN_DEBUG "\tatime %lld.%u\n",
422 (long long)le64_to_cpu(ino->atime_sec),
423 le32_to_cpu(ino->atime_nsec));
424 printk(KERN_DEBUG "\tmtime %lld.%u\n",
425 (long long)le64_to_cpu(ino->mtime_sec),
426 le32_to_cpu(ino->mtime_nsec));
427 printk(KERN_DEBUG "\tctime %lld.%u\n",
428 (long long)le64_to_cpu(ino->ctime_sec),
429 le32_to_cpu(ino->ctime_nsec));
430 printk(KERN_DEBUG "\tuid %u\n",
431 le32_to_cpu(ino->uid));
432 printk(KERN_DEBUG "\tgid %u\n",
433 le32_to_cpu(ino->gid));
434 printk(KERN_DEBUG "\tmode %u\n",
435 le32_to_cpu(ino->mode));
436 printk(KERN_DEBUG "\tflags %#x\n",
437 le32_to_cpu(ino->flags));
438 printk(KERN_DEBUG "\txattr_cnt %u\n",
439 le32_to_cpu(ino->xattr_cnt));
440 printk(KERN_DEBUG "\txattr_size %u\n",
441 le32_to_cpu(ino->xattr_size));
442 printk(KERN_DEBUG "\txattr_names %u\n",
443 le32_to_cpu(ino->xattr_names));
444 printk(KERN_DEBUG "\tcompr_type %#x\n",
445 (int)le16_to_cpu(ino->compr_type));
446 printk(KERN_DEBUG "\tdata len %u\n",
447 le32_to_cpu(ino->data_len));
448 break;
449 }
450 case UBIFS_DENT_NODE:
451 case UBIFS_XENT_NODE:
452 {
453 const struct ubifs_dent_node *dent = node;
454 int nlen = le16_to_cpu(dent->nlen);
455
456 key_read(c, &dent->key, &key);
457 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
458 printk(KERN_DEBUG "\tinum %llu\n",
459 (unsigned long long)le64_to_cpu(dent->inum));
460 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
461 printk(KERN_DEBUG "\tnlen %d\n", nlen);
462 printk(KERN_DEBUG "\tname ");
463
464 if (nlen > UBIFS_MAX_NLEN)
465 printk(KERN_DEBUG "(bad name length, not printing, "
466 "bad or corrupted node)");
467 else {
468 for (i = 0; i < nlen && dent->name[i]; i++)
469 printk("%c", dent->name[i]);
470 }
471 printk("\n");
472
473 break;
474 }
475 case UBIFS_DATA_NODE:
476 {
477 const struct ubifs_data_node *dn = node;
478 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
479
480 key_read(c, &dn->key, &key);
481 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
482 printk(KERN_DEBUG "\tsize %u\n",
483 le32_to_cpu(dn->size));
484 printk(KERN_DEBUG "\tcompr_typ %d\n",
485 (int)le16_to_cpu(dn->compr_type));
486 printk(KERN_DEBUG "\tdata size %d\n",
487 dlen);
488 printk(KERN_DEBUG "\tdata:\n");
489 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
490 (void *)&dn->data, dlen, 0);
491 break;
492 }
493 case UBIFS_TRUN_NODE:
494 {
495 const struct ubifs_trun_node *trun = node;
496
497 printk(KERN_DEBUG "\tinum %u\n",
498 le32_to_cpu(trun->inum));
499 printk(KERN_DEBUG "\told_size %llu\n",
500 (unsigned long long)le64_to_cpu(trun->old_size));
501 printk(KERN_DEBUG "\tnew_size %llu\n",
502 (unsigned long long)le64_to_cpu(trun->new_size));
503 break;
504 }
505 case UBIFS_IDX_NODE:
506 {
507 const struct ubifs_idx_node *idx = node;
508
509 n = le16_to_cpu(idx->child_cnt);
510 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
511 printk(KERN_DEBUG "\tlevel %d\n",
512 (int)le16_to_cpu(idx->level));
513 printk(KERN_DEBUG "\tBranches:\n");
514
515 for (i = 0; i < n && i < c->fanout - 1; i++) {
516 const struct ubifs_branch *br;
517
518 br = ubifs_idx_branch(c, idx, i);
519 key_read(c, &br->key, &key);
520 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
521 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
522 le32_to_cpu(br->len), DBGKEY(&key));
523 }
524 break;
525 }
526 case UBIFS_CS_NODE:
527 break;
528 case UBIFS_ORPH_NODE:
529 {
530 const struct ubifs_orph_node *orph = node;
531
532 printk(KERN_DEBUG "\tcommit number %llu\n",
533 (unsigned long long)
534 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
535 printk(KERN_DEBUG "\tlast node flag %llu\n",
536 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
537 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
538 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
539 for (i = 0; i < n; i++)
540 printk(KERN_DEBUG "\t ino %llu\n",
541 le64_to_cpu(orph->inos[i]));
542 break;
543 }
544 default:
545 printk(KERN_DEBUG "node type %d was not recognized\n",
546 (int)ch->node_type);
547 }
548 spin_unlock(&dbg_lock);
549}
550
551void dbg_dump_budget_req(const struct ubifs_budget_req *req)
552{
553 spin_lock(&dbg_lock);
554 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
555 req->new_ino, req->dirtied_ino);
556 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
557 req->new_ino_d, req->dirtied_ino_d);
558 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
559 req->new_page, req->dirtied_page);
560 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
561 req->new_dent, req->mod_dent);
562 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
563 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
564 req->data_growth, req->dd_growth);
565 spin_unlock(&dbg_lock);
566}
567
568void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
569{
570 spin_lock(&dbg_lock);
571 printk(KERN_DEBUG "Lprops statistics: empty_lebs %d, idx_lebs %d\n",
572 lst->empty_lebs, lst->idx_lebs);
573 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
574 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
575 lst->total_dirty);
576 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
577 "total_dead %lld\n", lst->total_used, lst->total_dark,
578 lst->total_dead);
579 spin_unlock(&dbg_lock);
580}
581
582void dbg_dump_budg(struct ubifs_info *c)
583{
584 int i;
585 struct rb_node *rb;
586 struct ubifs_bud *bud;
587 struct ubifs_gced_idx_leb *idx_gc;
588
589 spin_lock(&dbg_lock);
590 printk(KERN_DEBUG "Budgeting info: budg_data_growth %lld, "
591 "budg_dd_growth %lld, budg_idx_growth %lld\n",
592 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
593 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
594 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
595 c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
596 c->freeable_cnt);
597 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
598 "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
599 c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
600 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
601 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
602 atomic_long_read(&c->dirty_zn_cnt),
603 atomic_long_read(&c->clean_zn_cnt));
604 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
605 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
606 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
607 c->gc_lnum, c->ihead_lnum);
608 for (i = 0; i < c->jhead_cnt; i++)
609 printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
610 c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
611 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
612 bud = rb_entry(rb, struct ubifs_bud, rb);
613 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
614 }
615 list_for_each_entry(bud, &c->old_buds, list)
616 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
617 list_for_each_entry(idx_gc, &c->idx_gc, list)
618 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
619 idx_gc->lnum, idx_gc->unmap);
620 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
621 spin_unlock(&dbg_lock);
622}
623
624void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
625{
626 printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
627 "flags %#x\n", lp->lnum, lp->free, lp->dirty,
628 c->leb_size - lp->free - lp->dirty, lp->flags);
629}
630
631void dbg_dump_lprops(struct ubifs_info *c)
632{
633 int lnum, err;
634 struct ubifs_lprops lp;
635 struct ubifs_lp_stats lst;
636
637 printk(KERN_DEBUG "Dumping LEB properties\n");
638 ubifs_get_lp_stats(c, &lst);
639 dbg_dump_lstats(&lst);
640
641 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
642 err = ubifs_read_one_lp(c, lnum, &lp);
643 if (err)
644 ubifs_err("cannot read lprops for LEB %d", lnum);
645
646 dbg_dump_lprop(c, &lp);
647 }
648}
649
650void dbg_dump_leb(const struct ubifs_info *c, int lnum)
651{
652 struct ubifs_scan_leb *sleb;
653 struct ubifs_scan_node *snod;
654
655 if (dbg_failure_mode)
656 return;
657
658 printk(KERN_DEBUG "Dumping LEB %d\n", lnum);
659
660 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
661 if (IS_ERR(sleb)) {
662 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
663 return;
664 }
665
666 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
667 sleb->nodes_cnt, sleb->endpt);
668
669 list_for_each_entry(snod, &sleb->nodes, list) {
670 cond_resched();
671 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
672 snod->offs, snod->len);
673 dbg_dump_node(c, snod->node);
674 }
675
676 ubifs_scan_destroy(sleb);
677 return;
678}
679
680void dbg_dump_znode(const struct ubifs_info *c,
681 const struct ubifs_znode *znode)
682{
683 int n;
684 const struct ubifs_zbranch *zbr;
685
686 spin_lock(&dbg_lock);
687 if (znode->parent)
688 zbr = &znode->parent->zbranch[znode->iip];
689 else
690 zbr = &c->zroot;
691
692 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
693 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
694 zbr->len, znode->parent, znode->iip, znode->level,
695 znode->child_cnt, znode->flags);
696
697 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
698 spin_unlock(&dbg_lock);
699 return;
700 }
701
702 printk(KERN_DEBUG "zbranches:\n");
703 for (n = 0; n < znode->child_cnt; n++) {
704 zbr = &znode->zbranch[n];
705 if (znode->level > 0)
706 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
707 "%s\n", n, zbr->znode, zbr->lnum,
708 zbr->offs, zbr->len,
709 DBGKEY(&zbr->key));
710 else
711 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
712 "%s\n", n, zbr->znode, zbr->lnum,
713 zbr->offs, zbr->len,
714 DBGKEY(&zbr->key));
715 }
716 spin_unlock(&dbg_lock);
717}
718
719void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
720{
721 int i;
722
723 printk(KERN_DEBUG "Dumping heap cat %d (%d elements)\n",
724 cat, heap->cnt);
725 for (i = 0; i < heap->cnt; i++) {
726 struct ubifs_lprops *lprops = heap->arr[i];
727
728 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
729 "flags %d\n", i, lprops->lnum, lprops->hpos,
730 lprops->free, lprops->dirty, lprops->flags);
731 }
732}
733
734void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
735 struct ubifs_nnode *parent, int iip)
736{
737 int i;
738
739 printk(KERN_DEBUG "Dumping pnode:\n");
740 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
741 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
742 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
743 pnode->flags, iip, pnode->level, pnode->num);
744 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
745 struct ubifs_lprops *lp = &pnode->lprops[i];
746
747 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
748 i, lp->free, lp->dirty, lp->flags, lp->lnum);
749 }
750}
751
752void dbg_dump_tnc(struct ubifs_info *c)
753{
754 struct ubifs_znode *znode;
755 int level;
756
757 printk(KERN_DEBUG "\n");
758 printk(KERN_DEBUG "Dumping the TNC tree\n");
759 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
760 level = znode->level;
761 printk(KERN_DEBUG "== Level %d ==\n", level);
762 while (znode) {
763 if (level != znode->level) {
764 level = znode->level;
765 printk(KERN_DEBUG "== Level %d ==\n", level);
766 }
767 dbg_dump_znode(c, znode);
768 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
769 }
770
771 printk(KERN_DEBUG "\n");
772}
773
774static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
775 void *priv)
776{
777 dbg_dump_znode(c, znode);
778 return 0;
779}
780
781/**
782 * dbg_dump_index - dump the on-flash index.
783 * @c: UBIFS file-system description object
784 *
785 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
786 * which dumps only in-memory znodes and does not read znodes which from flash.
787 */
788void dbg_dump_index(struct ubifs_info *c)
789{
790 dbg_walk_index(c, NULL, dump_znode, NULL);
791}
792
793/**
794 * dbg_check_synced_i_size - check synchronized inode size.
795 * @inode: inode to check
796 *
797 * If inode is clean, synchronized inode size has to be equivalent to current
798 * inode size. This function has to be called only for locked inodes (@i_mutex
799 * has to be locked). Returns %0 if synchronized inode size if correct, and
800 * %-EINVAL if not.
801 */
802int dbg_check_synced_i_size(struct inode *inode)
803{
804 int err = 0;
805 struct ubifs_inode *ui = ubifs_inode(inode);
806
807 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
808 return 0;
809 if (!S_ISREG(inode->i_mode))
810 return 0;
811
812 mutex_lock(&ui->ui_mutex);
813 spin_lock(&ui->ui_lock);
814 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
815 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
816 "is clean", ui->ui_size, ui->synced_i_size);
817 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
818 inode->i_mode, i_size_read(inode));
819 dbg_dump_stack();
820 err = -EINVAL;
821 }
822 spin_unlock(&ui->ui_lock);
823 mutex_unlock(&ui->ui_mutex);
824 return err;
825}
826
827/*
828 * dbg_check_dir - check directory inode size and link count.
829 * @c: UBIFS file-system description object
830 * @dir: the directory to calculate size for
831 * @size: the result is returned here
832 *
833 * This function makes sure that directory size and link count are correct.
834 * Returns zero in case of success and a negative error code in case of
835 * failure.
836 *
837 * Note, it is good idea to make sure the @dir->i_mutex is locked before
838 * calling this function.
839 */
840int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
841{
842 unsigned int nlink = 2;
843 union ubifs_key key;
844 struct ubifs_dent_node *dent, *pdent = NULL;
845 struct qstr nm = { .name = NULL };
846 loff_t size = UBIFS_INO_NODE_SZ;
847
848 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
849 return 0;
850
851 if (!S_ISDIR(dir->i_mode))
852 return 0;
853
854 lowest_dent_key(c, &key, dir->i_ino);
855 while (1) {
856 int err;
857
858 dent = ubifs_tnc_next_ent(c, &key, &nm);
859 if (IS_ERR(dent)) {
860 err = PTR_ERR(dent);
861 if (err == -ENOENT)
862 break;
863 return err;
864 }
865
866 nm.name = dent->name;
867 nm.len = le16_to_cpu(dent->nlen);
868 size += CALC_DENT_SIZE(nm.len);
869 if (dent->type == UBIFS_ITYPE_DIR)
870 nlink += 1;
871 kfree(pdent);
872 pdent = dent;
873 key_read(c, &dent->key, &key);
874 }
875 kfree(pdent);
876
877 if (i_size_read(dir) != size) {
878 ubifs_err("directory inode %lu has size %llu, "
879 "but calculated size is %llu", dir->i_ino,
880 (unsigned long long)i_size_read(dir),
881 (unsigned long long)size);
882 dump_stack();
883 return -EINVAL;
884 }
885 if (dir->i_nlink != nlink) {
886 ubifs_err("directory inode %lu has nlink %u, but calculated "
887 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
888 dump_stack();
889 return -EINVAL;
890 }
891
892 return 0;
893}
894
895/**
896 * dbg_check_key_order - make sure that colliding keys are properly ordered.
897 * @c: UBIFS file-system description object
898 * @zbr1: first zbranch
899 * @zbr2: following zbranch
900 *
901 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
902 * names of the direntries/xentries which are referred by the keys. This
903 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
904 * sure the name of direntry/xentry referred by @zbr1 is less than
905 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
906 * and a negative error code in case of failure.
907 */
908static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
909 struct ubifs_zbranch *zbr2)
910{
911 int err, nlen1, nlen2, cmp;
912 struct ubifs_dent_node *dent1, *dent2;
913 union ubifs_key key;
914
915 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
916 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
917 if (!dent1)
918 return -ENOMEM;
919 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
920 if (!dent2) {
921 err = -ENOMEM;
922 goto out_free;
923 }
924
925 err = ubifs_tnc_read_node(c, zbr1, dent1);
926 if (err)
927 goto out_free;
928 err = ubifs_validate_entry(c, dent1);
929 if (err)
930 goto out_free;
931
932 err = ubifs_tnc_read_node(c, zbr2, dent2);
933 if (err)
934 goto out_free;
935 err = ubifs_validate_entry(c, dent2);
936 if (err)
937 goto out_free;
938
939 /* Make sure node keys are the same as in zbranch */
940 err = 1;
941 key_read(c, &dent1->key, &key);
942 if (keys_cmp(c, &zbr1->key, &key)) {
943 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
944 zbr1->offs, DBGKEY(&key));
945 dbg_err("but it should have key %s according to tnc",
946 DBGKEY(&zbr1->key));
947 dbg_dump_node(c, dent1);
948 goto out_free;
949 }
950
951 key_read(c, &dent2->key, &key);
952 if (keys_cmp(c, &zbr2->key, &key)) {
953 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
954 zbr1->offs, DBGKEY(&key));
955 dbg_err("but it should have key %s according to tnc",
956 DBGKEY(&zbr2->key));
957 dbg_dump_node(c, dent2);
958 goto out_free;
959 }
960
961 nlen1 = le16_to_cpu(dent1->nlen);
962 nlen2 = le16_to_cpu(dent2->nlen);
963
964 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
965 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
966 err = 0;
967 goto out_free;
968 }
969 if (cmp == 0 && nlen1 == nlen2)
970 dbg_err("2 xent/dent nodes with the same name");
971 else
972 dbg_err("bad order of colliding key %s",
973 DBGKEY(&key));
974
975 dbg_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
976 dbg_dump_node(c, dent1);
977 dbg_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
978 dbg_dump_node(c, dent2);
979
980out_free:
981 kfree(dent2);
982 kfree(dent1);
983 return err;
984}
985
986/**
987 * dbg_check_znode - check if znode is all right.
988 * @c: UBIFS file-system description object
989 * @zbr: zbranch which points to this znode
990 *
991 * This function makes sure that znode referred to by @zbr is all right.
992 * Returns zero if it is, and %-EINVAL if it is not.
993 */
994static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
995{
996 struct ubifs_znode *znode = zbr->znode;
997 struct ubifs_znode *zp = znode->parent;
998 int n, err, cmp;
999
1000 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1001 err = 1;
1002 goto out;
1003 }
1004 if (znode->level < 0) {
1005 err = 2;
1006 goto out;
1007 }
1008 if (znode->iip < 0 || znode->iip >= c->fanout) {
1009 err = 3;
1010 goto out;
1011 }
1012
1013 if (zbr->len == 0)
1014 /* Only dirty zbranch may have no on-flash nodes */
1015 if (!ubifs_zn_dirty(znode)) {
1016 err = 4;
1017 goto out;
1018 }
1019
1020 if (ubifs_zn_dirty(znode)) {
1021 /*
1022 * If znode is dirty, its parent has to be dirty as well. The
1023 * order of the operation is important, so we have to have
1024 * memory barriers.
1025 */
1026 smp_mb();
1027 if (zp && !ubifs_zn_dirty(zp)) {
1028 /*
1029 * The dirty flag is atomic and is cleared outside the
1030 * TNC mutex, so znode's dirty flag may now have
1031 * been cleared. The child is always cleared before the
1032 * parent, so we just need to check again.
1033 */
1034 smp_mb();
1035 if (ubifs_zn_dirty(znode)) {
1036 err = 5;
1037 goto out;
1038 }
1039 }
1040 }
1041
1042 if (zp) {
1043 const union ubifs_key *min, *max;
1044
1045 if (znode->level != zp->level - 1) {
1046 err = 6;
1047 goto out;
1048 }
1049
1050 /* Make sure the 'parent' pointer in our znode is correct */
1051 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1052 if (!err) {
1053 /* This zbranch does not exist in the parent */
1054 err = 7;
1055 goto out;
1056 }
1057
1058 if (znode->iip >= zp->child_cnt) {
1059 err = 8;
1060 goto out;
1061 }
1062
1063 if (znode->iip != n) {
1064 /* This may happen only in case of collisions */
1065 if (keys_cmp(c, &zp->zbranch[n].key,
1066 &zp->zbranch[znode->iip].key)) {
1067 err = 9;
1068 goto out;
1069 }
1070 n = znode->iip;
1071 }
1072
1073 /*
1074 * Make sure that the first key in our znode is greater than or
1075 * equal to the key in the pointing zbranch.
1076 */
1077 min = &zbr->key;
1078 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1079 if (cmp == 1) {
1080 err = 10;
1081 goto out;
1082 }
1083
1084 if (n + 1 < zp->child_cnt) {
1085 max = &zp->zbranch[n + 1].key;
1086
1087 /*
1088 * Make sure the last key in our znode is less or
1089 * equivalent than the the key in zbranch which goes
1090 * after our pointing zbranch.
1091 */
1092 cmp = keys_cmp(c, max,
1093 &znode->zbranch[znode->child_cnt - 1].key);
1094 if (cmp == -1) {
1095 err = 11;
1096 goto out;
1097 }
1098 }
1099 } else {
1100 /* This may only be root znode */
1101 if (zbr != &c->zroot) {
1102 err = 12;
1103 goto out;
1104 }
1105 }
1106
1107 /*
1108 * Make sure that next key is greater or equivalent then the previous
1109 * one.
1110 */
1111 for (n = 1; n < znode->child_cnt; n++) {
1112 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1113 &znode->zbranch[n].key);
1114 if (cmp > 0) {
1115 err = 13;
1116 goto out;
1117 }
1118 if (cmp == 0) {
1119 /* This can only be keys with colliding hash */
1120 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1121 err = 14;
1122 goto out;
1123 }
1124
1125 if (znode->level != 0 || c->replaying)
1126 continue;
1127
1128 /*
1129 * Colliding keys should follow binary order of
1130 * corresponding xentry/dentry names.
1131 */
1132 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1133 &znode->zbranch[n]);
1134 if (err < 0)
1135 return err;
1136 if (err) {
1137 err = 15;
1138 goto out;
1139 }
1140 }
1141 }
1142
1143 for (n = 0; n < znode->child_cnt; n++) {
1144 if (!znode->zbranch[n].znode &&
1145 (znode->zbranch[n].lnum == 0 ||
1146 znode->zbranch[n].len == 0)) {
1147 err = 16;
1148 goto out;
1149 }
1150
1151 if (znode->zbranch[n].lnum != 0 &&
1152 znode->zbranch[n].len == 0) {
1153 err = 17;
1154 goto out;
1155 }
1156
1157 if (znode->zbranch[n].lnum == 0 &&
1158 znode->zbranch[n].len != 0) {
1159 err = 18;
1160 goto out;
1161 }
1162
1163 if (znode->zbranch[n].lnum == 0 &&
1164 znode->zbranch[n].offs != 0) {
1165 err = 19;
1166 goto out;
1167 }
1168
1169 if (znode->level != 0 && znode->zbranch[n].znode)
1170 if (znode->zbranch[n].znode->parent != znode) {
1171 err = 20;
1172 goto out;
1173 }
1174 }
1175
1176 return 0;
1177
1178out:
1179 ubifs_err("failed, error %d", err);
1180 ubifs_msg("dump of the znode");
1181 dbg_dump_znode(c, znode);
1182 if (zp) {
1183 ubifs_msg("dump of the parent znode");
1184 dbg_dump_znode(c, zp);
1185 }
1186 dump_stack();
1187 return -EINVAL;
1188}
1189
1190/**
1191 * dbg_check_tnc - check TNC tree.
1192 * @c: UBIFS file-system description object
1193 * @extra: do extra checks that are possible at start commit
1194 *
1195 * This function traverses whole TNC tree and checks every znode. Returns zero
1196 * if everything is all right and %-EINVAL if something is wrong with TNC.
1197 */
1198int dbg_check_tnc(struct ubifs_info *c, int extra)
1199{
1200 struct ubifs_znode *znode;
1201 long clean_cnt = 0, dirty_cnt = 0;
1202 int err, last;
1203
1204 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1205 return 0;
1206
1207 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1208 if (!c->zroot.znode)
1209 return 0;
1210
1211 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1212 while (1) {
1213 struct ubifs_znode *prev;
1214 struct ubifs_zbranch *zbr;
1215
1216 if (!znode->parent)
1217 zbr = &c->zroot;
1218 else
1219 zbr = &znode->parent->zbranch[znode->iip];
1220
1221 err = dbg_check_znode(c, zbr);
1222 if (err)
1223 return err;
1224
1225 if (extra) {
1226 if (ubifs_zn_dirty(znode))
1227 dirty_cnt += 1;
1228 else
1229 clean_cnt += 1;
1230 }
1231
1232 prev = znode;
1233 znode = ubifs_tnc_postorder_next(znode);
1234 if (!znode)
1235 break;
1236
1237 /*
1238 * If the last key of this znode is equivalent to the first key
1239 * of the next znode (collision), then check order of the keys.
1240 */
1241 last = prev->child_cnt - 1;
1242 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1243 !keys_cmp(c, &prev->zbranch[last].key,
1244 &znode->zbranch[0].key)) {
1245 err = dbg_check_key_order(c, &prev->zbranch[last],
1246 &znode->zbranch[0]);
1247 if (err < 0)
1248 return err;
1249 if (err) {
1250 ubifs_msg("first znode");
1251 dbg_dump_znode(c, prev);
1252 ubifs_msg("second znode");
1253 dbg_dump_znode(c, znode);
1254 return -EINVAL;
1255 }
1256 }
1257 }
1258
1259 if (extra) {
1260 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1261 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1262 atomic_long_read(&c->clean_zn_cnt),
1263 clean_cnt);
1264 return -EINVAL;
1265 }
1266 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1267 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1268 atomic_long_read(&c->dirty_zn_cnt),
1269 dirty_cnt);
1270 return -EINVAL;
1271 }
1272 }
1273
1274 return 0;
1275}
1276
1277/**
1278 * dbg_walk_index - walk the on-flash index.
1279 * @c: UBIFS file-system description object
1280 * @leaf_cb: called for each leaf node
1281 * @znode_cb: called for each indexing node
1282 * @priv: private date which is passed to callbacks
1283 *
1284 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1285 * node and @znode_cb for each indexing node. Returns zero in case of success
1286 * and a negative error code in case of failure.
1287 *
1288 * It would be better if this function removed every znode it pulled to into
1289 * the TNC, so that the behavior more closely matched the non-debugging
1290 * behavior.
1291 */
1292int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1293 dbg_znode_callback znode_cb, void *priv)
1294{
1295 int err;
1296 struct ubifs_zbranch *zbr;
1297 struct ubifs_znode *znode, *child;
1298
1299 mutex_lock(&c->tnc_mutex);
1300 /* If the root indexing node is not in TNC - pull it */
1301 if (!c->zroot.znode) {
1302 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1303 if (IS_ERR(c->zroot.znode)) {
1304 err = PTR_ERR(c->zroot.znode);
1305 c->zroot.znode = NULL;
1306 goto out_unlock;
1307 }
1308 }
1309
1310 /*
1311 * We are going to traverse the indexing tree in the postorder manner.
1312 * Go down and find the leftmost indexing node where we are going to
1313 * start from.
1314 */
1315 znode = c->zroot.znode;
1316 while (znode->level > 0) {
1317 zbr = &znode->zbranch[0];
1318 child = zbr->znode;
1319 if (!child) {
1320 child = ubifs_load_znode(c, zbr, znode, 0);
1321 if (IS_ERR(child)) {
1322 err = PTR_ERR(child);
1323 goto out_unlock;
1324 }
1325 zbr->znode = child;
1326 }
1327
1328 znode = child;
1329 }
1330
1331 /* Iterate over all indexing nodes */
1332 while (1) {
1333 int idx;
1334
1335 cond_resched();
1336
1337 if (znode_cb) {
1338 err = znode_cb(c, znode, priv);
1339 if (err) {
1340 ubifs_err("znode checking function returned "
1341 "error %d", err);
1342 dbg_dump_znode(c, znode);
1343 goto out_dump;
1344 }
1345 }
1346 if (leaf_cb && znode->level == 0) {
1347 for (idx = 0; idx < znode->child_cnt; idx++) {
1348 zbr = &znode->zbranch[idx];
1349 err = leaf_cb(c, zbr, priv);
1350 if (err) {
1351 ubifs_err("leaf checking function "
1352 "returned error %d, for leaf "
1353 "at LEB %d:%d",
1354 err, zbr->lnum, zbr->offs);
1355 goto out_dump;
1356 }
1357 }
1358 }
1359
1360 if (!znode->parent)
1361 break;
1362
1363 idx = znode->iip + 1;
1364 znode = znode->parent;
1365 if (idx < znode->child_cnt) {
1366 /* Switch to the next index in the parent */
1367 zbr = &znode->zbranch[idx];
1368 child = zbr->znode;
1369 if (!child) {
1370 child = ubifs_load_znode(c, zbr, znode, idx);
1371 if (IS_ERR(child)) {
1372 err = PTR_ERR(child);
1373 goto out_unlock;
1374 }
1375 zbr->znode = child;
1376 }
1377 znode = child;
1378 } else
1379 /*
1380 * This is the last child, switch to the parent and
1381 * continue.
1382 */
1383 continue;
1384
1385 /* Go to the lowest leftmost znode in the new sub-tree */
1386 while (znode->level > 0) {
1387 zbr = &znode->zbranch[0];
1388 child = zbr->znode;
1389 if (!child) {
1390 child = ubifs_load_znode(c, zbr, znode, 0);
1391 if (IS_ERR(child)) {
1392 err = PTR_ERR(child);
1393 goto out_unlock;
1394 }
1395 zbr->znode = child;
1396 }
1397 znode = child;
1398 }
1399 }
1400
1401 mutex_unlock(&c->tnc_mutex);
1402 return 0;
1403
1404out_dump:
1405 if (znode->parent)
1406 zbr = &znode->parent->zbranch[znode->iip];
1407 else
1408 zbr = &c->zroot;
1409 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1410 dbg_dump_znode(c, znode);
1411out_unlock:
1412 mutex_unlock(&c->tnc_mutex);
1413 return err;
1414}
1415
1416/**
1417 * add_size - add znode size to partially calculated index size.
1418 * @c: UBIFS file-system description object
1419 * @znode: znode to add size for
1420 * @priv: partially calculated index size
1421 *
1422 * This is a helper function for 'dbg_check_idx_size()' which is called for
1423 * every indexing node and adds its size to the 'long long' variable pointed to
1424 * by @priv.
1425 */
1426static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1427{
1428 long long *idx_size = priv;
1429 int add;
1430
1431 add = ubifs_idx_node_sz(c, znode->child_cnt);
1432 add = ALIGN(add, 8);
1433 *idx_size += add;
1434 return 0;
1435}
1436
1437/**
1438 * dbg_check_idx_size - check index size.
1439 * @c: UBIFS file-system description object
1440 * @idx_size: size to check
1441 *
1442 * This function walks the UBIFS index, calculates its size and checks that the
1443 * size is equivalent to @idx_size. Returns zero in case of success and a
1444 * negative error code in case of failure.
1445 */
1446int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1447{
1448 int err;
1449 long long calc = 0;
1450
1451 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1452 return 0;
1453
1454 err = dbg_walk_index(c, NULL, add_size, &calc);
1455 if (err) {
1456 ubifs_err("error %d while walking the index", err);
1457 return err;
1458 }
1459
1460 if (calc != idx_size) {
1461 ubifs_err("index size check failed: calculated size is %lld, "
1462 "should be %lld", calc, idx_size);
1463 dump_stack();
1464 return -EINVAL;
1465 }
1466
1467 return 0;
1468}
1469
1470/**
1471 * struct fsck_inode - information about an inode used when checking the file-system.
1472 * @rb: link in the RB-tree of inodes
1473 * @inum: inode number
1474 * @mode: inode type, permissions, etc
1475 * @nlink: inode link count
1476 * @xattr_cnt: count of extended attributes
1477 * @references: how many directory/xattr entries refer this inode (calculated
1478 * while walking the index)
1479 * @calc_cnt: for directory inode count of child directories
1480 * @size: inode size (read from on-flash inode)
1481 * @xattr_sz: summary size of all extended attributes (read from on-flash
1482 * inode)
1483 * @calc_sz: for directories calculated directory size
1484 * @calc_xcnt: count of extended attributes
1485 * @calc_xsz: calculated summary size of all extended attributes
1486 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1487 * inode (read from on-flash inode)
1488 * @calc_xnms: calculated sum of lengths of all extended attribute names
1489 */
1490struct fsck_inode {
1491 struct rb_node rb;
1492 ino_t inum;
1493 umode_t mode;
1494 unsigned int nlink;
1495 unsigned int xattr_cnt;
1496 int references;
1497 int calc_cnt;
1498 long long size;
1499 unsigned int xattr_sz;
1500 long long calc_sz;
1501 long long calc_xcnt;
1502 long long calc_xsz;
1503 unsigned int xattr_nms;
1504 long long calc_xnms;
1505};
1506
1507/**
1508 * struct fsck_data - private FS checking information.
1509 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1510 */
1511struct fsck_data {
1512 struct rb_root inodes;
1513};
1514
1515/**
1516 * add_inode - add inode information to RB-tree of inodes.
1517 * @c: UBIFS file-system description object
1518 * @fsckd: FS checking information
1519 * @ino: raw UBIFS inode to add
1520 *
1521 * This is a helper function for 'check_leaf()' which adds information about
1522 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1523 * case of success and a negative error code in case of failure.
1524 */
1525static struct fsck_inode *add_inode(struct ubifs_info *c,
1526 struct fsck_data *fsckd,
1527 struct ubifs_ino_node *ino)
1528{
1529 struct rb_node **p, *parent = NULL;
1530 struct fsck_inode *fscki;
1531 ino_t inum = key_inum_flash(c, &ino->key);
1532
1533 p = &fsckd->inodes.rb_node;
1534 while (*p) {
1535 parent = *p;
1536 fscki = rb_entry(parent, struct fsck_inode, rb);
1537 if (inum < fscki->inum)
1538 p = &(*p)->rb_left;
1539 else if (inum > fscki->inum)
1540 p = &(*p)->rb_right;
1541 else
1542 return fscki;
1543 }
1544
1545 if (inum > c->highest_inum) {
1546 ubifs_err("too high inode number, max. is %lu",
1547 c->highest_inum);
1548 return ERR_PTR(-EINVAL);
1549 }
1550
1551 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1552 if (!fscki)
1553 return ERR_PTR(-ENOMEM);
1554
1555 fscki->inum = inum;
1556 fscki->nlink = le32_to_cpu(ino->nlink);
1557 fscki->size = le64_to_cpu(ino->size);
1558 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1559 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1560 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1561 fscki->mode = le32_to_cpu(ino->mode);
1562 if (S_ISDIR(fscki->mode)) {
1563 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1564 fscki->calc_cnt = 2;
1565 }
1566 rb_link_node(&fscki->rb, parent, p);
1567 rb_insert_color(&fscki->rb, &fsckd->inodes);
1568 return fscki;
1569}
1570
1571/**
1572 * search_inode - search inode in the RB-tree of inodes.
1573 * @fsckd: FS checking information
1574 * @inum: inode number to search
1575 *
1576 * This is a helper function for 'check_leaf()' which searches inode @inum in
1577 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1578 * the inode was not found.
1579 */
1580static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1581{
1582 struct rb_node *p;
1583 struct fsck_inode *fscki;
1584
1585 p = fsckd->inodes.rb_node;
1586 while (p) {
1587 fscki = rb_entry(p, struct fsck_inode, rb);
1588 if (inum < fscki->inum)
1589 p = p->rb_left;
1590 else if (inum > fscki->inum)
1591 p = p->rb_right;
1592 else
1593 return fscki;
1594 }
1595 return NULL;
1596}
1597
1598/**
1599 * read_add_inode - read inode node and add it to RB-tree of inodes.
1600 * @c: UBIFS file-system description object
1601 * @fsckd: FS checking information
1602 * @inum: inode number to read
1603 *
1604 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1605 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1606 * information pointer in case of success and a negative error code in case of
1607 * failure.
1608 */
1609static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1610 struct fsck_data *fsckd, ino_t inum)
1611{
1612 int n, err;
1613 union ubifs_key key;
1614 struct ubifs_znode *znode;
1615 struct ubifs_zbranch *zbr;
1616 struct ubifs_ino_node *ino;
1617 struct fsck_inode *fscki;
1618
1619 fscki = search_inode(fsckd, inum);
1620 if (fscki)
1621 return fscki;
1622
1623 ino_key_init(c, &key, inum);
1624 err = ubifs_lookup_level0(c, &key, &znode, &n);
1625 if (!err) {
1626 ubifs_err("inode %lu not found in index", inum);
1627 return ERR_PTR(-ENOENT);
1628 } else if (err < 0) {
1629 ubifs_err("error %d while looking up inode %lu", err, inum);
1630 return ERR_PTR(err);
1631 }
1632
1633 zbr = &znode->zbranch[n];
1634 if (zbr->len < UBIFS_INO_NODE_SZ) {
1635 ubifs_err("bad node %lu node length %d", inum, zbr->len);
1636 return ERR_PTR(-EINVAL);
1637 }
1638
1639 ino = kmalloc(zbr->len, GFP_NOFS);
1640 if (!ino)
1641 return ERR_PTR(-ENOMEM);
1642
1643 err = ubifs_tnc_read_node(c, zbr, ino);
1644 if (err) {
1645 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1646 zbr->lnum, zbr->offs, err);
1647 kfree(ino);
1648 return ERR_PTR(err);
1649 }
1650
1651 fscki = add_inode(c, fsckd, ino);
1652 kfree(ino);
1653 if (IS_ERR(fscki)) {
1654 ubifs_err("error %ld while adding inode %lu node",
1655 PTR_ERR(fscki), inum);
1656 return fscki;
1657 }
1658
1659 return fscki;
1660}
1661
1662/**
1663 * check_leaf - check leaf node.
1664 * @c: UBIFS file-system description object
1665 * @zbr: zbranch of the leaf node to check
1666 * @priv: FS checking information
1667 *
1668 * This is a helper function for 'dbg_check_filesystem()' which is called for
1669 * every single leaf node while walking the indexing tree. It checks that the
1670 * leaf node referred from the indexing tree exists, has correct CRC, and does
1671 * some other basic validation. This function is also responsible for building
1672 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1673 * calculates reference count, size, etc for each inode in order to later
1674 * compare them to the information stored inside the inodes and detect possible
1675 * inconsistencies. Returns zero in case of success and a negative error code
1676 * in case of failure.
1677 */
1678static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1679 void *priv)
1680{
1681 ino_t inum;
1682 void *node;
1683 struct ubifs_ch *ch;
1684 int err, type = key_type(c, &zbr->key);
1685 struct fsck_inode *fscki;
1686
1687 if (zbr->len < UBIFS_CH_SZ) {
1688 ubifs_err("bad leaf length %d (LEB %d:%d)",
1689 zbr->len, zbr->lnum, zbr->offs);
1690 return -EINVAL;
1691 }
1692
1693 node = kmalloc(zbr->len, GFP_NOFS);
1694 if (!node)
1695 return -ENOMEM;
1696
1697 err = ubifs_tnc_read_node(c, zbr, node);
1698 if (err) {
1699 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1700 zbr->lnum, zbr->offs, err);
1701 goto out_free;
1702 }
1703
1704 /* If this is an inode node, add it to RB-tree of inodes */
1705 if (type == UBIFS_INO_KEY) {
1706 fscki = add_inode(c, priv, node);
1707 if (IS_ERR(fscki)) {
1708 err = PTR_ERR(fscki);
1709 ubifs_err("error %d while adding inode node", err);
1710 goto out_dump;
1711 }
1712 goto out;
1713 }
1714
1715 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1716 type != UBIFS_DATA_KEY) {
1717 ubifs_err("unexpected node type %d at LEB %d:%d",
1718 type, zbr->lnum, zbr->offs);
1719 err = -EINVAL;
1720 goto out_free;
1721 }
1722
1723 ch = node;
1724 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1725 ubifs_err("too high sequence number, max. is %llu",
1726 c->max_sqnum);
1727 err = -EINVAL;
1728 goto out_dump;
1729 }
1730
1731 if (type == UBIFS_DATA_KEY) {
1732 long long blk_offs;
1733 struct ubifs_data_node *dn = node;
1734
1735 /*
1736 * Search the inode node this data node belongs to and insert
1737 * it to the RB-tree of inodes.
1738 */
1739 inum = key_inum_flash(c, &dn->key);
1740 fscki = read_add_inode(c, priv, inum);
1741 if (IS_ERR(fscki)) {
1742 err = PTR_ERR(fscki);
1743 ubifs_err("error %d while processing data node and "
1744 "trying to find inode node %lu", err, inum);
1745 goto out_dump;
1746 }
1747
1748 /* Make sure the data node is within inode size */
1749 blk_offs = key_block_flash(c, &dn->key);
1750 blk_offs <<= UBIFS_BLOCK_SHIFT;
1751 blk_offs += le32_to_cpu(dn->size);
1752 if (blk_offs > fscki->size) {
1753 ubifs_err("data node at LEB %d:%d is not within inode "
1754 "size %lld", zbr->lnum, zbr->offs,
1755 fscki->size);
1756 err = -EINVAL;
1757 goto out_dump;
1758 }
1759 } else {
1760 int nlen;
1761 struct ubifs_dent_node *dent = node;
1762 struct fsck_inode *fscki1;
1763
1764 err = ubifs_validate_entry(c, dent);
1765 if (err)
1766 goto out_dump;
1767
1768 /*
1769 * Search the inode node this entry refers to and the parent
1770 * inode node and insert them to the RB-tree of inodes.
1771 */
1772 inum = le64_to_cpu(dent->inum);
1773 fscki = read_add_inode(c, priv, inum);
1774 if (IS_ERR(fscki)) {
1775 err = PTR_ERR(fscki);
1776 ubifs_err("error %d while processing entry node and "
1777 "trying to find inode node %lu", err, inum);
1778 goto out_dump;
1779 }
1780
1781 /* Count how many direntries or xentries refers this inode */
1782 fscki->references += 1;
1783
1784 inum = key_inum_flash(c, &dent->key);
1785 fscki1 = read_add_inode(c, priv, inum);
1786 if (IS_ERR(fscki1)) {
1787 err = PTR_ERR(fscki);
1788 ubifs_err("error %d while processing entry node and "
1789 "trying to find parent inode node %lu",
1790 err, inum);
1791 goto out_dump;
1792 }
1793
1794 nlen = le16_to_cpu(dent->nlen);
1795 if (type == UBIFS_XENT_KEY) {
1796 fscki1->calc_xcnt += 1;
1797 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
1798 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
1799 fscki1->calc_xnms += nlen;
1800 } else {
1801 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
1802 if (dent->type == UBIFS_ITYPE_DIR)
1803 fscki1->calc_cnt += 1;
1804 }
1805 }
1806
1807out:
1808 kfree(node);
1809 return 0;
1810
1811out_dump:
1812 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
1813 dbg_dump_node(c, node);
1814out_free:
1815 kfree(node);
1816 return err;
1817}
1818
1819/**
1820 * free_inodes - free RB-tree of inodes.
1821 * @fsckd: FS checking information
1822 */
1823static void free_inodes(struct fsck_data *fsckd)
1824{
1825 struct rb_node *this = fsckd->inodes.rb_node;
1826 struct fsck_inode *fscki;
1827
1828 while (this) {
1829 if (this->rb_left)
1830 this = this->rb_left;
1831 else if (this->rb_right)
1832 this = this->rb_right;
1833 else {
1834 fscki = rb_entry(this, struct fsck_inode, rb);
1835 this = rb_parent(this);
1836 if (this) {
1837 if (this->rb_left == &fscki->rb)
1838 this->rb_left = NULL;
1839 else
1840 this->rb_right = NULL;
1841 }
1842 kfree(fscki);
1843 }
1844 }
1845}
1846
1847/**
1848 * check_inodes - checks all inodes.
1849 * @c: UBIFS file-system description object
1850 * @fsckd: FS checking information
1851 *
1852 * This is a helper function for 'dbg_check_filesystem()' which walks the
1853 * RB-tree of inodes after the index scan has been finished, and checks that
1854 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
1855 * %-EINVAL if not, and a negative error code in case of failure.
1856 */
1857static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
1858{
1859 int n, err;
1860 union ubifs_key key;
1861 struct ubifs_znode *znode;
1862 struct ubifs_zbranch *zbr;
1863 struct ubifs_ino_node *ino;
1864 struct fsck_inode *fscki;
1865 struct rb_node *this = rb_first(&fsckd->inodes);
1866
1867 while (this) {
1868 fscki = rb_entry(this, struct fsck_inode, rb);
1869 this = rb_next(this);
1870
1871 if (S_ISDIR(fscki->mode)) {
1872 /*
1873 * Directories have to have exactly one reference (they
1874 * cannot have hardlinks), although root inode is an
1875 * exception.
1876 */
1877 if (fscki->inum != UBIFS_ROOT_INO &&
1878 fscki->references != 1) {
1879 ubifs_err("directory inode %lu has %d "
1880 "direntries which refer it, but "
1881 "should be 1", fscki->inum,
1882 fscki->references);
1883 goto out_dump;
1884 }
1885 if (fscki->inum == UBIFS_ROOT_INO &&
1886 fscki->references != 0) {
1887 ubifs_err("root inode %lu has non-zero (%d) "
1888 "direntries which refer it",
1889 fscki->inum, fscki->references);
1890 goto out_dump;
1891 }
1892 if (fscki->calc_sz != fscki->size) {
1893 ubifs_err("directory inode %lu size is %lld, "
1894 "but calculated size is %lld",
1895 fscki->inum, fscki->size,
1896 fscki->calc_sz);
1897 goto out_dump;
1898 }
1899 if (fscki->calc_cnt != fscki->nlink) {
1900 ubifs_err("directory inode %lu nlink is %d, "
1901 "but calculated nlink is %d",
1902 fscki->inum, fscki->nlink,
1903 fscki->calc_cnt);
1904 goto out_dump;
1905 }
1906 } else {
1907 if (fscki->references != fscki->nlink) {
1908 ubifs_err("inode %lu nlink is %d, but "
1909 "calculated nlink is %d", fscki->inum,
1910 fscki->nlink, fscki->references);
1911 goto out_dump;
1912 }
1913 }
1914 if (fscki->xattr_sz != fscki->calc_xsz) {
1915 ubifs_err("inode %lu has xattr size %u, but "
1916 "calculated size is %lld",
1917 fscki->inum, fscki->xattr_sz,
1918 fscki->calc_xsz);
1919 goto out_dump;
1920 }
1921 if (fscki->xattr_cnt != fscki->calc_xcnt) {
1922 ubifs_err("inode %lu has %u xattrs, but "
1923 "calculated count is %lld", fscki->inum,
1924 fscki->xattr_cnt, fscki->calc_xcnt);
1925 goto out_dump;
1926 }
1927 if (fscki->xattr_nms != fscki->calc_xnms) {
1928 ubifs_err("inode %lu has xattr names' size %u, but "
1929 "calculated names' size is %lld",
1930 fscki->inum, fscki->xattr_nms,
1931 fscki->calc_xnms);
1932 goto out_dump;
1933 }
1934 }
1935
1936 return 0;
1937
1938out_dump:
1939 /* Read the bad inode and dump it */
1940 ino_key_init(c, &key, fscki->inum);
1941 err = ubifs_lookup_level0(c, &key, &znode, &n);
1942 if (!err) {
1943 ubifs_err("inode %lu not found in index", fscki->inum);
1944 return -ENOENT;
1945 } else if (err < 0) {
1946 ubifs_err("error %d while looking up inode %lu",
1947 err, fscki->inum);
1948 return err;
1949 }
1950
1951 zbr = &znode->zbranch[n];
1952 ino = kmalloc(zbr->len, GFP_NOFS);
1953 if (!ino)
1954 return -ENOMEM;
1955
1956 err = ubifs_tnc_read_node(c, zbr, ino);
1957 if (err) {
1958 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1959 zbr->lnum, zbr->offs, err);
1960 kfree(ino);
1961 return err;
1962 }
1963
1964 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
1965 fscki->inum, zbr->lnum, zbr->offs);
1966 dbg_dump_node(c, ino);
1967 kfree(ino);
1968 return -EINVAL;
1969}
1970
1971/**
1972 * dbg_check_filesystem - check the file-system.
1973 * @c: UBIFS file-system description object
1974 *
1975 * This function checks the file system, namely:
1976 * o makes sure that all leaf nodes exist and their CRCs are correct;
1977 * o makes sure inode nlink, size, xattr size/count are correct (for all
1978 * inodes).
1979 *
1980 * The function reads whole indexing tree and all nodes, so it is pretty
1981 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
1982 * not, and a negative error code in case of failure.
1983 */
1984int dbg_check_filesystem(struct ubifs_info *c)
1985{
1986 int err;
1987 struct fsck_data fsckd;
1988
1989 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
1990 return 0;
1991
1992 fsckd.inodes = RB_ROOT;
1993 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
1994 if (err)
1995 goto out_free;
1996
1997 err = check_inodes(c, &fsckd);
1998 if (err)
1999 goto out_free;
2000
2001 free_inodes(&fsckd);
2002 return 0;
2003
2004out_free:
2005 ubifs_err("file-system check failed with error %d", err);
2006 dump_stack();
2007 free_inodes(&fsckd);
2008 return err;
2009}
2010
2011static int invocation_cnt;
2012
2013int dbg_force_in_the_gaps(void)
2014{
2015 if (!dbg_force_in_the_gaps_enabled)
2016 return 0;
2017 /* Force in-the-gaps every 8th commit */
2018 return !((invocation_cnt++) & 0x7);
2019}
2020
2021/* Failure mode for recovery testing */
2022
2023#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2024
2025struct failure_mode_info {
2026 struct list_head list;
2027 struct ubifs_info *c;
2028};
2029
2030static LIST_HEAD(fmi_list);
2031static DEFINE_SPINLOCK(fmi_lock);
2032
2033static unsigned int next;
2034
2035static int simple_rand(void)
2036{
2037 if (next == 0)
2038 next = current->pid;
2039 next = next * 1103515245 + 12345;
2040 return (next >> 16) & 32767;
2041}
2042
2043void dbg_failure_mode_registration(struct ubifs_info *c)
2044{
2045 struct failure_mode_info *fmi;
2046
2047 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2048 if (!fmi) {
2049 dbg_err("Failed to register failure mode - no memory");
2050 return;
2051 }
2052 fmi->c = c;
2053 spin_lock(&fmi_lock);
2054 list_add_tail(&fmi->list, &fmi_list);
2055 spin_unlock(&fmi_lock);
2056}
2057
2058void dbg_failure_mode_deregistration(struct ubifs_info *c)
2059{
2060 struct failure_mode_info *fmi, *tmp;
2061
2062 spin_lock(&fmi_lock);
2063 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2064 if (fmi->c == c) {
2065 list_del(&fmi->list);
2066 kfree(fmi);
2067 }
2068 spin_unlock(&fmi_lock);
2069}
2070
2071static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2072{
2073 struct failure_mode_info *fmi;
2074
2075 spin_lock(&fmi_lock);
2076 list_for_each_entry(fmi, &fmi_list, list)
2077 if (fmi->c->ubi == desc) {
2078 struct ubifs_info *c = fmi->c;
2079
2080 spin_unlock(&fmi_lock);
2081 return c;
2082 }
2083 spin_unlock(&fmi_lock);
2084 return NULL;
2085}
2086
2087static int in_failure_mode(struct ubi_volume_desc *desc)
2088{
2089 struct ubifs_info *c = dbg_find_info(desc);
2090
2091 if (c && dbg_failure_mode)
2092 return c->failure_mode;
2093 return 0;
2094}
2095
2096static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2097{
2098 struct ubifs_info *c = dbg_find_info(desc);
2099
2100 if (!c || !dbg_failure_mode)
2101 return 0;
2102 if (c->failure_mode)
2103 return 1;
2104 if (!c->fail_cnt) {
2105 /* First call - decide delay to failure */
2106 if (chance(1, 2)) {
2107 unsigned int delay = 1 << (simple_rand() >> 11);
2108
2109 if (chance(1, 2)) {
2110 c->fail_delay = 1;
2111 c->fail_timeout = jiffies +
2112 msecs_to_jiffies(delay);
2113 dbg_rcvry("failing after %ums", delay);
2114 } else {
2115 c->fail_delay = 2;
2116 c->fail_cnt_max = delay;
2117 dbg_rcvry("failing after %u calls", delay);
2118 }
2119 }
2120 c->fail_cnt += 1;
2121 }
2122 /* Determine if failure delay has expired */
2123 if (c->fail_delay == 1) {
2124 if (time_before(jiffies, c->fail_timeout))
2125 return 0;
2126 } else if (c->fail_delay == 2)
2127 if (c->fail_cnt++ < c->fail_cnt_max)
2128 return 0;
2129 if (lnum == UBIFS_SB_LNUM) {
2130 if (write) {
2131 if (chance(1, 2))
2132 return 0;
2133 } else if (chance(19, 20))
2134 return 0;
2135 dbg_rcvry("failing in super block LEB %d", lnum);
2136 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2137 if (chance(19, 20))
2138 return 0;
2139 dbg_rcvry("failing in master LEB %d", lnum);
2140 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2141 if (write) {
2142 if (chance(99, 100))
2143 return 0;
2144 } else if (chance(399, 400))
2145 return 0;
2146 dbg_rcvry("failing in log LEB %d", lnum);
2147 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2148 if (write) {
2149 if (chance(7, 8))
2150 return 0;
2151 } else if (chance(19, 20))
2152 return 0;
2153 dbg_rcvry("failing in LPT LEB %d", lnum);
2154 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2155 if (write) {
2156 if (chance(1, 2))
2157 return 0;
2158 } else if (chance(9, 10))
2159 return 0;
2160 dbg_rcvry("failing in orphan LEB %d", lnum);
2161 } else if (lnum == c->ihead_lnum) {
2162 if (chance(99, 100))
2163 return 0;
2164 dbg_rcvry("failing in index head LEB %d", lnum);
2165 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2166 if (chance(9, 10))
2167 return 0;
2168 dbg_rcvry("failing in GC head LEB %d", lnum);
2169 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2170 !ubifs_search_bud(c, lnum)) {
2171 if (chance(19, 20))
2172 return 0;
2173 dbg_rcvry("failing in non-bud LEB %d", lnum);
2174 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2175 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2176 if (chance(999, 1000))
2177 return 0;
2178 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2179 } else {
2180 if (chance(9999, 10000))
2181 return 0;
2182 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2183 }
2184 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2185 c->failure_mode = 1;
2186 dump_stack();
2187 return 1;
2188}
2189
2190static void cut_data(const void *buf, int len)
2191{
2192 int flen, i;
2193 unsigned char *p = (void *)buf;
2194
2195 flen = (len * (long long)simple_rand()) >> 15;
2196 for (i = flen; i < len; i++)
2197 p[i] = 0xff;
2198}
2199
2200int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2201 int len, int check)
2202{
2203 if (in_failure_mode(desc))
2204 return -EIO;
2205 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2206}
2207
2208int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2209 int offset, int len, int dtype)
2210{
2211 int err;
2212
2213 if (in_failure_mode(desc))
2214 return -EIO;
2215 if (do_fail(desc, lnum, 1))
2216 cut_data(buf, len);
2217 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2218 if (err)
2219 return err;
2220 if (in_failure_mode(desc))
2221 return -EIO;
2222 return 0;
2223}
2224
2225int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2226 int len, int dtype)
2227{
2228 int err;
2229
2230 if (do_fail(desc, lnum, 1))
2231 return -EIO;
2232 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2233 if (err)
2234 return err;
2235 if (do_fail(desc, lnum, 1))
2236 return -EIO;
2237 return 0;
2238}
2239
2240int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2241{
2242 int err;
2243
2244 if (do_fail(desc, lnum, 0))
2245 return -EIO;
2246 err = ubi_leb_erase(desc, lnum);
2247 if (err)
2248 return err;
2249 if (do_fail(desc, lnum, 0))
2250 return -EIO;
2251 return 0;
2252}
2253
2254int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2255{
2256 int err;
2257
2258 if (do_fail(desc, lnum, 0))
2259 return -EIO;
2260 err = ubi_leb_unmap(desc, lnum);
2261 if (err)
2262 return err;
2263 if (do_fail(desc, lnum, 0))
2264 return -EIO;
2265 return 0;
2266}
2267
2268int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2269{
2270 if (in_failure_mode(desc))
2271 return -EIO;
2272 return ubi_is_mapped(desc, lnum);
2273}
2274
2275int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2276{
2277 int err;
2278
2279 if (do_fail(desc, lnum, 0))
2280 return -EIO;
2281 err = ubi_leb_map(desc, lnum, dtype);
2282 if (err)
2283 return err;
2284 if (do_fail(desc, lnum, 0))
2285 return -EIO;
2286 return 0;
2287}
2288
2289#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/debug.h b/fs/ubifs/debug.h
new file mode 100644
index 000000000000..3c4f1e93c9e0
--- /dev/null
+++ b/fs/ubifs/debug.h
@@ -0,0 +1,403 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23#ifndef __UBIFS_DEBUG_H__
24#define __UBIFS_DEBUG_H__
25
26#ifdef CONFIG_UBIFS_FS_DEBUG
27
28#define UBIFS_DBG(op) op
29
30#define ubifs_assert(expr) do { \
31 if (unlikely(!(expr))) { \
32 printk(KERN_CRIT "UBIFS assert failed in %s at %u (pid %d)\n", \
33 __func__, __LINE__, current->pid); \
34 dbg_dump_stack(); \
35 } \
36} while (0)
37
38#define ubifs_assert_cmt_locked(c) do { \
39 if (unlikely(down_write_trylock(&(c)->commit_sem))) { \
40 up_write(&(c)->commit_sem); \
41 printk(KERN_CRIT "commit lock is not locked!\n"); \
42 ubifs_assert(0); \
43 } \
44} while (0)
45
46#define dbg_dump_stack() do { \
47 if (!dbg_failure_mode) \
48 dump_stack(); \
49} while (0)
50
51/* Generic debugging messages */
52#define dbg_msg(fmt, ...) do { \
53 spin_lock(&dbg_lock); \
54 printk(KERN_DEBUG "UBIFS DBG (pid %d): %s: " fmt "\n", current->pid, \
55 __func__, ##__VA_ARGS__); \
56 spin_unlock(&dbg_lock); \
57} while (0)
58
59#define dbg_do_msg(typ, fmt, ...) do { \
60 if (ubifs_msg_flags & typ) \
61 dbg_msg(fmt, ##__VA_ARGS__); \
62} while (0)
63
64#define dbg_err(fmt, ...) do { \
65 spin_lock(&dbg_lock); \
66 ubifs_err(fmt, ##__VA_ARGS__); \
67 spin_unlock(&dbg_lock); \
68} while (0)
69
70const char *dbg_key_str0(const struct ubifs_info *c,
71 const union ubifs_key *key);
72const char *dbg_key_str1(const struct ubifs_info *c,
73 const union ubifs_key *key);
74
75/*
76 * DBGKEY macros require dbg_lock to be held, which it is in the dbg message
77 * macros.
78 */
79#define DBGKEY(key) dbg_key_str0(c, (key))
80#define DBGKEY1(key) dbg_key_str1(c, (key))
81
82/* General messages */
83#define dbg_gen(fmt, ...) dbg_do_msg(UBIFS_MSG_GEN, fmt, ##__VA_ARGS__)
84
85/* Additional journal messages */
86#define dbg_jnl(fmt, ...) dbg_do_msg(UBIFS_MSG_JNL, fmt, ##__VA_ARGS__)
87
88/* Additional TNC messages */
89#define dbg_tnc(fmt, ...) dbg_do_msg(UBIFS_MSG_TNC, fmt, ##__VA_ARGS__)
90
91/* Additional lprops messages */
92#define dbg_lp(fmt, ...) dbg_do_msg(UBIFS_MSG_LP, fmt, ##__VA_ARGS__)
93
94/* Additional LEB find messages */
95#define dbg_find(fmt, ...) dbg_do_msg(UBIFS_MSG_FIND, fmt, ##__VA_ARGS__)
96
97/* Additional mount messages */
98#define dbg_mnt(fmt, ...) dbg_do_msg(UBIFS_MSG_MNT, fmt, ##__VA_ARGS__)
99
100/* Additional I/O messages */
101#define dbg_io(fmt, ...) dbg_do_msg(UBIFS_MSG_IO, fmt, ##__VA_ARGS__)
102
103/* Additional commit messages */
104#define dbg_cmt(fmt, ...) dbg_do_msg(UBIFS_MSG_CMT, fmt, ##__VA_ARGS__)
105
106/* Additional budgeting messages */
107#define dbg_budg(fmt, ...) dbg_do_msg(UBIFS_MSG_BUDG, fmt, ##__VA_ARGS__)
108
109/* Additional log messages */
110#define dbg_log(fmt, ...) dbg_do_msg(UBIFS_MSG_LOG, fmt, ##__VA_ARGS__)
111
112/* Additional gc messages */
113#define dbg_gc(fmt, ...) dbg_do_msg(UBIFS_MSG_GC, fmt, ##__VA_ARGS__)
114
115/* Additional scan messages */
116#define dbg_scan(fmt, ...) dbg_do_msg(UBIFS_MSG_SCAN, fmt, ##__VA_ARGS__)
117
118/* Additional recovery messages */
119#define dbg_rcvry(fmt, ...) dbg_do_msg(UBIFS_MSG_RCVRY, fmt, ##__VA_ARGS__)
120
121/*
122 * Debugging message type flags (must match msg_type_names in debug.c).
123 *
124 * UBIFS_MSG_GEN: general messages
125 * UBIFS_MSG_JNL: journal messages
126 * UBIFS_MSG_MNT: mount messages
127 * UBIFS_MSG_CMT: commit messages
128 * UBIFS_MSG_FIND: LEB find messages
129 * UBIFS_MSG_BUDG: budgeting messages
130 * UBIFS_MSG_GC: garbage collection messages
131 * UBIFS_MSG_TNC: TNC messages
132 * UBIFS_MSG_LP: lprops messages
133 * UBIFS_MSG_IO: I/O messages
134 * UBIFS_MSG_LOG: log messages
135 * UBIFS_MSG_SCAN: scan messages
136 * UBIFS_MSG_RCVRY: recovery messages
137 */
138enum {
139 UBIFS_MSG_GEN = 0x1,
140 UBIFS_MSG_JNL = 0x2,
141 UBIFS_MSG_MNT = 0x4,
142 UBIFS_MSG_CMT = 0x8,
143 UBIFS_MSG_FIND = 0x10,
144 UBIFS_MSG_BUDG = 0x20,
145 UBIFS_MSG_GC = 0x40,
146 UBIFS_MSG_TNC = 0x80,
147 UBIFS_MSG_LP = 0x100,
148 UBIFS_MSG_IO = 0x200,
149 UBIFS_MSG_LOG = 0x400,
150 UBIFS_MSG_SCAN = 0x800,
151 UBIFS_MSG_RCVRY = 0x1000,
152};
153
154/* Debugging message type flags for each default debug message level */
155#define UBIFS_MSG_LVL_0 0
156#define UBIFS_MSG_LVL_1 0x1
157#define UBIFS_MSG_LVL_2 0x7f
158#define UBIFS_MSG_LVL_3 0xffff
159
160/*
161 * Debugging check flags (must match chk_names in debug.c).
162 *
163 * UBIFS_CHK_GEN: general checks
164 * UBIFS_CHK_TNC: check TNC
165 * UBIFS_CHK_IDX_SZ: check index size
166 * UBIFS_CHK_ORPH: check orphans
167 * UBIFS_CHK_OLD_IDX: check the old index
168 * UBIFS_CHK_LPROPS: check lprops
169 * UBIFS_CHK_FS: check the file-system
170 */
171enum {
172 UBIFS_CHK_GEN = 0x1,
173 UBIFS_CHK_TNC = 0x2,
174 UBIFS_CHK_IDX_SZ = 0x4,
175 UBIFS_CHK_ORPH = 0x8,
176 UBIFS_CHK_OLD_IDX = 0x10,
177 UBIFS_CHK_LPROPS = 0x20,
178 UBIFS_CHK_FS = 0x40,
179};
180
181/*
182 * Special testing flags (must match tst_names in debug.c).
183 *
184 * UBIFS_TST_FORCE_IN_THE_GAPS: force the use of in-the-gaps method
185 * UBIFS_TST_RCVRY: failure mode for recovery testing
186 */
187enum {
188 UBIFS_TST_FORCE_IN_THE_GAPS = 0x2,
189 UBIFS_TST_RCVRY = 0x4,
190};
191
192#if CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 1
193#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_1
194#elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 2
195#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_2
196#elif CONFIG_UBIFS_FS_DEBUG_MSG_LVL == 3
197#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_3
198#else
199#define UBIFS_MSG_FLAGS_DEFAULT UBIFS_MSG_LVL_0
200#endif
201
202#ifdef CONFIG_UBIFS_FS_DEBUG_CHKS
203#define UBIFS_CHK_FLAGS_DEFAULT 0xffffffff
204#else
205#define UBIFS_CHK_FLAGS_DEFAULT 0
206#endif
207
208extern spinlock_t dbg_lock;
209
210extern unsigned int ubifs_msg_flags;
211extern unsigned int ubifs_chk_flags;
212extern unsigned int ubifs_tst_flags;
213
214/* Dump functions */
215
216const char *dbg_ntype(int type);
217const char *dbg_cstate(int cmt_state);
218const char *dbg_get_key_dump(const struct ubifs_info *c,
219 const union ubifs_key *key);
220void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode);
221void dbg_dump_node(const struct ubifs_info *c, const void *node);
222void dbg_dump_budget_req(const struct ubifs_budget_req *req);
223void dbg_dump_lstats(const struct ubifs_lp_stats *lst);
224void dbg_dump_budg(struct ubifs_info *c);
225void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp);
226void dbg_dump_lprops(struct ubifs_info *c);
227void dbg_dump_leb(const struct ubifs_info *c, int lnum);
228void dbg_dump_znode(const struct ubifs_info *c,
229 const struct ubifs_znode *znode);
230void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat);
231void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
232 struct ubifs_nnode *parent, int iip);
233void dbg_dump_tnc(struct ubifs_info *c);
234void dbg_dump_index(struct ubifs_info *c);
235
236/* Checking helper functions */
237
238typedef int (*dbg_leaf_callback)(struct ubifs_info *c,
239 struct ubifs_zbranch *zbr, void *priv);
240typedef int (*dbg_znode_callback)(struct ubifs_info *c,
241 struct ubifs_znode *znode, void *priv);
242
243int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
244 dbg_znode_callback znode_cb, void *priv);
245
246/* Checking functions */
247
248int dbg_check_lprops(struct ubifs_info *c);
249
250int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot);
251int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot);
252
253int dbg_check_cats(struct ubifs_info *c);
254
255int dbg_check_ltab(struct ubifs_info *c);
256
257int dbg_check_synced_i_size(struct inode *inode);
258
259int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir);
260
261int dbg_check_tnc(struct ubifs_info *c, int extra);
262
263int dbg_check_idx_size(struct ubifs_info *c, long long idx_size);
264
265int dbg_check_filesystem(struct ubifs_info *c);
266
267void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
268 int add_pos);
269
270int dbg_check_lprops(struct ubifs_info *c);
271int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
272 int row, int col);
273
274/* Force the use of in-the-gaps method for testing */
275
276#define dbg_force_in_the_gaps_enabled \
277 (ubifs_tst_flags & UBIFS_TST_FORCE_IN_THE_GAPS)
278
279int dbg_force_in_the_gaps(void);
280
281/* Failure mode for recovery testing */
282
283#define dbg_failure_mode (ubifs_tst_flags & UBIFS_TST_RCVRY)
284
285void dbg_failure_mode_registration(struct ubifs_info *c);
286void dbg_failure_mode_deregistration(struct ubifs_info *c);
287
288#ifndef UBIFS_DBG_PRESERVE_UBI
289
290#define ubi_leb_read dbg_leb_read
291#define ubi_leb_write dbg_leb_write
292#define ubi_leb_change dbg_leb_change
293#define ubi_leb_erase dbg_leb_erase
294#define ubi_leb_unmap dbg_leb_unmap
295#define ubi_is_mapped dbg_is_mapped
296#define ubi_leb_map dbg_leb_map
297
298#endif
299
300int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
301 int len, int check);
302int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
303 int offset, int len, int dtype);
304int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
305 int len, int dtype);
306int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum);
307int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum);
308int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum);
309int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype);
310
311static inline int dbg_read(struct ubi_volume_desc *desc, int lnum, char *buf,
312 int offset, int len)
313{
314 return dbg_leb_read(desc, lnum, buf, offset, len, 0);
315}
316
317static inline int dbg_write(struct ubi_volume_desc *desc, int lnum,
318 const void *buf, int offset, int len)
319{
320 return dbg_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN);
321}
322
323static inline int dbg_change(struct ubi_volume_desc *desc, int lnum,
324 const void *buf, int len)
325{
326 return dbg_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
327}
328
329#else /* !CONFIG_UBIFS_FS_DEBUG */
330
331#define UBIFS_DBG(op)
332#define ubifs_assert(expr) ({})
333#define ubifs_assert_cmt_locked(c)
334#define dbg_dump_stack()
335#define dbg_err(fmt, ...) ({})
336#define dbg_msg(fmt, ...) ({})
337#define dbg_key(c, key, fmt, ...) ({})
338
339#define dbg_gen(fmt, ...) ({})
340#define dbg_jnl(fmt, ...) ({})
341#define dbg_tnc(fmt, ...) ({})
342#define dbg_lp(fmt, ...) ({})
343#define dbg_find(fmt, ...) ({})
344#define dbg_mnt(fmt, ...) ({})
345#define dbg_io(fmt, ...) ({})
346#define dbg_cmt(fmt, ...) ({})
347#define dbg_budg(fmt, ...) ({})
348#define dbg_log(fmt, ...) ({})
349#define dbg_gc(fmt, ...) ({})
350#define dbg_scan(fmt, ...) ({})
351#define dbg_rcvry(fmt, ...) ({})
352
353#define dbg_ntype(type) ""
354#define dbg_cstate(cmt_state) ""
355#define dbg_get_key_dump(c, key) ({})
356#define dbg_dump_inode(c, inode) ({})
357#define dbg_dump_node(c, node) ({})
358#define dbg_dump_budget_req(req) ({})
359#define dbg_dump_lstats(lst) ({})
360#define dbg_dump_budg(c) ({})
361#define dbg_dump_lprop(c, lp) ({})
362#define dbg_dump_lprops(c) ({})
363#define dbg_dump_leb(c, lnum) ({})
364#define dbg_dump_znode(c, znode) ({})
365#define dbg_dump_heap(c, heap, cat) ({})
366#define dbg_dump_pnode(c, pnode, parent, iip) ({})
367#define dbg_dump_tnc(c) ({})
368#define dbg_dump_index(c) ({})
369
370#define dbg_walk_index(c, leaf_cb, znode_cb, priv) 0
371
372#define dbg_old_index_check_init(c, zroot) 0
373#define dbg_check_old_index(c, zroot) 0
374
375#define dbg_check_cats(c) 0
376
377#define dbg_check_ltab(c) 0
378
379#define dbg_check_synced_i_size(inode) 0
380
381#define dbg_check_dir_size(c, dir) 0
382
383#define dbg_check_tnc(c, x) 0
384
385#define dbg_check_idx_size(c, idx_size) 0
386
387#define dbg_check_filesystem(c) 0
388
389#define dbg_check_heap(c, heap, cat, add_pos) ({})
390
391#define dbg_check_lprops(c) 0
392#define dbg_check_lpt_nodes(c, cnode, row, col) 0
393
394#define dbg_force_in_the_gaps_enabled 0
395#define dbg_force_in_the_gaps() 0
396
397#define dbg_failure_mode 0
398#define dbg_failure_mode_registration(c) ({})
399#define dbg_failure_mode_deregistration(c) ({})
400
401#endif /* !CONFIG_UBIFS_FS_DEBUG */
402
403#endif /* !__UBIFS_DEBUG_H__ */
diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
new file mode 100644
index 000000000000..e90374be7d3b
--- /dev/null
+++ b/fs/ubifs/dir.c
@@ -0,0 +1,1240 @@
1/* * This file is part of UBIFS.
2 *
3 * Copyright (C) 2006-2008 Nokia Corporation.
4 * Copyright (C) 2006, 2007 University of Szeged, Hungary
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 * Zoltan Sogor
22 */
23
24/*
25 * This file implements directory operations.
26 *
27 * All FS operations in this file allocate budget before writing anything to the
28 * media. If they fail to allocate it, the error is returned. The only
29 * exceptions are 'ubifs_unlink()' and 'ubifs_rmdir()' which keep working even
30 * if they unable to allocate the budget, because deletion %-ENOSPC failure is
31 * not what users are usually ready to get. UBIFS budgeting subsystem has some
32 * space reserved for these purposes.
33 *
34 * All operations in this file write all inodes which they change straight
35 * away, instead of marking them dirty. For example, 'ubifs_link()' changes
36 * @i_size of the parent inode and writes the parent inode together with the
37 * target inode. This was done to simplify file-system recovery which would
38 * otherwise be very difficult to do. The only exception is rename which marks
39 * the re-named inode dirty (because its @i_ctime is updated) but does not
40 * write it, but just marks it as dirty.
41 */
42
43#include "ubifs.h"
44
45/**
46 * inherit_flags - inherit flags of the parent inode.
47 * @dir: parent inode
48 * @mode: new inode mode flags
49 *
50 * This is a helper function for 'ubifs_new_inode()' which inherits flag of the
51 * parent directory inode @dir. UBIFS inodes inherit the following flags:
52 * o %UBIFS_COMPR_FL, which is useful to switch compression on/of on
53 * sub-directory basis;
54 * o %UBIFS_SYNC_FL - useful for the same reasons;
55 * o %UBIFS_DIRSYNC_FL - similar, but relevant only to directories.
56 *
57 * This function returns the inherited flags.
58 */
59static int inherit_flags(const struct inode *dir, int mode)
60{
61 int flags;
62 const struct ubifs_inode *ui = ubifs_inode(dir);
63
64 if (!S_ISDIR(dir->i_mode))
65 /*
66 * The parent is not a directory, which means that an extended
67 * attribute inode is being created. No flags.
68 */
69 return 0;
70
71 flags = ui->flags & (UBIFS_COMPR_FL | UBIFS_SYNC_FL | UBIFS_DIRSYNC_FL);
72 if (!S_ISDIR(mode))
73 /* The "DIRSYNC" flag only applies to directories */
74 flags &= ~UBIFS_DIRSYNC_FL;
75 return flags;
76}
77
78/**
79 * ubifs_new_inode - allocate new UBIFS inode object.
80 * @c: UBIFS file-system description object
81 * @dir: parent directory inode
82 * @mode: inode mode flags
83 *
84 * This function finds an unused inode number, allocates new inode and
85 * initializes it. Returns new inode in case of success and an error code in
86 * case of failure.
87 */
88struct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir,
89 int mode)
90{
91 struct inode *inode;
92 struct ubifs_inode *ui;
93
94 inode = new_inode(c->vfs_sb);
95 ui = ubifs_inode(inode);
96 if (!inode)
97 return ERR_PTR(-ENOMEM);
98
99 /*
100 * Set 'S_NOCMTIME' to prevent VFS form updating [mc]time of inodes and
101 * marking them dirty in file write path (see 'file_update_time()').
102 * UBIFS has to fully control "clean <-> dirty" transitions of inodes
103 * to make budgeting work.
104 */
105 inode->i_flags |= (S_NOCMTIME);
106
107 inode->i_uid = current->fsuid;
108 if (dir->i_mode & S_ISGID) {
109 inode->i_gid = dir->i_gid;
110 if (S_ISDIR(mode))
111 mode |= S_ISGID;
112 } else
113 inode->i_gid = current->fsgid;
114 inode->i_mode = mode;
115 inode->i_mtime = inode->i_atime = inode->i_ctime =
116 ubifs_current_time(inode);
117 inode->i_mapping->nrpages = 0;
118 /* Disable readahead */
119 inode->i_mapping->backing_dev_info = &c->bdi;
120
121 switch (mode & S_IFMT) {
122 case S_IFREG:
123 inode->i_mapping->a_ops = &ubifs_file_address_operations;
124 inode->i_op = &ubifs_file_inode_operations;
125 inode->i_fop = &ubifs_file_operations;
126 break;
127 case S_IFDIR:
128 inode->i_op = &ubifs_dir_inode_operations;
129 inode->i_fop = &ubifs_dir_operations;
130 inode->i_size = ui->ui_size = UBIFS_INO_NODE_SZ;
131 break;
132 case S_IFLNK:
133 inode->i_op = &ubifs_symlink_inode_operations;
134 break;
135 case S_IFSOCK:
136 case S_IFIFO:
137 case S_IFBLK:
138 case S_IFCHR:
139 inode->i_op = &ubifs_file_inode_operations;
140 break;
141 default:
142 BUG();
143 }
144
145 ui->flags = inherit_flags(dir, mode);
146 ubifs_set_inode_flags(inode);
147 if (S_ISREG(mode))
148 ui->compr_type = c->default_compr;
149 else
150 ui->compr_type = UBIFS_COMPR_NONE;
151 ui->synced_i_size = 0;
152
153 spin_lock(&c->cnt_lock);
154 /* Inode number overflow is currently not supported */
155 if (c->highest_inum >= INUM_WARN_WATERMARK) {
156 if (c->highest_inum >= INUM_WATERMARK) {
157 spin_unlock(&c->cnt_lock);
158 ubifs_err("out of inode numbers");
159 make_bad_inode(inode);
160 iput(inode);
161 return ERR_PTR(-EINVAL);
162 }
163 ubifs_warn("running out of inode numbers (current %lu, max %d)",
164 c->highest_inum, INUM_WATERMARK);
165 }
166
167 inode->i_ino = ++c->highest_inum;
168 inode->i_generation = ++c->vfs_gen;
169 /*
170 * The creation sequence number remains with this inode for its
171 * lifetime. All nodes for this inode have a greater sequence number,
172 * and so it is possible to distinguish obsolete nodes belonging to a
173 * previous incarnation of the same inode number - for example, for the
174 * purpose of rebuilding the index.
175 */
176 ui->creat_sqnum = ++c->max_sqnum;
177 spin_unlock(&c->cnt_lock);
178 return inode;
179}
180
181#ifdef CONFIG_UBIFS_FS_DEBUG
182
183static int dbg_check_name(struct ubifs_dent_node *dent, struct qstr *nm)
184{
185 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
186 return 0;
187 if (le16_to_cpu(dent->nlen) != nm->len)
188 return -EINVAL;
189 if (memcmp(dent->name, nm->name, nm->len))
190 return -EINVAL;
191 return 0;
192}
193
194#else
195
196#define dbg_check_name(dent, nm) 0
197
198#endif
199
200static struct dentry *ubifs_lookup(struct inode *dir, struct dentry *dentry,
201 struct nameidata *nd)
202{
203 int err;
204 union ubifs_key key;
205 struct inode *inode = NULL;
206 struct ubifs_dent_node *dent;
207 struct ubifs_info *c = dir->i_sb->s_fs_info;
208
209 dbg_gen("'%.*s' in dir ino %lu",
210 dentry->d_name.len, dentry->d_name.name, dir->i_ino);
211
212 if (dentry->d_name.len > UBIFS_MAX_NLEN)
213 return ERR_PTR(-ENAMETOOLONG);
214
215 dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
216 if (!dent)
217 return ERR_PTR(-ENOMEM);
218
219 dent_key_init(c, &key, dir->i_ino, &dentry->d_name);
220
221 err = ubifs_tnc_lookup_nm(c, &key, dent, &dentry->d_name);
222 if (err) {
223 /*
224 * Do not hash the direntry if parent 'i_nlink' is zero, because
225 * this has side-effects - '->delete_inode()' call will not be
226 * called for the parent orphan inode, because 'd_count' of its
227 * direntry will stay 1 (it'll be negative direntry I guess)
228 * and prevent 'iput_final()' until the dentry is destroyed due
229 * to unmount or memory pressure.
230 */
231 if (err == -ENOENT && dir->i_nlink != 0) {
232 dbg_gen("not found");
233 goto done;
234 }
235 goto out;
236 }
237
238 if (dbg_check_name(dent, &dentry->d_name)) {
239 err = -EINVAL;
240 goto out;
241 }
242
243 inode = ubifs_iget(dir->i_sb, le64_to_cpu(dent->inum));
244 if (IS_ERR(inode)) {
245 /*
246 * This should not happen. Probably the file-system needs
247 * checking.
248 */
249 err = PTR_ERR(inode);
250 ubifs_err("dead directory entry '%.*s', error %d",
251 dentry->d_name.len, dentry->d_name.name, err);
252 ubifs_ro_mode(c, err);
253 goto out;
254 }
255
256done:
257 kfree(dent);
258 /*
259 * Note, d_splice_alias() would be required instead if we supported
260 * NFS.
261 */
262 d_add(dentry, inode);
263 return NULL;
264
265out:
266 kfree(dent);
267 return ERR_PTR(err);
268}
269
270static int ubifs_create(struct inode *dir, struct dentry *dentry, int mode,
271 struct nameidata *nd)
272{
273 struct inode *inode;
274 struct ubifs_info *c = dir->i_sb->s_fs_info;
275 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
276 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
277 .dirtied_ino = 1 };
278 struct ubifs_inode *dir_ui = ubifs_inode(dir);
279
280 /*
281 * Budget request settings: new inode, new direntry, changing the
282 * parent directory inode.
283 */
284
285 dbg_gen("dent '%.*s', mode %#x in dir ino %lu",
286 dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino);
287
288 err = ubifs_budget_space(c, &req);
289 if (err)
290 return err;
291
292 inode = ubifs_new_inode(c, dir, mode);
293 if (IS_ERR(inode)) {
294 err = PTR_ERR(inode);
295 goto out_budg;
296 }
297
298 mutex_lock(&dir_ui->ui_mutex);
299 dir->i_size += sz_change;
300 dir_ui->ui_size = dir->i_size;
301 dir->i_mtime = dir->i_ctime = inode->i_ctime;
302 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
303 if (err)
304 goto out_cancel;
305 mutex_unlock(&dir_ui->ui_mutex);
306
307 ubifs_release_budget(c, &req);
308 insert_inode_hash(inode);
309 d_instantiate(dentry, inode);
310 return 0;
311
312out_cancel:
313 dir->i_size -= sz_change;
314 dir_ui->ui_size = dir->i_size;
315 mutex_unlock(&dir_ui->ui_mutex);
316 make_bad_inode(inode);
317 iput(inode);
318out_budg:
319 ubifs_release_budget(c, &req);
320 ubifs_err("cannot create regular file, error %d", err);
321 return err;
322}
323
324/**
325 * vfs_dent_type - get VFS directory entry type.
326 * @type: UBIFS directory entry type
327 *
328 * This function converts UBIFS directory entry type into VFS directory entry
329 * type.
330 */
331static unsigned int vfs_dent_type(uint8_t type)
332{
333 switch (type) {
334 case UBIFS_ITYPE_REG:
335 return DT_REG;
336 case UBIFS_ITYPE_DIR:
337 return DT_DIR;
338 case UBIFS_ITYPE_LNK:
339 return DT_LNK;
340 case UBIFS_ITYPE_BLK:
341 return DT_BLK;
342 case UBIFS_ITYPE_CHR:
343 return DT_CHR;
344 case UBIFS_ITYPE_FIFO:
345 return DT_FIFO;
346 case UBIFS_ITYPE_SOCK:
347 return DT_SOCK;
348 default:
349 BUG();
350 }
351 return 0;
352}
353
354/*
355 * The classical Unix view for directory is that it is a linear array of
356 * (name, inode number) entries. Linux/VFS assumes this model as well.
357 * Particularly, 'readdir()' call wants us to return a directory entry offset
358 * which later may be used to continue 'readdir()'ing the directory or to
359 * 'seek()' to that specific direntry. Obviously UBIFS does not really fit this
360 * model because directory entries are identified by keys, which may collide.
361 *
362 * UBIFS uses directory entry hash value for directory offsets, so
363 * 'seekdir()'/'telldir()' may not always work because of possible key
364 * collisions. But UBIFS guarantees that consecutive 'readdir()' calls work
365 * properly by means of saving full directory entry name in the private field
366 * of the file description object.
367 *
368 * This means that UBIFS cannot support NFS which requires full
369 * 'seekdir()'/'telldir()' support.
370 */
371static int ubifs_readdir(struct file *file, void *dirent, filldir_t filldir)
372{
373 int err, over = 0;
374 struct qstr nm;
375 union ubifs_key key;
376 struct ubifs_dent_node *dent;
377 struct inode *dir = file->f_path.dentry->d_inode;
378 struct ubifs_info *c = dir->i_sb->s_fs_info;
379
380 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
381
382 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
383 /*
384 * The directory was seek'ed to a senseless position or there
385 * are no more entries.
386 */
387 return 0;
388
389 /* File positions 0 and 1 correspond to "." and ".." */
390 if (file->f_pos == 0) {
391 ubifs_assert(!file->private_data);
392 over = filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR);
393 if (over)
394 return 0;
395 file->f_pos = 1;
396 }
397
398 if (file->f_pos == 1) {
399 ubifs_assert(!file->private_data);
400 over = filldir(dirent, "..", 2, 1,
401 parent_ino(file->f_path.dentry), DT_DIR);
402 if (over)
403 return 0;
404
405 /* Find the first entry in TNC and save it */
406 lowest_dent_key(c, &key, dir->i_ino);
407 nm.name = NULL;
408 dent = ubifs_tnc_next_ent(c, &key, &nm);
409 if (IS_ERR(dent)) {
410 err = PTR_ERR(dent);
411 goto out;
412 }
413
414 file->f_pos = key_hash_flash(c, &dent->key);
415 file->private_data = dent;
416 }
417
418 dent = file->private_data;
419 if (!dent) {
420 /*
421 * The directory was seek'ed to and is now readdir'ed.
422 * Find the entry corresponding to @file->f_pos or the
423 * closest one.
424 */
425 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
426 nm.name = NULL;
427 dent = ubifs_tnc_next_ent(c, &key, &nm);
428 if (IS_ERR(dent)) {
429 err = PTR_ERR(dent);
430 goto out;
431 }
432 file->f_pos = key_hash_flash(c, &dent->key);
433 file->private_data = dent;
434 }
435
436 while (1) {
437 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
438 dent->name, le64_to_cpu(dent->inum),
439 key_hash_flash(c, &dent->key));
440 ubifs_assert(dent->ch.sqnum > ubifs_inode(dir)->creat_sqnum);
441
442 nm.len = le16_to_cpu(dent->nlen);
443 over = filldir(dirent, dent->name, nm.len, file->f_pos,
444 le64_to_cpu(dent->inum),
445 vfs_dent_type(dent->type));
446 if (over)
447 return 0;
448
449 /* Switch to the next entry */
450 key_read(c, &dent->key, &key);
451 nm.name = dent->name;
452 dent = ubifs_tnc_next_ent(c, &key, &nm);
453 if (IS_ERR(dent)) {
454 err = PTR_ERR(dent);
455 goto out;
456 }
457
458 kfree(file->private_data);
459 file->f_pos = key_hash_flash(c, &dent->key);
460 file->private_data = dent;
461 cond_resched();
462 }
463
464out:
465 if (err != -ENOENT) {
466 ubifs_err("cannot find next direntry, error %d", err);
467 return err;
468 }
469
470 kfree(file->private_data);
471 file->private_data = NULL;
472 file->f_pos = 2;
473 return 0;
474}
475
476/* If a directory is seeked, we have to free saved readdir() state */
477static loff_t ubifs_dir_llseek(struct file *file, loff_t offset, int origin)
478{
479 kfree(file->private_data);
480 file->private_data = NULL;
481 return generic_file_llseek(file, offset, origin);
482}
483
484/* Free saved readdir() state when the directory is closed */
485static int ubifs_dir_release(struct inode *dir, struct file *file)
486{
487 kfree(file->private_data);
488 file->private_data = NULL;
489 return 0;
490}
491
492/**
493 * lock_2_inodes - lock two UBIFS inodes.
494 * @inode1: first inode
495 * @inode2: second inode
496 */
497static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
498{
499 if (inode1->i_ino < inode2->i_ino) {
500 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_2);
501 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_3);
502 } else {
503 mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
504 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_3);
505 }
506}
507
508/**
509 * unlock_2_inodes - unlock two UBIFS inodes inodes.
510 * @inode1: first inode
511 * @inode2: second inode
512 */
513static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
514{
515 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
516 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
517}
518
519static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
520 struct dentry *dentry)
521{
522 struct ubifs_info *c = dir->i_sb->s_fs_info;
523 struct inode *inode = old_dentry->d_inode;
524 struct ubifs_inode *ui = ubifs_inode(inode);
525 struct ubifs_inode *dir_ui = ubifs_inode(dir);
526 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
527 struct ubifs_budget_req req = { .new_dent = 1, .dirtied_ino = 2,
528 .dirtied_ino_d = ui->data_len };
529
530 /*
531 * Budget request settings: new direntry, changing the target inode,
532 * changing the parent inode.
533 */
534
535 dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu",
536 dentry->d_name.len, dentry->d_name.name, inode->i_ino,
537 inode->i_nlink, dir->i_ino);
538 err = dbg_check_synced_i_size(inode);
539 if (err)
540 return err;
541
542 err = ubifs_budget_space(c, &req);
543 if (err)
544 return err;
545
546 lock_2_inodes(dir, inode);
547 inc_nlink(inode);
548 atomic_inc(&inode->i_count);
549 inode->i_ctime = ubifs_current_time(inode);
550 dir->i_size += sz_change;
551 dir_ui->ui_size = dir->i_size;
552 dir->i_mtime = dir->i_ctime = inode->i_ctime;
553 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
554 if (err)
555 goto out_cancel;
556 unlock_2_inodes(dir, inode);
557
558 ubifs_release_budget(c, &req);
559 d_instantiate(dentry, inode);
560 return 0;
561
562out_cancel:
563 dir->i_size -= sz_change;
564 dir_ui->ui_size = dir->i_size;
565 drop_nlink(inode);
566 unlock_2_inodes(dir, inode);
567 ubifs_release_budget(c, &req);
568 iput(inode);
569 return err;
570}
571
572static int ubifs_unlink(struct inode *dir, struct dentry *dentry)
573{
574 struct ubifs_info *c = dir->i_sb->s_fs_info;
575 struct inode *inode = dentry->d_inode;
576 struct ubifs_inode *dir_ui = ubifs_inode(dir);
577 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
578 int err, budgeted = 1;
579 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
580
581 /*
582 * Budget request settings: deletion direntry, deletion inode (+1 for
583 * @dirtied_ino), changing the parent directory inode. If budgeting
584 * fails, go ahead anyway because we have extra space reserved for
585 * deletions.
586 */
587
588 dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu",
589 dentry->d_name.len, dentry->d_name.name, inode->i_ino,
590 inode->i_nlink, dir->i_ino);
591 err = dbg_check_synced_i_size(inode);
592 if (err)
593 return err;
594
595 err = ubifs_budget_space(c, &req);
596 if (err) {
597 if (err != -ENOSPC)
598 return err;
599 err = 0;
600 budgeted = 0;
601 }
602
603 lock_2_inodes(dir, inode);
604 inode->i_ctime = ubifs_current_time(dir);
605 drop_nlink(inode);
606 dir->i_size -= sz_change;
607 dir_ui->ui_size = dir->i_size;
608 dir->i_mtime = dir->i_ctime = inode->i_ctime;
609 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
610 if (err)
611 goto out_cancel;
612 unlock_2_inodes(dir, inode);
613
614 if (budgeted)
615 ubifs_release_budget(c, &req);
616 else {
617 /* We've deleted something - clean the "no space" flags */
618 c->nospace = c->nospace_rp = 0;
619 smp_wmb();
620 }
621 return 0;
622
623out_cancel:
624 dir->i_size += sz_change;
625 dir_ui->ui_size = dir->i_size;
626 inc_nlink(inode);
627 unlock_2_inodes(dir, inode);
628 if (budgeted)
629 ubifs_release_budget(c, &req);
630 return err;
631}
632
633/**
634 * check_dir_empty - check if a directory is empty or not.
635 * @c: UBIFS file-system description object
636 * @dir: VFS inode object of the directory to check
637 *
638 * This function checks if directory @dir is empty. Returns zero if the
639 * directory is empty, %-ENOTEMPTY if it is not, and other negative error codes
640 * in case of of errors.
641 */
642static int check_dir_empty(struct ubifs_info *c, struct inode *dir)
643{
644 struct qstr nm = { .name = NULL };
645 struct ubifs_dent_node *dent;
646 union ubifs_key key;
647 int err;
648
649 lowest_dent_key(c, &key, dir->i_ino);
650 dent = ubifs_tnc_next_ent(c, &key, &nm);
651 if (IS_ERR(dent)) {
652 err = PTR_ERR(dent);
653 if (err == -ENOENT)
654 err = 0;
655 } else {
656 kfree(dent);
657 err = -ENOTEMPTY;
658 }
659 return err;
660}
661
662static int ubifs_rmdir(struct inode *dir, struct dentry *dentry)
663{
664 struct ubifs_info *c = dir->i_sb->s_fs_info;
665 struct inode *inode = dentry->d_inode;
666 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
667 int err, budgeted = 1;
668 struct ubifs_inode *dir_ui = ubifs_inode(dir);
669 struct ubifs_budget_req req = { .mod_dent = 1, .dirtied_ino = 2 };
670
671 /*
672 * Budget request settings: deletion direntry, deletion inode and
673 * changing the parent inode. If budgeting fails, go ahead anyway
674 * because we have extra space reserved for deletions.
675 */
676
677 dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len,
678 dentry->d_name.name, inode->i_ino, dir->i_ino);
679
680 err = check_dir_empty(c, dentry->d_inode);
681 if (err)
682 return err;
683
684 err = ubifs_budget_space(c, &req);
685 if (err) {
686 if (err != -ENOSPC)
687 return err;
688 budgeted = 0;
689 }
690
691 lock_2_inodes(dir, inode);
692 inode->i_ctime = ubifs_current_time(dir);
693 clear_nlink(inode);
694 drop_nlink(dir);
695 dir->i_size -= sz_change;
696 dir_ui->ui_size = dir->i_size;
697 dir->i_mtime = dir->i_ctime = inode->i_ctime;
698 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 1, 0);
699 if (err)
700 goto out_cancel;
701 unlock_2_inodes(dir, inode);
702
703 if (budgeted)
704 ubifs_release_budget(c, &req);
705 else {
706 /* We've deleted something - clean the "no space" flags */
707 c->nospace = c->nospace_rp = 0;
708 smp_wmb();
709 }
710 return 0;
711
712out_cancel:
713 dir->i_size += sz_change;
714 dir_ui->ui_size = dir->i_size;
715 inc_nlink(dir);
716 inc_nlink(inode);
717 inc_nlink(inode);
718 unlock_2_inodes(dir, inode);
719 if (budgeted)
720 ubifs_release_budget(c, &req);
721 return err;
722}
723
724static int ubifs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
725{
726 struct inode *inode;
727 struct ubifs_inode *dir_ui = ubifs_inode(dir);
728 struct ubifs_info *c = dir->i_sb->s_fs_info;
729 int err, sz_change = CALC_DENT_SIZE(dentry->d_name.len);
730 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
731 .dirtied_ino_d = 1 };
732
733 /*
734 * Budget request settings: new inode, new direntry and changing parent
735 * directory inode.
736 */
737
738 dbg_gen("dent '%.*s', mode %#x in dir ino %lu",
739 dentry->d_name.len, dentry->d_name.name, mode, dir->i_ino);
740
741 err = ubifs_budget_space(c, &req);
742 if (err)
743 return err;
744
745 inode = ubifs_new_inode(c, dir, S_IFDIR | mode);
746 if (IS_ERR(inode)) {
747 err = PTR_ERR(inode);
748 goto out_budg;
749 }
750
751 mutex_lock(&dir_ui->ui_mutex);
752 insert_inode_hash(inode);
753 inc_nlink(inode);
754 inc_nlink(dir);
755 dir->i_size += sz_change;
756 dir_ui->ui_size = dir->i_size;
757 dir->i_mtime = dir->i_ctime = inode->i_ctime;
758 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
759 if (err) {
760 ubifs_err("cannot create directory, error %d", err);
761 goto out_cancel;
762 }
763 mutex_unlock(&dir_ui->ui_mutex);
764
765 ubifs_release_budget(c, &req);
766 d_instantiate(dentry, inode);
767 return 0;
768
769out_cancel:
770 dir->i_size -= sz_change;
771 dir_ui->ui_size = dir->i_size;
772 drop_nlink(dir);
773 mutex_unlock(&dir_ui->ui_mutex);
774 make_bad_inode(inode);
775 iput(inode);
776out_budg:
777 ubifs_release_budget(c, &req);
778 return err;
779}
780
781static int ubifs_mknod(struct inode *dir, struct dentry *dentry,
782 int mode, dev_t rdev)
783{
784 struct inode *inode;
785 struct ubifs_inode *ui;
786 struct ubifs_inode *dir_ui = ubifs_inode(dir);
787 struct ubifs_info *c = dir->i_sb->s_fs_info;
788 union ubifs_dev_desc *dev = NULL;
789 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
790 int err, devlen = 0;
791 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
792 .new_ino_d = devlen, .dirtied_ino = 1 };
793
794 /*
795 * Budget request settings: new inode, new direntry and changing parent
796 * directory inode.
797 */
798
799 dbg_gen("dent '%.*s' in dir ino %lu",
800 dentry->d_name.len, dentry->d_name.name, dir->i_ino);
801
802 if (!new_valid_dev(rdev))
803 return -EINVAL;
804
805 if (S_ISBLK(mode) || S_ISCHR(mode)) {
806 dev = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
807 if (!dev)
808 return -ENOMEM;
809 devlen = ubifs_encode_dev(dev, rdev);
810 }
811
812 err = ubifs_budget_space(c, &req);
813 if (err) {
814 kfree(dev);
815 return err;
816 }
817
818 inode = ubifs_new_inode(c, dir, mode);
819 if (IS_ERR(inode)) {
820 kfree(dev);
821 err = PTR_ERR(inode);
822 goto out_budg;
823 }
824
825 init_special_inode(inode, inode->i_mode, rdev);
826 inode->i_size = ubifs_inode(inode)->ui_size = devlen;
827 ui = ubifs_inode(inode);
828 ui->data = dev;
829 ui->data_len = devlen;
830
831 mutex_lock(&dir_ui->ui_mutex);
832 dir->i_size += sz_change;
833 dir_ui->ui_size = dir->i_size;
834 dir->i_mtime = dir->i_ctime = inode->i_ctime;
835 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
836 if (err)
837 goto out_cancel;
838 mutex_unlock(&dir_ui->ui_mutex);
839
840 ubifs_release_budget(c, &req);
841 insert_inode_hash(inode);
842 d_instantiate(dentry, inode);
843 return 0;
844
845out_cancel:
846 dir->i_size -= sz_change;
847 dir_ui->ui_size = dir->i_size;
848 mutex_unlock(&dir_ui->ui_mutex);
849 make_bad_inode(inode);
850 iput(inode);
851out_budg:
852 ubifs_release_budget(c, &req);
853 return err;
854}
855
856static int ubifs_symlink(struct inode *dir, struct dentry *dentry,
857 const char *symname)
858{
859 struct inode *inode;
860 struct ubifs_inode *ui;
861 struct ubifs_inode *dir_ui = ubifs_inode(dir);
862 struct ubifs_info *c = dir->i_sb->s_fs_info;
863 int err, len = strlen(symname);
864 int sz_change = CALC_DENT_SIZE(dentry->d_name.len);
865 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
866 .new_ino_d = len, .dirtied_ino = 1 };
867
868 /*
869 * Budget request settings: new inode, new direntry and changing parent
870 * directory inode.
871 */
872
873 dbg_gen("dent '%.*s', target '%s' in dir ino %lu", dentry->d_name.len,
874 dentry->d_name.name, symname, dir->i_ino);
875
876 if (len > UBIFS_MAX_INO_DATA)
877 return -ENAMETOOLONG;
878
879 err = ubifs_budget_space(c, &req);
880 if (err)
881 return err;
882
883 inode = ubifs_new_inode(c, dir, S_IFLNK | S_IRWXUGO);
884 if (IS_ERR(inode)) {
885 err = PTR_ERR(inode);
886 goto out_budg;
887 }
888
889 ui = ubifs_inode(inode);
890 ui->data = kmalloc(len + 1, GFP_NOFS);
891 if (!ui->data) {
892 err = -ENOMEM;
893 goto out_inode;
894 }
895
896 memcpy(ui->data, symname, len);
897 ((char *)ui->data)[len] = '\0';
898 /*
899 * The terminating zero byte is not written to the flash media and it
900 * is put just to make later in-memory string processing simpler. Thus,
901 * data length is @len, not @len + %1.
902 */
903 ui->data_len = len;
904 inode->i_size = ubifs_inode(inode)->ui_size = len;
905
906 mutex_lock(&dir_ui->ui_mutex);
907 dir->i_size += sz_change;
908 dir_ui->ui_size = dir->i_size;
909 dir->i_mtime = dir->i_ctime = inode->i_ctime;
910 err = ubifs_jnl_update(c, dir, &dentry->d_name, inode, 0, 0);
911 if (err)
912 goto out_cancel;
913 mutex_unlock(&dir_ui->ui_mutex);
914
915 ubifs_release_budget(c, &req);
916 insert_inode_hash(inode);
917 d_instantiate(dentry, inode);
918 return 0;
919
920out_cancel:
921 dir->i_size -= sz_change;
922 dir_ui->ui_size = dir->i_size;
923 mutex_unlock(&dir_ui->ui_mutex);
924out_inode:
925 make_bad_inode(inode);
926 iput(inode);
927out_budg:
928 ubifs_release_budget(c, &req);
929 return err;
930}
931
932/**
933 * lock_3_inodes - lock three UBIFS inodes for rename.
934 * @inode1: first inode
935 * @inode2: second inode
936 * @inode3: third inode
937 *
938 * For 'ubifs_rename()', @inode1 may be the same as @inode2 whereas @inode3 may
939 * be null.
940 */
941static void lock_3_inodes(struct inode *inode1, struct inode *inode2,
942 struct inode *inode3)
943{
944 struct inode *i1, *i2, *i3;
945
946 if (!inode3) {
947 if (inode1 != inode2) {
948 lock_2_inodes(inode1, inode2);
949 return;
950 }
951 mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
952 return;
953 }
954
955 if (inode1 == inode2) {
956 lock_2_inodes(inode1, inode3);
957 return;
958 }
959
960 /* 3 different inodes */
961 if (inode1 < inode2) {
962 i3 = inode2;
963 if (inode1 < inode3) {
964 i1 = inode1;
965 i2 = inode3;
966 } else {
967 i1 = inode3;
968 i2 = inode1;
969 }
970 } else {
971 i3 = inode1;
972 if (inode2 < inode3) {
973 i1 = inode2;
974 i2 = inode3;
975 } else {
976 i1 = inode3;
977 i2 = inode2;
978 }
979 }
980 mutex_lock_nested(&ubifs_inode(i1)->ui_mutex, WB_MUTEX_1);
981 lock_2_inodes(i2, i3);
982}
983
984/**
985 * unlock_3_inodes - unlock three UBIFS inodes for rename.
986 * @inode1: first inode
987 * @inode2: second inode
988 * @inode3: third inode
989 */
990static void unlock_3_inodes(struct inode *inode1, struct inode *inode2,
991 struct inode *inode3)
992{
993 mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
994 if (inode1 != inode2)
995 mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
996 if (inode3)
997 mutex_unlock(&ubifs_inode(inode3)->ui_mutex);
998}
999
1000static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry,
1001 struct inode *new_dir, struct dentry *new_dentry)
1002{
1003 struct ubifs_info *c = old_dir->i_sb->s_fs_info;
1004 struct inode *old_inode = old_dentry->d_inode;
1005 struct inode *new_inode = new_dentry->d_inode;
1006 struct ubifs_inode *old_inode_ui = ubifs_inode(old_inode);
1007 int err, release, sync = 0, move = (new_dir != old_dir);
1008 int is_dir = S_ISDIR(old_inode->i_mode);
1009 int unlink = !!new_inode;
1010 int new_sz = CALC_DENT_SIZE(new_dentry->d_name.len);
1011 int old_sz = CALC_DENT_SIZE(old_dentry->d_name.len);
1012 struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
1013 .dirtied_ino = 3 };
1014 struct ubifs_budget_req ino_req = { .dirtied_ino = 1,
1015 .dirtied_ino_d = old_inode_ui->data_len };
1016 struct timespec time;
1017
1018 /*
1019 * Budget request settings: deletion direntry, new direntry, removing
1020 * the old inode, and changing old and new parent directory inodes.
1021 *
1022 * However, this operation also marks the target inode as dirty and
1023 * does not write it, so we allocate budget for the target inode
1024 * separately.
1025 */
1026
1027 dbg_gen("dent '%.*s' ino %lu in dir ino %lu to dent '%.*s' in "
1028 "dir ino %lu", old_dentry->d_name.len, old_dentry->d_name.name,
1029 old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len,
1030 new_dentry->d_name.name, new_dir->i_ino);
1031
1032 if (unlink && is_dir) {
1033 err = check_dir_empty(c, new_inode);
1034 if (err)
1035 return err;
1036 }
1037
1038 err = ubifs_budget_space(c, &req);
1039 if (err)
1040 return err;
1041 err = ubifs_budget_space(c, &ino_req);
1042 if (err) {
1043 ubifs_release_budget(c, &req);
1044 return err;
1045 }
1046
1047 lock_3_inodes(old_dir, new_dir, new_inode);
1048
1049 /*
1050 * Like most other Unix systems, set the @i_ctime for inodes on a
1051 * rename.
1052 */
1053 time = ubifs_current_time(old_dir);
1054 old_inode->i_ctime = time;
1055
1056 /* We must adjust parent link count when renaming directories */
1057 if (is_dir) {
1058 if (move) {
1059 /*
1060 * @old_dir loses a link because we are moving
1061 * @old_inode to a different directory.
1062 */
1063 drop_nlink(old_dir);
1064 /*
1065 * @new_dir only gains a link if we are not also
1066 * overwriting an existing directory.
1067 */
1068 if (!unlink)
1069 inc_nlink(new_dir);
1070 } else {
1071 /*
1072 * @old_inode is not moving to a different directory,
1073 * but @old_dir still loses a link if we are
1074 * overwriting an existing directory.
1075 */
1076 if (unlink)
1077 drop_nlink(old_dir);
1078 }
1079 }
1080
1081 old_dir->i_size -= old_sz;
1082 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1083 old_dir->i_mtime = old_dir->i_ctime = time;
1084 new_dir->i_mtime = new_dir->i_ctime = time;
1085
1086 /*
1087 * And finally, if we unlinked a direntry which happened to have the
1088 * same name as the moved direntry, we have to decrement @i_nlink of
1089 * the unlinked inode and change its ctime.
1090 */
1091 if (unlink) {
1092 /*
1093 * Directories cannot have hard-links, so if this is a
1094 * directory, decrement its @i_nlink twice because an empty
1095 * directory has @i_nlink 2.
1096 */
1097 if (is_dir)
1098 drop_nlink(new_inode);
1099 new_inode->i_ctime = time;
1100 drop_nlink(new_inode);
1101 } else {
1102 new_dir->i_size += new_sz;
1103 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1104 }
1105
1106 /*
1107 * Do not ask 'ubifs_jnl_rename()' to flush write-buffer if @old_inode
1108 * is dirty, because this will be done later on at the end of
1109 * 'ubifs_rename()'.
1110 */
1111 if (IS_SYNC(old_inode)) {
1112 sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
1113 if (unlink && IS_SYNC(new_inode))
1114 sync = 1;
1115 }
1116 err = ubifs_jnl_rename(c, old_dir, old_dentry, new_dir, new_dentry,
1117 sync);
1118 if (err)
1119 goto out_cancel;
1120
1121 unlock_3_inodes(old_dir, new_dir, new_inode);
1122 ubifs_release_budget(c, &req);
1123
1124 mutex_lock(&old_inode_ui->ui_mutex);
1125 release = old_inode_ui->dirty;
1126 mark_inode_dirty_sync(old_inode);
1127 mutex_unlock(&old_inode_ui->ui_mutex);
1128
1129 if (release)
1130 ubifs_release_budget(c, &ino_req);
1131 if (IS_SYNC(old_inode))
1132 err = old_inode->i_sb->s_op->write_inode(old_inode, 1);
1133 return err;
1134
1135out_cancel:
1136 if (unlink) {
1137 if (is_dir)
1138 inc_nlink(new_inode);
1139 inc_nlink(new_inode);
1140 } else {
1141 new_dir->i_size -= new_sz;
1142 ubifs_inode(new_dir)->ui_size = new_dir->i_size;
1143 }
1144 old_dir->i_size += old_sz;
1145 ubifs_inode(old_dir)->ui_size = old_dir->i_size;
1146 if (is_dir) {
1147 if (move) {
1148 inc_nlink(old_dir);
1149 if (!unlink)
1150 drop_nlink(new_dir);
1151 } else {
1152 if (unlink)
1153 inc_nlink(old_dir);
1154 }
1155 }
1156 unlock_3_inodes(old_dir, new_dir, new_inode);
1157 ubifs_release_budget(c, &ino_req);
1158 ubifs_release_budget(c, &req);
1159 return err;
1160}
1161
1162int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1163 struct kstat *stat)
1164{
1165 loff_t size;
1166 struct inode *inode = dentry->d_inode;
1167 struct ubifs_inode *ui = ubifs_inode(inode);
1168
1169 mutex_lock(&ui->ui_mutex);
1170 stat->dev = inode->i_sb->s_dev;
1171 stat->ino = inode->i_ino;
1172 stat->mode = inode->i_mode;
1173 stat->nlink = inode->i_nlink;
1174 stat->uid = inode->i_uid;
1175 stat->gid = inode->i_gid;
1176 stat->rdev = inode->i_rdev;
1177 stat->atime = inode->i_atime;
1178 stat->mtime = inode->i_mtime;
1179 stat->ctime = inode->i_ctime;
1180 stat->blksize = UBIFS_BLOCK_SIZE;
1181 stat->size = ui->ui_size;
1182
1183 /*
1184 * Unfortunately, the 'stat()' system call was designed for block
1185 * device based file systems, and it is not appropriate for UBIFS,
1186 * because UBIFS does not have notion of "block". For example, it is
1187 * difficult to tell how many block a directory takes - it actually
1188 * takes less than 300 bytes, but we have to round it to block size,
1189 * which introduces large mistake. This makes utilities like 'du' to
1190 * report completely senseless numbers. This is the reason why UBIFS
1191 * goes the same way as JFFS2 - it reports zero blocks for everything
1192 * but regular files, which makes more sense than reporting completely
1193 * wrong sizes.
1194 */
1195 if (S_ISREG(inode->i_mode)) {
1196 size = ui->xattr_size;
1197 size += stat->size;
1198 size = ALIGN(size, UBIFS_BLOCK_SIZE);
1199 /*
1200 * Note, user-space expects 512-byte blocks count irrespectively
1201 * of what was reported in @stat->size.
1202 */
1203 stat->blocks = size >> 9;
1204 } else
1205 stat->blocks = 0;
1206 mutex_unlock(&ui->ui_mutex);
1207 return 0;
1208}
1209
1210struct inode_operations ubifs_dir_inode_operations = {
1211 .lookup = ubifs_lookup,
1212 .create = ubifs_create,
1213 .link = ubifs_link,
1214 .symlink = ubifs_symlink,
1215 .unlink = ubifs_unlink,
1216 .mkdir = ubifs_mkdir,
1217 .rmdir = ubifs_rmdir,
1218 .mknod = ubifs_mknod,
1219 .rename = ubifs_rename,
1220 .setattr = ubifs_setattr,
1221 .getattr = ubifs_getattr,
1222#ifdef CONFIG_UBIFS_FS_XATTR
1223 .setxattr = ubifs_setxattr,
1224 .getxattr = ubifs_getxattr,
1225 .listxattr = ubifs_listxattr,
1226 .removexattr = ubifs_removexattr,
1227#endif
1228};
1229
1230struct file_operations ubifs_dir_operations = {
1231 .llseek = ubifs_dir_llseek,
1232 .release = ubifs_dir_release,
1233 .read = generic_read_dir,
1234 .readdir = ubifs_readdir,
1235 .fsync = ubifs_fsync,
1236 .unlocked_ioctl = ubifs_ioctl,
1237#ifdef CONFIG_COMPAT
1238 .compat_ioctl = ubifs_compat_ioctl,
1239#endif
1240};
diff --git a/fs/ubifs/file.c b/fs/ubifs/file.c
new file mode 100644
index 000000000000..005a3b854d96
--- /dev/null
+++ b/fs/ubifs/file.c
@@ -0,0 +1,1275 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements VFS file and inode operations of regular files, device
25 * nodes and symlinks as well as address space operations.
26 *
27 * UBIFS uses 2 page flags: PG_private and PG_checked. PG_private is set if the
28 * page is dirty and is used for budgeting purposes - dirty pages should not be
29 * budgeted. The PG_checked flag is set if full budgeting is required for the
30 * page e.g., when it corresponds to a file hole or it is just beyond the file
31 * size. The budgeting is done in 'ubifs_write_begin()', because it is OK to
32 * fail in this function, and the budget is released in 'ubifs_write_end()'. So
33 * the PG_private and PG_checked flags carry the information about how the page
34 * was budgeted, to make it possible to release the budget properly.
35 *
36 * A thing to keep in mind: inode's 'i_mutex' is locked in most VFS operations
37 * we implement. However, this is not true for '->writepage()', which might be
38 * called with 'i_mutex' unlocked. For example, when pdflush is performing
39 * write-back, it calls 'writepage()' with unlocked 'i_mutex', although the
40 * inode has 'I_LOCK' flag in this case. At "normal" work-paths 'i_mutex' is
41 * locked in '->writepage', e.g. in "sys_write -> alloc_pages -> direct reclaim
42 * path'. So, in '->writepage()' we are only guaranteed that the page is
43 * locked.
44 *
45 * Similarly, 'i_mutex' does not have to be locked in readpage(), e.g.,
46 * readahead path does not have it locked ("sys_read -> generic_file_aio_read
47 * -> ondemand_readahead -> readpage"). In case of readahead, 'I_LOCK' flag is
48 * not set as well. However, UBIFS disables readahead.
49 *
50 * This, for example means that there might be 2 concurrent '->writepage()'
51 * calls for the same inode, but different inode dirty pages.
52 */
53
54#include "ubifs.h"
55#include <linux/mount.h>
56
57static int read_block(struct inode *inode, void *addr, unsigned int block,
58 struct ubifs_data_node *dn)
59{
60 struct ubifs_info *c = inode->i_sb->s_fs_info;
61 int err, len, out_len;
62 union ubifs_key key;
63 unsigned int dlen;
64
65 data_key_init(c, &key, inode->i_ino, block);
66 err = ubifs_tnc_lookup(c, &key, dn);
67 if (err) {
68 if (err == -ENOENT)
69 /* Not found, so it must be a hole */
70 memset(addr, 0, UBIFS_BLOCK_SIZE);
71 return err;
72 }
73
74 ubifs_assert(dn->ch.sqnum > ubifs_inode(inode)->creat_sqnum);
75
76 len = le32_to_cpu(dn->size);
77 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
78 goto dump;
79
80 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
81 out_len = UBIFS_BLOCK_SIZE;
82 err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
83 le16_to_cpu(dn->compr_type));
84 if (err || len != out_len)
85 goto dump;
86
87 /*
88 * Data length can be less than a full block, even for blocks that are
89 * not the last in the file (e.g., as a result of making a hole and
90 * appending data). Ensure that the remainder is zeroed out.
91 */
92 if (len < UBIFS_BLOCK_SIZE)
93 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
94
95 return 0;
96
97dump:
98 ubifs_err("bad data node (block %u, inode %lu)",
99 block, inode->i_ino);
100 dbg_dump_node(c, dn);
101 return -EINVAL;
102}
103
104static int do_readpage(struct page *page)
105{
106 void *addr;
107 int err = 0, i;
108 unsigned int block, beyond;
109 struct ubifs_data_node *dn;
110 struct inode *inode = page->mapping->host;
111 loff_t i_size = i_size_read(inode);
112
113 dbg_gen("ino %lu, pg %lu, i_size %lld, flags %#lx",
114 inode->i_ino, page->index, i_size, page->flags);
115 ubifs_assert(!PageChecked(page));
116 ubifs_assert(!PagePrivate(page));
117
118 addr = kmap(page);
119
120 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
121 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
122 if (block >= beyond) {
123 /* Reading beyond inode */
124 SetPageChecked(page);
125 memset(addr, 0, PAGE_CACHE_SIZE);
126 goto out;
127 }
128
129 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
130 if (!dn) {
131 err = -ENOMEM;
132 goto error;
133 }
134
135 i = 0;
136 while (1) {
137 int ret;
138
139 if (block >= beyond) {
140 /* Reading beyond inode */
141 err = -ENOENT;
142 memset(addr, 0, UBIFS_BLOCK_SIZE);
143 } else {
144 ret = read_block(inode, addr, block, dn);
145 if (ret) {
146 err = ret;
147 if (err != -ENOENT)
148 break;
149 }
150 }
151 if (++i >= UBIFS_BLOCKS_PER_PAGE)
152 break;
153 block += 1;
154 addr += UBIFS_BLOCK_SIZE;
155 }
156 if (err) {
157 if (err == -ENOENT) {
158 /* Not found, so it must be a hole */
159 SetPageChecked(page);
160 dbg_gen("hole");
161 goto out_free;
162 }
163 ubifs_err("cannot read page %lu of inode %lu, error %d",
164 page->index, inode->i_ino, err);
165 goto error;
166 }
167
168out_free:
169 kfree(dn);
170out:
171 SetPageUptodate(page);
172 ClearPageError(page);
173 flush_dcache_page(page);
174 kunmap(page);
175 return 0;
176
177error:
178 kfree(dn);
179 ClearPageUptodate(page);
180 SetPageError(page);
181 flush_dcache_page(page);
182 kunmap(page);
183 return err;
184}
185
186/**
187 * release_new_page_budget - release budget of a new page.
188 * @c: UBIFS file-system description object
189 *
190 * This is a helper function which releases budget corresponding to the budget
191 * of one new page of data.
192 */
193static void release_new_page_budget(struct ubifs_info *c)
194{
195 struct ubifs_budget_req req = { .recalculate = 1, .new_page = 1 };
196
197 ubifs_release_budget(c, &req);
198}
199
200/**
201 * release_existing_page_budget - release budget of an existing page.
202 * @c: UBIFS file-system description object
203 *
204 * This is a helper function which releases budget corresponding to the budget
205 * of changing one one page of data which already exists on the flash media.
206 */
207static void release_existing_page_budget(struct ubifs_info *c)
208{
209 struct ubifs_budget_req req = { .dd_growth = c->page_budget};
210
211 ubifs_release_budget(c, &req);
212}
213
214static int write_begin_slow(struct address_space *mapping,
215 loff_t pos, unsigned len, struct page **pagep)
216{
217 struct inode *inode = mapping->host;
218 struct ubifs_info *c = inode->i_sb->s_fs_info;
219 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
220 struct ubifs_budget_req req = { .new_page = 1 };
221 int uninitialized_var(err), appending = !!(pos + len > inode->i_size);
222 struct page *page;
223
224 dbg_gen("ino %lu, pos %llu, len %u, i_size %lld",
225 inode->i_ino, pos, len, inode->i_size);
226
227 /*
228 * At the slow path we have to budget before locking the page, because
229 * budgeting may force write-back, which would wait on locked pages and
230 * deadlock if we had the page locked. At this point we do not know
231 * anything about the page, so assume that this is a new page which is
232 * written to a hole. This corresponds to largest budget. Later the
233 * budget will be amended if this is not true.
234 */
235 if (appending)
236 /* We are appending data, budget for inode change */
237 req.dirtied_ino = 1;
238
239 err = ubifs_budget_space(c, &req);
240 if (unlikely(err))
241 return err;
242
243 page = __grab_cache_page(mapping, index);
244 if (unlikely(!page)) {
245 ubifs_release_budget(c, &req);
246 return -ENOMEM;
247 }
248
249 if (!PageUptodate(page)) {
250 if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE)
251 SetPageChecked(page);
252 else {
253 err = do_readpage(page);
254 if (err) {
255 unlock_page(page);
256 page_cache_release(page);
257 return err;
258 }
259 }
260
261 SetPageUptodate(page);
262 ClearPageError(page);
263 }
264
265 if (PagePrivate(page))
266 /*
267 * The page is dirty, which means it was budgeted twice:
268 * o first time the budget was allocated by the task which
269 * made the page dirty and set the PG_private flag;
270 * o and then we budgeted for it for the second time at the
271 * very beginning of this function.
272 *
273 * So what we have to do is to release the page budget we
274 * allocated.
275 */
276 release_new_page_budget(c);
277 else if (!PageChecked(page))
278 /*
279 * We are changing a page which already exists on the media.
280 * This means that changing the page does not make the amount
281 * of indexing information larger, and this part of the budget
282 * which we have already acquired may be released.
283 */
284 ubifs_convert_page_budget(c);
285
286 if (appending) {
287 struct ubifs_inode *ui = ubifs_inode(inode);
288
289 /*
290 * 'ubifs_write_end()' is optimized from the fast-path part of
291 * 'ubifs_write_begin()' and expects the @ui_mutex to be locked
292 * if data is appended.
293 */
294 mutex_lock(&ui->ui_mutex);
295 if (ui->dirty)
296 /*
297 * The inode is dirty already, so we may free the
298 * budget we allocated.
299 */
300 ubifs_release_dirty_inode_budget(c, ui);
301 }
302
303 *pagep = page;
304 return 0;
305}
306
307/**
308 * allocate_budget - allocate budget for 'ubifs_write_begin()'.
309 * @c: UBIFS file-system description object
310 * @page: page to allocate budget for
311 * @ui: UBIFS inode object the page belongs to
312 * @appending: non-zero if the page is appended
313 *
314 * This is a helper function for 'ubifs_write_begin()' which allocates budget
315 * for the operation. The budget is allocated differently depending on whether
316 * this is appending, whether the page is dirty or not, and so on. This
317 * function leaves the @ui->ui_mutex locked in case of appending. Returns zero
318 * in case of success and %-ENOSPC in case of failure.
319 */
320static int allocate_budget(struct ubifs_info *c, struct page *page,
321 struct ubifs_inode *ui, int appending)
322{
323 struct ubifs_budget_req req = { .fast = 1 };
324
325 if (PagePrivate(page)) {
326 if (!appending)
327 /*
328 * The page is dirty and we are not appending, which
329 * means no budget is needed at all.
330 */
331 return 0;
332
333 mutex_lock(&ui->ui_mutex);
334 if (ui->dirty)
335 /*
336 * The page is dirty and we are appending, so the inode
337 * has to be marked as dirty. However, it is already
338 * dirty, so we do not need any budget. We may return,
339 * but @ui->ui_mutex hast to be left locked because we
340 * should prevent write-back from flushing the inode
341 * and freeing the budget. The lock will be released in
342 * 'ubifs_write_end()'.
343 */
344 return 0;
345
346 /*
347 * The page is dirty, we are appending, the inode is clean, so
348 * we need to budget the inode change.
349 */
350 req.dirtied_ino = 1;
351 } else {
352 if (PageChecked(page))
353 /*
354 * The page corresponds to a hole and does not
355 * exist on the media. So changing it makes
356 * make the amount of indexing information
357 * larger, and we have to budget for a new
358 * page.
359 */
360 req.new_page = 1;
361 else
362 /*
363 * Not a hole, the change will not add any new
364 * indexing information, budget for page
365 * change.
366 */
367 req.dirtied_page = 1;
368
369 if (appending) {
370 mutex_lock(&ui->ui_mutex);
371 if (!ui->dirty)
372 /*
373 * The inode is clean but we will have to mark
374 * it as dirty because we are appending. This
375 * needs a budget.
376 */
377 req.dirtied_ino = 1;
378 }
379 }
380
381 return ubifs_budget_space(c, &req);
382}
383
384/*
385 * This function is called when a page of data is going to be written. Since
386 * the page of data will not necessarily go to the flash straight away, UBIFS
387 * has to reserve space on the media for it, which is done by means of
388 * budgeting.
389 *
390 * This is the hot-path of the file-system and we are trying to optimize it as
391 * much as possible. For this reasons it is split on 2 parts - slow and fast.
392 *
393 * There many budgeting cases:
394 * o a new page is appended - we have to budget for a new page and for
395 * changing the inode; however, if the inode is already dirty, there is
396 * no need to budget for it;
397 * o an existing clean page is changed - we have budget for it; if the page
398 * does not exist on the media (a hole), we have to budget for a new
399 * page; otherwise, we may budget for changing an existing page; the
400 * difference between these cases is that changing an existing page does
401 * not introduce anything new to the FS indexing information, so it does
402 * not grow, and smaller budget is acquired in this case;
403 * o an existing dirty page is changed - no need to budget at all, because
404 * the page budget has been acquired by earlier, when the page has been
405 * marked dirty.
406 *
407 * UBIFS budgeting sub-system may force write-back if it thinks there is no
408 * space to reserve. This imposes some locking restrictions and makes it
409 * impossible to take into account the above cases, and makes it impossible to
410 * optimize budgeting.
411 *
412 * The solution for this is that the fast path of 'ubifs_write_begin()' assumes
413 * there is a plenty of flash space and the budget will be acquired quickly,
414 * without forcing write-back. The slow path does not make this assumption.
415 */
416static int ubifs_write_begin(struct file *file, struct address_space *mapping,
417 loff_t pos, unsigned len, unsigned flags,
418 struct page **pagep, void **fsdata)
419{
420 struct inode *inode = mapping->host;
421 struct ubifs_info *c = inode->i_sb->s_fs_info;
422 struct ubifs_inode *ui = ubifs_inode(inode);
423 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
424 int uninitialized_var(err), appending = !!(pos + len > inode->i_size);
425 struct page *page;
426
427
428 ubifs_assert(ubifs_inode(inode)->ui_size == inode->i_size);
429
430 if (unlikely(c->ro_media))
431 return -EROFS;
432
433 /* Try out the fast-path part first */
434 page = __grab_cache_page(mapping, index);
435 if (unlikely(!page))
436 return -ENOMEM;
437
438 if (!PageUptodate(page)) {
439 /* The page is not loaded from the flash */
440 if (!(pos & PAGE_CACHE_MASK) && len == PAGE_CACHE_SIZE)
441 /*
442 * We change whole page so no need to load it. But we
443 * have to set the @PG_checked flag to make the further
444 * code the page is new. This might be not true, but it
445 * is better to budget more that to read the page from
446 * the media.
447 */
448 SetPageChecked(page);
449 else {
450 err = do_readpage(page);
451 if (err) {
452 unlock_page(page);
453 page_cache_release(page);
454 return err;
455 }
456 }
457
458 SetPageUptodate(page);
459 ClearPageError(page);
460 }
461
462 err = allocate_budget(c, page, ui, appending);
463 if (unlikely(err)) {
464 ubifs_assert(err == -ENOSPC);
465 /*
466 * Budgeting failed which means it would have to force
467 * write-back but didn't, because we set the @fast flag in the
468 * request. Write-back cannot be done now, while we have the
469 * page locked, because it would deadlock. Unlock and free
470 * everything and fall-back to slow-path.
471 */
472 if (appending) {
473 ubifs_assert(mutex_is_locked(&ui->ui_mutex));
474 mutex_unlock(&ui->ui_mutex);
475 }
476 unlock_page(page);
477 page_cache_release(page);
478
479 return write_begin_slow(mapping, pos, len, pagep);
480 }
481
482 /*
483 * Whee, we aquired budgeting quickly - without involving
484 * garbage-collection, committing or forceing write-back. We return
485 * with @ui->ui_mutex locked if we are appending pages, and unlocked
486 * otherwise. This is an optimization (slightly hacky though).
487 */
488 *pagep = page;
489 return 0;
490
491}
492
493/**
494 * cancel_budget - cancel budget.
495 * @c: UBIFS file-system description object
496 * @page: page to cancel budget for
497 * @ui: UBIFS inode object the page belongs to
498 * @appending: non-zero if the page is appended
499 *
500 * This is a helper function for a page write operation. It unlocks the
501 * @ui->ui_mutex in case of appending.
502 */
503static void cancel_budget(struct ubifs_info *c, struct page *page,
504 struct ubifs_inode *ui, int appending)
505{
506 if (appending) {
507 if (!ui->dirty)
508 ubifs_release_dirty_inode_budget(c, ui);
509 mutex_unlock(&ui->ui_mutex);
510 }
511 if (!PagePrivate(page)) {
512 if (PageChecked(page))
513 release_new_page_budget(c);
514 else
515 release_existing_page_budget(c);
516 }
517}
518
519static int ubifs_write_end(struct file *file, struct address_space *mapping,
520 loff_t pos, unsigned len, unsigned copied,
521 struct page *page, void *fsdata)
522{
523 struct inode *inode = mapping->host;
524 struct ubifs_inode *ui = ubifs_inode(inode);
525 struct ubifs_info *c = inode->i_sb->s_fs_info;
526 loff_t end_pos = pos + len;
527 int appending = !!(end_pos > inode->i_size);
528
529 dbg_gen("ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld",
530 inode->i_ino, pos, page->index, len, copied, inode->i_size);
531
532 if (unlikely(copied < len && len == PAGE_CACHE_SIZE)) {
533 /*
534 * VFS copied less data to the page that it intended and
535 * declared in its '->write_begin()' call via the @len
536 * argument. If the page was not up-to-date, and @len was
537 * @PAGE_CACHE_SIZE, the 'ubifs_write_begin()' function did
538 * not load it from the media (for optimization reasons). This
539 * means that part of the page contains garbage. So read the
540 * page now.
541 */
542 dbg_gen("copied %d instead of %d, read page and repeat",
543 copied, len);
544 cancel_budget(c, page, ui, appending);
545
546 /*
547 * Return 0 to force VFS to repeat the whole operation, or the
548 * error code if 'do_readpage()' failes.
549 */
550 copied = do_readpage(page);
551 goto out;
552 }
553
554 if (!PagePrivate(page)) {
555 SetPagePrivate(page);
556 atomic_long_inc(&c->dirty_pg_cnt);
557 __set_page_dirty_nobuffers(page);
558 }
559
560 if (appending) {
561 i_size_write(inode, end_pos);
562 ui->ui_size = end_pos;
563 /*
564 * Note, we do not set @I_DIRTY_PAGES (which means that the
565 * inode has dirty pages), this has been done in
566 * '__set_page_dirty_nobuffers()'.
567 */
568 __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
569 ubifs_assert(mutex_is_locked(&ui->ui_mutex));
570 mutex_unlock(&ui->ui_mutex);
571 }
572
573out:
574 unlock_page(page);
575 page_cache_release(page);
576 return copied;
577}
578
579static int ubifs_readpage(struct file *file, struct page *page)
580{
581 do_readpage(page);
582 unlock_page(page);
583 return 0;
584}
585
586static int do_writepage(struct page *page, int len)
587{
588 int err = 0, i, blen;
589 unsigned int block;
590 void *addr;
591 union ubifs_key key;
592 struct inode *inode = page->mapping->host;
593 struct ubifs_info *c = inode->i_sb->s_fs_info;
594
595#ifdef UBIFS_DEBUG
596 spin_lock(&ui->ui_lock);
597 ubifs_assert(page->index <= ui->synced_i_size << PAGE_CACHE_SIZE);
598 spin_unlock(&ui->ui_lock);
599#endif
600
601 /* Update radix tree tags */
602 set_page_writeback(page);
603
604 addr = kmap(page);
605 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
606 i = 0;
607 while (len) {
608 blen = min_t(int, len, UBIFS_BLOCK_SIZE);
609 data_key_init(c, &key, inode->i_ino, block);
610 err = ubifs_jnl_write_data(c, inode, &key, addr, blen);
611 if (err)
612 break;
613 if (++i >= UBIFS_BLOCKS_PER_PAGE)
614 break;
615 block += 1;
616 addr += blen;
617 len -= blen;
618 }
619 if (err) {
620 SetPageError(page);
621 ubifs_err("cannot write page %lu of inode %lu, error %d",
622 page->index, inode->i_ino, err);
623 ubifs_ro_mode(c, err);
624 }
625
626 ubifs_assert(PagePrivate(page));
627 if (PageChecked(page))
628 release_new_page_budget(c);
629 else
630 release_existing_page_budget(c);
631
632 atomic_long_dec(&c->dirty_pg_cnt);
633 ClearPagePrivate(page);
634 ClearPageChecked(page);
635
636 kunmap(page);
637 unlock_page(page);
638 end_page_writeback(page);
639 return err;
640}
641
642/*
643 * When writing-back dirty inodes, VFS first writes-back pages belonging to the
644 * inode, then the inode itself. For UBIFS this may cause a problem. Consider a
645 * situation when a we have an inode with size 0, then a megabyte of data is
646 * appended to the inode, then write-back starts and flushes some amount of the
647 * dirty pages, the journal becomes full, commit happens and finishes, and then
648 * an unclean reboot happens. When the file system is mounted next time, the
649 * inode size would still be 0, but there would be many pages which are beyond
650 * the inode size, they would be indexed and consume flash space. Because the
651 * journal has been committed, the replay would not be able to detect this
652 * situation and correct the inode size. This means UBIFS would have to scan
653 * whole index and correct all inode sizes, which is long an unacceptable.
654 *
655 * To prevent situations like this, UBIFS writes pages back only if they are
656 * within last synchronized inode size, i.e. the the size which has been
657 * written to the flash media last time. Otherwise, UBIFS forces inode
658 * write-back, thus making sure the on-flash inode contains current inode size,
659 * and then keeps writing pages back.
660 *
661 * Some locking issues explanation. 'ubifs_writepage()' first is called with
662 * the page locked, and it locks @ui_mutex. However, write-back does take inode
663 * @i_mutex, which means other VFS operations may be run on this inode at the
664 * same time. And the problematic one is truncation to smaller size, from where
665 * we have to call 'vmtruncate()', which first changes @inode->i_size, then
666 * drops the truncated pages. And while dropping the pages, it takes the page
667 * lock. This means that 'do_truncation()' cannot call 'vmtruncate()' with
668 * @ui_mutex locked, because it would deadlock with 'ubifs_writepage()'. This
669 * means that @inode->i_size is changed while @ui_mutex is unlocked.
670 *
671 * But in 'ubifs_writepage()' we have to guarantee that we do not write beyond
672 * inode size. How do we do this if @inode->i_size may became smaller while we
673 * are in the middle of 'ubifs_writepage()'? The UBIFS solution is the
674 * @ui->ui_isize "shadow" field which UBIFS uses instead of @inode->i_size
675 * internally and updates it under @ui_mutex.
676 *
677 * Q: why we do not worry that if we race with truncation, we may end up with a
678 * situation when the inode is truncated while we are in the middle of
679 * 'do_writepage()', so we do write beyond inode size?
680 * A: If we are in the middle of 'do_writepage()', truncation would be locked
681 * on the page lock and it would not write the truncated inode node to the
682 * journal before we have finished.
683 */
684static int ubifs_writepage(struct page *page, struct writeback_control *wbc)
685{
686 struct inode *inode = page->mapping->host;
687 struct ubifs_inode *ui = ubifs_inode(inode);
688 loff_t i_size = i_size_read(inode), synced_i_size;
689 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
690 int err, len = i_size & (PAGE_CACHE_SIZE - 1);
691 void *kaddr;
692
693 dbg_gen("ino %lu, pg %lu, pg flags %#lx",
694 inode->i_ino, page->index, page->flags);
695 ubifs_assert(PagePrivate(page));
696
697 /* Is the page fully outside @i_size? (truncate in progress) */
698 if (page->index > end_index || (page->index == end_index && !len)) {
699 err = 0;
700 goto out_unlock;
701 }
702
703 spin_lock(&ui->ui_lock);
704 synced_i_size = ui->synced_i_size;
705 spin_unlock(&ui->ui_lock);
706
707 /* Is the page fully inside @i_size? */
708 if (page->index < end_index) {
709 if (page->index >= synced_i_size >> PAGE_CACHE_SHIFT) {
710 err = inode->i_sb->s_op->write_inode(inode, 1);
711 if (err)
712 goto out_unlock;
713 /*
714 * The inode has been written, but the write-buffer has
715 * not been synchronized, so in case of an unclean
716 * reboot we may end up with some pages beyond inode
717 * size, but they would be in the journal (because
718 * commit flushes write buffers) and recovery would deal
719 * with this.
720 */
721 }
722 return do_writepage(page, PAGE_CACHE_SIZE);
723 }
724
725 /*
726 * The page straddles @i_size. It must be zeroed out on each and every
727 * writepage invocation because it may be mmapped. "A file is mapped
728 * in multiples of the page size. For a file that is not a multiple of
729 * the page size, the remaining memory is zeroed when mapped, and
730 * writes to that region are not written out to the file."
731 */
732 kaddr = kmap_atomic(page, KM_USER0);
733 memset(kaddr + len, 0, PAGE_CACHE_SIZE - len);
734 flush_dcache_page(page);
735 kunmap_atomic(kaddr, KM_USER0);
736
737 if (i_size > synced_i_size) {
738 err = inode->i_sb->s_op->write_inode(inode, 1);
739 if (err)
740 goto out_unlock;
741 }
742
743 return do_writepage(page, len);
744
745out_unlock:
746 unlock_page(page);
747 return err;
748}
749
750/**
751 * do_attr_changes - change inode attributes.
752 * @inode: inode to change attributes for
753 * @attr: describes attributes to change
754 */
755static void do_attr_changes(struct inode *inode, const struct iattr *attr)
756{
757 if (attr->ia_valid & ATTR_UID)
758 inode->i_uid = attr->ia_uid;
759 if (attr->ia_valid & ATTR_GID)
760 inode->i_gid = attr->ia_gid;
761 if (attr->ia_valid & ATTR_ATIME)
762 inode->i_atime = timespec_trunc(attr->ia_atime,
763 inode->i_sb->s_time_gran);
764 if (attr->ia_valid & ATTR_MTIME)
765 inode->i_mtime = timespec_trunc(attr->ia_mtime,
766 inode->i_sb->s_time_gran);
767 if (attr->ia_valid & ATTR_CTIME)
768 inode->i_ctime = timespec_trunc(attr->ia_ctime,
769 inode->i_sb->s_time_gran);
770 if (attr->ia_valid & ATTR_MODE) {
771 umode_t mode = attr->ia_mode;
772
773 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
774 mode &= ~S_ISGID;
775 inode->i_mode = mode;
776 }
777}
778
779/**
780 * do_truncation - truncate an inode.
781 * @c: UBIFS file-system description object
782 * @inode: inode to truncate
783 * @attr: inode attribute changes description
784 *
785 * This function implements VFS '->setattr()' call when the inode is truncated
786 * to a smaller size. Returns zero in case of success and a negative error code
787 * in case of failure.
788 */
789static int do_truncation(struct ubifs_info *c, struct inode *inode,
790 const struct iattr *attr)
791{
792 int err;
793 struct ubifs_budget_req req;
794 loff_t old_size = inode->i_size, new_size = attr->ia_size;
795 int offset = new_size & (UBIFS_BLOCK_SIZE - 1);
796 struct ubifs_inode *ui = ubifs_inode(inode);
797
798 dbg_gen("ino %lu, size %lld -> %lld", inode->i_ino, old_size, new_size);
799 memset(&req, 0, sizeof(struct ubifs_budget_req));
800
801 /*
802 * If this is truncation to a smaller size, and we do not truncate on a
803 * block boundary, budget for changing one data block, because the last
804 * block will be re-written.
805 */
806 if (new_size & (UBIFS_BLOCK_SIZE - 1))
807 req.dirtied_page = 1;
808
809 req.dirtied_ino = 1;
810 /* A funny way to budget for truncation node */
811 req.dirtied_ino_d = UBIFS_TRUN_NODE_SZ;
812 err = ubifs_budget_space(c, &req);
813 if (err)
814 return err;
815
816 err = vmtruncate(inode, new_size);
817 if (err)
818 goto out_budg;
819
820 if (offset) {
821 pgoff_t index = new_size >> PAGE_CACHE_SHIFT;
822 struct page *page;
823
824 page = find_lock_page(inode->i_mapping, index);
825 if (page) {
826 if (PageDirty(page)) {
827 /*
828 * 'ubifs_jnl_truncate()' will try to truncate
829 * the last data node, but it contains
830 * out-of-date data because the page is dirty.
831 * Write the page now, so that
832 * 'ubifs_jnl_truncate()' will see an already
833 * truncated (and up to date) data node.
834 */
835 ubifs_assert(PagePrivate(page));
836
837 clear_page_dirty_for_io(page);
838 if (UBIFS_BLOCKS_PER_PAGE_SHIFT)
839 offset = new_size &
840 (PAGE_CACHE_SIZE - 1);
841 err = do_writepage(page, offset);
842 page_cache_release(page);
843 if (err)
844 goto out_budg;
845 /*
846 * We could now tell 'ubifs_jnl_truncate()' not
847 * to read the last block.
848 */
849 } else {
850 /*
851 * We could 'kmap()' the page and pass the data
852 * to 'ubifs_jnl_truncate()' to save it from
853 * having to read it.
854 */
855 unlock_page(page);
856 page_cache_release(page);
857 }
858 }
859 }
860
861 mutex_lock(&ui->ui_mutex);
862 ui->ui_size = inode->i_size;
863 /* Truncation changes inode [mc]time */
864 inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
865 /* The other attributes may be changed at the same time as well */
866 do_attr_changes(inode, attr);
867
868 err = ubifs_jnl_truncate(c, inode, old_size, new_size);
869 mutex_unlock(&ui->ui_mutex);
870out_budg:
871 ubifs_release_budget(c, &req);
872 return err;
873}
874
875/**
876 * do_setattr - change inode attributes.
877 * @c: UBIFS file-system description object
878 * @inode: inode to change attributes for
879 * @attr: inode attribute changes description
880 *
881 * This function implements VFS '->setattr()' call for all cases except
882 * truncations to smaller size. Returns zero in case of success and a negative
883 * error code in case of failure.
884 */
885static int do_setattr(struct ubifs_info *c, struct inode *inode,
886 const struct iattr *attr)
887{
888 int err, release;
889 loff_t new_size = attr->ia_size;
890 struct ubifs_inode *ui = ubifs_inode(inode);
891 struct ubifs_budget_req req = { .dirtied_ino = 1,
892 .dirtied_ino_d = ui->data_len };
893
894 err = ubifs_budget_space(c, &req);
895 if (err)
896 return err;
897
898 if (attr->ia_valid & ATTR_SIZE) {
899 dbg_gen("size %lld -> %lld", inode->i_size, new_size);
900 err = vmtruncate(inode, new_size);
901 if (err)
902 goto out;
903 }
904
905 mutex_lock(&ui->ui_mutex);
906 if (attr->ia_valid & ATTR_SIZE) {
907 /* Truncation changes inode [mc]time */
908 inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
909 /* 'vmtruncate()' changed @i_size, update @ui_size */
910 ui->ui_size = inode->i_size;
911 }
912
913 do_attr_changes(inode, attr);
914
915 release = ui->dirty;
916 if (attr->ia_valid & ATTR_SIZE)
917 /*
918 * Inode length changed, so we have to make sure
919 * @I_DIRTY_DATASYNC is set.
920 */
921 __mark_inode_dirty(inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
922 else
923 mark_inode_dirty_sync(inode);
924 mutex_unlock(&ui->ui_mutex);
925
926 if (release)
927 ubifs_release_budget(c, &req);
928 if (IS_SYNC(inode))
929 err = inode->i_sb->s_op->write_inode(inode, 1);
930 return err;
931
932out:
933 ubifs_release_budget(c, &req);
934 return err;
935}
936
937int ubifs_setattr(struct dentry *dentry, struct iattr *attr)
938{
939 int err;
940 struct inode *inode = dentry->d_inode;
941 struct ubifs_info *c = inode->i_sb->s_fs_info;
942
943 dbg_gen("ino %lu, ia_valid %#x", inode->i_ino, attr->ia_valid);
944 err = inode_change_ok(inode, attr);
945 if (err)
946 return err;
947
948 err = dbg_check_synced_i_size(inode);
949 if (err)
950 return err;
951
952 if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size < inode->i_size)
953 /* Truncation to a smaller size */
954 err = do_truncation(c, inode, attr);
955 else
956 err = do_setattr(c, inode, attr);
957
958 return err;
959}
960
961static void ubifs_invalidatepage(struct page *page, unsigned long offset)
962{
963 struct inode *inode = page->mapping->host;
964 struct ubifs_info *c = inode->i_sb->s_fs_info;
965
966 ubifs_assert(PagePrivate(page));
967 if (offset)
968 /* Partial page remains dirty */
969 return;
970
971 if (PageChecked(page))
972 release_new_page_budget(c);
973 else
974 release_existing_page_budget(c);
975
976 atomic_long_dec(&c->dirty_pg_cnt);
977 ClearPagePrivate(page);
978 ClearPageChecked(page);
979}
980
981static void *ubifs_follow_link(struct dentry *dentry, struct nameidata *nd)
982{
983 struct ubifs_inode *ui = ubifs_inode(dentry->d_inode);
984
985 nd_set_link(nd, ui->data);
986 return NULL;
987}
988
989int ubifs_fsync(struct file *file, struct dentry *dentry, int datasync)
990{
991 struct inode *inode = dentry->d_inode;
992 struct ubifs_info *c = inode->i_sb->s_fs_info;
993 int err;
994
995 dbg_gen("syncing inode %lu", inode->i_ino);
996
997 /*
998 * VFS has already synchronized dirty pages for this inode. Synchronize
999 * the inode unless this is a 'datasync()' call.
1000 */
1001 if (!datasync || (inode->i_state & I_DIRTY_DATASYNC)) {
1002 err = inode->i_sb->s_op->write_inode(inode, 1);
1003 if (err)
1004 return err;
1005 }
1006
1007 /*
1008 * Nodes related to this inode may still sit in a write-buffer. Flush
1009 * them.
1010 */
1011 err = ubifs_sync_wbufs_by_inode(c, inode);
1012 if (err)
1013 return err;
1014
1015 return 0;
1016}
1017
1018/**
1019 * mctime_update_needed - check if mtime or ctime update is needed.
1020 * @inode: the inode to do the check for
1021 * @now: current time
1022 *
1023 * This helper function checks if the inode mtime/ctime should be updated or
1024 * not. If current values of the time-stamps are within the UBIFS inode time
1025 * granularity, they are not updated. This is an optimization.
1026 */
1027static inline int mctime_update_needed(const struct inode *inode,
1028 const struct timespec *now)
1029{
1030 if (!timespec_equal(&inode->i_mtime, now) ||
1031 !timespec_equal(&inode->i_ctime, now))
1032 return 1;
1033 return 0;
1034}
1035
1036/**
1037 * update_ctime - update mtime and ctime of an inode.
1038 * @c: UBIFS file-system description object
1039 * @inode: inode to update
1040 *
1041 * This function updates mtime and ctime of the inode if it is not equivalent to
1042 * current time. Returns zero in case of success and a negative error code in
1043 * case of failure.
1044 */
1045static int update_mctime(struct ubifs_info *c, struct inode *inode)
1046{
1047 struct timespec now = ubifs_current_time(inode);
1048 struct ubifs_inode *ui = ubifs_inode(inode);
1049
1050 if (mctime_update_needed(inode, &now)) {
1051 int err, release;
1052 struct ubifs_budget_req req = { .dirtied_ino = 1,
1053 .dirtied_ino_d = ui->data_len };
1054
1055 err = ubifs_budget_space(c, &req);
1056 if (err)
1057 return err;
1058
1059 mutex_lock(&ui->ui_mutex);
1060 inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
1061 release = ui->dirty;
1062 mark_inode_dirty_sync(inode);
1063 mutex_unlock(&ui->ui_mutex);
1064 if (release)
1065 ubifs_release_budget(c, &req);
1066 }
1067
1068 return 0;
1069}
1070
1071static ssize_t ubifs_aio_write(struct kiocb *iocb, const struct iovec *iov,
1072 unsigned long nr_segs, loff_t pos)
1073{
1074 int err;
1075 ssize_t ret;
1076 struct inode *inode = iocb->ki_filp->f_mapping->host;
1077 struct ubifs_info *c = inode->i_sb->s_fs_info;
1078
1079 err = update_mctime(c, inode);
1080 if (err)
1081 return err;
1082
1083 ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
1084 if (ret < 0)
1085 return ret;
1086
1087 if (ret > 0 && (IS_SYNC(inode) || iocb->ki_filp->f_flags & O_SYNC)) {
1088 err = ubifs_sync_wbufs_by_inode(c, inode);
1089 if (err)
1090 return err;
1091 }
1092
1093 return ret;
1094}
1095
1096static int ubifs_set_page_dirty(struct page *page)
1097{
1098 int ret;
1099
1100 ret = __set_page_dirty_nobuffers(page);
1101 /*
1102 * An attempt to dirty a page without budgeting for it - should not
1103 * happen.
1104 */
1105 ubifs_assert(ret == 0);
1106 return ret;
1107}
1108
1109static int ubifs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1110{
1111 /*
1112 * An attempt to release a dirty page without budgeting for it - should
1113 * not happen.
1114 */
1115 if (PageWriteback(page))
1116 return 0;
1117 ubifs_assert(PagePrivate(page));
1118 ubifs_assert(0);
1119 ClearPagePrivate(page);
1120 ClearPageChecked(page);
1121 return 1;
1122}
1123
1124/*
1125 * mmap()d file has taken write protection fault and is being made
1126 * writable. UBIFS must ensure page is budgeted for.
1127 */
1128static int ubifs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1129{
1130 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1131 struct ubifs_info *c = inode->i_sb->s_fs_info;
1132 struct timespec now = ubifs_current_time(inode);
1133 struct ubifs_budget_req req = { .new_page = 1 };
1134 int err, update_time;
1135
1136 dbg_gen("ino %lu, pg %lu, i_size %lld", inode->i_ino, page->index,
1137 i_size_read(inode));
1138 ubifs_assert(!(inode->i_sb->s_flags & MS_RDONLY));
1139
1140 if (unlikely(c->ro_media))
1141 return -EROFS;
1142
1143 /*
1144 * We have not locked @page so far so we may budget for changing the
1145 * page. Note, we cannot do this after we locked the page, because
1146 * budgeting may cause write-back which would cause deadlock.
1147 *
1148 * At the moment we do not know whether the page is dirty or not, so we
1149 * assume that it is not and budget for a new page. We could look at
1150 * the @PG_private flag and figure this out, but we may race with write
1151 * back and the page state may change by the time we lock it, so this
1152 * would need additional care. We do not bother with this at the
1153 * moment, although it might be good idea to do. Instead, we allocate
1154 * budget for a new page and amend it later on if the page was in fact
1155 * dirty.
1156 *
1157 * The budgeting-related logic of this function is similar to what we
1158 * do in 'ubifs_write_begin()' and 'ubifs_write_end()'. Glance there
1159 * for more comments.
1160 */
1161 update_time = mctime_update_needed(inode, &now);
1162 if (update_time)
1163 /*
1164 * We have to change inode time stamp which requires extra
1165 * budgeting.
1166 */
1167 req.dirtied_ino = 1;
1168
1169 err = ubifs_budget_space(c, &req);
1170 if (unlikely(err)) {
1171 if (err == -ENOSPC)
1172 ubifs_warn("out of space for mmapped file "
1173 "(inode number %lu)", inode->i_ino);
1174 return err;
1175 }
1176
1177 lock_page(page);
1178 if (unlikely(page->mapping != inode->i_mapping ||
1179 page_offset(page) > i_size_read(inode))) {
1180 /* Page got truncated out from underneath us */
1181 err = -EINVAL;
1182 goto out_unlock;
1183 }
1184
1185 if (PagePrivate(page))
1186 release_new_page_budget(c);
1187 else {
1188 if (!PageChecked(page))
1189 ubifs_convert_page_budget(c);
1190 SetPagePrivate(page);
1191 atomic_long_inc(&c->dirty_pg_cnt);
1192 __set_page_dirty_nobuffers(page);
1193 }
1194
1195 if (update_time) {
1196 int release;
1197 struct ubifs_inode *ui = ubifs_inode(inode);
1198
1199 mutex_lock(&ui->ui_mutex);
1200 inode->i_mtime = inode->i_ctime = ubifs_current_time(inode);
1201 release = ui->dirty;
1202 mark_inode_dirty_sync(inode);
1203 mutex_unlock(&ui->ui_mutex);
1204 if (release)
1205 ubifs_release_dirty_inode_budget(c, ui);
1206 }
1207
1208 unlock_page(page);
1209 return 0;
1210
1211out_unlock:
1212 unlock_page(page);
1213 ubifs_release_budget(c, &req);
1214 return err;
1215}
1216
1217static struct vm_operations_struct ubifs_file_vm_ops = {
1218 .fault = filemap_fault,
1219 .page_mkwrite = ubifs_vm_page_mkwrite,
1220};
1221
1222static int ubifs_file_mmap(struct file *file, struct vm_area_struct *vma)
1223{
1224 int err;
1225
1226 /* 'generic_file_mmap()' takes care of NOMMU case */
1227 err = generic_file_mmap(file, vma);
1228 if (err)
1229 return err;
1230 vma->vm_ops = &ubifs_file_vm_ops;
1231 return 0;
1232}
1233
1234struct address_space_operations ubifs_file_address_operations = {
1235 .readpage = ubifs_readpage,
1236 .writepage = ubifs_writepage,
1237 .write_begin = ubifs_write_begin,
1238 .write_end = ubifs_write_end,
1239 .invalidatepage = ubifs_invalidatepage,
1240 .set_page_dirty = ubifs_set_page_dirty,
1241 .releasepage = ubifs_releasepage,
1242};
1243
1244struct inode_operations ubifs_file_inode_operations = {
1245 .setattr = ubifs_setattr,
1246 .getattr = ubifs_getattr,
1247#ifdef CONFIG_UBIFS_FS_XATTR
1248 .setxattr = ubifs_setxattr,
1249 .getxattr = ubifs_getxattr,
1250 .listxattr = ubifs_listxattr,
1251 .removexattr = ubifs_removexattr,
1252#endif
1253};
1254
1255struct inode_operations ubifs_symlink_inode_operations = {
1256 .readlink = generic_readlink,
1257 .follow_link = ubifs_follow_link,
1258 .setattr = ubifs_setattr,
1259 .getattr = ubifs_getattr,
1260};
1261
1262struct file_operations ubifs_file_operations = {
1263 .llseek = generic_file_llseek,
1264 .read = do_sync_read,
1265 .write = do_sync_write,
1266 .aio_read = generic_file_aio_read,
1267 .aio_write = ubifs_aio_write,
1268 .mmap = ubifs_file_mmap,
1269 .fsync = ubifs_fsync,
1270 .unlocked_ioctl = ubifs_ioctl,
1271 .splice_read = generic_file_splice_read,
1272#ifdef CONFIG_COMPAT
1273 .compat_ioctl = ubifs_compat_ioctl,
1274#endif
1275};
diff --git a/fs/ubifs/find.c b/fs/ubifs/find.c
new file mode 100644
index 000000000000..10394c548367
--- /dev/null
+++ b/fs/ubifs/find.c
@@ -0,0 +1,975 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file contains functions for finding LEBs for various purposes e.g.
25 * garbage collection. In general, lprops category heaps and lists are used
26 * for fast access, falling back on scanning the LPT as a last resort.
27 */
28
29#include <linux/sort.h>
30#include "ubifs.h"
31
32/**
33 * struct scan_data - data provided to scan callback functions
34 * @min_space: minimum number of bytes for which to scan
35 * @pick_free: whether it is OK to scan for empty LEBs
36 * @lnum: LEB number found is returned here
37 * @exclude_index: whether to exclude index LEBs
38 */
39struct scan_data {
40 int min_space;
41 int pick_free;
42 int lnum;
43 int exclude_index;
44};
45
46/**
47 * valuable - determine whether LEB properties are valuable.
48 * @c: the UBIFS file-system description object
49 * @lprops: LEB properties
50 *
51 * This function return %1 if the LEB properties should be added to the LEB
52 * properties tree in memory. Otherwise %0 is returned.
53 */
54static int valuable(struct ubifs_info *c, const struct ubifs_lprops *lprops)
55{
56 int n, cat = lprops->flags & LPROPS_CAT_MASK;
57 struct ubifs_lpt_heap *heap;
58
59 switch (cat) {
60 case LPROPS_DIRTY:
61 case LPROPS_DIRTY_IDX:
62 case LPROPS_FREE:
63 heap = &c->lpt_heap[cat - 1];
64 if (heap->cnt < heap->max_cnt)
65 return 1;
66 if (lprops->free + lprops->dirty >= c->dark_wm)
67 return 1;
68 return 0;
69 case LPROPS_EMPTY:
70 n = c->lst.empty_lebs + c->freeable_cnt -
71 c->lst.taken_empty_lebs;
72 if (n < c->lsave_cnt)
73 return 1;
74 return 0;
75 case LPROPS_FREEABLE:
76 return 1;
77 case LPROPS_FRDI_IDX:
78 return 1;
79 }
80 return 0;
81}
82
83/**
84 * scan_for_dirty_cb - dirty space scan callback.
85 * @c: the UBIFS file-system description object
86 * @lprops: LEB properties to scan
87 * @in_tree: whether the LEB properties are in main memory
88 * @data: information passed to and from the caller of the scan
89 *
90 * This function returns a code that indicates whether the scan should continue
91 * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
92 * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
93 * (%LPT_SCAN_STOP).
94 */
95static int scan_for_dirty_cb(struct ubifs_info *c,
96 const struct ubifs_lprops *lprops, int in_tree,
97 struct scan_data *data)
98{
99 int ret = LPT_SCAN_CONTINUE;
100
101 /* Exclude LEBs that are currently in use */
102 if (lprops->flags & LPROPS_TAKEN)
103 return LPT_SCAN_CONTINUE;
104 /* Determine whether to add these LEB properties to the tree */
105 if (!in_tree && valuable(c, lprops))
106 ret |= LPT_SCAN_ADD;
107 /* Exclude LEBs with too little space */
108 if (lprops->free + lprops->dirty < data->min_space)
109 return ret;
110 /* If specified, exclude index LEBs */
111 if (data->exclude_index && lprops->flags & LPROPS_INDEX)
112 return ret;
113 /* If specified, exclude empty or freeable LEBs */
114 if (lprops->free + lprops->dirty == c->leb_size) {
115 if (!data->pick_free)
116 return ret;
117 /* Exclude LEBs with too little dirty space (unless it is empty) */
118 } else if (lprops->dirty < c->dead_wm)
119 return ret;
120 /* Finally we found space */
121 data->lnum = lprops->lnum;
122 return LPT_SCAN_ADD | LPT_SCAN_STOP;
123}
124
125/**
126 * scan_for_dirty - find a data LEB with free space.
127 * @c: the UBIFS file-system description object
128 * @min_space: minimum amount free plus dirty space the returned LEB has to
129 * have
130 * @pick_free: if it is OK to return a free or freeable LEB
131 * @exclude_index: whether to exclude index LEBs
132 *
133 * This function returns a pointer to the LEB properties found or a negative
134 * error code.
135 */
136static const struct ubifs_lprops *scan_for_dirty(struct ubifs_info *c,
137 int min_space, int pick_free,
138 int exclude_index)
139{
140 const struct ubifs_lprops *lprops;
141 struct ubifs_lpt_heap *heap;
142 struct scan_data data;
143 int err, i;
144
145 /* There may be an LEB with enough dirty space on the free heap */
146 heap = &c->lpt_heap[LPROPS_FREE - 1];
147 for (i = 0; i < heap->cnt; i++) {
148 lprops = heap->arr[i];
149 if (lprops->free + lprops->dirty < min_space)
150 continue;
151 if (lprops->dirty < c->dead_wm)
152 continue;
153 return lprops;
154 }
155 /*
156 * A LEB may have fallen off of the bottom of the dirty heap, and ended
157 * up as uncategorized even though it has enough dirty space for us now,
158 * so check the uncategorized list. N.B. neither empty nor freeable LEBs
159 * can end up as uncategorized because they are kept on lists not
160 * finite-sized heaps.
161 */
162 list_for_each_entry(lprops, &c->uncat_list, list) {
163 if (lprops->flags & LPROPS_TAKEN)
164 continue;
165 if (lprops->free + lprops->dirty < min_space)
166 continue;
167 if (exclude_index && (lprops->flags & LPROPS_INDEX))
168 continue;
169 if (lprops->dirty < c->dead_wm)
170 continue;
171 return lprops;
172 }
173 /* We have looked everywhere in main memory, now scan the flash */
174 if (c->pnodes_have >= c->pnode_cnt)
175 /* All pnodes are in memory, so skip scan */
176 return ERR_PTR(-ENOSPC);
177 data.min_space = min_space;
178 data.pick_free = pick_free;
179 data.lnum = -1;
180 data.exclude_index = exclude_index;
181 err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
182 (ubifs_lpt_scan_callback)scan_for_dirty_cb,
183 &data);
184 if (err)
185 return ERR_PTR(err);
186 ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
187 c->lscan_lnum = data.lnum;
188 lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
189 if (IS_ERR(lprops))
190 return lprops;
191 ubifs_assert(lprops->lnum == data.lnum);
192 ubifs_assert(lprops->free + lprops->dirty >= min_space);
193 ubifs_assert(lprops->dirty >= c->dead_wm ||
194 (pick_free &&
195 lprops->free + lprops->dirty == c->leb_size));
196 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
197 ubifs_assert(!exclude_index || !(lprops->flags & LPROPS_INDEX));
198 return lprops;
199}
200
201/**
202 * ubifs_find_dirty_leb - find a dirty LEB for the Garbage Collector.
203 * @c: the UBIFS file-system description object
204 * @ret_lp: LEB properties are returned here on exit
205 * @min_space: minimum amount free plus dirty space the returned LEB has to
206 * have
207 * @pick_free: controls whether it is OK to pick empty or index LEBs
208 *
209 * This function tries to find a dirty logical eraseblock which has at least
210 * @min_space free and dirty space. It prefers to take an LEB from the dirty or
211 * dirty index heap, and it falls-back to LPT scanning if the heaps are empty
212 * or do not have an LEB which satisfies the @min_space criteria.
213 *
214 * Note:
215 * o LEBs which have less than dead watermark of dirty space are never picked
216 * by this function;
217 *
218 * Returns zero and the LEB properties of
219 * found dirty LEB in case of success, %-ENOSPC if no dirty LEB was found and a
220 * negative error code in case of other failures. The returned LEB is marked as
221 * "taken".
222 *
223 * The additional @pick_free argument controls if this function has to return a
224 * free or freeable LEB if one is present. For example, GC must to set it to %1,
225 * when called from the journal space reservation function, because the
226 * appearance of free space may coincide with the loss of enough dirty space
227 * for GC to succeed anyway.
228 *
229 * In contrast, if the Garbage Collector is called from budgeting, it should
230 * just make free space, not return LEBs which are already free or freeable.
231 *
232 * In addition @pick_free is set to %2 by the recovery process in order to
233 * recover gc_lnum in which case an index LEB must not be returned.
234 */
235int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
236 int min_space, int pick_free)
237{
238 int err = 0, sum, exclude_index = pick_free == 2 ? 1 : 0;
239 const struct ubifs_lprops *lp = NULL, *idx_lp = NULL;
240 struct ubifs_lpt_heap *heap, *idx_heap;
241
242 ubifs_get_lprops(c);
243
244 if (pick_free) {
245 int lebs, rsvd_idx_lebs = 0;
246
247 spin_lock(&c->space_lock);
248 lebs = c->lst.empty_lebs;
249 lebs += c->freeable_cnt - c->lst.taken_empty_lebs;
250
251 /*
252 * Note, the index may consume more LEBs than have been reserved
253 * for it. It is OK because it might be consolidated by GC.
254 * But if the index takes fewer LEBs than it is reserved for it,
255 * this function must avoid picking those reserved LEBs.
256 */
257 if (c->min_idx_lebs >= c->lst.idx_lebs) {
258 rsvd_idx_lebs = c->min_idx_lebs - c->lst.idx_lebs;
259 exclude_index = 1;
260 }
261 spin_unlock(&c->space_lock);
262
263 /* Check if there are enough free LEBs for the index */
264 if (rsvd_idx_lebs < lebs) {
265 /* OK, try to find an empty LEB */
266 lp = ubifs_fast_find_empty(c);
267 if (lp)
268 goto found;
269
270 /* Or a freeable LEB */
271 lp = ubifs_fast_find_freeable(c);
272 if (lp)
273 goto found;
274 } else
275 /*
276 * We cannot pick free/freeable LEBs in the below code.
277 */
278 pick_free = 0;
279 } else {
280 spin_lock(&c->space_lock);
281 exclude_index = (c->min_idx_lebs >= c->lst.idx_lebs);
282 spin_unlock(&c->space_lock);
283 }
284
285 /* Look on the dirty and dirty index heaps */
286 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
287 idx_heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
288
289 if (idx_heap->cnt && !exclude_index) {
290 idx_lp = idx_heap->arr[0];
291 sum = idx_lp->free + idx_lp->dirty;
292 /*
293 * Since we reserve twice as more space for the index than it
294 * actually takes, it does not make sense to pick indexing LEBs
295 * with less than half LEB of dirty space.
296 */
297 if (sum < min_space || sum < c->half_leb_size)
298 idx_lp = NULL;
299 }
300
301 if (heap->cnt) {
302 lp = heap->arr[0];
303 if (lp->dirty + lp->free < min_space)
304 lp = NULL;
305 }
306
307 /* Pick the LEB with most space */
308 if (idx_lp && lp) {
309 if (idx_lp->free + idx_lp->dirty >= lp->free + lp->dirty)
310 lp = idx_lp;
311 } else if (idx_lp && !lp)
312 lp = idx_lp;
313
314 if (lp) {
315 ubifs_assert(lp->dirty >= c->dead_wm);
316 goto found;
317 }
318
319 /* Did not find a dirty LEB on the dirty heaps, have to scan */
320 dbg_find("scanning LPT for a dirty LEB");
321 lp = scan_for_dirty(c, min_space, pick_free, exclude_index);
322 if (IS_ERR(lp)) {
323 err = PTR_ERR(lp);
324 goto out;
325 }
326 ubifs_assert(lp->dirty >= c->dead_wm ||
327 (pick_free && lp->free + lp->dirty == c->leb_size));
328
329found:
330 dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
331 lp->lnum, lp->free, lp->dirty, lp->flags);
332
333 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
334 lp->flags | LPROPS_TAKEN, 0);
335 if (IS_ERR(lp)) {
336 err = PTR_ERR(lp);
337 goto out;
338 }
339
340 memcpy(ret_lp, lp, sizeof(struct ubifs_lprops));
341
342out:
343 ubifs_release_lprops(c);
344 return err;
345}
346
347/**
348 * scan_for_free_cb - free space scan callback.
349 * @c: the UBIFS file-system description object
350 * @lprops: LEB properties to scan
351 * @in_tree: whether the LEB properties are in main memory
352 * @data: information passed to and from the caller of the scan
353 *
354 * This function returns a code that indicates whether the scan should continue
355 * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
356 * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
357 * (%LPT_SCAN_STOP).
358 */
359static int scan_for_free_cb(struct ubifs_info *c,
360 const struct ubifs_lprops *lprops, int in_tree,
361 struct scan_data *data)
362{
363 int ret = LPT_SCAN_CONTINUE;
364
365 /* Exclude LEBs that are currently in use */
366 if (lprops->flags & LPROPS_TAKEN)
367 return LPT_SCAN_CONTINUE;
368 /* Determine whether to add these LEB properties to the tree */
369 if (!in_tree && valuable(c, lprops))
370 ret |= LPT_SCAN_ADD;
371 /* Exclude index LEBs */
372 if (lprops->flags & LPROPS_INDEX)
373 return ret;
374 /* Exclude LEBs with too little space */
375 if (lprops->free < data->min_space)
376 return ret;
377 /* If specified, exclude empty LEBs */
378 if (!data->pick_free && lprops->free == c->leb_size)
379 return ret;
380 /*
381 * LEBs that have only free and dirty space must not be allocated
382 * because they may have been unmapped already or they may have data
383 * that is obsolete only because of nodes that are still sitting in a
384 * wbuf.
385 */
386 if (lprops->free + lprops->dirty == c->leb_size && lprops->dirty > 0)
387 return ret;
388 /* Finally we found space */
389 data->lnum = lprops->lnum;
390 return LPT_SCAN_ADD | LPT_SCAN_STOP;
391}
392
393/**
394 * do_find_free_space - find a data LEB with free space.
395 * @c: the UBIFS file-system description object
396 * @min_space: minimum amount of free space required
397 * @pick_free: whether it is OK to scan for empty LEBs
398 * @squeeze: whether to try to find space in a non-empty LEB first
399 *
400 * This function returns a pointer to the LEB properties found or a negative
401 * error code.
402 */
403static
404const struct ubifs_lprops *do_find_free_space(struct ubifs_info *c,
405 int min_space, int pick_free,
406 int squeeze)
407{
408 const struct ubifs_lprops *lprops;
409 struct ubifs_lpt_heap *heap;
410 struct scan_data data;
411 int err, i;
412
413 if (squeeze) {
414 lprops = ubifs_fast_find_free(c);
415 if (lprops && lprops->free >= min_space)
416 return lprops;
417 }
418 if (pick_free) {
419 lprops = ubifs_fast_find_empty(c);
420 if (lprops)
421 return lprops;
422 }
423 if (!squeeze) {
424 lprops = ubifs_fast_find_free(c);
425 if (lprops && lprops->free >= min_space)
426 return lprops;
427 }
428 /* There may be an LEB with enough free space on the dirty heap */
429 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
430 for (i = 0; i < heap->cnt; i++) {
431 lprops = heap->arr[i];
432 if (lprops->free >= min_space)
433 return lprops;
434 }
435 /*
436 * A LEB may have fallen off of the bottom of the free heap, and ended
437 * up as uncategorized even though it has enough free space for us now,
438 * so check the uncategorized list. N.B. neither empty nor freeable LEBs
439 * can end up as uncategorized because they are kept on lists not
440 * finite-sized heaps.
441 */
442 list_for_each_entry(lprops, &c->uncat_list, list) {
443 if (lprops->flags & LPROPS_TAKEN)
444 continue;
445 if (lprops->flags & LPROPS_INDEX)
446 continue;
447 if (lprops->free >= min_space)
448 return lprops;
449 }
450 /* We have looked everywhere in main memory, now scan the flash */
451 if (c->pnodes_have >= c->pnode_cnt)
452 /* All pnodes are in memory, so skip scan */
453 return ERR_PTR(-ENOSPC);
454 data.min_space = min_space;
455 data.pick_free = pick_free;
456 data.lnum = -1;
457 err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
458 (ubifs_lpt_scan_callback)scan_for_free_cb,
459 &data);
460 if (err)
461 return ERR_PTR(err);
462 ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
463 c->lscan_lnum = data.lnum;
464 lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
465 if (IS_ERR(lprops))
466 return lprops;
467 ubifs_assert(lprops->lnum == data.lnum);
468 ubifs_assert(lprops->free >= min_space);
469 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
470 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
471 return lprops;
472}
473
474/**
475 * ubifs_find_free_space - find a data LEB with free space.
476 * @c: the UBIFS file-system description object
477 * @min_space: minimum amount of required free space
478 * @free: contains amount of free space in the LEB on exit
479 * @squeeze: whether to try to find space in a non-empty LEB first
480 *
481 * This function looks for an LEB with at least @min_space bytes of free space.
482 * It tries to find an empty LEB if possible. If no empty LEBs are available,
483 * this function searches for a non-empty data LEB. The returned LEB is marked
484 * as "taken".
485 *
486 * This function returns found LEB number in case of success, %-ENOSPC if it
487 * failed to find a LEB with @min_space bytes of free space and other a negative
488 * error codes in case of failure.
489 */
490int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *free,
491 int squeeze)
492{
493 const struct ubifs_lprops *lprops;
494 int lebs, rsvd_idx_lebs, pick_free = 0, err, lnum, flags;
495
496 dbg_find("min_space %d", min_space);
497 ubifs_get_lprops(c);
498
499 /* Check if there are enough empty LEBs for commit */
500 spin_lock(&c->space_lock);
501 if (c->min_idx_lebs > c->lst.idx_lebs)
502 rsvd_idx_lebs = c->min_idx_lebs - c->lst.idx_lebs;
503 else
504 rsvd_idx_lebs = 0;
505 lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
506 c->lst.taken_empty_lebs;
507 ubifs_assert(lebs + c->lst.idx_lebs >= c->min_idx_lebs);
508 if (rsvd_idx_lebs < lebs)
509 /*
510 * OK to allocate an empty LEB, but we still don't want to go
511 * looking for one if there aren't any.
512 */
513 if (c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
514 pick_free = 1;
515 /*
516 * Because we release the space lock, we must account
517 * for this allocation here. After the LEB properties
518 * flags have been updated, we subtract one. Note, the
519 * result of this is that lprops also decreases
520 * @taken_empty_lebs in 'ubifs_change_lp()', so it is
521 * off by one for a short period of time which may
522 * introduce a small disturbance to budgeting
523 * calculations, but this is harmless because at the
524 * worst case this would make the budgeting subsystem
525 * be more pessimistic than needed.
526 *
527 * Fundamentally, this is about serialization of the
528 * budgeting and lprops subsystems. We could make the
529 * @space_lock a mutex and avoid dropping it before
530 * calling 'ubifs_change_lp()', but mutex is more
531 * heavy-weight, and we want budgeting to be as fast as
532 * possible.
533 */
534 c->lst.taken_empty_lebs += 1;
535 }
536 spin_unlock(&c->space_lock);
537
538 lprops = do_find_free_space(c, min_space, pick_free, squeeze);
539 if (IS_ERR(lprops)) {
540 err = PTR_ERR(lprops);
541 goto out;
542 }
543
544 lnum = lprops->lnum;
545 flags = lprops->flags | LPROPS_TAKEN;
546
547 lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC, flags, 0);
548 if (IS_ERR(lprops)) {
549 err = PTR_ERR(lprops);
550 goto out;
551 }
552
553 if (pick_free) {
554 spin_lock(&c->space_lock);
555 c->lst.taken_empty_lebs -= 1;
556 spin_unlock(&c->space_lock);
557 }
558
559 *free = lprops->free;
560 ubifs_release_lprops(c);
561
562 if (*free == c->leb_size) {
563 /*
564 * Ensure that empty LEBs have been unmapped. They may not have
565 * been, for example, because of an unclean unmount. Also
566 * LEBs that were freeable LEBs (free + dirty == leb_size) will
567 * not have been unmapped.
568 */
569 err = ubifs_leb_unmap(c, lnum);
570 if (err)
571 return err;
572 }
573
574 dbg_find("found LEB %d, free %d", lnum, *free);
575 ubifs_assert(*free >= min_space);
576 return lnum;
577
578out:
579 if (pick_free) {
580 spin_lock(&c->space_lock);
581 c->lst.taken_empty_lebs -= 1;
582 spin_unlock(&c->space_lock);
583 }
584 ubifs_release_lprops(c);
585 return err;
586}
587
588/**
589 * scan_for_idx_cb - callback used by the scan for a free LEB for the index.
590 * @c: the UBIFS file-system description object
591 * @lprops: LEB properties to scan
592 * @in_tree: whether the LEB properties are in main memory
593 * @data: information passed to and from the caller of the scan
594 *
595 * This function returns a code that indicates whether the scan should continue
596 * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
597 * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
598 * (%LPT_SCAN_STOP).
599 */
600static int scan_for_idx_cb(struct ubifs_info *c,
601 const struct ubifs_lprops *lprops, int in_tree,
602 struct scan_data *data)
603{
604 int ret = LPT_SCAN_CONTINUE;
605
606 /* Exclude LEBs that are currently in use */
607 if (lprops->flags & LPROPS_TAKEN)
608 return LPT_SCAN_CONTINUE;
609 /* Determine whether to add these LEB properties to the tree */
610 if (!in_tree && valuable(c, lprops))
611 ret |= LPT_SCAN_ADD;
612 /* Exclude index LEBS */
613 if (lprops->flags & LPROPS_INDEX)
614 return ret;
615 /* Exclude LEBs that cannot be made empty */
616 if (lprops->free + lprops->dirty != c->leb_size)
617 return ret;
618 /*
619 * We are allocating for the index so it is safe to allocate LEBs with
620 * only free and dirty space, because write buffers are sync'd at commit
621 * start.
622 */
623 data->lnum = lprops->lnum;
624 return LPT_SCAN_ADD | LPT_SCAN_STOP;
625}
626
627/**
628 * scan_for_leb_for_idx - scan for a free LEB for the index.
629 * @c: the UBIFS file-system description object
630 */
631static const struct ubifs_lprops *scan_for_leb_for_idx(struct ubifs_info *c)
632{
633 struct ubifs_lprops *lprops;
634 struct scan_data data;
635 int err;
636
637 data.lnum = -1;
638 err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
639 (ubifs_lpt_scan_callback)scan_for_idx_cb,
640 &data);
641 if (err)
642 return ERR_PTR(err);
643 ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
644 c->lscan_lnum = data.lnum;
645 lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
646 if (IS_ERR(lprops))
647 return lprops;
648 ubifs_assert(lprops->lnum == data.lnum);
649 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
650 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
651 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
652 return lprops;
653}
654
655/**
656 * ubifs_find_free_leb_for_idx - find a free LEB for the index.
657 * @c: the UBIFS file-system description object
658 *
659 * This function looks for a free LEB and returns that LEB number. The returned
660 * LEB is marked as "taken", "index".
661 *
662 * Only empty LEBs are allocated. This is for two reasons. First, the commit
663 * calculates the number of LEBs to allocate based on the assumption that they
664 * will be empty. Secondly, free space at the end of an index LEB is not
665 * guaranteed to be empty because it may have been used by the in-the-gaps
666 * method prior to an unclean unmount.
667 *
668 * If no LEB is found %-ENOSPC is returned. For other failures another negative
669 * error code is returned.
670 */
671int ubifs_find_free_leb_for_idx(struct ubifs_info *c)
672{
673 const struct ubifs_lprops *lprops;
674 int lnum = -1, err, flags;
675
676 ubifs_get_lprops(c);
677
678 lprops = ubifs_fast_find_empty(c);
679 if (!lprops) {
680 lprops = ubifs_fast_find_freeable(c);
681 if (!lprops) {
682 ubifs_assert(c->freeable_cnt == 0);
683 if (c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
684 lprops = scan_for_leb_for_idx(c);
685 if (IS_ERR(lprops)) {
686 err = PTR_ERR(lprops);
687 goto out;
688 }
689 }
690 }
691 }
692
693 if (!lprops) {
694 err = -ENOSPC;
695 goto out;
696 }
697
698 lnum = lprops->lnum;
699
700 dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
701 lnum, lprops->free, lprops->dirty, lprops->flags);
702
703 flags = lprops->flags | LPROPS_TAKEN | LPROPS_INDEX;
704 lprops = ubifs_change_lp(c, lprops, c->leb_size, 0, flags, 0);
705 if (IS_ERR(lprops)) {
706 err = PTR_ERR(lprops);
707 goto out;
708 }
709
710 ubifs_release_lprops(c);
711
712 /*
713 * Ensure that empty LEBs have been unmapped. They may not have been,
714 * for example, because of an unclean unmount. Also LEBs that were
715 * freeable LEBs (free + dirty == leb_size) will not have been unmapped.
716 */
717 err = ubifs_leb_unmap(c, lnum);
718 if (err) {
719 ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
720 LPROPS_TAKEN | LPROPS_INDEX, 0);
721 return err;
722 }
723
724 return lnum;
725
726out:
727 ubifs_release_lprops(c);
728 return err;
729}
730
731static int cmp_dirty_idx(const struct ubifs_lprops **a,
732 const struct ubifs_lprops **b)
733{
734 const struct ubifs_lprops *lpa = *a;
735 const struct ubifs_lprops *lpb = *b;
736
737 return lpa->dirty + lpa->free - lpb->dirty - lpb->free;
738}
739
740static void swap_dirty_idx(struct ubifs_lprops **a, struct ubifs_lprops **b,
741 int size)
742{
743 struct ubifs_lprops *t = *a;
744
745 *a = *b;
746 *b = t;
747}
748
749/**
750 * ubifs_save_dirty_idx_lnums - save an array of the most dirty index LEB nos.
751 * @c: the UBIFS file-system description object
752 *
753 * This function is called each commit to create an array of LEB numbers of
754 * dirty index LEBs sorted in order of dirty and free space. This is used by
755 * the in-the-gaps method of TNC commit.
756 */
757int ubifs_save_dirty_idx_lnums(struct ubifs_info *c)
758{
759 int i;
760
761 ubifs_get_lprops(c);
762 /* Copy the LPROPS_DIRTY_IDX heap */
763 c->dirty_idx.cnt = c->lpt_heap[LPROPS_DIRTY_IDX - 1].cnt;
764 memcpy(c->dirty_idx.arr, c->lpt_heap[LPROPS_DIRTY_IDX - 1].arr,
765 sizeof(void *) * c->dirty_idx.cnt);
766 /* Sort it so that the dirtiest is now at the end */
767 sort(c->dirty_idx.arr, c->dirty_idx.cnt, sizeof(void *),
768 (int (*)(const void *, const void *))cmp_dirty_idx,
769 (void (*)(void *, void *, int))swap_dirty_idx);
770 dbg_find("found %d dirty index LEBs", c->dirty_idx.cnt);
771 if (c->dirty_idx.cnt)
772 dbg_find("dirtiest index LEB is %d with dirty %d and free %d",
773 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->lnum,
774 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->dirty,
775 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->free);
776 /* Replace the lprops pointers with LEB numbers */
777 for (i = 0; i < c->dirty_idx.cnt; i++)
778 c->dirty_idx.arr[i] = (void *)(size_t)c->dirty_idx.arr[i]->lnum;
779 ubifs_release_lprops(c);
780 return 0;
781}
782
783/**
784 * scan_dirty_idx_cb - callback used by the scan for a dirty index LEB.
785 * @c: the UBIFS file-system description object
786 * @lprops: LEB properties to scan
787 * @in_tree: whether the LEB properties are in main memory
788 * @data: information passed to and from the caller of the scan
789 *
790 * This function returns a code that indicates whether the scan should continue
791 * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
792 * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
793 * (%LPT_SCAN_STOP).
794 */
795static int scan_dirty_idx_cb(struct ubifs_info *c,
796 const struct ubifs_lprops *lprops, int in_tree,
797 struct scan_data *data)
798{
799 int ret = LPT_SCAN_CONTINUE;
800
801 /* Exclude LEBs that are currently in use */
802 if (lprops->flags & LPROPS_TAKEN)
803 return LPT_SCAN_CONTINUE;
804 /* Determine whether to add these LEB properties to the tree */
805 if (!in_tree && valuable(c, lprops))
806 ret |= LPT_SCAN_ADD;
807 /* Exclude non-index LEBs */
808 if (!(lprops->flags & LPROPS_INDEX))
809 return ret;
810 /* Exclude LEBs with too little space */
811 if (lprops->free + lprops->dirty < c->min_idx_node_sz)
812 return ret;
813 /* Finally we found space */
814 data->lnum = lprops->lnum;
815 return LPT_SCAN_ADD | LPT_SCAN_STOP;
816}
817
818/**
819 * find_dirty_idx_leb - find a dirty index LEB.
820 * @c: the UBIFS file-system description object
821 *
822 * This function returns LEB number upon success and a negative error code upon
823 * failure. In particular, -ENOSPC is returned if a dirty index LEB is not
824 * found.
825 *
826 * Note that this function scans the entire LPT but it is called very rarely.
827 */
828static int find_dirty_idx_leb(struct ubifs_info *c)
829{
830 const struct ubifs_lprops *lprops;
831 struct ubifs_lpt_heap *heap;
832 struct scan_data data;
833 int err, i, ret;
834
835 /* Check all structures in memory first */
836 data.lnum = -1;
837 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
838 for (i = 0; i < heap->cnt; i++) {
839 lprops = heap->arr[i];
840 ret = scan_dirty_idx_cb(c, lprops, 1, &data);
841 if (ret & LPT_SCAN_STOP)
842 goto found;
843 }
844 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
845 ret = scan_dirty_idx_cb(c, lprops, 1, &data);
846 if (ret & LPT_SCAN_STOP)
847 goto found;
848 }
849 list_for_each_entry(lprops, &c->uncat_list, list) {
850 ret = scan_dirty_idx_cb(c, lprops, 1, &data);
851 if (ret & LPT_SCAN_STOP)
852 goto found;
853 }
854 if (c->pnodes_have >= c->pnode_cnt)
855 /* All pnodes are in memory, so skip scan */
856 return -ENOSPC;
857 err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
858 (ubifs_lpt_scan_callback)scan_dirty_idx_cb,
859 &data);
860 if (err)
861 return err;
862found:
863 ubifs_assert(data.lnum >= c->main_first && data.lnum < c->leb_cnt);
864 c->lscan_lnum = data.lnum;
865 lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
866 if (IS_ERR(lprops))
867 return PTR_ERR(lprops);
868 ubifs_assert(lprops->lnum == data.lnum);
869 ubifs_assert(lprops->free + lprops->dirty >= c->min_idx_node_sz);
870 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
871 ubifs_assert((lprops->flags & LPROPS_INDEX));
872
873 dbg_find("found dirty LEB %d, free %d, dirty %d, flags %#x",
874 lprops->lnum, lprops->free, lprops->dirty, lprops->flags);
875
876 lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC,
877 lprops->flags | LPROPS_TAKEN, 0);
878 if (IS_ERR(lprops))
879 return PTR_ERR(lprops);
880
881 return lprops->lnum;
882}
883
884/**
885 * get_idx_gc_leb - try to get a LEB number from trivial GC.
886 * @c: the UBIFS file-system description object
887 */
888static int get_idx_gc_leb(struct ubifs_info *c)
889{
890 const struct ubifs_lprops *lp;
891 int err, lnum;
892
893 err = ubifs_get_idx_gc_leb(c);
894 if (err < 0)
895 return err;
896 lnum = err;
897 /*
898 * The LEB was due to be unmapped after the commit but
899 * it is needed now for this commit.
900 */
901 lp = ubifs_lpt_lookup_dirty(c, lnum);
902 if (unlikely(IS_ERR(lp)))
903 return PTR_ERR(lp);
904 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
905 lp->flags | LPROPS_INDEX, -1);
906 if (unlikely(IS_ERR(lp)))
907 return PTR_ERR(lp);
908 dbg_find("LEB %d, dirty %d and free %d flags %#x",
909 lp->lnum, lp->dirty, lp->free, lp->flags);
910 return lnum;
911}
912
913/**
914 * find_dirtiest_idx_leb - find dirtiest index LEB from dirtiest array.
915 * @c: the UBIFS file-system description object
916 */
917static int find_dirtiest_idx_leb(struct ubifs_info *c)
918{
919 const struct ubifs_lprops *lp;
920 int lnum;
921
922 while (1) {
923 if (!c->dirty_idx.cnt)
924 return -ENOSPC;
925 /* The lprops pointers were replaced by LEB numbers */
926 lnum = (size_t)c->dirty_idx.arr[--c->dirty_idx.cnt];
927 lp = ubifs_lpt_lookup(c, lnum);
928 if (IS_ERR(lp))
929 return PTR_ERR(lp);
930 if ((lp->flags & LPROPS_TAKEN) || !(lp->flags & LPROPS_INDEX))
931 continue;
932 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
933 lp->flags | LPROPS_TAKEN, 0);
934 if (IS_ERR(lp))
935 return PTR_ERR(lp);
936 break;
937 }
938 dbg_find("LEB %d, dirty %d and free %d flags %#x", lp->lnum, lp->dirty,
939 lp->free, lp->flags);
940 ubifs_assert(lp->flags | LPROPS_TAKEN);
941 ubifs_assert(lp->flags | LPROPS_INDEX);
942 return lnum;
943}
944
945/**
946 * ubifs_find_dirty_idx_leb - try to find dirtiest index LEB as at last commit.
947 * @c: the UBIFS file-system description object
948 *
949 * This function attempts to find an untaken index LEB with the most free and
950 * dirty space that can be used without overwriting index nodes that were in the
951 * last index committed.
952 */
953int ubifs_find_dirty_idx_leb(struct ubifs_info *c)
954{
955 int err;
956
957 ubifs_get_lprops(c);
958
959 /*
960 * We made an array of the dirtiest index LEB numbers as at the start of
961 * last commit. Try that array first.
962 */
963 err = find_dirtiest_idx_leb(c);
964
965 /* Next try scanning the entire LPT */
966 if (err == -ENOSPC)
967 err = find_dirty_idx_leb(c);
968
969 /* Finally take any index LEBs awaiting trivial GC */
970 if (err == -ENOSPC)
971 err = get_idx_gc_leb(c);
972
973 ubifs_release_lprops(c);
974 return err;
975}
diff --git a/fs/ubifs/gc.c b/fs/ubifs/gc.c
new file mode 100644
index 000000000000..d0f3dac29081
--- /dev/null
+++ b/fs/ubifs/gc.c
@@ -0,0 +1,773 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements garbage collection. The procedure for garbage collection
25 * is different depending on whether a LEB as an index LEB (contains index
26 * nodes) or not. For non-index LEBs, garbage collection finds a LEB which
27 * contains a lot of dirty space (obsolete nodes), and copies the non-obsolete
28 * nodes to the journal, at which point the garbage-collected LEB is free to be
29 * reused. For index LEBs, garbage collection marks the non-obsolete index nodes
30 * dirty in the TNC, and after the next commit, the garbage-collected LEB is
31 * to be reused. Garbage collection will cause the number of dirty index nodes
32 * to grow, however sufficient space is reserved for the index to ensure the
33 * commit will never run out of space.
34 */
35
36#include <linux/pagemap.h>
37#include "ubifs.h"
38
39/*
40 * GC tries to optimize the way it fit nodes to available space, and it sorts
41 * nodes a little. The below constants are watermarks which define "large",
42 * "medium", and "small" nodes.
43 */
44#define MEDIUM_NODE_WM (UBIFS_BLOCK_SIZE / 4)
45#define SMALL_NODE_WM UBIFS_MAX_DENT_NODE_SZ
46
47/*
48 * GC may need to move more then one LEB to make progress. The below constants
49 * define "soft" and "hard" limits on the number of LEBs the garbage collector
50 * may move.
51 */
52#define SOFT_LEBS_LIMIT 4
53#define HARD_LEBS_LIMIT 32
54
55/**
56 * switch_gc_head - switch the garbage collection journal head.
57 * @c: UBIFS file-system description object
58 * @buf: buffer to write
59 * @len: length of the buffer to write
60 * @lnum: LEB number written is returned here
61 * @offs: offset written is returned here
62 *
63 * This function switch the GC head to the next LEB which is reserved in
64 * @c->gc_lnum. Returns %0 in case of success, %-EAGAIN if commit is required,
65 * and other negative error code in case of failures.
66 */
67static int switch_gc_head(struct ubifs_info *c)
68{
69 int err, gc_lnum = c->gc_lnum;
70 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
71
72 ubifs_assert(gc_lnum != -1);
73 dbg_gc("switch GC head from LEB %d:%d to LEB %d (waste %d bytes)",
74 wbuf->lnum, wbuf->offs + wbuf->used, gc_lnum,
75 c->leb_size - wbuf->offs - wbuf->used);
76
77 err = ubifs_wbuf_sync_nolock(wbuf);
78 if (err)
79 return err;
80
81 /*
82 * The GC write-buffer was synchronized, we may safely unmap
83 * 'c->gc_lnum'.
84 */
85 err = ubifs_leb_unmap(c, gc_lnum);
86 if (err)
87 return err;
88
89 err = ubifs_add_bud_to_log(c, GCHD, gc_lnum, 0);
90 if (err)
91 return err;
92
93 c->gc_lnum = -1;
94 err = ubifs_wbuf_seek_nolock(wbuf, gc_lnum, 0, UBI_LONGTERM);
95 return err;
96}
97
98/**
99 * move_nodes - move nodes.
100 * @c: UBIFS file-system description object
101 * @sleb: describes nodes to move
102 *
103 * This function moves valid nodes from data LEB described by @sleb to the GC
104 * journal head. The obsolete nodes are dropped.
105 *
106 * When moving nodes we have to deal with classical bin-packing problem: the
107 * space in the current GC journal head LEB and in @c->gc_lnum are the "bins",
108 * where the nodes in the @sleb->nodes list are the elements which should be
109 * fit optimally to the bins. This function uses the "first fit decreasing"
110 * strategy, although it does not really sort the nodes but just split them on
111 * 3 classes - large, medium, and small, so they are roughly sorted.
112 *
113 * This function returns zero in case of success, %-EAGAIN if commit is
114 * required, and other negative error codes in case of other failures.
115 */
116static int move_nodes(struct ubifs_info *c, struct ubifs_scan_leb *sleb)
117{
118 struct ubifs_scan_node *snod, *tmp;
119 struct list_head large, medium, small;
120 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
121 int avail, err, min = INT_MAX;
122
123 INIT_LIST_HEAD(&large);
124 INIT_LIST_HEAD(&medium);
125 INIT_LIST_HEAD(&small);
126
127 list_for_each_entry_safe(snod, tmp, &sleb->nodes, list) {
128 struct list_head *lst;
129
130 ubifs_assert(snod->type != UBIFS_IDX_NODE);
131 ubifs_assert(snod->type != UBIFS_REF_NODE);
132 ubifs_assert(snod->type != UBIFS_CS_NODE);
133
134 err = ubifs_tnc_has_node(c, &snod->key, 0, sleb->lnum,
135 snod->offs, 0);
136 if (err < 0)
137 goto out;
138
139 lst = &snod->list;
140 list_del(lst);
141 if (!err) {
142 /* The node is obsolete, remove it from the list */
143 kfree(snod);
144 continue;
145 }
146
147 /*
148 * Sort the list of nodes so that large nodes go first, and
149 * small nodes go last.
150 */
151 if (snod->len > MEDIUM_NODE_WM)
152 list_add(lst, &large);
153 else if (snod->len > SMALL_NODE_WM)
154 list_add(lst, &medium);
155 else
156 list_add(lst, &small);
157
158 /* And find the smallest node */
159 if (snod->len < min)
160 min = snod->len;
161 }
162
163 /*
164 * Join the tree lists so that we'd have one roughly sorted list
165 * ('large' will be the head of the joined list).
166 */
167 list_splice(&medium, large.prev);
168 list_splice(&small, large.prev);
169
170 if (wbuf->lnum == -1) {
171 /*
172 * The GC journal head is not set, because it is the first GC
173 * invocation since mount.
174 */
175 err = switch_gc_head(c);
176 if (err)
177 goto out;
178 }
179
180 /* Write nodes to their new location. Use the first-fit strategy */
181 while (1) {
182 avail = c->leb_size - wbuf->offs - wbuf->used;
183 list_for_each_entry_safe(snod, tmp, &large, list) {
184 int new_lnum, new_offs;
185
186 if (avail < min)
187 break;
188
189 if (snod->len > avail)
190 /* This node does not fit */
191 continue;
192
193 cond_resched();
194
195 new_lnum = wbuf->lnum;
196 new_offs = wbuf->offs + wbuf->used;
197 err = ubifs_wbuf_write_nolock(wbuf, snod->node,
198 snod->len);
199 if (err)
200 goto out;
201 err = ubifs_tnc_replace(c, &snod->key, sleb->lnum,
202 snod->offs, new_lnum, new_offs,
203 snod->len);
204 if (err)
205 goto out;
206
207 avail = c->leb_size - wbuf->offs - wbuf->used;
208 list_del(&snod->list);
209 kfree(snod);
210 }
211
212 if (list_empty(&large))
213 break;
214
215 /*
216 * Waste the rest of the space in the LEB and switch to the
217 * next LEB.
218 */
219 err = switch_gc_head(c);
220 if (err)
221 goto out;
222 }
223
224 return 0;
225
226out:
227 list_for_each_entry_safe(snod, tmp, &large, list) {
228 list_del(&snod->list);
229 kfree(snod);
230 }
231 return err;
232}
233
234/**
235 * gc_sync_wbufs - sync write-buffers for GC.
236 * @c: UBIFS file-system description object
237 *
238 * We must guarantee that obsoleting nodes are on flash. Unfortunately they may
239 * be in a write-buffer instead. That is, a node could be written to a
240 * write-buffer, obsoleting another node in a LEB that is GC'd. If that LEB is
241 * erased before the write-buffer is sync'd and then there is an unclean
242 * unmount, then an existing node is lost. To avoid this, we sync all
243 * write-buffers.
244 *
245 * This function returns %0 on success or a negative error code on failure.
246 */
247static int gc_sync_wbufs(struct ubifs_info *c)
248{
249 int err, i;
250
251 for (i = 0; i < c->jhead_cnt; i++) {
252 if (i == GCHD)
253 continue;
254 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
255 if (err)
256 return err;
257 }
258 return 0;
259}
260
261/**
262 * ubifs_garbage_collect_leb - garbage-collect a logical eraseblock.
263 * @c: UBIFS file-system description object
264 * @lp: describes the LEB to garbage collect
265 *
266 * This function garbage-collects an LEB and returns one of the @LEB_FREED,
267 * @LEB_RETAINED, etc positive codes in case of success, %-EAGAIN if commit is
268 * required, and other negative error codes in case of failures.
269 */
270int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp)
271{
272 struct ubifs_scan_leb *sleb;
273 struct ubifs_scan_node *snod;
274 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
275 int err = 0, lnum = lp->lnum;
276
277 ubifs_assert(c->gc_lnum != -1 || wbuf->offs + wbuf->used == 0 ||
278 c->need_recovery);
279 ubifs_assert(c->gc_lnum != lnum);
280 ubifs_assert(wbuf->lnum != lnum);
281
282 /*
283 * We scan the entire LEB even though we only really need to scan up to
284 * (c->leb_size - lp->free).
285 */
286 sleb = ubifs_scan(c, lnum, 0, c->sbuf);
287 if (IS_ERR(sleb))
288 return PTR_ERR(sleb);
289
290 ubifs_assert(!list_empty(&sleb->nodes));
291 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
292
293 if (snod->type == UBIFS_IDX_NODE) {
294 struct ubifs_gced_idx_leb *idx_gc;
295
296 dbg_gc("indexing LEB %d (free %d, dirty %d)",
297 lnum, lp->free, lp->dirty);
298 list_for_each_entry(snod, &sleb->nodes, list) {
299 struct ubifs_idx_node *idx = snod->node;
300 int level = le16_to_cpu(idx->level);
301
302 ubifs_assert(snod->type == UBIFS_IDX_NODE);
303 key_read(c, ubifs_idx_key(c, idx), &snod->key);
304 err = ubifs_dirty_idx_node(c, &snod->key, level, lnum,
305 snod->offs);
306 if (err)
307 goto out;
308 }
309
310 idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
311 if (!idx_gc) {
312 err = -ENOMEM;
313 goto out;
314 }
315
316 idx_gc->lnum = lnum;
317 idx_gc->unmap = 0;
318 list_add(&idx_gc->list, &c->idx_gc);
319
320 /*
321 * Don't release the LEB until after the next commit, because
322 * it may contain date which is needed for recovery. So
323 * although we freed this LEB, it will become usable only after
324 * the commit.
325 */
326 err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0,
327 LPROPS_INDEX, 1);
328 if (err)
329 goto out;
330 err = LEB_FREED_IDX;
331 } else {
332 dbg_gc("data LEB %d (free %d, dirty %d)",
333 lnum, lp->free, lp->dirty);
334
335 err = move_nodes(c, sleb);
336 if (err)
337 goto out;
338
339 err = gc_sync_wbufs(c);
340 if (err)
341 goto out;
342
343 err = ubifs_change_one_lp(c, lnum, c->leb_size, 0, 0, 0, 0);
344 if (err)
345 goto out;
346
347 if (c->gc_lnum == -1) {
348 c->gc_lnum = lnum;
349 err = LEB_RETAINED;
350 } else {
351 err = ubifs_wbuf_sync_nolock(wbuf);
352 if (err)
353 goto out;
354
355 err = ubifs_leb_unmap(c, lnum);
356 if (err)
357 goto out;
358
359 err = LEB_FREED;
360 }
361 }
362
363out:
364 ubifs_scan_destroy(sleb);
365 return err;
366}
367
368/**
369 * ubifs_garbage_collect - UBIFS garbage collector.
370 * @c: UBIFS file-system description object
371 * @anyway: do GC even if there are free LEBs
372 *
373 * This function does out-of-place garbage collection. The return codes are:
374 * o positive LEB number if the LEB has been freed and may be used;
375 * o %-EAGAIN if the caller has to run commit;
376 * o %-ENOSPC if GC failed to make any progress;
377 * o other negative error codes in case of other errors.
378 *
379 * Garbage collector writes data to the journal when GC'ing data LEBs, and just
380 * marking indexing nodes dirty when GC'ing indexing LEBs. Thus, at some point
381 * commit may be required. But commit cannot be run from inside GC, because the
382 * caller might be holding the commit lock, so %-EAGAIN is returned instead;
383 * And this error code means that the caller has to run commit, and re-run GC
384 * if there is still no free space.
385 *
386 * There are many reasons why this function may return %-EAGAIN:
387 * o the log is full and there is no space to write an LEB reference for
388 * @c->gc_lnum;
389 * o the journal is too large and exceeds size limitations;
390 * o GC moved indexing LEBs, but they can be used only after the commit;
391 * o the shrinker fails to find clean znodes to free and requests the commit;
392 * o etc.
393 *
394 * Note, if the file-system is close to be full, this function may return
395 * %-EAGAIN infinitely, so the caller has to limit amount of re-invocations of
396 * the function. E.g., this happens if the limits on the journal size are too
397 * tough and GC writes too much to the journal before an LEB is freed. This
398 * might also mean that the journal is too large, and the TNC becomes to big,
399 * so that the shrinker is constantly called, finds not clean znodes to free,
400 * and requests commit. Well, this may also happen if the journal is all right,
401 * but another kernel process consumes too much memory. Anyway, infinite
402 * %-EAGAIN may happen, but in some extreme/misconfiguration cases.
403 */
404int ubifs_garbage_collect(struct ubifs_info *c, int anyway)
405{
406 int i, err, ret, min_space = c->dead_wm;
407 struct ubifs_lprops lp;
408 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
409
410 ubifs_assert_cmt_locked(c);
411
412 if (ubifs_gc_should_commit(c))
413 return -EAGAIN;
414
415 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
416
417 if (c->ro_media) {
418 ret = -EROFS;
419 goto out_unlock;
420 }
421
422 /* We expect the write-buffer to be empty on entry */
423 ubifs_assert(!wbuf->used);
424
425 for (i = 0; ; i++) {
426 int space_before = c->leb_size - wbuf->offs - wbuf->used;
427 int space_after;
428
429 cond_resched();
430
431 /* Give the commit an opportunity to run */
432 if (ubifs_gc_should_commit(c)) {
433 ret = -EAGAIN;
434 break;
435 }
436
437 if (i > SOFT_LEBS_LIMIT && !list_empty(&c->idx_gc)) {
438 /*
439 * We've done enough iterations. Indexing LEBs were
440 * moved and will be available after the commit.
441 */
442 dbg_gc("soft limit, some index LEBs GC'ed, -EAGAIN");
443 ubifs_commit_required(c);
444 ret = -EAGAIN;
445 break;
446 }
447
448 if (i > HARD_LEBS_LIMIT) {
449 /*
450 * We've moved too many LEBs and have not made
451 * progress, give up.
452 */
453 dbg_gc("hard limit, -ENOSPC");
454 ret = -ENOSPC;
455 break;
456 }
457
458 /*
459 * Empty and freeable LEBs can turn up while we waited for
460 * the wbuf lock, or while we have been running GC. In that
461 * case, we should just return one of those instead of
462 * continuing to GC dirty LEBs. Hence we request
463 * 'ubifs_find_dirty_leb()' to return an empty LEB if it can.
464 */
465 ret = ubifs_find_dirty_leb(c, &lp, min_space, anyway ? 0 : 1);
466 if (ret) {
467 if (ret == -ENOSPC)
468 dbg_gc("no more dirty LEBs");
469 break;
470 }
471
472 dbg_gc("found LEB %d: free %d, dirty %d, sum %d "
473 "(min. space %d)", lp.lnum, lp.free, lp.dirty,
474 lp.free + lp.dirty, min_space);
475
476 if (lp.free + lp.dirty == c->leb_size) {
477 /* An empty LEB was returned */
478 dbg_gc("LEB %d is free, return it", lp.lnum);
479 /*
480 * ubifs_find_dirty_leb() doesn't return freeable index
481 * LEBs.
482 */
483 ubifs_assert(!(lp.flags & LPROPS_INDEX));
484 if (lp.free != c->leb_size) {
485 /*
486 * Write buffers must be sync'd before
487 * unmapping freeable LEBs, because one of them
488 * may contain data which obsoletes something
489 * in 'lp.pnum'.
490 */
491 ret = gc_sync_wbufs(c);
492 if (ret)
493 goto out;
494 ret = ubifs_change_one_lp(c, lp.lnum,
495 c->leb_size, 0, 0, 0,
496 0);
497 if (ret)
498 goto out;
499 }
500 ret = ubifs_leb_unmap(c, lp.lnum);
501 if (ret)
502 goto out;
503 ret = lp.lnum;
504 break;
505 }
506
507 space_before = c->leb_size - wbuf->offs - wbuf->used;
508 if (wbuf->lnum == -1)
509 space_before = 0;
510
511 ret = ubifs_garbage_collect_leb(c, &lp);
512 if (ret < 0) {
513 if (ret == -EAGAIN || ret == -ENOSPC) {
514 /*
515 * These codes are not errors, so we have to
516 * return the LEB to lprops. But if the
517 * 'ubifs_return_leb()' function fails, its
518 * failure code is propagated to the caller
519 * instead of the original '-EAGAIN' or
520 * '-ENOSPC'.
521 */
522 err = ubifs_return_leb(c, lp.lnum);
523 if (err)
524 ret = err;
525 break;
526 }
527 goto out;
528 }
529
530 if (ret == LEB_FREED) {
531 /* An LEB has been freed and is ready for use */
532 dbg_gc("LEB %d freed, return", lp.lnum);
533 ret = lp.lnum;
534 break;
535 }
536
537 if (ret == LEB_FREED_IDX) {
538 /*
539 * This was an indexing LEB and it cannot be
540 * immediately used. And instead of requesting the
541 * commit straight away, we try to garbage collect some
542 * more.
543 */
544 dbg_gc("indexing LEB %d freed, continue", lp.lnum);
545 continue;
546 }
547
548 ubifs_assert(ret == LEB_RETAINED);
549 space_after = c->leb_size - wbuf->offs - wbuf->used;
550 dbg_gc("LEB %d retained, freed %d bytes", lp.lnum,
551 space_after - space_before);
552
553 if (space_after > space_before) {
554 /* GC makes progress, keep working */
555 min_space >>= 1;
556 if (min_space < c->dead_wm)
557 min_space = c->dead_wm;
558 continue;
559 }
560
561 dbg_gc("did not make progress");
562
563 /*
564 * GC moved an LEB bud have not done any progress. This means
565 * that the previous GC head LEB contained too few free space
566 * and the LEB which was GC'ed contained only large nodes which
567 * did not fit that space.
568 *
569 * We can do 2 things:
570 * 1. pick another LEB in a hope it'll contain a small node
571 * which will fit the space we have at the end of current GC
572 * head LEB, but there is no guarantee, so we try this out
573 * unless we have already been working for too long;
574 * 2. request an LEB with more dirty space, which will force
575 * 'ubifs_find_dirty_leb()' to start scanning the lprops
576 * table, instead of just picking one from the heap
577 * (previously it already picked the dirtiest LEB).
578 */
579 if (i < SOFT_LEBS_LIMIT) {
580 dbg_gc("try again");
581 continue;
582 }
583
584 min_space <<= 1;
585 if (min_space > c->dark_wm)
586 min_space = c->dark_wm;
587 dbg_gc("set min. space to %d", min_space);
588 }
589
590 if (ret == -ENOSPC && !list_empty(&c->idx_gc)) {
591 dbg_gc("no space, some index LEBs GC'ed, -EAGAIN");
592 ubifs_commit_required(c);
593 ret = -EAGAIN;
594 }
595
596 err = ubifs_wbuf_sync_nolock(wbuf);
597 if (!err)
598 err = ubifs_leb_unmap(c, c->gc_lnum);
599 if (err) {
600 ret = err;
601 goto out;
602 }
603out_unlock:
604 mutex_unlock(&wbuf->io_mutex);
605 return ret;
606
607out:
608 ubifs_assert(ret < 0);
609 ubifs_assert(ret != -ENOSPC && ret != -EAGAIN);
610 ubifs_ro_mode(c, ret);
611 ubifs_wbuf_sync_nolock(wbuf);
612 mutex_unlock(&wbuf->io_mutex);
613 ubifs_return_leb(c, lp.lnum);
614 return ret;
615}
616
617/**
618 * ubifs_gc_start_commit - garbage collection at start of commit.
619 * @c: UBIFS file-system description object
620 *
621 * If a LEB has only dirty and free space, then we may safely unmap it and make
622 * it free. Note, we cannot do this with indexing LEBs because dirty space may
623 * correspond index nodes that are required for recovery. In that case, the
624 * LEB cannot be unmapped until after the next commit.
625 *
626 * This function returns %0 upon success and a negative error code upon failure.
627 */
628int ubifs_gc_start_commit(struct ubifs_info *c)
629{
630 struct ubifs_gced_idx_leb *idx_gc;
631 const struct ubifs_lprops *lp;
632 int err = 0, flags;
633
634 ubifs_get_lprops(c);
635
636 /*
637 * Unmap (non-index) freeable LEBs. Note that recovery requires that all
638 * wbufs are sync'd before this, which is done in 'do_commit()'.
639 */
640 while (1) {
641 lp = ubifs_fast_find_freeable(c);
642 if (unlikely(IS_ERR(lp))) {
643 err = PTR_ERR(lp);
644 goto out;
645 }
646 if (!lp)
647 break;
648 ubifs_assert(!(lp->flags & LPROPS_TAKEN));
649 ubifs_assert(!(lp->flags & LPROPS_INDEX));
650 err = ubifs_leb_unmap(c, lp->lnum);
651 if (err)
652 goto out;
653 lp = ubifs_change_lp(c, lp, c->leb_size, 0, lp->flags, 0);
654 if (unlikely(IS_ERR(lp))) {
655 err = PTR_ERR(lp);
656 goto out;
657 }
658 ubifs_assert(!(lp->flags & LPROPS_TAKEN));
659 ubifs_assert(!(lp->flags & LPROPS_INDEX));
660 }
661
662 /* Mark GC'd index LEBs OK to unmap after this commit finishes */
663 list_for_each_entry(idx_gc, &c->idx_gc, list)
664 idx_gc->unmap = 1;
665
666 /* Record index freeable LEBs for unmapping after commit */
667 while (1) {
668 lp = ubifs_fast_find_frdi_idx(c);
669 if (unlikely(IS_ERR(lp))) {
670 err = PTR_ERR(lp);
671 goto out;
672 }
673 if (!lp)
674 break;
675 idx_gc = kmalloc(sizeof(struct ubifs_gced_idx_leb), GFP_NOFS);
676 if (!idx_gc) {
677 err = -ENOMEM;
678 goto out;
679 }
680 ubifs_assert(!(lp->flags & LPROPS_TAKEN));
681 ubifs_assert(lp->flags & LPROPS_INDEX);
682 /* Don't release the LEB until after the next commit */
683 flags = (lp->flags | LPROPS_TAKEN) ^ LPROPS_INDEX;
684 lp = ubifs_change_lp(c, lp, c->leb_size, 0, flags, 1);
685 if (unlikely(IS_ERR(lp))) {
686 err = PTR_ERR(lp);
687 kfree(idx_gc);
688 goto out;
689 }
690 ubifs_assert(lp->flags & LPROPS_TAKEN);
691 ubifs_assert(!(lp->flags & LPROPS_INDEX));
692 idx_gc->lnum = lp->lnum;
693 idx_gc->unmap = 1;
694 list_add(&idx_gc->list, &c->idx_gc);
695 }
696out:
697 ubifs_release_lprops(c);
698 return err;
699}
700
701/**
702 * ubifs_gc_end_commit - garbage collection at end of commit.
703 * @c: UBIFS file-system description object
704 *
705 * This function completes out-of-place garbage collection of index LEBs.
706 */
707int ubifs_gc_end_commit(struct ubifs_info *c)
708{
709 struct ubifs_gced_idx_leb *idx_gc, *tmp;
710 struct ubifs_wbuf *wbuf;
711 int err = 0;
712
713 wbuf = &c->jheads[GCHD].wbuf;
714 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
715 list_for_each_entry_safe(idx_gc, tmp, &c->idx_gc, list)
716 if (idx_gc->unmap) {
717 dbg_gc("LEB %d", idx_gc->lnum);
718 err = ubifs_leb_unmap(c, idx_gc->lnum);
719 if (err)
720 goto out;
721 err = ubifs_change_one_lp(c, idx_gc->lnum, LPROPS_NC,
722 LPROPS_NC, 0, LPROPS_TAKEN, -1);
723 if (err)
724 goto out;
725 list_del(&idx_gc->list);
726 kfree(idx_gc);
727 }
728out:
729 mutex_unlock(&wbuf->io_mutex);
730 return err;
731}
732
733/**
734 * ubifs_destroy_idx_gc - destroy idx_gc list.
735 * @c: UBIFS file-system description object
736 *
737 * This function destroys the idx_gc list. It is called when unmounting or
738 * remounting read-only so locks are not needed.
739 */
740void ubifs_destroy_idx_gc(struct ubifs_info *c)
741{
742 while (!list_empty(&c->idx_gc)) {
743 struct ubifs_gced_idx_leb *idx_gc;
744
745 idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb,
746 list);
747 c->idx_gc_cnt -= 1;
748 list_del(&idx_gc->list);
749 kfree(idx_gc);
750 }
751
752}
753
754/**
755 * ubifs_get_idx_gc_leb - get a LEB from GC'd index LEB list.
756 * @c: UBIFS file-system description object
757 *
758 * Called during start commit so locks are not needed.
759 */
760int ubifs_get_idx_gc_leb(struct ubifs_info *c)
761{
762 struct ubifs_gced_idx_leb *idx_gc;
763 int lnum;
764
765 if (list_empty(&c->idx_gc))
766 return -ENOSPC;
767 idx_gc = list_entry(c->idx_gc.next, struct ubifs_gced_idx_leb, list);
768 lnum = idx_gc->lnum;
769 /* c->idx_gc_cnt is updated by the caller when lprops are updated */
770 list_del(&idx_gc->list);
771 kfree(idx_gc);
772 return lnum;
773}
diff --git a/fs/ubifs/io.c b/fs/ubifs/io.c
new file mode 100644
index 000000000000..3374f91b6709
--- /dev/null
+++ b/fs/ubifs/io.c
@@ -0,0 +1,914 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors: Artem Bityutskiy (Битюцкий Артём)
21 * Adrian Hunter
22 * Zoltan Sogor
23 */
24
25/*
26 * This file implements UBIFS I/O subsystem which provides various I/O-related
27 * helper functions (reading/writing/checking/validating nodes) and implements
28 * write-buffering support. Write buffers help to save space which otherwise
29 * would have been wasted for padding to the nearest minimal I/O unit boundary.
30 * Instead, data first goes to the write-buffer and is flushed when the
31 * buffer is full or when it is not used for some time (by timer). This is
32 * similarto the mechanism is used by JFFS2.
33 *
34 * Write-buffers are defined by 'struct ubifs_wbuf' objects and protected by
35 * mutexes defined inside these objects. Since sometimes upper-level code
36 * has to lock the write-buffer (e.g. journal space reservation code), many
37 * functions related to write-buffers have "nolock" suffix which means that the
38 * caller has to lock the write-buffer before calling this function.
39 *
40 * UBIFS stores nodes at 64 bit-aligned addresses. If the node length is not
41 * aligned, UBIFS starts the next node from the aligned address, and the padded
42 * bytes may contain any rubbish. In other words, UBIFS does not put padding
43 * bytes in those small gaps. Common headers of nodes store real node lengths,
44 * not aligned lengths. Indexing nodes also store real lengths in branches.
45 *
46 * UBIFS uses padding when it pads to the next min. I/O unit. In this case it
47 * uses padding nodes or padding bytes, if the padding node does not fit.
48 *
49 * All UBIFS nodes are protected by CRC checksums and UBIFS checks all nodes
50 * every time they are read from the flash media.
51 */
52
53#include <linux/crc32.h>
54#include "ubifs.h"
55
56/**
57 * ubifs_check_node - check node.
58 * @c: UBIFS file-system description object
59 * @buf: node to check
60 * @lnum: logical eraseblock number
61 * @offs: offset within the logical eraseblock
62 * @quiet: print no messages
63 *
64 * This function checks node magic number and CRC checksum. This function also
65 * validates node length to prevent UBIFS from becoming crazy when an attacker
66 * feeds it a file-system image with incorrect nodes. For example, too large
67 * node length in the common header could cause UBIFS to read memory outside of
68 * allocated buffer when checking the CRC checksum.
69 *
70 * This function returns zero in case of success %-EUCLEAN in case of bad CRC
71 * or magic.
72 */
73int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
74 int offs, int quiet)
75{
76 int err = -EINVAL, type, node_len;
77 uint32_t crc, node_crc, magic;
78 const struct ubifs_ch *ch = buf;
79
80 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
81 ubifs_assert(!(offs & 7) && offs < c->leb_size);
82
83 magic = le32_to_cpu(ch->magic);
84 if (magic != UBIFS_NODE_MAGIC) {
85 if (!quiet)
86 ubifs_err("bad magic %#08x, expected %#08x",
87 magic, UBIFS_NODE_MAGIC);
88 err = -EUCLEAN;
89 goto out;
90 }
91
92 type = ch->node_type;
93 if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) {
94 if (!quiet)
95 ubifs_err("bad node type %d", type);
96 goto out;
97 }
98
99 node_len = le32_to_cpu(ch->len);
100 if (node_len + offs > c->leb_size)
101 goto out_len;
102
103 if (c->ranges[type].max_len == 0) {
104 if (node_len != c->ranges[type].len)
105 goto out_len;
106 } else if (node_len < c->ranges[type].min_len ||
107 node_len > c->ranges[type].max_len)
108 goto out_len;
109
110 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
111 node_crc = le32_to_cpu(ch->crc);
112 if (crc != node_crc) {
113 if (!quiet)
114 ubifs_err("bad CRC: calculated %#08x, read %#08x",
115 crc, node_crc);
116 err = -EUCLEAN;
117 goto out;
118 }
119
120 return 0;
121
122out_len:
123 if (!quiet)
124 ubifs_err("bad node length %d", node_len);
125out:
126 if (!quiet) {
127 ubifs_err("bad node at LEB %d:%d", lnum, offs);
128 dbg_dump_node(c, buf);
129 dbg_dump_stack();
130 }
131 return err;
132}
133
134/**
135 * ubifs_pad - pad flash space.
136 * @c: UBIFS file-system description object
137 * @buf: buffer to put padding to
138 * @pad: how many bytes to pad
139 *
140 * The flash media obliges us to write only in chunks of %c->min_io_size and
141 * when we have to write less data we add padding node to the write-buffer and
142 * pad it to the next minimal I/O unit's boundary. Padding nodes help when the
143 * media is being scanned. If the amount of wasted space is not enough to fit a
144 * padding node which takes %UBIFS_PAD_NODE_SZ bytes, we write padding bytes
145 * pattern (%UBIFS_PADDING_BYTE).
146 *
147 * Padding nodes are also used to fill gaps when the "commit-in-gaps" method is
148 * used.
149 */
150void ubifs_pad(const struct ubifs_info *c, void *buf, int pad)
151{
152 uint32_t crc;
153
154 ubifs_assert(pad >= 0 && !(pad & 7));
155
156 if (pad >= UBIFS_PAD_NODE_SZ) {
157 struct ubifs_ch *ch = buf;
158 struct ubifs_pad_node *pad_node = buf;
159
160 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
161 ch->node_type = UBIFS_PAD_NODE;
162 ch->group_type = UBIFS_NO_NODE_GROUP;
163 ch->padding[0] = ch->padding[1] = 0;
164 ch->sqnum = 0;
165 ch->len = cpu_to_le32(UBIFS_PAD_NODE_SZ);
166 pad -= UBIFS_PAD_NODE_SZ;
167 pad_node->pad_len = cpu_to_le32(pad);
168 crc = crc32(UBIFS_CRC32_INIT, buf + 8, UBIFS_PAD_NODE_SZ - 8);
169 ch->crc = cpu_to_le32(crc);
170 memset(buf + UBIFS_PAD_NODE_SZ, 0, pad);
171 } else if (pad > 0)
172 /* Too little space, padding node won't fit */
173 memset(buf, UBIFS_PADDING_BYTE, pad);
174}
175
176/**
177 * next_sqnum - get next sequence number.
178 * @c: UBIFS file-system description object
179 */
180static unsigned long long next_sqnum(struct ubifs_info *c)
181{
182 unsigned long long sqnum;
183
184 spin_lock(&c->cnt_lock);
185 sqnum = ++c->max_sqnum;
186 spin_unlock(&c->cnt_lock);
187
188 if (unlikely(sqnum >= SQNUM_WARN_WATERMARK)) {
189 if (sqnum >= SQNUM_WATERMARK) {
190 ubifs_err("sequence number overflow %llu, end of life",
191 sqnum);
192 ubifs_ro_mode(c, -EINVAL);
193 }
194 ubifs_warn("running out of sequence numbers, end of life soon");
195 }
196
197 return sqnum;
198}
199
200/**
201 * ubifs_prepare_node - prepare node to be written to flash.
202 * @c: UBIFS file-system description object
203 * @node: the node to pad
204 * @len: node length
205 * @pad: if the buffer has to be padded
206 *
207 * This function prepares node at @node to be written to the media - it
208 * calculates node CRC, fills the common header, and adds proper padding up to
209 * the next minimum I/O unit if @pad is not zero.
210 */
211void ubifs_prepare_node(struct ubifs_info *c, void *node, int len, int pad)
212{
213 uint32_t crc;
214 struct ubifs_ch *ch = node;
215 unsigned long long sqnum = next_sqnum(c);
216
217 ubifs_assert(len >= UBIFS_CH_SZ);
218
219 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
220 ch->len = cpu_to_le32(len);
221 ch->group_type = UBIFS_NO_NODE_GROUP;
222 ch->sqnum = cpu_to_le64(sqnum);
223 ch->padding[0] = ch->padding[1] = 0;
224 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
225 ch->crc = cpu_to_le32(crc);
226
227 if (pad) {
228 len = ALIGN(len, 8);
229 pad = ALIGN(len, c->min_io_size) - len;
230 ubifs_pad(c, node + len, pad);
231 }
232}
233
234/**
235 * ubifs_prep_grp_node - prepare node of a group to be written to flash.
236 * @c: UBIFS file-system description object
237 * @node: the node to pad
238 * @len: node length
239 * @last: indicates the last node of the group
240 *
241 * This function prepares node at @node to be written to the media - it
242 * calculates node CRC and fills the common header.
243 */
244void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last)
245{
246 uint32_t crc;
247 struct ubifs_ch *ch = node;
248 unsigned long long sqnum = next_sqnum(c);
249
250 ubifs_assert(len >= UBIFS_CH_SZ);
251
252 ch->magic = cpu_to_le32(UBIFS_NODE_MAGIC);
253 ch->len = cpu_to_le32(len);
254 if (last)
255 ch->group_type = UBIFS_LAST_OF_NODE_GROUP;
256 else
257 ch->group_type = UBIFS_IN_NODE_GROUP;
258 ch->sqnum = cpu_to_le64(sqnum);
259 ch->padding[0] = ch->padding[1] = 0;
260 crc = crc32(UBIFS_CRC32_INIT, node + 8, len - 8);
261 ch->crc = cpu_to_le32(crc);
262}
263
264/**
265 * wbuf_timer_callback - write-buffer timer callback function.
266 * @data: timer data (write-buffer descriptor)
267 *
268 * This function is called when the write-buffer timer expires.
269 */
270static void wbuf_timer_callback_nolock(unsigned long data)
271{
272 struct ubifs_wbuf *wbuf = (struct ubifs_wbuf *)data;
273
274 wbuf->need_sync = 1;
275 wbuf->c->need_wbuf_sync = 1;
276 ubifs_wake_up_bgt(wbuf->c);
277}
278
279/**
280 * new_wbuf_timer - start new write-buffer timer.
281 * @wbuf: write-buffer descriptor
282 */
283static void new_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
284{
285 ubifs_assert(!timer_pending(&wbuf->timer));
286
287 if (!wbuf->timeout)
288 return;
289
290 wbuf->timer.expires = jiffies + wbuf->timeout;
291 add_timer(&wbuf->timer);
292}
293
294/**
295 * cancel_wbuf_timer - cancel write-buffer timer.
296 * @wbuf: write-buffer descriptor
297 */
298static void cancel_wbuf_timer_nolock(struct ubifs_wbuf *wbuf)
299{
300 /*
301 * If the syncer is waiting for the lock (from the background thread's
302 * context) and another task is changing write-buffer then the syncing
303 * should be canceled.
304 */
305 wbuf->need_sync = 0;
306 del_timer(&wbuf->timer);
307}
308
309/**
310 * ubifs_wbuf_sync_nolock - synchronize write-buffer.
311 * @wbuf: write-buffer to synchronize
312 *
313 * This function synchronizes write-buffer @buf and returns zero in case of
314 * success or a negative error code in case of failure.
315 */
316int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf)
317{
318 struct ubifs_info *c = wbuf->c;
319 int err, dirt;
320
321 cancel_wbuf_timer_nolock(wbuf);
322 if (!wbuf->used || wbuf->lnum == -1)
323 /* Write-buffer is empty or not seeked */
324 return 0;
325
326 dbg_io("LEB %d:%d, %d bytes",
327 wbuf->lnum, wbuf->offs, wbuf->used);
328 ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY));
329 ubifs_assert(!(wbuf->avail & 7));
330 ubifs_assert(wbuf->offs + c->min_io_size <= c->leb_size);
331
332 if (c->ro_media)
333 return -EROFS;
334
335 ubifs_pad(c, wbuf->buf + wbuf->used, wbuf->avail);
336 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
337 c->min_io_size, wbuf->dtype);
338 if (err) {
339 ubifs_err("cannot write %d bytes to LEB %d:%d",
340 c->min_io_size, wbuf->lnum, wbuf->offs);
341 dbg_dump_stack();
342 return err;
343 }
344
345 dirt = wbuf->avail;
346
347 spin_lock(&wbuf->lock);
348 wbuf->offs += c->min_io_size;
349 wbuf->avail = c->min_io_size;
350 wbuf->used = 0;
351 wbuf->next_ino = 0;
352 spin_unlock(&wbuf->lock);
353
354 if (wbuf->sync_callback)
355 err = wbuf->sync_callback(c, wbuf->lnum,
356 c->leb_size - wbuf->offs, dirt);
357 return err;
358}
359
360/**
361 * ubifs_wbuf_seek_nolock - seek write-buffer.
362 * @wbuf: write-buffer
363 * @lnum: logical eraseblock number to seek to
364 * @offs: logical eraseblock offset to seek to
365 * @dtype: data type
366 *
367 * This function targets the write buffer to logical eraseblock @lnum:@offs.
368 * The write-buffer is synchronized if it is not empty. Returns zero in case of
369 * success and a negative error code in case of failure.
370 */
371int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
372 int dtype)
373{
374 const struct ubifs_info *c = wbuf->c;
375
376 dbg_io("LEB %d:%d", lnum, offs);
377 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt);
378 ubifs_assert(offs >= 0 && offs <= c->leb_size);
379 ubifs_assert(offs % c->min_io_size == 0 && !(offs & 7));
380 ubifs_assert(lnum != wbuf->lnum);
381
382 if (wbuf->used > 0) {
383 int err = ubifs_wbuf_sync_nolock(wbuf);
384
385 if (err)
386 return err;
387 }
388
389 spin_lock(&wbuf->lock);
390 wbuf->lnum = lnum;
391 wbuf->offs = offs;
392 wbuf->avail = c->min_io_size;
393 wbuf->used = 0;
394 spin_unlock(&wbuf->lock);
395 wbuf->dtype = dtype;
396
397 return 0;
398}
399
400/**
401 * ubifs_bg_wbufs_sync - synchronize write-buffers.
402 * @c: UBIFS file-system description object
403 *
404 * This function is called by background thread to synchronize write-buffers.
405 * Returns zero in case of success and a negative error code in case of
406 * failure.
407 */
408int ubifs_bg_wbufs_sync(struct ubifs_info *c)
409{
410 int err, i;
411
412 if (!c->need_wbuf_sync)
413 return 0;
414 c->need_wbuf_sync = 0;
415
416 if (c->ro_media) {
417 err = -EROFS;
418 goto out_timers;
419 }
420
421 dbg_io("synchronize");
422 for (i = 0; i < c->jhead_cnt; i++) {
423 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
424
425 cond_resched();
426
427 /*
428 * If the mutex is locked then wbuf is being changed, so
429 * synchronization is not necessary.
430 */
431 if (mutex_is_locked(&wbuf->io_mutex))
432 continue;
433
434 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
435 if (!wbuf->need_sync) {
436 mutex_unlock(&wbuf->io_mutex);
437 continue;
438 }
439
440 err = ubifs_wbuf_sync_nolock(wbuf);
441 mutex_unlock(&wbuf->io_mutex);
442 if (err) {
443 ubifs_err("cannot sync write-buffer, error %d", err);
444 ubifs_ro_mode(c, err);
445 goto out_timers;
446 }
447 }
448
449 return 0;
450
451out_timers:
452 /* Cancel all timers to prevent repeated errors */
453 for (i = 0; i < c->jhead_cnt; i++) {
454 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
455
456 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
457 cancel_wbuf_timer_nolock(wbuf);
458 mutex_unlock(&wbuf->io_mutex);
459 }
460 return err;
461}
462
463/**
464 * ubifs_wbuf_write_nolock - write data to flash via write-buffer.
465 * @wbuf: write-buffer
466 * @buf: node to write
467 * @len: node length
468 *
469 * This function writes data to flash via write-buffer @wbuf. This means that
470 * the last piece of the node won't reach the flash media immediately if it
471 * does not take whole minimal I/O unit. Instead, the node will sit in RAM
472 * until the write-buffer is synchronized (e.g., by timer).
473 *
474 * This function returns zero in case of success and a negative error code in
475 * case of failure. If the node cannot be written because there is no more
476 * space in this logical eraseblock, %-ENOSPC is returned.
477 */
478int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len)
479{
480 struct ubifs_info *c = wbuf->c;
481 int err, written, n, aligned_len = ALIGN(len, 8), offs;
482
483 dbg_io("%d bytes (%s) to wbuf at LEB %d:%d", len,
484 dbg_ntype(((struct ubifs_ch *)buf)->node_type), wbuf->lnum,
485 wbuf->offs + wbuf->used);
486 ubifs_assert(len > 0 && wbuf->lnum >= 0 && wbuf->lnum < c->leb_cnt);
487 ubifs_assert(wbuf->offs >= 0 && wbuf->offs % c->min_io_size == 0);
488 ubifs_assert(!(wbuf->offs & 7) && wbuf->offs <= c->leb_size);
489 ubifs_assert(wbuf->avail > 0 && wbuf->avail <= c->min_io_size);
490 ubifs_assert(mutex_is_locked(&wbuf->io_mutex));
491
492 if (c->leb_size - wbuf->offs - wbuf->used < aligned_len) {
493 err = -ENOSPC;
494 goto out;
495 }
496
497 cancel_wbuf_timer_nolock(wbuf);
498
499 if (c->ro_media)
500 return -EROFS;
501
502 if (aligned_len <= wbuf->avail) {
503 /*
504 * The node is not very large and fits entirely within
505 * write-buffer.
506 */
507 memcpy(wbuf->buf + wbuf->used, buf, len);
508
509 if (aligned_len == wbuf->avail) {
510 dbg_io("flush wbuf to LEB %d:%d", wbuf->lnum,
511 wbuf->offs);
512 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf,
513 wbuf->offs, c->min_io_size,
514 wbuf->dtype);
515 if (err)
516 goto out;
517
518 spin_lock(&wbuf->lock);
519 wbuf->offs += c->min_io_size;
520 wbuf->avail = c->min_io_size;
521 wbuf->used = 0;
522 wbuf->next_ino = 0;
523 spin_unlock(&wbuf->lock);
524 } else {
525 spin_lock(&wbuf->lock);
526 wbuf->avail -= aligned_len;
527 wbuf->used += aligned_len;
528 spin_unlock(&wbuf->lock);
529 }
530
531 goto exit;
532 }
533
534 /*
535 * The node is large enough and does not fit entirely within current
536 * minimal I/O unit. We have to fill and flush write-buffer and switch
537 * to the next min. I/O unit.
538 */
539 dbg_io("flush wbuf to LEB %d:%d", wbuf->lnum, wbuf->offs);
540 memcpy(wbuf->buf + wbuf->used, buf, wbuf->avail);
541 err = ubi_leb_write(c->ubi, wbuf->lnum, wbuf->buf, wbuf->offs,
542 c->min_io_size, wbuf->dtype);
543 if (err)
544 goto out;
545
546 offs = wbuf->offs + c->min_io_size;
547 len -= wbuf->avail;
548 aligned_len -= wbuf->avail;
549 written = wbuf->avail;
550
551 /*
552 * The remaining data may take more whole min. I/O units, so write the
553 * remains multiple to min. I/O unit size directly to the flash media.
554 * We align node length to 8-byte boundary because we anyway flash wbuf
555 * if the remaining space is less than 8 bytes.
556 */
557 n = aligned_len >> c->min_io_shift;
558 if (n) {
559 n <<= c->min_io_shift;
560 dbg_io("write %d bytes to LEB %d:%d", n, wbuf->lnum, offs);
561 err = ubi_leb_write(c->ubi, wbuf->lnum, buf + written, offs, n,
562 wbuf->dtype);
563 if (err)
564 goto out;
565 offs += n;
566 aligned_len -= n;
567 len -= n;
568 written += n;
569 }
570
571 spin_lock(&wbuf->lock);
572 if (aligned_len)
573 /*
574 * And now we have what's left and what does not take whole
575 * min. I/O unit, so write it to the write-buffer and we are
576 * done.
577 */
578 memcpy(wbuf->buf, buf + written, len);
579
580 wbuf->offs = offs;
581 wbuf->used = aligned_len;
582 wbuf->avail = c->min_io_size - aligned_len;
583 wbuf->next_ino = 0;
584 spin_unlock(&wbuf->lock);
585
586exit:
587 if (wbuf->sync_callback) {
588 int free = c->leb_size - wbuf->offs - wbuf->used;
589
590 err = wbuf->sync_callback(c, wbuf->lnum, free, 0);
591 if (err)
592 goto out;
593 }
594
595 if (wbuf->used)
596 new_wbuf_timer_nolock(wbuf);
597
598 return 0;
599
600out:
601 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
602 len, wbuf->lnum, wbuf->offs, err);
603 dbg_dump_node(c, buf);
604 dbg_dump_stack();
605 dbg_dump_leb(c, wbuf->lnum);
606 return err;
607}
608
609/**
610 * ubifs_write_node - write node to the media.
611 * @c: UBIFS file-system description object
612 * @buf: the node to write
613 * @len: node length
614 * @lnum: logical eraseblock number
615 * @offs: offset within the logical eraseblock
616 * @dtype: node life-time hint (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
617 *
618 * This function automatically fills node magic number, assigns sequence
619 * number, and calculates node CRC checksum. The length of the @buf buffer has
620 * to be aligned to the minimal I/O unit size. This function automatically
621 * appends padding node and padding bytes if needed. Returns zero in case of
622 * success and a negative error code in case of failure.
623 */
624int ubifs_write_node(struct ubifs_info *c, void *buf, int len, int lnum,
625 int offs, int dtype)
626{
627 int err, buf_len = ALIGN(len, c->min_io_size);
628
629 dbg_io("LEB %d:%d, %s, length %d (aligned %d)",
630 lnum, offs, dbg_ntype(((struct ubifs_ch *)buf)->node_type), len,
631 buf_len);
632 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
633 ubifs_assert(offs % c->min_io_size == 0 && offs < c->leb_size);
634
635 if (c->ro_media)
636 return -EROFS;
637
638 ubifs_prepare_node(c, buf, len, 1);
639 err = ubi_leb_write(c->ubi, lnum, buf, offs, buf_len, dtype);
640 if (err) {
641 ubifs_err("cannot write %d bytes to LEB %d:%d, error %d",
642 buf_len, lnum, offs, err);
643 dbg_dump_node(c, buf);
644 dbg_dump_stack();
645 }
646
647 return err;
648}
649
650/**
651 * ubifs_read_node_wbuf - read node from the media or write-buffer.
652 * @wbuf: wbuf to check for un-written data
653 * @buf: buffer to read to
654 * @type: node type
655 * @len: node length
656 * @lnum: logical eraseblock number
657 * @offs: offset within the logical eraseblock
658 *
659 * This function reads a node of known type and length, checks it and stores
660 * in @buf. If the node partially or fully sits in the write-buffer, this
661 * function takes data from the buffer, otherwise it reads the flash media.
662 * Returns zero in case of success, %-EUCLEAN if CRC mismatched and a negative
663 * error code in case of failure.
664 */
665int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
666 int lnum, int offs)
667{
668 const struct ubifs_info *c = wbuf->c;
669 int err, rlen, overlap;
670 struct ubifs_ch *ch = buf;
671
672 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
673 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
674 ubifs_assert(!(offs & 7) && offs < c->leb_size);
675 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
676
677 spin_lock(&wbuf->lock);
678 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
679 if (!overlap) {
680 /* We may safely unlock the write-buffer and read the data */
681 spin_unlock(&wbuf->lock);
682 return ubifs_read_node(c, buf, type, len, lnum, offs);
683 }
684
685 /* Don't read under wbuf */
686 rlen = wbuf->offs - offs;
687 if (rlen < 0)
688 rlen = 0;
689
690 /* Copy the rest from the write-buffer */
691 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
692 spin_unlock(&wbuf->lock);
693
694 if (rlen > 0) {
695 /* Read everything that goes before write-buffer */
696 err = ubi_read(c->ubi, lnum, buf, offs, rlen);
697 if (err && err != -EBADMSG) {
698 ubifs_err("failed to read node %d from LEB %d:%d, "
699 "error %d", type, lnum, offs, err);
700 dbg_dump_stack();
701 return err;
702 }
703 }
704
705 if (type != ch->node_type) {
706 ubifs_err("bad node type (%d but expected %d)",
707 ch->node_type, type);
708 goto out;
709 }
710
711 err = ubifs_check_node(c, buf, lnum, offs, 0);
712 if (err) {
713 ubifs_err("expected node type %d", type);
714 return err;
715 }
716
717 rlen = le32_to_cpu(ch->len);
718 if (rlen != len) {
719 ubifs_err("bad node length %d, expected %d", rlen, len);
720 goto out;
721 }
722
723 return 0;
724
725out:
726 ubifs_err("bad node at LEB %d:%d", lnum, offs);
727 dbg_dump_node(c, buf);
728 dbg_dump_stack();
729 return -EINVAL;
730}
731
732/**
733 * ubifs_read_node - read node.
734 * @c: UBIFS file-system description object
735 * @buf: buffer to read to
736 * @type: node type
737 * @len: node length (not aligned)
738 * @lnum: logical eraseblock number
739 * @offs: offset within the logical eraseblock
740 *
741 * This function reads a node of known type and and length, checks it and
742 * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
743 * and a negative error code in case of failure.
744 */
745int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
746 int lnum, int offs)
747{
748 int err, l;
749 struct ubifs_ch *ch = buf;
750
751 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
752 ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
753 ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
754 ubifs_assert(!(offs & 7) && offs < c->leb_size);
755 ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
756
757 err = ubi_read(c->ubi, lnum, buf, offs, len);
758 if (err && err != -EBADMSG) {
759 ubifs_err("cannot read node %d from LEB %d:%d, error %d",
760 type, lnum, offs, err);
761 return err;
762 }
763
764 if (type != ch->node_type) {
765 ubifs_err("bad node type (%d but expected %d)",
766 ch->node_type, type);
767 goto out;
768 }
769
770 err = ubifs_check_node(c, buf, lnum, offs, 0);
771 if (err) {
772 ubifs_err("expected node type %d", type);
773 return err;
774 }
775
776 l = le32_to_cpu(ch->len);
777 if (l != len) {
778 ubifs_err("bad node length %d, expected %d", l, len);
779 goto out;
780 }
781
782 return 0;
783
784out:
785 ubifs_err("bad node at LEB %d:%d", lnum, offs);
786 dbg_dump_node(c, buf);
787 dbg_dump_stack();
788 return -EINVAL;
789}
790
791/**
792 * ubifs_wbuf_init - initialize write-buffer.
793 * @c: UBIFS file-system description object
794 * @wbuf: write-buffer to initialize
795 *
796 * This function initializes write buffer. Returns zero in case of success
797 * %-ENOMEM in case of failure.
798 */
799int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf)
800{
801 size_t size;
802
803 wbuf->buf = kmalloc(c->min_io_size, GFP_KERNEL);
804 if (!wbuf->buf)
805 return -ENOMEM;
806
807 size = (c->min_io_size / UBIFS_CH_SZ + 1) * sizeof(ino_t);
808 wbuf->inodes = kmalloc(size, GFP_KERNEL);
809 if (!wbuf->inodes) {
810 kfree(wbuf->buf);
811 wbuf->buf = NULL;
812 return -ENOMEM;
813 }
814
815 wbuf->used = 0;
816 wbuf->lnum = wbuf->offs = -1;
817 wbuf->avail = c->min_io_size;
818 wbuf->dtype = UBI_UNKNOWN;
819 wbuf->sync_callback = NULL;
820 mutex_init(&wbuf->io_mutex);
821 spin_lock_init(&wbuf->lock);
822
823 wbuf->c = c;
824 init_timer(&wbuf->timer);
825 wbuf->timer.function = wbuf_timer_callback_nolock;
826 wbuf->timer.data = (unsigned long)wbuf;
827 wbuf->timeout = DEFAULT_WBUF_TIMEOUT;
828 wbuf->next_ino = 0;
829
830 return 0;
831}
832
833/**
834 * ubifs_wbuf_add_ino_nolock - add an inode number into the wbuf inode array.
835 * @wbuf: the write-buffer whereto add
836 * @inum: the inode number
837 *
838 * This function adds an inode number to the inode array of the write-buffer.
839 */
840void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum)
841{
842 if (!wbuf->buf)
843 /* NOR flash or something similar */
844 return;
845
846 spin_lock(&wbuf->lock);
847 if (wbuf->used)
848 wbuf->inodes[wbuf->next_ino++] = inum;
849 spin_unlock(&wbuf->lock);
850}
851
852/**
853 * wbuf_has_ino - returns if the wbuf contains data from the inode.
854 * @wbuf: the write-buffer
855 * @inum: the inode number
856 *
857 * This function returns with %1 if the write-buffer contains some data from the
858 * given inode otherwise it returns with %0.
859 */
860static int wbuf_has_ino(struct ubifs_wbuf *wbuf, ino_t inum)
861{
862 int i, ret = 0;
863
864 spin_lock(&wbuf->lock);
865 for (i = 0; i < wbuf->next_ino; i++)
866 if (inum == wbuf->inodes[i]) {
867 ret = 1;
868 break;
869 }
870 spin_unlock(&wbuf->lock);
871
872 return ret;
873}
874
875/**
876 * ubifs_sync_wbufs_by_inode - synchronize write-buffers for an inode.
877 * @c: UBIFS file-system description object
878 * @inode: inode to synchronize
879 *
880 * This function synchronizes write-buffers which contain nodes belonging to
881 * @inode. Returns zero in case of success and a negative error code in case of
882 * failure.
883 */
884int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode)
885{
886 int i, err = 0;
887
888 for (i = 0; i < c->jhead_cnt; i++) {
889 struct ubifs_wbuf *wbuf = &c->jheads[i].wbuf;
890
891 if (i == GCHD)
892 /*
893 * GC head is special, do not look at it. Even if the
894 * head contains something related to this inode, it is
895 * a _copy_ of corresponding on-flash node which sits
896 * somewhere else.
897 */
898 continue;
899
900 if (!wbuf_has_ino(wbuf, inode->i_ino))
901 continue;
902
903 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
904 if (wbuf_has_ino(wbuf, inode->i_ino))
905 err = ubifs_wbuf_sync_nolock(wbuf);
906 mutex_unlock(&wbuf->io_mutex);
907
908 if (err) {
909 ubifs_ro_mode(c, err);
910 return err;
911 }
912 }
913 return 0;
914}
diff --git a/fs/ubifs/ioctl.c b/fs/ubifs/ioctl.c
new file mode 100644
index 000000000000..5e82cffe9695
--- /dev/null
+++ b/fs/ubifs/ioctl.c
@@ -0,0 +1,204 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors: Zoltan Sogor
21 * Artem Bityutskiy (Битюцкий Артём)
22 * Adrian Hunter
23 */
24
25/* This file implements EXT2-compatible extended attribute ioctl() calls */
26
27#include <linux/compat.h>
28#include <linux/smp_lock.h>
29#include <linux/mount.h>
30#include "ubifs.h"
31
32/**
33 * ubifs_set_inode_flags - set VFS inode flags.
34 * @inode: VFS inode to set flags for
35 *
36 * This function propagates flags from UBIFS inode object to VFS inode object.
37 */
38void ubifs_set_inode_flags(struct inode *inode)
39{
40 unsigned int flags = ubifs_inode(inode)->flags;
41
42 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC);
43 if (flags & UBIFS_SYNC_FL)
44 inode->i_flags |= S_SYNC;
45 if (flags & UBIFS_APPEND_FL)
46 inode->i_flags |= S_APPEND;
47 if (flags & UBIFS_IMMUTABLE_FL)
48 inode->i_flags |= S_IMMUTABLE;
49 if (flags & UBIFS_DIRSYNC_FL)
50 inode->i_flags |= S_DIRSYNC;
51}
52
53/*
54 * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
55 * @ioctl_flags: flags to convert
56 *
57 * This function convert ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
58 * (@UBIFS_COMPR_FL, etc).
59 */
60static int ioctl2ubifs(int ioctl_flags)
61{
62 int ubifs_flags = 0;
63
64 if (ioctl_flags & FS_COMPR_FL)
65 ubifs_flags |= UBIFS_COMPR_FL;
66 if (ioctl_flags & FS_SYNC_FL)
67 ubifs_flags |= UBIFS_SYNC_FL;
68 if (ioctl_flags & FS_APPEND_FL)
69 ubifs_flags |= UBIFS_APPEND_FL;
70 if (ioctl_flags & FS_IMMUTABLE_FL)
71 ubifs_flags |= UBIFS_IMMUTABLE_FL;
72 if (ioctl_flags & FS_DIRSYNC_FL)
73 ubifs_flags |= UBIFS_DIRSYNC_FL;
74
75 return ubifs_flags;
76}
77
78/*
79 * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
80 * @ubifs_flags: flags to convert
81 *
82 * This function convert UBIFS (@UBIFS_COMPR_FL, etc) to ioctl flags
83 * (@FS_COMPR_FL, etc).
84 */
85static int ubifs2ioctl(int ubifs_flags)
86{
87 int ioctl_flags = 0;
88
89 if (ubifs_flags & UBIFS_COMPR_FL)
90 ioctl_flags |= FS_COMPR_FL;
91 if (ubifs_flags & UBIFS_SYNC_FL)
92 ioctl_flags |= FS_SYNC_FL;
93 if (ubifs_flags & UBIFS_APPEND_FL)
94 ioctl_flags |= FS_APPEND_FL;
95 if (ubifs_flags & UBIFS_IMMUTABLE_FL)
96 ioctl_flags |= FS_IMMUTABLE_FL;
97 if (ubifs_flags & UBIFS_DIRSYNC_FL)
98 ioctl_flags |= FS_DIRSYNC_FL;
99
100 return ioctl_flags;
101}
102
103static int setflags(struct inode *inode, int flags)
104{
105 int oldflags, err, release;
106 struct ubifs_inode *ui = ubifs_inode(inode);
107 struct ubifs_info *c = inode->i_sb->s_fs_info;
108 struct ubifs_budget_req req = { .dirtied_ino = 1,
109 .dirtied_ino_d = ui->data_len };
110
111 err = ubifs_budget_space(c, &req);
112 if (err)
113 return err;
114
115 /*
116 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
117 * the relevant capability.
118 */
119 mutex_lock(&ui->ui_mutex);
120 oldflags = ubifs2ioctl(ui->flags);
121 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
122 if (!capable(CAP_LINUX_IMMUTABLE)) {
123 err = -EPERM;
124 goto out_unlock;
125 }
126 }
127
128 ui->flags = ioctl2ubifs(flags);
129 ubifs_set_inode_flags(inode);
130 inode->i_ctime = ubifs_current_time(inode);
131 release = ui->dirty;
132 mark_inode_dirty_sync(inode);
133 mutex_unlock(&ui->ui_mutex);
134
135 if (release)
136 ubifs_release_budget(c, &req);
137 if (IS_SYNC(inode))
138 err = write_inode_now(inode, 1);
139 return err;
140
141out_unlock:
142 ubifs_err("can't modify inode %lu attributes", inode->i_ino);
143 mutex_unlock(&ui->ui_mutex);
144 ubifs_release_budget(c, &req);
145 return err;
146}
147
148long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
149{
150 int flags, err;
151 struct inode *inode = file->f_path.dentry->d_inode;
152
153 switch (cmd) {
154 case FS_IOC_GETFLAGS:
155 flags = ubifs2ioctl(ubifs_inode(inode)->flags);
156
157 return put_user(flags, (int __user *) arg);
158
159 case FS_IOC_SETFLAGS: {
160 if (IS_RDONLY(inode))
161 return -EROFS;
162
163 if (!is_owner_or_cap(inode))
164 return -EACCES;
165
166 if (get_user(flags, (int __user *) arg))
167 return -EFAULT;
168
169 if (!S_ISDIR(inode->i_mode))
170 flags &= ~FS_DIRSYNC_FL;
171
172 /*
173 * Make sure the file-system is read-write and make sure it
174 * will not become read-only while we are changing the flags.
175 */
176 err = mnt_want_write(file->f_path.mnt);
177 if (err)
178 return err;
179 err = setflags(inode, flags);
180 mnt_drop_write(file->f_path.mnt);
181 return err;
182 }
183
184 default:
185 return -ENOTTY;
186 }
187}
188
189#ifdef CONFIG_COMPAT
190long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
191{
192 switch (cmd) {
193 case FS_IOC32_GETFLAGS:
194 cmd = FS_IOC_GETFLAGS;
195 break;
196 case FS_IOC32_SETFLAGS:
197 cmd = FS_IOC_SETFLAGS;
198 break;
199 default:
200 return -ENOIOCTLCMD;
201 }
202 return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
203}
204#endif
diff --git a/fs/ubifs/journal.c b/fs/ubifs/journal.c
new file mode 100644
index 000000000000..283155abe5f5
--- /dev/null
+++ b/fs/ubifs/journal.c
@@ -0,0 +1,1387 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements UBIFS journal.
25 *
26 * The journal consists of 2 parts - the log and bud LEBs. The log has fixed
27 * length and position, while a bud logical eraseblock is any LEB in the main
28 * area. Buds contain file system data - data nodes, inode nodes, etc. The log
29 * contains only references to buds and some other stuff like commit
30 * start node. The idea is that when we commit the journal, we do
31 * not copy the data, the buds just become indexed. Since after the commit the
32 * nodes in bud eraseblocks become leaf nodes of the file system index tree, we
33 * use term "bud". Analogy is obvious, bud eraseblocks contain nodes which will
34 * become leafs in the future.
35 *
36 * The journal is multi-headed because we want to write data to the journal as
37 * optimally as possible. It is nice to have nodes belonging to the same inode
38 * in one LEB, so we may write data owned by different inodes to different
39 * journal heads, although at present only one data head is used.
40 *
41 * For recovery reasons, the base head contains all inode nodes, all directory
42 * entry nodes and all truncate nodes. This means that the other heads contain
43 * only data nodes.
44 *
45 * Bud LEBs may be half-indexed. For example, if the bud was not full at the
46 * time of commit, the bud is retained to continue to be used in the journal,
47 * even though the "front" of the LEB is now indexed. In that case, the log
48 * reference contains the offset where the bud starts for the purposes of the
49 * journal.
50 *
51 * The journal size has to be limited, because the larger is the journal, the
52 * longer it takes to mount UBIFS (scanning the journal) and the more memory it
53 * takes (indexing in the TNC).
54 *
55 * All the journal write operations like 'ubifs_jnl_update()' here, which write
56 * multiple UBIFS nodes to the journal at one go, are atomic with respect to
57 * unclean reboots. Should the unclean reboot happen, the recovery code drops
58 * all the nodes.
59 */
60
61#include "ubifs.h"
62
63/**
64 * zero_ino_node_unused - zero out unused fields of an on-flash inode node.
65 * @ino: the inode to zero out
66 */
67static inline void zero_ino_node_unused(struct ubifs_ino_node *ino)
68{
69 memset(ino->padding1, 0, 4);
70 memset(ino->padding2, 0, 26);
71}
72
73/**
74 * zero_dent_node_unused - zero out unused fields of an on-flash directory
75 * entry node.
76 * @dent: the directory entry to zero out
77 */
78static inline void zero_dent_node_unused(struct ubifs_dent_node *dent)
79{
80 dent->padding1 = 0;
81 memset(dent->padding2, 0, 4);
82}
83
84/**
85 * zero_data_node_unused - zero out unused fields of an on-flash data node.
86 * @data: the data node to zero out
87 */
88static inline void zero_data_node_unused(struct ubifs_data_node *data)
89{
90 memset(data->padding, 0, 2);
91}
92
93/**
94 * zero_trun_node_unused - zero out unused fields of an on-flash truncation
95 * node.
96 * @trun: the truncation node to zero out
97 */
98static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
99{
100 memset(trun->padding, 0, 12);
101}
102
103/**
104 * reserve_space - reserve space in the journal.
105 * @c: UBIFS file-system description object
106 * @jhead: journal head number
107 * @len: node length
108 *
109 * This function reserves space in journal head @head. If the reservation
110 * succeeded, the journal head stays locked and later has to be unlocked using
111 * 'release_head()'. 'write_node()' and 'write_head()' functions also unlock
112 * it. Returns zero in case of success, %-EAGAIN if commit has to be done, and
113 * other negative error codes in case of other failures.
114 */
115static int reserve_space(struct ubifs_info *c, int jhead, int len)
116{
117 int err = 0, err1, retries = 0, avail, lnum, offs, free, squeeze;
118 struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
119
120 /*
121 * Typically, the base head has smaller nodes written to it, so it is
122 * better to try to allocate space at the ends of eraseblocks. This is
123 * what the squeeze parameter does.
124 */
125 squeeze = (jhead == BASEHD);
126again:
127 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
128
129 if (c->ro_media) {
130 err = -EROFS;
131 goto out_unlock;
132 }
133
134 avail = c->leb_size - wbuf->offs - wbuf->used;
135 if (wbuf->lnum != -1 && avail >= len)
136 return 0;
137
138 /*
139 * Write buffer wasn't seek'ed or there is no enough space - look for an
140 * LEB with some empty space.
141 */
142 lnum = ubifs_find_free_space(c, len, &free, squeeze);
143 if (lnum >= 0) {
144 /* Found an LEB, add it to the journal head */
145 offs = c->leb_size - free;
146 err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
147 if (err)
148 goto out_return;
149 /* A new bud was successfully allocated and added to the log */
150 goto out;
151 }
152
153 err = lnum;
154 if (err != -ENOSPC)
155 goto out_unlock;
156
157 /*
158 * No free space, we have to run garbage collector to make
159 * some. But the write-buffer mutex has to be unlocked because
160 * GC also takes it.
161 */
162 dbg_jnl("no free space jhead %d, run GC", jhead);
163 mutex_unlock(&wbuf->io_mutex);
164
165 lnum = ubifs_garbage_collect(c, 0);
166 if (lnum < 0) {
167 err = lnum;
168 if (err != -ENOSPC)
169 return err;
170
171 /*
172 * GC could not make a free LEB. But someone else may
173 * have allocated new bud for this journal head,
174 * because we dropped @wbuf->io_mutex, so try once
175 * again.
176 */
177 dbg_jnl("GC couldn't make a free LEB for jhead %d", jhead);
178 if (retries++ < 2) {
179 dbg_jnl("retry (%d)", retries);
180 goto again;
181 }
182
183 dbg_jnl("return -ENOSPC");
184 return err;
185 }
186
187 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
188 dbg_jnl("got LEB %d for jhead %d", lnum, jhead);
189 avail = c->leb_size - wbuf->offs - wbuf->used;
190
191 if (wbuf->lnum != -1 && avail >= len) {
192 /*
193 * Someone else has switched the journal head and we have
194 * enough space now. This happens when more then one process is
195 * trying to write to the same journal head at the same time.
196 */
197 dbg_jnl("return LEB %d back, already have LEB %d:%d",
198 lnum, wbuf->lnum, wbuf->offs + wbuf->used);
199 err = ubifs_return_leb(c, lnum);
200 if (err)
201 goto out_unlock;
202 return 0;
203 }
204
205 err = ubifs_add_bud_to_log(c, jhead, lnum, 0);
206 if (err)
207 goto out_return;
208 offs = 0;
209
210out:
211 err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs, UBI_SHORTTERM);
212 if (err)
213 goto out_unlock;
214
215 return 0;
216
217out_unlock:
218 mutex_unlock(&wbuf->io_mutex);
219 return err;
220
221out_return:
222 /* An error occurred and the LEB has to be returned to lprops */
223 ubifs_assert(err < 0);
224 err1 = ubifs_return_leb(c, lnum);
225 if (err1 && err == -EAGAIN)
226 /*
227 * Return original error code only if it is not %-EAGAIN,
228 * which is not really an error. Otherwise, return the error
229 * code of 'ubifs_return_leb()'.
230 */
231 err = err1;
232 mutex_unlock(&wbuf->io_mutex);
233 return err;
234}
235
236/**
237 * write_node - write node to a journal head.
238 * @c: UBIFS file-system description object
239 * @jhead: journal head
240 * @node: node to write
241 * @len: node length
242 * @lnum: LEB number written is returned here
243 * @offs: offset written is returned here
244 *
245 * This function writes a node to reserved space of journal head @jhead.
246 * Returns zero in case of success and a negative error code in case of
247 * failure.
248 */
249static int write_node(struct ubifs_info *c, int jhead, void *node, int len,
250 int *lnum, int *offs)
251{
252 struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
253
254 ubifs_assert(jhead != GCHD);
255
256 *lnum = c->jheads[jhead].wbuf.lnum;
257 *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
258
259 dbg_jnl("jhead %d, LEB %d:%d, len %d", jhead, *lnum, *offs, len);
260 ubifs_prepare_node(c, node, len, 0);
261
262 return ubifs_wbuf_write_nolock(wbuf, node, len);
263}
264
265/**
266 * write_head - write data to a journal head.
267 * @c: UBIFS file-system description object
268 * @jhead: journal head
269 * @buf: buffer to write
270 * @len: length to write
271 * @lnum: LEB number written is returned here
272 * @offs: offset written is returned here
273 * @sync: non-zero if the write-buffer has to by synchronized
274 *
275 * This function is the same as 'write_node()' but it does not assume the
276 * buffer it is writing is a node, so it does not prepare it (which means
277 * initializing common header and calculating CRC).
278 */
279static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
280 int *lnum, int *offs, int sync)
281{
282 int err;
283 struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
284
285 ubifs_assert(jhead != GCHD);
286
287 *lnum = c->jheads[jhead].wbuf.lnum;
288 *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
289 dbg_jnl("jhead %d, LEB %d:%d, len %d", jhead, *lnum, *offs, len);
290
291 err = ubifs_wbuf_write_nolock(wbuf, buf, len);
292 if (err)
293 return err;
294 if (sync)
295 err = ubifs_wbuf_sync_nolock(wbuf);
296 return err;
297}
298
299/**
300 * make_reservation - reserve journal space.
301 * @c: UBIFS file-system description object
302 * @jhead: journal head
303 * @len: how many bytes to reserve
304 *
305 * This function makes space reservation in journal head @jhead. The function
306 * takes the commit lock and locks the journal head, and the caller has to
307 * unlock the head and finish the reservation with 'finish_reservation()'.
308 * Returns zero in case of success and a negative error code in case of
309 * failure.
310 *
311 * Note, the journal head may be unlocked as soon as the data is written, while
312 * the commit lock has to be released after the data has been added to the
313 * TNC.
314 */
315static int make_reservation(struct ubifs_info *c, int jhead, int len)
316{
317 int err, cmt_retries = 0, nospc_retries = 0;
318
319again:
320 down_read(&c->commit_sem);
321 err = reserve_space(c, jhead, len);
322 if (!err)
323 return 0;
324 up_read(&c->commit_sem);
325
326 if (err == -ENOSPC) {
327 /*
328 * GC could not make any progress. We should try to commit
329 * once because it could make some dirty space and GC would
330 * make progress, so make the error -EAGAIN so that the below
331 * will commit and re-try.
332 */
333 if (nospc_retries++ < 2) {
334 dbg_jnl("no space, retry");
335 err = -EAGAIN;
336 }
337
338 /*
339 * This means that the budgeting is incorrect. We always have
340 * to be able to write to the media, because all operations are
341 * budgeted. Deletions are not budgeted, though, but we reserve
342 * an extra LEB for them.
343 */
344 }
345
346 if (err != -EAGAIN)
347 goto out;
348
349 /*
350 * -EAGAIN means that the journal is full or too large, or the above
351 * code wants to do one commit. Do this and re-try.
352 */
353 if (cmt_retries > 128) {
354 /*
355 * This should not happen unless the journal size limitations
356 * are too tough.
357 */
358 ubifs_err("stuck in space allocation");
359 err = -ENOSPC;
360 goto out;
361 } else if (cmt_retries > 32)
362 ubifs_warn("too many space allocation re-tries (%d)",
363 cmt_retries);
364
365 dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
366 cmt_retries);
367 cmt_retries += 1;
368
369 err = ubifs_run_commit(c);
370 if (err)
371 return err;
372 goto again;
373
374out:
375 ubifs_err("cannot reserve %d bytes in jhead %d, error %d",
376 len, jhead, err);
377 if (err == -ENOSPC) {
378 /* This are some budgeting problems, print useful information */
379 down_write(&c->commit_sem);
380 spin_lock(&c->space_lock);
381 dbg_dump_stack();
382 dbg_dump_budg(c);
383 spin_unlock(&c->space_lock);
384 dbg_dump_lprops(c);
385 cmt_retries = dbg_check_lprops(c);
386 up_write(&c->commit_sem);
387 }
388 return err;
389}
390
391/**
392 * release_head - release a journal head.
393 * @c: UBIFS file-system description object
394 * @jhead: journal head
395 *
396 * This function releases journal head @jhead which was locked by
397 * the 'make_reservation()' function. It has to be called after each successful
398 * 'make_reservation()' invocation.
399 */
400static inline void release_head(struct ubifs_info *c, int jhead)
401{
402 mutex_unlock(&c->jheads[jhead].wbuf.io_mutex);
403}
404
405/**
406 * finish_reservation - finish a reservation.
407 * @c: UBIFS file-system description object
408 *
409 * This function finishes journal space reservation. It must be called after
410 * 'make_reservation()'.
411 */
412static void finish_reservation(struct ubifs_info *c)
413{
414 up_read(&c->commit_sem);
415}
416
417/**
418 * get_dent_type - translate VFS inode mode to UBIFS directory entry type.
419 * @mode: inode mode
420 */
421static int get_dent_type(int mode)
422{
423 switch (mode & S_IFMT) {
424 case S_IFREG:
425 return UBIFS_ITYPE_REG;
426 case S_IFDIR:
427 return UBIFS_ITYPE_DIR;
428 case S_IFLNK:
429 return UBIFS_ITYPE_LNK;
430 case S_IFBLK:
431 return UBIFS_ITYPE_BLK;
432 case S_IFCHR:
433 return UBIFS_ITYPE_CHR;
434 case S_IFIFO:
435 return UBIFS_ITYPE_FIFO;
436 case S_IFSOCK:
437 return UBIFS_ITYPE_SOCK;
438 default:
439 BUG();
440 }
441 return 0;
442}
443
444/**
445 * pack_inode - pack an inode node.
446 * @c: UBIFS file-system description object
447 * @ino: buffer in which to pack inode node
448 * @inode: inode to pack
449 * @last: indicates the last node of the group
450 * @last_reference: non-zero if this is a deletion inode
451 */
452static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
453 const struct inode *inode, int last,
454 int last_reference)
455{
456 int data_len = 0;
457 struct ubifs_inode *ui = ubifs_inode(inode);
458
459 ino->ch.node_type = UBIFS_INO_NODE;
460 ino_key_init_flash(c, &ino->key, inode->i_ino);
461 ino->creat_sqnum = cpu_to_le64(ui->creat_sqnum);
462 ino->atime_sec = cpu_to_le64(inode->i_atime.tv_sec);
463 ino->atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
464 ino->ctime_sec = cpu_to_le64(inode->i_ctime.tv_sec);
465 ino->ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
466 ino->mtime_sec = cpu_to_le64(inode->i_mtime.tv_sec);
467 ino->mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
468 ino->uid = cpu_to_le32(inode->i_uid);
469 ino->gid = cpu_to_le32(inode->i_gid);
470 ino->mode = cpu_to_le32(inode->i_mode);
471 ino->flags = cpu_to_le32(ui->flags);
472 ino->size = cpu_to_le64(ui->ui_size);
473 ino->nlink = cpu_to_le32(inode->i_nlink);
474 ino->compr_type = cpu_to_le16(ui->compr_type);
475 ino->data_len = cpu_to_le32(ui->data_len);
476 ino->xattr_cnt = cpu_to_le32(ui->xattr_cnt);
477 ino->xattr_size = cpu_to_le32(ui->xattr_size);
478 ino->xattr_names = cpu_to_le32(ui->xattr_names);
479 zero_ino_node_unused(ino);
480
481 /*
482 * Drop the attached data if this is a deletion inode, the data is not
483 * needed anymore.
484 */
485 if (!last_reference) {
486 memcpy(ino->data, ui->data, ui->data_len);
487 data_len = ui->data_len;
488 }
489
490 ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
491}
492
493/**
494 * mark_inode_clean - mark UBIFS inode as clean.
495 * @c: UBIFS file-system description object
496 * @ui: UBIFS inode to mark as clean
497 *
498 * This helper function marks UBIFS inode @ui as clean by cleaning the
499 * @ui->dirty flag and releasing its budget. Note, VFS may still treat the
500 * inode as dirty and try to write it back, but 'ubifs_write_inode()' would
501 * just do nothing.
502 */
503static void mark_inode_clean(struct ubifs_info *c, struct ubifs_inode *ui)
504{
505 if (ui->dirty)
506 ubifs_release_dirty_inode_budget(c, ui);
507 ui->dirty = 0;
508}
509
510/**
511 * ubifs_jnl_update - update inode.
512 * @c: UBIFS file-system description object
513 * @dir: parent inode or host inode in case of extended attributes
514 * @nm: directory entry name
515 * @inode: inode to update
516 * @deletion: indicates a directory entry deletion i.e unlink or rmdir
517 * @xent: non-zero if the directory entry is an extended attribute entry
518 *
519 * This function updates an inode by writing a directory entry (or extended
520 * attribute entry), the inode itself, and the parent directory inode (or the
521 * host inode) to the journal.
522 *
523 * The function writes the host inode @dir last, which is important in case of
524 * extended attributes. Indeed, then we guarantee that if the host inode gets
525 * synchronized (with 'fsync()'), and the write-buffer it sits in gets flushed,
526 * the extended attribute inode gets flushed too. And this is exactly what the
527 * user expects - synchronizing the host inode synchronizes its extended
528 * attributes. Similarly, this guarantees that if @dir is synchronized, its
529 * directory entry corresponding to @nm gets synchronized too.
530 *
531 * If the inode (@inode) or the parent directory (@dir) are synchronous, this
532 * function synchronizes the write-buffer.
533 *
534 * This function marks the @dir and @inode inodes as clean and returns zero on
535 * success. In case of failure, a negative error code is returned.
536 */
537int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
538 const struct qstr *nm, const struct inode *inode,
539 int deletion, int xent)
540{
541 int err, dlen, ilen, len, lnum, ino_offs, dent_offs;
542 int aligned_dlen, aligned_ilen, sync = IS_DIRSYNC(dir);
543 int last_reference = !!(deletion && inode->i_nlink == 0);
544 struct ubifs_inode *ui = ubifs_inode(inode);
545 struct ubifs_inode *dir_ui = ubifs_inode(dir);
546 struct ubifs_dent_node *dent;
547 struct ubifs_ino_node *ino;
548 union ubifs_key dent_key, ino_key;
549
550 dbg_jnl("ino %lu, dent '%.*s', data len %d in dir ino %lu",
551 inode->i_ino, nm->len, nm->name, ui->data_len, dir->i_ino);
552 ubifs_assert(dir_ui->data_len == 0);
553 ubifs_assert(mutex_is_locked(&dir_ui->ui_mutex));
554
555 dlen = UBIFS_DENT_NODE_SZ + nm->len + 1;
556 ilen = UBIFS_INO_NODE_SZ;
557
558 /*
559 * If the last reference to the inode is being deleted, then there is
560 * no need to attach and write inode data, it is being deleted anyway.
561 * And if the inode is being deleted, no need to synchronize
562 * write-buffer even if the inode is synchronous.
563 */
564 if (!last_reference) {
565 ilen += ui->data_len;
566 sync |= IS_SYNC(inode);
567 }
568
569 aligned_dlen = ALIGN(dlen, 8);
570 aligned_ilen = ALIGN(ilen, 8);
571 len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
572 dent = kmalloc(len, GFP_NOFS);
573 if (!dent)
574 return -ENOMEM;
575
576 /* Make reservation before allocating sequence numbers */
577 err = make_reservation(c, BASEHD, len);
578 if (err)
579 goto out_free;
580
581 if (!xent) {
582 dent->ch.node_type = UBIFS_DENT_NODE;
583 dent_key_init(c, &dent_key, dir->i_ino, nm);
584 } else {
585 dent->ch.node_type = UBIFS_XENT_NODE;
586 xent_key_init(c, &dent_key, dir->i_ino, nm);
587 }
588
589 key_write(c, &dent_key, dent->key);
590 dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
591 dent->type = get_dent_type(inode->i_mode);
592 dent->nlen = cpu_to_le16(nm->len);
593 memcpy(dent->name, nm->name, nm->len);
594 dent->name[nm->len] = '\0';
595 zero_dent_node_unused(dent);
596 ubifs_prep_grp_node(c, dent, dlen, 0);
597
598 ino = (void *)dent + aligned_dlen;
599 pack_inode(c, ino, inode, 0, last_reference);
600 ino = (void *)ino + aligned_ilen;
601 pack_inode(c, ino, dir, 1, 0);
602
603 if (last_reference) {
604 err = ubifs_add_orphan(c, inode->i_ino);
605 if (err) {
606 release_head(c, BASEHD);
607 goto out_finish;
608 }
609 }
610
611 err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
612 if (err)
613 goto out_release;
614 if (!sync) {
615 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
616
617 ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
618 ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
619 }
620 release_head(c, BASEHD);
621 kfree(dent);
622
623 if (deletion) {
624 err = ubifs_tnc_remove_nm(c, &dent_key, nm);
625 if (err)
626 goto out_ro;
627 err = ubifs_add_dirt(c, lnum, dlen);
628 } else
629 err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen, nm);
630 if (err)
631 goto out_ro;
632
633 /*
634 * Note, we do not remove the inode from TNC even if the last reference
635 * to it has just been deleted, because the inode may still be opened.
636 * Instead, the inode has been added to orphan lists and the orphan
637 * subsystem will take further care about it.
638 */
639 ino_key_init(c, &ino_key, inode->i_ino);
640 ino_offs = dent_offs + aligned_dlen;
641 err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen);
642 if (err)
643 goto out_ro;
644
645 ino_key_init(c, &ino_key, dir->i_ino);
646 ino_offs += aligned_ilen;
647 err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, UBIFS_INO_NODE_SZ);
648 if (err)
649 goto out_ro;
650
651 finish_reservation(c);
652 spin_lock(&ui->ui_lock);
653 ui->synced_i_size = ui->ui_size;
654 spin_unlock(&ui->ui_lock);
655 mark_inode_clean(c, ui);
656 mark_inode_clean(c, dir_ui);
657 return 0;
658
659out_finish:
660 finish_reservation(c);
661out_free:
662 kfree(dent);
663 return err;
664
665out_release:
666 release_head(c, BASEHD);
667out_ro:
668 ubifs_ro_mode(c, err);
669 if (last_reference)
670 ubifs_delete_orphan(c, inode->i_ino);
671 finish_reservation(c);
672 return err;
673}
674
675/**
676 * ubifs_jnl_write_data - write a data node to the journal.
677 * @c: UBIFS file-system description object
678 * @inode: inode the data node belongs to
679 * @key: node key
680 * @buf: buffer to write
681 * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
682 *
683 * This function writes a data node to the journal. Returns %0 if the data node
684 * was successfully written, and a negative error code in case of failure.
685 */
686int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
687 const union ubifs_key *key, const void *buf, int len)
688{
689 struct ubifs_data_node *data;
690 int err, lnum, offs, compr_type, out_len;
691 int dlen = UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR;
692 struct ubifs_inode *ui = ubifs_inode(inode);
693
694 dbg_jnl("ino %lu, blk %u, len %d, key %s", key_inum(c, key),
695 key_block(c, key), len, DBGKEY(key));
696 ubifs_assert(len <= UBIFS_BLOCK_SIZE);
697
698 data = kmalloc(dlen, GFP_NOFS);
699 if (!data)
700 return -ENOMEM;
701
702 data->ch.node_type = UBIFS_DATA_NODE;
703 key_write(c, key, &data->key);
704 data->size = cpu_to_le32(len);
705 zero_data_node_unused(data);
706
707 if (!(ui->flags && UBIFS_COMPR_FL))
708 /* Compression is disabled for this inode */
709 compr_type = UBIFS_COMPR_NONE;
710 else
711 compr_type = ui->compr_type;
712
713 out_len = dlen - UBIFS_DATA_NODE_SZ;
714 ubifs_compress(buf, len, &data->data, &out_len, &compr_type);
715 ubifs_assert(out_len <= UBIFS_BLOCK_SIZE);
716
717 dlen = UBIFS_DATA_NODE_SZ + out_len;
718 data->compr_type = cpu_to_le16(compr_type);
719
720 /* Make reservation before allocating sequence numbers */
721 err = make_reservation(c, DATAHD, dlen);
722 if (err)
723 goto out_free;
724
725 err = write_node(c, DATAHD, data, dlen, &lnum, &offs);
726 if (err)
727 goto out_release;
728 ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
729 release_head(c, DATAHD);
730
731 err = ubifs_tnc_add(c, key, lnum, offs, dlen);
732 if (err)
733 goto out_ro;
734
735 finish_reservation(c);
736 kfree(data);
737 return 0;
738
739out_release:
740 release_head(c, DATAHD);
741out_ro:
742 ubifs_ro_mode(c, err);
743 finish_reservation(c);
744out_free:
745 kfree(data);
746 return err;
747}
748
749/**
750 * ubifs_jnl_write_inode - flush inode to the journal.
751 * @c: UBIFS file-system description object
752 * @inode: inode to flush
753 * @deletion: inode has been deleted
754 *
755 * This function writes inode @inode to the journal. If the inode is
756 * synchronous, it also synchronizes the write-buffer. Returns zero in case of
757 * success and a negative error code in case of failure.
758 */
759int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode,
760 int deletion)
761{
762 int err, len, lnum, offs, sync = 0;
763 struct ubifs_ino_node *ino;
764 struct ubifs_inode *ui = ubifs_inode(inode);
765
766 dbg_jnl("ino %lu%s", inode->i_ino,
767 deletion ? " (last reference)" : "");
768 if (deletion)
769 ubifs_assert(inode->i_nlink == 0);
770
771 len = UBIFS_INO_NODE_SZ;
772 /*
773 * If the inode is being deleted, do not write the attached data. No
774 * need to synchronize the write-buffer either.
775 */
776 if (!deletion) {
777 len += ui->data_len;
778 sync = IS_SYNC(inode);
779 }
780 ino = kmalloc(len, GFP_NOFS);
781 if (!ino)
782 return -ENOMEM;
783
784 /* Make reservation before allocating sequence numbers */
785 err = make_reservation(c, BASEHD, len);
786 if (err)
787 goto out_free;
788
789 pack_inode(c, ino, inode, 1, deletion);
790 err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
791 if (err)
792 goto out_release;
793 if (!sync)
794 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
795 inode->i_ino);
796 release_head(c, BASEHD);
797
798 if (deletion) {
799 err = ubifs_tnc_remove_ino(c, inode->i_ino);
800 if (err)
801 goto out_ro;
802 ubifs_delete_orphan(c, inode->i_ino);
803 err = ubifs_add_dirt(c, lnum, len);
804 } else {
805 union ubifs_key key;
806
807 ino_key_init(c, &key, inode->i_ino);
808 err = ubifs_tnc_add(c, &key, lnum, offs, len);
809 }
810 if (err)
811 goto out_ro;
812
813 finish_reservation(c);
814 spin_lock(&ui->ui_lock);
815 ui->synced_i_size = ui->ui_size;
816 spin_unlock(&ui->ui_lock);
817 kfree(ino);
818 return 0;
819
820out_release:
821 release_head(c, BASEHD);
822out_ro:
823 ubifs_ro_mode(c, err);
824 finish_reservation(c);
825out_free:
826 kfree(ino);
827 return err;
828}
829
830/**
831 * ubifs_jnl_rename - rename a directory entry.
832 * @c: UBIFS file-system description object
833 * @old_dir: parent inode of directory entry to rename
834 * @old_dentry: directory entry to rename
835 * @new_dir: parent inode of directory entry to rename
836 * @new_dentry: new directory entry (or directory entry to replace)
837 * @sync: non-zero if the write-buffer has to be synchronized
838 *
839 * This function implements the re-name operation which may involve writing up
840 * to 3 inodes and 2 directory entries. It marks the written inodes as clean
841 * and returns zero on success. In case of failure, a negative error code is
842 * returned.
843 */
844int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
845 const struct dentry *old_dentry,
846 const struct inode *new_dir,
847 const struct dentry *new_dentry, int sync)
848{
849 void *p;
850 union ubifs_key key;
851 struct ubifs_dent_node *dent, *dent2;
852 int err, dlen1, dlen2, ilen, lnum, offs, len;
853 const struct inode *old_inode = old_dentry->d_inode;
854 const struct inode *new_inode = new_dentry->d_inode;
855 int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
856 int last_reference = !!(new_inode && new_inode->i_nlink == 0);
857 int move = (old_dir != new_dir);
858 struct ubifs_inode *uninitialized_var(new_ui);
859
860 dbg_jnl("dent '%.*s' in dir ino %lu to dent '%.*s' in dir ino %lu",
861 old_dentry->d_name.len, old_dentry->d_name.name,
862 old_dir->i_ino, new_dentry->d_name.len,
863 new_dentry->d_name.name, new_dir->i_ino);
864 ubifs_assert(ubifs_inode(old_dir)->data_len == 0);
865 ubifs_assert(ubifs_inode(new_dir)->data_len == 0);
866 ubifs_assert(mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
867 ubifs_assert(mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
868
869 dlen1 = UBIFS_DENT_NODE_SZ + new_dentry->d_name.len + 1;
870 dlen2 = UBIFS_DENT_NODE_SZ + old_dentry->d_name.len + 1;
871 if (new_inode) {
872 new_ui = ubifs_inode(new_inode);
873 ubifs_assert(mutex_is_locked(&new_ui->ui_mutex));
874 ilen = UBIFS_INO_NODE_SZ;
875 if (!last_reference)
876 ilen += new_ui->data_len;
877 } else
878 ilen = 0;
879
880 aligned_dlen1 = ALIGN(dlen1, 8);
881 aligned_dlen2 = ALIGN(dlen2, 8);
882 len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) + ALIGN(plen, 8);
883 if (old_dir != new_dir)
884 len += plen;
885 dent = kmalloc(len, GFP_NOFS);
886 if (!dent)
887 return -ENOMEM;
888
889 /* Make reservation before allocating sequence numbers */
890 err = make_reservation(c, BASEHD, len);
891 if (err)
892 goto out_free;
893
894 /* Make new dent */
895 dent->ch.node_type = UBIFS_DENT_NODE;
896 dent_key_init_flash(c, &dent->key, new_dir->i_ino, &new_dentry->d_name);
897 dent->inum = cpu_to_le64(old_inode->i_ino);
898 dent->type = get_dent_type(old_inode->i_mode);
899 dent->nlen = cpu_to_le16(new_dentry->d_name.len);
900 memcpy(dent->name, new_dentry->d_name.name, new_dentry->d_name.len);
901 dent->name[new_dentry->d_name.len] = '\0';
902 zero_dent_node_unused(dent);
903 ubifs_prep_grp_node(c, dent, dlen1, 0);
904
905 /* Make deletion dent */
906 dent2 = (void *)dent + aligned_dlen1;
907 dent2->ch.node_type = UBIFS_DENT_NODE;
908 dent_key_init_flash(c, &dent2->key, old_dir->i_ino,
909 &old_dentry->d_name);
910 dent2->inum = 0;
911 dent2->type = DT_UNKNOWN;
912 dent2->nlen = cpu_to_le16(old_dentry->d_name.len);
913 memcpy(dent2->name, old_dentry->d_name.name, old_dentry->d_name.len);
914 dent2->name[old_dentry->d_name.len] = '\0';
915 zero_dent_node_unused(dent2);
916 ubifs_prep_grp_node(c, dent2, dlen2, 0);
917
918 p = (void *)dent2 + aligned_dlen2;
919 if (new_inode) {
920 pack_inode(c, p, new_inode, 0, last_reference);
921 p += ALIGN(ilen, 8);
922 }
923
924 if (!move)
925 pack_inode(c, p, old_dir, 1, 0);
926 else {
927 pack_inode(c, p, old_dir, 0, 0);
928 p += ALIGN(plen, 8);
929 pack_inode(c, p, new_dir, 1, 0);
930 }
931
932 if (last_reference) {
933 err = ubifs_add_orphan(c, new_inode->i_ino);
934 if (err) {
935 release_head(c, BASEHD);
936 goto out_finish;
937 }
938 }
939
940 err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
941 if (err)
942 goto out_release;
943 if (!sync) {
944 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
945
946 ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
947 ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
948 if (new_inode)
949 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
950 new_inode->i_ino);
951 }
952 release_head(c, BASEHD);
953
954 dent_key_init(c, &key, new_dir->i_ino, &new_dentry->d_name);
955 err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, &new_dentry->d_name);
956 if (err)
957 goto out_ro;
958
959 err = ubifs_add_dirt(c, lnum, dlen2);
960 if (err)
961 goto out_ro;
962
963 dent_key_init(c, &key, old_dir->i_ino, &old_dentry->d_name);
964 err = ubifs_tnc_remove_nm(c, &key, &old_dentry->d_name);
965 if (err)
966 goto out_ro;
967
968 offs += aligned_dlen1 + aligned_dlen2;
969 if (new_inode) {
970 ino_key_init(c, &key, new_inode->i_ino);
971 err = ubifs_tnc_add(c, &key, lnum, offs, ilen);
972 if (err)
973 goto out_ro;
974 offs += ALIGN(ilen, 8);
975 }
976
977 ino_key_init(c, &key, old_dir->i_ino);
978 err = ubifs_tnc_add(c, &key, lnum, offs, plen);
979 if (err)
980 goto out_ro;
981
982 if (old_dir != new_dir) {
983 offs += ALIGN(plen, 8);
984 ino_key_init(c, &key, new_dir->i_ino);
985 err = ubifs_tnc_add(c, &key, lnum, offs, plen);
986 if (err)
987 goto out_ro;
988 }
989
990 finish_reservation(c);
991 if (new_inode) {
992 mark_inode_clean(c, new_ui);
993 spin_lock(&new_ui->ui_lock);
994 new_ui->synced_i_size = new_ui->ui_size;
995 spin_unlock(&new_ui->ui_lock);
996 }
997 mark_inode_clean(c, ubifs_inode(old_dir));
998 if (move)
999 mark_inode_clean(c, ubifs_inode(new_dir));
1000 kfree(dent);
1001 return 0;
1002
1003out_release:
1004 release_head(c, BASEHD);
1005out_ro:
1006 ubifs_ro_mode(c, err);
1007 if (last_reference)
1008 ubifs_delete_orphan(c, new_inode->i_ino);
1009out_finish:
1010 finish_reservation(c);
1011out_free:
1012 kfree(dent);
1013 return err;
1014}
1015
1016/**
1017 * recomp_data_node - re-compress a truncated data node.
1018 * @dn: data node to re-compress
1019 * @new_len: new length
1020 *
1021 * This function is used when an inode is truncated and the last data node of
1022 * the inode has to be re-compressed and re-written.
1023 */
1024static int recomp_data_node(struct ubifs_data_node *dn, int *new_len)
1025{
1026 void *buf;
1027 int err, len, compr_type, out_len;
1028
1029 out_len = le32_to_cpu(dn->size);
1030 buf = kmalloc(out_len * WORST_COMPR_FACTOR, GFP_NOFS);
1031 if (!buf)
1032 return -ENOMEM;
1033
1034 len = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
1035 compr_type = le16_to_cpu(dn->compr_type);
1036 err = ubifs_decompress(&dn->data, len, buf, &out_len, compr_type);
1037 if (err)
1038 goto out;
1039
1040 ubifs_compress(buf, *new_len, &dn->data, &out_len, &compr_type);
1041 ubifs_assert(out_len <= UBIFS_BLOCK_SIZE);
1042 dn->compr_type = cpu_to_le16(compr_type);
1043 dn->size = cpu_to_le32(*new_len);
1044 *new_len = UBIFS_DATA_NODE_SZ + out_len;
1045out:
1046 kfree(buf);
1047 return err;
1048}
1049
1050/**
1051 * ubifs_jnl_truncate - update the journal for a truncation.
1052 * @c: UBIFS file-system description object
1053 * @inode: inode to truncate
1054 * @old_size: old size
1055 * @new_size: new size
1056 *
1057 * When the size of a file decreases due to truncation, a truncation node is
1058 * written, the journal tree is updated, and the last data block is re-written
1059 * if it has been affected. The inode is also updated in order to synchronize
1060 * the new inode size.
1061 *
1062 * This function marks the inode as clean and returns zero on success. In case
1063 * of failure, a negative error code is returned.
1064 */
1065int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1066 loff_t old_size, loff_t new_size)
1067{
1068 union ubifs_key key, to_key;
1069 struct ubifs_ino_node *ino;
1070 struct ubifs_trun_node *trun;
1071 struct ubifs_data_node *uninitialized_var(dn);
1072 int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
1073 struct ubifs_inode *ui = ubifs_inode(inode);
1074 ino_t inum = inode->i_ino;
1075 unsigned int blk;
1076
1077 dbg_jnl("ino %lu, size %lld -> %lld", inum, old_size, new_size);
1078 ubifs_assert(!ui->data_len);
1079 ubifs_assert(S_ISREG(inode->i_mode));
1080 ubifs_assert(mutex_is_locked(&ui->ui_mutex));
1081
1082 sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
1083 UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
1084 ino = kmalloc(sz, GFP_NOFS);
1085 if (!ino)
1086 return -ENOMEM;
1087
1088 trun = (void *)ino + UBIFS_INO_NODE_SZ;
1089 trun->ch.node_type = UBIFS_TRUN_NODE;
1090 trun->inum = cpu_to_le32(inum);
1091 trun->old_size = cpu_to_le64(old_size);
1092 trun->new_size = cpu_to_le64(new_size);
1093 zero_trun_node_unused(trun);
1094
1095 dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
1096 if (dlen) {
1097 /* Get last data block so it can be truncated */
1098 dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
1099 blk = new_size >> UBIFS_BLOCK_SHIFT;
1100 data_key_init(c, &key, inum, blk);
1101 dbg_jnl("last block key %s", DBGKEY(&key));
1102 err = ubifs_tnc_lookup(c, &key, dn);
1103 if (err == -ENOENT)
1104 dlen = 0; /* Not found (so it is a hole) */
1105 else if (err)
1106 goto out_free;
1107 else {
1108 if (le32_to_cpu(dn->size) <= dlen)
1109 dlen = 0; /* Nothing to do */
1110 else {
1111 int compr_type = le16_to_cpu(dn->compr_type);
1112
1113 if (compr_type != UBIFS_COMPR_NONE) {
1114 err = recomp_data_node(dn, &dlen);
1115 if (err)
1116 goto out_free;
1117 } else {
1118 dn->size = cpu_to_le32(dlen);
1119 dlen += UBIFS_DATA_NODE_SZ;
1120 }
1121 zero_data_node_unused(dn);
1122 }
1123 }
1124 }
1125
1126 /* Must make reservation before allocating sequence numbers */
1127 len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
1128 if (dlen)
1129 len += dlen;
1130 err = make_reservation(c, BASEHD, len);
1131 if (err)
1132 goto out_free;
1133
1134 pack_inode(c, ino, inode, 0, 0);
1135 ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
1136 if (dlen)
1137 ubifs_prep_grp_node(c, dn, dlen, 1);
1138
1139 err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
1140 if (err)
1141 goto out_release;
1142 if (!sync)
1143 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
1144 release_head(c, BASEHD);
1145
1146 if (dlen) {
1147 sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
1148 err = ubifs_tnc_add(c, &key, lnum, sz, dlen);
1149 if (err)
1150 goto out_ro;
1151 }
1152
1153 ino_key_init(c, &key, inum);
1154 err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ);
1155 if (err)
1156 goto out_ro;
1157
1158 err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
1159 if (err)
1160 goto out_ro;
1161
1162 bit = new_size & (UBIFS_BLOCK_SIZE - 1);
1163 blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
1164 data_key_init(c, &key, inum, blk);
1165
1166 bit = old_size & (UBIFS_BLOCK_SIZE - 1);
1167 blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0: 1);
1168 data_key_init(c, &to_key, inum, blk);
1169
1170 err = ubifs_tnc_remove_range(c, &key, &to_key);
1171 if (err)
1172 goto out_ro;
1173
1174 finish_reservation(c);
1175 spin_lock(&ui->ui_lock);
1176 ui->synced_i_size = ui->ui_size;
1177 spin_unlock(&ui->ui_lock);
1178 mark_inode_clean(c, ui);
1179 kfree(ino);
1180 return 0;
1181
1182out_release:
1183 release_head(c, BASEHD);
1184out_ro:
1185 ubifs_ro_mode(c, err);
1186 finish_reservation(c);
1187out_free:
1188 kfree(ino);
1189 return err;
1190}
1191
1192#ifdef CONFIG_UBIFS_FS_XATTR
1193
1194/**
1195 * ubifs_jnl_delete_xattr - delete an extended attribute.
1196 * @c: UBIFS file-system description object
1197 * @host: host inode
1198 * @inode: extended attribute inode
1199 * @nm: extended attribute entry name
1200 *
1201 * This function delete an extended attribute which is very similar to
1202 * un-linking regular files - it writes a deletion xentry, a deletion inode and
1203 * updates the target inode. Returns zero in case of success and a negative
1204 * error code in case of failure.
1205 */
1206int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1207 const struct inode *inode, const struct qstr *nm)
1208{
1209 int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen;
1210 struct ubifs_dent_node *xent;
1211 struct ubifs_ino_node *ino;
1212 union ubifs_key xent_key, key1, key2;
1213 int sync = IS_DIRSYNC(host);
1214 struct ubifs_inode *host_ui = ubifs_inode(host);
1215
1216 dbg_jnl("host %lu, xattr ino %lu, name '%s', data len %d",
1217 host->i_ino, inode->i_ino, nm->name,
1218 ubifs_inode(inode)->data_len);
1219 ubifs_assert(inode->i_nlink == 0);
1220 ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
1221
1222 /*
1223 * Since we are deleting the inode, we do not bother to attach any data
1224 * to it and assume its length is %UBIFS_INO_NODE_SZ.
1225 */
1226 xlen = UBIFS_DENT_NODE_SZ + nm->len + 1;
1227 aligned_xlen = ALIGN(xlen, 8);
1228 hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
1229 len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
1230
1231 xent = kmalloc(len, GFP_NOFS);
1232 if (!xent)
1233 return -ENOMEM;
1234
1235 /* Make reservation before allocating sequence numbers */
1236 err = make_reservation(c, BASEHD, len);
1237 if (err) {
1238 kfree(xent);
1239 return err;
1240 }
1241
1242 xent->ch.node_type = UBIFS_XENT_NODE;
1243 xent_key_init(c, &xent_key, host->i_ino, nm);
1244 key_write(c, &xent_key, xent->key);
1245 xent->inum = 0;
1246 xent->type = get_dent_type(inode->i_mode);
1247 xent->nlen = cpu_to_le16(nm->len);
1248 memcpy(xent->name, nm->name, nm->len);
1249 xent->name[nm->len] = '\0';
1250 zero_dent_node_unused(xent);
1251 ubifs_prep_grp_node(c, xent, xlen, 0);
1252
1253 ino = (void *)xent + aligned_xlen;
1254 pack_inode(c, ino, inode, 0, 1);
1255 ino = (void *)ino + UBIFS_INO_NODE_SZ;
1256 pack_inode(c, ino, host, 1, 0);
1257
1258 err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
1259 if (!sync && !err)
1260 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
1261 release_head(c, BASEHD);
1262 kfree(xent);
1263 if (err)
1264 goto out_ro;
1265
1266 /* Remove the extended attribute entry from TNC */
1267 err = ubifs_tnc_remove_nm(c, &xent_key, nm);
1268 if (err)
1269 goto out_ro;
1270 err = ubifs_add_dirt(c, lnum, xlen);
1271 if (err)
1272 goto out_ro;
1273
1274 /*
1275 * Remove all nodes belonging to the extended attribute inode from TNC.
1276 * Well, there actually must be only one node - the inode itself.
1277 */
1278 lowest_ino_key(c, &key1, inode->i_ino);
1279 highest_ino_key(c, &key2, inode->i_ino);
1280 err = ubifs_tnc_remove_range(c, &key1, &key2);
1281 if (err)
1282 goto out_ro;
1283 err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
1284 if (err)
1285 goto out_ro;
1286
1287 /* And update TNC with the new host inode position */
1288 ino_key_init(c, &key1, host->i_ino);
1289 err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen);
1290 if (err)
1291 goto out_ro;
1292
1293 finish_reservation(c);
1294 spin_lock(&host_ui->ui_lock);
1295 host_ui->synced_i_size = host_ui->ui_size;
1296 spin_unlock(&host_ui->ui_lock);
1297 mark_inode_clean(c, host_ui);
1298 return 0;
1299
1300out_ro:
1301 ubifs_ro_mode(c, err);
1302 finish_reservation(c);
1303 return err;
1304}
1305
1306/**
1307 * ubifs_jnl_change_xattr - change an extended attribute.
1308 * @c: UBIFS file-system description object
1309 * @inode: extended attribute inode
1310 * @host: host inode
1311 *
1312 * This function writes the updated version of an extended attribute inode and
1313 * the host inode tho the journal (to the base head). The host inode is written
1314 * after the extended attribute inode in order to guarantee that the extended
1315 * attribute will be flushed when the inode is synchronized by 'fsync()' and
1316 * consequently, the write-buffer is synchronized. This function returns zero
1317 * in case of success and a negative error code in case of failure.
1318 */
1319int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
1320 const struct inode *host)
1321{
1322 int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
1323 struct ubifs_inode *host_ui = ubifs_inode(inode);
1324 struct ubifs_ino_node *ino;
1325 union ubifs_key key;
1326 int sync = IS_DIRSYNC(host);
1327
1328 dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
1329 ubifs_assert(host->i_nlink > 0);
1330 ubifs_assert(inode->i_nlink > 0);
1331 ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
1332
1333 len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
1334 len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
1335 aligned_len1 = ALIGN(len1, 8);
1336 aligned_len = aligned_len1 + ALIGN(len2, 8);
1337
1338 ino = kmalloc(aligned_len, GFP_NOFS);
1339 if (!ino)
1340 return -ENOMEM;
1341
1342 /* Make reservation before allocating sequence numbers */
1343 err = make_reservation(c, BASEHD, aligned_len);
1344 if (err)
1345 goto out_free;
1346
1347 pack_inode(c, ino, host, 0, 0);
1348 pack_inode(c, (void *)ino + aligned_len1, inode, 1, 0);
1349
1350 err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
1351 if (!sync && !err) {
1352 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1353
1354 ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
1355 ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
1356 }
1357 release_head(c, BASEHD);
1358 if (err)
1359 goto out_ro;
1360
1361 ino_key_init(c, &key, host->i_ino);
1362 err = ubifs_tnc_add(c, &key, lnum, offs, len1);
1363 if (err)
1364 goto out_ro;
1365
1366 ino_key_init(c, &key, inode->i_ino);
1367 err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2);
1368 if (err)
1369 goto out_ro;
1370
1371 finish_reservation(c);
1372 spin_lock(&host_ui->ui_lock);
1373 host_ui->synced_i_size = host_ui->ui_size;
1374 spin_unlock(&host_ui->ui_lock);
1375 mark_inode_clean(c, host_ui);
1376 kfree(ino);
1377 return 0;
1378
1379out_ro:
1380 ubifs_ro_mode(c, err);
1381 finish_reservation(c);
1382out_free:
1383 kfree(ino);
1384 return err;
1385}
1386
1387#endif /* CONFIG_UBIFS_FS_XATTR */
diff --git a/fs/ubifs/key.h b/fs/ubifs/key.h
new file mode 100644
index 000000000000..8f7476007549
--- /dev/null
+++ b/fs/ubifs/key.h
@@ -0,0 +1,533 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This header contains various key-related definitions and helper function.
25 * UBIFS allows several key schemes, so we access key fields only via these
26 * helpers. At the moment only one key scheme is supported.
27 *
28 * Simple key scheme
29 * ~~~~~~~~~~~~~~~~~
30 *
31 * Keys are 64-bits long. First 32-bits are inode number (parent inode number
32 * in case of direntry key). Next 3 bits are node type. The last 29 bits are
33 * 4KiB offset in case of inode node, and direntry hash in case of a direntry
34 * node. We use "r5" hash borrowed from reiserfs.
35 */
36
37#ifndef __UBIFS_KEY_H__
38#define __UBIFS_KEY_H__
39
40/**
41 * key_r5_hash - R5 hash function (borrowed from reiserfs).
42 * @s: direntry name
43 * @len: name length
44 */
45static inline uint32_t key_r5_hash(const char *s, int len)
46{
47 uint32_t a = 0;
48 const signed char *str = (const signed char *)s;
49
50 while (*str) {
51 a += *str << 4;
52 a += *str >> 4;
53 a *= 11;
54 str++;
55 }
56
57 a &= UBIFS_S_KEY_HASH_MASK;
58
59 /*
60 * We use hash values as offset in directories, so values %0 and %1 are
61 * reserved for "." and "..". %2 is reserved for "end of readdir"
62 * marker.
63 */
64 if (unlikely(a >= 0 && a <= 2))
65 a += 3;
66 return a;
67}
68
69/**
70 * key_test_hash - testing hash function.
71 * @str: direntry name
72 * @len: name length
73 */
74static inline uint32_t key_test_hash(const char *str, int len)
75{
76 uint32_t a = 0;
77
78 len = min_t(uint32_t, len, 4);
79 memcpy(&a, str, len);
80 a &= UBIFS_S_KEY_HASH_MASK;
81 if (unlikely(a >= 0 && a <= 2))
82 a += 3;
83 return a;
84}
85
86/**
87 * ino_key_init - initialize inode key.
88 * @c: UBIFS file-system description object
89 * @key: key to initialize
90 * @inum: inode number
91 */
92static inline void ino_key_init(const struct ubifs_info *c,
93 union ubifs_key *key, ino_t inum)
94{
95 key->u32[0] = inum;
96 key->u32[1] = UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS;
97}
98
99/**
100 * ino_key_init_flash - initialize on-flash inode key.
101 * @c: UBIFS file-system description object
102 * @k: key to initialize
103 * @inum: inode number
104 */
105static inline void ino_key_init_flash(const struct ubifs_info *c, void *k,
106 ino_t inum)
107{
108 union ubifs_key *key = k;
109
110 key->j32[0] = cpu_to_le32(inum);
111 key->j32[1] = cpu_to_le32(UBIFS_INO_KEY << UBIFS_S_KEY_BLOCK_BITS);
112 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
113}
114
115/**
116 * lowest_ino_key - get the lowest possible inode key.
117 * @c: UBIFS file-system description object
118 * @key: key to initialize
119 * @inum: inode number
120 */
121static inline void lowest_ino_key(const struct ubifs_info *c,
122 union ubifs_key *key, ino_t inum)
123{
124 key->u32[0] = inum;
125 key->u32[1] = 0;
126}
127
128/**
129 * highest_ino_key - get the highest possible inode key.
130 * @c: UBIFS file-system description object
131 * @key: key to initialize
132 * @inum: inode number
133 */
134static inline void highest_ino_key(const struct ubifs_info *c,
135 union ubifs_key *key, ino_t inum)
136{
137 key->u32[0] = inum;
138 key->u32[1] = 0xffffffff;
139}
140
141/**
142 * dent_key_init - initialize directory entry key.
143 * @c: UBIFS file-system description object
144 * @key: key to initialize
145 * @inum: parent inode number
146 * @nm: direntry name and length
147 */
148static inline void dent_key_init(const struct ubifs_info *c,
149 union ubifs_key *key, ino_t inum,
150 const struct qstr *nm)
151{
152 uint32_t hash = c->key_hash(nm->name, nm->len);
153
154 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
155 key->u32[0] = inum;
156 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
157}
158
159/**
160 * dent_key_init_hash - initialize directory entry key without re-calculating
161 * hash function.
162 * @c: UBIFS file-system description object
163 * @key: key to initialize
164 * @inum: parent inode number
165 * @hash: direntry name hash
166 */
167static inline void dent_key_init_hash(const struct ubifs_info *c,
168 union ubifs_key *key, ino_t inum,
169 uint32_t hash)
170{
171 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
172 key->u32[0] = inum;
173 key->u32[1] = hash | (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS);
174}
175
176/**
177 * dent_key_init_flash - initialize on-flash directory entry key.
178 * @c: UBIFS file-system description object
179 * @k: key to initialize
180 * @inum: parent inode number
181 * @nm: direntry name and length
182 */
183static inline void dent_key_init_flash(const struct ubifs_info *c, void *k,
184 ino_t inum, const struct qstr *nm)
185{
186 union ubifs_key *key = k;
187 uint32_t hash = c->key_hash(nm->name, nm->len);
188
189 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
190 key->j32[0] = cpu_to_le32(inum);
191 key->j32[1] = cpu_to_le32(hash |
192 (UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS));
193 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
194}
195
196/**
197 * lowest_dent_key - get the lowest possible directory entry key.
198 * @c: UBIFS file-system description object
199 * @key: where to store the lowest key
200 * @inum: parent inode number
201 */
202static inline void lowest_dent_key(const struct ubifs_info *c,
203 union ubifs_key *key, ino_t inum)
204{
205 key->u32[0] = inum;
206 key->u32[1] = UBIFS_DENT_KEY << UBIFS_S_KEY_HASH_BITS;
207}
208
209/**
210 * xent_key_init - initialize extended attribute entry key.
211 * @c: UBIFS file-system description object
212 * @key: key to initialize
213 * @inum: host inode number
214 * @nm: extended attribute entry name and length
215 */
216static inline void xent_key_init(const struct ubifs_info *c,
217 union ubifs_key *key, ino_t inum,
218 const struct qstr *nm)
219{
220 uint32_t hash = c->key_hash(nm->name, nm->len);
221
222 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
223 key->u32[0] = inum;
224 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
225}
226
227/**
228 * xent_key_init_hash - initialize extended attribute entry key without
229 * re-calculating hash function.
230 * @c: UBIFS file-system description object
231 * @key: key to initialize
232 * @inum: host inode number
233 * @hash: extended attribute entry name hash
234 */
235static inline void xent_key_init_hash(const struct ubifs_info *c,
236 union ubifs_key *key, ino_t inum,
237 uint32_t hash)
238{
239 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
240 key->u32[0] = inum;
241 key->u32[1] = hash | (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS);
242}
243
244/**
245 * xent_key_init_flash - initialize on-flash extended attribute entry key.
246 * @c: UBIFS file-system description object
247 * @k: key to initialize
248 * @inum: host inode number
249 * @nm: extended attribute entry name and length
250 */
251static inline void xent_key_init_flash(const struct ubifs_info *c, void *k,
252 ino_t inum, const struct qstr *nm)
253{
254 union ubifs_key *key = k;
255 uint32_t hash = c->key_hash(nm->name, nm->len);
256
257 ubifs_assert(!(hash & ~UBIFS_S_KEY_HASH_MASK));
258 key->j32[0] = cpu_to_le32(inum);
259 key->j32[1] = cpu_to_le32(hash |
260 (UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS));
261 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
262}
263
264/**
265 * lowest_xent_key - get the lowest possible extended attribute entry key.
266 * @c: UBIFS file-system description object
267 * @key: where to store the lowest key
268 * @inum: host inode number
269 */
270static inline void lowest_xent_key(const struct ubifs_info *c,
271 union ubifs_key *key, ino_t inum)
272{
273 key->u32[0] = inum;
274 key->u32[1] = UBIFS_XENT_KEY << UBIFS_S_KEY_HASH_BITS;
275}
276
277/**
278 * data_key_init - initialize data key.
279 * @c: UBIFS file-system description object
280 * @key: key to initialize
281 * @inum: inode number
282 * @block: block number
283 */
284static inline void data_key_init(const struct ubifs_info *c,
285 union ubifs_key *key, ino_t inum,
286 unsigned int block)
287{
288 ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
289 key->u32[0] = inum;
290 key->u32[1] = block | (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS);
291}
292
293/**
294 * data_key_init_flash - initialize on-flash data key.
295 * @c: UBIFS file-system description object
296 * @k: key to initialize
297 * @inum: inode number
298 * @block: block number
299 */
300static inline void data_key_init_flash(const struct ubifs_info *c, void *k,
301 ino_t inum, unsigned int block)
302{
303 union ubifs_key *key = k;
304
305 ubifs_assert(!(block & ~UBIFS_S_KEY_BLOCK_MASK));
306 key->j32[0] = cpu_to_le32(inum);
307 key->j32[1] = cpu_to_le32(block |
308 (UBIFS_DATA_KEY << UBIFS_S_KEY_BLOCK_BITS));
309 memset(k + 8, 0, UBIFS_MAX_KEY_LEN - 8);
310}
311
312/**
313 * trun_key_init - initialize truncation node key.
314 * @c: UBIFS file-system description object
315 * @key: key to initialize
316 * @inum: inode number
317 *
318 * Note, UBIFS does not have truncation keys on the media and this function is
319 * only used for purposes of replay.
320 */
321static inline void trun_key_init(const struct ubifs_info *c,
322 union ubifs_key *key, ino_t inum)
323{
324 key->u32[0] = inum;
325 key->u32[1] = UBIFS_TRUN_KEY << UBIFS_S_KEY_BLOCK_BITS;
326}
327
328/**
329 * key_type - get key type.
330 * @c: UBIFS file-system description object
331 * @key: key to get type of
332 */
333static inline int key_type(const struct ubifs_info *c,
334 const union ubifs_key *key)
335{
336 return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
337}
338
339/**
340 * key_type_flash - get type of a on-flash formatted key.
341 * @c: UBIFS file-system description object
342 * @k: key to get type of
343 */
344static inline int key_type_flash(const struct ubifs_info *c, const void *k)
345{
346 const union ubifs_key *key = k;
347
348 return le32_to_cpu(key->u32[1]) >> UBIFS_S_KEY_BLOCK_BITS;
349}
350
351/**
352 * key_inum - fetch inode number from key.
353 * @c: UBIFS file-system description object
354 * @k: key to fetch inode number from
355 */
356static inline ino_t key_inum(const struct ubifs_info *c, const void *k)
357{
358 const union ubifs_key *key = k;
359
360 return key->u32[0];
361}
362
363/**
364 * key_inum_flash - fetch inode number from an on-flash formatted key.
365 * @c: UBIFS file-system description object
366 * @k: key to fetch inode number from
367 */
368static inline ino_t key_inum_flash(const struct ubifs_info *c, const void *k)
369{
370 const union ubifs_key *key = k;
371
372 return le32_to_cpu(key->j32[0]);
373}
374
375/**
376 * key_hash - get directory entry hash.
377 * @c: UBIFS file-system description object
378 * @key: the key to get hash from
379 */
380static inline int key_hash(const struct ubifs_info *c,
381 const union ubifs_key *key)
382{
383 return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
384}
385
386/**
387 * key_hash_flash - get directory entry hash from an on-flash formatted key.
388 * @c: UBIFS file-system description object
389 * @k: the key to get hash from
390 */
391static inline int key_hash_flash(const struct ubifs_info *c, const void *k)
392{
393 const union ubifs_key *key = k;
394
395 return le32_to_cpu(key->j32[1]) & UBIFS_S_KEY_HASH_MASK;
396}
397
398/**
399 * key_block - get data block number.
400 * @c: UBIFS file-system description object
401 * @key: the key to get the block number from
402 */
403static inline unsigned int key_block(const struct ubifs_info *c,
404 const union ubifs_key *key)
405{
406 return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
407}
408
409/**
410 * key_block_flash - get data block number from an on-flash formatted key.
411 * @c: UBIFS file-system description object
412 * @k: the key to get the block number from
413 */
414static inline unsigned int key_block_flash(const struct ubifs_info *c,
415 const void *k)
416{
417 const union ubifs_key *key = k;
418
419 return le32_to_cpu(key->u32[1]) & UBIFS_S_KEY_BLOCK_MASK;
420}
421
422/**
423 * key_read - transform a key to in-memory format.
424 * @c: UBIFS file-system description object
425 * @from: the key to transform
426 * @to: the key to store the result
427 */
428static inline void key_read(const struct ubifs_info *c, const void *from,
429 union ubifs_key *to)
430{
431 const union ubifs_key *f = from;
432
433 to->u32[0] = le32_to_cpu(f->j32[0]);
434 to->u32[1] = le32_to_cpu(f->j32[1]);
435}
436
437/**
438 * key_write - transform a key from in-memory format.
439 * @c: UBIFS file-system description object
440 * @from: the key to transform
441 * @to: the key to store the result
442 */
443static inline void key_write(const struct ubifs_info *c,
444 const union ubifs_key *from, void *to)
445{
446 union ubifs_key *t = to;
447
448 t->j32[0] = cpu_to_le32(from->u32[0]);
449 t->j32[1] = cpu_to_le32(from->u32[1]);
450 memset(to + 8, 0, UBIFS_MAX_KEY_LEN - 8);
451}
452
453/**
454 * key_write_idx - transform a key from in-memory format for the index.
455 * @c: UBIFS file-system description object
456 * @from: the key to transform
457 * @to: the key to store the result
458 */
459static inline void key_write_idx(const struct ubifs_info *c,
460 const union ubifs_key *from, void *to)
461{
462 union ubifs_key *t = to;
463
464 t->j32[0] = cpu_to_le32(from->u32[0]);
465 t->j32[1] = cpu_to_le32(from->u32[1]);
466}
467
468/**
469 * key_copy - copy a key.
470 * @c: UBIFS file-system description object
471 * @from: the key to copy from
472 * @to: the key to copy to
473 */
474static inline void key_copy(const struct ubifs_info *c,
475 const union ubifs_key *from, union ubifs_key *to)
476{
477 to->u64[0] = from->u64[0];
478}
479
480/**
481 * keys_cmp - compare keys.
482 * @c: UBIFS file-system description object
483 * @key1: the first key to compare
484 * @key2: the second key to compare
485 *
486 * This function compares 2 keys and returns %-1 if @key1 is less than
487 * @key2, 0 if the keys are equivalent and %1 if @key1 is greater than @key2.
488 */
489static inline int keys_cmp(const struct ubifs_info *c,
490 const union ubifs_key *key1,
491 const union ubifs_key *key2)
492{
493 if (key1->u32[0] < key2->u32[0])
494 return -1;
495 if (key1->u32[0] > key2->u32[0])
496 return 1;
497 if (key1->u32[1] < key2->u32[1])
498 return -1;
499 if (key1->u32[1] > key2->u32[1])
500 return 1;
501
502 return 0;
503}
504
505/**
506 * is_hash_key - is a key vulnerable to hash collisions.
507 * @c: UBIFS file-system description object
508 * @key: key
509 *
510 * This function returns %1 if @key is a hashed key or %0 otherwise.
511 */
512static inline int is_hash_key(const struct ubifs_info *c,
513 const union ubifs_key *key)
514{
515 int type = key_type(c, key);
516
517 return type == UBIFS_DENT_KEY || type == UBIFS_XENT_KEY;
518}
519
520/**
521 * key_max_inode_size - get maximum file size allowed by current key format.
522 * @c: UBIFS file-system description object
523 */
524static inline unsigned long long key_max_inode_size(const struct ubifs_info *c)
525{
526 switch (c->key_fmt) {
527 case UBIFS_SIMPLE_KEY_FMT:
528 return (1ULL << UBIFS_S_KEY_BLOCK_BITS) * UBIFS_BLOCK_SIZE;
529 default:
530 return 0;
531 }
532}
533#endif /* !__UBIFS_KEY_H__ */
diff --git a/fs/ubifs/log.c b/fs/ubifs/log.c
new file mode 100644
index 000000000000..36857b9ed59e
--- /dev/null
+++ b/fs/ubifs/log.c
@@ -0,0 +1,805 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file is a part of UBIFS journal implementation and contains various
25 * functions which manipulate the log. The log is a fixed area on the flash
26 * which does not contain any data but refers to buds. The log is a part of the
27 * journal.
28 */
29
30#include "ubifs.h"
31
32#ifdef CONFIG_UBIFS_FS_DEBUG
33static int dbg_check_bud_bytes(struct ubifs_info *c);
34#else
35#define dbg_check_bud_bytes(c) 0
36#endif
37
38/**
39 * ubifs_search_bud - search bud LEB.
40 * @c: UBIFS file-system description object
41 * @lnum: logical eraseblock number to search
42 *
43 * This function searches bud LEB @lnum. Returns bud description object in case
44 * of success and %NULL if there is no bud with this LEB number.
45 */
46struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
47{
48 struct rb_node *p;
49 struct ubifs_bud *bud;
50
51 spin_lock(&c->buds_lock);
52 p = c->buds.rb_node;
53 while (p) {
54 bud = rb_entry(p, struct ubifs_bud, rb);
55 if (lnum < bud->lnum)
56 p = p->rb_left;
57 else if (lnum > bud->lnum)
58 p = p->rb_right;
59 else {
60 spin_unlock(&c->buds_lock);
61 return bud;
62 }
63 }
64 spin_unlock(&c->buds_lock);
65 return NULL;
66}
67
68/**
69 * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
70 * @c: UBIFS file-system description object
71 * @lnum: logical eraseblock number to search
72 *
73 * This functions returns the wbuf for @lnum or %NULL if there is not one.
74 */
75struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
76{
77 struct rb_node *p;
78 struct ubifs_bud *bud;
79 int jhead;
80
81 if (!c->jheads)
82 return NULL;
83
84 spin_lock(&c->buds_lock);
85 p = c->buds.rb_node;
86 while (p) {
87 bud = rb_entry(p, struct ubifs_bud, rb);
88 if (lnum < bud->lnum)
89 p = p->rb_left;
90 else if (lnum > bud->lnum)
91 p = p->rb_right;
92 else {
93 jhead = bud->jhead;
94 spin_unlock(&c->buds_lock);
95 return &c->jheads[jhead].wbuf;
96 }
97 }
98 spin_unlock(&c->buds_lock);
99 return NULL;
100}
101
102/**
103 * next_log_lnum - switch to the next log LEB.
104 * @c: UBIFS file-system description object
105 * @lnum: current log LEB
106 */
107static inline int next_log_lnum(const struct ubifs_info *c, int lnum)
108{
109 lnum += 1;
110 if (lnum > c->log_last)
111 lnum = UBIFS_LOG_LNUM;
112
113 return lnum;
114}
115
116/**
117 * empty_log_bytes - calculate amount of empty space in the log.
118 * @c: UBIFS file-system description object
119 */
120static inline long long empty_log_bytes(const struct ubifs_info *c)
121{
122 long long h, t;
123
124 h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
125 t = (long long)c->ltail_lnum * c->leb_size;
126
127 if (h >= t)
128 return c->log_bytes - h + t;
129 else
130 return t - h;
131}
132
133/**
134 * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
135 * @c: UBIFS file-system description object
136 * @bud: the bud to add
137 */
138void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
139{
140 struct rb_node **p, *parent = NULL;
141 struct ubifs_bud *b;
142 struct ubifs_jhead *jhead;
143
144 spin_lock(&c->buds_lock);
145 p = &c->buds.rb_node;
146 while (*p) {
147 parent = *p;
148 b = rb_entry(parent, struct ubifs_bud, rb);
149 ubifs_assert(bud->lnum != b->lnum);
150 if (bud->lnum < b->lnum)
151 p = &(*p)->rb_left;
152 else
153 p = &(*p)->rb_right;
154 }
155
156 rb_link_node(&bud->rb, parent, p);
157 rb_insert_color(&bud->rb, &c->buds);
158 if (c->jheads) {
159 jhead = &c->jheads[bud->jhead];
160 list_add_tail(&bud->list, &jhead->buds_list);
161 } else
162 ubifs_assert(c->replaying && (c->vfs_sb->s_flags & MS_RDONLY));
163
164 /*
165 * Note, although this is a new bud, we anyway account this space now,
166 * before any data has been written to it, because this is about to
167 * guarantee fixed mount time, and this bud will anyway be read and
168 * scanned.
169 */
170 c->bud_bytes += c->leb_size - bud->start;
171
172 dbg_log("LEB %d:%d, jhead %d, bud_bytes %lld", bud->lnum,
173 bud->start, bud->jhead, c->bud_bytes);
174 spin_unlock(&c->buds_lock);
175}
176
177/**
178 * ubifs_create_buds_lists - create journal head buds lists for remount rw.
179 * @c: UBIFS file-system description object
180 */
181void ubifs_create_buds_lists(struct ubifs_info *c)
182{
183 struct rb_node *p;
184
185 spin_lock(&c->buds_lock);
186 p = rb_first(&c->buds);
187 while (p) {
188 struct ubifs_bud *bud = rb_entry(p, struct ubifs_bud, rb);
189 struct ubifs_jhead *jhead = &c->jheads[bud->jhead];
190
191 list_add_tail(&bud->list, &jhead->buds_list);
192 p = rb_next(p);
193 }
194 spin_unlock(&c->buds_lock);
195}
196
197/**
198 * ubifs_add_bud_to_log - add a new bud to the log.
199 * @c: UBIFS file-system description object
200 * @jhead: journal head the bud belongs to
201 * @lnum: LEB number of the bud
202 * @offs: starting offset of the bud
203 *
204 * This function writes reference node for the new bud LEB @lnum it to the log,
205 * and adds it to the buds tress. It also makes sure that log size does not
206 * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
207 * %-EAGAIN if commit is required, and a negative error codes in case of
208 * failure.
209 */
210int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
211{
212 int err;
213 struct ubifs_bud *bud;
214 struct ubifs_ref_node *ref;
215
216 bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
217 if (!bud)
218 return -ENOMEM;
219 ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
220 if (!ref) {
221 kfree(bud);
222 return -ENOMEM;
223 }
224
225 mutex_lock(&c->log_mutex);
226
227 if (c->ro_media) {
228 err = -EROFS;
229 goto out_unlock;
230 }
231
232 /* Make sure we have enough space in the log */
233 if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
234 dbg_log("not enough log space - %lld, required %d",
235 empty_log_bytes(c), c->min_log_bytes);
236 ubifs_commit_required(c);
237 err = -EAGAIN;
238 goto out_unlock;
239 }
240
241 /*
242 * Make sure the the amount of space in buds will not exceed
243 * 'c->max_bud_bytes' limit, because we want to guarantee mount time
244 * limits.
245 *
246 * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
247 * because we are holding @c->log_mutex. All @c->bud_bytes take place
248 * when both @c->log_mutex and @c->bud_bytes are locked.
249 */
250 if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
251 dbg_log("bud bytes %lld (%lld max), require commit",
252 c->bud_bytes, c->max_bud_bytes);
253 ubifs_commit_required(c);
254 err = -EAGAIN;
255 goto out_unlock;
256 }
257
258 /*
259 * If the journal is full enough - start background commit. Note, it is
260 * OK to read 'c->cmt_state' without spinlock because integer reads
261 * are atomic in the kernel.
262 */
263 if (c->bud_bytes >= c->bg_bud_bytes &&
264 c->cmt_state == COMMIT_RESTING) {
265 dbg_log("bud bytes %lld (%lld max), initiate BG commit",
266 c->bud_bytes, c->max_bud_bytes);
267 ubifs_request_bg_commit(c);
268 }
269
270 bud->lnum = lnum;
271 bud->start = offs;
272 bud->jhead = jhead;
273
274 ref->ch.node_type = UBIFS_REF_NODE;
275 ref->lnum = cpu_to_le32(bud->lnum);
276 ref->offs = cpu_to_le32(bud->start);
277 ref->jhead = cpu_to_le32(jhead);
278
279 if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
280 c->lhead_lnum = next_log_lnum(c, c->lhead_lnum);
281 c->lhead_offs = 0;
282 }
283
284 if (c->lhead_offs == 0) {
285 /* Must ensure next log LEB has been unmapped */
286 err = ubifs_leb_unmap(c, c->lhead_lnum);
287 if (err)
288 goto out_unlock;
289 }
290
291 if (bud->start == 0) {
292 /*
293 * Before writing the LEB reference which refers an empty LEB
294 * to the log, we have to make sure it is mapped, because
295 * otherwise we'd risk to refer an LEB with garbage in case of
296 * an unclean reboot, because the target LEB might have been
297 * unmapped, but not yet physically erased.
298 */
299 err = ubi_leb_map(c->ubi, bud->lnum, UBI_SHORTTERM);
300 if (err)
301 goto out_unlock;
302 }
303
304 dbg_log("write ref LEB %d:%d",
305 c->lhead_lnum, c->lhead_offs);
306 err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
307 c->lhead_offs, UBI_SHORTTERM);
308 if (err)
309 goto out_unlock;
310
311 c->lhead_offs += c->ref_node_alsz;
312
313 ubifs_add_bud(c, bud);
314
315 mutex_unlock(&c->log_mutex);
316 kfree(ref);
317 return 0;
318
319out_unlock:
320 mutex_unlock(&c->log_mutex);
321 kfree(ref);
322 kfree(bud);
323 return err;
324}
325
326/**
327 * remove_buds - remove used buds.
328 * @c: UBIFS file-system description object
329 *
330 * This function removes use buds from the buds tree. It does not remove the
331 * buds which are pointed to by journal heads.
332 */
333static void remove_buds(struct ubifs_info *c)
334{
335 struct rb_node *p;
336
337 ubifs_assert(list_empty(&c->old_buds));
338 c->cmt_bud_bytes = 0;
339 spin_lock(&c->buds_lock);
340 p = rb_first(&c->buds);
341 while (p) {
342 struct rb_node *p1 = p;
343 struct ubifs_bud *bud;
344 struct ubifs_wbuf *wbuf;
345
346 p = rb_next(p);
347 bud = rb_entry(p1, struct ubifs_bud, rb);
348 wbuf = &c->jheads[bud->jhead].wbuf;
349
350 if (wbuf->lnum == bud->lnum) {
351 /*
352 * Do not remove buds which are pointed to by journal
353 * heads (non-closed buds).
354 */
355 c->cmt_bud_bytes += wbuf->offs - bud->start;
356 dbg_log("preserve %d:%d, jhead %d, bud bytes %d, "
357 "cmt_bud_bytes %lld", bud->lnum, bud->start,
358 bud->jhead, wbuf->offs - bud->start,
359 c->cmt_bud_bytes);
360 bud->start = wbuf->offs;
361 } else {
362 c->cmt_bud_bytes += c->leb_size - bud->start;
363 dbg_log("remove %d:%d, jhead %d, bud bytes %d, "
364 "cmt_bud_bytes %lld", bud->lnum, bud->start,
365 bud->jhead, c->leb_size - bud->start,
366 c->cmt_bud_bytes);
367 rb_erase(p1, &c->buds);
368 list_del(&bud->list);
369 /*
370 * If the commit does not finish, the recovery will need
371 * to replay the journal, in which case the old buds
372 * must be unchanged. Do not release them until post
373 * commit i.e. do not allow them to be garbage
374 * collected.
375 */
376 list_add(&bud->list, &c->old_buds);
377 }
378 }
379 spin_unlock(&c->buds_lock);
380}
381
382/**
383 * ubifs_log_start_commit - start commit.
384 * @c: UBIFS file-system description object
385 * @ltail_lnum: return new log tail LEB number
386 *
387 * The commit operation starts with writing "commit start" node to the log and
388 * reference nodes for all journal heads which will define new journal after
389 * the commit has been finished. The commit start and reference nodes are
390 * written in one go to the nearest empty log LEB (hence, when commit is
391 * finished UBIFS may safely unmap all the previous log LEBs). This function
392 * returns zero in case of success and a negative error code in case of
393 * failure.
394 */
395int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
396{
397 void *buf;
398 struct ubifs_cs_node *cs;
399 struct ubifs_ref_node *ref;
400 int err, i, max_len, len;
401
402 err = dbg_check_bud_bytes(c);
403 if (err)
404 return err;
405
406 max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
407 max_len = ALIGN(max_len, c->min_io_size);
408 buf = cs = kmalloc(max_len, GFP_NOFS);
409 if (!buf)
410 return -ENOMEM;
411
412 cs->ch.node_type = UBIFS_CS_NODE;
413 cs->cmt_no = cpu_to_le64(c->cmt_no + 1);
414 ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
415
416 /*
417 * Note, we do not lock 'c->log_mutex' because this is the commit start
418 * phase and we are exclusively using the log. And we do not lock
419 * write-buffer because nobody can write to the file-system at this
420 * phase.
421 */
422
423 len = UBIFS_CS_NODE_SZ;
424 for (i = 0; i < c->jhead_cnt; i++) {
425 int lnum = c->jheads[i].wbuf.lnum;
426 int offs = c->jheads[i].wbuf.offs;
427
428 if (lnum == -1 || offs == c->leb_size)
429 continue;
430
431 dbg_log("add ref to LEB %d:%d for jhead %d", lnum, offs, i);
432 ref = buf + len;
433 ref->ch.node_type = UBIFS_REF_NODE;
434 ref->lnum = cpu_to_le32(lnum);
435 ref->offs = cpu_to_le32(offs);
436 ref->jhead = cpu_to_le32(i);
437
438 ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
439 len += UBIFS_REF_NODE_SZ;
440 }
441
442 ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
443
444 /* Switch to the next log LEB */
445 if (c->lhead_offs) {
446 c->lhead_lnum = next_log_lnum(c, c->lhead_lnum);
447 c->lhead_offs = 0;
448 }
449
450 if (c->lhead_offs == 0) {
451 /* Must ensure next LEB has been unmapped */
452 err = ubifs_leb_unmap(c, c->lhead_lnum);
453 if (err)
454 goto out;
455 }
456
457 len = ALIGN(len, c->min_io_size);
458 dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
459 err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len, UBI_SHORTTERM);
460 if (err)
461 goto out;
462
463 *ltail_lnum = c->lhead_lnum;
464
465 c->lhead_offs += len;
466 if (c->lhead_offs == c->leb_size) {
467 c->lhead_lnum = next_log_lnum(c, c->lhead_lnum);
468 c->lhead_offs = 0;
469 }
470
471 remove_buds(c);
472
473 /*
474 * We have started the commit and now users may use the rest of the log
475 * for new writes.
476 */
477 c->min_log_bytes = 0;
478
479out:
480 kfree(buf);
481 return err;
482}
483
484/**
485 * ubifs_log_end_commit - end commit.
486 * @c: UBIFS file-system description object
487 * @ltail_lnum: new log tail LEB number
488 *
489 * This function is called on when the commit operation was finished. It
490 * moves log tail to new position and unmaps LEBs which contain obsolete data.
491 * Returns zero in case of success and a negative error code in case of
492 * failure.
493 */
494int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
495{
496 int err;
497
498 /*
499 * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
500 * writes during commit. Its only short "commit" start phase when
501 * writers are blocked.
502 */
503 mutex_lock(&c->log_mutex);
504
505 dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
506 c->ltail_lnum, ltail_lnum);
507
508 c->ltail_lnum = ltail_lnum;
509 /*
510 * The commit is finished and from now on it must be guaranteed that
511 * there is always enough space for the next commit.
512 */
513 c->min_log_bytes = c->leb_size;
514
515 spin_lock(&c->buds_lock);
516 c->bud_bytes -= c->cmt_bud_bytes;
517 spin_unlock(&c->buds_lock);
518
519 err = dbg_check_bud_bytes(c);
520
521 mutex_unlock(&c->log_mutex);
522 return err;
523}
524
525/**
526 * ubifs_log_post_commit - things to do after commit is completed.
527 * @c: UBIFS file-system description object
528 * @old_ltail_lnum: old log tail LEB number
529 *
530 * Release buds only after commit is completed, because they must be unchanged
531 * if recovery is needed.
532 *
533 * Unmap log LEBs only after commit is completed, because they may be needed for
534 * recovery.
535 *
536 * This function returns %0 on success and a negative error code on failure.
537 */
538int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
539{
540 int lnum, err = 0;
541
542 while (!list_empty(&c->old_buds)) {
543 struct ubifs_bud *bud;
544
545 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
546 err = ubifs_return_leb(c, bud->lnum);
547 if (err)
548 return err;
549 list_del(&bud->list);
550 kfree(bud);
551 }
552 mutex_lock(&c->log_mutex);
553 for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
554 lnum = next_log_lnum(c, lnum)) {
555 dbg_log("unmap log LEB %d", lnum);
556 err = ubifs_leb_unmap(c, lnum);
557 if (err)
558 goto out;
559 }
560out:
561 mutex_unlock(&c->log_mutex);
562 return err;
563}
564
565/**
566 * struct done_ref - references that have been done.
567 * @rb: rb-tree node
568 * @lnum: LEB number
569 */
570struct done_ref {
571 struct rb_node rb;
572 int lnum;
573};
574
575/**
576 * done_already - determine if a reference has been done already.
577 * @done_tree: rb-tree to store references that have been done
578 * @lnum: LEB number of reference
579 *
580 * This function returns %1 if the reference has been done, %0 if not, otherwise
581 * a negative error code is returned.
582 */
583static int done_already(struct rb_root *done_tree, int lnum)
584{
585 struct rb_node **p = &done_tree->rb_node, *parent = NULL;
586 struct done_ref *dr;
587
588 while (*p) {
589 parent = *p;
590 dr = rb_entry(parent, struct done_ref, rb);
591 if (lnum < dr->lnum)
592 p = &(*p)->rb_left;
593 else if (lnum > dr->lnum)
594 p = &(*p)->rb_right;
595 else
596 return 1;
597 }
598
599 dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
600 if (!dr)
601 return -ENOMEM;
602
603 dr->lnum = lnum;
604
605 rb_link_node(&dr->rb, parent, p);
606 rb_insert_color(&dr->rb, done_tree);
607
608 return 0;
609}
610
611/**
612 * destroy_done_tree - destroy the done tree.
613 * @done_tree: done tree to destroy
614 */
615static void destroy_done_tree(struct rb_root *done_tree)
616{
617 struct rb_node *this = done_tree->rb_node;
618 struct done_ref *dr;
619
620 while (this) {
621 if (this->rb_left) {
622 this = this->rb_left;
623 continue;
624 } else if (this->rb_right) {
625 this = this->rb_right;
626 continue;
627 }
628 dr = rb_entry(this, struct done_ref, rb);
629 this = rb_parent(this);
630 if (this) {
631 if (this->rb_left == &dr->rb)
632 this->rb_left = NULL;
633 else
634 this->rb_right = NULL;
635 }
636 kfree(dr);
637 }
638}
639
640/**
641 * add_node - add a node to the consolidated log.
642 * @c: UBIFS file-system description object
643 * @buf: buffer to which to add
644 * @lnum: LEB number to which to write is passed and returned here
645 * @offs: offset to where to write is passed and returned here
646 * @node: node to add
647 *
648 * This function returns %0 on success and a negative error code on failure.
649 */
650static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
651 void *node)
652{
653 struct ubifs_ch *ch = node;
654 int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
655
656 if (len > remains) {
657 int sz = ALIGN(*offs, c->min_io_size), err;
658
659 ubifs_pad(c, buf + *offs, sz - *offs);
660 err = ubifs_leb_change(c, *lnum, buf, sz, UBI_SHORTTERM);
661 if (err)
662 return err;
663 *lnum = next_log_lnum(c, *lnum);
664 *offs = 0;
665 }
666 memcpy(buf + *offs, node, len);
667 *offs += ALIGN(len, 8);
668 return 0;
669}
670
671/**
672 * ubifs_consolidate_log - consolidate the log.
673 * @c: UBIFS file-system description object
674 *
675 * Repeated failed commits could cause the log to be full, but at least 1 LEB is
676 * needed for commit. This function rewrites the reference nodes in the log
677 * omitting duplicates, and failed CS nodes, and leaving no gaps.
678 *
679 * This function returns %0 on success and a negative error code on failure.
680 */
681int ubifs_consolidate_log(struct ubifs_info *c)
682{
683 struct ubifs_scan_leb *sleb;
684 struct ubifs_scan_node *snod;
685 struct rb_root done_tree = RB_ROOT;
686 int lnum, err, first = 1, write_lnum, offs = 0;
687 void *buf;
688
689 dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
690 c->lhead_lnum);
691 buf = vmalloc(c->leb_size);
692 if (!buf)
693 return -ENOMEM;
694 lnum = c->ltail_lnum;
695 write_lnum = lnum;
696 while (1) {
697 sleb = ubifs_scan(c, lnum, 0, c->sbuf);
698 if (IS_ERR(sleb)) {
699 err = PTR_ERR(sleb);
700 goto out_free;
701 }
702 list_for_each_entry(snod, &sleb->nodes, list) {
703 switch (snod->type) {
704 case UBIFS_REF_NODE: {
705 struct ubifs_ref_node *ref = snod->node;
706 int ref_lnum = le32_to_cpu(ref->lnum);
707
708 err = done_already(&done_tree, ref_lnum);
709 if (err < 0)
710 goto out_scan;
711 if (err != 1) {
712 err = add_node(c, buf, &write_lnum,
713 &offs, snod->node);
714 if (err)
715 goto out_scan;
716 }
717 break;
718 }
719 case UBIFS_CS_NODE:
720 if (!first)
721 break;
722 err = add_node(c, buf, &write_lnum, &offs,
723 snod->node);
724 if (err)
725 goto out_scan;
726 first = 0;
727 break;
728 }
729 }
730 ubifs_scan_destroy(sleb);
731 if (lnum == c->lhead_lnum)
732 break;
733 lnum = next_log_lnum(c, lnum);
734 }
735 if (offs) {
736 int sz = ALIGN(offs, c->min_io_size);
737
738 ubifs_pad(c, buf + offs, sz - offs);
739 err = ubifs_leb_change(c, write_lnum, buf, sz, UBI_SHORTTERM);
740 if (err)
741 goto out_free;
742 offs = ALIGN(offs, c->min_io_size);
743 }
744 destroy_done_tree(&done_tree);
745 vfree(buf);
746 if (write_lnum == c->lhead_lnum) {
747 ubifs_err("log is too full");
748 return -EINVAL;
749 }
750 /* Unmap remaining LEBs */
751 lnum = write_lnum;
752 do {
753 lnum = next_log_lnum(c, lnum);
754 err = ubifs_leb_unmap(c, lnum);
755 if (err)
756 return err;
757 } while (lnum != c->lhead_lnum);
758 c->lhead_lnum = write_lnum;
759 c->lhead_offs = offs;
760 dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
761 return 0;
762
763out_scan:
764 ubifs_scan_destroy(sleb);
765out_free:
766 destroy_done_tree(&done_tree);
767 vfree(buf);
768 return err;
769}
770
771#ifdef CONFIG_UBIFS_FS_DEBUG
772
773/**
774 * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
775 * @c: UBIFS file-system description object
776 *
777 * This function makes sure the amount of flash space used by closed buds
778 * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
779 * case of failure.
780 */
781static int dbg_check_bud_bytes(struct ubifs_info *c)
782{
783 int i, err = 0;
784 struct ubifs_bud *bud;
785 long long bud_bytes = 0;
786
787 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
788 return 0;
789
790 spin_lock(&c->buds_lock);
791 for (i = 0; i < c->jhead_cnt; i++)
792 list_for_each_entry(bud, &c->jheads[i].buds_list, list)
793 bud_bytes += c->leb_size - bud->start;
794
795 if (c->bud_bytes != bud_bytes) {
796 ubifs_err("bad bud_bytes %lld, calculated %lld",
797 c->bud_bytes, bud_bytes);
798 err = -EINVAL;
799 }
800 spin_unlock(&c->buds_lock);
801
802 return err;
803}
804
805#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/lprops.c b/fs/ubifs/lprops.c
new file mode 100644
index 000000000000..2ba93da71b65
--- /dev/null
+++ b/fs/ubifs/lprops.c
@@ -0,0 +1,1357 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements the functions that access LEB properties and their
25 * categories. LEBs are categorized based on the needs of UBIFS, and the
26 * categories are stored as either heaps or lists to provide a fast way of
27 * finding a LEB in a particular category. For example, UBIFS may need to find
28 * an empty LEB for the journal, or a very dirty LEB for garbage collection.
29 */
30
31#include "ubifs.h"
32
33/**
34 * get_heap_comp_val - get the LEB properties value for heap comparisons.
35 * @lprops: LEB properties
36 * @cat: LEB category
37 */
38static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
39{
40 switch (cat) {
41 case LPROPS_FREE:
42 return lprops->free;
43 case LPROPS_DIRTY_IDX:
44 return lprops->free + lprops->dirty;
45 default:
46 return lprops->dirty;
47 }
48}
49
50/**
51 * move_up_lpt_heap - move a new heap entry up as far as possible.
52 * @c: UBIFS file-system description object
53 * @heap: LEB category heap
54 * @lprops: LEB properties to move
55 * @cat: LEB category
56 *
57 * New entries to a heap are added at the bottom and then moved up until the
58 * parent's value is greater. In the case of LPT's category heaps, the value
59 * is either the amount of free space or the amount of dirty space, depending
60 * on the category.
61 */
62static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
63 struct ubifs_lprops *lprops, int cat)
64{
65 int val1, val2, hpos;
66
67 hpos = lprops->hpos;
68 if (!hpos)
69 return; /* Already top of the heap */
70 val1 = get_heap_comp_val(lprops, cat);
71 /* Compare to parent and, if greater, move up the heap */
72 do {
73 int ppos = (hpos - 1) / 2;
74
75 val2 = get_heap_comp_val(heap->arr[ppos], cat);
76 if (val2 >= val1)
77 return;
78 /* Greater than parent so move up */
79 heap->arr[ppos]->hpos = hpos;
80 heap->arr[hpos] = heap->arr[ppos];
81 heap->arr[ppos] = lprops;
82 lprops->hpos = ppos;
83 hpos = ppos;
84 } while (hpos);
85}
86
87/**
88 * adjust_lpt_heap - move a changed heap entry up or down the heap.
89 * @c: UBIFS file-system description object
90 * @heap: LEB category heap
91 * @lprops: LEB properties to move
92 * @hpos: heap position of @lprops
93 * @cat: LEB category
94 *
95 * Changed entries in a heap are moved up or down until the parent's value is
96 * greater. In the case of LPT's category heaps, the value is either the amount
97 * of free space or the amount of dirty space, depending on the category.
98 */
99static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
100 struct ubifs_lprops *lprops, int hpos, int cat)
101{
102 int val1, val2, val3, cpos;
103
104 val1 = get_heap_comp_val(lprops, cat);
105 /* Compare to parent and, if greater than parent, move up the heap */
106 if (hpos) {
107 int ppos = (hpos - 1) / 2;
108
109 val2 = get_heap_comp_val(heap->arr[ppos], cat);
110 if (val1 > val2) {
111 /* Greater than parent so move up */
112 while (1) {
113 heap->arr[ppos]->hpos = hpos;
114 heap->arr[hpos] = heap->arr[ppos];
115 heap->arr[ppos] = lprops;
116 lprops->hpos = ppos;
117 hpos = ppos;
118 if (!hpos)
119 return;
120 ppos = (hpos - 1) / 2;
121 val2 = get_heap_comp_val(heap->arr[ppos], cat);
122 if (val1 <= val2)
123 return;
124 /* Still greater than parent so keep going */
125 }
126 }
127 }
128 /* Not greater than parent, so compare to children */
129 while (1) {
130 /* Compare to left child */
131 cpos = hpos * 2 + 1;
132 if (cpos >= heap->cnt)
133 return;
134 val2 = get_heap_comp_val(heap->arr[cpos], cat);
135 if (val1 < val2) {
136 /* Less than left child, so promote biggest child */
137 if (cpos + 1 < heap->cnt) {
138 val3 = get_heap_comp_val(heap->arr[cpos + 1],
139 cat);
140 if (val3 > val2)
141 cpos += 1; /* Right child is bigger */
142 }
143 heap->arr[cpos]->hpos = hpos;
144 heap->arr[hpos] = heap->arr[cpos];
145 heap->arr[cpos] = lprops;
146 lprops->hpos = cpos;
147 hpos = cpos;
148 continue;
149 }
150 /* Compare to right child */
151 cpos += 1;
152 if (cpos >= heap->cnt)
153 return;
154 val3 = get_heap_comp_val(heap->arr[cpos], cat);
155 if (val1 < val3) {
156 /* Less than right child, so promote right child */
157 heap->arr[cpos]->hpos = hpos;
158 heap->arr[hpos] = heap->arr[cpos];
159 heap->arr[cpos] = lprops;
160 lprops->hpos = cpos;
161 hpos = cpos;
162 continue;
163 }
164 return;
165 }
166}
167
168/**
169 * add_to_lpt_heap - add LEB properties to a LEB category heap.
170 * @c: UBIFS file-system description object
171 * @lprops: LEB properties to add
172 * @cat: LEB category
173 *
174 * This function returns %1 if @lprops is added to the heap for LEB category
175 * @cat, otherwise %0 is returned because the heap is full.
176 */
177static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops,
178 int cat)
179{
180 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
181
182 if (heap->cnt >= heap->max_cnt) {
183 const int b = LPT_HEAP_SZ / 2 - 1;
184 int cpos, val1, val2;
185
186 /* Compare to some other LEB on the bottom of heap */
187 /* Pick a position kind of randomly */
188 cpos = (((size_t)lprops >> 4) & b) + b;
189 ubifs_assert(cpos >= b);
190 ubifs_assert(cpos < LPT_HEAP_SZ);
191 ubifs_assert(cpos < heap->cnt);
192
193 val1 = get_heap_comp_val(lprops, cat);
194 val2 = get_heap_comp_val(heap->arr[cpos], cat);
195 if (val1 > val2) {
196 struct ubifs_lprops *lp;
197
198 lp = heap->arr[cpos];
199 lp->flags &= ~LPROPS_CAT_MASK;
200 lp->flags |= LPROPS_UNCAT;
201 list_add(&lp->list, &c->uncat_list);
202 lprops->hpos = cpos;
203 heap->arr[cpos] = lprops;
204 move_up_lpt_heap(c, heap, lprops, cat);
205 dbg_check_heap(c, heap, cat, lprops->hpos);
206 return 1; /* Added to heap */
207 }
208 dbg_check_heap(c, heap, cat, -1);
209 return 0; /* Not added to heap */
210 } else {
211 lprops->hpos = heap->cnt++;
212 heap->arr[lprops->hpos] = lprops;
213 move_up_lpt_heap(c, heap, lprops, cat);
214 dbg_check_heap(c, heap, cat, lprops->hpos);
215 return 1; /* Added to heap */
216 }
217}
218
219/**
220 * remove_from_lpt_heap - remove LEB properties from a LEB category heap.
221 * @c: UBIFS file-system description object
222 * @lprops: LEB properties to remove
223 * @cat: LEB category
224 */
225static void remove_from_lpt_heap(struct ubifs_info *c,
226 struct ubifs_lprops *lprops, int cat)
227{
228 struct ubifs_lpt_heap *heap;
229 int hpos = lprops->hpos;
230
231 heap = &c->lpt_heap[cat - 1];
232 ubifs_assert(hpos >= 0 && hpos < heap->cnt);
233 ubifs_assert(heap->arr[hpos] == lprops);
234 heap->cnt -= 1;
235 if (hpos < heap->cnt) {
236 heap->arr[hpos] = heap->arr[heap->cnt];
237 heap->arr[hpos]->hpos = hpos;
238 adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat);
239 }
240 dbg_check_heap(c, heap, cat, -1);
241}
242
243/**
244 * lpt_heap_replace - replace lprops in a category heap.
245 * @c: UBIFS file-system description object
246 * @old_lprops: LEB properties to replace
247 * @new_lprops: LEB properties with which to replace
248 * @cat: LEB category
249 *
250 * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
251 * and the lprops that the pnode contains. When that happens, references in
252 * the category heaps to those lprops must be updated to point to the new
253 * lprops. This function does that.
254 */
255static void lpt_heap_replace(struct ubifs_info *c,
256 struct ubifs_lprops *old_lprops,
257 struct ubifs_lprops *new_lprops, int cat)
258{
259 struct ubifs_lpt_heap *heap;
260 int hpos = new_lprops->hpos;
261
262 heap = &c->lpt_heap[cat - 1];
263 heap->arr[hpos] = new_lprops;
264}
265
266/**
267 * ubifs_add_to_cat - add LEB properties to a category list or heap.
268 * @c: UBIFS file-system description object
269 * @lprops: LEB properties to add
270 * @cat: LEB category to which to add
271 *
272 * LEB properties are categorized to enable fast find operations.
273 */
274void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
275 int cat)
276{
277 switch (cat) {
278 case LPROPS_DIRTY:
279 case LPROPS_DIRTY_IDX:
280 case LPROPS_FREE:
281 if (add_to_lpt_heap(c, lprops, cat))
282 break;
283 /* No more room on heap so make it uncategorized */
284 cat = LPROPS_UNCAT;
285 /* Fall through */
286 case LPROPS_UNCAT:
287 list_add(&lprops->list, &c->uncat_list);
288 break;
289 case LPROPS_EMPTY:
290 list_add(&lprops->list, &c->empty_list);
291 break;
292 case LPROPS_FREEABLE:
293 list_add(&lprops->list, &c->freeable_list);
294 c->freeable_cnt += 1;
295 break;
296 case LPROPS_FRDI_IDX:
297 list_add(&lprops->list, &c->frdi_idx_list);
298 break;
299 default:
300 ubifs_assert(0);
301 }
302 lprops->flags &= ~LPROPS_CAT_MASK;
303 lprops->flags |= cat;
304}
305
306/**
307 * ubifs_remove_from_cat - remove LEB properties from a category list or heap.
308 * @c: UBIFS file-system description object
309 * @lprops: LEB properties to remove
310 * @cat: LEB category from which to remove
311 *
312 * LEB properties are categorized to enable fast find operations.
313 */
314static void ubifs_remove_from_cat(struct ubifs_info *c,
315 struct ubifs_lprops *lprops, int cat)
316{
317 switch (cat) {
318 case LPROPS_DIRTY:
319 case LPROPS_DIRTY_IDX:
320 case LPROPS_FREE:
321 remove_from_lpt_heap(c, lprops, cat);
322 break;
323 case LPROPS_FREEABLE:
324 c->freeable_cnt -= 1;
325 ubifs_assert(c->freeable_cnt >= 0);
326 /* Fall through */
327 case LPROPS_UNCAT:
328 case LPROPS_EMPTY:
329 case LPROPS_FRDI_IDX:
330 ubifs_assert(!list_empty(&lprops->list));
331 list_del(&lprops->list);
332 break;
333 default:
334 ubifs_assert(0);
335 }
336}
337
338/**
339 * ubifs_replace_cat - replace lprops in a category list or heap.
340 * @c: UBIFS file-system description object
341 * @old_lprops: LEB properties to replace
342 * @new_lprops: LEB properties with which to replace
343 *
344 * During commit it is sometimes necessary to copy a pnode (see dirty_cow_pnode)
345 * and the lprops that the pnode contains. When that happens, references in
346 * category lists and heaps must be replaced. This function does that.
347 */
348void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
349 struct ubifs_lprops *new_lprops)
350{
351 int cat;
352
353 cat = new_lprops->flags & LPROPS_CAT_MASK;
354 switch (cat) {
355 case LPROPS_DIRTY:
356 case LPROPS_DIRTY_IDX:
357 case LPROPS_FREE:
358 lpt_heap_replace(c, old_lprops, new_lprops, cat);
359 break;
360 case LPROPS_UNCAT:
361 case LPROPS_EMPTY:
362 case LPROPS_FREEABLE:
363 case LPROPS_FRDI_IDX:
364 list_replace(&old_lprops->list, &new_lprops->list);
365 break;
366 default:
367 ubifs_assert(0);
368 }
369}
370
371/**
372 * ubifs_ensure_cat - ensure LEB properties are categorized.
373 * @c: UBIFS file-system description object
374 * @lprops: LEB properties
375 *
376 * A LEB may have fallen off of the bottom of a heap, and ended up as
377 * uncategorized even though it has enough space for us now. If that is the case
378 * this function will put the LEB back onto a heap.
379 */
380void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
381{
382 int cat = lprops->flags & LPROPS_CAT_MASK;
383
384 if (cat != LPROPS_UNCAT)
385 return;
386 cat = ubifs_categorize_lprops(c, lprops);
387 if (cat == LPROPS_UNCAT)
388 return;
389 ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
390 ubifs_add_to_cat(c, lprops, cat);
391}
392
393/**
394 * ubifs_categorize_lprops - categorize LEB properties.
395 * @c: UBIFS file-system description object
396 * @lprops: LEB properties to categorize
397 *
398 * LEB properties are categorized to enable fast find operations. This function
399 * returns the LEB category to which the LEB properties belong. Note however
400 * that if the LEB category is stored as a heap and the heap is full, the
401 * LEB properties may have their category changed to %LPROPS_UNCAT.
402 */
403int ubifs_categorize_lprops(const struct ubifs_info *c,
404 const struct ubifs_lprops *lprops)
405{
406 if (lprops->flags & LPROPS_TAKEN)
407 return LPROPS_UNCAT;
408
409 if (lprops->free == c->leb_size) {
410 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
411 return LPROPS_EMPTY;
412 }
413
414 if (lprops->free + lprops->dirty == c->leb_size) {
415 if (lprops->flags & LPROPS_INDEX)
416 return LPROPS_FRDI_IDX;
417 else
418 return LPROPS_FREEABLE;
419 }
420
421 if (lprops->flags & LPROPS_INDEX) {
422 if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
423 return LPROPS_DIRTY_IDX;
424 } else {
425 if (lprops->dirty >= c->dead_wm &&
426 lprops->dirty > lprops->free)
427 return LPROPS_DIRTY;
428 if (lprops->free > 0)
429 return LPROPS_FREE;
430 }
431
432 return LPROPS_UNCAT;
433}
434
435/**
436 * change_category - change LEB properties category.
437 * @c: UBIFS file-system description object
438 * @lprops: LEB properties to recategorize
439 *
440 * LEB properties are categorized to enable fast find operations. When the LEB
441 * properties change they must be recategorized.
442 */
443static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
444{
445 int old_cat = lprops->flags & LPROPS_CAT_MASK;
446 int new_cat = ubifs_categorize_lprops(c, lprops);
447
448 if (old_cat == new_cat) {
449 struct ubifs_lpt_heap *heap = &c->lpt_heap[new_cat - 1];
450
451 /* lprops on a heap now must be moved up or down */
452 if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
453 return; /* Not on a heap */
454 heap = &c->lpt_heap[new_cat - 1];
455 adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
456 } else {
457 ubifs_remove_from_cat(c, lprops, old_cat);
458 ubifs_add_to_cat(c, lprops, new_cat);
459 }
460}
461
462/**
463 * ubifs_get_lprops - get reference to LEB properties.
464 * @c: the UBIFS file-system description object
465 *
466 * This function locks lprops. Lprops have to be unlocked by
467 * 'ubifs_release_lprops()'.
468 */
469void ubifs_get_lprops(struct ubifs_info *c)
470{
471 mutex_lock(&c->lp_mutex);
472}
473
474/**
475 * calc_dark - calculate LEB dark space size.
476 * @c: the UBIFS file-system description object
477 * @spc: amount of free and dirty space in the LEB
478 *
479 * This function calculates amount of dark space in an LEB which has @spc bytes
480 * of free and dirty space. Returns the calculations result.
481 *
482 * Dark space is the space which is not always usable - it depends on which
483 * nodes are written in which order. E.g., if an LEB has only 512 free bytes,
484 * it is dark space, because it cannot fit a large data node. So UBIFS cannot
485 * count on this LEB and treat these 512 bytes as usable because it is not true
486 * if, for example, only big chunks of uncompressible data will be written to
487 * the FS.
488 */
489static int calc_dark(struct ubifs_info *c, int spc)
490{
491 ubifs_assert(!(spc & 7));
492
493 if (spc < c->dark_wm)
494 return spc;
495
496 /*
497 * If we have slightly more space then the dark space watermark, we can
498 * anyway safely assume it we'll be able to write a node of the
499 * smallest size there.
500 */
501 if (spc - c->dark_wm < MIN_WRITE_SZ)
502 return spc - MIN_WRITE_SZ;
503
504 return c->dark_wm;
505}
506
507/**
508 * is_lprops_dirty - determine if LEB properties are dirty.
509 * @c: the UBIFS file-system description object
510 * @lprops: LEB properties to test
511 */
512static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
513{
514 struct ubifs_pnode *pnode;
515 int pos;
516
517 pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
518 pnode = (struct ubifs_pnode *)container_of(lprops - pos,
519 struct ubifs_pnode,
520 lprops[0]);
521 return !test_bit(COW_ZNODE, &pnode->flags) &&
522 test_bit(DIRTY_CNODE, &pnode->flags);
523}
524
525/**
526 * ubifs_change_lp - change LEB properties.
527 * @c: the UBIFS file-system description object
528 * @lp: LEB properties to change
529 * @free: new free space amount
530 * @dirty: new dirty space amount
531 * @flags: new flags
532 * @idx_gc_cnt: change to the count of idx_gc list
533 *
534 * This function changes LEB properties. This function does not change a LEB
535 * property (@free, @dirty or @flag) if the value passed is %LPROPS_NC.
536 *
537 * This function returns a pointer to the updated LEB properties on success
538 * and a negative error code on failure. N.B. the LEB properties may have had to
539 * be copied (due to COW) and consequently the pointer returned may not be the
540 * same as the pointer passed.
541 */
542const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
543 const struct ubifs_lprops *lp,
544 int free, int dirty, int flags,
545 int idx_gc_cnt)
546{
547 /*
548 * This is the only function that is allowed to change lprops, so we
549 * discard the const qualifier.
550 */
551 struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
552
553 dbg_lp("LEB %d, free %d, dirty %d, flags %d",
554 lprops->lnum, free, dirty, flags);
555
556 ubifs_assert(mutex_is_locked(&c->lp_mutex));
557 ubifs_assert(c->lst.empty_lebs >= 0 &&
558 c->lst.empty_lebs <= c->main_lebs);
559 ubifs_assert(c->freeable_cnt >= 0);
560 ubifs_assert(c->freeable_cnt <= c->main_lebs);
561 ubifs_assert(c->lst.taken_empty_lebs >= 0);
562 ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs);
563 ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
564 ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
565 ubifs_assert(!(c->lst.total_used & 7));
566 ubifs_assert(free == LPROPS_NC || free >= 0);
567 ubifs_assert(dirty == LPROPS_NC || dirty >= 0);
568
569 if (!is_lprops_dirty(c, lprops)) {
570 lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
571 if (IS_ERR(lprops))
572 return lprops;
573 } else
574 ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
575
576 ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
577
578 spin_lock(&c->space_lock);
579
580 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
581 c->lst.taken_empty_lebs -= 1;
582
583 if (!(lprops->flags & LPROPS_INDEX)) {
584 int old_spc;
585
586 old_spc = lprops->free + lprops->dirty;
587 if (old_spc < c->dead_wm)
588 c->lst.total_dead -= old_spc;
589 else
590 c->lst.total_dark -= calc_dark(c, old_spc);
591
592 c->lst.total_used -= c->leb_size - old_spc;
593 }
594
595 if (free != LPROPS_NC) {
596 free = ALIGN(free, 8);
597 c->lst.total_free += free - lprops->free;
598
599 /* Increase or decrease empty LEBs counter if needed */
600 if (free == c->leb_size) {
601 if (lprops->free != c->leb_size)
602 c->lst.empty_lebs += 1;
603 } else if (lprops->free == c->leb_size)
604 c->lst.empty_lebs -= 1;
605 lprops->free = free;
606 }
607
608 if (dirty != LPROPS_NC) {
609 dirty = ALIGN(dirty, 8);
610 c->lst.total_dirty += dirty - lprops->dirty;
611 lprops->dirty = dirty;
612 }
613
614 if (flags != LPROPS_NC) {
615 /* Take care about indexing LEBs counter if needed */
616 if ((lprops->flags & LPROPS_INDEX)) {
617 if (!(flags & LPROPS_INDEX))
618 c->lst.idx_lebs -= 1;
619 } else if (flags & LPROPS_INDEX)
620 c->lst.idx_lebs += 1;
621 lprops->flags = flags;
622 }
623
624 if (!(lprops->flags & LPROPS_INDEX)) {
625 int new_spc;
626
627 new_spc = lprops->free + lprops->dirty;
628 if (new_spc < c->dead_wm)
629 c->lst.total_dead += new_spc;
630 else
631 c->lst.total_dark += calc_dark(c, new_spc);
632
633 c->lst.total_used += c->leb_size - new_spc;
634 }
635
636 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
637 c->lst.taken_empty_lebs += 1;
638
639 change_category(c, lprops);
640
641 c->idx_gc_cnt += idx_gc_cnt;
642
643 spin_unlock(&c->space_lock);
644
645 return lprops;
646}
647
648/**
649 * ubifs_release_lprops - release lprops lock.
650 * @c: the UBIFS file-system description object
651 *
652 * This function has to be called after each 'ubifs_get_lprops()' call to
653 * unlock lprops.
654 */
655void ubifs_release_lprops(struct ubifs_info *c)
656{
657 ubifs_assert(mutex_is_locked(&c->lp_mutex));
658 ubifs_assert(c->lst.empty_lebs >= 0 &&
659 c->lst.empty_lebs <= c->main_lebs);
660
661 mutex_unlock(&c->lp_mutex);
662}
663
664/**
665 * ubifs_get_lp_stats - get lprops statistics.
666 * @c: UBIFS file-system description object
667 * @st: return statistics
668 */
669void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *st)
670{
671 spin_lock(&c->space_lock);
672 memcpy(st, &c->lst, sizeof(struct ubifs_lp_stats));
673 spin_unlock(&c->space_lock);
674}
675
676/**
677 * ubifs_change_one_lp - change LEB properties.
678 * @c: the UBIFS file-system description object
679 * @lnum: LEB to change properties for
680 * @free: amount of free space
681 * @dirty: amount of dirty space
682 * @flags_set: flags to set
683 * @flags_clean: flags to clean
684 * @idx_gc_cnt: change to the count of idx_gc list
685 *
686 * This function changes properties of LEB @lnum. It is a helper wrapper over
687 * 'ubifs_change_lp()' which hides lprops get/release. The arguments are the
688 * same as in case of 'ubifs_change_lp()'. Returns zero in case of success and
689 * a negative error code in case of failure.
690 */
691int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
692 int flags_set, int flags_clean, int idx_gc_cnt)
693{
694 int err = 0, flags;
695 const struct ubifs_lprops *lp;
696
697 ubifs_get_lprops(c);
698
699 lp = ubifs_lpt_lookup_dirty(c, lnum);
700 if (IS_ERR(lp)) {
701 err = PTR_ERR(lp);
702 goto out;
703 }
704
705 flags = (lp->flags | flags_set) & ~flags_clean;
706 lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
707 if (IS_ERR(lp))
708 err = PTR_ERR(lp);
709
710out:
711 ubifs_release_lprops(c);
712 return err;
713}
714
715/**
716 * ubifs_update_one_lp - update LEB properties.
717 * @c: the UBIFS file-system description object
718 * @lnum: LEB to change properties for
719 * @free: amount of free space
720 * @dirty: amount of dirty space to add
721 * @flags_set: flags to set
722 * @flags_clean: flags to clean
723 *
724 * This function is the same as 'ubifs_change_one_lp()' but @dirty is added to
725 * current dirty space, not substitutes it.
726 */
727int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
728 int flags_set, int flags_clean)
729{
730 int err = 0, flags;
731 const struct ubifs_lprops *lp;
732
733 ubifs_get_lprops(c);
734
735 lp = ubifs_lpt_lookup_dirty(c, lnum);
736 if (IS_ERR(lp)) {
737 err = PTR_ERR(lp);
738 goto out;
739 }
740
741 flags = (lp->flags | flags_set) & ~flags_clean;
742 lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
743 if (IS_ERR(lp))
744 err = PTR_ERR(lp);
745
746out:
747 ubifs_release_lprops(c);
748 return err;
749}
750
751/**
752 * ubifs_read_one_lp - read LEB properties.
753 * @c: the UBIFS file-system description object
754 * @lnum: LEB to read properties for
755 * @lp: where to store read properties
756 *
757 * This helper function reads properties of a LEB @lnum and stores them in @lp.
758 * Returns zero in case of success and a negative error code in case of
759 * failure.
760 */
761int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
762{
763 int err = 0;
764 const struct ubifs_lprops *lpp;
765
766 ubifs_get_lprops(c);
767
768 lpp = ubifs_lpt_lookup(c, lnum);
769 if (IS_ERR(lpp)) {
770 err = PTR_ERR(lpp);
771 goto out;
772 }
773
774 memcpy(lp, lpp, sizeof(struct ubifs_lprops));
775
776out:
777 ubifs_release_lprops(c);
778 return err;
779}
780
781/**
782 * ubifs_fast_find_free - try to find a LEB with free space quickly.
783 * @c: the UBIFS file-system description object
784 *
785 * This function returns LEB properties for a LEB with free space or %NULL if
786 * the function is unable to find a LEB quickly.
787 */
788const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
789{
790 struct ubifs_lprops *lprops;
791 struct ubifs_lpt_heap *heap;
792
793 ubifs_assert(mutex_is_locked(&c->lp_mutex));
794
795 heap = &c->lpt_heap[LPROPS_FREE - 1];
796 if (heap->cnt == 0)
797 return NULL;
798
799 lprops = heap->arr[0];
800 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
801 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
802 return lprops;
803}
804
805/**
806 * ubifs_fast_find_empty - try to find an empty LEB quickly.
807 * @c: the UBIFS file-system description object
808 *
809 * This function returns LEB properties for an empty LEB or %NULL if the
810 * function is unable to find an empty LEB quickly.
811 */
812const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
813{
814 struct ubifs_lprops *lprops;
815
816 ubifs_assert(mutex_is_locked(&c->lp_mutex));
817
818 if (list_empty(&c->empty_list))
819 return NULL;
820
821 lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
822 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
823 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
824 ubifs_assert(lprops->free == c->leb_size);
825 return lprops;
826}
827
828/**
829 * ubifs_fast_find_freeable - try to find a freeable LEB quickly.
830 * @c: the UBIFS file-system description object
831 *
832 * This function returns LEB properties for a freeable LEB or %NULL if the
833 * function is unable to find a freeable LEB quickly.
834 */
835const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
836{
837 struct ubifs_lprops *lprops;
838
839 ubifs_assert(mutex_is_locked(&c->lp_mutex));
840
841 if (list_empty(&c->freeable_list))
842 return NULL;
843
844 lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
845 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
846 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
847 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
848 ubifs_assert(c->freeable_cnt > 0);
849 return lprops;
850}
851
852/**
853 * ubifs_fast_find_frdi_idx - try to find a freeable index LEB quickly.
854 * @c: the UBIFS file-system description object
855 *
856 * This function returns LEB properties for a freeable index LEB or %NULL if the
857 * function is unable to find a freeable index LEB quickly.
858 */
859const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
860{
861 struct ubifs_lprops *lprops;
862
863 ubifs_assert(mutex_is_locked(&c->lp_mutex));
864
865 if (list_empty(&c->frdi_idx_list))
866 return NULL;
867
868 lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
869 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
870 ubifs_assert((lprops->flags & LPROPS_INDEX));
871 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
872 return lprops;
873}
874
875#ifdef CONFIG_UBIFS_FS_DEBUG
876
877/**
878 * dbg_check_cats - check category heaps and lists.
879 * @c: UBIFS file-system description object
880 *
881 * This function returns %0 on success and a negative error code on failure.
882 */
883int dbg_check_cats(struct ubifs_info *c)
884{
885 struct ubifs_lprops *lprops;
886 struct list_head *pos;
887 int i, cat;
888
889 if (!(ubifs_chk_flags & (UBIFS_CHK_GEN | UBIFS_CHK_LPROPS)))
890 return 0;
891
892 list_for_each_entry(lprops, &c->empty_list, list) {
893 if (lprops->free != c->leb_size) {
894 ubifs_err("non-empty LEB %d on empty list "
895 "(free %d dirty %d flags %d)", lprops->lnum,
896 lprops->free, lprops->dirty, lprops->flags);
897 return -EINVAL;
898 }
899 if (lprops->flags & LPROPS_TAKEN) {
900 ubifs_err("taken LEB %d on empty list "
901 "(free %d dirty %d flags %d)", lprops->lnum,
902 lprops->free, lprops->dirty, lprops->flags);
903 return -EINVAL;
904 }
905 }
906
907 i = 0;
908 list_for_each_entry(lprops, &c->freeable_list, list) {
909 if (lprops->free + lprops->dirty != c->leb_size) {
910 ubifs_err("non-freeable LEB %d on freeable list "
911 "(free %d dirty %d flags %d)", lprops->lnum,
912 lprops->free, lprops->dirty, lprops->flags);
913 return -EINVAL;
914 }
915 if (lprops->flags & LPROPS_TAKEN) {
916 ubifs_err("taken LEB %d on freeable list "
917 "(free %d dirty %d flags %d)", lprops->lnum,
918 lprops->free, lprops->dirty, lprops->flags);
919 return -EINVAL;
920 }
921 i += 1;
922 }
923 if (i != c->freeable_cnt) {
924 ubifs_err("freeable list count %d expected %d", i,
925 c->freeable_cnt);
926 return -EINVAL;
927 }
928
929 i = 0;
930 list_for_each(pos, &c->idx_gc)
931 i += 1;
932 if (i != c->idx_gc_cnt) {
933 ubifs_err("idx_gc list count %d expected %d", i,
934 c->idx_gc_cnt);
935 return -EINVAL;
936 }
937
938 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
939 if (lprops->free + lprops->dirty != c->leb_size) {
940 ubifs_err("non-freeable LEB %d on frdi_idx list "
941 "(free %d dirty %d flags %d)", lprops->lnum,
942 lprops->free, lprops->dirty, lprops->flags);
943 return -EINVAL;
944 }
945 if (lprops->flags & LPROPS_TAKEN) {
946 ubifs_err("taken LEB %d on frdi_idx list "
947 "(free %d dirty %d flags %d)", lprops->lnum,
948 lprops->free, lprops->dirty, lprops->flags);
949 return -EINVAL;
950 }
951 if (!(lprops->flags & LPROPS_INDEX)) {
952 ubifs_err("non-index LEB %d on frdi_idx list "
953 "(free %d dirty %d flags %d)", lprops->lnum,
954 lprops->free, lprops->dirty, lprops->flags);
955 return -EINVAL;
956 }
957 }
958
959 for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
960 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
961
962 for (i = 0; i < heap->cnt; i++) {
963 lprops = heap->arr[i];
964 if (!lprops) {
965 ubifs_err("null ptr in LPT heap cat %d", cat);
966 return -EINVAL;
967 }
968 if (lprops->hpos != i) {
969 ubifs_err("bad ptr in LPT heap cat %d", cat);
970 return -EINVAL;
971 }
972 if (lprops->flags & LPROPS_TAKEN) {
973 ubifs_err("taken LEB in LPT heap cat %d", cat);
974 return -EINVAL;
975 }
976 }
977 }
978
979 return 0;
980}
981
982void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
983 int add_pos)
984{
985 int i = 0, j, err = 0;
986
987 if (!(ubifs_chk_flags & (UBIFS_CHK_GEN | UBIFS_CHK_LPROPS)))
988 return;
989
990 for (i = 0; i < heap->cnt; i++) {
991 struct ubifs_lprops *lprops = heap->arr[i];
992 struct ubifs_lprops *lp;
993
994 if (i != add_pos)
995 if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
996 err = 1;
997 goto out;
998 }
999 if (lprops->hpos != i) {
1000 err = 2;
1001 goto out;
1002 }
1003 lp = ubifs_lpt_lookup(c, lprops->lnum);
1004 if (IS_ERR(lp)) {
1005 err = 3;
1006 goto out;
1007 }
1008 if (lprops != lp) {
1009 dbg_msg("lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
1010 (size_t)lprops, (size_t)lp, lprops->lnum,
1011 lp->lnum);
1012 err = 4;
1013 goto out;
1014 }
1015 for (j = 0; j < i; j++) {
1016 lp = heap->arr[j];
1017 if (lp == lprops) {
1018 err = 5;
1019 goto out;
1020 }
1021 if (lp->lnum == lprops->lnum) {
1022 err = 6;
1023 goto out;
1024 }
1025 }
1026 }
1027out:
1028 if (err) {
1029 dbg_msg("failed cat %d hpos %d err %d", cat, i, err);
1030 dbg_dump_stack();
1031 dbg_dump_heap(c, heap, cat);
1032 }
1033}
1034
1035/**
1036 * struct scan_check_data - data provided to scan callback function.
1037 * @lst: LEB properties statistics
1038 * @err: error code
1039 */
1040struct scan_check_data {
1041 struct ubifs_lp_stats lst;
1042 int err;
1043};
1044
1045/**
1046 * scan_check_cb - scan callback.
1047 * @c: the UBIFS file-system description object
1048 * @lp: LEB properties to scan
1049 * @in_tree: whether the LEB properties are in main memory
1050 * @data: information passed to and from the caller of the scan
1051 *
1052 * This function returns a code that indicates whether the scan should continue
1053 * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
1054 * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
1055 * (%LPT_SCAN_STOP).
1056 */
1057static int scan_check_cb(struct ubifs_info *c,
1058 const struct ubifs_lprops *lp, int in_tree,
1059 struct scan_check_data *data)
1060{
1061 struct ubifs_scan_leb *sleb;
1062 struct ubifs_scan_node *snod;
1063 struct ubifs_lp_stats *lst = &data->lst;
1064 int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty;
1065
1066 cat = lp->flags & LPROPS_CAT_MASK;
1067 if (cat != LPROPS_UNCAT) {
1068 cat = ubifs_categorize_lprops(c, lp);
1069 if (cat != (lp->flags & LPROPS_CAT_MASK)) {
1070 ubifs_err("bad LEB category %d expected %d",
1071 (lp->flags & LPROPS_CAT_MASK), cat);
1072 goto out;
1073 }
1074 }
1075
1076 /* Check lp is on its category list (if it has one) */
1077 if (in_tree) {
1078 struct list_head *list = NULL;
1079
1080 switch (cat) {
1081 case LPROPS_EMPTY:
1082 list = &c->empty_list;
1083 break;
1084 case LPROPS_FREEABLE:
1085 list = &c->freeable_list;
1086 break;
1087 case LPROPS_FRDI_IDX:
1088 list = &c->frdi_idx_list;
1089 break;
1090 case LPROPS_UNCAT:
1091 list = &c->uncat_list;
1092 break;
1093 }
1094 if (list) {
1095 struct ubifs_lprops *lprops;
1096 int found = 0;
1097
1098 list_for_each_entry(lprops, list, list) {
1099 if (lprops == lp) {
1100 found = 1;
1101 break;
1102 }
1103 }
1104 if (!found) {
1105 ubifs_err("bad LPT list (category %d)", cat);
1106 goto out;
1107 }
1108 }
1109 }
1110
1111 /* Check lp is on its category heap (if it has one) */
1112 if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
1113 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
1114
1115 if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
1116 lp != heap->arr[lp->hpos]) {
1117 ubifs_err("bad LPT heap (category %d)", cat);
1118 goto out;
1119 }
1120 }
1121
1122 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
1123 if (IS_ERR(sleb)) {
1124 /*
1125 * After an unclean unmount, empty and freeable LEBs
1126 * may contain garbage.
1127 */
1128 if (lp->free == c->leb_size) {
1129 ubifs_err("scan errors were in empty LEB "
1130 "- continuing checking");
1131 lst->empty_lebs += 1;
1132 lst->total_free += c->leb_size;
1133 lst->total_dark += calc_dark(c, c->leb_size);
1134 return LPT_SCAN_CONTINUE;
1135 }
1136
1137 if (lp->free + lp->dirty == c->leb_size &&
1138 !(lp->flags & LPROPS_INDEX)) {
1139 ubifs_err("scan errors were in freeable LEB "
1140 "- continuing checking");
1141 lst->total_free += lp->free;
1142 lst->total_dirty += lp->dirty;
1143 lst->total_dark += calc_dark(c, c->leb_size);
1144 return LPT_SCAN_CONTINUE;
1145 }
1146 data->err = PTR_ERR(sleb);
1147 return LPT_SCAN_STOP;
1148 }
1149
1150 is_idx = -1;
1151 list_for_each_entry(snod, &sleb->nodes, list) {
1152 int found, level = 0;
1153
1154 cond_resched();
1155
1156 if (is_idx == -1)
1157 is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
1158
1159 if (is_idx && snod->type != UBIFS_IDX_NODE) {
1160 ubifs_err("indexing node in data LEB %d:%d",
1161 lnum, snod->offs);
1162 goto out_destroy;
1163 }
1164
1165 if (snod->type == UBIFS_IDX_NODE) {
1166 struct ubifs_idx_node *idx = snod->node;
1167
1168 key_read(c, ubifs_idx_key(c, idx), &snod->key);
1169 level = le16_to_cpu(idx->level);
1170 }
1171
1172 found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
1173 snod->offs, is_idx);
1174 if (found) {
1175 if (found < 0)
1176 goto out_destroy;
1177 used += ALIGN(snod->len, 8);
1178 }
1179 }
1180
1181 free = c->leb_size - sleb->endpt;
1182 dirty = sleb->endpt - used;
1183
1184 if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
1185 dirty < 0) {
1186 ubifs_err("bad calculated accounting for LEB %d: "
1187 "free %d, dirty %d", lnum, free, dirty);
1188 goto out_destroy;
1189 }
1190
1191 if (lp->free + lp->dirty == c->leb_size &&
1192 free + dirty == c->leb_size)
1193 if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
1194 (!is_idx && free == c->leb_size) ||
1195 lp->free == c->leb_size) {
1196 /*
1197 * Empty or freeable LEBs could contain index
1198 * nodes from an uncompleted commit due to an
1199 * unclean unmount. Or they could be empty for
1200 * the same reason. Or it may simply not have been
1201 * unmapped.
1202 */
1203 free = lp->free;
1204 dirty = lp->dirty;
1205 is_idx = 0;
1206 }
1207
1208 if (is_idx && lp->free + lp->dirty == free + dirty &&
1209 lnum != c->ihead_lnum) {
1210 /*
1211 * After an unclean unmount, an index LEB could have a different
1212 * amount of free space than the value recorded by lprops. That
1213 * is because the in-the-gaps method may use free space or
1214 * create free space (as a side-effect of using ubi_leb_change
1215 * and not writing the whole LEB). The incorrect free space
1216 * value is not a problem because the index is only ever
1217 * allocated empty LEBs, so there will never be an attempt to
1218 * write to the free space at the end of an index LEB - except
1219 * by the in-the-gaps method for which it is not a problem.
1220 */
1221 free = lp->free;
1222 dirty = lp->dirty;
1223 }
1224
1225 if (lp->free != free || lp->dirty != dirty)
1226 goto out_print;
1227
1228 if (is_idx && !(lp->flags & LPROPS_INDEX)) {
1229 if (free == c->leb_size)
1230 /* Free but not unmapped LEB, it's fine */
1231 is_idx = 0;
1232 else {
1233 ubifs_err("indexing node without indexing "
1234 "flag");
1235 goto out_print;
1236 }
1237 }
1238
1239 if (!is_idx && (lp->flags & LPROPS_INDEX)) {
1240 ubifs_err("data node with indexing flag");
1241 goto out_print;
1242 }
1243
1244 if (free == c->leb_size)
1245 lst->empty_lebs += 1;
1246
1247 if (is_idx)
1248 lst->idx_lebs += 1;
1249
1250 if (!(lp->flags & LPROPS_INDEX))
1251 lst->total_used += c->leb_size - free - dirty;
1252 lst->total_free += free;
1253 lst->total_dirty += dirty;
1254
1255 if (!(lp->flags & LPROPS_INDEX)) {
1256 int spc = free + dirty;
1257
1258 if (spc < c->dead_wm)
1259 lst->total_dead += spc;
1260 else
1261 lst->total_dark += calc_dark(c, spc);
1262 }
1263
1264 ubifs_scan_destroy(sleb);
1265
1266 return LPT_SCAN_CONTINUE;
1267
1268out_print:
1269 ubifs_err("bad accounting of LEB %d: free %d, dirty %d flags %#x, "
1270 "should be free %d, dirty %d",
1271 lnum, lp->free, lp->dirty, lp->flags, free, dirty);
1272 dbg_dump_leb(c, lnum);
1273out_destroy:
1274 ubifs_scan_destroy(sleb);
1275out:
1276 data->err = -EINVAL;
1277 return LPT_SCAN_STOP;
1278}
1279
1280/**
1281 * dbg_check_lprops - check all LEB properties.
1282 * @c: UBIFS file-system description object
1283 *
1284 * This function checks all LEB properties and makes sure they are all correct.
1285 * It returns zero if everything is fine, %-EINVAL if there is an inconsistency
1286 * and other negative error codes in case of other errors. This function is
1287 * called while the file system is locked (because of commit start), so no
1288 * additional locking is required. Note that locking the LPT mutex would cause
1289 * a circular lock dependency with the TNC mutex.
1290 */
1291int dbg_check_lprops(struct ubifs_info *c)
1292{
1293 int i, err;
1294 struct scan_check_data data;
1295 struct ubifs_lp_stats *lst = &data.lst;
1296
1297 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1298 return 0;
1299
1300 /*
1301 * As we are going to scan the media, the write buffers have to be
1302 * synchronized.
1303 */
1304 for (i = 0; i < c->jhead_cnt; i++) {
1305 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1306 if (err)
1307 return err;
1308 }
1309
1310 memset(lst, 0, sizeof(struct ubifs_lp_stats));
1311
1312 data.err = 0;
1313 err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
1314 (ubifs_lpt_scan_callback)scan_check_cb,
1315 &data);
1316 if (err && err != -ENOSPC)
1317 goto out;
1318 if (data.err) {
1319 err = data.err;
1320 goto out;
1321 }
1322
1323 if (lst->empty_lebs != c->lst.empty_lebs ||
1324 lst->idx_lebs != c->lst.idx_lebs ||
1325 lst->total_free != c->lst.total_free ||
1326 lst->total_dirty != c->lst.total_dirty ||
1327 lst->total_used != c->lst.total_used) {
1328 ubifs_err("bad overall accounting");
1329 ubifs_err("calculated: empty_lebs %d, idx_lebs %d, "
1330 "total_free %lld, total_dirty %lld, total_used %lld",
1331 lst->empty_lebs, lst->idx_lebs, lst->total_free,
1332 lst->total_dirty, lst->total_used);
1333 ubifs_err("read from lprops: empty_lebs %d, idx_lebs %d, "
1334 "total_free %lld, total_dirty %lld, total_used %lld",
1335 c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
1336 c->lst.total_dirty, c->lst.total_used);
1337 err = -EINVAL;
1338 goto out;
1339 }
1340
1341 if (lst->total_dead != c->lst.total_dead ||
1342 lst->total_dark != c->lst.total_dark) {
1343 ubifs_err("bad dead/dark space accounting");
1344 ubifs_err("calculated: total_dead %lld, total_dark %lld",
1345 lst->total_dead, lst->total_dark);
1346 ubifs_err("read from lprops: total_dead %lld, total_dark %lld",
1347 c->lst.total_dead, c->lst.total_dark);
1348 err = -EINVAL;
1349 goto out;
1350 }
1351
1352 err = dbg_check_cats(c);
1353out:
1354 return err;
1355}
1356
1357#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/lpt.c b/fs/ubifs/lpt.c
new file mode 100644
index 000000000000..9ff2463177e5
--- /dev/null
+++ b/fs/ubifs/lpt.c
@@ -0,0 +1,2243 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements the LEB properties tree (LPT) area. The LPT area
25 * contains the LEB properties tree, a table of LPT area eraseblocks (ltab), and
26 * (for the "big" model) a table of saved LEB numbers (lsave). The LPT area sits
27 * between the log and the orphan area.
28 *
29 * The LPT area is like a miniature self-contained file system. It is required
30 * that it never runs out of space, is fast to access and update, and scales
31 * logarithmically. The LEB properties tree is implemented as a wandering tree
32 * much like the TNC, and the LPT area has its own garbage collection.
33 *
34 * The LPT has two slightly different forms called the "small model" and the
35 * "big model". The small model is used when the entire LEB properties table
36 * can be written into a single eraseblock. In that case, garbage collection
37 * consists of just writing the whole table, which therefore makes all other
38 * eraseblocks reusable. In the case of the big model, dirty eraseblocks are
39 * selected for garbage collection, which consists are marking the nodes in
40 * that LEB as dirty, and then only the dirty nodes are written out. Also, in
41 * the case of the big model, a table of LEB numbers is saved so that the entire
42 * LPT does not to be scanned looking for empty eraseblocks when UBIFS is first
43 * mounted.
44 */
45
46#include <linux/crc16.h>
47#include "ubifs.h"
48
49/**
50 * do_calc_lpt_geom - calculate sizes for the LPT area.
51 * @c: the UBIFS file-system description object
52 *
53 * Calculate the sizes of LPT bit fields, nodes, and tree, based on the
54 * properties of the flash and whether LPT is "big" (c->big_lpt).
55 */
56static void do_calc_lpt_geom(struct ubifs_info *c)
57{
58 int i, n, bits, per_leb_wastage, max_pnode_cnt;
59 long long sz, tot_wastage;
60
61 n = c->main_lebs + c->max_leb_cnt - c->leb_cnt;
62 max_pnode_cnt = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
63
64 c->lpt_hght = 1;
65 n = UBIFS_LPT_FANOUT;
66 while (n < max_pnode_cnt) {
67 c->lpt_hght += 1;
68 n <<= UBIFS_LPT_FANOUT_SHIFT;
69 }
70
71 c->pnode_cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
72
73 n = DIV_ROUND_UP(c->pnode_cnt, UBIFS_LPT_FANOUT);
74 c->nnode_cnt = n;
75 for (i = 1; i < c->lpt_hght; i++) {
76 n = DIV_ROUND_UP(n, UBIFS_LPT_FANOUT);
77 c->nnode_cnt += n;
78 }
79
80 c->space_bits = fls(c->leb_size) - 3;
81 c->lpt_lnum_bits = fls(c->lpt_lebs);
82 c->lpt_offs_bits = fls(c->leb_size - 1);
83 c->lpt_spc_bits = fls(c->leb_size);
84
85 n = DIV_ROUND_UP(c->max_leb_cnt, UBIFS_LPT_FANOUT);
86 c->pcnt_bits = fls(n - 1);
87
88 c->lnum_bits = fls(c->max_leb_cnt - 1);
89
90 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
91 (c->big_lpt ? c->pcnt_bits : 0) +
92 (c->space_bits * 2 + 1) * UBIFS_LPT_FANOUT;
93 c->pnode_sz = (bits + 7) / 8;
94
95 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
96 (c->big_lpt ? c->pcnt_bits : 0) +
97 (c->lpt_lnum_bits + c->lpt_offs_bits) * UBIFS_LPT_FANOUT;
98 c->nnode_sz = (bits + 7) / 8;
99
100 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
101 c->lpt_lebs * c->lpt_spc_bits * 2;
102 c->ltab_sz = (bits + 7) / 8;
103
104 bits = UBIFS_LPT_CRC_BITS + UBIFS_LPT_TYPE_BITS +
105 c->lnum_bits * c->lsave_cnt;
106 c->lsave_sz = (bits + 7) / 8;
107
108 /* Calculate the minimum LPT size */
109 c->lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
110 c->lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
111 c->lpt_sz += c->ltab_sz;
112 c->lpt_sz += c->lsave_sz;
113
114 /* Add wastage */
115 sz = c->lpt_sz;
116 per_leb_wastage = max_t(int, c->pnode_sz, c->nnode_sz);
117 sz += per_leb_wastage;
118 tot_wastage = per_leb_wastage;
119 while (sz > c->leb_size) {
120 sz += per_leb_wastage;
121 sz -= c->leb_size;
122 tot_wastage += per_leb_wastage;
123 }
124 tot_wastage += ALIGN(sz, c->min_io_size) - sz;
125 c->lpt_sz += tot_wastage;
126}
127
128/**
129 * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
130 * @c: the UBIFS file-system description object
131 *
132 * This function returns %0 on success and a negative error code on failure.
133 */
134int ubifs_calc_lpt_geom(struct ubifs_info *c)
135{
136 int lebs_needed;
137 uint64_t sz;
138
139 do_calc_lpt_geom(c);
140
141 /* Verify that lpt_lebs is big enough */
142 sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
143 sz += c->leb_size - 1;
144 do_div(sz, c->leb_size);
145 lebs_needed = sz;
146 if (lebs_needed > c->lpt_lebs) {
147 ubifs_err("too few LPT LEBs");
148 return -EINVAL;
149 }
150
151 /* Verify that ltab fits in a single LEB (since ltab is a single node */
152 if (c->ltab_sz > c->leb_size) {
153 ubifs_err("LPT ltab too big");
154 return -EINVAL;
155 }
156
157 c->check_lpt_free = c->big_lpt;
158
159 return 0;
160}
161
162/**
163 * calc_dflt_lpt_geom - calculate default LPT geometry.
164 * @c: the UBIFS file-system description object
165 * @main_lebs: number of main area LEBs is passed and returned here
166 * @big_lpt: whether the LPT area is "big" is returned here
167 *
168 * The size of the LPT area depends on parameters that themselves are dependent
169 * on the size of the LPT area. This function, successively recalculates the LPT
170 * area geometry until the parameters and resultant geometry are consistent.
171 *
172 * This function returns %0 on success and a negative error code on failure.
173 */
174static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
175 int *big_lpt)
176{
177 int i, lebs_needed;
178 uint64_t sz;
179
180 /* Start by assuming the minimum number of LPT LEBs */
181 c->lpt_lebs = UBIFS_MIN_LPT_LEBS;
182 c->main_lebs = *main_lebs - c->lpt_lebs;
183 if (c->main_lebs <= 0)
184 return -EINVAL;
185
186 /* And assume we will use the small LPT model */
187 c->big_lpt = 0;
188
189 /*
190 * Calculate the geometry based on assumptions above and then see if it
191 * makes sense
192 */
193 do_calc_lpt_geom(c);
194
195 /* Small LPT model must have lpt_sz < leb_size */
196 if (c->lpt_sz > c->leb_size) {
197 /* Nope, so try again using big LPT model */
198 c->big_lpt = 1;
199 do_calc_lpt_geom(c);
200 }
201
202 /* Now check there are enough LPT LEBs */
203 for (i = 0; i < 64 ; i++) {
204 sz = c->lpt_sz * 4; /* Allow 4 times the size */
205 sz += c->leb_size - 1;
206 do_div(sz, c->leb_size);
207 lebs_needed = sz;
208 if (lebs_needed > c->lpt_lebs) {
209 /* Not enough LPT LEBs so try again with more */
210 c->lpt_lebs = lebs_needed;
211 c->main_lebs = *main_lebs - c->lpt_lebs;
212 if (c->main_lebs <= 0)
213 return -EINVAL;
214 do_calc_lpt_geom(c);
215 continue;
216 }
217 if (c->ltab_sz > c->leb_size) {
218 ubifs_err("LPT ltab too big");
219 return -EINVAL;
220 }
221 *main_lebs = c->main_lebs;
222 *big_lpt = c->big_lpt;
223 return 0;
224 }
225 return -EINVAL;
226}
227
228/**
229 * pack_bits - pack bit fields end-to-end.
230 * @addr: address at which to pack (passed and next address returned)
231 * @pos: bit position at which to pack (passed and next position returned)
232 * @val: value to pack
233 * @nrbits: number of bits of value to pack (1-32)
234 */
235static void pack_bits(uint8_t **addr, int *pos, uint32_t val, int nrbits)
236{
237 uint8_t *p = *addr;
238 int b = *pos;
239
240 ubifs_assert(nrbits > 0);
241 ubifs_assert(nrbits <= 32);
242 ubifs_assert(*pos >= 0);
243 ubifs_assert(*pos < 8);
244 ubifs_assert((val >> nrbits) == 0 || nrbits == 32);
245 if (b) {
246 *p |= ((uint8_t)val) << b;
247 nrbits += b;
248 if (nrbits > 8) {
249 *++p = (uint8_t)(val >>= (8 - b));
250 if (nrbits > 16) {
251 *++p = (uint8_t)(val >>= 8);
252 if (nrbits > 24) {
253 *++p = (uint8_t)(val >>= 8);
254 if (nrbits > 32)
255 *++p = (uint8_t)(val >>= 8);
256 }
257 }
258 }
259 } else {
260 *p = (uint8_t)val;
261 if (nrbits > 8) {
262 *++p = (uint8_t)(val >>= 8);
263 if (nrbits > 16) {
264 *++p = (uint8_t)(val >>= 8);
265 if (nrbits > 24)
266 *++p = (uint8_t)(val >>= 8);
267 }
268 }
269 }
270 b = nrbits & 7;
271 if (b == 0)
272 p++;
273 *addr = p;
274 *pos = b;
275}
276
277/**
278 * ubifs_unpack_bits - unpack bit fields.
279 * @addr: address at which to unpack (passed and next address returned)
280 * @pos: bit position at which to unpack (passed and next position returned)
281 * @nrbits: number of bits of value to unpack (1-32)
282 *
283 * This functions returns the value unpacked.
284 */
285uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits)
286{
287 const int k = 32 - nrbits;
288 uint8_t *p = *addr;
289 int b = *pos;
290 uint32_t val;
291
292 ubifs_assert(nrbits > 0);
293 ubifs_assert(nrbits <= 32);
294 ubifs_assert(*pos >= 0);
295 ubifs_assert(*pos < 8);
296 if (b) {
297 val = p[1] | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 16) |
298 ((uint32_t)p[4] << 24);
299 val <<= (8 - b);
300 val |= *p >> b;
301 nrbits += b;
302 } else
303 val = p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
304 ((uint32_t)p[3] << 24);
305 val <<= k;
306 val >>= k;
307 b = nrbits & 7;
308 p += nrbits / 8;
309 *addr = p;
310 *pos = b;
311 ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32);
312 return val;
313}
314
315/**
316 * ubifs_pack_pnode - pack all the bit fields of a pnode.
317 * @c: UBIFS file-system description object
318 * @buf: buffer into which to pack
319 * @pnode: pnode to pack
320 */
321void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
322 struct ubifs_pnode *pnode)
323{
324 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
325 int i, pos = 0;
326 uint16_t crc;
327
328 pack_bits(&addr, &pos, UBIFS_LPT_PNODE, UBIFS_LPT_TYPE_BITS);
329 if (c->big_lpt)
330 pack_bits(&addr, &pos, pnode->num, c->pcnt_bits);
331 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
332 pack_bits(&addr, &pos, pnode->lprops[i].free >> 3,
333 c->space_bits);
334 pack_bits(&addr, &pos, pnode->lprops[i].dirty >> 3,
335 c->space_bits);
336 if (pnode->lprops[i].flags & LPROPS_INDEX)
337 pack_bits(&addr, &pos, 1, 1);
338 else
339 pack_bits(&addr, &pos, 0, 1);
340 }
341 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
342 c->pnode_sz - UBIFS_LPT_CRC_BYTES);
343 addr = buf;
344 pos = 0;
345 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
346}
347
348/**
349 * ubifs_pack_nnode - pack all the bit fields of a nnode.
350 * @c: UBIFS file-system description object
351 * @buf: buffer into which to pack
352 * @nnode: nnode to pack
353 */
354void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
355 struct ubifs_nnode *nnode)
356{
357 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
358 int i, pos = 0;
359 uint16_t crc;
360
361 pack_bits(&addr, &pos, UBIFS_LPT_NNODE, UBIFS_LPT_TYPE_BITS);
362 if (c->big_lpt)
363 pack_bits(&addr, &pos, nnode->num, c->pcnt_bits);
364 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
365 int lnum = nnode->nbranch[i].lnum;
366
367 if (lnum == 0)
368 lnum = c->lpt_last + 1;
369 pack_bits(&addr, &pos, lnum - c->lpt_first, c->lpt_lnum_bits);
370 pack_bits(&addr, &pos, nnode->nbranch[i].offs,
371 c->lpt_offs_bits);
372 }
373 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
374 c->nnode_sz - UBIFS_LPT_CRC_BYTES);
375 addr = buf;
376 pos = 0;
377 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
378}
379
380/**
381 * ubifs_pack_ltab - pack the LPT's own lprops table.
382 * @c: UBIFS file-system description object
383 * @buf: buffer into which to pack
384 * @ltab: LPT's own lprops table to pack
385 */
386void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
387 struct ubifs_lpt_lprops *ltab)
388{
389 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
390 int i, pos = 0;
391 uint16_t crc;
392
393 pack_bits(&addr, &pos, UBIFS_LPT_LTAB, UBIFS_LPT_TYPE_BITS);
394 for (i = 0; i < c->lpt_lebs; i++) {
395 pack_bits(&addr, &pos, ltab[i].free, c->lpt_spc_bits);
396 pack_bits(&addr, &pos, ltab[i].dirty, c->lpt_spc_bits);
397 }
398 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
399 c->ltab_sz - UBIFS_LPT_CRC_BYTES);
400 addr = buf;
401 pos = 0;
402 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
403}
404
405/**
406 * ubifs_pack_lsave - pack the LPT's save table.
407 * @c: UBIFS file-system description object
408 * @buf: buffer into which to pack
409 * @lsave: LPT's save table to pack
410 */
411void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave)
412{
413 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
414 int i, pos = 0;
415 uint16_t crc;
416
417 pack_bits(&addr, &pos, UBIFS_LPT_LSAVE, UBIFS_LPT_TYPE_BITS);
418 for (i = 0; i < c->lsave_cnt; i++)
419 pack_bits(&addr, &pos, lsave[i], c->lnum_bits);
420 crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
421 c->lsave_sz - UBIFS_LPT_CRC_BYTES);
422 addr = buf;
423 pos = 0;
424 pack_bits(&addr, &pos, crc, UBIFS_LPT_CRC_BITS);
425}
426
427/**
428 * ubifs_add_lpt_dirt - add dirty space to LPT LEB properties.
429 * @c: UBIFS file-system description object
430 * @lnum: LEB number to which to add dirty space
431 * @dirty: amount of dirty space to add
432 */
433void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty)
434{
435 if (!dirty || !lnum)
436 return;
437 dbg_lp("LEB %d add %d to %d",
438 lnum, dirty, c->ltab[lnum - c->lpt_first].dirty);
439 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
440 c->ltab[lnum - c->lpt_first].dirty += dirty;
441}
442
443/**
444 * set_ltab - set LPT LEB properties.
445 * @c: UBIFS file-system description object
446 * @lnum: LEB number
447 * @free: amount of free space
448 * @dirty: amount of dirty space
449 */
450static void set_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
451{
452 dbg_lp("LEB %d free %d dirty %d to %d %d",
453 lnum, c->ltab[lnum - c->lpt_first].free,
454 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
455 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
456 c->ltab[lnum - c->lpt_first].free = free;
457 c->ltab[lnum - c->lpt_first].dirty = dirty;
458}
459
460/**
461 * ubifs_add_nnode_dirt - add dirty space to LPT LEB properties.
462 * @c: UBIFS file-system description object
463 * @nnode: nnode for which to add dirt
464 */
465void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode)
466{
467 struct ubifs_nnode *np = nnode->parent;
468
469 if (np)
470 ubifs_add_lpt_dirt(c, np->nbranch[nnode->iip].lnum,
471 c->nnode_sz);
472 else {
473 ubifs_add_lpt_dirt(c, c->lpt_lnum, c->nnode_sz);
474 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
475 c->lpt_drty_flgs |= LTAB_DIRTY;
476 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
477 }
478 }
479}
480
481/**
482 * add_pnode_dirt - add dirty space to LPT LEB properties.
483 * @c: UBIFS file-system description object
484 * @pnode: pnode for which to add dirt
485 */
486static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
487{
488 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
489 c->pnode_sz);
490}
491
492/**
493 * calc_nnode_num - calculate nnode number.
494 * @row: the row in the tree (root is zero)
495 * @col: the column in the row (leftmost is zero)
496 *
497 * The nnode number is a number that uniquely identifies a nnode and can be used
498 * easily to traverse the tree from the root to that nnode.
499 *
500 * This function calculates and returns the nnode number for the nnode at @row
501 * and @col.
502 */
503static int calc_nnode_num(int row, int col)
504{
505 int num, bits;
506
507 num = 1;
508 while (row--) {
509 bits = (col & (UBIFS_LPT_FANOUT - 1));
510 col >>= UBIFS_LPT_FANOUT_SHIFT;
511 num <<= UBIFS_LPT_FANOUT_SHIFT;
512 num |= bits;
513 }
514 return num;
515}
516
517/**
518 * calc_nnode_num_from_parent - calculate nnode number.
519 * @c: UBIFS file-system description object
520 * @parent: parent nnode
521 * @iip: index in parent
522 *
523 * The nnode number is a number that uniquely identifies a nnode and can be used
524 * easily to traverse the tree from the root to that nnode.
525 *
526 * This function calculates and returns the nnode number based on the parent's
527 * nnode number and the index in parent.
528 */
529static int calc_nnode_num_from_parent(struct ubifs_info *c,
530 struct ubifs_nnode *parent, int iip)
531{
532 int num, shft;
533
534 if (!parent)
535 return 1;
536 shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT;
537 num = parent->num ^ (1 << shft);
538 num |= (UBIFS_LPT_FANOUT + iip) << shft;
539 return num;
540}
541
542/**
543 * calc_pnode_num_from_parent - calculate pnode number.
544 * @c: UBIFS file-system description object
545 * @parent: parent nnode
546 * @iip: index in parent
547 *
548 * The pnode number is a number that uniquely identifies a pnode and can be used
549 * easily to traverse the tree from the root to that pnode.
550 *
551 * This function calculates and returns the pnode number based on the parent's
552 * nnode number and the index in parent.
553 */
554static int calc_pnode_num_from_parent(struct ubifs_info *c,
555 struct ubifs_nnode *parent, int iip)
556{
557 int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0;
558
559 for (i = 0; i < n; i++) {
560 num <<= UBIFS_LPT_FANOUT_SHIFT;
561 num |= pnum & (UBIFS_LPT_FANOUT - 1);
562 pnum >>= UBIFS_LPT_FANOUT_SHIFT;
563 }
564 num <<= UBIFS_LPT_FANOUT_SHIFT;
565 num |= iip;
566 return num;
567}
568
569/**
570 * ubifs_create_dflt_lpt - create default LPT.
571 * @c: UBIFS file-system description object
572 * @main_lebs: number of main area LEBs is passed and returned here
573 * @lpt_first: LEB number of first LPT LEB
574 * @lpt_lebs: number of LEBs for LPT is passed and returned here
575 * @big_lpt: use big LPT model is passed and returned here
576 *
577 * This function returns %0 on success and a negative error code on failure.
578 */
579int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
580 int *lpt_lebs, int *big_lpt)
581{
582 int lnum, err = 0, node_sz, iopos, i, j, cnt, len, alen, row;
583 int blnum, boffs, bsz, bcnt;
584 struct ubifs_pnode *pnode = NULL;
585 struct ubifs_nnode *nnode = NULL;
586 void *buf = NULL, *p;
587 struct ubifs_lpt_lprops *ltab = NULL;
588 int *lsave = NULL;
589
590 err = calc_dflt_lpt_geom(c, main_lebs, big_lpt);
591 if (err)
592 return err;
593 *lpt_lebs = c->lpt_lebs;
594
595 /* Needed by 'ubifs_pack_nnode()' and 'set_ltab()' */
596 c->lpt_first = lpt_first;
597 /* Needed by 'set_ltab()' */
598 c->lpt_last = lpt_first + c->lpt_lebs - 1;
599 /* Needed by 'ubifs_pack_lsave()' */
600 c->main_first = c->leb_cnt - *main_lebs;
601
602 lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_KERNEL);
603 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_KERNEL);
604 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_KERNEL);
605 buf = vmalloc(c->leb_size);
606 ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
607 if (!pnode || !nnode || !buf || !ltab || !lsave) {
608 err = -ENOMEM;
609 goto out;
610 }
611
612 ubifs_assert(!c->ltab);
613 c->ltab = ltab; /* Needed by set_ltab */
614
615 /* Initialize LPT's own lprops */
616 for (i = 0; i < c->lpt_lebs; i++) {
617 ltab[i].free = c->leb_size;
618 ltab[i].dirty = 0;
619 ltab[i].tgc = 0;
620 ltab[i].cmt = 0;
621 }
622
623 lnum = lpt_first;
624 p = buf;
625 /* Number of leaf nodes (pnodes) */
626 cnt = c->pnode_cnt;
627
628 /*
629 * The first pnode contains the LEB properties for the LEBs that contain
630 * the root inode node and the root index node of the index tree.
631 */
632 node_sz = ALIGN(ubifs_idx_node_sz(c, 1), 8);
633 iopos = ALIGN(node_sz, c->min_io_size);
634 pnode->lprops[0].free = c->leb_size - iopos;
635 pnode->lprops[0].dirty = iopos - node_sz;
636 pnode->lprops[0].flags = LPROPS_INDEX;
637
638 node_sz = UBIFS_INO_NODE_SZ;
639 iopos = ALIGN(node_sz, c->min_io_size);
640 pnode->lprops[1].free = c->leb_size - iopos;
641 pnode->lprops[1].dirty = iopos - node_sz;
642
643 for (i = 2; i < UBIFS_LPT_FANOUT; i++)
644 pnode->lprops[i].free = c->leb_size;
645
646 /* Add first pnode */
647 ubifs_pack_pnode(c, p, pnode);
648 p += c->pnode_sz;
649 len = c->pnode_sz;
650 pnode->num += 1;
651
652 /* Reset pnode values for remaining pnodes */
653 pnode->lprops[0].free = c->leb_size;
654 pnode->lprops[0].dirty = 0;
655 pnode->lprops[0].flags = 0;
656
657 pnode->lprops[1].free = c->leb_size;
658 pnode->lprops[1].dirty = 0;
659
660 /*
661 * To calculate the internal node branches, we keep information about
662 * the level below.
663 */
664 blnum = lnum; /* LEB number of level below */
665 boffs = 0; /* Offset of level below */
666 bcnt = cnt; /* Number of nodes in level below */
667 bsz = c->pnode_sz; /* Size of nodes in level below */
668
669 /* Add all remaining pnodes */
670 for (i = 1; i < cnt; i++) {
671 if (len + c->pnode_sz > c->leb_size) {
672 alen = ALIGN(len, c->min_io_size);
673 set_ltab(c, lnum, c->leb_size - alen, alen - len);
674 memset(p, 0xff, alen - len);
675 err = ubi_leb_change(c->ubi, lnum++, buf, alen,
676 UBI_SHORTTERM);
677 if (err)
678 goto out;
679 p = buf;
680 len = 0;
681 }
682 ubifs_pack_pnode(c, p, pnode);
683 p += c->pnode_sz;
684 len += c->pnode_sz;
685 /*
686 * pnodes are simply numbered left to right starting at zero,
687 * which means the pnode number can be used easily to traverse
688 * down the tree to the corresponding pnode.
689 */
690 pnode->num += 1;
691 }
692
693 row = 0;
694 for (i = UBIFS_LPT_FANOUT; cnt > i; i <<= UBIFS_LPT_FANOUT_SHIFT)
695 row += 1;
696 /* Add all nnodes, one level at a time */
697 while (1) {
698 /* Number of internal nodes (nnodes) at next level */
699 cnt = DIV_ROUND_UP(cnt, UBIFS_LPT_FANOUT);
700 for (i = 0; i < cnt; i++) {
701 if (len + c->nnode_sz > c->leb_size) {
702 alen = ALIGN(len, c->min_io_size);
703 set_ltab(c, lnum, c->leb_size - alen,
704 alen - len);
705 memset(p, 0xff, alen - len);
706 err = ubi_leb_change(c->ubi, lnum++, buf, alen,
707 UBI_SHORTTERM);
708 if (err)
709 goto out;
710 p = buf;
711 len = 0;
712 }
713 /* Only 1 nnode at this level, so it is the root */
714 if (cnt == 1) {
715 c->lpt_lnum = lnum;
716 c->lpt_offs = len;
717 }
718 /* Set branches to the level below */
719 for (j = 0; j < UBIFS_LPT_FANOUT; j++) {
720 if (bcnt) {
721 if (boffs + bsz > c->leb_size) {
722 blnum += 1;
723 boffs = 0;
724 }
725 nnode->nbranch[j].lnum = blnum;
726 nnode->nbranch[j].offs = boffs;
727 boffs += bsz;
728 bcnt--;
729 } else {
730 nnode->nbranch[j].lnum = 0;
731 nnode->nbranch[j].offs = 0;
732 }
733 }
734 nnode->num = calc_nnode_num(row, i);
735 ubifs_pack_nnode(c, p, nnode);
736 p += c->nnode_sz;
737 len += c->nnode_sz;
738 }
739 /* Only 1 nnode at this level, so it is the root */
740 if (cnt == 1)
741 break;
742 /* Update the information about the level below */
743 bcnt = cnt;
744 bsz = c->nnode_sz;
745 row -= 1;
746 }
747
748 if (*big_lpt) {
749 /* Need to add LPT's save table */
750 if (len + c->lsave_sz > c->leb_size) {
751 alen = ALIGN(len, c->min_io_size);
752 set_ltab(c, lnum, c->leb_size - alen, alen - len);
753 memset(p, 0xff, alen - len);
754 err = ubi_leb_change(c->ubi, lnum++, buf, alen,
755 UBI_SHORTTERM);
756 if (err)
757 goto out;
758 p = buf;
759 len = 0;
760 }
761
762 c->lsave_lnum = lnum;
763 c->lsave_offs = len;
764
765 for (i = 0; i < c->lsave_cnt && i < *main_lebs; i++)
766 lsave[i] = c->main_first + i;
767 for (; i < c->lsave_cnt; i++)
768 lsave[i] = c->main_first;
769
770 ubifs_pack_lsave(c, p, lsave);
771 p += c->lsave_sz;
772 len += c->lsave_sz;
773 }
774
775 /* Need to add LPT's own LEB properties table */
776 if (len + c->ltab_sz > c->leb_size) {
777 alen = ALIGN(len, c->min_io_size);
778 set_ltab(c, lnum, c->leb_size - alen, alen - len);
779 memset(p, 0xff, alen - len);
780 err = ubi_leb_change(c->ubi, lnum++, buf, alen, UBI_SHORTTERM);
781 if (err)
782 goto out;
783 p = buf;
784 len = 0;
785 }
786
787 c->ltab_lnum = lnum;
788 c->ltab_offs = len;
789
790 /* Update ltab before packing it */
791 len += c->ltab_sz;
792 alen = ALIGN(len, c->min_io_size);
793 set_ltab(c, lnum, c->leb_size - alen, alen - len);
794
795 ubifs_pack_ltab(c, p, ltab);
796 p += c->ltab_sz;
797
798 /* Write remaining buffer */
799 memset(p, 0xff, alen - len);
800 err = ubi_leb_change(c->ubi, lnum, buf, alen, UBI_SHORTTERM);
801 if (err)
802 goto out;
803
804 c->nhead_lnum = lnum;
805 c->nhead_offs = ALIGN(len, c->min_io_size);
806
807 dbg_lp("space_bits %d", c->space_bits);
808 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
809 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
810 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
811 dbg_lp("pcnt_bits %d", c->pcnt_bits);
812 dbg_lp("lnum_bits %d", c->lnum_bits);
813 dbg_lp("pnode_sz %d", c->pnode_sz);
814 dbg_lp("nnode_sz %d", c->nnode_sz);
815 dbg_lp("ltab_sz %d", c->ltab_sz);
816 dbg_lp("lsave_sz %d", c->lsave_sz);
817 dbg_lp("lsave_cnt %d", c->lsave_cnt);
818 dbg_lp("lpt_hght %d", c->lpt_hght);
819 dbg_lp("big_lpt %d", c->big_lpt);
820 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
821 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
822 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
823 if (c->big_lpt)
824 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
825out:
826 c->ltab = NULL;
827 kfree(lsave);
828 vfree(ltab);
829 vfree(buf);
830 kfree(nnode);
831 kfree(pnode);
832 return err;
833}
834
835/**
836 * update_cats - add LEB properties of a pnode to LEB category lists and heaps.
837 * @c: UBIFS file-system description object
838 * @pnode: pnode
839 *
840 * When a pnode is loaded into memory, the LEB properties it contains are added,
841 * by this function, to the LEB category lists and heaps.
842 */
843static void update_cats(struct ubifs_info *c, struct ubifs_pnode *pnode)
844{
845 int i;
846
847 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
848 int cat = pnode->lprops[i].flags & LPROPS_CAT_MASK;
849 int lnum = pnode->lprops[i].lnum;
850
851 if (!lnum)
852 return;
853 ubifs_add_to_cat(c, &pnode->lprops[i], cat);
854 }
855}
856
857/**
858 * replace_cats - add LEB properties of a pnode to LEB category lists and heaps.
859 * @c: UBIFS file-system description object
860 * @old_pnode: pnode copied
861 * @new_pnode: pnode copy
862 *
863 * During commit it is sometimes necessary to copy a pnode
864 * (see dirty_cow_pnode). When that happens, references in
865 * category lists and heaps must be replaced. This function does that.
866 */
867static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
868 struct ubifs_pnode *new_pnode)
869{
870 int i;
871
872 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
873 if (!new_pnode->lprops[i].lnum)
874 return;
875 ubifs_replace_cat(c, &old_pnode->lprops[i],
876 &new_pnode->lprops[i]);
877 }
878}
879
880/**
881 * check_lpt_crc - check LPT node crc is correct.
882 * @c: UBIFS file-system description object
883 * @buf: buffer containing node
884 * @len: length of node
885 *
886 * This function returns %0 on success and a negative error code on failure.
887 */
888static int check_lpt_crc(void *buf, int len)
889{
890 int pos = 0;
891 uint8_t *addr = buf;
892 uint16_t crc, calc_crc;
893
894 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
895 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
896 len - UBIFS_LPT_CRC_BYTES);
897 if (crc != calc_crc) {
898 ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc,
899 calc_crc);
900 dbg_dump_stack();
901 return -EINVAL;
902 }
903 return 0;
904}
905
906/**
907 * check_lpt_type - check LPT node type is correct.
908 * @c: UBIFS file-system description object
909 * @addr: address of type bit field is passed and returned updated here
910 * @pos: position of type bit field is passed and returned updated here
911 * @type: expected type
912 *
913 * This function returns %0 on success and a negative error code on failure.
914 */
915static int check_lpt_type(uint8_t **addr, int *pos, int type)
916{
917 int node_type;
918
919 node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
920 if (node_type != type) {
921 ubifs_err("invalid type (%d) in LPT node type %d", node_type,
922 type);
923 dbg_dump_stack();
924 return -EINVAL;
925 }
926 return 0;
927}
928
929/**
930 * unpack_pnode - unpack a pnode.
931 * @c: UBIFS file-system description object
932 * @buf: buffer containing packed pnode to unpack
933 * @pnode: pnode structure to fill
934 *
935 * This function returns %0 on success and a negative error code on failure.
936 */
937static int unpack_pnode(struct ubifs_info *c, void *buf,
938 struct ubifs_pnode *pnode)
939{
940 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
941 int i, pos = 0, err;
942
943 err = check_lpt_type(&addr, &pos, UBIFS_LPT_PNODE);
944 if (err)
945 return err;
946 if (c->big_lpt)
947 pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
948 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
949 struct ubifs_lprops * const lprops = &pnode->lprops[i];
950
951 lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
952 lprops->free <<= 3;
953 lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
954 lprops->dirty <<= 3;
955
956 if (ubifs_unpack_bits(&addr, &pos, 1))
957 lprops->flags = LPROPS_INDEX;
958 else
959 lprops->flags = 0;
960 lprops->flags |= ubifs_categorize_lprops(c, lprops);
961 }
962 err = check_lpt_crc(buf, c->pnode_sz);
963 return err;
964}
965
966/**
967 * unpack_nnode - unpack a nnode.
968 * @c: UBIFS file-system description object
969 * @buf: buffer containing packed nnode to unpack
970 * @nnode: nnode structure to fill
971 *
972 * This function returns %0 on success and a negative error code on failure.
973 */
974static int unpack_nnode(struct ubifs_info *c, void *buf,
975 struct ubifs_nnode *nnode)
976{
977 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
978 int i, pos = 0, err;
979
980 err = check_lpt_type(&addr, &pos, UBIFS_LPT_NNODE);
981 if (err)
982 return err;
983 if (c->big_lpt)
984 nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
985 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
986 int lnum;
987
988 lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
989 c->lpt_first;
990 if (lnum == c->lpt_last + 1)
991 lnum = 0;
992 nnode->nbranch[i].lnum = lnum;
993 nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
994 c->lpt_offs_bits);
995 }
996 err = check_lpt_crc(buf, c->nnode_sz);
997 return err;
998}
999
1000/**
1001 * unpack_ltab - unpack the LPT's own lprops table.
1002 * @c: UBIFS file-system description object
1003 * @buf: buffer from which to unpack
1004 *
1005 * This function returns %0 on success and a negative error code on failure.
1006 */
1007static int unpack_ltab(struct ubifs_info *c, void *buf)
1008{
1009 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1010 int i, pos = 0, err;
1011
1012 err = check_lpt_type(&addr, &pos, UBIFS_LPT_LTAB);
1013 if (err)
1014 return err;
1015 for (i = 0; i < c->lpt_lebs; i++) {
1016 int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1017 int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
1018
1019 if (free < 0 || free > c->leb_size || dirty < 0 ||
1020 dirty > c->leb_size || free + dirty > c->leb_size)
1021 return -EINVAL;
1022
1023 c->ltab[i].free = free;
1024 c->ltab[i].dirty = dirty;
1025 c->ltab[i].tgc = 0;
1026 c->ltab[i].cmt = 0;
1027 }
1028 err = check_lpt_crc(buf, c->ltab_sz);
1029 return err;
1030}
1031
1032/**
1033 * unpack_lsave - unpack the LPT's save table.
1034 * @c: UBIFS file-system description object
1035 * @buf: buffer from which to unpack
1036 *
1037 * This function returns %0 on success and a negative error code on failure.
1038 */
1039static int unpack_lsave(struct ubifs_info *c, void *buf)
1040{
1041 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1042 int i, pos = 0, err;
1043
1044 err = check_lpt_type(&addr, &pos, UBIFS_LPT_LSAVE);
1045 if (err)
1046 return err;
1047 for (i = 0; i < c->lsave_cnt; i++) {
1048 int lnum = ubifs_unpack_bits(&addr, &pos, c->lnum_bits);
1049
1050 if (lnum < c->main_first || lnum >= c->leb_cnt)
1051 return -EINVAL;
1052 c->lsave[i] = lnum;
1053 }
1054 err = check_lpt_crc(buf, c->lsave_sz);
1055 return err;
1056}
1057
1058/**
1059 * validate_nnode - validate a nnode.
1060 * @c: UBIFS file-system description object
1061 * @nnode: nnode to validate
1062 * @parent: parent nnode (or NULL for the root nnode)
1063 * @iip: index in parent
1064 *
1065 * This function returns %0 on success and a negative error code on failure.
1066 */
1067static int validate_nnode(struct ubifs_info *c, struct ubifs_nnode *nnode,
1068 struct ubifs_nnode *parent, int iip)
1069{
1070 int i, lvl, max_offs;
1071
1072 if (c->big_lpt) {
1073 int num = calc_nnode_num_from_parent(c, parent, iip);
1074
1075 if (nnode->num != num)
1076 return -EINVAL;
1077 }
1078 lvl = parent ? parent->level - 1 : c->lpt_hght;
1079 if (lvl < 1)
1080 return -EINVAL;
1081 if (lvl == 1)
1082 max_offs = c->leb_size - c->pnode_sz;
1083 else
1084 max_offs = c->leb_size - c->nnode_sz;
1085 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1086 int lnum = nnode->nbranch[i].lnum;
1087 int offs = nnode->nbranch[i].offs;
1088
1089 if (lnum == 0) {
1090 if (offs != 0)
1091 return -EINVAL;
1092 continue;
1093 }
1094 if (lnum < c->lpt_first || lnum > c->lpt_last)
1095 return -EINVAL;
1096 if (offs < 0 || offs > max_offs)
1097 return -EINVAL;
1098 }
1099 return 0;
1100}
1101
1102/**
1103 * validate_pnode - validate a pnode.
1104 * @c: UBIFS file-system description object
1105 * @pnode: pnode to validate
1106 * @parent: parent nnode
1107 * @iip: index in parent
1108 *
1109 * This function returns %0 on success and a negative error code on failure.
1110 */
1111static int validate_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
1112 struct ubifs_nnode *parent, int iip)
1113{
1114 int i;
1115
1116 if (c->big_lpt) {
1117 int num = calc_pnode_num_from_parent(c, parent, iip);
1118
1119 if (pnode->num != num)
1120 return -EINVAL;
1121 }
1122 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1123 int free = pnode->lprops[i].free;
1124 int dirty = pnode->lprops[i].dirty;
1125
1126 if (free < 0 || free > c->leb_size || free % c->min_io_size ||
1127 (free & 7))
1128 return -EINVAL;
1129 if (dirty < 0 || dirty > c->leb_size || (dirty & 7))
1130 return -EINVAL;
1131 if (dirty + free > c->leb_size)
1132 return -EINVAL;
1133 }
1134 return 0;
1135}
1136
1137/**
1138 * set_pnode_lnum - set LEB numbers on a pnode.
1139 * @c: UBIFS file-system description object
1140 * @pnode: pnode to update
1141 *
1142 * This function calculates the LEB numbers for the LEB properties it contains
1143 * based on the pnode number.
1144 */
1145static void set_pnode_lnum(struct ubifs_info *c, struct ubifs_pnode *pnode)
1146{
1147 int i, lnum;
1148
1149 lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
1150 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1151 if (lnum >= c->leb_cnt)
1152 return;
1153 pnode->lprops[i].lnum = lnum++;
1154 }
1155}
1156
1157/**
1158 * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
1159 * @c: UBIFS file-system description object
1160 * @parent: parent nnode (or NULL for the root)
1161 * @iip: index in parent
1162 *
1163 * This function returns %0 on success and a negative error code on failure.
1164 */
1165int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1166{
1167 struct ubifs_nbranch *branch = NULL;
1168 struct ubifs_nnode *nnode = NULL;
1169 void *buf = c->lpt_nod_buf;
1170 int err, lnum, offs;
1171
1172 if (parent) {
1173 branch = &parent->nbranch[iip];
1174 lnum = branch->lnum;
1175 offs = branch->offs;
1176 } else {
1177 lnum = c->lpt_lnum;
1178 offs = c->lpt_offs;
1179 }
1180 nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1181 if (!nnode) {
1182 err = -ENOMEM;
1183 goto out;
1184 }
1185 if (lnum == 0) {
1186 /*
1187 * This nnode was not written which just means that the LEB
1188 * properties in the subtree below it describe empty LEBs. We
1189 * make the nnode as though we had read it, which in fact means
1190 * doing almost nothing.
1191 */
1192 if (c->big_lpt)
1193 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1194 } else {
1195 err = ubi_read(c->ubi, lnum, buf, offs, c->nnode_sz);
1196 if (err)
1197 goto out;
1198 err = unpack_nnode(c, buf, nnode);
1199 if (err)
1200 goto out;
1201 }
1202 err = validate_nnode(c, nnode, parent, iip);
1203 if (err)
1204 goto out;
1205 if (!c->big_lpt)
1206 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1207 if (parent) {
1208 branch->nnode = nnode;
1209 nnode->level = parent->level - 1;
1210 } else {
1211 c->nroot = nnode;
1212 nnode->level = c->lpt_hght;
1213 }
1214 nnode->parent = parent;
1215 nnode->iip = iip;
1216 return 0;
1217
1218out:
1219 ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs);
1220 kfree(nnode);
1221 return err;
1222}
1223
1224/**
1225 * read_pnode - read a pnode from flash and link it to the tree in memory.
1226 * @c: UBIFS file-system description object
1227 * @parent: parent nnode
1228 * @iip: index in parent
1229 *
1230 * This function returns %0 on success and a negative error code on failure.
1231 */
1232static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
1233{
1234 struct ubifs_nbranch *branch;
1235 struct ubifs_pnode *pnode = NULL;
1236 void *buf = c->lpt_nod_buf;
1237 int err, lnum, offs;
1238
1239 branch = &parent->nbranch[iip];
1240 lnum = branch->lnum;
1241 offs = branch->offs;
1242 pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1243 if (!pnode) {
1244 err = -ENOMEM;
1245 goto out;
1246 }
1247 if (lnum == 0) {
1248 /*
1249 * This pnode was not written which just means that the LEB
1250 * properties in it describe empty LEBs. We make the pnode as
1251 * though we had read it.
1252 */
1253 int i;
1254
1255 if (c->big_lpt)
1256 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1257 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1258 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1259
1260 lprops->free = c->leb_size;
1261 lprops->flags = ubifs_categorize_lprops(c, lprops);
1262 }
1263 } else {
1264 err = ubi_read(c->ubi, lnum, buf, offs, c->pnode_sz);
1265 if (err)
1266 goto out;
1267 err = unpack_pnode(c, buf, pnode);
1268 if (err)
1269 goto out;
1270 }
1271 err = validate_pnode(c, pnode, parent, iip);
1272 if (err)
1273 goto out;
1274 if (!c->big_lpt)
1275 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1276 branch->pnode = pnode;
1277 pnode->parent = parent;
1278 pnode->iip = iip;
1279 set_pnode_lnum(c, pnode);
1280 c->pnodes_have += 1;
1281 return 0;
1282
1283out:
1284 ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
1285 dbg_dump_pnode(c, pnode, parent, iip);
1286 dbg_msg("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
1287 kfree(pnode);
1288 return err;
1289}
1290
1291/**
1292 * read_ltab - read LPT's own lprops table.
1293 * @c: UBIFS file-system description object
1294 *
1295 * This function returns %0 on success and a negative error code on failure.
1296 */
1297static int read_ltab(struct ubifs_info *c)
1298{
1299 int err;
1300 void *buf;
1301
1302 buf = vmalloc(c->ltab_sz);
1303 if (!buf)
1304 return -ENOMEM;
1305 err = ubi_read(c->ubi, c->ltab_lnum, buf, c->ltab_offs, c->ltab_sz);
1306 if (err)
1307 goto out;
1308 err = unpack_ltab(c, buf);
1309out:
1310 vfree(buf);
1311 return err;
1312}
1313
1314/**
1315 * read_lsave - read LPT's save table.
1316 * @c: UBIFS file-system description object
1317 *
1318 * This function returns %0 on success and a negative error code on failure.
1319 */
1320static int read_lsave(struct ubifs_info *c)
1321{
1322 int err, i;
1323 void *buf;
1324
1325 buf = vmalloc(c->lsave_sz);
1326 if (!buf)
1327 return -ENOMEM;
1328 err = ubi_read(c->ubi, c->lsave_lnum, buf, c->lsave_offs, c->lsave_sz);
1329 if (err)
1330 goto out;
1331 err = unpack_lsave(c, buf);
1332 if (err)
1333 goto out;
1334 for (i = 0; i < c->lsave_cnt; i++) {
1335 int lnum = c->lsave[i];
1336
1337 /*
1338 * Due to automatic resizing, the values in the lsave table
1339 * could be beyond the volume size - just ignore them.
1340 */
1341 if (lnum >= c->leb_cnt)
1342 continue;
1343 ubifs_lpt_lookup(c, lnum);
1344 }
1345out:
1346 vfree(buf);
1347 return err;
1348}
1349
1350/**
1351 * ubifs_get_nnode - get a nnode.
1352 * @c: UBIFS file-system description object
1353 * @parent: parent nnode (or NULL for the root)
1354 * @iip: index in parent
1355 *
1356 * This function returns a pointer to the nnode on success or a negative error
1357 * code on failure.
1358 */
1359struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1360 struct ubifs_nnode *parent, int iip)
1361{
1362 struct ubifs_nbranch *branch;
1363 struct ubifs_nnode *nnode;
1364 int err;
1365
1366 branch = &parent->nbranch[iip];
1367 nnode = branch->nnode;
1368 if (nnode)
1369 return nnode;
1370 err = ubifs_read_nnode(c, parent, iip);
1371 if (err)
1372 return ERR_PTR(err);
1373 return branch->nnode;
1374}
1375
1376/**
1377 * ubifs_get_pnode - get a pnode.
1378 * @c: UBIFS file-system description object
1379 * @parent: parent nnode
1380 * @iip: index in parent
1381 *
1382 * This function returns a pointer to the pnode on success or a negative error
1383 * code on failure.
1384 */
1385struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1386 struct ubifs_nnode *parent, int iip)
1387{
1388 struct ubifs_nbranch *branch;
1389 struct ubifs_pnode *pnode;
1390 int err;
1391
1392 branch = &parent->nbranch[iip];
1393 pnode = branch->pnode;
1394 if (pnode)
1395 return pnode;
1396 err = read_pnode(c, parent, iip);
1397 if (err)
1398 return ERR_PTR(err);
1399 update_cats(c, branch->pnode);
1400 return branch->pnode;
1401}
1402
1403/**
1404 * ubifs_lpt_lookup - lookup LEB properties in the LPT.
1405 * @c: UBIFS file-system description object
1406 * @lnum: LEB number to lookup
1407 *
1408 * This function returns a pointer to the LEB properties on success or a
1409 * negative error code on failure.
1410 */
1411struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
1412{
1413 int err, i, h, iip, shft;
1414 struct ubifs_nnode *nnode;
1415 struct ubifs_pnode *pnode;
1416
1417 if (!c->nroot) {
1418 err = ubifs_read_nnode(c, NULL, 0);
1419 if (err)
1420 return ERR_PTR(err);
1421 }
1422 nnode = c->nroot;
1423 i = lnum - c->main_first;
1424 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1425 for (h = 1; h < c->lpt_hght; h++) {
1426 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1427 shft -= UBIFS_LPT_FANOUT_SHIFT;
1428 nnode = ubifs_get_nnode(c, nnode, iip);
1429 if (IS_ERR(nnode))
1430 return ERR_PTR(PTR_ERR(nnode));
1431 }
1432 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1433 shft -= UBIFS_LPT_FANOUT_SHIFT;
1434 pnode = ubifs_get_pnode(c, nnode, iip);
1435 if (IS_ERR(pnode))
1436 return ERR_PTR(PTR_ERR(pnode));
1437 iip = (i & (UBIFS_LPT_FANOUT - 1));
1438 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1439 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1440 pnode->lprops[iip].flags);
1441 return &pnode->lprops[iip];
1442}
1443
1444/**
1445 * dirty_cow_nnode - ensure a nnode is not being committed.
1446 * @c: UBIFS file-system description object
1447 * @nnode: nnode to check
1448 *
1449 * Returns dirtied nnode on success or negative error code on failure.
1450 */
1451static struct ubifs_nnode *dirty_cow_nnode(struct ubifs_info *c,
1452 struct ubifs_nnode *nnode)
1453{
1454 struct ubifs_nnode *n;
1455 int i;
1456
1457 if (!test_bit(COW_CNODE, &nnode->flags)) {
1458 /* nnode is not being committed */
1459 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
1460 c->dirty_nn_cnt += 1;
1461 ubifs_add_nnode_dirt(c, nnode);
1462 }
1463 return nnode;
1464 }
1465
1466 /* nnode is being committed, so copy it */
1467 n = kmalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
1468 if (unlikely(!n))
1469 return ERR_PTR(-ENOMEM);
1470
1471 memcpy(n, nnode, sizeof(struct ubifs_nnode));
1472 n->cnext = NULL;
1473 __set_bit(DIRTY_CNODE, &n->flags);
1474 __clear_bit(COW_CNODE, &n->flags);
1475
1476 /* The children now have new parent */
1477 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1478 struct ubifs_nbranch *branch = &n->nbranch[i];
1479
1480 if (branch->cnode)
1481 branch->cnode->parent = n;
1482 }
1483
1484 ubifs_assert(!test_bit(OBSOLETE_CNODE, &nnode->flags));
1485 __set_bit(OBSOLETE_CNODE, &nnode->flags);
1486
1487 c->dirty_nn_cnt += 1;
1488 ubifs_add_nnode_dirt(c, nnode);
1489 if (nnode->parent)
1490 nnode->parent->nbranch[n->iip].nnode = n;
1491 else
1492 c->nroot = n;
1493 return n;
1494}
1495
1496/**
1497 * dirty_cow_pnode - ensure a pnode is not being committed.
1498 * @c: UBIFS file-system description object
1499 * @pnode: pnode to check
1500 *
1501 * Returns dirtied pnode on success or negative error code on failure.
1502 */
1503static struct ubifs_pnode *dirty_cow_pnode(struct ubifs_info *c,
1504 struct ubifs_pnode *pnode)
1505{
1506 struct ubifs_pnode *p;
1507
1508 if (!test_bit(COW_CNODE, &pnode->flags)) {
1509 /* pnode is not being committed */
1510 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
1511 c->dirty_pn_cnt += 1;
1512 add_pnode_dirt(c, pnode);
1513 }
1514 return pnode;
1515 }
1516
1517 /* pnode is being committed, so copy it */
1518 p = kmalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
1519 if (unlikely(!p))
1520 return ERR_PTR(-ENOMEM);
1521
1522 memcpy(p, pnode, sizeof(struct ubifs_pnode));
1523 p->cnext = NULL;
1524 __set_bit(DIRTY_CNODE, &p->flags);
1525 __clear_bit(COW_CNODE, &p->flags);
1526 replace_cats(c, pnode, p);
1527
1528 ubifs_assert(!test_bit(OBSOLETE_CNODE, &pnode->flags));
1529 __set_bit(OBSOLETE_CNODE, &pnode->flags);
1530
1531 c->dirty_pn_cnt += 1;
1532 add_pnode_dirt(c, pnode);
1533 pnode->parent->nbranch[p->iip].pnode = p;
1534 return p;
1535}
1536
1537/**
1538 * ubifs_lpt_lookup_dirty - lookup LEB properties in the LPT.
1539 * @c: UBIFS file-system description object
1540 * @lnum: LEB number to lookup
1541 *
1542 * This function returns a pointer to the LEB properties on success or a
1543 * negative error code on failure.
1544 */
1545struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
1546{
1547 int err, i, h, iip, shft;
1548 struct ubifs_nnode *nnode;
1549 struct ubifs_pnode *pnode;
1550
1551 if (!c->nroot) {
1552 err = ubifs_read_nnode(c, NULL, 0);
1553 if (err)
1554 return ERR_PTR(err);
1555 }
1556 nnode = c->nroot;
1557 nnode = dirty_cow_nnode(c, nnode);
1558 if (IS_ERR(nnode))
1559 return ERR_PTR(PTR_ERR(nnode));
1560 i = lnum - c->main_first;
1561 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1562 for (h = 1; h < c->lpt_hght; h++) {
1563 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1564 shft -= UBIFS_LPT_FANOUT_SHIFT;
1565 nnode = ubifs_get_nnode(c, nnode, iip);
1566 if (IS_ERR(nnode))
1567 return ERR_PTR(PTR_ERR(nnode));
1568 nnode = dirty_cow_nnode(c, nnode);
1569 if (IS_ERR(nnode))
1570 return ERR_PTR(PTR_ERR(nnode));
1571 }
1572 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1573 shft -= UBIFS_LPT_FANOUT_SHIFT;
1574 pnode = ubifs_get_pnode(c, nnode, iip);
1575 if (IS_ERR(pnode))
1576 return ERR_PTR(PTR_ERR(pnode));
1577 pnode = dirty_cow_pnode(c, pnode);
1578 if (IS_ERR(pnode))
1579 return ERR_PTR(PTR_ERR(pnode));
1580 iip = (i & (UBIFS_LPT_FANOUT - 1));
1581 dbg_lp("LEB %d, free %d, dirty %d, flags %d", lnum,
1582 pnode->lprops[iip].free, pnode->lprops[iip].dirty,
1583 pnode->lprops[iip].flags);
1584 ubifs_assert(test_bit(DIRTY_CNODE, &pnode->flags));
1585 return &pnode->lprops[iip];
1586}
1587
1588/**
1589 * lpt_init_rd - initialize the LPT for reading.
1590 * @c: UBIFS file-system description object
1591 *
1592 * This function returns %0 on success and a negative error code on failure.
1593 */
1594static int lpt_init_rd(struct ubifs_info *c)
1595{
1596 int err, i;
1597
1598 c->ltab = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1599 if (!c->ltab)
1600 return -ENOMEM;
1601
1602 i = max_t(int, c->nnode_sz, c->pnode_sz);
1603 c->lpt_nod_buf = kmalloc(i, GFP_KERNEL);
1604 if (!c->lpt_nod_buf)
1605 return -ENOMEM;
1606
1607 for (i = 0; i < LPROPS_HEAP_CNT; i++) {
1608 c->lpt_heap[i].arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ,
1609 GFP_KERNEL);
1610 if (!c->lpt_heap[i].arr)
1611 return -ENOMEM;
1612 c->lpt_heap[i].cnt = 0;
1613 c->lpt_heap[i].max_cnt = LPT_HEAP_SZ;
1614 }
1615
1616 c->dirty_idx.arr = kmalloc(sizeof(void *) * LPT_HEAP_SZ, GFP_KERNEL);
1617 if (!c->dirty_idx.arr)
1618 return -ENOMEM;
1619 c->dirty_idx.cnt = 0;
1620 c->dirty_idx.max_cnt = LPT_HEAP_SZ;
1621
1622 err = read_ltab(c);
1623 if (err)
1624 return err;
1625
1626 dbg_lp("space_bits %d", c->space_bits);
1627 dbg_lp("lpt_lnum_bits %d", c->lpt_lnum_bits);
1628 dbg_lp("lpt_offs_bits %d", c->lpt_offs_bits);
1629 dbg_lp("lpt_spc_bits %d", c->lpt_spc_bits);
1630 dbg_lp("pcnt_bits %d", c->pcnt_bits);
1631 dbg_lp("lnum_bits %d", c->lnum_bits);
1632 dbg_lp("pnode_sz %d", c->pnode_sz);
1633 dbg_lp("nnode_sz %d", c->nnode_sz);
1634 dbg_lp("ltab_sz %d", c->ltab_sz);
1635 dbg_lp("lsave_sz %d", c->lsave_sz);
1636 dbg_lp("lsave_cnt %d", c->lsave_cnt);
1637 dbg_lp("lpt_hght %d", c->lpt_hght);
1638 dbg_lp("big_lpt %d", c->big_lpt);
1639 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
1640 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
1641 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
1642 if (c->big_lpt)
1643 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
1644
1645 return 0;
1646}
1647
1648/**
1649 * lpt_init_wr - initialize the LPT for writing.
1650 * @c: UBIFS file-system description object
1651 *
1652 * 'lpt_init_rd()' must have been called already.
1653 *
1654 * This function returns %0 on success and a negative error code on failure.
1655 */
1656static int lpt_init_wr(struct ubifs_info *c)
1657{
1658 int err, i;
1659
1660 c->ltab_cmt = vmalloc(sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1661 if (!c->ltab_cmt)
1662 return -ENOMEM;
1663
1664 c->lpt_buf = vmalloc(c->leb_size);
1665 if (!c->lpt_buf)
1666 return -ENOMEM;
1667
1668 if (c->big_lpt) {
1669 c->lsave = kmalloc(sizeof(int) * c->lsave_cnt, GFP_NOFS);
1670 if (!c->lsave)
1671 return -ENOMEM;
1672 err = read_lsave(c);
1673 if (err)
1674 return err;
1675 }
1676
1677 for (i = 0; i < c->lpt_lebs; i++)
1678 if (c->ltab[i].free == c->leb_size) {
1679 err = ubifs_leb_unmap(c, i + c->lpt_first);
1680 if (err)
1681 return err;
1682 }
1683
1684 return 0;
1685}
1686
1687/**
1688 * ubifs_lpt_init - initialize the LPT.
1689 * @c: UBIFS file-system description object
1690 * @rd: whether to initialize lpt for reading
1691 * @wr: whether to initialize lpt for writing
1692 *
1693 * For mounting 'rw', @rd and @wr are both true. For mounting 'ro', @rd is true
1694 * and @wr is false. For mounting from 'ro' to 'rw', @rd is false and @wr is
1695 * true.
1696 *
1697 * This function returns %0 on success and a negative error code on failure.
1698 */
1699int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr)
1700{
1701 int err;
1702
1703 if (rd) {
1704 err = lpt_init_rd(c);
1705 if (err)
1706 return err;
1707 }
1708
1709 if (wr) {
1710 err = lpt_init_wr(c);
1711 if (err)
1712 return err;
1713 }
1714
1715 return 0;
1716}
1717
1718/**
1719 * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
1720 * @nnode: where to keep a nnode
1721 * @pnode: where to keep a pnode
1722 * @cnode: where to keep a cnode
1723 * @in_tree: is the node in the tree in memory
1724 * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
1725 * the tree
1726 * @ptr.pnode: ditto for pnode
1727 * @ptr.cnode: ditto for cnode
1728 */
1729struct lpt_scan_node {
1730 union {
1731 struct ubifs_nnode nnode;
1732 struct ubifs_pnode pnode;
1733 struct ubifs_cnode cnode;
1734 };
1735 int in_tree;
1736 union {
1737 struct ubifs_nnode *nnode;
1738 struct ubifs_pnode *pnode;
1739 struct ubifs_cnode *cnode;
1740 } ptr;
1741};
1742
1743/**
1744 * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
1745 * @c: the UBIFS file-system description object
1746 * @path: where to put the nnode
1747 * @parent: parent of the nnode
1748 * @iip: index in parent of the nnode
1749 *
1750 * This function returns a pointer to the nnode on success or a negative error
1751 * code on failure.
1752 */
1753static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
1754 struct lpt_scan_node *path,
1755 struct ubifs_nnode *parent, int iip)
1756{
1757 struct ubifs_nbranch *branch;
1758 struct ubifs_nnode *nnode;
1759 void *buf = c->lpt_nod_buf;
1760 int err;
1761
1762 branch = &parent->nbranch[iip];
1763 nnode = branch->nnode;
1764 if (nnode) {
1765 path->in_tree = 1;
1766 path->ptr.nnode = nnode;
1767 return nnode;
1768 }
1769 nnode = &path->nnode;
1770 path->in_tree = 0;
1771 path->ptr.nnode = nnode;
1772 memset(nnode, 0, sizeof(struct ubifs_nnode));
1773 if (branch->lnum == 0) {
1774 /*
1775 * This nnode was not written which just means that the LEB
1776 * properties in the subtree below it describe empty LEBs. We
1777 * make the nnode as though we had read it, which in fact means
1778 * doing almost nothing.
1779 */
1780 if (c->big_lpt)
1781 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1782 } else {
1783 err = ubi_read(c->ubi, branch->lnum, buf, branch->offs,
1784 c->nnode_sz);
1785 if (err)
1786 return ERR_PTR(err);
1787 err = unpack_nnode(c, buf, nnode);
1788 if (err)
1789 return ERR_PTR(err);
1790 }
1791 err = validate_nnode(c, nnode, parent, iip);
1792 if (err)
1793 return ERR_PTR(err);
1794 if (!c->big_lpt)
1795 nnode->num = calc_nnode_num_from_parent(c, parent, iip);
1796 nnode->level = parent->level - 1;
1797 nnode->parent = parent;
1798 nnode->iip = iip;
1799 return nnode;
1800}
1801
1802/**
1803 * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
1804 * @c: the UBIFS file-system description object
1805 * @path: where to put the pnode
1806 * @parent: parent of the pnode
1807 * @iip: index in parent of the pnode
1808 *
1809 * This function returns a pointer to the pnode on success or a negative error
1810 * code on failure.
1811 */
1812static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
1813 struct lpt_scan_node *path,
1814 struct ubifs_nnode *parent, int iip)
1815{
1816 struct ubifs_nbranch *branch;
1817 struct ubifs_pnode *pnode;
1818 void *buf = c->lpt_nod_buf;
1819 int err;
1820
1821 branch = &parent->nbranch[iip];
1822 pnode = branch->pnode;
1823 if (pnode) {
1824 path->in_tree = 1;
1825 path->ptr.pnode = pnode;
1826 return pnode;
1827 }
1828 pnode = &path->pnode;
1829 path->in_tree = 0;
1830 path->ptr.pnode = pnode;
1831 memset(pnode, 0, sizeof(struct ubifs_pnode));
1832 if (branch->lnum == 0) {
1833 /*
1834 * This pnode was not written which just means that the LEB
1835 * properties in it describe empty LEBs. We make the pnode as
1836 * though we had read it.
1837 */
1838 int i;
1839
1840 if (c->big_lpt)
1841 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1842 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1843 struct ubifs_lprops * const lprops = &pnode->lprops[i];
1844
1845 lprops->free = c->leb_size;
1846 lprops->flags = ubifs_categorize_lprops(c, lprops);
1847 }
1848 } else {
1849 ubifs_assert(branch->lnum >= c->lpt_first &&
1850 branch->lnum <= c->lpt_last);
1851 ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
1852 err = ubi_read(c->ubi, branch->lnum, buf, branch->offs,
1853 c->pnode_sz);
1854 if (err)
1855 return ERR_PTR(err);
1856 err = unpack_pnode(c, buf, pnode);
1857 if (err)
1858 return ERR_PTR(err);
1859 }
1860 err = validate_pnode(c, pnode, parent, iip);
1861 if (err)
1862 return ERR_PTR(err);
1863 if (!c->big_lpt)
1864 pnode->num = calc_pnode_num_from_parent(c, parent, iip);
1865 pnode->parent = parent;
1866 pnode->iip = iip;
1867 set_pnode_lnum(c, pnode);
1868 return pnode;
1869}
1870
1871/**
1872 * ubifs_lpt_scan_nolock - scan the LPT.
1873 * @c: the UBIFS file-system description object
1874 * @start_lnum: LEB number from which to start scanning
1875 * @end_lnum: LEB number at which to stop scanning
1876 * @scan_cb: callback function called for each lprops
1877 * @data: data to be passed to the callback function
1878 *
1879 * This function returns %0 on success and a negative error code on failure.
1880 */
1881int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1882 ubifs_lpt_scan_callback scan_cb, void *data)
1883{
1884 int err = 0, i, h, iip, shft;
1885 struct ubifs_nnode *nnode;
1886 struct ubifs_pnode *pnode;
1887 struct lpt_scan_node *path;
1888
1889 if (start_lnum == -1) {
1890 start_lnum = end_lnum + 1;
1891 if (start_lnum >= c->leb_cnt)
1892 start_lnum = c->main_first;
1893 }
1894
1895 ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
1896 ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
1897
1898 if (!c->nroot) {
1899 err = ubifs_read_nnode(c, NULL, 0);
1900 if (err)
1901 return err;
1902 }
1903
1904 path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
1905 GFP_NOFS);
1906 if (!path)
1907 return -ENOMEM;
1908
1909 path[0].ptr.nnode = c->nroot;
1910 path[0].in_tree = 1;
1911again:
1912 /* Descend to the pnode containing start_lnum */
1913 nnode = c->nroot;
1914 i = start_lnum - c->main_first;
1915 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
1916 for (h = 1; h < c->lpt_hght; h++) {
1917 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1918 shft -= UBIFS_LPT_FANOUT_SHIFT;
1919 nnode = scan_get_nnode(c, path + h, nnode, iip);
1920 if (IS_ERR(nnode)) {
1921 err = PTR_ERR(nnode);
1922 goto out;
1923 }
1924 }
1925 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
1926 shft -= UBIFS_LPT_FANOUT_SHIFT;
1927 pnode = scan_get_pnode(c, path + h, nnode, iip);
1928 if (IS_ERR(pnode)) {
1929 err = PTR_ERR(pnode);
1930 goto out;
1931 }
1932 iip = (i & (UBIFS_LPT_FANOUT - 1));
1933
1934 /* Loop for each lprops */
1935 while (1) {
1936 struct ubifs_lprops *lprops = &pnode->lprops[iip];
1937 int ret, lnum = lprops->lnum;
1938
1939 ret = scan_cb(c, lprops, path[h].in_tree, data);
1940 if (ret < 0) {
1941 err = ret;
1942 goto out;
1943 }
1944 if (ret & LPT_SCAN_ADD) {
1945 /* Add all the nodes in path to the tree in memory */
1946 for (h = 1; h < c->lpt_hght; h++) {
1947 const size_t sz = sizeof(struct ubifs_nnode);
1948 struct ubifs_nnode *parent;
1949
1950 if (path[h].in_tree)
1951 continue;
1952 nnode = kmalloc(sz, GFP_NOFS);
1953 if (!nnode) {
1954 err = -ENOMEM;
1955 goto out;
1956 }
1957 memcpy(nnode, &path[h].nnode, sz);
1958 parent = nnode->parent;
1959 parent->nbranch[nnode->iip].nnode = nnode;
1960 path[h].ptr.nnode = nnode;
1961 path[h].in_tree = 1;
1962 path[h + 1].cnode.parent = nnode;
1963 }
1964 if (path[h].in_tree)
1965 ubifs_ensure_cat(c, lprops);
1966 else {
1967 const size_t sz = sizeof(struct ubifs_pnode);
1968 struct ubifs_nnode *parent;
1969
1970 pnode = kmalloc(sz, GFP_NOFS);
1971 if (!pnode) {
1972 err = -ENOMEM;
1973 goto out;
1974 }
1975 memcpy(pnode, &path[h].pnode, sz);
1976 parent = pnode->parent;
1977 parent->nbranch[pnode->iip].pnode = pnode;
1978 path[h].ptr.pnode = pnode;
1979 path[h].in_tree = 1;
1980 update_cats(c, pnode);
1981 c->pnodes_have += 1;
1982 }
1983 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)
1984 c->nroot, 0, 0);
1985 if (err)
1986 goto out;
1987 err = dbg_check_cats(c);
1988 if (err)
1989 goto out;
1990 }
1991 if (ret & LPT_SCAN_STOP) {
1992 err = 0;
1993 break;
1994 }
1995 /* Get the next lprops */
1996 if (lnum == end_lnum) {
1997 /*
1998 * We got to the end without finding what we were
1999 * looking for
2000 */
2001 err = -ENOSPC;
2002 goto out;
2003 }
2004 if (lnum + 1 >= c->leb_cnt) {
2005 /* Wrap-around to the beginning */
2006 start_lnum = c->main_first;
2007 goto again;
2008 }
2009 if (iip + 1 < UBIFS_LPT_FANOUT) {
2010 /* Next lprops is in the same pnode */
2011 iip += 1;
2012 continue;
2013 }
2014 /* We need to get the next pnode. Go up until we can go right */
2015 iip = pnode->iip;
2016 while (1) {
2017 h -= 1;
2018 ubifs_assert(h >= 0);
2019 nnode = path[h].ptr.nnode;
2020 if (iip + 1 < UBIFS_LPT_FANOUT)
2021 break;
2022 iip = nnode->iip;
2023 }
2024 /* Go right */
2025 iip += 1;
2026 /* Descend to the pnode */
2027 h += 1;
2028 for (; h < c->lpt_hght; h++) {
2029 nnode = scan_get_nnode(c, path + h, nnode, iip);
2030 if (IS_ERR(nnode)) {
2031 err = PTR_ERR(nnode);
2032 goto out;
2033 }
2034 iip = 0;
2035 }
2036 pnode = scan_get_pnode(c, path + h, nnode, iip);
2037 if (IS_ERR(pnode)) {
2038 err = PTR_ERR(pnode);
2039 goto out;
2040 }
2041 iip = 0;
2042 }
2043out:
2044 kfree(path);
2045 return err;
2046}
2047
2048#ifdef CONFIG_UBIFS_FS_DEBUG
2049
2050/**
2051 * dbg_chk_pnode - check a pnode.
2052 * @c: the UBIFS file-system description object
2053 * @pnode: pnode to check
2054 * @col: pnode column
2055 *
2056 * This function returns %0 on success and a negative error code on failure.
2057 */
2058static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
2059 int col)
2060{
2061 int i;
2062
2063 if (pnode->num != col) {
2064 dbg_err("pnode num %d expected %d parent num %d iip %d",
2065 pnode->num, col, pnode->parent->num, pnode->iip);
2066 return -EINVAL;
2067 }
2068 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
2069 struct ubifs_lprops *lp, *lprops = &pnode->lprops[i];
2070 int lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + i +
2071 c->main_first;
2072 int found, cat = lprops->flags & LPROPS_CAT_MASK;
2073 struct ubifs_lpt_heap *heap;
2074 struct list_head *list = NULL;
2075
2076 if (lnum >= c->leb_cnt)
2077 continue;
2078 if (lprops->lnum != lnum) {
2079 dbg_err("bad LEB number %d expected %d",
2080 lprops->lnum, lnum);
2081 return -EINVAL;
2082 }
2083 if (lprops->flags & LPROPS_TAKEN) {
2084 if (cat != LPROPS_UNCAT) {
2085 dbg_err("LEB %d taken but not uncat %d",
2086 lprops->lnum, cat);
2087 return -EINVAL;
2088 }
2089 continue;
2090 }
2091 if (lprops->flags & LPROPS_INDEX) {
2092 switch (cat) {
2093 case LPROPS_UNCAT:
2094 case LPROPS_DIRTY_IDX:
2095 case LPROPS_FRDI_IDX:
2096 break;
2097 default:
2098 dbg_err("LEB %d index but cat %d",
2099 lprops->lnum, cat);
2100 return -EINVAL;
2101 }
2102 } else {
2103 switch (cat) {
2104 case LPROPS_UNCAT:
2105 case LPROPS_DIRTY:
2106 case LPROPS_FREE:
2107 case LPROPS_EMPTY:
2108 case LPROPS_FREEABLE:
2109 break;
2110 default:
2111 dbg_err("LEB %d not index but cat %d",
2112 lprops->lnum, cat);
2113 return -EINVAL;
2114 }
2115 }
2116 switch (cat) {
2117 case LPROPS_UNCAT:
2118 list = &c->uncat_list;
2119 break;
2120 case LPROPS_EMPTY:
2121 list = &c->empty_list;
2122 break;
2123 case LPROPS_FREEABLE:
2124 list = &c->freeable_list;
2125 break;
2126 case LPROPS_FRDI_IDX:
2127 list = &c->frdi_idx_list;
2128 break;
2129 }
2130 found = 0;
2131 switch (cat) {
2132 case LPROPS_DIRTY:
2133 case LPROPS_DIRTY_IDX:
2134 case LPROPS_FREE:
2135 heap = &c->lpt_heap[cat - 1];
2136 if (lprops->hpos < heap->cnt &&
2137 heap->arr[lprops->hpos] == lprops)
2138 found = 1;
2139 break;
2140 case LPROPS_UNCAT:
2141 case LPROPS_EMPTY:
2142 case LPROPS_FREEABLE:
2143 case LPROPS_FRDI_IDX:
2144 list_for_each_entry(lp, list, list)
2145 if (lprops == lp) {
2146 found = 1;
2147 break;
2148 }
2149 break;
2150 }
2151 if (!found) {
2152 dbg_err("LEB %d cat %d not found in cat heap/list",
2153 lprops->lnum, cat);
2154 return -EINVAL;
2155 }
2156 switch (cat) {
2157 case LPROPS_EMPTY:
2158 if (lprops->free != c->leb_size) {
2159 dbg_err("LEB %d cat %d free %d dirty %d",
2160 lprops->lnum, cat, lprops->free,
2161 lprops->dirty);
2162 return -EINVAL;
2163 }
2164 case LPROPS_FREEABLE:
2165 case LPROPS_FRDI_IDX:
2166 if (lprops->free + lprops->dirty != c->leb_size) {
2167 dbg_err("LEB %d cat %d free %d dirty %d",
2168 lprops->lnum, cat, lprops->free,
2169 lprops->dirty);
2170 return -EINVAL;
2171 }
2172 }
2173 }
2174 return 0;
2175}
2176
2177/**
2178 * dbg_check_lpt_nodes - check nnodes and pnodes.
2179 * @c: the UBIFS file-system description object
2180 * @cnode: next cnode (nnode or pnode) to check
2181 * @row: row of cnode (root is zero)
2182 * @col: column of cnode (leftmost is zero)
2183 *
2184 * This function returns %0 on success and a negative error code on failure.
2185 */
2186int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
2187 int row, int col)
2188{
2189 struct ubifs_nnode *nnode, *nn;
2190 struct ubifs_cnode *cn;
2191 int num, iip = 0, err;
2192
2193 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
2194 return 0;
2195
2196 while (cnode) {
2197 ubifs_assert(row >= 0);
2198 nnode = cnode->parent;
2199 if (cnode->level) {
2200 /* cnode is a nnode */
2201 num = calc_nnode_num(row, col);
2202 if (cnode->num != num) {
2203 dbg_err("nnode num %d expected %d "
2204 "parent num %d iip %d", cnode->num, num,
2205 (nnode ? nnode->num : 0), cnode->iip);
2206 return -EINVAL;
2207 }
2208 nn = (struct ubifs_nnode *)cnode;
2209 while (iip < UBIFS_LPT_FANOUT) {
2210 cn = nn->nbranch[iip].cnode;
2211 if (cn) {
2212 /* Go down */
2213 row += 1;
2214 col <<= UBIFS_LPT_FANOUT_SHIFT;
2215 col += iip;
2216 iip = 0;
2217 cnode = cn;
2218 break;
2219 }
2220 /* Go right */
2221 iip += 1;
2222 }
2223 if (iip < UBIFS_LPT_FANOUT)
2224 continue;
2225 } else {
2226 struct ubifs_pnode *pnode;
2227
2228 /* cnode is a pnode */
2229 pnode = (struct ubifs_pnode *)cnode;
2230 err = dbg_chk_pnode(c, pnode, col);
2231 if (err)
2232 return err;
2233 }
2234 /* Go up and to the right */
2235 row -= 1;
2236 col >>= UBIFS_LPT_FANOUT_SHIFT;
2237 iip = cnode->iip + 1;
2238 cnode = (struct ubifs_cnode *)nnode;
2239 }
2240 return 0;
2241}
2242
2243#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/lpt_commit.c b/fs/ubifs/lpt_commit.c
new file mode 100644
index 000000000000..5f0b83e20af6
--- /dev/null
+++ b/fs/ubifs/lpt_commit.c
@@ -0,0 +1,1648 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements commit-related functionality of the LEB properties
25 * subsystem.
26 */
27
28#include <linux/crc16.h>
29#include "ubifs.h"
30
31/**
32 * first_dirty_cnode - find first dirty cnode.
33 * @c: UBIFS file-system description object
34 * @nnode: nnode at which to start
35 *
36 * This function returns the first dirty cnode or %NULL if there is not one.
37 */
38static struct ubifs_cnode *first_dirty_cnode(struct ubifs_nnode *nnode)
39{
40 ubifs_assert(nnode);
41 while (1) {
42 int i, cont = 0;
43
44 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
45 struct ubifs_cnode *cnode;
46
47 cnode = nnode->nbranch[i].cnode;
48 if (cnode &&
49 test_bit(DIRTY_CNODE, &cnode->flags)) {
50 if (cnode->level == 0)
51 return cnode;
52 nnode = (struct ubifs_nnode *)cnode;
53 cont = 1;
54 break;
55 }
56 }
57 if (!cont)
58 return (struct ubifs_cnode *)nnode;
59 }
60}
61
62/**
63 * next_dirty_cnode - find next dirty cnode.
64 * @cnode: cnode from which to begin searching
65 *
66 * This function returns the next dirty cnode or %NULL if there is not one.
67 */
68static struct ubifs_cnode *next_dirty_cnode(struct ubifs_cnode *cnode)
69{
70 struct ubifs_nnode *nnode;
71 int i;
72
73 ubifs_assert(cnode);
74 nnode = cnode->parent;
75 if (!nnode)
76 return NULL;
77 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
78 cnode = nnode->nbranch[i].cnode;
79 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
80 if (cnode->level == 0)
81 return cnode; /* cnode is a pnode */
82 /* cnode is a nnode */
83 return first_dirty_cnode((struct ubifs_nnode *)cnode);
84 }
85 }
86 return (struct ubifs_cnode *)nnode;
87}
88
89/**
90 * get_cnodes_to_commit - create list of dirty cnodes to commit.
91 * @c: UBIFS file-system description object
92 *
93 * This function returns the number of cnodes to commit.
94 */
95static int get_cnodes_to_commit(struct ubifs_info *c)
96{
97 struct ubifs_cnode *cnode, *cnext;
98 int cnt = 0;
99
100 if (!c->nroot)
101 return 0;
102
103 if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
104 return 0;
105
106 c->lpt_cnext = first_dirty_cnode(c->nroot);
107 cnode = c->lpt_cnext;
108 if (!cnode)
109 return 0;
110 cnt += 1;
111 while (1) {
112 ubifs_assert(!test_bit(COW_ZNODE, &cnode->flags));
113 __set_bit(COW_ZNODE, &cnode->flags);
114 cnext = next_dirty_cnode(cnode);
115 if (!cnext) {
116 cnode->cnext = c->lpt_cnext;
117 break;
118 }
119 cnode->cnext = cnext;
120 cnode = cnext;
121 cnt += 1;
122 }
123 dbg_cmt("committing %d cnodes", cnt);
124 dbg_lp("committing %d cnodes", cnt);
125 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
126 return cnt;
127}
128
129/**
130 * upd_ltab - update LPT LEB properties.
131 * @c: UBIFS file-system description object
132 * @lnum: LEB number
133 * @free: amount of free space
134 * @dirty: amount of dirty space to add
135 */
136static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
137{
138 dbg_lp("LEB %d free %d dirty %d to %d +%d",
139 lnum, c->ltab[lnum - c->lpt_first].free,
140 c->ltab[lnum - c->lpt_first].dirty, free, dirty);
141 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last);
142 c->ltab[lnum - c->lpt_first].free = free;
143 c->ltab[lnum - c->lpt_first].dirty += dirty;
144}
145
146/**
147 * alloc_lpt_leb - allocate an LPT LEB that is empty.
148 * @c: UBIFS file-system description object
149 * @lnum: LEB number is passed and returned here
150 *
151 * This function finds the next empty LEB in the ltab starting from @lnum. If a
152 * an empty LEB is found it is returned in @lnum and the function returns %0.
153 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
154 * never to run out of space.
155 */
156static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
157{
158 int i, n;
159
160 n = *lnum - c->lpt_first + 1;
161 for (i = n; i < c->lpt_lebs; i++) {
162 if (c->ltab[i].tgc || c->ltab[i].cmt)
163 continue;
164 if (c->ltab[i].free == c->leb_size) {
165 c->ltab[i].cmt = 1;
166 *lnum = i + c->lpt_first;
167 return 0;
168 }
169 }
170
171 for (i = 0; i < n; i++) {
172 if (c->ltab[i].tgc || c->ltab[i].cmt)
173 continue;
174 if (c->ltab[i].free == c->leb_size) {
175 c->ltab[i].cmt = 1;
176 *lnum = i + c->lpt_first;
177 return 0;
178 }
179 }
180 dbg_err("last LEB %d", *lnum);
181 dump_stack();
182 return -ENOSPC;
183}
184
185/**
186 * layout_cnodes - layout cnodes for commit.
187 * @c: UBIFS file-system description object
188 *
189 * This function returns %0 on success and a negative error code on failure.
190 */
191static int layout_cnodes(struct ubifs_info *c)
192{
193 int lnum, offs, len, alen, done_lsave, done_ltab, err;
194 struct ubifs_cnode *cnode;
195
196 cnode = c->lpt_cnext;
197 if (!cnode)
198 return 0;
199 lnum = c->nhead_lnum;
200 offs = c->nhead_offs;
201 /* Try to place lsave and ltab nicely */
202 done_lsave = !c->big_lpt;
203 done_ltab = 0;
204 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
205 done_lsave = 1;
206 c->lsave_lnum = lnum;
207 c->lsave_offs = offs;
208 offs += c->lsave_sz;
209 }
210
211 if (offs + c->ltab_sz <= c->leb_size) {
212 done_ltab = 1;
213 c->ltab_lnum = lnum;
214 c->ltab_offs = offs;
215 offs += c->ltab_sz;
216 }
217
218 do {
219 if (cnode->level) {
220 len = c->nnode_sz;
221 c->dirty_nn_cnt -= 1;
222 } else {
223 len = c->pnode_sz;
224 c->dirty_pn_cnt -= 1;
225 }
226 while (offs + len > c->leb_size) {
227 alen = ALIGN(offs, c->min_io_size);
228 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
229 err = alloc_lpt_leb(c, &lnum);
230 if (err)
231 return err;
232 offs = 0;
233 ubifs_assert(lnum >= c->lpt_first &&
234 lnum <= c->lpt_last);
235 /* Try to place lsave and ltab nicely */
236 if (!done_lsave) {
237 done_lsave = 1;
238 c->lsave_lnum = lnum;
239 c->lsave_offs = offs;
240 offs += c->lsave_sz;
241 continue;
242 }
243 if (!done_ltab) {
244 done_ltab = 1;
245 c->ltab_lnum = lnum;
246 c->ltab_offs = offs;
247 offs += c->ltab_sz;
248 continue;
249 }
250 break;
251 }
252 if (cnode->parent) {
253 cnode->parent->nbranch[cnode->iip].lnum = lnum;
254 cnode->parent->nbranch[cnode->iip].offs = offs;
255 } else {
256 c->lpt_lnum = lnum;
257 c->lpt_offs = offs;
258 }
259 offs += len;
260 cnode = cnode->cnext;
261 } while (cnode && cnode != c->lpt_cnext);
262
263 /* Make sure to place LPT's save table */
264 if (!done_lsave) {
265 if (offs + c->lsave_sz > c->leb_size) {
266 alen = ALIGN(offs, c->min_io_size);
267 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
268 err = alloc_lpt_leb(c, &lnum);
269 if (err)
270 return err;
271 offs = 0;
272 ubifs_assert(lnum >= c->lpt_first &&
273 lnum <= c->lpt_last);
274 }
275 done_lsave = 1;
276 c->lsave_lnum = lnum;
277 c->lsave_offs = offs;
278 offs += c->lsave_sz;
279 }
280
281 /* Make sure to place LPT's own lprops table */
282 if (!done_ltab) {
283 if (offs + c->ltab_sz > c->leb_size) {
284 alen = ALIGN(offs, c->min_io_size);
285 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
286 err = alloc_lpt_leb(c, &lnum);
287 if (err)
288 return err;
289 offs = 0;
290 ubifs_assert(lnum >= c->lpt_first &&
291 lnum <= c->lpt_last);
292 }
293 done_ltab = 1;
294 c->ltab_lnum = lnum;
295 c->ltab_offs = offs;
296 offs += c->ltab_sz;
297 }
298
299 alen = ALIGN(offs, c->min_io_size);
300 upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
301 return 0;
302}
303
304/**
305 * realloc_lpt_leb - allocate an LPT LEB that is empty.
306 * @c: UBIFS file-system description object
307 * @lnum: LEB number is passed and returned here
308 *
309 * This function duplicates exactly the results of the function alloc_lpt_leb.
310 * It is used during end commit to reallocate the same LEB numbers that were
311 * allocated by alloc_lpt_leb during start commit.
312 *
313 * This function finds the next LEB that was allocated by the alloc_lpt_leb
314 * function starting from @lnum. If a LEB is found it is returned in @lnum and
315 * the function returns %0. Otherwise the function returns -ENOSPC.
316 * Note however, that LPT is designed never to run out of space.
317 */
318static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
319{
320 int i, n;
321
322 n = *lnum - c->lpt_first + 1;
323 for (i = n; i < c->lpt_lebs; i++)
324 if (c->ltab[i].cmt) {
325 c->ltab[i].cmt = 0;
326 *lnum = i + c->lpt_first;
327 return 0;
328 }
329
330 for (i = 0; i < n; i++)
331 if (c->ltab[i].cmt) {
332 c->ltab[i].cmt = 0;
333 *lnum = i + c->lpt_first;
334 return 0;
335 }
336 dbg_err("last LEB %d", *lnum);
337 dump_stack();
338 return -ENOSPC;
339}
340
341/**
342 * write_cnodes - write cnodes for commit.
343 * @c: UBIFS file-system description object
344 *
345 * This function returns %0 on success and a negative error code on failure.
346 */
347static int write_cnodes(struct ubifs_info *c)
348{
349 int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
350 struct ubifs_cnode *cnode;
351 void *buf = c->lpt_buf;
352
353 cnode = c->lpt_cnext;
354 if (!cnode)
355 return 0;
356 lnum = c->nhead_lnum;
357 offs = c->nhead_offs;
358 from = offs;
359 /* Ensure empty LEB is unmapped */
360 if (offs == 0) {
361 err = ubifs_leb_unmap(c, lnum);
362 if (err)
363 return err;
364 }
365 /* Try to place lsave and ltab nicely */
366 done_lsave = !c->big_lpt;
367 done_ltab = 0;
368 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
369 done_lsave = 1;
370 ubifs_pack_lsave(c, buf + offs, c->lsave);
371 offs += c->lsave_sz;
372 }
373
374 if (offs + c->ltab_sz <= c->leb_size) {
375 done_ltab = 1;
376 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
377 offs += c->ltab_sz;
378 }
379
380 /* Loop for each cnode */
381 do {
382 if (cnode->level)
383 len = c->nnode_sz;
384 else
385 len = c->pnode_sz;
386 while (offs + len > c->leb_size) {
387 wlen = offs - from;
388 if (wlen) {
389 alen = ALIGN(wlen, c->min_io_size);
390 memset(buf + offs, 0xff, alen - wlen);
391 err = ubifs_leb_write(c, lnum, buf + from, from,
392 alen, UBI_SHORTTERM);
393 if (err)
394 return err;
395 }
396 err = realloc_lpt_leb(c, &lnum);
397 if (err)
398 return err;
399 offs = 0;
400 from = 0;
401 ubifs_assert(lnum >= c->lpt_first &&
402 lnum <= c->lpt_last);
403 err = ubifs_leb_unmap(c, lnum);
404 if (err)
405 return err;
406 /* Try to place lsave and ltab nicely */
407 if (!done_lsave) {
408 done_lsave = 1;
409 ubifs_pack_lsave(c, buf + offs, c->lsave);
410 offs += c->lsave_sz;
411 continue;
412 }
413 if (!done_ltab) {
414 done_ltab = 1;
415 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
416 offs += c->ltab_sz;
417 continue;
418 }
419 break;
420 }
421 if (cnode->level)
422 ubifs_pack_nnode(c, buf + offs,
423 (struct ubifs_nnode *)cnode);
424 else
425 ubifs_pack_pnode(c, buf + offs,
426 (struct ubifs_pnode *)cnode);
427 /*
428 * The reason for the barriers is the same as in case of TNC.
429 * See comment in 'write_index()'. 'dirty_cow_nnode()' and
430 * 'dirty_cow_pnode()' are the functions for which this is
431 * important.
432 */
433 clear_bit(DIRTY_CNODE, &cnode->flags);
434 smp_mb__before_clear_bit();
435 clear_bit(COW_ZNODE, &cnode->flags);
436 smp_mb__after_clear_bit();
437 offs += len;
438 cnode = cnode->cnext;
439 } while (cnode && cnode != c->lpt_cnext);
440
441 /* Make sure to place LPT's save table */
442 if (!done_lsave) {
443 if (offs + c->lsave_sz > c->leb_size) {
444 wlen = offs - from;
445 alen = ALIGN(wlen, c->min_io_size);
446 memset(buf + offs, 0xff, alen - wlen);
447 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
448 UBI_SHORTTERM);
449 if (err)
450 return err;
451 err = realloc_lpt_leb(c, &lnum);
452 if (err)
453 return err;
454 offs = 0;
455 ubifs_assert(lnum >= c->lpt_first &&
456 lnum <= c->lpt_last);
457 err = ubifs_leb_unmap(c, lnum);
458 if (err)
459 return err;
460 }
461 done_lsave = 1;
462 ubifs_pack_lsave(c, buf + offs, c->lsave);
463 offs += c->lsave_sz;
464 }
465
466 /* Make sure to place LPT's own lprops table */
467 if (!done_ltab) {
468 if (offs + c->ltab_sz > c->leb_size) {
469 wlen = offs - from;
470 alen = ALIGN(wlen, c->min_io_size);
471 memset(buf + offs, 0xff, alen - wlen);
472 err = ubifs_leb_write(c, lnum, buf + from, from, alen,
473 UBI_SHORTTERM);
474 if (err)
475 return err;
476 err = realloc_lpt_leb(c, &lnum);
477 if (err)
478 return err;
479 offs = 0;
480 ubifs_assert(lnum >= c->lpt_first &&
481 lnum <= c->lpt_last);
482 err = ubifs_leb_unmap(c, lnum);
483 if (err)
484 return err;
485 }
486 done_ltab = 1;
487 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
488 offs += c->ltab_sz;
489 }
490
491 /* Write remaining data in buffer */
492 wlen = offs - from;
493 alen = ALIGN(wlen, c->min_io_size);
494 memset(buf + offs, 0xff, alen - wlen);
495 err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
496 if (err)
497 return err;
498 c->nhead_lnum = lnum;
499 c->nhead_offs = ALIGN(offs, c->min_io_size);
500
501 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
502 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
503 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
504 if (c->big_lpt)
505 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
506 return 0;
507}
508
509/**
510 * next_pnode - find next pnode.
511 * @c: UBIFS file-system description object
512 * @pnode: pnode
513 *
514 * This function returns the next pnode or %NULL if there are no more pnodes.
515 */
516static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
517 struct ubifs_pnode *pnode)
518{
519 struct ubifs_nnode *nnode;
520 int iip;
521
522 /* Try to go right */
523 nnode = pnode->parent;
524 iip = pnode->iip + 1;
525 if (iip < UBIFS_LPT_FANOUT) {
526 /* We assume here that LEB zero is never an LPT LEB */
527 if (nnode->nbranch[iip].lnum)
528 return ubifs_get_pnode(c, nnode, iip);
529 else
530 return NULL;
531 }
532
533 /* Go up while can't go right */
534 do {
535 iip = nnode->iip + 1;
536 nnode = nnode->parent;
537 if (!nnode)
538 return NULL;
539 /* We assume here that LEB zero is never an LPT LEB */
540 } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum);
541
542 /* Go right */
543 nnode = ubifs_get_nnode(c, nnode, iip);
544 if (IS_ERR(nnode))
545 return (void *)nnode;
546
547 /* Go down to level 1 */
548 while (nnode->level > 1) {
549 nnode = ubifs_get_nnode(c, nnode, 0);
550 if (IS_ERR(nnode))
551 return (void *)nnode;
552 }
553
554 return ubifs_get_pnode(c, nnode, 0);
555}
556
557/**
558 * pnode_lookup - lookup a pnode in the LPT.
559 * @c: UBIFS file-system description object
560 * @i: pnode number (0 to main_lebs - 1)
561 *
562 * This function returns a pointer to the pnode on success or a negative
563 * error code on failure.
564 */
565static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i)
566{
567 int err, h, iip, shft;
568 struct ubifs_nnode *nnode;
569
570 if (!c->nroot) {
571 err = ubifs_read_nnode(c, NULL, 0);
572 if (err)
573 return ERR_PTR(err);
574 }
575 i <<= UBIFS_LPT_FANOUT_SHIFT;
576 nnode = c->nroot;
577 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
578 for (h = 1; h < c->lpt_hght; h++) {
579 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
580 shft -= UBIFS_LPT_FANOUT_SHIFT;
581 nnode = ubifs_get_nnode(c, nnode, iip);
582 if (IS_ERR(nnode))
583 return ERR_PTR(PTR_ERR(nnode));
584 }
585 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
586 return ubifs_get_pnode(c, nnode, iip);
587}
588
589/**
590 * add_pnode_dirt - add dirty space to LPT LEB properties.
591 * @c: UBIFS file-system description object
592 * @pnode: pnode for which to add dirt
593 */
594static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
595{
596 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
597 c->pnode_sz);
598}
599
600/**
601 * do_make_pnode_dirty - mark a pnode dirty.
602 * @c: UBIFS file-system description object
603 * @pnode: pnode to mark dirty
604 */
605static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
606{
607 /* Assumes cnext list is empty i.e. not called during commit */
608 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
609 struct ubifs_nnode *nnode;
610
611 c->dirty_pn_cnt += 1;
612 add_pnode_dirt(c, pnode);
613 /* Mark parent and ancestors dirty too */
614 nnode = pnode->parent;
615 while (nnode) {
616 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
617 c->dirty_nn_cnt += 1;
618 ubifs_add_nnode_dirt(c, nnode);
619 nnode = nnode->parent;
620 } else
621 break;
622 }
623 }
624}
625
626/**
627 * make_tree_dirty - mark the entire LEB properties tree dirty.
628 * @c: UBIFS file-system description object
629 *
630 * This function is used by the "small" LPT model to cause the entire LEB
631 * properties tree to be written. The "small" LPT model does not use LPT
632 * garbage collection because it is more efficient to write the entire tree
633 * (because it is small).
634 *
635 * This function returns %0 on success and a negative error code on failure.
636 */
637static int make_tree_dirty(struct ubifs_info *c)
638{
639 struct ubifs_pnode *pnode;
640
641 pnode = pnode_lookup(c, 0);
642 while (pnode) {
643 do_make_pnode_dirty(c, pnode);
644 pnode = next_pnode(c, pnode);
645 if (IS_ERR(pnode))
646 return PTR_ERR(pnode);
647 }
648 return 0;
649}
650
651/**
652 * need_write_all - determine if the LPT area is running out of free space.
653 * @c: UBIFS file-system description object
654 *
655 * This function returns %1 if the LPT area is running out of free space and %0
656 * if it is not.
657 */
658static int need_write_all(struct ubifs_info *c)
659{
660 long long free = 0;
661 int i;
662
663 for (i = 0; i < c->lpt_lebs; i++) {
664 if (i + c->lpt_first == c->nhead_lnum)
665 free += c->leb_size - c->nhead_offs;
666 else if (c->ltab[i].free == c->leb_size)
667 free += c->leb_size;
668 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
669 free += c->leb_size;
670 }
671 /* Less than twice the size left */
672 if (free <= c->lpt_sz * 2)
673 return 1;
674 return 0;
675}
676
677/**
678 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
679 * @c: UBIFS file-system description object
680 *
681 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
682 * free space and so may be reused as soon as the next commit is completed.
683 * This function is called during start commit to mark LPT LEBs for trivial GC.
684 */
685static void lpt_tgc_start(struct ubifs_info *c)
686{
687 int i;
688
689 for (i = 0; i < c->lpt_lebs; i++) {
690 if (i + c->lpt_first == c->nhead_lnum)
691 continue;
692 if (c->ltab[i].dirty > 0 &&
693 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
694 c->ltab[i].tgc = 1;
695 c->ltab[i].free = c->leb_size;
696 c->ltab[i].dirty = 0;
697 dbg_lp("LEB %d", i + c->lpt_first);
698 }
699 }
700}
701
702/**
703 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
704 * @c: UBIFS file-system description object
705 *
706 * LPT trivial garbage collection is where a LPT LEB contains only dirty and
707 * free space and so may be reused as soon as the next commit is completed.
708 * This function is called after the commit is completed (master node has been
709 * written) and unmaps LPT LEBs that were marked for trivial GC.
710 */
711static int lpt_tgc_end(struct ubifs_info *c)
712{
713 int i, err;
714
715 for (i = 0; i < c->lpt_lebs; i++)
716 if (c->ltab[i].tgc) {
717 err = ubifs_leb_unmap(c, i + c->lpt_first);
718 if (err)
719 return err;
720 c->ltab[i].tgc = 0;
721 dbg_lp("LEB %d", i + c->lpt_first);
722 }
723 return 0;
724}
725
726/**
727 * populate_lsave - fill the lsave array with important LEB numbers.
728 * @c: the UBIFS file-system description object
729 *
730 * This function is only called for the "big" model. It records a small number
731 * of LEB numbers of important LEBs. Important LEBs are ones that are (from
732 * most important to least important): empty, freeable, freeable index, dirty
733 * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
734 * their pnodes into memory. That will stop us from having to scan the LPT
735 * straight away. For the "small" model we assume that scanning the LPT is no
736 * big deal.
737 */
738static void populate_lsave(struct ubifs_info *c)
739{
740 struct ubifs_lprops *lprops;
741 struct ubifs_lpt_heap *heap;
742 int i, cnt = 0;
743
744 ubifs_assert(c->big_lpt);
745 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
746 c->lpt_drty_flgs |= LSAVE_DIRTY;
747 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
748 }
749 list_for_each_entry(lprops, &c->empty_list, list) {
750 c->lsave[cnt++] = lprops->lnum;
751 if (cnt >= c->lsave_cnt)
752 return;
753 }
754 list_for_each_entry(lprops, &c->freeable_list, list) {
755 c->lsave[cnt++] = lprops->lnum;
756 if (cnt >= c->lsave_cnt)
757 return;
758 }
759 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
760 c->lsave[cnt++] = lprops->lnum;
761 if (cnt >= c->lsave_cnt)
762 return;
763 }
764 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
765 for (i = 0; i < heap->cnt; i++) {
766 c->lsave[cnt++] = heap->arr[i]->lnum;
767 if (cnt >= c->lsave_cnt)
768 return;
769 }
770 heap = &c->lpt_heap[LPROPS_DIRTY - 1];
771 for (i = 0; i < heap->cnt; i++) {
772 c->lsave[cnt++] = heap->arr[i]->lnum;
773 if (cnt >= c->lsave_cnt)
774 return;
775 }
776 heap = &c->lpt_heap[LPROPS_FREE - 1];
777 for (i = 0; i < heap->cnt; i++) {
778 c->lsave[cnt++] = heap->arr[i]->lnum;
779 if (cnt >= c->lsave_cnt)
780 return;
781 }
782 /* Fill it up completely */
783 while (cnt < c->lsave_cnt)
784 c->lsave[cnt++] = c->main_first;
785}
786
787/**
788 * nnode_lookup - lookup a nnode in the LPT.
789 * @c: UBIFS file-system description object
790 * @i: nnode number
791 *
792 * This function returns a pointer to the nnode on success or a negative
793 * error code on failure.
794 */
795static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
796{
797 int err, iip;
798 struct ubifs_nnode *nnode;
799
800 if (!c->nroot) {
801 err = ubifs_read_nnode(c, NULL, 0);
802 if (err)
803 return ERR_PTR(err);
804 }
805 nnode = c->nroot;
806 while (1) {
807 iip = i & (UBIFS_LPT_FANOUT - 1);
808 i >>= UBIFS_LPT_FANOUT_SHIFT;
809 if (!i)
810 break;
811 nnode = ubifs_get_nnode(c, nnode, iip);
812 if (IS_ERR(nnode))
813 return nnode;
814 }
815 return nnode;
816}
817
818/**
819 * make_nnode_dirty - find a nnode and, if found, make it dirty.
820 * @c: UBIFS file-system description object
821 * @node_num: nnode number of nnode to make dirty
822 * @lnum: LEB number where nnode was written
823 * @offs: offset where nnode was written
824 *
825 * This function is used by LPT garbage collection. LPT garbage collection is
826 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
827 * simply involves marking all the nodes in the LEB being garbage-collected as
828 * dirty. The dirty nodes are written next commit, after which the LEB is free
829 * to be reused.
830 *
831 * This function returns %0 on success and a negative error code on failure.
832 */
833static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
834 int offs)
835{
836 struct ubifs_nnode *nnode;
837
838 nnode = nnode_lookup(c, node_num);
839 if (IS_ERR(nnode))
840 return PTR_ERR(nnode);
841 if (nnode->parent) {
842 struct ubifs_nbranch *branch;
843
844 branch = &nnode->parent->nbranch[nnode->iip];
845 if (branch->lnum != lnum || branch->offs != offs)
846 return 0; /* nnode is obsolete */
847 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
848 return 0; /* nnode is obsolete */
849 /* Assumes cnext list is empty i.e. not called during commit */
850 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
851 c->dirty_nn_cnt += 1;
852 ubifs_add_nnode_dirt(c, nnode);
853 /* Mark parent and ancestors dirty too */
854 nnode = nnode->parent;
855 while (nnode) {
856 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
857 c->dirty_nn_cnt += 1;
858 ubifs_add_nnode_dirt(c, nnode);
859 nnode = nnode->parent;
860 } else
861 break;
862 }
863 }
864 return 0;
865}
866
867/**
868 * make_pnode_dirty - find a pnode and, if found, make it dirty.
869 * @c: UBIFS file-system description object
870 * @node_num: pnode number of pnode to make dirty
871 * @lnum: LEB number where pnode was written
872 * @offs: offset where pnode was written
873 *
874 * This function is used by LPT garbage collection. LPT garbage collection is
875 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
876 * simply involves marking all the nodes in the LEB being garbage-collected as
877 * dirty. The dirty nodes are written next commit, after which the LEB is free
878 * to be reused.
879 *
880 * This function returns %0 on success and a negative error code on failure.
881 */
882static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
883 int offs)
884{
885 struct ubifs_pnode *pnode;
886 struct ubifs_nbranch *branch;
887
888 pnode = pnode_lookup(c, node_num);
889 if (IS_ERR(pnode))
890 return PTR_ERR(pnode);
891 branch = &pnode->parent->nbranch[pnode->iip];
892 if (branch->lnum != lnum || branch->offs != offs)
893 return 0;
894 do_make_pnode_dirty(c, pnode);
895 return 0;
896}
897
898/**
899 * make_ltab_dirty - make ltab node dirty.
900 * @c: UBIFS file-system description object
901 * @lnum: LEB number where ltab was written
902 * @offs: offset where ltab was written
903 *
904 * This function is used by LPT garbage collection. LPT garbage collection is
905 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
906 * simply involves marking all the nodes in the LEB being garbage-collected as
907 * dirty. The dirty nodes are written next commit, after which the LEB is free
908 * to be reused.
909 *
910 * This function returns %0 on success and a negative error code on failure.
911 */
912static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
913{
914 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
915 return 0; /* This ltab node is obsolete */
916 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
917 c->lpt_drty_flgs |= LTAB_DIRTY;
918 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
919 }
920 return 0;
921}
922
923/**
924 * make_lsave_dirty - make lsave node dirty.
925 * @c: UBIFS file-system description object
926 * @lnum: LEB number where lsave was written
927 * @offs: offset where lsave was written
928 *
929 * This function is used by LPT garbage collection. LPT garbage collection is
930 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
931 * simply involves marking all the nodes in the LEB being garbage-collected as
932 * dirty. The dirty nodes are written next commit, after which the LEB is free
933 * to be reused.
934 *
935 * This function returns %0 on success and a negative error code on failure.
936 */
937static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
938{
939 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
940 return 0; /* This lsave node is obsolete */
941 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
942 c->lpt_drty_flgs |= LSAVE_DIRTY;
943 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
944 }
945 return 0;
946}
947
948/**
949 * make_node_dirty - make node dirty.
950 * @c: UBIFS file-system description object
951 * @node_type: LPT node type
952 * @node_num: node number
953 * @lnum: LEB number where node was written
954 * @offs: offset where node was written
955 *
956 * This function is used by LPT garbage collection. LPT garbage collection is
957 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
958 * simply involves marking all the nodes in the LEB being garbage-collected as
959 * dirty. The dirty nodes are written next commit, after which the LEB is free
960 * to be reused.
961 *
962 * This function returns %0 on success and a negative error code on failure.
963 */
964static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
965 int lnum, int offs)
966{
967 switch (node_type) {
968 case UBIFS_LPT_NNODE:
969 return make_nnode_dirty(c, node_num, lnum, offs);
970 case UBIFS_LPT_PNODE:
971 return make_pnode_dirty(c, node_num, lnum, offs);
972 case UBIFS_LPT_LTAB:
973 return make_ltab_dirty(c, lnum, offs);
974 case UBIFS_LPT_LSAVE:
975 return make_lsave_dirty(c, lnum, offs);
976 }
977 return -EINVAL;
978}
979
980/**
981 * get_lpt_node_len - return the length of a node based on its type.
982 * @c: UBIFS file-system description object
983 * @node_type: LPT node type
984 */
985static int get_lpt_node_len(struct ubifs_info *c, int node_type)
986{
987 switch (node_type) {
988 case UBIFS_LPT_NNODE:
989 return c->nnode_sz;
990 case UBIFS_LPT_PNODE:
991 return c->pnode_sz;
992 case UBIFS_LPT_LTAB:
993 return c->ltab_sz;
994 case UBIFS_LPT_LSAVE:
995 return c->lsave_sz;
996 }
997 return 0;
998}
999
1000/**
1001 * get_pad_len - return the length of padding in a buffer.
1002 * @c: UBIFS file-system description object
1003 * @buf: buffer
1004 * @len: length of buffer
1005 */
1006static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
1007{
1008 int offs, pad_len;
1009
1010 if (c->min_io_size == 1)
1011 return 0;
1012 offs = c->leb_size - len;
1013 pad_len = ALIGN(offs, c->min_io_size) - offs;
1014 return pad_len;
1015}
1016
1017/**
1018 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1019 * @c: UBIFS file-system description object
1020 * @buf: buffer
1021 * @node_num: node number is returned here
1022 */
1023static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
1024{
1025 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1026 int pos = 0, node_type;
1027
1028 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1029 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
1030 return node_type;
1031}
1032
1033/**
1034 * is_a_node - determine if a buffer contains a node.
1035 * @c: UBIFS file-system description object
1036 * @buf: buffer
1037 * @len: length of buffer
1038 *
1039 * This function returns %1 if the buffer contains a node or %0 if it does not.
1040 */
1041static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
1042{
1043 uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1044 int pos = 0, node_type, node_len;
1045 uint16_t crc, calc_crc;
1046
1047 node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
1048 if (node_type == UBIFS_LPT_NOT_A_NODE)
1049 return 0;
1050 node_len = get_lpt_node_len(c, node_type);
1051 if (!node_len || node_len > len)
1052 return 0;
1053 pos = 0;
1054 addr = buf;
1055 crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
1056 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1057 node_len - UBIFS_LPT_CRC_BYTES);
1058 if (crc != calc_crc)
1059 return 0;
1060 return 1;
1061}
1062
1063
1064/**
1065 * lpt_gc_lnum - garbage collect a LPT LEB.
1066 * @c: UBIFS file-system description object
1067 * @lnum: LEB number to garbage collect
1068 *
1069 * LPT garbage collection is used only for the "big" LPT model
1070 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1071 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1072 * next commit, after which the LEB is free to be reused.
1073 *
1074 * This function returns %0 on success and a negative error code on failure.
1075 */
1076static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1077{
1078 int err, len = c->leb_size, node_type, node_num, node_len, offs;
1079 void *buf = c->lpt_buf;
1080
1081 dbg_lp("LEB %d", lnum);
1082 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1083 if (err) {
1084 ubifs_err("cannot read LEB %d, error %d", lnum, err);
1085 return err;
1086 }
1087 while (1) {
1088 if (!is_a_node(c, buf, len)) {
1089 int pad_len;
1090
1091 pad_len = get_pad_len(c, buf, len);
1092 if (pad_len) {
1093 buf += pad_len;
1094 len -= pad_len;
1095 continue;
1096 }
1097 return 0;
1098 }
1099 node_type = get_lpt_node_type(c, buf, &node_num);
1100 node_len = get_lpt_node_len(c, node_type);
1101 offs = c->leb_size - len;
1102 ubifs_assert(node_len != 0);
1103 mutex_lock(&c->lp_mutex);
1104 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1105 mutex_unlock(&c->lp_mutex);
1106 if (err)
1107 return err;
1108 buf += node_len;
1109 len -= node_len;
1110 }
1111 return 0;
1112}
1113
1114/**
1115 * lpt_gc - LPT garbage collection.
1116 * @c: UBIFS file-system description object
1117 *
1118 * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1119 * Returns %0 on success and a negative error code on failure.
1120 */
1121static int lpt_gc(struct ubifs_info *c)
1122{
1123 int i, lnum = -1, dirty = 0;
1124
1125 mutex_lock(&c->lp_mutex);
1126 for (i = 0; i < c->lpt_lebs; i++) {
1127 ubifs_assert(!c->ltab[i].tgc);
1128 if (i + c->lpt_first == c->nhead_lnum ||
1129 c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1130 continue;
1131 if (c->ltab[i].dirty > dirty) {
1132 dirty = c->ltab[i].dirty;
1133 lnum = i + c->lpt_first;
1134 }
1135 }
1136 mutex_unlock(&c->lp_mutex);
1137 if (lnum == -1)
1138 return -ENOSPC;
1139 return lpt_gc_lnum(c, lnum);
1140}
1141
1142/**
1143 * ubifs_lpt_start_commit - UBIFS commit starts.
1144 * @c: the UBIFS file-system description object
1145 *
1146 * This function has to be called when UBIFS starts the commit operation.
1147 * This function "freezes" all currently dirty LEB properties and does not
1148 * change them anymore. Further changes are saved and tracked separately
1149 * because they are not part of this commit. This function returns zero in case
1150 * of success and a negative error code in case of failure.
1151 */
1152int ubifs_lpt_start_commit(struct ubifs_info *c)
1153{
1154 int err, cnt;
1155
1156 dbg_lp("");
1157
1158 mutex_lock(&c->lp_mutex);
1159 err = dbg_check_ltab(c);
1160 if (err)
1161 goto out;
1162
1163 if (c->check_lpt_free) {
1164 /*
1165 * We ensure there is enough free space in
1166 * ubifs_lpt_post_commit() by marking nodes dirty. That
1167 * information is lost when we unmount, so we also need
1168 * to check free space once after mounting also.
1169 */
1170 c->check_lpt_free = 0;
1171 while (need_write_all(c)) {
1172 mutex_unlock(&c->lp_mutex);
1173 err = lpt_gc(c);
1174 if (err)
1175 return err;
1176 mutex_lock(&c->lp_mutex);
1177 }
1178 }
1179
1180 lpt_tgc_start(c);
1181
1182 if (!c->dirty_pn_cnt) {
1183 dbg_cmt("no cnodes to commit");
1184 err = 0;
1185 goto out;
1186 }
1187
1188 if (!c->big_lpt && need_write_all(c)) {
1189 /* If needed, write everything */
1190 err = make_tree_dirty(c);
1191 if (err)
1192 goto out;
1193 lpt_tgc_start(c);
1194 }
1195
1196 if (c->big_lpt)
1197 populate_lsave(c);
1198
1199 cnt = get_cnodes_to_commit(c);
1200 ubifs_assert(cnt != 0);
1201
1202 err = layout_cnodes(c);
1203 if (err)
1204 goto out;
1205
1206 /* Copy the LPT's own lprops for end commit to write */
1207 memcpy(c->ltab_cmt, c->ltab,
1208 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1209 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1210
1211out:
1212 mutex_unlock(&c->lp_mutex);
1213 return err;
1214}
1215
1216/**
1217 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1218 * @c: UBIFS file-system description object
1219 */
1220static void free_obsolete_cnodes(struct ubifs_info *c)
1221{
1222 struct ubifs_cnode *cnode, *cnext;
1223
1224 cnext = c->lpt_cnext;
1225 if (!cnext)
1226 return;
1227 do {
1228 cnode = cnext;
1229 cnext = cnode->cnext;
1230 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1231 kfree(cnode);
1232 else
1233 cnode->cnext = NULL;
1234 } while (cnext != c->lpt_cnext);
1235 c->lpt_cnext = NULL;
1236}
1237
1238/**
1239 * ubifs_lpt_end_commit - finish the commit operation.
1240 * @c: the UBIFS file-system description object
1241 *
1242 * This function has to be called when the commit operation finishes. It
1243 * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1244 * the media. Returns zero in case of success and a negative error code in case
1245 * of failure.
1246 */
1247int ubifs_lpt_end_commit(struct ubifs_info *c)
1248{
1249 int err;
1250
1251 dbg_lp("");
1252
1253 if (!c->lpt_cnext)
1254 return 0;
1255
1256 err = write_cnodes(c);
1257 if (err)
1258 return err;
1259
1260 mutex_lock(&c->lp_mutex);
1261 free_obsolete_cnodes(c);
1262 mutex_unlock(&c->lp_mutex);
1263
1264 return 0;
1265}
1266
1267/**
1268 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1269 * @c: UBIFS file-system description object
1270 *
1271 * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1272 * commit for the "big" LPT model.
1273 */
1274int ubifs_lpt_post_commit(struct ubifs_info *c)
1275{
1276 int err;
1277
1278 mutex_lock(&c->lp_mutex);
1279 err = lpt_tgc_end(c);
1280 if (err)
1281 goto out;
1282 if (c->big_lpt)
1283 while (need_write_all(c)) {
1284 mutex_unlock(&c->lp_mutex);
1285 err = lpt_gc(c);
1286 if (err)
1287 return err;
1288 mutex_lock(&c->lp_mutex);
1289 }
1290out:
1291 mutex_unlock(&c->lp_mutex);
1292 return err;
1293}
1294
1295/**
1296 * first_nnode - find the first nnode in memory.
1297 * @c: UBIFS file-system description object
1298 * @hght: height of tree where nnode found is returned here
1299 *
1300 * This function returns a pointer to the nnode found or %NULL if no nnode is
1301 * found. This function is a helper to 'ubifs_lpt_free()'.
1302 */
1303static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1304{
1305 struct ubifs_nnode *nnode;
1306 int h, i, found;
1307
1308 nnode = c->nroot;
1309 *hght = 0;
1310 if (!nnode)
1311 return NULL;
1312 for (h = 1; h < c->lpt_hght; h++) {
1313 found = 0;
1314 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1315 if (nnode->nbranch[i].nnode) {
1316 found = 1;
1317 nnode = nnode->nbranch[i].nnode;
1318 *hght = h;
1319 break;
1320 }
1321 }
1322 if (!found)
1323 break;
1324 }
1325 return nnode;
1326}
1327
1328/**
1329 * next_nnode - find the next nnode in memory.
1330 * @c: UBIFS file-system description object
1331 * @nnode: nnode from which to start.
1332 * @hght: height of tree where nnode is, is passed and returned here
1333 *
1334 * This function returns a pointer to the nnode found or %NULL if no nnode is
1335 * found. This function is a helper to 'ubifs_lpt_free()'.
1336 */
1337static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1338 struct ubifs_nnode *nnode, int *hght)
1339{
1340 struct ubifs_nnode *parent;
1341 int iip, h, i, found;
1342
1343 parent = nnode->parent;
1344 if (!parent)
1345 return NULL;
1346 if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1347 *hght -= 1;
1348 return parent;
1349 }
1350 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1351 nnode = parent->nbranch[iip].nnode;
1352 if (nnode)
1353 break;
1354 }
1355 if (!nnode) {
1356 *hght -= 1;
1357 return parent;
1358 }
1359 for (h = *hght + 1; h < c->lpt_hght; h++) {
1360 found = 0;
1361 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1362 if (nnode->nbranch[i].nnode) {
1363 found = 1;
1364 nnode = nnode->nbranch[i].nnode;
1365 *hght = h;
1366 break;
1367 }
1368 }
1369 if (!found)
1370 break;
1371 }
1372 return nnode;
1373}
1374
1375/**
1376 * ubifs_lpt_free - free resources owned by the LPT.
1377 * @c: UBIFS file-system description object
1378 * @wr_only: free only resources used for writing
1379 */
1380void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1381{
1382 struct ubifs_nnode *nnode;
1383 int i, hght;
1384
1385 /* Free write-only things first */
1386
1387 free_obsolete_cnodes(c); /* Leftover from a failed commit */
1388
1389 vfree(c->ltab_cmt);
1390 c->ltab_cmt = NULL;
1391 vfree(c->lpt_buf);
1392 c->lpt_buf = NULL;
1393 kfree(c->lsave);
1394 c->lsave = NULL;
1395
1396 if (wr_only)
1397 return;
1398
1399 /* Now free the rest */
1400
1401 nnode = first_nnode(c, &hght);
1402 while (nnode) {
1403 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1404 kfree(nnode->nbranch[i].nnode);
1405 nnode = next_nnode(c, nnode, &hght);
1406 }
1407 for (i = 0; i < LPROPS_HEAP_CNT; i++)
1408 kfree(c->lpt_heap[i].arr);
1409 kfree(c->dirty_idx.arr);
1410 kfree(c->nroot);
1411 vfree(c->ltab);
1412 kfree(c->lpt_nod_buf);
1413}
1414
1415#ifdef CONFIG_UBIFS_FS_DEBUG
1416
1417/**
1418 * dbg_is_all_ff - determine if a buffer contains only 0xff bytes.
1419 * @buf: buffer
1420 * @len: buffer length
1421 */
1422static int dbg_is_all_ff(uint8_t *buf, int len)
1423{
1424 int i;
1425
1426 for (i = 0; i < len; i++)
1427 if (buf[i] != 0xff)
1428 return 0;
1429 return 1;
1430}
1431
1432/**
1433 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1434 * @c: the UBIFS file-system description object
1435 * @lnum: LEB number where nnode was written
1436 * @offs: offset where nnode was written
1437 */
1438static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1439{
1440 struct ubifs_nnode *nnode;
1441 int hght;
1442
1443 /* Entire tree is in memory so first_nnode / next_nnode are ok */
1444 nnode = first_nnode(c, &hght);
1445 for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1446 struct ubifs_nbranch *branch;
1447
1448 cond_resched();
1449 if (nnode->parent) {
1450 branch = &nnode->parent->nbranch[nnode->iip];
1451 if (branch->lnum != lnum || branch->offs != offs)
1452 continue;
1453 if (test_bit(DIRTY_CNODE, &nnode->flags))
1454 return 1;
1455 return 0;
1456 } else {
1457 if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1458 continue;
1459 if (test_bit(DIRTY_CNODE, &nnode->flags))
1460 return 1;
1461 return 0;
1462 }
1463 }
1464 return 1;
1465}
1466
1467/**
1468 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1469 * @c: the UBIFS file-system description object
1470 * @lnum: LEB number where pnode was written
1471 * @offs: offset where pnode was written
1472 */
1473static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1474{
1475 int i, cnt;
1476
1477 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1478 for (i = 0; i < cnt; i++) {
1479 struct ubifs_pnode *pnode;
1480 struct ubifs_nbranch *branch;
1481
1482 cond_resched();
1483 pnode = pnode_lookup(c, i);
1484 if (IS_ERR(pnode))
1485 return PTR_ERR(pnode);
1486 branch = &pnode->parent->nbranch[pnode->iip];
1487 if (branch->lnum != lnum || branch->offs != offs)
1488 continue;
1489 if (test_bit(DIRTY_CNODE, &pnode->flags))
1490 return 1;
1491 return 0;
1492 }
1493 return 1;
1494}
1495
1496/**
1497 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1498 * @c: the UBIFS file-system description object
1499 * @lnum: LEB number where ltab node was written
1500 * @offs: offset where ltab node was written
1501 */
1502static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1503{
1504 if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1505 return 1;
1506 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1507}
1508
1509/**
1510 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1511 * @c: the UBIFS file-system description object
1512 * @lnum: LEB number where lsave node was written
1513 * @offs: offset where lsave node was written
1514 */
1515static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1516{
1517 if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1518 return 1;
1519 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1520}
1521
1522/**
1523 * dbg_is_node_dirty - determine if a node is dirty.
1524 * @c: the UBIFS file-system description object
1525 * @node_type: node type
1526 * @lnum: LEB number where node was written
1527 * @offs: offset where node was written
1528 */
1529static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1530 int offs)
1531{
1532 switch (node_type) {
1533 case UBIFS_LPT_NNODE:
1534 return dbg_is_nnode_dirty(c, lnum, offs);
1535 case UBIFS_LPT_PNODE:
1536 return dbg_is_pnode_dirty(c, lnum, offs);
1537 case UBIFS_LPT_LTAB:
1538 return dbg_is_ltab_dirty(c, lnum, offs);
1539 case UBIFS_LPT_LSAVE:
1540 return dbg_is_lsave_dirty(c, lnum, offs);
1541 }
1542 return 1;
1543}
1544
1545/**
1546 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1547 * @c: the UBIFS file-system description object
1548 * @lnum: LEB number where node was written
1549 * @offs: offset where node was written
1550 *
1551 * This function returns %0 on success and a negative error code on failure.
1552 */
1553static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1554{
1555 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1556 int ret;
1557 void *buf = c->dbg_buf;
1558
1559 dbg_lp("LEB %d", lnum);
1560 err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
1561 if (err) {
1562 dbg_msg("ubi_read failed, LEB %d, error %d", lnum, err);
1563 return err;
1564 }
1565 while (1) {
1566 if (!is_a_node(c, buf, len)) {
1567 int i, pad_len;
1568
1569 pad_len = get_pad_len(c, buf, len);
1570 if (pad_len) {
1571 buf += pad_len;
1572 len -= pad_len;
1573 dirty += pad_len;
1574 continue;
1575 }
1576 if (!dbg_is_all_ff(buf, len)) {
1577 dbg_msg("invalid empty space in LEB %d at %d",
1578 lnum, c->leb_size - len);
1579 err = -EINVAL;
1580 }
1581 i = lnum - c->lpt_first;
1582 if (len != c->ltab[i].free) {
1583 dbg_msg("invalid free space in LEB %d "
1584 "(free %d, expected %d)",
1585 lnum, len, c->ltab[i].free);
1586 err = -EINVAL;
1587 }
1588 if (dirty != c->ltab[i].dirty) {
1589 dbg_msg("invalid dirty space in LEB %d "
1590 "(dirty %d, expected %d)",
1591 lnum, dirty, c->ltab[i].dirty);
1592 err = -EINVAL;
1593 }
1594 return err;
1595 }
1596 node_type = get_lpt_node_type(c, buf, &node_num);
1597 node_len = get_lpt_node_len(c, node_type);
1598 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1599 if (ret == 1)
1600 dirty += node_len;
1601 buf += node_len;
1602 len -= node_len;
1603 }
1604}
1605
1606/**
1607 * dbg_check_ltab - check the free and dirty space in the ltab.
1608 * @c: the UBIFS file-system description object
1609 *
1610 * This function returns %0 on success and a negative error code on failure.
1611 */
1612int dbg_check_ltab(struct ubifs_info *c)
1613{
1614 int lnum, err, i, cnt;
1615
1616 if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
1617 return 0;
1618
1619 /* Bring the entire tree into memory */
1620 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1621 for (i = 0; i < cnt; i++) {
1622 struct ubifs_pnode *pnode;
1623
1624 pnode = pnode_lookup(c, i);
1625 if (IS_ERR(pnode))
1626 return PTR_ERR(pnode);
1627 cond_resched();
1628 }
1629
1630 /* Check nodes */
1631 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1632 if (err)
1633 return err;
1634
1635 /* Check each LEB */
1636 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1637 err = dbg_check_ltab_lnum(c, lnum);
1638 if (err) {
1639 dbg_err("failed at LEB %d", lnum);
1640 return err;
1641 }
1642 }
1643
1644 dbg_lp("succeeded");
1645 return 0;
1646}
1647
1648#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/master.c b/fs/ubifs/master.c
new file mode 100644
index 000000000000..71d5493bf565
--- /dev/null
+++ b/fs/ubifs/master.c
@@ -0,0 +1,387 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/* This file implements reading and writing the master node */
24
25#include "ubifs.h"
26
27/**
28 * scan_for_master - search the valid master node.
29 * @c: UBIFS file-system description object
30 *
31 * This function scans the master node LEBs and search for the latest master
32 * node. Returns zero in case of success and a negative error code in case of
33 * failure.
34 */
35static int scan_for_master(struct ubifs_info *c)
36{
37 struct ubifs_scan_leb *sleb;
38 struct ubifs_scan_node *snod;
39 int lnum, offs = 0, nodes_cnt;
40
41 lnum = UBIFS_MST_LNUM;
42
43 sleb = ubifs_scan(c, lnum, 0, c->sbuf);
44 if (IS_ERR(sleb))
45 return PTR_ERR(sleb);
46 nodes_cnt = sleb->nodes_cnt;
47 if (nodes_cnt > 0) {
48 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
49 list);
50 if (snod->type != UBIFS_MST_NODE)
51 goto out;
52 memcpy(c->mst_node, snod->node, snod->len);
53 offs = snod->offs;
54 }
55 ubifs_scan_destroy(sleb);
56
57 lnum += 1;
58
59 sleb = ubifs_scan(c, lnum, 0, c->sbuf);
60 if (IS_ERR(sleb))
61 return PTR_ERR(sleb);
62 if (sleb->nodes_cnt != nodes_cnt)
63 goto out;
64 if (!sleb->nodes_cnt)
65 goto out;
66 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
67 if (snod->type != UBIFS_MST_NODE)
68 goto out;
69 if (snod->offs != offs)
70 goto out;
71 if (memcmp((void *)c->mst_node + UBIFS_CH_SZ,
72 (void *)snod->node + UBIFS_CH_SZ,
73 UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
74 goto out;
75 c->mst_offs = offs;
76 ubifs_scan_destroy(sleb);
77 return 0;
78
79out:
80 ubifs_scan_destroy(sleb);
81 return -EINVAL;
82}
83
84/**
85 * validate_master - validate master node.
86 * @c: UBIFS file-system description object
87 *
88 * This function validates data which was read from master node. Returns zero
89 * if the data is all right and %-EINVAL if not.
90 */
91static int validate_master(const struct ubifs_info *c)
92{
93 long long main_sz;
94 int err;
95
96 if (c->max_sqnum >= SQNUM_WATERMARK) {
97 err = 1;
98 goto out;
99 }
100
101 if (c->cmt_no >= c->max_sqnum) {
102 err = 2;
103 goto out;
104 }
105
106 if (c->highest_inum >= INUM_WATERMARK) {
107 err = 3;
108 goto out;
109 }
110
111 if (c->lhead_lnum < UBIFS_LOG_LNUM ||
112 c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
113 c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
114 c->lhead_offs & (c->min_io_size - 1)) {
115 err = 4;
116 goto out;
117 }
118
119 if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
120 c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
121 err = 5;
122 goto out;
123 }
124
125 if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
126 c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
127 err = 6;
128 goto out;
129 }
130
131 if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
132 err = 7;
133 goto out;
134 }
135
136 if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
137 c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
138 c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
139 err = 8;
140 goto out;
141 }
142
143 main_sz = (long long)c->main_lebs * c->leb_size;
144 if (c->old_idx_sz & 7 || c->old_idx_sz >= main_sz) {
145 err = 9;
146 goto out;
147 }
148
149 if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
150 c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
151 err = 10;
152 goto out;
153 }
154
155 if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
156 c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
157 c->nhead_offs > c->leb_size) {
158 err = 11;
159 goto out;
160 }
161
162 if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
163 c->ltab_offs < 0 ||
164 c->ltab_offs + c->ltab_sz > c->leb_size) {
165 err = 12;
166 goto out;
167 }
168
169 if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
170 c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
171 c->lsave_offs + c->lsave_sz > c->leb_size)) {
172 err = 13;
173 goto out;
174 }
175
176 if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
177 err = 14;
178 goto out;
179 }
180
181 if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
182 err = 15;
183 goto out;
184 }
185
186 if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
187 err = 16;
188 goto out;
189 }
190
191 if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
192 c->lst.total_free & 7) {
193 err = 17;
194 goto out;
195 }
196
197 if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
198 err = 18;
199 goto out;
200 }
201
202 if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
203 err = 19;
204 goto out;
205 }
206
207 if (c->lst.total_free + c->lst.total_dirty +
208 c->lst.total_used > main_sz) {
209 err = 20;
210 goto out;
211 }
212
213 if (c->lst.total_dead + c->lst.total_dark +
214 c->lst.total_used + c->old_idx_sz > main_sz) {
215 err = 21;
216 goto out;
217 }
218
219 if (c->lst.total_dead < 0 ||
220 c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
221 c->lst.total_dead & 7) {
222 err = 22;
223 goto out;
224 }
225
226 if (c->lst.total_dark < 0 ||
227 c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
228 c->lst.total_dark & 7) {
229 err = 23;
230 goto out;
231 }
232
233 return 0;
234
235out:
236 ubifs_err("bad master node at offset %d error %d", c->mst_offs, err);
237 dbg_dump_node(c, c->mst_node);
238 return -EINVAL;
239}
240
241/**
242 * ubifs_read_master - read master node.
243 * @c: UBIFS file-system description object
244 *
245 * This function finds and reads the master node during file-system mount. If
246 * the flash is empty, it creates default master node as well. Returns zero in
247 * case of success and a negative error code in case of failure.
248 */
249int ubifs_read_master(struct ubifs_info *c)
250{
251 int err, old_leb_cnt;
252
253 c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
254 if (!c->mst_node)
255 return -ENOMEM;
256
257 err = scan_for_master(c);
258 if (err) {
259 err = ubifs_recover_master_node(c);
260 if (err)
261 /*
262 * Note, we do not free 'c->mst_node' here because the
263 * unmount routine will take care of this.
264 */
265 return err;
266 }
267
268 /* Make sure that the recovery flag is clear */
269 c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
270
271 c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum);
272 c->highest_inum = le64_to_cpu(c->mst_node->highest_inum);
273 c->cmt_no = le64_to_cpu(c->mst_node->cmt_no);
274 c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum);
275 c->zroot.offs = le32_to_cpu(c->mst_node->root_offs);
276 c->zroot.len = le32_to_cpu(c->mst_node->root_len);
277 c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum);
278 c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum);
279 c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum);
280 c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs);
281 c->old_idx_sz = le64_to_cpu(c->mst_node->index_size);
282 c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum);
283 c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs);
284 c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum);
285 c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs);
286 c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum);
287 c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs);
288 c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum);
289 c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs);
290 c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum);
291 c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs);
292 c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs);
293 old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt);
294 c->lst.total_free = le64_to_cpu(c->mst_node->total_free);
295 c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
296 c->lst.total_used = le64_to_cpu(c->mst_node->total_used);
297 c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead);
298 c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark);
299
300 c->calc_idx_sz = c->old_idx_sz;
301
302 if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
303 c->no_orphs = 1;
304
305 if (old_leb_cnt != c->leb_cnt) {
306 /* The file system has been resized */
307 int growth = c->leb_cnt - old_leb_cnt;
308
309 if (c->leb_cnt < old_leb_cnt ||
310 c->leb_cnt < UBIFS_MIN_LEB_CNT) {
311 ubifs_err("bad leb_cnt on master node");
312 dbg_dump_node(c, c->mst_node);
313 return -EINVAL;
314 }
315
316 dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
317 old_leb_cnt, c->leb_cnt);
318 c->lst.empty_lebs += growth;
319 c->lst.total_free += growth * (long long)c->leb_size;
320 c->lst.total_dark += growth * (long long)c->dark_wm;
321
322 /*
323 * Reflect changes back onto the master node. N.B. the master
324 * node gets written immediately whenever mounting (or
325 * remounting) in read-write mode, so we do not need to write it
326 * here.
327 */
328 c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
329 c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
330 c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
331 c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
332 }
333
334 err = validate_master(c);
335 if (err)
336 return err;
337
338 err = dbg_old_index_check_init(c, &c->zroot);
339
340 return err;
341}
342
343/**
344 * ubifs_write_master - write master node.
345 * @c: UBIFS file-system description object
346 *
347 * This function writes the master node. The caller has to take the
348 * @c->mst_mutex lock before calling this function. Returns zero in case of
349 * success and a negative error code in case of failure. The master node is
350 * written twice to enable recovery.
351 */
352int ubifs_write_master(struct ubifs_info *c)
353{
354 int err, lnum, offs, len;
355
356 if (c->ro_media)
357 return -EINVAL;
358
359 lnum = UBIFS_MST_LNUM;
360 offs = c->mst_offs + c->mst_node_alsz;
361 len = UBIFS_MST_NODE_SZ;
362
363 if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
364 err = ubifs_leb_unmap(c, lnum);
365 if (err)
366 return err;
367 offs = 0;
368 }
369
370 c->mst_offs = offs;
371 c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
372
373 err = ubifs_write_node(c, c->mst_node, len, lnum, offs, UBI_SHORTTERM);
374 if (err)
375 return err;
376
377 lnum += 1;
378
379 if (offs == 0) {
380 err = ubifs_leb_unmap(c, lnum);
381 if (err)
382 return err;
383 }
384 err = ubifs_write_node(c, c->mst_node, len, lnum, offs, UBI_SHORTTERM);
385
386 return err;
387}
diff --git a/fs/ubifs/misc.h b/fs/ubifs/misc.h
new file mode 100644
index 000000000000..4beccfc256d2
--- /dev/null
+++ b/fs/ubifs/misc.h
@@ -0,0 +1,342 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file contains miscellaneous helper functions.
25 */
26
27#ifndef __UBIFS_MISC_H__
28#define __UBIFS_MISC_H__
29
30/**
31 * ubifs_zn_dirty - check if znode is dirty.
32 * @znode: znode to check
33 *
34 * This helper function returns %1 if @znode is dirty and %0 otherwise.
35 */
36static inline int ubifs_zn_dirty(const struct ubifs_znode *znode)
37{
38 return !!test_bit(DIRTY_ZNODE, &znode->flags);
39}
40
41/**
42 * ubifs_wake_up_bgt - wake up background thread.
43 * @c: UBIFS file-system description object
44 */
45static inline void ubifs_wake_up_bgt(struct ubifs_info *c)
46{
47 if (c->bgt && !c->need_bgt) {
48 c->need_bgt = 1;
49 wake_up_process(c->bgt);
50 }
51}
52
53/**
54 * ubifs_tnc_find_child - find next child in znode.
55 * @znode: znode to search at
56 * @start: the zbranch index to start at
57 *
58 * This helper function looks for znode child starting at index @start. Returns
59 * the child or %NULL if no children were found.
60 */
61static inline struct ubifs_znode *
62ubifs_tnc_find_child(struct ubifs_znode *znode, int start)
63{
64 while (start < znode->child_cnt) {
65 if (znode->zbranch[start].znode)
66 return znode->zbranch[start].znode;
67 start += 1;
68 }
69
70 return NULL;
71}
72
73/**
74 * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object.
75 * @inode: the VFS 'struct inode' pointer
76 */
77static inline struct ubifs_inode *ubifs_inode(const struct inode *inode)
78{
79 return container_of(inode, struct ubifs_inode, vfs_inode);
80}
81
82/**
83 * ubifs_ro_mode - switch UBIFS to read read-only mode.
84 * @c: UBIFS file-system description object
85 * @err: error code which is the reason of switching to R/O mode
86 */
87static inline void ubifs_ro_mode(struct ubifs_info *c, int err)
88{
89 if (!c->ro_media) {
90 c->ro_media = 1;
91 ubifs_warn("switched to read-only mode, error %d", err);
92 dbg_dump_stack();
93 }
94}
95
96/**
97 * ubifs_compr_present - check if compressor was compiled in.
98 * @compr_type: compressor type to check
99 *
100 * This function returns %1 of compressor of type @compr_type is present, and
101 * %0 if not.
102 */
103static inline int ubifs_compr_present(int compr_type)
104{
105 ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
106 return !!ubifs_compressors[compr_type]->capi_name;
107}
108
109/**
110 * ubifs_compr_name - get compressor name string by its type.
111 * @compr_type: compressor type
112 *
113 * This function returns compressor type string.
114 */
115static inline const char *ubifs_compr_name(int compr_type)
116{
117 ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT);
118 return ubifs_compressors[compr_type]->name;
119}
120
121/**
122 * ubifs_wbuf_sync - synchronize write-buffer.
123 * @wbuf: write-buffer to synchronize
124 *
125 * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume
126 * that the write-buffer is already locked.
127 */
128static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf)
129{
130 int err;
131
132 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
133 err = ubifs_wbuf_sync_nolock(wbuf);
134 mutex_unlock(&wbuf->io_mutex);
135 return err;
136}
137
138/**
139 * ubifs_leb_unmap - unmap an LEB.
140 * @c: UBIFS file-system description object
141 * @lnum: LEB number to unmap
142 *
143 * This function returns %0 on success and a negative error code on failure.
144 */
145static inline int ubifs_leb_unmap(const struct ubifs_info *c, int lnum)
146{
147 int err;
148
149 if (c->ro_media)
150 return -EROFS;
151 err = ubi_leb_unmap(c->ubi, lnum);
152 if (err) {
153 ubifs_err("unmap LEB %d failed, error %d", lnum, err);
154 return err;
155 }
156
157 return 0;
158}
159
160/**
161 * ubifs_leb_write - write to a LEB.
162 * @c: UBIFS file-system description object
163 * @lnum: LEB number to write
164 * @buf: buffer to write from
165 * @offs: offset within LEB to write to
166 * @len: length to write
167 * @dtype: data type
168 *
169 * This function returns %0 on success and a negative error code on failure.
170 */
171static inline int ubifs_leb_write(const struct ubifs_info *c, int lnum,
172 const void *buf, int offs, int len, int dtype)
173{
174 int err;
175
176 if (c->ro_media)
177 return -EROFS;
178 err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype);
179 if (err) {
180 ubifs_err("writing %d bytes at %d:%d, error %d",
181 len, lnum, offs, err);
182 return err;
183 }
184
185 return 0;
186}
187
188/**
189 * ubifs_leb_change - atomic LEB change.
190 * @c: UBIFS file-system description object
191 * @lnum: LEB number to write
192 * @buf: buffer to write from
193 * @len: length to write
194 * @dtype: data type
195 *
196 * This function returns %0 on success and a negative error code on failure.
197 */
198static inline int ubifs_leb_change(const struct ubifs_info *c, int lnum,
199 const void *buf, int len, int dtype)
200{
201 int err;
202
203 if (c->ro_media)
204 return -EROFS;
205 err = ubi_leb_change(c->ubi, lnum, buf, len, dtype);
206 if (err) {
207 ubifs_err("changing %d bytes in LEB %d, error %d",
208 len, lnum, err);
209 return err;
210 }
211
212 return 0;
213}
214
215/**
216 * ubifs_encode_dev - encode device node IDs.
217 * @dev: UBIFS device node information
218 * @rdev: device IDs to encode
219 *
220 * This is a helper function which encodes major/minor numbers of a device node
221 * into UBIFS device node description. We use standard Linux "new" and "huge"
222 * encodings.
223 */
224static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev)
225{
226 if (new_valid_dev(rdev)) {
227 dev->new = cpu_to_le32(new_encode_dev(rdev));
228 return sizeof(dev->new);
229 } else {
230 dev->huge = cpu_to_le64(huge_encode_dev(rdev));
231 return sizeof(dev->huge);
232 }
233}
234
235/**
236 * ubifs_add_dirt - add dirty space to LEB properties.
237 * @c: the UBIFS file-system description object
238 * @lnum: LEB to add dirty space for
239 * @dirty: dirty space to add
240 *
241 * This is a helper function which increased amount of dirty LEB space. Returns
242 * zero in case of success and a negative error code in case of failure.
243 */
244static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty)
245{
246 return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0);
247}
248
249/**
250 * ubifs_return_leb - return LEB to lprops.
251 * @c: the UBIFS file-system description object
252 * @lnum: LEB to return
253 *
254 * This helper function cleans the "taken" flag of a logical eraseblock in the
255 * lprops. Returns zero in case of success and a negative error code in case of
256 * failure.
257 */
258static inline int ubifs_return_leb(struct ubifs_info *c, int lnum)
259{
260 return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
261 LPROPS_TAKEN, 0);
262}
263
264/**
265 * ubifs_idx_node_sz - return index node size.
266 * @c: the UBIFS file-system description object
267 * @child_cnt: number of children of this index node
268 */
269static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt)
270{
271 return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len) * child_cnt;
272}
273
274/**
275 * ubifs_idx_branch - return pointer to an index branch.
276 * @c: the UBIFS file-system description object
277 * @idx: index node
278 * @bnum: branch number
279 */
280static inline
281struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
282 const struct ubifs_idx_node *idx,
283 int bnum)
284{
285 return (struct ubifs_branch *)((void *)idx->branches +
286 (UBIFS_BRANCH_SZ + c->key_len) * bnum);
287}
288
289/**
290 * ubifs_idx_key - return pointer to an index key.
291 * @c: the UBIFS file-system description object
292 * @idx: index node
293 */
294static inline void *ubifs_idx_key(const struct ubifs_info *c,
295 const struct ubifs_idx_node *idx)
296{
297 return (void *)((struct ubifs_branch *)idx->branches)->key;
298}
299
300/**
301 * ubifs_reported_space - calculate reported free space.
302 * @c: the UBIFS file-system description object
303 * @free: amount of free space
304 *
305 * This function calculates amount of free space which will be reported to
306 * user-space. User-space application tend to expect that if the file-system
307 * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
308 * are able to write a file of size N. UBIFS attaches node headers to each data
309 * node and it has to write indexind nodes as well. This introduces additional
310 * overhead, and UBIFS it has to report sligtly less free space to meet the
311 * above expectetion.
312 *
313 * This function assumes free space is made up of uncompressed data nodes and
314 * full index nodes (one per data node, doubled because we always allow enough
315 * space to write the index twice).
316 *
317 * Note, the calculation is pessimistic, which means that most of the time
318 * UBIFS reports less space than it actually has.
319 */
320static inline long long ubifs_reported_space(const struct ubifs_info *c,
321 uint64_t free)
322{
323 int divisor, factor;
324
325 divisor = UBIFS_MAX_DATA_NODE_SZ + (c->max_idx_node_sz << 1);
326 factor = UBIFS_MAX_DATA_NODE_SZ - UBIFS_DATA_NODE_SZ;
327 do_div(free, divisor);
328
329 return free * factor;
330}
331
332/**
333 * ubifs_current_time - round current time to time granularity.
334 * @inode: inode
335 */
336static inline struct timespec ubifs_current_time(struct inode *inode)
337{
338 return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
339 current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
340}
341
342#endif /* __UBIFS_MISC_H__ */
diff --git a/fs/ubifs/orphan.c b/fs/ubifs/orphan.c
new file mode 100644
index 000000000000..3afeb9242c6a
--- /dev/null
+++ b/fs/ubifs/orphan.c
@@ -0,0 +1,958 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Author: Adrian Hunter
20 */
21
22#include "ubifs.h"
23
24/*
25 * An orphan is an inode number whose inode node has been committed to the index
26 * with a link count of zero. That happens when an open file is deleted
27 * (unlinked) and then a commit is run. In the normal course of events the inode
28 * would be deleted when the file is closed. However in the case of an unclean
29 * unmount, orphans need to be accounted for. After an unclean unmount, the
30 * orphans' inodes must be deleted which means either scanning the entire index
31 * looking for them, or keeping a list on flash somewhere. This unit implements
32 * the latter approach.
33 *
34 * The orphan area is a fixed number of LEBs situated between the LPT area and
35 * the main area. The number of orphan area LEBs is specified when the file
36 * system is created. The minimum number is 1. The size of the orphan area
37 * should be so that it can hold the maximum number of orphans that are expected
38 * to ever exist at one time.
39 *
40 * The number of orphans that can fit in a LEB is:
41 *
42 * (c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64)
43 *
44 * For example: a 15872 byte LEB can fit 1980 orphans so 1 LEB may be enough.
45 *
46 * Orphans are accumulated in a rb-tree. When an inode's link count drops to
47 * zero, the inode number is added to the rb-tree. It is removed from the tree
48 * when the inode is deleted. Any new orphans that are in the orphan tree when
49 * the commit is run, are written to the orphan area in 1 or more orph nodes.
50 * If the orphan area is full, it is consolidated to make space. There is
51 * always enough space because validation prevents the user from creating more
52 * than the maximum number of orphans allowed.
53 */
54
55#ifdef CONFIG_UBIFS_FS_DEBUG
56static int dbg_check_orphans(struct ubifs_info *c);
57#else
58#define dbg_check_orphans(c) 0
59#endif
60
61/**
62 * ubifs_add_orphan - add an orphan.
63 * @c: UBIFS file-system description object
64 * @inum: orphan inode number
65 *
66 * Add an orphan. This function is called when an inodes link count drops to
67 * zero.
68 */
69int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
70{
71 struct ubifs_orphan *orphan, *o;
72 struct rb_node **p, *parent = NULL;
73
74 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
75 if (!orphan)
76 return -ENOMEM;
77 orphan->inum = inum;
78 orphan->new = 1;
79
80 spin_lock(&c->orphan_lock);
81 if (c->tot_orphans >= c->max_orphans) {
82 spin_unlock(&c->orphan_lock);
83 kfree(orphan);
84 return -ENFILE;
85 }
86 p = &c->orph_tree.rb_node;
87 while (*p) {
88 parent = *p;
89 o = rb_entry(parent, struct ubifs_orphan, rb);
90 if (inum < o->inum)
91 p = &(*p)->rb_left;
92 else if (inum > o->inum)
93 p = &(*p)->rb_right;
94 else {
95 dbg_err("orphaned twice");
96 spin_unlock(&c->orphan_lock);
97 kfree(orphan);
98 return 0;
99 }
100 }
101 c->tot_orphans += 1;
102 c->new_orphans += 1;
103 rb_link_node(&orphan->rb, parent, p);
104 rb_insert_color(&orphan->rb, &c->orph_tree);
105 list_add_tail(&orphan->list, &c->orph_list);
106 list_add_tail(&orphan->new_list, &c->orph_new);
107 spin_unlock(&c->orphan_lock);
108 dbg_gen("ino %lu", inum);
109 return 0;
110}
111
112/**
113 * ubifs_delete_orphan - delete an orphan.
114 * @c: UBIFS file-system description object
115 * @inum: orphan inode number
116 *
117 * Delete an orphan. This function is called when an inode is deleted.
118 */
119void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
120{
121 struct ubifs_orphan *o;
122 struct rb_node *p;
123
124 spin_lock(&c->orphan_lock);
125 p = c->orph_tree.rb_node;
126 while (p) {
127 o = rb_entry(p, struct ubifs_orphan, rb);
128 if (inum < o->inum)
129 p = p->rb_left;
130 else if (inum > o->inum)
131 p = p->rb_right;
132 else {
133 if (o->dnext) {
134 spin_unlock(&c->orphan_lock);
135 dbg_gen("deleted twice ino %lu", inum);
136 return;
137 }
138 if (o->cnext) {
139 o->dnext = c->orph_dnext;
140 c->orph_dnext = o;
141 spin_unlock(&c->orphan_lock);
142 dbg_gen("delete later ino %lu", inum);
143 return;
144 }
145 rb_erase(p, &c->orph_tree);
146 list_del(&o->list);
147 c->tot_orphans -= 1;
148 if (o->new) {
149 list_del(&o->new_list);
150 c->new_orphans -= 1;
151 }
152 spin_unlock(&c->orphan_lock);
153 kfree(o);
154 dbg_gen("inum %lu", inum);
155 return;
156 }
157 }
158 spin_unlock(&c->orphan_lock);
159 dbg_err("missing orphan ino %lu", inum);
160 dbg_dump_stack();
161}
162
163/**
164 * ubifs_orphan_start_commit - start commit of orphans.
165 * @c: UBIFS file-system description object
166 *
167 * Start commit of orphans.
168 */
169int ubifs_orphan_start_commit(struct ubifs_info *c)
170{
171 struct ubifs_orphan *orphan, **last;
172
173 spin_lock(&c->orphan_lock);
174 last = &c->orph_cnext;
175 list_for_each_entry(orphan, &c->orph_new, new_list) {
176 ubifs_assert(orphan->new);
177 orphan->new = 0;
178 *last = orphan;
179 last = &orphan->cnext;
180 }
181 *last = orphan->cnext;
182 c->cmt_orphans = c->new_orphans;
183 c->new_orphans = 0;
184 dbg_cmt("%d orphans to commit", c->cmt_orphans);
185 INIT_LIST_HEAD(&c->orph_new);
186 if (c->tot_orphans == 0)
187 c->no_orphs = 1;
188 else
189 c->no_orphs = 0;
190 spin_unlock(&c->orphan_lock);
191 return 0;
192}
193
194/**
195 * avail_orphs - calculate available space.
196 * @c: UBIFS file-system description object
197 *
198 * This function returns the number of orphans that can be written in the
199 * available space.
200 */
201static int avail_orphs(struct ubifs_info *c)
202{
203 int avail_lebs, avail, gap;
204
205 avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
206 avail = avail_lebs *
207 ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
208 gap = c->leb_size - c->ohead_offs;
209 if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
210 avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
211 return avail;
212}
213
214/**
215 * tot_avail_orphs - calculate total space.
216 * @c: UBIFS file-system description object
217 *
218 * This function returns the number of orphans that can be written in half
219 * the total space. That leaves half the space for adding new orphans.
220 */
221static int tot_avail_orphs(struct ubifs_info *c)
222{
223 int avail_lebs, avail;
224
225 avail_lebs = c->orph_lebs;
226 avail = avail_lebs *
227 ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
228 return avail / 2;
229}
230
231/**
232 * do_write_orph_node - write a node
233 * @c: UBIFS file-system description object
234 * @len: length of node
235 * @atomic: write atomically
236 *
237 * This function writes a node to the orphan head from the orphan buffer. If
238 * %atomic is not zero, then the write is done atomically. On success, %0 is
239 * returned, otherwise a negative error code is returned.
240 */
241static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
242{
243 int err = 0;
244
245 if (atomic) {
246 ubifs_assert(c->ohead_offs == 0);
247 ubifs_prepare_node(c, c->orph_buf, len, 1);
248 len = ALIGN(len, c->min_io_size);
249 err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len,
250 UBI_SHORTTERM);
251 } else {
252 if (c->ohead_offs == 0) {
253 /* Ensure LEB has been unmapped */
254 err = ubifs_leb_unmap(c, c->ohead_lnum);
255 if (err)
256 return err;
257 }
258 err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
259 c->ohead_offs, UBI_SHORTTERM);
260 }
261 return err;
262}
263
264/**
265 * write_orph_node - write an orph node
266 * @c: UBIFS file-system description object
267 * @atomic: write atomically
268 *
269 * This function builds an orph node from the cnext list and writes it to the
270 * orphan head. On success, %0 is returned, otherwise a negative error code
271 * is returned.
272 */
273static int write_orph_node(struct ubifs_info *c, int atomic)
274{
275 struct ubifs_orphan *orphan, *cnext;
276 struct ubifs_orph_node *orph;
277 int gap, err, len, cnt, i;
278
279 ubifs_assert(c->cmt_orphans > 0);
280 gap = c->leb_size - c->ohead_offs;
281 if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
282 c->ohead_lnum += 1;
283 c->ohead_offs = 0;
284 gap = c->leb_size;
285 if (c->ohead_lnum > c->orph_last) {
286 /*
287 * We limit the number of orphans so that this should
288 * never happen.
289 */
290 ubifs_err("out of space in orphan area");
291 return -EINVAL;
292 }
293 }
294 cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
295 if (cnt > c->cmt_orphans)
296 cnt = c->cmt_orphans;
297 len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
298 ubifs_assert(c->orph_buf);
299 orph = c->orph_buf;
300 orph->ch.node_type = UBIFS_ORPH_NODE;
301 spin_lock(&c->orphan_lock);
302 cnext = c->orph_cnext;
303 for (i = 0; i < cnt; i++) {
304 orphan = cnext;
305 orph->inos[i] = cpu_to_le64(orphan->inum);
306 cnext = orphan->cnext;
307 orphan->cnext = NULL;
308 }
309 c->orph_cnext = cnext;
310 c->cmt_orphans -= cnt;
311 spin_unlock(&c->orphan_lock);
312 if (c->cmt_orphans)
313 orph->cmt_no = cpu_to_le64(c->cmt_no + 1);
314 else
315 /* Mark the last node of the commit */
316 orph->cmt_no = cpu_to_le64((c->cmt_no + 1) | (1ULL << 63));
317 ubifs_assert(c->ohead_offs + len <= c->leb_size);
318 ubifs_assert(c->ohead_lnum >= c->orph_first);
319 ubifs_assert(c->ohead_lnum <= c->orph_last);
320 err = do_write_orph_node(c, len, atomic);
321 c->ohead_offs += ALIGN(len, c->min_io_size);
322 c->ohead_offs = ALIGN(c->ohead_offs, 8);
323 return err;
324}
325
326/**
327 * write_orph_nodes - write orph nodes until there are no more to commit
328 * @c: UBIFS file-system description object
329 * @atomic: write atomically
330 *
331 * This function writes orph nodes for all the orphans to commit. On success,
332 * %0 is returned, otherwise a negative error code is returned.
333 */
334static int write_orph_nodes(struct ubifs_info *c, int atomic)
335{
336 int err;
337
338 while (c->cmt_orphans > 0) {
339 err = write_orph_node(c, atomic);
340 if (err)
341 return err;
342 }
343 if (atomic) {
344 int lnum;
345
346 /* Unmap any unused LEBs after consolidation */
347 lnum = c->ohead_lnum + 1;
348 for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
349 err = ubifs_leb_unmap(c, lnum);
350 if (err)
351 return err;
352 }
353 }
354 return 0;
355}
356
357/**
358 * consolidate - consolidate the orphan area.
359 * @c: UBIFS file-system description object
360 *
361 * This function enables consolidation by putting all the orphans into the list
362 * to commit. The list is in the order that the orphans were added, and the
363 * LEBs are written atomically in order, so at no time can orphans be lost by
364 * an unclean unmount.
365 *
366 * This function returns %0 on success and a negative error code on failure.
367 */
368static int consolidate(struct ubifs_info *c)
369{
370 int tot_avail = tot_avail_orphs(c), err = 0;
371
372 spin_lock(&c->orphan_lock);
373 dbg_cmt("there is space for %d orphans and there are %d",
374 tot_avail, c->tot_orphans);
375 if (c->tot_orphans - c->new_orphans <= tot_avail) {
376 struct ubifs_orphan *orphan, **last;
377 int cnt = 0;
378
379 /* Change the cnext list to include all non-new orphans */
380 last = &c->orph_cnext;
381 list_for_each_entry(orphan, &c->orph_list, list) {
382 if (orphan->new)
383 continue;
384 *last = orphan;
385 last = &orphan->cnext;
386 cnt += 1;
387 }
388 *last = orphan->cnext;
389 ubifs_assert(cnt == c->tot_orphans - c->new_orphans);
390 c->cmt_orphans = cnt;
391 c->ohead_lnum = c->orph_first;
392 c->ohead_offs = 0;
393 } else {
394 /*
395 * We limit the number of orphans so that this should
396 * never happen.
397 */
398 ubifs_err("out of space in orphan area");
399 err = -EINVAL;
400 }
401 spin_unlock(&c->orphan_lock);
402 return err;
403}
404
405/**
406 * commit_orphans - commit orphans.
407 * @c: UBIFS file-system description object
408 *
409 * This function commits orphans to flash. On success, %0 is returned,
410 * otherwise a negative error code is returned.
411 */
412static int commit_orphans(struct ubifs_info *c)
413{
414 int avail, atomic = 0, err;
415
416 ubifs_assert(c->cmt_orphans > 0);
417 avail = avail_orphs(c);
418 if (avail < c->cmt_orphans) {
419 /* Not enough space to write new orphans, so consolidate */
420 err = consolidate(c);
421 if (err)
422 return err;
423 atomic = 1;
424 }
425 err = write_orph_nodes(c, atomic);
426 return err;
427}
428
429/**
430 * erase_deleted - erase the orphans marked for deletion.
431 * @c: UBIFS file-system description object
432 *
433 * During commit, the orphans being committed cannot be deleted, so they are
434 * marked for deletion and deleted by this function. Also, the recovery
435 * adds killed orphans to the deletion list, and therefore they are deleted
436 * here too.
437 */
438static void erase_deleted(struct ubifs_info *c)
439{
440 struct ubifs_orphan *orphan, *dnext;
441
442 spin_lock(&c->orphan_lock);
443 dnext = c->orph_dnext;
444 while (dnext) {
445 orphan = dnext;
446 dnext = orphan->dnext;
447 ubifs_assert(!orphan->new);
448 rb_erase(&orphan->rb, &c->orph_tree);
449 list_del(&orphan->list);
450 c->tot_orphans -= 1;
451 dbg_gen("deleting orphan ino %lu", orphan->inum);
452 kfree(orphan);
453 }
454 c->orph_dnext = NULL;
455 spin_unlock(&c->orphan_lock);
456}
457
458/**
459 * ubifs_orphan_end_commit - end commit of orphans.
460 * @c: UBIFS file-system description object
461 *
462 * End commit of orphans.
463 */
464int ubifs_orphan_end_commit(struct ubifs_info *c)
465{
466 int err;
467
468 if (c->cmt_orphans != 0) {
469 err = commit_orphans(c);
470 if (err)
471 return err;
472 }
473 erase_deleted(c);
474 err = dbg_check_orphans(c);
475 return err;
476}
477
478/**
479 * clear_orphans - erase all LEBs used for orphans.
480 * @c: UBIFS file-system description object
481 *
482 * If recovery is not required, then the orphans from the previous session
483 * are not needed. This function locates the LEBs used to record
484 * orphans, and un-maps them.
485 */
486static int clear_orphans(struct ubifs_info *c)
487{
488 int lnum, err;
489
490 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
491 err = ubifs_leb_unmap(c, lnum);
492 if (err)
493 return err;
494 }
495 c->ohead_lnum = c->orph_first;
496 c->ohead_offs = 0;
497 return 0;
498}
499
500/**
501 * insert_dead_orphan - insert an orphan.
502 * @c: UBIFS file-system description object
503 * @inum: orphan inode number
504 *
505 * This function is a helper to the 'do_kill_orphans()' function. The orphan
506 * must be kept until the next commit, so it is added to the rb-tree and the
507 * deletion list.
508 */
509static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
510{
511 struct ubifs_orphan *orphan, *o;
512 struct rb_node **p, *parent = NULL;
513
514 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
515 if (!orphan)
516 return -ENOMEM;
517 orphan->inum = inum;
518
519 p = &c->orph_tree.rb_node;
520 while (*p) {
521 parent = *p;
522 o = rb_entry(parent, struct ubifs_orphan, rb);
523 if (inum < o->inum)
524 p = &(*p)->rb_left;
525 else if (inum > o->inum)
526 p = &(*p)->rb_right;
527 else {
528 /* Already added - no problem */
529 kfree(orphan);
530 return 0;
531 }
532 }
533 c->tot_orphans += 1;
534 rb_link_node(&orphan->rb, parent, p);
535 rb_insert_color(&orphan->rb, &c->orph_tree);
536 list_add_tail(&orphan->list, &c->orph_list);
537 orphan->dnext = c->orph_dnext;
538 c->orph_dnext = orphan;
539 dbg_mnt("ino %lu, new %d, tot %d",
540 inum, c->new_orphans, c->tot_orphans);
541 return 0;
542}
543
544/**
545 * do_kill_orphans - remove orphan inodes from the index.
546 * @c: UBIFS file-system description object
547 * @sleb: scanned LEB
548 * @last_cmt_no: cmt_no of last orph node read is passed and returned here
549 * @outofdate: whether the LEB is out of date is returned here
550 * @last_flagged: whether the end orph node is encountered
551 *
552 * This function is a helper to the 'kill_orphans()' function. It goes through
553 * every orphan node in a LEB and for every inode number recorded, removes
554 * all keys for that inode from the TNC.
555 */
556static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
557 unsigned long long *last_cmt_no, int *outofdate,
558 int *last_flagged)
559{
560 struct ubifs_scan_node *snod;
561 struct ubifs_orph_node *orph;
562 unsigned long long cmt_no;
563 ino_t inum;
564 int i, n, err, first = 1;
565
566 list_for_each_entry(snod, &sleb->nodes, list) {
567 if (snod->type != UBIFS_ORPH_NODE) {
568 ubifs_err("invalid node type %d in orphan area at "
569 "%d:%d", snod->type, sleb->lnum, snod->offs);
570 dbg_dump_node(c, snod->node);
571 return -EINVAL;
572 }
573
574 orph = snod->node;
575
576 /* Check commit number */
577 cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
578 /*
579 * The commit number on the master node may be less, because
580 * of a failed commit. If there are several failed commits in a
581 * row, the commit number written on orph nodes will continue to
582 * increase (because the commit number is adjusted here) even
583 * though the commit number on the master node stays the same
584 * because the master node has not been re-written.
585 */
586 if (cmt_no > c->cmt_no)
587 c->cmt_no = cmt_no;
588 if (cmt_no < *last_cmt_no && *last_flagged) {
589 /*
590 * The last orph node had a higher commit number and was
591 * flagged as the last written for that commit number.
592 * That makes this orph node, out of date.
593 */
594 if (!first) {
595 ubifs_err("out of order commit number %llu in "
596 "orphan node at %d:%d",
597 cmt_no, sleb->lnum, snod->offs);
598 dbg_dump_node(c, snod->node);
599 return -EINVAL;
600 }
601 dbg_rcvry("out of date LEB %d", sleb->lnum);
602 *outofdate = 1;
603 return 0;
604 }
605
606 if (first)
607 first = 0;
608
609 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
610 for (i = 0; i < n; i++) {
611 inum = le64_to_cpu(orph->inos[i]);
612 dbg_rcvry("deleting orphaned inode %lu", inum);
613 err = ubifs_tnc_remove_ino(c, inum);
614 if (err)
615 return err;
616 err = insert_dead_orphan(c, inum);
617 if (err)
618 return err;
619 }
620
621 *last_cmt_no = cmt_no;
622 if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
623 dbg_rcvry("last orph node for commit %llu at %d:%d",
624 cmt_no, sleb->lnum, snod->offs);
625 *last_flagged = 1;
626 } else
627 *last_flagged = 0;
628 }
629
630 return 0;
631}
632
633/**
634 * kill_orphans - remove all orphan inodes from the index.
635 * @c: UBIFS file-system description object
636 *
637 * If recovery is required, then orphan inodes recorded during the previous
638 * session (which ended with an unclean unmount) must be deleted from the index.
639 * This is done by updating the TNC, but since the index is not updated until
640 * the next commit, the LEBs where the orphan information is recorded are not
641 * erased until the next commit.
642 */
643static int kill_orphans(struct ubifs_info *c)
644{
645 unsigned long long last_cmt_no = 0;
646 int lnum, err = 0, outofdate = 0, last_flagged = 0;
647
648 c->ohead_lnum = c->orph_first;
649 c->ohead_offs = 0;
650 /* Check no-orphans flag and skip this if no orphans */
651 if (c->no_orphs) {
652 dbg_rcvry("no orphans");
653 return 0;
654 }
655 /*
656 * Orph nodes always start at c->orph_first and are written to each
657 * successive LEB in turn. Generally unused LEBs will have been unmapped
658 * but may contain out of date orph nodes if the unmap didn't go
659 * through. In addition, the last orph node written for each commit is
660 * marked (top bit of orph->cmt_no is set to 1). It is possible that
661 * there are orph nodes from the next commit (i.e. the commit did not
662 * complete successfully). In that case, no orphans will have been lost
663 * due to the way that orphans are written, and any orphans added will
664 * be valid orphans anyway and so can be deleted.
665 */
666 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
667 struct ubifs_scan_leb *sleb;
668
669 dbg_rcvry("LEB %d", lnum);
670 sleb = ubifs_scan(c, lnum, 0, c->sbuf);
671 if (IS_ERR(sleb)) {
672 sleb = ubifs_recover_leb(c, lnum, 0, c->sbuf, 0);
673 if (IS_ERR(sleb)) {
674 err = PTR_ERR(sleb);
675 break;
676 }
677 }
678 err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
679 &last_flagged);
680 if (err || outofdate) {
681 ubifs_scan_destroy(sleb);
682 break;
683 }
684 if (sleb->endpt) {
685 c->ohead_lnum = lnum;
686 c->ohead_offs = sleb->endpt;
687 }
688 ubifs_scan_destroy(sleb);
689 }
690 return err;
691}
692
693/**
694 * ubifs_mount_orphans - delete orphan inodes and erase LEBs that recorded them.
695 * @c: UBIFS file-system description object
696 * @unclean: indicates recovery from unclean unmount
697 * @read_only: indicates read only mount
698 *
699 * This function is called when mounting to erase orphans from the previous
700 * session. If UBIFS was not unmounted cleanly, then the inodes recorded as
701 * orphans are deleted.
702 */
703int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
704{
705 int err = 0;
706
707 c->max_orphans = tot_avail_orphs(c);
708
709 if (!read_only) {
710 c->orph_buf = vmalloc(c->leb_size);
711 if (!c->orph_buf)
712 return -ENOMEM;
713 }
714
715 if (unclean)
716 err = kill_orphans(c);
717 else if (!read_only)
718 err = clear_orphans(c);
719
720 return err;
721}
722
723#ifdef CONFIG_UBIFS_FS_DEBUG
724
725struct check_orphan {
726 struct rb_node rb;
727 ino_t inum;
728};
729
730struct check_info {
731 unsigned long last_ino;
732 unsigned long tot_inos;
733 unsigned long missing;
734 unsigned long long leaf_cnt;
735 struct ubifs_ino_node *node;
736 struct rb_root root;
737};
738
739static int dbg_find_orphan(struct ubifs_info *c, ino_t inum)
740{
741 struct ubifs_orphan *o;
742 struct rb_node *p;
743
744 spin_lock(&c->orphan_lock);
745 p = c->orph_tree.rb_node;
746 while (p) {
747 o = rb_entry(p, struct ubifs_orphan, rb);
748 if (inum < o->inum)
749 p = p->rb_left;
750 else if (inum > o->inum)
751 p = p->rb_right;
752 else {
753 spin_unlock(&c->orphan_lock);
754 return 1;
755 }
756 }
757 spin_unlock(&c->orphan_lock);
758 return 0;
759}
760
761static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
762{
763 struct check_orphan *orphan, *o;
764 struct rb_node **p, *parent = NULL;
765
766 orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
767 if (!orphan)
768 return -ENOMEM;
769 orphan->inum = inum;
770
771 p = &root->rb_node;
772 while (*p) {
773 parent = *p;
774 o = rb_entry(parent, struct check_orphan, rb);
775 if (inum < o->inum)
776 p = &(*p)->rb_left;
777 else if (inum > o->inum)
778 p = &(*p)->rb_right;
779 else {
780 kfree(orphan);
781 return 0;
782 }
783 }
784 rb_link_node(&orphan->rb, parent, p);
785 rb_insert_color(&orphan->rb, root);
786 return 0;
787}
788
789static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
790{
791 struct check_orphan *o;
792 struct rb_node *p;
793
794 p = root->rb_node;
795 while (p) {
796 o = rb_entry(p, struct check_orphan, rb);
797 if (inum < o->inum)
798 p = p->rb_left;
799 else if (inum > o->inum)
800 p = p->rb_right;
801 else
802 return 1;
803 }
804 return 0;
805}
806
807static void dbg_free_check_tree(struct rb_root *root)
808{
809 struct rb_node *this = root->rb_node;
810 struct check_orphan *o;
811
812 while (this) {
813 if (this->rb_left) {
814 this = this->rb_left;
815 continue;
816 } else if (this->rb_right) {
817 this = this->rb_right;
818 continue;
819 }
820 o = rb_entry(this, struct check_orphan, rb);
821 this = rb_parent(this);
822 if (this) {
823 if (this->rb_left == &o->rb)
824 this->rb_left = NULL;
825 else
826 this->rb_right = NULL;
827 }
828 kfree(o);
829 }
830}
831
832static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
833 void *priv)
834{
835 struct check_info *ci = priv;
836 ino_t inum;
837 int err;
838
839 inum = key_inum(c, &zbr->key);
840 if (inum != ci->last_ino) {
841 /* Lowest node type is the inode node, so it comes first */
842 if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
843 ubifs_err("found orphan node ino %lu, type %d", inum,
844 key_type(c, &zbr->key));
845 ci->last_ino = inum;
846 ci->tot_inos += 1;
847 err = ubifs_tnc_read_node(c, zbr, ci->node);
848 if (err) {
849 ubifs_err("node read failed, error %d", err);
850 return err;
851 }
852 if (ci->node->nlink == 0)
853 /* Must be recorded as an orphan */
854 if (!dbg_find_check_orphan(&ci->root, inum) &&
855 !dbg_find_orphan(c, inum)) {
856 ubifs_err("missing orphan, ino %lu", inum);
857 ci->missing += 1;
858 }
859 }
860 ci->leaf_cnt += 1;
861 return 0;
862}
863
864static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
865{
866 struct ubifs_scan_node *snod;
867 struct ubifs_orph_node *orph;
868 ino_t inum;
869 int i, n, err;
870
871 list_for_each_entry(snod, &sleb->nodes, list) {
872 cond_resched();
873 if (snod->type != UBIFS_ORPH_NODE)
874 continue;
875 orph = snod->node;
876 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
877 for (i = 0; i < n; i++) {
878 inum = le64_to_cpu(orph->inos[i]);
879 err = dbg_ins_check_orphan(&ci->root, inum);
880 if (err)
881 return err;
882 }
883 }
884 return 0;
885}
886
887static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
888{
889 int lnum, err = 0;
890
891 /* Check no-orphans flag and skip this if no orphans */
892 if (c->no_orphs)
893 return 0;
894
895 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
896 struct ubifs_scan_leb *sleb;
897
898 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
899 if (IS_ERR(sleb)) {
900 err = PTR_ERR(sleb);
901 break;
902 }
903
904 err = dbg_read_orphans(ci, sleb);
905 ubifs_scan_destroy(sleb);
906 if (err)
907 break;
908 }
909
910 return err;
911}
912
913static int dbg_check_orphans(struct ubifs_info *c)
914{
915 struct check_info ci;
916 int err;
917
918 if (!(ubifs_chk_flags & UBIFS_CHK_ORPH))
919 return 0;
920
921 ci.last_ino = 0;
922 ci.tot_inos = 0;
923 ci.missing = 0;
924 ci.leaf_cnt = 0;
925 ci.root = RB_ROOT;
926 ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
927 if (!ci.node) {
928 ubifs_err("out of memory");
929 return -ENOMEM;
930 }
931
932 err = dbg_scan_orphans(c, &ci);
933 if (err)
934 goto out;
935
936 err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
937 if (err) {
938 ubifs_err("cannot scan TNC, error %d", err);
939 goto out;
940 }
941
942 if (ci.missing) {
943 ubifs_err("%lu missing orphan(s)", ci.missing);
944 err = -EINVAL;
945 goto out;
946 }
947
948 dbg_cmt("last inode number is %lu", ci.last_ino);
949 dbg_cmt("total number of inodes is %lu", ci.tot_inos);
950 dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
951
952out:
953 dbg_free_check_tree(&ci.root);
954 kfree(ci.node);
955 return err;
956}
957
958#endif /* CONFIG_UBIFS_FS_DEBUG */
diff --git a/fs/ubifs/recovery.c b/fs/ubifs/recovery.c
new file mode 100644
index 000000000000..77d26c141cf6
--- /dev/null
+++ b/fs/ubifs/recovery.c
@@ -0,0 +1,1519 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements functions needed to recover from unclean un-mounts.
25 * When UBIFS is mounted, it checks a flag on the master node to determine if
26 * an un-mount was completed sucessfully. If not, the process of mounting
27 * incorparates additional checking and fixing of on-flash data structures.
28 * UBIFS always cleans away all remnants of an unclean un-mount, so that
29 * errors do not accumulate. However UBIFS defers recovery if it is mounted
30 * read-only, and the flash is not modified in that case.
31 */
32
33#include <linux/crc32.h>
34#include "ubifs.h"
35
36/**
37 * is_empty - determine whether a buffer is empty (contains all 0xff).
38 * @buf: buffer to clean
39 * @len: length of buffer
40 *
41 * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
42 * %0 is returned.
43 */
44static int is_empty(void *buf, int len)
45{
46 uint8_t *p = buf;
47 int i;
48
49 for (i = 0; i < len; i++)
50 if (*p++ != 0xff)
51 return 0;
52 return 1;
53}
54
55/**
56 * get_master_node - get the last valid master node allowing for corruption.
57 * @c: UBIFS file-system description object
58 * @lnum: LEB number
59 * @pbuf: buffer containing the LEB read, is returned here
60 * @mst: master node, if found, is returned here
61 * @cor: corruption, if found, is returned here
62 *
63 * This function allocates a buffer, reads the LEB into it, and finds and
64 * returns the last valid master node allowing for one area of corruption.
65 * The corrupt area, if there is one, must be consistent with the assumption
66 * that it is the result of an unclean unmount while the master node was being
67 * written. Under those circumstances, it is valid to use the previously written
68 * master node.
69 *
70 * This function returns %0 on success and a negative error code on failure.
71 */
72static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
73 struct ubifs_mst_node **mst, void **cor)
74{
75 const int sz = c->mst_node_alsz;
76 int err, offs, len;
77 void *sbuf, *buf;
78
79 sbuf = vmalloc(c->leb_size);
80 if (!sbuf)
81 return -ENOMEM;
82
83 err = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
84 if (err && err != -EBADMSG)
85 goto out_free;
86
87 /* Find the first position that is definitely not a node */
88 offs = 0;
89 buf = sbuf;
90 len = c->leb_size;
91 while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
92 struct ubifs_ch *ch = buf;
93
94 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
95 break;
96 offs += sz;
97 buf += sz;
98 len -= sz;
99 }
100 /* See if there was a valid master node before that */
101 if (offs) {
102 int ret;
103
104 offs -= sz;
105 buf -= sz;
106 len += sz;
107 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
108 if (ret != SCANNED_A_NODE && offs) {
109 /* Could have been corruption so check one place back */
110 offs -= sz;
111 buf -= sz;
112 len += sz;
113 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
114 if (ret != SCANNED_A_NODE)
115 /*
116 * We accept only one area of corruption because
117 * we are assuming that it was caused while
118 * trying to write a master node.
119 */
120 goto out_err;
121 }
122 if (ret == SCANNED_A_NODE) {
123 struct ubifs_ch *ch = buf;
124
125 if (ch->node_type != UBIFS_MST_NODE)
126 goto out_err;
127 dbg_rcvry("found a master node at %d:%d", lnum, offs);
128 *mst = buf;
129 offs += sz;
130 buf += sz;
131 len -= sz;
132 }
133 }
134 /* Check for corruption */
135 if (offs < c->leb_size) {
136 if (!is_empty(buf, min_t(int, len, sz))) {
137 *cor = buf;
138 dbg_rcvry("found corruption at %d:%d", lnum, offs);
139 }
140 offs += sz;
141 buf += sz;
142 len -= sz;
143 }
144 /* Check remaining empty space */
145 if (offs < c->leb_size)
146 if (!is_empty(buf, len))
147 goto out_err;
148 *pbuf = sbuf;
149 return 0;
150
151out_err:
152 err = -EINVAL;
153out_free:
154 vfree(sbuf);
155 *mst = NULL;
156 *cor = NULL;
157 return err;
158}
159
160/**
161 * write_rcvrd_mst_node - write recovered master node.
162 * @c: UBIFS file-system description object
163 * @mst: master node
164 *
165 * This function returns %0 on success and a negative error code on failure.
166 */
167static int write_rcvrd_mst_node(struct ubifs_info *c,
168 struct ubifs_mst_node *mst)
169{
170 int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
171 uint32_t save_flags;
172
173 dbg_rcvry("recovery");
174
175 save_flags = mst->flags;
176 mst->flags = cpu_to_le32(le32_to_cpu(mst->flags) | UBIFS_MST_RCVRY);
177
178 ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
179 err = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
180 if (err)
181 goto out;
182 err = ubi_leb_change(c->ubi, lnum + 1, mst, sz, UBI_SHORTTERM);
183 if (err)
184 goto out;
185out:
186 mst->flags = save_flags;
187 return err;
188}
189
190/**
191 * ubifs_recover_master_node - recover the master node.
192 * @c: UBIFS file-system description object
193 *
194 * This function recovers the master node from corruption that may occur due to
195 * an unclean unmount.
196 *
197 * This function returns %0 on success and a negative error code on failure.
198 */
199int ubifs_recover_master_node(struct ubifs_info *c)
200{
201 void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
202 struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
203 const int sz = c->mst_node_alsz;
204 int err, offs1, offs2;
205
206 dbg_rcvry("recovery");
207
208 err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
209 if (err)
210 goto out_free;
211
212 err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
213 if (err)
214 goto out_free;
215
216 if (mst1) {
217 offs1 = (void *)mst1 - buf1;
218 if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
219 (offs1 == 0 && !cor1)) {
220 /*
221 * mst1 was written by recovery at offset 0 with no
222 * corruption.
223 */
224 dbg_rcvry("recovery recovery");
225 mst = mst1;
226 } else if (mst2) {
227 offs2 = (void *)mst2 - buf2;
228 if (offs1 == offs2) {
229 /* Same offset, so must be the same */
230 if (memcmp((void *)mst1 + UBIFS_CH_SZ,
231 (void *)mst2 + UBIFS_CH_SZ,
232 UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
233 goto out_err;
234 mst = mst1;
235 } else if (offs2 + sz == offs1) {
236 /* 1st LEB was written, 2nd was not */
237 if (cor1)
238 goto out_err;
239 mst = mst1;
240 } else if (offs1 == 0 && offs2 + sz >= c->leb_size) {
241 /* 1st LEB was unmapped and written, 2nd not */
242 if (cor1)
243 goto out_err;
244 mst = mst1;
245 } else
246 goto out_err;
247 } else {
248 /*
249 * 2nd LEB was unmapped and about to be written, so
250 * there must be only one master node in the first LEB
251 * and no corruption.
252 */
253 if (offs1 != 0 || cor1)
254 goto out_err;
255 mst = mst1;
256 }
257 } else {
258 if (!mst2)
259 goto out_err;
260 /*
261 * 1st LEB was unmapped and about to be written, so there must
262 * be no room left in 2nd LEB.
263 */
264 offs2 = (void *)mst2 - buf2;
265 if (offs2 + sz + sz <= c->leb_size)
266 goto out_err;
267 mst = mst2;
268 }
269
270 dbg_rcvry("recovered master node from LEB %d",
271 (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
272
273 memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
274
275 if ((c->vfs_sb->s_flags & MS_RDONLY)) {
276 /* Read-only mode. Keep a copy for switching to rw mode */
277 c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
278 if (!c->rcvrd_mst_node) {
279 err = -ENOMEM;
280 goto out_free;
281 }
282 memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
283 } else {
284 /* Write the recovered master node */
285 c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
286 err = write_rcvrd_mst_node(c, c->mst_node);
287 if (err)
288 goto out_free;
289 }
290
291 vfree(buf2);
292 vfree(buf1);
293
294 return 0;
295
296out_err:
297 err = -EINVAL;
298out_free:
299 ubifs_err("failed to recover master node");
300 if (mst1) {
301 dbg_err("dumping first master node");
302 dbg_dump_node(c, mst1);
303 }
304 if (mst2) {
305 dbg_err("dumping second master node");
306 dbg_dump_node(c, mst2);
307 }
308 vfree(buf2);
309 vfree(buf1);
310 return err;
311}
312
313/**
314 * ubifs_write_rcvrd_mst_node - write the recovered master node.
315 * @c: UBIFS file-system description object
316 *
317 * This function writes the master node that was recovered during mounting in
318 * read-only mode and must now be written because we are remounting rw.
319 *
320 * This function returns %0 on success and a negative error code on failure.
321 */
322int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
323{
324 int err;
325
326 if (!c->rcvrd_mst_node)
327 return 0;
328 c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
329 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
330 err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
331 if (err)
332 return err;
333 kfree(c->rcvrd_mst_node);
334 c->rcvrd_mst_node = NULL;
335 return 0;
336}
337
338/**
339 * is_last_write - determine if an offset was in the last write to a LEB.
340 * @c: UBIFS file-system description object
341 * @buf: buffer to check
342 * @offs: offset to check
343 *
344 * This function returns %1 if @offs was in the last write to the LEB whose data
345 * is in @buf, otherwise %0 is returned. The determination is made by checking
346 * for subsequent empty space starting from the next min_io_size boundary (or a
347 * bit less than the common header size if min_io_size is one).
348 */
349static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
350{
351 int empty_offs;
352 int check_len;
353 uint8_t *p;
354
355 if (c->min_io_size == 1) {
356 check_len = c->leb_size - offs;
357 p = buf + check_len;
358 for (; check_len > 0; check_len--)
359 if (*--p != 0xff)
360 break;
361 /*
362 * 'check_len' is the size of the corruption which cannot be
363 * more than the size of 1 node if it was caused by an unclean
364 * unmount.
365 */
366 if (check_len > UBIFS_MAX_NODE_SZ)
367 return 0;
368 return 1;
369 }
370
371 /*
372 * Round up to the next c->min_io_size boundary i.e. 'offs' is in the
373 * last wbuf written. After that should be empty space.
374 */
375 empty_offs = ALIGN(offs + 1, c->min_io_size);
376 check_len = c->leb_size - empty_offs;
377 p = buf + empty_offs - offs;
378
379 for (; check_len > 0; check_len--)
380 if (*p++ != 0xff)
381 return 0;
382 return 1;
383}
384
385/**
386 * clean_buf - clean the data from an LEB sitting in a buffer.
387 * @c: UBIFS file-system description object
388 * @buf: buffer to clean
389 * @lnum: LEB number to clean
390 * @offs: offset from which to clean
391 * @len: length of buffer
392 *
393 * This function pads up to the next min_io_size boundary (if there is one) and
394 * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
395 * min_io_size boundary (if there is one).
396 */
397static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
398 int *offs, int *len)
399{
400 int empty_offs, pad_len;
401
402 lnum = lnum;
403 dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
404
405 if (c->min_io_size == 1) {
406 memset(*buf, 0xff, c->leb_size - *offs);
407 return;
408 }
409
410 ubifs_assert(!(*offs & 7));
411 empty_offs = ALIGN(*offs, c->min_io_size);
412 pad_len = empty_offs - *offs;
413 ubifs_pad(c, *buf, pad_len);
414 *offs += pad_len;
415 *buf += pad_len;
416 *len -= pad_len;
417 memset(*buf, 0xff, c->leb_size - empty_offs);
418}
419
420/**
421 * no_more_nodes - determine if there are no more nodes in a buffer.
422 * @c: UBIFS file-system description object
423 * @buf: buffer to check
424 * @len: length of buffer
425 * @lnum: LEB number of the LEB from which @buf was read
426 * @offs: offset from which @buf was read
427 *
428 * This function scans @buf for more nodes and returns %0 is a node is found and
429 * %1 if no more nodes are found.
430 */
431static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
432 int lnum, int offs)
433{
434 int skip, next_offs = 0;
435
436 if (len > UBIFS_DATA_NODE_SZ) {
437 struct ubifs_ch *ch = buf;
438 int dlen = le32_to_cpu(ch->len);
439
440 if (ch->node_type == UBIFS_DATA_NODE && dlen >= UBIFS_CH_SZ &&
441 dlen <= UBIFS_MAX_DATA_NODE_SZ)
442 /* The corrupt node looks like a data node */
443 next_offs = ALIGN(offs + dlen, 8);
444 }
445
446 if (c->min_io_size == 1)
447 skip = 8;
448 else
449 skip = ALIGN(offs + 1, c->min_io_size) - offs;
450
451 offs += skip;
452 buf += skip;
453 len -= skip;
454 while (len > 8) {
455 struct ubifs_ch *ch = buf;
456 uint32_t magic = le32_to_cpu(ch->magic);
457 int ret;
458
459 if (magic == UBIFS_NODE_MAGIC) {
460 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
461 if (ret == SCANNED_A_NODE || ret > 0) {
462 /*
463 * There is a small chance this is just data in
464 * a data node, so check that possibility. e.g.
465 * this is part of a file that itself contains
466 * a UBIFS image.
467 */
468 if (next_offs && offs + le32_to_cpu(ch->len) <=
469 next_offs)
470 continue;
471 dbg_rcvry("unexpected node at %d:%d", lnum,
472 offs);
473 return 0;
474 }
475 }
476 offs += 8;
477 buf += 8;
478 len -= 8;
479 }
480 return 1;
481}
482
483/**
484 * fix_unclean_leb - fix an unclean LEB.
485 * @c: UBIFS file-system description object
486 * @sleb: scanned LEB information
487 * @start: offset where scan started
488 */
489static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
490 int start)
491{
492 int lnum = sleb->lnum, endpt = start;
493
494 /* Get the end offset of the last node we are keeping */
495 if (!list_empty(&sleb->nodes)) {
496 struct ubifs_scan_node *snod;
497
498 snod = list_entry(sleb->nodes.prev,
499 struct ubifs_scan_node, list);
500 endpt = snod->offs + snod->len;
501 }
502
503 if ((c->vfs_sb->s_flags & MS_RDONLY) && !c->remounting_rw) {
504 /* Add to recovery list */
505 struct ubifs_unclean_leb *ucleb;
506
507 dbg_rcvry("need to fix LEB %d start %d endpt %d",
508 lnum, start, sleb->endpt);
509 ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
510 if (!ucleb)
511 return -ENOMEM;
512 ucleb->lnum = lnum;
513 ucleb->endpt = endpt;
514 list_add_tail(&ucleb->list, &c->unclean_leb_list);
515 } else {
516 /* Write the fixed LEB back to flash */
517 int err;
518
519 dbg_rcvry("fixing LEB %d start %d endpt %d",
520 lnum, start, sleb->endpt);
521 if (endpt == 0) {
522 err = ubifs_leb_unmap(c, lnum);
523 if (err)
524 return err;
525 } else {
526 int len = ALIGN(endpt, c->min_io_size);
527
528 if (start) {
529 err = ubi_read(c->ubi, lnum, sleb->buf, 0,
530 start);
531 if (err)
532 return err;
533 }
534 /* Pad to min_io_size */
535 if (len > endpt) {
536 int pad_len = len - ALIGN(endpt, 8);
537
538 if (pad_len > 0) {
539 void *buf = sleb->buf + len - pad_len;
540
541 ubifs_pad(c, buf, pad_len);
542 }
543 }
544 err = ubi_leb_change(c->ubi, lnum, sleb->buf, len,
545 UBI_UNKNOWN);
546 if (err)
547 return err;
548 }
549 }
550 return 0;
551}
552
553/**
554 * drop_incomplete_group - drop nodes from an incomplete group.
555 * @sleb: scanned LEB information
556 * @offs: offset of dropped nodes is returned here
557 *
558 * This function returns %1 if nodes are dropped and %0 otherwise.
559 */
560static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
561{
562 int dropped = 0;
563
564 while (!list_empty(&sleb->nodes)) {
565 struct ubifs_scan_node *snod;
566 struct ubifs_ch *ch;
567
568 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
569 list);
570 ch = snod->node;
571 if (ch->group_type != UBIFS_IN_NODE_GROUP)
572 return dropped;
573 dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
574 *offs = snod->offs;
575 list_del(&snod->list);
576 kfree(snod);
577 sleb->nodes_cnt -= 1;
578 dropped = 1;
579 }
580 return dropped;
581}
582
583/**
584 * ubifs_recover_leb - scan and recover a LEB.
585 * @c: UBIFS file-system description object
586 * @lnum: LEB number
587 * @offs: offset
588 * @sbuf: LEB-sized buffer to use
589 * @grouped: nodes may be grouped for recovery
590 *
591 * This function does a scan of a LEB, but caters for errors that might have
592 * been caused by the unclean unmount from which we are attempting to recover.
593 *
594 * This function returns %0 on success and a negative error code on failure.
595 */
596struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
597 int offs, void *sbuf, int grouped)
598{
599 int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
600 int empty_chkd = 0, start = offs;
601 struct ubifs_scan_leb *sleb;
602 void *buf = sbuf + offs;
603
604 dbg_rcvry("%d:%d", lnum, offs);
605
606 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
607 if (IS_ERR(sleb))
608 return sleb;
609
610 if (sleb->ecc)
611 need_clean = 1;
612
613 while (len >= 8) {
614 int ret;
615
616 dbg_scan("look at LEB %d:%d (%d bytes left)",
617 lnum, offs, len);
618
619 cond_resched();
620
621 /*
622 * Scan quietly until there is an error from which we cannot
623 * recover
624 */
625 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
626
627 if (ret == SCANNED_A_NODE) {
628 /* A valid node, and not a padding node */
629 struct ubifs_ch *ch = buf;
630 int node_len;
631
632 err = ubifs_add_snod(c, sleb, buf, offs);
633 if (err)
634 goto error;
635 node_len = ALIGN(le32_to_cpu(ch->len), 8);
636 offs += node_len;
637 buf += node_len;
638 len -= node_len;
639 continue;
640 }
641
642 if (ret > 0) {
643 /* Padding bytes or a valid padding node */
644 offs += ret;
645 buf += ret;
646 len -= ret;
647 continue;
648 }
649
650 if (ret == SCANNED_EMPTY_SPACE) {
651 if (!is_empty(buf, len)) {
652 if (!is_last_write(c, buf, offs))
653 break;
654 clean_buf(c, &buf, lnum, &offs, &len);
655 need_clean = 1;
656 }
657 empty_chkd = 1;
658 break;
659 }
660
661 if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
662 if (is_last_write(c, buf, offs)) {
663 clean_buf(c, &buf, lnum, &offs, &len);
664 need_clean = 1;
665 empty_chkd = 1;
666 break;
667 }
668
669 if (ret == SCANNED_A_CORRUPT_NODE)
670 if (no_more_nodes(c, buf, len, lnum, offs)) {
671 clean_buf(c, &buf, lnum, &offs, &len);
672 need_clean = 1;
673 empty_chkd = 1;
674 break;
675 }
676
677 if (quiet) {
678 /* Redo the last scan but noisily */
679 quiet = 0;
680 continue;
681 }
682
683 switch (ret) {
684 case SCANNED_GARBAGE:
685 dbg_err("garbage");
686 goto corrupted;
687 case SCANNED_A_CORRUPT_NODE:
688 case SCANNED_A_BAD_PAD_NODE:
689 dbg_err("bad node");
690 goto corrupted;
691 default:
692 dbg_err("unknown");
693 goto corrupted;
694 }
695 }
696
697 if (!empty_chkd && !is_empty(buf, len)) {
698 if (is_last_write(c, buf, offs)) {
699 clean_buf(c, &buf, lnum, &offs, &len);
700 need_clean = 1;
701 } else {
702 ubifs_err("corrupt empty space at LEB %d:%d",
703 lnum, offs);
704 goto corrupted;
705 }
706 }
707
708 /* Drop nodes from incomplete group */
709 if (grouped && drop_incomplete_group(sleb, &offs)) {
710 buf = sbuf + offs;
711 len = c->leb_size - offs;
712 clean_buf(c, &buf, lnum, &offs, &len);
713 need_clean = 1;
714 }
715
716 if (offs % c->min_io_size) {
717 clean_buf(c, &buf, lnum, &offs, &len);
718 need_clean = 1;
719 }
720
721 ubifs_end_scan(c, sleb, lnum, offs);
722
723 if (need_clean) {
724 err = fix_unclean_leb(c, sleb, start);
725 if (err)
726 goto error;
727 }
728
729 return sleb;
730
731corrupted:
732 ubifs_scanned_corruption(c, lnum, offs, buf);
733 err = -EUCLEAN;
734error:
735 ubifs_err("LEB %d scanning failed", lnum);
736 ubifs_scan_destroy(sleb);
737 return ERR_PTR(err);
738}
739
740/**
741 * get_cs_sqnum - get commit start sequence number.
742 * @c: UBIFS file-system description object
743 * @lnum: LEB number of commit start node
744 * @offs: offset of commit start node
745 * @cs_sqnum: commit start sequence number is returned here
746 *
747 * This function returns %0 on success and a negative error code on failure.
748 */
749static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
750 unsigned long long *cs_sqnum)
751{
752 struct ubifs_cs_node *cs_node = NULL;
753 int err, ret;
754
755 dbg_rcvry("at %d:%d", lnum, offs);
756 cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
757 if (!cs_node)
758 return -ENOMEM;
759 if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
760 goto out_err;
761 err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
762 if (err && err != -EBADMSG)
763 goto out_free;
764 ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
765 if (ret != SCANNED_A_NODE) {
766 dbg_err("Not a valid node");
767 goto out_err;
768 }
769 if (cs_node->ch.node_type != UBIFS_CS_NODE) {
770 dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
771 goto out_err;
772 }
773 if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
774 dbg_err("CS node cmt_no %llu != current cmt_no %llu",
775 (unsigned long long)le64_to_cpu(cs_node->cmt_no),
776 c->cmt_no);
777 goto out_err;
778 }
779 *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
780 dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
781 kfree(cs_node);
782 return 0;
783
784out_err:
785 err = -EINVAL;
786out_free:
787 ubifs_err("failed to get CS sqnum");
788 kfree(cs_node);
789 return err;
790}
791
792/**
793 * ubifs_recover_log_leb - scan and recover a log LEB.
794 * @c: UBIFS file-system description object
795 * @lnum: LEB number
796 * @offs: offset
797 * @sbuf: LEB-sized buffer to use
798 *
799 * This function does a scan of a LEB, but caters for errors that might have
800 * been caused by the unclean unmount from which we are attempting to recover.
801 *
802 * This function returns %0 on success and a negative error code on failure.
803 */
804struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
805 int offs, void *sbuf)
806{
807 struct ubifs_scan_leb *sleb;
808 int next_lnum;
809
810 dbg_rcvry("LEB %d", lnum);
811 next_lnum = lnum + 1;
812 if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
813 next_lnum = UBIFS_LOG_LNUM;
814 if (next_lnum != c->ltail_lnum) {
815 /*
816 * We can only recover at the end of the log, so check that the
817 * next log LEB is empty or out of date.
818 */
819 sleb = ubifs_scan(c, next_lnum, 0, sbuf);
820 if (IS_ERR(sleb))
821 return sleb;
822 if (sleb->nodes_cnt) {
823 struct ubifs_scan_node *snod;
824 unsigned long long cs_sqnum = c->cs_sqnum;
825
826 snod = list_entry(sleb->nodes.next,
827 struct ubifs_scan_node, list);
828 if (cs_sqnum == 0) {
829 int err;
830
831 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
832 if (err) {
833 ubifs_scan_destroy(sleb);
834 return ERR_PTR(err);
835 }
836 }
837 if (snod->sqnum > cs_sqnum) {
838 ubifs_err("unrecoverable log corruption "
839 "in LEB %d", lnum);
840 ubifs_scan_destroy(sleb);
841 return ERR_PTR(-EUCLEAN);
842 }
843 }
844 ubifs_scan_destroy(sleb);
845 }
846 return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
847}
848
849/**
850 * recover_head - recover a head.
851 * @c: UBIFS file-system description object
852 * @lnum: LEB number of head to recover
853 * @offs: offset of head to recover
854 * @sbuf: LEB-sized buffer to use
855 *
856 * This function ensures that there is no data on the flash at a head location.
857 *
858 * This function returns %0 on success and a negative error code on failure.
859 */
860static int recover_head(const struct ubifs_info *c, int lnum, int offs,
861 void *sbuf)
862{
863 int len, err, need_clean = 0;
864
865 if (c->min_io_size > 1)
866 len = c->min_io_size;
867 else
868 len = 512;
869 if (offs + len > c->leb_size)
870 len = c->leb_size - offs;
871
872 if (!len)
873 return 0;
874
875 /* Read at the head location and check it is empty flash */
876 err = ubi_read(c->ubi, lnum, sbuf, offs, len);
877 if (err)
878 need_clean = 1;
879 else {
880 uint8_t *p = sbuf;
881
882 while (len--)
883 if (*p++ != 0xff) {
884 need_clean = 1;
885 break;
886 }
887 }
888
889 if (need_clean) {
890 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
891 if (offs == 0)
892 return ubifs_leb_unmap(c, lnum);
893 err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
894 if (err)
895 return err;
896 return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
897 }
898
899 return 0;
900}
901
902/**
903 * ubifs_recover_inl_heads - recover index and LPT heads.
904 * @c: UBIFS file-system description object
905 * @sbuf: LEB-sized buffer to use
906 *
907 * This function ensures that there is no data on the flash at the index and
908 * LPT head locations.
909 *
910 * This deals with the recovery of a half-completed journal commit. UBIFS is
911 * careful never to overwrite the last version of the index or the LPT. Because
912 * the index and LPT are wandering trees, data from a half-completed commit will
913 * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
914 * assumed to be empty and will be unmapped anyway before use, or in the index
915 * and LPT heads.
916 *
917 * This function returns %0 on success and a negative error code on failure.
918 */
919int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
920{
921 int err;
922
923 ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY) || c->remounting_rw);
924
925 dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
926 err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
927 if (err)
928 return err;
929
930 dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
931 err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
932 if (err)
933 return err;
934
935 return 0;
936}
937
938/**
939 * clean_an_unclean_leb - read and write a LEB to remove corruption.
940 * @c: UBIFS file-system description object
941 * @ucleb: unclean LEB information
942 * @sbuf: LEB-sized buffer to use
943 *
944 * This function reads a LEB up to a point pre-determined by the mount recovery,
945 * checks the nodes, and writes the result back to the flash, thereby cleaning
946 * off any following corruption, or non-fatal ECC errors.
947 *
948 * This function returns %0 on success and a negative error code on failure.
949 */
950static int clean_an_unclean_leb(const struct ubifs_info *c,
951 struct ubifs_unclean_leb *ucleb, void *sbuf)
952{
953 int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
954 void *buf = sbuf;
955
956 dbg_rcvry("LEB %d len %d", lnum, len);
957
958 if (len == 0) {
959 /* Nothing to read, just unmap it */
960 err = ubifs_leb_unmap(c, lnum);
961 if (err)
962 return err;
963 return 0;
964 }
965
966 err = ubi_read(c->ubi, lnum, buf, offs, len);
967 if (err && err != -EBADMSG)
968 return err;
969
970 while (len >= 8) {
971 int ret;
972
973 cond_resched();
974
975 /* Scan quietly until there is an error */
976 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
977
978 if (ret == SCANNED_A_NODE) {
979 /* A valid node, and not a padding node */
980 struct ubifs_ch *ch = buf;
981 int node_len;
982
983 node_len = ALIGN(le32_to_cpu(ch->len), 8);
984 offs += node_len;
985 buf += node_len;
986 len -= node_len;
987 continue;
988 }
989
990 if (ret > 0) {
991 /* Padding bytes or a valid padding node */
992 offs += ret;
993 buf += ret;
994 len -= ret;
995 continue;
996 }
997
998 if (ret == SCANNED_EMPTY_SPACE) {
999 ubifs_err("unexpected empty space at %d:%d",
1000 lnum, offs);
1001 return -EUCLEAN;
1002 }
1003
1004 if (quiet) {
1005 /* Redo the last scan but noisily */
1006 quiet = 0;
1007 continue;
1008 }
1009
1010 ubifs_scanned_corruption(c, lnum, offs, buf);
1011 return -EUCLEAN;
1012 }
1013
1014 /* Pad to min_io_size */
1015 len = ALIGN(ucleb->endpt, c->min_io_size);
1016 if (len > ucleb->endpt) {
1017 int pad_len = len - ALIGN(ucleb->endpt, 8);
1018
1019 if (pad_len > 0) {
1020 buf = c->sbuf + len - pad_len;
1021 ubifs_pad(c, buf, pad_len);
1022 }
1023 }
1024
1025 /* Write back the LEB atomically */
1026 err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
1027 if (err)
1028 return err;
1029
1030 dbg_rcvry("cleaned LEB %d", lnum);
1031
1032 return 0;
1033}
1034
1035/**
1036 * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
1037 * @c: UBIFS file-system description object
1038 * @sbuf: LEB-sized buffer to use
1039 *
1040 * This function cleans a LEB identified during recovery that needs to be
1041 * written but was not because UBIFS was mounted read-only. This happens when
1042 * remounting to read-write mode.
1043 *
1044 * This function returns %0 on success and a negative error code on failure.
1045 */
1046int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
1047{
1048 dbg_rcvry("recovery");
1049 while (!list_empty(&c->unclean_leb_list)) {
1050 struct ubifs_unclean_leb *ucleb;
1051 int err;
1052
1053 ucleb = list_entry(c->unclean_leb_list.next,
1054 struct ubifs_unclean_leb, list);
1055 err = clean_an_unclean_leb(c, ucleb, sbuf);
1056 if (err)
1057 return err;
1058 list_del(&ucleb->list);
1059 kfree(ucleb);
1060 }
1061 return 0;
1062}
1063
1064/**
1065 * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1066 * @c: UBIFS file-system description object
1067 *
1068 * Out-of-place garbage collection requires always one empty LEB with which to
1069 * start garbage collection. The LEB number is recorded in c->gc_lnum and is
1070 * written to the master node on unmounting. In the case of an unclean unmount
1071 * the value of gc_lnum recorded in the master node is out of date and cannot
1072 * be used. Instead, recovery must allocate an empty LEB for this purpose.
1073 * However, there may not be enough empty space, in which case it must be
1074 * possible to GC the dirtiest LEB into the GC head LEB.
1075 *
1076 * This function also runs the commit which causes the TNC updates from
1077 * size-recovery and orphans to be written to the flash. That is important to
1078 * ensure correct replay order for subsequent mounts.
1079 *
1080 * This function returns %0 on success and a negative error code on failure.
1081 */
1082int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1083{
1084 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1085 struct ubifs_lprops lp;
1086 int lnum, err;
1087
1088 c->gc_lnum = -1;
1089 if (wbuf->lnum == -1) {
1090 dbg_rcvry("no GC head LEB");
1091 goto find_free;
1092 }
1093 /*
1094 * See whether the used space in the dirtiest LEB fits in the GC head
1095 * LEB.
1096 */
1097 if (wbuf->offs == c->leb_size) {
1098 dbg_rcvry("no room in GC head LEB");
1099 goto find_free;
1100 }
1101 err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1102 if (err) {
1103 if (err == -ENOSPC)
1104 dbg_err("could not find a dirty LEB");
1105 return err;
1106 }
1107 ubifs_assert(!(lp.flags & LPROPS_INDEX));
1108 lnum = lp.lnum;
1109 if (lp.free + lp.dirty == c->leb_size) {
1110 /* An empty LEB was returned */
1111 if (lp.free != c->leb_size) {
1112 err = ubifs_change_one_lp(c, lnum, c->leb_size,
1113 0, 0, 0, 0);
1114 if (err)
1115 return err;
1116 }
1117 err = ubifs_leb_unmap(c, lnum);
1118 if (err)
1119 return err;
1120 c->gc_lnum = lnum;
1121 dbg_rcvry("allocated LEB %d for GC", lnum);
1122 /* Run the commit */
1123 dbg_rcvry("committing");
1124 return ubifs_run_commit(c);
1125 }
1126 /*
1127 * There was no empty LEB so the used space in the dirtiest LEB must fit
1128 * in the GC head LEB.
1129 */
1130 if (lp.free + lp.dirty < wbuf->offs) {
1131 dbg_rcvry("LEB %d doesn't fit in GC head LEB %d:%d",
1132 lnum, wbuf->lnum, wbuf->offs);
1133 err = ubifs_return_leb(c, lnum);
1134 if (err)
1135 return err;
1136 goto find_free;
1137 }
1138 /*
1139 * We run the commit before garbage collection otherwise subsequent
1140 * mounts will see the GC and orphan deletion in a different order.
1141 */
1142 dbg_rcvry("committing");
1143 err = ubifs_run_commit(c);
1144 if (err)
1145 return err;
1146 /*
1147 * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
1148 * - use locking to keep 'ubifs_assert()' happy.
1149 */
1150 dbg_rcvry("GC'ing LEB %d", lnum);
1151 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1152 err = ubifs_garbage_collect_leb(c, &lp);
1153 if (err >= 0) {
1154 int err2 = ubifs_wbuf_sync_nolock(wbuf);
1155
1156 if (err2)
1157 err = err2;
1158 }
1159 mutex_unlock(&wbuf->io_mutex);
1160 if (err < 0) {
1161 dbg_err("GC failed, error %d", err);
1162 if (err == -EAGAIN)
1163 err = -EINVAL;
1164 return err;
1165 }
1166 if (err != LEB_RETAINED) {
1167 dbg_err("GC returned %d", err);
1168 return -EINVAL;
1169 }
1170 err = ubifs_leb_unmap(c, c->gc_lnum);
1171 if (err)
1172 return err;
1173 dbg_rcvry("allocated LEB %d for GC", lnum);
1174 return 0;
1175
1176find_free:
1177 /*
1178 * There is no GC head LEB or the free space in the GC head LEB is too
1179 * small. Allocate gc_lnum by calling 'ubifs_find_free_leb_for_idx()' so
1180 * GC is not run.
1181 */
1182 lnum = ubifs_find_free_leb_for_idx(c);
1183 if (lnum < 0) {
1184 dbg_err("could not find an empty LEB");
1185 return lnum;
1186 }
1187 /* And reset the index flag */
1188 err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1189 LPROPS_INDEX, 0);
1190 if (err)
1191 return err;
1192 c->gc_lnum = lnum;
1193 dbg_rcvry("allocated LEB %d for GC", lnum);
1194 /* Run the commit */
1195 dbg_rcvry("committing");
1196 return ubifs_run_commit(c);
1197}
1198
1199/**
1200 * struct size_entry - inode size information for recovery.
1201 * @rb: link in the RB-tree of sizes
1202 * @inum: inode number
1203 * @i_size: size on inode
1204 * @d_size: maximum size based on data nodes
1205 * @exists: indicates whether the inode exists
1206 * @inode: inode if pinned in memory awaiting rw mode to fix it
1207 */
1208struct size_entry {
1209 struct rb_node rb;
1210 ino_t inum;
1211 loff_t i_size;
1212 loff_t d_size;
1213 int exists;
1214 struct inode *inode;
1215};
1216
1217/**
1218 * add_ino - add an entry to the size tree.
1219 * @c: UBIFS file-system description object
1220 * @inum: inode number
1221 * @i_size: size on inode
1222 * @d_size: maximum size based on data nodes
1223 * @exists: indicates whether the inode exists
1224 */
1225static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1226 loff_t d_size, int exists)
1227{
1228 struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1229 struct size_entry *e;
1230
1231 while (*p) {
1232 parent = *p;
1233 e = rb_entry(parent, struct size_entry, rb);
1234 if (inum < e->inum)
1235 p = &(*p)->rb_left;
1236 else
1237 p = &(*p)->rb_right;
1238 }
1239
1240 e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1241 if (!e)
1242 return -ENOMEM;
1243
1244 e->inum = inum;
1245 e->i_size = i_size;
1246 e->d_size = d_size;
1247 e->exists = exists;
1248
1249 rb_link_node(&e->rb, parent, p);
1250 rb_insert_color(&e->rb, &c->size_tree);
1251
1252 return 0;
1253}
1254
1255/**
1256 * find_ino - find an entry on the size tree.
1257 * @c: UBIFS file-system description object
1258 * @inum: inode number
1259 */
1260static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1261{
1262 struct rb_node *p = c->size_tree.rb_node;
1263 struct size_entry *e;
1264
1265 while (p) {
1266 e = rb_entry(p, struct size_entry, rb);
1267 if (inum < e->inum)
1268 p = p->rb_left;
1269 else if (inum > e->inum)
1270 p = p->rb_right;
1271 else
1272 return e;
1273 }
1274 return NULL;
1275}
1276
1277/**
1278 * remove_ino - remove an entry from the size tree.
1279 * @c: UBIFS file-system description object
1280 * @inum: inode number
1281 */
1282static void remove_ino(struct ubifs_info *c, ino_t inum)
1283{
1284 struct size_entry *e = find_ino(c, inum);
1285
1286 if (!e)
1287 return;
1288 rb_erase(&e->rb, &c->size_tree);
1289 kfree(e);
1290}
1291
1292/**
1293 * ubifs_destroy_size_tree - free resources related to the size tree.
1294 * @c: UBIFS file-system description object
1295 */
1296void ubifs_destroy_size_tree(struct ubifs_info *c)
1297{
1298 struct rb_node *this = c->size_tree.rb_node;
1299 struct size_entry *e;
1300
1301 while (this) {
1302 if (this->rb_left) {
1303 this = this->rb_left;
1304 continue;
1305 } else if (this->rb_right) {
1306 this = this->rb_right;
1307 continue;
1308 }
1309 e = rb_entry(this, struct size_entry, rb);
1310 if (e->inode)
1311 iput(e->inode);
1312 this = rb_parent(this);
1313 if (this) {
1314 if (this->rb_left == &e->rb)
1315 this->rb_left = NULL;
1316 else
1317 this->rb_right = NULL;
1318 }
1319 kfree(e);
1320 }
1321 c->size_tree = RB_ROOT;
1322}
1323
1324/**
1325 * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1326 * @c: UBIFS file-system description object
1327 * @key: node key
1328 * @deletion: node is for a deletion
1329 * @new_size: inode size
1330 *
1331 * This function has two purposes:
1332 * 1) to ensure there are no data nodes that fall outside the inode size
1333 * 2) to ensure there are no data nodes for inodes that do not exist
1334 * To accomplish those purposes, a rb-tree is constructed containing an entry
1335 * for each inode number in the journal that has not been deleted, and recording
1336 * the size from the inode node, the maximum size of any data node (also altered
1337 * by truncations) and a flag indicating a inode number for which no inode node
1338 * was present in the journal.
1339 *
1340 * Note that there is still the possibility that there are data nodes that have
1341 * been committed that are beyond the inode size, however the only way to find
1342 * them would be to scan the entire index. Alternatively, some provision could
1343 * be made to record the size of inodes at the start of commit, which would seem
1344 * very cumbersome for a scenario that is quite unlikely and the only negative
1345 * consequence of which is wasted space.
1346 *
1347 * This functions returns %0 on success and a negative error code on failure.
1348 */
1349int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1350 int deletion, loff_t new_size)
1351{
1352 ino_t inum = key_inum(c, key);
1353 struct size_entry *e;
1354 int err;
1355
1356 switch (key_type(c, key)) {
1357 case UBIFS_INO_KEY:
1358 if (deletion)
1359 remove_ino(c, inum);
1360 else {
1361 e = find_ino(c, inum);
1362 if (e) {
1363 e->i_size = new_size;
1364 e->exists = 1;
1365 } else {
1366 err = add_ino(c, inum, new_size, 0, 1);
1367 if (err)
1368 return err;
1369 }
1370 }
1371 break;
1372 case UBIFS_DATA_KEY:
1373 e = find_ino(c, inum);
1374 if (e) {
1375 if (new_size > e->d_size)
1376 e->d_size = new_size;
1377 } else {
1378 err = add_ino(c, inum, 0, new_size, 0);
1379 if (err)
1380 return err;
1381 }
1382 break;
1383 case UBIFS_TRUN_KEY:
1384 e = find_ino(c, inum);
1385 if (e)
1386 e->d_size = new_size;
1387 break;
1388 }
1389 return 0;
1390}
1391
1392/**
1393 * fix_size_in_place - fix inode size in place on flash.
1394 * @c: UBIFS file-system description object
1395 * @e: inode size information for recovery
1396 */
1397static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1398{
1399 struct ubifs_ino_node *ino = c->sbuf;
1400 unsigned char *p;
1401 union ubifs_key key;
1402 int err, lnum, offs, len;
1403 loff_t i_size;
1404 uint32_t crc;
1405
1406 /* Locate the inode node LEB number and offset */
1407 ino_key_init(c, &key, e->inum);
1408 err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1409 if (err)
1410 goto out;
1411 /*
1412 * If the size recorded on the inode node is greater than the size that
1413 * was calculated from nodes in the journal then don't change the inode.
1414 */
1415 i_size = le64_to_cpu(ino->size);
1416 if (i_size >= e->d_size)
1417 return 0;
1418 /* Read the LEB */
1419 err = ubi_read(c->ubi, lnum, c->sbuf, 0, c->leb_size);
1420 if (err)
1421 goto out;
1422 /* Change the size field and recalculate the CRC */
1423 ino = c->sbuf + offs;
1424 ino->size = cpu_to_le64(e->d_size);
1425 len = le32_to_cpu(ino->ch.len);
1426 crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1427 ino->ch.crc = cpu_to_le32(crc);
1428 /* Work out where data in the LEB ends and free space begins */
1429 p = c->sbuf;
1430 len = c->leb_size - 1;
1431 while (p[len] == 0xff)
1432 len -= 1;
1433 len = ALIGN(len + 1, c->min_io_size);
1434 /* Atomically write the fixed LEB back again */
1435 err = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
1436 if (err)
1437 goto out;
1438 dbg_rcvry("inode %lu at %d:%d size %lld -> %lld ", e->inum, lnum, offs,
1439 i_size, e->d_size);
1440 return 0;
1441
1442out:
1443 ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
1444 e->inum, e->i_size, e->d_size, err);
1445 return err;
1446}
1447
1448/**
1449 * ubifs_recover_size - recover inode size.
1450 * @c: UBIFS file-system description object
1451 *
1452 * This function attempts to fix inode size discrepancies identified by the
1453 * 'ubifs_recover_size_accum()' function.
1454 *
1455 * This functions returns %0 on success and a negative error code on failure.
1456 */
1457int ubifs_recover_size(struct ubifs_info *c)
1458{
1459 struct rb_node *this = rb_first(&c->size_tree);
1460
1461 while (this) {
1462 struct size_entry *e;
1463 int err;
1464
1465 e = rb_entry(this, struct size_entry, rb);
1466 if (!e->exists) {
1467 union ubifs_key key;
1468
1469 ino_key_init(c, &key, e->inum);
1470 err = ubifs_tnc_lookup(c, &key, c->sbuf);
1471 if (err && err != -ENOENT)
1472 return err;
1473 if (err == -ENOENT) {
1474 /* Remove data nodes that have no inode */
1475 dbg_rcvry("removing ino %lu", e->inum);
1476 err = ubifs_tnc_remove_ino(c, e->inum);
1477 if (err)
1478 return err;
1479 } else {
1480 struct ubifs_ino_node *ino = c->sbuf;
1481
1482 e->exists = 1;
1483 e->i_size = le64_to_cpu(ino->size);
1484 }
1485 }
1486 if (e->exists && e->i_size < e->d_size) {
1487 if (!e->inode && (c->vfs_sb->s_flags & MS_RDONLY)) {
1488 /* Fix the inode size and pin it in memory */
1489 struct inode *inode;
1490
1491 inode = ubifs_iget(c->vfs_sb, e->inum);
1492 if (IS_ERR(inode))
1493 return PTR_ERR(inode);
1494 if (inode->i_size < e->d_size) {
1495 dbg_rcvry("ino %lu size %lld -> %lld",
1496 e->inum, e->d_size,
1497 inode->i_size);
1498 inode->i_size = e->d_size;
1499 ubifs_inode(inode)->ui_size = e->d_size;
1500 e->inode = inode;
1501 this = rb_next(this);
1502 continue;
1503 }
1504 iput(inode);
1505 } else {
1506 /* Fix the size in place */
1507 err = fix_size_in_place(c, e);
1508 if (err)
1509 return err;
1510 if (e->inode)
1511 iput(e->inode);
1512 }
1513 }
1514 this = rb_next(this);
1515 rb_erase(&e->rb, &c->size_tree);
1516 kfree(e);
1517 }
1518 return 0;
1519}
diff --git a/fs/ubifs/replay.c b/fs/ubifs/replay.c
new file mode 100644
index 000000000000..7399692af859
--- /dev/null
+++ b/fs/ubifs/replay.c
@@ -0,0 +1,1075 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file contains journal replay code. It runs when the file-system is being
25 * mounted and requires no locking.
26 *
27 * The larger is the journal, the longer it takes to scan it, so the longer it
28 * takes to mount UBIFS. This is why the journal has limited size which may be
29 * changed depending on the system requirements. But a larger journal gives
30 * faster I/O speed because it writes the index less frequently. So this is a
31 * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
32 * larger is the journal, the more memory its index may consume.
33 */
34
35#include "ubifs.h"
36
37/*
38 * Replay flags.
39 *
40 * REPLAY_DELETION: node was deleted
41 * REPLAY_REF: node is a reference node
42 */
43enum {
44 REPLAY_DELETION = 1,
45 REPLAY_REF = 2,
46};
47
48/**
49 * struct replay_entry - replay tree entry.
50 * @lnum: logical eraseblock number of the node
51 * @offs: node offset
52 * @len: node length
53 * @sqnum: node sequence number
54 * @flags: replay flags
55 * @rb: links the replay tree
56 * @key: node key
57 * @nm: directory entry name
58 * @old_size: truncation old size
59 * @new_size: truncation new size
60 * @free: amount of free space in a bud
61 * @dirty: amount of dirty space in a bud from padding and deletion nodes
62 *
63 * UBIFS journal replay must compare node sequence numbers, which means it must
64 * build a tree of node information to insert into the TNC.
65 */
66struct replay_entry {
67 int lnum;
68 int offs;
69 int len;
70 unsigned long long sqnum;
71 int flags;
72 struct rb_node rb;
73 union ubifs_key key;
74 union {
75 struct qstr nm;
76 struct {
77 loff_t old_size;
78 loff_t new_size;
79 };
80 struct {
81 int free;
82 int dirty;
83 };
84 };
85};
86
87/**
88 * struct bud_entry - entry in the list of buds to replay.
89 * @list: next bud in the list
90 * @bud: bud description object
91 * @free: free bytes in the bud
92 * @sqnum: reference node sequence number
93 */
94struct bud_entry {
95 struct list_head list;
96 struct ubifs_bud *bud;
97 int free;
98 unsigned long long sqnum;
99};
100
101/**
102 * set_bud_lprops - set free and dirty space used by a bud.
103 * @c: UBIFS file-system description object
104 * @r: replay entry of bud
105 */
106static int set_bud_lprops(struct ubifs_info *c, struct replay_entry *r)
107{
108 const struct ubifs_lprops *lp;
109 int err = 0, dirty;
110
111 ubifs_get_lprops(c);
112
113 lp = ubifs_lpt_lookup_dirty(c, r->lnum);
114 if (IS_ERR(lp)) {
115 err = PTR_ERR(lp);
116 goto out;
117 }
118
119 dirty = lp->dirty;
120 if (r->offs == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
121 /*
122 * The LEB was added to the journal with a starting offset of
123 * zero which means the LEB must have been empty. The LEB
124 * property values should be lp->free == c->leb_size and
125 * lp->dirty == 0, but that is not the case. The reason is that
126 * the LEB was garbage collected. The garbage collector resets
127 * the free and dirty space without recording it anywhere except
128 * lprops, so if there is not a commit then lprops does not have
129 * that information next time the file system is mounted.
130 *
131 * We do not need to adjust free space because the scan has told
132 * us the exact value which is recorded in the replay entry as
133 * r->free.
134 *
135 * However we do need to subtract from the dirty space the
136 * amount of space that the garbage collector reclaimed, which
137 * is the whole LEB minus the amount of space that was free.
138 */
139 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", r->lnum,
140 lp->free, lp->dirty);
141 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", r->lnum,
142 lp->free, lp->dirty);
143 dirty -= c->leb_size - lp->free;
144 /*
145 * If the replay order was perfect the dirty space would now be
146 * zero. The order is not perfect because the the journal heads
147 * race with eachother. This is not a problem but is does mean
148 * that the dirty space may temporarily exceed c->leb_size
149 * during the replay.
150 */
151 if (dirty != 0)
152 dbg_msg("LEB %d lp: %d free %d dirty "
153 "replay: %d free %d dirty", r->lnum, lp->free,
154 lp->dirty, r->free, r->dirty);
155 }
156 lp = ubifs_change_lp(c, lp, r->free, dirty + r->dirty,
157 lp->flags | LPROPS_TAKEN, 0);
158 if (IS_ERR(lp)) {
159 err = PTR_ERR(lp);
160 goto out;
161 }
162out:
163 ubifs_release_lprops(c);
164 return err;
165}
166
167/**
168 * trun_remove_range - apply a replay entry for a truncation to the TNC.
169 * @c: UBIFS file-system description object
170 * @r: replay entry of truncation
171 */
172static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
173{
174 unsigned min_blk, max_blk;
175 union ubifs_key min_key, max_key;
176 ino_t ino;
177
178 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
179 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
180 min_blk += 1;
181
182 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
183 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
184 max_blk -= 1;
185
186 ino = key_inum(c, &r->key);
187
188 data_key_init(c, &min_key, ino, min_blk);
189 data_key_init(c, &max_key, ino, max_blk);
190
191 return ubifs_tnc_remove_range(c, &min_key, &max_key);
192}
193
194/**
195 * apply_replay_entry - apply a replay entry to the TNC.
196 * @c: UBIFS file-system description object
197 * @r: replay entry to apply
198 *
199 * Apply a replay entry to the TNC.
200 */
201static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
202{
203 int err, deletion = ((r->flags & REPLAY_DELETION) != 0);
204
205 dbg_mnt("LEB %d:%d len %d flgs %d sqnum %llu %s", r->lnum,
206 r->offs, r->len, r->flags, r->sqnum, DBGKEY(&r->key));
207
208 /* Set c->replay_sqnum to help deal with dangling branches. */
209 c->replay_sqnum = r->sqnum;
210
211 if (r->flags & REPLAY_REF)
212 err = set_bud_lprops(c, r);
213 else if (is_hash_key(c, &r->key)) {
214 if (deletion)
215 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
216 else
217 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
218 r->len, &r->nm);
219 } else {
220 if (deletion)
221 switch (key_type(c, &r->key)) {
222 case UBIFS_INO_KEY:
223 {
224 ino_t inum = key_inum(c, &r->key);
225
226 err = ubifs_tnc_remove_ino(c, inum);
227 break;
228 }
229 case UBIFS_TRUN_KEY:
230 err = trun_remove_range(c, r);
231 break;
232 default:
233 err = ubifs_tnc_remove(c, &r->key);
234 break;
235 }
236 else
237 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
238 r->len);
239 if (err)
240 return err;
241
242 if (c->need_recovery)
243 err = ubifs_recover_size_accum(c, &r->key, deletion,
244 r->new_size);
245 }
246
247 return err;
248}
249
250/**
251 * destroy_replay_tree - destroy the replay.
252 * @c: UBIFS file-system description object
253 *
254 * Destroy the replay tree.
255 */
256static void destroy_replay_tree(struct ubifs_info *c)
257{
258 struct rb_node *this = c->replay_tree.rb_node;
259 struct replay_entry *r;
260
261 while (this) {
262 if (this->rb_left) {
263 this = this->rb_left;
264 continue;
265 } else if (this->rb_right) {
266 this = this->rb_right;
267 continue;
268 }
269 r = rb_entry(this, struct replay_entry, rb);
270 this = rb_parent(this);
271 if (this) {
272 if (this->rb_left == &r->rb)
273 this->rb_left = NULL;
274 else
275 this->rb_right = NULL;
276 }
277 if (is_hash_key(c, &r->key))
278 kfree(r->nm.name);
279 kfree(r);
280 }
281 c->replay_tree = RB_ROOT;
282}
283
284/**
285 * apply_replay_tree - apply the replay tree to the TNC.
286 * @c: UBIFS file-system description object
287 *
288 * Apply the replay tree.
289 * Returns zero in case of success and a negative error code in case of
290 * failure.
291 */
292static int apply_replay_tree(struct ubifs_info *c)
293{
294 struct rb_node *this = rb_first(&c->replay_tree);
295
296 while (this) {
297 struct replay_entry *r;
298 int err;
299
300 cond_resched();
301
302 r = rb_entry(this, struct replay_entry, rb);
303 err = apply_replay_entry(c, r);
304 if (err)
305 return err;
306 this = rb_next(this);
307 }
308 return 0;
309}
310
311/**
312 * insert_node - insert a node to the replay tree.
313 * @c: UBIFS file-system description object
314 * @lnum: node logical eraseblock number
315 * @offs: node offset
316 * @len: node length
317 * @key: node key
318 * @sqnum: sequence number
319 * @deletion: non-zero if this is a deletion
320 * @used: number of bytes in use in a LEB
321 * @old_size: truncation old size
322 * @new_size: truncation new size
323 *
324 * This function inserts a scanned non-direntry node to the replay tree. The
325 * replay tree is an RB-tree containing @struct replay_entry elements which are
326 * indexed by the sequence number. The replay tree is applied at the very end
327 * of the replay process. Since the tree is sorted in sequence number order,
328 * the older modifications are applied first. This function returns zero in
329 * case of success and a negative error code in case of failure.
330 */
331static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
332 union ubifs_key *key, unsigned long long sqnum,
333 int deletion, int *used, loff_t old_size,
334 loff_t new_size)
335{
336 struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
337 struct replay_entry *r;
338
339 if (key_inum(c, key) >= c->highest_inum)
340 c->highest_inum = key_inum(c, key);
341
342 dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
343 while (*p) {
344 parent = *p;
345 r = rb_entry(parent, struct replay_entry, rb);
346 if (sqnum < r->sqnum) {
347 p = &(*p)->rb_left;
348 continue;
349 } else if (sqnum > r->sqnum) {
350 p = &(*p)->rb_right;
351 continue;
352 }
353 ubifs_err("duplicate sqnum in replay");
354 return -EINVAL;
355 }
356
357 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
358 if (!r)
359 return -ENOMEM;
360
361 if (!deletion)
362 *used += ALIGN(len, 8);
363 r->lnum = lnum;
364 r->offs = offs;
365 r->len = len;
366 r->sqnum = sqnum;
367 r->flags = (deletion ? REPLAY_DELETION : 0);
368 r->old_size = old_size;
369 r->new_size = new_size;
370 key_copy(c, key, &r->key);
371
372 rb_link_node(&r->rb, parent, p);
373 rb_insert_color(&r->rb, &c->replay_tree);
374 return 0;
375}
376
377/**
378 * insert_dent - insert a directory entry node into the replay tree.
379 * @c: UBIFS file-system description object
380 * @lnum: node logical eraseblock number
381 * @offs: node offset
382 * @len: node length
383 * @key: node key
384 * @name: directory entry name
385 * @nlen: directory entry name length
386 * @sqnum: sequence number
387 * @deletion: non-zero if this is a deletion
388 * @used: number of bytes in use in a LEB
389 *
390 * This function inserts a scanned directory entry node to the replay tree.
391 * Returns zero in case of success and a negative error code in case of
392 * failure.
393 *
394 * This function is also used for extended attribute entries because they are
395 * implemented as directory entry nodes.
396 */
397static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
398 union ubifs_key *key, const char *name, int nlen,
399 unsigned long long sqnum, int deletion, int *used)
400{
401 struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
402 struct replay_entry *r;
403 char *nbuf;
404
405 if (key_inum(c, key) >= c->highest_inum)
406 c->highest_inum = key_inum(c, key);
407
408 dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
409 while (*p) {
410 parent = *p;
411 r = rb_entry(parent, struct replay_entry, rb);
412 if (sqnum < r->sqnum) {
413 p = &(*p)->rb_left;
414 continue;
415 }
416 if (sqnum > r->sqnum) {
417 p = &(*p)->rb_right;
418 continue;
419 }
420 ubifs_err("duplicate sqnum in replay");
421 return -EINVAL;
422 }
423
424 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
425 if (!r)
426 return -ENOMEM;
427 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
428 if (!nbuf) {
429 kfree(r);
430 return -ENOMEM;
431 }
432
433 if (!deletion)
434 *used += ALIGN(len, 8);
435 r->lnum = lnum;
436 r->offs = offs;
437 r->len = len;
438 r->sqnum = sqnum;
439 r->nm.len = nlen;
440 memcpy(nbuf, name, nlen);
441 nbuf[nlen] = '\0';
442 r->nm.name = nbuf;
443 r->flags = (deletion ? REPLAY_DELETION : 0);
444 key_copy(c, key, &r->key);
445
446 ubifs_assert(!*p);
447 rb_link_node(&r->rb, parent, p);
448 rb_insert_color(&r->rb, &c->replay_tree);
449 return 0;
450}
451
452/**
453 * ubifs_validate_entry - validate directory or extended attribute entry node.
454 * @c: UBIFS file-system description object
455 * @dent: the node to validate
456 *
457 * This function validates directory or extended attribute entry node @dent.
458 * Returns zero if the node is all right and a %-EINVAL if not.
459 */
460int ubifs_validate_entry(struct ubifs_info *c,
461 const struct ubifs_dent_node *dent)
462{
463 int key_type = key_type_flash(c, dent->key);
464 int nlen = le16_to_cpu(dent->nlen);
465
466 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
467 dent->type >= UBIFS_ITYPES_CNT ||
468 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
469 strnlen(dent->name, nlen) != nlen ||
470 le64_to_cpu(dent->inum) > MAX_INUM) {
471 ubifs_err("bad %s node", key_type == UBIFS_DENT_KEY ?
472 "directory entry" : "extended attribute entry");
473 return -EINVAL;
474 }
475
476 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
477 ubifs_err("bad key type %d", key_type);
478 return -EINVAL;
479 }
480
481 return 0;
482}
483
484/**
485 * replay_bud - replay a bud logical eraseblock.
486 * @c: UBIFS file-system description object
487 * @lnum: bud logical eraseblock number to replay
488 * @offs: bud start offset
489 * @jhead: journal head to which this bud belongs
490 * @free: amount of free space in the bud is returned here
491 * @dirty: amount of dirty space from padding and deletion nodes is returned
492 * here
493 *
494 * This function returns zero in case of success and a negative error code in
495 * case of failure.
496 */
497static int replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
498 int *free, int *dirty)
499{
500 int err = 0, used = 0;
501 struct ubifs_scan_leb *sleb;
502 struct ubifs_scan_node *snod;
503 struct ubifs_bud *bud;
504
505 dbg_mnt("replay bud LEB %d, head %d", lnum, jhead);
506 if (c->need_recovery)
507 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, jhead != GCHD);
508 else
509 sleb = ubifs_scan(c, lnum, offs, c->sbuf);
510 if (IS_ERR(sleb))
511 return PTR_ERR(sleb);
512
513 /*
514 * The bud does not have to start from offset zero - the beginning of
515 * the 'lnum' LEB may contain previously committed data. One of the
516 * things we have to do in replay is to correctly update lprops with
517 * newer information about this LEB.
518 *
519 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
520 * bytes of free space because it only contain information about
521 * committed data.
522 *
523 * But we know that real amount of free space is 'c->leb_size -
524 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
525 * 'sleb->endpt' is used by bud data. We have to correctly calculate
526 * how much of these data are dirty and update lprops with this
527 * information.
528 *
529 * The dirt in that LEB region is comprised of padding nodes, deletion
530 * nodes, truncation nodes and nodes which are obsoleted by subsequent
531 * nodes in this LEB. So instead of calculating clean space, we
532 * calculate used space ('used' variable).
533 */
534
535 list_for_each_entry(snod, &sleb->nodes, list) {
536 int deletion = 0;
537
538 cond_resched();
539
540 if (snod->sqnum >= SQNUM_WATERMARK) {
541 ubifs_err("file system's life ended");
542 goto out_dump;
543 }
544
545 if (snod->sqnum > c->max_sqnum)
546 c->max_sqnum = snod->sqnum;
547
548 switch (snod->type) {
549 case UBIFS_INO_NODE:
550 {
551 struct ubifs_ino_node *ino = snod->node;
552 loff_t new_size = le64_to_cpu(ino->size);
553
554 if (le32_to_cpu(ino->nlink) == 0)
555 deletion = 1;
556 err = insert_node(c, lnum, snod->offs, snod->len,
557 &snod->key, snod->sqnum, deletion,
558 &used, 0, new_size);
559 break;
560 }
561 case UBIFS_DATA_NODE:
562 {
563 struct ubifs_data_node *dn = snod->node;
564 loff_t new_size = le32_to_cpu(dn->size) +
565 key_block(c, &snod->key) *
566 UBIFS_BLOCK_SIZE;
567
568 err = insert_node(c, lnum, snod->offs, snod->len,
569 &snod->key, snod->sqnum, deletion,
570 &used, 0, new_size);
571 break;
572 }
573 case UBIFS_DENT_NODE:
574 case UBIFS_XENT_NODE:
575 {
576 struct ubifs_dent_node *dent = snod->node;
577
578 err = ubifs_validate_entry(c, dent);
579 if (err)
580 goto out_dump;
581
582 err = insert_dent(c, lnum, snod->offs, snod->len,
583 &snod->key, dent->name,
584 le16_to_cpu(dent->nlen), snod->sqnum,
585 !le64_to_cpu(dent->inum), &used);
586 break;
587 }
588 case UBIFS_TRUN_NODE:
589 {
590 struct ubifs_trun_node *trun = snod->node;
591 loff_t old_size = le64_to_cpu(trun->old_size);
592 loff_t new_size = le64_to_cpu(trun->new_size);
593 union ubifs_key key;
594
595 /* Validate truncation node */
596 if (old_size < 0 || old_size > c->max_inode_sz ||
597 new_size < 0 || new_size > c->max_inode_sz ||
598 old_size <= new_size) {
599 ubifs_err("bad truncation node");
600 goto out_dump;
601 }
602
603 /*
604 * Create a fake truncation key just to use the same
605 * functions which expect nodes to have keys.
606 */
607 trun_key_init(c, &key, le32_to_cpu(trun->inum));
608 err = insert_node(c, lnum, snod->offs, snod->len,
609 &key, snod->sqnum, 1, &used,
610 old_size, new_size);
611 break;
612 }
613 default:
614 ubifs_err("unexpected node type %d in bud LEB %d:%d",
615 snod->type, lnum, snod->offs);
616 err = -EINVAL;
617 goto out_dump;
618 }
619 if (err)
620 goto out;
621 }
622
623 bud = ubifs_search_bud(c, lnum);
624 if (!bud)
625 BUG();
626
627 ubifs_assert(sleb->endpt - offs >= used);
628 ubifs_assert(sleb->endpt % c->min_io_size == 0);
629
630 if (sleb->endpt + c->min_io_size <= c->leb_size &&
631 !(c->vfs_sb->s_flags & MS_RDONLY))
632 err = ubifs_wbuf_seek_nolock(&c->jheads[jhead].wbuf, lnum,
633 sleb->endpt, UBI_SHORTTERM);
634
635 *dirty = sleb->endpt - offs - used;
636 *free = c->leb_size - sleb->endpt;
637
638out:
639 ubifs_scan_destroy(sleb);
640 return err;
641
642out_dump:
643 ubifs_err("bad node is at LEB %d:%d", lnum, snod->offs);
644 dbg_dump_node(c, snod->node);
645 ubifs_scan_destroy(sleb);
646 return -EINVAL;
647}
648
649/**
650 * insert_ref_node - insert a reference node to the replay tree.
651 * @c: UBIFS file-system description object
652 * @lnum: node logical eraseblock number
653 * @offs: node offset
654 * @sqnum: sequence number
655 * @free: amount of free space in bud
656 * @dirty: amount of dirty space from padding and deletion nodes
657 *
658 * This function inserts a reference node to the replay tree and returns zero
659 * in case of success ort a negative error code in case of failure.
660 */
661static int insert_ref_node(struct ubifs_info *c, int lnum, int offs,
662 unsigned long long sqnum, int free, int dirty)
663{
664 struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
665 struct replay_entry *r;
666
667 dbg_mnt("add ref LEB %d:%d", lnum, offs);
668 while (*p) {
669 parent = *p;
670 r = rb_entry(parent, struct replay_entry, rb);
671 if (sqnum < r->sqnum) {
672 p = &(*p)->rb_left;
673 continue;
674 } else if (sqnum > r->sqnum) {
675 p = &(*p)->rb_right;
676 continue;
677 }
678 ubifs_err("duplicate sqnum in replay tree");
679 return -EINVAL;
680 }
681
682 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
683 if (!r)
684 return -ENOMEM;
685
686 r->lnum = lnum;
687 r->offs = offs;
688 r->sqnum = sqnum;
689 r->flags = REPLAY_REF;
690 r->free = free;
691 r->dirty = dirty;
692
693 rb_link_node(&r->rb, parent, p);
694 rb_insert_color(&r->rb, &c->replay_tree);
695 return 0;
696}
697
698/**
699 * replay_buds - replay all buds.
700 * @c: UBIFS file-system description object
701 *
702 * This function returns zero in case of success and a negative error code in
703 * case of failure.
704 */
705static int replay_buds(struct ubifs_info *c)
706{
707 struct bud_entry *b;
708 int err, uninitialized_var(free), uninitialized_var(dirty);
709
710 list_for_each_entry(b, &c->replay_buds, list) {
711 err = replay_bud(c, b->bud->lnum, b->bud->start, b->bud->jhead,
712 &free, &dirty);
713 if (err)
714 return err;
715 err = insert_ref_node(c, b->bud->lnum, b->bud->start, b->sqnum,
716 free, dirty);
717 if (err)
718 return err;
719 }
720
721 return 0;
722}
723
724/**
725 * destroy_bud_list - destroy the list of buds to replay.
726 * @c: UBIFS file-system description object
727 */
728static void destroy_bud_list(struct ubifs_info *c)
729{
730 struct bud_entry *b;
731
732 while (!list_empty(&c->replay_buds)) {
733 b = list_entry(c->replay_buds.next, struct bud_entry, list);
734 list_del(&b->list);
735 kfree(b);
736 }
737}
738
739/**
740 * add_replay_bud - add a bud to the list of buds to replay.
741 * @c: UBIFS file-system description object
742 * @lnum: bud logical eraseblock number to replay
743 * @offs: bud start offset
744 * @jhead: journal head to which this bud belongs
745 * @sqnum: reference node sequence number
746 *
747 * This function returns zero in case of success and a negative error code in
748 * case of failure.
749 */
750static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
751 unsigned long long sqnum)
752{
753 struct ubifs_bud *bud;
754 struct bud_entry *b;
755
756 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
757
758 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
759 if (!bud)
760 return -ENOMEM;
761
762 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
763 if (!b) {
764 kfree(bud);
765 return -ENOMEM;
766 }
767
768 bud->lnum = lnum;
769 bud->start = offs;
770 bud->jhead = jhead;
771 ubifs_add_bud(c, bud);
772
773 b->bud = bud;
774 b->sqnum = sqnum;
775 list_add_tail(&b->list, &c->replay_buds);
776
777 return 0;
778}
779
780/**
781 * validate_ref - validate a reference node.
782 * @c: UBIFS file-system description object
783 * @ref: the reference node to validate
784 * @ref_lnum: LEB number of the reference node
785 * @ref_offs: reference node offset
786 *
787 * This function returns %1 if a bud reference already exists for the LEB. %0 is
788 * returned if the reference node is new, otherwise %-EINVAL is returned if
789 * validation failed.
790 */
791static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
792{
793 struct ubifs_bud *bud;
794 int lnum = le32_to_cpu(ref->lnum);
795 unsigned int offs = le32_to_cpu(ref->offs);
796 unsigned int jhead = le32_to_cpu(ref->jhead);
797
798 /*
799 * ref->offs may point to the end of LEB when the journal head points
800 * to the end of LEB and we write reference node for it during commit.
801 * So this is why we require 'offs > c->leb_size'.
802 */
803 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
804 lnum < c->main_first || offs > c->leb_size ||
805 offs & (c->min_io_size - 1))
806 return -EINVAL;
807
808 /* Make sure we have not already looked at this bud */
809 bud = ubifs_search_bud(c, lnum);
810 if (bud) {
811 if (bud->jhead == jhead && bud->start <= offs)
812 return 1;
813 ubifs_err("bud at LEB %d:%d was already referred", lnum, offs);
814 return -EINVAL;
815 }
816
817 return 0;
818}
819
820/**
821 * replay_log_leb - replay a log logical eraseblock.
822 * @c: UBIFS file-system description object
823 * @lnum: log logical eraseblock to replay
824 * @offs: offset to start replaying from
825 * @sbuf: scan buffer
826 *
827 * This function replays a log LEB and returns zero in case of success, %1 if
828 * this is the last LEB in the log, and a negative error code in case of
829 * failure.
830 */
831static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
832{
833 int err;
834 struct ubifs_scan_leb *sleb;
835 struct ubifs_scan_node *snod;
836 const struct ubifs_cs_node *node;
837
838 dbg_mnt("replay log LEB %d:%d", lnum, offs);
839 sleb = ubifs_scan(c, lnum, offs, sbuf);
840 if (IS_ERR(sleb)) {
841 if (c->need_recovery)
842 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
843 if (IS_ERR(sleb))
844 return PTR_ERR(sleb);
845 }
846
847 if (sleb->nodes_cnt == 0) {
848 err = 1;
849 goto out;
850 }
851
852 node = sleb->buf;
853
854 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
855 if (c->cs_sqnum == 0) {
856 /*
857 * This is the first log LEB we are looking at, make sure that
858 * the first node is a commit start node. Also record its
859 * sequence number so that UBIFS can determine where the log
860 * ends, because all nodes which were have higher sequence
861 * numbers.
862 */
863 if (snod->type != UBIFS_CS_NODE) {
864 dbg_err("first log node at LEB %d:%d is not CS node",
865 lnum, offs);
866 goto out_dump;
867 }
868 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
869 dbg_err("first CS node at LEB %d:%d has wrong "
870 "commit number %llu expected %llu",
871 lnum, offs,
872 (unsigned long long)le64_to_cpu(node->cmt_no),
873 c->cmt_no);
874 goto out_dump;
875 }
876
877 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
878 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
879 }
880
881 if (snod->sqnum < c->cs_sqnum) {
882 /*
883 * This means that we reached end of log and now
884 * look to the older log data, which was already
885 * committed but the eraseblock was not erased (UBIFS
886 * only unmaps it). So this basically means we have to
887 * exit with "end of log" code.
888 */
889 err = 1;
890 goto out;
891 }
892
893 /* Make sure the first node sits at offset zero of the LEB */
894 if (snod->offs != 0) {
895 dbg_err("first node is not at zero offset");
896 goto out_dump;
897 }
898
899 list_for_each_entry(snod, &sleb->nodes, list) {
900
901 cond_resched();
902
903 if (snod->sqnum >= SQNUM_WATERMARK) {
904 ubifs_err("file system's life ended");
905 goto out_dump;
906 }
907
908 if (snod->sqnum < c->cs_sqnum) {
909 dbg_err("bad sqnum %llu, commit sqnum %llu",
910 snod->sqnum, c->cs_sqnum);
911 goto out_dump;
912 }
913
914 if (snod->sqnum > c->max_sqnum)
915 c->max_sqnum = snod->sqnum;
916
917 switch (snod->type) {
918 case UBIFS_REF_NODE: {
919 const struct ubifs_ref_node *ref = snod->node;
920
921 err = validate_ref(c, ref);
922 if (err == 1)
923 break; /* Already have this bud */
924 if (err)
925 goto out_dump;
926
927 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
928 le32_to_cpu(ref->offs),
929 le32_to_cpu(ref->jhead),
930 snod->sqnum);
931 if (err)
932 goto out;
933
934 break;
935 }
936 case UBIFS_CS_NODE:
937 /* Make sure it sits at the beginning of LEB */
938 if (snod->offs != 0) {
939 ubifs_err("unexpected node in log");
940 goto out_dump;
941 }
942 break;
943 default:
944 ubifs_err("unexpected node in log");
945 goto out_dump;
946 }
947 }
948
949 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
950 c->lhead_lnum = lnum;
951 c->lhead_offs = sleb->endpt;
952 }
953
954 err = !sleb->endpt;
955out:
956 ubifs_scan_destroy(sleb);
957 return err;
958
959out_dump:
960 ubifs_err("log error detected while replying the log at LEB %d:%d",
961 lnum, offs + snod->offs);
962 dbg_dump_node(c, snod->node);
963 ubifs_scan_destroy(sleb);
964 return -EINVAL;
965}
966
967/**
968 * take_ihead - update the status of the index head in lprops to 'taken'.
969 * @c: UBIFS file-system description object
970 *
971 * This function returns the amount of free space in the index head LEB or a
972 * negative error code.
973 */
974static int take_ihead(struct ubifs_info *c)
975{
976 const struct ubifs_lprops *lp;
977 int err, free;
978
979 ubifs_get_lprops(c);
980
981 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
982 if (IS_ERR(lp)) {
983 err = PTR_ERR(lp);
984 goto out;
985 }
986
987 free = lp->free;
988
989 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
990 lp->flags | LPROPS_TAKEN, 0);
991 if (IS_ERR(lp)) {
992 err = PTR_ERR(lp);
993 goto out;
994 }
995
996 err = free;
997out:
998 ubifs_release_lprops(c);
999 return err;
1000}
1001
1002/**
1003 * ubifs_replay_journal - replay journal.
1004 * @c: UBIFS file-system description object
1005 *
1006 * This function scans the journal, replays and cleans it up. It makes sure all
1007 * memory data structures related to uncommitted journal are built (dirty TNC
1008 * tree, tree of buds, modified lprops, etc).
1009 */
1010int ubifs_replay_journal(struct ubifs_info *c)
1011{
1012 int err, i, lnum, offs, free;
1013 void *sbuf = NULL;
1014
1015 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1016
1017 /* Update the status of the index head in lprops to 'taken' */
1018 free = take_ihead(c);
1019 if (free < 0)
1020 return free; /* Error code */
1021
1022 if (c->ihead_offs != c->leb_size - free) {
1023 ubifs_err("bad index head LEB %d:%d", c->ihead_lnum,
1024 c->ihead_offs);
1025 return -EINVAL;
1026 }
1027
1028 sbuf = vmalloc(c->leb_size);
1029 if (!sbuf)
1030 return -ENOMEM;
1031
1032 dbg_mnt("start replaying the journal");
1033
1034 c->replaying = 1;
1035
1036 lnum = c->ltail_lnum = c->lhead_lnum;
1037 offs = c->lhead_offs;
1038
1039 for (i = 0; i < c->log_lebs; i++, lnum++) {
1040 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) {
1041 /*
1042 * The log is logically circular, we reached the last
1043 * LEB, switch to the first one.
1044 */
1045 lnum = UBIFS_LOG_LNUM;
1046 offs = 0;
1047 }
1048 err = replay_log_leb(c, lnum, offs, sbuf);
1049 if (err == 1)
1050 /* We hit the end of the log */
1051 break;
1052 if (err)
1053 goto out;
1054 offs = 0;
1055 }
1056
1057 err = replay_buds(c);
1058 if (err)
1059 goto out;
1060
1061 err = apply_replay_tree(c);
1062 if (err)
1063 goto out;
1064
1065 ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1066 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
1067 "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1068 c->highest_inum);
1069out:
1070 destroy_replay_tree(c);
1071 destroy_bud_list(c);
1072 vfree(sbuf);
1073 c->replaying = 0;
1074 return err;
1075}
diff --git a/fs/ubifs/sb.c b/fs/ubifs/sb.c
new file mode 100644
index 000000000000..2bf753b38889
--- /dev/null
+++ b/fs/ubifs/sb.c
@@ -0,0 +1,629 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements UBIFS superblock. The superblock is stored at the first
25 * LEB of the volume and is never changed by UBIFS. Only user-space tools may
26 * change it. The superblock node mostly contains geometry information.
27 */
28
29#include "ubifs.h"
30#include <linux/random.h>
31
32/*
33 * Default journal size in logical eraseblocks as a percent of total
34 * flash size.
35 */
36#define DEFAULT_JNL_PERCENT 5
37
38/* Default maximum journal size in bytes */
39#define DEFAULT_MAX_JNL (32*1024*1024)
40
41/* Default indexing tree fanout */
42#define DEFAULT_FANOUT 8
43
44/* Default number of data journal heads */
45#define DEFAULT_JHEADS_CNT 1
46
47/* Default positions of different LEBs in the main area */
48#define DEFAULT_IDX_LEB 0
49#define DEFAULT_DATA_LEB 1
50#define DEFAULT_GC_LEB 2
51
52/* Default number of LEB numbers in LPT's save table */
53#define DEFAULT_LSAVE_CNT 256
54
55/* Default reserved pool size as a percent of maximum free space */
56#define DEFAULT_RP_PERCENT 5
57
58/* The default maximum size of reserved pool in bytes */
59#define DEFAULT_MAX_RP_SIZE (5*1024*1024)
60
61/* Default time granularity in nanoseconds */
62#define DEFAULT_TIME_GRAN 1000000000
63
64/**
65 * create_default_filesystem - format empty UBI volume.
66 * @c: UBIFS file-system description object
67 *
68 * This function creates default empty file-system. Returns zero in case of
69 * success and a negative error code in case of failure.
70 */
71static int create_default_filesystem(struct ubifs_info *c)
72{
73 struct ubifs_sb_node *sup;
74 struct ubifs_mst_node *mst;
75 struct ubifs_idx_node *idx;
76 struct ubifs_branch *br;
77 struct ubifs_ino_node *ino;
78 struct ubifs_cs_node *cs;
79 union ubifs_key key;
80 int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
81 int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
82 int min_leb_cnt = UBIFS_MIN_LEB_CNT;
83 uint64_t tmp64, main_bytes;
84
85 /* Some functions called from here depend on the @c->key_len filed */
86 c->key_len = UBIFS_SK_LEN;
87
88 /*
89 * First of all, we have to calculate default file-system geometry -
90 * log size, journal size, etc.
91 */
92 if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
93 /* We can first multiply then divide and have no overflow */
94 jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
95 else
96 jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
97
98 if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
99 jnl_lebs = UBIFS_MIN_JNL_LEBS;
100 if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
101 jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
102
103 /*
104 * The log should be large enough to fit reference nodes for all bud
105 * LEBs. Because buds do not have to start from the beginning of LEBs
106 * (half of the LEB may contain committed data), the log should
107 * generally be larger, make it twice as large.
108 */
109 tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
110 log_lebs = tmp / c->leb_size;
111 /* Plus one LEB reserved for commit */
112 log_lebs += 1;
113 if (c->leb_cnt - min_leb_cnt > 8) {
114 /* And some extra space to allow writes while committing */
115 log_lebs += 1;
116 min_leb_cnt += 1;
117 }
118
119 max_buds = jnl_lebs - log_lebs;
120 if (max_buds < UBIFS_MIN_BUD_LEBS)
121 max_buds = UBIFS_MIN_BUD_LEBS;
122
123 /*
124 * Orphan nodes are stored in a separate area. One node can store a lot
125 * of orphan inode numbers, but when new orphan comes we just add a new
126 * orphan node. At some point the nodes are consolidated into one
127 * orphan node.
128 */
129 orph_lebs = UBIFS_MIN_ORPH_LEBS;
130#ifdef CONFIG_UBIFS_FS_DEBUG
131 if (c->leb_cnt - min_leb_cnt > 1)
132 /*
133 * For debugging purposes it is better to have at least 2
134 * orphan LEBs, because the orphan subsystem would need to do
135 * consolidations and would be stressed more.
136 */
137 orph_lebs += 1;
138#endif
139
140 main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
141 main_lebs -= orph_lebs;
142
143 lpt_first = UBIFS_LOG_LNUM + log_lebs;
144 c->lsave_cnt = DEFAULT_LSAVE_CNT;
145 c->max_leb_cnt = c->leb_cnt;
146 err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
147 &big_lpt);
148 if (err)
149 return err;
150
151 dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
152 lpt_first + lpt_lebs - 1);
153
154 main_first = c->leb_cnt - main_lebs;
155
156 /* Create default superblock */
157 tmp = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
158 sup = kzalloc(tmp, GFP_KERNEL);
159 if (!sup)
160 return -ENOMEM;
161
162 tmp64 = (uint64_t)max_buds * c->leb_size;
163 if (big_lpt)
164 sup_flags |= UBIFS_FLG_BIGLPT;
165
166 sup->ch.node_type = UBIFS_SB_NODE;
167 sup->key_hash = UBIFS_KEY_HASH_R5;
168 sup->flags = cpu_to_le32(sup_flags);
169 sup->min_io_size = cpu_to_le32(c->min_io_size);
170 sup->leb_size = cpu_to_le32(c->leb_size);
171 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
172 sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
173 sup->max_bud_bytes = cpu_to_le64(tmp64);
174 sup->log_lebs = cpu_to_le32(log_lebs);
175 sup->lpt_lebs = cpu_to_le32(lpt_lebs);
176 sup->orph_lebs = cpu_to_le32(orph_lebs);
177 sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
178 sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
179 sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
180 sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION);
181 sup->default_compr = cpu_to_le16(UBIFS_COMPR_LZO);
182 sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
183
184 generate_random_uuid(sup->uuid);
185
186 main_bytes = (uint64_t)main_lebs * c->leb_size;
187 tmp64 = main_bytes * DEFAULT_RP_PERCENT;
188 do_div(tmp64, 100);
189 if (tmp64 > DEFAULT_MAX_RP_SIZE)
190 tmp64 = DEFAULT_MAX_RP_SIZE;
191 sup->rp_size = cpu_to_le64(tmp64);
192
193 err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0, UBI_LONGTERM);
194 kfree(sup);
195 if (err)
196 return err;
197
198 dbg_gen("default superblock created at LEB 0:0");
199
200 /* Create default master node */
201 mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
202 if (!mst)
203 return -ENOMEM;
204
205 mst->ch.node_type = UBIFS_MST_NODE;
206 mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
207 mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
208 mst->cmt_no = 0;
209 mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
210 mst->root_offs = 0;
211 tmp = ubifs_idx_node_sz(c, 1);
212 mst->root_len = cpu_to_le32(tmp);
213 mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
214 mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
215 mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
216 mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
217 mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
218 mst->lpt_offs = cpu_to_le32(c->lpt_offs);
219 mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
220 mst->nhead_offs = cpu_to_le32(c->nhead_offs);
221 mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
222 mst->ltab_offs = cpu_to_le32(c->ltab_offs);
223 mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
224 mst->lsave_offs = cpu_to_le32(c->lsave_offs);
225 mst->lscan_lnum = cpu_to_le32(main_first);
226 mst->empty_lebs = cpu_to_le32(main_lebs - 2);
227 mst->idx_lebs = cpu_to_le32(1);
228 mst->leb_cnt = cpu_to_le32(c->leb_cnt);
229
230 /* Calculate lprops statistics */
231 tmp64 = main_bytes;
232 tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
233 tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
234 mst->total_free = cpu_to_le64(tmp64);
235
236 tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
237 ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
238 UBIFS_INO_NODE_SZ;
239 tmp64 += ino_waste;
240 tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
241 mst->total_dirty = cpu_to_le64(tmp64);
242
243 /* The indexing LEB does not contribute to dark space */
244 tmp64 = (c->main_lebs - 1) * c->dark_wm;
245 mst->total_dark = cpu_to_le64(tmp64);
246
247 mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
248
249 err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
250 UBI_UNKNOWN);
251 if (err) {
252 kfree(mst);
253 return err;
254 }
255 err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1, 0,
256 UBI_UNKNOWN);
257 kfree(mst);
258 if (err)
259 return err;
260
261 dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
262
263 /* Create the root indexing node */
264 tmp = ubifs_idx_node_sz(c, 1);
265 idx = kzalloc(ALIGN(tmp, c->min_io_size), GFP_KERNEL);
266 if (!idx)
267 return -ENOMEM;
268
269 c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
270 c->key_hash = key_r5_hash;
271
272 idx->ch.node_type = UBIFS_IDX_NODE;
273 idx->child_cnt = cpu_to_le16(1);
274 ino_key_init(c, &key, UBIFS_ROOT_INO);
275 br = ubifs_idx_branch(c, idx, 0);
276 key_write_idx(c, &key, &br->key);
277 br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
278 br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
279 err = ubifs_write_node(c, idx, tmp, main_first + DEFAULT_IDX_LEB, 0,
280 UBI_UNKNOWN);
281 kfree(idx);
282 if (err)
283 return err;
284
285 dbg_gen("default root indexing node created LEB %d:0",
286 main_first + DEFAULT_IDX_LEB);
287
288 /* Create default root inode */
289 tmp = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
290 ino = kzalloc(tmp, GFP_KERNEL);
291 if (!ino)
292 return -ENOMEM;
293
294 ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
295 ino->ch.node_type = UBIFS_INO_NODE;
296 ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
297 ino->nlink = cpu_to_le32(2);
298 tmp = cpu_to_le64(CURRENT_TIME_SEC.tv_sec);
299 ino->atime_sec = tmp;
300 ino->ctime_sec = tmp;
301 ino->mtime_sec = tmp;
302 ino->atime_nsec = 0;
303 ino->ctime_nsec = 0;
304 ino->mtime_nsec = 0;
305 ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
306 ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
307
308 /* Set compression enabled by default */
309 ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
310
311 err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
312 main_first + DEFAULT_DATA_LEB, 0,
313 UBI_UNKNOWN);
314 kfree(ino);
315 if (err)
316 return err;
317
318 dbg_gen("root inode created at LEB %d:0",
319 main_first + DEFAULT_DATA_LEB);
320
321 /*
322 * The first node in the log has to be the commit start node. This is
323 * always the case during normal file-system operation. Write a fake
324 * commit start node to the log.
325 */
326 tmp = ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size);
327 cs = kzalloc(tmp, GFP_KERNEL);
328 if (!cs)
329 return -ENOMEM;
330
331 cs->ch.node_type = UBIFS_CS_NODE;
332 err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM,
333 0, UBI_UNKNOWN);
334 kfree(cs);
335
336 ubifs_msg("default file-system created");
337 return 0;
338}
339
340/**
341 * validate_sb - validate superblock node.
342 * @c: UBIFS file-system description object
343 * @sup: superblock node
344 *
345 * This function validates superblock node @sup. Since most of data was read
346 * from the superblock and stored in @c, the function validates fields in @c
347 * instead. Returns zero in case of success and %-EINVAL in case of validation
348 * failure.
349 */
350static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
351{
352 long long max_bytes;
353 int err = 1, min_leb_cnt;
354
355 if (!c->key_hash) {
356 err = 2;
357 goto failed;
358 }
359
360 if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
361 err = 3;
362 goto failed;
363 }
364
365 if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
366 ubifs_err("min. I/O unit mismatch: %d in superblock, %d real",
367 le32_to_cpu(sup->min_io_size), c->min_io_size);
368 goto failed;
369 }
370
371 if (le32_to_cpu(sup->leb_size) != c->leb_size) {
372 ubifs_err("LEB size mismatch: %d in superblock, %d real",
373 le32_to_cpu(sup->leb_size), c->leb_size);
374 goto failed;
375 }
376
377 if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
378 c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
379 c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
380 c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
381 err = 4;
382 goto failed;
383 }
384
385 /*
386 * Calculate minimum allowed amount of main area LEBs. This is very
387 * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
388 * have just read from the superblock.
389 */
390 min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
391 min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
392
393 if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
394 ubifs_err("bad LEB count: %d in superblock, %d on UBI volume, "
395 "%d minimum required", c->leb_cnt, c->vi.size,
396 min_leb_cnt);
397 goto failed;
398 }
399
400 if (c->max_leb_cnt < c->leb_cnt) {
401 ubifs_err("max. LEB count %d less than LEB count %d",
402 c->max_leb_cnt, c->leb_cnt);
403 goto failed;
404 }
405
406 if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
407 err = 7;
408 goto failed;
409 }
410
411 if (c->max_bud_bytes < (long long)c->leb_size * UBIFS_MIN_BUD_LEBS ||
412 c->max_bud_bytes > (long long)c->leb_size * c->main_lebs) {
413 err = 8;
414 goto failed;
415 }
416
417 if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
418 c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
419 err = 9;
420 goto failed;
421 }
422
423 if (c->fanout < UBIFS_MIN_FANOUT ||
424 ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
425 err = 10;
426 goto failed;
427 }
428
429 if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
430 c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
431 c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
432 err = 11;
433 goto failed;
434 }
435
436 if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
437 c->orph_lebs + c->main_lebs != c->leb_cnt) {
438 err = 12;
439 goto failed;
440 }
441
442 if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
443 err = 13;
444 goto failed;
445 }
446
447 max_bytes = c->main_lebs * (long long)c->leb_size;
448 if (c->rp_size < 0 || max_bytes < c->rp_size) {
449 err = 14;
450 goto failed;
451 }
452
453 if (le32_to_cpu(sup->time_gran) > 1000000000 ||
454 le32_to_cpu(sup->time_gran) < 1) {
455 err = 15;
456 goto failed;
457 }
458
459 return 0;
460
461failed:
462 ubifs_err("bad superblock, error %d", err);
463 dbg_dump_node(c, sup);
464 return -EINVAL;
465}
466
467/**
468 * ubifs_read_sb_node - read superblock node.
469 * @c: UBIFS file-system description object
470 *
471 * This function returns a pointer to the superblock node or a negative error
472 * code.
473 */
474struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
475{
476 struct ubifs_sb_node *sup;
477 int err;
478
479 sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
480 if (!sup)
481 return ERR_PTR(-ENOMEM);
482
483 err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
484 UBIFS_SB_LNUM, 0);
485 if (err) {
486 kfree(sup);
487 return ERR_PTR(err);
488 }
489
490 return sup;
491}
492
493/**
494 * ubifs_write_sb_node - write superblock node.
495 * @c: UBIFS file-system description object
496 * @sup: superblock node read with 'ubifs_read_sb_node()'
497 *
498 * This function returns %0 on success and a negative error code on failure.
499 */
500int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
501{
502 int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
503
504 ubifs_prepare_node(c, sup, UBIFS_SB_NODE_SZ, 1);
505 return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len, UBI_LONGTERM);
506}
507
508/**
509 * ubifs_read_superblock - read superblock.
510 * @c: UBIFS file-system description object
511 *
512 * This function finds, reads and checks the superblock. If an empty UBI volume
513 * is being mounted, this function creates default superblock. Returns zero in
514 * case of success, and a negative error code in case of failure.
515 */
516int ubifs_read_superblock(struct ubifs_info *c)
517{
518 int err, sup_flags;
519 struct ubifs_sb_node *sup;
520
521 if (c->empty) {
522 err = create_default_filesystem(c);
523 if (err)
524 return err;
525 }
526
527 sup = ubifs_read_sb_node(c);
528 if (IS_ERR(sup))
529 return PTR_ERR(sup);
530
531 /*
532 * The software supports all previous versions but not future versions,
533 * due to the unavailability of time-travelling equipment.
534 */
535 c->fmt_version = le32_to_cpu(sup->fmt_version);
536 if (c->fmt_version > UBIFS_FORMAT_VERSION) {
537 ubifs_err("on-flash format version is %d, but software only "
538 "supports up to version %d", c->fmt_version,
539 UBIFS_FORMAT_VERSION);
540 err = -EINVAL;
541 goto out;
542 }
543
544 if (c->fmt_version < 3) {
545 ubifs_err("on-flash format version %d is not supported",
546 c->fmt_version);
547 err = -EINVAL;
548 goto out;
549 }
550
551 switch (sup->key_hash) {
552 case UBIFS_KEY_HASH_R5:
553 c->key_hash = key_r5_hash;
554 c->key_hash_type = UBIFS_KEY_HASH_R5;
555 break;
556
557 case UBIFS_KEY_HASH_TEST:
558 c->key_hash = key_test_hash;
559 c->key_hash_type = UBIFS_KEY_HASH_TEST;
560 break;
561 };
562
563 c->key_fmt = sup->key_fmt;
564
565 switch (c->key_fmt) {
566 case UBIFS_SIMPLE_KEY_FMT:
567 c->key_len = UBIFS_SK_LEN;
568 break;
569 default:
570 ubifs_err("unsupported key format");
571 err = -EINVAL;
572 goto out;
573 }
574
575 c->leb_cnt = le32_to_cpu(sup->leb_cnt);
576 c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
577 c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
578 c->log_lebs = le32_to_cpu(sup->log_lebs);
579 c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
580 c->orph_lebs = le32_to_cpu(sup->orph_lebs);
581 c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
582 c->fanout = le32_to_cpu(sup->fanout);
583 c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
584 c->default_compr = le16_to_cpu(sup->default_compr);
585 c->rp_size = le64_to_cpu(sup->rp_size);
586 c->rp_uid = le32_to_cpu(sup->rp_uid);
587 c->rp_gid = le32_to_cpu(sup->rp_gid);
588 sup_flags = le32_to_cpu(sup->flags);
589
590 c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
591
592 memcpy(&c->uuid, &sup->uuid, 16);
593
594 c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
595
596 /* Automatically increase file system size to the maximum size */
597 c->old_leb_cnt = c->leb_cnt;
598 if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
599 c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
600 if (c->vfs_sb->s_flags & MS_RDONLY)
601 dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
602 c->old_leb_cnt, c->leb_cnt);
603 else {
604 dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
605 c->old_leb_cnt, c->leb_cnt);
606 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
607 err = ubifs_write_sb_node(c, sup);
608 if (err)
609 goto out;
610 c->old_leb_cnt = c->leb_cnt;
611 }
612 }
613
614 c->log_bytes = (long long)c->log_lebs * c->leb_size;
615 c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
616 c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
617 c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
618 c->orph_first = c->lpt_last + 1;
619 c->orph_last = c->orph_first + c->orph_lebs - 1;
620 c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
621 c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
622 c->main_first = c->leb_cnt - c->main_lebs;
623 c->report_rp_size = ubifs_reported_space(c, c->rp_size);
624
625 err = validate_sb(c, sup);
626out:
627 kfree(sup);
628 return err;
629}
diff --git a/fs/ubifs/scan.c b/fs/ubifs/scan.c
new file mode 100644
index 000000000000..acf5c5fffc60
--- /dev/null
+++ b/fs/ubifs/scan.c
@@ -0,0 +1,362 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements the scan which is a general-purpose function for
25 * determining what nodes are in an eraseblock. The scan is used to replay the
26 * journal, to do garbage collection. for the TNC in-the-gaps method, and by
27 * debugging functions.
28 */
29
30#include "ubifs.h"
31
32/**
33 * scan_padding_bytes - scan for padding bytes.
34 * @buf: buffer to scan
35 * @len: length of buffer
36 *
37 * This function returns the number of padding bytes on success and
38 * %SCANNED_GARBAGE on failure.
39 */
40static int scan_padding_bytes(void *buf, int len)
41{
42 int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
43 uint8_t *p = buf;
44
45 dbg_scan("not a node");
46
47 while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
48 pad_len += 1;
49
50 if (!pad_len || (pad_len & 7))
51 return SCANNED_GARBAGE;
52
53 dbg_scan("%d padding bytes", pad_len);
54
55 return pad_len;
56}
57
58/**
59 * ubifs_scan_a_node - scan for a node or padding.
60 * @c: UBIFS file-system description object
61 * @buf: buffer to scan
62 * @len: length of buffer
63 * @lnum: logical eraseblock number
64 * @offs: offset within the logical eraseblock
65 * @quiet: print no messages
66 *
67 * This function returns a scanning code to indicate what was scanned.
68 */
69int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
70 int offs, int quiet)
71{
72 struct ubifs_ch *ch = buf;
73 uint32_t magic;
74
75 magic = le32_to_cpu(ch->magic);
76
77 if (magic == 0xFFFFFFFF) {
78 dbg_scan("hit empty space");
79 return SCANNED_EMPTY_SPACE;
80 }
81
82 if (magic != UBIFS_NODE_MAGIC)
83 return scan_padding_bytes(buf, len);
84
85 if (len < UBIFS_CH_SZ)
86 return SCANNED_GARBAGE;
87
88 dbg_scan("scanning %s", dbg_ntype(ch->node_type));
89
90 if (ubifs_check_node(c, buf, lnum, offs, quiet))
91 return SCANNED_A_CORRUPT_NODE;
92
93 if (ch->node_type == UBIFS_PAD_NODE) {
94 struct ubifs_pad_node *pad = buf;
95 int pad_len = le32_to_cpu(pad->pad_len);
96 int node_len = le32_to_cpu(ch->len);
97
98 /* Validate the padding node */
99 if (pad_len < 0 ||
100 offs + node_len + pad_len > c->leb_size) {
101 if (!quiet) {
102 ubifs_err("bad pad node at LEB %d:%d",
103 lnum, offs);
104 dbg_dump_node(c, pad);
105 }
106 return SCANNED_A_BAD_PAD_NODE;
107 }
108
109 /* Make the node pads to 8-byte boundary */
110 if ((node_len + pad_len) & 7) {
111 if (!quiet) {
112 dbg_err("bad padding length %d - %d",
113 offs, offs + node_len + pad_len);
114 }
115 return SCANNED_A_BAD_PAD_NODE;
116 }
117
118 dbg_scan("%d bytes padded, offset now %d",
119 pad_len, ALIGN(offs + node_len + pad_len, 8));
120
121 return node_len + pad_len;
122 }
123
124 return SCANNED_A_NODE;
125}
126
127/**
128 * ubifs_start_scan - create LEB scanning information at start of scan.
129 * @c: UBIFS file-system description object
130 * @lnum: logical eraseblock number
131 * @offs: offset to start at (usually zero)
132 * @sbuf: scan buffer (must be c->leb_size)
133 *
134 * This function returns %0 on success and a negative error code on failure.
135 */
136struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
137 int offs, void *sbuf)
138{
139 struct ubifs_scan_leb *sleb;
140 int err;
141
142 dbg_scan("scan LEB %d:%d", lnum, offs);
143
144 sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
145 if (!sleb)
146 return ERR_PTR(-ENOMEM);
147
148 sleb->lnum = lnum;
149 INIT_LIST_HEAD(&sleb->nodes);
150 sleb->buf = sbuf;
151
152 err = ubi_read(c->ubi, lnum, sbuf + offs, offs, c->leb_size - offs);
153 if (err && err != -EBADMSG) {
154 ubifs_err("cannot read %d bytes from LEB %d:%d,"
155 " error %d", c->leb_size - offs, lnum, offs, err);
156 kfree(sleb);
157 return ERR_PTR(err);
158 }
159
160 if (err == -EBADMSG)
161 sleb->ecc = 1;
162
163 return sleb;
164}
165
166/**
167 * ubifs_end_scan - update LEB scanning information at end of scan.
168 * @c: UBIFS file-system description object
169 * @sleb: scanning information
170 * @lnum: logical eraseblock number
171 * @offs: offset to start at (usually zero)
172 *
173 * This function returns %0 on success and a negative error code on failure.
174 */
175void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
176 int lnum, int offs)
177{
178 lnum = lnum;
179 dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
180 ubifs_assert(offs % c->min_io_size == 0);
181
182 sleb->endpt = ALIGN(offs, c->min_io_size);
183}
184
185/**
186 * ubifs_add_snod - add a scanned node to LEB scanning information.
187 * @c: UBIFS file-system description object
188 * @sleb: scanning information
189 * @buf: buffer containing node
190 * @offs: offset of node on flash
191 *
192 * This function returns %0 on success and a negative error code on failure.
193 */
194int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
195 void *buf, int offs)
196{
197 struct ubifs_ch *ch = buf;
198 struct ubifs_ino_node *ino = buf;
199 struct ubifs_scan_node *snod;
200
201 snod = kzalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
202 if (!snod)
203 return -ENOMEM;
204
205 snod->sqnum = le64_to_cpu(ch->sqnum);
206 snod->type = ch->node_type;
207 snod->offs = offs;
208 snod->len = le32_to_cpu(ch->len);
209 snod->node = buf;
210
211 switch (ch->node_type) {
212 case UBIFS_INO_NODE:
213 case UBIFS_DENT_NODE:
214 case UBIFS_XENT_NODE:
215 case UBIFS_DATA_NODE:
216 case UBIFS_TRUN_NODE:
217 /*
218 * The key is in the same place in all keyed
219 * nodes.
220 */
221 key_read(c, &ino->key, &snod->key);
222 break;
223 }
224 list_add_tail(&snod->list, &sleb->nodes);
225 sleb->nodes_cnt += 1;
226 return 0;
227}
228
229/**
230 * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
231 * @c: UBIFS file-system description object
232 * @lnum: LEB number of corruption
233 * @offs: offset of corruption
234 * @buf: buffer containing corruption
235 */
236void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
237 void *buf)
238{
239 int len;
240
241 ubifs_err("corrupted data at LEB %d:%d", lnum, offs);
242 if (dbg_failure_mode)
243 return;
244 len = c->leb_size - offs;
245 if (len > 4096)
246 len = 4096;
247 dbg_err("first %d bytes from LEB %d:%d", len, lnum, offs);
248 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
249}
250
251/**
252 * ubifs_scan - scan a logical eraseblock.
253 * @c: UBIFS file-system description object
254 * @lnum: logical eraseblock number
255 * @offs: offset to start at (usually zero)
256 * @sbuf: scan buffer (must be c->leb_size)
257 *
258 * This function scans LEB number @lnum and returns complete information about
259 * its contents. Returns an error code in case of failure.
260 */
261struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
262 int offs, void *sbuf)
263{
264 void *buf = sbuf + offs;
265 int err, len = c->leb_size - offs;
266 struct ubifs_scan_leb *sleb;
267
268 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
269 if (IS_ERR(sleb))
270 return sleb;
271
272 while (len >= 8) {
273 struct ubifs_ch *ch = buf;
274 int node_len, ret;
275
276 dbg_scan("look at LEB %d:%d (%d bytes left)",
277 lnum, offs, len);
278
279 cond_resched();
280
281 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 0);
282
283 if (ret > 0) {
284 /* Padding bytes or a valid padding node */
285 offs += ret;
286 buf += ret;
287 len -= ret;
288 continue;
289 }
290
291 if (ret == SCANNED_EMPTY_SPACE)
292 /* Empty space is checked later */
293 break;
294
295 switch (ret) {
296 case SCANNED_GARBAGE:
297 dbg_err("garbage");
298 goto corrupted;
299 case SCANNED_A_NODE:
300 break;
301 case SCANNED_A_CORRUPT_NODE:
302 case SCANNED_A_BAD_PAD_NODE:
303 dbg_err("bad node");
304 goto corrupted;
305 default:
306 dbg_err("unknown");
307 goto corrupted;
308 }
309
310 err = ubifs_add_snod(c, sleb, buf, offs);
311 if (err)
312 goto error;
313
314 node_len = ALIGN(le32_to_cpu(ch->len), 8);
315 offs += node_len;
316 buf += node_len;
317 len -= node_len;
318 }
319
320 if (offs % c->min_io_size)
321 goto corrupted;
322
323 ubifs_end_scan(c, sleb, lnum, offs);
324
325 for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
326 if (*(uint32_t *)buf != 0xffffffff)
327 break;
328 for (; len; offs++, buf++, len--)
329 if (*(uint8_t *)buf != 0xff) {
330 ubifs_err("corrupt empty space at LEB %d:%d",
331 lnum, offs);
332 goto corrupted;
333 }
334
335 return sleb;
336
337corrupted:
338 ubifs_scanned_corruption(c, lnum, offs, buf);
339 err = -EUCLEAN;
340error:
341 ubifs_err("LEB %d scanning failed", lnum);
342 ubifs_scan_destroy(sleb);
343 return ERR_PTR(err);
344}
345
346/**
347 * ubifs_scan_destroy - destroy LEB scanning information.
348 * @sleb: scanning information to free
349 */
350void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
351{
352 struct ubifs_scan_node *node;
353 struct list_head *head;
354
355 head = &sleb->nodes;
356 while (!list_empty(head)) {
357 node = list_entry(head->next, struct ubifs_scan_node, list);
358 list_del(&node->list);
359 kfree(node);
360 }
361 kfree(sleb);
362}
diff --git a/fs/ubifs/shrinker.c b/fs/ubifs/shrinker.c
new file mode 100644
index 000000000000..f248533841a2
--- /dev/null
+++ b/fs/ubifs/shrinker.c
@@ -0,0 +1,322 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements UBIFS shrinker which evicts clean znodes from the TNC
25 * tree when Linux VM needs more RAM.
26 *
27 * We do not implement any LRU lists to find oldest znodes to free because it
28 * would add additional overhead to the file system fast paths. So the shrinker
29 * just walks the TNC tree when searching for znodes to free.
30 *
31 * If the root of a TNC sub-tree is clean and old enough, then the children are
32 * also clean and old enough. So the shrinker walks the TNC in level order and
33 * dumps entire sub-trees.
34 *
35 * The age of znodes is just the time-stamp when they were last looked at.
36 * The current shrinker first tries to evict old znodes, then young ones.
37 *
38 * Since the shrinker is global, it has to protect against races with FS
39 * un-mounts, which is done by the 'ubifs_infos_lock' and 'c->umount_mutex'.
40 */
41
42#include "ubifs.h"
43
44/* List of all UBIFS file-system instances */
45LIST_HEAD(ubifs_infos);
46
47/*
48 * We number each shrinker run and record the number on the ubifs_info structure
49 * so that we can easily work out which ubifs_info structures have already been
50 * done by the current run.
51 */
52static unsigned int shrinker_run_no;
53
54/* Protects 'ubifs_infos' list */
55DEFINE_SPINLOCK(ubifs_infos_lock);
56
57/* Global clean znode counter (for all mounted UBIFS instances) */
58atomic_long_t ubifs_clean_zn_cnt;
59
60/**
61 * shrink_tnc - shrink TNC tree.
62 * @c: UBIFS file-system description object
63 * @nr: number of znodes to free
64 * @age: the age of znodes to free
65 * @contention: if any contention, this is set to %1
66 *
67 * This function traverses TNC tree and frees clean znodes. It does not free
68 * clean znodes which younger then @age. Returns number of freed znodes.
69 */
70static int shrink_tnc(struct ubifs_info *c, int nr, int age, int *contention)
71{
72 int total_freed = 0;
73 struct ubifs_znode *znode, *zprev;
74 int time = get_seconds();
75
76 ubifs_assert(mutex_is_locked(&c->umount_mutex));
77 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
78
79 if (!c->zroot.znode || atomic_long_read(&c->clean_zn_cnt) == 0)
80 return 0;
81
82 /*
83 * Traverse the TNC tree in levelorder manner, so that it is possible
84 * to destroy large sub-trees. Indeed, if a znode is old, then all its
85 * children are older or of the same age.
86 *
87 * Note, we are holding 'c->tnc_mutex', so we do not have to lock the
88 * 'c->space_lock' when _reading_ 'c->clean_zn_cnt', because it is
89 * changed only when the 'c->tnc_mutex' is held.
90 */
91 zprev = NULL;
92 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
93 while (znode && total_freed < nr &&
94 atomic_long_read(&c->clean_zn_cnt) > 0) {
95 int freed;
96
97 /*
98 * If the znode is clean, but it is in the 'c->cnext' list, this
99 * means that this znode has just been written to flash as a
100 * part of commit and was marked clean. They will be removed
101 * from the list at end commit. We cannot change the list,
102 * because it is not protected by any mutex (design decision to
103 * make commit really independent and parallel to main I/O). So
104 * we just skip these znodes.
105 *
106 * Note, the 'clean_zn_cnt' counters are not updated until
107 * after the commit, so the UBIFS shrinker does not report
108 * the znodes which are in the 'c->cnext' list as freeable.
109 *
110 * Also note, if the root of a sub-tree is not in 'c->cnext',
111 * then the whole sub-tree is not in 'c->cnext' as well, so it
112 * is safe to dump whole sub-tree.
113 */
114
115 if (znode->cnext) {
116 /*
117 * Very soon these znodes will be removed from the list
118 * and become freeable.
119 */
120 *contention = 1;
121 } else if (!ubifs_zn_dirty(znode) &&
122 abs(time - znode->time) >= age) {
123 if (znode->parent)
124 znode->parent->zbranch[znode->iip].znode = NULL;
125 else
126 c->zroot.znode = NULL;
127
128 freed = ubifs_destroy_tnc_subtree(znode);
129 atomic_long_sub(freed, &ubifs_clean_zn_cnt);
130 atomic_long_sub(freed, &c->clean_zn_cnt);
131 ubifs_assert(atomic_long_read(&c->clean_zn_cnt) >= 0);
132 total_freed += freed;
133 znode = zprev;
134 }
135
136 if (unlikely(!c->zroot.znode))
137 break;
138
139 zprev = znode;
140 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
141 cond_resched();
142 }
143
144 return total_freed;
145}
146
147/**
148 * shrink_tnc_trees - shrink UBIFS TNC trees.
149 * @nr: number of znodes to free
150 * @age: the age of znodes to free
151 * @contention: if any contention, this is set to %1
152 *
153 * This function walks the list of mounted UBIFS file-systems and frees clean
154 * znodes which are older then @age, until at least @nr znodes are freed.
155 * Returns the number of freed znodes.
156 */
157static int shrink_tnc_trees(int nr, int age, int *contention)
158{
159 struct ubifs_info *c;
160 struct list_head *p;
161 unsigned int run_no;
162 int freed = 0;
163
164 spin_lock(&ubifs_infos_lock);
165 do {
166 run_no = ++shrinker_run_no;
167 } while (run_no == 0);
168 /* Iterate over all mounted UBIFS file-systems and try to shrink them */
169 p = ubifs_infos.next;
170 while (p != &ubifs_infos) {
171 c = list_entry(p, struct ubifs_info, infos_list);
172 /*
173 * We move the ones we do to the end of the list, so we stop
174 * when we see one we have already done.
175 */
176 if (c->shrinker_run_no == run_no)
177 break;
178 if (!mutex_trylock(&c->umount_mutex)) {
179 /* Some un-mount is in progress, try next FS */
180 *contention = 1;
181 p = p->next;
182 continue;
183 }
184 /*
185 * We're holding 'c->umount_mutex', so the file-system won't go
186 * away.
187 */
188 if (!mutex_trylock(&c->tnc_mutex)) {
189 mutex_unlock(&c->umount_mutex);
190 *contention = 1;
191 p = p->next;
192 continue;
193 }
194 spin_unlock(&ubifs_infos_lock);
195 /*
196 * OK, now we have TNC locked, the file-system cannot go away -
197 * it is safe to reap the cache.
198 */
199 c->shrinker_run_no = run_no;
200 freed += shrink_tnc(c, nr, age, contention);
201 mutex_unlock(&c->tnc_mutex);
202 spin_lock(&ubifs_infos_lock);
203 /* Get the next list element before we move this one */
204 p = p->next;
205 /*
206 * Move this one to the end of the list to provide some
207 * fairness.
208 */
209 list_del(&c->infos_list);
210 list_add_tail(&c->infos_list, &ubifs_infos);
211 mutex_unlock(&c->umount_mutex);
212 if (freed >= nr)
213 break;
214 }
215 spin_unlock(&ubifs_infos_lock);
216 return freed;
217}
218
219/**
220 * kick_a_thread - kick a background thread to start commit.
221 *
222 * This function kicks a background thread to start background commit. Returns
223 * %-1 if a thread was kicked or there is another reason to assume the memory
224 * will soon be freed or become freeable. If there are no dirty znodes, returns
225 * %0.
226 */
227static int kick_a_thread(void)
228{
229 int i;
230 struct ubifs_info *c;
231
232 /*
233 * Iterate over all mounted UBIFS file-systems and find out if there is
234 * already an ongoing commit operation there. If no, then iterate for
235 * the second time and initiate background commit.
236 */
237 spin_lock(&ubifs_infos_lock);
238 for (i = 0; i < 2; i++) {
239 list_for_each_entry(c, &ubifs_infos, infos_list) {
240 long dirty_zn_cnt;
241
242 if (!mutex_trylock(&c->umount_mutex)) {
243 /*
244 * Some un-mount is in progress, it will
245 * certainly free memory, so just return.
246 */
247 spin_unlock(&ubifs_infos_lock);
248 return -1;
249 }
250
251 dirty_zn_cnt = atomic_long_read(&c->dirty_zn_cnt);
252
253 if (!dirty_zn_cnt || c->cmt_state == COMMIT_BROKEN ||
254 c->ro_media) {
255 mutex_unlock(&c->umount_mutex);
256 continue;
257 }
258
259 if (c->cmt_state != COMMIT_RESTING) {
260 spin_unlock(&ubifs_infos_lock);
261 mutex_unlock(&c->umount_mutex);
262 return -1;
263 }
264
265 if (i == 1) {
266 list_del(&c->infos_list);
267 list_add_tail(&c->infos_list, &ubifs_infos);
268 spin_unlock(&ubifs_infos_lock);
269
270 ubifs_request_bg_commit(c);
271 mutex_unlock(&c->umount_mutex);
272 return -1;
273 }
274 mutex_unlock(&c->umount_mutex);
275 }
276 }
277 spin_unlock(&ubifs_infos_lock);
278
279 return 0;
280}
281
282int ubifs_shrinker(int nr, gfp_t gfp_mask)
283{
284 int freed, contention = 0;
285 long clean_zn_cnt = atomic_long_read(&ubifs_clean_zn_cnt);
286
287 if (nr == 0)
288 return clean_zn_cnt;
289
290 if (!clean_zn_cnt) {
291 /*
292 * No clean znodes, nothing to reap. All we can do in this case
293 * is to kick background threads to start commit, which will
294 * probably make clean znodes which, in turn, will be freeable.
295 * And we return -1 which means will make VM call us again
296 * later.
297 */
298 dbg_tnc("no clean znodes, kick a thread");
299 return kick_a_thread();
300 }
301
302 freed = shrink_tnc_trees(nr, OLD_ZNODE_AGE, &contention);
303 if (freed >= nr)
304 goto out;
305
306 dbg_tnc("not enough old znodes, try to free young ones");
307 freed += shrink_tnc_trees(nr - freed, YOUNG_ZNODE_AGE, &contention);
308 if (freed >= nr)
309 goto out;
310
311 dbg_tnc("not enough young znodes, free all");
312 freed += shrink_tnc_trees(nr - freed, 0, &contention);
313
314 if (!freed && contention) {
315 dbg_tnc("freed nothing, but contention");
316 return -1;
317 }
318
319out:
320 dbg_tnc("%d znodes were freed, requested %d", freed, nr);
321 return freed;
322}
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
new file mode 100644
index 000000000000..00eb9c68ad03
--- /dev/null
+++ b/fs/ubifs/super.c
@@ -0,0 +1,1951 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements UBIFS initialization and VFS superblock operations. Some
25 * initialization stuff which is rather large and complex is placed at
26 * corresponding subsystems, but most of it is here.
27 */
28
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/module.h>
32#include <linux/ctype.h>
33#include <linux/random.h>
34#include <linux/kthread.h>
35#include <linux/parser.h>
36#include <linux/seq_file.h>
37#include <linux/mount.h>
38#include "ubifs.h"
39
40/* Slab cache for UBIFS inodes */
41struct kmem_cache *ubifs_inode_slab;
42
43/* UBIFS TNC shrinker description */
44static struct shrinker ubifs_shrinker_info = {
45 .shrink = ubifs_shrinker,
46 .seeks = DEFAULT_SEEKS,
47};
48
49/**
50 * validate_inode - validate inode.
51 * @c: UBIFS file-system description object
52 * @inode: the inode to validate
53 *
54 * This is a helper function for 'ubifs_iget()' which validates various fields
55 * of a newly built inode to make sure they contain sane values and prevent
56 * possible vulnerabilities. Returns zero if the inode is all right and
57 * a non-zero error code if not.
58 */
59static int validate_inode(struct ubifs_info *c, const struct inode *inode)
60{
61 int err;
62 const struct ubifs_inode *ui = ubifs_inode(inode);
63
64 if (inode->i_size > c->max_inode_sz) {
65 ubifs_err("inode is too large (%lld)",
66 (long long)inode->i_size);
67 return 1;
68 }
69
70 if (ui->compr_type < 0 || ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
71 ubifs_err("unknown compression type %d", ui->compr_type);
72 return 2;
73 }
74
75 if (ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX)
76 return 3;
77
78 if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
79 return 4;
80
81 if (ui->xattr && (inode->i_mode & S_IFMT) != S_IFREG)
82 return 5;
83
84 if (!ubifs_compr_present(ui->compr_type)) {
85 ubifs_warn("inode %lu uses '%s' compression, but it was not "
86 "compiled in", inode->i_ino,
87 ubifs_compr_name(ui->compr_type));
88 }
89
90 err = dbg_check_dir_size(c, inode);
91 return err;
92}
93
94struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
95{
96 int err;
97 union ubifs_key key;
98 struct ubifs_ino_node *ino;
99 struct ubifs_info *c = sb->s_fs_info;
100 struct inode *inode;
101 struct ubifs_inode *ui;
102
103 dbg_gen("inode %lu", inum);
104
105 inode = iget_locked(sb, inum);
106 if (!inode)
107 return ERR_PTR(-ENOMEM);
108 if (!(inode->i_state & I_NEW))
109 return inode;
110 ui = ubifs_inode(inode);
111
112 ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
113 if (!ino) {
114 err = -ENOMEM;
115 goto out;
116 }
117
118 ino_key_init(c, &key, inode->i_ino);
119
120 err = ubifs_tnc_lookup(c, &key, ino);
121 if (err)
122 goto out_ino;
123
124 inode->i_flags |= (S_NOCMTIME | S_NOATIME);
125 inode->i_nlink = le32_to_cpu(ino->nlink);
126 inode->i_uid = le32_to_cpu(ino->uid);
127 inode->i_gid = le32_to_cpu(ino->gid);
128 inode->i_atime.tv_sec = (int64_t)le64_to_cpu(ino->atime_sec);
129 inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
130 inode->i_mtime.tv_sec = (int64_t)le64_to_cpu(ino->mtime_sec);
131 inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
132 inode->i_ctime.tv_sec = (int64_t)le64_to_cpu(ino->ctime_sec);
133 inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
134 inode->i_mode = le32_to_cpu(ino->mode);
135 inode->i_size = le64_to_cpu(ino->size);
136
137 ui->data_len = le32_to_cpu(ino->data_len);
138 ui->flags = le32_to_cpu(ino->flags);
139 ui->compr_type = le16_to_cpu(ino->compr_type);
140 ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
141 ui->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
142 ui->xattr_size = le32_to_cpu(ino->xattr_size);
143 ui->xattr_names = le32_to_cpu(ino->xattr_names);
144 ui->synced_i_size = ui->ui_size = inode->i_size;
145
146 ui->xattr = (ui->flags & UBIFS_XATTR_FL) ? 1 : 0;
147
148 err = validate_inode(c, inode);
149 if (err)
150 goto out_invalid;
151
152 /* Disable readahead */
153 inode->i_mapping->backing_dev_info = &c->bdi;
154
155 switch (inode->i_mode & S_IFMT) {
156 case S_IFREG:
157 inode->i_mapping->a_ops = &ubifs_file_address_operations;
158 inode->i_op = &ubifs_file_inode_operations;
159 inode->i_fop = &ubifs_file_operations;
160 if (ui->xattr) {
161 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
162 if (!ui->data) {
163 err = -ENOMEM;
164 goto out_ino;
165 }
166 memcpy(ui->data, ino->data, ui->data_len);
167 ((char *)ui->data)[ui->data_len] = '\0';
168 } else if (ui->data_len != 0) {
169 err = 10;
170 goto out_invalid;
171 }
172 break;
173 case S_IFDIR:
174 inode->i_op = &ubifs_dir_inode_operations;
175 inode->i_fop = &ubifs_dir_operations;
176 if (ui->data_len != 0) {
177 err = 11;
178 goto out_invalid;
179 }
180 break;
181 case S_IFLNK:
182 inode->i_op = &ubifs_symlink_inode_operations;
183 if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
184 err = 12;
185 goto out_invalid;
186 }
187 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
188 if (!ui->data) {
189 err = -ENOMEM;
190 goto out_ino;
191 }
192 memcpy(ui->data, ino->data, ui->data_len);
193 ((char *)ui->data)[ui->data_len] = '\0';
194 break;
195 case S_IFBLK:
196 case S_IFCHR:
197 {
198 dev_t rdev;
199 union ubifs_dev_desc *dev;
200
201 ui->data = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
202 if (!ui->data) {
203 err = -ENOMEM;
204 goto out_ino;
205 }
206
207 dev = (union ubifs_dev_desc *)ino->data;
208 if (ui->data_len == sizeof(dev->new))
209 rdev = new_decode_dev(le32_to_cpu(dev->new));
210 else if (ui->data_len == sizeof(dev->huge))
211 rdev = huge_decode_dev(le64_to_cpu(dev->huge));
212 else {
213 err = 13;
214 goto out_invalid;
215 }
216 memcpy(ui->data, ino->data, ui->data_len);
217 inode->i_op = &ubifs_file_inode_operations;
218 init_special_inode(inode, inode->i_mode, rdev);
219 break;
220 }
221 case S_IFSOCK:
222 case S_IFIFO:
223 inode->i_op = &ubifs_file_inode_operations;
224 init_special_inode(inode, inode->i_mode, 0);
225 if (ui->data_len != 0) {
226 err = 14;
227 goto out_invalid;
228 }
229 break;
230 default:
231 err = 15;
232 goto out_invalid;
233 }
234
235 kfree(ino);
236 ubifs_set_inode_flags(inode);
237 unlock_new_inode(inode);
238 return inode;
239
240out_invalid:
241 ubifs_err("inode %lu validation failed, error %d", inode->i_ino, err);
242 dbg_dump_node(c, ino);
243 dbg_dump_inode(c, inode);
244 err = -EINVAL;
245out_ino:
246 kfree(ino);
247out:
248 ubifs_err("failed to read inode %lu, error %d", inode->i_ino, err);
249 iget_failed(inode);
250 return ERR_PTR(err);
251}
252
253static struct inode *ubifs_alloc_inode(struct super_block *sb)
254{
255 struct ubifs_inode *ui;
256
257 ui = kmem_cache_alloc(ubifs_inode_slab, GFP_NOFS);
258 if (!ui)
259 return NULL;
260
261 memset((void *)ui + sizeof(struct inode), 0,
262 sizeof(struct ubifs_inode) - sizeof(struct inode));
263 mutex_init(&ui->ui_mutex);
264 spin_lock_init(&ui->ui_lock);
265 return &ui->vfs_inode;
266};
267
268static void ubifs_destroy_inode(struct inode *inode)
269{
270 struct ubifs_inode *ui = ubifs_inode(inode);
271
272 kfree(ui->data);
273 kmem_cache_free(ubifs_inode_slab, inode);
274}
275
276/*
277 * Note, Linux write-back code calls this without 'i_mutex'.
278 */
279static int ubifs_write_inode(struct inode *inode, int wait)
280{
281 int err;
282 struct ubifs_info *c = inode->i_sb->s_fs_info;
283 struct ubifs_inode *ui = ubifs_inode(inode);
284
285 ubifs_assert(!ui->xattr);
286 if (is_bad_inode(inode))
287 return 0;
288
289 mutex_lock(&ui->ui_mutex);
290 /*
291 * Due to races between write-back forced by budgeting
292 * (see 'sync_some_inodes()') and pdflush write-back, the inode may
293 * have already been synchronized, do not do this again. This might
294 * also happen if it was synchronized in an VFS operation, e.g.
295 * 'ubifs_link()'.
296 */
297 if (!ui->dirty) {
298 mutex_unlock(&ui->ui_mutex);
299 return 0;
300 }
301
302 dbg_gen("inode %lu", inode->i_ino);
303 err = ubifs_jnl_write_inode(c, inode, 0);
304 if (err)
305 ubifs_err("can't write inode %lu, error %d", inode->i_ino, err);
306
307 ui->dirty = 0;
308 mutex_unlock(&ui->ui_mutex);
309 ubifs_release_dirty_inode_budget(c, ui);
310 return err;
311}
312
313static void ubifs_delete_inode(struct inode *inode)
314{
315 int err;
316 struct ubifs_info *c = inode->i_sb->s_fs_info;
317
318 if (ubifs_inode(inode)->xattr)
319 /*
320 * Extended attribute inode deletions are fully handled in
321 * 'ubifs_removexattr()'. These inodes are special and have
322 * limited usage, so there is nothing to do here.
323 */
324 goto out;
325
326 dbg_gen("inode %lu", inode->i_ino);
327 ubifs_assert(!atomic_read(&inode->i_count));
328 ubifs_assert(inode->i_nlink == 0);
329
330 truncate_inode_pages(&inode->i_data, 0);
331 if (is_bad_inode(inode))
332 goto out;
333
334 ubifs_inode(inode)->ui_size = inode->i_size = 0;
335 err = ubifs_jnl_write_inode(c, inode, 1);
336 if (err)
337 /*
338 * Worst case we have a lost orphan inode wasting space, so a
339 * simple error message is ok here.
340 */
341 ubifs_err("can't write inode %lu, error %d", inode->i_ino, err);
342out:
343 clear_inode(inode);
344}
345
346static void ubifs_dirty_inode(struct inode *inode)
347{
348 struct ubifs_inode *ui = ubifs_inode(inode);
349
350 ubifs_assert(mutex_is_locked(&ui->ui_mutex));
351 if (!ui->dirty) {
352 ui->dirty = 1;
353 dbg_gen("inode %lu", inode->i_ino);
354 }
355}
356
357static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
358{
359 struct ubifs_info *c = dentry->d_sb->s_fs_info;
360 unsigned long long free;
361
362 free = ubifs_budg_get_free_space(c);
363 dbg_gen("free space %lld bytes (%lld blocks)",
364 free, free >> UBIFS_BLOCK_SHIFT);
365
366 buf->f_type = UBIFS_SUPER_MAGIC;
367 buf->f_bsize = UBIFS_BLOCK_SIZE;
368 buf->f_blocks = c->block_cnt;
369 buf->f_bfree = free >> UBIFS_BLOCK_SHIFT;
370 if (free > c->report_rp_size)
371 buf->f_bavail = (free - c->report_rp_size) >> UBIFS_BLOCK_SHIFT;
372 else
373 buf->f_bavail = 0;
374 buf->f_files = 0;
375 buf->f_ffree = 0;
376 buf->f_namelen = UBIFS_MAX_NLEN;
377
378 return 0;
379}
380
381static int ubifs_show_options(struct seq_file *s, struct vfsmount *mnt)
382{
383 struct ubifs_info *c = mnt->mnt_sb->s_fs_info;
384
385 if (c->mount_opts.unmount_mode == 2)
386 seq_printf(s, ",fast_unmount");
387 else if (c->mount_opts.unmount_mode == 1)
388 seq_printf(s, ",norm_unmount");
389
390 return 0;
391}
392
393static int ubifs_sync_fs(struct super_block *sb, int wait)
394{
395 struct ubifs_info *c = sb->s_fs_info;
396 int i, ret = 0, err;
397
398 if (c->jheads)
399 for (i = 0; i < c->jhead_cnt; i++) {
400 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
401 if (err && !ret)
402 ret = err;
403 }
404 /*
405 * We ought to call sync for c->ubi but it does not have one. If it had
406 * it would in turn call mtd->sync, however mtd operations are
407 * synchronous anyway, so we don't lose any sleep here.
408 */
409 return ret;
410}
411
412/**
413 * init_constants_early - initialize UBIFS constants.
414 * @c: UBIFS file-system description object
415 *
416 * This function initialize UBIFS constants which do not need the superblock to
417 * be read. It also checks that the UBI volume satisfies basic UBIFS
418 * requirements. Returns zero in case of success and a negative error code in
419 * case of failure.
420 */
421static int init_constants_early(struct ubifs_info *c)
422{
423 if (c->vi.corrupted) {
424 ubifs_warn("UBI volume is corrupted - read-only mode");
425 c->ro_media = 1;
426 }
427
428 if (c->di.ro_mode) {
429 ubifs_msg("read-only UBI device");
430 c->ro_media = 1;
431 }
432
433 if (c->vi.vol_type == UBI_STATIC_VOLUME) {
434 ubifs_msg("static UBI volume - read-only mode");
435 c->ro_media = 1;
436 }
437
438 c->leb_cnt = c->vi.size;
439 c->leb_size = c->vi.usable_leb_size;
440 c->half_leb_size = c->leb_size / 2;
441 c->min_io_size = c->di.min_io_size;
442 c->min_io_shift = fls(c->min_io_size) - 1;
443
444 if (c->leb_size < UBIFS_MIN_LEB_SZ) {
445 ubifs_err("too small LEBs (%d bytes), min. is %d bytes",
446 c->leb_size, UBIFS_MIN_LEB_SZ);
447 return -EINVAL;
448 }
449
450 if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
451 ubifs_err("too few LEBs (%d), min. is %d",
452 c->leb_cnt, UBIFS_MIN_LEB_CNT);
453 return -EINVAL;
454 }
455
456 if (!is_power_of_2(c->min_io_size)) {
457 ubifs_err("bad min. I/O size %d", c->min_io_size);
458 return -EINVAL;
459 }
460
461 /*
462 * UBIFS aligns all node to 8-byte boundary, so to make function in
463 * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
464 * less than 8.
465 */
466 if (c->min_io_size < 8) {
467 c->min_io_size = 8;
468 c->min_io_shift = 3;
469 }
470
471 c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
472 c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
473
474 /*
475 * Initialize node length ranges which are mostly needed for node
476 * length validation.
477 */
478 c->ranges[UBIFS_PAD_NODE].len = UBIFS_PAD_NODE_SZ;
479 c->ranges[UBIFS_SB_NODE].len = UBIFS_SB_NODE_SZ;
480 c->ranges[UBIFS_MST_NODE].len = UBIFS_MST_NODE_SZ;
481 c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ;
482 c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
483 c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ;
484
485 c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
486 c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
487 c->ranges[UBIFS_ORPH_NODE].min_len =
488 UBIFS_ORPH_NODE_SZ + sizeof(__le64);
489 c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
490 c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
491 c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
492 c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
493 c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
494 c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
495 c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
496 /*
497 * Minimum indexing node size is amended later when superblock is
498 * read and the key length is known.
499 */
500 c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
501 /*
502 * Maximum indexing node size is amended later when superblock is
503 * read and the fanout is known.
504 */
505 c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
506
507 /*
508 * Initialize dead and dark LEB space watermarks.
509 *
510 * Dead space is the space which cannot be used. Its watermark is
511 * equivalent to min. I/O unit or minimum node size if it is greater
512 * then min. I/O unit.
513 *
514 * Dark space is the space which might be used, or might not, depending
515 * on which node should be written to the LEB. Its watermark is
516 * equivalent to maximum UBIFS node size.
517 */
518 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
519 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
520
521 return 0;
522}
523
524/**
525 * bud_wbuf_callback - bud LEB write-buffer synchronization call-back.
526 * @c: UBIFS file-system description object
527 * @lnum: LEB the write-buffer was synchronized to
528 * @free: how many free bytes left in this LEB
529 * @pad: how many bytes were padded
530 *
531 * This is a callback function which is called by the I/O unit when the
532 * write-buffer is synchronized. We need this to correctly maintain space
533 * accounting in bud logical eraseblocks. This function returns zero in case of
534 * success and a negative error code in case of failure.
535 *
536 * This function actually belongs to the journal, but we keep it here because
537 * we want to keep it static.
538 */
539static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
540{
541 return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
542}
543
544/*
545 * init_constants_late - initialize UBIFS constants.
546 * @c: UBIFS file-system description object
547 *
548 * This is a helper function which initializes various UBIFS constants after
549 * the superblock has been read. It also checks various UBIFS parameters and
550 * makes sure they are all right. Returns zero in case of success and a
551 * negative error code in case of failure.
552 */
553static int init_constants_late(struct ubifs_info *c)
554{
555 int tmp, err;
556 uint64_t tmp64;
557
558 c->main_bytes = (long long)c->main_lebs * c->leb_size;
559 c->max_znode_sz = sizeof(struct ubifs_znode) +
560 c->fanout * sizeof(struct ubifs_zbranch);
561
562 tmp = ubifs_idx_node_sz(c, 1);
563 c->ranges[UBIFS_IDX_NODE].min_len = tmp;
564 c->min_idx_node_sz = ALIGN(tmp, 8);
565
566 tmp = ubifs_idx_node_sz(c, c->fanout);
567 c->ranges[UBIFS_IDX_NODE].max_len = tmp;
568 c->max_idx_node_sz = ALIGN(tmp, 8);
569
570 /* Make sure LEB size is large enough to fit full commit */
571 tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
572 tmp = ALIGN(tmp, c->min_io_size);
573 if (tmp > c->leb_size) {
574 dbg_err("too small LEB size %d, at least %d needed",
575 c->leb_size, tmp);
576 return -EINVAL;
577 }
578
579 /*
580 * Make sure that the log is large enough to fit reference nodes for
581 * all buds plus one reserved LEB.
582 */
583 tmp64 = c->max_bud_bytes;
584 tmp = do_div(tmp64, c->leb_size);
585 c->max_bud_cnt = tmp64 + !!tmp;
586 tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
587 tmp /= c->leb_size;
588 tmp += 1;
589 if (c->log_lebs < tmp) {
590 dbg_err("too small log %d LEBs, required min. %d LEBs",
591 c->log_lebs, tmp);
592 return -EINVAL;
593 }
594
595 /*
596 * When budgeting we assume worst-case scenarios when the pages are not
597 * be compressed and direntries are of the maximum size.
598 *
599 * Note, data, which may be stored in inodes is budgeted separately, so
600 * it is not included into 'c->inode_budget'.
601 */
602 c->page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
603 c->inode_budget = UBIFS_INO_NODE_SZ;
604 c->dent_budget = UBIFS_MAX_DENT_NODE_SZ;
605
606 /*
607 * When the amount of flash space used by buds becomes
608 * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
609 * The writers are unblocked when the commit is finished. To avoid
610 * writers to be blocked UBIFS initiates background commit in advance,
611 * when number of bud bytes becomes above the limit defined below.
612 */
613 c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
614
615 /*
616 * Ensure minimum journal size. All the bytes in the journal heads are
617 * considered to be used, when calculating the current journal usage.
618 * Consequently, if the journal is too small, UBIFS will treat it as
619 * always full.
620 */
621 tmp64 = (uint64_t)(c->jhead_cnt + 1) * c->leb_size + 1;
622 if (c->bg_bud_bytes < tmp64)
623 c->bg_bud_bytes = tmp64;
624 if (c->max_bud_bytes < tmp64 + c->leb_size)
625 c->max_bud_bytes = tmp64 + c->leb_size;
626
627 err = ubifs_calc_lpt_geom(c);
628 if (err)
629 return err;
630
631 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
632
633 /*
634 * Calculate total amount of FS blocks. This number is not used
635 * internally because it does not make much sense for UBIFS, but it is
636 * necessary to report something for the 'statfs()' call.
637 *
638 * Subtract the LEB reserved for GC and the LEB which is reserved for
639 * deletions.
640 *
641 * Review 'ubifs_calc_available()' if changing this calculation.
642 */
643 tmp64 = c->main_lebs - 2;
644 tmp64 *= (uint64_t)c->leb_size - c->dark_wm;
645 tmp64 = ubifs_reported_space(c, tmp64);
646 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
647
648 return 0;
649}
650
651/**
652 * take_gc_lnum - reserve GC LEB.
653 * @c: UBIFS file-system description object
654 *
655 * This function ensures that the LEB reserved for garbage collection is
656 * unmapped and is marked as "taken" in lprops. We also have to set free space
657 * to LEB size and dirty space to zero, because lprops may contain out-of-date
658 * information if the file-system was un-mounted before it has been committed.
659 * This function returns zero in case of success and a negative error code in
660 * case of failure.
661 */
662static int take_gc_lnum(struct ubifs_info *c)
663{
664 int err;
665
666 if (c->gc_lnum == -1) {
667 ubifs_err("no LEB for GC");
668 return -EINVAL;
669 }
670
671 err = ubifs_leb_unmap(c, c->gc_lnum);
672 if (err)
673 return err;
674
675 /* And we have to tell lprops that this LEB is taken */
676 err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
677 LPROPS_TAKEN, 0, 0);
678 return err;
679}
680
681/**
682 * alloc_wbufs - allocate write-buffers.
683 * @c: UBIFS file-system description object
684 *
685 * This helper function allocates and initializes UBIFS write-buffers. Returns
686 * zero in case of success and %-ENOMEM in case of failure.
687 */
688static int alloc_wbufs(struct ubifs_info *c)
689{
690 int i, err;
691
692 c->jheads = kzalloc(c->jhead_cnt * sizeof(struct ubifs_jhead),
693 GFP_KERNEL);
694 if (!c->jheads)
695 return -ENOMEM;
696
697 /* Initialize journal heads */
698 for (i = 0; i < c->jhead_cnt; i++) {
699 INIT_LIST_HEAD(&c->jheads[i].buds_list);
700 err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
701 if (err)
702 return err;
703
704 c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
705 c->jheads[i].wbuf.jhead = i;
706 }
707
708 c->jheads[BASEHD].wbuf.dtype = UBI_SHORTTERM;
709 /*
710 * Garbage Collector head likely contains long-term data and
711 * does not need to be synchronized by timer.
712 */
713 c->jheads[GCHD].wbuf.dtype = UBI_LONGTERM;
714 c->jheads[GCHD].wbuf.timeout = 0;
715
716 return 0;
717}
718
719/**
720 * free_wbufs - free write-buffers.
721 * @c: UBIFS file-system description object
722 */
723static void free_wbufs(struct ubifs_info *c)
724{
725 int i;
726
727 if (c->jheads) {
728 for (i = 0; i < c->jhead_cnt; i++) {
729 kfree(c->jheads[i].wbuf.buf);
730 kfree(c->jheads[i].wbuf.inodes);
731 }
732 kfree(c->jheads);
733 c->jheads = NULL;
734 }
735}
736
737/**
738 * free_orphans - free orphans.
739 * @c: UBIFS file-system description object
740 */
741static void free_orphans(struct ubifs_info *c)
742{
743 struct ubifs_orphan *orph;
744
745 while (c->orph_dnext) {
746 orph = c->orph_dnext;
747 c->orph_dnext = orph->dnext;
748 list_del(&orph->list);
749 kfree(orph);
750 }
751
752 while (!list_empty(&c->orph_list)) {
753 orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
754 list_del(&orph->list);
755 kfree(orph);
756 dbg_err("orphan list not empty at unmount");
757 }
758
759 vfree(c->orph_buf);
760 c->orph_buf = NULL;
761}
762
763/**
764 * free_buds - free per-bud objects.
765 * @c: UBIFS file-system description object
766 */
767static void free_buds(struct ubifs_info *c)
768{
769 struct rb_node *this = c->buds.rb_node;
770 struct ubifs_bud *bud;
771
772 while (this) {
773 if (this->rb_left)
774 this = this->rb_left;
775 else if (this->rb_right)
776 this = this->rb_right;
777 else {
778 bud = rb_entry(this, struct ubifs_bud, rb);
779 this = rb_parent(this);
780 if (this) {
781 if (this->rb_left == &bud->rb)
782 this->rb_left = NULL;
783 else
784 this->rb_right = NULL;
785 }
786 kfree(bud);
787 }
788 }
789}
790
791/**
792 * check_volume_empty - check if the UBI volume is empty.
793 * @c: UBIFS file-system description object
794 *
795 * This function checks if the UBIFS volume is empty by looking if its LEBs are
796 * mapped or not. The result of checking is stored in the @c->empty variable.
797 * Returns zero in case of success and a negative error code in case of
798 * failure.
799 */
800static int check_volume_empty(struct ubifs_info *c)
801{
802 int lnum, err;
803
804 c->empty = 1;
805 for (lnum = 0; lnum < c->leb_cnt; lnum++) {
806 err = ubi_is_mapped(c->ubi, lnum);
807 if (unlikely(err < 0))
808 return err;
809 if (err == 1) {
810 c->empty = 0;
811 break;
812 }
813
814 cond_resched();
815 }
816
817 return 0;
818}
819
820/*
821 * UBIFS mount options.
822 *
823 * Opt_fast_unmount: do not run a journal commit before un-mounting
824 * Opt_norm_unmount: run a journal commit before un-mounting
825 * Opt_err: just end of array marker
826 */
827enum {
828 Opt_fast_unmount,
829 Opt_norm_unmount,
830 Opt_err,
831};
832
833static match_table_t tokens = {
834 {Opt_fast_unmount, "fast_unmount"},
835 {Opt_norm_unmount, "norm_unmount"},
836 {Opt_err, NULL},
837};
838
839/**
840 * ubifs_parse_options - parse mount parameters.
841 * @c: UBIFS file-system description object
842 * @options: parameters to parse
843 * @is_remount: non-zero if this is FS re-mount
844 *
845 * This function parses UBIFS mount options and returns zero in case success
846 * and a negative error code in case of failure.
847 */
848static int ubifs_parse_options(struct ubifs_info *c, char *options,
849 int is_remount)
850{
851 char *p;
852 substring_t args[MAX_OPT_ARGS];
853
854 if (!options)
855 return 0;
856
857 while ((p = strsep(&options, ","))) {
858 int token;
859
860 if (!*p)
861 continue;
862
863 token = match_token(p, tokens, args);
864 switch (token) {
865 case Opt_fast_unmount:
866 c->mount_opts.unmount_mode = 2;
867 c->fast_unmount = 1;
868 break;
869 case Opt_norm_unmount:
870 c->mount_opts.unmount_mode = 1;
871 c->fast_unmount = 0;
872 break;
873 default:
874 ubifs_err("unrecognized mount option \"%s\" "
875 "or missing value", p);
876 return -EINVAL;
877 }
878 }
879
880 return 0;
881}
882
883/**
884 * destroy_journal - destroy journal data structures.
885 * @c: UBIFS file-system description object
886 *
887 * This function destroys journal data structures including those that may have
888 * been created by recovery functions.
889 */
890static void destroy_journal(struct ubifs_info *c)
891{
892 while (!list_empty(&c->unclean_leb_list)) {
893 struct ubifs_unclean_leb *ucleb;
894
895 ucleb = list_entry(c->unclean_leb_list.next,
896 struct ubifs_unclean_leb, list);
897 list_del(&ucleb->list);
898 kfree(ucleb);
899 }
900 while (!list_empty(&c->old_buds)) {
901 struct ubifs_bud *bud;
902
903 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
904 list_del(&bud->list);
905 kfree(bud);
906 }
907 ubifs_destroy_idx_gc(c);
908 ubifs_destroy_size_tree(c);
909 ubifs_tnc_close(c);
910 free_buds(c);
911}
912
913/**
914 * mount_ubifs - mount UBIFS file-system.
915 * @c: UBIFS file-system description object
916 *
917 * This function mounts UBIFS file system. Returns zero in case of success and
918 * a negative error code in case of failure.
919 *
920 * Note, the function does not de-allocate resources it it fails half way
921 * through, and the caller has to do this instead.
922 */
923static int mount_ubifs(struct ubifs_info *c)
924{
925 struct super_block *sb = c->vfs_sb;
926 int err, mounted_read_only = (sb->s_flags & MS_RDONLY);
927 long long x;
928 size_t sz;
929
930 err = init_constants_early(c);
931 if (err)
932 return err;
933
934#ifdef CONFIG_UBIFS_FS_DEBUG
935 c->dbg_buf = vmalloc(c->leb_size);
936 if (!c->dbg_buf)
937 return -ENOMEM;
938#endif
939
940 err = check_volume_empty(c);
941 if (err)
942 goto out_free;
943
944 if (c->empty && (mounted_read_only || c->ro_media)) {
945 /*
946 * This UBI volume is empty, and read-only, or the file system
947 * is mounted read-only - we cannot format it.
948 */
949 ubifs_err("can't format empty UBI volume: read-only %s",
950 c->ro_media ? "UBI volume" : "mount");
951 err = -EROFS;
952 goto out_free;
953 }
954
955 if (c->ro_media && !mounted_read_only) {
956 ubifs_err("cannot mount read-write - read-only media");
957 err = -EROFS;
958 goto out_free;
959 }
960
961 /*
962 * The requirement for the buffer is that it should fit indexing B-tree
963 * height amount of integers. We assume the height if the TNC tree will
964 * never exceed 64.
965 */
966 err = -ENOMEM;
967 c->bottom_up_buf = kmalloc(BOTTOM_UP_HEIGHT * sizeof(int), GFP_KERNEL);
968 if (!c->bottom_up_buf)
969 goto out_free;
970
971 c->sbuf = vmalloc(c->leb_size);
972 if (!c->sbuf)
973 goto out_free;
974
975 if (!mounted_read_only) {
976 c->ileb_buf = vmalloc(c->leb_size);
977 if (!c->ileb_buf)
978 goto out_free;
979 }
980
981 err = ubifs_read_superblock(c);
982 if (err)
983 goto out_free;
984
985 /*
986 * Make sure the compressor which is set as the default on in the
987 * superblock was actually compiled in.
988 */
989 if (!ubifs_compr_present(c->default_compr)) {
990 ubifs_warn("'%s' compressor is set by superblock, but not "
991 "compiled in", ubifs_compr_name(c->default_compr));
992 c->default_compr = UBIFS_COMPR_NONE;
993 }
994
995 dbg_failure_mode_registration(c);
996
997 err = init_constants_late(c);
998 if (err)
999 goto out_dereg;
1000
1001 sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
1002 sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
1003 c->cbuf = kmalloc(sz, GFP_NOFS);
1004 if (!c->cbuf) {
1005 err = -ENOMEM;
1006 goto out_dereg;
1007 }
1008
1009 if (!mounted_read_only) {
1010 err = alloc_wbufs(c);
1011 if (err)
1012 goto out_cbuf;
1013
1014 /* Create background thread */
1015 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num,
1016 c->vi.vol_id);
1017 c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
1018 if (!c->bgt)
1019 c->bgt = ERR_PTR(-EINVAL);
1020 if (IS_ERR(c->bgt)) {
1021 err = PTR_ERR(c->bgt);
1022 c->bgt = NULL;
1023 ubifs_err("cannot spawn \"%s\", error %d",
1024 c->bgt_name, err);
1025 goto out_wbufs;
1026 }
1027 wake_up_process(c->bgt);
1028 }
1029
1030 err = ubifs_read_master(c);
1031 if (err)
1032 goto out_master;
1033
1034 if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
1035 ubifs_msg("recovery needed");
1036 c->need_recovery = 1;
1037 if (!mounted_read_only) {
1038 err = ubifs_recover_inl_heads(c, c->sbuf);
1039 if (err)
1040 goto out_master;
1041 }
1042 } else if (!mounted_read_only) {
1043 /*
1044 * Set the "dirty" flag so that if we reboot uncleanly we
1045 * will notice this immediately on the next mount.
1046 */
1047 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
1048 err = ubifs_write_master(c);
1049 if (err)
1050 goto out_master;
1051 }
1052
1053 err = ubifs_lpt_init(c, 1, !mounted_read_only);
1054 if (err)
1055 goto out_lpt;
1056
1057 err = dbg_check_idx_size(c, c->old_idx_sz);
1058 if (err)
1059 goto out_lpt;
1060
1061 err = ubifs_replay_journal(c);
1062 if (err)
1063 goto out_journal;
1064
1065 err = ubifs_mount_orphans(c, c->need_recovery, mounted_read_only);
1066 if (err)
1067 goto out_orphans;
1068
1069 if (!mounted_read_only) {
1070 int lnum;
1071
1072 /* Check for enough free space */
1073 if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) {
1074 ubifs_err("insufficient available space");
1075 err = -EINVAL;
1076 goto out_orphans;
1077 }
1078
1079 /* Check for enough log space */
1080 lnum = c->lhead_lnum + 1;
1081 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
1082 lnum = UBIFS_LOG_LNUM;
1083 if (lnum == c->ltail_lnum) {
1084 err = ubifs_consolidate_log(c);
1085 if (err)
1086 goto out_orphans;
1087 }
1088
1089 if (c->need_recovery) {
1090 err = ubifs_recover_size(c);
1091 if (err)
1092 goto out_orphans;
1093 err = ubifs_rcvry_gc_commit(c);
1094 } else
1095 err = take_gc_lnum(c);
1096 if (err)
1097 goto out_orphans;
1098
1099 err = dbg_check_lprops(c);
1100 if (err)
1101 goto out_orphans;
1102 } else if (c->need_recovery) {
1103 err = ubifs_recover_size(c);
1104 if (err)
1105 goto out_orphans;
1106 }
1107
1108 spin_lock(&ubifs_infos_lock);
1109 list_add_tail(&c->infos_list, &ubifs_infos);
1110 spin_unlock(&ubifs_infos_lock);
1111
1112 if (c->need_recovery) {
1113 if (mounted_read_only)
1114 ubifs_msg("recovery deferred");
1115 else {
1116 c->need_recovery = 0;
1117 ubifs_msg("recovery completed");
1118 }
1119 }
1120
1121 err = dbg_check_filesystem(c);
1122 if (err)
1123 goto out_infos;
1124
1125 ubifs_msg("mounted UBI device %d, volume %d", c->vi.ubi_num,
1126 c->vi.vol_id);
1127 if (mounted_read_only)
1128 ubifs_msg("mounted read-only");
1129 x = (long long)c->main_lebs * c->leb_size;
1130 ubifs_msg("file system size: %lld bytes (%lld KiB, %lld MiB, %d LEBs)",
1131 x, x >> 10, x >> 20, c->main_lebs);
1132 x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
1133 ubifs_msg("journal size: %lld bytes (%lld KiB, %lld MiB, %d LEBs)",
1134 x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt);
1135 ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
1136 ubifs_msg("media format %d, latest format %d",
1137 c->fmt_version, UBIFS_FORMAT_VERSION);
1138
1139 dbg_msg("compiled on: " __DATE__ " at " __TIME__);
1140 dbg_msg("min. I/O unit size: %d bytes", c->min_io_size);
1141 dbg_msg("LEB size: %d bytes (%d KiB)",
1142 c->leb_size, c->leb_size / 1024);
1143 dbg_msg("data journal heads: %d",
1144 c->jhead_cnt - NONDATA_JHEADS_CNT);
1145 dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
1146 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
1147 c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
1148 c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
1149 c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
1150 c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
1151 dbg_msg("fast unmount: %d", c->fast_unmount);
1152 dbg_msg("big_lpt %d", c->big_lpt);
1153 dbg_msg("log LEBs: %d (%d - %d)",
1154 c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
1155 dbg_msg("LPT area LEBs: %d (%d - %d)",
1156 c->lpt_lebs, c->lpt_first, c->lpt_last);
1157 dbg_msg("orphan area LEBs: %d (%d - %d)",
1158 c->orph_lebs, c->orph_first, c->orph_last);
1159 dbg_msg("main area LEBs: %d (%d - %d)",
1160 c->main_lebs, c->main_first, c->leb_cnt - 1);
1161 dbg_msg("index LEBs: %d", c->lst.idx_lebs);
1162 dbg_msg("total index bytes: %lld (%lld KiB, %lld MiB)",
1163 c->old_idx_sz, c->old_idx_sz >> 10, c->old_idx_sz >> 20);
1164 dbg_msg("key hash type: %d", c->key_hash_type);
1165 dbg_msg("tree fanout: %d", c->fanout);
1166 dbg_msg("reserved GC LEB: %d", c->gc_lnum);
1167 dbg_msg("first main LEB: %d", c->main_first);
1168 dbg_msg("dead watermark: %d", c->dead_wm);
1169 dbg_msg("dark watermark: %d", c->dark_wm);
1170 x = (long long)c->main_lebs * c->dark_wm;
1171 dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
1172 x, x >> 10, x >> 20);
1173 dbg_msg("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
1174 c->max_bud_bytes, c->max_bud_bytes >> 10,
1175 c->max_bud_bytes >> 20);
1176 dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
1177 c->bg_bud_bytes, c->bg_bud_bytes >> 10,
1178 c->bg_bud_bytes >> 20);
1179 dbg_msg("current bud bytes %lld (%lld KiB, %lld MiB)",
1180 c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
1181 dbg_msg("max. seq. number: %llu", c->max_sqnum);
1182 dbg_msg("commit number: %llu", c->cmt_no);
1183
1184 return 0;
1185
1186out_infos:
1187 spin_lock(&ubifs_infos_lock);
1188 list_del(&c->infos_list);
1189 spin_unlock(&ubifs_infos_lock);
1190out_orphans:
1191 free_orphans(c);
1192out_journal:
1193 destroy_journal(c);
1194out_lpt:
1195 ubifs_lpt_free(c, 0);
1196out_master:
1197 kfree(c->mst_node);
1198 kfree(c->rcvrd_mst_node);
1199 if (c->bgt)
1200 kthread_stop(c->bgt);
1201out_wbufs:
1202 free_wbufs(c);
1203out_cbuf:
1204 kfree(c->cbuf);
1205out_dereg:
1206 dbg_failure_mode_deregistration(c);
1207out_free:
1208 vfree(c->ileb_buf);
1209 vfree(c->sbuf);
1210 kfree(c->bottom_up_buf);
1211 UBIFS_DBG(vfree(c->dbg_buf));
1212 return err;
1213}
1214
1215/**
1216 * ubifs_umount - un-mount UBIFS file-system.
1217 * @c: UBIFS file-system description object
1218 *
1219 * Note, this function is called to free allocated resourced when un-mounting,
1220 * as well as free resources when an error occurred while we were half way
1221 * through mounting (error path cleanup function). So it has to make sure the
1222 * resource was actually allocated before freeing it.
1223 */
1224static void ubifs_umount(struct ubifs_info *c)
1225{
1226 dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
1227 c->vi.vol_id);
1228
1229 spin_lock(&ubifs_infos_lock);
1230 list_del(&c->infos_list);
1231 spin_unlock(&ubifs_infos_lock);
1232
1233 if (c->bgt)
1234 kthread_stop(c->bgt);
1235
1236 destroy_journal(c);
1237 free_wbufs(c);
1238 free_orphans(c);
1239 ubifs_lpt_free(c, 0);
1240
1241 kfree(c->cbuf);
1242 kfree(c->rcvrd_mst_node);
1243 kfree(c->mst_node);
1244 vfree(c->sbuf);
1245 kfree(c->bottom_up_buf);
1246 UBIFS_DBG(vfree(c->dbg_buf));
1247 vfree(c->ileb_buf);
1248 dbg_failure_mode_deregistration(c);
1249}
1250
1251/**
1252 * ubifs_remount_rw - re-mount in read-write mode.
1253 * @c: UBIFS file-system description object
1254 *
1255 * UBIFS avoids allocating many unnecessary resources when mounted in read-only
1256 * mode. This function allocates the needed resources and re-mounts UBIFS in
1257 * read-write mode.
1258 */
1259static int ubifs_remount_rw(struct ubifs_info *c)
1260{
1261 int err, lnum;
1262
1263 if (c->ro_media)
1264 return -EINVAL;
1265
1266 mutex_lock(&c->umount_mutex);
1267 c->remounting_rw = 1;
1268
1269 /* Check for enough free space */
1270 if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) {
1271 ubifs_err("insufficient available space");
1272 err = -EINVAL;
1273 goto out;
1274 }
1275
1276 if (c->old_leb_cnt != c->leb_cnt) {
1277 struct ubifs_sb_node *sup;
1278
1279 sup = ubifs_read_sb_node(c);
1280 if (IS_ERR(sup)) {
1281 err = PTR_ERR(sup);
1282 goto out;
1283 }
1284 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
1285 err = ubifs_write_sb_node(c, sup);
1286 if (err)
1287 goto out;
1288 }
1289
1290 if (c->need_recovery) {
1291 ubifs_msg("completing deferred recovery");
1292 err = ubifs_write_rcvrd_mst_node(c);
1293 if (err)
1294 goto out;
1295 err = ubifs_recover_size(c);
1296 if (err)
1297 goto out;
1298 err = ubifs_clean_lebs(c, c->sbuf);
1299 if (err)
1300 goto out;
1301 err = ubifs_recover_inl_heads(c, c->sbuf);
1302 if (err)
1303 goto out;
1304 }
1305
1306 if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
1307 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
1308 err = ubifs_write_master(c);
1309 if (err)
1310 goto out;
1311 }
1312
1313 c->ileb_buf = vmalloc(c->leb_size);
1314 if (!c->ileb_buf) {
1315 err = -ENOMEM;
1316 goto out;
1317 }
1318
1319 err = ubifs_lpt_init(c, 0, 1);
1320 if (err)
1321 goto out;
1322
1323 err = alloc_wbufs(c);
1324 if (err)
1325 goto out;
1326
1327 ubifs_create_buds_lists(c);
1328
1329 /* Create background thread */
1330 c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
1331 if (!c->bgt)
1332 c->bgt = ERR_PTR(-EINVAL);
1333 if (IS_ERR(c->bgt)) {
1334 err = PTR_ERR(c->bgt);
1335 c->bgt = NULL;
1336 ubifs_err("cannot spawn \"%s\", error %d",
1337 c->bgt_name, err);
1338 return err;
1339 }
1340 wake_up_process(c->bgt);
1341
1342 c->orph_buf = vmalloc(c->leb_size);
1343 if (!c->orph_buf)
1344 return -ENOMEM;
1345
1346 /* Check for enough log space */
1347 lnum = c->lhead_lnum + 1;
1348 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
1349 lnum = UBIFS_LOG_LNUM;
1350 if (lnum == c->ltail_lnum) {
1351 err = ubifs_consolidate_log(c);
1352 if (err)
1353 goto out;
1354 }
1355
1356 if (c->need_recovery)
1357 err = ubifs_rcvry_gc_commit(c);
1358 else
1359 err = take_gc_lnum(c);
1360 if (err)
1361 goto out;
1362
1363 if (c->need_recovery) {
1364 c->need_recovery = 0;
1365 ubifs_msg("deferred recovery completed");
1366 }
1367
1368 dbg_gen("re-mounted read-write");
1369 c->vfs_sb->s_flags &= ~MS_RDONLY;
1370 c->remounting_rw = 0;
1371 mutex_unlock(&c->umount_mutex);
1372 return 0;
1373
1374out:
1375 vfree(c->orph_buf);
1376 c->orph_buf = NULL;
1377 if (c->bgt) {
1378 kthread_stop(c->bgt);
1379 c->bgt = NULL;
1380 }
1381 free_wbufs(c);
1382 vfree(c->ileb_buf);
1383 c->ileb_buf = NULL;
1384 ubifs_lpt_free(c, 1);
1385 c->remounting_rw = 0;
1386 mutex_unlock(&c->umount_mutex);
1387 return err;
1388}
1389
1390/**
1391 * commit_on_unmount - commit the journal when un-mounting.
1392 * @c: UBIFS file-system description object
1393 *
1394 * This function is called during un-mounting and it commits the journal unless
1395 * the "fast unmount" mode is enabled. It also avoids committing the journal if
1396 * it contains too few data.
1397 *
1398 * Sometimes recovery requires the journal to be committed at least once, and
1399 * this function takes care about this.
1400 */
1401static void commit_on_unmount(struct ubifs_info *c)
1402{
1403 if (!c->fast_unmount) {
1404 long long bud_bytes;
1405
1406 spin_lock(&c->buds_lock);
1407 bud_bytes = c->bud_bytes;
1408 spin_unlock(&c->buds_lock);
1409 if (bud_bytes > c->leb_size)
1410 ubifs_run_commit(c);
1411 }
1412}
1413
1414/**
1415 * ubifs_remount_ro - re-mount in read-only mode.
1416 * @c: UBIFS file-system description object
1417 *
1418 * We rely on VFS to have stopped writing. Possibly the background thread could
1419 * be running a commit, however kthread_stop will wait in that case.
1420 */
1421static void ubifs_remount_ro(struct ubifs_info *c)
1422{
1423 int i, err;
1424
1425 ubifs_assert(!c->need_recovery);
1426 commit_on_unmount(c);
1427
1428 mutex_lock(&c->umount_mutex);
1429 if (c->bgt) {
1430 kthread_stop(c->bgt);
1431 c->bgt = NULL;
1432 }
1433
1434 for (i = 0; i < c->jhead_cnt; i++) {
1435 ubifs_wbuf_sync(&c->jheads[i].wbuf);
1436 del_timer_sync(&c->jheads[i].wbuf.timer);
1437 }
1438
1439 if (!c->ro_media) {
1440 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
1441 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
1442 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
1443 err = ubifs_write_master(c);
1444 if (err)
1445 ubifs_ro_mode(c, err);
1446 }
1447
1448 ubifs_destroy_idx_gc(c);
1449 free_wbufs(c);
1450 vfree(c->orph_buf);
1451 c->orph_buf = NULL;
1452 vfree(c->ileb_buf);
1453 c->ileb_buf = NULL;
1454 ubifs_lpt_free(c, 1);
1455 mutex_unlock(&c->umount_mutex);
1456}
1457
1458static void ubifs_put_super(struct super_block *sb)
1459{
1460 int i;
1461 struct ubifs_info *c = sb->s_fs_info;
1462
1463 ubifs_msg("un-mount UBI device %d, volume %d", c->vi.ubi_num,
1464 c->vi.vol_id);
1465 /*
1466 * The following asserts are only valid if there has not been a failure
1467 * of the media. For example, there will be dirty inodes if we failed
1468 * to write them back because of I/O errors.
1469 */
1470 ubifs_assert(atomic_long_read(&c->dirty_pg_cnt) == 0);
1471 ubifs_assert(c->budg_idx_growth == 0);
1472 ubifs_assert(c->budg_data_growth == 0);
1473
1474 /*
1475 * The 'c->umount_lock' prevents races between UBIFS memory shrinker
1476 * and file system un-mount. Namely, it prevents the shrinker from
1477 * picking this superblock for shrinking - it will be just skipped if
1478 * the mutex is locked.
1479 */
1480 mutex_lock(&c->umount_mutex);
1481 if (!(c->vfs_sb->s_flags & MS_RDONLY)) {
1482 /*
1483 * First of all kill the background thread to make sure it does
1484 * not interfere with un-mounting and freeing resources.
1485 */
1486 if (c->bgt) {
1487 kthread_stop(c->bgt);
1488 c->bgt = NULL;
1489 }
1490
1491 /* Synchronize write-buffers */
1492 if (c->jheads)
1493 for (i = 0; i < c->jhead_cnt; i++) {
1494 ubifs_wbuf_sync(&c->jheads[i].wbuf);
1495 del_timer_sync(&c->jheads[i].wbuf.timer);
1496 }
1497
1498 /*
1499 * On fatal errors c->ro_media is set to 1, in which case we do
1500 * not write the master node.
1501 */
1502 if (!c->ro_media) {
1503 /*
1504 * We are being cleanly unmounted which means the
1505 * orphans were killed - indicate this in the master
1506 * node. Also save the reserved GC LEB number.
1507 */
1508 int err;
1509
1510 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
1511 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
1512 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
1513 err = ubifs_write_master(c);
1514 if (err)
1515 /*
1516 * Recovery will attempt to fix the master area
1517 * next mount, so we just print a message and
1518 * continue to unmount normally.
1519 */
1520 ubifs_err("failed to write master node, "
1521 "error %d", err);
1522 }
1523 }
1524
1525 ubifs_umount(c);
1526 bdi_destroy(&c->bdi);
1527 ubi_close_volume(c->ubi);
1528 mutex_unlock(&c->umount_mutex);
1529 kfree(c);
1530}
1531
1532static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
1533{
1534 int err;
1535 struct ubifs_info *c = sb->s_fs_info;
1536
1537 dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags);
1538
1539 err = ubifs_parse_options(c, data, 1);
1540 if (err) {
1541 ubifs_err("invalid or unknown remount parameter");
1542 return err;
1543 }
1544 if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
1545 err = ubifs_remount_rw(c);
1546 if (err)
1547 return err;
1548 } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
1549 ubifs_remount_ro(c);
1550
1551 return 0;
1552}
1553
1554struct super_operations ubifs_super_operations = {
1555 .alloc_inode = ubifs_alloc_inode,
1556 .destroy_inode = ubifs_destroy_inode,
1557 .put_super = ubifs_put_super,
1558 .write_inode = ubifs_write_inode,
1559 .delete_inode = ubifs_delete_inode,
1560 .statfs = ubifs_statfs,
1561 .dirty_inode = ubifs_dirty_inode,
1562 .remount_fs = ubifs_remount_fs,
1563 .show_options = ubifs_show_options,
1564 .sync_fs = ubifs_sync_fs,
1565};
1566
1567/**
1568 * open_ubi - parse UBI device name string and open the UBI device.
1569 * @name: UBI volume name
1570 * @mode: UBI volume open mode
1571 *
1572 * There are several ways to specify UBI volumes when mounting UBIFS:
1573 * o ubiX_Y - UBI device number X, volume Y;
1574 * o ubiY - UBI device number 0, volume Y;
1575 * o ubiX:NAME - mount UBI device X, volume with name NAME;
1576 * o ubi:NAME - mount UBI device 0, volume with name NAME.
1577 *
1578 * Alternative '!' separator may be used instead of ':' (because some shells
1579 * like busybox may interpret ':' as an NFS host name separator). This function
1580 * returns ubi volume object in case of success and a negative error code in
1581 * case of failure.
1582 */
1583static struct ubi_volume_desc *open_ubi(const char *name, int mode)
1584{
1585 int dev, vol;
1586 char *endptr;
1587
1588 if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
1589 return ERR_PTR(-EINVAL);
1590
1591 /* ubi:NAME method */
1592 if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
1593 return ubi_open_volume_nm(0, name + 4, mode);
1594
1595 if (!isdigit(name[3]))
1596 return ERR_PTR(-EINVAL);
1597
1598 dev = simple_strtoul(name + 3, &endptr, 0);
1599
1600 /* ubiY method */
1601 if (*endptr == '\0')
1602 return ubi_open_volume(0, dev, mode);
1603
1604 /* ubiX_Y method */
1605 if (*endptr == '_' && isdigit(endptr[1])) {
1606 vol = simple_strtoul(endptr + 1, &endptr, 0);
1607 if (*endptr != '\0')
1608 return ERR_PTR(-EINVAL);
1609 return ubi_open_volume(dev, vol, mode);
1610 }
1611
1612 /* ubiX:NAME method */
1613 if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
1614 return ubi_open_volume_nm(dev, ++endptr, mode);
1615
1616 return ERR_PTR(-EINVAL);
1617}
1618
1619static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
1620{
1621 struct ubi_volume_desc *ubi = sb->s_fs_info;
1622 struct ubifs_info *c;
1623 struct inode *root;
1624 int err;
1625
1626 c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
1627 if (!c)
1628 return -ENOMEM;
1629
1630 spin_lock_init(&c->cnt_lock);
1631 spin_lock_init(&c->cs_lock);
1632 spin_lock_init(&c->buds_lock);
1633 spin_lock_init(&c->space_lock);
1634 spin_lock_init(&c->orphan_lock);
1635 init_rwsem(&c->commit_sem);
1636 mutex_init(&c->lp_mutex);
1637 mutex_init(&c->tnc_mutex);
1638 mutex_init(&c->log_mutex);
1639 mutex_init(&c->mst_mutex);
1640 mutex_init(&c->umount_mutex);
1641 init_waitqueue_head(&c->cmt_wq);
1642 c->buds = RB_ROOT;
1643 c->old_idx = RB_ROOT;
1644 c->size_tree = RB_ROOT;
1645 c->orph_tree = RB_ROOT;
1646 INIT_LIST_HEAD(&c->infos_list);
1647 INIT_LIST_HEAD(&c->idx_gc);
1648 INIT_LIST_HEAD(&c->replay_list);
1649 INIT_LIST_HEAD(&c->replay_buds);
1650 INIT_LIST_HEAD(&c->uncat_list);
1651 INIT_LIST_HEAD(&c->empty_list);
1652 INIT_LIST_HEAD(&c->freeable_list);
1653 INIT_LIST_HEAD(&c->frdi_idx_list);
1654 INIT_LIST_HEAD(&c->unclean_leb_list);
1655 INIT_LIST_HEAD(&c->old_buds);
1656 INIT_LIST_HEAD(&c->orph_list);
1657 INIT_LIST_HEAD(&c->orph_new);
1658
1659 c->highest_inum = UBIFS_FIRST_INO;
1660 get_random_bytes(&c->vfs_gen, sizeof(int));
1661 c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
1662
1663 ubi_get_volume_info(ubi, &c->vi);
1664 ubi_get_device_info(c->vi.ubi_num, &c->di);
1665
1666 /* Re-open the UBI device in read-write mode */
1667 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE);
1668 if (IS_ERR(c->ubi)) {
1669 err = PTR_ERR(c->ubi);
1670 goto out_free;
1671 }
1672
1673 /*
1674 * UBIFS provids 'backing_dev_info' in order to disable readahead. For
1675 * UBIFS, I/O is not deferred, it is done immediately in readpage,
1676 * which means the user would have to wait not just for their own I/O
1677 * but the readahead I/O as well i.e. completely pointless.
1678 *
1679 * Read-ahead will be disabled because @c->bdi.ra_pages is 0.
1680 */
1681 c->bdi.capabilities = BDI_CAP_MAP_COPY;
1682 c->bdi.unplug_io_fn = default_unplug_io_fn;
1683 err = bdi_init(&c->bdi);
1684 if (err)
1685 goto out_close;
1686
1687 err = ubifs_parse_options(c, data, 0);
1688 if (err)
1689 goto out_bdi;
1690
1691 c->vfs_sb = sb;
1692
1693 sb->s_fs_info = c;
1694 sb->s_magic = UBIFS_SUPER_MAGIC;
1695 sb->s_blocksize = UBIFS_BLOCK_SIZE;
1696 sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
1697 sb->s_dev = c->vi.cdev;
1698 sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
1699 if (c->max_inode_sz > MAX_LFS_FILESIZE)
1700 sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
1701 sb->s_op = &ubifs_super_operations;
1702
1703 mutex_lock(&c->umount_mutex);
1704 err = mount_ubifs(c);
1705 if (err) {
1706 ubifs_assert(err < 0);
1707 goto out_unlock;
1708 }
1709
1710 /* Read the root inode */
1711 root = ubifs_iget(sb, UBIFS_ROOT_INO);
1712 if (IS_ERR(root)) {
1713 err = PTR_ERR(root);
1714 goto out_umount;
1715 }
1716
1717 sb->s_root = d_alloc_root(root);
1718 if (!sb->s_root)
1719 goto out_iput;
1720
1721 mutex_unlock(&c->umount_mutex);
1722
1723 return 0;
1724
1725out_iput:
1726 iput(root);
1727out_umount:
1728 ubifs_umount(c);
1729out_unlock:
1730 mutex_unlock(&c->umount_mutex);
1731out_bdi:
1732 bdi_destroy(&c->bdi);
1733out_close:
1734 ubi_close_volume(c->ubi);
1735out_free:
1736 kfree(c);
1737 return err;
1738}
1739
1740static int sb_test(struct super_block *sb, void *data)
1741{
1742 dev_t *dev = data;
1743
1744 return sb->s_dev == *dev;
1745}
1746
1747static int sb_set(struct super_block *sb, void *data)
1748{
1749 dev_t *dev = data;
1750
1751 sb->s_dev = *dev;
1752 return 0;
1753}
1754
1755static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
1756 const char *name, void *data, struct vfsmount *mnt)
1757{
1758 struct ubi_volume_desc *ubi;
1759 struct ubi_volume_info vi;
1760 struct super_block *sb;
1761 int err;
1762
1763 dbg_gen("name %s, flags %#x", name, flags);
1764
1765 /*
1766 * Get UBI device number and volume ID. Mount it read-only so far
1767 * because this might be a new mount point, and UBI allows only one
1768 * read-write user at a time.
1769 */
1770 ubi = open_ubi(name, UBI_READONLY);
1771 if (IS_ERR(ubi)) {
1772 ubifs_err("cannot open \"%s\", error %d",
1773 name, (int)PTR_ERR(ubi));
1774 return PTR_ERR(ubi);
1775 }
1776 ubi_get_volume_info(ubi, &vi);
1777
1778 dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
1779
1780 sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
1781 if (IS_ERR(sb)) {
1782 err = PTR_ERR(sb);
1783 goto out_close;
1784 }
1785
1786 if (sb->s_root) {
1787 /* A new mount point for already mounted UBIFS */
1788 dbg_gen("this ubi volume is already mounted");
1789 if ((flags ^ sb->s_flags) & MS_RDONLY) {
1790 err = -EBUSY;
1791 goto out_deact;
1792 }
1793 } else {
1794 sb->s_flags = flags;
1795 /*
1796 * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
1797 * replaced by 'c'.
1798 */
1799 sb->s_fs_info = ubi;
1800 err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
1801 if (err)
1802 goto out_deact;
1803 /* We do not support atime */
1804 sb->s_flags |= MS_ACTIVE | MS_NOATIME;
1805 }
1806
1807 /* 'fill_super()' opens ubi again so we must close it here */
1808 ubi_close_volume(ubi);
1809
1810 return simple_set_mnt(mnt, sb);
1811
1812out_deact:
1813 up_write(&sb->s_umount);
1814 deactivate_super(sb);
1815out_close:
1816 ubi_close_volume(ubi);
1817 return err;
1818}
1819
1820static void ubifs_kill_sb(struct super_block *sb)
1821{
1822 struct ubifs_info *c = sb->s_fs_info;
1823
1824 /*
1825 * We do 'commit_on_unmount()' here instead of 'ubifs_put_super()'
1826 * in order to be outside BKL.
1827 */
1828 if (sb->s_root && !(sb->s_flags & MS_RDONLY))
1829 commit_on_unmount(c);
1830 /* The un-mount routine is actually done in put_super() */
1831 generic_shutdown_super(sb);
1832}
1833
1834static struct file_system_type ubifs_fs_type = {
1835 .name = "ubifs",
1836 .owner = THIS_MODULE,
1837 .get_sb = ubifs_get_sb,
1838 .kill_sb = ubifs_kill_sb
1839};
1840
1841/*
1842 * Inode slab cache constructor.
1843 */
1844static void inode_slab_ctor(struct kmem_cache *cachep, void *obj)
1845{
1846 struct ubifs_inode *ui = obj;
1847 inode_init_once(&ui->vfs_inode);
1848}
1849
1850static int __init ubifs_init(void)
1851{
1852 int err;
1853
1854 BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
1855
1856 /* Make sure node sizes are 8-byte aligned */
1857 BUILD_BUG_ON(UBIFS_CH_SZ & 7);
1858 BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
1859 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
1860 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
1861 BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
1862 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
1863 BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
1864 BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
1865 BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
1866 BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
1867 BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
1868
1869 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
1870 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
1871 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
1872 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
1873 BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
1874 BUILD_BUG_ON(MIN_WRITE_SZ & 7);
1875
1876 /* Check min. node size */
1877 BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
1878 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
1879 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
1880 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
1881
1882 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
1883 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
1884 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
1885 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
1886
1887 /* Defined node sizes */
1888 BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
1889 BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
1890 BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
1891 BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
1892
1893 /*
1894 * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
1895 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
1896 */
1897 if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
1898 ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
1899 " at least 4096 bytes",
1900 (unsigned int)PAGE_CACHE_SIZE);
1901 return -EINVAL;
1902 }
1903
1904 err = register_filesystem(&ubifs_fs_type);
1905 if (err) {
1906 ubifs_err("cannot register file system, error %d", err);
1907 return err;
1908 }
1909
1910 err = -ENOMEM;
1911 ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab",
1912 sizeof(struct ubifs_inode), 0,
1913 SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT,
1914 &inode_slab_ctor);
1915 if (!ubifs_inode_slab)
1916 goto out_reg;
1917
1918 register_shrinker(&ubifs_shrinker_info);
1919
1920 err = ubifs_compressors_init();
1921 if (err)
1922 goto out_compr;
1923
1924 return 0;
1925
1926out_compr:
1927 unregister_shrinker(&ubifs_shrinker_info);
1928 kmem_cache_destroy(ubifs_inode_slab);
1929out_reg:
1930 unregister_filesystem(&ubifs_fs_type);
1931 return err;
1932}
1933/* late_initcall to let compressors initialize first */
1934late_initcall(ubifs_init);
1935
1936static void __exit ubifs_exit(void)
1937{
1938 ubifs_assert(list_empty(&ubifs_infos));
1939 ubifs_assert(atomic_long_read(&ubifs_clean_zn_cnt) == 0);
1940
1941 ubifs_compressors_exit();
1942 unregister_shrinker(&ubifs_shrinker_info);
1943 kmem_cache_destroy(ubifs_inode_slab);
1944 unregister_filesystem(&ubifs_fs_type);
1945}
1946module_exit(ubifs_exit);
1947
1948MODULE_LICENSE("GPL");
1949MODULE_VERSION(__stringify(UBIFS_VERSION));
1950MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
1951MODULE_DESCRIPTION("UBIFS - UBI File System");
diff --git a/fs/ubifs/tnc.c b/fs/ubifs/tnc.c
new file mode 100644
index 000000000000..e909f4a96443
--- /dev/null
+++ b/fs/ubifs/tnc.c
@@ -0,0 +1,2956 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements TNC (Tree Node Cache) which caches indexing nodes of
25 * the UBIFS B-tree.
26 *
27 * At the moment the locking rules of the TNC tree are quite simple and
28 * straightforward. We just have a mutex and lock it when we traverse the
29 * tree. If a znode is not in memory, we read it from flash while still having
30 * the mutex locked.
31 */
32
33#include <linux/crc32.h>
34#include "ubifs.h"
35
36/*
37 * Returned codes of 'matches_name()' and 'fallible_matches_name()' functions.
38 * @NAME_LESS: name corresponding to the first argument is less than second
39 * @NAME_MATCHES: names match
40 * @NAME_GREATER: name corresponding to the second argument is greater than
41 * first
42 * @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
43 *
44 * These constants were introduce to improve readability.
45 */
46enum {
47 NAME_LESS = 0,
48 NAME_MATCHES = 1,
49 NAME_GREATER = 2,
50 NOT_ON_MEDIA = 3,
51};
52
53/**
54 * insert_old_idx - record an index node obsoleted since the last commit start.
55 * @c: UBIFS file-system description object
56 * @lnum: LEB number of obsoleted index node
57 * @offs: offset of obsoleted index node
58 *
59 * Returns %0 on success, and a negative error code on failure.
60 *
61 * For recovery, there must always be a complete intact version of the index on
62 * flash at all times. That is called the "old index". It is the index as at the
63 * time of the last successful commit. Many of the index nodes in the old index
64 * may be dirty, but they must not be erased until the next successful commit
65 * (at which point that index becomes the old index).
66 *
67 * That means that the garbage collection and the in-the-gaps method of
68 * committing must be able to determine if an index node is in the old index.
69 * Most of the old index nodes can be found by looking up the TNC using the
70 * 'lookup_znode()' function. However, some of the old index nodes may have
71 * been deleted from the current index or may have been changed so much that
72 * they cannot be easily found. In those cases, an entry is added to an RB-tree.
73 * That is what this function does. The RB-tree is ordered by LEB number and
74 * offset because they uniquely identify the old index node.
75 */
76static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
77{
78 struct ubifs_old_idx *old_idx, *o;
79 struct rb_node **p, *parent = NULL;
80
81 old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
82 if (unlikely(!old_idx))
83 return -ENOMEM;
84 old_idx->lnum = lnum;
85 old_idx->offs = offs;
86
87 p = &c->old_idx.rb_node;
88 while (*p) {
89 parent = *p;
90 o = rb_entry(parent, struct ubifs_old_idx, rb);
91 if (lnum < o->lnum)
92 p = &(*p)->rb_left;
93 else if (lnum > o->lnum)
94 p = &(*p)->rb_right;
95 else if (offs < o->offs)
96 p = &(*p)->rb_left;
97 else if (offs > o->offs)
98 p = &(*p)->rb_right;
99 else {
100 ubifs_err("old idx added twice!");
101 kfree(old_idx);
102 return 0;
103 }
104 }
105 rb_link_node(&old_idx->rb, parent, p);
106 rb_insert_color(&old_idx->rb, &c->old_idx);
107 return 0;
108}
109
110/**
111 * insert_old_idx_znode - record a znode obsoleted since last commit start.
112 * @c: UBIFS file-system description object
113 * @znode: znode of obsoleted index node
114 *
115 * Returns %0 on success, and a negative error code on failure.
116 */
117int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
118{
119 if (znode->parent) {
120 struct ubifs_zbranch *zbr;
121
122 zbr = &znode->parent->zbranch[znode->iip];
123 if (zbr->len)
124 return insert_old_idx(c, zbr->lnum, zbr->offs);
125 } else
126 if (c->zroot.len)
127 return insert_old_idx(c, c->zroot.lnum,
128 c->zroot.offs);
129 return 0;
130}
131
132/**
133 * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
134 * @c: UBIFS file-system description object
135 * @znode: znode of obsoleted index node
136 *
137 * Returns %0 on success, and a negative error code on failure.
138 */
139static int ins_clr_old_idx_znode(struct ubifs_info *c,
140 struct ubifs_znode *znode)
141{
142 int err;
143
144 if (znode->parent) {
145 struct ubifs_zbranch *zbr;
146
147 zbr = &znode->parent->zbranch[znode->iip];
148 if (zbr->len) {
149 err = insert_old_idx(c, zbr->lnum, zbr->offs);
150 if (err)
151 return err;
152 zbr->lnum = 0;
153 zbr->offs = 0;
154 zbr->len = 0;
155 }
156 } else
157 if (c->zroot.len) {
158 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
159 if (err)
160 return err;
161 c->zroot.lnum = 0;
162 c->zroot.offs = 0;
163 c->zroot.len = 0;
164 }
165 return 0;
166}
167
168/**
169 * destroy_old_idx - destroy the old_idx RB-tree.
170 * @c: UBIFS file-system description object
171 *
172 * During start commit, the old_idx RB-tree is used to avoid overwriting index
173 * nodes that were in the index last commit but have since been deleted. This
174 * is necessary for recovery i.e. the old index must be kept intact until the
175 * new index is successfully written. The old-idx RB-tree is used for the
176 * in-the-gaps method of writing index nodes and is destroyed every commit.
177 */
178void destroy_old_idx(struct ubifs_info *c)
179{
180 struct rb_node *this = c->old_idx.rb_node;
181 struct ubifs_old_idx *old_idx;
182
183 while (this) {
184 if (this->rb_left) {
185 this = this->rb_left;
186 continue;
187 } else if (this->rb_right) {
188 this = this->rb_right;
189 continue;
190 }
191 old_idx = rb_entry(this, struct ubifs_old_idx, rb);
192 this = rb_parent(this);
193 if (this) {
194 if (this->rb_left == &old_idx->rb)
195 this->rb_left = NULL;
196 else
197 this->rb_right = NULL;
198 }
199 kfree(old_idx);
200 }
201 c->old_idx = RB_ROOT;
202}
203
204/**
205 * copy_znode - copy a dirty znode.
206 * @c: UBIFS file-system description object
207 * @znode: znode to copy
208 *
209 * A dirty znode being committed may not be changed, so it is copied.
210 */
211static struct ubifs_znode *copy_znode(struct ubifs_info *c,
212 struct ubifs_znode *znode)
213{
214 struct ubifs_znode *zn;
215
216 zn = kmalloc(c->max_znode_sz, GFP_NOFS);
217 if (unlikely(!zn))
218 return ERR_PTR(-ENOMEM);
219
220 memcpy(zn, znode, c->max_znode_sz);
221 zn->cnext = NULL;
222 __set_bit(DIRTY_ZNODE, &zn->flags);
223 __clear_bit(COW_ZNODE, &zn->flags);
224
225 ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
226 __set_bit(OBSOLETE_ZNODE, &znode->flags);
227
228 if (znode->level != 0) {
229 int i;
230 const int n = zn->child_cnt;
231
232 /* The children now have new parent */
233 for (i = 0; i < n; i++) {
234 struct ubifs_zbranch *zbr = &zn->zbranch[i];
235
236 if (zbr->znode)
237 zbr->znode->parent = zn;
238 }
239 }
240
241 atomic_long_inc(&c->dirty_zn_cnt);
242 return zn;
243}
244
245/**
246 * add_idx_dirt - add dirt due to a dirty znode.
247 * @c: UBIFS file-system description object
248 * @lnum: LEB number of index node
249 * @dirt: size of index node
250 *
251 * This function updates lprops dirty space and the new size of the index.
252 */
253static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
254{
255 c->calc_idx_sz -= ALIGN(dirt, 8);
256 return ubifs_add_dirt(c, lnum, dirt);
257}
258
259/**
260 * dirty_cow_znode - ensure a znode is not being committed.
261 * @c: UBIFS file-system description object
262 * @zbr: branch of znode to check
263 *
264 * Returns dirtied znode on success or negative error code on failure.
265 */
266static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
267 struct ubifs_zbranch *zbr)
268{
269 struct ubifs_znode *znode = zbr->znode;
270 struct ubifs_znode *zn;
271 int err;
272
273 if (!test_bit(COW_ZNODE, &znode->flags)) {
274 /* znode is not being committed */
275 if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
276 atomic_long_inc(&c->dirty_zn_cnt);
277 atomic_long_dec(&c->clean_zn_cnt);
278 atomic_long_dec(&ubifs_clean_zn_cnt);
279 err = add_idx_dirt(c, zbr->lnum, zbr->len);
280 if (unlikely(err))
281 return ERR_PTR(err);
282 }
283 return znode;
284 }
285
286 zn = copy_znode(c, znode);
287 if (unlikely(IS_ERR(zn)))
288 return zn;
289
290 if (zbr->len) {
291 err = insert_old_idx(c, zbr->lnum, zbr->offs);
292 if (unlikely(err))
293 return ERR_PTR(err);
294 err = add_idx_dirt(c, zbr->lnum, zbr->len);
295 } else
296 err = 0;
297
298 zbr->znode = zn;
299 zbr->lnum = 0;
300 zbr->offs = 0;
301 zbr->len = 0;
302
303 if (unlikely(err))
304 return ERR_PTR(err);
305 return zn;
306}
307
308/**
309 * lnc_add - add a leaf node to the leaf node cache.
310 * @c: UBIFS file-system description object
311 * @zbr: zbranch of leaf node
312 * @node: leaf node
313 *
314 * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
315 * purpose of the leaf node cache is to save re-reading the same leaf node over
316 * and over again. Most things are cached by VFS, however the file system must
317 * cache directory entries for readdir and for resolving hash collisions. The
318 * present implementation of the leaf node cache is extremely simple, and
319 * allows for error returns that are not used but that may be needed if a more
320 * complex implementation is created.
321 *
322 * Note, this function does not add the @node object to LNC directly, but
323 * allocates a copy of the object and adds the copy to LNC. The reason for this
324 * is that @node has been allocated outside of the TNC subsystem and will be
325 * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
326 * may be changed at any time, e.g. freed by the shrinker.
327 */
328static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
329 const void *node)
330{
331 int err;
332 void *lnc_node;
333 const struct ubifs_dent_node *dent = node;
334
335 ubifs_assert(!zbr->leaf);
336 ubifs_assert(zbr->len != 0);
337 ubifs_assert(is_hash_key(c, &zbr->key));
338
339 err = ubifs_validate_entry(c, dent);
340 if (err) {
341 dbg_dump_stack();
342 dbg_dump_node(c, dent);
343 return err;
344 }
345
346 lnc_node = kmalloc(zbr->len, GFP_NOFS);
347 if (!lnc_node)
348 /* We don't have to have the cache, so no error */
349 return 0;
350
351 memcpy(lnc_node, node, zbr->len);
352 zbr->leaf = lnc_node;
353 return 0;
354}
355
356 /**
357 * lnc_add_directly - add a leaf node to the leaf-node-cache.
358 * @c: UBIFS file-system description object
359 * @zbr: zbranch of leaf node
360 * @node: leaf node
361 *
362 * This function is similar to 'lnc_add()', but it does not create a copy of
363 * @node but inserts @node to TNC directly.
364 */
365static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
366 void *node)
367{
368 int err;
369
370 ubifs_assert(!zbr->leaf);
371 ubifs_assert(zbr->len != 0);
372
373 err = ubifs_validate_entry(c, node);
374 if (err) {
375 dbg_dump_stack();
376 dbg_dump_node(c, node);
377 return err;
378 }
379
380 zbr->leaf = node;
381 return 0;
382}
383
384/**
385 * lnc_free - remove a leaf node from the leaf node cache.
386 * @zbr: zbranch of leaf node
387 * @node: leaf node
388 */
389static void lnc_free(struct ubifs_zbranch *zbr)
390{
391 if (!zbr->leaf)
392 return;
393 kfree(zbr->leaf);
394 zbr->leaf = NULL;
395}
396
397/**
398 * tnc_read_node_nm - read a "hashed" leaf node.
399 * @c: UBIFS file-system description object
400 * @zbr: key and position of the node
401 * @node: node is returned here
402 *
403 * This function reads a "hashed" node defined by @zbr from the leaf node cache
404 * (in it is there) or from the hash media, in which case the node is also
405 * added to LNC. Returns zero in case of success or a negative negative error
406 * code in case of failure.
407 */
408static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
409 void *node)
410{
411 int err;
412
413 ubifs_assert(is_hash_key(c, &zbr->key));
414
415 if (zbr->leaf) {
416 /* Read from the leaf node cache */
417 ubifs_assert(zbr->len != 0);
418 memcpy(node, zbr->leaf, zbr->len);
419 return 0;
420 }
421
422 err = ubifs_tnc_read_node(c, zbr, node);
423 if (err)
424 return err;
425
426 /* Add the node to the leaf node cache */
427 err = lnc_add(c, zbr, node);
428 return err;
429}
430
431/**
432 * try_read_node - read a node if it is a node.
433 * @c: UBIFS file-system description object
434 * @buf: buffer to read to
435 * @type: node type
436 * @len: node length (not aligned)
437 * @lnum: LEB number of node to read
438 * @offs: offset of node to read
439 *
440 * This function tries to read a node of known type and length, checks it and
441 * stores it in @buf. This function returns %1 if a node is present and %0 if
442 * a node is not present. A negative error code is returned for I/O errors.
443 * This function performs that same function as ubifs_read_node except that
444 * it does not require that there is actually a node present and instead
445 * the return code indicates if a node was read.
446 */
447static int try_read_node(const struct ubifs_info *c, void *buf, int type,
448 int len, int lnum, int offs)
449{
450 int err, node_len;
451 struct ubifs_ch *ch = buf;
452 uint32_t crc, node_crc;
453
454 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
455
456 err = ubi_read(c->ubi, lnum, buf, offs, len);
457 if (err) {
458 ubifs_err("cannot read node type %d from LEB %d:%d, error %d",
459 type, lnum, offs, err);
460 return err;
461 }
462
463 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
464 return 0;
465
466 if (ch->node_type != type)
467 return 0;
468
469 node_len = le32_to_cpu(ch->len);
470 if (node_len != len)
471 return 0;
472
473 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
474 node_crc = le32_to_cpu(ch->crc);
475 if (crc != node_crc)
476 return 0;
477
478 return 1;
479}
480
481/**
482 * fallible_read_node - try to read a leaf node.
483 * @c: UBIFS file-system description object
484 * @key: key of node to read
485 * @zbr: position of node
486 * @node: node returned
487 *
488 * This function tries to read a node and returns %1 if the node is read, %0
489 * if the node is not present, and a negative error code in the case of error.
490 */
491static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
492 struct ubifs_zbranch *zbr, void *node)
493{
494 int ret;
495
496 dbg_tnc("LEB %d:%d, key %s", zbr->lnum, zbr->offs, DBGKEY(key));
497
498 ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
499 zbr->offs);
500 if (ret == 1) {
501 union ubifs_key node_key;
502 struct ubifs_dent_node *dent = node;
503
504 /* All nodes have key in the same place */
505 key_read(c, &dent->key, &node_key);
506 if (keys_cmp(c, key, &node_key) != 0)
507 ret = 0;
508 }
509 if (ret == 0)
510 dbg_mnt("dangling branch LEB %d:%d len %d, key %s",
511 zbr->lnum, zbr->offs, zbr->len, DBGKEY(key));
512 return ret;
513}
514
515/**
516 * matches_name - determine if a direntry or xattr entry matches a given name.
517 * @c: UBIFS file-system description object
518 * @zbr: zbranch of dent
519 * @nm: name to match
520 *
521 * This function checks if xentry/direntry referred by zbranch @zbr matches name
522 * @nm. Returns %NAME_MATCHES if it does, %NAME_LESS if the name referred by
523 * @zbr is less than @nm, and %NAME_GREATER if it is greater than @nm. In case
524 * of failure, a negative error code is returned.
525 */
526static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
527 const struct qstr *nm)
528{
529 struct ubifs_dent_node *dent;
530 int nlen, err;
531
532 /* If possible, match against the dent in the leaf node cache */
533 if (!zbr->leaf) {
534 dent = kmalloc(zbr->len, GFP_NOFS);
535 if (!dent)
536 return -ENOMEM;
537
538 err = ubifs_tnc_read_node(c, zbr, dent);
539 if (err)
540 goto out_free;
541
542 /* Add the node to the leaf node cache */
543 err = lnc_add_directly(c, zbr, dent);
544 if (err)
545 goto out_free;
546 } else
547 dent = zbr->leaf;
548
549 nlen = le16_to_cpu(dent->nlen);
550 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
551 if (err == 0) {
552 if (nlen == nm->len)
553 return NAME_MATCHES;
554 else if (nlen < nm->len)
555 return NAME_LESS;
556 else
557 return NAME_GREATER;
558 } else if (err < 0)
559 return NAME_LESS;
560 else
561 return NAME_GREATER;
562
563out_free:
564 kfree(dent);
565 return err;
566}
567
568/**
569 * get_znode - get a TNC znode that may not be loaded yet.
570 * @c: UBIFS file-system description object
571 * @znode: parent znode
572 * @n: znode branch slot number
573 *
574 * This function returns the znode or a negative error code.
575 */
576static struct ubifs_znode *get_znode(struct ubifs_info *c,
577 struct ubifs_znode *znode, int n)
578{
579 struct ubifs_zbranch *zbr;
580
581 zbr = &znode->zbranch[n];
582 if (zbr->znode)
583 znode = zbr->znode;
584 else
585 znode = ubifs_load_znode(c, zbr, znode, n);
586 return znode;
587}
588
589/**
590 * tnc_next - find next TNC entry.
591 * @c: UBIFS file-system description object
592 * @zn: znode is passed and returned here
593 * @n: znode branch slot number is passed and returned here
594 *
595 * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
596 * no next entry, or a negative error code otherwise.
597 */
598static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
599{
600 struct ubifs_znode *znode = *zn;
601 int nn = *n;
602
603 nn += 1;
604 if (nn < znode->child_cnt) {
605 *n = nn;
606 return 0;
607 }
608 while (1) {
609 struct ubifs_znode *zp;
610
611 zp = znode->parent;
612 if (!zp)
613 return -ENOENT;
614 nn = znode->iip + 1;
615 znode = zp;
616 if (nn < znode->child_cnt) {
617 znode = get_znode(c, znode, nn);
618 if (IS_ERR(znode))
619 return PTR_ERR(znode);
620 while (znode->level != 0) {
621 znode = get_znode(c, znode, 0);
622 if (IS_ERR(znode))
623 return PTR_ERR(znode);
624 }
625 nn = 0;
626 break;
627 }
628 }
629 *zn = znode;
630 *n = nn;
631 return 0;
632}
633
634/**
635 * tnc_prev - find previous TNC entry.
636 * @c: UBIFS file-system description object
637 * @zn: znode is returned here
638 * @n: znode branch slot number is passed and returned here
639 *
640 * This function returns %0 if the previous TNC entry is found, %-ENOENT if
641 * there is no next entry, or a negative error code otherwise.
642 */
643static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
644{
645 struct ubifs_znode *znode = *zn;
646 int nn = *n;
647
648 if (nn > 0) {
649 *n = nn - 1;
650 return 0;
651 }
652 while (1) {
653 struct ubifs_znode *zp;
654
655 zp = znode->parent;
656 if (!zp)
657 return -ENOENT;
658 nn = znode->iip - 1;
659 znode = zp;
660 if (nn >= 0) {
661 znode = get_znode(c, znode, nn);
662 if (IS_ERR(znode))
663 return PTR_ERR(znode);
664 while (znode->level != 0) {
665 nn = znode->child_cnt - 1;
666 znode = get_znode(c, znode, nn);
667 if (IS_ERR(znode))
668 return PTR_ERR(znode);
669 }
670 nn = znode->child_cnt - 1;
671 break;
672 }
673 }
674 *zn = znode;
675 *n = nn;
676 return 0;
677}
678
679/**
680 * resolve_collision - resolve a collision.
681 * @c: UBIFS file-system description object
682 * @key: key of a directory or extended attribute entry
683 * @zn: znode is returned here
684 * @n: zbranch number is passed and returned here
685 * @nm: name of the entry
686 *
687 * This function is called for "hashed" keys to make sure that the found key
688 * really corresponds to the looked up node (directory or extended attribute
689 * entry). It returns %1 and sets @zn and @n if the collision is resolved.
690 * %0 is returned if @nm is not found and @zn and @n are set to the previous
691 * entry, i.e. to the entry after which @nm could follow if it were in TNC.
692 * This means that @n may be set to %-1 if the leftmost key in @zn is the
693 * previous one. A negative error code is returned on failures.
694 */
695static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
696 struct ubifs_znode **zn, int *n,
697 const struct qstr *nm)
698{
699 int err;
700
701 err = matches_name(c, &(*zn)->zbranch[*n], nm);
702 if (unlikely(err < 0))
703 return err;
704 if (err == NAME_MATCHES)
705 return 1;
706
707 if (err == NAME_GREATER) {
708 /* Look left */
709 while (1) {
710 err = tnc_prev(c, zn, n);
711 if (err == -ENOENT) {
712 ubifs_assert(*n == 0);
713 *n = -1;
714 return 0;
715 }
716 if (err < 0)
717 return err;
718 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
719 /*
720 * We have found the branch after which we would
721 * like to insert, but inserting in this znode
722 * may still be wrong. Consider the following 3
723 * znodes, in the case where we are resolving a
724 * collision with Key2.
725 *
726 * znode zp
727 * ----------------------
728 * level 1 | Key0 | Key1 |
729 * -----------------------
730 * | |
731 * znode za | | znode zb
732 * ------------ ------------
733 * level 0 | Key0 | | Key2 |
734 * ------------ ------------
735 *
736 * The lookup finds Key2 in znode zb. Lets say
737 * there is no match and the name is greater so
738 * we look left. When we find Key0, we end up
739 * here. If we return now, we will insert into
740 * znode za at slot n = 1. But that is invalid
741 * according to the parent's keys. Key2 must
742 * be inserted into znode zb.
743 *
744 * Note, this problem is not relevant for the
745 * case when we go right, because
746 * 'tnc_insert()' would correct the parent key.
747 */
748 if (*n == (*zn)->child_cnt - 1) {
749 err = tnc_next(c, zn, n);
750 if (err) {
751 /* Should be impossible */
752 ubifs_assert(0);
753 if (err == -ENOENT)
754 err = -EINVAL;
755 return err;
756 }
757 ubifs_assert(*n == 0);
758 *n = -1;
759 }
760 return 0;
761 }
762 err = matches_name(c, &(*zn)->zbranch[*n], nm);
763 if (err < 0)
764 return err;
765 if (err == NAME_LESS)
766 return 0;
767 if (err == NAME_MATCHES)
768 return 1;
769 ubifs_assert(err == NAME_GREATER);
770 }
771 } else {
772 int nn = *n;
773 struct ubifs_znode *znode = *zn;
774
775 /* Look right */
776 while (1) {
777 err = tnc_next(c, &znode, &nn);
778 if (err == -ENOENT)
779 return 0;
780 if (err < 0)
781 return err;
782 if (keys_cmp(c, &znode->zbranch[nn].key, key))
783 return 0;
784 err = matches_name(c, &znode->zbranch[nn], nm);
785 if (err < 0)
786 return err;
787 if (err == NAME_GREATER)
788 return 0;
789 *zn = znode;
790 *n = nn;
791 if (err == NAME_MATCHES)
792 return 1;
793 ubifs_assert(err == NAME_LESS);
794 }
795 }
796}
797
798/**
799 * fallible_matches_name - determine if a dent matches a given name.
800 * @c: UBIFS file-system description object
801 * @zbr: zbranch of dent
802 * @nm: name to match
803 *
804 * This is a "fallible" version of 'matches_name()' function which does not
805 * panic if the direntry/xentry referred by @zbr does not exist on the media.
806 *
807 * This function checks if xentry/direntry referred by zbranch @zbr matches name
808 * @nm. Returns %NAME_MATCHES it does, %NAME_LESS if the name referred by @zbr
809 * is less than @nm, %NAME_GREATER if it is greater than @nm, and @NOT_ON_MEDIA
810 * if xentry/direntry referred by @zbr does not exist on the media. A negative
811 * error code is returned in case of failure.
812 */
813static int fallible_matches_name(struct ubifs_info *c,
814 struct ubifs_zbranch *zbr,
815 const struct qstr *nm)
816{
817 struct ubifs_dent_node *dent;
818 int nlen, err;
819
820 /* If possible, match against the dent in the leaf node cache */
821 if (!zbr->leaf) {
822 dent = kmalloc(zbr->len, GFP_NOFS);
823 if (!dent)
824 return -ENOMEM;
825
826 err = fallible_read_node(c, &zbr->key, zbr, dent);
827 if (err < 0)
828 goto out_free;
829 if (err == 0) {
830 /* The node was not present */
831 err = NOT_ON_MEDIA;
832 goto out_free;
833 }
834 ubifs_assert(err == 1);
835
836 err = lnc_add_directly(c, zbr, dent);
837 if (err)
838 goto out_free;
839 } else
840 dent = zbr->leaf;
841
842 nlen = le16_to_cpu(dent->nlen);
843 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
844 if (err == 0) {
845 if (nlen == nm->len)
846 return NAME_MATCHES;
847 else if (nlen < nm->len)
848 return NAME_LESS;
849 else
850 return NAME_GREATER;
851 } else if (err < 0)
852 return NAME_LESS;
853 else
854 return NAME_GREATER;
855
856out_free:
857 kfree(dent);
858 return err;
859}
860
861/**
862 * fallible_resolve_collision - resolve a collision even if nodes are missing.
863 * @c: UBIFS file-system description object
864 * @key: key
865 * @zn: znode is returned here
866 * @n: branch number is passed and returned here
867 * @nm: name of directory entry
868 * @adding: indicates caller is adding a key to the TNC
869 *
870 * This is a "fallible" version of the 'resolve_collision()' function which
871 * does not panic if one of the nodes referred to by TNC does not exist on the
872 * media. This may happen when replaying the journal if a deleted node was
873 * Garbage-collected and the commit was not done. A branch that refers to a node
874 * that is not present is called a dangling branch. The following are the return
875 * codes for this function:
876 * o if @nm was found, %1 is returned and @zn and @n are set to the found
877 * branch;
878 * o if we are @adding and @nm was not found, %0 is returned;
879 * o if we are not @adding and @nm was not found, but a dangling branch was
880 * found, then %1 is returned and @zn and @n are set to the dangling branch;
881 * o a negative error code is returned in case of failure.
882 */
883static int fallible_resolve_collision(struct ubifs_info *c,
884 const union ubifs_key *key,
885 struct ubifs_znode **zn, int *n,
886 const struct qstr *nm, int adding)
887{
888 struct ubifs_znode *o_znode = NULL, *znode = *zn;
889 int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
890
891 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
892 if (unlikely(cmp < 0))
893 return cmp;
894 if (cmp == NAME_MATCHES)
895 return 1;
896 if (cmp == NOT_ON_MEDIA) {
897 o_znode = znode;
898 o_n = nn;
899 /*
900 * We are unlucky and hit a dangling branch straight away.
901 * Now we do not really know where to go to find the needed
902 * branch - to the left or to the right. Well, let's try left.
903 */
904 unsure = 1;
905 } else if (!adding)
906 unsure = 1; /* Remove a dangling branch wherever it is */
907
908 if (cmp == NAME_GREATER || unsure) {
909 /* Look left */
910 while (1) {
911 err = tnc_prev(c, zn, n);
912 if (err == -ENOENT) {
913 ubifs_assert(*n == 0);
914 *n = -1;
915 break;
916 }
917 if (err < 0)
918 return err;
919 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
920 /* See comments in 'resolve_collision()' */
921 if (*n == (*zn)->child_cnt - 1) {
922 err = tnc_next(c, zn, n);
923 if (err) {
924 /* Should be impossible */
925 ubifs_assert(0);
926 if (err == -ENOENT)
927 err = -EINVAL;
928 return err;
929 }
930 ubifs_assert(*n == 0);
931 *n = -1;
932 }
933 break;
934 }
935 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
936 if (err < 0)
937 return err;
938 if (err == NAME_MATCHES)
939 return 1;
940 if (err == NOT_ON_MEDIA) {
941 o_znode = *zn;
942 o_n = *n;
943 continue;
944 }
945 if (!adding)
946 continue;
947 if (err == NAME_LESS)
948 break;
949 else
950 unsure = 0;
951 }
952 }
953
954 if (cmp == NAME_LESS || unsure) {
955 /* Look right */
956 *zn = znode;
957 *n = nn;
958 while (1) {
959 err = tnc_next(c, &znode, &nn);
960 if (err == -ENOENT)
961 break;
962 if (err < 0)
963 return err;
964 if (keys_cmp(c, &znode->zbranch[nn].key, key))
965 break;
966 err = fallible_matches_name(c, &znode->zbranch[nn], nm);
967 if (err < 0)
968 return err;
969 if (err == NAME_GREATER)
970 break;
971 *zn = znode;
972 *n = nn;
973 if (err == NAME_MATCHES)
974 return 1;
975 if (err == NOT_ON_MEDIA) {
976 o_znode = znode;
977 o_n = nn;
978 }
979 }
980 }
981
982 /* Never match a dangling branch when adding */
983 if (adding || !o_znode)
984 return 0;
985
986 dbg_mnt("dangling match LEB %d:%d len %d %s",
987 o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
988 o_znode->zbranch[o_n].len, DBGKEY(key));
989 *zn = o_znode;
990 *n = o_n;
991 return 1;
992}
993
994/**
995 * matches_position - determine if a zbranch matches a given position.
996 * @zbr: zbranch of dent
997 * @lnum: LEB number of dent to match
998 * @offs: offset of dent to match
999 *
1000 * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
1001 */
1002static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
1003{
1004 if (zbr->lnum == lnum && zbr->offs == offs)
1005 return 1;
1006 else
1007 return 0;
1008}
1009
1010/**
1011 * resolve_collision_directly - resolve a collision directly.
1012 * @c: UBIFS file-system description object
1013 * @key: key of directory entry
1014 * @zn: znode is passed and returned here
1015 * @n: zbranch number is passed and returned here
1016 * @lnum: LEB number of dent node to match
1017 * @offs: offset of dent node to match
1018 *
1019 * This function is used for "hashed" keys to make sure the found directory or
1020 * extended attribute entry node is what was looked for. It is used when the
1021 * flash address of the right node is known (@lnum:@offs) which makes it much
1022 * easier to resolve collisions (no need to read entries and match full
1023 * names). This function returns %1 and sets @zn and @n if the collision is
1024 * resolved, %0 if @lnum:@offs is not found and @zn and @n are set to the
1025 * previous directory entry. Otherwise a negative error code is returned.
1026 */
1027static int resolve_collision_directly(struct ubifs_info *c,
1028 const union ubifs_key *key,
1029 struct ubifs_znode **zn, int *n,
1030 int lnum, int offs)
1031{
1032 struct ubifs_znode *znode;
1033 int nn, err;
1034
1035 znode = *zn;
1036 nn = *n;
1037 if (matches_position(&znode->zbranch[nn], lnum, offs))
1038 return 1;
1039
1040 /* Look left */
1041 while (1) {
1042 err = tnc_prev(c, &znode, &nn);
1043 if (err == -ENOENT)
1044 break;
1045 if (err < 0)
1046 return err;
1047 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1048 break;
1049 if (matches_position(&znode->zbranch[nn], lnum, offs)) {
1050 *zn = znode;
1051 *n = nn;
1052 return 1;
1053 }
1054 }
1055
1056 /* Look right */
1057 znode = *zn;
1058 nn = *n;
1059 while (1) {
1060 err = tnc_next(c, &znode, &nn);
1061 if (err == -ENOENT)
1062 return 0;
1063 if (err < 0)
1064 return err;
1065 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1066 return 0;
1067 *zn = znode;
1068 *n = nn;
1069 if (matches_position(&znode->zbranch[nn], lnum, offs))
1070 return 1;
1071 }
1072}
1073
1074/**
1075 * dirty_cow_bottom_up - dirty a znode and its ancestors.
1076 * @c: UBIFS file-system description object
1077 * @znode: znode to dirty
1078 *
1079 * If we do not have a unique key that resides in a znode, then we cannot
1080 * dirty that znode from the top down (i.e. by using lookup_level0_dirty)
1081 * This function records the path back to the last dirty ancestor, and then
1082 * dirties the znodes on that path.
1083 */
1084static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
1085 struct ubifs_znode *znode)
1086{
1087 struct ubifs_znode *zp;
1088 int *path = c->bottom_up_buf, p = 0;
1089
1090 ubifs_assert(c->zroot.znode);
1091 ubifs_assert(znode);
1092 if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) {
1093 kfree(c->bottom_up_buf);
1094 c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int),
1095 GFP_NOFS);
1096 if (!c->bottom_up_buf)
1097 return ERR_PTR(-ENOMEM);
1098 path = c->bottom_up_buf;
1099 }
1100 if (c->zroot.znode->level) {
1101 /* Go up until parent is dirty */
1102 while (1) {
1103 int n;
1104
1105 zp = znode->parent;
1106 if (!zp)
1107 break;
1108 n = znode->iip;
1109 ubifs_assert(p < c->zroot.znode->level);
1110 path[p++] = n;
1111 if (!zp->cnext && ubifs_zn_dirty(znode))
1112 break;
1113 znode = zp;
1114 }
1115 }
1116
1117 /* Come back down, dirtying as we go */
1118 while (1) {
1119 struct ubifs_zbranch *zbr;
1120
1121 zp = znode->parent;
1122 if (zp) {
1123 ubifs_assert(path[p - 1] >= 0);
1124 ubifs_assert(path[p - 1] < zp->child_cnt);
1125 zbr = &zp->zbranch[path[--p]];
1126 znode = dirty_cow_znode(c, zbr);
1127 } else {
1128 ubifs_assert(znode == c->zroot.znode);
1129 znode = dirty_cow_znode(c, &c->zroot);
1130 }
1131 if (unlikely(IS_ERR(znode)) || !p)
1132 break;
1133 ubifs_assert(path[p - 1] >= 0);
1134 ubifs_assert(path[p - 1] < znode->child_cnt);
1135 znode = znode->zbranch[path[p - 1]].znode;
1136 }
1137
1138 return znode;
1139}
1140
1141/**
1142 * ubifs_lookup_level0 - search for zero-level znode.
1143 * @c: UBIFS file-system description object
1144 * @key: key to lookup
1145 * @zn: znode is returned here
1146 * @n: znode branch slot number is returned here
1147 *
1148 * This function looks up the TNC tree and search for zero-level znode which
1149 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1150 * cases:
1151 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1152 * is returned and slot number of the matched branch is stored in @n;
1153 * o not exact match, which means that zero-level znode does not contain
1154 * @key, then %0 is returned and slot number of the closed branch is stored
1155 * in @n;
1156 * o @key is so small that it is even less than the lowest key of the
1157 * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
1158 *
1159 * Note, when the TNC tree is traversed, some znodes may be absent, then this
1160 * function reads corresponding indexing nodes and inserts them to TNC. In
1161 * case of failure, a negative error code is returned.
1162 */
1163int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1164 struct ubifs_znode **zn, int *n)
1165{
1166 int err, exact;
1167 struct ubifs_znode *znode;
1168 unsigned long time = get_seconds();
1169
1170 dbg_tnc("search key %s", DBGKEY(key));
1171
1172 znode = c->zroot.znode;
1173 if (unlikely(!znode)) {
1174 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1175 if (IS_ERR(znode))
1176 return PTR_ERR(znode);
1177 }
1178
1179 znode->time = time;
1180
1181 while (1) {
1182 struct ubifs_zbranch *zbr;
1183
1184 exact = ubifs_search_zbranch(c, znode, key, n);
1185
1186 if (znode->level == 0)
1187 break;
1188
1189 if (*n < 0)
1190 *n = 0;
1191 zbr = &znode->zbranch[*n];
1192
1193 if (zbr->znode) {
1194 znode->time = time;
1195 znode = zbr->znode;
1196 continue;
1197 }
1198
1199 /* znode is not in TNC cache, load it from the media */
1200 znode = ubifs_load_znode(c, zbr, znode, *n);
1201 if (IS_ERR(znode))
1202 return PTR_ERR(znode);
1203 }
1204
1205 *zn = znode;
1206 if (exact || !is_hash_key(c, key) || *n != -1) {
1207 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1208 return exact;
1209 }
1210
1211 /*
1212 * Here is a tricky place. We have not found the key and this is a
1213 * "hashed" key, which may collide. The rest of the code deals with
1214 * situations like this:
1215 *
1216 * | 3 | 5 |
1217 * / \
1218 * | 3 | 5 | | 6 | 7 | (x)
1219 *
1220 * Or more a complex example:
1221 *
1222 * | 1 | 5 |
1223 * / \
1224 * | 1 | 3 | | 5 | 8 |
1225 * \ /
1226 * | 5 | 5 | | 6 | 7 | (x)
1227 *
1228 * In the examples, if we are looking for key "5", we may reach nodes
1229 * marked with "(x)". In this case what we have do is to look at the
1230 * left and see if there is "5" key there. If there is, we have to
1231 * return it.
1232 *
1233 * Note, this whole situation is possible because we allow to have
1234 * elements which are equivalent to the next key in the parent in the
1235 * children of current znode. For example, this happens if we split a
1236 * znode like this: | 3 | 5 | 5 | 6 | 7 |, which results in something
1237 * like this:
1238 * | 3 | 5 |
1239 * / \
1240 * | 3 | 5 | | 5 | 6 | 7 |
1241 * ^
1242 * And this becomes what is at the first "picture" after key "5" marked
1243 * with "^" is removed. What could be done is we could prohibit
1244 * splitting in the middle of the colliding sequence. Also, when
1245 * removing the leftmost key, we would have to correct the key of the
1246 * parent node, which would introduce additional complications. Namely,
1247 * if we changed the the leftmost key of the parent znode, the garbage
1248 * collector would be unable to find it (GC is doing this when GC'ing
1249 * indexing LEBs). Although we already have an additional RB-tree where
1250 * we save such changed znodes (see 'ins_clr_old_idx_znode()') until
1251 * after the commit. But anyway, this does not look easy to implement
1252 * so we did not try this.
1253 */
1254 err = tnc_prev(c, &znode, n);
1255 if (err == -ENOENT) {
1256 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1257 *n = -1;
1258 return 0;
1259 }
1260 if (unlikely(err < 0))
1261 return err;
1262 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1263 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1264 *n = -1;
1265 return 0;
1266 }
1267
1268 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1269 *zn = znode;
1270 return 1;
1271}
1272
1273/**
1274 * lookup_level0_dirty - search for zero-level znode dirtying.
1275 * @c: UBIFS file-system description object
1276 * @key: key to lookup
1277 * @zn: znode is returned here
1278 * @n: znode branch slot number is returned here
1279 *
1280 * This function looks up the TNC tree and search for zero-level znode which
1281 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1282 * cases:
1283 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1284 * is returned and slot number of the matched branch is stored in @n;
1285 * o not exact match, which means that zero-level znode does not contain @key
1286 * then %0 is returned and slot number of the closed branch is stored in
1287 * @n;
1288 * o @key is so small that it is even less than the lowest key of the
1289 * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
1290 *
1291 * Additionally all znodes in the path from the root to the located zero-level
1292 * znode are marked as dirty.
1293 *
1294 * Note, when the TNC tree is traversed, some znodes may be absent, then this
1295 * function reads corresponding indexing nodes and inserts them to TNC. In
1296 * case of failure, a negative error code is returned.
1297 */
1298static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
1299 struct ubifs_znode **zn, int *n)
1300{
1301 int err, exact;
1302 struct ubifs_znode *znode;
1303 unsigned long time = get_seconds();
1304
1305 dbg_tnc("search and dirty key %s", DBGKEY(key));
1306
1307 znode = c->zroot.znode;
1308 if (unlikely(!znode)) {
1309 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1310 if (IS_ERR(znode))
1311 return PTR_ERR(znode);
1312 }
1313
1314 znode = dirty_cow_znode(c, &c->zroot);
1315 if (IS_ERR(znode))
1316 return PTR_ERR(znode);
1317
1318 znode->time = time;
1319
1320 while (1) {
1321 struct ubifs_zbranch *zbr;
1322
1323 exact = ubifs_search_zbranch(c, znode, key, n);
1324
1325 if (znode->level == 0)
1326 break;
1327
1328 if (*n < 0)
1329 *n = 0;
1330 zbr = &znode->zbranch[*n];
1331
1332 if (zbr->znode) {
1333 znode->time = time;
1334 znode = dirty_cow_znode(c, zbr);
1335 if (IS_ERR(znode))
1336 return PTR_ERR(znode);
1337 continue;
1338 }
1339
1340 /* znode is not in TNC cache, load it from the media */
1341 znode = ubifs_load_znode(c, zbr, znode, *n);
1342 if (IS_ERR(znode))
1343 return PTR_ERR(znode);
1344 znode = dirty_cow_znode(c, zbr);
1345 if (IS_ERR(znode))
1346 return PTR_ERR(znode);
1347 }
1348
1349 *zn = znode;
1350 if (exact || !is_hash_key(c, key) || *n != -1) {
1351 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1352 return exact;
1353 }
1354
1355 /*
1356 * See huge comment at 'lookup_level0_dirty()' what is the rest of the
1357 * code.
1358 */
1359 err = tnc_prev(c, &znode, n);
1360 if (err == -ENOENT) {
1361 *n = -1;
1362 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1363 return 0;
1364 }
1365 if (unlikely(err < 0))
1366 return err;
1367 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1368 *n = -1;
1369 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1370 return 0;
1371 }
1372
1373 if (znode->cnext || !ubifs_zn_dirty(znode)) {
1374 znode = dirty_cow_bottom_up(c, znode);
1375 if (IS_ERR(znode))
1376 return PTR_ERR(znode);
1377 }
1378
1379 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1380 *zn = znode;
1381 return 1;
1382}
1383
1384/**
1385 * ubifs_tnc_lookup - look up a file-system node.
1386 * @c: UBIFS file-system description object
1387 * @key: node key to lookup
1388 * @node: the node is returned here
1389 *
1390 * This function look up and reads node with key @key. The caller has to make
1391 * sure the @node buffer is large enough to fit the node. Returns zero in case
1392 * of success, %-ENOENT if the node was not found, and a negative error code in
1393 * case of failure.
1394 */
1395int ubifs_tnc_lookup(struct ubifs_info *c, const union ubifs_key *key,
1396 void *node)
1397{
1398 int found, n, err;
1399 struct ubifs_znode *znode;
1400 struct ubifs_zbranch zbr, *zt;
1401
1402 mutex_lock(&c->tnc_mutex);
1403 found = ubifs_lookup_level0(c, key, &znode, &n);
1404 if (!found) {
1405 err = -ENOENT;
1406 goto out;
1407 } else if (found < 0) {
1408 err = found;
1409 goto out;
1410 }
1411 zt = &znode->zbranch[n];
1412 if (is_hash_key(c, key)) {
1413 /*
1414 * In this case the leaf node cache gets used, so we pass the
1415 * address of the zbranch and keep the mutex locked
1416 */
1417 err = tnc_read_node_nm(c, zt, node);
1418 goto out;
1419 }
1420 zbr = znode->zbranch[n];
1421 mutex_unlock(&c->tnc_mutex);
1422
1423 err = ubifs_tnc_read_node(c, &zbr, node);
1424 return err;
1425
1426out:
1427 mutex_unlock(&c->tnc_mutex);
1428 return err;
1429}
1430
1431/**
1432 * ubifs_tnc_locate - look up a file-system node and return it and its location.
1433 * @c: UBIFS file-system description object
1434 * @key: node key to lookup
1435 * @node: the node is returned here
1436 * @lnum: LEB number is returned here
1437 * @offs: offset is returned here
1438 *
1439 * This function is the same as 'ubifs_tnc_lookup()' but it returns the node
1440 * location also. See 'ubifs_tnc_lookup()'.
1441 */
1442int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1443 void *node, int *lnum, int *offs)
1444{
1445 int found, n, err;
1446 struct ubifs_znode *znode;
1447 struct ubifs_zbranch zbr, *zt;
1448
1449 mutex_lock(&c->tnc_mutex);
1450 found = ubifs_lookup_level0(c, key, &znode, &n);
1451 if (!found) {
1452 err = -ENOENT;
1453 goto out;
1454 } else if (found < 0) {
1455 err = found;
1456 goto out;
1457 }
1458 zt = &znode->zbranch[n];
1459 if (is_hash_key(c, key)) {
1460 /*
1461 * In this case the leaf node cache gets used, so we pass the
1462 * address of the zbranch and keep the mutex locked
1463 */
1464 *lnum = zt->lnum;
1465 *offs = zt->offs;
1466 err = tnc_read_node_nm(c, zt, node);
1467 goto out;
1468 }
1469 zbr = znode->zbranch[n];
1470 mutex_unlock(&c->tnc_mutex);
1471
1472 *lnum = zbr.lnum;
1473 *offs = zbr.offs;
1474
1475 err = ubifs_tnc_read_node(c, &zbr, node);
1476 return err;
1477
1478out:
1479 mutex_unlock(&c->tnc_mutex);
1480 return err;
1481}
1482
1483/**
1484 * do_lookup_nm- look up a "hashed" node.
1485 * @c: UBIFS file-system description object
1486 * @key: node key to lookup
1487 * @node: the node is returned here
1488 * @nm: node name
1489 *
1490 * This function look up and reads a node which contains name hash in the key.
1491 * Since the hash may have collisions, there may be many nodes with the same
1492 * key, so we have to sequentially look to all of them until the needed one is
1493 * found. This function returns zero in case of success, %-ENOENT if the node
1494 * was not found, and a negative error code in case of failure.
1495 */
1496static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1497 void *node, const struct qstr *nm)
1498{
1499 int found, n, err;
1500 struct ubifs_znode *znode;
1501 struct ubifs_zbranch zbr;
1502
1503 dbg_tnc("name '%.*s' key %s", nm->len, nm->name, DBGKEY(key));
1504 mutex_lock(&c->tnc_mutex);
1505 found = ubifs_lookup_level0(c, key, &znode, &n);
1506 if (!found) {
1507 err = -ENOENT;
1508 goto out_unlock;
1509 } else if (found < 0) {
1510 err = found;
1511 goto out_unlock;
1512 }
1513
1514 ubifs_assert(n >= 0);
1515
1516 err = resolve_collision(c, key, &znode, &n, nm);
1517 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
1518 if (unlikely(err < 0))
1519 goto out_unlock;
1520 if (err == 0) {
1521 err = -ENOENT;
1522 goto out_unlock;
1523 }
1524
1525 zbr = znode->zbranch[n];
1526 mutex_unlock(&c->tnc_mutex);
1527
1528 err = tnc_read_node_nm(c, &zbr, node);
1529 return err;
1530
1531out_unlock:
1532 mutex_unlock(&c->tnc_mutex);
1533 return err;
1534}
1535
1536/**
1537 * ubifs_tnc_lookup_nm - look up a "hashed" node.
1538 * @c: UBIFS file-system description object
1539 * @key: node key to lookup
1540 * @node: the node is returned here
1541 * @nm: node name
1542 *
1543 * This function look up and reads a node which contains name hash in the key.
1544 * Since the hash may have collisions, there may be many nodes with the same
1545 * key, so we have to sequentially look to all of them until the needed one is
1546 * found. This function returns zero in case of success, %-ENOENT if the node
1547 * was not found, and a negative error code in case of failure.
1548 */
1549int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1550 void *node, const struct qstr *nm)
1551{
1552 int err, len;
1553 const struct ubifs_dent_node *dent = node;
1554
1555 /*
1556 * We assume that in most of the cases there are no name collisions and
1557 * 'ubifs_tnc_lookup()' returns us the right direntry.
1558 */
1559 err = ubifs_tnc_lookup(c, key, node);
1560 if (err)
1561 return err;
1562
1563 len = le16_to_cpu(dent->nlen);
1564 if (nm->len == len && !memcmp(dent->name, nm->name, len))
1565 return 0;
1566
1567 /*
1568 * Unluckily, there are hash collisions and we have to iterate over
1569 * them look at each direntry with colliding name hash sequentially.
1570 */
1571 return do_lookup_nm(c, key, node, nm);
1572}
1573
1574/**
1575 * correct_parent_keys - correct parent znodes' keys.
1576 * @c: UBIFS file-system description object
1577 * @znode: znode to correct parent znodes for
1578 *
1579 * This is a helper function for 'tnc_insert()'. When the key of the leftmost
1580 * zbranch changes, keys of parent znodes have to be corrected. This helper
1581 * function is called in such situations and corrects the keys if needed.
1582 */
1583static void correct_parent_keys(const struct ubifs_info *c,
1584 struct ubifs_znode *znode)
1585{
1586 union ubifs_key *key, *key1;
1587
1588 ubifs_assert(znode->parent);
1589 ubifs_assert(znode->iip == 0);
1590
1591 key = &znode->zbranch[0].key;
1592 key1 = &znode->parent->zbranch[0].key;
1593
1594 while (keys_cmp(c, key, key1) < 0) {
1595 key_copy(c, key, key1);
1596 znode = znode->parent;
1597 znode->alt = 1;
1598 if (!znode->parent || znode->iip)
1599 break;
1600 key1 = &znode->parent->zbranch[0].key;
1601 }
1602}
1603
1604/**
1605 * insert_zbranch - insert a zbranch into a znode.
1606 * @znode: znode into which to insert
1607 * @zbr: zbranch to insert
1608 * @n: slot number to insert to
1609 *
1610 * This is a helper function for 'tnc_insert()'. UBIFS does not allow "gaps" in
1611 * znode's array of zbranches and keeps zbranches consolidated, so when a new
1612 * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
1613 * slot, zbranches starting from @n have to be moved right.
1614 */
1615static void insert_zbranch(struct ubifs_znode *znode,
1616 const struct ubifs_zbranch *zbr, int n)
1617{
1618 int i;
1619
1620 ubifs_assert(ubifs_zn_dirty(znode));
1621
1622 if (znode->level) {
1623 for (i = znode->child_cnt; i > n; i--) {
1624 znode->zbranch[i] = znode->zbranch[i - 1];
1625 if (znode->zbranch[i].znode)
1626 znode->zbranch[i].znode->iip = i;
1627 }
1628 if (zbr->znode)
1629 zbr->znode->iip = n;
1630 } else
1631 for (i = znode->child_cnt; i > n; i--)
1632 znode->zbranch[i] = znode->zbranch[i - 1];
1633
1634 znode->zbranch[n] = *zbr;
1635 znode->child_cnt += 1;
1636
1637 /*
1638 * After inserting at slot zero, the lower bound of the key range of
1639 * this znode may have changed. If this znode is subsequently split
1640 * then the upper bound of the key range may change, and furthermore
1641 * it could change to be lower than the original lower bound. If that
1642 * happens, then it will no longer be possible to find this znode in the
1643 * TNC using the key from the index node on flash. That is bad because
1644 * if it is not found, we will assume it is obsolete and may overwrite
1645 * it. Then if there is an unclean unmount, we will start using the
1646 * old index which will be broken.
1647 *
1648 * So we first mark znodes that have insertions at slot zero, and then
1649 * if they are split we add their lnum/offs to the old_idx tree.
1650 */
1651 if (n == 0)
1652 znode->alt = 1;
1653}
1654
1655/**
1656 * tnc_insert - insert a node into TNC.
1657 * @c: UBIFS file-system description object
1658 * @znode: znode to insert into
1659 * @zbr: branch to insert
1660 * @n: slot number to insert new zbranch to
1661 *
1662 * This function inserts a new node described by @zbr into znode @znode. If
1663 * znode does not have a free slot for new zbranch, it is split. Parent znodes
1664 * are splat as well if needed. Returns zero in case of success or a negative
1665 * error code in case of failure.
1666 */
1667static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
1668 struct ubifs_zbranch *zbr, int n)
1669{
1670 struct ubifs_znode *zn, *zi, *zp;
1671 int i, keep, move, appending = 0;
1672 union ubifs_key *key = &zbr->key;
1673
1674 ubifs_assert(n >= 0 && n <= c->fanout);
1675
1676 /* Implement naive insert for now */
1677again:
1678 zp = znode->parent;
1679 if (znode->child_cnt < c->fanout) {
1680 ubifs_assert(n != c->fanout);
1681 dbg_tnc("inserted at %d level %d, key %s", n, znode->level,
1682 DBGKEY(key));
1683
1684 insert_zbranch(znode, zbr, n);
1685
1686 /* Ensure parent's key is correct */
1687 if (n == 0 && zp && znode->iip == 0)
1688 correct_parent_keys(c, znode);
1689
1690 return 0;
1691 }
1692
1693 /*
1694 * Unfortunately, @znode does not have more empty slots and we have to
1695 * split it.
1696 */
1697 dbg_tnc("splitting level %d, key %s", znode->level, DBGKEY(key));
1698
1699 if (znode->alt)
1700 /*
1701 * We can no longer be sure of finding this znode by key, so we
1702 * record it in the old_idx tree.
1703 */
1704 ins_clr_old_idx_znode(c, znode);
1705
1706 zn = kzalloc(c->max_znode_sz, GFP_NOFS);
1707 if (!zn)
1708 return -ENOMEM;
1709 zn->parent = zp;
1710 zn->level = znode->level;
1711
1712 /* Decide where to split */
1713 if (znode->level == 0 && n == c->fanout &&
1714 key_type(c, key) == UBIFS_DATA_KEY) {
1715 union ubifs_key *key1;
1716
1717 /*
1718 * If this is an inode which is being appended - do not split
1719 * it because no other zbranches can be inserted between
1720 * zbranches of consecutive data nodes anyway.
1721 */
1722 key1 = &znode->zbranch[n - 1].key;
1723 if (key_inum(c, key1) == key_inum(c, key) &&
1724 key_type(c, key1) == UBIFS_DATA_KEY &&
1725 key_block(c, key1) == key_block(c, key) - 1)
1726 appending = 1;
1727 }
1728
1729 if (appending) {
1730 keep = c->fanout;
1731 move = 0;
1732 } else {
1733 keep = (c->fanout + 1) / 2;
1734 move = c->fanout - keep;
1735 }
1736
1737 /*
1738 * Although we don't at present, we could look at the neighbors and see
1739 * if we can move some zbranches there.
1740 */
1741
1742 if (n < keep) {
1743 /* Insert into existing znode */
1744 zi = znode;
1745 move += 1;
1746 keep -= 1;
1747 } else {
1748 /* Insert into new znode */
1749 zi = zn;
1750 n -= keep;
1751 /* Re-parent */
1752 if (zn->level != 0)
1753 zbr->znode->parent = zn;
1754 }
1755
1756 __set_bit(DIRTY_ZNODE, &zn->flags);
1757 atomic_long_inc(&c->dirty_zn_cnt);
1758
1759 zn->child_cnt = move;
1760 znode->child_cnt = keep;
1761
1762 dbg_tnc("moving %d, keeping %d", move, keep);
1763
1764 /* Move zbranch */
1765 for (i = 0; i < move; i++) {
1766 zn->zbranch[i] = znode->zbranch[keep + i];
1767 /* Re-parent */
1768 if (zn->level != 0)
1769 if (zn->zbranch[i].znode) {
1770 zn->zbranch[i].znode->parent = zn;
1771 zn->zbranch[i].znode->iip = i;
1772 }
1773 }
1774
1775 /* Insert new key and branch */
1776 dbg_tnc("inserting at %d level %d, key %s", n, zn->level, DBGKEY(key));
1777
1778 insert_zbranch(zi, zbr, n);
1779
1780 /* Insert new znode (produced by spitting) into the parent */
1781 if (zp) {
1782 i = n;
1783 /* Locate insertion point */
1784 n = znode->iip + 1;
1785 if (appending && n != c->fanout)
1786 appending = 0;
1787
1788 if (i == 0 && zi == znode && znode->iip == 0)
1789 correct_parent_keys(c, znode);
1790
1791 /* Tail recursion */
1792 zbr->key = zn->zbranch[0].key;
1793 zbr->znode = zn;
1794 zbr->lnum = 0;
1795 zbr->offs = 0;
1796 zbr->len = 0;
1797 znode = zp;
1798
1799 goto again;
1800 }
1801
1802 /* We have to split root znode */
1803 dbg_tnc("creating new zroot at level %d", znode->level + 1);
1804
1805 zi = kzalloc(c->max_znode_sz, GFP_NOFS);
1806 if (!zi)
1807 return -ENOMEM;
1808
1809 zi->child_cnt = 2;
1810 zi->level = znode->level + 1;
1811
1812 __set_bit(DIRTY_ZNODE, &zi->flags);
1813 atomic_long_inc(&c->dirty_zn_cnt);
1814
1815 zi->zbranch[0].key = znode->zbranch[0].key;
1816 zi->zbranch[0].znode = znode;
1817 zi->zbranch[0].lnum = c->zroot.lnum;
1818 zi->zbranch[0].offs = c->zroot.offs;
1819 zi->zbranch[0].len = c->zroot.len;
1820 zi->zbranch[1].key = zn->zbranch[0].key;
1821 zi->zbranch[1].znode = zn;
1822
1823 c->zroot.lnum = 0;
1824 c->zroot.offs = 0;
1825 c->zroot.len = 0;
1826 c->zroot.znode = zi;
1827
1828 zn->parent = zi;
1829 zn->iip = 1;
1830 znode->parent = zi;
1831 znode->iip = 0;
1832
1833 return 0;
1834}
1835
1836/**
1837 * ubifs_tnc_add - add a node to TNC.
1838 * @c: UBIFS file-system description object
1839 * @key: key to add
1840 * @lnum: LEB number of node
1841 * @offs: node offset
1842 * @len: node length
1843 *
1844 * This function adds a node with key @key to TNC. The node may be new or it may
1845 * obsolete some existing one. Returns %0 on success or negative error code on
1846 * failure.
1847 */
1848int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
1849 int offs, int len)
1850{
1851 int found, n, err = 0;
1852 struct ubifs_znode *znode;
1853
1854 mutex_lock(&c->tnc_mutex);
1855 dbg_tnc("%d:%d, len %d, key %s", lnum, offs, len, DBGKEY(key));
1856 found = lookup_level0_dirty(c, key, &znode, &n);
1857 if (!found) {
1858 struct ubifs_zbranch zbr;
1859
1860 zbr.znode = NULL;
1861 zbr.lnum = lnum;
1862 zbr.offs = offs;
1863 zbr.len = len;
1864 key_copy(c, key, &zbr.key);
1865 err = tnc_insert(c, znode, &zbr, n + 1);
1866 } else if (found == 1) {
1867 struct ubifs_zbranch *zbr = &znode->zbranch[n];
1868
1869 lnc_free(zbr);
1870 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
1871 zbr->lnum = lnum;
1872 zbr->offs = offs;
1873 zbr->len = len;
1874 } else
1875 err = found;
1876 if (!err)
1877 err = dbg_check_tnc(c, 0);
1878 mutex_unlock(&c->tnc_mutex);
1879
1880 return err;
1881}
1882
1883/**
1884 * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
1885 * @c: UBIFS file-system description object
1886 * @key: key to add
1887 * @old_lnum: LEB number of old node
1888 * @old_offs: old node offset
1889 * @lnum: LEB number of node
1890 * @offs: node offset
1891 * @len: node length
1892 *
1893 * This function replaces a node with key @key in the TNC only if the old node
1894 * is found. This function is called by garbage collection when node are moved.
1895 * Returns %0 on success or negative error code on failure.
1896 */
1897int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
1898 int old_lnum, int old_offs, int lnum, int offs, int len)
1899{
1900 int found, n, err = 0;
1901 struct ubifs_znode *znode;
1902
1903 mutex_lock(&c->tnc_mutex);
1904 dbg_tnc("old LEB %d:%d, new LEB %d:%d, len %d, key %s", old_lnum,
1905 old_offs, lnum, offs, len, DBGKEY(key));
1906 found = lookup_level0_dirty(c, key, &znode, &n);
1907 if (found < 0) {
1908 err = found;
1909 goto out_unlock;
1910 }
1911
1912 if (found == 1) {
1913 struct ubifs_zbranch *zbr = &znode->zbranch[n];
1914
1915 found = 0;
1916 if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
1917 lnc_free(zbr);
1918 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
1919 if (err)
1920 goto out_unlock;
1921 zbr->lnum = lnum;
1922 zbr->offs = offs;
1923 zbr->len = len;
1924 found = 1;
1925 } else if (is_hash_key(c, key)) {
1926 found = resolve_collision_directly(c, key, &znode, &n,
1927 old_lnum, old_offs);
1928 dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
1929 found, znode, n, old_lnum, old_offs);
1930 if (found < 0) {
1931 err = found;
1932 goto out_unlock;
1933 }
1934
1935 if (found) {
1936 /* Ensure the znode is dirtied */
1937 if (znode->cnext || !ubifs_zn_dirty(znode)) {
1938 znode = dirty_cow_bottom_up(c,
1939 znode);
1940 if (IS_ERR(znode)) {
1941 err = PTR_ERR(znode);
1942 goto out_unlock;
1943 }
1944 }
1945 zbr = &znode->zbranch[n];
1946 lnc_free(zbr);
1947 err = ubifs_add_dirt(c, zbr->lnum,
1948 zbr->len);
1949 if (err)
1950 goto out_unlock;
1951 zbr->lnum = lnum;
1952 zbr->offs = offs;
1953 zbr->len = len;
1954 }
1955 }
1956 }
1957
1958 if (!found)
1959 err = ubifs_add_dirt(c, lnum, len);
1960
1961 if (!err)
1962 err = dbg_check_tnc(c, 0);
1963
1964out_unlock:
1965 mutex_unlock(&c->tnc_mutex);
1966 return err;
1967}
1968
1969/**
1970 * ubifs_tnc_add_nm - add a "hashed" node to TNC.
1971 * @c: UBIFS file-system description object
1972 * @key: key to add
1973 * @lnum: LEB number of node
1974 * @offs: node offset
1975 * @len: node length
1976 * @nm: node name
1977 *
1978 * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
1979 * may have collisions, like directory entry keys.
1980 */
1981int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
1982 int lnum, int offs, int len, const struct qstr *nm)
1983{
1984 int found, n, err = 0;
1985 struct ubifs_znode *znode;
1986
1987 mutex_lock(&c->tnc_mutex);
1988 dbg_tnc("LEB %d:%d, name '%.*s', key %s", lnum, offs, nm->len, nm->name,
1989 DBGKEY(key));
1990 found = lookup_level0_dirty(c, key, &znode, &n);
1991 if (found < 0) {
1992 err = found;
1993 goto out_unlock;
1994 }
1995
1996 if (found == 1) {
1997 if (c->replaying)
1998 found = fallible_resolve_collision(c, key, &znode, &n,
1999 nm, 1);
2000 else
2001 found = resolve_collision(c, key, &znode, &n, nm);
2002 dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
2003 if (found < 0) {
2004 err = found;
2005 goto out_unlock;
2006 }
2007
2008 /* Ensure the znode is dirtied */
2009 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2010 znode = dirty_cow_bottom_up(c, znode);
2011 if (IS_ERR(znode)) {
2012 err = PTR_ERR(znode);
2013 goto out_unlock;
2014 }
2015 }
2016
2017 if (found == 1) {
2018 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2019
2020 lnc_free(zbr);
2021 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2022 zbr->lnum = lnum;
2023 zbr->offs = offs;
2024 zbr->len = len;
2025 goto out_unlock;
2026 }
2027 }
2028
2029 if (!found) {
2030 struct ubifs_zbranch zbr;
2031
2032 zbr.znode = NULL;
2033 zbr.lnum = lnum;
2034 zbr.offs = offs;
2035 zbr.len = len;
2036 key_copy(c, key, &zbr.key);
2037 err = tnc_insert(c, znode, &zbr, n + 1);
2038 if (err)
2039 goto out_unlock;
2040 if (c->replaying) {
2041 /*
2042 * We did not find it in the index so there may be a
2043 * dangling branch still in the index. So we remove it
2044 * by passing 'ubifs_tnc_remove_nm()' the same key but
2045 * an unmatchable name.
2046 */
2047 struct qstr noname = { .len = 0, .name = "" };
2048
2049 err = dbg_check_tnc(c, 0);
2050 mutex_unlock(&c->tnc_mutex);
2051 if (err)
2052 return err;
2053 return ubifs_tnc_remove_nm(c, key, &noname);
2054 }
2055 }
2056
2057out_unlock:
2058 if (!err)
2059 err = dbg_check_tnc(c, 0);
2060 mutex_unlock(&c->tnc_mutex);
2061 return err;
2062}
2063
2064/**
2065 * tnc_delete - delete a znode form TNC.
2066 * @c: UBIFS file-system description object
2067 * @znode: znode to delete from
2068 * @n: zbranch slot number to delete
2069 *
2070 * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
2071 * case of success and a negative error code in case of failure.
2072 */
2073static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
2074{
2075 struct ubifs_zbranch *zbr;
2076 struct ubifs_znode *zp;
2077 int i, err;
2078
2079 /* Delete without merge for now */
2080 ubifs_assert(znode->level == 0);
2081 ubifs_assert(n >= 0 && n < c->fanout);
2082 dbg_tnc("deleting %s", DBGKEY(&znode->zbranch[n].key));
2083
2084 zbr = &znode->zbranch[n];
2085 lnc_free(zbr);
2086
2087 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2088 if (err) {
2089 dbg_dump_znode(c, znode);
2090 return err;
2091 }
2092
2093 /* We do not "gap" zbranch slots */
2094 for (i = n; i < znode->child_cnt - 1; i++)
2095 znode->zbranch[i] = znode->zbranch[i + 1];
2096 znode->child_cnt -= 1;
2097
2098 if (znode->child_cnt > 0)
2099 return 0;
2100
2101 /*
2102 * This was the last zbranch, we have to delete this znode from the
2103 * parent.
2104 */
2105
2106 do {
2107 ubifs_assert(!test_bit(OBSOLETE_ZNODE, &znode->flags));
2108 ubifs_assert(ubifs_zn_dirty(znode));
2109
2110 zp = znode->parent;
2111 n = znode->iip;
2112
2113 atomic_long_dec(&c->dirty_zn_cnt);
2114
2115 err = insert_old_idx_znode(c, znode);
2116 if (err)
2117 return err;
2118
2119 if (znode->cnext) {
2120 __set_bit(OBSOLETE_ZNODE, &znode->flags);
2121 atomic_long_inc(&c->clean_zn_cnt);
2122 atomic_long_inc(&ubifs_clean_zn_cnt);
2123 } else
2124 kfree(znode);
2125 znode = zp;
2126 } while (znode->child_cnt == 1); /* while removing last child */
2127
2128 /* Remove from znode, entry n - 1 */
2129 znode->child_cnt -= 1;
2130 ubifs_assert(znode->level != 0);
2131 for (i = n; i < znode->child_cnt; i++) {
2132 znode->zbranch[i] = znode->zbranch[i + 1];
2133 if (znode->zbranch[i].znode)
2134 znode->zbranch[i].znode->iip = i;
2135 }
2136
2137 /*
2138 * If this is the root and it has only 1 child then
2139 * collapse the tree.
2140 */
2141 if (!znode->parent) {
2142 while (znode->child_cnt == 1 && znode->level != 0) {
2143 zp = znode;
2144 zbr = &znode->zbranch[0];
2145 znode = get_znode(c, znode, 0);
2146 if (IS_ERR(znode))
2147 return PTR_ERR(znode);
2148 znode = dirty_cow_znode(c, zbr);
2149 if (IS_ERR(znode))
2150 return PTR_ERR(znode);
2151 znode->parent = NULL;
2152 znode->iip = 0;
2153 if (c->zroot.len) {
2154 err = insert_old_idx(c, c->zroot.lnum,
2155 c->zroot.offs);
2156 if (err)
2157 return err;
2158 }
2159 c->zroot.lnum = zbr->lnum;
2160 c->zroot.offs = zbr->offs;
2161 c->zroot.len = zbr->len;
2162 c->zroot.znode = znode;
2163 ubifs_assert(!test_bit(OBSOLETE_ZNODE,
2164 &zp->flags));
2165 ubifs_assert(test_bit(DIRTY_ZNODE, &zp->flags));
2166 atomic_long_dec(&c->dirty_zn_cnt);
2167
2168 if (zp->cnext) {
2169 __set_bit(OBSOLETE_ZNODE, &zp->flags);
2170 atomic_long_inc(&c->clean_zn_cnt);
2171 atomic_long_inc(&ubifs_clean_zn_cnt);
2172 } else
2173 kfree(zp);
2174 }
2175 }
2176
2177 return 0;
2178}
2179
2180/**
2181 * ubifs_tnc_remove - remove an index entry of a node.
2182 * @c: UBIFS file-system description object
2183 * @key: key of node
2184 *
2185 * Returns %0 on success or negative error code on failure.
2186 */
2187int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
2188{
2189 int found, n, err = 0;
2190 struct ubifs_znode *znode;
2191
2192 mutex_lock(&c->tnc_mutex);
2193 dbg_tnc("key %s", DBGKEY(key));
2194 found = lookup_level0_dirty(c, key, &znode, &n);
2195 if (found < 0) {
2196 err = found;
2197 goto out_unlock;
2198 }
2199 if (found == 1)
2200 err = tnc_delete(c, znode, n);
2201 if (!err)
2202 err = dbg_check_tnc(c, 0);
2203
2204out_unlock:
2205 mutex_unlock(&c->tnc_mutex);
2206 return err;
2207}
2208
2209/**
2210 * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2211 * @c: UBIFS file-system description object
2212 * @key: key of node
2213 * @nm: directory entry name
2214 *
2215 * Returns %0 on success or negative error code on failure.
2216 */
2217int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
2218 const struct qstr *nm)
2219{
2220 int n, err;
2221 struct ubifs_znode *znode;
2222
2223 mutex_lock(&c->tnc_mutex);
2224 dbg_tnc("%.*s, key %s", nm->len, nm->name, DBGKEY(key));
2225 err = lookup_level0_dirty(c, key, &znode, &n);
2226 if (err < 0)
2227 goto out_unlock;
2228
2229 if (err) {
2230 if (c->replaying)
2231 err = fallible_resolve_collision(c, key, &znode, &n,
2232 nm, 0);
2233 else
2234 err = resolve_collision(c, key, &znode, &n, nm);
2235 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
2236 if (err < 0)
2237 goto out_unlock;
2238 if (err) {
2239 /* Ensure the znode is dirtied */
2240 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2241 znode = dirty_cow_bottom_up(c, znode);
2242 if (IS_ERR(znode)) {
2243 err = PTR_ERR(znode);
2244 goto out_unlock;
2245 }
2246 }
2247 err = tnc_delete(c, znode, n);
2248 }
2249 }
2250
2251out_unlock:
2252 if (!err)
2253 err = dbg_check_tnc(c, 0);
2254 mutex_unlock(&c->tnc_mutex);
2255 return err;
2256}
2257
2258/**
2259 * key_in_range - determine if a key falls within a range of keys.
2260 * @c: UBIFS file-system description object
2261 * @key: key to check
2262 * @from_key: lowest key in range
2263 * @to_key: highest key in range
2264 *
2265 * This function returns %1 if the key is in range and %0 otherwise.
2266 */
2267static int key_in_range(struct ubifs_info *c, union ubifs_key *key,
2268 union ubifs_key *from_key, union ubifs_key *to_key)
2269{
2270 if (keys_cmp(c, key, from_key) < 0)
2271 return 0;
2272 if (keys_cmp(c, key, to_key) > 0)
2273 return 0;
2274 return 1;
2275}
2276
2277/**
2278 * ubifs_tnc_remove_range - remove index entries in range.
2279 * @c: UBIFS file-system description object
2280 * @from_key: lowest key to remove
2281 * @to_key: highest key to remove
2282 *
2283 * This function removes index entries starting at @from_key and ending at
2284 * @to_key. This function returns zero in case of success and a negative error
2285 * code in case of failure.
2286 */
2287int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
2288 union ubifs_key *to_key)
2289{
2290 int i, n, k, err = 0;
2291 struct ubifs_znode *znode;
2292 union ubifs_key *key;
2293
2294 mutex_lock(&c->tnc_mutex);
2295 while (1) {
2296 /* Find first level 0 znode that contains keys to remove */
2297 err = ubifs_lookup_level0(c, from_key, &znode, &n);
2298 if (err < 0)
2299 goto out_unlock;
2300
2301 if (err)
2302 key = from_key;
2303 else {
2304 err = tnc_next(c, &znode, &n);
2305 if (err == -ENOENT) {
2306 err = 0;
2307 goto out_unlock;
2308 }
2309 if (err < 0)
2310 goto out_unlock;
2311 key = &znode->zbranch[n].key;
2312 if (!key_in_range(c, key, from_key, to_key)) {
2313 err = 0;
2314 goto out_unlock;
2315 }
2316 }
2317
2318 /* Ensure the znode is dirtied */
2319 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2320 znode = dirty_cow_bottom_up(c, znode);
2321 if (IS_ERR(znode)) {
2322 err = PTR_ERR(znode);
2323 goto out_unlock;
2324 }
2325 }
2326
2327 /* Remove all keys in range except the first */
2328 for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) {
2329 key = &znode->zbranch[i].key;
2330 if (!key_in_range(c, key, from_key, to_key))
2331 break;
2332 lnc_free(&znode->zbranch[i]);
2333 err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
2334 znode->zbranch[i].len);
2335 if (err) {
2336 dbg_dump_znode(c, znode);
2337 goto out_unlock;
2338 }
2339 dbg_tnc("removing %s", DBGKEY(key));
2340 }
2341 if (k) {
2342 for (i = n + 1 + k; i < znode->child_cnt; i++)
2343 znode->zbranch[i - k] = znode->zbranch[i];
2344 znode->child_cnt -= k;
2345 }
2346
2347 /* Now delete the first */
2348 err = tnc_delete(c, znode, n);
2349 if (err)
2350 goto out_unlock;
2351 }
2352
2353out_unlock:
2354 if (!err)
2355 err = dbg_check_tnc(c, 0);
2356 mutex_unlock(&c->tnc_mutex);
2357 return err;
2358}
2359
2360/**
2361 * ubifs_tnc_remove_ino - remove an inode from TNC.
2362 * @c: UBIFS file-system description object
2363 * @inum: inode number to remove
2364 *
2365 * This function remove inode @inum and all the extended attributes associated
2366 * with the anode from TNC and returns zero in case of success or a negative
2367 * error code in case of failure.
2368 */
2369int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
2370{
2371 union ubifs_key key1, key2;
2372 struct ubifs_dent_node *xent, *pxent = NULL;
2373 struct qstr nm = { .name = NULL };
2374
2375 dbg_tnc("ino %lu", inum);
2376
2377 /*
2378 * Walk all extended attribute entries and remove them together with
2379 * corresponding extended attribute inodes.
2380 */
2381 lowest_xent_key(c, &key1, inum);
2382 while (1) {
2383 ino_t xattr_inum;
2384 int err;
2385
2386 xent = ubifs_tnc_next_ent(c, &key1, &nm);
2387 if (IS_ERR(xent)) {
2388 err = PTR_ERR(xent);
2389 if (err == -ENOENT)
2390 break;
2391 return err;
2392 }
2393
2394 xattr_inum = le64_to_cpu(xent->inum);
2395 dbg_tnc("xent '%s', ino %lu", xent->name, xattr_inum);
2396
2397 nm.name = xent->name;
2398 nm.len = le16_to_cpu(xent->nlen);
2399 err = ubifs_tnc_remove_nm(c, &key1, &nm);
2400 if (err) {
2401 kfree(xent);
2402 return err;
2403 }
2404
2405 lowest_ino_key(c, &key1, xattr_inum);
2406 highest_ino_key(c, &key2, xattr_inum);
2407 err = ubifs_tnc_remove_range(c, &key1, &key2);
2408 if (err) {
2409 kfree(xent);
2410 return err;
2411 }
2412
2413 kfree(pxent);
2414 pxent = xent;
2415 key_read(c, &xent->key, &key1);
2416 }
2417
2418 kfree(pxent);
2419 lowest_ino_key(c, &key1, inum);
2420 highest_ino_key(c, &key2, inum);
2421
2422 return ubifs_tnc_remove_range(c, &key1, &key2);
2423}
2424
2425/**
2426 * ubifs_tnc_next_ent - walk directory or extended attribute entries.
2427 * @c: UBIFS file-system description object
2428 * @key: key of last entry
2429 * @nm: name of last entry found or %NULL
2430 *
2431 * This function finds and reads the next directory or extended attribute entry
2432 * after the given key (@key) if there is one. @nm is used to resolve
2433 * collisions.
2434 *
2435 * If the name of the current entry is not known and only the key is known,
2436 * @nm->name has to be %NULL. In this case the semantics of this function is a
2437 * little bit different and it returns the entry corresponding to this key, not
2438 * the next one. If the key was not found, the closest "right" entry is
2439 * returned.
2440 *
2441 * If the fist entry has to be found, @key has to contain the lowest possible
2442 * key value for this inode and @name has to be %NULL.
2443 *
2444 * This function returns the found directory or extended attribute entry node
2445 * in case of success, %-ENOENT is returned if no entry was found, and a
2446 * negative error code is returned in case of failure.
2447 */
2448struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
2449 union ubifs_key *key,
2450 const struct qstr *nm)
2451{
2452 int n, err, type = key_type(c, key);
2453 struct ubifs_znode *znode;
2454 struct ubifs_dent_node *dent;
2455 struct ubifs_zbranch *zbr;
2456 union ubifs_key *dkey;
2457
2458 dbg_tnc("%s %s", nm->name ? (char *)nm->name : "(lowest)", DBGKEY(key));
2459 ubifs_assert(is_hash_key(c, key));
2460
2461 mutex_lock(&c->tnc_mutex);
2462 err = ubifs_lookup_level0(c, key, &znode, &n);
2463 if (unlikely(err < 0))
2464 goto out_unlock;
2465
2466 if (nm->name) {
2467 if (err) {
2468 /* Handle collisions */
2469 err = resolve_collision(c, key, &znode, &n, nm);
2470 dbg_tnc("rc returned %d, znode %p, n %d",
2471 err, znode, n);
2472 if (unlikely(err < 0))
2473 goto out_unlock;
2474 }
2475
2476 /* Now find next entry */
2477 err = tnc_next(c, &znode, &n);
2478 if (unlikely(err))
2479 goto out_unlock;
2480 } else {
2481 /*
2482 * The full name of the entry was not given, in which case the
2483 * behavior of this function is a little different and it
2484 * returns current entry, not the next one.
2485 */
2486 if (!err) {
2487 /*
2488 * However, the given key does not exist in the TNC
2489 * tree and @znode/@n variables contain the closest
2490 * "preceding" element. Switch to the next one.
2491 */
2492 err = tnc_next(c, &znode, &n);
2493 if (err)
2494 goto out_unlock;
2495 }
2496 }
2497
2498 zbr = &znode->zbranch[n];
2499 dent = kmalloc(zbr->len, GFP_NOFS);
2500 if (unlikely(!dent)) {
2501 err = -ENOMEM;
2502 goto out_unlock;
2503 }
2504
2505 /*
2506 * The above 'tnc_next()' call could lead us to the next inode, check
2507 * this.
2508 */
2509 dkey = &zbr->key;
2510 if (key_inum(c, dkey) != key_inum(c, key) ||
2511 key_type(c, dkey) != type) {
2512 err = -ENOENT;
2513 goto out_free;
2514 }
2515
2516 err = tnc_read_node_nm(c, zbr, dent);
2517 if (unlikely(err))
2518 goto out_free;
2519
2520 mutex_unlock(&c->tnc_mutex);
2521 return dent;
2522
2523out_free:
2524 kfree(dent);
2525out_unlock:
2526 mutex_unlock(&c->tnc_mutex);
2527 return ERR_PTR(err);
2528}
2529
2530/**
2531 * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
2532 * @c: UBIFS file-system description object
2533 *
2534 * Destroy left-over obsolete znodes from a failed commit.
2535 */
2536static void tnc_destroy_cnext(struct ubifs_info *c)
2537{
2538 struct ubifs_znode *cnext;
2539
2540 if (!c->cnext)
2541 return;
2542 ubifs_assert(c->cmt_state == COMMIT_BROKEN);
2543 cnext = c->cnext;
2544 do {
2545 struct ubifs_znode *znode = cnext;
2546
2547 cnext = cnext->cnext;
2548 if (test_bit(OBSOLETE_ZNODE, &znode->flags))
2549 kfree(znode);
2550 } while (cnext && cnext != c->cnext);
2551}
2552
2553/**
2554 * ubifs_tnc_close - close TNC subsystem and free all related resources.
2555 * @c: UBIFS file-system description object
2556 */
2557void ubifs_tnc_close(struct ubifs_info *c)
2558{
2559 long clean_freed;
2560
2561 tnc_destroy_cnext(c);
2562 if (c->zroot.znode) {
2563 clean_freed = ubifs_destroy_tnc_subtree(c->zroot.znode);
2564 atomic_long_sub(clean_freed, &ubifs_clean_zn_cnt);
2565 }
2566 kfree(c->gap_lebs);
2567 kfree(c->ilebs);
2568 destroy_old_idx(c);
2569}
2570
2571/**
2572 * left_znode - get the znode to the left.
2573 * @c: UBIFS file-system description object
2574 * @znode: znode
2575 *
2576 * This function returns a pointer to the znode to the left of @znode or NULL if
2577 * there is not one. A negative error code is returned on failure.
2578 */
2579static struct ubifs_znode *left_znode(struct ubifs_info *c,
2580 struct ubifs_znode *znode)
2581{
2582 int level = znode->level;
2583
2584 while (1) {
2585 int n = znode->iip - 1;
2586
2587 /* Go up until we can go left */
2588 znode = znode->parent;
2589 if (!znode)
2590 return NULL;
2591 if (n >= 0) {
2592 /* Now go down the rightmost branch to 'level' */
2593 znode = get_znode(c, znode, n);
2594 if (IS_ERR(znode))
2595 return znode;
2596 while (znode->level != level) {
2597 n = znode->child_cnt - 1;
2598 znode = get_znode(c, znode, n);
2599 if (IS_ERR(znode))
2600 return znode;
2601 }
2602 break;
2603 }
2604 }
2605 return znode;
2606}
2607
2608/**
2609 * right_znode - get the znode to the right.
2610 * @c: UBIFS file-system description object
2611 * @znode: znode
2612 *
2613 * This function returns a pointer to the znode to the right of @znode or NULL
2614 * if there is not one. A negative error code is returned on failure.
2615 */
2616static struct ubifs_znode *right_znode(struct ubifs_info *c,
2617 struct ubifs_znode *znode)
2618{
2619 int level = znode->level;
2620
2621 while (1) {
2622 int n = znode->iip + 1;
2623
2624 /* Go up until we can go right */
2625 znode = znode->parent;
2626 if (!znode)
2627 return NULL;
2628 if (n < znode->child_cnt) {
2629 /* Now go down the leftmost branch to 'level' */
2630 znode = get_znode(c, znode, n);
2631 if (IS_ERR(znode))
2632 return znode;
2633 while (znode->level != level) {
2634 znode = get_znode(c, znode, 0);
2635 if (IS_ERR(znode))
2636 return znode;
2637 }
2638 break;
2639 }
2640 }
2641 return znode;
2642}
2643
2644/**
2645 * lookup_znode - find a particular indexing node from TNC.
2646 * @c: UBIFS file-system description object
2647 * @key: index node key to lookup
2648 * @level: index node level
2649 * @lnum: index node LEB number
2650 * @offs: index node offset
2651 *
2652 * This function searches an indexing node by its first key @key and its
2653 * address @lnum:@offs. It looks up the indexing tree by pulling all indexing
2654 * nodes it traverses to TNC. This function is called fro indexing nodes which
2655 * were found on the media by scanning, for example when garbage-collecting or
2656 * when doing in-the-gaps commit. This means that the indexing node which is
2657 * looked for does not have to have exactly the same leftmost key @key, because
2658 * the leftmost key may have been changed, in which case TNC will contain a
2659 * dirty znode which still refers the same @lnum:@offs. This function is clever
2660 * enough to recognize such indexing nodes.
2661 *
2662 * Note, if a znode was deleted or changed too much, then this function will
2663 * not find it. For situations like this UBIFS has the old index RB-tree
2664 * (indexed by @lnum:@offs).
2665 *
2666 * This function returns a pointer to the znode found or %NULL if it is not
2667 * found. A negative error code is returned on failure.
2668 */
2669static struct ubifs_znode *lookup_znode(struct ubifs_info *c,
2670 union ubifs_key *key, int level,
2671 int lnum, int offs)
2672{
2673 struct ubifs_znode *znode, *zn;
2674 int n, nn;
2675
2676 /*
2677 * The arguments have probably been read off flash, so don't assume
2678 * they are valid.
2679 */
2680 if (level < 0)
2681 return ERR_PTR(-EINVAL);
2682
2683 /* Get the root znode */
2684 znode = c->zroot.znode;
2685 if (!znode) {
2686 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
2687 if (IS_ERR(znode))
2688 return znode;
2689 }
2690 /* Check if it is the one we are looking for */
2691 if (c->zroot.lnum == lnum && c->zroot.offs == offs)
2692 return znode;
2693 /* Descend to the parent level i.e. (level + 1) */
2694 if (level >= znode->level)
2695 return NULL;
2696 while (1) {
2697 ubifs_search_zbranch(c, znode, key, &n);
2698 if (n < 0) {
2699 /*
2700 * We reached a znode where the leftmost key is greater
2701 * than the key we are searching for. This is the same
2702 * situation as the one described in a huge comment at
2703 * the end of the 'ubifs_lookup_level0()' function. And
2704 * for exactly the same reasons we have to try to look
2705 * left before giving up.
2706 */
2707 znode = left_znode(c, znode);
2708 if (!znode)
2709 return NULL;
2710 if (IS_ERR(znode))
2711 return znode;
2712 ubifs_search_zbranch(c, znode, key, &n);
2713 ubifs_assert(n >= 0);
2714 }
2715 if (znode->level == level + 1)
2716 break;
2717 znode = get_znode(c, znode, n);
2718 if (IS_ERR(znode))
2719 return znode;
2720 }
2721 /* Check if the child is the one we are looking for */
2722 if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs)
2723 return get_znode(c, znode, n);
2724 /* If the key is unique, there is nowhere else to look */
2725 if (!is_hash_key(c, key))
2726 return NULL;
2727 /*
2728 * The key is not unique and so may be also in the znodes to either
2729 * side.
2730 */
2731 zn = znode;
2732 nn = n;
2733 /* Look left */
2734 while (1) {
2735 /* Move one branch to the left */
2736 if (n)
2737 n -= 1;
2738 else {
2739 znode = left_znode(c, znode);
2740 if (!znode)
2741 break;
2742 if (IS_ERR(znode))
2743 return znode;
2744 n = znode->child_cnt - 1;
2745 }
2746 /* Check it */
2747 if (znode->zbranch[n].lnum == lnum &&
2748 znode->zbranch[n].offs == offs)
2749 return get_znode(c, znode, n);
2750 /* Stop if the key is less than the one we are looking for */
2751 if (keys_cmp(c, &znode->zbranch[n].key, key) < 0)
2752 break;
2753 }
2754 /* Back to the middle */
2755 znode = zn;
2756 n = nn;
2757 /* Look right */
2758 while (1) {
2759 /* Move one branch to the right */
2760 if (++n >= znode->child_cnt) {
2761 znode = right_znode(c, znode);
2762 if (!znode)
2763 break;
2764 if (IS_ERR(znode))
2765 return znode;
2766 n = 0;
2767 }
2768 /* Check it */
2769 if (znode->zbranch[n].lnum == lnum &&
2770 znode->zbranch[n].offs == offs)
2771 return get_znode(c, znode, n);
2772 /* Stop if the key is greater than the one we are looking for */
2773 if (keys_cmp(c, &znode->zbranch[n].key, key) > 0)
2774 break;
2775 }
2776 return NULL;
2777}
2778
2779/**
2780 * is_idx_node_in_tnc - determine if an index node is in the TNC.
2781 * @c: UBIFS file-system description object
2782 * @key: key of index node
2783 * @level: index node level
2784 * @lnum: LEB number of index node
2785 * @offs: offset of index node
2786 *
2787 * This function returns %0 if the index node is not referred to in the TNC, %1
2788 * if the index node is referred to in the TNC and the corresponding znode is
2789 * dirty, %2 if an index node is referred to in the TNC and the corresponding
2790 * znode is clean, and a negative error code in case of failure.
2791 *
2792 * Note, the @key argument has to be the key of the first child. Also note,
2793 * this function relies on the fact that 0:0 is never a valid LEB number and
2794 * offset for a main-area node.
2795 */
2796int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
2797 int lnum, int offs)
2798{
2799 struct ubifs_znode *znode;
2800
2801 znode = lookup_znode(c, key, level, lnum, offs);
2802 if (!znode)
2803 return 0;
2804 if (IS_ERR(znode))
2805 return PTR_ERR(znode);
2806
2807 return ubifs_zn_dirty(znode) ? 1 : 2;
2808}
2809
2810/**
2811 * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
2812 * @c: UBIFS file-system description object
2813 * @key: node key
2814 * @lnum: node LEB number
2815 * @offs: node offset
2816 *
2817 * This function returns %1 if the node is referred to in the TNC, %0 if it is
2818 * not, and a negative error code in case of failure.
2819 *
2820 * Note, this function relies on the fact that 0:0 is never a valid LEB number
2821 * and offset for a main-area node.
2822 */
2823static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key,
2824 int lnum, int offs)
2825{
2826 struct ubifs_zbranch *zbr;
2827 struct ubifs_znode *znode, *zn;
2828 int n, found, err, nn;
2829 const int unique = !is_hash_key(c, key);
2830
2831 found = ubifs_lookup_level0(c, key, &znode, &n);
2832 if (found < 0)
2833 return found; /* Error code */
2834 if (!found)
2835 return 0;
2836 zbr = &znode->zbranch[n];
2837 if (lnum == zbr->lnum && offs == zbr->offs)
2838 return 1; /* Found it */
2839 if (unique)
2840 return 0;
2841 /*
2842 * Because the key is not unique, we have to look left
2843 * and right as well
2844 */
2845 zn = znode;
2846 nn = n;
2847 /* Look left */
2848 while (1) {
2849 err = tnc_prev(c, &znode, &n);
2850 if (err == -ENOENT)
2851 break;
2852 if (err)
2853 return err;
2854 if (keys_cmp(c, key, &znode->zbranch[n].key))
2855 break;
2856 zbr = &znode->zbranch[n];
2857 if (lnum == zbr->lnum && offs == zbr->offs)
2858 return 1; /* Found it */
2859 }
2860 /* Look right */
2861 znode = zn;
2862 n = nn;
2863 while (1) {
2864 err = tnc_next(c, &znode, &n);
2865 if (err) {
2866 if (err == -ENOENT)
2867 return 0;
2868 return err;
2869 }
2870 if (keys_cmp(c, key, &znode->zbranch[n].key))
2871 break;
2872 zbr = &znode->zbranch[n];
2873 if (lnum == zbr->lnum && offs == zbr->offs)
2874 return 1; /* Found it */
2875 }
2876 return 0;
2877}
2878
2879/**
2880 * ubifs_tnc_has_node - determine whether a node is in the TNC.
2881 * @c: UBIFS file-system description object
2882 * @key: node key
2883 * @level: index node level (if it is an index node)
2884 * @lnum: node LEB number
2885 * @offs: node offset
2886 * @is_idx: non-zero if the node is an index node
2887 *
2888 * This function returns %1 if the node is in the TNC, %0 if it is not, and a
2889 * negative error code in case of failure. For index nodes, @key has to be the
2890 * key of the first child. An index node is considered to be in the TNC only if
2891 * the corresponding znode is clean or has not been loaded.
2892 */
2893int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
2894 int lnum, int offs, int is_idx)
2895{
2896 int err;
2897
2898 mutex_lock(&c->tnc_mutex);
2899 if (is_idx) {
2900 err = is_idx_node_in_tnc(c, key, level, lnum, offs);
2901 if (err < 0)
2902 goto out_unlock;
2903 if (err == 1)
2904 /* The index node was found but it was dirty */
2905 err = 0;
2906 else if (err == 2)
2907 /* The index node was found and it was clean */
2908 err = 1;
2909 else
2910 BUG_ON(err != 0);
2911 } else
2912 err = is_leaf_node_in_tnc(c, key, lnum, offs);
2913
2914out_unlock:
2915 mutex_unlock(&c->tnc_mutex);
2916 return err;
2917}
2918
2919/**
2920 * ubifs_dirty_idx_node - dirty an index node.
2921 * @c: UBIFS file-system description object
2922 * @key: index node key
2923 * @level: index node level
2924 * @lnum: index node LEB number
2925 * @offs: index node offset
2926 *
2927 * This function loads and dirties an index node so that it can be garbage
2928 * collected. The @key argument has to be the key of the first child. This
2929 * function relies on the fact that 0:0 is never a valid LEB number and offset
2930 * for a main-area node. Returns %0 on success and a negative error code on
2931 * failure.
2932 */
2933int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
2934 int lnum, int offs)
2935{
2936 struct ubifs_znode *znode;
2937 int err = 0;
2938
2939 mutex_lock(&c->tnc_mutex);
2940 znode = lookup_znode(c, key, level, lnum, offs);
2941 if (!znode)
2942 goto out_unlock;
2943 if (IS_ERR(znode)) {
2944 err = PTR_ERR(znode);
2945 goto out_unlock;
2946 }
2947 znode = dirty_cow_bottom_up(c, znode);
2948 if (IS_ERR(znode)) {
2949 err = PTR_ERR(znode);
2950 goto out_unlock;
2951 }
2952
2953out_unlock:
2954 mutex_unlock(&c->tnc_mutex);
2955 return err;
2956}
diff --git a/fs/ubifs/tnc_commit.c b/fs/ubifs/tnc_commit.c
new file mode 100644
index 000000000000..8117e65ba2e9
--- /dev/null
+++ b/fs/ubifs/tnc_commit.c
@@ -0,0 +1,1103 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/* This file implements TNC functions for committing */
24
25#include "ubifs.h"
26
27/**
28 * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
29 * @c: UBIFS file-system description object
30 * @idx: buffer in which to place new index node
31 * @znode: znode from which to make new index node
32 * @lnum: LEB number where new index node will be written
33 * @offs: offset where new index node will be written
34 * @len: length of new index node
35 */
36static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
37 struct ubifs_znode *znode, int lnum, int offs, int len)
38{
39 struct ubifs_znode *zp;
40 int i, err;
41
42 /* Make index node */
43 idx->ch.node_type = UBIFS_IDX_NODE;
44 idx->child_cnt = cpu_to_le16(znode->child_cnt);
45 idx->level = cpu_to_le16(znode->level);
46 for (i = 0; i < znode->child_cnt; i++) {
47 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
48 struct ubifs_zbranch *zbr = &znode->zbranch[i];
49
50 key_write_idx(c, &zbr->key, &br->key);
51 br->lnum = cpu_to_le32(zbr->lnum);
52 br->offs = cpu_to_le32(zbr->offs);
53 br->len = cpu_to_le32(zbr->len);
54 if (!zbr->lnum || !zbr->len) {
55 ubifs_err("bad ref in znode");
56 dbg_dump_znode(c, znode);
57 if (zbr->znode)
58 dbg_dump_znode(c, zbr->znode);
59 }
60 }
61 ubifs_prepare_node(c, idx, len, 0);
62
63#ifdef CONFIG_UBIFS_FS_DEBUG
64 znode->lnum = lnum;
65 znode->offs = offs;
66 znode->len = len;
67#endif
68
69 err = insert_old_idx_znode(c, znode);
70
71 /* Update the parent */
72 zp = znode->parent;
73 if (zp) {
74 struct ubifs_zbranch *zbr;
75
76 zbr = &zp->zbranch[znode->iip];
77 zbr->lnum = lnum;
78 zbr->offs = offs;
79 zbr->len = len;
80 } else {
81 c->zroot.lnum = lnum;
82 c->zroot.offs = offs;
83 c->zroot.len = len;
84 }
85 c->calc_idx_sz += ALIGN(len, 8);
86
87 atomic_long_dec(&c->dirty_zn_cnt);
88
89 ubifs_assert(ubifs_zn_dirty(znode));
90 ubifs_assert(test_bit(COW_ZNODE, &znode->flags));
91
92 __clear_bit(DIRTY_ZNODE, &znode->flags);
93 __clear_bit(COW_ZNODE, &znode->flags);
94
95 return err;
96}
97
98/**
99 * fill_gap - make index nodes in gaps in dirty index LEBs.
100 * @c: UBIFS file-system description object
101 * @lnum: LEB number that gap appears in
102 * @gap_start: offset of start of gap
103 * @gap_end: offset of end of gap
104 * @dirt: adds dirty space to this
105 *
106 * This function returns the number of index nodes written into the gap.
107 */
108static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
109 int *dirt)
110{
111 int len, gap_remains, gap_pos, written, pad_len;
112
113 ubifs_assert((gap_start & 7) == 0);
114 ubifs_assert((gap_end & 7) == 0);
115 ubifs_assert(gap_end >= gap_start);
116
117 gap_remains = gap_end - gap_start;
118 if (!gap_remains)
119 return 0;
120 gap_pos = gap_start;
121 written = 0;
122 while (c->enext) {
123 len = ubifs_idx_node_sz(c, c->enext->child_cnt);
124 if (len < gap_remains) {
125 struct ubifs_znode *znode = c->enext;
126 const int alen = ALIGN(len, 8);
127 int err;
128
129 ubifs_assert(alen <= gap_remains);
130 err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
131 lnum, gap_pos, len);
132 if (err)
133 return err;
134 gap_remains -= alen;
135 gap_pos += alen;
136 c->enext = znode->cnext;
137 if (c->enext == c->cnext)
138 c->enext = NULL;
139 written += 1;
140 } else
141 break;
142 }
143 if (gap_end == c->leb_size) {
144 c->ileb_len = ALIGN(gap_pos, c->min_io_size);
145 /* Pad to end of min_io_size */
146 pad_len = c->ileb_len - gap_pos;
147 } else
148 /* Pad to end of gap */
149 pad_len = gap_remains;
150 dbg_gc("LEB %d:%d to %d len %d nodes written %d wasted bytes %d",
151 lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len);
152 ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
153 *dirt += pad_len;
154 return written;
155}
156
157/**
158 * find_old_idx - find an index node obsoleted since the last commit start.
159 * @c: UBIFS file-system description object
160 * @lnum: LEB number of obsoleted index node
161 * @offs: offset of obsoleted index node
162 *
163 * Returns %1 if found and %0 otherwise.
164 */
165static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
166{
167 struct ubifs_old_idx *o;
168 struct rb_node *p;
169
170 p = c->old_idx.rb_node;
171 while (p) {
172 o = rb_entry(p, struct ubifs_old_idx, rb);
173 if (lnum < o->lnum)
174 p = p->rb_left;
175 else if (lnum > o->lnum)
176 p = p->rb_right;
177 else if (offs < o->offs)
178 p = p->rb_left;
179 else if (offs > o->offs)
180 p = p->rb_right;
181 else
182 return 1;
183 }
184 return 0;
185}
186
187/**
188 * is_idx_node_in_use - determine if an index node can be overwritten.
189 * @c: UBIFS file-system description object
190 * @key: key of index node
191 * @level: index node level
192 * @lnum: LEB number of index node
193 * @offs: offset of index node
194 *
195 * If @key / @lnum / @offs identify an index node that was not part of the old
196 * index, then this function returns %0 (obsolete). Else if the index node was
197 * part of the old index but is now dirty %1 is returned, else if it is clean %2
198 * is returned. A negative error code is returned on failure.
199 */
200static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
201 int level, int lnum, int offs)
202{
203 int ret;
204
205 ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
206 if (ret < 0)
207 return ret; /* Error code */
208 if (ret == 0)
209 if (find_old_idx(c, lnum, offs))
210 return 1;
211 return ret;
212}
213
214/**
215 * layout_leb_in_gaps - layout index nodes using in-the-gaps method.
216 * @c: UBIFS file-system description object
217 * @p: return LEB number here
218 *
219 * This function lays out new index nodes for dirty znodes using in-the-gaps
220 * method of TNC commit.
221 * This function merely puts the next znode into the next gap, making no attempt
222 * to try to maximise the number of znodes that fit.
223 * This function returns the number of index nodes written into the gaps, or a
224 * negative error code on failure.
225 */
226static int layout_leb_in_gaps(struct ubifs_info *c, int *p)
227{
228 struct ubifs_scan_leb *sleb;
229 struct ubifs_scan_node *snod;
230 int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
231
232 tot_written = 0;
233 /* Get an index LEB with lots of obsolete index nodes */
234 lnum = ubifs_find_dirty_idx_leb(c);
235 if (lnum < 0)
236 /*
237 * There also may be dirt in the index head that could be
238 * filled, however we do not check there at present.
239 */
240 return lnum; /* Error code */
241 *p = lnum;
242 dbg_gc("LEB %d", lnum);
243 /*
244 * Scan the index LEB. We use the generic scan for this even though
245 * it is more comprehensive and less efficient than is needed for this
246 * purpose.
247 */
248 sleb = ubifs_scan(c, lnum, 0, c->ileb_buf);
249 c->ileb_len = 0;
250 if (IS_ERR(sleb))
251 return PTR_ERR(sleb);
252 gap_start = 0;
253 list_for_each_entry(snod, &sleb->nodes, list) {
254 struct ubifs_idx_node *idx;
255 int in_use, level;
256
257 ubifs_assert(snod->type == UBIFS_IDX_NODE);
258 idx = snod->node;
259 key_read(c, ubifs_idx_key(c, idx), &snod->key);
260 level = le16_to_cpu(idx->level);
261 /* Determine if the index node is in use (not obsolete) */
262 in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
263 snod->offs);
264 if (in_use < 0) {
265 ubifs_scan_destroy(sleb);
266 return in_use; /* Error code */
267 }
268 if (in_use) {
269 if (in_use == 1)
270 dirt += ALIGN(snod->len, 8);
271 /*
272 * The obsolete index nodes form gaps that can be
273 * overwritten. This gap has ended because we have
274 * found an index node that is still in use
275 * i.e. not obsolete
276 */
277 gap_end = snod->offs;
278 /* Try to fill gap */
279 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
280 if (written < 0) {
281 ubifs_scan_destroy(sleb);
282 return written; /* Error code */
283 }
284 tot_written += written;
285 gap_start = ALIGN(snod->offs + snod->len, 8);
286 }
287 }
288 ubifs_scan_destroy(sleb);
289 c->ileb_len = c->leb_size;
290 gap_end = c->leb_size;
291 /* Try to fill gap */
292 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
293 if (written < 0)
294 return written; /* Error code */
295 tot_written += written;
296 if (tot_written == 0) {
297 struct ubifs_lprops lp;
298
299 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
300 err = ubifs_read_one_lp(c, lnum, &lp);
301 if (err)
302 return err;
303 if (lp.free == c->leb_size) {
304 /*
305 * We must have snatched this LEB from the idx_gc list
306 * so we need to correct the free and dirty space.
307 */
308 err = ubifs_change_one_lp(c, lnum,
309 c->leb_size - c->ileb_len,
310 dirt, 0, 0, 0);
311 if (err)
312 return err;
313 }
314 return 0;
315 }
316 err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
317 0, 0, 0);
318 if (err)
319 return err;
320 err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len,
321 UBI_SHORTTERM);
322 if (err)
323 return err;
324 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
325 return tot_written;
326}
327
328/**
329 * get_leb_cnt - calculate the number of empty LEBs needed to commit.
330 * @c: UBIFS file-system description object
331 * @cnt: number of znodes to commit
332 *
333 * This function returns the number of empty LEBs needed to commit @cnt znodes
334 * to the current index head. The number is not exact and may be more than
335 * needed.
336 */
337static int get_leb_cnt(struct ubifs_info *c, int cnt)
338{
339 int d;
340
341 /* Assume maximum index node size (i.e. overestimate space needed) */
342 cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
343 if (cnt < 0)
344 cnt = 0;
345 d = c->leb_size / c->max_idx_node_sz;
346 return DIV_ROUND_UP(cnt, d);
347}
348
349/**
350 * layout_in_gaps - in-the-gaps method of committing TNC.
351 * @c: UBIFS file-system description object
352 * @cnt: number of dirty znodes to commit.
353 *
354 * This function lays out new index nodes for dirty znodes using in-the-gaps
355 * method of TNC commit.
356 *
357 * This function returns %0 on success and a negative error code on failure.
358 */
359static int layout_in_gaps(struct ubifs_info *c, int cnt)
360{
361 int err, leb_needed_cnt, written, *p;
362
363 dbg_gc("%d znodes to write", cnt);
364
365 c->gap_lebs = kmalloc(sizeof(int) * (c->lst.idx_lebs + 1), GFP_NOFS);
366 if (!c->gap_lebs)
367 return -ENOMEM;
368
369 p = c->gap_lebs;
370 do {
371 ubifs_assert(p < c->gap_lebs + sizeof(int) * c->lst.idx_lebs);
372 written = layout_leb_in_gaps(c, p);
373 if (written < 0) {
374 err = written;
375 if (err == -ENOSPC) {
376 if (!dbg_force_in_the_gaps_enabled) {
377 /*
378 * Do not print scary warnings if the
379 * debugging option which forces
380 * in-the-gaps is enabled.
381 */
382 ubifs_err("out of space");
383 spin_lock(&c->space_lock);
384 dbg_dump_budg(c);
385 spin_unlock(&c->space_lock);
386 dbg_dump_lprops(c);
387 }
388 /* Try to commit anyway */
389 err = 0;
390 break;
391 }
392 kfree(c->gap_lebs);
393 c->gap_lebs = NULL;
394 return err;
395 }
396 p++;
397 cnt -= written;
398 leb_needed_cnt = get_leb_cnt(c, cnt);
399 dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
400 leb_needed_cnt, c->ileb_cnt);
401 } while (leb_needed_cnt > c->ileb_cnt);
402
403 *p = -1;
404 return 0;
405}
406
407/**
408 * layout_in_empty_space - layout index nodes in empty space.
409 * @c: UBIFS file-system description object
410 *
411 * This function lays out new index nodes for dirty znodes using empty LEBs.
412 *
413 * This function returns %0 on success and a negative error code on failure.
414 */
415static int layout_in_empty_space(struct ubifs_info *c)
416{
417 struct ubifs_znode *znode, *cnext, *zp;
418 int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
419 int wlen, blen, err;
420
421 cnext = c->enext;
422 if (!cnext)
423 return 0;
424
425 lnum = c->ihead_lnum;
426 buf_offs = c->ihead_offs;
427
428 buf_len = ubifs_idx_node_sz(c, c->fanout);
429 buf_len = ALIGN(buf_len, c->min_io_size);
430 used = 0;
431 avail = buf_len;
432
433 /* Ensure there is enough room for first write */
434 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
435 if (buf_offs + next_len > c->leb_size)
436 lnum = -1;
437
438 while (1) {
439 znode = cnext;
440
441 len = ubifs_idx_node_sz(c, znode->child_cnt);
442
443 /* Determine the index node position */
444 if (lnum == -1) {
445 if (c->ileb_nxt >= c->ileb_cnt) {
446 ubifs_err("out of space");
447 return -ENOSPC;
448 }
449 lnum = c->ilebs[c->ileb_nxt++];
450 buf_offs = 0;
451 used = 0;
452 avail = buf_len;
453 }
454
455 offs = buf_offs + used;
456
457#ifdef CONFIG_UBIFS_FS_DEBUG
458 znode->lnum = lnum;
459 znode->offs = offs;
460 znode->len = len;
461#endif
462
463 /* Update the parent */
464 zp = znode->parent;
465 if (zp) {
466 struct ubifs_zbranch *zbr;
467 int i;
468
469 i = znode->iip;
470 zbr = &zp->zbranch[i];
471 zbr->lnum = lnum;
472 zbr->offs = offs;
473 zbr->len = len;
474 } else {
475 c->zroot.lnum = lnum;
476 c->zroot.offs = offs;
477 c->zroot.len = len;
478 }
479 c->calc_idx_sz += ALIGN(len, 8);
480
481 /*
482 * Once lprops is updated, we can decrease the dirty znode count
483 * but it is easier to just do it here.
484 */
485 atomic_long_dec(&c->dirty_zn_cnt);
486
487 /*
488 * Calculate the next index node length to see if there is
489 * enough room for it
490 */
491 cnext = znode->cnext;
492 if (cnext == c->cnext)
493 next_len = 0;
494 else
495 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
496
497 if (c->min_io_size == 1) {
498 buf_offs += ALIGN(len, 8);
499 if (next_len) {
500 if (buf_offs + next_len <= c->leb_size)
501 continue;
502 err = ubifs_update_one_lp(c, lnum, 0,
503 c->leb_size - buf_offs, 0, 0);
504 if (err)
505 return err;
506 lnum = -1;
507 continue;
508 }
509 err = ubifs_update_one_lp(c, lnum,
510 c->leb_size - buf_offs, 0, 0, 0);
511 if (err)
512 return err;
513 break;
514 }
515
516 /* Update buffer positions */
517 wlen = used + len;
518 used += ALIGN(len, 8);
519 avail -= ALIGN(len, 8);
520
521 if (next_len != 0 &&
522 buf_offs + used + next_len <= c->leb_size &&
523 avail > 0)
524 continue;
525
526 if (avail <= 0 && next_len &&
527 buf_offs + used + next_len <= c->leb_size)
528 blen = buf_len;
529 else
530 blen = ALIGN(wlen, c->min_io_size);
531
532 /* The buffer is full or there are no more znodes to do */
533 buf_offs += blen;
534 if (next_len) {
535 if (buf_offs + next_len > c->leb_size) {
536 err = ubifs_update_one_lp(c, lnum,
537 c->leb_size - buf_offs, blen - used,
538 0, 0);
539 if (err)
540 return err;
541 lnum = -1;
542 }
543 used -= blen;
544 if (used < 0)
545 used = 0;
546 avail = buf_len - used;
547 continue;
548 }
549 err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
550 blen - used, 0, 0);
551 if (err)
552 return err;
553 break;
554 }
555
556#ifdef CONFIG_UBIFS_FS_DEBUG
557 c->new_ihead_lnum = lnum;
558 c->new_ihead_offs = buf_offs;
559#endif
560
561 return 0;
562}
563
564/**
565 * layout_commit - determine positions of index nodes to commit.
566 * @c: UBIFS file-system description object
567 * @no_space: indicates that insufficient empty LEBs were allocated
568 * @cnt: number of znodes to commit
569 *
570 * Calculate and update the positions of index nodes to commit. If there were
571 * an insufficient number of empty LEBs allocated, then index nodes are placed
572 * into the gaps created by obsolete index nodes in non-empty index LEBs. For
573 * this purpose, an obsolete index node is one that was not in the index as at
574 * the end of the last commit. To write "in-the-gaps" requires that those index
575 * LEBs are updated atomically in-place.
576 */
577static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
578{
579 int err;
580
581 if (no_space) {
582 err = layout_in_gaps(c, cnt);
583 if (err)
584 return err;
585 }
586 err = layout_in_empty_space(c);
587 return err;
588}
589
590/**
591 * find_first_dirty - find first dirty znode.
592 * @znode: znode to begin searching from
593 */
594static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
595{
596 int i, cont;
597
598 if (!znode)
599 return NULL;
600
601 while (1) {
602 if (znode->level == 0) {
603 if (ubifs_zn_dirty(znode))
604 return znode;
605 return NULL;
606 }
607 cont = 0;
608 for (i = 0; i < znode->child_cnt; i++) {
609 struct ubifs_zbranch *zbr = &znode->zbranch[i];
610
611 if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
612 znode = zbr->znode;
613 cont = 1;
614 break;
615 }
616 }
617 if (!cont) {
618 if (ubifs_zn_dirty(znode))
619 return znode;
620 return NULL;
621 }
622 }
623}
624
625/**
626 * find_next_dirty - find next dirty znode.
627 * @znode: znode to begin searching from
628 */
629static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
630{
631 int n = znode->iip + 1;
632
633 znode = znode->parent;
634 if (!znode)
635 return NULL;
636 for (; n < znode->child_cnt; n++) {
637 struct ubifs_zbranch *zbr = &znode->zbranch[n];
638
639 if (zbr->znode && ubifs_zn_dirty(zbr->znode))
640 return find_first_dirty(zbr->znode);
641 }
642 return znode;
643}
644
645/**
646 * get_znodes_to_commit - create list of dirty znodes to commit.
647 * @c: UBIFS file-system description object
648 *
649 * This function returns the number of znodes to commit.
650 */
651static int get_znodes_to_commit(struct ubifs_info *c)
652{
653 struct ubifs_znode *znode, *cnext;
654 int cnt = 0;
655
656 c->cnext = find_first_dirty(c->zroot.znode);
657 znode = c->enext = c->cnext;
658 if (!znode) {
659 dbg_cmt("no znodes to commit");
660 return 0;
661 }
662 cnt += 1;
663 while (1) {
664 ubifs_assert(!test_bit(COW_ZNODE, &znode->flags));
665 __set_bit(COW_ZNODE, &znode->flags);
666 znode->alt = 0;
667 cnext = find_next_dirty(znode);
668 if (!cnext) {
669 znode->cnext = c->cnext;
670 break;
671 }
672 znode->cnext = cnext;
673 znode = cnext;
674 cnt += 1;
675 }
676 dbg_cmt("committing %d znodes", cnt);
677 ubifs_assert(cnt == atomic_long_read(&c->dirty_zn_cnt));
678 return cnt;
679}
680
681/**
682 * alloc_idx_lebs - allocate empty LEBs to be used to commit.
683 * @c: UBIFS file-system description object
684 * @cnt: number of znodes to commit
685 *
686 * This function returns %-ENOSPC if it cannot allocate a sufficient number of
687 * empty LEBs. %0 is returned on success, otherwise a negative error code
688 * is returned.
689 */
690static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
691{
692 int i, leb_cnt, lnum;
693
694 c->ileb_cnt = 0;
695 c->ileb_nxt = 0;
696 leb_cnt = get_leb_cnt(c, cnt);
697 dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
698 if (!leb_cnt)
699 return 0;
700 c->ilebs = kmalloc(leb_cnt * sizeof(int), GFP_NOFS);
701 if (!c->ilebs)
702 return -ENOMEM;
703 for (i = 0; i < leb_cnt; i++) {
704 lnum = ubifs_find_free_leb_for_idx(c);
705 if (lnum < 0)
706 return lnum;
707 c->ilebs[c->ileb_cnt++] = lnum;
708 dbg_cmt("LEB %d", lnum);
709 }
710 if (dbg_force_in_the_gaps())
711 return -ENOSPC;
712 return 0;
713}
714
715/**
716 * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
717 * @c: UBIFS file-system description object
718 *
719 * It is possible that we allocate more empty LEBs for the commit than we need.
720 * This functions frees the surplus.
721 *
722 * This function returns %0 on success and a negative error code on failure.
723 */
724static int free_unused_idx_lebs(struct ubifs_info *c)
725{
726 int i, err = 0, lnum, er;
727
728 for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
729 lnum = c->ilebs[i];
730 dbg_cmt("LEB %d", lnum);
731 er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
732 LPROPS_INDEX | LPROPS_TAKEN, 0);
733 if (!err)
734 err = er;
735 }
736 return err;
737}
738
739/**
740 * free_idx_lebs - free unused LEBs after commit end.
741 * @c: UBIFS file-system description object
742 *
743 * This function returns %0 on success and a negative error code on failure.
744 */
745static int free_idx_lebs(struct ubifs_info *c)
746{
747 int err;
748
749 err = free_unused_idx_lebs(c);
750 kfree(c->ilebs);
751 c->ilebs = NULL;
752 return err;
753}
754
755/**
756 * ubifs_tnc_start_commit - start TNC commit.
757 * @c: UBIFS file-system description object
758 * @zroot: new index root position is returned here
759 *
760 * This function prepares the list of indexing nodes to commit and lays out
761 * their positions on flash. If there is not enough free space it uses the
762 * in-gap commit method. Returns zero in case of success and a negative error
763 * code in case of failure.
764 */
765int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
766{
767 int err = 0, cnt;
768
769 mutex_lock(&c->tnc_mutex);
770 err = dbg_check_tnc(c, 1);
771 if (err)
772 goto out;
773 cnt = get_znodes_to_commit(c);
774 if (cnt != 0) {
775 int no_space = 0;
776
777 err = alloc_idx_lebs(c, cnt);
778 if (err == -ENOSPC)
779 no_space = 1;
780 else if (err)
781 goto out_free;
782 err = layout_commit(c, no_space, cnt);
783 if (err)
784 goto out_free;
785 ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
786 err = free_unused_idx_lebs(c);
787 if (err)
788 goto out;
789 }
790 destroy_old_idx(c);
791 memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
792
793 err = ubifs_save_dirty_idx_lnums(c);
794 if (err)
795 goto out;
796
797 spin_lock(&c->space_lock);
798 /*
799 * Although we have not finished committing yet, update size of the
800 * committed index ('c->old_idx_sz') and zero out the index growth
801 * budget. It is OK to do this now, because we've reserved all the
802 * space which is needed to commit the index, and it is save for the
803 * budgeting subsystem to assume the index is already committed,
804 * even though it is not.
805 */
806 c->old_idx_sz = c->calc_idx_sz;
807 c->budg_uncommitted_idx = 0;
808 spin_unlock(&c->space_lock);
809 mutex_unlock(&c->tnc_mutex);
810
811 dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
812 dbg_cmt("size of index %llu", c->calc_idx_sz);
813 return err;
814
815out_free:
816 free_idx_lebs(c);
817out:
818 mutex_unlock(&c->tnc_mutex);
819 return err;
820}
821
822/**
823 * write_index - write index nodes.
824 * @c: UBIFS file-system description object
825 *
826 * This function writes the index nodes whose positions were laid out in the
827 * layout_in_empty_space function.
828 */
829static int write_index(struct ubifs_info *c)
830{
831 struct ubifs_idx_node *idx;
832 struct ubifs_znode *znode, *cnext;
833 int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
834 int avail, wlen, err, lnum_pos = 0;
835
836 cnext = c->enext;
837 if (!cnext)
838 return 0;
839
840 /*
841 * Always write index nodes to the index head so that index nodes and
842 * other types of nodes are never mixed in the same erase block.
843 */
844 lnum = c->ihead_lnum;
845 buf_offs = c->ihead_offs;
846
847 /* Allocate commit buffer */
848 buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
849 used = 0;
850 avail = buf_len;
851
852 /* Ensure there is enough room for first write */
853 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
854 if (buf_offs + next_len > c->leb_size) {
855 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
856 LPROPS_TAKEN);
857 if (err)
858 return err;
859 lnum = -1;
860 }
861
862 while (1) {
863 cond_resched();
864
865 znode = cnext;
866 idx = c->cbuf + used;
867
868 /* Make index node */
869 idx->ch.node_type = UBIFS_IDX_NODE;
870 idx->child_cnt = cpu_to_le16(znode->child_cnt);
871 idx->level = cpu_to_le16(znode->level);
872 for (i = 0; i < znode->child_cnt; i++) {
873 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
874 struct ubifs_zbranch *zbr = &znode->zbranch[i];
875
876 key_write_idx(c, &zbr->key, &br->key);
877 br->lnum = cpu_to_le32(zbr->lnum);
878 br->offs = cpu_to_le32(zbr->offs);
879 br->len = cpu_to_le32(zbr->len);
880 if (!zbr->lnum || !zbr->len) {
881 ubifs_err("bad ref in znode");
882 dbg_dump_znode(c, znode);
883 if (zbr->znode)
884 dbg_dump_znode(c, zbr->znode);
885 }
886 }
887 len = ubifs_idx_node_sz(c, znode->child_cnt);
888 ubifs_prepare_node(c, idx, len, 0);
889
890 /* Determine the index node position */
891 if (lnum == -1) {
892 lnum = c->ilebs[lnum_pos++];
893 buf_offs = 0;
894 used = 0;
895 avail = buf_len;
896 }
897 offs = buf_offs + used;
898
899#ifdef CONFIG_UBIFS_FS_DEBUG
900 if (lnum != znode->lnum || offs != znode->offs ||
901 len != znode->len) {
902 ubifs_err("inconsistent znode posn");
903 return -EINVAL;
904 }
905#endif
906
907 /* Grab some stuff from znode while we still can */
908 cnext = znode->cnext;
909
910 ubifs_assert(ubifs_zn_dirty(znode));
911 ubifs_assert(test_bit(COW_ZNODE, &znode->flags));
912
913 /*
914 * It is important that other threads should see %DIRTY_ZNODE
915 * flag cleared before %COW_ZNODE. Specifically, it matters in
916 * the 'dirty_cow_znode()' function. This is the reason for the
917 * first barrier. Also, we want the bit changes to be seen to
918 * other threads ASAP, to avoid unnecesarry copying, which is
919 * the reason for the second barrier.
920 */
921 clear_bit(DIRTY_ZNODE, &znode->flags);
922 smp_mb__before_clear_bit();
923 clear_bit(COW_ZNODE, &znode->flags);
924 smp_mb__after_clear_bit();
925
926 /* Do not access znode from this point on */
927
928 /* Update buffer positions */
929 wlen = used + len;
930 used += ALIGN(len, 8);
931 avail -= ALIGN(len, 8);
932
933 /*
934 * Calculate the next index node length to see if there is
935 * enough room for it
936 */
937 if (cnext == c->cnext)
938 next_len = 0;
939 else
940 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
941
942 if (c->min_io_size == 1) {
943 /*
944 * Write the prepared index node immediately if there is
945 * no minimum IO size
946 */
947 err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs,
948 wlen, UBI_SHORTTERM);
949 if (err)
950 return err;
951 buf_offs += ALIGN(wlen, 8);
952 if (next_len) {
953 used = 0;
954 avail = buf_len;
955 if (buf_offs + next_len > c->leb_size) {
956 err = ubifs_update_one_lp(c, lnum,
957 LPROPS_NC, 0, 0, LPROPS_TAKEN);
958 if (err)
959 return err;
960 lnum = -1;
961 }
962 continue;
963 }
964 } else {
965 int blen, nxt_offs = buf_offs + used + next_len;
966
967 if (next_len && nxt_offs <= c->leb_size) {
968 if (avail > 0)
969 continue;
970 else
971 blen = buf_len;
972 } else {
973 wlen = ALIGN(wlen, 8);
974 blen = ALIGN(wlen, c->min_io_size);
975 ubifs_pad(c, c->cbuf + wlen, blen - wlen);
976 }
977 /*
978 * The buffer is full or there are no more znodes
979 * to do
980 */
981 err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs,
982 blen, UBI_SHORTTERM);
983 if (err)
984 return err;
985 buf_offs += blen;
986 if (next_len) {
987 if (nxt_offs > c->leb_size) {
988 err = ubifs_update_one_lp(c, lnum,
989 LPROPS_NC, 0, 0, LPROPS_TAKEN);
990 if (err)
991 return err;
992 lnum = -1;
993 }
994 used -= blen;
995 if (used < 0)
996 used = 0;
997 avail = buf_len - used;
998 memmove(c->cbuf, c->cbuf + blen, used);
999 continue;
1000 }
1001 }
1002 break;
1003 }
1004
1005#ifdef CONFIG_UBIFS_FS_DEBUG
1006 if (lnum != c->new_ihead_lnum || buf_offs != c->new_ihead_offs) {
1007 ubifs_err("inconsistent ihead");
1008 return -EINVAL;
1009 }
1010#endif
1011
1012 c->ihead_lnum = lnum;
1013 c->ihead_offs = buf_offs;
1014
1015 return 0;
1016}
1017
1018/**
1019 * free_obsolete_znodes - free obsolete znodes.
1020 * @c: UBIFS file-system description object
1021 *
1022 * At the end of commit end, obsolete znodes are freed.
1023 */
1024static void free_obsolete_znodes(struct ubifs_info *c)
1025{
1026 struct ubifs_znode *znode, *cnext;
1027
1028 cnext = c->cnext;
1029 do {
1030 znode = cnext;
1031 cnext = znode->cnext;
1032 if (test_bit(OBSOLETE_ZNODE, &znode->flags))
1033 kfree(znode);
1034 else {
1035 znode->cnext = NULL;
1036 atomic_long_inc(&c->clean_zn_cnt);
1037 atomic_long_inc(&ubifs_clean_zn_cnt);
1038 }
1039 } while (cnext != c->cnext);
1040}
1041
1042/**
1043 * return_gap_lebs - return LEBs used by the in-gap commit method.
1044 * @c: UBIFS file-system description object
1045 *
1046 * This function clears the "taken" flag for the LEBs which were used by the
1047 * "commit in-the-gaps" method.
1048 */
1049static int return_gap_lebs(struct ubifs_info *c)
1050{
1051 int *p, err;
1052
1053 if (!c->gap_lebs)
1054 return 0;
1055
1056 dbg_cmt("");
1057 for (p = c->gap_lebs; *p != -1; p++) {
1058 err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
1059 LPROPS_TAKEN, 0);
1060 if (err)
1061 return err;
1062 }
1063
1064 kfree(c->gap_lebs);
1065 c->gap_lebs = NULL;
1066 return 0;
1067}
1068
1069/**
1070 * ubifs_tnc_end_commit - update the TNC for commit end.
1071 * @c: UBIFS file-system description object
1072 *
1073 * Write the dirty znodes.
1074 */
1075int ubifs_tnc_end_commit(struct ubifs_info *c)
1076{
1077 int err;
1078
1079 if (!c->cnext)
1080 return 0;
1081
1082 err = return_gap_lebs(c);
1083 if (err)
1084 return err;
1085
1086 err = write_index(c);
1087 if (err)
1088 return err;
1089
1090 mutex_lock(&c->tnc_mutex);
1091
1092 dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
1093
1094 free_obsolete_znodes(c);
1095
1096 c->cnext = NULL;
1097 kfree(c->ilebs);
1098 c->ilebs = NULL;
1099
1100 mutex_unlock(&c->tnc_mutex);
1101
1102 return 0;
1103}
diff --git a/fs/ubifs/tnc_misc.c b/fs/ubifs/tnc_misc.c
new file mode 100644
index 000000000000..a25c1cc1f8d9
--- /dev/null
+++ b/fs/ubifs/tnc_misc.c
@@ -0,0 +1,494 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file contains miscelanious TNC-related functions shared betweend
25 * different files. This file does not form any logically separate TNC
26 * sub-system. The file was created because there is a lot of TNC code and
27 * putting it all in one file would make that file too big and unreadable.
28 */
29
30#include "ubifs.h"
31
32/**
33 * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
34 * @zr: root of the subtree to traverse
35 * @znode: previous znode
36 *
37 * This function implements levelorder TNC traversal. The LNC is ignored.
38 * Returns the next element or %NULL if @znode is already the last one.
39 */
40struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
41 struct ubifs_znode *znode)
42{
43 int level, iip, level_search = 0;
44 struct ubifs_znode *zn;
45
46 ubifs_assert(zr);
47
48 if (unlikely(!znode))
49 return zr;
50
51 if (unlikely(znode == zr)) {
52 if (znode->level == 0)
53 return NULL;
54 return ubifs_tnc_find_child(zr, 0);
55 }
56
57 level = znode->level;
58
59 iip = znode->iip;
60 while (1) {
61 ubifs_assert(znode->level <= zr->level);
62
63 /*
64 * First walk up until there is a znode with next branch to
65 * look at.
66 */
67 while (znode->parent != zr && iip >= znode->parent->child_cnt) {
68 znode = znode->parent;
69 iip = znode->iip;
70 }
71
72 if (unlikely(znode->parent == zr &&
73 iip >= znode->parent->child_cnt)) {
74 /* This level is done, switch to the lower one */
75 level -= 1;
76 if (level_search || level < 0)
77 /*
78 * We were already looking for znode at lower
79 * level ('level_search'). As we are here
80 * again, it just does not exist. Or all levels
81 * were finished ('level < 0').
82 */
83 return NULL;
84
85 level_search = 1;
86 iip = -1;
87 znode = ubifs_tnc_find_child(zr, 0);
88 ubifs_assert(znode);
89 }
90
91 /* Switch to the next index */
92 zn = ubifs_tnc_find_child(znode->parent, iip + 1);
93 if (!zn) {
94 /* No more children to look at, we have walk up */
95 iip = znode->parent->child_cnt;
96 continue;
97 }
98
99 /* Walk back down to the level we came from ('level') */
100 while (zn->level != level) {
101 znode = zn;
102 zn = ubifs_tnc_find_child(zn, 0);
103 if (!zn) {
104 /*
105 * This path is not too deep so it does not
106 * reach 'level'. Try next path.
107 */
108 iip = znode->iip;
109 break;
110 }
111 }
112
113 if (zn) {
114 ubifs_assert(zn->level >= 0);
115 return zn;
116 }
117 }
118}
119
120/**
121 * ubifs_search_zbranch - search znode branch.
122 * @c: UBIFS file-system description object
123 * @znode: znode to search in
124 * @key: key to search for
125 * @n: znode branch slot number is returned here
126 *
127 * This is a helper function which search branch with key @key in @znode using
128 * binary search. The result of the search may be:
129 * o exact match, then %1 is returned, and the slot number of the branch is
130 * stored in @n;
131 * o no exact match, then %0 is returned and the slot number of the left
132 * closest branch is returned in @n; the slot if all keys in this znode are
133 * greater than @key, then %-1 is returned in @n.
134 */
135int ubifs_search_zbranch(const struct ubifs_info *c,
136 const struct ubifs_znode *znode,
137 const union ubifs_key *key, int *n)
138{
139 int beg = 0, end = znode->child_cnt, uninitialized_var(mid);
140 int uninitialized_var(cmp);
141 const struct ubifs_zbranch *zbr = &znode->zbranch[0];
142
143 ubifs_assert(end > beg);
144
145 while (end > beg) {
146 mid = (beg + end) >> 1;
147 cmp = keys_cmp(c, key, &zbr[mid].key);
148 if (cmp > 0)
149 beg = mid + 1;
150 else if (cmp < 0)
151 end = mid;
152 else {
153 *n = mid;
154 return 1;
155 }
156 }
157
158 *n = end - 1;
159
160 /* The insert point is after *n */
161 ubifs_assert(*n >= -1 && *n < znode->child_cnt);
162 if (*n == -1)
163 ubifs_assert(keys_cmp(c, key, &zbr[0].key) < 0);
164 else
165 ubifs_assert(keys_cmp(c, key, &zbr[*n].key) > 0);
166 if (*n + 1 < znode->child_cnt)
167 ubifs_assert(keys_cmp(c, key, &zbr[*n + 1].key) < 0);
168
169 return 0;
170}
171
172/**
173 * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
174 * @znode: znode to start at (root of the sub-tree to traverse)
175 *
176 * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
177 * ignored.
178 */
179struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
180{
181 if (unlikely(!znode))
182 return NULL;
183
184 while (znode->level > 0) {
185 struct ubifs_znode *child;
186
187 child = ubifs_tnc_find_child(znode, 0);
188 if (!child)
189 return znode;
190 znode = child;
191 }
192
193 return znode;
194}
195
196/**
197 * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
198 * @znode: previous znode
199 *
200 * This function implements postorder TNC traversal. The LNC is ignored.
201 * Returns the next element or %NULL if @znode is already the last one.
202 */
203struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode)
204{
205 struct ubifs_znode *zn;
206
207 ubifs_assert(znode);
208 if (unlikely(!znode->parent))
209 return NULL;
210
211 /* Switch to the next index in the parent */
212 zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
213 if (!zn)
214 /* This is in fact the last child, return parent */
215 return znode->parent;
216
217 /* Go to the first znode in this new subtree */
218 return ubifs_tnc_postorder_first(zn);
219}
220
221/**
222 * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
223 * @znode: znode defining subtree to destroy
224 *
225 * This function destroys subtree of the TNC tree. Returns number of clean
226 * znodes in the subtree.
227 */
228long ubifs_destroy_tnc_subtree(struct ubifs_znode *znode)
229{
230 struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
231 long clean_freed = 0;
232 int n;
233
234 ubifs_assert(zn);
235 while (1) {
236 for (n = 0; n < zn->child_cnt; n++) {
237 if (!zn->zbranch[n].znode)
238 continue;
239
240 if (zn->level > 0 &&
241 !ubifs_zn_dirty(zn->zbranch[n].znode))
242 clean_freed += 1;
243
244 cond_resched();
245 kfree(zn->zbranch[n].znode);
246 }
247
248 if (zn == znode) {
249 if (!ubifs_zn_dirty(zn))
250 clean_freed += 1;
251 kfree(zn);
252 return clean_freed;
253 }
254
255 zn = ubifs_tnc_postorder_next(zn);
256 }
257}
258
259/**
260 * read_znode - read an indexing node from flash and fill znode.
261 * @c: UBIFS file-system description object
262 * @lnum: LEB of the indexing node to read
263 * @offs: node offset
264 * @len: node length
265 * @znode: znode to read to
266 *
267 * This function reads an indexing node from the flash media and fills znode
268 * with the read data. Returns zero in case of success and a negative error
269 * code in case of failure. The read indexing node is validated and if anything
270 * is wrong with it, this function prints complaint messages and returns
271 * %-EINVAL.
272 */
273static int read_znode(struct ubifs_info *c, int lnum, int offs, int len,
274 struct ubifs_znode *znode)
275{
276 int i, err, type, cmp;
277 struct ubifs_idx_node *idx;
278
279 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
280 if (!idx)
281 return -ENOMEM;
282
283 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
284 if (err < 0) {
285 kfree(idx);
286 return err;
287 }
288
289 znode->child_cnt = le16_to_cpu(idx->child_cnt);
290 znode->level = le16_to_cpu(idx->level);
291
292 dbg_tnc("LEB %d:%d, level %d, %d branch",
293 lnum, offs, znode->level, znode->child_cnt);
294
295 if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
296 dbg_err("current fanout %d, branch count %d",
297 c->fanout, znode->child_cnt);
298 dbg_err("max levels %d, znode level %d",
299 UBIFS_MAX_LEVELS, znode->level);
300 err = 1;
301 goto out_dump;
302 }
303
304 for (i = 0; i < znode->child_cnt; i++) {
305 const struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
306 struct ubifs_zbranch *zbr = &znode->zbranch[i];
307
308 key_read(c, &br->key, &zbr->key);
309 zbr->lnum = le32_to_cpu(br->lnum);
310 zbr->offs = le32_to_cpu(br->offs);
311 zbr->len = le32_to_cpu(br->len);
312 zbr->znode = NULL;
313
314 /* Validate branch */
315
316 if (zbr->lnum < c->main_first ||
317 zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
318 zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
319 dbg_err("bad branch %d", i);
320 err = 2;
321 goto out_dump;
322 }
323
324 switch (key_type(c, &zbr->key)) {
325 case UBIFS_INO_KEY:
326 case UBIFS_DATA_KEY:
327 case UBIFS_DENT_KEY:
328 case UBIFS_XENT_KEY:
329 break;
330 default:
331 dbg_msg("bad key type at slot %d: %s", i,
332 DBGKEY(&zbr->key));
333 err = 3;
334 goto out_dump;
335 }
336
337 if (znode->level)
338 continue;
339
340 type = key_type(c, &zbr->key);
341 if (c->ranges[type].max_len == 0) {
342 if (zbr->len != c->ranges[type].len) {
343 dbg_err("bad target node (type %d) length (%d)",
344 type, zbr->len);
345 dbg_err("have to be %d", c->ranges[type].len);
346 err = 4;
347 goto out_dump;
348 }
349 } else if (zbr->len < c->ranges[type].min_len ||
350 zbr->len > c->ranges[type].max_len) {
351 dbg_err("bad target node (type %d) length (%d)",
352 type, zbr->len);
353 dbg_err("have to be in range of %d-%d",
354 c->ranges[type].min_len,
355 c->ranges[type].max_len);
356 err = 5;
357 goto out_dump;
358 }
359 }
360
361 /*
362 * Ensure that the next key is greater or equivalent to the
363 * previous one.
364 */
365 for (i = 0; i < znode->child_cnt - 1; i++) {
366 const union ubifs_key *key1, *key2;
367
368 key1 = &znode->zbranch[i].key;
369 key2 = &znode->zbranch[i + 1].key;
370
371 cmp = keys_cmp(c, key1, key2);
372 if (cmp > 0) {
373 dbg_err("bad key order (keys %d and %d)", i, i + 1);
374 err = 6;
375 goto out_dump;
376 } else if (cmp == 0 && !is_hash_key(c, key1)) {
377 /* These can only be keys with colliding hash */
378 dbg_err("keys %d and %d are not hashed but equivalent",
379 i, i + 1);
380 err = 7;
381 goto out_dump;
382 }
383 }
384
385 kfree(idx);
386 return 0;
387
388out_dump:
389 ubifs_err("bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
390 dbg_dump_node(c, idx);
391 kfree(idx);
392 return -EINVAL;
393}
394
395/**
396 * ubifs_load_znode - load znode to TNC cache.
397 * @c: UBIFS file-system description object
398 * @zbr: znode branch
399 * @parent: znode's parent
400 * @iip: index in parent
401 *
402 * This function loads znode pointed to by @zbr into the TNC cache and
403 * returns pointer to it in case of success and a negative error code in case
404 * of failure.
405 */
406struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
407 struct ubifs_zbranch *zbr,
408 struct ubifs_znode *parent, int iip)
409{
410 int err;
411 struct ubifs_znode *znode;
412
413 ubifs_assert(!zbr->znode);
414 /*
415 * A slab cache is not presently used for znodes because the znode size
416 * depends on the fanout which is stored in the superblock.
417 */
418 znode = kzalloc(c->max_znode_sz, GFP_NOFS);
419 if (!znode)
420 return ERR_PTR(-ENOMEM);
421
422 err = read_znode(c, zbr->lnum, zbr->offs, zbr->len, znode);
423 if (err)
424 goto out;
425
426 atomic_long_inc(&c->clean_zn_cnt);
427
428 /*
429 * Increment the global clean znode counter as well. It is OK that
430 * global and per-FS clean znode counters may be inconsistent for some
431 * short time (because we might be preempted at this point), the global
432 * one is only used in shrinker.
433 */
434 atomic_long_inc(&ubifs_clean_zn_cnt);
435
436 zbr->znode = znode;
437 znode->parent = parent;
438 znode->time = get_seconds();
439 znode->iip = iip;
440
441 return znode;
442
443out:
444 kfree(znode);
445 return ERR_PTR(err);
446}
447
448/**
449 * ubifs_tnc_read_node - read a leaf node from the flash media.
450 * @c: UBIFS file-system description object
451 * @zbr: key and position of the node
452 * @node: node is returned here
453 *
454 * This function reads a node defined by @zbr from the flash media. Returns
455 * zero in case of success or a negative negative error code in case of
456 * failure.
457 */
458int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
459 void *node)
460{
461 union ubifs_key key1, *key = &zbr->key;
462 int err, type = key_type(c, key);
463 struct ubifs_wbuf *wbuf;
464
465 /*
466 * 'zbr' has to point to on-flash node. The node may sit in a bud and
467 * may even be in a write buffer, so we have to take care about this.
468 */
469 wbuf = ubifs_get_wbuf(c, zbr->lnum);
470 if (wbuf)
471 err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
472 zbr->lnum, zbr->offs);
473 else
474 err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
475 zbr->offs);
476
477 if (err) {
478 dbg_tnc("key %s", DBGKEY(key));
479 return err;
480 }
481
482 /* Make sure the key of the read node is correct */
483 key_read(c, key, &key1);
484 if (memcmp(node + UBIFS_KEY_OFFSET, &key1, c->key_len)) {
485 ubifs_err("bad key in node at LEB %d:%d",
486 zbr->lnum, zbr->offs);
487 dbg_tnc("looked for key %s found node's key %s",
488 DBGKEY(key), DBGKEY1(&key1));
489 dbg_dump_node(c, node);
490 return -EINVAL;
491 }
492
493 return 0;
494}
diff --git a/fs/ubifs/ubifs-media.h b/fs/ubifs/ubifs-media.h
new file mode 100644
index 000000000000..0cc7da9bed47
--- /dev/null
+++ b/fs/ubifs/ubifs-media.h
@@ -0,0 +1,745 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file describes UBIFS on-flash format and contains definitions of all the
25 * relevant data structures and constants.
26 *
27 * All UBIFS on-flash objects are stored in the form of nodes. All nodes start
28 * with the UBIFS node magic number and have the same common header. Nodes
29 * always sit at 8-byte aligned positions on the media and node header sizes are
30 * also 8-byte aligned (except for the indexing node and the padding node).
31 */
32
33#ifndef __UBIFS_MEDIA_H__
34#define __UBIFS_MEDIA_H__
35
36/* UBIFS node magic number (must not have the padding byte first or last) */
37#define UBIFS_NODE_MAGIC 0x06101831
38
39/* UBIFS on-flash format version */
40#define UBIFS_FORMAT_VERSION 4
41
42/* Minimum logical eraseblock size in bytes */
43#define UBIFS_MIN_LEB_SZ (15*1024)
44
45/* Initial CRC32 value used when calculating CRC checksums */
46#define UBIFS_CRC32_INIT 0xFFFFFFFFU
47
48/*
49 * UBIFS does not try to compress data if its length is less than the below
50 * constant.
51 */
52#define UBIFS_MIN_COMPR_LEN 128
53
54/* Root inode number */
55#define UBIFS_ROOT_INO 1
56
57/* Lowest inode number used for regular inodes (not UBIFS-only internal ones) */
58#define UBIFS_FIRST_INO 64
59
60/*
61 * Maximum file name and extended attribute length (must be a multiple of 8,
62 * minus 1).
63 */
64#define UBIFS_MAX_NLEN 255
65
66/* Maximum number of data journal heads */
67#define UBIFS_MAX_JHEADS 1
68
69/*
70 * Size of UBIFS data block. Note, UBIFS is not a block oriented file-system,
71 * which means that it does not treat the underlying media as consisting of
72 * blocks like in case of hard drives. Do not be confused. UBIFS block is just
73 * the maximum amount of data which one data node can have or which can be
74 * attached to an inode node.
75 */
76#define UBIFS_BLOCK_SIZE 4096
77#define UBIFS_BLOCK_SHIFT 12
78#define UBIFS_BLOCK_MASK 0x00000FFF
79
80/* UBIFS padding byte pattern (must not be first or last byte of node magic) */
81#define UBIFS_PADDING_BYTE 0xCE
82
83/* Maximum possible key length */
84#define UBIFS_MAX_KEY_LEN 16
85
86/* Key length ("simple" format) */
87#define UBIFS_SK_LEN 8
88
89/* Minimum index tree fanout */
90#define UBIFS_MIN_FANOUT 2
91
92/* Maximum number of levels in UBIFS indexing B-tree */
93#define UBIFS_MAX_LEVELS 512
94
95/* Maximum amount of data attached to an inode in bytes */
96#define UBIFS_MAX_INO_DATA UBIFS_BLOCK_SIZE
97
98/* LEB Properties Tree fanout (must be power of 2) and fanout shift */
99#define UBIFS_LPT_FANOUT 4
100#define UBIFS_LPT_FANOUT_SHIFT 2
101
102/* LEB Properties Tree bit field sizes */
103#define UBIFS_LPT_CRC_BITS 16
104#define UBIFS_LPT_CRC_BYTES 2
105#define UBIFS_LPT_TYPE_BITS 4
106
107/* The key is always at the same position in all keyed nodes */
108#define UBIFS_KEY_OFFSET offsetof(struct ubifs_ino_node, key)
109
110/*
111 * LEB Properties Tree node types.
112 *
113 * UBIFS_LPT_PNODE: LPT leaf node (contains LEB properties)
114 * UBIFS_LPT_NNODE: LPT internal node
115 * UBIFS_LPT_LTAB: LPT's own lprops table
116 * UBIFS_LPT_LSAVE: LPT's save table (big model only)
117 * UBIFS_LPT_NODE_CNT: count of LPT node types
118 * UBIFS_LPT_NOT_A_NODE: all ones (15 for 4 bits) is never a valid node type
119 */
120enum {
121 UBIFS_LPT_PNODE,
122 UBIFS_LPT_NNODE,
123 UBIFS_LPT_LTAB,
124 UBIFS_LPT_LSAVE,
125 UBIFS_LPT_NODE_CNT,
126 UBIFS_LPT_NOT_A_NODE = (1 << UBIFS_LPT_TYPE_BITS) - 1,
127};
128
129/*
130 * UBIFS inode types.
131 *
132 * UBIFS_ITYPE_REG: regular file
133 * UBIFS_ITYPE_DIR: directory
134 * UBIFS_ITYPE_LNK: soft link
135 * UBIFS_ITYPE_BLK: block device node
136 * UBIFS_ITYPE_CHR: character device node
137 * UBIFS_ITYPE_FIFO: fifo
138 * UBIFS_ITYPE_SOCK: socket
139 * UBIFS_ITYPES_CNT: count of supported file types
140 */
141enum {
142 UBIFS_ITYPE_REG,
143 UBIFS_ITYPE_DIR,
144 UBIFS_ITYPE_LNK,
145 UBIFS_ITYPE_BLK,
146 UBIFS_ITYPE_CHR,
147 UBIFS_ITYPE_FIFO,
148 UBIFS_ITYPE_SOCK,
149 UBIFS_ITYPES_CNT,
150};
151
152/*
153 * Supported key hash functions.
154 *
155 * UBIFS_KEY_HASH_R5: R5 hash
156 * UBIFS_KEY_HASH_TEST: test hash which just returns first 4 bytes of the name
157 */
158enum {
159 UBIFS_KEY_HASH_R5,
160 UBIFS_KEY_HASH_TEST,
161};
162
163/*
164 * Supported key formats.
165 *
166 * UBIFS_SIMPLE_KEY_FMT: simple key format
167 */
168enum {
169 UBIFS_SIMPLE_KEY_FMT,
170};
171
172/*
173 * The simple key format uses 29 bits for storing UBIFS block number and hash
174 * value.
175 */
176#define UBIFS_S_KEY_BLOCK_BITS 29
177#define UBIFS_S_KEY_BLOCK_MASK 0x1FFFFFFF
178#define UBIFS_S_KEY_HASH_BITS UBIFS_S_KEY_BLOCK_BITS
179#define UBIFS_S_KEY_HASH_MASK UBIFS_S_KEY_BLOCK_MASK
180
181/*
182 * Key types.
183 *
184 * UBIFS_INO_KEY: inode node key
185 * UBIFS_DATA_KEY: data node key
186 * UBIFS_DENT_KEY: directory entry node key
187 * UBIFS_XENT_KEY: extended attribute entry key
188 * UBIFS_KEY_TYPES_CNT: number of supported key types
189 */
190enum {
191 UBIFS_INO_KEY,
192 UBIFS_DATA_KEY,
193 UBIFS_DENT_KEY,
194 UBIFS_XENT_KEY,
195 UBIFS_KEY_TYPES_CNT,
196};
197
198/* Count of LEBs reserved for the superblock area */
199#define UBIFS_SB_LEBS 1
200/* Count of LEBs reserved for the master area */
201#define UBIFS_MST_LEBS 2
202
203/* First LEB of the superblock area */
204#define UBIFS_SB_LNUM 0
205/* First LEB of the master area */
206#define UBIFS_MST_LNUM (UBIFS_SB_LNUM + UBIFS_SB_LEBS)
207/* First LEB of the log area */
208#define UBIFS_LOG_LNUM (UBIFS_MST_LNUM + UBIFS_MST_LEBS)
209
210/*
211 * The below constants define the absolute minimum values for various UBIFS
212 * media areas. Many of them actually depend of flash geometry and the FS
213 * configuration (number of journal heads, orphan LEBs, etc). This means that
214 * the smallest volume size which can be used for UBIFS cannot be pre-defined
215 * by these constants. The file-system that meets the below limitation will not
216 * necessarily mount. UBIFS does run-time calculations and validates the FS
217 * size.
218 */
219
220/* Minimum number of logical eraseblocks in the log */
221#define UBIFS_MIN_LOG_LEBS 2
222/* Minimum number of bud logical eraseblocks (one for each head) */
223#define UBIFS_MIN_BUD_LEBS 3
224/* Minimum number of journal logical eraseblocks */
225#define UBIFS_MIN_JNL_LEBS (UBIFS_MIN_LOG_LEBS + UBIFS_MIN_BUD_LEBS)
226/* Minimum number of LPT area logical eraseblocks */
227#define UBIFS_MIN_LPT_LEBS 2
228/* Minimum number of orphan area logical eraseblocks */
229#define UBIFS_MIN_ORPH_LEBS 1
230/*
231 * Minimum number of main area logical eraseblocks (buds, 2 for the index, 1
232 * for GC, 1 for deletions, and at least 1 for committed data).
233 */
234#define UBIFS_MIN_MAIN_LEBS (UBIFS_MIN_BUD_LEBS + 5)
235
236/* Minimum number of logical eraseblocks */
237#define UBIFS_MIN_LEB_CNT (UBIFS_SB_LEBS + UBIFS_MST_LEBS + \
238 UBIFS_MIN_LOG_LEBS + UBIFS_MIN_LPT_LEBS + \
239 UBIFS_MIN_ORPH_LEBS + UBIFS_MIN_MAIN_LEBS)
240
241/* Node sizes (N.B. these are guaranteed to be multiples of 8) */
242#define UBIFS_CH_SZ sizeof(struct ubifs_ch)
243#define UBIFS_INO_NODE_SZ sizeof(struct ubifs_ino_node)
244#define UBIFS_DATA_NODE_SZ sizeof(struct ubifs_data_node)
245#define UBIFS_DENT_NODE_SZ sizeof(struct ubifs_dent_node)
246#define UBIFS_TRUN_NODE_SZ sizeof(struct ubifs_trun_node)
247#define UBIFS_PAD_NODE_SZ sizeof(struct ubifs_pad_node)
248#define UBIFS_SB_NODE_SZ sizeof(struct ubifs_sb_node)
249#define UBIFS_MST_NODE_SZ sizeof(struct ubifs_mst_node)
250#define UBIFS_REF_NODE_SZ sizeof(struct ubifs_ref_node)
251#define UBIFS_IDX_NODE_SZ sizeof(struct ubifs_idx_node)
252#define UBIFS_CS_NODE_SZ sizeof(struct ubifs_cs_node)
253#define UBIFS_ORPH_NODE_SZ sizeof(struct ubifs_orph_node)
254/* Extended attribute entry nodes are identical to directory entry nodes */
255#define UBIFS_XENT_NODE_SZ UBIFS_DENT_NODE_SZ
256/* Only this does not have to be multiple of 8 bytes */
257#define UBIFS_BRANCH_SZ sizeof(struct ubifs_branch)
258
259/* Maximum node sizes (N.B. these are guaranteed to be multiples of 8) */
260#define UBIFS_MAX_DATA_NODE_SZ (UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE)
261#define UBIFS_MAX_INO_NODE_SZ (UBIFS_INO_NODE_SZ + UBIFS_MAX_INO_DATA)
262#define UBIFS_MAX_DENT_NODE_SZ (UBIFS_DENT_NODE_SZ + UBIFS_MAX_NLEN + 1)
263#define UBIFS_MAX_XENT_NODE_SZ UBIFS_MAX_DENT_NODE_SZ
264
265/* The largest UBIFS node */
266#define UBIFS_MAX_NODE_SZ UBIFS_MAX_INO_NODE_SZ
267
268/*
269 * On-flash inode flags.
270 *
271 * UBIFS_COMPR_FL: use compression for this inode
272 * UBIFS_SYNC_FL: I/O on this inode has to be synchronous
273 * UBIFS_IMMUTABLE_FL: inode is immutable
274 * UBIFS_APPEND_FL: writes to the inode may only append data
275 * UBIFS_DIRSYNC_FL: I/O on this directory inode has to be synchronous
276 * UBIFS_XATTR_FL: this inode is the inode for an extended attribute value
277 *
278 * Note, these are on-flash flags which correspond to ioctl flags
279 * (@FS_COMPR_FL, etc). They have the same values now, but generally, do not
280 * have to be the same.
281 */
282enum {
283 UBIFS_COMPR_FL = 0x01,
284 UBIFS_SYNC_FL = 0x02,
285 UBIFS_IMMUTABLE_FL = 0x04,
286 UBIFS_APPEND_FL = 0x08,
287 UBIFS_DIRSYNC_FL = 0x10,
288 UBIFS_XATTR_FL = 0x20,
289};
290
291/* Inode flag bits used by UBIFS */
292#define UBIFS_FL_MASK 0x0000001F
293
294/*
295 * UBIFS compression algorithms.
296 *
297 * UBIFS_COMPR_NONE: no compression
298 * UBIFS_COMPR_LZO: LZO compression
299 * UBIFS_COMPR_ZLIB: ZLIB compression
300 * UBIFS_COMPR_TYPES_CNT: count of supported compression types
301 */
302enum {
303 UBIFS_COMPR_NONE,
304 UBIFS_COMPR_LZO,
305 UBIFS_COMPR_ZLIB,
306 UBIFS_COMPR_TYPES_CNT,
307};
308
309/*
310 * UBIFS node types.
311 *
312 * UBIFS_INO_NODE: inode node
313 * UBIFS_DATA_NODE: data node
314 * UBIFS_DENT_NODE: directory entry node
315 * UBIFS_XENT_NODE: extended attribute node
316 * UBIFS_TRUN_NODE: truncation node
317 * UBIFS_PAD_NODE: padding node
318 * UBIFS_SB_NODE: superblock node
319 * UBIFS_MST_NODE: master node
320 * UBIFS_REF_NODE: LEB reference node
321 * UBIFS_IDX_NODE: index node
322 * UBIFS_CS_NODE: commit start node
323 * UBIFS_ORPH_NODE: orphan node
324 * UBIFS_NODE_TYPES_CNT: count of supported node types
325 *
326 * Note, we index arrays by these numbers, so keep them low and contiguous.
327 * Node type constants for inodes, direntries and so on have to be the same as
328 * corresponding key type constants.
329 */
330enum {
331 UBIFS_INO_NODE,
332 UBIFS_DATA_NODE,
333 UBIFS_DENT_NODE,
334 UBIFS_XENT_NODE,
335 UBIFS_TRUN_NODE,
336 UBIFS_PAD_NODE,
337 UBIFS_SB_NODE,
338 UBIFS_MST_NODE,
339 UBIFS_REF_NODE,
340 UBIFS_IDX_NODE,
341 UBIFS_CS_NODE,
342 UBIFS_ORPH_NODE,
343 UBIFS_NODE_TYPES_CNT,
344};
345
346/*
347 * Master node flags.
348 *
349 * UBIFS_MST_DIRTY: rebooted uncleanly - master node is dirty
350 * UBIFS_MST_NO_ORPHS: no orphan inodes present
351 * UBIFS_MST_RCVRY: written by recovery
352 */
353enum {
354 UBIFS_MST_DIRTY = 1,
355 UBIFS_MST_NO_ORPHS = 2,
356 UBIFS_MST_RCVRY = 4,
357};
358
359/*
360 * Node group type (used by recovery to recover whole group or none).
361 *
362 * UBIFS_NO_NODE_GROUP: this node is not part of a group
363 * UBIFS_IN_NODE_GROUP: this node is a part of a group
364 * UBIFS_LAST_OF_NODE_GROUP: this node is the last in a group
365 */
366enum {
367 UBIFS_NO_NODE_GROUP = 0,
368 UBIFS_IN_NODE_GROUP,
369 UBIFS_LAST_OF_NODE_GROUP,
370};
371
372/*
373 * Superblock flags.
374 *
375 * UBIFS_FLG_BIGLPT: if "big" LPT model is used if set
376 */
377enum {
378 UBIFS_FLG_BIGLPT = 0x02,
379};
380
381/**
382 * struct ubifs_ch - common header node.
383 * @magic: UBIFS node magic number (%UBIFS_NODE_MAGIC)
384 * @crc: CRC-32 checksum of the node header
385 * @sqnum: sequence number
386 * @len: full node length
387 * @node_type: node type
388 * @group_type: node group type
389 * @padding: reserved for future, zeroes
390 *
391 * Every UBIFS node starts with this common part. If the node has a key, the
392 * key always goes next.
393 */
394struct ubifs_ch {
395 __le32 magic;
396 __le32 crc;
397 __le64 sqnum;
398 __le32 len;
399 __u8 node_type;
400 __u8 group_type;
401 __u8 padding[2];
402} __attribute__ ((packed));
403
404/**
405 * union ubifs_dev_desc - device node descriptor.
406 * @new: new type device descriptor
407 * @huge: huge type device descriptor
408 *
409 * This data structure describes major/minor numbers of a device node. In an
410 * inode is a device node then its data contains an object of this type. UBIFS
411 * uses standard Linux "new" and "huge" device node encodings.
412 */
413union ubifs_dev_desc {
414 __le32 new;
415 __le64 huge;
416} __attribute__ ((packed));
417
418/**
419 * struct ubifs_ino_node - inode node.
420 * @ch: common header
421 * @key: node key
422 * @creat_sqnum: sequence number at time of creation
423 * @size: inode size in bytes (amount of uncompressed data)
424 * @atime_sec: access time seconds
425 * @ctime_sec: creation time seconds
426 * @mtime_sec: modification time seconds
427 * @atime_nsec: access time nanoseconds
428 * @ctime_nsec: creation time nanoseconds
429 * @mtime_nsec: modification time nanoseconds
430 * @nlink: number of hard links
431 * @uid: owner ID
432 * @gid: group ID
433 * @mode: access flags
434 * @flags: per-inode flags (%UBIFS_COMPR_FL, %UBIFS_SYNC_FL, etc)
435 * @data_len: inode data length
436 * @xattr_cnt: count of extended attributes this inode has
437 * @xattr_size: summarized size of all extended attributes in bytes
438 * @padding1: reserved for future, zeroes
439 * @xattr_names: sum of lengths of all extended attribute names belonging to
440 * this inode
441 * @compr_type: compression type used for this inode
442 * @padding2: reserved for future, zeroes
443 * @data: data attached to the inode
444 *
445 * Note, even though inode compression type is defined by @compr_type, some
446 * nodes of this inode may be compressed with different compressor - this
447 * happens if compression type is changed while the inode already has data
448 * nodes. But @compr_type will be use for further writes to the inode.
449 *
450 * Note, do not forget to amend 'zero_ino_node_unused()' function when changing
451 * the padding fields.
452 */
453struct ubifs_ino_node {
454 struct ubifs_ch ch;
455 __u8 key[UBIFS_MAX_KEY_LEN];
456 __le64 creat_sqnum;
457 __le64 size;
458 __le64 atime_sec;
459 __le64 ctime_sec;
460 __le64 mtime_sec;
461 __le32 atime_nsec;
462 __le32 ctime_nsec;
463 __le32 mtime_nsec;
464 __le32 nlink;
465 __le32 uid;
466 __le32 gid;
467 __le32 mode;
468 __le32 flags;
469 __le32 data_len;
470 __le32 xattr_cnt;
471 __le32 xattr_size;
472 __u8 padding1[4]; /* Watch 'zero_ino_node_unused()' if changing! */
473 __le32 xattr_names;
474 __le16 compr_type;
475 __u8 padding2[26]; /* Watch 'zero_ino_node_unused()' if changing! */
476 __u8 data[];
477} __attribute__ ((packed));
478
479/**
480 * struct ubifs_dent_node - directory entry node.
481 * @ch: common header
482 * @key: node key
483 * @inum: target inode number
484 * @padding1: reserved for future, zeroes
485 * @type: type of the target inode (%UBIFS_ITYPE_REG, %UBIFS_ITYPE_DIR, etc)
486 * @nlen: name length
487 * @padding2: reserved for future, zeroes
488 * @name: zero-terminated name
489 *
490 * Note, do not forget to amend 'zero_dent_node_unused()' function when
491 * changing the padding fields.
492 */
493struct ubifs_dent_node {
494 struct ubifs_ch ch;
495 __u8 key[UBIFS_MAX_KEY_LEN];
496 __le64 inum;
497 __u8 padding1;
498 __u8 type;
499 __le16 nlen;
500 __u8 padding2[4]; /* Watch 'zero_dent_node_unused()' if changing! */
501 __u8 name[];
502} __attribute__ ((packed));
503
504/**
505 * struct ubifs_data_node - data node.
506 * @ch: common header
507 * @key: node key
508 * @size: uncompressed data size in bytes
509 * @compr_type: compression type (%UBIFS_COMPR_NONE, %UBIFS_COMPR_LZO, etc)
510 * @padding: reserved for future, zeroes
511 * @data: data
512 *
513 * Note, do not forget to amend 'zero_data_node_unused()' function when
514 * changing the padding fields.
515 */
516struct ubifs_data_node {
517 struct ubifs_ch ch;
518 __u8 key[UBIFS_MAX_KEY_LEN];
519 __le32 size;
520 __le16 compr_type;
521 __u8 padding[2]; /* Watch 'zero_data_node_unused()' if changing! */
522 __u8 data[];
523} __attribute__ ((packed));
524
525/**
526 * struct ubifs_trun_node - truncation node.
527 * @ch: common header
528 * @inum: truncated inode number
529 * @padding: reserved for future, zeroes
530 * @old_size: size before truncation
531 * @new_size: size after truncation
532 *
533 * This node exists only in the journal and never goes to the main area. Note,
534 * do not forget to amend 'zero_trun_node_unused()' function when changing the
535 * padding fields.
536 */
537struct ubifs_trun_node {
538 struct ubifs_ch ch;
539 __le32 inum;
540 __u8 padding[12]; /* Watch 'zero_trun_node_unused()' if changing! */
541 __le64 old_size;
542 __le64 new_size;
543} __attribute__ ((packed));
544
545/**
546 * struct ubifs_pad_node - padding node.
547 * @ch: common header
548 * @pad_len: how many bytes after this node are unused (because padded)
549 * @padding: reserved for future, zeroes
550 */
551struct ubifs_pad_node {
552 struct ubifs_ch ch;
553 __le32 pad_len;
554} __attribute__ ((packed));
555
556/**
557 * struct ubifs_sb_node - superblock node.
558 * @ch: common header
559 * @padding: reserved for future, zeroes
560 * @key_hash: type of hash function used in keys
561 * @key_fmt: format of the key
562 * @flags: file-system flags (%UBIFS_FLG_BIGLPT, etc)
563 * @min_io_size: minimal input/output unit size
564 * @leb_size: logical eraseblock size in bytes
565 * @leb_cnt: count of LEBs used by file-system
566 * @max_leb_cnt: maximum count of LEBs used by file-system
567 * @max_bud_bytes: maximum amount of data stored in buds
568 * @log_lebs: log size in logical eraseblocks
569 * @lpt_lebs: number of LEBs used for lprops table
570 * @orph_lebs: number of LEBs used for recording orphans
571 * @jhead_cnt: count of journal heads
572 * @fanout: tree fanout (max. number of links per indexing node)
573 * @lsave_cnt: number of LEB numbers in LPT's save table
574 * @fmt_version: UBIFS on-flash format version
575 * @default_compr: default compression algorithm (%UBIFS_COMPR_LZO, etc)
576 * @padding1: reserved for future, zeroes
577 * @rp_uid: reserve pool UID
578 * @rp_gid: reserve pool GID
579 * @rp_size: size of the reserved pool in bytes
580 * @padding2: reserved for future, zeroes
581 * @time_gran: time granularity in nanoseconds
582 * @uuid: UUID generated when the file system image was created
583 */
584struct ubifs_sb_node {
585 struct ubifs_ch ch;
586 __u8 padding[2];
587 __u8 key_hash;
588 __u8 key_fmt;
589 __le32 flags;
590 __le32 min_io_size;
591 __le32 leb_size;
592 __le32 leb_cnt;
593 __le32 max_leb_cnt;
594 __le64 max_bud_bytes;
595 __le32 log_lebs;
596 __le32 lpt_lebs;
597 __le32 orph_lebs;
598 __le32 jhead_cnt;
599 __le32 fanout;
600 __le32 lsave_cnt;
601 __le32 fmt_version;
602 __le16 default_compr;
603 __u8 padding1[2];
604 __le32 rp_uid;
605 __le32 rp_gid;
606 __le64 rp_size;
607 __le32 time_gran;
608 __u8 uuid[16];
609 __u8 padding2[3972];
610} __attribute__ ((packed));
611
612/**
613 * struct ubifs_mst_node - master node.
614 * @ch: common header
615 * @highest_inum: highest inode number in the committed index
616 * @cmt_no: commit number
617 * @flags: various flags (%UBIFS_MST_DIRTY, etc)
618 * @log_lnum: start of the log
619 * @root_lnum: LEB number of the root indexing node
620 * @root_offs: offset within @root_lnum
621 * @root_len: root indexing node length
622 * @gc_lnum: LEB reserved for garbage collection (%-1 value means the LEB was
623 * not reserved and should be reserved on mount)
624 * @ihead_lnum: LEB number of index head
625 * @ihead_offs: offset of index head
626 * @index_size: size of index on flash
627 * @total_free: total free space in bytes
628 * @total_dirty: total dirty space in bytes
629 * @total_used: total used space in bytes (includes only data LEBs)
630 * @total_dead: total dead space in bytes (includes only data LEBs)
631 * @total_dark: total dark space in bytes (includes only data LEBs)
632 * @lpt_lnum: LEB number of LPT root nnode
633 * @lpt_offs: offset of LPT root nnode
634 * @nhead_lnum: LEB number of LPT head
635 * @nhead_offs: offset of LPT head
636 * @ltab_lnum: LEB number of LPT's own lprops table
637 * @ltab_offs: offset of LPT's own lprops table
638 * @lsave_lnum: LEB number of LPT's save table (big model only)
639 * @lsave_offs: offset of LPT's save table (big model only)
640 * @lscan_lnum: LEB number of last LPT scan
641 * @empty_lebs: number of empty logical eraseblocks
642 * @idx_lebs: number of indexing logical eraseblocks
643 * @leb_cnt: count of LEBs used by file-system
644 * @padding: reserved for future, zeroes
645 */
646struct ubifs_mst_node {
647 struct ubifs_ch ch;
648 __le64 highest_inum;
649 __le64 cmt_no;
650 __le32 flags;
651 __le32 log_lnum;
652 __le32 root_lnum;
653 __le32 root_offs;
654 __le32 root_len;
655 __le32 gc_lnum;
656 __le32 ihead_lnum;
657 __le32 ihead_offs;
658 __le64 index_size;
659 __le64 total_free;
660 __le64 total_dirty;
661 __le64 total_used;
662 __le64 total_dead;
663 __le64 total_dark;
664 __le32 lpt_lnum;
665 __le32 lpt_offs;
666 __le32 nhead_lnum;
667 __le32 nhead_offs;
668 __le32 ltab_lnum;
669 __le32 ltab_offs;
670 __le32 lsave_lnum;
671 __le32 lsave_offs;
672 __le32 lscan_lnum;
673 __le32 empty_lebs;
674 __le32 idx_lebs;
675 __le32 leb_cnt;
676 __u8 padding[344];
677} __attribute__ ((packed));
678
679/**
680 * struct ubifs_ref_node - logical eraseblock reference node.
681 * @ch: common header
682 * @lnum: the referred logical eraseblock number
683 * @offs: start offset in the referred LEB
684 * @jhead: journal head number
685 * @padding: reserved for future, zeroes
686 */
687struct ubifs_ref_node {
688 struct ubifs_ch ch;
689 __le32 lnum;
690 __le32 offs;
691 __le32 jhead;
692 __u8 padding[28];
693} __attribute__ ((packed));
694
695/**
696 * struct ubifs_branch - key/reference/length branch
697 * @lnum: LEB number of the target node
698 * @offs: offset within @lnum
699 * @len: target node length
700 * @key: key
701 */
702struct ubifs_branch {
703 __le32 lnum;
704 __le32 offs;
705 __le32 len;
706 __u8 key[];
707} __attribute__ ((packed));
708
709/**
710 * struct ubifs_idx_node - indexing node.
711 * @ch: common header
712 * @child_cnt: number of child index nodes
713 * @level: tree level
714 * @branches: LEB number / offset / length / key branches
715 */
716struct ubifs_idx_node {
717 struct ubifs_ch ch;
718 __le16 child_cnt;
719 __le16 level;
720 __u8 branches[];
721} __attribute__ ((packed));
722
723/**
724 * struct ubifs_cs_node - commit start node.
725 * @ch: common header
726 * @cmt_no: commit number
727 */
728struct ubifs_cs_node {
729 struct ubifs_ch ch;
730 __le64 cmt_no;
731} __attribute__ ((packed));
732
733/**
734 * struct ubifs_orph_node - orphan node.
735 * @ch: common header
736 * @cmt_no: commit number (also top bit is set on the last node of the commit)
737 * @inos: inode numbers of orphans
738 */
739struct ubifs_orph_node {
740 struct ubifs_ch ch;
741 __le64 cmt_no;
742 __le64 inos[];
743} __attribute__ ((packed));
744
745#endif /* __UBIFS_MEDIA_H__ */
diff --git a/fs/ubifs/ubifs.h b/fs/ubifs/ubifs.h
new file mode 100644
index 000000000000..e4f89f271827
--- /dev/null
+++ b/fs/ubifs/ubifs.h
@@ -0,0 +1,1649 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/* Implementation version 0.7 */
24
25#ifndef __UBIFS_H__
26#define __UBIFS_H__
27
28#include <asm/div64.h>
29#include <linux/statfs.h>
30#include <linux/fs.h>
31#include <linux/err.h>
32#include <linux/sched.h>
33#include <linux/vmalloc.h>
34#include <linux/spinlock.h>
35#include <linux/mutex.h>
36#include <linux/rwsem.h>
37#include <linux/mtd/ubi.h>
38#include <linux/pagemap.h>
39#include <linux/backing-dev.h>
40#include "ubifs-media.h"
41
42/* Version of this UBIFS implementation */
43#define UBIFS_VERSION 1
44
45/* Normal UBIFS messages */
46#define ubifs_msg(fmt, ...) \
47 printk(KERN_NOTICE "UBIFS: " fmt "\n", ##__VA_ARGS__)
48/* UBIFS error messages */
49#define ubifs_err(fmt, ...) \
50 printk(KERN_ERR "UBIFS error (pid %d): %s: " fmt "\n", current->pid, \
51 __func__, ##__VA_ARGS__)
52/* UBIFS warning messages */
53#define ubifs_warn(fmt, ...) \
54 printk(KERN_WARNING "UBIFS warning (pid %d): %s: " fmt "\n", \
55 current->pid, __func__, ##__VA_ARGS__)
56
57/* UBIFS file system VFS magic number */
58#define UBIFS_SUPER_MAGIC 0x24051905
59
60/* Number of UBIFS blocks per VFS page */
61#define UBIFS_BLOCKS_PER_PAGE (PAGE_CACHE_SIZE / UBIFS_BLOCK_SIZE)
62#define UBIFS_BLOCKS_PER_PAGE_SHIFT (PAGE_CACHE_SHIFT - UBIFS_BLOCK_SHIFT)
63
64/* "File system end of life" sequence number watermark */
65#define SQNUM_WARN_WATERMARK 0xFFFFFFFF00000000ULL
66#define SQNUM_WATERMARK 0xFFFFFFFFFF000000ULL
67
68/* Minimum amount of data UBIFS writes to the flash */
69#define MIN_WRITE_SZ (UBIFS_DATA_NODE_SZ + 8)
70
71/*
72 * Currently we do not support inode number overlapping and re-using, so this
73 * watermark defines dangerous inode number level. This should be fixed later,
74 * although it is difficult to exceed current limit. Another option is to use
75 * 64-bit inode numbers, but this means more overhead.
76 */
77#define INUM_WARN_WATERMARK 0xFFF00000
78#define INUM_WATERMARK 0xFFFFFF00
79
80/* Largest key size supported in this implementation */
81#define CUR_MAX_KEY_LEN UBIFS_SK_LEN
82
83/* Maximum number of entries in each LPT (LEB category) heap */
84#define LPT_HEAP_SZ 256
85
86/*
87 * Background thread name pattern. The numbers are UBI device and volume
88 * numbers.
89 */
90#define BGT_NAME_PATTERN "ubifs_bgt%d_%d"
91
92/* Default write-buffer synchronization timeout (5 secs) */
93#define DEFAULT_WBUF_TIMEOUT (5 * HZ)
94
95/* Maximum possible inode number (only 32-bit inodes are supported now) */
96#define MAX_INUM 0xFFFFFFFF
97
98/* Number of non-data journal heads */
99#define NONDATA_JHEADS_CNT 2
100
101/* Garbage collector head */
102#define GCHD 0
103/* Base journal head number */
104#define BASEHD 1
105/* First "general purpose" journal head */
106#define DATAHD 2
107
108/* 'No change' value for 'ubifs_change_lp()' */
109#define LPROPS_NC 0x80000001
110
111/*
112 * There is no notion of truncation key because truncation nodes do not exist
113 * in TNC. However, when replaying, it is handy to introduce fake "truncation"
114 * keys for truncation nodes because the code becomes simpler. So we define
115 * %UBIFS_TRUN_KEY type.
116 */
117#define UBIFS_TRUN_KEY UBIFS_KEY_TYPES_CNT
118
119/*
120 * How much a directory entry/extended attribute entry adds to the parent/host
121 * inode.
122 */
123#define CALC_DENT_SIZE(name_len) ALIGN(UBIFS_DENT_NODE_SZ + (name_len) + 1, 8)
124
125/* How much an extended attribute adds to the host inode */
126#define CALC_XATTR_BYTES(data_len) ALIGN(UBIFS_INO_NODE_SZ + (data_len) + 1, 8)
127
128/*
129 * Znodes which were not touched for 'OLD_ZNODE_AGE' seconds are considered
130 * "old", and znode which were touched last 'YOUNG_ZNODE_AGE' seconds ago are
131 * considered "young". This is used by shrinker when selecting znode to trim
132 * off.
133 */
134#define OLD_ZNODE_AGE 20
135#define YOUNG_ZNODE_AGE 5
136
137/*
138 * Some compressors, like LZO, may end up with more data then the input buffer.
139 * So UBIFS always allocates larger output buffer, to be sure the compressor
140 * will not corrupt memory in case of worst case compression.
141 */
142#define WORST_COMPR_FACTOR 2
143
144/* Maximum expected tree height for use by bottom_up_buf */
145#define BOTTOM_UP_HEIGHT 64
146
147/*
148 * Lockdep classes for UBIFS inode @ui_mutex.
149 */
150enum {
151 WB_MUTEX_1 = 0,
152 WB_MUTEX_2 = 1,
153 WB_MUTEX_3 = 2,
154};
155
156/*
157 * Znode flags (actually, bit numbers which store the flags).
158 *
159 * DIRTY_ZNODE: znode is dirty
160 * COW_ZNODE: znode is being committed and a new instance of this znode has to
161 * be created before changing this znode
162 * OBSOLETE_ZNODE: znode is obsolete, which means it was deleted, but it is
163 * still in the commit list and the ongoing commit operation
164 * will commit it, and delete this znode after it is done
165 */
166enum {
167 DIRTY_ZNODE = 0,
168 COW_ZNODE = 1,
169 OBSOLETE_ZNODE = 2,
170};
171
172/*
173 * Commit states.
174 *
175 * COMMIT_RESTING: commit is not wanted
176 * COMMIT_BACKGROUND: background commit has been requested
177 * COMMIT_REQUIRED: commit is required
178 * COMMIT_RUNNING_BACKGROUND: background commit is running
179 * COMMIT_RUNNING_REQUIRED: commit is running and it is required
180 * COMMIT_BROKEN: commit failed
181 */
182enum {
183 COMMIT_RESTING = 0,
184 COMMIT_BACKGROUND,
185 COMMIT_REQUIRED,
186 COMMIT_RUNNING_BACKGROUND,
187 COMMIT_RUNNING_REQUIRED,
188 COMMIT_BROKEN,
189};
190
191/*
192 * 'ubifs_scan_a_node()' return values.
193 *
194 * SCANNED_GARBAGE: scanned garbage
195 * SCANNED_EMPTY_SPACE: scanned empty space
196 * SCANNED_A_NODE: scanned a valid node
197 * SCANNED_A_CORRUPT_NODE: scanned a corrupted node
198 * SCANNED_A_BAD_PAD_NODE: scanned a padding node with invalid pad length
199 *
200 * Greater than zero means: 'scanned that number of padding bytes'
201 */
202enum {
203 SCANNED_GARBAGE = 0,
204 SCANNED_EMPTY_SPACE = -1,
205 SCANNED_A_NODE = -2,
206 SCANNED_A_CORRUPT_NODE = -3,
207 SCANNED_A_BAD_PAD_NODE = -4,
208};
209
210/*
211 * LPT cnode flag bits.
212 *
213 * DIRTY_CNODE: cnode is dirty
214 * COW_CNODE: cnode is being committed and must be copied before writing
215 * OBSOLETE_CNODE: cnode is being committed and has been copied (or deleted),
216 * so it can (and must) be freed when the commit is finished
217 */
218enum {
219 DIRTY_CNODE = 0,
220 COW_CNODE = 1,
221 OBSOLETE_CNODE = 2,
222};
223
224/*
225 * Dirty flag bits (lpt_drty_flgs) for LPT special nodes.
226 *
227 * LTAB_DIRTY: ltab node is dirty
228 * LSAVE_DIRTY: lsave node is dirty
229 */
230enum {
231 LTAB_DIRTY = 1,
232 LSAVE_DIRTY = 2,
233};
234
235/*
236 * Return codes used by the garbage collector.
237 * @LEB_FREED: the logical eraseblock was freed and is ready to use
238 * @LEB_FREED_IDX: indexing LEB was freed and can be used only after the commit
239 * @LEB_RETAINED: the logical eraseblock was freed and retained for GC purposes
240 */
241enum {
242 LEB_FREED,
243 LEB_FREED_IDX,
244 LEB_RETAINED,
245};
246
247/**
248 * struct ubifs_old_idx - index node obsoleted since last commit start.
249 * @rb: rb-tree node
250 * @lnum: LEB number of obsoleted index node
251 * @offs: offset of obsoleted index node
252 */
253struct ubifs_old_idx {
254 struct rb_node rb;
255 int lnum;
256 int offs;
257};
258
259/* The below union makes it easier to deal with keys */
260union ubifs_key {
261 uint8_t u8[CUR_MAX_KEY_LEN];
262 uint32_t u32[CUR_MAX_KEY_LEN/4];
263 uint64_t u64[CUR_MAX_KEY_LEN/8];
264 __le32 j32[CUR_MAX_KEY_LEN/4];
265};
266
267/**
268 * struct ubifs_scan_node - UBIFS scanned node information.
269 * @list: list of scanned nodes
270 * @key: key of node scanned (if it has one)
271 * @sqnum: sequence number
272 * @type: type of node scanned
273 * @offs: offset with LEB of node scanned
274 * @len: length of node scanned
275 * @node: raw node
276 */
277struct ubifs_scan_node {
278 struct list_head list;
279 union ubifs_key key;
280 unsigned long long sqnum;
281 int type;
282 int offs;
283 int len;
284 void *node;
285};
286
287/**
288 * struct ubifs_scan_leb - UBIFS scanned LEB information.
289 * @lnum: logical eraseblock number
290 * @nodes_cnt: number of nodes scanned
291 * @nodes: list of struct ubifs_scan_node
292 * @endpt: end point (and therefore the start of empty space)
293 * @ecc: read returned -EBADMSG
294 * @buf: buffer containing entire LEB scanned
295 */
296struct ubifs_scan_leb {
297 int lnum;
298 int nodes_cnt;
299 struct list_head nodes;
300 int endpt;
301 int ecc;
302 void *buf;
303};
304
305/**
306 * struct ubifs_gced_idx_leb - garbage-collected indexing LEB.
307 * @list: list
308 * @lnum: LEB number
309 * @unmap: OK to unmap this LEB
310 *
311 * This data structure is used to temporary store garbage-collected indexing
312 * LEBs - they are not released immediately, but only after the next commit.
313 * This is needed to guarantee recoverability.
314 */
315struct ubifs_gced_idx_leb {
316 struct list_head list;
317 int lnum;
318 int unmap;
319};
320
321/**
322 * struct ubifs_inode - UBIFS in-memory inode description.
323 * @vfs_inode: VFS inode description object
324 * @creat_sqnum: sequence number at time of creation
325 * @xattr_size: summarized size of all extended attributes in bytes
326 * @xattr_cnt: count of extended attributes this inode has
327 * @xattr_names: sum of lengths of all extended attribute names belonging to
328 * this inode
329 * @dirty: non-zero if the inode is dirty
330 * @xattr: non-zero if this is an extended attribute inode
331 * @ui_mutex: serializes inode write-back with the rest of VFS operations,
332 * serializes "clean <-> dirty" state changes, protects @dirty,
333 * @ui_size, and @xattr_size
334 * @ui_lock: protects @synced_i_size
335 * @synced_i_size: synchronized size of inode, i.e. the value of inode size
336 * currently stored on the flash; used only for regular file
337 * inodes
338 * @ui_size: inode size used by UBIFS when writing to flash
339 * @flags: inode flags (@UBIFS_COMPR_FL, etc)
340 * @compr_type: default compression type used for this inode
341 * @data_len: length of the data attached to the inode
342 * @data: inode's data
343 *
344 * @ui_mutex exists for two main reasons. At first it prevents inodes from
345 * being written back while UBIFS changing them, being in the middle of an VFS
346 * operation. This way UBIFS makes sure the inode fields are consistent. For
347 * example, in 'ubifs_rename()' we change 3 inodes simultaneously, and
348 * write-back must not write any of them before we have finished.
349 *
350 * The second reason is budgeting - UBIFS has to budget all operations. If an
351 * operation is going to mark an inode dirty, it has to allocate budget for
352 * this. It cannot just mark it dirty because there is no guarantee there will
353 * be enough flash space to write the inode back later. This means UBIFS has
354 * to have full control over inode "clean <-> dirty" transitions (and pages
355 * actually). But unfortunately, VFS marks inodes dirty in many places, and it
356 * does not ask the file-system if it is allowed to do so (there is a notifier,
357 * but it is not enough), i.e., there is no mechanism to synchronize with this.
358 * So UBIFS has its own inode dirty flag and its own mutex to serialize
359 * "clean <-> dirty" transitions.
360 *
361 * The @synced_i_size field is used to make sure we never write pages which are
362 * beyond last synchronized inode size. See 'ubifs_writepage()' for more
363 * information.
364 *
365 * The @ui_size is a "shadow" variable for @inode->i_size and UBIFS uses
366 * @ui_size instead of @inode->i_size. The reason for this is that UBIFS cannot
367 * make sure @inode->i_size is always changed under @ui_mutex, because it
368 * cannot call 'vmtruncate()' with @ui_mutex locked, because it would deadlock
369 * with 'ubifs_writepage()' (see file.c). All the other inode fields are
370 * changed under @ui_mutex, so they do not need "shadow" fields. Note, one
371 * could consider to rework locking and base it on "shadow" fields.
372 */
373struct ubifs_inode {
374 struct inode vfs_inode;
375 unsigned long long creat_sqnum;
376 unsigned int xattr_size;
377 unsigned int xattr_cnt;
378 unsigned int xattr_names;
379 unsigned int dirty:1;
380 unsigned int xattr:1;
381 struct mutex ui_mutex;
382 spinlock_t ui_lock;
383 loff_t synced_i_size;
384 loff_t ui_size;
385 int flags;
386 int compr_type;
387 int data_len;
388 void *data;
389};
390
391/**
392 * struct ubifs_unclean_leb - records a LEB recovered under read-only mode.
393 * @list: list
394 * @lnum: LEB number of recovered LEB
395 * @endpt: offset where recovery ended
396 *
397 * This structure records a LEB identified during recovery that needs to be
398 * cleaned but was not because UBIFS was mounted read-only. The information
399 * is used to clean the LEB when remounting to read-write mode.
400 */
401struct ubifs_unclean_leb {
402 struct list_head list;
403 int lnum;
404 int endpt;
405};
406
407/*
408 * LEB properties flags.
409 *
410 * LPROPS_UNCAT: not categorized
411 * LPROPS_DIRTY: dirty > 0, not index
412 * LPROPS_DIRTY_IDX: dirty + free > UBIFS_CH_SZ and index
413 * LPROPS_FREE: free > 0, not empty, not index
414 * LPROPS_HEAP_CNT: number of heaps used for storing categorized LEBs
415 * LPROPS_EMPTY: LEB is empty, not taken
416 * LPROPS_FREEABLE: free + dirty == leb_size, not index, not taken
417 * LPROPS_FRDI_IDX: free + dirty == leb_size and index, may be taken
418 * LPROPS_CAT_MASK: mask for the LEB categories above
419 * LPROPS_TAKEN: LEB was taken (this flag is not saved on the media)
420 * LPROPS_INDEX: LEB contains indexing nodes (this flag also exists on flash)
421 */
422enum {
423 LPROPS_UNCAT = 0,
424 LPROPS_DIRTY = 1,
425 LPROPS_DIRTY_IDX = 2,
426 LPROPS_FREE = 3,
427 LPROPS_HEAP_CNT = 3,
428 LPROPS_EMPTY = 4,
429 LPROPS_FREEABLE = 5,
430 LPROPS_FRDI_IDX = 6,
431 LPROPS_CAT_MASK = 15,
432 LPROPS_TAKEN = 16,
433 LPROPS_INDEX = 32,
434};
435
436/**
437 * struct ubifs_lprops - logical eraseblock properties.
438 * @free: amount of free space in bytes
439 * @dirty: amount of dirty space in bytes
440 * @flags: LEB properties flags (see above)
441 * @lnum: LEB number
442 * @list: list of same-category lprops (for LPROPS_EMPTY and LPROPS_FREEABLE)
443 * @hpos: heap position in heap of same-category lprops (other categories)
444 */
445struct ubifs_lprops {
446 int free;
447 int dirty;
448 int flags;
449 int lnum;
450 union {
451 struct list_head list;
452 int hpos;
453 };
454};
455
456/**
457 * struct ubifs_lpt_lprops - LPT logical eraseblock properties.
458 * @free: amount of free space in bytes
459 * @dirty: amount of dirty space in bytes
460 * @tgc: trivial GC flag (1 => unmap after commit end)
461 * @cmt: commit flag (1 => reserved for commit)
462 */
463struct ubifs_lpt_lprops {
464 int free;
465 int dirty;
466 unsigned tgc : 1;
467 unsigned cmt : 1;
468};
469
470/**
471 * struct ubifs_lp_stats - statistics of eraseblocks in the main area.
472 * @empty_lebs: number of empty LEBs
473 * @taken_empty_lebs: number of taken LEBs
474 * @idx_lebs: number of indexing LEBs
475 * @total_free: total free space in bytes
476 * @total_dirty: total dirty space in bytes
477 * @total_used: total used space in bytes (includes only data LEBs)
478 * @total_dead: total dead space in bytes (includes only data LEBs)
479 * @total_dark: total dark space in bytes (includes only data LEBs)
480 *
481 * N.B. total_dirty and total_used are different to other total_* fields,
482 * because they account _all_ LEBs, not just data LEBs.
483 *
484 * 'taken_empty_lebs' counts the LEBs that are in the transient state of having
485 * been 'taken' for use but not yet written to. 'taken_empty_lebs' is needed
486 * to account correctly for gc_lnum, otherwise 'empty_lebs' could be used
487 * by itself (in which case 'unused_lebs' would be a better name). In the case
488 * of gc_lnum, it is 'taken' at mount time or whenever a LEB is retained by GC,
489 * but unlike other empty LEBs that are 'taken', it may not be written straight
490 * away (i.e. before the next commit start or unmount), so either gc_lnum must
491 * be specially accounted for, or the current approach followed i.e. count it
492 * under 'taken_empty_lebs'.
493 */
494struct ubifs_lp_stats {
495 int empty_lebs;
496 int taken_empty_lebs;
497 int idx_lebs;
498 long long total_free;
499 long long total_dirty;
500 long long total_used;
501 long long total_dead;
502 long long total_dark;
503};
504
505struct ubifs_nnode;
506
507/**
508 * struct ubifs_cnode - LEB Properties Tree common node.
509 * @parent: parent nnode
510 * @cnext: next cnode to commit
511 * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
512 * @iip: index in parent
513 * @level: level in the tree (zero for pnodes, greater than zero for nnodes)
514 * @num: node number
515 */
516struct ubifs_cnode {
517 struct ubifs_nnode *parent;
518 struct ubifs_cnode *cnext;
519 unsigned long flags;
520 int iip;
521 int level;
522 int num;
523};
524
525/**
526 * struct ubifs_pnode - LEB Properties Tree leaf node.
527 * @parent: parent nnode
528 * @cnext: next cnode to commit
529 * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
530 * @iip: index in parent
531 * @level: level in the tree (always zero for pnodes)
532 * @num: node number
533 * @lprops: LEB properties array
534 */
535struct ubifs_pnode {
536 struct ubifs_nnode *parent;
537 struct ubifs_cnode *cnext;
538 unsigned long flags;
539 int iip;
540 int level;
541 int num;
542 struct ubifs_lprops lprops[UBIFS_LPT_FANOUT];
543};
544
545/**
546 * struct ubifs_nbranch - LEB Properties Tree internal node branch.
547 * @lnum: LEB number of child
548 * @offs: offset of child
549 * @nnode: nnode child
550 * @pnode: pnode child
551 * @cnode: cnode child
552 */
553struct ubifs_nbranch {
554 int lnum;
555 int offs;
556 union {
557 struct ubifs_nnode *nnode;
558 struct ubifs_pnode *pnode;
559 struct ubifs_cnode *cnode;
560 };
561};
562
563/**
564 * struct ubifs_nnode - LEB Properties Tree internal node.
565 * @parent: parent nnode
566 * @cnext: next cnode to commit
567 * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
568 * @iip: index in parent
569 * @level: level in the tree (always greater than zero for nnodes)
570 * @num: node number
571 * @nbranch: branches to child nodes
572 */
573struct ubifs_nnode {
574 struct ubifs_nnode *parent;
575 struct ubifs_cnode *cnext;
576 unsigned long flags;
577 int iip;
578 int level;
579 int num;
580 struct ubifs_nbranch nbranch[UBIFS_LPT_FANOUT];
581};
582
583/**
584 * struct ubifs_lpt_heap - heap of categorized lprops.
585 * @arr: heap array
586 * @cnt: number in heap
587 * @max_cnt: maximum number allowed in heap
588 *
589 * There are %LPROPS_HEAP_CNT heaps.
590 */
591struct ubifs_lpt_heap {
592 struct ubifs_lprops **arr;
593 int cnt;
594 int max_cnt;
595};
596
597/*
598 * Return codes for LPT scan callback function.
599 *
600 * LPT_SCAN_CONTINUE: continue scanning
601 * LPT_SCAN_ADD: add the LEB properties scanned to the tree in memory
602 * LPT_SCAN_STOP: stop scanning
603 */
604enum {
605 LPT_SCAN_CONTINUE = 0,
606 LPT_SCAN_ADD = 1,
607 LPT_SCAN_STOP = 2,
608};
609
610struct ubifs_info;
611
612/* Callback used by the 'ubifs_lpt_scan_nolock()' function */
613typedef int (*ubifs_lpt_scan_callback)(struct ubifs_info *c,
614 const struct ubifs_lprops *lprops,
615 int in_tree, void *data);
616
617/**
618 * struct ubifs_wbuf - UBIFS write-buffer.
619 * @c: UBIFS file-system description object
620 * @buf: write-buffer (of min. flash I/O unit size)
621 * @lnum: logical eraseblock number the write-buffer points to
622 * @offs: write-buffer offset in this logical eraseblock
623 * @avail: number of bytes available in the write-buffer
624 * @used: number of used bytes in the write-buffer
625 * @dtype: type of data stored in this LEB (%UBI_LONGTERM, %UBI_SHORTTERM,
626 * %UBI_UNKNOWN)
627 * @jhead: journal head the mutex belongs to (note, needed only to shut lockdep
628 * up by 'mutex_lock_nested()).
629 * @sync_callback: write-buffer synchronization callback
630 * @io_mutex: serializes write-buffer I/O
631 * @lock: serializes @buf, @lnum, @offs, @avail, @used, @next_ino and @inodes
632 * fields
633 * @timer: write-buffer timer
634 * @timeout: timer expire interval in jiffies
635 * @need_sync: it is set if its timer expired and needs sync
636 * @next_ino: points to the next position of the following inode number
637 * @inodes: stores the inode numbers of the nodes which are in wbuf
638 *
639 * The write-buffer synchronization callback is called when the write-buffer is
640 * synchronized in order to notify how much space was wasted due to
641 * write-buffer padding and how much free space is left in the LEB.
642 *
643 * Note: the fields @buf, @lnum, @offs, @avail and @used can be read under
644 * spin-lock or mutex because they are written under both mutex and spin-lock.
645 * @buf is appended to under mutex but overwritten under both mutex and
646 * spin-lock. Thus the data between @buf and @buf + @used can be read under
647 * spinlock.
648 */
649struct ubifs_wbuf {
650 struct ubifs_info *c;
651 void *buf;
652 int lnum;
653 int offs;
654 int avail;
655 int used;
656 int dtype;
657 int jhead;
658 int (*sync_callback)(struct ubifs_info *c, int lnum, int free, int pad);
659 struct mutex io_mutex;
660 spinlock_t lock;
661 struct timer_list timer;
662 int timeout;
663 int need_sync;
664 int next_ino;
665 ino_t *inodes;
666};
667
668/**
669 * struct ubifs_bud - bud logical eraseblock.
670 * @lnum: logical eraseblock number
671 * @start: where the (uncommitted) bud data starts
672 * @jhead: journal head number this bud belongs to
673 * @list: link in the list buds belonging to the same journal head
674 * @rb: link in the tree of all buds
675 */
676struct ubifs_bud {
677 int lnum;
678 int start;
679 int jhead;
680 struct list_head list;
681 struct rb_node rb;
682};
683
684/**
685 * struct ubifs_jhead - journal head.
686 * @wbuf: head's write-buffer
687 * @buds_list: list of bud LEBs belonging to this journal head
688 *
689 * Note, the @buds list is protected by the @c->buds_lock.
690 */
691struct ubifs_jhead {
692 struct ubifs_wbuf wbuf;
693 struct list_head buds_list;
694};
695
696/**
697 * struct ubifs_zbranch - key/coordinate/length branch stored in znodes.
698 * @key: key
699 * @znode: znode address in memory
700 * @lnum: LEB number of the indexing node
701 * @offs: offset of the indexing node within @lnum
702 * @len: target node length
703 */
704struct ubifs_zbranch {
705 union ubifs_key key;
706 union {
707 struct ubifs_znode *znode;
708 void *leaf;
709 };
710 int lnum;
711 int offs;
712 int len;
713};
714
715/**
716 * struct ubifs_znode - in-memory representation of an indexing node.
717 * @parent: parent znode or NULL if it is the root
718 * @cnext: next znode to commit
719 * @flags: znode flags (%DIRTY_ZNODE, %COW_ZNODE or %OBSOLETE_ZNODE)
720 * @time: last access time (seconds)
721 * @level: level of the entry in the TNC tree
722 * @child_cnt: count of child znodes
723 * @iip: index in parent's zbranch array
724 * @alt: lower bound of key range has altered i.e. child inserted at slot 0
725 * @lnum: LEB number of the corresponding indexing node
726 * @offs: offset of the corresponding indexing node
727 * @len: length of the corresponding indexing node
728 * @zbranch: array of znode branches (@c->fanout elements)
729 */
730struct ubifs_znode {
731 struct ubifs_znode *parent;
732 struct ubifs_znode *cnext;
733 unsigned long flags;
734 unsigned long time;
735 int level;
736 int child_cnt;
737 int iip;
738 int alt;
739#ifdef CONFIG_UBIFS_FS_DEBUG
740 int lnum, offs, len;
741#endif
742 struct ubifs_zbranch zbranch[];
743};
744
745/**
746 * struct ubifs_node_range - node length range description data structure.
747 * @len: fixed node length
748 * @min_len: minimum possible node length
749 * @max_len: maximum possible node length
750 *
751 * If @max_len is %0, the node has fixed length @len.
752 */
753struct ubifs_node_range {
754 union {
755 int len;
756 int min_len;
757 };
758 int max_len;
759};
760
761/**
762 * struct ubifs_compressor - UBIFS compressor description structure.
763 * @compr_type: compressor type (%UBIFS_COMPR_LZO, etc)
764 * @cc: cryptoapi compressor handle
765 * @comp_mutex: mutex used during compression
766 * @decomp_mutex: mutex used during decompression
767 * @name: compressor name
768 * @capi_name: cryptoapi compressor name
769 */
770struct ubifs_compressor {
771 int compr_type;
772 struct crypto_comp *cc;
773 struct mutex *comp_mutex;
774 struct mutex *decomp_mutex;
775 const char *name;
776 const char *capi_name;
777};
778
779/**
780 * struct ubifs_budget_req - budget requirements of an operation.
781 *
782 * @fast: non-zero if the budgeting should try to aquire budget quickly and
783 * should not try to call write-back
784 * @recalculate: non-zero if @idx_growth, @data_growth, and @dd_growth fields
785 * have to be re-calculated
786 * @new_page: non-zero if the operation adds a new page
787 * @dirtied_page: non-zero if the operation makes a page dirty
788 * @new_dent: non-zero if the operation adds a new directory entry
789 * @mod_dent: non-zero if the operation removes or modifies an existing
790 * directory entry
791 * @new_ino: non-zero if the operation adds a new inode
792 * @new_ino_d: now much data newly created inode contains
793 * @dirtied_ino: how many inodes the operation makes dirty
794 * @dirtied_ino_d: now much data dirtied inode contains
795 * @idx_growth: how much the index will supposedly grow
796 * @data_growth: how much new data the operation will supposedly add
797 * @dd_growth: how much data that makes other data dirty the operation will
798 * supposedly add
799 *
800 * @idx_growth, @data_growth and @dd_growth are not used in budget request. The
801 * budgeting subsystem caches index and data growth values there to avoid
802 * re-calculating them when the budget is released. However, if @idx_growth is
803 * %-1, it is calculated by the release function using other fields.
804 *
805 * An inode may contain 4KiB of data at max., thus the widths of @new_ino_d
806 * is 13 bits, and @dirtied_ino_d - 15, because up to 4 inodes may be made
807 * dirty by the re-name operation.
808 */
809struct ubifs_budget_req {
810 unsigned int fast:1;
811 unsigned int recalculate:1;
812 unsigned int new_page:1;
813 unsigned int dirtied_page:1;
814 unsigned int new_dent:1;
815 unsigned int mod_dent:1;
816 unsigned int new_ino:1;
817 unsigned int new_ino_d:13;
818#ifndef UBIFS_DEBUG
819 unsigned int dirtied_ino:4;
820 unsigned int dirtied_ino_d:15;
821#else
822 /* Not bit-fields to check for overflows */
823 unsigned int dirtied_ino;
824 unsigned int dirtied_ino_d;
825#endif
826 int idx_growth;
827 int data_growth;
828 int dd_growth;
829};
830
831/**
832 * struct ubifs_orphan - stores the inode number of an orphan.
833 * @rb: rb-tree node of rb-tree of orphans sorted by inode number
834 * @list: list head of list of orphans in order added
835 * @new_list: list head of list of orphans added since the last commit
836 * @cnext: next orphan to commit
837 * @dnext: next orphan to delete
838 * @inum: inode number
839 * @new: %1 => added since the last commit, otherwise %0
840 */
841struct ubifs_orphan {
842 struct rb_node rb;
843 struct list_head list;
844 struct list_head new_list;
845 struct ubifs_orphan *cnext;
846 struct ubifs_orphan *dnext;
847 ino_t inum;
848 int new;
849};
850
851/**
852 * struct ubifs_mount_opts - UBIFS-specific mount options information.
853 * @unmount_mode: selected unmount mode (%0 default, %1 normal, %2 fast)
854 */
855struct ubifs_mount_opts {
856 unsigned int unmount_mode:2;
857};
858
859/**
860 * struct ubifs_info - UBIFS file-system description data structure
861 * (per-superblock).
862 * @vfs_sb: VFS @struct super_block object
863 * @bdi: backing device info object to make VFS happy and disable readahead
864 *
865 * @highest_inum: highest used inode number
866 * @vfs_gen: VFS inode generation counter
867 * @max_sqnum: current global sequence number
868 * @cmt_no: commit number (last successfully completed commit)
869 * @cnt_lock: protects @highest_inum, @vfs_gen, and @max_sqnum counters
870 * @fmt_version: UBIFS on-flash format version
871 * @uuid: UUID from super block
872 *
873 * @lhead_lnum: log head logical eraseblock number
874 * @lhead_offs: log head offset
875 * @ltail_lnum: log tail logical eraseblock number (offset is always 0)
876 * @log_mutex: protects the log, @lhead_lnum, @lhead_offs, @ltail_lnum, and
877 * @bud_bytes
878 * @min_log_bytes: minimum required number of bytes in the log
879 * @cmt_bud_bytes: used during commit to temporarily amount of bytes in
880 * committed buds
881 *
882 * @buds: tree of all buds indexed by bud LEB number
883 * @bud_bytes: how many bytes of flash is used by buds
884 * @buds_lock: protects the @buds tree, @bud_bytes, and per-journal head bud
885 * lists
886 * @jhead_cnt: count of journal heads
887 * @jheads: journal heads (head zero is base head)
888 * @max_bud_bytes: maximum number of bytes allowed in buds
889 * @bg_bud_bytes: number of bud bytes when background commit is initiated
890 * @old_buds: buds to be released after commit ends
891 * @max_bud_cnt: maximum number of buds
892 *
893 * @commit_sem: synchronizes committer with other processes
894 * @cmt_state: commit state
895 * @cs_lock: commit state lock
896 * @cmt_wq: wait queue to sleep on if the log is full and a commit is running
897 * @fast_unmount: do not run journal commit before un-mounting
898 * @big_lpt: flag that LPT is too big to write whole during commit
899 * @check_lpt_free: flag that indicates LPT GC may be needed
900 * @nospace: non-zero if the file-system does not have flash space (used as
901 * optimization)
902 * @nospace_rp: the same as @nospace, but additionally means that even reserved
903 * pool is full
904 *
905 * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and
906 * @calc_idx_sz
907 * @zroot: zbranch which points to the root index node and znode
908 * @cnext: next znode to commit
909 * @enext: next znode to commit to empty space
910 * @gap_lebs: array of LEBs used by the in-gaps commit method
911 * @cbuf: commit buffer
912 * @ileb_buf: buffer for commit in-the-gaps method
913 * @ileb_len: length of data in ileb_buf
914 * @ihead_lnum: LEB number of index head
915 * @ihead_offs: offset of index head
916 * @ilebs: pre-allocated index LEBs
917 * @ileb_cnt: number of pre-allocated index LEBs
918 * @ileb_nxt: next pre-allocated index LEBs
919 * @old_idx: tree of index nodes obsoleted since the last commit start
920 * @bottom_up_buf: a buffer which is used by 'dirty_cow_bottom_up()' in tnc.c
921 * @new_ihead_lnum: used by debugging to check ihead_lnum
922 * @new_ihead_offs: used by debugging to check ihead_offs
923 *
924 * @mst_node: master node
925 * @mst_offs: offset of valid master node
926 * @mst_mutex: protects the master node area, @mst_node, and @mst_offs
927 *
928 * @log_lebs: number of logical eraseblocks in the log
929 * @log_bytes: log size in bytes
930 * @log_last: last LEB of the log
931 * @lpt_lebs: number of LEBs used for lprops table
932 * @lpt_first: first LEB of the lprops table area
933 * @lpt_last: last LEB of the lprops table area
934 * @orph_lebs: number of LEBs used for the orphan area
935 * @orph_first: first LEB of the orphan area
936 * @orph_last: last LEB of the orphan area
937 * @main_lebs: count of LEBs in the main area
938 * @main_first: first LEB of the main area
939 * @main_bytes: main area size in bytes
940 * @default_compr: default compression algorithm (%UBIFS_COMPR_LZO, etc)
941 *
942 * @key_hash_type: type of the key hash
943 * @key_hash: direntry key hash function
944 * @key_fmt: key format
945 * @key_len: key length
946 * @fanout: fanout of the index tree (number of links per indexing node)
947 *
948 * @min_io_size: minimal input/output unit size
949 * @min_io_shift: number of bits in @min_io_size minus one
950 * @leb_size: logical eraseblock size in bytes
951 * @half_leb_size: half LEB size
952 * @leb_cnt: count of logical eraseblocks
953 * @max_leb_cnt: maximum count of logical eraseblocks
954 * @old_leb_cnt: count of logical eraseblocks before re-size
955 * @ro_media: the underlying UBI volume is read-only
956 *
957 * @dirty_pg_cnt: number of dirty pages (not used)
958 * @dirty_zn_cnt: number of dirty znodes
959 * @clean_zn_cnt: number of clean znodes
960 *
961 * @budg_idx_growth: amount of bytes budgeted for index growth
962 * @budg_data_growth: amount of bytes budgeted for cached data
963 * @budg_dd_growth: amount of bytes budgeted for cached data that will make
964 * other data dirty
965 * @budg_uncommitted_idx: amount of bytes were budgeted for growth of the index,
966 * but which still have to be taken into account because
967 * the index has not been committed so far
968 * @space_lock: protects @budg_idx_growth, @budg_data_growth, @budg_dd_growth,
969 * @budg_uncommited_idx, @min_idx_lebs, @old_idx_sz, and @lst;
970 * @min_idx_lebs: minimum number of LEBs required for the index
971 * @old_idx_sz: size of index on flash
972 * @calc_idx_sz: temporary variable which is used to calculate new index size
973 * (contains accurate new index size at end of TNC commit start)
974 * @lst: lprops statistics
975 *
976 * @page_budget: budget for a page
977 * @inode_budget: budget for an inode
978 * @dent_budget: budget for a directory entry
979 *
980 * @ref_node_alsz: size of the LEB reference node aligned to the min. flash
981 * I/O unit
982 * @mst_node_alsz: master node aligned size
983 * @min_idx_node_sz: minimum indexing node aligned on 8-bytes boundary
984 * @max_idx_node_sz: maximum indexing node aligned on 8-bytes boundary
985 * @max_inode_sz: maximum possible inode size in bytes
986 * @max_znode_sz: size of znode in bytes
987 * @dead_wm: LEB dead space watermark
988 * @dark_wm: LEB dark space watermark
989 * @block_cnt: count of 4KiB blocks on the FS
990 *
991 * @ranges: UBIFS node length ranges
992 * @ubi: UBI volume descriptor
993 * @di: UBI device information
994 * @vi: UBI volume information
995 *
996 * @orph_tree: rb-tree of orphan inode numbers
997 * @orph_list: list of orphan inode numbers in order added
998 * @orph_new: list of orphan inode numbers added since last commit
999 * @orph_cnext: next orphan to commit
1000 * @orph_dnext: next orphan to delete
1001 * @orphan_lock: lock for orph_tree and orph_new
1002 * @orph_buf: buffer for orphan nodes
1003 * @new_orphans: number of orphans since last commit
1004 * @cmt_orphans: number of orphans being committed
1005 * @tot_orphans: number of orphans in the rb_tree
1006 * @max_orphans: maximum number of orphans allowed
1007 * @ohead_lnum: orphan head LEB number
1008 * @ohead_offs: orphan head offset
1009 * @no_orphs: non-zero if there are no orphans
1010 *
1011 * @bgt: UBIFS background thread
1012 * @bgt_name: background thread name
1013 * @need_bgt: if background thread should run
1014 * @need_wbuf_sync: if write-buffers have to be synchronized
1015 *
1016 * @gc_lnum: LEB number used for garbage collection
1017 * @sbuf: a buffer of LEB size used by GC and replay for scanning
1018 * @idx_gc: list of index LEBs that have been garbage collected
1019 * @idx_gc_cnt: number of elements on the idx_gc list
1020 *
1021 * @infos_list: links all 'ubifs_info' objects
1022 * @umount_mutex: serializes shrinker and un-mount
1023 * @shrinker_run_no: shrinker run number
1024 *
1025 * @space_bits: number of bits needed to record free or dirty space
1026 * @lpt_lnum_bits: number of bits needed to record a LEB number in the LPT
1027 * @lpt_offs_bits: number of bits needed to record an offset in the LPT
1028 * @lpt_spc_bits: number of bits needed to space in the LPT
1029 * @pcnt_bits: number of bits needed to record pnode or nnode number
1030 * @lnum_bits: number of bits needed to record LEB number
1031 * @nnode_sz: size of on-flash nnode
1032 * @pnode_sz: size of on-flash pnode
1033 * @ltab_sz: size of on-flash LPT lprops table
1034 * @lsave_sz: size of on-flash LPT save table
1035 * @pnode_cnt: number of pnodes
1036 * @nnode_cnt: number of nnodes
1037 * @lpt_hght: height of the LPT
1038 * @pnodes_have: number of pnodes in memory
1039 *
1040 * @lp_mutex: protects lprops table and all the other lprops-related fields
1041 * @lpt_lnum: LEB number of the root nnode of the LPT
1042 * @lpt_offs: offset of the root nnode of the LPT
1043 * @nhead_lnum: LEB number of LPT head
1044 * @nhead_offs: offset of LPT head
1045 * @lpt_drty_flgs: dirty flags for LPT special nodes e.g. ltab
1046 * @dirty_nn_cnt: number of dirty nnodes
1047 * @dirty_pn_cnt: number of dirty pnodes
1048 * @lpt_sz: LPT size
1049 * @lpt_nod_buf: buffer for an on-flash nnode or pnode
1050 * @lpt_buf: buffer of LEB size used by LPT
1051 * @nroot: address in memory of the root nnode of the LPT
1052 * @lpt_cnext: next LPT node to commit
1053 * @lpt_heap: array of heaps of categorized lprops
1054 * @dirty_idx: a (reverse sorted) copy of the LPROPS_DIRTY_IDX heap as at
1055 * previous commit start
1056 * @uncat_list: list of un-categorized LEBs
1057 * @empty_list: list of empty LEBs
1058 * @freeable_list: list of freeable non-index LEBs (free + dirty == leb_size)
1059 * @frdi_idx_list: list of freeable index LEBs (free + dirty == leb_size)
1060 * @freeable_cnt: number of freeable LEBs in @freeable_list
1061 *
1062 * @ltab_lnum: LEB number of LPT's own lprops table
1063 * @ltab_offs: offset of LPT's own lprops table
1064 * @ltab: LPT's own lprops table
1065 * @ltab_cmt: LPT's own lprops table (commit copy)
1066 * @lsave_cnt: number of LEB numbers in LPT's save table
1067 * @lsave_lnum: LEB number of LPT's save table
1068 * @lsave_offs: offset of LPT's save table
1069 * @lsave: LPT's save table
1070 * @lscan_lnum: LEB number of last LPT scan
1071 *
1072 * @rp_size: size of the reserved pool in bytes
1073 * @report_rp_size: size of the reserved pool reported to user-space
1074 * @rp_uid: reserved pool user ID
1075 * @rp_gid: reserved pool group ID
1076 *
1077 * @empty: if the UBI device is empty
1078 * @replay_tree: temporary tree used during journal replay
1079 * @replay_list: temporary list used during journal replay
1080 * @replay_buds: list of buds to replay
1081 * @cs_sqnum: sequence number of first node in the log (commit start node)
1082 * @replay_sqnum: sequence number of node currently being replayed
1083 * @need_recovery: file-system needs recovery
1084 * @replaying: set to %1 during journal replay
1085 * @unclean_leb_list: LEBs to recover when mounting ro to rw
1086 * @rcvrd_mst_node: recovered master node to write when mounting ro to rw
1087 * @size_tree: inode size information for recovery
1088 * @remounting_rw: set while remounting from ro to rw (sb flags have MS_RDONLY)
1089 * @mount_opts: UBIFS-specific mount options
1090 *
1091 * @dbg_buf: a buffer of LEB size used for debugging purposes
1092 * @old_zroot: old index root - used by 'dbg_check_old_index()'
1093 * @old_zroot_level: old index root level - used by 'dbg_check_old_index()'
1094 * @old_zroot_sqnum: old index root sqnum - used by 'dbg_check_old_index()'
1095 * @failure_mode: failure mode for recovery testing
1096 * @fail_delay: 0=>don't delay, 1=>delay a time, 2=>delay a number of calls
1097 * @fail_timeout: time in jiffies when delay of failure mode expires
1098 * @fail_cnt: current number of calls to failure mode I/O functions
1099 * @fail_cnt_max: number of calls by which to delay failure mode
1100 */
1101struct ubifs_info {
1102 struct super_block *vfs_sb;
1103 struct backing_dev_info bdi;
1104
1105 ino_t highest_inum;
1106 unsigned int vfs_gen;
1107 unsigned long long max_sqnum;
1108 unsigned long long cmt_no;
1109 spinlock_t cnt_lock;
1110 int fmt_version;
1111 unsigned char uuid[16];
1112
1113 int lhead_lnum;
1114 int lhead_offs;
1115 int ltail_lnum;
1116 struct mutex log_mutex;
1117 int min_log_bytes;
1118 long long cmt_bud_bytes;
1119
1120 struct rb_root buds;
1121 long long bud_bytes;
1122 spinlock_t buds_lock;
1123 int jhead_cnt;
1124 struct ubifs_jhead *jheads;
1125 long long max_bud_bytes;
1126 long long bg_bud_bytes;
1127 struct list_head old_buds;
1128 int max_bud_cnt;
1129
1130 struct rw_semaphore commit_sem;
1131 int cmt_state;
1132 spinlock_t cs_lock;
1133 wait_queue_head_t cmt_wq;
1134 unsigned int fast_unmount:1;
1135 unsigned int big_lpt:1;
1136 unsigned int check_lpt_free:1;
1137 unsigned int nospace:1;
1138 unsigned int nospace_rp:1;
1139
1140 struct mutex tnc_mutex;
1141 struct ubifs_zbranch zroot;
1142 struct ubifs_znode *cnext;
1143 struct ubifs_znode *enext;
1144 int *gap_lebs;
1145 void *cbuf;
1146 void *ileb_buf;
1147 int ileb_len;
1148 int ihead_lnum;
1149 int ihead_offs;
1150 int *ilebs;
1151 int ileb_cnt;
1152 int ileb_nxt;
1153 struct rb_root old_idx;
1154 int *bottom_up_buf;
1155#ifdef CONFIG_UBIFS_FS_DEBUG
1156 int new_ihead_lnum;
1157 int new_ihead_offs;
1158#endif
1159
1160 struct ubifs_mst_node *mst_node;
1161 int mst_offs;
1162 struct mutex mst_mutex;
1163
1164 int log_lebs;
1165 long long log_bytes;
1166 int log_last;
1167 int lpt_lebs;
1168 int lpt_first;
1169 int lpt_last;
1170 int orph_lebs;
1171 int orph_first;
1172 int orph_last;
1173 int main_lebs;
1174 int main_first;
1175 long long main_bytes;
1176 int default_compr;
1177
1178 uint8_t key_hash_type;
1179 uint32_t (*key_hash)(const char *str, int len);
1180 int key_fmt;
1181 int key_len;
1182 int fanout;
1183
1184 int min_io_size;
1185 int min_io_shift;
1186 int leb_size;
1187 int half_leb_size;
1188 int leb_cnt;
1189 int max_leb_cnt;
1190 int old_leb_cnt;
1191 int ro_media;
1192
1193 atomic_long_t dirty_pg_cnt;
1194 atomic_long_t dirty_zn_cnt;
1195 atomic_long_t clean_zn_cnt;
1196
1197 long long budg_idx_growth;
1198 long long budg_data_growth;
1199 long long budg_dd_growth;
1200 long long budg_uncommitted_idx;
1201 spinlock_t space_lock;
1202 int min_idx_lebs;
1203 unsigned long long old_idx_sz;
1204 unsigned long long calc_idx_sz;
1205 struct ubifs_lp_stats lst;
1206
1207 int page_budget;
1208 int inode_budget;
1209 int dent_budget;
1210
1211 int ref_node_alsz;
1212 int mst_node_alsz;
1213 int min_idx_node_sz;
1214 int max_idx_node_sz;
1215 long long max_inode_sz;
1216 int max_znode_sz;
1217 int dead_wm;
1218 int dark_wm;
1219 int block_cnt;
1220
1221 struct ubifs_node_range ranges[UBIFS_NODE_TYPES_CNT];
1222 struct ubi_volume_desc *ubi;
1223 struct ubi_device_info di;
1224 struct ubi_volume_info vi;
1225
1226 struct rb_root orph_tree;
1227 struct list_head orph_list;
1228 struct list_head orph_new;
1229 struct ubifs_orphan *orph_cnext;
1230 struct ubifs_orphan *orph_dnext;
1231 spinlock_t orphan_lock;
1232 void *orph_buf;
1233 int new_orphans;
1234 int cmt_orphans;
1235 int tot_orphans;
1236 int max_orphans;
1237 int ohead_lnum;
1238 int ohead_offs;
1239 int no_orphs;
1240
1241 struct task_struct *bgt;
1242 char bgt_name[sizeof(BGT_NAME_PATTERN) + 9];
1243 int need_bgt;
1244 int need_wbuf_sync;
1245
1246 int gc_lnum;
1247 void *sbuf;
1248 struct list_head idx_gc;
1249 int idx_gc_cnt;
1250
1251 struct list_head infos_list;
1252 struct mutex umount_mutex;
1253 unsigned int shrinker_run_no;
1254
1255 int space_bits;
1256 int lpt_lnum_bits;
1257 int lpt_offs_bits;
1258 int lpt_spc_bits;
1259 int pcnt_bits;
1260 int lnum_bits;
1261 int nnode_sz;
1262 int pnode_sz;
1263 int ltab_sz;
1264 int lsave_sz;
1265 int pnode_cnt;
1266 int nnode_cnt;
1267 int lpt_hght;
1268 int pnodes_have;
1269
1270 struct mutex lp_mutex;
1271 int lpt_lnum;
1272 int lpt_offs;
1273 int nhead_lnum;
1274 int nhead_offs;
1275 int lpt_drty_flgs;
1276 int dirty_nn_cnt;
1277 int dirty_pn_cnt;
1278 long long lpt_sz;
1279 void *lpt_nod_buf;
1280 void *lpt_buf;
1281 struct ubifs_nnode *nroot;
1282 struct ubifs_cnode *lpt_cnext;
1283 struct ubifs_lpt_heap lpt_heap[LPROPS_HEAP_CNT];
1284 struct ubifs_lpt_heap dirty_idx;
1285 struct list_head uncat_list;
1286 struct list_head empty_list;
1287 struct list_head freeable_list;
1288 struct list_head frdi_idx_list;
1289 int freeable_cnt;
1290
1291 int ltab_lnum;
1292 int ltab_offs;
1293 struct ubifs_lpt_lprops *ltab;
1294 struct ubifs_lpt_lprops *ltab_cmt;
1295 int lsave_cnt;
1296 int lsave_lnum;
1297 int lsave_offs;
1298 int *lsave;
1299 int lscan_lnum;
1300
1301 long long rp_size;
1302 long long report_rp_size;
1303 uid_t rp_uid;
1304 gid_t rp_gid;
1305
1306 /* The below fields are used only during mounting and re-mounting */
1307 int empty;
1308 struct rb_root replay_tree;
1309 struct list_head replay_list;
1310 struct list_head replay_buds;
1311 unsigned long long cs_sqnum;
1312 unsigned long long replay_sqnum;
1313 int need_recovery;
1314 int replaying;
1315 struct list_head unclean_leb_list;
1316 struct ubifs_mst_node *rcvrd_mst_node;
1317 struct rb_root size_tree;
1318 int remounting_rw;
1319 struct ubifs_mount_opts mount_opts;
1320
1321#ifdef CONFIG_UBIFS_FS_DEBUG
1322 void *dbg_buf;
1323 struct ubifs_zbranch old_zroot;
1324 int old_zroot_level;
1325 unsigned long long old_zroot_sqnum;
1326 int failure_mode;
1327 int fail_delay;
1328 unsigned long fail_timeout;
1329 unsigned int fail_cnt;
1330 unsigned int fail_cnt_max;
1331#endif
1332};
1333
1334extern struct list_head ubifs_infos;
1335extern spinlock_t ubifs_infos_lock;
1336extern atomic_long_t ubifs_clean_zn_cnt;
1337extern struct kmem_cache *ubifs_inode_slab;
1338extern struct super_operations ubifs_super_operations;
1339extern struct address_space_operations ubifs_file_address_operations;
1340extern struct file_operations ubifs_file_operations;
1341extern struct inode_operations ubifs_file_inode_operations;
1342extern struct file_operations ubifs_dir_operations;
1343extern struct inode_operations ubifs_dir_inode_operations;
1344extern struct inode_operations ubifs_symlink_inode_operations;
1345extern struct backing_dev_info ubifs_backing_dev_info;
1346extern struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
1347
1348/* io.c */
1349int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len);
1350int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs,
1351 int dtype);
1352int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf);
1353int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
1354 int lnum, int offs);
1355int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
1356 int lnum, int offs);
1357int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum,
1358 int offs, int dtype);
1359int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
1360 int offs, int quiet);
1361void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad);
1362void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last);
1363int ubifs_io_init(struct ubifs_info *c);
1364void ubifs_pad(const struct ubifs_info *c, void *buf, int pad);
1365int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf);
1366int ubifs_bg_wbufs_sync(struct ubifs_info *c);
1367void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum);
1368int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode);
1369
1370/* scan.c */
1371struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
1372 int offs, void *sbuf);
1373void ubifs_scan_destroy(struct ubifs_scan_leb *sleb);
1374int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
1375 int offs, int quiet);
1376struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
1377 int offs, void *sbuf);
1378void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
1379 int lnum, int offs);
1380int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
1381 void *buf, int offs);
1382void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
1383 void *buf);
1384
1385/* log.c */
1386void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud);
1387void ubifs_create_buds_lists(struct ubifs_info *c);
1388int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs);
1389struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum);
1390struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum);
1391int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum);
1392int ubifs_log_end_commit(struct ubifs_info *c, int new_ltail_lnum);
1393int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum);
1394int ubifs_consolidate_log(struct ubifs_info *c);
1395
1396/* journal.c */
1397int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
1398 const struct qstr *nm, const struct inode *inode,
1399 int deletion, int xent);
1400int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
1401 const union ubifs_key *key, const void *buf, int len);
1402int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode,
1403 int last_reference);
1404int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
1405 const struct dentry *old_dentry,
1406 const struct inode *new_dir,
1407 const struct dentry *new_dentry, int sync);
1408int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1409 loff_t old_size, loff_t new_size);
1410int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1411 const struct inode *inode, const struct qstr *nm);
1412int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode1,
1413 const struct inode *inode2);
1414
1415/* budget.c */
1416int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req);
1417void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req);
1418void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
1419 struct ubifs_inode *ui);
1420int ubifs_budget_inode_op(struct ubifs_info *c, struct inode *inode,
1421 struct ubifs_budget_req *req);
1422void ubifs_release_ino_dirty(struct ubifs_info *c, struct inode *inode,
1423 struct ubifs_budget_req *req);
1424void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode,
1425 struct ubifs_budget_req *req);
1426long long ubifs_budg_get_free_space(struct ubifs_info *c);
1427int ubifs_calc_min_idx_lebs(struct ubifs_info *c);
1428void ubifs_convert_page_budget(struct ubifs_info *c);
1429long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs);
1430
1431/* find.c */
1432int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *free,
1433 int squeeze);
1434int ubifs_find_free_leb_for_idx(struct ubifs_info *c);
1435int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
1436 int min_space, int pick_free);
1437int ubifs_find_dirty_idx_leb(struct ubifs_info *c);
1438int ubifs_save_dirty_idx_lnums(struct ubifs_info *c);
1439
1440/* tnc.c */
1441int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1442 struct ubifs_znode **zn, int *n);
1443int ubifs_tnc_lookup(struct ubifs_info *c, const union ubifs_key *key,
1444 void *node);
1445int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1446 void *node, const struct qstr *nm);
1447int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1448 void *node, int *lnum, int *offs);
1449int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
1450 int offs, int len);
1451int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
1452 int old_lnum, int old_offs, int lnum, int offs, int len);
1453int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
1454 int lnum, int offs, int len, const struct qstr *nm);
1455int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key);
1456int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
1457 const struct qstr *nm);
1458int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
1459 union ubifs_key *to_key);
1460int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum);
1461struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
1462 union ubifs_key *key,
1463 const struct qstr *nm);
1464void ubifs_tnc_close(struct ubifs_info *c);
1465int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
1466 int lnum, int offs, int is_idx);
1467int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
1468 int lnum, int offs);
1469/* Shared by tnc.c for tnc_commit.c */
1470void destroy_old_idx(struct ubifs_info *c);
1471int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
1472 int lnum, int offs);
1473int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode);
1474
1475/* tnc_misc.c */
1476struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
1477 struct ubifs_znode *znode);
1478int ubifs_search_zbranch(const struct ubifs_info *c,
1479 const struct ubifs_znode *znode,
1480 const union ubifs_key *key, int *n);
1481struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode);
1482struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode);
1483long ubifs_destroy_tnc_subtree(struct ubifs_znode *zr);
1484struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
1485 struct ubifs_zbranch *zbr,
1486 struct ubifs_znode *parent, int iip);
1487int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1488 void *node);
1489
1490/* tnc_commit.c */
1491int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot);
1492int ubifs_tnc_end_commit(struct ubifs_info *c);
1493
1494/* shrinker.c */
1495int ubifs_shrinker(int nr_to_scan, gfp_t gfp_mask);
1496
1497/* commit.c */
1498int ubifs_bg_thread(void *info);
1499void ubifs_commit_required(struct ubifs_info *c);
1500void ubifs_request_bg_commit(struct ubifs_info *c);
1501int ubifs_run_commit(struct ubifs_info *c);
1502void ubifs_recovery_commit(struct ubifs_info *c);
1503int ubifs_gc_should_commit(struct ubifs_info *c);
1504void ubifs_wait_for_commit(struct ubifs_info *c);
1505
1506/* master.c */
1507int ubifs_read_master(struct ubifs_info *c);
1508int ubifs_write_master(struct ubifs_info *c);
1509
1510/* sb.c */
1511int ubifs_read_superblock(struct ubifs_info *c);
1512struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c);
1513int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup);
1514
1515/* replay.c */
1516int ubifs_validate_entry(struct ubifs_info *c,
1517 const struct ubifs_dent_node *dent);
1518int ubifs_replay_journal(struct ubifs_info *c);
1519
1520/* gc.c */
1521int ubifs_garbage_collect(struct ubifs_info *c, int anyway);
1522int ubifs_gc_start_commit(struct ubifs_info *c);
1523int ubifs_gc_end_commit(struct ubifs_info *c);
1524void ubifs_destroy_idx_gc(struct ubifs_info *c);
1525int ubifs_get_idx_gc_leb(struct ubifs_info *c);
1526int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp);
1527
1528/* orphan.c */
1529int ubifs_add_orphan(struct ubifs_info *c, ino_t inum);
1530void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum);
1531int ubifs_orphan_start_commit(struct ubifs_info *c);
1532int ubifs_orphan_end_commit(struct ubifs_info *c);
1533int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only);
1534
1535/* lpt.c */
1536int ubifs_calc_lpt_geom(struct ubifs_info *c);
1537int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
1538 int *lpt_lebs, int *big_lpt);
1539int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr);
1540struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum);
1541struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum);
1542int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1543 ubifs_lpt_scan_callback scan_cb, void *data);
1544
1545/* Shared by lpt.c for lpt_commit.c */
1546void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave);
1547void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
1548 struct ubifs_lpt_lprops *ltab);
1549void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
1550 struct ubifs_pnode *pnode);
1551void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
1552 struct ubifs_nnode *nnode);
1553struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1554 struct ubifs_nnode *parent, int iip);
1555struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1556 struct ubifs_nnode *parent, int iip);
1557int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip);
1558void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty);
1559void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode);
1560uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits);
1561struct ubifs_nnode *ubifs_first_nnode(struct ubifs_info *c, int *hght);
1562
1563/* lpt_commit.c */
1564int ubifs_lpt_start_commit(struct ubifs_info *c);
1565int ubifs_lpt_end_commit(struct ubifs_info *c);
1566int ubifs_lpt_post_commit(struct ubifs_info *c);
1567void ubifs_lpt_free(struct ubifs_info *c, int wr_only);
1568
1569/* lprops.c */
1570void ubifs_get_lprops(struct ubifs_info *c);
1571const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
1572 const struct ubifs_lprops *lp,
1573 int free, int dirty, int flags,
1574 int idx_gc_cnt);
1575void ubifs_release_lprops(struct ubifs_info *c);
1576void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *stats);
1577void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
1578 int cat);
1579void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
1580 struct ubifs_lprops *new_lprops);
1581void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops);
1582int ubifs_categorize_lprops(const struct ubifs_info *c,
1583 const struct ubifs_lprops *lprops);
1584int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
1585 int flags_set, int flags_clean, int idx_gc_cnt);
1586int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
1587 int flags_set, int flags_clean);
1588int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp);
1589const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c);
1590const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c);
1591const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c);
1592const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c);
1593
1594/* file.c */
1595int ubifs_fsync(struct file *file, struct dentry *dentry, int datasync);
1596int ubifs_setattr(struct dentry *dentry, struct iattr *attr);
1597
1598/* dir.c */
1599struct inode *ubifs_new_inode(struct ubifs_info *c, const struct inode *dir,
1600 int mode);
1601int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1602 struct kstat *stat);
1603
1604/* xattr.c */
1605int ubifs_setxattr(struct dentry *dentry, const char *name,
1606 const void *value, size_t size, int flags);
1607ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
1608 size_t size);
1609ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size);
1610int ubifs_removexattr(struct dentry *dentry, const char *name);
1611
1612/* super.c */
1613struct inode *ubifs_iget(struct super_block *sb, unsigned long inum);
1614
1615/* recovery.c */
1616int ubifs_recover_master_node(struct ubifs_info *c);
1617int ubifs_write_rcvrd_mst_node(struct ubifs_info *c);
1618struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
1619 int offs, void *sbuf, int grouped);
1620struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
1621 int offs, void *sbuf);
1622int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf);
1623int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf);
1624int ubifs_rcvry_gc_commit(struct ubifs_info *c);
1625int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1626 int deletion, loff_t new_size);
1627int ubifs_recover_size(struct ubifs_info *c);
1628void ubifs_destroy_size_tree(struct ubifs_info *c);
1629
1630/* ioctl.c */
1631long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1632void ubifs_set_inode_flags(struct inode *inode);
1633#ifdef CONFIG_COMPAT
1634long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1635#endif
1636
1637/* compressor.c */
1638int __init ubifs_compressors_init(void);
1639void __exit ubifs_compressors_exit(void);
1640void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
1641 int *compr_type);
1642int ubifs_decompress(const void *buf, int len, void *out, int *out_len,
1643 int compr_type);
1644
1645#include "debug.h"
1646#include "misc.h"
1647#include "key.h"
1648
1649#endif /* !__UBIFS_H__ */
diff --git a/fs/ubifs/xattr.c b/fs/ubifs/xattr.c
new file mode 100644
index 000000000000..1388a078e1a9
--- /dev/null
+++ b/fs/ubifs/xattr.c
@@ -0,0 +1,581 @@
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements UBIFS extended attributes support.
25 *
26 * Extended attributes are implemented as regular inodes with attached data,
27 * which limits extended attribute size to UBIFS block size (4KiB). Names of
28 * extended attributes are described by extended attribute entries (xentries),
29 * which are almost identical to directory entries, but have different key type.
30 *
31 * In other words, the situation with extended attributes is very similar to
32 * directories. Indeed, any inode (but of course not xattr inodes) may have a
33 * number of associated xentries, just like directory inodes have associated
34 * directory entries. Extended attribute entries store the name of the extended
35 * attribute, the host inode number, and the extended attribute inode number.
36 * Similarly, direntries store the name, the parent and the target inode
37 * numbers. Thus, most of the common UBIFS mechanisms may be re-used for
38 * extended attributes.
39 *
40 * The number of extended attributes is not limited, but there is Linux
41 * limitation on the maximum possible size of the list of all extended
42 * attributes associated with an inode (%XATTR_LIST_MAX), so UBIFS makes sure
43 * the sum of all extended attribute names of the inode does not exceed that
44 * limit.
45 *
46 * Extended attributes are synchronous, which means they are written to the
47 * flash media synchronously and there is no write-back for extended attribute
48 * inodes. The extended attribute values are not stored in compressed form on
49 * the media.
50 *
51 * Since extended attributes are represented by regular inodes, they are cached
52 * in the VFS inode cache. The xentries are cached in the LNC cache (see
53 * tnc.c).
54 *
55 * ACL support is not implemented.
56 */
57
58#include <linux/xattr.h>
59#include <linux/posix_acl_xattr.h>
60#include "ubifs.h"
61
62/*
63 * Limit the number of extended attributes per inode so that the total size
64 * (xattr_size) is guaranteeded to fit in an 'unsigned int'.
65 */
66#define MAX_XATTRS_PER_INODE 65535
67
68/*
69 * Extended attribute type constants.
70 *
71 * USER_XATTR: user extended attribute ("user.*")
72 * TRUSTED_XATTR: trusted extended attribute ("trusted.*)
73 * SECURITY_XATTR: security extended attribute ("security.*")
74 */
75enum {
76 USER_XATTR,
77 TRUSTED_XATTR,
78 SECURITY_XATTR,
79};
80
81static struct inode_operations none_inode_operations;
82static struct address_space_operations none_address_operations;
83static struct file_operations none_file_operations;
84
85/**
86 * create_xattr - create an extended attribute.
87 * @c: UBIFS file-system description object
88 * @host: host inode
89 * @nm: extended attribute name
90 * @value: extended attribute value
91 * @size: size of extended attribute value
92 *
93 * This is a helper function which creates an extended attribute of name @nm
94 * and value @value for inode @host. The host inode is also updated on flash
95 * because the ctime and extended attribute accounting data changes. This
96 * function returns zero in case of success and a negative error code in case
97 * of failure.
98 */
99static int create_xattr(struct ubifs_info *c, struct inode *host,
100 const struct qstr *nm, const void *value, int size)
101{
102 int err;
103 struct inode *inode;
104 struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
105 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
106 .new_ino_d = size, .dirtied_ino = 1,
107 .dirtied_ino_d = host_ui->data_len};
108
109 if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE)
110 return -ENOSPC;
111 /*
112 * Linux limits the maximum size of the extended attribute names list
113 * to %XATTR_LIST_MAX. This means we should not allow creating more*
114 * extended attributes if the name list becomes larger. This limitation
115 * is artificial for UBIFS, though.
116 */
117 if (host_ui->xattr_names + host_ui->xattr_cnt +
118 nm->len + 1 > XATTR_LIST_MAX)
119 return -ENOSPC;
120
121 err = ubifs_budget_space(c, &req);
122 if (err)
123 return err;
124
125 inode = ubifs_new_inode(c, host, S_IFREG | S_IRWXUGO);
126 if (IS_ERR(inode)) {
127 err = PTR_ERR(inode);
128 goto out_budg;
129 }
130
131 mutex_lock(&host_ui->ui_mutex);
132 /* Re-define all operations to be "nothing" */
133 inode->i_mapping->a_ops = &none_address_operations;
134 inode->i_op = &none_inode_operations;
135 inode->i_fop = &none_file_operations;
136
137 inode->i_flags |= S_SYNC | S_NOATIME | S_NOCMTIME | S_NOQUOTA;
138 ui = ubifs_inode(inode);
139 ui->xattr = 1;
140 ui->flags |= UBIFS_XATTR_FL;
141 ui->data = kmalloc(size, GFP_NOFS);
142 if (!ui->data) {
143 err = -ENOMEM;
144 goto out_unlock;
145 }
146
147 memcpy(ui->data, value, size);
148 host->i_ctime = ubifs_current_time(host);
149 host_ui->xattr_cnt += 1;
150 host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
151 host_ui->xattr_size += CALC_XATTR_BYTES(size);
152 host_ui->xattr_names += nm->len;
153
154 /*
155 * We do not use i_size_write() because nobody can race with us as we
156 * are holding host @host->i_mutex - every xattr operation for this
157 * inode is serialized by it.
158 */
159 inode->i_size = ui->ui_size = size;
160 ui->data_len = size;
161 err = ubifs_jnl_update(c, host, nm, inode, 0, 1);
162 if (err)
163 goto out_cancel;
164 mutex_unlock(&host_ui->ui_mutex);
165
166 ubifs_release_budget(c, &req);
167 insert_inode_hash(inode);
168 iput(inode);
169 return 0;
170
171out_cancel:
172 host_ui->xattr_cnt -= 1;
173 host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
174 host_ui->xattr_size -= CALC_XATTR_BYTES(size);
175out_unlock:
176 mutex_unlock(&host_ui->ui_mutex);
177 make_bad_inode(inode);
178 iput(inode);
179out_budg:
180 ubifs_release_budget(c, &req);
181 return err;
182}
183
184/**
185 * change_xattr - change an extended attribute.
186 * @c: UBIFS file-system description object
187 * @host: host inode
188 * @inode: extended attribute inode
189 * @value: extended attribute value
190 * @size: size of extended attribute value
191 *
192 * This helper function changes the value of extended attribute @inode with new
193 * data from @value. Returns zero in case of success and a negative error code
194 * in case of failure.
195 */
196static int change_xattr(struct ubifs_info *c, struct inode *host,
197 struct inode *inode, const void *value, int size)
198{
199 int err;
200 struct ubifs_inode *host_ui = ubifs_inode(host);
201 struct ubifs_inode *ui = ubifs_inode(inode);
202 struct ubifs_budget_req req = { .dirtied_ino = 2,
203 .dirtied_ino_d = size + host_ui->data_len };
204
205 ubifs_assert(ui->data_len == inode->i_size);
206 err = ubifs_budget_space(c, &req);
207 if (err)
208 return err;
209
210 mutex_lock(&host_ui->ui_mutex);
211 host->i_ctime = ubifs_current_time(host);
212 host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
213 host_ui->xattr_size += CALC_XATTR_BYTES(size);
214
215 kfree(ui->data);
216 ui->data = kmalloc(size, GFP_NOFS);
217 if (!ui->data) {
218 err = -ENOMEM;
219 goto out_unlock;
220 }
221
222 memcpy(ui->data, value, size);
223 inode->i_size = ui->ui_size = size;
224 ui->data_len = size;
225
226 /*
227 * It is important to write the host inode after the xattr inode
228 * because if the host inode gets synchronized (via 'fsync()'), then
229 * the extended attribute inode gets synchronized, because it goes
230 * before the host inode in the write-buffer.
231 */
232 err = ubifs_jnl_change_xattr(c, inode, host);
233 if (err)
234 goto out_cancel;
235 mutex_unlock(&host_ui->ui_mutex);
236
237 ubifs_release_budget(c, &req);
238 return 0;
239
240out_cancel:
241 host_ui->xattr_size -= CALC_XATTR_BYTES(size);
242 host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
243 make_bad_inode(inode);
244out_unlock:
245 mutex_unlock(&host_ui->ui_mutex);
246 ubifs_release_budget(c, &req);
247 return err;
248}
249
250/**
251 * check_namespace - check extended attribute name-space.
252 * @nm: extended attribute name
253 *
254 * This function makes sure the extended attribute name belongs to one of the
255 * supported extended attribute name-spaces. Returns name-space index in case
256 * of success and a negative error code in case of failure.
257 */
258static int check_namespace(const struct qstr *nm)
259{
260 int type;
261
262 if (nm->len > UBIFS_MAX_NLEN)
263 return -ENAMETOOLONG;
264
265 if (!strncmp(nm->name, XATTR_TRUSTED_PREFIX,
266 XATTR_TRUSTED_PREFIX_LEN)) {
267 if (nm->name[sizeof(XATTR_TRUSTED_PREFIX) - 1] == '\0')
268 return -EINVAL;
269 type = TRUSTED_XATTR;
270 } else if (!strncmp(nm->name, XATTR_USER_PREFIX,
271 XATTR_USER_PREFIX_LEN)) {
272 if (nm->name[XATTR_USER_PREFIX_LEN] == '\0')
273 return -EINVAL;
274 type = USER_XATTR;
275 } else if (!strncmp(nm->name, XATTR_SECURITY_PREFIX,
276 XATTR_SECURITY_PREFIX_LEN)) {
277 if (nm->name[sizeof(XATTR_SECURITY_PREFIX) - 1] == '\0')
278 return -EINVAL;
279 type = SECURITY_XATTR;
280 } else
281 return -EOPNOTSUPP;
282
283 return type;
284}
285
286static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
287{
288 struct inode *inode;
289
290 inode = ubifs_iget(c->vfs_sb, inum);
291 if (IS_ERR(inode)) {
292 ubifs_err("dead extended attribute entry, error %d",
293 (int)PTR_ERR(inode));
294 return inode;
295 }
296 if (ubifs_inode(inode)->xattr)
297 return inode;
298 ubifs_err("corrupt extended attribute entry");
299 iput(inode);
300 return ERR_PTR(-EINVAL);
301}
302
303int ubifs_setxattr(struct dentry *dentry, const char *name,
304 const void *value, size_t size, int flags)
305{
306 struct inode *inode, *host = dentry->d_inode;
307 struct ubifs_info *c = host->i_sb->s_fs_info;
308 struct qstr nm = { .name = name, .len = strlen(name) };
309 struct ubifs_dent_node *xent;
310 union ubifs_key key;
311 int err, type;
312
313 dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
314 host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
315
316 if (size > UBIFS_MAX_INO_DATA)
317 return -ERANGE;
318
319 type = check_namespace(&nm);
320 if (type < 0)
321 return type;
322
323 xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
324 if (!xent)
325 return -ENOMEM;
326
327 /*
328 * The extended attribute entries are stored in LNC, so multiple
329 * look-ups do not involve reading the flash.
330 */
331 xent_key_init(c, &key, host->i_ino, &nm);
332 err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
333 if (err) {
334 if (err != -ENOENT)
335 goto out_free;
336
337 if (flags & XATTR_REPLACE)
338 /* We are asked not to create the xattr */
339 err = -ENODATA;
340 else
341 err = create_xattr(c, host, &nm, value, size);
342 goto out_free;
343 }
344
345 if (flags & XATTR_CREATE) {
346 /* We are asked not to replace the xattr */
347 err = -EEXIST;
348 goto out_free;
349 }
350
351 inode = iget_xattr(c, le64_to_cpu(xent->inum));
352 if (IS_ERR(inode)) {
353 err = PTR_ERR(inode);
354 goto out_free;
355 }
356
357 err = change_xattr(c, host, inode, value, size);
358 iput(inode);
359
360out_free:
361 kfree(xent);
362 return err;
363}
364
365ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
366 size_t size)
367{
368 struct inode *inode, *host = dentry->d_inode;
369 struct ubifs_info *c = host->i_sb->s_fs_info;
370 struct qstr nm = { .name = name, .len = strlen(name) };
371 struct ubifs_inode *ui;
372 struct ubifs_dent_node *xent;
373 union ubifs_key key;
374 int err;
375
376 dbg_gen("xattr '%s', ino %lu ('%.*s'), buf size %zd", name,
377 host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
378
379 err = check_namespace(&nm);
380 if (err < 0)
381 return err;
382
383 xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
384 if (!xent)
385 return -ENOMEM;
386
387 mutex_lock(&host->i_mutex);
388 xent_key_init(c, &key, host->i_ino, &nm);
389 err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
390 if (err) {
391 if (err == -ENOENT)
392 err = -ENODATA;
393 goto out_unlock;
394 }
395
396 inode = iget_xattr(c, le64_to_cpu(xent->inum));
397 if (IS_ERR(inode)) {
398 err = PTR_ERR(inode);
399 goto out_unlock;
400 }
401
402 ui = ubifs_inode(inode);
403 ubifs_assert(inode->i_size == ui->data_len);
404 ubifs_assert(ubifs_inode(host)->xattr_size > ui->data_len);
405
406 if (buf) {
407 /* If @buf is %NULL we are supposed to return the length */
408 if (ui->data_len > size) {
409 dbg_err("buffer size %zd, xattr len %d",
410 size, ui->data_len);
411 err = -ERANGE;
412 goto out_iput;
413 }
414
415 memcpy(buf, ui->data, ui->data_len);
416 }
417 err = ui->data_len;
418
419out_iput:
420 iput(inode);
421out_unlock:
422 mutex_unlock(&host->i_mutex);
423 kfree(xent);
424 return err;
425}
426
427ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size)
428{
429 union ubifs_key key;
430 struct inode *host = dentry->d_inode;
431 struct ubifs_info *c = host->i_sb->s_fs_info;
432 struct ubifs_inode *host_ui = ubifs_inode(host);
433 struct ubifs_dent_node *xent, *pxent = NULL;
434 int err, len, written = 0;
435 struct qstr nm = { .name = NULL };
436
437 dbg_gen("ino %lu ('%.*s'), buffer size %zd", host->i_ino,
438 dentry->d_name.len, dentry->d_name.name, size);
439
440 len = host_ui->xattr_names + host_ui->xattr_cnt;
441 if (!buffer)
442 /*
443 * We should return the minimum buffer size which will fit a
444 * null-terminated list of all the extended attribute names.
445 */
446 return len;
447
448 if (len > size)
449 return -ERANGE;
450
451 lowest_xent_key(c, &key, host->i_ino);
452
453 mutex_lock(&host->i_mutex);
454 while (1) {
455 int type;
456
457 xent = ubifs_tnc_next_ent(c, &key, &nm);
458 if (unlikely(IS_ERR(xent))) {
459 err = PTR_ERR(xent);
460 break;
461 }
462
463 nm.name = xent->name;
464 nm.len = le16_to_cpu(xent->nlen);
465
466 type = check_namespace(&nm);
467 if (unlikely(type < 0)) {
468 err = type;
469 break;
470 }
471
472 /* Show trusted namespace only for "power" users */
473 if (type != TRUSTED_XATTR || capable(CAP_SYS_ADMIN)) {
474 memcpy(buffer + written, nm.name, nm.len + 1);
475 written += nm.len + 1;
476 }
477
478 kfree(pxent);
479 pxent = xent;
480 key_read(c, &xent->key, &key);
481 }
482 mutex_unlock(&host->i_mutex);
483
484 kfree(pxent);
485 if (err != -ENOENT) {
486 ubifs_err("cannot find next direntry, error %d", err);
487 return err;
488 }
489
490 ubifs_assert(written <= size);
491 return written;
492}
493
494static int remove_xattr(struct ubifs_info *c, struct inode *host,
495 struct inode *inode, const struct qstr *nm)
496{
497 int err;
498 struct ubifs_inode *host_ui = ubifs_inode(host);
499 struct ubifs_inode *ui = ubifs_inode(inode);
500 struct ubifs_budget_req req = { .dirtied_ino = 1, .mod_dent = 1,
501 .dirtied_ino_d = host_ui->data_len };
502
503 ubifs_assert(ui->data_len == inode->i_size);
504
505 err = ubifs_budget_space(c, &req);
506 if (err)
507 return err;
508
509 mutex_lock(&host_ui->ui_mutex);
510 host->i_ctime = ubifs_current_time(host);
511 host_ui->xattr_cnt -= 1;
512 host_ui->xattr_size -= CALC_DENT_SIZE(nm->len);
513 host_ui->xattr_size -= CALC_XATTR_BYTES(ui->data_len);
514 host_ui->xattr_names -= nm->len;
515
516 err = ubifs_jnl_delete_xattr(c, host, inode, nm);
517 if (err)
518 goto out_cancel;
519 mutex_unlock(&host_ui->ui_mutex);
520
521 ubifs_release_budget(c, &req);
522 return 0;
523
524out_cancel:
525 host_ui->xattr_cnt += 1;
526 host_ui->xattr_size += CALC_DENT_SIZE(nm->len);
527 host_ui->xattr_size += CALC_XATTR_BYTES(ui->data_len);
528 mutex_unlock(&host_ui->ui_mutex);
529 ubifs_release_budget(c, &req);
530 make_bad_inode(inode);
531 return err;
532}
533
534int ubifs_removexattr(struct dentry *dentry, const char *name)
535{
536 struct inode *inode, *host = dentry->d_inode;
537 struct ubifs_info *c = host->i_sb->s_fs_info;
538 struct qstr nm = { .name = name, .len = strlen(name) };
539 struct ubifs_dent_node *xent;
540 union ubifs_key key;
541 int err;
542
543 dbg_gen("xattr '%s', ino %lu ('%.*s')", name,
544 host->i_ino, dentry->d_name.len, dentry->d_name.name);
545 ubifs_assert(mutex_is_locked(&host->i_mutex));
546
547 err = check_namespace(&nm);
548 if (err < 0)
549 return err;
550
551 xent = kmalloc(UBIFS_MAX_XENT_NODE_SZ, GFP_NOFS);
552 if (!xent)
553 return -ENOMEM;
554
555 xent_key_init(c, &key, host->i_ino, &nm);
556 err = ubifs_tnc_lookup_nm(c, &key, xent, &nm);
557 if (err) {
558 if (err == -ENOENT)
559 err = -ENODATA;
560 goto out_free;
561 }
562
563 inode = iget_xattr(c, le64_to_cpu(xent->inum));
564 if (IS_ERR(inode)) {
565 err = PTR_ERR(inode);
566 goto out_free;
567 }
568
569 ubifs_assert(inode->i_nlink == 1);
570 inode->i_nlink = 0;
571 err = remove_xattr(c, host, inode, &nm);
572 if (err)
573 inode->i_nlink = 1;
574
575 /* If @i_nlink is 0, 'iput()' will delete the inode */
576 iput(inode);
577
578out_free:
579 kfree(xent);
580 return err;
581}
diff --git a/include/acpi/acconfig.h b/include/acpi/acconfig.h
index 28fe8bae1037..4eb75a88795a 100644
--- a/include/acpi/acconfig.h
+++ b/include/acpi/acconfig.h
@@ -63,7 +63,7 @@
63 63
64/* Current ACPICA subsystem version in YYYYMMDD format */ 64/* Current ACPICA subsystem version in YYYYMMDD format */
65 65
66#define ACPI_CA_VERSION 0x20080321 66#define ACPI_CA_VERSION 0x20080609
67 67
68/* 68/*
69 * OS name, used for the _OS object. The _OS object is essentially obsolete, 69 * OS name, used for the _OS object. The _OS object is essentially obsolete,
diff --git a/include/acpi/acdisasm.h b/include/acpi/acdisasm.h
index 788f88782012..f53faca8ec80 100644
--- a/include/acpi/acdisasm.h
+++ b/include/acpi/acdisasm.h
@@ -162,6 +162,7 @@ extern struct acpi_dmtable_info acpi_dm_table_info_dmar_hdr[];
162extern struct acpi_dmtable_info acpi_dm_table_info_dmar_scope[]; 162extern struct acpi_dmtable_info acpi_dm_table_info_dmar_scope[];
163extern struct acpi_dmtable_info acpi_dm_table_info_dmar0[]; 163extern struct acpi_dmtable_info acpi_dm_table_info_dmar0[];
164extern struct acpi_dmtable_info acpi_dm_table_info_dmar1[]; 164extern struct acpi_dmtable_info acpi_dm_table_info_dmar1[];
165extern struct acpi_dmtable_info acpi_dm_table_info_dmar2[];
165extern struct acpi_dmtable_info acpi_dm_table_info_ecdt[]; 166extern struct acpi_dmtable_info acpi_dm_table_info_ecdt[];
166extern struct acpi_dmtable_info acpi_dm_table_info_einj[]; 167extern struct acpi_dmtable_info acpi_dm_table_info_einj[];
167extern struct acpi_dmtable_info acpi_dm_table_info_einj0[]; 168extern struct acpi_dmtable_info acpi_dm_table_info_einj0[];
diff --git a/include/acpi/acdispat.h b/include/acpi/acdispat.h
index 910f018d92c7..21a73a105d0a 100644
--- a/include/acpi/acdispat.h
+++ b/include/acpi/acdispat.h
@@ -221,7 +221,7 @@ acpi_ds_method_error(acpi_status status, struct acpi_walk_state *walk_state);
221 * dsinit 221 * dsinit
222 */ 222 */
223acpi_status 223acpi_status
224acpi_ds_initialize_objects(acpi_native_uint table_index, 224acpi_ds_initialize_objects(u32 table_index,
225 struct acpi_namespace_node *start_node); 225 struct acpi_namespace_node *start_node);
226 226
227/* 227/*
diff --git a/include/acpi/acexcep.h b/include/acpi/acexcep.h
index 1f591171bf31..e5a890ffeb02 100644
--- a/include/acpi/acexcep.h
+++ b/include/acpi/acexcep.h
@@ -108,8 +108,9 @@
108#define AE_BAD_HEX_CONSTANT (acpi_status) (0x0007 | AE_CODE_PROGRAMMER) 108#define AE_BAD_HEX_CONSTANT (acpi_status) (0x0007 | AE_CODE_PROGRAMMER)
109#define AE_BAD_OCTAL_CONSTANT (acpi_status) (0x0008 | AE_CODE_PROGRAMMER) 109#define AE_BAD_OCTAL_CONSTANT (acpi_status) (0x0008 | AE_CODE_PROGRAMMER)
110#define AE_BAD_DECIMAL_CONSTANT (acpi_status) (0x0009 | AE_CODE_PROGRAMMER) 110#define AE_BAD_DECIMAL_CONSTANT (acpi_status) (0x0009 | AE_CODE_PROGRAMMER)
111#define AE_MISSING_ARGUMENTS (acpi_status) (0x000A | AE_CODE_PROGRAMMER)
111 112
112#define AE_CODE_PGM_MAX 0x0009 113#define AE_CODE_PGM_MAX 0x000A
113 114
114/* 115/*
115 * Acpi table exceptions 116 * Acpi table exceptions
@@ -225,6 +226,7 @@ char const *acpi_gbl_exception_names_env[] = {
225}; 226};
226 227
227char const *acpi_gbl_exception_names_pgm[] = { 228char const *acpi_gbl_exception_names_pgm[] = {
229 NULL,
228 "AE_BAD_PARAMETER", 230 "AE_BAD_PARAMETER",
229 "AE_BAD_CHARACTER", 231 "AE_BAD_CHARACTER",
230 "AE_BAD_PATHNAME", 232 "AE_BAD_PATHNAME",
@@ -233,10 +235,12 @@ char const *acpi_gbl_exception_names_pgm[] = {
233 "AE_ALIGNMENT", 235 "AE_ALIGNMENT",
234 "AE_BAD_HEX_CONSTANT", 236 "AE_BAD_HEX_CONSTANT",
235 "AE_BAD_OCTAL_CONSTANT", 237 "AE_BAD_OCTAL_CONSTANT",
236 "AE_BAD_DECIMAL_CONSTANT" 238 "AE_BAD_DECIMAL_CONSTANT",
239 "AE_MISSING_ARGUMENTS"
237}; 240};
238 241
239char const *acpi_gbl_exception_names_tbl[] = { 242char const *acpi_gbl_exception_names_tbl[] = {
243 NULL,
240 "AE_BAD_SIGNATURE", 244 "AE_BAD_SIGNATURE",
241 "AE_BAD_HEADER", 245 "AE_BAD_HEADER",
242 "AE_BAD_CHECKSUM", 246 "AE_BAD_CHECKSUM",
@@ -246,6 +250,7 @@ char const *acpi_gbl_exception_names_tbl[] = {
246}; 250};
247 251
248char const *acpi_gbl_exception_names_aml[] = { 252char const *acpi_gbl_exception_names_aml[] = {
253 NULL,
249 "AE_AML_ERROR", 254 "AE_AML_ERROR",
250 "AE_AML_PARSE", 255 "AE_AML_PARSE",
251 "AE_AML_BAD_OPCODE", 256 "AE_AML_BAD_OPCODE",
@@ -283,6 +288,7 @@ char const *acpi_gbl_exception_names_aml[] = {
283}; 288};
284 289
285char const *acpi_gbl_exception_names_ctrl[] = { 290char const *acpi_gbl_exception_names_ctrl[] = {
291 NULL,
286 "AE_CTRL_RETURN_VALUE", 292 "AE_CTRL_RETURN_VALUE",
287 "AE_CTRL_PENDING", 293 "AE_CTRL_PENDING",
288 "AE_CTRL_TERMINATE", 294 "AE_CTRL_TERMINATE",
diff --git a/include/acpi/acglobal.h b/include/acpi/acglobal.h
index 74ad971241db..15dda46b70d1 100644
--- a/include/acpi/acglobal.h
+++ b/include/acpi/acglobal.h
@@ -140,7 +140,7 @@ ACPI_EXTERN u32 acpi_gbl_trace_flags;
140 */ 140 */
141ACPI_EXTERN struct acpi_internal_rsdt acpi_gbl_root_table_list; 141ACPI_EXTERN struct acpi_internal_rsdt acpi_gbl_root_table_list;
142ACPI_EXTERN struct acpi_table_fadt acpi_gbl_FADT; 142ACPI_EXTERN struct acpi_table_fadt acpi_gbl_FADT;
143extern acpi_native_uint acpi_gbl_permanent_mmap; 143extern u8 acpi_gbl_permanent_mmap;
144 144
145/* These addresses are calculated from FADT address values */ 145/* These addresses are calculated from FADT address values */
146 146
diff --git a/include/acpi/achware.h b/include/acpi/achware.h
index d4fb9bbc903c..97a72b193276 100644
--- a/include/acpi/achware.h
+++ b/include/acpi/achware.h
@@ -87,6 +87,8 @@ acpi_status acpi_hw_clear_acpi_status(void);
87/* 87/*
88 * hwgpe - GPE support 88 * hwgpe - GPE support
89 */ 89 */
90acpi_status acpi_hw_low_disable_gpe(struct acpi_gpe_event_info *gpe_event_info);
91
90acpi_status 92acpi_status
91acpi_hw_write_gpe_enable_reg(struct acpi_gpe_event_info *gpe_event_info); 93acpi_hw_write_gpe_enable_reg(struct acpi_gpe_event_info *gpe_event_info);
92 94
@@ -100,11 +102,9 @@ acpi_status
100acpi_hw_clear_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info, 102acpi_hw_clear_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
101 struct acpi_gpe_block_info *gpe_block); 103 struct acpi_gpe_block_info *gpe_block);
102 104
103#ifdef ACPI_FUTURE_USAGE
104acpi_status 105acpi_status
105acpi_hw_get_gpe_status(struct acpi_gpe_event_info *gpe_event_info, 106acpi_hw_get_gpe_status(struct acpi_gpe_event_info *gpe_event_info,
106 acpi_event_status * event_status); 107 acpi_event_status * event_status);
107#endif /* ACPI_FUTURE_USAGE */
108 108
109acpi_status acpi_hw_disable_all_gpes(void); 109acpi_status acpi_hw_disable_all_gpes(void);
110 110
diff --git a/include/acpi/acinterp.h b/include/acpi/acinterp.h
index e249ce5d3300..e8db7a3143a5 100644
--- a/include/acpi/acinterp.h
+++ b/include/acpi/acinterp.h
@@ -366,10 +366,7 @@ void acpi_ex_dump_operand(union acpi_operand_object *obj_desc, u32 depth);
366 366
367void 367void
368acpi_ex_dump_operands(union acpi_operand_object **operands, 368acpi_ex_dump_operands(union acpi_operand_object **operands,
369 acpi_interpreter_mode interpreter_mode, 369 const char *opcode_name, u32 num_opcodes);
370 char *ident,
371 u32 num_levels,
372 char *note, char *module_name, u32 line_number);
373 370
374#ifdef ACPI_FUTURE_USAGE 371#ifdef ACPI_FUTURE_USAGE
375void 372void
diff --git a/include/acpi/aclocal.h b/include/acpi/aclocal.h
index c5cdc32ac2f8..b221c8583ddd 100644
--- a/include/acpi/aclocal.h
+++ b/include/acpi/aclocal.h
@@ -98,8 +98,8 @@ union acpi_parse_object;
98 98
99static char *acpi_gbl_mutex_names[ACPI_NUM_MUTEX] = { 99static char *acpi_gbl_mutex_names[ACPI_NUM_MUTEX] = {
100 "ACPI_MTX_Interpreter", 100 "ACPI_MTX_Interpreter",
101 "ACPI_MTX_Tables",
102 "ACPI_MTX_Namespace", 101 "ACPI_MTX_Namespace",
102 "ACPI_MTX_Tables",
103 "ACPI_MTX_Events", 103 "ACPI_MTX_Events",
104 "ACPI_MTX_Caches", 104 "ACPI_MTX_Caches",
105 "ACPI_MTX_Memory", 105 "ACPI_MTX_Memory",
@@ -282,8 +282,8 @@ struct acpi_predefined_names {
282/* Info structure used to convert external<->internal namestrings */ 282/* Info structure used to convert external<->internal namestrings */
283 283
284struct acpi_namestring_info { 284struct acpi_namestring_info {
285 char *external_name; 285 const char *external_name;
286 char *next_external_char; 286 const char *next_external_char;
287 char *internal_name; 287 char *internal_name;
288 u32 length; 288 u32 length;
289 u32 num_segments; 289 u32 num_segments;
diff --git a/include/acpi/acmacros.h b/include/acpi/acmacros.h
index fb41a3b802fc..57ab9e9d7593 100644
--- a/include/acpi/acmacros.h
+++ b/include/acpi/acmacros.h
@@ -80,12 +80,12 @@
80 */ 80 */
81#define ACPI_CAST_PTR(t, p) ((t *) (acpi_uintptr_t) (p)) 81#define ACPI_CAST_PTR(t, p) ((t *) (acpi_uintptr_t) (p))
82#define ACPI_CAST_INDIRECT_PTR(t, p) ((t **) (acpi_uintptr_t) (p)) 82#define ACPI_CAST_INDIRECT_PTR(t, p) ((t **) (acpi_uintptr_t) (p))
83#define ACPI_ADD_PTR(t,a,b) ACPI_CAST_PTR (t, (ACPI_CAST_PTR (u8,(a)) + (acpi_native_uint)(b))) 83#define ACPI_ADD_PTR(t, a, b) ACPI_CAST_PTR (t, (ACPI_CAST_PTR (u8,(a)) + (acpi_size)(b)))
84#define ACPI_PTR_DIFF(a,b) (acpi_native_uint) (ACPI_CAST_PTR (u8,(a)) - ACPI_CAST_PTR (u8,(b))) 84#define ACPI_PTR_DIFF(a, b) (acpi_size) (ACPI_CAST_PTR (u8,(a)) - ACPI_CAST_PTR (u8,(b)))
85 85
86/* Pointer/Integer type conversions */ 86/* Pointer/Integer type conversions */
87 87
88#define ACPI_TO_POINTER(i) ACPI_ADD_PTR (void,(void *) NULL,(acpi_native_uint) i) 88#define ACPI_TO_POINTER(i) ACPI_ADD_PTR (void, (void *) NULL, (acpi_size) i)
89#define ACPI_TO_INTEGER(p) ACPI_PTR_DIFF (p,(void *) NULL) 89#define ACPI_TO_INTEGER(p) ACPI_PTR_DIFF (p,(void *) NULL)
90#define ACPI_OFFSET(d,f) (acpi_size) ACPI_PTR_DIFF (&(((d *)0)->f),(void *) NULL) 90#define ACPI_OFFSET(d,f) (acpi_size) ACPI_PTR_DIFF (&(((d *)0)->f),(void *) NULL)
91#define ACPI_PHYSADDR_TO_PTR(i) ACPI_TO_POINTER(i) 91#define ACPI_PHYSADDR_TO_PTR(i) ACPI_TO_POINTER(i)
@@ -296,22 +296,22 @@ struct acpi_integer_overlay {
296/* 296/*
297 * Rounding macros (Power of two boundaries only) 297 * Rounding macros (Power of two boundaries only)
298 */ 298 */
299#define ACPI_ROUND_DOWN(value,boundary) (((acpi_native_uint)(value)) & \ 299#define ACPI_ROUND_DOWN(value, boundary) (((acpi_size)(value)) & \
300 (~(((acpi_native_uint) boundary)-1))) 300 (~(((acpi_size) boundary)-1)))
301 301
302#define ACPI_ROUND_UP(value,boundary) ((((acpi_native_uint)(value)) + \ 302#define ACPI_ROUND_UP(value, boundary) ((((acpi_size)(value)) + \
303 (((acpi_native_uint) boundary)-1)) & \ 303 (((acpi_size) boundary)-1)) & \
304 (~(((acpi_native_uint) boundary)-1))) 304 (~(((acpi_size) boundary)-1)))
305 305
306/* Note: sizeof(acpi_native_uint) evaluates to either 2, 4, or 8 */ 306/* Note: sizeof(acpi_size) evaluates to either 4 or 8 (32- vs 64-bit mode) */
307 307
308#define ACPI_ROUND_DOWN_TO_32BIT(a) ACPI_ROUND_DOWN(a,4) 308#define ACPI_ROUND_DOWN_TO_32BIT(a) ACPI_ROUND_DOWN(a,4)
309#define ACPI_ROUND_DOWN_TO_64BIT(a) ACPI_ROUND_DOWN(a,8) 309#define ACPI_ROUND_DOWN_TO_64BIT(a) ACPI_ROUND_DOWN(a,8)
310#define ACPI_ROUND_DOWN_TO_NATIVE_WORD(a) ACPI_ROUND_DOWN(a,sizeof(acpi_native_uint)) 310#define ACPI_ROUND_DOWN_TO_NATIVE_WORD(a) ACPI_ROUND_DOWN(a,sizeof(acpi_size))
311 311
312#define ACPI_ROUND_UP_TO_32BIT(a) ACPI_ROUND_UP(a,4) 312#define ACPI_ROUND_UP_TO_32BIT(a) ACPI_ROUND_UP(a,4)
313#define ACPI_ROUND_UP_TO_64BIT(a) ACPI_ROUND_UP(a,8) 313#define ACPI_ROUND_UP_TO_64BIT(a) ACPI_ROUND_UP(a,8)
314#define ACPI_ROUND_UP_TO_NATIVE_WORD(a) ACPI_ROUND_UP(a,sizeof(acpi_native_uint)) 314#define ACPI_ROUND_UP_TO_NATIVE_WORD(a) ACPI_ROUND_UP(a,sizeof(acpi_size))
315 315
316#define ACPI_ROUND_BITS_UP_TO_BYTES(a) ACPI_DIV_8((a) + 7) 316#define ACPI_ROUND_BITS_UP_TO_BYTES(a) ACPI_DIV_8((a) + 7)
317#define ACPI_ROUND_BITS_DOWN_TO_BYTES(a) ACPI_DIV_8((a)) 317#define ACPI_ROUND_BITS_DOWN_TO_BYTES(a) ACPI_DIV_8((a))
@@ -322,7 +322,7 @@ struct acpi_integer_overlay {
322 322
323#define ACPI_ROUND_UP_TO(value,boundary) (((value) + ((boundary)-1)) / (boundary)) 323#define ACPI_ROUND_UP_TO(value,boundary) (((value) + ((boundary)-1)) / (boundary))
324 324
325#define ACPI_IS_MISALIGNED(value) (((acpi_native_uint)value) & (sizeof(acpi_native_uint)-1)) 325#define ACPI_IS_MISALIGNED(value) (((acpi_size)value) & (sizeof(acpi_size)-1))
326 326
327/* 327/*
328 * Bitmask creation 328 * Bitmask creation
@@ -414,7 +414,7 @@ struct acpi_integer_overlay {
414 * error messages. The __FILE__ macro is not very useful for this, because it 414 * error messages. The __FILE__ macro is not very useful for this, because it
415 * often includes the entire pathname to the module 415 * often includes the entire pathname to the module
416 */ 416 */
417#define ACPI_MODULE_NAME(name) static char ACPI_UNUSED_VAR *_acpi_module_name = name; 417#define ACPI_MODULE_NAME(name) static const char ACPI_UNUSED_VAR _acpi_module_name[] = name;
418#else 418#else
419#define ACPI_MODULE_NAME(name) 419#define ACPI_MODULE_NAME(name)
420#endif 420#endif
@@ -467,19 +467,17 @@ struct acpi_integer_overlay {
467/* 467/*
468 * If ACPI_GET_FUNCTION_NAME was not defined in the compiler-dependent header, 468 * If ACPI_GET_FUNCTION_NAME was not defined in the compiler-dependent header,
469 * define it now. This is the case where there the compiler does not support 469 * define it now. This is the case where there the compiler does not support
470 * a __FUNCTION__ macro or equivalent. We save the function name on the 470 * a __FUNCTION__ macro or equivalent.
471 * local stack.
472 */ 471 */
473#ifndef ACPI_GET_FUNCTION_NAME 472#ifndef ACPI_GET_FUNCTION_NAME
474#define ACPI_GET_FUNCTION_NAME _acpi_function_name 473#define ACPI_GET_FUNCTION_NAME _acpi_function_name
475/* 474/*
476 * The Name parameter should be the procedure name as a quoted string. 475 * The Name parameter should be the procedure name as a quoted string.
477 * This is declared as a local string ("MyFunctionName") so that it can 476 * The function name is also used by the function exit macros below.
478 * be also used by the function exit macros below.
479 * Note: (const char) is used to be compatible with the debug interfaces 477 * Note: (const char) is used to be compatible with the debug interfaces
480 * and macros such as __FUNCTION__. 478 * and macros such as __FUNCTION__.
481 */ 479 */
482#define ACPI_FUNCTION_NAME(name) const char *_acpi_function_name = #name; 480#define ACPI_FUNCTION_NAME(name) static const char _acpi_function_name[] = #name;
483 481
484#else 482#else
485/* Compiler supports __FUNCTION__ (or equivalent) -- Ignore this macro */ 483/* Compiler supports __FUNCTION__ (or equivalent) -- Ignore this macro */
@@ -599,7 +597,7 @@ struct acpi_integer_overlay {
599/* Stack and buffer dumping */ 597/* Stack and buffer dumping */
600 598
601#define ACPI_DUMP_STACK_ENTRY(a) acpi_ex_dump_operand((a),0) 599#define ACPI_DUMP_STACK_ENTRY(a) acpi_ex_dump_operand((a),0)
602#define ACPI_DUMP_OPERANDS(a,b,c,d,e) acpi_ex_dump_operands(a,b,c,d,e,_acpi_module_name,__LINE__) 600#define ACPI_DUMP_OPERANDS(a,b,c) acpi_ex_dump_operands(a,b,c)
603 601
604#define ACPI_DUMP_ENTRY(a,b) acpi_ns_dump_entry (a,b) 602#define ACPI_DUMP_ENTRY(a,b) acpi_ns_dump_entry (a,b)
605#define ACPI_DUMP_PATHNAME(a,b,c,d) acpi_ns_dump_pathname(a,b,c,d) 603#define ACPI_DUMP_PATHNAME(a,b,c,d) acpi_ns_dump_pathname(a,b,c,d)
@@ -635,7 +633,7 @@ struct acpi_integer_overlay {
635#define ACPI_FUNCTION_VALUE_EXIT(s) do { } while(0) 633#define ACPI_FUNCTION_VALUE_EXIT(s) do { } while(0)
636#define ACPI_FUNCTION_ENTRY() do { } while(0) 634#define ACPI_FUNCTION_ENTRY() do { } while(0)
637#define ACPI_DUMP_STACK_ENTRY(a) do { } while(0) 635#define ACPI_DUMP_STACK_ENTRY(a) do { } while(0)
638#define ACPI_DUMP_OPERANDS(a,b,c,d,e) do { } while(0) 636#define ACPI_DUMP_OPERANDS(a,b,c) do { } while(0)
639#define ACPI_DUMP_ENTRY(a,b) do { } while(0) 637#define ACPI_DUMP_ENTRY(a,b) do { } while(0)
640#define ACPI_DUMP_TABLES(a,b) do { } while(0) 638#define ACPI_DUMP_TABLES(a,b) do { } while(0)
641#define ACPI_DUMP_PATHNAME(a,b,c,d) do { } while(0) 639#define ACPI_DUMP_PATHNAME(a,b,c,d) do { } while(0)
diff --git a/include/acpi/acnamesp.h b/include/acpi/acnamesp.h
index 713b30903fe5..9ed70a050580 100644
--- a/include/acpi/acnamesp.h
+++ b/include/acpi/acnamesp.h
@@ -86,8 +86,7 @@ acpi_status acpi_ns_initialize_devices(void);
86acpi_status acpi_ns_load_namespace(void); 86acpi_status acpi_ns_load_namespace(void);
87 87
88acpi_status 88acpi_status
89acpi_ns_load_table(acpi_native_uint table_index, 89acpi_ns_load_table(u32 table_index, struct acpi_namespace_node *node);
90 struct acpi_namespace_node *node);
91 90
92/* 91/*
93 * nswalk - walk the namespace 92 * nswalk - walk the namespace
@@ -108,12 +107,11 @@ struct acpi_namespace_node *acpi_ns_get_next_node(acpi_object_type type, struct
108 * nsparse - table parsing 107 * nsparse - table parsing
109 */ 108 */
110acpi_status 109acpi_status
111acpi_ns_parse_table(acpi_native_uint table_index, 110acpi_ns_parse_table(u32 table_index, struct acpi_namespace_node *start_node);
112 struct acpi_namespace_node *start_node);
113 111
114acpi_status 112acpi_status
115acpi_ns_one_complete_parse(acpi_native_uint pass_number, 113acpi_ns_one_complete_parse(u32 pass_number,
116 acpi_native_uint table_index, 114 u32 table_index,
117 struct acpi_namespace_node *start_node); 115 struct acpi_namespace_node *start_node);
118 116
119/* 117/*
@@ -201,7 +199,7 @@ acpi_ns_pattern_match(struct acpi_namespace_node *obj_node, char *search_for);
201 199
202acpi_status 200acpi_status
203acpi_ns_get_node(struct acpi_namespace_node *prefix_node, 201acpi_ns_get_node(struct acpi_namespace_node *prefix_node,
204 char *external_pathname, 202 const char *external_pathname,
205 u32 flags, struct acpi_namespace_node **out_node); 203 u32 flags, struct acpi_namespace_node **out_node);
206 204
207acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node); 205acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node);
@@ -265,28 +263,30 @@ acpi_object_type acpi_ns_get_type(struct acpi_namespace_node *node);
265u32 acpi_ns_local(acpi_object_type type); 263u32 acpi_ns_local(acpi_object_type type);
266 264
267void 265void
268acpi_ns_report_error(char *module_name, 266acpi_ns_report_error(const char *module_name,
269 u32 line_number, 267 u32 line_number,
270 char *internal_name, acpi_status lookup_status); 268 const char *internal_name, acpi_status lookup_status);
271 269
272void 270void
273acpi_ns_report_method_error(char *module_name, 271acpi_ns_report_method_error(const char *module_name,
274 u32 line_number, 272 u32 line_number,
275 char *message, 273 const char *message,
276 struct acpi_namespace_node *node, 274 struct acpi_namespace_node *node,
277 char *path, acpi_status lookup_status); 275 const char *path, acpi_status lookup_status);
278 276
279void acpi_ns_print_node_pathname(struct acpi_namespace_node *node, char *msg); 277void
278acpi_ns_print_node_pathname(struct acpi_namespace_node *node, const char *msg);
280 279
281acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info); 280acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info);
282 281
283void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info); 282void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info);
284 283
285acpi_status acpi_ns_internalize_name(char *dotted_name, char **converted_name); 284acpi_status
285acpi_ns_internalize_name(const char *dotted_name, char **converted_name);
286 286
287acpi_status 287acpi_status
288acpi_ns_externalize_name(u32 internal_name_length, 288acpi_ns_externalize_name(u32 internal_name_length,
289 char *internal_name, 289 const char *internal_name,
290 u32 * converted_name_length, char **converted_name); 290 u32 * converted_name_length, char **converted_name);
291 291
292struct acpi_namespace_node *acpi_ns_map_handle_to_node(acpi_handle handle); 292struct acpi_namespace_node *acpi_ns_map_handle_to_node(acpi_handle handle);
diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h
index 2f1c68c7a727..a5ac0bc7f52e 100644
--- a/include/acpi/acpi_bus.h
+++ b/include/acpi/acpi_bus.h
@@ -259,6 +259,7 @@ struct acpi_device_perf {
259/* Wakeup Management */ 259/* Wakeup Management */
260struct acpi_device_wakeup_flags { 260struct acpi_device_wakeup_flags {
261 u8 valid:1; /* Can successfully enable wakeup? */ 261 u8 valid:1; /* Can successfully enable wakeup? */
262 u8 prepared:1; /* Has the wake-up capability been enabled? */
262 u8 run_wake:1; /* Run-Wake GPE devices */ 263 u8 run_wake:1; /* Run-Wake GPE devices */
263}; 264};
264 265
@@ -335,6 +336,8 @@ void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context);
335int acpi_bus_get_status(struct acpi_device *device); 336int acpi_bus_get_status(struct acpi_device *device);
336int acpi_bus_get_power(acpi_handle handle, int *state); 337int acpi_bus_get_power(acpi_handle handle, int *state);
337int acpi_bus_set_power(acpi_handle handle, int state); 338int acpi_bus_set_power(acpi_handle handle, int state);
339bool acpi_bus_power_manageable(acpi_handle handle);
340bool acpi_bus_can_wakeup(acpi_handle handle);
338#ifdef CONFIG_ACPI_PROC_EVENT 341#ifdef CONFIG_ACPI_PROC_EVENT
339int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data); 342int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data);
340int acpi_bus_generate_proc_event4(const char *class, const char *bid, u8 type, int data); 343int acpi_bus_generate_proc_event4(const char *class, const char *bid, u8 type, int data);
@@ -376,14 +379,19 @@ acpi_handle acpi_get_pci_rootbridge_handle(unsigned int, unsigned int);
376#define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)((dev)->archdata.acpi_handle)) 379#define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)((dev)->archdata.acpi_handle))
377 380
378#ifdef CONFIG_PM_SLEEP 381#ifdef CONFIG_PM_SLEEP
379int acpi_pm_device_sleep_state(struct device *, int, int *); 382int acpi_pm_device_sleep_state(struct device *, int *);
383int acpi_pm_device_sleep_wake(struct device *, bool);
380#else /* !CONFIG_PM_SLEEP */ 384#else /* !CONFIG_PM_SLEEP */
381static inline int acpi_pm_device_sleep_state(struct device *d, int w, int *p) 385static inline int acpi_pm_device_sleep_state(struct device *d, int *p)
382{ 386{
383 if (p) 387 if (p)
384 *p = ACPI_STATE_D0; 388 *p = ACPI_STATE_D0;
385 return ACPI_STATE_D3; 389 return ACPI_STATE_D3;
386} 390}
391static inline int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
392{
393 return -ENODEV;
394}
387#endif /* !CONFIG_PM_SLEEP */ 395#endif /* !CONFIG_PM_SLEEP */
388 396
389#endif /* CONFIG_ACPI */ 397#endif /* CONFIG_ACPI */
diff --git a/include/acpi/acpi_drivers.h b/include/acpi/acpi_drivers.h
index 9757a040a505..e5f38e5ce86f 100644
--- a/include/acpi/acpi_drivers.h
+++ b/include/acpi/acpi_drivers.h
@@ -87,7 +87,9 @@ struct pci_bus *pci_acpi_scan_root(struct acpi_device *device, int domain,
87 -------------------------------------------------------------------------- */ 87 -------------------------------------------------------------------------- */
88 88
89#ifdef CONFIG_ACPI_POWER 89#ifdef CONFIG_ACPI_POWER
90int acpi_enable_wakeup_device_power(struct acpi_device *dev); 90int acpi_device_sleep_wake(struct acpi_device *dev,
91 int enable, int sleep_state, int dev_state);
92int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state);
91int acpi_disable_wakeup_device_power(struct acpi_device *dev); 93int acpi_disable_wakeup_device_power(struct acpi_device *dev);
92int acpi_power_get_inferred_state(struct acpi_device *device); 94int acpi_power_get_inferred_state(struct acpi_device *device);
93int acpi_power_transition(struct acpi_device *device, int state); 95int acpi_power_transition(struct acpi_device *device, int state);
diff --git a/include/acpi/acpiosxf.h b/include/acpi/acpiosxf.h
index d4a560d2deb6..3f93a6b4e17f 100644
--- a/include/acpi/acpiosxf.h
+++ b/include/acpi/acpiosxf.h
@@ -144,7 +144,7 @@ void acpi_os_release_mutex(acpi_mutex handle);
144void *acpi_os_allocate(acpi_size size); 144void *acpi_os_allocate(acpi_size size);
145 145
146void __iomem *acpi_os_map_memory(acpi_physical_address where, 146void __iomem *acpi_os_map_memory(acpi_physical_address where,
147 acpi_native_uint length); 147 acpi_size length);
148 148
149void acpi_os_unmap_memory(void __iomem * logical_address, acpi_size size); 149void acpi_os_unmap_memory(void __iomem * logical_address, acpi_size size);
150 150
diff --git a/include/acpi/acpixf.h b/include/acpi/acpixf.h
index 2c3806e6546f..94d94e126e9f 100644
--- a/include/acpi/acpixf.h
+++ b/include/acpi/acpixf.h
@@ -98,7 +98,7 @@ void acpi_free(void *address);
98 */ 98 */
99acpi_status acpi_reallocate_root_table(void); 99acpi_status acpi_reallocate_root_table(void);
100 100
101acpi_status acpi_find_root_pointer(acpi_native_uint * rsdp_address); 101acpi_status acpi_find_root_pointer(acpi_size *rsdp_address);
102 102
103acpi_status acpi_load_tables(void); 103acpi_status acpi_load_tables(void);
104 104
@@ -108,15 +108,15 @@ acpi_status acpi_unload_table_id(acpi_owner_id id);
108 108
109acpi_status 109acpi_status
110acpi_get_table_header(acpi_string signature, 110acpi_get_table_header(acpi_string signature,
111 acpi_native_uint instance, 111 u32 instance,
112 struct acpi_table_header *out_table_header); 112 struct acpi_table_header *out_table_header);
113 113
114acpi_status 114acpi_status
115acpi_get_table(acpi_string signature, 115acpi_get_table(acpi_string signature,
116 acpi_native_uint instance, struct acpi_table_header **out_table); 116 u32 instance, struct acpi_table_header **out_table);
117 117
118acpi_status 118acpi_status
119acpi_get_table_by_index(acpi_native_uint table_index, 119acpi_get_table_by_index(u32 table_index,
120 struct acpi_table_header **out_table); 120 struct acpi_table_header **out_table);
121 121
122acpi_status 122acpi_status
@@ -248,9 +248,7 @@ acpi_status acpi_disable_event(u32 event, u32 flags);
248 248
249acpi_status acpi_clear_event(u32 event); 249acpi_status acpi_clear_event(u32 event);
250 250
251#ifdef ACPI_FUTURE_USAGE
252acpi_status acpi_get_event_status(u32 event, acpi_event_status * event_status); 251acpi_status acpi_get_event_status(u32 event, acpi_event_status * event_status);
253#endif /* ACPI_FUTURE_USAGE */
254 252
255acpi_status acpi_set_gpe_type(acpi_handle gpe_device, u32 gpe_number, u8 type); 253acpi_status acpi_set_gpe_type(acpi_handle gpe_device, u32 gpe_number, u8 type);
256 254
@@ -260,12 +258,10 @@ acpi_status acpi_disable_gpe(acpi_handle gpe_device, u32 gpe_number, u32 flags);
260 258
261acpi_status acpi_clear_gpe(acpi_handle gpe_device, u32 gpe_number, u32 flags); 259acpi_status acpi_clear_gpe(acpi_handle gpe_device, u32 gpe_number, u32 flags);
262 260
263#ifdef ACPI_FUTURE_USAGE
264acpi_status 261acpi_status
265acpi_get_gpe_status(acpi_handle gpe_device, 262acpi_get_gpe_status(acpi_handle gpe_device,
266 u32 gpe_number, 263 u32 gpe_number,
267 u32 flags, acpi_event_status * event_status); 264 u32 flags, acpi_event_status * event_status);
268#endif /* ACPI_FUTURE_USAGE */
269 265
270acpi_status 266acpi_status
271acpi_install_gpe_block(acpi_handle gpe_device, 267acpi_install_gpe_block(acpi_handle gpe_device,
diff --git a/include/acpi/acstruct.h b/include/acpi/acstruct.h
index a907c67d651e..7980a26bad35 100644
--- a/include/acpi/acstruct.h
+++ b/include/acpi/acstruct.h
@@ -108,7 +108,6 @@ struct acpi_walk_state {
108 union acpi_operand_object **caller_return_desc; 108 union acpi_operand_object **caller_return_desc;
109 union acpi_generic_state *control_state; /* List of control states (nested IFs) */ 109 union acpi_generic_state *control_state; /* List of control states (nested IFs) */
110 struct acpi_namespace_node *deferred_node; /* Used when executing deferred opcodes */ 110 struct acpi_namespace_node *deferred_node; /* Used when executing deferred opcodes */
111 struct acpi_gpe_event_info *gpe_event_info; /* Info for GPE (_Lxx/_Exx methods only */
112 union acpi_operand_object *implicit_return_obj; 111 union acpi_operand_object *implicit_return_obj;
113 struct acpi_namespace_node *method_call_node; /* Called method Node */ 112 struct acpi_namespace_node *method_call_node; /* Called method Node */
114 union acpi_parse_object *method_call_op; /* method_call Op if running a method */ 113 union acpi_parse_object *method_call_op; /* method_call Op if running a method */
@@ -143,7 +142,7 @@ struct acpi_init_walk_info {
143 u16 package_init; 142 u16 package_init;
144 u16 object_count; 143 u16 object_count;
145 acpi_owner_id owner_id; 144 acpi_owner_id owner_id;
146 acpi_native_uint table_index; 145 u32 table_index;
147}; 146};
148 147
149struct acpi_get_devices_info { 148struct acpi_get_devices_info {
@@ -189,17 +188,12 @@ struct acpi_evaluate_info {
189 union acpi_operand_object **parameters; 188 union acpi_operand_object **parameters;
190 struct acpi_namespace_node *resolved_node; 189 struct acpi_namespace_node *resolved_node;
191 union acpi_operand_object *return_object; 190 union acpi_operand_object *return_object;
191 u8 param_count;
192 u8 pass_number; 192 u8 pass_number;
193 u8 parameter_type;
194 u8 return_object_type; 193 u8 return_object_type;
195 u8 flags; 194 u8 flags;
196}; 195};
197 196
198/* Types for parameter_type above */
199
200#define ACPI_PARAM_ARGS 0
201#define ACPI_PARAM_GPE 1
202
203/* Values for Flags above */ 197/* Values for Flags above */
204 198
205#define ACPI_IGNORE_RETURN_VALUE 1 199#define ACPI_IGNORE_RETURN_VALUE 1
diff --git a/include/acpi/actables.h b/include/acpi/actables.h
index 4b36a55b0b3b..0cbe1b9ab522 100644
--- a/include/acpi/actables.h
+++ b/include/acpi/actables.h
@@ -49,7 +49,7 @@ acpi_status acpi_allocate_root_table(u32 initial_table_count);
49/* 49/*
50 * tbfadt - FADT parse/convert/validate 50 * tbfadt - FADT parse/convert/validate
51 */ 51 */
52void acpi_tb_parse_fadt(acpi_native_uint table_index, u8 flags); 52void acpi_tb_parse_fadt(u32 table_index, u8 flags);
53 53
54void acpi_tb_create_local_fadt(struct acpi_table_header *table, u32 length); 54void acpi_tb_create_local_fadt(struct acpi_table_header *table, u32 length);
55 55
@@ -58,8 +58,7 @@ void acpi_tb_create_local_fadt(struct acpi_table_header *table, u32 length);
58 */ 58 */
59acpi_status 59acpi_status
60acpi_tb_find_table(char *signature, 60acpi_tb_find_table(char *signature,
61 char *oem_id, 61 char *oem_id, char *oem_table_id, u32 *table_index);
62 char *oem_table_id, acpi_native_uint * table_index);
63 62
64/* 63/*
65 * tbinstal - Table removal and deletion 64 * tbinstal - Table removal and deletion
@@ -69,30 +68,28 @@ acpi_status acpi_tb_resize_root_table_list(void);
69acpi_status acpi_tb_verify_table(struct acpi_table_desc *table_desc); 68acpi_status acpi_tb_verify_table(struct acpi_table_desc *table_desc);
70 69
71acpi_status 70acpi_status
72acpi_tb_add_table(struct acpi_table_desc *table_desc, 71acpi_tb_add_table(struct acpi_table_desc *table_desc, u32 *table_index);
73 acpi_native_uint * table_index);
74 72
75acpi_status 73acpi_status
76acpi_tb_store_table(acpi_physical_address address, 74acpi_tb_store_table(acpi_physical_address address,
77 struct acpi_table_header *table, 75 struct acpi_table_header *table,
78 u32 length, u8 flags, acpi_native_uint * table_index); 76 u32 length, u8 flags, u32 *table_index);
79 77
80void acpi_tb_delete_table(struct acpi_table_desc *table_desc); 78void acpi_tb_delete_table(struct acpi_table_desc *table_desc);
81 79
82void acpi_tb_terminate(void); 80void acpi_tb_terminate(void);
83 81
84void acpi_tb_delete_namespace_by_owner(acpi_native_uint table_index); 82void acpi_tb_delete_namespace_by_owner(u32 table_index);
85 83
86acpi_status acpi_tb_allocate_owner_id(acpi_native_uint table_index); 84acpi_status acpi_tb_allocate_owner_id(u32 table_index);
87 85
88acpi_status acpi_tb_release_owner_id(acpi_native_uint table_index); 86acpi_status acpi_tb_release_owner_id(u32 table_index);
89 87
90acpi_status 88acpi_status acpi_tb_get_owner_id(u32 table_index, acpi_owner_id *owner_id);
91acpi_tb_get_owner_id(acpi_native_uint table_index, acpi_owner_id * owner_id);
92 89
93u8 acpi_tb_is_table_loaded(acpi_native_uint table_index); 90u8 acpi_tb_is_table_loaded(u32 table_index);
94 91
95void acpi_tb_set_table_loaded_flag(acpi_native_uint table_index, u8 is_loaded); 92void acpi_tb_set_table_loaded_flag(u32 table_index, u8 is_loaded);
96 93
97/* 94/*
98 * tbutils - table manager utilities 95 * tbutils - table manager utilities
@@ -103,14 +100,14 @@ void
103acpi_tb_print_table_header(acpi_physical_address address, 100acpi_tb_print_table_header(acpi_physical_address address,
104 struct acpi_table_header *header); 101 struct acpi_table_header *header);
105 102
106u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length); 103u8 acpi_tb_checksum(u8 *buffer, u32 length);
107 104
108acpi_status 105acpi_status
109acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length); 106acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length);
110 107
111void 108void
112acpi_tb_install_table(acpi_physical_address address, 109acpi_tb_install_table(acpi_physical_address address,
113 u8 flags, char *signature, acpi_native_uint table_index); 110 u8 flags, char *signature, u32 table_index);
114 111
115acpi_status 112acpi_status
116acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags); 113acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags);
diff --git a/include/acpi/actbl1.h b/include/acpi/actbl1.h
index 9af239bd1153..d38f9be2f6ee 100644
--- a/include/acpi/actbl1.h
+++ b/include/acpi/actbl1.h
@@ -300,6 +300,7 @@ struct acpi_table_dbgp {
300/******************************************************************************* 300/*******************************************************************************
301 * 301 *
302 * DMAR - DMA Remapping table 302 * DMAR - DMA Remapping table
303 * From "Intel Virtualization Technology for Directed I/O", Sept. 2007
303 * 304 *
304 ******************************************************************************/ 305 ******************************************************************************/
305 306
@@ -310,6 +311,10 @@ struct acpi_table_dmar {
310 u8 reserved[10]; 311 u8 reserved[10];
311}; 312};
312 313
314/* Flags */
315
316#define ACPI_DMAR_INTR_REMAP (1)
317
313/* DMAR subtable header */ 318/* DMAR subtable header */
314 319
315struct acpi_dmar_header { 320struct acpi_dmar_header {
@@ -382,6 +387,20 @@ struct acpi_dmar_reserved_memory {
382 387
383#define ACPI_DMAR_ALLOW_ALL (1) 388#define ACPI_DMAR_ALLOW_ALL (1)
384 389
390
391/* 2: Root Port ATS Capability Reporting Structure */
392
393struct acpi_dmar_atsr {
394 struct acpi_dmar_header header;
395 u8 flags;
396 u8 reserved;
397 u16 segment;
398};
399
400/* Flags */
401
402#define ACPI_DMAR_ALL_PORTS (1)
403
385/******************************************************************************* 404/*******************************************************************************
386 * 405 *
387 * ECDT - Embedded Controller Boot Resources Table 406 * ECDT - Embedded Controller Boot Resources Table
@@ -1156,9 +1175,9 @@ struct acpi_srat_mem_affinity {
1156 u16 reserved; /* Reserved, must be zero */ 1175 u16 reserved; /* Reserved, must be zero */
1157 u64 base_address; 1176 u64 base_address;
1158 u64 length; 1177 u64 length;
1159 u32 memory_type; /* See acpi_address_range_id */ 1178 u32 reserved1;
1160 u32 flags; 1179 u32 flags;
1161 u64 reserved1; /* Reserved, must be zero */ 1180 u64 reserved2; /* Reserved, must be zero */
1162}; 1181};
1163 1182
1164/* Flags */ 1183/* Flags */
diff --git a/include/acpi/actypes.h b/include/acpi/actypes.h
index dfea2d440488..4ea4f40bf894 100644
--- a/include/acpi/actypes.h
+++ b/include/acpi/actypes.h
@@ -110,10 +110,10 @@
110 * usually used for memory allocation, efficient loop counters, and array 110 * usually used for memory allocation, efficient loop counters, and array
111 * indexes. The types are similar to the size_t type in the C library and are 111 * indexes. The types are similar to the size_t type in the C library and are
112 * required because there is no C type that consistently represents the native 112 * required because there is no C type that consistently represents the native
113 * data width. 113 * data width. ACPI_SIZE is needed because there is no guarantee that a
114 * kernel-level C library is present.
114 * 115 *
115 * ACPI_SIZE 16/32/64-bit unsigned value 116 * ACPI_SIZE 16/32/64-bit unsigned value
116 * ACPI_NATIVE_UINT 16/32/64-bit unsigned value
117 * ACPI_NATIVE_INT 16/32/64-bit signed value 117 * ACPI_NATIVE_INT 16/32/64-bit signed value
118 * 118 *
119 */ 119 */
@@ -147,9 +147,9 @@ typedef int INT32;
147 147
148/*! [End] no source code translation !*/ 148/*! [End] no source code translation !*/
149 149
150typedef u64 acpi_native_uint;
151typedef s64 acpi_native_int; 150typedef s64 acpi_native_int;
152 151
152typedef u64 acpi_size;
153typedef u64 acpi_io_address; 153typedef u64 acpi_io_address;
154typedef u64 acpi_physical_address; 154typedef u64 acpi_physical_address;
155 155
@@ -186,9 +186,9 @@ typedef int INT32;
186 186
187/*! [End] no source code translation !*/ 187/*! [End] no source code translation !*/
188 188
189typedef u32 acpi_native_uint;
190typedef s32 acpi_native_int; 189typedef s32 acpi_native_int;
191 190
191typedef u32 acpi_size;
192typedef u32 acpi_io_address; 192typedef u32 acpi_io_address;
193typedef u32 acpi_physical_address; 193typedef u32 acpi_physical_address;
194 194
@@ -202,10 +202,6 @@ typedef u32 acpi_physical_address;
202#error unknown ACPI_MACHINE_WIDTH 202#error unknown ACPI_MACHINE_WIDTH
203#endif 203#endif
204 204
205/* Variable-width type, used instead of clib size_t */
206
207typedef acpi_native_uint acpi_size;
208
209/******************************************************************************* 205/*******************************************************************************
210 * 206 *
211 * OS-dependent and compiler-dependent types 207 * OS-dependent and compiler-dependent types
@@ -219,7 +215,7 @@ typedef acpi_native_uint acpi_size;
219/* Value returned by acpi_os_get_thread_id */ 215/* Value returned by acpi_os_get_thread_id */
220 216
221#ifndef acpi_thread_id 217#ifndef acpi_thread_id
222#define acpi_thread_id acpi_native_uint 218#define acpi_thread_id acpi_size
223#endif 219#endif
224 220
225/* Object returned from acpi_os_create_lock */ 221/* Object returned from acpi_os_create_lock */
@@ -231,7 +227,7 @@ typedef acpi_native_uint acpi_size;
231/* Flags for acpi_os_acquire_lock/acpi_os_release_lock */ 227/* Flags for acpi_os_acquire_lock/acpi_os_release_lock */
232 228
233#ifndef acpi_cpu_flags 229#ifndef acpi_cpu_flags
234#define acpi_cpu_flags acpi_native_uint 230#define acpi_cpu_flags acpi_size
235#endif 231#endif
236 232
237/* Object returned from acpi_os_create_cache */ 233/* Object returned from acpi_os_create_cache */
diff --git a/include/acpi/acutils.h b/include/acpi/acutils.h
index b42cadf07302..69f8888771ff 100644
--- a/include/acpi/acutils.h
+++ b/include/acpi/acutils.h
@@ -172,7 +172,7 @@ char *acpi_ut_strstr(char *string1, char *string2);
172 172
173void *acpi_ut_memcpy(void *dest, const void *src, acpi_size count); 173void *acpi_ut_memcpy(void *dest, const void *src, acpi_size count);
174 174
175void *acpi_ut_memset(void *dest, acpi_native_uint value, acpi_size count); 175void *acpi_ut_memset(void *dest, u8 value, acpi_size count);
176 176
177int acpi_ut_to_upper(int c); 177int acpi_ut_to_upper(int c);
178 178
@@ -245,41 +245,45 @@ void acpi_ut_track_stack_ptr(void);
245 245
246void 246void
247acpi_ut_trace(u32 line_number, 247acpi_ut_trace(u32 line_number,
248 const char *function_name, char *module_name, u32 component_id); 248 const char *function_name,
249 const char *module_name, u32 component_id);
249 250
250void 251void
251acpi_ut_trace_ptr(u32 line_number, 252acpi_ut_trace_ptr(u32 line_number,
252 const char *function_name, 253 const char *function_name,
253 char *module_name, u32 component_id, void *pointer); 254 const char *module_name, u32 component_id, void *pointer);
254 255
255void 256void
256acpi_ut_trace_u32(u32 line_number, 257acpi_ut_trace_u32(u32 line_number,
257 const char *function_name, 258 const char *function_name,
258 char *module_name, u32 component_id, u32 integer); 259 const char *module_name, u32 component_id, u32 integer);
259 260
260void 261void
261acpi_ut_trace_str(u32 line_number, 262acpi_ut_trace_str(u32 line_number,
262 const char *function_name, 263 const char *function_name,
263 char *module_name, u32 component_id, char *string); 264 const char *module_name, u32 component_id, char *string);
264 265
265void 266void
266acpi_ut_exit(u32 line_number, 267acpi_ut_exit(u32 line_number,
267 const char *function_name, char *module_name, u32 component_id); 268 const char *function_name,
269 const char *module_name, u32 component_id);
268 270
269void 271void
270acpi_ut_status_exit(u32 line_number, 272acpi_ut_status_exit(u32 line_number,
271 const char *function_name, 273 const char *function_name,
272 char *module_name, u32 component_id, acpi_status status); 274 const char *module_name,
275 u32 component_id, acpi_status status);
273 276
274void 277void
275acpi_ut_value_exit(u32 line_number, 278acpi_ut_value_exit(u32 line_number,
276 const char *function_name, 279 const char *function_name,
277 char *module_name, u32 component_id, acpi_integer value); 280 const char *module_name,
281 u32 component_id, acpi_integer value);
278 282
279void 283void
280acpi_ut_ptr_exit(u32 line_number, 284acpi_ut_ptr_exit(u32 line_number,
281 const char *function_name, 285 const char *function_name,
282 char *module_name, u32 component_id, u8 * ptr); 286 const char *module_name, u32 component_id, u8 *ptr);
283 287
284void acpi_ut_dump_buffer(u8 * buffer, u32 count, u32 display, u32 component_id); 288void acpi_ut_dump_buffer(u8 * buffer, u32 count, u32 display, u32 component_id);
285 289
@@ -297,33 +301,35 @@ void ACPI_INTERNAL_VAR_XFACE
297acpi_ut_debug_print(u32 requested_debug_level, 301acpi_ut_debug_print(u32 requested_debug_level,
298 u32 line_number, 302 u32 line_number,
299 const char *function_name, 303 const char *function_name,
300 char *module_name, 304 const char *module_name,
301 u32 component_id, char *format, ...) ACPI_PRINTF_LIKE(6); 305 u32 component_id,
306 const char *format, ...) ACPI_PRINTF_LIKE(6);
302 307
303void ACPI_INTERNAL_VAR_XFACE 308void ACPI_INTERNAL_VAR_XFACE
304acpi_ut_debug_print_raw(u32 requested_debug_level, 309acpi_ut_debug_print_raw(u32 requested_debug_level,
305 u32 line_number, 310 u32 line_number,
306 const char *function_name, 311 const char *function_name,
307 char *module_name, 312 const char *module_name,
308 u32 component_id, 313 u32 component_id,
309 char *format, ...) ACPI_PRINTF_LIKE(6); 314 const char *format, ...) ACPI_PRINTF_LIKE(6);
310 315
311void ACPI_INTERNAL_VAR_XFACE 316void ACPI_INTERNAL_VAR_XFACE
312acpi_ut_error(char *module_name, 317acpi_ut_error(const char *module_name,
313 u32 line_number, char *format, ...) ACPI_PRINTF_LIKE(3); 318 u32 line_number, const char *format, ...) ACPI_PRINTF_LIKE(3);
314 319
315void ACPI_INTERNAL_VAR_XFACE 320void ACPI_INTERNAL_VAR_XFACE
316acpi_ut_exception(char *module_name, 321acpi_ut_exception(const char *module_name,
317 u32 line_number, 322 u32 line_number,
318 acpi_status status, char *format, ...) ACPI_PRINTF_LIKE(4); 323 acpi_status status,
324 const char *format, ...) ACPI_PRINTF_LIKE(4);
319 325
320void ACPI_INTERNAL_VAR_XFACE 326void ACPI_INTERNAL_VAR_XFACE
321acpi_ut_warning(char *module_name, 327acpi_ut_warning(const char *module_name,
322 u32 line_number, char *format, ...) ACPI_PRINTF_LIKE(3); 328 u32 line_number, const char *format, ...) ACPI_PRINTF_LIKE(3);
323 329
324void ACPI_INTERNAL_VAR_XFACE 330void ACPI_INTERNAL_VAR_XFACE
325acpi_ut_info(char *module_name, 331acpi_ut_info(const char *module_name,
326 u32 line_number, char *format, ...) ACPI_PRINTF_LIKE(3); 332 u32 line_number, const char *format, ...) ACPI_PRINTF_LIKE(3);
327 333
328/* 334/*
329 * utdelete - Object deletion and reference counts 335 * utdelete - Object deletion and reference counts
@@ -376,13 +382,14 @@ acpi_ut_execute_sxds(struct acpi_namespace_node *device_node, u8 * highest);
376/* 382/*
377 * utobject - internal object create/delete/cache routines 383 * utobject - internal object create/delete/cache routines
378 */ 384 */
379union acpi_operand_object *acpi_ut_create_internal_object_dbg(char *module_name, 385union acpi_operand_object *acpi_ut_create_internal_object_dbg(const char
386 *module_name,
380 u32 line_number, 387 u32 line_number,
381 u32 component_id, 388 u32 component_id,
382 acpi_object_type 389 acpi_object_type
383 type); 390 type);
384 391
385void *acpi_ut_allocate_object_desc_dbg(char *module_name, 392void *acpi_ut_allocate_object_desc_dbg(const char *module_name,
386 u32 line_number, u32 component_id); 393 u32 line_number, u32 component_id);
387 394
388#define acpi_ut_create_internal_object(t) acpi_ut_create_internal_object_dbg (_acpi_module_name,__LINE__,_COMPONENT,t) 395#define acpi_ut_create_internal_object(t) acpi_ut_create_internal_object_dbg (_acpi_module_name,__LINE__,_COMPONENT,t)
@@ -476,7 +483,7 @@ u8 acpi_ut_valid_acpi_name(u32 name);
476 483
477acpi_name acpi_ut_repair_name(char *name); 484acpi_name acpi_ut_repair_name(char *name);
478 485
479u8 acpi_ut_valid_acpi_char(char character, acpi_native_uint position); 486u8 acpi_ut_valid_acpi_char(char character, u32 position);
480 487
481acpi_status 488acpi_status
482acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer); 489acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer);
@@ -543,26 +550,29 @@ acpi_status
543acpi_ut_initialize_buffer(struct acpi_buffer *buffer, 550acpi_ut_initialize_buffer(struct acpi_buffer *buffer,
544 acpi_size required_length); 551 acpi_size required_length);
545 552
546void *acpi_ut_allocate(acpi_size size, u32 component, char *module, u32 line); 553void *acpi_ut_allocate(acpi_size size,
554 u32 component, const char *module, u32 line);
547 555
548void *acpi_ut_allocate_zeroed(acpi_size size, 556void *acpi_ut_allocate_zeroed(acpi_size size,
549 u32 component, char *module, u32 line); 557 u32 component, const char *module, u32 line);
550 558
551#ifdef ACPI_DBG_TRACK_ALLOCATIONS 559#ifdef ACPI_DBG_TRACK_ALLOCATIONS
552void *acpi_ut_allocate_and_track(acpi_size size, 560void *acpi_ut_allocate_and_track(acpi_size size,
553 u32 component, char *module, u32 line); 561 u32 component, const char *module, u32 line);
554 562
555void *acpi_ut_allocate_zeroed_and_track(acpi_size size, 563void *acpi_ut_allocate_zeroed_and_track(acpi_size size,
556 u32 component, char *module, u32 line); 564 u32 component,
565 const char *module, u32 line);
557 566
558void 567void
559acpi_ut_free_and_track(void *address, u32 component, char *module, u32 line); 568acpi_ut_free_and_track(void *address,
569 u32 component, const char *module, u32 line);
560 570
561#ifdef ACPI_FUTURE_USAGE 571#ifdef ACPI_FUTURE_USAGE
562void acpi_ut_dump_allocation_info(void); 572void acpi_ut_dump_allocation_info(void);
563#endif /* ACPI_FUTURE_USAGE */ 573#endif /* ACPI_FUTURE_USAGE */
564 574
565void acpi_ut_dump_allocations(u32 component, char *module); 575void acpi_ut_dump_allocations(u32 component, const char *module);
566 576
567acpi_status 577acpi_status
568acpi_ut_create_list(char *list_name, 578acpi_ut_create_list(char *list_name,
diff --git a/include/acpi/processor.h b/include/acpi/processor.h
index 06ebb6ef72aa..3795590e152a 100644
--- a/include/acpi/processor.h
+++ b/include/acpi/processor.h
@@ -255,7 +255,7 @@ extern void acpi_processor_unregister_performance(struct
255int acpi_processor_notify_smm(struct module *calling_module); 255int acpi_processor_notify_smm(struct module *calling_module);
256 256
257/* for communication between multiple parts of the processor kernel module */ 257/* for communication between multiple parts of the processor kernel module */
258extern struct acpi_processor *processors[NR_CPUS]; 258DECLARE_PER_CPU(struct acpi_processor *, processors);
259extern struct acpi_processor_errata errata; 259extern struct acpi_processor_errata errata;
260 260
261void arch_acpi_processor_init_pdc(struct acpi_processor *pr); 261void arch_acpi_processor_init_pdc(struct acpi_processor *pr);
diff --git a/include/acpi/reboot.h b/include/acpi/reboot.h
index 8857f57e0b78..0419184ce886 100644
--- a/include/acpi/reboot.h
+++ b/include/acpi/reboot.h
@@ -1,9 +1,11 @@
1#ifndef __ACPI_REBOOT_H
2#define __ACPI_REBOOT_H
3
4#ifdef CONFIG_ACPI
5extern void acpi_reboot(void);
6#else
7static inline void acpi_reboot(void) { }
8#endif
1 9
2/*
3 * Dummy placeholder to make the EFI patches apply to the x86 tree.
4 * Andrew/Len, please just kill this file if you encounter it.
5 */
6#ifndef acpi_reboot
7# define acpi_reboot() do { } while (0)
8#endif 10#endif
9 11
diff --git a/include/asm-arm/arch-at91/at91_mci.h b/include/asm-arm/arch-at91/at91_mci.h
index 1551fc24eb43..400ec10014b4 100644
--- a/include/asm-arm/arch-at91/at91_mci.h
+++ b/include/asm-arm/arch-at91/at91_mci.h
@@ -75,6 +75,10 @@
75#define AT91_MCI_TRTYP_MULTIPLE (1 << 19) 75#define AT91_MCI_TRTYP_MULTIPLE (1 << 19)
76#define AT91_MCI_TRTYP_STREAM (2 << 19) 76#define AT91_MCI_TRTYP_STREAM (2 << 19)
77 77
78#define AT91_MCI_BLKR 0x18 /* Block Register */
79#define AT91_MCI_BLKR_BCNT(n) ((0xffff & (n)) << 0) /* Block count */
80#define AT91_MCI_BLKR_BLKLEN(n) ((0xffff & (n)) << 16) /* Block lenght */
81
78#define AT91_MCI_RSPR(n) (0x20 + ((n) * 4)) /* Response Registers 0-3 */ 82#define AT91_MCI_RSPR(n) (0x20 + ((n) * 4)) /* Response Registers 0-3 */
79#define AT91_MCR_RDR 0x30 /* Receive Data Register */ 83#define AT91_MCR_RDR 0x30 /* Receive Data Register */
80#define AT91_MCR_TDR 0x34 /* Transmit Data Register */ 84#define AT91_MCR_TDR 0x34 /* Transmit Data Register */
diff --git a/include/asm-arm/arch-s3c2410/regs-sdi.h b/include/asm-arm/arch-s3c2410/regs-sdi.h
index bb9d30b72952..bfb222fa4abb 100644
--- a/include/asm-arm/arch-s3c2410/regs-sdi.h
+++ b/include/asm-arm/arch-s3c2410/regs-sdi.h
@@ -28,9 +28,15 @@
28#define S3C2410_SDIDCNT (0x30) 28#define S3C2410_SDIDCNT (0x30)
29#define S3C2410_SDIDSTA (0x34) 29#define S3C2410_SDIDSTA (0x34)
30#define S3C2410_SDIFSTA (0x38) 30#define S3C2410_SDIFSTA (0x38)
31
31#define S3C2410_SDIDATA (0x3C) 32#define S3C2410_SDIDATA (0x3C)
32#define S3C2410_SDIIMSK (0x40) 33#define S3C2410_SDIIMSK (0x40)
33 34
35#define S3C2440_SDIDATA (0x40)
36#define S3C2440_SDIIMSK (0x3C)
37
38#define S3C2440_SDICON_SDRESET (1<<8)
39#define S3C2440_SDICON_MMCCLOCK (1<<5)
34#define S3C2410_SDICON_BYTEORDER (1<<4) 40#define S3C2410_SDICON_BYTEORDER (1<<4)
35#define S3C2410_SDICON_SDIOIRQ (1<<3) 41#define S3C2410_SDICON_SDIOIRQ (1<<3)
36#define S3C2410_SDICON_RWAITEN (1<<2) 42#define S3C2410_SDICON_RWAITEN (1<<2)
@@ -42,7 +48,8 @@
42#define S3C2410_SDICMDCON_LONGRSP (1<<10) 48#define S3C2410_SDICMDCON_LONGRSP (1<<10)
43#define S3C2410_SDICMDCON_WAITRSP (1<<9) 49#define S3C2410_SDICMDCON_WAITRSP (1<<9)
44#define S3C2410_SDICMDCON_CMDSTART (1<<8) 50#define S3C2410_SDICMDCON_CMDSTART (1<<8)
45#define S3C2410_SDICMDCON_INDEX (0xff) 51#define S3C2410_SDICMDCON_SENDERHOST (1<<6)
52#define S3C2410_SDICMDCON_INDEX (0x3f)
46 53
47#define S3C2410_SDICMDSTAT_CRCFAIL (1<<12) 54#define S3C2410_SDICMDSTAT_CRCFAIL (1<<12)
48#define S3C2410_SDICMDSTAT_CMDSENT (1<<11) 55#define S3C2410_SDICMDSTAT_CMDSENT (1<<11)
@@ -51,6 +58,9 @@
51#define S3C2410_SDICMDSTAT_XFERING (1<<8) 58#define S3C2410_SDICMDSTAT_XFERING (1<<8)
52#define S3C2410_SDICMDSTAT_INDEX (0xff) 59#define S3C2410_SDICMDSTAT_INDEX (0xff)
53 60
61#define S3C2440_SDIDCON_DS_BYTE (0<<22)
62#define S3C2440_SDIDCON_DS_HALFWORD (1<<22)
63#define S3C2440_SDIDCON_DS_WORD (2<<22)
54#define S3C2410_SDIDCON_IRQPERIOD (1<<21) 64#define S3C2410_SDIDCON_IRQPERIOD (1<<21)
55#define S3C2410_SDIDCON_TXAFTERRESP (1<<20) 65#define S3C2410_SDIDCON_TXAFTERRESP (1<<20)
56#define S3C2410_SDIDCON_RXAFTERCMD (1<<19) 66#define S3C2410_SDIDCON_RXAFTERCMD (1<<19)
@@ -59,6 +69,7 @@
59#define S3C2410_SDIDCON_WIDEBUS (1<<16) 69#define S3C2410_SDIDCON_WIDEBUS (1<<16)
60#define S3C2410_SDIDCON_DMAEN (1<<15) 70#define S3C2410_SDIDCON_DMAEN (1<<15)
61#define S3C2410_SDIDCON_STOP (1<<14) 71#define S3C2410_SDIDCON_STOP (1<<14)
72#define S3C2440_SDIDCON_DATSTART (1<<14)
62#define S3C2410_SDIDCON_DATMODE (3<<12) 73#define S3C2410_SDIDCON_DATMODE (3<<12)
63#define S3C2410_SDIDCON_BLKNUM (0x7ff) 74#define S3C2410_SDIDCON_BLKNUM (0x7ff)
64 75
@@ -68,6 +79,7 @@
68#define S3C2410_SDIDCON_XFER_RXSTART (2<<12) 79#define S3C2410_SDIDCON_XFER_RXSTART (2<<12)
69#define S3C2410_SDIDCON_XFER_TXSTART (3<<12) 80#define S3C2410_SDIDCON_XFER_TXSTART (3<<12)
70 81
82#define S3C2410_SDIDCON_BLKNUM_MASK (0xFFF)
71#define S3C2410_SDIDCNT_BLKNUM_SHIFT (12) 83#define S3C2410_SDIDCNT_BLKNUM_SHIFT (12)
72 84
73#define S3C2410_SDIDSTA_RDYWAITREQ (1<<10) 85#define S3C2410_SDIDSTA_RDYWAITREQ (1<<10)
@@ -82,10 +94,12 @@
82#define S3C2410_SDIDSTA_TXDATAON (1<<1) 94#define S3C2410_SDIDSTA_TXDATAON (1<<1)
83#define S3C2410_SDIDSTA_RXDATAON (1<<0) 95#define S3C2410_SDIDSTA_RXDATAON (1<<0)
84 96
97#define S3C2440_SDIFSTA_FIFORESET (1<<16)
98#define S3C2440_SDIFSTA_FIFOFAIL (3<<14) /* 3 is correct (2 bits) */
85#define S3C2410_SDIFSTA_TFDET (1<<13) 99#define S3C2410_SDIFSTA_TFDET (1<<13)
86#define S3C2410_SDIFSTA_RFDET (1<<12) 100#define S3C2410_SDIFSTA_RFDET (1<<12)
87#define S3C2410_SDIFSTA_TXHALF (1<<11) 101#define S3C2410_SDIFSTA_TFHALF (1<<11)
88#define S3C2410_SDIFSTA_TXEMPTY (1<<10) 102#define S3C2410_SDIFSTA_TFEMPTY (1<<10)
89#define S3C2410_SDIFSTA_RFLAST (1<<9) 103#define S3C2410_SDIFSTA_RFLAST (1<<9)
90#define S3C2410_SDIFSTA_RFFULL (1<<8) 104#define S3C2410_SDIFSTA_RFFULL (1<<8)
91#define S3C2410_SDIFSTA_RFHALF (1<<7) 105#define S3C2410_SDIFSTA_RFHALF (1<<7)
diff --git a/include/asm-arm/plat-s3c24xx/mci.h b/include/asm-arm/plat-s3c24xx/mci.h
new file mode 100644
index 000000000000..2d0852ac3b27
--- /dev/null
+++ b/include/asm-arm/plat-s3c24xx/mci.h
@@ -0,0 +1,15 @@
1#ifndef _ARCH_MCI_H
2#define _ARCH_MCI_H
3
4struct s3c24xx_mci_pdata {
5 unsigned int wprotect_invert : 1;
6 unsigned int detect_invert : 1; /* set => detect active high. */
7
8 unsigned int gpio_detect;
9 unsigned int gpio_wprotect;
10 unsigned long ocr_avail;
11 void (*set_power)(unsigned char power_mode,
12 unsigned short vdd);
13};
14
15#endif /* _ARCH_NCI_H */
diff --git a/include/asm-avr32/arch-at32ap/board.h b/include/asm-avr32/arch-at32ap/board.h
index b4cddfaca90e..a3783861cdd2 100644
--- a/include/asm-avr32/arch-at32ap/board.h
+++ b/include/asm-avr32/arch-at32ap/board.h
@@ -77,7 +77,11 @@ struct i2c_board_info;
77struct platform_device *at32_add_device_twi(unsigned int id, 77struct platform_device *at32_add_device_twi(unsigned int id,
78 struct i2c_board_info *b, 78 struct i2c_board_info *b,
79 unsigned int n); 79 unsigned int n);
80struct platform_device *at32_add_device_mci(unsigned int id); 80
81struct mci_platform_data;
82struct platform_device *
83at32_add_device_mci(unsigned int id, struct mci_platform_data *data);
84
81struct platform_device *at32_add_device_ac97c(unsigned int id); 85struct platform_device *at32_add_device_ac97c(unsigned int id);
82struct platform_device *at32_add_device_abdac(unsigned int id); 86struct platform_device *at32_add_device_abdac(unsigned int id);
83struct platform_device *at32_add_device_psif(unsigned int id); 87struct platform_device *at32_add_device_psif(unsigned int id);
diff --git a/include/asm-avr32/atmel-mci.h b/include/asm-avr32/atmel-mci.h
new file mode 100644
index 000000000000..c2ea6e1c9aa1
--- /dev/null
+++ b/include/asm-avr32/atmel-mci.h
@@ -0,0 +1,9 @@
1#ifndef __ASM_AVR32_ATMEL_MCI_H
2#define __ASM_AVR32_ATMEL_MCI_H
3
4struct mci_platform_data {
5 int detect_pin;
6 int wp_pin;
7};
8
9#endif /* __ASM_AVR32_ATMEL_MCI_H */
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index b20409404c7d..729f6b0a60e9 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -86,6 +86,12 @@
86 VMLINUX_SYMBOL(__start_pci_fixups_resume) = .; \ 86 VMLINUX_SYMBOL(__start_pci_fixups_resume) = .; \
87 *(.pci_fixup_resume) \ 87 *(.pci_fixup_resume) \
88 VMLINUX_SYMBOL(__end_pci_fixups_resume) = .; \ 88 VMLINUX_SYMBOL(__end_pci_fixups_resume) = .; \
89 VMLINUX_SYMBOL(__start_pci_fixups_resume_early) = .; \
90 *(.pci_fixup_resume_early) \
91 VMLINUX_SYMBOL(__end_pci_fixups_resume_early) = .; \
92 VMLINUX_SYMBOL(__start_pci_fixups_suspend) = .; \
93 *(.pci_fixup_suspend) \
94 VMLINUX_SYMBOL(__end_pci_fixups_suspend) = .; \
89 } \ 95 } \
90 \ 96 \
91 /* Built-in firmware blobs */ \ 97 /* Built-in firmware blobs */ \
diff --git a/include/asm-ia64/processor.h b/include/asm-ia64/processor.h
index 6aff126fc07e..f88fa054d01d 100644
--- a/include/asm-ia64/processor.h
+++ b/include/asm-ia64/processor.h
@@ -763,6 +763,8 @@ prefetchw (const void *x)
763#define spin_lock_prefetch(x) prefetchw(x) 763#define spin_lock_prefetch(x) prefetchw(x)
764 764
765extern unsigned long boot_option_idle_override; 765extern unsigned long boot_option_idle_override;
766extern unsigned long idle_halt;
767extern unsigned long idle_nomwait;
766 768
767#endif /* !__ASSEMBLY__ */ 769#endif /* !__ASSEMBLY__ */
768 770
diff --git a/include/asm-mips/mach-au1x00/au1100_mmc.h b/include/asm-mips/mach-au1x00/au1100_mmc.h
index 9e0028f60a43..c35e20918490 100644
--- a/include/asm-mips/mach-au1x00/au1100_mmc.h
+++ b/include/asm-mips/mach-au1x00/au1100_mmc.h
@@ -38,15 +38,15 @@
38#ifndef __ASM_AU1100_MMC_H 38#ifndef __ASM_AU1100_MMC_H
39#define __ASM_AU1100_MMC_H 39#define __ASM_AU1100_MMC_H
40 40
41 41#include <linux/leds.h>
42#define NUM_AU1100_MMC_CONTROLLERS 2 42
43 43struct au1xmmc_platform_data {
44#if defined(CONFIG_SOC_AU1100) 44 int(*cd_setup)(void *mmc_host, int on);
45#define AU1100_SD_IRQ AU1100_SD_INT 45 int(*card_inserted)(void *mmc_host);
46#elif defined(CONFIG_SOC_AU1200) 46 int(*card_readonly)(void *mmc_host);
47#define AU1100_SD_IRQ AU1200_SD_INT 47 void(*set_power)(void *mmc_host, int state);
48#endif 48 struct led_classdev *led;
49 49};
50 50
51#define SD0_BASE 0xB0600000 51#define SD0_BASE 0xB0600000
52#define SD1_BASE 0xB0680000 52#define SD1_BASE 0xB0680000
diff --git a/include/asm-s390/Kbuild b/include/asm-s390/Kbuild
index 09f312501eb5..bb5e9edb9825 100644
--- a/include/asm-s390/Kbuild
+++ b/include/asm-s390/Kbuild
@@ -8,9 +8,9 @@ header-y += ucontext.h
8header-y += vtoc.h 8header-y += vtoc.h
9header-y += zcrypt.h 9header-y += zcrypt.h
10header-y += kvm.h 10header-y += kvm.h
11header-y += schid.h
12header-y += chsc.h 11header-y += chsc.h
13 12
14unifdef-y += cmb.h 13unifdef-y += cmb.h
15unifdef-y += debug.h 14unifdef-y += debug.h
16unifdef-y += chpid.h 15unifdef-y += chpid.h
16unifdef-y += schid.h
diff --git a/include/asm-s390/chpid.h b/include/asm-s390/chpid.h
index 606844d0a5c3..dfe3c7f3439a 100644
--- a/include/asm-s390/chpid.h
+++ b/include/asm-s390/chpid.h
@@ -20,6 +20,9 @@ struct chp_id {
20 u8 id; 20 u8 id;
21} __attribute__((packed)); 21} __attribute__((packed));
22 22
23#ifdef __KERNEL__
24#include <asm/cio.h>
25
23static inline void chp_id_init(struct chp_id *chpid) 26static inline void chp_id_init(struct chp_id *chpid)
24{ 27{
25 memset(chpid, 0, sizeof(struct chp_id)); 28 memset(chpid, 0, sizeof(struct chp_id));
@@ -40,9 +43,6 @@ static inline void chp_id_next(struct chp_id *chpid)
40 } 43 }
41} 44}
42 45
43#ifdef __KERNEL__
44#include <asm/cio.h>
45
46static inline int chp_id_is_valid(struct chp_id *chpid) 46static inline int chp_id_is_valid(struct chp_id *chpid)
47{ 47{
48 return (chpid->cssid <= __MAX_CSSID); 48 return (chpid->cssid <= __MAX_CSSID);
diff --git a/include/asm-s390/qdio.h b/include/asm-s390/qdio.h
index 11240342a0f4..6813772171f2 100644
--- a/include/asm-s390/qdio.h
+++ b/include/asm-s390/qdio.h
@@ -1,404 +1,382 @@
1/* 1/*
2 * linux/include/asm-s390/qdio.h 2 * linux/include/asm-s390/qdio.h
3 * 3 *
4 * Linux for S/390 QDIO base support, Hipersocket base support 4 * Copyright 2000,2008 IBM Corp.
5 * version 2
6 *
7 * Copyright 2000,2002 IBM Corporation
8 * Author(s): Utz Bacher <utz.bacher@de.ibm.com> 5 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
6 * Jan Glauber <jang@linux.vnet.ibm.com>
9 * 7 *
10 */ 8 */
11#ifndef __QDIO_H__ 9#ifndef __QDIO_H__
12#define __QDIO_H__ 10#define __QDIO_H__
13 11
14/* note, that most of the typedef's are from ingo. */
15
16#include <linux/interrupt.h> 12#include <linux/interrupt.h>
17#include <asm/cio.h> 13#include <asm/cio.h>
18#include <asm/ccwdev.h> 14#include <asm/ccwdev.h>
19 15
20#define QDIO_NAME "qdio " 16#define QDIO_MAX_QUEUES_PER_IRQ 32
21 17#define QDIO_MAX_BUFFERS_PER_Q 128
22#ifndef __s390x__ 18#define QDIO_MAX_BUFFERS_MASK (QDIO_MAX_BUFFERS_PER_Q - 1)
23#define QDIO_32_BIT 19#define QDIO_MAX_ELEMENTS_PER_BUFFER 16
24#endif /* __s390x__ */ 20#define QDIO_SBAL_SIZE 256
25 21
26/**** CONSTANTS, that are relied on without using these symbols *****/ 22#define QDIO_QETH_QFMT 0
27#define QDIO_MAX_QUEUES_PER_IRQ 32 /* used in width of unsigned int */ 23#define QDIO_ZFCP_QFMT 1
28/************************ END of CONSTANTS **************************/ 24#define QDIO_IQDIO_QFMT 2
29#define QDIO_MAX_BUFFERS_PER_Q 128 /* must be a power of 2 (%x=&(x-1)*/ 25
30#define QDIO_BUF_ORDER 7 /* 2**this == number of pages used for sbals in 1 q */ 26/**
31#define QDIO_MAX_ELEMENTS_PER_BUFFER 16 27 * struct qdesfmt0 - queue descriptor, format 0
32#define SBAL_SIZE 256 28 * @sliba: storage list information block address
33 29 * @sla: storage list address
34#define QDIO_QETH_QFMT 0 30 * @slsba: storage list state block address
35#define QDIO_ZFCP_QFMT 1 31 * @akey: access key for DLIB
36#define QDIO_IQDIO_QFMT 2 32 * @bkey: access key for SL
37#define QDIO_IQDIO_QFMT_ASYNCH 3 33 * @ckey: access key for SBALs
38 34 * @dkey: access key for SLSB
39struct qdio_buffer_element{ 35 */
40 unsigned int flags;
41 unsigned int length;
42#ifdef QDIO_32_BIT
43 void *reserved;
44#endif /* QDIO_32_BIT */
45 void *addr;
46} __attribute__ ((packed,aligned(16)));
47
48struct qdio_buffer{
49 volatile struct qdio_buffer_element element[16];
50} __attribute__ ((packed,aligned(256)));
51
52
53/* params are: ccw_device, status, qdio_error, siga_error,
54 queue_number, first element processed, number of elements processed,
55 int_parm */
56typedef void qdio_handler_t(struct ccw_device *,unsigned int,unsigned int,
57 unsigned int,unsigned int,int,int,unsigned long);
58
59
60#define QDIO_STATUS_INBOUND_INT 0x01
61#define QDIO_STATUS_OUTBOUND_INT 0x02
62#define QDIO_STATUS_LOOK_FOR_ERROR 0x04
63#define QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR 0x08
64#define QDIO_STATUS_MORE_THAN_ONE_SIGA_ERROR 0x10
65#define QDIO_STATUS_ACTIVATE_CHECK_CONDITION 0x20
66
67#define QDIO_SIGA_ERROR_ACCESS_EXCEPTION 0x10
68#define QDIO_SIGA_ERROR_B_BIT_SET 0x20
69
70/* for qdio_initialize */
71#define QDIO_INBOUND_0COPY_SBALS 0x01
72#define QDIO_OUTBOUND_0COPY_SBALS 0x02
73#define QDIO_USE_OUTBOUND_PCIS 0x04
74
75/* for qdio_cleanup */
76#define QDIO_FLAG_CLEANUP_USING_CLEAR 0x01
77#define QDIO_FLAG_CLEANUP_USING_HALT 0x02
78
79struct qdio_initialize {
80 struct ccw_device *cdev;
81 unsigned char q_format;
82 unsigned char adapter_name[8];
83 unsigned int qib_param_field_format; /*adapter dependent*/
84 /* pointer to 128 bytes or NULL, if no param field */
85 unsigned char *qib_param_field; /* adapter dependent */
86 /* pointer to no_queues*128 words of data or NULL */
87 unsigned long *input_slib_elements;
88 unsigned long *output_slib_elements;
89 unsigned int min_input_threshold;
90 unsigned int max_input_threshold;
91 unsigned int min_output_threshold;
92 unsigned int max_output_threshold;
93 unsigned int no_input_qs;
94 unsigned int no_output_qs;
95 qdio_handler_t *input_handler;
96 qdio_handler_t *output_handler;
97 unsigned long int_parm;
98 unsigned long flags;
99 void **input_sbal_addr_array; /* addr of n*128 void ptrs */
100 void **output_sbal_addr_array; /* addr of n*128 void ptrs */
101};
102
103extern int qdio_initialize(struct qdio_initialize *init_data);
104extern int qdio_allocate(struct qdio_initialize *init_data);
105extern int qdio_establish(struct qdio_initialize *init_data);
106
107extern int qdio_activate(struct ccw_device *,int flags);
108
109#define QDIO_STATE_MUST_USE_OUTB_PCI 0x00000001
110#define QDIO_STATE_INACTIVE 0x00000002 /* after qdio_cleanup */
111#define QDIO_STATE_ESTABLISHED 0x00000004 /* after qdio_initialize */
112#define QDIO_STATE_ACTIVE 0x00000008 /* after qdio_activate */
113#define QDIO_STATE_STOPPED 0x00000010 /* after queues went down */
114extern unsigned long qdio_get_status(int irq);
115
116
117#define QDIO_FLAG_SYNC_INPUT 0x01
118#define QDIO_FLAG_SYNC_OUTPUT 0x02
119#define QDIO_FLAG_UNDER_INTERRUPT 0x04
120#define QDIO_FLAG_NO_INPUT_INTERRUPT_CONTEXT 0x08 /* no effect on
121 adapter interrupts */
122#define QDIO_FLAG_DONT_SIGA 0x10
123#define QDIO_FLAG_PCI_OUT 0x20
124
125extern int do_QDIO(struct ccw_device*, unsigned int flags,
126 unsigned int queue_number,
127 unsigned int qidx,unsigned int count,
128 struct qdio_buffer *buffers);
129
130extern int qdio_get_ssqd_pct(struct ccw_device*);
131extern int qdio_synchronize(struct ccw_device*, unsigned int flags,
132 unsigned int queue_number);
133
134extern int qdio_cleanup(struct ccw_device*, int how);
135extern int qdio_shutdown(struct ccw_device*, int how);
136extern int qdio_free(struct ccw_device*);
137
138unsigned char qdio_get_slsb_state(struct ccw_device*, unsigned int flag,
139 unsigned int queue_number,
140 unsigned int qidx);
141
142extern void qdio_init_scrubber(void);
143
144struct qdesfmt0 { 36struct qdesfmt0 {
145#ifdef QDIO_32_BIT 37 u64 sliba;
146 unsigned long res1; /* reserved */ 38 u64 sla;
147#endif /* QDIO_32_BIT */ 39 u64 slsba;
148 unsigned long sliba; /* storage-list-information-block 40 u32 : 32;
149 address */ 41 u32 akey : 4;
150#ifdef QDIO_32_BIT 42 u32 bkey : 4;
151 unsigned long res2; /* reserved */ 43 u32 ckey : 4;
152#endif /* QDIO_32_BIT */ 44 u32 dkey : 4;
153 unsigned long sla; /* storage-list address */ 45 u32 : 16;
154#ifdef QDIO_32_BIT
155 unsigned long res3; /* reserved */
156#endif /* QDIO_32_BIT */
157 unsigned long slsba; /* storage-list-state-block address */
158 unsigned int res4; /* reserved */
159 unsigned int akey : 4; /* access key for DLIB */
160 unsigned int bkey : 4; /* access key for SL */
161 unsigned int ckey : 4; /* access key for SBALs */
162 unsigned int dkey : 4; /* access key for SLSB */
163 unsigned int res5 : 16; /* reserved */
164} __attribute__ ((packed)); 46} __attribute__ ((packed));
165 47
166/* 48/**
167 * Queue-Description record (QDR) 49 * struct qdr - queue description record (QDR)
50 * @qfmt: queue format
51 * @pfmt: implementation dependent parameter format
52 * @ac: adapter characteristics
53 * @iqdcnt: input queue descriptor count
54 * @oqdcnt: output queue descriptor count
55 * @iqdsz: inpout queue descriptor size
56 * @oqdsz: output queue descriptor size
57 * @qiba: queue information block address
58 * @qkey: queue information block key
59 * @qdf0: queue descriptions
168 */ 60 */
169struct qdr { 61struct qdr {
170 unsigned int qfmt : 8; /* queue format */ 62 u32 qfmt : 8;
171 unsigned int pfmt : 8; /* impl. dep. parameter format */ 63 u32 pfmt : 8;
172 unsigned int res1 : 8; /* reserved */ 64 u32 : 8;
173 unsigned int ac : 8; /* adapter characteristics */ 65 u32 ac : 8;
174 unsigned int res2 : 8; /* reserved */ 66 u32 : 8;
175 unsigned int iqdcnt : 8; /* input-queue-descriptor count */ 67 u32 iqdcnt : 8;
176 unsigned int res3 : 8; /* reserved */ 68 u32 : 8;
177 unsigned int oqdcnt : 8; /* output-queue-descriptor count */ 69 u32 oqdcnt : 8;
178 unsigned int res4 : 8; /* reserved */ 70 u32 : 8;
179 unsigned int iqdsz : 8; /* input-queue-descriptor size */ 71 u32 iqdsz : 8;
180 unsigned int res5 : 8; /* reserved */ 72 u32 : 8;
181 unsigned int oqdsz : 8; /* output-queue-descriptor size */ 73 u32 oqdsz : 8;
182 unsigned int res6[9]; /* reserved */ 74 /* private: */
183#ifdef QDIO_32_BIT 75 u32 res[9];
184 unsigned long res7; /* reserved */ 76 /* public: */
185#endif /* QDIO_32_BIT */ 77 u64 qiba;
186 unsigned long qiba; /* queue-information-block address */ 78 u32 : 32;
187 unsigned int res8; /* reserved */ 79 u32 qkey : 4;
188 unsigned int qkey : 4; /* queue-information-block key */ 80 u32 : 28;
189 unsigned int res9 : 28; /* reserved */ 81 struct qdesfmt0 qdf0[126];
190/* union _qd {*/ /* why this? */ 82} __attribute__ ((packed, aligned(4096)));
191 struct qdesfmt0 qdf0[126]; 83
192/* } qd;*/ 84#define QIB_AC_OUTBOUND_PCI_SUPPORTED 0x40
193} __attribute__ ((packed,aligned(4096)));
194
195
196/*
197 * queue information block (QIB)
198 */
199#define QIB_AC_INBOUND_PCI_SUPPORTED 0x80
200#define QIB_AC_OUTBOUND_PCI_SUPPORTED 0x40
201#define QIB_RFLAGS_ENABLE_QEBSM 0x80 85#define QIB_RFLAGS_ENABLE_QEBSM 0x80
202 86
87/**
88 * struct qib - queue information block (QIB)
89 * @qfmt: queue format
90 * @pfmt: implementation dependent parameter format
91 * @rflags: QEBSM
92 * @ac: adapter characteristics
93 * @isliba: absolute address of first input SLIB
94 * @osliba: absolute address of first output SLIB
95 * @ebcnam: adapter identifier in EBCDIC
96 * @parm: implementation dependent parameters
97 */
203struct qib { 98struct qib {
204 unsigned int qfmt : 8; /* queue format */ 99 u32 qfmt : 8;
205 unsigned int pfmt : 8; /* impl. dep. parameter format */ 100 u32 pfmt : 8;
206 unsigned int rflags : 8; /* QEBSM */ 101 u32 rflags : 8;
207 unsigned int ac : 8; /* adapter characteristics */ 102 u32 ac : 8;
208 unsigned int res2; /* reserved */ 103 u32 : 32;
209#ifdef QDIO_32_BIT 104 u64 isliba;
210 unsigned long res3; /* reserved */ 105 u64 osliba;
211#endif /* QDIO_32_BIT */ 106 u32 : 32;
212 unsigned long isliba; /* absolute address of 1st 107 u32 : 32;
213 input SLIB */ 108 u8 ebcnam[8];
214#ifdef QDIO_32_BIT 109 /* private: */
215 unsigned long res4; /* reserved */ 110 u8 res[88];
216#endif /* QDIO_32_BIT */ 111 /* public: */
217 unsigned long osliba; /* absolute address of 1st 112 u8 parm[QDIO_MAX_BUFFERS_PER_Q];
218 output SLIB */ 113} __attribute__ ((packed, aligned(256)));
219 unsigned int res5; /* reserved */ 114
220 unsigned int res6; /* reserved */ 115/**
221 unsigned char ebcnam[8]; /* adapter identifier in EBCDIC */ 116 * struct slibe - storage list information block element (SLIBE)
222 unsigned char res7[88]; /* reserved */ 117 * @parms: implementation dependent parameters
223 unsigned char parm[QDIO_MAX_BUFFERS_PER_Q];
224 /* implementation dependent
225 parameters */
226} __attribute__ ((packed,aligned(256)));
227
228
229/*
230 * storage-list-information block element (SLIBE)
231 */ 118 */
232struct slibe { 119struct slibe {
233#ifdef QDIO_32_BIT 120 u64 parms;
234 unsigned long res; /* reserved */
235#endif /* QDIO_32_BIT */
236 unsigned long parms; /* implementation dependent
237 parameters */
238}; 121};
239 122
240/* 123/**
241 * storage-list-information block (SLIB) 124 * struct slib - storage list information block (SLIB)
125 * @nsliba: next SLIB address (if any)
126 * @sla: SL address
127 * @slsba: SLSB address
128 * @slibe: SLIB elements
242 */ 129 */
243struct slib { 130struct slib {
244#ifdef QDIO_32_BIT 131 u64 nsliba;
245 unsigned long res1; /* reserved */ 132 u64 sla;
246#endif /* QDIO_32_BIT */ 133 u64 slsba;
247 unsigned long nsliba; /* next SLIB address (if any) */ 134 /* private: */
248#ifdef QDIO_32_BIT 135 u8 res[1000];
249 unsigned long res2; /* reserved */ 136 /* public: */
250#endif /* QDIO_32_BIT */ 137 struct slibe slibe[QDIO_MAX_BUFFERS_PER_Q];
251 unsigned long sla; /* SL address */ 138} __attribute__ ((packed, aligned(2048)));
252#ifdef QDIO_32_BIT 139
253 unsigned long res3; /* reserved */ 140/**
254#endif /* QDIO_32_BIT */ 141 * struct sbal_flags - storage block address list flags
255 unsigned long slsba; /* SLSB address */ 142 * @last: last entry
256 unsigned char res4[1000]; /* reserved */ 143 * @cont: contiguous storage
257 struct slibe slibe[QDIO_MAX_BUFFERS_PER_Q]; /* SLIB elements */ 144 * @frag: fragmentation
258} __attribute__ ((packed,aligned(2048))); 145 */
259
260struct sbal_flags { 146struct sbal_flags {
261 unsigned char res1 : 1; /* reserved */ 147 u8 : 1;
262 unsigned char last : 1; /* last entry */ 148 u8 last : 1;
263 unsigned char cont : 1; /* contiguous storage */ 149 u8 cont : 1;
264 unsigned char res2 : 1; /* reserved */ 150 u8 : 1;
265 unsigned char frag : 2; /* fragmentation (s.below) */ 151 u8 frag : 2;
266 unsigned char res3 : 2; /* reserved */ 152 u8 : 2;
267} __attribute__ ((packed)); 153} __attribute__ ((packed));
268 154
269#define SBAL_FLAGS_FIRST_FRAG 0x04000000UL 155#define SBAL_FLAGS_FIRST_FRAG 0x04000000UL
270#define SBAL_FLAGS_MIDDLE_FRAG 0x08000000UL 156#define SBAL_FLAGS_MIDDLE_FRAG 0x08000000UL
271#define SBAL_FLAGS_LAST_FRAG 0x0c000000UL 157#define SBAL_FLAGS_LAST_FRAG 0x0c000000UL
272#define SBAL_FLAGS_LAST_ENTRY 0x40000000UL 158#define SBAL_FLAGS_LAST_ENTRY 0x40000000UL
273#define SBAL_FLAGS_CONTIGUOUS 0x20000000UL 159#define SBAL_FLAGS_CONTIGUOUS 0x20000000UL
274 160
275#define SBAL_FLAGS0_DATA_CONTINUATION 0x20UL 161#define SBAL_FLAGS0_DATA_CONTINUATION 0x20UL
276 162
277/* Awesome OpenFCP extensions */ 163/* Awesome OpenFCP extensions */
278#define SBAL_FLAGS0_TYPE_STATUS 0x00UL 164#define SBAL_FLAGS0_TYPE_STATUS 0x00UL
279#define SBAL_FLAGS0_TYPE_WRITE 0x08UL 165#define SBAL_FLAGS0_TYPE_WRITE 0x08UL
280#define SBAL_FLAGS0_TYPE_READ 0x10UL 166#define SBAL_FLAGS0_TYPE_READ 0x10UL
281#define SBAL_FLAGS0_TYPE_WRITE_READ 0x18UL 167#define SBAL_FLAGS0_TYPE_WRITE_READ 0x18UL
282#define SBAL_FLAGS0_MORE_SBALS 0x04UL 168#define SBAL_FLAGS0_MORE_SBALS 0x04UL
283#define SBAL_FLAGS0_COMMAND 0x02UL 169#define SBAL_FLAGS0_COMMAND 0x02UL
284#define SBAL_FLAGS0_LAST_SBAL 0x00UL 170#define SBAL_FLAGS0_LAST_SBAL 0x00UL
285#define SBAL_FLAGS0_ONLY_SBAL SBAL_FLAGS0_COMMAND 171#define SBAL_FLAGS0_ONLY_SBAL SBAL_FLAGS0_COMMAND
286#define SBAL_FLAGS0_MIDDLE_SBAL SBAL_FLAGS0_MORE_SBALS 172#define SBAL_FLAGS0_MIDDLE_SBAL SBAL_FLAGS0_MORE_SBALS
287#define SBAL_FLAGS0_FIRST_SBAL SBAL_FLAGS0_MORE_SBALS | SBAL_FLAGS0_COMMAND 173#define SBAL_FLAGS0_FIRST_SBAL SBAL_FLAGS0_MORE_SBALS | SBAL_FLAGS0_COMMAND
288/* Naught of interest beyond this point */ 174#define SBAL_FLAGS0_PCI 0x40
289 175
290#define SBAL_FLAGS0_PCI 0x40 176/**
177 * struct sbal_sbalf_0 - sbal flags for sbale 0
178 * @pci: PCI indicator
179 * @cont: data continuation
180 * @sbtype: storage-block type (FCP)
181 */
291struct sbal_sbalf_0 { 182struct sbal_sbalf_0 {
292 unsigned char res1 : 1; /* reserved */ 183 u8 : 1;
293 unsigned char pci : 1; /* PCI indicator */ 184 u8 pci : 1;
294 unsigned char cont : 1; /* data continuation */ 185 u8 cont : 1;
295 unsigned char sbtype: 2; /* storage-block type (OpenFCP) */ 186 u8 sbtype : 2;
296 unsigned char res2 : 3; /* reserved */ 187 u8 : 3;
297} __attribute__ ((packed)); 188} __attribute__ ((packed));
298 189
190/**
191 * struct sbal_sbalf_1 - sbal flags for sbale 1
192 * @key: storage key
193 */
299struct sbal_sbalf_1 { 194struct sbal_sbalf_1 {
300 unsigned char res1 : 4; /* reserved */ 195 u8 : 4;
301 unsigned char key : 4; /* storage key */ 196 u8 key : 4;
302} __attribute__ ((packed)); 197} __attribute__ ((packed));
303 198
199/**
200 * struct sbal_sbalf_14 - sbal flags for sbale 14
201 * @erridx: error index
202 */
304struct sbal_sbalf_14 { 203struct sbal_sbalf_14 {
305 unsigned char res1 : 4; /* reserved */ 204 u8 : 4;
306 unsigned char erridx : 4; /* error index */ 205 u8 erridx : 4;
307} __attribute__ ((packed)); 206} __attribute__ ((packed));
308 207
208/**
209 * struct sbal_sbalf_15 - sbal flags for sbale 15
210 * @reason: reason for error state
211 */
309struct sbal_sbalf_15 { 212struct sbal_sbalf_15 {
310 unsigned char reason; /* reserved */ 213 u8 reason;
311} __attribute__ ((packed)); 214} __attribute__ ((packed));
312 215
216/**
217 * union sbal_sbalf - storage block address list flags
218 * @i0: sbalf0
219 * @i1: sbalf1
220 * @i14: sbalf14
221 * @i15: sblaf15
222 * @value: raw value
223 */
313union sbal_sbalf { 224union sbal_sbalf {
314 struct sbal_sbalf_0 i0; 225 struct sbal_sbalf_0 i0;
315 struct sbal_sbalf_1 i1; 226 struct sbal_sbalf_1 i1;
316 struct sbal_sbalf_14 i14; 227 struct sbal_sbalf_14 i14;
317 struct sbal_sbalf_15 i15; 228 struct sbal_sbalf_15 i15;
318 unsigned char value; 229 u8 value;
319}; 230};
320 231
321struct sbal_element { 232/**
322 union { 233 * struct qdio_buffer_element - SBAL entry
323 struct sbal_flags bits; /* flags */ 234 * @flags: flags
324 unsigned char value; 235 * @length: length
325 } flags; 236 * @addr: address
326 unsigned int res1 : 16; /* reserved */ 237*/
327 union sbal_sbalf sbalf; /* SBAL flags */ 238struct qdio_buffer_element {
328 unsigned int res2 : 16; /* reserved */ 239 u32 flags;
329 unsigned int count : 16; /* data count */ 240 u32 length;
330#ifdef QDIO_32_BIT 241#ifdef CONFIG_32BIT
331 unsigned long res3; /* reserved */ 242 /* private: */
332#endif /* QDIO_32_BIT */ 243 void *reserved;
333 unsigned long addr; /* absolute data address */ 244 /* public: */
334} __attribute__ ((packed,aligned(16))); 245#endif
246 void *addr;
247} __attribute__ ((packed, aligned(16)));
335 248
336/* 249/**
337 * strorage-block access-list (SBAL) 250 * struct qdio_buffer - storage block address list (SBAL)
251 * @element: SBAL entries
338 */ 252 */
339struct sbal { 253struct qdio_buffer {
340 struct sbal_element element[QDIO_MAX_ELEMENTS_PER_BUFFER]; 254 struct qdio_buffer_element element[QDIO_MAX_ELEMENTS_PER_BUFFER];
341} __attribute__ ((packed,aligned(256))); 255} __attribute__ ((packed, aligned(256)));
342 256
343/* 257/**
344 * storage-list (SL) 258 * struct sl_element - storage list entry
259 * @sbal: absolute SBAL address
345 */ 260 */
346struct sl_element { 261struct sl_element {
347#ifdef QDIO_32_BIT 262#ifdef CONFIG_32BIT
348 unsigned long res; /* reserved */ 263 /* private: */
349#endif /* QDIO_32_BIT */ 264 unsigned long reserved;
350 unsigned long sbal; /* absolute SBAL address */ 265 /* public: */
266#endif
267 unsigned long sbal;
351} __attribute__ ((packed)); 268} __attribute__ ((packed));
352 269
270/**
271 * struct sl - storage list (SL)
272 * @element: SL entries
273 */
353struct sl { 274struct sl {
354 struct sl_element element[QDIO_MAX_BUFFERS_PER_Q]; 275 struct sl_element element[QDIO_MAX_BUFFERS_PER_Q];
355} __attribute__ ((packed,aligned(1024))); 276} __attribute__ ((packed, aligned(1024)));
356 277
357/* 278/**
358 * storage-list-state block (SLSB) 279 * struct slsb - storage list state block (SLSB)
280 * @val: state per buffer
359 */ 281 */
360struct slsb_flags { 282struct slsb {
361 unsigned char owner : 2; /* SBAL owner */ 283 u8 val[QDIO_MAX_BUFFERS_PER_Q];
362 unsigned char type : 1; /* buffer type */ 284} __attribute__ ((packed, aligned(256)));
363 unsigned char state : 5; /* processing state */ 285
286struct qdio_ssqd_desc {
287 u8 flags;
288 u8:8;
289 u16 sch;
290 u8 qfmt;
291 u8 parm;
292 u8 qdioac1;
293 u8 sch_class;
294 u8 pcnt;
295 u8 icnt;
296 u8:8;
297 u8 ocnt;
298 u8:8;
299 u8 mbccnt;
300 u16 qdioac2;
301 u64 sch_token;
302 u64:64;
364} __attribute__ ((packed)); 303} __attribute__ ((packed));
365 304
305/* params are: ccw_device, qdio_error, queue_number,
306 first element processed, number of elements processed, int_parm */
307typedef void qdio_handler_t(struct ccw_device *, unsigned int, int,
308 int, int, unsigned long);
366 309
367struct slsb { 310/* qdio errors reported to the upper-layer program */
368 union { 311#define QDIO_ERROR_SIGA_ACCESS_EXCEPTION 0x10
369 unsigned char val[QDIO_MAX_BUFFERS_PER_Q]; 312#define QDIO_ERROR_SIGA_BUSY 0x20
370 struct slsb_flags flags[QDIO_MAX_BUFFERS_PER_Q]; 313#define QDIO_ERROR_ACTIVATE_CHECK_CONDITION 0x40
371 } acc; 314#define QDIO_ERROR_SLSB_STATE 0x80
372} __attribute__ ((packed,aligned(256)));
373 315
374/* 316/* for qdio_initialize */
375 * SLSB values 317#define QDIO_INBOUND_0COPY_SBALS 0x01
318#define QDIO_OUTBOUND_0COPY_SBALS 0x02
319#define QDIO_USE_OUTBOUND_PCIS 0x04
320
321/* for qdio_cleanup */
322#define QDIO_FLAG_CLEANUP_USING_CLEAR 0x01
323#define QDIO_FLAG_CLEANUP_USING_HALT 0x02
324
325/**
326 * struct qdio_initialize - qdio initalization data
327 * @cdev: associated ccw device
328 * @q_format: queue format
329 * @adapter_name: name for the adapter
330 * @qib_param_field_format: format for qib_parm_field
331 * @qib_param_field: pointer to 128 bytes or NULL, if no param field
332 * @input_slib_elements: pointer to no_input_qs * 128 words of data or NULL
333 * @output_slib_elements: pointer to no_output_qs * 128 words of data or NULL
334 * @no_input_qs: number of input queues
335 * @no_output_qs: number of output queues
336 * @input_handler: handler to be called for input queues
337 * @output_handler: handler to be called for output queues
338 * @int_parm: interruption parameter
339 * @flags: initialization flags
340 * @input_sbal_addr_array: address of no_input_qs * 128 pointers
341 * @output_sbal_addr_array: address of no_output_qs * 128 pointers
376 */ 342 */
377#define SLSB_OWNER_PROG 1 343struct qdio_initialize {
378#define SLSB_OWNER_CU 2 344 struct ccw_device *cdev;
379 345 unsigned char q_format;
380#define SLSB_TYPE_INPUT 0 346 unsigned char adapter_name[8];
381#define SLSB_TYPE_OUTPUT 1 347 unsigned int qib_param_field_format;
382 348 unsigned char *qib_param_field;
383#define SLSB_STATE_NOT_INIT 0 349 unsigned long *input_slib_elements;
384#define SLSB_STATE_EMPTY 1 350 unsigned long *output_slib_elements;
385#define SLSB_STATE_PRIMED 2 351 unsigned int no_input_qs;
386#define SLSB_STATE_HALTED 0xe 352 unsigned int no_output_qs;
387#define SLSB_STATE_ERROR 0xf 353 qdio_handler_t *input_handler;
388 354 qdio_handler_t *output_handler;
389#define SLSB_P_INPUT_NOT_INIT 0x80 355 unsigned long int_parm;
390#define SLSB_P_INPUT_PROCESSING 0x81 356 unsigned long flags;
391#define SLSB_CU_INPUT_EMPTY 0x41 357 void **input_sbal_addr_array;
392#define SLSB_P_INPUT_PRIMED 0x82 358 void **output_sbal_addr_array;
393#define SLSB_P_INPUT_HALTED 0x8E 359};
394#define SLSB_P_INPUT_ERROR 0x8F 360
395 361#define QDIO_STATE_INACTIVE 0x00000002 /* after qdio_cleanup */
396#define SLSB_P_OUTPUT_NOT_INIT 0xA0 362#define QDIO_STATE_ESTABLISHED 0x00000004 /* after qdio_establish */
397#define SLSB_P_OUTPUT_EMPTY 0xA1 363#define QDIO_STATE_ACTIVE 0x00000008 /* after qdio_activate */
398#define SLSB_CU_OUTPUT_PRIMED 0x62 364#define QDIO_STATE_STOPPED 0x00000010 /* after queues went down */
399#define SLSB_P_OUTPUT_HALTED 0xAE 365
400#define SLSB_P_OUTPUT_ERROR 0xAF 366#define QDIO_FLAG_SYNC_INPUT 0x01
401 367#define QDIO_FLAG_SYNC_OUTPUT 0x02
402#define SLSB_ERROR_DURING_LOOKUP 0xFF 368#define QDIO_FLAG_PCI_OUT 0x10
369
370extern int qdio_initialize(struct qdio_initialize *init_data);
371extern int qdio_allocate(struct qdio_initialize *init_data);
372extern int qdio_establish(struct qdio_initialize *init_data);
373extern int qdio_activate(struct ccw_device *);
374
375extern int do_QDIO(struct ccw_device*, unsigned int flags,
376 int q_nr, int qidx, int count);
377extern int qdio_cleanup(struct ccw_device*, int how);
378extern int qdio_shutdown(struct ccw_device*, int how);
379extern int qdio_free(struct ccw_device *);
380extern struct qdio_ssqd_desc *qdio_get_ssqd_desc(struct ccw_device *cdev);
403 381
404#endif /* __QDIO_H__ */ 382#endif /* __QDIO_H__ */
diff --git a/include/asm-s390/schid.h b/include/asm-s390/schid.h
index 5017ffa78e04..7bdc0fe15691 100644
--- a/include/asm-s390/schid.h
+++ b/include/asm-s390/schid.h
@@ -10,6 +10,7 @@ struct subchannel_id {
10 __u32 sch_no : 16; 10 __u32 sch_no : 16;
11} __attribute__ ((packed, aligned(4))); 11} __attribute__ ((packed, aligned(4)));
12 12
13#ifdef __KERNEL__
13 14
14/* Helper function for sane state of pre-allocated subchannel_id. */ 15/* Helper function for sane state of pre-allocated subchannel_id. */
15static inline void 16static inline void
@@ -25,4 +26,6 @@ schid_equal(struct subchannel_id *schid1, struct subchannel_id *schid2)
25 return !memcmp(schid1, schid2, sizeof(struct subchannel_id)); 26 return !memcmp(schid1, schid2, sizeof(struct subchannel_id));
26} 27}
27 28
29#endif /* __KERNEL__ */
30
28#endif /* ASM_SCHID_H */ 31#endif /* ASM_SCHID_H */
diff --git a/include/asm-s390/setup.h b/include/asm-s390/setup.h
index f09ee3f72977..4ba14e463e83 100644
--- a/include/asm-s390/setup.h
+++ b/include/asm-s390/setup.h
@@ -65,6 +65,7 @@ extern unsigned long machine_flags;
65 65
66#define MACHINE_FLAG_VM (1UL << 0) 66#define MACHINE_FLAG_VM (1UL << 0)
67#define MACHINE_FLAG_IEEE (1UL << 1) 67#define MACHINE_FLAG_IEEE (1UL << 1)
68#define MACHINE_FLAG_P390 (1UL << 2)
68#define MACHINE_FLAG_CSP (1UL << 3) 69#define MACHINE_FLAG_CSP (1UL << 3)
69#define MACHINE_FLAG_MVPG (1UL << 4) 70#define MACHINE_FLAG_MVPG (1UL << 4)
70#define MACHINE_FLAG_DIAG44 (1UL << 5) 71#define MACHINE_FLAG_DIAG44 (1UL << 5)
@@ -77,7 +78,6 @@ extern unsigned long machine_flags;
77 78
78#define MACHINE_IS_VM (machine_flags & MACHINE_FLAG_VM) 79#define MACHINE_IS_VM (machine_flags & MACHINE_FLAG_VM)
79#define MACHINE_IS_KVM (machine_flags & MACHINE_FLAG_KVM) 80#define MACHINE_IS_KVM (machine_flags & MACHINE_FLAG_KVM)
80#define MACHINE_IS_P390 (machine_flags & MACHINE_FLAG_P390)
81#define MACHINE_HAS_DIAG9C (machine_flags & MACHINE_FLAG_DIAG9C) 81#define MACHINE_HAS_DIAG9C (machine_flags & MACHINE_FLAG_DIAG9C)
82 82
83#ifndef __s390x__ 83#ifndef __s390x__
diff --git a/include/asm-x86/pci-direct.h b/include/asm-x86/pci-direct.h
index 5b21485be573..80c775d9fe20 100644
--- a/include/asm-x86/pci-direct.h
+++ b/include/asm-x86/pci-direct.h
@@ -11,7 +11,11 @@ extern u8 read_pci_config_byte(u8 bus, u8 slot, u8 func, u8 offset);
11extern u16 read_pci_config_16(u8 bus, u8 slot, u8 func, u8 offset); 11extern u16 read_pci_config_16(u8 bus, u8 slot, u8 func, u8 offset);
12extern void write_pci_config(u8 bus, u8 slot, u8 func, u8 offset, u32 val); 12extern void write_pci_config(u8 bus, u8 slot, u8 func, u8 offset, u32 val);
13extern void write_pci_config_byte(u8 bus, u8 slot, u8 func, u8 offset, u8 val); 13extern void write_pci_config_byte(u8 bus, u8 slot, u8 func, u8 offset, u8 val);
14extern void write_pci_config_16(u8 bus, u8 slot, u8 func, u8 offset, u16 val);
14 15
15extern int early_pci_allowed(void); 16extern int early_pci_allowed(void);
16 17
18extern unsigned int pci_early_dump_regs;
19extern void early_dump_pci_device(u8 bus, u8 slot, u8 func);
20extern void early_dump_pci_devices(void);
17#endif 21#endif
diff --git a/include/asm-x86/processor.h b/include/asm-x86/processor.h
index 7f7382704592..55402d2ab938 100644
--- a/include/asm-x86/processor.h
+++ b/include/asm-x86/processor.h
@@ -727,6 +727,8 @@ extern int force_mwait;
727extern void select_idle_routine(const struct cpuinfo_x86 *c); 727extern void select_idle_routine(const struct cpuinfo_x86 *c);
728 728
729extern unsigned long boot_option_idle_override; 729extern unsigned long boot_option_idle_override;
730extern unsigned long idle_halt;
731extern unsigned long idle_nomwait;
730 732
731extern void enable_sep_cpu(void); 733extern void enable_sep_cpu(void);
732extern int sysenter_setup(void); 734extern int sysenter_setup(void);
diff --git a/include/linux/acpi.h b/include/linux/acpi.h
index 0601075d09a1..a17177639376 100644
--- a/include/linux/acpi.h
+++ b/include/linux/acpi.h
@@ -235,6 +235,9 @@ int acpi_check_region(resource_size_t start, resource_size_t n,
235int acpi_check_mem_region(resource_size_t start, resource_size_t n, 235int acpi_check_mem_region(resource_size_t start, resource_size_t n,
236 const char *name); 236 const char *name);
237 237
238#ifdef CONFIG_PM_SLEEP
239void __init acpi_old_suspend_ordering(void);
240#endif /* CONFIG_PM_SLEEP */
238#else /* CONFIG_ACPI */ 241#else /* CONFIG_ACPI */
239 242
240static inline int early_acpi_boot_init(void) 243static inline int early_acpi_boot_init(void)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 32a441b05fd5..88d68081a0f1 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -985,6 +985,9 @@ static inline int bdev_integrity_enabled(struct block_device *bdev, int rw)
985 985
986static inline int blk_integrity_rq(struct request *rq) 986static inline int blk_integrity_rq(struct request *rq)
987{ 987{
988 if (rq->bio == NULL)
989 return 0;
990
988 return bio_integrity(rq->bio); 991 return bio_integrity(rq->bio);
989} 992}
990 993
diff --git a/include/linux/device.h b/include/linux/device.h
index 6a2d04c011bc..f71a78d123ae 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -68,6 +68,8 @@ struct bus_type {
68 int (*resume_early)(struct device *dev); 68 int (*resume_early)(struct device *dev);
69 int (*resume)(struct device *dev); 69 int (*resume)(struct device *dev);
70 70
71 struct pm_ext_ops *pm;
72
71 struct bus_type_private *p; 73 struct bus_type_private *p;
72}; 74};
73 75
@@ -131,6 +133,8 @@ struct device_driver {
131 int (*resume) (struct device *dev); 133 int (*resume) (struct device *dev);
132 struct attribute_group **groups; 134 struct attribute_group **groups;
133 135
136 struct pm_ops *pm;
137
134 struct driver_private *p; 138 struct driver_private *p;
135}; 139};
136 140
@@ -197,6 +201,8 @@ struct class {
197 201
198 int (*suspend)(struct device *dev, pm_message_t state); 202 int (*suspend)(struct device *dev, pm_message_t state);
199 int (*resume)(struct device *dev); 203 int (*resume)(struct device *dev);
204
205 struct pm_ops *pm;
200}; 206};
201 207
202extern int __must_check class_register(struct class *class); 208extern int __must_check class_register(struct class *class);
@@ -248,8 +254,11 @@ struct device_type {
248 struct attribute_group **groups; 254 struct attribute_group **groups;
249 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 255 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
250 void (*release)(struct device *dev); 256 void (*release)(struct device *dev);
257
251 int (*suspend)(struct device *dev, pm_message_t state); 258 int (*suspend)(struct device *dev, pm_message_t state);
252 int (*resume)(struct device *dev); 259 int (*resume)(struct device *dev);
260
261 struct pm_ops *pm;
253}; 262};
254 263
255/* interface for exporting device attributes */ 264/* interface for exporting device attributes */
diff --git a/include/linux/freezer.h b/include/linux/freezer.h
index 08934995c7ab..deddeedf3257 100644
--- a/include/linux/freezer.h
+++ b/include/linux/freezer.h
@@ -128,6 +128,15 @@ static inline void set_freezable(void)
128} 128}
129 129
130/* 130/*
131 * Tell the freezer that the current task should be frozen by it and that it
132 * should send a fake signal to the task to freeze it.
133 */
134static inline void set_freezable_with_signal(void)
135{
136 current->flags &= ~(PF_NOFREEZE | PF_FREEZER_NOSIG);
137}
138
139/*
131 * Freezer-friendly wrappers around wait_event_interruptible() and 140 * Freezer-friendly wrappers around wait_event_interruptible() and
132 * wait_event_interruptible_timeout(), originally defined in <linux/wait.h> 141 * wait_event_interruptible_timeout(), originally defined in <linux/wait.h>
133 */ 142 */
@@ -174,6 +183,7 @@ static inline void freezer_do_not_count(void) {}
174static inline void freezer_count(void) {} 183static inline void freezer_count(void) {}
175static inline int freezer_should_skip(struct task_struct *p) { return 0; } 184static inline int freezer_should_skip(struct task_struct *p) { return 0; }
176static inline void set_freezable(void) {} 185static inline void set_freezable(void) {}
186static inline void set_freezable_with_signal(void) {}
177 187
178#define wait_event_freezable(wq, condition) \ 188#define wait_event_freezable(wq, condition) \
179 wait_event_interruptible(wq, condition) 189 wait_event_interruptible(wq, condition)
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 52e510a0aec2..c6455dadb21b 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1729,6 +1729,8 @@ static inline void invalidate_remote_inode(struct inode *inode)
1729extern int invalidate_inode_pages2(struct address_space *mapping); 1729extern int invalidate_inode_pages2(struct address_space *mapping);
1730extern int invalidate_inode_pages2_range(struct address_space *mapping, 1730extern int invalidate_inode_pages2_range(struct address_space *mapping,
1731 pgoff_t start, pgoff_t end); 1731 pgoff_t start, pgoff_t end);
1732extern void generic_sync_sb_inodes(struct super_block *sb,
1733 struct writeback_control *wbc);
1732extern int write_inode_now(struct inode *, int); 1734extern int write_inode_now(struct inode *, int);
1733extern int filemap_fdatawrite(struct address_space *); 1735extern int filemap_fdatawrite(struct address_space *);
1734extern int filemap_flush(struct address_space *); 1736extern int filemap_flush(struct address_space *);
diff --git a/include/linux/ide.h b/include/linux/ide.h
index ac4eeb2932ef..4726126f5a59 100644
--- a/include/linux/ide.h
+++ b/include/linux/ide.h
@@ -139,6 +139,12 @@ struct ide_io_ports {
139#define WAIT_MIN_SLEEP (2*HZ/100) /* 20msec - minimum sleep time */ 139#define WAIT_MIN_SLEEP (2*HZ/100) /* 20msec - minimum sleep time */
140 140
141/* 141/*
142 * Op codes for special requests to be handled by ide_special_rq().
143 * Values should be in the range of 0x20 to 0x3f.
144 */
145#define REQ_DRIVE_RESET 0x20
146
147/*
142 * Check for an interrupt and acknowledge the interrupt status 148 * Check for an interrupt and acknowledge the interrupt status
143 */ 149 */
144struct hwif_s; 150struct hwif_s;
@@ -171,7 +177,7 @@ typedef struct hw_regs_s {
171 int irq; /* our irq number */ 177 int irq; /* our irq number */
172 ide_ack_intr_t *ack_intr; /* acknowledge interrupt */ 178 ide_ack_intr_t *ack_intr; /* acknowledge interrupt */
173 hwif_chipset_t chipset; 179 hwif_chipset_t chipset;
174 struct device *dev; 180 struct device *dev, *parent;
175} hw_regs_t; 181} hw_regs_t;
176 182
177void ide_init_port_data(struct hwif_s *, unsigned int); 183void ide_init_port_data(struct hwif_s *, unsigned int);
@@ -405,8 +411,8 @@ typedef struct ide_drive_s {
405struct ide_port_info; 411struct ide_port_info;
406 412
407struct ide_port_ops { 413struct ide_port_ops {
408 /* host specific initialization of devices on a port */ 414 /* host specific initialization of a device */
409 void (*port_init_devs)(struct hwif_s *); 415 void (*init_dev)(ide_drive_t *);
410 /* routine to program host for PIO mode */ 416 /* routine to program host for PIO mode */
411 void (*set_pio_mode)(ide_drive_t *, const u8); 417 void (*set_pio_mode)(ide_drive_t *, const u8);
412 /* routine to program host for DMA mode */ 418 /* routine to program host for DMA mode */
@@ -565,8 +571,6 @@ typedef struct hwgroup_s {
565 unsigned int sleeping : 1; 571 unsigned int sleeping : 1;
566 /* BOOL: polling active & poll_timeout field valid */ 572 /* BOOL: polling active & poll_timeout field valid */
567 unsigned int polling : 1; 573 unsigned int polling : 1;
568 /* BOOL: in a polling reset situation. Must not trigger another reset yet */
569 unsigned int resetting : 1;
570 574
571 /* current drive */ 575 /* current drive */
572 ide_drive_t *drive; 576 ide_drive_t *drive;
@@ -786,7 +790,6 @@ struct ide_driver_s {
786 ide_startstop_t (*do_request)(ide_drive_t *, struct request *, sector_t); 790 ide_startstop_t (*do_request)(ide_drive_t *, struct request *, sector_t);
787 int (*end_request)(ide_drive_t *, int, int); 791 int (*end_request)(ide_drive_t *, int, int);
788 ide_startstop_t (*error)(ide_drive_t *, struct request *rq, u8, u8); 792 ide_startstop_t (*error)(ide_drive_t *, struct request *rq, u8, u8);
789 ide_startstop_t (*abort)(ide_drive_t *, struct request *rq);
790 struct device_driver gen_driver; 793 struct device_driver gen_driver;
791 int (*probe)(ide_drive_t *); 794 int (*probe)(ide_drive_t *);
792 void (*remove)(ide_drive_t *); 795 void (*remove)(ide_drive_t *);
@@ -801,18 +804,6 @@ struct ide_driver_s {
801 804
802int generic_ide_ioctl(ide_drive_t *, struct file *, struct block_device *, unsigned, unsigned long); 805int generic_ide_ioctl(ide_drive_t *, struct file *, struct block_device *, unsigned, unsigned long);
803 806
804/*
805 * ide_hwifs[] is the master data structure used to keep track
806 * of just about everything in ide.c. Whenever possible, routines
807 * should be using pointers to a drive (ide_drive_t *) or
808 * pointers to a hwif (ide_hwif_t *), rather than indexing this
809 * structure directly (the allocation/layout may change!).
810 *
811 */
812#ifndef _IDE_C
813extern ide_hwif_t ide_hwifs[]; /* master data repository */
814#endif
815
816extern int ide_vlb_clk; 807extern int ide_vlb_clk;
817extern int ide_pci_clk; 808extern int ide_pci_clk;
818 809
@@ -840,10 +831,6 @@ ide_startstop_t __ide_error(ide_drive_t *, struct request *, u8, u8);
840 831
841ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, byte stat); 832ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, byte stat);
842 833
843ide_startstop_t __ide_abort(ide_drive_t *, struct request *);
844
845extern ide_startstop_t ide_abort(ide_drive_t *, const char *);
846
847extern void ide_fix_driveid(struct hd_driveid *); 834extern void ide_fix_driveid(struct hd_driveid *);
848 835
849extern void ide_fixstring(u8 *, const int, const int); 836extern void ide_fixstring(u8 *, const int, const int);
@@ -1271,16 +1258,43 @@ static inline int ide_dev_is_sata(struct hd_driveid *id)
1271u64 ide_get_lba_addr(struct ide_taskfile *, int); 1258u64 ide_get_lba_addr(struct ide_taskfile *, int);
1272u8 ide_dump_status(ide_drive_t *, const char *, u8); 1259u8 ide_dump_status(ide_drive_t *, const char *, u8);
1273 1260
1274typedef struct ide_pio_timings_s { 1261struct ide_timing {
1275 int setup_time; /* Address setup (ns) minimum */ 1262 u8 mode;
1276 int active_time; /* Active pulse (ns) minimum */ 1263 u8 setup; /* t1 */
1277 int cycle_time; /* Cycle time (ns) minimum = */ 1264 u16 act8b; /* t2 for 8-bit io */
1278 /* active + recovery (+ setup for some chips) */ 1265 u16 rec8b; /* t2i for 8-bit io */
1279} ide_pio_timings_t; 1266 u16 cyc8b; /* t0 for 8-bit io */
1267 u16 active; /* t2 or tD */
1268 u16 recover; /* t2i or tK */
1269 u16 cycle; /* t0 */
1270 u16 udma; /* t2CYCTYP/2 */
1271};
1272
1273enum {
1274 IDE_TIMING_SETUP = (1 << 0),
1275 IDE_TIMING_ACT8B = (1 << 1),
1276 IDE_TIMING_REC8B = (1 << 2),
1277 IDE_TIMING_CYC8B = (1 << 3),
1278 IDE_TIMING_8BIT = IDE_TIMING_ACT8B | IDE_TIMING_REC8B |
1279 IDE_TIMING_CYC8B,
1280 IDE_TIMING_ACTIVE = (1 << 4),
1281 IDE_TIMING_RECOVER = (1 << 5),
1282 IDE_TIMING_CYCLE = (1 << 6),
1283 IDE_TIMING_UDMA = (1 << 7),
1284 IDE_TIMING_ALL = IDE_TIMING_SETUP | IDE_TIMING_8BIT |
1285 IDE_TIMING_ACTIVE | IDE_TIMING_RECOVER |
1286 IDE_TIMING_CYCLE | IDE_TIMING_UDMA,
1287};
1288
1289struct ide_timing *ide_timing_find_mode(u8);
1290u16 ide_pio_cycle_time(ide_drive_t *, u8);
1291void ide_timing_merge(struct ide_timing *, struct ide_timing *,
1292 struct ide_timing *, unsigned int);
1293int ide_timing_compute(ide_drive_t *, u8, struct ide_timing *, int, int);
1294
1295int ide_scan_pio_blacklist(char *);
1280 1296
1281unsigned int ide_pio_cycle_time(ide_drive_t *, u8);
1282u8 ide_get_best_pio_mode(ide_drive_t *, u8, u8); 1297u8 ide_get_best_pio_mode(ide_drive_t *, u8, u8);
1283extern const ide_pio_timings_t ide_pio_timings[6];
1284 1298
1285int ide_set_pio_mode(ide_drive_t *, u8); 1299int ide_set_pio_mode(ide_drive_t *, u8);
1286int ide_set_dma_mode(ide_drive_t *, u8); 1300int ide_set_dma_mode(ide_drive_t *, u8);
diff --git a/include/linux/inet.h b/include/linux/inet.h
index 1354080cf8cf..4cca05c9678e 100644
--- a/include/linux/inet.h
+++ b/include/linux/inet.h
@@ -44,6 +44,13 @@
44 44
45#include <linux/types.h> 45#include <linux/types.h>
46 46
47/*
48 * These mimic similar macros defined in user-space for inet_ntop(3).
49 * See /usr/include/netinet/in.h .
50 */
51#define INET_ADDRSTRLEN (16)
52#define INET6_ADDRSTRLEN (48)
53
47extern __be32 in_aton(const char *str); 54extern __be32 in_aton(const char *str);
48extern int in4_pton(const char *src, int srclen, u8 *dst, int delim, const char **end); 55extern int in4_pton(const char *src, int srclen, u8 *dst, int delim, const char **end);
49extern int in6_pton(const char *src, int srclen, u8 *dst, int delim, const char **end); 56extern int in6_pton(const char *src, int srclen, u8 *dst, int delim, const char **end);
diff --git a/include/linux/init_task.h b/include/linux/init_task.h
index 9927a88674a3..93c45acf249a 100644
--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -140,8 +140,8 @@ extern struct group_info init_groups;
140 .nr_cpus_allowed = NR_CPUS, \ 140 .nr_cpus_allowed = NR_CPUS, \
141 }, \ 141 }, \
142 .tasks = LIST_HEAD_INIT(tsk.tasks), \ 142 .tasks = LIST_HEAD_INIT(tsk.tasks), \
143 .ptrace_children= LIST_HEAD_INIT(tsk.ptrace_children), \ 143 .ptraced = LIST_HEAD_INIT(tsk.ptraced), \
144 .ptrace_list = LIST_HEAD_INIT(tsk.ptrace_list), \ 144 .ptrace_entry = LIST_HEAD_INIT(tsk.ptrace_entry), \
145 .real_parent = &tsk, \ 145 .real_parent = &tsk, \
146 .parent = &tsk, \ 146 .parent = &tsk, \
147 .children = LIST_HEAD_INIT(tsk.children), \ 147 .children = LIST_HEAD_INIT(tsk.children), \
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index c6801bffe76d..2cd07cc29687 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -59,6 +59,7 @@ struct resource_list {
59#define IORESOURCE_IRQ_HIGHLEVEL (1<<2) 59#define IORESOURCE_IRQ_HIGHLEVEL (1<<2)
60#define IORESOURCE_IRQ_LOWLEVEL (1<<3) 60#define IORESOURCE_IRQ_LOWLEVEL (1<<3)
61#define IORESOURCE_IRQ_SHAREABLE (1<<4) 61#define IORESOURCE_IRQ_SHAREABLE (1<<4)
62#define IORESOURCE_IRQ_OPTIONAL (1<<5)
62 63
63/* PnP DMA specific bits (IORESOURCE_BITS) */ 64/* PnP DMA specific bits (IORESOURCE_BITS) */
64#define IORESOURCE_DMA_TYPE_MASK (3<<0) 65#define IORESOURCE_DMA_TYPE_MASK (3<<0)
@@ -88,6 +89,10 @@ struct resource_list {
88#define IORESOURCE_MEM_SHADOWABLE (1<<5) /* dup: IORESOURCE_SHADOWABLE */ 89#define IORESOURCE_MEM_SHADOWABLE (1<<5) /* dup: IORESOURCE_SHADOWABLE */
89#define IORESOURCE_MEM_EXPANSIONROM (1<<6) 90#define IORESOURCE_MEM_EXPANSIONROM (1<<6)
90 91
92/* PnP I/O specific bits (IORESOURCE_BITS) */
93#define IORESOURCE_IO_16BIT_ADDR (1<<0)
94#define IORESOURCE_IO_FIXED (1<<1)
95
91/* PCI ROM control bits (IORESOURCE_BITS) */ 96/* PCI ROM control bits (IORESOURCE_BITS) */
92#define IORESOURCE_ROM_ENABLE (1<<0) /* ROM is enabled, same as PCI_ROM_ADDRESS_ENABLE */ 97#define IORESOURCE_ROM_ENABLE (1<<0) /* ROM is enabled, same as PCI_ROM_ADDRESS_ENABLE */
93#define IORESOURCE_ROM_SHADOW (1<<1) /* ROM is copy at C000:0 */ 98#define IORESOURCE_ROM_SHADOW (1<<1) /* ROM is copy at C000:0 */
diff --git a/include/linux/mmc/core.h b/include/linux/mmc/core.h
index d0c3abed74c2..143cebf0586f 100644
--- a/include/linux/mmc/core.h
+++ b/include/linux/mmc/core.h
@@ -135,6 +135,7 @@ extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
135 struct mmc_command *, int); 135 struct mmc_command *, int);
136 136
137extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *); 137extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *);
138extern unsigned int mmc_align_data_size(struct mmc_card *, unsigned int);
138 139
139extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort); 140extern int __mmc_claim_host(struct mmc_host *host, atomic_t *abort);
140extern void mmc_release_host(struct mmc_host *host); 141extern void mmc_release_host(struct mmc_host *host);
diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
index 7ab962fa1d73..10a2080086ca 100644
--- a/include/linux/mmc/host.h
+++ b/include/linux/mmc/host.h
@@ -51,8 +51,30 @@ struct mmc_ios {
51 51
52struct mmc_host_ops { 52struct mmc_host_ops {
53 void (*request)(struct mmc_host *host, struct mmc_request *req); 53 void (*request)(struct mmc_host *host, struct mmc_request *req);
54 /*
55 * Avoid calling these three functions too often or in a "fast path",
56 * since underlaying controller might implement them in an expensive
57 * and/or slow way.
58 *
59 * Also note that these functions might sleep, so don't call them
60 * in the atomic contexts!
61 *
62 * Return values for the get_ro callback should be:
63 * 0 for a read/write card
64 * 1 for a read-only card
65 * -ENOSYS when not supported (equal to NULL callback)
66 * or a negative errno value when something bad happened
67 *
68 * Return values for the get_ro callback should be:
69 * 0 for a absent card
70 * 1 for a present card
71 * -ENOSYS when not supported (equal to NULL callback)
72 * or a negative errno value when something bad happened
73 */
54 void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios); 74 void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);
55 int (*get_ro)(struct mmc_host *host); 75 int (*get_ro)(struct mmc_host *host);
76 int (*get_cd)(struct mmc_host *host);
77
56 void (*enable_sdio_irq)(struct mmc_host *host, int enable); 78 void (*enable_sdio_irq)(struct mmc_host *host, int enable);
57}; 79};
58 80
@@ -89,11 +111,11 @@ struct mmc_host {
89 unsigned long caps; /* Host capabilities */ 111 unsigned long caps; /* Host capabilities */
90 112
91#define MMC_CAP_4_BIT_DATA (1 << 0) /* Can the host do 4 bit transfers */ 113#define MMC_CAP_4_BIT_DATA (1 << 0) /* Can the host do 4 bit transfers */
92#define MMC_CAP_MULTIWRITE (1 << 1) /* Can accurately report bytes sent to card on error */ 114#define MMC_CAP_MMC_HIGHSPEED (1 << 1) /* Can do MMC high-speed timing */
93#define MMC_CAP_MMC_HIGHSPEED (1 << 2) /* Can do MMC high-speed timing */ 115#define MMC_CAP_SD_HIGHSPEED (1 << 2) /* Can do SD high-speed timing */
94#define MMC_CAP_SD_HIGHSPEED (1 << 3) /* Can do SD high-speed timing */ 116#define MMC_CAP_SDIO_IRQ (1 << 3) /* Can signal pending SDIO IRQs */
95#define MMC_CAP_SDIO_IRQ (1 << 4) /* Can signal pending SDIO IRQs */ 117#define MMC_CAP_SPI (1 << 4) /* Talks only SPI protocols */
96#define MMC_CAP_SPI (1 << 5) /* Talks only SPI protocols */ 118#define MMC_CAP_NEEDS_POLL (1 << 5) /* Needs polling for card-detection */
97 119
98 /* host specific block data */ 120 /* host specific block data */
99 unsigned int max_seg_size; /* see blk_queue_max_segment_size */ 121 unsigned int max_seg_size; /* see blk_queue_max_segment_size */
diff --git a/include/linux/mmc/mmc.h b/include/linux/mmc/mmc.h
index 4236fbf0b6fb..14b81f3e5232 100644
--- a/include/linux/mmc/mmc.h
+++ b/include/linux/mmc/mmc.h
@@ -16,7 +16,6 @@
16 * Based strongly on code by: 16 * Based strongly on code by:
17 * 17 *
18 * Author: Yong-iL Joh <tolkien@mizi.com> 18 * Author: Yong-iL Joh <tolkien@mizi.com>
19 * Date : $Date: 2002/06/18 12:37:30 $
20 * 19 *
21 * Author: Andrew Christian 20 * Author: Andrew Christian
22 * 15 May 2002 21 * 15 May 2002
diff --git a/include/linux/mmc/sdio_func.h b/include/linux/mmc/sdio_func.h
index b050f4d7b41f..07bee4a0d457 100644
--- a/include/linux/mmc/sdio_func.h
+++ b/include/linux/mmc/sdio_func.h
@@ -1,7 +1,7 @@
1/* 1/*
2 * include/linux/mmc/sdio_func.h 2 * include/linux/mmc/sdio_func.h
3 * 3 *
4 * Copyright 2007 Pierre Ossman 4 * Copyright 2007-2008 Pierre Ossman
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
@@ -46,6 +46,8 @@ struct sdio_func {
46 unsigned max_blksize; /* maximum block size */ 46 unsigned max_blksize; /* maximum block size */
47 unsigned cur_blksize; /* current block size */ 47 unsigned cur_blksize; /* current block size */
48 48
49 unsigned enable_timeout; /* max enable timeout in msec */
50
49 unsigned int state; /* function state */ 51 unsigned int state; /* function state */
50#define SDIO_STATE_PRESENT (1<<0) /* present in sysfs */ 52#define SDIO_STATE_PRESENT (1<<0) /* present in sysfs */
51 53
@@ -120,23 +122,22 @@ extern int sdio_set_block_size(struct sdio_func *func, unsigned blksz);
120extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler); 122extern int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler);
121extern int sdio_release_irq(struct sdio_func *func); 123extern int sdio_release_irq(struct sdio_func *func);
122 124
123extern unsigned char sdio_readb(struct sdio_func *func, 125extern unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz);
124 unsigned int addr, int *err_ret); 126
125extern unsigned short sdio_readw(struct sdio_func *func, 127extern u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret);
126 unsigned int addr, int *err_ret); 128extern u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret);
127extern unsigned long sdio_readl(struct sdio_func *func, 129extern u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret);
128 unsigned int addr, int *err_ret);
129 130
130extern int sdio_memcpy_fromio(struct sdio_func *func, void *dst, 131extern int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
131 unsigned int addr, int count); 132 unsigned int addr, int count);
132extern int sdio_readsb(struct sdio_func *func, void *dst, 133extern int sdio_readsb(struct sdio_func *func, void *dst,
133 unsigned int addr, int count); 134 unsigned int addr, int count);
134 135
135extern void sdio_writeb(struct sdio_func *func, unsigned char b, 136extern void sdio_writeb(struct sdio_func *func, u8 b,
136 unsigned int addr, int *err_ret); 137 unsigned int addr, int *err_ret);
137extern void sdio_writew(struct sdio_func *func, unsigned short b, 138extern void sdio_writew(struct sdio_func *func, u16 b,
138 unsigned int addr, int *err_ret); 139 unsigned int addr, int *err_ret);
139extern void sdio_writel(struct sdio_func *func, unsigned long b, 140extern void sdio_writel(struct sdio_func *func, u32 b,
140 unsigned int addr, int *err_ret); 141 unsigned int addr, int *err_ret);
141 142
142extern int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr, 143extern int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
diff --git a/include/linux/nfs_fs.h b/include/linux/nfs_fs.h
index 27d6a8d98cef..29d261918734 100644
--- a/include/linux/nfs_fs.h
+++ b/include/linux/nfs_fs.h
@@ -12,9 +12,19 @@
12#include <linux/magic.h> 12#include <linux/magic.h>
13 13
14/* Default timeout values */ 14/* Default timeout values */
15#define NFS_DEF_UDP_TIMEO (11)
16#define NFS_DEF_UDP_RETRANS (3)
17#define NFS_DEF_TCP_TIMEO (600)
18#define NFS_DEF_TCP_RETRANS (2)
19
15#define NFS_MAX_UDP_TIMEOUT (60*HZ) 20#define NFS_MAX_UDP_TIMEOUT (60*HZ)
16#define NFS_MAX_TCP_TIMEOUT (600*HZ) 21#define NFS_MAX_TCP_TIMEOUT (600*HZ)
17 22
23#define NFS_DEF_ACREGMIN (3)
24#define NFS_DEF_ACREGMAX (60)
25#define NFS_DEF_ACDIRMIN (30)
26#define NFS_DEF_ACDIRMAX (60)
27
18/* 28/*
19 * When flushing a cluster of dirty pages, there can be different 29 * When flushing a cluster of dirty pages, there can be different
20 * strategies: 30 * strategies:
diff --git a/include/linux/nfs_iostat.h b/include/linux/nfs_iostat.h
new file mode 100644
index 000000000000..1cb9a3fed2b3
--- /dev/null
+++ b/include/linux/nfs_iostat.h
@@ -0,0 +1,119 @@
1/*
2 * User-space visible declarations for NFS client per-mount
3 * point statistics
4 *
5 * Copyright (C) 2005, 2006 Chuck Lever <cel@netapp.com>
6 *
7 * NFS client per-mount statistics provide information about the
8 * health of the NFS client and the health of each NFS mount point.
9 * Generally these are not for detailed problem diagnosis, but
10 * simply to indicate that there is a problem.
11 *
12 * These counters are not meant to be human-readable, but are meant
13 * to be integrated into system monitoring tools such as "sar" and
14 * "iostat". As such, the counters are sampled by the tools over
15 * time, and are never zeroed after a file system is mounted.
16 * Moving averages can be computed by the tools by taking the
17 * difference between two instantaneous samples and dividing that
18 * by the time between the samples.
19 */
20
21#ifndef _LINUX_NFS_IOSTAT
22#define _LINUX_NFS_IOSTAT
23
24#define NFS_IOSTAT_VERS "1.0"
25
26/*
27 * NFS byte counters
28 *
29 * 1. SERVER - the number of payload bytes read from or written
30 * to the server by the NFS client via an NFS READ or WRITE
31 * request.
32 *
33 * 2. NORMAL - the number of bytes read or written by applications
34 * via the read(2) and write(2) system call interfaces.
35 *
36 * 3. DIRECT - the number of bytes read or written from files
37 * opened with the O_DIRECT flag.
38 *
39 * These counters give a view of the data throughput into and out
40 * of the NFS client. Comparing the number of bytes requested by
41 * an application with the number of bytes the client requests from
42 * the server can provide an indication of client efficiency
43 * (per-op, cache hits, etc).
44 *
45 * These counters can also help characterize which access methods
46 * are in use. DIRECT by itself shows whether there is any O_DIRECT
47 * traffic. NORMAL + DIRECT shows how much data is going through
48 * the system call interface. A large amount of SERVER traffic
49 * without much NORMAL or DIRECT traffic shows that applications
50 * are using mapped files.
51 *
52 * NFS page counters
53 *
54 * These count the number of pages read or written via nfs_readpage(),
55 * nfs_readpages(), or their write equivalents.
56 *
57 * NB: When adding new byte counters, please include the measured
58 * units in the name of each byte counter to help users of this
59 * interface determine what exactly is being counted.
60 */
61enum nfs_stat_bytecounters {
62 NFSIOS_NORMALREADBYTES = 0,
63 NFSIOS_NORMALWRITTENBYTES,
64 NFSIOS_DIRECTREADBYTES,
65 NFSIOS_DIRECTWRITTENBYTES,
66 NFSIOS_SERVERREADBYTES,
67 NFSIOS_SERVERWRITTENBYTES,
68 NFSIOS_READPAGES,
69 NFSIOS_WRITEPAGES,
70 __NFSIOS_BYTESMAX,
71};
72
73/*
74 * NFS event counters
75 *
76 * These counters provide a low-overhead way of monitoring client
77 * activity without enabling NFS trace debugging. The counters
78 * show the rate at which VFS requests are made, and how often the
79 * client invalidates its data and attribute caches. This allows
80 * system administrators to monitor such things as how close-to-open
81 * is working, and answer questions such as "why are there so many
82 * GETATTR requests on the wire?"
83 *
84 * They also count anamolous events such as short reads and writes,
85 * silly renames due to close-after-delete, and operations that
86 * change the size of a file (such operations can often be the
87 * source of data corruption if applications aren't using file
88 * locking properly).
89 */
90enum nfs_stat_eventcounters {
91 NFSIOS_INODEREVALIDATE = 0,
92 NFSIOS_DENTRYREVALIDATE,
93 NFSIOS_DATAINVALIDATE,
94 NFSIOS_ATTRINVALIDATE,
95 NFSIOS_VFSOPEN,
96 NFSIOS_VFSLOOKUP,
97 NFSIOS_VFSACCESS,
98 NFSIOS_VFSUPDATEPAGE,
99 NFSIOS_VFSREADPAGE,
100 NFSIOS_VFSREADPAGES,
101 NFSIOS_VFSWRITEPAGE,
102 NFSIOS_VFSWRITEPAGES,
103 NFSIOS_VFSGETDENTS,
104 NFSIOS_VFSSETATTR,
105 NFSIOS_VFSFLUSH,
106 NFSIOS_VFSFSYNC,
107 NFSIOS_VFSLOCK,
108 NFSIOS_VFSRELEASE,
109 NFSIOS_CONGESTIONWAIT,
110 NFSIOS_SETATTRTRUNC,
111 NFSIOS_EXTENDWRITE,
112 NFSIOS_SILLYRENAME,
113 NFSIOS_SHORTREAD,
114 NFSIOS_SHORTWRITE,
115 NFSIOS_DELAY,
116 __NFSIOS_COUNTSMAX,
117};
118
119#endif /* _LINUX_NFS_IOSTAT */
diff --git a/include/linux/nfs_page.h b/include/linux/nfs_page.h
index a1676e19e491..3c60685d972b 100644
--- a/include/linux/nfs_page.h
+++ b/include/linux/nfs_page.h
@@ -27,9 +27,12 @@
27/* 27/*
28 * Valid flags for a dirty buffer 28 * Valid flags for a dirty buffer
29 */ 29 */
30#define PG_BUSY 0 30enum {
31#define PG_NEED_COMMIT 1 31 PG_BUSY = 0,
32#define PG_NEED_RESCHED 2 32 PG_CLEAN,
33 PG_NEED_COMMIT,
34 PG_NEED_RESCHED,
35};
33 36
34struct nfs_inode; 37struct nfs_inode;
35struct nfs_page { 38struct nfs_page {
diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h
index 24263bb8e0be..8c77c11224d1 100644
--- a/include/linux/nfs_xdr.h
+++ b/include/linux/nfs_xdr.h
@@ -829,9 +829,8 @@ struct nfs_rpc_ops {
829 int (*write_done) (struct rpc_task *, struct nfs_write_data *); 829 int (*write_done) (struct rpc_task *, struct nfs_write_data *);
830 void (*commit_setup) (struct nfs_write_data *, struct rpc_message *); 830 void (*commit_setup) (struct nfs_write_data *, struct rpc_message *);
831 int (*commit_done) (struct rpc_task *, struct nfs_write_data *); 831 int (*commit_done) (struct rpc_task *, struct nfs_write_data *);
832 int (*file_open) (struct inode *, struct file *);
833 int (*file_release) (struct inode *, struct file *);
834 int (*lock)(struct file *, int, struct file_lock *); 832 int (*lock)(struct file *, int, struct file_lock *);
833 int (*lock_check_bounds)(const struct file_lock *);
835 void (*clear_acl_cache)(struct inode *); 834 void (*clear_acl_cache)(struct inode *);
836}; 835};
837 836
diff --git a/include/linux/pci.h b/include/linux/pci.h
index d18b1dd49fab..a6a088e1a804 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -17,8 +17,7 @@
17#ifndef LINUX_PCI_H 17#ifndef LINUX_PCI_H
18#define LINUX_PCI_H 18#define LINUX_PCI_H
19 19
20/* Include the pci register defines */ 20#include <linux/pci_regs.h> /* The pci register defines */
21#include <linux/pci_regs.h>
22 21
23/* 22/*
24 * The PCI interface treats multi-function devices as independent 23 * The PCI interface treats multi-function devices as independent
@@ -49,12 +48,22 @@
49#include <linux/list.h> 48#include <linux/list.h>
50#include <linux/compiler.h> 49#include <linux/compiler.h>
51#include <linux/errno.h> 50#include <linux/errno.h>
51#include <linux/kobject.h>
52#include <asm/atomic.h> 52#include <asm/atomic.h>
53#include <linux/device.h> 53#include <linux/device.h>
54 54
55/* Include the ID list */ 55/* Include the ID list */
56#include <linux/pci_ids.h> 56#include <linux/pci_ids.h>
57 57
58/* pci_slot represents a physical slot */
59struct pci_slot {
60 struct pci_bus *bus; /* The bus this slot is on */
61 struct list_head list; /* node in list of slots on this bus */
62 struct hotplug_slot *hotplug; /* Hotplug info (migrate over time) */
63 unsigned char number; /* PCI_SLOT(pci_dev->devfn) */
64 struct kobject kobj;
65};
66
58/* File state for mmap()s on /proc/bus/pci/X/Y */ 67/* File state for mmap()s on /proc/bus/pci/X/Y */
59enum pci_mmap_state { 68enum pci_mmap_state {
60 pci_mmap_io, 69 pci_mmap_io,
@@ -142,6 +151,7 @@ struct pci_dev {
142 151
143 void *sysdata; /* hook for sys-specific extension */ 152 void *sysdata; /* hook for sys-specific extension */
144 struct proc_dir_entry *procent; /* device entry in /proc/bus/pci */ 153 struct proc_dir_entry *procent; /* device entry in /proc/bus/pci */
154 struct pci_slot *slot; /* Physical slot this device is in */
145 155
146 unsigned int devfn; /* encoded device & function index */ 156 unsigned int devfn; /* encoded device & function index */
147 unsigned short vendor; 157 unsigned short vendor;
@@ -167,6 +177,13 @@ struct pci_dev {
167 pci_power_t current_state; /* Current operating state. In ACPI-speak, 177 pci_power_t current_state; /* Current operating state. In ACPI-speak,
168 this is D0-D3, D0 being fully functional, 178 this is D0-D3, D0 being fully functional,
169 and D3 being off. */ 179 and D3 being off. */
180 int pm_cap; /* PM capability offset in the
181 configuration space */
182 unsigned int pme_support:5; /* Bitmask of states from which PME#
183 can be generated */
184 unsigned int d1_support:1; /* Low power state D1 is supported */
185 unsigned int d2_support:1; /* Low power state D2 is supported */
186 unsigned int no_d1d2:1; /* Only allow D0 and D3 */
170 187
171#ifdef CONFIG_PCIEASPM 188#ifdef CONFIG_PCIEASPM
172 struct pcie_link_state *link_state; /* ASPM link state. */ 189 struct pcie_link_state *link_state; /* ASPM link state. */
@@ -191,7 +208,6 @@ struct pci_dev {
191 unsigned int is_added:1; 208 unsigned int is_added:1;
192 unsigned int is_busmaster:1; /* device is busmaster */ 209 unsigned int is_busmaster:1; /* device is busmaster */
193 unsigned int no_msi:1; /* device may not use msi */ 210 unsigned int no_msi:1; /* device may not use msi */
194 unsigned int no_d1d2:1; /* only allow d0 or d3 */
195 unsigned int block_ucfg_access:1; /* userspace config space access is blocked */ 211 unsigned int block_ucfg_access:1; /* userspace config space access is blocked */
196 unsigned int broken_parity_status:1; /* Device generates false positive parity */ 212 unsigned int broken_parity_status:1; /* Device generates false positive parity */
197 unsigned int msi_enabled:1; 213 unsigned int msi_enabled:1;
@@ -267,6 +283,7 @@ struct pci_bus {
267 struct list_head children; /* list of child buses */ 283 struct list_head children; /* list of child buses */
268 struct list_head devices; /* list of devices on this bus */ 284 struct list_head devices; /* list of devices on this bus */
269 struct pci_dev *self; /* bridge device as seen by parent */ 285 struct pci_dev *self; /* bridge device as seen by parent */
286 struct list_head slots; /* list of slots on this bus */
270 struct resource *resource[PCI_BUS_NUM_RESOURCES]; 287 struct resource *resource[PCI_BUS_NUM_RESOURCES];
271 /* address space routed to this bus */ 288 /* address space routed to this bus */
272 289
@@ -328,7 +345,7 @@ struct pci_bus_region {
328struct pci_dynids { 345struct pci_dynids {
329 spinlock_t lock; /* protects list, index */ 346 spinlock_t lock; /* protects list, index */
330 struct list_head list; /* for IDs added at runtime */ 347 struct list_head list; /* for IDs added at runtime */
331 unsigned int use_driver_data:1; /* pci_driver->driver_data is used */ 348 unsigned int use_driver_data:1; /* pci_device_id->driver_data is used */
332}; 349};
333 350
334/* ---------------------------------------------------------------- */ 351/* ---------------------------------------------------------------- */
@@ -390,7 +407,7 @@ struct pci_driver {
390 int (*resume_early) (struct pci_dev *dev); 407 int (*resume_early) (struct pci_dev *dev);
391 int (*resume) (struct pci_dev *dev); /* Device woken up */ 408 int (*resume) (struct pci_dev *dev); /* Device woken up */
392 void (*shutdown) (struct pci_dev *dev); 409 void (*shutdown) (struct pci_dev *dev);
393 410 struct pm_ext_ops *pm;
394 struct pci_error_handlers *err_handler; 411 struct pci_error_handlers *err_handler;
395 struct device_driver driver; 412 struct device_driver driver;
396 struct pci_dynids dynids; 413 struct pci_dynids dynids;
@@ -489,6 +506,10 @@ struct pci_bus *pci_create_bus(struct device *parent, int bus,
489 struct pci_ops *ops, void *sysdata); 506 struct pci_ops *ops, void *sysdata);
490struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, 507struct pci_bus *pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev,
491 int busnr); 508 int busnr);
509struct pci_slot *pci_create_slot(struct pci_bus *parent, int slot_nr,
510 const char *name);
511void pci_destroy_slot(struct pci_slot *slot);
512void pci_update_slot_number(struct pci_slot *slot, int slot_nr);
492int pci_scan_slot(struct pci_bus *bus, int devfn); 513int pci_scan_slot(struct pci_bus *bus, int devfn);
493struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn); 514struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn);
494void pci_device_add(struct pci_dev *dev, struct pci_bus *bus); 515void pci_device_add(struct pci_dev *dev, struct pci_bus *bus);
@@ -618,6 +639,8 @@ int pci_restore_state(struct pci_dev *dev);
618int pci_set_power_state(struct pci_dev *dev, pci_power_t state); 639int pci_set_power_state(struct pci_dev *dev, pci_power_t state);
619pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state); 640pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state);
620int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable); 641int pci_enable_wake(struct pci_dev *dev, pci_power_t state, int enable);
642int pci_prepare_to_sleep(struct pci_dev *dev);
643int pci_back_from_sleep(struct pci_dev *dev);
621 644
622/* Functions for PCI Hotplug drivers to use */ 645/* Functions for PCI Hotplug drivers to use */
623int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap); 646int pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap);
@@ -839,6 +862,11 @@ static inline int pci_set_dma_mask(struct pci_dev *dev, u64 mask)
839 return -EIO; 862 return -EIO;
840} 863}
841 864
865static inline int pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
866{
867 return -EIO;
868}
869
842static inline int pci_set_dma_max_seg_size(struct pci_dev *dev, 870static inline int pci_set_dma_max_seg_size(struct pci_dev *dev,
843 unsigned int size) 871 unsigned int size)
844{ 872{
@@ -977,9 +1005,9 @@ static inline void pci_set_drvdata(struct pci_dev *pdev, void *data)
977/* If you want to know what to call your pci_dev, ask this function. 1005/* If you want to know what to call your pci_dev, ask this function.
978 * Again, it's a wrapper around the generic device. 1006 * Again, it's a wrapper around the generic device.
979 */ 1007 */
980static inline char *pci_name(struct pci_dev *pdev) 1008static inline const char *pci_name(struct pci_dev *pdev)
981{ 1009{
982 return pdev->dev.bus_id; 1010 return dev_name(&pdev->dev);
983} 1011}
984 1012
985 1013
@@ -1014,7 +1042,9 @@ enum pci_fixup_pass {
1014 pci_fixup_header, /* After reading configuration header */ 1042 pci_fixup_header, /* After reading configuration header */
1015 pci_fixup_final, /* Final phase of device fixups */ 1043 pci_fixup_final, /* Final phase of device fixups */
1016 pci_fixup_enable, /* pci_enable_device() time */ 1044 pci_fixup_enable, /* pci_enable_device() time */
1017 pci_fixup_resume, /* pci_enable_device() time */ 1045 pci_fixup_resume, /* pci_device_resume() */
1046 pci_fixup_suspend, /* pci_device_suspend */
1047 pci_fixup_resume_early, /* pci_device_resume_early() */
1018}; 1048};
1019 1049
1020/* Anonymous variables would be nice... */ 1050/* Anonymous variables would be nice... */
@@ -1036,6 +1066,12 @@ enum pci_fixup_pass {
1036#define DECLARE_PCI_FIXUP_RESUME(vendor, device, hook) \ 1066#define DECLARE_PCI_FIXUP_RESUME(vendor, device, hook) \
1037 DECLARE_PCI_FIXUP_SECTION(.pci_fixup_resume, \ 1067 DECLARE_PCI_FIXUP_SECTION(.pci_fixup_resume, \
1038 resume##vendor##device##hook, vendor, device, hook) 1068 resume##vendor##device##hook, vendor, device, hook)
1069#define DECLARE_PCI_FIXUP_RESUME_EARLY(vendor, device, hook) \
1070 DECLARE_PCI_FIXUP_SECTION(.pci_fixup_resume_early, \
1071 resume_early##vendor##device##hook, vendor, device, hook)
1072#define DECLARE_PCI_FIXUP_SUSPEND(vendor, device, hook) \
1073 DECLARE_PCI_FIXUP_SECTION(.pci_fixup_suspend, \
1074 suspend##vendor##device##hook, vendor, device, hook)
1039 1075
1040 1076
1041void pci_fixup_device(enum pci_fixup_pass pass, struct pci_dev *dev); 1077void pci_fixup_device(enum pci_fixup_pass pass, struct pci_dev *dev);
@@ -1060,7 +1096,10 @@ extern int pci_pci_problems;
1060extern unsigned long pci_cardbus_io_size; 1096extern unsigned long pci_cardbus_io_size;
1061extern unsigned long pci_cardbus_mem_size; 1097extern unsigned long pci_cardbus_mem_size;
1062 1098
1063extern int pcibios_add_platform_entries(struct pci_dev *dev); 1099int pcibios_add_platform_entries(struct pci_dev *dev);
1100void pcibios_disable_device(struct pci_dev *dev);
1101int pcibios_set_pcie_reset_state(struct pci_dev *dev,
1102 enum pcie_reset_state state);
1064 1103
1065#ifdef CONFIG_PCI_MMCONFIG 1104#ifdef CONFIG_PCI_MMCONFIG
1066extern void __init pci_mmcfg_early_init(void); 1105extern void __init pci_mmcfg_early_init(void);
diff --git a/include/linux/pci_hotplug.h b/include/linux/pci_hotplug.h
index 8f67e8f2a3cc..a08cd06b541a 100644
--- a/include/linux/pci_hotplug.h
+++ b/include/linux/pci_hotplug.h
@@ -95,9 +95,6 @@ struct hotplug_slot_attribute {
95 * @get_adapter_status: Called to get see if an adapter is present in the slot or not. 95 * @get_adapter_status: Called to get see if an adapter is present in the slot or not.
96 * If this field is NULL, the value passed in the struct hotplug_slot_info 96 * If this field is NULL, the value passed in the struct hotplug_slot_info
97 * will be used when this value is requested by a user. 97 * will be used when this value is requested by a user.
98 * @get_address: Called to get pci address of a slot.
99 * If this field is NULL, the value passed in the struct hotplug_slot_info
100 * will be used when this value is requested by a user.
101 * @get_max_bus_speed: Called to get the max bus speed for a slot. 98 * @get_max_bus_speed: Called to get the max bus speed for a slot.
102 * If this field is NULL, the value passed in the struct hotplug_slot_info 99 * If this field is NULL, the value passed in the struct hotplug_slot_info
103 * will be used when this value is requested by a user. 100 * will be used when this value is requested by a user.
@@ -120,7 +117,6 @@ struct hotplug_slot_ops {
120 int (*get_attention_status) (struct hotplug_slot *slot, u8 *value); 117 int (*get_attention_status) (struct hotplug_slot *slot, u8 *value);
121 int (*get_latch_status) (struct hotplug_slot *slot, u8 *value); 118 int (*get_latch_status) (struct hotplug_slot *slot, u8 *value);
122 int (*get_adapter_status) (struct hotplug_slot *slot, u8 *value); 119 int (*get_adapter_status) (struct hotplug_slot *slot, u8 *value);
123 int (*get_address) (struct hotplug_slot *slot, u32 *value);
124 int (*get_max_bus_speed) (struct hotplug_slot *slot, enum pci_bus_speed *value); 120 int (*get_max_bus_speed) (struct hotplug_slot *slot, enum pci_bus_speed *value);
125 int (*get_cur_bus_speed) (struct hotplug_slot *slot, enum pci_bus_speed *value); 121 int (*get_cur_bus_speed) (struct hotplug_slot *slot, enum pci_bus_speed *value);
126}; 122};
@@ -140,7 +136,6 @@ struct hotplug_slot_info {
140 u8 attention_status; 136 u8 attention_status;
141 u8 latch_status; 137 u8 latch_status;
142 u8 adapter_status; 138 u8 adapter_status;
143 u32 address;
144 enum pci_bus_speed max_bus_speed; 139 enum pci_bus_speed max_bus_speed;
145 enum pci_bus_speed cur_bus_speed; 140 enum pci_bus_speed cur_bus_speed;
146}; 141};
@@ -166,15 +161,14 @@ struct hotplug_slot {
166 161
167 /* Variables below this are for use only by the hotplug pci core. */ 162 /* Variables below this are for use only by the hotplug pci core. */
168 struct list_head slot_list; 163 struct list_head slot_list;
169 struct kobject kobj; 164 struct pci_slot *pci_slot;
170}; 165};
171#define to_hotplug_slot(n) container_of(n, struct hotplug_slot, kobj) 166#define to_hotplug_slot(n) container_of(n, struct hotplug_slot, kobj)
172 167
173extern int pci_hp_register (struct hotplug_slot *slot); 168extern int pci_hp_register(struct hotplug_slot *, struct pci_bus *, int nr);
174extern int pci_hp_deregister (struct hotplug_slot *slot); 169extern int pci_hp_deregister(struct hotplug_slot *slot);
175extern int __must_check pci_hp_change_slot_info (struct hotplug_slot *slot, 170extern int __must_check pci_hp_change_slot_info (struct hotplug_slot *slot,
176 struct hotplug_slot_info *info); 171 struct hotplug_slot_info *info);
177extern struct kset *pci_hotplug_slots_kset;
178 172
179/* PCI Setting Record (Type 0) */ 173/* PCI Setting Record (Type 0) */
180struct hpp_type0 { 174struct hpp_type0 {
@@ -227,9 +221,9 @@ struct hotplug_params {
227#include <acpi/acpi.h> 221#include <acpi/acpi.h>
228#include <acpi/acpi_bus.h> 222#include <acpi/acpi_bus.h>
229#include <acpi/actypes.h> 223#include <acpi/actypes.h>
230extern acpi_status acpi_run_oshp(acpi_handle handle);
231extern acpi_status acpi_get_hp_params_from_firmware(struct pci_bus *bus, 224extern acpi_status acpi_get_hp_params_from_firmware(struct pci_bus *bus,
232 struct hotplug_params *hpp); 225 struct hotplug_params *hpp);
226int acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev, u32 flags);
233int acpi_root_bridge(acpi_handle handle); 227int acpi_root_bridge(acpi_handle handle);
234#endif 228#endif
235#endif 229#endif
diff --git a/include/linux/pci_ids.h b/include/linux/pci_ids.h
index 1cf4084b51e8..6be6a7943d8b 100644
--- a/include/linux/pci_ids.h
+++ b/include/linux/pci_ids.h
@@ -2190,6 +2190,7 @@
2190#define PCI_DEVICE_ID_JMICRON_JMB366 0x2366 2190#define PCI_DEVICE_ID_JMICRON_JMB366 0x2366
2191#define PCI_DEVICE_ID_JMICRON_JMB368 0x2368 2191#define PCI_DEVICE_ID_JMICRON_JMB368 0x2368
2192#define PCI_DEVICE_ID_JMICRON_JMB38X_SD 0x2381 2192#define PCI_DEVICE_ID_JMICRON_JMB38X_SD 0x2381
2193#define PCI_DEVICE_ID_JMICRON_JMB38X_MMC 0x2382
2193#define PCI_DEVICE_ID_JMICRON_JMB38X_MS 0x2383 2194#define PCI_DEVICE_ID_JMICRON_JMB38X_MS 0x2383
2194 2195
2195#define PCI_VENDOR_ID_KORENIX 0x1982 2196#define PCI_VENDOR_ID_KORENIX 0x1982
diff --git a/include/linux/pci_regs.h b/include/linux/pci_regs.h
index c0c1223c9194..19958b929905 100644
--- a/include/linux/pci_regs.h
+++ b/include/linux/pci_regs.h
@@ -231,6 +231,7 @@
231#define PCI_PM_CAP_PME_D2 0x2000 /* PME# from D2 */ 231#define PCI_PM_CAP_PME_D2 0x2000 /* PME# from D2 */
232#define PCI_PM_CAP_PME_D3 0x4000 /* PME# from D3 (hot) */ 232#define PCI_PM_CAP_PME_D3 0x4000 /* PME# from D3 (hot) */
233#define PCI_PM_CAP_PME_D3cold 0x8000 /* PME# from D3 (cold) */ 233#define PCI_PM_CAP_PME_D3cold 0x8000 /* PME# from D3 (cold) */
234#define PCI_PM_CAP_PME_SHIFT 11 /* Start of the PME Mask in PMC */
234#define PCI_PM_CTRL 4 /* PM control and status register */ 235#define PCI_PM_CTRL 4 /* PM control and status register */
235#define PCI_PM_CTRL_STATE_MASK 0x0003 /* Current power state (D0 to D3) */ 236#define PCI_PM_CTRL_STATE_MASK 0x0003 /* Current power state (D0 to D3) */
236#define PCI_PM_CTRL_NO_SOFT_RESET 0x0004 /* No reset for D3hot->D0 */ 237#define PCI_PM_CTRL_NO_SOFT_RESET 0x0004 /* No reset for D3hot->D0 */
diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
index 3261681c82a4..95ac21ab3a09 100644
--- a/include/linux/platform_device.h
+++ b/include/linux/platform_device.h
@@ -53,6 +53,7 @@ struct platform_driver {
53 int (*suspend_late)(struct platform_device *, pm_message_t state); 53 int (*suspend_late)(struct platform_device *, pm_message_t state);
54 int (*resume_early)(struct platform_device *); 54 int (*resume_early)(struct platform_device *);
55 int (*resume)(struct platform_device *); 55 int (*resume)(struct platform_device *);
56 struct pm_ext_ops *pm;
56 struct device_driver driver; 57 struct device_driver driver;
57}; 58};
58 59
diff --git a/include/linux/pm.h b/include/linux/pm.h
index 39a7ee859b67..4ad9de94449a 100644
--- a/include/linux/pm.h
+++ b/include/linux/pm.h
@@ -112,7 +112,9 @@ typedef struct pm_message {
112 int event; 112 int event;
113} pm_message_t; 113} pm_message_t;
114 114
115/* 115/**
116 * struct pm_ops - device PM callbacks
117 *
116 * Several driver power state transitions are externally visible, affecting 118 * Several driver power state transitions are externally visible, affecting
117 * the state of pending I/O queues and (for drivers that touch hardware) 119 * the state of pending I/O queues and (for drivers that touch hardware)
118 * interrupts, wakeups, DMA, and other hardware state. There may also be 120 * interrupts, wakeups, DMA, and other hardware state. There may also be
@@ -120,6 +122,284 @@ typedef struct pm_message {
120 * to the rest of the driver stack (such as a driver that's ON gating off 122 * to the rest of the driver stack (such as a driver that's ON gating off
121 * clocks which are not in active use). 123 * clocks which are not in active use).
122 * 124 *
125 * The externally visible transitions are handled with the help of the following
126 * callbacks included in this structure:
127 *
128 * @prepare: Prepare the device for the upcoming transition, but do NOT change
129 * its hardware state. Prevent new children of the device from being
130 * registered after @prepare() returns (the driver's subsystem and
131 * generally the rest of the kernel is supposed to prevent new calls to the
132 * probe method from being made too once @prepare() has succeeded). If
133 * @prepare() detects a situation it cannot handle (e.g. registration of a
134 * child already in progress), it may return -EAGAIN, so that the PM core
135 * can execute it once again (e.g. after the new child has been registered)
136 * to recover from the race condition. This method is executed for all
137 * kinds of suspend transitions and is followed by one of the suspend
138 * callbacks: @suspend(), @freeze(), or @poweroff().
139 * The PM core executes @prepare() for all devices before starting to
140 * execute suspend callbacks for any of them, so drivers may assume all of
141 * the other devices to be present and functional while @prepare() is being
142 * executed. In particular, it is safe to make GFP_KERNEL memory
143 * allocations from within @prepare(). However, drivers may NOT assume
144 * anything about the availability of the user space at that time and it
145 * is not correct to request firmware from within @prepare() (it's too
146 * late to do that). [To work around this limitation, drivers may
147 * register suspend and hibernation notifiers that are executed before the
148 * freezing of tasks.]
149 *
150 * @complete: Undo the changes made by @prepare(). This method is executed for
151 * all kinds of resume transitions, following one of the resume callbacks:
152 * @resume(), @thaw(), @restore(). Also called if the state transition
153 * fails before the driver's suspend callback (@suspend(), @freeze(),
154 * @poweroff()) can be executed (e.g. if the suspend callback fails for one
155 * of the other devices that the PM core has unsuccessfully attempted to
156 * suspend earlier).
157 * The PM core executes @complete() after it has executed the appropriate
158 * resume callback for all devices.
159 *
160 * @suspend: Executed before putting the system into a sleep state in which the
161 * contents of main memory are preserved. Quiesce the device, put it into
162 * a low power state appropriate for the upcoming system state (such as
163 * PCI_D3hot), and enable wakeup events as appropriate.
164 *
165 * @resume: Executed after waking the system up from a sleep state in which the
166 * contents of main memory were preserved. Put the device into the
167 * appropriate state, according to the information saved in memory by the
168 * preceding @suspend(). The driver starts working again, responding to
169 * hardware events and software requests. The hardware may have gone
170 * through a power-off reset, or it may have maintained state from the
171 * previous suspend() which the driver may rely on while resuming. On most
172 * platforms, there are no restrictions on availability of resources like
173 * clocks during @resume().
174 *
175 * @freeze: Hibernation-specific, executed before creating a hibernation image.
176 * Quiesce operations so that a consistent image can be created, but do NOT
177 * otherwise put the device into a low power device state and do NOT emit
178 * system wakeup events. Save in main memory the device settings to be
179 * used by @restore() during the subsequent resume from hibernation or by
180 * the subsequent @thaw(), if the creation of the image or the restoration
181 * of main memory contents from it fails.
182 *
183 * @thaw: Hibernation-specific, executed after creating a hibernation image OR
184 * if the creation of the image fails. Also executed after a failing
185 * attempt to restore the contents of main memory from such an image.
186 * Undo the changes made by the preceding @freeze(), so the device can be
187 * operated in the same way as immediately before the call to @freeze().
188 *
189 * @poweroff: Hibernation-specific, executed after saving a hibernation image.
190 * Quiesce the device, put it into a low power state appropriate for the
191 * upcoming system state (such as PCI_D3hot), and enable wakeup events as
192 * appropriate.
193 *
194 * @restore: Hibernation-specific, executed after restoring the contents of main
195 * memory from a hibernation image. Driver starts working again,
196 * responding to hardware events and software requests. Drivers may NOT
197 * make ANY assumptions about the hardware state right prior to @restore().
198 * On most platforms, there are no restrictions on availability of
199 * resources like clocks during @restore().
200 *
201 * All of the above callbacks, except for @complete(), return error codes.
202 * However, the error codes returned by the resume operations, @resume(),
203 * @thaw(), and @restore(), do not cause the PM core to abort the resume
204 * transition during which they are returned. The error codes returned in
205 * that cases are only printed by the PM core to the system logs for debugging
206 * purposes. Still, it is recommended that drivers only return error codes
207 * from their resume methods in case of an unrecoverable failure (i.e. when the
208 * device being handled refuses to resume and becomes unusable) to allow us to
209 * modify the PM core in the future, so that it can avoid attempting to handle
210 * devices that failed to resume and their children.
211 *
212 * It is allowed to unregister devices while the above callbacks are being
213 * executed. However, it is not allowed to unregister a device from within any
214 * of its own callbacks.
215 */
216
217struct pm_ops {
218 int (*prepare)(struct device *dev);
219 void (*complete)(struct device *dev);
220 int (*suspend)(struct device *dev);
221 int (*resume)(struct device *dev);
222 int (*freeze)(struct device *dev);
223 int (*thaw)(struct device *dev);
224 int (*poweroff)(struct device *dev);
225 int (*restore)(struct device *dev);
226};
227
228/**
229 * struct pm_ext_ops - extended device PM callbacks
230 *
231 * Some devices require certain operations related to suspend and hibernation
232 * to be carried out with interrupts disabled. Thus, 'struct pm_ext_ops' below
233 * is defined, adding callbacks to be executed with interrupts disabled to
234 * 'struct pm_ops'.
235 *
236 * The following callbacks included in 'struct pm_ext_ops' are executed with
237 * the nonboot CPUs switched off and with interrupts disabled on the only
238 * functional CPU. They also are executed with the PM core list of devices
239 * locked, so they must NOT unregister any devices.
240 *
241 * @suspend_noirq: Complete the operations of ->suspend() by carrying out any
242 * actions required for suspending the device that need interrupts to be
243 * disabled
244 *
245 * @resume_noirq: Prepare for the execution of ->resume() by carrying out any
246 * actions required for resuming the device that need interrupts to be
247 * disabled
248 *
249 * @freeze_noirq: Complete the operations of ->freeze() by carrying out any
250 * actions required for freezing the device that need interrupts to be
251 * disabled
252 *
253 * @thaw_noirq: Prepare for the execution of ->thaw() by carrying out any
254 * actions required for thawing the device that need interrupts to be
255 * disabled
256 *
257 * @poweroff_noirq: Complete the operations of ->poweroff() by carrying out any
258 * actions required for handling the device that need interrupts to be
259 * disabled
260 *
261 * @restore_noirq: Prepare for the execution of ->restore() by carrying out any
262 * actions required for restoring the operations of the device that need
263 * interrupts to be disabled
264 *
265 * All of the above callbacks return error codes, but the error codes returned
266 * by the resume operations, @resume_noirq(), @thaw_noirq(), and
267 * @restore_noirq(), do not cause the PM core to abort the resume transition
268 * during which they are returned. The error codes returned in that cases are
269 * only printed by the PM core to the system logs for debugging purposes.
270 * Still, as stated above, it is recommended that drivers only return error
271 * codes from their resume methods if the device being handled fails to resume
272 * and is not usable any more.
273 */
274
275struct pm_ext_ops {
276 struct pm_ops base;
277 int (*suspend_noirq)(struct device *dev);
278 int (*resume_noirq)(struct device *dev);
279 int (*freeze_noirq)(struct device *dev);
280 int (*thaw_noirq)(struct device *dev);
281 int (*poweroff_noirq)(struct device *dev);
282 int (*restore_noirq)(struct device *dev);
283};
284
285/**
286 * PM_EVENT_ messages
287 *
288 * The following PM_EVENT_ messages are defined for the internal use of the PM
289 * core, in order to provide a mechanism allowing the high level suspend and
290 * hibernation code to convey the necessary information to the device PM core
291 * code:
292 *
293 * ON No transition.
294 *
295 * FREEZE System is going to hibernate, call ->prepare() and ->freeze()
296 * for all devices.
297 *
298 * SUSPEND System is going to suspend, call ->prepare() and ->suspend()
299 * for all devices.
300 *
301 * HIBERNATE Hibernation image has been saved, call ->prepare() and
302 * ->poweroff() for all devices.
303 *
304 * QUIESCE Contents of main memory are going to be restored from a (loaded)
305 * hibernation image, call ->prepare() and ->freeze() for all
306 * devices.
307 *
308 * RESUME System is resuming, call ->resume() and ->complete() for all
309 * devices.
310 *
311 * THAW Hibernation image has been created, call ->thaw() and
312 * ->complete() for all devices.
313 *
314 * RESTORE Contents of main memory have been restored from a hibernation
315 * image, call ->restore() and ->complete() for all devices.
316 *
317 * RECOVER Creation of a hibernation image or restoration of the main
318 * memory contents from a hibernation image has failed, call
319 * ->thaw() and ->complete() for all devices.
320 */
321
322#define PM_EVENT_ON 0x0000
323#define PM_EVENT_FREEZE 0x0001
324#define PM_EVENT_SUSPEND 0x0002
325#define PM_EVENT_HIBERNATE 0x0004
326#define PM_EVENT_QUIESCE 0x0008
327#define PM_EVENT_RESUME 0x0010
328#define PM_EVENT_THAW 0x0020
329#define PM_EVENT_RESTORE 0x0040
330#define PM_EVENT_RECOVER 0x0080
331
332#define PM_EVENT_SLEEP (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE)
333
334#define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, })
335#define PMSG_QUIESCE ((struct pm_message){ .event = PM_EVENT_QUIESCE, })
336#define PMSG_SUSPEND ((struct pm_message){ .event = PM_EVENT_SUSPEND, })
337#define PMSG_HIBERNATE ((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
338#define PMSG_RESUME ((struct pm_message){ .event = PM_EVENT_RESUME, })
339#define PMSG_THAW ((struct pm_message){ .event = PM_EVENT_THAW, })
340#define PMSG_RESTORE ((struct pm_message){ .event = PM_EVENT_RESTORE, })
341#define PMSG_RECOVER ((struct pm_message){ .event = PM_EVENT_RECOVER, })
342#define PMSG_ON ((struct pm_message){ .event = PM_EVENT_ON, })
343
344/**
345 * Device power management states
346 *
347 * These state labels are used internally by the PM core to indicate the current
348 * status of a device with respect to the PM core operations.
349 *
350 * DPM_ON Device is regarded as operational. Set this way
351 * initially and when ->complete() is about to be called.
352 * Also set when ->prepare() fails.
353 *
354 * DPM_PREPARING Device is going to be prepared for a PM transition. Set
355 * when ->prepare() is about to be called.
356 *
357 * DPM_RESUMING Device is going to be resumed. Set when ->resume(),
358 * ->thaw(), or ->restore() is about to be called.
359 *
360 * DPM_SUSPENDING Device has been prepared for a power transition. Set
361 * when ->prepare() has just succeeded.
362 *
363 * DPM_OFF Device is regarded as inactive. Set immediately after
364 * ->suspend(), ->freeze(), or ->poweroff() has succeeded.
365 * Also set when ->resume()_noirq, ->thaw_noirq(), or
366 * ->restore_noirq() is about to be called.
367 *
368 * DPM_OFF_IRQ Device is in a "deep sleep". Set immediately after
369 * ->suspend_noirq(), ->freeze_noirq(), or
370 * ->poweroff_noirq() has just succeeded.
371 */
372
373enum dpm_state {
374 DPM_INVALID,
375 DPM_ON,
376 DPM_PREPARING,
377 DPM_RESUMING,
378 DPM_SUSPENDING,
379 DPM_OFF,
380 DPM_OFF_IRQ,
381};
382
383struct dev_pm_info {
384 pm_message_t power_state;
385 unsigned can_wakeup:1;
386 unsigned should_wakeup:1;
387 enum dpm_state status; /* Owned by the PM core */
388#ifdef CONFIG_PM_SLEEP
389 struct list_head entry;
390#endif
391};
392
393/*
394 * The PM_EVENT_ messages are also used by drivers implementing the legacy
395 * suspend framework, based on the ->suspend() and ->resume() callbacks common
396 * for suspend and hibernation transitions, according to the rules below.
397 */
398
399/* Necessary, because several drivers use PM_EVENT_PRETHAW */
400#define PM_EVENT_PRETHAW PM_EVENT_QUIESCE
401
402/*
123 * One transition is triggered by resume(), after a suspend() call; the 403 * One transition is triggered by resume(), after a suspend() call; the
124 * message is implicit: 404 * message is implicit:
125 * 405 *
@@ -164,35 +444,13 @@ typedef struct pm_message {
164 * or from system low-power states such as standby or suspend-to-RAM. 444 * or from system low-power states such as standby or suspend-to-RAM.
165 */ 445 */
166 446
167#define PM_EVENT_ON 0 447#ifdef CONFIG_PM_SLEEP
168#define PM_EVENT_FREEZE 1 448extern void device_pm_lock(void);
169#define PM_EVENT_SUSPEND 2 449extern void device_power_up(pm_message_t state);
170#define PM_EVENT_HIBERNATE 4 450extern void device_resume(pm_message_t state);
171#define PM_EVENT_PRETHAW 8
172
173#define PM_EVENT_SLEEP (PM_EVENT_SUSPEND | PM_EVENT_HIBERNATE)
174
175#define PMSG_FREEZE ((struct pm_message){ .event = PM_EVENT_FREEZE, })
176#define PMSG_PRETHAW ((struct pm_message){ .event = PM_EVENT_PRETHAW, })
177#define PMSG_SUSPEND ((struct pm_message){ .event = PM_EVENT_SUSPEND, })
178#define PMSG_HIBERNATE ((struct pm_message){ .event = PM_EVENT_HIBERNATE, })
179#define PMSG_ON ((struct pm_message){ .event = PM_EVENT_ON, })
180
181struct dev_pm_info {
182 pm_message_t power_state;
183 unsigned can_wakeup:1;
184 unsigned should_wakeup:1;
185 bool sleeping:1; /* Owned by the PM core */
186#ifdef CONFIG_PM_SLEEP
187 struct list_head entry;
188#endif
189};
190 451
452extern void device_pm_unlock(void);
191extern int device_power_down(pm_message_t state); 453extern int device_power_down(pm_message_t state);
192extern void device_power_up(void);
193extern void device_resume(void);
194
195#ifdef CONFIG_PM_SLEEP
196extern int device_suspend(pm_message_t state); 454extern int device_suspend(pm_message_t state);
197extern int device_prepare_suspend(pm_message_t state); 455extern int device_prepare_suspend(pm_message_t state);
198 456
diff --git a/include/linux/pm_wakeup.h b/include/linux/pm_wakeup.h
index f0d0b2cb8d20..0aae7776185e 100644
--- a/include/linux/pm_wakeup.h
+++ b/include/linux/pm_wakeup.h
@@ -35,6 +35,11 @@ static inline void device_init_wakeup(struct device *dev, int val)
35 dev->power.can_wakeup = dev->power.should_wakeup = !!val; 35 dev->power.can_wakeup = dev->power.should_wakeup = !!val;
36} 36}
37 37
38static inline void device_set_wakeup_capable(struct device *dev, int val)
39{
40 dev->power.can_wakeup = !!val;
41}
42
38static inline int device_can_wakeup(struct device *dev) 43static inline int device_can_wakeup(struct device *dev)
39{ 44{
40 return dev->power.can_wakeup; 45 return dev->power.can_wakeup;
@@ -47,21 +52,7 @@ static inline void device_set_wakeup_enable(struct device *dev, int val)
47 52
48static inline int device_may_wakeup(struct device *dev) 53static inline int device_may_wakeup(struct device *dev)
49{ 54{
50 return dev->power.can_wakeup & dev->power.should_wakeup; 55 return dev->power.can_wakeup && dev->power.should_wakeup;
51}
52
53/*
54 * Platform hook to activate device wakeup capability, if that's not already
55 * handled by enable_irq_wake() etc.
56 * Returns zero on success, else negative errno
57 */
58extern int (*platform_enable_wakeup)(struct device *dev, int is_on);
59
60static inline int call_platform_enable_wakeup(struct device *dev, int is_on)
61{
62 if (platform_enable_wakeup)
63 return (*platform_enable_wakeup)(dev, is_on);
64 return 0;
65} 56}
66 57
67#else /* !CONFIG_PM */ 58#else /* !CONFIG_PM */
@@ -72,6 +63,8 @@ static inline void device_init_wakeup(struct device *dev, int val)
72 dev->power.can_wakeup = !!val; 63 dev->power.can_wakeup = !!val;
73} 64}
74 65
66static inline void device_set_wakeup_capable(struct device *dev, int val) { }
67
75static inline int device_can_wakeup(struct device *dev) 68static inline int device_can_wakeup(struct device *dev)
76{ 69{
77 return dev->power.can_wakeup; 70 return dev->power.can_wakeup;
@@ -80,11 +73,6 @@ static inline int device_can_wakeup(struct device *dev)
80#define device_set_wakeup_enable(dev, val) do {} while (0) 73#define device_set_wakeup_enable(dev, val) do {} while (0)
81#define device_may_wakeup(dev) 0 74#define device_may_wakeup(dev) 0
82 75
83static inline int call_platform_enable_wakeup(struct device *dev, int is_on)
84{
85 return 0;
86}
87
88#endif /* !CONFIG_PM */ 76#endif /* !CONFIG_PM */
89 77
90#endif /* _LINUX_PM_WAKEUP_H */ 78#endif /* _LINUX_PM_WAKEUP_H */
diff --git a/include/linux/pnp.h b/include/linux/pnp.h
index 63b128d512fb..1ce54b63085d 100644
--- a/include/linux/pnp.h
+++ b/include/linux/pnp.h
@@ -1,6 +1,8 @@
1/* 1/*
2 * Linux Plug and Play Support 2 * Linux Plug and Play Support
3 * Copyright by Adam Belay <ambx1@neo.rr.com> 3 * Copyright by Adam Belay <ambx1@neo.rr.com>
4 * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
4 */ 6 */
5 7
6#ifndef _LINUX_PNP_H 8#ifndef _LINUX_PNP_H
@@ -15,7 +17,6 @@
15 17
16struct pnp_protocol; 18struct pnp_protocol;
17struct pnp_dev; 19struct pnp_dev;
18struct pnp_resource_table;
19 20
20/* 21/*
21 * Resource Management 22 * Resource Management
@@ -24,7 +25,14 @@ struct resource *pnp_get_resource(struct pnp_dev *, unsigned int, unsigned int);
24 25
25static inline int pnp_resource_valid(struct resource *res) 26static inline int pnp_resource_valid(struct resource *res)
26{ 27{
27 if (res && !(res->flags & IORESOURCE_UNSET)) 28 if (res)
29 return 1;
30 return 0;
31}
32
33static inline int pnp_resource_enabled(struct resource *res)
34{
35 if (res && !(res->flags & IORESOURCE_DISABLED))
28 return 1; 36 return 1;
29 return 0; 37 return 0;
30} 38}
@@ -40,19 +48,31 @@ static inline resource_size_t pnp_resource_len(struct resource *res)
40static inline resource_size_t pnp_port_start(struct pnp_dev *dev, 48static inline resource_size_t pnp_port_start(struct pnp_dev *dev,
41 unsigned int bar) 49 unsigned int bar)
42{ 50{
43 return pnp_get_resource(dev, IORESOURCE_IO, bar)->start; 51 struct resource *res = pnp_get_resource(dev, IORESOURCE_IO, bar);
52
53 if (pnp_resource_valid(res))
54 return res->start;
55 return 0;
44} 56}
45 57
46static inline resource_size_t pnp_port_end(struct pnp_dev *dev, 58static inline resource_size_t pnp_port_end(struct pnp_dev *dev,
47 unsigned int bar) 59 unsigned int bar)
48{ 60{
49 return pnp_get_resource(dev, IORESOURCE_IO, bar)->end; 61 struct resource *res = pnp_get_resource(dev, IORESOURCE_IO, bar);
62
63 if (pnp_resource_valid(res))
64 return res->end;
65 return 0;
50} 66}
51 67
52static inline unsigned long pnp_port_flags(struct pnp_dev *dev, 68static inline unsigned long pnp_port_flags(struct pnp_dev *dev,
53 unsigned int bar) 69 unsigned int bar)
54{ 70{
55 return pnp_get_resource(dev, IORESOURCE_IO, bar)->flags; 71 struct resource *res = pnp_get_resource(dev, IORESOURCE_IO, bar);
72
73 if (pnp_resource_valid(res))
74 return res->flags;
75 return IORESOURCE_IO | IORESOURCE_AUTO;
56} 76}
57 77
58static inline int pnp_port_valid(struct pnp_dev *dev, unsigned int bar) 78static inline int pnp_port_valid(struct pnp_dev *dev, unsigned int bar)
@@ -63,25 +83,41 @@ static inline int pnp_port_valid(struct pnp_dev *dev, unsigned int bar)
63static inline resource_size_t pnp_port_len(struct pnp_dev *dev, 83static inline resource_size_t pnp_port_len(struct pnp_dev *dev,
64 unsigned int bar) 84 unsigned int bar)
65{ 85{
66 return pnp_resource_len(pnp_get_resource(dev, IORESOURCE_IO, bar)); 86 struct resource *res = pnp_get_resource(dev, IORESOURCE_IO, bar);
87
88 if (pnp_resource_valid(res))
89 return pnp_resource_len(res);
90 return 0;
67} 91}
68 92
69 93
70static inline resource_size_t pnp_mem_start(struct pnp_dev *dev, 94static inline resource_size_t pnp_mem_start(struct pnp_dev *dev,
71 unsigned int bar) 95 unsigned int bar)
72{ 96{
73 return pnp_get_resource(dev, IORESOURCE_MEM, bar)->start; 97 struct resource *res = pnp_get_resource(dev, IORESOURCE_MEM, bar);
98
99 if (pnp_resource_valid(res))
100 return res->start;
101 return 0;
74} 102}
75 103
76static inline resource_size_t pnp_mem_end(struct pnp_dev *dev, 104static inline resource_size_t pnp_mem_end(struct pnp_dev *dev,
77 unsigned int bar) 105 unsigned int bar)
78{ 106{
79 return pnp_get_resource(dev, IORESOURCE_MEM, bar)->end; 107 struct resource *res = pnp_get_resource(dev, IORESOURCE_MEM, bar);
108
109 if (pnp_resource_valid(res))
110 return res->end;
111 return 0;
80} 112}
81 113
82static inline unsigned long pnp_mem_flags(struct pnp_dev *dev, unsigned int bar) 114static inline unsigned long pnp_mem_flags(struct pnp_dev *dev, unsigned int bar)
83{ 115{
84 return pnp_get_resource(dev, IORESOURCE_MEM, bar)->flags; 116 struct resource *res = pnp_get_resource(dev, IORESOURCE_MEM, bar);
117
118 if (pnp_resource_valid(res))
119 return res->flags;
120 return IORESOURCE_MEM | IORESOURCE_AUTO;
85} 121}
86 122
87static inline int pnp_mem_valid(struct pnp_dev *dev, unsigned int bar) 123static inline int pnp_mem_valid(struct pnp_dev *dev, unsigned int bar)
@@ -92,18 +128,30 @@ static inline int pnp_mem_valid(struct pnp_dev *dev, unsigned int bar)
92static inline resource_size_t pnp_mem_len(struct pnp_dev *dev, 128static inline resource_size_t pnp_mem_len(struct pnp_dev *dev,
93 unsigned int bar) 129 unsigned int bar)
94{ 130{
95 return pnp_resource_len(pnp_get_resource(dev, IORESOURCE_MEM, bar)); 131 struct resource *res = pnp_get_resource(dev, IORESOURCE_MEM, bar);
132
133 if (pnp_resource_valid(res))
134 return pnp_resource_len(res);
135 return 0;
96} 136}
97 137
98 138
99static inline resource_size_t pnp_irq(struct pnp_dev *dev, unsigned int bar) 139static inline resource_size_t pnp_irq(struct pnp_dev *dev, unsigned int bar)
100{ 140{
101 return pnp_get_resource(dev, IORESOURCE_IRQ, bar)->start; 141 struct resource *res = pnp_get_resource(dev, IORESOURCE_IRQ, bar);
142
143 if (pnp_resource_valid(res))
144 return res->start;
145 return -1;
102} 146}
103 147
104static inline unsigned long pnp_irq_flags(struct pnp_dev *dev, unsigned int bar) 148static inline unsigned long pnp_irq_flags(struct pnp_dev *dev, unsigned int bar)
105{ 149{
106 return pnp_get_resource(dev, IORESOURCE_IRQ, bar)->flags; 150 struct resource *res = pnp_get_resource(dev, IORESOURCE_IRQ, bar);
151
152 if (pnp_resource_valid(res))
153 return res->flags;
154 return IORESOURCE_IRQ | IORESOURCE_AUTO;
107} 155}
108 156
109static inline int pnp_irq_valid(struct pnp_dev *dev, unsigned int bar) 157static inline int pnp_irq_valid(struct pnp_dev *dev, unsigned int bar)
@@ -114,12 +162,20 @@ static inline int pnp_irq_valid(struct pnp_dev *dev, unsigned int bar)
114 162
115static inline resource_size_t pnp_dma(struct pnp_dev *dev, unsigned int bar) 163static inline resource_size_t pnp_dma(struct pnp_dev *dev, unsigned int bar)
116{ 164{
117 return pnp_get_resource(dev, IORESOURCE_DMA, bar)->start; 165 struct resource *res = pnp_get_resource(dev, IORESOURCE_DMA, bar);
166
167 if (pnp_resource_valid(res))
168 return res->start;
169 return -1;
118} 170}
119 171
120static inline unsigned long pnp_dma_flags(struct pnp_dev *dev, unsigned int bar) 172static inline unsigned long pnp_dma_flags(struct pnp_dev *dev, unsigned int bar)
121{ 173{
122 return pnp_get_resource(dev, IORESOURCE_DMA, bar)->flags; 174 struct resource *res = pnp_get_resource(dev, IORESOURCE_DMA, bar);
175
176 if (pnp_resource_valid(res))
177 return res->flags;
178 return IORESOURCE_DMA | IORESOURCE_AUTO;
123} 179}
124 180
125static inline int pnp_dma_valid(struct pnp_dev *dev, unsigned int bar) 181static inline int pnp_dma_valid(struct pnp_dev *dev, unsigned int bar)
@@ -128,57 +184,6 @@ static inline int pnp_dma_valid(struct pnp_dev *dev, unsigned int bar)
128} 184}
129 185
130 186
131#define PNP_PORT_FLAG_16BITADDR (1<<0)
132#define PNP_PORT_FLAG_FIXED (1<<1)
133
134struct pnp_port {
135 unsigned short min; /* min base number */
136 unsigned short max; /* max base number */
137 unsigned char align; /* align boundary */
138 unsigned char size; /* size of range */
139 unsigned char flags; /* port flags */
140 unsigned char pad; /* pad */
141 struct pnp_port *next; /* next port */
142};
143
144#define PNP_IRQ_NR 256
145struct pnp_irq {
146 DECLARE_BITMAP(map, PNP_IRQ_NR); /* bitmask for IRQ lines */
147 unsigned char flags; /* IRQ flags */
148 unsigned char pad; /* pad */
149 struct pnp_irq *next; /* next IRQ */
150};
151
152struct pnp_dma {
153 unsigned char map; /* bitmask for DMA channels */
154 unsigned char flags; /* DMA flags */
155 struct pnp_dma *next; /* next port */
156};
157
158struct pnp_mem {
159 unsigned int min; /* min base number */
160 unsigned int max; /* max base number */
161 unsigned int align; /* align boundary */
162 unsigned int size; /* size of range */
163 unsigned char flags; /* memory flags */
164 unsigned char pad; /* pad */
165 struct pnp_mem *next; /* next memory resource */
166};
167
168#define PNP_RES_PRIORITY_PREFERRED 0
169#define PNP_RES_PRIORITY_ACCEPTABLE 1
170#define PNP_RES_PRIORITY_FUNCTIONAL 2
171#define PNP_RES_PRIORITY_INVALID 65535
172
173struct pnp_option {
174 unsigned short priority; /* priority */
175 struct pnp_port *port; /* first port */
176 struct pnp_irq *irq; /* first IRQ */
177 struct pnp_dma *dma; /* first DMA */
178 struct pnp_mem *mem; /* first memory resource */
179 struct pnp_option *next; /* used to chain dependent resources */
180};
181
182/* 187/*
183 * Device Management 188 * Device Management
184 */ 189 */
@@ -246,9 +251,9 @@ struct pnp_dev {
246 251
247 int active; 252 int active;
248 int capabilities; 253 int capabilities;
249 struct pnp_option *independent; 254 unsigned int num_dependent_sets;
250 struct pnp_option *dependent; 255 struct list_head resources;
251 struct pnp_resource_table *res; 256 struct list_head options;
252 257
253 char name[PNP_NAME_LEN]; /* contains a human-readable name */ 258 char name[PNP_NAME_LEN]; /* contains a human-readable name */
254 int flags; /* used by protocols */ 259 int flags; /* used by protocols */
@@ -425,6 +430,8 @@ void pnp_unregister_card_driver(struct pnp_card_driver *drv);
425extern struct list_head pnp_cards; 430extern struct list_head pnp_cards;
426 431
427/* resource management */ 432/* resource management */
433int pnp_possible_config(struct pnp_dev *dev, int type, resource_size_t base,
434 resource_size_t size);
428int pnp_auto_config_dev(struct pnp_dev *dev); 435int pnp_auto_config_dev(struct pnp_dev *dev);
429int pnp_start_dev(struct pnp_dev *dev); 436int pnp_start_dev(struct pnp_dev *dev);
430int pnp_stop_dev(struct pnp_dev *dev); 437int pnp_stop_dev(struct pnp_dev *dev);
@@ -452,6 +459,9 @@ static inline int pnp_register_card_driver(struct pnp_card_driver *drv) { return
452static inline void pnp_unregister_card_driver(struct pnp_card_driver *drv) { } 459static inline void pnp_unregister_card_driver(struct pnp_card_driver *drv) { }
453 460
454/* resource management */ 461/* resource management */
462static inline int pnp_possible_config(struct pnp_dev *dev, int type,
463 resource_size_t base,
464 resource_size_t size) { return 0; }
455static inline int pnp_auto_config_dev(struct pnp_dev *dev) { return -ENODEV; } 465static inline int pnp_auto_config_dev(struct pnp_dev *dev) { return -ENODEV; }
456static inline int pnp_start_dev(struct pnp_dev *dev) { return -ENODEV; } 466static inline int pnp_start_dev(struct pnp_dev *dev) { return -ENODEV; }
457static inline int pnp_stop_dev(struct pnp_dev *dev) { return -ENODEV; } 467static inline int pnp_stop_dev(struct pnp_dev *dev) { return -ENODEV; }
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 21349173d148..1941d8b5cf11 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1062,12 +1062,6 @@ struct task_struct {
1062#endif 1062#endif
1063 1063
1064 struct list_head tasks; 1064 struct list_head tasks;
1065 /*
1066 * ptrace_list/ptrace_children forms the list of my children
1067 * that were stolen by a ptracer.
1068 */
1069 struct list_head ptrace_children;
1070 struct list_head ptrace_list;
1071 1065
1072 struct mm_struct *mm, *active_mm; 1066 struct mm_struct *mm, *active_mm;
1073 1067
@@ -1089,18 +1083,25 @@ struct task_struct {
1089 /* 1083 /*
1090 * pointers to (original) parent process, youngest child, younger sibling, 1084 * pointers to (original) parent process, youngest child, younger sibling,
1091 * older sibling, respectively. (p->father can be replaced with 1085 * older sibling, respectively. (p->father can be replaced with
1092 * p->parent->pid) 1086 * p->real_parent->pid)
1093 */ 1087 */
1094 struct task_struct *real_parent; /* real parent process (when being debugged) */ 1088 struct task_struct *real_parent; /* real parent process */
1095 struct task_struct *parent; /* parent process */ 1089 struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */
1096 /* 1090 /*
1097 * children/sibling forms the list of my children plus the 1091 * children/sibling forms the list of my natural children
1098 * tasks I'm ptracing.
1099 */ 1092 */
1100 struct list_head children; /* list of my children */ 1093 struct list_head children; /* list of my children */
1101 struct list_head sibling; /* linkage in my parent's children list */ 1094 struct list_head sibling; /* linkage in my parent's children list */
1102 struct task_struct *group_leader; /* threadgroup leader */ 1095 struct task_struct *group_leader; /* threadgroup leader */
1103 1096
1097 /*
1098 * ptraced is the list of tasks this task is using ptrace on.
1099 * This includes both natural children and PTRACE_ATTACH targets.
1100 * p->ptrace_entry is p's link on the p->parent->ptraced list.
1101 */
1102 struct list_head ptraced;
1103 struct list_head ptrace_entry;
1104
1104 /* PID/PID hash table linkage. */ 1105 /* PID/PID hash table linkage. */
1105 struct pid_link pids[PIDTYPE_MAX]; 1106 struct pid_link pids[PIDTYPE_MAX];
1106 struct list_head thread_group; 1107 struct list_head thread_group;
@@ -1494,6 +1495,7 @@ static inline void put_task_struct(struct task_struct *t)
1494#define PF_MEMPOLICY 0x10000000 /* Non-default NUMA mempolicy */ 1495#define PF_MEMPOLICY 0x10000000 /* Non-default NUMA mempolicy */
1495#define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */ 1496#define PF_MUTEX_TESTER 0x20000000 /* Thread belongs to the rt mutex tester */
1496#define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezeable */ 1497#define PF_FREEZER_SKIP 0x40000000 /* Freezer should not count it as freezeable */
1498#define PF_FREEZER_NOSIG 0x80000000 /* Freezer won't send signals to it */
1497 1499
1498/* 1500/*
1499 * Only the _current_ task can read/write to tsk->flags, but other 1501 * Only the _current_ task can read/write to tsk->flags, but other
@@ -1875,9 +1877,6 @@ extern void wait_task_inactive(struct task_struct * p);
1875#define wait_task_inactive(p) do { } while (0) 1877#define wait_task_inactive(p) do { } while (0)
1876#endif 1878#endif
1877 1879
1878#define remove_parent(p) list_del_init(&(p)->sibling)
1879#define add_parent(p) list_add_tail(&(p)->sibling,&(p)->parent->children)
1880
1881#define next_task(p) list_entry(rcu_dereference((p)->tasks.next), struct task_struct, tasks) 1880#define next_task(p) list_entry(rcu_dereference((p)->tasks.next), struct task_struct, tasks)
1882 1881
1883#define for_each_process(p) \ 1882#define for_each_process(p) \
diff --git a/include/linux/spi/mmc_spi.h b/include/linux/spi/mmc_spi.h
index d5ca78b93a3b..a3626aedaec9 100644
--- a/include/linux/spi/mmc_spi.h
+++ b/include/linux/spi/mmc_spi.h
@@ -23,6 +23,15 @@ struct mmc_spi_platform_data {
23 /* sense switch on sd cards */ 23 /* sense switch on sd cards */
24 int (*get_ro)(struct device *); 24 int (*get_ro)(struct device *);
25 25
26 /*
27 * If board does not use CD interrupts, driver can optimize polling
28 * using this function.
29 */
30 int (*get_cd)(struct device *);
31
32 /* Capabilities to pass into mmc core (e.g. MMC_CAP_NEEDS_POLL). */
33 unsigned long caps;
34
26 /* how long to debounce card detect, in msecs */ 35 /* how long to debounce card detect, in msecs */
27 u16 detect_delay; 36 u16 detect_delay;
28 37
diff --git a/include/linux/sunrpc/clnt.h b/include/linux/sunrpc/clnt.h
index 6fff7f82ef12..e5bfe01ee305 100644
--- a/include/linux/sunrpc/clnt.h
+++ b/include/linux/sunrpc/clnt.h
@@ -42,7 +42,8 @@ struct rpc_clnt {
42 42
43 unsigned int cl_softrtry : 1,/* soft timeouts */ 43 unsigned int cl_softrtry : 1,/* soft timeouts */
44 cl_discrtry : 1,/* disconnect before retry */ 44 cl_discrtry : 1,/* disconnect before retry */
45 cl_autobind : 1;/* use getport() */ 45 cl_autobind : 1,/* use getport() */
46 cl_chatty : 1;/* be verbose */
46 47
47 struct rpc_rtt * cl_rtt; /* RTO estimator data */ 48 struct rpc_rtt * cl_rtt; /* RTO estimator data */
48 const struct rpc_timeout *cl_timeout; /* Timeout strategy */ 49 const struct rpc_timeout *cl_timeout; /* Timeout strategy */
@@ -114,6 +115,7 @@ struct rpc_create_args {
114#define RPC_CLNT_CREATE_NONPRIVPORT (1UL << 3) 115#define RPC_CLNT_CREATE_NONPRIVPORT (1UL << 3)
115#define RPC_CLNT_CREATE_NOPING (1UL << 4) 116#define RPC_CLNT_CREATE_NOPING (1UL << 4)
116#define RPC_CLNT_CREATE_DISCRTRY (1UL << 5) 117#define RPC_CLNT_CREATE_DISCRTRY (1UL << 5)
118#define RPC_CLNT_CREATE_QUIET (1UL << 6)
117 119
118struct rpc_clnt *rpc_create(struct rpc_create_args *args); 120struct rpc_clnt *rpc_create(struct rpc_create_args *args);
119struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *, 121struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *,
@@ -123,6 +125,9 @@ void rpc_shutdown_client(struct rpc_clnt *);
123void rpc_release_client(struct rpc_clnt *); 125void rpc_release_client(struct rpc_clnt *);
124 126
125int rpcb_register(u32, u32, int, unsigned short, int *); 127int rpcb_register(u32, u32, int, unsigned short, int *);
128int rpcb_v4_register(const u32 program, const u32 version,
129 const struct sockaddr *address,
130 const char *netid, int *result);
126int rpcb_getport_sync(struct sockaddr_in *, u32, u32, int); 131int rpcb_getport_sync(struct sockaddr_in *, u32, u32, int);
127void rpcb_getport_async(struct rpc_task *); 132void rpcb_getport_async(struct rpc_task *);
128 133
diff --git a/include/linux/sunrpc/sched.h b/include/linux/sunrpc/sched.h
index d1a5c8c1a0f1..64981a2f1cae 100644
--- a/include/linux/sunrpc/sched.h
+++ b/include/linux/sunrpc/sched.h
@@ -135,7 +135,6 @@ struct rpc_task_setup {
135#define RPC_IS_SWAPPER(t) ((t)->tk_flags & RPC_TASK_SWAPPER) 135#define RPC_IS_SWAPPER(t) ((t)->tk_flags & RPC_TASK_SWAPPER)
136#define RPC_DO_ROOTOVERRIDE(t) ((t)->tk_flags & RPC_TASK_ROOTCREDS) 136#define RPC_DO_ROOTOVERRIDE(t) ((t)->tk_flags & RPC_TASK_ROOTCREDS)
137#define RPC_ASSASSINATED(t) ((t)->tk_flags & RPC_TASK_KILLED) 137#define RPC_ASSASSINATED(t) ((t)->tk_flags & RPC_TASK_KILLED)
138#define RPC_DO_CALLBACK(t) ((t)->tk_callback != NULL)
139#define RPC_IS_SOFT(t) ((t)->tk_flags & RPC_TASK_SOFT) 138#define RPC_IS_SOFT(t) ((t)->tk_flags & RPC_TASK_SOFT)
140 139
141#define RPC_TASK_RUNNING 0 140#define RPC_TASK_RUNNING 0
diff --git a/include/linux/suspend.h b/include/linux/suspend.h
index a6977423baf7..e8e69159af71 100644
--- a/include/linux/suspend.h
+++ b/include/linux/suspend.h
@@ -86,6 +86,11 @@ typedef int __bitwise suspend_state_t;
86 * that implement @begin(), but platforms implementing @begin() should 86 * that implement @begin(), but platforms implementing @begin() should
87 * also provide a @end() which cleans up transitions aborted before 87 * also provide a @end() which cleans up transitions aborted before
88 * @enter(). 88 * @enter().
89 *
90 * @recover: Recover the platform from a suspend failure.
91 * Called by the PM core if the suspending of devices fails.
92 * This callback is optional and should only be implemented by platforms
93 * which require special recovery actions in that situation.
89 */ 94 */
90struct platform_suspend_ops { 95struct platform_suspend_ops {
91 int (*valid)(suspend_state_t state); 96 int (*valid)(suspend_state_t state);
@@ -94,6 +99,7 @@ struct platform_suspend_ops {
94 int (*enter)(suspend_state_t state); 99 int (*enter)(suspend_state_t state);
95 void (*finish)(void); 100 void (*finish)(void);
96 void (*end)(void); 101 void (*end)(void);
102 void (*recover)(void);
97}; 103};
98 104
99#ifdef CONFIG_SUSPEND 105#ifdef CONFIG_SUSPEND
@@ -149,7 +155,7 @@ extern void mark_free_pages(struct zone *zone);
149 * The methods in this structure allow a platform to carry out special 155 * The methods in this structure allow a platform to carry out special
150 * operations required by it during a hibernation transition. 156 * operations required by it during a hibernation transition.
151 * 157 *
152 * All the methods below must be implemented. 158 * All the methods below, except for @recover(), must be implemented.
153 * 159 *
154 * @begin: Tell the platform driver that we're starting hibernation. 160 * @begin: Tell the platform driver that we're starting hibernation.
155 * Called right after shrinking memory and before freezing devices. 161 * Called right after shrinking memory and before freezing devices.
@@ -189,6 +195,11 @@ extern void mark_free_pages(struct zone *zone);
189 * @restore_cleanup: Clean up after a failing image restoration. 195 * @restore_cleanup: Clean up after a failing image restoration.
190 * Called right after the nonboot CPUs have been enabled and before 196 * Called right after the nonboot CPUs have been enabled and before
191 * thawing devices (runs with IRQs on). 197 * thawing devices (runs with IRQs on).
198 *
199 * @recover: Recover the platform from a failure to suspend devices.
200 * Called by the PM core if the suspending of devices during hibernation
201 * fails. This callback is optional and should only be implemented by
202 * platforms which require special recovery actions in that situation.
192 */ 203 */
193struct platform_hibernation_ops { 204struct platform_hibernation_ops {
194 int (*begin)(void); 205 int (*begin)(void);
@@ -200,6 +211,7 @@ struct platform_hibernation_ops {
200 void (*leave)(void); 211 void (*leave)(void);
201 int (*pre_restore)(void); 212 int (*pre_restore)(void);
202 void (*restore_cleanup)(void); 213 void (*restore_cleanup)(void);
214 void (*recover)(void);
203}; 215};
204 216
205#ifdef CONFIG_HIBERNATION 217#ifdef CONFIG_HIBERNATION
diff --git a/init/do_mounts.c b/init/do_mounts.c
index 660c1e50c91b..a1de1bf3d6b9 100644
--- a/init/do_mounts.c
+++ b/init/do_mounts.c
@@ -372,7 +372,8 @@ void __init prepare_namespace(void)
372 372
373 if (saved_root_name[0]) { 373 if (saved_root_name[0]) {
374 root_device_name = saved_root_name; 374 root_device_name = saved_root_name;
375 if (!strncmp(root_device_name, "mtd", 3)) { 375 if (!strncmp(root_device_name, "mtd", 3) ||
376 !strncmp(root_device_name, "ubi", 3)) {
376 mount_block_root(root_device_name, root_mountflags); 377 mount_block_root(root_device_name, root_mountflags);
377 goto out; 378 goto out;
378 } 379 }
diff --git a/kernel/Makefile b/kernel/Makefile
index 0a7ed838984b..985ddb7da4d0 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -11,8 +11,6 @@ obj-y = sched.o fork.o exec_domain.o panic.o printk.o profile.o \
11 hrtimer.o rwsem.o nsproxy.o srcu.o semaphore.o \ 11 hrtimer.o rwsem.o nsproxy.o srcu.o semaphore.o \
12 notifier.o ksysfs.o pm_qos_params.o sched_clock.o 12 notifier.o ksysfs.o pm_qos_params.o sched_clock.o
13 13
14CFLAGS_REMOVE_sched.o = -mno-spe
15
16ifdef CONFIG_FTRACE 14ifdef CONFIG_FTRACE
17# Do not trace debug files and internal ftrace files 15# Do not trace debug files and internal ftrace files
18CFLAGS_REMOVE_lockdep.o = -pg 16CFLAGS_REMOVE_lockdep.o = -pg
@@ -21,6 +19,7 @@ CFLAGS_REMOVE_mutex-debug.o = -pg
21CFLAGS_REMOVE_rtmutex-debug.o = -pg 19CFLAGS_REMOVE_rtmutex-debug.o = -pg
22CFLAGS_REMOVE_cgroup-debug.o = -pg 20CFLAGS_REMOVE_cgroup-debug.o = -pg
23CFLAGS_REMOVE_sched_clock.o = -pg 21CFLAGS_REMOVE_sched_clock.o = -pg
22CFLAGS_REMOVE_sched.o = -mno-spe -pg
24endif 23endif
25 24
26obj-$(CONFIG_SYSCTL_SYSCALL_CHECK) += sysctl_check.o 25obj-$(CONFIG_SYSCTL_SYSCALL_CHECK) += sysctl_check.o
diff --git a/kernel/cpu.c b/kernel/cpu.c
index b11f06dc149a..cfb1d43ab801 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -299,6 +299,7 @@ int __ref cpu_down(unsigned int cpu)
299 cpu_maps_update_done(); 299 cpu_maps_update_done();
300 return err; 300 return err;
301} 301}
302EXPORT_SYMBOL(cpu_down);
302#endif /*CONFIG_HOTPLUG_CPU*/ 303#endif /*CONFIG_HOTPLUG_CPU*/
303 304
304/* Requires cpu_add_remove_lock to be held */ 305/* Requires cpu_add_remove_lock to be held */
diff --git a/kernel/exit.c b/kernel/exit.c
index ceb258782835..93d2711b9381 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -71,7 +71,7 @@ static void __unhash_process(struct task_struct *p)
71 __get_cpu_var(process_counts)--; 71 __get_cpu_var(process_counts)--;
72 } 72 }
73 list_del_rcu(&p->thread_group); 73 list_del_rcu(&p->thread_group);
74 remove_parent(p); 74 list_del_init(&p->sibling);
75} 75}
76 76
77/* 77/*
@@ -152,6 +152,18 @@ static void delayed_put_task_struct(struct rcu_head *rhp)
152 put_task_struct(container_of(rhp, struct task_struct, rcu)); 152 put_task_struct(container_of(rhp, struct task_struct, rcu));
153} 153}
154 154
155/*
156 * Do final ptrace-related cleanup of a zombie being reaped.
157 *
158 * Called with write_lock(&tasklist_lock) held.
159 */
160static void ptrace_release_task(struct task_struct *p)
161{
162 BUG_ON(!list_empty(&p->ptraced));
163 ptrace_unlink(p);
164 BUG_ON(!list_empty(&p->ptrace_entry));
165}
166
155void release_task(struct task_struct * p) 167void release_task(struct task_struct * p)
156{ 168{
157 struct task_struct *leader; 169 struct task_struct *leader;
@@ -160,8 +172,7 @@ repeat:
160 atomic_dec(&p->user->processes); 172 atomic_dec(&p->user->processes);
161 proc_flush_task(p); 173 proc_flush_task(p);
162 write_lock_irq(&tasklist_lock); 174 write_lock_irq(&tasklist_lock);
163 ptrace_unlink(p); 175 ptrace_release_task(p);
164 BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
165 __exit_signal(p); 176 __exit_signal(p);
166 177
167 /* 178 /*
@@ -315,9 +326,8 @@ static void reparent_to_kthreadd(void)
315 326
316 ptrace_unlink(current); 327 ptrace_unlink(current);
317 /* Reparent to init */ 328 /* Reparent to init */
318 remove_parent(current);
319 current->real_parent = current->parent = kthreadd_task; 329 current->real_parent = current->parent = kthreadd_task;
320 add_parent(current); 330 list_move_tail(&current->sibling, &current->real_parent->children);
321 331
322 /* Set the exit signal to SIGCHLD so we signal init on exit */ 332 /* Set the exit signal to SIGCHLD so we signal init on exit */
323 current->exit_signal = SIGCHLD; 333 current->exit_signal = SIGCHLD;
@@ -692,37 +702,97 @@ static void exit_mm(struct task_struct * tsk)
692 mmput(mm); 702 mmput(mm);
693} 703}
694 704
695static void 705/*
696reparent_thread(struct task_struct *p, struct task_struct *father, int traced) 706 * Return nonzero if @parent's children should reap themselves.
707 *
708 * Called with write_lock_irq(&tasklist_lock) held.
709 */
710static int ignoring_children(struct task_struct *parent)
697{ 711{
698 if (p->pdeath_signal) 712 int ret;
699 /* We already hold the tasklist_lock here. */ 713 struct sighand_struct *psig = parent->sighand;
700 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p); 714 unsigned long flags;
715 spin_lock_irqsave(&psig->siglock, flags);
716 ret = (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
717 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT));
718 spin_unlock_irqrestore(&psig->siglock, flags);
719 return ret;
720}
701 721
702 /* Move the child from its dying parent to the new one. */ 722/*
703 if (unlikely(traced)) { 723 * Detach all tasks we were using ptrace on.
704 /* Preserve ptrace links if someone else is tracing this child. */ 724 * Any that need to be release_task'd are put on the @dead list.
705 list_del_init(&p->ptrace_list); 725 *
706 if (ptrace_reparented(p)) 726 * Called with write_lock(&tasklist_lock) held.
707 list_add(&p->ptrace_list, &p->real_parent->ptrace_children); 727 */
708 } else { 728static void ptrace_exit(struct task_struct *parent, struct list_head *dead)
709 /* If this child is being traced, then we're the one tracing it 729{
710 * anyway, so let go of it. 730 struct task_struct *p, *n;
731 int ign = -1;
732
733 list_for_each_entry_safe(p, n, &parent->ptraced, ptrace_entry) {
734 __ptrace_unlink(p);
735
736 if (p->exit_state != EXIT_ZOMBIE)
737 continue;
738
739 /*
740 * If it's a zombie, our attachedness prevented normal
741 * parent notification or self-reaping. Do notification
742 * now if it would have happened earlier. If it should
743 * reap itself, add it to the @dead list. We can't call
744 * release_task() here because we already hold tasklist_lock.
745 *
746 * If it's our own child, there is no notification to do.
747 * But if our normal children self-reap, then this child
748 * was prevented by ptrace and we must reap it now.
711 */ 749 */
712 p->ptrace = 0; 750 if (!task_detached(p) && thread_group_empty(p)) {
713 remove_parent(p); 751 if (!same_thread_group(p->real_parent, parent))
714 p->parent = p->real_parent; 752 do_notify_parent(p, p->exit_signal);
715 add_parent(p); 753 else {
754 if (ign < 0)
755 ign = ignoring_children(parent);
756 if (ign)
757 p->exit_signal = -1;
758 }
759 }
716 760
717 if (task_is_traced(p)) { 761 if (task_detached(p)) {
718 /* 762 /*
719 * If it was at a trace stop, turn it into 763 * Mark it as in the process of being reaped.
720 * a normal stop since it's no longer being
721 * traced.
722 */ 764 */
723 ptrace_untrace(p); 765 p->exit_state = EXIT_DEAD;
766 list_add(&p->ptrace_entry, dead);
724 } 767 }
725 } 768 }
769}
770
771/*
772 * Finish up exit-time ptrace cleanup.
773 *
774 * Called without locks.
775 */
776static void ptrace_exit_finish(struct task_struct *parent,
777 struct list_head *dead)
778{
779 struct task_struct *p, *n;
780
781 BUG_ON(!list_empty(&parent->ptraced));
782
783 list_for_each_entry_safe(p, n, dead, ptrace_entry) {
784 list_del_init(&p->ptrace_entry);
785 release_task(p);
786 }
787}
788
789static void reparent_thread(struct task_struct *p, struct task_struct *father)
790{
791 if (p->pdeath_signal)
792 /* We already hold the tasklist_lock here. */
793 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
794
795 list_move_tail(&p->sibling, &p->real_parent->children);
726 796
727 /* If this is a threaded reparent there is no need to 797 /* If this is a threaded reparent there is no need to
728 * notify anyone anything has happened. 798 * notify anyone anything has happened.
@@ -737,7 +807,8 @@ reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
737 /* If we'd notified the old parent about this child's death, 807 /* If we'd notified the old parent about this child's death,
738 * also notify the new parent. 808 * also notify the new parent.
739 */ 809 */
740 if (!traced && p->exit_state == EXIT_ZOMBIE && 810 if (!ptrace_reparented(p) &&
811 p->exit_state == EXIT_ZOMBIE &&
741 !task_detached(p) && thread_group_empty(p)) 812 !task_detached(p) && thread_group_empty(p))
742 do_notify_parent(p, p->exit_signal); 813 do_notify_parent(p, p->exit_signal);
743 814
@@ -754,12 +825,15 @@ reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
754static void forget_original_parent(struct task_struct *father) 825static void forget_original_parent(struct task_struct *father)
755{ 826{
756 struct task_struct *p, *n, *reaper = father; 827 struct task_struct *p, *n, *reaper = father;
757 struct list_head ptrace_dead; 828 LIST_HEAD(ptrace_dead);
758
759 INIT_LIST_HEAD(&ptrace_dead);
760 829
761 write_lock_irq(&tasklist_lock); 830 write_lock_irq(&tasklist_lock);
762 831
832 /*
833 * First clean up ptrace if we were using it.
834 */
835 ptrace_exit(father, &ptrace_dead);
836
763 do { 837 do {
764 reaper = next_thread(reaper); 838 reaper = next_thread(reaper);
765 if (reaper == father) { 839 if (reaper == father) {
@@ -768,58 +842,19 @@ static void forget_original_parent(struct task_struct *father)
768 } 842 }
769 } while (reaper->flags & PF_EXITING); 843 } while (reaper->flags & PF_EXITING);
770 844
771 /*
772 * There are only two places where our children can be:
773 *
774 * - in our child list
775 * - in our ptraced child list
776 *
777 * Search them and reparent children.
778 */
779 list_for_each_entry_safe(p, n, &father->children, sibling) { 845 list_for_each_entry_safe(p, n, &father->children, sibling) {
780 int ptrace;
781
782 ptrace = p->ptrace;
783
784 /* if father isn't the real parent, then ptrace must be enabled */
785 BUG_ON(father != p->real_parent && !ptrace);
786
787 if (father == p->real_parent) {
788 /* reparent with a reaper, real father it's us */
789 p->real_parent = reaper;
790 reparent_thread(p, father, 0);
791 } else {
792 /* reparent ptraced task to its real parent */
793 __ptrace_unlink (p);
794 if (p->exit_state == EXIT_ZOMBIE && !task_detached(p) &&
795 thread_group_empty(p))
796 do_notify_parent(p, p->exit_signal);
797 }
798
799 /*
800 * if the ptraced child is a detached zombie we must collect
801 * it before we exit, or it will remain zombie forever since
802 * we prevented it from self-reap itself while it was being
803 * traced by us, to be able to see it in wait4.
804 */
805 if (unlikely(ptrace && p->exit_state == EXIT_ZOMBIE && task_detached(p)))
806 list_add(&p->ptrace_list, &ptrace_dead);
807 }
808
809 list_for_each_entry_safe(p, n, &father->ptrace_children, ptrace_list) {
810 p->real_parent = reaper; 846 p->real_parent = reaper;
811 reparent_thread(p, father, 1); 847 if (p->parent == father) {
848 BUG_ON(p->ptrace);
849 p->parent = p->real_parent;
850 }
851 reparent_thread(p, father);
812 } 852 }
813 853
814 write_unlock_irq(&tasklist_lock); 854 write_unlock_irq(&tasklist_lock);
815 BUG_ON(!list_empty(&father->children)); 855 BUG_ON(!list_empty(&father->children));
816 BUG_ON(!list_empty(&father->ptrace_children));
817
818 list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_list) {
819 list_del_init(&p->ptrace_list);
820 release_task(p);
821 }
822 856
857 ptrace_exit_finish(father, &ptrace_dead);
823} 858}
824 859
825/* 860/*
@@ -1180,13 +1215,6 @@ static int eligible_child(enum pid_type type, struct pid *pid, int options,
1180 return 0; 1215 return 0;
1181 } 1216 }
1182 1217
1183 /*
1184 * Do not consider detached threads that are
1185 * not ptraced:
1186 */
1187 if (task_detached(p) && !p->ptrace)
1188 return 0;
1189
1190 /* Wait for all children (clone and not) if __WALL is set; 1218 /* Wait for all children (clone and not) if __WALL is set;
1191 * otherwise, wait for clone children *only* if __WCLONE is 1219 * otherwise, wait for clone children *only* if __WCLONE is
1192 * set; otherwise, wait for non-clone children *only*. (Note: 1220 * set; otherwise, wait for non-clone children *only*. (Note:
@@ -1197,14 +1225,10 @@ static int eligible_child(enum pid_type type, struct pid *pid, int options,
1197 return 0; 1225 return 0;
1198 1226
1199 err = security_task_wait(p); 1227 err = security_task_wait(p);
1200 if (likely(!err)) 1228 if (err)
1201 return 1; 1229 return err;
1202 1230
1203 if (type != PIDTYPE_PID) 1231 return 1;
1204 return 0;
1205 /* This child was explicitly requested, abort */
1206 read_unlock(&tasklist_lock);
1207 return err;
1208} 1232}
1209 1233
1210static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid, 1234static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
@@ -1238,7 +1262,7 @@ static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
1238 * the lock and this task is uninteresting. If we return nonzero, we have 1262 * the lock and this task is uninteresting. If we return nonzero, we have
1239 * released the lock and the system call should return. 1263 * released the lock and the system call should return.
1240 */ 1264 */
1241static int wait_task_zombie(struct task_struct *p, int noreap, 1265static int wait_task_zombie(struct task_struct *p, int options,
1242 struct siginfo __user *infop, 1266 struct siginfo __user *infop,
1243 int __user *stat_addr, struct rusage __user *ru) 1267 int __user *stat_addr, struct rusage __user *ru)
1244{ 1268{
@@ -1246,7 +1270,10 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
1246 int retval, status, traced; 1270 int retval, status, traced;
1247 pid_t pid = task_pid_vnr(p); 1271 pid_t pid = task_pid_vnr(p);
1248 1272
1249 if (unlikely(noreap)) { 1273 if (!likely(options & WEXITED))
1274 return 0;
1275
1276 if (unlikely(options & WNOWAIT)) {
1250 uid_t uid = p->uid; 1277 uid_t uid = p->uid;
1251 int exit_code = p->exit_code; 1278 int exit_code = p->exit_code;
1252 int why, status; 1279 int why, status;
@@ -1396,21 +1423,24 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
1396 * the lock and this task is uninteresting. If we return nonzero, we have 1423 * the lock and this task is uninteresting. If we return nonzero, we have
1397 * released the lock and the system call should return. 1424 * released the lock and the system call should return.
1398 */ 1425 */
1399static int wait_task_stopped(struct task_struct *p, 1426static int wait_task_stopped(int ptrace, struct task_struct *p,
1400 int noreap, struct siginfo __user *infop, 1427 int options, struct siginfo __user *infop,
1401 int __user *stat_addr, struct rusage __user *ru) 1428 int __user *stat_addr, struct rusage __user *ru)
1402{ 1429{
1403 int retval, exit_code, why; 1430 int retval, exit_code, why;
1404 uid_t uid = 0; /* unneeded, required by compiler */ 1431 uid_t uid = 0; /* unneeded, required by compiler */
1405 pid_t pid; 1432 pid_t pid;
1406 1433
1434 if (!(options & WUNTRACED))
1435 return 0;
1436
1407 exit_code = 0; 1437 exit_code = 0;
1408 spin_lock_irq(&p->sighand->siglock); 1438 spin_lock_irq(&p->sighand->siglock);
1409 1439
1410 if (unlikely(!task_is_stopped_or_traced(p))) 1440 if (unlikely(!task_is_stopped_or_traced(p)))
1411 goto unlock_sig; 1441 goto unlock_sig;
1412 1442
1413 if (!(p->ptrace & PT_PTRACED) && p->signal->group_stop_count > 0) 1443 if (!ptrace && p->signal->group_stop_count > 0)
1414 /* 1444 /*
1415 * A group stop is in progress and this is the group leader. 1445 * A group stop is in progress and this is the group leader.
1416 * We won't report until all threads have stopped. 1446 * We won't report until all threads have stopped.
@@ -1421,7 +1451,7 @@ static int wait_task_stopped(struct task_struct *p,
1421 if (!exit_code) 1451 if (!exit_code)
1422 goto unlock_sig; 1452 goto unlock_sig;
1423 1453
1424 if (!noreap) 1454 if (!unlikely(options & WNOWAIT))
1425 p->exit_code = 0; 1455 p->exit_code = 0;
1426 1456
1427 uid = p->uid; 1457 uid = p->uid;
@@ -1439,10 +1469,10 @@ unlock_sig:
1439 */ 1469 */
1440 get_task_struct(p); 1470 get_task_struct(p);
1441 pid = task_pid_vnr(p); 1471 pid = task_pid_vnr(p);
1442 why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED; 1472 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1443 read_unlock(&tasklist_lock); 1473 read_unlock(&tasklist_lock);
1444 1474
1445 if (unlikely(noreap)) 1475 if (unlikely(options & WNOWAIT))
1446 return wait_noreap_copyout(p, pid, uid, 1476 return wait_noreap_copyout(p, pid, uid,
1447 why, exit_code, 1477 why, exit_code,
1448 infop, ru); 1478 infop, ru);
@@ -1476,7 +1506,7 @@ unlock_sig:
1476 * the lock and this task is uninteresting. If we return nonzero, we have 1506 * the lock and this task is uninteresting. If we return nonzero, we have
1477 * released the lock and the system call should return. 1507 * released the lock and the system call should return.
1478 */ 1508 */
1479static int wait_task_continued(struct task_struct *p, int noreap, 1509static int wait_task_continued(struct task_struct *p, int options,
1480 struct siginfo __user *infop, 1510 struct siginfo __user *infop,
1481 int __user *stat_addr, struct rusage __user *ru) 1511 int __user *stat_addr, struct rusage __user *ru)
1482{ 1512{
@@ -1484,6 +1514,9 @@ static int wait_task_continued(struct task_struct *p, int noreap,
1484 pid_t pid; 1514 pid_t pid;
1485 uid_t uid; 1515 uid_t uid;
1486 1516
1517 if (!unlikely(options & WCONTINUED))
1518 return 0;
1519
1487 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) 1520 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1488 return 0; 1521 return 0;
1489 1522
@@ -1493,7 +1526,7 @@ static int wait_task_continued(struct task_struct *p, int noreap,
1493 spin_unlock_irq(&p->sighand->siglock); 1526 spin_unlock_irq(&p->sighand->siglock);
1494 return 0; 1527 return 0;
1495 } 1528 }
1496 if (!noreap) 1529 if (!unlikely(options & WNOWAIT))
1497 p->signal->flags &= ~SIGNAL_STOP_CONTINUED; 1530 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1498 spin_unlock_irq(&p->sighand->siglock); 1531 spin_unlock_irq(&p->sighand->siglock);
1499 1532
@@ -1519,89 +1552,161 @@ static int wait_task_continued(struct task_struct *p, int noreap,
1519 return retval; 1552 return retval;
1520} 1553}
1521 1554
1555/*
1556 * Consider @p for a wait by @parent.
1557 *
1558 * -ECHILD should be in *@notask_error before the first call.
1559 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1560 * Returns zero if the search for a child should continue;
1561 * then *@notask_error is 0 if @p is an eligible child,
1562 * or another error from security_task_wait(), or still -ECHILD.
1563 */
1564static int wait_consider_task(struct task_struct *parent, int ptrace,
1565 struct task_struct *p, int *notask_error,
1566 enum pid_type type, struct pid *pid, int options,
1567 struct siginfo __user *infop,
1568 int __user *stat_addr, struct rusage __user *ru)
1569{
1570 int ret = eligible_child(type, pid, options, p);
1571 if (!ret)
1572 return ret;
1573
1574 if (unlikely(ret < 0)) {
1575 /*
1576 * If we have not yet seen any eligible child,
1577 * then let this error code replace -ECHILD.
1578 * A permission error will give the user a clue
1579 * to look for security policy problems, rather
1580 * than for mysterious wait bugs.
1581 */
1582 if (*notask_error)
1583 *notask_error = ret;
1584 }
1585
1586 if (likely(!ptrace) && unlikely(p->ptrace)) {
1587 /*
1588 * This child is hidden by ptrace.
1589 * We aren't allowed to see it now, but eventually we will.
1590 */
1591 *notask_error = 0;
1592 return 0;
1593 }
1594
1595 if (p->exit_state == EXIT_DEAD)
1596 return 0;
1597
1598 /*
1599 * We don't reap group leaders with subthreads.
1600 */
1601 if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
1602 return wait_task_zombie(p, options, infop, stat_addr, ru);
1603
1604 /*
1605 * It's stopped or running now, so it might
1606 * later continue, exit, or stop again.
1607 */
1608 *notask_error = 0;
1609
1610 if (task_is_stopped_or_traced(p))
1611 return wait_task_stopped(ptrace, p, options,
1612 infop, stat_addr, ru);
1613
1614 return wait_task_continued(p, options, infop, stat_addr, ru);
1615}
1616
1617/*
1618 * Do the work of do_wait() for one thread in the group, @tsk.
1619 *
1620 * -ECHILD should be in *@notask_error before the first call.
1621 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1622 * Returns zero if the search for a child should continue; then
1623 * *@notask_error is 0 if there were any eligible children,
1624 * or another error from security_task_wait(), or still -ECHILD.
1625 */
1626static int do_wait_thread(struct task_struct *tsk, int *notask_error,
1627 enum pid_type type, struct pid *pid, int options,
1628 struct siginfo __user *infop, int __user *stat_addr,
1629 struct rusage __user *ru)
1630{
1631 struct task_struct *p;
1632
1633 list_for_each_entry(p, &tsk->children, sibling) {
1634 /*
1635 * Do not consider detached threads.
1636 */
1637 if (!task_detached(p)) {
1638 int ret = wait_consider_task(tsk, 0, p, notask_error,
1639 type, pid, options,
1640 infop, stat_addr, ru);
1641 if (ret)
1642 return ret;
1643 }
1644 }
1645
1646 return 0;
1647}
1648
1649static int ptrace_do_wait(struct task_struct *tsk, int *notask_error,
1650 enum pid_type type, struct pid *pid, int options,
1651 struct siginfo __user *infop, int __user *stat_addr,
1652 struct rusage __user *ru)
1653{
1654 struct task_struct *p;
1655
1656 /*
1657 * Traditionally we see ptrace'd stopped tasks regardless of options.
1658 */
1659 options |= WUNTRACED;
1660
1661 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1662 int ret = wait_consider_task(tsk, 1, p, notask_error,
1663 type, pid, options,
1664 infop, stat_addr, ru);
1665 if (ret)
1666 return ret;
1667 }
1668
1669 return 0;
1670}
1671
1522static long do_wait(enum pid_type type, struct pid *pid, int options, 1672static long do_wait(enum pid_type type, struct pid *pid, int options,
1523 struct siginfo __user *infop, int __user *stat_addr, 1673 struct siginfo __user *infop, int __user *stat_addr,
1524 struct rusage __user *ru) 1674 struct rusage __user *ru)
1525{ 1675{
1526 DECLARE_WAITQUEUE(wait, current); 1676 DECLARE_WAITQUEUE(wait, current);
1527 struct task_struct *tsk; 1677 struct task_struct *tsk;
1528 int flag, retval; 1678 int retval;
1529 1679
1530 add_wait_queue(&current->signal->wait_chldexit,&wait); 1680 add_wait_queue(&current->signal->wait_chldexit,&wait);
1531repeat: 1681repeat:
1532 /* If there is nothing that can match our critier just get out */ 1682 /*
1683 * If there is nothing that can match our critiera just get out.
1684 * We will clear @retval to zero if we see any child that might later
1685 * match our criteria, even if we are not able to reap it yet.
1686 */
1533 retval = -ECHILD; 1687 retval = -ECHILD;
1534 if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type]))) 1688 if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type])))
1535 goto end; 1689 goto end;
1536 1690
1537 /*
1538 * We will set this flag if we see any child that might later
1539 * match our criteria, even if we are not able to reap it yet.
1540 */
1541 flag = retval = 0;
1542 current->state = TASK_INTERRUPTIBLE; 1691 current->state = TASK_INTERRUPTIBLE;
1543 read_lock(&tasklist_lock); 1692 read_lock(&tasklist_lock);
1544 tsk = current; 1693 tsk = current;
1545 do { 1694 do {
1546 struct task_struct *p; 1695 int tsk_result = do_wait_thread(tsk, &retval,
1547 1696 type, pid, options,
1548 list_for_each_entry(p, &tsk->children, sibling) { 1697 infop, stat_addr, ru);
1549 int ret = eligible_child(type, pid, options, p); 1698 if (!tsk_result)
1550 if (!ret) 1699 tsk_result = ptrace_do_wait(tsk, &retval,
1551 continue; 1700 type, pid, options,
1552 1701 infop, stat_addr, ru);
1553 if (unlikely(ret < 0)) { 1702 if (tsk_result) {
1554 retval = ret; 1703 /*
1555 } else if (task_is_stopped_or_traced(p)) { 1704 * tasklist_lock is unlocked and we have a final result.
1556 /* 1705 */
1557 * It's stopped now, so it might later 1706 retval = tsk_result;
1558 * continue, exit, or stop again. 1707 goto end;
1559 */
1560 flag = 1;
1561 if (!(p->ptrace & PT_PTRACED) &&
1562 !(options & WUNTRACED))
1563 continue;
1564
1565 retval = wait_task_stopped(p,
1566 (options & WNOWAIT), infop,
1567 stat_addr, ru);
1568 } else if (p->exit_state == EXIT_ZOMBIE &&
1569 !delay_group_leader(p)) {
1570 /*
1571 * We don't reap group leaders with subthreads.
1572 */
1573 if (!likely(options & WEXITED))
1574 continue;
1575 retval = wait_task_zombie(p,
1576 (options & WNOWAIT), infop,
1577 stat_addr, ru);
1578 } else if (p->exit_state != EXIT_DEAD) {
1579 /*
1580 * It's running now, so it might later
1581 * exit, stop, or stop and then continue.
1582 */
1583 flag = 1;
1584 if (!unlikely(options & WCONTINUED))
1585 continue;
1586 retval = wait_task_continued(p,
1587 (options & WNOWAIT), infop,
1588 stat_addr, ru);
1589 }
1590 if (retval != 0) /* tasklist_lock released */
1591 goto end;
1592 }
1593 if (!flag) {
1594 list_for_each_entry(p, &tsk->ptrace_children,
1595 ptrace_list) {
1596 flag = eligible_child(type, pid, options, p);
1597 if (!flag)
1598 continue;
1599 if (likely(flag > 0))
1600 break;
1601 retval = flag;
1602 goto end;
1603 }
1604 } 1708 }
1709
1605 if (options & __WNOTHREAD) 1710 if (options & __WNOTHREAD)
1606 break; 1711 break;
1607 tsk = next_thread(tsk); 1712 tsk = next_thread(tsk);
@@ -1609,16 +1714,14 @@ repeat:
1609 } while (tsk != current); 1714 } while (tsk != current);
1610 read_unlock(&tasklist_lock); 1715 read_unlock(&tasklist_lock);
1611 1716
1612 if (flag) { 1717 if (!retval && !(options & WNOHANG)) {
1613 if (options & WNOHANG)
1614 goto end;
1615 retval = -ERESTARTSYS; 1718 retval = -ERESTARTSYS;
1616 if (signal_pending(current)) 1719 if (!signal_pending(current)) {
1617 goto end; 1720 schedule();
1618 schedule(); 1721 goto repeat;
1619 goto repeat; 1722 }
1620 } 1723 }
1621 retval = -ECHILD; 1724
1622end: 1725end:
1623 current->state = TASK_RUNNING; 1726 current->state = TASK_RUNNING;
1624 remove_wait_queue(&current->signal->wait_chldexit,&wait); 1727 remove_wait_queue(&current->signal->wait_chldexit,&wait);
diff --git a/kernel/fork.c b/kernel/fork.c
index 4bd2f516401f..adefc1131f27 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1125,8 +1125,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1125 */ 1125 */
1126 p->group_leader = p; 1126 p->group_leader = p;
1127 INIT_LIST_HEAD(&p->thread_group); 1127 INIT_LIST_HEAD(&p->thread_group);
1128 INIT_LIST_HEAD(&p->ptrace_children); 1128 INIT_LIST_HEAD(&p->ptrace_entry);
1129 INIT_LIST_HEAD(&p->ptrace_list); 1129 INIT_LIST_HEAD(&p->ptraced);
1130 1130
1131 /* Now that the task is set up, run cgroup callbacks if 1131 /* Now that the task is set up, run cgroup callbacks if
1132 * necessary. We need to run them before the task is visible 1132 * necessary. We need to run them before the task is visible
@@ -1198,7 +1198,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
1198 } 1198 }
1199 1199
1200 if (likely(p->pid)) { 1200 if (likely(p->pid)) {
1201 add_parent(p); 1201 list_add_tail(&p->sibling, &p->real_parent->children);
1202 if (unlikely(p->ptrace & PT_PTRACED)) 1202 if (unlikely(p->ptrace & PT_PTRACED))
1203 __ptrace_link(p, current->parent); 1203 __ptrace_link(p, current->parent);
1204 1204
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 97747cdd37c9..ac3fb7326641 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -235,7 +235,7 @@ int kthreadd(void *unused)
235 set_user_nice(tsk, KTHREAD_NICE_LEVEL); 235 set_user_nice(tsk, KTHREAD_NICE_LEVEL);
236 set_cpus_allowed(tsk, CPU_MASK_ALL); 236 set_cpus_allowed(tsk, CPU_MASK_ALL);
237 237
238 current->flags |= PF_NOFREEZE; 238 current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;
239 239
240 for (;;) { 240 for (;;) {
241 set_current_state(TASK_INTERRUPTIBLE); 241 set_current_state(TASK_INTERRUPTIBLE);
diff --git a/kernel/power/disk.c b/kernel/power/disk.c
index 14a656cdc652..f011e0870b52 100644
--- a/kernel/power/disk.c
+++ b/kernel/power/disk.c
@@ -180,6 +180,17 @@ static void platform_restore_cleanup(int platform_mode)
180} 180}
181 181
182/** 182/**
183 * platform_recover - recover the platform from a failure to suspend
184 * devices.
185 */
186
187static void platform_recover(int platform_mode)
188{
189 if (platform_mode && hibernation_ops && hibernation_ops->recover)
190 hibernation_ops->recover();
191}
192
193/**
183 * create_image - freeze devices that need to be frozen with interrupts 194 * create_image - freeze devices that need to be frozen with interrupts
184 * off, create the hibernation image and thaw those devices. Control 195 * off, create the hibernation image and thaw those devices. Control
185 * reappears in this routine after a restore. 196 * reappears in this routine after a restore.
@@ -193,6 +204,7 @@ static int create_image(int platform_mode)
193 if (error) 204 if (error)
194 return error; 205 return error;
195 206
207 device_pm_lock();
196 local_irq_disable(); 208 local_irq_disable();
197 /* At this point, device_suspend() has been called, but *not* 209 /* At this point, device_suspend() has been called, but *not*
198 * device_power_down(). We *must* call device_power_down() now. 210 * device_power_down(). We *must* call device_power_down() now.
@@ -224,9 +236,11 @@ static int create_image(int platform_mode)
224 /* NOTE: device_power_up() is just a resume() for devices 236 /* NOTE: device_power_up() is just a resume() for devices
225 * that suspended with irqs off ... no overall powerup. 237 * that suspended with irqs off ... no overall powerup.
226 */ 238 */
227 device_power_up(); 239 device_power_up(in_suspend ?
240 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
228 Enable_irqs: 241 Enable_irqs:
229 local_irq_enable(); 242 local_irq_enable();
243 device_pm_unlock();
230 return error; 244 return error;
231} 245}
232 246
@@ -255,10 +269,10 @@ int hibernation_snapshot(int platform_mode)
255 suspend_console(); 269 suspend_console();
256 error = device_suspend(PMSG_FREEZE); 270 error = device_suspend(PMSG_FREEZE);
257 if (error) 271 if (error)
258 goto Resume_console; 272 goto Recover_platform;
259 273
260 if (hibernation_test(TEST_DEVICES)) 274 if (hibernation_test(TEST_DEVICES))
261 goto Resume_devices; 275 goto Recover_platform;
262 276
263 error = platform_pre_snapshot(platform_mode); 277 error = platform_pre_snapshot(platform_mode);
264 if (error || hibernation_test(TEST_PLATFORM)) 278 if (error || hibernation_test(TEST_PLATFORM))
@@ -280,12 +294,16 @@ int hibernation_snapshot(int platform_mode)
280 Finish: 294 Finish:
281 platform_finish(platform_mode); 295 platform_finish(platform_mode);
282 Resume_devices: 296 Resume_devices:
283 device_resume(); 297 device_resume(in_suspend ?
284 Resume_console: 298 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
285 resume_console(); 299 resume_console();
286 Close: 300 Close:
287 platform_end(platform_mode); 301 platform_end(platform_mode);
288 return error; 302 return error;
303
304 Recover_platform:
305 platform_recover(platform_mode);
306 goto Resume_devices;
289} 307}
290 308
291/** 309/**
@@ -300,8 +318,9 @@ static int resume_target_kernel(void)
300{ 318{
301 int error; 319 int error;
302 320
321 device_pm_lock();
303 local_irq_disable(); 322 local_irq_disable();
304 error = device_power_down(PMSG_PRETHAW); 323 error = device_power_down(PMSG_QUIESCE);
305 if (error) { 324 if (error) {
306 printk(KERN_ERR "PM: Some devices failed to power down, " 325 printk(KERN_ERR "PM: Some devices failed to power down, "
307 "aborting resume\n"); 326 "aborting resume\n");
@@ -329,9 +348,10 @@ static int resume_target_kernel(void)
329 swsusp_free(); 348 swsusp_free();
330 restore_processor_state(); 349 restore_processor_state();
331 touch_softlockup_watchdog(); 350 touch_softlockup_watchdog();
332 device_power_up(); 351 device_power_up(PMSG_RECOVER);
333 Enable_irqs: 352 Enable_irqs:
334 local_irq_enable(); 353 local_irq_enable();
354 device_pm_unlock();
335 return error; 355 return error;
336} 356}
337 357
@@ -350,7 +370,7 @@ int hibernation_restore(int platform_mode)
350 370
351 pm_prepare_console(); 371 pm_prepare_console();
352 suspend_console(); 372 suspend_console();
353 error = device_suspend(PMSG_PRETHAW); 373 error = device_suspend(PMSG_QUIESCE);
354 if (error) 374 if (error)
355 goto Finish; 375 goto Finish;
356 376
@@ -362,7 +382,7 @@ int hibernation_restore(int platform_mode)
362 enable_nonboot_cpus(); 382 enable_nonboot_cpus();
363 } 383 }
364 platform_restore_cleanup(platform_mode); 384 platform_restore_cleanup(platform_mode);
365 device_resume(); 385 device_resume(PMSG_RECOVER);
366 Finish: 386 Finish:
367 resume_console(); 387 resume_console();
368 pm_restore_console(); 388 pm_restore_console();
@@ -392,8 +412,11 @@ int hibernation_platform_enter(void)
392 412
393 suspend_console(); 413 suspend_console();
394 error = device_suspend(PMSG_HIBERNATE); 414 error = device_suspend(PMSG_HIBERNATE);
395 if (error) 415 if (error) {
396 goto Resume_console; 416 if (hibernation_ops->recover)
417 hibernation_ops->recover();
418 goto Resume_devices;
419 }
397 420
398 error = hibernation_ops->prepare(); 421 error = hibernation_ops->prepare();
399 if (error) 422 if (error)
@@ -403,6 +426,7 @@ int hibernation_platform_enter(void)
403 if (error) 426 if (error)
404 goto Finish; 427 goto Finish;
405 428
429 device_pm_lock();
406 local_irq_disable(); 430 local_irq_disable();
407 error = device_power_down(PMSG_HIBERNATE); 431 error = device_power_down(PMSG_HIBERNATE);
408 if (!error) { 432 if (!error) {
@@ -411,6 +435,7 @@ int hibernation_platform_enter(void)
411 while (1); 435 while (1);
412 } 436 }
413 local_irq_enable(); 437 local_irq_enable();
438 device_pm_unlock();
414 439
415 /* 440 /*
416 * We don't need to reenable the nonboot CPUs or resume consoles, since 441 * We don't need to reenable the nonboot CPUs or resume consoles, since
@@ -419,8 +444,7 @@ int hibernation_platform_enter(void)
419 Finish: 444 Finish:
420 hibernation_ops->finish(); 445 hibernation_ops->finish();
421 Resume_devices: 446 Resume_devices:
422 device_resume(); 447 device_resume(PMSG_RESTORE);
423 Resume_console:
424 resume_console(); 448 resume_console();
425 Close: 449 Close:
426 hibernation_ops->end(); 450 hibernation_ops->end();
diff --git a/kernel/power/main.c b/kernel/power/main.c
index 6a6d5eb3524e..3398f4651aa1 100644
--- a/kernel/power/main.c
+++ b/kernel/power/main.c
@@ -228,6 +228,7 @@ static int suspend_enter(suspend_state_t state)
228{ 228{
229 int error = 0; 229 int error = 0;
230 230
231 device_pm_lock();
231 arch_suspend_disable_irqs(); 232 arch_suspend_disable_irqs();
232 BUG_ON(!irqs_disabled()); 233 BUG_ON(!irqs_disabled());
233 234
@@ -239,10 +240,11 @@ static int suspend_enter(suspend_state_t state)
239 if (!suspend_test(TEST_CORE)) 240 if (!suspend_test(TEST_CORE))
240 error = suspend_ops->enter(state); 241 error = suspend_ops->enter(state);
241 242
242 device_power_up(); 243 device_power_up(PMSG_RESUME);
243 Done: 244 Done:
244 arch_suspend_enable_irqs(); 245 arch_suspend_enable_irqs();
245 BUG_ON(irqs_disabled()); 246 BUG_ON(irqs_disabled());
247 device_pm_unlock();
246 return error; 248 return error;
247} 249}
248 250
@@ -267,11 +269,11 @@ int suspend_devices_and_enter(suspend_state_t state)
267 error = device_suspend(PMSG_SUSPEND); 269 error = device_suspend(PMSG_SUSPEND);
268 if (error) { 270 if (error) {
269 printk(KERN_ERR "PM: Some devices failed to suspend\n"); 271 printk(KERN_ERR "PM: Some devices failed to suspend\n");
270 goto Resume_console; 272 goto Recover_platform;
271 } 273 }
272 274
273 if (suspend_test(TEST_DEVICES)) 275 if (suspend_test(TEST_DEVICES))
274 goto Resume_devices; 276 goto Recover_platform;
275 277
276 if (suspend_ops->prepare) { 278 if (suspend_ops->prepare) {
277 error = suspend_ops->prepare(); 279 error = suspend_ops->prepare();
@@ -291,13 +293,17 @@ int suspend_devices_and_enter(suspend_state_t state)
291 if (suspend_ops->finish) 293 if (suspend_ops->finish)
292 suspend_ops->finish(); 294 suspend_ops->finish();
293 Resume_devices: 295 Resume_devices:
294 device_resume(); 296 device_resume(PMSG_RESUME);
295 Resume_console:
296 resume_console(); 297 resume_console();
297 Close: 298 Close:
298 if (suspend_ops->end) 299 if (suspend_ops->end)
299 suspend_ops->end(); 300 suspend_ops->end();
300 return error; 301 return error;
302
303 Recover_platform:
304 if (suspend_ops->recover)
305 suspend_ops->recover();
306 goto Resume_devices;
301} 307}
302 308
303/** 309/**
diff --git a/kernel/power/process.c b/kernel/power/process.c
index f1d0b345c9ba..5fb87652f214 100644
--- a/kernel/power/process.c
+++ b/kernel/power/process.c
@@ -19,9 +19,6 @@
19 */ 19 */
20#define TIMEOUT (20 * HZ) 20#define TIMEOUT (20 * HZ)
21 21
22#define FREEZER_KERNEL_THREADS 0
23#define FREEZER_USER_SPACE 1
24
25static inline int freezeable(struct task_struct * p) 22static inline int freezeable(struct task_struct * p)
26{ 23{
27 if ((p == current) || 24 if ((p == current) ||
@@ -84,63 +81,53 @@ static void fake_signal_wake_up(struct task_struct *p)
84 spin_unlock_irqrestore(&p->sighand->siglock, flags); 81 spin_unlock_irqrestore(&p->sighand->siglock, flags);
85} 82}
86 83
87static int has_mm(struct task_struct *p) 84static inline bool should_send_signal(struct task_struct *p)
88{ 85{
89 return (p->mm && !(p->flags & PF_BORROWED_MM)); 86 return !(p->flags & PF_FREEZER_NOSIG);
90} 87}
91 88
92/** 89/**
93 * freeze_task - send a freeze request to given task 90 * freeze_task - send a freeze request to given task
94 * @p: task to send the request to 91 * @p: task to send the request to
95 * @with_mm_only: if set, the request will only be sent if the task has its 92 * @sig_only: if set, the request will only be sent if the task has the
96 * own mm 93 * PF_FREEZER_NOSIG flag unset
97 * Return value: 0, if @with_mm_only is set and the task has no mm of its 94 * Return value: 'false', if @sig_only is set and the task has
98 * own or the task is frozen, 1, otherwise 95 * PF_FREEZER_NOSIG set or the task is frozen, 'true', otherwise
99 * 96 *
100 * The freeze request is sent by seting the tasks's TIF_FREEZE flag and 97 * The freeze request is sent by setting the tasks's TIF_FREEZE flag and
101 * either sending a fake signal to it or waking it up, depending on whether 98 * either sending a fake signal to it or waking it up, depending on whether
102 * or not it has its own mm (ie. it is a user land task). If @with_mm_only 99 * or not it has PF_FREEZER_NOSIG set. If @sig_only is set and the task
103 * is set and the task has no mm of its own (ie. it is a kernel thread), 100 * has PF_FREEZER_NOSIG set (ie. it is a typical kernel thread), its
104 * its TIF_FREEZE flag should not be set. 101 * TIF_FREEZE flag will not be set.
105 *
106 * The task_lock() is necessary to prevent races with exit_mm() or
107 * use_mm()/unuse_mm() from occuring.
108 */ 102 */
109static int freeze_task(struct task_struct *p, int with_mm_only) 103static bool freeze_task(struct task_struct *p, bool sig_only)
110{ 104{
111 int ret = 1; 105 /*
106 * We first check if the task is freezing and next if it has already
107 * been frozen to avoid the race with frozen_process() which first marks
108 * the task as frozen and next clears its TIF_FREEZE.
109 */
110 if (!freezing(p)) {
111 rmb();
112 if (frozen(p))
113 return false;
112 114
113 task_lock(p); 115 if (!sig_only || should_send_signal(p))
114 if (freezing(p)) { 116 set_freeze_flag(p);
115 if (has_mm(p)) { 117 else
116 if (!signal_pending(p)) 118 return false;
117 fake_signal_wake_up(p); 119 }
118 } else { 120
119 if (with_mm_only) 121 if (should_send_signal(p)) {
120 ret = 0; 122 if (!signal_pending(p))
121 else 123 fake_signal_wake_up(p);
122 wake_up_state(p, TASK_INTERRUPTIBLE); 124 } else if (sig_only) {
123 } 125 return false;
124 } else { 126 } else {
125 rmb(); 127 wake_up_state(p, TASK_INTERRUPTIBLE);
126 if (frozen(p)) {
127 ret = 0;
128 } else {
129 if (has_mm(p)) {
130 set_freeze_flag(p);
131 fake_signal_wake_up(p);
132 } else {
133 if (with_mm_only) {
134 ret = 0;
135 } else {
136 set_freeze_flag(p);
137 wake_up_state(p, TASK_INTERRUPTIBLE);
138 }
139 }
140 }
141 } 128 }
142 task_unlock(p); 129
143 return ret; 130 return true;
144} 131}
145 132
146static void cancel_freezing(struct task_struct *p) 133static void cancel_freezing(struct task_struct *p)
@@ -156,7 +143,7 @@ static void cancel_freezing(struct task_struct *p)
156 } 143 }
157} 144}
158 145
159static int try_to_freeze_tasks(int freeze_user_space) 146static int try_to_freeze_tasks(bool sig_only)
160{ 147{
161 struct task_struct *g, *p; 148 struct task_struct *g, *p;
162 unsigned long end_time; 149 unsigned long end_time;
@@ -175,7 +162,7 @@ static int try_to_freeze_tasks(int freeze_user_space)
175 if (frozen(p) || !freezeable(p)) 162 if (frozen(p) || !freezeable(p))
176 continue; 163 continue;
177 164
178 if (!freeze_task(p, freeze_user_space)) 165 if (!freeze_task(p, sig_only))
179 continue; 166 continue;
180 167
181 /* 168 /*
@@ -235,13 +222,13 @@ int freeze_processes(void)
235 int error; 222 int error;
236 223
237 printk("Freezing user space processes ... "); 224 printk("Freezing user space processes ... ");
238 error = try_to_freeze_tasks(FREEZER_USER_SPACE); 225 error = try_to_freeze_tasks(true);
239 if (error) 226 if (error)
240 goto Exit; 227 goto Exit;
241 printk("done.\n"); 228 printk("done.\n");
242 229
243 printk("Freezing remaining freezable tasks ... "); 230 printk("Freezing remaining freezable tasks ... ");
244 error = try_to_freeze_tasks(FREEZER_KERNEL_THREADS); 231 error = try_to_freeze_tasks(false);
245 if (error) 232 if (error)
246 goto Exit; 233 goto Exit;
247 printk("done."); 234 printk("done.");
@@ -251,7 +238,7 @@ int freeze_processes(void)
251 return error; 238 return error;
252} 239}
253 240
254static void thaw_tasks(int thaw_user_space) 241static void thaw_tasks(bool nosig_only)
255{ 242{
256 struct task_struct *g, *p; 243 struct task_struct *g, *p;
257 244
@@ -260,7 +247,7 @@ static void thaw_tasks(int thaw_user_space)
260 if (!freezeable(p)) 247 if (!freezeable(p))
261 continue; 248 continue;
262 249
263 if (!p->mm == thaw_user_space) 250 if (nosig_only && should_send_signal(p))
264 continue; 251 continue;
265 252
266 thaw_process(p); 253 thaw_process(p);
@@ -271,8 +258,8 @@ static void thaw_tasks(int thaw_user_space)
271void thaw_processes(void) 258void thaw_processes(void)
272{ 259{
273 printk("Restarting tasks ... "); 260 printk("Restarting tasks ... ");
274 thaw_tasks(FREEZER_KERNEL_THREADS); 261 thaw_tasks(true);
275 thaw_tasks(FREEZER_USER_SPACE); 262 thaw_tasks(false);
276 schedule(); 263 schedule();
277 printk("done.\n"); 264 printk("done.\n");
278} 265}
diff --git a/kernel/power/user.c b/kernel/power/user.c
index f5512cb3aa86..a6332a313262 100644
--- a/kernel/power/user.c
+++ b/kernel/power/user.c
@@ -23,6 +23,7 @@
23#include <linux/console.h> 23#include <linux/console.h>
24#include <linux/cpu.h> 24#include <linux/cpu.h>
25#include <linux/freezer.h> 25#include <linux/freezer.h>
26#include <linux/smp_lock.h>
26 27
27#include <asm/uaccess.h> 28#include <asm/uaccess.h>
28 29
@@ -69,16 +70,22 @@ static int snapshot_open(struct inode *inode, struct file *filp)
69 struct snapshot_data *data; 70 struct snapshot_data *data;
70 int error; 71 int error;
71 72
72 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) 73 mutex_lock(&pm_mutex);
73 return -EBUSY; 74
75 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
76 error = -EBUSY;
77 goto Unlock;
78 }
74 79
75 if ((filp->f_flags & O_ACCMODE) == O_RDWR) { 80 if ((filp->f_flags & O_ACCMODE) == O_RDWR) {
76 atomic_inc(&snapshot_device_available); 81 atomic_inc(&snapshot_device_available);
77 return -ENOSYS; 82 error = -ENOSYS;
83 goto Unlock;
78 } 84 }
79 if(create_basic_memory_bitmaps()) { 85 if(create_basic_memory_bitmaps()) {
80 atomic_inc(&snapshot_device_available); 86 atomic_inc(&snapshot_device_available);
81 return -ENOMEM; 87 error = -ENOMEM;
88 goto Unlock;
82 } 89 }
83 nonseekable_open(inode, filp); 90 nonseekable_open(inode, filp);
84 data = &snapshot_state; 91 data = &snapshot_state;
@@ -98,33 +105,36 @@ static int snapshot_open(struct inode *inode, struct file *filp)
98 if (error) 105 if (error)
99 pm_notifier_call_chain(PM_POST_HIBERNATION); 106 pm_notifier_call_chain(PM_POST_HIBERNATION);
100 } 107 }
101 if (error) { 108 if (error)
102 atomic_inc(&snapshot_device_available); 109 atomic_inc(&snapshot_device_available);
103 return error;
104 }
105 data->frozen = 0; 110 data->frozen = 0;
106 data->ready = 0; 111 data->ready = 0;
107 data->platform_support = 0; 112 data->platform_support = 0;
108 113
109 return 0; 114 Unlock:
115 mutex_unlock(&pm_mutex);
116
117 return error;
110} 118}
111 119
112static int snapshot_release(struct inode *inode, struct file *filp) 120static int snapshot_release(struct inode *inode, struct file *filp)
113{ 121{
114 struct snapshot_data *data; 122 struct snapshot_data *data;
115 123
124 mutex_lock(&pm_mutex);
125
116 swsusp_free(); 126 swsusp_free();
117 free_basic_memory_bitmaps(); 127 free_basic_memory_bitmaps();
118 data = filp->private_data; 128 data = filp->private_data;
119 free_all_swap_pages(data->swap); 129 free_all_swap_pages(data->swap);
120 if (data->frozen) { 130 if (data->frozen)
121 mutex_lock(&pm_mutex);
122 thaw_processes(); 131 thaw_processes();
123 mutex_unlock(&pm_mutex);
124 }
125 pm_notifier_call_chain(data->mode == O_WRONLY ? 132 pm_notifier_call_chain(data->mode == O_WRONLY ?
126 PM_POST_HIBERNATION : PM_POST_RESTORE); 133 PM_POST_HIBERNATION : PM_POST_RESTORE);
127 atomic_inc(&snapshot_device_available); 134 atomic_inc(&snapshot_device_available);
135
136 mutex_unlock(&pm_mutex);
137
128 return 0; 138 return 0;
129} 139}
130 140
@@ -134,9 +144,13 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf,
134 struct snapshot_data *data; 144 struct snapshot_data *data;
135 ssize_t res; 145 ssize_t res;
136 146
147 mutex_lock(&pm_mutex);
148
137 data = filp->private_data; 149 data = filp->private_data;
138 if (!data->ready) 150 if (!data->ready) {
139 return -ENODATA; 151 res = -ENODATA;
152 goto Unlock;
153 }
140 res = snapshot_read_next(&data->handle, count); 154 res = snapshot_read_next(&data->handle, count);
141 if (res > 0) { 155 if (res > 0) {
142 if (copy_to_user(buf, data_of(data->handle), res)) 156 if (copy_to_user(buf, data_of(data->handle), res))
@@ -144,6 +158,10 @@ static ssize_t snapshot_read(struct file *filp, char __user *buf,
144 else 158 else
145 *offp = data->handle.offset; 159 *offp = data->handle.offset;
146 } 160 }
161
162 Unlock:
163 mutex_unlock(&pm_mutex);
164
147 return res; 165 return res;
148} 166}
149 167
@@ -153,6 +171,8 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
153 struct snapshot_data *data; 171 struct snapshot_data *data;
154 ssize_t res; 172 ssize_t res;
155 173
174 mutex_lock(&pm_mutex);
175
156 data = filp->private_data; 176 data = filp->private_data;
157 res = snapshot_write_next(&data->handle, count); 177 res = snapshot_write_next(&data->handle, count);
158 if (res > 0) { 178 if (res > 0) {
@@ -161,11 +181,14 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
161 else 181 else
162 *offp = data->handle.offset; 182 *offp = data->handle.offset;
163 } 183 }
184
185 mutex_unlock(&pm_mutex);
186
164 return res; 187 return res;
165} 188}
166 189
167static int snapshot_ioctl(struct inode *inode, struct file *filp, 190static long snapshot_ioctl(struct file *filp, unsigned int cmd,
168 unsigned int cmd, unsigned long arg) 191 unsigned long arg)
169{ 192{
170 int error = 0; 193 int error = 0;
171 struct snapshot_data *data; 194 struct snapshot_data *data;
@@ -179,6 +202,9 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
179 if (!capable(CAP_SYS_ADMIN)) 202 if (!capable(CAP_SYS_ADMIN))
180 return -EPERM; 203 return -EPERM;
181 204
205 if (!mutex_trylock(&pm_mutex))
206 return -EBUSY;
207
182 data = filp->private_data; 208 data = filp->private_data;
183 209
184 switch (cmd) { 210 switch (cmd) {
@@ -186,7 +212,6 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
186 case SNAPSHOT_FREEZE: 212 case SNAPSHOT_FREEZE:
187 if (data->frozen) 213 if (data->frozen)
188 break; 214 break;
189 mutex_lock(&pm_mutex);
190 printk("Syncing filesystems ... "); 215 printk("Syncing filesystems ... ");
191 sys_sync(); 216 sys_sync();
192 printk("done.\n"); 217 printk("done.\n");
@@ -194,7 +219,6 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
194 error = freeze_processes(); 219 error = freeze_processes();
195 if (error) 220 if (error)
196 thaw_processes(); 221 thaw_processes();
197 mutex_unlock(&pm_mutex);
198 if (!error) 222 if (!error)
199 data->frozen = 1; 223 data->frozen = 1;
200 break; 224 break;
@@ -202,9 +226,7 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
202 case SNAPSHOT_UNFREEZE: 226 case SNAPSHOT_UNFREEZE:
203 if (!data->frozen || data->ready) 227 if (!data->frozen || data->ready)
204 break; 228 break;
205 mutex_lock(&pm_mutex);
206 thaw_processes(); 229 thaw_processes();
207 mutex_unlock(&pm_mutex);
208 data->frozen = 0; 230 data->frozen = 0;
209 break; 231 break;
210 232
@@ -307,16 +329,11 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
307 error = -EPERM; 329 error = -EPERM;
308 break; 330 break;
309 } 331 }
310 if (!mutex_trylock(&pm_mutex)) {
311 error = -EBUSY;
312 break;
313 }
314 /* 332 /*
315 * Tasks are frozen and the notifiers have been called with 333 * Tasks are frozen and the notifiers have been called with
316 * PM_HIBERNATION_PREPARE 334 * PM_HIBERNATION_PREPARE
317 */ 335 */
318 error = suspend_devices_and_enter(PM_SUSPEND_MEM); 336 error = suspend_devices_and_enter(PM_SUSPEND_MEM);
319 mutex_unlock(&pm_mutex);
320 break; 337 break;
321 338
322 case SNAPSHOT_PLATFORM_SUPPORT: 339 case SNAPSHOT_PLATFORM_SUPPORT:
@@ -390,6 +407,8 @@ static int snapshot_ioctl(struct inode *inode, struct file *filp,
390 407
391 } 408 }
392 409
410 mutex_unlock(&pm_mutex);
411
393 return error; 412 return error;
394} 413}
395 414
@@ -399,7 +418,7 @@ static const struct file_operations snapshot_fops = {
399 .read = snapshot_read, 418 .read = snapshot_read,
400 .write = snapshot_write, 419 .write = snapshot_write,
401 .llseek = no_llseek, 420 .llseek = no_llseek,
402 .ioctl = snapshot_ioctl, 421 .unlocked_ioctl = snapshot_ioctl,
403}; 422};
404 423
405static struct miscdevice snapshot_device = { 424static struct miscdevice snapshot_device = {
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index e337390fce01..8392a9da6450 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -33,13 +33,9 @@
33 */ 33 */
34void __ptrace_link(struct task_struct *child, struct task_struct *new_parent) 34void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
35{ 35{
36 BUG_ON(!list_empty(&child->ptrace_list)); 36 BUG_ON(!list_empty(&child->ptrace_entry));
37 if (child->parent == new_parent) 37 list_add(&child->ptrace_entry, &new_parent->ptraced);
38 return;
39 list_add(&child->ptrace_list, &child->parent->ptrace_children);
40 remove_parent(child);
41 child->parent = new_parent; 38 child->parent = new_parent;
42 add_parent(child);
43} 39}
44 40
45/* 41/*
@@ -73,12 +69,8 @@ void __ptrace_unlink(struct task_struct *child)
73 BUG_ON(!child->ptrace); 69 BUG_ON(!child->ptrace);
74 70
75 child->ptrace = 0; 71 child->ptrace = 0;
76 if (ptrace_reparented(child)) { 72 child->parent = child->real_parent;
77 list_del_init(&child->ptrace_list); 73 list_del_init(&child->ptrace_entry);
78 remove_parent(child);
79 child->parent = child->real_parent;
80 add_parent(child);
81 }
82 74
83 if (task_is_traced(child)) 75 if (task_is_traced(child))
84 ptrace_untrace(child); 76 ptrace_untrace(child);
@@ -492,15 +484,34 @@ int ptrace_traceme(void)
492 /* 484 /*
493 * Are we already being traced? 485 * Are we already being traced?
494 */ 486 */
487repeat:
495 task_lock(current); 488 task_lock(current);
496 if (!(current->ptrace & PT_PTRACED)) { 489 if (!(current->ptrace & PT_PTRACED)) {
490 /*
491 * See ptrace_attach() comments about the locking here.
492 */
493 unsigned long flags;
494 if (!write_trylock_irqsave(&tasklist_lock, flags)) {
495 task_unlock(current);
496 do {
497 cpu_relax();
498 } while (!write_can_lock(&tasklist_lock));
499 goto repeat;
500 }
501
497 ret = security_ptrace(current->parent, current, 502 ret = security_ptrace(current->parent, current,
498 PTRACE_MODE_ATTACH); 503 PTRACE_MODE_ATTACH);
504
499 /* 505 /*
500 * Set the ptrace bit in the process ptrace flags. 506 * Set the ptrace bit in the process ptrace flags.
507 * Then link us on our parent's ptraced list.
501 */ 508 */
502 if (!ret) 509 if (!ret) {
503 current->ptrace |= PT_PTRACED; 510 current->ptrace |= PT_PTRACED;
511 __ptrace_link(current, current->real_parent);
512 }
513
514 write_unlock_irqrestore(&tasklist_lock, flags);
504 } 515 }
505 task_unlock(current); 516 task_unlock(current);
506 return ret; 517 return ret;
diff --git a/lib/Makefile b/lib/Makefile
index 2c62a9c06fbe..818c4d455518 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -2,21 +2,17 @@
2# Makefile for some libs needed in the kernel. 2# Makefile for some libs needed in the kernel.
3# 3#
4 4
5ifdef CONFIG_FTRACE
6ORIG_CFLAGS := $(KBUILD_CFLAGS)
7KBUILD_CFLAGS = $(subst -pg,,$(ORIG_CFLAGS))
8endif
9
5lib-y := ctype.o string.o vsprintf.o cmdline.o \ 10lib-y := ctype.o string.o vsprintf.o cmdline.o \
6 rbtree.o radix-tree.o dump_stack.o \ 11 rbtree.o radix-tree.o dump_stack.o \
7 idr.o int_sqrt.o extable.o prio_tree.o \ 12 idr.o int_sqrt.o extable.o prio_tree.o \
8 sha1.o irq_regs.o reciprocal_div.o argv_split.o \ 13 sha1.o irq_regs.o reciprocal_div.o argv_split.o \
9 proportions.o prio_heap.o ratelimit.o 14 proportions.o prio_heap.o ratelimit.o
10 15
11ifdef CONFIG_FTRACE
12# Do not profile string.o, since it may be used in early boot or vdso
13CFLAGS_REMOVE_string.o = -pg
14# Also do not profile any debug utilities
15CFLAGS_REMOVE_spinlock_debug.o = -pg
16CFLAGS_REMOVE_list_debug.o = -pg
17CFLAGS_REMOVE_debugobjects.o = -pg
18endif
19
20lib-$(CONFIG_MMU) += ioremap.o 16lib-$(CONFIG_MMU) += ioremap.o
21lib-$(CONFIG_SMP) += cpumask.o 17lib-$(CONFIG_SMP) += cpumask.o
22 18
diff --git a/lib/kobject.c b/lib/kobject.c
index 718e5101c263..dcade0543bd2 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -439,6 +439,7 @@ out:
439 439
440 return error; 440 return error;
441} 441}
442EXPORT_SYMBOL_GPL(kobject_rename);
442 443
443/** 444/**
444 * kobject_move - move object to another parent 445 * kobject_move - move object to another parent
diff --git a/net/sunrpc/auth_gss/auth_gss.c b/net/sunrpc/auth_gss/auth_gss.c
index cc12d5f5d5da..834a83199bdf 100644
--- a/net/sunrpc/auth_gss/auth_gss.c
+++ b/net/sunrpc/auth_gss/auth_gss.c
@@ -63,22 +63,11 @@ static const struct rpc_credops gss_nullops;
63# define RPCDBG_FACILITY RPCDBG_AUTH 63# define RPCDBG_FACILITY RPCDBG_AUTH
64#endif 64#endif
65 65
66#define NFS_NGROUPS 16 66#define GSS_CRED_SLACK 1024
67
68#define GSS_CRED_SLACK 1024 /* XXX: unused */
69/* length of a krb5 verifier (48), plus data added before arguments when 67/* length of a krb5 verifier (48), plus data added before arguments when
70 * using integrity (two 4-byte integers): */ 68 * using integrity (two 4-byte integers): */
71#define GSS_VERF_SLACK 100 69#define GSS_VERF_SLACK 100
72 70
73/* XXX this define must match the gssd define
74* as it is passed to gssd to signal the use of
75* machine creds should be part of the shared rpc interface */
76
77#define CA_RUN_AS_MACHINE 0x00000200
78
79/* dump the buffer in `emacs-hexl' style */
80#define isprint(c) ((c > 0x1f) && (c < 0x7f))
81
82struct gss_auth { 71struct gss_auth {
83 struct kref kref; 72 struct kref kref;
84 struct rpc_auth rpc_auth; 73 struct rpc_auth rpc_auth;
@@ -146,7 +135,7 @@ simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
146 q = (const void *)((const char *)p + len); 135 q = (const void *)((const char *)p + len);
147 if (unlikely(q > end || q < p)) 136 if (unlikely(q > end || q < p))
148 return ERR_PTR(-EFAULT); 137 return ERR_PTR(-EFAULT);
149 dest->data = kmemdup(p, len, GFP_KERNEL); 138 dest->data = kmemdup(p, len, GFP_NOFS);
150 if (unlikely(dest->data == NULL)) 139 if (unlikely(dest->data == NULL))
151 return ERR_PTR(-ENOMEM); 140 return ERR_PTR(-ENOMEM);
152 dest->len = len; 141 dest->len = len;
@@ -171,7 +160,7 @@ gss_alloc_context(void)
171{ 160{
172 struct gss_cl_ctx *ctx; 161 struct gss_cl_ctx *ctx;
173 162
174 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 163 ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
175 if (ctx != NULL) { 164 if (ctx != NULL) {
176 ctx->gc_proc = RPC_GSS_PROC_DATA; 165 ctx->gc_proc = RPC_GSS_PROC_DATA;
177 ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */ 166 ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
@@ -272,7 +261,7 @@ __gss_find_upcall(struct rpc_inode *rpci, uid_t uid)
272 return NULL; 261 return NULL;
273} 262}
274 263
275/* Try to add a upcall to the pipefs queue. 264/* Try to add an upcall to the pipefs queue.
276 * If an upcall owned by our uid already exists, then we return a reference 265 * If an upcall owned by our uid already exists, then we return a reference
277 * to that upcall instead of adding the new upcall. 266 * to that upcall instead of adding the new upcall.
278 */ 267 */
@@ -341,7 +330,7 @@ gss_alloc_msg(struct gss_auth *gss_auth, uid_t uid)
341{ 330{
342 struct gss_upcall_msg *gss_msg; 331 struct gss_upcall_msg *gss_msg;
343 332
344 gss_msg = kzalloc(sizeof(*gss_msg), GFP_KERNEL); 333 gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
345 if (gss_msg != NULL) { 334 if (gss_msg != NULL) {
346 INIT_LIST_HEAD(&gss_msg->list); 335 INIT_LIST_HEAD(&gss_msg->list);
347 rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq"); 336 rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
@@ -493,7 +482,6 @@ gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
493{ 482{
494 const void *p, *end; 483 const void *p, *end;
495 void *buf; 484 void *buf;
496 struct rpc_clnt *clnt;
497 struct gss_upcall_msg *gss_msg; 485 struct gss_upcall_msg *gss_msg;
498 struct inode *inode = filp->f_path.dentry->d_inode; 486 struct inode *inode = filp->f_path.dentry->d_inode;
499 struct gss_cl_ctx *ctx; 487 struct gss_cl_ctx *ctx;
@@ -503,11 +491,10 @@ gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
503 if (mlen > MSG_BUF_MAXSIZE) 491 if (mlen > MSG_BUF_MAXSIZE)
504 goto out; 492 goto out;
505 err = -ENOMEM; 493 err = -ENOMEM;
506 buf = kmalloc(mlen, GFP_KERNEL); 494 buf = kmalloc(mlen, GFP_NOFS);
507 if (!buf) 495 if (!buf)
508 goto out; 496 goto out;
509 497
510 clnt = RPC_I(inode)->private;
511 err = -EFAULT; 498 err = -EFAULT;
512 if (copy_from_user(buf, src, mlen)) 499 if (copy_from_user(buf, src, mlen))
513 goto err; 500 goto err;
@@ -806,7 +793,7 @@ gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
806 dprintk("RPC: gss_create_cred for uid %d, flavor %d\n", 793 dprintk("RPC: gss_create_cred for uid %d, flavor %d\n",
807 acred->uid, auth->au_flavor); 794 acred->uid, auth->au_flavor);
808 795
809 if (!(cred = kzalloc(sizeof(*cred), GFP_KERNEL))) 796 if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
810 goto out_err; 797 goto out_err;
811 798
812 rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops); 799 rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
diff --git a/net/sunrpc/auth_gss/gss_krb5_mech.c b/net/sunrpc/auth_gss/gss_krb5_mech.c
index 60c3dba545d7..ef45eba22485 100644
--- a/net/sunrpc/auth_gss/gss_krb5_mech.c
+++ b/net/sunrpc/auth_gss/gss_krb5_mech.c
@@ -70,7 +70,7 @@ simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
70 q = (const void *)((const char *)p + len); 70 q = (const void *)((const char *)p + len);
71 if (unlikely(q > end || q < p)) 71 if (unlikely(q > end || q < p))
72 return ERR_PTR(-EFAULT); 72 return ERR_PTR(-EFAULT);
73 res->data = kmemdup(p, len, GFP_KERNEL); 73 res->data = kmemdup(p, len, GFP_NOFS);
74 if (unlikely(res->data == NULL)) 74 if (unlikely(res->data == NULL))
75 return ERR_PTR(-ENOMEM); 75 return ERR_PTR(-ENOMEM);
76 res->len = len; 76 res->len = len;
@@ -131,7 +131,7 @@ gss_import_sec_context_kerberos(const void *p,
131 struct krb5_ctx *ctx; 131 struct krb5_ctx *ctx;
132 int tmp; 132 int tmp;
133 133
134 if (!(ctx = kzalloc(sizeof(*ctx), GFP_KERNEL))) 134 if (!(ctx = kzalloc(sizeof(*ctx), GFP_NOFS)))
135 goto out_err; 135 goto out_err;
136 136
137 p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate)); 137 p = simple_get_bytes(p, end, &ctx->initiate, sizeof(ctx->initiate));
diff --git a/net/sunrpc/auth_gss/gss_spkm3_mech.c b/net/sunrpc/auth_gss/gss_spkm3_mech.c
index 5deb4b6e4514..035e1dd6af1b 100644
--- a/net/sunrpc/auth_gss/gss_spkm3_mech.c
+++ b/net/sunrpc/auth_gss/gss_spkm3_mech.c
@@ -76,7 +76,7 @@ simple_get_netobj(const void *p, const void *end, struct xdr_netobj *res)
76 q = (const void *)((const char *)p + len); 76 q = (const void *)((const char *)p + len);
77 if (unlikely(q > end || q < p)) 77 if (unlikely(q > end || q < p))
78 return ERR_PTR(-EFAULT); 78 return ERR_PTR(-EFAULT);
79 res->data = kmemdup(p, len, GFP_KERNEL); 79 res->data = kmemdup(p, len, GFP_NOFS);
80 if (unlikely(res->data == NULL)) 80 if (unlikely(res->data == NULL))
81 return ERR_PTR(-ENOMEM); 81 return ERR_PTR(-ENOMEM);
82 return q; 82 return q;
@@ -90,7 +90,7 @@ gss_import_sec_context_spkm3(const void *p, size_t len,
90 struct spkm3_ctx *ctx; 90 struct spkm3_ctx *ctx;
91 int version; 91 int version;
92 92
93 if (!(ctx = kzalloc(sizeof(*ctx), GFP_KERNEL))) 93 if (!(ctx = kzalloc(sizeof(*ctx), GFP_NOFS)))
94 goto out_err; 94 goto out_err;
95 95
96 p = simple_get_bytes(p, end, &version, sizeof(version)); 96 p = simple_get_bytes(p, end, &version, sizeof(version));
diff --git a/net/sunrpc/auth_gss/gss_spkm3_token.c b/net/sunrpc/auth_gss/gss_spkm3_token.c
index 6cdd241ad267..3308157436d2 100644
--- a/net/sunrpc/auth_gss/gss_spkm3_token.c
+++ b/net/sunrpc/auth_gss/gss_spkm3_token.c
@@ -90,7 +90,7 @@ asn1_bitstring_len(struct xdr_netobj *in, int *enclen, int *zerobits)
90int 90int
91decode_asn1_bitstring(struct xdr_netobj *out, char *in, int enclen, int explen) 91decode_asn1_bitstring(struct xdr_netobj *out, char *in, int enclen, int explen)
92{ 92{
93 if (!(out->data = kzalloc(explen,GFP_KERNEL))) 93 if (!(out->data = kzalloc(explen,GFP_NOFS)))
94 return 0; 94 return 0;
95 out->len = explen; 95 out->len = explen;
96 memcpy(out->data, in, enclen); 96 memcpy(out->data, in, enclen);
diff --git a/net/sunrpc/auth_unix.c b/net/sunrpc/auth_unix.c
index 44920b90bdc4..46b2647c5bd2 100644
--- a/net/sunrpc/auth_unix.c
+++ b/net/sunrpc/auth_unix.c
@@ -66,7 +66,7 @@ unx_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
66 dprintk("RPC: allocating UNIX cred for uid %d gid %d\n", 66 dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
67 acred->uid, acred->gid); 67 acred->uid, acred->gid);
68 68
69 if (!(cred = kmalloc(sizeof(*cred), GFP_KERNEL))) 69 if (!(cred = kmalloc(sizeof(*cred), GFP_NOFS)))
70 return ERR_PTR(-ENOMEM); 70 return ERR_PTR(-ENOMEM);
71 71
72 rpcauth_init_cred(&cred->uc_base, acred, auth, &unix_credops); 72 rpcauth_init_cred(&cred->uc_base, acred, auth, &unix_credops);
diff --git a/net/sunrpc/clnt.c b/net/sunrpc/clnt.c
index 8945307556ec..76739e928d0d 100644
--- a/net/sunrpc/clnt.c
+++ b/net/sunrpc/clnt.c
@@ -25,6 +25,7 @@
25 25
26#include <linux/module.h> 26#include <linux/module.h>
27#include <linux/types.h> 27#include <linux/types.h>
28#include <linux/kallsyms.h>
28#include <linux/mm.h> 29#include <linux/mm.h>
29#include <linux/slab.h> 30#include <linux/slab.h>
30#include <linux/smp_lock.h> 31#include <linux/smp_lock.h>
@@ -58,7 +59,6 @@ static void call_start(struct rpc_task *task);
58static void call_reserve(struct rpc_task *task); 59static void call_reserve(struct rpc_task *task);
59static void call_reserveresult(struct rpc_task *task); 60static void call_reserveresult(struct rpc_task *task);
60static void call_allocate(struct rpc_task *task); 61static void call_allocate(struct rpc_task *task);
61static void call_encode(struct rpc_task *task);
62static void call_decode(struct rpc_task *task); 62static void call_decode(struct rpc_task *task);
63static void call_bind(struct rpc_task *task); 63static void call_bind(struct rpc_task *task);
64static void call_bind_status(struct rpc_task *task); 64static void call_bind_status(struct rpc_task *task);
@@ -70,9 +70,9 @@ static void call_refreshresult(struct rpc_task *task);
70static void call_timeout(struct rpc_task *task); 70static void call_timeout(struct rpc_task *task);
71static void call_connect(struct rpc_task *task); 71static void call_connect(struct rpc_task *task);
72static void call_connect_status(struct rpc_task *task); 72static void call_connect_status(struct rpc_task *task);
73static __be32 * call_header(struct rpc_task *task);
74static __be32 * call_verify(struct rpc_task *task);
75 73
74static __be32 *rpc_encode_header(struct rpc_task *task);
75static __be32 *rpc_verify_header(struct rpc_task *task);
76static int rpc_ping(struct rpc_clnt *clnt, int flags); 76static int rpc_ping(struct rpc_clnt *clnt, int flags);
77 77
78static void rpc_register_client(struct rpc_clnt *clnt) 78static void rpc_register_client(struct rpc_clnt *clnt)
@@ -324,6 +324,8 @@ struct rpc_clnt *rpc_create(struct rpc_create_args *args)
324 clnt->cl_autobind = 1; 324 clnt->cl_autobind = 1;
325 if (args->flags & RPC_CLNT_CREATE_DISCRTRY) 325 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
326 clnt->cl_discrtry = 1; 326 clnt->cl_discrtry = 1;
327 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
328 clnt->cl_chatty = 1;
327 329
328 return clnt; 330 return clnt;
329} 331}
@@ -690,6 +692,21 @@ rpc_restart_call(struct rpc_task *task)
690} 692}
691EXPORT_SYMBOL_GPL(rpc_restart_call); 693EXPORT_SYMBOL_GPL(rpc_restart_call);
692 694
695#ifdef RPC_DEBUG
696static const char *rpc_proc_name(const struct rpc_task *task)
697{
698 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
699
700 if (proc) {
701 if (proc->p_name)
702 return proc->p_name;
703 else
704 return "NULL";
705 } else
706 return "no proc";
707}
708#endif
709
693/* 710/*
694 * 0. Initial state 711 * 0. Initial state
695 * 712 *
@@ -701,9 +718,9 @@ call_start(struct rpc_task *task)
701{ 718{
702 struct rpc_clnt *clnt = task->tk_client; 719 struct rpc_clnt *clnt = task->tk_client;
703 720
704 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid, 721 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
705 clnt->cl_protname, clnt->cl_vers, 722 clnt->cl_protname, clnt->cl_vers,
706 task->tk_msg.rpc_proc->p_proc, 723 rpc_proc_name(task),
707 (RPC_IS_ASYNC(task) ? "async" : "sync")); 724 (RPC_IS_ASYNC(task) ? "async" : "sync"));
708 725
709 /* Increment call count */ 726 /* Increment call count */
@@ -861,7 +878,7 @@ rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
861 * 3. Encode arguments of an RPC call 878 * 3. Encode arguments of an RPC call
862 */ 879 */
863static void 880static void
864call_encode(struct rpc_task *task) 881rpc_xdr_encode(struct rpc_task *task)
865{ 882{
866 struct rpc_rqst *req = task->tk_rqstp; 883 struct rpc_rqst *req = task->tk_rqstp;
867 kxdrproc_t encode; 884 kxdrproc_t encode;
@@ -876,23 +893,19 @@ call_encode(struct rpc_task *task)
876 (char *)req->rq_buffer + req->rq_callsize, 893 (char *)req->rq_buffer + req->rq_callsize,
877 req->rq_rcvsize); 894 req->rq_rcvsize);
878 895
879 /* Encode header and provided arguments */ 896 p = rpc_encode_header(task);
880 encode = task->tk_msg.rpc_proc->p_encode; 897 if (p == NULL) {
881 if (!(p = call_header(task))) { 898 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
882 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
883 rpc_exit(task, -EIO); 899 rpc_exit(task, -EIO);
884 return; 900 return;
885 } 901 }
902
903 encode = task->tk_msg.rpc_proc->p_encode;
886 if (encode == NULL) 904 if (encode == NULL)
887 return; 905 return;
888 906
889 task->tk_status = rpcauth_wrap_req(task, encode, req, p, 907 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
890 task->tk_msg.rpc_argp); 908 task->tk_msg.rpc_argp);
891 if (task->tk_status == -ENOMEM) {
892 /* XXX: Is this sane? */
893 rpc_delay(task, 3*HZ);
894 task->tk_status = -EAGAIN;
895 }
896} 909}
897 910
898/* 911/*
@@ -929,11 +942,9 @@ call_bind_status(struct rpc_task *task)
929 } 942 }
930 943
931 switch (task->tk_status) { 944 switch (task->tk_status) {
932 case -EAGAIN: 945 case -ENOMEM:
933 dprintk("RPC: %5u rpcbind waiting for another request " 946 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
934 "to finish\n", task->tk_pid); 947 rpc_delay(task, HZ >> 2);
935 /* avoid busy-waiting here -- could be a network outage. */
936 rpc_delay(task, 5*HZ);
937 goto retry_timeout; 948 goto retry_timeout;
938 case -EACCES: 949 case -EACCES:
939 dprintk("RPC: %5u remote rpcbind: RPC program/version " 950 dprintk("RPC: %5u remote rpcbind: RPC program/version "
@@ -1046,10 +1057,16 @@ call_transmit(struct rpc_task *task)
1046 /* Encode here so that rpcsec_gss can use correct sequence number. */ 1057 /* Encode here so that rpcsec_gss can use correct sequence number. */
1047 if (rpc_task_need_encode(task)) { 1058 if (rpc_task_need_encode(task)) {
1048 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0); 1059 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
1049 call_encode(task); 1060 rpc_xdr_encode(task);
1050 /* Did the encode result in an error condition? */ 1061 /* Did the encode result in an error condition? */
1051 if (task->tk_status != 0) 1062 if (task->tk_status != 0) {
1063 /* Was the error nonfatal? */
1064 if (task->tk_status == -EAGAIN)
1065 rpc_delay(task, HZ >> 4);
1066 else
1067 rpc_exit(task, task->tk_status);
1052 return; 1068 return;
1069 }
1053 } 1070 }
1054 xprt_transmit(task); 1071 xprt_transmit(task);
1055 if (task->tk_status < 0) 1072 if (task->tk_status < 0)
@@ -1132,7 +1149,8 @@ call_status(struct rpc_task *task)
1132 rpc_exit(task, status); 1149 rpc_exit(task, status);
1133 break; 1150 break;
1134 default: 1151 default:
1135 printk("%s: RPC call returned error %d\n", 1152 if (clnt->cl_chatty)
1153 printk("%s: RPC call returned error %d\n",
1136 clnt->cl_protname, -status); 1154 clnt->cl_protname, -status);
1137 rpc_exit(task, status); 1155 rpc_exit(task, status);
1138 } 1156 }
@@ -1157,7 +1175,8 @@ call_timeout(struct rpc_task *task)
1157 task->tk_timeouts++; 1175 task->tk_timeouts++;
1158 1176
1159 if (RPC_IS_SOFT(task)) { 1177 if (RPC_IS_SOFT(task)) {
1160 printk(KERN_NOTICE "%s: server %s not responding, timed out\n", 1178 if (clnt->cl_chatty)
1179 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1161 clnt->cl_protname, clnt->cl_server); 1180 clnt->cl_protname, clnt->cl_server);
1162 rpc_exit(task, -EIO); 1181 rpc_exit(task, -EIO);
1163 return; 1182 return;
@@ -1165,7 +1184,8 @@ call_timeout(struct rpc_task *task)
1165 1184
1166 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) { 1185 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1167 task->tk_flags |= RPC_CALL_MAJORSEEN; 1186 task->tk_flags |= RPC_CALL_MAJORSEEN;
1168 printk(KERN_NOTICE "%s: server %s not responding, still trying\n", 1187 if (clnt->cl_chatty)
1188 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1169 clnt->cl_protname, clnt->cl_server); 1189 clnt->cl_protname, clnt->cl_server);
1170 } 1190 }
1171 rpc_force_rebind(clnt); 1191 rpc_force_rebind(clnt);
@@ -1196,8 +1216,9 @@ call_decode(struct rpc_task *task)
1196 task->tk_pid, task->tk_status); 1216 task->tk_pid, task->tk_status);
1197 1217
1198 if (task->tk_flags & RPC_CALL_MAJORSEEN) { 1218 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1199 printk(KERN_NOTICE "%s: server %s OK\n", 1219 if (clnt->cl_chatty)
1200 clnt->cl_protname, clnt->cl_server); 1220 printk(KERN_NOTICE "%s: server %s OK\n",
1221 clnt->cl_protname, clnt->cl_server);
1201 task->tk_flags &= ~RPC_CALL_MAJORSEEN; 1222 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1202 } 1223 }
1203 1224
@@ -1224,8 +1245,7 @@ call_decode(struct rpc_task *task)
1224 goto out_retry; 1245 goto out_retry;
1225 } 1246 }
1226 1247
1227 /* Verify the RPC header */ 1248 p = rpc_verify_header(task);
1228 p = call_verify(task);
1229 if (IS_ERR(p)) { 1249 if (IS_ERR(p)) {
1230 if (p == ERR_PTR(-EAGAIN)) 1250 if (p == ERR_PTR(-EAGAIN))
1231 goto out_retry; 1251 goto out_retry;
@@ -1243,7 +1263,7 @@ call_decode(struct rpc_task *task)
1243 return; 1263 return;
1244out_retry: 1264out_retry:
1245 task->tk_status = 0; 1265 task->tk_status = 0;
1246 /* Note: call_verify() may have freed the RPC slot */ 1266 /* Note: rpc_verify_header() may have freed the RPC slot */
1247 if (task->tk_rqstp == req) { 1267 if (task->tk_rqstp == req) {
1248 req->rq_received = req->rq_rcv_buf.len = 0; 1268 req->rq_received = req->rq_rcv_buf.len = 0;
1249 if (task->tk_client->cl_discrtry) 1269 if (task->tk_client->cl_discrtry)
@@ -1290,11 +1310,8 @@ call_refreshresult(struct rpc_task *task)
1290 return; 1310 return;
1291} 1311}
1292 1312
1293/*
1294 * Call header serialization
1295 */
1296static __be32 * 1313static __be32 *
1297call_header(struct rpc_task *task) 1314rpc_encode_header(struct rpc_task *task)
1298{ 1315{
1299 struct rpc_clnt *clnt = task->tk_client; 1316 struct rpc_clnt *clnt = task->tk_client;
1300 struct rpc_rqst *req = task->tk_rqstp; 1317 struct rpc_rqst *req = task->tk_rqstp;
@@ -1314,11 +1331,8 @@ call_header(struct rpc_task *task)
1314 return p; 1331 return p;
1315} 1332}
1316 1333
1317/*
1318 * Reply header verification
1319 */
1320static __be32 * 1334static __be32 *
1321call_verify(struct rpc_task *task) 1335rpc_verify_header(struct rpc_task *task)
1322{ 1336{
1323 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0]; 1337 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1324 int len = task->tk_rqstp->rq_rcv_buf.len >> 2; 1338 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
@@ -1392,7 +1406,7 @@ call_verify(struct rpc_task *task)
1392 task->tk_action = call_bind; 1406 task->tk_action = call_bind;
1393 goto out_retry; 1407 goto out_retry;
1394 case RPC_AUTH_TOOWEAK: 1408 case RPC_AUTH_TOOWEAK:
1395 printk(KERN_NOTICE "call_verify: server %s requires stronger " 1409 printk(KERN_NOTICE "RPC: server %s requires stronger "
1396 "authentication.\n", task->tk_client->cl_server); 1410 "authentication.\n", task->tk_client->cl_server);
1397 break; 1411 break;
1398 default: 1412 default:
@@ -1431,10 +1445,10 @@ call_verify(struct rpc_task *task)
1431 error = -EPROTONOSUPPORT; 1445 error = -EPROTONOSUPPORT;
1432 goto out_err; 1446 goto out_err;
1433 case RPC_PROC_UNAVAIL: 1447 case RPC_PROC_UNAVAIL:
1434 dprintk("RPC: %5u %s: proc %p unsupported by program %u, " 1448 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
1435 "version %u on server %s\n", 1449 "version %u on server %s\n",
1436 task->tk_pid, __func__, 1450 task->tk_pid, __func__,
1437 task->tk_msg.rpc_proc, 1451 rpc_proc_name(task),
1438 task->tk_client->cl_prog, 1452 task->tk_client->cl_prog,
1439 task->tk_client->cl_vers, 1453 task->tk_client->cl_vers,
1440 task->tk_client->cl_server); 1454 task->tk_client->cl_server);
@@ -1517,44 +1531,53 @@ struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int
1517EXPORT_SYMBOL_GPL(rpc_call_null); 1531EXPORT_SYMBOL_GPL(rpc_call_null);
1518 1532
1519#ifdef RPC_DEBUG 1533#ifdef RPC_DEBUG
1534static void rpc_show_header(void)
1535{
1536 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
1537 "-timeout ---ops--\n");
1538}
1539
1540static void rpc_show_task(const struct rpc_clnt *clnt,
1541 const struct rpc_task *task)
1542{
1543 const char *rpc_waitq = "none";
1544 char *p, action[KSYM_SYMBOL_LEN];
1545
1546 if (RPC_IS_QUEUED(task))
1547 rpc_waitq = rpc_qname(task->tk_waitqueue);
1548
1549 /* map tk_action pointer to a function name; then trim off
1550 * the "+0x0 [sunrpc]" */
1551 sprint_symbol(action, (unsigned long)task->tk_action);
1552 p = strchr(action, '+');
1553 if (p)
1554 *p = '\0';
1555
1556 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%s q:%s\n",
1557 task->tk_pid, task->tk_flags, task->tk_status,
1558 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
1559 clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
1560 action, rpc_waitq);
1561}
1562
1520void rpc_show_tasks(void) 1563void rpc_show_tasks(void)
1521{ 1564{
1522 struct rpc_clnt *clnt; 1565 struct rpc_clnt *clnt;
1523 struct rpc_task *t; 1566 struct rpc_task *task;
1567 int header = 0;
1524 1568
1525 spin_lock(&rpc_client_lock); 1569 spin_lock(&rpc_client_lock);
1526 if (list_empty(&all_clients))
1527 goto out;
1528 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1529 "-rpcwait -action- ---ops--\n");
1530 list_for_each_entry(clnt, &all_clients, cl_clients) { 1570 list_for_each_entry(clnt, &all_clients, cl_clients) {
1531 if (list_empty(&clnt->cl_tasks))
1532 continue;
1533 spin_lock(&clnt->cl_lock); 1571 spin_lock(&clnt->cl_lock);
1534 list_for_each_entry(t, &clnt->cl_tasks, tk_task) { 1572 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
1535 const char *rpc_waitq = "none"; 1573 if (!header) {
1536 int proc; 1574 rpc_show_header();
1537 1575 header++;
1538 if (t->tk_msg.rpc_proc) 1576 }
1539 proc = t->tk_msg.rpc_proc->p_proc; 1577 rpc_show_task(clnt, task);
1540 else
1541 proc = -1;
1542
1543 if (RPC_IS_QUEUED(t))
1544 rpc_waitq = rpc_qname(t->tk_waitqueue);
1545
1546 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1547 t->tk_pid, proc,
1548 t->tk_flags, t->tk_status,
1549 t->tk_client,
1550 (t->tk_client ? t->tk_client->cl_prog : 0),
1551 t->tk_rqstp, t->tk_timeout,
1552 rpc_waitq,
1553 t->tk_action, t->tk_ops);
1554 } 1578 }
1555 spin_unlock(&clnt->cl_lock); 1579 spin_unlock(&clnt->cl_lock);
1556 } 1580 }
1557out:
1558 spin_unlock(&rpc_client_lock); 1581 spin_unlock(&rpc_client_lock);
1559} 1582}
1560#endif 1583#endif
diff --git a/net/sunrpc/rpcb_clnt.c b/net/sunrpc/rpcb_clnt.c
index e6fb21b19b86..24db2b4d12d3 100644
--- a/net/sunrpc/rpcb_clnt.c
+++ b/net/sunrpc/rpcb_clnt.c
@@ -32,6 +32,10 @@
32#define RPCBIND_PROGRAM (100000u) 32#define RPCBIND_PROGRAM (100000u)
33#define RPCBIND_PORT (111u) 33#define RPCBIND_PORT (111u)
34 34
35#define RPCBVERS_2 (2u)
36#define RPCBVERS_3 (3u)
37#define RPCBVERS_4 (4u)
38
35enum { 39enum {
36 RPCBPROC_NULL, 40 RPCBPROC_NULL,
37 RPCBPROC_SET, 41 RPCBPROC_SET,
@@ -64,6 +68,7 @@ enum {
64#define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING) 68#define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
65 69
66static void rpcb_getport_done(struct rpc_task *, void *); 70static void rpcb_getport_done(struct rpc_task *, void *);
71static void rpcb_map_release(void *data);
67static struct rpc_program rpcb_program; 72static struct rpc_program rpcb_program;
68 73
69struct rpcbind_args { 74struct rpcbind_args {
@@ -76,41 +81,73 @@ struct rpcbind_args {
76 const char * r_netid; 81 const char * r_netid;
77 const char * r_addr; 82 const char * r_addr;
78 const char * r_owner; 83 const char * r_owner;
84
85 int r_status;
79}; 86};
80 87
81static struct rpc_procinfo rpcb_procedures2[]; 88static struct rpc_procinfo rpcb_procedures2[];
82static struct rpc_procinfo rpcb_procedures3[]; 89static struct rpc_procinfo rpcb_procedures3[];
90static struct rpc_procinfo rpcb_procedures4[];
83 91
84struct rpcb_info { 92struct rpcb_info {
85 int rpc_vers; 93 u32 rpc_vers;
86 struct rpc_procinfo * rpc_proc; 94 struct rpc_procinfo * rpc_proc;
87}; 95};
88 96
89static struct rpcb_info rpcb_next_version[]; 97static struct rpcb_info rpcb_next_version[];
90static struct rpcb_info rpcb_next_version6[]; 98static struct rpcb_info rpcb_next_version6[];
91 99
100static const struct rpc_call_ops rpcb_getport_ops = {
101 .rpc_call_done = rpcb_getport_done,
102 .rpc_release = rpcb_map_release,
103};
104
105static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
106{
107 xprt_clear_binding(xprt);
108 rpc_wake_up_status(&xprt->binding, status);
109}
110
92static void rpcb_map_release(void *data) 111static void rpcb_map_release(void *data)
93{ 112{
94 struct rpcbind_args *map = data; 113 struct rpcbind_args *map = data;
95 114
115 rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
96 xprt_put(map->r_xprt); 116 xprt_put(map->r_xprt);
97 kfree(map); 117 kfree(map);
98} 118}
99 119
100static const struct rpc_call_ops rpcb_getport_ops = { 120static const struct sockaddr_in rpcb_inaddr_loopback = {
101 .rpc_call_done = rpcb_getport_done, 121 .sin_family = AF_INET,
102 .rpc_release = rpcb_map_release, 122 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
123 .sin_port = htons(RPCBIND_PORT),
103}; 124};
104 125
105static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status) 126static const struct sockaddr_in6 rpcb_in6addr_loopback = {
127 .sin6_family = AF_INET6,
128 .sin6_addr = IN6ADDR_LOOPBACK_INIT,
129 .sin6_port = htons(RPCBIND_PORT),
130};
131
132static struct rpc_clnt *rpcb_create_local(struct sockaddr *addr,
133 size_t addrlen, u32 version)
106{ 134{
107 xprt_clear_binding(xprt); 135 struct rpc_create_args args = {
108 rpc_wake_up_status(&xprt->binding, status); 136 .protocol = XPRT_TRANSPORT_UDP,
137 .address = addr,
138 .addrsize = addrlen,
139 .servername = "localhost",
140 .program = &rpcb_program,
141 .version = version,
142 .authflavor = RPC_AUTH_UNIX,
143 .flags = RPC_CLNT_CREATE_NOPING,
144 };
145
146 return rpc_create(&args);
109} 147}
110 148
111static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr, 149static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
112 size_t salen, int proto, u32 version, 150 size_t salen, int proto, u32 version)
113 int privileged)
114{ 151{
115 struct rpc_create_args args = { 152 struct rpc_create_args args = {
116 .protocol = proto, 153 .protocol = proto,
@@ -120,7 +157,8 @@ static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
120 .program = &rpcb_program, 157 .program = &rpcb_program,
121 .version = version, 158 .version = version,
122 .authflavor = RPC_AUTH_UNIX, 159 .authflavor = RPC_AUTH_UNIX,
123 .flags = RPC_CLNT_CREATE_NOPING, 160 .flags = (RPC_CLNT_CREATE_NOPING |
161 RPC_CLNT_CREATE_NONPRIVPORT),
124 }; 162 };
125 163
126 switch (srvaddr->sa_family) { 164 switch (srvaddr->sa_family) {
@@ -134,29 +172,72 @@ static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
134 return NULL; 172 return NULL;
135 } 173 }
136 174
137 if (!privileged)
138 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
139 return rpc_create(&args); 175 return rpc_create(&args);
140} 176}
141 177
178static int rpcb_register_call(struct sockaddr *addr, size_t addrlen,
179 u32 version, struct rpc_message *msg,
180 int *result)
181{
182 struct rpc_clnt *rpcb_clnt;
183 int error = 0;
184
185 *result = 0;
186
187 rpcb_clnt = rpcb_create_local(addr, addrlen, version);
188 if (!IS_ERR(rpcb_clnt)) {
189 error = rpc_call_sync(rpcb_clnt, msg, 0);
190 rpc_shutdown_client(rpcb_clnt);
191 } else
192 error = PTR_ERR(rpcb_clnt);
193
194 if (error < 0)
195 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
196 "server (errno %d).\n", -error);
197 dprintk("RPC: registration status %d/%d\n", error, *result);
198
199 return error;
200}
201
142/** 202/**
143 * rpcb_register - set or unset a port registration with the local rpcbind svc 203 * rpcb_register - set or unset a port registration with the local rpcbind svc
144 * @prog: RPC program number to bind 204 * @prog: RPC program number to bind
145 * @vers: RPC version number to bind 205 * @vers: RPC version number to bind
146 * @prot: transport protocol to use to make this request 206 * @prot: transport protocol to register
147 * @port: port value to register 207 * @port: port value to register
148 * @okay: result code 208 * @okay: OUT: result code
209 *
210 * RPC services invoke this function to advertise their contact
211 * information via the system's rpcbind daemon. RPC services
212 * invoke this function once for each [program, version, transport]
213 * tuple they wish to advertise.
214 *
215 * Callers may also unregister RPC services that are no longer
216 * available by setting the passed-in port to zero. This removes
217 * all registered transports for [program, version] from the local
218 * rpcbind database.
219 *
220 * Returns zero if the registration request was dispatched
221 * successfully and a reply was received. The rpcbind daemon's
222 * boolean result code is stored in *okay.
223 *
224 * Returns an errno value and sets *result to zero if there was
225 * some problem that prevented the rpcbind request from being
226 * dispatched, or if the rpcbind daemon did not respond within
227 * the timeout.
149 * 228 *
150 * port == 0 means unregister, port != 0 means register. 229 * This function uses rpcbind protocol version 2 to contact the
230 * local rpcbind daemon.
151 * 231 *
152 * This routine supports only rpcbind version 2. 232 * Registration works over both AF_INET and AF_INET6, and services
233 * registered via this function are advertised as available for any
234 * address. If the local rpcbind daemon is listening on AF_INET6,
235 * services registered via this function will be advertised on
236 * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
237 * addresses).
153 */ 238 */
154int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay) 239int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
155{ 240{
156 struct sockaddr_in sin = {
157 .sin_family = AF_INET,
158 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
159 };
160 struct rpcbind_args map = { 241 struct rpcbind_args map = {
161 .r_prog = prog, 242 .r_prog = prog,
162 .r_vers = vers, 243 .r_vers = vers,
@@ -164,32 +245,159 @@ int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
164 .r_port = port, 245 .r_port = port,
165 }; 246 };
166 struct rpc_message msg = { 247 struct rpc_message msg = {
167 .rpc_proc = &rpcb_procedures2[port ?
168 RPCBPROC_SET : RPCBPROC_UNSET],
169 .rpc_argp = &map, 248 .rpc_argp = &map,
170 .rpc_resp = okay, 249 .rpc_resp = okay,
171 }; 250 };
172 struct rpc_clnt *rpcb_clnt;
173 int error = 0;
174 251
175 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local " 252 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
176 "rpcbind\n", (port ? "" : "un"), 253 "rpcbind\n", (port ? "" : "un"),
177 prog, vers, prot, port); 254 prog, vers, prot, port);
178 255
179 rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin, 256 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
180 sizeof(sin), XPRT_TRANSPORT_UDP, 2, 1); 257 if (port)
181 if (IS_ERR(rpcb_clnt)) 258 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
182 return PTR_ERR(rpcb_clnt);
183 259
184 error = rpc_call_sync(rpcb_clnt, &msg, 0); 260 return rpcb_register_call((struct sockaddr *)&rpcb_inaddr_loopback,
261 sizeof(rpcb_inaddr_loopback),
262 RPCBVERS_2, &msg, okay);
263}
185 264
186 rpc_shutdown_client(rpcb_clnt); 265/*
187 if (error < 0) 266 * Fill in AF_INET family-specific arguments to register
188 printk(KERN_WARNING "RPC: failed to contact local rpcbind " 267 */
189 "server (errno %d).\n", -error); 268static int rpcb_register_netid4(struct sockaddr_in *address_to_register,
190 dprintk("RPC: registration status %d/%d\n", error, *okay); 269 struct rpc_message *msg)
270{
271 struct rpcbind_args *map = msg->rpc_argp;
272 unsigned short port = ntohs(address_to_register->sin_port);
273 char buf[32];
274
275 /* Construct AF_INET universal address */
276 snprintf(buf, sizeof(buf),
277 NIPQUAD_FMT".%u.%u",
278 NIPQUAD(address_to_register->sin_addr.s_addr),
279 port >> 8, port & 0xff);
280 map->r_addr = buf;
281
282 dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
283 "local rpcbind\n", (port ? "" : "un"),
284 map->r_prog, map->r_vers,
285 map->r_addr, map->r_netid);
286
287 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
288 if (port)
289 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
290
291 return rpcb_register_call((struct sockaddr *)&rpcb_inaddr_loopback,
292 sizeof(rpcb_inaddr_loopback),
293 RPCBVERS_4, msg, msg->rpc_resp);
294}
191 295
192 return error; 296/*
297 * Fill in AF_INET6 family-specific arguments to register
298 */
299static int rpcb_register_netid6(struct sockaddr_in6 *address_to_register,
300 struct rpc_message *msg)
301{
302 struct rpcbind_args *map = msg->rpc_argp;
303 unsigned short port = ntohs(address_to_register->sin6_port);
304 char buf[64];
305
306 /* Construct AF_INET6 universal address */
307 snprintf(buf, sizeof(buf),
308 NIP6_FMT".%u.%u",
309 NIP6(address_to_register->sin6_addr),
310 port >> 8, port & 0xff);
311 map->r_addr = buf;
312
313 dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
314 "local rpcbind\n", (port ? "" : "un"),
315 map->r_prog, map->r_vers,
316 map->r_addr, map->r_netid);
317
318 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
319 if (port)
320 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
321
322 return rpcb_register_call((struct sockaddr *)&rpcb_in6addr_loopback,
323 sizeof(rpcb_in6addr_loopback),
324 RPCBVERS_4, msg, msg->rpc_resp);
325}
326
327/**
328 * rpcb_v4_register - set or unset a port registration with the local rpcbind
329 * @program: RPC program number of service to (un)register
330 * @version: RPC version number of service to (un)register
331 * @address: address family, IP address, and port to (un)register
332 * @netid: netid of transport protocol to (un)register
333 * @result: result code from rpcbind RPC call
334 *
335 * RPC services invoke this function to advertise their contact
336 * information via the system's rpcbind daemon. RPC services
337 * invoke this function once for each [program, version, address,
338 * netid] tuple they wish to advertise.
339 *
340 * Callers may also unregister RPC services that are no longer
341 * available by setting the port number in the passed-in address
342 * to zero. Callers pass a netid of "" to unregister all
343 * transport netids associated with [program, version, address].
344 *
345 * Returns zero if the registration request was dispatched
346 * successfully and a reply was received. The rpcbind daemon's
347 * result code is stored in *result.
348 *
349 * Returns an errno value and sets *result to zero if there was
350 * some problem that prevented the rpcbind request from being
351 * dispatched, or if the rpcbind daemon did not respond within
352 * the timeout.
353 *
354 * This function uses rpcbind protocol version 4 to contact the
355 * local rpcbind daemon. The local rpcbind daemon must support
356 * version 4 of the rpcbind protocol in order for these functions
357 * to register a service successfully.
358 *
359 * Supported netids include "udp" and "tcp" for UDP and TCP over
360 * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
361 * respectively.
362 *
363 * The contents of @address determine the address family and the
364 * port to be registered. The usual practice is to pass INADDR_ANY
365 * as the raw address, but specifying a non-zero address is also
366 * supported by this API if the caller wishes to advertise an RPC
367 * service on a specific network interface.
368 *
369 * Note that passing in INADDR_ANY does not create the same service
370 * registration as IN6ADDR_ANY. The former advertises an RPC
371 * service on any IPv4 address, but not on IPv6. The latter
372 * advertises the service on all IPv4 and IPv6 addresses.
373 */
374int rpcb_v4_register(const u32 program, const u32 version,
375 const struct sockaddr *address, const char *netid,
376 int *result)
377{
378 struct rpcbind_args map = {
379 .r_prog = program,
380 .r_vers = version,
381 .r_netid = netid,
382 .r_owner = RPCB_OWNER_STRING,
383 };
384 struct rpc_message msg = {
385 .rpc_argp = &map,
386 .rpc_resp = result,
387 };
388
389 *result = 0;
390
391 switch (address->sa_family) {
392 case AF_INET:
393 return rpcb_register_netid4((struct sockaddr_in *)address,
394 &msg);
395 case AF_INET6:
396 return rpcb_register_netid6((struct sockaddr_in6 *)address,
397 &msg);
398 }
399
400 return -EAFNOSUPPORT;
193} 401}
194 402
195/** 403/**
@@ -227,7 +435,7 @@ int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
227 __func__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot); 435 __func__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
228 436
229 rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin, 437 rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
230 sizeof(*sin), prot, 2, 0); 438 sizeof(*sin), prot, RPCBVERS_2);
231 if (IS_ERR(rpcb_clnt)) 439 if (IS_ERR(rpcb_clnt))
232 return PTR_ERR(rpcb_clnt); 440 return PTR_ERR(rpcb_clnt);
233 441
@@ -289,17 +497,16 @@ void rpcb_getport_async(struct rpc_task *task)
289 /* Autobind on cloned rpc clients is discouraged */ 497 /* Autobind on cloned rpc clients is discouraged */
290 BUG_ON(clnt->cl_parent != clnt); 498 BUG_ON(clnt->cl_parent != clnt);
291 499
500 /* Put self on the wait queue to ensure we get notified if
501 * some other task is already attempting to bind the port */
502 rpc_sleep_on(&xprt->binding, task, NULL);
503
292 if (xprt_test_and_set_binding(xprt)) { 504 if (xprt_test_and_set_binding(xprt)) {
293 status = -EAGAIN; /* tell caller to check again */
294 dprintk("RPC: %5u %s: waiting for another binder\n", 505 dprintk("RPC: %5u %s: waiting for another binder\n",
295 task->tk_pid, __func__); 506 task->tk_pid, __func__);
296 goto bailout_nowake; 507 return;
297 } 508 }
298 509
299 /* Put self on queue before sending rpcbind request, in case
300 * rpcb_getport_done completes before we return from rpc_run_task */
301 rpc_sleep_on(&xprt->binding, task, NULL);
302
303 /* Someone else may have bound if we slept */ 510 /* Someone else may have bound if we slept */
304 if (xprt_bound(xprt)) { 511 if (xprt_bound(xprt)) {
305 status = 0; 512 status = 0;
@@ -338,7 +545,7 @@ void rpcb_getport_async(struct rpc_task *task)
338 task->tk_pid, __func__, bind_version); 545 task->tk_pid, __func__, bind_version);
339 546
340 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot, 547 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
341 bind_version, 0); 548 bind_version);
342 if (IS_ERR(rpcb_clnt)) { 549 if (IS_ERR(rpcb_clnt)) {
343 status = PTR_ERR(rpcb_clnt); 550 status = PTR_ERR(rpcb_clnt);
344 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n", 551 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
@@ -361,15 +568,15 @@ void rpcb_getport_async(struct rpc_task *task)
361 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID); 568 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
362 map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR); 569 map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR);
363 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */ 570 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
571 map->r_status = -EIO;
364 572
365 child = rpcb_call_async(rpcb_clnt, map, proc); 573 child = rpcb_call_async(rpcb_clnt, map, proc);
366 rpc_release_client(rpcb_clnt); 574 rpc_release_client(rpcb_clnt);
367 if (IS_ERR(child)) { 575 if (IS_ERR(child)) {
368 status = -EIO;
369 /* rpcb_map_release() has freed the arguments */ 576 /* rpcb_map_release() has freed the arguments */
370 dprintk("RPC: %5u %s: rpc_run_task failed\n", 577 dprintk("RPC: %5u %s: rpc_run_task failed\n",
371 task->tk_pid, __func__); 578 task->tk_pid, __func__);
372 goto bailout_nofree; 579 return;
373 } 580 }
374 rpc_put_task(child); 581 rpc_put_task(child);
375 582
@@ -378,7 +585,6 @@ void rpcb_getport_async(struct rpc_task *task)
378 585
379bailout_nofree: 586bailout_nofree:
380 rpcb_wake_rpcbind_waiters(xprt, status); 587 rpcb_wake_rpcbind_waiters(xprt, status);
381bailout_nowake:
382 task->tk_status = status; 588 task->tk_status = status;
383} 589}
384EXPORT_SYMBOL_GPL(rpcb_getport_async); 590EXPORT_SYMBOL_GPL(rpcb_getport_async);
@@ -417,9 +623,13 @@ static void rpcb_getport_done(struct rpc_task *child, void *data)
417 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n", 623 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
418 child->tk_pid, status, map->r_port); 624 child->tk_pid, status, map->r_port);
419 625
420 rpcb_wake_rpcbind_waiters(xprt, status); 626 map->r_status = status;
421} 627}
422 628
629/*
630 * XDR functions for rpcbind
631 */
632
423static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p, 633static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
424 struct rpcbind_args *rpcb) 634 struct rpcbind_args *rpcb)
425{ 635{
@@ -438,7 +648,7 @@ static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
438 unsigned short *portp) 648 unsigned short *portp)
439{ 649{
440 *portp = (unsigned short) ntohl(*p++); 650 *portp = (unsigned short) ntohl(*p++);
441 dprintk("RPC: rpcb_decode_getport result %u\n", 651 dprintk("RPC: rpcb_decode_getport result %u\n",
442 *portp); 652 *portp);
443 return 0; 653 return 0;
444} 654}
@@ -447,8 +657,8 @@ static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
447 unsigned int *boolp) 657 unsigned int *boolp)
448{ 658{
449 *boolp = (unsigned int) ntohl(*p++); 659 *boolp = (unsigned int) ntohl(*p++);
450 dprintk("RPC: rpcb_decode_set result %u\n", 660 dprintk("RPC: rpcb_decode_set: call %s\n",
451 *boolp); 661 (*boolp ? "succeeded" : "failed"));
452 return 0; 662 return 0;
453} 663}
454 664
@@ -571,52 +781,60 @@ out_err:
571static struct rpc_procinfo rpcb_procedures2[] = { 781static struct rpc_procinfo rpcb_procedures2[] = {
572 PROC(SET, mapping, set), 782 PROC(SET, mapping, set),
573 PROC(UNSET, mapping, set), 783 PROC(UNSET, mapping, set),
574 PROC(GETADDR, mapping, getport), 784 PROC(GETPORT, mapping, getport),
575}; 785};
576 786
577static struct rpc_procinfo rpcb_procedures3[] = { 787static struct rpc_procinfo rpcb_procedures3[] = {
578 PROC(SET, mapping, set), 788 PROC(SET, getaddr, set),
579 PROC(UNSET, mapping, set), 789 PROC(UNSET, getaddr, set),
580 PROC(GETADDR, getaddr, getaddr), 790 PROC(GETADDR, getaddr, getaddr),
581}; 791};
582 792
583static struct rpc_procinfo rpcb_procedures4[] = { 793static struct rpc_procinfo rpcb_procedures4[] = {
584 PROC(SET, mapping, set), 794 PROC(SET, getaddr, set),
585 PROC(UNSET, mapping, set), 795 PROC(UNSET, getaddr, set),
796 PROC(GETADDR, getaddr, getaddr),
586 PROC(GETVERSADDR, getaddr, getaddr), 797 PROC(GETVERSADDR, getaddr, getaddr),
587}; 798};
588 799
589static struct rpcb_info rpcb_next_version[] = { 800static struct rpcb_info rpcb_next_version[] = {
590#ifdef CONFIG_SUNRPC_BIND34 801 {
591 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] }, 802 .rpc_vers = RPCBVERS_2,
592 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] }, 803 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
593#endif 804 },
594 { 2, &rpcb_procedures2[RPCBPROC_GETPORT] }, 805 {
595 { 0, NULL }, 806 .rpc_proc = NULL,
807 },
596}; 808};
597 809
598static struct rpcb_info rpcb_next_version6[] = { 810static struct rpcb_info rpcb_next_version6[] = {
599#ifdef CONFIG_SUNRPC_BIND34 811 {
600 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] }, 812 .rpc_vers = RPCBVERS_4,
601 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] }, 813 .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
602#endif 814 },
603 { 0, NULL }, 815 {
816 .rpc_vers = RPCBVERS_3,
817 .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
818 },
819 {
820 .rpc_proc = NULL,
821 },
604}; 822};
605 823
606static struct rpc_version rpcb_version2 = { 824static struct rpc_version rpcb_version2 = {
607 .number = 2, 825 .number = RPCBVERS_2,
608 .nrprocs = RPCB_HIGHPROC_2, 826 .nrprocs = RPCB_HIGHPROC_2,
609 .procs = rpcb_procedures2 827 .procs = rpcb_procedures2
610}; 828};
611 829
612static struct rpc_version rpcb_version3 = { 830static struct rpc_version rpcb_version3 = {
613 .number = 3, 831 .number = RPCBVERS_3,
614 .nrprocs = RPCB_HIGHPROC_3, 832 .nrprocs = RPCB_HIGHPROC_3,
615 .procs = rpcb_procedures3 833 .procs = rpcb_procedures3
616}; 834};
617 835
618static struct rpc_version rpcb_version4 = { 836static struct rpc_version rpcb_version4 = {
619 .number = 4, 837 .number = RPCBVERS_4,
620 .nrprocs = RPCB_HIGHPROC_4, 838 .nrprocs = RPCB_HIGHPROC_4,
621 .procs = rpcb_procedures4 839 .procs = rpcb_procedures4
622}; 840};
diff --git a/net/sunrpc/sched.c b/net/sunrpc/sched.c
index 6eab9bf94baf..385f427bedad 100644
--- a/net/sunrpc/sched.c
+++ b/net/sunrpc/sched.c
@@ -576,9 +576,7 @@ EXPORT_SYMBOL_GPL(rpc_delay);
576 */ 576 */
577static void rpc_prepare_task(struct rpc_task *task) 577static void rpc_prepare_task(struct rpc_task *task)
578{ 578{
579 lock_kernel();
580 task->tk_ops->rpc_call_prepare(task, task->tk_calldata); 579 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
581 unlock_kernel();
582} 580}
583 581
584/* 582/*
@@ -588,9 +586,7 @@ void rpc_exit_task(struct rpc_task *task)
588{ 586{
589 task->tk_action = NULL; 587 task->tk_action = NULL;
590 if (task->tk_ops->rpc_call_done != NULL) { 588 if (task->tk_ops->rpc_call_done != NULL) {
591 lock_kernel();
592 task->tk_ops->rpc_call_done(task, task->tk_calldata); 589 task->tk_ops->rpc_call_done(task, task->tk_calldata);
593 unlock_kernel();
594 if (task->tk_action != NULL) { 590 if (task->tk_action != NULL) {
595 WARN_ON(RPC_ASSASSINATED(task)); 591 WARN_ON(RPC_ASSASSINATED(task));
596 /* Always release the RPC slot and buffer memory */ 592 /* Always release the RPC slot and buffer memory */
@@ -602,11 +598,8 @@ EXPORT_SYMBOL_GPL(rpc_exit_task);
602 598
603void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata) 599void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
604{ 600{
605 if (ops->rpc_release != NULL) { 601 if (ops->rpc_release != NULL)
606 lock_kernel();
607 ops->rpc_release(calldata); 602 ops->rpc_release(calldata);
608 unlock_kernel();
609 }
610} 603}
611 604
612/* 605/*
@@ -626,19 +619,15 @@ static void __rpc_execute(struct rpc_task *task)
626 /* 619 /*
627 * Execute any pending callback. 620 * Execute any pending callback.
628 */ 621 */
629 if (RPC_DO_CALLBACK(task)) { 622 if (task->tk_callback) {
630 /* Define a callback save pointer */
631 void (*save_callback)(struct rpc_task *); 623 void (*save_callback)(struct rpc_task *);
632 624
633 /* 625 /*
634 * If a callback exists, save it, reset it, 626 * We set tk_callback to NULL before calling it,
635 * call it. 627 * in case it sets the tk_callback field itself:
636 * The save is needed to stop from resetting
637 * another callback set within the callback handler
638 * - Dave
639 */ 628 */
640 save_callback=task->tk_callback; 629 save_callback = task->tk_callback;
641 task->tk_callback=NULL; 630 task->tk_callback = NULL;
642 save_callback(task); 631 save_callback(task);
643 } 632 }
644 633
diff --git a/net/sunrpc/xprt.c b/net/sunrpc/xprt.c
index e1770f7ba0b3..99a52aabe332 100644
--- a/net/sunrpc/xprt.c
+++ b/net/sunrpc/xprt.c
@@ -690,7 +690,7 @@ static void xprt_connect_status(struct rpc_task *task)
690{ 690{
691 struct rpc_xprt *xprt = task->tk_xprt; 691 struct rpc_xprt *xprt = task->tk_xprt;
692 692
693 if (task->tk_status >= 0) { 693 if (task->tk_status == 0) {
694 xprt->stat.connect_count++; 694 xprt->stat.connect_count++;
695 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; 695 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
696 dprintk("RPC: %5u xprt_connect_status: connection established\n", 696 dprintk("RPC: %5u xprt_connect_status: connection established\n",
@@ -699,12 +699,6 @@ static void xprt_connect_status(struct rpc_task *task)
699 } 699 }
700 700
701 switch (task->tk_status) { 701 switch (task->tk_status) {
702 case -ECONNREFUSED:
703 case -ECONNRESET:
704 dprintk("RPC: %5u xprt_connect_status: server %s refused "
705 "connection\n", task->tk_pid,
706 task->tk_client->cl_server);
707 break;
708 case -ENOTCONN: 702 case -ENOTCONN:
709 dprintk("RPC: %5u xprt_connect_status: connection broken\n", 703 dprintk("RPC: %5u xprt_connect_status: connection broken\n",
710 task->tk_pid); 704 task->tk_pid);
@@ -878,6 +872,7 @@ void xprt_transmit(struct rpc_task *task)
878 return; 872 return;
879 873
880 req->rq_connect_cookie = xprt->connect_cookie; 874 req->rq_connect_cookie = xprt->connect_cookie;
875 req->rq_xtime = jiffies;
881 status = xprt->ops->send_request(task); 876 status = xprt->ops->send_request(task);
882 if (status == 0) { 877 if (status == 0) {
883 dprintk("RPC: %5u xmit complete\n", task->tk_pid); 878 dprintk("RPC: %5u xmit complete\n", task->tk_pid);
diff --git a/net/sunrpc/xprtsock.c b/net/sunrpc/xprtsock.c
index ddbe981ab516..4486c59c3aca 100644
--- a/net/sunrpc/xprtsock.c
+++ b/net/sunrpc/xprtsock.c
@@ -579,7 +579,6 @@ static int xs_udp_send_request(struct rpc_task *task)
579 req->rq_svec->iov_base, 579 req->rq_svec->iov_base,
580 req->rq_svec->iov_len); 580 req->rq_svec->iov_len);
581 581
582 req->rq_xtime = jiffies;
583 status = xs_sendpages(transport->sock, 582 status = xs_sendpages(transport->sock,
584 xs_addr(xprt), 583 xs_addr(xprt),
585 xprt->addrlen, xdr, 584 xprt->addrlen, xdr,
@@ -671,7 +670,6 @@ static int xs_tcp_send_request(struct rpc_task *task)
671 * to cope with writespace callbacks arriving _after_ we have 670 * to cope with writespace callbacks arriving _after_ we have
672 * called sendmsg(). */ 671 * called sendmsg(). */
673 while (1) { 672 while (1) {
674 req->rq_xtime = jiffies;
675 status = xs_sendpages(transport->sock, 673 status = xs_sendpages(transport->sock,
676 NULL, 0, xdr, req->rq_bytes_sent); 674 NULL, 0, xdr, req->rq_bytes_sent);
677 675
diff --git a/scripts/Makefile.fwinst b/scripts/Makefile.fwinst
index 3d2f4609578f..c972c0f54ce0 100644
--- a/scripts/Makefile.fwinst
+++ b/scripts/Makefile.fwinst
@@ -28,18 +28,39 @@ endif
28installed-fw := $(addprefix $(INSTALL_FW_PATH)/,$(fw-shipped-all)) 28installed-fw := $(addprefix $(INSTALL_FW_PATH)/,$(fw-shipped-all))
29installed-fw-dirs := $(sort $(dir $(installed-fw))) $(INSTALL_FW_PATH)/. 29installed-fw-dirs := $(sort $(dir $(installed-fw))) $(INSTALL_FW_PATH)/.
30 30
31# Workaround for make < 3.81, where .SECONDEXPANSION doesn't work.
32PHONY += $(INSTALL_FW_PATH)/$$(%) install-all-dirs
33$(INSTALL_FW_PATH)/$$(%): install-all-dirs
34 @true
35install-all-dirs: $(installed-fw-dirs)
36 @true
37
31quiet_cmd_install = INSTALL $(subst $(srctree)/,,$@) 38quiet_cmd_install = INSTALL $(subst $(srctree)/,,$@)
32 cmd_install = $(INSTALL) -m0644 $< $@ 39 cmd_install = $(INSTALL) -m0644 $< $@
33 40
34$(installed-fw-dirs): 41$(installed-fw-dirs):
35 $(call cmd,mkdir) 42 $(call cmd,mkdir)
36 43
37$(installed-fw): $(INSTALL_FW_PATH)/%: $(obj)/% | $(INSTALL_FW_PATH)/$$(dir %)/ 44$(installed-fw): $(INSTALL_FW_PATH)/%: $(obj)/% | $(INSTALL_FW_PATH)/$$(dir %)
38 $(call cmd,install) 45 $(call cmd,install)
39 46
40.PHONY: __fw_install __fw_modinst FORCE 47PHONY += __fw_install __fw_modinst FORCE
48
49.PHONY: $(PHONY)
41 50
42__fw_install: $(installed-fw) 51__fw_install: $(installed-fw)
43__fw_modinst: $(mod-fw) 52__fw_modinst: $(mod-fw)
44 53
45FORCE: 54FORCE:
55
56# Read all saved command lines and dependencies for the $(targets) we
57# may be building using $(if_changed{,_dep}). As an optimization, we
58# don't need to read them if the target does not exist; we will rebuild
59# anyway in that case.
60
61targets := $(wildcard $(sort $(targets)))
62cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
63
64ifneq ($(cmd_files),)
65 include $(cmd_files)
66endif