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