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$
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 */
44204648Smav#define CISS_MAX_REQUESTS	1024
4587011Smsmith
4687011Smsmith/*
4787011Smsmith * Maximum number of logical drives we support.
48245459Ssbruno * If the controller does not indicate a maximum
49245459Ssbruno * value.  This is a compatibiliy value to support
50245459Ssbruno * older ciss controllers (e.g. model 6i)
5187011Smsmith */
52245459Ssbruno#define CISS_MAX_LOGICAL	16
5387011Smsmith
5487011Smsmith/*
55128337Sps * Maximum number of physical devices we support.
56128337Sps */
57128337Sps#define CISS_MAX_PHYSICAL	1024
58128337Sps
59128337Sps/*
6087011Smsmith * Interrupt reduction can be controlled by tuning the interrupt
6187011Smsmith * coalesce delay and count paramters.  The delay (in microseconds)
6287011Smsmith * defers delivery of interrupts to increase the chance of there being
6387011Smsmith * more than one completed command ready when the interrupt is
6487011Smsmith * delivered.  The count expedites the delivery of the interrupt when
6587011Smsmith * the given number of commands are ready.
6687011Smsmith *
6787011Smsmith * If the delay is set to 0, interrupts are delivered immediately.
6887011Smsmith */
69180454Sscottl#define CISS_INTERRUPT_COALESCE_DELAY	0
7087011Smsmith#define CISS_INTERRUPT_COALESCE_COUNT	16
7187011Smsmith
7287011Smsmith/*
7387011Smsmith * Heartbeat routine timeout in seconds.  Note that since event
7487011Smsmith * handling is performed on a callback basis, we don't need this to
7587011Smsmith * run very often.
7687011Smsmith */
7787011Smsmith#define CISS_HEARTBEAT_RATE		10
7887011Smsmith
7987011Smsmith/************************************************************************
8087187Smsmith * Compatibility with older versions of FreeBSD
8187187Smsmith */
8287187Smsmith#if __FreeBSD_version < 440001
8387187Smsmith#warning testing old-FreeBSD compat
8487187Smsmithtypedef struct proc	d_thread_t;
8587187Smsmith#endif
8687187Smsmith
8787187Smsmith/************************************************************************
8887011Smsmith * Driver version.  Only really significant to the ACU interface.
8987011Smsmith */
9087187Smsmith#define CISS_DRIVER_VERSION	20011201
9187011Smsmith
9287011Smsmith/************************************************************************
9387011Smsmith * Driver data structures
9487011Smsmith */
9587011Smsmith
9687011Smsmith/*
9787011Smsmith * Each command issued to the adapter is managed by a request
9887011Smsmith * structure.
9987011Smsmith */
10087011Smsmithstruct ciss_request
10187011Smsmith{
102180454Sscottl    STAILQ_ENTRY(ciss_request)	cr_link;
10387011Smsmith    int				cr_onq;		/* which queue we are on */
10487011Smsmith
10587011Smsmith    struct ciss_softc		*cr_sc;		/* controller softc */
10687011Smsmith    void			*cr_data;	/* data buffer */
10787011Smsmith    u_int32_t			cr_length;	/* data length */
10887011Smsmith    bus_dmamap_t		cr_datamap;	/* DMA map for data */
109197263Sscottl    struct ciss_command		*cr_cc;
110197263Sscottl    uint32_t			cr_ccphys;
11187011Smsmith    int				cr_tag;
11287011Smsmith    int				cr_flags;
11387011Smsmith#define CISS_REQ_MAPPED		(1<<0)		/* data mapped */
11487011Smsmith#define CISS_REQ_SLEEP		(1<<1)		/* submitter sleeping */
11587011Smsmith#define CISS_REQ_POLL		(1<<2)		/* submitter polling */
11687011Smsmith#define CISS_REQ_DATAOUT	(1<<3)		/* data host->adapter */
11787011Smsmith#define CISS_REQ_DATAIN		(1<<4)		/* data adapter->host */
118180454Sscottl#define CISS_REQ_BUSY		(1<<5)		/* controller has req */
119246713Skib#define CISS_REQ_CCB		(1<<6)		/* data is ccb */
120128334Sps
12187011Smsmith    void			(* cr_complete)(struct ciss_request *);
12287011Smsmith    void			*cr_private;
123180454Sscottl    int				cr_sg_tag;
124180454Sscottl#define CISS_SG_MAX		((CISS_SG_FETCH_MAX << 1) | 0x1)
125180454Sscottl#define CISS_SG_1		((CISS_SG_FETCH_1 << 1) | 0x01)
126180454Sscottl#define CISS_SG_2		((CISS_SG_FETCH_2 << 1) | 0x01)
127180454Sscottl#define CISS_SG_4		((CISS_SG_FETCH_4 << 1) | 0x01)
128180454Sscottl#define CISS_SG_8		((CISS_SG_FETCH_8 << 1) | 0x01)
129180454Sscottl#define CISS_SG_16		((CISS_SG_FETCH_16 << 1) | 0x01)
130180454Sscottl#define CISS_SG_32		((CISS_SG_FETCH_32 << 1) | 0x01)
131180454Sscottl#define CISS_SG_NONE		((CISS_SG_FETCH_NONE << 1) | 0x01)
13287011Smsmith};
13387011Smsmith
13487011Smsmith/*
13587011Smsmith * The adapter command structure is defined with a zero-length
13687011Smsmith * scatter/gather list size.  In practise, we want space for a
13787011Smsmith * scatter-gather list, and we also want to avoid having commands
13887011Smsmith * cross page boundaries.
13987011Smsmith *
140197263Sscottl * The size of the ciss_command is 52 bytes.  65 s/g elements are reserved
141197263Sscottl * to allow a max i/o size of 256k.  This gives a total command size of
142197263Sscottl * 1120 bytes, including the 32 byte alignment padding.  Modern controllers
143197263Sscottl * seem to saturate nicely at this value.
14487011Smsmith */
14587011Smsmith
146197263Sscottl#define CISS_MAX_SG_ELEMENTS	65
147197263Sscottl#define CISS_COMMAND_ALIGN	32
148197263Sscottl#define CISS_COMMAND_SG_LENGTH	(sizeof(struct ciss_sg_entry) * CISS_MAX_SG_ELEMENTS)
149197263Sscottl#define CISS_COMMAND_ALLOC_SIZE		(roundup2(sizeof(struct ciss_command) + CISS_COMMAND_SG_LENGTH, CISS_COMMAND_ALIGN))
15087011Smsmith
15187011Smsmith/*
15287011Smsmith * Per-logical-drive data.
15387011Smsmith */
154128334Spsstruct ciss_ldrive
15587011Smsmith{
15687011Smsmith    union ciss_device_address	cl_address;
157128337Sps    union ciss_device_address	*cl_controller;
15887011Smsmith    int				cl_status;
15987011Smsmith#define CISS_LD_NONEXISTENT	0
16087011Smsmith#define CISS_LD_ONLINE		1
16187011Smsmith#define CISS_LD_OFFLINE		2
16287011Smsmith
163128337Sps    int				cl_update;
164128337Sps
16587011Smsmith    struct ciss_bmic_id_ldrive	*cl_ldrive;
16687011Smsmith    struct ciss_bmic_id_lstatus	*cl_lstatus;
167110366Sps    struct ciss_ldrive_geometry	cl_geometry;
16887011Smsmith
16987011Smsmith    char			cl_name[16];		/* device name */
17087011Smsmith};
17187011Smsmith
17287011Smsmith/*
173130874Sscottl * Per-physical-drive data
174130874Sscottl */
175130874Sscottlstruct ciss_pdrive
176130874Sscottl{
177130874Sscottl    union ciss_device_address	cp_address;
178130874Sscottl    int				cp_online;
179130874Sscottl};
180130874Sscottl
181130874Sscottl#define CISS_PHYSICAL_SHIFT	5
182130874Sscottl#define CISS_PHYSICAL_BASE	(1 << CISS_PHYSICAL_SHIFT)
183197260Sscottl#define CISS_MAX_PHYSTGT	256
184130874Sscottl
185130874Sscottl#define CISS_IS_PHYSICAL(bus)	(bus >= CISS_PHYSICAL_BASE)
186130874Sscottl#define CISS_CAM_TO_PBUS(bus)	(bus - CISS_PHYSICAL_BASE)
187130874Sscottl
188130874Sscottl/*
18987011Smsmith * Per-adapter data
19087011Smsmith */
191128334Spsstruct ciss_softc
19287011Smsmith{
19387011Smsmith    /* bus connections */
19487011Smsmith    device_t			ciss_dev;		/* bus attachment */
195130585Sphk    struct cdev *ciss_dev_t;		/* control device */
19687011Smsmith
19787011Smsmith    struct resource		*ciss_regs_resource;	/* register interface window */
19887011Smsmith    int				ciss_regs_rid;		/* resource ID */
19987011Smsmith    bus_space_handle_t		ciss_regs_bhandle;	/* bus space handle */
20087011Smsmith    bus_space_tag_t		ciss_regs_btag;		/* bus space tag */
20187011Smsmith
20287011Smsmith    struct resource		*ciss_cfg_resource;	/* config struct interface window */
20387011Smsmith    int				ciss_cfg_rid;		/* resource ID */
20487011Smsmith    struct ciss_config_table	*ciss_cfg;		/* config table in adapter memory */
205180454Sscottl    struct ciss_perf_config	*ciss_perf;		/* config table for the performant */
20687011Smsmith    struct ciss_bmic_id_table	*ciss_id;		/* ID table in host memory */
20787011Smsmith    u_int32_t			ciss_heartbeat;		/* last heartbeat value */
20887011Smsmith    int				ciss_heart_attack;	/* number of times we have seen this value */
20987011Smsmith
210180454Sscottl    int				ciss_msi;
21187011Smsmith    struct resource		*ciss_irq_resource;	/* interrupt */
212180454Sscottl    int				ciss_irq_rid[CISS_MSI_COUNT];		/* resource ID */
21387011Smsmith    void			*ciss_intr;		/* interrupt handle */
21487011Smsmith
21587011Smsmith    bus_dma_tag_t		ciss_parent_dmat;	/* parent DMA tag */
21687011Smsmith    bus_dma_tag_t		ciss_buffer_dmat;	/* data buffer/command DMA tag */
21787011Smsmith
21887011Smsmith    u_int32_t			ciss_interrupt_mask;	/* controller interrupt mask bits */
21987011Smsmith
220180454Sscottl    uint64_t			*ciss_reply;
221180454Sscottl    int				ciss_cycle;
222180454Sscottl    int				ciss_rqidx;
223180454Sscottl    bus_dma_tag_t		ciss_reply_dmat;
224180454Sscottl    bus_dmamap_t		ciss_reply_map;
225180454Sscottl    uint32_t			ciss_reply_phys;
226180454Sscottl
22787011Smsmith    int				ciss_max_requests;
22887011Smsmith    struct ciss_request		ciss_request[CISS_MAX_REQUESTS];	/* requests */
22987011Smsmith    void			*ciss_command;		/* command structures */
23087011Smsmith    bus_dma_tag_t		ciss_command_dmat;	/* command DMA tag */
23187011Smsmith    bus_dmamap_t		ciss_command_map;	/* command DMA map */
23287011Smsmith    u_int32_t			ciss_command_phys;	/* command array base address */
233180454Sscottl    cr_qhead_t			ciss_free;		/* requests available for reuse */
234180454Sscottl    cr_qhead_t			ciss_notify;		/* requests which are defered for processing */
235128337Sps    struct proc			*ciss_notify_thread;
23687011Smsmith
237169160Sscottl    struct callout		ciss_periodic;		/* periodic event handling */
23887011Smsmith    struct ciss_request		*ciss_periodic_notify;	/* notify callback request */
23987011Smsmith
240169160Sscottl    struct mtx			ciss_mtx;
241128337Sps    struct ciss_ldrive		**ciss_logical;
242130874Sscottl    struct ciss_pdrive		**ciss_physical;
243128337Sps    union ciss_device_address	*ciss_controllers;	/* controller address */
244128337Sps    int				ciss_max_bus_number;	/* maximum bus number */
245130874Sscottl    int				ciss_max_logical_bus;
246130874Sscottl    int				ciss_max_physical_bus;
24787011Smsmith
24887011Smsmith    struct cam_devq		*ciss_cam_devq;
249128337Sps    struct cam_sim		**ciss_cam_sim;
25087011Smsmith
251145258Sps    int				ciss_soft_reset;
252145258Sps
25387011Smsmith    int				ciss_flags;
25487011Smsmith#define CISS_FLAG_NOTIFY_OK	(1<<0)		/* notify command running OK */
25587011Smsmith#define CISS_FLAG_CONTROL_OPEN	(1<<1)		/* control device is open */
25687011Smsmith#define CISS_FLAG_ABORTING	(1<<2)		/* driver is going away */
25787011Smsmith#define CISS_FLAG_RUNNING	(1<<3)		/* driver is running (interrupts usable) */
258204648Smav#define CISS_FLAG_BUSY		(1<<4)		/* no free commands */
25987011Smsmith
26087011Smsmith#define CISS_FLAG_FAKE_SYNCH	(1<<16)		/* needs SYNCHRONISE_CACHE faked */
26187011Smsmith#define CISS_FLAG_BMIC_ABORT	(1<<17)		/* use BMIC command to abort Notify on Event */
262128337Sps#define CISS_FLAG_THREAD_SHUT	(1<<20)		/* shutdown the kthread */
26387011Smsmith
26487011Smsmith    struct ciss_qstat		ciss_qstat[CISSQ_COUNT];	/* queue statistics */
26587011Smsmith};
26687011Smsmith
26787011Smsmith/************************************************************************
26887011Smsmith * Debugging/diagnostic output.
26987011Smsmith */
27087011Smsmith
27187011Smsmith/*
27287011Smsmith * Debugging levels:
27387011Smsmith *  0 - quiet, only emit warnings
27487011Smsmith *  1 - talkative, log major events, but nothing on the I/O path
27587011Smsmith *  2 - noisy, log events on the I/O path
27687011Smsmith *  3 - extremely noisy, log items in loops
27787011Smsmith */
27887011Smsmith#ifdef CISS_DEBUG
27987011Smsmith# define debug(level, fmt, args...)							\
28087011Smsmith	do {										\
28187599Sobrien	    if (level <= CISS_DEBUG) printf("%s: " fmt "\n", __func__ , ##args);	\
28287011Smsmith	} while(0)
28387011Smsmith# define debug_called(level)						\
28487011Smsmith	do {								\
28587599Sobrien	    if (level <= CISS_DEBUG) printf("%s: called\n", __func__);	\
28687011Smsmith	} while(0)
287106039Smux# define debug_struct(s)		printf("  SIZE %s: %zu\n", #s, sizeof(struct s))
288106039Smux# define debug_union(s)			printf("  SIZE %s: %zu\n", #s, sizeof(union s))
289106039Smux# define debug_type(s)			printf("  SIZE %s: %zu\n", #s, sizeof(s))
29087011Smsmith# define debug_field(s, f)		printf("  OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f)))
291106039Smux# define debug_const(c)			printf("  CONST %s %jd/0x%jx\n", #c, (intmax_t)c, (intmax_t)c);
29287011Smsmith#else
29387011Smsmith# define debug(level, fmt, args...)
29487011Smsmith# define debug_called(level)
29587011Smsmith# define debug_struct(s)
29687011Smsmith# define debug_union(s)
29787011Smsmith# define debug_type(s)
29887011Smsmith# define debug_field(s, f)
29987011Smsmith# define debug_const(c)
30087011Smsmith#endif
30187011Smsmith
30287011Smsmith#define ciss_printf(sc, fmt, args...)	device_printf(sc->ciss_dev, fmt , ##args)
30387011Smsmith
30487011Smsmith/************************************************************************
30587011Smsmith * Queue primitives
30687011Smsmith */
30787011Smsmith
30887011Smsmith#define CISSQ_ADD(sc, qname)					\
30987011Smsmith	do {							\
31087011Smsmith	    struct ciss_qstat *qs = &(sc)->ciss_qstat[qname];	\
31187011Smsmith								\
31287011Smsmith	    qs->q_length++;					\
31387011Smsmith	    if (qs->q_length > qs->q_max)			\
31487011Smsmith		qs->q_max = qs->q_length;			\
31587011Smsmith	} while(0)
31687011Smsmith
31787011Smsmith#define CISSQ_REMOVE(sc, qname)    (sc)->ciss_qstat[qname].q_length--
31887011Smsmith#define CISSQ_INIT(sc, qname)			\
31987011Smsmith	do {					\
32087011Smsmith	    sc->ciss_qstat[qname].q_length = 0;	\
32187011Smsmith	    sc->ciss_qstat[qname].q_max = 0;	\
32287011Smsmith	} while(0)
32387011Smsmith
32487011Smsmith
32587011Smsmith#define CISSQ_REQUEST_QUEUE(name, index)				\
32687011Smsmithstatic __inline void							\
32787011Smsmithciss_initq_ ## name (struct ciss_softc *sc)				\
32887011Smsmith{									\
329180454Sscottl    STAILQ_INIT(&sc->ciss_ ## name);					\
33087011Smsmith    CISSQ_INIT(sc, index);						\
33187011Smsmith}									\
33287011Smsmithstatic __inline void							\
33387011Smsmithciss_enqueue_ ## name (struct ciss_request *cr)				\
33487011Smsmith{									\
33587011Smsmith									\
336180454Sscottl    STAILQ_INSERT_TAIL(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
33787011Smsmith    CISSQ_ADD(cr->cr_sc, index);					\
33887011Smsmith    cr->cr_onq = index;							\
33987011Smsmith}									\
34087011Smsmithstatic __inline void							\
34187011Smsmithciss_requeue_ ## name (struct ciss_request *cr)				\
34287011Smsmith{									\
34387011Smsmith									\
344180454Sscottl    STAILQ_INSERT_HEAD(&cr->cr_sc->ciss_ ## name, cr, cr_link);		\
34587011Smsmith    CISSQ_ADD(cr->cr_sc, index);					\
34687011Smsmith    cr->cr_onq = index;							\
34787011Smsmith}									\
34887011Smsmithstatic __inline struct ciss_request *					\
34987011Smsmithciss_dequeue_ ## name (struct ciss_softc *sc)				\
35087011Smsmith{									\
35187011Smsmith    struct ciss_request	*cr;						\
35287011Smsmith									\
353180454Sscottl    if ((cr = STAILQ_FIRST(&sc->ciss_ ## name)) != NULL) {		\
354180454Sscottl	STAILQ_REMOVE_HEAD(&sc->ciss_ ## name, cr_link);		\
35587011Smsmith	CISSQ_REMOVE(sc, index);					\
35687011Smsmith	cr->cr_onq = -1;						\
35787011Smsmith    }									\
35887011Smsmith    return(cr);								\
35987011Smsmith}									\
36087011Smsmithstruct hack
36187011Smsmith
36287011SmsmithCISSQ_REQUEST_QUEUE(free, CISSQ_FREE);
363128337SpsCISSQ_REQUEST_QUEUE(notify, CISSQ_NOTIFY);
36487011Smsmith
365180454Sscottlstatic __inline void
366180454Sscottlciss_enqueue_complete(struct ciss_request *ac, cr_qhead_t *head)
367180454Sscottl{
368180454Sscottl
369180454Sscottl    STAILQ_INSERT_TAIL(head, ac, cr_link);
370180454Sscottl}
371180454Sscottl
372180454Sscottlstatic __inline struct ciss_request *
373180454Sscottlciss_dequeue_complete(struct ciss_softc *sc, cr_qhead_t *head)
374180454Sscottl{
375180454Sscottl    struct ciss_request  *ac;
376180454Sscottl
377180454Sscottl    if ((ac = STAILQ_FIRST(head)) != NULL)
378180454Sscottl        STAILQ_REMOVE_HEAD(head, cr_link);
379180454Sscottl    return(ac);
380180454Sscottl}
381180454Sscottl
38287011Smsmith/********************************************************************************
38387011Smsmith * space-fill a character string
38487011Smsmith */
38587011Smsmithstatic __inline void
38687011Smsmithpadstr(char *targ, const char *src, int len)
38787011Smsmith{
38887011Smsmith    while (len-- > 0) {
38987011Smsmith	if (*src != 0) {
39087011Smsmith	    *targ++ = *src++;
39187011Smsmith	} else {
39287011Smsmith	    *targ++ = ' ';
39387011Smsmith	}
39487011Smsmith    }
39587011Smsmith}
396180454Sscottl
397180454Sscottl#define ciss_report_request(a, b, c)	\
398180454Sscottl	_ciss_report_request(a, b, c, __FUNCTION__)
399