[4] | 1 | /******************************************************************************
|
---|
| 2 | * ata.c - ATA command processing
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2010 Christian Mueller. Parts copied from/inspired by the
|
---|
| 5 | * Linux AHCI driver; those parts are (c) Linux AHCI/ATA maintainers
|
---|
| 6 | *
|
---|
| 7 | * This program is free software; you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * This program is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with this program; if not, write to the Free Software
|
---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #include "os2ahci.h"
|
---|
| 23 | #include "ata.h"
|
---|
| 24 |
|
---|
| 25 | /* -------------------------- macros and constants ------------------------- */
|
---|
| 26 |
|
---|
| 27 | /* ------------------------ typedefs and structures ------------------------ */
|
---|
| 28 |
|
---|
| 29 | /* -------------------------- function prototypes -------------------------- */
|
---|
| 30 |
|
---|
| 31 | /* ------------------------ global/static variables ------------------------ */
|
---|
| 32 |
|
---|
| 33 | /* ----------------------------- start of code ----------------------------- */
|
---|
| 34 |
|
---|
| 35 | /******************************************************************************
|
---|
| 36 | * Initialize AHCI command slot, FIS and S/G list for the specified ATA
|
---|
| 37 | * command. The command parameters are passed as a variable argument list
|
---|
| 38 | * of type and value(s). The list is terminated by AP_END.
|
---|
| 39 | *
|
---|
| 40 | * Notes:
|
---|
| 41 | *
|
---|
| 42 | * - The specified command slot is expected to be idle; no checks are
|
---|
| 43 | * performed to prevent messing with a busy port.
|
---|
| 44 | *
|
---|
| 45 | * - Port multipliers are not supported, yet, thus 'd' should always
|
---|
| 46 | * be 0 for the time being.
|
---|
| 47 | *
|
---|
| 48 | * - 'cmd' is passwd as 16-bit integer because the compiler would push
|
---|
| 49 | * a 'u8' as 16-bit value (it's a fixed argument) and the stdarg
|
---|
| 50 | * macros would screw up the address of the first variable argument
|
---|
| 51 | * if the size of the last fixed argument wouldn't match what the
|
---|
| 52 | * compiler pushed on the stack.
|
---|
| 53 | *
|
---|
| 54 | * Return values:
|
---|
| 55 | * 0 : success
|
---|
| 56 | * > 0 : could not map all S/G entries; the return value is the number of
|
---|
| 57 | * S/G entries that could be mapped.
|
---|
| 58 | * < 0 : other error
|
---|
| 59 | */
|
---|
| 60 | int ata_cmd(AD_INFO *ai, int p, int d, int slot, int cmd, ...)
|
---|
| 61 | {
|
---|
| 62 | va_list va;
|
---|
| 63 | va_start(va, cmd);
|
---|
| 64 | return(v_ata_cmd(ai, p, d, slot, cmd, va));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | int v_ata_cmd(AD_INFO *ai, int p, int d, int slot, int cmd, va_list va)
|
---|
| 68 | {
|
---|
| 69 | AHCI_PORT_DMA _far *dma_base_virt;
|
---|
| 70 | AHCI_CMD_HDR _far *cmd_hdr;
|
---|
| 71 | AHCI_CMD_TBL _far *cmd_tbl;
|
---|
| 72 | SCATGATENTRY _far *sg_list = NULL;
|
---|
| 73 | SCATGATENTRY sg_single;
|
---|
| 74 | ATA_PARM ap;
|
---|
| 75 | ATA_CMD ata_cmd;
|
---|
| 76 | void _far *atapi_cmd = NULL;
|
---|
| 77 | u32 dma_base_phys;
|
---|
| 78 | u16 atapi_cmd_len = 0;
|
---|
| 79 | u16 ahci_flags = 0;
|
---|
| 80 | u16 sg_cnt = 0;
|
---|
| 81 | int i;
|
---|
| 82 | int n;
|
---|
| 83 |
|
---|
| 84 | /* --------------------------------------------------------------------------
|
---|
| 85 | * Initialize ATA command. The ATA command is set up with the main command
|
---|
| 86 | * value and a variable list of additional parameters such as the sector
|
---|
| 87 | * address, transfer count, ...
|
---|
| 88 | */
|
---|
| 89 | memset(&ata_cmd, 0x00, sizeof(ata_cmd));
|
---|
| 90 | ata_cmd.cmd = (u8) cmd;
|
---|
| 91 |
|
---|
| 92 | /* parse variable arguments */
|
---|
| 93 | do {
|
---|
| 94 | switch ((ap = va_arg(va, ATA_PARM))) {
|
---|
| 95 |
|
---|
| 96 | case AP_AHCI_FLAGS:
|
---|
| 97 | ahci_flags |= va_arg(va, u16);
|
---|
| 98 | break;
|
---|
| 99 |
|
---|
[8] | 100 | case AP_WRITE:
|
---|
[4] | 101 | ahci_flags |= AHCI_CMD_WRITE;
|
---|
| 102 | break;
|
---|
| 103 |
|
---|
| 104 | case AP_FEATURES:
|
---|
| 105 | /* ATA features word */
|
---|
| 106 | ata_cmd.features = va_arg(va, u16);
|
---|
| 107 | break;
|
---|
| 108 |
|
---|
| 109 | case AP_COUNT:
|
---|
| 110 | /* transfer count */
|
---|
| 111 | ata_cmd.count = va_arg(va, u16);
|
---|
| 112 | break;
|
---|
| 113 |
|
---|
| 114 | case AP_SECTOR_28:
|
---|
| 115 | /* 28-bit sector address */
|
---|
| 116 | ata_cmd.lba_l = va_arg(va, u32);
|
---|
| 117 | if (ata_cmd.lba_l & 0xf0000000UL) {
|
---|
| 118 | dprintf("error: LBA-28 address %ld has more than 28 bits\n", ata_cmd.lba_l);
|
---|
| 119 | return(-1);
|
---|
| 120 | }
|
---|
| 121 | break;
|
---|
| 122 |
|
---|
| 123 | case AP_SECTOR_48:
|
---|
| 124 | /* 48-bit sector address */
|
---|
| 125 | ata_cmd.lba_l = va_arg(va, u32);
|
---|
| 126 | ata_cmd.lba_h = va_arg(va, u16);
|
---|
| 127 | break;
|
---|
| 128 |
|
---|
| 129 | case AP_DEVICE:
|
---|
| 130 | /* ATA device byte */
|
---|
| 131 | ata_cmd.device = va_arg(va, u16) >> 8;
|
---|
| 132 | break;
|
---|
| 133 |
|
---|
| 134 | case AP_SGLIST:
|
---|
| 135 | /* scatter/gather list in SCATGATENTRY/count format */
|
---|
| 136 | sg_list = va_arg(va, void _far *);
|
---|
| 137 | sg_cnt = va_arg(va, u16);
|
---|
| 138 | break;
|
---|
| 139 |
|
---|
| 140 | case AP_VADDR:
|
---|
| 141 | /* virtual buffer address in addr/len format (up to 4K) */
|
---|
| 142 | DevHelp_VirtToPhys(va_arg(va, void _far *), &sg_single.ppXferBuf);
|
---|
| 143 | sg_single.XferBufLen = va_arg(va, u16);
|
---|
| 144 | sg_list = &sg_single;
|
---|
| 145 | sg_cnt = 1;
|
---|
| 146 | break;
|
---|
| 147 |
|
---|
| 148 | case AP_ATAPI_CMD:
|
---|
| 149 | atapi_cmd = va_arg(va, void _far *);
|
---|
| 150 | atapi_cmd_len = va_arg(va, u16);
|
---|
| 151 | ahci_flags |= AHCI_CMD_ATAPI;
|
---|
| 152 | break;
|
---|
| 153 |
|
---|
| 154 | case AP_END:
|
---|
| 155 | break;
|
---|
| 156 |
|
---|
| 157 | default:
|
---|
| 158 | dprintf("error: v_ata_cmd() called with invalid parameter type (%d)\n", (int) ap);
|
---|
| 159 | return(-1);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | } while (ap != AP_END);
|
---|
| 163 |
|
---|
| 164 | /* --------------------------------------------------------------------------
|
---|
| 165 | * Fill in AHCI ATA command information. This includes the port command slot,
|
---|
| 166 | * the corresponding command FIS and the S/G list. The layout of the AHCI
|
---|
| 167 | * port DMA region is based on the Linux AHCI driver and looks like this:
|
---|
| 168 | *
|
---|
| 169 | * - 32 AHCI command headers (AHCI_CMD_HDR) with 32 bytes, each
|
---|
| 170 | * - 1 FIS receive area with 256 bytes (AHCI_RX_FIS_SZ)
|
---|
| 171 | * - 32 AHCI command tables, each consisting of
|
---|
| 172 | * - 64 bytes for command FIS
|
---|
| 173 | * - 16 bytes for ATAPI comands
|
---|
| 174 | * - 48 bytes reserved
|
---|
| 175 | * - 48 S/G entries (AHCI_SG) with 32 bytes, each
|
---|
| 176 | *
|
---|
| 177 | * Since the whole DMA buffer for all ports is larger than 64KB and we need
|
---|
| 178 | * multiple segments to address all of them, there are no virtual pointers
|
---|
| 179 | * to the individual elements in AD_INFO. Instead, we're relying on macros
|
---|
| 180 | * for getting the base address of a particular port's DMA region, then
|
---|
| 181 | * map a structure on top of that for convenience (AHCI_PORT_DMA).
|
---|
| 182 | */
|
---|
| 183 | dma_base_virt = port_dma_base(ai, p);
|
---|
| 184 | dma_base_phys = port_dma_base_phys(ai, p);
|
---|
| 185 |
|
---|
| 186 | /* AHCI command header */
|
---|
| 187 | cmd_hdr = dma_base_virt->cmd_hdr + slot;
|
---|
| 188 | memset(cmd_hdr, 0x00, sizeof(*cmd_hdr));
|
---|
| 189 | cmd_hdr->options = ((d & 0x0f) << 12);
|
---|
| 190 | cmd_hdr->options |= ahci_flags; /* AHCI commaand flags */
|
---|
| 191 | cmd_hdr->options |= 5; /* length of command FIS in 32-bit words */
|
---|
| 192 | cmd_hdr->tbl_addr = dma_base_phys + offsetof(AHCI_PORT_DMA, cmd_tbl[slot]);
|
---|
| 193 |
|
---|
| 194 | /* AHCI command table */
|
---|
| 195 | cmd_tbl = dma_base_virt->cmd_tbl + slot;
|
---|
| 196 | memset(cmd_tbl, 0x00, sizeof(*cmd_tbl));
|
---|
| 197 | ata_cmd_to_fis(cmd_tbl->cmd_fis, &ata_cmd, d);
|
---|
| 198 |
|
---|
| 199 | if (atapi_cmd != NULL) {
|
---|
| 200 | /* copy ATAPI command */
|
---|
| 201 | memcpy(cmd_tbl->atapi_cmd, atapi_cmd, atapi_cmd_len);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /* PRDT (S/G list)
|
---|
| 205 | *
|
---|
| 206 | * - The S/G list for AHCI adapters is limited to 22 bits for the transfer
|
---|
| 207 | * size of each element, thus we need to split S/G elements larger than
|
---|
| 208 | * 22 bits into 2 AHCI_SG elements.
|
---|
| 209 | *
|
---|
| 210 | * - The S/G element size for AHCI is what the spec calls "'0' based"
|
---|
| 211 | * (i.e. 0 means 1 bytes). On top of that, the spec requires S/G transfer
|
---|
| 212 | * sizes to be even in the context of 16-bit transfers, thus bit '1'
|
---|
| 213 | * always needs to be set.
|
---|
| 214 | *
|
---|
| 215 | * - AHCI_MAX_SG_ELEMENT_LEN defines the maximum size of an AHCI S/G
|
---|
| 216 | * element in bytes, ignoring the '0'-based methodology (i.e. 1 << 22).
|
---|
| 217 | *
|
---|
| 218 | * - There's a limit on the maximum number of S/G elements in the port DMA
|
---|
| 219 | * buffer (AHCI_MAX_SG) which is lower than the HW maximum. It's beyond
|
---|
| 220 | * the control of this function to split commands which require more
|
---|
| 221 | * than AHCI_MAX_SG entries. In order to help the caller, the return value
|
---|
| 222 | * of this function will indicate how many OS/2 S/G entries were
|
---|
[8] | 223 | * successfully mapped.
|
---|
[4] | 224 | *
|
---|
| 225 | */
|
---|
| 226 | for (i = n = 0; i < sg_cnt; i++) {
|
---|
| 227 | u32 sg_addr = sg_list[i].ppXferBuf;
|
---|
| 228 | u32 sg_size = sg_list[i].XferBufLen;
|
---|
| 229 |
|
---|
| 230 | do {
|
---|
| 231 | u32 chunk = (sg_size > AHCI_MAX_SG_ELEMENT_LEN) ? AHCI_MAX_SG_ELEMENT_LEN
|
---|
| 232 | : sg_size;
|
---|
| 233 | if (n >= AHCI_MAX_SG) {
|
---|
| 234 | /* couldn't store all S/G elements in our DMA buffer */
|
---|
| 235 | ddprintf("ata_cmd(): too many S/G elements\n");
|
---|
| 236 | return(i - 1);
|
---|
| 237 | }
|
---|
| 238 | ddprintf("s/g list element: addr = 0x%08lx, size = 0x%04lx\n", sg_addr, chunk);
|
---|
| 239 | cmd_tbl->sg_list[n].addr = sg_addr;
|
---|
| 240 | cmd_tbl->sg_list[n].size = chunk - 1;
|
---|
| 241 | sg_addr += chunk;
|
---|
| 242 | sg_size -= chunk;
|
---|
| 243 | n++;
|
---|
| 244 | } while (sg_size > 0);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /* set final S/G count in AHCI command header */
|
---|
| 248 | cmd_hdr->options |= (u32) n << 16;
|
---|
| 249 |
|
---|
| 250 | if (debug >= 2) {
|
---|
| 251 | printf("ATA command for %d.%d.%d:\n", ad_no(ai), p, d);
|
---|
| 252 | phex(cmd_hdr, offsetof(AHCI_CMD_HDR, reserved), "cmd_hdr: ");
|
---|
| 253 | phex(&ata_cmd, sizeof(ata_cmd), "ata_cmd: ");
|
---|
| 254 | if (atapi_cmd != NULL) {
|
---|
| 255 | phex(atapi_cmd, atapi_cmd_len, "atapi_cmd: ");
|
---|
| 256 | }
|
---|
| 257 | if (n > 0) {
|
---|
| 258 | phex(cmd_tbl->sg_list, sizeof(*cmd_tbl->sg_list) * n, "sg_list: ");
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | return(0);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /******************************************************************************
|
---|
| 266 | * Fill SATA command FIS with values extracted from an ATA command structure.
|
---|
| 267 | * The command FIS buffer (fis) is expected to be initialized to 0s. The
|
---|
| 268 | * structure of the FIS maps to the ATA shadow register block, including
|
---|
| 269 | * registers which can be written twice to store 16 bits (called 'exp').
|
---|
| 270 | *
|
---|
| 271 | * The FIS structure looks like this (using LSB notation):
|
---|
| 272 | *
|
---|
| 273 | * +----------------+----------------+----------------+----------------+
|
---|
| 274 | * 00 | FIS type (27h) | C|R|R|R|PMP | Command | Features |
|
---|
| 275 | * +----------------+----------------+----------------+----------------+
|
---|
| 276 | * 04 | LBA 7:0 | LBA 15:8 | LBA 23:16 | R|R|R|D|Head |
|
---|
| 277 | * +----------------+----------------+----------------+----------------+
|
---|
| 278 | * 08 | LBA 31:24 | LBA 40:32 | LBA 47:40 | Features exp |
|
---|
| 279 | * +----------------+----------------+----------------+----------------+
|
---|
| 280 | * 12 | Count 7:0 | Count 15:8 | Reserved | Control |
|
---|
| 281 | * +----------------+----------------+----------------+----------------+
|
---|
| 282 | * 16 | Reserved | Reserved | Reserved | Reserved |
|
---|
| 283 | * +----------------+----------------+----------------+----------------+
|
---|
| 284 | */
|
---|
| 285 | void ata_cmd_to_fis(u8 _far *fis, ATA_CMD _far *ata_cmd, int d)
|
---|
| 286 | {
|
---|
| 287 | fis[0] = 0x27; /* register - host to device FIS */
|
---|
| 288 | fis[1] = (u8) (d & 0xf); /* port multiplier number */
|
---|
| 289 | fis[1] |= 0x80; /* bit 7 indicates Command FIS */
|
---|
| 290 | fis[2] = (u8) ata_cmd->cmd;
|
---|
| 291 | fis[3] = (u8) ata_cmd->features;
|
---|
| 292 |
|
---|
| 293 | fis[4] = (u8) ata_cmd->lba_l;
|
---|
| 294 | fis[5] = (u8) (ata_cmd->lba_l >> 8);
|
---|
| 295 | fis[6] = (u8) (ata_cmd->lba_l >> 16);
|
---|
| 296 | fis[7] = (u8) ata_cmd->device;
|
---|
| 297 |
|
---|
| 298 | fis[8] = (u8) (ata_cmd->lba_l >> 24);
|
---|
| 299 | fis[9] = (u8) ata_cmd->lba_h;
|
---|
| 300 | fis[10] = (u8) (ata_cmd->lba_h >> 8);
|
---|
| 301 | fis[11] = (u8) (ata_cmd->features >> 8);
|
---|
| 302 |
|
---|
| 303 | fis[12] = (u8) ata_cmd->count;
|
---|
| 304 | fis[13] = (u8) (ata_cmd->count >> 8);
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /******************************************************************************
|
---|
[8] | 308 | * Get index in S/G list for the number of transferred sectors in the IORB.
|
---|
| 309 | *
|
---|
| 310 | * Returning io->cSGList indicates an error.
|
---|
| 311 | *
|
---|
| 312 | * NOTE: OS/2 makes sure S/G lists are set up such that entries at the HW
|
---|
| 313 | * limit will never cross sector boundaries. This means that splitting
|
---|
| 314 | * S/G lists into multiple commands can be done without editing the S/G
|
---|
| 315 | * lists.
|
---|
| 316 | */
|
---|
| 317 | u16 ata_get_sg_indx(IORB_EXECUTEIO _far *io)
|
---|
| 318 | {
|
---|
| 319 | ULONG offset = io->BlocksXferred * io->BlockSize;
|
---|
| 320 | USHORT i;
|
---|
| 321 |
|
---|
| 322 | for (i = 0; i < io->cSGList && offset > 0; i++) {
|
---|
| 323 | offset -= io->pSGList[i].XferBufLen;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | return(i);
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | /******************************************************************************
|
---|
| 330 | * Get max S/G count which will fit into our HW S/G buffers. This function is
|
---|
| 331 | * called when the S/G list is too long and we need to split the IORB into
|
---|
| 332 | * multiple commands. It returns both the number of sectors and S/G list
|
---|
| 333 | * elements that we can handle in a single command.
|
---|
| 334 | *
|
---|
| 335 | * The parameter 'sg_indx' indicates the current start index in the S/G list
|
---|
| 336 | * (0 if this is the first command iteration).
|
---|
| 337 | *
|
---|
| 338 | * The parameter 'sg_max' is the return value of v_ata_cmd() and indicates
|
---|
| 339 | * how many S/G elements were successfully mapped. Whatever we return needs to
|
---|
| 340 | * be less or equal to this value.
|
---|
| 341 | *
|
---|
| 342 | * Returning 0 in *sg_cnt indicates an error.
|
---|
| 343 | *
|
---|
| 344 | * NOTE: OS/2 makes sure S/G lists are set up such that entries at HW limits
|
---|
| 345 | * will never cross sector boundaries. This means that splitting S/G
|
---|
| 346 | * lists into multiple commands can be done without editing S/G list
|
---|
| 347 | * elements. Since AHCI only allows 22 bits for each S/G element, the
|
---|
| 348 | * hardware limits are reported as AHCI_MAX_SG / 2 but will vary based
|
---|
| 349 | * on the actual length of S/G elements. This function looks for the
|
---|
| 350 | * maximum number of S/G elements that can be mapped on sector
|
---|
| 351 | * boundaries which will still fit into our HW S/G list.
|
---|
| 352 | */
|
---|
| 353 | void ata_max_sg_cnt(IORB_EXECUTEIO _far *io, USHORT sg_indx, USHORT sg_max,
|
---|
| 354 | USHORT _far *sg_cnt, USHORT _far *sector_cnt)
|
---|
| 355 | {
|
---|
| 356 | ULONG max_sector_cnt = 0;
|
---|
| 357 | USHORT max_sg_cnt = 0;
|
---|
| 358 | ULONG offset = 0;
|
---|
| 359 | USHORT i;
|
---|
| 360 |
|
---|
| 361 | for (i = sg_indx; i < io->cSGList; i++) {
|
---|
| 362 | if (i - sg_indx >= sg_max) {
|
---|
| 363 | /* we're beyond the number of S/G elements we can map */
|
---|
| 364 | break;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | offset += io->pSGList[i].XferBufLen;
|
---|
| 368 | if (offset % io->BlockSize == 0) {
|
---|
| 369 | /* this S/G element ends on a sector boundary */
|
---|
| 370 | max_sector_cnt = offset / io->BlockSize;
|
---|
| 371 | max_sg_cnt = i + 1;
|
---|
| 372 | }
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | /* return the best match we found so far (0 indicating failure) */
|
---|
| 376 | *sector_cnt = max_sector_cnt;
|
---|
| 377 | *sg_cnt = max_sg_cnt;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 |
|
---|
| 381 | /******************************************************************************
|
---|
[4] | 382 | * Get device or media geometry. Device and media geometry are expected to be
|
---|
| 383 | * the same for non-removable devices, which will always be the case for the
|
---|
| 384 | * ATA devices we're dealing with (hard disks). ATAPI is a different story
|
---|
| 385 | * and handled by atapi_get_geometry().
|
---|
| 386 | */
|
---|
| 387 | int ata_get_geometry(IORBH _far *iorb, int slot)
|
---|
| 388 | {
|
---|
| 389 | ADD_WORKSPACE _far *aws = add_workspace(iorb);
|
---|
| 390 | int rc;
|
---|
| 391 |
|
---|
| 392 | /* allocate buffer for ATA identify information */
|
---|
| 393 | if ((aws->buf = malloc(ATA_ID_WORDS * sizeof(u16))) == NULL) {
|
---|
| 394 | iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
|
---|
| 395 | return(-1);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | /* request ATA identify information */
|
---|
| 399 | aws->ppfunc = ata_get_geometry_pp;
|
---|
| 400 | rc = ata_cmd(ad_infos + iorb_unit_adapter(iorb),
|
---|
| 401 | iorb_unit_port(iorb),
|
---|
| 402 | iorb_unit_device(iorb),
|
---|
| 403 | slot,
|
---|
| 404 | ATA_CMD_ID_ATA,
|
---|
| 405 | AP_VADDR, (void _far *) aws->buf, ATA_ID_WORDS * sizeof(u16),
|
---|
| 406 | AP_END);
|
---|
| 407 |
|
---|
| 408 | if (rc != 0) {
|
---|
| 409 | free(aws->buf);
|
---|
| 410 | iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | return(rc);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | /******************************************************************************
|
---|
| 417 | * Post processing function for ata_get_geometry(): convert the ATA identify
|
---|
| 418 | * information to OS/2 IOCC_GEOMETRY information.
|
---|
| 419 | */
|
---|
| 420 | void ata_get_geometry_pp(IORBH _far *iorb)
|
---|
| 421 | {
|
---|
| 422 | GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
|
---|
| 423 | USHORT geometry_len = ((IORB_GEOMETRY _far *) iorb)->GeometryLen;
|
---|
| 424 | u16 *id_buf = add_workspace(iorb)->buf;
|
---|
| 425 |
|
---|
| 426 | /* Fill-in geometry information; os2ahci has been designed for devices
|
---|
| 427 | * adhering to the ATA spec v8 or later, thus the real geometry doesn't
|
---|
| 428 | * really matter (it's actually marked "obsolete" in the ATA 8 specs).
|
---|
| 429 | * In order to maintain compatibitily to the BIOS and partition tables,
|
---|
| 430 | * we'll use the same algorithm as used by typical PC BIOS versions:
|
---|
| 431 | *
|
---|
| 432 | * - 512 bytes per sector
|
---|
| 433 | * - 255 heads
|
---|
| 434 | * - 63 sectors per track
|
---|
| 435 | * - x cylinders (calculated)
|
---|
| 436 | *
|
---|
| 437 | * Please note that os2ahci currently does not support ATA sectors larger
|
---|
| 438 | * than 512 bytes, therefore relies on the translation logic built into the
|
---|
| 439 | * corresponding ATA disks. In theory, partitions should be aligned to the
|
---|
| 440 | * large sectors to prevent needless mapping all over the place but HPFS
|
---|
| 441 | * uses logical block sizes smaller than the typical large sectors found on
|
---|
| 442 | * modern hard disks so this won't make much of a difference. Large sector
|
---|
| 443 | * support will be evaluated at a later time (it's unclear right now whether
|
---|
| 444 | * HPFS would even support anything larger than 512 bytes).
|
---|
| 445 | *
|
---|
| 446 | * Another limitation is that OS/2 has a 32-bit variable for the total number
|
---|
| 447 | * of sectors, limiting the maximum capacity to roughly 2TB. This is another
|
---|
| 448 | * issue that needs to be addressed sooner or later; large sectors could
|
---|
| 449 | * raise this limit to something like 8TB but this is not really much of a
|
---|
| 450 | * difference. Maybe there's something in later DDKs that allows more than
|
---|
| 451 | * 32 bits?
|
---|
| 452 | */
|
---|
| 453 | memset(geometry, 0x00, geometry_len);
|
---|
| 454 |
|
---|
| 455 | /* extract total number of sectors */
|
---|
| 456 | if (id_buf[ATA_ID_CFS_ENABLE_2] & 0x40) {
|
---|
| 457 | /* 48-bit LBA supported */
|
---|
| 458 | if (id_buf[ATA_ID_LBA_CAPACITY_2 + 2] != 0) {
|
---|
| 459 | /* more than 32 bits for number of sectors */
|
---|
| 460 | dprintf("warning: limiting disk %d.%d.%d to 2TB\n",
|
---|
| 461 | iorb_unit_adapter(iorb), iorb_unit_port(iorb),
|
---|
| 462 | iorb_unit_device(iorb));
|
---|
| 463 | geometry->TotalSectors = 0xffffffffUL;
|
---|
| 464 | } else {
|
---|
| 465 | geometry->TotalSectors = *((u32 *) (id_buf + ATA_ID_LBA_CAPACITY_2));
|
---|
| 466 | }
|
---|
| 467 | } else {
|
---|
| 468 | /* 28-bit LBA */
|
---|
| 469 | geometry->TotalSectors = *((u32 *) (id_buf + ATA_ID_LBA_CAPACITY)) &
|
---|
| 470 | 0x0fffffffUL;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | geometry->BytesPerSector = 512;
|
---|
| 474 | geometry->NumHeads = 255;
|
---|
| 475 | geometry->SectorsPerTrack = 63;
|
---|
| 476 | geometry->TotalCylinders = geometry->TotalSectors /
|
---|
| 477 | ((u32) geometry->NumHeads *
|
---|
| 478 | (u32) geometry->SectorsPerTrack);
|
---|
[8] | 479 |
|
---|
| 480 | /* tell interrupt handler that this IORB is complete */
|
---|
| 481 | add_workspace(iorb)->complete = 1;
|
---|
[4] | 482 | }
|
---|
| 483 |
|
---|
| 484 | /******************************************************************************
|
---|
| 485 | * Test whether unit is ready.
|
---|
| 486 | */
|
---|
| 487 | int ata_unit_ready(IORBH _far *iorb, int slot)
|
---|
| 488 | {
|
---|
| 489 | /* This is a NOP for ATA devices (at least right now); returning an error
|
---|
| 490 | * without setting an error code means ahci_exec_iorb() will not queue any
|
---|
| 491 | * HW command and the IORB will complete successfully.
|
---|
| 492 | */
|
---|
| 493 | ((IORB_UNIT_STATUS _far *) iorb)->UnitStatus = US_READY | US_POWER;
|
---|
| 494 | return(-1);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | /******************************************************************************
|
---|
| 498 | * Read sectors from AHCI device.
|
---|
| 499 | */
|
---|
| 500 | int ata_read(IORBH _far *iorb, int slot)
|
---|
| 501 | {
|
---|
| 502 | IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
|
---|
| 503 | AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
|
---|
[8] | 504 | ULONG sector = io->RBA + io->BlocksXferred;
|
---|
| 505 | USHORT count = io->BlockCount - io->BlocksXferred;
|
---|
| 506 | USHORT sg_indx;
|
---|
| 507 | USHORT sg_cnt;
|
---|
[4] | 508 | int p = iorb_unit_port(iorb);
|
---|
| 509 | int d = iorb_unit_device(iorb);
|
---|
| 510 | int rc;
|
---|
| 511 |
|
---|
[8] | 512 | /* prepare read command while keeping an eye on S/G count limitations */
|
---|
| 513 | do {
|
---|
| 514 | sg_indx = ata_get_sg_indx(io);
|
---|
| 515 | sg_cnt = io->cSGList - sg_indx;
|
---|
| 516 |
|
---|
| 517 | if (sector >= (1UL << 28) || count > 256) {
|
---|
| 518 | /* need LBA48 for this command */
|
---|
| 519 | if (!ai->ports[p].devs[d].lba48) {
|
---|
| 520 | iorb_seterr(iorb, IOERR_RBA_LIMIT);
|
---|
| 521 | return(-1);
|
---|
| 522 | }
|
---|
| 523 | rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ_EXT,
|
---|
| 524 | AP_SECTOR_48, (u32) sector, (u16) 0,
|
---|
| 525 | AP_COUNT, (u16) count,
|
---|
| 526 | AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
|
---|
| 527 | AP_DEVICE, 0x4000,
|
---|
| 528 | AP_END);
|
---|
| 529 | } else {
|
---|
| 530 | rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ,
|
---|
| 531 | AP_SECTOR_28, (u32) sector,
|
---|
| 532 | AP_COUNT, (u16) count & 0xffU,
|
---|
| 533 | AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
|
---|
| 534 | AP_DEVICE, 0x4000,
|
---|
| 535 | AP_END);
|
---|
[4] | 536 | }
|
---|
[8] | 537 |
|
---|
| 538 | if (rc > 0) {
|
---|
| 539 | /* couldn't map all S/G elements */
|
---|
| 540 | ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
|
---|
| 541 | }
|
---|
| 542 | } while (rc > 0 && sg_cnt > 0);
|
---|
| 543 |
|
---|
| 544 | if (rc == 0) {
|
---|
| 545 | add_workspace(iorb)->blocks = count;
|
---|
| 546 | add_workspace(iorb)->ppfunc = ata_read_pp;
|
---|
| 547 |
|
---|
| 548 | } else if (rc > 0) {
|
---|
| 549 | iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
|
---|
| 550 |
|
---|
[4] | 551 | } else {
|
---|
[8] | 552 | iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
|
---|
[4] | 553 | }
|
---|
| 554 |
|
---|
| 555 | return(rc);
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | /******************************************************************************
|
---|
[8] | 559 | * Post processing function for ata_read(); this function updates the
|
---|
| 560 | * BlocksXferred counter in the IORB and, if not all blocks have been
|
---|
| 561 | * transferred, requeues the IORB to process the remaining sectors.
|
---|
[4] | 562 | */
|
---|
[8] | 563 | void ata_read_pp(IORBH _far *iorb)
|
---|
| 564 | {
|
---|
| 565 | IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
|
---|
| 566 |
|
---|
| 567 | io->BlocksXferred += add_workspace(iorb)->blocks;
|
---|
| 568 | ddprintf("ata_read_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
|
---|
| 569 |
|
---|
| 570 | if (io->BlocksXferred >= io->BlockCount) {
|
---|
| 571 | /* we're done; tell IRQ handler the IORB is complete */
|
---|
| 572 | add_workspace(iorb)->complete = 1;
|
---|
| 573 | } else {
|
---|
| 574 | /* requeue this IORB for next iteration */
|
---|
| 575 | iorb_requeue(iorb);
|
---|
| 576 | }
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | /******************************************************************************
|
---|
| 580 | * Verify readability of sectors on ATA device.
|
---|
| 581 | */
|
---|
[4] | 582 | int ata_verify(IORBH _far *iorb, int slot)
|
---|
| 583 | {
|
---|
| 584 | IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
|
---|
| 585 | AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
|
---|
| 586 | int p = iorb_unit_port(iorb);
|
---|
| 587 | int d = iorb_unit_device(iorb);
|
---|
| 588 | int rc;
|
---|
| 589 |
|
---|
| 590 | /* prepare verify command */
|
---|
| 591 | if (io->RBA >= (1UL << 28) || io->BlockCount > 256) {
|
---|
| 592 | /* need LBA48 for this command */
|
---|
| 593 | if (!ai->ports[p].devs[d].lba48) {
|
---|
| 594 | iorb_seterr(iorb, IOERR_RBA_LIMIT);
|
---|
| 595 | return(-1);
|
---|
| 596 | }
|
---|
| 597 | rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY_EXT,
|
---|
| 598 | AP_SECTOR_48, (u32) io->RBA, (u16) 0,
|
---|
| 599 | AP_COUNT, (u16) io->BlockCount,
|
---|
| 600 | AP_DEVICE, 0x4000,
|
---|
| 601 | AP_END);
|
---|
| 602 | } else {
|
---|
| 603 | rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY,
|
---|
| 604 | AP_SECTOR_28, (u32) io->RBA,
|
---|
| 605 | AP_COUNT, (u16) io->BlockCount & 0xffU,
|
---|
| 606 | AP_END);
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | return(rc);
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | /******************************************************************************
|
---|
| 613 | * Write sectors to AHCI device.
|
---|
| 614 | */
|
---|
| 615 | int ata_write(IORBH _far *iorb, int slot)
|
---|
| 616 | {
|
---|
| 617 | IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
|
---|
| 618 | AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
|
---|
[8] | 619 | ULONG sector = io->RBA + io->BlocksXferred;
|
---|
| 620 | USHORT count = io->BlockCount - io->BlocksXferred;
|
---|
| 621 | USHORT sg_indx;
|
---|
| 622 | USHORT sg_cnt;
|
---|
[4] | 623 | int p = iorb_unit_port(iorb);
|
---|
| 624 | int d = iorb_unit_device(iorb);
|
---|
| 625 | int rc;
|
---|
| 626 |
|
---|
[8] | 627 | /* prepare write command while keeping an eye on S/G count limitations */
|
---|
| 628 | do {
|
---|
| 629 | sg_indx = ata_get_sg_indx(io);
|
---|
| 630 | sg_cnt = io->cSGList - sg_indx;
|
---|
| 631 |
|
---|
| 632 | if (sector >= (1UL << 28) || count > 256) {
|
---|
| 633 | /* need LBA48 for this command */
|
---|
| 634 | if (!ai->ports[p].devs[d].lba48) {
|
---|
| 635 | iorb_seterr(iorb, IOERR_RBA_LIMIT);
|
---|
| 636 | return(-1);
|
---|
| 637 | }
|
---|
| 638 | rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE_EXT,
|
---|
| 639 | AP_SECTOR_48, (u32) sector, (u16) 0,
|
---|
| 640 | AP_COUNT, (u16) count,
|
---|
| 641 | AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
|
---|
| 642 | AP_DEVICE, 0x4000,
|
---|
| 643 | AP_WRITE,
|
---|
| 644 | AP_END);
|
---|
| 645 | } else {
|
---|
| 646 | rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE,
|
---|
| 647 | AP_SECTOR_28, (u32) sector,
|
---|
| 648 | AP_COUNT, (u16) count & 0xffU,
|
---|
| 649 | AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
|
---|
| 650 | AP_DEVICE, 0x4000,
|
---|
| 651 | AP_WRITE,
|
---|
| 652 | AP_END);
|
---|
[4] | 653 | }
|
---|
[8] | 654 |
|
---|
| 655 | if (rc > 0) {
|
---|
| 656 | /* couldn't map all S/G elements */
|
---|
| 657 | ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
|
---|
| 658 | }
|
---|
| 659 | } while (rc > 0 && sg_cnt > 0);
|
---|
| 660 |
|
---|
| 661 | if (rc == 0) {
|
---|
| 662 | add_workspace(iorb)->blocks = count;
|
---|
| 663 | add_workspace(iorb)->ppfunc = ata_write_pp;
|
---|
| 664 |
|
---|
| 665 | } else if (rc > 0) {
|
---|
| 666 | iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
|
---|
| 667 |
|
---|
[4] | 668 | } else {
|
---|
[8] | 669 | iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
|
---|
[4] | 670 | }
|
---|
| 671 |
|
---|
| 672 | return(rc);
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | /******************************************************************************
|
---|
[8] | 676 | * Post processing function for ata_write(); this function updates the
|
---|
| 677 | * BlocksXferred counter in the IORB and, if not all blocks have been
|
---|
| 678 | * transferred, requeues the IORB to process the remaining sectors.
|
---|
| 679 | */
|
---|
| 680 | void ata_write_pp(IORBH _far *iorb)
|
---|
| 681 | {
|
---|
| 682 | IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
|
---|
| 683 |
|
---|
| 684 | io->BlocksXferred += add_workspace(iorb)->blocks;
|
---|
| 685 | ddprintf("ata_write_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
|
---|
| 686 |
|
---|
| 687 | if (io->BlocksXferred >= io->BlockCount) {
|
---|
| 688 | /* we're done; tell IRQ handler the IORB is complete */
|
---|
| 689 | add_workspace(iorb)->complete = 1;
|
---|
| 690 | } else {
|
---|
| 691 | /* requeue this IORB for next iteration */
|
---|
| 692 | iorb_requeue(iorb);
|
---|
| 693 | }
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | /******************************************************************************
|
---|
[4] | 697 | * Execute ATA command.
|
---|
| 698 | */
|
---|
| 699 | int ata_execute_ata(IORBH _far *iorb, int slot)
|
---|
| 700 | {
|
---|
| 701 | iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
|
---|
| 702 | return(-1);
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | /******************************************************************************
|
---|
[8] | 706 | * Request sense information for a failed command. Since there is no "request
|
---|
| 707 | * sense" command for ATA devices, we need to read the current error code from
|
---|
| 708 | * the AHCI task file register and fabricate the sense information.
|
---|
| 709 | *
|
---|
| 710 | * NOTES:
|
---|
| 711 | *
|
---|
| 712 | * - This function must be called right after an ATA command has failed and
|
---|
| 713 | * before any other commands are queued on the corresponding port. This
|
---|
| 714 | * function is typically called in the port restart context hook which is
|
---|
| 715 | * triggered by an AHCI error interrupt.
|
---|
| 716 | *
|
---|
| 717 | * - The ATA error bits are a complete mess. We'll try and catch the most
|
---|
| 718 | * interesting error codes (such as medium errors) and report everything
|
---|
| 719 | * else with a generic error code.
|
---|
[4] | 720 | */
|
---|
| 721 | int ata_req_sense(IORBH _far *iorb, int slot)
|
---|
| 722 | {
|
---|
[8] | 723 | AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
|
---|
| 724 | u8 _far *port_mmio = port_base(ai, iorb_unit_port(iorb));
|
---|
| 725 | u32 tf_data = readl(port_mmio + PORT_TFDATA);
|
---|
| 726 | u8 err = (u8) (tf_data >> 8);
|
---|
| 727 | u8 sts = (u8) (tf_data);
|
---|
| 728 |
|
---|
| 729 | if (sts & ATA_ERR) {
|
---|
| 730 | if (sts & ATA_DF) {
|
---|
| 731 | /* there is a device-specific error condition */
|
---|
| 732 | if (err & ATA_ICRC) {
|
---|
| 733 | iorb_seterr(iorb, IOERR_ADAPTER_DEVICEBUSCHECK);
|
---|
| 734 | } else if (err & ATA_UNC) {
|
---|
| 735 | iorb_seterr(iorb, IOERR_MEDIA);
|
---|
| 736 | } else if (err & ATA_IDNF) {
|
---|
| 737 | iorb_seterr(iorb, IOERR_RBA_ADDRESSING_ERROR);
|
---|
| 738 | } else {
|
---|
| 739 | iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | } else {
|
---|
| 743 | iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
|
---|
| 744 | }
|
---|
| 745 | }
|
---|
| 746 |
|
---|
| 747 | /* TBD: fill in SCSI sense buffer in IORB */
|
---|
| 748 |
|
---|
| 749 | /* Return an error to indicate there's no HW command to be submitted and
|
---|
| 750 | * that the IORB can be completed "as is" (the upstream code expects the
|
---|
| 751 | * IORB error code, if any, to be set when this happens and this is exactly
|
---|
| 752 | * what this function is all about).
|
---|
| 753 | */
|
---|
[4] | 754 | return(-1);
|
---|
| 755 | }
|
---|
| 756 |
|
---|
| 757 | /******************************************************************************
|
---|
| 758 | * Extract vendor and device name from an ATA INDENTIFY buffer. Since strings
|
---|
| 759 | * in the indentify buffer are byte-swapped, we need to swap them back.
|
---|
| 760 | */
|
---|
| 761 | char *ata_dev_name(u16 *id_buf)
|
---|
| 762 | {
|
---|
| 763 | static char dev_name[ATA_ID_PROD_LEN + 1];
|
---|
| 764 | char *t = dev_name;
|
---|
| 765 | char *s = (char *) (id_buf + ATA_ID_PROD);
|
---|
| 766 | int i;
|
---|
| 767 |
|
---|
| 768 | dev_name[sizeof(dev_name)-1] = '\0';
|
---|
| 769 |
|
---|
| 770 | for (i = 0; i < ATA_ID_PROD_LEN / 2; i++) {
|
---|
| 771 | *(t++) = s[1];
|
---|
| 772 | *(t++) = s[0];
|
---|
| 773 | s += 2;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | return(dev_name);
|
---|
| 777 | }
|
---|
| 778 |
|
---|