source: trunk/src/os2ahci/ata.c@ 80

Last change on this file since 80 was 80, checked in by chris, 14 years ago

Version 1.06
============

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