ld_sdmmc.c revision 1.39 1 1.39 riastrad /* $NetBSD: ld_sdmmc.c,v 1.39 2020/07/22 17:17:37 riastradh 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.39 riastrad __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.39 2020/07/22 17:17:37 riastradh 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.39 riastrad #include <sys/kmem.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.38 riastrad #define DPRINTF(s) __nothing
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 struct ld_sdmmc_softc *task_sc;
76 1.33 jmcneill
77 1.1 nonaka struct buf *task_bp;
78 1.24 kiyohara int task_retries; /* number of xfer retry */
79 1.24 kiyohara struct callout task_restart_ch;
80 1.35 jmcneill
81 1.38 riastrad bool task_poll;
82 1.38 riastrad int *task_errorp;
83 1.35 jmcneill
84 1.38 riastrad TAILQ_ENTRY(ld_sdmmc_task) task_entry;
85 1.1 nonaka };
86 1.1 nonaka
87 1.1 nonaka struct ld_sdmmc_softc {
88 1.1 nonaka struct ld_softc sc_ld;
89 1.1 nonaka int sc_hwunit;
90 1.38 riastrad char *sc_typename;
91 1.38 riastrad struct sdmmc_function *sc_sf;
92 1.1 nonaka
93 1.38 riastrad kmutex_t sc_lock;
94 1.38 riastrad kcondvar_t sc_cv;
95 1.38 riastrad TAILQ_HEAD(, ld_sdmmc_task) sc_freeq;
96 1.38 riastrad TAILQ_HEAD(, ld_sdmmc_task) sc_xferq;
97 1.38 riastrad unsigned sc_busy;
98 1.38 riastrad bool sc_dying;
99 1.33 jmcneill
100 1.33 jmcneill struct evcnt sc_ev_discard; /* discard counter */
101 1.33 jmcneill struct evcnt sc_ev_discarderr; /* discard error counter */
102 1.33 jmcneill struct evcnt sc_ev_discardbusy; /* discard busy counter */
103 1.35 jmcneill struct evcnt sc_ev_cachesyncbusy; /* cache sync busy counter */
104 1.38 riastrad
105 1.38 riastrad struct ld_sdmmc_task sc_task[LD_SDMMC_MAXTASKCNT];
106 1.1 nonaka };
107 1.1 nonaka
108 1.2 cegger static int ld_sdmmc_match(device_t, cfdata_t, void *);
109 1.1 nonaka static void ld_sdmmc_attach(device_t, device_t, void *);
110 1.1 nonaka static int ld_sdmmc_detach(device_t, int);
111 1.1 nonaka
112 1.1 nonaka static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
113 1.1 nonaka static int ld_sdmmc_start(struct ld_softc *, struct buf *);
114 1.24 kiyohara static void ld_sdmmc_restart(void *);
115 1.34 mlelstv static int ld_sdmmc_discard(struct ld_softc *, struct buf *);
116 1.31 jmcneill static int ld_sdmmc_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
117 1.1 nonaka
118 1.3 nonaka static void ld_sdmmc_doattach(void *);
119 1.1 nonaka static void ld_sdmmc_dobio(void *);
120 1.33 jmcneill static void ld_sdmmc_dodiscard(void *);
121 1.1 nonaka
122 1.1 nonaka CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
123 1.1 nonaka ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
124 1.1 nonaka
125 1.38 riastrad static struct ld_sdmmc_task *
126 1.38 riastrad ld_sdmmc_task_get(struct ld_sdmmc_softc *sc)
127 1.38 riastrad {
128 1.38 riastrad struct ld_sdmmc_task *task;
129 1.38 riastrad
130 1.38 riastrad KASSERT(mutex_owned(&sc->sc_lock));
131 1.38 riastrad
132 1.38 riastrad if (sc->sc_dying || (task = TAILQ_FIRST(&sc->sc_freeq)) == NULL)
133 1.38 riastrad return NULL;
134 1.38 riastrad TAILQ_REMOVE(&sc->sc_freeq, task, task_entry);
135 1.38 riastrad TAILQ_INSERT_TAIL(&sc->sc_xferq, task, task_entry);
136 1.38 riastrad KASSERT(task->task_bp == NULL);
137 1.38 riastrad KASSERT(task->task_errorp == NULL);
138 1.38 riastrad
139 1.38 riastrad return task;
140 1.38 riastrad }
141 1.38 riastrad
142 1.38 riastrad static void
143 1.38 riastrad ld_sdmmc_task_put(struct ld_sdmmc_softc *sc, struct ld_sdmmc_task *task)
144 1.38 riastrad {
145 1.38 riastrad
146 1.38 riastrad KASSERT(mutex_owned(&sc->sc_lock));
147 1.38 riastrad
148 1.38 riastrad TAILQ_REMOVE(&sc->sc_xferq, task, task_entry);
149 1.38 riastrad TAILQ_INSERT_TAIL(&sc->sc_freeq, task, task_entry);
150 1.38 riastrad task->task_bp = NULL;
151 1.38 riastrad task->task_errorp = NULL;
152 1.38 riastrad }
153 1.38 riastrad
154 1.38 riastrad static void
155 1.38 riastrad ld_sdmmc_task_cancel(struct ld_sdmmc_softc *sc, struct ld_sdmmc_task *task)
156 1.38 riastrad {
157 1.38 riastrad struct buf *bp;
158 1.38 riastrad int *errorp;
159 1.38 riastrad
160 1.38 riastrad KASSERT(mutex_owned(&sc->sc_lock));
161 1.38 riastrad KASSERT(sc->sc_dying);
162 1.38 riastrad
163 1.38 riastrad /*
164 1.38 riastrad * Either the callout or the task may be pending, but not both.
165 1.38 riastrad * First, determine whether the callout is pending.
166 1.38 riastrad */
167 1.38 riastrad if (callout_pending(&task->task_restart_ch) ||
168 1.38 riastrad callout_invoking(&task->task_restart_ch)) {
169 1.38 riastrad /*
170 1.38 riastrad * The callout either is pending, or just started but
171 1.38 riastrad * is waiting for us to release the lock. At this
172 1.38 riastrad * point, it will notice sc->sc_dying and give up, so
173 1.38 riastrad * just wait for it to complete and then we will
174 1.38 riastrad * release everything.
175 1.38 riastrad */
176 1.38 riastrad callout_halt(&task->task_restart_ch, &sc->sc_lock);
177 1.38 riastrad } else {
178 1.38 riastrad /*
179 1.38 riastrad * If the callout is running, it has just scheduled, so
180 1.38 riastrad * after we wait for the callout to finish running, the
181 1.38 riastrad * task is either pending or running. If the task is
182 1.38 riastrad * already running, it will notice sc->sc_dying and
183 1.38 riastrad * give up; otherwise we have to release everything.
184 1.38 riastrad */
185 1.38 riastrad callout_halt(&task->task_restart_ch, &sc->sc_lock);
186 1.38 riastrad if (!sdmmc_del_task(sc->sc_sf->sc, &task->task, &sc->sc_lock))
187 1.38 riastrad return; /* task already started, let it clean up */
188 1.38 riastrad }
189 1.38 riastrad
190 1.38 riastrad /*
191 1.38 riastrad * It is our responsibility to clean up. Move it from xferq
192 1.38 riastrad * back to freeq and make sure to notify anyone waiting that
193 1.38 riastrad * it's finished.
194 1.38 riastrad */
195 1.38 riastrad bp = task->task_bp;
196 1.38 riastrad errorp = task->task_errorp;
197 1.38 riastrad ld_sdmmc_task_put(sc, task);
198 1.38 riastrad
199 1.38 riastrad /*
200 1.38 riastrad * If the task was for an asynchronous I/O xfer, fail the I/O
201 1.38 riastrad * xfer, with the softc lock dropped since this is a callback
202 1.38 riastrad * into arbitrary other subsystems.
203 1.38 riastrad */
204 1.38 riastrad if (bp) {
205 1.38 riastrad mutex_exit(&sc->sc_lock);
206 1.38 riastrad /*
207 1.38 riastrad * XXX We assume that the same sequence works for bio
208 1.38 riastrad * and discard -- that lddiscardend is just the same as
209 1.38 riastrad * setting bp->b_resid = bp->b_bcount in the event of
210 1.38 riastrad * error and then calling lddone.
211 1.38 riastrad */
212 1.38 riastrad bp->b_error = ENXIO;
213 1.38 riastrad bp->b_resid = bp->b_bcount;
214 1.38 riastrad lddone(&sc->sc_ld, bp);
215 1.38 riastrad mutex_enter(&sc->sc_lock);
216 1.38 riastrad }
217 1.38 riastrad
218 1.38 riastrad /*
219 1.38 riastrad * If the task was for a synchronous operation (cachesync),
220 1.38 riastrad * then just set the error indicator and wake up the waiter.
221 1.38 riastrad */
222 1.38 riastrad if (errorp) {
223 1.38 riastrad *errorp = ENXIO;
224 1.38 riastrad cv_broadcast(&sc->sc_cv);
225 1.38 riastrad }
226 1.38 riastrad }
227 1.1 nonaka
228 1.1 nonaka /* ARGSUSED */
229 1.1 nonaka static int
230 1.2 cegger ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
231 1.1 nonaka {
232 1.1 nonaka struct sdmmc_softc *sdmsc = device_private(parent);
233 1.1 nonaka
234 1.1 nonaka if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
235 1.1 nonaka return 1;
236 1.1 nonaka return 0;
237 1.1 nonaka }
238 1.1 nonaka
239 1.1 nonaka /* ARGSUSED */
240 1.1 nonaka static void
241 1.1 nonaka ld_sdmmc_attach(device_t parent, device_t self, void *aux)
242 1.1 nonaka {
243 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(self);
244 1.1 nonaka struct sdmmc_attach_args *sa = aux;
245 1.1 nonaka struct ld_softc *ld = &sc->sc_ld;
246 1.24 kiyohara struct ld_sdmmc_task *task;
247 1.3 nonaka struct lwp *lwp;
248 1.24 kiyohara int i;
249 1.1 nonaka
250 1.1 nonaka ld->sc_dv = self;
251 1.1 nonaka
252 1.11 jakllsch aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
253 1.11 jakllsch sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
254 1.11 jakllsch sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
255 1.1 nonaka aprint_naive("\n");
256 1.1 nonaka
257 1.36 mlelstv sc->sc_typename = kmem_asprintf("0x%02x:0x%04x:%s",
258 1.36 mlelstv sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm);
259 1.36 mlelstv
260 1.33 jmcneill evcnt_attach_dynamic(&sc->sc_ev_discard, EVCNT_TYPE_MISC,
261 1.33 jmcneill NULL, device_xname(self), "sdmmc discard count");
262 1.33 jmcneill evcnt_attach_dynamic(&sc->sc_ev_discarderr, EVCNT_TYPE_MISC,
263 1.33 jmcneill NULL, device_xname(self), "sdmmc discard errors");
264 1.33 jmcneill evcnt_attach_dynamic(&sc->sc_ev_discardbusy, EVCNT_TYPE_MISC,
265 1.33 jmcneill NULL, device_xname(self), "sdmmc discard busy");
266 1.33 jmcneill
267 1.38 riastrad mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SDMMC);
268 1.38 riastrad cv_init(&sc->sc_cv, "ldsdmmc");
269 1.38 riastrad TAILQ_INIT(&sc->sc_freeq);
270 1.38 riastrad TAILQ_INIT(&sc->sc_xferq);
271 1.38 riastrad sc->sc_dying = false;
272 1.38 riastrad
273 1.27 jmcneill const int ntask = __arraycount(sc->sc_task);
274 1.27 jmcneill for (i = 0; i < ntask; i++) {
275 1.24 kiyohara task = &sc->sc_task[i];
276 1.24 kiyohara task->task_sc = sc;
277 1.27 jmcneill callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
278 1.38 riastrad TAILQ_INSERT_TAIL(&sc->sc_freeq, task, task_entry);
279 1.24 kiyohara }
280 1.20 mlelstv
281 1.1 nonaka sc->sc_hwunit = 0; /* always 0? */
282 1.1 nonaka sc->sc_sf = sa->sf;
283 1.1 nonaka
284 1.32 mlelstv ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
285 1.1 nonaka ld->sc_secperunit = sc->sc_sf->csd.capacity;
286 1.4 nonaka ld->sc_secsize = SDMMC_SECTOR_SIZE;
287 1.1 nonaka ld->sc_maxxfer = MAXPHYS;
288 1.20 mlelstv ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
289 1.1 nonaka ld->sc_dump = ld_sdmmc_dump;
290 1.1 nonaka ld->sc_start = ld_sdmmc_start;
291 1.28 jmcneill ld->sc_discard = ld_sdmmc_discard;
292 1.31 jmcneill ld->sc_ioctl = ld_sdmmc_ioctl;
293 1.36 mlelstv ld->sc_typename = sc->sc_typename;
294 1.1 nonaka
295 1.3 nonaka /*
296 1.30 mlelstv * Defer attachment of ld + disk subsystem to a thread.
297 1.30 mlelstv *
298 1.30 mlelstv * This is necessary because wedge autodiscover needs to
299 1.30 mlelstv * open and call into the ld driver, which could deadlock
300 1.30 mlelstv * when the sdmmc driver isn't ready in early bootstrap.
301 1.30 mlelstv *
302 1.30 mlelstv * Don't mark thread as MPSAFE to keep aprint output sane.
303 1.3 nonaka */
304 1.12 christos config_pending_incr(self);
305 1.29 jmcneill if (kthread_create(PRI_NONE, 0, NULL,
306 1.3 nonaka ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
307 1.3 nonaka aprint_error_dev(self, "couldn't create thread\n");
308 1.3 nonaka }
309 1.3 nonaka }
310 1.3 nonaka
311 1.3 nonaka static void
312 1.3 nonaka ld_sdmmc_doattach(void *arg)
313 1.3 nonaka {
314 1.3 nonaka struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
315 1.3 nonaka struct ld_softc *ld = &sc->sc_ld;
316 1.6 kiyohara struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
317 1.31 jmcneill const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
318 1.31 jmcneill char buf[sizeof("9999 KB")];
319 1.3 nonaka
320 1.22 jdolecek ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
321 1.19 jmcneill aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
322 1.19 jmcneill if (ssc->sc_transfer_mode != NULL)
323 1.19 jmcneill aprint_normal(" %s,", ssc->sc_transfer_mode);
324 1.31 jmcneill if (cache_size > 0) {
325 1.31 jmcneill format_bytes(buf, sizeof(buf), cache_size);
326 1.31 jmcneill aprint_normal(" %s cache%s,", buf,
327 1.31 jmcneill ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
328 1.31 jmcneill " (disabled)");
329 1.31 jmcneill }
330 1.6 kiyohara if ((ssc->sc_busclk / 1000) != 0)
331 1.6 kiyohara aprint_normal(" %u.%03u MHz\n",
332 1.6 kiyohara ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
333 1.6 kiyohara else
334 1.6 kiyohara aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
335 1.12 christos config_pending_decr(ld->sc_dv);
336 1.3 nonaka kthread_exit(0);
337 1.1 nonaka }
338 1.1 nonaka
339 1.1 nonaka static int
340 1.1 nonaka ld_sdmmc_detach(device_t dev, int flags)
341 1.1 nonaka {
342 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(dev);
343 1.1 nonaka struct ld_softc *ld = &sc->sc_ld;
344 1.38 riastrad struct ld_sdmmc_task *task;
345 1.24 kiyohara int rv, i;
346 1.1 nonaka
347 1.38 riastrad /*
348 1.38 riastrad * Block new xfers, abort all pending tasks, and wait for all
349 1.38 riastrad * pending waiters to notice that we're gone.
350 1.38 riastrad */
351 1.38 riastrad mutex_enter(&sc->sc_lock);
352 1.38 riastrad sc->sc_dying = true;
353 1.38 riastrad while ((task = TAILQ_FIRST(&sc->sc_xferq)) != NULL)
354 1.38 riastrad ld_sdmmc_task_cancel(sc, task);
355 1.38 riastrad while (sc->sc_busy)
356 1.38 riastrad cv_wait(&sc->sc_cv, &sc->sc_lock);
357 1.38 riastrad mutex_exit(&sc->sc_lock);
358 1.38 riastrad
359 1.38 riastrad /* Do the ld detach dance. */
360 1.38 riastrad if ((rv = ldbegindetach(ld, flags)) != 0) {
361 1.38 riastrad /* Detach failed -- back out. */
362 1.38 riastrad mutex_enter(&sc->sc_lock);
363 1.38 riastrad sc->sc_dying = false;
364 1.38 riastrad mutex_exit(&sc->sc_lock);
365 1.1 nonaka return rv;
366 1.38 riastrad }
367 1.1 nonaka ldenddetach(ld);
368 1.1 nonaka
369 1.38 riastrad KASSERT(TAILQ_EMPTY(&sc->sc_xferq));
370 1.38 riastrad
371 1.38 riastrad for (i = 0; i < __arraycount(sc->sc_task); i++)
372 1.24 kiyohara callout_destroy(&sc->sc_task[i].task_restart_ch);
373 1.24 kiyohara
374 1.38 riastrad cv_destroy(&sc->sc_cv);
375 1.38 riastrad mutex_destroy(&sc->sc_lock);
376 1.38 riastrad
377 1.33 jmcneill evcnt_detach(&sc->sc_ev_discard);
378 1.33 jmcneill evcnt_detach(&sc->sc_ev_discarderr);
379 1.33 jmcneill evcnt_detach(&sc->sc_ev_discardbusy);
380 1.36 mlelstv kmem_free(sc->sc_typename, strlen(sc->sc_typename) + 1);
381 1.27 jmcneill
382 1.1 nonaka return 0;
383 1.1 nonaka }
384 1.1 nonaka
385 1.1 nonaka static int
386 1.1 nonaka ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
387 1.1 nonaka {
388 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
389 1.38 riastrad struct ld_sdmmc_task *task;
390 1.38 riastrad int error;
391 1.20 mlelstv
392 1.38 riastrad mutex_enter(&sc->sc_lock);
393 1.38 riastrad if ((task = ld_sdmmc_task_get(sc)) == NULL) {
394 1.38 riastrad error = EAGAIN;
395 1.38 riastrad goto out;
396 1.38 riastrad }
397 1.1 nonaka
398 1.1 nonaka task->task_bp = bp;
399 1.24 kiyohara task->task_retries = 0;
400 1.1 nonaka sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
401 1.1 nonaka
402 1.1 nonaka sdmmc_add_task(sc->sc_sf->sc, &task->task);
403 1.1 nonaka
404 1.38 riastrad /* Success! The xfer is now queued. */
405 1.38 riastrad error = 0;
406 1.38 riastrad
407 1.38 riastrad out: mutex_exit(&sc->sc_lock);
408 1.38 riastrad return error;
409 1.1 nonaka }
410 1.1 nonaka
411 1.1 nonaka static void
412 1.24 kiyohara ld_sdmmc_restart(void *arg)
413 1.24 kiyohara {
414 1.24 kiyohara struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
415 1.24 kiyohara struct ld_sdmmc_softc *sc = task->task_sc;
416 1.24 kiyohara struct buf *bp = task->task_bp;
417 1.24 kiyohara
418 1.24 kiyohara bp->b_resid = bp->b_bcount;
419 1.24 kiyohara
420 1.38 riastrad mutex_enter(&sc->sc_lock);
421 1.38 riastrad callout_ack(&task->task_restart_ch);
422 1.38 riastrad if (!sc->sc_dying)
423 1.38 riastrad sdmmc_add_task(sc->sc_sf->sc, &task->task);
424 1.38 riastrad mutex_exit(&sc->sc_lock);
425 1.24 kiyohara }
426 1.24 kiyohara
427 1.24 kiyohara static void
428 1.1 nonaka ld_sdmmc_dobio(void *arg)
429 1.1 nonaka {
430 1.1 nonaka struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
431 1.1 nonaka struct ld_sdmmc_softc *sc = task->task_sc;
432 1.1 nonaka struct buf *bp = task->task_bp;
433 1.18 mlelstv int error;
434 1.1 nonaka
435 1.1 nonaka /*
436 1.1 nonaka * I/O operation
437 1.1 nonaka */
438 1.1 nonaka DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
439 1.1 nonaka device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
440 1.1 nonaka bp->b_rawblkno, bp->b_bcount));
441 1.1 nonaka
442 1.1 nonaka /* is everything done in terms of blocks? */
443 1.1 nonaka if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
444 1.1 nonaka /* trying to read or write past end of device */
445 1.13 mlelstv aprint_error_dev(sc->sc_ld.sc_dv,
446 1.13 mlelstv "blkno 0x%" PRIu64 " exceeds capacity %d\n",
447 1.13 mlelstv bp->b_rawblkno, sc->sc_sf->csd.capacity);
448 1.13 mlelstv bp->b_error = EINVAL;
449 1.1 nonaka bp->b_resid = bp->b_bcount;
450 1.26 jmcneill
451 1.26 jmcneill goto done;
452 1.1 nonaka }
453 1.1 nonaka
454 1.1 nonaka if (bp->b_flags & B_READ)
455 1.1 nonaka error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
456 1.1 nonaka bp->b_data, bp->b_bcount);
457 1.1 nonaka else
458 1.1 nonaka error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
459 1.1 nonaka bp->b_data, bp->b_bcount);
460 1.1 nonaka if (error) {
461 1.24 kiyohara if (task->task_retries < LD_SDMMC_IORETRIES) {
462 1.24 kiyohara struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
463 1.24 kiyohara struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
464 1.24 kiyohara
465 1.24 kiyohara diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
466 1.24 kiyohara dksc->sc_dkdev.dk_label);
467 1.24 kiyohara printf(", retrying\n");
468 1.24 kiyohara task->task_retries++;
469 1.38 riastrad mutex_enter(&sc->sc_lock);
470 1.38 riastrad if (sc->sc_dying) {
471 1.38 riastrad bp->b_resid = bp->b_bcount;
472 1.38 riastrad bp->b_error = error;
473 1.38 riastrad goto done_locked;
474 1.38 riastrad } else {
475 1.38 riastrad callout_reset(&task->task_restart_ch,
476 1.38 riastrad RECOVERYTIME, ld_sdmmc_restart, task);
477 1.38 riastrad }
478 1.38 riastrad mutex_exit(&sc->sc_lock);
479 1.24 kiyohara return;
480 1.24 kiyohara }
481 1.13 mlelstv bp->b_error = error;
482 1.1 nonaka bp->b_resid = bp->b_bcount;
483 1.1 nonaka } else {
484 1.1 nonaka bp->b_resid = 0;
485 1.1 nonaka }
486 1.1 nonaka
487 1.26 jmcneill done:
488 1.38 riastrad /* Dissociate the task from the I/O xfer and release it. */
489 1.38 riastrad mutex_enter(&sc->sc_lock);
490 1.38 riastrad done_locked:
491 1.38 riastrad ld_sdmmc_task_put(sc, task);
492 1.38 riastrad mutex_exit(&sc->sc_lock);
493 1.24 kiyohara
494 1.1 nonaka lddone(&sc->sc_ld, bp);
495 1.1 nonaka }
496 1.1 nonaka
497 1.1 nonaka static int
498 1.1 nonaka ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
499 1.1 nonaka {
500 1.1 nonaka struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
501 1.1 nonaka
502 1.1 nonaka return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
503 1.1 nonaka blkcnt * ld->sc_secsize);
504 1.1 nonaka }
505 1.23 pgoyette
506 1.33 jmcneill static void
507 1.33 jmcneill ld_sdmmc_dodiscard(void *arg)
508 1.33 jmcneill {
509 1.33 jmcneill struct ld_sdmmc_task *task = arg;
510 1.33 jmcneill struct ld_sdmmc_softc *sc = task->task_sc;
511 1.34 mlelstv struct buf *bp = task->task_bp;
512 1.34 mlelstv uint32_t sblkno, nblks;
513 1.33 jmcneill int error;
514 1.33 jmcneill
515 1.34 mlelstv /* first and last block to erase */
516 1.34 mlelstv sblkno = bp->b_rawblkno;
517 1.34 mlelstv nblks = howmany(bp->b_bcount, sc->sc_ld.sc_secsize);
518 1.34 mlelstv
519 1.33 jmcneill /* An error from discard is non-fatal */
520 1.34 mlelstv error = sdmmc_mem_discard(sc->sc_sf, sblkno, sblkno + nblks - 1);
521 1.38 riastrad
522 1.38 riastrad /* Count error or success and release the task. */
523 1.38 riastrad mutex_enter(&sc->sc_lock);
524 1.38 riastrad if (error)
525 1.33 jmcneill sc->sc_ev_discarderr.ev_count++;
526 1.33 jmcneill else
527 1.33 jmcneill sc->sc_ev_discard.ev_count++;
528 1.38 riastrad ld_sdmmc_task_put(sc, task);
529 1.38 riastrad mutex_exit(&sc->sc_lock);
530 1.33 jmcneill
531 1.38 riastrad /* Record the error and notify the xfer of completion. */
532 1.34 mlelstv if (error)
533 1.34 mlelstv bp->b_error = error;
534 1.34 mlelstv lddiscardend(&sc->sc_ld, bp);
535 1.33 jmcneill }
536 1.33 jmcneill
537 1.28 jmcneill static int
538 1.34 mlelstv ld_sdmmc_discard(struct ld_softc *ld, struct buf *bp)
539 1.28 jmcneill {
540 1.28 jmcneill struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
541 1.38 riastrad struct ld_sdmmc_task *task;
542 1.38 riastrad int error;
543 1.38 riastrad
544 1.38 riastrad mutex_enter(&sc->sc_lock);
545 1.33 jmcneill
546 1.38 riastrad /* Acquire a free task, or drop the request altogether. */
547 1.38 riastrad if ((task = ld_sdmmc_task_get(sc)) == NULL) {
548 1.33 jmcneill sc->sc_ev_discardbusy.ev_count++;
549 1.38 riastrad error = EBUSY;
550 1.38 riastrad goto out;
551 1.33 jmcneill }
552 1.28 jmcneill
553 1.38 riastrad /* Set up the task and schedule it. */
554 1.34 mlelstv task->task_bp = bp;
555 1.33 jmcneill sdmmc_init_task(&task->task, ld_sdmmc_dodiscard, task);
556 1.33 jmcneill
557 1.33 jmcneill sdmmc_add_task(sc->sc_sf->sc, &task->task);
558 1.33 jmcneill
559 1.38 riastrad /* Success! The request is queued. */
560 1.38 riastrad error = 0;
561 1.38 riastrad
562 1.38 riastrad out: mutex_exit(&sc->sc_lock);
563 1.38 riastrad return error;
564 1.28 jmcneill }
565 1.28 jmcneill
566 1.35 jmcneill static void
567 1.35 jmcneill ld_sdmmc_docachesync(void *arg)
568 1.35 jmcneill {
569 1.35 jmcneill struct ld_sdmmc_task *task = arg;
570 1.35 jmcneill struct ld_sdmmc_softc *sc = task->task_sc;
571 1.38 riastrad int error;
572 1.35 jmcneill
573 1.38 riastrad /* Flush the cache. */
574 1.38 riastrad error = sdmmc_mem_flush_cache(sc->sc_sf, task->task_poll);
575 1.35 jmcneill
576 1.38 riastrad mutex_enter(&sc->sc_lock);
577 1.38 riastrad
578 1.38 riastrad /* Notify the other thread that we're done; pass on the error. */
579 1.38 riastrad *task->task_errorp = error;
580 1.38 riastrad cv_broadcast(&sc->sc_cv);
581 1.38 riastrad
582 1.38 riastrad /* Release the task. */
583 1.38 riastrad ld_sdmmc_task_put(sc, task);
584 1.38 riastrad
585 1.38 riastrad mutex_exit(&sc->sc_lock);
586 1.35 jmcneill }
587 1.35 jmcneill
588 1.35 jmcneill static int
589 1.35 jmcneill ld_sdmmc_cachesync(struct ld_softc *ld, bool poll)
590 1.35 jmcneill {
591 1.35 jmcneill struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
592 1.38 riastrad struct ld_sdmmc_task *task;
593 1.38 riastrad int error = -1;
594 1.38 riastrad
595 1.38 riastrad mutex_enter(&sc->sc_lock);
596 1.35 jmcneill
597 1.38 riastrad /* Acquire a free task, or fail with EBUSY. */
598 1.38 riastrad if ((task = ld_sdmmc_task_get(sc)) == NULL) {
599 1.35 jmcneill sc->sc_ev_cachesyncbusy.ev_count++;
600 1.38 riastrad error = EBUSY;
601 1.38 riastrad goto out;
602 1.35 jmcneill }
603 1.35 jmcneill
604 1.38 riastrad /* Set up the task and schedule it. */
605 1.38 riastrad task->task_poll = poll;
606 1.38 riastrad task->task_errorp = &error;
607 1.35 jmcneill sdmmc_init_task(&task->task, ld_sdmmc_docachesync, task);
608 1.35 jmcneill
609 1.35 jmcneill sdmmc_add_task(sc->sc_sf->sc, &task->task);
610 1.35 jmcneill
611 1.38 riastrad /*
612 1.38 riastrad * Wait for the task to complete. If the device is yanked,
613 1.38 riastrad * detach will notify us. Keep the busy count up until we're
614 1.38 riastrad * done waiting so that the softc doesn't go away until we're
615 1.38 riastrad * done.
616 1.38 riastrad */
617 1.38 riastrad sc->sc_busy++;
618 1.38 riastrad KASSERT(sc->sc_busy <= LD_SDMMC_MAXTASKCNT);
619 1.38 riastrad while (error == -1)
620 1.38 riastrad cv_wait(&sc->sc_cv, &sc->sc_lock);
621 1.38 riastrad if (--sc->sc_busy == 0)
622 1.38 riastrad cv_broadcast(&sc->sc_cv);
623 1.35 jmcneill
624 1.38 riastrad out: mutex_exit(&sc->sc_lock);
625 1.35 jmcneill return error;
626 1.35 jmcneill }
627 1.35 jmcneill
628 1.31 jmcneill static int
629 1.31 jmcneill ld_sdmmc_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
630 1.31 jmcneill bool poll)
631 1.31 jmcneill {
632 1.31 jmcneill
633 1.31 jmcneill switch (cmd) {
634 1.31 jmcneill case DIOCCACHESYNC:
635 1.35 jmcneill return ld_sdmmc_cachesync(ld, poll);
636 1.31 jmcneill default:
637 1.31 jmcneill return EPASSTHROUGH;
638 1.31 jmcneill }
639 1.31 jmcneill }
640 1.31 jmcneill
641 1.23 pgoyette MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
642 1.23 pgoyette
643 1.23 pgoyette #ifdef _MODULE
644 1.23 pgoyette /*
645 1.23 pgoyette * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
646 1.23 pgoyette * XXX it will be defined in the common-code module
647 1.23 pgoyette */
648 1.23 pgoyette #undef CFDRIVER_DECL
649 1.23 pgoyette #define CFDRIVER_DECL(name, class, attr)
650 1.37 mlelstv #include "ioconf.c"
651 1.23 pgoyette #endif
652 1.23 pgoyette
653 1.23 pgoyette static int
654 1.23 pgoyette ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
655 1.23 pgoyette {
656 1.23 pgoyette #ifdef _MODULE
657 1.23 pgoyette /*
658 1.23 pgoyette * We ignore the cfdriver_vec[] that ioconf provides, since
659 1.23 pgoyette * the cfdrivers are attached already.
660 1.23 pgoyette */
661 1.23 pgoyette static struct cfdriver * const no_cfdriver_vec[] = { NULL };
662 1.23 pgoyette #endif
663 1.23 pgoyette int error = 0;
664 1.37 mlelstv
665 1.23 pgoyette #ifdef _MODULE
666 1.23 pgoyette switch (cmd) {
667 1.23 pgoyette case MODULE_CMD_INIT:
668 1.23 pgoyette error = config_init_component(no_cfdriver_vec,
669 1.23 pgoyette cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
670 1.37 mlelstv break;
671 1.23 pgoyette case MODULE_CMD_FINI:
672 1.23 pgoyette error = config_fini_component(no_cfdriver_vec,
673 1.23 pgoyette cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
674 1.23 pgoyette break;
675 1.23 pgoyette default:
676 1.23 pgoyette error = ENOTTY;
677 1.23 pgoyette break;
678 1.23 pgoyette }
679 1.23 pgoyette #endif
680 1.23 pgoyette
681 1.23 pgoyette return error;
682 1.23 pgoyette }
683