blob: a77a8bd2dd70ae3bda3f23333d3cf26b14792573 [file] [log] [blame]
Ben Hutchingsf4150722008-11-04 20:34:28 +00001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
Ben Hutchingsf4150722008-11-04 20:34:28 +00003 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01004 * Copyright 2006-2013 Solarflare Communications Inc.
Ben Hutchingsf4150722008-11-04 20:34:28 +00005 *
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.
9 */
10
11#include <linux/module.h>
12#include <linux/mtd/mtd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Ben Hutchings76884832009-11-29 15:10:44 +000014#include <linux/rtnetlink.h>
Ben Hutchingsf4150722008-11-04 20:34:28 +000015
Ben Hutchingsf4150722008-11-04 20:34:28 +000016#include "net_driver.h"
Hannes Ederff2ef902009-02-18 17:49:50 -080017#include "efx.h"
Ben Hutchings76884832009-11-29 15:10:44 +000018
Ben Hutchings76884832009-11-29 15:10:44 +000019#define to_efx_mtd_partition(mtd) \
20 container_of(mtd, struct efx_mtd_partition, mtd)
21
Ben Hutchingsf4150722008-11-04 20:34:28 +000022/* MTD interface */
23
Ben Hutchingsf4150722008-11-04 20:34:28 +000024static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
25{
Ben Hutchingsb7666302012-11-28 04:38:10 +000026 struct efx_nic *efx = mtd->priv;
Ben Hutchingsf4150722008-11-04 20:34:28 +000027 int rc;
28
Ben Hutchings45a3fd52012-11-28 04:38:14 +000029 rc = efx->type->mtd_erase(mtd, erase->addr, erase->len);
Ben Hutchingsf4150722008-11-04 20:34:28 +000030 if (rc == 0) {
31 erase->state = MTD_ERASE_DONE;
32 } else {
33 erase->state = MTD_ERASE_FAILED;
Shmulik Ladkani88dfda52012-02-12 11:13:08 +020034 erase->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Ben Hutchingsf4150722008-11-04 20:34:28 +000035 }
36 mtd_erase_callback(erase);
37 return rc;
38}
39
Ben Hutchings76884832009-11-29 15:10:44 +000040static void efx_mtd_sync(struct mtd_info *mtd)
Ben Hutchingsf4150722008-11-04 20:34:28 +000041{
Ben Hutchings0c605a22010-06-23 11:29:24 +000042 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
Ben Hutchingsb7666302012-11-28 04:38:10 +000043 struct efx_nic *efx = mtd->priv;
Ben Hutchings76884832009-11-29 15:10:44 +000044 int rc;
45
Ben Hutchings45a3fd52012-11-28 04:38:14 +000046 rc = efx->type->mtd_sync(mtd);
Ben Hutchings76884832009-11-29 15:10:44 +000047 if (rc)
Ben Hutchings0c605a22010-06-23 11:29:24 +000048 pr_err("%s: %s sync failed (%d)\n",
Ben Hutchingsb7666302012-11-28 04:38:10 +000049 part->name, part->dev_type_name, rc);
Ben Hutchings76884832009-11-29 15:10:44 +000050}
51
52static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
53{
54 int rc;
55
56 for (;;) {
Jamie Ilesee0e87b2011-05-23 10:23:40 +010057 rc = mtd_device_unregister(&part->mtd);
Ben Hutchings76884832009-11-29 15:10:44 +000058 if (rc != -EBUSY)
59 break;
60 ssleep(1);
61 }
62 WARN_ON(rc);
Ben Hutchingsb7666302012-11-28 04:38:10 +000063 list_del(&part->node);
Ben Hutchings76884832009-11-29 15:10:44 +000064}
65
Ben Hutchings45a3fd52012-11-28 04:38:14 +000066int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
67 size_t n_parts, size_t sizeof_part)
Ben Hutchings76884832009-11-29 15:10:44 +000068{
69 struct efx_mtd_partition *part;
Ben Hutchingsb7666302012-11-28 04:38:10 +000070 size_t i;
Ben Hutchings76884832009-11-29 15:10:44 +000071
Ben Hutchingsb7666302012-11-28 04:38:10 +000072 for (i = 0; i < n_parts; i++) {
Ben Hutchings141d7482012-11-28 04:38:14 +000073 part = (struct efx_mtd_partition *)((char *)parts +
74 i * sizeof_part);
Ben Hutchings76884832009-11-29 15:10:44 +000075
Ben Hutchings76884832009-11-29 15:10:44 +000076 part->mtd.writesize = 1;
77
78 part->mtd.owner = THIS_MODULE;
Ben Hutchingsb7666302012-11-28 04:38:10 +000079 part->mtd.priv = efx;
Ben Hutchings76884832009-11-29 15:10:44 +000080 part->mtd.name = part->name;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +020081 part->mtd._erase = efx_mtd_erase;
Ben Hutchings45a3fd52012-11-28 04:38:14 +000082 part->mtd._read = efx->type->mtd_read;
83 part->mtd._write = efx->type->mtd_write;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +020084 part->mtd._sync = efx_mtd_sync;
Ben Hutchings76884832009-11-29 15:10:44 +000085
Ben Hutchings45a3fd52012-11-28 04:38:14 +000086 efx->type->mtd_rename(part);
Ben Hutchingsb7666302012-11-28 04:38:10 +000087
Jamie Ilesee0e87b2011-05-23 10:23:40 +010088 if (mtd_device_register(&part->mtd, NULL, 0))
Ben Hutchings76884832009-11-29 15:10:44 +000089 goto fail;
Ben Hutchingsb7666302012-11-28 04:38:10 +000090
91 /* Add to list in order - efx_mtd_remove() depends on this */
92 list_add_tail(&part->node, &efx->mtd_list);
Ben Hutchings76884832009-11-29 15:10:44 +000093 }
94
Ben Hutchings76884832009-11-29 15:10:44 +000095 return 0;
96
97fail:
Ben Hutchings141d7482012-11-28 04:38:14 +000098 while (i--) {
99 part = (struct efx_mtd_partition *)((char *)parts +
100 i * sizeof_part);
101 efx_mtd_remove_partition(part);
102 }
Ben Hutchings7c431612012-01-27 17:23:58 +0000103 /* Failure is unlikely here, but probably means we're out of memory */
Ben Hutchings76884832009-11-29 15:10:44 +0000104 return -ENOMEM;
105}
106
107void efx_mtd_remove(struct efx_nic *efx)
108{
Ben Hutchingsb7666302012-11-28 04:38:10 +0000109 struct efx_mtd_partition *parts, *part, *next;
Ben Hutchings76884832009-11-29 15:10:44 +0000110
111 WARN_ON(efx_dev_registered(efx));
112
Ben Hutchingsb7666302012-11-28 04:38:10 +0000113 if (list_empty(&efx->mtd_list))
114 return;
115
116 parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
117 node);
118
119 list_for_each_entry_safe(part, next, &efx->mtd_list, node)
120 efx_mtd_remove_partition(part);
121
122 kfree(parts);
Ben Hutchings76884832009-11-29 15:10:44 +0000123}
124
125void efx_mtd_rename(struct efx_nic *efx)
126{
Ben Hutchingsb7666302012-11-28 04:38:10 +0000127 struct efx_mtd_partition *part;
Ben Hutchings76884832009-11-29 15:10:44 +0000128
129 ASSERT_RTNL();
130
Ben Hutchingsb7666302012-11-28 04:38:10 +0000131 list_for_each_entry(part, &efx->mtd_list, node)
Ben Hutchings45a3fd52012-11-28 04:38:14 +0000132 efx->type->mtd_rename(part);
Ben Hutchings76884832009-11-29 15:10:44 +0000133}