1d48523cbSMartin Habets // SPDX-License-Identifier: GPL-2.0-only
2d48523cbSMartin Habets /****************************************************************************
3d48523cbSMartin Habets * Driver for Solarflare network controllers and boards
4d48523cbSMartin Habets * Copyright 2005-2006 Fen Systems Ltd.
5d48523cbSMartin Habets * Copyright 2006-2013 Solarflare Communications Inc.
6d48523cbSMartin Habets */
7d48523cbSMartin Habets
8d48523cbSMartin Habets #include <linux/module.h>
9d48523cbSMartin Habets #include <linux/mtd/mtd.h>
10d48523cbSMartin Habets #include <linux/slab.h>
11d48523cbSMartin Habets #include <linux/rtnetlink.h>
12d48523cbSMartin Habets
13d48523cbSMartin Habets #include "net_driver.h"
14d48523cbSMartin Habets #include "efx.h"
15d48523cbSMartin Habets
16d48523cbSMartin Habets #define to_efx_mtd_partition(mtd) \
17d48523cbSMartin Habets container_of(mtd, struct efx_mtd_partition, mtd)
18d48523cbSMartin Habets
19d48523cbSMartin Habets /* MTD interface */
20d48523cbSMartin Habets
efx_mtd_erase(struct mtd_info * mtd,struct erase_info * erase)21d48523cbSMartin Habets static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
22d48523cbSMartin Habets {
23d48523cbSMartin Habets struct efx_nic *efx = mtd->priv;
24d48523cbSMartin Habets
25d48523cbSMartin Habets return efx->type->mtd_erase(mtd, erase->addr, erase->len);
26d48523cbSMartin Habets }
27d48523cbSMartin Habets
efx_mtd_sync(struct mtd_info * mtd)28d48523cbSMartin Habets static void efx_mtd_sync(struct mtd_info *mtd)
29d48523cbSMartin Habets {
30d48523cbSMartin Habets struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
31d48523cbSMartin Habets struct efx_nic *efx = mtd->priv;
32d48523cbSMartin Habets int rc;
33d48523cbSMartin Habets
34d48523cbSMartin Habets rc = efx->type->mtd_sync(mtd);
35d48523cbSMartin Habets if (rc)
36d48523cbSMartin Habets pr_err("%s: %s sync failed (%d)\n",
37d48523cbSMartin Habets part->name, part->dev_type_name, rc);
38d48523cbSMartin Habets }
39d48523cbSMartin Habets
efx_siena_mtd_remove_partition(struct efx_mtd_partition * part)40*71ad88f6SMartin Habets static void efx_siena_mtd_remove_partition(struct efx_mtd_partition *part)
41d48523cbSMartin Habets {
42d48523cbSMartin Habets int rc;
43d48523cbSMartin Habets
44d48523cbSMartin Habets for (;;) {
45d48523cbSMartin Habets rc = mtd_device_unregister(&part->mtd);
46d48523cbSMartin Habets if (rc != -EBUSY)
47d48523cbSMartin Habets break;
48d48523cbSMartin Habets ssleep(1);
49d48523cbSMartin Habets }
50d48523cbSMartin Habets WARN_ON(rc);
51d48523cbSMartin Habets list_del(&part->node);
52d48523cbSMartin Habets }
53d48523cbSMartin Habets
efx_siena_mtd_add(struct efx_nic * efx,struct efx_mtd_partition * parts,size_t n_parts,size_t sizeof_part)54*71ad88f6SMartin Habets int efx_siena_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
55d48523cbSMartin Habets size_t n_parts, size_t sizeof_part)
56d48523cbSMartin Habets {
57d48523cbSMartin Habets struct efx_mtd_partition *part;
58d48523cbSMartin Habets size_t i;
59d48523cbSMartin Habets
60d48523cbSMartin Habets for (i = 0; i < n_parts; i++) {
61d48523cbSMartin Habets part = (struct efx_mtd_partition *)((char *)parts +
62d48523cbSMartin Habets i * sizeof_part);
63d48523cbSMartin Habets
64d48523cbSMartin Habets part->mtd.writesize = 1;
65d48523cbSMartin Habets
66d48523cbSMartin Habets if (!(part->mtd.flags & MTD_NO_ERASE))
67d48523cbSMartin Habets part->mtd.flags |= MTD_WRITEABLE;
68d48523cbSMartin Habets
69d48523cbSMartin Habets part->mtd.owner = THIS_MODULE;
70d48523cbSMartin Habets part->mtd.priv = efx;
71d48523cbSMartin Habets part->mtd.name = part->name;
72d48523cbSMartin Habets part->mtd._erase = efx_mtd_erase;
73d48523cbSMartin Habets part->mtd._read = efx->type->mtd_read;
74d48523cbSMartin Habets part->mtd._write = efx->type->mtd_write;
75d48523cbSMartin Habets part->mtd._sync = efx_mtd_sync;
76d48523cbSMartin Habets
77d48523cbSMartin Habets efx->type->mtd_rename(part);
78d48523cbSMartin Habets
79d48523cbSMartin Habets if (mtd_device_register(&part->mtd, NULL, 0))
80d48523cbSMartin Habets goto fail;
81d48523cbSMartin Habets
82*71ad88f6SMartin Habets /* Add to list in order - efx_siena_mtd_remove() depends on this */
83d48523cbSMartin Habets list_add_tail(&part->node, &efx->mtd_list);
84d48523cbSMartin Habets }
85d48523cbSMartin Habets
86d48523cbSMartin Habets return 0;
87d48523cbSMartin Habets
88d48523cbSMartin Habets fail:
89d48523cbSMartin Habets while (i--) {
90d48523cbSMartin Habets part = (struct efx_mtd_partition *)((char *)parts +
91d48523cbSMartin Habets i * sizeof_part);
92*71ad88f6SMartin Habets efx_siena_mtd_remove_partition(part);
93d48523cbSMartin Habets }
94d48523cbSMartin Habets /* Failure is unlikely here, but probably means we're out of memory */
95d48523cbSMartin Habets return -ENOMEM;
96d48523cbSMartin Habets }
97d48523cbSMartin Habets
efx_siena_mtd_remove(struct efx_nic * efx)98*71ad88f6SMartin Habets void efx_siena_mtd_remove(struct efx_nic *efx)
99d48523cbSMartin Habets {
100d48523cbSMartin Habets struct efx_mtd_partition *parts, *part, *next;
101d48523cbSMartin Habets
102d48523cbSMartin Habets WARN_ON(efx_dev_registered(efx));
103d48523cbSMartin Habets
104d48523cbSMartin Habets if (list_empty(&efx->mtd_list))
105d48523cbSMartin Habets return;
106d48523cbSMartin Habets
107d48523cbSMartin Habets parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
108d48523cbSMartin Habets node);
109d48523cbSMartin Habets
110d48523cbSMartin Habets list_for_each_entry_safe(part, next, &efx->mtd_list, node)
111*71ad88f6SMartin Habets efx_siena_mtd_remove_partition(part);
112d48523cbSMartin Habets
113d48523cbSMartin Habets kfree(parts);
114d48523cbSMartin Habets }
115d48523cbSMartin Habets
efx_siena_mtd_rename(struct efx_nic * efx)116*71ad88f6SMartin Habets void efx_siena_mtd_rename(struct efx_nic *efx)
117d48523cbSMartin Habets {
118d48523cbSMartin Habets struct efx_mtd_partition *part;
119d48523cbSMartin Habets
120d48523cbSMartin Habets ASSERT_RTNL();
121d48523cbSMartin Habets
122d48523cbSMartin Habets list_for_each_entry(part, &efx->mtd_list, node)
123d48523cbSMartin Habets efx->type->mtd_rename(part);
124d48523cbSMartin Habets }
125