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

Last change on this file since 157 was 157, checked in by David Azarewicz, 12 years ago

Fixed up timer functions

File size: 42.4 KB
Line 
1/******************************************************************************
2 * ata.c - ATA command processing
3 *
4 * Copyright (c) 2011 thi.guten Software Development
5 * Copyright (c) 2011 Mensys B.V.
6 *
7 * Authors: Christian Mueller, Markus Thielen
8 *
9 * Parts copied from/inspired by the Linux AHCI driver;
10 * those parts are (c) Linux AHCI/ATA maintainers
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include "os2ahci.h"
28#include "ata.h"
29
30/* -------------------------- macros and constants ------------------------- */
31
32/* ------------------------ typedefs and structures ------------------------ */
33
34/* -------------------------- function prototypes -------------------------- */
35
36static int ata_cmd_read (IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
37 ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
38 ULONG sg_cnt);
39
40static int ata_cmd_write(IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
41 ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
42 ULONG sg_cnt, int write_through);
43
44/* ------------------------ global/static variables ------------------------ */
45
46/* ----------------------------- start of code ----------------------------- */
47
48/******************************************************************************
49 * Initialize AHCI command slot, FIS and S/G list for the specified ATA
50 * command. The command parameters are passed as a variable argument list
51 * of type and value(s). The list is terminated by AP_END.
52 *
53 * Notes:
54 *
55 * - The specified command slot is expected to be idle; no checks are
56 * performed to prevent messing with a busy port.
57 *
58 * - Port multipliers are not supported, yet, thus 'd' should always
59 * be 0 for the time being.
60 *
61 * - 'cmd' is passed as 16-bit integer because the compiler would push
62 * a 'u8' as 16-bit value (it's a fixed argument) and the stdarg
63 * macros would screw up the address of the first variable argument
64 * if the size of the last fixed argument wouldn't match what the
65 * compiler pushed on the stack.
66 *
67 * Return values:
68 * 0 : success
69 * > 0 : could not map all S/G entries; the return value is the number of
70 * S/G entries that could be mapped.
71 * < 0 : other error
72 */
73int ata_cmd(AD_INFO *ai, int p, int d, int slot, int cmd, ...)
74{
75 va_list va;
76 va_start(va, cmd);
77 return(v_ata_cmd(ai, p, d, slot, cmd, va));
78}
79
80int v_ata_cmd(AD_INFO *ai, int p, int d, int slot, int cmd, va_list va)
81{
82 AHCI_PORT_DMA _far *dma_base_virt;
83 AHCI_CMD_HDR _far *cmd_hdr;
84 AHCI_CMD_TBL _far *cmd_tbl;
85 SCATGATENTRY _far *sg_list = NULL;
86 SCATGATENTRY sg_single;
87 ATA_PARM ap;
88 ATA_CMD ata_cmd;
89 void _far *atapi_cmd = NULL;
90 u32 dma_base_phys;
91 u16 atapi_cmd_len = 0;
92 u16 ahci_flags = 0;
93 u16 sg_cnt = 0;
94 int i;
95 int n;
96
97 /* --------------------------------------------------------------------------
98 * Initialize ATA command. The ATA command is set up with the main command
99 * value and a variable list of additional parameters such as the sector
100 * address, transfer count, ...
101 */
102 memset(&ata_cmd, 0x00, sizeof(ata_cmd));
103 ata_cmd.cmd = (u8) cmd;
104
105 /* parse variable arguments */
106 do {
107 switch ((ap = va_arg(va, ATA_PARM))) {
108
109 case AP_AHCI_FLAGS:
110 ahci_flags |= va_arg(va, u16);
111 break;
112
113 case AP_WRITE:
114 if (va_arg(va, u16) != 0) {
115 ahci_flags |= AHCI_CMD_WRITE;
116 }
117 break;
118
119 case AP_FEATURES:
120 /* ATA features word */
121 ata_cmd.features |= va_arg(va, u16);
122 break;
123
124 case AP_COUNT:
125 /* transfer count */
126 ata_cmd.count = va_arg(va, u16);
127 break;
128
129 case AP_SECTOR_28:
130 /* 28-bit sector address */
131 ata_cmd.lba_l = va_arg(va, u32);
132 if (ata_cmd.lba_l & 0xf0000000UL) {
133 dprintf("error: LBA-28 address %ld has more than 28 bits\n", ata_cmd.lba_l);
134 return(ATA_CMD_INVALID_PARM);
135 }
136 /* add upper 4 bits to device field */
137 ata_cmd.device |= (ata_cmd.lba_l >> 24) & 0x0fU;
138 /* only lower 24 bits come into lba_l */
139 ata_cmd.lba_l &= 0x00ffffffUL;
140 break;
141
142 case AP_SECTOR_48:
143 /* 48-bit sector address */
144 ata_cmd.lba_l = va_arg(va, u32);
145 ata_cmd.lba_h = va_arg(va, u16);
146 break;
147
148 case AP_DEVICE:
149 /* ATA device byte; note that this byte contains the highest
150 * 4 bits of LBA-28 address; we have to leave them alone here. */
151 ata_cmd.device |= va_arg(va, u16) & 0xf0U;
152 break;
153
154 case AP_SGLIST:
155 /* scatter/gather list in SCATGATENTRY/count format */
156 sg_list = va_arg(va, void _far *);
157 sg_cnt = va_arg(va, u16);
158 break;
159
160 case AP_VADDR:
161 /* virtual buffer address in addr/len format (up to 4K) */
162 sg_single.ppXferBuf = virt_to_phys(va_arg(va, void _far *));
163 sg_single.XferBufLen = va_arg(va, u16);
164 sg_list = &sg_single;
165 sg_cnt = 1;
166 break;
167
168 case AP_ATAPI_CMD:
169 /* ATAPI command */
170 atapi_cmd = va_arg(va, void _far *);
171 atapi_cmd_len = va_arg(va, u16);
172 ahci_flags |= AHCI_CMD_ATAPI;
173 break;
174
175 case AP_ATA_CMD:
176 /* ATA command "pass-through" */
177 memcpy(&ata_cmd, va_arg(va, void _far *), sizeof(ATA_CMD));
178 break;
179
180 case AP_END:
181 break;
182
183 default:
184 dprintf("error: v_ata_cmd() called with invalid parameter type (%d)\n", (int) ap);
185 return(ATA_CMD_INVALID_PARM);
186 }
187
188 } while (ap != AP_END);
189
190 /* --------------------------------------------------------------------------
191 * Fill in AHCI ATA command information. This includes the port command slot,
192 * the corresponding command FIS and the S/G list. The layout of the AHCI
193 * port DMA region is based on the Linux AHCI driver and looks like this:
194 *
195 * - 32 AHCI command headers (AHCI_CMD_HDR) with 32 bytes, each
196 * - 1 FIS receive area with 256 bytes (AHCI_RX_FIS_SZ)
197 * - 32 AHCI command tables, each consisting of
198 * - 64 bytes for command FIS
199 * - 16 bytes for ATAPI comands
200 * - 48 bytes reserved
201 * - 48 S/G entries (AHCI_SG) with 32 bytes, each
202 *
203 * Since the whole DMA buffer for all ports is larger than 64KB and we need
204 * multiple segments to address all of them, there are no virtual pointers
205 * to the individual elements in AD_INFO. Instead, we're relying on macros
206 * for getting the base address of a particular port's DMA region, then
207 * map a structure on top of that for convenience (AHCI_PORT_DMA).
208 */
209 dma_base_virt = port_dma_base(ai, p);
210 dma_base_phys = port_dma_base_phys(ai, p);
211
212 /* AHCI command header */
213 cmd_hdr = dma_base_virt->cmd_hdr + slot;
214 memset(cmd_hdr, 0x00, sizeof(*cmd_hdr));
215 cmd_hdr->options = ((d & 0x0f) << 12);
216 cmd_hdr->options |= ahci_flags; /* AHCI command flags */
217 cmd_hdr->options |= 5; /* length of command FIS in 32-bit words */
218 cmd_hdr->tbl_addr = dma_base_phys + offsetof(AHCI_PORT_DMA, cmd_tbl[slot]);
219
220 /* AHCI command table */
221 cmd_tbl = dma_base_virt->cmd_tbl + slot;
222 memset(cmd_tbl, 0x00, sizeof(*cmd_tbl));
223 ata_cmd_to_fis(cmd_tbl->cmd_fis, &ata_cmd, d);
224
225 if (atapi_cmd != NULL) {
226 /* copy ATAPI command */
227 memcpy(cmd_tbl->atapi_cmd, atapi_cmd, atapi_cmd_len);
228 }
229
230 /* PRDT (S/G list)
231 *
232 * - The S/G list for AHCI adapters is limited to 22 bits for the transfer
233 * size of each element, thus we need to split S/G elements larger than
234 * 22 bits into 2 AHCI_SG elements.
235 *
236 * - The S/G element size for AHCI is what the spec calls '0'-based
237 * (i.e. 0 means 1 bytes). On top of that, the spec requires S/G transfer
238 * sizes to be even in the context of 16-bit transfers, thus bit '1'
239 * always needs to be set.
240 *
241 * - AHCI_MAX_SG_ELEMENT_LEN defines the maximum size of an AHCI S/G
242 * element in bytes, ignoring the '0'-based methodology (i.e. 1 << 22).
243 *
244 * - There's a limit on the maximum number of S/G elements in the port DMA
245 * buffer (AHCI_MAX_SG) which is lower than the HW maximum. It's beyond
246 * the control of this function to split commands which require more
247 * than AHCI_MAX_SG entries. In order to help the caller, the return value
248 * of this function will indicate how many OS/2 S/G entries were
249 * successfully mapped.
250 */
251 for (i = n = 0; i < sg_cnt; i++) {
252 u32 sg_addr = sg_list[i].ppXferBuf;
253 u32 sg_size = sg_list[i].XferBufLen;
254
255 do {
256 u32 chunk = (sg_size > AHCI_MAX_SG_ELEMENT_LEN) ? AHCI_MAX_SG_ELEMENT_LEN
257 : sg_size;
258 if (n >= AHCI_MAX_SG) {
259 /* couldn't store all S/G elements in our DMA buffer */
260 ddprintf("ata_cmd(): too many S/G elements\n");
261 return(i - 1);
262 }
263 if ((sg_addr & 1) || (chunk & 1)) {
264 ddprintf("error: ata_cmd() called with unaligned S/G element(s)\n");
265 return(ATA_CMD_UNALIGNED_ADDR);
266 }
267 cmd_tbl->sg_list[n].addr = sg_addr;
268 cmd_tbl->sg_list[n].size = chunk - 1;
269 sg_addr += chunk;
270 sg_size -= chunk;
271 n++;
272 } while (sg_size > 0);
273 }
274
275 /* set final S/G count in AHCI command header */
276 cmd_hdr->options |= (u32) n << 16;
277
278 if (debug >= 2) {
279 printf("ATA command for %d.%d.%d, slot %d:\n", ad_no(ai), p, d, slot);
280 phex(cmd_hdr, offsetof(AHCI_CMD_HDR, reserved), "cmd_hdr: ");
281 phex(&ata_cmd, sizeof(ata_cmd), "ata_cmd: ");
282 if (atapi_cmd != NULL) {
283 phex(atapi_cmd, atapi_cmd_len, "atapi_cmd: ");
284 }
285 if (n > 0) {
286 phex(cmd_tbl->sg_list, sizeof(*cmd_tbl->sg_list) * n, "sg_list: ");
287 }
288 }
289
290 return(ATA_CMD_SUCCESS);
291}
292
293/******************************************************************************
294 * Fill SATA command FIS with values extracted from an ATA command structure.
295 * The command FIS buffer (fis) is expected to be initialized to 0s. The
296 * structure of the FIS maps to the ATA shadow register block, including
297 * registers which can be written twice to store 16 bits (called 'exp').
298 *
299 * The FIS structure looks like this (using LSB notation):
300 *
301 * +----------------+----------------+----------------+----------------+
302 * 00 | FIS type (27h) | C|R|R|R|PMP | Command | Features |
303 * +----------------+----------------+----------------+----------------+
304 * 04 | LBA 7:0 | LBA 15:8 | LBA 23:16 | R|R|R|D|Head |
305 * +----------------+----------------+----------------+----------------+
306 * 08 | LBA 31:24 | LBA 40:32 | LBA 47:40 | Features exp |
307 * +----------------+----------------+----------------+----------------+
308 * 12 | Count 7:0 | Count 15:8 | Reserved | Control |
309 * +----------------+----------------+----------------+----------------+
310 * 16 | Reserved | Reserved | Reserved | Reserved |
311 * +----------------+----------------+----------------+----------------+
312 */
313void ata_cmd_to_fis(u8 _far *fis, ATA_CMD _far *ata_cmd, int d)
314{
315 fis[0] = 0x27; /* register - host to device FIS */
316 fis[1] = (u8) (d & 0xf); /* port multiplier number */
317 fis[1] |= 0x80; /* bit 7 indicates Command FIS */
318 fis[2] = (u8) ata_cmd->cmd;
319 fis[3] = (u8) ata_cmd->features;
320
321 fis[4] = (u8) ata_cmd->lba_l;
322 fis[5] = (u8) (ata_cmd->lba_l >> 8);
323 fis[6] = (u8) (ata_cmd->lba_l >> 16);
324 fis[7] = (u8) ata_cmd->device;
325
326 fis[8] = (u8) (ata_cmd->lba_l >> 24);
327 fis[9] = (u8) ata_cmd->lba_h;
328 fis[10] = (u8) (ata_cmd->lba_h >> 8);
329 fis[11] = (u8) (ata_cmd->features >> 8);
330
331 fis[12] = (u8) ata_cmd->count;
332 fis[13] = (u8) (ata_cmd->count >> 8);
333}
334
335/******************************************************************************
336 * Get index in S/G list for the number of transferred sectors in the IORB.
337 *
338 * Returning io->cSGList indicates an error.
339 *
340 * NOTE: OS/2 makes sure S/G lists are set up such that entries at the HW
341 * limit will never cross sector boundaries. This means that splitting
342 * S/G lists into multiple commands can be done without editing the S/G
343 * lists.
344 */
345u16 ata_get_sg_indx(IORB_EXECUTEIO _far *io)
346{
347 ULONG offset = io->BlocksXferred * io->BlockSize;
348 USHORT i;
349
350 for (i = 0; i < io->cSGList && offset > 0; i++) {
351 offset -= io->pSGList[i].XferBufLen;
352 }
353
354 return(i);
355}
356
357/******************************************************************************
358 * Get max S/G count which will fit into our HW S/G buffers. This function is
359 * called when the S/G list is too long and we need to split the IORB into
360 * multiple commands. It returns both the number of sectors and S/G list
361 * elements that we can handle in a single command.
362 *
363 * The parameter 'sg_indx' indicates the current start index in the S/G list
364 * (0 if this is the first command iteration).
365 *
366 * The parameter 'sg_max' is the return value of v_ata_cmd() and indicates
367 * how many S/G elements were successfully mapped. Whatever we return needs to
368 * be less or equal to this value.
369 *
370 * Returning 0 in *sg_cnt indicates an error.
371 *
372 * NOTE: OS/2 makes sure S/G lists are set up such that entries at HW limits
373 * will never cross sector boundaries. This means that splitting S/G
374 * lists into multiple commands can be done without editing S/G list
375 * elements. Since AHCI only allows 22 bits for each S/G element, the
376 * hardware limits are reported as AHCI_MAX_SG / 2 but will vary based
377 * on the actual length of S/G elements. This function looks for the
378 * maximum number of S/G elements that can be mapped on sector
379 * boundaries which will still fit into our HW S/G list.
380 */
381void ata_max_sg_cnt(IORB_EXECUTEIO _far *io, USHORT sg_indx, USHORT sg_max,
382 USHORT _far *sg_cnt, USHORT _far *sector_cnt)
383{
384 ULONG max_sector_cnt = 0;
385 USHORT max_sg_cnt = 0;
386 ULONG offset = 0;
387 USHORT i;
388
389 for (i = sg_indx; i < io->cSGList; i++) {
390 if (i - sg_indx >= sg_max) {
391 /* we're beyond the number of S/G elements we can map */
392 break;
393 }
394
395 offset += io->pSGList[i].XferBufLen;
396 if (offset % io->BlockSize == 0) {
397 /* this S/G element ends on a sector boundary */
398 max_sector_cnt = offset / io->BlockSize;
399 max_sg_cnt = i + 1;
400 }
401 }
402
403 /* return the best match we found (0 indicating failure) */
404 *sector_cnt = max_sector_cnt;
405 *sg_cnt = max_sg_cnt;
406}
407
408
409/******************************************************************************
410 * Get device or media geometry. Device and media geometry are expected to be
411 * the same for non-removable devices, which will always be the case for the
412 * ATA devices we're dealing with (hard disks). ATAPI is a different story
413 * and handled by atapi_get_geometry().
414 */
415int ata_get_geometry(IORBH _far *iorb, int slot)
416{
417 ADD_WORKSPACE _far *aws = add_workspace(iorb);
418 int rc;
419
420 /* allocate buffer for ATA identify information */
421 if ((aws->buf = malloc(ATA_ID_WORDS * sizeof(u16))) == NULL) {
422 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
423 return(-1);
424 }
425
426 /* request ATA identify information */
427 aws->ppfunc = ata_get_geometry_pp;
428 rc = ata_cmd(ad_infos + iorb_unit_adapter(iorb),
429 iorb_unit_port(iorb),
430 iorb_unit_device(iorb),
431 slot,
432 ATA_CMD_ID_ATA,
433 AP_VADDR, (void _far *) aws->buf, ATA_ID_WORDS * sizeof(u16),
434 AP_END);
435
436 if (rc != 0) {
437 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
438 }
439
440 return(rc);
441}
442
443int check_lvm(IORBH _far *iorb, ULONG sector)
444{
445 DLA_Table_Sector *pDLA = (DLA_Table_Sector*)add_workspace(iorb)->buf;
446 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
447 GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
448 int p = iorb_unit_port(iorb);
449 int rc;
450
451 rc = ahci_exec_polled_cmd(ai, p, 0, 500, ATA_CMD_READ,
452 AP_SECTOR_28, (u32) sector-1,
453 AP_COUNT, (u16) 1,
454 AP_VADDR, (void _far *) pDLA, 512,
455 AP_DEVICE, 0x40,
456 AP_END);
457 if (rc) return 0;
458
459 ddphex(pDLA, sizeof(DLA_Table_Sector), "DLA sector %d:\n", sector-1);
460
461 if ((pDLA->DLA_Signature1 == DLA_TABLE_SIGNATURE1) && (pDLA->DLA_Signature2 == DLA_TABLE_SIGNATURE2)) {
462 ddprintf("is_lvm_geometry found at sector %d\n", sector-1);
463 geometry->TotalCylinders = pDLA->Cylinders;
464 geometry->NumHeads = pDLA->Heads_Per_Cylinder;
465 geometry->SectorsPerTrack = pDLA->Sectors_Per_Track;
466 geometry->TotalSectors = pDLA->Cylinders * pDLA->Heads_Per_Cylinder * pDLA->Sectors_Per_Track;
467 return 1;
468 }
469
470 return 0;
471}
472
473/******************************************************************************
474 * Try to read LVM information from the disk. If found, use the LVM geometry.
475 * This function will only work at init time. A better strategy would be to
476 * calculate the geometry during ahci_scan_ports and save it away and then just
477 * return the saved values when ata_get_geometry() is called.
478 */
479int is_lvm_geometry(IORBH _far *iorb)
480{
481 GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
482 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
483 int p = iorb_unit_port(iorb);
484 int d = iorb_unit_device(iorb);
485 ULONG sector;
486
487 if (init_complete) return 0; /* We cannot use ahci_exec_polled_cmd() after init_complete */
488
489 if (use_lvm_info) {
490 ddprintf("is_lvm_geometry (%d.%d.%d)\n", ad_no(ai), p, d);
491
492 /* First check the sector reported by the hardware */
493 if (check_lvm(iorb, geometry->SectorsPerTrack)) return 1;
494
495 for (sector = 255; sector >= 63; sector >>= 1) {
496 if (sector == geometry->SectorsPerTrack) continue;
497 if (check_lvm(iorb, sector)) return 1;
498 }
499 }
500
501 return 0;
502}
503
504/******************************************************************************
505 * Post processing function for ata_get_geometry(): convert the ATA identify
506 * information to OS/2 IOCC_GEOMETRY information.
507 */
508void ata_get_geometry_pp(IORBH _far *iorb)
509{
510 GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
511 USHORT geometry_len = ((IORB_GEOMETRY _far *) iorb)->GeometryLen;
512 u16 *id_buf = add_workspace(iorb)->buf;
513 int a = iorb_unit_adapter(iorb);
514 int p = iorb_unit_port(iorb);
515 char *Method;
516
517 /* Fill-in geometry information; the ATA-8 spec declares the geometry
518 * fields in the ATA ID buffer as obsolete but it's still the best
519 * guess in most cases. If the information stored in the geometry
520 * fields is apparently incorrect, we'll use the algorithm typically
521 * used by SCSI adapters and modern PC BIOS versions:
522 *
523 * - 512 bytes per sector
524 * - 255 heads
525 * - 63 sectors per track (or 56 with the parameter "/4")
526 * - x cylinders (calculated)
527 *
528 * Please note that os2ahci currently does not natively support ATA sectors
529 * larger than 512 bytes, therefore relies on the translation logic built
530 * into the corresponding ATA disks. In order to prevent file systems that
531 * use block sizes larger than 512 bytes (FAT, JFS, ...) from ending up on
532 * incorrectly aligned physical sector accesses, hence using more physical
533 * I/Os than necessary, the command line parameter "/4" can be used to force
534 * a track size of 56 sectors. This way, partitions will start on 4K
535 * boundaries.
536 *
537 * Another limitation is that OS/2 has a 32-bit variable for the total number
538 * of sectors, limiting the maximum capacity to roughly 2TB. This is another
539 * issue that needs to be addressed sooner or later; large sectors could
540 * raise this limit to something like 8TB but this is not really much of a
541 * difference. Maybe there's something in later DDKs that allows more than
542 * 32 bits?
543 */
544 memset(geometry, 0x00, geometry_len);
545 geometry->BytesPerSector = ATA_SECTOR_SIZE;
546
547 /* extract total number of sectors */
548 if (id_buf[ATA_ID_CFS_ENABLE_2] & 0x400) {
549 /* 48-bit LBA supported */
550 if (ATA_CAPACITY48_H(id_buf) != 0) {
551 /* more than 32 bits for number of sectors */
552 dprintf("warning: limiting disk %d.%d.%d to 2TB\n",
553 iorb_unit_adapter(iorb), iorb_unit_port(iorb),
554 iorb_unit_device(iorb));
555 geometry->TotalSectors = 0xffffffffUL;
556 } else {
557 geometry->TotalSectors = ATA_CAPACITY48_L(id_buf);
558 }
559 } else {
560 /* 28-bit LBA */
561 geometry->TotalSectors = ATA_CAPACITY(id_buf) & 0x0fffffffUL;
562 }
563
564 Method = "None";
565 /* fabricate the remaining geometry fields */
566 if (track_size[a][p] != 0) {
567 /* A specific track size has been requested for this port; this is
568 * typically done for disks with 4K sectors to make sure partitions
569 * start on 8-sector boundaries (parameter "/4").
570 */
571 geometry->NumHeads = 255;
572 geometry->SectorsPerTrack = track_size[a][p];
573 geometry->TotalCylinders = geometry->TotalSectors /
574 ((u32) geometry->NumHeads *
575 (u32) geometry->SectorsPerTrack);
576 Method = "Custom";
577 } else if (CUR_HEADS(id_buf) > 0 && CUR_CYLS(id_buf) > 0 &&
578 CUR_SECTORS(id_buf) > 0 &&
579 CUR_CAPACITY(id_buf) == (u32) CUR_HEADS(id_buf) *
580 (u32) CUR_CYLS(id_buf) *
581 (u32) CUR_SECTORS(id_buf)) {
582 /* BIOS-supplied (aka "current") geometry values look valid */
583 geometry->NumHeads = CUR_HEADS(id_buf);
584 geometry->SectorsPerTrack = CUR_SECTORS(id_buf);
585 geometry->TotalCylinders = CUR_CYLS(id_buf);
586 Method = "BIOS";
587 } else if (ATA_HEADS(id_buf) > 0 && ATA_CYLS(id_buf) > 0 &&
588 ATA_SECTORS(id_buf) > 0) {
589 /* ATA-supplied values for geometry look valid */
590 geometry->NumHeads = ATA_HEADS(id_buf);
591 geometry->SectorsPerTrack = ATA_SECTORS(id_buf);
592 geometry->TotalCylinders = ATA_CYLS(id_buf);
593 Method = "ATA";
594 } else {
595 /* use typical SCSI geometry */
596 geometry->NumHeads = 255;
597 geometry->SectorsPerTrack = 63;
598 geometry->TotalCylinders = geometry->TotalSectors /
599 ((u32) geometry->NumHeads *
600 (u32) geometry->SectorsPerTrack);
601 Method = "SCSI";
602 }
603
604 if (is_lvm_geometry(iorb)) Method = "LVM";
605
606 dprintf("Drive geometry: %ld cylinders, %d heads, %d sectors per track (%ldMB) (%s)\n",
607 (u32) geometry->TotalCylinders, (u16) geometry->NumHeads, (u16) geometry->SectorsPerTrack,
608 (u32) (geometry->TotalSectors / 2048), Method);
609
610 /* tell interrupt handler that this IORB is complete */
611 add_workspace(iorb)->complete = 1;
612}
613
614/******************************************************************************
615 * Test whether unit is ready.
616 */
617int ata_unit_ready(IORBH _far *iorb, int slot)
618{
619 /* This is a NOP for ATA devices (at least right now); returning an error
620 * without setting an error code means ahci_exec_iorb() will not queue any
621 * HW command and the IORB will complete successfully.
622 */
623 ((IORB_UNIT_STATUS _far *) iorb)->UnitStatus = US_READY | US_POWER;
624 return(-1);
625}
626
627/******************************************************************************
628 * Read sectors from AHCI device.
629 */
630int ata_read(IORBH _far *iorb, int slot)
631{
632 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
633 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
634 ULONG sector = io->RBA + io->BlocksXferred;
635 USHORT count = io->BlockCount - io->BlocksXferred;
636 USHORT sg_indx;
637 USHORT sg_cnt;
638 int p = iorb_unit_port(iorb);
639 int d = iorb_unit_device(iorb);
640 int rc;
641
642 if (io->BlockCount == 0) {
643 /* NOP; return -1 without error in IORB to indicate success */
644 return(-1);
645 }
646
647 if (add_workspace(iorb)->unaligned) {
648 /* unaligned S/G addresses present; need to use double buffers */
649 return(ata_read_unaligned(iorb, slot));
650 }
651
652 /* Kludge: some I/O commands during boot use excessive S/G buffer lengths
653 * which cause NCQ commands to lock up. If there's only one S/G element
654 * and this element is already larger than what we can derive from the sector
655 * count, we'll adjust that element.
656 */
657 if (io->BlocksXferred == 0 && io->cSGList == 1 &&
658 io->pSGList[0].XferBufLen > (ULONG) io->BlockCount * io->BlockSize) {
659 io->pSGList[0].XferBufLen = (ULONG) io->BlockCount * io->BlockSize;
660 }
661
662 /* prepare read command while keeping an eye on S/G count limitations */
663 do {
664 sg_indx = ata_get_sg_indx(io);
665 sg_cnt = io->cSGList - sg_indx;
666 if ((rc = ata_cmd_read(iorb, ai, p, d, slot, sector, count,
667 io->pSGList + sg_indx, sg_cnt)) > 0) {
668 /* couldn't map all S/G elements */
669 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
670 }
671 } while (rc > 0 && sg_cnt > 0);
672
673 if (rc == 0) {
674 add_workspace(iorb)->blocks = count;
675 add_workspace(iorb)->ppfunc = ata_read_pp;
676
677 } else if (rc > 0) {
678 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
679
680 } else if (rc == ATA_CMD_UNALIGNED_ADDR) {
681 /* unaligned S/G addresses detected; need to use double buffers */
682 add_workspace(iorb)->unaligned = 1;
683 return(ata_read_unaligned(iorb, slot));
684
685 } else {
686 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
687 }
688
689 return(rc);
690}
691
692/******************************************************************************
693 * Read sectors from AHCI device with unaligned S/G element addresses. AHCI
694 * only allows aligned S/G addresses while OS/2 doesn't have these kind of
695 * restrictions. This doesn't happen very often but when it does, we need to
696 * use a transfer buffer and copy the data manually.
697 */
698int ata_read_unaligned(IORBH _far *iorb, int slot)
699{
700 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
701 ADD_WORKSPACE _far *aws = add_workspace(iorb);
702 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
703 ULONG sector = io->RBA + io->BlocksXferred;
704 SCATGATENTRY sg_single;
705 int p = iorb_unit_port(iorb);
706 int d = iorb_unit_device(iorb);
707 int rc;
708
709 ddprintf("ata_read_unaligned(%d.%d.%d, %ld)\n", ad_no(ai), p, d, sector);
710
711 /* allocate transfer buffer */
712 if ((aws->buf = malloc(io->BlockSize)) == NULL) {
713 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
714 return(-1);
715 }
716
717 /* prepare read command using transfer buffer */
718 sg_single.ppXferBuf = virt_to_phys(aws->buf);
719 sg_single.XferBufLen = io->BlockSize;
720 rc = ata_cmd_read(iorb, ai, p, d, slot, sector, 1, &sg_single, 1);
721
722 if (rc == 0) {
723 add_workspace(iorb)->blocks = 1;
724 add_workspace(iorb)->ppfunc = ata_read_pp;
725
726 } else if (rc > 0) {
727 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
728
729 } else {
730 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
731 }
732
733 return(rc);
734}
735
736/******************************************************************************
737 * Post processing function for ata_read(); this function updates the
738 * BlocksXferred counter in the IORB and, if not all blocks have been
739 * transferred, requeues the IORB to process the remaining sectors. It also
740 * takes care of copying data from the transfer buffer for unaligned reads.
741 */
742void ata_read_pp(IORBH _far *iorb)
743{
744 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
745 ADD_WORKSPACE _far *aws = add_workspace(iorb);
746
747 if (aws->unaligned) {
748 /* copy transfer buffer to corresponding physical address in S/G list */
749 sg_memcpy(io->pSGList, io->cSGList,
750 (ULONG) io->BlocksXferred * (ULONG) io->BlockSize,
751 aws->buf, io->BlockSize, BUF_TO_SG);
752 }
753
754 io->BlocksXferred += add_workspace(iorb)->blocks;
755 ddprintf("ata_read_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
756
757 if (io->BlocksXferred >= io->BlockCount) {
758 /* we're done; tell IRQ handler the IORB is complete */
759 add_workspace(iorb)->complete = 1;
760 } else {
761 /* requeue this IORB for next iteration */
762 iorb_requeue(iorb);
763 }
764}
765
766/******************************************************************************
767 * Verify readability of sectors on ATA device.
768 */
769int ata_verify(IORBH _far *iorb, int slot)
770{
771 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
772 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
773 int p = iorb_unit_port(iorb);
774 int d = iorb_unit_device(iorb);
775 int rc;
776
777 if (io->BlockCount == 0) {
778 /* NOP; return -1 without error in IORB to indicate success */
779 return(-1);
780 }
781
782 /* prepare verify command */
783 if (io->RBA >= (1UL << 28) || io->BlockCount > 256) {
784 /* need LBA48 for this command */
785 if (!ai->ports[p].devs[d].lba48) {
786 iorb_seterr(iorb, IOERR_RBA_LIMIT);
787 return(-1);
788 }
789 rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY_EXT,
790 AP_SECTOR_48, (u32) io->RBA, (u16) 0,
791 AP_COUNT, (u16) io->BlockCount,
792 AP_DEVICE, 0x40,
793 AP_END);
794 } else {
795 rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY,
796 AP_SECTOR_28, (u32) io->RBA,
797 AP_COUNT, (u16) io->BlockCount & 0xffU,
798 AP_DEVICE, 0x40,
799 AP_END);
800 }
801
802 return(rc);
803}
804
805/******************************************************************************
806 * Write sectors to AHCI device.
807 */
808int ata_write(IORBH _far *iorb, int slot)
809{
810 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
811 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
812 ULONG sector = io->RBA + io->BlocksXferred;
813 USHORT count = io->BlockCount - io->BlocksXferred;
814 USHORT sg_indx;
815 USHORT sg_cnt;
816 int p = iorb_unit_port(iorb);
817 int d = iorb_unit_device(iorb);
818 int rc;
819
820 if (io->BlockCount == 0) {
821 /* NOP; return -1 without error in IORB to indicate success */
822 return(-1);
823 }
824
825 if (add_workspace(iorb)->unaligned) {
826 /* unaligned S/G addresses present; need to use double buffers */
827 return(ata_write_unaligned(iorb, slot));
828 }
829
830 /* prepare write command while keeping an eye on S/G count limitations */
831 do {
832 sg_indx = ata_get_sg_indx(io);
833 sg_cnt = io->cSGList - sg_indx;
834 if ((rc = ata_cmd_write(iorb, ai, p, d, slot, sector, count,
835 io->pSGList + sg_indx, sg_cnt,
836 io->Flags & XIO_DISABLE_HW_WRITE_CACHE)) > 0) {
837 /* couldn't map all S/G elements */
838 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
839 }
840 } while (rc > 0 && sg_cnt > 0);
841
842 if (rc == 0) {
843 add_workspace(iorb)->blocks = count;
844 add_workspace(iorb)->ppfunc = ata_write_pp;
845
846 } else if (rc > 0) {
847 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
848
849 } else if (rc == ATA_CMD_UNALIGNED_ADDR) {
850 /* unaligned S/G addresses detected; need to use double buffers */
851 add_workspace(iorb)->unaligned = 1;
852 return(ata_write_unaligned(iorb, slot));
853
854 } else {
855 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
856 }
857
858 return(rc);
859}
860
861/******************************************************************************
862 * Write sectors from AHCI device with unaligned S/G element addresses. AHCI
863 * only allows aligned S/G addresses while OS/2 doesn't have these kind of
864 * restrictions. This doesn't happen very often but when it does, we need to
865 * use a transfer buffer and copy the data manually.
866 */
867int ata_write_unaligned(IORBH _far *iorb, int slot)
868{
869 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
870 ADD_WORKSPACE _far *aws = add_workspace(iorb);
871 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
872 ULONG sector = io->RBA + io->BlocksXferred;
873 SCATGATENTRY sg_single;
874 int p = iorb_unit_port(iorb);
875 int d = iorb_unit_device(iorb);
876 int rc;
877
878 ddprintf("ata_write_unaligned(%d.%d.%d, %ld)\n", ad_no(ai), p, d, sector);
879
880 /* allocate transfer buffer */
881 if ((aws->buf = malloc(io->BlockSize)) == NULL) {
882 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
883 return(-1);
884 }
885
886 /* copy next sector from S/G list to transfer buffer */
887 sg_memcpy(io->pSGList, io->cSGList,
888 (ULONG) io->BlocksXferred * (ULONG) io->BlockSize,
889 aws->buf, io->BlockSize, SG_TO_BUF);
890
891 /* prepare write command using transfer buffer */
892 sg_single.ppXferBuf = virt_to_phys(aws->buf);
893 sg_single.XferBufLen = io->BlockSize;
894 rc = ata_cmd_write(iorb, ai, p, d, slot, sector, 1, &sg_single, 1,
895 io->Flags & XIO_DISABLE_HW_WRITE_CACHE);
896
897 if (rc == 0) {
898 add_workspace(iorb)->blocks = 1;
899 add_workspace(iorb)->ppfunc = ata_write_pp;
900
901 } else if (rc > 0) {
902 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
903
904 } else {
905 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
906 }
907
908 return(rc);
909}
910
911
912/******************************************************************************
913 * Post processing function for ata_write(); this function updates the
914 * BlocksXferred counter in the IORB and, if not all blocks have been
915 * transferred, requeues the IORB to process the remaining sectors.
916 */
917void ata_write_pp(IORBH _far *iorb)
918{
919 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
920
921 io->BlocksXferred += add_workspace(iorb)->blocks;
922 ddprintf("ata_write_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
923
924 if (io->BlocksXferred >= io->BlockCount) {
925 /* we're done; tell IRQ handler the IORB is complete */
926 add_workspace(iorb)->complete = 1;
927 } else {
928 /* requeue this IORB for next iteration */
929 iorb_requeue(iorb);
930 }
931}
932
933/******************************************************************************
934 * Execute ATA command.
935 */
936int ata_execute_ata(IORBH _far *iorb, int slot)
937{
938 IORB_ADAPTER_PASSTHRU _far *apt = (IORB_ADAPTER_PASSTHRU _far *) iorb;
939 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
940 int p = iorb_unit_port(iorb);
941 int d = iorb_unit_device(iorb);
942 int rc;
943
944 if (apt->ControllerCmdLen != sizeof(ATA_CMD)) {
945 iorb_seterr(iorb, IOERR_CMD_SYNTAX);
946 return(-1);
947 }
948
949 rc = ata_cmd(ai, p, d, slot, 0,
950 AP_SGLIST, apt->pSGList, apt->cSGList,
951 AP_ATA_CMD, apt->pControllerCmd,
952 AP_WRITE, !(apt->Flags & PT_DIRECTION_IN),
953 AP_END);
954
955 if (rc == 0) {
956 add_workspace(iorb)->ppfunc = ata_execute_ata_pp;
957 }
958
959 return(rc);
960}
961
962/******************************************************************************
963 * Post processing function for ata_execute_ata(); the main purpose of this
964 * function is to copy the received D2H FIS (i.e. the device registers after
965 * command completion) back to the ATA command structure.
966 *
967 * See ata_cmd_to_fis() for an explanation of the mapping.
968 */
969void ata_execute_ata_pp(IORBH _far *iorb)
970{
971 AHCI_PORT_DMA _far *dma_base;
972 ATA_CMD _far *cmd;
973 AD_INFO *ai;
974 u8 _far *fis;
975 int p;
976
977 /* get address of D2H FIS */
978 ai = ad_infos + iorb_unit_adapter(iorb);
979 p = iorb_unit_port(iorb);
980 dma_base = port_dma_base(ai, p);
981 fis = dma_base->rx_fis + 0x40;
982
983 if (fis[0] != 0x34) {
984 /* this is not a D2H FIS - give up silently */
985 ddprintf("ata_execute_ata_pp(): D2H FIS type incorrect: %d\n", fis[0]);
986 add_workspace(iorb)->complete = 1;
987 return;
988 }
989
990 /* map D2H FIS to the original ATA controller command structure */
991 cmd = (ATA_CMD _far *) ((IORB_ADAPTER_PASSTHRU _far *) iorb)->pControllerCmd;
992
993 cmd->cmd = fis[2];
994 cmd->device = fis[7];
995 cmd->features = ((u16) fis[3])
996 | ((u16) fis[11]);
997 cmd->lba_l = ((u32) fis[4])
998 | ((u32) fis[5] << 8)
999 | ((u32) fis[6] << 16)
1000 | ((u32) fis[8] << 24);
1001 cmd->lba_h = ((u16) fis[9])
1002 | ((u16) fis[10] << 8);
1003 cmd->count = ((u16) fis[12])
1004 | ((u16) fis[13] << 8);
1005
1006 dphex(cmd, sizeof(*cmd), "ahci_execute_ata_pp(): cmd after completion:\n");
1007
1008 /* signal completion to interrupt handler */
1009 add_workspace(iorb)->complete = 1;
1010}
1011
1012/******************************************************************************
1013 * Request sense information for a failed command. Since there is no "request
1014 * sense" command for ATA devices, we need to read the current error code from
1015 * the AHCI task file register and fabricate the sense information.
1016 *
1017 * NOTES:
1018 *
1019 * - This function must be called right after an ATA command has failed and
1020 * before any other commands are queued on the corresponding port. This
1021 * function is typically called in the port restart context hook which is
1022 * triggered by an AHCI error interrupt.
1023 *
1024 * - The ATA error bits are a complete mess. We'll try and catch the most
1025 * interesting error codes (such as medium errors) and report everything
1026 * else with a generic error code.
1027 */
1028int ata_req_sense(IORBH _far *iorb, int slot)
1029{
1030 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
1031 u8 _far *port_mmio = port_base(ai, iorb_unit_port(iorb));
1032 u32 tf_data = readl(port_mmio + PORT_TFDATA);
1033 u8 err = (u8) (tf_data >> 8);
1034 u8 sts = (u8) (tf_data);
1035
1036 if (sts & ATA_ERR) {
1037 if (sts & ATA_DF) {
1038 /* there is a device-specific error condition */
1039 if (err & ATA_ICRC) {
1040 iorb_seterr(iorb, IOERR_ADAPTER_DEVICEBUSCHECK);
1041 } else if (err & ATA_UNC) {
1042 iorb_seterr(iorb, IOERR_MEDIA);
1043 } else if (err & ATA_IDNF) {
1044 iorb_seterr(iorb, IOERR_RBA_ADDRESSING_ERROR);
1045 } else {
1046 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
1047 }
1048
1049 } else {
1050 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
1051 }
1052 } else {
1053 /* this function only gets called when we received an error interrupt */
1054 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
1055 }
1056
1057 /* Return an error to indicate there's no HW command to be submitted and
1058 * that the IORB can be completed "as is" (the upstream code expects the
1059 * IORB error code, if any, to be set when this happens and this is exactly
1060 * what this function is all about).
1061 */
1062 return(-1);
1063}
1064
1065/******************************************************************************
1066 * Extract vendor and device name from an ATA INDENTIFY buffer. Since strings
1067 * in the indentify buffer are byte-swapped, we need to swap them back.
1068 */
1069char *ata_dev_name(u16 *id_buf)
1070{
1071 static char dev_name[ATA_ID_PROD_LEN + 1];
1072 char *t = dev_name;
1073 char *s = (char *) (id_buf + ATA_ID_PROD);
1074 int i;
1075
1076 dev_name[sizeof(dev_name)-1] = '\0';
1077
1078 for (i = 0; i < ATA_ID_PROD_LEN / 2; i++) {
1079 *(t++) = s[1];
1080 *(t++) = s[0];
1081 s += 2;
1082 }
1083
1084 return(dev_name);
1085}
1086
1087/******************************************************************************
1088 * Fabricate ATA READ command based on the capabilities of the corresponding
1089 * device and the paramters set from above (NCQ, etc).
1090 */
1091static int ata_cmd_read(IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
1092 ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
1093 ULONG sg_cnt)
1094{
1095 int rc;
1096
1097 if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
1098 /* need LBA48 for this command */
1099 if (!ai->ports[p].devs[d].lba48) {
1100 iorb_seterr(iorb, IOERR_RBA_LIMIT);
1101 return(-1);
1102 }
1103 if (add_workspace(iorb)->is_ncq) {
1104 /* use NCQ read; count goes into feature register, tag into count! */
1105 rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_READ,
1106 AP_SECTOR_48, (u32) sector, (u16) 0,
1107 AP_FEATURES, (u16) count,
1108 AP_COUNT, (u16) (slot << 3), /* tag == slot */
1109 AP_SGLIST, sg_list, (u16) sg_cnt,
1110 AP_DEVICE, 0x40,
1111 AP_END);
1112 } else {
1113 rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ_EXT,
1114 AP_SECTOR_48, (u32) sector, (u16) 0,
1115 AP_COUNT, (u16) count,
1116 AP_SGLIST, sg_list, (u16) sg_cnt,
1117 AP_DEVICE, 0x40,
1118 AP_END);
1119 }
1120
1121 } else {
1122 rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ,
1123 AP_SECTOR_28, (u32) sector,
1124 AP_COUNT, (u16) count & 0xffU,
1125 AP_SGLIST, sg_list, (u16) sg_cnt,
1126 AP_DEVICE, 0x40,
1127 AP_END);
1128 }
1129
1130 return(rc);
1131}
1132
1133/******************************************************************************
1134 * Fabricate ATA WRITE command based on the capabilities of the corresponding
1135 * device and the paramters set from above (NCQ, etc)
1136 */
1137static int ata_cmd_write(IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
1138 ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
1139 ULONG sg_cnt, int write_through)
1140{
1141 int rc;
1142
1143 if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
1144 /* need LBA48 for this command */
1145 if (!ai->ports[p].devs[d].lba48) {
1146 iorb_seterr(iorb, IOERR_RBA_LIMIT);
1147 return(-1);
1148 }
1149 if (add_workspace(iorb)->is_ncq) {
1150 /* use NCQ write; count goes into feature register, tag into count! */
1151 rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_WRITE,
1152 AP_SECTOR_48, (u32) sector, (u16) 0,
1153 AP_FEATURES, (u16) count,
1154 /* tag = slot */
1155 AP_COUNT, (u16) (slot << 3),
1156 AP_SGLIST, sg_list, (u16) sg_cnt,
1157 AP_DEVICE, 0x40,
1158 /* force unit access */
1159 AP_DEVICE, (write_through && !force_write_cache) ? 0x80 : 0,
1160 AP_WRITE, 1,
1161 AP_END);
1162 } else {
1163 rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE_EXT,
1164 AP_SECTOR_48, (u32) sector, (u16) 0,
1165 AP_COUNT, (u16) count,
1166 AP_SGLIST, sg_list, (u16) sg_cnt,
1167 AP_DEVICE, 0x40,
1168 AP_WRITE, 1,
1169 AP_END);
1170 }
1171
1172 } else {
1173 rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE,
1174 AP_SECTOR_28, (u32) sector,
1175 AP_COUNT, (u16) count & 0xffU,
1176 AP_SGLIST, sg_list, (u16) sg_cnt,
1177 AP_DEVICE, 0x40,
1178 AP_WRITE, 1,
1179 AP_END);
1180 }
1181
1182 return(rc);
1183}
Note: See TracBrowser for help on using the repository browser.