xref: /DragonOS/kernel/src/driver/disk/ahci/ahcidisk.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
1 use super::{_port, hba::HbaCmdTable, virt_2_phys};
2 use crate::driver::base::block::block_device::{BlockDevice, BlockId};
3 use crate::driver::base::block::disk_info::Partition;
4 use crate::driver::base::class::Class;
5 use crate::driver::base::device::bus::Bus;
6 
7 use crate::driver::base::device::driver::Driver;
8 use crate::driver::base::device::{Device, DeviceType, IdTable};
9 use crate::driver::base::kobject::{KObjType, KObject, KObjectState};
10 use crate::driver::base::kset::KSet;
11 use crate::driver::disk::ahci::HBA_PxIS_TFES;
12 
13 use crate::filesystem::kernfs::KernFSInode;
14 use crate::filesystem::mbr::MbrDiskPartionTable;
15 
16 use crate::driver::disk::ahci::hba::{
17     FisRegH2D, FisType, HbaCmdHeader, ATA_CMD_READ_DMA_EXT, ATA_CMD_WRITE_DMA_EXT, ATA_DEV_BUSY,
18     ATA_DEV_DRQ,
19 };
20 use crate::libs::rwlock::{RwLockReadGuard, RwLockWriteGuard};
21 use crate::libs::spinlock::SpinLock;
22 use crate::mm::{phys_2_virt, verify_area, VirtAddr};
23 use log::error;
24 use system_error::SystemError;
25 
26 use alloc::sync::Weak;
27 use alloc::{string::String, sync::Arc, vec::Vec};
28 
29 use core::fmt::Debug;
30 use core::sync::atomic::{compiler_fence, Ordering};
31 use core::{mem::size_of, ptr::write_bytes};
32 
33 /// @brief: 只支持MBR分区格式的磁盘结构体
34 pub struct AhciDisk {
35     pub name: String,
36     pub flags: u16,                      // 磁盘的状态flags
37     pub partitions: Vec<Arc<Partition>>, // 磁盘分区数组
38     // port: &'static mut HbaPort,      // 控制硬盘的端口
39     pub ctrl_num: u8,
40     pub port_num: u8,
41     /// 指向LockAhciDisk的弱引用
42     self_ref: Weak<LockedAhciDisk>,
43 }
44 
45 /// @brief: 带锁的AhciDisk
46 #[derive(Debug)]
47 pub struct LockedAhciDisk(pub SpinLock<AhciDisk>);
48 /// 函数实现
49 impl Debug for AhciDisk {
50     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51         write!(
52             f,
53             "{{ name: {}, flags: {}, part_s: {:?} }}",
54             self.name, self.flags, self.partitions
55         )?;
56         return Ok(());
57     }
58 }
59 
60 impl AhciDisk {
61     fn read_at(
62         &self,
63         lba_id_start: BlockId, // 起始lba编号
64         count: usize,          // 读取lba的数量
65         buf: &mut [u8],
66     ) -> Result<usize, SystemError> {
67         assert!((buf.len() & 511) == 0);
68         compiler_fence(Ordering::SeqCst);
69         let check_length = ((count - 1) >> 4) + 1; // prdt length
70         if count * 512 > buf.len() || check_length > 8_usize {
71             error!("ahci read: e2big");
72             // 不可能的操作
73             return Err(SystemError::E2BIG);
74         } else if count == 0 {
75             return Ok(0);
76         }
77 
78         let port = _port(self.ctrl_num, self.port_num);
79         volatile_write!(port.is, u32::MAX); // Clear pending interrupt bits
80 
81         let slot = port.find_cmdslot().unwrap_or(u32::MAX);
82 
83         if slot == u32::MAX {
84             return Err(SystemError::EIO);
85         }
86 
87         #[allow(unused_unsafe)]
88         let cmdheader: &mut HbaCmdHeader = unsafe {
89             (phys_2_virt(
90                 volatile_read!(port.clb) as usize + slot as usize * size_of::<HbaCmdHeader>(),
91             ) as *mut HbaCmdHeader)
92                 .as_mut()
93                 .unwrap()
94         };
95 
96         cmdheader.cfl = (size_of::<FisRegH2D>() / size_of::<u32>()) as u8;
97 
98         volatile_set_bit!(cmdheader.cfl, 1 << 6, false); //  Read/Write bit : Read from device
99         volatile_write!(cmdheader.prdtl, check_length as u16); // PRDT entries count
100 
101         // 设置数据存放地址
102         let mut buf_ptr = buf as *mut [u8] as *mut usize as usize;
103 
104         // 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间
105         // TODO:在内存管理重构后,可以直接使用用户空间的内存地址
106 
107         let user_buf = verify_area(VirtAddr::new(buf_ptr), buf.len()).is_ok();
108         let mut kbuf = if user_buf {
109             let x: Vec<u8> = vec![0; buf.len()];
110             Some(x)
111         } else {
112             None
113         };
114 
115         if kbuf.is_some() {
116             buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize;
117         }
118 
119         #[allow(unused_unsafe)]
120         let cmdtbl = unsafe {
121             (phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable)
122                 .as_mut()
123                 .unwrap() // 必须使用 as_mut ,得到的才是原来的变量
124         };
125         let mut tmp_count = count;
126 
127         unsafe {
128             // 清空整个table的旧数据
129             write_bytes(cmdtbl, 0, 1);
130         }
131         // debug!("cmdheader.prdtl={}", volatile_read!(cmdheader.prdtl));
132 
133         // 8K bytes (16 sectors) per PRDT
134         for i in 0..((volatile_read!(cmdheader.prdtl) - 1) as usize) {
135             volatile_write!(cmdtbl.prdt_entry[i].dba, virt_2_phys(buf_ptr) as u64);
136             cmdtbl.prdt_entry[i].dbc = 8 * 1024 - 1;
137             volatile_set_bit!(cmdtbl.prdt_entry[i].dbc, 1 << 31, true); // 允许中断 prdt_entry.i
138             buf_ptr += 8 * 1024;
139             tmp_count -= 16;
140         }
141 
142         // Last entry
143         let las = (volatile_read!(cmdheader.prdtl) - 1) as usize;
144         volatile_write!(cmdtbl.prdt_entry[las].dba, virt_2_phys(buf_ptr) as u64);
145         cmdtbl.prdt_entry[las].dbc = ((tmp_count << 9) - 1) as u32; // 数据长度
146 
147         volatile_set_bit!(cmdtbl.prdt_entry[las].dbc, 1 << 31, true); // 允许中断
148 
149         // 设置命令
150         let cmdfis = unsafe {
151             ((&mut cmdtbl.cfis) as *mut [u8] as *mut usize as *mut FisRegH2D)
152                 .as_mut()
153                 .unwrap()
154         };
155         volatile_write!(cmdfis.fis_type, FisType::RegH2D as u8);
156         volatile_set_bit!(cmdfis.pm, 1 << 7, true); // command_bit set
157         volatile_write!(cmdfis.command, ATA_CMD_READ_DMA_EXT);
158 
159         volatile_write!(cmdfis.lba0, (lba_id_start & 0xFF) as u8);
160         volatile_write!(cmdfis.lba1, ((lba_id_start >> 8) & 0xFF) as u8);
161         volatile_write!(cmdfis.lba2, ((lba_id_start >> 16) & 0xFF) as u8);
162         volatile_write!(cmdfis.lba3, ((lba_id_start >> 24) & 0xFF) as u8);
163         volatile_write!(cmdfis.lba4, ((lba_id_start >> 32) & 0xFF) as u8);
164         volatile_write!(cmdfis.lba5, ((lba_id_start >> 40) & 0xFF) as u8);
165 
166         volatile_write!(cmdfis.countl, (count & 0xFF) as u8);
167         volatile_write!(cmdfis.counth, ((count >> 8) & 0xFF) as u8);
168 
169         volatile_write!(cmdfis.device, 1 << 6); // LBA Mode
170 
171         // 等待之前的操作完成
172         let mut spin_count = 0;
173         const SPIN_LIMIT: u32 = 10000;
174 
175         while (volatile_read!(port.tfd) as u8 & (ATA_DEV_BUSY | ATA_DEV_DRQ)) > 0
176             && spin_count < SPIN_LIMIT
177         {
178             spin_count += 1;
179         }
180 
181         if spin_count == SPIN_LIMIT {
182             error!("Port is hung");
183             return Err(SystemError::EIO);
184         }
185 
186         volatile_set_bit!(port.ci, 1 << slot, true); // Issue command
187                                                      // debug!("To wait ahci read complete.");
188                                                      // 等待操作完成
189         loop {
190             if (volatile_read!(port.ci) & (1 << slot)) == 0 {
191                 break;
192             }
193             if (volatile_read!(port.is) & HBA_PxIS_TFES) > 0 {
194                 error!("Read disk error");
195                 return Err(SystemError::EIO);
196             }
197         }
198         if let Some(kbuf) = &kbuf {
199             buf.copy_from_slice(kbuf);
200         }
201 
202         compiler_fence(Ordering::SeqCst);
203         // successfully read
204         return Ok(count * 512);
205     }
206 
207     fn write_at(
208         &self,
209         lba_id_start: BlockId,
210         count: usize,
211         buf: &[u8],
212     ) -> Result<usize, SystemError> {
213         assert!((buf.len() & 511) == 0);
214         compiler_fence(Ordering::SeqCst);
215         let check_length = ((count - 1) >> 4) + 1; // prdt length
216         if count * 512 > buf.len() || check_length > 8 {
217             // 不可能的操作
218             return Err(SystemError::E2BIG);
219         } else if count == 0 {
220             return Ok(0);
221         }
222 
223         let port = _port(self.ctrl_num, self.port_num);
224 
225         volatile_write!(port.is, u32::MAX); // Clear pending interrupt bits
226 
227         let slot = port.find_cmdslot().unwrap_or(u32::MAX);
228 
229         if slot == u32::MAX {
230             return Err(SystemError::EIO);
231         }
232 
233         compiler_fence(Ordering::SeqCst);
234         #[allow(unused_unsafe)]
235         let cmdheader: &mut HbaCmdHeader = unsafe {
236             (phys_2_virt(
237                 volatile_read!(port.clb) as usize + slot as usize * size_of::<HbaCmdHeader>(),
238             ) as *mut HbaCmdHeader)
239                 .as_mut()
240                 .unwrap()
241         };
242         compiler_fence(Ordering::SeqCst);
243 
244         volatile_write_bit!(
245             cmdheader.cfl,
246             (1 << 5) - 1_u8,
247             (size_of::<FisRegH2D>() / size_of::<u32>()) as u8
248         ); // Command FIS size
249 
250         volatile_set_bit!(cmdheader.cfl, 7 << 5, true); // (p,c,w)都设置为1, Read/Write bit :  Write from device
251         volatile_write!(cmdheader.prdtl, check_length as u16); // PRDT entries count
252 
253         // 设置数据存放地址
254         compiler_fence(Ordering::SeqCst);
255         let mut buf_ptr = buf as *const [u8] as *mut usize as usize;
256 
257         // 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间
258         // TODO:在内存管理重构后,可以直接使用用户空间的内存地址
259         let user_buf = verify_area(VirtAddr::new(buf_ptr), buf.len()).is_ok();
260         let mut kbuf = if user_buf {
261             let mut x: Vec<u8> = vec![0; buf.len()];
262             x.resize(buf.len(), 0);
263             x.copy_from_slice(buf);
264             Some(x)
265         } else {
266             None
267         };
268 
269         if kbuf.is_some() {
270             buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize;
271         }
272 
273         #[allow(unused_unsafe)]
274         let cmdtbl = unsafe {
275             (phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable)
276                 .as_mut()
277                 .unwrap()
278         };
279         let mut tmp_count = count;
280         compiler_fence(Ordering::SeqCst);
281 
282         unsafe {
283             // 清空整个table的旧数据
284             write_bytes(cmdtbl, 0, 1);
285         }
286 
287         // 8K bytes (16 sectors) per PRDT
288         for i in 0..((volatile_read!(cmdheader.prdtl) - 1) as usize) {
289             volatile_write!(cmdtbl.prdt_entry[i].dba, virt_2_phys(buf_ptr) as u64);
290             volatile_write_bit!(cmdtbl.prdt_entry[i].dbc, (1 << 22) - 1, 8 * 1024 - 1); // 数据长度
291             volatile_set_bit!(cmdtbl.prdt_entry[i].dbc, 1 << 31, true); // 允许中断
292             buf_ptr += 8 * 1024;
293             tmp_count -= 16;
294         }
295 
296         // Last entry
297         let las = (volatile_read!(cmdheader.prdtl) - 1) as usize;
298         volatile_write!(cmdtbl.prdt_entry[las].dba, virt_2_phys(buf_ptr) as u64);
299         volatile_set_bit!(cmdtbl.prdt_entry[las].dbc, 1 << 31, true); // 允许中断
300         volatile_write_bit!(
301             cmdtbl.prdt_entry[las].dbc,
302             (1 << 22) - 1,
303             ((tmp_count << 9) - 1) as u32
304         ); // 数据长度
305 
306         // 设置命令
307         let cmdfis = unsafe {
308             ((&mut cmdtbl.cfis) as *mut [u8] as *mut usize as *mut FisRegH2D)
309                 .as_mut()
310                 .unwrap()
311         };
312         volatile_write!(cmdfis.fis_type, FisType::RegH2D as u8);
313         volatile_set_bit!(cmdfis.pm, 1 << 7, true); // command_bit set
314         volatile_write!(cmdfis.command, ATA_CMD_WRITE_DMA_EXT);
315 
316         volatile_write!(cmdfis.lba0, (lba_id_start & 0xFF) as u8);
317         volatile_write!(cmdfis.lba1, ((lba_id_start >> 8) & 0xFF) as u8);
318         volatile_write!(cmdfis.lba2, ((lba_id_start >> 16) & 0xFF) as u8);
319         volatile_write!(cmdfis.lba3, ((lba_id_start >> 24) & 0xFF) as u8);
320         volatile_write!(cmdfis.lba4, ((lba_id_start >> 32) & 0xFF) as u8);
321         volatile_write!(cmdfis.lba5, ((lba_id_start >> 40) & 0xFF) as u8);
322 
323         volatile_write!(cmdfis.countl, (count & 0xFF) as u8);
324         volatile_write!(cmdfis.counth, ((count >> 8) & 0xFF) as u8);
325 
326         volatile_write!(cmdfis.device, 1 << 6); // LBA Mode
327 
328         volatile_set_bit!(port.ci, 1 << slot, true); // Issue command
329 
330         // 等待操作完成
331         loop {
332             if (volatile_read!(port.ci) & (1 << slot)) == 0 {
333                 break;
334             }
335             if (volatile_read!(port.is) & HBA_PxIS_TFES) > 0 {
336                 error!("Write disk error");
337                 return Err(SystemError::EIO);
338             }
339         }
340 
341         compiler_fence(Ordering::SeqCst);
342         // successfully read
343         return Ok(count * 512);
344     }
345 
346     fn sync(&self) -> Result<(), SystemError> {
347         // 由于目前没有block cache, 因此sync返回成功即可
348         return Ok(());
349     }
350 }
351 
352 impl LockedAhciDisk {
353     pub fn new(
354         name: String,
355         flags: u16,
356         ctrl_num: u8,
357         port_num: u8,
358     ) -> Result<Arc<LockedAhciDisk>, SystemError> {
359         // 构建磁盘结构体
360         let result: Arc<LockedAhciDisk> = Arc::new_cyclic(|self_ref| {
361             LockedAhciDisk(SpinLock::new(AhciDisk {
362                 name,
363                 flags,
364                 partitions: Default::default(),
365                 ctrl_num,
366                 port_num,
367                 self_ref: self_ref.clone(),
368             }))
369         });
370         let table: MbrDiskPartionTable = result.read_mbr_table()?;
371 
372         // 求出有多少可用分区
373         let partitions = table.partitions(Arc::downgrade(&result) as Weak<dyn BlockDevice>);
374         result.0.lock().partitions = partitions;
375 
376         return Ok(result);
377     }
378 
379     /// @brief: 从磁盘中读取 MBR 分区表结构体
380     pub fn read_mbr_table(&self) -> Result<MbrDiskPartionTable, SystemError> {
381         let disk = self.0.lock().self_ref.upgrade().unwrap() as Arc<dyn BlockDevice>;
382         MbrDiskPartionTable::from_disk(disk)
383     }
384 }
385 
386 impl KObject for LockedAhciDisk {
387     fn as_any_ref(&self) -> &dyn core::any::Any {
388         self
389     }
390 
391     fn inode(&self) -> Option<Arc<KernFSInode>> {
392         todo!()
393     }
394 
395     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
396         todo!()
397     }
398 
399     fn kset(&self) -> Option<Arc<KSet>> {
400         todo!()
401     }
402 
403     fn parent(&self) -> Option<Weak<dyn KObject>> {
404         todo!()
405     }
406 
407     fn set_inode(&self, _inode: Option<Arc<KernFSInode>>) {
408         todo!()
409     }
410 
411     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
412         todo!()
413     }
414 
415     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
416         todo!()
417     }
418 
419     fn set_kobj_state(&self, _state: KObjectState) {
420         todo!()
421     }
422 
423     fn name(&self) -> alloc::string::String {
424         todo!()
425     }
426 
427     fn set_name(&self, _name: alloc::string::String) {
428         todo!()
429     }
430 
431     fn set_kset(&self, _kset: Option<Arc<KSet>>) {
432         todo!()
433     }
434 
435     fn set_parent(&self, _parent: Option<Weak<dyn KObject>>) {
436         todo!()
437     }
438 
439     fn set_kobj_type(&self, _ktype: Option<&'static dyn KObjType>) {
440         todo!()
441     }
442 }
443 
444 impl Device for LockedAhciDisk {
445     fn dev_type(&self) -> DeviceType {
446         return DeviceType::Block;
447     }
448 
449     fn id_table(&self) -> IdTable {
450         todo!()
451     }
452 
453     fn bus(&self) -> Option<Weak<dyn Bus>> {
454         todo!("LockedAhciDisk::bus()")
455     }
456 
457     fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) {
458         todo!("LockedAhciDisk::set_bus()")
459     }
460 
461     fn driver(&self) -> Option<Arc<dyn Driver>> {
462         todo!("LockedAhciDisk::driver()")
463     }
464 
465     fn is_dead(&self) -> bool {
466         false
467     }
468 
469     fn set_driver(&self, _driver: Option<Weak<dyn Driver>>) {
470         todo!("LockedAhciDisk::set_driver()")
471     }
472 
473     fn can_match(&self) -> bool {
474         todo!()
475     }
476 
477     fn set_can_match(&self, _can_match: bool) {
478         todo!()
479     }
480 
481     fn state_synced(&self) -> bool {
482         todo!()
483     }
484 
485     fn set_class(&self, _class: Option<Weak<dyn Class>>) {
486         todo!()
487     }
488 }
489 
490 impl BlockDevice for LockedAhciDisk {
491     #[inline]
492     fn as_any_ref(&self) -> &dyn core::any::Any {
493         self
494     }
495 
496     #[inline]
497     fn blk_size_log2(&self) -> u8 {
498         9
499     }
500 
501     fn sync(&self) -> Result<(), SystemError> {
502         return self.0.lock().sync();
503     }
504 
505     #[inline]
506     fn device(&self) -> Arc<dyn Device> {
507         return self.0.lock().self_ref.upgrade().unwrap();
508     }
509 
510     fn block_size(&self) -> usize {
511         todo!()
512     }
513 
514     fn partitions(&self) -> Vec<Arc<Partition>> {
515         return self.0.lock().partitions.clone();
516     }
517 
518     #[inline]
519     fn read_at_sync(
520         &self,
521         lba_id_start: BlockId, // 起始lba编号
522         count: usize,          // 读取lba的数量
523         buf: &mut [u8],
524     ) -> Result<usize, SystemError> {
525         self.0.lock().read_at(lba_id_start, count, buf)
526     }
527 
528     #[inline]
529     fn write_at_sync(
530         &self,
531         lba_id_start: BlockId,
532         count: usize,
533         buf: &[u8],
534     ) -> Result<usize, SystemError> {
535         self.0.lock().write_at(lba_id_start, count, buf)
536     }
537 }
538