2010年10月13日 星期三

Linux USB, max_sectors

今天在看USB 3.0的問題時,順便去Google來的一些資料,雖然沒有找到為何USB 3.0的max_sectors不能設超過240為什麼某些 device 會無法接受 max_sectors > 240,但也算長了知識.......

Q: What is max_sectors and how should I use it?
A:For USB Mass Storage devices (that is, devices which use the usb-storage driver) max_sectors controls the maximum amount of data that will be transferred to or from the device in a single command. As the name implies this transfer length is measured in sectors, where a sector is 512 bytes (that's a logical sector size, not necessarily the same as the size of a physical sector on the device). Thus for example, max_sectors = 240 means that a single command will not transfer more than 120 KB of data.

Linux 2.6 gives you the ability to see and to change the max_sectors value for each USB storage device, independently. Assuming you have a sysfs filesystem mounted on /sys and assuming /dev/sdb is a USB drive, you can see the max_sectors value for /dev/sdb simply by running:

cat /sys/block/sdb/device/max_sectors
and you can set max_sectors to 64 by running (as root):

echo 64 >/sys/block/sdb/device/max_sectors
Values should be positive multiples of 8 (16 on the Alpha and other 64-bit platforms). There is no upper limit, but you probably shouldn't make max_sectors much bigger than 2048 (corresponding to 1 MB, which is quite a lot).

In general, increasing max_sectors will improve throughput since it means that larger amounts of data can be transferred in a single command with no need for being split up among multiple commands. Of course this is subject to diminishing returns when max_sectors is very big. More importantly, it's true only up to a point. Many devices have limits on the amount of data they can transfer, and if you try to exceed that limit you will most likely crash the device.

The default value of 240 works well with most devices. If you're not running at USB 2.0's high speed (480 Mb/s) there's no reason to increase max_sectors. If you are running at high speed and your device can take it, feel free to go as high as you like. Note that with many devices there isn't much penalty for using a smaller-than-optimum value unless you set max_sectors to something really low! Go ahead and experiment to find what value works best with your hardware.

Some devices can only transfer 64 KB or less at a time. The most notable example is the suite of USB-IDE adapters made by Genesys Logic. According to their technical support staff transfers should be limited to 32 KB (max_sectors = 64), and usb-storage automatically sets max_sectors to this value when it detects a Genesys Logic device. However people have had no trouble using 64 KB transfers (max_sectors = 128), and that's what Windows uses. You can always increase the value above 64 using sysfs, but don't go beyond 128 as Genesys Logic devices are known to fail when transferring more than 64 KB.

USB storage media - data integrity issues on linux
Linux USB Frequently Asked Questions #5