10 #include "module_debug.h"
12 #define getBI32(__buf) ((((u8 *)(__buf))[3] << 0) | (((u8 *)(__buf))[2] << 8) | (((u8 *)(__buf))[1] << 16) | (((u8 *)(__buf))[0] << 24))
13 #define SCSI_MAX_RETRIES 16
17 u8 peripheral_device_type;
20 u8 response_data_format;
65 static int scsi_cmd(
struct block_device *bd,
unsigned char cmd,
void *buffer,
int buf_size,
int cmd_size)
67 unsigned char comData[12] = {0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
73 comData[4] = cmd_size;
75 return scsi->queue_cmd(scsi, comData, 12, buffer, buf_size, 0);
78 static inline int scsi_cmd_test_unit_ready(
struct block_device *bd)
80 M_DEBUG(
"%s\n", __func__);
82 return scsi_cmd(bd, 0x00, NULL, 0, 0);
85 static inline int scsi_cmd_request_sense(
struct block_device *bd,
void *buffer,
int size)
87 M_DEBUG(
"%s\n", __func__);
89 return scsi_cmd(bd, 0x03, buffer, size, size);
92 static inline int scsi_cmd_inquiry(
struct block_device *bd,
void *buffer,
int size)
94 M_DEBUG(
"%s\n", __func__);
96 return scsi_cmd(bd, 0x12, buffer, size, size);
99 static int scsi_cmd_start_stop_unit(
struct block_device *bd, u8 param)
101 M_DEBUG(
"%s\n", __func__);
103 return scsi_cmd(bd, 0x1b, NULL, 0, param);
106 static inline int scsi_cmd_read_capacity10(
struct block_device *bd,
void *buffer,
int buf_size)
108 M_DEBUG(
"%s\n", __func__);
110 return scsi_cmd(bd, 0x25, buffer, buf_size, 0);
113 static inline int scsi_cmd_read_capacity16(
struct block_device *bd,
void *buffer,
int buf_size)
115 unsigned char comData[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
118 M_DEBUG(
"%s\n", __func__);
122 comData[13] = buf_size;
124 return scsi->queue_cmd(scsi, comData, 16, buffer, buf_size, 0);
127 static int scsi_cmd_rw_sector(
struct block_device *bd, u64 lba,
const void *buffer,
unsigned short int sectorCount,
unsigned int write)
129 unsigned char comData[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
132 DEBUG_U64_2XU32(lba);
133 M_DEBUG(
"scsi_cmd_rw_sector - 0x%08x%08x %p 0x%04x\n", lba_u32[1], lba_u32[0], buffer, sectorCount);
138 comData[0] = write ? 0x2a : 0x28;
139 comData[2] = (lba & 0xFF000000) >> 24;
140 comData[3] = (lba & 0xFF0000) >> 16;
141 comData[4] = (lba & 0xFF00) >> 8;
142 comData[5] = (lba & 0xFF);
143 comData[7] = (sectorCount & 0xFF00) >> 8;
144 comData[8] = (sectorCount & 0xFF);
145 return scsi->queue_cmd(scsi, comData, 12, (
void *)buffer, bd->sectorSize * sectorCount, write);
158 M_DEBUG(
"%s\n", __func__);
160 stat = scsi->get_max_lun(scsi);
161 M_DEBUG(
"scsi->get_max_lun %d\n", stat);
164 if ((stat = scsi_cmd_inquiry(bd, &
id,
sizeof(
inquiry_data))) < 0) {
165 M_PRINTF(
"ERROR: scsi_cmd_inquiry %d\n", stat);
169 M_PRINTF(
"Vendor: %.8s\n",
id.vendor);
170 M_PRINTF(
"Product: %.16s\n",
id.product);
171 M_PRINTF(
"Revision: %.4s\n",
id.revision);
173 while ((stat = scsi_cmd_test_unit_ready(bd)) != 0) {
174 M_PRINTF(
"ERROR: scsi_cmd_test_unit_ready %d\n", stat);
177 stat = scsi_cmd_request_sense(bd, &sd,
sizeof(
sense_data));
179 M_PRINTF(
"ERROR: scsi_cmd_request_sense %d\n", stat);
182 if ((sd.error_code == 0x70) && (sd.sense_key != 0x00)) {
183 M_PRINTF(
"Sense Data key: %02X code: %02X qual: %02X\n", sd.sense_key, sd.add_sense_code, sd.add_sense_qual);
185 if ((sd.sense_key == 0x02) && (sd.add_sense_code == 0x04) && (sd.add_sense_qual == 0x02)) {
186 M_PRINTF(
"ERROR: Additional initalization is required for this device!\n");
187 if ((stat = scsi_cmd_start_stop_unit(bd, 1)) != 0) {
188 M_PRINTF(
"ERROR: scsi_cmd_start_stop_unit %d\n", stat);
199 M_PRINTF(
"ERROR: scsi_cmd_read_capacity10 %d\n", stat);
202 bd->sectorCount = getBI32(&rc10d.last_lba);
203 bd->sectorSize = getBI32(&rc10d.block_length);
204 bd->sectorOffset = 0;
217 u64 sectorCount = bd->sectorCount;
218 U64_2XU32(sectorCount);
219 M_PRINTF(
"0x%08x%08x %u-byte logical blocks: (%lu MB / %lu MiB)\n", sectorCount_u32[1], sectorCount_u32[0], bd->sectorSize,
220 (u32)(bd->sectorCount / ((1000 * 1000) / bd->sectorSize)), (u32)(bd->sectorCount / ((1024 * 1024) / bd->sectorSize)));
228 static int scsi_read(
struct block_device *bd, u64 sector,
void *buffer, u16
count)
231 u16 sc_remaining =
count;
234 DEBUG_U64_2XU32(sector);
235 M_DEBUG(
"%s: sector=0x%08x%08x, count=%d\n", __func__, sector_u32[1], sector_u32[0],
count);
237 while (sc_remaining > 0) {
238 u16 sc = sc_remaining > scsi->max_sectors ? scsi->max_sectors : sc_remaining;
240 for (retries = SCSI_MAX_RETRIES; retries > 0; retries--) {
241 if (scsi_cmd_rw_sector(bd, sector, buffer, sc, 0) == 0)
250 buffer = (u8 *)buffer + (sc * bd->sectorSize);
256 static int scsi_write(
struct block_device *bd, u64 sector,
const void *buffer, u16
count)
259 u16 sc_remaining =
count;
262 DEBUG_U64_2XU32(sector);
263 M_DEBUG(
"%s: sector=0x%08x%08x, count=%d\n", __func__, sector_u32[1], sector_u32[0],
count);
265 while (sc_remaining > 0) {
266 u16 sc = sc_remaining > scsi->max_sectors ? scsi->max_sectors : sc_remaining;
268 for (retries = SCSI_MAX_RETRIES; retries > 0; retries--) {
269 if (scsi_cmd_rw_sector(bd, sector, buffer, sc, 1) == 0)
278 buffer = (u8 *)buffer + (sc * bd->sectorSize);
288 M_DEBUG(
"%s\n", __func__);
297 M_DEBUG(
"%s\n", __func__);
299 if ((stat = scsi_cmd_start_stop_unit(bd, 0)) != 0) {
300 M_PRINTF(
"ERROR: scsi_cmd_start_stop_unit %d\n", stat);
313 M_DEBUG(
"%s\n", __func__);
315 for (i = 0; i < NUM_DEVICES; ++i) {
316 if (g_scsi_bd[i].priv == NULL) {
320 bd->name = scsi->name;
322 bd->devNr = scsi->devNr;
334 M_DEBUG(
"%s\n", __func__);
336 for (i = 0; i < NUM_DEVICES; ++i) {
337 if (g_scsi_bd[i].priv == scsi) {
339 bdm_disconnect_bd(bd);
350 M_DEBUG(
"%s\n", __func__);
352 for (i = 0; i < NUM_DEVICES; ++i) {
353 g_scsi_bd[i].parNr = 0;
354 g_scsi_bd[i].parId = 0x00;
356 g_scsi_bd[i].priv = NULL;
357 g_scsi_bd[i].read = scsi_read;
358 g_scsi_bd[i].write = scsi_write;
359 g_scsi_bd[i].flush = scsi_flush;
360 g_scsi_bd[i].stop = scsi_stop;