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

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

changes for drive geometry mapping

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