ATA.h

Go to the documentation of this file.
00001 /* 
00002     Copyright (c) 2002-04, Thomas Kurschel. All rights reserved.
00003     Distributed under the terms of the MIT License.
00004     
00005     Part of ATA bus manager
00006     
00007 
00008     ATA bus manager interface.
00009     
00010     Every ATA host adapter consists of one or channels (like primary and
00011     secondary channel). For every channel, a managed node must be registered.
00012     As a fixed consumer, the ATA bus manager must be specified, and the
00013     Manager Interface must be ATA_BUS_CHANNEL_INTERFACE. 
00014 */
00015 
00016 #ifndef _ATA_H
00017 #define _ATA_H
00018 
00019 #include <bus_manager.h>
00020 #include <iovec.h>
00021 #include <lendian_bitfield.h>
00022 #include <pnp/pnp_driver.h>
00023 #include "r5_wrapper.h"
00024 #include <KernelExport.h>
00025 
00026 // ATA task file.
00027 // contains the command block interpreted under different conditions with
00028 // first byte being first command register, second byte second command register
00029 // etc.; for lba48, registers must be written twice, therefore there
00030 // are twice as many bytes as registers - the first eight bytes are those
00031 // that must be written first, the second eight bytes are those that
00032 // must be written second.
00033 typedef union {
00034     struct {
00035         uint8   features;
00036         uint8   sector_count;
00037         uint8   sector_number;
00038         uint8   cylinder_0_7;
00039         uint8   cylinder_8_15;
00040         LBITFIELD8_3(
00041             head                : 4,
00042             device              : 1,
00043             mode                : 3
00044         );
00045         uint8   command;
00046     } chs;
00047     struct {
00048         uint8   features;
00049         uint8   sector_count;
00050         uint8   lba_0_7;
00051         uint8   lba_8_15;
00052         uint8   lba_16_23;
00053         LBITFIELD8_3(
00054             lba_24_27           : 4,
00055             device              : 1,
00056             mode                : 3
00057         );
00058         uint8   command;
00059     } lba;
00060     struct {
00061         LBITFIELD8_3(
00062             dma                 : 1,
00063             ovl                 : 1,
00064             _0_res2             : 6
00065         );
00066         LBITFIELD8_2(
00067             _1_res0             : 3,
00068             tag                 : 5
00069         );
00070         uint8   _2_res;
00071         uint8   byte_count_0_7;
00072         uint8   byte_count_8_15;
00073         LBITFIELD8_6(
00074             lun                 : 3,
00075             _5_res3             : 1,
00076             device              : 1,
00077             _5_one5             : 1,
00078             _5_res6             : 1,
00079             _5_one7             : 1
00080         );
00081         uint8   command;
00082     } packet;
00083     struct {
00084         LBITFIELD8_5(
00085             ili                 : 1,
00086             eom                 : 1,
00087             abrt                : 1,
00088             _0_res3             : 1,
00089             sense_key           : 4
00090         );
00091         LBITFIELD8_4(
00092             cmd_or_data         : 1,    // 1 - cmd, 0 - data
00093             input_or_output     : 1,    // 0 - input (to device), 1 - output
00094             release             : 1,
00095             tag                 : 5
00096         );
00097         uint8   _2_res;
00098         uint8   byte_count_0_7;
00099         uint8   byte_count_8_15;
00100         LBITFIELD8_5(
00101             _4_res0             : 4,
00102             device              : 1,
00103             _4_obs5             : 1,
00104             _4_res6             : 1,
00105             _4_obs7             : 1
00106         );
00107         LBITFIELD8_7(
00108             chk                 : 1,
00109             _7_res1                 : 2,
00110             drq                 : 1,
00111             serv                : 1,
00112             dmrd                : 1,
00113             drdy                : 1,
00114             bsy                 : 1
00115         );
00116     } packet_res;
00117     struct {
00118         uint8   sector_count;
00119         LBITFIELD8_4(                   // only <tag> is defined for write
00120             cmd_or_data         : 1,    // 1 - cmd, 0 - data
00121             input_or_output     : 1,    // 0 - input (to device), 1 - output
00122             release             : 1,
00123             tag                 : 5
00124         );
00125         uint8   lba_0_7;
00126         uint8   lba_8_15;
00127         uint8   lba_16_23;
00128         LBITFIELD8_3(
00129             lba_24_27           : 4,
00130             device              : 1,
00131             mode                : 3
00132         );
00133         uint8   command;
00134     } queued;
00135     struct {
00136         // low order bytes
00137         uint8   features;
00138         uint8   sector_count_0_7;
00139         uint8   lba_0_7;
00140         uint8   lba_8_15;
00141         uint8   lba_16_23;
00142         LBITFIELD8_3(
00143             _5low_res0          : 4,
00144             device              : 1,
00145             mode                : 3
00146         );
00147         uint8   command;
00148         
00149         // high order bytes
00150         uint8   _0high_res;
00151         uint8   sector_count_8_15;
00152         uint8   lba_24_31;
00153         uint8   lba_32_39;
00154         uint8   lba_40_47;
00155     } lba48;
00156     struct {
00157         // low order bytes
00158         uint8   sector_count_0_7;
00159         LBITFIELD8_4(
00160             cmd_or_data         : 1,    // 1 - cmd, 0 - data
00161             input_or_output     : 1,    // 0 - input (to device), 1 - output
00162             release             : 1,
00163             tag                 : 5
00164         );
00165         uint8   lba_0_7;
00166         uint8   lba_8_15;
00167         uint8   lba_16_23;
00168         LBITFIELD8_3(
00169             _5low_res0              : 4,
00170             device                  : 1,
00171             mode                    : 3
00172         );
00173         uint8   command;
00174         
00175         // high order bytes
00176         uint8   sector_count_8_15;
00177         uint8   _1high_res;
00178         uint8   lba_24_31;
00179         uint8   lba_32_39;
00180         uint8   lba_40_47;
00181     } queued48;
00182     struct {
00183         uint8   _0_res[3];
00184         uint8   ver;            // RMSN version
00185         LBITFIELD8_3(
00186             pena    : 1,        // previously enabled
00187             lock    : 1,        // capable of locking
00188             pej     : 1         // can physically eject
00189         );
00190     } set_MSN_res;
00191     struct {
00192         uint8   r[7+5];
00193     } raw;
00194     struct {
00195         uint8   features;
00196         uint8   sector_count;
00197         uint8   sector_number;
00198         uint8   cylinder_low;
00199         uint8   cylinder_high;
00200         uint8   device_head;
00201         uint8   command;
00202     } write;
00203     struct {
00204         uint8   error;
00205         uint8   sector_count;
00206         uint8   sector_number;
00207         uint8   cylinder_low;
00208         uint8   cylinder_high;
00209         uint8   device_head;
00210         uint8   status;
00211     } read;
00212 } ata_task_file;
00213 
00214 // content of "mode" field
00215 enum {
00216     ata_mode_chs = 5,
00217     ata_mode_lba = 7
00218 };
00219 
00220 // mask for ata_task_file fields to be written
00221 typedef enum {
00222     ata_mask_features           = 0x01,
00223     ata_mask_sector_count       = 0x02,
00224 
00225     // CHS
00226     ata_mask_sector_number      = 0x04,
00227     ata_mask_cylinder_low       = 0x08,
00228     ata_mask_cylinder_high      = 0x10,
00229     
00230     // LBA
00231     ata_mask_LBA_low            = 0x04,
00232     ata_mask_LBA_mid            = 0x08,
00233     ata_mask_LBA_high           = 0x10,
00234 
00235     // packet
00236     ata_mask_byte_count         = 0x18,
00237     
00238     // packet and dma queued result
00239     ata_mask_error              = 0x01,
00240     ata_mask_ireason            = 0x02,
00241 
00242     ata_mask_device_head        = 0x20,
00243     ata_mask_command            = 0x40,
00244     
00245     ata_mask_status             = 0x40,
00246 
00247     // for 48 bits, the following flags tell which registers to load twice
00248     ata_mask_features_48        = 0x80 | ata_mask_features,
00249     ata_mask_sector_count_48    = 0x100 | ata_mask_sector_count,
00250     ata_mask_LBA_low_48         = 0x200 | ata_mask_LBA_low,
00251     ata_mask_LBA_mid_48         = 0x400 | ata_mask_LBA_mid,
00252     ata_mask_LBA_high_48        = 0x800 | ata_mask_LBA_high,
00253     
00254     ata_mask_HOB                = 0xf80
00255 
00256     //ata_mask_all          = 0x7f
00257 } ata_reg_mask;
00258 
00259 // status register
00260 enum {
00261     ata_status_err      = 0x01,     // error
00262     ata_status_index    = 0x02,     // obsolete
00263     ata_status_corr     = 0x04,     // obsolete
00264     ata_status_drq      = 0x08,     // data request
00265     ata_status_dsc      = 0x10,     // reserved
00266     ata_status_service  = 0x10,     // ready to service device
00267     ata_status_dwf      = 0x20,     // reserved
00268     ata_status_dma      = 0x20,     // reserved
00269     ata_status_dmrd     = 0x20,     // packet: DMA ready
00270     ata_status_df       = 0x20,     // packet: disk failure
00271     ata_status_drdy     = 0x40,     // device ready
00272     ata_status_bsy      = 0x80      // busy
00273 } ata_status_mask;
00274 
00275 // device control register
00276 enum {
00277                                     // bit 0 must be zero
00278     ata_devctrl_nien    = 0x02,     // disable INTRQ
00279     ata_devctrl_srst    = 0x04,     // software device reset
00280     ata_devctrl_bit3    = 0x08,     // don't know, but must be set
00281                                     // bits inbetween are reserved
00282     ata_devctrl_hob     = 0x80      // read high order byte (for 48-bit lba)
00283 } ata_devcntrl_mask;
00284 
00285 // error register - most bits are command specific
00286 enum {
00287     // always used
00288     ata_error_abrt      = 0x04,     // command aborted
00289     
00290     // used for Ultra DMA modes
00291     ata_error_icrc      = 0x80,     // interface CRC error
00292 
00293     // used by reading data transfers
00294     ata_error_unc       = 0x40,     // uncorrectable data error
00295     // used by writing data transfers
00296     ata_error_wp        = 0x40,     // media write protect
00297 
00298     // used by all data transfer commands
00299     ata_error_mc        = 0x20,     // medium changed
00300     ata_error_idnf      = 0x10,     // CHS translation not init./ invalid CHS address
00301     ata_error_mcr       = 0x08,     // media change requested
00302     ata_error_nm        = 0x02,     // no media (for removable media devices)
00303 } ata_error_mask;
00304 
00305 
00306 
00307 // ATA bus node
00308 
00309 
00310 // channel cookie, issued by ata bus manager
00311 typedef struct ata_bus_info *ata_bus;
00312 
00313 // interface for channel drivers.
00314 // must be specified as Manager Interface for ATA channel nodes
00315 typedef struct ata_bus_for_channel_interface {
00316     // IRQ was raised.
00317     //      status  - status read from host adapter (_not_ alt_status, as 
00318     //                reading normal status acknowledges IRQ request of device)
00319     // must be called directly from IRQ handler; handling of interrupt 
00320     // sharing is up to the channel driver
00321     status_t    (*irq_handler)( 
00322         ata_bus         bus, 
00323         uint8           status 
00324     );
00325 } ata_bus_for_channel_interface;
00326 
00327 #define ATA_BUS_FOR_CHANNEL_INTERFACE "ata/bus/for_channel/v1"
00328 
00329 // name of ATA bus manager.
00330 // must be specified as fixed consumer of ATA channel nodes
00331 #define ATA_BUS_MANAGER_MODULE_NAME "ata/ata_bus_manager/bus/v1"
00332 
00333 
00334 
00335 // ATA channel node
00336 
00337 
00338 // attributes:
00339 
00340 // node type
00341 #define ATA_CHANNEL_TYPE "ata/channel"
00342 // maximum number of devices connected to host adapter (uint8, optional, default:2)
00343 #define ATA_CHANNEL_MAX_DEVICES_ITEM "ata/max_devices"
00344 // set to not-0 if DMA is supported (uint8, optional, default:0)
00345 // (if so, publish necessary blkdev restriction too)
00346 #define ATA_CHANNEL_CAN_DMA_ITEM "ata/can_DMA"
00347 // set to not-0 if CQ is supported (uint8, optional, default:1)
00348 #define ATA_CHANNEL_CAN_CQ_ITEM "ata/can_CQ"
00349 
00350 // channel cookie to be defined by host adapter driver
00351 typedef struct ata_channel_info *ata_channel_cookie;
00352 
00353 
00354 // interface of channel drivers.
00355 // used by ATA bus manager
00356 typedef struct ata_channel_interface {
00357     // write task file.
00358     //      tf      - task file, i.e. command block registers
00359     //      mask    - bitmask which command registers are to be written
00360     status_t    (*write_command_block_regs)(
00361                     ata_channel_cookie  channel, 
00362                     ata_task_file       *tf, 
00363                     ata_reg_mask        mask
00364                 );
00365     // read task file.
00366     // see write_command_block_regs()
00367     status_t    (*read_command_block_regs)(
00368                     ata_channel_cookie  channel, 
00369                     ata_task_file       *tf, 
00370                     ata_reg_mask        mask
00371                 );
00372     // read alternate status.
00373     //      return  - content of alternate status register
00374     uint8       (*get_altstatus)(
00375                     ata_channel_cookie channel
00376                 );
00377     // write device control register.
00378     //      val     - new content of device control register
00379     status_t    (*write_device_control)(
00380                     ata_channel_cookie  channel, 
00381                     uint8               val
00382                 );
00383 
00384     // write data in PIO mode.
00385     //      data        - pointer to data
00386     //      count       - number of words (i.e. 16 bit values) to write
00387     //      force_16bit - if true, don't write 32 bits at once
00388     status_t    (*write_pio)(
00389                     ata_channel_cookie  channel, 
00390                     uint16 *            data, 
00391                     int                 count, 
00392                     bool                force_16bit 
00393                 );
00394     // read data in PIO mode.
00395     // see write_pio()
00396     status_t    (*read_pio)(
00397                     ata_channel_cookie  channel, 
00398                     uint16              *data,
00399                     int                 count,
00400                     bool                force_16bit 
00401                 );
00402 
00403     // prepare DMA transmission.
00404     //      sg_list         - scatter/gather list, containing physical addresses
00405     //      sg_list_count   - number of S/G entries
00406     //      write           - if true, DMA transmission writes data to device
00407     status_t    (*prepare_dma)(
00408                     ata_channel_cookie  channel, 
00409                     const physical_entry *sg_list, 
00410                     size_t              sg_list_count,
00411                     bool                write
00412                 );
00413     // start DMA transmission.
00414     status_t    (*start_dma)(
00415                     ata_channel_cookie channel
00416                 );
00417     // finish DMA transmission.
00418     // the channel driver must verify that transmission was successful and
00419     // return proper error codes
00420     status_t    (*finish_dma)(
00421                     ata_channel_cookie channel
00422                 );
00423 } ata_channel_interface;
00424 
00425 #define ATA_CHANNEL_INTERFACE   "ata/channel/v1"
00426 
00427 
00428 #endif

Copyright 2005 by yellowTAB GmbH, Be Inc., Palm Source Inc. and their respective legal successors
All rights reserved.