ld_sdmmc.c revision 1.32 1 /* $NetBSD: ld_sdmmc.c,v 1.32 2017/08/09 16:44:40 mlelstv Exp $ */
2
3 /*
4 * Copyright (c) 2008 KIYOHARA Takashi
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.32 2017/08/09 16:44:40 mlelstv Exp $");
32
33 #ifdef _KERNEL_OPT
34 #include "opt_sdmmc.h"
35 #endif
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/buf.h>
42 #include <sys/bufq.h>
43 #include <sys/bus.h>
44 #include <sys/endian.h>
45 #include <sys/dkio.h>
46 #include <sys/disk.h>
47 #include <sys/disklabel.h>
48 #include <sys/kthread.h>
49 #include <sys/syslog.h>
50 #include <sys/module.h>
51 #include <sys/pcq.h>
52
53 #include <dev/ldvar.h>
54
55 #include <dev/sdmmc/sdmmcvar.h>
56
57 #include "ioconf.h"
58
59 #ifdef LD_SDMMC_DEBUG
60 #define DPRINTF(s) printf s
61 #else
62 #define DPRINTF(s) /**/
63 #endif
64
65 #define LD_SDMMC_IORETRIES 5 /* number of retries before giving up */
66 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
67
68 struct ld_sdmmc_softc;
69
70 struct ld_sdmmc_task {
71 struct sdmmc_task task;
72
73 struct ld_sdmmc_softc *task_sc;
74 struct buf *task_bp;
75 int task_retries; /* number of xfer retry */
76 struct callout task_restart_ch;
77 };
78
79 struct ld_sdmmc_softc {
80 struct ld_softc sc_ld;
81 int sc_hwunit;
82
83 struct sdmmc_function *sc_sf;
84 #define LD_SDMMC_MAXQUEUECNT 4
85 struct ld_sdmmc_task sc_task[LD_SDMMC_MAXQUEUECNT];
86 pcq_t *sc_freeq;
87 };
88
89 static int ld_sdmmc_match(device_t, cfdata_t, void *);
90 static void ld_sdmmc_attach(device_t, device_t, void *);
91 static int ld_sdmmc_detach(device_t, int);
92
93 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
94 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
95 static void ld_sdmmc_restart(void *);
96 static int ld_sdmmc_discard(struct ld_softc *, off_t, off_t);
97 static int ld_sdmmc_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
98
99 static void ld_sdmmc_doattach(void *);
100 static void ld_sdmmc_dobio(void *);
101
102 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
103 ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
104
105
106 /* ARGSUSED */
107 static int
108 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
109 {
110 struct sdmmc_softc *sdmsc = device_private(parent);
111
112 if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
113 return 1;
114 return 0;
115 }
116
117 /* ARGSUSED */
118 static void
119 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
120 {
121 struct ld_sdmmc_softc *sc = device_private(self);
122 struct sdmmc_attach_args *sa = aux;
123 struct ld_softc *ld = &sc->sc_ld;
124 struct ld_sdmmc_task *task;
125 struct lwp *lwp;
126 int i;
127
128 ld->sc_dv = self;
129
130 aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
131 sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
132 sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
133 aprint_naive("\n");
134
135 const int ntask = __arraycount(sc->sc_task);
136 sc->sc_freeq = pcq_create(ntask, KM_SLEEP);
137 for (i = 0; i < ntask; i++) {
138 task = &sc->sc_task[i];
139 task->task_sc = sc;
140 callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
141 pcq_put(sc->sc_freeq, task);
142 }
143
144 sc->sc_hwunit = 0; /* always 0? */
145 sc->sc_sf = sa->sf;
146
147 ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
148 ld->sc_secperunit = sc->sc_sf->csd.capacity;
149 ld->sc_secsize = SDMMC_SECTOR_SIZE;
150 ld->sc_maxxfer = MAXPHYS;
151 ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
152 ld->sc_dump = ld_sdmmc_dump;
153 ld->sc_start = ld_sdmmc_start;
154 ld->sc_discard = ld_sdmmc_discard;
155 ld->sc_ioctl = ld_sdmmc_ioctl;
156
157 /*
158 * Defer attachment of ld + disk subsystem to a thread.
159 *
160 * This is necessary because wedge autodiscover needs to
161 * open and call into the ld driver, which could deadlock
162 * when the sdmmc driver isn't ready in early bootstrap.
163 *
164 * Don't mark thread as MPSAFE to keep aprint output sane.
165 */
166 config_pending_incr(self);
167 if (kthread_create(PRI_NONE, 0, NULL,
168 ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
169 aprint_error_dev(self, "couldn't create thread\n");
170 }
171 }
172
173 static void
174 ld_sdmmc_doattach(void *arg)
175 {
176 struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
177 struct ld_softc *ld = &sc->sc_ld;
178 struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
179 const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
180 char buf[sizeof("9999 KB")];
181
182 ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
183 aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
184 if (ssc->sc_transfer_mode != NULL)
185 aprint_normal(" %s,", ssc->sc_transfer_mode);
186 if (cache_size > 0) {
187 format_bytes(buf, sizeof(buf), cache_size);
188 aprint_normal(" %s cache%s,", buf,
189 ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
190 " (disabled)");
191 }
192 if ((ssc->sc_busclk / 1000) != 0)
193 aprint_normal(" %u.%03u MHz\n",
194 ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
195 else
196 aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
197 config_pending_decr(ld->sc_dv);
198 kthread_exit(0);
199 }
200
201 static int
202 ld_sdmmc_detach(device_t dev, int flags)
203 {
204 struct ld_sdmmc_softc *sc = device_private(dev);
205 struct ld_softc *ld = &sc->sc_ld;
206 int rv, i;
207
208 if ((rv = ldbegindetach(ld, flags)) != 0)
209 return rv;
210 ldenddetach(ld);
211
212 for (i = 0; i < __arraycount(sc->sc_task); i++)
213 callout_destroy(&sc->sc_task[i].task_restart_ch);
214
215 pcq_destroy(sc->sc_freeq);
216
217 return 0;
218 }
219
220 static int
221 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
222 {
223 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
224 struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
225
226 if (task == NULL)
227 return EAGAIN;
228
229 task->task_bp = bp;
230 task->task_retries = 0;
231 sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
232
233 sdmmc_add_task(sc->sc_sf->sc, &task->task);
234
235 return 0;
236 }
237
238 static void
239 ld_sdmmc_restart(void *arg)
240 {
241 struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
242 struct ld_sdmmc_softc *sc = task->task_sc;
243 struct buf *bp = task->task_bp;
244
245 bp->b_resid = bp->b_bcount;
246
247 sdmmc_add_task(sc->sc_sf->sc, &task->task);
248 }
249
250 static void
251 ld_sdmmc_dobio(void *arg)
252 {
253 struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
254 struct ld_sdmmc_softc *sc = task->task_sc;
255 struct buf *bp = task->task_bp;
256 int error;
257
258 /*
259 * I/O operation
260 */
261 DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
262 device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
263 bp->b_rawblkno, bp->b_bcount));
264
265 /* is everything done in terms of blocks? */
266 if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
267 /* trying to read or write past end of device */
268 aprint_error_dev(sc->sc_ld.sc_dv,
269 "blkno 0x%" PRIu64 " exceeds capacity %d\n",
270 bp->b_rawblkno, sc->sc_sf->csd.capacity);
271 bp->b_error = EINVAL;
272 bp->b_resid = bp->b_bcount;
273
274 goto done;
275 }
276
277 if (bp->b_flags & B_READ)
278 error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
279 bp->b_data, bp->b_bcount);
280 else
281 error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
282 bp->b_data, bp->b_bcount);
283 if (error) {
284 if (task->task_retries < LD_SDMMC_IORETRIES) {
285 struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
286 struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
287
288 diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
289 dksc->sc_dkdev.dk_label);
290 printf(", retrying\n");
291 task->task_retries++;
292 callout_reset(&task->task_restart_ch, RECOVERYTIME,
293 ld_sdmmc_restart, task);
294 return;
295 }
296 bp->b_error = error;
297 bp->b_resid = bp->b_bcount;
298 } else {
299 bp->b_resid = 0;
300 }
301
302 done:
303 pcq_put(sc->sc_freeq, task);
304
305 lddone(&sc->sc_ld, bp);
306 }
307
308 static int
309 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
310 {
311 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
312
313 return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
314 blkcnt * ld->sc_secsize);
315 }
316
317 static int
318 ld_sdmmc_discard(struct ld_softc *ld, off_t pos, off_t len)
319 {
320 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
321
322 return sdmmc_mem_discard(sc->sc_sf, pos, len);
323 }
324
325 static int
326 ld_sdmmc_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
327 bool poll)
328 {
329 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
330
331 switch (cmd) {
332 case DIOCCACHESYNC:
333 return sdmmc_mem_flush_cache(sc->sc_sf, poll);
334 default:
335 return EPASSTHROUGH;
336 }
337 }
338
339 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
340
341 #ifdef _MODULE
342 /*
343 * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
344 * XXX it will be defined in the common-code module
345 */
346 #undef CFDRIVER_DECL
347 #define CFDRIVER_DECL(name, class, attr)
348 #include "ioconf.c"
349 #endif
350
351 static int
352 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
353 {
354 #ifdef _MODULE
355 /*
356 * We ignore the cfdriver_vec[] that ioconf provides, since
357 * the cfdrivers are attached already.
358 */
359 static struct cfdriver * const no_cfdriver_vec[] = { NULL };
360 #endif
361 int error = 0;
362
363 #ifdef _MODULE
364 switch (cmd) {
365 case MODULE_CMD_INIT:
366 error = config_init_component(no_cfdriver_vec,
367 cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
368 break;
369 case MODULE_CMD_FINI:
370 error = config_fini_component(no_cfdriver_vec,
371 cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
372 break;
373 default:
374 error = ENOTTY;
375 break;
376 }
377 #endif
378
379 return error;
380 }
381