summaryrefslogtreecommitdiffstats
path: root/sys/dev/altera/sdcard/altera_sdcard_disk.c
blob: cae3a82cfbc3821dd745a8c0b981dcfae0d21a2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*-
 * Copyright (c) 2012 Robert N. M. Watson
 * All rights reserved.
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/condvar.h>
#include <sys/conf.h>
#include <sys/bio.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/rman.h>
#include <sys/systm.h>
#include <sys/taskqueue.h>

#include <machine/bus.h>
#include <machine/resource.h>

#include <geom/geom_disk.h>

#include <dev/altera/sdcard/altera_sdcard.h>

static int
altera_sdcard_disk_dump(void *arg, void *virtual, vm_offset_t physical,
    off_t offset, size_t length)
{

	panic("%s: not yet", __func__);
}

static int
altera_sdcard_disk_ioctl(struct disk *disk, u_long cmd, void *data, int fflag,
    struct thread *td)
{

	/* XXXRW: more here? */
	return (EINVAL);
}

static void
altera_sdcard_disk_strategy(struct bio *bp)
{
	struct altera_sdcard_softc *sc;

	/*
	 * Although the SD Card doesn't need sorting, we don't want to
	 * introduce barriers, so use bioq_disksort().
	 */
	sc = bp->bio_disk->d_drv1;
	ALTERA_SDCARD_LOCK(sc);
	switch (sc->as_state) {
	case ALTERA_SDCARD_STATE_NOCARD:
		device_printf(sc->as_dev, "%s: unexpected I/O on NOCARD",
		    __func__);
		biofinish(bp, NULL, ENXIO);
		break;

	case ALTERA_SDCARD_STATE_BADCARD:
		device_printf(sc->as_dev, "%s: unexpected I/O on BADCARD",
		    __func__);
		biofinish(bp, NULL, ENXIO);
		break;

	case ALTERA_SDCARD_STATE_DETACHED:
		device_printf(sc->as_dev, "%s: unexpected I/O on DETACHED",
		    __func__);
		biofinish(bp, NULL, ENXIO);

	case ALTERA_SDCARD_STATE_IDLE:
		bioq_disksort(&sc->as_bioq, bp);
		altera_sdcard_start(sc);
		break;

	case ALTERA_SDCARD_STATE_IO:
		bioq_disksort(&sc->as_bioq, bp);
		break;

	default:
		panic("%s: invalid state %d", __func__, sc->as_state);
	}
	ALTERA_SDCARD_UNLOCK(sc);
}

void
altera_sdcard_disk_insert(struct altera_sdcard_softc *sc)
{
	struct disk *disk;
	uint64_t size;

	ALTERA_SDCARD_LOCK_ASSERT(sc);

	/*
	 * Because the disk insertion routine occupies the driver instance's
	 * task queue thread, and the disk(9) instance isn't hooked up yet by
	 * definition, the only other source of events of concern is a thread
	 * initiating driver detach.  That thread has to issue a detach
	 * request and await an ACK from the taskqueue thread.  It is
	 * therefore safe to drop the lock here.
	 */
	ALTERA_SDCARD_UNLOCK(sc);
	disk = disk_alloc();
	disk->d_drv1 = sc;
	disk->d_name = "altera_sdcard";
	disk->d_unit = sc->as_unit;
	disk->d_strategy = altera_sdcard_disk_strategy;
	disk->d_dump = altera_sdcard_disk_dump;
	disk->d_ioctl = altera_sdcard_disk_ioctl;
	disk->d_sectorsize = ALTERA_SDCARD_SECTORSIZE;
	disk->d_mediasize = sc->as_mediasize;
	disk->d_maxsize = ALTERA_SDCARD_SECTORSIZE;
	sc->as_disk = disk;
	disk_create(disk, DISK_VERSION);
	ALTERA_SDCARD_LOCK(sc);

	/*
	 * Print a pretty-ish card insertion string.  We could stand to
	 * decorate this further, e.g., with card vendor information.
	 */
	size = sc->as_mediasize / (1000 * 1000);
	device_printf(sc->as_dev, "%juM SD Card inserted\n", (uintmax_t)size);
}

void
altera_sdcard_disk_remove(struct altera_sdcard_softc *sc)
{
	struct disk *disk;

	ALTERA_SDCARD_LOCK_ASSERT(sc);
	KASSERT(sc->as_disk != NULL, ("%s: as_disk NULL", __func__));

	/*
	 * sc->as_state will be updated by the caller.
	 *
	 * XXXRW: Is it OK to call disk_destroy() under the mutex, or should
	 * we be deferring that to the calling context once it is released?
	 */
	disk = sc->as_disk;
	disk_gone(disk);
	disk_destroy(disk);
	sc->as_disk = NULL;

	/*
	 * Cancel all outstanding I/O on the SD Card.
	 */
	if (sc->as_currentbio != NULL) {
		device_printf(sc->as_dev, "%s: SD Card removed during I/O",
		    __func__);
		biofinish(sc->as_currentbio, NULL, ENXIO);
		sc->as_currentbio = NULL;
	}
	bioq_flush(&sc->as_bioq, NULL, ENXIO);
	device_printf(sc->as_dev, "SD Card removed\n");
}
OpenPOWER on IntegriCloud