/* Return: 0 in case of success, a negative error code otherwise.  */
int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
/* Return: 0 in case of success, a negative error code otherwise.  */
int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos)
{
	struct mtd_info *mtd = nanddev_to_mtd(nand);
	unsigned int entry;
	int ret = 0;
	if (nanddev_isbad(nand, pos))
		return 0;
	ret = nand->ops->markbad(nand, pos);
	if (ret)
		pr_warn("failed to write BBM to block @%llx (err = %d)\n",
			nanddev_pos_to_offs(nand, pos), ret);
	if (!nanddev_bbt_is_initialized(nand))
		goto out;
	entry = nanddev_bbt_pos_to_entry(nand, pos);
	ret = nanddev_bbt_set_block_status(nand, entry, NAND_BBT_BLOCK_WORN);
	if (ret)
		goto out;
	ret = nanddev_bbt_update(nand);
out:
	if (!ret)
		mtd->ecc_stats.badblocks++;
	return ret;
}