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 count Block count
54 * @v buffer Data buffer
55 * @ret rc Return status code
57 static int scsi_read ( struct block_device *blockdev, uint64_t block,
58 unsigned long count, userptr_t buffer ) {
59 struct scsi_device *scsi = block_to_scsi ( blockdev );
60 struct scsi_command command;
61 struct scsi_cdb_read_16 *cdb = &command.cdb.read16;
64 memset ( &command, 0, sizeof ( command ) );
65 cdb->opcode = SCSI_OPCODE_READ_16;
66 cdb->lba = cpu_to_be64 ( block );
67 cdb->len = cpu_to_be32 ( count );
68 command.data_in = buffer;
69 command.data_in_len = ( count * blockdev->blksize );
70 return scsi_command ( scsi, &command );
74 * Write block to SCSI device
76 * @v blockdev Block device
77 * @v block LBA block number
78 * @v count Block count
79 * @v buffer Data buffer
80 * @ret rc Return status code
82 static int scsi_write ( struct block_device *blockdev, uint64_t block,
83 unsigned long count, userptr_t buffer ) {
84 struct scsi_device *scsi = block_to_scsi ( blockdev );
85 struct scsi_command command;
86 struct scsi_cdb_write_16 *cdb = &command.cdb.write16;
88 /* Issue WRITE (16) */
89 memset ( &command, 0, sizeof ( command ) );
90 cdb->opcode = SCSI_OPCODE_WRITE_16;
91 cdb->lba = cpu_to_be64 ( block );
92 cdb->len = cpu_to_be32 ( count );
93 command.data_out = buffer;
94 command.data_out_len = ( count * blockdev->blksize );
95 return scsi_command ( scsi, &command );
99 * Read capacity of SCSI device via READ CAPACITY (10)
101 * @v blockdev Block device
102 * @ret rc Return status code
104 static int scsi_read_capacity_10 ( struct block_device *blockdev ) {
105 struct scsi_device *scsi = block_to_scsi ( blockdev );
106 struct scsi_command command;
107 struct scsi_cdb_read_capacity_10 *cdb = &command.cdb.readcap10;
108 struct scsi_capacity_10 capacity;
111 /* Issue READ CAPACITY (10) */
112 memset ( &command, 0, sizeof ( command ) );
113 cdb->opcode = SCSI_OPCODE_READ_CAPACITY_10;
114 command.data_in = virt_to_user ( &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 = ( be32_to_cpu ( capacity.lba ) + 1 );
128 * Read capacity of SCSI device via READ CAPACITY (16)
130 * @v blockdev Block device
131 * @ret rc Return status code
133 static int scsi_read_capacity_16 ( struct block_device *blockdev ) {
134 struct scsi_device *scsi = block_to_scsi ( blockdev );
135 struct scsi_command command;
136 struct scsi_cdb_read_capacity_16 *cdb = &command.cdb.readcap16;
137 struct scsi_capacity_16 capacity;
140 /* Issue READ CAPACITY (16) */
141 memset ( &command, 0, sizeof ( command ) );
142 cdb->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
143 cdb->service_action = SCSI_SERVICE_ACTION_READ_CAPACITY_16;
144 cdb->len = cpu_to_be32 ( sizeof ( capacity ) );
145 command.data_in = virt_to_user ( &capacity );
146 command.data_in_len = sizeof ( capacity );
148 if ( ( rc = scsi_command ( scsi, &command ) ) != 0 )
151 /* Fill in block device fields */
152 blockdev->blksize = be32_to_cpu ( capacity.blksize );
153 blockdev->blocks = ( be64_to_cpu ( capacity.lba ) + 1 );
158 * Read capacity of SCSI device
160 * @v blockdev Block device
161 * @ret rc Return status code
163 static int scsi_read_capacity ( struct block_device *blockdev ) {
166 /* Try READ CAPACITY (10), which is a mandatory command, first. */
167 if ( ( rc = scsi_read_capacity_10 ( blockdev ) ) != 0 )
170 /* If capacity range was exceeded (i.e. capacity.lba was
171 * 0xffffffff, meaning that blockdev->blocks is now zero), use
172 * READ CAPACITY (16) instead. READ CAPACITY (16) is not
173 * mandatory, so we can't just use it straight off.
175 if ( blockdev->blocks == 0 ) {
176 if ( ( rc = scsi_read_capacity_16 ( blockdev ) ) != 0 )
184 * Initialise SCSI device
186 * @v scsi SCSI device
187 * @ret rc Return status code
189 * Initialises a SCSI device. The scsi_device::command and
190 * scsi_device::lun fields must already be filled in. This function
191 * will configure scsi_device::blockdev, including issuing a READ
192 * CAPACITY call to determine the block size and total device size.
194 int init_scsidev ( struct scsi_device *scsi ) {
195 /** Fill in read and write methods, and get device capacity */
196 scsi->blockdev.read = scsi_read;
197 scsi->blockdev.write = scsi_write;
198 return scsi_read_capacity ( &scsi->blockdev );