1 // SPDX-License-Identifier: GPL-2.0 2 3 //! This module provides an interface for blk-mq drivers to implement. 4 //! 5 //! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h) 6 7 use crate::{ 8 bindings, 9 block::mq::request::RequestDataWrapper, 10 block::mq::Request, 11 error::{from_result, Result}, 12 prelude::*, 13 types::ARef, 14 }; 15 use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ordering}; 16 17 /// Implement this trait to interface blk-mq as block devices. 18 /// 19 /// To implement a block device driver, implement this trait as described in the 20 /// [module level documentation]. The kernel will use the implementation of the 21 /// functions defined in this trait to interface a block device driver. Note: 22 /// There is no need for an exit_request() implementation, because the `drop` 23 /// implementation of the [`Request`] type will be invoked by automatically by 24 /// the C/Rust glue logic. 25 /// 26 /// [module level documentation]: kernel::block::mq 27 #[macros::vtable] 28 pub trait Operations: Sized { 29 /// Called by the kernel to queue a request with the driver. If `is_last` is 30 /// `false`, the driver is allowed to defer committing the request. 31 fn queue_rq(rq: ARef<Request<Self>>, is_last: bool) -> Result; 32 33 /// Called by the kernel to indicate that queued requests should be submitted. 34 fn commit_rqs(); 35 36 /// Called by the kernel to poll the device for completed requests. Only 37 /// used for poll queues. 38 fn poll() -> bool { 39 build_error!(crate::error::VTABLE_DEFAULT_ERROR) 40 } 41 } 42 43 /// A vtable for blk-mq to interact with a block device driver. 44 /// 45 /// A `bindings::blk_mq_ops` vtable is constructed from pointers to the `extern 46 /// "C"` functions of this struct, exposed through the `OperationsVTable::VTABLE`. 47 /// 48 /// For general documentation of these methods, see the kernel source 49 /// documentation related to `struct blk_mq_operations` in 50 /// [`include/linux/blk-mq.h`]. 51 /// 52 /// [`include/linux/blk-mq.h`]: srctree/include/linux/blk-mq.h 53 pub(crate) struct OperationsVTable<T: Operations>(PhantomData<T>); 54 55 impl<T: Operations> OperationsVTable<T> { 56 /// This function is called by the C kernel. A pointer to this function is 57 /// installed in the `blk_mq_ops` vtable for the driver. 58 /// 59 /// # Safety 60 /// 61 /// - The caller of this function must ensure that the pointee of `bd` is 62 /// valid for reads for the duration of this function. 63 /// - This function must be called for an initialized and live `hctx`. That 64 /// is, `Self::init_hctx_callback` was called and 65 /// `Self::exit_hctx_callback()` was not yet called. 66 /// - `(*bd).rq` must point to an initialized and live `bindings:request`. 67 /// That is, `Self::init_request_callback` was called but 68 /// `Self::exit_request_callback` was not yet called for the request. 69 /// - `(*bd).rq` must be owned by the driver. That is, the block layer must 70 /// promise to not access the request until the driver calls 71 /// `bindings::blk_mq_end_request` for the request. 72 unsafe extern "C" fn queue_rq_callback( 73 _hctx: *mut bindings::blk_mq_hw_ctx, 74 bd: *const bindings::blk_mq_queue_data, 75 ) -> bindings::blk_status_t { 76 // SAFETY: `bd.rq` is valid as required by the safety requirement for 77 // this function. 78 let request = unsafe { &*(*bd).rq.cast::<Request<T>>() }; 79 80 // One refcount for the ARef, one for being in flight 81 request.wrapper_ref().refcount().store(2, Ordering::Relaxed); 82 83 // SAFETY: 84 // - We own a refcount that we took above. We pass that to `ARef`. 85 // - By the safety requirements of this function, `request` is a valid 86 // `struct request` and the private data is properly initialized. 87 // - `rq` will be alive until `blk_mq_end_request` is called and is 88 // reference counted by `ARef` until then. 89 let rq = unsafe { Request::aref_from_raw((*bd).rq) }; 90 91 // SAFETY: We have exclusive access and we just set the refcount above. 92 unsafe { Request::start_unchecked(&rq) }; 93 94 let ret = T::queue_rq( 95 rq, 96 // SAFETY: `bd` is valid as required by the safety requirement for 97 // this function. 98 unsafe { (*bd).last }, 99 ); 100 101 if let Err(e) = ret { 102 e.to_blk_status() 103 } else { 104 bindings::BLK_STS_OK as _ 105 } 106 } 107 108 /// This function is called by the C kernel. A pointer to this function is 109 /// installed in the `blk_mq_ops` vtable for the driver. 110 /// 111 /// # Safety 112 /// 113 /// This function may only be called by blk-mq C infrastructure. 114 unsafe extern "C" fn commit_rqs_callback(_hctx: *mut bindings::blk_mq_hw_ctx) { 115 T::commit_rqs() 116 } 117 118 /// This function is called by the C kernel. It is not currently 119 /// implemented, and there is no way to exercise this code path. 120 /// 121 /// # Safety 122 /// 123 /// This function may only be called by blk-mq C infrastructure. 124 unsafe extern "C" fn complete_callback(_rq: *mut bindings::request) {} 125 126 /// This function is called by the C kernel. A pointer to this function is 127 /// installed in the `blk_mq_ops` vtable for the driver. 128 /// 129 /// # Safety 130 /// 131 /// This function may only be called by blk-mq C infrastructure. 132 unsafe extern "C" fn poll_callback( 133 _hctx: *mut bindings::blk_mq_hw_ctx, 134 _iob: *mut bindings::io_comp_batch, 135 ) -> crate::ffi::c_int { 136 T::poll().into() 137 } 138 139 /// This function is called by the C kernel. A pointer to this function is 140 /// installed in the `blk_mq_ops` vtable for the driver. 141 /// 142 /// # Safety 143 /// 144 /// This function may only be called by blk-mq C infrastructure. This 145 /// function may only be called once before `exit_hctx_callback` is called 146 /// for the same context. 147 unsafe extern "C" fn init_hctx_callback( 148 _hctx: *mut bindings::blk_mq_hw_ctx, 149 _tagset_data: *mut crate::ffi::c_void, 150 _hctx_idx: crate::ffi::c_uint, 151 ) -> crate::ffi::c_int { 152 from_result(|| Ok(0)) 153 } 154 155 /// This function is called by the C kernel. A pointer to this function is 156 /// installed in the `blk_mq_ops` vtable for the driver. 157 /// 158 /// # Safety 159 /// 160 /// This function may only be called by blk-mq C infrastructure. 161 unsafe extern "C" fn exit_hctx_callback( 162 _hctx: *mut bindings::blk_mq_hw_ctx, 163 _hctx_idx: crate::ffi::c_uint, 164 ) { 165 } 166 167 /// This function is called by the C kernel. A pointer to this function is 168 /// installed in the `blk_mq_ops` vtable for the driver. 169 /// 170 /// # Safety 171 /// 172 /// - This function may only be called by blk-mq C infrastructure. 173 /// - `_set` must point to an initialized `TagSet<T>`. 174 /// - `rq` must point to an initialized `bindings::request`. 175 /// - The allocation pointed to by `rq` must be at the size of `Request` 176 /// plus the size of `RequestDataWrapper`. 177 unsafe extern "C" fn init_request_callback( 178 _set: *mut bindings::blk_mq_tag_set, 179 rq: *mut bindings::request, 180 _hctx_idx: crate::ffi::c_uint, 181 _numa_node: crate::ffi::c_uint, 182 ) -> crate::ffi::c_int { 183 from_result(|| { 184 // SAFETY: By the safety requirements of this function, `rq` points 185 // to a valid allocation. 186 let pdu = unsafe { Request::wrapper_ptr(rq.cast::<Request<T>>()) }; 187 188 // SAFETY: The refcount field is allocated but not initialized, so 189 // it is valid for writes. 190 unsafe { RequestDataWrapper::refcount_ptr(pdu.as_ptr()).write(AtomicU64::new(0)) }; 191 192 Ok(0) 193 }) 194 } 195 196 /// This function is called by the C kernel. A pointer to this function is 197 /// installed in the `blk_mq_ops` vtable for the driver. 198 /// 199 /// # Safety 200 /// 201 /// - This function may only be called by blk-mq C infrastructure. 202 /// - `_set` must point to an initialized `TagSet<T>`. 203 /// - `rq` must point to an initialized and valid `Request`. 204 unsafe extern "C" fn exit_request_callback( 205 _set: *mut bindings::blk_mq_tag_set, 206 rq: *mut bindings::request, 207 _hctx_idx: crate::ffi::c_uint, 208 ) { 209 // SAFETY: The tagset invariants guarantee that all requests are allocated with extra memory 210 // for the request data. 211 let pdu = unsafe { bindings::blk_mq_rq_to_pdu(rq) }.cast::<RequestDataWrapper>(); 212 213 // SAFETY: `pdu` is valid for read and write and is properly initialised. 214 unsafe { core::ptr::drop_in_place(pdu) }; 215 } 216 217 const VTABLE: bindings::blk_mq_ops = bindings::blk_mq_ops { 218 queue_rq: Some(Self::queue_rq_callback), 219 queue_rqs: None, 220 commit_rqs: Some(Self::commit_rqs_callback), 221 get_budget: None, 222 put_budget: None, 223 set_rq_budget_token: None, 224 get_rq_budget_token: None, 225 timeout: None, 226 poll: if T::HAS_POLL { 227 Some(Self::poll_callback) 228 } else { 229 None 230 }, 231 complete: Some(Self::complete_callback), 232 init_hctx: Some(Self::init_hctx_callback), 233 exit_hctx: Some(Self::exit_hctx_callback), 234 init_request: Some(Self::init_request_callback), 235 exit_request: Some(Self::exit_request_callback), 236 cleanup_rq: None, 237 busy: None, 238 map_queues: None, 239 #[cfg(CONFIG_BLK_DEBUG_FS)] 240 show_rq: None, 241 }; 242 243 pub(crate) const fn build() -> &'static bindings::blk_mq_ops { 244 &Self::VTABLE 245 } 246 } 247