Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*	$NetBSD: ld_iop.c,v 1.39 2017/08/09 16:44:39 mlelstv Exp $	*/

/*-
 * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * I2O front-end for ld(4) driver, supporting random block storage class
 * devices.  Currently, this doesn't handle anything more complex than
 * fixed direct-access devices.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ld_iop.c,v 1.39 2017/08/09 16:44:39 mlelstv Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/endian.h>
#include <sys/dkio.h>
#include <sys/disk.h>
#include <sys/proc.h>

#include <sys/bus.h>

#include <dev/ldvar.h>

#include <dev/i2o/i2o.h>
#include <dev/i2o/iopio.h>
#include <dev/i2o/iopvar.h>

#define	LD_IOP_TIMEOUT		30*1000

#define	LD_IOP_CLAIMED		0x01
#define	LD_IOP_NEW_EVTMASK	0x02

struct ld_iop_softc {
	struct	ld_softc sc_ld;
	struct	iop_initiator sc_ii;
	struct	iop_initiator sc_eventii;
	int	sc_flags;
};

static void	ld_iop_adjqparam(device_t, int);
static void	ld_iop_attach(device_t, device_t, void *);
static int	ld_iop_detach(device_t, int);
static int	ld_iop_dump(struct ld_softc *, void *, int, int);
static int	ld_iop_flush(struct ld_softc *, bool);
static int	ld_iop_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
static void	ld_iop_intr(device_t, struct iop_msg *, void *);
static void	ld_iop_intr_event(device_t, struct iop_msg *, void *);
static int	ld_iop_match(device_t, cfdata_t, void *);
static int	ld_iop_start(struct ld_softc *, struct buf *);
static void	ld_iop_unconfig(struct ld_iop_softc *, int);

CFATTACH_DECL_NEW(ld_iop, sizeof(struct ld_iop_softc),
    ld_iop_match, ld_iop_attach, ld_iop_detach, NULL);

static const char * const ld_iop_errors[] = {
	"success",
	"media error",
	"access error",
	"device failure",
	"device not ready",
	"media not present",
	"media locked",
	"media failure",
	"protocol failure",
	"bus failure",
	"access violation",
	"media write protected",
	"device reset",
	"volume changed, waiting for acknowledgement",
	"timeout",
};

static int
ld_iop_match(device_t parent, cfdata_t match, void *aux)
{
	struct iop_attach_args *ia;

	ia = aux;

	return (ia->ia_class == I2O_CLASS_RANDOM_BLOCK_STORAGE);
}

static void
ld_iop_attach(device_t parent, device_t self, void *aux)
{
	struct iop_attach_args *ia = aux;
	struct ld_iop_softc *sc = device_private(self);
	struct iop_softc *iop = device_private(parent);
	struct ld_softc *ld = &sc->sc_ld;
	int rv, evreg, enable;
	const char *typestr, *fixedstr;
	u_int cachesz;
	u_int32_t timeoutbase, rwvtimeoutbase, rwvtimeout;
	struct {
		struct	i2o_param_op_results pr;
		struct	i2o_param_read_results prr;
		union {
			struct	i2o_param_rbs_cache_control cc;
			struct	i2o_param_rbs_device_info bdi;
		} p;
	} __packed param;

	ld->sc_dv = self;
	evreg = 0;

	/* Register us as an initiator. */
	sc->sc_ii.ii_dv = self;
	sc->sc_ii.ii_intr = ld_iop_intr;
	sc->sc_ii.ii_adjqparam = ld_iop_adjqparam;
	sc->sc_ii.ii_flags = 0;
	sc->sc_ii.ii_tid = ia->ia_tid;
	iop_initiator_register(iop, &sc->sc_ii);

	/* Register another initiator to handle events from the device. */
	sc->sc_eventii.ii_dv = self;
	sc->sc_eventii.ii_intr = ld_iop_intr_event;
	sc->sc_eventii.ii_flags = II_NOTCTX | II_UTILITY;
	sc->sc_eventii.ii_tid = ia->ia_tid;
	iop_initiator_register(iop, &sc->sc_eventii);

	rv = iop_util_eventreg(iop, &sc->sc_eventii,
	    I2O_EVENT_GEN_EVENT_MASK_MODIFIED |
	    I2O_EVENT_GEN_DEVICE_RESET |
	    I2O_EVENT_GEN_STATE_CHANGE |
	    I2O_EVENT_GEN_GENERAL_WARNING);
	if (rv != 0) {
		aprint_error_dev(self, "unable to register for events");
		goto bad;
	}
	evreg = 1;

	/*
	 * Start out with one queued command.  The `iop' driver will adjust
	 * the queue parameters once we're up and running.
	 */
	ld->sc_maxqueuecnt = 1;

	ld->sc_maxxfer = IOP_MAX_XFER;
	ld->sc_dump = ld_iop_dump;
	ld->sc_ioctl = ld_iop_ioctl;
	ld->sc_start = ld_iop_start;
	ld->sc_flags = LDF_MPSAFE;

	/* Say what the device is. */
	printf(":");
	iop_print_ident(iop, ia->ia_tid);

	/*
	 * Claim the device so that we don't get any nasty surprises.  Allow
	 * failure.
	 */
	rv = iop_util_claim(iop, &sc->sc_ii, 0,
	    I2O_UTIL_CLAIM_CAPACITY_SENSITIVE |
	    I2O_UTIL_CLAIM_NO_PEER_SERVICE |
	    I2O_UTIL_CLAIM_NO_MANAGEMENT_SERVICE |
	    I2O_UTIL_CLAIM_PRIMARY_USER);
	sc->sc_flags = rv ? 0 : LD_IOP_CLAIMED;

	rv = iop_field_get_all(iop, ia->ia_tid, I2O_PARAM_RBS_DEVICE_INFO,
	    &param, sizeof(param), NULL);
	if (rv != 0)
		goto bad;

	ld->sc_secsize = le32toh(param.p.bdi.blocksize);
	ld->sc_secperunit = (int)
	    (le64toh(param.p.bdi.capacity) / ld->sc_secsize);

	switch (param.p.bdi.type) {
	case I2O_RBS_TYPE_DIRECT:
		typestr = "direct access";
		enable = 1;
		break;
	case I2O_RBS_TYPE_WORM:
		typestr = "WORM";
		enable = 0;
		break;
	case I2O_RBS_TYPE_CDROM:
		typestr = "CD-ROM";
		enable = 0;
		break;
	case I2O_RBS_TYPE_OPTICAL:
		typestr = "optical";
		enable = 0;
		break;
	default:
		typestr = "unknown";
		enable = 0;
		break;
	}

	if ((le32toh(param.p.bdi.capabilities) & I2O_RBS_CAP_REMOVABLE_MEDIA)
	    != 0) {
		/* ld->sc_flags |= LDF_REMOVABLE; */
		fixedstr = "removable";
		enable = 0;
	} else
		fixedstr = "fixed";

	printf(" %s, %s", typestr, fixedstr);

	/*
	 * Determine if the device has an private cache.  If so, print the
	 * cache size.  Even if the device doesn't appear to have a cache,
	 * we perform a flush at shutdown.
	 */
	rv = iop_field_get_all(iop, ia->ia_tid, I2O_PARAM_RBS_CACHE_CONTROL,
	    &param, sizeof(param), NULL);
	if (rv != 0)
		goto bad;

	if ((cachesz = le32toh(param.p.cc.totalcachesize)) != 0)
		printf(", %dkB cache", cachesz >> 10);

	printf("\n");

	/*
	 * Configure the DDM's timeout functions to time out all commands
	 * after 30 seconds.
	 */
	timeoutbase = htole32(LD_IOP_TIMEOUT * 1000);
	rwvtimeoutbase = htole32(LD_IOP_TIMEOUT * 1000);
	rwvtimeout = 0;

	iop_field_set(iop, ia->ia_tid, I2O_PARAM_RBS_OPERATION,
	    &timeoutbase, sizeof(timeoutbase),
	    I2O_PARAM_RBS_OPERATION_timeoutbase);
	iop_field_set(iop, ia->ia_tid, I2O_PARAM_RBS_OPERATION,
	    &rwvtimeoutbase, sizeof(rwvtimeoutbase),
	    I2O_PARAM_RBS_OPERATION_rwvtimeoutbase);
	iop_field_set(iop, ia->ia_tid, I2O_PARAM_RBS_OPERATION,
	    &rwvtimeout, sizeof(rwvtimeout),
	    I2O_PARAM_RBS_OPERATION_rwvtimeoutbase);

	if (enable)
		ld->sc_flags |= LDF_ENABLED;
	else
		aprint_error_dev(self, "device not yet supported\n");

	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
	return;

 bad:
	ld_iop_unconfig(sc, evreg);
}

static void
ld_iop_unconfig(struct ld_iop_softc *sc, int evreg)
{
	struct iop_softc *iop;

	iop = device_private(device_parent(sc->sc_ld.sc_dv));

	if ((sc->sc_flags & LD_IOP_CLAIMED) != 0)
		iop_util_claim(iop, &sc->sc_ii, 1,
		    I2O_UTIL_CLAIM_PRIMARY_USER);

	if (evreg) {
		/*
		 * Mask off events, and wait up to 5 seconds for a reply.
		 * Note that some adapters won't reply to this (XXX We
		 * should check the event capabilities).
		 */
		mutex_spin_enter(&iop->sc_intrlock);
		sc->sc_flags &= ~LD_IOP_NEW_EVTMASK;
		mutex_spin_exit(&iop->sc_intrlock);

		iop_util_eventreg(iop, &sc->sc_eventii,
		    I2O_EVENT_GEN_EVENT_MASK_MODIFIED);

		mutex_spin_enter(&iop->sc_intrlock);
		if ((sc->sc_flags & LD_IOP_NEW_EVTMASK) == 0)
			cv_timedwait(&sc->sc_eventii.ii_cv,
			    &iop->sc_intrlock, hz * 5);
		mutex_spin_exit(&iop->sc_intrlock);
	}

	iop_initiator_unregister(iop, &sc->sc_eventii);
	iop_initiator_unregister(iop, &sc->sc_ii);
}

static int
ld_iop_detach(device_t self, int flags)
{
	struct ld_iop_softc *sc;
	struct iop_softc *iop;
	int rv;

	sc = device_private(self);
	iop = device_private(device_parent(self));

	if ((rv = ldbegindetach(&sc->sc_ld, flags)) != 0)
		return (rv);

	/*
	 * Abort any requests queued with the IOP, but allow requests that
	 * are already in progress to complete.
	 */
	if ((sc->sc_ld.sc_flags & LDF_ENABLED) != 0)
		iop_util_abort(iop, &sc->sc_ii, 0, 0,
		    I2O_UTIL_ABORT_WILD | I2O_UTIL_ABORT_CLEAN);

	ldenddetach(&sc->sc_ld);

	/* Un-claim the target, and un-register our initiators. */
	if ((sc->sc_ld.sc_flags & LDF_ENABLED) != 0)
		ld_iop_unconfig(sc, 1);

	return (0);
}

static int
ld_iop_start(struct ld_softc *ld, struct buf *bp)
{
	struct iop_msg *im;
	struct iop_softc *iop;
	struct ld_iop_softc *sc;
	struct i2o_rbs_block_read *mf;
	u_int rv, flags, write;
	u_int64_t ba;
	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];

	sc = device_private(ld->sc_dv);
	iop = device_private(device_parent(ld->sc_dv));

	im = iop_msg_alloc(iop, 0);
	im->im_dvcontext = bp;

	write = ((bp->b_flags & B_READ) == 0);
	ba = (u_int64_t)bp->b_rawblkno * ld->sc_secsize;

	/*
	 * Write through the cache when performing synchronous writes.  When
	 * performing a read, we don't request that the DDM cache the data,
	 * as there's little advantage to it.
	 */
	if (write) {
		if ((bp->b_flags & B_ASYNC) == 0)
			flags = I2O_RBS_BLOCK_WRITE_CACHE_WT;
		else
			flags = I2O_RBS_BLOCK_WRITE_CACHE_WB;
	} else
		flags = 0;

	/*
	 * Fill the message frame.  We can use the block_read structure for
	 * both reads and writes, as it's almost identical to the
	 * block_write structure.
	 */
	mf = (struct i2o_rbs_block_read *)mb;
	mf->msgflags = I2O_MSGFLAGS(i2o_rbs_block_read);
	mf->msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid,
	    write ? I2O_RBS_BLOCK_WRITE : I2O_RBS_BLOCK_READ);
	mf->msgictx = sc->sc_ii.ii_ictx;
	mf->msgtctx = im->im_tctx;
	mf->flags = flags | (1 << 16);		/* flags & time multiplier */
	mf->datasize = bp->b_bcount;
	mf->lowoffset = (u_int32_t)ba;
	mf->highoffset = (u_int32_t)(ba >> 32);

	/* Map the data transfer and enqueue the command. */
	rv = iop_msg_map_bio(iop, im, mb, bp->b_data, bp->b_bcount, write);
	if (rv == 0) {
		if ((rv = iop_post(iop, mb)) != 0) {
			iop_msg_unmap(iop, im);
			iop_msg_free(iop, im);
		}
	}
	return (rv);
}

static int
ld_iop_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
{
	struct iop_msg *im;
	struct iop_softc *iop;
	struct ld_iop_softc *sc;
	struct i2o_rbs_block_write *mf;
	int rv, bcount;
	u_int64_t ba;
	u_int32_t mb[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];

	sc = device_private(ld->sc_dv);
	iop = device_private(device_parent(ld->sc_dv));
	bcount = blkcnt * ld->sc_secsize;
	ba = (u_int64_t)blkno * ld->sc_secsize;
	im = iop_msg_alloc(iop, IM_POLL);

	mf = (struct i2o_rbs_block_write *)mb;
	mf->msgflags = I2O_MSGFLAGS(i2o_rbs_block_write);
	mf->msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, I2O_RBS_BLOCK_WRITE);
	mf->msgictx = sc->sc_ii.ii_ictx;
	mf->msgtctx = im->im_tctx;
	mf->flags = I2O_RBS_BLOCK_WRITE_CACHE_WT | (1 << 16);
	mf->datasize = bcount;
	mf->lowoffset = (u_int32_t)ba;
	mf->highoffset = (u_int32_t)(ba >> 32);

	if ((rv = iop_msg_map(iop, im, mb, data, bcount, 1, NULL)) != 0) {
		iop_msg_free(iop, im);
		return (rv);
	}

	rv = iop_msg_post(iop, im, mb, LD_IOP_TIMEOUT * 2);
	iop_msg_unmap(iop, im);
	iop_msg_free(iop, im);
 	return (rv);
}

static int
ld_iop_flush(struct ld_softc *ld, bool poll)
{
	struct iop_msg *im;
	struct iop_softc *iop;
	struct ld_iop_softc *sc;
	struct i2o_rbs_cache_flush mf;
	int rv;

	sc = device_private(ld->sc_dv);
	iop = device_private(device_parent(ld->sc_dv));
	im = iop_msg_alloc(iop, poll ? IM_POLL : IM_WAIT);

	mf.msgflags = I2O_MSGFLAGS(i2o_rbs_cache_flush);
	mf.msgfunc = I2O_MSGFUNC(sc->sc_ii.ii_tid, I2O_RBS_CACHE_FLUSH);
	mf.msgictx = sc->sc_ii.ii_ictx;
	mf.msgtctx = im->im_tctx;
	mf.flags = 1 << 16;			/* time multiplier */

	/* Ancient disks will return an error here. */
	rv = iop_msg_post(iop, im, &mf, LD_IOP_TIMEOUT * 2);
	iop_msg_free(iop, im);
	return (rv);
}

static int
ld_iop_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
{
	int error;

	switch (cmd) {
        case DIOCCACHESYNC:
		error = ld_iop_flush(ld, poll);
		break;

	default:
		error = EPASSTHROUGH;
		break;
	}

        return error;
}

static void
ld_iop_intr(device_t dv, struct iop_msg *im, void *reply)
{
	struct i2o_rbs_reply *rb;
	struct buf *bp;
	struct ld_iop_softc *sc;
	struct iop_softc *iop;
	int err, detail;
	const char *errstr;

	rb = reply;
	bp = im->im_dvcontext;
	sc = device_private(dv);
	iop = device_private(device_parent(dv));

	err = ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0);

	if (!err && rb->reqstatus != I2O_STATUS_SUCCESS) {
		detail = le16toh(rb->detail);
		if (detail >= __arraycount(ld_iop_errors))
			errstr = "<unknown>";
		else
			errstr = ld_iop_errors[detail];
		aprint_error_dev(dv, "error 0x%04x: %s\n", detail, errstr);
		err = 1;
	}

	if (err) {
		bp->b_error = EIO;
		bp->b_resid = bp->b_bcount;
	} else
		bp->b_resid = bp->b_bcount - le32toh(rb->transfercount);

	iop_msg_unmap(iop, im);
	iop_msg_free(iop, im);
	lddone(&sc->sc_ld, bp);
}

static void
ld_iop_intr_event(device_t dv, struct iop_msg *im, void *reply)
{
	struct i2o_util_event_register_reply *rb;
	struct ld_iop_softc *sc;
	struct iop_softc *iop;
	u_int event;

	rb = reply;

	if ((rb->msgflags & I2O_MSGFLAGS_FAIL) != 0)
		return;

	event = le32toh(rb->event);
	sc = device_private(dv);

	if (event == I2O_EVENT_GEN_EVENT_MASK_MODIFIED) {
		iop = device_private(device_parent(dv));
		mutex_spin_enter(&iop->sc_intrlock);
		sc->sc_flags |= LD_IOP_NEW_EVTMASK;
		cv_broadcast(&sc->sc_eventii.ii_cv);
		mutex_spin_exit(&iop->sc_intrlock);
		return;
	}

	printf("%s: event 0x%08x received\n", device_xname(dv), event);
}

static void
ld_iop_adjqparam(device_t dv, int mpi)
{
	struct ld_iop_softc *sc = device_private(dv);
	struct iop_softc *iop = device_private(device_parent(dv));
	struct ld_softc *ld = &sc->sc_ld;

	/*
	 * AMI controllers seem to loose the plot if you hand off lots of
	 * queued commands.
	 */
	if (le16toh(I2O_ORG_AMI) == iop->sc_status.orgid && mpi > 64)
		mpi = 64;

	ldadjqparam(ld, mpi);
}