ld_sdmmc.c revision 1.34.4.2 1 1.34.4.2 martin /* $NetBSD: ld_sdmmc.c,v 1.34.4.2 2020/04/13 08:04:48 martin Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*
4 1.1 nonaka * Copyright (c) 2008 KIYOHARA Takashi
5 1.1 nonaka * All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * Redistribution and use in source and binary forms, with or without
8 1.1 nonaka * modification, are permitted provided that the following conditions
9 1.1 nonaka * are met:
10 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
11 1.1 nonaka * notice, this list of conditions and the following disclaimer.
12 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
14 1.1 nonaka * documentation and/or other materials provided with the distribution.
15 1.1 nonaka *
16 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 nonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 nonaka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 1.1 nonaka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.1 nonaka * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 nonaka * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 nonaka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 nonaka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1 nonaka * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 1.1 nonaka * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 nonaka * POSSIBILITY OF SUCH DAMAGE.
27 1.1 nonaka *
28 1.1 nonaka */
29 1.1 nonaka
30 1.1 nonaka #include <sys/cdefs.h>
31 1.34.4.2 martin __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.34.4.2 2020/04/13 08:04:48 martin Exp $");
32 1.1 nonaka
33 1.9 matt #ifdef _KERNEL_OPT
34 1.9 matt #include "opt_sdmmc.h"
35 1.9 matt #endif
36 1.1 nonaka
37 1.1 nonaka #include <sys/param.h>
38 1.1 nonaka #include <sys/systm.h>
39 1.1 nonaka #include <sys/kernel.h>
40 1.1 nonaka #include <sys/device.h>
41 1.1 nonaka #include <sys/buf.h>
42 1.1 nonaka #include <sys/bufq.h>
43 1.1 nonaka #include <sys/bus.h>
44 1.1 nonaka #include <sys/endian.h>
45 1.1 nonaka #include <sys/dkio.h>
46 1.1 nonaka #include <sys/disk.h>
47 1.25 martin #include <sys/disklabel.h>
48 1.3 nonaka #include <sys/kthread.h>
49 1.25 martin #include <sys/syslog.h>
50 1.23 pgoyette #include <sys/module.h>
51 1.27 jmcneill #include <sys/pcq.h>
52 1.1 nonaka
53 1.1 nonaka #include <dev/ldvar.h>
54 1.1 nonaka
55 1.1 nonaka #include <dev/sdmmc/sdmmcvar.h>
56 1.1 nonaka
57 1.23 pgoyette #include "ioconf.h"
58 1.23 pgoyette
59 1.14 jmcneill #ifdef LD_SDMMC_DEBUG
60 1.1 nonaka #define DPRINTF(s) printf s
61 1.1 nonaka #else
62 1.1 nonaka #define DPRINTF(s) /**/
63 1.1 nonaka #endif
64 1.1 nonaka
65 1.24 kiyohara #define LD_SDMMC_IORETRIES 5 /* number of retries before giving up */
66 1.24 kiyohara #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
67 1.24 kiyohara
68 1.33 jmcneill #define LD_SDMMC_MAXQUEUECNT 4 /* number of queued bio requests */
69 1.33 jmcneill #define LD_SDMMC_MAXTASKCNT 8 /* number of tasks in task pool */
70 1.33 jmcneill
71 1.1 nonaka struct ld_sdmmc_softc;
72 1.1 nonaka
73 1.1 nonaka struct ld_sdmmc_task {
74 1.1 nonaka struct sdmmc_task task;
75 1.1 nonaka
76 1.1 nonaka struct ld_sdmmc_softc *task_sc;
77 1.33 jmcneill
78 1.1 nonaka struct buf *task_bp;
79 1.24 kiyohara int task_retries; /* number of xfer retry */
80 1.24 kiyohara struct callout task_restart_ch;
81 1.34.4.1 christos
82 1.34.4.1 christos kmutex_t task_lock;
83 1.34.4.1 christos kcondvar_t task_cv;
84 1.34.4.1 christos
85 1.34.4.1 christos uintptr_t task_data;
86 1.1 nonaka };
87 1.1 nonaka
88 1.1 nonaka struct ld_sdmmc_softc {
89 1.1 nonaka struct ld_softc sc_ld;
90 1.1 nonaka int sc_hwunit;
91 1.1 nonaka
92 1.1 nonaka struct sdmmc_function *sc_sf;
93 1.33 jmcneill struct ld_sdmmc_task sc_task[LD_SDMMC_MAXTASKCNT];
94 1.27 jmcneill pcq_t *sc_freeq;
95 1.34.4.1 christos char *sc_typename;
96 1.33 jmcneill
97 1.33 jmcneill struct evcnt sc_ev_discard; /* discard counter */
98 1.33 jmcneill struct evcnt sc_ev_discarderr; /* discard error counter */
99 1.33 jmcneill struct evcnt sc_ev_discardbusy; /* discard busy counter */
100 1.34.4.1 christos struct evcnt sc_ev_cachesyncbusy; /* cache sync busy counter */
101 1.1 nonaka };
102 1.1 nonaka
103 1.2 cegger static int ld_sdmmc_match(device_t, cfdata_t, void *);
104 1.1 nonaka static void ld_sdmmc_attach(device_t, device_t, void *);
105 1.1 nonaka static int ld_sdmmc_detach(device_t, int);
106 1.1 nonaka
107 1.1 nonaka static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
108 1.1 nonaka static int ld_sdmmc_start(struct ld_softc *, struct buf *);
109 1.24 kiyohara static void ld_sdmmc_restart(void *);
110 1.34 mlelstv static int ld_sdmmc_discard(struct ld_softc *, struct buf *);
111 1.31 jmcneill static int ld_sdmmc_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
112 1.1 nonaka
113 1.3 nonaka static void ld_sdmmc_doattach(void *);
114 1.1 nonaka static void ld_sdmmc_dobio(void *);
115 1.33 jmcneill static void ld_sdmmc_dodiscard(void *);
116 1.1 nonaka
117 1.1 nonaka CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
118 1.1 nonaka ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
119 1.1 nonaka
120 1.1 nonaka
121 1.1 nonaka /* ARGSUSED */
122 1.1 nonaka static int
123 1.2 cegger ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
124 1.1 nonaka {
125 1.1 nonaka struct sdmmc_softc *sdmsc = device_private(parent);
126 1.1 nonaka
127 1.1 nonaka if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
128 1.1 nonaka return 1;
129 1.1 nonaka return 0;
130 1.1 nonaka }
131 1.1 nonaka
132 1.1 nonaka /* ARGSUSED */
133 1.1 nonaka static void
134 1.1 nonaka ld_sdmmc_attach(device_t parent, device_t self, void *aux)
135 1.1 nonaka {
136 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(self);
137 1.1 nonaka struct sdmmc_attach_args *sa = aux;
138 1.1 nonaka struct ld_softc *ld = &sc->sc_ld;
139 1.24 kiyohara struct ld_sdmmc_task *task;
140 1.3 nonaka struct lwp *lwp;
141 1.24 kiyohara int i;
142 1.1 nonaka
143 1.1 nonaka ld->sc_dv = self;
144 1.1 nonaka
145 1.11 jakllsch aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
146 1.11 jakllsch sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
147 1.11 jakllsch sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
148 1.1 nonaka aprint_naive("\n");
149 1.1 nonaka
150 1.34.4.1 christos sc->sc_typename = kmem_asprintf("0x%02x:0x%04x:%s",
151 1.34.4.1 christos sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm);
152 1.34.4.1 christos
153 1.33 jmcneill evcnt_attach_dynamic(&sc->sc_ev_discard, EVCNT_TYPE_MISC,
154 1.33 jmcneill NULL, device_xname(self), "sdmmc discard count");
155 1.33 jmcneill evcnt_attach_dynamic(&sc->sc_ev_discarderr, EVCNT_TYPE_MISC,
156 1.33 jmcneill NULL, device_xname(self), "sdmmc discard errors");
157 1.33 jmcneill evcnt_attach_dynamic(&sc->sc_ev_discardbusy, EVCNT_TYPE_MISC,
158 1.33 jmcneill NULL, device_xname(self), "sdmmc discard busy");
159 1.33 jmcneill
160 1.27 jmcneill const int ntask = __arraycount(sc->sc_task);
161 1.27 jmcneill sc->sc_freeq = pcq_create(ntask, KM_SLEEP);
162 1.27 jmcneill for (i = 0; i < ntask; i++) {
163 1.24 kiyohara task = &sc->sc_task[i];
164 1.24 kiyohara task->task_sc = sc;
165 1.27 jmcneill callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
166 1.34.4.1 christos mutex_init(&task->task_lock, MUTEX_DEFAULT, IPL_NONE);
167 1.34.4.1 christos cv_init(&task->task_cv, "ldsdmmctask");
168 1.27 jmcneill pcq_put(sc->sc_freeq, task);
169 1.24 kiyohara }
170 1.20 mlelstv
171 1.1 nonaka sc->sc_hwunit = 0; /* always 0? */
172 1.1 nonaka sc->sc_sf = sa->sf;
173 1.1 nonaka
174 1.32 mlelstv ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
175 1.1 nonaka ld->sc_secperunit = sc->sc_sf->csd.capacity;
176 1.4 nonaka ld->sc_secsize = SDMMC_SECTOR_SIZE;
177 1.1 nonaka ld->sc_maxxfer = MAXPHYS;
178 1.20 mlelstv ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
179 1.1 nonaka ld->sc_dump = ld_sdmmc_dump;
180 1.1 nonaka ld->sc_start = ld_sdmmc_start;
181 1.28 jmcneill ld->sc_discard = ld_sdmmc_discard;
182 1.31 jmcneill ld->sc_ioctl = ld_sdmmc_ioctl;
183 1.34.4.1 christos ld->sc_typename = sc->sc_typename;
184 1.1 nonaka
185 1.3 nonaka /*
186 1.30 mlelstv * Defer attachment of ld + disk subsystem to a thread.
187 1.30 mlelstv *
188 1.30 mlelstv * This is necessary because wedge autodiscover needs to
189 1.30 mlelstv * open and call into the ld driver, which could deadlock
190 1.30 mlelstv * when the sdmmc driver isn't ready in early bootstrap.
191 1.30 mlelstv *
192 1.30 mlelstv * Don't mark thread as MPSAFE to keep aprint output sane.
193 1.3 nonaka */
194 1.12 christos config_pending_incr(self);
195 1.29 jmcneill if (kthread_create(PRI_NONE, 0, NULL,
196 1.3 nonaka ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
197 1.3 nonaka aprint_error_dev(self, "couldn't create thread\n");
198 1.3 nonaka }
199 1.3 nonaka }
200 1.3 nonaka
201 1.3 nonaka static void
202 1.3 nonaka ld_sdmmc_doattach(void *arg)
203 1.3 nonaka {
204 1.3 nonaka struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
205 1.3 nonaka struct ld_softc *ld = &sc->sc_ld;
206 1.6 kiyohara struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
207 1.31 jmcneill const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
208 1.31 jmcneill char buf[sizeof("9999 KB")];
209 1.3 nonaka
210 1.22 jdolecek ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
211 1.19 jmcneill aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
212 1.19 jmcneill if (ssc->sc_transfer_mode != NULL)
213 1.19 jmcneill aprint_normal(" %s,", ssc->sc_transfer_mode);
214 1.31 jmcneill if (cache_size > 0) {
215 1.31 jmcneill format_bytes(buf, sizeof(buf), cache_size);
216 1.31 jmcneill aprint_normal(" %s cache%s,", buf,
217 1.31 jmcneill ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
218 1.31 jmcneill " (disabled)");
219 1.31 jmcneill }
220 1.6 kiyohara if ((ssc->sc_busclk / 1000) != 0)
221 1.6 kiyohara aprint_normal(" %u.%03u MHz\n",
222 1.6 kiyohara ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
223 1.6 kiyohara else
224 1.6 kiyohara aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
225 1.12 christos config_pending_decr(ld->sc_dv);
226 1.3 nonaka kthread_exit(0);
227 1.1 nonaka }
228 1.1 nonaka
229 1.1 nonaka static int
230 1.1 nonaka ld_sdmmc_detach(device_t dev, int flags)
231 1.1 nonaka {
232 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(dev);
233 1.1 nonaka struct ld_softc *ld = &sc->sc_ld;
234 1.24 kiyohara int rv, i;
235 1.1 nonaka
236 1.1 nonaka if ((rv = ldbegindetach(ld, flags)) != 0)
237 1.1 nonaka return rv;
238 1.1 nonaka ldenddetach(ld);
239 1.1 nonaka
240 1.34.4.1 christos for (i = 0; i < __arraycount(sc->sc_task); i++) {
241 1.24 kiyohara callout_destroy(&sc->sc_task[i].task_restart_ch);
242 1.34.4.1 christos mutex_destroy(&sc->sc_task[i].task_lock);
243 1.34.4.1 christos cv_destroy(&sc->sc_task[i].task_cv);
244 1.34.4.1 christos }
245 1.24 kiyohara
246 1.27 jmcneill pcq_destroy(sc->sc_freeq);
247 1.33 jmcneill evcnt_detach(&sc->sc_ev_discard);
248 1.33 jmcneill evcnt_detach(&sc->sc_ev_discarderr);
249 1.33 jmcneill evcnt_detach(&sc->sc_ev_discardbusy);
250 1.34.4.1 christos kmem_free(sc->sc_typename, strlen(sc->sc_typename) + 1);
251 1.27 jmcneill
252 1.1 nonaka return 0;
253 1.1 nonaka }
254 1.1 nonaka
255 1.1 nonaka static int
256 1.1 nonaka ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
257 1.1 nonaka {
258 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
259 1.27 jmcneill struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
260 1.20 mlelstv
261 1.27 jmcneill if (task == NULL)
262 1.27 jmcneill return EAGAIN;
263 1.1 nonaka
264 1.1 nonaka task->task_bp = bp;
265 1.24 kiyohara task->task_retries = 0;
266 1.1 nonaka sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
267 1.1 nonaka
268 1.1 nonaka sdmmc_add_task(sc->sc_sf->sc, &task->task);
269 1.1 nonaka
270 1.1 nonaka return 0;
271 1.1 nonaka }
272 1.1 nonaka
273 1.1 nonaka static void
274 1.24 kiyohara ld_sdmmc_restart(void *arg)
275 1.24 kiyohara {
276 1.24 kiyohara struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
277 1.24 kiyohara struct ld_sdmmc_softc *sc = task->task_sc;
278 1.24 kiyohara struct buf *bp = task->task_bp;
279 1.24 kiyohara
280 1.24 kiyohara bp->b_resid = bp->b_bcount;
281 1.24 kiyohara
282 1.24 kiyohara sdmmc_add_task(sc->sc_sf->sc, &task->task);
283 1.24 kiyohara }
284 1.24 kiyohara
285 1.24 kiyohara static void
286 1.1 nonaka ld_sdmmc_dobio(void *arg)
287 1.1 nonaka {
288 1.1 nonaka struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
289 1.1 nonaka struct ld_sdmmc_softc *sc = task->task_sc;
290 1.1 nonaka struct buf *bp = task->task_bp;
291 1.18 mlelstv int error;
292 1.1 nonaka
293 1.1 nonaka /*
294 1.1 nonaka * I/O operation
295 1.1 nonaka */
296 1.1 nonaka DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
297 1.1 nonaka device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
298 1.1 nonaka bp->b_rawblkno, bp->b_bcount));
299 1.1 nonaka
300 1.1 nonaka /* is everything done in terms of blocks? */
301 1.1 nonaka if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
302 1.1 nonaka /* trying to read or write past end of device */
303 1.13 mlelstv aprint_error_dev(sc->sc_ld.sc_dv,
304 1.13 mlelstv "blkno 0x%" PRIu64 " exceeds capacity %d\n",
305 1.13 mlelstv bp->b_rawblkno, sc->sc_sf->csd.capacity);
306 1.13 mlelstv bp->b_error = EINVAL;
307 1.1 nonaka bp->b_resid = bp->b_bcount;
308 1.26 jmcneill
309 1.26 jmcneill goto done;
310 1.1 nonaka }
311 1.1 nonaka
312 1.1 nonaka if (bp->b_flags & B_READ)
313 1.1 nonaka error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
314 1.1 nonaka bp->b_data, bp->b_bcount);
315 1.1 nonaka else
316 1.1 nonaka error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
317 1.1 nonaka bp->b_data, bp->b_bcount);
318 1.1 nonaka if (error) {
319 1.24 kiyohara if (task->task_retries < LD_SDMMC_IORETRIES) {
320 1.24 kiyohara struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
321 1.24 kiyohara struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
322 1.24 kiyohara
323 1.24 kiyohara diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
324 1.24 kiyohara dksc->sc_dkdev.dk_label);
325 1.24 kiyohara printf(", retrying\n");
326 1.24 kiyohara task->task_retries++;
327 1.24 kiyohara callout_reset(&task->task_restart_ch, RECOVERYTIME,
328 1.24 kiyohara ld_sdmmc_restart, task);
329 1.24 kiyohara return;
330 1.24 kiyohara }
331 1.13 mlelstv bp->b_error = error;
332 1.1 nonaka bp->b_resid = bp->b_bcount;
333 1.1 nonaka } else {
334 1.1 nonaka bp->b_resid = 0;
335 1.1 nonaka }
336 1.1 nonaka
337 1.26 jmcneill done:
338 1.27 jmcneill pcq_put(sc->sc_freeq, task);
339 1.24 kiyohara
340 1.1 nonaka lddone(&sc->sc_ld, bp);
341 1.1 nonaka }
342 1.1 nonaka
343 1.1 nonaka static int
344 1.1 nonaka ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
345 1.1 nonaka {
346 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
347 1.1 nonaka
348 1.1 nonaka return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
349 1.1 nonaka blkcnt * ld->sc_secsize);
350 1.1 nonaka }
351 1.23 pgoyette
352 1.33 jmcneill static void
353 1.33 jmcneill ld_sdmmc_dodiscard(void *arg)
354 1.33 jmcneill {
355 1.33 jmcneill struct ld_sdmmc_task *task = arg;
356 1.33 jmcneill struct ld_sdmmc_softc *sc = task->task_sc;
357 1.34 mlelstv struct buf *bp = task->task_bp;
358 1.34 mlelstv uint32_t sblkno, nblks;
359 1.33 jmcneill int error;
360 1.33 jmcneill
361 1.34 mlelstv /* first and last block to erase */
362 1.34 mlelstv sblkno = bp->b_rawblkno;
363 1.34 mlelstv nblks = howmany(bp->b_bcount, sc->sc_ld.sc_secsize);
364 1.34 mlelstv
365 1.33 jmcneill /* An error from discard is non-fatal */
366 1.34 mlelstv error = sdmmc_mem_discard(sc->sc_sf, sblkno, sblkno + nblks - 1);
367 1.33 jmcneill if (error != 0)
368 1.33 jmcneill sc->sc_ev_discarderr.ev_count++;
369 1.33 jmcneill else
370 1.33 jmcneill sc->sc_ev_discard.ev_count++;
371 1.34 mlelstv pcq_put(sc->sc_freeq, task);
372 1.33 jmcneill
373 1.34 mlelstv if (error)
374 1.34 mlelstv bp->b_error = error;
375 1.34 mlelstv
376 1.34 mlelstv lddiscardend(&sc->sc_ld, bp);
377 1.33 jmcneill }
378 1.33 jmcneill
379 1.28 jmcneill static int
380 1.34 mlelstv ld_sdmmc_discard(struct ld_softc *ld, struct buf *bp)
381 1.28 jmcneill {
382 1.28 jmcneill struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
383 1.33 jmcneill struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
384 1.33 jmcneill
385 1.33 jmcneill if (task == NULL) {
386 1.33 jmcneill sc->sc_ev_discardbusy.ev_count++;
387 1.33 jmcneill return 0;
388 1.33 jmcneill }
389 1.28 jmcneill
390 1.34 mlelstv task->task_bp = bp;
391 1.33 jmcneill
392 1.33 jmcneill sdmmc_init_task(&task->task, ld_sdmmc_dodiscard, task);
393 1.33 jmcneill
394 1.33 jmcneill sdmmc_add_task(sc->sc_sf->sc, &task->task);
395 1.33 jmcneill
396 1.33 jmcneill return 0;
397 1.28 jmcneill }
398 1.28 jmcneill
399 1.34.4.1 christos static void
400 1.34.4.1 christos ld_sdmmc_docachesync(void *arg)
401 1.34.4.1 christos {
402 1.34.4.1 christos struct ld_sdmmc_task *task = arg;
403 1.34.4.1 christos struct ld_sdmmc_softc *sc = task->task_sc;
404 1.34.4.1 christos const bool poll = (bool)task->task_data;
405 1.34.4.1 christos
406 1.34.4.1 christos task->task_data = sdmmc_mem_flush_cache(sc->sc_sf, poll);
407 1.34.4.1 christos
408 1.34.4.1 christos mutex_enter(&task->task_lock);
409 1.34.4.1 christos cv_signal(&task->task_cv);
410 1.34.4.1 christos mutex_exit(&task->task_lock);
411 1.34.4.1 christos }
412 1.34.4.1 christos
413 1.34.4.1 christos static int
414 1.34.4.1 christos ld_sdmmc_cachesync(struct ld_softc *ld, bool poll)
415 1.34.4.1 christos {
416 1.34.4.1 christos struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
417 1.34.4.1 christos struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
418 1.34.4.1 christos int error = 0;
419 1.34.4.1 christos
420 1.34.4.1 christos if (task == NULL) {
421 1.34.4.1 christos sc->sc_ev_cachesyncbusy.ev_count++;
422 1.34.4.1 christos return EBUSY;
423 1.34.4.1 christos }
424 1.34.4.1 christos
425 1.34.4.1 christos sdmmc_init_task(&task->task, ld_sdmmc_docachesync, task);
426 1.34.4.1 christos task->task_data = poll;
427 1.34.4.1 christos
428 1.34.4.1 christos mutex_enter(&task->task_lock);
429 1.34.4.1 christos sdmmc_add_task(sc->sc_sf->sc, &task->task);
430 1.34.4.1 christos error = cv_wait_sig(&task->task_cv, &task->task_lock);
431 1.34.4.1 christos mutex_exit(&task->task_lock);
432 1.34.4.1 christos
433 1.34.4.1 christos if (error == 0)
434 1.34.4.1 christos error = (int)task->task_data;
435 1.34.4.1 christos
436 1.34.4.1 christos pcq_put(sc->sc_freeq, task);
437 1.34.4.1 christos
438 1.34.4.1 christos return error;
439 1.34.4.1 christos }
440 1.34.4.1 christos
441 1.31 jmcneill static int
442 1.31 jmcneill ld_sdmmc_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
443 1.31 jmcneill bool poll)
444 1.31 jmcneill {
445 1.31 jmcneill
446 1.31 jmcneill switch (cmd) {
447 1.31 jmcneill case DIOCCACHESYNC:
448 1.34.4.1 christos return ld_sdmmc_cachesync(ld, poll);
449 1.31 jmcneill default:
450 1.31 jmcneill return EPASSTHROUGH;
451 1.31 jmcneill }
452 1.31 jmcneill }
453 1.31 jmcneill
454 1.23 pgoyette MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
455 1.23 pgoyette
456 1.23 pgoyette #ifdef _MODULE
457 1.23 pgoyette /*
458 1.23 pgoyette * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
459 1.23 pgoyette * XXX it will be defined in the common-code module
460 1.23 pgoyette */
461 1.23 pgoyette #undef CFDRIVER_DECL
462 1.23 pgoyette #define CFDRIVER_DECL(name, class, attr)
463 1.34.4.2 martin #include "ioconf.c"
464 1.23 pgoyette #endif
465 1.23 pgoyette
466 1.23 pgoyette static int
467 1.23 pgoyette ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
468 1.23 pgoyette {
469 1.23 pgoyette #ifdef _MODULE
470 1.23 pgoyette /*
471 1.23 pgoyette * We ignore the cfdriver_vec[] that ioconf provides, since
472 1.23 pgoyette * the cfdrivers are attached already.
473 1.23 pgoyette */
474 1.23 pgoyette static struct cfdriver * const no_cfdriver_vec[] = { NULL };
475 1.23 pgoyette #endif
476 1.23 pgoyette int error = 0;
477 1.34.4.2 martin
478 1.23 pgoyette #ifdef _MODULE
479 1.23 pgoyette switch (cmd) {
480 1.23 pgoyette case MODULE_CMD_INIT:
481 1.23 pgoyette error = config_init_component(no_cfdriver_vec,
482 1.23 pgoyette cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
483 1.34.4.2 martin break;
484 1.23 pgoyette case MODULE_CMD_FINI:
485 1.23 pgoyette error = config_fini_component(no_cfdriver_vec,
486 1.23 pgoyette cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
487 1.23 pgoyette break;
488 1.23 pgoyette default:
489 1.23 pgoyette error = ENOTTY;
490 1.23 pgoyette break;
491 1.23 pgoyette }
492 1.23 pgoyette #endif
493 1.23 pgoyette
494 1.23 pgoyette return error;
495 1.23 pgoyette }
496