| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | bindings,
|
| 9 | container_of,
|
| 10 | device,
|
| 11 | prelude::*,
|
| 12 | sync::aref::{
|
| 13 | ARef,
|
| 14 | AlwaysRefCounted, |
| 15 | },
|
| 16 | types::Opaque, |
| 17 | };
|
| 18 | use core::{
|
| 19 | alloc::Layout,
|
| 20 | cell::UnsafeCell,
|
| 21 | marker::PhantomData,
|
| 22 | ptr::NonNull,
|
| 23 | slice, |
| 24 | };
|
| 25 |
|
| 26 | |
| 27 | const fn kmalloc_aligned_size<T>() -> usize {
|
| 28 | Layout::new::<T>().pad_to_align().size()
|
| 29 | }
|
| 30 |
|
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | #[repr(u32)]
|
| 37 | #[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
| 38 | pub enum DeviceType {
|
| 39 | |
| 40 | Mlx5 = bindings::fwctl_device_type_FWCTL_DEVICE_TYPE_MLX5,
|
| 41 | |
| 42 | Cxl = bindings::fwctl_device_type_FWCTL_DEVICE_TYPE_CXL,
|
| 43 | |
| 44 | Pds = bindings::fwctl_device_type_FWCTL_DEVICE_TYPE_PDS,
|
| 45 | |
| 46 | Bnxt = bindings::fwctl_device_type_FWCTL_DEVICE_TYPE_BNXT,
|
| 47 | }
|
| 48 |
|
| 49 | |
| 50 | |
| 51 | |
| 52 | #[repr(u32)]
|
| 53 | #[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
| 54 | pub enum RpcScope {
|
| 55 | |
| 56 | Configuration = bindings::fwctl_rpc_scope_FWCTL_RPC_CONFIGURATION,
|
| 57 | |
| 58 | DebugReadOnly = bindings::fwctl_rpc_scope_FWCTL_RPC_DEBUG_READ_ONLY,
|
| 59 | |
| 60 | DebugWrite = bindings::fwctl_rpc_scope_FWCTL_RPC_DEBUG_WRITE,
|
| 61 | |
| 62 | DebugWriteFull = bindings::fwctl_rpc_scope_FWCTL_RPC_DEBUG_WRITE_FULL,
|
| 63 | }
|
| 64 |
|
| 65 | impl TryFrom<u32> for RpcScope {
|
| 66 | type Error = Error;
|
| 67 |
|
| 68 | #[inline]
|
| 69 | fn try_from(value: u32) -> Result<Self, Error> {
|
| 70 | match value {
|
| 71 | v if v == Self::Configuration as u32 => Ok(Self::Configuration),
|
| 72 | v if v == Self::DebugReadOnly as u32 => Ok(Self::DebugReadOnly),
|
| 73 | v if v == Self::DebugWrite as u32 => Ok(Self::DebugWrite),
|
| 74 | v if v == Self::DebugWriteFull as u32 => Ok(Self::DebugWriteFull),
|
| 75 | _ => Err(EINVAL),
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | |
| 81 | pub enum FwRpcResponse {
|
| 82 | |
| 83 | |
| 84 | |
| 85 | InPlace(usize),
|
| 86 | |
| 87 | NewBuffer(KVVec<u8>),
|
| 88 | }
|
| 89 |
|
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | pub trait Operations: Sized + Send + Sync + 'static {
|
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | type RegistrationData<'a>: Send + Sync + 'a
|
| 105 | where
|
| 106 | Self: 'a;
|
| 107 |
|
| 108 | |
| 109 | const DEVICE_TYPE: DeviceType;
|
| 110 |
|
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | fn open<'a>(
|
| 116 | device: &Device<Self>,
|
| 117 | reg_data: &Self::RegistrationData<'a>,
|
| 118 | ) -> impl PinInit<Self, Error>;
|
| 119 |
|
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | fn close<'a>(
|
| 126 | _this: Pin<&mut Self>,
|
| 127 | _device: &Device<Self>,
|
| 128 | _reg_data: &Self::RegistrationData<'a>,
|
| 129 | ) {
|
| 130 | }
|
| 131 |
|
| 132 | |
| 133 | |
| 134 | |
| 135 | fn info<'a>(
|
| 136 | _this: Pin<&Self>,
|
| 137 | _device: &Device<Self>,
|
| 138 | _reg_data: &Self::RegistrationData<'a>,
|
| 139 | ) -> Result<KVec<u8>, Error> {
|
| 140 | Ok(KVec::new())
|
| 141 | }
|
| 142 |
|
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | fn fw_rpc<'a>(
|
| 149 | this: Pin<&Self>,
|
| 150 | device: &Device<Self>,
|
| 151 | reg_data: &Self::RegistrationData<'a>,
|
| 152 | scope: RpcScope,
|
| 153 | rpc_buf: &mut [u8],
|
| 154 | max_output_len: usize,
|
| 155 | ) -> Result<FwRpcResponse, Error>;
|
| 156 | }
|
| 157 |
|
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | #[repr(C)]
|
| 171 | pub struct Device<T: Operations> {
|
| 172 | dev: Opaque<bindings::fwctl_device>,
|
| 173 | registration_data: UnsafeCell<NonNull<T::RegistrationData<'static>>>,
|
| 174 | }
|
| 175 |
|
| 176 | impl<T: Operations> Device<T> {
|
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | pub fn new(parent: &device::Device<device::Bound>) -> Result<ARef<Self>> {
|
| 182 | const_assert!(
|
| 183 | core::mem::offset_of!(Self, dev) == 0,
|
| 184 | "struct fwctl_device must be at offset 0"
|
| 185 | );
|
| 186 |
|
| 187 | let size = kmalloc_aligned_size::<Self>();
|
| 188 | let ops = core::ptr::from_ref::<bindings::fwctl_ops>(&VTable::<T>::VTABLE).cast_mut();
|
| 189 |
|
| 190 | |
| 191 | |
| 192 | let raw = unsafe { bindings::_fwctl_alloc_device(parent.as_raw(), ops, size) };
|
| 193 | let this = NonNull::new(raw.cast::<Self>()).ok_or(ENOMEM)?;
|
| 194 |
|
| 195 | |
| 196 | |
| 197 | unsafe {
|
| 198 | (&raw mut (*this.as_ptr()).registration_data)
|
| 199 | .write(UnsafeCell::new(NonNull::dangling()));
|
| 200 | };
|
| 201 |
|
| 202 | |
| 203 | Ok(unsafe { ARef::from_raw(this) })
|
| 204 | }
|
| 205 |
|
| 206 | |
| 207 | #[inline]
|
| 208 | fn as_raw(&self) -> *mut bindings::fwctl_device {
|
| 209 | self.dev.get()
|
| 210 | }
|
| 211 |
|
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | #[inline]
|
| 218 | unsafe fn from_raw<'a>(ptr: *mut bindings::fwctl_device) -> &'a Self {
|
| 219 | |
| 220 | unsafe { &*ptr.cast() }
|
| 221 | }
|
| 222 |
|
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | #[inline]
|
| 233 | unsafe fn with_registration_data<R>(
|
| 234 | &self,
|
| 235 | f: impl for<'a> FnOnce(&Device<T>, &'a T::RegistrationData<'a>) -> R,
|
| 236 | ) -> R {
|
| 237 | |
| 238 | |
| 239 | |
| 240 | let reg_data = unsafe {
|
| 241 | (*self.registration_data.get())
|
| 242 | .cast::<T::RegistrationData<'_>>()
|
| 243 | .as_ref()
|
| 244 | };
|
| 245 |
|
| 246 | f(self, reg_data)
|
| 247 | }
|
| 248 | }
|
| 249 |
|
| 250 | impl<T: Operations> AsRef<device::Device> for Device<T> {
|
| 251 | #[inline]
|
| 252 | fn as_ref(&self) -> &device::Device {
|
| 253 | |
| 254 | let dev = unsafe { &raw mut (*self.as_raw()).dev };
|
| 255 | |
| 256 | unsafe { device::Device::from_raw(dev) }
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | |
| 261 | |
| 262 | unsafe impl<T: Operations> AlwaysRefCounted for Device<T> {
|
| 263 | #[inline]
|
| 264 | fn inc_ref(&self) {
|
| 265 | |
| 266 | unsafe { bindings::fwctl_get(self.as_raw()) };
|
| 267 | }
|
| 268 |
|
| 269 | #[inline]
|
| 270 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 271 | |
| 272 | unsafe { bindings::fwctl_put(obj.cast().as_ptr()) };
|
| 273 | }
|
| 274 | }
|
| 275 |
|
| 276 | |
| 277 | unsafe impl<T: Operations> Send for Device<T> {}
|
| 278 |
|
| 279 | |
| 280 | |
| 281 | |
| 282 | unsafe impl<T: Operations> Sync for Device<T> {}
|
| 283 |
|
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | pub struct Registration<'a, T: Operations> {
|
| 295 | dev: ARef<Device<T>>,
|
| 296 | _reg_data: Pin<KBox<T::RegistrationData<'a>>>,
|
| 297 | }
|
| 298 |
|
| 299 | impl<'a, T: Operations> Registration<'a, T> {
|
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | pub unsafe fn new(
|
| 313 | parent: &'a device::Device<device::Bound>,
|
| 314 | dev: &Device<T>,
|
| 315 | reg_data: impl PinInit<T::RegistrationData<'a>, Error>,
|
| 316 | ) -> Result<Self> {
|
| 317 | let actual_parent = dev.as_ref().parent().ok_or(EINVAL)?;
|
| 318 | let parent_device: &device::Device = parent;
|
| 319 | if !core::ptr::eq(actual_parent, parent_device) {
|
| 320 | return Err(EINVAL);
|
| 321 | }
|
| 322 |
|
| 323 | let reg_data: Pin<KBox<T::RegistrationData<'a>>> = KBox::pin_init(reg_data, GFP_KERNEL)?;
|
| 324 |
|
| 325 | |
| 326 | |
| 327 | |
| 328 | let ptr: NonNull<T::RegistrationData<'static>> =
|
| 329 | NonNull::from(Pin::get_ref(reg_data.as_ref())).cast();
|
| 330 |
|
| 331 | |
| 332 | unsafe { *dev.registration_data.get() = ptr };
|
| 333 |
|
| 334 | |
| 335 | let ret = unsafe { bindings::fwctl_register(dev.as_raw()) };
|
| 336 | if ret != 0 {
|
| 337 | |
| 338 | unsafe { *dev.registration_data.get() = NonNull::dangling() };
|
| 339 | return Err(Error::from_errno(ret));
|
| 340 | }
|
| 341 |
|
| 342 | Ok(Self {
|
| 343 | dev: dev.into(),
|
| 344 | _reg_data: reg_data,
|
| 345 | })
|
| 346 | }
|
| 347 | }
|
| 348 |
|
| 349 | impl<T: Operations> Drop for Registration<'_, T> {
|
| 350 | fn drop(&mut self) {
|
| 351 | |
| 352 | |
| 353 | |
| 354 | unsafe { bindings::fwctl_unregister(self.dev.as_raw()) };
|
| 355 |
|
| 356 | |
| 357 | unsafe { *self.dev.registration_data.get() = NonNull::dangling() };
|
| 358 |
|
| 359 | |
| 360 | }
|
| 361 | }
|
| 362 |
|
| 363 | |
| 364 | |
| 365 | |
| 366 | #[repr(C)]
|
| 367 | #[pin_data]
|
| 368 | struct UserCtx<T: Operations> {
|
| 369 | #[pin]
|
| 370 | fwctl_uctx: Opaque<bindings::fwctl_uctx>,
|
| 371 | #[pin]
|
| 372 | uctx: T,
|
| 373 | }
|
| 374 |
|
| 375 | impl<T: Operations> UserCtx<T> {
|
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | #[inline]
|
| 383 | unsafe fn from_raw<'a>(ptr: *mut bindings::fwctl_uctx) -> Pin<&'a Self> {
|
| 384 | |
| 385 | unsafe { Pin::new_unchecked(&*container_of!(Opaque::cast_from(ptr), Self, fwctl_uctx)) }
|
| 386 | }
|
| 387 |
|
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | #[inline]
|
| 396 | unsafe fn from_raw_mut<'a>(ptr: *mut bindings::fwctl_uctx) -> Pin<&'a mut Self> {
|
| 397 | |
| 398 | unsafe {
|
| 399 | Pin::new_unchecked(
|
| 400 | &mut *container_of!(Opaque::cast_from(ptr), Self, fwctl_uctx).cast_mut(),
|
| 401 | )
|
| 402 | }
|
| 403 | }
|
| 404 |
|
| 405 | |
| 406 | #[inline]
|
| 407 | fn device(self: Pin<&Self>) -> &Device<T> {
|
| 408 | |
| 409 | let raw_fwctl = unsafe { (*self.fwctl_uctx.get()).fwctl };
|
| 410 | |
| 411 | unsafe { Device::from_raw(raw_fwctl) }
|
| 412 | }
|
| 413 |
|
| 414 | |
| 415 | #[inline]
|
| 416 | fn uctx(self: Pin<&Self>) -> Pin<&T> {
|
| 417 | ::pin_init::assert_pinned!(UserCtx<T>, uctx, T, inline);
|
| 418 |
|
| 419 | |
| 420 | unsafe { self.map_unchecked(|ctx| &ctx.uctx) }
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | |
| 425 | struct VTable<T: Operations>(PhantomData<T>);
|
| 426 |
|
| 427 | impl<T: Operations> VTable<T> {
|
| 428 | |
| 429 | const VTABLE: bindings::fwctl_ops = bindings::fwctl_ops {
|
| 430 | |
| 431 | device_type: T::DEVICE_TYPE as u32,
|
| 432 | uctx_size: kmalloc_aligned_size::<UserCtx<T>>(),
|
| 433 | open_uctx: Some(Self::open_uctx_callback),
|
| 434 | close_uctx: Some(Self::close_uctx_callback),
|
| 435 | info: Some(Self::info_callback),
|
| 436 | fw_rpc: Some(Self::fw_rpc_callback),
|
| 437 | };
|
| 438 |
|
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | unsafe extern "C" fn open_uctx_callback(uctx: *mut bindings::fwctl_uctx) -> ffi::c_int {
|
| 446 | const_assert!(
|
| 447 | core::mem::offset_of!(UserCtx<T>, fwctl_uctx) == 0,
|
| 448 | "struct fwctl_uctx must be at offset 0"
|
| 449 | );
|
| 450 |
|
| 451 | |
| 452 | let raw_fwctl = unsafe { (*uctx).fwctl };
|
| 453 | |
| 454 | let device = unsafe { Device::<T>::from_raw(raw_fwctl) };
|
| 455 |
|
| 456 | let uctx_offset = core::mem::offset_of!(UserCtx<T>, uctx);
|
| 457 | |
| 458 | let uctx_ptr: *mut T = unsafe { uctx.byte_add(uctx_offset).cast() };
|
| 459 |
|
| 460 | |
| 461 | |
| 462 | |
| 463 | unsafe {
|
| 464 | device.with_registration_data(|device, reg_data| {
|
| 465 | match pin_init::raw_try_init(uctx_ptr, T::open(device, reg_data)) {
|
| 466 | Ok(()) => 0,
|
| 467 | Err(e) => e.to_errno(),
|
| 468 | }
|
| 469 | })
|
| 470 | }
|
| 471 | }
|
| 472 |
|
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | unsafe extern "C" fn close_uctx_callback(uctx: *mut bindings::fwctl_uctx) {
|
| 479 | |
| 480 | let device = unsafe { Device::<T>::from_raw((*uctx).fwctl) };
|
| 481 |
|
| 482 | |
| 483 | let mut ctx = unsafe { UserCtx::<T>::from_raw_mut(uctx) };
|
| 484 |
|
| 485 | |
| 486 | |
| 487 | unsafe {
|
| 488 | device.with_registration_data(|device, reg_data| {
|
| 489 | T::close(ctx.as_mut().project().uctx, device, reg_data);
|
| 490 | });
|
| 491 | }
|
| 492 |
|
| 493 | |
| 494 | unsafe { core::ptr::drop_in_place(ctx.project().uctx.get_unchecked_mut()) };
|
| 495 | }
|
| 496 |
|
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | unsafe extern "C" fn info_callback(
|
| 504 | uctx: *mut bindings::fwctl_uctx,
|
| 505 | length: *mut usize,
|
| 506 | ) -> *mut ffi::c_void {
|
| 507 | |
| 508 | let ctx = unsafe { UserCtx::<T>::from_raw(uctx) };
|
| 509 | let device = ctx.device();
|
| 510 |
|
| 511 | |
| 512 | let result = unsafe {
|
| 513 | device.with_registration_data(|device, reg_data| T::info(ctx.uctx(), device, reg_data))
|
| 514 | };
|
| 515 |
|
| 516 | match result {
|
| 517 | Ok(kvec) if kvec.is_empty() => {
|
| 518 | |
| 519 | unsafe { *length = 0 };
|
| 520 | |
| 521 | core::ptr::null_mut()
|
| 522 | }
|
| 523 | Ok(kvec) => {
|
| 524 | let (ptr, len, _cap) = kvec.into_raw_parts();
|
| 525 | |
| 526 | unsafe { *length = len };
|
| 527 | ptr.cast::<ffi::c_void>()
|
| 528 | }
|
| 529 | Err(e) => Error::to_ptr(e),
|
| 530 | }
|
| 531 | }
|
| 532 |
|
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | unsafe extern "C" fn fw_rpc_callback(
|
| 541 | uctx: *mut bindings::fwctl_uctx,
|
| 542 | scope: u32,
|
| 543 | rpc_in: *mut ffi::c_void,
|
| 544 | in_len: usize,
|
| 545 | out_len: *mut usize,
|
| 546 | ) -> *mut ffi::c_void {
|
| 547 | let scope = match RpcScope::try_from(scope) {
|
| 548 | Ok(s) => s,
|
| 549 | Err(e) => return Error::to_ptr(e),
|
| 550 | };
|
| 551 |
|
| 552 | |
| 553 | let max_output_len = unsafe { *out_len };
|
| 554 |
|
| 555 | |
| 556 | let ctx = unsafe { UserCtx::<T>::from_raw(uctx) };
|
| 557 | let device = ctx.device();
|
| 558 |
|
| 559 | |
| 560 | |
| 561 | let rpc_buf = unsafe { slice::from_raw_parts_mut(rpc_in.cast::<u8>(), in_len) };
|
| 562 |
|
| 563 | |
| 564 | let result = unsafe {
|
| 565 | device.with_registration_data(|device, reg_data| {
|
| 566 | T::fw_rpc(ctx.uctx(), device, reg_data, scope, rpc_buf, max_output_len)
|
| 567 | })
|
| 568 | };
|
| 569 |
|
| 570 | let (response, response_len) = match result {
|
| 571 | Ok(FwRpcResponse::InPlace(len)) => {
|
| 572 | if len > in_len {
|
| 573 | return Error::to_ptr(EINVAL);
|
| 574 | }
|
| 575 |
|
| 576 | (rpc_in, len)
|
| 577 | }
|
| 578 | Ok(FwRpcResponse::NewBuffer(kvec)) if kvec.is_empty() => {
|
| 579 | |
| 580 | (core::ptr::null_mut(), 0)
|
| 581 | }
|
| 582 | Ok(FwRpcResponse::NewBuffer(kvec)) => {
|
| 583 | let (ptr, len, _cap) = kvec.into_raw_parts();
|
| 584 | (ptr.cast::<ffi::c_void>(), len)
|
| 585 | }
|
| 586 | Err(e) => return Error::to_ptr(e),
|
| 587 | };
|
| 588 |
|
| 589 | |
| 590 | unsafe { *out_len = response_len };
|
| 591 | response
|
| 592 | }
|
| 593 | }
|