2008-02-16 22:13:11 +00:00
|
|
|
/*-
|
2008-11-19 17:34:28 +00:00
|
|
|
* Copyright (c) 2008 Semihalf, Rafal Jaworowski
|
2009-05-05 16:29:08 +00:00
|
|
|
* Copyright (c) 2009 Semihalf, Piotr Ziecik
|
2012-09-09 11:33:06 +00:00
|
|
|
* Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
|
2008-02-16 22:13:11 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2008-11-19 17:34:28 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2008-02-16 22:13:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2008-11-19 17:34:28 +00:00
|
|
|
* Block storage I/O routines for U-Boot
|
2008-02-16 22:13:11 +00:00
|
|
|
*/
|
|
|
|
|
2008-11-19 17:34:28 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2008-02-16 22:13:11 +00:00
|
|
|
#include <sys/param.h>
|
2012-09-09 11:33:06 +00:00
|
|
|
#include <sys/disk.h>
|
2008-02-16 22:13:11 +00:00
|
|
|
#include <machine/stdarg.h>
|
|
|
|
#include <stand.h>
|
|
|
|
|
2008-11-19 17:34:28 +00:00
|
|
|
#include "api_public.h"
|
2008-02-16 22:13:11 +00:00
|
|
|
#include "bootstrap.h"
|
2012-09-09 11:33:06 +00:00
|
|
|
#include "disk.h"
|
2008-11-19 17:34:28 +00:00
|
|
|
#include "glue.h"
|
|
|
|
#include "libuboot.h"
|
2008-02-16 22:13:11 +00:00
|
|
|
|
2008-11-19 17:34:28 +00:00
|
|
|
#define stor_printf(fmt, args...) do { \
|
2018-03-12 21:39:49 +00:00
|
|
|
printf("%s%d: ", dev->dd.d_dev->dv_name, dev->dd.d_unit); \
|
2008-11-19 17:34:28 +00:00
|
|
|
printf(fmt, ##args); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define debugf(fmt, args...) do { printf("%s(): ", __func__); \
|
|
|
|
printf(fmt,##args); } while (0)
|
|
|
|
#else
|
|
|
|
#define debugf(fmt, args...)
|
|
|
|
#endif
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
static struct {
|
|
|
|
int opened; /* device is opened */
|
|
|
|
int handle; /* storage device handle */
|
|
|
|
int type; /* storage type */
|
|
|
|
off_t blocks; /* block count */
|
|
|
|
u_int bsize; /* block size */
|
|
|
|
} stor_info[UB_MAX_DEV];
|
2008-11-19 17:34:28 +00:00
|
|
|
|
2018-03-12 21:39:49 +00:00
|
|
|
#define SI(dev) (stor_info[(dev)->dd.d_unit])
|
2009-05-05 16:29:08 +00:00
|
|
|
|
2008-11-19 17:34:28 +00:00
|
|
|
static int stor_info_no = 0;
|
2012-09-09 11:33:06 +00:00
|
|
|
static int stor_opendev(struct disk_devdesc *);
|
|
|
|
static int stor_readdev(struct disk_devdesc *, daddr_t, size_t, char *);
|
2008-11-19 17:34:28 +00:00
|
|
|
|
|
|
|
/* devsw I/F */
|
|
|
|
static int stor_init(void);
|
2016-12-30 19:06:29 +00:00
|
|
|
static int stor_strategy(void *, int, daddr_t, size_t, char *, size_t *);
|
2008-11-19 17:34:28 +00:00
|
|
|
static int stor_open(struct open_file *, ...);
|
|
|
|
static int stor_close(struct open_file *);
|
2012-09-09 11:33:06 +00:00
|
|
|
static int stor_ioctl(struct open_file *f, u_long cmd, void *data);
|
2016-11-08 06:50:18 +00:00
|
|
|
static int stor_print(int);
|
2012-09-09 11:33:06 +00:00
|
|
|
static void stor_cleanup(void);
|
2008-11-19 17:34:28 +00:00
|
|
|
|
|
|
|
struct devsw uboot_storage = {
|
|
|
|
"disk",
|
|
|
|
DEVT_DISK,
|
|
|
|
stor_init,
|
|
|
|
stor_strategy,
|
|
|
|
stor_open,
|
|
|
|
stor_close,
|
2012-09-09 11:33:06 +00:00
|
|
|
stor_ioctl,
|
|
|
|
stor_print,
|
|
|
|
stor_cleanup
|
2008-11-19 17:34:28 +00:00
|
|
|
};
|
2008-02-16 22:13:11 +00:00
|
|
|
|
|
|
|
static int
|
2008-11-19 17:34:28 +00:00
|
|
|
stor_init(void)
|
2008-02-16 22:13:11 +00:00
|
|
|
{
|
2008-11-19 17:34:28 +00:00
|
|
|
struct device_info *di;
|
2012-09-09 11:33:06 +00:00
|
|
|
int i;
|
2008-11-19 17:34:28 +00:00
|
|
|
|
|
|
|
if (devs_no == 0) {
|
|
|
|
printf("No U-Boot devices! Really enumerated?\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
2008-03-13 17:54:21 +00:00
|
|
|
|
2008-11-19 17:34:28 +00:00
|
|
|
for (i = 0; i < devs_no; i++) {
|
|
|
|
di = ub_dev_get(i);
|
|
|
|
if ((di != NULL) && (di->type & DEV_TYP_STOR)) {
|
|
|
|
if (stor_info_no >= UB_MAX_DEV) {
|
|
|
|
printf("Too many storage devices: %d\n",
|
|
|
|
stor_info_no);
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-09-09 11:33:06 +00:00
|
|
|
stor_info[stor_info_no].handle = i;
|
|
|
|
stor_info[stor_info_no].opened = 0;
|
|
|
|
stor_info[stor_info_no].type = di->type;
|
|
|
|
stor_info[stor_info_no].blocks =
|
|
|
|
di->di_stor.block_count;
|
|
|
|
stor_info[stor_info_no].bsize =
|
|
|
|
di->di_stor.block_size;
|
|
|
|
stor_info_no++;
|
2008-11-19 17:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
if (!stor_info_no) {
|
2010-05-25 09:59:53 +00:00
|
|
|
debugf("No storage devices\n");
|
2008-11-19 17:34:28 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
debugf("storage devices found: %d\n", stor_info_no);
|
|
|
|
return (0);
|
2008-02-16 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
static void
|
|
|
|
stor_cleanup(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < stor_info_no; i++)
|
|
|
|
if (stor_info[i].opened > 0)
|
|
|
|
ub_dev_close(stor_info[i].handle);
|
|
|
|
}
|
|
|
|
|
2008-02-16 22:13:11 +00:00
|
|
|
static int
|
2016-12-30 19:06:29 +00:00
|
|
|
stor_strategy(void *devdata, int rw, daddr_t blk, size_t size,
|
A new implementation of the loader block cache
The block cache implementation in loader has proven to be almost useless, and in worst case even slowing down the disk reads due to insufficient cache size and extra memory copy.
Also the current cache implementation does not cache reads from CDs, or work with zfs built on top of multiple disks.
Instead of an LRU, this code uses a simple hash (O(1) read from cache), and instead of a single global cache, a separate cache per block device.
The cache also implements limited read-ahead to increase performance.
To simplify read ahead management, the read ahead will not wrap over bcache end, so in worst case, single block physical read will be performed to fill the last block in bcache.
Booting from a virtual CD over IPMI:
0ms latency, before: 27 second, after: 7 seconds
60ms latency, before: over 12 minutes, after: under 5 minutes.
Submitted by: Toomas Soome <tsoome@me.com>
Reviewed by: delphij (previous version), emaste (previous version)
Relnotes: yes
Differential Revision: https://reviews.freebsd.org/D4713
2016-04-18 23:09:22 +00:00
|
|
|
char *buf, size_t *rsize)
|
2008-02-16 22:13:11 +00:00
|
|
|
{
|
2012-09-09 11:33:06 +00:00
|
|
|
struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
|
|
|
|
daddr_t bcount;
|
|
|
|
int err;
|
2008-11-19 17:34:28 +00:00
|
|
|
|
2017-04-18 18:07:54 +00:00
|
|
|
rw &= F_MASK;
|
2008-11-19 17:34:28 +00:00
|
|
|
if (rw != F_READ) {
|
|
|
|
stor_printf("write attempt, operation not supported!\n");
|
|
|
|
return (EROFS);
|
|
|
|
}
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
if (size % SI(dev).bsize) {
|
2016-02-29 07:27:49 +00:00
|
|
|
stor_printf("size=%zu not multiple of device "
|
|
|
|
"block size=%d\n",
|
2012-09-09 11:33:06 +00:00
|
|
|
size, SI(dev).bsize);
|
2008-11-19 17:34:28 +00:00
|
|
|
return (EIO);
|
|
|
|
}
|
2012-09-09 11:33:06 +00:00
|
|
|
bcount = size / SI(dev).bsize;
|
2008-11-19 17:34:28 +00:00
|
|
|
if (rsize)
|
|
|
|
*rsize = 0;
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
err = stor_readdev(dev, blk + dev->d_offset, bcount, buf);
|
2008-11-19 17:34:28 +00:00
|
|
|
if (!err && rsize)
|
|
|
|
*rsize = size;
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
stor_open(struct open_file *f, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2012-09-09 11:33:06 +00:00
|
|
|
struct disk_devdesc *dev;
|
2008-11-19 17:34:28 +00:00
|
|
|
|
|
|
|
va_start(ap, f);
|
2012-09-09 11:33:06 +00:00
|
|
|
dev = va_arg(ap, struct disk_devdesc *);
|
2008-11-19 17:34:28 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
return (stor_opendev(dev));
|
2008-02-16 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-09-09 11:33:06 +00:00
|
|
|
stor_opendev(struct disk_devdesc *dev)
|
2008-02-16 22:13:11 +00:00
|
|
|
{
|
2012-09-09 11:33:06 +00:00
|
|
|
int err;
|
2012-05-01 05:04:49 +00:00
|
|
|
|
2018-03-12 21:39:49 +00:00
|
|
|
if (dev->dd.d_unit < 0 || dev->dd.d_unit >= stor_info_no)
|
2012-09-09 11:33:06 +00:00
|
|
|
return (EIO);
|
2012-05-01 05:04:49 +00:00
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
if (SI(dev).opened == 0) {
|
|
|
|
err = ub_dev_open(SI(dev).handle);
|
|
|
|
if (err != 0) {
|
|
|
|
stor_printf("device open failed with error=%d, "
|
|
|
|
"handle=%d\n", err, SI(dev).handle);
|
|
|
|
return (ENXIO);
|
2012-05-01 05:04:49 +00:00
|
|
|
}
|
2012-09-09 11:33:06 +00:00
|
|
|
SI(dev).opened++;
|
2012-05-01 05:04:49 +00:00
|
|
|
}
|
2012-09-09 11:33:06 +00:00
|
|
|
return (disk_open(dev, SI(dev).blocks * SI(dev).bsize,
|
2017-03-16 12:04:43 +00:00
|
|
|
SI(dev).bsize));
|
2012-05-01 05:04:49 +00:00
|
|
|
}
|
|
|
|
|
2008-02-16 22:13:11 +00:00
|
|
|
static int
|
2012-09-09 11:33:06 +00:00
|
|
|
stor_close(struct open_file *f)
|
2008-02-16 22:13:11 +00:00
|
|
|
{
|
2012-09-09 11:33:06 +00:00
|
|
|
struct disk_devdesc *dev;
|
2008-11-19 17:34:28 +00:00
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
dev = (struct disk_devdesc *)(f->f_devdata);
|
|
|
|
return (disk_close(dev));
|
2008-11-19 17:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-09-09 11:33:06 +00:00
|
|
|
stor_readdev(struct disk_devdesc *dev, daddr_t blk, size_t size, char *buf)
|
2008-11-19 17:34:28 +00:00
|
|
|
{
|
|
|
|
lbasize_t real_size;
|
2012-09-09 11:33:06 +00:00
|
|
|
int err;
|
2008-11-19 17:34:28 +00:00
|
|
|
|
2012-05-01 05:04:49 +00:00
|
|
|
debugf("reading blk=%d size=%d @ 0x%08x\n", (int)blk, size, (uint32_t)buf);
|
2008-11-19 17:34:28 +00:00
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
err = ub_dev_read(SI(dev).handle, buf, size, blk, &real_size);
|
2008-11-19 17:34:28 +00:00
|
|
|
if (err != 0) {
|
|
|
|
stor_printf("read failed, error=%d\n", err);
|
|
|
|
return (EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (real_size != size) {
|
|
|
|
stor_printf("real size != size\n");
|
|
|
|
err = EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2016-11-08 06:50:18 +00:00
|
|
|
static int
|
2012-09-09 11:33:06 +00:00
|
|
|
stor_print(int verbose)
|
2009-05-05 16:29:08 +00:00
|
|
|
{
|
2012-09-09 11:33:06 +00:00
|
|
|
struct disk_devdesc dev;
|
|
|
|
static char line[80];
|
2016-11-08 06:50:18 +00:00
|
|
|
int i, ret = 0;
|
2009-05-05 16:29:08 +00:00
|
|
|
|
2016-11-19 08:54:21 +00:00
|
|
|
if (stor_info_no == 0)
|
|
|
|
return (ret);
|
|
|
|
|
|
|
|
printf("%s devices:", uboot_storage.dv_name);
|
|
|
|
if ((ret = pager_output("\n")) != 0)
|
|
|
|
return (ret);
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
for (i = 0; i < stor_info_no; i++) {
|
2018-03-12 21:39:49 +00:00
|
|
|
dev.dd.d_dev = &uboot_storage;
|
|
|
|
dev.dd.d_unit = i;
|
2012-09-09 11:33:06 +00:00
|
|
|
dev.d_slice = -1;
|
|
|
|
dev.d_partition = -1;
|
2016-11-08 06:50:18 +00:00
|
|
|
snprintf(line, sizeof(line), "\tdisk%d (%s)\n", i,
|
2012-09-09 11:33:06 +00:00
|
|
|
ub_stor_type(SI(&dev).type));
|
2016-11-08 06:50:18 +00:00
|
|
|
if ((ret = pager_output(line)) != 0)
|
2016-05-18 05:59:05 +00:00
|
|
|
break;
|
2012-09-09 11:33:06 +00:00
|
|
|
if (stor_opendev(&dev) == 0) {
|
|
|
|
sprintf(line, "\tdisk%d", i);
|
2016-11-08 06:50:18 +00:00
|
|
|
ret = disk_print(&dev, line, verbose);
|
2012-09-09 11:33:06 +00:00
|
|
|
disk_close(&dev);
|
2016-11-08 06:50:18 +00:00
|
|
|
if (ret != 0)
|
|
|
|
break;
|
2008-11-19 17:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-08 06:50:18 +00:00
|
|
|
return (ret);
|
2008-02-16 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
static int
|
|
|
|
stor_ioctl(struct open_file *f, u_long cmd, void *data)
|
2008-02-16 22:13:11 +00:00
|
|
|
{
|
2012-09-09 11:33:06 +00:00
|
|
|
struct disk_devdesc *dev;
|
2017-04-18 19:36:58 +00:00
|
|
|
int rc;
|
2012-09-09 11:33:06 +00:00
|
|
|
|
|
|
|
dev = (struct disk_devdesc *)f->f_devdata;
|
2017-04-18 19:36:58 +00:00
|
|
|
rc = disk_ioctl(dev, cmd, data);
|
|
|
|
if (rc != ENOTTY)
|
|
|
|
return (rc);
|
|
|
|
|
2012-09-09 11:33:06 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case DIOCGSECTORSIZE:
|
|
|
|
*(u_int *)data = SI(dev).bsize;
|
|
|
|
break;
|
|
|
|
case DIOCGMEDIASIZE:
|
2017-02-01 20:10:56 +00:00
|
|
|
*(uint64_t *)data = SI(dev).bsize * SI(dev).blocks;
|
2012-09-09 11:33:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ENOTTY);
|
2008-11-19 17:34:28 +00:00
|
|
|
}
|
2012-09-09 11:33:06 +00:00
|
|
|
return (0);
|
2008-02-16 22:13:11 +00:00
|
|
|
}
|
2012-09-09 11:33:06 +00:00
|
|
|
|
2014-03-11 22:02:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the device unit number for the given type and type-relative unit
|
|
|
|
* number.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
uboot_diskgetunit(int type, int type_unit)
|
|
|
|
{
|
|
|
|
int local_type_unit;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
local_type_unit = 0;
|
|
|
|
for (i = 0; i < stor_info_no; i++) {
|
|
|
|
if ((stor_info[i].type & type) == type) {
|
|
|
|
if (local_type_unit == type_unit) {
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
local_type_unit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|