fs: begin basic block driver
This just makes the API for accessing drives generic, instead of relying solely on the corresponding driver code
This commit is contained in:
parent
7ca418ff10
commit
03eb0b0048
3 changed files with 116 additions and 4 deletions
54
dev/ahci.c
54
dev/ahci.c
|
|
@ -1,12 +1,21 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include <kernel/mem.h>
|
#include <kernel/mem.h>
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
|
#include <kernel/block.h>
|
||||||
#include <kernel/irq.h>
|
#include <kernel/irq.h>
|
||||||
#include <kernel/pio.h>
|
#include <kernel/pio.h>
|
||||||
|
|
||||||
#include <dev/ahci.h>
|
#include <dev/ahci.h>
|
||||||
#include <dev/pci.h>
|
#include <dev/pci.h>
|
||||||
|
|
||||||
|
static int ahci_block_read(struct block_device *dev, void *buf, size_t offset, size_t sector_count);
|
||||||
|
static int ahci_block_write(struct block_device *dev, void *buf, size_t offset, size_t sector_count);
|
||||||
|
|
||||||
|
static struct block_device_ops ahci_block_ops = {
|
||||||
|
.read = ahci_block_read,
|
||||||
|
.write = ahci_block_write,
|
||||||
|
};
|
||||||
|
|
||||||
static hbamem_t *abar = NULL;
|
static hbamem_t *abar = NULL;
|
||||||
|
|
||||||
static uint8_t sata_device_count = 0;
|
static uint8_t sata_device_count = 0;
|
||||||
|
|
@ -89,13 +98,18 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
|
||||||
if (pi & 1) {
|
if (pi & 1) {
|
||||||
uint32_t type = ahci_port_type(&abar->ports[i]);
|
uint32_t type = ahci_port_type(&abar->ports[i]);
|
||||||
if (type == AHCI_DEV_SATA) {
|
if (type == AHCI_DEV_SATA) {
|
||||||
printf(LOG_INFO "SATA drive found at port %d\n", i);
|
struct sata_device *sdev = &sata_devices[sata_device_count];
|
||||||
sata_devices[sata_device_count].port = i;
|
sdev->port = i;
|
||||||
sata_devices[sata_device_count].controller = dev;
|
sdev->controller = dev;
|
||||||
sata_devices[sata_device_count].abar = abar;
|
sdev->abar = abar;
|
||||||
|
|
||||||
|
char *name = kzalloc(32, ALLOC_KERN);
|
||||||
|
snprintf(name, "ahci%d", 32, sata_device_count);
|
||||||
|
block_register(name, &ahci_block_ops, sdev);
|
||||||
|
|
||||||
ahci_rebase(&abar->ports[i], i);
|
ahci_rebase(&abar->ports[i], i);
|
||||||
sata_device_count++;
|
sata_device_count++;
|
||||||
|
printf(LOG_INFO "SATA drive found at port %d\n", i);
|
||||||
} else if (type == AHCI_DEV_SATAPI) {
|
} else if (type == AHCI_DEV_SATAPI) {
|
||||||
printf(LOG_INFO "SATAPI drive found at port %d\n", i);
|
printf(LOG_INFO "SATAPI drive found at port %d\n", i);
|
||||||
} else if (type == AHCI_DEV_SEMB) {
|
} else if (type == AHCI_DEV_SEMB) {
|
||||||
|
|
@ -203,6 +217,38 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int ahci_block_read(struct block_device *dev, void *buf, size_t offset, size_t sector_count)
|
||||||
|
{
|
||||||
|
struct sata_device *sdev = (struct sata_device *)dev->data;
|
||||||
|
|
||||||
|
/* repeat every 64K, due to the current limitations of this driver */
|
||||||
|
while (sector_count > 0) {
|
||||||
|
if (!ahci_access_sectors(sdev, offset, MIN(sector_count, 128u), (paddr_t)buf, false))
|
||||||
|
return -1;
|
||||||
|
sector_count -= MIN(sector_count, 128u);
|
||||||
|
offset += 128;
|
||||||
|
buf += 128 << 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ahci_block_write(struct block_device *dev, void *buf, size_t offset, size_t sector_count)
|
||||||
|
{
|
||||||
|
struct sata_device *sdev = (struct sata_device *)dev->data;
|
||||||
|
|
||||||
|
/* repeat every 64K, same as above */
|
||||||
|
while (sector_count > 0) {
|
||||||
|
if (!ahci_access_sectors(sdev, offset, MIN(sector_count, 128u), (paddr_t)buf, true))
|
||||||
|
return -1;
|
||||||
|
sector_count -= MIN(sector_count, 128u);
|
||||||
|
offset += 128;
|
||||||
|
buf += 128 << 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void ahci_init()
|
void ahci_init()
|
||||||
{
|
{
|
||||||
struct pci_device *dev = pci_find_device(0x01, 0x06);
|
struct pci_device *dev = pci_find_device(0x01, 0x06);
|
||||||
|
|
|
||||||
41
fs/block.c
Normal file
41
fs/block.c
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/block.h>
|
||||||
|
#include <kernel/slab.h>
|
||||||
|
|
||||||
|
#define MAX_BLOCK_DEVICES 24
|
||||||
|
|
||||||
|
struct block_device *block_devices[MAX_BLOCK_DEVICES];
|
||||||
|
size_t block_devices_count = 0;
|
||||||
|
|
||||||
|
struct block_device *block_register(char *name, struct block_device_ops *ops, void *data)
|
||||||
|
{
|
||||||
|
struct block_device *bdev;
|
||||||
|
|
||||||
|
bdev = kmalloc(sizeof(struct block_device), ALLOC_KERN);
|
||||||
|
if (!bdev)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
bdev->name = name;
|
||||||
|
bdev->data = data;
|
||||||
|
memcpy(&bdev->ops, ops, sizeof(struct block_device_ops));
|
||||||
|
|
||||||
|
block_devices[block_devices_count] = bdev;
|
||||||
|
block_devices_count++;
|
||||||
|
|
||||||
|
#ifdef KDEBUG
|
||||||
|
printf(LOG_DEBUG "Registered block device %s\n", name);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return bdev;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size)
|
||||||
|
{
|
||||||
|
return bdev->ops.read(bdev, buf, offset, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size)
|
||||||
|
{
|
||||||
|
return bdev->ops.write(bdev, buf, offset, size);
|
||||||
|
}
|
||||||
25
include/kernel/block.h
Normal file
25
include/kernel/block.h
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#ifndef _BLOCK_H_
|
||||||
|
#define _BLOCK_H_
|
||||||
|
|
||||||
|
#include <kernel/common.h>
|
||||||
|
|
||||||
|
struct block_device;
|
||||||
|
|
||||||
|
struct block_device_ops {
|
||||||
|
int (*read)(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||||
|
int (*write)(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct block_device {
|
||||||
|
char *name;
|
||||||
|
struct block_device_ops ops;
|
||||||
|
void *data;
|
||||||
|
size_t size;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct block_device *block_register(char *name, struct block_device_ops *ops, void *data);
|
||||||
|
int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||||
|
int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||||
|
|
||||||
|
#endif /* _BLOCK_H_ */
|
||||||
Loading…
Reference in a new issue