refactor: add vzalloc and make vcalloc sounds right.
chore: format code and renaming things
// 清空任何待响应的中断
port->regs[HBA_RPxIS] = 0;
- port->device = vcalloc(sizeof(struct hba_device));
+ port->device = vzalloc(sizeof(struct hba_device));
+ port->device->port = port;
// 在命令表中构建命令FIS
struct sata_reg_fis* cmd_fis = (struct sata_reg_fis*)cmd_table->command_fis;
#include <lunaix/spike.h>
int
-__sata_buffer_io(struct hba_port* port,
+__sata_buffer_io(struct hba_device* dev,
uint64_t lba,
void* buffer,
uint32_t size,
{
assert_msg(((uintptr_t)buffer & 0x3) == 0, "HBA: Bad buffer alignment");
+ struct hba_port* port = dev->port;
struct hba_cmdh* header;
struct hba_cmdt* table;
int slot = hba_prepare_cmd(port, &table, &header, buffer, size);
}
int
-sata_read_buffer(struct hba_port* port,
+sata_read_buffer(struct hba_device* dev,
uint64_t lba,
void* buffer,
uint32_t size)
{
- return __sata_buffer_io(port, lba, buffer, size, 0);
+ return __sata_buffer_io(dev, lba, buffer, size, 0);
}
int
-sata_write_buffer(struct hba_port* port,
+sata_write_buffer(struct hba_device* dev,
uint64_t lba,
void* buffer,
uint32_t size)
{
- return __sata_buffer_io(port, lba, buffer, size, 1);
+ return __sata_buffer_io(dev, lba, buffer, size, 1);
}
void
}
void
-__scsi_buffer_io(struct hba_port* port,
+__scsi_buffer_io(struct hba_device* dev,
uint64_t lba,
void* buffer,
uint32_t size,
{
assert_msg(((uintptr_t)buffer & 0x3) == 0, "HBA: Bad buffer alignment");
+ struct hba_port* port = dev->port;
struct hba_cmdh* header;
struct hba_cmdt* table;
int slot = hba_prepare_cmd(port, &table, &header, buffer, size);
}
void
-scsi_read_buffer(struct hba_port* port,
+scsi_read_buffer(struct hba_device* dev,
uint64_t lba,
void* buffer,
uint32_t size)
{
- __scsi_buffer_io(port, lba, buffer, size, 0);
+ __scsi_buffer_io(dev, lba, buffer, size, 0);
}
void
-scsi_write_buffer(struct hba_port* port,
+scsi_write_buffer(struct hba_device* dev,
uint64_t lba,
void* buffer,
uint32_t size)
{
- __scsi_buffer_io(port, lba, buffer, size, 1);
+ __scsi_buffer_io(dev, lba, buffer, size, 1);
}
\ No newline at end of file
pci_reg_t reg1 = pci_read_cspace(device->cspace_base, device->msi_loc);
// manipulate the MSI_CTRL to allow device using MSI to request service.
- reg1 = ((((reg1 >> 16) & ~0x70) | 0x1) << 16) | (reg1 & 0xffff);
+ reg1 = (reg1 & 0xff8fffff) | 0x10000;
pci_write_cspace(device->cspace_base, device->msi_loc, reg1);
}
#define HBA_PxCMD_ST (1)
#define HBA_PxINTR_DMA (1 << 2)
#define HBA_PxINTR_D2HR (1)
+#define HBA_PxINTR_DPE (1 << 5)
#define HBA_PxTFD_ERR (1)
#define HBA_PxTFD_BSY (1 << 7)
#define HBA_PxTFD_DRQ (1 << 3)
uint32_t alignment_offset;
uint32_t block_per_sec;
uint32_t capabilities;
+ struct hba_port* port;
struct
{
- int (*identify)(struct hba_port* port);
- int (*read_buffer)(struct hba_port* port,
+ int (*identify)(struct hba_device* dev);
+ int (*read_buffer)(struct hba_device* dev,
uint64_t lba,
void* buffer,
uint32_t size);
- int (*write_buffer)(struct hba_port* port,
+ int (*write_buffer)(struct hba_device* dev,
uint64_t lba,
void* buffer,
uint32_t size);
valloc(unsigned int size);
void*
-vcalloc(unsigned int size);
+vzalloc(unsigned int size);
+
+void*
+vcalloc(unsigned int size, unsigned int count);
void
vfree(void* ptr);
valloc_dma(unsigned int size);
void*
-vcalloc_dma(unsigned int size);
+vzalloc_dma(unsigned int size);
void
vfree_dma(void* ptr);
pos = list_entry(pile->free.next, typeof(*pos), cakes);
}
-found:
piece_index_t found_index = pos->next_free;
pos->next_free = pos->free_list[found_index];
pos->used_pieces++;
int
cake_release(struct cake_pile* pile, void* area)
{
- piece_index_t area_index;
+ piece_index_t piece_index;
struct cake_s *pos, *n;
struct llist_header* hdrs[2] = { &pile->full, &pile->partial };
if (pos->first_piece > area) {
continue;
}
- area_index =
+ piece_index =
(uintptr_t)(area - pos->first_piece) / pile->piece_size;
- if (area_index < pile->pieces_per_cake) {
+ if (piece_index < pile->pieces_per_cake) {
goto found;
}
}
return 0;
found:
- pos->free_list[area_index] = pos->next_free;
- pos->next_free = area_index;
+ pos->free_list[piece_index] = pos->next_free;
+ pos->next_free = piece_index;
pos->used_pieces--;
pile->alloced_pieces--;
}
void*
-vcalloc(unsigned int size)
+vzalloc(unsigned int size)
{
void* ptr = __valloc(size, &piles);
memset(ptr, 0, size);
return ptr;
}
+void*
+vcalloc(unsigned int size, unsigned int count)
+{
+ unsigned int alloc_size;
+ if (__builtin_umul_overflow(size, count, &alloc_size)) {
+ return 0;
+ }
+
+ void* ptr = __valloc(alloc_size, &piles);
+ memset(ptr, 0, alloc_size);
+ return ptr;
+}
+
void
vfree(void* ptr)
{
}
void*
-vcalloc_dma(unsigned int size)
+vzalloc_dma(unsigned int size)
{
void* ptr = __valloc(size, &piles_dma);
memset(ptr, 0, size);
} else {
x86_page_table* ptd = (x86_page_table*)(L2_VADDR(l1inx));
size_t i = L2_INDEX(current_addr);
- for (; i < 1024 && examed_size < size; i++) {
+ for (; i < PG_MAX_ENTRIES && examed_size < size; i++) {
if (!ptd->entry[i]) {
examed_size += PG_SIZE;
} else if (examed_size) {
__test_disk_io()
{
struct hba_port* port = ahci_get_port(0);
- char* buffer = vcalloc_dma(port->device->block_size);
+ char* buffer = vzalloc_dma(port->device->block_size);
strcpy(buffer, test_sequence);
kprintf("WRITE: %s\n", buffer);
int result;