|
@@ -0,0 +1,142 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
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.
|
|
8
|
+ *
|
|
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.
|
|
13
|
+ *
|
|
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.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+#include <stddef.h>
|
|
20
|
+#include <string.h>
|
|
21
|
+#include <byteswap.h>
|
|
22
|
+#include <gpxe/blockdev.h>
|
|
23
|
+#include <gpxe/scsi.h>
|
|
24
|
+
|
|
25
|
+/** @file
|
|
26
|
+ *
|
|
27
|
+ * SCSI block device
|
|
28
|
+ *
|
|
29
|
+ */
|
|
30
|
+
|
|
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 );
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+/**
|
|
37
|
+ * Issue SCSI command
|
|
38
|
+ *
|
|
39
|
+ * @v scsi SCSI device
|
|
40
|
+ * @v command SCSI command
|
|
41
|
+ * @ret rc Return status code
|
|
42
|
+ */
|
|
43
|
+static int scsi_command ( struct scsi_device *scsi,
|
|
44
|
+ struct scsi_command *command ) {
|
|
45
|
+ return scsi->command ( scsi, command );
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+/**
|
|
49
|
+ * Read block from SCSI device
|
|
50
|
+ *
|
|
51
|
+ * @v blockdev Block device
|
|
52
|
+ * @v block LBA block number
|
|
53
|
+ * @v buffer Data buffer
|
|
54
|
+ * @ret rc Return status code
|
|
55
|
+ */
|
|
56
|
+static int scsi_read ( struct block_device *blockdev, uint64_t block,
|
|
57
|
+ void *buffer ) {
|
|
58
|
+ struct scsi_device *scsi = block_to_scsi ( blockdev );
|
|
59
|
+ struct scsi_command command;
|
|
60
|
+ struct scsi_cdb_read_16 *cdb = &command.cdb.read16;
|
|
61
|
+
|
|
62
|
+ /* Issue READ (16) */
|
|
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 );
|
|
70
|
+}
|
|
71
|
+
|
|
72
|
+/**
|
|
73
|
+ * Write block to SCSI device
|
|
74
|
+ *
|
|
75
|
+ * @v blockdev Block device
|
|
76
|
+ * @v block LBA block number
|
|
77
|
+ * @v buffer Data buffer
|
|
78
|
+ * @ret rc Return status code
|
|
79
|
+ */
|
|
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;
|
|
85
|
+
|
|
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 );
|
|
94
|
+}
|
|
95
|
+
|
|
96
|
+/**
|
|
97
|
+ * Read capacity of SCSI device
|
|
98
|
+ *
|
|
99
|
+ * @v blockdev Block device
|
|
100
|
+ * @ret rc Return status code
|
|
101
|
+ */
|
|
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;
|
|
107
|
+ int rc;
|
|
108
|
+
|
|
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 );
|
|
116
|
+
|
|
117
|
+ if ( ( rc = scsi_command ( scsi, &command ) ) != 0 )
|
|
118
|
+ return rc;
|
|
119
|
+
|
|
120
|
+ /* Fill in block device fields */
|
|
121
|
+ blockdev->blksize = be32_to_cpu ( capacity.blksize );
|
|
122
|
+ blockdev->blocks = ( be64_to_cpu ( capacity.lba ) + 1 );
|
|
123
|
+ return 0;
|
|
124
|
+}
|
|
125
|
+
|
|
126
|
+/**
|
|
127
|
+ * Initialise SCSI device
|
|
128
|
+ *
|
|
129
|
+ * @v scsi SCSI device
|
|
130
|
+ * @ret rc Return status code
|
|
131
|
+ *
|
|
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.
|
|
136
|
+ */
|
|
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 );
|
|
142
|
+}
|