Friday, January 29, 2010

Steps to Init Block Device Driver on Linux

from Essential Linux Device Driver
Registers the block device using register_blkdev(). This block library
routine assigns an unused major number to myblkdev and adds an entry for
the device in /proc/devices.
Associates a request method with the block device. It does this by
supplying the address of myblkdev_request() to blk_init_queue(). The
call to blk_init_queue() returns the request_queue for myblkdev. Refer
back to Figure 14.2 to see how the request_queue sits relative to the
driver. The second argument to blk_init_queue(), myblkdev_lock, is a
spinlock to protect the request_queue from concurrent access.
Hardware performs disk transactions in units of sectors, whereas
software subsystems, such as filesystems, deal with data in terms of
blocks. The common sector size is 512 bytes; the usual block size is
4096 bytes. You need to inform the block layer about the sector size
supported by your storage hardware and the maximum number of sectors
that your driver can receive per request. myblkdev_init() accomplishes
these by invoking blk_queue_hardsect_size() and blk_queue_max_sectors(),
Allocates a gendisk corresponding to myblkdev using alloc_disk() and
populates it. One important gendisk field that myblkdev_init() supplies
is the address of the driver's block_device_operations. Another
parameter that myblkdev_init() fills in is the storage capacity of
myblkdev in units of sectors. This is accomplished by calling
set_capacity(). Each gendisk also contains a flag that signals the
properties of the underlying storage hardware. If the drive is
removable, for example, the gendisk's flag field should be marked
Associates the gendisk prepared in Step 4 with the request_queue
obtained in Step 2. Also, connects the gendisk with the device's
major/minor numbers and a name.
Adds the disk to the block I/O layer by invoking add_disk(). When this
is done, the driver has to be ready to receive requests. So, this is
usually the last step of the initialization sequence.

No comments: