source: mainline/uspace/lib/ata/src/ata.c@ 59c0f478

Last change on this file since 59c0f478 was 2791fbb7, checked in by Jiri Svoboda <jiri@…>, 15 months ago

Move generic ATA code out to libata

  • Property mode set to 100644
File size: 37.3 KB
Line 
1/*
2 * Copyright (c) 2024 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 libata
30 * @{
31 */
32
33/**
34 * @file
35 * @brief ATA disk driver library
36 *
37 * This driver library implements the generic part of ATA/ATAPI. It is
38 * meant to be used by a driver which implements the actual transport
39 * (such as ISA, PCI).
40 *
41 * This driver libary supports CHS, 28-bit and 48-bit LBA addressing,
42 * as well as PACKET devices. It supports PIO transfers and IRQ.
43 *
44 * There is no support DMA, S.M.A.R.T or removable devices.
45 *
46 * This driver is based on the ATA-1, ATA-2, ATA-3 and ATA/ATAPI-4 through 7
47 * standards, as published by the ANSI, NCITS and INCITS standards bodies,
48 * which are freely available. This driver contains no vendor-specific
49 * code at this moment.
50 */
51
52#include <bd_srv.h>
53#include <byteorder.h>
54#include <errno.h>
55#include <macros.h>
56#include <scsi/mmc.h>
57#include <scsi/sbc.h>
58#include <scsi/spc.h>
59#include <stdarg.h>
60#include <stdbool.h>
61#include <stddef.h>
62#include <stdint.h>
63#include <stdio.h>
64#include <stdlib.h>
65
66#include "ata/ata.h"
67#include "ata/ata_hw.h"
68
69#define MSG_BUF_SIZE 256
70
71/**
72 * Size of data returned from Identify Device or Identify Packet Device
73 * command.
74 */
75static const size_t identify_data_size = 512;
76static void ata_msg_note(ata_channel_t *, const char *, ...);
77static void ata_msg_debug(ata_channel_t *, const char *, ...);
78static void ata_msg_warn(ata_channel_t *, const char *, ...);
79static void ata_msg_error(ata_channel_t *, const char *, ...);
80static errno_t ata_device_add(ata_device_t *);
81static errno_t ata_device_remove(ata_device_t *);
82static void ata_read_data_16(ata_channel_t *, uint16_t *, size_t);
83static void ata_write_data_16(ata_channel_t *, uint16_t *, size_t);
84static uint8_t ata_read_cmd_8(ata_channel_t *, uint16_t);
85static void ata_write_cmd_8(ata_channel_t *, uint16_t, uint8_t);
86
87static errno_t ata_bd_init_irq(ata_channel_t *);
88static void ata_bd_fini_irq(ata_channel_t *);
89
90static errno_t ata_bd_open(bd_srvs_t *, bd_srv_t *);
91static errno_t ata_bd_close(bd_srv_t *);
92static errno_t ata_bd_read_blocks(bd_srv_t *, uint64_t, size_t, void *, size_t);
93static errno_t ata_bd_read_toc(bd_srv_t *, uint8_t, void *, size_t);
94static errno_t ata_bd_write_blocks(bd_srv_t *, uint64_t, size_t, const void *,
95 size_t);
96static errno_t ata_bd_get_block_size(bd_srv_t *, size_t *);
97static errno_t ata_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
98static errno_t ata_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
99
100static errno_t ata_rcmd_read(ata_device_t *, uint64_t, size_t, void *);
101static errno_t ata_rcmd_write(ata_device_t *, uint64_t, size_t,
102 const void *);
103static errno_t ata_rcmd_flush_cache(ata_device_t *);
104static errno_t ata_device_init(ata_channel_t *, ata_device_t *, int);
105static errno_t ata_identify_dev(ata_device_t *, void *);
106static errno_t ata_identify_pkt_dev(ata_device_t *, void *);
107static errno_t ata_cmd_packet(ata_device_t *, const void *, size_t, void *,
108 size_t, size_t *);
109static errno_t ata_pcmd_inquiry(ata_device_t *, void *, size_t, size_t *);
110static errno_t ata_pcmd_read_12(ata_device_t *, uint64_t, size_t, void *, size_t);
111static errno_t ata_pcmd_read_capacity(ata_device_t *, uint64_t *, size_t *);
112static errno_t ata_pcmd_read_toc(ata_device_t *, uint8_t, void *, size_t);
113static void disk_print_summary(ata_device_t *);
114static size_t ata_disk_maxnb(ata_device_t *);
115static errno_t coord_calc(ata_device_t *, uint64_t, block_coord_t *);
116static void coord_sc_program(ata_channel_t *, const block_coord_t *, uint16_t);
117static errno_t wait_status(ata_channel_t *, unsigned, unsigned, uint8_t *,
118 unsigned);
119static errno_t wait_irq(ata_channel_t *, uint8_t *, unsigned);
120
121static bd_ops_t ata_bd_ops = {
122 .open = ata_bd_open,
123 .close = ata_bd_close,
124 .read_blocks = ata_bd_read_blocks,
125 .read_toc = ata_bd_read_toc,
126 .write_blocks = ata_bd_write_blocks,
127 .get_block_size = ata_bd_get_block_size,
128 .get_num_blocks = ata_bd_get_num_blocks,
129 .sync_cache = ata_bd_sync_cache
130};
131
132static ata_device_t *bd_srv_device(bd_srv_t *bd)
133{
134 return (ata_device_t *)bd->srvs->sarg;
135}
136
137static int disk_dev_idx(ata_device_t *device)
138{
139 return (device->device_id & 1);
140}
141
142/** Create ATA channel.
143 *
144 * @param params Channel creation parameters
145 * @param rchan Place to store pointer to new channel
146 * @return EOK on success or an error code
147 */
148errno_t ata_channel_create(ata_params_t *params, ata_channel_t **rchan)
149{
150 ata_channel_t *chan;
151
152 chan = calloc(1, sizeof(ata_channel_t));
153 if (chan == NULL)
154 return ENOMEM;
155
156 chan->params = *params;
157 ata_msg_debug(chan, "ata_channel_create()");
158
159 fibril_mutex_initialize(&chan->lock);
160 fibril_mutex_initialize(&chan->irq_lock);
161 fibril_condvar_initialize(&chan->irq_cv);
162
163 *rchan = chan;
164 return EOK;
165}
166
167/** Initialize ATA channel.
168 *
169 * @param params Channel creation parameters
170 * @param rchan Place to store pointer to new channel
171 * @return EOK on success or an error code
172 */
173errno_t ata_channel_initialize(ata_channel_t *chan)
174{
175 int i;
176 errno_t rc;
177 int n_disks;
178 bool irq_inited = false;
179
180 ata_msg_debug(chan, "ata_channel_initialize()");
181
182 rc = ata_bd_init_irq(chan);
183 if (rc != EOK)
184 return rc;
185
186 irq_inited = true;
187
188 for (i = 0; i < MAX_DEVICES; i++) {
189 ata_msg_debug(chan, "Identify drive %d...", i);
190
191 rc = ata_device_init(chan, &chan->device[i], i);
192
193 if (rc == EOK) {
194 disk_print_summary(&chan->device[i]);
195 } else {
196 ata_msg_debug(chan, "Not found.");
197 }
198 }
199
200 n_disks = 0;
201
202 for (i = 0; i < MAX_DEVICES; i++) {
203 /* Skip unattached devices. */
204 if (chan->device[i].present == false)
205 continue;
206
207 rc = ata_device_add(&chan->device[i]);
208 if (rc != EOK) {
209 ata_msg_error(chan, "Unable to add device %d.", i);
210 goto error;
211 }
212 ++n_disks;
213 }
214
215 if (n_disks == 0) {
216 ata_msg_warn(chan, "No devices detected.");
217 rc = ENOENT;
218 goto error;
219 }
220
221 return EOK;
222error:
223 for (i = 0; i < MAX_DEVICES; i++) {
224 if (ata_device_add(&chan->device[i]) != EOK) {
225 ata_msg_error(chan, "Unable to remove device %d.", i);
226 }
227 }
228 if (irq_inited)
229 ata_bd_fini_irq(chan);
230 return rc;
231}
232
233/** Destroy ATA channel. */
234errno_t ata_channel_destroy(ata_channel_t *chan)
235{
236 int i;
237 errno_t rc;
238
239 ata_msg_debug(chan, ": ata_channel_destroy()");
240
241 fibril_mutex_lock(&chan->lock);
242
243 for (i = 0; i < MAX_DEVICES; i++) {
244 rc = ata_device_remove(&chan->device[i]);
245 if (rc != EOK) {
246 ata_msg_error(chan, "Unable to remove device %d.", i);
247 return rc;
248 }
249 }
250
251 ata_bd_fini_irq(chan);
252 fibril_mutex_unlock(&chan->lock);
253
254 return EOK;
255}
256
257/** Add ATA device.
258 *
259 * @param d Device
260 * @return EOK on success or an error code
261 */
262static errno_t ata_device_add(ata_device_t *d)
263{
264 bd_srvs_init(&d->bds);
265 d->bds.ops = &ata_bd_ops;
266 d->bds.sarg = (void *)d;
267
268 return d->chan->params.add_device(d->chan->params.arg, d->device_id,
269 (void *)d);
270}
271
272/** Remove ATA device.
273 *
274 * @param d Device
275 * @return EOK on success or an error code
276 */
277static errno_t ata_device_remove(ata_device_t *d)
278{
279 return d->chan->params.remove_device(d->chan->params.arg, d->device_id);
280}
281
282/** Read 16 bits from data port.
283 *
284 * @param chan ATA channel
285 * @param buf Buffer to hold data
286 * @param nwords Number of words to read
287 */
288static void ata_read_data_16(ata_channel_t *chan, uint16_t *buf,
289 size_t nwords)
290{
291 chan->params.read_data_16(chan->params.arg, buf, nwords);
292}
293
294/** Write 16 bits to data port.
295 *
296 * @param chan ATA channel
297 * @param data Data
298 * @param nwords Number of words to write
299 */
300static void ata_write_data_16(ata_channel_t *chan, uint16_t *data,
301 size_t nwords)
302{
303 chan->params.write_data_16(chan->params.arg, data, nwords);
304}
305
306/** Read 8 bits from 8-bit command port.
307 *
308 * @param chan ATA channel
309 * @param port Port number
310 * @return 8-bit register value
311 */
312static uint8_t ata_read_cmd_8(ata_channel_t *chan, uint16_t port)
313{
314 return chan->params.read_cmd_8(chan->params.arg, port);
315}
316
317/** Write 8 bits to 8-bit command port.
318 *
319 * @param chan ATA channel
320 * @param port Port number
321 * @param value Register value
322 */
323static void ata_write_cmd_8(ata_channel_t *chan, uint16_t port, uint8_t value)
324{
325 return chan->params.write_cmd_8(chan->params.arg, port, value);
326}
327
328/** Log a notice message.
329 *
330 * @param chan Channel
331 * @param fmt Format
332 */
333static void ata_msg_note(ata_channel_t *chan, const char *fmt, ...)
334{
335 va_list ap;
336 char buf[MSG_BUF_SIZE];
337
338 va_start(ap, fmt);
339 vsnprintf(buf, sizeof(buf), fmt, ap);
340 va_end(ap);
341
342 chan->params.msg_note(chan->params.arg, buf);
343}
344
345/** Log a debug message.
346 *
347 * @param chan Channel
348 * @param fmt Format
349 */
350static void ata_msg_debug(ata_channel_t *chan, const char *fmt, ...)
351{
352 va_list ap;
353 char buf[MSG_BUF_SIZE];
354
355 va_start(ap, fmt);
356 vsnprintf(buf, sizeof(buf), fmt, ap);
357 va_end(ap);
358
359 chan->params.msg_debug(chan->params.arg, buf);
360}
361
362/** Log a warning message.
363 *
364 * @param chan Channel
365 * @param fmt Format
366 */
367static void ata_msg_warn(ata_channel_t *chan, const char *fmt, ...)
368{
369 va_list ap;
370 char buf[MSG_BUF_SIZE];
371
372 va_start(ap, fmt);
373 vsnprintf(buf, sizeof(buf), fmt, ap);
374 va_end(ap);
375
376 chan->params.msg_warn(chan->params.arg, buf);
377}
378
379/** Log an error message.
380 *
381 * @param chan Channel
382 * @param fmt Format
383 */
384static void ata_msg_error(ata_channel_t *chan, const char *fmt, ...)
385{
386 va_list ap;
387 char buf[MSG_BUF_SIZE];
388
389 va_start(ap, fmt);
390 vsnprintf(buf, sizeof(buf), fmt, ap);
391 va_end(ap);
392
393 chan->params.msg_error(chan->params.arg, buf);
394}
395
396/** Print one-line device summary. */
397static void disk_print_summary(ata_device_t *d)
398{
399 uint64_t mbytes;
400 char *atype = NULL;
401 char *cap = NULL;
402 int rc;
403
404 if (d->dev_type == ata_reg_dev) {
405 switch (d->amode) {
406 case am_chs:
407 rc = asprintf(&atype, "CHS %u cylinders, %u heads, "
408 "%u sectors", d->geom.cylinders, d->geom.heads,
409 d->geom.sectors);
410 if (rc < 0) {
411 /* Out of memory */
412 atype = NULL;
413 }
414 break;
415 case am_lba28:
416 atype = str_dup("LBA-28");
417 break;
418 case am_lba48:
419 atype = str_dup("LBA-48");
420 break;
421 }
422 } else {
423 atype = str_dup("PACKET");
424 }
425
426 if (atype == NULL)
427 return;
428
429 mbytes = d->blocks / (2 * 1024);
430 if (mbytes > 0) {
431 rc = asprintf(&cap, " %" PRIu64 " MB.", mbytes);
432 if (rc < 0) {
433 cap = NULL;
434 goto cleanup;
435 }
436 }
437
438 ata_msg_note(d->chan, "%s: %s %" PRIu64 " blocks%s", d->model, atype,
439 d->blocks, cap);
440cleanup:
441 free(atype);
442 free(cap);
443}
444
445/** Initialize IRQ. */
446static errno_t ata_bd_init_irq(ata_channel_t *chan)
447{
448 return chan->params.irq_enable(chan->params.arg);
449}
450
451/** Clean up IRQ. */
452static void ata_bd_fini_irq(ata_channel_t *chan)
453{
454 (void)chan->params.irq_disable(chan->params.arg);
455}
456
457/** Initialize a device.
458 *
459 * Probes for a device, determines its parameters and initializes
460 * the device structure.
461 */
462static errno_t ata_device_init(ata_channel_t *chan, ata_device_t *d,
463 int device_id)
464{
465 identify_data_t idata;
466 uint8_t model[40];
467 scsi_std_inquiry_data_t inq_data;
468 size_t isize;
469 uint16_t w;
470 uint8_t c;
471 uint16_t bc;
472 uint64_t nblocks;
473 size_t block_size;
474 size_t pos, len;
475 errno_t rc;
476 unsigned i;
477
478 d->chan = chan;
479 d->device_id = device_id;
480 d->present = false;
481
482 /* Try identify command. */
483 rc = ata_identify_dev(d, &idata);
484 if (rc == EOK) {
485 /* Success. It's a register (non-packet) device. */
486 ata_msg_debug(chan, "ATA register-only device found.");
487 d->dev_type = ata_reg_dev;
488 } else if (rc == EIO) {
489 /*
490 * There is something, but not a register device. Check to see
491 * whether the IDENTIFY command left the packet signature in
492 * the registers in case this is a packet device.
493 *
494 * According to the ATA specification, the LBA low and
495 * interrupt reason registers should be set to 0x01. However,
496 * there are many devices that do not follow this and only set
497 * the byte count registers. So, only check these.
498 */
499 bc = ((uint16_t)ata_read_cmd_8(chan, REG_CYLINDER_HIGH) << 8) |
500 ata_read_cmd_8(chan, REG_CYLINDER_LOW);
501
502 if (bc == PDEV_SIGNATURE_BC) {
503 rc = ata_identify_pkt_dev(d, &idata);
504 if (rc == EOK) {
505 /* We have a packet device. */
506 d->dev_type = ata_pkt_dev;
507 } else {
508 return EIO;
509 }
510 } else {
511 /* Nope. Something's there, but not recognized. */
512 return EIO;
513 }
514 } else {
515 /* Operation timed out. That means there is no device there. */
516 return EIO;
517 }
518
519 if (d->dev_type == ata_pkt_dev) {
520 /* Packet device */
521 d->amode = 0;
522
523 d->geom.cylinders = 0;
524 d->geom.heads = 0;
525 d->geom.sectors = 0;
526
527 d->blocks = 0;
528 } else if ((idata.caps & rd_cap_lba) == 0) {
529 /* Device only supports CHS addressing. */
530 d->amode = am_chs;
531
532 d->geom.cylinders = idata.cylinders;
533 d->geom.heads = idata.heads;
534 d->geom.sectors = idata.sectors;
535
536 d->blocks = d->geom.cylinders * d->geom.heads * d->geom.sectors;
537 } else if ((idata.cmd_set1 & cs1_addr48) == 0) {
538 /* Device only supports LBA-28 addressing. */
539 d->amode = am_lba28;
540
541 d->geom.cylinders = 0;
542 d->geom.heads = 0;
543 d->geom.sectors = 0;
544
545 d->blocks =
546 (uint32_t) idata.total_lba28_0 |
547 ((uint32_t) idata.total_lba28_1 << 16);
548 } else {
549 /* Device supports LBA-48 addressing. */
550 d->amode = am_lba48;
551
552 d->geom.cylinders = 0;
553 d->geom.heads = 0;
554 d->geom.sectors = 0;
555
556 d->blocks =
557 (uint64_t) idata.total_lba48_0 |
558 ((uint64_t) idata.total_lba48_1 << 16) |
559 ((uint64_t) idata.total_lba48_2 << 32) |
560 ((uint64_t) idata.total_lba48_3 << 48);
561 }
562
563 /*
564 * Convert model name to string representation.
565 */
566 for (i = 0; i < 20; i++) {
567 w = idata.model_name[i];
568 model[2 * i] = w >> 8;
569 model[2 * i + 1] = w & 0x00ff;
570 }
571
572 len = 40;
573 while (len > 0 && model[len - 1] == 0x20)
574 --len;
575
576 pos = 0;
577 for (i = 0; i < len; ++i) {
578 c = model[i];
579 if (c >= 0x80)
580 c = '?';
581
582 chr_encode(c, d->model, &pos, 40);
583 }
584 d->model[pos] = '\0';
585
586 if (d->dev_type == ata_pkt_dev) {
587 /* Send inquiry. */
588 rc = ata_pcmd_inquiry(d, &inq_data, sizeof(inq_data), &isize);
589 if (rc != EOK || isize < sizeof(inq_data)) {
590 ata_msg_error(chan, "Device inquiry failed.");
591 d->present = false;
592 return EIO;
593 }
594
595 /* Check device type. */
596 if (INQUIRY_PDEV_TYPE(inq_data.pqual_devtype) != SCSI_DEV_CD_DVD)
597 ata_msg_warn(chan, "Peripheral device type is not CD-ROM.");
598
599 rc = ata_pcmd_read_capacity(d, &nblocks, &block_size);
600 if (rc != EOK) {
601 ata_msg_error(chan, "Read capacity command failed.");
602 d->present = false;
603 return EIO;
604 }
605
606 d->blocks = nblocks;
607 d->block_size = block_size;
608 } else {
609 /* Assume register Read always uses 512-byte blocks. */
610 d->block_size = 512;
611 }
612
613 d->present = true;
614 return EOK;
615}
616
617static errno_t ata_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
618{
619 return EOK;
620}
621
622static errno_t ata_bd_close(bd_srv_t *bd)
623{
624 return EOK;
625}
626
627/** Read multiple blocks from the device. */
628static errno_t ata_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
629 void *buf, size_t size)
630{
631 ata_device_t *device = bd_srv_device(bd);
632 size_t maxnb;
633 size_t nb;
634 errno_t rc;
635
636 if (size < cnt * device->block_size) {
637 rc = EINVAL;
638 goto error;
639 }
640
641 /* Maximum number of blocks to transfer at the same time */
642 maxnb = ata_disk_maxnb(device);
643 while (cnt > 0) {
644 nb = min(maxnb, cnt);
645 if (device->dev_type == ata_reg_dev) {
646 rc = ata_rcmd_read(device, ba, nb, buf);
647 } else {
648 rc = ata_pcmd_read_12(device, ba, nb, buf,
649 device->block_size);
650 }
651
652 if (rc != EOK)
653 goto error;
654
655 ba += nb;
656 cnt -= nb;
657 buf += device->block_size * nb;
658 }
659
660 return EOK;
661error:
662 ata_msg_debug(device->chan, "ata_bd_read_blocks: rc=%d", rc);
663 return rc;
664}
665
666/** Read TOC from device. */
667static errno_t ata_bd_read_toc(bd_srv_t *bd, uint8_t session, void *buf, size_t size)
668{
669 ata_device_t *device = bd_srv_device(bd);
670
671 return ata_pcmd_read_toc(device, session, buf, size);
672}
673
674/** Write multiple blocks to the device. */
675static errno_t ata_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
676 const void *buf, size_t size)
677{
678 ata_device_t *device = bd_srv_device(bd);
679 size_t maxnb;
680 size_t nb;
681 errno_t rc;
682
683 if (device->dev_type != ata_reg_dev)
684 return ENOTSUP;
685
686 if (size < cnt * device->block_size)
687 return EINVAL;
688
689 /* Maximum number of blocks to transfer at the same time */
690 maxnb = ata_disk_maxnb(device);
691 while (cnt > 0) {
692 nb = min(maxnb, cnt);
693 rc = ata_rcmd_write(device, ba, nb, buf);
694 if (rc != EOK)
695 return rc;
696
697 ba += nb;
698 cnt -= nb;
699 buf += device->block_size * nb;
700 }
701
702 return EOK;
703}
704
705/** Get device block size. */
706static errno_t ata_bd_get_block_size(bd_srv_t *bd, size_t *rbsize)
707{
708 ata_device_t *device = bd_srv_device(bd);
709
710 *rbsize = device->block_size;
711 return EOK;
712}
713
714/** Get device number of blocks. */
715static errno_t ata_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
716{
717 ata_device_t *device = bd_srv_device(bd);
718
719 *rnb = device->blocks;
720 return EOK;
721}
722
723/** Flush cache. */
724static errno_t ata_bd_sync_cache(bd_srv_t *bd, uint64_t ba, size_t cnt)
725{
726 ata_device_t *device = bd_srv_device(bd);
727
728 /* ATA cannot flush just some blocks, we just flush everything. */
729 (void)ba;
730 (void)cnt;
731
732 return ata_rcmd_flush_cache(device);
733}
734
735/** PIO data-in command protocol. */
736static errno_t ata_pio_data_in(ata_device_t *device, void *obuf, size_t obuf_size,
737 size_t blk_size, size_t nblocks)
738{
739 ata_channel_t *chan = device->chan;
740 uint8_t status;
741 errno_t rc;
742
743 assert(nblocks > 0);
744 assert(blk_size % 2 == 0);
745
746 while (nblocks > 0) {
747 if (chan->params.have_irq)
748 rc = wait_irq(chan, &status, TIMEOUT_BSY);
749 else
750 rc = wait_status(chan, 0, ~SR_BSY, &status, TIMEOUT_BSY);
751
752 if (rc != EOK) {
753 ata_msg_debug(chan, "wait_irq/wait_status failed");
754 return EIO;
755 }
756
757 if ((status & SR_DRQ) == 0) {
758 ata_msg_debug(chan, "DRQ == 0");
759 break;
760 }
761
762 /* Read data from the device buffer. */
763 ata_read_data_16(chan, (uint16_t *)obuf, blk_size / 2);
764 obuf += blk_size;
765
766 --nblocks;
767 }
768
769 if ((status & SR_ERR) != 0) {
770 ata_msg_debug(chan, "status & SR_ERR != 0");
771 return EIO;
772 }
773 if (nblocks > 0) {
774 ata_msg_debug(chan, "remaining nblocks = %zu", nblocks);
775 return EIO;
776 }
777
778 return EOK;
779}
780
781/** PIO data-out command protocol. */
782static errno_t ata_pio_data_out(ata_device_t *device, const void *buf, size_t buf_size,
783 size_t blk_size, size_t nblocks)
784{
785 ata_channel_t *chan = device->chan;
786 uint8_t status;
787 errno_t rc;
788
789 assert(nblocks > 0);
790 assert(blk_size % 2 == 0);
791
792 rc = wait_status(chan, 0, ~SR_BSY, &status, TIMEOUT_BSY);
793 if (rc != EOK)
794 return EIO;
795
796 while (nblocks > 0) {
797 if ((status & SR_DRQ) == 0) {
798 ata_msg_debug(chan, "pio_data_out: unexpected DRQ=0");
799 break;
800 }
801
802 /* Write data to the device buffer. */
803 ata_write_data_16(chan, (uint16_t *)buf, blk_size / 2);
804 buf += blk_size;
805
806 if (chan->params.have_irq)
807 rc = wait_irq(chan, &status, TIMEOUT_BSY);
808 else
809 rc = wait_status(chan, 0, ~SR_BSY, &status, TIMEOUT_BSY);
810 if (rc != EOK)
811 return EIO;
812
813 --nblocks;
814 }
815
816 if (status & SR_ERR)
817 return EIO;
818 if (nblocks > 0)
819 return EIO;
820
821 return EOK;
822}
823
824/** PIO non-data command protocol. */
825static errno_t ata_pio_nondata(ata_device_t *device)
826{
827 ata_channel_t *chan = device->chan;
828 uint8_t status;
829 errno_t rc;
830
831 if (chan->params.have_irq)
832 rc = wait_irq(chan, &status, TIMEOUT_BSY);
833 else
834 rc = wait_status(chan, 0, ~SR_BSY, &status, TIMEOUT_BSY);
835
836 if (rc != EOK)
837 return EIO;
838
839 if (status & SR_ERR)
840 return EIO;
841
842 return EOK;
843}
844
845/** Issue IDENTIFY DEVICE command.
846 *
847 * Reads @c identify data into the provided buffer. This is used to detect
848 * whether an ATA device is present and if so, to determine its parameters.
849 *
850 * @param device Device
851 * @param buf Pointer to a 512-byte buffer.
852 *
853 * @return ETIMEOUT on timeout (this can mean the device is
854 * not present). EIO if device responds with error.
855 */
856static errno_t ata_identify_dev(ata_device_t *device, void *buf)
857{
858 ata_channel_t *chan = device->chan;
859 uint8_t status;
860 uint8_t drv_head;
861
862 drv_head = ((disk_dev_idx(device) != 0) ? DHR_DRV : 0);
863
864 if (wait_status(chan, 0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
865 return ETIMEOUT;
866
867 ata_write_cmd_8(chan, REG_DRIVE_HEAD, drv_head);
868
869 /*
870 * Do not wait for DRDY to be set in case this is a packet device.
871 * We determine whether the device is present by waiting for DRQ to be
872 * set after issuing the command.
873 */
874 if (wait_status(chan, 0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
875 return ETIMEOUT;
876
877 ata_write_cmd_8(chan, REG_COMMAND, CMD_IDENTIFY_DRIVE);
878
879 /*
880 * For probing purposes we need to wait for some status bit to become
881 * active - otherwise we could be fooled just by receiving all zeroes.
882 */
883 if (wait_status(chan, SR_DRQ, ~SR_BSY, &status, TIMEOUT_PROBE) != EOK) {
884 if ((status & SR_ERR) == 0) {
885 /* Probably no device at all */
886 return ETIMEOUT;
887 }
888 }
889
890 return ata_pio_data_in(device, buf, identify_data_size,
891 identify_data_size, 1);
892}
893
894/** Issue Identify Packet Device command.
895 *
896 * Reads @c identify data into the provided buffer. This is used to detect
897 * whether an ATAPI device is present and if so, to determine its parameters.
898 *
899 * @param device Device
900 * @param buf Pointer to a 512-byte buffer.
901 */
902static errno_t ata_identify_pkt_dev(ata_device_t *device, void *buf)
903{
904 ata_channel_t *chan = device->chan;
905 uint8_t drv_head;
906
907 drv_head = ((disk_dev_idx(device) != 0) ? DHR_DRV : 0);
908
909 if (wait_status(chan, 0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
910 return EIO;
911
912 ata_write_cmd_8(chan, REG_DRIVE_HEAD, drv_head);
913
914 /* For ATAPI commands we do not need to wait for DRDY. */
915 if (wait_status(chan, 0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK)
916 return EIO;
917
918 ata_write_cmd_8(chan, REG_COMMAND, CMD_IDENTIFY_PKT_DEV);
919
920 return ata_pio_data_in(device, buf, identify_data_size,
921 identify_data_size, 1);
922}
923
924/** Issue packet command (i. e. write a command packet to the device).
925 *
926 * Only data-in commands are supported (e.g. inquiry, read).
927 *
928 * @param device Device
929 * @param obuf Buffer for storing data read from device
930 * @param obuf_size Size of obuf in bytes
931 * @param rcvd_size Place to store number of bytes read or @c NULL
932 *
933 * @return EOK on success, EIO on error.
934 */
935static errno_t ata_cmd_packet(ata_device_t *device, const void *cpkt, size_t cpkt_size,
936 void *obuf, size_t obuf_size, size_t *rcvd_size)
937{
938 ata_channel_t *chan = device->chan;
939 uint8_t status;
940 uint8_t drv_head;
941 size_t data_size;
942 size_t remain;
943 errno_t rc;
944
945 fibril_mutex_lock(&chan->lock);
946
947 /* New value for Drive/Head register */
948 drv_head =
949 ((disk_dev_idx(device) != 0) ? DHR_DRV : 0);
950
951 if (wait_status(chan, 0, ~SR_BSY, NULL, TIMEOUT_PROBE) != EOK) {
952 fibril_mutex_unlock(&chan->lock);
953 return EIO;
954 }
955
956 ata_write_cmd_8(chan, REG_DRIVE_HEAD, drv_head);
957
958 if (wait_status(chan, 0, ~(SR_BSY | SR_DRQ), NULL, TIMEOUT_BSY) != EOK) {
959 fibril_mutex_unlock(&chan->lock);
960 return EIO;
961 }
962
963 /* Byte count <- max. number of bytes we can read in one transfer. */
964 ata_write_cmd_8(chan, REG_CYLINDER_LOW, 0xfe);
965 ata_write_cmd_8(chan, REG_CYLINDER_HIGH, 0xff);
966
967 ata_write_cmd_8(chan, REG_COMMAND, CMD_PACKET);
968
969 if (wait_status(chan, SR_DRQ, ~SR_BSY, &status, TIMEOUT_BSY) != EOK) {
970 fibril_mutex_unlock(&chan->lock);
971 return EIO;
972 }
973
974 /* Write command packet. */
975 ata_write_data_16(chan, ((uint16_t *) cpkt), (cpkt_size + 1) / 2);
976
977 remain = obuf_size;
978 while (remain > 0) {
979 if (chan->params.have_irq)
980 rc = wait_irq(chan, &status, TIMEOUT_BSY);
981 else
982 rc = wait_status(chan, 0, ~SR_BSY, &status, TIMEOUT_BSY);
983
984 if (rc != EOK) {
985 fibril_mutex_unlock(&chan->lock);
986 return EIO;
987 }
988
989 if ((status & SR_DRQ) == 0)
990 break;
991
992 /* Read byte count. */
993 data_size = (uint16_t) ata_read_cmd_8(chan, REG_CYLINDER_LOW) +
994 ((uint16_t) ata_read_cmd_8(chan, REG_CYLINDER_HIGH) << 8);
995
996 /* Check whether data fits into output buffer. */
997 if (data_size > obuf_size) {
998 /* Output buffer is too small to store data. */
999 fibril_mutex_unlock(&chan->lock);
1000 return EIO;
1001 }
1002
1003 /* Read data from the device buffer. */
1004 ata_read_data_16(chan, obuf, (data_size + 1) / 2);
1005 obuf += data_size;
1006
1007 remain -= data_size;
1008 }
1009
1010 if (chan->params.have_irq)
1011 rc = wait_irq(chan, &status, TIMEOUT_BSY);
1012 else
1013 rc = wait_status(chan, 0, ~SR_BSY, &status, TIMEOUT_BSY);
1014
1015 fibril_mutex_unlock(&chan->lock);
1016
1017 if (status & SR_ERR)
1018 return EIO;
1019
1020 if (rcvd_size != NULL)
1021 *rcvd_size = obuf_size - remain;
1022 return EOK;
1023}
1024
1025/** Issue ATAPI Inquiry.
1026 *
1027 * @param device Device
1028 * @param obuf Buffer for storing inquiry data read from device
1029 * @param obuf_size Size of obuf in bytes
1030 *
1031 * @return EOK on success, EIO on error.
1032 */
1033static errno_t ata_pcmd_inquiry(ata_device_t *device, void *obuf, size_t obuf_size,
1034 size_t *rcvd_size)
1035{
1036 uint8_t cpb[12];
1037 scsi_cdb_inquiry_t *cp = (scsi_cdb_inquiry_t *)cpb;
1038 errno_t rc;
1039
1040 memset(cpb, 0, sizeof(cpb));
1041
1042 /*
1043 * For SFF 8020 compliance the inquiry must be padded to 12 bytes
1044 * and allocation length must fit in one byte.
1045 */
1046 cp->op_code = SCSI_CMD_INQUIRY;
1047
1048 /* Allocation length */
1049 cp->alloc_len = host2uint16_t_be(min(obuf_size, 0xff));
1050
1051 rc = ata_cmd_packet(device, cpb, sizeof(cpb), obuf, obuf_size, rcvd_size);
1052 if (rc != EOK)
1053 return rc;
1054
1055 return EOK;
1056}
1057
1058/** Issue ATAPI read capacity(10) command.
1059 *
1060 * @param device Device
1061 * @param nblocks Place to store number of blocks
1062 * @param block_size Place to store block size
1063 *
1064 * @return EOK on success, EIO on error.
1065 */
1066static errno_t ata_pcmd_read_capacity(ata_device_t *device, uint64_t *nblocks,
1067 size_t *block_size)
1068{
1069 scsi_cdb_read_capacity_10_t cdb;
1070 scsi_read_capacity_10_data_t data;
1071 size_t rsize;
1072 errno_t rc;
1073
1074 memset(&cdb, 0, sizeof(cdb));
1075 cdb.op_code = SCSI_CMD_READ_CAPACITY_10;
1076
1077 rc = ata_cmd_packet(device, &cdb, sizeof(cdb), &data, sizeof(data), &rsize);
1078 if (rc != EOK)
1079 return rc;
1080
1081 if (rsize != sizeof(data))
1082 return EIO;
1083
1084 *nblocks = uint32_t_be2host(data.last_lba) + 1;
1085 *block_size = uint32_t_be2host(data.block_size);
1086
1087 return EOK;
1088}
1089
1090/** Issue ATAPI read(12) command.
1091 *
1092 * Output buffer must be large enough to hold the data, otherwise the
1093 * function will fail.
1094 *
1095 * @param device Device
1096 * @param ba Starting block address
1097 * @param cnt Number of blocks to read
1098 * @param obuf Buffer for storing inquiry data read from device
1099 * @param obuf_size Size of obuf in bytes
1100 *
1101 * @return EOK on success, EIO on error.
1102 */
1103static errno_t ata_pcmd_read_12(ata_device_t *device, uint64_t ba, size_t cnt,
1104 void *obuf, size_t obuf_size)
1105{
1106 scsi_cdb_read_12_t cp;
1107 errno_t rc;
1108
1109 if (ba > UINT32_MAX)
1110 return EINVAL;
1111
1112 memset(&cp, 0, sizeof(cp));
1113
1114 cp.op_code = SCSI_CMD_READ_12;
1115 cp.lba = host2uint32_t_be(ba);
1116 cp.xfer_len = host2uint32_t_be(cnt);
1117
1118 rc = ata_cmd_packet(device, &cp, sizeof(cp), obuf, obuf_size, NULL);
1119 if (rc != EOK)
1120 return rc;
1121
1122 return EOK;
1123}
1124
1125/** Issue ATAPI read TOC command.
1126 *
1127 * Read TOC in 'multi-session' format (first and last session number
1128 * with last session LBA).
1129 *
1130 * http://suif.stanford.edu/~csapuntz/specs/INF-8020.PDF page 171
1131 *
1132 * Output buffer must be large enough to hold the data, otherwise the
1133 * function will fail.
1134 *
1135 * @param device Device
1136 * @param session Starting session
1137 * @param obuf Buffer for storing inquiry data read from device
1138 * @param obuf_size Size of obuf in bytes
1139 *
1140 * @return EOK on success, EIO on error.
1141 */
1142static errno_t ata_pcmd_read_toc(ata_device_t *device, uint8_t session,
1143 void *obuf, size_t obuf_size)
1144{
1145 uint8_t cpb[12];
1146 scsi_cdb_read_toc_t *cp = (scsi_cdb_read_toc_t *)cpb;
1147 errno_t rc;
1148
1149 memset(cpb, 0, sizeof(cpb));
1150
1151 cp->op_code = SCSI_CMD_READ_TOC;
1152 cp->msf = 0;
1153 cp->format = 0x01; /* 0x01 = multi-session mode */
1154 cp->track_sess_no = session;
1155 cp->alloc_len = host2uint16_t_be(obuf_size);
1156 cp->control = 0x40; /* 0x01 = multi-session mode (shifted to MSB) */
1157
1158 rc = ata_cmd_packet(device, cpb, sizeof(cpb), obuf, obuf_size, NULL);
1159 if (rc != EOK)
1160 return rc;
1161
1162 return EOK;
1163}
1164
1165/** Read a physical block from the device.
1166 *
1167 * @param device Device
1168 * @param ba Address the first block.
1169 * @param cnt Number of blocks to transfer.
1170 * @param buf Buffer for holding the data.
1171 *
1172 * @return EOK on success, EIO on error.
1173 */
1174static errno_t ata_rcmd_read(ata_device_t *device, uint64_t ba, size_t blk_cnt,
1175 void *buf)
1176{
1177 ata_channel_t *chan = device->chan;
1178 uint8_t drv_head;
1179 block_coord_t bc;
1180 errno_t rc;
1181
1182 /* Silence warning. */
1183 memset(&bc, 0, sizeof(bc));
1184
1185 /* Compute block coordinates. */
1186 if (coord_calc(device, ba, &bc) != EOK) {
1187 ata_msg_note(chan, "ata_rcmd_read() -> coord_calc failed");
1188 return EINVAL;
1189 }
1190
1191 /* New value for Drive/Head register */
1192 drv_head =
1193 ((disk_dev_idx(device) != 0) ? DHR_DRV : 0) |
1194 ((device->amode != am_chs) ? DHR_LBA : 0) |
1195 (bc.h & 0x0f);
1196
1197 fibril_mutex_lock(&chan->lock);
1198
1199 /* Program a Read Sectors operation. */
1200
1201 if (wait_status(chan, 0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
1202 fibril_mutex_unlock(&chan->lock);
1203 ata_msg_note(chan, "ata_rcmd_read() -> wait_status failed");
1204 return EIO;
1205 }
1206
1207 ata_write_cmd_8(chan, REG_DRIVE_HEAD, drv_head);
1208
1209 if (wait_status(chan, SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
1210 fibril_mutex_unlock(&chan->lock);
1211 ata_msg_note(chan, "ata_rcmd_read() -> wait_status 2 failed");
1212 return EIO;
1213 }
1214
1215 /* Program block coordinates into the device. */
1216 coord_sc_program(chan, &bc, blk_cnt);
1217
1218 ata_write_cmd_8(chan, REG_COMMAND, device->amode == am_lba48 ?
1219 CMD_READ_SECTORS_EXT : CMD_READ_SECTORS);
1220
1221 rc = ata_pio_data_in(device, buf, blk_cnt * device->block_size,
1222 device->block_size, blk_cnt);
1223
1224 fibril_mutex_unlock(&chan->lock);
1225
1226 if (rc != EOK)
1227 ata_msg_note(chan, "ata_rcmd_read() -> pio_data_in->%d", rc);
1228 return rc;
1229}
1230
1231/** Write a physical block to the device.
1232 *
1233 * @param device Device
1234 * @param ba Address of the first block.
1235 * @param cnt Number of blocks to transfer.
1236 * @param buf Buffer holding the data to write.
1237 *
1238 * @return EOK on success, EIO on error.
1239 */
1240static errno_t ata_rcmd_write(ata_device_t *device, uint64_t ba, size_t cnt,
1241 const void *buf)
1242{
1243 ata_channel_t *chan = device->chan;
1244 uint8_t drv_head;
1245 block_coord_t bc;
1246 errno_t rc;
1247
1248 /* Silence warning. */
1249 memset(&bc, 0, sizeof(bc));
1250
1251 /* Compute block coordinates. */
1252 if (coord_calc(device, ba, &bc) != EOK)
1253 return EINVAL;
1254
1255 /* New value for Drive/Head register */
1256 drv_head =
1257 ((disk_dev_idx(device) != 0) ? DHR_DRV : 0) |
1258 ((device->amode != am_chs) ? DHR_LBA : 0) |
1259 (bc.h & 0x0f);
1260
1261 fibril_mutex_lock(&chan->lock);
1262
1263 /* Program a Write Sectors operation. */
1264
1265 if (wait_status(chan, 0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
1266 fibril_mutex_unlock(&chan->lock);
1267 return EIO;
1268 }
1269
1270 ata_write_cmd_8(chan, REG_DRIVE_HEAD, drv_head);
1271
1272 if (wait_status(chan, SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
1273 fibril_mutex_unlock(&chan->lock);
1274 return EIO;
1275 }
1276
1277 /* Program block coordinates into the device. */
1278 coord_sc_program(chan, &bc, cnt);
1279
1280 ata_write_cmd_8(chan, REG_COMMAND, device->amode == am_lba48 ?
1281 CMD_WRITE_SECTORS_EXT : CMD_WRITE_SECTORS);
1282
1283 rc = ata_pio_data_out(device, buf, cnt * device->block_size,
1284 device->block_size, cnt);
1285
1286 fibril_mutex_unlock(&chan->lock);
1287 return rc;
1288}
1289
1290/** Flush cached data to nonvolatile storage.
1291 *
1292 * @param device Device
1293 *
1294 * @return EOK on success, EIO on error.
1295 */
1296static errno_t ata_rcmd_flush_cache(ata_device_t *device)
1297{
1298 ata_channel_t *chan = device->chan;
1299 uint8_t drv_head;
1300 errno_t rc;
1301
1302 /* New value for Drive/Head register */
1303 drv_head =
1304 (disk_dev_idx(device) != 0) ? DHR_DRV : 0;
1305
1306 fibril_mutex_lock(&chan->lock);
1307
1308 /* Program a Flush Cache operation. */
1309
1310 if (wait_status(chan, 0, ~SR_BSY, NULL, TIMEOUT_BSY) != EOK) {
1311 fibril_mutex_unlock(&chan->lock);
1312 return EIO;
1313 }
1314
1315 ata_write_cmd_8(chan, REG_DRIVE_HEAD, drv_head);
1316
1317 if (wait_status(chan, SR_DRDY, ~SR_BSY, NULL, TIMEOUT_DRDY) != EOK) {
1318 fibril_mutex_unlock(&chan->lock);
1319 return EIO;
1320 }
1321
1322 ata_write_cmd_8(chan, REG_COMMAND, CMD_FLUSH_CACHE);
1323
1324 rc = ata_pio_nondata(device);
1325
1326 fibril_mutex_unlock(&chan->lock);
1327 return rc;
1328}
1329
1330/** Get the maximum number of blocks to be transferred in one I/O
1331 *
1332 * @param d Device
1333 * @return Maximum number of blocks
1334 */
1335static size_t ata_disk_maxnb(ata_device_t *d)
1336{
1337 size_t maxnb;
1338
1339 maxnb = 0;
1340
1341 if (d->dev_type == ata_pkt_dev) {
1342 /* Could be more depending on SCSI command support */
1343 maxnb = 0x100;
1344 } else {
1345 switch (d->amode) {
1346 case am_chs:
1347 case am_lba28:
1348 maxnb = 0x100;
1349 break;
1350 case am_lba48:
1351 maxnb = 0x10000;
1352 break;
1353 }
1354 }
1355
1356 /*
1357 * If using DMA, this needs to be further restricted not to
1358 * exceed DMA buffer size.
1359 */
1360 return maxnb;
1361}
1362
1363/** Calculate block coordinates.
1364 *
1365 * Calculates block coordinates in the best coordinate system supported
1366 * by the device. These can be later programmed into the device using
1367 * @c coord_sc_program().
1368 *
1369 * @return EOK on success or EINVAL if block index is past end of device.
1370 */
1371static errno_t coord_calc(ata_device_t *d, uint64_t ba, block_coord_t *bc)
1372{
1373 uint64_t c;
1374 uint64_t idx;
1375
1376 /* Check device bounds. */
1377 if (ba >= d->blocks)
1378 return EINVAL;
1379
1380 bc->amode = d->amode;
1381
1382 switch (d->amode) {
1383 case am_chs:
1384 /* Compute CHS coordinates. */
1385 c = ba / (d->geom.heads * d->geom.sectors);
1386 idx = ba % (d->geom.heads * d->geom.sectors);
1387
1388 bc->cyl_lo = c & 0xff;
1389 bc->cyl_hi = (c >> 8) & 0xff;
1390 bc->h = (idx / d->geom.sectors) & 0x0f;
1391 bc->sector = (1 + (idx % d->geom.sectors)) & 0xff;
1392 break;
1393
1394 case am_lba28:
1395 /* Compute LBA-28 coordinates. */
1396 bc->c0 = ba & 0xff; /* bits 0-7 */
1397 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
1398 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
1399 bc->h = (ba >> 24) & 0x0f; /* bits 24-27 */
1400 break;
1401
1402 case am_lba48:
1403 /* Compute LBA-48 coordinates. */
1404 bc->c0 = ba & 0xff; /* bits 0-7 */
1405 bc->c1 = (ba >> 8) & 0xff; /* bits 8-15 */
1406 bc->c2 = (ba >> 16) & 0xff; /* bits 16-23 */
1407 bc->c3 = (ba >> 24) & 0xff; /* bits 24-31 */
1408 bc->c4 = (ba >> 32) & 0xff; /* bits 32-39 */
1409 bc->c5 = (ba >> 40) & 0xff; /* bits 40-47 */
1410 bc->h = 0;
1411 break;
1412 }
1413
1414 return EOK;
1415}
1416
1417/** Program block coordinates and sector count into ATA registers.
1418 *
1419 * Note that bc->h must be programmed separately into the device/head register.
1420 *
1421 * @param chan Channel
1422 * @param bc Block coordinates
1423 * @param scnt Sector count
1424 */
1425static void coord_sc_program(ata_channel_t *chan, const block_coord_t *bc,
1426 uint16_t scnt)
1427{
1428 if (bc->amode == am_lba48) {
1429 /* Write high-order bits. */
1430 ata_write_cmd_8(chan, REG_SECTOR_COUNT, scnt >> 8);
1431 ata_write_cmd_8(chan, REG_SECTOR_NUMBER, bc->c3);
1432 ata_write_cmd_8(chan, REG_CYLINDER_LOW, bc->c4);
1433 ata_write_cmd_8(chan, REG_CYLINDER_HIGH, bc->c5);
1434 }
1435
1436 /* Write low-order bits. */
1437 ata_write_cmd_8(chan, REG_SECTOR_COUNT, scnt & 0x00ff);
1438 ata_write_cmd_8(chan, REG_SECTOR_NUMBER, bc->c0);
1439 ata_write_cmd_8(chan, REG_CYLINDER_LOW, bc->c1);
1440 ata_write_cmd_8(chan, REG_CYLINDER_HIGH, bc->c2);
1441}
1442
1443/** Wait until some status bits are set and some are reset.
1444 *
1445 * Example: wait_status(chan, SR_DRDY, ~SR_BSY, ...) waits for SR_DRDY to become
1446 * set and SR_BSY to become reset.
1447 *
1448 * @param chan Channel
1449 * @param set Combination if bits which must be all set.
1450 * @param n_reset Negated combination of bits which must be all reset.
1451 * @param pstatus Pointer where to store last read status or NULL.
1452 * @param timeout Timeout in 10ms units.
1453 *
1454 * @return EOK on success, EIO on timeout.
1455 */
1456static errno_t wait_status(ata_channel_t *chan, unsigned set, unsigned n_reset,
1457 uint8_t *pstatus, unsigned timeout)
1458{
1459 uint8_t status;
1460 int cnt;
1461
1462 status = ata_read_cmd_8(chan, REG_STATUS);
1463
1464 /*
1465 * This is crude, yet simple. First try with 1us delays
1466 * (most likely the device will respond very fast). If not,
1467 * start trying every 10 ms.
1468 */
1469
1470 cnt = 100;
1471 while ((status & ~n_reset) != 0 || (status & set) != set) {
1472 --cnt;
1473 if (cnt <= 0)
1474 break;
1475
1476 status = ata_read_cmd_8(chan, REG_STATUS);
1477 }
1478
1479 cnt = timeout;
1480 while ((status & ~n_reset) != 0 || (status & set) != set) {
1481 fibril_usleep(10000);
1482 --cnt;
1483 if (cnt <= 0)
1484 break;
1485
1486 status = ata_read_cmd_8(chan, REG_STATUS);
1487 }
1488
1489 if (pstatus)
1490 *pstatus = status;
1491
1492 if (cnt == 0)
1493 return EIO;
1494
1495 return EOK;
1496}
1497
1498/** Wait for IRQ and return status.
1499 *
1500 * @param chan Channel
1501 * @param pstatus Pointer where to store last read status or NULL.
1502 * @param timeout Timeout in 10ms units.
1503 *
1504 * @return EOK on success, EIO on timeout.
1505 */
1506static errno_t wait_irq(ata_channel_t *chan, uint8_t *pstatus, unsigned timeout)
1507{
1508 fibril_mutex_lock(&chan->irq_lock);
1509 while (!chan->irq_fired)
1510 fibril_condvar_wait(&chan->irq_cv, &chan->irq_lock);
1511
1512 chan->irq_fired = false;
1513 *pstatus = chan->irq_status;
1514 fibril_mutex_unlock(&chan->irq_lock);
1515 return EOK;
1516}
1517
1518/** Interrupt handler.
1519 *
1520 * @param chan ATA channel
1521 * @param status Status read by interrupt handler
1522 */
1523void ata_channel_irq(ata_channel_t *chan, uint8_t status)
1524{
1525 fibril_mutex_lock(&chan->irq_lock);
1526 chan->irq_fired = true;
1527 chan->irq_status = status;
1528 fibril_mutex_unlock(&chan->irq_lock);
1529 fibril_condvar_broadcast(&chan->irq_cv);
1530}
1531
1532/** Block device connection handler */
1533void ata_connection(ipc_call_t *icall, ata_device_t *device)
1534{
1535 bd_conn(icall, &device->bds);
1536}
1537
1538/**
1539 * @}
1540 */
Note: See TracBrowser for help on using the repository browser.