//! K2136–K2140 unsupported/failed power fallback path. A profile without //! SYSTEM_OFF support parks immediately in `DegradedParked` (`UnsupportedProfile`), //! and a failing reset/off path allows at most `max_attempts` firmware calls //! before entering `DegradedParked` (`FirmwareRejected`) and refusing further //! retries (`RetryLimit`). Neither path ever claims clean shutdown/reboot //! success, and the guest's degraded park diagnosis is strictly separated from //! a host runner timeout. pub const MAX_ATTEMPTS:u64=4; pub const OP_OFF:u64=1; pub const OP_RESET:u64=2; #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Configuration=1, Ticket, State, RetryLimit, FalseSuccessClaim, HostTimeoutConfused } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Phase { Idle=0, Trying=1, DegradedParked=2 } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Reason { None=0, UnsupportedProfile=1, FirmwareRejected=2 } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Action { Retry=1, ParkDegraded=2 } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Diagnosis { pub op:u64, pub ticket_sequence:u64, pub reason:Reason, pub attempts:u64, pub last_code:i32, pub guest_parked:bool, pub claimed_success:bool, pub host_timeout:bool } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Status { pub max_attempts:u64, pub phase:Phase, pub op:u64, pub ticket_sequence:u64, pub reason:Reason, pub attempts:u64, pub retry_refusals:u64, pub last_code:i32, pub parked_count:u64 } #[repr(C)] #[derive(Debug)] pub struct Fallback { max_attempts:u64, phase:Phase, op:u64, ticket_sequence:u64, reason:Reason, attempts:u64, retry_refusals:u64, last_code:i32, parked_count:u64 } impl Fallback { pub fn new(max_attempts:u64)->Result{ if max_attempts==0||max_attempts>MAX_ATTEMPTS{return Err(Error::Configuration)} Ok(Self{max_attempts,phase:Phase::Idle,op:0,ticket_sequence:0,reason:Reason::None,attempts:0,retry_refusals:0,last_code:0,parked_count:0}) } pub fn status(&self)->Status { Status{max_attempts:self.max_attempts,phase:self.phase,op:self.op,ticket_sequence:self.ticket_sequence,reason:self.reason,attempts:self.attempts,retry_refusals:self.retry_refusals,last_code:self.last_code,parked_count:self.parked_count} } pub fn begin(&mut self,op:u64,ticket_sequence:u64,supported:bool)->Result{ if self.phase!=Phase::Idle{return Err(Error::State)} if (op!=OP_OFF&&op!=OP_RESET)||ticket_sequence==0{return Err(Error::Ticket)} self.op=op;self.ticket_sequence=ticket_sequence; if !supported{ self.phase=Phase::DegradedParked;self.reason=Reason::UnsupportedProfile;self.last_code=-1;self.parked_count+=1; return Ok(Action::ParkDegraded); } self.phase=Phase::Trying;Ok(Action::Retry) } pub fn record_failure(&mut self,firmware_code:i32)->Result{ if self.phase==Phase::DegradedParked{self.retry_refusals+=1;return Err(Error::RetryLimit)} if self.phase!=Phase::Trying{return Err(Error::State)} if firmware_code>=0{return Err(Error::Configuration)} self.attempts+=1;self.last_code=firmware_code; if self.attempts>=self.max_attempts{ self.phase=Phase::DegradedParked;self.reason=Reason::FirmwareRejected;self.parked_count+=1; Ok(Action::ParkDegraded) }else{Ok(Action::Retry)} } pub fn verify_degraded_park(&self,d:Diagnosis)->Result<(),Error>{ if self.phase!=Phase::DegradedParked{return Err(Error::State)} if d.claimed_success{return Err(Error::FalseSuccessClaim)} if !d.guest_parked||d.host_timeout{return Err(Error::HostTimeoutConfused)} if d.op!=self.op||d.ticket_sequence!=self.ticket_sequence||d.reason!=self.reason||d.attempts!=self.attempts||d.last_code!=self.last_code{return Err(Error::Ticket)} Ok(()) } }