Skip to contentSkip to conversation

exclude certain commands

PATCH [email protected] 7 participants 17 messages Apr 26, 2003 – Apr 28, 2003 SCSI

lore
Share & tools
lore
#0 lore raw

[[problem solved, we understand the hardware, it seems, now wish to find an elegant way of doing what is required (and only what is required) in the scsi and/or usb storage code]]

(1) usb-storage is broken in the sense that it uses sr_bufflen for the transfer size instead of the buffer length, then fudges commands to make them transfer a different length, but without updating sr_bufflen.

(1A)
Of course it is bad when variables have a function that differs from what is suggested by their name. So, sr_bufflen must be the length of the buffer and nothing else. If the length of the transfer is needed, there must be a field sr_xferlen or so.

(1B)
Of course it is bad when usb-storage fudges commands.

(2) Also other parts of the kernel do this translation, so there is a lot of duplication; see for example the idescsi_transform code in ide-scsi.c.

Lazy as I am I had hoped that I would find the right setup in my mailbox. But upon returning I see essentially two replies:

(a) Matt tells about sr_bufflen.
(b) Alan says that changing must be avoided when possible. Of course I agree. (By the way, he says among other things "The SCSI midlayer could try multiple forms of MODE_SENSE", but that happens already.)

So I made a patch, but will only describe it since it is long, and I am not quite happy yet, as you will see below.

The first part, in drivers/usb/storage/scsiglue.c is very satisfactory:

-
-#define USB_STOR_SCSI_SENSE_HDRSZ 4
-#define USB_STOR_SCSI_SENSE_10_HDRSZ 8
-
-struct usb_stor_scsi_sense_hdr
-{
- __u8* dataLength;
- __u8* mediumType;
...
- }
- }
-
- /* Set value of length passed in */
- *length_p = length;
-}
-

(496 lines deleted). That is a good start.
If we do not fudge commands in usb-storage, then a lot of fudging code can go.

The next part, in drivers/usb/storage/protocol.c, is almost as satisfactory:

...
- /* determine the correct (or minimum) data length for these commands */
- switch (srb->cmnd[0]) {
-
- /* change MODE_SENSE/MODE_SELECT from 6 to 10 byte commands */
- case MODE_SENSE:
- case MODE_SELECT:
- /* save the command so we can tell what it was */
- old_cmnd = srb->cmnd[0];
...
- /* change READ_6/WRITE_6 to READ_10/WRITE_10, which
- * are ATAPI commands */
- case WRITE_6:
- case READ_6:
- srb->cmnd[11] = 0;
- srb->cmnd[10] = 0;
...

(deleted some 200 lines changing READ_6 into READ_10, etc.)

These lines can be deleted because the SCSI layer does not send such commands. (What about sg you ask? I don't care. People who send commands "by hand" are responsible themselves. Moreover, it is really bad when these handcrafted commands are changed by the driver - probably they were intended precisely as written.)

Why doesnt SCSI send READ_6/WRITE_6? Because there is a field "ten" that is initially 1 and says that READ_10 must be used. It will be cleared only when the device returns ILLEGAL_REQUEST.

I dislike the choice of identifier "ten" - it is difficult to grep for, and I changed it into use_10_for_rw, with the same function as before. Added a field use_10_for_ms. Thus, in scsi.h:

+ unsigned use_10_for_rw:1; /* first try 10-byte read / write */
+ unsigned use_10_for_ms:1; /* first try 10-byte mode sense/select */

in the declaration of struct scsi_device.

Now sd_do_mode_sense6() becomes sd_do_mode_sense() and does

+ if (SRpnt->sr_device->use_10_for_ms) {
+ if (len < 8)
+ len = 8;
+
+ cmd[0] = MODE_SENSE_10;
+ cmd[8] = len;
+ } else {
+ if (len < 4)
+ len = 4;
+
+ cmd[0] = MODE_SENSE;
+ cmd[4] = len;
+ }

(and its first parameter can go).

All this is nice and well. Remains the question how usb-storage makes sure that the use_10_for_rw and use_10_for_ms flags are set for its devices.

Tonight I kludged this by setting this (in protocol.c) at the moment the first INQUIRY is done. But that is terribly ugly.
Did I overlook some obvious means of communication?

Comments?

Andries

#1 lore raw
Quoted 4 lines · On Sat, Apr 26, 2003 at 11:44:10PM +0200, [email protected] wrote:

(1) usb-storage is broken in the sense that it uses sr_bufflen for the transfer size instead of the buffer length, then fudges commands to make them transfer a different length, but without updating sr_bufflen.

I would argue with the term 'broken', especially after having discussed this publically and having been told that using sr_bufflen this way was not only apropriate but recommended.

But that's water under the bridge.

Quoted 5 lines

(1A)
Of course it is bad when variables have a function that differs from what is suggested by their name. So, sr_bufflen must be the length of the buffer and nothing else. If the length of the transfer is needed, there must be a field sr_xferlen or so.

Sounds good. It also sounds very similar to what I proposed some time ago. Here's a question from that discussion: How you you set sr_xferlen? I'm guessing that you want to add a parameters to scsi_wait_req()? Or will you simply remove the bufflen parameter and force callers to set both fields in the struct scsi_request?

Either way, you'll be touching a great deal of code. Which isn't necessarily a bad thing, but it might raise a few eyebrows....

Quoted 2 lines

(1B)
Of course it is bad when usb-storage fudges commands.

Agreed. I don't think you'll find anyone who would argue this point.

Quoted 3 lines

(2) Also other parts of the kernel do this translation, so there is a lot of duplication; see for example the idescsi_transform code in ide-scsi.c.

Yes, there are. That's bothered me too.... especially when programs like cdrecord use a special IOCTL to turn that translation on-and-off.

Quoted 2 lines

Lazy as I am I had hoped that I would find the right setup in my mailbox. But upon returning I see essentially two replies:

Hey, I've got about a 0% success rate with patches so far, so I'm a bit hesitant. Besides, I'm still waiting for the EVPD-removal patch to get accepted by Linus.

Quoted 4 lines

(b) Alan says that changing must be avoided when possible. Of course I agree. (By the way, he says among other things "The SCSI midlayer could try multiple forms of MODE_SENSE", but that happens already.)

It does? I know it tries MODE_SENSE for cache-data... but I think Alan was referring to WP-detect. Maybe I just missed something.

Quoted 9 lines

So I made a patch, but will only describe it since it is long, and I am not quite happy yet, as you will see below.

The first part, in drivers/usb/storage/scsiglue.c is very satisfactory:

(496 lines deleted). That is a good start.
If we do not fudge commands in usb-storage, then a lot of fudging code can go.

Very true. If you can say that sd.c and SCSI core won't send MODE_SENSE, all this can go.

Quoted 4 lines

The next part, in drivers/usb/storage/protocol.c, is almost as satisfactory:

(deleted some 200 lines changing READ_6 into READ_10, etc.)

We need to be careful here... there are several different flavors of comamnd-protocol used by usb-storage, and we need to double-check things like UFI before we rip everything out. I think it can all go, but we just need to make sure (and I'd want to see the actual patch before doing that check).

Quoted 5 lines

These lines can be deleted because the SCSI layer does not send such commands. (What about sg you ask? I don't care. People who send commands "by hand" are responsible themselves. Moreover, it is really bad when these handcrafted commands are changed by the driver - probably they were intended precisely as written.)

Agreed.

Quoted 11 lines

Why doesnt SCSI send READ_6/WRITE_6? Because there is a field "ten" that is initially 1 and says that READ_10 must be used. It will be cleared only when the device returns ILLEGAL_REQUEST.

I dislike the choice of identifier "ten" - it is difficult to grep for, and I changed it into use_10_for_rw, with the same function as before. Added a field use_10_for_ms. Thus, in scsi.h:

(quoted diff) +2 -0
+       unsigned use_10_for_rw:1; /* first try 10-byte read / write */
+       unsigned use_10_for_ms:1; /* first try 10-byte mode sense/select */
Quoted 3 lines

All this is nice and well. Remains the question how usb-storage makes sure that the use_10_for_rw and use_10_for_ms flags are set for its devices.

Why not just make us_10_for_ms default to 1, just like use_10_for_rw? The same logic (fallback if ILLEGAL_REQUEST) could apply to both.

Quoted 3 lines

Tonight I kludged this by setting this (in protocol.c) at the moment the first INQUIRY is done. But that is terribly ugly.
Did I overlook some obvious means of communication?

Not that I'm aware of.

Matt

Signature · 6 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

What, are you one of those Microsoft-bashing Linux freaks?
					-- Customer to Greg
User Friendly, 2/10/1999
#2 lore raw
Quoted 5 lines · Matthew Dharm
Quoted 4 lines

Of course it is bad when variables have a function that differs from what is suggested by their name. So, sr_bufflen must be the length of the buffer and nothing else. If the length of the transfer is needed, there must be a field sr_xferlen or so.

Quoted 8 lines

Sounds good. It also sounds very similar to what I proposed some time ago. Here's a question from that discussion: How you you set sr_xferlen? I'm guessing that you want to add a parameters to scsi_wait_req()? Or will you simply remove the bufflen parameter and force callers to set both fields in the struct scsi_request?

Either way, you'll be touching a great deal of code. Which isn't necessarily a bad thing, but it might raise a few eyebrows....

What's actually wrong with simply parsing the commands? For all SCSI Spec commands there's a well known algorithm to get the transfer size from the command group. For vendor specific commands, well the mid-layer doesn't issue them so you'd just have to rely on the user doing it to pass the correct buffer length (which is no different really from what happens today).

James

#3 lore raw
Quoted 15 lines · James Bottomley
Quoted 5 lines · Matthew Dharm
Quoted 4 lines

Of course it is bad when variables have a function that differs from what is suggested by their name. So, sr_bufflen must be the length of the buffer and nothing else. If the length of the transfer is needed, there must be a field sr_xferlen or so.

Quoted 8 lines

Sounds good. It also sounds very similar to what I proposed some time ago. Here's a question from that discussion: How you you set sr_xferlen? I'm guessing that you want to add a parameters to scsi_wait_req()? Or will you simply remove the bufflen parameter and force callers to set both fields in the struct scsi_request?

Either way, you'll be touching a great deal of code. Which isn't necessarily a bad thing, but it might raise a few eyebrows....

Quoted 6 lines

What's actually wrong with simply parsing the commands? For all SCSI Spec commands there's a well known algorithm to get the transfer size from the command group. For vendor specific commands, well the mid-layer doesn't issue them so you'd just have to rely on the user doing it to pass the correct buffer length (which is no different really from what happens today).

To parse correctly, I would need to remember what type of device it is by snooping the INQUIRY data. I would also have to keep around data such as block size (for TYPE_DISK). I haven't even looked at what extra data I would need to keep around for all the other types.

Why should usb-storage do that when someone else (the command source) already knows this data? Whatever is sending the command already knows how much data to expect -- my reparsing the command to try to figure it out is guaranteed to be less precise than just using the correct answer from the source of the command.

Also, user-issued commands can specify buffer size and transfer length separately, but only one value gets given to the usb-storage driver. The other is dropped on the floor somewhere in the mid-layer.

In the end, I guess the short-answer to 'what is wrong with parsing' is that we tried it for over a year, and it kept biting us on the ass with weird corner-cases and strange devices.

Matt

Signature · 6 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

We can customize our colonels.
					-- Tux
User Friendly, 12/1/1998
#4 lore raw
Quoted 4 lines · Matthew Dharm

To parse correctly, I would need to remember what type of device it is by snooping the INQUIRY data. I would also have to keep around data such as block size (for TYPE_DISK). I haven't even looked at what extra data I would need to keep around for all the other types.

OK, I don't understand this. To extract the transfer length from a given SCSI command (excluding vendor specific ones) is a well defined process. All you need is the command, it doesn't require knowing device type or anything else.

Quoted 5 lines

Why should usb-storage do that when someone else (the command source) already knows this data? Whatever is sending the command already knows how much data to expect -- my reparsing the command to try to figure it out is guaranteed to be less precise than just using the correct answer from the source of the command.

Because we're exploring options for this "lengthen the transfer size" behaviour the USB layer exhibits. Assuming you can transfer more bytes than the indicated buffer size is a recipe for data corruption problems down the line.

Quoted 3 lines

Also, user-issued commands can specify buffer size and transfer length separately, but only one value gets given to the usb-storage driver. The other is dropped on the floor somewhere in the mid-layer.

I usually use the SCSI_IOCTL_SEND_COMMAND, which only allows the specification of a buffer size (rather than both length and buffer size), how do you send SCSI commands?

Quoted 3 lines

In the end, I guess the short-answer to 'what is wrong with parsing' is that we tried it for over a year, and it kept biting us on the ass with weird corner-cases and strange devices.

Well, the only weird corner cases are vendor specific commands and for these the rule is that we assume the buffer size and the transfer size to be the same. Since they aren't currently issued by the mid-layer this must be true today (otherwise they wouldn't work today), so I think we have everything covered.

James

#5 lore raw
Quoted 10 lines · James Bottomley
Quoted 4 lines · Matthew Dharm

To parse correctly, I would need to remember what type of device it is by snooping the INQUIRY data. I would also have to keep around data such as block size (for TYPE_DISK). I haven't even looked at what extra data I would need to keep around for all the other types.

OK, I don't understand this. To extract the transfer length from a given SCSI command (excluding vendor specific ones) is a well defined process. All you need is the command, it doesn't require knowing device type or anything else.

Okay. A READ_10 command for 4 blocks. How many bytes is that? The command bytes in question look something like this:

	0x28 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x4 0x0

I don't see a way to know the byte count without having additional knowledge about the device. Maybe I'm wrong -- I certainly don't claim to be a SCSI expert. So explain it to me.

Quoted 10 lines
Quoted 5 lines

Why should usb-storage do that when someone else (the command source) already knows this data? Whatever is sending the command already knows how much data to expect -- my reparsing the command to try to figure it out is guaranteed to be less precise than just using the correct answer from the source of the command.

Because we're exploring options for this "lengthen the transfer size" behaviour the USB layer exhibits. Assuming you can transfer more bytes than the indicated buffer size is a recipe for data corruption problems down the line.

My point in asking the question was to emphasize that I do not believe the transfer length to be calculable (only guessable) -- why guess when some bit of code has to know?

The problem if transferring more data than the buffer has room for is an obvious problem -- that I don't debate.

Quoted 7 lines
Quoted 3 lines

Also, user-issued commands can specify buffer size and transfer length separately, but only one value gets given to the usb-storage driver. The other is dropped on the floor somewhere in the mid-layer.

I usually use the SCSI_IOCTL_SEND_COMMAND, which only allows the specification of a buffer size (rather than both length and buffer size), how do you send SCSI commands?

Okay, I'm wrong here. I would have sworn that the SG3 interface allowed me to specify the two values separately -- I have quite a bit of code that uses that interface.

Interestinly enough, the one value it specifies it defines as the length of the transfer, not the size of the buffer -- at least, according to the sg HOWTO.

Matt

Signature · 6 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

Hey, has anyone seen the Microsoft sales guy? It's his feeding time...
					-- Mike
User Friendly, 4/17/1998
#6 lore raw
Quoted 3 lines · Matthew Dharm

I don't see a way to know the byte count without having additional knowledge about the device. Maybe I'm wrong -- I certainly don't claim to be a SCSI expert. So explain it to me.

Certainly. It's documented in the SCSI3 Primary commands (for reference, I'm using SPC-3 r12), section 4.3.

The SCSI opcode is divided into a group code (bits 7-5) and a Command Code (bits 4-0), see section 4.3.4.1.

The group code identifies the command length:

Group Length
0 6 byte
1 10 byte
2 10 byte
3 reserved (except for 0x7f, the variable length commands)
4 16 byte
5 12 byte
6 vendor specific
7 vendor specific

Thus, when you | 0x40 into MODE_SENSE, you change it from group0 to group2 and it becomes a ten byte command.

For each of the non vendor specific groups, called the fixed length commands, the location of the transfer count in each command is laid out in the standard, too, section 4.3.3 (as well as other things like the block location).

They are

Command Size transfer offset transfer size
6 byte byte 4 1 byte
10 byte byte 7 2 bytes
12 byte byte 6 4 bytes
16 byte byte 10 4 bytes
32 byte(*) byte 28 2 bytes

* The 32 byte commands are variable length commands (opcode 0x7f) with Additional CDB length set to 0x18. The actual command is specified in the service action field (two bytes).

So the algorithm is quite simple: you get the command length from the group (using a table with one exception for the 32 byte commands), and then extract the transfer length from the command

For the other reserved or vendor specific commands, you just return -1 or some other indicator that we have to believe the buffer length to be the command length.

James

#7 lore raw
Quoted 8 lines · James Bottomley
Quoted 3 lines · Matthew Dharm

I don't see a way to know the byte count without having additional knowledge about the device. Maybe I'm wrong -- I certainly don't claim to be a SCSI expert. So explain it to me.

Certainly. It's documented in the SCSI3 Primary commands (for reference, I'm using SPC-3 r12), section 4.3.

...

Quoted 5 lines

For each of the non vendor specific groups, called the fixed length commands, the location of the transfer count in each command is laid out in the standard, too, section 4.3.3 (as well as other things like the block location).

Quoting from 4.3.1:
---
The fields shown in 4.3.2 and 4.3.3 and described in 4.3.4 are used consistently by most commands. However, the actual usage of any field (except OPERATION CODE and CONTROL) is described in the subclause defining that command.
---

This does not say that _all_ commands behave according to this description. An example is the READ_6 command for sequential access devices. The opcode defines a 6 byte command. Transfer length in this command occupies bytes 2 - 4 (3 bytes instead of 1). This does not necessarily specify the transfer length in bytes. Bit 0 of byte 1 specifies if the command uses fixed block size or variable block size. If fixed block size is specified, the transfer length field has to be multiplied by the block size. This may have been earlier specified using a MODE SELECT command (in block descriptor). However, the MODE SELECT may not have been used if the user knows the block size. The mid-level code should the perform and extra MODE SENSE to get the blocks size. In case of a very old device, MODE SENSE may not even be supported (optional in SCSI-1).

I would say the mid-level should not try to determine the transfer length ;-(

Signature · 1 line
Kai
#8 lore raw
In reply to James Bottomley
Quoted 3 lines · James Bottomley

So the algorithm is quite simple: you get the command length from the group (using a table with one exception for the 32 byte commands), and then extract the transfer length from the command

I'm with you up to about this point.

So tell me how many bytes my command wanted to move? READ_10 specifies the transfer length is 'blocks'. How many bytes are in a block is device-dependent.

Some commands (i.e. INQUIRY) specify transfer length in bytes. Others (i.e. READ_10) specify it in blocks, and the size of a block is not known based simply on the current command.

Matt

Signature · 6 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

Okay, this isn't funny anymore! Let me down!  I'll tell Bill on you!!
					-- Microsoft Salesman
User Friendly, 4/1/1998
#9 lore raw
Quoted 9 lines · Matthew Dharm
Quoted 3 lines · James Bottomley

So the algorithm is quite simple: you get the command length from the group (using a table with one exception for the 32 byte commands), and then extract the transfer length from the command

I'm with you up to about this point.

Quoted 7 lines

So tell me how many bytes my command wanted to move? READ_10 specifies the transfer length is 'blocks'. How many bytes are in a block is device-dependent.

Some commands (i.e. INQUIRY) specify transfer length in bytes. Others (i.e. READ_10) specify it in blocks, and the size of a block is not known based simply on the current command.

Matt,

You cannot really constrict yourself to the CDB only.

As a transport/interconnect (what a LLDD is, as well as USB Storage) you get an Execute Command remote procedure call, in SCSI Core this is represented by struct scsi_cmnd.

Execute Command rpc contains all that the transport/ic needs in order to deliver the command to the device server (who will execute the CDB) and perform (the interconnect) the data transfer.

The reference you want to take a peek at is SAM-3r6, 5.1 -- this is what USB storage is working with, not just the CDB.

Now, you've been talking about the Data-Out buffer size. This is either ALLOCATION LENGTH (normally bytes), or TRANFER LENGTH (READ/WRITE, normally blocks).

The reference here is SPC3r12, 4.3.4.4 Transfer Length and 4.3.4.6 Allocation Length.

Please note that Data-Out buffer size *is* the Expected (maxumum) Data Tranfer Length, the device server may tranfer less data, (request_bufflen, bufflen). This is what the standards specify, and what SCSI Core sets it to. The actual memory buffer may be bigger. The device server may transfer less data, but not more. The device server will NOT modify the returned data to reflect the insufficient data-in buffer.

So to repeat what the standards say and what SCSI Core
does: sr_bufflen, request_buflen, buflen *is* the Expected (maxumum) Data Tranfer Length:
- when it means Allocation Length it is the
maximum available space (e.g. IQUIRY, Request Sense), (The transport CANNOT write more data than this!)
- when it means Transfer Length it is the
Expected Data Transfer Length (READ/WRITE), and I say ``expected'' less any End Of Media or similar errors pop up, (i.e. number of blocks from CDB * block_size = this value).

I.e. the _actual_ buffer size *is* IRRELEVANT to LLDD and the Transport/IC -- since it maybe an offset to another yet bigger buffer and SCSI Core/Appl. Client maybe requesting part of some data (see SAM-3r6, 5.4.3).

Some transports provide an Overflow and Underflow bits in the transport protocol to let the Initiator port (LLDD) know about any residuals -- SCSI Core struct scsi_cmnd supports only an underflow residual (bytes that were not transferred out of the number of bytes that were expected to be transferred).

I don't know much about USB Storage, but what do you think about this: add an unsigned overflow_resid to struct scsi_cmnd, and set it to an applicable value when a USB storage device provided more data to be transferred, but the buffer was too small. (I can see how this will not work, if you need a large enough ptr to just write the data to...)

Signature · 1 line
Luben
#10 lore raw
Quoted 9 lines · Luben Tuikov

Matt,

You cannot really constrict yourself to the CDB only.

As a transport/interconnect (what a LLDD is, as well as USB Storage) you get an Execute Command remote procedure call, in SCSI Core this is represented by struct scsi_cmnd.

Quoted 8 lines

Execute Command rpc contains all that the transport/ic needs in order to deliver the command to the device server (who will execute the CDB) and perform (the interconnect) the data transfer.

The reference you want to take a peek at is SAM-3r6, 5.1 -- this is what USB storage is working with, not just the CDB.

Now, you've been talking about the Data-Out buffer size.

Corr: Data-In

Quoted 7 lines

This is either ALLOCATION LENGTH (normally bytes), or TRANFER LENGTH (READ/WRITE, normally blocks).

The reference here is SPC3r12, 4.3.4.4 Transfer Length and 4.3.4.6 Allocation Length.

Please note that Data-Out buffer size *is* the

Corr: Data-In

Quoted 37 lines

Expected (maxumum) Data Tranfer Length, the device server may tranfer less data, (request_bufflen, bufflen). This is what the standards specify, and what SCSI Core sets it to. The actual memory buffer may be bigger. The device server may transfer less data, but not more. The device server will NOT modify the returned data to reflect the insufficient data-in buffer.

So to repeat what the standards say and what SCSI Core
does: sr_bufflen, request_buflen, buflen *is* the Expected (maxumum) Data Tranfer Length:
- when it means Allocation Length it is the
maximum available space (e.g. IQUIRY, Request Sense), (The transport CANNOT write more data than this!)
- when it means Transfer Length it is the
Expected Data Transfer Length (READ/WRITE), and I say ``expected'' less any End Of Media or similar errors pop up, (i.e. number of blocks from CDB * block_size = this value).

I.e. the _actual_ buffer size *is* IRRELEVANT to LLDD and the Transport/IC -- since it maybe an offset to another yet bigger buffer and SCSI Core/Appl. Client maybe requesting part of some data (see SAM-3r6, 5.4.3).

Some transports provide an Overflow and Underflow bits in the transport protocol to let the Initiator port (LLDD) know about any residuals -- SCSI Core struct scsi_cmnd supports only an underflow residual (bytes that were not transferred out of the number of bytes that were expected to be transferred).

I don't know much about USB Storage, but what do you think about this: add an unsigned overflow_resid to struct scsi_cmnd, and set it to an applicable value when a USB storage device provided more data to be transferred, but the buffer was too small. (I can see how this will not work, if you need a large enough ptr to just write the data to...)

Signature · 1 line
Luben
#11 lore raw
In reply to Luben Tuikov
Quoted 3 lines · Luben Tuikov

Now, you've been talking about the Data-Out buffer size. This is either ALLOCATION LENGTH (normally bytes), or TRANFER LENGTH (READ/WRITE, normally blocks).

Now we're making progess.

Quoted 10 lines

So to repeat what the standards say and what SCSI Core
does: sr_bufflen, request_buflen, buflen *is* the Expected (maxumum) Data Tranfer Length:
- when it means Allocation Length it is the
maximum available space (e.g. IQUIRY, Request Sense), (The transport CANNOT write more data than this!)
- when it means Transfer Length it is the
Expected Data Transfer Length (READ/WRITE), and I say ``expected'' less any End Of Media or similar errors pop up, (i.e. number of blocks from CDB * block_size = this value).

So, to paraphrase and make certain I understand: For a certain class of commands, buflen is the ALLOCATION LENGTH. Right now, we guarantee that this matches the 'expected transfer length', but that's only by convention.

For the other class of commands, it's a TRANSFER_LENGTH, which we guarantee to be the 'expected transfer length'.

You know, it occurs to me that we're talking in circles. The only reason this is an issue is because usb-storage re-writes commands into something which is more acceptable -- if we eliminate this re-writing with a patch similar to Andries', the problem goes away.

Matt

Signature · 8 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

A:  The most ironic oxymoron wins ...
DP: "Microsoft Works"
A:  Uh, okay, you win.
					-- A.J. & Dust Puppy
User Friendly, 1/18/1998
#12 lore raw
Quoted 13 lines · Matthew Dharm
Quoted 11 lines

So to repeat what the standards say and what SCSI Core
does: sr_bufflen, request_buflen, buflen *is* the Expected (maxumum) Data Tranfer Length:
- when it means Allocation Length it is the
maximum available space (e.g. IQUIRY, Request Sense), (The transport CANNOT write more data than this!)
- when it means Transfer Length it is the
Expected Data Transfer Length (READ/WRITE), and I say ``expected'' less any End Of Media or similar errors pop up, (i.e. number of blocks from CDB * block_size = this value).

Quoted 3 lines

So, to paraphrase and make certain I understand: For a certain class of commands, buflen is the ALLOCATION LENGTH. Right now, we guarantee that this matches the 'expected transfer length', but that's only by convention.

ALLOCATION LENGTH is for things like INQUIRY and REQUEST SENSE, and its metric is bytes.

It is *this* value which the device server may send less data or may _have_ more data than the length of the Data-In buffer.

I.e. it is this value for which we are not certain, this is to say, the data-in buffer size (request_buflen) is a guess -- the device may have more or less data to send.

(all directions are always with respect to the Initiator (SCSI Core)).

Quoted 2 lines

For the other class of commands, it's a TRANSFER_LENGTH, which we guarantee to be the 'expected transfer length'.

In general -- yes. When it is TRANSFER LENGTH (e.g. READ/WRITE), you can be more or less certain that exactly that many blocks will be transferred. I.e. you can fully rely on request_buflen (bytes). The exception is when an error occurs, like End Of Partition or End of Media.

Quoted 4 lines

You know, it occurs to me that we're talking in circles. The only reason this is an issue is because usb-storage re-writes commands into something which is more acceptable -- if we eliminate this re-writing with a patch similar to Andries', the problem goes away.

As long as any re-writing is *not* arbitrary *and* is reversibe on the way up.

As Alan Stern mentioned, it may be the case that the command sent is sent as intended to test the device (or whatever). If this will crash the device, USB storage should return the appropriate sense data or response code. If this will crash the system, SCSI Core should return the appropriate response/error code.

All, please remember that we're providing mechanism, NOT policy.

So, ok, let us (SCSI Core) send the 10 byte versions of commands, unless
the device server reports CHECK CONDITION status with the sense key set to ILLEGAL REQUEST and an additional sense code of INVALID FIELD IN CDB. (as per the standard), and then we drop to 6 (rarely).

Inquiry first 36, then more if needed.

As for REQUEST SENSE, the standards specify that in order to guarantee that all sense data has been retrieved, ALLOCATION LENGTH should be 252. And if less is specified, then the sense data is lost as REQUEST SENSE clears the sense data buffer of the device server.

In this case you cannot blame SCSI Core or the application client, and will have to filter it yourself or provide a settable filter
as per Alan Cox's suggestion for a filter (I think you had the same idea:
the filter framework is universal, but the values are per host/device settable).

(Because SCSI Core/Appl. Client have full right to request 252 bytes in the ALLOCATION LENGTH field.)

In general, we cannot change SCSI Core much more than what SAM-3 and SPC-3 specify in order to accomodate broken device servers of a particular SCSI transport -- the transport is invisible to SCSI Core.

There can and will be a compromise, but only a LLDD knows the particulars of the transport/ic and the device to do proper job in filtering or whatever is necessary in order to deliver the command to the device -- but it will have to really be a minimalistic filter.

So overall I see a great improvement in SCSI Core regarding those issues, beginning with Andries' patch (but buffer extents will have to be checked!).

Signature · 12 lines
Luben



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
[email protected]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel
#13 lore raw
In reply to [email protected]
Quoted 8 lines · On Sat, 2003-04-26 at 16:44, [email protected] wrote:

[[problem solved, we understand the hardware, it seems, now wish to find an elegant way of doing what is required (and only what is required) in the scsi and/or usb storage code]]

(1) usb-storage is broken in the sense that it uses sr_bufflen for the transfer size instead of the buffer length, then fudges commands to make them transfer a different length, but without updating sr_bufflen.

definitely agreed.

Quoted 5 lines

(1A)
Of course it is bad when variables have a function that differs from what is suggested by their name. So, sr_bufflen must be the length of the buffer and nothing else. If the length of the transfer is needed, there must be a field sr_xferlen or so.

This looks fine to me. Originally, sr_bufflen was exactly that (because all SCSI LLDs have to be prepared to transfer fewer than the specified number of bytes anyway, since that's required by the spec). I believe only USB needs this to be set to the actual command length (and further require that we ensure that the device *will* transfer exactly this...that's to some extent the origin of the INQUIRY problems), so the change should be small.

[...]

Quoted 1 line

I dislike the choice of identifier "ten" - it is difficult to grep for,

agreed.

Quoted 9 lines

and I changed it into use_10_for_rw, with the same function as before. Added a field use_10_for_ms. Thus, in scsi.h:

(quoted diff) +2 -0
+       unsigned use_10_for_rw:1; /* first try 10-byte read / write */
+       unsigned use_10_for_ms:1; /* first try 10-byte mode sense/select */

in the declaration of struct scsi_device.

Now sd_do_mode_sense6() becomes sd_do_mode_sense() and does

This looks good (probably this should go in scsi_lib.c and sr should start using it for consistency).

Quoted 3 lines

All this is nice and well. Remains the question how usb-storage makes sure that the use_10_for_rw and use_10_for_ms flags are set for its devices.

Well, this should be done in the USB storage slave_configure...that's almost exactly what the interface is designed for (altering scsi_device parameters before the SCSI subsystem starts using it).

Quoted 5 lines

Tonight I kludged this by setting this (in protocol.c) at the moment the first INQUIRY is done. But that is terribly ugly.
Did I overlook some obvious means of communication?

Comments?

This sounds to be exactly the right approach (other than the missing slave_configure).

James

#14 lore raw
Quoted 8 lines
Quoted 4 lines

All this is nice and well. Remains the question how usb-storage makes sure that the use_10_for_rw and use_10_for_ms flags are set for its devices.

Well, this should be done in the USB storage slave_configure...that's almost exactly what the interface is designed for (altering scsi_device parameters before the SCSI subsystem starts using it).

I prefer Matt's suggestion, that we handle this similar to sd.c use of use_10_for_rw so the alogrithm works for all devices independent of the LLDD.

Is MODE SENSE 10 used by default in other OSes and more likely to work than MODE SENSE 6?

-- Patrick Mansfield

#15 lore raw
In reply to James Bottomley
Quoted 7 lines · James Bottomley

This looks fine to me. Originally, sr_bufflen was exactly that (because all SCSI LLDs have to be prepared to transfer fewer than the specified number of bytes anyway, since that's required by the spec). I believe only USB needs this to be set to the actual command length (and further require that we ensure that the device *will* transfer exactly this...that's to some extent the origin of the INQUIRY problems), so the change should be small.

This is a common misconception. usb-storage does not need to know, in advance, exactly how many bytes will transfer. It does, however, need to know exactly how many bytes are being requested for transfer.

There's a subtle difference there. usb-storage can handle the case of underrun or overrun properly. But there are USB<->[SCSI|ATA|whatever] bridges which can't handle a discrepancy between expected transfer length and actual transfer length when no error was detected.

I can give examples, but it's confusing and not really relevant unless someone wants to know.

There are also broken devices which are specifically broken with respect to the INQUIRY command for length != 36, but that's a separate issue to a certain degree. These are really two separate problems.

Matt

Signature · 6 lines
Matthew Dharm                              Home: [email protected] 
Maintainer, Linux USB Mass Storage Driver

YOU SEE!!?? It's like being born with only one nipple!
					-- Erwin
User Friendly, 10/19/1998
#16 lore raw
Quoted 8 lines · Matthew Dharm

This is a common misconception. usb-storage does not need to know, in advance, exactly how many bytes will transfer. It does, however, need to know exactly how many bytes are being requested for transfer.

There's a subtle difference there. usb-storage can handle the case of underrun or overrun properly. But there are USB<->[SCSI|ATA|whatever] bridges which can't handle a discrepancy between expected transfer length and actual transfer length when no error was detected.

I wonder about this a little, for overruns. It's certainly true that we detect an error and report problems when a transfer is larger than expected (unless the expected length is exactly a multiple of the USB packet size). But it gets reported as DID_ERROR, indicating a possibly transient communications problem.

This is just what Andries saw. The MODE-SENSE commands wanted 4 bytes but the devices insisted on sending more. These waere reported as errors rather than command failures (CHECK-CONDITION and so on), so the error-handler kicked in and the same commands were retried to no avail. That's no way to handle a systematic problem.

It would have worked better if instead we treated -EOVERFLOW as a failure, not an error. Or if we had known that the device wouldn't work properly with a 4-byte MODE-SENSE(6) transfer. We could have returned sense data that would tell the SCSI layer to try a different form of the command.

What I'm not sure about is the practicality of trying to handle things this way. Under what conditions is it correct to treat -EOVERFLOW during the data phase as a failure instead of an error? And do we want to try and track the little foibles of many different sorts of devices so that we can reject commands they don't handle properly? Yes, we already do this to some extent, but how far do we want to carry it through?

Alan Stern