-// Request SENSE
-static int
-atapi_get_sense(struct disk_op_s *op, u8 *asc, u8 *ascq)
-{
- struct cdb_request_sense cmd;
- struct cdbres_request_sense data;
- memset(&cmd, 0, sizeof(cmd));
- cmd.command = CDB_CMD_REQUEST_SENSE;
- cmd.length = sizeof(data);
- op->count = 1;
- op->buf_fl = &data;
- int ret = atapi_cmd_data(op, &cmd, sizeof(data));
- if (ret)
- return ret;
-
- *asc = data.asc;
- *ascq = data.ascq;
- return 0;
-}
-
-// Request capacity
-static int
-atapi_read_capacity(struct disk_op_s *op, u32 *blksize, u32 *sectors)
-{
- struct cdb_read_capacity cmd;
- struct cdbres_read_capacity data;
- memset(&cmd, 0, sizeof(cmd));
- cmd.command = CDB_CMD_READ_CAPACITY;
- op->count = 1;
- op->buf_fl = &data;
- int ret = atapi_cmd_data(op, &cmd, sizeof(data));
- if (ret)
- return ret;
-
- *blksize = ntohl(data.blksize);
- *sectors = ntohl(data.sectors);
- return 0;
-}
-
-static int
-atapi_is_ready(struct disk_op_s *op)
-{
- dprintf(6, "atapi_is_ready (drive=%p)\n", op->drive_g);
-
- /* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT is
- * reported by the device. If the device reports "IN PROGRESS",
- * 30 seconds is added. */
- u32 blksize, sectors;
- int in_progress = 0;
- u64 end = calc_future_tsc(5000);
- for (;;) {
- if (check_time(end)) {
- dprintf(1, "read capacity failed\n");
- return -1;
- }
-
- int ret = atapi_read_capacity(op, &blksize, §ors);
- if (!ret)
- // Success
- break;
-
- u8 asc, ascq;
- ret = atapi_get_sense(op, &asc, &ascq);
- if (ret)
- // Error - retry.
- continue;
-
- // Sense succeeded.
- if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
- dprintf(1, "Device reports MEDIUM NOT PRESENT\n");
- return -1;
- }
-
- if (asc == 0x04 && ascq == 0x01 && !in_progress) {
- /* IN PROGRESS OF BECOMING READY */
- printf("Waiting for device to detect medium... ");
- /* Allow 30 seconds more */
- end = calc_future_tsc(30000);
- in_progress = 1;
- }
- }
-
- if (blksize != GET_GLOBAL(op->drive_g->blksize)) {
- printf("Unsupported sector size %u\n", blksize);
- return -1;
- }
-
- dprintf(6, "sectors=%u\n", sectors);
- printf("%dMB medium detected\n", sectors>>(20-11));
- return 0;
-}
-