amrvar.h revision 140688
1/*-
2 * Copyright (c) 1999,2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * Copyright (c) 2002 Eric Moore
28 * Copyright (c) 2002 LSI Logic Corporation
29 * All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 *    notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 *    notice, this list of conditions and the following disclaimer in the
38 *    documentation and/or other materials provided with the distribution.
39 * 3. The party using or redistributing the source code and binary forms
40 *    agrees to the disclaimer below and the terms and conditions set forth
41 *    herein.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 *
56 *      $FreeBSD: head/sys/dev/amr/amrvar.h 140688 2005-01-23 23:25:41Z scottl $
57 */
58
59#if __FreeBSD_version >= 500005
60# include <geom/geom_disk.h>
61# include <sys/lock.h>
62# include <sys/mutex.h>
63#endif
64
65#define LSI_DESC_PCI "LSILogic MegaRAID 1.51"
66
67#ifdef AMR_DEBUG
68# define debug(level, fmt, args...)	do {if (level <= AMR_DEBUG) printf("%s: " fmt "\n", __func__ , ##args);} while(0)
69# define debug_called(level)		do {if (level <= AMR_DEBUG) printf("%s: called\n", __func__);} while(0)
70#else
71# define debug(level, fmt, args...)
72# define debug_called(level)
73#endif
74#define xdebug(fmt, args...)	printf("%s: " fmt "\n", __func__ , ##args)
75
76/*
77 * Per-logical-drive datastructure
78 */
79struct amr_logdrive
80{
81    u_int32_t	al_size;
82    int		al_state;
83    int		al_properties;
84
85    /* synthetic geometry */
86    int		al_cylinders;
87    int		al_heads;
88    int		al_sectors;
89
90    /* driver */
91    device_t	al_disk;
92};
93
94/*
95 * Due to the difficulty of using the zone allocator to create a new
96 * zone from within a module, we use our own clustering to reduce
97 * memory wastage due to allocating lots of these small structures.
98 *
99 * 16k gives us a little under 200 command structures, which should
100 * normally be plenty.  We will grab more if we need them.
101 */
102
103#define AMR_CMD_CLUSTERSIZE	(16 * 1024)
104
105/*
106 * Per-command control structure.
107 */
108struct amr_command
109{
110    TAILQ_ENTRY(amr_command)	ac_link;
111
112    struct amr_softc		*ac_sc;
113    u_int8_t			ac_slot;
114    int				ac_status;	/* command completion status */
115    struct amr_mailbox		ac_mailbox;
116    int				ac_flags;
117#define AMR_CMD_DATAIN		(1<<0)
118#define AMR_CMD_DATAOUT		(1<<1)
119#define AMR_CMD_CCB_DATAIN	(1<<2)
120#define AMR_CMD_CCB_DATAOUT	(1<<3)
121#define AMR_CMD_PRIORITY	(1<<4)
122#define AMR_CMD_MAPPED		(1<<5)
123#define AMR_CMD_SLEEP		(1<<6)
124#define AMR_CMD_BUSY		(1<<7)
125
126    struct bio			*ac_bio;
127
128    void			*ac_data;
129    size_t			ac_length;
130    bus_dmamap_t		ac_dmamap;
131    u_int32_t			ac_dataphys;
132
133    void			*ac_ccb_data;
134    size_t			ac_ccb_length;
135    bus_dmamap_t		ac_ccb_dmamap;
136    u_int32_t			ac_ccb_dataphys;
137
138    void			(* ac_complete)(struct amr_command *ac);
139    void			*ac_private;
140};
141
142struct amr_command_cluster
143{
144    TAILQ_ENTRY(amr_command_cluster)	acc_link;
145    struct amr_command		acc_command[0];
146};
147
148#define AMR_CMD_CLUSTERCOUNT	((AMR_CMD_CLUSTERSIZE - sizeof(struct amr_command_cluster)) /	\
149				 sizeof(struct amr_command))
150
151/*
152 * Per-controller-instance data
153 */
154struct amr_softc
155{
156    /* bus attachments */
157    device_t			amr_dev;
158    struct resource		*amr_reg;		/* control registers */
159    bus_space_handle_t		amr_bhandle;
160    bus_space_tag_t		amr_btag;
161    bus_dma_tag_t		amr_parent_dmat;	/* parent DMA tag */
162    bus_dma_tag_t		amr_buffer_dmat;	/* data buffer DMA tag */
163    struct resource		*amr_irq;		/* interrupt */
164    void			*amr_intr;
165
166    /* mailbox */
167    volatile struct amr_mailbox		*amr_mailbox;
168    volatile struct amr_mailbox64	*amr_mailbox64;
169    u_int32_t			amr_mailboxphys;
170    bus_dma_tag_t		amr_mailbox_dmat;
171    bus_dmamap_t		amr_mailbox_dmamap;
172
173    /* scatter/gather lists and their controller-visible mappings */
174    struct amr_sgentry		*amr_sgtable;		/* s/g lists */
175    u_int32_t			amr_sgbusaddr;		/* s/g table base address in bus space */
176    bus_dma_tag_t		amr_sg_dmat;		/* s/g buffer DMA tag */
177    bus_dmamap_t		amr_sg_dmamap;		/* map for s/g buffers */
178
179    /* controller limits and features */
180    int				amr_nextslot;		/* Next slot to use for newly allocated commands */
181    int				amr_maxio;		/* maximum number of I/O transactions */
182    int				amr_maxdrives;		/* max number of logical drives */
183    int				amr_maxchan;		/* count of SCSI channels */
184
185    /* connected logical drives */
186    struct amr_logdrive		amr_drive[AMR_MAXLD];
187
188    /* controller state */
189    int				amr_state;
190#define AMR_STATE_OPEN		(1<<0)
191#define AMR_STATE_SUSPEND	(1<<1)
192#define AMR_STATE_INTEN		(1<<2)
193#define AMR_STATE_SHUTDOWN	(1<<3)
194#define AMR_STATE_CRASHDUMP	(1<<4)
195#define AMR_STATE_QUEUE_FRZN	(1<<5)
196
197    /* per-controller queues */
198    struct bio_queue_head 	amr_bioq;		/* pending I/O with no commands */
199    TAILQ_HEAD(,amr_command)	amr_ready;		/* commands ready to be submitted */
200    struct amr_command		*amr_busycmd[AMR_MAXCMD];
201    int				amr_busyslots;
202    TAILQ_HEAD(,amr_command)	amr_completed;
203    TAILQ_HEAD(,amr_command)	amr_freecmds;
204    TAILQ_HEAD(,amr_command_cluster)	amr_cmd_clusters;
205
206    /* CAM attachments for passthrough */
207    struct cam_sim		*amr_cam_sim[AMR_MAX_CHANNELS];
208    TAILQ_HEAD(, ccb_hdr)	amr_cam_ccbq;
209    struct cam_devq		*amr_cam_devq;
210
211    /* control device */
212    struct cdev *amr_dev_t;
213
214    /* controller type-specific support */
215    int				amr_type;
216#define AMR_TYPE_QUARTZ		(1<<0)
217#define AMR_IS_QUARTZ(sc)	((sc)->amr_type & AMR_TYPE_QUARTZ)
218#define AMR_TYPE_40LD		(1<<1)
219#define AMR_IS_40LD(sc)		((sc)->amr_type & AMR_TYPE_40LD)
220    int				(* amr_submit_command)(struct amr_softc *sc);
221    int				(* amr_get_work)(struct amr_softc *sc, struct amr_mailbox *mbsave);
222    int				(*amr_poll_command)(struct amr_command *ac);
223    int				(*amr_poll_command1)(struct amr_softc *sc, struct amr_command *ac);
224    int 			support_ext_cdb;	/* greater than 10 byte cdb support */
225
226    /* misc glue */
227    struct intr_config_hook	amr_ich;		/* wait-for-interrupts probe hook */
228    struct callout_handle	amr_timeout;		/* periodic status check */
229    struct mtx			amr_io_lock;
230};
231
232/*
233 * Interface between bus connections and driver core.
234 */
235extern int              amr_attach(struct amr_softc *sc);
236extern void		amr_free(struct amr_softc *sc);
237extern int		amr_flush(struct amr_softc *sc);
238extern int		amr_done(struct amr_softc *sc);
239extern void		amr_startio(struct amr_softc *sc);
240
241/*
242 * Command buffer allocation.
243 */
244extern struct amr_command	*amr_alloccmd(struct amr_softc *sc);
245extern void			amr_releasecmd(struct amr_command *ac);
246
247/*
248 * CAM interface
249 */
250extern int		amr_cam_attach(struct amr_softc *sc);
251extern void		amr_cam_detach(struct amr_softc *sc);
252extern int		amr_cam_command(struct amr_softc *sc, struct amr_command **acp);
253
254/*
255 * MegaRAID logical disk driver
256 */
257struct amrd_softc
258{
259    device_t		amrd_dev;
260    struct amr_softc	*amrd_controller;
261    struct amr_logdrive	*amrd_drive;
262    struct disk		*amrd_disk;
263    int			amrd_unit;
264};
265
266/*
267 * Interface between driver core and disk driver (should be using a bus?)
268 */
269extern int	amr_submit_bio(struct amr_softc *sc, struct bio *bio);
270extern int 	amr_dump_blocks(struct amr_softc *sc, int unit, u_int32_t lba, void *data, int blks);
271extern void	amrd_intr(void *data);
272
273/********************************************************************************
274 * Enqueue/dequeue functions
275 */
276static __inline void
277amr_enqueue_bio(struct amr_softc *sc, struct bio *bio)
278{
279    int		s;
280
281    s = splbio();
282    bioq_insert_tail(&sc->amr_bioq, bio);
283    splx(s);
284}
285
286static __inline struct bio *
287amr_dequeue_bio(struct amr_softc *sc)
288{
289    struct bio	*bio;
290    int		s;
291
292    s = splbio();
293    if ((bio = bioq_first(&sc->amr_bioq)) != NULL)
294	bioq_remove(&sc->amr_bioq, bio);
295    splx(s);
296    return(bio);
297}
298
299static __inline void
300amr_enqueue_ready(struct amr_command *ac)
301{
302    int		s;
303
304    s = splbio();
305    TAILQ_INSERT_TAIL(&ac->ac_sc->amr_ready, ac, ac_link);
306    splx(s);
307}
308
309static __inline void
310amr_requeue_ready(struct amr_command *ac)
311{
312    int		s;
313
314    s = splbio();
315    TAILQ_INSERT_HEAD(&ac->ac_sc->amr_ready, ac, ac_link);
316    splx(s);
317}
318
319static __inline struct amr_command *
320amr_dequeue_ready(struct amr_softc *sc)
321{
322    struct amr_command	*ac;
323    int			s;
324
325    s = splbio();
326    if ((ac = TAILQ_FIRST(&sc->amr_ready)) != NULL)
327	TAILQ_REMOVE(&sc->amr_ready, ac, ac_link);
328    splx(s);
329    return(ac);
330}
331
332static __inline void
333amr_enqueue_completed(struct amr_command *ac)
334{
335    int		s;
336
337    s = splbio();
338    TAILQ_INSERT_TAIL(&ac->ac_sc->amr_completed, ac, ac_link);
339    splx(s);
340}
341
342static __inline struct amr_command *
343amr_dequeue_completed(struct amr_softc *sc)
344{
345    struct amr_command	*ac;
346    int			s;
347
348    s = splbio();
349    if ((ac = TAILQ_FIRST(&sc->amr_completed)) != NULL)
350	TAILQ_REMOVE(&sc->amr_completed, ac, ac_link);
351    splx(s);
352    return(ac);
353}
354
355static __inline void
356amr_enqueue_free(struct amr_command *ac)
357{
358    int		s;
359
360    s = splbio();
361    TAILQ_INSERT_TAIL(&ac->ac_sc->amr_freecmds, ac, ac_link);
362    splx(s);
363}
364
365static __inline struct amr_command *
366amr_dequeue_free(struct amr_softc *sc)
367{
368    struct amr_command	*ac;
369    int			s;
370
371    s = splbio();
372    if ((ac = TAILQ_FIRST(&sc->amr_freecmds)) != NULL)
373	TAILQ_REMOVE(&sc->amr_freecmds, ac, ac_link);
374    splx(s);
375    return(ac);
376}
377