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

Last change on this file since 13 was 13, checked in by root, 15 years ago

latest NCQ changes from Christian

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