1/*
2  File: AppleSCSIPDT03Emulator.cpp
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//-----------------------------------------------------------------------------
44//	Includes
45//-----------------------------------------------------------------------------
46
47#include "AppleSCSIPDT03Emulator.h"
48
49#include <IOKit/IOMemoryDescriptor.h>
50
51#include <IOKit/scsi/SCSICommandOperationCodes.h>
52#include <IOKit/scsi/SCSICmds_INQUIRY_Definitions.h>
53#include <IOKit/scsi/SCSICmds_REPORT_LUNS_Definitions.h>
54#include <IOKit/scsi/SCSICmds_READ_CAPACITY_Definitions.h>
55
56
57//-----------------------------------------------------------------------------
58//	Macros
59//-----------------------------------------------------------------------------
60
61#define DEBUG 												0
62#define DEBUG_ASSERT_COMPONENT_NAME_STRING					"PDT03LUNEmulator"
63
64#if DEBUG
65#define EMULATOR_ADAPTER_DEBUGGING_LEVEL					4
66#endif
67
68#include "DebugSupport.h"
69
70#if ( EMULATOR_ADAPTER_DEBUGGING_LEVEL >= 1 )
71#define PANIC_NOW(x)		panic x
72#else
73#define PANIC_NOW(x)
74#endif
75
76#if ( EMULATOR_ADAPTER_DEBUGGING_LEVEL >= 2 )
77#define ERROR_LOG(x)		IOLog x; IOSleep(1)
78#else
79#define ERROR_LOG(x)
80#endif
81
82#if ( EMULATOR_ADAPTER_DEBUGGING_LEVEL >= 3 )
83#define STATUS_LOG(x)		IOLog x; IOSleep(1)
84#else
85#define STATUS_LOG(x)
86#endif
87
88#if ( EMULATOR_ADAPTER_DEBUGGING_LEVEL >= 4 )
89#define COMMAND_LOG(x)		IOLog x; IOSleep(1)
90#else
91#define COMMAND_LOG(x)
92#endif
93
94#define super AppleSCSILogicalUnitEmulator
95OSDefineMetaClassAndStructors ( AppleSCSIPDT03Emulator, AppleSCSILogicalUnitEmulator );
96
97
98//-----------------------------------------------------------------------------
99//	Globals
100//-----------------------------------------------------------------------------
101
102SCSICmd_INQUIRY_StandardData sInquiryData =
103{
104	kINQUIRY_PERIPHERAL_TYPE_ProcessorSPCDevice,	// PERIPHERAL_DEVICE_TYPE
105	0,	// RMB;
106	5,	// VERSION
107	2,	// RESPONSE_DATA_FORMAT
108	sizeof ( SCSICmd_INQUIRY_StandardData ) - 5,	// ADDITIONAL_LENGTH
109	0,	// SCCSReserved
110	0,	// flags1
111	0,	// flags2
112	"APPLE",
113	"SCSI Emulator",
114	"1.0",
115};
116
117
118
119//-----------------------------------------------------------------------------
120//	Create
121//-----------------------------------------------------------------------------
122
123AppleSCSIPDT03Emulator *
124AppleSCSIPDT03Emulator::Create ( void )
125{
126
127	AppleSCSIPDT03Emulator *	logicalUnit = NULL;
128	bool						result		= false;
129
130	STATUS_LOG ( ( "AppleSCSIPDT03Emulator::Create\n" ) );
131
132	logicalUnit = OSTypeAlloc ( AppleSCSIPDT03Emulator );
133	require_nonzero ( logicalUnit, ErrorExit );
134
135	result = logicalUnit->init ( );
136	require ( result, ReleaseLogicalUnit );
137
138	return logicalUnit;
139
140
141ReleaseLogicalUnit:
142
143
144	logicalUnit->release ( );
145
146
147ErrorExit:
148
149
150	return NULL;
151
152}
153
154
155//-----------------------------------------------------------------------------
156//	SendCommand
157//-----------------------------------------------------------------------------
158
159int
160AppleSCSIPDT03Emulator::SendCommand (
161	UInt8 *					cdb,
162	UInt8 					cbdLen,
163	IOMemoryDescriptor *	dataDesc,
164	UInt64 *				dataLen,
165	SCSITaskStatus *		scsiStatus,
166	SCSI_Sense_Data *		senseBuffer,
167	UInt8 *					senseBufferLen )
168{
169
170	STATUS_LOG ( ( "AppleSCSIPDT03Emulator::SendCommand, LUN = %qd\n", GetLogicalUnitNumber ( ) ) );
171
172	switch ( cdb[0] )
173	{
174
175		case kSCSICmd_TEST_UNIT_READY:
176		{
177
178			COMMAND_LOG ( ( "SCSI Command: TEST_UNIT_READY\n" ) );
179
180			*scsiStatus = kSCSITaskStatus_GOOD;
181			*dataLen = 0;
182			break;
183
184		}
185
186		case kSCSICmd_INQUIRY:
187		{
188
189			COMMAND_LOG ( ( "SCSI Command: INQUIRY\n" ) );
190
191			if ( cdb[1] == 1 )
192			{
193
194				COMMAND_LOG ( ( "INQUIRY VPD requested, PDT03 doesn't support it\n" ) );
195
196				*scsiStatus = kSCSITaskStatus_CHECK_CONDITION;
197
198				if ( senseBuffer != NULL )
199				{
200
201					UInt8	amount = min ( *senseBufferLen, sizeof ( SCSI_Sense_Data ) );
202
203					bzero ( senseBuffer, *senseBufferLen );
204					bcopy ( &gInvalidCDBFieldSenseData, senseBuffer, amount );
205
206					*senseBufferLen = amount;
207
208				}
209
210			}
211
212			else if ( ( cdb[1] == 2 ) || ( cdb[2] != 0 ) || ( cdb[3] != 0 ) )
213			{
214
215				COMMAND_LOG ( ( "Illegal request\n" ) );
216
217				// Don't support CMDDT bit, or PAGE_CODE without EVPD set.
218				*scsiStatus = kSCSITaskStatus_CHECK_CONDITION;
219
220				if ( senseBuffer != NULL )
221				{
222
223					UInt8	amount = min ( *senseBufferLen, sizeof ( SCSI_Sense_Data ) );
224
225					bzero ( senseBuffer, *senseBufferLen );
226					bcopy ( &gInvalidCDBFieldSenseData, senseBuffer, amount );
227
228					*senseBufferLen = amount;
229
230				}
231
232			}
233
234			else
235			{
236
237				COMMAND_LOG ( ( "Standard INQUIRY\n" ) );
238
239				*dataLen = min ( sizeof ( sInquiryData ), *dataLen );
240				dataDesc->writeBytes ( 0, &sInquiryData, *dataLen );
241
242				*scsiStatus = kSCSITaskStatus_GOOD;
243
244			}
245
246		}
247		break;
248
249		case kSCSICmd_REQUEST_SENSE:
250		{
251
252			COMMAND_LOG ( ( "SCSI Command: REQUEST_SENSE (desc = %s, allocation length = %d bytes) - returning CHECK CONDITION with INVALID COMMAND\n", (cdb[1] & 0x01) ? "TRUE" : "FALSE", cdb[4] ) );
253
254			*scsiStatus = kSCSITaskStatus_CHECK_CONDITION;
255			*dataLen = 0;
256
257			if ( senseBuffer != NULL )
258			{
259
260				UInt8	amount = min ( *senseBufferLen, sizeof ( SCSI_Sense_Data ) );
261
262				bzero ( senseBuffer, *senseBufferLen );
263				bcopy ( &gInvalidCommandSenseData, senseBuffer, amount );
264
265				*senseBufferLen = amount;
266
267			}
268
269		}
270		break;
271
272
273		default:
274		{
275
276			COMMAND_LOG ( ( "SCSI Command: Unknown: 0x%X\n", cdb[0] ) );
277
278			*scsiStatus = kSCSITaskStatus_CHECK_CONDITION;
279
280			if ( senseBuffer != NULL )
281			{
282
283				UInt8	amount = min ( *senseBufferLen, sizeof ( SCSI_Sense_Data ) );
284
285				bzero ( senseBuffer, *senseBufferLen );
286				bcopy ( &gInvalidCommandSenseData, senseBuffer, amount );
287
288				*senseBufferLen = amount;
289
290			}
291
292		}
293		break;
294
295	}
296
297	return 1;
298
299}