M7350/kernel/drivers/net/ethernet/sfc/mtd.c

134 lines
3.0 KiB
C
Raw Normal View History

2024-09-09 08:52:07 +00:00
/****************************************************************************
2024-09-09 08:57:42 +00:00
* Driver for Solarflare network controllers and boards
2024-09-09 08:52:07 +00:00
* Copyright 2005-2006 Fen Systems Ltd.
2024-09-09 08:57:42 +00:00
* Copyright 2006-2013 Solarflare Communications Inc.
2024-09-09 08:52:07 +00:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, incorporated herein by reference.
*/
#include <linux/module.h>
#include <linux/mtd/mtd.h>
#include <linux/slab.h>
#include <linux/rtnetlink.h>
#include "net_driver.h"
#include "efx.h"
#define to_efx_mtd_partition(mtd) \
container_of(mtd, struct efx_mtd_partition, mtd)
/* MTD interface */
static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
{
2024-09-09 08:57:42 +00:00
struct efx_nic *efx = mtd->priv;
2024-09-09 08:52:07 +00:00
int rc;
2024-09-09 08:57:42 +00:00
rc = efx->type->mtd_erase(mtd, erase->addr, erase->len);
2024-09-09 08:52:07 +00:00
if (rc == 0) {
erase->state = MTD_ERASE_DONE;
} else {
erase->state = MTD_ERASE_FAILED;
erase->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
}
mtd_erase_callback(erase);
return rc;
}
static void efx_mtd_sync(struct mtd_info *mtd)
{
struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
2024-09-09 08:57:42 +00:00
struct efx_nic *efx = mtd->priv;
2024-09-09 08:52:07 +00:00
int rc;
2024-09-09 08:57:42 +00:00
rc = efx->type->mtd_sync(mtd);
2024-09-09 08:52:07 +00:00
if (rc)
pr_err("%s: %s sync failed (%d)\n",
2024-09-09 08:57:42 +00:00
part->name, part->dev_type_name, rc);
2024-09-09 08:52:07 +00:00
}
static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
{
int rc;
for (;;) {
rc = mtd_device_unregister(&part->mtd);
if (rc != -EBUSY)
break;
ssleep(1);
}
WARN_ON(rc);
2024-09-09 08:57:42 +00:00
list_del(&part->node);
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
size_t n_parts, size_t sizeof_part)
2024-09-09 08:52:07 +00:00
{
struct efx_mtd_partition *part;
2024-09-09 08:57:42 +00:00
size_t i;
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
for (i = 0; i < n_parts; i++) {
part = (struct efx_mtd_partition *)((char *)parts +
i * sizeof_part);
2024-09-09 08:52:07 +00:00
part->mtd.writesize = 1;
part->mtd.owner = THIS_MODULE;
2024-09-09 08:57:42 +00:00
part->mtd.priv = efx;
2024-09-09 08:52:07 +00:00
part->mtd.name = part->name;
part->mtd._erase = efx_mtd_erase;
2024-09-09 08:57:42 +00:00
part->mtd._read = efx->type->mtd_read;
part->mtd._write = efx->type->mtd_write;
2024-09-09 08:52:07 +00:00
part->mtd._sync = efx_mtd_sync;
2024-09-09 08:57:42 +00:00
efx->type->mtd_rename(part);
2024-09-09 08:52:07 +00:00
if (mtd_device_register(&part->mtd, NULL, 0))
goto fail;
2024-09-09 08:57:42 +00:00
/* Add to list in order - efx_mtd_remove() depends on this */
list_add_tail(&part->node, &efx->mtd_list);
2024-09-09 08:52:07 +00:00
}
return 0;
fail:
2024-09-09 08:57:42 +00:00
while (i--) {
part = (struct efx_mtd_partition *)((char *)parts +
i * sizeof_part);
2024-09-09 08:52:07 +00:00
efx_mtd_remove_partition(part);
}
/* Failure is unlikely here, but probably means we're out of memory */
return -ENOMEM;
}
void efx_mtd_remove(struct efx_nic *efx)
{
2024-09-09 08:57:42 +00:00
struct efx_mtd_partition *parts, *part, *next;
2024-09-09 08:52:07 +00:00
WARN_ON(efx_dev_registered(efx));
2024-09-09 08:57:42 +00:00
if (list_empty(&efx->mtd_list))
return;
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
node);
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
list_for_each_entry_safe(part, next, &efx->mtd_list, node)
efx_mtd_remove_partition(part);
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
kfree(parts);
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
void efx_mtd_rename(struct efx_nic *efx)
2024-09-09 08:52:07 +00:00
{
struct efx_mtd_partition *part;
ASSERT_RTNL();
2024-09-09 08:57:42 +00:00
list_for_each_entry(part, &efx->mtd_list, node)
efx->type->mtd_rename(part);
2024-09-09 08:52:07 +00:00
}