blob: b702862a09244f8efd2bf4923eaa44389f234ce3 [file] [log] [blame]
Ben Hutchingsf4150722008-11-04 20:34:28 +00001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2006-2010 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
Ben Hutchings8880f4e2009-11-29 15:15:41 +000011#include <linux/bitops.h>
Ben Hutchingsf4150722008-11-04 20:34:28 +000012#include <linux/module.h>
Ben Hutchings5f3f9d62011-11-04 22:29:14 +000013#undef DEBUG /* <linux/mtd/mtd.h> has its own use for DEBUG */
Ben Hutchingsf4150722008-11-04 20:34:28 +000014#include <linux/mtd/mtd.h>
15#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Ben Hutchings76884832009-11-29 15:10:44 +000017#include <linux/rtnetlink.h>
Ben Hutchingsf4150722008-11-04 20:34:28 +000018
Ben Hutchingsf4150722008-11-04 20:34:28 +000019#include "net_driver.h"
20#include "spi.h"
Hannes Ederff2ef902009-02-18 17:49:50 -080021#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000022#include "nic.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000023#include "mcdi.h"
24#include "mcdi_pcol.h"
Ben Hutchingsf4150722008-11-04 20:34:28 +000025
26#define EFX_SPI_VERIFY_BUF_LEN 16
27
Ben Hutchings76884832009-11-29 15:10:44 +000028struct efx_mtd_partition {
Ben Hutchingsf4150722008-11-04 20:34:28 +000029 struct mtd_info mtd;
Ben Hutchings8880f4e2009-11-29 15:15:41 +000030 union {
31 struct {
32 bool updating;
33 u8 nvram_type;
34 u16 fw_subtype;
35 } mcdi;
36 size_t offset;
37 };
Ben Hutchings76884832009-11-29 15:10:44 +000038 const char *type_name;
Ben Hutchingsf4150722008-11-04 20:34:28 +000039 char name[IFNAMSIZ + 20];
40};
41
Ben Hutchings76884832009-11-29 15:10:44 +000042struct efx_mtd_ops {
43 int (*read)(struct mtd_info *mtd, loff_t start, size_t len,
44 size_t *retlen, u8 *buffer);
45 int (*erase)(struct mtd_info *mtd, loff_t start, size_t len);
46 int (*write)(struct mtd_info *mtd, loff_t start, size_t len,
47 size_t *retlen, const u8 *buffer);
48 int (*sync)(struct mtd_info *mtd);
49};
50
51struct efx_mtd {
52 struct list_head node;
53 struct efx_nic *efx;
54 const struct efx_spi_device *spi;
55 const char *name;
56 const struct efx_mtd_ops *ops;
57 size_t n_parts;
58 struct efx_mtd_partition part[0];
59};
60
61#define efx_for_each_partition(part, efx_mtd) \
62 for ((part) = &(efx_mtd)->part[0]; \
63 (part) != &(efx_mtd)->part[(efx_mtd)->n_parts]; \
64 (part)++)
65
66#define to_efx_mtd_partition(mtd) \
67 container_of(mtd, struct efx_mtd_partition, mtd)
68
69static int falcon_mtd_probe(struct efx_nic *efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +000070static int siena_mtd_probe(struct efx_nic *efx);
Ben Hutchings76884832009-11-29 15:10:44 +000071
Ben Hutchingsf4150722008-11-04 20:34:28 +000072/* SPI utilities */
73
Ben Hutchings0c605a22010-06-23 11:29:24 +000074static int
75efx_spi_slow_wait(struct efx_mtd_partition *part, bool uninterruptible)
Ben Hutchingsf4150722008-11-04 20:34:28 +000076{
Ben Hutchings0c605a22010-06-23 11:29:24 +000077 struct efx_mtd *efx_mtd = part->mtd.priv;
Ben Hutchingsf4150722008-11-04 20:34:28 +000078 const struct efx_spi_device *spi = efx_mtd->spi;
Ben Hutchings76884832009-11-29 15:10:44 +000079 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchingsf4150722008-11-04 20:34:28 +000080 u8 status;
81 int rc, i;
82
83 /* Wait up to 4s for flash/EEPROM to finish a slow operation. */
84 for (i = 0; i < 40; i++) {
85 __set_current_state(uninterruptible ?
86 TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
87 schedule_timeout(HZ / 10);
Ben Hutchings76884832009-11-29 15:10:44 +000088 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
Ben Hutchingsf4150722008-11-04 20:34:28 +000089 &status, sizeof(status));
90 if (rc)
91 return rc;
92 if (!(status & SPI_STATUS_NRDY))
93 return 0;
94 if (signal_pending(current))
95 return -EINTR;
96 }
Ben Hutchings0c605a22010-06-23 11:29:24 +000097 pr_err("%s: timed out waiting for %s\n", part->name, efx_mtd->name);
Ben Hutchingsf4150722008-11-04 20:34:28 +000098 return -ETIMEDOUT;
99}
100
Ben Hutchings76884832009-11-29 15:10:44 +0000101static int
102efx_spi_unlock(struct efx_nic *efx, const struct efx_spi_device *spi)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000103{
104 const u8 unlock_mask = (SPI_STATUS_BP2 | SPI_STATUS_BP1 |
105 SPI_STATUS_BP0);
106 u8 status;
107 int rc;
108
Ben Hutchings76884832009-11-29 15:10:44 +0000109 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
110 &status, sizeof(status));
Ben Hutchingsf4150722008-11-04 20:34:28 +0000111 if (rc)
112 return rc;
113
114 if (!(status & unlock_mask))
115 return 0; /* already unlocked */
116
Ben Hutchings76884832009-11-29 15:10:44 +0000117 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000118 if (rc)
119 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000120 rc = falcon_spi_cmd(efx, spi, SPI_SST_EWSR, -1, NULL, NULL, 0);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000121 if (rc)
122 return rc;
123
124 status &= ~unlock_mask;
Ben Hutchings76884832009-11-29 15:10:44 +0000125 rc = falcon_spi_cmd(efx, spi, SPI_WRSR, -1, &status,
126 NULL, sizeof(status));
Ben Hutchingsf4150722008-11-04 20:34:28 +0000127 if (rc)
128 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000129 rc = falcon_spi_wait_write(efx, spi);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000130 if (rc)
131 return rc;
132
133 return 0;
134}
135
Ben Hutchings0c605a22010-06-23 11:29:24 +0000136static int
137efx_spi_erase(struct efx_mtd_partition *part, loff_t start, size_t len)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000138{
Ben Hutchings0c605a22010-06-23 11:29:24 +0000139 struct efx_mtd *efx_mtd = part->mtd.priv;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000140 const struct efx_spi_device *spi = efx_mtd->spi;
Ben Hutchings76884832009-11-29 15:10:44 +0000141 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000142 unsigned pos, block_len;
143 u8 empty[EFX_SPI_VERIFY_BUF_LEN];
144 u8 buffer[EFX_SPI_VERIFY_BUF_LEN];
145 int rc;
146
147 if (len != spi->erase_size)
148 return -EINVAL;
149
150 if (spi->erase_command == 0)
151 return -EOPNOTSUPP;
152
Ben Hutchings76884832009-11-29 15:10:44 +0000153 rc = efx_spi_unlock(efx, spi);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000154 if (rc)
155 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000156 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000157 if (rc)
158 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000159 rc = falcon_spi_cmd(efx, spi, spi->erase_command, start, NULL,
160 NULL, 0);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000161 if (rc)
162 return rc;
Ben Hutchings0c605a22010-06-23 11:29:24 +0000163 rc = efx_spi_slow_wait(part, false);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000164
165 /* Verify the entire region has been wiped */
166 memset(empty, 0xff, sizeof(empty));
167 for (pos = 0; pos < len; pos += block_len) {
168 block_len = min(len - pos, sizeof(buffer));
Ben Hutchings76884832009-11-29 15:10:44 +0000169 rc = falcon_spi_read(efx, spi, start + pos, block_len,
170 NULL, buffer);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000171 if (rc)
172 return rc;
173 if (memcmp(empty, buffer, block_len))
174 return -EIO;
175
176 /* Avoid locking up the system */
177 cond_resched();
178 if (signal_pending(current))
179 return -EINTR;
180 }
181
182 return rc;
183}
184
185/* MTD interface */
186
Ben Hutchingsf4150722008-11-04 20:34:28 +0000187static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
188{
189 struct efx_mtd *efx_mtd = mtd->priv;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000190 int rc;
191
Ben Hutchings76884832009-11-29 15:10:44 +0000192 rc = efx_mtd->ops->erase(mtd, erase->addr, erase->len);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000193 if (rc == 0) {
194 erase->state = MTD_ERASE_DONE;
195 } else {
196 erase->state = MTD_ERASE_FAILED;
197 erase->fail_addr = 0xffffffff;
198 }
199 mtd_erase_callback(erase);
200 return rc;
201}
202
Ben Hutchings76884832009-11-29 15:10:44 +0000203static void efx_mtd_sync(struct mtd_info *mtd)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000204{
Ben Hutchings0c605a22010-06-23 11:29:24 +0000205 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000206 struct efx_mtd *efx_mtd = mtd->priv;
Ben Hutchings76884832009-11-29 15:10:44 +0000207 int rc;
208
209 rc = efx_mtd->ops->sync(mtd);
210 if (rc)
Ben Hutchings0c605a22010-06-23 11:29:24 +0000211 pr_err("%s: %s sync failed (%d)\n",
212 part->name, efx_mtd->name, rc);
Ben Hutchings76884832009-11-29 15:10:44 +0000213}
214
215static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
216{
217 int rc;
218
219 for (;;) {
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100220 rc = mtd_device_unregister(&part->mtd);
Ben Hutchings76884832009-11-29 15:10:44 +0000221 if (rc != -EBUSY)
222 break;
223 ssleep(1);
224 }
225 WARN_ON(rc);
226}
227
228static void efx_mtd_remove_device(struct efx_mtd *efx_mtd)
229{
230 struct efx_mtd_partition *part;
231
232 efx_for_each_partition(part, efx_mtd)
233 efx_mtd_remove_partition(part);
234 list_del(&efx_mtd->node);
235 kfree(efx_mtd);
236}
237
238static void efx_mtd_rename_device(struct efx_mtd *efx_mtd)
239{
240 struct efx_mtd_partition *part;
241
242 efx_for_each_partition(part, efx_mtd)
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000243 if (efx_nic_rev(efx_mtd->efx) >= EFX_REV_SIENA_A0)
244 snprintf(part->name, sizeof(part->name),
245 "%s %s:%02x", efx_mtd->efx->name,
246 part->type_name, part->mcdi.fw_subtype);
247 else
248 snprintf(part->name, sizeof(part->name),
249 "%s %s", efx_mtd->efx->name,
250 part->type_name);
Ben Hutchings76884832009-11-29 15:10:44 +0000251}
252
253static int efx_mtd_probe_device(struct efx_nic *efx, struct efx_mtd *efx_mtd)
254{
255 struct efx_mtd_partition *part;
256
257 efx_mtd->efx = efx;
258
259 efx_mtd_rename_device(efx_mtd);
260
261 efx_for_each_partition(part, efx_mtd) {
262 part->mtd.writesize = 1;
263
264 part->mtd.owner = THIS_MODULE;
265 part->mtd.priv = efx_mtd;
266 part->mtd.name = part->name;
267 part->mtd.erase = efx_mtd_erase;
268 part->mtd.read = efx_mtd->ops->read;
269 part->mtd.write = efx_mtd->ops->write;
270 part->mtd.sync = efx_mtd_sync;
271
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100272 if (mtd_device_register(&part->mtd, NULL, 0))
Ben Hutchings76884832009-11-29 15:10:44 +0000273 goto fail;
274 }
275
276 list_add(&efx_mtd->node, &efx->mtd_list);
277 return 0;
278
279fail:
280 while (part != &efx_mtd->part[0]) {
281 --part;
282 efx_mtd_remove_partition(part);
283 }
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100284 /* mtd_device_register() returns 1 if the MTD table is full */
Ben Hutchings76884832009-11-29 15:10:44 +0000285 return -ENOMEM;
286}
287
288void efx_mtd_remove(struct efx_nic *efx)
289{
290 struct efx_mtd *efx_mtd, *next;
291
292 WARN_ON(efx_dev_registered(efx));
293
294 list_for_each_entry_safe(efx_mtd, next, &efx->mtd_list, node)
295 efx_mtd_remove_device(efx_mtd);
296}
297
298void efx_mtd_rename(struct efx_nic *efx)
299{
300 struct efx_mtd *efx_mtd;
301
302 ASSERT_RTNL();
303
304 list_for_each_entry(efx_mtd, &efx->mtd_list, node)
305 efx_mtd_rename_device(efx_mtd);
306}
307
308int efx_mtd_probe(struct efx_nic *efx)
309{
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000310 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
311 return siena_mtd_probe(efx);
312 else
313 return falcon_mtd_probe(efx);
Ben Hutchings76884832009-11-29 15:10:44 +0000314}
315
316/* Implementation of MTD operations for Falcon */
317
318static int falcon_mtd_read(struct mtd_info *mtd, loff_t start,
319 size_t len, size_t *retlen, u8 *buffer)
320{
321 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
322 struct efx_mtd *efx_mtd = mtd->priv;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000323 const struct efx_spi_device *spi = efx_mtd->spi;
Ben Hutchings76884832009-11-29 15:10:44 +0000324 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchings4de92182010-12-02 13:47:29 +0000325 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000326 int rc;
327
Ben Hutchings4de92182010-12-02 13:47:29 +0000328 rc = mutex_lock_interruptible(&nic_data->spi_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000329 if (rc)
330 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000331 rc = falcon_spi_read(efx, spi, part->offset + start, len,
332 retlen, buffer);
Ben Hutchings4de92182010-12-02 13:47:29 +0000333 mutex_unlock(&nic_data->spi_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000334 return rc;
335}
336
Ben Hutchings76884832009-11-29 15:10:44 +0000337static int falcon_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
338{
339 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
340 struct efx_mtd *efx_mtd = mtd->priv;
341 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchings4de92182010-12-02 13:47:29 +0000342 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchings76884832009-11-29 15:10:44 +0000343 int rc;
344
Ben Hutchings4de92182010-12-02 13:47:29 +0000345 rc = mutex_lock_interruptible(&nic_data->spi_lock);
Ben Hutchings76884832009-11-29 15:10:44 +0000346 if (rc)
347 return rc;
Ben Hutchings0c605a22010-06-23 11:29:24 +0000348 rc = efx_spi_erase(part, part->offset + start, len);
Ben Hutchings4de92182010-12-02 13:47:29 +0000349 mutex_unlock(&nic_data->spi_lock);
Ben Hutchings76884832009-11-29 15:10:44 +0000350 return rc;
351}
352
353static int falcon_mtd_write(struct mtd_info *mtd, loff_t start,
354 size_t len, size_t *retlen, const u8 *buffer)
355{
356 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
357 struct efx_mtd *efx_mtd = mtd->priv;
358 const struct efx_spi_device *spi = efx_mtd->spi;
359 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchings4de92182010-12-02 13:47:29 +0000360 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchings76884832009-11-29 15:10:44 +0000361 int rc;
362
Ben Hutchings4de92182010-12-02 13:47:29 +0000363 rc = mutex_lock_interruptible(&nic_data->spi_lock);
Ben Hutchings76884832009-11-29 15:10:44 +0000364 if (rc)
365 return rc;
366 rc = falcon_spi_write(efx, spi, part->offset + start, len,
367 retlen, buffer);
Ben Hutchings4de92182010-12-02 13:47:29 +0000368 mutex_unlock(&nic_data->spi_lock);
Ben Hutchings76884832009-11-29 15:10:44 +0000369 return rc;
370}
371
372static int falcon_mtd_sync(struct mtd_info *mtd)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000373{
Ben Hutchings0c605a22010-06-23 11:29:24 +0000374 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000375 struct efx_mtd *efx_mtd = mtd->priv;
Ben Hutchings76884832009-11-29 15:10:44 +0000376 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchings4de92182010-12-02 13:47:29 +0000377 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000378 int rc;
379
Ben Hutchings4de92182010-12-02 13:47:29 +0000380 mutex_lock(&nic_data->spi_lock);
Ben Hutchings0c605a22010-06-23 11:29:24 +0000381 rc = efx_spi_slow_wait(part, true);
Ben Hutchings4de92182010-12-02 13:47:29 +0000382 mutex_unlock(&nic_data->spi_lock);
Ben Hutchings76884832009-11-29 15:10:44 +0000383 return rc;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000384}
385
Ben Hutchings18e83e42012-01-05 19:05:20 +0000386static const struct efx_mtd_ops falcon_mtd_ops = {
Ben Hutchings76884832009-11-29 15:10:44 +0000387 .read = falcon_mtd_read,
388 .erase = falcon_mtd_erase,
389 .write = falcon_mtd_write,
390 .sync = falcon_mtd_sync,
391};
Ben Hutchingsf4150722008-11-04 20:34:28 +0000392
Ben Hutchings76884832009-11-29 15:10:44 +0000393static int falcon_mtd_probe(struct efx_nic *efx)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000394{
Ben Hutchings4de92182010-12-02 13:47:29 +0000395 struct falcon_nic_data *nic_data = efx->nic_data;
Ben Hutchings6a8872c2010-12-02 13:47:10 +0000396 struct efx_spi_device *spi;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000397 struct efx_mtd *efx_mtd;
Ben Hutchings6a8872c2010-12-02 13:47:10 +0000398 int rc = -ENODEV;
Ben Hutchings76884832009-11-29 15:10:44 +0000399
400 ASSERT_RTNL();
Ben Hutchingsf4150722008-11-04 20:34:28 +0000401
Ben Hutchings4de92182010-12-02 13:47:29 +0000402 spi = &nic_data->spi_flash;
403 if (efx_spi_present(spi) && spi->size > FALCON_FLASH_BOOTCODE_START) {
Ben Hutchings6a8872c2010-12-02 13:47:10 +0000404 efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]),
405 GFP_KERNEL);
406 if (!efx_mtd)
407 return -ENOMEM;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000408
Ben Hutchings6a8872c2010-12-02 13:47:10 +0000409 efx_mtd->spi = spi;
410 efx_mtd->name = "flash";
411 efx_mtd->ops = &falcon_mtd_ops;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000412
Ben Hutchings6a8872c2010-12-02 13:47:10 +0000413 efx_mtd->n_parts = 1;
414 efx_mtd->part[0].mtd.type = MTD_NORFLASH;
415 efx_mtd->part[0].mtd.flags = MTD_CAP_NORFLASH;
416 efx_mtd->part[0].mtd.size = spi->size - FALCON_FLASH_BOOTCODE_START;
417 efx_mtd->part[0].mtd.erasesize = spi->erase_size;
418 efx_mtd->part[0].offset = FALCON_FLASH_BOOTCODE_START;
419 efx_mtd->part[0].type_name = "sfc_flash_bootrom";
Ben Hutchingsf4150722008-11-04 20:34:28 +0000420
Ben Hutchings6a8872c2010-12-02 13:47:10 +0000421 rc = efx_mtd_probe_device(efx, efx_mtd);
422 if (rc) {
423 kfree(efx_mtd);
424 return rc;
425 }
426 }
Ben Hutchingsf4150722008-11-04 20:34:28 +0000427
Ben Hutchings4de92182010-12-02 13:47:29 +0000428 spi = &nic_data->spi_eeprom;
429 if (efx_spi_present(spi) && spi->size > EFX_EEPROM_BOOTCONFIG_START) {
Ben Hutchings6a8872c2010-12-02 13:47:10 +0000430 efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]),
431 GFP_KERNEL);
432 if (!efx_mtd)
433 return -ENOMEM;
434
435 efx_mtd->spi = spi;
436 efx_mtd->name = "EEPROM";
437 efx_mtd->ops = &falcon_mtd_ops;
438
439 efx_mtd->n_parts = 1;
440 efx_mtd->part[0].mtd.type = MTD_RAM;
441 efx_mtd->part[0].mtd.flags = MTD_CAP_RAM;
442 efx_mtd->part[0].mtd.size =
443 min(spi->size, EFX_EEPROM_BOOTCONFIG_END) -
444 EFX_EEPROM_BOOTCONFIG_START;
445 efx_mtd->part[0].mtd.erasesize = spi->erase_size;
446 efx_mtd->part[0].offset = EFX_EEPROM_BOOTCONFIG_START;
447 efx_mtd->part[0].type_name = "sfc_bootconfig";
448
449 rc = efx_mtd_probe_device(efx, efx_mtd);
450 if (rc) {
451 kfree(efx_mtd);
452 return rc;
453 }
454 }
455
Ben Hutchings76884832009-11-29 15:10:44 +0000456 return rc;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000457}
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000458
459/* Implementation of MTD operations for Siena */
460
461static int siena_mtd_read(struct mtd_info *mtd, loff_t start,
462 size_t len, size_t *retlen, u8 *buffer)
463{
464 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
465 struct efx_mtd *efx_mtd = mtd->priv;
466 struct efx_nic *efx = efx_mtd->efx;
467 loff_t offset = start;
468 loff_t end = min_t(loff_t, start + len, mtd->size);
469 size_t chunk;
470 int rc = 0;
471
472 while (offset < end) {
Ben Hutchings5a27e862010-01-25 15:49:59 -0800473 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000474 rc = efx_mcdi_nvram_read(efx, part->mcdi.nvram_type, offset,
475 buffer, chunk);
476 if (rc)
477 goto out;
478 offset += chunk;
479 buffer += chunk;
480 }
481out:
482 *retlen = offset - start;
483 return rc;
484}
485
486static int siena_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
487{
488 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
489 struct efx_mtd *efx_mtd = mtd->priv;
490 struct efx_nic *efx = efx_mtd->efx;
491 loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
492 loff_t end = min_t(loff_t, start + len, mtd->size);
493 size_t chunk = part->mtd.erasesize;
494 int rc = 0;
495
496 if (!part->mcdi.updating) {
497 rc = efx_mcdi_nvram_update_start(efx, part->mcdi.nvram_type);
498 if (rc)
499 goto out;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000500 part->mcdi.updating = true;
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000501 }
502
503 /* The MCDI interface can in fact do multiple erase blocks at once;
504 * but erasing may be slow, so we make multiple calls here to avoid
505 * tripping the MCDI RPC timeout. */
506 while (offset < end) {
507 rc = efx_mcdi_nvram_erase(efx, part->mcdi.nvram_type, offset,
508 chunk);
509 if (rc)
510 goto out;
511 offset += chunk;
512 }
513out:
514 return rc;
515}
516
517static int siena_mtd_write(struct mtd_info *mtd, loff_t start,
518 size_t len, size_t *retlen, const u8 *buffer)
519{
520 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
521 struct efx_mtd *efx_mtd = mtd->priv;
522 struct efx_nic *efx = efx_mtd->efx;
523 loff_t offset = start;
524 loff_t end = min_t(loff_t, start + len, mtd->size);
525 size_t chunk;
526 int rc = 0;
527
528 if (!part->mcdi.updating) {
529 rc = efx_mcdi_nvram_update_start(efx, part->mcdi.nvram_type);
530 if (rc)
531 goto out;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000532 part->mcdi.updating = true;
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000533 }
534
535 while (offset < end) {
Ben Hutchings5a27e862010-01-25 15:49:59 -0800536 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000537 rc = efx_mcdi_nvram_write(efx, part->mcdi.nvram_type, offset,
538 buffer, chunk);
539 if (rc)
540 goto out;
541 offset += chunk;
542 buffer += chunk;
543 }
544out:
545 *retlen = offset - start;
546 return rc;
547}
548
549static int siena_mtd_sync(struct mtd_info *mtd)
550{
551 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
552 struct efx_mtd *efx_mtd = mtd->priv;
553 struct efx_nic *efx = efx_mtd->efx;
554 int rc = 0;
555
556 if (part->mcdi.updating) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000557 part->mcdi.updating = false;
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000558 rc = efx_mcdi_nvram_update_finish(efx, part->mcdi.nvram_type);
559 }
560
561 return rc;
562}
563
Ben Hutchings18e83e42012-01-05 19:05:20 +0000564static const struct efx_mtd_ops siena_mtd_ops = {
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000565 .read = siena_mtd_read,
566 .erase = siena_mtd_erase,
567 .write = siena_mtd_write,
568 .sync = siena_mtd_sync,
569};
570
571struct siena_nvram_type_info {
572 int port;
573 const char *name;
574};
575
Ben Hutchings18e83e42012-01-05 19:05:20 +0000576static const struct siena_nvram_type_info siena_nvram_types[] = {
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000577 [MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO] = { 0, "sfc_dummy_phy" },
578 [MC_CMD_NVRAM_TYPE_MC_FW] = { 0, "sfc_mcfw" },
579 [MC_CMD_NVRAM_TYPE_MC_FW_BACKUP] = { 0, "sfc_mcfw_backup" },
580 [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0] = { 0, "sfc_static_cfg" },
581 [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1] = { 1, "sfc_static_cfg" },
582 [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0] = { 0, "sfc_dynamic_cfg" },
583 [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1] = { 1, "sfc_dynamic_cfg" },
584 [MC_CMD_NVRAM_TYPE_EXP_ROM] = { 0, "sfc_exp_rom" },
585 [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT0] = { 0, "sfc_exp_rom_cfg" },
586 [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT1] = { 1, "sfc_exp_rom_cfg" },
587 [MC_CMD_NVRAM_TYPE_PHY_PORT0] = { 0, "sfc_phy_fw" },
588 [MC_CMD_NVRAM_TYPE_PHY_PORT1] = { 1, "sfc_phy_fw" },
589};
590
591static int siena_mtd_probe_partition(struct efx_nic *efx,
592 struct efx_mtd *efx_mtd,
593 unsigned int part_id,
594 unsigned int type)
595{
596 struct efx_mtd_partition *part = &efx_mtd->part[part_id];
Ben Hutchings18e83e42012-01-05 19:05:20 +0000597 const struct siena_nvram_type_info *info;
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000598 size_t size, erase_size;
599 bool protected;
600 int rc;
601
602 if (type >= ARRAY_SIZE(siena_nvram_types))
603 return -ENODEV;
604
605 info = &siena_nvram_types[type];
606
607 if (info->port != efx_port_num(efx))
608 return -ENODEV;
609
610 rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
611 if (rc)
612 return rc;
613 if (protected)
614 return -ENODEV; /* hide it */
615
616 part->mcdi.nvram_type = type;
617 part->type_name = info->name;
618
619 part->mtd.type = MTD_NORFLASH;
620 part->mtd.flags = MTD_CAP_NORFLASH;
621 part->mtd.size = size;
622 part->mtd.erasesize = erase_size;
623
624 return 0;
625}
626
627static int siena_mtd_get_fw_subtypes(struct efx_nic *efx,
628 struct efx_mtd *efx_mtd)
629{
630 struct efx_mtd_partition *part;
Ben Hutchings05a93202011-12-20 00:44:06 +0000631 uint16_t fw_subtype_list[MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MINNUM];
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000632 int rc;
633
634 rc = efx_mcdi_get_board_cfg(efx, NULL, fw_subtype_list);
635 if (rc)
636 return rc;
637
638 efx_for_each_partition(part, efx_mtd)
639 part->mcdi.fw_subtype = fw_subtype_list[part->mcdi.nvram_type];
640
641 return 0;
642}
643
644static int siena_mtd_probe(struct efx_nic *efx)
645{
646 struct efx_mtd *efx_mtd;
647 int rc = -ENODEV;
648 u32 nvram_types;
649 unsigned int type;
650
651 ASSERT_RTNL();
652
653 rc = efx_mcdi_nvram_types(efx, &nvram_types);
654 if (rc)
655 return rc;
656
657 efx_mtd = kzalloc(sizeof(*efx_mtd) +
658 hweight32(nvram_types) * sizeof(efx_mtd->part[0]),
659 GFP_KERNEL);
660 if (!efx_mtd)
661 return -ENOMEM;
662
663 efx_mtd->name = "Siena NVRAM manager";
664
665 efx_mtd->ops = &siena_mtd_ops;
666
667 type = 0;
668 efx_mtd->n_parts = 0;
669
670 while (nvram_types != 0) {
671 if (nvram_types & 1) {
672 rc = siena_mtd_probe_partition(efx, efx_mtd,
673 efx_mtd->n_parts, type);
674 if (rc == 0)
675 efx_mtd->n_parts++;
676 else if (rc != -ENODEV)
677 goto fail;
678 }
679 type++;
680 nvram_types >>= 1;
681 }
682
683 rc = siena_mtd_get_fw_subtypes(efx, efx_mtd);
684 if (rc)
685 goto fail;
686
687 rc = efx_mtd_probe_device(efx, efx_mtd);
688fail:
689 if (rc)
690 kfree(efx_mtd);
691 return rc;
692}
693