ld_sdmmc.c revision 1.26.4.5 1 /* $NetBSD: ld_sdmmc.c,v 1.26.4.5 2017/09/01 09:59:10 martin 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.26.4.5 2017/09/01 09:59:10 martin 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 #define LD_SDMMC_MAXQUEUECNT 4 /* number of queued bio requests */
69 #define LD_SDMMC_MAXTASKCNT 8 /* number of tasks in task pool */
70
71 struct ld_sdmmc_softc;
72
73 struct ld_sdmmc_task {
74 struct sdmmc_task task;
75
76 struct ld_sdmmc_softc *task_sc;
77
78 struct buf *task_bp;
79 int task_retries; /* number of xfer retry */
80 struct callout task_restart_ch;
81 };
82
83 struct ld_sdmmc_softc {
84 struct ld_softc sc_ld;
85 int sc_hwunit;
86
87 struct sdmmc_function *sc_sf;
88 struct ld_sdmmc_task sc_task[LD_SDMMC_MAXTASKCNT];
89 pcq_t *sc_freeq;
90
91 struct evcnt sc_ev_discard; /* discard counter */
92 struct evcnt sc_ev_discarderr; /* discard error counter */
93 struct evcnt sc_ev_discardbusy; /* discard busy counter */
94 };
95
96 static int ld_sdmmc_match(device_t, cfdata_t, void *);
97 static void ld_sdmmc_attach(device_t, device_t, void *);
98 static int ld_sdmmc_detach(device_t, int);
99
100 static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
101 static int ld_sdmmc_start(struct ld_softc *, struct buf *);
102 static void ld_sdmmc_restart(void *);
103 static int ld_sdmmc_discard(struct ld_softc *, struct buf *);
104 static int ld_sdmmc_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
105
106 static void ld_sdmmc_doattach(void *);
107 static void ld_sdmmc_dobio(void *);
108 static void ld_sdmmc_dodiscard(void *);
109
110 CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
111 ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
112
113
114 /* ARGSUSED */
115 static int
116 ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
117 {
118 struct sdmmc_softc *sdmsc = device_private(parent);
119
120 if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
121 return 1;
122 return 0;
123 }
124
125 /* ARGSUSED */
126 static void
127 ld_sdmmc_attach(device_t parent, device_t self, void *aux)
128 {
129 struct ld_sdmmc_softc *sc = device_private(self);
130 struct sdmmc_attach_args *sa = aux;
131 struct ld_softc *ld = &sc->sc_ld;
132 struct ld_sdmmc_task *task;
133 struct lwp *lwp;
134 int i;
135
136 ld->sc_dv = self;
137
138 aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
139 sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
140 sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
141 aprint_naive("\n");
142
143 evcnt_attach_dynamic(&sc->sc_ev_discard, EVCNT_TYPE_MISC,
144 NULL, device_xname(self), "sdmmc discard count");
145 evcnt_attach_dynamic(&sc->sc_ev_discarderr, EVCNT_TYPE_MISC,
146 NULL, device_xname(self), "sdmmc discard errors");
147 evcnt_attach_dynamic(&sc->sc_ev_discardbusy, EVCNT_TYPE_MISC,
148 NULL, device_xname(self), "sdmmc discard busy");
149
150 const int ntask = __arraycount(sc->sc_task);
151 sc->sc_freeq = pcq_create(ntask, KM_SLEEP);
152 for (i = 0; i < ntask; i++) {
153 task = &sc->sc_task[i];
154 task->task_sc = sc;
155 callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
156 pcq_put(sc->sc_freeq, task);
157 }
158
159 sc->sc_hwunit = 0; /* always 0? */
160 sc->sc_sf = sa->sf;
161
162 ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
163 ld->sc_secperunit = sc->sc_sf->csd.capacity;
164 ld->sc_secsize = SDMMC_SECTOR_SIZE;
165 ld->sc_maxxfer = MAXPHYS;
166 ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
167 ld->sc_dump = ld_sdmmc_dump;
168 ld->sc_start = ld_sdmmc_start;
169 ld->sc_discard = ld_sdmmc_discard;
170 ld->sc_ioctl = ld_sdmmc_ioctl;
171
172 /*
173 * It is avoided that the error occurs when the card attaches it,
174 * when wedge is supported.
175 */
176 config_pending_incr(self);
177 if (kthread_create(PRI_NONE, 0, NULL,
178 ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
179 aprint_error_dev(self, "couldn't create thread\n");
180 }
181 }
182
183 static void
184 ld_sdmmc_doattach(void *arg)
185 {
186 struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
187 struct ld_softc *ld = &sc->sc_ld;
188 struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
189 const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
190 char buf[sizeof("9999 KB")];
191
192 ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
193 aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
194 if (ssc->sc_transfer_mode != NULL)
195 aprint_normal(" %s,", ssc->sc_transfer_mode);
196 if (cache_size > 0) {
197 format_bytes(buf, sizeof(buf), cache_size);
198 aprint_normal(" %s cache%s,", buf,
199 ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
200 " (disabled)");
201 }
202 if ((ssc->sc_busclk / 1000) != 0)
203 aprint_normal(" %u.%03u MHz\n",
204 ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
205 else
206 aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
207 config_pending_decr(ld->sc_dv);
208 kthread_exit(0);
209 }
210
211 static int
212 ld_sdmmc_detach(device_t dev, int flags)
213 {
214 struct ld_sdmmc_softc *sc = device_private(dev);
215 struct ld_softc *ld = &sc->sc_ld;
216 int rv, i;
217
218 if ((rv = ldbegindetach(ld, flags)) != 0)
219 return rv;
220 ldenddetach(ld);
221
222 for (i = 0; i < __arraycount(sc->sc_task); i++)
223 callout_destroy(&sc->sc_task[i].task_restart_ch);
224
225 pcq_destroy(sc->sc_freeq);
226 evcnt_detach(&sc->sc_ev_discard);
227 evcnt_detach(&sc->sc_ev_discarderr);
228 evcnt_detach(&sc->sc_ev_discardbusy);
229
230 return 0;
231 }
232
233 static int
234 ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
235 {
236 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
237 struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
238
239 if (task == NULL)
240 return EAGAIN;
241
242 task->task_bp = bp;
243 task->task_retries = 0;
244 sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
245
246 sdmmc_add_task(sc->sc_sf->sc, &task->task);
247
248 return 0;
249 }
250
251 static void
252 ld_sdmmc_restart(void *arg)
253 {
254 struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
255 struct ld_sdmmc_softc *sc = task->task_sc;
256 struct buf *bp = task->task_bp;
257
258 bp->b_resid = bp->b_bcount;
259
260 sdmmc_add_task(sc->sc_sf->sc, &task->task);
261 }
262
263 static void
264 ld_sdmmc_dobio(void *arg)
265 {
266 struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
267 struct ld_sdmmc_softc *sc = task->task_sc;
268 struct buf *bp = task->task_bp;
269 int error;
270
271 /*
272 * I/O operation
273 */
274 DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
275 device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
276 bp->b_rawblkno, bp->b_bcount));
277
278 /* is everything done in terms of blocks? */
279 if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
280 /* trying to read or write past end of device */
281 aprint_error_dev(sc->sc_ld.sc_dv,
282 "blkno 0x%" PRIu64 " exceeds capacity %d\n",
283 bp->b_rawblkno, sc->sc_sf->csd.capacity);
284 bp->b_error = EINVAL;
285 bp->b_resid = bp->b_bcount;
286
287 goto done;
288 }
289
290 if (bp->b_flags & B_READ)
291 error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
292 bp->b_data, bp->b_bcount);
293 else
294 error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
295 bp->b_data, bp->b_bcount);
296 if (error) {
297 if (task->task_retries < LD_SDMMC_IORETRIES) {
298 struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
299 struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
300
301 diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
302 dksc->sc_dkdev.dk_label);
303 printf(", retrying\n");
304 task->task_retries++;
305 callout_reset(&task->task_restart_ch, RECOVERYTIME,
306 ld_sdmmc_restart, task);
307 return;
308 }
309 bp->b_error = error;
310 bp->b_resid = bp->b_bcount;
311 } else {
312 bp->b_resid = 0;
313 }
314
315 done:
316 pcq_put(sc->sc_freeq, task);
317
318 lddone(&sc->sc_ld, bp);
319 }
320
321 static int
322 ld_sdmmc_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
323 {
324 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
325
326 return sdmmc_mem_write_block(sc->sc_sf, blkno, data,
327 blkcnt * ld->sc_secsize);
328 }
329
330 static void
331 ld_sdmmc_dodiscard(void *arg)
332 {
333 struct ld_sdmmc_task *task = arg;
334 struct ld_sdmmc_softc *sc = task->task_sc;
335 struct buf *bp = task->task_bp;
336 uint32_t sblkno, nblks;
337 int error;
338
339 /* first and last block to erase */
340 sblkno = bp->b_rawblkno;
341 nblks = howmany(bp->b_bcount, sc->sc_ld.sc_secsize);
342
343 /* An error from discard is non-fatal */
344 error = sdmmc_mem_discard(sc->sc_sf, sblkno, sblkno + nblks - 1);
345 if (error != 0)
346 sc->sc_ev_discarderr.ev_count++;
347 else
348 sc->sc_ev_discard.ev_count++;
349 pcq_put(sc->sc_freeq, task);
350
351 if (error)
352 bp->b_error = error;
353
354 lddiscardend(&sc->sc_ld, bp);
355 }
356
357 static int
358 ld_sdmmc_discard(struct ld_softc *ld, struct buf *bp)
359 {
360 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
361 struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
362
363 if (task == NULL) {
364 sc->sc_ev_discardbusy.ev_count++;
365 return 0;
366 }
367
368 task->task_bp = bp;
369
370 sdmmc_init_task(&task->task, ld_sdmmc_dodiscard, task);
371
372 sdmmc_add_task(sc->sc_sf->sc, &task->task);
373
374 return 0;
375 }
376
377 static int
378 ld_sdmmc_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag,
379 bool poll)
380 {
381 struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
382
383 switch (cmd) {
384 case DIOCCACHESYNC:
385 return sdmmc_mem_flush_cache(sc->sc_sf, poll);
386 default:
387 return EPASSTHROUGH;
388 }
389 }
390
391 MODULE(MODULE_CLASS_DRIVER, ld_sdmmc, "ld");
392
393 #ifdef _MODULE
394 /*
395 * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
396 * XXX it will be defined in the common-code module
397 */
398 #undef CFDRIVER_DECL
399 #define CFDRIVER_DECL(name, class, attr)
400 #include "ioconf.c"
401 #endif
402
403 static int
404 ld_sdmmc_modcmd(modcmd_t cmd, void *opaque)
405 {
406 #ifdef _MODULE
407 /*
408 * We ignore the cfdriver_vec[] that ioconf provides, since
409 * the cfdrivers are attached already.
410 */
411 static struct cfdriver * const no_cfdriver_vec[] = { NULL };
412 #endif
413 int error = 0;
414
415 #ifdef _MODULE
416 switch (cmd) {
417 case MODULE_CMD_INIT:
418 error = config_init_component(no_cfdriver_vec,
419 cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
420 break;
421 case MODULE_CMD_FINI:
422 error = config_fini_component(no_cfdriver_vec,
423 cfattach_ioconf_ld_sdmmc, cfdata_ioconf_ld_sdmmc);
424 break;
425 default:
426 error = ENOTTY;
427 break;
428 }
429 #endif
430
431 return error;
432 }
433