2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sys/dev/aac/aacvar.h,v 1.4.2.7 2003/04/08 13:22:08 scottl Exp $
30 * $DragonFly: src/sys/dev/raid/aac/aacvar.h,v 1.13 2006/06/10 20:00:16 dillon Exp $
33 #include <sys/thread2.h>
36 * Driver Parameter Definitions
40 * The firmware interface allows for a 16-bit s/g list length. We limit
41 * ourselves to a reasonable maximum and ensure alignment.
43 #define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */
46 * We allocate a small set of FIBs for the adapter to use to send us messages.
48 #define AAC_ADAPTER_FIBS 8
51 * FIBs are allocated up-front, and the pool isn't grown. We should allocate
52 * enough here to let us keep the adapter busy without wasting large amounts
53 * of kernel memory. The current interface implementation limits us to 512
54 * FIBs queued for the adapter at any one time.
56 #define AAC_FIB_COUNT 128
59 * The controller reports status events in AIFs. We hang on to a number of
60 * these in order to pass them out to user-space management tools.
62 #define AAC_AIFQ_LENGTH 64
65 * Firmware messages are passed in the printf buffer.
67 #define AAC_PRINTF_BUFSIZE 256
70 * We wait this many seconds for the adapter to come ready if it is still
73 #define AAC_BOOT_TIMEOUT (3 * 60)
76 * Timeout for immediate commands.
78 #define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */
81 * Timeout for normal commands
83 #define AAC_CMD_TIMEOUT 30 /* seconds */
86 * Rate at which we periodically check for timed out commands and kick the
89 #define AAC_PERIODIC_INTERVAL 20 /* seconds */
92 * Character device major numbers.
94 #define AAC_DISK_MAJOR 200
97 * Driver Variable Definitions
100 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
101 # include <sys/taskqueue.h>
105 * Per-container data structure
109 struct aac_mntobj co_mntobj;
112 TAILQ_ENTRY(aac_container) co_link;
122 struct aac_softc *ad_controller;
123 struct aac_container *ad_container;
125 struct devstat ad_stats;
126 struct disklabel ad_label;
128 #define AAC_DISK_OPEN (1<<0)
137 * Per-command control structure.
141 TAILQ_ENTRY(aac_command) cm_link; /* list linkage */
143 struct aac_softc *cm_sc; /* controller that owns us */
145 struct aac_fib *cm_fib; /* FIB associated with this
147 u_int32_t cm_fibphys; /* bus address of the FIB */
148 struct buf *cm_data; /* pointer to data in kernel
150 u_int32_t cm_datalen; /* data length */
151 bus_dmamap_t cm_datamap; /* DMA map for bio data */
152 struct aac_sg_table *cm_sgtable; /* pointer to s/g table in
155 #define AAC_CMD_MAPPED (1<<0) /* command has had its data
157 #define AAC_CMD_DATAIN (1<<1) /* command involves data moving
158 * from controller to host */
159 #define AAC_CMD_DATAOUT (1<<2) /* command involves data moving
160 * from host to controller */
161 #define AAC_CMD_COMPLETED (1<<3) /* command has been completed */
162 #define AAC_CMD_TIMEDOUT (1<<4) /* command taken too long */
163 #define AAC_ON_AACQ_FREE (1<<5)
164 #define AAC_ON_AACQ_READY (1<<6)
165 #define AAC_ON_AACQ_BUSY (1<<7)
166 #define AAC_ON_AACQ_COMPLETE (1<<8)
167 #define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8))
169 void (* cm_complete)(struct aac_command *cm);
171 time_t cm_timestamp; /* command creation time */
176 * We gather a number of adapter-visible items into a single structure.
178 * The ordering of this strucure may be important; we copy the Linux driver:
182 * Queue headers (Comm Area)
185 * In addition, we add:
189 /* fibs for the controller to send us messages */
190 struct aac_fib ac_fibs[AAC_ADAPTER_FIBS];
192 /* the init structure */
193 struct aac_adapter_init ac_init;
195 /* arena within which the queue structures are kept */
196 u_int8_t ac_qbuf[sizeof(struct aac_queue_table) +
199 /* buffer for text messages from the controller */
200 char ac_printf[AAC_PRINTF_BUFSIZE];
202 /* fib for synchronous commands */
203 struct aac_fib ac_sync_fib;
207 * Interface operations
211 int (*aif_get_fwstatus)(struct aac_softc *sc);
212 void (*aif_qnotify)(struct aac_softc *sc, int qbit);
213 int (*aif_get_istatus)(struct aac_softc *sc);
214 void (*aif_clr_istatus)(struct aac_softc *sc, int mask);
215 void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
216 u_int32_t arg0, u_int32_t arg1,
217 u_int32_t arg2, u_int32_t arg3);
218 int (*aif_get_mailbox)(struct aac_softc *sc, int mb);
219 void (*aif_set_interrupts)(struct aac_softc *sc, int enable);
221 extern struct aac_interface aac_rx_interface;
222 extern struct aac_interface aac_sa_interface;
223 extern struct aac_interface aac_fa_interface;
225 #define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc)))
226 #define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit)))
227 #define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc)))
228 #define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \
230 #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
231 ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
233 #define AAC_GET_MAILBOX(sc, mb) ((sc)->aac_if.aif_get_mailbox((sc), \
235 #define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \
237 #define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \
240 #define AAC_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag, \
241 sc->aac_bhandle, reg, val)
242 #define AAC_GETREG4(sc, reg) bus_space_read_4 (sc->aac_btag, \
243 sc->aac_bhandle, reg)
244 #define AAC_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag, \
245 sc->aac_bhandle, reg, val)
246 #define AAC_GETREG2(sc, reg) bus_space_read_2 (sc->aac_btag, \
247 sc->aac_bhandle, reg)
248 #define AAC_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag, \
249 sc->aac_bhandle, reg, val)
250 #define AAC_GETREG1(sc, reg) bus_space_read_1 (sc->aac_btag, \
251 sc->aac_bhandle, reg)
253 TAILQ_HEAD(aac_container_tq, aac_container);
255 /* Define the OS version specific locks */
256 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
257 #include <sys/lock.h>
258 #include <sys/mutex.h>
259 typedef struct mtx aac_lock_t;
260 #define AAC_LOCK_INIT(l, s) mtx_init(l, s, MTX_DEF)
261 #define AAC_LOCK_ACQUIRE(l) mtx_lock(l)
262 #define AAC_LOCK_RELEASE(l) mtx_unlock(l)
264 typedef struct lock aac_lock_t;
265 #define AAC_LOCK_INIT(l, s) lockinit(l, "aac", 0, 0)
266 #define AAC_LOCK_ACQUIRE(l) lockmgr(l, LK_EXCLUSIVE|LK_RETRY)
267 #define AAC_LOCK_RELEASE(l) lockmgr(l, LK_RELEASE)
270 #include <sys/selinfo.h>
273 * Per-controller structure.
277 /* bus connections */
279 struct callout aac_watchdog;
280 struct resource *aac_regs_resource; /* register interface
282 int aac_regs_rid; /* resource ID */
283 bus_space_handle_t aac_bhandle; /* bus space handle */
284 bus_space_tag_t aac_btag; /* bus space tag */
285 bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */
286 bus_dma_tag_t aac_buffer_dmat; /* data buffer/command
288 struct resource *aac_irq; /* interrupt */
290 void *aac_intr; /* interrupt handle */
292 /* controller features, limits and status */
294 #define AAC_STATE_SUSPEND (1<<0)
295 #define AAC_STATE_OPEN (1<<1)
296 #define AAC_STATE_INTERRUPTS_ON (1<<2)
297 #define AAC_STATE_AIF_SLEEPER (1<<3)
298 struct FsaRevision aac_revision;
300 /* controller hardware interface */
302 #define AAC_HWIF_I960RX 0
303 #define AAC_HWIF_STRONGARM 1
304 #define AAC_HWIF_FALCON 2
305 #define AAC_HWIF_UNKNOWN -1
306 bus_dma_tag_t aac_common_dmat; /* common structure
308 bus_dmamap_t aac_common_dmamap; /* common structure
310 struct aac_common *aac_common;
311 u_int32_t aac_common_busaddr;
312 struct aac_interface aac_if;
314 /* command/fib resources */
315 bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */
316 struct aac_fib *aac_fibs;
317 bus_dmamap_t aac_fibmap;
318 u_int32_t aac_fibphys;
319 struct aac_command aac_command[AAC_FIB_COUNT];
321 /* command management */
322 TAILQ_HEAD(,aac_command) aac_free; /* command structures
323 * available for reuse */
324 TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for
325 * controller resources */
326 TAILQ_HEAD(,aac_command) aac_busy;
327 TAILQ_HEAD(,aac_command) aac_complete; /* commands which have been
328 * returned by the controller */
329 struct bio_queue_head aac_bioq;
330 struct aac_queue_table *aac_queues;
331 struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT];
333 struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */
335 /* connected containters */
336 struct aac_container_tq aac_container_tqh;
337 aac_lock_t aac_container_lock;
339 /* Protect the sync fib */
340 #define AAC_SYNC_LOCK_FORCE (1 << 0)
341 aac_lock_t aac_sync_lock;
343 /* delayed activity infrastructure */
344 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
345 struct task aac_task_complete; /* deferred-completion
348 struct intr_config_hook aac_ich;
350 /* management interface */
352 aac_lock_t aac_aifq_lock;
353 struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH];
356 struct selinfo rcv_select;
357 struct thread *aifthread;
359 #define AAC_AIFFLAGS_RUNNING (1 << 0)
360 #define AAC_AIFFLAGS_PENDING (1 << 1)
361 #define AAC_AIFFLAGS_EXIT (1 << 2)
362 #define AAC_AIFFLAGS_EXITED (1 << 3)
364 #define AAC_FLAGS_PERC2QC (1 << 0)
365 #define AAC_FLAGS_ENABLE_CAM (1 << 1) /* No SCSI passthrough */
366 #define AAC_FLAGS_CAM_NORESET (1 << 2) /* Fake SCSI resets */
367 #define AAC_FLAGS_CAM_PASSONLY (1 << 3) /* Only create pass devices */
368 #define AAC_FLAGS_4GB_WINDOW (1 << 5) /* Device can access host mem
370 #define AAC_FLAGS_NO4GB (1 << 6) /* Can't access host mem >2GB */
371 #define AAC_FLAGS_256FIBS (1 << 7) /* Can only do 256 commands */
373 u_int32_t supported_options;
374 u_int32_t scsi_method_id;
381 extern void aac_free(struct aac_softc *sc);
382 extern int aac_attach(struct aac_softc *sc);
383 extern int aac_detach(device_t dev);
384 extern int aac_shutdown(device_t dev);
385 extern int aac_suspend(device_t dev);
386 extern int aac_resume(device_t dev);
387 extern void aac_intr(void *arg);
388 extern devclass_t aac_devclass;
389 extern void aac_submit_bio(struct aac_disk *ad, struct bio *bio);
390 extern void aac_biodone(struct bio *bio, const char *code);
391 extern int aac_dump_enqueue(struct aac_disk *ad, u_int32_t lba,
392 void *data, int nblks);
393 extern void aac_dump_complete(struct aac_softc *sc);
394 extern void aac_startio(struct aac_softc *sc);
395 extern int aac_alloc_command(struct aac_softc *sc,
396 struct aac_command **cmp);
397 extern void aac_release_command(struct aac_command *cm);
398 extern int aac_alloc_sync_fib(struct aac_softc *sc,
399 struct aac_fib **fib, int flags);
400 extern void aac_release_sync_fib(struct aac_softc *sc);
401 extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command,
402 u_int32_t xferstate, struct aac_fib *fib,
407 * 0 - quiet, only emit warnings
408 * 1 - noisy, emit major function points and things done
409 * 2 - extremely noisy, emit trace items in loops, etc.
412 # define debug(level, fmt, args...) \
414 if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
416 # define debug_called(level) \
418 if (level <= AAC_DEBUG) printf(__func__ ": called\n"); \
421 extern void aac_print_queues(struct aac_softc *sc);
422 extern void aac_panic(struct aac_softc *sc, char *reason);
423 extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
425 extern void aac_print_aif(struct aac_softc *sc,
426 struct aac_aif_command *aif);
428 # define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__)
431 # define debug(level, fmt, args...)
432 # define debug_called(level)
434 # define aac_print_queues(sc)
435 # define aac_panic(sc, reason)
437 # define AAC_PRINT_FIB(sc, fib)
438 # define aac_print_aif(sc, aac_aif_command)
441 struct aac_code_lookup {
447 * Queue primitives for driver queues.
449 #define AACQ_ADD(sc, qname) \
451 struct aac_qstat *qs; \
453 qs = &(sc)->aac_qstat[qname]; \
456 if (qs->q_length > qs->q_max) \
457 qs->q_max = qs->q_length; \
460 #define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length--
461 #define AACQ_INIT(sc, qname) \
463 sc->aac_qstat[qname].q_length = 0; \
464 sc->aac_qstat[qname].q_max = 0; \
468 #define AACQ_COMMAND_QUEUE(name, index) \
469 static __inline void \
470 aac_initq_ ## name (struct aac_softc *sc) \
472 TAILQ_INIT(&sc->aac_ ## name); \
473 AACQ_INIT(sc, index); \
475 static __inline void \
476 aac_enqueue_ ## name (struct aac_command *cm) \
479 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
480 printf("command %p is on another queue, flags = %#x\n", \
482 panic("command is on another queue"); \
484 TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \
485 cm->cm_flags |= AAC_ON_ ## index; \
486 AACQ_ADD(cm->cm_sc, index); \
489 static __inline void \
490 aac_requeue_ ## name (struct aac_command *cm) \
493 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
494 printf("command %p is on another queue, flags = %#x\n", \
496 panic("command is on another queue"); \
498 TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \
499 cm->cm_flags |= AAC_ON_ ## index; \
500 AACQ_ADD(cm->cm_sc, index); \
503 static __inline struct aac_command * \
504 aac_dequeue_ ## name (struct aac_softc *sc) \
506 struct aac_command *cm; \
509 if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \
510 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
511 printf("command %p not in queue, flags = %#x, " \
512 "bit = %#x\n", cm, cm->cm_flags, \
514 panic("command not in queue"); \
516 TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \
517 cm->cm_flags &= ~AAC_ON_ ## index; \
518 AACQ_REMOVE(sc, index); \
523 static __inline void \
524 aac_remove_ ## name (struct aac_command *cm) \
527 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
528 printf("command %p not in queue, flags = %#x, " \
529 "bit = %#x\n", cm, cm->cm_flags, \
531 panic("command not in queue"); \
533 TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \
534 cm->cm_flags &= ~AAC_ON_ ## index; \
535 AACQ_REMOVE(cm->cm_sc, index); \
540 AACQ_COMMAND_QUEUE(free, AACQ_FREE);
541 AACQ_COMMAND_QUEUE(ready, AACQ_READY);
542 AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
543 AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE);
546 * outstanding bio queue
549 aac_initq_bio(struct aac_softc *sc)
551 bioq_init(&sc->aac_bioq);
552 AACQ_INIT(sc, AACQ_BIO);
556 aac_enqueue_bio(struct aac_softc *sc, struct bio *bio)
559 bioq_insert_tail(&sc->aac_bioq, bio);
560 AACQ_ADD(sc, AACQ_BIO);
564 static __inline struct bio *
565 aac_dequeue_bio(struct aac_softc *sc)
570 if ((bio = bioq_first(&sc->aac_bioq)) != NULL) {
571 bioq_remove(&sc->aac_bioq, bio);
572 AACQ_REMOVE(sc, AACQ_BIO);
579 aac_print_printf(struct aac_softc *sc)
582 * XXX We have the ability to read the length of the printf string
583 * from out of the mailboxes.
585 device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
586 sc->aac_common->ac_printf);
587 AAC_QNOTIFY(sc, AAC_DB_PRINTF);