1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/condvar.h>
39 #include <sys/conf.h>
40 #include <sys/bio.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/rman.h>
47 #include <sys/systm.h>
48 #include <sys/taskqueue.h>
49
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52
53 #include <geom/geom_disk.h>
54
55 #include <dev/altera/sdcard/altera_sdcard.h>
56
57 static int
58 altera_sdcard_disk_dump(void *arg, void *virtual, off_t offset, size_t length)
59 {
60
61 panic("%s: not yet", __func__);
62 }
63
64 static int
65 altera_sdcard_disk_ioctl(struct disk *disk, u_long cmd, void *data, int fflag,
66 struct thread *td)
67 {
68
69 /* XXXRW: more here? */
70 return (EINVAL);
71 }
72
73 static void
74 altera_sdcard_disk_strategy(struct bio *bp)
75 {
76 struct altera_sdcard_softc *sc;
77
78 /*
79 * Although the SD Card doesn't need sorting, we don't want to
80 * introduce barriers, so use bioq_disksort().
81 */
82 sc = bp->bio_disk->d_drv1;
83 ALTERA_SDCARD_LOCK(sc);
84 switch (sc->as_state) {
85 case ALTERA_SDCARD_STATE_NOCARD:
86 device_printf(sc->as_dev, "%s: unexpected I/O on NOCARD",
87 __func__);
88 biofinish(bp, NULL, ENXIO);
89 break;
90
91 case ALTERA_SDCARD_STATE_BADCARD:
92 device_printf(sc->as_dev, "%s: unexpected I/O on BADCARD",
93 __func__);
94 biofinish(bp, NULL, ENXIO);
95 break;
96
97 case ALTERA_SDCARD_STATE_DETACHED:
98 device_printf(sc->as_dev, "%s: unexpected I/O on DETACHED",
99 __func__);
100 biofinish(bp, NULL, ENXIO);
101
102 case ALTERA_SDCARD_STATE_IDLE:
103 bioq_disksort(&sc->as_bioq, bp);
104 altera_sdcard_start(sc);
105 break;
106
107 case ALTERA_SDCARD_STATE_IO:
108 bioq_disksort(&sc->as_bioq, bp);
109 break;
110
111 default:
112 panic("%s: invalid state %d", __func__, sc->as_state);
113 }
114 ALTERA_SDCARD_UNLOCK(sc);
115 }
116
117 void
118 altera_sdcard_disk_insert(struct altera_sdcard_softc *sc)
119 {
120 struct disk *disk;
121 uint64_t size;
122
123 ALTERA_SDCARD_LOCK_ASSERT(sc);
124
125 /*
126 * Because the disk insertion routine occupies the driver instance's
127 * task queue thread, and the disk(9) instance isn't hooked up yet by
128 * definition, the only other source of events of concern is a thread
129 * initiating driver detach. That thread has to issue a detach
130 * request and await an ACK from the taskqueue thread. It is
131 * therefore safe to drop the lock here.
132 */
133 ALTERA_SDCARD_UNLOCK(sc);
134 disk = disk_alloc();
135 disk->d_drv1 = sc;
136 disk->d_name = "altera_sdcard";
137 disk->d_unit = sc->as_unit;
138 disk->d_strategy = altera_sdcard_disk_strategy;
139 disk->d_dump = altera_sdcard_disk_dump;
140 disk->d_ioctl = altera_sdcard_disk_ioctl;
141 disk->d_sectorsize = ALTERA_SDCARD_SECTORSIZE;
142 disk->d_mediasize = sc->as_mediasize;
143 disk->d_maxsize = ALTERA_SDCARD_SECTORSIZE;
144 sc->as_disk = disk;
145 disk_create(disk, DISK_VERSION);
146 ALTERA_SDCARD_LOCK(sc);
147
148 /*
149 * Print a pretty-ish card insertion string. We could stand to
150 * decorate this further, e.g., with card vendor information.
151 */
152 size = sc->as_mediasize / (1000 * 1000);
153 device_printf(sc->as_dev, "%juM SD Card inserted\n", (uintmax_t)size);
154 }
155
156 void
157 altera_sdcard_disk_remove(struct altera_sdcard_softc *sc)
158 {
159 struct disk *disk;
160
161 ALTERA_SDCARD_LOCK_ASSERT(sc);
162 KASSERT(sc->as_disk != NULL, ("%s: as_disk NULL", __func__));
163
164 /*
165 * sc->as_state will be updated by the caller.
166 *
167 * XXXRW: Is it OK to call disk_destroy() under the mutex, or should
168 * we be deferring that to the calling context once it is released?
169 */
170 disk = sc->as_disk;
171 disk_gone(disk);
172 disk_destroy(disk);
173 sc->as_disk = NULL;
174
175 /*
176 * Cancel all outstanding I/O on the SD Card.
177 */
178 if (sc->as_currentbio != NULL) {
179 device_printf(sc->as_dev, "%s: SD Card removed during I/O",
180 __func__);
181 biofinish(sc->as_currentbio, NULL, ENXIO);
182 sc->as_currentbio = NULL;
183 }
184 bioq_flush(&sc->as_bioq, NULL, ENXIO);
185 device_printf(sc->as_dev, "SD Card removed\n");
186 }
Cache object: ee99358688f334c42b70c1d96971a09d
|