Ignore:
Timestamp:
Nov 29, 2016, 5:30:22 AM (9 years ago)
Author:
David Azarewicz
Message:

Major reorganization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/os2ahci/ata.c

    r176 r178  
    44 * Copyright (c) 2011 thi.guten Software Development
    55 * Copyright (c) 2011 Mensys B.V.
    6  * Portions copyright (c) 2013-2015 David Azarewicz
     6 * Copyright (c) 2013-2016 David Azarewicz
    77 *
    88 * Authors: Christian Mueller, Markus Thielen
     
    3535/* -------------------------- function prototypes -------------------------- */
    3636
    37 static int ata_cmd_read (IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
    38                          ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
    39                          ULONG sg_cnt);
    40 
    41 static int ata_cmd_write(IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
    42                          ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
     37static int ata_cmd_read(IORBH *pIorb, AD_INFO *ai, int p, int d, int slot,
     38                        ULONG sector, ULONG count, SCATGATENTRY *sg_list,
     39                        ULONG sg_cnt);
     40
     41static int ata_cmd_write(IORBH *pIorb, AD_INFO *ai, int p, int d, int slot,
     42                         ULONG sector, ULONG count, SCATGATENTRY *sg_list,
    4343                         ULONG sg_cnt, int write_through);
    4444
     
    8181int v_ata_cmd(AD_INFO *ai, int p, int d, int slot, int cmd, va_list va)
    8282{
    83   AHCI_PORT_DMA _far *dma_base_virt;
    84   AHCI_CMD_HDR _far *cmd_hdr;
    85   AHCI_CMD_TBL _far *cmd_tbl;
    86   SCATGATENTRY _far *sg_list = NULL;
     83  AHCI_PORT_DMA *dma_base_virt;
     84  AHCI_CMD_HDR *cmd_hdr;
     85  AHCI_CMD_TBL *cmd_tbl;
     86  SCATGATENTRY *sg_list = NULL;
    8787  SCATGATENTRY sg_single;
    8888  ATA_PARM ap;
    8989  ATA_CMD ata_cmd;
    90   void _far *atapi_cmd = NULL;
     90  void *atapi_cmd = NULL;
    9191  u32 dma_base_phys;
    92   u16 atapi_cmd_len = 0;
    93   u16 ahci_flags = 0;
    94   u16 sg_cnt = 0;
    95   int i;
    96   int n;
     92  u32 atapi_cmd_len = 0;
     93  u32 ahci_flags = 0;
     94  u32 sg_cnt = 0;
     95  u32 i;
     96  u32 n;
    9797
    9898  /* --------------------------------------------------------------------------
     
    102102   */
    103103  memset(&ata_cmd, 0x00, sizeof(ata_cmd));
    104   ata_cmd.cmd = (u8) cmd;
     104  ata_cmd.cmd = cmd;
    105105
    106106  /* parse variable arguments */
    107   do {
    108     switch ((ap = va_arg(va, ATA_PARM))) {
     107  do
     108  {
     109    switch ((ap = va_arg(va, ATA_PARM)))
     110    {
    109111
    110112    case AP_AHCI_FLAGS:
    111       ahci_flags |= va_arg(va, u16);
     113      ahci_flags |= va_arg(va, u32);
    112114      break;
    113115
    114116    case AP_WRITE:
    115       if (va_arg(va, u16) != 0) {
     117      if (va_arg(va, u32) != 0)
     118      {
    116119        ahci_flags |= AHCI_CMD_WRITE;
    117120      }
     
    120123    case AP_FEATURES:
    121124      /* ATA features word */
    122       ata_cmd.features |= va_arg(va, u16);
     125      ata_cmd.features |= va_arg(va, u32);
    123126      break;
    124127
    125128    case AP_COUNT:
    126129      /* transfer count */
    127       ata_cmd.count = va_arg(va, u16);
     130      ata_cmd.count = va_arg(va, u32);
    128131      break;
    129132
     
    131134      /* 28-bit sector address */
    132135      ata_cmd.lba_l = va_arg(va, u32);
    133       if (ata_cmd.lba_l & 0xf0000000UL) {
    134         dprintf("error: LBA-28 address %ld has more than 28 bits\n", ata_cmd.lba_l);
     136      if (ata_cmd.lba_l & 0xf0000000UL)
     137      {
     138        DPRINTF(0,"error: LBA-28 address %d has more than 28 bits\n", ata_cmd.lba_l);
    135139        return(ATA_CMD_INVALID_PARM);
    136140      }
     
    144148      /* 48-bit sector address */
    145149      ata_cmd.lba_l = va_arg(va, u32);
    146       ata_cmd.lba_h = va_arg(va, u16);
     150      ata_cmd.lba_h = va_arg(va, u32);
    147151      break;
    148152
     
    150154      /* ATA device byte; note that this byte contains the highest
    151155       * 4 bits of LBA-28 address; we have to leave them alone here. */
    152       ata_cmd.device |= va_arg(va, u16) & 0xf0U;
     156      ata_cmd.device |= va_arg(va, u32) & 0xf0;
    153157      break;
    154158
    155159    case AP_SGLIST:
    156160      /* scatter/gather list in SCATGATENTRY/count format */
    157       sg_list = va_arg(va, void _far *);
    158       sg_cnt = va_arg(va, u16);
     161      sg_list = va_arg(va, void *);
     162      sg_cnt = va_arg(va, u32);
    159163      break;
    160164
    161165    case AP_VADDR:
    162166      /* virtual buffer address in addr/len format (up to 4K) */
    163       sg_single.ppXferBuf = virt_to_phys(va_arg(va, void _far *));
    164       sg_single.XferBufLen = va_arg(va, u16);
     167      sg_single.ppXferBuf = MemPhysAdr(va_arg(va, void *));
     168      sg_single.XferBufLen = va_arg(va, u32);
    165169      sg_list = &sg_single;
    166170      sg_cnt = 1;
     
    169173    case AP_ATAPI_CMD:
    170174      /* ATAPI command */
    171       atapi_cmd = va_arg(va, void _far *);
    172       atapi_cmd_len = va_arg(va, u16);
     175      atapi_cmd = va_arg(va, void *);
     176      atapi_cmd_len = va_arg(va, u32);
    173177      ahci_flags |= AHCI_CMD_ATAPI;
    174178      break;
     
    176180    case AP_ATA_CMD:
    177181      /* ATA command "pass-through" */
    178       memcpy(&ata_cmd, va_arg(va, void _far *), sizeof(ATA_CMD));
     182      memcpy(&ata_cmd, va_arg(va, void *), sizeof(ATA_CMD));
    179183      break;
    180184
     
    183187
    184188    default:
    185       dprintf("error: v_ata_cmd() called with invalid parameter type (%d)\n", (int) ap);
     189      DPRINTF(0,"error: v_ata_cmd() called with invalid parameter type (%d)\n", (int) ap);
    186190      return(ATA_CMD_INVALID_PARM);
    187191    }
     
    212216
    213217  /* AHCI command header */
    214   cmd_hdr = dma_base_virt->cmd_hdr + slot;
     218  cmd_hdr = &dma_base_virt->cmd_hdr[slot];
    215219  memset(cmd_hdr, 0x00, sizeof(*cmd_hdr));
    216220  cmd_hdr->options  = ((d & 0x0f) << 12);
     
    218222  cmd_hdr->options |= 5;           /* length of command FIS in 32-bit words */
    219223  cmd_hdr->tbl_addr = dma_base_phys + offsetof(AHCI_PORT_DMA, cmd_tbl[slot]);
     224  /* DAZ can use MemPhysAdr(&dma_base_virt->cmd_tbl[slot]), but is probably slower. */
    220225
    221226  /* AHCI command table */
    222   cmd_tbl = dma_base_virt->cmd_tbl + slot;
     227  cmd_tbl = &dma_base_virt->cmd_tbl[slot];
    223228  memset(cmd_tbl, 0x00, sizeof(*cmd_tbl));
    224229  ata_cmd_to_fis(cmd_tbl->cmd_fis, &ata_cmd, d);
    225230
    226   if (atapi_cmd != NULL) {
     231  if (atapi_cmd != NULL)
     232  {
    227233    /* copy ATAPI command */
    228234    memcpy(cmd_tbl->atapi_cmd, atapi_cmd, atapi_cmd_len);
     
    250256   *    successfully mapped.
    251257   */
    252   for (i = n = 0; i < sg_cnt; i++) {
     258  for (i = n = 0; i < sg_cnt; i++)
     259  {
    253260    u32 sg_addr = sg_list[i].ppXferBuf;
    254261    u32 sg_size = sg_list[i].XferBufLen;
    255262
    256     do {
     263    do
     264    {
    257265      u32 chunk = (sg_size > AHCI_MAX_SG_ELEMENT_LEN) ? AHCI_MAX_SG_ELEMENT_LEN
    258266                                                      : sg_size;
    259       if (n >= AHCI_MAX_SG) {
     267      if (n >= AHCI_MAX_SG)
     268      {
    260269        /* couldn't store all S/G elements in our DMA buffer */
    261         ddprintf("ata_cmd(): too many S/G elements\n");
     270        DPRINTF(0,"ata_cmd(): too many S/G elements\n");
    262271        return(i - 1);
    263272      }
    264       if ((sg_addr & 1) || (chunk & 1)) {
    265         ddprintf("error: ata_cmd() called with unaligned S/G element(s)\n");
     273      if ((sg_addr & 1) || (chunk & 1))
     274      {
     275        DPRINTF(0,"error: ata_cmd() called with unaligned S/G element(s)\n");
    266276        return(ATA_CMD_UNALIGNED_ADDR);
    267277      }
     
    275285
    276286  /* set final S/G count in AHCI command header */
    277   cmd_hdr->options |= (u32) n << 16;
    278 
    279   if (debug >= 2) {
    280     aprintf("ATA command for %d.%d.%d, slot %d:\n", ad_no(ai), p, d, slot);
    281     phex(cmd_hdr, offsetof(AHCI_CMD_HDR, reserved), "cmd_hdr:   ");
    282     phex(&ata_cmd, sizeof(ata_cmd), "ata_cmd:   ");
    283     if (atapi_cmd != NULL) {
    284       phex(atapi_cmd, atapi_cmd_len, "atapi_cmd: ");
    285     }
    286     if (n > 0) {
    287       phex(cmd_tbl->sg_list, sizeof(*cmd_tbl->sg_list) * n, "sg_list:   ");
     287  cmd_hdr->options |= n << 16;
     288
     289  if (D32g_DbgLevel >= 2)
     290  {
     291    DPRINTF(2,"ATA command for %d.%d.%d, slot %d:\n", ad_no(ai), p, d, slot);
     292    dHexDump(0,cmd_hdr, offsetof(AHCI_CMD_HDR, reserved), "cmd_hdr: ");
     293    dHexDump(0,&ata_cmd, sizeof(ata_cmd), "ata_cmd: ");
     294    if (atapi_cmd != NULL)
     295    {
     296      dHexDump(0,atapi_cmd, atapi_cmd_len, "atapi_cmd: ");
     297    }
     298    if (n > 0)
     299    {
     300      dHexDump(0,cmd_tbl->sg_list, sizeof(*cmd_tbl->sg_list) * n, "sg_list: ");
    288301    }
    289302  }
     
    312325 *       +----------------+----------------+----------------+----------------+
    313326 */
    314 void ata_cmd_to_fis(u8 _far *fis, ATA_CMD _far *ata_cmd, int d)
     327void ata_cmd_to_fis(u8 *fis, ATA_CMD *ata_cmd, int d)
    315328{
    316329  fis[0]  = 0x27;                  /* register - host to device FIS */
     
    344357 *       lists.
    345358 */
    346 u16 ata_get_sg_indx(IORB_EXECUTEIO _far *io)
     359u16 ata_get_sg_indx(IORB_EXECUTEIO *io)
    347360{
    348361  ULONG offset = io->BlocksXferred * io->BlockSize;
     362  SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(io->pSGList);
    349363  USHORT i;
    350364
    351   for (i = 0; i < io->cSGList && offset > 0; i++) {
    352     offset -= io->pSGList[i].XferBufLen;
     365  for (i = 0; i < io->cSGList && offset > 0; i++)
     366  {
     367    offset -= pSGList[i].XferBufLen;
    353368  }
    354369
     
    380395 *       boundaries which will still fit into our HW S/G list.
    381396 */
    382 void ata_max_sg_cnt(IORB_EXECUTEIO _far *io, USHORT sg_indx, USHORT sg_max,
    383                     USHORT _far *sg_cnt, USHORT _far *sector_cnt)
     397void ata_max_sg_cnt(IORB_EXECUTEIO *io, USHORT sg_indx, USHORT sg_max,
     398                    USHORT *sg_cnt, USHORT *sector_cnt)
    384399{
    385400  ULONG max_sector_cnt = 0;
     
    387402  ULONG offset = 0;
    388403  USHORT i;
    389 
    390   for (i = sg_indx; i < io->cSGList; i++) {
    391     if (i - sg_indx >= sg_max) {
     404  SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(io->pSGList);
     405
     406  for (i = sg_indx; i < io->cSGList; i++)
     407  {
     408    if (i - sg_indx >= sg_max)
     409    {
    392410      /* we're beyond the number of S/G elements we can map */
    393411      break;
    394412    }
    395413
    396     offset += io->pSGList[i].XferBufLen;
    397     if (offset % io->BlockSize == 0) {
     414    offset += pSGList[i].XferBufLen;
     415    if (offset % io->BlockSize == 0)
     416    {
    398417      /* this S/G element ends on a sector boundary */
    399418      max_sector_cnt = offset / io->BlockSize;
     
    414433 * and handled by atapi_get_geometry().
    415434 */
    416 int ata_get_geometry(IORBH _far *iorb, int slot)
    417 {
    418   ADD_WORKSPACE _far *aws = add_workspace(iorb);
     435int ata_get_geometry(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
     436{
     437  ADD_WORKSPACE *aws = add_workspace(pIorb);
    419438  int rc;
    420439
    421440  /* allocate buffer for ATA identify information */
    422   if ((aws->buf = malloc(ATA_ID_WORDS * sizeof(u16))) == NULL) {
    423     iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
     441  if ((aws->buf = MemAlloc(ATA_ID_WORDS * sizeof(u16))) == NULL)
     442  {
     443    iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
    424444    return(-1);
    425445  }
     
    427447  /* request ATA identify information */
    428448  aws->ppfunc = ata_get_geometry_pp;
    429   rc = ata_cmd(ad_infos + iorb_unit_adapter(iorb),
    430                iorb_unit_port(iorb),
    431                iorb_unit_device(iorb),
     449  rc = ata_cmd(ad_infos + iorb_unit_adapter(pIorb),
     450               iorb_unit_port(pIorb),
     451               iorb_unit_device(pIorb),
    432452               slot,
    433453               ATA_CMD_ID_ATA,
    434                AP_VADDR, (void _far *) aws->buf, ATA_ID_WORDS * sizeof(u16),
     454               AP_VADDR, (void *) aws->buf, ATA_ID_WORDS * sizeof(u16),
    435455               AP_END);
    436456
    437   if (rc != 0) {
    438     iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
     457  if (rc != 0)
     458  {
     459    iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
    439460  }
    440461
     
    445466 * geometry to the last full cylinder.
    446467 */
    447 int adjust_cylinders(GEOMETRY _far *geometry, ULONG TotalSectors) {
     468int adjust_cylinders(GEOMETRY *geometry, ULONG TotalSectors)
     469{
    448470  USHORT SecPerCyl;
    449471  int rc = FALSE;
     
    451473  geometry->TotalSectors = TotalSectors;
    452474  SecPerCyl = geometry->SectorsPerTrack * geometry->NumHeads;
    453   if (SecPerCyl > 0) {
     475  if (SecPerCyl > 0)
     476  {
    454477    ULONG TotalCylinders  = TotalSectors / SecPerCyl;
    455478
    456479    geometry->TotalSectors   = TotalCylinders * SecPerCyl;
    457480    geometry->TotalCylinders = TotalCylinders;
    458     if (TotalCylinders >> 16) {
     481    if (TotalCylinders >> 16)
     482    {
    459483      geometry->TotalCylinders = 65535;
    460484      rc = TRUE;
     
    470494#define BIOS_MAX_NUMHEADS         255
    471495#define BIOS_MAX_SECTORSPERTRACK   63
    472 void log_geom_calculate_LBA_assist(GEOMETRY _far *geometry, ULONG TotalSectors)
     496void log_geom_calculate_LBA_assist(GEOMETRY *geometry, ULONG TotalSectors)
    473497{
    474498  UCHAR numSpT   = BIOS_MAX_SECTORSPERTRACK;
     
    476500  ULONG Cylinders;
    477501
    478   if (TotalSectors <= (BIOS_MAX_CYLINDERS * 128 * BIOS_MAX_SECTORSPERTRACK)) {
     502  if (TotalSectors <= (BIOS_MAX_CYLINDERS * 128 * BIOS_MAX_SECTORSPERTRACK))
     503  {
    479504    USHORT temp = (TotalSectors - 1) / (BIOS_MAX_CYLINDERS * BIOS_MAX_SECTORSPERTRACK);
    480505
     
    485510  }
    486511
    487   do {
     512  do
     513  {
    488514    Cylinders = TotalSectors / (USHORT)(numHeads * numSpT);
    489     if (Cylinders >> 16) {
     515    if (Cylinders >> 16)
     516    {
    490517      if (numSpT < 128)
    491518        numSpT = (numSpT << 1) | 1;
     
    500527}
    501528
    502 int check_lvm(IORBH _far *iorb, ULONG sector)
    503 {
    504   DLA_Table_Sector *pDLA = (DLA_Table_Sector*)add_workspace(iorb)->buf;
    505   AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
    506   GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
    507   int p = iorb_unit_port(iorb);
     529int check_lvm(IORBH *pIorb, ULONG sector)
     530{
     531  DLA_Table_Sector *pDLA = (DLA_Table_Sector*)add_workspace(pIorb)->buf;
     532  AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
     533  GEOMETRY *geometry = ((IORB_GEOMETRY*)pIorb)->pGeometry;
     534  int p = iorb_unit_port(pIorb);
    508535  int rc;
    509536
    510537  rc = ahci_exec_polled_cmd(ai, p, 0, 500, ATA_CMD_READ,
    511          AP_SECTOR_28, (u32) sector-1,
    512          AP_COUNT, (u16) 1,
    513          AP_VADDR, (void _far *) pDLA, 512,
     538         AP_SECTOR_28, sector-1,
     539         AP_COUNT, 1,
     540         AP_VADDR, (void *)pDLA, 512,
    514541         AP_DEVICE, 0x40,
    515542         AP_END);
    516543  if (rc) return 0;
    517544
    518   ddphex(pDLA, sizeof(DLA_Table_Sector), "DLA sector %d:\n", sector-1);
     545  DHEXDUMP(3,pDLA, sizeof(DLA_Table_Sector), "DLA sector %d:\n", sector-1);
    519546
    520547  if ((pDLA->DLA_Signature1 == DLA_TABLE_SIGNATURE1) && (pDLA->DLA_Signature2 == DLA_TABLE_SIGNATURE2)) {
    521     ddprintf("is_lvm_geometry found at sector %d\n", sector-1);
     548    DPRINTF(3,"is_lvm_geometry found at sector %d\n", sector-1);
    522549    geometry->TotalCylinders = pDLA->Cylinders;
    523550    geometry->NumHeads = pDLA->Heads_Per_Cylinder;
     
    536563 * return the saved values when ata_get_geometry() is called.
    537564 */
    538 int is_lvm_geometry(IORBH _far *iorb)
    539 {
    540   GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
     565int is_lvm_geometry(IORBH *pIorb)
     566{
     567  GEOMETRY *geometry = ((IORB_GEOMETRY*)pIorb)->pGeometry;
    541568  ULONG sector;
    542569
    543570  if (init_complete) return 0; /* We cannot use ahci_exec_polled_cmd() after init_complete */
    544571
    545   if (use_lvm_info) {
     572  if (use_lvm_info)
     573  {
    546574    #ifdef DEBUG
    547     AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
    548     int p = iorb_unit_port(iorb);
    549     int d = iorb_unit_device(iorb);
    550     ddprintf("is_lvm_geometry (%d.%d.%d)\n", ad_no(ai), p, d);
     575    AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
     576    int p = iorb_unit_port(pIorb);
     577    int d = iorb_unit_device(pIorb);
     578    DPRINTF(3,"is_lvm_geometry (%d.%d.%d)\n", ad_no(ai), p, d);
    551579    #endif
    552580
    553581    /* First check the sector reported by the hardware */
    554     if (check_lvm(iorb, geometry->SectorsPerTrack)) return 1;
    555 
    556     for (sector = 255; sector >= 63; sector >>= 1) {
     582    if (check_lvm(pIorb, geometry->SectorsPerTrack)) return 1;
     583
     584    for (sector = 255; sector >= 63; sector >>= 1)
     585    {
    557586      if (sector == geometry->SectorsPerTrack) continue;
    558       if (check_lvm(iorb, sector)) return 1;
     587      if (check_lvm(pIorb, sector)) return 1;
    559588    }
    560589  }
     
    567596 * information to OS/2 IOCC_GEOMETRY information.
    568597 */
    569 void ata_get_geometry_pp(IORBH _far *iorb)
    570 {
    571   GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
    572   USHORT geometry_len =  ((IORB_GEOMETRY _far *) iorb)->GeometryLen;
    573   u16 *id_buf = add_workspace(iorb)->buf;
    574   int a = iorb_unit_adapter(iorb);
    575   int p = iorb_unit_port(iorb);
     598void ata_get_geometry_pp(IORBH FAR16DATA *vIorb, IORBH *pIorb)
     599{
     600  GEOMETRY *geometry = ((IORB_GEOMETRY*)pIorb)->pGeometry;
     601  USHORT geometry_len =  ((IORB_GEOMETRY *)pIorb)->GeometryLen;
     602  u16 *id_buf = add_workspace(pIorb)->buf;
     603  int a = iorb_unit_adapter(pIorb);
     604  int p = iorb_unit_port(pIorb);
    576605  char *Method;
    577606
     
    607636
    608637  /* extract total number of sectors */
    609   if (id_buf[ATA_ID_CFS_ENABLE_2] & 0x400) {
     638  if (id_buf[ATA_ID_CFS_ENABLE_2] & 0x400)
     639  {
    610640    /* 48-bit LBA supported */
    611     if (ATA_CAPACITY48_H(id_buf) != 0) {
     641    if (ATA_CAPACITY48_H(id_buf) != 0)
     642    {
    612643      /* more than 32 bits for number of sectors */
    613       dprintf("warning: limiting disk %d.%d.%d to 2TB\n",
    614               iorb_unit_adapter(iorb), iorb_unit_port(iorb),
    615               iorb_unit_device(iorb));
     644      DPRINTF(0,"warning: limiting disk %d.%d.%d to 2TB\n",
     645              iorb_unit_adapter(pIorb), iorb_unit_port(pIorb),
     646              iorb_unit_device(pIorb));
    616647      geometry->TotalSectors = 0xffffffffUL;
    617     } else {
     648    }
     649    else
     650    {
    618651      geometry->TotalSectors = ATA_CAPACITY48_L(id_buf);
    619652    }
    620   } else {
     653  }
     654  else
     655  {
    621656    /* 28-bit LBA */
    622657    geometry->TotalSectors = ATA_CAPACITY(id_buf) & 0x0fffffffUL;
     
    625660  Method = "None";
    626661  /* fabricate the remaining geometry fields */
    627   if (track_size[a][p] != 0) {
     662  if (track_size[a][p] != 0)
     663  {
    628664    /* A specific track size has been requested for this port; this is
    629665     * typically done for disks with 4K sectors to make sure partitions
     
    634670    geometry->TotalCylinders  = geometry->TotalSectors / ((u32) geometry->NumHeads * (u32) geometry->SectorsPerTrack);
    635671    Method = "Custom";
    636   } else if (CUR_HEADS(id_buf) > 0 && CUR_CYLS(id_buf) > 0 && CUR_SECTORS(id_buf) > 0 &&
    637              CUR_CAPACITY(id_buf) == (u32) CUR_HEADS(id_buf) * (u32) CUR_CYLS(id_buf) * (u32) CUR_SECTORS(id_buf)) {
     672  }
     673  else if (CUR_HEADS(id_buf) > 0 && CUR_CYLS(id_buf) > 0 && CUR_SECTORS(id_buf) > 0 &&
     674             CUR_CAPACITY(id_buf) == (u32) CUR_HEADS(id_buf) * (u32) CUR_CYLS(id_buf) * (u32) CUR_SECTORS(id_buf))
     675  {
    638676    /* BIOS-supplied (aka "current") geometry values look valid */
    639677    geometry->NumHeads        = CUR_HEADS(id_buf);
     
    641679    geometry->TotalCylinders  = CUR_CYLS(id_buf);
    642680    Method = "BIOS";
    643   } else if (ATA_HEADS(id_buf) > 0 && ATA_CYLS(id_buf) > 0 && ATA_SECTORS(id_buf) > 0) {
     681  }
     682  else if (ATA_HEADS(id_buf) > 0 && ATA_CYLS(id_buf) > 0 && ATA_SECTORS(id_buf) > 0)
     683  {
    644684    /* ATA-supplied values for geometry look valid */
    645685    geometry->NumHeads        = ATA_HEADS(id_buf);
     
    647687    geometry->TotalCylinders  = ATA_CYLS(id_buf);
    648688    Method = "ATA";
    649   } else {
     689  }
     690  else
     691  {
    650692    /* use typical SCSI geometry */
    651693    geometry->NumHeads        = 255;
     
    655697  }
    656698
    657   dprintf("Physical geometry: %ld cylinders, %d heads, %d sectors per track (%ldMB) (%s)\n",
    658       (u32) geometry->TotalCylinders, (u16) geometry->NumHeads, (u16) geometry->SectorsPerTrack,
    659       (u32) (geometry->TotalSectors / 2048), Method);
     699  DPRINTF(2,"Physical geometry: %d cylinders, %d heads, %d sectors per track (%dMB) (%s)\n",
     700      geometry->TotalCylinders, geometry->NumHeads, geometry->SectorsPerTrack,
     701      (geometry->TotalSectors / 2048), Method);
    660702
    661703  /* Fixup the geometry in case the geometry reported by the BIOS is bad */
    662   if (adjust_cylinders(geometry, geometry->TotalSectors)) { // cylinder overflow
     704  if (adjust_cylinders(geometry, geometry->TotalSectors))
     705  { // cylinder overflow
    663706    log_geom_calculate_LBA_assist(geometry, geometry->TotalSectors);
    664707    geometry->TotalSectors = (USHORT)(geometry->NumHeads * geometry->SectorsPerTrack) * (ULONG)geometry->TotalCylinders;
     
    666709  adjust_cylinders(geometry, geometry->TotalSectors);
    667710
    668   dprintf("Logical geometry: %ld cylinders, %d heads, %d sectors per track (%ldMB) (%s)\n",
    669       (u32) geometry->TotalCylinders, (u16) geometry->NumHeads, (u16) geometry->SectorsPerTrack,
    670       (u32) (geometry->TotalSectors / 2048), Method);
    671 
    672   if (is_lvm_geometry(iorb)) Method = "LVM";
     711  DPRINTF(2,"Logical geometry: %d cylinders, %d heads, %d sectors per track (%dMB) (%s)\n",
     712      geometry->TotalCylinders, geometry->NumHeads, geometry->SectorsPerTrack,
     713      (geometry->TotalSectors / 2048), Method);
     714
     715  if (is_lvm_geometry(pIorb)) Method = "LVM";
    673716  ad_infos[a].ports[p].devs[0].dev_info.Cylinders = geometry->TotalCylinders;
    674717  ad_infos[a].ports[p].devs[0].dev_info.HeadsPerCylinder = geometry->NumHeads;
     
    677720  ad_infos[a].ports[p].devs[0].dev_info.Method = Method;
    678721
    679   dprintf("Reported geometry: %ld cylinders, %d heads, %d sectors per track (%ldMB) (%s)\n",
    680       (u32) geometry->TotalCylinders, (u16) geometry->NumHeads, (u16) geometry->SectorsPerTrack,
    681       (u32) (geometry->TotalSectors / 2048), Method);
     722  DPRINTF(2,"Reported geometry: %d cylinders, %d heads, %d sectors per track (%dMB) (%s)\n",
     723      geometry->TotalCylinders, geometry->NumHeads, geometry->SectorsPerTrack,
     724      (geometry->TotalSectors / 2048), Method);
    682725
    683726  /* tell interrupt handler that this IORB is complete */
    684   add_workspace(iorb)->complete = 1;
     727  add_workspace(pIorb)->complete = 1;
    685728}
    686729
     
    688731 * Test whether unit is ready.
    689732 */
    690 int ata_unit_ready(IORBH _far *iorb, int slot)
     733int ata_unit_ready(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
    691734{
    692735  /* This is a NOP for ATA devices (at least right now); returning an error
     
    694737   * HW command and the IORB will complete successfully.
    695738   */
    696   ((IORB_UNIT_STATUS _far *) iorb)->UnitStatus = US_READY | US_POWER;
     739  ((IORB_UNIT_STATUS *)pIorb)->UnitStatus = US_READY | US_POWER;
    697740  return(-1);
    698741}
     
    701744 * Read sectors from AHCI device.
    702745 */
    703 int ata_read(IORBH _far *iorb, int slot)
    704 {
    705   IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
    706   AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
     746int ata_read(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
     747{
     748  IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
     749  SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(io->pSGList);
     750  AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
    707751  ULONG sector = io->RBA + io->BlocksXferred;
    708752  USHORT count = io->BlockCount - io->BlocksXferred;
    709753  USHORT sg_indx;
    710754  USHORT sg_cnt;
    711   int p = iorb_unit_port(iorb);
    712   int d = iorb_unit_device(iorb);
     755  int p = iorb_unit_port(pIorb);
     756  int d = iorb_unit_device(pIorb);
    713757  int rc;
    714758
    715   if (io->BlockCount == 0) {
     759  if (io->BlockCount == 0)
     760  {
    716761    /* NOP; return -1 without error in IORB to indicate success */
    717762    return(-1);
    718763  }
    719764
    720   if (add_workspace(iorb)->unaligned) {
     765  if (add_workspace(pIorb)->unaligned)
     766  {
    721767    /* unaligned S/G addresses present; need to use double buffers */
    722     return(ata_read_unaligned(iorb, slot));
     768    return(ata_read_unaligned(pIorb, slot));
    723769  }
    724770
     
    729775   */
    730776  if (io->BlocksXferred == 0 && io->cSGList == 1 &&
    731       io->pSGList[0].XferBufLen > (ULONG) io->BlockCount * io->BlockSize) {
    732     io->pSGList[0].XferBufLen = (ULONG) io->BlockCount * io->BlockSize;
     777      pSGList[0].XferBufLen > (ULONG) io->BlockCount * io->BlockSize)
     778  {
     779    pSGList[0].XferBufLen = (ULONG) io->BlockCount * io->BlockSize;
    733780  }
    734781
    735782  /* prepare read command while keeping an eye on S/G count limitations */
    736   do {
     783  do
     784  {
    737785    sg_indx = ata_get_sg_indx(io);
    738786    sg_cnt = io->cSGList - sg_indx;
    739     if ((rc = ata_cmd_read(iorb, ai, p, d, slot, sector, count,
    740                            io->pSGList + sg_indx, sg_cnt)) > 0) {
     787    if ((rc = ata_cmd_read(pIorb, ai, p, d, slot, sector, count,
     788                           pSGList + sg_indx, sg_cnt)) > 0)
     789    {
    741790      /* couldn't map all S/G elements */
    742       ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
     791      ata_max_sg_cnt(io, sg_indx, rc, &sg_cnt, &count);
    743792    }
    744793  } while (rc > 0 && sg_cnt > 0);
    745794
    746   if (rc == 0) {
    747     add_workspace(iorb)->blocks = count;
    748     add_workspace(iorb)->ppfunc = ata_read_pp;
    749 
    750   } else if (rc > 0) {
    751     iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
    752 
    753   } else if (rc == ATA_CMD_UNALIGNED_ADDR) {
     795  if (rc == 0)
     796  {
     797    add_workspace(pIorb)->blocks = count;
     798    add_workspace(pIorb)->ppfunc = ata_read_pp;
     799  }
     800  else if (rc > 0)
     801  {
     802    iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
     803  }
     804  else if (rc == ATA_CMD_UNALIGNED_ADDR)
     805  {
    754806    /* unaligned S/G addresses detected; need to use double buffers */
    755     add_workspace(iorb)->unaligned = 1;
    756     return(ata_read_unaligned(iorb, slot));
    757 
    758   } else {
    759     iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
     807    add_workspace(pIorb)->unaligned = 1;
     808    return(ata_read_unaligned(pIorb, slot));
     809
     810  }
     811  else
     812  {
     813    iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
    760814  }
    761815
     
    769823 * use a transfer buffer and copy the data manually.
    770824 */
    771 int ata_read_unaligned(IORBH _far *iorb, int slot)
    772 {
    773   IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
    774   ADD_WORKSPACE _far *aws = add_workspace(iorb);
    775   AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
     825int ata_read_unaligned(IORBH *pIorb, int slot)
     826{
     827  IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
     828  ADD_WORKSPACE *aws = add_workspace(pIorb);
     829  AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
    776830  ULONG sector = io->RBA + io->BlocksXferred;
    777831  SCATGATENTRY sg_single;
    778   int p = iorb_unit_port(iorb);
    779   int d = iorb_unit_device(iorb);
     832  int p = iorb_unit_port(pIorb);
     833  int d = iorb_unit_device(pIorb);
    780834  int rc;
    781835
    782   ddprintf("ata_read_unaligned(%d.%d.%d, %ld)\n", ad_no(ai), p, d, sector);
     836  DPRINTF(3,"ata_read_unaligned(%d.%d.%d, %d)\n", ad_no(ai), p, d, sector);
    783837
    784838  /* allocate transfer buffer */
    785   if ((aws->buf = malloc(io->BlockSize)) == NULL) {
    786     iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
     839  if ((aws->buf = MemAlloc(io->BlockSize)) == NULL)
     840  {
     841    iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
    787842    return(-1);
    788843  }
    789844
    790845  /* prepare read command using transfer buffer */
    791   sg_single.ppXferBuf = virt_to_phys(aws->buf);
     846  sg_single.ppXferBuf = MemPhysAdr(aws->buf);
    792847  sg_single.XferBufLen = io->BlockSize;
    793   rc = ata_cmd_read(iorb, ai, p, d, slot, sector, 1, &sg_single, 1);
     848  rc = ata_cmd_read(pIorb, ai, p, d, slot, sector, 1, &sg_single, 1);
    794849
    795850  if (rc == 0) {
    796     add_workspace(iorb)->blocks = 1;
    797     add_workspace(iorb)->ppfunc = ata_read_pp;
     851    add_workspace(pIorb)->blocks = 1;
     852    add_workspace(pIorb)->ppfunc = ata_read_pp;
    798853
    799854  } else if (rc > 0) {
    800     iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
     855    iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
    801856
    802857  } else {
    803     iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
     858    iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
    804859  }
    805860
     
    813868 * takes care of copying data from the transfer buffer for unaligned reads.
    814869 */
    815 void ata_read_pp(IORBH _far *iorb)
    816 {
    817   IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
    818   ADD_WORKSPACE _far *aws = add_workspace(iorb);
    819 
    820   if (aws->unaligned) {
     870void ata_read_pp(IORBH FAR16DATA *vIorb, IORBH *pIorb)
     871{
     872  IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
     873  SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(io->pSGList);
     874  ADD_WORKSPACE *aws = add_workspace(pIorb);
     875
     876  if (aws->unaligned)
     877  {
    821878    /* copy transfer buffer to corresponding physical address in S/G list */
    822     sg_memcpy(io->pSGList, io->cSGList,
     879    sg_memcpy(pSGList, io->cSGList,
    823880              (ULONG) io->BlocksXferred * (ULONG) io->BlockSize,
    824881              aws->buf, io->BlockSize, BUF_TO_SG);
    825882  }
    826883
    827   io->BlocksXferred += add_workspace(iorb)->blocks;
    828   ddprintf("ata_read_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
    829 
    830   if (io->BlocksXferred >= io->BlockCount) {
     884  io->BlocksXferred += add_workspace(pIorb)->blocks;
     885  DPRINTF(3,"ata_read_pp(): blocks transferred = %d\n", io->BlocksXferred);
     886
     887  if (io->BlocksXferred >= io->BlockCount)
     888  {
    831889    /* we're done; tell IRQ handler the IORB is complete */
    832     add_workspace(iorb)->complete = 1;
    833   } else {
     890    add_workspace(pIorb)->complete = 1;
     891  }
     892  else
     893  {
    834894    /* requeue this IORB for next iteration */
    835     iorb_requeue(iorb);
     895    iorb_requeue(pIorb);
    836896  }
    837897}
     
    840900 * Verify readability of sectors on ATA device.
    841901 */
    842 int ata_verify(IORBH _far *iorb, int slot)
    843 {
    844   IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
    845   AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
    846   int p = iorb_unit_port(iorb);
    847   int d = iorb_unit_device(iorb);
     902int ata_verify(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
     903{
     904  IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
     905  AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
     906  int p = iorb_unit_port(pIorb);
     907  int d = iorb_unit_device(pIorb);
    848908  int rc;
    849909
    850   if (io->BlockCount == 0) {
     910  if (io->BlockCount == 0)
     911  {
    851912    /* NOP; return -1 without error in IORB to indicate success */
    852913    return(-1);
     
    854915
    855916  /* prepare verify command */
    856   if (io->RBA >= (1UL << 28) || io->BlockCount > 256) {
     917  if (io->RBA >= (1UL << 28) || io->BlockCount > 256)
     918  {
    857919    /* need LBA48 for this command */
    858920    if (!ai->ports[p].devs[d].lba48) {
    859       iorb_seterr(iorb, IOERR_RBA_LIMIT);
     921      iorb_seterr(pIorb, IOERR_RBA_LIMIT);
    860922      return(-1);
    861923    }
    862924    rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY_EXT,
    863                  AP_SECTOR_48, (u32) io->RBA, (u16) 0,
    864                  AP_COUNT,     (u16) io->BlockCount,
     925                 AP_SECTOR_48, io->RBA, 0,
     926                 AP_COUNT,     io->BlockCount,
    865927                 AP_DEVICE,    0x40,
    866928                 AP_END);
    867929  } else {
    868930    rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY,
    869                  AP_SECTOR_28, (u32) io->RBA,
    870                  AP_COUNT,     (u16) io->BlockCount & 0xffU,
     931                 AP_SECTOR_28, io->RBA,
     932                 AP_COUNT,     io->BlockCount & 0xffU,
    871933                 AP_DEVICE,    0x40,
    872934                 AP_END);
     
    879941 * Write sectors to AHCI device.
    880942 */
    881 int ata_write(IORBH _far *iorb, int slot)
    882 {
    883   IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
    884   AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
     943int ata_write(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
     944{
     945  IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
     946  SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(io->pSGList);
     947  AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
    885948  ULONG sector = io->RBA + io->BlocksXferred;
    886949  USHORT count = io->BlockCount - io->BlocksXferred;
    887950  USHORT sg_indx;
    888951  USHORT sg_cnt;
    889   int p = iorb_unit_port(iorb);
    890   int d = iorb_unit_device(iorb);
     952  int p = iorb_unit_port(pIorb);
     953  int d = iorb_unit_device(pIorb);
    891954  int rc;
    892955
    893   if (io->BlockCount == 0) {
     956  if (io->BlockCount == 0)
     957  {
    894958    /* NOP; return -1 without error in IORB to indicate success */
    895959    return(-1);
    896960  }
    897961
    898   if (add_workspace(iorb)->unaligned) {
     962  if (add_workspace(pIorb)->unaligned)
     963  {
    899964    /* unaligned S/G addresses present; need to use double buffers */
    900     return(ata_write_unaligned(iorb, slot));
     965    return(ata_write_unaligned(pIorb, slot));
    901966  }
    902967
     
    905970    sg_indx = ata_get_sg_indx(io);
    906971    sg_cnt = io->cSGList - sg_indx;
    907     if ((rc = ata_cmd_write(iorb, ai, p, d, slot, sector, count,
    908                             io->pSGList + sg_indx, sg_cnt,
    909                             io->Flags & XIO_DISABLE_HW_WRITE_CACHE)) > 0) {
     972    if ((rc = ata_cmd_write(pIorb, ai, p, d, slot, sector, count,
     973                            pSGList + sg_indx, sg_cnt,
     974                            io->Flags & XIO_DISABLE_HW_WRITE_CACHE)) > 0)
     975    {
    910976      /* couldn't map all S/G elements */
    911977      ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
     
    913979  } while (rc > 0 && sg_cnt > 0);
    914980
    915   if (rc == 0) {
    916     add_workspace(iorb)->blocks = count;
    917     add_workspace(iorb)->ppfunc = ata_write_pp;
    918 
    919   } else if (rc > 0) {
    920     iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
    921 
    922   } else if (rc == ATA_CMD_UNALIGNED_ADDR) {
     981  if (rc == 0)
     982  {
     983    add_workspace(pIorb)->blocks = count;
     984    add_workspace(pIorb)->ppfunc = ata_write_pp;
     985  }
     986  else if (rc > 0)
     987  {
     988    iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
     989  }
     990  else if (rc == ATA_CMD_UNALIGNED_ADDR)
     991  {
    923992    /* unaligned S/G addresses detected; need to use double buffers */
    924     add_workspace(iorb)->unaligned = 1;
    925     return(ata_write_unaligned(iorb, slot));
    926 
    927   } else {
    928     iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
     993    add_workspace(pIorb)->unaligned = 1;
     994    return(ata_write_unaligned(pIorb, slot));
     995  }
     996  else
     997  {
     998    iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
    929999  }
    9301000
     
    9381008 * use a transfer buffer and copy the data manually.
    9391009 */
    940 int ata_write_unaligned(IORBH _far *iorb, int slot)
    941 {
    942   IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
    943   ADD_WORKSPACE _far *aws = add_workspace(iorb);
    944   AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
     1010int ata_write_unaligned(IORBH *pIorb, int slot)
     1011{
     1012  IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
     1013  SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(io->pSGList);
     1014  ADD_WORKSPACE *aws = add_workspace(pIorb);
     1015  AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
    9451016  ULONG sector = io->RBA + io->BlocksXferred;
    9461017  SCATGATENTRY sg_single;
    947   int p = iorb_unit_port(iorb);
    948   int d = iorb_unit_device(iorb);
     1018  int p = iorb_unit_port(pIorb);
     1019  int d = iorb_unit_device(pIorb);
    9491020  int rc;
    9501021
    951   ddprintf("ata_write_unaligned(%d.%d.%d, %ld)\n", ad_no(ai), p, d, sector);
     1022  DPRINTF(3,"ata_write_unaligned(%d.%d.%d, %d)\n", ad_no(ai), p, d, sector);
    9521023
    9531024  /* allocate transfer buffer */
    954   if ((aws->buf = malloc(io->BlockSize)) == NULL) {
    955     iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
     1025  if ((aws->buf = MemAlloc(io->BlockSize)) == NULL)
     1026  {
     1027    iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
    9561028    return(-1);
    9571029  }
    9581030
    9591031  /* copy next sector from S/G list to transfer buffer */
    960   sg_memcpy(io->pSGList, io->cSGList,
     1032  sg_memcpy(pSGList, io->cSGList,
    9611033            (ULONG) io->BlocksXferred * (ULONG) io->BlockSize,
    9621034            aws->buf, io->BlockSize, SG_TO_BUF);
    9631035
    9641036  /* prepare write command using transfer buffer */
    965   sg_single.ppXferBuf = virt_to_phys(aws->buf);
     1037  sg_single.ppXferBuf = MemPhysAdr(aws->buf);
    9661038  sg_single.XferBufLen = io->BlockSize;
    967   rc = ata_cmd_write(iorb, ai, p, d, slot, sector, 1, &sg_single, 1,
     1039  rc = ata_cmd_write(pIorb, ai, p, d, slot, sector, 1, &sg_single, 1,
    9681040                     io->Flags & XIO_DISABLE_HW_WRITE_CACHE);
    9691041
    970   if (rc == 0) {
    971     add_workspace(iorb)->blocks = 1;
    972     add_workspace(iorb)->ppfunc = ata_write_pp;
    973 
    974   } else if (rc > 0) {
    975     iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
    976 
    977   } else {
    978     iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
     1042  if (rc == 0)
     1043  {
     1044    add_workspace(pIorb)->blocks = 1;
     1045    add_workspace(pIorb)->ppfunc = ata_write_pp;
     1046  }
     1047  else if (rc > 0)
     1048  {
     1049    iorb_seterr(pIorb, IOERR_CMD_SGLIST_BAD);
     1050  }
     1051  else
     1052  {
     1053    iorb_seterr(pIorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
    9791054  }
    9801055
     
    9881063 * transferred, requeues the IORB to process the remaining sectors.
    9891064 */
    990 void ata_write_pp(IORBH _far *iorb)
    991 {
    992   IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
    993 
    994   io->BlocksXferred += add_workspace(iorb)->blocks;
    995   ddprintf("ata_write_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
    996 
    997   if (io->BlocksXferred >= io->BlockCount) {
     1065void ata_write_pp(IORBH FAR16DATA *vIorb, IORBH *pIorb)
     1066{
     1067  IORB_EXECUTEIO *io = (IORB_EXECUTEIO *)pIorb;
     1068
     1069  io->BlocksXferred += add_workspace(pIorb)->blocks;
     1070  DPRINTF(3,"ata_write_pp(): blocks transferred = %d\n", io->BlocksXferred);
     1071
     1072  if (io->BlocksXferred >= io->BlockCount)
     1073  {
    9981074    /* we're done; tell IRQ handler the IORB is complete */
    999     add_workspace(iorb)->complete = 1;
    1000   } else {
     1075    add_workspace(pIorb)->complete = 1;
     1076  }
     1077  else
     1078  {
    10011079    /* requeue this IORB for next iteration */
    1002     iorb_requeue(iorb);
     1080    iorb_requeue(pIorb);
    10031081  }
    10041082}
     
    10071085 * Execute ATA command.
    10081086 */
    1009 int ata_execute_ata(IORBH _far *iorb, int slot)
    1010 {
    1011   IORB_ADAPTER_PASSTHRU _far *apt = (IORB_ADAPTER_PASSTHRU _far *) iorb;
    1012   AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
    1013   int p = iorb_unit_port(iorb);
    1014   int d = iorb_unit_device(iorb);
     1087int ata_execute_ata(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
     1088{
     1089  IORB_ADAPTER_PASSTHRU *apt = (IORB_ADAPTER_PASSTHRU *)pIorb;
     1090  SCATGATENTRY *pSGList = (SCATGATENTRY*)Far16ToFlat(apt->pSGList);
     1091  AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
     1092  int p = iorb_unit_port(pIorb);
     1093  int d = iorb_unit_device(pIorb);
    10151094  int rc;
    10161095
    1017   if (apt->ControllerCmdLen != sizeof(ATA_CMD)) {
    1018     iorb_seterr(iorb, IOERR_CMD_SYNTAX);
     1096  if (apt->ControllerCmdLen != sizeof(ATA_CMD))
     1097  {
     1098    iorb_seterr(pIorb, IOERR_CMD_SYNTAX);
    10191099    return(-1);
    10201100  }
    10211101
    10221102  rc = ata_cmd(ai, p, d, slot, 0,
    1023                AP_SGLIST,   apt->pSGList, apt->cSGList,
    1024                AP_ATA_CMD,  apt->pControllerCmd,
     1103               AP_SGLIST,   pSGList, apt->cSGList,
     1104               AP_ATA_CMD,  Far16ToFlat(apt->pControllerCmd),
    10251105               AP_WRITE,    !(apt->Flags & PT_DIRECTION_IN),
    10261106               AP_END);
    10271107
    1028   if (rc == 0) {
    1029     add_workspace(iorb)->ppfunc = ata_execute_ata_pp;
     1108  if (rc == 0)
     1109  {
     1110    add_workspace(pIorb)->ppfunc = ata_execute_ata_pp;
    10301111  }
    10311112
     
    10401121 * See ata_cmd_to_fis() for an explanation of the mapping.
    10411122 */
    1042 void ata_execute_ata_pp(IORBH _far *iorb)
    1043 {
    1044   AHCI_PORT_DMA _far *dma_base;
    1045   ATA_CMD _far *cmd;
     1123void ata_execute_ata_pp(IORBH FAR16DATA *vIorb, IORBH *pIorb)
     1124{
     1125  AHCI_PORT_DMA *dma_base;
     1126  ATA_CMD *cmd;
    10461127  AD_INFO *ai;
    1047   u8 _far *fis;
     1128  u8 *fis;
    10481129  int p;
    10491130
    10501131  /* get address of D2H FIS */
    1051   ai = ad_infos + iorb_unit_adapter(iorb);
    1052   p = iorb_unit_port(iorb);
     1132  ai = ad_infos + iorb_unit_adapter(pIorb);
     1133  p = iorb_unit_port(pIorb);
    10531134  dma_base = port_dma_base(ai, p);
    10541135  fis = dma_base->rx_fis + 0x40;
    10551136
    1056   if (fis[0] != 0x34) {
     1137  if (fis[0] != 0x34)
     1138  {
    10571139    /* this is not a D2H FIS - give up silently */
    1058     ddprintf("ata_execute_ata_pp(): D2H FIS type incorrect: %d\n", fis[0]);
    1059     add_workspace(iorb)->complete = 1;
     1140    DPRINTF(3,"ata_execute_ata_pp(): D2H FIS type incorrect: %d\n", fis[0]);
     1141    add_workspace(pIorb)->complete = 1;
    10601142    return;
    10611143  }
    10621144
    10631145  /* map D2H FIS to the original ATA controller command structure */
    1064   cmd = (ATA_CMD _far *) ((IORB_ADAPTER_PASSTHRU _far *) iorb)->pControllerCmd;
     1146  cmd = (ATA_CMD *)Far16ToFlat(((IORB_ADAPTER_PASSTHRU*)pIorb)->pControllerCmd);
    10651147
    10661148  cmd->cmd      = fis[2];
     
    10771159                | ((u16) fis[13] << 8);
    10781160
    1079   dphex(cmd, sizeof(*cmd), "ahci_execute_ata_pp(): cmd after completion:\n");
     1161  DHEXDUMP(0,cmd, sizeof(*cmd), "ahci_execute_ata_pp(): cmd after completion:\n");
    10801162
    10811163  /* signal completion to interrupt handler */
    1082   add_workspace(iorb)->complete = 1;
     1164  add_workspace(pIorb)->complete = 1;
    10831165}
    10841166
     
    10991181 *     else with a generic error code.
    11001182 */
    1101 int ata_req_sense(IORBH _far *iorb, int slot)
    1102 {
    1103   AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
    1104   u8 _far *port_mmio = port_base(ai, iorb_unit_port(iorb));
     1183int ata_req_sense(IORBH FAR16DATA *vIorb, IORBH *pIorb, int slot)
     1184{
     1185  AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
     1186  u8 *port_mmio = port_base(ai, iorb_unit_port(pIorb));
    11051187  u32 tf_data = readl(port_mmio + PORT_TFDATA);
    1106   u8 err = (u8) (tf_data >> 8);
    1107   u8 sts = (u8) (tf_data);
    1108 
    1109   if (sts & ATA_ERR) {
    1110     if (sts & ATA_DF) {
     1188  u8 err = (tf_data >> 8);
     1189  u8 sts = (tf_data);
     1190
     1191  if (sts & ATA_ERR)
     1192  {
     1193    if (sts & ATA_DF)
     1194    {
    11111195      /* there is a device-specific error condition */
    1112       if (err & ATA_ICRC) {
    1113         iorb_seterr(iorb, IOERR_ADAPTER_DEVICEBUSCHECK);
    1114       } else if (err & ATA_UNC) {
    1115         iorb_seterr(iorb, IOERR_MEDIA);
    1116       } else if (err & ATA_IDNF) {
    1117         iorb_seterr(iorb, IOERR_RBA_ADDRESSING_ERROR);
    1118       } else {
    1119         iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
     1196      if (err & ATA_ICRC)
     1197      {
     1198        iorb_seterr(pIorb, IOERR_ADAPTER_DEVICEBUSCHECK);
    11201199      }
    1121 
    1122     } else {
    1123       iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
    1124     }
    1125   } else {
     1200      else if (err & ATA_UNC)
     1201      {
     1202        iorb_seterr(pIorb, IOERR_MEDIA);
     1203      }
     1204      else if (err & ATA_IDNF)
     1205      {
     1206        iorb_seterr(pIorb, IOERR_RBA_ADDRESSING_ERROR);
     1207      }
     1208      else
     1209      {
     1210        iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
     1211      }
     1212
     1213    }
     1214    else
     1215    {
     1216      iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
     1217    }
     1218  }
     1219  else
     1220  {
    11261221    /* this function only gets called when we received an error interrupt */
    1127     iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
     1222    iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
    11281223  }
    11291224
     
    11621257 * device and the paramters set from above (NCQ, etc).
    11631258 */
    1164 static int ata_cmd_read(IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
    1165                         ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
     1259static int ata_cmd_read(IORBH *pIorb, AD_INFO *ai, int p, int d, int slot,
     1260                        ULONG sector, ULONG count, SCATGATENTRY *sg_list,
    11661261                        ULONG sg_cnt)
    11671262{
    11681263  int rc;
    11691264
    1170   if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
     1265  if (sector >= (1UL << 28) || count > 256 || add_workspace(pIorb)->is_ncq)
     1266  {
    11711267    /* need LBA48 for this command */
    1172     if (!ai->ports[p].devs[d].lba48) {
    1173       iorb_seterr(iorb, IOERR_RBA_LIMIT);
     1268    if (!ai->ports[p].devs[d].lba48)
     1269    {
     1270      iorb_seterr(pIorb, IOERR_RBA_LIMIT);
    11741271      return(-1);
    11751272    }
    1176     if (add_workspace(iorb)->is_ncq) {
     1273    if (add_workspace(pIorb)->is_ncq)
     1274    {
    11771275      /* use NCQ read; count goes into feature register, tag into count! */
    11781276      rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_READ,
    1179                    AP_SECTOR_48, (u32) sector, (u16) 0,
    1180                    AP_FEATURES,  (u16) count,
    1181                    AP_COUNT,     (u16) (slot << 3), /* tag == slot */
    1182                    AP_SGLIST,    sg_list, (u16) sg_cnt,
     1277                   AP_SECTOR_48, sector, 0,
     1278                   AP_FEATURES,  count,
     1279                   AP_COUNT,     (slot << 3), /* tag == slot */
     1280                   AP_SGLIST,    sg_list, sg_cnt,
    11831281                   AP_DEVICE,    0x40,
    11841282                   AP_END);
    1185     } else {
     1283    }
     1284    else
     1285    {
    11861286      rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ_EXT,
    1187                    AP_SECTOR_48, (u32) sector, (u16) 0,
    1188                    AP_COUNT,     (u16) count,
    1189                    AP_SGLIST,    sg_list, (u16) sg_cnt,
     1287                   AP_SECTOR_48, sector, 0,
     1288                   AP_COUNT,     count,
     1289                   AP_SGLIST,    sg_list, sg_cnt,
    11901290                   AP_DEVICE,    0x40,
    11911291                   AP_END);
    11921292    }
    11931293
    1194   } else {
     1294  }
     1295  else
     1296  {
    11951297    rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ,
    1196                  AP_SECTOR_28, (u32) sector,
    1197                  AP_COUNT,     (u16) count & 0xffU,
    1198                  AP_SGLIST,    sg_list, (u16) sg_cnt,
     1298                 AP_SECTOR_28, sector,
     1299                 AP_COUNT,     count & 0xffU,
     1300                 AP_SGLIST,    sg_list, sg_cnt,
    11991301                 AP_DEVICE,    0x40,
    12001302                 AP_END);
     
    12081310 * device and the paramters set from above (NCQ, etc)
    12091311 */
    1210 static int ata_cmd_write(IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
    1211                          ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
     1312static int ata_cmd_write(IORBH *pIorb, AD_INFO *ai, int p, int d, int slot,
     1313                         ULONG sector, ULONG count, SCATGATENTRY *sg_list,
    12121314                         ULONG sg_cnt, int write_through)
    12131315{
    12141316  int rc;
    12151317
    1216   if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
     1318  if (sector >= (1UL << 28) || count > 256 || add_workspace(pIorb)->is_ncq)
     1319  {
    12171320    /* need LBA48 for this command */
    1218     if (!ai->ports[p].devs[d].lba48) {
    1219       iorb_seterr(iorb, IOERR_RBA_LIMIT);
     1321    if (!ai->ports[p].devs[d].lba48)
     1322    {
     1323      iorb_seterr(pIorb, IOERR_RBA_LIMIT);
    12201324      return(-1);
    12211325    }
    1222     if (add_workspace(iorb)->is_ncq) {
     1326    if (add_workspace(pIorb)->is_ncq)
     1327    {
    12231328      /* use NCQ write; count goes into feature register, tag into count! */
    12241329      rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_WRITE,
    1225                    AP_SECTOR_48, (u32) sector, (u16) 0,
    1226                    AP_FEATURES,  (u16) count,
     1330                   AP_SECTOR_48, sector, 0,
     1331                   AP_FEATURES,  count,
    12271332                   /* tag = slot */
    1228                    AP_COUNT,     (u16) (slot << 3),
    1229                    AP_SGLIST,    sg_list, (u16) sg_cnt,
     1333                   AP_COUNT,     (slot << 3),
     1334                   AP_SGLIST,    sg_list, sg_cnt,
    12301335                   AP_DEVICE,    0x40,
    12311336                   /* force unit access */
     
    12331338                   AP_WRITE,     1,
    12341339                   AP_END);
    1235     } else {
     1340    }
     1341    else
     1342    {
    12361343      rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE_EXT,
    1237                    AP_SECTOR_48, (u32) sector, (u16) 0,
    1238                    AP_COUNT,     (u16) count,
    1239                    AP_SGLIST,    sg_list, (u16) sg_cnt,
     1344                   AP_SECTOR_48, sector, 0,
     1345                   AP_COUNT,     count,
     1346                   AP_SGLIST,    sg_list, sg_cnt,
    12401347                   AP_DEVICE,    0x40,
    12411348                   AP_WRITE,     1,
    12421349                   AP_END);
    12431350    }
    1244 
    1245   } else {
     1351  }
     1352  else
     1353  {
    12461354    rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE,
    1247                  AP_SECTOR_28, (u32) sector,
    1248                  AP_COUNT,     (u16) count & 0xffU,
    1249                  AP_SGLIST,    sg_list, (u16) sg_cnt,
     1355                 AP_SECTOR_28, sector,
     1356                 AP_COUNT,     count & 0xffU,
     1357                 AP_SGLIST,    sg_list, sg_cnt,
    12501358                 AP_DEVICE,    0x40,
    12511359                 AP_WRITE,     1,
     
    12551363  return(rc);
    12561364}
     1365
     1366/******************************************************************************
     1367 * Copy block from S/G list to virtual address or vice versa.
     1368 */
     1369void sg_memcpy(SCATGATENTRY *sg_list, USHORT sg_cnt, ULONG sg_off,
     1370               void *buf, USHORT len, SG_MEMCPY_DIRECTION dir)
     1371{
     1372  USHORT i;
     1373  USHORT l;
     1374  ULONG phys_addr;
     1375  ULONG pos = 0;
     1376  char *p;
     1377
     1378  /* walk through S/G list to find the elements involved in the operation */
     1379  for (i = 0; i < sg_cnt && len > 0; i++)
     1380  {
     1381    if (pos <= sg_off && pos + sg_list[i].XferBufLen > sg_off)
     1382    {
     1383      /* this S/G element intersects with the block to be copied */
     1384      phys_addr = sg_list[i].ppXferBuf + (sg_off - pos);
     1385      if ((l = sg_list[i].XferBufLen - (sg_off - pos)) > len)
     1386      {
     1387        l = len;
     1388      }
     1389
     1390      if (Dev32Help_PhysToLin(phys_addr, l, (PVOID) &p))
     1391      {
     1392        panic("sg_memcpy(): DevHelp_PhysToLin() failed");
     1393      }
     1394      if (dir == SG_TO_BUF)
     1395      {
     1396        memcpy(buf, p, l);
     1397      }
     1398      else
     1399      {
     1400        memcpy(p, buf, l);
     1401      }
     1402      sg_off += l;
     1403      buf = (char *) buf + l;
     1404      len -= l;
     1405    }
     1406
     1407    pos += sg_list[i].XferBufLen;
     1408  }
     1409}
     1410
     1411/******************************************************************************
     1412 * Halt processing by submitting an internal error. This is a last resort and
     1413 * should only be called when the system state is corrupt.
     1414 */
     1415void panic(char *msg)
     1416{
     1417  Dev32Help_InternalError(msg, strlen(msg));
     1418}
     1419
Note: See TracChangeset for help on using the changeset viewer.