Serenity Operating System
1/*
2 * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/IntrusiveList.h>
10#include <Kernel/Devices/BlockDevice.h>
11#include <Kernel/Interrupts/IRQHandler.h>
12#include <Kernel/Locking/Mutex.h>
13#include <Kernel/Storage/DiskPartition.h>
14#include <Kernel/Storage/StorageController.h>
15
16namespace Kernel {
17
18class RamdiskDevice;
19class StorageDevice : public BlockDevice {
20 friend class StorageManagement;
21 friend class DeviceManagement;
22
23public:
24 // Note: this attribute describes the internal command set of a Storage device.
25 // For example, an ordinary harddrive utilizes the ATA command set, while
26 // an ATAPI device (e.g. Optical drive) that is connected to the ATA bus,
27 // is actually using SCSI commands (packets) encapsulated inside an ATA command.
28 // The IDE controller code being aware of the possibility of ATAPI devices attached
29 // to the ATA bus, will check whether the Command set is ATA or SCSI and will act
30 // accordingly.
31 // Note: For now, there's simply no distinction between the interface type and the commandset.
32 // As mentioned above, ATAPI devices use the ATA interface with actual SCSI packets so
33 // the commandset is SCSI while the interface type is ATA. We simply don't support SCSI over ATA (ATAPI)
34 // and ATAPI is the exception to no-distinction rule. If we ever put SCSI support in the kernel,
35 // we can create another enum class to put the distinction.
36 enum class CommandSet {
37 PlainMemory,
38 SCSI,
39 ATA,
40 NVMe,
41 };
42
43 // Note: The most reliable way to address this device from userspace interfaces,
44 // such as SysFS, is to have one way to enumerate everything in the eyes of userspace.
45 // Therefore, SCSI LUN (logical unit number) addressing seem to be the most generic way to do this.
46 // For example, on a legacy ATA instance, one might connect an harddrive to the second IDE controller,
47 // to the Primary channel as a slave device, which translates to LUN 1:0:1.
48 // On NVMe, for example, connecting a second PCIe NVMe storage device as a sole NVMe namespace translates
49 // to LUN 1:1:0.
50 struct LUNAddress {
51 u32 controller_id;
52 u32 target_id;
53 u32 disk_id;
54 };
55
56public:
57 virtual u64 max_addressable_block() const { return m_max_addressable_block; }
58
59 // ^BlockDevice
60 virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
61 virtual bool can_read(OpenFileDescription const&, u64) const override;
62 virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override;
63 virtual bool can_write(OpenFileDescription const&, u64) const override;
64 virtual void prepare_for_unplug() { m_partitions.clear(); }
65
66 Vector<NonnullLockRefPtr<DiskPartition>> const& partitions() const { return m_partitions; }
67
68 void add_partition(NonnullLockRefPtr<DiskPartition> disk_partition) { MUST(m_partitions.try_append(disk_partition)); }
69
70 LUNAddress const& logical_unit_number_address() const { return m_logical_unit_number_address; }
71
72 u32 parent_controller_hardware_relative_id() const { return m_hardware_relative_controller_id; }
73
74 virtual CommandSet command_set() const = 0;
75
76 StringView command_set_to_string_view() const;
77
78 // ^File
79 virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) final;
80
81protected:
82 StorageDevice(LUNAddress, u32 hardware_relative_controller_id, size_t sector_size, u64);
83
84 // Note: We want to be able to put distinction between Storage devices and Ramdisk-based devices.
85 // We do this because it will make selecting ramdisk devices much more easier in boot time in the kernel commandline.
86 StorageDevice(Badge<RamdiskDevice>, LUNAddress, u32 hardware_relative_controller_id, MajorNumber, MinorNumber, size_t sector_size, u64);
87
88 // ^DiskDevice
89 virtual StringView class_name() const override;
90
91private:
92 virtual ErrorOr<void> after_inserting() override;
93 virtual void will_be_destroyed() override;
94
95 mutable IntrusiveListNode<StorageDevice, LockRefPtr<StorageDevice>> m_list_node;
96 Vector<NonnullLockRefPtr<DiskPartition>> m_partitions;
97
98 LUNAddress const m_logical_unit_number_address;
99
100 // Note: This data member should be used with LUNAddress target_id and disk_id.
101 // LUNs are agnostic system-wide addresses, so they are assigned without caring about the specific hardware interfaces.
102 // This class member on the other side, is meant to be assigned *per hardware type*,
103 // which means in contrast to the LUNAddress controller_id struct member, we take the index of the hardware
104 // controller among its fellow controllers of the same hardware type in the system.
105 u32 const m_hardware_relative_controller_id { 0 };
106
107 u64 m_max_addressable_block { 0 };
108 size_t m_blocks_per_page { 0 };
109};
110
111}