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

Last change on this file since 176 was 176, checked in by David Azarewicz, 9 years ago

Documentation and makefile changes for Arca Noae packaging.
No code changes.

File size: 45.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 * Portions copyright (c) 2013-2015 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
444/* Adjust the cylinder count in the physical
445 * geometry to the last full cylinder.
446 */
447int adjust_cylinders(GEOMETRY _far *geometry, ULONG TotalSectors) {
448 USHORT SecPerCyl;
449 int rc = FALSE;
450
451 geometry->TotalSectors = TotalSectors;
452 SecPerCyl = geometry->SectorsPerTrack * geometry->NumHeads;
453 if (SecPerCyl > 0) {
454 ULONG TotalCylinders = TotalSectors / SecPerCyl;
455
456 geometry->TotalSectors = TotalCylinders * SecPerCyl;
457 geometry->TotalCylinders = TotalCylinders;
458 if (TotalCylinders >> 16) {
459 geometry->TotalCylinders = 65535;
460 rc = TRUE;
461 }
462 }
463 return (rc);
464}
465
466/* Calculate the logical geometry based on the input physcial geometry
467 * using the LBA Assist Translation algorithm.
468 */
469#define BIOS_MAX_CYLINDERS 1024l
470#define BIOS_MAX_NUMHEADS 255
471#define BIOS_MAX_SECTORSPERTRACK 63
472void log_geom_calculate_LBA_assist(GEOMETRY _far *geometry, ULONG TotalSectors)
473{
474 UCHAR numSpT = BIOS_MAX_SECTORSPERTRACK;
475 UCHAR numHeads = BIOS_MAX_NUMHEADS;
476 ULONG Cylinders;
477
478 if (TotalSectors <= (BIOS_MAX_CYLINDERS * 128 * BIOS_MAX_SECTORSPERTRACK)) {
479 USHORT temp = (TotalSectors - 1) / (BIOS_MAX_CYLINDERS * BIOS_MAX_SECTORSPERTRACK);
480
481 if (temp < 16) numHeads = 16;
482 else if (temp < 32) numHeads = 32;
483 else if (temp < 64) numHeads = 64;
484 else numHeads = 128;
485 }
486
487 do {
488 Cylinders = TotalSectors / (USHORT)(numHeads * numSpT);
489 if (Cylinders >> 16) {
490 if (numSpT < 128)
491 numSpT = (numSpT << 1) | 1;
492 else
493 Cylinders = 65535; // overflow !
494 }
495 } while (Cylinders >> 16);
496
497 geometry->TotalCylinders = Cylinders;
498 geometry->NumHeads = numHeads;
499 geometry->SectorsPerTrack = numSpT;
500}
501
502int check_lvm(IORBH _far *iorb, ULONG sector)
503{
504 DLA_Table_Sector *pDLA = (DLA_Table_Sector*)add_workspace(iorb)->buf;
505 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
506 GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
507 int p = iorb_unit_port(iorb);
508 int rc;
509
510 rc = ahci_exec_polled_cmd(ai, p, 0, 500, ATA_CMD_READ,
511 AP_SECTOR_28, (u32) sector-1,
512 AP_COUNT, (u16) 1,
513 AP_VADDR, (void _far *) pDLA, 512,
514 AP_DEVICE, 0x40,
515 AP_END);
516 if (rc) return 0;
517
518 ddphex(pDLA, sizeof(DLA_Table_Sector), "DLA sector %d:\n", sector-1);
519
520 if ((pDLA->DLA_Signature1 == DLA_TABLE_SIGNATURE1) && (pDLA->DLA_Signature2 == DLA_TABLE_SIGNATURE2)) {
521 ddprintf("is_lvm_geometry found at sector %d\n", sector-1);
522 geometry->TotalCylinders = pDLA->Cylinders;
523 geometry->NumHeads = pDLA->Heads_Per_Cylinder;
524 geometry->SectorsPerTrack = pDLA->Sectors_Per_Track;
525 geometry->TotalSectors = pDLA->Cylinders * pDLA->Heads_Per_Cylinder * pDLA->Sectors_Per_Track;
526 return 1;
527 }
528
529 return 0;
530}
531
532/******************************************************************************
533 * Try to read LVM information from the disk. If found, use the LVM geometry.
534 * This function will only work at init time. A better strategy would be to
535 * calculate the geometry during ahci_scan_ports and save it away and then just
536 * return the saved values when ata_get_geometry() is called.
537 */
538int is_lvm_geometry(IORBH _far *iorb)
539{
540 GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
541 ULONG sector;
542
543 if (init_complete) return 0; /* We cannot use ahci_exec_polled_cmd() after init_complete */
544
545 if (use_lvm_info) {
546 #ifdef DEBUG
547 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
548 int p = iorb_unit_port(iorb);
549 int d = iorb_unit_device(iorb);
550 ddprintf("is_lvm_geometry (%d.%d.%d)\n", ad_no(ai), p, d);
551 #endif
552
553 /* First check the sector reported by the hardware */
554 if (check_lvm(iorb, geometry->SectorsPerTrack)) return 1;
555
556 for (sector = 255; sector >= 63; sector >>= 1) {
557 if (sector == geometry->SectorsPerTrack) continue;
558 if (check_lvm(iorb, sector)) return 1;
559 }
560 }
561
562 return 0;
563}
564
565/******************************************************************************
566 * Post processing function for ata_get_geometry(): convert the ATA identify
567 * information to OS/2 IOCC_GEOMETRY information.
568 */
569void ata_get_geometry_pp(IORBH _far *iorb)
570{
571 GEOMETRY _far *geometry = ((IORB_GEOMETRY _far *) iorb)->pGeometry;
572 USHORT geometry_len = ((IORB_GEOMETRY _far *) iorb)->GeometryLen;
573 u16 *id_buf = add_workspace(iorb)->buf;
574 int a = iorb_unit_adapter(iorb);
575 int p = iorb_unit_port(iorb);
576 char *Method;
577
578 /* Fill-in geometry information; the ATA-8 spec declares the geometry
579 * fields in the ATA ID buffer as obsolete but it's still the best
580 * guess in most cases. If the information stored in the geometry
581 * fields is apparently incorrect, we'll use the algorithm typically
582 * used by SCSI adapters and modern PC BIOS versions:
583 *
584 * - 512 bytes per sector
585 * - 255 heads
586 * - 63 sectors per track (or 56 with the parameter "/4")
587 * - x cylinders (calculated)
588 *
589 * Please note that os2ahci currently does not natively support ATA sectors
590 * larger than 512 bytes, therefore relies on the translation logic built
591 * into the corresponding ATA disks. In order to prevent file systems that
592 * use block sizes larger than 512 bytes (FAT, JFS, ...) from ending up on
593 * incorrectly aligned physical sector accesses, hence using more physical
594 * I/Os than necessary, the command line parameter "/4" can be used to force
595 * a track size of 56 sectors. This way, partitions will start on 4K
596 * boundaries.
597 *
598 * Another limitation is that OS/2 has a 32-bit variable for the total number
599 * of sectors, limiting the maximum capacity to roughly 2TB. This is another
600 * issue that needs to be addressed sooner or later; large sectors could
601 * raise this limit to something like 8TB but this is not really much of a
602 * difference. Maybe there's something in later DDKs that allows more than
603 * 32 bits?
604 */
605 memset(geometry, 0x00, geometry_len);
606 geometry->BytesPerSector = ATA_SECTOR_SIZE;
607
608 /* extract total number of sectors */
609 if (id_buf[ATA_ID_CFS_ENABLE_2] & 0x400) {
610 /* 48-bit LBA supported */
611 if (ATA_CAPACITY48_H(id_buf) != 0) {
612 /* more than 32 bits for number of sectors */
613 dprintf("warning: limiting disk %d.%d.%d to 2TB\n",
614 iorb_unit_adapter(iorb), iorb_unit_port(iorb),
615 iorb_unit_device(iorb));
616 geometry->TotalSectors = 0xffffffffUL;
617 } else {
618 geometry->TotalSectors = ATA_CAPACITY48_L(id_buf);
619 }
620 } else {
621 /* 28-bit LBA */
622 geometry->TotalSectors = ATA_CAPACITY(id_buf) & 0x0fffffffUL;
623 }
624
625 Method = "None";
626 /* fabricate the remaining geometry fields */
627 if (track_size[a][p] != 0) {
628 /* A specific track size has been requested for this port; this is
629 * typically done for disks with 4K sectors to make sure partitions
630 * start on 8-sector boundaries (parameter "/4").
631 */
632 geometry->NumHeads = 255;
633 geometry->SectorsPerTrack = track_size[a][p];
634 geometry->TotalCylinders = geometry->TotalSectors / ((u32) geometry->NumHeads * (u32) geometry->SectorsPerTrack);
635 Method = "Custom";
636 } else if (CUR_HEADS(id_buf) > 0 && CUR_CYLS(id_buf) > 0 && CUR_SECTORS(id_buf) > 0 &&
637 CUR_CAPACITY(id_buf) == (u32) CUR_HEADS(id_buf) * (u32) CUR_CYLS(id_buf) * (u32) CUR_SECTORS(id_buf)) {
638 /* BIOS-supplied (aka "current") geometry values look valid */
639 geometry->NumHeads = CUR_HEADS(id_buf);
640 geometry->SectorsPerTrack = CUR_SECTORS(id_buf);
641 geometry->TotalCylinders = CUR_CYLS(id_buf);
642 Method = "BIOS";
643 } else if (ATA_HEADS(id_buf) > 0 && ATA_CYLS(id_buf) > 0 && ATA_SECTORS(id_buf) > 0) {
644 /* ATA-supplied values for geometry look valid */
645 geometry->NumHeads = ATA_HEADS(id_buf);
646 geometry->SectorsPerTrack = ATA_SECTORS(id_buf);
647 geometry->TotalCylinders = ATA_CYLS(id_buf);
648 Method = "ATA";
649 } else {
650 /* use typical SCSI geometry */
651 geometry->NumHeads = 255;
652 geometry->SectorsPerTrack = 63;
653 geometry->TotalCylinders = geometry->TotalSectors / ((u32) geometry->NumHeads * (u32) geometry->SectorsPerTrack);
654 Method = "SCSI";
655 }
656
657 dprintf("Physical geometry: %ld cylinders, %d heads, %d sectors per track (%ldMB) (%s)\n",
658 (u32) geometry->TotalCylinders, (u16) geometry->NumHeads, (u16) geometry->SectorsPerTrack,
659 (u32) (geometry->TotalSectors / 2048), Method);
660
661 /* Fixup the geometry in case the geometry reported by the BIOS is bad */
662 if (adjust_cylinders(geometry, geometry->TotalSectors)) { // cylinder overflow
663 log_geom_calculate_LBA_assist(geometry, geometry->TotalSectors);
664 geometry->TotalSectors = (USHORT)(geometry->NumHeads * geometry->SectorsPerTrack) * (ULONG)geometry->TotalCylinders;
665 }
666 adjust_cylinders(geometry, geometry->TotalSectors);
667
668 dprintf("Logical geometry: %ld cylinders, %d heads, %d sectors per track (%ldMB) (%s)\n",
669 (u32) geometry->TotalCylinders, (u16) geometry->NumHeads, (u16) geometry->SectorsPerTrack,
670 (u32) (geometry->TotalSectors / 2048), Method);
671
672 if (is_lvm_geometry(iorb)) Method = "LVM";
673 ad_infos[a].ports[p].devs[0].dev_info.Cylinders = geometry->TotalCylinders;
674 ad_infos[a].ports[p].devs[0].dev_info.HeadsPerCylinder = geometry->NumHeads;
675 ad_infos[a].ports[p].devs[0].dev_info.SectorsPerTrack = geometry->SectorsPerTrack;
676 ad_infos[a].ports[p].devs[0].dev_info.TotalSectors = geometry->TotalSectors;
677 ad_infos[a].ports[p].devs[0].dev_info.Method = Method;
678
679 dprintf("Reported geometry: %ld cylinders, %d heads, %d sectors per track (%ldMB) (%s)\n",
680 (u32) geometry->TotalCylinders, (u16) geometry->NumHeads, (u16) geometry->SectorsPerTrack,
681 (u32) (geometry->TotalSectors / 2048), Method);
682
683 /* tell interrupt handler that this IORB is complete */
684 add_workspace(iorb)->complete = 1;
685}
686
687/******************************************************************************
688 * Test whether unit is ready.
689 */
690int ata_unit_ready(IORBH _far *iorb, int slot)
691{
692 /* This is a NOP for ATA devices (at least right now); returning an error
693 * without setting an error code means ahci_exec_iorb() will not queue any
694 * HW command and the IORB will complete successfully.
695 */
696 ((IORB_UNIT_STATUS _far *) iorb)->UnitStatus = US_READY | US_POWER;
697 return(-1);
698}
699
700/******************************************************************************
701 * Read sectors from AHCI device.
702 */
703int ata_read(IORBH _far *iorb, int slot)
704{
705 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
706 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
707 ULONG sector = io->RBA + io->BlocksXferred;
708 USHORT count = io->BlockCount - io->BlocksXferred;
709 USHORT sg_indx;
710 USHORT sg_cnt;
711 int p = iorb_unit_port(iorb);
712 int d = iorb_unit_device(iorb);
713 int rc;
714
715 if (io->BlockCount == 0) {
716 /* NOP; return -1 without error in IORB to indicate success */
717 return(-1);
718 }
719
720 if (add_workspace(iorb)->unaligned) {
721 /* unaligned S/G addresses present; need to use double buffers */
722 return(ata_read_unaligned(iorb, slot));
723 }
724
725 /* Kludge: some I/O commands during boot use excessive S/G buffer lengths
726 * which cause NCQ commands to lock up. If there's only one S/G element
727 * and this element is already larger than what we can derive from the sector
728 * count, we'll adjust that element.
729 */
730 if (io->BlocksXferred == 0 && io->cSGList == 1 &&
731 io->pSGList[0].XferBufLen > (ULONG) io->BlockCount * io->BlockSize) {
732 io->pSGList[0].XferBufLen = (ULONG) io->BlockCount * io->BlockSize;
733 }
734
735 /* prepare read command while keeping an eye on S/G count limitations */
736 do {
737 sg_indx = ata_get_sg_indx(io);
738 sg_cnt = io->cSGList - sg_indx;
739 if ((rc = ata_cmd_read(iorb, ai, p, d, slot, sector, count,
740 io->pSGList + sg_indx, sg_cnt)) > 0) {
741 /* couldn't map all S/G elements */
742 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
743 }
744 } while (rc > 0 && sg_cnt > 0);
745
746 if (rc == 0) {
747 add_workspace(iorb)->blocks = count;
748 add_workspace(iorb)->ppfunc = ata_read_pp;
749
750 } else if (rc > 0) {
751 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
752
753 } else if (rc == ATA_CMD_UNALIGNED_ADDR) {
754 /* unaligned S/G addresses detected; need to use double buffers */
755 add_workspace(iorb)->unaligned = 1;
756 return(ata_read_unaligned(iorb, slot));
757
758 } else {
759 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
760 }
761
762 return(rc);
763}
764
765/******************************************************************************
766 * Read sectors from AHCI device with unaligned S/G element addresses. AHCI
767 * only allows aligned S/G addresses while OS/2 doesn't have these kind of
768 * restrictions. This doesn't happen very often but when it does, we need to
769 * use a transfer buffer and copy the data manually.
770 */
771int ata_read_unaligned(IORBH _far *iorb, int slot)
772{
773 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
774 ADD_WORKSPACE _far *aws = add_workspace(iorb);
775 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
776 ULONG sector = io->RBA + io->BlocksXferred;
777 SCATGATENTRY sg_single;
778 int p = iorb_unit_port(iorb);
779 int d = iorb_unit_device(iorb);
780 int rc;
781
782 ddprintf("ata_read_unaligned(%d.%d.%d, %ld)\n", ad_no(ai), p, d, sector);
783
784 /* allocate transfer buffer */
785 if ((aws->buf = malloc(io->BlockSize)) == NULL) {
786 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
787 return(-1);
788 }
789
790 /* prepare read command using transfer buffer */
791 sg_single.ppXferBuf = virt_to_phys(aws->buf);
792 sg_single.XferBufLen = io->BlockSize;
793 rc = ata_cmd_read(iorb, ai, p, d, slot, sector, 1, &sg_single, 1);
794
795 if (rc == 0) {
796 add_workspace(iorb)->blocks = 1;
797 add_workspace(iorb)->ppfunc = ata_read_pp;
798
799 } else if (rc > 0) {
800 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
801
802 } else {
803 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
804 }
805
806 return(rc);
807}
808
809/******************************************************************************
810 * Post processing function for ata_read(); this function updates the
811 * BlocksXferred counter in the IORB and, if not all blocks have been
812 * transferred, requeues the IORB to process the remaining sectors. It also
813 * takes care of copying data from the transfer buffer for unaligned reads.
814 */
815void ata_read_pp(IORBH _far *iorb)
816{
817 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
818 ADD_WORKSPACE _far *aws = add_workspace(iorb);
819
820 if (aws->unaligned) {
821 /* copy transfer buffer to corresponding physical address in S/G list */
822 sg_memcpy(io->pSGList, io->cSGList,
823 (ULONG) io->BlocksXferred * (ULONG) io->BlockSize,
824 aws->buf, io->BlockSize, BUF_TO_SG);
825 }
826
827 io->BlocksXferred += add_workspace(iorb)->blocks;
828 ddprintf("ata_read_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
829
830 if (io->BlocksXferred >= io->BlockCount) {
831 /* we're done; tell IRQ handler the IORB is complete */
832 add_workspace(iorb)->complete = 1;
833 } else {
834 /* requeue this IORB for next iteration */
835 iorb_requeue(iorb);
836 }
837}
838
839/******************************************************************************
840 * Verify readability of sectors on ATA device.
841 */
842int ata_verify(IORBH _far *iorb, int slot)
843{
844 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
845 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
846 int p = iorb_unit_port(iorb);
847 int d = iorb_unit_device(iorb);
848 int rc;
849
850 if (io->BlockCount == 0) {
851 /* NOP; return -1 without error in IORB to indicate success */
852 return(-1);
853 }
854
855 /* prepare verify command */
856 if (io->RBA >= (1UL << 28) || io->BlockCount > 256) {
857 /* need LBA48 for this command */
858 if (!ai->ports[p].devs[d].lba48) {
859 iorb_seterr(iorb, IOERR_RBA_LIMIT);
860 return(-1);
861 }
862 rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY_EXT,
863 AP_SECTOR_48, (u32) io->RBA, (u16) 0,
864 AP_COUNT, (u16) io->BlockCount,
865 AP_DEVICE, 0x40,
866 AP_END);
867 } else {
868 rc = ata_cmd(ai, p, d, slot, ATA_CMD_VERIFY,
869 AP_SECTOR_28, (u32) io->RBA,
870 AP_COUNT, (u16) io->BlockCount & 0xffU,
871 AP_DEVICE, 0x40,
872 AP_END);
873 }
874
875 return(rc);
876}
877
878/******************************************************************************
879 * Write sectors to AHCI device.
880 */
881int ata_write(IORBH _far *iorb, int slot)
882{
883 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
884 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
885 ULONG sector = io->RBA + io->BlocksXferred;
886 USHORT count = io->BlockCount - io->BlocksXferred;
887 USHORT sg_indx;
888 USHORT sg_cnt;
889 int p = iorb_unit_port(iorb);
890 int d = iorb_unit_device(iorb);
891 int rc;
892
893 if (io->BlockCount == 0) {
894 /* NOP; return -1 without error in IORB to indicate success */
895 return(-1);
896 }
897
898 if (add_workspace(iorb)->unaligned) {
899 /* unaligned S/G addresses present; need to use double buffers */
900 return(ata_write_unaligned(iorb, slot));
901 }
902
903 /* prepare write command while keeping an eye on S/G count limitations */
904 do {
905 sg_indx = ata_get_sg_indx(io);
906 sg_cnt = io->cSGList - sg_indx;
907 if ((rc = ata_cmd_write(iorb, ai, p, d, slot, sector, count,
908 io->pSGList + sg_indx, sg_cnt,
909 io->Flags & XIO_DISABLE_HW_WRITE_CACHE)) > 0) {
910 /* couldn't map all S/G elements */
911 ata_max_sg_cnt(io, sg_indx, (USHORT) rc, &sg_cnt, &count);
912 }
913 } while (rc > 0 && sg_cnt > 0);
914
915 if (rc == 0) {
916 add_workspace(iorb)->blocks = count;
917 add_workspace(iorb)->ppfunc = ata_write_pp;
918
919 } else if (rc > 0) {
920 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
921
922 } else if (rc == ATA_CMD_UNALIGNED_ADDR) {
923 /* unaligned S/G addresses detected; need to use double buffers */
924 add_workspace(iorb)->unaligned = 1;
925 return(ata_write_unaligned(iorb, slot));
926
927 } else {
928 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
929 }
930
931 return(rc);
932}
933
934/******************************************************************************
935 * Write sectors from AHCI device with unaligned S/G element addresses. AHCI
936 * only allows aligned S/G addresses while OS/2 doesn't have these kind of
937 * restrictions. This doesn't happen very often but when it does, we need to
938 * use a transfer buffer and copy the data manually.
939 */
940int ata_write_unaligned(IORBH _far *iorb, int slot)
941{
942 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
943 ADD_WORKSPACE _far *aws = add_workspace(iorb);
944 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
945 ULONG sector = io->RBA + io->BlocksXferred;
946 SCATGATENTRY sg_single;
947 int p = iorb_unit_port(iorb);
948 int d = iorb_unit_device(iorb);
949 int rc;
950
951 ddprintf("ata_write_unaligned(%d.%d.%d, %ld)\n", ad_no(ai), p, d, sector);
952
953 /* allocate transfer buffer */
954 if ((aws->buf = malloc(io->BlockSize)) == NULL) {
955 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
956 return(-1);
957 }
958
959 /* copy next sector from S/G list to transfer buffer */
960 sg_memcpy(io->pSGList, io->cSGList,
961 (ULONG) io->BlocksXferred * (ULONG) io->BlockSize,
962 aws->buf, io->BlockSize, SG_TO_BUF);
963
964 /* prepare write command using transfer buffer */
965 sg_single.ppXferBuf = virt_to_phys(aws->buf);
966 sg_single.XferBufLen = io->BlockSize;
967 rc = ata_cmd_write(iorb, ai, p, d, slot, sector, 1, &sg_single, 1,
968 io->Flags & XIO_DISABLE_HW_WRITE_CACHE);
969
970 if (rc == 0) {
971 add_workspace(iorb)->blocks = 1;
972 add_workspace(iorb)->ppfunc = ata_write_pp;
973
974 } else if (rc > 0) {
975 iorb_seterr(iorb, IOERR_CMD_SGLIST_BAD);
976
977 } else {
978 iorb_seterr(iorb, IOERR_CMD_ADD_SOFTWARE_FAILURE);
979 }
980
981 return(rc);
982}
983
984
985/******************************************************************************
986 * Post processing function for ata_write(); this function updates the
987 * BlocksXferred counter in the IORB and, if not all blocks have been
988 * transferred, requeues the IORB to process the remaining sectors.
989 */
990void ata_write_pp(IORBH _far *iorb)
991{
992 IORB_EXECUTEIO _far *io = (IORB_EXECUTEIO _far *) iorb;
993
994 io->BlocksXferred += add_workspace(iorb)->blocks;
995 ddprintf("ata_write_pp(): blocks transferred = %d\n", (int) io->BlocksXferred);
996
997 if (io->BlocksXferred >= io->BlockCount) {
998 /* we're done; tell IRQ handler the IORB is complete */
999 add_workspace(iorb)->complete = 1;
1000 } else {
1001 /* requeue this IORB for next iteration */
1002 iorb_requeue(iorb);
1003 }
1004}
1005
1006/******************************************************************************
1007 * Execute ATA command.
1008 */
1009int ata_execute_ata(IORBH _far *iorb, int slot)
1010{
1011 IORB_ADAPTER_PASSTHRU _far *apt = (IORB_ADAPTER_PASSTHRU _far *) iorb;
1012 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
1013 int p = iorb_unit_port(iorb);
1014 int d = iorb_unit_device(iorb);
1015 int rc;
1016
1017 if (apt->ControllerCmdLen != sizeof(ATA_CMD)) {
1018 iorb_seterr(iorb, IOERR_CMD_SYNTAX);
1019 return(-1);
1020 }
1021
1022 rc = ata_cmd(ai, p, d, slot, 0,
1023 AP_SGLIST, apt->pSGList, apt->cSGList,
1024 AP_ATA_CMD, apt->pControllerCmd,
1025 AP_WRITE, !(apt->Flags & PT_DIRECTION_IN),
1026 AP_END);
1027
1028 if (rc == 0) {
1029 add_workspace(iorb)->ppfunc = ata_execute_ata_pp;
1030 }
1031
1032 return(rc);
1033}
1034
1035/******************************************************************************
1036 * Post processing function for ata_execute_ata(); the main purpose of this
1037 * function is to copy the received D2H FIS (i.e. the device registers after
1038 * command completion) back to the ATA command structure.
1039 *
1040 * See ata_cmd_to_fis() for an explanation of the mapping.
1041 */
1042void ata_execute_ata_pp(IORBH _far *iorb)
1043{
1044 AHCI_PORT_DMA _far *dma_base;
1045 ATA_CMD _far *cmd;
1046 AD_INFO *ai;
1047 u8 _far *fis;
1048 int p;
1049
1050 /* get address of D2H FIS */
1051 ai = ad_infos + iorb_unit_adapter(iorb);
1052 p = iorb_unit_port(iorb);
1053 dma_base = port_dma_base(ai, p);
1054 fis = dma_base->rx_fis + 0x40;
1055
1056 if (fis[0] != 0x34) {
1057 /* this is not a D2H FIS - give up silently */
1058 ddprintf("ata_execute_ata_pp(): D2H FIS type incorrect: %d\n", fis[0]);
1059 add_workspace(iorb)->complete = 1;
1060 return;
1061 }
1062
1063 /* map D2H FIS to the original ATA controller command structure */
1064 cmd = (ATA_CMD _far *) ((IORB_ADAPTER_PASSTHRU _far *) iorb)->pControllerCmd;
1065
1066 cmd->cmd = fis[2];
1067 cmd->device = fis[7];
1068 cmd->features = ((u16) fis[3])
1069 | ((u16) fis[11]);
1070 cmd->lba_l = ((u32) fis[4])
1071 | ((u32) fis[5] << 8)
1072 | ((u32) fis[6] << 16)
1073 | ((u32) fis[8] << 24);
1074 cmd->lba_h = ((u16) fis[9])
1075 | ((u16) fis[10] << 8);
1076 cmd->count = ((u16) fis[12])
1077 | ((u16) fis[13] << 8);
1078
1079 dphex(cmd, sizeof(*cmd), "ahci_execute_ata_pp(): cmd after completion:\n");
1080
1081 /* signal completion to interrupt handler */
1082 add_workspace(iorb)->complete = 1;
1083}
1084
1085/******************************************************************************
1086 * Request sense information for a failed command. Since there is no "request
1087 * sense" command for ATA devices, we need to read the current error code from
1088 * the AHCI task file register and fabricate the sense information.
1089 *
1090 * NOTES:
1091 *
1092 * - This function must be called right after an ATA command has failed and
1093 * before any other commands are queued on the corresponding port. This
1094 * function is typically called in the port restart context hook which is
1095 * triggered by an AHCI error interrupt.
1096 *
1097 * - The ATA error bits are a complete mess. We'll try and catch the most
1098 * interesting error codes (such as medium errors) and report everything
1099 * else with a generic error code.
1100 */
1101int ata_req_sense(IORBH _far *iorb, int slot)
1102{
1103 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
1104 u8 _far *port_mmio = port_base(ai, iorb_unit_port(iorb));
1105 u32 tf_data = readl(port_mmio + PORT_TFDATA);
1106 u8 err = (u8) (tf_data >> 8);
1107 u8 sts = (u8) (tf_data);
1108
1109 if (sts & ATA_ERR) {
1110 if (sts & ATA_DF) {
1111 /* there is a device-specific error condition */
1112 if (err & ATA_ICRC) {
1113 iorb_seterr(iorb, IOERR_ADAPTER_DEVICEBUSCHECK);
1114 } else if (err & ATA_UNC) {
1115 iorb_seterr(iorb, IOERR_MEDIA);
1116 } else if (err & ATA_IDNF) {
1117 iorb_seterr(iorb, IOERR_RBA_ADDRESSING_ERROR);
1118 } else {
1119 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
1120 }
1121
1122 } else {
1123 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
1124 }
1125 } else {
1126 /* this function only gets called when we received an error interrupt */
1127 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
1128 }
1129
1130 /* Return an error to indicate there's no HW command to be submitted and
1131 * that the IORB can be completed "as is" (the upstream code expects the
1132 * IORB error code, if any, to be set when this happens and this is exactly
1133 * what this function is all about).
1134 */
1135 return(-1);
1136}
1137
1138/******************************************************************************
1139 * Extract vendor and device name from an ATA INDENTIFY buffer. Since strings
1140 * in the indentify buffer are byte-swapped, we need to swap them back.
1141 */
1142char *ata_dev_name(u16 *id_buf)
1143{
1144 static char dev_name[ATA_ID_PROD_LEN + 1];
1145 char *t = dev_name;
1146 char *s = (char *) (id_buf + ATA_ID_PROD);
1147 int i;
1148
1149 dev_name[sizeof(dev_name)-1] = '\0';
1150
1151 for (i = 0; i < ATA_ID_PROD_LEN / 2; i++) {
1152 *(t++) = s[1];
1153 *(t++) = s[0];
1154 s += 2;
1155 }
1156
1157 return(dev_name);
1158}
1159
1160/******************************************************************************
1161 * Fabricate ATA READ command based on the capabilities of the corresponding
1162 * device and the paramters set from above (NCQ, etc).
1163 */
1164static int ata_cmd_read(IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
1165 ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
1166 ULONG sg_cnt)
1167{
1168 int rc;
1169
1170 if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
1171 /* need LBA48 for this command */
1172 if (!ai->ports[p].devs[d].lba48) {
1173 iorb_seterr(iorb, IOERR_RBA_LIMIT);
1174 return(-1);
1175 }
1176 if (add_workspace(iorb)->is_ncq) {
1177 /* use NCQ read; count goes into feature register, tag into count! */
1178 rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_READ,
1179 AP_SECTOR_48, (u32) sector, (u16) 0,
1180 AP_FEATURES, (u16) count,
1181 AP_COUNT, (u16) (slot << 3), /* tag == slot */
1182 AP_SGLIST, sg_list, (u16) sg_cnt,
1183 AP_DEVICE, 0x40,
1184 AP_END);
1185 } else {
1186 rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ_EXT,
1187 AP_SECTOR_48, (u32) sector, (u16) 0,
1188 AP_COUNT, (u16) count,
1189 AP_SGLIST, sg_list, (u16) sg_cnt,
1190 AP_DEVICE, 0x40,
1191 AP_END);
1192 }
1193
1194 } else {
1195 rc = ata_cmd(ai, p, d, slot, ATA_CMD_READ,
1196 AP_SECTOR_28, (u32) sector,
1197 AP_COUNT, (u16) count & 0xffU,
1198 AP_SGLIST, sg_list, (u16) sg_cnt,
1199 AP_DEVICE, 0x40,
1200 AP_END);
1201 }
1202
1203 return(rc);
1204}
1205
1206/******************************************************************************
1207 * Fabricate ATA WRITE command based on the capabilities of the corresponding
1208 * device and the paramters set from above (NCQ, etc)
1209 */
1210static int ata_cmd_write(IORBH _far *iorb, AD_INFO *ai, int p, int d, int slot,
1211 ULONG sector, ULONG count, SCATGATENTRY _far *sg_list,
1212 ULONG sg_cnt, int write_through)
1213{
1214 int rc;
1215
1216 if (sector >= (1UL << 28) || count > 256 || add_workspace(iorb)->is_ncq) {
1217 /* need LBA48 for this command */
1218 if (!ai->ports[p].devs[d].lba48) {
1219 iorb_seterr(iorb, IOERR_RBA_LIMIT);
1220 return(-1);
1221 }
1222 if (add_workspace(iorb)->is_ncq) {
1223 /* use NCQ write; count goes into feature register, tag into count! */
1224 rc = ata_cmd(ai, p, d, slot, ATA_CMD_FPDMA_WRITE,
1225 AP_SECTOR_48, (u32) sector, (u16) 0,
1226 AP_FEATURES, (u16) count,
1227 /* tag = slot */
1228 AP_COUNT, (u16) (slot << 3),
1229 AP_SGLIST, sg_list, (u16) sg_cnt,
1230 AP_DEVICE, 0x40,
1231 /* force unit access */
1232 AP_DEVICE, (write_through && !force_write_cache) ? 0x80 : 0,
1233 AP_WRITE, 1,
1234 AP_END);
1235 } else {
1236 rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE_EXT,
1237 AP_SECTOR_48, (u32) sector, (u16) 0,
1238 AP_COUNT, (u16) count,
1239 AP_SGLIST, sg_list, (u16) sg_cnt,
1240 AP_DEVICE, 0x40,
1241 AP_WRITE, 1,
1242 AP_END);
1243 }
1244
1245 } else {
1246 rc = ata_cmd(ai, p, d, slot, ATA_CMD_WRITE,
1247 AP_SECTOR_28, (u32) sector,
1248 AP_COUNT, (u16) count & 0xffU,
1249 AP_SGLIST, sg_list, (u16) sg_cnt,
1250 AP_DEVICE, 0x40,
1251 AP_WRITE, 1,
1252 AP_END);
1253 }
1254
1255 return(rc);
1256}
Note: See TracBrowser for help on using the repository browser.