diff mbox

[v3,1/8] mtd: nand: add more helpers in nand.h

Message ID 1489646857-10112-2-git-send-email-peterpandong@micron.com
State Superseded
Delegated to: Boris Brezillon
Headers show

Commit Message

Peter Pan 潘栋 (peterpandong) March 16, 2017, 6:47 a.m. UTC
This commit adds some helpers in nand.h
    nand_size()
    valid_nand_address()
    valid_nand_oob_ops()
    nand_oob_ops_across_page()
    valid_nand_erase_ops()

Signed-off-by: Peter Pan <peterpandong@micron.com>
---
 include/linux/mtd/nand.h | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

Comments

Boris Brezillon March 17, 2017, 1:07 p.m. UTC | #1
On Thu, 16 Mar 2017 14:47:30 +0800
Peter Pan <peterpandong@micron.com> wrote:

> This commit adds some helpers in nand.h
>     nand_size()
>     valid_nand_address()
>     valid_nand_oob_ops()
>     nand_oob_ops_across_page()
>     valid_nand_erase_ops()
> 
> Signed-off-by: Peter Pan <peterpandong@micron.com>
> ---
>  include/linux/mtd/nand.h | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 59 insertions(+)
> 
> diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
> index c2197b4..4a812e6 100644
> --- a/include/linux/mtd/nand.h
> +++ b/include/linux/mtd/nand.h
> @@ -410,6 +410,65 @@ static inline int nand_neraseblocks(struct nand_device *nand)
>  }
>  
>  /**
> + * nand_size - Get NAND size
> + * @nand: NAND device
> + *
> + * Returns the total size exposed by @nand.
> + */
> +static inline u64 nand_size(struct nand_device *nand)
> +{
> +	return nand->memorg.ndies * nand->memorg.diesize;
> +}
> +
> +static inline bool valid_nand_address(struct nand_device *nand, loff_t addr)

Please keep consistent prefixes. Maybe we should name it
nand_check_address(), return -EINVAL if it's wrong and 0 otherwise:

static inline int nand_check_address(struct nand_device *nand,
				     loff_t addr)
{
	return addr < nand_size(nand) ? 0 : -EINVAL;
}

> +{
> +	return addr < nand_size(nand);
> +}
> +
> +static inline bool valid_nand_oob_ops(struct nand_device *nand, loff_t start,
> +				      struct mtd_oob_ops *ops)
> +{
> +	struct mtd_info *mtd = nand_to_mtd(nand);
> +	int ooblen = ops->mode == MTD_OPS_AUTO_OOB ?
> +		mtd->oobavail : mtd->oobsize;
> +
> +	if (ops->ooboffs >= ooblen)
> +		return false;
> +	if (ops->ooboffs + ops->ooblen >
> +	    (nand_len_to_pages(nand, nand_size(nand)) -
> +			       nand_offs_to_page(nand, start)) * ooblen)
> +		return false;
> +
> +	return true;
> +}

Ditto.

> +
> +static inline bool nand_oob_ops_across_page(struct nand_device *nand,
> +					    struct mtd_oob_ops *ops)
> +{
> +	struct mtd_info *mtd = nand_to_mtd(nand);
> +	int ooblen = ops->mode == MTD_OPS_AUTO_OOB ?
> +		     mtd->oobavail : mtd->oobsize;
> +
> +	return (ops->ooboffs + ops->ooblen) > ooblen;
> +}
> +
> +static inline bool valid_nand_erase_ops(struct nand_device *nand,
> +					struct erase_info *einfo)

Ditto.

> +{
> +	/* check address align on block boundary */
> +	if (einfo->addr & (nand_eraseblock_size(nand) - 1))
> +		return false;
> +	/* check lendth align on block boundary */
> +	if (einfo->len & (nand_eraseblock_size(nand) - 1))
> +		return false;
> +	/* Do not allow erase past end of device */
> +	if ((einfo->addr + einfo->len) > nand_size(nand))
> +		return false;
> +
> +	return true;
> +}
> +
> +/**
>   * nand_register - Register a NAND device
>   * @nand: NAND device
>   *
Peter Pan March 20, 2017, 4:51 a.m. UTC | #2
Hi Boris,

On Fri, Mar 17, 2017 at 9:07 PM, Boris Brezillon
<boris.brezillon@free-electrons.com> wrote:
> On Thu, 16 Mar 2017 14:47:30 +0800
> Peter Pan <peterpandong@micron.com> wrote:
>
>> This commit adds some helpers in nand.h
>>     nand_size()
>>     valid_nand_address()
>>     valid_nand_oob_ops()
>>     nand_oob_ops_across_page()
>>     valid_nand_erase_ops()
>>
>> Signed-off-by: Peter Pan <peterpandong@micron.com>
>> ---
>>  include/linux/mtd/nand.h | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 59 insertions(+)
>>
>> diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
>> index c2197b4..4a812e6 100644
>> --- a/include/linux/mtd/nand.h
>> +++ b/include/linux/mtd/nand.h
>> @@ -410,6 +410,65 @@ static inline int nand_neraseblocks(struct nand_device *nand)
>>  }
>>
>>  /**
>> + * nand_size - Get NAND size
>> + * @nand: NAND device
>> + *
>> + * Returns the total size exposed by @nand.
>> + */
>> +static inline u64 nand_size(struct nand_device *nand)
>> +{
>> +     return nand->memorg.ndies * nand->memorg.diesize;
>> +}
>> +
>> +static inline bool valid_nand_address(struct nand_device *nand, loff_t addr)
>
> Please keep consistent prefixes. Maybe we should name it
> nand_check_address(), return -EINVAL if it's wrong and 0 otherwise:
>
> static inline int nand_check_address(struct nand_device *nand,
>                                      loff_t addr)
> {
>         return addr < nand_size(nand) ? 0 : -EINVAL;
> }

Fix this in v4

>
>> +{
>> +     return addr < nand_size(nand);
>> +}
>> +
>> +static inline bool valid_nand_oob_ops(struct nand_device *nand, loff_t start,
>> +                                   struct mtd_oob_ops *ops)
>> +{
>> +     struct mtd_info *mtd = nand_to_mtd(nand);
>> +     int ooblen = ops->mode == MTD_OPS_AUTO_OOB ?
>> +             mtd->oobavail : mtd->oobsize;
>> +
>> +     if (ops->ooboffs >= ooblen)
>> +             return false;
>> +     if (ops->ooboffs + ops->ooblen >
>> +         (nand_len_to_pages(nand, nand_size(nand)) -
>> +                            nand_offs_to_page(nand, start)) * ooblen)
>> +             return false;
>> +
>> +     return true;
>> +}
>
> Ditto.

Fix this in v4.
BTW, I considered to put check the mtd ops consistency here. What do you think?

>
>> +
>> +static inline bool nand_oob_ops_across_page(struct nand_device *nand,
>> +                                         struct mtd_oob_ops *ops)
>> +{
>> +     struct mtd_info *mtd = nand_to_mtd(nand);
>> +     int ooblen = ops->mode == MTD_OPS_AUTO_OOB ?
>> +                  mtd->oobavail : mtd->oobsize;
>> +
>> +     return (ops->ooboffs + ops->ooblen) > ooblen;
>> +}
>> +
>> +static inline bool valid_nand_erase_ops(struct nand_device *nand,
>> +                                     struct erase_info *einfo)
>
> Ditto.

Fix this in v4.

Thanks
Peter Pan
diff mbox

Patch

diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
index c2197b4..4a812e6 100644
--- a/include/linux/mtd/nand.h
+++ b/include/linux/mtd/nand.h
@@ -410,6 +410,65 @@  static inline int nand_neraseblocks(struct nand_device *nand)
 }
 
 /**
+ * nand_size - Get NAND size
+ * @nand: NAND device
+ *
+ * Returns the total size exposed by @nand.
+ */
+static inline u64 nand_size(struct nand_device *nand)
+{
+	return nand->memorg.ndies * nand->memorg.diesize;
+}
+
+static inline bool valid_nand_address(struct nand_device *nand, loff_t addr)
+{
+	return addr < nand_size(nand);
+}
+
+static inline bool valid_nand_oob_ops(struct nand_device *nand, loff_t start,
+				      struct mtd_oob_ops *ops)
+{
+	struct mtd_info *mtd = nand_to_mtd(nand);
+	int ooblen = ops->mode == MTD_OPS_AUTO_OOB ?
+		mtd->oobavail : mtd->oobsize;
+
+	if (ops->ooboffs >= ooblen)
+		return false;
+	if (ops->ooboffs + ops->ooblen >
+	    (nand_len_to_pages(nand, nand_size(nand)) -
+			       nand_offs_to_page(nand, start)) * ooblen)
+		return false;
+
+	return true;
+}
+
+static inline bool nand_oob_ops_across_page(struct nand_device *nand,
+					    struct mtd_oob_ops *ops)
+{
+	struct mtd_info *mtd = nand_to_mtd(nand);
+	int ooblen = ops->mode == MTD_OPS_AUTO_OOB ?
+		     mtd->oobavail : mtd->oobsize;
+
+	return (ops->ooboffs + ops->ooblen) > ooblen;
+}
+
+static inline bool valid_nand_erase_ops(struct nand_device *nand,
+					struct erase_info *einfo)
+{
+	/* check address align on block boundary */
+	if (einfo->addr & (nand_eraseblock_size(nand) - 1))
+		return false;
+	/* check lendth align on block boundary */
+	if (einfo->len & (nand_eraseblock_size(nand) - 1))
+		return false;
+	/* Do not allow erase past end of device */
+	if ((einfo->addr + einfo->len) > nand_size(nand))
+		return false;
+
+	return true;
+}
+
+/**
  * nand_register - Register a NAND device
  * @nand: NAND device
  *