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

Last change on this file since 57 was 57, checked in by markus, 15 years ago

fixed ata_cmd to FIS mapping (LBA-28); removed sector mapping test code

File size: 31.8 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 dprintf("LBA28: 0x%08lx\n", ata_cmd.lba_l);
121 if (ata_cmd.lba_l & 0xf0000000UL) {
122 dprintf("error: LBA-28 address %ld has more than 28 bits\n", ata_cmd.lba_l);
123 return(-1);
124 }
125 /* add upper 4 bits to device field */
126 ata_cmd.device |= (ata_cmd.lba_l >> 24) & 0x0fU;
127 /* only lower 24 bits come into _debug lba_l */
128 ata_cmd.lba_l &= 0x00ffffffUL;
129 break;
130
131 case AP_SECTOR_48:
132 /* 48-bit sector address */
133 ata_cmd.lba_l = va_arg(va, u32);
134 ata_cmd.lba_h = va_arg(va, u16);
135 dprintf("LBA48: 0x%08lx\n", ata_cmd.lba_l);
136 break;
137
138 case AP_DEVICE:
139 /* ATA device byte; note that this byte contains the highest
140 * 4 bits of LBA-28 address; we have to leave them alone here. */
141 ata_cmd.device |= (va_arg(va, u16) & 0xf000U) >> 8;
142 break;
143
144 case AP_SGLIST:
145 /* scatter/gather list in SCATGATENTRY/count format */
146 sg_list = va_arg(va, void _far *);
147 sg_cnt = va_arg(va, u16);
148 break;
149
150 case AP_VADDR:
151 /* virtual buffer address in addr/len format (up to 4K) */
152 DevHelp_VirtToPhys(va_arg(va, void _far *), &sg_single.ppXferBuf);
153 sg_single.XferBufLen = va_arg(va, u16);
154 sg_list = &sg_single;
155 sg_cnt = 1;
156 break;
157
158 case AP_ATAPI_CMD:
159 /* ATAPI command */
160 atapi_cmd = va_arg(va, void _far *);
161 atapi_cmd_len = va_arg(va, u16);
162 ahci_flags |= AHCI_CMD_ATAPI;
163 break;
164
165 case AP_ATA_CMD:
166 /* ATA command "pass-through" */
167 memcpy(&ata_cmd, va_arg(va, void _far *), sizeof(ATA_CMD));
168 break;
169
170 case AP_END:
171 break;
172
173 default:
174 dprintf("error: v_ata_cmd() called with invalid parameter type (%d)\n", (int) ap);
175 return(-1);
176 }
177
178 } while (ap != AP_END);
179
180 /* --------------------------------------------------------------------------
181 * Fill in AHCI ATA command information. This includes the port command slot,
182 * the corresponding command FIS and the S/G list. The layout of the AHCI
183 * port DMA region is based on the Linux AHCI driver and looks like this:
184 *
185 * - 32 AHCI command headers (AHCI_CMD_HDR) with 32 bytes, each
186 * - 1 FIS receive area with 256 bytes (AHCI_RX_FIS_SZ)
187 * - 32 AHCI command tables, each consisting of
188 * - 64 bytes for command FIS
189 * - 16 bytes for ATAPI comands
190 * - 48 bytes reserved
191 * - 48 S/G entries (AHCI_SG) with 32 bytes, each
192 *
193 * Since the whole DMA buffer for all ports is larger than 64KB and we need
194 * multiple segments to address all of them, there are no virtual pointers
195 * to the individual elements in AD_INFO. Instead, we're relying on macros
196 * for getting the base address of a particular port's DMA region, then
197 * map a structure on top of that for convenience (AHCI_PORT_DMA).
198 */
199 dma_base_virt = port_dma_base(ai, p);
200 dma_base_phys = port_dma_base_phys(ai, p);
201
202 /* AHCI command header */
203 cmd_hdr = dma_base_virt->cmd_hdr + slot;
204 memset(cmd_hdr, 0x00, sizeof(*cmd_hdr));
205 cmd_hdr->options = ((d & 0x0f) << 12);
206 cmd_hdr->options |= ahci_flags; /* AHCI commaand flags */
207 cmd_hdr->options |= 5; /* length of command FIS in 32-bit words */
208 cmd_hdr->tbl_addr = dma_base_phys + offsetof(AHCI_PORT_DMA, cmd_tbl[slot]);
209
210 /* AHCI command table */
211 cmd_tbl = dma_base_virt->cmd_tbl + slot;
212 memset(cmd_tbl, 0x00, sizeof(*cmd_tbl));
213 ata_cmd_to_fis(cmd_tbl->cmd_fis, &ata_cmd, d);
214
215 if (atapi_cmd != NULL) {
216 /* copy ATAPI command */
217 memcpy(cmd_tbl->atapi_cmd, atapi_cmd, atapi_cmd_len);
218 }
219
220 /* PRDT (S/G list)
221 *
222 * - The S/G list for AHCI adapters is limited to 22 bits for the transfer
223 * size of each element, thus we need to split S/G elements larger than
224 * 22 bits into 2 AHCI_SG elements.
225 *
226 * - The S/G element size for AHCI is what the spec calls "'0' based"
227 * (i.e. 0 means 1 bytes). On top of that, the spec requires S/G transfer
228 * sizes to be even in the context of 16-bit transfers, thus bit '1'
229 * always needs to be set.
230 *
231 * - AHCI_MAX_SG_ELEMENT_LEN defines the maximum size of an AHCI S/G
232 * element in bytes, ignoring the '0'-based methodology (i.e. 1 << 22).
233 *
234 * - There's a limit on the maximum number of S/G elements in the port DMA
235 * buffer (AHCI_MAX_SG) which is lower than the HW maximum. It's beyond
236 * the control of this function to split commands which require more
237 * than AHCI_MAX_SG entries. In order to help the caller, the return value
238 * of this function will indicate how many OS/2 S/G entries were
239 * successfully mapped.
240 *
241 */
242 for (i = n = 0; i < sg_cnt; i++) {
243 u32 sg_addr = sg_list[i].ppXferBuf;
244 u32 sg_size = sg_list[i].XferBufLen;
245
246 do {
247 u32 chunk = (sg_size > AHCI_MAX_SG_ELEMENT_LEN) ? AHCI_MAX_SG_ELEMENT_LEN
248 : sg_size;
249 if (n >= AHCI_MAX_SG) {
250 /* couldn't store all S/G elements in our DMA buffer */
251 ddprintf("ata_cmd(): too many S/G elements\n");
252 return(i - 1);
253 }
254 cmd_tbl->sg_list[n].addr = sg_addr;
255 cmd_tbl->sg_list[n].size = chunk - 1;
256 sg_addr += chunk;
257 sg_size -= chunk;
258 n++;
259 } while (sg_size > 0);
260 }
261
262 /* set final S/G count in AHCI command header */
263 cmd_hdr->options |= (u32) n << 16;
264
265 if (debug >= 2) {
266 printf("ATA command for %d.%d.%d:\n", ad_no(ai), p, d);
267 phex(cmd_hdr, offsetof(AHCI_CMD_HDR, reserved), "cmd_hdr: ");
268 phex(&ata_cmd, sizeof(ata_cmd), "ata_cmd: ");
269 if (atapi_cmd != NULL) {
270 phex(atapi_cmd, atapi_cmd_len, "atapi_cmd: ");
271 }
272 if (n > 0) {
273 phex(cmd_tbl->sg_list, sizeof(*cmd_tbl->sg_list) * n, "sg_list: ");
274 }
275 }
276
277 return(0);
278}
279
280/******************************************************************************
281 * Fill SATA command FIS with values extracted from an ATA command structure.
282 * The command FIS buffer (fis) is expected to be initialized to 0s. The
283 * structure of the FIS maps to the ATA shadow register block, including
284 * registers which can be written twice to store 16 bits (called 'exp').
285 *
286 * The FIS structure looks like this (using LSB notation):
287 *
288 * +----------------+----------------+----------------+----------------+
289 * 00 | FIS type (27h) | C|R|R|R|PMP | Command | Features |
290 * +----------------+----------------+----------------+----------------+
291 * 04 | LBA 7:0 | LBA 15:8 | LBA 23:16 | R|R|R|D|Head |
292 * +----------------+----------------+----------------+----------------+
293 * 08 | LBA 31:24 | LBA 40:32 | LBA 47:40 | Features exp |
294 * +----------------+----------------+----------------+----------------+
295 * 12 | Count 7:0 | Count 15:8 | Reserved | Control |
296 * +----------------+----------------+----------------+----------------+
297 * 16 | Reserved | Reserved | Reserved | Reserved |
298 * +----------------+----------------+----------------+----------------+
299 */
300void ata_cmd_to_fis(u8 _far *fis, ATA_CMD _far *ata_cmd, int d)
301{
302 fis[0] = 0x27; /* register - host to device FIS */
303 fis[1] = (u8) (d & 0xf); /* port multiplier number */
304 fis[1] |= 0x80; /* bit 7 indicates Command FIS */
305 fis[2] = (u8) ata_cmd->cmd;
306 fis[3] = (u8) ata_cmd->features;
307
308 fis[4] = (u8) ata_cmd->lba_l;
309 fis[5] = (u8) (ata_cmd->lba_l >> 8);
310 fis[6] = (u8) (ata_cmd->lba_l >> 16);
311 fis[7] = (u8) ata_cmd->device;
312
313 fis[8] = (u8) (ata_cmd->lba_l >> 24);
314 fis[9] = (u8) ata_cmd->lba_h;
315 fis[10] = (u8) (ata_cmd->lba_h >> 8);
316 fis[11] = (u8) (ata_cmd->features >> 8);
317
318 fis[12] = (u8) ata_cmd->count;
319 fis[13] = (u8) (ata_cmd->count >> 8);
320}
321
322/******************************************************************************
323 * Get index in S/G list for the number of transferred sectors in the IORB.
324 *
325 * Returning io->cSGList indicates an error.
326 *
327 * NOTE: OS/2 makes sure S/G lists are set up such that entries at the HW
328 * limit will never cross sector boundaries. This means that splitting
329 * S/G lists into multiple commands can be done without editing the S/G
330 * lists.
331 */
332u16 ata_get_sg_indx(IORB_EXECUTEIO _far *io)
333{
334 ULONG offset = io->BlocksXferred * io->BlockSize;
335 USHORT i;
336
337 for (i = 0; i < io->cSGList && offset > 0; i++) {
338 offset -= io->pSGList[i].XferBufLen;
339 }
340
341 return(i);
342}
343
344/******************************************************************************
345 * Get max S/G count which will fit into our HW S/G buffers. This function is
346 * called when the S/G list is too long and we need to split the IORB into
347 * multiple commands. It returns both the number of sectors and S/G list
348 * elements that we can handle in a single command.
349 *
350 * The parameter 'sg_indx' indicates the current start index in the S/G list
351 * (0 if this is the first command iteration).
352 *
353 * The parameter 'sg_max' is the return value of v_ata_cmd() and indicates
354 * how many S/G elements were successfully mapped. Whatever we return needs to
355 * be less or equal to this value.
356 *
357 * Returning 0 in *sg_cnt indicates an error.
358 *
359 * NOTE: OS/2 makes sure S/G lists are set up such that entries at HW limits
360 * will never cross sector boundaries. This means that splitting S/G
361 * lists into multiple commands can be done without editing S/G list
362 * elements. Since AHCI only allows 22 bits for each S/G element, the
363 * hardware limits are reported as AHCI_MAX_SG / 2 but will vary based
364 * on the actual length of S/G elements. This function looks for the
365 * maximum number of S/G elements that can be mapped on sector
366 * boundaries which will still fit into our HW S/G list.
367 */
368void ata_max_sg_cnt(IORB_EXECUTEIO _far *io, USHORT sg_indx, USHORT sg_max,
369 USHORT _far *sg_cnt, USHORT _far *sector_cnt)
370{
371 ULONG max_sector_cnt = 0;
372 USHORT max_sg_cnt = 0;
373 ULONG offset = 0;
374 USHORT i;
375
376 for (i = sg_indx; i < io->cSGList; i++) {
377 if (i - sg_indx >= sg_max) {
378 /* we're beyond the number of S/G elements we can map */
379 break;
380 }
381
382 offset += io->pSGList[i].XferBufLen;
383 if (offset % io->BlockSize == 0) {
384 /* this S/G element ends on a sector boundary */
385 max_sector_cnt = offset / io->BlockSize;
386 max_sg_cnt = i + 1;
387 }
388 }
389
390 /* return the best match we found so far (0 indicating failure) */
391 *sector_cnt = max_sector_cnt;
392 *sg_cnt = max_sg_cnt;
393}
394
395
396/******************************************************************************
397 * Get device or media geometry. Device and media geometry are expected to be
398 * the same for non-removable devices, which will always be the case for the
399 * ATA devices we're dealing with (hard disks). ATAPI is a different story
400 * and handled by atapi_get_geometry().
401 */
402int ata_get_geometry(IORBH _far *iorb, int slot)
403{
404 ADD_WORKSPACE _far *aws = add_workspace(iorb);
405 int rc;
406
407 /* allocate buffer for ATA identify information */
408 if ((aws->buf = malloc(ATA_ID_WORDS * sizeof(u16))) == NULL) {
409 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
410 return(-1);
411 }
412
413 /* request ATA identify information */
414 aws->ppfunc = ata_get_geometry_pp;
415 rc = ata_cmd(ad_infos + iorb_unit_adapter(iorb),
416 iorb_unit_port(iorb),
417 iorb_unit_device(iorb),
418 slot,
419 ATA_CMD_ID_ATA,
420 AP_VADDR, (void _far *) aws->buf, ATA_ID_WORDS * sizeof(u16),
421 AP_END);
422
423 if (rc != 0) {
424 free(aws->buf);
425 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
426 }
427
428 return(rc);
429}
430
431/******************************************************************************
432 * Post processing function for ata_get_geometry(): convert the ATA identify
433 * information to OS/2 IOCC_GEOMETRY information.
434 */
435void ata_get_geometry_pp(IORBH _far *iorb)
436{
437 GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
438 USHORT geometry_len = ((IORB_GEOMETRY _far *) iorb)->GeometryLen;
439 u16 *id_buf = add_workspace(iorb)->buf;
440
441 /* Fill-in geometry information; the ATA-8 spec declares the geometry
442 * fields in the ATA ID buffer as obsolete but it's still the best
443 * guess in most cases. If the information stored in the geometry
444 * fields is apparently incorrect, we'll use the algorithm typically
445 * used by SCSI adapters and modern PC BIOS releases:
446 *
447 * - 512 bytes per sector
448 * - 255 heads
449 * - 63 sectors per track
450 * - x cylinders (calculated)
451 *
452 * Please note that os2ahci currently does not support ATA sectors larger
453 * than 512 bytes, therefore relies on the translation logic built into the
454 * corresponding ATA disks. In theory, partitions should be aligned to the
455 * large sectors to prevent needless mapping all over the place but HPFS
456 * uses logical block sizes smaller than the typical large sectors found on
457 * modern hard disks so this won't make much of a difference. Large sector
458 * support will be evaluated at a later time (it's unclear right now whether
459 * HPFS would even support anything larger than 512 bytes).
460 *
461 * Another limitation is that OS/2 has a 32-bit variable for the total number
462 * of sectors, limiting the maximum capacity to roughly 2TB. This is another
463 * issue that needs to be addressed sooner or later; large sectors could
464 * raise this limit to something like 8TB but this is not really much of a
465 * difference. Maybe there's something in later DDKs that allows more than
466 * 32 bits?
467 */
468 memset(geometry, 0x00, geometry_len);
469 geometry->BytesPerSector = 512;
470
471 /* extract total number of sectors */
472 if (id_buf[ATA_ID_CFS_ENABLE_2] & 0x400) {
473 /* 48-bit LBA supported */
474 if (ATA_CAPACITY48_H(id_buf) != 0) {
475 /* more than 32 bits for number of sectors */
476 dprintf("warning: limiting disk %d.%d.%d to 2TB\n",
477 iorb_unit_adapter(iorb), iorb_unit_port(iorb),
478 iorb_unit_device(iorb));
479 geometry->TotalSectors = 0xffffffffUL;
480 } else {
481 geometry->TotalSectors = ATA_CAPACITY48_L(id_buf);
482 }
483 } else {
484 /* 28-bit LBA */
485 geometry->TotalSectors = ATA_CAPACITY(id_buf) & 0x0fffffffUL;
486 }
487
488 /* see whether the "current" (read: BIOS-supplied) geometry looks OK */
489 if (CUR_HEADS(id_buf) > 0 && CUR_CYLS(id_buf) > 0 &&
490 CUR_SECTORS(id_buf) > 0 &&
491 CUR_CAPACITY(id_buf) == CUR_HEADS(id_buf) *
492 CUR_CYLS(id_buf) *
493 CUR_SECTORS(id_buf)) {
494 /* use BIOS-supplied values for geometry */
495 geometry->NumHeads = CUR_HEADS(id_buf);
496 geometry->SectorsPerTrack = CUR_SECTORS(id_buf);
497 geometry->TotalCylinders = CUR_CYLS(id_buf);
498
499 } else if (ATA_HEADS(id_buf) > 0 && ATA_CYLS(id_buf) > 0 &&
500 ATA_SECTORS(id_buf) > 0) {
501 /* use ATA-supplied values for geometry */
502 geometry->NumHeads = ATA_HEADS(id_buf);
503 geometry->SectorsPerTrack = ATA_SECTORS(id_buf);
504 geometry->TotalCylinders = ATA_CYLS(id_buf);
505
506 }
507
508 /* MT: if neither ATA nor BIOS supply valid data, or if their
509 * CHS values result in a sector count lower than determined,
510 * we use a typical [SCSI] BIOS algorithm
511 */
512 if ((u32) geometry->NumHeads * (u32) geometry->SectorsPerTrack *
513 (u32) geometry->TotalCylinders < geometry->TotalSectors) {
514 geometry->NumHeads = 255;
515 geometry->SectorsPerTrack = 63;
516 geometry->TotalCylinders = geometry->TotalSectors /
517 ((u32) geometry->NumHeads *
518 (u32) geometry->SectorsPerTrack);
519 }
520
521 if (debug) {
522 printf("geometry information:\n");
523 printf(" heads: %d\n", (u16) geometry->NumHeads);
524 printf(" sectors: %d\n", (u16) geometry->SectorsPerTrack);
525 printf(" cylinders: %d\n", (u16) geometry->TotalCylinders);
526 printf(" capacity: %ldMB\n", (u32) (geometry->TotalSectors / 2048));
527 }
528
529 /* tell interrupt handler that this IORB is complete */
530 add_workspace(iorb)->complete = 1;
531}
532
533/******************************************************************************
534 * Test whether unit is ready.
535 */
536int ata_unit_ready(IORBH _far *iorb, int slot)
537{
538 /* This is a NOP for ATA devices (at least right now); returning an error
539 * without setting an error code means ahci_exec_iorb() will not queue any
540 * HW command and the IORB will complete successfully.
541 */
542 ((IORB_UNIT_STATUS _far *) iorb)->UnitStatus = US_READY | US_POWER;
543 return(-1);
544}
545
546/******************************************************************************
547 * Read sectors from AHCI device.
548 */
549int ata_read(IORBH _far *iorb, int slot)
550{
551 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
552 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
553 ULONG sector = io->RBA + io->BlocksXferred;
554 USHORT count = io->BlockCount - io->BlocksXferred;
555 USHORT sg_indx;
556 USHORT sg_cnt;
557 int p = iorb_unit_port(iorb);
558 int d = iorb_unit_device(iorb);
559 int rc;
560
561 /* prepare read command while keeping an eye on S/G count limitations */
562 do {
563 sg_indx = ata_get_sg_indx(io);
564 sg_cnt = io->cSGList - sg_indx;
565
566 if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
567 /* need LBA48 for this command */
568 if (!ai->ports[p].devs[d].lba48) {
569 iorb_seterr(iorb, IOERR_RBA_LIMIT);
570 return(-1);
571 }
572 if (add_workspace(iorb)->is_ncq) {
573 /* use NCQ read; count goes into feature register, tag into count! */
574 rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_READ,
575 AP_SECTOR_48, (u32) sector, (u16) 0,
576 AP_FEATURES, (u16) count,
577 AP_COUNT, (u16) slot, /* tag = slot */
578 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
579 AP_DEVICE, 0x4000,
580 AP_END);
581 } else {
582 rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ_EXT,
583 AP_SECTOR_48, (u32) sector, (u16) 0,
584 AP_COUNT, (u16) count,
585 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
586 AP_DEVICE, 0x4000,
587 AP_END);
588 }
589
590 } else {
591 rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ,
592 AP_SECTOR_28, (u32) sector,
593 AP_COUNT, (u16) count & 0xffU,
594 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
595 AP_DEVICE, 0x4000,
596 AP_END);
597 }
598
599 if (rc > 0) {
600 /* couldn't map all S/G elements */
601 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
602 }
603 } while (rc > 0 && sg_cnt > 0);
604
605 if (rc == 0) {
606 add_workspace(iorb)->blocks = count;
607 add_workspace(iorb)->ppfunc = ata_read_pp;
608
609 } else if (rc > 0) {
610 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
611
612 } else {
613 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
614 }
615
616 return(rc);
617}
618
619/******************************************************************************
620 * Post processing function for ata_read(); this function updates the
621 * BlocksXferred counter in the IORB and, if not all blocks have been
622 * transferred, requeues the IORB to process the remaining sectors.
623 */
624void ata_read_pp(IORBH _far *iorb)
625{
626 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
627/* unsigned char _far *p; */
628/* u16 mode_sw; */
629
630 io->BlocksXferred += add_workspace(iorb)->blocks;
631 ddprintf("ata_read_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
632
633 if (io->BlocksXferred >= io->BlockCount) {
634 /* we're done; tell IRQ handler the IORB is complete */
635 add_workspace(iorb)->complete = 1;
636 } else {
637 /* requeue this IORB for next iteration */
638 iorb_requeue(iorb);
639/* printf("MT: IORB requeued\n"); */
640 }
641
642 /* MT: print hex dump of first sector read */
643/* if (io->cSGList) { */
644/* if (DevHelp_PhysToVirt(io->pSGList->ppXferBuf, */
645/* (USHORT) io->pSGList->XferBufLen, */
646/* (PVOID) &p, &mode_sw)) { */
647/* printf("MT: failed to convert S/G pointer to virt address\n"); */
648/* } else { */
649/* phex(p, (int) io->pSGList->XferBufLen, "sector dump, RBA=0x%08lx\n", */
650/* io->RBA); */
651/* } */
652/* } */
653
654}
655
656/******************************************************************************
657 * Verify readability of sectors on ATA device.
658 */
659int ata_verify(IORBH _far *iorb, int slot)
660{
661 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
662 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
663 int p = iorb_unit_port(iorb);
664 int d = iorb_unit_device(iorb);
665 int rc;
666
667 /* prepare verify command */
668 if (io->RBA >= (1UL << 28) || io->BlockCount > 256) {
669 /* need LBA48 for this command */
670 if (!ai->ports[p].devs[d].lba48) {
671 iorb_seterr(iorb, IOERR_RBA_LIMIT);
672 return(-1);
673 }
674 rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY_EXT,
675 AP_SECTOR_48, (u32) io->RBA, (u16) 0,
676 AP_COUNT, (u16) io->BlockCount,
677 AP_DEVICE, 0x4000,
678 AP_END);
679 } else {
680 rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY,
681 AP_SECTOR_28, (u32) io->RBA,
682 AP_COUNT, (u16) io->BlockCount & 0xffU,
683 AP_END);
684 }
685
686 return(rc);
687}
688
689/******************************************************************************
690 * Write sectors to AHCI device.
691 */
692int ata_write(IORBH _far *iorb, int slot)
693{
694 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
695 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
696 ULONG sector = io->RBA + io->BlocksXferred;
697 USHORT count = io->BlockCount - io->BlocksXferred;
698 USHORT sg_indx;
699 USHORT sg_cnt;
700 int p = iorb_unit_port(iorb);
701 int d = iorb_unit_device(iorb);
702 int rc;
703
704 /* prepare write command while keeping an eye on S/G count limitations */
705 do {
706 sg_indx = ata_get_sg_indx(io);
707 sg_cnt = io->cSGList - sg_indx;
708
709 if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
710 /* need LBA48 for this command */
711 if (!ai->ports[p].devs[d].lba48) {
712 iorb_seterr(iorb, IOERR_RBA_LIMIT);
713 return(-1);
714 }
715 if (add_workspace(iorb)->is_ncq) {
716 /* use NCQ write; count goes into feature register, tag into count! */
717 rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_WRITE,
718 AP_SECTOR_48, (u32) sector, (u16) 0,
719 AP_FEATURES, (u16) count,
720 AP_COUNT, (u16) slot, /* tag = slot */
721 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
722 AP_DEVICE, 0xc000, /* force unit access (FUA) */
723 AP_WRITE, 1,
724 AP_END);
725 } else {
726 rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE_EXT,
727 AP_SECTOR_48, (u32) sector, (u16) 0,
728 AP_COUNT, (u16) count,
729 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
730 AP_DEVICE, 0x4000,
731 AP_WRITE, 1,
732 AP_END);
733 }
734
735 } else {
736 rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE,
737 AP_SECTOR_28, (u32) sector,
738 AP_COUNT, (u16) count & 0xffU,
739 AP_SGLIST, io->pSGList + sg_indx, (u16) sg_cnt,
740 AP_DEVICE, 0x4000,
741 AP_WRITE, 1,
742 AP_END);
743 }
744
745 if (rc > 0) {
746 /* couldn't map all S/G elements */
747 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
748 }
749 } while (rc > 0 && sg_cnt > 0);
750
751 if (rc == 0) {
752 add_workspace(iorb)->blocks = count;
753 add_workspace(iorb)->ppfunc = ata_write_pp;
754
755 } else if (rc > 0) {
756 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
757
758 } else {
759 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
760 }
761
762 return(rc);
763}
764
765/******************************************************************************
766 * Post processing function for ata_write(); this function updates the
767 * BlocksXferred counter in the IORB and, if not all blocks have been
768 * transferred, requeues the IORB to process the remaining sectors.
769 */
770void ata_write_pp(IORBH _far *iorb)
771{
772 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
773
774 io->BlocksXferred += add_workspace(iorb)->blocks;
775 ddprintf("ata_write_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
776
777 if (io->BlocksXferred >= io->BlockCount) {
778 /* we're done; tell IRQ handler the IORB is complete */
779 add_workspace(iorb)->complete = 1;
780 } else {
781 /* requeue this IORB for next iteration */
782 iorb_requeue(iorb);
783 }
784}
785
786/******************************************************************************
787 * Execute ATA command.
788 */
789int ata_execute_ata(IORBH _far *iorb, int slot)
790{
791 IORB_ADAPTER_PASSTHRU _far *apt = (IORB_ADAPTER_PASSTHRU _far *) iorb;
792 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
793 int p = iorb_unit_port(iorb);
794 int d = iorb_unit_device(iorb);
795 int rc;
796
797 if (apt->ControllerCmdLen != sizeof(ATA_CMD)) {
798 iorb_seterr(iorb, IOERR_CMD_SYNTAX);
799 return(-1);
800 }
801
802 rc = ata_cmd(ai, p, d, slot, 0,
803 AP_SGLIST, apt->pSGList, apt->cSGList,
804 AP_ATA_CMD, apt->pControllerCmd,
805 AP_WRITE, !(apt->Flags & PT_DIRECTION_IN),
806 AP_END);
807
808 return(rc);
809}
810
811/******************************************************************************
812 * Request sense information for a failed command. Since there is no "request
813 * sense" command for ATA devices, we need to read the current error code from
814 * the AHCI task file register and fabricate the sense information.
815 *
816 * NOTES:
817 *
818 * - This function must be called right after an ATA command has failed and
819 * before any other commands are queued on the corresponding port. This
820 * function is typically called in the port restart context hook which is
821 * triggered by an AHCI error interrupt.
822 *
823 * - The ATA error bits are a complete mess. We'll try and catch the most
824 * interesting error codes (such as medium errors) and report everything
825 * else with a generic error code.
826 */
827int ata_req_sense(IORBH _far *iorb, int slot)
828{
829 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
830 u8 _far *port_mmio = port_base(ai, iorb_unit_port(iorb));
831 u32 tf_data = readl(port_mmio + PORT_TFDATA);
832 u8 err = (u8) (tf_data >> 8);
833 u8 sts = (u8) (tf_data);
834
835 if (sts & ATA_ERR) {
836 if (sts & ATA_DF) {
837 /* there is a device-specific error condition */
838 if (err & ATA_ICRC) {
839 iorb_seterr(iorb, IOERR_ADAPTER_DEVICEBUSCHECK);
840 } else if (err & ATA_UNC) {
841 iorb_seterr(iorb, IOERR_MEDIA);
842 } else if (err & ATA_IDNF) {
843 iorb_seterr(iorb, IOERR_RBA_ADDRESSING_ERROR);
844 } else {
845 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
846 }
847
848 } else {
849 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
850 }
851 }
852
853 /* Return an error to indicate there's no HW command to be submitted and
854 * that the IORB can be completed "as is" (the upstream code expects the
855 * IORB error code, if any, to be set when this happens and this is exactly
856 * what this function is all about).
857 */
858 return(-1);
859}
860
861/******************************************************************************
862 * Extract vendor and device name from an ATA INDENTIFY buffer. Since strings
863 * in the indentify buffer are byte-swapped, we need to swap them back.
864 */
865char *ata_dev_name(u16 *id_buf)
866{
867 static char dev_name[ATA_ID_PROD_LEN + 1];
868 char *t = dev_name;
869 char *s = (char *) (id_buf + ATA_ID_PROD);
870 int i;
871
872 dev_name[sizeof(dev_name)-1] = '\0';
873
874 for (i = 0; i < ATA_ID_PROD_LEN / 2; i++) {
875 *(t++) = s[1];
876 *(t++) = s[0];
877 s += 2;
878 }
879
880 return(dev_name);
881}
882
Note: See TracBrowser for help on using the repository browser.