2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Bernd Walter <tisco@FreeBSD.org>
5 * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org>
6 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
7 * Copyright (c) 2015-2017 Ilya Bakulin <kibab@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
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.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Some code derived from the sys/dev/mmc and sys/cam/ata
32 * Thanks to Warner Losh <imp@FreeBSD.org>, Alexander Motin <mav@FreeBSD.org>
33 * Bernd Walter <tisco@FreeBSD.org>, and other authors.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 //#include "opt_sdda.h"
41 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/endian.h>
48 #include <sys/taskqueue.h>
50 #include <sys/mutex.h>
52 #include <sys/devicestat.h>
53 #include <sys/eventhandler.h>
54 #include <sys/malloc.h>
57 #include <sys/reboot.h>
58 #include <geom/geom_disk.h>
59 #include <machine/_inttypes.h> /* for PRIu64 */
68 #include <cam/cam_ccb.h>
69 #include <cam/cam_queue.h>
70 #include <cam/cam_periph.h>
71 #include <cam/cam_sim.h>
72 #include <cam/cam_xpt.h>
73 #include <cam/cam_xpt_sim.h>
74 #include <cam/cam_xpt_periph.h>
75 #include <cam/cam_xpt_internal.h>
76 #include <cam/cam_debug.h>
79 #include <cam/mmc/mmc_all.h>
81 #include <machine/md_var.h> /* geometry translation */
86 SDDA_FLAG_OPEN = 0x0002,
87 SDDA_FLAG_DIRTY = 0x0004
94 SDDA_STATE_PART_SWITCH,
97 #define SDDA_FMT_BOOT "sdda%dboot"
98 #define SDDA_FMT_GP "sdda%dgp"
99 #define SDDA_FMT_RPMB "sdda%drpmb"
100 #define SDDA_LABEL_ENH "enh"
102 #define SDDA_PART_NAMELEN (16 + 1)
108 struct bio_queue_head bio_queue;
110 struct sdda_softc *sc;
114 char name[SDDA_PART_NAMELEN];
118 int outstanding_cmds; /* Number of active commands */
119 int refcount; /* Active xpt_action() calls */
121 struct mmc_data *mmcdata;
122 struct cam_periph *periph;
123 // sdda_quirks quirks;
124 struct task start_init_task;
126 uint8_t raw_ext_csd[512]; /* MMC only? */
130 /* Calculated from CSD */
131 uint64_t sector_count;
134 /* Calculated from CID */
135 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
136 char card_sn_string[16];/* Formatted serial # for disk->d_ident */
137 /* Determined from CSD + is highspeed card*/
140 /* Generic switch timeout */
142 /* MMC partitions support */
143 struct sdda_part *part[MMC_PART_MAX];
144 uint8_t part_curr; /* Partition currently switched to */
145 uint8_t part_requested; /* What partition we're currently switching to */
146 uint32_t part_time; /* Partition switch timeout [us] */
147 off_t enh_base; /* Enhanced user data area slice base ... */
148 off_t enh_size; /* ... and size [bytes] */
150 struct timeval log_time;
153 #define ccb_bp ppriv_ptr1
155 static disk_strategy_t sddastrategy;
156 static periph_init_t sddainit;
157 static void sddaasync(void *callback_arg, u_int32_t code,
158 struct cam_path *path, void *arg);
159 static periph_ctor_t sddaregister;
160 static periph_dtor_t sddacleanup;
161 static periph_start_t sddastart;
162 static periph_oninv_t sddaoninvalidate;
163 static void sddadone(struct cam_periph *periph,
164 union ccb *done_ccb);
165 static int sddaerror(union ccb *ccb, u_int32_t cam_flags,
166 u_int32_t sense_flags);
168 static uint16_t get_rca(struct cam_periph *periph);
169 static void sdda_start_init(void *context, union ccb *start_ccb);
170 static void sdda_start_init_task(void *context, int pending);
171 static void sdda_process_mmc_partitions(struct cam_periph *periph, union ccb *start_ccb);
172 static uint32_t sdda_get_host_caps(struct cam_periph *periph, union ccb *ccb);
173 static void sdda_init_switch_part(struct cam_periph *periph, union ccb *start_ccb, u_int part);
174 static int mmc_select_card(struct cam_periph *periph, union ccb *ccb, uint32_t rca);
175 static inline uint32_t mmc_get_sector_size(struct cam_periph *periph) {return MMC_SECTOR_SIZE;}
177 /* TODO: actually issue GET_TRAN_SETTINGS to get R/O status */
178 static inline bool sdda_get_read_only(struct cam_periph *periph, union ccb *start_ccb)
184 static uint32_t mmc_get_spec_vers(struct cam_periph *periph);
185 static uint64_t mmc_get_media_size(struct cam_periph *periph);
186 static uint32_t mmc_get_cmd6_timeout(struct cam_periph *periph);
187 static void sdda_add_part(struct cam_periph *periph, u_int type,
188 const char *name, u_int cnt, off_t media_size, bool ro);
190 static struct periph_driver sddadriver =
193 TAILQ_HEAD_INITIALIZER(sddadriver.units), /* generation */ 0
196 PERIPHDRIVER_DECLARE(sdda, sddadriver);
198 static MALLOC_DEFINE(M_SDDA, "sd_da", "sd_da buffers");
200 static const int exp[8] = {
201 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
204 static const int mant[16] = {
205 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
208 static const int cur_min[8] = {
209 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
212 static const int cur_max[8] = {
213 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
217 get_rca(struct cam_periph *periph) {
218 return periph->path->device->mmc_ident_data.card_rca;
222 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
224 const int i = (bit_len / 32) - (start / 32) - 1;
225 const int shift = start & 31;
226 uint32_t retval = bits[i] >> shift;
227 if (size + shift > 32)
228 retval |= bits[i - 1] << (32 - shift);
229 return (retval & ((1llu << size) - 1));
234 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
240 memset(csd, 0, sizeof(*csd));
241 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
243 m = mmc_get_bits(raw_csd, 128, 115, 4);
244 e = mmc_get_bits(raw_csd, 128, 112, 3);
245 csd->tacc = (exp[e] * mant[m] + 9) / 10;
246 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
247 m = mmc_get_bits(raw_csd, 128, 99, 4);
248 e = mmc_get_bits(raw_csd, 128, 96, 3);
249 csd->tran_speed = exp[e] * 10000 * mant[m];
250 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
251 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
252 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
253 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
254 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
255 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
256 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
257 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
258 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
259 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
260 m = mmc_get_bits(raw_csd, 128, 62, 12);
261 e = mmc_get_bits(raw_csd, 128, 47, 3);
262 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
263 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
264 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
265 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
266 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
267 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
268 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
269 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
271 m = mmc_get_bits(raw_csd, 128, 115, 4);
272 e = mmc_get_bits(raw_csd, 128, 112, 3);
273 csd->tacc = (exp[e] * mant[m] + 9) / 10;
274 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
275 m = mmc_get_bits(raw_csd, 128, 99, 4);
276 e = mmc_get_bits(raw_csd, 128, 96, 3);
277 csd->tran_speed = exp[e] * 10000 * mant[m];
278 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
279 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
280 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
281 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
282 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
283 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
284 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
286 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
287 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
288 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
289 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
290 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
291 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
292 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
294 panic("unknown SD CSD version");
298 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
303 memset(csd, 0, sizeof(*csd));
304 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
305 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
306 m = mmc_get_bits(raw_csd, 128, 115, 4);
307 e = mmc_get_bits(raw_csd, 128, 112, 3);
308 csd->tacc = exp[e] * mant[m] + 9 / 10;
309 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
310 m = mmc_get_bits(raw_csd, 128, 99, 4);
311 e = mmc_get_bits(raw_csd, 128, 96, 3);
312 csd->tran_speed = exp[e] * 10000 * mant[m];
313 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
314 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
315 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
316 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
317 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
318 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
319 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
320 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
321 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
322 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
323 m = mmc_get_bits(raw_csd, 128, 62, 12);
324 e = mmc_get_bits(raw_csd, 128, 47, 3);
325 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
326 csd->erase_blk_en = 0;
327 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
328 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
329 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
330 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
331 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
332 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
333 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
337 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
341 /* There's no version info, so we take it on faith */
342 memset(cid, 0, sizeof(*cid));
343 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
344 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
345 for (i = 0; i < 5; i++)
346 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
348 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
349 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
350 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
351 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
355 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
359 /* There's no version info, so we take it on faith */
360 memset(cid, 0, sizeof(*cid));
361 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
362 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
363 for (i = 0; i < 6; i++)
364 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
366 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
367 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
368 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
369 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
373 mmc_format_card_id_string(struct sdda_softc *sc, struct mmc_params *mmcp)
380 * Format a card ID string for use by the mmcsd driver, it's what
381 * appears between the <> in the following:
382 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
383 * 22.5MHz/4bit/128-block
385 * Also format just the card serial number, which the mmcsd driver will
386 * use as the disk->d_ident string.
388 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
389 * and our max formatted length is currently 55 bytes if every field
390 * contains the largest value.
392 * Sometimes the oid is two printable ascii chars; when it's not,
393 * format it as 0xnnnn instead.
395 c1 = (sc->cid.oid >> 8) & 0x0ff;
396 c2 = sc->cid.oid & 0x0ff;
397 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
398 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
400 snprintf(oidstr, sizeof(oidstr), "0x%04x", sc->cid.oid);
401 snprintf(sc->card_sn_string, sizeof(sc->card_sn_string),
402 "%08X", sc->cid.psn);
403 snprintf(sc->card_id_string, sizeof(sc->card_id_string),
404 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
405 mmcp->card_features & CARD_FEATURE_MMC ? "MMC" : "SD",
406 mmcp->card_features & CARD_FEATURE_SDHC ? "HC" : "",
407 sc->cid.pnm, sc->cid.prv >> 4, sc->cid.prv & 0x0f,
408 sc->cid.psn, sc->cid.mdt_month, sc->cid.mdt_year,
409 sc->cid.mid, oidstr);
413 sddaopen(struct disk *dp)
415 struct sdda_part *part;
416 struct cam_periph *periph;
417 struct sdda_softc *softc;
420 part = (struct sdda_part *)dp->d_drv1;
422 periph = softc->periph;
423 if (cam_periph_acquire(periph) != 0) {
427 cam_periph_lock(periph);
428 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
429 cam_periph_unlock(periph);
430 cam_periph_release(periph);
434 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaopen\n"));
436 part->flags |= SDDA_FLAG_OPEN;
438 cam_periph_unhold(periph);
439 cam_periph_unlock(periph);
444 sddaclose(struct disk *dp)
446 struct sdda_part *part;
447 struct cam_periph *periph;
448 struct sdda_softc *softc;
450 part = (struct sdda_part *)dp->d_drv1;
452 periph = softc->periph;
453 part->flags &= ~SDDA_FLAG_OPEN;
455 cam_periph_lock(periph);
457 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaclose\n"));
459 while (softc->refcount != 0)
460 cam_periph_sleep(periph, &softc->refcount, PRIBIO, "sddaclose", 1);
461 cam_periph_unlock(periph);
462 cam_periph_release(periph);
467 sddaschedule(struct cam_periph *periph)
469 struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
470 struct sdda_part *part;
474 /* Check if we have more work to do. */
475 /* Find partition that has outstanding commands. Prefer current partition. */
476 bp = bioq_first(&softc->part[softc->part_curr]->bio_queue);
478 for (i = 0; i < MMC_PART_MAX; i++) {
479 if ((part = softc->part[i]) != NULL &&
480 (bp = bioq_first(&softc->part[i]->bio_queue)) != NULL)
485 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
490 * Actually translate the requested transfer into one the physical driver
491 * can understand. The transfer is described by a buf and will include
492 * only one physical transfer.
495 sddastrategy(struct bio *bp)
497 struct cam_periph *periph;
498 struct sdda_part *part;
499 struct sdda_softc *softc;
501 part = (struct sdda_part *)bp->bio_disk->d_drv1;
503 periph = softc->periph;
505 cam_periph_lock(periph);
507 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddastrategy(%p)\n", bp));
510 * If the device has been made invalid, error out
512 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
513 cam_periph_unlock(periph);
514 biofinish(bp, NULL, ENXIO);
519 * Place it in the queue of disk activities for this disk
521 bioq_disksort(&part->bio_queue, bp);
524 * Schedule ourselves for performing the work.
526 sddaschedule(periph);
527 cam_periph_unlock(periph);
538 * Install a global async callback. This callback will
539 * receive async callbacks like "new device found".
541 status = xpt_register_async(AC_FOUND_DEVICE, sddaasync, NULL, NULL);
543 if (status != CAM_REQ_CMP) {
544 printf("sdda: Failed to attach master async callback "
545 "due to status 0x%x!\n", status);
550 * Callback from GEOM, called when it has finished cleaning up its
554 sddadiskgonecb(struct disk *dp)
556 struct cam_periph *periph;
557 struct sdda_part *part;
559 part = (struct sdda_part *)dp->d_drv1;
560 periph = part->sc->periph;
561 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddadiskgonecb\n"));
563 cam_periph_release(periph);
567 sddaoninvalidate(struct cam_periph *periph)
569 struct sdda_softc *softc;
570 struct sdda_part *part;
572 softc = (struct sdda_softc *)periph->softc;
574 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaoninvalidate\n"));
577 * De-register any async callbacks.
579 xpt_register_async(0, sddaasync, periph, periph->path);
582 * Return all queued I/O with ENXIO.
583 * XXX Handle any transactions queued to the card
584 * with XPT_ABORT_CCB.
586 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("bioq_flush start\n"));
587 for (int i = 0; i < MMC_PART_MAX; i++) {
588 if ((part = softc->part[i]) != NULL) {
589 bioq_flush(&part->bio_queue, NULL, ENXIO);
590 disk_gone(part->disk);
593 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("bioq_flush end\n"));
598 sddacleanup(struct cam_periph *periph)
600 struct sdda_softc *softc;
601 struct sdda_part *part;
604 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddacleanup\n"));
605 softc = (struct sdda_softc *)periph->softc;
607 cam_periph_unlock(periph);
609 for (i = 0; i < MMC_PART_MAX; i++) {
610 if ((part = softc->part[i]) != NULL) {
611 disk_destroy(part->disk);
612 free(part, M_DEVBUF);
613 softc->part[i] = NULL;
616 free(softc, M_DEVBUF);
617 cam_periph_lock(periph);
621 sddaasync(void *callback_arg, u_int32_t code,
622 struct cam_path *path, void *arg)
624 struct ccb_getdev cgd;
625 struct cam_periph *periph;
626 struct sdda_softc *softc;
628 periph = (struct cam_periph *)callback_arg;
629 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("sddaasync(code=%d)\n", code));
631 case AC_FOUND_DEVICE:
633 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_FOUND_DEVICE\n"));
634 struct ccb_getdev *cgd;
637 cgd = (struct ccb_getdev *)arg;
641 if (cgd->protocol != PROTO_MMCSD)
644 if (!(path->device->mmc_ident_data.card_features & CARD_FEATURE_MEMORY)) {
645 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("No memory on the card!\n"));
650 * Allocate a peripheral instance for
651 * this device and start the probe
654 status = cam_periph_alloc(sddaregister, sddaoninvalidate,
655 sddacleanup, sddastart,
656 "sdda", CAM_PERIPH_BIO,
658 AC_FOUND_DEVICE, cgd);
660 if (status != CAM_REQ_CMP
661 && status != CAM_REQ_INPROG)
662 printf("sddaasync: Unable to attach to new device "
663 "due to status 0x%x\n", status);
666 case AC_GETDEV_CHANGED:
668 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_GETDEV_CHANGED\n"));
669 softc = (struct sdda_softc *)periph->softc;
670 xpt_setup_ccb(&cgd.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
671 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
672 xpt_action((union ccb *)&cgd);
673 cam_periph_async(periph, code, path, arg);
676 case AC_ADVINFO_CHANGED:
681 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> AC_ADVINFO_CHANGED\n"));
682 buftype = (uintptr_t)arg;
683 if (buftype == CDAI_TYPE_PHYS_PATH) {
684 struct sdda_softc *softc;
685 struct sdda_part *part;
687 softc = periph->softc;
688 for (i = 0; i < MMC_PART_MAX; i++) {
689 if ((part = softc->part[i]) != NULL) {
690 disk_attr_changed(part->disk, "GEOM::physpath",
698 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("=> default?!\n"));
699 cam_periph_async(periph, code, path, arg);
706 sddagetattr(struct bio *bp)
708 struct cam_periph *periph;
709 struct sdda_softc *softc;
710 struct sdda_part *part;
713 part = (struct sdda_part *)bp->bio_disk->d_drv1;
715 periph = softc->periph;
716 cam_periph_lock(periph);
717 ret = xpt_getattr(bp->bio_data, bp->bio_length, bp->bio_attribute,
719 cam_periph_unlock(periph);
721 bp->bio_completed = bp->bio_length;
726 sddaregister(struct cam_periph *periph, void *arg)
728 struct sdda_softc *softc;
729 struct ccb_getdev *cgd;
730 union ccb *request_ccb; /* CCB representing the probe request */
732 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddaregister\n"));
733 cgd = (struct ccb_getdev *)arg;
735 printf("sddaregister: no getdev CCB, can't register device\n");
736 return (CAM_REQ_CMP_ERR);
739 softc = (struct sdda_softc *)malloc(sizeof(*softc), M_DEVBUF,
743 printf("sddaregister: Unable to probe new device. "
744 "Unable to allocate softc\n");
745 return (CAM_REQ_CMP_ERR);
748 softc->state = SDDA_STATE_INIT;
750 (struct mmc_data *)malloc(sizeof(struct mmc_data), M_DEVBUF, M_NOWAIT|M_ZERO);
751 periph->softc = softc;
752 softc->periph = periph;
754 request_ccb = (union ccb*) arg;
755 xpt_schedule(periph, CAM_PRIORITY_XPT);
756 TASK_INIT(&softc->start_init_task, 0, sdda_start_init_task, periph);
757 taskqueue_enqueue(taskqueue_thread, &softc->start_init_task);
759 return (CAM_REQ_CMP);
763 mmc_exec_app_cmd(struct cam_periph *periph, union ccb *ccb,
764 struct mmc_command *cmd) {
767 /* Send APP_CMD first */
768 memset(&ccb->mmcio.cmd, 0, sizeof(struct mmc_command));
769 memset(&ccb->mmcio.stop, 0, sizeof(struct mmc_command));
770 cam_fill_mmcio(&ccb->mmcio,
773 /*flags*/ CAM_DIR_NONE,
774 /*mmc_opcode*/ MMC_APP_CMD,
775 /*mmc_arg*/ get_rca(periph) << 16,
776 /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_AC,
780 err = cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
783 if (!(ccb->mmcio.cmd.resp[0] & R1_APP_CMD))
784 return MMC_ERR_FAILED;
786 /* Now exec actual command */
788 if (cmd->data != NULL) {
789 ccb->mmcio.cmd.data = cmd->data;
790 if (cmd->data->flags & MMC_DATA_READ)
792 if (cmd->data->flags & MMC_DATA_WRITE)
793 flags |= CAM_DIR_OUT;
794 } else flags = CAM_DIR_NONE;
796 cam_fill_mmcio(&ccb->mmcio,
800 /*mmc_opcode*/ cmd->opcode,
801 /*mmc_arg*/ cmd->arg,
802 /*mmc_flags*/ cmd->flags,
803 /*mmc_data*/ cmd->data,
806 err = cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
807 memcpy(cmd->resp, ccb->mmcio.cmd.resp, sizeof(cmd->resp));
808 cmd->error = ccb->mmcio.cmd.error;
815 mmc_app_get_scr(struct cam_periph *periph, union ccb *ccb, uint32_t *rawscr) {
817 struct mmc_command cmd;
820 memset(&cmd, 0, sizeof(cmd));
821 memset(&d, 0, sizeof(d));
823 memset(rawscr, 0, 8);
824 cmd.opcode = ACMD_SEND_SCR;
825 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
830 d.flags = MMC_DATA_READ;
833 err = mmc_exec_app_cmd(periph, ccb, &cmd);
834 rawscr[0] = be32toh(rawscr[0]);
835 rawscr[1] = be32toh(rawscr[1]);
840 mmc_send_ext_csd(struct cam_periph *periph, union ccb *ccb,
841 uint8_t *rawextcsd, size_t buf_len) {
845 KASSERT(buf_len == 512, ("Buffer for ext csd must be 512 bytes"));
848 d.flags = MMC_DATA_READ;
849 memset(d.data, 0, d.len);
851 cam_fill_mmcio(&ccb->mmcio,
854 /*flags*/ CAM_DIR_IN,
855 /*mmc_opcode*/ MMC_SEND_EXT_CSD,
857 /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_ADTC,
861 err = cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
864 return (MMC_ERR_NONE);
868 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
870 unsigned int scr_struct;
872 memset(scr, 0, sizeof(*scr));
874 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
875 if (scr_struct != 0) {
876 printf("Unrecognised SCR structure version %d\n",
880 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
881 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
885 mmc_switch_fill_mmcio(union ccb *ccb,
886 uint8_t set, uint8_t index, uint8_t value, u_int timeout)
888 int arg = (MMC_SWITCH_FUNC_WR << 24) |
893 cam_fill_mmcio(&ccb->mmcio,
896 /*flags*/ CAM_DIR_NONE,
897 /*mmc_opcode*/ MMC_SWITCH_FUNC,
899 /*mmc_flags*/ MMC_RSP_R1B | MMC_CMD_AC,
901 /*timeout*/ timeout);
905 mmc_select_card(struct cam_periph *periph, union ccb *ccb, uint32_t rca)
909 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
910 cam_fill_mmcio(&ccb->mmcio,
913 /*flags*/ CAM_DIR_IN,
914 /*mmc_opcode*/ MMC_SELECT_CARD,
915 /*mmc_arg*/ rca << 16,
920 cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
922 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)) {
923 if (ccb->mmcio.cmd.error != 0) {
924 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_PERIPH,
925 ("%s: MMC_SELECT command failed", __func__));
928 return 0; /* Normal return */
930 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_PERIPH,
931 ("%s: CAM request failed\n", __func__));
937 mmc_switch(struct cam_periph *periph, union ccb *ccb,
938 uint8_t set, uint8_t index, uint8_t value, u_int timeout)
941 mmc_switch_fill_mmcio(ccb, set, index, value, timeout);
942 cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
944 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)) {
945 if (ccb->mmcio.cmd.error != 0) {
946 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_PERIPH,
947 ("%s: MMC command failed", __func__));
950 return (0); /* Normal return */
952 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_PERIPH,
953 ("%s: CAM request failed\n", __func__));
960 mmc_get_spec_vers(struct cam_periph *periph) {
961 struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
963 return (softc->csd.spec_vers);
967 mmc_get_media_size(struct cam_periph *periph) {
968 struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
970 return (softc->mediasize);
974 mmc_get_cmd6_timeout(struct cam_periph *periph)
976 struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
978 if (mmc_get_spec_vers(periph) >= 6)
979 return (softc->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME] * 10);
984 mmc_sd_switch(struct cam_periph *periph, union ccb *ccb,
985 uint8_t mode, uint8_t grp, uint8_t value,
988 struct mmc_data mmc_d;
994 mmc_d.flags = MMC_DATA_READ;
996 arg = mode << 31; /* 0 - check, 1 - set */
998 arg &= ~(0xF << (grp * 4));
999 arg |= value << (grp * 4);
1001 cam_fill_mmcio(&ccb->mmcio,
1004 /*flags*/ CAM_DIR_IN,
1005 /*mmc_opcode*/ SD_SWITCH_FUNC,
1007 /*mmc_flags*/ MMC_RSP_R1 | MMC_CMD_ADTC,
1008 /*mmc_data*/ &mmc_d,
1011 cam_periph_runccb(ccb, sddaerror, CAM_FLAG_NONE, /*sense_flags*/0, NULL);
1013 if (((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)) {
1014 if (ccb->mmcio.cmd.error != 0) {
1015 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_PERIPH,
1016 ("%s: MMC command failed", __func__));
1019 return 0; /* Normal return */
1021 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_PERIPH,
1022 ("%s: CAM request failed\n", __func__));
1028 mmc_set_timing(struct cam_periph *periph,
1030 enum mmc_bus_timing timing)
1032 u_char switch_res[64];
1035 struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1036 struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1038 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
1039 ("mmc_set_timing(timing=%d)", timing));
1041 case bus_timing_normal:
1048 return (MMC_ERR_INVALID);
1050 if (mmcp->card_features & CARD_FEATURE_MMC) {
1051 err = mmc_switch(periph, ccb, EXT_CSD_CMD_SET_NORMAL,
1052 EXT_CSD_HS_TIMING, value, softc->cmd6_time);
1054 err = mmc_sd_switch(periph, ccb, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, value, switch_res);
1057 /* Set high-speed timing on the host */
1058 struct ccb_trans_settings_mmc *cts;
1059 cts = &ccb->cts.proto_specific.mmc;
1060 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1061 ccb->ccb_h.flags = CAM_DIR_NONE;
1062 ccb->ccb_h.retry_count = 0;
1063 ccb->ccb_h.timeout = 100;
1064 ccb->ccb_h.cbfcnp = NULL;
1065 cts->ios.timing = timing;
1066 cts->ios_valid = MMC_BT;
1073 sdda_start_init_task(void *context, int pending) {
1075 struct cam_periph *periph;
1077 periph = (struct cam_periph *)context;
1078 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_start_init_task\n"));
1079 new_ccb = xpt_alloc_ccb();
1080 xpt_setup_ccb(&new_ccb->ccb_h, periph->path,
1083 cam_periph_lock(periph);
1084 sdda_start_init(context, new_ccb);
1085 cam_periph_unlock(periph);
1086 xpt_free_ccb(new_ccb);
1090 sdda_set_bus_width(struct cam_periph *periph, union ccb *ccb, int width) {
1091 struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1092 struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1095 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_set_bus_width\n"));
1097 /* First set for the card, then for the host */
1098 if (mmcp->card_features & CARD_FEATURE_MMC) {
1102 value = EXT_CSD_BUS_WIDTH_1;
1105 value = EXT_CSD_BUS_WIDTH_4;
1108 value = EXT_CSD_BUS_WIDTH_8;
1111 panic("Invalid bus width %d", width);
1113 err = mmc_switch(periph, ccb, EXT_CSD_CMD_SET_NORMAL,
1114 EXT_CSD_BUS_WIDTH, value, softc->cmd6_time);
1116 /* For SD cards we send ACMD6 with the required bus width in arg */
1117 struct mmc_command cmd;
1118 memset(&cmd, 0, sizeof(struct mmc_command));
1119 cmd.opcode = ACMD_SET_BUS_WIDTH;
1121 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1122 err = mmc_exec_app_cmd(periph, ccb, &cmd);
1125 if (err != MMC_ERR_NONE) {
1126 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Error %d when setting bus width on the card\n", err));
1129 /* Now card is done, set the host to the same width */
1130 struct ccb_trans_settings_mmc *cts;
1131 cts = &ccb->cts.proto_specific.mmc;
1132 ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1133 ccb->ccb_h.flags = CAM_DIR_NONE;
1134 ccb->ccb_h.retry_count = 0;
1135 ccb->ccb_h.timeout = 100;
1136 ccb->ccb_h.cbfcnp = NULL;
1137 cts->ios.bus_width = width;
1138 cts->ios_valid = MMC_BW;
1142 static inline const char
1143 *part_type(u_int type)
1147 case EXT_CSD_PART_CONFIG_ACC_RPMB:
1149 case EXT_CSD_PART_CONFIG_ACC_DEFAULT:
1151 case EXT_CSD_PART_CONFIG_ACC_BOOT0:
1153 case EXT_CSD_PART_CONFIG_ACC_BOOT1:
1155 case EXT_CSD_PART_CONFIG_ACC_GP0:
1156 case EXT_CSD_PART_CONFIG_ACC_GP1:
1157 case EXT_CSD_PART_CONFIG_ACC_GP2:
1158 case EXT_CSD_PART_CONFIG_ACC_GP3:
1159 return ("general purpose");
1161 return ("(unknown type)");
1165 static inline const char
1166 *bus_width_str(enum mmc_bus_width w)
1180 sdda_get_host_caps(struct cam_periph *periph, union ccb *ccb)
1182 struct ccb_trans_settings_mmc *cts;
1184 cts = &ccb->cts.proto_specific.mmc;
1186 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1187 ccb->ccb_h.flags = CAM_DIR_NONE;
1188 ccb->ccb_h.retry_count = 0;
1189 ccb->ccb_h.timeout = 100;
1190 ccb->ccb_h.cbfcnp = NULL;
1193 if (ccb->ccb_h.status != CAM_REQ_CMP)
1194 panic("Cannot get host caps");
1195 return (cts->host_caps);
1199 sdda_get_max_data(struct cam_periph *periph, union ccb *ccb)
1201 struct ccb_trans_settings_mmc *cts;
1203 cts = &ccb->cts.proto_specific.mmc;
1204 memset(cts, 0, sizeof(struct ccb_trans_settings_mmc));
1206 ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1207 ccb->ccb_h.flags = CAM_DIR_NONE;
1208 ccb->ccb_h.retry_count = 0;
1209 ccb->ccb_h.timeout = 100;
1210 ccb->ccb_h.cbfcnp = NULL;
1213 if (ccb->ccb_h.status != CAM_REQ_CMP)
1214 panic("Cannot get host max data");
1215 KASSERT(cts->host_max_data != 0, ("host_max_data == 0?!"));
1216 return (cts->host_max_data);
1220 sdda_start_init(void *context, union ccb *start_ccb)
1222 struct cam_periph *periph = (struct cam_periph *)context;
1223 struct ccb_trans_settings_mmc *cts;
1229 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sdda_start_init\n"));
1230 /* periph was held for us when this task was enqueued */
1231 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
1232 cam_periph_release(periph);
1236 struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1237 //struct ccb_mmcio *mmcio = &start_ccb->mmcio;
1238 struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1239 struct cam_ed *device = periph->path->device;
1241 if (mmcp->card_features & CARD_FEATURE_MMC) {
1242 mmc_decode_csd_mmc(mmcp->card_csd, &softc->csd);
1243 mmc_decode_cid_mmc(mmcp->card_cid, &softc->cid);
1244 if (mmc_get_spec_vers(periph) >= 4) {
1245 err = mmc_send_ext_csd(periph, start_ccb,
1246 (uint8_t *)&softc->raw_ext_csd,
1247 sizeof(softc->raw_ext_csd));
1249 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1250 ("Cannot read EXT_CSD, err %d", err));
1255 mmc_decode_csd_sd(mmcp->card_csd, &softc->csd);
1256 mmc_decode_cid_sd(mmcp->card_cid, &softc->cid);
1259 softc->sector_count = softc->csd.capacity / 512;
1260 softc->mediasize = softc->csd.capacity;
1261 softc->cmd6_time = mmc_get_cmd6_timeout(periph);
1263 /* MMC >= 4.x have EXT_CSD that has its own opinion about capacity */
1264 if (mmc_get_spec_vers(periph) >= 4) {
1265 sec_count = softc->raw_ext_csd[EXT_CSD_SEC_CNT] +
1266 (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1267 (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1268 (softc->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1269 if (sec_count != 0) {
1270 softc->sector_count = sec_count;
1271 softc->mediasize = softc->sector_count * 512;
1272 /* FIXME: there should be a better name for this option...*/
1273 mmcp->card_features |= CARD_FEATURE_SDHC;
1277 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1278 ("Capacity: %"PRIu64", sectors: %"PRIu64"\n",
1280 softc->sector_count));
1281 mmc_format_card_id_string(softc, mmcp);
1283 /* Update info for CAM */
1284 device->serial_num_len = strlen(softc->card_sn_string);
1285 device->serial_num = (u_int8_t *)malloc((device->serial_num_len + 1),
1286 M_CAMXPT, M_NOWAIT);
1287 strlcpy(device->serial_num, softc->card_sn_string, device->serial_num_len);
1289 device->device_id_len = strlen(softc->card_id_string);
1290 device->device_id = (u_int8_t *)malloc((device->device_id_len + 1),
1291 M_CAMXPT, M_NOWAIT);
1292 strlcpy(device->device_id, softc->card_id_string, device->device_id_len);
1294 strlcpy(mmcp->model, softc->card_id_string, sizeof(mmcp->model));
1296 /* Set the clock frequency that the card can handle */
1297 cts = &start_ccb->cts.proto_specific.mmc;
1299 /* First, get the host's max freq */
1300 start_ccb->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1301 start_ccb->ccb_h.flags = CAM_DIR_NONE;
1302 start_ccb->ccb_h.retry_count = 0;
1303 start_ccb->ccb_h.timeout = 100;
1304 start_ccb->ccb_h.cbfcnp = NULL;
1305 xpt_action(start_ccb);
1307 if (start_ccb->ccb_h.status != CAM_REQ_CMP)
1308 panic("Cannot get max host freq");
1309 host_f_max = cts->host_f_max;
1310 host_caps = cts->host_caps;
1311 if (cts->ios.bus_width != bus_width_1)
1312 panic("Bus width in ios is not 1-bit");
1314 /* Now check if the card supports High-speed */
1315 softc->card_f_max = softc->csd.tran_speed;
1317 if (host_caps & MMC_CAP_HSPEED) {
1318 /* Find out if the card supports High speed timing */
1319 if (mmcp->card_features & CARD_FEATURE_SD20) {
1320 /* Get and decode SCR */
1323 if (mmc_app_get_scr(periph, start_ccb, rawscr)) {
1324 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Cannot get SCR\n"));
1325 goto finish_hs_tests;
1327 mmc_app_decode_scr(rawscr, &softc->scr);
1329 if ((softc->scr.sda_vsn >= 1) && (softc->csd.ccc & (1<<10))) {
1330 mmc_sd_switch(periph, start_ccb, SD_SWITCH_MODE_CHECK,
1331 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, res);
1333 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Card supports HS\n"));
1334 softc->card_f_max = SD_HS_MAX;
1338 * We deselect then reselect the card here. Some cards
1339 * become unselected and timeout with the above two
1340 * commands, although the state tables / diagrams in the
1341 * standard suggest they go back to the transfer state.
1342 * Other cards don't become deselected, and if we
1343 * attempt to blindly re-select them, we get timeout
1344 * errors from some controllers. So we deselect then
1345 * reselect to handle all situations.
1347 mmc_select_card(periph, start_ccb, 0);
1348 mmc_select_card(periph, start_ccb, get_rca(periph));
1350 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Not trying the switch\n"));
1351 goto finish_hs_tests;
1355 if (mmcp->card_features & CARD_FEATURE_MMC && mmc_get_spec_vers(periph) >= 4) {
1356 if (softc->raw_ext_csd[EXT_CSD_CARD_TYPE]
1357 & EXT_CSD_CARD_TYPE_HS_52)
1358 softc->card_f_max = MMC_TYPE_HS_52_MAX;
1359 else if (softc->raw_ext_csd[EXT_CSD_CARD_TYPE]
1360 & EXT_CSD_CARD_TYPE_HS_26)
1361 softc->card_f_max = MMC_TYPE_HS_26_MAX;
1366 f_max = min(host_f_max, softc->card_f_max);
1367 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH, ("Set SD freq to %d MHz (min out of host f=%d MHz and card f=%d MHz)\n", f_max / 1000000, host_f_max / 1000000, softc->card_f_max / 1000000));
1369 /* Enable high-speed timing on the card */
1370 if (f_max > 25000000) {
1371 err = mmc_set_timing(periph, start_ccb, bus_timing_hs);
1372 if (err != MMC_ERR_NONE) {
1373 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("Cannot switch card to high-speed mode"));
1377 /* Set frequency on the controller */
1378 start_ccb->ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1379 start_ccb->ccb_h.flags = CAM_DIR_NONE;
1380 start_ccb->ccb_h.retry_count = 0;
1381 start_ccb->ccb_h.timeout = 100;
1382 start_ccb->ccb_h.cbfcnp = NULL;
1383 cts->ios.clock = f_max;
1384 cts->ios_valid = MMC_CLK;
1385 xpt_action(start_ccb);
1388 enum mmc_bus_width desired_bus_width = bus_width_1;
1389 enum mmc_bus_width max_host_bus_width =
1390 (host_caps & MMC_CAP_8_BIT_DATA ? bus_width_8 :
1391 host_caps & MMC_CAP_4_BIT_DATA ? bus_width_4 : bus_width_1);
1392 enum mmc_bus_width max_card_bus_width = bus_width_1;
1393 if (mmcp->card_features & CARD_FEATURE_SD20 &&
1394 softc->scr.bus_widths & SD_SCR_BUS_WIDTH_4)
1395 max_card_bus_width = bus_width_4;
1397 * Unlike SD, MMC cards don't have any information about supported bus width...
1398 * So we need to perform read/write test to find out the width.
1400 /* TODO: figure out bus width for MMC; use 8-bit for now (to test on BBB) */
1401 if (mmcp->card_features & CARD_FEATURE_MMC)
1402 max_card_bus_width = bus_width_8;
1404 desired_bus_width = min(max_host_bus_width, max_card_bus_width);
1405 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1406 ("Set bus width to %s (min of host %s and card %s)\n",
1407 bus_width_str(desired_bus_width),
1408 bus_width_str(max_host_bus_width),
1409 bus_width_str(max_card_bus_width)));
1410 sdda_set_bus_width(periph, start_ccb, desired_bus_width);
1412 softc->state = SDDA_STATE_NORMAL;
1414 /* MMC partitions support */
1415 if (mmcp->card_features & CARD_FEATURE_MMC && mmc_get_spec_vers(periph) >= 4) {
1416 sdda_process_mmc_partitions(periph, start_ccb);
1417 } else if (mmcp->card_features & CARD_FEATURE_SD20) {
1418 /* For SD[HC] cards, just add one partition that is the whole card */
1419 sdda_add_part(periph, 0, "sdda",
1420 periph->unit_number,
1421 mmc_get_media_size(periph),
1422 sdda_get_read_only(periph, start_ccb));
1423 softc->part_curr = 0;
1426 xpt_announce_periph(periph, softc->card_id_string);
1428 * Add async callbacks for bus reset and bus device reset calls.
1429 * I don't bother checking if this fails as, in most cases,
1430 * the system will function just fine without them and the only
1431 * alternative would be to not attach the device on failure.
1433 xpt_register_async(AC_LOST_DEVICE | AC_GETDEV_CHANGED |
1434 AC_ADVINFO_CHANGED, sddaasync, periph, periph->path);
1438 sdda_add_part(struct cam_periph *periph, u_int type, const char *name,
1439 u_int cnt, off_t media_size, bool ro)
1441 struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
1442 struct sdda_part *part;
1443 struct ccb_pathinq cpi;
1445 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1446 ("Partition type '%s', size %ju %s\n",
1449 ro ? "(read-only)" : ""));
1451 part = sc->part[type] = malloc(sizeof(*part), M_DEVBUF,
1458 snprintf(part->name, sizeof(part->name), name, periph->unit_number);
1461 * Due to the nature of RPMB partition it doesn't make much sense
1462 * to add it as a disk. It would be more appropriate to create a
1463 * userland tool to operate on the partition or leverage the existing
1464 * tools from sysutils/mmc-utils.
1466 if (type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
1467 /* TODO: Create device, assign IOCTL handler */
1468 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1469 ("Don't know what to do with RPMB partitions yet\n"));
1473 bioq_init(&part->bio_queue);
1475 bzero(&cpi, sizeof(cpi));
1476 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
1477 cpi.ccb_h.func_code = XPT_PATH_INQ;
1478 xpt_action((union ccb *)&cpi);
1481 * Register this media as a disk
1483 (void)cam_periph_hold(periph, PRIBIO);
1484 cam_periph_unlock(periph);
1486 part->disk = disk_alloc();
1487 part->disk->d_rotation_rate = DISK_RR_NON_ROTATING;
1488 part->disk->d_devstat = devstat_new_entry(part->name,
1490 DEVSTAT_ALL_SUPPORTED,
1491 DEVSTAT_TYPE_DIRECT | XPORT_DEVSTAT_TYPE(cpi.transport),
1492 DEVSTAT_PRIORITY_DISK);
1494 part->disk->d_open = sddaopen;
1495 part->disk->d_close = sddaclose;
1496 part->disk->d_strategy = sddastrategy;
1497 part->disk->d_getattr = sddagetattr;
1498 // sc->disk->d_dump = sddadump;
1499 part->disk->d_gone = sddadiskgonecb;
1500 part->disk->d_name = part->name;
1501 part->disk->d_drv1 = part;
1502 part->disk->d_maxsize =
1503 MIN(MAXPHYS, sdda_get_max_data(periph,
1504 (union ccb *)&cpi) * mmc_get_sector_size(periph));
1505 part->disk->d_unit = cnt;
1506 part->disk->d_flags = 0;
1507 strlcpy(part->disk->d_descr, sc->card_id_string,
1508 MIN(sizeof(part->disk->d_descr), sizeof(sc->card_id_string)));
1509 strlcpy(part->disk->d_ident, sc->card_sn_string,
1510 MIN(sizeof(part->disk->d_ident), sizeof(sc->card_sn_string)));
1511 part->disk->d_hba_vendor = cpi.hba_vendor;
1512 part->disk->d_hba_device = cpi.hba_device;
1513 part->disk->d_hba_subvendor = cpi.hba_subvendor;
1514 part->disk->d_hba_subdevice = cpi.hba_subdevice;
1516 part->disk->d_sectorsize = mmc_get_sector_size(periph);
1517 part->disk->d_mediasize = media_size;
1518 part->disk->d_stripesize = 0;
1519 part->disk->d_fwsectors = 0;
1520 part->disk->d_fwheads = 0;
1523 * Acquire a reference to the periph before we register with GEOM.
1524 * We'll release this reference once GEOM calls us back (via
1525 * sddadiskgonecb()) telling us that our provider has been freed.
1527 if (cam_periph_acquire(periph) != 0) {
1528 xpt_print(periph->path, "%s: lost periph during "
1529 "registration!\n", __func__);
1530 cam_periph_lock(periph);
1533 disk_create(part->disk, DISK_VERSION);
1534 cam_periph_lock(periph);
1535 cam_periph_unhold(periph);
1539 * For MMC cards, process EXT_CSD and add partitions that are supported by
1543 sdda_process_mmc_partitions(struct cam_periph *periph, union ccb *ccb)
1545 struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
1546 struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1547 off_t erase_size, sector_size, size, wp_size;
1549 const uint8_t *ext_csd;
1553 ext_csd = sc->raw_ext_csd;
1556 * Enhanced user data area and general purpose partitions are only
1557 * supported in revision 1.4 (EXT_CSD_REV == 4) and later, the RPMB
1558 * partition in revision 1.5 (MMC v4.41, EXT_CSD_REV == 5) and later.
1560 rev = ext_csd[EXT_CSD_REV];
1563 * Ignore user-creatable enhanced user data area and general purpose
1564 * partitions partitions as long as partitioning hasn't been finished.
1566 comp = (ext_csd[EXT_CSD_PART_SET] & EXT_CSD_PART_SET_COMPLETED) != 0;
1569 * Add enhanced user data area slice, unless it spans the entirety of
1570 * the user data area. The enhanced area is of a multiple of high
1571 * capacity write protect groups ((ERASE_GRP_SIZE + HC_WP_GRP_SIZE) *
1572 * 512 KB) and its offset given in either sectors or bytes, depending
1573 * on whether it's a high capacity device or not.
1574 * NB: The slicer and its slices need to be registered before adding
1575 * the disk for the corresponding user data area as re-tasting is
1578 sector_size = mmc_get_sector_size(periph);
1579 size = ext_csd[EXT_CSD_ENH_SIZE_MULT] +
1580 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 1] << 8) +
1581 (ext_csd[EXT_CSD_ENH_SIZE_MULT + 2] << 16);
1582 if (rev >= 4 && comp == TRUE && size > 0 &&
1583 (ext_csd[EXT_CSD_PART_SUPPORT] &
1584 EXT_CSD_PART_SUPPORT_ENH_ATTR_EN) != 0 &&
1585 (ext_csd[EXT_CSD_PART_ATTR] & (EXT_CSD_PART_ATTR_ENH_USR)) != 0) {
1586 erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
1588 wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1589 size *= erase_size * wp_size;
1590 if (size != mmc_get_media_size(periph) * sector_size) {
1591 sc->enh_size = size;
1592 sc->enh_base = (ext_csd[EXT_CSD_ENH_START_ADDR] +
1593 (ext_csd[EXT_CSD_ENH_START_ADDR + 1] << 8) +
1594 (ext_csd[EXT_CSD_ENH_START_ADDR + 2] << 16) +
1595 (ext_csd[EXT_CSD_ENH_START_ADDR + 3] << 24)) *
1596 ((mmcp->card_features & CARD_FEATURE_SDHC) ? 1: MMC_SECTOR_SIZE);
1598 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1599 ("enhanced user data area spans entire device"));
1603 * Add default partition. This may be the only one or the user
1604 * data area in case partitions are supported.
1606 ro = sdda_get_read_only(periph, ccb);
1607 sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_DEFAULT, "sdda",
1608 periph->unit_number, mmc_get_media_size(periph), ro);
1609 sc->part_curr = EXT_CSD_PART_CONFIG_ACC_DEFAULT;
1611 if (mmc_get_spec_vers(periph) < 3)
1614 /* Belatedly announce enhanced user data slice. */
1615 if (sc->enh_size != 0) {
1616 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1617 ("enhanced user data area off 0x%jx size %ju bytes\n",
1618 sc->enh_base, sc->enh_size));
1622 * Determine partition switch timeout (provided in units of 10 ms)
1623 * and ensure it's at least 300 ms as some eMMC chips lie.
1625 sc->part_time = max(ext_csd[EXT_CSD_PART_SWITCH_TO] * 10 * 1000,
1628 /* Add boot partitions, which are of a fixed multiple of 128 KB. */
1629 size = ext_csd[EXT_CSD_BOOT_SIZE_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
1630 if (size > 0 && (sdda_get_host_caps(periph, ccb) & MMC_CAP_BOOT_NOACC) == 0) {
1631 sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1632 SDDA_FMT_BOOT, 0, size,
1633 ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
1634 EXT_CSD_BOOT_WP_STATUS_BOOT0_MASK) != 0));
1635 sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1636 SDDA_FMT_BOOT, 1, size,
1637 ro | ((ext_csd[EXT_CSD_BOOT_WP_STATUS] &
1638 EXT_CSD_BOOT_WP_STATUS_BOOT1_MASK) != 0));
1641 /* Add RPMB partition, which also is of a fixed multiple of 128 KB. */
1642 size = ext_csd[EXT_CSD_RPMB_MULT] * MMC_BOOT_RPMB_BLOCK_SIZE;
1643 if (rev >= 5 && size > 0)
1644 sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_RPMB,
1645 SDDA_FMT_RPMB, 0, size, ro);
1647 if (rev <= 3 || comp == FALSE)
1651 * Add general purpose partitions, which are of a multiple of high
1652 * capacity write protect groups, too.
1654 if ((ext_csd[EXT_CSD_PART_SUPPORT] & EXT_CSD_PART_SUPPORT_EN) != 0) {
1655 erase_size = ext_csd[EXT_CSD_ERASE_GRP_SIZE] * 1024 *
1657 wp_size = ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1658 for (i = 0; i < MMC_PART_GP_MAX; i++) {
1659 size = ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3] +
1660 (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 1] << 8) +
1661 (ext_csd[EXT_CSD_GP_SIZE_MULT + i * 3 + 2] << 16);
1664 sdda_add_part(periph, EXT_CSD_PART_CONFIG_ACC_GP0 + i,
1665 SDDA_FMT_GP, i, size * erase_size * wp_size, ro);
1671 * We cannot just call mmc_switch() since it will sleep, and we are in
1672 * GEOM context and cannot sleep. Instead, create an MMCIO request to switch
1673 * partitions and send it to h/w, and upon completion resume processing
1675 * This function cannot fail, instead check switch errors in sddadone().
1678 sdda_init_switch_part(struct cam_periph *periph, union ccb *start_ccb, u_int part) {
1679 struct sdda_softc *sc = (struct sdda_softc *)periph->softc;
1682 sc->part_requested = part;
1684 value = (sc->raw_ext_csd[EXT_CSD_PART_CONFIG] &
1685 ~EXT_CSD_PART_CONFIG_ACC_MASK) | part;
1687 mmc_switch_fill_mmcio(start_ccb, EXT_CSD_CMD_SET_NORMAL,
1688 EXT_CSD_PART_CONFIG, value, sc->part_time);
1689 start_ccb->ccb_h.cbfcnp = sddadone;
1691 sc->outstanding_cmds++;
1692 cam_periph_unlock(periph);
1693 xpt_action(start_ccb);
1694 cam_periph_lock(periph);
1697 /* Called with periph lock held! */
1699 sddastart(struct cam_periph *periph, union ccb *start_ccb)
1702 struct sdda_softc *softc = (struct sdda_softc *)periph->softc;
1703 struct sdda_part *part;
1704 struct mmc_params *mmcp = &periph->path->device->mmc_ident_data;
1707 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sddastart\n"));
1709 if (softc->state != SDDA_STATE_NORMAL) {
1710 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("device is not in SDDA_STATE_NORMAL yet\n"));
1711 xpt_release_ccb(start_ccb);
1715 /* Find partition that has outstanding commands. Prefer current partition. */
1716 part = softc->part[softc->part_curr];
1717 bp = bioq_first(&part->bio_queue);
1719 for (part_index = 0; part_index < MMC_PART_MAX; part_index++) {
1720 if ((part = softc->part[part_index]) != NULL &&
1721 (bp = bioq_first(&softc->part[part_index]->bio_queue)) != NULL)
1726 xpt_release_ccb(start_ccb);
1729 if (part_index != softc->part_curr) {
1730 CAM_DEBUG(periph->path, CAM_DEBUG_PERIPH,
1731 ("Partition %d -> %d\n", softc->part_curr, part_index));
1733 * According to section "6.2.2 Command restrictions" of the eMMC
1734 * specification v5.1, CMD19/CMD21 aren't allowed to be used with
1735 * RPMB partitions. So we pause re-tuning along with triggering
1736 * it up-front to decrease the likelihood of re-tuning becoming
1737 * necessary while accessing an RPMB partition. Consequently, an
1738 * RPMB partition should immediately be switched away from again
1739 * after an access in order to allow for re-tuning to take place
1742 /* TODO: pause retune if switching to RPMB partition */
1743 softc->state = SDDA_STATE_PART_SWITCH;
1744 sdda_init_switch_part(periph, start_ccb, part_index);
1748 bioq_remove(&part->bio_queue, bp);
1750 switch (bp->bio_cmd) {
1752 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_WRITE\n"));
1753 part->flags |= SDDA_FLAG_DIRTY;
1757 struct ccb_mmcio *mmcio;
1758 uint64_t blockno = bp->bio_pblkno;
1759 uint16_t count = bp->bio_bcount / 512;
1762 if (bp->bio_cmd == BIO_READ)
1763 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_READ\n"));
1764 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1765 ("Block %"PRIu64" cnt %u\n", blockno, count));
1767 /* Construct new MMC command */
1768 if (bp->bio_cmd == BIO_READ) {
1770 opcode = MMC_READ_MULTIPLE_BLOCK;
1772 opcode = MMC_READ_SINGLE_BLOCK;
1775 opcode = MMC_WRITE_MULTIPLE_BLOCK;
1777 opcode = MMC_WRITE_BLOCK;
1780 start_ccb->ccb_h.func_code = XPT_MMC_IO;
1781 start_ccb->ccb_h.flags = (bp->bio_cmd == BIO_READ ? CAM_DIR_IN : CAM_DIR_OUT);
1782 start_ccb->ccb_h.retry_count = 0;
1783 start_ccb->ccb_h.timeout = 15 * 1000;
1784 start_ccb->ccb_h.cbfcnp = sddadone;
1786 mmcio = &start_ccb->mmcio;
1787 mmcio->cmd.opcode = opcode;
1788 mmcio->cmd.arg = blockno;
1789 if (!(mmcp->card_features & CARD_FEATURE_SDHC))
1790 mmcio->cmd.arg <<= 9;
1792 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1793 mmcio->cmd.data = softc->mmcdata;
1794 mmcio->cmd.data->data = bp->bio_data;
1795 mmcio->cmd.data->len = 512 * count;
1796 mmcio->cmd.data->flags = (bp->bio_cmd == BIO_READ ? MMC_DATA_READ : MMC_DATA_WRITE);
1797 /* Direct h/w to issue CMD12 upon completion */
1799 mmcio->cmd.data->flags |= MMC_DATA_MULTI;
1800 mmcio->stop.opcode = MMC_STOP_TRANSMISSION;
1801 mmcio->stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
1802 mmcio->stop.arg = 0;
1808 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_FLUSH\n"));
1809 sddaschedule(periph);
1812 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("BIO_DELETE\n"));
1813 sddaschedule(periph);
1816 start_ccb->ccb_h.ccb_bp = bp;
1817 softc->outstanding_cmds++;
1819 cam_periph_unlock(periph);
1820 xpt_action(start_ccb);
1821 cam_periph_lock(periph);
1823 /* May have more work to do, so ensure we stay scheduled */
1824 sddaschedule(periph);
1828 sddadone(struct cam_periph *periph, union ccb *done_ccb)
1831 struct sdda_softc *softc;
1832 struct ccb_mmcio *mmcio;
1833 struct cam_path *path;
1834 uint32_t card_status;
1837 softc = (struct sdda_softc *)periph->softc;
1838 mmcio = &done_ccb->mmcio;
1839 path = done_ccb->ccb_h.path;
1841 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("sddadone\n"));
1842 // cam_periph_lock(periph);
1843 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1844 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Error!!!\n"));
1845 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1846 cam_release_devq(path,
1850 /*getcount_only*/0);
1851 error = 5; /* EIO */
1853 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1854 panic("REQ_CMP with QFRZN");
1858 card_status = mmcio->cmd.resp[0];
1859 CAM_DEBUG(path, CAM_DEBUG_TRACE,
1860 ("Card status: %08x\n", R1_STATUS(card_status)));
1861 CAM_DEBUG(path, CAM_DEBUG_TRACE,
1862 ("Current state: %d\n", R1_CURRENT_STATE(card_status)));
1864 /* Process result of switching MMC partitions */
1865 if (softc->state == SDDA_STATE_PART_SWITCH) {
1866 CAM_DEBUG(path, CAM_DEBUG_TRACE,
1867 ("Compteting partition switch to %d\n", softc->part_requested));
1868 softc->outstanding_cmds--;
1869 /* Complete partition switch */
1870 softc->state = SDDA_STATE_NORMAL;
1871 if (error != MMC_ERR_NONE) {
1872 /* TODO: Unpause retune if accessing RPMB */
1873 xpt_release_ccb(done_ccb);
1874 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1878 softc->raw_ext_csd[EXT_CSD_PART_CONFIG] =
1879 (softc->raw_ext_csd[EXT_CSD_PART_CONFIG] &
1880 ~EXT_CSD_PART_CONFIG_ACC_MASK) | softc->part_requested;
1881 /* TODO: Unpause retune if accessing RPMB */
1882 softc->part_curr = softc->part_requested;
1883 xpt_release_ccb(done_ccb);
1885 /* Return to processing BIO requests */
1886 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1890 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1891 bp->bio_error = error;
1893 bp->bio_resid = bp->bio_bcount;
1894 bp->bio_flags |= BIO_ERROR;
1896 /* XXX: How many bytes remaining? */
1898 if (bp->bio_resid > 0)
1899 bp->bio_flags |= BIO_ERROR;
1902 softc->outstanding_cmds--;
1903 xpt_release_ccb(done_ccb);
1905 * Release the periph refcount taken in sddastart() for each CCB.
1907 KASSERT(softc->refcount >= 1, ("sddadone softc %p refcount %d", softc, softc->refcount));
1913 sddaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1915 return(cam_periph_error(ccb, cam_flags, sense_flags));
1917 #endif /* _KERNEL */