david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

Defined SCSI device interface, and added SCSI block device

implementation.
This commit is contained in:
Michael Brown 2006-05-13 11:37:50 +00:00
parent 8638a5e471
commit f33f01c126
2 changed files with 281 additions and 9 deletions

142
src/drivers/block/scsi.c Normal file
View File

@ -0,0 +1,142 @@
/*
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stddef.h>
#include <string.h>
#include <byteswap.h>
#include <gpxe/blockdev.h>
#include <gpxe/scsi.h>
/** @file
*
* SCSI block device
*
*/
static inline __attribute__ (( always_inline )) struct scsi_device *
block_to_scsi ( struct block_device *blockdev ) {
return container_of ( blockdev, struct scsi_device, blockdev );
}
/**
* Issue SCSI command
*
* @v scsi SCSI device
* @v command SCSI command
* @ret rc Return status code
*/
static int scsi_command ( struct scsi_device *scsi,
struct scsi_command *command ) {
return scsi->command ( scsi, command );
}
/**
* Read block from SCSI device
*
* @v blockdev Block device
* @v block LBA block number
* @v buffer Data buffer
* @ret rc Return status code
*/
static int scsi_read ( struct block_device *blockdev, uint64_t block,
void *buffer ) {
struct scsi_device *scsi = block_to_scsi ( blockdev );
struct scsi_command command;
struct scsi_cdb_read_16 *cdb = &command.cdb.read16;
/* Issue READ (16) */
memset ( &command, 0, sizeof ( command ) );
cdb->opcode = SCSI_OPCODE_READ_16;
cdb->lba = cpu_to_be64 ( block );
cdb->len = cpu_to_be32 ( 1 ); /* always a single block */
command.data_in = buffer;
command.data_in_len = blockdev->blksize;
return scsi_command ( scsi, &command );
}
/**
* Write block to SCSI device
*
* @v blockdev Block device
* @v block LBA block number
* @v buffer Data buffer
* @ret rc Return status code
*/
static int scsi_write ( struct block_device *blockdev, uint64_t block,
const void *buffer ) {
struct scsi_device *scsi = block_to_scsi ( blockdev );
struct scsi_command command;
struct scsi_cdb_write_16 *cdb = &command.cdb.write16;
/* Issue WRITE (16) */
memset ( &command, 0, sizeof ( command ) );
cdb->opcode = SCSI_OPCODE_WRITE_16;
cdb->lba = cpu_to_be64 ( block );
cdb->len = cpu_to_be32 ( 1 ); /* always a single block */
command.data_out = buffer;
command.data_out_len = blockdev->blksize;
return scsi_command ( scsi, &command );
}
/**
* Read capacity of SCSI device
*
* @v blockdev Block device
* @ret rc Return status code
*/
static int scsi_read_capacity ( struct block_device *blockdev ) {
struct scsi_device *scsi = block_to_scsi ( blockdev );
struct scsi_command command;
struct scsi_cdb_read_capacity_16 *cdb = &command.cdb.readcap16;
struct scsi_capacity_16 capacity;
int rc;
/* Issue READ CAPACITY (16) */
memset ( &command, 0, sizeof ( command ) );
cdb->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
cdb->service_action = SCSI_SERVICE_ACTION_READ_CAPACITY_16;
cdb->len = cpu_to_be32 ( sizeof ( capacity ) );
command.data_in = &capacity;
command.data_in_len = sizeof ( capacity );
if ( ( rc = scsi_command ( scsi, &command ) ) != 0 )
return rc;
/* Fill in block device fields */
blockdev->blksize = be32_to_cpu ( capacity.blksize );
blockdev->blocks = ( be64_to_cpu ( capacity.lba ) + 1 );
return 0;
}
/**
* Initialise SCSI device
*
* @v scsi SCSI device
* @ret rc Return status code
*
* Initialises a SCSI device. The scsi_device::command and
* scsi_device::lun fields must already be filled in. This function
* will configure scsi_device::blockdev, including issuing a READ
* CAPACITY call to determine the block size and total device size.
*/
int init_scsidev ( struct scsi_device *scsi ) {
/** Fill in read and write methods, and get device capacity */
scsi->blockdev.read = scsi_read;
scsi->blockdev.write = scsi_write;
return scsi_read_capacity ( &scsi->blockdev );
}

View File

@ -2,9 +2,39 @@
#define _GPXE_SCSI_H
#include <stdint.h>
#include <gpxe/blockdev.h>
struct scsi_cdb_read_10 {
/** Opcode */
/**
* @defgroup scsiops SCSI operation codes
* @{
*/
#define SCSI_OPCODE_READ_16 0x88 /**< READ (16) */
#define SCSI_OPCODE_WRITE_16 0x8a /**< WRITE (16) */
#define SCSI_OPCODE_SERVICE_ACTION_IN 0x9e /**< SERVICE ACTION IN */
#define SCSI_SERVICE_ACTION_READ_CAPACITY_16 0x10 /**< READ CAPACITY (16) */
/** @} */
/**
* @defgroup scsiflags SCSI flags
* @{
*/
#define SCSI_FL_FUA_NV 0x02 /**< Force unit access to NVS */
#define SCSI_FL_FUA 0x08 /**< Force unit access */
#define SCSI_FL_DPO 0x10 /**< Disable cache page out */
/** @} */
/**
* @defgroup scsicdbs SCSI command data blocks
* @{
*/
/** A SCSI "READ (16)" CDB */
struct scsi_cdb_read_16 {
/** Opcode (0x88) */
uint8_t opcode;
/** Flags */
uint8_t flags;
@ -12,23 +42,123 @@ struct scsi_cdb_read_10 {
*
* This is a logical block number, in big-endian order.
*/
uint32_t lba;
/** Group number */
uint8_t group;
uint64_t lba;
/** Transfer length
*
* This is a logical block count.
* This is a logical block count, in big-endian order.
*/
uint16_t len;
uint32_t len;
/** Group number */
uint8_t group;
/** Control byte */
uint8_t control;
} __attribute__ (( packed ));
#define SCSI_OPCODE_READ_10 0x28
/** A SCSI "WRITE (16)" CDB */
struct scsi_cdb_write_16 {
/** Opcode (0x8a) */
uint8_t opcode;
/** Flags */
uint8_t flags;
/** Start address
*
* This is a logical block number, in big-endian order.
*/
uint64_t lba;
/** Transfer length
*
* This is a logical block count, in big-endian order.
*/
uint32_t len;
/** Group number */
uint8_t group;
/** Control byte */
uint8_t control;
} __attribute__ (( packed ));
/** A SCSI "READ CAPACITY (16)" CDB */
struct scsi_cdb_read_capacity_16 {
/** Opcode (0x9e) */
uint8_t opcode;
/** Service action */
uint8_t service_action;
/** Logical block address
*
* Applicable only if the PMI bit is set.
*/
uint64_t lba;
/** Transfer length
*
* This is the size of the data-in buffer, in bytes.
*/
uint32_t len;
/** Reserved */
uint8_t reserved;
/** Control byte */
uint8_t control;
} __attribute__ (( packed ));
/** SCSI "READ CAPACITY (16)" parameter data */
struct scsi_capacity_16 {
/** Maximum logical block number */
uint64_t lba;
/** Block length in bytes */
uint32_t blksize;
/** Reserved */
uint8_t reserved[20];
} __attribute__ (( packed ));
/** A SCSI Command Data Block */
union scsi_cdb {
struct scsi_cdb_read_10 read_10;
struct scsi_cdb_read_16 read16;
struct scsi_cdb_write_16 write16;
struct scsi_cdb_read_capacity_16 readcap16;
char bytes[16];
};
/** @} */
/** A SCSI command */
struct scsi_command {
/** CDB for this command */
union scsi_cdb cdb;
/** Data-out buffer (may be NULL) */
const void *data_out;
/** Data-out buffer length
*
* Must be zero if @c data_out is NULL
*/
size_t data_out_len;
/** Data-in buffer (may be NULL) */
void *data_in;
/** Data-in buffer length
*
* Must be zero if @c data_in is NULL
*/
size_t data_in_len;
};
/** A SCSI device */
struct scsi_device {
/** Block device interface */
struct block_device blockdev;
/** Logical unit number (LUN)
*
* This is a four-level LUN as specified by SAM-2, in
* big-endian order.
*/
uint64_t lun;
/**
* Issue SCSI command
*
* @v scsi SCSI device
* @v command SCSI command
* @ret rc Return status code
*/
int ( * command ) ( struct scsi_device *scsi,
struct scsi_command *command );
};
extern int init_scsidev ( struct scsi_device *scsi );
#endif /* _GPXE_SCSI_H */