1 /*-
2 * Copyright (c) 2017 Ilya Bakulin. All rights reserved.
3 * Copyright (c) 2018-2019 The FreeBSD Foundation
4 *
5 * Portions of this software were developed by Björn Zeeb
6 * under sponsorship from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 *
29 * Portions of this software may have been developed with reference to
30 * the SD Simplified Specification. The following disclaimer may apply:
31 *
32 * The following conditions apply to the release of the simplified
33 * specification ("Simplified Specification") by the SD Card Association and
34 * the SD Group. The Simplified Specification is a subset of the complete SD
35 * Specification which is owned by the SD Card Association and the SD
36 * Group. This Simplified Specification is provided on a non-confidential
37 * basis subject to the disclaimers below. Any implementation of the
38 * Simplified Specification may require a license from the SD Card
39 * Association, SD Group, SD-3C LLC or other third parties.
40 *
41 * Disclaimers:
42 *
43 * The information contained in the Simplified Specification is presented only
44 * as a standard specification for SD Cards and SD Host/Ancillary products and
45 * is provided "AS-IS" without any representations or warranties of any
46 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
47 * Card Association for any damages, any infringements of patents or other
48 * right of the SD Group, SD-3C LLC, the SD Card Association or any third
49 * parties, which may result from its use. No license is granted by
50 * implication, estoppel or otherwise under any patent or other rights of the
51 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
52 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
53 * or the SD Card Association to disclose or distribute any technical
54 * information, know-how or other confidential information to any third party.
55 */
56 /*
57 * Implements the (kernel specific) SDIO parts.
58 * This will hide all cam(4) functionality from the SDIO driver implementations
59 * which will just be newbus/device(9) and hence look like any other driver for,
60 * e.g., PCI.
61 * The sdiob(4) parts effetively "translate" between the two worlds "bridging"
62 * messages from MMCCAM to newbus and back.
63 */
64
65 #include <sys/cdefs.h>
66 #include "opt_cam.h"
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/types.h>
71 #include <sys/kernel.h>
72 #include <sys/bus.h>
73 #include <sys/endian.h>
74 #include <sys/lock.h>
75 #include <sys/malloc.h>
76 #include <sys/module.h>
77 #include <sys/mutex.h>
78
79 #include <cam/cam.h>
80 #include <cam/cam_ccb.h>
81 #include <cam/cam_queue.h>
82 #include <cam/cam_periph.h>
83 #include <cam/cam_xpt.h>
84 #include <cam/cam_xpt_periph.h>
85 #include <cam/cam_xpt_internal.h> /* for cam_path */
86 #include <cam/cam_debug.h>
87
88 #include <dev/mmc/mmcreg.h>
89
90 #include <dev/sdio/sdiob.h>
91 #include <dev/sdio/sdio_subr.h>
92
93 #include "sdio_if.h"
94
95 #ifdef DEBUG
96 #define DPRINTF(...) printf(__VA_ARGS__)
97 #define DPRINTFDEV(_dev, ...) device_printf((_dev), __VA_ARGS__)
98 #else
99 #define DPRINTF(...)
100 #define DPRINTFDEV(_dev, ...)
101 #endif
102
103 struct sdiob_softc {
104 uint32_t sdio_state;
105 #define SDIO_STATE_DEAD 0x0001
106 #define SDIO_STATE_INITIALIZING 0x0002
107 #define SDIO_STATE_READY 0x0004
108 uint32_t nb_state;
109 #define NB_STATE_DEAD 0x0001
110 #define NB_STATE_SIM_ADDED 0x0002
111 #define NB_STATE_READY 0x0004
112
113 /* CAM side. */
114 struct card_info cardinfo;
115 struct cam_periph *periph;
116 union ccb *ccb;
117 struct task discover_task;
118
119 /* Newbus side. */
120 device_t dev; /* Ourselves. */
121 device_t child[8];
122 };
123
124 /* -------------------------------------------------------------------------- */
125 /*
126 * SDIO CMD52 and CM53 implementations along with wrapper functions for
127 * read/write and a CAM periph helper function.
128 * These are the backend implementations of the sdio_if.m framework talking
129 * through CAM to sdhci.
130 * Note: these functions are also called during early discovery stage when
131 * we are not a device(9) yet. Hence they cannot always use device_printf()
132 * to log errors and have to call CAM_DEBUG() during these early stages.
133 */
134
135 static int
sdioerror(union ccb * ccb,u_int32_t cam_flags,u_int32_t sense_flags)136 sdioerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
137 {
138
139 return (cam_periph_error(ccb, cam_flags, sense_flags));
140 }
141
142 /* CMD52: direct byte access. */
143 static int
sdiob_rw_direct_sc(struct sdiob_softc * sc,uint8_t fn,uint32_t addr,bool wr,uint8_t * val)144 sdiob_rw_direct_sc(struct sdiob_softc *sc, uint8_t fn, uint32_t addr, bool wr,
145 uint8_t *val)
146 {
147 uint32_t arg, flags;
148 int error;
149
150 KASSERT((val != NULL), ("%s val passed as NULL\n", __func__));
151
152 if (sc->ccb == NULL)
153 sc->ccb = xpt_alloc_ccb();
154 else
155 memset(sc->ccb, 0, sizeof(*sc->ccb));
156 xpt_setup_ccb(&sc->ccb->ccb_h, sc->periph->path, CAM_PRIORITY_NONE);
157 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_TRACE,
158 ("%s(fn=%d, addr=%#02x, wr=%d, *val=%#02x)\n", __func__,
159 fn, addr, wr, *val));
160
161 flags = MMC_RSP_R5 | MMC_CMD_AC;
162 arg = SD_IO_RW_FUNC(fn) | SD_IO_RW_ADR(addr);
163 if (wr)
164 arg |= SD_IO_RW_WR | SD_IO_RW_RAW | SD_IO_RW_DAT(*val);
165
166 cam_fill_mmcio(&sc->ccb->mmcio,
167 /*retries*/ 0,
168 /*cbfcnp*/ NULL,
169 /*flags*/ CAM_DIR_NONE,
170 /*mmc_opcode*/ SD_IO_RW_DIRECT,
171 /*mmc_arg*/ arg,
172 /*mmc_flags*/ flags,
173 /*mmc_data*/ 0,
174 /*timeout*/ sc->cardinfo.f[fn].timeout);
175 error = cam_periph_runccb(sc->ccb, sdioerror, CAM_FLAG_NONE, 0, NULL);
176 if (error != 0) {
177 if (sc->dev != NULL)
178 device_printf(sc->dev,
179 "%s: Failed to %s address %#10x error=%d\n",
180 __func__, (wr) ? "write" : "read", addr, error);
181 else
182 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_INFO,
183 ("%s: Failed to %s address: %#10x error=%d\n",
184 __func__, (wr) ? "write" : "read", addr, error));
185 return (error);
186 }
187
188 /* TODO: Add handling of MMC errors */
189 /* ccb->mmcio.cmd.error ? */
190 if (wr == false)
191 *val = sc->ccb->mmcio.cmd.resp[0] & 0xff;
192
193 return (0);
194 }
195
196 static int
sdio_rw_direct(device_t dev,uint8_t fn,uint32_t addr,bool wr,uint8_t * val)197 sdio_rw_direct(device_t dev, uint8_t fn, uint32_t addr, bool wr,
198 uint8_t *val)
199 {
200 struct sdiob_softc *sc;
201 int error;
202
203 sc = device_get_softc(dev);
204 cam_periph_lock(sc->periph);
205 error = sdiob_rw_direct_sc(sc, fn, addr, wr, val);
206 cam_periph_unlock(sc->periph);
207 return (error);
208 }
209
210 static int
sdiob_read_direct(device_t dev,uint8_t fn,uint32_t addr,uint8_t * val)211 sdiob_read_direct(device_t dev, uint8_t fn, uint32_t addr, uint8_t *val)
212 {
213 int error;
214 uint8_t v;
215
216 error = sdio_rw_direct(dev, fn, addr, false, &v);
217 /* Be polite and do not touch the value on read error. */
218 if (error == 0 && val != NULL)
219 *val = v;
220 return (error);
221 }
222
223 static int
sdiob_write_direct(device_t dev,uint8_t fn,uint32_t addr,uint8_t val)224 sdiob_write_direct(device_t dev, uint8_t fn, uint32_t addr, uint8_t val)
225 {
226
227 return (sdio_rw_direct(dev, fn, addr, true, &val));
228 }
229
230 /*
231 * CMD53: IO_RW_EXTENDED, read and write multiple I/O registers.
232 * Increment false gets FIFO mode (single register address).
233 */
234 /*
235 * A b_count of 0 means byte mode, b_count > 0 gets block mode.
236 * A b_count of >= 512 would mean infinitive block transfer, which would become
237 * b_count = 0, is not yet supported.
238 * For b_count == 0, blksz is the len of bytes, otherwise it is the amount of
239 * full sized blocks (you must not round the blocks up and leave the last one
240 * partial!)
241 * For byte mode, the maximum of blksz is the functions cur_blksize.
242 * This function should ever only be called by sdio_rw_extended_sc()!
243 */
244 static int
sdiob_rw_extended_cam(struct sdiob_softc * sc,uint8_t fn,uint32_t addr,bool wr,uint8_t * buffer,bool incaddr,uint32_t b_count,uint16_t blksz)245 sdiob_rw_extended_cam(struct sdiob_softc *sc, uint8_t fn, uint32_t addr,
246 bool wr, uint8_t *buffer, bool incaddr, uint32_t b_count, uint16_t blksz)
247 {
248 struct mmc_data mmcd;
249 uint32_t arg, cam_flags, flags, len;
250 int error;
251
252 if (sc->ccb == NULL)
253 sc->ccb = xpt_alloc_ccb();
254 else
255 memset(sc->ccb, 0, sizeof(*sc->ccb));
256 xpt_setup_ccb(&sc->ccb->ccb_h, sc->periph->path, CAM_PRIORITY_NONE);
257 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_TRACE,
258 ("%s(fn=%d addr=%#0x wr=%d b_count=%u blksz=%u buf=%p incr=%d)\n",
259 __func__, fn, addr, wr, b_count, blksz, buffer, incaddr));
260
261 KASSERT((b_count <= 511), ("%s: infinitive block transfer not yet "
262 "supported: b_count %u blksz %u, sc %p, fn %u, addr %#10x, %s, "
263 "buffer %p, %s\n", __func__, b_count, blksz, sc, fn, addr,
264 wr ? "wr" : "rd", buffer, incaddr ? "incaddr" : "fifo"));
265 /* Blksz needs to be within bounds for both byte and block mode! */
266 KASSERT((blksz <= sc->cardinfo.f[fn].cur_blksize), ("%s: blksz "
267 "%u > bur_blksize %u, sc %p, fn %u, addr %#10x, %s, "
268 "buffer %p, %s, b_count %u\n", __func__, blksz,
269 sc->cardinfo.f[fn].cur_blksize, sc, fn, addr,
270 wr ? "wr" : "rd", buffer, incaddr ? "incaddr" : "fifo",
271 b_count));
272 if (b_count == 0) {
273 /* Byte mode */
274 len = blksz;
275 if (blksz == 512)
276 blksz = 0;
277 arg = SD_IOE_RW_LEN(blksz);
278 } else {
279 /* Block mode. */
280 #ifdef __notyet__
281 if (b_count > 511) {
282 /* Infinitive block transfer. */
283 b_count = 0;
284 }
285 #endif
286 len = b_count * blksz;
287 arg = SD_IOE_RW_BLK | SD_IOE_RW_LEN(b_count);
288 }
289
290 flags = MMC_RSP_R5 | MMC_CMD_ADTC;
291 arg |= SD_IOE_RW_FUNC(fn) | SD_IOE_RW_ADR(addr);
292 if (incaddr)
293 arg |= SD_IOE_RW_INCR;
294
295 memset(&mmcd, 0, sizeof(mmcd));
296 mmcd.data = buffer;
297 mmcd.len = len;
298 if (arg & SD_IOE_RW_BLK) {
299 /* XXX both should be known from elsewhere, aren't they? */
300 mmcd.block_size = blksz;
301 mmcd.block_count = b_count;
302 }
303
304 if (wr) {
305 arg |= SD_IOE_RW_WR;
306 cam_flags = CAM_DIR_OUT;
307 mmcd.flags = MMC_DATA_WRITE;
308 } else {
309 cam_flags = CAM_DIR_IN;
310 mmcd.flags = MMC_DATA_READ;
311 }
312 #ifdef __notyet__
313 if (b_count == 0) {
314 /* XXX-BZ TODO FIXME. Cancel I/O: CCCR -> ASx */
315 /* Stop cmd. */
316 }
317 #endif
318 cam_fill_mmcio(&sc->ccb->mmcio,
319 /*retries*/ 0,
320 /*cbfcnp*/ NULL,
321 /*flags*/ cam_flags,
322 /*mmc_opcode*/ SD_IO_RW_EXTENDED,
323 /*mmc_arg*/ arg,
324 /*mmc_flags*/ flags,
325 /*mmc_data*/ &mmcd,
326 /*timeout*/ sc->cardinfo.f[fn].timeout);
327 if (arg & SD_IOE_RW_BLK) {
328 mmcd.flags |= MMC_DATA_BLOCK_SIZE;
329 if (b_count != 1)
330 sc->ccb->mmcio.cmd.data->flags |= MMC_DATA_MULTI;
331 }
332
333 /* Execute. */
334 error = cam_periph_runccb(sc->ccb, sdioerror, CAM_FLAG_NONE, 0, NULL);
335 if (error != 0) {
336 if (sc->dev != NULL)
337 device_printf(sc->dev,
338 "%s: Failed to %s address %#10x buffer %p size %u "
339 "%s b_count %u blksz %u error=%d\n",
340 __func__, (wr) ? "write to" : "read from", addr,
341 buffer, len, (incaddr) ? "incr" : "fifo",
342 b_count, blksz, error);
343 else
344 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_INFO,
345 ("%s: Failed to %s address %#10x buffer %p size %u "
346 "%s b_count %u blksz %u error=%d\n",
347 __func__, (wr) ? "write to" : "read from", addr,
348 buffer, len, (incaddr) ? "incr" : "fifo",
349 b_count, blksz, error));
350 return (error);
351 }
352
353 /* TODO: Add handling of MMC errors */
354 /* ccb->mmcio.cmd.error ? */
355 error = sc->ccb->mmcio.cmd.resp[0] & 0xff;
356 if (error != 0) {
357 if (sc->dev != NULL)
358 device_printf(sc->dev,
359 "%s: Failed to %s address %#10x buffer %p size %u "
360 "%s b_count %u blksz %u mmcio resp error=%d\n",
361 __func__, (wr) ? "write to" : "read from", addr,
362 buffer, len, (incaddr) ? "incr" : "fifo",
363 b_count, blksz, error);
364 else
365 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_INFO,
366 ("%s: Failed to %s address %#10x buffer %p size %u "
367 "%s b_count %u blksz %u mmcio resp error=%d\n",
368 __func__, (wr) ? "write to" : "read from", addr,
369 buffer, len, (incaddr) ? "incr" : "fifo",
370 b_count, blksz, error));
371 }
372 return (error);
373 }
374
375 static int
sdiob_rw_extended_sc(struct sdiob_softc * sc,uint8_t fn,uint32_t addr,bool wr,uint32_t size,uint8_t * buffer,bool incaddr)376 sdiob_rw_extended_sc(struct sdiob_softc *sc, uint8_t fn, uint32_t addr,
377 bool wr, uint32_t size, uint8_t *buffer, bool incaddr)
378 {
379 int error;
380 uint32_t len;
381 uint32_t b_count;
382
383 /*
384 * If block mode is supported and we have at least 4 bytes to write and
385 * the size is at least one block, then start doing blk transfers.
386 */
387 while (sc->cardinfo.support_multiblk &&
388 size > 4 && size >= sc->cardinfo.f[fn].cur_blksize) {
389 b_count = size / sc->cardinfo.f[fn].cur_blksize;
390 KASSERT(b_count >= 1, ("%s: block count too small %u size %u "
391 "cur_blksize %u\n", __func__, b_count, size,
392 sc->cardinfo.f[fn].cur_blksize));
393
394 #ifdef __notyet__
395 /* XXX support inifinite transfer with b_count = 0. */
396 #else
397 if (b_count > 511)
398 b_count = 511;
399 #endif
400 len = b_count * sc->cardinfo.f[fn].cur_blksize;
401 error = sdiob_rw_extended_cam(sc, fn, addr, wr, buffer, incaddr,
402 b_count, sc->cardinfo.f[fn].cur_blksize);
403 if (error != 0)
404 return (error);
405
406 size -= len;
407 buffer += len;
408 if (incaddr)
409 addr += len;
410 }
411
412 while (size > 0) {
413 len = MIN(size, sc->cardinfo.f[fn].cur_blksize);
414
415 error = sdiob_rw_extended_cam(sc, fn, addr, wr, buffer, incaddr,
416 0, len);
417 if (error != 0)
418 return (error);
419
420 /* Prepare for next iteration. */
421 size -= len;
422 buffer += len;
423 if (incaddr)
424 addr += len;
425 }
426
427 return (0);
428 }
429
430 static int
sdiob_rw_extended(device_t dev,uint8_t fn,uint32_t addr,bool wr,uint32_t size,uint8_t * buffer,bool incaddr)431 sdiob_rw_extended(device_t dev, uint8_t fn, uint32_t addr, bool wr,
432 uint32_t size, uint8_t *buffer, bool incaddr)
433 {
434 struct sdiob_softc *sc;
435 int error;
436
437 sc = device_get_softc(dev);
438 cam_periph_lock(sc->periph);
439 error = sdiob_rw_extended_sc(sc, fn, addr, wr, size, buffer, incaddr);
440 cam_periph_unlock(sc->periph);
441 return (error);
442 }
443
444 static int
sdiob_read_extended(device_t dev,uint8_t fn,uint32_t addr,uint32_t size,uint8_t * buffer,bool incaddr)445 sdiob_read_extended(device_t dev, uint8_t fn, uint32_t addr, uint32_t size,
446 uint8_t *buffer, bool incaddr)
447 {
448
449 return (sdiob_rw_extended(dev, fn, addr, false, size, buffer, incaddr));
450 }
451
452 static int
sdiob_write_extended(device_t dev,uint8_t fn,uint32_t addr,uint32_t size,uint8_t * buffer,bool incaddr)453 sdiob_write_extended(device_t dev, uint8_t fn, uint32_t addr, uint32_t size,
454 uint8_t *buffer, bool incaddr)
455 {
456
457 return (sdiob_rw_extended(dev, fn, addr, true, size, buffer, incaddr));
458 }
459
460 /* -------------------------------------------------------------------------- */
461 /* Bus interface, ivars handling. */
462
463 static int
sdiob_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)464 sdiob_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
465 {
466 struct sdiob_softc *sc;
467 struct sdio_func *f;
468
469 f = device_get_ivars(child);
470 KASSERT(f != NULL, ("%s: dev %p child %p which %d, child ivars NULL\n",
471 __func__, dev, child, which));
472
473 switch (which) {
474 case SDIOB_IVAR_SUPPORT_MULTIBLK:
475 sc = device_get_softc(dev);
476 KASSERT(sc != NULL, ("%s: dev %p child %p which %d, sc NULL\n",
477 __func__, dev, child, which));
478 *result = sc->cardinfo.support_multiblk;
479 break;
480 case SDIOB_IVAR_FUNCTION:
481 *result = (uintptr_t)f;
482 break;
483 case SDIOB_IVAR_FUNCNUM:
484 *result = f->fn;
485 break;
486 case SDIOB_IVAR_CLASS:
487 *result = f->class;
488 break;
489 case SDIOB_IVAR_VENDOR:
490 *result = f->vendor;
491 break;
492 case SDIOB_IVAR_DEVICE:
493 *result = f->device;
494 break;
495 case SDIOB_IVAR_DRVDATA:
496 *result = f->drvdata;
497 break;
498 default:
499 return (ENOENT);
500 }
501 return (0);
502 }
503
504 static int
sdiob_write_ivar(device_t dev,device_t child,int which,uintptr_t value)505 sdiob_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
506 {
507 struct sdio_func *f;
508
509 f = device_get_ivars(child);
510 KASSERT(f != NULL, ("%s: dev %p child %p which %d, child ivars NULL\n",
511 __func__, dev, child, which));
512
513 switch (which) {
514 case SDIOB_IVAR_SUPPORT_MULTIBLK:
515 case SDIOB_IVAR_FUNCTION:
516 case SDIOB_IVAR_FUNCNUM:
517 case SDIOB_IVAR_CLASS:
518 case SDIOB_IVAR_VENDOR:
519 case SDIOB_IVAR_DEVICE:
520 return (EINVAL); /* Disallowed. */
521 case SDIOB_IVAR_DRVDATA:
522 f->drvdata = value;
523 break;
524 default:
525 return (ENOENT);
526 }
527
528 return (0);
529 }
530
531 /* -------------------------------------------------------------------------- */
532 /*
533 * Newbus functions for ourselves to probe/attach/detach and become a proper
534 * device(9). Attach will also probe for child devices (another driver
535 * implementing SDIO).
536 */
537
538 static int
sdiob_probe(device_t dev)539 sdiob_probe(device_t dev)
540 {
541
542 device_set_desc(dev, "SDIO CAM-Newbus bridge");
543 return (BUS_PROBE_DEFAULT);
544 }
545
546 static int
sdiob_attach(device_t dev)547 sdiob_attach(device_t dev)
548 {
549 struct sdiob_softc *sc;
550 int error, i;
551
552 sc = device_get_softc(dev);
553 if (sc == NULL)
554 return (ENXIO);
555
556 /*
557 * Now that we are a dev, create one child device per function,
558 * initialize the backpointer, so we can pass them around and
559 * call CAM operations on the parent, and also set the function
560 * itself as ivars, so that we can query/update them.
561 * Do this before any child gets a chance to attach.
562 */
563 for (i = 0; i < sc->cardinfo.num_funcs; i++) {
564 sc->child[i] = device_add_child(dev, NULL, -1);
565 if (sc->child[i] == NULL) {
566 device_printf(dev, "%s: failed to add child\n", __func__);
567 return (ENXIO);
568 }
569 sc->cardinfo.f[i].dev = sc->child[i];
570
571 /* Set the function as ivar to the child device. */
572 device_set_ivars(sc->child[i], &sc->cardinfo.f[i]);
573 }
574
575 /*
576 * No one will ever attach to F0; we do the above to have a "device"
577 * to talk to in a general way in the code.
578 * Also do the probe/attach in a 2nd loop, so that all devices are
579 * present as we do have drivers consuming more than one device/func
580 * and might play "tricks" in order to do that assuming devices and
581 * ivars are available for all.
582 */
583 for (i = 1; i < sc->cardinfo.num_funcs; i++) {
584 error = device_probe_and_attach(sc->child[i]);
585 if (error != 0 && bootverbose)
586 device_printf(dev, "%s: device_probe_and_attach(%p %s) "
587 "failed %d for function %d, no child yet\n",
588 __func__,
589 sc->child, device_get_nameunit(sc->child[i]),
590 error, i);
591 }
592
593 sc->nb_state = NB_STATE_READY;
594
595 cam_periph_lock(sc->periph);
596 xpt_announce_periph(sc->periph, NULL);
597 cam_periph_unlock(sc->periph);
598
599 return (0);
600 }
601
602 static int
sdiob_detach(device_t dev)603 sdiob_detach(device_t dev)
604 {
605
606 /* XXX TODO? */
607 return (EOPNOTSUPP);
608 }
609
610 /* -------------------------------------------------------------------------- */
611 /*
612 * driver(9) and device(9) "control plane".
613 * This is what we use when we are making ourselves a device(9) in order to
614 * provide a newbus interface again, as well as the implementation of the
615 * SDIO interface.
616 */
617
618 static device_method_t sdiob_methods[] = {
619 /* Device interface. */
620 DEVMETHOD(device_probe, sdiob_probe),
621 DEVMETHOD(device_attach, sdiob_attach),
622 DEVMETHOD(device_detach, sdiob_detach),
623
624 /* Bus interface. */
625 DEVMETHOD(bus_add_child, bus_generic_add_child),
626 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
627 DEVMETHOD(bus_read_ivar, sdiob_read_ivar),
628 DEVMETHOD(bus_write_ivar, sdiob_write_ivar),
629
630 /* SDIO interface. */
631 DEVMETHOD(sdio_read_direct, sdiob_read_direct),
632 DEVMETHOD(sdio_write_direct, sdiob_write_direct),
633 DEVMETHOD(sdio_read_extended, sdiob_read_extended),
634 DEVMETHOD(sdio_write_extended, sdiob_write_extended),
635
636 DEVMETHOD_END
637 };
638
639 static driver_t sdiob_driver = {
640 SDIOB_NAME_S,
641 sdiob_methods,
642 0
643 };
644
645 /* -------------------------------------------------------------------------- */
646 /*
647 * CIS related.
648 * Read card and function information and populate the cardinfo structure.
649 */
650
651 static int
sdio_read_direct_sc(struct sdiob_softc * sc,uint8_t fn,uint32_t addr,uint8_t * val)652 sdio_read_direct_sc(struct sdiob_softc *sc, uint8_t fn, uint32_t addr,
653 uint8_t *val)
654 {
655 int error;
656 uint8_t v;
657
658 error = sdiob_rw_direct_sc(sc, fn, addr, false, &v);
659 if (error == 0 && val != NULL)
660 *val = v;
661 return (error);
662 }
663
664 static int
sdio_func_read_cis(struct sdiob_softc * sc,uint8_t fn,uint32_t cis_addr)665 sdio_func_read_cis(struct sdiob_softc *sc, uint8_t fn, uint32_t cis_addr)
666 {
667 char cis1_info_buf[256];
668 char *cis1_info[4];
669 int start, i, count, ret;
670 uint32_t addr;
671 uint8_t ch, tuple_id, tuple_len, tuple_count, v;
672
673 /* If we encounter any read errors, abort and return. */
674 #define ERR_OUT(ret) \
675 if (ret != 0) \
676 goto err;
677 ret = 0;
678 /* Use to prevent infinite loop in case of parse errors. */
679 tuple_count = 0;
680 memset(cis1_info_buf, 0, 256);
681 do {
682 addr = cis_addr;
683 ret = sdio_read_direct_sc(sc, 0, addr++, &tuple_id);
684 ERR_OUT(ret);
685 if (tuple_id == SD_IO_CISTPL_END)
686 break;
687 if (tuple_id == 0) {
688 cis_addr++;
689 continue;
690 }
691 ret = sdio_read_direct_sc(sc, 0, addr++, &tuple_len);
692 ERR_OUT(ret);
693 if (tuple_len == 0) {
694 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_PERIPH,
695 ("%s: parse error: 0-length tuple %#02x\n",
696 __func__, tuple_id));
697 return (EIO);
698 }
699
700 switch (tuple_id) {
701 case SD_IO_CISTPL_VERS_1:
702 addr += 2;
703 for (count = 0, start = 0, i = 0;
704 (count < 4) && ((i + 4) < 256); i++) {
705 ret = sdio_read_direct_sc(sc, 0, addr + i, &ch);
706 ERR_OUT(ret);
707 DPRINTF("%s: count=%d, start=%d, i=%d, got "
708 "(%#02x)\n", __func__, count, start, i, ch);
709 if (ch == 0xff)
710 break;
711 cis1_info_buf[i] = ch;
712 if (ch == 0) {
713 cis1_info[count] =
714 cis1_info_buf + start;
715 start = i + 1;
716 count++;
717 }
718 }
719 DPRINTF("Card info: ");
720 for (i=0; i < 4; i++)
721 if (cis1_info[i])
722 DPRINTF(" %s", cis1_info[i]);
723 DPRINTF("\n");
724 break;
725 case SD_IO_CISTPL_MANFID:
726 /* TPLMID_MANF */
727 ret = sdio_read_direct_sc(sc, 0, addr++, &v);
728 ERR_OUT(ret);
729 sc->cardinfo.f[fn].vendor = v;
730 ret = sdio_read_direct_sc(sc, 0, addr++, &v);
731 ERR_OUT(ret);
732 sc->cardinfo.f[fn].vendor |= (v << 8);
733 /* TPLMID_CARD */
734 ret = sdio_read_direct_sc(sc, 0, addr++, &v);
735 ERR_OUT(ret);
736 sc->cardinfo.f[fn].device = v;
737 ret = sdio_read_direct_sc(sc, 0, addr, &v);
738 ERR_OUT(ret);
739 sc->cardinfo.f[fn].device |= (v << 8);
740 break;
741 case SD_IO_CISTPL_FUNCID:
742 /* Not sure if we need to parse it? */
743 break;
744 case SD_IO_CISTPL_FUNCE:
745 if (tuple_len < 4) {
746 printf("%s: FUNCE is too short: %d\n",
747 __func__, tuple_len);
748 break;
749 }
750 /* TPLFE_TYPE (Extended Data) */
751 ret = sdio_read_direct_sc(sc, 0, addr++, &v);
752 ERR_OUT(ret);
753 if (fn == 0) {
754 if (v != 0x00)
755 break;
756 } else {
757 if (v != 0x01)
758 break;
759 addr += 0x0b;
760 }
761 ret = sdio_read_direct_sc(sc, 0, addr, &v);
762 ERR_OUT(ret);
763 sc->cardinfo.f[fn].max_blksize = v;
764 ret = sdio_read_direct_sc(sc, 0, addr+1, &v);
765 ERR_OUT(ret);
766 sc->cardinfo.f[fn].max_blksize |= (v << 8);
767 break;
768 default:
769 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_PERIPH,
770 ("%s: Skipping fn %d tuple %d ID %#02x "
771 "len %#02x\n", __func__, fn, tuple_count,
772 tuple_id, tuple_len));
773 }
774 if (tuple_len == 0xff) {
775 /* Also marks the end of a tuple chain (E1 16.2) */
776 /* The tuple is valid, hence this going at the end. */
777 break;
778 }
779 cis_addr += 2 + tuple_len;
780 tuple_count++;
781 } while (tuple_count < 20);
782 err:
783 #undef ERR_OUT
784 return (ret);
785 }
786
787 static int
sdio_get_common_cis_addr(struct sdiob_softc * sc,uint32_t * addr)788 sdio_get_common_cis_addr(struct sdiob_softc *sc, uint32_t *addr)
789 {
790 int error;
791 uint32_t a;
792 uint8_t val;
793
794 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 0, &val);
795 if (error != 0)
796 goto err;
797 a = val;
798 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 1, &val);
799 if (error != 0)
800 goto err;
801 a |= (val << 8);
802 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 2, &val);
803 if (error != 0)
804 goto err;
805 a |= (val << 16);
806
807 if (a < SD_IO_CIS_START || a > SD_IO_CIS_START + SD_IO_CIS_SIZE) {
808 err:
809 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_PERIPH,
810 ("%s: bad CIS address: %#04x, error %d\n", __func__, a,
811 error));
812 } else if (error == 0 && addr != NULL)
813 *addr = a;
814
815 return (error);
816 }
817
818 static int
sdiob_get_card_info(struct sdiob_softc * sc)819 sdiob_get_card_info(struct sdiob_softc *sc)
820 {
821 struct mmc_params *mmcp;
822 uint32_t cis_addr, fbr_addr;
823 int fn, error;
824 uint8_t fn_max, val;
825
826 error = sdio_get_common_cis_addr(sc, &cis_addr);
827 if (error != 0)
828 return (-1);
829
830 memset(&sc->cardinfo, 0, sizeof(sc->cardinfo));
831
832 /* F0 must always be present. */
833 fn = 0;
834 error = sdio_func_read_cis(sc, fn, cis_addr);
835 if (error != 0)
836 return (error);
837 sc->cardinfo.num_funcs++;
838 /* Read CCCR Card Capability. */
839 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CARDCAP, &val);
840 if (error != 0)
841 return (error);
842 sc->cardinfo.support_multiblk = (val & CCCR_CC_SMB) ? true : false;
843 DPRINTF("%s: F%d: Vendor %#04x product %#04x max block size %d bytes "
844 "support_multiblk %s\n",
845 __func__, fn, sc->cardinfo.f[fn].vendor, sc->cardinfo.f[fn].device,
846 sc->cardinfo.f[fn].max_blksize,
847 sc->cardinfo.support_multiblk ? "yes" : "no");
848
849 /* mmcp->sdio_func_count contains the number of functions w/o F0. */
850 mmcp = &sc->ccb->ccb_h.path->device->mmc_ident_data;
851 fn_max = MIN(mmcp->sdio_func_count + 1, nitems(sc->cardinfo.f));
852 for (fn = 1; fn < fn_max; fn++) {
853 fbr_addr = SD_IO_FBR_START * fn + SD_IO_FBR_CIS_OFFSET;
854
855 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val);
856 if (error != 0)
857 break;
858 cis_addr = val;
859 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val);
860 if (error != 0)
861 break;
862 cis_addr |= (val << 8);
863 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val);
864 if (error != 0)
865 break;
866 cis_addr |= (val << 16);
867
868 error = sdio_func_read_cis(sc, fn, cis_addr);
869 if (error != 0)
870 break;
871
872 /* Read the Standard SDIO Function Interface Code. */
873 fbr_addr = SD_IO_FBR_START * fn;
874 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val);
875 if (error != 0)
876 break;
877 sc->cardinfo.f[fn].class = (val & 0x0f);
878 if (sc->cardinfo.f[fn].class == 0x0f) {
879 error = sdio_read_direct_sc(sc, 0, fbr_addr, &val);
880 if (error != 0)
881 break;
882 sc->cardinfo.f[fn].class = val;
883 }
884
885 sc->cardinfo.f[fn].fn = fn;
886 sc->cardinfo.f[fn].cur_blksize = sc->cardinfo.f[fn].max_blksize;
887 sc->cardinfo.f[fn].retries = 0;
888 sc->cardinfo.f[fn].timeout = 5000;
889
890 DPRINTF("%s: F%d: Class %d Vendor %#04x product %#04x "
891 "max_blksize %d bytes\n", __func__, fn,
892 sc->cardinfo.f[fn].class,
893 sc->cardinfo.f[fn].vendor, sc->cardinfo.f[fn].device,
894 sc->cardinfo.f[fn].max_blksize);
895 if (sc->cardinfo.f[fn].vendor == 0) {
896 DPRINTF("%s: F%d doesn't exist\n", __func__, fn);
897 break;
898 }
899 sc->cardinfo.num_funcs++;
900 }
901 return (error);
902 }
903
904 /* -------------------------------------------------------------------------- */
905 /*
906 * CAM periph registration, allocation, and detached from that a discovery
907 * task, which goes off reads cardinfo, and then adds ourselves to our SIM's
908 * device adding the devclass and registering the driver. This keeps the
909 * newbus chain connected though we will talk CAM in the middle (until one
910 * day CAM might be newbusyfied).
911 */
912
913 static int
sdio_newbus_sim_add(struct sdiob_softc * sc)914 sdio_newbus_sim_add(struct sdiob_softc *sc)
915 {
916 device_t pdev;
917 devclass_t bus_devclass;
918 int error;
919
920 /* Add ourselves to our parent (SIM) device. */
921
922 /* Add ourselves to our parent. That way we can become a parent. */
923 pdev = xpt_path_sim_device(sc->periph->path);
924 KASSERT(pdev != NULL,
925 ("%s: pdev is NULL, sc %p periph %p sim %p\n",
926 __func__, sc, sc->periph, sc->periph->sim));
927
928 if (sc->dev == NULL)
929 sc->dev = BUS_ADD_CHILD(pdev, 0, SDIOB_NAME_S, -1);
930 if (sc->dev == NULL)
931 return (ENXIO);
932 device_set_softc(sc->dev, sc);
933
934 /*
935 * Don't set description here; devclass_add_driver() ->
936 * device_probe_child() -> device_set_driver() will nuke it again.
937 */
938 bus_devclass = device_get_devclass(pdev);
939 if (bus_devclass == NULL) {
940 printf("%s: Failed to get devclass from %s.\n", __func__,
941 device_get_nameunit(pdev));
942 return (ENXIO);
943 }
944
945 bus_topo_lock();
946 error = devclass_add_driver(bus_devclass, &sdiob_driver,
947 BUS_PASS_DEFAULT, NULL);
948 bus_topo_unlock();
949 if (error != 0) {
950 printf("%s: Failed to add driver to devclass: %d.\n",
951 __func__, error);
952 return (error);
953 }
954
955 /* Done. */
956 sc->nb_state = NB_STATE_SIM_ADDED;
957
958 return (0);
959 }
960
961 static void
sdiobdiscover(void * context,int pending)962 sdiobdiscover(void *context, int pending)
963 {
964 struct cam_periph *periph;
965 struct sdiob_softc *sc;
966 int error;
967
968 KASSERT(context != NULL, ("%s: context is NULL\n", __func__));
969 periph = (struct cam_periph *)context;
970 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s\n", __func__));
971
972 /* Periph was held for us when this task was enqueued. */
973 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
974 cam_periph_release(periph);
975 return;
976 }
977
978 sc = periph->softc;
979 sc->sdio_state = SDIO_STATE_INITIALIZING;
980
981 if (sc->ccb == NULL)
982 sc->ccb = xpt_alloc_ccb();
983 else
984 memset(sc->ccb, 0, sizeof(*sc->ccb));
985 xpt_setup_ccb(&sc->ccb->ccb_h, periph->path, CAM_PRIORITY_NONE);
986
987 /*
988 * Read CCCR and FBR of each function, get manufacturer and device IDs,
989 * max block size, and whatever else we deem necessary.
990 */
991 cam_periph_lock(periph);
992 error = sdiob_get_card_info(sc);
993 if (error == 0)
994 sc->sdio_state = SDIO_STATE_READY;
995 else
996 sc->sdio_state = SDIO_STATE_DEAD;
997 cam_periph_unlock(periph);
998
999 if (error)
1000 return;
1001
1002 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: num_func %d\n",
1003 __func__, sc->cardinfo.num_funcs));
1004
1005 /*
1006 * Now CAM portion of the driver has been initialized and
1007 * we know VID/PID of all the functions on the card.
1008 * Time to hook into the newbus.
1009 */
1010 error = sdio_newbus_sim_add(sc);
1011 if (error != 0)
1012 sc->nb_state = NB_STATE_DEAD;
1013
1014 return;
1015 }
1016
1017 /* Called at the end of cam_periph_alloc() for us to finish allocation. */
1018 static cam_status
sdiobregister(struct cam_periph * periph,void * arg)1019 sdiobregister(struct cam_periph *periph, void *arg)
1020 {
1021 struct sdiob_softc *sc;
1022 int error;
1023
1024 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: arg %p\n", __func__, arg));
1025 if (arg == NULL) {
1026 printf("%s: no getdev CCB, can't register device pariph %p\n",
1027 __func__, periph);
1028 return(CAM_REQ_CMP_ERR);
1029 }
1030 if (xpt_path_sim_device(periph->path) == NULL) {
1031 printf("%s: no device_t for sim %p\n", __func__, periph->sim);
1032 return(CAM_REQ_CMP_ERR);
1033 }
1034
1035 sc = (struct sdiob_softc *) malloc(sizeof(*sc), M_DEVBUF,
1036 M_NOWAIT|M_ZERO);
1037 if (sc == NULL) {
1038 printf("%s: unable to allocate sc\n", __func__);
1039 return (CAM_REQ_CMP_ERR);
1040 }
1041 sc->sdio_state = SDIO_STATE_DEAD;
1042 sc->nb_state = NB_STATE_DEAD;
1043 TASK_INIT(&sc->discover_task, 0, sdiobdiscover, periph);
1044
1045 /* Refcount until we are setup. Can't block. */
1046 error = cam_periph_hold(periph, PRIBIO);
1047 if (error != 0) {
1048 printf("%s: lost periph during registration!\n", __func__);
1049 free(sc, M_DEVBUF);
1050 return(CAM_REQ_CMP_ERR);
1051 }
1052 periph->softc = sc;
1053 sc->periph = periph;
1054 cam_periph_unlock(periph);
1055
1056 error = taskqueue_enqueue(taskqueue_thread, &sc->discover_task);
1057
1058 cam_periph_lock(periph);
1059 /* We will continue to hold a refcount for discover_task. */
1060 /* cam_periph_unhold(periph); */
1061
1062 xpt_schedule(periph, CAM_PRIORITY_XPT);
1063
1064 return (CAM_REQ_CMP);
1065 }
1066
1067 static void
sdioboninvalidate(struct cam_periph * periph)1068 sdioboninvalidate(struct cam_periph *periph)
1069 {
1070
1071 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s:\n", __func__));
1072
1073 return;
1074 }
1075
1076 static void
sdiobcleanup(struct cam_periph * periph)1077 sdiobcleanup(struct cam_periph *periph)
1078 {
1079
1080 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s:\n", __func__));
1081
1082 return;
1083 }
1084
1085 static void
sdiobstart(struct cam_periph * periph,union ccb * ccb)1086 sdiobstart(struct cam_periph *periph, union ccb *ccb)
1087 {
1088
1089 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: ccb %p\n", __func__, ccb));
1090
1091 return;
1092 }
1093
1094 static void
sdiobasync(void * softc,uint32_t code,struct cam_path * path,void * arg)1095 sdiobasync(void *softc, uint32_t code, struct cam_path *path, void *arg)
1096 {
1097 struct cam_periph *periph;
1098 struct ccb_getdev *cgd;
1099 cam_status status;
1100
1101 periph = (struct cam_periph *)softc;
1102
1103 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("%s(code=%d)\n", __func__, code));
1104 switch (code) {
1105 case AC_FOUND_DEVICE:
1106 if (arg == NULL)
1107 break;
1108 cgd = (struct ccb_getdev *)arg;
1109 if (cgd->protocol != PROTO_MMCSD)
1110 break;
1111
1112 /* We do not support SD memory (Combo) Cards. */
1113 if ((path->device->mmc_ident_data.card_features &
1114 CARD_FEATURE_MEMORY)) {
1115 CAM_DEBUG(path, CAM_DEBUG_TRACE,
1116 ("Memory card, not interested\n"));
1117 break;
1118 }
1119
1120 /*
1121 * Allocate a peripheral instance for this device which starts
1122 * the probe process.
1123 */
1124 status = cam_periph_alloc(sdiobregister, sdioboninvalidate,
1125 sdiobcleanup, sdiobstart, SDIOB_NAME_S, CAM_PERIPH_BIO, path,
1126 sdiobasync, AC_FOUND_DEVICE, cgd);
1127 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG)
1128 CAM_DEBUG(path, CAM_DEBUG_PERIPH,
1129 ("%s: Unable to attach to new device due to "
1130 "status %#02x\n", __func__, status));
1131 break;
1132 default:
1133 CAM_DEBUG(path, CAM_DEBUG_PERIPH,
1134 ("%s: cannot handle async code %#02x\n", __func__, code));
1135 cam_periph_async(periph, code, path, arg);
1136 break;
1137 }
1138 }
1139
1140 static void
sdiobinit(void)1141 sdiobinit(void)
1142 {
1143 cam_status status;
1144
1145 /*
1146 * Register for new device notification. We will be notified for all
1147 * already existing ones.
1148 */
1149 status = xpt_register_async(AC_FOUND_DEVICE, sdiobasync, NULL, NULL);
1150 if (status != CAM_REQ_CMP)
1151 printf("%s: Failed to attach async callback, statux %#02x",
1152 __func__, status);
1153 }
1154
1155 /* This function will allow unloading the KLD. */
1156 static int
sdiobdeinit(void)1157 sdiobdeinit(void)
1158 {
1159
1160 return (EOPNOTSUPP);
1161 }
1162
1163 static struct periph_driver sdiobdriver =
1164 {
1165 .init = sdiobinit,
1166 .driver_name = SDIOB_NAME_S,
1167 .units = TAILQ_HEAD_INITIALIZER(sdiobdriver.units),
1168 .generation = 0,
1169 .flags = 0,
1170 .deinit = sdiobdeinit,
1171 };
1172
1173 PERIPHDRIVER_DECLARE(SDIOB_NAME, sdiobdriver);
1174 MODULE_VERSION(SDIOB_NAME, 1);
1175