xref: /haiku/src/add-ons/kernel/generic/scsi_periph/io.cpp (revision a5bf12376daeded4049521eb17a6cc41192250d9)
1 /*
2  * Copyright 2004-2010, Haiku, Inc. All Rights Reserved.
3  * Copyright 2002/03, Thomas Kurschel. All rights reserved.
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 
8 
9 //!	Everything doing the real input/output stuff.
10 
11 
12 #include "scsi_periph_int.h"
13 #include <scsi.h>
14 
15 #include <string.h>
16 #include <stdlib.h>
17 
18 
19 static status_t
20 inquiry(scsi_periph_device_info *device, scsi_inquiry *inquiry)
21 {
22 	const scsi_res_inquiry *device_inquiry = NULL;
23 	size_t inquiryLength;
24 
25 	if (gDeviceManager->get_attr_raw(device->node, SCSI_DEVICE_INQUIRY_ITEM,
26 			(const void **)&device_inquiry, &inquiryLength, true) != B_OK)
27 		return B_ERROR;
28 
29 	memcpy(inquiry, device_inquiry, min_c(inquiryLength, sizeof(scsi_inquiry)));
30 	return B_OK;
31 }
32 
33 
34 static status_t
35 prevent_allow(scsi_periph_device_info *device, bool prevent)
36 {
37 	scsi_cmd_prevent_allow cmd;
38 
39 	SHOW_FLOW0(0, "");
40 
41 	memset(&cmd, 0, sizeof(cmd));
42 	cmd.opcode = SCSI_OP_PREVENT_ALLOW;
43 	cmd.prevent = prevent;
44 
45 	return periph_simple_exec(device, (uint8 *)&cmd, sizeof(cmd), NULL, 0,
46 		SCSI_DIR_NONE);
47 }
48 
49 
50 /*! Keep this in sync with scsi_raw driver!!! */
51 static status_t
52 raw_command(scsi_periph_device_info *device, raw_device_command *cmd)
53 {
54 	scsi_ccb *request;
55 
56 	SHOW_FLOW0(0, "");
57 
58 	request = device->scsi->alloc_ccb(device->scsi_device);
59 	if (request == NULL)
60 		return B_NO_MEMORY;
61 
62 	request->flags = 0;
63 
64 	if (cmd->flags & B_RAW_DEVICE_DATA_IN)
65 		request->flags |= SCSI_DIR_IN;
66 	else if (cmd->data_length)
67 		request->flags |= SCSI_DIR_OUT;
68 	else
69 		request->flags |= SCSI_DIR_NONE;
70 
71 	request->data = (uint8*)cmd->data;
72 	request->sg_list = NULL;
73 	request->data_length = cmd->data_length;
74 	request->sort = -1;
75 	request->timeout = cmd->timeout;
76 
77 	memcpy(request->cdb, cmd->command, SCSI_MAX_CDB_SIZE);
78 	request->cdb_length = cmd->command_length;
79 
80 	device->scsi->sync_io(request);
81 
82 	// TBD: should we call standard error handler here, or may the
83 	// actions done there (like starting the unit) confuse the application?
84 
85 	cmd->cam_status = request->subsys_status;
86 	cmd->scsi_status = request->device_status;
87 
88 	if ((request->subsys_status & SCSI_AUTOSNS_VALID) != 0 && cmd->sense_data) {
89 		memcpy(cmd->sense_data, request->sense, min_c(cmd->sense_data_length,
90 			(size_t)SCSI_MAX_SENSE_SIZE - request->sense_resid));
91 	}
92 
93 	if ((cmd->flags & B_RAW_DEVICE_REPORT_RESIDUAL) != 0) {
94 		// this is a bit strange, see Be's sample code where I pinched this from;
95 		// normally, residual means "number of unused bytes left"
96 		// but here, we have to return "number of used bytes", which is the opposite
97 		cmd->data_length = cmd->data_length - request->data_resid;
98 		cmd->sense_data_length = SCSI_MAX_SENSE_SIZE - request->sense_resid;
99 	}
100 
101 	device->scsi->free_ccb(request);
102 
103 	return B_OK;
104 }
105 
106 
107 /*! Universal read/write function */
108 static status_t
109 read_write(scsi_periph_device_info *device, scsi_ccb *request,
110 	io_operation *operation, uint64 offset, size_t originalNumBlocks,
111 	physical_entry* vecs, size_t vecCount, bool isWrite,
112 	size_t* _bytesTransferred)
113 {
114 	uint32 blockSize = device->block_size;
115 	size_t numBlocks = originalNumBlocks;
116 	uint32 pos = offset;
117 	err_res res;
118 	int retries = 0;
119 
120 	do {
121 		size_t numBytes;
122 		bool isReadWrite10;
123 
124 		request->flags = isWrite ? SCSI_DIR_OUT : SCSI_DIR_IN;
125 
126 		// make sure we avoid 10 byte commands if they aren't supported
127 		if (!device->rw10_enabled || device->preferred_ccb_size == 6) {
128 			// restricting transfer is OK - the block manager will
129 			// take care of transferring the rest
130 			if (numBlocks > 0x100)
131 				numBlocks = 0x100;
132 
133 			// no way to break the 21 bit address limit
134 			if (pos > 0x200000)
135 				return B_BAD_VALUE;
136 
137 			// don't allow transfer cross the 24 bit address limit
138 			// (I'm not sure whether this is allowed, but this way we
139 			// are sure to not ask for trouble)
140 			if (pos < 0x100000)
141 				numBlocks = min_c(numBlocks, 0x100000 - pos);
142 		}
143 
144 		numBytes = numBlocks * blockSize;
145 		if (numBlocks != originalNumBlocks)
146 			panic("I/O operation would need to be cut.");
147 
148 		request->data = NULL;
149 		request->sg_list = vecs;
150 		request->data_length = numBytes;
151 		request->sg_count = vecCount;
152 		request->io_operation = operation;
153 		request->sort = pos;
154 		request->timeout = device->std_timeout;
155 		// see whether daemon instructed us to post an ordered command;
156 		// reset flag after read
157 		SHOW_FLOW(3, "flag=%x, next_tag=%x, ordered: %s",
158 			(int)request->flags, (int)device->next_tag_action,
159 			(request->flags & SCSI_ORDERED_QTAG) != 0 ? "yes" : "no");
160 
161 		// use shortest commands whenever possible
162 		if (pos + numBlocks < 0x200000 && numBlocks <= 0x100) {
163 			scsi_cmd_rw_6 *cmd = (scsi_cmd_rw_6 *)request->cdb;
164 
165 			isReadWrite10 = false;
166 
167 			memset(cmd, 0, sizeof(*cmd));
168 			cmd->opcode = isWrite ? SCSI_OP_WRITE_6 : SCSI_OP_READ_6;
169 			cmd->high_lba = (pos >> 16) & 0x1f;
170 			cmd->mid_lba = (pos >> 8) & 0xff;
171 			cmd->low_lba = pos & 0xff;
172 			cmd->length = numBlocks;
173 
174 			request->cdb_length = sizeof(*cmd);
175 		} else {
176 			scsi_cmd_rw_10 *cmd = (scsi_cmd_rw_10 *)request->cdb;
177 
178 			isReadWrite10 = true;
179 
180 			memset(cmd, 0, sizeof(*cmd));
181 			cmd->opcode = isWrite ? SCSI_OP_WRITE_10 : SCSI_OP_READ_10;
182 			cmd->relative_address = 0;
183 			cmd->force_unit_access = 0;
184 			cmd->disable_page_out = 0;
185 			cmd->lba = B_HOST_TO_BENDIAN_INT32(pos);
186 			cmd->length = B_HOST_TO_BENDIAN_INT16(numBlocks);
187 
188 			request->cdb_length = sizeof(*cmd);
189 		}
190 
191 		// TODO: last chance to detect errors that occured during concurrent accesses
192 		//status_t status = handle->pending_error;
193 		//if (status != B_OK)
194 		//	return status;
195 
196 		device->scsi->async_io(request);
197 
198 		acquire_sem(request->completion_sem);
199 
200 		// ask generic peripheral layer what to do now
201 		res = periph_check_error(device, request);
202 
203 		// TODO: bytes might have been transferred even in the error case!
204 		switch (res.action) {
205 			case err_act_ok:
206 				*_bytesTransferred = numBytes - request->data_resid;
207 				break;
208 
209 			case err_act_start:
210 				res = periph_send_start_stop(device, request, 1,
211 					device->removable);
212 				if (res.action == err_act_ok)
213 					res.action = err_act_retry;
214 				break;
215 
216 			case err_act_invalid_req:
217 				// if this was a 10 byte command, the device probably doesn't
218 				// support them, so disable them and retry
219 				if (isReadWrite10) {
220 					atomic_and(&device->rw10_enabled, 0);
221 					res.action = err_act_retry;
222 				} else
223 					res.action = err_act_fail;
224 				break;
225 		}
226 	} while ((res.action == err_act_retry && retries++ < 3)
227 		|| (res.action == err_act_many_retries && retries++ < 30));
228 
229 	// peripheral layer only created "read" error, so we have to
230 	// map them to "write" errors if this was a write request
231 	if (res.error_code == B_DEV_READ_ERROR && isWrite)
232 		return B_DEV_WRITE_ERROR;
233 
234 	return res.error_code;
235 }
236 
237 
238 // #pragma mark - public functions
239 
240 
241 status_t
242 periph_ioctl(scsi_periph_handle_info *handle, int op, void *buffer,
243 	size_t length)
244 {
245 	switch (op) {
246 		case B_GET_MEDIA_STATUS: {
247 			status_t res = B_OK;
248 
249 			if (handle->device->removable)
250 				res = periph_get_media_status(handle);
251 
252 			SHOW_FLOW(2, "%s", strerror(res));
253 
254 			*(status_t *)buffer = res;
255 			return B_OK;
256 		}
257 
258 		case B_SCSI_INQUIRY:
259 			return inquiry(handle->device, (scsi_inquiry *)buffer);
260 
261 		case B_SCSI_PREVENT_ALLOW:
262 			return prevent_allow(handle->device, *(bool *)buffer);
263 
264 		case B_RAW_DEVICE_COMMAND:
265 			return raw_command(handle->device, (raw_device_command*)buffer);
266 
267 		default:
268 			if (handle->device->scsi->ioctl != NULL) {
269 				return handle->device->scsi->ioctl(handle->device->scsi_device,
270 					op, buffer, length);
271 			}
272 
273 			SHOW_ERROR(4, "Unknown ioctl: %x", op);
274 			return B_BAD_VALUE;
275 	}
276 }
277 
278 
279 /*!	Kernel daemon - once in a minute, it sets a flag so that the next command
280 	is executed ordered; this way, we avoid starvation of SCSI commands inside
281 	the SCSI queuing system - the ordered command waits for all previous
282 	commands and thus no command can starve longer then a minute
283 */
284 void
285 periph_sync_queue_daemon(void *arg, int iteration)
286 {
287 	scsi_periph_device_info *device = (scsi_periph_device_info *)arg;
288 
289 	SHOW_FLOW0(3, "Setting ordered flag for next R/W access");
290 	atomic_or(&device->next_tag_action, SCSI_ORDERED_QTAG);
291 }
292 
293 
294 status_t
295 periph_read_write(scsi_periph_device_info *device, scsi_ccb *request,
296 	uint64 offset, size_t numBlocks, physical_entry* vecs, size_t vecCount,
297 	bool isWrite, size_t* _bytesTransferred)
298 {
299 	return read_write(device, request, NULL, offset, numBlocks, vecs, vecCount,
300 		isWrite, _bytesTransferred);
301 }
302 
303 
304 status_t
305 periph_io(scsi_periph_device_info *device, io_operation *operation,
306 	size_t* _bytesTransferred)
307 {
308 	const uint32 blockSize = device->block_size;
309 
310 	// don't test rw10_enabled restrictions - this flag may get changed
311 	scsi_ccb *request = device->scsi->alloc_ccb(device->scsi_device);
312 	if (request == NULL)
313 		return B_NO_MEMORY;
314 
315 	status_t status = read_write(device, request, operation,
316 		operation->Offset() / blockSize, operation->Length() / blockSize,
317 		(physical_entry *)operation->Vecs(), operation->VecCount(),
318 		operation->IsWrite(), _bytesTransferred);
319 
320 	device->scsi->free_ccb(request);
321 	return status;
322 }
323 
324