All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 13/25] scsi-disk: respect the page control (PC) field in the MODE SENSE command
Date: Wed,  8 Sep 2010 15:29:30 +0200	[thread overview]
Message-ID: <1283952582-17498-14-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1283952582-17498-1-git-send-email-kwolf@redhat.com>

From: Bernhard Kohl <bernhard.kohl@nsn.com>

The page control (PC) field defines the type of mode parameter values
to be returned in the mode pages:

PC=0 : Current values
PC=1 : Changeable values
PC=2 : Default values
PC=3 : Saved values

The current implementation always returns the same type of parameters.
This is OK for Current and Default values as we don't support changes
to be done by the MODE SELECT command.

For Saved values the following applies (implemented by this patch):
"A PC field value of 3h requests that the target return the saved
values of the mode parameters. Implementation of saved page parameters
is optional. Mode parameters not supported by the target shall be set
to zero. If saved values are not implemented, the command shall be
terminated with CHECK CONDITION status, the sense key set to
ILLEGAL REQUEST and the additional sense code set to
SAVING PARAMETERS NOT SUPPORTED."

For Changeable values the following applies (implemented by this patch):
"A PC field value of 1h requests that the target return a mask denoting
those mode parameters that are changeable. In the mask, the fields of
the mode parameters that are changeable shall be set to all one bits and
the fields of the mode parameters that are non-changeable (i.e. defined
by the target) shall be set to all zero bits."

In newer versions of the SCSI-2 spec the following clause was added.
"If the logical unit does not implement changeable parameters mode pages
and the device server receives a MODE SENSE command with 01b in the PC
field, then the command shall be terminated with CHECK CONDITION status,
with the sense key set to ILLEGAL REQUEST, and the additional sense code
set to INVALID FIELD IN CDB."

This was not yet included in the SCSI-2 Working Drafts from 1986-1993.
I assume that the variant to return CHECK CONDITION for PC=1 is not
widely implemented by real devices. I have a legacy OS which fails,
if MODE_SENSE returns non GOOD for PC=1. So for highest compatibility I
implemented the former variant with this patch.

The last Working Draft X3T9.2 Rev. 10L 7-SEP-93 can be found here:
http://ldkelley.com/SCSI2/SCSI2/SCSI2-08.html#8.2.10

In mode_sense_page() this patch also avoids multiple hard coded
definitions of the same mode page length. Instead I use the varable
p[1]. In fact the returned length of the mode pages 4 and 5 were wrong
(2 bytes less).

Signed-off-by: Bernhard Kohl <bernhard.kohl@nsn.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 hw/scsi-disk.c |   45 +++++++++++++++++++++++++++++++++++----------
 1 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/hw/scsi-disk.c b/hw/scsi-disk.c
index 3e727b9..1287cab 100644
--- a/hw/scsi-disk.c
+++ b/hw/scsi-disk.c
@@ -486,16 +486,26 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
     return buflen;
 }
 
-static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p)
+static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p,
+                           int page_control)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
     BlockDriverState *bdrv = s->bs;
     int cylinders, heads, secs;
 
+    /*
+     * If Changeable Values are requested, a mask denoting those mode parameters
+     * that are changeable shall be returned. As we currently don't support
+     * parameter changes via MODE_SELECT all bits are returned set to zero.
+     * The buffer was already menset to zero by the caller of this function.
+     */
     switch (page) {
     case 4: /* Rigid disk device geometry page. */
         p[0] = 4;
         p[1] = 0x16;
+        if (page_control == 1) { /* Changeable Values */
+            return p[1] + 2;
+        }
         /* if a geometry hint is available, use it */
         bdrv_get_geometry_hint(bdrv, &cylinders, &heads, &secs);
         p[2] = (cylinders >> 16) & 0xff;
@@ -520,11 +530,14 @@ static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p)
         /* Medium rotation rate [rpm], 5400 rpm */
         p[20] = (5400 >> 8) & 0xff;
         p[21] = 5400 & 0xff;
-        return 0x16;
+        return p[1] + 2;
 
     case 5: /* Flexible disk device geometry page. */
         p[0] = 5;
         p[1] = 0x1e;
+        if (page_control == 1) { /* Changeable Values */
+            return p[1] + 2;
+        }
         /* Transfer rate [kbit/s], 5Mbit/s */
         p[2] = 5000 >> 8;
         p[3] = 5000 & 0xff;
@@ -556,21 +569,27 @@ static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p)
         /* Medium rotation rate [rpm], 5400 rpm */
         p[28] = (5400 >> 8) & 0xff;
         p[29] = 5400 & 0xff;
-        return 0x1e;
+        return p[1] + 2;
 
     case 8: /* Caching page.  */
         p[0] = 8;
         p[1] = 0x12;
+        if (page_control == 1) { /* Changeable Values */
+            return p[1] + 2;
+        }
         if (bdrv_enable_write_cache(s->bs)) {
             p[2] = 4; /* WCE */
         }
-        return 20;
+        return p[1] + 2;
 
     case 0x2a: /* CD Capabilities and Mechanical Status page. */
         if (bdrv_get_type_hint(bdrv) != BDRV_TYPE_CDROM)
             return 0;
         p[0] = 0x2a;
         p[1] = 0x14;
+        if (page_control == 1) { /* Changeable Values */
+            return p[1] + 2;
+        }
         p[2] = 3; // CD-R & CD-RW read
         p[3] = 0; // Writing not supported
         p[4] = 0x7f; /* Audio, composite, digital out,
@@ -594,7 +613,7 @@ static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p)
         p[19] = (16 * 176) & 0xff;
         p[20] = (16 * 176) >> 8; // 16x write speed current
         p[21] = (16 * 176) & 0xff;
-        return 22;
+        return p[1] + 2;
 
     default:
         return 0;
@@ -605,13 +624,15 @@ static int scsi_disk_emulate_mode_sense(SCSIRequest *req, uint8_t *outbuf)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
     uint64_t nb_sectors;
-    int page, dbd, buflen;
+    int page, dbd, buflen, page_control;
     uint8_t *p;
     uint8_t dev_specific_param;
 
     dbd = req->cmd.buf[1]  & 0x8;
     page = req->cmd.buf[2] & 0x3f;
-    DPRINTF("Mode Sense (page %d, len %zd)\n", page, req->cmd.xfer);
+    page_control = (req->cmd.buf[2] & 0xc0) >> 6;
+    DPRINTF("Mode Sense(%d) (page %d, len %d, page_control %d)\n",
+        (req->cmd.buf[0] == MODE_SENSE) ? 6 : 10, page, len, page_control);
     memset(outbuf, 0, req->cmd.xfer);
     p = outbuf;
 
@@ -655,16 +676,20 @@ static int scsi_disk_emulate_mode_sense(SCSIRequest *req, uint8_t *outbuf)
         p += 8;
     }
 
+    if (page_control == 3) { /* Saved Values */
+        return -1; /* ILLEGAL_REQUEST */
+    }
+
     switch (page) {
     case 0x04:
     case 0x05:
     case 0x08:
     case 0x2a:
-        p += mode_sense_page(req, page, p);
+        p += mode_sense_page(req, page, p, page_control);
         break;
     case 0x3f:
-        p += mode_sense_page(req, 0x08, p);
-        p += mode_sense_page(req, 0x2a, p);
+        p += mode_sense_page(req, 0x08, p, page_control);
+        p += mode_sense_page(req, 0x2a, p, page_control);
         break;
     }
 
-- 
1.7.2.2

  parent reply	other threads:[~2010-09-08 13:50 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-08 13:29 [Qemu-devel] [PULL v2 00/25] Block patches Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 01/25] virtio: Factor virtqueue_map_sg out Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 02/25] virtio-blk: Fix migration of queued requests Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 03/25] block: Fix image re-open in bdrv_commit Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 04/25] sheepdog: remove unnecessary includes Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 05/25] qemu-img rebase: Open new backing file read-only Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 06/25] vvfat: fat_chksum(): fix access above array bounds Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 07/25] nbd: Introduce NBD named exports Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 08/25] posix-aio-compat: Fix async_conmtext for ioctl Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 09/25] monitor: make 'info snapshots' show only fully available snapshots Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 10/25] savevm: Generate a name when run without one Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 11/25] scsi-disk: fix the mode data length field returned by the MODE SENSE command Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 12/25] scsi-disk: fix the mode data header returned by the MODE SENSE(10) command Kevin Wolf
2010-09-08 13:29 ` Kevin Wolf [this message]
2010-09-08 13:29 ` [Qemu-devel] [PATCH 14/25] scsi-disk: fix the block descriptor returned by the MODE SENSE command Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 15/25] scsi-disk: return CHECK CONDITION for unknown page codes in " Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 16/25] scsi-disk: fix the check of the DBD bit " Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 17/25] scsi: fix and improve debug prints Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 18/25] qemu-io: Make alloc output useful when nb_sectors=1 Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 19/25] raw-posix: Don't use file name for host_cdrom detection on Linux Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 20/25] Improve ATA IDENTIFY word 64 contents Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 21/25] scsi-disk: add some optional scsi commands Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 22/25] raw-posix: improve detection of scsi-generic devices Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 23/25] qemu-img convert: Use cache=unsafe for output image Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 24/25] block: Fix BDRV_O_CACHE_MASK Kevin Wolf
2010-09-08 13:29 ` [Qemu-devel] [PATCH 25/25] qcow2: Remove unnecessary flush after L2 write Kevin Wolf
2010-09-08 19:30 ` [Qemu-devel] [PULL v2 00/25] Block patches Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1283952582-17498-14-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=anthony@codemonkey.ws \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.