// SPDX-License-Identifier: GPL-2.012//! IO polling.3//!4//! C header: [`include/linux/iopoll.h`](srctree/include/linux/iopoll.h).56use crate::{7prelude::*,8processor::cpu_relax,9task::might_sleep,10time::{11delay::{12fsleep,13udelay, //14},15Delta,16Instant,17Monotonic, //18},19};2021/// Polls periodically until a condition is met, an error occurs,22/// or the timeout is reached.23///24/// The function repeatedly executes the given operation `op` closure and25/// checks its result using the condition closure `cond`.26///27/// If `cond` returns `true`, the function returns successfully with28/// the result of `op`. Otherwise, it waits for a duration specified29/// by `sleep_delta` before executing `op` again.30///31/// This process continues until either `op` returns an error, `cond`32/// returns `true`, or the timeout specified by `timeout_delta` is33/// reached.34///35/// This function can only be used in a nonatomic context.36///37/// # Errors38///39/// If `op` returns an error, then that error is returned directly.40///41/// If the timeout specified by `timeout_delta` is reached, then42/// `Err(ETIMEDOUT)` is returned.43///44/// # Examples45///46/// ```no_run47/// use kernel::io::{Io, poll::read_poll_timeout};48/// use kernel::time::Delta;49///50/// const HW_READY: u16 = 0x01;51///52/// fn wait_for_hardware<const SIZE: usize>(io: &Io<SIZE>) -> Result {53/// read_poll_timeout(54/// // The `op` closure reads the value of a specific status register.55/// || io.try_read16(0x1000),56/// // The `cond` closure takes a reference to the value returned by `op`57/// // and checks whether the hardware is ready.58/// |val: &u16| *val == HW_READY,59/// Delta::from_millis(50),60/// Delta::from_secs(3),61/// )?;62/// Ok(())63/// }64/// ```65#[track_caller]66pub fn read_poll_timeout<Op, Cond, T>(67mut op: Op,68mut cond: Cond,69sleep_delta: Delta,70timeout_delta: Delta,71) -> Result<T>72where73Op: FnMut() -> Result<T>,74Cond: FnMut(&T) -> bool,75{76let start: Instant<Monotonic> = Instant::now();7778// Unlike the C version, we always call `might_sleep()` unconditionally,79// as conditional calls are error-prone. We clearly separate80// `read_poll_timeout()` and `read_poll_timeout_atomic()` to aid81// tools like klint.82might_sleep();8384loop {85let val = op()?;86if cond(&val) {87// Unlike the C version, we immediately return.88// We know the condition is met so we don't need to check again.89return Ok(val);90}9192if start.elapsed() > timeout_delta {93// Unlike the C version, we immediately return.94// We have just called `op()` so we don't need to call it again.95return Err(ETIMEDOUT);96}9798if !sleep_delta.is_zero() {99fsleep(sleep_delta);100}101102// `fsleep()` could be a busy-wait loop so we always call `cpu_relax()`.103cpu_relax();104}105}106107/// Polls periodically until a condition is met, an error occurs,108/// or the attempt limit is reached.109///110/// The function repeatedly executes the given operation `op` closure and111/// checks its result using the condition closure `cond`.112///113/// If `cond` returns `true`, the function returns successfully with the result of `op`.114/// Otherwise, it performs a busy wait for a duration specified by `delay_delta`115/// before executing `op` again.116///117/// This process continues until either `op` returns an error, `cond`118/// returns `true`, or the attempt limit specified by `retry` is reached.119///120/// # Errors121///122/// If `op` returns an error, then that error is returned directly.123///124/// If the attempt limit specified by `retry` is reached, then125/// `Err(ETIMEDOUT)` is returned.126///127/// # Examples128///129/// ```no_run130/// use kernel::io::{poll::read_poll_timeout_atomic, Io};131/// use kernel::time::Delta;132///133/// const HW_READY: u16 = 0x01;134///135/// fn wait_for_hardware<const SIZE: usize>(io: &Io<SIZE>) -> Result {136/// read_poll_timeout_atomic(137/// // The `op` closure reads the value of a specific status register.138/// || io.try_read16(0x1000),139/// // The `cond` closure takes a reference to the value returned by `op`140/// // and checks whether the hardware is ready.141/// |val: &u16| *val == HW_READY,142/// Delta::from_micros(50),143/// 1000,144/// )?;145/// Ok(())146/// }147/// ```148pub fn read_poll_timeout_atomic<Op, Cond, T>(149mut op: Op,150mut cond: Cond,151delay_delta: Delta,152retry: usize,153) -> Result<T>154where155Op: FnMut() -> Result<T>,156Cond: FnMut(&T) -> bool,157{158for _ in 0..retry {159let val = op()?;160if cond(&val) {161return Ok(val);162}163164if !delay_delta.is_zero() {165udelay(delay_delta);166}167168cpu_relax();169}170171Err(ETIMEDOUT)172}173174175