From 7db6b77e7545f4224a4ebd2f0a97b753d291b4de Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 22 Apr 2022 16:15:41 +0300 Subject: [PATCH] Kernel: Export both interface type and command set of a StorageDevice --- Kernel/Storage/ATA/ATADiskDevice.h | 1 + Kernel/Storage/ATA/ATAPIDiscDevice.h | 1 + Kernel/Storage/NVMe/NVMeNameSpace.h | 1 + Kernel/Storage/Ramdisk/Device.h | 1 + Kernel/Storage/StorageDevice.cpp | 34 ++++++++++++++++++++++++++++ Kernel/Storage/StorageDevice.h | 16 +++++++++++++ 6 files changed, 54 insertions(+) diff --git a/Kernel/Storage/ATA/ATADiskDevice.h b/Kernel/Storage/ATA/ATADiskDevice.h index 4f08973ca82..5d611e01206 100644 --- a/Kernel/Storage/ATA/ATADiskDevice.h +++ b/Kernel/Storage/ATA/ATADiskDevice.h @@ -26,6 +26,7 @@ public: virtual CommandSet command_set() const override { return CommandSet::ATA; } private: + virtual InterfaceType interface_type() const override { return InterfaceType::ATA; } ATADiskDevice(ATAController const&, Address, MinorNumber, u16, u16, u64, NonnullOwnPtr); // ^DiskDevice diff --git a/Kernel/Storage/ATA/ATAPIDiscDevice.h b/Kernel/Storage/ATA/ATAPIDiscDevice.h index bfb80befe92..daed2e9c7e5 100644 --- a/Kernel/Storage/ATA/ATAPIDiscDevice.h +++ b/Kernel/Storage/ATA/ATAPIDiscDevice.h @@ -26,6 +26,7 @@ public: virtual CommandSet command_set() const override { return CommandSet::SCSI; } private: + virtual InterfaceType interface_type() const override { return InterfaceType::ATA; } ATAPIDiscDevice(ATAController const&, Address, MinorNumber, u16, u64, NonnullOwnPtr); // ^DiskDevice diff --git a/Kernel/Storage/NVMe/NVMeNameSpace.h b/Kernel/Storage/NVMe/NVMeNameSpace.h index b36cf2aa61f..1890d714600 100644 --- a/Kernel/Storage/NVMe/NVMeNameSpace.h +++ b/Kernel/Storage/NVMe/NVMeNameSpace.h @@ -29,6 +29,7 @@ public: void start_request(AsyncBlockDeviceRequest& request) override; private: + virtual InterfaceType interface_type() const override { return InterfaceType::NVMe; } u16 m_nsid; NonnullRefPtrVector m_queues; }; diff --git a/Kernel/Storage/Ramdisk/Device.h b/Kernel/Storage/Ramdisk/Device.h index 0e4ef6cca9b..dc398281a88 100644 --- a/Kernel/Storage/Ramdisk/Device.h +++ b/Kernel/Storage/Ramdisk/Device.h @@ -32,6 +32,7 @@ private: // ^StorageDevice virtual CommandSet command_set() const override { return CommandSet::PlainMemory; } + virtual InterfaceType interface_type() const override { return InterfaceType::PlainMemory; } Mutex m_lock { "RamdiskDevice"sv }; diff --git a/Kernel/Storage/StorageDevice.cpp b/Kernel/Storage/StorageDevice.cpp index 339b61cb313..6c8cc784cbb 100644 --- a/Kernel/Storage/StorageDevice.cpp +++ b/Kernel/Storage/StorageDevice.cpp @@ -27,6 +27,40 @@ StringView StorageDevice::class_name() const return "StorageDevice"sv; } +StringView StorageDevice::command_set_to_string_view() const +{ + switch (command_set()) { + case CommandSet::PlainMemory: + return "memory"sv; + case CommandSet::SCSI: + return "scsi"sv; + case CommandSet::ATA: + return "ata"sv; + case CommandSet::NVMe: + return "nvme"sv; + default: + break; + } + VERIFY_NOT_REACHED(); +} + +StringView StorageDevice::interface_type_to_string_view() const +{ + switch (interface_type()) { + case InterfaceType::PlainMemory: + return "memory"sv; + case InterfaceType::SCSI: + return "scsi"sv; + case InterfaceType::ATA: + return "ata"sv; + case InterfaceType::NVMe: + return "nvme"sv; + default: + break; + } + VERIFY_NOT_REACHED(); +} + ErrorOr StorageDevice::read(OpenFileDescription&, u64 offset, UserOrKernelBuffer& outbuf, size_t len) { u64 index = offset >> block_size_log(); diff --git a/Kernel/Storage/StorageDevice.h b/Kernel/Storage/StorageDevice.h index 2b178bc7441..a7747b1a119 100644 --- a/Kernel/Storage/StorageDevice.h +++ b/Kernel/Storage/StorageDevice.h @@ -33,6 +33,18 @@ public: NVMe, }; + // Note: this attribute describes the interface type of a Storage device. + // For example, an ordinary harddrive utilizes the ATA command set, while + // an ATAPI device (e.g. Optical drive) that is connected to the ATA bus, + // is actually using SCSI commands (packets) encapsulated inside an ATA command. + // Therefore, an ATAPI device is still using the ATA interface. + enum class InterfaceType { + PlainMemory, + SCSI, + ATA, + NVMe, + }; + public: virtual u64 max_addressable_block() const { return m_max_addressable_block; } @@ -52,6 +64,9 @@ public: virtual CommandSet command_set() const = 0; + StringView interface_type_to_string_view() const; + StringView command_set_to_string_view() const; + // ^File virtual ErrorOr ioctl(OpenFileDescription&, unsigned request, Userspace arg) final; @@ -61,6 +76,7 @@ protected: virtual StringView class_name() const override; private: + virtual InterfaceType interface_type() const = 0; mutable IntrusiveListNode> m_list_node; NonnullRefPtrVector m_partitions;