blob: f3ac7f30b5e718e3178dbdcf2797b5a0d13703d7 [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 Hutchings906bb262009-11-29 15:16:19 +00004 * Copyright 2006-2009 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>
13#include <linux/mtd/mtd.h>
14#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Ben Hutchings76884832009-11-29 15:10:44 +000016#include <linux/rtnetlink.h>
Ben Hutchingsf4150722008-11-04 20:34:28 +000017
18#define EFX_DRIVER_NAME "sfc_mtd"
19#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
74static int efx_spi_slow_wait(struct efx_mtd *efx_mtd, bool uninterruptible)
75{
76 const struct efx_spi_device *spi = efx_mtd->spi;
Ben Hutchings76884832009-11-29 15:10:44 +000077 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchingsf4150722008-11-04 20:34:28 +000078 u8 status;
79 int rc, i;
80
81 /* Wait up to 4s for flash/EEPROM to finish a slow operation. */
82 for (i = 0; i < 40; i++) {
83 __set_current_state(uninterruptible ?
84 TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
85 schedule_timeout(HZ / 10);
Ben Hutchings76884832009-11-29 15:10:44 +000086 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
Ben Hutchingsf4150722008-11-04 20:34:28 +000087 &status, sizeof(status));
88 if (rc)
89 return rc;
90 if (!(status & SPI_STATUS_NRDY))
91 return 0;
92 if (signal_pending(current))
93 return -EINTR;
94 }
95 EFX_ERR(efx, "timed out waiting for %s\n", efx_mtd->name);
96 return -ETIMEDOUT;
97}
98
Ben Hutchings76884832009-11-29 15:10:44 +000099static int
100efx_spi_unlock(struct efx_nic *efx, const struct efx_spi_device *spi)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000101{
102 const u8 unlock_mask = (SPI_STATUS_BP2 | SPI_STATUS_BP1 |
103 SPI_STATUS_BP0);
104 u8 status;
105 int rc;
106
Ben Hutchings76884832009-11-29 15:10:44 +0000107 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
108 &status, sizeof(status));
Ben Hutchingsf4150722008-11-04 20:34:28 +0000109 if (rc)
110 return rc;
111
112 if (!(status & unlock_mask))
113 return 0; /* already unlocked */
114
Ben Hutchings76884832009-11-29 15:10:44 +0000115 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000116 if (rc)
117 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000118 rc = falcon_spi_cmd(efx, spi, SPI_SST_EWSR, -1, NULL, NULL, 0);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000119 if (rc)
120 return rc;
121
122 status &= ~unlock_mask;
Ben Hutchings76884832009-11-29 15:10:44 +0000123 rc = falcon_spi_cmd(efx, spi, SPI_WRSR, -1, &status,
124 NULL, sizeof(status));
Ben Hutchingsf4150722008-11-04 20:34:28 +0000125 if (rc)
126 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000127 rc = falcon_spi_wait_write(efx, spi);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000128 if (rc)
129 return rc;
130
131 return 0;
132}
133
134static int efx_spi_erase(struct efx_mtd *efx_mtd, loff_t start, size_t len)
135{
136 const struct efx_spi_device *spi = efx_mtd->spi;
Ben Hutchings76884832009-11-29 15:10:44 +0000137 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000138 unsigned pos, block_len;
139 u8 empty[EFX_SPI_VERIFY_BUF_LEN];
140 u8 buffer[EFX_SPI_VERIFY_BUF_LEN];
141 int rc;
142
143 if (len != spi->erase_size)
144 return -EINVAL;
145
146 if (spi->erase_command == 0)
147 return -EOPNOTSUPP;
148
Ben Hutchings76884832009-11-29 15:10:44 +0000149 rc = efx_spi_unlock(efx, spi);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000150 if (rc)
151 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000152 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000153 if (rc)
154 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000155 rc = falcon_spi_cmd(efx, spi, spi->erase_command, start, NULL,
156 NULL, 0);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000157 if (rc)
158 return rc;
159 rc = efx_spi_slow_wait(efx_mtd, false);
160
161 /* Verify the entire region has been wiped */
162 memset(empty, 0xff, sizeof(empty));
163 for (pos = 0; pos < len; pos += block_len) {
164 block_len = min(len - pos, sizeof(buffer));
Ben Hutchings76884832009-11-29 15:10:44 +0000165 rc = falcon_spi_read(efx, spi, start + pos, block_len,
166 NULL, buffer);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000167 if (rc)
168 return rc;
169 if (memcmp(empty, buffer, block_len))
170 return -EIO;
171
172 /* Avoid locking up the system */
173 cond_resched();
174 if (signal_pending(current))
175 return -EINTR;
176 }
177
178 return rc;
179}
180
181/* MTD interface */
182
Ben Hutchingsf4150722008-11-04 20:34:28 +0000183static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
184{
185 struct efx_mtd *efx_mtd = mtd->priv;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000186 int rc;
187
Ben Hutchings76884832009-11-29 15:10:44 +0000188 rc = efx_mtd->ops->erase(mtd, erase->addr, erase->len);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000189 if (rc == 0) {
190 erase->state = MTD_ERASE_DONE;
191 } else {
192 erase->state = MTD_ERASE_FAILED;
193 erase->fail_addr = 0xffffffff;
194 }
195 mtd_erase_callback(erase);
196 return rc;
197}
198
Ben Hutchings76884832009-11-29 15:10:44 +0000199static void efx_mtd_sync(struct mtd_info *mtd)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000200{
201 struct efx_mtd *efx_mtd = mtd->priv;
Ben Hutchings76884832009-11-29 15:10:44 +0000202 struct efx_nic *efx = efx_mtd->efx;
203 int rc;
204
205 rc = efx_mtd->ops->sync(mtd);
206 if (rc)
207 EFX_ERR(efx, "%s sync failed (%d)\n", efx_mtd->name, rc);
208}
209
210static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
211{
212 int rc;
213
214 for (;;) {
215 rc = del_mtd_device(&part->mtd);
216 if (rc != -EBUSY)
217 break;
218 ssleep(1);
219 }
220 WARN_ON(rc);
221}
222
223static void efx_mtd_remove_device(struct efx_mtd *efx_mtd)
224{
225 struct efx_mtd_partition *part;
226
227 efx_for_each_partition(part, efx_mtd)
228 efx_mtd_remove_partition(part);
229 list_del(&efx_mtd->node);
230 kfree(efx_mtd);
231}
232
233static void efx_mtd_rename_device(struct efx_mtd *efx_mtd)
234{
235 struct efx_mtd_partition *part;
236
237 efx_for_each_partition(part, efx_mtd)
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000238 if (efx_nic_rev(efx_mtd->efx) >= EFX_REV_SIENA_A0)
239 snprintf(part->name, sizeof(part->name),
240 "%s %s:%02x", efx_mtd->efx->name,
241 part->type_name, part->mcdi.fw_subtype);
242 else
243 snprintf(part->name, sizeof(part->name),
244 "%s %s", efx_mtd->efx->name,
245 part->type_name);
Ben Hutchings76884832009-11-29 15:10:44 +0000246}
247
248static int efx_mtd_probe_device(struct efx_nic *efx, struct efx_mtd *efx_mtd)
249{
250 struct efx_mtd_partition *part;
251
252 efx_mtd->efx = efx;
253
254 efx_mtd_rename_device(efx_mtd);
255
256 efx_for_each_partition(part, efx_mtd) {
257 part->mtd.writesize = 1;
258
259 part->mtd.owner = THIS_MODULE;
260 part->mtd.priv = efx_mtd;
261 part->mtd.name = part->name;
262 part->mtd.erase = efx_mtd_erase;
263 part->mtd.read = efx_mtd->ops->read;
264 part->mtd.write = efx_mtd->ops->write;
265 part->mtd.sync = efx_mtd_sync;
266
267 if (add_mtd_device(&part->mtd))
268 goto fail;
269 }
270
271 list_add(&efx_mtd->node, &efx->mtd_list);
272 return 0;
273
274fail:
275 while (part != &efx_mtd->part[0]) {
276 --part;
277 efx_mtd_remove_partition(part);
278 }
279 /* add_mtd_device() returns 1 if the MTD table is full */
280 return -ENOMEM;
281}
282
283void efx_mtd_remove(struct efx_nic *efx)
284{
285 struct efx_mtd *efx_mtd, *next;
286
287 WARN_ON(efx_dev_registered(efx));
288
289 list_for_each_entry_safe(efx_mtd, next, &efx->mtd_list, node)
290 efx_mtd_remove_device(efx_mtd);
291}
292
293void efx_mtd_rename(struct efx_nic *efx)
294{
295 struct efx_mtd *efx_mtd;
296
297 ASSERT_RTNL();
298
299 list_for_each_entry(efx_mtd, &efx->mtd_list, node)
300 efx_mtd_rename_device(efx_mtd);
301}
302
303int efx_mtd_probe(struct efx_nic *efx)
304{
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000305 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
306 return siena_mtd_probe(efx);
307 else
308 return falcon_mtd_probe(efx);
Ben Hutchings76884832009-11-29 15:10:44 +0000309}
310
311/* Implementation of MTD operations for Falcon */
312
313static int falcon_mtd_read(struct mtd_info *mtd, loff_t start,
314 size_t len, size_t *retlen, u8 *buffer)
315{
316 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
317 struct efx_mtd *efx_mtd = mtd->priv;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000318 const struct efx_spi_device *spi = efx_mtd->spi;
Ben Hutchings76884832009-11-29 15:10:44 +0000319 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000320 int rc;
321
322 rc = mutex_lock_interruptible(&efx->spi_lock);
323 if (rc)
324 return rc;
Ben Hutchings76884832009-11-29 15:10:44 +0000325 rc = falcon_spi_read(efx, spi, part->offset + start, len,
326 retlen, buffer);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000327 mutex_unlock(&efx->spi_lock);
328 return rc;
329}
330
Ben Hutchings76884832009-11-29 15:10:44 +0000331static int falcon_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
332{
333 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
334 struct efx_mtd *efx_mtd = mtd->priv;
335 struct efx_nic *efx = efx_mtd->efx;
336 int rc;
337
338 rc = mutex_lock_interruptible(&efx->spi_lock);
339 if (rc)
340 return rc;
341 rc = efx_spi_erase(efx_mtd, part->offset + start, len);
342 mutex_unlock(&efx->spi_lock);
343 return rc;
344}
345
346static int falcon_mtd_write(struct mtd_info *mtd, loff_t start,
347 size_t len, size_t *retlen, const u8 *buffer)
348{
349 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
350 struct efx_mtd *efx_mtd = mtd->priv;
351 const struct efx_spi_device *spi = efx_mtd->spi;
352 struct efx_nic *efx = efx_mtd->efx;
353 int rc;
354
355 rc = mutex_lock_interruptible(&efx->spi_lock);
356 if (rc)
357 return rc;
358 rc = falcon_spi_write(efx, spi, part->offset + start, len,
359 retlen, buffer);
360 mutex_unlock(&efx->spi_lock);
361 return rc;
362}
363
364static int falcon_mtd_sync(struct mtd_info *mtd)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000365{
366 struct efx_mtd *efx_mtd = mtd->priv;
Ben Hutchings76884832009-11-29 15:10:44 +0000367 struct efx_nic *efx = efx_mtd->efx;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000368 int rc;
369
370 mutex_lock(&efx->spi_lock);
371 rc = efx_spi_slow_wait(efx_mtd, true);
372 mutex_unlock(&efx->spi_lock);
Ben Hutchings76884832009-11-29 15:10:44 +0000373 return rc;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000374}
375
Ben Hutchings76884832009-11-29 15:10:44 +0000376static struct efx_mtd_ops falcon_mtd_ops = {
377 .read = falcon_mtd_read,
378 .erase = falcon_mtd_erase,
379 .write = falcon_mtd_write,
380 .sync = falcon_mtd_sync,
381};
Ben Hutchingsf4150722008-11-04 20:34:28 +0000382
Ben Hutchings76884832009-11-29 15:10:44 +0000383static int falcon_mtd_probe(struct efx_nic *efx)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000384{
385 struct efx_spi_device *spi = efx->spi_flash;
386 struct efx_mtd *efx_mtd;
Ben Hutchings76884832009-11-29 15:10:44 +0000387 int rc;
388
389 ASSERT_RTNL();
Ben Hutchingsf4150722008-11-04 20:34:28 +0000390
391 if (!spi || spi->size <= FALCON_FLASH_BOOTCODE_START)
392 return -ENODEV;
393
Ben Hutchings76884832009-11-29 15:10:44 +0000394 efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]),
395 GFP_KERNEL);
Ben Hutchingsf4150722008-11-04 20:34:28 +0000396 if (!efx_mtd)
397 return -ENOMEM;
398
399 efx_mtd->spi = spi;
Ben Hutchings76884832009-11-29 15:10:44 +0000400 efx_mtd->name = "flash";
401 efx_mtd->ops = &falcon_mtd_ops;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000402
Ben Hutchings76884832009-11-29 15:10:44 +0000403 efx_mtd->n_parts = 1;
404 efx_mtd->part[0].mtd.type = MTD_NORFLASH;
405 efx_mtd->part[0].mtd.flags = MTD_CAP_NORFLASH;
406 efx_mtd->part[0].mtd.size = spi->size - FALCON_FLASH_BOOTCODE_START;
407 efx_mtd->part[0].mtd.erasesize = spi->erase_size;
408 efx_mtd->part[0].offset = FALCON_FLASH_BOOTCODE_START;
409 efx_mtd->part[0].type_name = "sfc_flash_bootrom";
Ben Hutchingsf4150722008-11-04 20:34:28 +0000410
Ben Hutchings76884832009-11-29 15:10:44 +0000411 rc = efx_mtd_probe_device(efx, efx_mtd);
412 if (rc)
Ben Hutchingsf4150722008-11-04 20:34:28 +0000413 kfree(efx_mtd);
Ben Hutchings76884832009-11-29 15:10:44 +0000414 return rc;
Ben Hutchingsf4150722008-11-04 20:34:28 +0000415}
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000416
417/* Implementation of MTD operations for Siena */
418
419static int siena_mtd_read(struct mtd_info *mtd, loff_t start,
420 size_t len, size_t *retlen, u8 *buffer)
421{
422 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
423 struct efx_mtd *efx_mtd = mtd->priv;
424 struct efx_nic *efx = efx_mtd->efx;
425 loff_t offset = start;
426 loff_t end = min_t(loff_t, start + len, mtd->size);
427 size_t chunk;
428 int rc = 0;
429
430 while (offset < end) {
Ben Hutchings5a27e862010-01-25 15:49:59 -0800431 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000432 rc = efx_mcdi_nvram_read(efx, part->mcdi.nvram_type, offset,
433 buffer, chunk);
434 if (rc)
435 goto out;
436 offset += chunk;
437 buffer += chunk;
438 }
439out:
440 *retlen = offset - start;
441 return rc;
442}
443
444static int siena_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
445{
446 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
447 struct efx_mtd *efx_mtd = mtd->priv;
448 struct efx_nic *efx = efx_mtd->efx;
449 loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
450 loff_t end = min_t(loff_t, start + len, mtd->size);
451 size_t chunk = part->mtd.erasesize;
452 int rc = 0;
453
454 if (!part->mcdi.updating) {
455 rc = efx_mcdi_nvram_update_start(efx, part->mcdi.nvram_type);
456 if (rc)
457 goto out;
458 part->mcdi.updating = 1;
459 }
460
461 /* The MCDI interface can in fact do multiple erase blocks at once;
462 * but erasing may be slow, so we make multiple calls here to avoid
463 * tripping the MCDI RPC timeout. */
464 while (offset < end) {
465 rc = efx_mcdi_nvram_erase(efx, part->mcdi.nvram_type, offset,
466 chunk);
467 if (rc)
468 goto out;
469 offset += chunk;
470 }
471out:
472 return rc;
473}
474
475static int siena_mtd_write(struct mtd_info *mtd, loff_t start,
476 size_t len, size_t *retlen, const u8 *buffer)
477{
478 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
479 struct efx_mtd *efx_mtd = mtd->priv;
480 struct efx_nic *efx = efx_mtd->efx;
481 loff_t offset = start;
482 loff_t end = min_t(loff_t, start + len, mtd->size);
483 size_t chunk;
484 int rc = 0;
485
486 if (!part->mcdi.updating) {
487 rc = efx_mcdi_nvram_update_start(efx, part->mcdi.nvram_type);
488 if (rc)
489 goto out;
490 part->mcdi.updating = 1;
491 }
492
493 while (offset < end) {
Ben Hutchings5a27e862010-01-25 15:49:59 -0800494 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
Ben Hutchings8880f4e2009-11-29 15:15:41 +0000495 rc = efx_mcdi_nvram_write(efx, part->mcdi.nvram_type, offset,
496 buffer, chunk);
497 if (rc)
498 goto out;
499 offset += chunk;
500 buffer += chunk;
501 }
502out:
503 *retlen = offset - start;
504 return rc;
505}
506
507static int siena_mtd_sync(struct mtd_info *mtd)
508{
509 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
510 struct efx_mtd *efx_mtd = mtd->priv;
511 struct efx_nic *efx = efx_mtd->efx;
512 int rc = 0;
513
514 if (part->mcdi.updating) {
515 part->mcdi.updating = 0;
516 rc = efx_mcdi_nvram_update_finish(efx, part->mcdi.nvram_type);
517 }
518
519 return rc;
520}
521
522static struct efx_mtd_ops siena_mtd_ops = {
523 .read = siena_mtd_read,
524 .erase = siena_mtd_erase,
525 .write = siena_mtd_write,
526 .sync = siena_mtd_sync,
527};
528
529struct siena_nvram_type_info {
530 int port;
531 const char *name;
532};
533
534static struct siena_nvram_type_info siena_nvram_types[] = {
535 [MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO] = { 0, "sfc_dummy_phy" },
536 [MC_CMD_NVRAM_TYPE_MC_FW] = { 0, "sfc_mcfw" },
537 [MC_CMD_NVRAM_TYPE_MC_FW_BACKUP] = { 0, "sfc_mcfw_backup" },
538 [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0] = { 0, "sfc_static_cfg" },
539 [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1] = { 1, "sfc_static_cfg" },
540 [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0] = { 0, "sfc_dynamic_cfg" },
541 [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1] = { 1, "sfc_dynamic_cfg" },
542 [MC_CMD_NVRAM_TYPE_EXP_ROM] = { 0, "sfc_exp_rom" },
543 [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT0] = { 0, "sfc_exp_rom_cfg" },
544 [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT1] = { 1, "sfc_exp_rom_cfg" },
545 [MC_CMD_NVRAM_TYPE_PHY_PORT0] = { 0, "sfc_phy_fw" },
546 [MC_CMD_NVRAM_TYPE_PHY_PORT1] = { 1, "sfc_phy_fw" },
547};
548
549static int siena_mtd_probe_partition(struct efx_nic *efx,
550 struct efx_mtd *efx_mtd,
551 unsigned int part_id,
552 unsigned int type)
553{
554 struct efx_mtd_partition *part = &efx_mtd->part[part_id];
555 struct siena_nvram_type_info *info;
556 size_t size, erase_size;
557 bool protected;
558 int rc;
559
560 if (type >= ARRAY_SIZE(siena_nvram_types))
561 return -ENODEV;
562
563 info = &siena_nvram_types[type];
564
565 if (info->port != efx_port_num(efx))
566 return -ENODEV;
567
568 rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
569 if (rc)
570 return rc;
571 if (protected)
572 return -ENODEV; /* hide it */
573
574 part->mcdi.nvram_type = type;
575 part->type_name = info->name;
576
577 part->mtd.type = MTD_NORFLASH;
578 part->mtd.flags = MTD_CAP_NORFLASH;
579 part->mtd.size = size;
580 part->mtd.erasesize = erase_size;
581
582 return 0;
583}
584
585static int siena_mtd_get_fw_subtypes(struct efx_nic *efx,
586 struct efx_mtd *efx_mtd)
587{
588 struct efx_mtd_partition *part;
589 uint16_t fw_subtype_list[MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_LEN /
590 sizeof(uint16_t)];
591 int rc;
592
593 rc = efx_mcdi_get_board_cfg(efx, NULL, fw_subtype_list);
594 if (rc)
595 return rc;
596
597 efx_for_each_partition(part, efx_mtd)
598 part->mcdi.fw_subtype = fw_subtype_list[part->mcdi.nvram_type];
599
600 return 0;
601}
602
603static int siena_mtd_probe(struct efx_nic *efx)
604{
605 struct efx_mtd *efx_mtd;
606 int rc = -ENODEV;
607 u32 nvram_types;
608 unsigned int type;
609
610 ASSERT_RTNL();
611
612 rc = efx_mcdi_nvram_types(efx, &nvram_types);
613 if (rc)
614 return rc;
615
616 efx_mtd = kzalloc(sizeof(*efx_mtd) +
617 hweight32(nvram_types) * sizeof(efx_mtd->part[0]),
618 GFP_KERNEL);
619 if (!efx_mtd)
620 return -ENOMEM;
621
622 efx_mtd->name = "Siena NVRAM manager";
623
624 efx_mtd->ops = &siena_mtd_ops;
625
626 type = 0;
627 efx_mtd->n_parts = 0;
628
629 while (nvram_types != 0) {
630 if (nvram_types & 1) {
631 rc = siena_mtd_probe_partition(efx, efx_mtd,
632 efx_mtd->n_parts, type);
633 if (rc == 0)
634 efx_mtd->n_parts++;
635 else if (rc != -ENODEV)
636 goto fail;
637 }
638 type++;
639 nvram_types >>= 1;
640 }
641
642 rc = siena_mtd_get_fw_subtypes(efx, efx_mtd);
643 if (rc)
644 goto fail;
645
646 rc = efx_mtd_probe_device(efx, efx_mtd);
647fail:
648 if (rc)
649 kfree(efx_mtd);
650 return rc;
651}
652