1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_SDCARD_SDA_IMPL_H
27 #define	_SYS_SDCARD_SDA_IMPL_H
28 
29 #include <sys/list.h>
30 #include <sys/ksynch.h>
31 #include <sys/note.h>
32 #include <sys/ddi.h>
33 #include <sys/sunddi.h>
34 #include <sys/sdcard/sda.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * Type and structure definitions.
42  */
43 typedef struct sda_slot sda_slot_t;
44 
45 /*
46  * Per slot state.
47  */
48 struct sda_slot {
49 	sda_host_t	*s_host;
50 	void		*s_prv;			/* bus private data */
51 
52 	int		s_slot_num;
53 	boolean_t	s_inserted;
54 	boolean_t	s_failed;
55 	uint8_t		s_num_io;
56 	uint32_t	s_cur_ocr;		/* current ocr */
57 
58 	uint16_t	s_rca;
59 	uint32_t	s_maxclk;		/* maximum freq for card */
60 
61 	sda_cmd_t	*s_xfrp;		/* pending transfer cmd */
62 	hrtime_t	s_xfrtmo;		/* transfer timeout */
63 
64 	boolean_t	s_reap;
65 	boolean_t	s_warn;
66 	boolean_t	s_ready;		/* target node ready */
67 	boolean_t	s_init;			/* slot initializing */
68 
69 	/* these are protected by the evlock */
70 	boolean_t	s_wake;			/* wake up thread */
71 	boolean_t	s_detach;		/* detach in progress */
72 	boolean_t	s_detect;		/* detect event occurred */
73 	sda_fault_t	s_fault;
74 	boolean_t	s_xfrdone;		/* transfer event occurred */
75 	sda_err_t	s_errno;
76 
77 	uint16_t	s_flags;
78 #define	SLOTF_WRITABLE		0x0004
79 #define	SLOTF_4BITS		0x0008
80 #define	SLOTF_IFCOND		0x0010
81 #define	SLOTF_MMC		0x0020
82 #define	SLOTF_SDMEM		0x0040
83 #define	SLOTF_SDIO		0x0080
84 #define	SLOTF_SDHC		0x0100
85 #define	SLOTF_MEMORY		(SLOTF_MMC | SLOTF_SDMEM)
86 #define	SLOTF_SD		(SLOTF_SDMEM | SLOTF_SDIO)
87 
88 	uint16_t	s_caps;
89 #define	SLOT_CAP_NOPIO		0x0002
90 #define	SLOT_CAP_HISPEED	0x0004
91 #define	SLOT_CAP_4BITS		0x0008
92 
93 	list_t		s_cmdlist;
94 	list_t		s_abortlist;
95 
96 	/*
97 	 * Slot operations.  Slot local copy for performance.
98 	 */
99 	sda_ops_t	s_ops;
100 
101 	/*
102 	 * Recursive locking of slot.
103 	 */
104 	kmutex_t	s_lock;
105 	kcondvar_t	s_cv;
106 	kt_did_t	s_owner;	/* owner holding the slot */
107 	uint32_t	s_circular;	/* circular sda_slot_enter() calls */
108 
109 	/*
110 	 * Event notification/thread wakeup.
111 	 */
112 	kmutex_t	s_evlock;
113 	kcondvar_t	s_evcv;
114 
115 	/*
116 	 * Asynch. threads.
117 	 */
118 	kt_did_t	s_thrid;	/* processing thread id */
119 	ddi_taskq_t	*s_tq;		/* insert taskq */
120 
121 	/*
122 	 * Timestamping for cfgadm benefit.
123 	 */
124 	uint8_t		s_intransit;
125 	time_t		s_stamp;
126 
127 	/*
128 	 * Memory card-specific.
129 	 */
130 	uint32_t	s_rcsd[4];	/* raw csd */
131 	uint32_t	s_rcid[4];	/* raw cid */
132 	uint32_t	s_nblks;	/* total blocks on device */
133 	uint16_t	s_blksz;	/* device block size (typ. 512) */
134 	uint16_t	s_bshift;	/* block address shift factor */
135 	uint32_t	s_speed;	/* max memory clock in hz */
136 
137 	/* Other CID and CSD values */
138 	uint32_t	s_mfg;		/* mfg id */
139 	char		s_prod[8];	/* product id */
140 	char		s_oem[2];	/* oem id */
141 	uint32_t	s_serial;
142 	uint8_t		s_majver;
143 	uint8_t		s_minver;
144 	uint16_t	s_year;
145 	uint8_t		s_month;
146 
147 	uint16_t	s_ccc;		/* card command classes */
148 	uint8_t		s_r2w;		/* read/write factor */
149 	uint8_t		s_dsr;		/* DSR implemented? */
150 	uint8_t		s_perm_wp;	/* permanent write protect set? */
151 	uint8_t		s_temp_wp;	/* temporary write protect set? */
152 
153 	char		s_uuid[40];	/* fabricated universal unique id */
154 
155 	struct b2s_nexus	*s_nexus;
156 	struct b2s_leaf		*s_leaf;
157 };
158 
159 _NOTE(MUTEX_PROTECTS_DATA(sda_slot::s_lock, sda_slot::s_circular))
160 _NOTE(MUTEX_PROTECTS_DATA(sda_slot::s_evlock, sda_slot::s_wake))
161 _NOTE(MUTEX_PROTECTS_DATA(sda_slot::s_evlock, sda_slot::s_detach))
162 _NOTE(MUTEX_PROTECTS_DATA(sda_slot::s_evlock, sda_slot::s_detect))
163 _NOTE(MUTEX_PROTECTS_DATA(sda_slot::s_evlock, sda_slot::s_fault))
164 _NOTE(MUTEX_PROTECTS_DATA(sda_slot::s_evlock, sda_slot::s_xfrdone))
165 _NOTE(MUTEX_PROTECTS_DATA(sda_slot::s_evlock, sda_slot::s_errno))
166 _NOTE(SCHEME_PROTECTS_DATA("slot_enter", sda_slot::s_warn))
167 _NOTE(SCHEME_PROTECTS_DATA("slot_enter", sda_slot::s_xfrtmo))
168 _NOTE(SCHEME_PROTECTS_DATA("slot_enter", sda_slot::s_xfrp))
169 
170 /*
171  * Per host state.  One per devinfo node.  There could be multiple
172  * slots per devinfo node.
173  */
174 struct sda_host {
175 	dev_info_t	*h_dip;
176 	int		h_nslot;
177 	sda_slot_t	*h_slots;
178 	ddi_dma_attr_t	*h_dma;		/* dma attr, needed for mem */
179 
180 	list_node_t	h_node;		/* nexus node linkage */
181 
182 	uint32_t	h_flags;
183 #define	HOST_ATTACH	(1U << 0)	/* host attach completed */
184 #define	HOST_XOPEN	(1U << 2)	/* exclusive open */
185 #define	HOST_SOPEN	(1U << 3)	/* shared open */
186 };
187 
188 _NOTE(SCHEME_PROTECTS_DATA("stable data", sda_host::h_dip))
189 _NOTE(SCHEME_PROTECTS_DATA("stable data", sda_host::h_nslot))
190 _NOTE(SCHEME_PROTECTS_DATA("stable data", sda_host::h_dma))
191 
192 /*
193  * Useful function-like macros.
194  */
195 #define	sda_setprop(s, p, v)	s->s_ops.so_setprop(s->s_prv, p, v)
196 #define	sda_getprop(s, p, v)	s->s_ops.so_getprop(s->s_prv, p, v)
197 
198 /*
199  * sda_cmd.c
200  */
201 void sda_cmd_init(void);
202 void sda_cmd_fini(void);
203 void sda_cmd_list_init(list_t *);
204 void sda_cmd_list_fini(list_t *);
205 sda_cmd_t *sda_cmd_alloc(sda_slot_t *, sda_index_t, uint32_t, sda_rtype_t,
206     void *, int);
207 sda_cmd_t *sda_cmd_alloc_acmd(sda_slot_t *, sda_index_t, uint32_t, sda_rtype_t,
208     void *, int);
209 void sda_cmd_free(sda_cmd_t *);
210 sda_err_t sda_cmd_errno(sda_cmd_t *);
211 void *sda_cmd_data(sda_cmd_t *);
212 void sda_cmd_submit(sda_slot_t *, sda_cmd_t *, void (*)(sda_cmd_t *));
213 void sda_cmd_resubmit_acmd(sda_slot_t *, sda_cmd_t *);
214 void sda_cmd_notify(sda_cmd_t *, uint16_t, sda_err_t);
215 sda_err_t sda_cmd_exec(sda_slot_t *, sda_cmd_t *, uint32_t *);
216 
217 /*
218  * sda_init.c
219  */
220 sda_err_t sda_init_card(sda_slot_t *);
221 
222 /*
223  * sda_mem.c
224  */
225 void sda_mem_init(struct modlinkage *);
226 void sda_mem_fini(struct modlinkage *);
227 uint32_t sda_mem_maxclk(sda_slot_t *);
228 uint32_t sda_mem_getbits(uint32_t *, int, int);
229 
230 
231 /*
232  * sda_nexus.c
233  */
234 void sda_nexus_init(void);
235 void sda_nexus_fini(void);
236 void sda_nexus_register(sda_host_t *);
237 void sda_nexus_unregister(sda_host_t *);
238 int sda_nexus_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
239 int sda_nexus_open(dev_t *, int, int, cred_t *);
240 int sda_nexus_close(dev_t, int, int, cred_t *);
241 int sda_nexus_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
242 int sda_nexus_bus_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
243     void *);
244 void sda_nexus_remove(sda_slot_t *);
245 void sda_nexus_insert(sda_slot_t *);
246 void sda_nexus_reap(void *);
247 
248 /*
249  * sda_slot.c
250  */
251 void sda_slot_init(sda_slot_t *);
252 void sda_slot_fini(sda_slot_t *);
253 void sda_slot_enter(sda_slot_t *);
254 void sda_slot_exit(sda_slot_t *);
255 boolean_t sda_slot_owned(sda_slot_t *);
256 void sda_slot_attach(sda_slot_t *);
257 void sda_slot_detach(sda_slot_t *);
258 void sda_slot_reset(sda_slot_t *);
259 void sda_slot_wakeup(sda_slot_t *);
260 void sda_slot_detect(sda_slot_t *);
261 int sda_slot_power_on(sda_slot_t *);
262 void sda_slot_power_off(sda_slot_t *);
263 void sda_slot_reset(sda_slot_t *);
264 void sda_slot_shutdown(sda_slot_t *);
265 void sda_slot_transfer(sda_slot_t *, sda_err_t);
266 void sda_slot_mem_reset(sda_slot_t *, sda_err_t);
267 void sda_slot_fault(sda_slot_t *, sda_fault_t);
268 /*PRINTFLIKE2*/
269 void sda_slot_err(sda_slot_t *, const char *, ...);
270 /*PRINTFLIKE2*/
271 void sda_slot_log(sda_slot_t *, const char *, ...);
272 
273 #ifdef	DEBUG
274 #define	sda_slot_debug(...)	sda_slot_log(__VA_ARGS__)
275 #else
276 #define	sda_slot_debug(...)
277 #endif
278 
279 #ifdef __cplusplus
280 }
281 #endif
282 
283 #endif	/* _SYS_SDCARD_SDA_IMPL_H */
284