1 |
/* |
2 |
* scsi_beos.cpp - SCSI Manager, BeOS specific stuff |
3 |
* |
4 |
* Basilisk II (C) 1997-1999 Christian Bauer |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#include <string.h> |
22 |
#include <stdlib.h> |
23 |
#include <stdio.h> |
24 |
#include <unistd.h> |
25 |
#include <device/scsi.h> |
26 |
#include <drivers/CAM.h> |
27 |
|
28 |
#include "sysdeps.h" |
29 |
#include "main.h" |
30 |
#include "prefs.h" |
31 |
#include "user_strings.h" |
32 |
#include "scsi.h" |
33 |
|
34 |
#define DEBUG 0 |
35 |
#include "debug.h" |
36 |
|
37 |
|
38 |
// Global variables |
39 |
static raw_device_command rdc; |
40 |
|
41 |
static int fds[8*8]; // fd's for 8 units and 8 LUNs each |
42 |
static int fd; // Active fd (selected target) |
43 |
|
44 |
static uint32 buffer_size; // Size of data buffer |
45 |
static uint8 *buffer = NULL; // Pointer to data buffer |
46 |
|
47 |
static uint8 sense_data[256]; // Buffer for autosense data |
48 |
|
49 |
|
50 |
/* |
51 |
* Initialization |
52 |
*/ |
53 |
|
54 |
void SCSIInit(void) |
55 |
{ |
56 |
int id, lun; |
57 |
|
58 |
// Allocate buffer |
59 |
buffer = (uint8 *)malloc(buffer_size = 0x10000); |
60 |
|
61 |
// Open scsi_raw driver for all 8 units (and all 8 LUNs) |
62 |
char dev_name[256]; |
63 |
for (id=0; id<8; id++) { |
64 |
for (lun=0; lun<8; lun++) |
65 |
fds[id*8+lun] = -1; |
66 |
char prefs_name[16]; |
67 |
sprintf(prefs_name, "scsi%d", id); |
68 |
const char *str = PrefsFindString(prefs_name); |
69 |
if (str) { |
70 |
int bus, unit; |
71 |
if (sscanf(str, "%d/%d", &bus, &unit) == 2) { |
72 |
for (lun=0; lun<8; lun++) { |
73 |
sprintf(dev_name, "/dev/bus/scsi/%d/%d/%d/raw", bus, unit, lun); |
74 |
D(bug("SCSI %d: Opening %s\n", id, dev_name)); |
75 |
fds[id*8+lun] = open(dev_name, O_RDWR); |
76 |
} |
77 |
} |
78 |
} |
79 |
} |
80 |
|
81 |
// Reset SCSI bus |
82 |
SCSIReset(); |
83 |
|
84 |
// Init rdc |
85 |
memset(&rdc, 0, sizeof(rdc)); |
86 |
rdc.data = buffer; |
87 |
rdc.sense_data = sense_data; |
88 |
} |
89 |
|
90 |
|
91 |
/* |
92 |
* Deinitialization |
93 |
*/ |
94 |
|
95 |
void SCSIExit(void) |
96 |
{ |
97 |
// Close all devices |
98 |
for (int i=0; i<8; i++) |
99 |
for (int j=0; j<8; j++) { |
100 |
int fd = fds[i*8+j]; |
101 |
if (fd > 0) |
102 |
close(fd); |
103 |
} |
104 |
|
105 |
// Free buffer |
106 |
if (buffer) { |
107 |
free(buffer); |
108 |
buffer = NULL; |
109 |
} |
110 |
} |
111 |
|
112 |
|
113 |
/* |
114 |
* Check if requested data size fits into buffer, allocate new buffer if needed |
115 |
*/ |
116 |
|
117 |
static bool try_buffer(int size) |
118 |
{ |
119 |
if (size <= buffer_size) |
120 |
return true; |
121 |
|
122 |
uint8 *new_buffer = (uint8 *)malloc(size); |
123 |
if (new_buffer == NULL) |
124 |
return false; |
125 |
free(buffer); |
126 |
buffer = new_buffer; |
127 |
buffer_size = size; |
128 |
return true; |
129 |
} |
130 |
|
131 |
|
132 |
/* |
133 |
* Set SCSI command to be sent by scsi_send_cmd() |
134 |
*/ |
135 |
|
136 |
void scsi_set_cmd(int cmd_length, uint8 *cmd) |
137 |
{ |
138 |
rdc.command_length = cmd_length; |
139 |
memcpy(rdc.command, cmd, cmd_length); |
140 |
} |
141 |
|
142 |
|
143 |
/* |
144 |
* Check for presence of SCSI target |
145 |
*/ |
146 |
|
147 |
bool scsi_is_target_present(int id) |
148 |
{ |
149 |
return fds[id * 8] > 0; |
150 |
} |
151 |
|
152 |
|
153 |
/* |
154 |
* Set SCSI target (returns false on error) |
155 |
*/ |
156 |
|
157 |
bool scsi_set_target(int id, int lun) |
158 |
{ |
159 |
int new_fd = fds[id * 8 + lun]; |
160 |
if (new_fd < 0) |
161 |
return false; |
162 |
if (new_fd != fd) |
163 |
rdc.cam_status &= ~CAM_AUTOSNS_VALID; // Clear sense data when selecting new target |
164 |
fd = new_fd; |
165 |
return true; |
166 |
} |
167 |
|
168 |
|
169 |
/* |
170 |
* Send SCSI command to active target (scsi_set_command() must have been called), |
171 |
* read/write data according to S/G table (returns false on error); timeout is in 1/60 sec |
172 |
*/ |
173 |
|
174 |
bool scsi_send_cmd(size_t data_length, bool reading, int sg_size, uint8 **sg_ptr, uint32 *sg_len, uint16 *stat, uint32 timeout) |
175 |
{ |
176 |
// Check if buffer is large enough, allocate new buffer if needed |
177 |
if (!try_buffer(data_length)) { |
178 |
char str[256]; |
179 |
sprintf(str, GetString(STR_SCSI_BUFFER_ERR), data_length); |
180 |
ErrorAlert(str); |
181 |
return false; |
182 |
} |
183 |
|
184 |
// Process S/G table when writing |
185 |
if (!reading) { |
186 |
D(bug(" writing to buffer\n")); |
187 |
uint8 *buffer_ptr = buffer; |
188 |
for (int i=0; i<sg_size; i++) { |
189 |
uint32 len = sg_len[i]; |
190 |
D(bug(" %d bytes from %08lx\n", len, sg_ptr[i])); |
191 |
memcpy(buffer_ptr, sg_ptr[i], len); |
192 |
buffer_ptr += len; |
193 |
} |
194 |
} |
195 |
|
196 |
// Request Sense and autosense data valid? |
197 |
status_t res = B_NO_ERROR; |
198 |
if (rdc.command[0] == 0x03 && (rdc.cam_status & CAM_AUTOSNS_VALID)) { |
199 |
|
200 |
// Yes, fake command |
201 |
D(bug(" autosense\n")); |
202 |
memcpy(buffer, sense_data, 256 - rdc.sense_data_length); |
203 |
rdc.scsi_status = 0; |
204 |
rdc.cam_status = CAM_REQ_CMP; |
205 |
|
206 |
} else { |
207 |
|
208 |
// No, send regular command |
209 |
D(bug(" sending command, length %d\n", data_length)); |
210 |
rdc.flags = (reading ? B_RAW_DEVICE_DATA_IN : 0) | B_RAW_DEVICE_REPORT_RESIDUAL | B_RAW_DEVICE_SHORT_READ_VALID; |
211 |
rdc.data_length = data_length; |
212 |
rdc.sense_data_length = 256; |
213 |
rdc.scsi_status = 0; |
214 |
rdc.cam_status = CAM_REQ_CMP; |
215 |
rdc.timeout = timeout * 16667; |
216 |
res = ioctl(fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc)); |
217 |
D(bug(" command sent, res %08x, status %d, cam_status %02x\n", res, rdc.scsi_status, rdc.cam_status)); |
218 |
*stat = rdc.scsi_status; |
219 |
} |
220 |
|
221 |
// Process S/G table when reading |
222 |
if (reading && rdc.cam_status == CAM_REQ_CMP) { |
223 |
D(bug(" reading from buffer\n")); |
224 |
uint8 *buffer_ptr = buffer; |
225 |
for (int i=0; i<sg_size; i++) { |
226 |
uint32 len = sg_len[i]; |
227 |
D(bug(" %d bytes to %08lx\n", len, sg_ptr[i])); |
228 |
memcpy(sg_ptr[i], buffer_ptr, len); |
229 |
buffer_ptr += len; |
230 |
} |
231 |
} |
232 |
return res ? false : true; |
233 |
} |