xref: /openbmc/linux/drivers/net/ethernet/sfc/mtd.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2874aeea5SJeff Kirsher /****************************************************************************
3f7a6d2c4SBen Hutchings  * Driver for Solarflare network controllers and boards
4874aeea5SJeff Kirsher  * Copyright 2005-2006 Fen Systems Ltd.
5f7a6d2c4SBen Hutchings  * Copyright 2006-2013 Solarflare Communications Inc.
6874aeea5SJeff Kirsher  */
7874aeea5SJeff Kirsher 
8874aeea5SJeff Kirsher #include <linux/module.h>
9874aeea5SJeff Kirsher #include <linux/mtd/mtd.h>
10874aeea5SJeff Kirsher #include <linux/slab.h>
11874aeea5SJeff Kirsher #include <linux/rtnetlink.h>
12874aeea5SJeff Kirsher 
13874aeea5SJeff Kirsher #include "net_driver.h"
14874aeea5SJeff Kirsher #include "efx.h"
15874aeea5SJeff Kirsher 
16874aeea5SJeff Kirsher #define to_efx_mtd_partition(mtd)				\
17874aeea5SJeff Kirsher 	container_of(mtd, struct efx_mtd_partition, mtd)
18874aeea5SJeff Kirsher 
19874aeea5SJeff Kirsher /* MTD interface */
20874aeea5SJeff Kirsher 
efx_mtd_erase(struct mtd_info * mtd,struct erase_info * erase)21874aeea5SJeff Kirsher static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
22874aeea5SJeff Kirsher {
23b766630bSBen Hutchings 	struct efx_nic *efx = mtd->priv;
24874aeea5SJeff Kirsher 
25e7bfb3fdSBoris Brezillon 	return efx->type->mtd_erase(mtd, erase->addr, erase->len);
26874aeea5SJeff Kirsher }
27874aeea5SJeff Kirsher 
efx_mtd_sync(struct mtd_info * mtd)28874aeea5SJeff Kirsher static void efx_mtd_sync(struct mtd_info *mtd)
29874aeea5SJeff Kirsher {
30874aeea5SJeff Kirsher 	struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
31b766630bSBen Hutchings 	struct efx_nic *efx = mtd->priv;
32874aeea5SJeff Kirsher 	int rc;
33874aeea5SJeff Kirsher 
3445a3fd55SBen Hutchings 	rc = efx->type->mtd_sync(mtd);
35874aeea5SJeff Kirsher 	if (rc)
36874aeea5SJeff Kirsher 		pr_err("%s: %s sync failed (%d)\n",
37b766630bSBen Hutchings 		       part->name, part->dev_type_name, rc);
38874aeea5SJeff Kirsher }
39874aeea5SJeff Kirsher 
efx_mtd_remove_partition(struct efx_mtd_partition * part)40874aeea5SJeff Kirsher static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
41874aeea5SJeff Kirsher {
42874aeea5SJeff Kirsher 	int rc;
43874aeea5SJeff Kirsher 
44874aeea5SJeff Kirsher 	for (;;) {
45874aeea5SJeff Kirsher 		rc = mtd_device_unregister(&part->mtd);
46874aeea5SJeff Kirsher 		if (rc != -EBUSY)
47874aeea5SJeff Kirsher 			break;
48874aeea5SJeff Kirsher 		ssleep(1);
49874aeea5SJeff Kirsher 	}
50874aeea5SJeff Kirsher 	WARN_ON(rc);
51b766630bSBen Hutchings 	list_del(&part->node);
52874aeea5SJeff Kirsher }
53874aeea5SJeff Kirsher 
efx_mtd_add(struct efx_nic * efx,struct efx_mtd_partition * parts,size_t n_parts,size_t sizeof_part)5445a3fd55SBen Hutchings int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
55141d748eSBen Hutchings 		size_t n_parts, size_t sizeof_part)
56874aeea5SJeff Kirsher {
57874aeea5SJeff Kirsher 	struct efx_mtd_partition *part;
58b766630bSBen Hutchings 	size_t i;
59874aeea5SJeff Kirsher 
60b766630bSBen Hutchings 	for (i = 0; i < n_parts; i++) {
61141d748eSBen Hutchings 		part = (struct efx_mtd_partition *)((char *)parts +
62141d748eSBen Hutchings 						    i * sizeof_part);
63874aeea5SJeff Kirsher 
64874aeea5SJeff Kirsher 		part->mtd.writesize = 1;
65874aeea5SJeff Kirsher 
665fb1beecSBert Kenward 		if (!(part->mtd.flags & MTD_NO_ERASE))
675fb1beecSBert Kenward 			part->mtd.flags |= MTD_WRITEABLE;
685fb1beecSBert Kenward 
69874aeea5SJeff Kirsher 		part->mtd.owner = THIS_MODULE;
70b766630bSBen Hutchings 		part->mtd.priv = efx;
71874aeea5SJeff Kirsher 		part->mtd.name = part->name;
723c3c10bbSArtem Bityutskiy 		part->mtd._erase = efx_mtd_erase;
7345a3fd55SBen Hutchings 		part->mtd._read = efx->type->mtd_read;
7445a3fd55SBen Hutchings 		part->mtd._write = efx->type->mtd_write;
753c3c10bbSArtem Bityutskiy 		part->mtd._sync = efx_mtd_sync;
76874aeea5SJeff Kirsher 
7745a3fd55SBen Hutchings 		efx->type->mtd_rename(part);
78b766630bSBen Hutchings 
79874aeea5SJeff Kirsher 		if (mtd_device_register(&part->mtd, NULL, 0))
80874aeea5SJeff Kirsher 			goto fail;
81b766630bSBen Hutchings 
82b766630bSBen Hutchings 		/* Add to list in order - efx_mtd_remove() depends on this */
83b766630bSBen Hutchings 		list_add_tail(&part->node, &efx->mtd_list);
84874aeea5SJeff Kirsher 	}
85874aeea5SJeff Kirsher 
86874aeea5SJeff Kirsher 	return 0;
87874aeea5SJeff Kirsher 
88874aeea5SJeff Kirsher fail:
89141d748eSBen Hutchings 	while (i--) {
90141d748eSBen Hutchings 		part = (struct efx_mtd_partition *)((char *)parts +
91141d748eSBen Hutchings 						    i * sizeof_part);
92141d748eSBen Hutchings 		efx_mtd_remove_partition(part);
93141d748eSBen Hutchings 	}
947c43161cSBen Hutchings 	/* Failure is unlikely here, but probably means we're out of memory */
95874aeea5SJeff Kirsher 	return -ENOMEM;
96874aeea5SJeff Kirsher }
97874aeea5SJeff Kirsher 
efx_mtd_remove(struct efx_nic * efx)98874aeea5SJeff Kirsher void efx_mtd_remove(struct efx_nic *efx)
99874aeea5SJeff Kirsher {
100b766630bSBen Hutchings 	struct efx_mtd_partition *parts, *part, *next;
101874aeea5SJeff Kirsher 
102874aeea5SJeff Kirsher 	WARN_ON(efx_dev_registered(efx));
103874aeea5SJeff Kirsher 
104b766630bSBen Hutchings 	if (list_empty(&efx->mtd_list))
105b766630bSBen Hutchings 		return;
106b766630bSBen Hutchings 
107b766630bSBen Hutchings 	parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
108b766630bSBen Hutchings 				 node);
109b766630bSBen Hutchings 
110b766630bSBen Hutchings 	list_for_each_entry_safe(part, next, &efx->mtd_list, node)
111b766630bSBen Hutchings 		efx_mtd_remove_partition(part);
112b766630bSBen Hutchings 
113b766630bSBen Hutchings 	kfree(parts);
114874aeea5SJeff Kirsher }
115874aeea5SJeff Kirsher 
efx_mtd_rename(struct efx_nic * efx)116874aeea5SJeff Kirsher void efx_mtd_rename(struct efx_nic *efx)
117874aeea5SJeff Kirsher {
118b766630bSBen Hutchings 	struct efx_mtd_partition *part;
119874aeea5SJeff Kirsher 
120874aeea5SJeff Kirsher 	ASSERT_RTNL();
121874aeea5SJeff Kirsher 
122b766630bSBen Hutchings 	list_for_each_entry(part, &efx->mtd_list, node)
12345a3fd55SBen Hutchings 		efx->type->mtd_rename(part);
124874aeea5SJeff Kirsher }
125