source: rtems/c/src/libchip/ide/ata.c @ 92c70b9

4.104.11
Last change on this file since 92c70b9 was 92c70b9, checked in by Chris Johns <chrisj@…>, on Apr 29, 2009 at 8:36:45 AM

2009-04-29 Chris Johns <chrisj@…>

  • libchip/ide/ata.c: Remove a detection test that fails in qemu.
  • libchip/ide/ide_controller.c: Change the status detection and add a message if the transfer fails. There is currently no error handle at the IDE drvier level.
  • Property mode set to 100644
File size: 50.0 KB
RevLine 
[109fc26]1/*
2 * ata.c
3 *
[a3d3d9a]4 * ATA RTEMS driver. ATA driver is hardware independant implementation of
5 * ATA-2 standart, working draft X3T10/0948D, revision 4c. ATA driver bases
[109fc26]6 * on RTEMS IDE controller driver.
7 *
8 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
9 * Authors: Eugeny S. Mints <Eugeny.Mints@oktet.ru>
10 *
11 * The license and distribution terms for this file may be
12 * found in the file LICENSE in this distribution or at
[94365d9]13 * http://www.rtems.com/license/LICENSE.
[a3d3d9a]14 *
[109fc26]15 * $Id$
[a3d3d9a]16 *
[109fc26]17 */
18#include <errno.h>
[4caeb10]19#include <rtems/chain.h>
[109fc26]20#include <assert.h>
21#include <string.h> /* for "memset" declaration */
22
23#include <rtems/diskdevs.h>
24#include <rtems/blkdev.h>
25#include <libchip/ide_ctrl_io.h>
26#include <libchip/ide_ctrl_cfg.h>
27#include "ata_internal.h"
28#include <libchip/ata.h>
[f031251c]29/* #define DEBUG */
[109fc26]30
31#ifdef DEBUG
32#include <stdio.h>
33#endif
34
[d5ed9e1]35/*
36 * FIXME: make this better...
37 * find out, which exception model is used
38 * assume, that all i386 BSPs use new exception handling
39 * assume, that some PPC BSPs use new exception handling
40 * assume, that all other BSPs use old exception handling
41 */
42#if defined(_OLD_EXCEPTIONS) || (!defined(__i386__) && !defined(__PPC__))
43
44#define ATA_USE_OLD_EXCEPTIONS
45#endif
46
47#if !defined(ATA_USE_OLD_EXCEPTIONS)
48#include <bsp/irq.h>
49#define ATA_IRQ_CHAIN_MAX_CNT 4 /* support up to 4 ATA devices */
50typedef struct {
51  rtems_irq_number name;
[b9c38ed]52  rtems_chain_control irq_chain;
[d5ed9e1]53} ata_irq_chain_t;
54
55ata_irq_chain_t ata_irq_chain[ATA_IRQ_CHAIN_MAX_CNT];
56int ata_irq_chain_cnt = 0;
57#endif
58
[109fc26]59#define SAFE
[3735f08]60#define SAFE_MUTEX
61
[109fc26]62#ifdef SAFE
[3735f08]63#ifdef SAFE_MUTEX
64static rtems_id ata_lock;
65static void
[142025c]66rtems_ata_lock (void)
[3735f08]67{
68  rtems_status_code sc = rtems_semaphore_obtain (ata_lock,
69                                                 RTEMS_WAIT,
70                                                 RTEMS_NO_TIMEOUT);
71  if (sc != RTEMS_SUCCESSFUL)
72    rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR);
73}
74
75static void
[142025c]76rtems_ata_unlock (void)
[3735f08]77{
78  rtems_status_code sc = rtems_semaphore_release (ata_lock);
79  if (sc != RTEMS_SUCCESSFUL)
80    rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR);
81}
82
83#define RTEMS_ATA_LOCK_ATTRIBS \
84  (RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE | \
85   RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL)
86
87#define PREEMPTION_KEY(key)
88#define DISABLE_PREEMPTION(key) rtems_ata_lock ()
89#define ENABLE_PREEMPTION(key)  rtems_ata_unlock ()
90
91#else /* !SAFE_MUTEX */
[109fc26]92typedef rtems_mode preemption_key;
93
[3735f08]94#define PREEMPTION_KEY(key) preemption_key key
95
96#define DISABLE_PREEMPTION(key)                                        \
[109fc26]97    do {                                                               \
98        rtems_task_mode(RTEMS_NO_PREEMPT, RTEMS_PREEMPT_MASK, &(key)); \
99    } while (0)
100
101#define ENABLE_PREEMPTION(key) \
102    do {                                                        \
103        rtems_mode temp;                                        \
104        rtems_task_mode((key), RTEMS_PREEMPT_MASK, &temp);      \
105    } while (0)
[3735f08]106#endif
107#else /* !SAFE */
[6640459d]108typedef bool preemption_key;
[109fc26]109
[3735f08]110#define PREEMPTION_KEY(key) preemption_key key
111
[109fc26]112#define DISABLE_PREEMPTION(key) \
113    do {                                             \
114        (key) = _Thread_Executing->is_preemptible;   \
115        _Thread_Executing->is_preemptible = 0;       \
116    } while (0)
[a3d3d9a]117
[109fc26]118#define ENABLE_PREEMPTION(key) \
119    do {                                             \
120        _Thread_Executing->is_preemptible = (key);   \
121        if (_Thread_Evaluate_mode())                 \
122            _Thread_Dispatch();                      \
123    } while (0)
124
125#endif
126
127/* FIXME: case if ATA device is FLASH device need more attention */
128#undef ATA_DEV_IS_FLASH_DISK
129
130/* Block device request with a single buffer provided */
131typedef struct blkdev_request1 {
[3735f08]132    rtems_blkdev_request   req;
133    rtems_blkdev_sg_buffer sg[1];
[109fc26]134} blkdev_request1;
135
136
137/* Array indexed by controllers minor number */
138static ata_ide_ctrl_t ata_ide_ctrls[IDE_CTRL_MAX_MINOR_NUMBER];
139
140/*
[a3d3d9a]141 * Mapping from ATA-minor numbers to
[109fc26]142 * controller-minor and device on this controller.
143 */
144static ata_ide_dev_t ata_devs[2 * IDE_CTRL_MAX_MINOR_NUMBER];
145static int ata_devs_number;
146
147/* Flag meaning that ATA driver has already been initialized */
[6640459d]148static bool ata_initialized = false;
[109fc26]149
150
151/* task and queue used for asynchronous I/O operations */
152static rtems_id ata_task_id;
153static rtems_id ata_queue_id;
154
[d5ed9e1]155#if defined (ATA_USE_OLD_EXCEPTIONS)
[109fc26]156/* Mapping of interrupt vectors to devices */
[b9c38ed]157static rtems_chain_control ata_int_vec[ATA_MAX_RTEMS_INT_VEC_NUMBER + 1];
[d5ed9e1]158#endif
[109fc26]159
160static void
161ata_process_request(rtems_device_minor_number ctrl_minor);
162
163static void
164ata_process_request_on_init_phase(rtems_device_minor_number  ctrl_minor,
165                                  ata_req_t                 *areq);
166
[a3d3d9a]167static void
168ata_add_to_controller_queue(rtems_device_minor_number  ctrl_minor,
[109fc26]169                            ata_req_t                 *areq);
170
171/*
[a3d3d9a]172 * read/write, open/close and ioctl are provided by general block device
[109fc26]173 * driver. Only initialization and ata-specific ioctl are here.
174 */
175
[a3d3d9a]176/* ata_io_data_request --
177 *     Form read/write request for an ATA device and enqueue it to
178 *     IDE controller.
179 *
[109fc26]180 * PARAMETERS:
181 *     device - device identifier
182 *     req    - read/write request from block device driver
183 *
184 * RETURNS:
185 *     RTEMS_SUCCESSFUL on success, or error code if
[a3d3d9a]186 *     error occured
[109fc26]187 */
188static rtems_status_code
[3735f08]189ata_io_data_request(dev_t device, rtems_blkdev_request *req)
[109fc26]190{
191    ata_req_t                 *areq; /* ATA request */
[a3d3d9a]192    rtems_device_minor_number  rel_minor; /* relative minor which indexes
[109fc26]193                                           * ata_devs array
194                                           */
195    rtems_device_minor_number  ctrl_minor;
[ee4f57d]196    uint8_t                    dev;
[a3d3d9a]197
198    rel_minor = (rtems_filesystem_dev_minor_t(device)) /
[109fc26]199                ATA_MINOR_NUM_RESERVED_PER_ATA_DEVICE;
[a3d3d9a]200
[109fc26]201    /* get controller which serves the ATA device */
202    ctrl_minor = ata_devs[rel_minor].ctrl_minor;
203
204    /* get ATA device identifier (0 or 1) */
205    dev = ata_devs[rel_minor].device;
206
207    areq = malloc(sizeof(ata_req_t));
208    if (areq == NULL)
209    {
210        return RTEMS_NO_MEMORY;
211    }
[a3d3d9a]212
[109fc26]213    areq->breq = req;
[f031251c]214    areq->cnt = req->bufnum;
[109fc26]215    areq->cbuf = 0;
216    areq->pos = 0;
[a3d3d9a]217
218    /* set up registers masks */
[109fc26]219    areq->regs.to_write = ATA_REGISTERS_POSITION;
220    areq->regs.to_read = ATA_REGISTERS_VALUE(IDE_REGISTER_STATUS);
[a3d3d9a]221
[109fc26]222    /* choose device on the controller for which the command will be issued */
[a3d3d9a]223    areq->regs.regs[IDE_REGISTER_DEVICE_HEAD] =
[109fc26]224                                    (dev << IDE_REGISTER_DEVICE_HEAD_DEV_POS);
225
226    /* Find ATA command and its type */
227    if (ATA_DEV_INFO(ctrl_minor, dev).mode_active & ATA_MODES_DMA)
228    {
229        /* XXX: never has been tested */
230        areq->type = ATA_COMMAND_TYPE_DMA;
[3735f08]231        if (req->req == RTEMS_BLKDEV_REQ_READ)
[109fc26]232            areq->regs.regs[IDE_REGISTER_COMMAND] = ATA_COMMAND_READ_DMA;
[a3d3d9a]233        else
[109fc26]234            areq->regs.regs[IDE_REGISTER_COMMAND] = ATA_COMMAND_WRITE_DMA;
235    }
[a3d3d9a]236    else
[109fc26]237    {
[3735f08]238        if (req->req == RTEMS_BLKDEV_REQ_READ)
[109fc26]239        {
[3735f08]240#ifdef DEBUG
241            printf("ata_io_data_request: type: READ: %d, %d\n",
[f031251c]242                   req->bufs[0].block, req->bufnum);
[3735f08]243#endif           
[109fc26]244            areq->type = ATA_COMMAND_TYPE_PIO_IN;
[a3d3d9a]245
246            /*
247             * choose command to issue: if the number of blocks to be
[109fc26]248             * exchanged is greater then 1 and for ATA command READ MULTIPLE
249             * data block consists of more then 1 sector choose READ MULTIPLE
250             * otherwise READ SECTORS
251             */
[a3d3d9a]252            areq->regs.regs[IDE_REGISTER_COMMAND] =
253                ((ATA_DEV_INFO(ctrl_minor, dev).max_multiple) &&
[f031251c]254                 (req->bufnum > 1) &&
[a3d3d9a]255                 (ATA_DEV_INFO(ctrl_minor, dev).current_multiple > 1)) ?
256                 ATA_COMMAND_READ_MULTIPLE :
[109fc26]257                 ATA_COMMAND_READ_SECTORS;
258        }
259        else
260        {
[3735f08]261#ifdef DEBUG
262            printf("ata_io_data_request: type: WRITE: %d, %d\n",
[f031251c]263                   req->bufs[0].block, req->bufnum);
[3735f08]264#endif           
[109fc26]265            areq->type = ATA_COMMAND_TYPE_PIO_OUT;
266
[a3d3d9a]267            /*
268             * choose command to issue: if the number of blocks to be
[109fc26]269             * exchanged is greater then 1 and for ATA command WRITE MULTIPLE
270             * data block consists of more then 1 sector choose WRITE MULTIPLE
271             * otherwise WRITE SECTORS
272             */
[a3d3d9a]273            areq->regs.regs[IDE_REGISTER_COMMAND] =
[109fc26]274              ((ATA_DEV_INFO(ctrl_minor, dev).max_multiple) &&
[f031251c]275               (req->bufnum > 1) &&
[a3d3d9a]276               (ATA_DEV_INFO(ctrl_minor, dev).current_multiple > 1)) ?
277               ATA_COMMAND_WRITE_MULTIPLE :
[109fc26]278               ATA_COMMAND_WRITE_SECTORS;
279        }
280    }
281
282    /*
283     * Fill position registers
284     */
285    if (ATA_DEV_INFO(ctrl_minor, dev).lba_avaible)
286    {
[3735f08]287        uint32_t start = req->bufs[0].block;
288        areq->regs.regs[IDE_REGISTER_LBA0] = (uint8_t)start;
289        areq->regs.regs[IDE_REGISTER_LBA1] = (uint8_t)(start >> 8);
290        areq->regs.regs[IDE_REGISTER_LBA2] = (uint8_t)(start >> 16);
291        areq->regs.regs[IDE_REGISTER_LBA3] |= (uint8_t) (start >> 24);
[109fc26]292        areq->regs.regs[IDE_REGISTER_LBA3] |= IDE_REGISTER_LBA3_L;
293    }
294    else
295    {
[3735f08]296        uint32_t   count = req->bufs[0].block;
[109fc26]297
298        areq->regs.regs[IDE_REGISTER_SECTOR_NUMBER] =
299                        (count % ATA_DEV_INFO(ctrl_minor, dev).sectors) + 1;
300
301        /* now count = number of tracks: */
302        count /= ATA_DEV_INFO(ctrl_minor, dev).sectors;
[a3d3d9a]303        areq->regs.regs[IDE_REGISTER_DEVICE_HEAD] |=
[109fc26]304                          (count / ATA_DEV_INFO(ctrl_minor, dev).cylinders);
305
306        /* now count = number of cylinders */
307        count %= ATA_DEV_INFO(ctrl_minor, dev).cylinders;
[509fec9c]308        areq->regs.regs[IDE_REGISTER_CYLINDER_LOW] = (uint8_t)count;
309        areq->regs.regs[IDE_REGISTER_CYLINDER_HIGH] = (uint8_t)(count >> 8);
[a3d3d9a]310        areq->regs.regs[IDE_REGISTER_DEVICE_HEAD] &=
[109fc26]311                                                ~IDE_REGISTER_DEVICE_HEAD_L;
312    }
313
314    /* fill sector count register */
[f031251c]315    areq->regs.regs[IDE_REGISTER_SECTOR_COUNT] = areq->breq->bufnum;
[a3d3d9a]316
[109fc26]317    /* add request to the queue of awaiting requests to the controller */
318    ata_add_to_controller_queue(ctrl_minor, areq);
[3735f08]319   
[109fc26]320    return RTEMS_SUCCESSFUL;
321}
322
[a3d3d9a]323/* ata_non_data_request --
324 *     Form and serve request of NON DATA type for an ATA device.
[109fc26]325 *     Processing of NON DATA request is SYNChronous operation.
[a3d3d9a]326 *
[109fc26]327 * PARAMETERS:
328 *     device - device identifier
329 *     cmd    - command
330 *     argp   - arguments for command
331 *
332 * RETURNS:
333 *     RTEMS_SUCCESSFUL on success, or error code if
[a3d3d9a]334 *     error occured
[109fc26]335 */
336static rtems_status_code
337ata_non_data_request(dev_t device, int cmd, void *argp)
338{
339    rtems_status_code          rc;
340    ata_req_t                 *areq;       /* ATA request */
341    rtems_device_minor_number  rel_minor; /* relative minor which indexes
342                                           * ata_devs array
343                                           */
344    rtems_device_minor_number  ctrl_minor;
[ee4f57d]345    uint8_t                    dev;
[a3d3d9a]346    ata_queue_msg_t            msg;
347
[109fc26]348    rel_minor = (rtems_filesystem_dev_minor_t(device)) /
[a3d3d9a]349                ATA_MINOR_NUM_RESERVED_PER_ATA_DEVICE;
[109fc26]350
351    /* get controller which serves the ATA device */
352    ctrl_minor = ata_devs[rel_minor].ctrl_minor;
353
354    /* get ATA device identifier (0 or 1) */
355    dev = ata_devs[rel_minor].device;
356
357    /* form the request */
358    areq = malloc(sizeof(ata_req_t));
359    if (areq == NULL)
360    {
361        return RTEMS_NO_MEMORY;
362    }
[a3d3d9a]363    memset(areq, 0, sizeof(ata_req_t));
[109fc26]364
365    areq->type = ATA_COMMAND_TYPE_NON_DATA;
366    areq->regs.to_write = ATA_REGISTERS_VALUE(IDE_REGISTER_COMMAND);
[a3d3d9a]367    areq->regs.regs[IDE_REGISTER_DEVICE_HEAD] |=
[109fc26]368                                    (dev << IDE_REGISTER_DEVICE_HEAD_DEV_POS);
369    areq->breq = NULL;
370    areq->regs.to_read = ATA_REGISTERS_VALUE(IDE_REGISTER_ERROR);
[a3d3d9a]371
372    /*
373     * depending on command fill command register and additional registers
[109fc26]374     * which are needed for command execution
375     */
376    switch(cmd)
[a3d3d9a]377    {
[109fc26]378        case ATAIO_SET_MULTIPLE_MODE:
[a3d3d9a]379            areq->regs.regs[IDE_REGISTER_COMMAND] =
[109fc26]380                                                ATA_COMMAND_SET_MULTIPLE_MODE;
[a3d3d9a]381            areq->regs.to_write |=
[109fc26]382                               ATA_REGISTERS_VALUE(IDE_REGISTER_SECTOR_COUNT);
[509fec9c]383            areq->regs.regs[IDE_REGISTER_SECTOR_COUNT] = *(uint8_t*)argp;
[109fc26]384            break;
[a3d3d9a]385
[109fc26]386        default:
387            free(areq);
388            return RTEMS_INVALID_NUMBER;
[a3d3d9a]389            break;
390    }
[109fc26]391
[a3d3d9a]392    rc = rtems_semaphore_create(rtems_build_name('I', 'D', 'E', 'S'),
393                                0,
394                                RTEMS_FIFO | RTEMS_COUNTING_SEMAPHORE |
[109fc26]395                                RTEMS_NO_INHERIT_PRIORITY |
396                                RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL,
397                                0,
398                                &(areq->sema));
399    if (rc != RTEMS_SUCCESSFUL)
400    {
401        free(areq);
402        return rc;
[a3d3d9a]403    }
404
[109fc26]405    ata_add_to_controller_queue(ctrl_minor, areq);
[a3d3d9a]406
[109fc26]407    /* wait for request processing... */
408    rc = rtems_semaphore_obtain(areq->sema, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
409    if (rc != RTEMS_SUCCESSFUL)
410    {
411        free(areq);
412        return rc;
[a3d3d9a]413    }
[109fc26]414
415    rtems_semaphore_delete(areq->sema);
[a3d3d9a]416
417    /*
418     * if no error occurred and if necessary, update internal ata driver data
[109fc26]419     * structures to reflect changes (in device configuration, for example)
[a3d3d9a]420     */
[109fc26]421    if (areq->status == RTEMS_SUCCESSFUL)
422    {
423        switch(cmd)
[a3d3d9a]424        {
[109fc26]425            case ATAIO_SET_MULTIPLE_MODE:
[a3d3d9a]426                ATA_DEV_INFO(ctrl_minor, dev).current_multiple =
[509fec9c]427                                                           *(uint8_t*)argp;
[109fc26]428                break;
[a3d3d9a]429
[109fc26]430            default:
431                rc = RTEMS_INVALID_NUMBER;
[a3d3d9a]432                break;
433        }
434    }
435    else
436    {
437        /* XXX: should be correct error processing: for ex, may be
[109fc26]438         * ABRT and then we should return RTEMS_NOT_IMPLEMENTED
439         */
440        rc = RTEMS_IO_ERROR;
[a3d3d9a]441    }
442
[109fc26]443    /* tell ata driver that controller ready to serve next request */
444    ATA_SEND_EVT(msg, ATA_MSG_SUCCESS_EVT, ctrl_minor, 0);
[a3d3d9a]445
[109fc26]446    return rc;
447}
448
[a3d3d9a]449/* ata_process_request --
450 *     Get first request from controller's queue and process it.
451 *
[109fc26]452 * PARAMETERS:
453 *     ctrl_minor - controller identifier
454 *
455 * RETURNS:
[a3d3d9a]456 *     NONE
[109fc26]457 */
458static void
459ata_process_request(rtems_device_minor_number ctrl_minor)
460{
461    ata_req_t       *areq;
[ee4f57d]462    uint16_t         byte; /* emphasize that only 8 low bits is meaningful */
[109fc26]463    ata_queue_msg_t  msg;
[ee4f57d]464    uint8_t          i, dev;
465    uint16_t         val;
[a3d3d9a]466    uint16_t         data_bs; /* the number of 512-bytes sectors in one
467                               * data block
[109fc26]468                               */
[a3d3d9a]469    ISR_Level        level;
[3735f08]470   
[109fc26]471    /* if no requests to controller then do nothing */
[b9c38ed]472    if (rtems_chain_is_empty(&ata_ide_ctrls[ctrl_minor].reqs))
[109fc26]473        return;
474
475    /* get first request in the controller's queue */
476    _ISR_Disable(level);
477    areq = (ata_req_t *)(ata_ide_ctrls[ctrl_minor].reqs.first);
478    _ISR_Enable(level);
[a3d3d9a]479
[109fc26]480    /* get ATA device identifier (0 or 1) */
[a3d3d9a]481    dev =  areq->regs.regs[IDE_REGISTER_DEVICE_HEAD] &
[109fc26]482           IDE_REGISTER_DEVICE_HEAD_DEV;
483
484    /* get data block size */
[a3d3d9a]485    data_bs = ATA_DEV_INFO(ctrl_minor, dev).current_multiple ?
486              ATA_DEV_INFO(ctrl_minor, dev).current_multiple : 1;
487
[109fc26]488    /* execute device select protocol */
[a3d3d9a]489    ide_controller_write_register(ctrl_minor, IDE_REGISTER_DEVICE_HEAD,
[109fc26]490                                  areq->regs.regs[IDE_REGISTER_DEVICE_HEAD]);
[a3d3d9a]491
[109fc26]492    do {
493        ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS, &byte);
[a3d3d9a]494    } while ((byte & IDE_REGISTER_STATUS_BSY) ||
[109fc26]495             (!(byte & IDE_REGISTER_STATUS_DRDY)));
496
497    /* fill in all  necessary registers on the controller */
498    for (i=0; i< ATA_MAX_CMD_REG_OFFSET; i++)
499    {
[ee4f57d]500        uint32_t   reg = (1 << i);
[109fc26]501        if (areq->regs.to_write & reg)
502            ide_controller_write_register(ctrl_minor, i, areq->regs.regs[i]);
503    }
504
[3735f08]505#ifdef DEBUG
506    printf("ata_process_request: type: %d\n", areq->type);
507#endif
508   
[109fc26]509    /* continue to execute ATA protocols depending on type of request */
510    if (areq->type == ATA_COMMAND_TYPE_PIO_OUT)
511    {
512        do {
[a3d3d9a]513            ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS,
[109fc26]514                                         &byte);
515        } while (byte & IDE_REGISTER_STATUS_BSY);
516
517        if (byte & IDE_REGISTER_STATUS_DRQ)
518        {
519            ide_controller_write_data_block(
[a3d3d9a]520                ctrl_minor,
[109fc26]521                MIN(data_bs, areq->cnt) * ATA_SECTOR_SIZE,
522                areq->breq->bufs, &areq->cbuf,
523                &areq->pos);
524            areq->cnt -= MIN(data_bs, areq->cnt);
[a3d3d9a]525        }
[109fc26]526        else
[a3d3d9a]527        {
[6640459d]528            if (IDE_Controller_Table[ctrl_minor].int_driven == false)
[109fc26]529            {
530                ide_controller_read_register(
[a3d3d9a]531                    ctrl_minor,
[109fc26]532                    IDE_REGISTER_ALTERNATE_STATUS_OFFSET,
533                    &val);
534                ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS,
535                                             &val);
536
[a3d3d9a]537                ATA_SEND_EVT(msg, ATA_MSG_ERROR_EVT, ctrl_minor,
[109fc26]538                             RTEMS_IO_ERROR);
539            }
[a3d3d9a]540        }
[109fc26]541    }
542
[6640459d]543    if (IDE_Controller_Table[ctrl_minor].int_driven == false)
[109fc26]544    {
545        do {
[a3d3d9a]546            ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS,
[109fc26]547                                         &byte);
548        } while (byte & IDE_REGISTER_STATUS_BSY);
[a3d3d9a]549
[109fc26]550        ATA_SEND_EVT(msg, ATA_MSG_GEN_EVT, ctrl_minor, 0);
[a3d3d9a]551    }
[109fc26]552}
553
[a3d3d9a]554/* ata_request_done --
[109fc26]555 *     Extract request from controller queue, execute callback if necessary
556 *     and process next request for the controller.
[a3d3d9a]557 *
[109fc26]558 * PARAMETERS:
[a3d3d9a]559 *     areq       - ATA request
[109fc26]560 *     ctrl_minor - controller identifier
561 *     status     - status with which request has been done
562 *     error      - error, if status != RTEMS_SUCCESSFUL
563 *
564 * RETURNS:
565 *     NONE
566 */
567static inline void
568ata_request_done(ata_req_t *areq, rtems_device_minor_number ctrl_minor,
569                 rtems_status_code status, int error)
570{
571    assert(areq);
572
[3735f08]573#ifdef DEBUG
574    printf("ata_request_done: entry\n");
575#endif
576   
[109fc26]577    ATA_EXEC_CALLBACK(areq, status, error);
[b9c38ed]578    rtems_chain_extract(&areq->link);
[3735f08]579   
[b9c38ed]580    if (!rtems_chain_is_empty(&ata_ide_ctrls[ctrl_minor].reqs))
[109fc26]581    {
[3735f08]582        free(areq);
[109fc26]583        ata_process_request(ctrl_minor);
584        return;
585    }
[3735f08]586   
[3d14a45]587    free(areq);
[3735f08]588   
589#ifdef DEBUG
590    printf("ata_request_done: exit\n");
591#endif
[109fc26]592}
593
[a3d3d9a]594/* ata_non_data_request_done --
[109fc26]595 *     Set up request status and release request's semaphore.
[a3d3d9a]596 *
[109fc26]597 * PARAMETERS:
[a3d3d9a]598 *     areq       - ATA request
[109fc26]599 *     ctrl_minor - controller identifier
600 *     status     - status with which request has been done
601 *     error      - error, if status != RTEMS_SUCCESSFUL
602 *
603 * RETURNS:
604 *     NONE
605 */
606static inline void
[a3d3d9a]607ata_non_data_request_done(ata_req_t *areq,
[109fc26]608                          rtems_device_minor_number ctrl_minor,
609                          rtems_status_code status, int error)
610{
[92c70b9]611#ifdef DEBUG
612    printf("ata_non_data_request_done: entry\n");
613#endif
614   
[109fc26]615    areq->status = status;
616    areq->error = error;
617    rtems_semaphore_release(areq->sema);
618}
619
620
[a3d3d9a]621/* ata_add_to_controller_queue --
[109fc26]622 *     Add request to the controller's queue.
[a3d3d9a]623 *
[109fc26]624 * PARAMETERS:
625 *     ctrl_minor - controller identifier
[a3d3d9a]626 *     areq       - ATA request
[109fc26]627 *
628 * RETURNS:
629 *     NONE
630 */
631static void
[a3d3d9a]632ata_add_to_controller_queue(rtems_device_minor_number  ctrl_minor,
[109fc26]633                            ata_req_t                 *areq)
634{
[3735f08]635    PREEMPTION_KEY(key);
636   
637    DISABLE_PREEMPTION(key);
638   
[b9c38ed]639    rtems_chain_append(&ata_ide_ctrls[ctrl_minor].reqs, &areq->link);
640    if (rtems_chain_has_only_one_node(&ata_ide_ctrls[ctrl_minor].reqs))
[109fc26]641    {
[d5ed9e1]642
[109fc26]643        ata_queue_msg_t msg;
644
[92c70b9]645#ifdef DEBUG_DOES_NOT_WORK_WITH_QEMU
[d5ed9e1]646        uint16_t      val;
[a3d3d9a]647        /*
[109fc26]648         * read IDE_REGISTER_ALTERNATE_STATUS instead IDE_REGISTER_STATUS
649         * to prevent clearing of pending interrupt
650         */
[a3d3d9a]651        ide_controller_read_register(ctrl_minor,
652                                     IDE_REGISTER_ALTERNATE_STATUS,
[109fc26]653                                     &val);
654        if (val & IDE_REGISTER_STATUS_BSY)
655            return;
656#endif
657        ATA_SEND_EVT(msg, ATA_MSG_PROCESS_NEXT_EVT, ctrl_minor, 0);
[a3d3d9a]658    }
[3735f08]659
660    ENABLE_PREEMPTION(key);
[a3d3d9a]661}
[109fc26]662
663
[a3d3d9a]664/* ata_interrupt_handler --
[109fc26]665 *     ATA driver interrrupt handler. If interrrupt happend it mapped it to
666 *     controller (controllerS, if a number of controllers share one int line)
667 *     and generates ATA event(s).
[a3d3d9a]668 *
[109fc26]669 * PARAMETERS:
[a3d3d9a]670 *     vec - interrupt vector
[109fc26]671 *
672 * RETURNS:
673 *     NONE
674 */
[d5ed9e1]675#if defined(ATA_USE_OLD_EXCEPTIONS)
[109fc26]676rtems_isr
677ata_interrupt_handler(rtems_vector_number vec)
678{
[b9c38ed]679    rtems_chain_node *the_node = ((rtems_chain_control *)(&ata_int_vec[vec]))->first;
[109fc26]680    ata_queue_msg_t  msg;
[ee4f57d]681    uint16_t         byte; /* emphasize that only 8 low bits is meaningful */
[a3d3d9a]682
[b9c38ed]683    for ( ; !rtems_chain_is_tail(&ata_int_vec[vec], the_node) ; )
[109fc26]684    {
685        /* if (1) - is temporary hack - currently I don't know how to identify
686         * controller which asserted interrupt if few controllers share one
687         * interrupt line
688         */
689        if (1)
690        {
691            msg.ctrl_minor = ((ata_int_st_t *)the_node)->ctrl_minor;
[a3d3d9a]692            ide_controller_read_register(msg.ctrl_minor, IDE_REGISTER_STATUS,
[109fc26]693                                         &byte);
[a3d3d9a]694            ATA_SEND_EVT(msg, ATA_MSG_GEN_EVT, msg.ctrl_minor, 0);
[109fc26]695        }
696        the_node = the_node->next;
[a3d3d9a]697    }
[109fc26]698}
[d5ed9e1]699#else
700void ata_interrupt_handler(rtems_irq_hdl_param handle)
701{
702  int ata_irq_chain_index = (int) handle;
[b9c38ed]703    rtems_chain_node *the_node =
[d5ed9e1]704      ata_irq_chain[ata_irq_chain_index].irq_chain.last;
705    ata_queue_msg_t  msg;
706    uint16_t       byte; /* emphasize that only 8 low bits is meaningful */
707
708
[b9c38ed]709    for ( ; !rtems_chain_is_tail(&ata_irq_chain[ata_irq_chain_index].irq_chain,
[d5ed9e1]710                           the_node) ; )
711    {
712        /* if (1) - is temporary hack - currently I don't know how to identify
713         * controller which asserted interrupt if few controllers share one
714         * interrupt line
715         */
716        if (1)
717        {
718            msg.ctrl_minor = ((ata_int_st_t *)the_node)->ctrl_minor;
719            ide_controller_read_register(msg.ctrl_minor, IDE_REGISTER_STATUS,
720                                         &byte);
721            ATA_SEND_EVT(msg, ATA_MSG_GEN_EVT, msg.ctrl_minor, 0);
722        }
723        the_node = the_node->next;
724    }
725}
726
727void ata_interrupt_on(const rtems_irq_connect_data *ptr)
728  {
729
730    /* enable ATA device interrupt */
731    ide_controller_write_register(0,
732                                  IDE_REGISTER_DEVICE_CONTROL_OFFSET,
733                                  0x00
734                                 );
735  }
736
737
738void ata_interrupt_off(const rtems_irq_connect_data *ptr)
739  {
740
741    /* disable ATA device interrupt */
742    ide_controller_write_register(0,
743                                  IDE_REGISTER_DEVICE_CONTROL_OFFSET,
744                                  IDE_REGISTER_DEVICE_CONTROL_nIEN
745                                 );
746  }
747
748
749int ata_interrupt_isOn(const rtems_irq_connect_data *ptr)
750  {
751  uint16_t byte; /* emphasize that only 8 low bits is meaningful */
752
753    /* return int. status od ATA device */
754    ide_controller_read_register(0,
755                                IDE_REGISTER_DEVICE_CONTROL_OFFSET,
756                                &byte
757                                );
758
759    return !(byte & IDE_REGISTER_DEVICE_CONTROL_nIEN);
760  }
761
762
763static rtems_irq_connect_data ata_irq_data =
764  {
765
766    0, /* filled out before use... */
[397060c]767    ata_interrupt_handler,/* filled out before use... */
768    NULL,
769    ata_interrupt_on,
770    ata_interrupt_off,
771    ata_interrupt_isOn
[d5ed9e1]772  };
773#endif
[109fc26]774
[a3d3d9a]775/* ata_pio_in_protocol --
[109fc26]776 *     ATA PIO_IN protocol implementation, see specification
[a3d3d9a]777 *
[109fc26]778 * PARAMETERS:
779 *     ctrl_minor - controller identifier
[a3d3d9a]780 *     areq       - ATA request
[109fc26]781 *
782 * RETURNS:
783 *     NONE
784 */
785static inline void
786ata_pio_in_protocol(rtems_device_minor_number ctrl_minor, ata_req_t *areq)
787{
[ee4f57d]788    uint16_t        bs, val;
789    uint8_t         dev;
790    uint32_t        min_val;
[109fc26]791    ata_queue_msg_t msg;
[a3d3d9a]792
793    dev =  areq->regs.regs[IDE_REGISTER_DEVICE_HEAD] &
[109fc26]794           IDE_REGISTER_DEVICE_HEAD_DEV;
795
[a3d3d9a]796    bs = ATA_DEV_INFO(ctrl_minor, dev).current_multiple ?
797         ATA_DEV_INFO(ctrl_minor, dev).current_multiple : 1;
[109fc26]798    min_val = MIN(bs, areq->cnt);
[a3d3d9a]799
800    ide_controller_read_data_block(ctrl_minor, min_val * ATA_SECTOR_SIZE,
[109fc26]801                                   areq->breq->bufs, &areq->cbuf, &areq->pos);
[a3d3d9a]802
[109fc26]803    areq->cnt -= min_val;
804    if (areq->cnt == 0)
805    {
806        ata_request_done(areq, ctrl_minor, RTEMS_SUCCESSFUL, RTEMS_SUCCESSFUL);
807    }
[6640459d]808    else if (IDE_Controller_Table[ctrl_minor].int_driven == false)
[109fc26]809    {
810        do {
811           ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS, &val);
812        } while (val & IDE_REGISTER_STATUS_BSY);
[a3d3d9a]813
[109fc26]814        ATA_SEND_EVT(msg, ATA_MSG_GEN_EVT, ctrl_minor, 0);
815    }
816}
817
[a3d3d9a]818/* ata_pio_out_protocol --
[109fc26]819 *     ATA PIO_OUT protocol implementation, see specification
[a3d3d9a]820 *
[109fc26]821 * PARAMETERS:
822 *     ctrl_minor - controller identifier
[a3d3d9a]823 *     areq       - ATA request
[109fc26]824 *
825 * RETURNS:
826 *     NONE
827 */
828static inline void
829ata_pio_out_protocol(rtems_device_minor_number ctrl_minor, ata_req_t *areq)
830{
[ee4f57d]831    uint16_t        bs, val;
832    uint8_t         dev;
833    uint32_t        min_val;
[109fc26]834    ata_queue_msg_t msg;
[a3d3d9a]835
[3735f08]836#ifdef DEBUG
837    printf("ata_pio_out_protocol:\n");
838#endif
839   
[a3d3d9a]840    dev =  areq->regs.regs[IDE_REGISTER_DEVICE_HEAD] &
[109fc26]841           IDE_REGISTER_DEVICE_HEAD_DEV;
842
[a3d3d9a]843    bs = ATA_DEV_INFO(ctrl_minor, dev).current_multiple ?
844         ATA_DEV_INFO(ctrl_minor, dev).current_multiple : 1;
[109fc26]845
[a3d3d9a]846    min_val = MIN(bs, areq->cnt);
[109fc26]847
848    if (areq->cnt == 0)
849    {
850        ata_request_done(areq, ctrl_minor, RTEMS_SUCCESSFUL, RTEMS_SUCCESSFUL);
851    }
[a3d3d9a]852    else
[109fc26]853    {
854        ide_controller_write_data_block(ctrl_minor, min_val * ATA_SECTOR_SIZE,
[a3d3d9a]855                                        areq->breq->bufs, &areq->cbuf,
[109fc26]856                                        &areq->pos);
857        areq->cnt -= min_val;
[6640459d]858        if (IDE_Controller_Table[ctrl_minor].int_driven == false)
[109fc26]859        {
860            do {
[a3d3d9a]861                ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS,
[109fc26]862                                             &val);
863            } while (val & IDE_REGISTER_STATUS_BSY);
864
865            ATA_SEND_EVT(msg, ATA_MSG_GEN_EVT, ctrl_minor, 0);
866        }
867  }
868}
869
[a3d3d9a]870/* ata_queue_task --
[109fc26]871 *     Task which manages ATA driver events queue.
[a3d3d9a]872 *
[109fc26]873 * PARAMETERS:
874 *     arg - ignored
875 *
876 * RETURNS:
877 *     NONE
878 *
879 * NOTES:
[a3d3d9a]880 *     should be non-preemptive
[109fc26]881 */
882static rtems_task
883ata_queue_task(rtems_task_argument arg)
884{
885    ata_queue_msg_t            msg;
[41a1c33]886    size_t                     size;
[109fc26]887    ata_req_t                 *areq;
888    rtems_device_minor_number  ctrl_minor;
[ee4f57d]889    uint16_t                   val;
890    uint16_t                   val1;
[109fc26]891    rtems_status_code          rc;
892    ISR_Level                  level;
[3735f08]893   
894    PREEMPTION_KEY(key);
[109fc26]895
[3735f08]896    DISABLE_PREEMPTION(key);
897   
[109fc26]898    while (1)
899    {
[3735f08]900        ENABLE_PREEMPTION(key);
901       
[109fc26]902        /* get event which has happend */
[a3d3d9a]903        rc = rtems_message_queue_receive(ata_queue_id, &msg, &size, RTEMS_WAIT,
[109fc26]904                                         RTEMS_NO_TIMEOUT);
905        if (rc != RTEMS_SUCCESSFUL)
906            rtems_fatal_error_occurred(RTEMS_INTERNAL_ERROR);
[a3d3d9a]907
[109fc26]908        /* get controller on which event has happend */
909        ctrl_minor = msg.ctrl_minor;
[a3d3d9a]910
[3735f08]911        DISABLE_PREEMPTION(key);
912       
[109fc26]913        /* get current request to the controller */
[a3d3d9a]914        _ISR_Disable(level);
[109fc26]915        areq = (ata_req_t *)(ata_ide_ctrls[ctrl_minor].reqs.first);
916        _ISR_Enable(level);
[3735f08]917       
[109fc26]918        switch(msg.type)
919        {
920            case ATA_MSG_PROCESS_NEXT_EVT:
921                /* process next request in the controller queue */
922                ata_process_request(ctrl_minor);
923                break;
924
925            case ATA_MSG_SUCCESS_EVT:
[a3d3d9a]926                /*
927                 * finish processing of current request with successful
928                 * status and start processing of the next request in the
[109fc26]929                 * controller queue
930                 */
[a3d3d9a]931                ata_request_done(areq, ctrl_minor, RTEMS_SUCCESSFUL,
[109fc26]932                                 msg.error);
933                break;
[a3d3d9a]934
[109fc26]935            case ATA_MSG_ERROR_EVT:
[a3d3d9a]936                /*
937                 * finish processing of current request with error
938                 * status and start processing of the next request in the
[109fc26]939                 * controller queue
940                 */
[a3d3d9a]941                ata_request_done(areq, ctrl_minor, RTEMS_UNSATISFIED,
[109fc26]942                                 msg.error);
943                break;
[a3d3d9a]944
[109fc26]945            case ATA_MSG_GEN_EVT:
[a3d3d9a]946                /*
947                 * continue processing of the current request to the
948                 * controller according to current request state and
[109fc26]949                 * ATA protocol
950                 */
[a3d3d9a]951                ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS,
[109fc26]952                                           &val);
953                /* process error case */
954                if (val & IDE_REGISTER_STATUS_ERR)
[a3d3d9a]955                {
956                    ide_controller_read_register(ctrl_minor,
957                                                 IDE_REGISTER_ERROR,
[109fc26]958                                                 &val);
959                    if (areq->type == ATA_COMMAND_TYPE_NON_DATA)
[a3d3d9a]960                        ata_non_data_request_done(areq, ctrl_minor,
961                                                  RTEMS_UNSATISFIED,
[109fc26]962                                                  RTEMS_IO_ERROR);
[a3d3d9a]963                    else
964                        ata_request_done(areq, ctrl_minor, RTEMS_UNSATISFIED,
[109fc26]965                                         RTEMS_IO_ERROR);
[a3d3d9a]966                    break;
[109fc26]967                }
968
969                switch(areq->type)
970                {
971                    case ATA_COMMAND_TYPE_PIO_IN:
972                        ata_pio_in_protocol(ctrl_minor, areq);
973                        break;
[a3d3d9a]974
[109fc26]975                    case ATA_COMMAND_TYPE_PIO_OUT:
976                        ata_pio_out_protocol(ctrl_minor, areq);
977                        break;
[a3d3d9a]978
[109fc26]979                    case ATA_COMMAND_TYPE_NON_DATA:
[a3d3d9a]980                        ide_controller_read_register(ctrl_minor,
981                                                      IDE_REGISTER_ERROR,
[109fc26]982                                                     &val1);
983                        ata_non_data_request_done(areq, ctrl_minor,
984                                                  RTEMS_SUCCESSFUL,
985                                                  val1);
986                        break;
[a3d3d9a]987
[109fc26]988                    default:
[d5ed9e1]989#ifdef DEBUG
[109fc26]990                        printf("ata_queue_task: non-supported command type\n");
[d5ed9e1]991#endif
[109fc26]992                        ata_request_done(areq, ctrl_minor,
993                                         RTEMS_UNSATISFIED,
994                                         RTEMS_NOT_IMPLEMENTED);
[a3d3d9a]995                        break;
[109fc26]996                }
[a3d3d9a]997                break;
[109fc26]998
999            default:
[3735f08]1000#ifdef DEBUG
1001                printf("ata_queue_task: internal error\n");
1002                rtems_task_delete (RTEMS_SELF);
1003#endif
[109fc26]1004                rtems_fatal_error_occurred(RTEMS_INTERNAL_ERROR);
1005                break;
1006        }
1007    }
1008}
1009
[a3d3d9a]1010/* ata_ioctl --
[109fc26]1011 *     ATA driver ioctl interface.
[a3d3d9a]1012 *
[109fc26]1013 * PARAMETERS:
1014 *     device - device identifier
1015 *     cmd    - command
1016 *     argp   - arguments
1017 *
1018 * RETURNS:
1019 *     depend on 'cmd'
1020 */
[a3d3d9a]1021int
[109fc26]1022ata_ioctl(dev_t device, int cmd, void *argp)
1023{
1024    rtems_status_code         status;
1025    rtems_device_minor_number rel_minor;
[a3d3d9a]1026
[109fc26]1027    rel_minor = (rtems_filesystem_dev_minor_t(device)) /
1028                ATA_MINOR_NUM_RESERVED_PER_ATA_DEVICE;
1029
[a3d3d9a]1030    /*
[109fc26]1031     * in most cases this means that device 'device' is not an registred ATA
1032     * device
[a3d3d9a]1033     */
[109fc26]1034    if (ata_devs[rel_minor].device == ATA_UNDEFINED_VALUE)
1035    {
1036        errno = ENODEV;
1037        return -1;
[a3d3d9a]1038    }
[109fc26]1039
[a3d3d9a]1040    switch (cmd)
[109fc26]1041    {
[3735f08]1042        case RTEMS_BLKIO_REQUEST:
1043            status = ata_io_data_request(device, (rtems_blkdev_request *)argp);
[109fc26]1044            break;
[a3d3d9a]1045
[109fc26]1046        case ATAIO_SET_MULTIPLE_MODE:
1047            status = ata_non_data_request(device, cmd, argp);
1048            break;
[a3d3d9a]1049
[3735f08]1050        case RTEMS_BLKDEV_CAPABILITIES:
1051            *((uint32_t*) argp)  = RTEMS_BLKDEV_CAP_MULTISECTOR_CONT;
1052            status = RTEMS_SUCCESSFUL;
1053            break;
1054
[109fc26]1055        default:
1056            errno = EBADRQC;
1057            return -1;
1058            break;
1059    }
1060
1061    if (status != RTEMS_SUCCESSFUL)
1062    {
1063        errno = EIO;
[a3d3d9a]1064        return -1;
[109fc26]1065    }
1066    return 0;
1067}
1068
1069/*
1070 * ata_initialize --
1071 *     Initializes all ATA devices found on initialized IDE controllers.
[a3d3d9a]1072 *
[109fc26]1073 * PARAMETERS:
1074 *     major - device major number
1075 *     minor - device minor number
1076 *     args   - arguments
1077 *
1078 * RETURNS:
1079 *     RTEMS_SUCCESSFUL on success, or error code if
[a3d3d9a]1080 *     error occured
[109fc26]1081 */
[a3d3d9a]1082rtems_device_driver
[3735f08]1083rtems_ata_initialize(rtems_device_major_number major,
1084                     rtems_device_minor_number minor_arg,
1085                     void *args)
[109fc26]1086{
[ee4f57d]1087    uint32_t           ctrl_minor;
[109fc26]1088    rtems_status_code  status;
1089    ata_req_t          areq;
[a3d3d9a]1090    blkdev_request1    breq;
[ee4f57d]1091    uint8_t            i, dev = 0;
1092    uint16_t          *buffer;
1093    uint16_t           ec;
[109fc26]1094    char               name[ATA_MAX_NAME_LENGTH];
1095    dev_t              device;
1096    ata_int_st_t      *int_st;
[c9b005a9]1097
[d5ed9e1]1098#if defined(ATA_USE_OLD_EXCEPTIONS)
[109fc26]1099    rtems_isr_entry    old_isr;
[d5ed9e1]1100#else
1101    int ata_irq_chain_use;
1102#endif
[109fc26]1103
1104    if (ata_initialized)
1105        return RTEMS_SUCCESSFUL;
[a3d3d9a]1106
[109fc26]1107    /* initialization of disk devices library */
1108    status = rtems_disk_io_initialize();
1109    if (status != RTEMS_SUCCESSFUL)
1110        return status;
[a3d3d9a]1111
[3735f08]1112#ifdef SAFE
1113#ifdef SAFE_MUTEX
1114    status = rtems_semaphore_create (rtems_build_name ('A', 'T', 'A', 'L'),
1115                                     1, RTEMS_ATA_LOCK_ATTRIBS, 0,
1116                                     &ata_lock);
1117    if (status != RTEMS_SUCCESSFUL)
1118        return status;
1119#endif
1120#endif
1121   
[109fc26]1122    /* create queue for asynchronous requests handling */
1123    status = rtems_message_queue_create(
1124                 rtems_build_name('A', 'T', 'A', 'Q'),
1125                 ATA_DRIVER_MESSAGE_QUEUE_SIZE,
1126                 sizeof(ata_queue_msg_t),
1127                 RTEMS_FIFO | RTEMS_LOCAL,
1128                 &ata_queue_id);
1129    if (status != RTEMS_SUCCESSFUL)
1130    {
1131        rtems_disk_io_done();
1132        return status;
[a3d3d9a]1133    }
[109fc26]1134
[a3d3d9a]1135    /*
1136     * create ATA driver task, see comments for task implementation for
1137     * details
[109fc26]1138     */
1139    status = rtems_task_create(
1140                 rtems_build_name ('A', 'T', 'A', 'T'),
[3735f08]1141                 ((rtems_ata_driver_task_priority > 0) 
1142                  ? rtems_ata_driver_task_priority
[c9b005a9]1143                  : ATA_DRIVER_TASK_DEFAULT_PRIORITY),
[109fc26]1144                 ATA_DRIVER_TASK_STACK_SIZE,
[3735f08]1145                 RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR |
[109fc26]1146                 RTEMS_INTERRUPT_LEVEL(0),
1147                 RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
1148                 &ata_task_id);
1149    if (status != RTEMS_SUCCESSFUL)
1150    {
1151        rtems_message_queue_delete(ata_queue_id);
1152        rtems_disk_io_done();
1153        return status;
[a3d3d9a]1154    }
1155
1156    /*
1157     * start ATA driver task. Actually the task will not start immediately -
[109fc26]1158     * it will start only after multitasking support will be started
[a3d3d9a]1159     */
1160    status = rtems_task_start(ata_task_id, ata_queue_task, 0);
[109fc26]1161    if (status != RTEMS_SUCCESSFUL)
1162    {
1163        rtems_task_delete(ata_task_id);
1164        rtems_message_queue_delete(ata_queue_id);
1165        rtems_disk_io_done();
1166        return status;
[a3d3d9a]1167    }
1168
[509fec9c]1169    buffer = (uint16_t*)malloc(ATA_SECTOR_SIZE);
[109fc26]1170    if (buffer == NULL)
1171    {
1172        rtems_task_delete(ata_task_id);
1173        rtems_message_queue_delete(ata_queue_id);
1174        rtems_disk_io_done();
1175        return RTEMS_NO_MEMORY;
[a3d3d9a]1176    }
1177
[109fc26]1178    ata_devs_number = 0;
1179
1180    for (i = 0; i < (2 * IDE_CTRL_MAX_MINOR_NUMBER); i++)
1181        ata_devs[i].device = ATA_UNDEFINED_VALUE;
1182
[d5ed9e1]1183#if defined(ATA_USE_OLD_EXCEPTIONS)
[109fc26]1184    /* prepare ATA driver for handling  interrupt driven devices */
1185    for (i = 0; i < ATA_MAX_RTEMS_INT_VEC_NUMBER; i++)
[b9c38ed]1186        rtems_chain_initialize_empty(&ata_int_vec[i]);
[d5ed9e1]1187#else
1188    for (i = 0; i < ATA_IRQ_CHAIN_MAX_CNT; i++) {
[b9c38ed]1189      rtems_chain_initialize_empty(&(ata_irq_chain[i].irq_chain));
[d5ed9e1]1190    }
1191#endif
[109fc26]1192
[a3d3d9a]1193    /*
1194     * during ATA driver initialization EXECUTE DEVICE DIAGNOSTIC and
1195     * IDENTIFY DEVICE ATA command should be issued; for these purposes ATA
1196     * requests should be formed; ATA requests contain block device request,
[109fc26]1197     * so form block device request first
1198     */
1199    memset(&breq, 0, sizeof(blkdev_request1));
1200    breq.req.req_done = NULL;
1201    breq.req.done_arg = &breq;
1202    breq.req.bufnum = 1;
1203    breq.req.bufs[0].length = ATA_SECTOR_SIZE;
1204    breq.req.bufs[0].buffer = buffer;
1205
[a3d3d9a]1206    /*
[109fc26]1207     * for each presented IDE controller execute EXECUTE DEVICE DIAGNOSTIC
[a3d3d9a]1208     * ATA command; for each found device execute IDENTIFY DEVICE ATA
1209     * command
[109fc26]1210     */
1211    for (ctrl_minor = 0; ctrl_minor < IDE_Controller_Count; ctrl_minor++)
1212    if (IDE_Controller_Table[ctrl_minor].status == IDE_CTRL_INITIALIZED)
1213    {
[b9c38ed]1214        rtems_chain_initialize_empty(&ata_ide_ctrls[ctrl_minor].reqs);
[a3d3d9a]1215
[6640459d]1216        if (IDE_Controller_Table[ctrl_minor].int_driven == true)
[109fc26]1217        {
1218            int_st = malloc(sizeof(ata_int_st_t));
1219            if (int_st == NULL)
1220            {
1221                free(buffer);
1222                rtems_task_delete(ata_task_id);
1223                rtems_message_queue_delete(ata_queue_id);
1224                rtems_disk_io_done();
1225                return RTEMS_NO_MEMORY;
[a3d3d9a]1226            }
[109fc26]1227
1228            int_st->ctrl_minor = ctrl_minor;
[d5ed9e1]1229#if defined(ATA_USE_OLD_EXCEPTIONS)
[109fc26]1230            status = rtems_interrupt_catch(
[a3d3d9a]1231                         ata_interrupt_handler,
1232                         IDE_Controller_Table[ctrl_minor].int_vec,
[109fc26]1233                         &old_isr);
[d5ed9e1]1234#else
1235            /*
1236             * FIXME: check existing entries. if they use the same
1237             * IRQ name, then append int_st to respective chain
1238             * otherwise, use new ata_irq_chain entry
1239             */
1240            ata_irq_chain_use = -1;
1241            for (i = 0;
1242                 ((i < ata_irq_chain_cnt) &&
1243                  (ata_irq_chain_use < 0));i++) {
1244              if (ata_irq_chain[i].name ==
1245                  IDE_Controller_Table[ctrl_minor].int_vec) {
1246                ata_irq_chain_use = i;
1247              }
1248            }
1249            if (ata_irq_chain_use < 0) {
1250              /*
1251               * no match found, try to use new channel entry
1252               */
1253              if (ata_irq_chain_cnt < ATA_IRQ_CHAIN_MAX_CNT) {
1254                ata_irq_chain_use = ata_irq_chain_cnt++;
1255
1256                ata_irq_chain[ata_irq_chain_use].name =
1257                  IDE_Controller_Table[ctrl_minor].int_vec;
1258                ata_irq_data.name   =
1259                  IDE_Controller_Table[ctrl_minor].int_vec;
1260                ata_irq_data.hdl    = ata_interrupt_handler;
[f610e83f]1261                ata_irq_data.handle = (rtems_irq_hdl_param) ctrl_minor;
[d5ed9e1]1262
1263                status = ((0 == BSP_install_rtems_irq_handler(&ata_irq_data))
1264                          ? RTEMS_INVALID_NUMBER
1265                          : RTEMS_SUCCESSFUL);
1266              }
1267              else {
1268                status = RTEMS_TOO_MANY;
1269              }
1270            }
1271#endif
[109fc26]1272            if (status != RTEMS_SUCCESSFUL)
1273            {
1274                free(int_st);
1275                free(buffer);
1276                rtems_task_delete(ata_task_id);
1277                rtems_message_queue_delete(ata_queue_id);
1278                rtems_disk_io_done();
1279                return status;
[a3d3d9a]1280            }
[d5ed9e1]1281#if defined(ATA_USE_OLD_EXCEPTIONS)
[b9c38ed]1282            rtems_chain_append(
[109fc26]1283                &ata_int_vec[IDE_Controller_Table[ctrl_minor].int_vec],
1284                &int_st->link);
[d5ed9e1]1285#else
[b9c38ed]1286            rtems_chain_append(
[d5ed9e1]1287                &(ata_irq_chain[ata_irq_chain_use].irq_chain),
1288                &int_st->link);
1289#endif
[109fc26]1290
1291            /* disable interrupts */
[a3d3d9a]1292            ide_controller_write_register(ctrl_minor,
1293                                          IDE_REGISTER_DEVICE_CONTROL_OFFSET,
[109fc26]1294                                          IDE_REGISTER_DEVICE_CONTROL_nIEN);
1295        }
[92c70b9]1296#if 0
[109fc26]1297        /*
1298         * Issue EXECUTE DEVICE DIAGNOSTIC ATA command for explore is
1299         * there any ATA device on the controller.
1300         */
1301        memset(&areq, 0, sizeof(ata_req_t));
1302        areq.type = ATA_COMMAND_TYPE_NON_DATA;
1303        areq.regs.to_write = ATA_REGISTERS_VALUE(IDE_REGISTER_COMMAND);
[a3d3d9a]1304        areq.regs.regs[IDE_REGISTER_COMMAND] =
[109fc26]1305                                  ATA_COMMAND_EXECUTE_DEVICE_DIAGNOSTIC;
1306        areq.regs.to_read = ATA_REGISTERS_VALUE(IDE_REGISTER_ERROR);
1307
[3735f08]1308        areq.breq = (rtems_blkdev_request *)&breq;
[109fc26]1309
[a3d3d9a]1310        /*
1311         * Process the request. Special processing of requests on
1312         * initialization phase is needed because at this moment there
[109fc26]1313         * is no multitasking enviroment
1314         */
1315        ata_process_request_on_init_phase(ctrl_minor, &areq);
1316
1317        /*
1318         * check status of I/O operation
1319         */
1320        if (breq.req.status != RTEMS_SUCCESSFUL)
1321            continue;
[92c70b9]1322#endif
1323        breq.req.error = ATA_DEV0_PASSED_DEV1_PASSED_OR_NOT_PRSNT;
[a3d3d9a]1324        /* disassemble returned diagnostic codes */
[109fc26]1325        if (breq.req.error == ATA_DEV0_PASSED_DEV1_PASSED_OR_NOT_PRSNT)
1326        {
[6640459d]1327            ATA_DEV_INFO(ctrl_minor, 0).present = true;
1328            ATA_DEV_INFO(ctrl_minor,1).present = true;
[109fc26]1329        }
1330        else if (breq.req.error == ATA_DEV0_PASSED_DEV1_FAILED)
1331        {
[6640459d]1332            ATA_DEV_INFO(ctrl_minor,0).present = true;
1333            ATA_DEV_INFO(ctrl_minor,1).present = false;
[109fc26]1334        }
1335        else if (breq.req.error < ATA_DEV1_PASSED_DEV0_FAILED)
1336        {
[6640459d]1337            ATA_DEV_INFO(ctrl_minor,0).present = false;
1338            ATA_DEV_INFO(ctrl_minor,1).present = true;
[109fc26]1339        }
1340        else
1341        {
[6640459d]1342            ATA_DEV_INFO(ctrl_minor, 0).present = false;
1343            ATA_DEV_INFO(ctrl_minor, 1).present = false;
[109fc26]1344        }
1345
1346        /* refine the returned codes */
[6640459d]1347        if (ATA_DEV_INFO(ctrl_minor, 1).present != false)
[109fc26]1348        {
1349            ide_controller_read_register(ctrl_minor, IDE_REGISTER_ERROR, &ec);
1350            if (ec & ATA_DEV1_PASSED_DEV0_FAILED)
[6640459d]1351                ATA_DEV_INFO(ctrl_minor, 1).present = true;
[109fc26]1352            else
[6640459d]1353                ATA_DEV_INFO(ctrl_minor, 1).present = false;
[109fc26]1354        }
1355
1356        /* for each found ATA device obtain it configuration */
1357        for (dev = 0; dev < 2; dev++)
1358        if (ATA_DEV_INFO(ctrl_minor, dev).present)
1359        {
1360            /*
[a3d3d9a]1361             * Issue DEVICE IDENTIFY ATA command and get device
1362             * configuration
[109fc26]1363             */
1364            memset(&areq, 0, sizeof(ata_req_t));
1365            areq.type = ATA_COMMAND_TYPE_PIO_IN;
1366            areq.regs.to_write = ATA_REGISTERS_VALUE(IDE_REGISTER_COMMAND);
[a3d3d9a]1367            areq.regs.regs[IDE_REGISTER_COMMAND] =
[109fc26]1368                                              ATA_COMMAND_IDENTIFY_DEVICE;
1369            areq.regs.to_read = ATA_REGISTERS_VALUE(IDE_REGISTER_STATUS);
[3735f08]1370            areq.breq = (rtems_blkdev_request *)&breq;
[a3d3d9a]1371
[f031251c]1372            areq.cnt = breq.req.bufnum;
[109fc26]1373
1374            areq.regs.regs[IDE_REGISTER_DEVICE_HEAD] |=
1375                                    (dev << IDE_REGISTER_DEVICE_HEAD_DEV_POS);
1376
[a3d3d9a]1377            /*
1378             * Process the request. Special processing of requests on
1379             * initialization phase is needed because at this moment there
[109fc26]1380             * is no multitasking enviroment
1381             */
1382            ata_process_request_on_init_phase(ctrl_minor, &areq);
1383
1384            /* check status of I/O operation */
1385            if (breq.req.status != RTEMS_SUCCESSFUL)
1386                continue;
1387
1388            /*
[a3d3d9a]1389             * Parse returned device configuration and fill in ATA internal
[109fc26]1390             * device info structure
1391             */
[a3d3d9a]1392            ATA_DEV_INFO(ctrl_minor, dev).cylinders =
[109fc26]1393                CF_LE_W(buffer[ATA_IDENT_WORD_NUM_OF_CURR_LOG_CLNDS]);
[a3d3d9a]1394            ATA_DEV_INFO(ctrl_minor, dev).heads =
[109fc26]1395                CF_LE_W(buffer[ATA_IDENT_WORD_NUM_OF_CURR_LOG_HEADS]);
[a3d3d9a]1396            ATA_DEV_INFO(ctrl_minor, dev).sectors =
[109fc26]1397                CF_LE_W(buffer[ATA_IDENT_WORD_NUM_OF_CURR_LOG_SECS]);
[a3d3d9a]1398            ATA_DEV_INFO(ctrl_minor, dev).lba_sectors =
1399                (CF_LE_W(buffer[ATA_IDENT_WORD_NUM_OF_USR_SECS0]) << 16) +
[109fc26]1400                 CF_LE_W(buffer[ATA_IDENT_WORD_NUM_OF_USR_SECS1]);
[a3d3d9a]1401            ATA_DEV_INFO(ctrl_minor, dev).lba_avaible =
[109fc26]1402                (CF_LE_W(buffer[ATA_IDENT_WORD_CAPABILITIES]) >> 9) & 0x1;
[a3d3d9a]1403            ATA_DEV_INFO(ctrl_minor, dev).max_multiple =
[509fec9c]1404                (uint8_t) (CF_LE_W(buffer[ATA_IDENT_WORD_RW_MULT]));
[a3d3d9a]1405            ATA_DEV_INFO(ctrl_minor, dev).current_multiple =
1406                (CF_LE_W(buffer[ATA_IDENT_WORD_MULT_SECS]) & 0x100) ?
[509fec9c]1407                (uint8_t)(CF_LE_W(buffer[ATA_IDENT_WORD_MULT_SECS])) :
[109fc26]1408                 0;
1409
[a3d3d9a]1410            if ((CF_LE_W(buffer[ATA_IDENT_WORD_FIELD_VALIDITY]) &
[109fc26]1411                 ATA_IDENT_BIT_VALID) == 0) {
1412              /* no "supported modes" info -> use default */
1413              ATA_DEV_INFO(ctrl_minor, dev).mode_active = ATA_MODES_PIO3;
1414            }
1415            else {
[bf474880]1416              ATA_DEV_INFO(ctrl_minor, dev).modes_available =
[109fc26]1417                ((CF_LE_W(buffer[64]) & 0x1) ? ATA_MODES_PIO3 : 0) |
1418                ((CF_LE_W(buffer[64]) & 0x2) ? ATA_MODES_PIO4 : 0) |
1419                ((CF_LE_W(buffer[63]) & 0x1) ? ATA_MODES_DMA0 : 0) |
[a3d3d9a]1420                ((CF_LE_W(buffer[63]) & 0x2) ?
[109fc26]1421                 ATA_MODES_DMA0 | ATA_MODES_DMA1 : 0) |
[a3d3d9a]1422                ((CF_LE_W(buffer[63]) & 0x4) ?
[109fc26]1423                 ATA_MODES_DMA0 | ATA_MODES_DMA1 | ATA_MODES_DMA2 : 0);
[bf474880]1424              if (ATA_DEV_INFO(ctrl_minor, dev).modes_available == 0)
[109fc26]1425                continue;
[a3d3d9a]1426              /*
[109fc26]1427               * choose most appropriate ATA device data I/O speed supported
1428               * by the controller
1429               */
1430              status = ide_controller_config_io_speed(
[a3d3d9a]1431                ctrl_minor,
[bf474880]1432                ATA_DEV_INFO(ctrl_minor, dev).modes_available);
[109fc26]1433              if (status != RTEMS_SUCCESSFUL)
1434                continue;
[a3d3d9a]1435            }
[109fc26]1436            /*
1437             * Ok, let register new ATA device in the system
1438             */
1439            ata_devs[ata_devs_number].ctrl_minor = ctrl_minor;
1440            ata_devs[ata_devs_number].device = dev;
1441
[c54764f9]1442            /* The space leaves a hole for the character. */
1443            strcpy(name, "/dev/hd ");
1444            name[7] = 'a' + 2 * ctrl_minor + dev;
1445
[109fc26]1446            device = rtems_filesystem_make_dev_t(
[a3d3d9a]1447                         major,
1448                         (ata_devs_number *
[109fc26]1449                          ATA_MINOR_NUM_RESERVED_PER_ATA_DEVICE));
1450
[a3d3d9a]1451            status = rtems_disk_create_phys(device, ATA_SECTOR_SIZE,
1452                ATA_DEV_INFO(ctrl_minor, dev).lba_avaible ?
1453                ATA_DEV_INFO(ctrl_minor, dev).lba_sectors :
[109fc26]1454                (ATA_DEV_INFO(ctrl_minor, dev).heads *
1455                 ATA_DEV_INFO(ctrl_minor, dev).cylinders *
1456                 ATA_DEV_INFO(ctrl_minor, dev).sectors),
[3735f08]1457                (rtems_block_device_ioctl) ata_ioctl, name);
[109fc26]1458            if (status != RTEMS_SUCCESSFUL)
1459            {
1460                ata_devs[ata_devs_number].device = ATA_UNDEFINED_VALUE;
1461                continue;
[a3d3d9a]1462            }
[109fc26]1463            ata_devs_number++;
1464        }
[6640459d]1465        if (IDE_Controller_Table[ctrl_minor].int_driven == true)
[a3d3d9a]1466        {
1467            ide_controller_write_register(ctrl_minor,
1468                                          IDE_REGISTER_DEVICE_CONTROL_OFFSET,
[109fc26]1469                                          0x00);
1470        }
1471    }
1472
1473    free(buffer);
[6640459d]1474    ata_initialized = true;
[109fc26]1475    return RTEMS_SUCCESSFUL;
1476}
1477
[a3d3d9a]1478/* ata_process_request_on_init_phase --
1479 *     Process the ATA request during system initialization. Request
[109fc26]1480 *     processing is syncronous and doesn't use multiprocessing enviroment.
[a3d3d9a]1481 *
[109fc26]1482 * PARAMETERS:
1483 *     ctrl_minor - controller identifier
1484 *     areq       - ATA request
1485 *
1486 * RETURNS:
[a3d3d9a]1487 *     NONE
[109fc26]1488 */
1489static void
1490ata_process_request_on_init_phase(rtems_device_minor_number  ctrl_minor,
1491                                  ata_req_t                 *areq)
1492{
[ee4f57d]1493    uint16_t           byte;/* emphasize that only 8 low bits is meaningful */
1494    uint8_t            i, dev;
1495    uint16_t           val, val1;
[a3d3d9a]1496    uint16_t           data_bs; /* the number of 512 bytes sectors into one
1497                                 * data block
[109fc26]1498                                 */
[89d19033]1499        unsigned           retries;
[109fc26]1500    assert(areq);
[a3d3d9a]1501
1502    dev =  areq->regs.regs[IDE_REGISTER_DEVICE_HEAD] &
[109fc26]1503           IDE_REGISTER_DEVICE_HEAD_DEV;
1504
[a3d3d9a]1505    data_bs = ATA_DEV_INFO(ctrl_minor, dev).current_multiple ?
1506              ATA_DEV_INFO(ctrl_minor, dev).current_multiple : 1;
1507
1508    ide_controller_write_register(ctrl_minor, IDE_REGISTER_DEVICE_HEAD,
[109fc26]1509                                  areq->regs.regs[IDE_REGISTER_DEVICE_HEAD]);
[a3d3d9a]1510
[89d19033]1511        retries = 0;
[109fc26]1512    do {
1513        ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS, &byte);
[89d19033]1514        /* If device (on INIT, i.e. it should be idle) is neither
1515         * busy nor ready something's fishy, i.e., there is probably
1516         * no device present.
1517         * I'd like to do a proper timeout but don't know of a portable
1518         * timeout routine (w/o using multitasking / rtems_task_wake_after())
1519         */
1520        if ( ! (byte & (IDE_REGISTER_STATUS_BSY | IDE_REGISTER_STATUS_DRDY)))
1521            retries++;
1522        else
1523            retries=0;
1524        if ( 10000 == retries ) {
1525            /* probably no drive connected */
1526            areq->breq->status = RTEMS_UNSATISFIED;
1527            areq->breq->error = RTEMS_IO_ERROR;
1528            return;
1529        }
[a3d3d9a]1530    } while ((byte & IDE_REGISTER_STATUS_BSY) ||
[109fc26]1531             (!(byte & IDE_REGISTER_STATUS_DRDY)));
1532
1533    for (i=0; i< ATA_MAX_CMD_REG_OFFSET; i++)
1534    {
[ee4f57d]1535        uint32_t   reg = (1 << i);
[109fc26]1536        if (areq->regs.to_write & reg)
[a3d3d9a]1537            ide_controller_write_register(ctrl_minor, i,
[109fc26]1538                                          areq->regs.regs[i]);
1539    }
1540
1541    do {
1542        ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS, &byte);
1543    } while (byte & IDE_REGISTER_STATUS_BSY);
1544
1545    ide_controller_read_register(ctrl_minor, IDE_REGISTER_STATUS, &val);
1546    ide_controller_read_register(ctrl_minor, IDE_REGISTER_ERROR, &val1);
1547
1548    if (val & IDE_REGISTER_STATUS_ERR)
1549    {
1550        areq->breq->status = RTEMS_UNSATISFIED;
1551        areq->breq->error = RTEMS_IO_ERROR;
1552        return;
1553    }
[a3d3d9a]1554
[109fc26]1555    switch(areq->type)
1556    {
1557        case ATA_COMMAND_TYPE_PIO_IN:
1558            ide_controller_read_data_block(
[a3d3d9a]1559                ctrl_minor,
[109fc26]1560                MIN(data_bs, areq->cnt) * ATA_SECTOR_SIZE,
1561                areq->breq->bufs, &areq->cbuf,
1562                &areq->pos);
[a3d3d9a]1563
[109fc26]1564            areq->cnt -= MIN(data_bs, areq->cnt);
1565            if (areq->cnt == 0)
1566            {
1567                areq->breq->status = RTEMS_SUCCESSFUL;
1568            }
[a3d3d9a]1569            else
[109fc26]1570            {
[a3d3d9a]1571                /*
1572                 * this shouldn't happend on the initialization
1573                 * phase!
[109fc26]1574                 */
[a3d3d9a]1575                rtems_fatal_error_occurred(RTEMS_INTERNAL_ERROR);
[109fc26]1576            }
1577            break;
[a3d3d9a]1578
[109fc26]1579        case ATA_COMMAND_TYPE_NON_DATA:
1580            areq->breq->status = RTEMS_SUCCESSFUL;
1581            areq->breq->error = val1;
1582            break;
[a3d3d9a]1583
[109fc26]1584        default:
[d5ed9e1]1585#ifdef DEBUG
[109fc26]1586            printf("ata_queue_task: non-supported command type\n");
[d5ed9e1]1587#endif
[109fc26]1588            areq->breq->status = RTEMS_UNSATISFIED;
1589            areq->breq->error = RTEMS_NOT_IMPLEMENTED;
[a3d3d9a]1590            break;
[109fc26]1591    }
1592}
Note: See TracBrowser for help on using the repository browser.