xref: /DragonOS/kernel/src/driver/base/device/dd.rs (revision 01c18c64b14b4ebabd98fa92c587c26874275eb1)
1 use core::intrinsics::unlikely;
2 
3 use alloc::{string::ToString, sync::Arc};
4 use intertrait::cast::CastArc;
5 use log::{debug, error, warn};
6 
7 use crate::{
8     driver::base::kobject::KObject,
9     filesystem::{
10         sysfs::{
11             file::sysfs_emit_str, sysfs_instance, Attribute, SysFSOpsSupport, SYSFS_ATTR_MODE_WO,
12         },
13         vfs::syscall::ModeType,
14     },
15     libs::wait_queue::WaitQueue,
16 };
17 use system_error::SystemError;
18 
19 use super::{
20     bus::BusNotifyEvent,
21     device_manager,
22     driver::{driver_manager, Driver, DriverManager},
23     Device, DeviceManager,
24 };
25 
26 static PROBE_WAIT_QUEUE: WaitQueue = WaitQueue::default();
27 
28 impl DeviceManager {
29     /// 尝试把一个设备与一个驱动匹配
30     ///
31     /// 当前函数会遍历整个bus的驱动列表,并且尝试把设备与每一个驱动进行匹配。
32     /// 一旦有一个驱动匹配成功,就会返回。
33     ///
34     /// ## 参数
35     ///
36     /// - `dev`: 设备
37     ///
38     /// ## 返回
39     ///
40     /// - Ok(true): 匹配成功
41     /// - Ok(false): 没有匹配成功
42     /// - Err(SystemError::ENODEV): 设备还没被注册
43     ///
44     /// ## 参考
45     ///
46     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c#1049
47     pub fn device_attach(&self, dev: &Arc<dyn Device>) -> Result<bool, SystemError> {
48         return self.do_device_attach(dev, false);
49     }
50 
51     pub fn device_initial_probe(&self, dev: &Arc<dyn Device>) -> Result<bool, SystemError> {
52         return self.do_device_attach(dev, true);
53     }
54 
55     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c#978
56     fn do_device_attach(
57         &self,
58         dev: &Arc<dyn Device>,
59         allow_async: bool,
60     ) -> Result<bool, SystemError> {
61         if unlikely(allow_async) {
62             // todo!("do_device_attach: allow_async")
63             warn!("do_device_attach: allow_async is true, but currently not supported");
64         }
65         if dev.is_dead() {
66             return Ok(false);
67         }
68 
69         warn!("do_device_attach: dev: '{}'", dev.name());
70 
71         let mut do_async = false;
72         let mut r = Ok(false);
73 
74         if dev.driver().is_some() {
75             if self.device_is_bound(dev) {
76                 debug!(
77                     "do_device_attach: device '{}' is already bound.",
78                     dev.name()
79                 );
80                 return Ok(true);
81             }
82 
83             if self.device_bind_driver(dev).is_ok() {
84                 return Ok(true);
85             } else {
86                 dev.set_driver(None);
87                 return Ok(false);
88             }
89         } else {
90             debug!("do_device_attach: device '{}' is not bound.", dev.name());
91             let bus = dev
92                 .bus()
93                 .and_then(|bus| bus.upgrade())
94                 .ok_or(SystemError::EINVAL)?;
95             let mut data = DeviceAttachData::new(dev.clone(), allow_async, false);
96             let mut flag = false;
97             for driver in bus.subsystem().drivers().iter() {
98                 let r = self.do_device_attach_driver(driver, &mut data);
99                 if unlikely(r.is_err()) {
100                     flag = false;
101                     break;
102                 } else if r.unwrap() {
103                     flag = true;
104                     break;
105                 }
106             }
107 
108             if flag {
109                 r = Ok(true);
110             }
111 
112             if !flag && allow_async && data.have_async {
113                 // If we could not find appropriate driver
114                 // synchronously and we are allowed to do
115                 // async probes and there are drivers that
116                 // want to probe asynchronously, we'll
117                 // try them.
118 
119                 do_async = true;
120                 debug!(
121                     "do_device_attach: try scheduling asynchronous probe for device: {}",
122                     dev.name()
123                 );
124             }
125         }
126 
127         if do_async {
128             todo!("do_device_attach: do_async")
129         }
130         return r;
131     }
132 
133     /// 匹配设备和驱动
134     ///
135     /// ## 参数
136     ///
137     /// - `driver`: 驱动
138     /// - `data`: 匹配数据
139     ///
140     /// ## 返回
141     ///
142     /// - Ok(true): 匹配成功
143     /// - Ok(false): 没有匹配成功
144     /// - Err(SystemError): 匹配过程中出现意外错误,没有匹配成功
145     ///
146     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c#899
147     fn do_device_attach_driver(
148         &self,
149         driver: &Arc<dyn Driver>,
150         data: &mut DeviceAttachData,
151     ) -> Result<bool, SystemError> {
152         if let Some(bus) = driver.bus().and_then(|bus| bus.upgrade()) {
153             let r = bus.match_device(&data.dev, driver);
154 
155             if let Err(e) = r {
156                 // 如果不是ENOSYS,则总线出错
157                 if e != SystemError::ENOSYS {
158                     debug!(
159                         "do_device_attach_driver: bus.match_device() failed, dev: '{}', err: {:?}",
160                         data.dev.name(),
161                         e
162                     );
163                     return Err(e);
164                 }
165             } else if !r.unwrap() {
166                 return Ok(false);
167             }
168         }
169 
170         let async_allowed = driver.allows_async_probing();
171         if data.check_async && async_allowed != data.want_async {
172             return Ok(false);
173         }
174 
175         return driver_manager()
176             .probe_device(driver, &data.dev)
177             .map(|_| true);
178     }
179 
180     /// 检查设备是否绑定到驱动程序
181     ///
182     /// ## 参数
183     ///
184     /// - `dev`: 设备
185     ///
186     /// ## 返回
187     ///
188     /// 如果传递的设备已成功完成对驱动程序的探测,则返回true,否则返回false。
189     pub fn device_is_bound(&self, dev: &Arc<dyn Device>) -> bool {
190         return driver_manager().driver_is_bound(dev);
191     }
192 
193     /// 把一个驱动绑定到设备上
194     ///
195     /// 允许手动绑定驱动到设备上。调用者需要设置好dev.driver(),保证其不为None
196     ///
197     /// ## 参数
198     ///
199     /// - `dev`: 设备
200     ///
201     /// ## 建议
202     ///
203     /// 使用device_manager().driver_attach()会更好
204     ///
205     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c#496
206     pub fn device_bind_driver(&self, dev: &Arc<dyn Device>) -> Result<(), SystemError> {
207         let r = driver_manager().driver_sysfs_add(dev);
208         if r.is_ok() {
209             self.device_links_force_bind(dev);
210             driver_manager().driver_bound(dev);
211         } else if let Some(bus) = dev.bus().and_then(|bus| bus.upgrade()) {
212             bus.subsystem().bus_notifier().call_chain(
213                 BusNotifyEvent::DriverNotBound,
214                 Some(dev),
215                 None,
216             );
217         }
218 
219         if let Err(e) = r.as_ref() {
220             error!(
221                 "device_bind_driver: driver_sysfs_add failed, dev: '{}', err: {:?}",
222                 dev.name(),
223                 e
224             );
225         }
226         return r;
227     }
228 
229     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c?fi=driver_attach#528
230     fn unbind_cleanup(&self, dev: &Arc<dyn Device>) {
231         dev.set_driver(None);
232         // todo: 添加更多操作,清理数据
233     }
234 }
235 
236 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c#866
237 #[derive(Debug)]
238 #[allow(dead_code)]
239 struct DeviceAttachData {
240     dev: Arc<dyn Device>,
241 
242     ///  Indicates whether we are considering asynchronous probing or
243     ///  not. Only initial binding after device or driver registration
244     ///  (including deferral processing) may be done asynchronously, the
245     ///  rest is always synchronous, as we expect it is being done by
246     ///  request from userspace.
247     check_async: bool,
248 
249     /// Indicates if we are binding synchronous or asynchronous drivers.
250     /// When asynchronous probing is enabled we'll execute 2 passes
251     /// over drivers: first pass doing synchronous probing and second
252     /// doing asynchronous probing (if synchronous did not succeed -
253     /// most likely because there was no driver requiring synchronous
254     /// probing - and we found asynchronous driver during first pass).
255     /// The 2 passes are done because we can't shoot asynchronous
256     /// probe for given device and driver from bus_for_each_drv() since
257     /// driver pointer is not guaranteed to stay valid once
258     /// bus_for_each_drv() iterates to the next driver on the bus.
259     want_async: bool,
260 
261     /// We'll set have_async to 'true' if, while scanning for matching
262     /// driver, we'll encounter one that requests asynchronous probing.
263     have_async: bool,
264 }
265 
266 impl DeviceAttachData {
267     pub fn new(dev: Arc<dyn Device>, check_async: bool, want_async: bool) -> Self {
268         Self {
269             dev,
270             check_async,
271             want_async,
272             have_async: false,
273         }
274     }
275 
276     #[allow(dead_code)]
277     #[inline(always)]
278     fn set_have_async(&mut self) {
279         self.have_async = true;
280     }
281 }
282 
283 impl DriverManager {
284     /// 尝试把驱动绑定到现有的设备上
285     ///
286     /// 这个函数会遍历驱动现有的全部设备,然后尝试把他们匹配。
287     /// 一旦有一个设备匹配成功,就会返回,并且设备的driver字段会被设置。
288     pub fn driver_attach(&self, driver: &Arc<dyn Driver>) -> Result<(), SystemError> {
289         let bus = driver
290             .bus()
291             .and_then(|bus| bus.upgrade())
292             .ok_or(SystemError::EINVAL)?;
293         for dev in bus.subsystem().devices().iter() {
294             self.do_driver_attach(dev, driver);
295         }
296 
297         return Ok(());
298     }
299 
300     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c?fi=driver_attach#1134
301     #[inline(never)]
302     fn do_driver_attach(&self, device: &Arc<dyn Device>, driver: &Arc<dyn Driver>) -> bool {
303         let r = self.match_device(driver, device).unwrap_or(false);
304         if !r {
305             // 不匹配
306             return false;
307         }
308 
309         if driver.allows_async_probing() {
310             unimplemented!(
311                 "do_driver_attach: probe driver '{}' asynchronously",
312                 driver.name()
313             );
314         }
315 
316         if self.probe_device(driver, device).is_err() {
317             return false;
318         }
319 
320         return true;
321     }
322 
323     #[inline(always)]
324     pub fn match_device(
325         &self,
326         driver: &Arc<dyn Driver>,
327         device: &Arc<dyn Device>,
328     ) -> Result<bool, SystemError> {
329         return driver
330             .bus()
331             .and_then(|bus| bus.upgrade())
332             .unwrap()
333             .match_device(device, driver);
334     }
335 
336     /// 尝试把设备和驱动绑定在一起
337     ///
338     ///
339     /// ## 返回
340     ///
341     /// - Ok(): 绑定成功
342     /// - Err(ENODEV): 设备未注册
343     /// - Err(EBUSY): 设备已经绑定到驱动上
344     ///
345     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c?fi=driver_attach#802
346     fn probe_device(
347         &self,
348         driver: &Arc<dyn Driver>,
349         device: &Arc<dyn Device>,
350     ) -> Result<(), SystemError> {
351         let r = self.do_probe_device(driver, device);
352         PROBE_WAIT_QUEUE.wakeup_all(None);
353         return r;
354     }
355 
356     fn do_probe_device(
357         &self,
358         driver: &Arc<dyn Driver>,
359         device: &Arc<dyn Device>,
360     ) -> Result<(), SystemError> {
361         if device.is_dead() || (!device.is_registered()) {
362             return Err(SystemError::ENODEV);
363         }
364         if device.driver().is_some() {
365             return Err(SystemError::EBUSY);
366         }
367 
368         device.set_can_match(true);
369 
370         self.really_probe(driver, device)?;
371 
372         return Ok(());
373     }
374 
375     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c?fi=driver_attach#584
376     fn really_probe(
377         &self,
378         driver: &Arc<dyn Driver>,
379         device: &Arc<dyn Device>,
380     ) -> Result<(), SystemError> {
381         let bind_failed = || {
382             device_manager().unbind_cleanup(device);
383         };
384 
385         let sysfs_failed = || {
386             if let Some(bus) = device.bus().and_then(|bus| bus.upgrade()) {
387                 bus.subsystem().bus_notifier().call_chain(
388                     BusNotifyEvent::DriverNotBound,
389                     Some(device),
390                     None,
391                 );
392             }
393         };
394 
395         let probe_failed = || {
396             self.remove_from_sysfs(device);
397         };
398 
399         let dev_groups_failed = || {
400             device_manager().remove(device);
401         };
402 
403         device.set_driver(Some(Arc::downgrade(driver)));
404 
405         self.add_to_sysfs(device).map_err(|e| {
406             error!(
407                 "really_probe: add_to_sysfs failed, dev: '{}', err: {:?}",
408                 device.name(),
409                 e
410             );
411             sysfs_failed();
412             bind_failed();
413             e
414         })?;
415 
416         self.call_driver_probe(device, driver).map_err(|e| {
417             error!(
418                 "really_probe: call_driver_probe failed, dev: '{}', err: {:?}",
419                 device.name(),
420                 e
421             );
422 
423             probe_failed();
424             sysfs_failed();
425             bind_failed();
426             e
427         })?;
428 
429         device_manager()
430             .add_groups(device, driver.dev_groups())
431             .map_err(|e| {
432                 error!(
433                     "really_probe: add_groups failed, dev: '{}', err: {:?}",
434                     device.name(),
435                     e
436                 );
437                 dev_groups_failed();
438                 probe_failed();
439                 sysfs_failed();
440                 bind_failed();
441                 e
442             })?;
443 
444         // 我们假设所有的设备都有 sync_state 这个属性。如果没有的话,也创建属性文件。
445         device_manager()
446             .create_file(device, &DeviceAttrStateSynced)
447             .map_err(|e| {
448                 error!(
449                     "really_probe: create_file failed, dev: '{}', err: {:?}",
450                     device.name(),
451                     e
452                 );
453                 dev_groups_failed();
454                 probe_failed();
455                 sysfs_failed();
456                 bind_failed();
457                 e
458             })?;
459 
460         self.driver_bound(device);
461 
462         return Ok(());
463     }
464 
465     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c?fi=driver_attach#434
466     fn add_to_sysfs(&self, device: &Arc<dyn Device>) -> Result<(), SystemError> {
467         let driver = device.driver().ok_or(SystemError::EINVAL)?;
468 
469         if let Some(bus) = device.bus().and_then(|bus| bus.upgrade()) {
470             bus.subsystem().bus_notifier().call_chain(
471                 BusNotifyEvent::BindDriver,
472                 Some(device),
473                 None,
474             );
475         }
476 
477         let driver_kobj = driver.clone() as Arc<dyn KObject>;
478         let device_kobj = device.clone() as Arc<dyn KObject>;
479 
480         sysfs_instance().create_link(Some(&driver_kobj), &device_kobj, device.name())?;
481 
482         let fail_rm_dev_link = || {
483             sysfs_instance().remove_link(&driver_kobj, device.name());
484         };
485 
486         sysfs_instance()
487             .create_link(Some(&device_kobj), &driver_kobj, "driver".to_string())
488             .inspect_err(|_e| {
489                 fail_rm_dev_link();
490             })?;
491 
492         device_manager()
493             .create_file(device, &DeviceAttrCoredump)
494             .inspect_err(|_e| {
495                 sysfs_instance().remove_link(&device_kobj, "driver".to_string());
496                 fail_rm_dev_link();
497             })?;
498 
499         return Ok(());
500     }
501 
502     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c?fi=driver_attach#469
503     fn remove_from_sysfs(&self, _device: &Arc<dyn Device>) {
504         todo!("remove_from_sysfs")
505     }
506 
507     fn call_driver_probe(
508         &self,
509         device: &Arc<dyn Device>,
510         driver: &Arc<dyn Driver>,
511     ) -> Result<(), SystemError> {
512         let bus = device
513             .bus()
514             .and_then(|bus| bus.upgrade())
515             .ok_or(SystemError::EINVAL)?;
516         let r = bus.probe(device);
517         if r == Err(SystemError::ENOSYS) {
518             error!(
519                 "call_driver_probe: bus.probe() failed, dev: '{}', err: {:?}",
520                 device.name(),
521                 r
522             );
523             return r;
524         }
525 
526         if r.is_ok() {
527             return Ok(());
528         }
529 
530         let err = r.unwrap_err();
531         match err {
532             SystemError::ENODEV | SystemError::ENXIO => {
533                 debug!(
534                     "driver'{}': probe of {} rejects match {:?}",
535                     driver.name(),
536                     device.name(),
537                     err
538                 );
539             }
540 
541             _ => {
542                 warn!(
543                     "driver'{}': probe of {} failed with error {:?}",
544                     driver.name(),
545                     device.name(),
546                     err
547                 );
548             }
549         }
550 
551         return Err(err);
552     }
553 
554     /// 当设备被成功探测,进行了'设备->驱动'绑定后,调用这个函数,完成'驱动->设备'的绑定
555     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/dd.c#393
556     fn driver_bound(&self, device: &Arc<dyn Device>) {
557         if self.driver_is_bound(device) {
558             warn!("driver_bound: device '{}' is already bound.", device.name());
559             return;
560         }
561 
562         let driver = device.driver().unwrap();
563         driver.add_device(device.clone());
564 
565         if let Some(bus) = device.bus().and_then(|bus| bus.upgrade()) {
566             bus.subsystem().bus_notifier().call_chain(
567                 BusNotifyEvent::BoundDriver,
568                 Some(device),
569                 None,
570             );
571         }
572 
573         // todo: 发送kobj bind的uevent
574     }
575 
576     fn driver_is_bound(&self, device: &Arc<dyn Device>) -> bool {
577         if let Some(driver) = device.driver() {
578             if driver.find_device_by_name(&device.name()).is_some() {
579                 return true;
580             }
581         }
582 
583         return false;
584     }
585 }
586 
587 /// 设备文件夹下的`dev`文件的属性
588 #[derive(Debug, Clone, Copy)]
589 pub struct DeviceAttrStateSynced;
590 
591 impl Attribute for DeviceAttrStateSynced {
592     fn mode(&self) -> ModeType {
593         // 0o444
594         return ModeType::S_IRUGO;
595     }
596 
597     fn name(&self) -> &str {
598         "state_synced"
599     }
600 
601     fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
602         let dev = kobj.cast::<dyn Device>().map_err(|kobj| {
603             error!(
604                 "Intertrait casting not implemented for kobj: {}",
605                 kobj.name()
606             );
607             SystemError::ENOSYS
608         })?;
609 
610         let val = dev.state_synced();
611         let val = if val { 1 } else { 0 };
612         return sysfs_emit_str(buf, format!("{}\n", val).as_str());
613     }
614 
615     fn support(&self) -> SysFSOpsSupport {
616         SysFSOpsSupport::ATTR_SHOW
617     }
618 }
619 
620 #[derive(Debug)]
621 pub(super) struct DeviceAttrCoredump;
622 
623 impl Attribute for DeviceAttrCoredump {
624     fn name(&self) -> &str {
625         "coredump"
626     }
627 
628     fn mode(&self) -> ModeType {
629         SYSFS_ATTR_MODE_WO
630     }
631 
632     fn support(&self) -> SysFSOpsSupport {
633         SysFSOpsSupport::ATTR_STORE
634     }
635 
636     fn store(&self, kobj: Arc<dyn KObject>, buf: &[u8]) -> Result<usize, SystemError> {
637         let dev = kobj.cast::<dyn Device>().map_err(|kobj| {
638             error!(
639                 "Intertrait casting not implemented for kobj: {}",
640                 kobj.name()
641             );
642             SystemError::ENOSYS
643         })?;
644 
645         let drv = dev.driver().ok_or(SystemError::EINVAL)?;
646         drv.coredump(&dev)?;
647 
648         return Ok(buf.len());
649     }
650 }
651