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