Skip to content

Commit e4c849b

Browse files
author
Ian Seyler
committed
add virtio_scsi_cmd
1 parent 67cea9f commit e4c849b

File tree

2 files changed

+137
-18
lines changed

2 files changed

+137
-18
lines changed

src/drivers/nvs/virtio-scsi.asm

Lines changed: 136 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -215,30 +215,53 @@ virtio_scsi_init_pop2:
215215
cmp al, 0
216216
jne virtio_scsi_init_pop2
217217

218+
; Set sizes
219+
; sense_size to 96
220+
; cdb_size to 32
221+
218222
; 3.1.1 - Step 8 - At this point the device is “live”
219223
mov al, VIRTIO_STATUS_ACKNOWLEDGE | VIRTIO_STATUS_DRIVER | VIRTIO_STATUS_DRIVER_OK | VIRTIO_STATUS_FEATURES_OK
220224
mov [rsi+VIRTIO_DEVICE_STATUS], al
221225

226+
; Check which LUNs exist
222227
; REPORT LUNS 0xA0 - 124
228+
mov rdi, cmd_cdb
229+
mov al, 0xa0
230+
stosb ; Operation Code
231+
mov al, 0x00
232+
stosb ; Select Report
233+
stosb ; Reserved
234+
stosb ; Reserved
235+
stosb ; Reserved
236+
stosb ; Reserved
237+
mov eax, 512
238+
stosd
223239

240+
call virtio_scsi_cmd
241+
242+
; Check each LUN
224243
; INQUIRY 0x12 - 144
225244

245+
; Verify each is ready
226246
; TEST UNIT READY 0x00 - 108
227247

228248
; REQUEST SENSE 0x03 - 126
229249

230250
; TEST UNIT READY 0x00 - 108
231251

252+
; Check the capacity
232253
; READ CAPACITY (10) 0x25 - 116
233254

234255
; MODE SENSE (10) 0x5A - 135
235256

257+
; Read some data
236258
; READ (10) 0x28
237259

238-
mov rcx, 1
239-
mov rdi, 0x600000
240-
call virtio_scsi_io
241-
jmp virtio_scsi_init_error
260+
jmp $
261+
; mov rcx, 1
262+
; mov rdi, 0x600000
263+
; call virtio_scsi_io
264+
; jmp virtio_scsi_init_error
242265

243266
virtio_scsi_init_done:
244267
bts word [os_nvsVar], 4 ; Set the bit flag that Virtio SCSI has been initialized
@@ -265,6 +288,92 @@ virtio_scsi_init_error:
265288
; -----------------------------------------------------------------------------
266289

267290

291+
; -----------------------------------------------------------------------------
292+
; virtio_scsi_cmd -- Perform a VIRTIO SCSI command
293+
; IN: TBD
294+
; OUT: Nothing
295+
; All other registers preserved
296+
virtio_scsi_cmd:
297+
push r9
298+
push rdi
299+
push rdx
300+
push rcx
301+
push rbx
302+
push rax
303+
304+
mov r9, rdi ; Save the memory address
305+
306+
; Build the request
307+
; todo
308+
309+
mov rdi, os_nvs_mem ; This driver always starts at beginning of the Descriptor Table
310+
; FIXME: Add desc_index offset
311+
add rdi, 16384
312+
313+
; Add Request to Descriptor Entry 0
314+
mov rax, cmd ; Address of the request
315+
stosq ; 64-bit address
316+
mov eax, 51 ; 19 byte REQ Header + 32 byte CDB
317+
stosd ; 32-bit length
318+
mov ax, VIRTQ_DESC_F_NEXT
319+
stosw ; 16-bit Flags
320+
add rdi, 2 ; Skip Next as it is pre-populated
321+
322+
; Add Response to Descriptor Entry 1
323+
mov rax, 0x600000 ; Address of the response
324+
stosq ; 64-bit address
325+
mov eax, 108
326+
stosd ; 32-bit length
327+
mov eax, VIRTQ_DESC_F_NEXT | VIRTQ_DESC_F_WRITE
328+
stosw ; 16-bit Flags
329+
add rdi, 2 ; Skip Next as it is pre-populated
330+
331+
; Add data to Descriptor Entry 2
332+
mov rax, 0x610000 ; Address to store the data
333+
stosq
334+
mov eax, 512 ; TODO remote hardcoded length
335+
stosd
336+
mov ax, VIRTQ_DESC_F_WRITE
337+
stosw ; 16-bit Flags
338+
add rdi, 2 ; Skip Next as it is pre-populated
339+
340+
; Add entry to Avail
341+
mov rdi, os_nvs_mem+0x5000 ; Offset to start of Availability Ring
342+
mov ax, 1 ; 1 for no interrupts
343+
stosw ; 16-bit flags
344+
mov ax, [availindex]
345+
stosw ; 16-bit index
346+
mov ax, 0
347+
stosw ; 16-bit ring
348+
349+
; Notify the queue
350+
mov rdi, [os_virtioscsi_base]
351+
add rdi, [notify_offset] ; This driver only uses Queue 0 so no multiplier needed
352+
add rdi, 8
353+
xor eax, eax
354+
stosw
355+
356+
; Inspect the used ring
357+
mov rdi, os_nvs_mem+0x6002 ; Offset to start of Used Ring
358+
mov bx, [availindex]
359+
virtio_scsi_cmd_wait:
360+
mov ax, [rdi] ; Load the index
361+
cmp ax, bx
362+
jne virtio_scsi_cmd_wait
363+
364+
add word [descindex], 3 ; 3 entries were required
365+
add word [availindex], 1
366+
367+
pop rax
368+
pop rbx
369+
pop rcx
370+
pop rdx
371+
pop rdi
372+
pop r9
373+
ret
374+
; -----------------------------------------------------------------------------
375+
376+
268377
; -----------------------------------------------------------------------------
269378
; virtio_scsi_io -- Perform an I/O operation on a VIRTIO SCSI device
270379
; IN: RAX = starting sector #
@@ -392,16 +501,25 @@ virtio_scsi_id:
392501

393502
align 16
394503
req: ; 19 bytes
395-
lun: db 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00
396-
id: dq 0x0000000000000000
397-
task_attr: db 0x00
398-
prio: db 0x00
399-
crn: db 0x00 ;
504+
;lun: db 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00
505+
;id: dq 0x0000000000000000
506+
;task_attr: db 0x00
507+
;prio: db 0x00
508+
;crn: db 0x00 ;
400509

401510
; 10-byte Command Descriptor Block
402-
cdb: db 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
511+
;cdb: db 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00
403512

404-
blank: db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
513+
;blank: db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ;0x00
514+
515+
align 16
516+
cmd:
517+
cmd_lun: db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
518+
cmd_tag: dq 0
519+
cmd_task_attr: db 0x00
520+
cmd_prio: db 0x00
521+
cmd_crn: db 0x00
522+
cmd_cdb: db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
405523

406524
; 16-byte Command Descriptor Block
407525
;cdb_opcode: db 0x28
@@ -431,14 +549,14 @@ blank: db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
431549

432550
;cdb: db 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ;times 32 db 0x00
433551

434-
align 16
552+
;align 16
435553
resp:
436-
sense_len: dd 0x00000000
437-
residual: dd 0x00000000
438-
status_qualifier: dw 0x0000
439-
status: db 0x00
440-
response: db 0x00
441-
sense: times 96 db 0x00
554+
;sense_len: dd 0x00000000
555+
;residual: dd 0x00000000
556+
;status_qualifier: dw 0x0000
557+
;status: db 0x00
558+
;response: db 0x00
559+
;sense: times 96 db 0x00
442560

443561

444562
; VIRTIO SCSI Registers - Device Config

src/kernel.asm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
BITS 64 ; Specify 64-bit
1010
ORG 0x0000000000100000 ; The kernel needs to be loaded at this address
11+
DEFAULT ABS
1112

1213
%DEFINE BAREMETAL_VER 'v1.0.0 (January 21, 2020)', 13, 'Copyright (C) 2008-2025 Return Infinity', 13, 0
1314
%DEFINE BAREMETAL_API_VER 1

0 commit comments

Comments
 (0)