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

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

code cleanup - debug messages
fixed defect in smart ioctl

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