1/*
2  File: AppleSCSILogicalUnitEmulator.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 "AppleSCSILogicalUnitEmulator.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 												1
62#define DEBUG_ASSERT_COMPONENT_NAME_STRING					"LUNEmulator"
63
64#if DEBUG
65#define EMULATOR_ADAPTER_DEBUGGING_LEVEL					2
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
95#define super OSObject
96OSDefineMetaClass ( AppleSCSILogicalUnitEmulator, OSObject );
97OSDefineAbstractStructors ( AppleSCSILogicalUnitEmulator, OSObject );
98
99
100//-----------------------------------------------------------------------------
101//	Globals
102//-----------------------------------------------------------------------------
103
104SCSI_Sense_Data gInvalidCDBFieldSenseData =
105{
106	/* VALID_RESPONSE_CODE */				kSENSE_DATA_VALID | kSENSE_RESPONSE_CODE_Current_Errors,
107	/* SEGMENT_NUMBER */					0x00, // Obsolete
108	/* SENSE_KEY */							kSENSE_KEY_ILLEGAL_REQUEST,
109	/* INFORMATION_1 */						0x00,
110	/* INFORMATION_2 */						0x00,
111	/* INFORMATION_3 */						0x00,
112	/* INFORMATION_4 */						0x00,
113	/* ADDITIONAL_SENSE_LENGTH */			0x00,
114	/* COMMAND_SPECIFIC_INFORMATION_1 */	0x00,
115	/* COMMAND_SPECIFIC_INFORMATION_2 */	0x00,
116	/* COMMAND_SPECIFIC_INFORMATION_3 */	0x00,
117	/* COMMAND_SPECIFIC_INFORMATION_4 */	0x00,
118	/* ADDITIONAL_SENSE_CODE */				0x24, // INVALID FIELD IN CDB
119	/* ADDITIONAL_SENSE_CODE_QUALIFIER */	0x00,
120	/* FIELD_REPLACEABLE_UNIT_CODE */		0x00,
121	/* SKSV_SENSE_KEY_SPECIFIC_MSB */		0x00,
122	/* SENSE_KEY_SPECIFIC_MID */			0x00,
123	/* SENSE_KEY_SPECIFIC_LSB */			0x00
124};
125
126SCSI_Sense_Data gInvalidCommandSenseData =
127{
128	/* VALID_RESPONSE_CODE */				kSENSE_DATA_VALID | kSENSE_RESPONSE_CODE_Current_Errors,
129	/* SEGMENT_NUMBER */					0x00, // Obsolete
130	/* SENSE_KEY */							kSENSE_KEY_ILLEGAL_REQUEST,
131	/* INFORMATION_1 */						0x00,
132	/* INFORMATION_2 */						0x00,
133	/* INFORMATION_3 */						0x00,
134	/* INFORMATION_4 */						0x00,
135	/* ADDITIONAL_SENSE_LENGTH */			0x00,
136	/* COMMAND_SPECIFIC_INFORMATION_1 */	0x00,
137	/* COMMAND_SPECIFIC_INFORMATION_2 */	0x00,
138	/* COMMAND_SPECIFIC_INFORMATION_3 */	0x00,
139	/* COMMAND_SPECIFIC_INFORMATION_4 */	0x00,
140	/* ADDITIONAL_SENSE_CODE */				0x20, // Invalid command code
141	/* ADDITIONAL_SENSE_CODE_QUALIFIER */	0x00,
142	/* FIELD_REPLACEABLE_UNIT_CODE */		0x00,
143	/* SKSV_SENSE_KEY_SPECIFIC_MSB */		0x00,
144	/* SENSE_KEY_SPECIFIC_MID */			0x00,
145	/* SENSE_KEY_SPECIFIC_LSB */			0x00
146};
147
148
149//-----------------------------------------------------------------------------
150//	SetLogicalUnitNumber
151//-----------------------------------------------------------------------------
152
153void
154AppleSCSILogicalUnitEmulator::SetLogicalUnitNumber ( SCSILogicalUnitNumber logicalUnitNumber )
155{
156
157#if USE_LUN_BYTES
158	bzero ( fLogicalUnitBytes, sizeof ( SCSILogicalUnitBytes ) );
159
160	if ( logicalUnitNumber < 256 )
161	{
162
163		fLogicalUnitBytes[0] = 0;
164		fLogicalUnitBytes[1] = logicalUnitNumber & 0xFF;
165
166	}
167
168	else if ( logicalUnitNumber < 16384 )
169	{
170
171		fLogicalUnitBytes[0] = ( kREPORT_LUNS_ADDRESS_METHOD_FLAT_SPACE << 6 ) | ( ( logicalUnitNumber >> 8 ) & 0xFF );
172		fLogicalUnitBytes[1] = logicalUnitNumber & 0xFF;
173
174	}
175
176#endif
177
178	fLogicalUnitNumber = logicalUnitNumber;
179
180}
181
182
183//-----------------------------------------------------------------------------
184//	GetLogicalUnitNumber
185//-----------------------------------------------------------------------------
186
187SCSILogicalUnitNumber
188AppleSCSILogicalUnitEmulator::GetLogicalUnitNumber ( void )
189{
190	return fLogicalUnitNumber;
191}
192
193
194#if USE_LUN_BYTES
195
196//-----------------------------------------------------------------------------
197//	GetLogicalUnitBytes
198//-----------------------------------------------------------------------------
199
200void
201AppleSCSILogicalUnitEmulator::GetLogicalUnitBytes ( SCSILogicalUnitBytes * logicalUnitBytes )
202{
203	bcopy ( fLogicalUnitBytes, logicalUnitBytes, sizeof ( SCSILogicalUnitBytes ) );
204}
205
206#endif	/* USE_LUN_BYTES */