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