block: pass block dev not num to read/write/erase()

This will allow the implementation to make use of data in the block_dev
structure beyond the base device number. This will be useful so that eMMC
block devices can encompass the HW partition ID rather than treating this
out-of-band. Equally, the existence of the priv field is crying out for
this patch to exist.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
diff --git a/common/cmd_disk.c b/common/cmd_disk.c
index 8a1fda9..3025225 100644
--- a/common/cmd_disk.c
+++ b/common/cmd_disk.c
@@ -56,7 +56,7 @@
 	      ", Block Size: %ld\n",
 	      info.start, info.size, info.blksz);
 
-	if (dev_desc->block_read(dev, info.start, 1, (ulong *) addr) != 1) {
+	if (dev_desc->block_read(dev_desc, info.start, 1, (ulong *)addr) != 1) {
 		printf("** Read error on %d:%d\n", dev, part);
 		bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
 		return 1;
@@ -100,8 +100,8 @@
 	cnt /= info.blksz;
 	cnt -= 1;
 
-	if (dev_desc->block_read(dev, info.start + 1, cnt,
-					 (ulong *)(addr + info.blksz)) != cnt) {
+	if (dev_desc->block_read(dev_desc, info.start + 1, cnt,
+				 (ulong *)(addr + info.blksz)) != cnt) {
 		printf("** Read error on %d:%d\n", dev, part);
 		bootstage_error(BOOTSTAGE_ID_IDE_READ);
 		return 1;
diff --git a/common/cmd_ide.c b/common/cmd_ide.c
index ecd3e9d..f19a7ce 100644
--- a/common/cmd_ide.c
+++ b/common/cmd_ide.c
@@ -79,8 +79,8 @@
 
 #ifdef CONFIG_ATAPI
 static void	atapi_inquiry(block_dev_desc_t *dev_desc);
-static ulong atapi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
-			void *buffer);
+static ulong atapi_read(block_dev_desc_t *block_dev, lbaint_t blknr,
+			lbaint_t blkcnt, void *buffer);
 #endif
 
 
@@ -187,6 +187,7 @@
 		if (strcmp(argv[1], "read") == 0) {
 			ulong addr = simple_strtoul(argv[2], NULL, 16);
 			ulong cnt = simple_strtoul(argv[4], NULL, 16);
+			block_dev_desc_t *dev_desc;
 			ulong n;
 
 #ifdef CONFIG_SYS_64BIT_LBA
@@ -201,9 +202,9 @@
 				curr_device, blk, cnt);
 #endif
 
-			n = ide_dev_desc[curr_device].block_read(curr_device,
-								 blk, cnt,
-								 (ulong *)addr);
+			dev_desc = &ide_dev_desc[curr_device];
+			n = dev_desc->block_read(dev_desc, blk, cnt,
+						 (ulong *)addr);
 			/* flush cache after read */
 			flush_cache(addr,
 				    cnt * ide_dev_desc[curr_device].blksz);
@@ -230,7 +231,8 @@
 			printf("\nIDE write: device %d block # %ld, count %ld ... ",
 				curr_device, blk, cnt);
 #endif
-			n = ide_write(curr_device, blk, cnt, (ulong *) addr);
+			n = ide_write(&ide_dev_desc[curr_device], blk, cnt,
+				      (ulong *)addr);
 
 			printf("%ld blocks written: %s\n",
 				n, (n == cnt) ? "OK" : "ERROR");
@@ -711,8 +713,10 @@
 
 /* ------------------------------------------------------------------------- */
 
-ulong ide_read(int device, lbaint_t blknr, lbaint_t blkcnt, void *buffer)
+ulong ide_read(block_dev_desc_t *block_dev, lbaint_t blknr, lbaint_t blkcnt,
+	       void *buffer)
 {
+	int device = block_dev->dev;
 	ulong n = 0;
 	unsigned char c;
 	unsigned char pwrsave = 0;	/* power save */
@@ -835,8 +839,10 @@
 /* ------------------------------------------------------------------------- */
 
 
-ulong ide_write(int device, lbaint_t blknr, lbaint_t blkcnt, const void *buffer)
+ulong ide_write(block_dev_desc_t *block_dev, lbaint_t blknr, lbaint_t blkcnt,
+		const void *buffer)
 {
+	int device = block_dev->dev;
 	ulong n = 0;
 	unsigned char c;
 
@@ -1388,8 +1394,10 @@
 #define ATAPI_READ_BLOCK_SIZE	2048	/* assuming CD part */
 #define ATAPI_READ_MAX_BLOCK	(ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
 
-ulong atapi_read(int device, lbaint_t blknr, lbaint_t blkcnt, void *buffer)
+ulong atapi_read(block_dev_desc_t *block_dev, lbaint_t blknr, lbaint_t blkcnt,
+		 void *buffer)
 {
+	int device = block_dev->dev;
 	ulong n = 0;
 	unsigned char ccb[12];	/* Command descriptor block */
 	ulong cnt;
diff --git a/common/cmd_mmc.c b/common/cmd_mmc.c
index a6b7313..6b5c1ac 100644
--- a/common/cmd_mmc.c
+++ b/common/cmd_mmc.c
@@ -351,7 +351,7 @@
 	printf("\nMMC read: dev # %d, block # %d, count %d ... ",
 	       curr_device, blk, cnt);
 
-	n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
+	n = mmc->block_dev.block_read(&mmc->block_dev, blk, cnt, addr);
 	/* flush cache after read */
 	flush_cache((ulong)addr, cnt * 512); /* FIXME */
 	printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
@@ -383,7 +383,7 @@
 		printf("Error: card is write protected!\n");
 		return CMD_RET_FAILURE;
 	}
-	n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
+	n = mmc->block_dev.block_write(&mmc->block_dev, blk, cnt, addr);
 	printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
 
 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
@@ -411,7 +411,7 @@
 		printf("Error: card is write protected!\n");
 		return CMD_RET_FAILURE;
 	}
-	n = mmc->block_dev.block_erase(curr_device, blk, cnt);
+	n = mmc->block_dev.block_erase(&mmc->block_dev, blk, cnt);
 	printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
 
 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
diff --git a/common/cmd_read.c b/common/cmd_read.c
index f0fc9bf..8710288 100644
--- a/common/cmd_read.c
+++ b/common/cmd_read.c
@@ -66,7 +66,7 @@
 		return 1;
 	}
 
-	if (dev_desc->block_read(dev, offset + blk, cnt, addr) < 0) {
+	if (dev_desc->block_read(dev_desc, offset + blk, cnt, addr) < 0) {
 		printf("Error reading blocks\n");
 		return 1;
 	}
diff --git a/common/cmd_sata.c b/common/cmd_sata.c
index 51f6703..76bacea 100644
--- a/common/cmd_sata.c
+++ b/common/cmd_sata.c
@@ -18,6 +18,18 @@
 static int sata_curr_device = -1;
 block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
 
+static unsigned long sata_bread(block_dev_desc_t *block_dev, lbaint_t start,
+				lbaint_t blkcnt, void *dst)
+{
+	return sata_read(block_dev->dev, start, blkcnt, dst);
+}
+
+static unsigned long sata_bwrite(block_dev_desc_t *block_dev, lbaint_t start,
+				 lbaint_t blkcnt, const void *buffer)
+{
+	return sata_write(block_dev->dev, start, blkcnt, buffer);
+}
+
 int __sata_initialize(void)
 {
 	int rc;
@@ -32,8 +44,8 @@
 		sata_dev_desc[i].lba = 0;
 		sata_dev_desc[i].blksz = 512;
 		sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz);
-		sata_dev_desc[i].block_read = sata_read;
-		sata_dev_desc[i].block_write = sata_write;
+		sata_dev_desc[i].block_read = sata_bread;
+		sata_dev_desc[i].block_write = sata_bwrite;
 
 		rc = init_sata(i);
 		if (!rc) {
diff --git a/common/cmd_scsi.c b/common/cmd_scsi.c
index 8695408..bc7d1b6 100644
--- a/common/cmd_scsi.c
+++ b/common/cmd_scsi.c
@@ -66,9 +66,9 @@
 
 static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity,
 			      unsigned long *blksz);
-static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
-		       void *buffer);
-static ulong scsi_write(int device, lbaint_t blknr,
+static ulong scsi_read(block_dev_desc_t *block_dev, lbaint_t blknr,
+		       lbaint_t blkcnt, void *buffer);
+static ulong scsi_write(block_dev_desc_t *block_dev, lbaint_t blknr,
 			lbaint_t blkcnt, const void *buffer);
 
 
@@ -346,7 +346,8 @@
 				ulong n;
 				printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
 						scsi_curr_dev, blk, cnt);
-				n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
+				n = scsi_read(&scsi_dev_desc[scsi_curr_dev],
+					      blk, cnt, (ulong *)addr);
 				printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
 				return 0;
 			} else if (strcmp(argv[1], "write") == 0) {
@@ -357,8 +358,8 @@
 				printf("\nSCSI write: device %d block # %ld, "
 				       "count %ld ... ",
 				       scsi_curr_dev, blk, cnt);
-				n = scsi_write(scsi_curr_dev, blk, cnt,
-					       (ulong *)addr);
+				n = scsi_write(&scsi_dev_desc[scsi_curr_dev],
+					       blk, cnt, (ulong *)addr);
 				printf("%ld blocks written: %s\n", n,
 				       (n == cnt) ? "OK" : "ERROR");
 				return 0;
@@ -375,9 +376,10 @@
 #define SCSI_MAX_READ_BLK 0xFFFF
 #define SCSI_LBA48_READ	0xFFFFFFF
 
-static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
-		       void *buffer)
+static ulong scsi_read(block_dev_desc_t *block_dev, lbaint_t blknr,
+		       lbaint_t blkcnt, void *buffer)
 {
+	int device = block_dev->dev;
 	lbaint_t start, blks;
 	uintptr_t buf_addr;
 	unsigned short smallblks = 0;
@@ -441,9 +443,10 @@
 /* Almost the maximum amount of the scsi_ext command.. */
 #define SCSI_MAX_WRITE_BLK 0xFFFF
 
-static ulong scsi_write(int device, lbaint_t blknr,
+static ulong scsi_write(block_dev_desc_t *block_dev, lbaint_t blknr,
 			lbaint_t blkcnt, const void *buffer)
 {
+	int device = block_dev->dev;
 	lbaint_t start, blks;
 	uintptr_t buf_addr;
 	unsigned short smallblks;
diff --git a/common/cmd_usb.c b/common/cmd_usb.c
index a540b42..c7b642c 100644
--- a/common/cmd_usb.c
+++ b/common/cmd_usb.c
@@ -759,7 +759,7 @@
 			printf("\nUSB read: device %d block # %ld, count %ld"
 				" ... ", usb_stor_curr_dev, blk, cnt);
 			stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
-			n = stor_dev->block_read(usb_stor_curr_dev, blk, cnt,
+			n = stor_dev->block_read(stor_dev, blk, cnt,
 						 (ulong *)addr);
 			printf("%ld blocks read: %s\n", n,
 				(n == cnt) ? "OK" : "ERROR");
@@ -781,7 +781,7 @@
 			printf("\nUSB write: device %d block # %ld, count %ld"
 				" ... ", usb_stor_curr_dev, blk, cnt);
 			stor_dev = usb_stor_get_dev(usb_stor_curr_dev);
-			n = stor_dev->block_write(usb_stor_curr_dev, blk, cnt,
+			n = stor_dev->block_write(stor_dev, blk, cnt,
 						(ulong *)addr);
 			printf("%ld blocks write: %s\n", n,
 				(n == cnt) ? "OK" : "ERROR");
diff --git a/common/cmd_usb_mass_storage.c b/common/cmd_usb_mass_storage.c
index 0407389..f4bafec 100644
--- a/common/cmd_usb_mass_storage.c
+++ b/common/cmd_usb_mass_storage.c
@@ -19,9 +19,8 @@
 {
 	block_dev_desc_t *block_dev = ums_dev->block_dev;
 	lbaint_t blkstart = start + ums_dev->start_sector;
-	int dev_num = block_dev->dev;
 
-	return block_dev->block_read(dev_num, blkstart, blkcnt, buf);
+	return block_dev->block_read(block_dev, blkstart, blkcnt, buf);
 }
 
 static int ums_write_sector(struct ums *ums_dev,
@@ -29,9 +28,8 @@
 {
 	block_dev_desc_t *block_dev = ums_dev->block_dev;
 	lbaint_t blkstart = start + ums_dev->start_sector;
-	int dev_num = block_dev->dev;
 
-	return block_dev->block_write(dev_num, blkstart, blkcnt, buf);
+	return block_dev->block_write(block_dev, blkstart, blkcnt, buf);
 }
 
 static struct ums ums_dev = {
diff --git a/common/env_mmc.c b/common/env_mmc.c
index 9639822..f182749 100644
--- a/common/env_mmc.c
+++ b/common/env_mmc.c
@@ -127,7 +127,7 @@
 	blk_start	= ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
 	blk_cnt		= ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
 
-	n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
+	n = mmc->block_dev.block_write(&mmc->block_dev, blk_start,
 					blk_cnt, (u_char *)buffer);
 
 	return (n == blk_cnt) ? 0 : -1;
@@ -192,16 +192,12 @@
 			   unsigned long offset, const void *buffer)
 {
 	uint blk_start, blk_cnt, n;
-	int dev = CONFIG_SYS_MMC_ENV_DEV;
-
-#ifdef CONFIG_SPL_BUILD
-	dev = 0;
-#endif
 
 	blk_start	= ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
 	blk_cnt		= ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
 
-	n = mmc->block_dev.block_read(dev, blk_start, blk_cnt, (uchar *)buffer);
+	n = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt,
+				      (uchar *)buffer);
 
 	return (n == blk_cnt) ? 0 : -1;
 }
diff --git a/common/fb_mmc.c b/common/fb_mmc.c
index b480e76..6e742da 100644
--- a/common/fb_mmc.c
+++ b/common/fb_mmc.c
@@ -58,7 +58,7 @@
 	block_dev_desc_t *dev_desc = sparse->dev_desc;
 	int ret;
 
-	ret = dev_desc->block_write(dev_desc->dev, offset, size, data);
+	ret = dev_desc->block_write(dev_desc, offset, size, data);
 	if (!ret)
 		return -EIO;
 
@@ -84,8 +84,7 @@
 
 	puts("Flashing Raw Image\n");
 
-	blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
-				     buffer);
+	blks = dev_desc->block_write(dev_desc, info->start, blkcnt, buffer);
 	if (blks != blkcnt) {
 		error("failed writing to device %d\n", dev_desc->dev);
 		fastboot_fail(response_str, "failed writing to device");
@@ -206,7 +205,7 @@
 	printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
 	       blks_start, blks_start + blks_size);
 
-	blks = dev_desc->block_erase(dev_desc->dev, blks_start, blks_size);
+	blks = dev_desc->block_erase(dev_desc, blks_start, blks_size);
 	if (blks != blks_size) {
 		error("failed erasing from device %d", dev_desc->dev);
 		fastboot_fail(response_str, "failed erasing from device");
diff --git a/common/spl/spl_mmc.c b/common/spl/spl_mmc.c
index c1c29c0..c3931c6 100644
--- a/common/spl/spl_mmc.c
+++ b/common/spl/spl_mmc.c
@@ -23,13 +23,12 @@
 	unsigned long count;
 	u32 image_size_sectors;
 	struct image_header *header;
-	int dev_num = mmc->block_dev.dev;
 
 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
 					 sizeof(struct image_header));
 
 	/* read image header to find the image size & load address */
-	count = mmc->block_dev.block_read(dev_num, sector, 1, header);
+	count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
 	debug("read sector %lx, count=%lu\n", sector, count);
 	if (count == 0)
 		goto end;
@@ -46,7 +45,8 @@
 			     mmc->read_bl_len;
 
 	/* Read the header too to avoid extra memcpy */
-	count = mmc->block_dev.block_read(dev_num, sector, image_size_sectors,
+	count = mmc->block_dev.block_read(&mmc->block_dev, sector,
+					  image_size_sectors,
 					  (void *)(ulong)spl_image.load_addr);
 	debug("read %x sectors to %x\n", image_size_sectors,
 	      spl_image.load_addr);
@@ -150,8 +150,7 @@
 {
 	unsigned long count;
 
-	count = mmc->block_dev.block_read(
-		mmc->block_dev.dev,
+	count = mmc->block_dev.block_read(&mmc->block_dev,
 		CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
 		CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
 		(void *) CONFIG_SYS_SPL_ARGS_ADDR);
diff --git a/common/usb_storage.c b/common/usb_storage.c
index e61a8c8..4fdb55f 100644
--- a/common/usb_storage.c
+++ b/common/usb_storage.c
@@ -119,10 +119,10 @@
 		      block_dev_desc_t *dev_desc);
 int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
 		      struct us_data *ss);
-unsigned long usb_stor_read(int device, lbaint_t blknr,
-			    lbaint_t blkcnt, void *buffer);
-unsigned long usb_stor_write(int device, lbaint_t blknr,
-			     lbaint_t blkcnt, const void *buffer);
+static unsigned long usb_stor_read(block_dev_desc_t *block_dev, lbaint_t blknr,
+				   lbaint_t blkcnt, void *buffer);
+static unsigned long usb_stor_write(block_dev_desc_t *block_dev, lbaint_t blknr,
+				    lbaint_t blkcnt, const void *buffer);
 void uhci_show_temp_int_td(void);
 
 #ifdef CONFIG_PARTITIONS
@@ -1027,9 +1027,10 @@
 }
 #endif /* CONFIG_USB_BIN_FIXUP */
 
-unsigned long usb_stor_read(int device, lbaint_t blknr,
-			    lbaint_t blkcnt, void *buffer)
+static unsigned long usb_stor_read(block_dev_desc_t *block_dev, lbaint_t blknr,
+				   lbaint_t blkcnt, void *buffer)
 {
+	int device = block_dev->dev;
 	lbaint_t start, blks;
 	uintptr_t buf_addr;
 	unsigned short smallblks;
@@ -1097,9 +1098,10 @@
 	return blkcnt;
 }
 
-unsigned long usb_stor_write(int device, lbaint_t blknr,
-				lbaint_t blkcnt, const void *buffer)
+static unsigned long usb_stor_write(block_dev_desc_t *block_dev, lbaint_t blknr,
+				    lbaint_t blkcnt, const void *buffer)
 {
+	int device = block_dev->dev;
 	lbaint_t start, blks;
 	uintptr_t buf_addr;
 	unsigned short smallblks;