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

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

dump LBA28/LBA48 and hex dump of FIS to serial terminal

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