1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2012 Robert N. M. Watson
5 * All rights reserved.
6 *
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9 * ("CTSRD"), as part of the DARPA CRASH research programme.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/bus.h>
35#include <sys/condvar.h>
36#include <sys/conf.h>
37#include <sys/bio.h>
38#include <sys/kernel.h>
39#include <sys/lock.h>
40#include <sys/malloc.h>
41#include <sys/module.h>
42#include <sys/mutex.h>
43#include <sys/rman.h>
44#include <sys/systm.h>
45#include <sys/taskqueue.h>
46
47#include <machine/bus.h>
48#include <machine/resource.h>
49
50#include <geom/geom_disk.h>
51
52#include <dev/altera/sdcard/altera_sdcard.h>
53
54static int
55altera_sdcard_disk_dump(void *arg, void *virtual, vm_offset_t physical,
56    off_t offset, size_t length)
57{
58
59	panic("%s: not yet", __func__);
60}
61
62static int
63altera_sdcard_disk_ioctl(struct disk *disk, u_long cmd, void *data, int fflag,
64    struct thread *td)
65{
66
67	/* XXXRW: more here? */
68	return (EINVAL);
69}
70
71static void
72altera_sdcard_disk_strategy(struct bio *bp)
73{
74	struct altera_sdcard_softc *sc;
75
76	/*
77	 * Although the SD Card doesn't need sorting, we don't want to
78	 * introduce barriers, so use bioq_disksort().
79	 */
80	sc = bp->bio_disk->d_drv1;
81	ALTERA_SDCARD_LOCK(sc);
82	switch (sc->as_state) {
83	case ALTERA_SDCARD_STATE_NOCARD:
84		device_printf(sc->as_dev, "%s: unexpected I/O on NOCARD",
85		    __func__);
86		biofinish(bp, NULL, ENXIO);
87		break;
88
89	case ALTERA_SDCARD_STATE_BADCARD:
90		device_printf(sc->as_dev, "%s: unexpected I/O on BADCARD",
91		    __func__);
92		biofinish(bp, NULL, ENXIO);
93		break;
94
95	case ALTERA_SDCARD_STATE_DETACHED:
96		device_printf(sc->as_dev, "%s: unexpected I/O on DETACHED",
97		    __func__);
98		biofinish(bp, NULL, ENXIO);
99
100	case ALTERA_SDCARD_STATE_IDLE:
101		bioq_disksort(&sc->as_bioq, bp);
102		altera_sdcard_start(sc);
103		break;
104
105	case ALTERA_SDCARD_STATE_IO:
106		bioq_disksort(&sc->as_bioq, bp);
107		break;
108
109	default:
110		panic("%s: invalid state %d", __func__, sc->as_state);
111	}
112	ALTERA_SDCARD_UNLOCK(sc);
113}
114
115void
116altera_sdcard_disk_insert(struct altera_sdcard_softc *sc)
117{
118	struct disk *disk;
119	uint64_t size;
120
121	ALTERA_SDCARD_LOCK_ASSERT(sc);
122
123	/*
124	 * Because the disk insertion routine occupies the driver instance's
125	 * task queue thread, and the disk(9) instance isn't hooked up yet by
126	 * definition, the only other source of events of concern is a thread
127	 * initiating driver detach.  That thread has to issue a detach
128	 * request and await an ACK from the taskqueue thread.  It is
129	 * therefore safe to drop the lock here.
130	 */
131	ALTERA_SDCARD_UNLOCK(sc);
132	disk = disk_alloc();
133	disk->d_drv1 = sc;
134	disk->d_name = "altera_sdcard";
135	disk->d_unit = sc->as_unit;
136	disk->d_strategy = altera_sdcard_disk_strategy;
137	disk->d_dump = altera_sdcard_disk_dump;
138	disk->d_ioctl = altera_sdcard_disk_ioctl;
139	disk->d_sectorsize = ALTERA_SDCARD_SECTORSIZE;
140	disk->d_mediasize = sc->as_mediasize;
141	disk->d_maxsize = ALTERA_SDCARD_SECTORSIZE;
142	sc->as_disk = disk;
143	disk_create(disk, DISK_VERSION);
144	ALTERA_SDCARD_LOCK(sc);
145
146	/*
147	 * Print a pretty-ish card insertion string.  We could stand to
148	 * decorate this further, e.g., with card vendor information.
149	 */
150	size = sc->as_mediasize / (1000 * 1000);
151	device_printf(sc->as_dev, "%juM SD Card inserted\n", (uintmax_t)size);
152}
153
154void
155altera_sdcard_disk_remove(struct altera_sdcard_softc *sc)
156{
157	struct disk *disk;
158
159	ALTERA_SDCARD_LOCK_ASSERT(sc);
160	KASSERT(sc->as_disk != NULL, ("%s: as_disk NULL", __func__));
161
162	/*
163	 * sc->as_state will be updated by the caller.
164	 *
165	 * XXXRW: Is it OK to call disk_destroy() under the mutex, or should
166	 * we be deferring that to the calling context once it is released?
167	 */
168	disk = sc->as_disk;
169	disk_gone(disk);
170	disk_destroy(disk);
171	sc->as_disk = NULL;
172
173	/*
174	 * Cancel all outstanding I/O on the SD Card.
175	 */
176	if (sc->as_currentbio != NULL) {
177		device_printf(sc->as_dev, "%s: SD Card removed during I/O",
178		    __func__);
179		biofinish(sc->as_currentbio, NULL, ENXIO);
180		sc->as_currentbio = NULL;
181	}
182	bioq_flush(&sc->as_bioq, NULL, ENXIO);
183	device_printf(sc->as_dev, "SD Card removed\n");
184}
185