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