From faa5b2063d38ce11376c9bbb3abc2d27d959b79e Mon Sep 17 00:00:00 2001
From: Frank Hunleth <fhunleth@troodon-software.com>
Date: Sat, 30 Jul 2016 14:30:21 -0400
Subject: [PATCH] mmc_blk: remove max_devices variable

This addresses the following error:

[    0.769920] Waiting for root device /dev/mmcblk0p1...
[    1.391827] mmc0: new high speed SD card at address 1234
[    1.402674] mmcblk: probe of mmc0:1234 failed with error -28

For unknown reasons, the max_devices variable would get corrupt between
initialization and use. It was also only this variable and adding guard
variables around it didn't change anything. Normally, I'd expect this
issue to point to a larger problem, but I couldn't find it. To work
around the corruption, I removed all of the code and data related to it.
I can't imagine ever using the feature anyway.
---
 drivers/mmc/card/block.c | 35 +++++++----------------------------
 1 file changed, 7 insertions(+), 28 deletions(-)

diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index c641c20..8d526a7 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -72,19 +72,6 @@ MODULE_ALIAS("mmc:block");
 
 static DEFINE_MUTEX(block_mutex);
 
-/*
- * The defaults come from config options but can be overriden by module
- * or bootarg options.
- */
-static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
-
-/*
- * We've only got one major, so number of mmcblk devices is
- * limited to (1 << 20) / number of minors per device.  It is also
- * currently limited by the size of the static bitmaps below.
- */
-static int max_devices;
-
 #define MAX_DEVICES 256
 
 /* TODO: Replace these with struct ida */
@@ -134,9 +121,6 @@ enum {
 	MMC_PACKED_NR_SINGLE,
 };
 
-module_param(perdev_minors, int, 0444);
-MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
-
 static inline int mmc_blk_part_switch(struct mmc_card *card,
 				      struct mmc_blk_data *md);
 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
@@ -172,10 +156,10 @@ static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
 static inline int mmc_get_devidx(struct gendisk *disk)
 {
 	int devmaj = MAJOR(disk_devt(disk));
-	int devidx = MINOR(disk_devt(disk)) / perdev_minors;
+	int devidx = MINOR(disk_devt(disk)) / CONFIG_MMC_BLOCK_MINORS;
 
 	if (!devmaj)
-		devidx = disk->first_minor / perdev_minors;
+		devidx = disk->first_minor / CONFIG_MMC_BLOCK_MINORS;
 	return devidx;
 }
 
@@ -2196,8 +2180,8 @@ static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
 	struct mmc_blk_data *md;
 	int devidx, ret;
 
-	devidx = find_first_zero_bit(dev_use, max_devices);
-	if (devidx >= max_devices)
+	devidx = find_first_zero_bit(dev_use, (MAX_DEVICES / CONFIG_MMC_BLOCK_MINORS));
+	if (devidx >= (MAX_DEVICES / CONFIG_MMC_BLOCK_MINORS))
 		return ERR_PTR(-ENOSPC);
 	__set_bit(devidx, dev_use);
 
@@ -2214,7 +2198,7 @@ static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
 	 * index anymore so we keep track of a name index.
 	 */
 	if (!subname) {
-		md->name_idx = find_first_zero_bit(name_use, max_devices);
+		md->name_idx = find_first_zero_bit(name_use, (MAX_DEVICES / CONFIG_MMC_BLOCK_MINORS));
 		__set_bit(md->name_idx, name_use);
 	} else
 		md->name_idx = ((struct mmc_blk_data *)
@@ -2228,7 +2212,7 @@ static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
 	 */
 	md->read_only = mmc_blk_readonly(card);
 
-	md->disk = alloc_disk(perdev_minors);
+	md->disk = alloc_disk(CONFIG_MMC_BLOCK_MINORS);
 	if (md->disk == NULL) {
 		ret = -ENOMEM;
 		goto err_kfree;
@@ -2246,7 +2230,7 @@ static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
 	md->queue.data = md;
 
 	md->disk->major	= MMC_BLOCK_MAJOR;
-	md->disk->first_minor = devidx * perdev_minors;
+	md->disk->first_minor = devidx * CONFIG_MMC_BLOCK_MINORS;
 	md->disk->fops = &mmc_bdops;
 	md->disk->private_data = md;
 	md->disk->queue = md->queue.queue;
@@ -2690,11 +2674,6 @@ static int __init mmc_blk_init(void)
 {
 	int res;
 
-	if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
-		pr_info("mmcblk: using %d minors per device\n", perdev_minors);
-
-	max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
-
 	res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
 	if (res)
 		goto out;
-- 
2.7.4

