Changes in uspace/drv/block/ahci/ahci.c [730dce77:267f235] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/block/ahci/ahci.c
r730dce77 r267f235 34 34 #include <errno.h> 35 35 #include <stdio.h> 36 #include <devman.h>37 36 #include <ddf/interrupt.h> 38 37 #include <ddf/log.h> … … 49 48 #define NAME "ahci" 50 49 51 #define AHCI_TIMER_TICKS 100000000052 53 50 #define LO(ptr) \ 54 51 ((uint32_t) (((uint64_t) ((uintptr_t) (ptr))) & 0xffffffff)) … … 56 53 #define HI(ptr) \ 57 54 ((uint32_t) (((uint64_t) ((uintptr_t) (ptr))) >> 32)) 55 56 /** Interrupt pseudocode for a single port 57 * 58 * The interrupt handling works as follows: 59 * 60 * 1. Port interrupt status register is read 61 * (stored as arg2). 62 * 2. If port interrupt is indicated, then: 63 * 3. Port interrupt status register is cleared. 64 * 4. Global interrupt status register is read 65 * and cleared (any potential interrupts from 66 * other ports are reasserted automatically). 67 * 5. Port number is stored as arg1. 68 * 6. The interrupt is accepted. 69 * 70 */ 71 #define AHCI_PORT_CMDS(port) \ 72 { \ 73 /* Read port interrupt status register */ \ 74 .cmd = CMD_PIO_READ_32, \ 75 .addr = NULL, \ 76 .dstarg = 2 \ 77 }, \ 78 { \ 79 /* Check if port asserted interrupt */ \ 80 .cmd = CMD_PREDICATE, \ 81 .value = 5, \ 82 .srcarg = 2, \ 83 }, \ 84 { \ 85 /* Clear port interrupt status register */ \ 86 .cmd = CMD_PIO_WRITE_A_32, \ 87 .addr = NULL, \ 88 .srcarg = 2 \ 89 }, \ 90 { \ 91 /* Read global interrupt status register */ \ 92 .cmd = CMD_PIO_READ_32, \ 93 .addr = NULL, \ 94 .dstarg = 0 \ 95 }, \ 96 { \ 97 /* Clear global interrupt status register */ \ 98 .cmd = CMD_PIO_WRITE_A_32, \ 99 .addr = NULL, \ 100 .srcarg = 0 \ 101 }, \ 102 { \ 103 /* Indicate port interrupt assertion */ \ 104 .cmd = CMD_LOAD, \ 105 .value = (port), \ 106 .dstarg = 1 \ 107 }, \ 108 { \ 109 /* Accept the interrupt */ \ 110 .cmd = CMD_ACCEPT \ 111 } 58 112 59 113 static int ahci_get_sata_device_name(ddf_fun_t *, size_t, char *); … … 70 124 static void ahci_sata_devices_create(ahci_dev_t *, ddf_dev_t *); 71 125 static ahci_dev_t *ahci_ahci_create(ddf_dev_t *); 72 static void ahci_ahci_ init(ahci_dev_t *);126 static void ahci_ahci_hw_start(ahci_dev_t *); 73 127 74 128 static int ahci_dev_add(ddf_dev_t *); 75 129 76 130 static void ahci_get_model_name(uint16_t *, char *); 77 static int ahci_ pciintel_enable_interrupt(int);131 static int ahci_enable_interrupt(int); 78 132 79 133 static fibril_mutex_t sata_devices_count_lock; … … 105 159 }; 106 160 161 /** Get SATA structure from DDF function. */ 162 static sata_dev_t *fun_sata_dev(ddf_fun_t *fun) 163 { 164 return ddf_fun_data_get(fun); 165 } 166 167 /** Get AHCI structure from DDF device. */ 168 static ahci_dev_t *dev_ahci_dev(ddf_dev_t *dev) 169 { 170 return ddf_dev_data_get(dev); 171 } 172 173 /** Get SATA device name. 174 * 175 * @param fun Device function handling the call. 176 * @param sata_dev_name_length Length of the sata_dev_name buffer. 177 * @param sata_dev_name Buffer for SATA device name. 178 * 179 * @return EOK. 180 * 181 */ 107 182 static int ahci_get_sata_device_name(ddf_fun_t *fun, 108 183 size_t sata_dev_name_length, char *sata_dev_name) 109 184 { 110 sata_dev_t *sata = (sata_dev_t *) fun->driver_data;185 sata_dev_t *sata = fun_sata_dev(fun); 111 186 str_cpy(sata_dev_name, sata_dev_name_length, sata->model); 112 187 return EOK; 113 188 } 114 189 190 /** Get Number of blocks in SATA device. 191 * 192 * @param fun Device function handling the call. 193 * @param blocks Return number of blocks in SATA device. 194 * 195 * @return EOK. 196 * 197 */ 115 198 static int ahci_get_num_blocks(ddf_fun_t *fun, uint64_t *num_blocks) 116 199 { 117 sata_dev_t *sata = (sata_dev_t *) fun->driver_data;200 sata_dev_t *sata = fun_sata_dev(fun); 118 201 *num_blocks = sata->blocks; 119 202 return EOK; 120 203 } 121 204 205 /** Get SATA device block size. 206 * 207 * @param fun Device function handling the call. 208 * @param block_size Return block size. 209 * 210 * @return EOK. 211 * 212 */ 122 213 static int ahci_get_block_size(ddf_fun_t *fun, size_t *block_size) 123 214 { 124 sata_dev_t *sata = (sata_dev_t *) fun->driver_data;215 sata_dev_t *sata = fun_sata_dev(fun); 125 216 *block_size = sata->block_size; 126 217 return EOK; 127 218 } 128 219 220 /** Read data blocks into SATA device. 221 * 222 * @param fun Device function handling the call. 223 * @param blocknum Number of first block. 224 * @param count Number of blocks to read. 225 * @param buf Buffer for data. 226 * 227 * @return EOK if succeed, error code otherwise 228 * 229 */ 129 230 static int ahci_read_blocks(ddf_fun_t *fun, uint64_t blocknum, 130 231 size_t count, void *buf) 131 232 { 132 int rc = EOK; 133 sata_dev_t *sata = (sata_dev_t *) fun->driver_data; 233 sata_dev_t *sata = fun_sata_dev(fun); 134 234 135 235 void *phys; 136 236 void *ibuf; 137 138 dmamem_map_anonymous(sata->block_size, AS_AREA_READ | AS_AREA_WRITE, 237 int rc = dmamem_map_anonymous(sata->block_size, AS_AREA_READ | AS_AREA_WRITE, 139 238 0, &phys, (void **) &ibuf); 239 if (rc != EOK) { 240 ddf_msg(LVL_ERROR, "Cannot allocate read buffer."); 241 return rc; 242 } 243 140 244 bzero(buf, sata->block_size); 141 245 … … 157 261 } 158 262 263 /** Write data blocks into SATA device. 264 * 265 * @param fun Device function handling the call. 266 * @param blocknum Number of first block. 267 * @param count Number of blocks to write. 268 * @param buf Buffer with data. 269 * 270 * @return EOK if succeed, error code otherwise 271 * 272 */ 159 273 static int ahci_write_blocks(ddf_fun_t *fun, uint64_t blocknum, 160 274 size_t count, void *buf) 161 275 { 162 int rc = EOK; 163 sata_dev_t *sata = (sata_dev_t *) fun->driver_data; 276 sata_dev_t *sata = fun_sata_dev(fun); 164 277 165 278 void *phys; 166 279 void *ibuf; 167 168 dmamem_map_anonymous(sata->block_size, AS_AREA_READ | AS_AREA_WRITE, 280 int rc = dmamem_map_anonymous(sata->block_size, AS_AREA_READ | AS_AREA_WRITE, 169 281 0, &phys, (void **) &ibuf); 282 if (rc != EOK) { 283 ddf_msg(LVL_ERROR, "Cannot allocate write buffer."); 284 return rc; 285 } 170 286 171 287 fibril_mutex_lock(&sata->lock); … … 181 297 fibril_mutex_unlock(&sata->lock); 182 298 dmamem_unmap_anonymous(ibuf); 299 183 300 return rc; 184 301 } … … 188 305 /*----------------------------------------------------------------------------*/ 189 306 307 /** Wait for interrupt event. 308 * 309 * @param sata SATA device structure. 310 * 311 * @return Value of interrupt state register. 312 * 313 */ 314 static ahci_port_is_t ahci_wait_event(sata_dev_t *sata) 315 { 316 fibril_mutex_lock(&sata->event_lock); 317 318 sata->event_pxis = 0; 319 while (sata->event_pxis == 0) 320 fibril_condvar_wait(&sata->event_condvar, &sata->event_lock); 321 322 ahci_port_is_t pxis = sata->event_pxis; 323 324 if (ahci_port_is_permanent_error(pxis)) 325 sata->is_invalid_device = true; 326 327 fibril_mutex_unlock(&sata->event_lock); 328 329 return pxis; 330 } 331 332 /** Set AHCI registers for identifying SATA device. 333 * 334 * @param sata SATA device structure. 335 * @param phys Physical address of working buffer. 336 * 337 */ 190 338 static void ahci_identify_device_cmd(sata_dev_t *sata, void *phys) 191 339 { 192 volatile s td_command_frame_t *cmd =193 (s td_command_frame_t *) sata->cmd_table;194 195 cmd->fis_type = 0x27;196 cmd->c = 0x80;340 volatile sata_std_command_frame_t *cmd = 341 (sata_std_command_frame_t *) sata->cmd_table; 342 343 cmd->fis_type = SATA_CMD_FIS_TYPE; 344 cmd->c = SATA_CMD_FIS_COMMAND_INDICATOR; 197 345 cmd->command = 0xec; 198 346 cmd->features = 0; … … 212 360 prdt->data_address_upper = HI(phys); 213 361 prdt->reserved1 = 0; 214 prdt->dbc = 511;362 prdt->dbc = SATA_IDENTIFY_DEVICE_BUFFER_LENGTH - 1; 215 363 prdt->reserved2 = 0; 216 364 prdt->ioc = 0; 217 365 218 366 sata->cmd_header->prdtl = 1; 219 sata->cmd_header->flags = 0x402; 367 sata->cmd_header->flags = 368 AHCI_CMDHDR_FLAGS_CLEAR_BUSY_UPON_OK | 369 AHCI_CMDHDR_FLAGS_2DWCMD; 220 370 sata->cmd_header->bytesprocessed = 0; 221 371 372 /* Run command. */ 222 373 sata->port->pxsact |= 1; 223 374 sata->port->pxci |= 1; 224 375 } 225 376 377 /** Set AHCI registers for identifying packet SATA device. 378 * 379 * @param sata SATA device structure. 380 * @param phys Physical address of working buffer. 381 * 382 */ 226 383 static void ahci_identify_packet_device_cmd(sata_dev_t *sata, void *phys) 227 384 { 228 volatile s td_command_frame_t *cmd =229 (s td_command_frame_t *) sata->cmd_table;230 231 cmd->fis_type = 0x27;232 cmd->c = 0x80;385 volatile sata_std_command_frame_t *cmd = 386 (sata_std_command_frame_t *) sata->cmd_table; 387 388 cmd->fis_type = SATA_CMD_FIS_TYPE; 389 cmd->c = SATA_CMD_FIS_COMMAND_INDICATOR; 233 390 cmd->command = 0xa1; 234 391 cmd->features = 0; … … 248 405 prdt->data_address_upper = HI(phys); 249 406 prdt->reserved1 = 0; 250 prdt->dbc = 511;407 prdt->dbc = SATA_IDENTIFY_DEVICE_BUFFER_LENGTH - 1; 251 408 prdt->reserved2 = 0; 252 409 prdt->ioc = 0; 253 410 254 411 sata->cmd_header->prdtl = 1; 255 sata->cmd_header->flags = 0x402; 412 sata->cmd_header->flags = 413 AHCI_CMDHDR_FLAGS_CLEAR_BUSY_UPON_OK | 414 AHCI_CMDHDR_FLAGS_2DWCMD; 256 415 sata->cmd_header->bytesprocessed = 0; 257 416 417 /* Run command. */ 258 418 sata->port->pxsact |= 1; 259 419 sata->port->pxci |= 1; 260 420 } 261 421 422 /** Fill device identification in SATA device structure. 423 * 424 * @param sata SATA device structure. 425 * 426 * @return EOK if succeed, error code otherwise. 427 * 428 */ 262 429 static int ahci_identify_device(sata_dev_t *sata) 263 430 { 264 if (sata->i nvalid_device) {431 if (sata->is_invalid_device) { 265 432 ddf_msg(LVL_ERROR, 266 433 "Identify command device on invalid device"); … … 269 436 270 437 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); 438 sata_identify_data_t *idata; 439 int rc = dmamem_map_anonymous(SATA_IDENTIFY_DEVICE_BUFFER_LENGTH, 440 AS_AREA_READ | AS_AREA_WRITE, 0, &phys, (void **) &idata); 441 if (rc != EOK) { 442 ddf_msg(LVL_ERROR, "Cannot allocate buffer to identify device."); 443 return rc; 444 } 445 446 bzero(idata, SATA_IDENTIFY_DEVICE_BUFFER_LENGTH); 276 447 277 448 fibril_mutex_lock(&sata->lock); 278 449 279 450 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) { 451 ahci_port_is_t pxis = ahci_wait_event(sata); 452 453 if (sata->is_invalid_device) { 289 454 ddf_msg(LVL_ERROR, 290 455 "Unrecoverable error during ata identify device"); … … 294 459 if (ahci_port_is_tfes(pxis)) { 295 460 ahci_identify_packet_device_cmd(sata, phys); 461 pxis = ahci_wait_event(sata); 296 462 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))) { 463 if ((sata->is_invalid_device) || (ahci_port_is_error(pxis))) { 305 464 ddf_msg(LVL_ERROR, 306 465 "Unrecoverable error during ata identify packet device"); … … 308 467 } 309 468 310 sata->packet_device = true; 311 } else 312 sata->packet_device = false; 469 sata->is_packet_device = true; 470 } 313 471 314 472 ahci_get_model_name(idata->model_name, sata->model); … … 318 476 * only NCQ FPDMA mode is supported. 319 477 */ 320 if ((idata->sata_cap & np_cap_ncq) == 0) {478 if ((idata->sata_cap & sata_np_cap_ncq) == 0) { 321 479 ddf_msg(LVL_ERROR, "%s: NCQ must be supported", sata->model); 322 480 goto error; 323 481 } 324 482 325 if (sata->packet_device) { 483 uint16_t logsec = idata->physical_logic_sector_size; 484 if ((logsec & 0xc000) == 0x4000) { 485 /* Length of sector may be larger than 512 B */ 486 if (logsec & 0x0100) { 487 /* Size of sector is larger than 512 B */ 488 ddf_msg(LVL_ERROR, 489 "%s: Sector length other than 512 B not supported", 490 sata->model); 491 goto error; 492 } 493 494 if ((logsec & 0x0200) && ((logsec & 0x000f) != 0)) { 495 /* Physical sectors per logical sector is greather than 1 */ 496 ddf_msg(LVL_ERROR, 497 "%s: Sector length other than 512 B not supported", 498 sata->model); 499 goto error; 500 } 501 } 502 503 if (sata->is_packet_device) { 326 504 /* 327 505 * Due to QEMU limitation (as of 2012-06-22), 328 * only NCQ FPDMA mode supported - block size is 512 B,329 * not 2048 B!506 * only NCQ FPDMA mode supported - block size is 507 * 512 B, not 2048 B! 330 508 */ 331 sata->block_size = 512;509 sata->block_size = SATA_DEFAULT_SECTOR_SIZE; 332 510 sata->blocks = 0; 333 511 } else { 334 sata->block_size = 512;512 sata->block_size = SATA_DEFAULT_SECTOR_SIZE; 335 513 336 if ((idata->caps & rd_cap_lba) == 0) {514 if ((idata->caps & sata_rd_cap_lba) == 0) { 337 515 ddf_msg(LVL_ERROR, "%s: LBA for NCQ must be supported", 338 516 sata->model); 339 517 goto error; 340 } else if ((idata->cmd_set1 & cs1_addr48) == 0) {518 } else if ((idata->cmd_set1 & sata_cs1_addr48) == 0) { 341 519 sata->blocks = (uint32_t) idata->total_lba28_0 | 342 520 ((uint32_t) idata->total_lba28_1 << 16); … … 351 529 352 530 uint8_t udma_mask = idata->udma & 0x007f; 531 sata->highest_udma_mode = (uint8_t) -1; 353 532 if (udma_mask == 0) { 354 533 ddf_msg(LVL_ERROR, … … 357 536 goto error; 358 537 } else { 359 for (u nsigned int i = 0; i < 7; i++) {538 for (uint8_t i = 0; i < 7; i++) { 360 539 if (udma_mask & (1 << i)) 361 540 sata->highest_udma_mode = i; … … 375 554 } 376 555 556 /** Set AHCI registers for setting SATA device transfer mode. 557 * 558 * @param sata SATA device structure. 559 * @param phys Physical address of working buffer. 560 * @param mode Required mode. 561 * 562 */ 377 563 static void ahci_set_mode_cmd(sata_dev_t *sata, void* phys, uint8_t mode) 378 564 { 379 volatile s td_command_frame_t *cmd =380 (s td_command_frame_t *) sata->cmd_table;381 382 cmd->fis_type = 0x27;383 cmd->c = 0x80;565 volatile sata_std_command_frame_t *cmd = 566 (sata_std_command_frame_t *) sata->cmd_table; 567 568 cmd->fis_type = SATA_CMD_FIS_TYPE; 569 cmd->c = SATA_CMD_FIS_COMMAND_INDICATOR; 384 570 cmd->command = 0xef; 385 571 cmd->features = 0x03; … … 393 579 cmd->reserved2 = 0; 394 580 395 volatile ahci_cmd_prdt_t *prdt =581 volatile ahci_cmd_prdt_t *prdt = 396 582 (ahci_cmd_prdt_t *) (&sata->cmd_table[0x20]); 397 583 … … 399 585 prdt->data_address_upper = HI(phys); 400 586 prdt->reserved1 = 0; 401 prdt->dbc = 511;587 prdt->dbc = SATA_SET_FEATURE_BUFFER_LENGTH - 1; 402 588 prdt->reserved2 = 0; 403 589 prdt->ioc = 0; 404 590 405 591 sata->cmd_header->prdtl = 1; 406 sata->cmd_header->flags = 0x402; 592 sata->cmd_header->flags = 593 AHCI_CMDHDR_FLAGS_CLEAR_BUSY_UPON_OK | 594 AHCI_CMDHDR_FLAGS_2DWCMD; 407 595 sata->cmd_header->bytesprocessed = 0; 408 596 597 /* Run command. */ 409 598 sata->port->pxsact |= 1; 410 599 sata->port->pxci |= 1; 411 600 } 412 601 602 /** Set highest ultra DMA mode supported by SATA device. 603 * 604 * @param sata SATA device structure. 605 * 606 * @return EOK if succeed, error code otherwise 607 * 608 */ 413 609 static int ahci_set_highest_ultra_dma_mode(sata_dev_t *sata) 414 610 { 415 if (sata->i nvalid_device) {611 if (sata->is_invalid_device) { 416 612 ddf_msg(LVL_ERROR, 417 613 "%s: Setting highest UDMA mode on invalid device", … … 420 616 } 421 617 618 if (sata->highest_udma_mode == (uint8_t) -1) { 619 ddf_msg(LVL_ERROR, 620 "%s: No AHCI UDMA support.", sata->model); 621 return EINTR; 622 } 623 624 if (sata->highest_udma_mode > 6) { 625 ddf_msg(LVL_ERROR, 626 "%s: Unknown AHCI UDMA mode.", sata->model); 627 return EINTR; 628 } 629 422 630 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); 631 sata_identify_data_t *idata; 632 int rc = dmamem_map_anonymous(SATA_SET_FEATURE_BUFFER_LENGTH, 633 AS_AREA_READ | AS_AREA_WRITE, 0, &phys, (void **) &idata); 634 if (rc != EOK) { 635 ddf_msg(LVL_ERROR, "Cannot allocate buffer for device set mode."); 636 return rc; 637 } 638 639 bzero(idata, SATA_SET_FEATURE_BUFFER_LENGTH); 428 640 429 641 fibril_mutex_lock(&sata->lock); … … 431 643 uint8_t mode = 0x40 | (sata->highest_udma_mode & 0x07); 432 644 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) { 645 ahci_port_is_t pxis = ahci_wait_event(sata); 646 647 if (sata->is_invalid_device) { 442 648 ddf_msg(LVL_ERROR, 443 649 "%s: Unrecoverable error during set highest UDMA mode", … … 464 670 } 465 671 672 /** Set AHCI registers for reading one sector from the SATA device using FPDMA. 673 * 674 * @param sata SATA device structure. 675 * @param phys Physical address of buffer for sector data. 676 * @param blocknum Block number to read. 677 * 678 */ 466 679 static void ahci_rb_fpdma_cmd(sata_dev_t *sata, void *phys, uint64_t blocknum) 467 680 { 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;681 volatile sata_ncq_command_frame_t *cmd = 682 (sata_ncq_command_frame_t *) sata->cmd_table; 683 684 cmd->fis_type = SATA_CMD_FIS_TYPE; 685 cmd->c = SATA_CMD_FIS_COMMAND_INDICATOR; 473 686 cmd->command = 0x60; 474 687 cmd->tag = 0; … … 503 716 504 717 sata->cmd_header->prdtl = 1; 505 sata->cmd_header->flags = 0x405; 718 sata->cmd_header->flags = 719 AHCI_CMDHDR_FLAGS_CLEAR_BUSY_UPON_OK | 720 AHCI_CMDHDR_FLAGS_5DWCMD; 506 721 sata->cmd_header->bytesprocessed = 0; 507 722 … … 510 725 } 511 726 727 /** Read one sector from the SATA device using FPDMA. 728 * 729 * @param sata SATA device structure. 730 * @param phys Physical address of buffer for sector data. 731 * @param blocknum Block number to read. 732 * 733 * @return EOK if succeed, error code otherwise 734 * 735 */ 512 736 static int ahci_rb_fpdma(sata_dev_t *sata, void *phys, uint64_t blocknum) 513 737 { 514 if (sata->i nvalid_device) {738 if (sata->is_invalid_device) { 515 739 ddf_msg(LVL_ERROR, 516 740 "%s: FPDMA read from invalid device", sata->model); … … 519 743 520 744 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))) { 745 ahci_port_is_t pxis = ahci_wait_event(sata); 746 747 if ((sata->is_invalid_device) || (ahci_port_is_error(pxis))) { 530 748 ddf_msg(LVL_ERROR, 531 749 "%s: Unrecoverable error during FPDMA read", sata->model); … … 536 754 } 537 755 756 /** Set AHCI registers for writing one sector to the SATA device, use FPDMA. 757 * 758 * @param sata SATA device structure. 759 * @param phys Physical address of buffer with sector data. 760 * @param blocknum Block number to write. 761 * 762 * @return EOK if succeed, error code otherwise 763 * 764 */ 538 765 static void ahci_wb_fpdma_cmd(sata_dev_t *sata, void *phys, uint64_t blocknum) 539 766 { 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;767 volatile sata_ncq_command_frame_t *cmd = 768 (sata_ncq_command_frame_t *) sata->cmd_table; 769 770 cmd->fis_type = SATA_CMD_FIS_TYPE; 771 cmd->c = SATA_CMD_FIS_COMMAND_INDICATOR; 545 772 cmd->command = 0x61; 546 773 cmd->tag = 0; … … 564 791 cmd->lba5 = (blocknum >> 40) & 0xff; 565 792 566 volatile ahci_cmd_prdt_t * 793 volatile ahci_cmd_prdt_t *prdt = 567 794 (ahci_cmd_prdt_t *) (&sata->cmd_table[0x20]); 568 795 … … 575 802 576 803 sata->cmd_header->prdtl = 1; 577 sata->cmd_header->flags = 0x445; 804 sata->cmd_header->flags = 805 AHCI_CMDHDR_FLAGS_CLEAR_BUSY_UPON_OK | 806 AHCI_CMDHDR_FLAGS_WRITE | 807 AHCI_CMDHDR_FLAGS_5DWCMD; 578 808 sata->cmd_header->bytesprocessed = 0; 579 809 … … 582 812 } 583 813 814 /** Write one sector into the SATA device, use FPDMA. 815 * 816 * @param sata SATA device structure. 817 * @param phys Physical addres of buffer with sector data. 818 * @param blocknum Block number to write. 819 * 820 * @return EOK if succeed, error code otherwise 821 * 822 */ 584 823 static int ahci_wb_fpdma(sata_dev_t *sata, void *phys, uint64_t blocknum) 585 824 { 586 if (sata->i nvalid_device) {825 if (sata->is_invalid_device) { 587 826 ddf_msg(LVL_ERROR, 588 827 "%s: FPDMA write to invalid device", sata->model); … … 591 830 592 831 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))) { 832 ahci_port_is_t pxis = ahci_wait_event(sata); 833 834 if ((sata->is_invalid_device) || (ahci_port_is_error(pxis))) { 602 835 ddf_msg(LVL_ERROR, 603 836 "%s: Unrecoverable error during FPDMA write", sata->model); … … 609 842 610 843 /*----------------------------------------------------------------------------*/ 611 /*-- Interrupts and timer unified handling-----------------------------------*/844 /*-- Interrupts handling -----------------------------------------------------*/ 612 845 /*----------------------------------------------------------------------------*/ 613 846 … … 615 848 { 616 849 .base = 0, 617 .size = 32,850 .size = 0, 618 851 } 619 852 }; 620 853 621 854 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 } 855 AHCI_PORT_CMDS(0), 856 AHCI_PORT_CMDS(1), 857 AHCI_PORT_CMDS(2), 858 AHCI_PORT_CMDS(3), 859 AHCI_PORT_CMDS(4), 860 AHCI_PORT_CMDS(5), 861 AHCI_PORT_CMDS(6), 862 AHCI_PORT_CMDS(7), 863 AHCI_PORT_CMDS(8), 864 AHCI_PORT_CMDS(9), 865 AHCI_PORT_CMDS(10), 866 AHCI_PORT_CMDS(11), 867 AHCI_PORT_CMDS(12), 868 AHCI_PORT_CMDS(13), 869 AHCI_PORT_CMDS(14), 870 AHCI_PORT_CMDS(15), 871 AHCI_PORT_CMDS(16), 872 AHCI_PORT_CMDS(17), 873 AHCI_PORT_CMDS(18), 874 AHCI_PORT_CMDS(19), 875 AHCI_PORT_CMDS(20), 876 AHCI_PORT_CMDS(21), 877 AHCI_PORT_CMDS(22), 878 AHCI_PORT_CMDS(23), 879 AHCI_PORT_CMDS(24), 880 AHCI_PORT_CMDS(25), 881 AHCI_PORT_CMDS(26), 882 AHCI_PORT_CMDS(27), 883 AHCI_PORT_CMDS(28), 884 AHCI_PORT_CMDS(29), 885 AHCI_PORT_CMDS(30), 886 AHCI_PORT_CMDS(31) 642 887 }; 643 888 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; 889 /** AHCI interrupt handler. 890 * 891 * @param dev DDF device structure. 892 * @param iid The IPC call id. 893 * @param icall The IPC call structure. 894 * 895 */ 896 static void ahci_interrupt(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *icall) 897 { 898 ahci_dev_t *ahci = dev_ahci_dev(dev); 899 unsigned int port = IPC_GET_ARG1(*icall); 900 ahci_port_is_t pxis = IPC_GET_ARG2(*icall); 901 902 if (port >= AHCI_MAX_PORTS) 903 return; 904 905 sata_dev_t *sata = (sata_dev_t *) ahci->sata_devs[port]; 906 if (sata == NULL) 907 return; 908 909 /* Evaluate port event */ 910 if ((ahci_port_is_end_of_operation(pxis)) || 911 (ahci_port_is_error(pxis))) { 912 fibril_mutex_lock(&sata->event_lock); 670 913 671 pxis.u32 = ahci->memregs->ports[i].pxis;672 ahci->memregs->ports[i].pxis = pxis.u32;914 sata->event_pxis = pxis; 915 fibril_condvar_signal(&sata->event_condvar); 673 916 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; 917 fibril_mutex_unlock(&sata->event_lock); 918 } 730 919 } 731 920 … … 734 923 /*----------------------------------------------------------------------------*/ 735 924 736 static sata_dev_t *ahci_sata_device_allocate(volatile ahci_port_t *port) 925 /** Allocate SATA device structure with buffers for hardware. 926 * 927 * @param port AHCI port structure 928 * 929 * @return SATA device structure if succeed, NULL otherwise. 930 * 931 */ 932 static sata_dev_t *ahci_sata_allocate(ahci_dev_t *ahci, volatile ahci_port_t *port) 737 933 { 738 934 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)); 935 void *phys = NULL; 936 void *virt_fb = NULL; 937 void *virt_cmd = NULL; 938 void *virt_table = NULL; 939 ddf_fun_t *fun; 940 941 fun = ddf_fun_create(ahci->dev, fun_exposed, NULL); 942 943 sata_dev_t *sata = ddf_fun_data_alloc(fun, sizeof(sata_dev_t)); 745 944 if (sata == NULL) 746 945 return NULL; 747 946 748 bzero(sata, sizeof(sata_dev_t)); 749 947 sata->fun = fun; 750 948 sata->port = port; 751 949 … … 763 961 rc = dmamem_map_anonymous(size, AS_AREA_READ | AS_AREA_WRITE, 0, 764 962 &phys, &virt_cmd); 765 766 963 if (rc != EOK) 767 964 goto error_cmd; … … 775 972 rc = dmamem_map_anonymous(size, AS_AREA_READ | AS_AREA_WRITE, 0, 776 973 &phys, &virt_table); 777 778 974 if (rc != EOK) 779 975 goto error_table; … … 793 989 free(sata); 794 990 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, 991 } 992 993 /** Initialize and start SATA hardware device. 994 * 995 * @param sata SATA device structure. 996 * 997 */ 998 static void ahci_sata_hw_start(sata_dev_t *sata) 999 { 1000 ahci_port_cmd_t pxcmd; 1001 1002 pxcmd.u32 = sata->port->pxcmd; 1003 1004 /* Frame receiver disabled. */ 1005 pxcmd.fre = 0; 1006 1007 /* Disable process the command list. */ 1008 pxcmd.st = 0; 1009 1010 sata->port->pxcmd = pxcmd.u32; 1011 1012 /* Clear interrupt status. */ 1013 sata->port->pxis = 0xffffffff; 1014 1015 /* Clear error status. */ 1016 sata->port->pxserr = 0xffffffff; 1017 1018 /* Enable all interrupts. */ 1019 sata->port->pxie = 0xffffffff; 1020 1021 /* Frame receiver enabled. */ 1022 pxcmd.fre = 1; 1023 1024 /* Enable process the command list. */ 1025 pxcmd.st = 1; 1026 1027 sata->port->pxcmd = pxcmd.u32; 1028 } 1029 1030 /** Create and initialize connected SATA structure device 1031 * 1032 * @param ahci AHCI device structure. 1033 * @param dev DDF device structure. 1034 * @param port AHCI port structure. 1035 * @param port_num Number of AHCI port with existing SATA device. 1036 * 1037 * @return EOK if succeed, error code otherwise. 1038 * 1039 */ 1040 static int ahci_sata_create(ahci_dev_t *ahci, ddf_dev_t *dev, 803 1041 volatile ahci_port_t *port, unsigned int port_num) 804 1042 { 805 1043 ddf_fun_t *fun = NULL; 806 sata_dev_t *sata = ahci_sata_device_allocate(port); 807 1044 int rc; 1045 1046 sata_dev_t *sata = ahci_sata_allocate(ahci, port); 808 1047 if (sata == NULL) 809 1048 return EINTR; … … 814 1053 ahci->sata_devs[port_num] = sata; 815 1054 1055 /* Initialize synchronization structures */ 816 1056 fibril_mutex_initialize(&sata->lock); 817 1057 fibril_mutex_initialize(&sata->event_lock); 818 1058 fibril_condvar_initialize(&sata->event_condvar); 819 1059 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 1060 ahci_sata_hw_start(sata); 1061 1062 /* Identify device. */ 827 1063 if (ahci_identify_device(sata) != EOK) 828 1064 goto error; 829 1065 1066 /* Set required UDMA mode */ 830 1067 if (ahci_set_highest_ultra_dma_mode(sata) != EOK) 831 1068 goto error; 832 1069 833 /* Add sata device to system.*/834 char sata_dev_name[1 024];835 snprintf(sata_dev_name, 1 024, "ahci_%u", sata_devices_count);1070 /* Add device to the system */ 1071 char sata_dev_name[16]; 1072 snprintf(sata_dev_name, 16, "ahci_%u", sata_devices_count); 836 1073 837 1074 fibril_mutex_lock(&sata_devices_count_lock); … … 839 1076 fibril_mutex_unlock(&sata_devices_count_lock); 840 1077 841 fun = ddf_fun_create(dev, fun_exposed, sata_dev_name);842 if ( fun == NULL) {843 ddf_msg(LVL_ERROR, "Failed creating function.");1078 rc= ddf_fun_set_name(sata->fun, sata_dev_name); 1079 if (rc != EOK) { 1080 ddf_msg(LVL_ERROR, "Failed setting function name."); 844 1081 goto error; 845 1082 } 846 1083 847 fun->ops = &ahci_ops;848 fun->driver_data = sata;849 intrc = ddf_fun_bind(fun);1084 ddf_fun_set_ops(fun, &ahci_ops); 1085 1086 rc = ddf_fun_bind(fun); 850 1087 if (rc != EOK) { 851 1088 ddf_msg(LVL_ERROR, "Failed binding function."); … … 856 1093 857 1094 error: 858 sata->i nvalid_device = true;1095 sata->is_invalid_device = true; 859 1096 if (fun != NULL) 860 1097 ddf_fun_destroy(fun); … … 863 1100 } 864 1101 1102 /** Create and initialize all SATA structure devices for connected SATA drives. 1103 * 1104 * @param ahci AHCI device structure. 1105 * @param dev DDF device structure. 1106 * 1107 */ 865 1108 static void ahci_sata_devices_create(ahci_dev_t *ahci, ddf_dev_t *dev) 866 1109 { 867 for (unsigned int port_num = 0; port_num < 32; port_num++) {1110 for (unsigned int port_num = 0; port_num < AHCI_MAX_PORTS; port_num++) { 868 1111 /* Active ports only */ 869 1112 if (!(ahci->memregs->ghc.pi & (1 << port_num))) … … 873 1116 874 1117 /* Active devices only */ 875 if ((port->pxssts & 0x0f) != 3) 1118 ahci_port_ssts_t pxssts; 1119 pxssts.u32 = port->pxssts; 1120 if (pxssts.det != AHCI_PORT_SSTS_DET_ACTIVE) 876 1121 continue; 877 1122 878 ahci_sata_device_create(ahci, dev, port, port_num); 879 } 880 } 881 1123 ahci_sata_create(ahci, dev, port, port_num); 1124 } 1125 } 1126 1127 /** Create AHCI device structure, intialize it and register interrupt routine. 1128 * 1129 * @param dev DDF device structure. 1130 * 1131 * @return AHCI device structure if succeed, NULL otherwise. 1132 * 1133 */ 882 1134 static ahci_dev_t *ahci_ahci_create(ddf_dev_t *dev) 883 1135 { 884 ahci_dev_t *ahci = malloc(sizeof(ahci_dev_t));1136 ahci_dev_t *ahci = ddf_dev_data_alloc(dev, sizeof(ahci_dev_t)); 885 1137 if (!ahci) 886 1138 return NULL; 887 1139 888 bzero(ahci, sizeof(ahci_dev_t)); 1140 /* Connect to parent device */ 1141 ahci->parent_sess = ddf_dev_parent_sess_create(dev, EXCHANGE_SERIALIZE); 1142 if (ahci->parent_sess == NULL) 1143 return NULL; 889 1144 890 1145 ahci->dev = dev; … … 892 1147 hw_res_list_parsed_t hw_res_parsed; 893 1148 hw_res_list_parsed_init(&hw_res_parsed); 894 if (hw_res_get_list_parsed( dev->parent_sess, &hw_res_parsed, 0) != EOK)1149 if (hw_res_get_list_parsed(ahci->parent_sess, &hw_res_parsed, 0) != EOK) 895 1150 goto error_get_res_parsed; 1151 1152 /* Map AHCI registers. */ 1153 ahci->memregs = NULL; 1154 1155 physmem_map((void *) (size_t) (hw_res_parsed.mem_ranges.ranges[0].address), 1156 AHCI_MEMREGS_PAGES_COUNT, AS_AREA_READ | AS_AREA_WRITE, 1157 (void **) &ahci->memregs); 1158 if (ahci->memregs == NULL) 1159 goto error_map_registers; 896 1160 897 1161 /* Register interrupt handler */ 898 1162 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; 1163 ahci_ranges[0].size = sizeof(ahci_memregs_t); 1164 1165 for (unsigned int port = 0; port < AHCI_MAX_PORTS; port++) { 1166 size_t base = port * 7; 1167 1168 ahci_cmds[base].addr = 1169 ((uint32_t *) (size_t) hw_res_parsed.mem_ranges.ranges[0].address) + 1170 AHCI_PORTS_REGISTERS_OFFSET + port * AHCI_PORT_REGISTERS_SIZE + 1171 AHCI_PORT_IS_REGISTER_OFFSET; 1172 ahci_cmds[base + 2].addr = ahci_cmds[base].addr; 1173 1174 ahci_cmds[base + 3].addr = 1175 ((uint32_t *) (size_t) hw_res_parsed.mem_ranges.ranges[0].address) + 1176 AHCI_GHC_IS_REGISTER_OFFSET; 1177 ahci_cmds[base + 4].addr = ahci_cmds[base + 3].addr; 1178 } 905 1179 906 1180 irq_code_t ct; 907 ct.cmdcount = 3;1181 ct.cmdcount = sizeof(ahci_cmds) / sizeof(irq_cmd_t); 908 1182 ct.cmds = ahci_cmds; 909 ct.rangecount = 1;1183 ct.rangecount = sizeof(ahci_ranges) / sizeof(irq_pio_range_t); 910 1184 ct.ranges = ahci_ranges; 911 1185 912 1186 int rc = register_interrupt_handler(dev, hw_res_parsed.irqs.irqs[0], 913 1187 ahci_interrupt, &ct); 914 915 1188 if (rc != EOK) { 916 ddf_msg(LVL_ERROR, "Failed register _interrupt_handler function.");1189 ddf_msg(LVL_ERROR, "Failed registering interrupt handler."); 917 1190 goto error_register_interrupt_handler; 918 1191 } 919 1192 920 if (ahci_pciintel_enable_interrupt(hw_res_parsed.irqs.irqs[0]) != EOK) { 1193 rc = ahci_enable_interrupt(hw_res_parsed.irqs.irqs[0]); 1194 if (rc != EOK) { 921 1195 ddf_msg(LVL_ERROR, "Failed enable interupt."); 922 1196 goto error_enable_interrupt; 923 1197 } 924 1198 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 1199 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 1200 return ahci; 936 1201 1202 error_enable_interrupt: 1203 unregister_interrupt_handler(dev, hw_res_parsed.irqs.irqs[0]); 1204 1205 error_register_interrupt_handler: 1206 // FIXME: unmap physical memory 1207 937 1208 error_map_registers: 938 error_enable_interrupt:939 error_register_interrupt_handler:940 1209 hw_res_list_parsed_clean(&hw_res_parsed); 1210 941 1211 error_get_res_parsed: 1212 free(ahci); 942 1213 return NULL; 943 1214 } 944 1215 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. */ 1216 /** Initialize and start AHCI hardware device. 1217 * 1218 * @param ahci AHCI device. 1219 * 1220 */ 1221 static void ahci_ahci_hw_start(ahci_dev_t *ahci) 1222 { 1223 /* Disable command completion coalescing feature */ 959 1224 ahci_ghc_ccc_ctl_t ccc; 1225 960 1226 ccc.u32 = ahci->memregs->ghc.ccc_ctl; 961 1227 ccc.en = 0; 962 1228 ahci->memregs->ghc.ccc_ctl = ccc.u32; 963 1229 1230 /* Set master latency timer. */ 1231 pci_config_space_write_8(ahci->parent_sess, AHCI_PCI_MLT, 32); 1232 1233 /* Enable PCI interrupt and bus mastering */ 1234 ahci_pcireg_cmd_t cmd; 1235 1236 pci_config_space_read_16(ahci->parent_sess, AHCI_PCI_CMD, &cmd.u16); 1237 cmd.id = 0; 1238 cmd.bme = 1; 1239 pci_config_space_write_16(ahci->parent_sess, AHCI_PCI_CMD, cmd.u16); 1240 964 1241 /* 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 1242 ahci->memregs->ghc.ghc = AHCI_GHC_GHC_AE | AHCI_GHC_GHC_IE; 1243 } 1244 1245 /** AHCI device driver initialization 1246 * 1247 * Create and initialize all SATA structure devices for connected 1248 * SATA drives. 1249 * 1250 * @param dev DDF device structure. 1251 * 1252 * @return EOK if succeed, error code otherwise. 1253 * 1254 */ 971 1255 static int ahci_dev_add(ddf_dev_t *dev) 972 1256 { 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 1257 ahci_dev_t *ahci = ahci_ahci_create(dev); 979 1258 if (ahci == NULL) 980 return EINTR; 981 982 dev->driver_data = ahci; 983 ahci_ahci_init(ahci); 1259 goto error; 1260 1261 /* Start AHCI hardware. */ 1262 ahci_ahci_hw_start(ahci); 1263 1264 /* Create device structures for sata devices attached to AHCI. */ 984 1265 ahci_sata_devices_create(ahci, dev); 985 1266 986 1267 return EOK; 1268 1269 error: 1270 return EINTR; 987 1271 } 988 1272 … … 991 1275 /*----------------------------------------------------------------------------*/ 992 1276 1277 /** Convert SATA model name 1278 * 1279 * Convert SATA model name from machine format returned by 1280 * identify device command to human readable form. 1281 * 1282 * @param src Source buffer with device name in machine format. 1283 * @param dst Buffer for human readable string, minimum size is 41 chars. 1284 * 1285 */ 993 1286 static void ahci_get_model_name(uint16_t *src, char *dst) 994 1287 { … … 1018 1311 } 1019 1312 1020 static int ahci_pciintel_enable_interrupt(int irq) 1313 /** Enable interrupt using SERVICE_IRC. 1314 * 1315 * @param irq Requested irq number. 1316 * 1317 * @return EOK if succeed, error code otherwise. 1318 * 1319 */ 1320 static int ahci_enable_interrupt(int irq) 1021 1321 { 1022 1322 async_sess_t *irc_sess = NULL; 1023 1323 irc_sess = service_connect_blocking(EXCHANGE_SERIALIZE, SERVICE_IRC, 0, 0); 1024 1025 1324 if (!irc_sess) 1026 1325 return EINTR; … … 1041 1340 { 1042 1341 printf("%s: HelenOS AHCI device driver\n", NAME); 1043 ddf_log_init(NAME , LVL_ERROR);1342 ddf_log_init(NAME); 1044 1343 fibril_mutex_initialize(&sata_devices_count_lock); 1045 1344 return ddf_driver_main(&ahci_driver);
Note:
See TracChangeset
for help on using the changeset viewer.