3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  SCSI command processing routines, for SCSI commands issued by the host. Mass Storage 
  34  *  devices use a thin "Bulk-Only Transport" protocol for issuing commands and status information, 
  35  *  which wrap around standard SCSI device commands for controlling the actual storage medium. 
  38 #define  INCLUDE_FROM_SCSI_C 
  41 /** Structure to hold the SCSI response data to a SCSI INQUIRY command. This gives information about the device's 
  42  *  features and capabilities. 
  44 SCSI_Inquiry_Response_t InquiryData 
= 
  46                 .DeviceType          
= DEVICE_TYPE_BLOCK
, 
  47                 .PeripheralQualifier 
= 0, 
  53                 .ResponseDataFormat  
= 2, 
  58                 .AdditionalLength    
= 0x1F, 
  64                 .WideBus16Bit        
= false, 
  65                 .WideBus32Bit        
= false, 
  69                 .ProductID           
= "Dataflash Disk", 
  70                 .RevisionID          
= {'0','.','0','0'}, 
  73 /** Structure to hold the sense data for the last issued SCSI command, which is returned to the host after a SCSI REQUEST SENSE 
  74  *  command is issued. This gives information on exactly why the last command failed to complete. 
  76 SCSI_Request_Sense_Response_t SenseData 
= 
  79                 .AdditionalLength    
= 0x0A, 
  83 /** Main routine to process the SCSI command located in the Command Block Wrapper read from the host. This dispatches 
  84  *  to the appropriate SCSI command handling routine if the issued command is supported by the device, else it returns 
  85  *  a command failure due to a ILLEGAL REQUEST. 
  87  *  \return Boolean true if the command completed successfully, false otherwise 
  89 bool SCSI_DecodeSCSICommand(void) 
  91         bool CommandSuccess 
= false; 
  93         /* Run the appropriate SCSI command hander function based on the passed command */ 
  94         switch (CommandBlock
.SCSICommandData
[0]) 
  96                 case SCSI_CMD_INQUIRY
: 
  97                         CommandSuccess 
= SCSI_Command_Inquiry(); 
  99                 case SCSI_CMD_REQUEST_SENSE
: 
 100                         CommandSuccess 
= SCSI_Command_Request_Sense(); 
 102                 case SCSI_CMD_READ_CAPACITY_10
: 
 103                         CommandSuccess 
= SCSI_Command_Read_Capacity_10(); 
 105                 case SCSI_CMD_SEND_DIAGNOSTIC
: 
 106                         CommandSuccess 
= SCSI_Command_Send_Diagnostic(); 
 108                 case SCSI_CMD_WRITE_10
: 
 109                         CommandSuccess 
= SCSI_Command_ReadWrite_10(DATA_WRITE
); 
 111                 case SCSI_CMD_READ_10
: 
 112                         CommandSuccess 
= SCSI_Command_ReadWrite_10(DATA_READ
); 
 114                 case SCSI_CMD_TEST_UNIT_READY
: 
 115                 case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
: 
 116                 case SCSI_CMD_VERIFY_10
: 
 117                         /* These commands should just succeed, no handling required */ 
 118                         CommandSuccess 
= true; 
 119                         CommandBlock
.DataTransferLength 
= 0; 
 122                         /* Update the SENSE key to reflect the invalid command */ 
 123                         SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST
, 
 124                                    SCSI_ASENSE_INVALID_COMMAND
, 
 125                                    SCSI_ASENSEQ_NO_QUALIFIER
); 
 129         /* Check if command was successfully processed */ 
 132                 SCSI_SET_SENSE(SCSI_SENSE_KEY_GOOD
, 
 133                                SCSI_ASENSE_NO_ADDITIONAL_INFORMATION
, 
 134                                SCSI_ASENSEQ_NO_QUALIFIER
); 
 142 /** Command processing for an issued SCSI INQUIRY command. This command returns information about the device's features 
 143  *  and capabilities to the host. 
 145  *  \return Boolean true if the command completed successfully, false otherwise. 
 147 static bool SCSI_Command_Inquiry(void) 
 149         uint16_t AllocationLength  
= SwapEndian_16(*(uint16_t*)&CommandBlock
.SCSICommandData
[3]); 
 150         uint16_t BytesTransferred  
= (AllocationLength 
< sizeof(InquiryData
))? AllocationLength 
: 
 153         /* Only the standard INQUIRY data is supported, check if any optional INQUIRY bits set */ 
 154         if ((CommandBlock
.SCSICommandData
[1] & ((1 << 0) | (1 << 1))) || 
 155              CommandBlock
.SCSICommandData
[2]) 
 157                 /* Optional but unsupported bits set - update the SENSE key and fail the request */ 
 158                 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST
, 
 159                                SCSI_ASENSE_INVALID_FIELD_IN_CDB
, 
 160                                SCSI_ASENSEQ_NO_QUALIFIER
); 
 165         /* Write the INQUIRY data to the endpoint */ 
 166         Endpoint_Write_Stream_LE(&InquiryData
, BytesTransferred
, StreamCallback_AbortOnMassStoreReset
); 
 168         uint8_t PadBytes
[AllocationLength 
- BytesTransferred
]; 
 170         /* Pad out remaining bytes with 0x00 */ 
 171         Endpoint_Write_Stream_LE(&PadBytes
, sizeof(PadBytes
), StreamCallback_AbortOnMassStoreReset
); 
 173         /* Finalize the stream transfer to send the last packet */ 
 176         /* Succeed the command and update the bytes transferred counter */ 
 177         CommandBlock
.DataTransferLength 
-= BytesTransferred
; 
 182 /** Command processing for an issued SCSI REQUEST SENSE command. This command returns information about the last issued command, 
 183  *  including the error code and additional error information so that the host can determine why a command failed to complete. 
 185  *  \return Boolean true if the command completed successfully, false otherwise. 
 187 static bool SCSI_Command_Request_Sense(void) 
 189         uint8_t  AllocationLength 
= CommandBlock
.SCSICommandData
[4]; 
 190         uint8_t  BytesTransferred 
= (AllocationLength 
< sizeof(SenseData
))? AllocationLength 
: sizeof(SenseData
); 
 192         /* Send the SENSE data - this indicates to the host the status of the last command */ 
 193         Endpoint_Write_Stream_LE(&SenseData
, BytesTransferred
, StreamCallback_AbortOnMassStoreReset
); 
 195         uint8_t PadBytes
[AllocationLength 
- BytesTransferred
]; 
 197         /* Pad out remaining bytes with 0x00 */ 
 198         Endpoint_Write_Stream_LE(&PadBytes
, sizeof(PadBytes
), StreamCallback_AbortOnMassStoreReset
); 
 200         /* Finalize the stream transfer to send the last packet */ 
 203         /* Succeed the command and update the bytes transferred counter */ 
 204         CommandBlock
.DataTransferLength 
-= BytesTransferred
; 
 209 /** Command processing for an issued SCSI READ CAPACITY (10) command. This command returns information about the device's capacity 
 210  *  on the selected Logical Unit (drive), as a number of OS-sized blocks. 
 212  *  \return Boolean true if the command completed successfully, false otherwise. 
 214 static bool SCSI_Command_Read_Capacity_10(void) 
 216         /* Send the total number of logical blocks in the current LUN */ 
 217         Endpoint_Write_DWord_BE(LUN_MEDIA_BLOCKS 
- 1); 
 219         /* Send the logical block size of the device (must be 512 bytes) */ 
 220         Endpoint_Write_DWord_BE(VIRTUAL_MEMORY_BLOCK_SIZE
); 
 222         /* Check if the current command is being aborted by the host */ 
 223         if (IsMassStoreReset
) 
 226         /* Send the endpoint data packet to the host */ 
 229         /* Succeed the command and update the bytes transferred counter */ 
 230         CommandBlock
.DataTransferLength 
-= 8; 
 235 /** Command processing for an issued SCSI SEND DIAGNOSTIC command. This command performs a quick check of the Dataflash ICs on the 
 236  *  board, and indicates if they are present and functioning correctly. Only the Self-Test portion of the diagnostic command is 
 239  *  \return Boolean true if the command completed successfully, false otherwise. 
 241 static bool SCSI_Command_Send_Diagnostic(void) 
 243         /* Check to see if the SELF TEST bit is not set */ 
 244         if (!(CommandBlock
.SCSICommandData
[1] & (1 << 2))) 
 246                 /* Only self-test supported - update SENSE key and fail the command */ 
 247                 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST
, 
 248                                SCSI_ASENSE_INVALID_FIELD_IN_CDB
, 
 249                                SCSI_ASENSEQ_NO_QUALIFIER
); 
 254         /* Check to see if all attached Dataflash ICs are functional */ 
 255         if (!(DataflashManager_CheckDataflashOperation())) 
 257                 /* Update SENSE key with a hardware error condition and return command fail */ 
 258                 SCSI_SET_SENSE(SCSI_SENSE_KEY_HARDWARE_ERROR
, 
 259                                SCSI_ASENSE_NO_ADDITIONAL_INFORMATION
, 
 260                                SCSI_ASENSEQ_NO_QUALIFIER
); 
 265         /* Succeed the command and update the bytes transferred counter */ 
 266         CommandBlock
.DataTransferLength 
= 0; 
 271 /** Command processing for an issued SCSI READ (10) or WRITE (10) command. This command reads in the block start address 
 272  *  and total number of blocks to process, then calls the appropriate low-level Dataflash routine to handle the actual 
 273  *  reading and writing of the data. 
 275  *  \param[in] IsDataRead  Indicates if the command is a READ (10) command or WRITE (10) command (DATA_READ or DATA_WRITE) 
 277  *  \return Boolean true if the command completed successfully, false otherwise. 
 279 static bool SCSI_Command_ReadWrite_10(const bool IsDataRead
) 
 281         uint32_t BlockAddress 
= SwapEndian_32(*(uint32_t*)&CommandBlock
.SCSICommandData
[2]); 
 282         uint16_t TotalBlocks  
= SwapEndian_16(*(uint16_t*)&CommandBlock
.SCSICommandData
[7]); 
 284         /* Check if the block address is outside the maximum allowable value for the LUN */ 
 285         if (BlockAddress 
>= LUN_MEDIA_BLOCKS
) 
 287                 /* Block address is invalid, update SENSE key and return command fail */ 
 288                 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST
, 
 289                                SCSI_ASENSE_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
, 
 290                                SCSI_ASENSEQ_NO_QUALIFIER
); 
 296         /* Adjust the given block address to the real media address based on the selected LUN */ 
 297         BlockAddress 
+= ((uint32_t)CommandBlock
.LUN 
* LUN_MEDIA_BLOCKS
); 
 300         /* Determine if the packet is a READ (10) or WRITE (10) command, call appropriate function */ 
 301         if (IsDataRead 
== DATA_READ
) 
 302           DataflashManager_ReadBlocks(BlockAddress
, TotalBlocks
); 
 304           DataflashManager_WriteBlocks(BlockAddress
, TotalBlocks
); 
 306         /* Update the bytes transferred counter and succeed the command */ 
 307         CommandBlock
.DataTransferLength 
-= ((uint32_t)TotalBlocks 
* VIRTUAL_MEMORY_BLOCK_SIZE
);