refactor: change the disk io api to accept device instead of port struct
authorMinep <zelong56@gmail.com>
Wed, 20 Jul 2022 12:24:25 +0000 (13:24 +0100)
committerMinep <zelong56@gmail.com>
Wed, 20 Jul 2022 12:24:25 +0000 (13:24 +0100)
refactor: add vzalloc and make vcalloc sounds right.
chore: format code and renaming things

lunaix-os/hal/ahci/ahci.c
lunaix-os/hal/ahci/ata.c
lunaix-os/hal/ahci/atapi.c
lunaix-os/hal/pci.c
lunaix-os/includes/hal/ahci/hba.h
lunaix-os/includes/lunaix/mm/valloc.h
lunaix-os/kernel/mm/cake.c
lunaix-os/kernel/mm/valloc.c
lunaix-os/kernel/mm/vmap.c
lunaix-os/kernel/proc0.c

index 133513b049e096d74542c7f2ce9a20cff4cf4d17..156267605b013a1189c800ea52ce841dfbb67ce6 100644 (file)
@@ -316,7 +316,8 @@ ahci_init_device(struct hba_port* port)
 
     // 清空任何待响应的中断
     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;
index f6ff7c15a43acc04f2ac4be1b4c1353e524ee727..a2f10ba769bfa49c24e7440baae63b7e98231c65 100644 (file)
@@ -6,7 +6,7 @@
 #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,
@@ -14,6 +14,7 @@ __sata_buffer_io(struct hba_port* port,
 {
     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);
@@ -70,21 +71,21 @@ fail:
 }
 
 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
index 397c7338033602ab916f23589fab9e99388a151a..0ef5099404b3bfa5a0f5a1211858743620662835 100644 (file)
@@ -42,7 +42,7 @@ scsi_parse_capacity(struct hba_device* device, uint32_t* parameter)
 }
 
 void
-__scsi_buffer_io(struct hba_port* port,
+__scsi_buffer_io(struct hba_device* dev,
                  uint64_t lba,
                  void* buffer,
                  uint32_t size,
@@ -50,6 +50,7 @@ __scsi_buffer_io(struct hba_port* port,
 {
     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);
@@ -107,19 +108,19 @@ fail:
 }
 
 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
index 4bf4c56863dac16b6fa4b16e483ea97557199376..b03c128fb792335638ea425d35a664b37e239e2a 100644 (file)
@@ -187,7 +187,7 @@ pci_setup_msi(struct pci_device* device, int vector)
     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);
 }
 
index e7f8d01f7cfe5f0d04d8c4ff63879f94ac31e682..91ecfedc3545ef4b34d7f3d57d2bedfe6f22c2a5 100644 (file)
@@ -32,6 +32,7 @@
 #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)
@@ -104,15 +105,16 @@ struct hba_device
     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);
index d5d0c7530b5e97d08e225c33e8141f6164f8ede2..9be750c4e023a8e039d76256b9e6acefcdc91731 100644 (file)
@@ -5,7 +5,10 @@ void*
 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);
@@ -14,7 +17,7 @@ void*
 valloc_dma(unsigned int size);
 
 void*
-vcalloc_dma(unsigned int size);
+vzalloc_dma(unsigned int size);
 
 void
 vfree_dma(void* ptr);
index 7e3142b1fbe6579b3d5dc2351cba4e1dae074c87..f6e4340388859cd5aaf38224b8c4ee615fc2a301 100644 (file)
@@ -120,7 +120,6 @@ cake_grab(struct cake_pile* pile)
         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++;
@@ -140,7 +139,7 @@ found:
 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 };
 
@@ -150,9 +149,9 @@ cake_release(struct cake_pile* pile, void* area)
             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;
             }
         }
@@ -161,8 +160,8 @@ cake_release(struct cake_pile* pile, void* area)
     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--;
 
index 631f39ee6f0ccd8070f8882bce4a40193588c1f3..02384ef662833f5b00012d7526cd0f6cdb352108 100644 (file)
@@ -67,13 +67,26 @@ valloc(unsigned int size)
 }
 
 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)
 {
@@ -87,7 +100,7 @@ valloc_dma(unsigned int size)
 }
 
 void*
-vcalloc_dma(unsigned int size)
+vzalloc_dma(unsigned int size)
 {
     void* ptr = __valloc(size, &piles_dma);
     memset(ptr, 0, size);
index 5df524d532986fd2111521d0a6c6c378ed1f010c..deb18d30a05577333e824f3c7c8d7ded5e093b55 100644 (file)
@@ -27,7 +27,7 @@ vmm_vmap(uintptr_t paddr, size_t size, pt_attr attr)
         } 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) {
index 759db2c0d19906bdffd2a34e8639615659e50bdb..36d872710a2af622e113c97d053485492790188c 100644 (file)
@@ -119,7 +119,7 @@ void
 __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;