xref: /haiku/src/add-ons/kernel/bus_managers/scsi/scsi_internal.h (revision 9e25244c5e9051f6cd333820d6332397361abd6c)
1 /*
2  * Copyright 2002/03, Thomas Kurschel. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _SCSI_INTERNAL_H
6 #define _SCSI_INTERNAL_H
7 
8 //!	Internal structures/definitions
9 
10 #include <sys/cdefs.h>
11 
12 #include <bus/SCSI.h>
13 #include <scsi_cmds.h>
14 #include <locked_pool.h>
15 #include <device_manager.h>
16 #include <lock.h>
17 
18 #define debug_level_error 4
19 #define debug_level_info 4
20 #define debug_level_flow 4
21 
22 #define DEBUG_MSG_PREFIX "SCSI -- "
23 
24 #include "wrapper.h"
25 #include "scsi_lock.h"
26 
27 
28 #define MAX_PATH_ID 255
29 #define MAX_TARGET_ID 15
30 #define MAX_LUN_ID 7
31 
32 
33 // maximum number of fragments for temporary S/G lists
34 // for real SCSI controllers, there's no limit to transmission length
35 // but we need a limit - ATA transmits up to 128K, so we allow that
36 // (for massive data transmission, peripheral drivers should provide own
37 // SG list anyway)
38 // add one extra entry in case data is not page aligned
39 #define MAX_TEMP_SG_FRAGMENTS (128*1024 / B_PAGE_SIZE + 1)
40 
41 // maximum number of temporary S/G lists
42 #define MAX_TEMP_SG_LISTS 32
43 
44 // delay in µs before DMA buffer is cleaned up
45 #define SCSI_DMA_BUFFER_CLEANUP_DELAY 10*1000000
46 
47 // buffer size for emulated SCSI commands that ATAPI cannot handle;
48 // for MODE SELECT 6, maximum size is 255 + header,
49 // for MODE SENSE 6, we use MODE SENSE 10 which can return 64 K,
50 // but as the caller has to live with the 255 + header restriction,
51 // we hope that this buffer is large enough
52 #define SCSI_ATAPI_BUFFER_SIZE 512
53 
54 
55 // name of pnp generator of path ids
56 #define SCSI_PATHID_GENERATOR "scsi/path_id"
57 // true, if SCSI device needs ATAPI emulation (ui8)
58 #define SCSI_DEVICE_IS_ATAPI_ITEM "scsi/is_atapi"
59 // true, if device requires auto-sense emulation (ui8)
60 #define SCSI_DEVICE_MANUAL_AUTOSENSE_ITEM "scsi/manual_autosense"
61 
62 // name of internal scsi_bus_raw device driver
63 #define SCSI_BUS_RAW_MODULE_NAME "bus_managers/scsi/bus/raw/device_v1"
64 
65 // info about DPC
66 typedef struct scsi_dpc_info {
67 	struct scsi_dpc_info *next;
68 	bool registered;			// true, if already/still in dpc list
69 
70 	void (*func)( void * );
71 	void *arg;
72 } scsi_dpc_info;
73 
74 
75 // controller restrictions (see blkman.h)
76 typedef struct dma_params {
77 	uint32 alignment;
78 	uint32 max_blocks;
79 	uint32 dma_boundary;
80 	uint32 max_sg_block_size;
81 	uint32 max_sg_blocks;
82 } dma_params;
83 
84 
85 // SCSI bus
86 typedef struct scsi_bus_info {
87 	int lock_count;				// sum of blocked[0..1] and sim_overflow
88 	int blocked[2];				// depth of nested locks by bus manager (0) and SIM (1)
89 	int left_slots;				// left command queuing slots on HBA
90 	bool sim_overflow;			// 1, if SIM refused req because of bus queue overflow
91 
92 	uchar path_id;				// SCSI path id
93 	uint32 max_target_count;	// maximum count of target_ids on the bus
94 	uint32 max_lun_count;		// maximum count of lun_ids on the bus
95 
96 	thread_id service_thread;	// service thread
97 	sem_id start_service;		// released whenever service thread has work to do
98 	bool shutting_down;			// set to true to tell service thread to shut down
99 
100 	struct mutex mutex;			// used to synchronize changes in queueing and blocking
101 
102 	sem_id scan_lun_lock;		// allocated whenever a lun is scanned
103 
104 	scsi_sim_interface *interface;	// SIM interface
105 	scsi_sim_cookie sim_cookie;	// internal SIM cookie
106 
107 	spinlock_irq dpc_lock;		// synchronizer for dpc list
108 	scsi_dpc_info *dpc_list;	// list of dpcs to execute
109 
110 	struct scsi_device_info *waiting_devices;	// devices ready to receive requests
111 
112 	locked_pool_cookie ccb_pool;	// ccb pool (one per bus)
113 
114 	device_node *node;		// pnp node of bus
115 
116 	struct dma_params dma_params;	// dma restrictions of controller
117 
118 	scsi_path_inquiry inquiry_data;	// inquiry data as read on init
119 } scsi_bus_info;
120 
121 
122 // DMA buffer
123 typedef struct dma_buffer {
124 	area_id area;			// area of DMA buffer
125 	uchar *address;			// address of DMA buffer
126 	size_t size;			// size of DMA buffer
127 	area_id sg_list_area;	// area of S/G list
128 	physical_entry *sg_list;	// address of S/G list
129 	uint32 sg_count;			// number of entries in S/G list
130 	bool inuse;				// true, if in use
131 	bigtime_t last_use;		// timestamp of last usage
132 
133 	area_id sg_orig;					// area of S/G list to original data
134 	physical_entry *sg_list_orig;		// S/G list to original data
135 	uint32 sg_count_max_orig;			// maximum size (in entries)
136 	uint32 sg_count_orig;				// current size (in entries)
137 
138 	uchar *orig_data;					// pointer to original data
139 	const physical_entry *orig_sg_list;	// original S/G list
140 	uint32 orig_sg_count;				// size of original S/G list
141 } dma_buffer;
142 
143 
144 // SCSI device
145 typedef struct scsi_device_info {
146 	struct scsi_device_info *waiting_next;
147 	struct scsi_device_info *waiting_prev;
148 
149 	bool manual_autosense : 1;	// no autosense support
150 	bool is_atapi : 1;			// ATAPI device - needs some commands emulated
151 
152 	int lock_count;				// sum of blocked[0..1] and sim_overflow
153 	int blocked[2];				// depth of nested locks by bus manager (0) and SIM (1)
154 	int sim_overflow;			// 1, if SIM returned a request because of device queue overflow
155 	int left_slots;				// left command queuing slots for device
156 	int total_slots;			// total number of command queuing slots for device
157 
158 	scsi_ccb *queued_reqs;		// queued requests, circularly doubly linked
159 								// (scsi_insert_new_request depends on circular)
160 
161 	int64 last_sort;			// last sort value (for elevator sort)
162 	int32 valid;				// access must be atomic!
163 
164 	scsi_bus_info *bus;
165 	uchar target_id;
166 	uchar target_lun;
167 
168 	scsi_ccb *auto_sense_request;		// auto-sense request
169 	scsi_ccb *auto_sense_originator;	// request that auto-sense is
170 										// currently requested for
171 	area_id auto_sense_area;			// area of auto-sense data and S/G list
172 
173 	uint8 emulation_map[256/8];		// bit field with index being command code:
174 								// 1 indicates that this command is not supported
175 								// and thus must be emulated
176 
177 	scsi_res_inquiry inquiry_data;
178 	device_node *node;	// device node
179 
180 	struct mutex dma_buffer_lock;	// lock between DMA buffer user and clean-up daemon
181 	sem_id dma_buffer_owner;	// to be acquired before using DMA buffer
182 	struct dma_buffer dma_buffer;	// DMA buffer
183 
184 	// buffer used for emulating SCSI commands
185 	char *buffer;
186 	physical_entry *buffer_sg_list;
187 	size_t buffer_sg_count;
188 	size_t buffer_size;
189 	area_id buffer_area;
190 	sem_id buffer_sem;
191 } scsi_device_info;
192 
193 enum {
194 	ev_scsi_requeue_request = 1,
195 	ev_scsi_resubmit_request,
196 	ev_scsi_submit_autosense,
197 	ev_scsi_finish_autosense,
198 	ev_scsi_device_queue_overflow,
199 	ev_scsi_request_finished,
200 	ev_scsi_async_io,
201 	ev_scsi_do_resend_request,
202 	ev_copy_sg_data
203 };
204 
205 // check whether device is in bus's wait queue
206 // we use the fact the queue is circular, so we don't need an explicit flag
207 #define DEVICE_IN_WAIT_QUEUE( device ) ((device)->waiting_next != NULL)
208 
209 
210 // state of ccb
211 enum {
212 	SCSI_STATE_FREE = 0,
213 	SCSI_STATE_INWORK = 1,
214 	SCSI_STATE_QUEUED = 2,
215 	SCSI_STATE_SENT = 3,
216 	SCSI_STATE_FINISHED = 5,
217 };
218 
219 
220 extern locked_pool_interface *locked_pool;
221 extern device_manager_info *pnp;
222 
223 extern scsi_for_sim_interface scsi_for_sim_module;
224 extern scsi_bus_interface scsi_bus_module;
225 extern scsi_device_interface scsi_device_module;
226 extern struct device_module_info gSCSIBusRawModule;
227 
228 
229 __BEGIN_DECLS
230 
231 
232 // busses.c
233 uchar scsi_inquiry_path(scsi_bus bus, scsi_path_inquiry *inquiry_data);
234 
235 
236 // ccb.c
237 scsi_ccb *scsi_alloc_ccb(scsi_device_info *device);
238 void scsi_free_ccb(scsi_ccb *ccb);
239 
240 status_t scsi_init_ccb_alloc(scsi_bus_info *bus);
241 void scsi_uninit_ccb_alloc(scsi_bus_info *bus);
242 
243 
244 // devices.c
245 status_t scsi_force_get_device(scsi_bus_info *bus,
246 	uchar target_id, uchar target_lun, scsi_device_info **res_device);
247 void scsi_put_forced_device(scsi_device_info *device);
248 status_t scsi_register_device(scsi_bus_info *bus, uchar target_id,
249 	uchar target_lun, scsi_res_inquiry *inquiry_data);
250 
251 
252 // device_scan.c
253 status_t scsi_scan_bus(scsi_bus_info *bus);
254 status_t scsi_scan_lun(scsi_bus_info *bus, uchar target_id, uchar target_lun);
255 
256 
257 // dpc.c
258 status_t scsi_alloc_dpc(scsi_dpc_info **dpc);
259 status_t scsi_free_dpc(scsi_dpc_info *dpc);
260 bool scsi_check_exec_dpc(scsi_bus_info *bus);
261 
262 status_t scsi_schedule_dpc(scsi_bus_info *bus, scsi_dpc_info *dpc, /*int flags,*/
263 	void (*func)( void *arg ), void *arg);
264 
265 
266 // scsi_io.c
267 void scsi_async_io(scsi_ccb *request);
268 void scsi_sync_io(scsi_ccb *request);
269 uchar scsi_term_io(scsi_ccb *ccb_to_terminate);
270 uchar scsi_abort(scsi_ccb *ccb_to_abort);
271 
272 bool scsi_check_exec_service(scsi_bus_info *bus);
273 
274 void scsi_done_io(scsi_ccb *ccb);
275 
276 void scsi_requeue_request(scsi_ccb *request, bool bus_overflow);
277 void scsi_resubmit_request(scsi_ccb *request);
278 void scsi_request_finished(scsi_ccb *request, uint num_requests);
279 
280 
281 // scatter_gather.c
282 bool create_temp_sg(scsi_ccb *ccb);
283 void cleanup_tmp_sg(scsi_ccb *ccb);
284 
285 int init_temp_sg(void);
286 void uninit_temp_sg(void);
287 
288 
289 // dma_buffer.c
290 void scsi_dma_buffer_daemon(void *dev, int counter);
291 void scsi_release_dma_buffer(scsi_ccb *request);
292 bool scsi_get_dma_buffer(scsi_ccb *request);
293 void scsi_dma_buffer_free(dma_buffer *buffer);
294 void scsi_dma_buffer_init(dma_buffer *buffer);
295 
296 
297 // queuing.c
298 
299 
300 // emulation.c
301 bool scsi_start_emulation(scsi_ccb *request);
302 void scsi_finish_emulation(scsi_ccb *request);
303 void scsi_free_emulation_buffer(scsi_device_info *device);
304 status_t scsi_init_emulation_buffer(scsi_device_info *device, size_t buffer_size);
305 
306 
307 __END_DECLS
308 
309 
310 #endif	/* _SCSI_INTERNAL_H */
311