linux/drivers/net/ethernet/hisilicon/hns/hnae.c
Yonglong Liu 8601a99d7c net: hns: Fix WARNING when remove HNS driver with SMMU enabled
When enable SMMU, remove HNS driver will cause a WARNING:

[  141.924177] WARNING: CPU: 36 PID: 2708 at drivers/iommu/dma-iommu.c:443 __iommu_dma_unmap+0xc0/0xc8
[  141.954673] Modules linked in: hns_enet_drv(-)
[  141.963615] CPU: 36 PID: 2708 Comm: rmmod Tainted: G        W         5.0.0-rc1-28723-gb729c57de95c-dirty #32
[  141.983593] Hardware name: Huawei D05/D05, BIOS Hisilicon D05 UEFI Nemo 1.8 RC0 08/31/2017
[  142.000244] pstate: 60000005 (nZCv daif -PAN -UAO)
[  142.009886] pc : __iommu_dma_unmap+0xc0/0xc8
[  142.018476] lr : __iommu_dma_unmap+0xc0/0xc8
[  142.027066] sp : ffff000013533b90
[  142.033728] x29: ffff000013533b90 x28: ffff8013e6983600
[  142.044420] x27: 0000000000000000 x26: 0000000000000000
[  142.055113] x25: 0000000056000000 x24: 0000000000000015
[  142.065806] x23: 0000000000000028 x22: ffff8013e66eee68
[  142.076499] x21: ffff8013db919800 x20: 0000ffffefbff000
[  142.087192] x19: 0000000000001000 x18: 0000000000000007
[  142.097885] x17: 000000000000000e x16: 0000000000000001
[  142.108578] x15: 0000000000000019 x14: 363139343a70616d
[  142.119270] x13: 6e75656761705f67 x12: 0000000000000000
[  142.129963] x11: 00000000ffffffff x10: 0000000000000006
[  142.140656] x9 : 1346c1aa88093500 x8 : ffff0000114de4e0
[  142.151349] x7 : 6662666578303d72 x6 : ffff0000105ffec8
[  142.162042] x5 : 0000000000000000 x4 : 0000000000000000
[  142.172734] x3 : 00000000ffffffff x2 : ffff0000114de500
[  142.183427] x1 : 0000000000000000 x0 : 0000000000000035
[  142.194120] Call trace:
[  142.199030]  __iommu_dma_unmap+0xc0/0xc8
[  142.206920]  iommu_dma_unmap_page+0x20/0x28
[  142.215335]  __iommu_unmap_page+0x40/0x60
[  142.223399]  hnae_unmap_buffer+0x110/0x134
[  142.231639]  hnae_free_desc+0x6c/0x10c
[  142.239177]  hnae_fini_ring+0x14/0x34
[  142.246540]  hnae_fini_queue+0x2c/0x40
[  142.254080]  hnae_put_handle+0x38/0xcc
[  142.261619]  hns_nic_dev_remove+0x54/0xfc [hns_enet_drv]
[  142.272312]  platform_drv_remove+0x24/0x64
[  142.280552]  device_release_driver_internal+0x17c/0x20c
[  142.291070]  driver_detach+0x4c/0x90
[  142.298259]  bus_remove_driver+0x5c/0xd8
[  142.306148]  driver_unregister+0x2c/0x54
[  142.314037]  platform_driver_unregister+0x10/0x18
[  142.323505]  hns_nic_dev_driver_exit+0x14/0xf0c [hns_enet_drv]
[  142.335248]  __arm64_sys_delete_module+0x214/0x25c
[  142.344891]  el0_svc_common+0xb0/0x10c
[  142.352430]  el0_svc_handler+0x24/0x80
[  142.359968]  el0_svc+0x8/0x7c0
[  142.366104] ---[ end trace 60ad1cd58e63c407 ]---

The tx ring buffer map when xmit and unmap when xmit done. So in
hnae_init_ring() did not map tx ring buffer, but in hnae_fini_ring()
have a unmap operation for tx ring buffer, which is already unmapped
when xmit done, than cause this WARNING.

The hnae_alloc_buffers() is called in hnae_init_ring(),
so the hnae_free_buffers() should be in hnae_fini_ring(), not in
hnae_free_desc().

In hnae_fini_ring(), adds a check is_rx_ring() as in hnae_init_ring().
When the ring buffer is tx ring, adds a piece of code to ensure that
the tx ring is unmap.

Signed-off-by: Yonglong Liu <liuyonglong@huawei.com>
Signed-off-by: Peng Li <lipeng321@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-04 10:35:42 -07:00

474 lines
10 KiB
C

/*
* Copyright (c) 2014-2015 Hisilicon Limited.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/of.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include "hnae.h"
#define cls_to_ae_dev(dev) container_of(dev, struct hnae_ae_dev, cls_dev)
static struct class *hnae_class;
static void
hnae_list_add(spinlock_t *lock, struct list_head *node, struct list_head *head)
{
unsigned long flags;
spin_lock_irqsave(lock, flags);
list_add_tail_rcu(node, head);
spin_unlock_irqrestore(lock, flags);
}
static void hnae_list_del(spinlock_t *lock, struct list_head *node)
{
unsigned long flags;
spin_lock_irqsave(lock, flags);
list_del_rcu(node);
spin_unlock_irqrestore(lock, flags);
}
static int hnae_alloc_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
{
unsigned int order = hnae_page_order(ring);
struct page *p = dev_alloc_pages(order);
if (!p)
return -ENOMEM;
cb->priv = p;
cb->page_offset = 0;
cb->reuse_flag = 0;
cb->buf = page_address(p);
cb->length = hnae_page_size(ring);
cb->type = DESC_TYPE_PAGE;
return 0;
}
static void hnae_free_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
{
if (unlikely(!cb->priv))
return;
if (cb->type == DESC_TYPE_SKB)
dev_kfree_skb_any((struct sk_buff *)cb->priv);
else if (unlikely(is_rx_ring(ring)))
put_page((struct page *)cb->priv);
cb->priv = NULL;
}
static int hnae_map_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
{
cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0,
cb->length, ring_to_dma_dir(ring));
if (dma_mapping_error(ring_to_dev(ring), cb->dma))
return -EIO;
return 0;
}
static void hnae_unmap_buffer(struct hnae_ring *ring, struct hnae_desc_cb *cb)
{
if (cb->type == DESC_TYPE_SKB)
dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length,
ring_to_dma_dir(ring));
else if (cb->length)
dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length,
ring_to_dma_dir(ring));
}
static struct hnae_buf_ops hnae_bops = {
.alloc_buffer = hnae_alloc_buffer,
.free_buffer = hnae_free_buffer,
.map_buffer = hnae_map_buffer,
.unmap_buffer = hnae_unmap_buffer,
};
static int __ae_match(struct device *dev, const void *data)
{
struct hnae_ae_dev *hdev = cls_to_ae_dev(dev);
if (dev_of_node(hdev->dev))
return (data == &hdev->dev->of_node->fwnode);
else if (is_acpi_node(hdev->dev->fwnode))
return (data == hdev->dev->fwnode);
dev_err(dev, "__ae_match cannot read cfg data from OF or acpi\n");
return 0;
}
static struct hnae_ae_dev *find_ae(const struct fwnode_handle *fwnode)
{
struct device *dev;
WARN_ON(!fwnode);
dev = class_find_device(hnae_class, NULL, fwnode, __ae_match);
return dev ? cls_to_ae_dev(dev) : NULL;
}
static void hnae_free_buffers(struct hnae_ring *ring)
{
int i;
for (i = 0; i < ring->desc_num; i++)
hnae_free_buffer_detach(ring, i);
}
/* Allocate memory for raw pkg, and map with dma */
static int hnae_alloc_buffers(struct hnae_ring *ring)
{
int i, j, ret;
for (i = 0; i < ring->desc_num; i++) {
ret = hnae_alloc_buffer_attach(ring, i);
if (ret)
goto out_buffer_fail;
}
return 0;
out_buffer_fail:
for (j = i - 1; j >= 0; j--)
hnae_free_buffer_detach(ring, j);
return ret;
}
/* free desc along with its attached buffer */
static void hnae_free_desc(struct hnae_ring *ring)
{
dma_unmap_single(ring_to_dev(ring), ring->desc_dma_addr,
ring->desc_num * sizeof(ring->desc[0]),
ring_to_dma_dir(ring));
ring->desc_dma_addr = 0;
kfree(ring->desc);
ring->desc = NULL;
}
/* alloc desc, without buffer attached */
static int hnae_alloc_desc(struct hnae_ring *ring)
{
int size = ring->desc_num * sizeof(ring->desc[0]);
ring->desc = kzalloc(size, GFP_KERNEL);
if (!ring->desc)
return -ENOMEM;
ring->desc_dma_addr = dma_map_single(ring_to_dev(ring),
ring->desc, size, ring_to_dma_dir(ring));
if (dma_mapping_error(ring_to_dev(ring), ring->desc_dma_addr)) {
ring->desc_dma_addr = 0;
kfree(ring->desc);
ring->desc = NULL;
return -ENOMEM;
}
return 0;
}
/* fini ring, also free the buffer for the ring */
static void hnae_fini_ring(struct hnae_ring *ring)
{
if (is_rx_ring(ring))
hnae_free_buffers(ring);
hnae_free_desc(ring);
kfree(ring->desc_cb);
ring->desc_cb = NULL;
ring->next_to_clean = 0;
ring->next_to_use = 0;
}
/* init ring, and with buffer for rx ring */
static int
hnae_init_ring(struct hnae_queue *q, struct hnae_ring *ring, int flags)
{
int ret;
if (ring->desc_num <= 0 || ring->buf_size <= 0)
return -EINVAL;
ring->q = q;
ring->flags = flags;
spin_lock_init(&ring->lock);
ring->coal_param = q->handle->coal_param;
assert(!ring->desc && !ring->desc_cb && !ring->desc_dma_addr);
/* not matter for tx or rx ring, the ntc and ntc start from 0 */
assert(ring->next_to_use == 0);
assert(ring->next_to_clean == 0);
ring->desc_cb = kcalloc(ring->desc_num, sizeof(ring->desc_cb[0]),
GFP_KERNEL);
if (!ring->desc_cb) {
ret = -ENOMEM;
goto out;
}
ret = hnae_alloc_desc(ring);
if (ret)
goto out_with_desc_cb;
if (is_rx_ring(ring)) {
ret = hnae_alloc_buffers(ring);
if (ret)
goto out_with_desc;
}
return 0;
out_with_desc:
hnae_free_desc(ring);
out_with_desc_cb:
kfree(ring->desc_cb);
ring->desc_cb = NULL;
out:
return ret;
}
static int hnae_init_queue(struct hnae_handle *h, struct hnae_queue *q,
struct hnae_ae_dev *dev)
{
int ret;
q->dev = dev;
q->handle = h;
ret = hnae_init_ring(q, &q->tx_ring, q->tx_ring.flags | RINGF_DIR);
if (ret)
goto out;
ret = hnae_init_ring(q, &q->rx_ring, q->rx_ring.flags & ~RINGF_DIR);
if (ret)
goto out_with_tx_ring;
if (dev->ops->init_queue)
dev->ops->init_queue(q);
return 0;
out_with_tx_ring:
hnae_fini_ring(&q->tx_ring);
out:
return ret;
}
static void hnae_fini_queue(struct hnae_queue *q)
{
if (q->dev->ops->fini_queue)
q->dev->ops->fini_queue(q);
hnae_fini_ring(&q->tx_ring);
hnae_fini_ring(&q->rx_ring);
}
/**
* ae_chain - define ae chain head
*/
static RAW_NOTIFIER_HEAD(ae_chain);
int hnae_register_notifier(struct notifier_block *nb)
{
return raw_notifier_chain_register(&ae_chain, nb);
}
EXPORT_SYMBOL(hnae_register_notifier);
void hnae_unregister_notifier(struct notifier_block *nb)
{
if (raw_notifier_chain_unregister(&ae_chain, nb))
dev_err(NULL, "notifier chain unregister fail\n");
}
EXPORT_SYMBOL(hnae_unregister_notifier);
int hnae_reinit_handle(struct hnae_handle *handle)
{
int i, j;
int ret;
for (i = 0; i < handle->q_num; i++) /* free ring*/
hnae_fini_queue(handle->qs[i]);
if (handle->dev->ops->reset)
handle->dev->ops->reset(handle);
for (i = 0; i < handle->q_num; i++) {/* reinit ring*/
ret = hnae_init_queue(handle, handle->qs[i], handle->dev);
if (ret)
goto out_when_init_queue;
}
return 0;
out_when_init_queue:
for (j = i - 1; j >= 0; j--)
hnae_fini_queue(handle->qs[j]);
return ret;
}
EXPORT_SYMBOL(hnae_reinit_handle);
/* hnae_get_handle - get a handle from the AE
* @owner_dev: the dev use this handle
* @ae_id: the id of the ae to be used
* @ae_opts: the options set for the handle
* @bops: the callbacks for buffer management
*
* return handle ptr or ERR_PTR
*/
struct hnae_handle *hnae_get_handle(struct device *owner_dev,
const struct fwnode_handle *fwnode,
u32 port_id,
struct hnae_buf_ops *bops)
{
struct hnae_ae_dev *dev;
struct hnae_handle *handle;
int i, j;
int ret;
dev = find_ae(fwnode);
if (!dev)
return ERR_PTR(-ENODEV);
handle = dev->ops->get_handle(dev, port_id);
if (IS_ERR(handle)) {
put_device(&dev->cls_dev);
return handle;
}
handle->dev = dev;
handle->owner_dev = owner_dev;
handle->bops = bops ? bops : &hnae_bops;
handle->eport_id = port_id;
for (i = 0; i < handle->q_num; i++) {
ret = hnae_init_queue(handle, handle->qs[i], dev);
if (ret)
goto out_when_init_queue;
}
__module_get(dev->owner);
hnae_list_add(&dev->lock, &handle->node, &dev->handle_list);
return handle;
out_when_init_queue:
for (j = i - 1; j >= 0; j--)
hnae_fini_queue(handle->qs[j]);
put_device(&dev->cls_dev);
return ERR_PTR(-ENOMEM);
}
EXPORT_SYMBOL(hnae_get_handle);
void hnae_put_handle(struct hnae_handle *h)
{
struct hnae_ae_dev *dev = h->dev;
int i;
for (i = 0; i < h->q_num; i++)
hnae_fini_queue(h->qs[i]);
if (h->dev->ops->reset)
h->dev->ops->reset(h);
hnae_list_del(&dev->lock, &h->node);
if (dev->ops->put_handle)
dev->ops->put_handle(h);
module_put(dev->owner);
put_device(&dev->cls_dev);
}
EXPORT_SYMBOL(hnae_put_handle);
static void hnae_release(struct device *dev)
{
}
/**
* hnae_ae_register - register a AE engine to hnae framework
* @hdev: the hnae ae engine device
* @owner: the module who provides this dev
* NOTE: the duplicated name will not be checked
*/
int hnae_ae_register(struct hnae_ae_dev *hdev, struct module *owner)
{
static atomic_t id = ATOMIC_INIT(-1);
int ret;
if (!hdev->dev)
return -ENODEV;
if (!hdev->ops || !hdev->ops->get_handle ||
!hdev->ops->toggle_ring_irq ||
!hdev->ops->get_status || !hdev->ops->adjust_link)
return -EINVAL;
hdev->owner = owner;
hdev->id = (int)atomic_inc_return(&id);
hdev->cls_dev.parent = hdev->dev;
hdev->cls_dev.class = hnae_class;
hdev->cls_dev.release = hnae_release;
(void)dev_set_name(&hdev->cls_dev, "hnae%d", hdev->id);
ret = device_register(&hdev->cls_dev);
if (ret)
return ret;
__module_get(THIS_MODULE);
INIT_LIST_HEAD(&hdev->handle_list);
spin_lock_init(&hdev->lock);
ret = raw_notifier_call_chain(&ae_chain, HNAE_AE_REGISTER, NULL);
if (ret)
dev_dbg(hdev->dev,
"has not notifier for AE: %s\n", hdev->name);
return 0;
}
EXPORT_SYMBOL(hnae_ae_register);
/**
* hnae_ae_unregister - unregisters a HNAE AE engine
* @cdev: the device to unregister
*/
void hnae_ae_unregister(struct hnae_ae_dev *hdev)
{
device_unregister(&hdev->cls_dev);
module_put(THIS_MODULE);
}
EXPORT_SYMBOL(hnae_ae_unregister);
static int __init hnae_init(void)
{
hnae_class = class_create(THIS_MODULE, "hnae");
return PTR_ERR_OR_ZERO(hnae_class);
}
static void __exit hnae_exit(void)
{
class_destroy(hnae_class);
}
subsys_initcall(hnae_init);
module_exit(hnae_exit);
MODULE_AUTHOR("Hisilicon, Inc.");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hisilicon Network Acceleration Engine Framework");
/* vi: set tw=78 noet: */