mmc: Allow host drivers to specify a max block size

Most controllers have an upper limit on the block size. Allow the host
drivers to specify this and make sure we avoid hitting this limit.

Signed-off-by: Pierre Ossman <drzeus@drzeus.cx>
diff --git a/drivers/mmc/at91_mci.c b/drivers/mmc/at91_mci.c
index aa152f3..e28850d 100644
--- a/drivers/mmc/at91_mci.c
+++ b/drivers/mmc/at91_mci.c
@@ -823,6 +823,8 @@
 	mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
 	mmc->caps = MMC_CAP_BYTEBLOCK;
 
+	mmc->max_blk_size = 4095;
+
 	host = mmc_priv(mmc);
 	host->mmc = mmc;
 	host->buffer = NULL;
diff --git a/drivers/mmc/au1xmmc.c b/drivers/mmc/au1xmmc.c
index 6d6fe6e..74e6ac0 100644
--- a/drivers/mmc/au1xmmc.c
+++ b/drivers/mmc/au1xmmc.c
@@ -922,6 +922,8 @@
 		mmc->max_seg_size = AU1XMMC_DESCRIPTOR_SIZE;
 		mmc->max_phys_segs = AU1XMMC_DESCRIPTOR_COUNT;
 
+		mmc->max_blk_size = 2048;
+
 		mmc->ocr_avail = AU1XMMC_OCR;
 
 		host = mmc_priv(mmc);
diff --git a/drivers/mmc/imxmmc.c b/drivers/mmc/imxmmc.c
index bfb9ff6..2107f8a 100644
--- a/drivers/mmc/imxmmc.c
+++ b/drivers/mmc/imxmmc.c
@@ -960,6 +960,7 @@
 	mmc->max_phys_segs = 64;
 	mmc->max_sectors = 64;		/* default 1 << (PAGE_CACHE_SHIFT - 9) */
 	mmc->max_seg_size = 64*512;	/* default PAGE_CACHE_SIZE */
+	mmc->max_blk_size = 2048;
 
 	host = mmc_priv(mmc);
 	host->mmc = mmc;
diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c
index b48c277..9bda3fd 100644
--- a/drivers/mmc/mmc.c
+++ b/drivers/mmc/mmc.c
@@ -108,6 +108,8 @@
 	mrq->cmd->error = 0;
 	mrq->cmd->mrq = mrq;
 	if (mrq->data) {
+		BUG_ON(mrq->data->blksz > host->max_blk_size);
+
 		mrq->cmd->data = mrq->data;
 		mrq->data->error = 0;
 		mrq->data->mrq = mrq;
@@ -1605,6 +1607,8 @@
 		host->max_phys_segs = 1;
 		host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
 		host->max_seg_size = PAGE_CACHE_SIZE;
+
+		host->max_blk_size = 512;
 	}
 
 	return host;
diff --git a/drivers/mmc/mmci.c b/drivers/mmc/mmci.c
index ccfe656..5d48e00 100644
--- a/drivers/mmc/mmci.c
+++ b/drivers/mmc/mmci.c
@@ -534,6 +534,11 @@
 	 */
 	mmc->max_seg_size = mmc->max_sectors << 9;
 
+	/*
+	 * Block size can be up to 2048 bytes, but must be a power of two.
+	 */
+	mmc->max_blk_size = 2048;
+
 	spin_lock_init(&host->lock);
 
 	writel(0, host->base + MMCIMASK0);
diff --git a/drivers/mmc/omap.c b/drivers/mmc/omap.c
index d30540b..fa69a0d 100644
--- a/drivers/mmc/omap.c
+++ b/drivers/mmc/omap.c
@@ -1099,6 +1099,7 @@
 	 */
 	mmc->max_phys_segs = 32;
 	mmc->max_hw_segs = 32;
+	mmc->max_blk_size = 2048;	/* BLEN is 11 bits (+1) */
 	mmc->max_sectors = 256; /* NBLK max 11-bits, OMAP also limited by DMA */
 	mmc->max_seg_size = mmc->max_sectors * 512;
 
diff --git a/drivers/mmc/pxamci.c b/drivers/mmc/pxamci.c
index 6073d99..9fc9aed 100644
--- a/drivers/mmc/pxamci.c
+++ b/drivers/mmc/pxamci.c
@@ -450,6 +450,11 @@
 	 */
 	mmc->max_seg_size = PAGE_SIZE;
 
+	/*
+	 * Block length register is 10 bits.
+	 */
+	mmc->max_blk_size = 1023;
+
 	host = mmc_priv(mmc);
 	host->mmc = mmc;
 	host->dma = -1;
diff --git a/drivers/mmc/sdhci.c b/drivers/mmc/sdhci.c
index 175a942..155aafe 100644
--- a/drivers/mmc/sdhci.c
+++ b/drivers/mmc/sdhci.c
@@ -381,7 +381,7 @@
 
 	/* Sanity checks */
 	BUG_ON(data->blksz * data->blocks > 524288);
-	BUG_ON(data->blksz > host->max_block);
+	BUG_ON(data->blksz > host->mmc->max_blk_size);
 	BUG_ON(data->blocks > 65535);
 
 	/* timeout in us */
@@ -1290,15 +1290,6 @@
 	if (caps & SDHCI_TIMEOUT_CLK_UNIT)
 		host->timeout_clk *= 1000;
 
-	host->max_block = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
-	if (host->max_block >= 3) {
-		printk(KERN_ERR "%s: Invalid maximum block size.\n",
-			host->slot_descr);
-		ret = -ENODEV;
-		goto unmap;
-	}
-	host->max_block = 512 << host->max_block;
-
 	/*
 	 * Set host parameters.
 	 */
@@ -1353,6 +1344,19 @@
 	mmc->max_seg_size = mmc->max_sectors * 512;
 
 	/*
+	 * Maximum block size. This varies from controller to controller and
+	 * is specified in the capabilities register.
+	 */
+	mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
+	if (mmc->max_blk_size >= 3) {
+		printk(KERN_ERR "%s: Invalid maximum block size.\n",
+			host->slot_descr);
+		ret = -ENODEV;
+		goto unmap;
+	}
+	mmc->max_blk_size = 512 << mmc->max_blk_size;
+
+	/*
 	 * Init tasklets.
 	 */
 	tasklet_init(&host->card_tasklet,
diff --git a/drivers/mmc/sdhci.h b/drivers/mmc/sdhci.h
index f9d1a0a..bc6bf7e 100644
--- a/drivers/mmc/sdhci.h
+++ b/drivers/mmc/sdhci.h
@@ -174,7 +174,6 @@
 
 	unsigned int		max_clk;	/* Max possible freq (MHz) */
 	unsigned int		timeout_clk;	/* Timeout freq (KHz) */
-	unsigned int		max_block;	/* Max block size (bytes) */
 
 	unsigned int		clock;		/* Current clock (MHz) */
 	unsigned short		power;		/* Current voltage */
diff --git a/drivers/mmc/tifm_sd.c b/drivers/mmc/tifm_sd.c
index ce19b04..bdfad15 100644
--- a/drivers/mmc/tifm_sd.c
+++ b/drivers/mmc/tifm_sd.c
@@ -886,7 +886,9 @@
 	mmc->max_hw_segs = 1;
 	mmc->max_phys_segs = 1;
 	mmc->max_sectors = 127;
-	mmc->max_seg_size = mmc->max_sectors << 11; //2k maximum hw block length
+	//2k maximum hw block length
+	mmc->max_seg_size = mmc->max_sectors << 11;
+	mmc->max_blk_size = 2048;
 	sock->signal_irq = tifm_sd_signal_irq;
 	rc = tifm_sd_initialize_host(host);
 
diff --git a/drivers/mmc/wbsd.c b/drivers/mmc/wbsd.c
index 7a28267..5711bee 100644
--- a/drivers/mmc/wbsd.c
+++ b/drivers/mmc/wbsd.c
@@ -1354,6 +1354,12 @@
 	 */
 	mmc->max_seg_size = mmc->max_sectors * 512;
 
+	/*
+	 * Maximum block size. We have 12 bits (= 4095) but have to subtract
+	 * space for CRC. So the maximum is 4095 - 4*2 = 4087.
+	 */
+	mmc->max_blk_size = 4087;
+
 	dev_set_drvdata(dev, mmc);
 
 	return 0;
diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
index ae98d67..2da0c91 100644
--- a/include/linux/mmc/host.h
+++ b/include/linux/mmc/host.h
@@ -94,6 +94,7 @@
 	unsigned short		max_phys_segs;	/* see blk_queue_max_phys_segments */
 	unsigned short		max_sectors;	/* see blk_queue_max_sectors */
 	unsigned short		unused;
+	unsigned int		max_blk_size;	/* maximum size of one mmc block */
 
 	/* private data */
 	struct mmc_ios		ios;		/* current io bus settings */