2 * Copyright 2013, Jérôme Duval, korli@users.berlios.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef VIRTIO_SCSI_PRIVATE_H
6 #define VIRTIO_SCSI_PRIVATE_H
10 #include <condition_variable.h>
12 #include <scsi_cmds.h>
15 #include "virtio_scsi.h"
18 //#define TRACE_VIRTIO_SCSI
19 #ifdef TRACE_VIRTIO_SCSI
20 # define TRACE(x...) dprintf("virtio_scsi: " x)
22 # define TRACE(x...) ;
24 #define ERROR(x...) dprintf("\33[33mvirtio_scsi:\33[0m " x)
25 #define CALLED() TRACE("CALLED %s\n", __PRETTY_FUNCTION__)
27 extern device_manager_info
* gDeviceManager
;
28 extern scsi_for_sim_interface
*gSCSI
;
30 bool copy_sg_data(scsi_ccb
*ccb
, uint offset
, uint allocationLength
,
31 void *buffer
, int size
, bool toBuffer
);
32 void swap_words(void *data
, size_t size
);
35 #define VIRTIO_SCSI_STANDARD_TIMEOUT 10 * 1000 * 1000
36 #define VIRTIO_SCSI_INITIATOR_ID 7
38 #define VIRTIO_SCSI_NUM_EVENTS 4
41 class VirtioSCSIRequest
;
44 class VirtioSCSIController
{
46 VirtioSCSIController(device_node
* node
);
47 ~VirtioSCSIController();
51 void SetBus(scsi_bus bus
);
52 scsi_bus
Bus() const { return fBus
; }
54 void PathInquiry(scsi_path_inquiry
* info
);
55 void GetRestrictions(uint8 targetID
, bool* isATAPI
,
56 bool* noAutoSense
, uint32
* maxBlocks
);
57 uchar
ResetDevice(uchar targetID
, uchar targetLUN
);
58 status_t
ExecuteRequest(scsi_ccb
* request
);
59 uchar
AbortRequest(scsi_ccb
* request
);
60 uchar
TerminateRequest(scsi_ccb
* request
);
61 status_t
Control(uint8 targetID
, uint32 op
,
62 void* buffer
, size_t length
);
65 static void _RequestCallback(void* driverCookie
,
67 void _RequestInterrupt();
68 static void _EventCallback(void *driverCookie
, void *cookie
);
69 void _EventInterrupt(struct virtio_scsi_event
* event
);
70 static void _RescanChildBus(void *cookie
);
72 void _SubmitEvent(uint32 event
);
77 virtio_device_interface
* fVirtio
;
78 virtio_device
* fVirtioDevice
;
81 struct virtio_scsi_config fConfig
;
83 ::virtio_queue fControlVirtioQueue
;
84 ::virtio_queue fEventVirtioQueue
;
85 ::virtio_queue fRequestVirtioQueue
;
88 struct virtio_scsi_event
* fEvents
;
90 VirtioSCSIRequest
* fRequest
;
92 spinlock fInterruptLock
;
93 ConditionVariable fInterruptCondition
;
94 ConditionVariableEntry fInterruptConditionEntry
;
95 bool fExpectsInterrupt
;
97 scsi_dpc_cookie fEventDPC
;
98 struct virtio_scsi_event fEventBuffers
[VIRTIO_SCSI_NUM_EVENTS
];
102 class VirtioSCSIRequest
{
104 VirtioSCSIRequest(bool hasLock
);
105 ~VirtioSCSIRequest();
107 void SetStatus(uint8 status
);
108 uint8
Status() const { return fStatus
; }
110 void SetTimeout(bigtime_t timeout
);
111 bigtime_t
Timeout() const { return fTimeout
; }
114 return (fResponse
->sense_len
> 0); }
116 void SetIsWrite(bool isWrite
);
117 bool IsWrite() const { return fIsWrite
; }
119 void SetBytesLeft(uint32 bytesLeft
);
120 size_t* BytesLeft() { return &fBytesLeft
; }
123 { return fCCB
->data_length
> 0; }
125 status_t
Finish(bool resubmit
);
128 status_t
Start(scsi_ccb
*ccb
);
129 scsi_ccb
* CCB() { return fCCB
; }
133 void FillRequest(uint32 inCount
, uint32 outCount
,
134 physical_entry
*entries
);
137 void _FillSense(scsi_sense
*sense
);
138 uchar
_ResponseStatus();
152 struct virtio_scsi_cmd_req
*fRequest
;
153 struct virtio_scsi_cmd_resp
*fResponse
;
157 #endif // VIRTIO_SCSI_PRIVATE_H