Device Drivers The OS Operating System is one of the desired to cover the user's hardware devices. For example, a virtual file system presents a unified attempt of the installed file system, and the underlying physical device is independent. This chapter describes how the Lin UX core is how physical devices in the system. The CPU is not the only smart device in the system, and each physical device is made by its own hardware controller. The keyboard, the mouse, and serial port are controlled by the superIO chip, the IDE disk is controlled by the IDE controller, and the SCSI disk is controlled by SCSI controller, and so on. Each hardware controller is different from its own control and status controller (CSR), and different devices are different. An Adaptec 2940 SCSI controller CSR and NCR 810 SCSI controller is completely different. CSR is used to start and stop devices, initializing devices, and diagnosing it. Managing these hardware controllers is not placed in each application, but placed in Linux core. These processes or manage the software foot of the hardware controller to do device drivers. The Linux core device driver is essentially privileged, the shared library of low hardware control routines that resident memory. It is the qualities of Linux's device drivers in handling them managed. A basic feature of UNIX is that it abstracts the processing of the device. All hardware devices look like regular files: They can be used and the standard system calls are available, and the standard system calls are turned on, off and read. Each device in the system uses a special file representative. For example, the first IDE hard disk in the system is represented by / dev / had. For blocks (disks) and character devices, these devices are created with mknod commands, and describe the device using the main (MAJOR) and sub-device number. Network devices also use equipment special files, but they are created by Linux when they are found and initialize the network controller in the system. All devices controlled by the same device driver are numbered by a common Major device. The secondary device number is used to distinguish between different devices and their controllers. For example, different partitions of the main IDE disk are numbered by a different secondary device. Therefore, the main device number of the second partition of / dev / hda2, the main IDE disk is 3, and the secondary device number is 2. Linux uses the master table and some system tables (such as character device table chrdevs) to map the device special files (such as installing a file system on a block device) on a device driver. See FS / Devices.c Linux supports three types of hardware devices: characters, blocks, and networks. The character device is directly read and written, there is no buffer, such as serial port / dev / cua0 and / dev / cua1 of the system. The block device can only read and write in a multiple of one block (typically 512 bytes or 1024 bytes). The block device is accessed through Buffer Cache, which is random access, that is, any block can read and write without having to consider where it is in the device. Block equipment can be accessed through special files of their equipment, but more common is to access through file systems. Only one block device can support a installed file system. The network device is accessed through the BSD Socket interface, and the network subsystem is described in the network chapter (Chapter 10). Linux has many different device drivers (this is also one of Linux's power) but they all have some general attributes: the Kernel Code device driver and other code in the core are similar, which is part of the kena, if an error occurs, may be serious Damage system. A write-wrong driver may even destroy the system, which may damage the file system, lose data.
The KENEL Interfaces device driver must provide a standard interface to the Linux core or its subsystem. For example, the terminal driver provides a file I / O interface to the Linux core, while the SCSI device driver provides SCSI device interface to the SCSI subsystem, then providing the core I / O and Bu Ffer Cache interface to the core. The KERNEL MECHANISMS AND Services device driver uses standard core services such as memory allocation, interrupt forwarding, and waiting queue to complete working loadable Linux Most device drivers can be loaded as core modules when needed, and uninstall it when they need it. This makes the core are very adaptive and efficient for system resources. The Configurable Linux device driver can be built at the core. Which devices are built to the core when compiling. Dynamic is started in the system, and each device starts the program initialization it finds it managed hardware devices. If there is no relationship if the device controlled by a device driver does not matter. At this time, the device driver is only redundant, which takes up very little system memory without harm. 8.1 POLING AND Interrupts When each time gives the device command, for example, "Mushing the read head to the 42nd sector of the floppy disk", the device driver can select how it determines whether the command is completed. The device driver can poll the device or interrupt. Polling devices typically mean constantly reading its status registers until the status change of the device indicates that it has been completed. Because the device driver is part of the core, if the driver has been polling, the core cannot run anything else before the device completes the request, it will be heavy. So polling device drivers use a system timer to allow the system to call one of the routines in the device driver later. This timer routine checks the status of the command, and Linux's floppy disk driver works this. The use of the timer is a best approach, while more effective ways is to use interrupts. The interrupt device driver will issue a hardware interrupt when it is controlled by the hardware device. For example, an Ethernet device driver is interrupted when the device receives an Ethernet message on the network. Linux core needs to be able to forward interrupt from hardware devices to the correct device driver. This is implemented by using the device driver to register the interrupt it uses to the core. It registers the address of the interrupt handler routine and it wants to have interrupt numbers. You can see which interrupts and each type of interrupt use it through / proc / interrupts, how many times: 0: 727432 Timer 1: 20534 Keyboard 2: 0 Cascade 3: 79691 Serial 4: 28258 Serial 5: 1 Sound Blaster 11: 20868 AIC7xxx 13: 1 Math Error 14: 247 IDE0 15: 170 IDE1 Request for Interrupt Resources The time is initialized in the driver. Some interrupts in the system are fixed, which is a legacy of the IBM PC architecture. For example, the floppy disk controller always uses interrupt 6. Other interrupts, such as interrupts of PCI devices, dynamically allocate when starting. At this time, the device driver must first identify the interrupt number of the device it controls, and then requests this interrupt (processing). For PCI interrupts, Linux supports standard PCI BIOS callbacks (Callback) to determine information in the system, including their IRQ. An interrupt itself is forwarded to the CPU dependent on the architecture. However, in most systems, interrupts are transmitted in a special pattern, and other interrupts occur in the system.
The device driver should do as little as possible in its interrupt processing routine, so that the Linux core can end the interrupt and return it to its interrupt. After receiving the interruption, you need to do a lot of work, you can use the core Bottom Half Handler or task queue to bring the routine in order to call later. 8.2 Direct Memory Access (DMA) is quite good when using an interrupt driven device driver using an interrupt drive. For example, a 9600 baud rate MODEM is approximately one character to each millisecond (1/1000 second). If the interrupt delay is issued from the hardware device to the interrupt handler that the interrupt handler in the start calling the device driver is relatively small (such as 2 milliseconds), then the data transmission is very small. The 9600 baud rate MODEM data will only take 0.002% of the CPU processing time. But for high-speed devices, such as hard disk controllers or Ethernet devices, data transmission rates are quite high. A SCSI device can transmit information up to 40M bytes per second. Direct memory access, or DMA, is the invention to solve this problem. A DMA controller allows the device to do not require the intervention of the processor and store data in the system. The PC's ISA DMA controller consists of 8 DMA channels, 7 of which can be used for device drivers. Each DMA channel is associated with a 16-bit address register and a 16-bit count register (Count register). In order to initialize a data transfer, the device driver needs to establish an address and count register of the DMA channel, plus the direction, read or write of data transfer. When the transmission ends, the device is interrupted. In this way, when the transmission occurs, the CPU can make other things. The device driver must be careful when using DMA. First, all DMA controllers do not know virtual memory, which can only access physical memory in the system. Therefore, the memory required to perform DMA transmission must be a continuous block in physical memory. This means that you cannot communicate DMA access to virtual address spaces of a process. However, you can lock the physics of the process to the memory when performing DMA operations. Second: The DMA controller cannot access all physical memory. The address register of the DMA channel represents the first 16-bit of the DMA address, followed by the page register. This means that the DMA request is limited to 16M memory at the bottom. The DMA channel is a rare resource, only 7, and cannot be shared between the device driver. Like the interrupt, the device driver must have the ability to find which DMA channel it can use. As interrupted, some devices have a fixed DMA channel. For example, a floppy drive, always use DMA channel 2. Sometimes, the DMA channel of the device can be set with jumper: Some Ethernet devices use this technology. Some more flexible devices can tell it (which DMA channel is used (through their CSR), at this time, the device driver can simply find an available DMA channel. Linux traces the use of the DMA channel using the DMA_CHAN data structure meter (one of each DMA channel). The DMA_CHAN data structure has only two jade: a character pointer, describes the owner of this DMA channel, and a flag shows whether this DMA channel is assigned. The DMA_C HAN vector table is displayed when you CAT / PROC / DMA. 8.3 Memory device drivers must be careful to use memory. Because they are part of the Linux core, they cannot use virtual memory.
When each device driver is running, it may be an interrupt or scheduling a BUT TOM HALF HANDLER or task queue, and the current process may change. The device driver cannot rely on a special process that is running. Like other parts of the core, the device driver uses the data structure to track the device it controls. These data structures can be staticly allocated in the code part of the device driver, but this will make the core unnecessarily increased and waste. Most device drivers allocate core, missed memory stored them. The Linux core provides the core memory allocation and release routines, and the device driver is using these routines. The core memory is allocated according to the block of 2. For example, 128 or 512 bytes, even if the number of device driver requests is not so much. The number of bytes requested by the device driver is het with the size of the next block. This makes the core memory recovery easier because smaller idle blocks can be combined into larger blocks. Linux also needs to do more additional work when requesting core memory. If the total number of idle memory is too small, the physical page needs to be discarded or written to the switch. Typically, Linux will hang a requestor and put this process in a waiting queue until you have enough physical memory. Not all device drivers (or the core code of Linux) I hope that this is something, the core memory allocation routine can request to fail if the memory cannot be assigned. If the device driver wants to access the memory for DMA, it also needs to indicate that this memory is a DMA. Because it is necessary to make Linux core understand which of the continuous DMA memory can be made, not the device driver is determined. 8.4 Interfacing Device Drivers with The Kernel (Device Drivers and Core Interface) Linux core must be able to use standards and their roles. Each class of equipment drivers: characters, blocks, and networks provide a general interface for use when requested to request their services. These universal interfaces mean that the core can be seen exactly the same as very different devices and their device drivers. For example, the behavior of SCSI and IDE disks is very different, but the Linux core uses the same interface to them. Linux is very dynamic, each Linux core starts, which may encounter different physical devices to require different device drivers. Linux allows you to include the device driver by configuring the script in the core. These device drivers are initialized when starting, and they may not find any hardware they can control. Other drivers can be loaded as a core module when needed. In order to deal with this dynamic qualities of the device driver, the device driver registers the core when they initialize. Linux maintains a list of registered device drivers, as part of their interface. These lists include pointers of routines and information that supports interfaces of this class. 8.4.1 Character Devices Character Devices, Linux simplest devices, like files. The application uses the standard system call to open, read, write, and off, it is true that this device is a normal file. This is also the case if it is connected to a Linux system's PPP daemon. When the character device is initialized, its device driver regishes the Linux core, adds a Device_Struct data structure entry in the Chrdevs vector table. The main device identifier of this device (for example, a TTY device is 4), used as an index of this vector table. The master identifier of a device is fixed. Each entry in the chrdevs vector table, a DEVI CE_STRUCT data structure, including two elements: a pointer to the name of a registered device driver and a pointer to a set of files.
This file operation itself is located in the character device driver of this device, each handling specific file operations such as opening, reading, writing, and off. / proc / devices The contents of the character devices come from the chrdevs vector table See include / linux / major.h When a character device (such as / dev / cua0) is opened, the core must do something, thus getting right The file operation routine for the character device driver. Like normal files or directories, each device special file is expressed in VFS I node. VFS inode (actually all device special files) of this character special file includes the MAJOR and MINOR identifier of the device. This VFS i node is created based on the actual file system when the special file system is found by the underlying file system (eg EXT2). See fs / ext2 / inode.c ext2_read_inode () Each VFS I node is connected to a set of files, depending on the file system object represented by the i node. Regardless of whether a VFS I node representing a character special file is created, its file operation is set to the default operation of the character device. This has only one file operation: OPEN operation. When an application opens this character special file, the general Open file operation uses the device's main device identifier as an index in the ChRDE VS vector table, removes the file operation block of this special device. It also establishes the file data structure describing this character special file, allowing its file operation to point to the operation in the device driver. Then all file system operations of the application are mapped to the file operation of the character device. See fs / devices.c chrdev_open () DEF_CHR_FOPS 8.4.2 Block Devices block device also supports the image as a file. This mechanism for providing the correct file operation group for open blocks is very similar. Linux maintains the registered block device file with the BLKDEVS vector table. It is like a chrdevs, using the main device number of the device as an index. Its entry is also the Device_s Truct data structure. Unlike characters, block devices are classified. SCSI is one of the classes, and IDE is another class. The class registers the Linux core and provides file operations to the core. A block device class device driver provides such a class-related interface. For example, the SCSI device driver must provide an interface to the SCSI subsystem, so that the SCSI subsystem is used to provide file operations with the core to provide this device for the FS / Devices.c Each block device driver must provide a normal file operation interface and Buffer Cache interface. Each block device driver fills its BLK_DEV_STRUCT data structure in the BLK_DEV vector table. The index of this vector table is also the main device number of the device. This BLK_DEV_STRUCT data structure includes an address of a request routine and a pointer pointing to a list of request data structures, each expressing a request to write a piece of data to the device. See Drivers / Block / LL_RW_BLK.C Include / Linux / BLKDEV.H Each time Buff Cache wishes to read a piece of data or from a registered device it adds an REQUEST data structure in its BLK_DEV_STRUC. Figure 8.2 shows that each Request has a pointer pointing to one or more buffer_head data structures, each is a request to read and write a piece of data. This buffer_head data structure is locked (Buffer Cache) may have a process to complete the blocking process waiting for this buffer. Each request structure is allocated from a static table, all of the all_request table.
If this request increases to an empty Request list, call the driver's REQ UEST function to handle this Request queue. Otherwise, the driver simply handles every request in the Request queue. Once the device driver has completed a request, it must remove each buffer_head structure from the Reques T structure, tagget it is the latest, then unlock. The unlocking of buffer_head will wake up any process that is waiting for this blocking operation. This example includes file analysis: Waiting for the EXT2 file system to read data blocks including the next EXT2 directory entry from block devices that include this file system, this process will be in the buff_head queue that will include the directory entry. It is until the device driver wakes up. This Request data structure is marked as idle, and can be used by another block. 8.5 Hard Disks The hard disk stores the data on the rotated disk, providing a more permanent storage data. In order to write data, a tiny magnetic head puts a tiny point magnetization of the surface of the disk. You can read the data by the magnetic head to detect whether the specified particles are magnetized so that data can be read. One disk drive consists of one or more disks, each of which is made of a relatively smooth glass or ceramic and covering a fine metal oxide. The disk is placed on a central axis and rotates at a stable speed. The rotational speed is from 3000 to 1000 rpm (rotation / per minute) according to the model. The disk read / write magnetic head is responsible for reading and writing data, each with a pair, one for each side. The read / write head and the surface of the disk do not have a physical contact, but float on a very thin air cushion (one hundred thousandth inch). The read and write head moves through a driver on the surface of the disc. All of the magnetic heads are sticky together and move together on the surface of the disc. The surface of each disk is divided into a plurality of narrow concentric rings called tracks. The track 0 is the outermost track, the highest number of tracks are the tracks closest to the central axis. A cylinder is a combination of the same number track. So all of the 5th tracks of each surface of each disk is the 5th cylinder. Because the number of cylindrical numbers and the number of tracks are the same, the size of the magnetic disk is often described. Each track is divided into a sector. A sector is a minimum data unit that can be read from the hard disk, that is, the block size of the disk. Typically the sector size is 512 bytes, and the sector size is usually set when it is formatted when the disk is manufactured. Disks typically describe its size (Geometry): cylindrical number, number of magnetic heads, and number of sectors. For example, when Linux describes my IDE disk this time: HDB: Conner Peripherals 540MB - CFS540A, 516MB W / 64KB Cache, CHS = 1050/16/63 This means it consists of 1050 cylinders (tracks), 16 heads (8 A disk) and 63 sectors / tracks. For a 512-byte sector or block size, the disk capacity is 529200k bytes. This is not compliant with the 516m storage capacity of the disk declaration because some sectors are used as partition information for the disk. Some disks can automatically find bad sectors and re-index them. The hard disk can be divided into partitions. A partition is a large group of sectors that are allocated for specific purposes. The disk partition allows the disk to be used for several operating systems or more purposes. Most single disk Linux systems are made from 3 partitions: a DOS file system, the other is an EXT2 file system, and the third is a swap partition. The partition of the hard disk is described by partitioning table, and each entry uses a head, sector, and cylinder to describe the stand position of the partition.
For DOS disks formatted with fdisk, there can be four primary disk partitions. Not four entries of the partition table must be used. FDISK supports three types of partitions: primary partitions, extensions, and logical partitions. The extended partition is not a real partition, which can include any number of logical partitions. The invention extension partitions and logical partitions are to exceed the limitations of four primary partitions. Below is an Output of FDISK, including 2 primary partitions: Disk / dev / sda: 64 Heads, 32 Sectors, 510 Cylinders Units = cylinders of 2048 * 512 BYTES Device Boot Begin Start End Blocks ID System / dev / sda1 1 1 478 489456 83 Linux Native / Dev / SDA2 479 479 510 32768 82 Linux Swap Expert Command (M for Help): P Disk / Dev / SDA: 64 Heads, 32 Sectors, 510 Cylinders Nr Af HD Sec Cyl HD Sec Cyl Start Size ID 1 00 1 1 0 63 32 477 32 978912 83 2 00 0 1 478 63 32 509 978944 65536 82 3 00 0 0 0 0 0 0 0 00 4 00 0 0 0 0 0 0 0 00 It shows the first The partition begins at the cylindrical or track 0, the head 1, and the sector 1 until the cylinder 477, the sector 32, and the head 63. Because a track consists of 32 sectors and 64 read-write heads, the cylinders of this partition are entirely included. FDisk defaults to align the partition on the border of the cylinder. It starts from the outermost cylinder (0), toward the central axis, extends 478 cylinders. The second partition, swap partition, start in the next cylinder (478) and extended to the innermost cylinder of the disk. Topological structure of the hard drive in the Linux mapping system in initialization. It finds how many hard drives are in the system and the type of hard drive. Linux also finds how each disk is partitioned. These are a list of GenDisk data structures directed by the GenDisk_Head pointer list. Generating the GenDisk data structure represents the disk it finds for each of the disk subsystems, such as IDE, initialization. This process and it registers its file operation and increasing its entry in the BLK_DEV data structure to occur at the same time. Each GenDisk data structure is all the same as a unique main device number, and a special device. For example, the SCSI disk subsystem creates a stand-alone GenDisk entry ("SD"), the main device number is 8 (all the main device number of all SCSI disk devices). Figure 8.3 shows two genDisk entries, the first is the SCSI disk subsystem, the second is the IDE disk controller. Here is the IDE0, the main IDE controller. Although the disk subsystem will establish a corresponding GenDisk entry when it is initialized, Linux is only used when the partition check is performed. Each disk subsystem must maintain its own data structure, allowing itself to map the main device number of the device and the secondary device number to the partition of the physical disk. Whether you read a write block device, the core is based on the main device number and the second device number found in the block special equipment file (such as / dev / sd a2), according to the main device number and the secondary device number found in the block special equipment file (such as / dev / sd a2). equipment. Each device driver or subsystem maps the secondary device number to a real physical device.
8.5.1 IDE Disks (IDE Disk) Today's most commonly used disk in the Linux system is an IDE disk (Integrated Disk Electronic). The IDE and SCSI are one disk interface instead of an I / O bus. Each IDE controller can support up to 2 disks, one is Master, and the other is slave. Master and slave typically set with jumpers on disk. The first IDE controller in the system is called the main IDE controller, the next called slave controller, and the like. IDE can be transferred from / 3.3 m / sec to the disk, and the maximum size of the IDE disk is 538m bytes. Extended IDE or EIDE increases the maximum disk size to 8.6g bytes, with a data transfer rate of up to 16.6 m / sec. IDE and EIDE disks are cheap than the SCSI disk, and most modern PCs have an IDE controller on one or more of the motherboards. Linux names IDE disk in the order of the controller it discovers. The main drive on the main controller is / dev / h ad, the slave disk is / dev / hdb. / DEV / HDC is the master disk on the Idee controller. The ID E subsystem registers the IDE controller instead of disk. The primary identifier of the main IDE controller is 3, the identifier of the secondary IDE controller is 22. This means that if a system has two IDE controllers, there will be an entry of the IDE subsystem in the index 3 and 22 in the BLK_DEV and BLKDEVS vectors. The block special file of the IDE disk reflects this number: Disk / DEV / HAD and / DEV / HDB, all connected to the main IDE controller, the main device number is 3. The core uses the master identifier as an index, and all files or Buffer Cache operations for these block special files or Buffer Cache operations are directed to the corresponding Ide subsystem. When an request is executed, the IDE subsystem is responsible for determining which IDE disk is for this request. To this end, the IDE subsystem uses the secondary device number in the device special file that allows it to direct the requested to the correct partition of the correct disk. / DEV / HDB, the device identifier of the slave IDE disk on the main IDE controller is (3, 64). Its first partition (/ dev / hdb1) device identifier is (3, 65). 8.5.2 Initializing The Ide Subsystem has IDE disks in most history of IBM PC. During this period, the interfaces of these devices have changed. This makes the initialization process of the IDE subsystem more complicated than it appears for the first time. The maximum number of IDE controllers that Linux can support is 4. Each controller is represented by an IDE_HWIF_T data structure in an IDE_HWIFS vector table. Each IDE_HWIF_T data structure contains two IDE_DRIVE_T data structures, which represent MASTER and SLAVE IDE drivers that may be supported, respectively. During the Ide Subsystem Initialization, Linux first checks information of the disk recorded in the system's CMOS memory. This is not lost when there is a PC shutdown in the backup of the battery. This CMOS memory is actually in the real-time clock device of the system, whether your PC is opened or closed, it is running. The location of the CMOS memory is set by the system's BIOS setting, and it tells what IDE controllers and drives found in the Linux system.
Linux gets the size of the disk found in the BIOS, and uses this information to set the data structure of the IDE_HW IF_T of this drive. Most modern PCs use a PCI chipset such as Intel's 82430 VX chipset, including a PCI EIDE controller. The IDE subsystem uses the PCI (E) IDE controller in the PCI BIOS callback positioning system. Then call the inquiry routine of these chipsets. Once an IDE interface or controller is found, set its IDE_HWIF_T to reflect this controller and the upper disk. The IDE command register of the IDE driver to the I / O memory space during operation is written. The default I / O address of the control and status register of the main IDE controller is 0x1f0-0x1f7. These addresses are early IBM PCs. The IDE driver registers each controller to Linux's buffer cache and VFS, add it to the BLK_DEV and BLKDEVS table. The IDE driver also requests to control the appropriate interrupt. Similarly, these interruptions also have agreed that the main IDE controller is 14, and the secondary IDE controller is 15. However, like all IDE details, these can change with the core command line option. The IDE driver also adds a GenDisk entry to the GenDisk list when the IDE driver is started. This list is later used to view the partition table of all hard drives found when starting. Partition check codes understand that each I DE controller can control two IDE disks. 8.5.3 SCSI Disks (SCSI Disk) SCSI (Small Computer System Interface Small Computer System Interface) Bus is a valid point-to-point data bus, and each bus supports up to 8 devices, each host can have one or more. Each device must be set by a unique identifier, usually with jumper settings on the disk. The data can be synchronized or asynchronously between any two devices on the bus, and 32-bit width data can be transmitted, and the speed may be as high as 40 m / sec. The SCSI bus can transmit data and status information between devices, the transactions between the initiator and the target, and the target (Target) involve up to 8 different stages. You can judge the current phase through the five signals on the SCSI bus. These 8 phases are: Bus Free has the control of the bus with the bus, and there is currently no transaction. Arbitration (Arbitration) A SCSI device attempts to get the control of the SCSI bus, which declares its SCSI identifier on the address pin. The highest number of SCSI identifiers are successful. Selection A device successfully obtained the control of the SCSI bus through the arbitration, and now it must send a signal to the SCSI target to send commands. It declares the target's SCSI identifier on the address pin. The RESELECTION SCSI device may disconnect in the process of processing the request, and the goal will reselect the initiator. Not all SCSI devices support this phase. Commands of Command 6, 10 or 12 bytes can be sent from the initiator to the target. Data IN, Data OUT is transmitted between the initiator and the target. Status has completed all the commands to enter this phase. Allow the target to send a status byte to the initiator, indicating success or failure. Message IN, Message Out The additional information passed between the initiator and the target.
The Linux SCSI subsystem consists of two basic elements, each with a data structure: Host A SCSI Host is a physical hardware, an SCSI controller. The NCR810 PCI SCSI controller is an example of SCSI Host. If a Linux system is more than one type of SCSI controller, each instance is represented by a SCSI Host, respectively. This means that an SCSI device driver may control an example of more than one controller. SCSI Host is typically always the initiator of the SCSI command (INITIATO R). Device SCSI devices are typically disk, but SCSI standards support multiple types: tape, CD-ROM, and Generic SCSI devices. SCSI devices are usually the target of SCSI commands. These devices must be treated differently. For example, removable media such as a CD-ROM or tape, Linux requires detecting whether the medium is removed. Different disk types have different master numbers, allowing Linux to direct block devices to the appropriate SCSI type. Initializing The SCSI Subsystem (Initialized SCSI Subsystem) Initializing the SCSI subsystem is quite complex, reflecting the cultural substance of the SCSI bus and device. Linux initializes the SCSI subsystem when started: it looks for the SCSI controller (SCSI host) in the system and detects each SCSI bus to find each device. These devices are then initialized, allowing the rest of the Linux core to access them through ordinary files and Buffer Cache block devices. This initialization process has four phases: First, Linux finds which SCSI HOST adapter or controller that can control when the core is established. Each built-in SCSI Host has an entry for SCSI_HOST_TEMPLATE in the buildin_scsi_hosts vector table. This SCSI_HOST_TEMPLATE data structure includes a pointer to the routine, which can perform and SCSI Host, for example, to detect what SCSI Host adheres to what S CSI device. These routines are called during the SCSI subsystem configuration, which is part of the SCSI device driver that supports this Host type. Each Isolated SCSI controller (with real SCSI device adheres), its SCSI_HOST_TEMPLATE data structure is added to the SCSI_HOSTS list, indicating a valid SCSI Host. Each example of each detected Host type is represented by a SCSI_ Host data structure in the SCSI_HostList list. For example, one system has two NCR810 PCI SCSI controllers, and there are two SCSI_HOST entries in this list, each of the controllers. Each SCSI_HOST pointing to SCSI_HOST_T EMPLATE indicates its device driver. Now every SCSI Host is found, the SCSI subsystem must find all SCSI devices on each Host bus. The SCSI device number ranges from 0 to 7, each device number or SCSI identifier is unique on its adhered SCSI bus. The SCSI identifier usually uses jumper settings on the device. The SCSI initialization code finds each SCSI device on a SCSI bus by sending a Test_Unit_ready command to each device. When a device responds, send an eNquiry command to it to complete its discrimination. This gives the L INUX a model and revision number of the Vendor's name and device.
The SCSI command is represented by a SCSI_CMND data structure that passes to the device driver by calling the device driver routine in the SCSI_HOST_TEMPLATE data structure of this SCSI Host. Each found SCSI device represents an SCSI_Device data structure, each pointing to its parent SCSI_HOST. All SCSI_Device data structures are added to the SCSI_DEVices list. Figure 8.4 shows the main data structure and other data structures. There are four SCSI device types: disks, tape, CD, and generic. Each SCSI type is separately registered, and there are different primary block devices. However, they only register them when they are found in one or more given SCSI device types. Each SCSI type, such as a SCSI disk, maintains its own device table. It uses these tables to direct the core block operation (file or buffer cache) to the correct device driver or SCSI Host. Each SCSI type is represented by an SCSI_TYPE_TEMPLATE data structure. It includes this type of SCSI device information and the address of the routine that performs a variety of tasks. The SCSI subsystem uses these templates to call the SCSI type routine of each SCSI device type. In other words, if the SCSI subsystem wants to adhere to an SCSI disk device, it calls the SCSI disk type routine. If you detect a type of one or more SCSI devices, its SCSI_TYPE_TEMPLATES is added to the SCSI _DeviceList list. The final phase of the SCSI subsystem initialization is to call the completion function of each registered SCSI_Device_template. For the SCSI disk type, all SCSI disks are rotated and record their disk size. It also puts a link to the link to the disk of the GenDisk data structure feeding of all SCSI disks, as shown in Figure 8.3. Delivering Block Device Requests You can use SCSI devices once Linux initialize the SCSI subsystem. Each effective SCSI device type is registered in the core, so Linux can direct the block device to it. These requests may be by BUFFER CACHE requests for BLK_DEV or by BLKDEVS. Take a SCSI disk drive that is partitioned by one or more EXT2 file system as an example. How does the core buffer request is directed to the correct SCSI disk when it is installed on the EXT2 partition installation? Each request to / write a piece of data from an SCSI disk partition will join a new Request data structure in the CURRENT_REQUEST list of this SCSI disk in the BLK_DEV meter. If this Request list is being processed, then buffer cache doesn't need to do. Otherwise it must let the SCSI disk subsystem processed its request queue. Each of the SCSI disks in the system is represented by a SCSI_Disk data structure. They are saved in the RSCSI_DISKS vector table, part of the secondary device number of the SCSI disk partition as an index. For example, / dev / sdb1 master number 8, the secondary device number 17, which is therefore 1. The data structure of each SCSI_DISK includes a pointer to the SCSI_Device data structure representing the device. The SCSI_DEV ICE also points to a "own" SCSI_HOST data structure.
The REQUEST data structure conversion in Buffer Cache becomes a SCSI_CMD data structure that needs to be sent to the SCSI command of the SCSI device, and queues in the SCSI_HOST data structure representing the device. Once the appropriate data block reads / writes, it will be processed by the respective SCSI device driver. 8.6 Network Devices A network device that is an entity that transmits and receives a packet as long as the network subsystem is related to Linux. Usually a physical device, such as an Ethernet card. However, some network devices are pure software, such as a LOOPB ACK device, which is used to send data to themselves. Each network device is represented by a DEVICE data structure. The network device driver is registered to Linux when the core starts network initialization. The DEVICE data structure includes information of this device and allows for a large number of supported network protocols to use the function of the service of this device. Most of these functions and use this network device to transfer data. The device uses a standard network support mechanism to transmit the received data to the appropriate protocol layer. All network data (package packets) of transmission and reception are represented by SK _BUFF data structure, which is a flexible data structure that allows the network protocol header to easily increase and delete. How to use network devices with network devices, how do they use SK_BUFF data structures to transfer data in a detailed description in the network chapter (Chapter 10). This chapter focuses on the DEVICE data structure and how the network device is discovered and initialized. See the include / Linux / NetDevice.h device data structure includes information about network devices: Name is not like blocks and character devices, their device special files are created with mknod commands, and the network device special file is found and initialized when the system network device is initialized. appear. Their names are standard, and each name represents its device type. Multiple devices of the same type are numbered from 0. Therefore, the Ethernet device is numbered / dev / eth0, / dev / eth1, / dev / eth2, and the like. Some common network devices are: / DEV / ETHN Ethernet Device / DEV / SLN SLIP Device / DEV / PPPN PPP Device / DEV / LOPBACK Device Bus Information This is the information you need to device driver control device. IRQ is an interrupt used by the device. Base Address is the address of the device's control and status register in the I / O stored address. The DMA channel is the DMA channel number used by this network device. All this information is set when the device is initialized at startup. Interface Flags These describe the characteristics and capabilities of this network device. IFF_UP interfaces up, running broadcast address valid IFF_DEBUG device debug option IFF_BROADCAST devices open IFF_LOOPBACK This is a loopback device IFF_POINTTOPOINT This is the point of connection (SLIP and PPP) IFF_NOTRAILERS No network trailers IFF_RUNNING allocate resources IFF_NOARP does not support the ARP protocol IF_PROMISC equipment In the promiscuous reception mode, it will receive all the packages, regardless of who their address is. IFF_allmulti Receives All IP Multicast Frame IFF_Multicast can receive IP Multicast Federation Protocal Information Each device describes how it can be used by the network protocol layer: The MTU does not include the maximum size of the network that requires an increased link layer to be transmitted. This maximum is used for the protocol layer such as IP to select a suitable package size for transmission.