1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/dev/aac/aacvar.h 117363 2003-07-09 21:16:23Z scottl $
30 */
31
32#include <sys/bio.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/taskqueue.h>
36#include <sys/selinfo.h>
37#include <geom/geom_disk.h>
38
39
40/*
41 * Driver Parameter Definitions
42 */
43
44/*
45 * The firmware interface allows for a 16-bit s/g list length. We limit
46 * ourselves to a reasonable maximum and ensure alignment.
47 */
48#define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */
49
50/*
51 * We allocate a small set of FIBs for the adapter to use to send us messages.
52 */
53#define AAC_ADAPTER_FIBS 8
54
55/*
56 * FIBs are allocated in page-size chunks and can grow up to the 512
57 * limit imposed by the hardware.
58 */
59#define AAC_FIB_COUNT (PAGE_SIZE/sizeof(struct aac_fib))
60#define AAC_PREALLOCATE_FIBS 128
61#define AAC_MAX_FIBS 512
62
63/*
64 * The controller reports status events in AIFs. We hang on to a number of
65 * these in order to pass them out to user-space management tools.
66 */
67#define AAC_AIFQ_LENGTH 64
68
69/*
70 * Firmware messages are passed in the printf buffer.
71 */
72#define AAC_PRINTF_BUFSIZE 256
73
74/*
75 * We wait this many seconds for the adapter to come ready if it is still
76 * booting
77 */
78#define AAC_BOOT_TIMEOUT (3 * 60)
79
80/*
81 * Timeout for immediate commands.
82 */
83#define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */
84
85/*
86 * Timeout for normal commands
87 */
88#define AAC_CMD_TIMEOUT 30 /* seconds */
89
90/*
91 * Rate at which we periodically check for timed out commands and kick the
92 * controller.
93 */
94#define AAC_PERIODIC_INTERVAL 20 /* seconds */
95
96/*
97 * Per-container data structure
98 */
99struct aac_container
100{
101 struct aac_mntobj co_mntobj;
102 device_t co_disk;
103 int co_found;
104 TAILQ_ENTRY(aac_container) co_link;
105};
106
107/*
108 * Per-SIM data structure
109 */
110struct aac_sim
111{
112 device_t sim_dev;
113 int TargetsPerBus;
114 int BusNumber;
115 int InitiatorBusId;
116 struct aac_softc *aac_sc;
117 TAILQ_ENTRY(aac_sim) sim_link;
118};
119
120/*
121 * Per-disk structure
122 */
123struct aac_disk
124{
125 device_t ad_dev;
126 struct aac_softc *ad_controller;
127 struct aac_container *ad_container;
128 struct disk ad_disk;
129 int ad_flags;
130#define AAC_DISK_OPEN (1<<0)
131 int ad_cylinders;
132 int ad_heads;
133 int ad_sectors;
134 u_int32_t ad_size;
135 int unit;
136};
137
138/*
139 * Per-command control structure.
140 */
141struct aac_command
142{
143 TAILQ_ENTRY(aac_command) cm_link; /* list linkage */
144
145 struct aac_softc *cm_sc; /* controller that owns us */
146
147 struct aac_fib *cm_fib; /* FIB associated with this
148 * command */
149 u_int32_t cm_fibphys; /* bus address of the FIB */
150 struct bio *cm_data; /* pointer to data in kernel
151 * space */
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
155 * command */
156 int cm_flags;
157#define AAC_CMD_MAPPED (1<<0) /* command has had its data
158 * mapped */
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_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8))
170#define AAC_QUEUE_FRZN (1<<9) /* Freeze the processing of
171 * commands on the queue. */
172
173 void (* cm_complete)(struct aac_command *cm);
174 void *cm_private;
175 time_t cm_timestamp; /* command creation time */
176 int cm_queue;
177 int cm_index;
178};
179
180struct aac_fibmap {
181 TAILQ_ENTRY(aac_fibmap) fm_link; /* list linkage */
182 struct aac_fib *aac_fibs;
183 bus_dmamap_t aac_fibmap;
184 struct aac_command *aac_commands;
185};
186
187/*
188 * We gather a number of adapter-visible items into a single structure.
189 *
190 * The ordering of this strucure may be important; we copy the Linux driver:
191 *
192 * Adapter FIBs
193 * Init struct
194 * Queue headers (Comm Area)
195 * Printf buffer
196 *
197 * In addition, we add:
198 * Sync Fib
199 */
200struct aac_common {
201 /* fibs for the controller to send us messages */
202 struct aac_fib ac_fibs[AAC_ADAPTER_FIBS];
203
204 /* the init structure */
205 struct aac_adapter_init ac_init;
206
207 /* arena within which the queue structures are kept */
208 u_int8_t ac_qbuf[sizeof(struct aac_queue_table) +
209 AAC_QUEUE_ALIGN];
210
211 /* buffer for text messages from the controller */
212 char ac_printf[AAC_PRINTF_BUFSIZE];
213
214 /* fib for synchronous commands */
215 struct aac_fib ac_sync_fib;
216};
217
218/*
219 * Interface operations
220 */
221struct aac_interface
222{
223 int (*aif_get_fwstatus)(struct aac_softc *sc);
224 void (*aif_qnotify)(struct aac_softc *sc, int qbit);
225 int (*aif_get_istatus)(struct aac_softc *sc);
226 void (*aif_clr_istatus)(struct aac_softc *sc, int mask);
227 void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
228 u_int32_t arg0, u_int32_t arg1,
229 u_int32_t arg2, u_int32_t arg3);
230 int (*aif_get_mailbox)(struct aac_softc *sc, int mb);
231 void (*aif_set_interrupts)(struct aac_softc *sc, int enable);
232};
233extern struct aac_interface aac_rx_interface;
234extern struct aac_interface aac_sa_interface;
235extern struct aac_interface aac_fa_interface;
236
237#define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc)))
238#define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit)))
239#define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc)))
240#define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \
241 (mask)))
242#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
243 ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
244 (arg3)))
245#define AAC_GET_MAILBOX(sc, mb) ((sc)->aac_if.aif_get_mailbox((sc), \
246 (mb)))
247#define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \
248 0))
249#define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \
250 1))
251
252#define AAC_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag, \
253 sc->aac_bhandle, reg, val)
254#define AAC_GETREG4(sc, reg) bus_space_read_4 (sc->aac_btag, \
255 sc->aac_bhandle, reg)
256#define AAC_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag, \
257 sc->aac_bhandle, reg, val)
258#define AAC_GETREG2(sc, reg) bus_space_read_2 (sc->aac_btag, \
259 sc->aac_bhandle, reg)
260#define AAC_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag, \
261 sc->aac_bhandle, reg, val)
262#define AAC_GETREG1(sc, reg) bus_space_read_1 (sc->aac_btag, \
263 sc->aac_bhandle, reg)
264
265/* Define the OS version specific locks */
266typedef struct mtx aac_lock_t;
267#define AAC_LOCK_INIT(l, s) mtx_init(l, s, NULL, MTX_DEF)
268#define AAC_LOCK_ACQUIRE(l) mtx_lock(l)
269#define AAC_LOCK_RELEASE(l) mtx_unlock(l)
270
271
272/*
273 * Per-controller structure.
274 */
275struct aac_softc
276{
277 /* bus connections */
278 device_t aac_dev;
279 struct resource *aac_regs_resource; /* register interface
280 * window */
281 int aac_regs_rid; /* resource ID */
282 bus_space_handle_t aac_bhandle; /* bus space handle */
283 bus_space_tag_t aac_btag; /* bus space tag */
284 bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */
285 bus_dma_tag_t aac_buffer_dmat; /* data buffer/command
286 * DMA tag */
287 struct resource *aac_irq; /* interrupt */
288 int aac_irq_rid;
289 void *aac_intr; /* interrupt handle */
290 eventhandler_tag eh;
291
292 /* controller features, limits and status */
293 int aac_state;
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;
299
300 /* controller hardware interface */
301 int aac_hwif;
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
307 * DMA tag */
308 bus_dmamap_t aac_common_dmamap; /* common structure
309 * DMA map */
310 struct aac_common *aac_common;
311 u_int32_t aac_common_busaddr;
312 struct aac_interface aac_if;
313
314 /* command/fib resources */
315 bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */
316 TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh;
317 uint total_fibs;
318 struct aac_command *aac_commands;
319
320 /* command management */
321 TAILQ_HEAD(,aac_command) aac_free; /* command structures
322 * available for reuse */
323 TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for
324 * controller resources */
325 TAILQ_HEAD(,aac_command) aac_busy;
326 struct bio_queue_head aac_bioq;
327 struct aac_queue_table *aac_queues;
328 struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT];
329
330 struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */
331
332 /* connected containters */
333 TAILQ_HEAD(,aac_container) aac_container_tqh;
334 aac_lock_t aac_container_lock;
335
336 /* Protect the sync fib */
337#define AAC_SYNC_LOCK_FORCE (1 << 0)
338 aac_lock_t aac_sync_lock;
339
340 aac_lock_t aac_io_lock;
341
342 /* delayed activity infrastructure */
343 struct task aac_task_complete; /* deferred-completion
344 * task */
345 struct intr_config_hook aac_ich;
346
347 /* management interface */
348 dev_t aac_dev_t;
349 aac_lock_t aac_aifq_lock;
350 struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH];
351 int aac_aifq_head;
352 int aac_aifq_tail;
353 struct selinfo rcv_select;
354 struct proc *aifthread;
355 int aifflags;
356#define AAC_AIFFLAGS_RUNNING (1 << 0)
357#define AAC_AIFFLAGS_AIF (1 << 1)
358#define AAC_AIFFLAGS_EXIT (1 << 2)
359#define AAC_AIFFLAGS_EXITED (1 << 3)
360#define AAC_AIFFLAGS_PRINTF (1 << 4)
361#define AAC_AIFFLAGS_ALLOCFIBS (1 << 5)
362#define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
363 AAC_AIFFLAGS_ALLOCFIBS)
364 u_int32_t flags;
365#define AAC_FLAGS_PERC2QC (1 << 0)
366#define AAC_FLAGS_ENABLE_CAM (1 << 1) /* No SCSI passthrough */
367#define AAC_FLAGS_CAM_NORESET (1 << 2) /* Fake SCSI resets */
368#define AAC_FLAGS_CAM_PASSONLY (1 << 3) /* Only create pass devices */
369#define AAC_FLAGS_SG_64BIT (1 << 4) /* Use 64-bit S/G addresses */
370#define AAC_FLAGS_4GB_WINDOW (1 << 5) /* Device can access host mem
371 * 2GB-4GB range */
372#define AAC_FLAGS_NO4GB (1 << 6) /* Can't access host mem >2GB */
373#define AAC_FLAGS_256FIBS (1 << 7) /* Can only do 256 commands */
374#define AAC_FLAGS_BROKEN_MEMMAP (1 << 8) /* Broken HostPhysMemPages */
375
376 u_int32_t supported_options;
377 int aac_max_fibs;
378 u_int32_t scsi_method_id;
379 TAILQ_HEAD(,aac_sim) aac_sim_tqh;
380};
381
382
383/*
384 * Public functions
385 */
386extern void aac_free(struct aac_softc *sc);
387extern int aac_attach(struct aac_softc *sc);
388extern int aac_detach(device_t dev);
389extern int aac_shutdown(device_t dev);
390extern int aac_suspend(device_t dev);
391extern int aac_resume(device_t dev);
392extern void aac_intr(void *arg);
393extern void aac_submit_bio(struct bio *bp);
394extern void aac_biodone(struct bio *bp);
395extern void aac_startio(struct aac_softc *sc);
396extern int aac_alloc_command(struct aac_softc *sc,
397 struct aac_command **cmp);
398extern void aac_release_command(struct aac_command *cm);
399extern int aac_alloc_sync_fib(struct aac_softc *sc,
400 struct aac_fib **fib, int flags);
401extern void aac_release_sync_fib(struct aac_softc *sc);
402extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command,
403 u_int32_t xferstate, struct aac_fib *fib,
404 u_int16_t datasize);
405
406/*
407 * Debugging levels:
408 * 0 - quiet, only emit warnings
409 * 1 - noisy, emit major function points and things done
410 * 2 - extremely noisy, emit trace items in loops, etc.
411 */
412#ifdef AAC_DEBUG
413# define debug(level, fmt, args...) \
414 do { \
415 if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
416 } while (0)
417# define debug_called(level) \
418 do { \
419 if (level <= AAC_DEBUG) printf("%s: called\n", __func__); \
420 } while (0)
421
422extern void aac_print_queues(struct aac_softc *sc);
423extern void aac_panic(struct aac_softc *sc, char *reason);
424extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
425 const char *caller);
426extern void aac_print_aif(struct aac_softc *sc,
427 struct aac_aif_command *aif);
428
429#define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__)
430
431#else
432# define debug(level, fmt, args...)
433# define debug_called(level)
434
435# define aac_print_queues(sc)
436# define aac_panic(sc, reason)
437
438# define AAC_PRINT_FIB(sc, fib)
439# define aac_print_aif(sc, aac_aif_command)
440#endif
441
442struct aac_code_lookup {
443 char *string;
444 u_int32_t code;
445};
446
447/*
448 * Queue primitives for driver queues.
449 */
450#define AACQ_ADD(sc, qname) \
451 do { \
452 struct aac_qstat *qs; \
453 \
454 qs = &(sc)->aac_qstat[qname]; \
455 \
456 qs->q_length++; \
457 if (qs->q_length > qs->q_max) \
458 qs->q_max = qs->q_length; \
459 } while (0)
460
461#define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length--
462#define AACQ_INIT(sc, qname) \
463 do { \
464 sc->aac_qstat[qname].q_length = 0; \
465 sc->aac_qstat[qname].q_max = 0; \
466 } while (0)
467
468
469#define AACQ_COMMAND_QUEUE(name, index) \
470static __inline void \
471aac_initq_ ## name (struct aac_softc *sc) \
472{ \
473 TAILQ_INIT(&sc->aac_ ## name); \
474 AACQ_INIT(sc, index); \
475} \
476static __inline void \
477aac_enqueue_ ## name (struct aac_command *cm) \
478{ \
479 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
480 printf("command %p is on another queue, flags = %#x\n", \
481 cm, cm->cm_flags); \
482 panic("command is on another queue"); \
483 } \
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); \
487} \
488static __inline void \
489aac_requeue_ ## name (struct aac_command *cm) \
490{ \
491 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
492 printf("command %p is on another queue, flags = %#x\n", \
493 cm, cm->cm_flags); \
494 panic("command is on another queue"); \
495 } \
496 TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \
497 cm->cm_flags |= AAC_ON_ ## index; \
498 AACQ_ADD(cm->cm_sc, index); \
499} \
500static __inline struct aac_command * \
501aac_dequeue_ ## name (struct aac_softc *sc) \
502{ \
503 struct aac_command *cm; \
504 \
505 if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \
506 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
507 printf("command %p not in queue, flags = %#x, " \
508 "bit = %#x\n", cm, cm->cm_flags, \
509 AAC_ON_ ## index); \
510 panic("command not in queue"); \
511 } \
512 TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \
513 cm->cm_flags &= ~AAC_ON_ ## index; \
514 AACQ_REMOVE(sc, index); \
515 } \
516 return(cm); \
517} \
518static __inline void \
519aac_remove_ ## name (struct aac_command *cm) \
520{ \
521 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
522 printf("command %p not in queue, flags = %#x, " \
523 "bit = %#x\n", cm, cm->cm_flags, \
524 AAC_ON_ ## index); \
525 panic("command not in queue"); \
526 } \
527 TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \
528 cm->cm_flags &= ~AAC_ON_ ## index; \
529 AACQ_REMOVE(cm->cm_sc, index); \
530} \
531struct hack
532
533AACQ_COMMAND_QUEUE(free, AACQ_FREE);
534AACQ_COMMAND_QUEUE(ready, AACQ_READY);
535AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
536
537/*
538 * outstanding bio queue
539 */
540static __inline void
541aac_initq_bio(struct aac_softc *sc)
542{
543 bioq_init(&sc->aac_bioq);
544 AACQ_INIT(sc, AACQ_BIO);
545}
546
547static __inline void
548aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
549{
550 bioq_insert_tail(&sc->aac_bioq, bp);
551 AACQ_ADD(sc, AACQ_BIO);
552}
553
554static __inline struct bio *
555aac_dequeue_bio(struct aac_softc *sc)
556{
557 struct bio *bp;
558
559 if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
560 bioq_remove(&sc->aac_bioq, bp);
561 AACQ_REMOVE(sc, AACQ_BIO);
562 }
563 return(bp);
564}
565
566static __inline void
567aac_print_printf(struct aac_softc *sc)
568{
569 /*
570 * XXX We have the ability to read the length of the printf string
571 * from out of the mailboxes.
572 */
573 device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
574 sc->aac_common->ac_printf);
575 AAC_QNOTIFY(sc, AAC_DB_PRINTF);
576}
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/dev/aac/aacvar.h 117363 2003-07-09 21:16:23Z scottl $
30 */
31
32#include <sys/bio.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/taskqueue.h>
36#include <sys/selinfo.h>
37#include <geom/geom_disk.h>
38
39
40/*
41 * Driver Parameter Definitions
42 */
43
44/*
45 * The firmware interface allows for a 16-bit s/g list length. We limit
46 * ourselves to a reasonable maximum and ensure alignment.
47 */
48#define AAC_MAXSGENTRIES 64 /* max S/G entries, limit 65535 */
49
50/*
51 * We allocate a small set of FIBs for the adapter to use to send us messages.
52 */
53#define AAC_ADAPTER_FIBS 8
54
55/*
56 * FIBs are allocated in page-size chunks and can grow up to the 512
57 * limit imposed by the hardware.
58 */
59#define AAC_FIB_COUNT (PAGE_SIZE/sizeof(struct aac_fib))
60#define AAC_PREALLOCATE_FIBS 128
61#define AAC_MAX_FIBS 512
62
63/*
64 * The controller reports status events in AIFs. We hang on to a number of
65 * these in order to pass them out to user-space management tools.
66 */
67#define AAC_AIFQ_LENGTH 64
68
69/*
70 * Firmware messages are passed in the printf buffer.
71 */
72#define AAC_PRINTF_BUFSIZE 256
73
74/*
75 * We wait this many seconds for the adapter to come ready if it is still
76 * booting
77 */
78#define AAC_BOOT_TIMEOUT (3 * 60)
79
80/*
81 * Timeout for immediate commands.
82 */
83#define AAC_IMMEDIATE_TIMEOUT 30 /* seconds */
84
85/*
86 * Timeout for normal commands
87 */
88#define AAC_CMD_TIMEOUT 30 /* seconds */
89
90/*
91 * Rate at which we periodically check for timed out commands and kick the
92 * controller.
93 */
94#define AAC_PERIODIC_INTERVAL 20 /* seconds */
95
96/*
97 * Per-container data structure
98 */
99struct aac_container
100{
101 struct aac_mntobj co_mntobj;
102 device_t co_disk;
103 int co_found;
104 TAILQ_ENTRY(aac_container) co_link;
105};
106
107/*
108 * Per-SIM data structure
109 */
110struct aac_sim
111{
112 device_t sim_dev;
113 int TargetsPerBus;
114 int BusNumber;
115 int InitiatorBusId;
116 struct aac_softc *aac_sc;
117 TAILQ_ENTRY(aac_sim) sim_link;
118};
119
120/*
121 * Per-disk structure
122 */
123struct aac_disk
124{
125 device_t ad_dev;
126 struct aac_softc *ad_controller;
127 struct aac_container *ad_container;
128 struct disk ad_disk;
129 int ad_flags;
130#define AAC_DISK_OPEN (1<<0)
131 int ad_cylinders;
132 int ad_heads;
133 int ad_sectors;
134 u_int32_t ad_size;
135 int unit;
136};
137
138/*
139 * Per-command control structure.
140 */
141struct aac_command
142{
143 TAILQ_ENTRY(aac_command) cm_link; /* list linkage */
144
145 struct aac_softc *cm_sc; /* controller that owns us */
146
147 struct aac_fib *cm_fib; /* FIB associated with this
148 * command */
149 u_int32_t cm_fibphys; /* bus address of the FIB */
150 struct bio *cm_data; /* pointer to data in kernel
151 * space */
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
155 * command */
156 int cm_flags;
157#define AAC_CMD_MAPPED (1<<0) /* command has had its data
158 * mapped */
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_MASK ((1<<5)|(1<<6)|(1<<7)|(1<<8))
170#define AAC_QUEUE_FRZN (1<<9) /* Freeze the processing of
171 * commands on the queue. */
172
173 void (* cm_complete)(struct aac_command *cm);
174 void *cm_private;
175 time_t cm_timestamp; /* command creation time */
176 int cm_queue;
177 int cm_index;
178};
179
180struct aac_fibmap {
181 TAILQ_ENTRY(aac_fibmap) fm_link; /* list linkage */
182 struct aac_fib *aac_fibs;
183 bus_dmamap_t aac_fibmap;
184 struct aac_command *aac_commands;
185};
186
187/*
188 * We gather a number of adapter-visible items into a single structure.
189 *
190 * The ordering of this strucure may be important; we copy the Linux driver:
191 *
192 * Adapter FIBs
193 * Init struct
194 * Queue headers (Comm Area)
195 * Printf buffer
196 *
197 * In addition, we add:
198 * Sync Fib
199 */
200struct aac_common {
201 /* fibs for the controller to send us messages */
202 struct aac_fib ac_fibs[AAC_ADAPTER_FIBS];
203
204 /* the init structure */
205 struct aac_adapter_init ac_init;
206
207 /* arena within which the queue structures are kept */
208 u_int8_t ac_qbuf[sizeof(struct aac_queue_table) +
209 AAC_QUEUE_ALIGN];
210
211 /* buffer for text messages from the controller */
212 char ac_printf[AAC_PRINTF_BUFSIZE];
213
214 /* fib for synchronous commands */
215 struct aac_fib ac_sync_fib;
216};
217
218/*
219 * Interface operations
220 */
221struct aac_interface
222{
223 int (*aif_get_fwstatus)(struct aac_softc *sc);
224 void (*aif_qnotify)(struct aac_softc *sc, int qbit);
225 int (*aif_get_istatus)(struct aac_softc *sc);
226 void (*aif_clr_istatus)(struct aac_softc *sc, int mask);
227 void (*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
228 u_int32_t arg0, u_int32_t arg1,
229 u_int32_t arg2, u_int32_t arg3);
230 int (*aif_get_mailbox)(struct aac_softc *sc, int mb);
231 void (*aif_set_interrupts)(struct aac_softc *sc, int enable);
232};
233extern struct aac_interface aac_rx_interface;
234extern struct aac_interface aac_sa_interface;
235extern struct aac_interface aac_fa_interface;
236
237#define AAC_GET_FWSTATUS(sc) ((sc)->aac_if.aif_get_fwstatus((sc)))
238#define AAC_QNOTIFY(sc, qbit) ((sc)->aac_if.aif_qnotify((sc), (qbit)))
239#define AAC_GET_ISTATUS(sc) ((sc)->aac_if.aif_get_istatus((sc)))
240#define AAC_CLEAR_ISTATUS(sc, mask) ((sc)->aac_if.aif_clr_istatus((sc), \
241 (mask)))
242#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
243 ((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
244 (arg3)))
245#define AAC_GET_MAILBOX(sc, mb) ((sc)->aac_if.aif_get_mailbox((sc), \
246 (mb)))
247#define AAC_MASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \
248 0))
249#define AAC_UNMASK_INTERRUPTS(sc) ((sc)->aac_if.aif_set_interrupts((sc), \
250 1))
251
252#define AAC_SETREG4(sc, reg, val) bus_space_write_4(sc->aac_btag, \
253 sc->aac_bhandle, reg, val)
254#define AAC_GETREG4(sc, reg) bus_space_read_4 (sc->aac_btag, \
255 sc->aac_bhandle, reg)
256#define AAC_SETREG2(sc, reg, val) bus_space_write_2(sc->aac_btag, \
257 sc->aac_bhandle, reg, val)
258#define AAC_GETREG2(sc, reg) bus_space_read_2 (sc->aac_btag, \
259 sc->aac_bhandle, reg)
260#define AAC_SETREG1(sc, reg, val) bus_space_write_1(sc->aac_btag, \
261 sc->aac_bhandle, reg, val)
262#define AAC_GETREG1(sc, reg) bus_space_read_1 (sc->aac_btag, \
263 sc->aac_bhandle, reg)
264
265/* Define the OS version specific locks */
266typedef struct mtx aac_lock_t;
267#define AAC_LOCK_INIT(l, s) mtx_init(l, s, NULL, MTX_DEF)
268#define AAC_LOCK_ACQUIRE(l) mtx_lock(l)
269#define AAC_LOCK_RELEASE(l) mtx_unlock(l)
270
271
272/*
273 * Per-controller structure.
274 */
275struct aac_softc
276{
277 /* bus connections */
278 device_t aac_dev;
279 struct resource *aac_regs_resource; /* register interface
280 * window */
281 int aac_regs_rid; /* resource ID */
282 bus_space_handle_t aac_bhandle; /* bus space handle */
283 bus_space_tag_t aac_btag; /* bus space tag */
284 bus_dma_tag_t aac_parent_dmat; /* parent DMA tag */
285 bus_dma_tag_t aac_buffer_dmat; /* data buffer/command
286 * DMA tag */
287 struct resource *aac_irq; /* interrupt */
288 int aac_irq_rid;
289 void *aac_intr; /* interrupt handle */
290 eventhandler_tag eh;
291
292 /* controller features, limits and status */
293 int aac_state;
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;
299
300 /* controller hardware interface */
301 int aac_hwif;
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
307 * DMA tag */
308 bus_dmamap_t aac_common_dmamap; /* common structure
309 * DMA map */
310 struct aac_common *aac_common;
311 u_int32_t aac_common_busaddr;
312 struct aac_interface aac_if;
313
314 /* command/fib resources */
315 bus_dma_tag_t aac_fib_dmat; /* DMA tag for allocing FIBs */
316 TAILQ_HEAD(,aac_fibmap) aac_fibmap_tqh;
317 uint total_fibs;
318 struct aac_command *aac_commands;
319
320 /* command management */
321 TAILQ_HEAD(,aac_command) aac_free; /* command structures
322 * available for reuse */
323 TAILQ_HEAD(,aac_command) aac_ready; /* commands on hold for
324 * controller resources */
325 TAILQ_HEAD(,aac_command) aac_busy;
326 struct bio_queue_head aac_bioq;
327 struct aac_queue_table *aac_queues;
328 struct aac_queue_entry *aac_qentries[AAC_QUEUE_COUNT];
329
330 struct aac_qstat aac_qstat[AACQ_COUNT]; /* queue statistics */
331
332 /* connected containters */
333 TAILQ_HEAD(,aac_container) aac_container_tqh;
334 aac_lock_t aac_container_lock;
335
336 /* Protect the sync fib */
337#define AAC_SYNC_LOCK_FORCE (1 << 0)
338 aac_lock_t aac_sync_lock;
339
340 aac_lock_t aac_io_lock;
341
342 /* delayed activity infrastructure */
343 struct task aac_task_complete; /* deferred-completion
344 * task */
345 struct intr_config_hook aac_ich;
346
347 /* management interface */
348 dev_t aac_dev_t;
349 aac_lock_t aac_aifq_lock;
350 struct aac_aif_command aac_aifq[AAC_AIFQ_LENGTH];
351 int aac_aifq_head;
352 int aac_aifq_tail;
353 struct selinfo rcv_select;
354 struct proc *aifthread;
355 int aifflags;
356#define AAC_AIFFLAGS_RUNNING (1 << 0)
357#define AAC_AIFFLAGS_AIF (1 << 1)
358#define AAC_AIFFLAGS_EXIT (1 << 2)
359#define AAC_AIFFLAGS_EXITED (1 << 3)
360#define AAC_AIFFLAGS_PRINTF (1 << 4)
361#define AAC_AIFFLAGS_ALLOCFIBS (1 << 5)
362#define AAC_AIFFLAGS_PENDING (AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
363 AAC_AIFFLAGS_ALLOCFIBS)
364 u_int32_t flags;
365#define AAC_FLAGS_PERC2QC (1 << 0)
366#define AAC_FLAGS_ENABLE_CAM (1 << 1) /* No SCSI passthrough */
367#define AAC_FLAGS_CAM_NORESET (1 << 2) /* Fake SCSI resets */
368#define AAC_FLAGS_CAM_PASSONLY (1 << 3) /* Only create pass devices */
369#define AAC_FLAGS_SG_64BIT (1 << 4) /* Use 64-bit S/G addresses */
370#define AAC_FLAGS_4GB_WINDOW (1 << 5) /* Device can access host mem
371 * 2GB-4GB range */
372#define AAC_FLAGS_NO4GB (1 << 6) /* Can't access host mem >2GB */
373#define AAC_FLAGS_256FIBS (1 << 7) /* Can only do 256 commands */
374#define AAC_FLAGS_BROKEN_MEMMAP (1 << 8) /* Broken HostPhysMemPages */
375
376 u_int32_t supported_options;
377 int aac_max_fibs;
378 u_int32_t scsi_method_id;
379 TAILQ_HEAD(,aac_sim) aac_sim_tqh;
380};
381
382
383/*
384 * Public functions
385 */
386extern void aac_free(struct aac_softc *sc);
387extern int aac_attach(struct aac_softc *sc);
388extern int aac_detach(device_t dev);
389extern int aac_shutdown(device_t dev);
390extern int aac_suspend(device_t dev);
391extern int aac_resume(device_t dev);
392extern void aac_intr(void *arg);
393extern void aac_submit_bio(struct bio *bp);
394extern void aac_biodone(struct bio *bp);
395extern void aac_startio(struct aac_softc *sc);
396extern int aac_alloc_command(struct aac_softc *sc,
397 struct aac_command **cmp);
398extern void aac_release_command(struct aac_command *cm);
399extern int aac_alloc_sync_fib(struct aac_softc *sc,
400 struct aac_fib **fib, int flags);
401extern void aac_release_sync_fib(struct aac_softc *sc);
402extern int aac_sync_fib(struct aac_softc *sc, u_int32_t command,
403 u_int32_t xferstate, struct aac_fib *fib,
404 u_int16_t datasize);
405
406/*
407 * Debugging levels:
408 * 0 - quiet, only emit warnings
409 * 1 - noisy, emit major function points and things done
410 * 2 - extremely noisy, emit trace items in loops, etc.
411 */
412#ifdef AAC_DEBUG
413# define debug(level, fmt, args...) \
414 do { \
415 if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
416 } while (0)
417# define debug_called(level) \
418 do { \
419 if (level <= AAC_DEBUG) printf("%s: called\n", __func__); \
420 } while (0)
421
422extern void aac_print_queues(struct aac_softc *sc);
423extern void aac_panic(struct aac_softc *sc, char *reason);
424extern void aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
425 const char *caller);
426extern void aac_print_aif(struct aac_softc *sc,
427 struct aac_aif_command *aif);
428
429#define AAC_PRINT_FIB(sc, fib) aac_print_fib(sc, fib, __func__)
430
431#else
432# define debug(level, fmt, args...)
433# define debug_called(level)
434
435# define aac_print_queues(sc)
436# define aac_panic(sc, reason)
437
438# define AAC_PRINT_FIB(sc, fib)
439# define aac_print_aif(sc, aac_aif_command)
440#endif
441
442struct aac_code_lookup {
443 char *string;
444 u_int32_t code;
445};
446
447/*
448 * Queue primitives for driver queues.
449 */
450#define AACQ_ADD(sc, qname) \
451 do { \
452 struct aac_qstat *qs; \
453 \
454 qs = &(sc)->aac_qstat[qname]; \
455 \
456 qs->q_length++; \
457 if (qs->q_length > qs->q_max) \
458 qs->q_max = qs->q_length; \
459 } while (0)
460
461#define AACQ_REMOVE(sc, qname) (sc)->aac_qstat[qname].q_length--
462#define AACQ_INIT(sc, qname) \
463 do { \
464 sc->aac_qstat[qname].q_length = 0; \
465 sc->aac_qstat[qname].q_max = 0; \
466 } while (0)
467
468
469#define AACQ_COMMAND_QUEUE(name, index) \
470static __inline void \
471aac_initq_ ## name (struct aac_softc *sc) \
472{ \
473 TAILQ_INIT(&sc->aac_ ## name); \
474 AACQ_INIT(sc, index); \
475} \
476static __inline void \
477aac_enqueue_ ## name (struct aac_command *cm) \
478{ \
479 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
480 printf("command %p is on another queue, flags = %#x\n", \
481 cm, cm->cm_flags); \
482 panic("command is on another queue"); \
483 } \
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); \
487} \
488static __inline void \
489aac_requeue_ ## name (struct aac_command *cm) \
490{ \
491 if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) { \
492 printf("command %p is on another queue, flags = %#x\n", \
493 cm, cm->cm_flags); \
494 panic("command is on another queue"); \
495 } \
496 TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link); \
497 cm->cm_flags |= AAC_ON_ ## index; \
498 AACQ_ADD(cm->cm_sc, index); \
499} \
500static __inline struct aac_command * \
501aac_dequeue_ ## name (struct aac_softc *sc) \
502{ \
503 struct aac_command *cm; \
504 \
505 if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) { \
506 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
507 printf("command %p not in queue, flags = %#x, " \
508 "bit = %#x\n", cm, cm->cm_flags, \
509 AAC_ON_ ## index); \
510 panic("command not in queue"); \
511 } \
512 TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link); \
513 cm->cm_flags &= ~AAC_ON_ ## index; \
514 AACQ_REMOVE(sc, index); \
515 } \
516 return(cm); \
517} \
518static __inline void \
519aac_remove_ ## name (struct aac_command *cm) \
520{ \
521 if ((cm->cm_flags & AAC_ON_ ## index) == 0) { \
522 printf("command %p not in queue, flags = %#x, " \
523 "bit = %#x\n", cm, cm->cm_flags, \
524 AAC_ON_ ## index); \
525 panic("command not in queue"); \
526 } \
527 TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link); \
528 cm->cm_flags &= ~AAC_ON_ ## index; \
529 AACQ_REMOVE(cm->cm_sc, index); \
530} \
531struct hack
532
533AACQ_COMMAND_QUEUE(free, AACQ_FREE);
534AACQ_COMMAND_QUEUE(ready, AACQ_READY);
535AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
536
537/*
538 * outstanding bio queue
539 */
540static __inline void
541aac_initq_bio(struct aac_softc *sc)
542{
543 bioq_init(&sc->aac_bioq);
544 AACQ_INIT(sc, AACQ_BIO);
545}
546
547static __inline void
548aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
549{
550 bioq_insert_tail(&sc->aac_bioq, bp);
551 AACQ_ADD(sc, AACQ_BIO);
552}
553
554static __inline struct bio *
555aac_dequeue_bio(struct aac_softc *sc)
556{
557 struct bio *bp;
558
559 if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
560 bioq_remove(&sc->aac_bioq, bp);
561 AACQ_REMOVE(sc, AACQ_BIO);
562 }
563 return(bp);
564}
565
566static __inline void
567aac_print_printf(struct aac_softc *sc)
568{
569 /*
570 * XXX We have the ability to read the length of the printf string
571 * from out of the mailboxes.
572 */
573 device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
574 sc->aac_common->ac_printf);
575 AAC_QNOTIFY(sc, AAC_DB_PRINTF);
576}