📄 highlevel.c
字号:
/*
* IEEE 1394 for Linux
*
* Copyright (C) 1999 Andreas E. Bombe
*
* This code is licensed under the GPL. See the file COPYING in the root
* directory of the kernel sources for details.
*
*
* Contributions:
*
* Christian Toegel <christian.toegel@gmx.at>
* unregister address space
*
* Manfred Weihs <weihs@ict.tuwien.ac.at>
* unregister address space
*
*/
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/bitops.h>
#include "ieee1394.h"
#include "ieee1394_types.h"
#include "hosts.h"
#include "ieee1394_core.h"
#include "highlevel.h"
#include "nodemgr.h"
struct hl_host_info {
struct list_head list;
struct hpsb_host *host;
size_t size;
unsigned long key;
void *data;
};
static LIST_HEAD(hl_drivers);
static DECLARE_RWSEM(hl_drivers_sem);
static LIST_HEAD(hl_irqs);
static DEFINE_RWLOCK(hl_irqs_lock);
static DEFINE_RWLOCK(addr_space_lock);
/* addr_space list will have zero and max already included as bounds */
static struct hpsb_address_ops dummy_ops = { NULL, NULL, NULL, NULL };
static struct hpsb_address_serve dummy_zero_addr, dummy_max_addr;
static struct hl_host_info *hl_get_hostinfo(struct hpsb_highlevel *hl,
struct hpsb_host *host)
{
struct hl_host_info *hi = NULL;
if (!hl || !host)
return NULL;
read_lock(&hl->host_info_lock);
list_for_each_entry(hi, &hl->host_info_list, list) {
if (hi->host == host) {
read_unlock(&hl->host_info_lock);
return hi;
}
}
read_unlock(&hl->host_info_lock);
return NULL;
}
/**
* hpsb_get_hostinfo - retrieve a hostinfo pointer bound to this driver/host
*
* Returns a per @host and @hl driver data structure that was previously stored
* by hpsb_create_hostinfo.
*/
void *hpsb_get_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
{
struct hl_host_info *hi = hl_get_hostinfo(hl, host);
return hi ? hi->data : NULL;
}
/**
* hpsb_create_hostinfo - allocate a hostinfo pointer bound to this driver/host
*
* Allocate a hostinfo pointer backed by memory with @data_size and bind it to
* to this @hl driver and @host. If @data_size is zero, then the return here is
* only valid for error checking.
*/
void *hpsb_create_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
size_t data_size)
{
struct hl_host_info *hi;
void *data;
unsigned long flags;
hi = hl_get_hostinfo(hl, host);
if (hi) {
HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already"
" exists", hl->name);
return NULL;
}
hi = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
if (!hi)
return NULL;
if (data_size) {
data = hi->data = hi + 1;
hi->size = data_size;
} else
data = hi;
hi->host = host;
write_lock_irqsave(&hl->host_info_lock, flags);
list_add_tail(&hi->list, &hl->host_info_list);
write_unlock_irqrestore(&hl->host_info_lock, flags);
return data;
}
/**
* hpsb_set_hostinfo - set the hostinfo pointer to something useful
*
* Usually follows a call to hpsb_create_hostinfo, where the size is 0.
*/
int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
void *data)
{
struct hl_host_info *hi;
hi = hl_get_hostinfo(hl, host);
if (hi) {
if (!hi->size && !hi->data) {
hi->data = data;
return 0;
} else
HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo "
"already has data", hl->name);
} else
HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
hl->name);
return -EINVAL;
}
/**
* hpsb_destroy_hostinfo - free and remove a hostinfo pointer
*
* Free and remove the hostinfo pointer bound to this @hl driver and @host.
*/
void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
{
struct hl_host_info *hi;
hi = hl_get_hostinfo(hl, host);
if (hi) {
unsigned long flags;
write_lock_irqsave(&hl->host_info_lock, flags);
list_del(&hi->list);
write_unlock_irqrestore(&hl->host_info_lock, flags);
kfree(hi);
}
return;
}
/**
* hpsb_set_hostinfo_key - set an alternate lookup key for an hostinfo
*
* Sets an alternate lookup key for the hostinfo bound to this @hl driver and
* @host.
*/
void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host,
unsigned long key)
{
struct hl_host_info *hi;
hi = hl_get_hostinfo(hl, host);
if (hi)
hi->key = key;
return;
}
/**
* hpsb_get_hostinfo_bykey - retrieve a hostinfo pointer by its alternate key
*/
void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
{
struct hl_host_info *hi;
void *data = NULL;
if (!hl)
return NULL;
read_lock(&hl->host_info_lock);
list_for_each_entry(hi, &hl->host_info_list, list) {
if (hi->key == key) {
data = hi->data;
break;
}
}
read_unlock(&hl->host_info_lock);
return data;
}
static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
{
struct hpsb_highlevel *hl = __data;
hl->add_host(host);
if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
HPSB_ERR("Failed to generate Configuration ROM image for host "
"%s-%d", hl->name, host->id);
return 0;
}
/**
* hpsb_register_highlevel - register highlevel driver
*
* The name pointer in @hl has to stay valid at all times because the string is
* not copied.
*/
void hpsb_register_highlevel(struct hpsb_highlevel *hl)
{
unsigned long flags;
INIT_LIST_HEAD(&hl->addr_list);
INIT_LIST_HEAD(&hl->host_info_list);
rwlock_init(&hl->host_info_lock);
down_write(&hl_drivers_sem);
list_add_tail(&hl->hl_list, &hl_drivers);
up_write(&hl_drivers_sem);
write_lock_irqsave(&hl_irqs_lock, flags);
list_add_tail(&hl->irq_list, &hl_irqs);
write_unlock_irqrestore(&hl_irqs_lock, flags);
if (hl->add_host)
nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
return;
}
static void __delete_addr(struct hpsb_address_serve *as)
{
list_del(&as->host_list);
list_del(&as->hl_list);
kfree(as);
}
static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host,
int update_cr)
{
unsigned long flags;
struct list_head *lh, *next;
struct hpsb_address_serve *as;
/* First, let the highlevel driver unreg */
if (hl->remove_host)
hl->remove_host(host);
/* Remove any addresses that are matched for this highlevel driver
* and this particular host. */
write_lock_irqsave(&addr_space_lock, flags);
list_for_each_safe (lh, next, &hl->addr_list) {
as = list_entry(lh, struct hpsb_address_serve, hl_list);
if (as->host == host)
__delete_addr(as);
}
write_unlock_irqrestore(&addr_space_lock, flags);
/* Now update the config-rom to reflect anything removed by the
* highlevel driver. */
if (update_cr && host->update_config_rom &&
hpsb_update_config_rom_image(host) < 0)
HPSB_ERR("Failed to generate Configuration ROM image for host "
"%s-%d", hl->name, host->id);
/* Finally remove all the host info associated between these two. */
hpsb_destroy_hostinfo(hl, host);
}
static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
{
struct hpsb_highlevel *hl = __data;
__unregister_host(hl, host, 1);
return 0;
}
/**
* hpsb_unregister_highlevel - unregister highlevel driver
*/
void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
{
unsigned long flags;
write_lock_irqsave(&hl_irqs_lock, flags);
list_del(&hl->irq_list);
write_unlock_irqrestore(&hl_irqs_lock, flags);
down_write(&hl_drivers_sem);
list_del(&hl->hl_list);
up_write(&hl_drivers_sem);
nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
}
/**
* hpsb_allocate_and_register_addrspace - alloc' and reg' a host address space
*
* @start and @end are 48 bit pointers and have to be quadlet aligned.
* @end points to the first address behind the handled addresses. This
* function can be called multiple times for a single hpsb_highlevel @hl to
* implement sparse register sets. The requested region must not overlap any
* previously allocated region, otherwise registering will fail.
*
* It returns true for successful allocation. Address spaces can be
* unregistered with hpsb_unregister_addrspace. All remaining address spaces
* are automatically deallocated together with the hpsb_highlevel @hl.
*/
u64 hpsb_allocate_and_register_addrspace(struct hpsb_highlevel *hl,
struct hpsb_host *host,
struct hpsb_address_ops *ops,
u64 size, u64 alignment,
u64 start, u64 end)
{
struct hpsb_address_serve *as, *a1, *a2;
struct list_head *entry;
u64 retval = CSR1212_INVALID_ADDR_SPACE;
unsigned long flags;
u64 align_mask = ~(alignment - 1);
if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
(hweight64(alignment) != 1)) {
HPSB_ERR("%s called with invalid alignment: 0x%048llx",
__FUNCTION__, (unsigned long long)alignment);
return retval;
}
/* default range,
* avoids controller's posted write area (see OHCI 1.1 clause 1.5) */
if (start == CSR1212_INVALID_ADDR_SPACE &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -