summaryrefslogtreecommitdiffstats
path: root/qemu/roms/SLOF/slof/fs/usb/dev-storage.fs
blob: 94f8421d35a7b8188217bd0bc3d03909253b4c5b (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
\ *****************************************************************************
\ * Copyright (c) 2013 IBM Corporation
\ * All rights reserved.
\ * This program and the accompanying materials
\ * are made available under the terms of the BSD License
\ * which accompanies this distribution, and is available at
\ * http://www.opensource.org/licenses/bsd-license.php
\ *
\ * Contributors:
\ *     IBM Corporation - initial implementation
\ ****************************************************************************/

\ ( usbdev -- )

new-device

VALUE usbdev

s" slofdev.fs" included

false VALUE usb-disk-debug?

usbdev slof-dev>port l@ dup set-unit encode-phys " reg" property
s" storage" device-name

s" dev-parent-calls.fs" included

2 encode-int s" #address-cells" property
0 encode-int s" #size-cells" property

: decode-unit 2 hex64-decode-unit ;
: encode-unit 2 hex64-encode-unit ;

0 CONSTANT USB_PIPE_OUT
1 CONSTANT USB_PIPE_IN

\ -----------------------------------------------------------
\ Specific properties
\ -----------------------------------------------------------

usbdev slof-dev>udev @ VALUE udev
usbdev slof-dev>port l@ VALUE port
usbdev slof-dev>hcitype l@ VALUE hcitype

0 INSTANCE VALUE lun
10000 VALUE dev-max-transfer
0     VALUE resp-buffer
0     VALUE resp-size
0f CONSTANT SCSI-COMMAND-OFFSET

\ -------------------------------------------------------
\ DMA-able buffers
\ -------------------------------------------------------

STRUCT
   dev-max-transfer FIELD usb>data
   40 FIELD usb>cmd
   20 FIELD usb>csw
CONSTANT /dma-buf

0 VALUE dma-buf
0 VALUE dma-buf-phys
0 VALUE td-buf
0 VALUE td-buf-phys
1000 CONSTANT /td-buf

: (dma-buf-init)  ( -- )
   /dma-buf dma-alloc TO dma-buf
   dma-buf /dma-buf 0 dma-map-in TO dma-buf-phys
   /td-buf dma-alloc TO td-buf
   td-buf /td-buf 0 dma-map-in TO td-buf-phys
;

: (dma-buf-free)  ( -- )
   td-buf td-buf-phys /td-buf dma-map-out
   td-buf /td-buf dma-free
   0 TO td-buf
   0 TO td-buf-phys
   dma-buf dma-buf-phys /dma-buf dma-map-out
   dma-buf /dma-buf dma-free
   0 TO dma-buf
   0 TO dma-buf-phys
;


scsi-open

\ -----------------------------------------------------------
\ Perform SCSI commands
\ -----------------------------------------------------------

0 INSTANCE VALUE current-target

\ SCSI command. We do *NOT* implement the "standard" execute-command
\ because that doesn't have a way to return the sense buffer back, and
\ we do have auto-sense with some hosts. Instead we implement a made-up
\ do-scsi-command.
\
\ Note: stat is -1 for "hw error" (ie, error queuing the command or
\ getting the response).
\
\ A sense buffer is returned whenever the status is non-0 however
\ if sense-len is 0 then no sense data is actually present
\

: do-bulk-command ( resp-buffer resp-size -- TRUE | FALSE )
    TO resp-size
    TO resp-buffer
    udev USB_PIPE_OUT td-buf td-buf-phys dma-buf-phys usb>cmd 1F
    usb-transfer-bulk IF \ transfer CBW
	resp-size IF
	    d# 125 us
	    udev USB_PIPE_IN td-buf td-buf-phys resp-buffer resp-size
	    usb-transfer-bulk 1 = not IF \ transfer data
	        usb-disk-debug?	IF ." Data phase failed " cr THEN
		\ FALSE EXIT
		\ in case of a stall/halted endpoint we clear the halt
		\ Fall through and try reading the CSW
	    THEN
	THEN
	d# 125 us
	udev USB_PIPE_IN td-buf td-buf-phys dma-buf-phys usb>csw 0D
	usb-transfer-bulk \ transfer CSW
    ELSE
	FALSE EXIT
    THEN
;

STRUCT \ cbw
    /l FIELD cbw>sig
    /l FIELD cbw>tag
    /l FIELD cbw>len
    /c FIELD cbw>flags
    /c FIELD cbw>lun     \ 0:3 bits
    /c FIELD cbw>cblen   \ 0:4 bits
CONSTANT cbw-length

STRUCT \ csw
    /l FIELD csw>sig
    /l FIELD csw>tag
    /l FIELD csw>data-residue
    /c FIELD csw>status
CONSTANT cbw-length

0 VALUE cbw-addr
0 VALUE csw-addr

: build-cbw ( tag xfer-len dir lun cmd-len addr -- )
    TO cbw-addr               ( tag xfer-len dir lun cmd-len )
    cbw-addr cbw-length erase ( tag xfer-len dir lun cmd-len )
    cbw-addr cbw>cblen c!     ( tag xfer-len dir lun )
    cbw-addr cbw>lun c!       ( tag xfer-len dir )
    \ dir is true or false
    \ bmCBWFlags
    \           BIT 7 Direction
    \               0 - OUT
    \               1 - IN
    IF 80 ELSE 0 THEN
    cbw-addr cbw>flags c!     ( tag xfer-len )
    cbw-addr cbw>len l!-le    ( tag )
    cbw-addr cbw>tag l!-le    ( )
    43425355 cbw-addr cbw>sig l!-le
;

0 INSTANCE VALUE usb-buf-addr
0 INSTANCE VALUE usb-buf-len
0 INSTANCE VALUE usb-dir
0 INSTANCE VALUE usb-cmd-addr
0 INSTANCE VALUE usb-cmd-len
1 VALUE tag

: execute-scsi-command ( buf-addr buf-len dir cmd-addr cmd-len -- ... )
                       ( ... [ sense-buf sense-len ] stat )
    \ Cleanup virtio request and response
    to usb-cmd-len to usb-cmd-addr to usb-dir to usb-buf-len to usb-buf-addr

    dma-buf usb>cmd 40 0 fill
    dma-buf usb>csw 20 0 fill

    tag usb-buf-len usb-dir lun usb-cmd-len dma-buf usb>cmd
    ( tag transfer-len dir lun cmd-len addr )
    build-cbw
    1 tag + to tag

    usb-cmd-addr
    dma-buf usb>cmd SCSI-COMMAND-OFFSET +
    usb-cmd-len
    move

    \ Send it
    dma-buf-phys usb>data usb-buf-len
    do-bulk-command IF
	dma-buf usb>data usb-buf-addr usb-buf-len move
    ELSE
        ." USB-DISK: Bulk commad failed!" cr
        0 0 -1 EXIT
    THEN

    dma-buf usb>csw to csw-addr
    csw-addr csw>sig l@ 55534253 <> IF
	." USB-DISK: CSW signature invalid " cr
	0 0 -1 EXIT
    THEN

    csw-addr csw>status c@ CASE
	0 OF ENDOF			\ Good
	1 OF
	    usb-disk-debug? IF
		." USB-DISK: CSW Data residue: "
		csw-addr csw>data-residue l@-le . cr
	    THEN
	    0 0 8 EXIT ENDOF	\ Command failed, Retry
	dup OF 0 0 -1 EXIT ENDOF	\ Anything else -> HW error
    ENDCASE

    \ Other error status
    csw-addr csw>status c@ dup 0<> IF
	usb-disk-debug? IF
	    over scsi-get-sense-data
	    ." USB-DISK: Sense key [ " dup . ." ] " .sense-text
	    ."  ASC,ASCQ: " . . cr
        THEN
       rot
    THEN
;

\ --------------------------------
\ Include the generic host helpers
\ --------------------------------

" scsi-host-helpers.fs" included

0 VALUE open-count

: usb-storage-init  (  -- TRUE )
    td-buf 0= IF
	usb-disk-debug? IF ." USB-DISK: Allocating buffer "  cr THEN
	(dma-buf-init)
	udev USB-MSC-INIT 0= IF
	    ." USB-DISK: Unable to initialize MSC " cr
	    FALSE
	ELSE
	    TRUE
	THEN
    THEN
;

: usb-storage-cleanup
    td-buf 0<> IF
	usb-disk-debug? IF ." USB-DISK: Freeing buffer " cr THEN
	(dma-buf-free)
	udev USB-MSC-EXIT 0= IF ." USB-DISK: Unable to exit MSC " cr THEN
    THEN
;

: open
    usb-disk-debug? IF ." USB-DISK: Opening (count is " open-count . ." )" cr THEN

    open-count 0= IF
	usb-storage-init IF
	    1 to open-count true
	ELSE ." USB-DISK initialization failed !" cr false THEN
    ELSE
	open-count 1 + to open-count
	true
    THEN
;

: close
    usb-disk-debug? IF ." USB-DISK: Closing (count is " open-count . ." )" cr THEN

    open-count 0> IF
        open-count 1 - dup to open-count
	0= IF
	    usb-storage-cleanup
	THEN
    THEN
;

\ -----------------------------------------------------------
\ SCSI scan at boot and child device support
\ -----------------------------------------------------------

\ We use SRP luns of the form 01000000 | (target << 8) | lun
\ in the top 32 bits of the 64-bit LUN
: (set-target)
    dup 20 >> FFFF and to lun
    dup 30 >> FF and to port
    to current-target
    usb-disk-debug? IF ." USB-DISK: udev " udev . ." lun:" lun . ." port:" port . cr THEN
;

: dev-generate-srplun ( target lun-id -- srplun )
    swap drop port 0100 or 10 << or 20 <<
;

\ FIXME: Check max transfer coming from virtio config
: max-transfer ( -- n )
    dev-max-transfer
;

\ We obtain here a unit address on the stack, since our #address-cells
\ is 2, the 64-bit srplun is split in two cells that we need to join
\
\ Note: This diverges a bit from the original OF scsi spec as the two
\ cells are the 2 words of a 64-bit SRP LUN
: set-address ( srplun.lo srplun.hi -- )
    lxjoin (set-target)
    usb-disk-debug? IF ." USB-DISK: udev " udev . ." lun:" lun . ." port:" port . cr THEN
;

1 CONSTANT #target
: dev-max-target ( -- #target )
    #target
;

" scsi-probe-helpers.fs" included

scsi-close        \ no further scsi words required

\ Set scsi alias if none is set yet
: setup-alias
    s" scsi" find-alias 0= IF
	s" scsi" get-node node>path set-alias
    ELSE
	drop
    THEN
;

: usb-storage-init-and-scan ( -- )
   usb-disk-debug? IF ." Initializing usb-disk: udev " udev . cr THEN

  \ Create instance for scanning:
   0 0 get-node open-node ?dup 0= IF EXIT THEN
   my-self >r
   dup to my-self

   hcitype
   CASE
      1 OF 4000 TO dev-max-transfer ENDOF \ OHCI
      2 OF 10000 TO dev-max-transfer ENDOF \ EHCI
      3 OF F000 TO dev-max-transfer ENDOF \ XHCI
   ENDCASE
   usb-storage-init
   scsi-find-disks
   setup-alias
   usb-storage-cleanup
   \ Close the temporary instance:
   close-node
   r> to my-self
;

."     USB Storage " cr
: usb-scsi-add-disk
     " scsi-disk.fs" included
;

usb-scsi-add-disk
usb-storage-init-and-scan

finish-device