summaryrefslogtreecommitdiffstats
path: root/qemu/roms/SLOF/slof/fs/scsi-probe-helpers.fs
blob: 6aec8b1596a048687e8bbf6435c90abd5efd59aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
\ This file is meant to be included by SCSI hosts to provide
\ probing helpers - scsi-find-disks

: wrapped-inquiry ( -- true | false )
    inquiry 0= IF false EXIT THEN
    \ Skip devices with PQ != 0
    sector inquiry-data>peripheral c@ e0 and 0 =
;

: scsi-read-lun     ( addr -- lun true | false )
    dup c@ C0 AND CASE
	40 OF w@-be 3FFF AND TRUE ENDOF
	0  OF w@-be          TRUE ENDOF
	dup dup OF ." Unsupported LUN format = " . cr FALSE ENDOF
    ENDCASE
;

: vscsi-report-luns ( -- array ndev )
    \ array of pointers, up to 8 devices
    dev-max-target 3 << alloc-mem dup
    0                                    ( devarray devcur ndev )
    dev-max-target 0 DO
	i 0 dev-generate-srplun (set-target)
	report-luns nip IF
	    sector l@                     ( devarray devcur ndev size )
	    sector 8 + swap               ( devarray devcur ndev lunarray size )
	    dup 8 + dup alloc-mem         ( devarray devcur ndev lunarray size size+ mem )
	    dup rot 0 fill                ( devarray devcur ndev lunarray size mem )
	    dup >r swap move r>           ( devarray devcur ndev mem )
	    dup sector l@ 3 >> 0 ?DO      ( devarray devcur ndev mem memcur )
		dup dup scsi-read-lun IF
		    j swap dev-generate-srplun  swap x! 8 +
		ELSE
		    2drop
		THEN
	    LOOP drop
	    rot                           ( devarray ndev mem devcur )
	    dup >r x! r> 8 +              ( devarray ndev devcur )
	    swap 1 +
	ELSE
	    dev-max-target 1 = IF
		\ Some USB MSC devices do not implement report
		\ luns. That will stall the bulk pipe. These devices are
		\ single lun devices, report it accordingly

		( devarray devcur ndev )
		16 alloc-mem ( devarray devcur ndev mem )
		dup 16 0 fill ( devarray devcur ndev mem )
		dup 0 0 dev-generate-srplun swap x!  ( devarray devcur ndev mem )
		rot x!  ( devarray ndev )
		1 +
		UNLOOP EXIT
	    THEN
	THEN
    LOOP
    nip
;

: make-media-alias ( $name srplun -- )
    >r
    get-next-alias ?dup IF
        r> make-disk-alias
    ELSE
        r> drop
    THEN
;

: scsi-find-disks      ( -- )
    ."        SCSI: Looking for devices" cr
    vscsi-report-luns
    0 ?DO
	dup x@
	BEGIN
	    dup x@
	    dup 0= IF drop TRUE ELSE
		(set-target) wrapped-inquiry IF
		    ."           " current-target (u.) type ."  "
		    \ XXX FIXME: Check top bits to ignore unsupported units
		    \            and maybe provide better printout & more cases
		    \ XXX FIXME: Actually check for LUNs
		    sector inquiry-data>peripheral c@ CASE
			0   OF ." DISK     : " " disk"  current-target make-media-alias ENDOF
			5   OF ." CD-ROM   : " " cdrom" current-target make-media-alias ENDOF
			7   OF ." OPTICAL  : " " cdrom" current-target make-media-alias ENDOF
			e   OF ." RED-BLOCK: " " disk"  current-target make-media-alias ENDOF
			dup dup OF ." ? (" . 8 emit 29 emit 5 spaces ENDOF
		    ENDCASE
		    sector .inquiry-text cr
		THEN
		8 + FALSE
	    THEN
	UNTIL drop
	8 +
    LOOP drop
;