1 | /*
|
---|
2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup bd
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief ATA disk driver
|
---|
36 | *
|
---|
37 | * This driver supports CHS, 28-bit and 48-bit LBA addressing. It only uses
|
---|
38 | * PIO transfers. There is no support DMA, the PACKET feature set or any other
|
---|
39 | * fancy features such as S.M.A.R.T, removable devices, etc.
|
---|
40 | *
|
---|
41 | * This driver is based on the ATA-1, ATA-2, ATA-3 and ATA/ATAPI-4 through 7
|
---|
42 | * standards, as published by the ANSI, NCITS and INCITS standards bodies,
|
---|
43 | * which are freely available. This driver contains no vendor-specific
|
---|
44 | * code at this moment.
|
---|
45 | *
|
---|
46 | * The driver services a single controller which can have up to two disks
|
---|
47 | * attached.
|
---|
48 | */
|
---|
49 |
|
---|
50 | #include <stdio.h>
|
---|
51 | #include <libarch/ddi.h>
|
---|
52 | #include <ddi.h>
|
---|
53 | #include <ipc/bd.h>
|
---|
54 | #include <async.h>
|
---|
55 | #include <as.h>
|
---|
56 | #include <fibril_synch.h>
|
---|
57 | #include <stdint.h>
|
---|
58 | #include <str.h>
|
---|
59 | #include <devmap.h>
|
---|
60 | #include <sys/types.h>
|
---|
61 | #include <inttypes.h>
|
---|
62 | #include <errno.h>
|
---|
63 | #include <bool.h>
|
---|
64 | #include <byteorder.h>
|
---|
65 | #include <task.h>
|
---|
66 | #include <macros.h>
|
---|
67 |
|
---|
68 | #include "ata_hw.h"
|
---|
69 | #include "ata_bd.h"
|
---|
70 |
|
---|
71 | #define NAME "ata_bd"
|
---|
72 | #define NAMESPACE "bd"
|
---|
73 |
|
---|
74 | /** Number of defined legacy controller base addresses. */
|
---|
75 | #define LEGACY_CTLS 4
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Size of data returned from Identify Device or Identify Packet Device
|
---|
79 | * command.
|
---|
80 | */
|
---|
81 | static const size_t identify_data_size = 512;
|
---|
82 |
|
---|
83 | /** Size of the communication area. */
|
---|
84 | static size_t comm_size;
|
---|
85 |
|
---|
86 | /** I/O base address of the command registers. */
|
---|
87 | static uintptr_t cmd_physical;
|
---|
88 | /** I/O base address of the control registers. */
|
---|
89 | static uintptr_t ctl_physical;
|
---|
90 |
|
---|
91 | /** I/O base addresses for legacy (ISA-compatible) controllers. */
|
---|
92 | static ata_base_t legacy_base[LEGACY_CTLS] = {
|
---|
93 | { 0x1f0, 0x3f0 },
|
---|
94 | { 0x170, 0x370 },
|
---|
95 | { 0x1e8, 0x3e8 },
|
---|
96 | { 0x168, 0x368 }
|
---|
97 | };
|
---|
98 |
|
---|
99 | static ata_cmd_t *cmd;
|
---|
100 | static ata_ctl_t *ctl;
|
---|
101 |
|
---|
102 | /** Per-disk state. */
|
---|
103 | static disk_t disk[MAX_DISKS];
|
---|
104 |
|
---|
105 | static void print_syntax(void);
|
---|
106 | static int ata_bd_init(void);
|
---|
107 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
|
---|
108 | static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
|
---|
109 | void *buf);
|
---|
110 | static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
|
---|
111 | const void *buf);
|
---|
112 | static int ata_rcmd_read(int disk_id, uint64_t ba, size_t cnt,
|
---|
113 | void *buf);
|
---|
114 | static int ata_rcmd_write(int disk_id, uint64_t ba, size_t cnt,
|
---|
115 | const void *buf);
|
---|
116 | static int disk_init(disk_t *d, int disk_id);
|
---|
117 | static int drive_identify(int drive_id, void *buf);
|
---|
118 | static int identify_pkt_dev(int dev_idx, void *buf);
|
---|
119 | static int ata_cmd_packet(int dev_idx, const void *cpkt, size_t cpkt_size,
|
---|
120 | void *obuf, size_t obuf_size);
|
---|
121 | static int ata_pcmd_inquiry(int dev_idx, void *obuf, size_t obuf_size);
|
---|
122 | static int ata_pcmd_read_12(int dev_idx, uint64_t ba, size_t cnt,
|
---|
123 | void *obuf, size_t obuf_size);
|
---|
124 | static void disk_print_summary(disk_t *d);
|
---|
125 | static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc);
|
---|
126 | static void coord_sc_program(const block_coord_t *bc, uint16_t scnt);
|
---|
127 | static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
|
---|
128 | unsigned timeout);
|
---|
129 |
|
---|
130 | int main(int argc, char **argv)
|
---|
131 | {
|
---|
132 | char name[16];
|
---|
133 | int i, rc;
|
---|
134 | int n_disks;
|
---|
135 | unsigned ctl_num;
|
---|
136 | char *eptr;
|
---|
137 |
|
---|
138 | printf(NAME ": ATA disk driver\n");
|
---|
139 |
|
---|
140 | if (argc > 1) {
|
---|
141 | ctl_num = strtoul(argv[1], &eptr, 0);
|
---|
142 | if (*eptr != '\0' || ctl_num == 0 || ctl_num > 4) {
|
---|
143 | printf("Invalid argument.\n");
|
---|
144 | print_syntax();
|
---|
145 | return -1;
|
---|
146 | }
|
---|
147 | } else {
|
---|
148 | ctl_num = 1;
|
---|
149 | }
|
---|
150 |
|
---|
151 | cmd_physical = legacy_base[ctl_num - 1].cmd;
|
---|
152 | ctl_physical = legacy_base[ctl_num - 1].ctl;
|
---|
153 |
|
---|
154 | printf("I/O address %p/%p\n", (void *) cmd_physical,
|
---|
155 | (void *) ctl_physical);
|
---|
156 |
|
---|
157 | if (ata_bd_init() != EOK)
|
---|
158 | return -1;
|
---|
159 |
|
---|
160 | for (i = 0; i < MAX_DISKS; i++) {
|
---|
161 | printf("Identify drive %d... ", i);
|
---|
162 | fflush(stdout);
|
---|
163 |
|
---|
164 | rc = disk_init(&disk[i], i);
|
---|
165 |
|
---|
166 | if (rc == EOK) {
|
---|
167 | disk_print_summary(&disk[i]);
|
---|
168 | } else {
|
---|
169 | printf("Not found.\n");
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | n_disks = 0;
|
---|
174 |
|
---|
175 | for (i = 0; i < MAX_DISKS; i++) {
|
---|
176 | /* Skip unattached drives. */
|
---|
177 | if (disk[i].present == false)
|
---|
178 | continue;
|
---|
179 |
|
---|
180 | snprintf(name, 16, "%s/ata%udisk%d", NAMESPACE, ctl_num, i);
|
---|
181 | rc = devmap_device_register(name, &disk[i].devmap_handle);
|
---|
182 | if (rc != EOK) {
|
---|
183 | printf(NAME ": Unable to register device %s.\n", name);
|
---|
184 | return rc;
|
---|
185 | }
|
---|
186 | ++n_disks;
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (n_disks == 0) {
|
---|
190 | printf("No disks detected.\n");
|
---|
191 | return -1;
|
---|
192 | }
|
---|
193 |
|
---|
194 | printf(NAME ": Accepting connections\n");
|
---|
195 | task_retval(0);
|
---|
196 | async_manager();
|
---|
197 |
|
---|
198 | /* Not reached */
|
---|
199 | return 0;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | static void print_syntax(void)
|
---|
204 | {
|
---|
205 | printf("Syntax: " NAME " <controller_number>\n");
|
---|
206 | printf("Controller number = 1..4\n");
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Print one-line device summary. */
|
---|
210 | static void disk_print_summary(disk_t *d)
|
---|
211 | {
|
---|
212 | uint64_t mbytes;
|
---|
213 |
|
---|
214 | printf("%s: ", d->model);
|
---|
215 |
|
---|
216 | if (d->dev_type == ata_reg_dev) {
|
---|
217 | switch (d->amode) {
|
---|
218 | case am_chs:
|
---|
219 | printf("CHS %u cylinders, %u heads, %u sectors",
|
---|
220 | disk->geom.cylinders, disk->geom.heads,
|
---|
221 | disk->geom.sectors);
|
---|
222 | break;
|
---|
223 | case am_lba28:
|
---|
224 | printf("LBA-28");
|
---|
225 | break;
|
---|
226 | case am_lba48:
|
---|
227 | printf("LBA-48");
|
---|
228 | break;
|
---|
229 | }
|
---|
230 | } else {
|
---|
231 | printf("PACKET");
|
---|
232 | }
|
---|
233 |
|
---|
234 | printf(" %" PRIu64 " blocks", d->blocks);
|
---|
235 |
|
---|
236 | mbytes = d->blocks / (2 * 1024);
|
---|
237 | if (mbytes > 0)
|
---|
238 | printf(" %" PRIu64 " MB.", mbytes);
|
---|
239 |
|
---|
240 | printf("\n");
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** Register driver and enable device I/O. */
|
---|
244 | static int ata_bd_init(void)
|
---|
245 | {
|
---|
246 | void *vaddr;
|
---|
247 | int rc;
|
---|
248 |
|
---|
249 | rc = devmap_driver_register(NAME, ata_bd_connection);
|
---|
250 | if (rc < 0) {
|
---|
251 | printf(NAME ": Unable to register driver.\n");
|
---|
252 | return rc;
|
---|
253 | }
|
---|
254 |
|
---|
255 | rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
|
---|
256 | if (rc != EOK) {
|
---|
257 | printf(NAME ": Could not initialize device I/O space.\n");
|
---|
258 | return rc;
|
---|
259 | }
|
---|
260 |
|
---|
261 | cmd = vaddr;
|
---|
262 |
|
---|
263 | rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
|
---|
264 | if (rc != EOK) {
|
---|
265 | printf(NAME ": Could not initialize device I/O space.\n");
|
---|
266 | return rc;
|
---|
267 | }
|
---|
268 |
|
---|
269 | ctl = vaddr;
|
---|
270 |
|
---|
271 |
|
---|
272 | return EOK;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /** Block device connection handler */
|
---|
276 | static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
277 | {
|
---|
278 | void *fs_va = NULL;
|
---|
279 | ipc_callid_t callid;
|
---|
280 | ipc_call_t call;
|
---|
281 | sysarg_t method;
|
---|
282 | devmap_handle_t dh;
|
---|
283 | unsigned int flags;
|
---|
284 | int retval;
|
---|
285 | uint64_t ba;
|
---|
286 | size_t cnt;
|
---|
287 | int disk_id, i;
|
---|
288 |
|
---|
289 | /* Get the device handle. */
|
---|
290 | dh = IPC_GET_ARG1(*icall);
|
---|
291 |
|
---|
292 | /* Determine which disk device is the client connecting to. */
|
---|
293 | disk_id = -1;
|
---|
294 | for (i = 0; i < MAX_DISKS; i++)
|
---|
295 | if (disk[i].devmap_handle == dh)
|
---|
296 | disk_id = i;
|
---|
297 |
|
---|
298 | if (disk_id < 0 || disk[disk_id].present == false) {
|
---|
299 | async_answer_0(iid, EINVAL);
|
---|
300 | return;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
---|
304 | async_answer_0(iid, EOK);
|
---|
305 |
|
---|
306 | if (!async_share_out_receive(&callid, &comm_size, &flags)) {
|
---|
307 | async_answer_0(callid, EHANGUP);
|
---|
308 | return;
|
---|
309 | }
|
---|
310 |
|
---|
311 | fs_va = as_get_mappable_page(comm_size);
|
---|
312 | if (fs_va == NULL) {
|
---|
313 | async_answer_0(callid, EHANGUP);
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | (void) async_share_out_finalize(callid, fs_va);
|
---|
318 |
|
---|
319 | while (1) {
|
---|
320 | callid = async_get_call(&call);
|
---|
321 | method = IPC_GET_IMETHOD(call);
|
---|
322 | switch (method) {
|
---|
323 | case IPC_M_PHONE_HUNGUP:
|
---|
324 | /* The other side has hung up. */
|
---|
325 | async_answer_0(callid, EOK);
|
---|
326 | return;
|
---|
327 | case BD_READ_BLOCKS:
|
---|
328 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
---|
329 | IPC_GET_ARG2(call));
|
---|
330 | cnt = IPC_GET_ARG3(call);
|
---|
331 | if (cnt * disk[disk_id].block_size > comm_size) {
|
---|
332 | retval = ELIMIT;
|
---|
333 | break;
|
---|
334 | }
|
---|
335 | retval = ata_bd_read_blocks(disk_id, ba, cnt, fs_va);
|
---|
336 | break;
|
---|
337 | case BD_WRITE_BLOCKS:
|
---|
338 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
---|
339 | IPC_GET_ARG2(call));
|
---|
340 | cnt = IPC_GET_ARG3(call);
|
---|
341 | if (cnt * disk[disk_id].block_size > comm_size) {
|
---|
342 | retval = ELIMIT;
|
---|
343 | break;
|
---|
344 | }
|
---|
345 | retval = ata_bd_write_blocks(disk_id, ba, cnt, fs_va);
|
---|
346 | break;
|
---|
347 | case BD_GET_BLOCK_SIZE:
|
---|
348 | async_answer_1(callid, EOK, disk[disk_id].block_size);
|
---|
349 | continue;
|
---|
350 | case BD_GET_NUM_BLOCKS:
|
---|
351 | async_answer_2(callid, EOK, LOWER32(disk[disk_id].blocks),
|
---|
352 | UPPER32(disk[disk_id].blocks));
|
---|
353 | continue;
|
---|
354 | default:
|
---|
355 | retval = EINVAL;
|
---|
356 | break;
|
---|
357 | }
|
---|
358 | async_answer_0(callid, retval);
|
---|
359 | }
|
---|
360 | }
|
---|
361 |
|
---|
362 | /** Initialize a disk.
|
---|
363 | *
|
---|
364 | * Probes for a disk, determines its parameters and initializes
|
---|
365 | * the disk structure.
|
---|
366 | */
|
---|
367 | static int disk_init(disk_t *d, int disk_id)
|
---|
368 | {
|
---|
369 | identify_data_t idata;
|
---|
370 | uint8_t model[40];
|
---|
371 | ata_inquiry_data_t inq_data;
|
---|
372 | uint16_t w;
|
---|
373 | uint8_t c;
|
---|
374 | size_t pos, len;
|
---|
375 | int rc;
|
---|
376 | unsigned i;
|
---|
377 |
|
---|
378 | d->present = false;
|
---|
379 | fibril_mutex_initialize(&d->lock);
|
---|
380 |
|
---|
381 | /* Try identify command. */
|
---|
382 | rc = drive_identify(disk_id, &idata);
|
---|
383 | if (rc == EOK) {
|
---|
384 | /* Success. It's a register (non-packet) device. */
|
---|
385 | printf("ATA register-only device found.\n");
|
---|
386 | d->dev_type = ata_reg_dev;
|
---|
387 | } else if (rc == EIO) {
|
---|
388 | /*
|
---|
389 | * There is something, but not a register device.
|
---|
390 | * It could be a packet device.
|
---|
391 | */
|
---|
392 | rc = identify_pkt_dev(disk_id, &idata);
|
---|
393 | if (rc == EOK) {
|
---|
394 | /* We have a packet device. */
|
---|
395 | d->dev_type = ata_pkt_dev;
|
---|
396 | } else {
|
---|
397 | /* Nope. Something's there, but not recognized. */
|
---|
398 | return EIO;
|
---|
399 | }
|
---|
400 | } else {
|
---|
401 | /* Operation timed out. That means there is no device there. */
|
---|
402 | return EIO;
|
---|
403 | }
|
---|
404 |
|
---|
405 | printf("device caps: 0x%04x\n", idata.caps);
|
---|
406 | if (d->dev_type == ata_pkt_dev) {
|
---|
407 | /* Packet device */
|
---|
408 | d->amode = 0;
|
---|
409 |
|
---|
410 | d->geom.cylinders = 0;
|
---|
411 | d->geom.heads = 0;
|
---|
412 | d->geom.sectors = 0;
|
---|
413 |
|
---|
414 | d->blocks = 0;
|
---|
415 | } else if ((idata.caps & rd_cap_lba) == 0) {
|
---|
416 | /* Device only supports CHS addressing. */
|
---|
417 | d->amode = am_chs;
|
---|
418 |
|
---|
419 | d->geom.cylinders = idata.cylinders;
|
---|
420 | d->geom.heads = idata.heads;
|
---|
421 | d->geom.sectors = idata.sectors;
|
---|
422 |
|
---|
423 | d->blocks = d->geom.cylinders * d->geom.heads * d->geom.sectors;
|
---|
424 | } else if ((idata.cmd_set1 & cs1_addr48) == 0) {
|
---|
425 | /* Device only supports LBA-28 addressing. */
|
---|
426 | d->amode = am_lba28;
|
---|
427 |
|
---|
428 | d->geom.cylinders = 0;
|
---|
429 | d->geom.heads = 0;
|
---|
430 | d->geom.sectors = 0;
|
---|
431 |
|
---|
432 | d->blocks =
|
---|
433 | (uint32_t) idata.total_lba28_0 |
|
---|
434 | ((uint32_t) idata.total_lba28_1 << 16);
|
---|
435 | } else {
|
---|
436 | /* Device supports LBA-48 addressing. */
|
---|
437 | d->amode = am_lba48;
|
---|
438 |
|
---|
439 | d->geom.cylinders = 0;
|
---|
440 | d->geom.heads = 0;
|
---|
441 | d->geom.sectors = 0;
|
---|
442 |
|
---|
443 | d->blocks =
|
---|
444 | (uint64_t) idata.total_lba48_0 |
|
---|
445 | ((uint64_t) idata.total_lba48_1 << 16) |
|
---|
446 | ((uint64_t) idata.total_lba48_2 << 32) |
|
---|
447 | ((uint64_t) idata.total_lba48_3 << 48);
|
---|
448 | }
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Convert model name to string representation.
|
---|
452 | */
|
---|
453 | for (i = 0; i < 20; i++) {
|
---|
454 | w = idata.model_name[i];
|
---|
455 | model[2 * i] = w >> 8;
|
---|
456 | model[2 * i + 1] = w & 0x00ff;
|
---|
457 | }
|
---|
458 |
|
---|
459 | len = 40;
|
---|
460 | while (len > 0 && model[len - 1] == 0x20)
|
---|
461 | --len;
|
---|
462 |
|
---|
463 | pos = 0;
|
---|
464 | for (i = 0; i < len; ++i) {
|
---|
465 | c = model[i];
|
---|
466 | if (c >= 0x80) c = '?';
|
---|
467 |
|
---|
468 | chr_encode(c, d->model, &pos, 40);
|
---|
469 | }
|
---|
470 | d->model[pos] = '\0';
|
---|
471 |
|
---|
472 | if (d->dev_type == ata_pkt_dev) {
|
---|
473 | /* Send inquiry. */
|
---|
474 | rc = ata_pcmd_inquiry(0, &inq_data, sizeof(inq_data));
|
---|
475 | if (rc != EOK) {
|
---|
476 | printf("Device inquiry failed.\n");
|
---|
477 | d->present = false;
|
---|
478 | return EIO;
|
---|
479 | }
|
---|
480 |
|
---|
481 | /* Check device type. */
|
---|
482 | if (INQUIRY_PDEV_TYPE(inq_data.pdev_type) != PDEV_TYPE_CDROM)
|
---|
483 | printf("Warning: Peripheral device type is not CD-ROM.\n");
|
---|
484 |
|
---|
485 | /* Assume 2k block size for now. */
|
---|
486 | d->block_size = 2048;
|
---|
487 | } else {
|
---|
488 | /* Assume register Read always uses 512-byte blocks. */
|
---|
489 | d->block_size = 512;
|
---|
490 | }
|
---|
491 |
|
---|
492 | d->present = true;
|
---|
493 | return EOK;
|
---|
494 | }
|
---|
495 |
|
---|
496 | /** Read multiple blocks from the device. */
|
---|
497 | static int ata_bd_read_blocks(int disk_id, uint64_t ba, size_t cnt,
|
---|
498 | void *buf) {
|
---|
499 |
|
---|
500 | int rc;
|
---|
501 |
|
---|
502 | while (cnt > 0) {
|
---|
503 | if (disk[disk_id].dev_type == ata_reg_dev)
|
---|
504 | rc = ata_rcmd_read(disk_id, ba, 1, buf);
|
---|
505 | else
|
---|
506 | rc = ata_pcmd_read_12(disk_id, ba, 1, buf,
|
---|
507 | disk[disk_id].block_size);
|
---|
508 |
|
---|
509 | if (rc != EOK)
|
---|
510 | return rc;
|
---|
511 |
|
---|
512 | ++ba;
|
---|
513 | --cnt;
|
---|
514 | buf += disk[disk_id].block_size;
|
---|
515 | }
|
---|
516 |
|
---|
517 | return EOK;
|
---|
518 | }
|
---|
519 |
|
---|
520 | /** Write multiple blocks to the device. */
|
---|
521 | static int ata_bd_write_blocks(int disk_id, uint64_t ba, size_t cnt,
|
---|
522 | const void *buf) {
|
---|
523 |
|
---|
524 | int rc;
|
---|
525 |
|
---|
526 | if (disk[disk_id].dev_type != ata_reg_dev)
|
---|
527 | return ENOTSUP;
|
---|
528 |
|
---|
529 | while (cnt > 0) {
|
---|
530 | rc = ata_rcmd_write(disk_id, ba, 1, buf);
|
---|
531 | if (rc != EOK)
|
---|
532 | return rc;
|
---|
533 |
|
---|
534 | ++ba;
|
---|
535 | --cnt;
|
---|
536 | buf += disk[disk_id].block_size;
|
---|
537 | }
|
---|
538 |
|
---|
539 | return EOK;
|
---|
540 | }
|
---|
541 |
|
---|
542 | /** Issue IDENTIFY command.
|
---|
543 | *
|
---|
544 | * Reads @c identify data into the provided buffer. This is used to detect
|
---|
545 | * whether an ATA device is present and if so, to determine its parameters.
|
---|
546 | *
|
---|
547 | * @param disk_id Device ID, 0 or 1.
|
---|
548 | * @param buf Pointer to a 512-byte buffer.
|
---|
549 | *
|
---|
550 | * @return ETIMEOUT on timeout (this can mean the device is
|
---|
551 | * not present). EIO if device responds with error.
|
---|
552 | */
|
---|
553 | static int drive_identify(int disk_id, void *buf)
|
---|
554 | {
|
---|
555 | uint16_t data;
|
---|
556 | uint8_t status;
|
---|
557 | uint8_t drv_head;
|
---|
558 | size_t i;
|
---|
559 |
|
---|
560 | drv_head = ((disk_id != 0) ? DHR_DRV : 0);
|
---|
561 |
|
---|
562 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
|
---|
563 | return ETIMEOUT;
|
---|
564 |
|
---|
565 | pio_write_8(&cmd->drive_head, drv_head);
|
---|
566 |
|
---|
567 | /*
|
---|
568 | * This is where we would most likely expect a non-existing device to
|
---|
569 | * show up by not setting SR_DRDY.
|
---|
570 | */
|
---|
571 | if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
|
---|
572 | return ETIMEOUT;
|
---|
573 |
|
---|
574 | pio_write_8(&cmd->command, CMD_IDENTIFY_DRIVE);
|
---|
575 |
|
---|
576 | if (wait_status(0, ~SR_BSY, &status, TIMEOUT_PROBE) != EOK)
|
---|
577 | return ETIMEOUT;
|
---|
578 |
|
---|
579 | /* Read data from the disk buffer. */
|
---|
580 |
|
---|
581 | if ((status & SR_DRQ) != 0) {
|
---|
582 | for (i = 0; i < identify_data_size / 2; i++) {
|
---|
583 | data = pio_read_16(&cmd->data_port);
|
---|
584 | ((uint16_t *) buf)[i] = data;
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | if ((status & SR_ERR) != 0) {
|
---|
589 | return EIO;
|
---|
590 | }
|
---|
591 |
|
---|
592 | return EOK;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /** Issue Identify Packet Device command.
|
---|
596 | *
|
---|
597 | * Reads @c identify data into the provided buffer. This is used to detect
|
---|
598 | * whether an ATAPI device is present and if so, to determine its parameters.
|
---|
599 | *
|
---|
600 | * @param dev_idx Device index, 0 or 1.
|
---|
601 | * @param buf Pointer to a 512-byte buffer.
|
---|
602 | */
|
---|
603 | static int identify_pkt_dev(int dev_idx, void *buf)
|
---|
604 | {
|
---|
605 | uint16_t data;
|
---|
606 | uint8_t status;
|
---|
607 | uint8_t drv_head;
|
---|
608 | size_t i;
|
---|
609 |
|
---|
610 | drv_head = ((dev_idx != 0) ? DHR_DRV : 0);
|
---|
611 |
|
---|
612 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
|
---|
613 | return EIO;
|
---|
614 |
|
---|
615 | pio_write_8(&cmd->drive_head, drv_head);
|
---|
616 |
|
---|
617 | /* For ATAPI commands we do not need to wait for DRDY. */
|
---|
618 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
|
---|
619 | return EIO;
|
---|
620 |
|
---|
621 | pio_write_8(&cmd->command, CMD_IDENTIFY_PKT_DEV);
|
---|
622 |
|
---|
623 | if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK)
|
---|
624 | return EIO;
|
---|
625 |
|
---|
626 | /* Read data from the device buffer. */
|
---|
627 |
|
---|
628 | if ((status & SR_DRQ) != 0) {
|
---|
629 | for (i = 0; i < identify_data_size / 2; i++) {
|
---|
630 | data = pio_read_16(&cmd->data_port);
|
---|
631 | ((uint16_t *) buf)[i] = data;
|
---|
632 | }
|
---|
633 | }
|
---|
634 |
|
---|
635 | if ((status & SR_ERR) != 0)
|
---|
636 | return EIO;
|
---|
637 |
|
---|
638 | return EOK;
|
---|
639 | }
|
---|
640 |
|
---|
641 | /** Issue packet command (i. e. write a command packet to the device).
|
---|
642 | *
|
---|
643 | * Only data-in commands are supported (e.g. inquiry, read).
|
---|
644 | *
|
---|
645 | * @param dev_idx Device index (0 or 1)
|
---|
646 | * @param obuf Buffer for storing data read from device
|
---|
647 | * @param obuf_size Size of obuf in bytes
|
---|
648 | *
|
---|
649 | * @return EOK on success, EIO on error.
|
---|
650 | */
|
---|
651 | static int ata_cmd_packet(int dev_idx, const void *cpkt, size_t cpkt_size,
|
---|
652 | void *obuf, size_t obuf_size)
|
---|
653 | {
|
---|
654 | size_t i;
|
---|
655 | uint8_t status;
|
---|
656 | uint8_t drv_head;
|
---|
657 | disk_t *d;
|
---|
658 | size_t data_size;
|
---|
659 | uint16_t val;
|
---|
660 |
|
---|
661 | d = &disk[dev_idx];
|
---|
662 | fibril_mutex_lock(&d->lock);
|
---|
663 |
|
---|
664 | /* New value for Drive/Head register */
|
---|
665 | drv_head =
|
---|
666 | ((dev_idx != 0) ? DHR_DRV : 0);
|
---|
667 |
|
---|
668 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK) {
|
---|
669 | fibril_mutex_unlock(&d->lock);
|
---|
670 | return EIO;
|
---|
671 | }
|
---|
672 |
|
---|
673 | pio_write_8(&cmd->drive_head, drv_head);
|
---|
674 |
|
---|
675 | if (wait_status(0, ~(SR_BSY|SR_DRQ), NULL, TIMEOUT_BSY) != EOK) {
|
---|
676 | fibril_mutex_unlock(&d->lock);
|
---|
677 | return EIO;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /* Byte count <- max. number of bytes we can read in one transfer. */
|
---|
681 | pio_write_8(&cmd->cylinder_low, 0xfe);
|
---|
682 | pio_write_8(&cmd->cylinder_high, 0xff);
|
---|
683 |
|
---|
684 | pio_write_8(&cmd->command, CMD_PACKET);
|
---|
685 |
|
---|
686 | if (wait_status(SR_DRQ, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
|
---|
687 | fibril_mutex_unlock(&d->lock);
|
---|
688 | return EIO;
|
---|
689 | }
|
---|
690 |
|
---|
691 | /* Write command packet. */
|
---|
692 | for (i = 0; i < (cpkt_size + 1) / 2; i++)
|
---|
693 | pio_write_16(&cmd->data_port, ((uint16_t *) cpkt)[i]);
|
---|
694 |
|
---|
695 | if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
|
---|
696 | fibril_mutex_unlock(&d->lock);
|
---|
697 | return EIO;
|
---|
698 | }
|
---|
699 |
|
---|
700 | if ((status & SR_DRQ) == 0) {
|
---|
701 | fibril_mutex_unlock(&d->lock);
|
---|
702 | return EIO;
|
---|
703 | }
|
---|
704 |
|
---|
705 | /* Read byte count. */
|
---|
706 | data_size = (uint16_t) pio_read_8(&cmd->cylinder_low) +
|
---|
707 | ((uint16_t) pio_read_8(&cmd->cylinder_high) << 8);
|
---|
708 |
|
---|
709 | /* Check whether data fits into output buffer. */
|
---|
710 | if (data_size > obuf_size) {
|
---|
711 | /* Output buffer is too small to store data. */
|
---|
712 | fibril_mutex_unlock(&d->lock);
|
---|
713 | return EIO;
|
---|
714 | }
|
---|
715 |
|
---|
716 | /* Read data from the device buffer. */
|
---|
717 | for (i = 0; i < (data_size + 1) / 2; i++) {
|
---|
718 | val = pio_read_16(&cmd->data_port);
|
---|
719 | ((uint16_t *) obuf)[i] = val;
|
---|
720 | }
|
---|
721 |
|
---|
722 | if (status & SR_ERR) {
|
---|
723 | fibril_mutex_unlock(&d->lock);
|
---|
724 | return EIO;
|
---|
725 | }
|
---|
726 |
|
---|
727 | fibril_mutex_unlock(&d->lock);
|
---|
728 |
|
---|
729 | return EOK;
|
---|
730 | }
|
---|
731 |
|
---|
732 | /** Issue ATAPI Inquiry.
|
---|
733 | *
|
---|
734 | * @param dev_idx Device index (0 or 1)
|
---|
735 | * @param obuf Buffer for storing inquiry data read from device
|
---|
736 | * @param obuf_size Size of obuf in bytes
|
---|
737 | *
|
---|
738 | * @return EOK on success, EIO on error.
|
---|
739 | */
|
---|
740 | static int ata_pcmd_inquiry(int dev_idx, void *obuf, size_t obuf_size)
|
---|
741 | {
|
---|
742 | ata_pcmd_inquiry_t cp;
|
---|
743 | int rc;
|
---|
744 |
|
---|
745 | memset(&cp, 0, sizeof(cp));
|
---|
746 |
|
---|
747 | cp.opcode = PCMD_INQUIRY;
|
---|
748 | cp.alloc_len = min(obuf_size, 0xff); /* Allocation length */
|
---|
749 |
|
---|
750 | rc = ata_cmd_packet(0, &cp, sizeof(cp), obuf, obuf_size);
|
---|
751 | if (rc != EOK)
|
---|
752 | return rc;
|
---|
753 |
|
---|
754 | return EOK;
|
---|
755 | }
|
---|
756 |
|
---|
757 | /** Issue ATAPI read(12) command.
|
---|
758 | *
|
---|
759 | * Output buffer must be large enough to hold the data, otherwise the
|
---|
760 | * function will fail.
|
---|
761 | *
|
---|
762 | * @param dev_idx Device index (0 or 1)
|
---|
763 | * @param ba Starting block address
|
---|
764 | * @param cnt Number of blocks to read
|
---|
765 | * @param obuf Buffer for storing inquiry data read from device
|
---|
766 | * @param obuf_size Size of obuf in bytes
|
---|
767 | *
|
---|
768 | * @return EOK on success, EIO on error.
|
---|
769 | */
|
---|
770 | static int ata_pcmd_read_12(int dev_idx, uint64_t ba, size_t cnt,
|
---|
771 | void *obuf, size_t obuf_size)
|
---|
772 | {
|
---|
773 | ata_pcmd_read_12_t cp;
|
---|
774 | int rc;
|
---|
775 |
|
---|
776 | if (ba > UINT32_MAX)
|
---|
777 | return EINVAL;
|
---|
778 |
|
---|
779 | memset(&cp, 0, sizeof(cp));
|
---|
780 |
|
---|
781 | cp.opcode = PCMD_READ_12;
|
---|
782 | cp.ba = host2uint32_t_be(ba);
|
---|
783 | cp.nblocks = host2uint32_t_be(cnt);
|
---|
784 |
|
---|
785 | rc = ata_cmd_packet(0, &cp, sizeof(cp), obuf, obuf_size);
|
---|
786 | if (rc != EOK)
|
---|
787 | return rc;
|
---|
788 |
|
---|
789 | return EOK;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /** Read a physical from the device.
|
---|
793 | *
|
---|
794 | * @param disk_id Device index (0 or 1)
|
---|
795 | * @param ba Address the first block.
|
---|
796 | * @param cnt Number of blocks to transfer.
|
---|
797 | * @param buf Buffer for holding the data.
|
---|
798 | *
|
---|
799 | * @return EOK on success, EIO on error.
|
---|
800 | */
|
---|
801 | static int ata_rcmd_read(int disk_id, uint64_t ba, size_t blk_cnt,
|
---|
802 | void *buf)
|
---|
803 | {
|
---|
804 | size_t i;
|
---|
805 | uint16_t data;
|
---|
806 | uint8_t status;
|
---|
807 | uint8_t drv_head;
|
---|
808 | disk_t *d;
|
---|
809 | block_coord_t bc;
|
---|
810 |
|
---|
811 | d = &disk[disk_id];
|
---|
812 |
|
---|
813 | /* Silence warning. */
|
---|
814 | memset(&bc, 0, sizeof(bc));
|
---|
815 |
|
---|
816 | /* Compute block coordinates. */
|
---|
817 | if (coord_calc(d, ba, &bc) != EOK)
|
---|
818 | return EINVAL;
|
---|
819 |
|
---|
820 | /* New value for Drive/Head register */
|
---|
821 | drv_head =
|
---|
822 | ((disk_id != 0) ? DHR_DRV : 0) |
|
---|
823 | ((d->amode != am_chs) ? DHR_LBA : 0) |
|
---|
824 | (bc.h & 0x0f);
|
---|
825 |
|
---|
826 | fibril_mutex_lock(&d->lock);
|
---|
827 |
|
---|
828 | /* Program a Read Sectors operation. */
|
---|
829 |
|
---|
830 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
|
---|
831 | fibril_mutex_unlock(&d->lock);
|
---|
832 | return EIO;
|
---|
833 | }
|
---|
834 |
|
---|
835 | pio_write_8(&cmd->drive_head, drv_head);
|
---|
836 |
|
---|
837 | if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
|
---|
838 | fibril_mutex_unlock(&d->lock);
|
---|
839 | return EIO;
|
---|
840 | }
|
---|
841 |
|
---|
842 | /* Program block coordinates into the device. */
|
---|
843 | coord_sc_program(&bc, 1);
|
---|
844 |
|
---|
845 | pio_write_8(&cmd->command, d->amode == am_lba48 ?
|
---|
846 | CMD_READ_SECTORS_EXT : CMD_READ_SECTORS);
|
---|
847 |
|
---|
848 | if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
|
---|
849 | fibril_mutex_unlock(&d->lock);
|
---|
850 | return EIO;
|
---|
851 | }
|
---|
852 |
|
---|
853 | if ((status & SR_DRQ) != 0) {
|
---|
854 | /* Read data from the device buffer. */
|
---|
855 |
|
---|
856 | for (i = 0; i < disk[disk_id].block_size / 2; i++) {
|
---|
857 | data = pio_read_16(&cmd->data_port);
|
---|
858 | ((uint16_t *) buf)[i] = data;
|
---|
859 | }
|
---|
860 | }
|
---|
861 |
|
---|
862 | if ((status & SR_ERR) != 0)
|
---|
863 | return EIO;
|
---|
864 |
|
---|
865 | fibril_mutex_unlock(&d->lock);
|
---|
866 | return EOK;
|
---|
867 | }
|
---|
868 |
|
---|
869 | /** Write a physical block to the device.
|
---|
870 | *
|
---|
871 | * @param disk_id Device index (0 or 1)
|
---|
872 | * @param ba Address of the first block.
|
---|
873 | * @param cnt Number of blocks to transfer.
|
---|
874 | * @param buf Buffer holding the data to write.
|
---|
875 | *
|
---|
876 | * @return EOK on success, EIO on error.
|
---|
877 | */
|
---|
878 | static int ata_rcmd_write(int disk_id, uint64_t ba, size_t cnt,
|
---|
879 | const void *buf)
|
---|
880 | {
|
---|
881 | size_t i;
|
---|
882 | uint8_t status;
|
---|
883 | uint8_t drv_head;
|
---|
884 | disk_t *d;
|
---|
885 | block_coord_t bc;
|
---|
886 |
|
---|
887 | d = &disk[disk_id];
|
---|
888 |
|
---|
889 | /* Silence warning. */
|
---|
890 | memset(&bc, 0, sizeof(bc));
|
---|
891 |
|
---|
892 | /* Compute block coordinates. */
|
---|
893 | if (coord_calc(d, ba, &bc) != EOK)
|
---|
894 | return EINVAL;
|
---|
895 |
|
---|
896 | /* New value for Drive/Head register */
|
---|
897 | drv_head =
|
---|
898 | ((disk_id != 0) ? DHR_DRV : 0) |
|
---|
899 | ((d->amode != am_chs) ? DHR_LBA : 0) |
|
---|
900 | (bc.h & 0x0f);
|
---|
901 |
|
---|
902 | fibril_mutex_lock(&d->lock);
|
---|
903 |
|
---|
904 | /* Program a Write Sectors operation. */
|
---|
905 |
|
---|
906 | if (wait_status(0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
|
---|
907 | fibril_mutex_unlock(&d->lock);
|
---|
908 | return EIO;
|
---|
909 | }
|
---|
910 |
|
---|
911 | pio_write_8(&cmd->drive_head, drv_head);
|
---|
912 |
|
---|
913 | if (wait_status(SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
|
---|
914 | fibril_mutex_unlock(&d->lock);
|
---|
915 | return EIO;
|
---|
916 | }
|
---|
917 |
|
---|
918 | /* Program block coordinates into the device. */
|
---|
919 | coord_sc_program(&bc, 1);
|
---|
920 |
|
---|
921 | pio_write_8(&cmd->command, d->amode == am_lba48 ?
|
---|
922 | CMD_WRITE_SECTORS_EXT : CMD_WRITE_SECTORS);
|
---|
923 |
|
---|
924 | if (wait_status(0, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
|
---|
925 | fibril_mutex_unlock(&d->lock);
|
---|
926 | return EIO;
|
---|
927 | }
|
---|
928 |
|
---|
929 | if ((status & SR_DRQ) != 0) {
|
---|
930 | /* Write data to the device buffer. */
|
---|
931 |
|
---|
932 | for (i = 0; i < disk[disk_id].block_size / 2; i++) {
|
---|
933 | pio_write_16(&cmd->data_port, ((uint16_t *) buf)[i]);
|
---|
934 | }
|
---|
935 | }
|
---|
936 |
|
---|
937 | fibril_mutex_unlock(&d->lock);
|
---|
938 |
|
---|
939 | if (status & SR_ERR)
|
---|
940 | return EIO;
|
---|
941 |
|
---|
942 | return EOK;
|
---|
943 | }
|
---|
944 |
|
---|
945 | /** Calculate block coordinates.
|
---|
946 | *
|
---|
947 | * Calculates block coordinates in the best coordinate system supported
|
---|
948 | * by the device. These can be later programmed into the device using
|
---|
949 | * @c coord_sc_program().
|
---|
950 | *
|
---|
951 | * @return EOK on success or EINVAL if block index is past end of device.
|
---|
952 | */
|
---|
953 | static int coord_calc(disk_t *d, uint64_t ba, block_coord_t *bc)
|
---|
954 | {
|
---|
955 | uint64_t c;
|
---|
956 | uint64_t idx;
|
---|
957 |
|
---|
958 | /* Check device bounds. */
|
---|
959 | if (ba >= d->blocks)
|
---|
960 | return EINVAL;
|
---|
961 |
|
---|
962 | bc->amode = d->amode;
|
---|
963 |
|
---|
964 | switch (d->amode) {
|
---|
965 | case am_chs:
|
---|
966 | /* Compute CHS coordinates. */
|
---|
967 | c = ba / (d->geom.heads * d->geom.sectors);
|
---|
968 | idx = ba % (d->geom.heads * d->geom.sectors);
|
---|
969 |
|
---|
970 | bc->cyl_lo = c & 0xff;
|
---|
971 | bc->cyl_hi = (c >> 8) & 0xff;
|
---|
972 | bc->h = (idx / d->geom.sectors) & 0x0f;
|
---|
973 | bc->sector = (1 + (idx % d->geom.sectors)) & 0xff;
|
---|
974 | break;
|
---|
975 |
|
---|
976 | case am_lba28:
|
---|
977 | /* Compute LBA-28 coordinates. */
|
---|
978 | bc->c0 = ba & 0xff; /* bits 0-7 */
|
---|
979 | bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
|
---|
980 | bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
|
---|
981 | bc->h = (ba >> 24) & 0x0f; /* bits 24-27 */
|
---|
982 | break;
|
---|
983 |
|
---|
984 | case am_lba48:
|
---|
985 | /* Compute LBA-48 coordinates. */
|
---|
986 | bc->c0 = ba & 0xff; /* bits 0-7 */
|
---|
987 | bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
|
---|
988 | bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
|
---|
989 | bc->c3 = (ba >> 24) & 0xff; /* bits 24-31 */
|
---|
990 | bc->c4 = (ba >> 32) & 0xff; /* bits 32-39 */
|
---|
991 | bc->c5 = (ba >> 40) & 0xff; /* bits 40-47 */
|
---|
992 | bc->h = 0;
|
---|
993 | break;
|
---|
994 | }
|
---|
995 |
|
---|
996 | return EOK;
|
---|
997 | }
|
---|
998 |
|
---|
999 | /** Program block coordinates and sector count into ATA registers.
|
---|
1000 | *
|
---|
1001 | * Note that bc->h must be programmed separately into the device/head register.
|
---|
1002 | */
|
---|
1003 | static void coord_sc_program(const block_coord_t *bc, uint16_t scnt)
|
---|
1004 | {
|
---|
1005 | if (bc->amode == am_lba48) {
|
---|
1006 | /* Write high-order bits. */
|
---|
1007 | pio_write_8(&cmd->sector_count, scnt >> 8);
|
---|
1008 | pio_write_8(&cmd->sector_number, bc->c3);
|
---|
1009 | pio_write_8(&cmd->cylinder_low, bc->c4);
|
---|
1010 | pio_write_8(&cmd->cylinder_high, bc->c5);
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | /* Write low-order bits. */
|
---|
1014 | pio_write_8(&cmd->sector_count, scnt & 0x00ff);
|
---|
1015 | pio_write_8(&cmd->sector_number, bc->c0);
|
---|
1016 | pio_write_8(&cmd->cylinder_low, bc->c1);
|
---|
1017 | pio_write_8(&cmd->cylinder_high, bc->c2);
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | /** Wait until some status bits are set and some are reset.
|
---|
1021 | *
|
---|
1022 | * Example: wait_status(SR_DRDY, ~SR_BSY) waits for SR_DRDY to become
|
---|
1023 | * set and SR_BSY to become reset.
|
---|
1024 | *
|
---|
1025 | * @param set Combination if bits which must be all set.
|
---|
1026 | * @param n_reset Negated combination of bits which must be all reset.
|
---|
1027 | * @param pstatus Pointer where to store last read status or NULL.
|
---|
1028 | * @param timeout Timeout in 10ms units.
|
---|
1029 | *
|
---|
1030 | * @return EOK on success, EIO on timeout.
|
---|
1031 | */
|
---|
1032 | static int wait_status(unsigned set, unsigned n_reset, uint8_t *pstatus,
|
---|
1033 | unsigned timeout)
|
---|
1034 | {
|
---|
1035 | uint8_t status;
|
---|
1036 | int cnt;
|
---|
1037 |
|
---|
1038 | status = pio_read_8(&cmd->status);
|
---|
1039 |
|
---|
1040 | /*
|
---|
1041 | * This is crude, yet simple. First try with 1us delays
|
---|
1042 | * (most likely the device will respond very fast). If not,
|
---|
1043 | * start trying every 10 ms.
|
---|
1044 | */
|
---|
1045 |
|
---|
1046 | cnt = 100;
|
---|
1047 | while ((status & ~n_reset) != 0 || (status & set) != set) {
|
---|
1048 | async_usleep(1);
|
---|
1049 | --cnt;
|
---|
1050 | if (cnt <= 0) break;
|
---|
1051 |
|
---|
1052 | status = pio_read_8(&cmd->status);
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | cnt = timeout;
|
---|
1056 | while ((status & ~n_reset) != 0 || (status & set) != set) {
|
---|
1057 | async_usleep(10000);
|
---|
1058 | --cnt;
|
---|
1059 | if (cnt <= 0) break;
|
---|
1060 |
|
---|
1061 | status = pio_read_8(&cmd->status);
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | if (pstatus)
|
---|
1065 | *pstatus = status;
|
---|
1066 |
|
---|
1067 | if (cnt == 0)
|
---|
1068 | return EIO;
|
---|
1069 |
|
---|
1070 | return EOK;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | /**
|
---|
1074 | * @}
|
---|
1075 | */
|
---|