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

Last change on this file since 71 was 71, checked in by chris, 15 years ago
  • Added minimum NCQ depth for SATA devices which don't support NCQ at all
  • Removed ATAPI devices from the flush cache queue as they caused timeouts with a DVD drive in an HP test box
  • Various cosmetic changes to debug levels, etc.
File size: 31.2 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 }
504
505 /* MT: if neither ATA nor BIOS supply valid data, or if their
506 * CHS values result in a sector count lower than determined,
507 * we use a typical [SCSI] BIOS algorithm
508 */
509 if ((u32) geometry->NumHeads * (u32) geometry->SectorsPerTrack *
510 (u32) geometry->TotalCylinders < geometry->TotalSectors) {
511 geometry->NumHeads = 255;
512 geometry->SectorsPerTrack = 63;
513 geometry->TotalCylinders = geometry->TotalSectors /
514 ((u32) geometry->NumHeads *
515 (u32) geometry->SectorsPerTrack);
516 }
517
518 if (debug) {
519 printf("geometry information:\n");
520 printf(" heads: %d\n", (u16) geometry->NumHeads);
521 printf(" sectors: %d\n", (u16) geometry->SectorsPerTrack);
522 printf(" cylinders: %d\n", (u16) geometry->TotalCylinders);
523 printf(" capacity: %ldMB\n", (u32) (geometry->TotalSectors / 2048));
524 }
525
526 /* tell interrupt handler that this IORB is complete */
527 add_workspace(iorb)->complete = 1;
528}
529
530/******************************************************************************
531 * Test whether unit is ready.
532 */
533int ata_unit_ready(IORBH _far *iorb, int slot)
534{
535 /* This is a NOP for ATA devices (at least right now); returning an error
536 * without setting an error code means ahci_exec_iorb() will not queue any
537 * HW command and the IORB will complete successfully.
538 */
539 ((IORB_UNIT_STATUS _far *) iorb)->UnitStatus = US_READY | US_POWER;
540 return(-1);
541}
542
543/******************************************************************************
544 * Read sectors from AHCI device.
545 */
546int ata_read(IORBH _far *iorb, int slot)
547{
548 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
549 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
550 ULONG sector = io->RBA + io->BlocksXferred;
551 USHORT count = io->BlockCount - io->BlocksXferred;
552 USHORT sg_indx;
553 USHORT sg_cnt;
554 int p = iorb_unit_port(iorb);
555 int d = iorb_unit_device(iorb);
556 int rc;
557
558 /* prepare read command while keeping an eye on S/G count limitations */
559 do {
560 sg_indx = ata_get_sg_indx(io);
561 sg_cnt = io->cSGList - sg_indx;
562
563 if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
564 /* need LBA48 for this command */
565 if (!ai->ports[p].devs[d].lba48) {
566 iorb_seterr(iorb, IOERR_RBA_LIMIT);
567 return(-1);
568 }
569 if (add_workspace(iorb)->is_ncq) {
570 /* use NCQ read; count goes into feature register, tag into count! */
571 rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_READ,
572 AP_SECTOR_48, (u32) sector, (u16) 0,
573 AP_FEATURES, (u16) count,
574 AP_COUNT, (u16) (slot << 3), /* tag = slot */
575 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
576 AP_DEVICE, 0x4000,
577 AP_END);
578 } else {
579 rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ_EXT,
580 AP_SECTOR_48, (u32) sector, (u16) 0,
581 AP_COUNT, (u16) count,
582 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
583 AP_DEVICE, 0x4000,
584 AP_END);
585 }
586
587 } else {
588 rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ,
589 AP_SECTOR_28, (u32) sector,
590 AP_COUNT, (u16) count & 0xffU,
591 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
592 AP_DEVICE, 0x4000,
593 AP_END);
594 }
595
596 if (rc > 0) {
597 /* couldn't map all S/G elements */
598 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
599 }
600 } while (rc > 0 && sg_cnt > 0);
601
602 if (rc == 0) {
603 add_workspace(iorb)->blocks = count;
604 add_workspace(iorb)->ppfunc = ata_read_pp;
605
606 } else if (rc > 0) {
607 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
608
609 } else {
610 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
611 }
612
613 return(rc);
614}
615
616/******************************************************************************
617 * Post processing function for ata_read(); this function updates the
618 * BlocksXferred counter in the IORB and, if not all blocks have been
619 * transferred, requeues the IORB to process the remaining sectors.
620 */
621void ata_read_pp(IORBH _far *iorb)
622{
623 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
624
625 io->BlocksXferred += add_workspace(iorb)->blocks;
626 dprintf("ata_read_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
627
628 if (io->BlocksXferred >= io->BlockCount) {
629 /* we're done; tell IRQ handler the IORB is complete */
630 add_workspace(iorb)->complete = 1;
631 } else {
632 /* requeue this IORB for next iteration */
633 iorb_requeue(iorb);
634 }
635}
636
637/******************************************************************************
638 * Verify readability of sectors on ATA device.
639 */
640int ata_verify(IORBH _far *iorb, int slot)
641{
642 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
643 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
644 int p = iorb_unit_port(iorb);
645 int d = iorb_unit_device(iorb);
646 int rc;
647
648 /* prepare verify command */
649 if (io->RBA >= (1UL << 28) || io->BlockCount > 256) {
650 /* need LBA48 for this command */
651 if (!ai->ports[p].devs[d].lba48) {
652 iorb_seterr(iorb, IOERR_RBA_LIMIT);
653 return(-1);
654 }
655 rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY_EXT,
656 AP_SECTOR_48, (u32) io->RBA, (u16) 0,
657 AP_COUNT, (u16) io->BlockCount,
658 AP_DEVICE, 0x4000,
659 AP_END);
660 } else {
661 rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY,
662 AP_SECTOR_28, (u32) io->RBA,
663 AP_COUNT, (u16) io->BlockCount & 0xffU,
664 AP_END);
665 }
666
667 return(rc);
668}
669
670/******************************************************************************
671 * Write sectors to AHCI device.
672 */
673int ata_write(IORBH _far *iorb, int slot)
674{
675 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
676 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
677 ULONG sector = io->RBA + io->BlocksXferred;
678 USHORT count = io->BlockCount - io->BlocksXferred;
679 USHORT sg_indx;
680 USHORT sg_cnt;
681 int p = iorb_unit_port(iorb);
682 int d = iorb_unit_device(iorb);
683 int rc;
684
685 /* prepare write command while keeping an eye on S/G count limitations */
686 do {
687 sg_indx = ata_get_sg_indx(io);
688 sg_cnt = io->cSGList - sg_indx;
689
690 if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
691 /* need LBA48 for this command */
692 if (!ai->ports[p].devs[d].lba48) {
693 iorb_seterr(iorb, IOERR_RBA_LIMIT);
694 return(-1);
695 }
696 if (add_workspace(iorb)->is_ncq) {
697 /* use NCQ write; count goes into feature register, tag into count! */
698 rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_WRITE,
699 AP_SECTOR_48, (u32) sector, (u16) 0,
700 AP_FEATURES, (u16) count,
701 AP_COUNT, (u16) (slot << 3), /* tag = slot */
702 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
703 AP_DEVICE, 0x4000,
704 AP_DEVICE, (io->Flags & XIO_DISABLE_HW_WRITE_CACHE) ?
705 0x8000 : 0, /* force unit access */
706 AP_WRITE, 1,
707 AP_END);
708 } else {
709 rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE_EXT,
710 AP_SECTOR_48, (u32) sector, (u16) 0,
711 AP_COUNT, (u16) count,
712 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
713 AP_DEVICE, 0x4000,
714 AP_WRITE, 1,
715 AP_END);
716 }
717
718 } else {
719 rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE,
720 AP_SECTOR_28, (u32) sector,
721 AP_COUNT, (u16) count & 0xffU,
722 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
723 AP_DEVICE, 0x4000,
724 AP_WRITE, 1,
725 AP_END);
726 }
727
728 if (rc > 0) {
729 /* couldn't map all S/G elements */
730 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
731 }
732 } while (rc > 0 && sg_cnt > 0);
733
734 if (rc == 0) {
735 add_workspace(iorb)->blocks = count;
736 add_workspace(iorb)->ppfunc = ata_write_pp;
737
738 } else if (rc > 0) {
739 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
740
741 } else {
742 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
743 }
744
745 return(rc);
746}
747
748/******************************************************************************
749 * Post processing function for ata_write(); this function updates the
750 * BlocksXferred counter in the IORB and, if not all blocks have been
751 * transferred, requeues the IORB to process the remaining sectors.
752 */
753void ata_write_pp(IORBH _far *iorb)
754{
755 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
756
757 io->BlocksXferred += add_workspace(iorb)->blocks;
758 dprintf("ata_write_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
759
760 if (io->BlocksXferred >= io->BlockCount) {
761 /* we're done; tell IRQ handler the IORB is complete */
762 add_workspace(iorb)->complete = 1;
763 } else {
764 /* requeue this IORB for next iteration */
765 iorb_requeue(iorb);
766 }
767}
768
769/******************************************************************************
770 * Execute ATA command.
771 */
772int ata_execute_ata(IORBH _far *iorb, int slot)
773{
774 IORB_ADAPTER_PASSTHRU _far *apt = (IORB_ADAPTER_PASSTHRU _far *) iorb;
775 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
776 int p = iorb_unit_port(iorb);
777 int d = iorb_unit_device(iorb);
778 int rc;
779
780 if (apt->ControllerCmdLen != sizeof(ATA_CMD)) {
781 iorb_seterr(iorb, IOERR_CMD_SYNTAX);
782 return(-1);
783 }
784
785 rc = ata_cmd(ai, p, d, slot, 0,
786 AP_SGLIST, apt->pSGList, apt->cSGList,
787 AP_ATA_CMD, apt->pControllerCmd,
788 AP_WRITE, !(apt->Flags & PT_DIRECTION_IN),
789 AP_END);
790
791 return(rc);
792}
793
794/******************************************************************************
795 * Request sense information for a failed command. Since there is no "request
796 * sense" command for ATA devices, we need to read the current error code from
797 * the AHCI task file register and fabricate the sense information.
798 *
799 * NOTES:
800 *
801 * - This function must be called right after an ATA command has failed and
802 * before any other commands are queued on the corresponding port. This
803 * function is typically called in the port restart context hook which is
804 * triggered by an AHCI error interrupt.
805 *
806 * - The ATA error bits are a complete mess. We'll try and catch the most
807 * interesting error codes (such as medium errors) and report everything
808 * else with a generic error code.
809 */
810int ata_req_sense(IORBH _far *iorb, int slot)
811{
812 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
813 u8 _far *port_mmio = port_base(ai, iorb_unit_port(iorb));
814 u32 tf_data = readl(port_mmio + PORT_TFDATA);
815 u8 err = (u8) (tf_data >> 8);
816 u8 sts = (u8) (tf_data);
817
818 if (sts & ATA_ERR) {
819 if (sts & ATA_DF) {
820 /* there is a device-specific error condition */
821 if (err & ATA_ICRC) {
822 iorb_seterr(iorb, IOERR_ADAPTER_DEVICEBUSCHECK);
823 } else if (err & ATA_UNC) {
824 iorb_seterr(iorb, IOERR_MEDIA);
825 } else if (err & ATA_IDNF) {
826 iorb_seterr(iorb, IOERR_RBA_ADDRESSING_ERROR);
827 } else {
828 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
829 }
830
831 } else {
832 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
833 }
834 }
835
836 /* Return an error to indicate there's no HW command to be submitted and
837 * that the IORB can be completed "as is" (the upstream code expects the
838 * IORB error code, if any, to be set when this happens and this is exactly
839 * what this function is all about).
840 */
841 return(-1);
842}
843
844/******************************************************************************
845 * Extract vendor and device name from an ATA INDENTIFY buffer. Since strings
846 * in the indentify buffer are byte-swapped, we need to swap them back.
847 */
848char *ata_dev_name(u16 *id_buf)
849{
850 static char dev_name[ATA_ID_PROD_LEN + 1];
851 char *t = dev_name;
852 char *s = (char *) (id_buf + ATA_ID_PROD);
853 int i;
854
855 dev_name[sizeof(dev_name)-1] = '\0';
856
857 for (i = 0; i < ATA_ID_PROD_LEN / 2; i++) {
858 *(t++) = s[1];
859 *(t++) = s[0];
860 s += 2;
861 }
862
863 return(dev_name);
864}
865
Note: See TracBrowser for help on using the repository browser.