2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/blockdev.h>
23 #include <gpxe/scsi.h>
31 static inline __attribute__ (( always_inline )) struct scsi_device *
32 block_to_scsi ( struct block_device *blockdev ) {
33 return container_of ( blockdev, struct scsi_device, blockdev );
40 * @v command SCSI command
41 * @ret rc Return status code
43 static int scsi_command ( struct scsi_device *scsi,
44 struct scsi_command *command ) {
45 return scsi->command ( scsi, command );
49 * Read block from SCSI device
51 * @v blockdev Block device
52 * @v block LBA block number
53 * @v buffer Data buffer
54 * @ret rc Return status code
56 static int scsi_read ( struct block_device *blockdev, uint64_t block,
58 struct scsi_device *scsi = block_to_scsi ( blockdev );
59 struct scsi_command command;
60 struct scsi_cdb_read_16 *cdb = &command.cdb.read16;
63 memset ( &command, 0, sizeof ( command ) );
64 cdb->opcode = SCSI_OPCODE_READ_16;
65 cdb->lba = cpu_to_be64 ( block );
66 cdb->len = cpu_to_be32 ( 1 ); /* always a single block */
67 command.data_in = buffer;
68 command.data_in_len = blockdev->blksize;
69 return scsi_command ( scsi, &command );
73 * Write block to SCSI device
75 * @v blockdev Block device
76 * @v block LBA block number
77 * @v buffer Data buffer
78 * @ret rc Return status code
80 static int scsi_write ( struct block_device *blockdev, uint64_t block,
81 const void *buffer ) {
82 struct scsi_device *scsi = block_to_scsi ( blockdev );
83 struct scsi_command command;
84 struct scsi_cdb_write_16 *cdb = &command.cdb.write16;
86 /* Issue WRITE (16) */
87 memset ( &command, 0, sizeof ( command ) );
88 cdb->opcode = SCSI_OPCODE_WRITE_16;
89 cdb->lba = cpu_to_be64 ( block );
90 cdb->len = cpu_to_be32 ( 1 ); /* always a single block */
91 command.data_out = buffer;
92 command.data_out_len = blockdev->blksize;
93 return scsi_command ( scsi, &command );
97 * Read capacity of SCSI device
99 * @v blockdev Block device
100 * @ret rc Return status code
102 static int scsi_read_capacity ( struct block_device *blockdev ) {
103 struct scsi_device *scsi = block_to_scsi ( blockdev );
104 struct scsi_command command;
105 struct scsi_cdb_read_capacity_16 *cdb = &command.cdb.readcap16;
106 struct scsi_capacity_16 capacity;
109 /* Issue READ CAPACITY (16) */
110 memset ( &command, 0, sizeof ( command ) );
111 cdb->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
112 cdb->service_action = SCSI_SERVICE_ACTION_READ_CAPACITY_16;
113 cdb->len = cpu_to_be32 ( sizeof ( capacity ) );
114 command.data_in = &capacity;
115 command.data_in_len = sizeof ( capacity );
117 if ( ( rc = scsi_command ( scsi, &command ) ) != 0 )
120 /* Fill in block device fields */
121 blockdev->blksize = be32_to_cpu ( capacity.blksize );
122 blockdev->blocks = ( be64_to_cpu ( capacity.lba ) + 1 );
127 * Initialise SCSI device
129 * @v scsi SCSI device
130 * @ret rc Return status code
132 * Initialises a SCSI device. The scsi_device::command and
133 * scsi_device::lun fields must already be filled in. This function
134 * will configure scsi_device::blockdev, including issuing a READ
135 * CAPACITY call to determine the block size and total device size.
137 int init_scsidev ( struct scsi_device *scsi ) {
138 /** Fill in read and write methods, and get device capacity */
139 scsi->blockdev.read = scsi_read;
140 scsi->blockdev.write = scsi_write;
141 return scsi_read_capacity ( &scsi->blockdev );