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