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 $
34 #include <sys/eventhandler.h>
36 #include <sys/event.h>
37 #include <sys/taskqueue.h>
38 #include <sys/thread2.h>
41 * Driver Parameter Definitions
45 * The firmware interface allows for a 16-bit s/g list length. We limit
46 * ourselves to a reasonable maximum and ensure alignment.
48 #define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */
51 * We allocate a small set of FIBs for the adapter to use to send us messages.
53 #define AAC_ADAPTER_FIBS 8
56 * FIBs are allocated in page-size chunks and can grow up to the 512
57 * limit imposed by the hardware.
59 #define AAC_PREALLOCATE_FIBS 128
60 #define AAC_NUM_MGT_FIB 8
62 * The controller reports status events in AIFs. We hang on to a number of
63 * these in order to pass them out to user-space management tools.
65 #define AAC_AIFQ_LENGTH 64
68 * Firmware messages are passed in the kprintf buffer.
70 #define AAC_PRINTF_BUFSIZE 256
73 * We wait this many seconds for the adapter to come ready if it is still
76 #define AAC_BOOT_TIMEOUT (3 * 60)
79 * Timeout for immediate commands.
81 #define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */
84 * Timeout for normal commands
86 #define AAC_CMD_TIMEOUT 30 /* seconds */
89 * Rate at which we periodically check for timed out commands and kick the
92 #define AAC_PERIODIC_INTERVAL 20 /* seconds */
95 * Per-container data structure
99 struct aac_mntobj co_mntobj;
102 TAILQ_ENTRY(aac_container) co_link;
106 * Per-SIM data structure
114 struct aac_softc *aac_sc;
115 TAILQ_ENTRY(aac_sim) sim_link;
125 struct aac_softc *ad_controller;
126 struct aac_container *ad_container;
128 struct devstat ad_stats;
130 #define AAC_DISK_OPEN (1<<0)
139 * Per-command control structure.
143 TAILQ_ENTRY(aac_command) cm_link; /* list linkage */
145 struct aac_softc *cm_sc; /* controller that owns us */
147 struct aac_fib *cm_fib; /* FIB associated with this
149 u_int64_t cm_fibphys; /* bus address of the FIB */
150 struct buf *cm_data; /* pointer to data in kernel
152 u_int32_t cm_datalen; /* data length */
153 bus_dmamap_t cm_datamap; /* DMA map for bio data */
154 struct aac_sg_table *cm_sgtable; /* pointer to s/g table in
157 #define AAC_CMD_MAPPED (1<<0) /* command has had its data
159 #define AAC_CMD_DATAIN (1<<1) /* command involves data moving
160 * from controller to host */
161 #define AAC_CMD_DATAOUT (1<<2) /* command involves data moving
162 * from host to controller */
163 #define AAC_CMD_COMPLETED (1<<3) /* command has been completed */
164 #define AAC_CMD_TIMEDOUT (1<<4) /* command taken too long */
165 #define AAC_ON_AACQ_FREE (1<<5)
166 #define AAC_ON_AACQ_READY (1<<6)
167 #define AAC_ON_AACQ_BUSY (1<<7)
168 #define AAC_ON_AACQ_COMPLETE (1<<8)
169 #define AAC_ON_AACQ_NORM (1<<10)
170 #define AAC_ON_AACQ_AIF (1<<11)
171 #define AAC_ON_AACQ_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10)|(1<<11))
172 #define AAC_QUEUE_FRZN (1<<9) /* Freeze the processing of
173 * commands on the queue. */
175 void (* cm_complete)(struct aac_command *cm);
177 time_t cm_timestamp; /* command creation time */
183 TAILQ_ENTRY(aac_fibmap) fm_link; /* list linkage */
184 struct aac_fib *aac_fibs;
185 bus_dmamap_t aac_fibmap;
186 struct aac_command *aac_commands;
190 * We gather a number of adapter-visible items into a single structure.
192 * The ordering of this strucure may be important; we copy the Linux driver:
196 * Queue headers (Comm Area)
199 * In addition, we add:
203 /* fibs for the controller to send us messages */
204 struct aac_fib ac_fibs[AAC_ADAPTER_FIBS];
206 /* the init structure */
207 struct aac_adapter_init ac_init;
209 /* arena within which the queue structures are kept */
210 u_int8_t ac_qbuf[sizeof(struct aac_queue_table) +
213 /* buffer for text messages from the controller */
214 char ac_printf[AAC_PRINTF_BUFSIZE];
216 /* fib for synchronous commands */
217 struct aac_fib ac_sync_fib;
221 * Interface operations
225 int (*aif_get_fwstatus)(struct aac_softc *sc);
226 void (*aif_qnotify)(struct aac_softc *sc, int qbit);
227 int (*aif_get_istatus)(struct aac_softc *sc);
228 void (*aif_clr_istatus)(struct aac_softc *sc, int mask);
229 void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
230 u_int32_t arg0, u_int32_t arg1,
231 u_int32_t arg2, u_int32_t arg3);
232 int (*aif_get_mailbox)(struct aac_softc *sc, int mb);
233 void (*aif_set_interrupts)(struct aac_softc *sc, int enable);
234 int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm);
235 int (*aif_get_outb_queue)(struct aac_softc *sc);
236 void (*aif_set_outb_queue)(struct aac_softc *sc, int index);
238 extern struct aac_interface aac_rx_interface;
239 extern struct aac_interface aac_sa_interface;
240 extern struct aac_interface aac_fa_interface;
241 extern struct aac_interface aac_rkt_interface;
243 #define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc)))
244 #define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit)))
245 #define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc)))
246 #define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \
248 #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
249 ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
251 #define AAC_GET_MAILBOX(sc, mb) ((sc)->aac_if.aif_get_mailbox((sc), \
253 #define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \
255 #define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \
257 #define AAC_SEND_COMMAND(sc, cm) ((sc)->aac_if.aif_send_command((sc), (cm)))
258 #define AAC_GET_OUTB_QUEUE(sc) ((sc)->aac_if.aif_get_outb_queue((sc)))
259 #define AAC_SET_OUTB_QUEUE(sc, idx) ((sc)->aac_if.aif_set_outb_queue((sc), (idx)))
261 #define AAC_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag, \
262 sc->aac_bhandle, reg, val)
263 #define AAC_GETREG4(sc, reg) bus_space_read_4 (sc->aac_btag, \
264 sc->aac_bhandle, reg)
265 #define AAC_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag, \
266 sc->aac_bhandle, reg, val)
267 #define AAC_GETREG2(sc, reg) bus_space_read_2 (sc->aac_btag, \
268 sc->aac_bhandle, reg)
269 #define AAC_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag, \
270 sc->aac_bhandle, reg, val)
271 #define AAC_GETREG1(sc, reg) bus_space_read_1 (sc->aac_btag, \
272 sc->aac_bhandle, reg)
274 /* Define the OS version specific locks */
275 typedef struct lock aac_lock_t;
276 #define AAC_LOCK_INIT(l, s) lockinit(l, s, 0, LK_CANRECURSE)
277 #define AAC_LOCK_ACQUIRE(l) lockmgr(l, LK_EXCLUSIVE)
278 #define AAC_LOCK_RELEASE(l) lockmgr(l, LK_RELEASE)
281 * Per-controller structure.
285 /* bus connections */
287 struct callout aac_watchdog;
288 struct resource *aac_regs_resource; /* register interface
290 int aac_regs_rid; /* resource ID */
291 bus_space_handle_t aac_bhandle; /* bus space handle */
292 bus_space_tag_t aac_btag; /* bus space tag */
293 bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */
294 bus_dma_tag_t aac_buffer_dmat; /* data buffer/command
296 struct resource *aac_irq; /* interrupt */
298 void *aac_intr; /* interrupt handle */
301 /* controller features, limits and status */
303 #define AAC_STATE_SUSPEND (1<<0)
304 #define AAC_STATE_OPEN (1<<1)
305 #define AAC_STATE_INTERRUPTS_ON (1<<2)
306 #define AAC_STATE_AIF_SLEEPER (1<<3)
307 struct FsaRevision aac_revision;
309 /* controller hardware interface */
311 #define AAC_HWIF_I960RX 0
312 #define AAC_HWIF_STRONGARM 1
313 #define AAC_HWIF_FALCON 2
314 #define AAC_HWIF_RKT 3
315 #define AAC_HWIF_UNKNOWN -1
316 bus_dma_tag_t aac_common_dmat; /* common structure
318 bus_dmamap_t aac_common_dmamap; /* common structure
320 struct aac_common *aac_common;
321 u_int32_t aac_common_busaddr;
322 struct aac_interface aac_if;
324 /* command/fib resources */
325 bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */
326 TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh;
328 struct aac_command *aac_commands;
330 /* command management */
331 TAILQ_HEAD(,aac_command) aac_free; /* command structures
332 * available for reuse */
333 TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for
334 * controller resources */
335 TAILQ_HEAD(,aac_command) aac_busy;
336 TAILQ_HEAD(,aac_command) aac_complete; /* commands which have been
337 * returned by the controller */
338 TAILQ_HEAD(,aac_command) aac_aif;
340 TAILQ_HEAD(,aac_command) aac_norm;
342 TAILQ_HEAD(,aac_event) aac_ev_cmfree;
343 struct bio_queue_head aac_bioq;
344 struct aac_queue_table *aac_queues;
345 struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT];
347 struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */
349 /* connected containters */
350 TAILQ_HEAD(,aac_container) aac_container_tqh;
351 aac_lock_t aac_container_lock;
354 * The general I/O lock. This protects the sync fib, the lists, the
355 * queues, and the registers.
357 aac_lock_t aac_io_lock;
359 /* delayed activity infrastructure */
360 struct task aac_task_complete; /* deferred-completion
362 struct intr_config_hook aac_ich;
364 /* management interface */
366 aac_lock_t aac_aifq_lock;
367 struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH];
370 struct kqinfo rcv_kq;
371 struct thread *aifthread;
373 #define AAC_AIFFLAGS_RUNNING (1 << 0)
374 #define AAC_AIFFLAGS_AIF (1 << 1)
375 #define AAC_AIFFLAGS_EXIT (1 << 2)
376 #define AAC_AIFFLAGS_EXITED (1 << 3)
377 #define AAC_AIFFLAGS_PRINTF (1 << 4)
378 #define AAC_AIFFLAGS_ALLOCFIBS (1 << 5)
379 #define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
380 AAC_AIFFLAGS_ALLOCFIBS)
382 #define AAC_FLAGS_PERC2QC (1 << 0)
383 #define AAC_FLAGS_ENABLE_CAM (1 << 1) /* No SCSI passthrough */
384 #define AAC_FLAGS_CAM_NORESET (1 << 2) /* Fake SCSI resets */
385 #define AAC_FLAGS_CAM_PASSONLY (1 << 3) /* Only create pass devices */
386 #define AAC_FLAGS_SG_64BIT (1 << 4) /* Use 64-bit S/G addresses */
387 #define AAC_FLAGS_4GB_WINDOW (1 << 5) /* Device can access host mem
389 #define AAC_FLAGS_NO4GB (1 << 6) /* Can't access host mem >2GB */
390 #define AAC_FLAGS_256FIBS (1 << 7) /* Can only do 256 commands */
391 #define AAC_FLAGS_BROKEN_MEMMAP (1 << 8) /* Broken HostPhysMemPages */
392 #define AAC_FLAGS_SLAVE (1 << 9)
393 #define AAC_FLAGS_MASTER (1 << 10)
394 #define AAC_FLAGS_NEW_COMM (1 << 11) /* New comm. interface supported */
395 #define AAC_FLAGS_RAW_IO (1 << 12) /* Raw I/O interface */
396 #define AAC_FLAGS_ARRAY_64BIT (1 << 13) /* 64-bit array size */
398 u_int32_t supported_options;
399 u_int32_t scsi_method_id;
400 TAILQ_HEAD(,aac_sim) aac_sim_tqh;
402 u_int32_t aac_max_fibs; /* max. FIB count */
403 u_int32_t aac_max_fibs_alloc; /* max. alloc. per alloc_commands() */
404 u_int32_t aac_max_fib_size; /* max. FIB size */
405 u_int32_t aac_sg_tablesize; /* max. sg count from host */
406 u_int32_t aac_max_sectors; /* max. I/O size from host (blocks) */
410 * Event callback mechanism for the driver
412 #define AAC_EVENT_NONE 0x00
413 #define AAC_EVENT_CMFREE 0x01
414 #define AAC_EVENT_MASK 0xff
415 #define AAC_EVENT_REPEAT 0x100
417 typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event,
420 TAILQ_ENTRY(aac_event) ev_links;
422 aac_event_cb_t *ev_callback;
426 #ifndef AAC_DRIVER_BUILD
427 # define AAC_DRIVER_BUILD 1
433 extern void aac_free(struct aac_softc *sc);
434 extern int aac_attach(struct aac_softc *sc);
435 extern int aac_detach(device_t dev);
436 extern int aac_suspend(device_t dev);
437 extern int aac_resume(device_t dev);
438 extern void aac_new_intr(void *arg);
439 extern void aac_fast_intr(void *arg);
440 extern devclass_t aac_devclass;
441 extern void aac_submit_bio(struct aac_disk *ad, struct bio *bio);
442 extern void aac_biodone(struct bio *bio, const char *code);
443 extern int aac_dump_enqueue(struct aac_disk *ad, u_int64_t lba,
444 void *data, int nblks);
445 extern void aac_dump_complete(struct aac_softc *sc);
446 extern void aac_startio(struct aac_softc *sc);
447 extern int aac_alloc_command(struct aac_softc *sc,
448 struct aac_command **cmp);
449 extern void aac_release_command(struct aac_command *cm);
450 extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command,
451 u_int32_t xferstate, struct aac_fib *fib,
453 extern void aac_add_event(struct aac_softc *sc, struct aac_event
458 * 0 - quiet, only emit warnings
459 * 1 - noisy, emit major function points and things done
460 * 2 - extremely noisy, emit trace items in loops, etc.
463 # define debug(level, fmt, args...) \
465 if (level <=AAC_DEBUG) kprintf("%s: " fmt "\n", __func__ , ##args); \
467 # define debug_called(level) \
469 if (level <= AAC_DEBUG) kprintf("%s: called\n", __func__); \
472 extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
474 extern void aac_print_aif(struct aac_softc *sc,
475 struct aac_aif_command *aif);
477 # define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__)
480 # define debug(level, fmt, args...)
481 # define debug_called(level)
483 # define AAC_PRINT_FIB(sc, fib)
484 # define aac_print_aif(sc, aac_aif_command)
487 struct aac_code_lookup {
493 * Queue primitives for driver queues.
495 #define AACQ_ADD(sc, qname) \
497 struct aac_qstat *qs; \
499 qs = &(sc)->aac_qstat[qname]; \
502 if (qs->q_length > qs->q_max) \
503 qs->q_max = qs->q_length; \
506 #define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length--
507 #define AACQ_INIT(sc, qname) \
509 sc->aac_qstat[qname].q_length = 0; \
510 sc->aac_qstat[qname].q_max = 0; \
514 #define AACQ_COMMAND_QUEUE(name, index) \
515 static __inline void \
516 aac_initq_ ## name (struct aac_softc *sc) \
518 TAILQ_INIT(&sc->aac_ ## name); \
519 AACQ_INIT(sc, index); \
521 static __inline void \
522 aac_enqueue_ ## name (struct aac_command *cm) \
524 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
525 kprintf("command %p is on another queue, flags = %#x\n", \
527 panic("command is on another queue"); \
529 TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link); \
530 cm->cm_flags |= AAC_ON_ ## index; \
531 AACQ_ADD(cm->cm_sc, index); \
533 static __inline void \
534 aac_requeue_ ## name (struct aac_command *cm) \
536 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
537 kprintf("command %p is on another queue, flags = %#x\n", \
539 panic("command is on another queue"); \
541 TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \
542 cm->cm_flags |= AAC_ON_ ## index; \
543 AACQ_ADD(cm->cm_sc, index); \
545 static __inline struct aac_command * \
546 aac_dequeue_ ## name (struct aac_softc *sc) \
548 struct aac_command *cm; \
550 if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \
551 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
552 kprintf("command %p not in queue, flags = %#x, " \
553 "bit = %#x\n", cm, cm->cm_flags, \
555 panic("command not in queue"); \
557 TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \
558 cm->cm_flags &= ~AAC_ON_ ## index; \
559 AACQ_REMOVE(sc, index); \
563 static __inline void \
564 aac_remove_ ## name (struct aac_command *cm) \
566 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
567 kprintf("command %p not in queue, flags = %#x, " \
568 "bit = %#x\n", cm, cm->cm_flags, \
570 panic("command not in queue"); \
572 TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \
573 cm->cm_flags &= ~AAC_ON_ ## index; \
574 AACQ_REMOVE(cm->cm_sc, index); \
578 AACQ_COMMAND_QUEUE(free, AACQ_FREE);
579 AACQ_COMMAND_QUEUE(ready, AACQ_READY);
580 AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
581 AACQ_COMMAND_QUEUE(complete, AACQ_COMPLETE);
584 * outstanding bio queue
587 aac_initq_bio(struct aac_softc *sc)
589 bioq_init(&sc->aac_bioq);
590 AACQ_INIT(sc, AACQ_BIO);
594 aac_enqueue_bio(struct aac_softc *sc, struct bio *bio)
596 bioqdisksort(&sc->aac_bioq, bio);
597 AACQ_ADD(sc, AACQ_BIO);
600 static __inline struct bio *
601 aac_dequeue_bio(struct aac_softc *sc)
605 if ((bio = bioq_first(&sc->aac_bioq)) != NULL) {
606 bioq_remove(&sc->aac_bioq, bio);
607 AACQ_REMOVE(sc, AACQ_BIO);
613 aac_print_printf(struct aac_softc *sc)
616 * XXX We have the ability to read the length of the kprintf string
617 * from out of the mailboxes.
619 device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
620 sc->aac_common->ac_printf);
621 sc->aac_common->ac_printf[0] = 0;
622 AAC_QNOTIFY(sc, AAC_DB_PRINTF);
626 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
629 KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);
630 *fib = &sc->aac_common->ac_sync_fib;
635 aac_release_sync_fib(struct aac_softc *sc)
638 KKASSERT(lockstatus(&sc->aac_io_lock, curthread) != 0);