cissvar.h revision 197263
187011Smsmith/*-
287011Smsmith * Copyright (c) 2001 Michael Smith
387011Smsmith * All rights reserved.
487011Smsmith *
587011Smsmith * Redistribution and use in source and binary forms, with or without
687011Smsmith * modification, are permitted provided that the following conditions
787011Smsmith * are met:
887011Smsmith * 1. Redistributions of source code must retain the above copyright
987011Smsmith *    notice, this list of conditions and the following disclaimer.
1087011Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1187011Smsmith *    notice, this list of conditions and the following disclaimer in the
1287011Smsmith *    documentation and/or other materials provided with the distribution.
1387011Smsmith *
1487011Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1587011Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1687011Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1787011Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1887011Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1987011Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2087011Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2187011Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2287011Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2387011Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2487011Smsmith * SUCH DAMAGE.
2587011Smsmith *
2687011Smsmith *	$FreeBSD: head/sys/dev/ciss/cissvar.h 197263 2009-09-16 23:27:14Z scottl $
2787011Smsmith */
2887011Smsmith
2987011Smsmith/*
3087011Smsmith * CISS adapter driver datastructures
3187011Smsmith */
3287011Smsmith
33180454Sscottltypedef STAILQ_HEAD(, ciss_request)	cr_qhead_t;
34180454Sscottl
3587011Smsmith/************************************************************************
3687011Smsmith * Tunable parameters
3787011Smsmith */
3887011Smsmith
3987011Smsmith/*
4087011Smsmith * There is no guaranteed upper bound on the number of concurrent
4187011Smsmith * commands an adapter may claim to support.  Cap it at a reasonable
4287011Smsmith * value.
4387011Smsmith */
4487011Smsmith#define CISS_MAX_REQUESTS	256
4587011Smsmith
4687011Smsmith/*
4787011Smsmith * Maximum number of logical drives we support.
4887011Smsmith */
4987011Smsmith#define CISS_MAX_LOGICAL	15
5087011Smsmith
5187011Smsmith/*
52128337Sps * Maximum number of physical devices we support.
53128337Sps */
54128337Sps#define CISS_MAX_PHYSICAL	1024
55128337Sps
56128337Sps/*
5787011Smsmith * Interrupt reduction can be controlled by tuning the interrupt
5887011Smsmith * coalesce delay and count paramters.  The delay (in microseconds)
5987011Smsmith * defers delivery of interrupts to increase the chance of there being
6087011Smsmith * more than one completed command ready when the interrupt is
6187011Smsmith * delivered.  The count expedites the delivery of the interrupt when
6287011Smsmith * the given number of commands are ready.
6387011Smsmith *
6487011Smsmith * If the delay is set to 0, interrupts are delivered immediately.
6587011Smsmith */
66180454Sscottl#define CISS_INTERRUPT_COALESCE_DELAY	0
6787011Smsmith#define CISS_INTERRUPT_COALESCE_COUNT	16
6887011Smsmith
6987011Smsmith/*
7087011Smsmith * Heartbeat routine timeout in seconds.  Note that since event
7187011Smsmith * handling is performed on a callback basis, we don't need this to
7287011Smsmith * run very often.
7387011Smsmith */
7487011Smsmith#define CISS_HEARTBEAT_RATE		10
7587011Smsmith
7687011Smsmith/************************************************************************
7787187Smsmith * Compatibility with older versions of FreeBSD
7887187Smsmith */
7987187Smsmith#if __FreeBSD_version < 440001
8087187Smsmith#warning testing old-FreeBSD compat
8187187Smsmithtypedef struct proc	d_thread_t;
8287187Smsmith#endif
8387187Smsmith
8487187Smsmith/************************************************************************
8587011Smsmith * Driver version.  Only really significant to the ACU interface.
8687011Smsmith */
8787187Smsmith#define CISS_DRIVER_VERSION	20011201
8887011Smsmith
8987011Smsmith/************************************************************************
9087011Smsmith * Driver data structures
9187011Smsmith */
9287011Smsmith
9387011Smsmith/*
9487011Smsmith * Each command issued to the adapter is managed by a request
9587011Smsmith * structure.
9687011Smsmith */
9787011Smsmithstruct ciss_request
9887011Smsmith{
99180454Sscottl    STAILQ_ENTRY(ciss_request)	cr_link;
10087011Smsmith    int				cr_onq;		/* which queue we are on */
10187011Smsmith
10287011Smsmith    struct ciss_softc		*cr_sc;		/* controller softc */
10387011Smsmith    void			*cr_data;	/* data buffer */
10487011Smsmith    u_int32_t			cr_length;	/* data length */
10587011Smsmith    bus_dmamap_t		cr_datamap;	/* DMA map for data */
106197263Sscottl    struct ciss_command		*cr_cc;
107197263Sscottl    uint32_t			cr_ccphys;
10887011Smsmith    int				cr_tag;
10987011Smsmith    int				cr_flags;
11087011Smsmith#define CISS_REQ_MAPPED		(1<<0)		/* data mapped */
11187011Smsmith#define CISS_REQ_SLEEP		(1<<1)		/* submitter sleeping */
11287011Smsmith#define CISS_REQ_POLL		(1<<2)		/* submitter polling */
11387011Smsmith#define CISS_REQ_DATAOUT	(1<<3)		/* data host->adapter */
11487011Smsmith#define CISS_REQ_DATAIN		(1<<4)		/* data adapter->host */
115180454Sscottl#define CISS_REQ_BUSY		(1<<5)		/* controller has req */
116128334Sps
11787011Smsmith    void			(* cr_complete)(struct ciss_request *);
11887011Smsmith    void			*cr_private;
119180454Sscottl    int				cr_sg_tag;
120180454Sscottl#define CISS_SG_MAX		((CISS_SG_FETCH_MAX << 1) | 0x1)
121180454Sscottl#define CISS_SG_1		((CISS_SG_FETCH_1 << 1) | 0x01)
122180454Sscottl#define CISS_SG_2		((CISS_SG_FETCH_2 << 1) | 0x01)
123180454Sscottl#define CISS_SG_4		((CISS_SG_FETCH_4 << 1) | 0x01)
124180454Sscottl#define CISS_SG_8		((CISS_SG_FETCH_8 << 1) | 0x01)
125180454Sscottl#define CISS_SG_16		((CISS_SG_FETCH_16 << 1) | 0x01)
126180454Sscottl#define CISS_SG_32		((CISS_SG_FETCH_32 << 1) | 0x01)
127180454Sscottl#define CISS_SG_NONE		((CISS_SG_FETCH_NONE << 1) | 0x01)
12887011Smsmith};
12987011Smsmith
13087011Smsmith/*
13187011Smsmith * The adapter command structure is defined with a zero-length
13287011Smsmith * scatter/gather list size.  In practise, we want space for a
13387011Smsmith * scatter-gather list, and we also want to avoid having commands
13487011Smsmith * cross page boundaries.
13587011Smsmith *
136197263Sscottl * The size of the ciss_command is 52 bytes.  65 s/g elements are reserved
137197263Sscottl * to allow a max i/o size of 256k.  This gives a total command size of
138197263Sscottl * 1120 bytes, including the 32 byte alignment padding.  Modern controllers
139197263Sscottl * seem to saturate nicely at this value.
14087011Smsmith */
14187011Smsmith
142197263Sscottl#define CISS_MAX_SG_ELEMENTS	65
143197263Sscottl#define CISS_COMMAND_ALIGN	32
144197263Sscottl#define CISS_COMMAND_SG_LENGTH	(sizeof(struct ciss_sg_entry) * CISS_MAX_SG_ELEMENTS)
145197263Sscottl#define CISS_COMMAND_ALLOC_SIZE		(roundup2(sizeof(struct ciss_command) + CISS_COMMAND_SG_LENGTH, CISS_COMMAND_ALIGN))
14687011Smsmith
14787011Smsmith/*
14887011Smsmith * Per-logical-drive data.
14987011Smsmith */
150128334Spsstruct ciss_ldrive
15187011Smsmith{
15287011Smsmith    union ciss_device_address	cl_address;
153128337Sps    union ciss_device_address	*cl_controller;
15487011Smsmith    int				cl_status;
15587011Smsmith#define CISS_LD_NONEXISTENT	0
15687011Smsmith#define CISS_LD_ONLINE		1
15787011Smsmith#define CISS_LD_OFFLINE		2
15887011Smsmith
159128337Sps    int				cl_update;
160128337Sps
16187011Smsmith    struct ciss_bmic_id_ldrive	*cl_ldrive;
16287011Smsmith    struct ciss_bmic_id_lstatus	*cl_lstatus;
163110366Sps    struct ciss_ldrive_geometry	cl_geometry;
16487011Smsmith
16587011Smsmith    char			cl_name[16];		/* device name */
16687011Smsmith};
16787011Smsmith
16887011Smsmith/*
169130874Sscottl * Per-physical-drive data
170130874Sscottl */
171130874Sscottlstruct ciss_pdrive
172130874Sscottl{
173130874Sscottl    union ciss_device_address	cp_address;
174130874Sscottl    int				cp_online;
175130874Sscottl};
176130874Sscottl
177130874Sscottl#define CISS_PHYSICAL_SHIFT	5
178130874Sscottl#define CISS_PHYSICAL_BASE	(1 << CISS_PHYSICAL_SHIFT)
179197260Sscottl#define CISS_MAX_PHYSTGT	256
180130874Sscottl
181130874Sscottl#define CISS_IS_PHYSICAL(bus)	(bus >= CISS_PHYSICAL_BASE)
182130874Sscottl#define CISS_CAM_TO_PBUS(bus)	(bus - CISS_PHYSICAL_BASE)
183130874Sscottl
184130874Sscottl/*
18587011Smsmith * Per-adapter data
18687011Smsmith */
187128334Spsstruct ciss_softc
18887011Smsmith{
18987011Smsmith    /* bus connections */
19087011Smsmith    device_t			ciss_dev;		/* bus attachment */
191130585Sphk    struct cdev *ciss_dev_t;		/* control device */
19287011Smsmith
19387011Smsmith    struct resource		*ciss_regs_resource;	/* register interface window */
19487011Smsmith    int				ciss_regs_rid;		/* resource ID */
19587011Smsmith    bus_space_handle_t		ciss_regs_bhandle;	/* bus space handle */
19687011Smsmith    bus_space_tag_t		ciss_regs_btag;		/* bus space tag */
19787011Smsmith
19887011Smsmith    struct resource		*ciss_cfg_resource;	/* config struct interface window */
19987011Smsmith    int				ciss_cfg_rid;		/* resource ID */
20087011Smsmith    struct ciss_config_table	*ciss_cfg;		/* config table in adapter memory */
201180454Sscottl    struct ciss_perf_config	*ciss_perf;		/* config table for the performant */
20287011Smsmith    struct ciss_bmic_id_table	*ciss_id;		/* ID table in host memory */
20387011Smsmith    u_int32_t			ciss_heartbeat;		/* last heartbeat value */
20487011Smsmith    int				ciss_heart_attack;	/* number of times we have seen this value */
20587011Smsmith
206180454Sscottl    int				ciss_msi;
20787011Smsmith    struct resource		*ciss_irq_resource;	/* interrupt */
208180454Sscottl    int				ciss_irq_rid[CISS_MSI_COUNT];		/* resource ID */
20987011Smsmith    void			*ciss_intr;		/* interrupt handle */
21087011Smsmith
21187011Smsmith    bus_dma_tag_t		ciss_parent_dmat;	/* parent DMA tag */
21287011Smsmith    bus_dma_tag_t		ciss_buffer_dmat;	/* data buffer/command DMA tag */
21387011Smsmith
21487011Smsmith    u_int32_t			ciss_interrupt_mask;	/* controller interrupt mask bits */
21587011Smsmith
216180454Sscottl    uint64_t			*ciss_reply;
217180454Sscottl    int				ciss_cycle;
218180454Sscottl    int				ciss_rqidx;
219180454Sscottl    bus_dma_tag_t		ciss_reply_dmat;
220180454Sscottl    bus_dmamap_t		ciss_reply_map;
221180454Sscottl    uint32_t			ciss_reply_phys;
222180454Sscottl
22387011Smsmith    int				ciss_max_requests;
22487011Smsmith    struct ciss_request		ciss_request[CISS_MAX_REQUESTS];	/* requests */
22587011Smsmith    void			*ciss_command;		/* command structures */
22687011Smsmith    bus_dma_tag_t		ciss_command_dmat;	/* command DMA tag */
22787011Smsmith    bus_dmamap_t		ciss_command_map;	/* command DMA map */
22887011Smsmith    u_int32_t			ciss_command_phys;	/* command array base address */
229180454Sscottl    cr_qhead_t			ciss_free;		/* requests available for reuse */
230180454Sscottl    cr_qhead_t			ciss_notify;		/* requests which are defered for processing */
231128337Sps    struct proc			*ciss_notify_thread;
23287011Smsmith
233169160Sscottl    struct callout		ciss_periodic;		/* periodic event handling */
23487011Smsmith    struct ciss_request		*ciss_periodic_notify;	/* notify callback request */
23587011Smsmith
236169160Sscottl    struct mtx			ciss_mtx;
237128337Sps    struct ciss_ldrive		**ciss_logical;
238130874Sscottl    struct ciss_pdrive		**ciss_physical;
239128337Sps    union ciss_device_address	*ciss_controllers;	/* controller address */
240128337Sps    int				ciss_max_bus_number;	/* maximum bus number */
241130874Sscottl    int				ciss_max_logical_bus;
242130874Sscottl    int				ciss_max_physical_bus;
24387011Smsmith
24487011Smsmith    struct cam_devq		*ciss_cam_devq;
245128337Sps    struct cam_sim		**ciss_cam_sim;
24687011Smsmith
247145258Sps    int				ciss_soft_reset;
248145258Sps
24987011Smsmith    int				ciss_flags;
25087011Smsmith#define CISS_FLAG_NOTIFY_OK	(1<<0)		/* notify command running OK */
25187011Smsmith#define CISS_FLAG_CONTROL_OPEN	(1<<1)		/* control device is open */
25287011Smsmith#define CISS_FLAG_ABORTING	(1<<2)		/* driver is going away */
25387011Smsmith#define CISS_FLAG_RUNNING	(1<<3)		/* driver is running (interrupts usable) */
25487011Smsmith
25587011Smsmith#define CISS_FLAG_FAKE_SYNCH	(1<<16)		/* needs SYNCHRONISE_CACHE faked */
25687011Smsmith#define CISS_FLAG_BMIC_ABORT	(1<<17)		/* use BMIC command to abort Notify on Event */
257128337Sps#define CISS_FLAG_THREAD_SHUT	(1<<20)		/* shutdown the kthread */
25887011Smsmith
25987011Smsmith    struct ciss_qstat		ciss_qstat[CISSQ_COUNT];	/* queue statistics */
26087011Smsmith};
26187011Smsmith
26287011Smsmith/************************************************************************
26387011Smsmith * Debugging/diagnostic output.
26487011Smsmith */
26587011Smsmith
26687011Smsmith/*
26787011Smsmith * Debugging levels:
26887011Smsmith *  0 - quiet, only emit warnings
26987011Smsmith *  1 - talkative, log major events, but nothing on the I/O path
27087011Smsmith *  2 - noisy, log events on the I/O path
27187011Smsmith *  3 - extremely noisy, log items in loops
27287011Smsmith */
27387011Smsmith#ifdef CISS_DEBUG
27487011Smsmith# define debug(level, fmt, args...)							\
27587011Smsmith	do {										\
27687599Sobrien	    if (level <= CISS_DEBUG) printf("%s: " fmt "\n", __func__ , ##args);	\
27787011Smsmith	} while(0)
27887011Smsmith# define debug_called(level)						\
27987011Smsmith	do {								\
28087599Sobrien	    if (level <= CISS_DEBUG) printf("%s: called\n", __func__);	\
28187011Smsmith	} while(0)
282106039Smux# define debug_struct(s)		printf("  SIZE %s: %zu\n", #s, sizeof(struct s))
283106039Smux# define debug_union(s)			printf("  SIZE %s: %zu\n", #s, sizeof(union s))
284106039Smux# define debug_type(s)			printf("  SIZE %s: %zu\n", #s, sizeof(s))
28587011Smsmith# define debug_field(s, f)		printf("  OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f)))
286106039Smux# define debug_const(c)			printf("  CONST %s %jd/0x%jx\n", #c, (intmax_t)c, (intmax_t)c);
28787011Smsmith#else
28887011Smsmith# define debug(level, fmt, args...)
28987011Smsmith# define debug_called(level)
29087011Smsmith# define debug_struct(s)
29187011Smsmith# define debug_union(s)
29287011Smsmith# define debug_type(s)
29387011Smsmith# define debug_field(s, f)
29487011Smsmith# define debug_const(c)
29587011Smsmith#endif
29687011Smsmith
29787011Smsmith#define ciss_printf(sc, fmt, args...)	device_printf(sc->ciss_dev, fmt , ##args)
29887011Smsmith
29987011Smsmith/************************************************************************
30087011Smsmith * Queue primitives
30187011Smsmith */
30287011Smsmith
30387011Smsmith#define CISSQ_ADD(sc, qname)					\
30487011Smsmith	do {							\
30587011Smsmith	    struct ciss_qstat *qs = &(sc)->ciss_qstat[qname];	\
30687011Smsmith								\
30787011Smsmith	    qs->q_length++;					\
30887011Smsmith	    if (qs->q_length > qs->q_max)			\
30987011Smsmith		qs->q_max = qs->q_length;			\
31087011Smsmith	} while(0)
31187011Smsmith
31287011Smsmith#define CISSQ_REMOVE(sc, qname)    (sc)->ciss_qstat[qname].q_length--
31387011Smsmith#define CISSQ_INIT(sc, qname)			\
31487011Smsmith	do {					\
31587011Smsmith	    sc->ciss_qstat[qname].q_length = 0;	\
31687011Smsmith	    sc->ciss_qstat[qname].q_max = 0;	\
31787011Smsmith	} while(0)
31887011Smsmith
31987011Smsmith
32087011Smsmith#define CISSQ_REQUEST_QUEUE(name, index)				\
32187011Smsmithstatic __inline void							\
32287011Smsmithciss_initq_ ## name (struct ciss_softc *sc)				\
32387011Smsmith{									\
324180454Sscottl    STAILQ_INIT(&sc->ciss_ ## name);					\
32587011Smsmith    CISSQ_INIT(sc, index);						\
32687011Smsmith}									\
32787011Smsmithstatic __inline void							\
32887011Smsmithciss_enqueue_ ## name (struct ciss_request *cr)				\
32987011Smsmith{									\
33087011Smsmith									\
331180454Sscottl    STAILQ_INSERT_TAIL(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
33287011Smsmith    CISSQ_ADD(cr->cr_sc, index);					\
33387011Smsmith    cr->cr_onq = index;							\
33487011Smsmith}									\
33587011Smsmithstatic __inline void							\
33687011Smsmithciss_requeue_ ## name (struct ciss_request *cr)				\
33787011Smsmith{									\
33887011Smsmith									\
339180454Sscottl    STAILQ_INSERT_HEAD(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
34087011Smsmith    CISSQ_ADD(cr->cr_sc, index);					\
34187011Smsmith    cr->cr_onq = index;							\
34287011Smsmith}									\
34387011Smsmithstatic __inline struct ciss_request *					\
34487011Smsmithciss_dequeue_ ## name (struct ciss_softc *sc)				\
34587011Smsmith{									\
34687011Smsmith    struct ciss_request	*cr;						\
34787011Smsmith									\
348180454Sscottl    if ((cr = STAILQ_FIRST(&sc->ciss_ ## name)) != NULL) {		\
349180454Sscottl	STAILQ_REMOVE_HEAD(&sc->ciss_ ## name, cr_link);		\
35087011Smsmith	CISSQ_REMOVE(sc, index);					\
35187011Smsmith	cr->cr_onq = -1;						\
35287011Smsmith    }									\
35387011Smsmith    return(cr);								\
35487011Smsmith}									\
35587011Smsmithstruct hack
35687011Smsmith
35787011SmsmithCISSQ_REQUEST_QUEUE(free, CISSQ_FREE);
358128337SpsCISSQ_REQUEST_QUEUE(notify, CISSQ_NOTIFY);
35987011Smsmith
360180454Sscottlstatic __inline void
361180454Sscottlciss_enqueue_complete(struct ciss_request *ac, cr_qhead_t *head)
362180454Sscottl{
363180454Sscottl
364180454Sscottl    STAILQ_INSERT_TAIL(head, ac, cr_link);
365180454Sscottl}
366180454Sscottl
367180454Sscottlstatic __inline struct ciss_request *
368180454Sscottlciss_dequeue_complete(struct ciss_softc *sc, cr_qhead_t *head)
369180454Sscottl{
370180454Sscottl    struct ciss_request  *ac;
371180454Sscottl
372180454Sscottl    if ((ac = STAILQ_FIRST(head)) != NULL)
373180454Sscottl        STAILQ_REMOVE_HEAD(head, cr_link);
374180454Sscottl    return(ac);
375180454Sscottl}
376180454Sscottl
37787011Smsmith/********************************************************************************
37887011Smsmith * space-fill a character string
37987011Smsmith */
38087011Smsmithstatic __inline void
38187011Smsmithpadstr(char *targ, const char *src, int len)
38287011Smsmith{
38387011Smsmith    while (len-- > 0) {
38487011Smsmith	if (*src != 0) {
38587011Smsmith	    *targ++ = *src++;
38687011Smsmith	} else {
38787011Smsmith	    *targ++ = ' ';
38887011Smsmith	}
38987011Smsmith    }
39087011Smsmith}
391180454Sscottl
392180454Sscottl#define ciss_report_request(a, b, c)	\
393180454Sscottl	_ciss_report_request(a, b, c, __FUNCTION__)
394