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

Last change on this file since 82 was 82, checked in by chris, 14 years ago

Version 1.09
============

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