dbb69752f7
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEZH8oZUiU471FcZm+ONu9yGCSaT4FAmUBaBUACgkQONu9yGCS aT6OkBAArqBSUyCYQJrhoUlFYBnBqF7BLSkj0GwINGSUOlt5ilJ3kZwH9ftjvpWp ZtO0Rp/1yH2H5PpcsaLljPl055Sf30e0oCkz6vX16vy17NGnzI4rJi55+nRZbFRH tBMhMjblgIJoTiTPEQPSGghENok+QzJ9Imffo4/Wru3w5ytkBnGcPPXreHJw+8V5 Pjhzg5tcjhz23rk2wzVtR4VfEqWaHQaapv49rKB1Yls578WYn4QXl4jgUyB7rCo7 9vBB7xy77H1hr9m8ifB/9v1ToV/vw6L1xGPWWWbhsSikFAMBoq34SCsq+6RdeURo 43CCcFsx1s5acM7NQWvxkoV5Hgl8Hc3WgFsx5eVBlNd+vS6ezkgdYuGmN76t+dF/ hZ7XGEoEFuoz9NKQC/5rKjdBd2p/IQYx6vf8EpK0IxFPD4h+DY9pn0FvwuAmxAcA M41xLYGbXX5l/QJR016B1AYiB3DqVxRRRyQT0yNip+PDAh2N06MOJ84KgMSR9lg7 jyeFKZM2vQ619RopMIspuHTWxNiMw7x94aUhBnY1oD+fDzaRn+VNL8po6QYHLK8U QTDhrWplTbTuGIF72h+1IyX1aUj6ozoCewl9Y9ry1u9jBb7LZoupVd0s1dwqORIk 2OSo74pDu5F2BT+4hEcCpDRcYvWlfKbZWBunRrMqvHN8BON0Mks= =aFyS -----END PGP SIGNATURE----- Merge 6.1.53 into android14-6.1-lts Changes in 6.1.53 Revert "bridge: Add extack warning when enabling STP in netns." Partially revert "drm/amd/display: Fix possible underflow for displays with large vblank" scsi: ufs: Try harder to change the power mode Revert "Revert drm/amd/display: Enable Freesync Video Mode by default" ARM: dts: imx: Set default tuning step for imx7d usdhc ALSA: hda/realtek: Enable 4 amplifiers instead of 2 on a HP platform powerpc/boot: Disable power10 features after BOOTAFLAGS assignment media: uapi: HEVC: Add num_delta_pocs_of_ref_rps_idx field Revert "MIPS: unhide PATA_PLATFORM" phy: qcom-snps-femto-v2: use qcom_snps_hsphy_suspend/resume error code media: amphion: use dev_err_probe media: pulse8-cec: handle possible ping error media: pci: cx23885: fix error handling for cx23885 ATSC boards 9p: virtio: fix unlikely null pointer deref in handle_rerror 9p: virtio: make sure 'offs' is initialized in zc_request ksmbd: fix out of bounds in smb3_decrypt_req() ksmbd: validate session id and tree id in compound request ksmbd: no response from compound read ksmbd: fix out of bounds in init_smb2_rsp_hdr() ASoC: da7219: Flush pending AAD IRQ when suspending ASoC: da7219: Check for failure reading AAD IRQ events ASoC: nau8821: Add DMI quirk mechanism for active-high jack-detect ethernet: atheros: fix return value check in atl1c_tso_csum() m68k: Fix invalid .section syntax s390/dasd: use correct number of retries for ERP requests s390/dasd: fix hanging device after request requeue fs/nls: make load_nls() take a const parameter ASoC: rt5682-sdw: fix for JD event handling in ClockStop Mode0 ASoc: codecs: ES8316: Fix DMIC config ASoC: rt711: fix for JD event handling in ClockStop Mode0 ASoC: rt711-sdca: fix for JD event handling in ClockStop Mode0 ASoC: atmel: Fix the 8K sample parameter in I2SC master ALSA: usb-audio: Add quirk for Microsoft Modern Wireless Headset platform/x86: intel: hid: Always call BTNL ACPI method platform/x86/intel/hid: Add HP Dragonfly G2 to VGBS DMI quirks platform/x86: think-lmi: Use kfree_sensitive instead of kfree platform/x86: asus-wmi: Fix setting RGB mode on some TUF laptops platform/x86: huawei-wmi: Silence ambient light sensor drm/amd/smu: use AverageGfxclkFrequency* to replace previous GFX Curr Clock drm/amd/display: Guard DCN31 PHYD32CLK logic against chip family drm/amd/display: Exit idle optimizations before attempt to access PHY ovl: Always reevaluate the file signature for IMA ata: pata_arasan_cf: Use dev_err_probe() instead dev_err() in data_xfer() ALSA: usb-audio: Update for native DSD support quirks staging: fbtft: ili9341: use macro FBTFT_REGISTER_SPI_DRIVER security: keys: perform capable check only on privileged operations kprobes: Prohibit probing on CFI preamble symbol clk: fixed-mmio: make COMMON_CLK_FIXED_MMIO depend on HAS_IOMEM vmbus_testing: fix wrong python syntax for integer value comparison Revert "wifi: ath6k: silence false positive -Wno-dangling-pointer warning on GCC 12" net: dsa: microchip: KSZ9477 register regmap alignment to 32 bit boundaries net: annotate data-races around sk->sk_{rcv|snd}timeo net: usb: qmi_wwan: add Quectel EM05GV2 wifi: brcmfmac: Fix field-spanning write in brcmf_scan_params_v2_to_v1() powerpc/powermac: Use early_* IO variants in via_calibrate_decr() idmaengine: make FSL_EDMA and INTEL_IDMA64 depends on HAS_IOMEM platform/x86/amd/pmf: Fix unsigned comparison with less than zero scsi: lpfc: Remove reftag check in DIF paths scsi: qedi: Fix potential deadlock on &qedi_percpu->p_work_lock net: hns3: restore user pause configure when disable autoneg drm/amdgpu: Match against exact bootloader status wifi: cfg80211: remove links only on AP wifi: mac80211: Use active_links instead of valid_links in Tx netlabel: fix shift wrapping bug in netlbl_catmap_setlong() bnx2x: fix page fault following EEH recovery cifs: fix sockaddr comparison in iface_cmp cifs: fix max_credits implementation sctp: handle invalid error codes without calling BUG() scsi: aacraid: Reply queue mapping to CPUs based on IRQ affinity scsi: storvsc: Always set no_report_opcodes scsi: lpfc: Fix incorrect big endian type assignment in bsg loopback path LoongArch: Let pmd_present() return true when splitting pmd LoongArch: Fix the write_fcsr() macro ALSA: seq: oss: Fix racy open/close of MIDI devices net: sfp: handle 100G/25G active optical cables in sfp_parse_support tracing: Introduce pipe_cpumask to avoid race on trace_pipes platform/mellanox: Fix mlxbf-tmfifo not handling all virtio CONSOLE notifications of: property: Simplify of_link_to_phandle() cpufreq: intel_pstate: set stale CPU frequency to minimum crypto: rsa-pkcs1pad - Use helper to set reqsize tpm: Enable hwrng only for Pluton on AMD CPUs KVM: x86/mmu: Use kstrtobool() instead of strtobool() KVM: x86/mmu: Add "never" option to allow sticky disabling of nx_huge_pages net: Avoid address overwrite in kernel_connect drm/amd/display: ensure async flips are only accepted for fast updates udf: Check consistency of Space Bitmap Descriptor udf: Handle error when adding extent to a file Input: i8042 - add quirk for TUXEDO Gemini 17 Gen1/Clevo PD70PN Revert "PCI: tegra194: Enable support for 256 Byte payload" Revert "net: macsec: preserve ingress frame ordering" tools lib subcmd: Add install target tools lib subcmd: Make install_headers clearer tools lib subcmd: Add dependency test to install_headers tools/resolve_btfids: Use pkg-config to locate libelf tools/resolve_btfids: Install subcmd headers tools/resolve_btfids: Alter how HOSTCC is forced tools/resolve_btfids: Compile resolve_btfids as host program tools/resolve_btfids: Tidy HOST_OVERRIDES tools/resolve_btfids: Pass HOSTCFLAGS as EXTRA_CFLAGS to prepare targets tools/resolve_btfids: Fix setting HOSTCFLAGS reiserfs: Check the return value from __getblk() eventfd: prevent underflow for eventfd semaphores fs: Fix error checking for d_hash_and_lookup() iomap: Remove large folio handling in iomap_invalidate_folio() tmpfs: verify {g,u}id mount options correctly selftests/harness: Actually report SKIP for signal tests vfs, security: Fix automount superblock LSM init problem, preventing NFS sb sharing ARM: ptrace: Restore syscall restart tracing ARM: ptrace: Restore syscall skipping for tracers refscale: Fix uninitalized use of wait_queue_head_t OPP: Fix passing 0 to PTR_ERR in _opp_attach_genpd() selftests/resctrl: Add resctrl.h into build deps selftests/resctrl: Don't leak buffer in fill_cache() selftests/resctrl: Unmount resctrl FS if child fails to run benchmark selftests/resctrl: Close perf value read fd on errors arm64/ptrace: Clean up error handling path in sve_set_common() sched/psi: Select KERNFS as needed x86/decompressor: Don't rely on upper 32 bits of GPRs being preserved arm64/sme: Don't use streaming mode to probe the maximum SME VL arm64/fpsimd: Only provide the length to cpufeature for xCR registers sched/rt: Fix sysctl_sched_rr_timeslice intial value perf/imx_ddr: don't enable counter0 if none of 4 counters are used selftests/futex: Order calls to futex_lock_pi s390/pkey: fix/harmonize internal keyblob headers s390/pkey: fix PKEY_TYPE_EP11_AES handling in PKEY_GENSECK2 IOCTL s390/pkey: fix PKEY_TYPE_EP11_AES handling for sysfs attributes s390/paes: fix PKEY_TYPE_EP11_AES handling for secure keyblobs irqchip/loongson-eiointc: Fix return value checking of eiointc_index ACPI: x86: s2idle: Post-increment variables when getting constraints ACPI: x86: s2idle: Fix a logic error parsing AMD constraints table thermal/of: Fix potential uninitialized value access cpufreq: amd-pstate-ut: Remove module parameter access cpufreq: amd-pstate-ut: Fix kernel panic when loading the driver x86/efistub: Fix PCI ROM preservation in mixed mode cpufreq: powernow-k8: Use related_cpus instead of cpus in driver.exit() selftests/bpf: Fix bpf_nf failure upon test rerun bpftool: use a local copy of perf_event to fix accessing :: Bpf_cookie bpftool: Define a local bpf_perf_link to fix accessing its fields bpftool: Use a local copy of BPF_LINK_TYPE_PERF_EVENT in pid_iter.bpf.c bpftool: Use a local bpf_perf_event_value to fix accessing its fields libbpf: Fix realloc API handling in zero-sized edge cases bpf: Clear the probe_addr for uprobe bpf: Fix an error in verifying a field in a union crypto: qat - change value of default idle filter tcp: tcp_enter_quickack_mode() should be static hwrng: nomadik - keep clock enabled while hwrng is registered hwrng: pic32 - use devm_clk_get_enabled regmap: rbtree: Use alloc_flags for memory allocations wifi: rtw89: debug: Fix error handling in rtw89_debug_priv_btc_manual_set() wifi: mt76: mt7921: fix non-PSC channel scan fail udp: re-score reuseport groups when connected sockets are present bpf: reject unhashed sockets in bpf_sk_assign wifi: mt76: testmode: add nla_policy for MT76_TM_ATTR_TX_LENGTH spi: tegra20-sflash: fix to check return value of platform_get_irq() in tegra_sflash_probe() can: gs_usb: gs_usb_receive_bulk_callback(): count RX overflow errors also in case of OOM wifi: mt76: mt7915: fix power-limits while chan_switch wifi: mwifiex: Fix OOB and integer underflow when rx packets wifi: mwifiex: fix error recovery in PCIE buffer descriptor management selftests/bpf: fix static assert compilation issue for test_cls_*.c kbuild: rust_is_available: remove -v option kbuild: rust_is_available: fix version check when CC has multiple arguments kbuild: rust_is_available: add check for `bindgen` invocation kbuild: rust_is_available: fix confusion when a version appears in the path crypto: stm32 - Properly handle pm_runtime_get failing crypto: api - Use work queue in crypto_destroy_instance Bluetooth: nokia: fix value check in nokia_bluetooth_serdev_probe() Bluetooth: Fix potential use-after-free when clear keys Bluetooth: hci_sync: Don't double print name in add/remove adv_monitor Bluetooth: hci_sync: Avoid use-after-free in dbg for hci_add_adv_monitor() net: tcp: fix unexcepted socket die when snd_wnd is 0 selftests/bpf: Fix repeat option when kfunc_call verification fails selftests/bpf: Clean up fmod_ret in bench_rename test script net-memcg: Fix scope of sockmem pressure indicators ice: ice_aq_check_events: fix off-by-one check when filling buffer crypto: caam - fix unchecked return value error hwrng: iproc-rng200 - Implement suspend and resume calls lwt: Fix return values of BPF xmit ops lwt: Check LWTUNNEL_XMIT_CONTINUE strictly fs: ocfs2: namei: check return value of ocfs2_add_entry() net: annotate data-races around sk->sk_lingertime wifi: mwifiex: fix memory leak in mwifiex_histogram_read() wifi: mwifiex: Fix missed return in oob checks failed path ARM: dts: Add .dts files missing from the build samples/bpf: fix bio latency check with tracepoint samples/bpf: fix broken map lookup probe wifi: ath9k: fix races between ath9k_wmi_cmd and ath9k_wmi_ctrl_rx wifi: ath9k: protect WMI command response buffer replacement with a lock wifi: nl80211/cfg80211: add forgotten nla_policy for BSS color attribute mac80211: make ieee80211_tx_info padding explicit wifi: mwifiex: avoid possible NULL skb pointer dereference Bluetooth: btusb: Do not call kfree_skb() under spin_lock_irqsave() arm64: mm: use ptep_clear() instead of pte_clear() in clear_flush() wifi: ath9k: use IS_ERR() with debugfs_create_dir() ice: avoid executing commands on other ports when driving sync net: arcnet: Do not call kfree_skb() under local_irq_disable() mlxsw: i2c: Fix chunk size setting in output mailbox buffer mlxsw: i2c: Limit single transaction buffer size mlxsw: core_hwmon: Adjust module label names based on MTCAP sensor counter hwmon: (tmp513) Fix the channel number in tmp51x_is_visible() octeontx2-pf: Refactor schedular queue alloc/free calls octeontx2-pf: Fix PFC TX scheduler free cteonxt2-pf: Fix backpressure config for multiple PFC priorities to work simultaneously sfc: Check firmware supports Ethernet PTP filter net/sched: sch_hfsc: Ensure inner classes have fsc curve netrom: Deny concurrent connect(). drm/bridge: tc358764: Fix debug print parameter order ASoC: cs43130: Fix numerator/denominator mixup quota: factor out dquot_write_dquot() quota: rename dquot_active() to inode_quota_active() quota: add new helper dquot_active() quota: fix dqput() to follow the guarantees dquot_srcu should provide drm/amd/display: Do not set drr on pipe commit drm/hyperv: Fix a compilation issue because of not including screen_info.h ASoC: stac9766: fix build errors with REGMAP_AC97 soc: qcom: ocmem: Add OCMEM hardware version print soc: qcom: ocmem: Fix NUM_PORTS & NUM_MACROS macros arm64: dts: qcom: sm6350: Fix ZAP region arm64: dts: qcom: sm8250: correct dynamic power coefficients arm64: dts: qcom: msm8916-l8150: correct light sensor VDDIO supply arm64: dts: qcom: sm8250-edo: Add gpio line names for TLMM arm64: dts: qcom: sm8250-edo: Add GPIO line names for PMIC GPIOs arm64: dts: qcom: sm8250-edo: Rectify gpio-keys arm64: dts: qcom: sc8280xp-crd: Correct vreg_misc_3p3 GPIO arm64: dts: qcom: sc8280xp: Add missing SCM interconnect arm64: dts: qcom: msm8996: Add missing interrupt to the USB2 controller arm64: dts: qcom: sdm845-tama: Set serial indices and stdout-path arm64: dts: qcom: sm8350: Fix CPU idle state residency times arm64: dts: qcom: sm8350: Add missing LMH interrupts to cpufreq arm64: dts: qcom: sm8350: Use proper CPU compatibles arm64: dts: qcom: pm8350: fix thermal zone name arm64: dts: qcom: pm8350b: fix thermal zone name arm64: dts: qcom: pmr735b: fix thermal zone name arm64: dts: qcom: pmk8350: fix ADC-TM compatible string arm64: dts: qcom: sm8250: Mark PCIe hosts as DMA coherent ARM: dts: stm32: Rename mdio0 to mdio ARM: dts: stm32: YAML validation fails for Argon Boards ARM: dts: stm32: adopt generic iio bindings for adc channels on emstamp-argon ARM: dts: stm32: Add missing detach mailbox for emtrion emSBC-Argon ARM: dts: stm32: YAML validation fails for Odyssey Boards ARM: dts: stm32: Add missing detach mailbox for Odyssey SoM ARM: dts: stm32: Update to generic ADC channel binding on DHSOM systems ARM: dts: stm32: Add missing detach mailbox for DHCOM SoM firmware: ti_sci: Use system_state to determine polling drm/amdgpu: avoid integer overflow warning in amdgpu_device_resize_fb_bar() ARM: dts: BCM53573: Drop nonexistent #usb-cells ARM: dts: BCM53573: Add cells sizes to PCIe node ARM: dts: BCM53573: Use updated "spi-gpio" binding properties arm64: tegra: Fix HSUART for Jetson AGX Orin arm64: dts: qcom: sm8250-sony-xperia: correct GPIO keys wakeup again arm64: dts: qcom: pm6150l: Add missing short interrupt arm64: dts: qcom: pm660l: Add missing short interrupt arm64: dts: qcom: pmi8994: Add missing OVP interrupt arm64: tegra: Fix HSUART for Smaug drm/etnaviv: fix dumping of active MMU context block: cleanup queue_wc_store block: don't allow enabling a cache on devices that don't support it x86/mm: Fix PAT bit missing from page protection modify mask drm/bridge: anx7625: Use common macros for DP power sequencing commands drm/bridge: anx7625: Use common macros for HDCP capabilities ARM: dts: samsung: s3c6410-mini6410: correct ethernet reg addresses (split) ARM: dts: s5pv210: add dummy 5V regulator for backlight on SMDKv210 ARM: dts: samsung: s5pv210-smdkv210: correct ethernet reg addresses (split) drm: adv7511: Fix low refresh rate register for ADV7533/5 ARM: dts: BCM53573: Fix Ethernet info for Luxul devices arm64: dts: qcom: sdm845: Add missing RPMh power domain to GCC arm64: dts: qcom: sdm845: Fix the min frequency of "ice_core_clk" arm64: dts: qcom: msm8996-gemini: fix touchscreen VIO supply drm/amdgpu: Update min() to min_t() in 'amdgpu_info_ioctl' md: Factor out is_md_suspended helper md: Change active_io to percpu md: restore 'noio_flag' for the last mddev_resume() md/raid10: factor out dereference_rdev_and_rrdev() md/raid10: use dereference_rdev_and_rrdev() to get devices md/md-bitmap: remove unnecessary local variable in backlog_store() md/md-bitmap: hold 'reconfig_mutex' in backlog_store() drm/msm: Update dev core dump to not print backwards drm/tegra: dpaux: Fix incorrect return value of platform_get_irq of: unittest: fix null pointer dereferencing in of_unittest_find_node_by_name() arm64: dts: qcom: sm8150: Fix the I2C7 interrupt ARM: dts: BCM53573: Fix Tenda AC9 switch CPU port drm/armada: Fix off-by-one error in armada_overlay_get_property() drm/repaper: Reduce temporary buffer size in repaper_fb_dirty() drm/panel: simple: Add missing connector type and pixel format for AUO T215HVN01 ima: Remove deprecated IMA_TRUSTED_KEYRING Kconfig drm: xlnx: zynqmp_dpsub: Add missing check for dma_set_mask soc: qcom: smem: Fix incompatible types in comparison drm/msm/mdp5: Don't leak some plane state firmware: meson_sm: fix to avoid potential NULL pointer dereference drm/msm/dpu: fix the irq index in dpu_encoder_phys_wb_wait_for_commit_done smackfs: Prevent underflow in smk_set_cipso() drm/amd/pm: fix variable dereferenced issue in amdgpu_device_attr_create() drm/msm/a2xx: Call adreno_gpu_init() earlier audit: fix possible soft lockup in __audit_inode_child() block/mq-deadline: use correct way to throttling write requests io_uring: fix drain stalls by invalid SQE drm/mediatek: dp: Add missing error checks in mtk_dp_parse_capabilities bus: ti-sysc: Fix build warning for 64-bit build drm/mediatek: Remove freeing not dynamic allocated memory ARM: dts: qcom: ipq4019: correct SDHCI XO clock drm/mediatek: Fix potential memory leak if vmap() fail arm64: dts: qcom: apq8016-sbc: Fix ov5640 regulator supply names arm64: dts: qcom: msm8998: Drop bus clock reference from MMSS SMMU arm64: dts: qcom: msm8998: Add missing power domain to MMSS SMMU arm64: dts: qcom: msm8996: Fix dsi1 interrupts arm64: dts: qcom: sc8280xp-x13s: Unreserve NC pins bus: ti-sysc: Fix cast to enum warning md/raid5-cache: fix a deadlock in r5l_exit_log() md/raid5-cache: fix null-ptr-deref for r5l_flush_stripe_to_raid() firmware: cs_dsp: Fix new control name check md: add error_handlers for raid0 and linear md/raid0: Factor out helper for mapping and submitting a bio md/raid0: Fix performance regression for large sequential writes md: raid0: account for split bio in iostat accounting ASoC: SOF: amd: clear dsp to host interrupt status of: overlay: Call of_changeset_init() early of: unittest: Fix overlay type in apply/revert check ALSA: ac97: Fix possible error value of *rac97 ipmi:ssif: Add check for kstrdup ipmi:ssif: Fix a memory leak when scanning for an adapter clk: qcom: gpucc-sm6350: Introduce index-based clk lookup clk: qcom: gpucc-sm6350: Fix clock source names clk: qcom: gcc-sc8280xp: Add EMAC GDSCs clk: qcom: gcc-sc8280xp: Add missing GDSC flags dt-bindings: clock: qcom,gcc-sc8280xp: Add missing GDSCs clk: qcom: gcc-sc8280xp: Add missing GDSCs clk: rockchip: rk3568: Fix PLL rate setting for 78.75MHz PCI: apple: Initialize pcie->nvecs before use PCI: qcom-ep: Switch MHI bus master clock off during L1SS drivers: clk: keystone: Fix parameter judgment in _of_pll_clk_init() PCI/DOE: Fix destroy_work_on_stack() race clk: sunxi-ng: Modify mismatched function name clk: qcom: gcc-sc7180: Fix up gcc_sdcc2_apps_clk_src EDAC/igen6: Fix the issue of no error events ext4: correct grp validation in ext4_mb_good_group ext4: avoid potential data overflow in next_linear_group clk: qcom: gcc-sm8250: Fix gcc_sdcc2_apps_clk_src kvm/vfio: Prepare for accepting vfio device fd kvm/vfio: ensure kvg instance stays around in kvm_vfio_group_add() clk: qcom: reset: Use the correct type of sleep/delay based on length clk: qcom: gcc-sm6350: Fix gcc_sdcc2_apps_clk_src PCI: microchip: Correct the DED and SEC interrupt bit offsets PCI: Mark NVIDIA T4 GPUs to avoid bus reset pinctrl: mcp23s08: check return value of devm_kasprintf() PCI: Allow drivers to request exclusive config regions PCI: Add locking to RMW PCI Express Capability Register accessors PCI: pciehp: Use RMW accessors for changing LNKCTL PCI/ASPM: Use RMW accessors for changing LNKCTL clk: qcom: gcc-sm8450: Use floor ops for SDCC RCGs clk: imx: pllv4: Fix SPLL2 MULT range clk: imx: imx8ulp: update SPLL2 type clk: imx8mp: fix sai4 clock clk: imx: composite-8m: fix clock pauses when set_rate would be a no-op powerpc/radix: Move some functions into #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE vfio/type1: fix cap_migration information leak nvdimm: Fix memleak of pmu attr_groups in unregister_nvdimm_pmu() nvdimm: Fix dereference after free in register_nvdimm_pmu() powerpc/fadump: reset dump area size if fadump memory reserve fails powerpc/perf: Convert fsl_emb notifier to state machine callbacks drm/amdgpu: Use RMW accessors for changing LNKCTL drm/radeon: Use RMW accessors for changing LNKCTL net/mlx5: Use RMW accessors for changing LNKCTL wifi: ath11k: Use RMW accessors for changing LNKCTL wifi: ath10k: Use RMW accessors for changing LNKCTL NFSv4.2: Rework scratch handling for READ_PLUS NFSv4.2: Fix READ_PLUS smatch warnings NFSv4.2: Fix up READ_PLUS alignment NFSv4.2: Fix READ_PLUS size calculations powerpc: Don't include lppaca.h in paca.h powerpc/pseries: Rework lppaca_shared_proc() to avoid DEBUG_PREEMPT nfs/blocklayout: Use the passed in gfp flags powerpc/pseries: Fix hcall tracepoints with JUMP_LABEL=n powerpc/mpc5xxx: Add missing fwnode_handle_put() powerpc/iommu: Fix notifiers being shared by PCI and VIO buses ext4: fix unttached inode after power cut with orphan file feature enabled jfs: validate max amount of blocks before allocation. fs: lockd: avoid possible wrong NULL parameter NFSD: da_addr_body field missing in some GETDEVICEINFO replies NFS: Guard against READDIR loop when entry names exceed MAXNAMELEN NFSv4.2: fix handling of COPY ERR_OFFLOAD_NO_REQ pNFS: Fix assignment of xprtdata.cred cgroup/cpuset: Inherit parent's load balance state in v2 RDMA/qedr: Remove a duplicate assignment in irdma_query_ah() media: ov5640: fix low resolution image abnormal issue media: ad5820: Drop unsupported ad5823 from i2c_ and of_device_id tables media: i2c: tvp5150: check return value of devm_kasprintf() media: v4l2-core: Fix a potential resource leak in v4l2_fwnode_parse_link() iommu/amd/iommu_v2: Fix pasid_state refcount dec hit 0 warning on pasid unbind iommu: rockchip: Fix directory table address encoding drivers: usb: smsusb: fix error handling code in smsusb_init_device media: dib7000p: Fix potential division by zero media: dvb-usb: m920x: Fix a potential memory leak in m920x_i2c_xfer() media: cx24120: Add retval check for cx24120_message_send() RDMA/siw: Fabricate a GID on tun and loopback devices scsi: hisi_sas: Fix warnings detected by sparse scsi: hisi_sas: Fix normally completed I/O analysed as failed dt-bindings: extcon: maxim,max77843: restrict connector properties media: amphion: reinit vpu if reqbufs output 0 media: amphion: add helper function to get id name media: mtk-jpeg: Fix use after free bug due to uncanceled work media: rkvdec: increase max supported height for H.264 media: amphion: fix CHECKED_RETURN issues reported by coverity media: amphion: fix REVERSE_INULL issues reported by coverity media: amphion: fix UNINIT issues reported by coverity media: amphion: fix UNUSED_VALUE issue reported by coverity media: amphion: ensure the bitops don't cross boundaries media: mediatek: vcodec: Return NULL if no vdec_fb is found media: mediatek: vcodec: fix potential double free media: mediatek: vcodec: fix resource leaks in vdec_msg_queue_init() usb: phy: mxs: fix getting wrong state with mxs_phy_is_otg_host() scsi: RDMA/srp: Fix residual handling scsi: iscsi: Rename iscsi_set_param() to iscsi_if_set_param() scsi: iscsi: Add length check for nlattr payload scsi: iscsi: Add strlen() check in iscsi_if_set{_host}_param() scsi: be2iscsi: Add length check when parsing nlattrs scsi: qla4xxx: Add length check when parsing nlattrs iio: accel: adxl313: Fix adxl313_i2c_id[] table serial: sprd: Assign sprd_port after initialized to avoid wrong access serial: sprd: Fix DMA buffer leak issue x86/APM: drop the duplicate APM_MINOR_DEV macro RDMA/rxe: Split rxe_run_task() into two subroutines RDMA/rxe: Fix incomplete state save in rxe_requester scsi: qedf: Do not touch __user pointer in qedf_dbg_stop_io_on_error_cmd_read() directly scsi: qedf: Do not touch __user pointer in qedf_dbg_debug_cmd_read() directly scsi: qedf: Do not touch __user pointer in qedf_dbg_fp_int_cmd_read() directly RDMA/irdma: Replace one-element array with flexible-array member coresight: tmc: Explicit type conversions to prevent integer overflow interconnect: qcom: qcm2290: Enable sync state dma-buf/sync_file: Fix docs syntax driver core: test_async: fix an error code driver core: Call dma_cleanup() on the test_remove path kernfs: add stub helper for kernfs_generic_poll() extcon: cht_wc: add POWER_SUPPLY dependency iommu/mediatek: Remove unused "mapping" member from mtk_iommu_data iommu/mediatek: Fix two IOMMU share pagetable issue iommu/sprd: Add missing force_aperture RDMA/hns: Fix port active speed RDMA/hns: Fix incorrect post-send with direct wqe of wr-list RDMA/hns: Fix inaccurate error label name in init instance RDMA/hns: Fix CQ and QP cache affinity IB/uverbs: Fix an potential error pointer dereference fsi: aspeed: Reset master errors after CFAM reset iommu/qcom: Disable and reset context bank before programming iommu/vt-d: Fix to flush cache of PASID directory table platform/x86: dell-sysman: Fix reference leak media: cec: core: add adap_nb_transmit_canceled() callback media: cec: core: add adap_unconfigured() callback media: go7007: Remove redundant if statement media: venus: hfi_venus: Only consider sys_idle_indicator on V1 docs: ABI: fix spelling/grammar in SBEFIFO timeout interface USB: gadget: core: Add missing kerneldoc for vbus_work USB: gadget: f_mass_storage: Fix unused variable warning drivers: base: Free devm resources when unregistering a device HID: input: Support devices sending Eraser without Invert media: ov5640: Enable MIPI interface in ov5640_set_power_mipi() media: ov5640: Fix initial RESETB state and annotate timings media: i2c: ov2680: Set V4L2_CTRL_FLAG_MODIFY_LAYOUT on flips media: ov2680: Remove auto-gain and auto-exposure controls media: ov2680: Fix ov2680_bayer_order() media: ov2680: Fix vflip / hflip set functions media: ov2680: Remove VIDEO_V4L2_SUBDEV_API ifdef-s media: ov2680: Don't take the lock for try_fmt calls media: ov2680: Add ov2680_fill_format() helper function media: ov2680: Fix ov2680_set_fmt() which == V4L2_SUBDEV_FORMAT_TRY not working media: ov2680: Fix regulators being left enabled on ov2680_power_on() errors media: i2c: rdacm21: Fix uninitialized value f2fs: fix to avoid mmap vs set_compress_option case f2fs: judge whether discard_unit is section only when have CONFIG_BLK_DEV_ZONED f2fs: Only lfs mode is allowed with zoned block device feature Revert "f2fs: fix to do sanity check on extent cache correctly" cgroup:namespace: Remove unused cgroup_namespaces_init() coresight: trbe: Fix TRBE potential sleep in atomic context RDMA/irdma: Prevent zero-length STAG registration scsi: core: Use 32-bit hostnum in scsi_host_lookup() scsi: fcoe: Fix potential deadlock on &fip->ctlr_lock interconnect: qcom: sm8450: Enable sync_state interconnect: qcom: bcm-voter: Improve enable_mask handling interconnect: qcom: bcm-voter: Use enable_maks for keepalive voting serial: tegra: handle clk prepare error in tegra_uart_hw_init() amba: bus: fix refcount leak Revert "IB/isert: Fix incorrect release of isert connection" RDMA/siw: Balance the reference of cep->kref in the error path RDMA/siw: Correct wrong debug message RDMA/efa: Fix wrong resources deallocation order HID: logitech-dj: Fix error handling in logi_dj_recv_switch_to_dj_mode() HID: uclogic: Correct devm device reference for hidinput input_dev name HID: multitouch: Correct devm device reference for hidinput input_dev name platform/x86/amd/pmf: Fix a missing cleanup path tick/rcu: Fix false positive "softirq work is pending" messages x86/speculation: Mark all Skylake CPUs as vulnerable to GDS tracing: Remove extra space at the end of hwlat_detector/mode tracing: Fix race issue between cpu buffer write and swap mtd: rawnand: brcmnand: Fix mtd oobsize dmaengine: idxd: Modify the dependence of attribute pasid_enabled phy/rockchip: inno-hdmi: use correct vco_div_5 macro on rk3328 phy/rockchip: inno-hdmi: round fractal pixclock in rk3328 recalc_rate phy/rockchip: inno-hdmi: do not power on rk3328 post pll on reg write rpmsg: glink: Add check for kstrdup leds: pwm: Fix error code in led_pwm_create_fwnode() leds: multicolor: Use rounded division when calculating color components leds: Fix BUG_ON check for LED_COLOR_ID_MULTI that is always false leds: trigger: tty: Do not use LED_ON/OFF constants, use led_blink_set_oneshot instead mtd: spi-nor: Check bus width while setting QE bit mtd: rawnand: fsmc: handle clk prepare error in fsmc_nand_resume() um: Fix hostaudio build errors dmaengine: ste_dma40: Add missing IRQ check in d40_probe Drivers: hv: vmbus: Don't dereference ACPI root object handle cpufreq: Fix the race condition while updating the transition_task of policy virtio_ring: fix avail_wrap_counter in virtqueue_add_packed igmp: limit igmpv3_newpack() packet size to IP_MAX_MTU netfilter: ipset: add the missing IP_SET_HASH_WITH_NET0 macro for ip_set_hash_netportnet.c netfilter: nft_exthdr: Fix non-linear header modification netfilter: xt_u32: validate user space input netfilter: xt_sctp: validate the flag_info count skbuff: skb_segment, Call zero copy functions before using skbuff frags igb: set max size RX buffer when store bad packet is enabled PM / devfreq: Fix leak in devfreq_dev_release() ALSA: pcm: Fix missing fixup call in compat hw_refine ioctl rcu: dump vmalloc memory info safely printk: ringbuffer: Fix truncating buffer size min_t cast scsi: core: Fix the scsi_set_resid() documentation mm/vmalloc: add a safer version of find_vm_area() for debug cpu/hotplug: Prevent self deadlock on CPU hot-unplug media: i2c: ccs: Check rules is non-NULL media: i2c: Add a camera sensor top level menu PCI: rockchip: Use 64-bit mask on MSI 64-bit PCI address ipmi_si: fix a memleak in try_smi_init() ARM: OMAP2+: Fix -Warray-bounds warning in _pwrdm_state_switch() XArray: Do not return sibling entries from xa_load() io_uring: break iopolling on signal backlight/gpio_backlight: Compare against struct fb_info.device backlight/bd6107: Compare against struct fb_info.device backlight/lv5207lp: Compare against struct fb_info.device drm/amd/display: register edp_backlight_control() for DCN301 xtensa: PMU: fix base address for the newer hardware LoongArch: mm: Add p?d_leaf() definitions i3c: master: svc: fix probe failure when no i3c device exist arm64: csum: Fix OoB access in IP checksum code for negative lengths ALSA: hda/cirrus: Fix broken audio on hardware with two CS42L42 codecs. media: dvb: symbol fixup for dvb_attach() media: venus: hfi_venus: Write to VIDC_CTRL_INIT after unmasking interrupts Revert "scsi: qla2xxx: Fix buffer overrun" scsi: mpt3sas: Perform additional retries if doorbell read returns 0 PCI: Free released resource after coalescing PCI: hv: Fix a crash in hv_pci_restore_msi_msg() during hibernation PCI/PM: Only read PCI_PM_CTRL register when available ntb: Drop packets when qp link is down ntb: Clean up tx tail index on link down ntb: Fix calculation ntb_transport_tx_free_entry() Revert "PCI: Mark NVIDIA T4 GPUs to avoid bus reset" block: don't add or resize partition on the disk with GENHD_FL_NO_PART procfs: block chmod on /proc/thread-self/comm parisc: Fix /proc/cpuinfo output for lscpu drm/amd/display: Add smu write msg id fail retry process bpf: Fix issue in verifying allow_ptr_leaks dlm: fix plock lookup when using multiple lockspaces dccp: Fix out of bounds access in DCCP error handler x86/sev: Make enc_dec_hypercall() accept a size instead of npages r8169: fix ASPM-related issues on a number of systems with NIC version from RTL8168h X.509: if signature is unsupported skip validation net: handle ARPHRD_PPP in dev_is_mac_header_xmit() fsverity: skip PKCS#7 parser when keyring is empty x86/MCE: Always save CS register on AMD Zen IF Poison errors platform/chrome: chromeos_acpi: print hex string for ACPI_TYPE_BUFFER mmc: renesas_sdhi: register irqs before registering controller pstore/ram: Check start of empty przs during init arm64: sdei: abort running SDEI handlers during crash s390/dcssblk: fix kernel crash with list_add corruption s390/ipl: add missing secure/has_secure file to ipl type 'unknown' s390/dasd: fix string length handling crypto: stm32 - fix loop iterating through scatterlist for DMA cpufreq: brcmstb-avs-cpufreq: Fix -Warray-bounds bug of: property: fw_devlink: Add a devlink for panel followers usb: typec: tcpm: set initial svdm version based on pd revision usb: typec: bus: verify partner exists in typec_altmode_attention USB: core: Unite old scheme and new scheme descriptor reads USB: core: Change usb_get_device_descriptor() API USB: core: Fix race by not overwriting udev->descriptor in hub_port_init() USB: core: Fix oversight in SuperSpeed initialization x86/sgx: Break up long non-preemptible delays in sgx_vepc_release() perf/x86/uncore: Correct the number of CHAs on EMR serial: sc16is7xx: remove obsolete out_thread label serial: sc16is7xx: fix regression with GPIO configuration tracing: Zero the pipe cpumask on alloc to avoid spurious -EBUSY Revert "drm/amd/display: Do not set drr on pipe commit" md: Free resources in __md_stop NFSv4.2: Fix a potential double free with READ_PLUS NFSv4.2: Rework scratch handling for READ_PLUS (again) md: fix regression for null-ptr-deference in __md_stop() clk: Mark a fwnode as initialized when using CLK_OF_DECLARE() macro treewide: Fix probing of devices in DT overlays clk: Avoid invalid function names in CLK_OF_DECLARE() udf: initialize newblock to 0 Linux 6.1.53 Change-Id: I6f5858bce0f20963ae42515eac36ac14cb686f24 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
2887 lines
74 KiB
C
2887 lines
74 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* Virtio ring implementation.
|
|
*
|
|
* Copyright 2007 Rusty Russell IBM Corporation
|
|
*/
|
|
#include <linux/virtio.h>
|
|
#include <linux/virtio_ring.h>
|
|
#include <linux/virtio_config.h>
|
|
#include <linux/device.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/module.h>
|
|
#include <linux/hrtimer.h>
|
|
#include <linux/dma-mapping.h>
|
|
#include <linux/kmsan.h>
|
|
#include <linux/spinlock.h>
|
|
#include <xen/xen.h>
|
|
|
|
#ifdef DEBUG
|
|
/* For development, we want to crash whenever the ring is screwed. */
|
|
#define BAD_RING(_vq, fmt, args...) \
|
|
do { \
|
|
dev_err(&(_vq)->vq.vdev->dev, \
|
|
"%s:"fmt, (_vq)->vq.name, ##args); \
|
|
BUG(); \
|
|
} while (0)
|
|
/* Caller is supposed to guarantee no reentry. */
|
|
#define START_USE(_vq) \
|
|
do { \
|
|
if ((_vq)->in_use) \
|
|
panic("%s:in_use = %i\n", \
|
|
(_vq)->vq.name, (_vq)->in_use); \
|
|
(_vq)->in_use = __LINE__; \
|
|
} while (0)
|
|
#define END_USE(_vq) \
|
|
do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
|
|
#define LAST_ADD_TIME_UPDATE(_vq) \
|
|
do { \
|
|
ktime_t now = ktime_get(); \
|
|
\
|
|
/* No kick or get, with .1 second between? Warn. */ \
|
|
if ((_vq)->last_add_time_valid) \
|
|
WARN_ON(ktime_to_ms(ktime_sub(now, \
|
|
(_vq)->last_add_time)) > 100); \
|
|
(_vq)->last_add_time = now; \
|
|
(_vq)->last_add_time_valid = true; \
|
|
} while (0)
|
|
#define LAST_ADD_TIME_CHECK(_vq) \
|
|
do { \
|
|
if ((_vq)->last_add_time_valid) { \
|
|
WARN_ON(ktime_to_ms(ktime_sub(ktime_get(), \
|
|
(_vq)->last_add_time)) > 100); \
|
|
} \
|
|
} while (0)
|
|
#define LAST_ADD_TIME_INVALID(_vq) \
|
|
((_vq)->last_add_time_valid = false)
|
|
#else
|
|
#define BAD_RING(_vq, fmt, args...) \
|
|
do { \
|
|
dev_err(&_vq->vq.vdev->dev, \
|
|
"%s:"fmt, (_vq)->vq.name, ##args); \
|
|
(_vq)->broken = true; \
|
|
} while (0)
|
|
#define START_USE(vq)
|
|
#define END_USE(vq)
|
|
#define LAST_ADD_TIME_UPDATE(vq)
|
|
#define LAST_ADD_TIME_CHECK(vq)
|
|
#define LAST_ADD_TIME_INVALID(vq)
|
|
#endif
|
|
|
|
struct vring_desc_state_split {
|
|
void *data; /* Data for callback. */
|
|
struct vring_desc *indir_desc; /* Indirect descriptor, if any. */
|
|
};
|
|
|
|
struct vring_desc_state_packed {
|
|
void *data; /* Data for callback. */
|
|
struct vring_packed_desc *indir_desc; /* Indirect descriptor, if any. */
|
|
u16 num; /* Descriptor list length. */
|
|
u16 last; /* The last desc state in a list. */
|
|
};
|
|
|
|
struct vring_desc_extra {
|
|
dma_addr_t addr; /* Descriptor DMA addr. */
|
|
u32 len; /* Descriptor length. */
|
|
u16 flags; /* Descriptor flags. */
|
|
u16 next; /* The next desc state in a list. */
|
|
};
|
|
|
|
struct vring_virtqueue_split {
|
|
/* Actual memory layout for this queue. */
|
|
struct vring vring;
|
|
|
|
/* Last written value to avail->flags */
|
|
u16 avail_flags_shadow;
|
|
|
|
/*
|
|
* Last written value to avail->idx in
|
|
* guest byte order.
|
|
*/
|
|
u16 avail_idx_shadow;
|
|
|
|
/* Per-descriptor state. */
|
|
struct vring_desc_state_split *desc_state;
|
|
struct vring_desc_extra *desc_extra;
|
|
|
|
/* DMA address and size information */
|
|
dma_addr_t queue_dma_addr;
|
|
size_t queue_size_in_bytes;
|
|
|
|
/*
|
|
* The parameters for creating vrings are reserved for creating new
|
|
* vring.
|
|
*/
|
|
u32 vring_align;
|
|
bool may_reduce_num;
|
|
};
|
|
|
|
struct vring_virtqueue_packed {
|
|
/* Actual memory layout for this queue. */
|
|
struct {
|
|
unsigned int num;
|
|
struct vring_packed_desc *desc;
|
|
struct vring_packed_desc_event *driver;
|
|
struct vring_packed_desc_event *device;
|
|
} vring;
|
|
|
|
/* Driver ring wrap counter. */
|
|
bool avail_wrap_counter;
|
|
|
|
/* Avail used flags. */
|
|
u16 avail_used_flags;
|
|
|
|
/* Index of the next avail descriptor. */
|
|
u16 next_avail_idx;
|
|
|
|
/*
|
|
* Last written value to driver->flags in
|
|
* guest byte order.
|
|
*/
|
|
u16 event_flags_shadow;
|
|
|
|
/* Per-descriptor state. */
|
|
struct vring_desc_state_packed *desc_state;
|
|
struct vring_desc_extra *desc_extra;
|
|
|
|
/* DMA address and size information */
|
|
dma_addr_t ring_dma_addr;
|
|
dma_addr_t driver_event_dma_addr;
|
|
dma_addr_t device_event_dma_addr;
|
|
size_t ring_size_in_bytes;
|
|
size_t event_size_in_bytes;
|
|
};
|
|
|
|
struct vring_virtqueue {
|
|
struct virtqueue vq;
|
|
|
|
/* Is this a packed ring? */
|
|
bool packed_ring;
|
|
|
|
/* Is DMA API used? */
|
|
bool use_dma_api;
|
|
|
|
/* Can we use weak barriers? */
|
|
bool weak_barriers;
|
|
|
|
/* Other side has made a mess, don't try any more. */
|
|
bool broken;
|
|
|
|
/* Host supports indirect buffers */
|
|
bool indirect;
|
|
|
|
/* Host publishes avail event idx */
|
|
bool event;
|
|
|
|
/* Head of free buffer list. */
|
|
unsigned int free_head;
|
|
/* Number we've added since last sync. */
|
|
unsigned int num_added;
|
|
|
|
/* Last used index we've seen.
|
|
* for split ring, it just contains last used index
|
|
* for packed ring:
|
|
* bits up to VRING_PACKED_EVENT_F_WRAP_CTR include the last used index.
|
|
* bits from VRING_PACKED_EVENT_F_WRAP_CTR include the used wrap counter.
|
|
*/
|
|
u16 last_used_idx;
|
|
|
|
/* Hint for event idx: already triggered no need to disable. */
|
|
bool event_triggered;
|
|
|
|
union {
|
|
/* Available for split ring */
|
|
struct vring_virtqueue_split split;
|
|
|
|
/* Available for packed ring */
|
|
struct vring_virtqueue_packed packed;
|
|
};
|
|
|
|
/* How to notify other side. FIXME: commonalize hcalls! */
|
|
bool (*notify)(struct virtqueue *vq);
|
|
|
|
/* DMA, allocation, and size information */
|
|
bool we_own_ring;
|
|
|
|
#ifdef DEBUG
|
|
/* They're supposed to lock for us. */
|
|
unsigned int in_use;
|
|
|
|
/* Figure out if their kicks are too delayed. */
|
|
bool last_add_time_valid;
|
|
ktime_t last_add_time;
|
|
#endif
|
|
};
|
|
|
|
static struct virtqueue *__vring_new_virtqueue(unsigned int index,
|
|
struct vring_virtqueue_split *vring_split,
|
|
struct virtio_device *vdev,
|
|
bool weak_barriers,
|
|
bool context,
|
|
bool (*notify)(struct virtqueue *),
|
|
void (*callback)(struct virtqueue *),
|
|
const char *name);
|
|
static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num);
|
|
static void vring_free(struct virtqueue *_vq);
|
|
|
|
/*
|
|
* Helpers.
|
|
*/
|
|
|
|
#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
|
|
|
|
static inline bool virtqueue_use_indirect(struct vring_virtqueue *vq,
|
|
unsigned int total_sg)
|
|
{
|
|
/*
|
|
* If the host supports indirect descriptor tables, and we have multiple
|
|
* buffers, then go indirect. FIXME: tune this threshold
|
|
*/
|
|
return (vq->indirect && total_sg > 1 && vq->vq.num_free);
|
|
}
|
|
|
|
/*
|
|
* Modern virtio devices have feature bits to specify whether they need a
|
|
* quirk and bypass the IOMMU. If not there, just use the DMA API.
|
|
*
|
|
* If there, the interaction between virtio and DMA API is messy.
|
|
*
|
|
* On most systems with virtio, physical addresses match bus addresses,
|
|
* and it doesn't particularly matter whether we use the DMA API.
|
|
*
|
|
* On some systems, including Xen and any system with a physical device
|
|
* that speaks virtio behind a physical IOMMU, we must use the DMA API
|
|
* for virtio DMA to work at all.
|
|
*
|
|
* On other systems, including SPARC and PPC64, virtio-pci devices are
|
|
* enumerated as though they are behind an IOMMU, but the virtio host
|
|
* ignores the IOMMU, so we must either pretend that the IOMMU isn't
|
|
* there or somehow map everything as the identity.
|
|
*
|
|
* For the time being, we preserve historic behavior and bypass the DMA
|
|
* API.
|
|
*
|
|
* TODO: install a per-device DMA ops structure that does the right thing
|
|
* taking into account all the above quirks, and use the DMA API
|
|
* unconditionally on data path.
|
|
*/
|
|
|
|
static bool vring_use_dma_api(struct virtio_device *vdev)
|
|
{
|
|
if (!virtio_has_dma_quirk(vdev))
|
|
return true;
|
|
|
|
/* Otherwise, we are left to guess. */
|
|
/*
|
|
* In theory, it's possible to have a buggy QEMU-supposed
|
|
* emulated Q35 IOMMU and Xen enabled at the same time. On
|
|
* such a configuration, virtio has never worked and will
|
|
* not work without an even larger kludge. Instead, enable
|
|
* the DMA API if we're a Xen guest, which at least allows
|
|
* all of the sensible Xen configurations to work correctly.
|
|
*/
|
|
if (xen_domain())
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
size_t virtio_max_dma_size(struct virtio_device *vdev)
|
|
{
|
|
size_t max_segment_size = SIZE_MAX;
|
|
|
|
if (vring_use_dma_api(vdev))
|
|
max_segment_size = dma_max_mapping_size(vdev->dev.parent);
|
|
|
|
return max_segment_size;
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtio_max_dma_size);
|
|
|
|
static void *vring_alloc_queue(struct virtio_device *vdev, size_t size,
|
|
dma_addr_t *dma_handle, gfp_t flag)
|
|
{
|
|
if (vring_use_dma_api(vdev)) {
|
|
return dma_alloc_coherent(vdev->dev.parent, size,
|
|
dma_handle, flag);
|
|
} else {
|
|
void *queue = alloc_pages_exact(PAGE_ALIGN(size), flag);
|
|
|
|
if (queue) {
|
|
phys_addr_t phys_addr = virt_to_phys(queue);
|
|
*dma_handle = (dma_addr_t)phys_addr;
|
|
|
|
/*
|
|
* Sanity check: make sure we dind't truncate
|
|
* the address. The only arches I can find that
|
|
* have 64-bit phys_addr_t but 32-bit dma_addr_t
|
|
* are certain non-highmem MIPS and x86
|
|
* configurations, but these configurations
|
|
* should never allocate physical pages above 32
|
|
* bits, so this is fine. Just in case, throw a
|
|
* warning and abort if we end up with an
|
|
* unrepresentable address.
|
|
*/
|
|
if (WARN_ON_ONCE(*dma_handle != phys_addr)) {
|
|
free_pages_exact(queue, PAGE_ALIGN(size));
|
|
return NULL;
|
|
}
|
|
}
|
|
return queue;
|
|
}
|
|
}
|
|
|
|
static void vring_free_queue(struct virtio_device *vdev, size_t size,
|
|
void *queue, dma_addr_t dma_handle)
|
|
{
|
|
if (vring_use_dma_api(vdev))
|
|
dma_free_coherent(vdev->dev.parent, size, queue, dma_handle);
|
|
else
|
|
free_pages_exact(queue, PAGE_ALIGN(size));
|
|
}
|
|
|
|
/*
|
|
* The DMA ops on various arches are rather gnarly right now, and
|
|
* making all of the arch DMA ops work on the vring device itself
|
|
* is a mess. For now, we use the parent device for DMA ops.
|
|
*/
|
|
static inline struct device *vring_dma_dev(const struct vring_virtqueue *vq)
|
|
{
|
|
return vq->vq.vdev->dev.parent;
|
|
}
|
|
|
|
/* Map one sg entry. */
|
|
static dma_addr_t vring_map_one_sg(const struct vring_virtqueue *vq,
|
|
struct scatterlist *sg,
|
|
enum dma_data_direction direction)
|
|
{
|
|
if (!vq->use_dma_api) {
|
|
/*
|
|
* If DMA is not used, KMSAN doesn't know that the scatterlist
|
|
* is initialized by the hardware. Explicitly check/unpoison it
|
|
* depending on the direction.
|
|
*/
|
|
kmsan_handle_dma(sg_page(sg), sg->offset, sg->length, direction);
|
|
return (dma_addr_t)sg_phys(sg);
|
|
}
|
|
|
|
/*
|
|
* We can't use dma_map_sg, because we don't use scatterlists in
|
|
* the way it expects (we don't guarantee that the scatterlist
|
|
* will exist for the lifetime of the mapping).
|
|
*/
|
|
return dma_map_page(vring_dma_dev(vq),
|
|
sg_page(sg), sg->offset, sg->length,
|
|
direction);
|
|
}
|
|
|
|
static dma_addr_t vring_map_single(const struct vring_virtqueue *vq,
|
|
void *cpu_addr, size_t size,
|
|
enum dma_data_direction direction)
|
|
{
|
|
if (!vq->use_dma_api)
|
|
return (dma_addr_t)virt_to_phys(cpu_addr);
|
|
|
|
return dma_map_single(vring_dma_dev(vq),
|
|
cpu_addr, size, direction);
|
|
}
|
|
|
|
static int vring_mapping_error(const struct vring_virtqueue *vq,
|
|
dma_addr_t addr)
|
|
{
|
|
if (!vq->use_dma_api)
|
|
return 0;
|
|
|
|
return dma_mapping_error(vring_dma_dev(vq), addr);
|
|
}
|
|
|
|
static void virtqueue_init(struct vring_virtqueue *vq, u32 num)
|
|
{
|
|
vq->vq.num_free = num;
|
|
|
|
if (vq->packed_ring)
|
|
vq->last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR);
|
|
else
|
|
vq->last_used_idx = 0;
|
|
|
|
vq->event_triggered = false;
|
|
vq->num_added = 0;
|
|
|
|
#ifdef DEBUG
|
|
vq->in_use = false;
|
|
vq->last_add_time_valid = false;
|
|
#endif
|
|
}
|
|
|
|
|
|
/*
|
|
* Split ring specific functions - *_split().
|
|
*/
|
|
|
|
static void vring_unmap_one_split_indirect(const struct vring_virtqueue *vq,
|
|
struct vring_desc *desc)
|
|
{
|
|
u16 flags;
|
|
|
|
if (!vq->use_dma_api)
|
|
return;
|
|
|
|
flags = virtio16_to_cpu(vq->vq.vdev, desc->flags);
|
|
|
|
dma_unmap_page(vring_dma_dev(vq),
|
|
virtio64_to_cpu(vq->vq.vdev, desc->addr),
|
|
virtio32_to_cpu(vq->vq.vdev, desc->len),
|
|
(flags & VRING_DESC_F_WRITE) ?
|
|
DMA_FROM_DEVICE : DMA_TO_DEVICE);
|
|
}
|
|
|
|
static unsigned int vring_unmap_one_split(const struct vring_virtqueue *vq,
|
|
unsigned int i)
|
|
{
|
|
struct vring_desc_extra *extra = vq->split.desc_extra;
|
|
u16 flags;
|
|
|
|
if (!vq->use_dma_api)
|
|
goto out;
|
|
|
|
flags = extra[i].flags;
|
|
|
|
if (flags & VRING_DESC_F_INDIRECT) {
|
|
dma_unmap_single(vring_dma_dev(vq),
|
|
extra[i].addr,
|
|
extra[i].len,
|
|
(flags & VRING_DESC_F_WRITE) ?
|
|
DMA_FROM_DEVICE : DMA_TO_DEVICE);
|
|
} else {
|
|
dma_unmap_page(vring_dma_dev(vq),
|
|
extra[i].addr,
|
|
extra[i].len,
|
|
(flags & VRING_DESC_F_WRITE) ?
|
|
DMA_FROM_DEVICE : DMA_TO_DEVICE);
|
|
}
|
|
|
|
out:
|
|
return extra[i].next;
|
|
}
|
|
|
|
static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq,
|
|
unsigned int total_sg,
|
|
gfp_t gfp)
|
|
{
|
|
struct vring_desc *desc;
|
|
unsigned int i;
|
|
|
|
/*
|
|
* We require lowmem mappings for the descriptors because
|
|
* otherwise virt_to_phys will give us bogus addresses in the
|
|
* virtqueue.
|
|
*/
|
|
gfp &= ~__GFP_HIGHMEM;
|
|
|
|
desc = kmalloc_array(total_sg, sizeof(struct vring_desc), gfp);
|
|
if (!desc)
|
|
return NULL;
|
|
|
|
for (i = 0; i < total_sg; i++)
|
|
desc[i].next = cpu_to_virtio16(_vq->vdev, i + 1);
|
|
return desc;
|
|
}
|
|
|
|
static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq,
|
|
struct vring_desc *desc,
|
|
unsigned int i,
|
|
dma_addr_t addr,
|
|
unsigned int len,
|
|
u16 flags,
|
|
bool indirect)
|
|
{
|
|
struct vring_virtqueue *vring = to_vvq(vq);
|
|
struct vring_desc_extra *extra = vring->split.desc_extra;
|
|
u16 next;
|
|
|
|
desc[i].flags = cpu_to_virtio16(vq->vdev, flags);
|
|
desc[i].addr = cpu_to_virtio64(vq->vdev, addr);
|
|
desc[i].len = cpu_to_virtio32(vq->vdev, len);
|
|
|
|
if (!indirect) {
|
|
next = extra[i].next;
|
|
desc[i].next = cpu_to_virtio16(vq->vdev, next);
|
|
|
|
extra[i].addr = addr;
|
|
extra[i].len = len;
|
|
extra[i].flags = flags;
|
|
} else
|
|
next = virtio16_to_cpu(vq->vdev, desc[i].next);
|
|
|
|
return next;
|
|
}
|
|
|
|
static inline int virtqueue_add_split(struct virtqueue *_vq,
|
|
struct scatterlist *sgs[],
|
|
unsigned int total_sg,
|
|
unsigned int out_sgs,
|
|
unsigned int in_sgs,
|
|
void *data,
|
|
void *ctx,
|
|
gfp_t gfp)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
struct scatterlist *sg;
|
|
struct vring_desc *desc;
|
|
unsigned int i, n, avail, descs_used, prev, err_idx;
|
|
int head;
|
|
bool indirect;
|
|
|
|
START_USE(vq);
|
|
|
|
BUG_ON(data == NULL);
|
|
BUG_ON(ctx && vq->indirect);
|
|
|
|
if (unlikely(vq->broken)) {
|
|
END_USE(vq);
|
|
return -EIO;
|
|
}
|
|
|
|
LAST_ADD_TIME_UPDATE(vq);
|
|
|
|
BUG_ON(total_sg == 0);
|
|
|
|
head = vq->free_head;
|
|
|
|
if (virtqueue_use_indirect(vq, total_sg))
|
|
desc = alloc_indirect_split(_vq, total_sg, gfp);
|
|
else {
|
|
desc = NULL;
|
|
WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect);
|
|
}
|
|
|
|
if (desc) {
|
|
/* Use a single buffer which doesn't continue */
|
|
indirect = true;
|
|
/* Set up rest to use this indirect table. */
|
|
i = 0;
|
|
descs_used = 1;
|
|
} else {
|
|
indirect = false;
|
|
desc = vq->split.vring.desc;
|
|
i = head;
|
|
descs_used = total_sg;
|
|
}
|
|
|
|
if (unlikely(vq->vq.num_free < descs_used)) {
|
|
pr_debug("Can't add buf len %i - avail = %i\n",
|
|
descs_used, vq->vq.num_free);
|
|
/* FIXME: for historical reasons, we force a notify here if
|
|
* there are outgoing parts to the buffer. Presumably the
|
|
* host should service the ring ASAP. */
|
|
if (out_sgs)
|
|
vq->notify(&vq->vq);
|
|
if (indirect)
|
|
kfree(desc);
|
|
END_USE(vq);
|
|
return -ENOSPC;
|
|
}
|
|
|
|
for (n = 0; n < out_sgs; n++) {
|
|
for (sg = sgs[n]; sg; sg = sg_next(sg)) {
|
|
dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_TO_DEVICE);
|
|
if (vring_mapping_error(vq, addr))
|
|
goto unmap_release;
|
|
|
|
prev = i;
|
|
/* Note that we trust indirect descriptor
|
|
* table since it use stream DMA mapping.
|
|
*/
|
|
i = virtqueue_add_desc_split(_vq, desc, i, addr, sg->length,
|
|
VRING_DESC_F_NEXT,
|
|
indirect);
|
|
}
|
|
}
|
|
for (; n < (out_sgs + in_sgs); n++) {
|
|
for (sg = sgs[n]; sg; sg = sg_next(sg)) {
|
|
dma_addr_t addr = vring_map_one_sg(vq, sg, DMA_FROM_DEVICE);
|
|
if (vring_mapping_error(vq, addr))
|
|
goto unmap_release;
|
|
|
|
prev = i;
|
|
/* Note that we trust indirect descriptor
|
|
* table since it use stream DMA mapping.
|
|
*/
|
|
i = virtqueue_add_desc_split(_vq, desc, i, addr,
|
|
sg->length,
|
|
VRING_DESC_F_NEXT |
|
|
VRING_DESC_F_WRITE,
|
|
indirect);
|
|
}
|
|
}
|
|
/* Last one doesn't continue. */
|
|
desc[prev].flags &= cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT);
|
|
if (!indirect && vq->use_dma_api)
|
|
vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=
|
|
~VRING_DESC_F_NEXT;
|
|
|
|
if (indirect) {
|
|
/* Now that the indirect table is filled in, map it. */
|
|
dma_addr_t addr = vring_map_single(
|
|
vq, desc, total_sg * sizeof(struct vring_desc),
|
|
DMA_TO_DEVICE);
|
|
if (vring_mapping_error(vq, addr))
|
|
goto unmap_release;
|
|
|
|
virtqueue_add_desc_split(_vq, vq->split.vring.desc,
|
|
head, addr,
|
|
total_sg * sizeof(struct vring_desc),
|
|
VRING_DESC_F_INDIRECT,
|
|
false);
|
|
}
|
|
|
|
/* We're using some buffers from the free list. */
|
|
vq->vq.num_free -= descs_used;
|
|
|
|
/* Update free pointer */
|
|
if (indirect)
|
|
vq->free_head = vq->split.desc_extra[head].next;
|
|
else
|
|
vq->free_head = i;
|
|
|
|
/* Store token and indirect buffer state. */
|
|
vq->split.desc_state[head].data = data;
|
|
if (indirect)
|
|
vq->split.desc_state[head].indir_desc = desc;
|
|
else
|
|
vq->split.desc_state[head].indir_desc = ctx;
|
|
|
|
/* Put entry in available array (but don't update avail->idx until they
|
|
* do sync). */
|
|
avail = vq->split.avail_idx_shadow & (vq->split.vring.num - 1);
|
|
vq->split.vring.avail->ring[avail] = cpu_to_virtio16(_vq->vdev, head);
|
|
|
|
/* Descriptors and available array need to be set before we expose the
|
|
* new available array entries. */
|
|
virtio_wmb(vq->weak_barriers);
|
|
vq->split.avail_idx_shadow++;
|
|
vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
|
|
vq->split.avail_idx_shadow);
|
|
vq->num_added++;
|
|
|
|
pr_debug("Added buffer head %i to %p\n", head, vq);
|
|
END_USE(vq);
|
|
|
|
/* This is very unlikely, but theoretically possible. Kick
|
|
* just in case. */
|
|
if (unlikely(vq->num_added == (1 << 16) - 1))
|
|
virtqueue_kick(_vq);
|
|
|
|
return 0;
|
|
|
|
unmap_release:
|
|
err_idx = i;
|
|
|
|
if (indirect)
|
|
i = 0;
|
|
else
|
|
i = head;
|
|
|
|
for (n = 0; n < total_sg; n++) {
|
|
if (i == err_idx)
|
|
break;
|
|
if (indirect) {
|
|
vring_unmap_one_split_indirect(vq, &desc[i]);
|
|
i = virtio16_to_cpu(_vq->vdev, desc[i].next);
|
|
} else
|
|
i = vring_unmap_one_split(vq, i);
|
|
}
|
|
|
|
if (indirect)
|
|
kfree(desc);
|
|
|
|
END_USE(vq);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
static bool virtqueue_kick_prepare_split(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
u16 new, old;
|
|
bool needs_kick;
|
|
|
|
START_USE(vq);
|
|
/* We need to expose available array entries before checking avail
|
|
* event. */
|
|
virtio_mb(vq->weak_barriers);
|
|
|
|
old = vq->split.avail_idx_shadow - vq->num_added;
|
|
new = vq->split.avail_idx_shadow;
|
|
vq->num_added = 0;
|
|
|
|
LAST_ADD_TIME_CHECK(vq);
|
|
LAST_ADD_TIME_INVALID(vq);
|
|
|
|
if (vq->event) {
|
|
needs_kick = vring_need_event(virtio16_to_cpu(_vq->vdev,
|
|
vring_avail_event(&vq->split.vring)),
|
|
new, old);
|
|
} else {
|
|
needs_kick = !(vq->split.vring.used->flags &
|
|
cpu_to_virtio16(_vq->vdev,
|
|
VRING_USED_F_NO_NOTIFY));
|
|
}
|
|
END_USE(vq);
|
|
return needs_kick;
|
|
}
|
|
|
|
static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,
|
|
void **ctx)
|
|
{
|
|
unsigned int i, j;
|
|
__virtio16 nextflag = cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT);
|
|
|
|
/* Clear data ptr. */
|
|
vq->split.desc_state[head].data = NULL;
|
|
|
|
/* Put back on free list: unmap first-level descriptors and find end */
|
|
i = head;
|
|
|
|
while (vq->split.vring.desc[i].flags & nextflag) {
|
|
vring_unmap_one_split(vq, i);
|
|
i = vq->split.desc_extra[i].next;
|
|
vq->vq.num_free++;
|
|
}
|
|
|
|
vring_unmap_one_split(vq, i);
|
|
vq->split.desc_extra[i].next = vq->free_head;
|
|
vq->free_head = head;
|
|
|
|
/* Plus final descriptor */
|
|
vq->vq.num_free++;
|
|
|
|
if (vq->indirect) {
|
|
struct vring_desc *indir_desc =
|
|
vq->split.desc_state[head].indir_desc;
|
|
u32 len;
|
|
|
|
/* Free the indirect table, if any, now that it's unmapped. */
|
|
if (!indir_desc)
|
|
return;
|
|
|
|
len = vq->split.desc_extra[head].len;
|
|
|
|
BUG_ON(!(vq->split.desc_extra[head].flags &
|
|
VRING_DESC_F_INDIRECT));
|
|
BUG_ON(len == 0 || len % sizeof(struct vring_desc));
|
|
|
|
for (j = 0; j < len / sizeof(struct vring_desc); j++)
|
|
vring_unmap_one_split_indirect(vq, &indir_desc[j]);
|
|
|
|
kfree(indir_desc);
|
|
vq->split.desc_state[head].indir_desc = NULL;
|
|
} else if (ctx) {
|
|
*ctx = vq->split.desc_state[head].indir_desc;
|
|
}
|
|
}
|
|
|
|
static inline bool more_used_split(const struct vring_virtqueue *vq)
|
|
{
|
|
return vq->last_used_idx != virtio16_to_cpu(vq->vq.vdev,
|
|
vq->split.vring.used->idx);
|
|
}
|
|
|
|
static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq,
|
|
unsigned int *len,
|
|
void **ctx)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
void *ret;
|
|
unsigned int i;
|
|
u16 last_used;
|
|
|
|
START_USE(vq);
|
|
|
|
if (unlikely(vq->broken)) {
|
|
END_USE(vq);
|
|
return NULL;
|
|
}
|
|
|
|
if (!more_used_split(vq)) {
|
|
pr_debug("No more buffers in queue\n");
|
|
END_USE(vq);
|
|
return NULL;
|
|
}
|
|
|
|
/* Only get used array entries after they have been exposed by host. */
|
|
virtio_rmb(vq->weak_barriers);
|
|
|
|
last_used = (vq->last_used_idx & (vq->split.vring.num - 1));
|
|
i = virtio32_to_cpu(_vq->vdev,
|
|
vq->split.vring.used->ring[last_used].id);
|
|
*len = virtio32_to_cpu(_vq->vdev,
|
|
vq->split.vring.used->ring[last_used].len);
|
|
|
|
if (unlikely(i >= vq->split.vring.num)) {
|
|
BAD_RING(vq, "id %u out of range\n", i);
|
|
return NULL;
|
|
}
|
|
if (unlikely(!vq->split.desc_state[i].data)) {
|
|
BAD_RING(vq, "id %u is not a head!\n", i);
|
|
return NULL;
|
|
}
|
|
|
|
/* detach_buf_split clears data, so grab it now. */
|
|
ret = vq->split.desc_state[i].data;
|
|
detach_buf_split(vq, i, ctx);
|
|
vq->last_used_idx++;
|
|
/* If we expect an interrupt for the next entry, tell host
|
|
* by writing event index and flush out the write before
|
|
* the read in the next get_buf call. */
|
|
if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
|
|
virtio_store_mb(vq->weak_barriers,
|
|
&vring_used_event(&vq->split.vring),
|
|
cpu_to_virtio16(_vq->vdev, vq->last_used_idx));
|
|
|
|
LAST_ADD_TIME_INVALID(vq);
|
|
|
|
END_USE(vq);
|
|
return ret;
|
|
}
|
|
|
|
static void virtqueue_disable_cb_split(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) {
|
|
vq->split.avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
|
|
|
|
/*
|
|
* If device triggered an event already it won't trigger one again:
|
|
* no need to disable.
|
|
*/
|
|
if (vq->event_triggered)
|
|
return;
|
|
|
|
if (vq->event)
|
|
/* TODO: this is a hack. Figure out a cleaner value to write. */
|
|
vring_used_event(&vq->split.vring) = 0x0;
|
|
else
|
|
vq->split.vring.avail->flags =
|
|
cpu_to_virtio16(_vq->vdev,
|
|
vq->split.avail_flags_shadow);
|
|
}
|
|
}
|
|
|
|
static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
u16 last_used_idx;
|
|
|
|
START_USE(vq);
|
|
|
|
/* We optimistically turn back on interrupts, then check if there was
|
|
* more to do. */
|
|
/* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
|
|
* either clear the flags bit or point the event index at the next
|
|
* entry. Always do both to keep code simple. */
|
|
if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
|
|
vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
|
|
if (!vq->event)
|
|
vq->split.vring.avail->flags =
|
|
cpu_to_virtio16(_vq->vdev,
|
|
vq->split.avail_flags_shadow);
|
|
}
|
|
vring_used_event(&vq->split.vring) = cpu_to_virtio16(_vq->vdev,
|
|
last_used_idx = vq->last_used_idx);
|
|
END_USE(vq);
|
|
return last_used_idx;
|
|
}
|
|
|
|
static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_used_idx)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
return (u16)last_used_idx != virtio16_to_cpu(_vq->vdev,
|
|
vq->split.vring.used->idx);
|
|
}
|
|
|
|
static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
u16 bufs;
|
|
|
|
START_USE(vq);
|
|
|
|
/* We optimistically turn back on interrupts, then check if there was
|
|
* more to do. */
|
|
/* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
|
|
* either clear the flags bit or point the event index at the next
|
|
* entry. Always update the event index to keep code simple. */
|
|
if (vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT) {
|
|
vq->split.avail_flags_shadow &= ~VRING_AVAIL_F_NO_INTERRUPT;
|
|
if (!vq->event)
|
|
vq->split.vring.avail->flags =
|
|
cpu_to_virtio16(_vq->vdev,
|
|
vq->split.avail_flags_shadow);
|
|
}
|
|
/* TODO: tune this threshold */
|
|
bufs = (u16)(vq->split.avail_idx_shadow - vq->last_used_idx) * 3 / 4;
|
|
|
|
virtio_store_mb(vq->weak_barriers,
|
|
&vring_used_event(&vq->split.vring),
|
|
cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs));
|
|
|
|
if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx)
|
|
- vq->last_used_idx) > bufs)) {
|
|
END_USE(vq);
|
|
return false;
|
|
}
|
|
|
|
END_USE(vq);
|
|
return true;
|
|
}
|
|
|
|
static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
unsigned int i;
|
|
void *buf;
|
|
|
|
START_USE(vq);
|
|
|
|
for (i = 0; i < vq->split.vring.num; i++) {
|
|
if (!vq->split.desc_state[i].data)
|
|
continue;
|
|
/* detach_buf_split clears data, so grab it now. */
|
|
buf = vq->split.desc_state[i].data;
|
|
detach_buf_split(vq, i, NULL);
|
|
vq->split.avail_idx_shadow--;
|
|
vq->split.vring.avail->idx = cpu_to_virtio16(_vq->vdev,
|
|
vq->split.avail_idx_shadow);
|
|
END_USE(vq);
|
|
return buf;
|
|
}
|
|
/* That should have freed everything. */
|
|
BUG_ON(vq->vq.num_free != vq->split.vring.num);
|
|
|
|
END_USE(vq);
|
|
return NULL;
|
|
}
|
|
|
|
static void virtqueue_vring_init_split(struct vring_virtqueue_split *vring_split,
|
|
struct vring_virtqueue *vq)
|
|
{
|
|
struct virtio_device *vdev;
|
|
|
|
vdev = vq->vq.vdev;
|
|
|
|
vring_split->avail_flags_shadow = 0;
|
|
vring_split->avail_idx_shadow = 0;
|
|
|
|
/* No callback? Tell other side not to bother us. */
|
|
if (!vq->vq.callback) {
|
|
vring_split->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
|
|
if (!vq->event)
|
|
vring_split->vring.avail->flags = cpu_to_virtio16(vdev,
|
|
vring_split->avail_flags_shadow);
|
|
}
|
|
}
|
|
|
|
static void virtqueue_reinit_split(struct vring_virtqueue *vq)
|
|
{
|
|
int num;
|
|
|
|
num = vq->split.vring.num;
|
|
|
|
vq->split.vring.avail->flags = 0;
|
|
vq->split.vring.avail->idx = 0;
|
|
|
|
/* reset avail event */
|
|
vq->split.vring.avail->ring[num] = 0;
|
|
|
|
vq->split.vring.used->flags = 0;
|
|
vq->split.vring.used->idx = 0;
|
|
|
|
/* reset used event */
|
|
*(__virtio16 *)&(vq->split.vring.used->ring[num]) = 0;
|
|
|
|
virtqueue_init(vq, num);
|
|
|
|
virtqueue_vring_init_split(&vq->split, vq);
|
|
}
|
|
|
|
static void virtqueue_vring_attach_split(struct vring_virtqueue *vq,
|
|
struct vring_virtqueue_split *vring_split)
|
|
{
|
|
vq->split = *vring_split;
|
|
|
|
/* Put everything in free lists. */
|
|
vq->free_head = 0;
|
|
}
|
|
|
|
static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_split)
|
|
{
|
|
struct vring_desc_state_split *state;
|
|
struct vring_desc_extra *extra;
|
|
u32 num = vring_split->vring.num;
|
|
|
|
state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL);
|
|
if (!state)
|
|
goto err_state;
|
|
|
|
extra = vring_alloc_desc_extra(num);
|
|
if (!extra)
|
|
goto err_extra;
|
|
|
|
memset(state, 0, num * sizeof(struct vring_desc_state_split));
|
|
|
|
vring_split->desc_state = state;
|
|
vring_split->desc_extra = extra;
|
|
return 0;
|
|
|
|
err_extra:
|
|
kfree(state);
|
|
err_state:
|
|
return -ENOMEM;
|
|
}
|
|
|
|
static void vring_free_split(struct vring_virtqueue_split *vring_split,
|
|
struct virtio_device *vdev)
|
|
{
|
|
vring_free_queue(vdev, vring_split->queue_size_in_bytes,
|
|
vring_split->vring.desc,
|
|
vring_split->queue_dma_addr);
|
|
|
|
kfree(vring_split->desc_state);
|
|
kfree(vring_split->desc_extra);
|
|
}
|
|
|
|
static int vring_alloc_queue_split(struct vring_virtqueue_split *vring_split,
|
|
struct virtio_device *vdev,
|
|
u32 num,
|
|
unsigned int vring_align,
|
|
bool may_reduce_num)
|
|
{
|
|
void *queue = NULL;
|
|
dma_addr_t dma_addr;
|
|
|
|
/* We assume num is a power of 2. */
|
|
if (num & (num - 1)) {
|
|
dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* TODO: allocate each queue chunk individually */
|
|
for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
|
|
queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
|
|
&dma_addr,
|
|
GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
|
|
if (queue)
|
|
break;
|
|
if (!may_reduce_num)
|
|
return -ENOMEM;
|
|
}
|
|
|
|
if (!num)
|
|
return -ENOMEM;
|
|
|
|
if (!queue) {
|
|
/* Try to get a single page. You are my only hope! */
|
|
queue = vring_alloc_queue(vdev, vring_size(num, vring_align),
|
|
&dma_addr, GFP_KERNEL | __GFP_ZERO);
|
|
}
|
|
if (!queue)
|
|
return -ENOMEM;
|
|
|
|
vring_init(&vring_split->vring, num, queue, vring_align);
|
|
|
|
vring_split->queue_dma_addr = dma_addr;
|
|
vring_split->queue_size_in_bytes = vring_size(num, vring_align);
|
|
|
|
vring_split->vring_align = vring_align;
|
|
vring_split->may_reduce_num = may_reduce_num;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct virtqueue *vring_create_virtqueue_split(
|
|
unsigned int index,
|
|
unsigned int num,
|
|
unsigned int vring_align,
|
|
struct virtio_device *vdev,
|
|
bool weak_barriers,
|
|
bool may_reduce_num,
|
|
bool context,
|
|
bool (*notify)(struct virtqueue *),
|
|
void (*callback)(struct virtqueue *),
|
|
const char *name)
|
|
{
|
|
struct vring_virtqueue_split vring_split = {};
|
|
struct virtqueue *vq;
|
|
int err;
|
|
|
|
err = vring_alloc_queue_split(&vring_split, vdev, num, vring_align,
|
|
may_reduce_num);
|
|
if (err)
|
|
return NULL;
|
|
|
|
vq = __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
|
|
context, notify, callback, name);
|
|
if (!vq) {
|
|
vring_free_split(&vring_split, vdev);
|
|
return NULL;
|
|
}
|
|
|
|
to_vvq(vq)->we_own_ring = true;
|
|
|
|
return vq;
|
|
}
|
|
|
|
static int virtqueue_resize_split(struct virtqueue *_vq, u32 num)
|
|
{
|
|
struct vring_virtqueue_split vring_split = {};
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
struct virtio_device *vdev = _vq->vdev;
|
|
int err;
|
|
|
|
err = vring_alloc_queue_split(&vring_split, vdev, num,
|
|
vq->split.vring_align,
|
|
vq->split.may_reduce_num);
|
|
if (err)
|
|
goto err;
|
|
|
|
err = vring_alloc_state_extra_split(&vring_split);
|
|
if (err)
|
|
goto err_state_extra;
|
|
|
|
vring_free(&vq->vq);
|
|
|
|
virtqueue_vring_init_split(&vring_split, vq);
|
|
|
|
virtqueue_init(vq, vring_split.vring.num);
|
|
virtqueue_vring_attach_split(vq, &vring_split);
|
|
|
|
return 0;
|
|
|
|
err_state_extra:
|
|
vring_free_split(&vring_split, vdev);
|
|
err:
|
|
virtqueue_reinit_split(vq);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
|
|
/*
|
|
* Packed ring specific functions - *_packed().
|
|
*/
|
|
static inline bool packed_used_wrap_counter(u16 last_used_idx)
|
|
{
|
|
return !!(last_used_idx & (1 << VRING_PACKED_EVENT_F_WRAP_CTR));
|
|
}
|
|
|
|
static inline u16 packed_last_used(u16 last_used_idx)
|
|
{
|
|
return last_used_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR));
|
|
}
|
|
|
|
static void vring_unmap_extra_packed(const struct vring_virtqueue *vq,
|
|
struct vring_desc_extra *extra)
|
|
{
|
|
u16 flags;
|
|
|
|
if (!vq->use_dma_api)
|
|
return;
|
|
|
|
flags = extra->flags;
|
|
|
|
if (flags & VRING_DESC_F_INDIRECT) {
|
|
dma_unmap_single(vring_dma_dev(vq),
|
|
extra->addr, extra->len,
|
|
(flags & VRING_DESC_F_WRITE) ?
|
|
DMA_FROM_DEVICE : DMA_TO_DEVICE);
|
|
} else {
|
|
dma_unmap_page(vring_dma_dev(vq),
|
|
extra->addr, extra->len,
|
|
(flags & VRING_DESC_F_WRITE) ?
|
|
DMA_FROM_DEVICE : DMA_TO_DEVICE);
|
|
}
|
|
}
|
|
|
|
static void vring_unmap_desc_packed(const struct vring_virtqueue *vq,
|
|
struct vring_packed_desc *desc)
|
|
{
|
|
u16 flags;
|
|
|
|
if (!vq->use_dma_api)
|
|
return;
|
|
|
|
flags = le16_to_cpu(desc->flags);
|
|
|
|
dma_unmap_page(vring_dma_dev(vq),
|
|
le64_to_cpu(desc->addr),
|
|
le32_to_cpu(desc->len),
|
|
(flags & VRING_DESC_F_WRITE) ?
|
|
DMA_FROM_DEVICE : DMA_TO_DEVICE);
|
|
}
|
|
|
|
static struct vring_packed_desc *alloc_indirect_packed(unsigned int total_sg,
|
|
gfp_t gfp)
|
|
{
|
|
struct vring_packed_desc *desc;
|
|
|
|
/*
|
|
* We require lowmem mappings for the descriptors because
|
|
* otherwise virt_to_phys will give us bogus addresses in the
|
|
* virtqueue.
|
|
*/
|
|
gfp &= ~__GFP_HIGHMEM;
|
|
|
|
desc = kmalloc_array(total_sg, sizeof(struct vring_packed_desc), gfp);
|
|
|
|
return desc;
|
|
}
|
|
|
|
static int virtqueue_add_indirect_packed(struct vring_virtqueue *vq,
|
|
struct scatterlist *sgs[],
|
|
unsigned int total_sg,
|
|
unsigned int out_sgs,
|
|
unsigned int in_sgs,
|
|
void *data,
|
|
gfp_t gfp)
|
|
{
|
|
struct vring_packed_desc *desc;
|
|
struct scatterlist *sg;
|
|
unsigned int i, n, err_idx;
|
|
u16 head, id;
|
|
dma_addr_t addr;
|
|
|
|
head = vq->packed.next_avail_idx;
|
|
desc = alloc_indirect_packed(total_sg, gfp);
|
|
if (!desc)
|
|
return -ENOMEM;
|
|
|
|
if (unlikely(vq->vq.num_free < 1)) {
|
|
pr_debug("Can't add buf len 1 - avail = 0\n");
|
|
kfree(desc);
|
|
END_USE(vq);
|
|
return -ENOSPC;
|
|
}
|
|
|
|
i = 0;
|
|
id = vq->free_head;
|
|
BUG_ON(id == vq->packed.vring.num);
|
|
|
|
for (n = 0; n < out_sgs + in_sgs; n++) {
|
|
for (sg = sgs[n]; sg; sg = sg_next(sg)) {
|
|
addr = vring_map_one_sg(vq, sg, n < out_sgs ?
|
|
DMA_TO_DEVICE : DMA_FROM_DEVICE);
|
|
if (vring_mapping_error(vq, addr))
|
|
goto unmap_release;
|
|
|
|
desc[i].flags = cpu_to_le16(n < out_sgs ?
|
|
0 : VRING_DESC_F_WRITE);
|
|
desc[i].addr = cpu_to_le64(addr);
|
|
desc[i].len = cpu_to_le32(sg->length);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
/* Now that the indirect table is filled in, map it. */
|
|
addr = vring_map_single(vq, desc,
|
|
total_sg * sizeof(struct vring_packed_desc),
|
|
DMA_TO_DEVICE);
|
|
if (vring_mapping_error(vq, addr))
|
|
goto unmap_release;
|
|
|
|
vq->packed.vring.desc[head].addr = cpu_to_le64(addr);
|
|
vq->packed.vring.desc[head].len = cpu_to_le32(total_sg *
|
|
sizeof(struct vring_packed_desc));
|
|
vq->packed.vring.desc[head].id = cpu_to_le16(id);
|
|
|
|
if (vq->use_dma_api) {
|
|
vq->packed.desc_extra[id].addr = addr;
|
|
vq->packed.desc_extra[id].len = total_sg *
|
|
sizeof(struct vring_packed_desc);
|
|
vq->packed.desc_extra[id].flags = VRING_DESC_F_INDIRECT |
|
|
vq->packed.avail_used_flags;
|
|
}
|
|
|
|
/*
|
|
* A driver MUST NOT make the first descriptor in the list
|
|
* available before all subsequent descriptors comprising
|
|
* the list are made available.
|
|
*/
|
|
virtio_wmb(vq->weak_barriers);
|
|
vq->packed.vring.desc[head].flags = cpu_to_le16(VRING_DESC_F_INDIRECT |
|
|
vq->packed.avail_used_flags);
|
|
|
|
/* We're using some buffers from the free list. */
|
|
vq->vq.num_free -= 1;
|
|
|
|
/* Update free pointer */
|
|
n = head + 1;
|
|
if (n >= vq->packed.vring.num) {
|
|
n = 0;
|
|
vq->packed.avail_wrap_counter ^= 1;
|
|
vq->packed.avail_used_flags ^=
|
|
1 << VRING_PACKED_DESC_F_AVAIL |
|
|
1 << VRING_PACKED_DESC_F_USED;
|
|
}
|
|
vq->packed.next_avail_idx = n;
|
|
vq->free_head = vq->packed.desc_extra[id].next;
|
|
|
|
/* Store token and indirect buffer state. */
|
|
vq->packed.desc_state[id].num = 1;
|
|
vq->packed.desc_state[id].data = data;
|
|
vq->packed.desc_state[id].indir_desc = desc;
|
|
vq->packed.desc_state[id].last = id;
|
|
|
|
vq->num_added += 1;
|
|
|
|
pr_debug("Added buffer head %i to %p\n", head, vq);
|
|
END_USE(vq);
|
|
|
|
return 0;
|
|
|
|
unmap_release:
|
|
err_idx = i;
|
|
|
|
for (i = 0; i < err_idx; i++)
|
|
vring_unmap_desc_packed(vq, &desc[i]);
|
|
|
|
kfree(desc);
|
|
|
|
END_USE(vq);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
static inline int virtqueue_add_packed(struct virtqueue *_vq,
|
|
struct scatterlist *sgs[],
|
|
unsigned int total_sg,
|
|
unsigned int out_sgs,
|
|
unsigned int in_sgs,
|
|
void *data,
|
|
void *ctx,
|
|
gfp_t gfp)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
struct vring_packed_desc *desc;
|
|
struct scatterlist *sg;
|
|
unsigned int i, n, c, descs_used, err_idx;
|
|
__le16 head_flags, flags;
|
|
u16 head, id, prev, curr, avail_used_flags;
|
|
int err;
|
|
|
|
START_USE(vq);
|
|
|
|
BUG_ON(data == NULL);
|
|
BUG_ON(ctx && vq->indirect);
|
|
|
|
if (unlikely(vq->broken)) {
|
|
END_USE(vq);
|
|
return -EIO;
|
|
}
|
|
|
|
LAST_ADD_TIME_UPDATE(vq);
|
|
|
|
BUG_ON(total_sg == 0);
|
|
|
|
if (virtqueue_use_indirect(vq, total_sg)) {
|
|
err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
|
|
in_sgs, data, gfp);
|
|
if (err != -ENOMEM) {
|
|
END_USE(vq);
|
|
return err;
|
|
}
|
|
|
|
/* fall back on direct */
|
|
}
|
|
|
|
head = vq->packed.next_avail_idx;
|
|
avail_used_flags = vq->packed.avail_used_flags;
|
|
|
|
WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect);
|
|
|
|
desc = vq->packed.vring.desc;
|
|
i = head;
|
|
descs_used = total_sg;
|
|
|
|
if (unlikely(vq->vq.num_free < descs_used)) {
|
|
pr_debug("Can't add buf len %i - avail = %i\n",
|
|
descs_used, vq->vq.num_free);
|
|
END_USE(vq);
|
|
return -ENOSPC;
|
|
}
|
|
|
|
id = vq->free_head;
|
|
BUG_ON(id == vq->packed.vring.num);
|
|
|
|
curr = id;
|
|
c = 0;
|
|
for (n = 0; n < out_sgs + in_sgs; n++) {
|
|
for (sg = sgs[n]; sg; sg = sg_next(sg)) {
|
|
dma_addr_t addr = vring_map_one_sg(vq, sg, n < out_sgs ?
|
|
DMA_TO_DEVICE : DMA_FROM_DEVICE);
|
|
if (vring_mapping_error(vq, addr))
|
|
goto unmap_release;
|
|
|
|
flags = cpu_to_le16(vq->packed.avail_used_flags |
|
|
(++c == total_sg ? 0 : VRING_DESC_F_NEXT) |
|
|
(n < out_sgs ? 0 : VRING_DESC_F_WRITE));
|
|
if (i == head)
|
|
head_flags = flags;
|
|
else
|
|
desc[i].flags = flags;
|
|
|
|
desc[i].addr = cpu_to_le64(addr);
|
|
desc[i].len = cpu_to_le32(sg->length);
|
|
desc[i].id = cpu_to_le16(id);
|
|
|
|
if (unlikely(vq->use_dma_api)) {
|
|
vq->packed.desc_extra[curr].addr = addr;
|
|
vq->packed.desc_extra[curr].len = sg->length;
|
|
vq->packed.desc_extra[curr].flags =
|
|
le16_to_cpu(flags);
|
|
}
|
|
prev = curr;
|
|
curr = vq->packed.desc_extra[curr].next;
|
|
|
|
if ((unlikely(++i >= vq->packed.vring.num))) {
|
|
i = 0;
|
|
vq->packed.avail_used_flags ^=
|
|
1 << VRING_PACKED_DESC_F_AVAIL |
|
|
1 << VRING_PACKED_DESC_F_USED;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (i <= head)
|
|
vq->packed.avail_wrap_counter ^= 1;
|
|
|
|
/* We're using some buffers from the free list. */
|
|
vq->vq.num_free -= descs_used;
|
|
|
|
/* Update free pointer */
|
|
vq->packed.next_avail_idx = i;
|
|
vq->free_head = curr;
|
|
|
|
/* Store token. */
|
|
vq->packed.desc_state[id].num = descs_used;
|
|
vq->packed.desc_state[id].data = data;
|
|
vq->packed.desc_state[id].indir_desc = ctx;
|
|
vq->packed.desc_state[id].last = prev;
|
|
|
|
/*
|
|
* A driver MUST NOT make the first descriptor in the list
|
|
* available before all subsequent descriptors comprising
|
|
* the list are made available.
|
|
*/
|
|
virtio_wmb(vq->weak_barriers);
|
|
vq->packed.vring.desc[head].flags = head_flags;
|
|
vq->num_added += descs_used;
|
|
|
|
pr_debug("Added buffer head %i to %p\n", head, vq);
|
|
END_USE(vq);
|
|
|
|
return 0;
|
|
|
|
unmap_release:
|
|
err_idx = i;
|
|
i = head;
|
|
curr = vq->free_head;
|
|
|
|
vq->packed.avail_used_flags = avail_used_flags;
|
|
|
|
for (n = 0; n < total_sg; n++) {
|
|
if (i == err_idx)
|
|
break;
|
|
vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]);
|
|
curr = vq->packed.desc_extra[curr].next;
|
|
i++;
|
|
if (i >= vq->packed.vring.num)
|
|
i = 0;
|
|
}
|
|
|
|
END_USE(vq);
|
|
return -EIO;
|
|
}
|
|
|
|
static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
u16 new, old, off_wrap, flags, wrap_counter, event_idx;
|
|
bool needs_kick;
|
|
union {
|
|
struct {
|
|
__le16 off_wrap;
|
|
__le16 flags;
|
|
};
|
|
u32 u32;
|
|
} snapshot;
|
|
|
|
START_USE(vq);
|
|
|
|
/*
|
|
* We need to expose the new flags value before checking notification
|
|
* suppressions.
|
|
*/
|
|
virtio_mb(vq->weak_barriers);
|
|
|
|
old = vq->packed.next_avail_idx - vq->num_added;
|
|
new = vq->packed.next_avail_idx;
|
|
vq->num_added = 0;
|
|
|
|
snapshot.u32 = *(u32 *)vq->packed.vring.device;
|
|
flags = le16_to_cpu(snapshot.flags);
|
|
|
|
LAST_ADD_TIME_CHECK(vq);
|
|
LAST_ADD_TIME_INVALID(vq);
|
|
|
|
if (flags != VRING_PACKED_EVENT_FLAG_DESC) {
|
|
needs_kick = (flags != VRING_PACKED_EVENT_FLAG_DISABLE);
|
|
goto out;
|
|
}
|
|
|
|
off_wrap = le16_to_cpu(snapshot.off_wrap);
|
|
|
|
wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
|
|
event_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
|
|
if (wrap_counter != vq->packed.avail_wrap_counter)
|
|
event_idx -= vq->packed.vring.num;
|
|
|
|
needs_kick = vring_need_event(event_idx, new, old);
|
|
out:
|
|
END_USE(vq);
|
|
return needs_kick;
|
|
}
|
|
|
|
static void detach_buf_packed(struct vring_virtqueue *vq,
|
|
unsigned int id, void **ctx)
|
|
{
|
|
struct vring_desc_state_packed *state = NULL;
|
|
struct vring_packed_desc *desc;
|
|
unsigned int i, curr;
|
|
|
|
state = &vq->packed.desc_state[id];
|
|
|
|
/* Clear data ptr. */
|
|
state->data = NULL;
|
|
|
|
vq->packed.desc_extra[state->last].next = vq->free_head;
|
|
vq->free_head = id;
|
|
vq->vq.num_free += state->num;
|
|
|
|
if (unlikely(vq->use_dma_api)) {
|
|
curr = id;
|
|
for (i = 0; i < state->num; i++) {
|
|
vring_unmap_extra_packed(vq,
|
|
&vq->packed.desc_extra[curr]);
|
|
curr = vq->packed.desc_extra[curr].next;
|
|
}
|
|
}
|
|
|
|
if (vq->indirect) {
|
|
u32 len;
|
|
|
|
/* Free the indirect table, if any, now that it's unmapped. */
|
|
desc = state->indir_desc;
|
|
if (!desc)
|
|
return;
|
|
|
|
if (vq->use_dma_api) {
|
|
len = vq->packed.desc_extra[id].len;
|
|
for (i = 0; i < len / sizeof(struct vring_packed_desc);
|
|
i++)
|
|
vring_unmap_desc_packed(vq, &desc[i]);
|
|
}
|
|
kfree(desc);
|
|
state->indir_desc = NULL;
|
|
} else if (ctx) {
|
|
*ctx = state->indir_desc;
|
|
}
|
|
}
|
|
|
|
static inline bool is_used_desc_packed(const struct vring_virtqueue *vq,
|
|
u16 idx, bool used_wrap_counter)
|
|
{
|
|
bool avail, used;
|
|
u16 flags;
|
|
|
|
flags = le16_to_cpu(vq->packed.vring.desc[idx].flags);
|
|
avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
|
|
used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
|
|
|
|
return avail == used && used == used_wrap_counter;
|
|
}
|
|
|
|
static inline bool more_used_packed(const struct vring_virtqueue *vq)
|
|
{
|
|
u16 last_used;
|
|
u16 last_used_idx;
|
|
bool used_wrap_counter;
|
|
|
|
last_used_idx = READ_ONCE(vq->last_used_idx);
|
|
last_used = packed_last_used(last_used_idx);
|
|
used_wrap_counter = packed_used_wrap_counter(last_used_idx);
|
|
return is_used_desc_packed(vq, last_used, used_wrap_counter);
|
|
}
|
|
|
|
static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq,
|
|
unsigned int *len,
|
|
void **ctx)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
u16 last_used, id, last_used_idx;
|
|
bool used_wrap_counter;
|
|
void *ret;
|
|
|
|
START_USE(vq);
|
|
|
|
if (unlikely(vq->broken)) {
|
|
END_USE(vq);
|
|
return NULL;
|
|
}
|
|
|
|
if (!more_used_packed(vq)) {
|
|
pr_debug("No more buffers in queue\n");
|
|
END_USE(vq);
|
|
return NULL;
|
|
}
|
|
|
|
/* Only get used elements after they have been exposed by host. */
|
|
virtio_rmb(vq->weak_barriers);
|
|
|
|
last_used_idx = READ_ONCE(vq->last_used_idx);
|
|
used_wrap_counter = packed_used_wrap_counter(last_used_idx);
|
|
last_used = packed_last_used(last_used_idx);
|
|
id = le16_to_cpu(vq->packed.vring.desc[last_used].id);
|
|
*len = le32_to_cpu(vq->packed.vring.desc[last_used].len);
|
|
|
|
if (unlikely(id >= vq->packed.vring.num)) {
|
|
BAD_RING(vq, "id %u out of range\n", id);
|
|
return NULL;
|
|
}
|
|
if (unlikely(!vq->packed.desc_state[id].data)) {
|
|
BAD_RING(vq, "id %u is not a head!\n", id);
|
|
return NULL;
|
|
}
|
|
|
|
/* detach_buf_packed clears data, so grab it now. */
|
|
ret = vq->packed.desc_state[id].data;
|
|
detach_buf_packed(vq, id, ctx);
|
|
|
|
last_used += vq->packed.desc_state[id].num;
|
|
if (unlikely(last_used >= vq->packed.vring.num)) {
|
|
last_used -= vq->packed.vring.num;
|
|
used_wrap_counter ^= 1;
|
|
}
|
|
|
|
last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
|
|
WRITE_ONCE(vq->last_used_idx, last_used);
|
|
|
|
/*
|
|
* If we expect an interrupt for the next entry, tell host
|
|
* by writing event index and flush out the write before
|
|
* the read in the next get_buf call.
|
|
*/
|
|
if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DESC)
|
|
virtio_store_mb(vq->weak_barriers,
|
|
&vq->packed.vring.driver->off_wrap,
|
|
cpu_to_le16(vq->last_used_idx));
|
|
|
|
LAST_ADD_TIME_INVALID(vq);
|
|
|
|
END_USE(vq);
|
|
return ret;
|
|
}
|
|
|
|
static void virtqueue_disable_cb_packed(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (vq->packed.event_flags_shadow != VRING_PACKED_EVENT_FLAG_DISABLE) {
|
|
vq->packed.event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
|
|
|
|
/*
|
|
* If device triggered an event already it won't trigger one again:
|
|
* no need to disable.
|
|
*/
|
|
if (vq->event_triggered)
|
|
return;
|
|
|
|
vq->packed.vring.driver->flags =
|
|
cpu_to_le16(vq->packed.event_flags_shadow);
|
|
}
|
|
}
|
|
|
|
static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
START_USE(vq);
|
|
|
|
/*
|
|
* We optimistically turn back on interrupts, then check if there was
|
|
* more to do.
|
|
*/
|
|
|
|
if (vq->event) {
|
|
vq->packed.vring.driver->off_wrap =
|
|
cpu_to_le16(vq->last_used_idx);
|
|
/*
|
|
* We need to update event offset and event wrap
|
|
* counter first before updating event flags.
|
|
*/
|
|
virtio_wmb(vq->weak_barriers);
|
|
}
|
|
|
|
if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
|
|
vq->packed.event_flags_shadow = vq->event ?
|
|
VRING_PACKED_EVENT_FLAG_DESC :
|
|
VRING_PACKED_EVENT_FLAG_ENABLE;
|
|
vq->packed.vring.driver->flags =
|
|
cpu_to_le16(vq->packed.event_flags_shadow);
|
|
}
|
|
|
|
END_USE(vq);
|
|
return vq->last_used_idx;
|
|
}
|
|
|
|
static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
bool wrap_counter;
|
|
u16 used_idx;
|
|
|
|
wrap_counter = off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR;
|
|
used_idx = off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR);
|
|
|
|
return is_used_desc_packed(vq, used_idx, wrap_counter);
|
|
}
|
|
|
|
static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
u16 used_idx, wrap_counter, last_used_idx;
|
|
u16 bufs;
|
|
|
|
START_USE(vq);
|
|
|
|
/*
|
|
* We optimistically turn back on interrupts, then check if there was
|
|
* more to do.
|
|
*/
|
|
|
|
if (vq->event) {
|
|
/* TODO: tune this threshold */
|
|
bufs = (vq->packed.vring.num - vq->vq.num_free) * 3 / 4;
|
|
last_used_idx = READ_ONCE(vq->last_used_idx);
|
|
wrap_counter = packed_used_wrap_counter(last_used_idx);
|
|
|
|
used_idx = packed_last_used(last_used_idx) + bufs;
|
|
if (used_idx >= vq->packed.vring.num) {
|
|
used_idx -= vq->packed.vring.num;
|
|
wrap_counter ^= 1;
|
|
}
|
|
|
|
vq->packed.vring.driver->off_wrap = cpu_to_le16(used_idx |
|
|
(wrap_counter << VRING_PACKED_EVENT_F_WRAP_CTR));
|
|
|
|
/*
|
|
* We need to update event offset and event wrap
|
|
* counter first before updating event flags.
|
|
*/
|
|
virtio_wmb(vq->weak_barriers);
|
|
}
|
|
|
|
if (vq->packed.event_flags_shadow == VRING_PACKED_EVENT_FLAG_DISABLE) {
|
|
vq->packed.event_flags_shadow = vq->event ?
|
|
VRING_PACKED_EVENT_FLAG_DESC :
|
|
VRING_PACKED_EVENT_FLAG_ENABLE;
|
|
vq->packed.vring.driver->flags =
|
|
cpu_to_le16(vq->packed.event_flags_shadow);
|
|
}
|
|
|
|
/*
|
|
* We need to update event suppression structure first
|
|
* before re-checking for more used buffers.
|
|
*/
|
|
virtio_mb(vq->weak_barriers);
|
|
|
|
last_used_idx = READ_ONCE(vq->last_used_idx);
|
|
wrap_counter = packed_used_wrap_counter(last_used_idx);
|
|
used_idx = packed_last_used(last_used_idx);
|
|
if (is_used_desc_packed(vq, used_idx, wrap_counter)) {
|
|
END_USE(vq);
|
|
return false;
|
|
}
|
|
|
|
END_USE(vq);
|
|
return true;
|
|
}
|
|
|
|
static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
unsigned int i;
|
|
void *buf;
|
|
|
|
START_USE(vq);
|
|
|
|
for (i = 0; i < vq->packed.vring.num; i++) {
|
|
if (!vq->packed.desc_state[i].data)
|
|
continue;
|
|
/* detach_buf clears data, so grab it now. */
|
|
buf = vq->packed.desc_state[i].data;
|
|
detach_buf_packed(vq, i, NULL);
|
|
END_USE(vq);
|
|
return buf;
|
|
}
|
|
/* That should have freed everything. */
|
|
BUG_ON(vq->vq.num_free != vq->packed.vring.num);
|
|
|
|
END_USE(vq);
|
|
return NULL;
|
|
}
|
|
|
|
static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num)
|
|
{
|
|
struct vring_desc_extra *desc_extra;
|
|
unsigned int i;
|
|
|
|
desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
|
|
GFP_KERNEL);
|
|
if (!desc_extra)
|
|
return NULL;
|
|
|
|
memset(desc_extra, 0, num * sizeof(struct vring_desc_extra));
|
|
|
|
for (i = 0; i < num - 1; i++)
|
|
desc_extra[i].next = i + 1;
|
|
|
|
return desc_extra;
|
|
}
|
|
|
|
static void vring_free_packed(struct vring_virtqueue_packed *vring_packed,
|
|
struct virtio_device *vdev)
|
|
{
|
|
if (vring_packed->vring.desc)
|
|
vring_free_queue(vdev, vring_packed->ring_size_in_bytes,
|
|
vring_packed->vring.desc,
|
|
vring_packed->ring_dma_addr);
|
|
|
|
if (vring_packed->vring.driver)
|
|
vring_free_queue(vdev, vring_packed->event_size_in_bytes,
|
|
vring_packed->vring.driver,
|
|
vring_packed->driver_event_dma_addr);
|
|
|
|
if (vring_packed->vring.device)
|
|
vring_free_queue(vdev, vring_packed->event_size_in_bytes,
|
|
vring_packed->vring.device,
|
|
vring_packed->device_event_dma_addr);
|
|
|
|
kfree(vring_packed->desc_state);
|
|
kfree(vring_packed->desc_extra);
|
|
}
|
|
|
|
static int vring_alloc_queue_packed(struct vring_virtqueue_packed *vring_packed,
|
|
struct virtio_device *vdev,
|
|
u32 num)
|
|
{
|
|
struct vring_packed_desc *ring;
|
|
struct vring_packed_desc_event *driver, *device;
|
|
dma_addr_t ring_dma_addr, driver_event_dma_addr, device_event_dma_addr;
|
|
size_t ring_size_in_bytes, event_size_in_bytes;
|
|
|
|
ring_size_in_bytes = num * sizeof(struct vring_packed_desc);
|
|
|
|
ring = vring_alloc_queue(vdev, ring_size_in_bytes,
|
|
&ring_dma_addr,
|
|
GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
|
|
if (!ring)
|
|
goto err;
|
|
|
|
vring_packed->vring.desc = ring;
|
|
vring_packed->ring_dma_addr = ring_dma_addr;
|
|
vring_packed->ring_size_in_bytes = ring_size_in_bytes;
|
|
|
|
event_size_in_bytes = sizeof(struct vring_packed_desc_event);
|
|
|
|
driver = vring_alloc_queue(vdev, event_size_in_bytes,
|
|
&driver_event_dma_addr,
|
|
GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
|
|
if (!driver)
|
|
goto err;
|
|
|
|
vring_packed->vring.driver = driver;
|
|
vring_packed->event_size_in_bytes = event_size_in_bytes;
|
|
vring_packed->driver_event_dma_addr = driver_event_dma_addr;
|
|
|
|
device = vring_alloc_queue(vdev, event_size_in_bytes,
|
|
&device_event_dma_addr,
|
|
GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
|
|
if (!device)
|
|
goto err;
|
|
|
|
vring_packed->vring.device = device;
|
|
vring_packed->device_event_dma_addr = device_event_dma_addr;
|
|
|
|
vring_packed->vring.num = num;
|
|
|
|
return 0;
|
|
|
|
err:
|
|
vring_free_packed(vring_packed, vdev);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
static int vring_alloc_state_extra_packed(struct vring_virtqueue_packed *vring_packed)
|
|
{
|
|
struct vring_desc_state_packed *state;
|
|
struct vring_desc_extra *extra;
|
|
u32 num = vring_packed->vring.num;
|
|
|
|
state = kmalloc_array(num, sizeof(struct vring_desc_state_packed), GFP_KERNEL);
|
|
if (!state)
|
|
goto err_desc_state;
|
|
|
|
memset(state, 0, num * sizeof(struct vring_desc_state_packed));
|
|
|
|
extra = vring_alloc_desc_extra(num);
|
|
if (!extra)
|
|
goto err_desc_extra;
|
|
|
|
vring_packed->desc_state = state;
|
|
vring_packed->desc_extra = extra;
|
|
|
|
return 0;
|
|
|
|
err_desc_extra:
|
|
kfree(state);
|
|
err_desc_state:
|
|
return -ENOMEM;
|
|
}
|
|
|
|
static void virtqueue_vring_init_packed(struct vring_virtqueue_packed *vring_packed,
|
|
bool callback)
|
|
{
|
|
vring_packed->next_avail_idx = 0;
|
|
vring_packed->avail_wrap_counter = 1;
|
|
vring_packed->event_flags_shadow = 0;
|
|
vring_packed->avail_used_flags = 1 << VRING_PACKED_DESC_F_AVAIL;
|
|
|
|
/* No callback? Tell other side not to bother us. */
|
|
if (!callback) {
|
|
vring_packed->event_flags_shadow = VRING_PACKED_EVENT_FLAG_DISABLE;
|
|
vring_packed->vring.driver->flags =
|
|
cpu_to_le16(vring_packed->event_flags_shadow);
|
|
}
|
|
}
|
|
|
|
static void virtqueue_vring_attach_packed(struct vring_virtqueue *vq,
|
|
struct vring_virtqueue_packed *vring_packed)
|
|
{
|
|
vq->packed = *vring_packed;
|
|
|
|
/* Put everything in free lists. */
|
|
vq->free_head = 0;
|
|
}
|
|
|
|
static void virtqueue_reinit_packed(struct vring_virtqueue *vq)
|
|
{
|
|
memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes);
|
|
memset(vq->packed.vring.driver, 0, vq->packed.event_size_in_bytes);
|
|
|
|
/* we need to reset the desc.flags. For more, see is_used_desc_packed() */
|
|
memset(vq->packed.vring.desc, 0, vq->packed.ring_size_in_bytes);
|
|
|
|
virtqueue_init(vq, vq->packed.vring.num);
|
|
virtqueue_vring_init_packed(&vq->packed, !!vq->vq.callback);
|
|
}
|
|
|
|
static struct virtqueue *vring_create_virtqueue_packed(
|
|
unsigned int index,
|
|
unsigned int num,
|
|
unsigned int vring_align,
|
|
struct virtio_device *vdev,
|
|
bool weak_barriers,
|
|
bool may_reduce_num,
|
|
bool context,
|
|
bool (*notify)(struct virtqueue *),
|
|
void (*callback)(struct virtqueue *),
|
|
const char *name)
|
|
{
|
|
struct vring_virtqueue_packed vring_packed = {};
|
|
struct vring_virtqueue *vq;
|
|
int err;
|
|
|
|
if (vring_alloc_queue_packed(&vring_packed, vdev, num))
|
|
goto err_ring;
|
|
|
|
vq = kmalloc(sizeof(*vq), GFP_KERNEL);
|
|
if (!vq)
|
|
goto err_vq;
|
|
|
|
vq->vq.callback = callback;
|
|
vq->vq.vdev = vdev;
|
|
vq->vq.name = name;
|
|
vq->vq.index = index;
|
|
vq->vq.reset = false;
|
|
vq->we_own_ring = true;
|
|
vq->notify = notify;
|
|
vq->weak_barriers = weak_barriers;
|
|
#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
|
|
vq->broken = true;
|
|
#else
|
|
vq->broken = false;
|
|
#endif
|
|
vq->packed_ring = true;
|
|
vq->use_dma_api = vring_use_dma_api(vdev);
|
|
|
|
vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
|
|
!context;
|
|
vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
|
|
vq->weak_barriers = false;
|
|
|
|
err = vring_alloc_state_extra_packed(&vring_packed);
|
|
if (err)
|
|
goto err_state_extra;
|
|
|
|
virtqueue_vring_init_packed(&vring_packed, !!callback);
|
|
|
|
virtqueue_init(vq, num);
|
|
virtqueue_vring_attach_packed(vq, &vring_packed);
|
|
|
|
spin_lock(&vdev->vqs_list_lock);
|
|
list_add_tail(&vq->vq.list, &vdev->vqs);
|
|
spin_unlock(&vdev->vqs_list_lock);
|
|
return &vq->vq;
|
|
|
|
err_state_extra:
|
|
kfree(vq);
|
|
err_vq:
|
|
vring_free_packed(&vring_packed, vdev);
|
|
err_ring:
|
|
return NULL;
|
|
}
|
|
|
|
static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num)
|
|
{
|
|
struct vring_virtqueue_packed vring_packed = {};
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
struct virtio_device *vdev = _vq->vdev;
|
|
int err;
|
|
|
|
if (vring_alloc_queue_packed(&vring_packed, vdev, num))
|
|
goto err_ring;
|
|
|
|
err = vring_alloc_state_extra_packed(&vring_packed);
|
|
if (err)
|
|
goto err_state_extra;
|
|
|
|
vring_free(&vq->vq);
|
|
|
|
virtqueue_vring_init_packed(&vring_packed, !!vq->vq.callback);
|
|
|
|
virtqueue_init(vq, vring_packed.vring.num);
|
|
virtqueue_vring_attach_packed(vq, &vring_packed);
|
|
|
|
return 0;
|
|
|
|
err_state_extra:
|
|
vring_free_packed(&vring_packed, vdev);
|
|
err_ring:
|
|
virtqueue_reinit_packed(vq);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
|
|
/*
|
|
* Generic functions and exported symbols.
|
|
*/
|
|
|
|
static inline int virtqueue_add(struct virtqueue *_vq,
|
|
struct scatterlist *sgs[],
|
|
unsigned int total_sg,
|
|
unsigned int out_sgs,
|
|
unsigned int in_sgs,
|
|
void *data,
|
|
void *ctx,
|
|
gfp_t gfp)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg,
|
|
out_sgs, in_sgs, data, ctx, gfp) :
|
|
virtqueue_add_split(_vq, sgs, total_sg,
|
|
out_sgs, in_sgs, data, ctx, gfp);
|
|
}
|
|
|
|
/**
|
|
* virtqueue_add_sgs - expose buffers to other end
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
* @sgs: array of terminated scatterlists.
|
|
* @out_sgs: the number of scatterlists readable by other side
|
|
* @in_sgs: the number of scatterlists which are writable (after readable ones)
|
|
* @data: the token identifying the buffer.
|
|
* @gfp: how to do memory allocations (if necessary).
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue operations
|
|
* at the same time (except where noted).
|
|
*
|
|
* Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
|
|
*/
|
|
int virtqueue_add_sgs(struct virtqueue *_vq,
|
|
struct scatterlist *sgs[],
|
|
unsigned int out_sgs,
|
|
unsigned int in_sgs,
|
|
void *data,
|
|
gfp_t gfp)
|
|
{
|
|
unsigned int i, total_sg = 0;
|
|
|
|
/* Count them first. */
|
|
for (i = 0; i < out_sgs + in_sgs; i++) {
|
|
struct scatterlist *sg;
|
|
|
|
for (sg = sgs[i]; sg; sg = sg_next(sg))
|
|
total_sg++;
|
|
}
|
|
return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
|
|
data, NULL, gfp);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
|
|
|
|
/**
|
|
* virtqueue_add_outbuf - expose output buffers to other end
|
|
* @vq: the struct virtqueue we're talking about.
|
|
* @sg: scatterlist (must be well-formed and terminated!)
|
|
* @num: the number of entries in @sg readable by other side
|
|
* @data: the token identifying the buffer.
|
|
* @gfp: how to do memory allocations (if necessary).
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue operations
|
|
* at the same time (except where noted).
|
|
*
|
|
* Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
|
|
*/
|
|
int virtqueue_add_outbuf(struct virtqueue *vq,
|
|
struct scatterlist *sg, unsigned int num,
|
|
void *data,
|
|
gfp_t gfp)
|
|
{
|
|
return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, gfp);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
|
|
|
|
/**
|
|
* virtqueue_add_inbuf - expose input buffers to other end
|
|
* @vq: the struct virtqueue we're talking about.
|
|
* @sg: scatterlist (must be well-formed and terminated!)
|
|
* @num: the number of entries in @sg writable by other side
|
|
* @data: the token identifying the buffer.
|
|
* @gfp: how to do memory allocations (if necessary).
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue operations
|
|
* at the same time (except where noted).
|
|
*
|
|
* Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
|
|
*/
|
|
int virtqueue_add_inbuf(struct virtqueue *vq,
|
|
struct scatterlist *sg, unsigned int num,
|
|
void *data,
|
|
gfp_t gfp)
|
|
{
|
|
return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, gfp);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
|
|
|
|
/**
|
|
* virtqueue_add_inbuf_ctx - expose input buffers to other end
|
|
* @vq: the struct virtqueue we're talking about.
|
|
* @sg: scatterlist (must be well-formed and terminated!)
|
|
* @num: the number of entries in @sg writable by other side
|
|
* @data: the token identifying the buffer.
|
|
* @ctx: extra context for the token
|
|
* @gfp: how to do memory allocations (if necessary).
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue operations
|
|
* at the same time (except where noted).
|
|
*
|
|
* Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
|
|
*/
|
|
int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
|
|
struct scatterlist *sg, unsigned int num,
|
|
void *data,
|
|
void *ctx,
|
|
gfp_t gfp)
|
|
{
|
|
return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, gfp);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
|
|
|
|
/**
|
|
* virtqueue_kick_prepare - first half of split virtqueue_kick call.
|
|
* @_vq: the struct virtqueue
|
|
*
|
|
* Instead of virtqueue_kick(), you can do:
|
|
* if (virtqueue_kick_prepare(vq))
|
|
* virtqueue_notify(vq);
|
|
*
|
|
* This is sometimes useful because the virtqueue_kick_prepare() needs
|
|
* to be serialized, but the actual virtqueue_notify() call does not.
|
|
*/
|
|
bool virtqueue_kick_prepare(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) :
|
|
virtqueue_kick_prepare_split(_vq);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
|
|
|
|
/**
|
|
* virtqueue_notify - second half of split virtqueue_kick call.
|
|
* @_vq: the struct virtqueue
|
|
*
|
|
* This does not need to be serialized.
|
|
*
|
|
* Returns false if host notify failed or queue is broken, otherwise true.
|
|
*/
|
|
bool virtqueue_notify(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (unlikely(vq->broken))
|
|
return false;
|
|
|
|
/* Prod other side to tell it about changes. */
|
|
if (!vq->notify(_vq)) {
|
|
vq->broken = true;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_notify);
|
|
|
|
/**
|
|
* virtqueue_kick - update after add_buf
|
|
* @vq: the struct virtqueue
|
|
*
|
|
* After one or more virtqueue_add_* calls, invoke this to kick
|
|
* the other side.
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue
|
|
* operations at the same time (except where noted).
|
|
*
|
|
* Returns false if kick failed, otherwise true.
|
|
*/
|
|
bool virtqueue_kick(struct virtqueue *vq)
|
|
{
|
|
if (virtqueue_kick_prepare(vq))
|
|
return virtqueue_notify(vq);
|
|
return true;
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_kick);
|
|
|
|
/**
|
|
* virtqueue_get_buf_ctx - get the next used buffer
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
* @len: the length written into the buffer
|
|
* @ctx: extra context for the token
|
|
*
|
|
* If the device wrote data into the buffer, @len will be set to the
|
|
* amount written. This means you don't need to clear the buffer
|
|
* beforehand to ensure there's no data leakage in the case of short
|
|
* writes.
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue
|
|
* operations at the same time (except where noted).
|
|
*
|
|
* Returns NULL if there are no used buffers, or the "data" token
|
|
* handed to virtqueue_add_*().
|
|
*/
|
|
void *virtqueue_get_buf_ctx(struct virtqueue *_vq, unsigned int *len,
|
|
void **ctx)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) :
|
|
virtqueue_get_buf_ctx_split(_vq, len, ctx);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx);
|
|
|
|
void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
|
|
{
|
|
return virtqueue_get_buf_ctx(_vq, len, NULL);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_get_buf);
|
|
/**
|
|
* virtqueue_disable_cb - disable callbacks
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
*
|
|
* Note that this is not necessarily synchronous, hence unreliable and only
|
|
* useful as an optimization.
|
|
*
|
|
* Unlike other operations, this need not be serialized.
|
|
*/
|
|
void virtqueue_disable_cb(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (vq->packed_ring)
|
|
virtqueue_disable_cb_packed(_vq);
|
|
else
|
|
virtqueue_disable_cb_split(_vq);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
|
|
|
|
/**
|
|
* virtqueue_enable_cb_prepare - restart callbacks after disable_cb
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
*
|
|
* This re-enables callbacks; it returns current queue state
|
|
* in an opaque unsigned value. This value should be later tested by
|
|
* virtqueue_poll, to detect a possible race between the driver checking for
|
|
* more work, and enabling callbacks.
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue
|
|
* operations at the same time (except where noted).
|
|
*/
|
|
unsigned int virtqueue_enable_cb_prepare(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (vq->event_triggered)
|
|
vq->event_triggered = false;
|
|
|
|
return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) :
|
|
virtqueue_enable_cb_prepare_split(_vq);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
|
|
|
|
/**
|
|
* virtqueue_poll - query pending used buffers
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
* @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
|
|
*
|
|
* Returns "true" if there are pending used buffers in the queue.
|
|
*
|
|
* This does not need to be serialized.
|
|
*/
|
|
bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (unlikely(vq->broken))
|
|
return false;
|
|
|
|
virtio_mb(vq->weak_barriers);
|
|
return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) :
|
|
virtqueue_poll_split(_vq, last_used_idx);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_poll);
|
|
|
|
/**
|
|
* virtqueue_enable_cb - restart callbacks after disable_cb.
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
*
|
|
* This re-enables callbacks; it returns "false" if there are pending
|
|
* buffers in the queue, to detect a possible race between the driver
|
|
* checking for more work, and enabling callbacks.
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue
|
|
* operations at the same time (except where noted).
|
|
*/
|
|
bool virtqueue_enable_cb(struct virtqueue *_vq)
|
|
{
|
|
unsigned int last_used_idx = virtqueue_enable_cb_prepare(_vq);
|
|
|
|
return !virtqueue_poll(_vq, last_used_idx);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
|
|
|
|
/**
|
|
* virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
*
|
|
* This re-enables callbacks but hints to the other side to delay
|
|
* interrupts until most of the available buffers have been processed;
|
|
* it returns "false" if there are many pending buffers in the queue,
|
|
* to detect a possible race between the driver checking for more work,
|
|
* and enabling callbacks.
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue
|
|
* operations at the same time (except where noted).
|
|
*/
|
|
bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (vq->event_triggered)
|
|
vq->event_triggered = false;
|
|
|
|
return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) :
|
|
virtqueue_enable_cb_delayed_split(_vq);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
|
|
|
|
/**
|
|
* virtqueue_detach_unused_buf - detach first unused buffer
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
*
|
|
* Returns NULL or the "data" token handed to virtqueue_add_*().
|
|
* This is not valid on an active queue; it is useful for device
|
|
* shutdown or the reset queue.
|
|
*/
|
|
void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) :
|
|
virtqueue_detach_unused_buf_split(_vq);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
|
|
|
|
static inline bool more_used(const struct vring_virtqueue *vq)
|
|
{
|
|
return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq);
|
|
}
|
|
|
|
/**
|
|
* vring_interrupt - notify a virtqueue on an interrupt
|
|
* @irq: the IRQ number (ignored)
|
|
* @_vq: the struct virtqueue to notify
|
|
*
|
|
* Calls the callback function of @_vq to process the virtqueue
|
|
* notification.
|
|
*/
|
|
irqreturn_t vring_interrupt(int irq, void *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (!more_used(vq)) {
|
|
pr_debug("virtqueue interrupt with no work for %p\n", vq);
|
|
return IRQ_NONE;
|
|
}
|
|
|
|
if (unlikely(vq->broken)) {
|
|
#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
|
|
dev_warn_once(&vq->vq.vdev->dev,
|
|
"virtio vring IRQ raised before DRIVER_OK");
|
|
return IRQ_NONE;
|
|
#else
|
|
return IRQ_HANDLED;
|
|
#endif
|
|
}
|
|
|
|
/* Just a hint for performance: so it's ok that this can be racy! */
|
|
if (vq->event)
|
|
vq->event_triggered = true;
|
|
|
|
pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
|
|
if (vq->vq.callback)
|
|
vq->vq.callback(&vq->vq);
|
|
|
|
return IRQ_HANDLED;
|
|
}
|
|
EXPORT_SYMBOL_GPL(vring_interrupt);
|
|
|
|
/* Only available for split ring */
|
|
static struct virtqueue *__vring_new_virtqueue(unsigned int index,
|
|
struct vring_virtqueue_split *vring_split,
|
|
struct virtio_device *vdev,
|
|
bool weak_barriers,
|
|
bool context,
|
|
bool (*notify)(struct virtqueue *),
|
|
void (*callback)(struct virtqueue *),
|
|
const char *name)
|
|
{
|
|
struct vring_virtqueue *vq;
|
|
int err;
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
|
|
return NULL;
|
|
|
|
vq = kmalloc(sizeof(*vq), GFP_KERNEL);
|
|
if (!vq)
|
|
return NULL;
|
|
|
|
vq->packed_ring = false;
|
|
vq->vq.callback = callback;
|
|
vq->vq.vdev = vdev;
|
|
vq->vq.name = name;
|
|
vq->vq.index = index;
|
|
vq->vq.reset = false;
|
|
vq->we_own_ring = false;
|
|
vq->notify = notify;
|
|
vq->weak_barriers = weak_barriers;
|
|
#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
|
|
vq->broken = true;
|
|
#else
|
|
vq->broken = false;
|
|
#endif
|
|
vq->use_dma_api = vring_use_dma_api(vdev);
|
|
|
|
vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) &&
|
|
!context;
|
|
vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM))
|
|
vq->weak_barriers = false;
|
|
|
|
err = vring_alloc_state_extra_split(vring_split);
|
|
if (err) {
|
|
kfree(vq);
|
|
return NULL;
|
|
}
|
|
|
|
virtqueue_vring_init_split(vring_split, vq);
|
|
|
|
virtqueue_init(vq, vring_split->vring.num);
|
|
virtqueue_vring_attach_split(vq, vring_split);
|
|
|
|
spin_lock(&vdev->vqs_list_lock);
|
|
list_add_tail(&vq->vq.list, &vdev->vqs);
|
|
spin_unlock(&vdev->vqs_list_lock);
|
|
return &vq->vq;
|
|
}
|
|
|
|
struct virtqueue *vring_create_virtqueue(
|
|
unsigned int index,
|
|
unsigned int num,
|
|
unsigned int vring_align,
|
|
struct virtio_device *vdev,
|
|
bool weak_barriers,
|
|
bool may_reduce_num,
|
|
bool context,
|
|
bool (*notify)(struct virtqueue *),
|
|
void (*callback)(struct virtqueue *),
|
|
const char *name)
|
|
{
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
|
|
return vring_create_virtqueue_packed(index, num, vring_align,
|
|
vdev, weak_barriers, may_reduce_num,
|
|
context, notify, callback, name);
|
|
|
|
return vring_create_virtqueue_split(index, num, vring_align,
|
|
vdev, weak_barriers, may_reduce_num,
|
|
context, notify, callback, name);
|
|
}
|
|
EXPORT_SYMBOL_GPL(vring_create_virtqueue);
|
|
|
|
/**
|
|
* virtqueue_resize - resize the vring of vq
|
|
* @_vq: the struct virtqueue we're talking about.
|
|
* @num: new ring num
|
|
* @recycle: callback for recycle the useless buffer
|
|
*
|
|
* When it is really necessary to create a new vring, it will set the current vq
|
|
* into the reset state. Then call the passed callback to recycle the buffer
|
|
* that is no longer used. Only after the new vring is successfully created, the
|
|
* old vring will be released.
|
|
*
|
|
* Caller must ensure we don't call this with other virtqueue operations
|
|
* at the same time (except where noted).
|
|
*
|
|
* Returns zero or a negative error.
|
|
* 0: success.
|
|
* -ENOMEM: Failed to allocate a new ring, fall back to the original ring size.
|
|
* vq can still work normally
|
|
* -EBUSY: Failed to sync with device, vq may not work properly
|
|
* -ENOENT: Transport or device not supported
|
|
* -E2BIG/-EINVAL: num error
|
|
* -EPERM: Operation not permitted
|
|
*
|
|
*/
|
|
int virtqueue_resize(struct virtqueue *_vq, u32 num,
|
|
void (*recycle)(struct virtqueue *vq, void *buf))
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
struct virtio_device *vdev = vq->vq.vdev;
|
|
void *buf;
|
|
int err;
|
|
|
|
if (!vq->we_own_ring)
|
|
return -EPERM;
|
|
|
|
if (num > vq->vq.num_max)
|
|
return -E2BIG;
|
|
|
|
if (!num)
|
|
return -EINVAL;
|
|
|
|
if ((vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num) == num)
|
|
return 0;
|
|
|
|
if (!vdev->config->disable_vq_and_reset)
|
|
return -ENOENT;
|
|
|
|
if (!vdev->config->enable_vq_after_reset)
|
|
return -ENOENT;
|
|
|
|
err = vdev->config->disable_vq_and_reset(_vq);
|
|
if (err)
|
|
return err;
|
|
|
|
while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
|
|
recycle(_vq, buf);
|
|
|
|
if (vq->packed_ring)
|
|
err = virtqueue_resize_packed(_vq, num);
|
|
else
|
|
err = virtqueue_resize_split(_vq, num);
|
|
|
|
if (vdev->config->enable_vq_after_reset(_vq))
|
|
return -EBUSY;
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_resize);
|
|
|
|
/* Only available for split ring */
|
|
struct virtqueue *vring_new_virtqueue(unsigned int index,
|
|
unsigned int num,
|
|
unsigned int vring_align,
|
|
struct virtio_device *vdev,
|
|
bool weak_barriers,
|
|
bool context,
|
|
void *pages,
|
|
bool (*notify)(struct virtqueue *vq),
|
|
void (*callback)(struct virtqueue *vq),
|
|
const char *name)
|
|
{
|
|
struct vring_virtqueue_split vring_split = {};
|
|
|
|
if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED))
|
|
return NULL;
|
|
|
|
vring_init(&vring_split.vring, num, pages, vring_align);
|
|
return __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers,
|
|
context, notify, callback, name);
|
|
}
|
|
EXPORT_SYMBOL_GPL(vring_new_virtqueue);
|
|
|
|
static void vring_free(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
if (vq->we_own_ring) {
|
|
if (vq->packed_ring) {
|
|
vring_free_queue(vq->vq.vdev,
|
|
vq->packed.ring_size_in_bytes,
|
|
vq->packed.vring.desc,
|
|
vq->packed.ring_dma_addr);
|
|
|
|
vring_free_queue(vq->vq.vdev,
|
|
vq->packed.event_size_in_bytes,
|
|
vq->packed.vring.driver,
|
|
vq->packed.driver_event_dma_addr);
|
|
|
|
vring_free_queue(vq->vq.vdev,
|
|
vq->packed.event_size_in_bytes,
|
|
vq->packed.vring.device,
|
|
vq->packed.device_event_dma_addr);
|
|
|
|
kfree(vq->packed.desc_state);
|
|
kfree(vq->packed.desc_extra);
|
|
} else {
|
|
vring_free_queue(vq->vq.vdev,
|
|
vq->split.queue_size_in_bytes,
|
|
vq->split.vring.desc,
|
|
vq->split.queue_dma_addr);
|
|
}
|
|
}
|
|
if (!vq->packed_ring) {
|
|
kfree(vq->split.desc_state);
|
|
kfree(vq->split.desc_extra);
|
|
}
|
|
}
|
|
|
|
void vring_del_virtqueue(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
spin_lock(&vq->vq.vdev->vqs_list_lock);
|
|
list_del(&_vq->list);
|
|
spin_unlock(&vq->vq.vdev->vqs_list_lock);
|
|
|
|
vring_free(_vq);
|
|
|
|
kfree(vq);
|
|
}
|
|
EXPORT_SYMBOL_GPL(vring_del_virtqueue);
|
|
|
|
/* Manipulates transport-specific feature bits. */
|
|
void vring_transport_features(struct virtio_device *vdev)
|
|
{
|
|
unsigned int i;
|
|
|
|
for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
|
|
switch (i) {
|
|
case VIRTIO_RING_F_INDIRECT_DESC:
|
|
break;
|
|
case VIRTIO_RING_F_EVENT_IDX:
|
|
break;
|
|
case VIRTIO_F_VERSION_1:
|
|
break;
|
|
case VIRTIO_F_ACCESS_PLATFORM:
|
|
break;
|
|
case VIRTIO_F_RING_PACKED:
|
|
break;
|
|
case VIRTIO_F_ORDER_PLATFORM:
|
|
break;
|
|
default:
|
|
/* We don't understand this bit. */
|
|
__virtio_clear_bit(vdev, i);
|
|
}
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(vring_transport_features);
|
|
|
|
/**
|
|
* virtqueue_get_vring_size - return the size of the virtqueue's vring
|
|
* @_vq: the struct virtqueue containing the vring of interest.
|
|
*
|
|
* Returns the size of the vring. This is mainly used for boasting to
|
|
* userspace. Unlike other operations, this need not be serialized.
|
|
*/
|
|
unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
|
|
{
|
|
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num;
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
|
|
|
|
/*
|
|
* This function should only be called by the core, not directly by the driver.
|
|
*/
|
|
void __virtqueue_break(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
|
|
WRITE_ONCE(vq->broken, true);
|
|
}
|
|
EXPORT_SYMBOL_GPL(__virtqueue_break);
|
|
|
|
/*
|
|
* This function should only be called by the core, not directly by the driver.
|
|
*/
|
|
void __virtqueue_unbreak(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
|
|
WRITE_ONCE(vq->broken, false);
|
|
}
|
|
EXPORT_SYMBOL_GPL(__virtqueue_unbreak);
|
|
|
|
bool virtqueue_is_broken(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
return READ_ONCE(vq->broken);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_is_broken);
|
|
|
|
/*
|
|
* This should prevent the device from being used, allowing drivers to
|
|
* recover. You may need to grab appropriate locks to flush.
|
|
*/
|
|
void virtio_break_device(struct virtio_device *dev)
|
|
{
|
|
struct virtqueue *_vq;
|
|
|
|
spin_lock(&dev->vqs_list_lock);
|
|
list_for_each_entry(_vq, &dev->vqs, list) {
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
|
|
WRITE_ONCE(vq->broken, true);
|
|
}
|
|
spin_unlock(&dev->vqs_list_lock);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtio_break_device);
|
|
|
|
/*
|
|
* This should allow the device to be used by the driver. You may
|
|
* need to grab appropriate locks to flush the write to
|
|
* vq->broken. This should only be used in some specific case e.g
|
|
* (probing and restoring). This function should only be called by the
|
|
* core, not directly by the driver.
|
|
*/
|
|
void __virtio_unbreak_device(struct virtio_device *dev)
|
|
{
|
|
struct virtqueue *_vq;
|
|
|
|
spin_lock(&dev->vqs_list_lock);
|
|
list_for_each_entry(_vq, &dev->vqs, list) {
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
/* Pairs with READ_ONCE() in virtqueue_is_broken(). */
|
|
WRITE_ONCE(vq->broken, false);
|
|
}
|
|
spin_unlock(&dev->vqs_list_lock);
|
|
}
|
|
EXPORT_SYMBOL_GPL(__virtio_unbreak_device);
|
|
|
|
dma_addr_t virtqueue_get_desc_addr(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
BUG_ON(!vq->we_own_ring);
|
|
|
|
if (vq->packed_ring)
|
|
return vq->packed.ring_dma_addr;
|
|
|
|
return vq->split.queue_dma_addr;
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_get_desc_addr);
|
|
|
|
dma_addr_t virtqueue_get_avail_addr(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
BUG_ON(!vq->we_own_ring);
|
|
|
|
if (vq->packed_ring)
|
|
return vq->packed.driver_event_dma_addr;
|
|
|
|
return vq->split.queue_dma_addr +
|
|
((char *)vq->split.vring.avail - (char *)vq->split.vring.desc);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_get_avail_addr);
|
|
|
|
dma_addr_t virtqueue_get_used_addr(struct virtqueue *_vq)
|
|
{
|
|
struct vring_virtqueue *vq = to_vvq(_vq);
|
|
|
|
BUG_ON(!vq->we_own_ring);
|
|
|
|
if (vq->packed_ring)
|
|
return vq->packed.device_event_dma_addr;
|
|
|
|
return vq->split.queue_dma_addr +
|
|
((char *)vq->split.vring.used - (char *)vq->split.vring.desc);
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_get_used_addr);
|
|
|
|
/* Only available for split ring */
|
|
const struct vring *virtqueue_get_vring(struct virtqueue *vq)
|
|
{
|
|
return &to_vvq(vq)->split.vring;
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_get_vring);
|
|
|
|
/*
|
|
* Prevents use of DMA API for buffers passed via the specified virtqueue.
|
|
* DMA API may still be used for the vrings themselves.
|
|
*/
|
|
void virtqueue_disable_dma_api_for_buffers(struct virtqueue *vq)
|
|
{
|
|
to_vvq(vq)->use_dma_api = false;
|
|
}
|
|
EXPORT_SYMBOL_GPL(virtqueue_disable_dma_api_for_buffers);
|
|
|
|
MODULE_LICENSE("GPL");
|