Index: uspace/srv/bd/ata_bd/ata_bd.c
===================================================================
--- uspace/srv/bd/ata_bd/ata_bd.c	(revision 135486df91381258aa51c130b7b463fc4ff5e0a8)
+++ uspace/srv/bd/ata_bd/ata_bd.c	(revision beb93360b4fa19e27b1668938b25de12f07de3f6)
@@ -114,17 +114,17 @@
 static int ata_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
 
-static int ata_rcmd_read(int disk_id, uint64_t ba, size_t cnt,
+static int ata_rcmd_read(disk_t *disk, uint64_t ba, size_t cnt,
     void *buf);
-static int ata_rcmd_write(int disk_id, uint64_t ba, size_t cnt,
+static int ata_rcmd_write(disk_t *disk, uint64_t ba, size_t cnt,
     const void *buf);
 static int disk_init(disk_t *d, int disk_id);
-static int drive_identify(int drive_id, void *buf);
-static int identify_pkt_dev(int dev_idx, void *buf);
-static int ata_cmd_packet(int dev_idx, const void *cpkt, size_t cpkt_size,
+static int drive_identify(disk_t *disk, void *buf);
+static int identify_pkt_dev(disk_t *disk, void *buf);
+static int ata_cmd_packet(disk_t *disk, const void *cpkt, size_t cpkt_size,
     void *obuf, size_t obuf_size);
-static int ata_pcmd_inquiry(int dev_idx, void *obuf, size_t obuf_size);
-static int ata_pcmd_read_12(int dev_idx, uint64_t ba, size_t cnt,
+static int ata_pcmd_inquiry(disk_t *disk, void *obuf, size_t obuf_size);
+static int ata_pcmd_read_12(disk_t *disk, uint64_t ba, size_t cnt,
     void *obuf, size_t obuf_size);
-static int ata_pcmd_read_toc(int dev_idx, uint8_t ses,
+static int ata_pcmd_read_toc(disk_t *disk, uint8_t ses,
     void *obuf, size_t obuf_size);
 static void disk_print_summary(disk_t *d);
@@ -147,4 +147,9 @@
 {
 	return (disk_t *)bd->srvs->sarg;
+}
+
+static int disk_dev_idx(disk_t *disk)
+{
+	return (disk->disk_id & 1);
 }
 
@@ -296,5 +301,6 @@
 {
 	service_id_t dsid;
-	int disk_id, i;
+	int i;
+	disk_t *disk;
 
 	/* Get the device service ID. */
@@ -302,15 +308,15 @@
 
 	/* Determine which disk device is the client connecting to. */
-	disk_id = -1;
+	disk = NULL;
 	for (i = 0; i < MAX_DISKS; i++)
 		if (ata_disk[i].service_id == dsid)
-			disk_id = i;
-
-	if (disk_id < 0 || ata_disk[disk_id].present == false) {
+			disk = &ata_disk[i];
+
+	if (disk == NULL || disk->present == false) {
 		async_answer_0(iid, EINVAL);
 		return;
 	}
 
-	bd_conn(iid, icall, &ata_disk[disk_id].bds);
+	bd_conn(iid, icall, &disk->bds);
 }
 
@@ -341,5 +347,5 @@
 
 	/* Try identify command. */
-	rc = drive_identify(disk_id, &idata);
+	rc = drive_identify(d, &idata);
 	if (rc == EOK) {
 		/* Success. It's a register (non-packet) device. */
@@ -361,5 +367,5 @@
 
 		if (bc == PDEV_SIGNATURE_BC) {
-			rc = identify_pkt_dev(disk_id, &idata);
+			rc = identify_pkt_dev(d, &idata);
 			if (rc == EOK) {
 				/* We have a packet device. */
@@ -445,5 +451,5 @@
 	if (d->dev_type == ata_pkt_dev) {
 		/* Send inquiry. */
-		rc = ata_pcmd_inquiry(0, &inq_data, sizeof(inq_data));
+		rc = ata_pcmd_inquiry(d, &inq_data, sizeof(inq_data));
 		if (rc != EOK) {
 			printf("Device inquiry failed.\n");
@@ -488,9 +494,10 @@
 
 	while (cnt > 0) {
-		if (disk->dev_type == ata_reg_dev)
-			rc = ata_rcmd_read(disk->disk_id, ba, 1, buf);
-		else
-			rc = ata_pcmd_read_12(disk->disk_id, ba, 1, buf,
+		if (disk->dev_type == ata_reg_dev) {
+			rc = ata_rcmd_read(disk, ba, 1, buf);
+		} else {
+			rc = ata_pcmd_read_12(disk, ba, 1, buf,
 			    disk->block_size);
+		}
 
 		if (rc != EOK)
@@ -510,5 +517,5 @@
 	disk_t *disk = bd_srv_disk(bd);
 
-	return ata_pcmd_read_toc(disk->disk_id, session, buf, size);
+	return ata_pcmd_read_toc(disk, session, buf, size);
 }
 
@@ -527,5 +534,5 @@
 
 	while (cnt > 0) {
-		rc = ata_rcmd_write(disk->disk_id, ba, 1, buf);
+		rc = ata_rcmd_write(disk, ba, 1, buf);
 		if (rc != EOK)
 			return rc;
@@ -562,5 +569,5 @@
  * whether an ATA device is present and if so, to determine its parameters.
  *
- * @param disk_id	Device ID, 0 or 1.
+ * @param disk		Disk
  * @param buf		Pointer to a 512-byte buffer.
  *
@@ -568,5 +575,5 @@
  *			not present). EIO if device responds with error.
  */
-static int drive_identify(int disk_id, void *buf)
+static int drive_identify(disk_t *disk, void *buf)
 {
 	uint16_t data;
@@ -575,5 +582,5 @@
 	size_t i;
 
-	drv_head = ((disk_id != 0) ? DHR_DRV : 0);
+	drv_head = ((disk_dev_idx(disk) != 0) ? DHR_DRV : 0);
 
 	if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
@@ -621,8 +628,8 @@
  * whether an ATAPI device is present and if so, to determine its parameters.
  *
- * @param dev_idx	Device index, 0 or 1.
+ * @param disk		Disk
  * @param buf		Pointer to a 512-byte buffer.
  */
-static int identify_pkt_dev(int dev_idx, void *buf)
+static int identify_pkt_dev(disk_t *disk, void *buf)
 {
 	uint16_t data;
@@ -631,5 +638,5 @@
 	size_t i;
 
-	drv_head = ((dev_idx != 0) ? DHR_DRV : 0);
+	drv_head = ((disk_dev_idx(disk) != 0) ? DHR_DRV : 0);
 
 	if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
@@ -666,5 +673,5 @@
  * Only data-in commands are supported (e.g. inquiry, read).
  *
- * @param dev_idx	Device index (0 or 1)
+ * @param disk		Disk
  * @param obuf		Buffer for storing data read from device
  * @param obuf_size	Size of obuf in bytes
@@ -672,5 +679,5 @@
  * @return EOK on success, EIO on error.
  */
-static int ata_cmd_packet(int dev_idx, const void *cpkt, size_t cpkt_size,
+static int ata_cmd_packet(disk_t *disk, const void *cpkt, size_t cpkt_size,
     void *obuf, size_t obuf_size)
 {
@@ -678,17 +685,15 @@
 	uint8_t status;
 	uint8_t drv_head;
-	disk_t *d;
 	size_t data_size;
 	uint16_t val;
 
-	d = &ata_disk[dev_idx];
-	fibril_mutex_lock(&d->lock);
+	fibril_mutex_lock(&disk->lock);
 
 	/* New value for Drive/Head register */
 	drv_head =
-	    ((dev_idx != 0) ? DHR_DRV : 0);
+	    ((disk_dev_idx(disk) != 0) ? DHR_DRV : 0);
 
 	if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -697,5 +702,5 @@
 
 	if (wait_status(0, ~(SR_BSY|SR_DRQ), NULL, TIMEOUT_BSY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -708,5 +713,5 @@
 
 	if (wait_status(SR_DRQ, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -717,10 +722,10 @@
 
 	if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
 
 	if ((status & SR_DRQ) == 0) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -733,5 +738,5 @@
 	if (data_size > obuf_size) {
 		/* Output buffer is too small to store data. */
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -744,9 +749,9 @@
 
 	if (status & SR_ERR) {
-		fibril_mutex_unlock(&d->lock);
-		return EIO;
-	}
-
-	fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
+		return EIO;
+	}
+
+	fibril_mutex_unlock(&disk->lock);
 
 	return EOK;
@@ -755,5 +760,5 @@
 /** Issue ATAPI Inquiry.
  *
- * @param dev_idx	Device index (0 or 1)
+ * @param disk		Disk
  * @param obuf		Buffer for storing inquiry data read from device
  * @param obuf_size	Size of obuf in bytes
@@ -761,5 +766,5 @@
  * @return EOK on success, EIO on error.
  */
-static int ata_pcmd_inquiry(int dev_idx, void *obuf, size_t obuf_size)
+static int ata_pcmd_inquiry(disk_t *disk, void *obuf, size_t obuf_size)
 {
 	ata_pcmd_inquiry_t cp;
@@ -771,5 +776,5 @@
 	cp.alloc_len = min(obuf_size, 0xff); /* Allocation length */
 
-	rc = ata_cmd_packet(0, &cp, sizeof(cp), obuf, obuf_size);
+	rc = ata_cmd_packet(disk, &cp, sizeof(cp), obuf, obuf_size);
 	if (rc != EOK)
 		return rc;
@@ -783,5 +788,5 @@
  * function will fail.
  *
- * @param dev_idx	Device index (0 or 1)
+ * @param disk		Disk
  * @param ba		Starting block address
  * @param cnt		Number of blocks to read
@@ -791,5 +796,5 @@
  * @return EOK on success, EIO on error.
  */
-static int ata_pcmd_read_12(int dev_idx, uint64_t ba, size_t cnt,
+static int ata_pcmd_read_12(disk_t *disk, uint64_t ba, size_t cnt,
     void *obuf, size_t obuf_size)
 {
@@ -806,5 +811,5 @@
 	cp.nblocks = host2uint32_t_be(cnt);
 
-	rc = ata_cmd_packet(0, &cp, sizeof(cp), obuf, obuf_size);
+	rc = ata_cmd_packet(disk, &cp, sizeof(cp), obuf, obuf_size);
 	if (rc != EOK)
 		return rc;
@@ -823,5 +828,5 @@
  * function will fail.
  *
- * @param dev_idx	Device index (0 or 1)
+ * @param disk		Disk
  * @param session	Starting session
  * @param obuf		Buffer for storing inquiry data read from device
@@ -830,5 +835,5 @@
  * @return EOK on success, EIO on error.
  */
-static int ata_pcmd_read_toc(int dev_idx, uint8_t session, void *obuf,
+static int ata_pcmd_read_toc(disk_t *disk, uint8_t session, void *obuf,
     size_t obuf_size)
 {
@@ -854,5 +859,5 @@
 /** Read a physical from the device.
  *
- * @param disk_id	Device index (0 or 1)
+ * @param disk		Disk
  * @param ba		Address the first block.
  * @param cnt		Number of blocks to transfer.
@@ -861,5 +866,5 @@
  * @return EOK on success, EIO on error.
  */
-static int ata_rcmd_read(int disk_id, uint64_t ba, size_t blk_cnt,
+static int ata_rcmd_read(disk_t *disk, uint64_t ba, size_t blk_cnt,
     void *buf)
 {
@@ -868,28 +873,25 @@
 	uint8_t status;
 	uint8_t drv_head;
-	disk_t *d;
 	block_coord_t bc;
 
-	d = &ata_disk[disk_id];
-	
 	/* Silence warning. */
 	memset(&bc, 0, sizeof(bc));
 
 	/* Compute block coordinates. */
-	if (coord_calc(d, ba, &bc) != EOK)
+	if (coord_calc(disk, ba, &bc) != EOK)
 		return EINVAL;
 
 	/* New value for Drive/Head register */
 	drv_head =
-	    ((disk_id != 0) ? DHR_DRV : 0) |
-	    ((d->amode != am_chs) ? DHR_LBA : 0) |
+	    ((disk_dev_idx(disk) != 0) ? DHR_DRV : 0) |
+	    ((disk->amode != am_chs) ? DHR_LBA : 0) |
 	    (bc.h & 0x0f);
 
-	fibril_mutex_lock(&d->lock);
+	fibril_mutex_lock(&disk->lock);
 
 	/* Program a Read Sectors operation. */
 
 	if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -898,5 +900,5 @@
 
 	if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -905,9 +907,9 @@
 	coord_sc_program(&bc, 1);
 
-	pio_write_8(&cmd->command, d->amode == am_lba48 ?
+	pio_write_8(&cmd->command, disk->amode == am_lba48 ?
 	    CMD_READ_SECTORS_EXT : CMD_READ_SECTORS);
 
 	if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -916,5 +918,5 @@
 		/* Read data from the device buffer. */
 
-		for (i = 0; i < ata_disk[disk_id].block_size / 2; i++) {
+		for (i = 0; i < disk->block_size / 2; i++) {
 			data = pio_read_16(&cmd->data_port);
 			((uint16_t *) buf)[i] = data;
@@ -925,5 +927,5 @@
 		return EIO;
 
-	fibril_mutex_unlock(&d->lock);
+	fibril_mutex_unlock(&disk->lock);
 	return EOK;
 }
@@ -931,5 +933,5 @@
 /** Write a physical block to the device.
  *
- * @param disk_id	Device index (0 or 1)
+ * @param disk		Disk
  * @param ba		Address of the first block.
  * @param cnt		Number of blocks to transfer.
@@ -938,5 +940,5 @@
  * @return EOK on success, EIO on error.
  */
-static int ata_rcmd_write(int disk_id, uint64_t ba, size_t cnt,
+static int ata_rcmd_write(disk_t *disk, uint64_t ba, size_t cnt,
     const void *buf)
 {
@@ -944,28 +946,25 @@
 	uint8_t status;
 	uint8_t drv_head;
-	disk_t *d;
 	block_coord_t bc;
 
-	d = &ata_disk[disk_id];
-	
 	/* Silence warning. */
 	memset(&bc, 0, sizeof(bc));
 
 	/* Compute block coordinates. */
-	if (coord_calc(d, ba, &bc) != EOK)
+	if (coord_calc(disk, ba, &bc) != EOK)
 		return EINVAL;
 
 	/* New value for Drive/Head register */
 	drv_head =
-	    ((disk_id != 0) ? DHR_DRV : 0) |
-	    ((d->amode != am_chs) ? DHR_LBA : 0) |
+	    ((disk_dev_idx(disk) != 0) ? DHR_DRV : 0) |
+	    ((disk->amode != am_chs) ? DHR_LBA : 0) |
 	    (bc.h & 0x0f);
 
-	fibril_mutex_lock(&d->lock);
+	fibril_mutex_lock(&disk->lock);
 
 	/* Program a Write Sectors operation. */
 
 	if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -974,5 +973,5 @@
 
 	if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -981,9 +980,9 @@
 	coord_sc_program(&bc, 1);
 
-	pio_write_8(&cmd->command, d->amode == am_lba48 ?
+	pio_write_8(&cmd->command, disk->amode == am_lba48 ?
 	    CMD_WRITE_SECTORS_EXT : CMD_WRITE_SECTORS);
 
 	if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
-		fibril_mutex_unlock(&d->lock);
+		fibril_mutex_unlock(&disk->lock);
 		return EIO;
 	}
@@ -992,10 +991,10 @@
 		/* Write data to the device buffer. */
 
-		for (i = 0; i < d->block_size / 2; i++) {
+		for (i = 0; i < disk->block_size / 2; i++) {
 			pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
 		}
 	}
 
-	fibril_mutex_unlock(&d->lock);
+	fibril_mutex_unlock(&disk->lock);
 
 	if (status & SR_ERR)
