| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Petr Jerman
|
|---|
| 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 | /** @file
|
|---|
| 30 | * AHCI SATA driver implementation.
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | #include <as.h>
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include <stdio.h>
|
|---|
| 36 | #include <devman.h>
|
|---|
| 37 | #include <ddf/interrupt.h>
|
|---|
| 38 | #include <ddf/log.h>
|
|---|
| 39 | #include <device/hw_res_parsed.h>
|
|---|
| 40 | #include <device/pci.h>
|
|---|
| 41 | #include <sysinfo.h>
|
|---|
| 42 | #include <ipc/irc.h>
|
|---|
| 43 | #include <ns.h>
|
|---|
| 44 | #include <ahci_iface.h>
|
|---|
| 45 | #include "ahci.h"
|
|---|
| 46 | #include "ahci_hw.h"
|
|---|
| 47 | #include "ahci_sata.h"
|
|---|
| 48 |
|
|---|
| 49 | #define NAME "ahci"
|
|---|
| 50 |
|
|---|
| 51 | #define AHCI_TIMER_TICKS 1000000000
|
|---|
| 52 |
|
|---|
| 53 | #define LO(ptr) \
|
|---|
| 54 | ((uint32_t) (((uint64_t) ((uintptr_t) (ptr))) & 0xffffffff))
|
|---|
| 55 |
|
|---|
| 56 | #define HI(ptr) \
|
|---|
| 57 | ((uint32_t) (((uint64_t) ((uintptr_t) (ptr))) >> 32))
|
|---|
| 58 |
|
|---|
| 59 | static int ahci_get_sata_device_name(ddf_fun_t *, size_t, char *);
|
|---|
| 60 | static int ahci_get_num_blocks(ddf_fun_t *, uint64_t *);
|
|---|
| 61 | static int ahci_get_block_size(ddf_fun_t *, size_t *);
|
|---|
| 62 | static int ahci_read_blocks(ddf_fun_t *, uint64_t, size_t, void *);
|
|---|
| 63 | static int ahci_write_blocks(ddf_fun_t *, uint64_t, size_t, void *);
|
|---|
| 64 |
|
|---|
| 65 | static int ahci_identify_device(sata_dev_t *);
|
|---|
| 66 | static int ahci_set_highest_ultra_dma_mode(sata_dev_t *);
|
|---|
| 67 | static int ahci_rb_fpdma(sata_dev_t *, void *, uint64_t);
|
|---|
| 68 | static int ahci_wb_fpdma(sata_dev_t *, void *, uint64_t);
|
|---|
| 69 |
|
|---|
| 70 | static void ahci_sata_devices_create(ahci_dev_t *, ddf_dev_t *);
|
|---|
| 71 | static ahci_dev_t *ahci_ahci_create(ddf_dev_t *);
|
|---|
| 72 | static void ahci_ahci_init(ahci_dev_t *);
|
|---|
| 73 |
|
|---|
| 74 | static int ahci_dev_add(ddf_dev_t *);
|
|---|
| 75 |
|
|---|
| 76 | static void ahci_get_model_name(uint16_t *, char *);
|
|---|
| 77 | static int ahci_pciintel_enable_interrupt(int);
|
|---|
| 78 |
|
|---|
| 79 | static fibril_mutex_t sata_devices_count_lock;
|
|---|
| 80 | static int sata_devices_count = 0;
|
|---|
| 81 |
|
|---|
| 82 | /*----------------------------------------------------------------------------*/
|
|---|
| 83 | /*-- AHCI Interface ----------------------------------------------------------*/
|
|---|
| 84 | /*----------------------------------------------------------------------------*/
|
|---|
| 85 |
|
|---|
| 86 | static ahci_iface_t ahci_interface = {
|
|---|
| 87 | .get_sata_device_name = &ahci_get_sata_device_name,
|
|---|
| 88 | .get_num_blocks = &ahci_get_num_blocks,
|
|---|
| 89 | .get_block_size = &ahci_get_block_size,
|
|---|
| 90 | .read_blocks = &ahci_read_blocks,
|
|---|
| 91 | .write_blocks = &ahci_write_blocks
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 | static ddf_dev_ops_t ahci_ops = {
|
|---|
| 95 | .interfaces[AHCI_DEV_IFACE] = &ahci_interface
|
|---|
| 96 | };
|
|---|
| 97 |
|
|---|
| 98 | static driver_ops_t driver_ops = {
|
|---|
| 99 | .dev_add = &ahci_dev_add
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 | static driver_t ahci_driver = {
|
|---|
| 103 | .name = NAME,
|
|---|
| 104 | .driver_ops = &driver_ops
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | static int ahci_get_sata_device_name(ddf_fun_t *fun,
|
|---|
| 108 | size_t sata_dev_name_length, char *sata_dev_name)
|
|---|
| 109 | {
|
|---|
| 110 | sata_dev_t *sata = (sata_dev_t *) fun->driver_data;
|
|---|
| 111 | str_cpy(sata_dev_name, sata_dev_name_length, sata->model);
|
|---|
| 112 | return EOK;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | static int ahci_get_num_blocks(ddf_fun_t *fun, uint64_t *num_blocks)
|
|---|
| 116 | {
|
|---|
| 117 | sata_dev_t *sata = (sata_dev_t *) fun->driver_data;
|
|---|
| 118 | *num_blocks = sata->blocks;
|
|---|
| 119 | return EOK;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | static int ahci_get_block_size(ddf_fun_t *fun, size_t *block_size)
|
|---|
| 123 | {
|
|---|
| 124 | sata_dev_t *sata = (sata_dev_t *) fun->driver_data;
|
|---|
| 125 | *block_size = sata->block_size;
|
|---|
| 126 | return EOK;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | static int ahci_read_blocks(ddf_fun_t *fun, uint64_t blocknum,
|
|---|
| 130 | size_t count, void *buf)
|
|---|
| 131 | {
|
|---|
| 132 | int rc = EOK;
|
|---|
| 133 | sata_dev_t *sata = (sata_dev_t *) fun->driver_data;
|
|---|
| 134 |
|
|---|
| 135 | void *phys;
|
|---|
| 136 | void *ibuf;
|
|---|
| 137 |
|
|---|
| 138 | dmamem_map_anonymous(sata->block_size, AS_AREA_READ | AS_AREA_WRITE,
|
|---|
| 139 | 0, &phys, (void **) &ibuf);
|
|---|
| 140 | bzero(buf, sata->block_size);
|
|---|
| 141 |
|
|---|
| 142 | fibril_mutex_lock(&sata->lock);
|
|---|
| 143 |
|
|---|
| 144 | for (size_t cur = 0; cur < count; cur++) {
|
|---|
| 145 | rc = ahci_rb_fpdma(sata, phys, blocknum + cur);
|
|---|
| 146 | if (rc != EOK)
|
|---|
| 147 | break;
|
|---|
| 148 |
|
|---|
| 149 | memcpy((void *) (((uint8_t *) buf) + (sata->block_size * cur)),
|
|---|
| 150 | ibuf, sata->block_size);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | fibril_mutex_unlock(&sata->lock);
|
|---|
| 154 | dmamem_unmap_anonymous(ibuf);
|
|---|
| 155 |
|
|---|
| 156 | return rc;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | static int ahci_write_blocks(ddf_fun_t *fun, uint64_t blocknum,
|
|---|
| 160 | size_t count, void *buf)
|
|---|
| 161 | {
|
|---|
| 162 | int rc = EOK;
|
|---|
| 163 | sata_dev_t *sata = (sata_dev_t *) fun->driver_data;
|
|---|
| 164 |
|
|---|
| 165 | void *phys;
|
|---|
| 166 | void *ibuf;
|
|---|
| 167 |
|
|---|
| 168 | dmamem_map_anonymous(sata->block_size, AS_AREA_READ | AS_AREA_WRITE,
|
|---|
| 169 | 0, &phys, (void **) &ibuf);
|
|---|
| 170 |
|
|---|
| 171 | fibril_mutex_lock(&sata->lock);
|
|---|
| 172 |
|
|---|
| 173 | for (size_t cur = 0; cur < count; cur++) {
|
|---|
| 174 | memcpy(ibuf, (void *) (((uint8_t *) buf) + (sata->block_size * cur)),
|
|---|
| 175 | sata->block_size);
|
|---|
| 176 | rc = ahci_wb_fpdma(sata, phys, blocknum + cur);
|
|---|
| 177 | if (rc != EOK)
|
|---|
| 178 | break;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | fibril_mutex_unlock(&sata->lock);
|
|---|
| 182 | dmamem_unmap_anonymous(ibuf);
|
|---|
| 183 | return rc;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /*----------------------------------------------------------------------------*/
|
|---|
| 187 | /*-- AHCI Commands -----------------------------------------------------------*/
|
|---|
| 188 | /*----------------------------------------------------------------------------*/
|
|---|
| 189 |
|
|---|
| 190 | static void ahci_identify_device_cmd(sata_dev_t *sata, void *phys)
|
|---|
| 191 | {
|
|---|
| 192 | volatile std_command_frame_t *cmd =
|
|---|
| 193 | (std_command_frame_t *) sata->cmd_table;
|
|---|
| 194 |
|
|---|
| 195 | cmd->fis_type = 0x27;
|
|---|
| 196 | cmd->c = 0x80;
|
|---|
| 197 | cmd->command = 0xec;
|
|---|
| 198 | cmd->features = 0;
|
|---|
| 199 | cmd->lba_lower = 0;
|
|---|
| 200 | cmd->device = 0;
|
|---|
| 201 | cmd->lba_upper = 0;
|
|---|
| 202 | cmd->features_upper = 0;
|
|---|
| 203 | cmd->count = 0;
|
|---|
| 204 | cmd->reserved1 = 0;
|
|---|
| 205 | cmd->control = 0;
|
|---|
| 206 | cmd->reserved2 = 0;
|
|---|
| 207 |
|
|---|
| 208 | volatile ahci_cmd_prdt_t *prdt =
|
|---|
| 209 | (ahci_cmd_prdt_t *) (&sata->cmd_table[0x20]);
|
|---|
| 210 |
|
|---|
| 211 | prdt->data_address_low = LO(phys);
|
|---|
| 212 | prdt->data_address_upper = HI(phys);
|
|---|
| 213 | prdt->reserved1 = 0;
|
|---|
| 214 | prdt->dbc = 511;
|
|---|
| 215 | prdt->reserved2 = 0;
|
|---|
| 216 | prdt->ioc = 0;
|
|---|
| 217 |
|
|---|
| 218 | sata->cmd_header->prdtl = 1;
|
|---|
| 219 | sata->cmd_header->flags = 0x402;
|
|---|
| 220 | sata->cmd_header->bytesprocessed = 0;
|
|---|
| 221 |
|
|---|
| 222 | sata->port->pxsact |= 1;
|
|---|
| 223 | sata->port->pxci |= 1;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | static void ahci_identify_packet_device_cmd(sata_dev_t *sata, void *phys)
|
|---|
| 227 | {
|
|---|
| 228 | volatile std_command_frame_t *cmd =
|
|---|
| 229 | (std_command_frame_t *) sata->cmd_table;
|
|---|
| 230 |
|
|---|
| 231 | cmd->fis_type = 0x27;
|
|---|
| 232 | cmd->c = 0x80;
|
|---|
| 233 | cmd->command = 0xa1;
|
|---|
| 234 | cmd->features = 0;
|
|---|
| 235 | cmd->lba_lower = 0;
|
|---|
| 236 | cmd->device = 0;
|
|---|
| 237 | cmd->lba_upper = 0;
|
|---|
| 238 | cmd->features_upper = 0;
|
|---|
| 239 | cmd->count = 0;
|
|---|
| 240 | cmd->reserved1 = 0;
|
|---|
| 241 | cmd->control = 0;
|
|---|
| 242 | cmd->reserved2 = 0;
|
|---|
| 243 |
|
|---|
| 244 | volatile ahci_cmd_prdt_t *prdt =
|
|---|
| 245 | (ahci_cmd_prdt_t *) (&sata->cmd_table[0x20]);
|
|---|
| 246 |
|
|---|
| 247 | prdt->data_address_low = LO(phys);
|
|---|
| 248 | prdt->data_address_upper = HI(phys);
|
|---|
| 249 | prdt->reserved1 = 0;
|
|---|
| 250 | prdt->dbc = 511;
|
|---|
| 251 | prdt->reserved2 = 0;
|
|---|
| 252 | prdt->ioc = 0;
|
|---|
| 253 |
|
|---|
| 254 | sata->cmd_header->prdtl = 1;
|
|---|
| 255 | sata->cmd_header->flags = 0x402;
|
|---|
| 256 | sata->cmd_header->bytesprocessed = 0;
|
|---|
| 257 |
|
|---|
| 258 | sata->port->pxsact |= 1;
|
|---|
| 259 | sata->port->pxci |= 1;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | static int ahci_identify_device(sata_dev_t *sata)
|
|---|
| 263 | {
|
|---|
| 264 | if (sata->invalid_device) {
|
|---|
| 265 | ddf_msg(LVL_ERROR,
|
|---|
| 266 | "Identify command device on invalid device");
|
|---|
| 267 | return EINTR;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | void *phys;
|
|---|
| 271 | identify_data_t *idata;
|
|---|
| 272 |
|
|---|
| 273 | dmamem_map_anonymous(512, AS_AREA_READ | AS_AREA_WRITE, 0, &phys,
|
|---|
| 274 | (void **) &idata);
|
|---|
| 275 | bzero(idata, 512);
|
|---|
| 276 |
|
|---|
| 277 | fibril_mutex_lock(&sata->lock);
|
|---|
| 278 |
|
|---|
| 279 | ahci_identify_device_cmd(sata, phys);
|
|---|
| 280 |
|
|---|
| 281 | fibril_mutex_lock(&sata->event_lock);
|
|---|
| 282 | fibril_condvar_wait(&sata->event_condvar, &sata->event_lock);
|
|---|
| 283 | fibril_mutex_unlock(&sata->event_lock);
|
|---|
| 284 |
|
|---|
| 285 | ahci_port_is_t pxis = sata->shadow_pxis;
|
|---|
| 286 | sata->shadow_pxis.u32 &= ~pxis.u32;
|
|---|
| 287 |
|
|---|
| 288 | if (sata->invalid_device) {
|
|---|
| 289 | ddf_msg(LVL_ERROR,
|
|---|
| 290 | "Unrecoverable error during ata identify device");
|
|---|
| 291 | goto error;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | if (ahci_port_is_tfes(pxis)) {
|
|---|
| 295 | ahci_identify_packet_device_cmd(sata, phys);
|
|---|
| 296 |
|
|---|
| 297 | fibril_mutex_lock(&sata->event_lock);
|
|---|
| 298 | fibril_condvar_wait(&sata->event_condvar, &sata->event_lock);
|
|---|
| 299 | fibril_mutex_unlock(&sata->event_lock);
|
|---|
| 300 |
|
|---|
| 301 | pxis = sata->shadow_pxis;
|
|---|
| 302 | sata->shadow_pxis.u32 &= ~pxis.u32;
|
|---|
| 303 |
|
|---|
| 304 | if ((sata->invalid_device) || (ahci_port_is_error(pxis))) {
|
|---|
| 305 | ddf_msg(LVL_ERROR,
|
|---|
| 306 | "Unrecoverable error during ata identify packet device");
|
|---|
| 307 | goto error;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | sata->packet_device = true;
|
|---|
| 311 | } else
|
|---|
| 312 | sata->packet_device = false;
|
|---|
| 313 |
|
|---|
| 314 | ahci_get_model_name(idata->model_name, sata->model);
|
|---|
| 315 |
|
|---|
| 316 | /*
|
|---|
| 317 | * Due to QEMU limitation (as of 2012-06-22),
|
|---|
| 318 | * only NCQ FPDMA mode is supported.
|
|---|
| 319 | */
|
|---|
| 320 | if ((idata->sata_cap & np_cap_ncq) == 0) {
|
|---|
| 321 | ddf_msg(LVL_ERROR, "%s: NCQ must be supported", sata->model);
|
|---|
| 322 | goto error;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | if (sata->packet_device) {
|
|---|
| 326 | /*
|
|---|
| 327 | * Due to QEMU limitation (as of 2012-06-22),
|
|---|
| 328 | * only NCQ FPDMA mode supported - block size is 512 B,
|
|---|
| 329 | * not 2048 B!
|
|---|
| 330 | */
|
|---|
| 331 | sata->block_size = 512;
|
|---|
| 332 | sata->blocks = 0;
|
|---|
| 333 | } else {
|
|---|
| 334 | sata->block_size = 512;
|
|---|
| 335 |
|
|---|
| 336 | if ((idata->caps & rd_cap_lba) == 0) {
|
|---|
| 337 | ddf_msg(LVL_ERROR, "%s: LBA for NCQ must be supported",
|
|---|
| 338 | sata->model);
|
|---|
| 339 | goto error;
|
|---|
| 340 | } else if ((idata->cmd_set1 & cs1_addr48) == 0) {
|
|---|
| 341 | sata->blocks = (uint32_t) idata->total_lba28_0 |
|
|---|
| 342 | ((uint32_t) idata->total_lba28_1 << 16);
|
|---|
| 343 | } else {
|
|---|
| 344 | /* Device supports LBA-48 addressing. */
|
|---|
| 345 | sata->blocks = (uint64_t) idata->total_lba48_0 |
|
|---|
| 346 | ((uint64_t) idata->total_lba48_1 << 16) |
|
|---|
| 347 | ((uint64_t) idata->total_lba48_2 << 32) |
|
|---|
| 348 | ((uint64_t) idata->total_lba48_3 << 48);
|
|---|
| 349 | }
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | uint8_t udma_mask = idata->udma & 0x007f;
|
|---|
| 353 | if (udma_mask == 0) {
|
|---|
| 354 | ddf_msg(LVL_ERROR,
|
|---|
| 355 | "%s: UDMA mode for NCQ FPDMA mode must be supported",
|
|---|
| 356 | sata->model);
|
|---|
| 357 | goto error;
|
|---|
| 358 | } else {
|
|---|
| 359 | for (unsigned int i = 0; i < 7; i++) {
|
|---|
| 360 | if (udma_mask & (1 << i))
|
|---|
| 361 | sata->highest_udma_mode = i;
|
|---|
| 362 | }
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | fibril_mutex_unlock(&sata->lock);
|
|---|
| 366 | dmamem_unmap_anonymous(idata);
|
|---|
| 367 |
|
|---|
| 368 | return EOK;
|
|---|
| 369 |
|
|---|
| 370 | error:
|
|---|
| 371 | fibril_mutex_unlock(&sata->lock);
|
|---|
| 372 | dmamem_unmap_anonymous(idata);
|
|---|
| 373 |
|
|---|
| 374 | return EINTR;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | static void ahci_set_mode_cmd(sata_dev_t *sata, void* phys, uint8_t mode)
|
|---|
| 378 | {
|
|---|
| 379 | volatile std_command_frame_t *cmd =
|
|---|
| 380 | (std_command_frame_t *) sata->cmd_table;
|
|---|
| 381 |
|
|---|
| 382 | cmd->fis_type = 0x27;
|
|---|
| 383 | cmd->c = 0x80;
|
|---|
| 384 | cmd->command = 0xef;
|
|---|
| 385 | cmd->features = 0x03;
|
|---|
| 386 | cmd->lba_lower = 0;
|
|---|
| 387 | cmd->device = 0;
|
|---|
| 388 | cmd->lba_upper = 0;
|
|---|
| 389 | cmd->features_upper = 0;
|
|---|
| 390 | cmd->count = mode;
|
|---|
| 391 | cmd->reserved1 = 0;
|
|---|
| 392 | cmd->control = 0;
|
|---|
| 393 | cmd->reserved2 = 0;
|
|---|
| 394 |
|
|---|
| 395 | volatile ahci_cmd_prdt_t* prdt =
|
|---|
| 396 | (ahci_cmd_prdt_t *) (&sata->cmd_table[0x20]);
|
|---|
| 397 |
|
|---|
| 398 | prdt->data_address_low = LO(phys);
|
|---|
| 399 | prdt->data_address_upper = HI(phys);
|
|---|
| 400 | prdt->reserved1 = 0;
|
|---|
| 401 | prdt->dbc = 511;
|
|---|
| 402 | prdt->reserved2 = 0;
|
|---|
| 403 | prdt->ioc = 0;
|
|---|
| 404 |
|
|---|
| 405 | sata->cmd_header->prdtl = 1;
|
|---|
| 406 | sata->cmd_header->flags = 0x402;
|
|---|
| 407 | sata->cmd_header->bytesprocessed = 0;
|
|---|
| 408 |
|
|---|
| 409 | sata->port->pxsact |= 1;
|
|---|
| 410 | sata->port->pxci |= 1;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | static int ahci_set_highest_ultra_dma_mode(sata_dev_t *sata)
|
|---|
| 414 | {
|
|---|
| 415 | if (sata->invalid_device) {
|
|---|
| 416 | ddf_msg(LVL_ERROR,
|
|---|
| 417 | "%s: Setting highest UDMA mode on invalid device",
|
|---|
| 418 | sata->model);
|
|---|
| 419 | return EINTR;
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | void *phys;
|
|---|
| 423 | identify_data_t *idata;
|
|---|
| 424 |
|
|---|
| 425 | dmamem_map_anonymous(512, AS_AREA_READ | AS_AREA_WRITE, 0, &phys,
|
|---|
| 426 | (void **) &idata);
|
|---|
| 427 | bzero(idata, 512);
|
|---|
| 428 |
|
|---|
| 429 | fibril_mutex_lock(&sata->lock);
|
|---|
| 430 |
|
|---|
| 431 | uint8_t mode = 0x40 | (sata->highest_udma_mode & 0x07);
|
|---|
| 432 | ahci_set_mode_cmd(sata, phys, mode);
|
|---|
| 433 |
|
|---|
| 434 | fibril_mutex_lock(&sata->event_lock);
|
|---|
| 435 | fibril_condvar_wait(&sata->event_condvar, &sata->event_lock);
|
|---|
| 436 | fibril_mutex_unlock(&sata->event_lock);
|
|---|
| 437 |
|
|---|
| 438 | ahci_port_is_t pxis = sata->shadow_pxis;
|
|---|
| 439 | sata->shadow_pxis.u32 &= ~pxis.u32;
|
|---|
| 440 |
|
|---|
| 441 | if (sata->invalid_device) {
|
|---|
| 442 | ddf_msg(LVL_ERROR,
|
|---|
| 443 | "%s: Unrecoverable error during set highest UDMA mode",
|
|---|
| 444 | sata->model);
|
|---|
| 445 | goto error;
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | if (ahci_port_is_error(pxis)) {
|
|---|
| 449 | ddf_msg(LVL_ERROR,
|
|---|
| 450 | "%s: Error during set highest UDMA mode", sata->model);
|
|---|
| 451 | goto error;
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | fibril_mutex_unlock(&sata->lock);
|
|---|
| 455 | dmamem_unmap_anonymous(idata);
|
|---|
| 456 |
|
|---|
| 457 | return EOK;
|
|---|
| 458 |
|
|---|
| 459 | error:
|
|---|
| 460 | fibril_mutex_unlock(&sata->lock);
|
|---|
| 461 | dmamem_unmap_anonymous(idata);
|
|---|
| 462 |
|
|---|
| 463 | return EINTR;
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | static void ahci_rb_fpdma_cmd(sata_dev_t *sata, void *phys, uint64_t blocknum)
|
|---|
| 467 | {
|
|---|
| 468 | volatile ncq_command_frame_t *cmd =
|
|---|
| 469 | (ncq_command_frame_t *) sata->cmd_table;
|
|---|
| 470 |
|
|---|
| 471 | cmd->fis_type = 0x27;
|
|---|
| 472 | cmd->c = 0x80;
|
|---|
| 473 | cmd->command = 0x60;
|
|---|
| 474 | cmd->tag = 0;
|
|---|
| 475 | cmd->control = 0;
|
|---|
| 476 |
|
|---|
| 477 | cmd->reserved1 = 0;
|
|---|
| 478 | cmd->reserved2 = 0;
|
|---|
| 479 | cmd->reserved3 = 0;
|
|---|
| 480 | cmd->reserved4 = 0;
|
|---|
| 481 | cmd->reserved5 = 0;
|
|---|
| 482 | cmd->reserved6 = 0;
|
|---|
| 483 |
|
|---|
| 484 | cmd->sector_count_low = 1;
|
|---|
| 485 | cmd->sector_count_high = 0;
|
|---|
| 486 |
|
|---|
| 487 | cmd->lba0 = blocknum & 0xff;
|
|---|
| 488 | cmd->lba1 = (blocknum >> 8) & 0xff;
|
|---|
| 489 | cmd->lba2 = (blocknum >> 16) & 0xff;
|
|---|
| 490 | cmd->lba3 = (blocknum >> 24) & 0xff;
|
|---|
| 491 | cmd->lba4 = (blocknum >> 32) & 0xff;
|
|---|
| 492 | cmd->lba5 = (blocknum >> 40) & 0xff;
|
|---|
| 493 |
|
|---|
| 494 | volatile ahci_cmd_prdt_t *prdt =
|
|---|
| 495 | (ahci_cmd_prdt_t *) (&sata->cmd_table[0x20]);
|
|---|
| 496 |
|
|---|
| 497 | prdt->data_address_low = LO(phys);
|
|---|
| 498 | prdt->data_address_upper = HI(phys);
|
|---|
| 499 | prdt->reserved1 = 0;
|
|---|
| 500 | prdt->dbc = sata->block_size - 1;
|
|---|
| 501 | prdt->reserved2 = 0;
|
|---|
| 502 | prdt->ioc = 0;
|
|---|
| 503 |
|
|---|
| 504 | sata->cmd_header->prdtl = 1;
|
|---|
| 505 | sata->cmd_header->flags = 0x405;
|
|---|
| 506 | sata->cmd_header->bytesprocessed = 0;
|
|---|
| 507 |
|
|---|
| 508 | sata->port->pxsact |= 1;
|
|---|
| 509 | sata->port->pxci |= 1;
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | static int ahci_rb_fpdma(sata_dev_t *sata, void *phys, uint64_t blocknum)
|
|---|
| 513 | {
|
|---|
| 514 | if (sata->invalid_device) {
|
|---|
| 515 | ddf_msg(LVL_ERROR,
|
|---|
| 516 | "%s: FPDMA read from invalid device", sata->model);
|
|---|
| 517 | return EINTR;
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | ahci_rb_fpdma_cmd(sata, phys, blocknum);
|
|---|
| 521 |
|
|---|
| 522 | fibril_mutex_lock(&sata->event_lock);
|
|---|
| 523 | fibril_condvar_wait(&sata->event_condvar, &sata->event_lock);
|
|---|
| 524 | fibril_mutex_unlock(&sata->event_lock);
|
|---|
| 525 |
|
|---|
| 526 | ahci_port_is_t pxis = sata->shadow_pxis;
|
|---|
| 527 | sata->shadow_pxis.u32 &= ~pxis.u32;
|
|---|
| 528 |
|
|---|
| 529 | if ((sata->invalid_device) || (ahci_port_is_error(pxis))) {
|
|---|
| 530 | ddf_msg(LVL_ERROR,
|
|---|
| 531 | "%s: Unrecoverable error during FPDMA read", sata->model);
|
|---|
| 532 | return EINTR;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | return EOK;
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | static void ahci_wb_fpdma_cmd(sata_dev_t *sata, void *phys, uint64_t blocknum)
|
|---|
| 539 | {
|
|---|
| 540 | volatile ncq_command_frame_t *cmd =
|
|---|
| 541 | (ncq_command_frame_t *) sata->cmd_table;
|
|---|
| 542 |
|
|---|
| 543 | cmd->fis_type = 0x27;
|
|---|
| 544 | cmd->c = 0x80;
|
|---|
| 545 | cmd->command = 0x61;
|
|---|
| 546 | cmd->tag = 0;
|
|---|
| 547 | cmd->control = 0;
|
|---|
| 548 |
|
|---|
| 549 | cmd->reserved1 = 0;
|
|---|
| 550 | cmd->reserved2 = 0;
|
|---|
| 551 | cmd->reserved3 = 0;
|
|---|
| 552 | cmd->reserved4 = 0;
|
|---|
| 553 | cmd->reserved5 = 0;
|
|---|
| 554 | cmd->reserved6 = 0;
|
|---|
| 555 |
|
|---|
| 556 | cmd->sector_count_low = 1;
|
|---|
| 557 | cmd->sector_count_high = 0;
|
|---|
| 558 |
|
|---|
| 559 | cmd->lba0 = blocknum & 0xff;
|
|---|
| 560 | cmd->lba1 = (blocknum >> 8) & 0xff;
|
|---|
| 561 | cmd->lba2 = (blocknum >> 16) & 0xff;
|
|---|
| 562 | cmd->lba3 = (blocknum >> 24) & 0xff;
|
|---|
| 563 | cmd->lba4 = (blocknum >> 32) & 0xff;
|
|---|
| 564 | cmd->lba5 = (blocknum >> 40) & 0xff;
|
|---|
| 565 |
|
|---|
| 566 | volatile ahci_cmd_prdt_t * prdt =
|
|---|
| 567 | (ahci_cmd_prdt_t *) (&sata->cmd_table[0x20]);
|
|---|
| 568 |
|
|---|
| 569 | prdt->data_address_low = LO(phys);
|
|---|
| 570 | prdt->data_address_upper = HI(phys);
|
|---|
| 571 | prdt->reserved1 = 0;
|
|---|
| 572 | prdt->dbc = sata->block_size - 1;
|
|---|
| 573 | prdt->reserved2 = 0;
|
|---|
| 574 | prdt->ioc = 0;
|
|---|
| 575 |
|
|---|
| 576 | sata->cmd_header->prdtl = 1;
|
|---|
| 577 | sata->cmd_header->flags = 0x445;
|
|---|
| 578 | sata->cmd_header->bytesprocessed = 0;
|
|---|
| 579 |
|
|---|
| 580 | sata->port->pxsact |= 1;
|
|---|
| 581 | sata->port->pxci |= 1;
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | static int ahci_wb_fpdma(sata_dev_t *sata, void *phys, uint64_t blocknum)
|
|---|
| 585 | {
|
|---|
| 586 | if (sata->invalid_device) {
|
|---|
| 587 | ddf_msg(LVL_ERROR,
|
|---|
| 588 | "%s: FPDMA write to invalid device", sata->model);
|
|---|
| 589 | return EINTR;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | ahci_wb_fpdma_cmd(sata, phys, blocknum);
|
|---|
| 593 |
|
|---|
| 594 | fibril_mutex_lock(&sata->event_lock);
|
|---|
| 595 | fibril_condvar_wait(&sata->event_condvar, &sata->event_lock);
|
|---|
| 596 | fibril_mutex_unlock(&sata->event_lock);
|
|---|
| 597 |
|
|---|
| 598 | ahci_port_is_t pxis = sata->shadow_pxis;
|
|---|
| 599 | sata->shadow_pxis.u32 &= ~pxis.u32;
|
|---|
| 600 |
|
|---|
| 601 | if ((sata->invalid_device) || (ahci_port_is_error(pxis))) {
|
|---|
| 602 | ddf_msg(LVL_ERROR,
|
|---|
| 603 | "%s: Unrecoverable error during FPDMA write", sata->model);
|
|---|
| 604 | return EINTR;
|
|---|
| 605 | }
|
|---|
| 606 |
|
|---|
| 607 | return EOK;
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | /*----------------------------------------------------------------------------*/
|
|---|
| 611 | /*-- Interrupts and timer unified handling -----------------------------------*/
|
|---|
| 612 | /*----------------------------------------------------------------------------*/
|
|---|
| 613 |
|
|---|
| 614 | static irq_pio_range_t ahci_ranges[] = {
|
|---|
| 615 | {
|
|---|
| 616 | .base = 0,
|
|---|
| 617 | .size = 32,
|
|---|
| 618 | }
|
|---|
| 619 | };
|
|---|
| 620 |
|
|---|
| 621 | static irq_cmd_t ahci_cmds[] = {
|
|---|
| 622 | {
|
|---|
| 623 | /* Disable interrupt - interrupt is deasserted in qemu 1.0.1 */
|
|---|
| 624 | .cmd = CMD_PIO_WRITE_32,
|
|---|
| 625 | .addr = NULL,
|
|---|
| 626 | .value = AHCI_GHC_GHC_AE
|
|---|
| 627 | },
|
|---|
| 628 | {
|
|---|
| 629 | .cmd = CMD_PIO_READ_32,
|
|---|
| 630 | .addr = NULL,
|
|---|
| 631 | .dstarg = 1
|
|---|
| 632 | },
|
|---|
| 633 | {
|
|---|
| 634 | /* Clear interrupt status register - for vbox and real hw */
|
|---|
| 635 | .cmd = CMD_PIO_WRITE_A_32,
|
|---|
| 636 | .addr = NULL,
|
|---|
| 637 | .srcarg = 1
|
|---|
| 638 | },
|
|---|
| 639 | {
|
|---|
| 640 | .cmd = CMD_ACCEPT
|
|---|
| 641 | }
|
|---|
| 642 | };
|
|---|
| 643 |
|
|---|
| 644 | /** Unified AHCI interrupt and timer interrupt handler.
|
|---|
| 645 | *
|
|---|
| 646 | * @param ahci AHCI device.
|
|---|
| 647 | * @param is_timer Indicate timer interrupt.
|
|---|
| 648 | *
|
|---|
| 649 | */
|
|---|
| 650 | static void ahci_interrupt_or_timer(ahci_dev_t *ahci, bool is_timer)
|
|---|
| 651 | {
|
|---|
| 652 | /*
|
|---|
| 653 | * Get current value of hardware interrupt state register,
|
|---|
| 654 | * clear hardware register (write to clear behavior).
|
|---|
| 655 | */
|
|---|
| 656 | ahci_ghc_is_t is;
|
|---|
| 657 |
|
|---|
| 658 | is.u32 = ahci->memregs->ghc.is;
|
|---|
| 659 | ahci->memregs->ghc.is = is.u32;
|
|---|
| 660 | is.u32 = ahci->memregs->ghc.is;
|
|---|
| 661 |
|
|---|
| 662 | uint32_t port_event_flags = 0;
|
|---|
| 663 | uint32_t port_mask = 1;
|
|---|
| 664 | for (unsigned int i = 0; i < 32; i++) {
|
|---|
| 665 | /*
|
|---|
| 666 | * Get current value of hardware port interrupt state register,
|
|---|
| 667 | * clear hardware register (write to clear behavior).
|
|---|
| 668 | */
|
|---|
| 669 | ahci_port_is_t pxis;
|
|---|
| 670 |
|
|---|
| 671 | pxis.u32 = ahci->memregs->ports[i].pxis;
|
|---|
| 672 | ahci->memregs->ports[i].pxis = pxis.u32;
|
|---|
| 673 |
|
|---|
| 674 | sata_dev_t *sata = (sata_dev_t *) ahci->sata_devs[i];
|
|---|
| 675 | if (sata != NULL) {
|
|---|
| 676 | /* add value to shadow copy of port interrupt state register. */
|
|---|
| 677 | sata->shadow_pxis.u32 |= pxis.u32;
|
|---|
| 678 |
|
|---|
| 679 | /* Evaluate port event. */
|
|---|
| 680 | if ((ahci_port_is_end_of_operation(pxis)) ||
|
|---|
| 681 | (ahci_port_is_error(pxis)))
|
|---|
| 682 | port_event_flags |= port_mask;
|
|---|
| 683 |
|
|---|
| 684 | if (ahci_port_is_permanent_error(pxis))
|
|---|
| 685 | sata->invalid_device = true;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | port_mask <<= 1;
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | port_mask = 1;
|
|---|
| 692 | for (unsigned int i = 0; i < 32; i++) {
|
|---|
| 693 | sata_dev_t *sata = (sata_dev_t *) ahci->sata_devs[i];
|
|---|
| 694 | if ((port_event_flags & port_mask) && (sata != NULL)) {
|
|---|
| 695 | fibril_mutex_lock(&sata->event_lock);
|
|---|
| 696 | fibril_condvar_signal(&sata->event_condvar);
|
|---|
| 697 | fibril_mutex_unlock(&sata->event_lock);
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | port_mask <<= 1;
|
|---|
| 701 | }
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | /** AHCI timer interrupt handler.
|
|---|
| 705 | *
|
|---|
| 706 | * @param arg Pointer to AHCI device.
|
|---|
| 707 | *
|
|---|
| 708 | */
|
|---|
| 709 | static void ahci_timer(void *arg)
|
|---|
| 710 | {
|
|---|
| 711 | ahci_dev_t *ahci = (ahci_dev_t *) arg;
|
|---|
| 712 |
|
|---|
| 713 | ahci_interrupt_or_timer(ahci, 1);
|
|---|
| 714 | fibril_timer_set(ahci->timer, AHCI_TIMER_TICKS, ahci_timer, ahci);
|
|---|
| 715 | }
|
|---|
| 716 |
|
|---|
| 717 | /** AHCI interrupt handler.
|
|---|
| 718 | *
|
|---|
| 719 | * @param dev Pointer to device driver handler.
|
|---|
| 720 | *
|
|---|
| 721 | */
|
|---|
| 722 | static void ahci_interrupt(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 723 | {
|
|---|
| 724 | ahci_dev_t *ahci = (ahci_dev_t *) dev->driver_data;
|
|---|
| 725 |
|
|---|
| 726 | ahci_interrupt_or_timer(ahci, 0);
|
|---|
| 727 |
|
|---|
| 728 | /* Enable interrupt. */
|
|---|
| 729 | ahci->memregs->ghc.ghc |= 2;
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 | /*----------------------------------------------------------------------------*/
|
|---|
| 733 | /*-- AHCI and SATA device creating and initializing routines -----------------*/
|
|---|
| 734 | /*----------------------------------------------------------------------------*/
|
|---|
| 735 |
|
|---|
| 736 | static sata_dev_t *ahci_sata_device_allocate(volatile ahci_port_t *port)
|
|---|
| 737 | {
|
|---|
| 738 | size_t size = 4096;
|
|---|
| 739 | void* phys = NULL;
|
|---|
| 740 | void* virt_fb = NULL;
|
|---|
| 741 | void* virt_cmd = NULL;
|
|---|
| 742 | void* virt_table = NULL;
|
|---|
| 743 |
|
|---|
| 744 | sata_dev_t *sata = malloc(sizeof(sata_dev_t));
|
|---|
| 745 | if (sata == NULL)
|
|---|
| 746 | return NULL;
|
|---|
| 747 |
|
|---|
| 748 | bzero(sata, sizeof(sata_dev_t));
|
|---|
| 749 |
|
|---|
| 750 | sata->port = port;
|
|---|
| 751 |
|
|---|
| 752 | /* Allocate and init retfis structure. */
|
|---|
| 753 | int rc = dmamem_map_anonymous(size, AS_AREA_READ | AS_AREA_WRITE, 0,
|
|---|
| 754 | &phys, &virt_fb);
|
|---|
| 755 | if (rc != EOK)
|
|---|
| 756 | goto error_retfis;
|
|---|
| 757 |
|
|---|
| 758 | bzero(virt_fb, size);
|
|---|
| 759 | sata->port->pxfbu = HI(phys);
|
|---|
| 760 | sata->port->pxfb = LO(phys);
|
|---|
| 761 |
|
|---|
| 762 | /* Allocate and init command header structure. */
|
|---|
| 763 | rc = dmamem_map_anonymous(size, AS_AREA_READ | AS_AREA_WRITE, 0,
|
|---|
| 764 | &phys, &virt_cmd);
|
|---|
| 765 |
|
|---|
| 766 | if (rc != EOK)
|
|---|
| 767 | goto error_cmd;
|
|---|
| 768 |
|
|---|
| 769 | bzero(virt_cmd, size);
|
|---|
| 770 | sata->port->pxclbu = HI(phys);
|
|---|
| 771 | sata->port->pxclb = LO(phys);
|
|---|
| 772 | sata->cmd_header = (ahci_cmdhdr_t *) virt_cmd;
|
|---|
| 773 |
|
|---|
| 774 | /* Allocate and init command table structure. */
|
|---|
| 775 | rc = dmamem_map_anonymous(size, AS_AREA_READ | AS_AREA_WRITE, 0,
|
|---|
| 776 | &phys, &virt_table);
|
|---|
| 777 |
|
|---|
| 778 | if (rc != EOK)
|
|---|
| 779 | goto error_table;
|
|---|
| 780 |
|
|---|
| 781 | bzero(virt_table, size);
|
|---|
| 782 | sata->cmd_header->cmdtableu = HI(phys);
|
|---|
| 783 | sata->cmd_header->cmdtable = LO(phys);
|
|---|
| 784 | sata->cmd_table = (uint32_t*) virt_table;
|
|---|
| 785 |
|
|---|
| 786 | return sata;
|
|---|
| 787 |
|
|---|
| 788 | error_table:
|
|---|
| 789 | dmamem_unmap(virt_cmd, size);
|
|---|
| 790 | error_cmd:
|
|---|
| 791 | dmamem_unmap(virt_fb, size);
|
|---|
| 792 | error_retfis:
|
|---|
| 793 | free(sata);
|
|---|
| 794 | return NULL;
|
|---|
| 795 |
|
|---|
| 796 | /*
|
|---|
| 797 | * Deleting of pointers in memory hardware mapped register
|
|---|
| 798 | * unneccessary, hardware port is not in operational state.
|
|---|
| 799 | */
|
|---|
| 800 | }
|
|---|
| 801 |
|
|---|
| 802 | static int ahci_sata_device_create(ahci_dev_t *ahci, ddf_dev_t *dev,
|
|---|
| 803 | volatile ahci_port_t *port, unsigned int port_num)
|
|---|
| 804 | {
|
|---|
| 805 | ddf_fun_t *fun = NULL;
|
|---|
| 806 | sata_dev_t *sata = ahci_sata_device_allocate(port);
|
|---|
| 807 |
|
|---|
| 808 | if (sata == NULL)
|
|---|
| 809 | return EINTR;
|
|---|
| 810 |
|
|---|
| 811 | /* Set pointers between SATA and AHCI structures. */
|
|---|
| 812 | sata->ahci = ahci;
|
|---|
| 813 | sata->port_num = port_num;
|
|---|
| 814 | ahci->sata_devs[port_num] = sata;
|
|---|
| 815 |
|
|---|
| 816 | fibril_mutex_initialize(&sata->lock);
|
|---|
| 817 | fibril_mutex_initialize(&sata->event_lock);
|
|---|
| 818 | fibril_condvar_initialize(&sata->event_condvar);
|
|---|
| 819 |
|
|---|
| 820 | /* Initialize SATA port operational registers. */
|
|---|
| 821 | sata->port->pxis = 0;
|
|---|
| 822 | sata->port->pxie = 0xffffffff;
|
|---|
| 823 | sata->port->pxserr = 0;
|
|---|
| 824 | sata->port->pxcmd |= 0x10;
|
|---|
| 825 | sata->port->pxcmd |= 0x01;
|
|---|
| 826 |
|
|---|
| 827 | if (ahci_identify_device(sata) != EOK)
|
|---|
| 828 | goto error;
|
|---|
| 829 |
|
|---|
| 830 | if (ahci_set_highest_ultra_dma_mode(sata) != EOK)
|
|---|
| 831 | goto error;
|
|---|
| 832 |
|
|---|
| 833 | /* Add sata device to system. */
|
|---|
| 834 | char sata_dev_name[1024];
|
|---|
| 835 | snprintf(sata_dev_name, 1024, "ahci_%u", sata_devices_count);
|
|---|
| 836 |
|
|---|
| 837 | fibril_mutex_lock(&sata_devices_count_lock);
|
|---|
| 838 | sata_devices_count++;
|
|---|
| 839 | fibril_mutex_unlock(&sata_devices_count_lock);
|
|---|
| 840 |
|
|---|
| 841 | fun = ddf_fun_create(dev, fun_exposed, sata_dev_name);
|
|---|
| 842 | if (fun == NULL) {
|
|---|
| 843 | ddf_msg(LVL_ERROR, "Failed creating function.");
|
|---|
| 844 | goto error;
|
|---|
| 845 | }
|
|---|
| 846 |
|
|---|
| 847 | fun->ops = &ahci_ops;
|
|---|
| 848 | fun->driver_data = sata;
|
|---|
| 849 | int rc = ddf_fun_bind(fun);
|
|---|
| 850 | if (rc != EOK) {
|
|---|
| 851 | ddf_msg(LVL_ERROR, "Failed binding function.");
|
|---|
| 852 | goto error;
|
|---|
| 853 | }
|
|---|
| 854 |
|
|---|
| 855 | return EOK;
|
|---|
| 856 |
|
|---|
| 857 | error:
|
|---|
| 858 | sata->invalid_device = true;
|
|---|
| 859 | if (fun != NULL)
|
|---|
| 860 | ddf_fun_destroy(fun);
|
|---|
| 861 |
|
|---|
| 862 | return EINTR;
|
|---|
| 863 | }
|
|---|
| 864 |
|
|---|
| 865 | static void ahci_sata_devices_create(ahci_dev_t *ahci, ddf_dev_t *dev)
|
|---|
| 866 | {
|
|---|
| 867 | for (unsigned int port_num = 0; port_num < 32; port_num++) {
|
|---|
| 868 | /* Active ports only */
|
|---|
| 869 | if (!(ahci->memregs->ghc.pi & (1 << port_num)))
|
|---|
| 870 | continue;
|
|---|
| 871 |
|
|---|
| 872 | volatile ahci_port_t *port = ahci->memregs->ports + port_num;
|
|---|
| 873 |
|
|---|
| 874 | /* Active devices only */
|
|---|
| 875 | if ((port->pxssts & 0x0f) != 3)
|
|---|
| 876 | continue;
|
|---|
| 877 |
|
|---|
| 878 | ahci_sata_device_create(ahci, dev, port, port_num);
|
|---|
| 879 | }
|
|---|
| 880 | }
|
|---|
| 881 |
|
|---|
| 882 | static ahci_dev_t *ahci_ahci_create(ddf_dev_t *dev)
|
|---|
| 883 | {
|
|---|
| 884 | ahci_dev_t *ahci = malloc(sizeof(ahci_dev_t));
|
|---|
| 885 | if (!ahci)
|
|---|
| 886 | return NULL;
|
|---|
| 887 |
|
|---|
| 888 | bzero(ahci, sizeof(ahci_dev_t));
|
|---|
| 889 |
|
|---|
| 890 | ahci->dev = dev;
|
|---|
| 891 |
|
|---|
| 892 | hw_res_list_parsed_t hw_res_parsed;
|
|---|
| 893 | hw_res_list_parsed_init(&hw_res_parsed);
|
|---|
| 894 | if (hw_res_get_list_parsed(dev->parent_sess, &hw_res_parsed, 0) != EOK)
|
|---|
| 895 | goto error_get_res_parsed;
|
|---|
| 896 |
|
|---|
| 897 | /* Register interrupt handler */
|
|---|
| 898 | ahci_ranges[0].base = (size_t) hw_res_parsed.mem_ranges.ranges[0].address;
|
|---|
| 899 | ahci_ranges[0].size = sizeof(ahci_dev_t);
|
|---|
| 900 | ahci_cmds[0].addr =
|
|---|
| 901 | ((uint32_t *) (size_t) hw_res_parsed.mem_ranges.ranges[0].address) + 1;
|
|---|
| 902 | ahci_cmds[1].addr =
|
|---|
| 903 | ((uint32_t *) (size_t) hw_res_parsed.mem_ranges.ranges[0].address) + 2;
|
|---|
| 904 | ahci_cmds[2].addr = ahci_cmds[1].addr;
|
|---|
| 905 |
|
|---|
| 906 | irq_code_t ct;
|
|---|
| 907 | ct.cmdcount = 3;
|
|---|
| 908 | ct.cmds = ahci_cmds;
|
|---|
| 909 | ct.rangecount = 1;
|
|---|
| 910 | ct.ranges = ahci_ranges;
|
|---|
| 911 |
|
|---|
| 912 | int rc = register_interrupt_handler(dev, hw_res_parsed.irqs.irqs[0],
|
|---|
| 913 | ahci_interrupt, &ct);
|
|---|
| 914 |
|
|---|
| 915 | if (rc != EOK) {
|
|---|
| 916 | ddf_msg(LVL_ERROR, "Failed register_interrupt_handler function.");
|
|---|
| 917 | goto error_register_interrupt_handler;
|
|---|
| 918 | }
|
|---|
| 919 |
|
|---|
| 920 | if (ahci_pciintel_enable_interrupt(hw_res_parsed.irqs.irqs[0]) != EOK) {
|
|---|
| 921 | ddf_msg(LVL_ERROR, "Failed enable interupt.");
|
|---|
| 922 | goto error_enable_interrupt;
|
|---|
| 923 | }
|
|---|
| 924 |
|
|---|
| 925 | /* Map AHCI register. */
|
|---|
| 926 | physmem_map((void *) (size_t) (hw_res_parsed.mem_ranges.ranges[0].address),
|
|---|
| 927 | 8, AS_AREA_READ | AS_AREA_WRITE, (void **) &ahci->memregs);
|
|---|
| 928 | hw_res_list_parsed_clean(&hw_res_parsed);
|
|---|
| 929 |
|
|---|
| 930 | if (ahci->memregs == NULL)
|
|---|
| 931 | goto error_map_registers;
|
|---|
| 932 |
|
|---|
| 933 | ahci->timer = fibril_timer_create();
|
|---|
| 934 |
|
|---|
| 935 | return ahci;
|
|---|
| 936 |
|
|---|
| 937 | error_map_registers:
|
|---|
| 938 | error_enable_interrupt:
|
|---|
| 939 | error_register_interrupt_handler:
|
|---|
| 940 | hw_res_list_parsed_clean(&hw_res_parsed);
|
|---|
| 941 | error_get_res_parsed:
|
|---|
| 942 | return NULL;
|
|---|
| 943 | }
|
|---|
| 944 |
|
|---|
| 945 | static void ahci_ahci_init(ahci_dev_t *ahci)
|
|---|
| 946 | {
|
|---|
| 947 | /* Enable interrupt and bus mastering */
|
|---|
| 948 | ahci_pcireg_cmd_t cmd;
|
|---|
| 949 |
|
|---|
| 950 | pci_config_space_read_16(ahci->dev->parent_sess, AHCI_PCI_CMD, &cmd.u16);
|
|---|
| 951 | cmd.id = 0;
|
|---|
| 952 | cmd.bme = 1;
|
|---|
| 953 | pci_config_space_write_16(ahci->dev->parent_sess, AHCI_PCI_CMD, cmd.u16);
|
|---|
| 954 |
|
|---|
| 955 | /* Set master latency timer */
|
|---|
| 956 | pci_config_space_write_8(ahci->dev->parent_sess, AHCI_PCI_MLT, 32);
|
|---|
| 957 |
|
|---|
| 958 | /* Disable command completion coalescing feature. */
|
|---|
| 959 | ahci_ghc_ccc_ctl_t ccc;
|
|---|
| 960 | ccc.u32 = ahci->memregs->ghc.ccc_ctl;
|
|---|
| 961 | ccc.en = 0;
|
|---|
| 962 | ahci->memregs->ghc.ccc_ctl = ccc.u32;
|
|---|
| 963 |
|
|---|
| 964 | /* Enable AHCI and interrupt. */
|
|---|
| 965 | ahci->memregs->ghc.ghc = AHCI_GHC_GHC_AE | AHCI_GHC_GHC_IE;
|
|---|
| 966 |
|
|---|
| 967 | /* Enable timer. */
|
|---|
| 968 | fibril_timer_set(ahci->timer, AHCI_TIMER_TICKS, ahci_timer, ahci);
|
|---|
| 969 | }
|
|---|
| 970 |
|
|---|
| 971 | static int ahci_dev_add(ddf_dev_t *dev)
|
|---|
| 972 | {
|
|---|
| 973 | dev->parent_sess = devman_parent_device_connect(EXCHANGE_SERIALIZE,
|
|---|
| 974 | dev->handle, IPC_FLAG_BLOCKING);
|
|---|
| 975 | if (dev->parent_sess == NULL)
|
|---|
| 976 | return EINTR;
|
|---|
| 977 |
|
|---|
| 978 | ahci_dev_t *ahci = ahci_ahci_create(dev);
|
|---|
| 979 | if (ahci == NULL)
|
|---|
| 980 | return EINTR;
|
|---|
| 981 |
|
|---|
| 982 | dev->driver_data = ahci;
|
|---|
| 983 | ahci_ahci_init(ahci);
|
|---|
| 984 | ahci_sata_devices_create(ahci, dev);
|
|---|
| 985 |
|
|---|
| 986 | return EOK;
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | /*----------------------------------------------------------------------------*/
|
|---|
| 990 | /*-- Helpers and utilities ---------------------------------------------------*/
|
|---|
| 991 | /*----------------------------------------------------------------------------*/
|
|---|
| 992 |
|
|---|
| 993 | static void ahci_get_model_name(uint16_t *src, char *dst)
|
|---|
| 994 | {
|
|---|
| 995 | uint8_t model[40];
|
|---|
| 996 | bzero(model, 40);
|
|---|
| 997 |
|
|---|
| 998 | for (unsigned int i = 0; i < 20; i++) {
|
|---|
| 999 | uint16_t w = src[i];
|
|---|
| 1000 | model[2 * i] = w >> 8;
|
|---|
| 1001 | model[2 * i + 1] = w & 0x00ff;
|
|---|
| 1002 | }
|
|---|
| 1003 |
|
|---|
| 1004 | uint32_t len = 40;
|
|---|
| 1005 | while ((len > 0) && (model[len - 1] == 0x20))
|
|---|
| 1006 | len--;
|
|---|
| 1007 |
|
|---|
| 1008 | size_t pos = 0;
|
|---|
| 1009 | for (unsigned int i = 0; i < len; i++) {
|
|---|
| 1010 | uint8_t c = model[i];
|
|---|
| 1011 | if (c >= 0x80)
|
|---|
| 1012 | c = '?';
|
|---|
| 1013 |
|
|---|
| 1014 | chr_encode(c, dst, &pos, 40);
|
|---|
| 1015 | }
|
|---|
| 1016 |
|
|---|
| 1017 | dst[pos] = '\0';
|
|---|
| 1018 | }
|
|---|
| 1019 |
|
|---|
| 1020 | static int ahci_pciintel_enable_interrupt(int irq)
|
|---|
| 1021 | {
|
|---|
| 1022 | async_sess_t *irc_sess = NULL;
|
|---|
| 1023 | irc_sess = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_IRC, 0, 0);
|
|---|
| 1024 |
|
|---|
| 1025 | if (!irc_sess)
|
|---|
| 1026 | return EINTR;
|
|---|
| 1027 |
|
|---|
| 1028 | async_exch_t *exch = async_exchange_begin(irc_sess);
|
|---|
| 1029 | const int rc = async_req_1_0(exch, IRC_ENABLE_INTERRUPT, irq);
|
|---|
| 1030 | async_exchange_end(exch);
|
|---|
| 1031 |
|
|---|
| 1032 | async_hangup(irc_sess);
|
|---|
| 1033 | return rc;
|
|---|
| 1034 | }
|
|---|
| 1035 |
|
|---|
| 1036 | /*----------------------------------------------------------------------------*/
|
|---|
| 1037 | /*-- AHCI Main routine -------------------------------------------------------*/
|
|---|
| 1038 | /*----------------------------------------------------------------------------*/
|
|---|
| 1039 |
|
|---|
| 1040 | int main(int argc, char *argv[])
|
|---|
| 1041 | {
|
|---|
| 1042 | printf("%s: HelenOS AHCI device driver\n", NAME);
|
|---|
| 1043 | ddf_log_init(NAME, LVL_ERROR);
|
|---|
| 1044 | fibril_mutex_initialize(&sata_devices_count_lock);
|
|---|
| 1045 | return ddf_driver_main(&ahci_driver);
|
|---|
| 1046 | }
|
|---|