Deleted Added
full compact
mmcsd.c (312405) mmcsd.c (318197)
1/*-
2 * Copyright (c) 2006 Bernd Walter. All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
1/*-
2 * Copyright (c) 2006 Bernd Walter. All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
4 * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the

--- 34 unchanged lines hidden (view full) ---

46 * implication, estoppel or otherwise under any patent or other rights of the
47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49 * or the SD Card Association to disclose or distribute any technical
50 * information, know-how or other confidential information to any third party.
51 */
52
53#include <sys/cdefs.h>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the

--- 34 unchanged lines hidden (view full) ---

47 * implication, estoppel or otherwise under any patent or other rights of the
48 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
49 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
50 * or the SD Card Association to disclose or distribute any technical
51 * information, know-how or other confidential information to any third party.
52 */
53
54#include <sys/cdefs.h>
54__FBSDID("$FreeBSD: stable/11/sys/dev/mmc/mmcsd.c 312405 2017-01-19 11:16:25Z mav $");
55__FBSDID("$FreeBSD: stable/11/sys/dev/mmc/mmcsd.c 318197 2017-05-11 20:55:11Z marius $");
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/bio.h>
59#include <sys/bus.h>
60#include <sys/conf.h>
56
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/bio.h>
60#include <sys/bus.h>
61#include <sys/conf.h>
62#include <sys/fcntl.h>
63#include <sys/ioccom.h>
61#include <sys/kernel.h>
62#include <sys/kthread.h>
63#include <sys/lock.h>
64#include <sys/malloc.h>
65#include <sys/module.h>
66#include <sys/mutex.h>
64#include <sys/kernel.h>
65#include <sys/kthread.h>
66#include <sys/lock.h>
67#include <sys/malloc.h>
68#include <sys/module.h>
69#include <sys/mutex.h>
70#include <sys/slicer.h>
67#include <sys/time.h>
71#include <sys/time.h>
72
68#include <geom/geom.h>
69#include <geom/geom_disk.h>
70
73#include <geom/geom.h>
74#include <geom/geom_disk.h>
75
76#include <dev/mmc/bridge.h>
77#include <dev/mmc/mmc_ioctl.h>
78#include <dev/mmc/mmc_subr.h>
71#include <dev/mmc/mmcbrvar.h>
72#include <dev/mmc/mmcreg.h>
73#include <dev/mmc/mmcvar.h>
74
75#include "mmcbus_if.h"
76
77#if __FreeBSD_version < 800002
78#define kproc_create kthread_create
79#define kproc_exit kthread_exit
80#endif
81
79#include <dev/mmc/mmcbrvar.h>
80#include <dev/mmc/mmcreg.h>
81#include <dev/mmc/mmcvar.h>
82
83#include "mmcbus_if.h"
84
85#if __FreeBSD_version < 800002
86#define kproc_create kthread_create
87#define kproc_exit kthread_exit
88#endif
89
82struct mmcsd_softc {
83 device_t dev;
84 struct mtx sc_mtx;
90#define MMCSD_CMD_RETRIES 5
91
92#define MMCSD_FMT_BOOT "mmcsd%dboot"
93#define MMCSD_FMT_GP "mmcsd%dgp"
94#define MMCSD_FMT_RPMB "mmcsd%drpmb"
95#define MMCSD_LABEL_ENH "enh"
96
97#define MMCSD_PART_NAMELEN (16 + 1)
98
99struct mmcsd_softc;
100
101struct mmcsd_part {
102 struct mtx part_mtx;
103 struct mmcsd_softc *sc;
85 struct disk *disk;
86 struct proc *p;
87 struct bio_queue_head bio_queue;
88 daddr_t eblock, eend; /* Range remaining after the last erase. */
104 struct disk *disk;
105 struct proc *p;
106 struct bio_queue_head bio_queue;
107 daddr_t eblock, eend; /* Range remaining after the last erase. */
108 u_int cnt;
109 u_int type;
89 int running;
90 int suspend;
110 int running;
111 int suspend;
112 bool ro;
113 char name[MMCSD_PART_NAMELEN];
114};
115
116struct mmcsd_softc {
117 device_t dev;
118 device_t mmcbr;
119 struct mmcsd_part *part[MMC_PART_MAX];
120 enum mmc_card_mode mode;
121 uint8_t part_curr; /* Partition currently switched to */
122 uint8_t ext_csd[MMC_EXTCSD_SIZE];
123 uint16_t rca;
124 uint32_t part_time; /* Partition switch timeout [us] */
125 off_t enh_base; /* Enhanced user data area slice base ... */
126 off_t enh_size; /* ... and size [bytes] */
91 int log_count;
92 struct timeval log_time;
127 int log_count;
128 struct timeval log_time;
129 struct cdev *rpmb_dev;
93};
94
95static const char *errmsg[] =
96{
97 "None",
98 "Timeout",
99 "Bad CRC",
100 "Fifo",

--- 7 unchanged lines hidden (view full) ---

108/* bus entry points */
109static int mmcsd_attach(device_t dev);
110static int mmcsd_detach(device_t dev);
111static int mmcsd_probe(device_t dev);
112
113/* disk routines */
114static int mmcsd_close(struct disk *dp);
115static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
130};
131
132static const char *errmsg[] =
133{
134 "None",
135 "Timeout",
136 "Bad CRC",
137 "Fifo",

--- 7 unchanged lines hidden (view full) ---

145/* bus entry points */
146static int mmcsd_attach(device_t dev);
147static int mmcsd_detach(device_t dev);
148static int mmcsd_probe(device_t dev);
149
150/* disk routines */
151static int mmcsd_close(struct disk *dp);
152static int mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
116 off_t offset, size_t length);
153 off_t offset, size_t length);
154static int mmcsd_getattr(struct bio *);
155static int mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data,
156 int fflag, struct thread *td);
117static int mmcsd_open(struct disk *dp);
118static void mmcsd_strategy(struct bio *bp);
119static void mmcsd_task(void *arg);
120
157static int mmcsd_open(struct disk *dp);
158static void mmcsd_strategy(struct bio *bp);
159static void mmcsd_task(void *arg);
160
161/* RMPB cdev interface */
162static int mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
163 int fflag, struct thread *td);
164
165static void mmcsd_add_part(struct mmcsd_softc *sc, u_int type,
166 const char *name, u_int cnt, off_t media_size, off_t erase_size, bool ro);
121static int mmcsd_bus_bit_width(device_t dev);
167static int mmcsd_bus_bit_width(device_t dev);
122static daddr_t mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp);
123static daddr_t mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp);
168static daddr_t mmcsd_delete(struct mmcsd_part *part, struct bio *bp);
169static int mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data,
170 int fflag);
171static int mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic,
172 int fflag);
173static uintmax_t mmcsd_pretty_size(off_t size, char *unit);
174static daddr_t mmcsd_rw(struct mmcsd_part *part, struct bio *bp);
175static int mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool rel);
176static int mmcsd_slicer(device_t dev, const char *provider,
177 struct flash_slice *slices, int *nslices);
178static int mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca,
179 u_int part);
124
180
125#define MMCSD_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
126#define MMCSD_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
127#define MMCSD_LOCK_INIT(_sc) \
128 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
129 "mmcsd", MTX_DEF)
130#define MMCSD_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
131#define MMCSD_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
132#define MMCSD_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
181#define MMCSD_PART_LOCK(_part) mtx_lock(&(_part)->part_mtx)
182#define MMCSD_PART_UNLOCK(_part) mtx_unlock(&(_part)->part_mtx)
183#define MMCSD_PART_LOCK_INIT(_part) \
184 mtx_init(&(_part)->part_mtx, (_part)->name, "mmcsd part", MTX_DEF)
185#define MMCSD_PART_LOCK_DESTROY(_part) mtx_destroy(&(_part)->part_mtx);
186#define MMCSD_PART_ASSERT_LOCKED(_part) \
187 mtx_assert(&(_part)->part_mtx, MA_OWNED);
188#define MMCSD_PART_ASSERT_UNLOCKED(_part) \
189 mtx_assert(&(_part)->part_mtx, MA_NOTOWNED);
133
134static int
135mmcsd_probe(device_t dev)
136{
137
138 device_quiet(dev);
139 device_set_desc(dev, "MMC/SD Memory Card");
140 return (0);
141}
142
143static int
144mmcsd_attach(device_t dev)
145{
190
191static int
192mmcsd_probe(device_t dev)
193{
194
195 device_quiet(dev);
196 device_set_desc(dev, "MMC/SD Memory Card");
197 return (0);
198}
199
200static int
201mmcsd_attach(device_t dev)
202{
203 device_t mmcbr;
146 struct mmcsd_softc *sc;
204 struct mmcsd_softc *sc;
147 struct disk *d;
148 intmax_t mb;
149 uint32_t speed;
150 uint32_t maxblocks;
151 char unit;
205 const uint8_t *ext_csd;
206 off_t erase_size, sector_size, size, wp_size;
207 uintmax_t bytes;
208 int err, i;
209 uint8_t rev;
210 bool comp, ro;
211 char unit[2];
152
153 sc = device_get_softc(dev);
154 sc->dev = dev;
212
213 sc = device_get_softc(dev);
214 sc->dev = dev;
155 MMCSD_LOCK_INIT(sc);
215 sc->mmcbr = mmcbr = device_get_parent(dev);
216 sc->mode = mmcbr_get_mode(mmcbr);
217 sc->rca = mmc_get_rca(dev);
156
218
157 d = sc->disk = disk_alloc();
158 d->d_open = mmcsd_open;
159 d->d_close = mmcsd_close;
160 d->d_strategy = mmcsd_strategy;
161 d->d_dump = mmcsd_dump;
162 d->d_name = "mmcsd";
163 d->d_drv1 = sc;
164 d->d_sectorsize = mmc_get_sector_size(dev);
165 d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize;
166 d->d_mediasize = (off_t)mmc_get_media_size(dev) * d->d_sectorsize;
167 d->d_stripesize = mmc_get_erase_sector(dev) * d->d_sectorsize;
168 d->d_unit = device_get_unit(dev);
169 d->d_flags = DISKFLAG_CANDELETE;
170 d->d_delmaxsize = mmc_get_erase_sector(dev) * d->d_sectorsize;
171 strlcpy(d->d_ident, mmc_get_card_sn_string(dev), sizeof(d->d_ident));
172 strlcpy(d->d_descr, mmc_get_card_id_string(dev), sizeof(d->d_descr));
173 d->d_rotation_rate = DISK_RR_NON_ROTATING;
219 /* Only MMC >= 4.x devices support EXT_CSD. */
220 if (mmc_get_spec_vers(dev) >= 4) {
221 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
222 err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
223 MMCBUS_RELEASE_BUS(mmcbr, dev);
224 if (err != MMC_ERR_NONE)
225 bzero(sc->ext_csd, sizeof(sc->ext_csd));
226 }
227 ext_csd = sc->ext_csd;
174
175 /*
228
229 /*
176 * Display in most natural units. There's no cards < 1MB. The SD
177 * standard goes to 2GiB due to its reliance on FAT, but the data
178 * format supports up to 4GiB and some card makers push it up to this
179 * limit. The SDHC standard only goes to 32GiB due to FAT32, but the
180 * data format supports up to 2TiB however. 2048GB isn't too ugly, so
181 * we note it in passing here and don't add the code to print
182 * TB). Since these cards are sold in terms of MB and GB not MiB and
183 * GiB, report them like that. We also round to the nearest unit, since
184 * many cards are a few percent short, even of the power of 10 size.
230 * Enhanced user data area and general purpose partitions are only
231 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
232 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
185 */
233 */
186 mb = (d->d_mediasize + 1000000 / 2 - 1) / 1000000;
187 unit = 'M';
188 if (mb >= 1000) {
189 unit = 'G';
190 mb = (mb + 1000 / 2 - 1) / 1000;
234 rev = ext_csd[EXT_CSD_REV];
235
236 /*
237 * Ignore user-creatable enhanced user data area and general purpose
238 * partitions partitions as long as partitioning hasn't been finished.
239 */
240 comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
241
242 /*
243 * Add enhanced user data area slice, unless it spans the entirety of
244 * the user data area. The enhanced area is of a multiple of high
245 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
246 * 512 KB) and its offset given in either sectors or bytes, depending
247 * on whether it's a high capacity device or not.
248 * NB: The slicer and its slices need to be registered before adding
249 * the disk for the corresponding user data area as re-tasting is
250 * racy.
251 */
252 sector_size = mmc_get_sector_size(dev);
253 size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
254 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
255 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
256 if (rev >= 4 && comp == TRUE && size > 0 &&
257 (ext_csd[EXT_CSD_PART_SUPPORT] &
258 EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
259 (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
260 erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
261 MMC_SECTOR_SIZE;
262 wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
263 size *= erase_size * wp_size;
264 if (size != mmc_get_media_size(dev) * sector_size) {
265 sc->enh_size = size;
266 sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
267 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
268 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
269 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
270 (mmc_get_high_cap(dev) ? MMC_SECTOR_SIZE : 1);
271 } else if (bootverbose)
272 device_printf(dev,
273 "enhanced user data area spans entire device\n");
191 }
274 }
275
192 /*
276 /*
193 * Report the clock speed of the underlying hardware, which might be
194 * different than what the card reports due to hardware limitations.
195 * Report how many blocks the hardware transfers at once.
277 * Add default partition. This may be the only one or the user
278 * data area in case partitions are supported.
196 */
279 */
197 speed = mmcbr_get_clock(device_get_parent(dev));
198 maxblocks = mmc_get_max_data(dev);
199 device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
200 mb, unit, d->d_descr,
201 mmc_get_read_only(dev) ? " (read-only)" : "",
202 device_get_nameunit(device_get_parent(dev)),
203 speed / 1000000, (speed / 100000) % 10,
204 mmcsd_bus_bit_width(dev), maxblocks);
205 disk_create(d, DISK_VERSION);
206 bioq_init(&sc->bio_queue);
280 ro = mmc_get_read_only(dev);
281 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "mmcsd",
282 device_get_unit(dev), mmc_get_media_size(dev) * sector_size,
283 mmc_get_erase_sector(dev) * sector_size, ro);
207
284
208 sc->running = 1;
209 sc->suspend = 0;
210 sc->eblock = sc->eend = 0;
211 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
212 device_get_nameunit(dev));
285 if (mmc_get_spec_vers(dev) < 3)
286 return (0);
213
287
288 /* Belatedly announce enhanced user data slice. */
289 if (sc->enh_size != 0) {
290 bytes = mmcsd_pretty_size(size, unit);
291 printf(FLASH_SLICES_FMT ": %ju%sB enhanced user data area "
292 "slice offset 0x%jx at %s\n", device_get_nameunit(dev),
293 MMCSD_LABEL_ENH, bytes, unit, (uintmax_t)sc->enh_base,
294 device_get_nameunit(dev));
295 }
296
297 /*
298 * Determine partition switch timeout (provided in units of 10 ms)
299 * and ensure it's at least 300 ms as some eMMC chips lie.
300 */
301 sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
302 300 * 1000);
303
304 /* Add boot partitions, which are of a fixed multiple of 128 KB. */
305 size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
306 if (size > 0 && (mmcbr_get_caps(mmcbr) & MMC_CAP_BOOT_NOACC) == 0) {
307 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT0,
308 MMCSD_FMT_BOOT, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE,
309 ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
310 EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
311 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_BOOT1,
312 MMCSD_FMT_BOOT, 1, size, MMC_BOOT_RPMB_BLOCK_SIZE,
313 ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
314 EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
315 }
316
317 /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
318 size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
319 if (rev >= 5 && size > 0)
320 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_RPMB,
321 MMCSD_FMT_RPMB, 0, size, MMC_BOOT_RPMB_BLOCK_SIZE, ro);
322
323 if (rev <= 3 || comp == FALSE)
324 return (0);
325
326 /*
327 * Add general purpose partitions, which are of a multiple of high
328 * capacity write protect groups, too.
329 */
330 if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
331 erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
332 MMC_SECTOR_SIZE;
333 wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
334 for (i = 0; i < MMC_PART_GP_MAX; i++) {
335 size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
336 (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
337 (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
338 if (size == 0)
339 continue;
340 mmcsd_add_part(sc, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
341 MMCSD_FMT_GP, i, size * erase_size * wp_size,
342 erase_size, ro);
343 }
344 }
214 return (0);
215}
216
345 return (0);
346}
347
348static uintmax_t
349mmcsd_pretty_size(off_t size, char *unit)
350{
351 uintmax_t bytes;
352 int i;
353
354 /*
355 * Display in most natural units. There's no card < 1MB. However,
356 * RPMB partitions occasionally are smaller than that, though. The
357 * SD standard goes to 2 GiB due to its reliance on FAT, but the data
358 * format supports up to 4 GiB and some card makers push it up to this
359 * limit. The SDHC standard only goes to 32 GiB due to FAT32, but the
360 * data format supports up to 2 TiB however. 2048 GB isn't too ugly,
361 * so we note it in passing here and don't add the code to print TB).
362 * Since these cards are sold in terms of MB and GB not MiB and GiB,
363 * report them like that. We also round to the nearest unit, since
364 * many cards are a few percent short, even of the power of 10 size.
365 */
366 bytes = size;
367 unit[0] = unit[1] = '\0';
368 for (i = 0; i <= 2 && bytes >= 1000; i++) {
369 bytes = (bytes + 1000 / 2 - 1) / 1000;
370 switch (i) {
371 case 0:
372 unit[0] = 'k';
373 break;
374 case 1:
375 unit[0] = 'M';
376 break;
377 case 2:
378 unit[0] = 'G';
379 break;
380 default:
381 break;
382 }
383 }
384 return (bytes);
385}
386
387static struct cdevsw mmcsd_rpmb_cdevsw = {
388 .d_version = D_VERSION,
389 .d_name = "mmcsdrpmb",
390 .d_ioctl = mmcsd_ioctl_rpmb
391};
392
393static void
394mmcsd_add_part(struct mmcsd_softc *sc, u_int type, const char *name, u_int cnt,
395 off_t media_size, off_t erase_size, bool ro)
396{
397 struct make_dev_args args;
398 device_t dev, mmcbr;
399 const char *ext;
400 const uint8_t *ext_csd;
401 struct mmcsd_part *part;
402 struct disk *d;
403 uintmax_t bytes;
404 u_int gp;
405 uint32_t speed;
406 uint8_t extattr;
407 bool enh;
408 char unit[2];
409
410 dev = sc->dev;
411 mmcbr = sc->mmcbr;
412 part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
413 M_WAITOK | M_ZERO);
414 part->sc = sc;
415 part->cnt = cnt;
416 part->type = type;
417 part->ro = ro;
418 snprintf(part->name, sizeof(part->name), name, device_get_unit(dev));
419
420 /* For the RPMB partition, allow IOCTL access only. */
421 if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
422 make_dev_args_init(&args);
423 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
424 args.mda_devsw = &mmcsd_rpmb_cdevsw;
425 args.mda_uid = UID_ROOT;
426 args.mda_gid = GID_OPERATOR;
427 args.mda_mode = 0640;
428 args.mda_si_drv1 = part;
429 if (make_dev_s(&args, &sc->rpmb_dev, "%s", part->name) != 0) {
430 device_printf(dev, "Failed to make RPMB device\n");
431 free(part, M_DEVBUF);
432 return;
433 }
434 } else {
435 MMCSD_PART_LOCK_INIT(part);
436
437 d = part->disk = disk_alloc();
438 d->d_open = mmcsd_open;
439 d->d_close = mmcsd_close;
440 d->d_strategy = mmcsd_strategy;
441 d->d_ioctl = mmcsd_ioctl_disk;
442 d->d_dump = mmcsd_dump;
443 d->d_getattr = mmcsd_getattr;
444 d->d_name = part->name;
445 d->d_drv1 = part;
446 d->d_sectorsize = mmc_get_sector_size(dev);
447 d->d_maxsize = mmc_get_max_data(dev) * d->d_sectorsize;
448 d->d_mediasize = media_size;
449 d->d_stripesize = erase_size;
450 d->d_unit = cnt;
451 d->d_flags = DISKFLAG_CANDELETE;
452 d->d_delmaxsize = erase_size;
453 strlcpy(d->d_ident, mmc_get_card_sn_string(dev),
454 sizeof(d->d_ident));
455 strlcpy(d->d_descr, mmc_get_card_id_string(dev),
456 sizeof(d->d_descr));
457 d->d_rotation_rate = DISK_RR_NON_ROTATING;
458
459 disk_create(d, DISK_VERSION);
460 bioq_init(&part->bio_queue);
461
462 part->running = 1;
463 kproc_create(&mmcsd_task, part, &part->p, 0, 0,
464 "%s%d: mmc/sd card", part->name, cnt);
465 }
466
467 bytes = mmcsd_pretty_size(media_size, unit);
468 if (type == EXT_CSD_PART_CONFIG_ACC_DEFAULT) {
469 speed = mmcbr_get_clock(mmcbr);
470 printf("%s%d: %ju%sB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
471 part->name, cnt, bytes, unit, mmc_get_card_id_string(dev),
472 ro ? " (read-only)" : "", device_get_nameunit(mmcbr),
473 speed / 1000000, (speed / 100000) % 10,
474 mmcsd_bus_bit_width(dev), mmc_get_max_data(dev));
475 } else if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
476 printf("%s: %ju%sB partion %d%s at %s\n", part->name, bytes,
477 unit, type, ro ? " (read-only)" : "",
478 device_get_nameunit(dev));
479 } else {
480 enh = false;
481 ext = NULL;
482 extattr = 0;
483 if (type >= EXT_CSD_PART_CONFIG_ACC_GP0 &&
484 type <= EXT_CSD_PART_CONFIG_ACC_GP3) {
485 ext_csd = sc->ext_csd;
486 gp = type - EXT_CSD_PART_CONFIG_ACC_GP0;
487 if ((ext_csd[EXT_CSD_PART_SUPPORT] &
488 EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
489 (ext_csd[EXT_CSD_PART_ATTR] &
490 (EXT_CSD_PART_ATTR_ENH_GP0 << gp)) != 0)
491 enh = true;
492 else if ((ext_csd[EXT_CSD_PART_SUPPORT] &
493 EXT_CSD_PART_SUPPORT_EXT_ATTR_EN) != 0) {
494 extattr = (ext_csd[EXT_CSD_EXT_PART_ATTR +
495 (gp / 2)] >> (4 * (gp % 2))) & 0xF;
496 switch (extattr) {
497 case EXT_CSD_EXT_PART_ATTR_DEFAULT:
498 break;
499 case EXT_CSD_EXT_PART_ATTR_SYSTEMCODE:
500 ext = "system code";
501 break;
502 case EXT_CSD_EXT_PART_ATTR_NPERSISTENT:
503 ext = "non-persistent";
504 break;
505 default:
506 ext = "reserved";
507 break;
508 }
509 }
510 }
511 if (ext == NULL)
512 printf("%s%d: %ju%sB partion %d%s%s at %s\n",
513 part->name, cnt, bytes, unit, type, enh ?
514 " enhanced" : "", ro ? " (read-only)" : "",
515 device_get_nameunit(dev));
516 else
517 printf("%s%d: %ju%sB partion %d extended 0x%x "
518 "(%s)%s at %s\n", part->name, cnt, bytes, unit,
519 type, extattr, ext, ro ? " (read-only)" : "",
520 device_get_nameunit(dev));
521 }
522}
523
217static int
524static int
525mmcsd_slicer(device_t dev, const char *provider,
526 struct flash_slice *slices, int *nslices)
527{
528 char name[MMCSD_PART_NAMELEN];
529 struct mmcsd_softc *sc;
530 struct mmcsd_part *part;
531
532 *nslices = 0;
533 if (slices == NULL)
534 return (ENOMEM);
535
536 sc = device_get_softc(dev);
537 if (sc->enh_size == 0)
538 return (ENXIO);
539
540 part = sc->part[EXT_CSD_PART_CONFIG_ACC_DEFAULT];
541 snprintf(name, sizeof(name), "%s%d", part->disk->d_name,
542 part->disk->d_unit);
543 if (strcmp(name, provider) != 0)
544 return (ENXIO);
545
546 *nslices = 1;
547 slices[0].base = sc->enh_base;
548 slices[0].size = sc->enh_size;
549 slices[0].label = MMCSD_LABEL_ENH;
550 return (0);
551}
552
553static int
218mmcsd_detach(device_t dev)
219{
220 struct mmcsd_softc *sc = device_get_softc(dev);
554mmcsd_detach(device_t dev)
555{
556 struct mmcsd_softc *sc = device_get_softc(dev);
557 struct mmcsd_part *part;
558 int i;
221
559
222 MMCSD_LOCK(sc);
223 sc->suspend = 0;
224 if (sc->running > 0) {
225 /* kill thread */
226 sc->running = 0;
227 wakeup(sc);
228 /* wait for thread to finish. */
229 while (sc->running != -1)
230 msleep(sc, &sc->sc_mtx, 0, "detach", 0);
560 for (i = 0; i < MMC_PART_MAX; i++) {
561 part = sc->part[i];
562 if (part != NULL && part->disk != NULL) {
563 MMCSD_PART_LOCK(part);
564 part->suspend = 0;
565 if (part->running > 0) {
566 /* kill thread */
567 part->running = 0;
568 wakeup(part);
569 /* wait for thread to finish. */
570 while (part->running != -1)
571 msleep(part, &part->part_mtx, 0,
572 "detach", 0);
573 }
574 MMCSD_PART_UNLOCK(part);
575 }
231 }
576 }
232 MMCSD_UNLOCK(sc);
233
577
234 /* Flush the request queue. */
235 bioq_flush(&sc->bio_queue, NULL, ENXIO);
236 /* kill disk */
237 disk_destroy(sc->disk);
578 if (sc->rpmb_dev != NULL)
579 destroy_dev(sc->rpmb_dev);
238
580
239 MMCSD_LOCK_DESTROY(sc);
581 for (i = 0; i < MMC_PART_MAX; i++) {
582 part = sc->part[i];
583 if (part != NULL) {
584 if (part->disk != NULL) {
585 /* Flush the request queue. */
586 bioq_flush(&part->bio_queue, NULL, ENXIO);
587 /* kill disk */
588 disk_destroy(part->disk);
240
589
590 MMCSD_PART_LOCK_DESTROY(part);
591 }
592 free(part, M_DEVBUF);
593 }
594 }
241 return (0);
242}
243
244static int
245mmcsd_suspend(device_t dev)
246{
247 struct mmcsd_softc *sc = device_get_softc(dev);
595 return (0);
596}
597
598static int
599mmcsd_suspend(device_t dev)
600{
601 struct mmcsd_softc *sc = device_get_softc(dev);
602 struct mmcsd_part *part;
603 int i;
248
604
249 MMCSD_LOCK(sc);
250 sc->suspend = 1;
251 if (sc->running > 0) {
252 /* kill thread */
253 sc->running = 0;
254 wakeup(sc);
255 /* wait for thread to finish. */
256 while (sc->running != -1)
257 msleep(sc, &sc->sc_mtx, 0, "detach", 0);
605 for (i = 0; i < MMC_PART_MAX; i++) {
606 part = sc->part[i];
607 if (part != NULL && part->disk != NULL) {
608 MMCSD_PART_LOCK(part);
609 part->suspend = 1;
610 if (part->running > 0) {
611 /* kill thread */
612 part->running = 0;
613 wakeup(part);
614 /* wait for thread to finish. */
615 while (part->running != -1)
616 msleep(part, &part->part_mtx, 0,
617 "detach", 0);
618 }
619 MMCSD_PART_UNLOCK(part);
620 }
258 }
621 }
259 MMCSD_UNLOCK(sc);
260 return (0);
261}
262
263static int
264mmcsd_resume(device_t dev)
265{
266 struct mmcsd_softc *sc = device_get_softc(dev);
622 return (0);
623}
624
625static int
626mmcsd_resume(device_t dev)
627{
628 struct mmcsd_softc *sc = device_get_softc(dev);
629 struct mmcsd_part *part;
630 int i;
267
631
268 MMCSD_LOCK(sc);
269 sc->suspend = 0;
270 if (sc->running <= 0) {
271 sc->running = 1;
272 MMCSD_UNLOCK(sc);
273 kproc_create(&mmcsd_task, sc, &sc->p, 0, 0, "%s: mmc/sd card",
274 device_get_nameunit(dev));
275 } else
276 MMCSD_UNLOCK(sc);
632 for (i = 0; i < MMC_PART_MAX; i++) {
633 part = sc->part[i];
634 if (part != NULL && part->disk != NULL) {
635 MMCSD_PART_LOCK(part);
636 part->suspend = 0;
637 if (part->running <= 0) {
638 part->running = 1;
639 kproc_create(&mmcsd_task, part, &part->p, 0, 0,
640 "%s%d: mmc/sd card", part->name, part->cnt);
641 MMCSD_PART_UNLOCK(part);
642 } else
643 MMCSD_PART_UNLOCK(part);
644 }
645 }
277 return (0);
278}
279
280static int
646 return (0);
647}
648
649static int
281mmcsd_open(struct disk *dp)
650mmcsd_open(struct disk *dp __unused)
282{
283
284 return (0);
285}
286
287static int
651{
652
653 return (0);
654}
655
656static int
288mmcsd_close(struct disk *dp)
657mmcsd_close(struct disk *dp __unused)
289{
290
291 return (0);
292}
293
294static void
295mmcsd_strategy(struct bio *bp)
296{
297 struct mmcsd_softc *sc;
658{
659
660 return (0);
661}
662
663static void
664mmcsd_strategy(struct bio *bp)
665{
666 struct mmcsd_softc *sc;
667 struct mmcsd_part *part;
298
668
299 sc = (struct mmcsd_softc *)bp->bio_disk->d_drv1;
300 MMCSD_LOCK(sc);
301 if (sc->running > 0 || sc->suspend > 0) {
302 bioq_disksort(&sc->bio_queue, bp);
303 MMCSD_UNLOCK(sc);
304 wakeup(sc);
669 part = bp->bio_disk->d_drv1;
670 sc = part->sc;
671 MMCSD_PART_LOCK(part);
672 if (part->running > 0 || part->suspend > 0) {
673 bioq_disksort(&part->bio_queue, bp);
674 MMCSD_PART_UNLOCK(part);
675 wakeup(part);
305 } else {
676 } else {
306 MMCSD_UNLOCK(sc);
677 MMCSD_PART_UNLOCK(part);
307 biofinish(bp, NULL, ENXIO);
308 }
309}
310
678 biofinish(bp, NULL, ENXIO);
679 }
680}
681
682static int
683mmcsd_ioctl_rpmb(struct cdev *dev, u_long cmd, caddr_t data,
684 int fflag, struct thread *td __unused)
685{
686
687 return (mmcsd_ioctl(dev->si_drv1, cmd, data, fflag));
688}
689
690static int
691mmcsd_ioctl_disk(struct disk *disk, u_long cmd, void *data, int fflag,
692 struct thread *td __unused)
693{
694
695 return (mmcsd_ioctl(disk->d_drv1, cmd, data, fflag));
696}
697
698static int
699mmcsd_ioctl(struct mmcsd_part *part, u_long cmd, void *data, int fflag)
700{
701 struct mmc_ioc_cmd *mic;
702 struct mmc_ioc_multi_cmd *mimc;
703 int i, err;
704 u_long cnt, size;
705
706 if ((fflag & FREAD) == 0)
707 return (EBADF);
708
709 err = 0;
710 switch (cmd) {
711 case MMC_IOC_CMD:
712 mic = data;
713 err = mmcsd_ioctl_cmd(part, data, fflag);
714 break;
715 case MMC_IOC_CMD_MULTI:
716 mimc = data;
717 if (mimc->num_of_cmds == 0)
718 break;
719 if (mimc->num_of_cmds > MMC_IOC_MAX_CMDS)
720 return (EINVAL);
721 cnt = mimc->num_of_cmds;
722 size = sizeof(*mic) * cnt;
723 mic = malloc(size, M_TEMP, M_WAITOK);
724 err = copyin((const void *)mimc->cmds, mic, size);
725 if (err != 0)
726 break;
727 for (i = 0; i < cnt; i++) {
728 err = mmcsd_ioctl_cmd(part, &mic[i], fflag);
729 if (err != 0)
730 break;
731 }
732 free(mic, M_TEMP);
733 break;
734 default:
735 return (ENOIOCTL);
736 }
737 return (err);
738}
739
740static int
741mmcsd_ioctl_cmd(struct mmcsd_part *part, struct mmc_ioc_cmd *mic, int fflag)
742{
743 struct mmc_command cmd;
744 struct mmc_data data;
745 struct mmcsd_softc *sc;
746 device_t dev, mmcbr;
747 void *dp;
748 u_long len;
749 int err, retries;
750 uint32_t status;
751 uint16_t rca;
752
753 if ((fflag & FWRITE) == 0 && mic->write_flag != 0)
754 return (EBADF);
755
756 if (part->ro == TRUE && mic->write_flag != 0)
757 return (EROFS);
758
759 err = 0;
760 dp = NULL;
761 len = mic->blksz * mic->blocks;
762 if (len > MMC_IOC_MAX_BYTES)
763 return (EOVERFLOW);
764 if (len != 0) {
765 dp = malloc(len, M_TEMP, M_WAITOK);
766 err = copyin((void *)(uintptr_t)mic->data_ptr, dp, len);
767 if (err != 0)
768 goto out;
769 }
770 memset(&cmd, 0, sizeof(cmd));
771 memset(&data, 0, sizeof(data));
772 cmd.opcode = mic->opcode;
773 cmd.arg = mic->arg;
774 cmd.flags = mic->flags;
775 if (len != 0) {
776 data.len = len;
777 data.data = dp;
778 data.flags = mic->write_flag != 0 ? MMC_DATA_WRITE :
779 MMC_DATA_READ;
780 cmd.data = &data;
781 }
782 sc = part->sc;
783 rca = sc->rca;
784 if (mic->is_acmd == 0) {
785 /* Enforce/patch/restrict RCA-based commands */
786 switch (cmd.opcode) {
787 case MMC_SET_RELATIVE_ADDR:
788 case MMC_SELECT_CARD:
789 err = EPERM;
790 goto out;
791 case MMC_STOP_TRANSMISSION:
792 if ((cmd.arg & 0x1) == 0)
793 break;
794 /* FALLTHROUGH */
795 case MMC_SLEEP_AWAKE:
796 case MMC_SEND_CSD:
797 case MMC_SEND_CID:
798 case MMC_SEND_STATUS:
799 case MMC_GO_INACTIVE_STATE:
800 case MMC_FAST_IO:
801 case MMC_APP_CMD:
802 cmd.arg = (cmd.arg & 0x0000FFFF) | (rca << 16);
803 break;
804 default:
805 break;
806 }
807 }
808 dev = sc->dev;
809 mmcbr = sc->mmcbr;
810 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
811 err = mmcsd_switch_part(mmcbr, dev, rca, part->type);
812 if (err != MMC_ERR_NONE)
813 goto release;
814 if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
815 err = mmcsd_set_blockcount(sc, mic->blocks,
816 mic->write_flag & (1 << 31));
817 if (err != MMC_ERR_NONE)
818 goto release;
819 }
820 if (mic->is_acmd != 0)
821 (void)mmc_wait_for_app_cmd(mmcbr, dev, rca, &cmd, 0);
822 else
823 (void)mmc_wait_for_cmd(mmcbr, dev, &cmd, 0);
824 if (part->type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
825 /*
826 * If the request went to the RPMB partition, try to ensure
827 * that the command actually has completed ...
828 */
829 retries = MMCSD_CMD_RETRIES;
830 do {
831 err = mmc_send_status(mmcbr, dev, rca, &status);
832 if (err != MMC_ERR_NONE)
833 break;
834 if (R1_STATUS(status) == 0 &&
835 R1_CURRENT_STATE(status) != R1_STATE_PRG)
836 break;
837 DELAY(1000);
838 } while (retries-- > 0);
839
840 /* ... and always switch back to the default partition. */
841 err = mmcsd_switch_part(mmcbr, dev, rca,
842 EXT_CSD_PART_CONFIG_ACC_DEFAULT);
843 if (err != MMC_ERR_NONE)
844 goto release;
845 }
846 /*
847 * If EXT_CSD was changed, our copy is outdated now. Specifically,
848 * the upper bits of EXT_CSD_PART_CONFIG used in mmcsd_switch_part(),
849 * so retrieve EXT_CSD again.
850 */
851 if (cmd.opcode == MMC_SWITCH_FUNC) {
852 err = mmc_send_ext_csd(mmcbr, dev, sc->ext_csd);
853 if (err != MMC_ERR_NONE)
854 goto release;
855 }
856 MMCBUS_RELEASE_BUS(mmcbr, dev);
857 if (cmd.error != MMC_ERR_NONE) {
858 switch (cmd.error) {
859 case MMC_ERR_TIMEOUT:
860 err = ETIMEDOUT;
861 break;
862 case MMC_ERR_BADCRC:
863 err = EILSEQ;
864 break;
865 case MMC_ERR_INVALID:
866 err = EINVAL;
867 break;
868 case MMC_ERR_NO_MEMORY:
869 err = ENOMEM;
870 break;
871 default:
872 err = EIO;
873 break;
874 }
875 goto out;
876 }
877 memcpy(mic->response, cmd.resp, 4 * sizeof(uint32_t));
878 if (mic->write_flag == 0 && len != 0) {
879 err = copyout(dp, (void *)(uintptr_t)mic->data_ptr, len);
880 if (err != 0)
881 goto out;
882 }
883 goto out;
884
885release:
886 MMCBUS_RELEASE_BUS(mmcbr, dev);
887 err = EIO;
888
889out:
890 if (dp != NULL)
891 free(dp, M_TEMP);
892 return (err);
893}
894
895static int
896mmcsd_getattr(struct bio *bp)
897{
898 struct mmcsd_part *part;
899 device_t dev;
900
901 if (strcmp(bp->bio_attribute, "MMC::device") == 0) {
902 if (bp->bio_length != sizeof(dev))
903 return (EFAULT);
904 part = bp->bio_disk->d_drv1;
905 dev = part->sc->dev;
906 bcopy(&dev, bp->bio_data, sizeof(dev));
907 bp->bio_completed = bp->bio_length;
908 return (0);
909 }
910 return (-1);
911}
912
913static int
914mmcsd_set_blockcount(struct mmcsd_softc *sc, u_int count, bool reliable)
915{
916 struct mmc_command cmd;
917 struct mmc_request req;
918
919 memset(&req, 0, sizeof(req));
920 memset(&cmd, 0, sizeof(cmd));
921 cmd.mrq = &req;
922 req.cmd = &cmd;
923 cmd.opcode = MMC_SET_BLOCK_COUNT;
924 cmd.arg = count & 0x0000FFFF;
925 if (reliable)
926 cmd.arg |= 1 << 31;
927 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
928 MMCBUS_WAIT_FOR_REQUEST(sc->mmcbr, sc->dev, &req);
929 return (cmd.error);
930}
931
932static int
933mmcsd_switch_part(device_t bus, device_t dev, uint16_t rca, u_int part)
934{
935 struct mmcsd_softc *sc;
936 int err;
937 uint8_t value;
938
939 sc = device_get_softc(dev);
940
941 if (sc->part_curr == part)
942 return (MMC_ERR_NONE);
943
944 if (sc->mode == mode_sd)
945 return (MMC_ERR_NONE);
946
947 value = (sc->ext_csd[EXT_CSD_PART_CONFIG] &
948 ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
949 /* Jump! */
950 err = mmc_switch(bus, dev, rca, EXT_CSD_CMD_SET_NORMAL,
951 EXT_CSD_PART_CONFIG, value, sc->part_time, true);
952 if (err != MMC_ERR_NONE)
953 return (err);
954
955 sc->ext_csd[EXT_CSD_PART_CONFIG] = value;
956 sc->part_curr = part;
957 return (MMC_ERR_NONE);
958}
959
311static const char *
312mmcsd_errmsg(int e)
313{
960static const char *
961mmcsd_errmsg(int e)
962{
963
314 if (e < 0 || e > MMC_ERR_MAX)
315 return "Bad error code";
316 return errmsg[e];
317}
318
319static daddr_t
964 if (e < 0 || e > MMC_ERR_MAX)
965 return "Bad error code";
966 return errmsg[e];
967}
968
969static daddr_t
320mmcsd_rw(struct mmcsd_softc *sc, struct bio *bp)
970mmcsd_rw(struct mmcsd_part *part, struct bio *bp)
321{
322 daddr_t block, end;
323 struct mmc_command cmd;
324 struct mmc_command stop;
325 struct mmc_request req;
326 struct mmc_data data;
971{
972 daddr_t block, end;
973 struct mmc_command cmd;
974 struct mmc_command stop;
975 struct mmc_request req;
976 struct mmc_data data;
327 device_t dev = sc->dev;
328 int sz = sc->disk->d_sectorsize;
329 device_t mmcbr = device_get_parent(dev);
977 struct mmcsd_softc *sc;
978 device_t dev, mmcbr;
979 int numblocks, sz;
980 char *vaddr;
330
981
982 sc = part->sc;
983 dev = sc->dev;
984 mmcbr = sc->mmcbr;
985
331 block = bp->bio_pblkno;
986 block = bp->bio_pblkno;
987 sz = part->disk->d_sectorsize;
332 end = bp->bio_pblkno + (bp->bio_bcount / sz);
333 while (block < end) {
988 end = bp->bio_pblkno + (bp->bio_bcount / sz);
989 while (block < end) {
334 char *vaddr = bp->bio_data +
335 (block - bp->bio_pblkno) * sz;
336 int numblocks = min(end - block, mmc_get_max_data(dev));
990 vaddr = bp->bio_data + (block - bp->bio_pblkno) * sz;
991 numblocks = min(end - block, mmc_get_max_data(dev));
337 memset(&req, 0, sizeof(req));
992 memset(&req, 0, sizeof(req));
338 memset(&cmd, 0, sizeof(cmd));
993 memset(&cmd, 0, sizeof(cmd));
339 memset(&stop, 0, sizeof(stop));
340 memset(&data, 0, sizeof(data));
341 cmd.mrq = &req;
342 req.cmd = &cmd;
343 cmd.data = &data;
344 if (bp->bio_cmd == BIO_READ) {
345 if (numblocks > 1)
346 cmd.opcode = MMC_READ_MULTIPLE_BLOCK;

--- 21 unchanged lines hidden (view full) ---

368 stop.opcode = MMC_STOP_TRANSMISSION;
369 stop.arg = 0;
370 stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
371 stop.mrq = &req;
372 req.stop = &stop;
373 }
374 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
375 if (req.cmd->error != MMC_ERR_NONE) {
994 memset(&stop, 0, sizeof(stop));
995 memset(&data, 0, sizeof(data));
996 cmd.mrq = &req;
997 req.cmd = &cmd;
998 cmd.data = &data;
999 if (bp->bio_cmd == BIO_READ) {
1000 if (numblocks > 1)
1001 cmd.opcode = MMC_READ_MULTIPLE_BLOCK;

--- 21 unchanged lines hidden (view full) ---

1023 stop.opcode = MMC_STOP_TRANSMISSION;
1024 stop.arg = 0;
1025 stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1026 stop.mrq = &req;
1027 req.stop = &stop;
1028 }
1029 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1030 if (req.cmd->error != MMC_ERR_NONE) {
376 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
1031 if (ppsratecheck(&sc->log_time, &sc->log_count,
1032 LOG_PPS))
377 device_printf(dev, "Error indicated: %d %s\n",
1033 device_printf(dev, "Error indicated: %d %s\n",
378 req.cmd->error, mmcsd_errmsg(req.cmd->error));
379 }
1034 req.cmd->error,
1035 mmcsd_errmsg(req.cmd->error));
380 break;
381 }
382 block += numblocks;
383 }
384 return (block);
385}
386
387static daddr_t
1036 break;
1037 }
1038 block += numblocks;
1039 }
1040 return (block);
1041}
1042
1043static daddr_t
388mmcsd_delete(struct mmcsd_softc *sc, struct bio *bp)
1044mmcsd_delete(struct mmcsd_part *part, struct bio *bp)
389{
390 daddr_t block, end, start, stop;
391 struct mmc_command cmd;
392 struct mmc_request req;
1045{
1046 daddr_t block, end, start, stop;
1047 struct mmc_command cmd;
1048 struct mmc_request req;
393 device_t dev = sc->dev;
394 int sz = sc->disk->d_sectorsize;
395 int erase_sector;
396 device_t mmcbr = device_get_parent(dev);
1049 struct mmcsd_softc *sc;
1050 device_t dev, mmcbr;
1051 int erase_sector, sz;
397
1052
1053 sc = part->sc;
1054 dev = sc->dev;
1055 mmcbr = sc->mmcbr;
1056
398 block = bp->bio_pblkno;
1057 block = bp->bio_pblkno;
1058 sz = part->disk->d_sectorsize;
399 end = bp->bio_pblkno + (bp->bio_bcount / sz);
400 /* Coalesce with part remaining from previous request. */
1059 end = bp->bio_pblkno + (bp->bio_bcount / sz);
1060 /* Coalesce with part remaining from previous request. */
401 if (block > sc->eblock && block <= sc->eend)
402 block = sc->eblock;
403 if (end >= sc->eblock && end < sc->eend)
404 end = sc->eend;
1061 if (block > part->eblock && block <= part->eend)
1062 block = part->eblock;
1063 if (end >= part->eblock && end < part->eend)
1064 end = part->eend;
405 /* Safe round to the erase sector boundaries. */
406 erase_sector = mmc_get_erase_sector(dev);
407 start = block + erase_sector - 1; /* Round up. */
408 start -= start % erase_sector;
409 stop = end; /* Round down. */
1065 /* Safe round to the erase sector boundaries. */
1066 erase_sector = mmc_get_erase_sector(dev);
1067 start = block + erase_sector - 1; /* Round up. */
1068 start -= start % erase_sector;
1069 stop = end; /* Round down. */
410 stop -= end % erase_sector;
411 /* We can't erase area smaller then sector, store it for later. */
1070 stop -= end % erase_sector;
1071 /* We can't erase an area smaller than a sector, store it for later. */
412 if (start >= stop) {
1072 if (start >= stop) {
413 sc->eblock = block;
414 sc->eend = end;
1073 part->eblock = block;
1074 part->eend = end;
415 return (end);
416 }
417
418 /* Set erase start position. */
419 memset(&req, 0, sizeof(req));
420 memset(&cmd, 0, sizeof(cmd));
421 cmd.mrq = &req;
422 req.cmd = &cmd;

--- 36 unchanged lines hidden (view full) ---

459 cmd.arg = 0;
460 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
461 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
462 if (req.cmd->error != MMC_ERR_NONE) {
463 printf("erase err3 %d\n", req.cmd->error);
464 return (block);
465 }
466 /* Store one of remaining parts for the next call. */
1075 return (end);
1076 }
1077
1078 /* Set erase start position. */
1079 memset(&req, 0, sizeof(req));
1080 memset(&cmd, 0, sizeof(cmd));
1081 cmd.mrq = &req;
1082 req.cmd = &cmd;

--- 36 unchanged lines hidden (view full) ---

1119 cmd.arg = 0;
1120 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1121 MMCBUS_WAIT_FOR_REQUEST(mmcbr, dev, &req);
1122 if (req.cmd->error != MMC_ERR_NONE) {
1123 printf("erase err3 %d\n", req.cmd->error);
1124 return (block);
1125 }
1126 /* Store one of remaining parts for the next call. */
467 if (bp->bio_pblkno >= sc->eblock || block == start) {
468 sc->eblock = stop; /* Predict next forward. */
469 sc->eend = end;
1127 if (bp->bio_pblkno >= part->eblock || block == start) {
1128 part->eblock = stop; /* Predict next forward. */
1129 part->eend = end;
470 } else {
1130 } else {
471 sc->eblock = block; /* Predict next backward. */
472 sc->eend = start;
1131 part->eblock = block; /* Predict next backward. */
1132 part->eend = start;
473 }
474 return (end);
475}
476
477static int
1133 }
1134 return (end);
1135}
1136
1137static int
478mmcsd_dump(void *arg, void *virtual, vm_offset_t physical,
479 off_t offset, size_t length)
1138mmcsd_dump(void *arg, void *virtual, vm_offset_t physical, off_t offset,
1139 size_t length)
480{
1140{
481 struct disk *disk = arg;
482 struct mmcsd_softc *sc = (struct mmcsd_softc *)disk->d_drv1;
483 device_t dev = sc->dev;
484 struct bio bp;
485 daddr_t block, end;
1141 struct bio bp;
1142 daddr_t block, end;
486 device_t mmcbr = device_get_parent(dev);
1143 struct disk *disk;
1144 struct mmcsd_softc *sc;
1145 struct mmcsd_part *part;
1146 device_t dev, mmcbr;
1147 int err;
487
488 /* length zero is special and really means flush buffers to media */
489 if (!length)
490 return (0);
491
1148
1149 /* length zero is special and really means flush buffers to media */
1150 if (!length)
1151 return (0);
1152
1153 disk = arg;
1154 part = disk->d_drv1;
1155 sc = part->sc;
1156 dev = sc->dev;
1157 mmcbr = sc->mmcbr;
1158
492 g_reset_bio(&bp);
493 bp.bio_disk = disk;
494 bp.bio_pblkno = offset / disk->d_sectorsize;
495 bp.bio_bcount = length;
496 bp.bio_data = virtual;
497 bp.bio_cmd = BIO_WRITE;
1159 g_reset_bio(&bp);
1160 bp.bio_disk = disk;
1161 bp.bio_pblkno = offset / disk->d_sectorsize;
1162 bp.bio_bcount = length;
1163 bp.bio_data = virtual;
1164 bp.bio_cmd = BIO_WRITE;
498 end = bp.bio_pblkno + bp.bio_bcount / sc->disk->d_sectorsize;
1165 end = bp.bio_pblkno + bp.bio_bcount / disk->d_sectorsize;
499 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
1166 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
500 block = mmcsd_rw(sc, &bp);
1167 err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
1168 if (err != MMC_ERR_NONE) {
1169 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS))
1170 device_printf(dev, "Partition switch error\n");
1171 MMCBUS_RELEASE_BUS(mmcbr, dev);
1172 return (EIO);
1173 }
1174 block = mmcsd_rw(part, &bp);
501 MMCBUS_RELEASE_BUS(mmcbr, dev);
502 return ((end < block) ? EIO : 0);
503}
504
505static void
506mmcsd_task(void *arg)
507{
1175 MMCBUS_RELEASE_BUS(mmcbr, dev);
1176 return ((end < block) ? EIO : 0);
1177}
1178
1179static void
1180mmcsd_task(void *arg)
1181{
508 struct mmcsd_softc *sc = (struct mmcsd_softc*)arg;
509 struct bio *bp;
510 int sz;
511 daddr_t block, end;
1182 daddr_t block, end;
512 device_t dev = sc->dev;
513 device_t mmcbr = device_get_parent(sc->dev);
1183 struct mmcsd_part *part;
1184 struct mmcsd_softc *sc;
1185 struct bio *bp;
1186 device_t dev, mmcbr;
1187 int err, sz;
514
1188
1189 part = arg;
1190 sc = part->sc;
1191 dev = sc->dev;
1192 mmcbr = sc->mmcbr;
1193
515 while (1) {
1194 while (1) {
516 MMCSD_LOCK(sc);
1195 MMCSD_PART_LOCK(part);
517 do {
1196 do {
518 if (sc->running == 0)
1197 if (part->running == 0)
519 goto out;
1198 goto out;
520 bp = bioq_takefirst(&sc->bio_queue);
1199 bp = bioq_takefirst(&part->bio_queue);
521 if (bp == NULL)
1200 if (bp == NULL)
522 msleep(sc, &sc->sc_mtx, PRIBIO, "jobqueue", 0);
1201 msleep(part, &part->part_mtx, PRIBIO,
1202 "jobqueue", 0);
523 } while (bp == NULL);
1203 } while (bp == NULL);
524 MMCSD_UNLOCK(sc);
525 if (bp->bio_cmd != BIO_READ && mmc_get_read_only(dev)) {
1204 MMCSD_PART_UNLOCK(part);
1205 if (bp->bio_cmd != BIO_READ && part->ro) {
526 bp->bio_error = EROFS;
527 bp->bio_resid = bp->bio_bcount;
528 bp->bio_flags |= BIO_ERROR;
529 biodone(bp);
530 continue;
531 }
532 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
1206 bp->bio_error = EROFS;
1207 bp->bio_resid = bp->bio_bcount;
1208 bp->bio_flags |= BIO_ERROR;
1209 biodone(bp);
1210 continue;
1211 }
1212 MMCBUS_ACQUIRE_BUS(mmcbr, dev);
533 sz = sc->disk->d_sectorsize;
1213 sz = part->disk->d_sectorsize;
534 block = bp->bio_pblkno;
535 end = bp->bio_pblkno + (bp->bio_bcount / sz);
1214 block = bp->bio_pblkno;
1215 end = bp->bio_pblkno + (bp->bio_bcount / sz);
1216 err = mmcsd_switch_part(mmcbr, dev, sc->rca, part->type);
1217 if (err != MMC_ERR_NONE) {
1218 if (ppsratecheck(&sc->log_time, &sc->log_count,
1219 LOG_PPS))
1220 device_printf(dev, "Partition switch error\n");
1221 goto release;
1222 }
536 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
537 /* Access to the remaining erase block obsoletes it. */
1223 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1224 /* Access to the remaining erase block obsoletes it. */
538 if (block < sc->eend && end > sc->eblock)
539 sc->eblock = sc->eend = 0;
540 block = mmcsd_rw(sc, bp);
1225 if (block < part->eend && end > part->eblock)
1226 part->eblock = part->eend = 0;
1227 block = mmcsd_rw(part, bp);
541 } else if (bp->bio_cmd == BIO_DELETE) {
1228 } else if (bp->bio_cmd == BIO_DELETE) {
542 block = mmcsd_delete(sc, bp);
1229 block = mmcsd_delete(part, bp);
543 }
1230 }
1231release:
544 MMCBUS_RELEASE_BUS(mmcbr, dev);
545 if (block < end) {
546 bp->bio_error = EIO;
547 bp->bio_resid = (end - block) * sz;
548 bp->bio_flags |= BIO_ERROR;
549 } else {
550 bp->bio_resid = 0;
551 }
552 biodone(bp);
553 }
554out:
555 /* tell parent we're done */
1232 MMCBUS_RELEASE_BUS(mmcbr, dev);
1233 if (block < end) {
1234 bp->bio_error = EIO;
1235 bp->bio_resid = (end - block) * sz;
1236 bp->bio_flags |= BIO_ERROR;
1237 } else {
1238 bp->bio_resid = 0;
1239 }
1240 biodone(bp);
1241 }
1242out:
1243 /* tell parent we're done */
556 sc->running = -1;
557 MMCSD_UNLOCK(sc);
558 wakeup(sc);
1244 part->running = -1;
1245 MMCSD_PART_UNLOCK(part);
1246 wakeup(part);
559
560 kproc_exit(0);
561}
562
563static int
564mmcsd_bus_bit_width(device_t dev)
565{
566

--- 15 unchanged lines hidden (view full) ---

582
583static driver_t mmcsd_driver = {
584 "mmcsd",
585 mmcsd_methods,
586 sizeof(struct mmcsd_softc),
587};
588static devclass_t mmcsd_devclass;
589
1247
1248 kproc_exit(0);
1249}
1250
1251static int
1252mmcsd_bus_bit_width(device_t dev)
1253{
1254

--- 15 unchanged lines hidden (view full) ---

1270
1271static driver_t mmcsd_driver = {
1272 "mmcsd",
1273 mmcsd_methods,
1274 sizeof(struct mmcsd_softc),
1275};
1276static devclass_t mmcsd_devclass;
1277
590DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, NULL, NULL);
1278static int
1279mmcsd_handler(module_t mod __unused, int what, void *arg __unused)
1280{
1281
1282 switch (what) {
1283 case MOD_LOAD:
1284 flash_register_slicer(mmcsd_slicer, FLASH_SLICES_TYPE_MMC,
1285 TRUE);
1286 return (0);
1287 case MOD_UNLOAD:
1288 flash_register_slicer(NULL, FLASH_SLICES_TYPE_MMC, TRUE);
1289 return (0);
1290 }
1291 return (0);
1292}
1293
1294DRIVER_MODULE(mmcsd, mmc, mmcsd_driver, mmcsd_devclass, mmcsd_handler, NULL);
1295MODULE_DEPEND(mmcsd, g_flashmap, 0, 0, 0);
1296MMC_DEPEND(mmcsd);