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