Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/pciintel/pci.c

    raf6b5157 rfb78ae72  
    11/*
    22 * Copyright (c) 2010 Lenka Trochtova
    3  * Copyright (c) 2011 Jiri Svoboda
    43 * All rights reserved.
    54 *
     
    4544#include <ctype.h>
    4645#include <macros.h>
    47 #include <str_error.h>
    48 
    49 #include <ddf/driver.h>
     46
     47#include <driver.h>
    5048#include <devman.h>
    5149#include <ipc/devman.h>
    5250#include <ipc/dev_iface.h>
     51#include <ipc/irc.h>
     52#include <ipc/ns.h>
     53#include <ipc/services.h>
     54#include <sysinfo.h>
    5355#include <ops/hw_res.h>
    5456#include <device/hw_res.h>
     
    6365        ((1 << 31) | (bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
    6466
    65 /** Obtain PCI function soft-state from DDF function node */
    66 #define PCI_FUN(fnode) ((pci_fun_t *) (fnode)->driver_data)
    67 
    68 /** Obtain PCI bus soft-state from DDF device node */
    69 #define PCI_BUS(dnode) ((pci_bus_t *) (dnode)->driver_data)
    70 
    71 /** Obtain PCI bus soft-state from function soft-state */
    72 #define PCI_BUS_FROM_FUN(fun) ((fun)->busptr)
    73 
    74 static hw_resource_list_t *pciintel_get_resources(ddf_fun_t *fnode)
    75 {
    76         pci_fun_t *fun = PCI_FUN(fnode);
    77        
    78         if (fun == NULL)
     67static hw_resource_list_t *pciintel_get_child_resources(device_t *dev)
     68{
     69        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     70       
     71        if (dev_data == NULL)
    7972                return NULL;
    80         return &fun->hw_resources;
    81 }
    82 
    83 static bool pciintel_enable_interrupt(ddf_fun_t *fnode)
    84 {
    85         /* TODO */
    86        
    87         return false;
    88 }
    89 
    90 static hw_res_ops_t pciintel_hw_res_ops = {
    91         &pciintel_get_resources,
    92         &pciintel_enable_interrupt
     73        return &dev_data->hw_resources;
     74}
     75
     76static bool pciintel_enable_child_interrupt(device_t *dev)
     77{
     78        /* This is an old ugly way, copied from ne2000 driver */
     79        assert(dev);
     80        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     81
     82  sysarg_t apic;
     83  sysarg_t i8259;
     84        int irc_phone = -1;
     85        int irc_service = 0;
     86
     87  if ((sysinfo_get_value("apic", &apic) == EOK) && (apic)) {
     88    irc_service = SERVICE_APIC;
     89        } else if ((sysinfo_get_value("i8259", &i8259) == EOK) && (i8259)) {
     90    irc_service = SERVICE_I8259;
     91        }
     92
     93  if (irc_service) {
     94    while (irc_phone < 0)
     95      irc_phone = service_connect_blocking(irc_service, 0, 0);
     96  } else {
     97                return false;
     98        }
     99
     100        size_t i;
     101  for (i = 0; i < dev_data->hw_resources.count; i++) {
     102                if (dev_data->hw_resources.resources[i].type == INTERRUPT) {
     103                        int irq = dev_data->hw_resources.resources[i].res.interrupt.irq;
     104                        async_msg_1(irc_phone, IRC_ENABLE_INTERRUPT, irq);
     105                }
     106        }
     107
     108        async_hangup(irc_phone);
     109        return true;
     110}
     111
     112static hw_res_ops_t pciintel_child_hw_res_ops = {
     113        &pciintel_get_child_resources,
     114        &pciintel_enable_child_interrupt
    93115};
    94116
    95 static ddf_dev_ops_t pci_fun_ops;
    96 
    97 static int pci_add_device(ddf_dev_t *);
    98 
    99 /** PCI bus driver standard operations */
     117static device_ops_t pci_child_ops;
     118
     119static int pci_add_device(device_t *);
     120
     121/** The pci bus driver's standard operations. */
    100122static driver_ops_t pci_ops = {
    101123        .add_device = &pci_add_device
    102124};
    103125
    104 /** PCI bus driver structure */
     126/** The pci bus driver structure. */
    105127static driver_t pci_driver = {
    106128        .name = NAME,
     
    108130};
    109131
    110 static pci_bus_t *pci_bus_new(void)
    111 {
    112         pci_bus_t *bus;
    113        
    114         bus = (pci_bus_t *) calloc(1, sizeof(pci_bus_t));
    115         if (bus == NULL)
    116                 return NULL;
    117        
    118         fibril_mutex_initialize(&bus->conf_mutex);
    119         return bus;
    120 }
    121 
    122 static void pci_bus_delete(pci_bus_t *bus)
    123 {
    124         assert(bus != NULL);
    125         free(bus);
    126 }
    127 
    128 static void pci_conf_read(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
    129 {
    130         pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
    131        
    132         fibril_mutex_lock(&bus->conf_mutex);
     132typedef struct pciintel_bus_data {
     133        uint32_t conf_io_addr;
     134        void *conf_data_port;
     135        void *conf_addr_port;
     136        fibril_mutex_t conf_mutex;
     137} pci_bus_data_t;
     138
     139static pci_bus_data_t *create_pci_bus_data(void)
     140{
     141        pci_bus_data_t *bus_data;
     142       
     143        bus_data = (pci_bus_data_t *) malloc(sizeof(pci_bus_data_t));
     144        if (bus_data != NULL) {
     145                memset(bus_data, 0, sizeof(pci_bus_data_t));
     146                fibril_mutex_initialize(&bus_data->conf_mutex);
     147        }
     148
     149        return bus_data;
     150}
     151
     152static void delete_pci_bus_data(pci_bus_data_t *bus_data)
     153{
     154        free(bus_data);
     155}
     156
     157static void pci_conf_read(device_t *dev, int reg, uint8_t *buf, size_t len)
     158{
     159        assert(dev->parent != NULL);
     160       
     161        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     162        pci_bus_data_t *bus_data = (pci_bus_data_t *) dev->parent->driver_data;
     163       
     164        fibril_mutex_lock(&bus_data->conf_mutex);
    133165       
    134166        uint32_t conf_addr;
    135         conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
    136         void *addr = bus->conf_data_port + (reg & 3);
    137        
    138         pio_write_32(bus->conf_addr_port, conf_addr);
     167        conf_addr = CONF_ADDR(dev_data->bus, dev_data->dev, dev_data->fn, reg);
     168        void *addr = bus_data->conf_data_port + (reg & 3);
     169       
     170        pio_write_32(bus_data->conf_addr_port, conf_addr);
    139171       
    140172        switch (len) {
     
    150182        }
    151183       
    152         fibril_mutex_unlock(&bus->conf_mutex);
    153 }
    154 
    155 static void pci_conf_write(pci_fun_t *fun, int reg, uint8_t *buf, size_t len)
    156 {
    157         pci_bus_t *bus = PCI_BUS_FROM_FUN(fun);
    158        
    159         fibril_mutex_lock(&bus->conf_mutex);
     184        fibril_mutex_unlock(&bus_data->conf_mutex);
     185}
     186
     187static void pci_conf_write(device_t *dev, int reg, uint8_t *buf, size_t len)
     188{
     189        assert(dev->parent != NULL);
     190       
     191        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     192        pci_bus_data_t *bus_data = (pci_bus_data_t *) dev->parent->driver_data;
     193       
     194        fibril_mutex_lock(&bus_data->conf_mutex);
    160195       
    161196        uint32_t conf_addr;
    162         conf_addr = CONF_ADDR(fun->bus, fun->dev, fun->fn, reg);
    163         void *addr = bus->conf_data_port + (reg & 3);
    164        
    165         pio_write_32(bus->conf_addr_port, conf_addr);
     197        conf_addr = CONF_ADDR(dev_data->bus, dev_data->dev, dev_data->fn, reg);
     198        void *addr = bus_data->conf_data_port + (reg & 3);
     199       
     200        pio_write_32(bus_data->conf_addr_port, conf_addr);
    166201       
    167202        switch (len) {
     
    177212        }
    178213       
    179         fibril_mutex_unlock(&bus->conf_mutex);
    180 }
    181 
    182 uint8_t pci_conf_read_8(pci_fun_t *fun, int reg)
     214        fibril_mutex_unlock(&bus_data->conf_mutex);
     215}
     216
     217uint8_t pci_conf_read_8(device_t *dev, int reg)
    183218{
    184219        uint8_t res;
    185         pci_conf_read(fun, reg, &res, 1);
     220        pci_conf_read(dev, reg, &res, 1);
    186221        return res;
    187222}
    188223
    189 uint16_t pci_conf_read_16(pci_fun_t *fun, int reg)
     224uint16_t pci_conf_read_16(device_t *dev, int reg)
    190225{
    191226        uint16_t res;
    192         pci_conf_read(fun, reg, (uint8_t *) &res, 2);
     227        pci_conf_read(dev, reg, (uint8_t *) &res, 2);
    193228        return res;
    194229}
    195230
    196 uint32_t pci_conf_read_32(pci_fun_t *fun, int reg)
     231uint32_t pci_conf_read_32(device_t *dev, int reg)
    197232{
    198233        uint32_t res;
    199         pci_conf_read(fun, reg, (uint8_t *) &res, 4);
     234        pci_conf_read(dev, reg, (uint8_t *) &res, 4);
    200235        return res;
    201236}
    202237
    203 void pci_conf_write_8(pci_fun_t *fun, int reg, uint8_t val)
    204 {
    205         pci_conf_write(fun, reg, (uint8_t *) &val, 1);
    206 }
    207 
    208 void pci_conf_write_16(pci_fun_t *fun, int reg, uint16_t val)
    209 {
    210         pci_conf_write(fun, reg, (uint8_t *) &val, 2);
    211 }
    212 
    213 void pci_conf_write_32(pci_fun_t *fun, int reg, uint32_t val)
    214 {
    215         pci_conf_write(fun, reg, (uint8_t *) &val, 4);
    216 }
    217 
    218 void pci_fun_create_match_ids(pci_fun_t *fun)
    219 {
     238void pci_conf_write_8(device_t *dev, int reg, uint8_t val)
     239{
     240        pci_conf_write(dev, reg, (uint8_t *) &val, 1);
     241}
     242
     243void pci_conf_write_16(device_t *dev, int reg, uint16_t val)
     244{
     245        pci_conf_write(dev, reg, (uint8_t *) &val, 2);
     246}
     247
     248void pci_conf_write_32(device_t *dev, int reg, uint32_t val)
     249{
     250        pci_conf_write(dev, reg, (uint8_t *) &val, 4);
     251}
     252
     253void create_pci_match_ids(device_t *dev)
     254{
     255        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     256        match_id_t *match_id = NULL;
    220257        char *match_id_str;
    221         int rc;
    222        
    223         asprintf(&match_id_str, "pci/ven=%04x&dev=%04x",
    224             fun->vendor_id, fun->device_id);
    225 
    226         if (match_id_str == NULL) {
    227                 printf(NAME ": out of memory creating match ID.\n");
    228                 return;
    229         }
    230 
    231         rc = ddf_fun_add_match_id(fun->fnode, match_id_str, 90);
    232         if (rc != EOK) {
    233                 printf(NAME ": error adding match ID: %s\n",
    234                     str_error(rc));
    235         }
    236        
     258       
     259        match_id = create_match_id();
     260        if (match_id != NULL) {
     261                asprintf(&match_id_str, "pci/ven=%04x&dev=%04x",
     262                    dev_data->vendor_id, dev_data->device_id);
     263                match_id->id = match_id_str;
     264                match_id->score = 90;
     265                add_match_id(&dev->match_ids, match_id);
     266        }
     267
    237268        /* TODO add more ids (with subsys ids, using class id etc.) */
    238269}
    239270
    240 void pci_add_range(pci_fun_t *fun, uint64_t range_addr, size_t range_size,
    241     bool io)
    242 {
    243         hw_resource_list_t *hw_res_list = &fun->hw_resources;
     271void
     272pci_add_range(device_t *dev, uint64_t range_addr, size_t range_size, bool io)
     273{
     274        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     275        hw_resource_list_t *hw_res_list = &dev_data->hw_resources;
    244276        hw_resource_t *hw_resources =  hw_res_list->resources;
    245277        size_t count = hw_res_list->count;
     
    266298 * address add it to the devices hw resource list.
    267299 *
    268  * @param fun   PCI function
     300 * @param dev   The pci device.
    269301 * @param addr  The address of the BAR in the PCI configuration address space of
    270  *              the device
    271  * @return      The addr the address of the BAR which should be read next
     302 *              the device.
     303 * @return      The addr the address of the BAR which should be read next.
    272304 */
    273 int pci_read_bar(pci_fun_t *fun, int addr)
    274 {
     305int pci_read_bar(device_t *dev, int addr)
     306{       
    275307        /* Value of the BAR */
    276308        uint32_t val, mask;
     
    286318       
    287319        /* Get the value of the BAR. */
    288         val = pci_conf_read_32(fun, addr);
     320        val = pci_conf_read_32(dev, addr);
    289321       
    290322        io = (bool) (val & 1);
     
    306338       
    307339        /* Get the address mask. */
    308         pci_conf_write_32(fun, addr, 0xffffffff);
    309         mask = pci_conf_read_32(fun, addr);
     340        pci_conf_write_32(dev, addr, 0xffffffff);
     341        mask = pci_conf_read_32(dev, addr);
    310342       
    311343        /* Restore the original value. */
    312         pci_conf_write_32(fun, addr, val);
    313         val = pci_conf_read_32(fun, addr);
     344        pci_conf_write_32(dev, addr, val);
     345        val = pci_conf_read_32(dev, addr);
    314346       
    315347        range_size = pci_bar_mask_to_size(mask);
    316348       
    317349        if (addrw64) {
    318                 range_addr = ((uint64_t)pci_conf_read_32(fun, addr + 4) << 32) |
     350                range_addr = ((uint64_t)pci_conf_read_32(dev, addr + 4) << 32) |
    319351                    (val & 0xfffffff0);
    320352        } else {
     
    323355       
    324356        if (range_addr != 0) {
    325                 printf(NAME ": function %s : ", fun->fnode->name);
     357                printf(NAME ": device %s : ", dev->name);
    326358                printf("address = %" PRIx64, range_addr);
    327359                printf(", size = %x\n", (unsigned int) range_size);
    328360        }
    329361       
    330         pci_add_range(fun, range_addr, range_size, io);
     362        pci_add_range(dev, range_addr, range_size, io);
    331363       
    332364        if (addrw64)
     
    336368}
    337369
    338 void pci_add_interrupt(pci_fun_t *fun, int irq)
    339 {
    340         hw_resource_list_t *hw_res_list = &fun->hw_resources;
     370void pci_add_interrupt(device_t *dev, int irq)
     371{
     372        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     373        hw_resource_list_t *hw_res_list = &dev_data->hw_resources;
    341374        hw_resource_t *hw_resources = hw_res_list->resources;
    342375        size_t count = hw_res_list->count;
     
    350383        hw_res_list->count++;
    351384       
    352         printf(NAME ": function %s uses irq %x.\n", fun->fnode->name, irq);
    353 }
    354 
    355 void pci_read_interrupt(pci_fun_t *fun)
    356 {
    357         uint8_t irq = pci_conf_read_8(fun, PCI_BRIDGE_INT_LINE);
     385        printf(NAME ": device %s uses irq %x.\n", dev->name, irq);
     386}
     387
     388void pci_read_interrupt(device_t *dev)
     389{
     390        uint8_t irq = pci_conf_read_8(dev, PCI_BRIDGE_INT_LINE);
    358391        if (irq != 0xff)
    359                 pci_add_interrupt(fun, irq);
     392                pci_add_interrupt(dev, irq);
    360393}
    361394
    362395/** Enumerate (recursively) and register the devices connected to a pci bus.
    363396 *
    364  * @param bus           Host-to-PCI bridge
    365  * @param bus_num       Bus number
     397 * @param parent        The host-to-pci bridge device.
     398 * @param bus_num       The bus number.
    366399 */
    367 void pci_bus_scan(pci_bus_t *bus, int bus_num)
    368 {
    369         ddf_fun_t *fnode;
    370         pci_fun_t *fun;
     400void pci_bus_scan(device_t *parent, int bus_num)
     401{
     402        device_t *dev = create_device();
     403        pci_dev_data_t *dev_data = create_pci_dev_data();
     404        dev->driver_data = dev_data;
     405        dev->parent = parent;
    371406       
    372407        int child_bus = 0;
    373408        int dnum, fnum;
    374409        bool multi;
    375         uint8_t header_type;
    376        
    377         fun = pci_fun_new(bus);
     410        uint8_t header_type;
    378411       
    379412        for (dnum = 0; dnum < 32; dnum++) {
    380413                multi = true;
    381414                for (fnum = 0; multi && fnum < 8; fnum++) {
    382                         pci_fun_init(fun, bus_num, dnum, fnum);
    383                         fun->vendor_id = pci_conf_read_16(fun,
     415                        init_pci_dev_data(dev_data, bus_num, dnum, fnum);
     416                        dev_data->vendor_id = pci_conf_read_16(dev,
    384417                            PCI_VENDOR_ID);
    385                         fun->device_id = pci_conf_read_16(fun,
     418                        dev_data->device_id = pci_conf_read_16(dev,
    386419                            PCI_DEVICE_ID);
    387                         if (fun->vendor_id == 0xffff) {
     420                        if (dev_data->vendor_id == 0xffff) {
    388421                                /*
    389422                                 * The device is not present, go on scanning the
     
    396429                        }
    397430                       
    398                         header_type = pci_conf_read_8(fun, PCI_HEADER_TYPE);
     431                        header_type = pci_conf_read_8(dev, PCI_HEADER_TYPE);
    399432                        if (fnum == 0) {
    400433                                /* Is the device multifunction? */
     
    404437                        header_type = header_type & 0x7F;
    405438                       
    406                         char *fun_name = pci_fun_create_name(fun);
    407                         if (fun_name == NULL) {
    408                                 printf(NAME ": out of memory.\n");
    409                                 return;
    410                         }
    411                        
    412                         fnode = ddf_fun_create(bus->dnode, fun_inner, fun_name);
    413                         if (fnode == NULL) {
    414                                 printf(NAME ": error creating function.\n");
    415                                 return;
    416                         }
    417                        
    418                         free(fun_name);
    419                         fun->fnode = fnode;
    420                        
    421                         pci_alloc_resource_list(fun);
    422                         pci_read_bars(fun);
    423                         pci_read_interrupt(fun);
    424                        
    425                         fnode->ops = &pci_fun_ops;
    426                         fnode->driver_data = fun;
    427                        
    428                         printf(NAME ": adding new function %s.\n",
    429                             fnode->name);
    430                        
    431                         pci_fun_create_match_ids(fun);
    432                        
    433                         if (ddf_fun_bind(fnode) != EOK) {
    434                                 pci_clean_resource_list(fun);
    435                                 clean_match_ids(&fnode->match_ids);
    436                                 free((char *) fnode->name);
    437                                 fnode->name = NULL;
     439                        create_pci_dev_name(dev);
     440                       
     441                        pci_alloc_resource_list(dev);
     442                        pci_read_bars(dev);
     443                        pci_read_interrupt(dev);
     444                       
     445                        dev->ops = &pci_child_ops;
     446                       
     447                        printf(NAME ": adding new child device %s.\n",
     448                            dev->name);
     449                       
     450                        create_pci_match_ids(dev);
     451                       
     452                        if (child_device_register(dev, parent) != EOK) {
     453                                pci_clean_resource_list(dev);
     454                                clean_match_ids(&dev->match_ids);
     455                                free((char *) dev->name);
     456                                dev->name = NULL;
    438457                                continue;
    439458                        }
     
    441460                        if (header_type == PCI_HEADER_TYPE_BRIDGE ||
    442461                            header_type == PCI_HEADER_TYPE_CARDBUS) {
    443                                 child_bus = pci_conf_read_8(fun,
     462                                child_bus = pci_conf_read_8(dev,
    444463                                    PCI_BRIDGE_SEC_BUS_NUM);
    445464                                printf(NAME ": device is pci-to-pci bridge, "
    446465                                    "secondary bus number = %d.\n", bus_num);
    447466                                if (child_bus > bus_num)
    448                                         pci_bus_scan(bus, child_bus);
     467                                        pci_bus_scan(parent, child_bus);
    449468                        }
    450469                       
    451                         fun = pci_fun_new(bus);
     470                        /* Alloc new aux. dev. structure. */
     471                        dev = create_device();
     472                        dev_data = create_pci_dev_data();
     473                        dev->driver_data = dev_data;
     474                        dev->parent = parent;
    452475                }
    453476        }
    454477       
    455         if (fun->vendor_id == 0xffff) {
    456                 /* Free the auxiliary function structure. */
    457                 pci_fun_delete(fun);
    458         }
    459 }
    460 
    461 static int pci_add_device(ddf_dev_t *dnode)
    462 {
    463         pci_bus_t *bus = NULL;
    464         ddf_fun_t *ctl = NULL;
    465         bool got_res = false;
     478        if (dev_data->vendor_id == 0xffff) {
     479                delete_device(dev);
     480                /* Free the auxiliary device structure. */
     481                delete_pci_dev_data(dev_data);
     482        }
     483}
     484
     485static int pci_add_device(device_t *dev)
     486{
    466487        int rc;
    467        
     488
    468489        printf(NAME ": pci_add_device\n");
    469         dnode->parent_phone = -1;
    470        
    471         bus = pci_bus_new();
    472         if (bus == NULL) {
     490       
     491        pci_bus_data_t *bus_data = create_pci_bus_data();
     492        if (bus_data == NULL) {
    473493                printf(NAME ": pci_add_device allocation failed.\n");
    474                 rc = ENOMEM;
    475                 goto fail;
    476         }
    477         bus->dnode = dnode;
    478         dnode->driver_data = bus;
    479        
    480         dnode->parent_phone = devman_parent_device_connect(dnode->handle,
     494                return ENOMEM;
     495        }
     496       
     497        dev->parent_phone = devman_parent_device_connect(dev->handle,
    481498            IPC_FLAG_BLOCKING);
    482         if (dnode->parent_phone < 0) {
     499        if (dev->parent_phone < 0) {
    483500                printf(NAME ": pci_add_device failed to connect to the "
    484501                    "parent's driver.\n");
    485                 rc = dnode->parent_phone;
    486                 goto fail;
     502                delete_pci_bus_data(bus_data);
     503                return dev->parent_phone;
    487504        }
    488505       
    489506        hw_resource_list_t hw_resources;
    490507       
    491         rc = hw_res_get_resource_list(dnode->parent_phone, &hw_resources);
     508        rc = hw_res_get_resource_list(dev->parent_phone, &hw_resources);
    492509        if (rc != EOK) {
    493510                printf(NAME ": pci_add_device failed to get hw resources for "
    494511                    "the device.\n");
    495                 goto fail;
    496         }
    497         got_res = true;
     512                delete_pci_bus_data(bus_data);
     513                async_hangup(dev->parent_phone);
     514                return rc;
     515        }       
    498516       
    499517        printf(NAME ": conf_addr = %" PRIx64 ".\n",
     
    504522        assert(hw_resources.resources[0].res.io_range.size == 8);
    505523       
    506         bus->conf_io_addr =
     524        bus_data->conf_io_addr =
    507525            (uint32_t) hw_resources.resources[0].res.io_range.address;
    508526       
    509         if (pio_enable((void *)(uintptr_t)bus->conf_io_addr, 8,
    510             &bus->conf_addr_port)) {
     527        if (pio_enable((void *)(uintptr_t)bus_data->conf_io_addr, 8,
     528            &bus_data->conf_addr_port)) {
    511529                printf(NAME ": failed to enable configuration ports.\n");
    512                 rc = EADDRNOTAVAIL;
    513                 goto fail;
    514         }
    515         bus->conf_data_port = (char *) bus->conf_addr_port + 4;
    516        
    517         /* Make the bus device more visible. It has no use yet. */
    518         printf(NAME ": adding a 'ctl' function\n");
    519        
    520         ctl = ddf_fun_create(bus->dnode, fun_exposed, "ctl");
    521         if (ctl == NULL) {
    522                 printf(NAME ": error creating control function.\n");
    523                 rc = ENOMEM;
    524                 goto fail;
    525         }
    526        
    527         rc = ddf_fun_bind(ctl);
    528         if (rc != EOK) {
    529                 printf(NAME ": error binding control function.\n");
    530                 goto fail;
    531         }
    532        
    533         /* Enumerate functions. */
     530                delete_pci_bus_data(bus_data);
     531                async_hangup(dev->parent_phone);
     532                hw_res_clean_resource_list(&hw_resources);
     533                return EADDRNOTAVAIL;
     534        }
     535        bus_data->conf_data_port = (char *) bus_data->conf_addr_port + 4;
     536       
     537        dev->driver_data = bus_data;
     538       
     539        /* Enumerate child devices. */
    534540        printf(NAME ": scanning the bus\n");
    535         pci_bus_scan(bus, 0);
     541        pci_bus_scan(dev, 0);
    536542       
    537543        hw_res_clean_resource_list(&hw_resources);
    538544       
    539545        return EOK;
    540        
    541 fail:
    542         if (bus != NULL)
    543                 pci_bus_delete(bus);
    544         if (dnode->parent_phone >= 0)
    545                 async_hangup(dnode->parent_phone);
    546         if (got_res)
    547                 hw_res_clean_resource_list(&hw_resources);
    548         if (ctl != NULL)
    549                 ddf_fun_destroy(ctl);
    550 
    551         return rc;
    552546}
    553547
    554548static void pciintel_init(void)
    555549{
    556         pci_fun_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_hw_res_ops;
    557 }
    558 
    559 pci_fun_t *pci_fun_new(pci_bus_t *bus)
    560 {
    561         pci_fun_t *fun;
    562        
    563         fun = (pci_fun_t *) calloc(1, sizeof(pci_fun_t));
    564         if (fun == NULL)
    565                 return NULL;
    566 
    567         fun->busptr = bus;
    568         return fun;
    569 }
    570 
    571 void pci_fun_init(pci_fun_t *fun, int bus, int dev, int fn)
    572 {
    573         fun->bus = bus;
    574         fun->dev = dev;
    575         fun->fn = fn;
    576 }
    577 
    578 void pci_fun_delete(pci_fun_t *fun)
    579 {
    580         assert(fun != NULL);
    581         hw_res_clean_resource_list(&fun->hw_resources);
    582         free(fun);
    583 }
    584 
    585 char *pci_fun_create_name(pci_fun_t *fun)
    586 {
     550        pci_child_ops.interfaces[HW_RES_DEV_IFACE] = &pciintel_child_hw_res_ops;
     551}
     552
     553pci_dev_data_t *create_pci_dev_data(void)
     554{
     555        pci_dev_data_t *res = (pci_dev_data_t *) malloc(sizeof(pci_dev_data_t));
     556       
     557        if (res != NULL)
     558                memset(res, 0, sizeof(pci_dev_data_t));
     559        return res;
     560}
     561
     562void init_pci_dev_data(pci_dev_data_t *dev_data, int bus, int dev, int fn)
     563{
     564        dev_data->bus = bus;
     565        dev_data->dev = dev;
     566        dev_data->fn = fn;
     567}
     568
     569void delete_pci_dev_data(pci_dev_data_t *dev_data)
     570{
     571        if (dev_data != NULL) {
     572                hw_res_clean_resource_list(&dev_data->hw_resources);
     573                free(dev_data);
     574        }
     575}
     576
     577void create_pci_dev_name(device_t *dev)
     578{
     579        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
    587580        char *name = NULL;
    588581       
    589         asprintf(&name, "%02x:%02x.%01x", fun->bus, fun->dev,
    590             fun->fn);
    591         return name;
    592 }
    593 
    594 bool pci_alloc_resource_list(pci_fun_t *fun)
    595 {
    596         fun->hw_resources.resources =
     582        asprintf(&name, "%02x:%02x.%01x", dev_data->bus, dev_data->dev,
     583            dev_data->fn);
     584        dev->name = name;
     585}
     586
     587bool pci_alloc_resource_list(device_t *dev)
     588{
     589        pci_dev_data_t *dev_data = (pci_dev_data_t *)dev->driver_data;
     590       
     591        dev_data->hw_resources.resources =
    597592            (hw_resource_t *) malloc(PCI_MAX_HW_RES * sizeof(hw_resource_t));
    598         return fun->hw_resources.resources != NULL;
    599 }
    600 
    601 void pci_clean_resource_list(pci_fun_t *fun)
    602 {
    603         if (fun->hw_resources.resources != NULL) {
    604                 free(fun->hw_resources.resources);
    605                 fun->hw_resources.resources = NULL;
    606         }
    607 }
    608 
    609 /** Read the base address registers (BARs) of the function and add the addresses
    610  * to its HW resource list.
     593        return dev_data->hw_resources.resources != NULL;
     594}
     595
     596void pci_clean_resource_list(device_t *dev)
     597{
     598        pci_dev_data_t *dev_data = (pci_dev_data_t *) dev->driver_data;
     599       
     600        if (dev_data->hw_resources.resources != NULL) {
     601                free(dev_data->hw_resources.resources);
     602                dev_data->hw_resources.resources = NULL;
     603        }
     604}
     605
     606/** Read the base address registers (BARs) of the device and adds the addresses
     607 * to its hw resource list.
    611608 *
    612  * @param fun   PCI function
     609 * @param dev the pci device.
    613610 */
    614 void pci_read_bars(pci_fun_t *fun)
     611void pci_read_bars(device_t *dev)
    615612{
    616613        /*
     
    621618       
    622619        while (addr <= PCI_BASE_ADDR_5)
    623                 addr = pci_read_bar(fun, addr);
     620                addr = pci_read_bar(dev, addr);
    624621}
    625622
     
    633630        printf(NAME ": HelenOS pci bus driver (intel method 1).\n");
    634631        pciintel_init();
    635         return ddf_driver_main(&pci_driver);
     632        return driver_main(&pci_driver);
    636633}
    637634
Note: See TracChangeset for help on using the changeset viewer.