public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Pavel Herrmann <morpheus.ibis@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 05/11] DM: add ata and partition blockdev drivers
Date: Thu, 20 Sep 2012 21:37:41 +0200	[thread overview]
Message-ID: <1348169867-2917-6-git-send-email-morpheus.ibis@gmail.com> (raw)
In-Reply-To: <1348169867-2917-1-git-send-email-morpheus.ibis@gmail.com>

ata blockdev is an universal child of a blockctrl device, be it (P/S)ATA or SCSI
partition blockdev is a child of other blockdev drivers

Signed-off-by: Pavel Herrmann <morpheus.ibis@gmail.com>
---
 drivers/blockdev/Makefile    |   2 +-
 drivers/blockdev/ata.c       | 234 +++++++++++++++++++++++++++++++++++++++++++
 drivers/blockdev/ata.h       |  37 +++++++
 drivers/blockdev/partition.c | 179 +++++++++++++++++++++++++++++++++
 4 files changed, 451 insertions(+), 1 deletion(-)
 create mode 100644 drivers/blockdev/ata.c
 create mode 100644 drivers/blockdev/ata.h
 create mode 100644 drivers/blockdev/partition.c

diff --git a/drivers/blockdev/Makefile b/drivers/blockdev/Makefile
index a988924..668ffbd 100644
--- a/drivers/blockdev/Makefile
+++ b/drivers/blockdev/Makefile
@@ -21,7 +21,7 @@ include $(TOPDIR)/config.mk
 
 LIB	:= $(obj)libblockdev.o
 
-COBJS-${CONFIG_DM_BLOCK} := core.o
+COBJS-${CONFIG_DM_BLOCK} := core.o ata.o partition.o
 COBJS-${CONFIG_DOS_PARTITION} += part_types/part_dos.o
 
 COBJS	:= $(COBJS-y)
diff --git a/drivers/blockdev/ata.c b/drivers/blockdev/ata.c
new file mode 100644
index 0000000..698dc18
--- /dev/null
+++ b/drivers/blockdev/ata.c
@@ -0,0 +1,234 @@
+/*
+ * (C) Copyright 2012
+ * Pavel Herrmann <morpheus.ibis@gmail.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include "ata.h"
+#include <dm/blockctrl.h>
+#include <dm/manager.h>
+#include <common.h>
+#include <malloc.h>
+
+static lbaint_t read(struct instance *dev, lbaint_t start, lbaint_t blkcnt,
+		void *buffer)
+{
+	if (!buffer)
+		return 0;
+
+	struct blockdev_ata_platform_data *platform = dev->info->platform_data;
+	struct blockdev_ata_private_data *priv = dev->private_data;
+	/* check for access beyond disk boundary */
+	if ((start + blkcnt) > priv->block_count)
+		blkcnt = (priv->block_count - start);
+
+	return blockctrl_read(dev->bus, platform->port_number,
+		start, blkcnt, buffer);
+}
+
+static lbaint_t write(struct instance *dev, lbaint_t start, lbaint_t blkcnt,
+		void *buffer)
+{
+	if (!buffer)
+		return 0;
+
+	struct blockdev_ata_platform_data *platform = dev->info->platform_data;
+	struct blockdev_ata_private_data *priv = dev->private_data;
+	/* check for access beyond disk boundary */
+	if ((start + blkcnt) > priv->block_count)
+		blkcnt = (priv->block_count - start);
+
+	return blockctrl_write(dev->bus, platform->port_number,
+		start, blkcnt, buffer);
+}
+
+static lbaint_t erase(struct instance *dev, lbaint_t start, lbaint_t blkcnt)
+{
+	/* TRIM is not supported on ATA, so we just pretend to do something */
+	return blkcnt;
+}
+
+static int get_option(struct instance *dev, enum blockdev_option_code op,
+		struct option *result)
+{
+	struct blockdev_ata_private_data *priv = dev->private_data;
+	struct blockdev_ata_platform_data *plat = dev->info->platform_data;
+	int port = plat->port_number;
+
+	if (!result || !priv)
+		return -EINVAL;
+
+	switch (op) {
+	case BLKD_OPT_IFTYPE:
+		result->flags = OPTION_TYPE_U;
+		result->data.data_u = BLOCKDEV_IFTYPE_ATA;
+		return 0;
+	case BLKD_OPT_TYPE:
+		result->flags = OPTION_TYPE_U;
+		result->data.data_u = BLOCKDEV_TYPE_HARDDISK;
+		return 0;
+	case BLKD_OPT_OFFSET:
+		result->flags = OPTION_TYPE_U;
+		result->data.data_u = 0;
+		return 0;
+	case BLKD_OPT_BLOCKSIZE:
+		result->flags = OPTION_TYPE_U;
+		result->data.data_u = priv->block_size;
+		return 0;
+	case BLKD_OPT_BLOCKCOUNT:
+		result->flags = OPTION_TYPE_U;
+		result->data.data_u = priv->block_count;
+		return 0;
+	case BLKD_OPT_VENDOR:
+		result->flags = OPTION_TYPE_S;
+		result->data.data_s = priv->vendor;
+		return 0;
+	case BLKD_OPT_PRODUCT:
+		result->flags = OPTION_TYPE_S;
+		result->data.data_s = priv->product;
+		return 0;
+	case BLKD_OPT_REVISION:
+		result->flags = OPTION_TYPE_S;
+		result->data.data_s = priv->revision;
+		return 0;
+	case BLKD_OPT_LBA48:
+		return blockctrl_get_port_option(dev->bus, port,
+			BLKP_OPT_LBA48, result);
+	default:
+		break;
+		/* TODO: more options */
+
+	}
+	return -EINVAL;
+}
+
+static int set_option(struct instance *dev, enum blockdev_option_code op,
+		struct option *value)
+{
+	/* there are currently no meaningful options to set */
+	return -EINVAL;
+}
+
+static struct blockdev_ops ops = {
+	.read = read,
+	.write = write,
+	.erase = erase,
+	.get_option = get_option,
+	.set_option = set_option,
+};
+
+
+static int bind(struct instance *dev)
+{
+	/* prepare hint for the core to properly identify this as a ATA disk */
+	struct blockdev_core_hint hint = {
+		.iftype = BLOCKDEV_IFTYPE_ATA
+	};
+	core_bind(CORE_BLOCKDEV, dev, &ops, &hint);
+	return 0;
+}
+
+static int probe(struct instance *dev)
+{
+	struct option op;
+	struct blockdev_ata_private_data *priv;
+	int error;
+	struct blockdev_ata_platform_data *plat = dev->info->platform_data;
+	int port = plat->port_number;
+
+	priv = calloc(sizeof(*priv), 1);
+	if (!priv)
+		return -ENOMEM;
+
+	error = blockctrl_get_port_option(dev->bus, port, BLKP_OPT_BLOCKSIZE,
+			&op);
+	if (error || (OPTION_TYPE(op) != OPTION_TYPE_U))
+		return -EINVAL;
+	priv->block_size = op.data.data_u;
+
+	error = blockctrl_get_port_option(dev->bus, port, BLKP_OPT_BLOCKCOUNT,
+			&op);
+	if (error || (OPTION_TYPE(op) != OPTION_TYPE_U))
+		return -EINVAL;
+	priv->block_count = op.data.data_u;
+
+	error = blockctrl_get_port_option(dev->bus, port, BLKP_OPT_VENDOR, &op);
+	if (error || (OPTION_TYPE(op) != OPTION_TYPE_S))
+		return -EINVAL;
+	strncpy(priv->vendor, op.data.data_s, 40);
+	priv->vendor[40] = 0;
+	if (op.flags & OPTION_PTR_MALLOCED)
+		free(op.data.data_s);
+
+	error = blockctrl_get_port_option(dev->bus, port, BLKP_OPT_PRODUCT,
+			&op);
+	if (error || (OPTION_TYPE(op) != OPTION_TYPE_S))
+		return -EINVAL;
+	strncpy(priv->product, op.data.data_s, 20);
+	priv->vendor[20] = 0;
+	if (op.flags & OPTION_PTR_MALLOCED)
+		free(op.data.data_s);
+
+	error = blockctrl_get_port_option(dev->bus, port, BLKP_OPT_REVISION,
+			&op);
+	if (error || (OPTION_TYPE(op) != OPTION_TYPE_S))
+		return -EINVAL;
+	strncpy(priv->revision, op.data.data_s, 8);
+	priv->revision[8] = 0;
+	if (op.flags & OPTION_PTR_MALLOCED)
+		free(op.data.data_s);
+
+	dev->private_data = priv;
+
+	return 0;
+}
+
+static int reloc(struct instance *dev, struct instance *old)
+{
+	core_replace(CORE_BLOCKDEV, dev, old);
+	dev->private_data = malloc(sizeof(struct blockdev_ata_private_data));
+	if (!dev->private_data)
+		return -ENOMEM;
+
+	memcpy(dev->private_data, old->private_data,
+		sizeof(struct blockdev_ata_private_data));
+
+	return 0;
+}
+
+static int remove(struct instance *dev)
+{
+	/* remove our metadata cache */
+	free(dev->private_data);
+	return 0;
+}
+
+static int unbind(struct instance *dev)
+{
+	core_unbind(CORE_BLOCKDEV, dev);
+	return 0;
+}
+
+U_BOOT_DRIVER(blockdev_ata,
+	bind,
+	probe,
+	reloc,
+	remove,
+	unbind);
diff --git a/drivers/blockdev/ata.h b/drivers/blockdev/ata.h
new file mode 100644
index 0000000..da63b5d
--- /dev/null
+++ b/drivers/blockdev/ata.h
@@ -0,0 +1,37 @@
+/*
+ * (C) Copyright 2012
+ * Pavel Herrmann <morpheus.ibis@gmail.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _BLOCKDEV_ATA_H_
+#define _BLOCKDEV_ATA_H_
+#include <dm/blockdev.h>
+
+struct blockdev_ata_private_data {
+	/* cache for get_option() */
+	lbaint_t block_size;
+	lbaint_t block_count;
+	char vendor[40+1];
+	char product[20+1];
+	char revision[8+1];
+};
+
+#endif
diff --git a/drivers/blockdev/partition.c b/drivers/blockdev/partition.c
new file mode 100644
index 0000000..c5c0f48
--- /dev/null
+++ b/drivers/blockdev/partition.c
@@ -0,0 +1,179 @@
+/*
+ * (C) Copyright 2012
+ * Pavel Herrmann <morpheus.ibis@gmail.com>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <dm/blockdev.h>
+#include <dm/manager.h>
+
+static lbaint_t read(struct instance *dev, lbaint_t start, lbaint_t blkcnt,
+		void *buffer)
+{
+	if (!buffer)
+		return 0;
+
+	struct blockdev_partition_platform_data *platform =
+		dev->info->platform_data;
+	/* check for access beyond partition boundary */
+	if ((start + blkcnt) > platform->block_count)
+		blkcnt = (platform->block_count - start);
+
+	start += platform->offset;
+
+	return blockdev_read(dev->bus, start, blkcnt, buffer);
+}
+
+static lbaint_t write(struct instance *dev, lbaint_t start, lbaint_t blkcnt,
+		void *buffer)
+{
+	if (!buffer)
+		return 0;
+
+	struct blockdev_partition_platform_data *platform =
+		dev->info->platform_data;
+	/* check for access beyond partition boundary */
+	if ((start + blkcnt) > platform->block_count)
+		blkcnt = (platform->block_count - start);
+
+	start += platform->offset;
+
+	return blockdev_write(dev->bus, start, blkcnt, buffer);
+}
+
+static lbaint_t erase(struct instance *dev, lbaint_t start, lbaint_t blkcnt)
+{
+	struct blockdev_partition_platform_data *platform =
+		dev->info->platform_data;
+	/* check for access beyond partition boundary */
+	if ((start + blkcnt) > platform->block_count)
+		blkcnt = (platform->block_count - start);
+
+	start += platform->offset;
+
+	return blockdev_erase(dev->bus, start, blkcnt);
+}
+
+static int get_option(struct instance *dev, enum blockdev_option_code op,
+		struct option *result)
+{
+	if (!result)
+		return -EINVAL;
+
+	struct blockdev_partition_platform_data *platform =
+		dev->info->platform_data;
+
+	switch (op) {
+	case BLKD_OPT_TYPE:
+		result->flags = OPTION_TYPE_U;
+		result->data.data_u = BLOCKDEV_TYPE_PARTITION;
+		return 0;
+	case BLKD_OPT_BLOCKCOUNT:
+		result->flags = OPTION_TYPE_U;
+		result->data.data_u = platform->block_count;
+		return 0;
+	case BLKD_OPT_OFFSET:
+		result->flags = OPTION_TYPE_U;
+		result->data.data_u = platform->offset;
+		return 0;
+	case BLKD_OPT_IFTYPE:
+	case BLKD_OPT_BLOCKSIZE:
+	case BLKD_OPT_REMOVABLE:
+	case BLKD_OPT_LBA48:
+	case BLKD_OPT_VENDOR:
+	case BLKD_OPT_PRODUCT:
+	case BLKD_OPT_REVISION:
+	case BLKD_OPT_SCSILUN:
+	case BLKD_OPT_SCSITARGET:
+		return blockdev_get_option(dev->bus, op, result);
+
+	default:
+		break;
+	}
+
+	return -EINVAL;
+}
+
+
+static int set_option(struct instance *dev, enum blockdev_option_code op,
+		struct option *value)
+{
+	/* there are currently no meaningful options to set */
+	return -EINVAL;
+}
+
+static struct blockdev_ops ops = {
+	.read = read,
+	.write = write,
+	.erase = erase,
+	.get_option = get_option,
+	.set_option = set_option
+};
+
+
+static int bind(struct instance *dev)
+{
+	/* prepare hint for the core to properly identify this as a partition */
+	struct blockdev_partition_platform_data *platform =
+		dev->info->platform_data;
+
+	struct blockdev_core_hint hint = {
+		.iftype = BLOCKDEV_IFTYPE_PARTITION,
+		.part_number = platform->part_number
+	};
+	core_bind(CORE_BLOCKDEV, dev, &ops, &hint);
+
+	return 0;
+}
+
+static int probe(struct instance *dev)
+{
+	/* there is nothing to activate */
+	return 0;
+}
+
+static int reloc(struct instance *dev, struct instance *old)
+{
+	core_replace(CORE_BLOCKDEV, dev, old);
+	/* there are no private data to relocate */
+
+	return 0;
+}
+
+static int remove(struct instance *dev)
+{
+	/* nothing to do here */
+	return 0;
+}
+
+static int unbind(struct instance *dev)
+{
+	core_unbind(CORE_BLOCKDEV, dev);
+
+	return 0;
+}
+
+
+U_BOOT_DRIVER(blockdev_partition,
+	bind,
+	probe,
+	reloc,
+	remove,
+	unbind);
-- 
1.7.12

  parent reply	other threads:[~2012-09-20 19:37 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-20 19:37 [U-Boot] [PATCH 00/11] Add DM blockdev subsystem Pavel Herrmann
2012-09-20 19:37 ` [U-Boot] [PATCH 01/11] DM: add block device core Pavel Herrmann
2012-09-20 19:58   ` Marek Vasut
2012-09-21  7:11     ` Pavel Herrmann
2012-09-21 12:39       ` Marek Vasut
2012-09-21 13:27         ` Pavel Herrmann
2012-09-21 13:53           ` Marek Vasut
2012-09-21 14:57             ` Pavel Herrmann
2012-09-21 15:34               ` Marek Vasut
2012-09-21 15:48                 ` Pavel Herrmann
2012-09-21 15:55                   ` Marek Vasut
2012-09-21 17:19                     ` Pavel Herrmann
2012-09-21 18:00                       ` Marek Vasut
2012-09-21 18:53                         ` Pavel Herrmann
2012-09-21 19:17                           ` Marek Vasut
2012-09-21 19:29                             ` Pavel Herrmann
2012-09-21 21:11                               ` Marek Vasut
2012-09-21 23:43                                 ` Pavel Herrmann
2012-09-22  0:09                                   ` Marek Vasut
2012-09-22  9:39                                     ` Pavel Herrmann
2012-09-22 13:33                                       ` Marek Vasut
2012-09-22 13:59                                         ` Pavel Herrmann
2012-09-24 12:23                                           ` Pavel Herrmann
2012-09-20 20:49   ` [U-Boot] [U-Boot-DM] " Vikram Narayanan
2012-09-21  7:09     ` Pavel Herrmann
2012-09-21 12:39       ` Marek Vasut
2012-09-20 19:37 ` [U-Boot] [PATCH 02/11] DM: add support for scanning DOS partitions to blockdev core Pavel Herrmann
2012-09-20 20:03   ` Marek Vasut
2012-09-21  7:22     ` Pavel Herrmann
2012-09-21 12:47       ` Marek Vasut
2012-09-21 13:18         ` Pavel Herrmann
2012-09-21 13:54           ` Marek Vasut
2012-09-20 19:37 ` [U-Boot] [PATCH 03/11] DM: add block controller core Pavel Herrmann
2012-09-20 20:05   ` Marek Vasut
2012-09-21  7:21     ` Pavel Herrmann
2012-09-21 12:51       ` Marek Vasut
2012-09-21 13:14         ` Pavel Herrmann
2012-09-21 13:56           ` Marek Vasut
2012-09-21 15:04             ` Pavel Herrmann
2012-09-21 13:33         ` Pavel Herrmann
2012-09-21 13:58           ` Marek Vasut
2012-09-21 15:09             ` Pavel Herrmann
2012-09-21 15:39               ` Marek Vasut
2012-09-21 15:46                 ` Pavel Herrmann
2012-09-21 16:08                   ` Marek Vasut
2012-09-21 17:22                     ` Pavel Herrmann
2012-09-21 18:01                       ` Marek Vasut
2012-09-21 19:15                         ` Pavel Herrmann
2012-09-21 19:22                           ` Marek Vasut
2012-09-20 19:37 ` [U-Boot] [PATCH 04/11] DM: add sata_legacy driver for blockctrl Pavel Herrmann
2012-09-20 19:37 ` Pavel Herrmann [this message]
2012-09-20 19:37 ` [U-Boot] [PATCH 06/11] DM: add cmd_block command Pavel Herrmann
2012-09-20 19:37 ` [U-Boot] [PATCH 07/11] DM: use new blockdev API in FAT Pavel Herrmann
2012-09-20 19:37 ` [U-Boot] [PATCH 08/11] DM: use new blockdev API in ext2 Pavel Herrmann
2012-09-20 19:37 ` [U-Boot] [PATCH 09/11] DM: use new blockdev API in reiserfs Pavel Herrmann
2012-09-20 19:37 ` [U-Boot] [PATCH 10/11] DM: use new blockdev API in ZFS Pavel Herrmann
2012-09-20 19:37 ` [U-Boot] [PATCH 11/11] DM: switch sandbox to DM blockdev Pavel Herrmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1348169867-2917-6-git-send-email-morpheus.ibis@gmail.com \
    --to=morpheus.ibis@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox