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

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

cleanup

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