1 /*- 2 * Copyright (c) 2001 Michael Smith 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * CISS adapter driver datastructures 31 */ 32 33 /************************************************************************ 34 * Tunable parameters 35 */ 36 37 /* 38 * There is no guaranteed upper bound on the number of concurrent 39 * commands an adapter may claim to support. Cap it at a reasonable 40 * value. 41 */ 42 #define CISS_MAX_REQUESTS 256 43 44 /* 45 * Maximum number of logical drives we support. 46 */ 47 #define CISS_MAX_LOGICAL 15 48 49 /* 50 * Maximum number of physical devices we support. 51 */ 52 #define CISS_MAX_PHYSICAL 1024 53 54 /* 55 * Interrupt reduction can be controlled by tuning the interrupt 56 * coalesce delay and count paramters. The delay (in microseconds) 57 * defers delivery of interrupts to increase the chance of there being 58 * more than one completed command ready when the interrupt is 59 * delivered. The count expedites the delivery of the interrupt when 60 * the given number of commands are ready. 61 * 62 * If the delay is set to 0, interrupts are delivered immediately. 63 */ 64 #define CISS_INTERRUPT_COALESCE_DELAY 1000 65 #define CISS_INTERRUPT_COALESCE_COUNT 16 66 67 /* 68 * Heartbeat routine timeout in seconds. Note that since event 69 * handling is performed on a callback basis, we don't need this to 70 * run very often. 71 */ 72 #define CISS_HEARTBEAT_RATE 10 73 74 /************************************************************************ 75 * Compatibility with older versions of FreeBSD 76 */ 77 #if __FreeBSD_version < 440001 78 #warning testing old-FreeBSD compat 79 typedef struct proc d_thread_t; 80 #endif 81 82 /************************************************************************ 83 * Command queue statistics 84 */ 85 86 #define CISSQ_FREE 0 87 #define CISSQ_BUSY 1 88 #define CISSQ_COMPLETE 2 89 #define CISSQ_NOTIFY 3 90 #define CISSQ_COUNT 4 91 92 struct ciss_qstat 93 { 94 u_int32_t q_length; 95 u_int32_t q_max; 96 }; 97 98 /************************************************************************ 99 * Driver version. Only really significant to the ACU interface. 100 */ 101 #define CISS_DRIVER_VERSION 20011201 102 103 /************************************************************************ 104 * Driver data structures 105 */ 106 107 /* 108 * Each command issued to the adapter is managed by a request 109 * structure. 110 */ 111 struct ciss_request 112 { 113 TAILQ_ENTRY(ciss_request) cr_link; 114 int cr_onq; /* which queue we are on */ 115 116 struct ciss_softc *cr_sc; /* controller softc */ 117 void *cr_data; /* data buffer */ 118 u_int32_t cr_length; /* data length */ 119 bus_dmamap_t cr_datamap; /* DMA map for data */ 120 int cr_tag; 121 int cr_flags; 122 #define CISS_REQ_MAPPED (1<<0) /* data mapped */ 123 #define CISS_REQ_SLEEP (1<<1) /* submitter sleeping */ 124 #define CISS_REQ_POLL (1<<2) /* submitter polling */ 125 #define CISS_REQ_DATAOUT (1<<3) /* data host->adapter */ 126 #define CISS_REQ_DATAIN (1<<4) /* data adapter->host */ 127 128 void (* cr_complete)(struct ciss_request *); 129 void *cr_private; 130 }; 131 132 /* 133 * The adapter command structure is defined with a zero-length 134 * scatter/gather list size. In practise, we want space for a 135 * scatter-gather list, and we also want to avoid having commands 136 * cross page boundaries. 137 * 138 * Note that 512 bytes yields 28 scatter/gather entries, or the 139 * ability to map (26 * PAGE_SIZE) + 2 bytes of data. On x86, this is 140 * 104kB. 256 bytes would only yield 12 entries, giving a mere 40kB, 141 * too small. 142 */ 143 144 #define CISS_COMMAND_ALLOC_SIZE 512 /* XXX tune to get sensible s/g list length */ 145 #define CISS_COMMAND_SG_LENGTH ((CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command)) \ 146 / sizeof(struct ciss_sg_entry)) 147 148 /* 149 * Per-logical-drive data. 150 */ 151 struct ciss_ldrive 152 { 153 union ciss_device_address cl_address; 154 union ciss_device_address *cl_controller; 155 int cl_status; 156 #define CISS_LD_NONEXISTENT 0 157 #define CISS_LD_ONLINE 1 158 #define CISS_LD_OFFLINE 2 159 160 int cl_update; 161 162 struct ciss_bmic_id_ldrive *cl_ldrive; 163 struct ciss_bmic_id_lstatus *cl_lstatus; 164 struct ciss_ldrive_geometry cl_geometry; 165 166 char cl_name[16]; /* device name */ 167 }; 168 169 /* 170 * Per-physical-drive data 171 */ 172 struct ciss_pdrive 173 { 174 union ciss_device_address cp_address; 175 int cp_online; 176 }; 177 178 #define CISS_PHYSICAL_SHIFT 5 179 #define CISS_PHYSICAL_BASE (1 << CISS_PHYSICAL_SHIFT) 180 #define CISS_MAX_PHYSTGT 15 181 182 #define CISS_IS_PHYSICAL(bus) (bus >= CISS_PHYSICAL_BASE) 183 #define CISS_CAM_TO_PBUS(bus) (bus - CISS_PHYSICAL_BASE) 184 185 /* 186 * Per-adapter data 187 */ 188 struct ciss_softc 189 { 190 /* bus connections */ 191 device_t ciss_dev; /* bus attachment */ 192 struct cdev *ciss_dev_t; /* control device */ 193 194 struct resource *ciss_regs_resource; /* register interface window */ 195 int ciss_regs_rid; /* resource ID */ 196 bus_space_handle_t ciss_regs_bhandle; /* bus space handle */ 197 bus_space_tag_t ciss_regs_btag; /* bus space tag */ 198 199 struct resource *ciss_cfg_resource; /* config struct interface window */ 200 int ciss_cfg_rid; /* resource ID */ 201 struct ciss_config_table *ciss_cfg; /* config table in adapter memory */ 202 struct ciss_bmic_id_table *ciss_id; /* ID table in host memory */ 203 u_int32_t ciss_heartbeat; /* last heartbeat value */ 204 int ciss_heart_attack; /* number of times we have seen this value */ 205 206 struct resource *ciss_irq_resource; /* interrupt */ 207 int ciss_irq_rid; /* resource ID */ 208 void *ciss_intr; /* interrupt handle */ 209 210 bus_dma_tag_t ciss_parent_dmat; /* parent DMA tag */ 211 bus_dma_tag_t ciss_buffer_dmat; /* data buffer/command DMA tag */ 212 213 u_int32_t ciss_interrupt_mask; /* controller interrupt mask bits */ 214 215 int ciss_max_requests; 216 struct ciss_request ciss_request[CISS_MAX_REQUESTS]; /* requests */ 217 void *ciss_command; /* command structures */ 218 bus_dma_tag_t ciss_command_dmat; /* command DMA tag */ 219 bus_dmamap_t ciss_command_map; /* command DMA map */ 220 u_int32_t ciss_command_phys; /* command array base address */ 221 TAILQ_HEAD(,ciss_request) ciss_free; /* requests available for reuse */ 222 TAILQ_HEAD(,ciss_request) ciss_busy; /* requests in the adapter */ 223 TAILQ_HEAD(,ciss_request) ciss_complete; /* requests which have been returned by the adapter */ 224 TAILQ_HEAD(,ciss_request) ciss_notify; /* requests which are defered for processing */ 225 struct proc *ciss_notify_thread; 226 227 struct callout_handle ciss_periodic; /* periodic event handling */ 228 struct ciss_request *ciss_periodic_notify; /* notify callback request */ 229 230 struct ciss_ldrive **ciss_logical; 231 struct ciss_pdrive **ciss_physical; 232 union ciss_device_address *ciss_controllers; /* controller address */ 233 int ciss_max_bus_number; /* maximum bus number */ 234 int ciss_max_logical_bus; 235 int ciss_max_physical_bus; 236 237 struct cam_devq *ciss_cam_devq; 238 struct cam_sim **ciss_cam_sim; 239 240 int ciss_flags; 241 #define CISS_FLAG_NOTIFY_OK (1<<0) /* notify command running OK */ 242 #define CISS_FLAG_CONTROL_OPEN (1<<1) /* control device is open */ 243 #define CISS_FLAG_ABORTING (1<<2) /* driver is going away */ 244 #define CISS_FLAG_RUNNING (1<<3) /* driver is running (interrupts usable) */ 245 246 #define CISS_FLAG_FAKE_SYNCH (1<<16) /* needs SYNCHRONISE_CACHE faked */ 247 #define CISS_FLAG_BMIC_ABORT (1<<17) /* use BMIC command to abort Notify on Event */ 248 #define CISS_FLAG_THREAD_SHUT (1<<20) /* shutdown the kthread */ 249 250 struct ciss_qstat ciss_qstat[CISSQ_COUNT]; /* queue statistics */ 251 }; 252 253 /* 254 * Given a request tag, find the corresponding command in virtual or 255 * physical space. 256 * 257 * The arithmetic here is due to the allocation of ciss_command structures 258 * inside CISS_COMMAND_ALLOC_SIZE blocks. See the comment at the definition 259 * of CISS_COMMAND_ALLOC_SIZE above. 260 */ 261 #define CISS_FIND_COMMAND(cr) \ 262 (struct ciss_command *)((u_int8_t *)(cr)->cr_sc->ciss_command + \ 263 ((cr)->cr_tag * CISS_COMMAND_ALLOC_SIZE)) 264 #define CISS_FIND_COMMANDPHYS(cr) ((cr)->cr_sc->ciss_command_phys + \ 265 ((cr)->cr_tag * CISS_COMMAND_ALLOC_SIZE)) 266 267 /************************************************************************ 268 * Debugging/diagnostic output. 269 */ 270 271 /* 272 * Debugging levels: 273 * 0 - quiet, only emit warnings 274 * 1 - talkative, log major events, but nothing on the I/O path 275 * 2 - noisy, log events on the I/O path 276 * 3 - extremely noisy, log items in loops 277 */ 278 #ifdef CISS_DEBUG 279 # define debug(level, fmt, args...) \ 280 do { \ 281 if (level <= CISS_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \ 282 } while(0) 283 # define debug_called(level) \ 284 do { \ 285 if (level <= CISS_DEBUG) printf("%s: called\n", __func__); \ 286 } while(0) 287 # define debug_struct(s) printf(" SIZE %s: %zu\n", #s, sizeof(struct s)) 288 # define debug_union(s) printf(" SIZE %s: %zu\n", #s, sizeof(union s)) 289 # define debug_type(s) printf(" SIZE %s: %zu\n", #s, sizeof(s)) 290 # define debug_field(s, f) printf(" OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f))) 291 # define debug_const(c) printf(" CONST %s %jd/0x%jx\n", #c, (intmax_t)c, (intmax_t)c); 292 #else 293 # define debug(level, fmt, args...) 294 # define debug_called(level) 295 # define debug_struct(s) 296 # define debug_union(s) 297 # define debug_type(s) 298 # define debug_field(s, f) 299 # define debug_const(c) 300 #endif 301 302 #define ciss_printf(sc, fmt, args...) device_printf(sc->ciss_dev, fmt , ##args) 303 304 /************************************************************************ 305 * Queue primitives 306 */ 307 308 #define CISSQ_ADD(sc, qname) \ 309 do { \ 310 struct ciss_qstat *qs = &(sc)->ciss_qstat[qname]; \ 311 \ 312 qs->q_length++; \ 313 if (qs->q_length > qs->q_max) \ 314 qs->q_max = qs->q_length; \ 315 } while(0) 316 317 #define CISSQ_REMOVE(sc, qname) (sc)->ciss_qstat[qname].q_length-- 318 #define CISSQ_INIT(sc, qname) \ 319 do { \ 320 sc->ciss_qstat[qname].q_length = 0; \ 321 sc->ciss_qstat[qname].q_max = 0; \ 322 } while(0) 323 324 325 #define CISSQ_REQUEST_QUEUE(name, index) \ 326 static __inline void \ 327 ciss_initq_ ## name (struct ciss_softc *sc) \ 328 { \ 329 TAILQ_INIT(&sc->ciss_ ## name); \ 330 CISSQ_INIT(sc, index); \ 331 } \ 332 static __inline void \ 333 ciss_enqueue_ ## name (struct ciss_request *cr) \ 334 { \ 335 int s; \ 336 \ 337 s = splcam(); \ 338 TAILQ_INSERT_TAIL(&cr->cr_sc->ciss_ ## name, cr, cr_link); \ 339 CISSQ_ADD(cr->cr_sc, index); \ 340 cr->cr_onq = index; \ 341 splx(s); \ 342 } \ 343 static __inline void \ 344 ciss_requeue_ ## name (struct ciss_request *cr) \ 345 { \ 346 int s; \ 347 \ 348 s = splcam(); \ 349 TAILQ_INSERT_HEAD(&cr->cr_sc->ciss_ ## name, cr, cr_link); \ 350 CISSQ_ADD(cr->cr_sc, index); \ 351 cr->cr_onq = index; \ 352 splx(s); \ 353 } \ 354 static __inline struct ciss_request * \ 355 ciss_dequeue_ ## name (struct ciss_softc *sc) \ 356 { \ 357 struct ciss_request *cr; \ 358 int s; \ 359 \ 360 s = splcam(); \ 361 if ((cr = TAILQ_FIRST(&sc->ciss_ ## name)) != NULL) { \ 362 TAILQ_REMOVE(&sc->ciss_ ## name, cr, cr_link); \ 363 CISSQ_REMOVE(sc, index); \ 364 cr->cr_onq = -1; \ 365 } \ 366 splx(s); \ 367 return(cr); \ 368 } \ 369 static __inline int \ 370 ciss_remove_ ## name (struct ciss_request *cr) \ 371 { \ 372 int s, error; \ 373 \ 374 s = splcam(); \ 375 if (cr->cr_onq != index) { \ 376 printf("request on queue %d (expected %d)\n", cr->cr_onq, index);\ 377 error = 1; \ 378 } else { \ 379 TAILQ_REMOVE(&cr->cr_sc->ciss_ ## name, cr, cr_link); \ 380 CISSQ_REMOVE(cr->cr_sc, index); \ 381 cr->cr_onq = -1; \ 382 error = 0; \ 383 } \ 384 splx(s); \ 385 return(error); \ 386 } \ 387 struct hack 388 389 CISSQ_REQUEST_QUEUE(free, CISSQ_FREE); 390 CISSQ_REQUEST_QUEUE(busy, CISSQ_BUSY); 391 CISSQ_REQUEST_QUEUE(complete, CISSQ_COMPLETE); 392 CISSQ_REQUEST_QUEUE(notify, CISSQ_NOTIFY); 393 394 /******************************************************************************** 395 * space-fill a character string 396 */ 397 static __inline void 398 padstr(char *targ, const char *src, int len) 399 { 400 while (len-- > 0) { 401 if (*src != 0) { 402 *targ++ = *src++; 403 } else { 404 *targ++ = ' '; 405 } 406 } 407 } 408