1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2013 Solarflare Communications Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
11 #include <linux/module.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/slab.h>
14 #include <linux/rtnetlink.h>
16 #include "net_driver.h"
19 #define to_efx_mtd_partition(mtd) \
20 container_of(mtd, struct efx_mtd_partition, mtd)
24 static int efx_mtd_erase(struct mtd_info
*mtd
, struct erase_info
*erase
)
26 struct efx_nic
*efx
= mtd
->priv
;
29 rc
= efx
->type
->mtd_erase(mtd
, erase
->addr
, erase
->len
);
31 erase
->state
= MTD_ERASE_DONE
;
33 erase
->state
= MTD_ERASE_FAILED
;
34 erase
->fail_addr
= MTD_FAIL_ADDR_UNKNOWN
;
36 mtd_erase_callback(erase
);
40 static void efx_mtd_sync(struct mtd_info
*mtd
)
42 struct efx_mtd_partition
*part
= to_efx_mtd_partition(mtd
);
43 struct efx_nic
*efx
= mtd
->priv
;
46 rc
= efx
->type
->mtd_sync(mtd
);
48 pr_err("%s: %s sync failed (%d)\n",
49 part
->name
, part
->dev_type_name
, rc
);
52 static void efx_mtd_remove_partition(struct efx_mtd_partition
*part
)
57 rc
= mtd_device_unregister(&part
->mtd
);
63 list_del(&part
->node
);
66 int efx_mtd_add(struct efx_nic
*efx
, struct efx_mtd_partition
*parts
,
67 size_t n_parts
, size_t sizeof_part
)
69 struct efx_mtd_partition
*part
;
72 for (i
= 0; i
< n_parts
; i
++) {
73 part
= (struct efx_mtd_partition
*)((char *)parts
+
76 part
->mtd
.writesize
= 1;
78 part
->mtd
.owner
= THIS_MODULE
;
80 part
->mtd
.name
= part
->name
;
81 part
->mtd
._erase
= efx_mtd_erase
;
82 part
->mtd
._read
= efx
->type
->mtd_read
;
83 part
->mtd
._write
= efx
->type
->mtd_write
;
84 part
->mtd
._sync
= efx_mtd_sync
;
86 efx
->type
->mtd_rename(part
);
88 if (mtd_device_register(&part
->mtd
, NULL
, 0))
91 /* Add to list in order - efx_mtd_remove() depends on this */
92 list_add_tail(&part
->node
, &efx
->mtd_list
);
99 part
= (struct efx_mtd_partition
*)((char *)parts
+
101 efx_mtd_remove_partition(part
);
103 /* Failure is unlikely here, but probably means we're out of memory */
107 void efx_mtd_remove(struct efx_nic
*efx
)
109 struct efx_mtd_partition
*parts
, *part
, *next
;
111 WARN_ON(efx_dev_registered(efx
));
113 if (list_empty(&efx
->mtd_list
))
116 parts
= list_first_entry(&efx
->mtd_list
, struct efx_mtd_partition
,
119 list_for_each_entry_safe(part
, next
, &efx
->mtd_list
, node
)
120 efx_mtd_remove_partition(part
);
125 void efx_mtd_rename(struct efx_nic
*efx
)
127 struct efx_mtd_partition
*part
;
131 list_for_each_entry(part
, &efx
->mtd_list
, node
)
132 efx
->type
->mtd_rename(part
);