* no locks are applied!
*/
-void xillybus_do_cleanup(struct xilly_cleanup *mem,
- struct xilly_endpoint *endpoint)
-{
- struct list_head *this, *next;
-
- list_for_each_safe(this, next, &mem->to_unmap) {
- struct xilly_dma *entry =
- list_entry(this, struct xilly_dma, node);
-
- endpoint->ephw->unmap_single(entry);
- kfree(entry);
- }
-
- INIT_LIST_HEAD(&mem->to_unmap);
-
- list_for_each_safe(this, next, &mem->to_kfree)
- kfree(this);
-
- INIT_LIST_HEAD(&mem->to_kfree);
-
- list_for_each_safe(this, next, &mem->to_pagefree) {
- struct xilly_page *entry =
- list_entry(this, struct xilly_page, node);
-
- free_pages(entry->addr, entry->order);
- kfree(entry);
- }
- INIT_LIST_HEAD(&mem->to_pagefree);
-}
-EXPORT_SYMBOL(xillybus_do_cleanup);
-
-static void *xilly_malloc(struct xilly_cleanup *mem, size_t size)
-{
- void *ptr;
-
- ptr = kzalloc(sizeof(struct list_head) + size, GFP_KERNEL);
-
- if (!ptr)
- return ptr;
-
- list_add_tail((struct list_head *) ptr, &mem->to_kfree);
-
- return ptr + sizeof(struct list_head);
-}
-
-static unsigned long xilly_pagealloc(struct xilly_cleanup *mem,
- unsigned long order)
-{
- unsigned long addr;
- struct xilly_page *this;
-
- this = kmalloc(sizeof(struct xilly_page), GFP_KERNEL);
- if (!this)
- return 0;
-
- addr = __get_free_pages(GFP_KERNEL | __GFP_DMA32 | __GFP_ZERO, order);
-
- if (!addr) {
- kfree(this);
- return 0;
- }
-
- this->addr = addr;
- this->order = order;
-
- list_add_tail(&this->node, &mem->to_pagefree);
-
- return addr;
-}
-
-
static void xillybus_autoflush(struct work_struct *work);
static int xilly_setupchannels(struct xilly_endpoint *ep,
- struct xilly_cleanup *mem,
unsigned char *chandesc,
int entries
)
{
+ struct device *dev = ep->dev;
int i, entry, wr_nbuffer, rd_nbuffer;
struct xilly_channel *channel;
int channelnum, bufnum, bufsize, format, is_writebuf;
int left_of_rd_salami = 0;
dma_addr_t dma_addr;
int msg_buf_done = 0;
+ const gfp_t gfp_mask = GFP_KERNEL | __GFP_DMA32 | __GFP_ZERO;
struct xilly_buffer *this_buffer = NULL; /* Init to silence warning */
+ int rc = 0;
- channel = xilly_malloc(mem, ep->num_channels *
- sizeof(struct xilly_channel));
+ channel = devm_kzalloc(dev, ep->num_channels *
+ sizeof(struct xilly_channel), GFP_KERNEL);
if (!channel)
goto memfail;
- ep->channels = xilly_malloc(mem, (ep->num_channels + 1) *
- sizeof(struct xilly_channel *));
+ ep->channels = devm_kzalloc(dev, (ep->num_channels + 1) *
+ sizeof(struct xilly_channel *),
+ GFP_KERNEL);
if (!ep->channels)
goto memfail;
channel->rd_exclusive_open = exclusive_open;
channel->seekable = seekable;
- channel->rd_buffers = xilly_malloc(
- mem,
- bufnum * sizeof(struct xilly_buffer *));
+ channel->rd_buffers = devm_kzalloc(dev,
+ bufnum * sizeof(struct xilly_buffer *),
+ GFP_KERNEL);
if (!channel->rd_buffers)
goto memfail;
- this_buffer = xilly_malloc(
- mem,
- bufnum * sizeof(struct xilly_buffer));
+ this_buffer = devm_kzalloc(dev,
+ bufnum * sizeof(struct xilly_buffer),
+ GFP_KERNEL);
if (!this_buffer)
goto memfail;
channel->wr_synchronous = synchronous;
channel->wr_exclusive_open = exclusive_open;
- channel->wr_buffers = xilly_malloc(
- mem,
- bufnum * sizeof(struct xilly_buffer *));
+ channel->wr_buffers = devm_kzalloc(dev,
+ bufnum * sizeof(struct xilly_buffer *),
+ GFP_KERNEL);
if (!channel->wr_buffers)
goto memfail;
- this_buffer = xilly_malloc(
- mem,
- bufnum * sizeof(struct xilly_buffer));
+ this_buffer = devm_kzalloc(dev,
+ bufnum * sizeof(struct xilly_buffer),
+ GFP_KERNEL);
if (!this_buffer)
goto memfail;
}
wr_salami = (void *)
- xilly_pagealloc(mem,
- allocorder);
+ devm_get_free_pages(
+ dev, gfp_mask,
+ allocorder);
+
if (!wr_salami)
goto memfail;
left_of_wr_salami = allocsize;
}
- dma_addr = ep->ephw->map_single(
- mem,
- ep,
- wr_salami,
- bytebufsize,
- DMA_FROM_DEVICE);
+ rc = ep->ephw->map_single(ep, wr_salami,
+ bytebufsize,
+ DMA_FROM_DEVICE,
+ &dma_addr);
- if (!dma_addr)
+ if (rc)
goto dmafail;
iowrite32(
}
rd_salami = (void *)
- xilly_pagealloc(
- mem,
+ devm_get_free_pages(
+ dev, gfp_mask,
allocorder);
if (!rd_salami)
left_of_rd_salami = allocsize;
}
- dma_addr = ep->ephw->map_single(
- mem,
- ep,
- rd_salami,
- bytebufsize,
- DMA_TO_DEVICE);
- if (!dma_addr)
+ rc = ep->ephw->map_single(ep, rd_salami,
+ bytebufsize,
+ DMA_TO_DEVICE,
+ &dma_addr);
+
+ if (rc)
goto dmafail;
iowrite32(
return -ENOMEM;
dmafail:
dev_err(ep->dev, "Failed to map DMA memory!. Aborting.\n");
- return -ENOMEM;
+ return rc;
}
static void xilly_scan_idt(struct xilly_endpoint *endpoint,
endpoint->pdev = pdev;
endpoint->dev = dev;
endpoint->ephw = ephw;
- INIT_LIST_HEAD(&endpoint->cleanup.to_kfree);
- INIT_LIST_HEAD(&endpoint->cleanup.to_pagefree);
- INIT_LIST_HEAD(&endpoint->cleanup.to_unmap);
endpoint->msg_counter = 0x0b;
endpoint->failed_messages = 0;
endpoint->fatal_error = 0;
if (endpoint->idtlen < 0) {
dev_err(endpoint->dev,
- "Failed to quiesce the device on exit. Quitting while leaving a mess.\n");
+ "Failed to quiesce the device on exit.\n");
return -ENODEV;
}
return 0; /* Success */
{
int rc = 0;
- struct xilly_cleanup tmpmem;
+ void *bootstrap_resources;
int idtbuffersize = (1 << PAGE_SHIFT);
+ struct device *dev = endpoint->dev;
/*
* The bogus IDT is used during bootstrap for allocating the initial
3, 192, PAGE_SHIFT, 0 };
struct xilly_idt_handle idt_handle;
- INIT_LIST_HEAD(&tmpmem.to_kfree);
- INIT_LIST_HEAD(&tmpmem.to_pagefree);
- INIT_LIST_HEAD(&tmpmem.to_unmap);
-
/*
* Writing the value 0x00000001 to Endianness register signals which
* endianness this processor is using, so the FPGA can swap words as
/* Bootstrap phase I: Allocate temporary message buffer */
+ bootstrap_resources = devres_open_group(dev, NULL, GFP_KERNEL);
+ if (!bootstrap_resources)
+ return -ENOMEM;
+
endpoint->num_channels = 0;
- rc = xilly_setupchannels(endpoint, &tmpmem, bogus_idt, 1);
+ rc = xilly_setupchannels(endpoint, bogus_idt, 1);
if (rc)
- goto failed_buffers;
+ return rc;
/* Clear the message subsystem (and counter in particular) */
iowrite32(0x04, &endpoint->registers[fpga_msg_ctrl_reg]);
if (endpoint->idtlen < 0) {
dev_err(endpoint->dev, "No response from FPGA. Aborting.\n");
- rc = -ENODEV;
- goto failed_quiesce;
+ return -ENODEV;
}
/* Enable DMA */
endpoint->num_channels = 1;
- rc = xilly_setupchannels(endpoint, &tmpmem, bogus_idt, 2);
+ rc = xilly_setupchannels(endpoint, bogus_idt, 2);
if (rc)
goto failed_idt;
rc = -ENODEV;
goto failed_idt;
}
+
+ devres_close_group(dev, bootstrap_resources);
+
/* Bootstrap phase III: Allocate buffers according to IDT */
rc = xilly_setupchannels(endpoint,
- &endpoint->cleanup,
idt_handle.chandesc,
idt_handle.entries);
if (rc)
goto failed_chrdevs;
- xillybus_do_cleanup(&tmpmem, endpoint);
+ devres_release_group(dev, bootstrap_resources);
return 0;
mutex_unlock(&ep_list_lock);
failed_idt:
- /* Quiesce the device. Now it's serious to do it */
- rc = xilly_quiesce(endpoint);
-
- if (rc)
- return rc; /* FPGA may still DMA, so no release */
-
+ xilly_quiesce(endpoint);
flush_workqueue(xillybus_wq);
-failed_quiesce:
-failed_buffers:
- xillybus_do_cleanup(&tmpmem, endpoint);
return rc;
}
{
}
-static dma_addr_t xilly_map_single_of(struct xilly_cleanup *mem,
- struct xilly_endpoint *ep,
- void *ptr,
- size_t size,
- int direction
- )
+static void xilly_of_unmap(void *ptr)
{
+ struct xilly_mapping *data = ptr;
- dma_addr_t addr = 0;
- struct xilly_dma *this;
+ dma_unmap_single(data->device, data->dma_addr,
+ data->size, data->direction);
+
+ kfree(ptr);
+}
+
+static int xilly_map_single_of(struct xilly_endpoint *ep,
+ void *ptr,
+ size_t size,
+ int direction,
+ dma_addr_t *ret_dma_handle
+ )
+{
+ dma_addr_t addr;
+ struct xilly_mapping *this;
+ int rc;
- this = kmalloc(sizeof(struct xilly_dma), GFP_KERNEL);
+ this = kzalloc(sizeof(*this), GFP_KERNEL);
if (!this)
- return 0;
+ return -ENOMEM;
addr = dma_map_single(ep->dev, ptr, size, direction);
- this->direction = direction;
if (dma_mapping_error(ep->dev, addr)) {
kfree(this);
- return 0;
+ return -ENODEV;
}
+ this->device = ep->dev;
this->dma_addr = addr;
- this->dev = ep->dev;
this->size = size;
+ this->direction = direction;
- list_add_tail(&this->node, &mem->to_unmap);
+ *ret_dma_handle = addr;
- return addr;
-}
+ rc = devm_add_action(ep->dev, xilly_of_unmap, this);
-static void xilly_unmap_single_of(struct xilly_dma *entry)
-{
- dma_unmap_single(entry->dev,
- entry->dma_addr,
- entry->size,
- entry->direction);
+ if (rc) {
+ dma_unmap_single(ep->dev, addr, size, direction);
+ kfree(this);
+ }
+
+ return rc;
}
static struct xilly_endpoint_hardware of_hw = {
.hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_of,
.hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_of,
.map_single = xilly_map_single_of,
- .unmap_single = xilly_unmap_single_of
};
static struct xilly_endpoint_hardware of_hw_coherent = {
.hw_sync_sgl_for_cpu = xilly_dma_sync_single_nop,
.hw_sync_sgl_for_device = xilly_dma_sync_single_nop,
.map_single = xilly_map_single_of,
- .unmap_single = xilly_unmap_single_of
};
static int xilly_drv_probe(struct platform_device *op)
dev_set_drvdata(dev, endpoint);
rc = of_address_to_resource(dev->of_node, 0, &res);
- if (rc) {
- dev_warn(endpoint->dev,
- "Failed to obtain device tree resource\n");
- return rc;
- }
-
endpoint->registers = devm_ioremap_resource(dev, &res);
if (IS_ERR(endpoint->registers))
return -ENODEV;
}
- rc = xillybus_endpoint_discovery(endpoint);
-
- if (!rc)
- return 0;
-
- xillybus_do_cleanup(&endpoint->cleanup, endpoint);
-
- return rc;
+ return xillybus_endpoint_discovery(endpoint);
}
static int xilly_drv_remove(struct platform_device *op)
xillybus_endpoint_remove(endpoint);
- xillybus_do_cleanup(&endpoint->cleanup, endpoint);
-
return 0;
}
xilly_pci_direction(direction));
}
+static void xilly_pci_unmap(void *ptr)
+{
+ struct xilly_mapping *data = ptr;
+
+ pci_unmap_single(data->device, data->dma_addr,
+ data->size, data->direction);
+
+ kfree(ptr);
+}
+
/*
* Map either through the PCI DMA mapper or the non_PCI one. Behind the
* scenes exactly the same functions are called with the same parameters,
* but that can change.
*/
-static dma_addr_t xilly_map_single_pci(struct xilly_cleanup *mem,
- struct xilly_endpoint *ep,
- void *ptr,
- size_t size,
- int direction
+static int xilly_map_single_pci(struct xilly_endpoint *ep,
+ void *ptr,
+ size_t size,
+ int direction,
+ dma_addr_t *ret_dma_handle
)
{
-
- dma_addr_t addr = 0;
- struct xilly_dma *this;
int pci_direction;
+ dma_addr_t addr;
+ struct xilly_mapping *this;
+ int rc = 0;
- this = kmalloc(sizeof(struct xilly_dma), GFP_KERNEL);
+ this = kzalloc(sizeof(*this), GFP_KERNEL);
if (!this)
- return 0;
+ return -ENOMEM;
pci_direction = xilly_pci_direction(direction);
+
addr = pci_map_single(ep->pdev, ptr, size, pci_direction);
- this->direction = pci_direction;
if (pci_dma_mapping_error(ep->pdev, addr)) {
kfree(this);
- return 0;
+ return -ENODEV;
}
+ this->device = ep->pdev;
this->dma_addr = addr;
- this->pdev = ep->pdev;
this->size = size;
+ this->direction = pci_direction;
- list_add_tail(&this->node, &mem->to_unmap);
+ *ret_dma_handle = addr;
- return addr;
-}
+ rc = devm_add_action(ep->dev, xilly_pci_unmap, this);
-static void xilly_unmap_single_pci(struct xilly_dma *entry)
-{
- pci_unmap_single(entry->pdev,
- entry->dma_addr,
- entry->size,
- entry->direction);
+ if (rc) {
+ pci_unmap_single(ep->pdev, addr, size, pci_direction);
+ kfree(this);
+ }
+
+ return rc;
}
static struct xilly_endpoint_hardware pci_hw = {
.hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_pci,
.hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_pci,
.map_single = xilly_map_single_pci,
- .unmap_single = xilly_unmap_single_pci
};
static int xilly_probe(struct pci_dev *pdev,
return -ENODEV;
}
- rc = xillybus_endpoint_discovery(endpoint);
-
- if (!rc)
- return 0;
-
- xillybus_do_cleanup(&endpoint->cleanup, endpoint);
-
- return rc;
+ return xillybus_endpoint_discovery(endpoint);
}
static void xilly_remove(struct pci_dev *pdev)
struct xilly_endpoint *endpoint = pci_get_drvdata(pdev);
xillybus_endpoint_remove(endpoint);
-
- xillybus_do_cleanup(&endpoint->cleanup, endpoint);
}
MODULE_DEVICE_TABLE(pci, xillyids);