1/*
2  File: AppleSCSITargetEmulator.h
3
4  Contains:
5
6  Version: 1.0.0
7
8  Copyright: Copyright (c) 2007 by Apple Inc., All Rights Reserved.
9
10Disclaimer:IMPORTANT:  This Apple software is supplied to you by Apple Inc.
11("Apple") in consideration of your agreement to the following terms, and your use,
12installation, modification or redistribution of this Apple software constitutes acceptance
13of these terms.  If you do not agree with these terms, please do not use, install, modify or
14redistribute this Apple software.
15
16In consideration of your agreement to abide by the following terms, and subject
17to these terms, Apple grants you a personal, non-exclusive license, under Apple's
18copyrights in this original Apple software (the "Apple Software"), to use, reproduce,
19modify and redistribute the Apple Software, with or without modifications, in source and/or
20binary forms; provided that if you redistribute the Apple Software in its entirety
21and without modifications, you must retain this notice and the following text
22and disclaimers in all such redistributions of the Apple Software.  Neither the
23name, trademarks, service marks or logos of Apple Inc. may be used to
24endorse or promote products derived from the Apple Software without specific prior
25written permission from Apple.  Except as expressly stated in this notice, no
26other rights or licenses, express or implied, are granted by Apple herein,
27including but not limited to any patent rights that may be infringed by your derivative
28works or by other works in which the Apple Software may be incorporated.
29
30The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
31EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
32MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE
33OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL APPLE
34BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
37REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
38AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT
39LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40*/
41
42
43#ifndef __APPLE_SCSI_TARGET_EMULATOR_H__
44#define __APPLE_SCSI_TARGET_EMULATOR_H__
45
46
47//-----------------------------------------------------------------------------
48//	Includes
49//-----------------------------------------------------------------------------
50
51#include <IOKit/IOMemoryDescriptor.h>
52#include <IOKit/scsi/SCSITask.h>
53#include <libkern/c++/OSArray.h>
54#include <IOKit/IOLocks.h>
55#include <IOKit/scsi/SCSICmds_REQUEST_SENSE_Defs.h>
56#include <IOKit/scsi/SCSICmds_REPORT_LUNS_Definitions.h>
57#include "AppleSCSIEmulatorDefines.h"
58
59
60//-----------------------------------------------------------------------------
61//	Constants
62//-----------------------------------------------------------------------------
63
64enum
65{
66	kTargetStateChangeActiveBit			= 0,
67	kTargetStateChangeActiveWaitBit		= 1,
68
69	kTargetStateChangeActiveMask		= (1 << kTargetStateChangeActiveBit),
70	kTargetStateChangeActiveWaitMask	= (1 << kTargetStateChangeActiveWaitBit),
71};
72
73
74//-----------------------------------------------------------------------------
75//	Class declaration
76//-----------------------------------------------------------------------------
77
78class AppleSCSITargetEmulator : public OSObject
79{
80
81	OSDeclareDefaultStructors ( AppleSCSITargetEmulator )
82
83public:
84
85	static AppleSCSITargetEmulator *	Create ( SCSITargetIdentifier targetID );
86
87	inline SCSITargetIdentifier GetTargetID ( void ) { return fTargetID; }
88
89	bool	Init ( SCSITargetIdentifier targetID );
90
91	bool	AddLogicalUnit (
92		SCSILogicalUnitNumber 	logicalUnitNumber,
93		UInt64					capacity,
94		IOMemoryDescriptor * 	inquiryBuffer,
95		IOMemoryDescriptor * 	inquiryPage00Buffer,
96		IOMemoryDescriptor * 	inquiryPage80Buffer,
97		IOMemoryDescriptor * 	inquiryPage83Buffer );
98
99	void	RemoveLogicalUnit ( SCSILogicalUnitNumber logicalUnitNumber );
100	void	RebuildListOfLUNs ( void );
101
102	void	free ( void );
103
104#if USE_LUN_BYTES
105
106	int SendCommand ( UInt8 *				cdb,
107					  UInt8					cbdLen,
108					  IOMemoryDescriptor * 	dataDesc,
109					  UInt64 *				dataLen,
110					  SCSILogicalUnitBytes	logicalUnitBytes,
111					  SCSITaskStatus * 		scsiStatus,
112					  SCSI_Sense_Data * 	senseBuffer,
113					  UInt8 *				senseBufferLen );
114
115#else
116
117	int SendCommand ( UInt8 *				cdb,
118					  UInt8					cbdLen,
119					  IOMemoryDescriptor * 	dataDesc,
120					  UInt64 *				dataLen,
121					  SCSILogicalUnitNumber	logicalUnitNumber,
122					  SCSITaskStatus * 		scsiStatus,
123					  SCSI_Sense_Data * 	senseBuffer,
124					  UInt8 *				senseBufferLen );
125
126#endif	/* USE_LUN_BYTES */
127
128
129	static SCSI_Sense_Data			sBadLUNSenseData;
130	static SCSI_Sense_Data			sLUNInventoryChangedData;
131
132	SCSITargetIdentifier 			fTargetID;
133	SCSICmd_REPORT_LUNS_Header *	fLUNReportBuffer;
134	UInt32							fLUNReportBufferSize;
135	UInt32							fLUNDataAvailable;
136	OSOrderedSet *					fLUNs;
137	IOLock *						fLock;
138	UInt32							fState;
139	bool							fLUNInventoryChanged;
140
141};
142
143
144#endif	/* __APPLE_SCSI_TARGET_EMULATOR_H__ */