Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/perseant-stdc-iso10646]: src/sys/dev/sdmmc 2978427
details: https://anonhg.NetBSD.org/src/rev/6740527c3e73
branches: perseant-stdc-iso10646
changeset: 850655:6740527c3e73
user: jmcneill <jmcneill%NetBSD.org@localhost>
date: Sun Jul 16 17:11:47 2017 +0000
description:
2978427
diffstat:
sys/dev/sdmmc/ld_sdmmc.c | 380 +++++++
sys/dev/sdmmc/sdmmc_mem.c | 2243 +++++++++++++++++++++++++++++++++++++++++++++
sys/dev/sdmmc/sdmmcreg.h | 428 ++++++++
sys/dev/sdmmc/sdmmcvar.h | 387 +++++++
4 files changed, 3438 insertions(+), 0 deletions(-)
diffs (truncated from 3454 to 300 lines):
diff -r ef754dae63fb -r 6740527c3e73 sys/dev/sdmmc/ld_sdmmc.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/sdmmc/ld_sdmmc.c Sun Jul 16 17:11:47 2017 +0000
@@ -0,0 +1,380 @@
+/* $NetBSD: ld_sdmmc.c,v 1.31.2.2 2017/07/16 17:11:47 jmcneill Exp $ */
+
+/*
+ * Copyright (c) 2008 KIYOHARA Takashi
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: ld_sdmmc.c,v 1.31.2.2 2017/07/16 17:11:47 jmcneill Exp $");
+
+#ifdef _KERNEL_OPT
+#include "opt_sdmmc.h"
+#endif
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/buf.h>
+#include <sys/bufq.h>
+#include <sys/bus.h>
+#include <sys/endian.h>
+#include <sys/dkio.h>
+#include <sys/disk.h>
+#include <sys/disklabel.h>
+#include <sys/kthread.h>
+#include <sys/syslog.h>
+#include <sys/module.h>
+#include <sys/pcq.h>
+
+#include <dev/ldvar.h>
+
+#include <dev/sdmmc/sdmmcvar.h>
+
+#include "ioconf.h"
+
+#ifdef LD_SDMMC_DEBUG
+#define DPRINTF(s) printf s
+#else
+#define DPRINTF(s) /**/
+#endif
+
+#define LD_SDMMC_IORETRIES 5 /* number of retries before giving up */
+#define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
+
+struct ld_sdmmc_softc;
+
+struct ld_sdmmc_task {
+ struct sdmmc_task task;
+
+ struct ld_sdmmc_softc *task_sc;
+ struct buf *task_bp;
+ int task_retries; /* number of xfer retry */
+ struct callout task_restart_ch;
+};
+
+struct ld_sdmmc_softc {
+ struct ld_softc sc_ld;
+ int sc_hwunit;
+
+ struct sdmmc_function *sc_sf;
+#define LD_SDMMC_MAXQUEUECNT 4
+ struct ld_sdmmc_task sc_task[LD_SDMMC_MAXQUEUECNT];
+ pcq_t *sc_freeq;
+};
+
+static int ld_sdmmc_match(device_t, cfdata_t, void *);
+static void ld_sdmmc_attach(device_t, device_t, void *);
+static int ld_sdmmc_detach(device_t, int);
+
+static int ld_sdmmc_dump(struct ld_softc *, void *, int, int);
+static int ld_sdmmc_start(struct ld_softc *, struct buf *);
+static void ld_sdmmc_restart(void *);
+static int ld_sdmmc_discard(struct ld_softc *, off_t, off_t);
+static int ld_sdmmc_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
+
+static void ld_sdmmc_doattach(void *);
+static void ld_sdmmc_dobio(void *);
+
+CFATTACH_DECL_NEW(ld_sdmmc, sizeof(struct ld_sdmmc_softc),
+ ld_sdmmc_match, ld_sdmmc_attach, ld_sdmmc_detach, NULL);
+
+
+/* ARGSUSED */
+static int
+ld_sdmmc_match(device_t parent, cfdata_t match, void *aux)
+{
+ struct sdmmc_softc *sdmsc = device_private(parent);
+
+ if (ISSET(sdmsc->sc_flags, SMF_MEM_MODE))
+ return 1;
+ return 0;
+}
+
+/* ARGSUSED */
+static void
+ld_sdmmc_attach(device_t parent, device_t self, void *aux)
+{
+ struct ld_sdmmc_softc *sc = device_private(self);
+ struct sdmmc_attach_args *sa = aux;
+ struct ld_softc *ld = &sc->sc_ld;
+ struct ld_sdmmc_task *task;
+ struct lwp *lwp;
+ int i;
+
+ ld->sc_dv = self;
+
+ aprint_normal(": <0x%02x:0x%04x:%s:0x%02x:0x%08x:0x%03x>\n",
+ sa->sf->cid.mid, sa->sf->cid.oid, sa->sf->cid.pnm,
+ sa->sf->cid.rev, sa->sf->cid.psn, sa->sf->cid.mdt);
+ aprint_naive("\n");
+
+ const int ntask = __arraycount(sc->sc_task);
+ sc->sc_freeq = pcq_create(ntask, KM_SLEEP);
+ for (i = 0; i < ntask; i++) {
+ task = &sc->sc_task[i];
+ task->task_sc = sc;
+ callout_init(&task->task_restart_ch, CALLOUT_MPSAFE);
+ pcq_put(sc->sc_freeq, task);
+ }
+
+ sc->sc_hwunit = 0; /* always 0? */
+ sc->sc_sf = sa->sf;
+
+ ld->sc_flags = LDF_ENABLED;
+ ld->sc_secperunit = sc->sc_sf->csd.capacity;
+ ld->sc_secsize = SDMMC_SECTOR_SIZE;
+ ld->sc_maxxfer = MAXPHYS;
+ ld->sc_maxqueuecnt = LD_SDMMC_MAXQUEUECNT;
+ ld->sc_dump = ld_sdmmc_dump;
+ ld->sc_start = ld_sdmmc_start;
+ ld->sc_discard = ld_sdmmc_discard;
+ ld->sc_ioctl = ld_sdmmc_ioctl;
+
+ /*
+ * Defer attachment of ld + disk subsystem to a thread.
+ *
+ * This is necessary because wedge autodiscover needs to
+ * open and call into the ld driver, which could deadlock
+ * when the sdmmc driver isn't ready in early bootstrap.
+ *
+ * Don't mark thread as MPSAFE to keep aprint output sane.
+ */
+ config_pending_incr(self);
+ if (kthread_create(PRI_NONE, 0, NULL,
+ ld_sdmmc_doattach, sc, &lwp, "%sattach", device_xname(self))) {
+ aprint_error_dev(self, "couldn't create thread\n");
+ }
+}
+
+static void
+ld_sdmmc_doattach(void *arg)
+{
+ struct ld_sdmmc_softc *sc = (struct ld_sdmmc_softc *)arg;
+ struct ld_softc *ld = &sc->sc_ld;
+ struct sdmmc_softc *ssc = device_private(device_parent(ld->sc_dv));
+ const u_int cache_size = sc->sc_sf->ext_csd.cache_size;
+ char buf[sizeof("9999 KB")];
+
+ ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
+ aprint_normal_dev(ld->sc_dv, "%d-bit width,", sc->sc_sf->width);
+ if (ssc->sc_transfer_mode != NULL)
+ aprint_normal(" %s,", ssc->sc_transfer_mode);
+ if (cache_size > 0) {
+ format_bytes(buf, sizeof(buf), cache_size);
+ aprint_normal(" %s cache%s,", buf,
+ ISSET(sc->sc_sf->flags, SFF_CACHE_ENABLED) ? "" :
+ " (disabled)");
+ }
+ if ((ssc->sc_busclk / 1000) != 0)
+ aprint_normal(" %u.%03u MHz\n",
+ ssc->sc_busclk / 1000, ssc->sc_busclk % 1000);
+ else
+ aprint_normal(" %u KHz\n", ssc->sc_busclk % 1000);
+ config_pending_decr(ld->sc_dv);
+ kthread_exit(0);
+}
+
+static int
+ld_sdmmc_detach(device_t dev, int flags)
+{
+ struct ld_sdmmc_softc *sc = device_private(dev);
+ struct ld_softc *ld = &sc->sc_ld;
+ int rv, i;
+
+ if ((rv = ldbegindetach(ld, flags)) != 0)
+ return rv;
+ ldenddetach(ld);
+
+ for (i = 0; i < __arraycount(sc->sc_task); i++)
+ callout_destroy(&sc->sc_task[i].task_restart_ch);
+
+ pcq_destroy(sc->sc_freeq);
+
+ return 0;
+}
+
+static int
+ld_sdmmc_start(struct ld_softc *ld, struct buf *bp)
+{
+ struct ld_sdmmc_softc *sc = device_private(ld->sc_dv);
+ struct ld_sdmmc_task *task = pcq_get(sc->sc_freeq);
+
+ if (task == NULL)
+ return EAGAIN;
+
+ task->task_bp = bp;
+ task->task_retries = 0;
+ sdmmc_init_task(&task->task, ld_sdmmc_dobio, task);
+
+ sdmmc_add_task(sc->sc_sf->sc, &task->task);
+
+ return 0;
+}
+
+static void
+ld_sdmmc_restart(void *arg)
+{
+ struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
+ struct ld_sdmmc_softc *sc = task->task_sc;
+ struct buf *bp = task->task_bp;
+
+ bp->b_resid = bp->b_bcount;
+
+ sdmmc_add_task(sc->sc_sf->sc, &task->task);
+}
+
+static void
+ld_sdmmc_dobio(void *arg)
+{
+ struct ld_sdmmc_task *task = (struct ld_sdmmc_task *)arg;
+ struct ld_sdmmc_softc *sc = task->task_sc;
+ struct buf *bp = task->task_bp;
+ int error;
+
+ /*
+ * I/O operation
+ */
+ DPRINTF(("%s: I/O operation (dir=%s, blkno=0x%jx, bcnt=0x%x)\n",
+ device_xname(sc->sc_ld.sc_dv), bp->b_flags & B_READ ? "IN" : "OUT",
+ bp->b_rawblkno, bp->b_bcount));
+
+ /* is everything done in terms of blocks? */
+ if (bp->b_rawblkno >= sc->sc_sf->csd.capacity) {
+ /* trying to read or write past end of device */
+ aprint_error_dev(sc->sc_ld.sc_dv,
+ "blkno 0x%" PRIu64 " exceeds capacity %d\n",
+ bp->b_rawblkno, sc->sc_sf->csd.capacity);
+ bp->b_error = EINVAL;
+ bp->b_resid = bp->b_bcount;
+
+ goto done;
+ }
+
+ if (bp->b_flags & B_READ)
+ error = sdmmc_mem_read_block(sc->sc_sf, bp->b_rawblkno,
+ bp->b_data, bp->b_bcount);
+ else
+ error = sdmmc_mem_write_block(sc->sc_sf, bp->b_rawblkno,
+ bp->b_data, bp->b_bcount);
+ if (error) {
+ if (task->task_retries < LD_SDMMC_IORETRIES) {
+ struct dk_softc *dksc = &sc->sc_ld.sc_dksc;
+ struct cfdriver *cd = device_cfdriver(dksc->sc_dev);
+
+ diskerr(bp, cd->cd_name, "error", LOG_PRINTF, 0,
+ dksc->sc_dkdev.dk_label);
+ printf(", retrying\n");
+ task->task_retries++;
+ callout_reset(&task->task_restart_ch, RECOVERYTIME,
+ ld_sdmmc_restart, task);
+ return;
+ }
+ bp->b_error = error;
Home |
Main Index |
Thread Index |
Old Index