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

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