mtd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2010 Solarflare Communications Inc.
  5. *
  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. #include <linux/bitops.h>
  11. #include <linux/module.h>
  12. #undef DEBUG /* <linux/mtd/mtd.h> has its own use for DEBUG */
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <linux/rtnetlink.h>
  17. #include "net_driver.h"
  18. #include "spi.h"
  19. #include "efx.h"
  20. #include "nic.h"
  21. #include "mcdi.h"
  22. #include "mcdi_pcol.h"
  23. #define EFX_SPI_VERIFY_BUF_LEN 16
  24. struct efx_mtd_partition {
  25. struct mtd_info mtd;
  26. union {
  27. struct {
  28. bool updating;
  29. u8 nvram_type;
  30. u16 fw_subtype;
  31. } mcdi;
  32. size_t offset;
  33. };
  34. const char *type_name;
  35. char name[IFNAMSIZ + 20];
  36. };
  37. struct efx_mtd_ops {
  38. int (*read)(struct mtd_info *mtd, loff_t start, size_t len,
  39. size_t *retlen, u8 *buffer);
  40. int (*erase)(struct mtd_info *mtd, loff_t start, size_t len);
  41. int (*write)(struct mtd_info *mtd, loff_t start, size_t len,
  42. size_t *retlen, const u8 *buffer);
  43. int (*sync)(struct mtd_info *mtd);
  44. };
  45. struct efx_mtd {
  46. struct list_head node;
  47. struct efx_nic *efx;
  48. const struct efx_spi_device *spi;
  49. const char *name;
  50. const struct efx_mtd_ops *ops;
  51. size_t n_parts;
  52. struct efx_mtd_partition part[0];
  53. };
  54. #define efx_for_each_partition(part, efx_mtd) \
  55. for ((part) = &(efx_mtd)->part[0]; \
  56. (part) != &(efx_mtd)->part[(efx_mtd)->n_parts]; \
  57. (part)++)
  58. #define to_efx_mtd_partition(mtd) \
  59. container_of(mtd, struct efx_mtd_partition, mtd)
  60. static int falcon_mtd_probe(struct efx_nic *efx);
  61. static int siena_mtd_probe(struct efx_nic *efx);
  62. /* SPI utilities */
  63. static int
  64. efx_spi_slow_wait(struct efx_mtd_partition *part, bool uninterruptible)
  65. {
  66. struct efx_mtd *efx_mtd = part->mtd.priv;
  67. const struct efx_spi_device *spi = efx_mtd->spi;
  68. struct efx_nic *efx = efx_mtd->efx;
  69. u8 status;
  70. int rc, i;
  71. /* Wait up to 4s for flash/EEPROM to finish a slow operation. */
  72. for (i = 0; i < 40; i++) {
  73. __set_current_state(uninterruptible ?
  74. TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
  75. schedule_timeout(HZ / 10);
  76. rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
  77. &status, sizeof(status));
  78. if (rc)
  79. return rc;
  80. if (!(status & SPI_STATUS_NRDY))
  81. return 0;
  82. if (signal_pending(current))
  83. return -EINTR;
  84. }
  85. pr_err("%s: timed out waiting for %s\n", part->name, efx_mtd->name);
  86. return -ETIMEDOUT;
  87. }
  88. static int
  89. efx_spi_unlock(struct efx_nic *efx, const struct efx_spi_device *spi)
  90. {
  91. const u8 unlock_mask = (SPI_STATUS_BP2 | SPI_STATUS_BP1 |
  92. SPI_STATUS_BP0);
  93. u8 status;
  94. int rc;
  95. rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
  96. &status, sizeof(status));
  97. if (rc)
  98. return rc;
  99. if (!(status & unlock_mask))
  100. return 0; /* already unlocked */
  101. rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
  102. if (rc)
  103. return rc;
  104. rc = falcon_spi_cmd(efx, spi, SPI_SST_EWSR, -1, NULL, NULL, 0);
  105. if (rc)
  106. return rc;
  107. status &= ~unlock_mask;
  108. rc = falcon_spi_cmd(efx, spi, SPI_WRSR, -1, &status,
  109. NULL, sizeof(status));
  110. if (rc)
  111. return rc;
  112. rc = falcon_spi_wait_write(efx, spi);
  113. if (rc)
  114. return rc;
  115. return 0;
  116. }
  117. static int
  118. efx_spi_erase(struct efx_mtd_partition *part, loff_t start, size_t len)
  119. {
  120. struct efx_mtd *efx_mtd = part->mtd.priv;
  121. const struct efx_spi_device *spi = efx_mtd->spi;
  122. struct efx_nic *efx = efx_mtd->efx;
  123. unsigned pos, block_len;
  124. u8 empty[EFX_SPI_VERIFY_BUF_LEN];
  125. u8 buffer[EFX_SPI_VERIFY_BUF_LEN];
  126. int rc;
  127. if (len != spi->erase_size)
  128. return -EINVAL;
  129. if (spi->erase_command == 0)
  130. return -EOPNOTSUPP;
  131. rc = efx_spi_unlock(efx, spi);
  132. if (rc)
  133. return rc;
  134. rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
  135. if (rc)
  136. return rc;
  137. rc = falcon_spi_cmd(efx, spi, spi->erase_command, start, NULL,
  138. NULL, 0);
  139. if (rc)
  140. return rc;
  141. rc = efx_spi_slow_wait(part, false);
  142. /* Verify the entire region has been wiped */
  143. memset(empty, 0xff, sizeof(empty));
  144. for (pos = 0; pos < len; pos += block_len) {
  145. block_len = min(len - pos, sizeof(buffer));
  146. rc = falcon_spi_read(efx, spi, start + pos, block_len,
  147. NULL, buffer);
  148. if (rc)
  149. return rc;
  150. if (memcmp(empty, buffer, block_len))
  151. return -EIO;
  152. /* Avoid locking up the system */
  153. cond_resched();
  154. if (signal_pending(current))
  155. return -EINTR;
  156. }
  157. return rc;
  158. }
  159. /* MTD interface */
  160. static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
  161. {
  162. struct efx_mtd *efx_mtd = mtd->priv;
  163. int rc;
  164. rc = efx_mtd->ops->erase(mtd, erase->addr, erase->len);
  165. if (rc == 0) {
  166. erase->state = MTD_ERASE_DONE;
  167. } else {
  168. erase->state = MTD_ERASE_FAILED;
  169. erase->fail_addr = 0xffffffff;
  170. }
  171. mtd_erase_callback(erase);
  172. return rc;
  173. }
  174. static void efx_mtd_sync(struct mtd_info *mtd)
  175. {
  176. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  177. struct efx_mtd *efx_mtd = mtd->priv;
  178. int rc;
  179. rc = efx_mtd->ops->sync(mtd);
  180. if (rc)
  181. pr_err("%s: %s sync failed (%d)\n",
  182. part->name, efx_mtd->name, rc);
  183. }
  184. static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
  185. {
  186. int rc;
  187. for (;;) {
  188. rc = mtd_device_unregister(&part->mtd);
  189. if (rc != -EBUSY)
  190. break;
  191. ssleep(1);
  192. }
  193. WARN_ON(rc);
  194. }
  195. static void efx_mtd_remove_device(struct efx_mtd *efx_mtd)
  196. {
  197. struct efx_mtd_partition *part;
  198. efx_for_each_partition(part, efx_mtd)
  199. efx_mtd_remove_partition(part);
  200. list_del(&efx_mtd->node);
  201. kfree(efx_mtd);
  202. }
  203. static void efx_mtd_rename_device(struct efx_mtd *efx_mtd)
  204. {
  205. struct efx_mtd_partition *part;
  206. efx_for_each_partition(part, efx_mtd)
  207. if (efx_nic_rev(efx_mtd->efx) >= EFX_REV_SIENA_A0)
  208. snprintf(part->name, sizeof(part->name),
  209. "%s %s:%02x", efx_mtd->efx->name,
  210. part->type_name, part->mcdi.fw_subtype);
  211. else
  212. snprintf(part->name, sizeof(part->name),
  213. "%s %s", efx_mtd->efx->name,
  214. part->type_name);
  215. }
  216. static int efx_mtd_probe_device(struct efx_nic *efx, struct efx_mtd *efx_mtd)
  217. {
  218. struct efx_mtd_partition *part;
  219. efx_mtd->efx = efx;
  220. efx_mtd_rename_device(efx_mtd);
  221. efx_for_each_partition(part, efx_mtd) {
  222. part->mtd.writesize = 1;
  223. part->mtd.owner = THIS_MODULE;
  224. part->mtd.priv = efx_mtd;
  225. part->mtd.name = part->name;
  226. part->mtd.erase = efx_mtd_erase;
  227. part->mtd.read = efx_mtd->ops->read;
  228. part->mtd.write = efx_mtd->ops->write;
  229. part->mtd.sync = efx_mtd_sync;
  230. if (mtd_device_register(&part->mtd, NULL, 0))
  231. goto fail;
  232. }
  233. list_add(&efx_mtd->node, &efx->mtd_list);
  234. return 0;
  235. fail:
  236. while (part != &efx_mtd->part[0]) {
  237. --part;
  238. efx_mtd_remove_partition(part);
  239. }
  240. /* mtd_device_register() returns 1 if the MTD table is full */
  241. return -ENOMEM;
  242. }
  243. void efx_mtd_remove(struct efx_nic *efx)
  244. {
  245. struct efx_mtd *efx_mtd, *next;
  246. WARN_ON(efx_dev_registered(efx));
  247. list_for_each_entry_safe(efx_mtd, next, &efx->mtd_list, node)
  248. efx_mtd_remove_device(efx_mtd);
  249. }
  250. void efx_mtd_rename(struct efx_nic *efx)
  251. {
  252. struct efx_mtd *efx_mtd;
  253. ASSERT_RTNL();
  254. list_for_each_entry(efx_mtd, &efx->mtd_list, node)
  255. efx_mtd_rename_device(efx_mtd);
  256. }
  257. int efx_mtd_probe(struct efx_nic *efx)
  258. {
  259. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
  260. return siena_mtd_probe(efx);
  261. else
  262. return falcon_mtd_probe(efx);
  263. }
  264. /* Implementation of MTD operations for Falcon */
  265. static int falcon_mtd_read(struct mtd_info *mtd, loff_t start,
  266. size_t len, size_t *retlen, u8 *buffer)
  267. {
  268. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  269. struct efx_mtd *efx_mtd = mtd->priv;
  270. const struct efx_spi_device *spi = efx_mtd->spi;
  271. struct efx_nic *efx = efx_mtd->efx;
  272. struct falcon_nic_data *nic_data = efx->nic_data;
  273. int rc;
  274. rc = mutex_lock_interruptible(&nic_data->spi_lock);
  275. if (rc)
  276. return rc;
  277. rc = falcon_spi_read(efx, spi, part->offset + start, len,
  278. retlen, buffer);
  279. mutex_unlock(&nic_data->spi_lock);
  280. return rc;
  281. }
  282. static int falcon_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
  283. {
  284. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  285. struct efx_mtd *efx_mtd = mtd->priv;
  286. struct efx_nic *efx = efx_mtd->efx;
  287. struct falcon_nic_data *nic_data = efx->nic_data;
  288. int rc;
  289. rc = mutex_lock_interruptible(&nic_data->spi_lock);
  290. if (rc)
  291. return rc;
  292. rc = efx_spi_erase(part, part->offset + start, len);
  293. mutex_unlock(&nic_data->spi_lock);
  294. return rc;
  295. }
  296. static int falcon_mtd_write(struct mtd_info *mtd, loff_t start,
  297. size_t len, size_t *retlen, const u8 *buffer)
  298. {
  299. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  300. struct efx_mtd *efx_mtd = mtd->priv;
  301. const struct efx_spi_device *spi = efx_mtd->spi;
  302. struct efx_nic *efx = efx_mtd->efx;
  303. struct falcon_nic_data *nic_data = efx->nic_data;
  304. int rc;
  305. rc = mutex_lock_interruptible(&nic_data->spi_lock);
  306. if (rc)
  307. return rc;
  308. rc = falcon_spi_write(efx, spi, part->offset + start, len,
  309. retlen, buffer);
  310. mutex_unlock(&nic_data->spi_lock);
  311. return rc;
  312. }
  313. static int falcon_mtd_sync(struct mtd_info *mtd)
  314. {
  315. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  316. struct efx_mtd *efx_mtd = mtd->priv;
  317. struct efx_nic *efx = efx_mtd->efx;
  318. struct falcon_nic_data *nic_data = efx->nic_data;
  319. int rc;
  320. mutex_lock(&nic_data->spi_lock);
  321. rc = efx_spi_slow_wait(part, true);
  322. mutex_unlock(&nic_data->spi_lock);
  323. return rc;
  324. }
  325. static const struct efx_mtd_ops falcon_mtd_ops = {
  326. .read = falcon_mtd_read,
  327. .erase = falcon_mtd_erase,
  328. .write = falcon_mtd_write,
  329. .sync = falcon_mtd_sync,
  330. };
  331. static int falcon_mtd_probe(struct efx_nic *efx)
  332. {
  333. struct falcon_nic_data *nic_data = efx->nic_data;
  334. struct efx_spi_device *spi;
  335. struct efx_mtd *efx_mtd;
  336. int rc = -ENODEV;
  337. ASSERT_RTNL();
  338. spi = &nic_data->spi_flash;
  339. if (efx_spi_present(spi) && spi->size > FALCON_FLASH_BOOTCODE_START) {
  340. efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]),
  341. GFP_KERNEL);
  342. if (!efx_mtd)
  343. return -ENOMEM;
  344. efx_mtd->spi = spi;
  345. efx_mtd->name = "flash";
  346. efx_mtd->ops = &falcon_mtd_ops;
  347. efx_mtd->n_parts = 1;
  348. efx_mtd->part[0].mtd.type = MTD_NORFLASH;
  349. efx_mtd->part[0].mtd.flags = MTD_CAP_NORFLASH;
  350. efx_mtd->part[0].mtd.size = spi->size - FALCON_FLASH_BOOTCODE_START;
  351. efx_mtd->part[0].mtd.erasesize = spi->erase_size;
  352. efx_mtd->part[0].offset = FALCON_FLASH_BOOTCODE_START;
  353. efx_mtd->part[0].type_name = "sfc_flash_bootrom";
  354. rc = efx_mtd_probe_device(efx, efx_mtd);
  355. if (rc) {
  356. kfree(efx_mtd);
  357. return rc;
  358. }
  359. }
  360. spi = &nic_data->spi_eeprom;
  361. if (efx_spi_present(spi) && spi->size > EFX_EEPROM_BOOTCONFIG_START) {
  362. efx_mtd = kzalloc(sizeof(*efx_mtd) + sizeof(efx_mtd->part[0]),
  363. GFP_KERNEL);
  364. if (!efx_mtd)
  365. return -ENOMEM;
  366. efx_mtd->spi = spi;
  367. efx_mtd->name = "EEPROM";
  368. efx_mtd->ops = &falcon_mtd_ops;
  369. efx_mtd->n_parts = 1;
  370. efx_mtd->part[0].mtd.type = MTD_RAM;
  371. efx_mtd->part[0].mtd.flags = MTD_CAP_RAM;
  372. efx_mtd->part[0].mtd.size =
  373. min(spi->size, EFX_EEPROM_BOOTCONFIG_END) -
  374. EFX_EEPROM_BOOTCONFIG_START;
  375. efx_mtd->part[0].mtd.erasesize = spi->erase_size;
  376. efx_mtd->part[0].offset = EFX_EEPROM_BOOTCONFIG_START;
  377. efx_mtd->part[0].type_name = "sfc_bootconfig";
  378. rc = efx_mtd_probe_device(efx, efx_mtd);
  379. if (rc) {
  380. kfree(efx_mtd);
  381. return rc;
  382. }
  383. }
  384. return rc;
  385. }
  386. /* Implementation of MTD operations for Siena */
  387. static int siena_mtd_read(struct mtd_info *mtd, loff_t start,
  388. size_t len, size_t *retlen, u8 *buffer)
  389. {
  390. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  391. struct efx_mtd *efx_mtd = mtd->priv;
  392. struct efx_nic *efx = efx_mtd->efx;
  393. loff_t offset = start;
  394. loff_t end = min_t(loff_t, start + len, mtd->size);
  395. size_t chunk;
  396. int rc = 0;
  397. while (offset < end) {
  398. chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
  399. rc = efx_mcdi_nvram_read(efx, part->mcdi.nvram_type, offset,
  400. buffer, chunk);
  401. if (rc)
  402. goto out;
  403. offset += chunk;
  404. buffer += chunk;
  405. }
  406. out:
  407. *retlen = offset - start;
  408. return rc;
  409. }
  410. static int siena_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
  411. {
  412. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  413. struct efx_mtd *efx_mtd = mtd->priv;
  414. struct efx_nic *efx = efx_mtd->efx;
  415. loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
  416. loff_t end = min_t(loff_t, start + len, mtd->size);
  417. size_t chunk = part->mtd.erasesize;
  418. int rc = 0;
  419. if (!part->mcdi.updating) {
  420. rc = efx_mcdi_nvram_update_start(efx, part->mcdi.nvram_type);
  421. if (rc)
  422. goto out;
  423. part->mcdi.updating = true;
  424. }
  425. /* The MCDI interface can in fact do multiple erase blocks at once;
  426. * but erasing may be slow, so we make multiple calls here to avoid
  427. * tripping the MCDI RPC timeout. */
  428. while (offset < end) {
  429. rc = efx_mcdi_nvram_erase(efx, part->mcdi.nvram_type, offset,
  430. chunk);
  431. if (rc)
  432. goto out;
  433. offset += chunk;
  434. }
  435. out:
  436. return rc;
  437. }
  438. static int siena_mtd_write(struct mtd_info *mtd, loff_t start,
  439. size_t len, size_t *retlen, const u8 *buffer)
  440. {
  441. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  442. struct efx_mtd *efx_mtd = mtd->priv;
  443. struct efx_nic *efx = efx_mtd->efx;
  444. loff_t offset = start;
  445. loff_t end = min_t(loff_t, start + len, mtd->size);
  446. size_t chunk;
  447. int rc = 0;
  448. if (!part->mcdi.updating) {
  449. rc = efx_mcdi_nvram_update_start(efx, part->mcdi.nvram_type);
  450. if (rc)
  451. goto out;
  452. part->mcdi.updating = true;
  453. }
  454. while (offset < end) {
  455. chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
  456. rc = efx_mcdi_nvram_write(efx, part->mcdi.nvram_type, offset,
  457. buffer, chunk);
  458. if (rc)
  459. goto out;
  460. offset += chunk;
  461. buffer += chunk;
  462. }
  463. out:
  464. *retlen = offset - start;
  465. return rc;
  466. }
  467. static int siena_mtd_sync(struct mtd_info *mtd)
  468. {
  469. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  470. struct efx_mtd *efx_mtd = mtd->priv;
  471. struct efx_nic *efx = efx_mtd->efx;
  472. int rc = 0;
  473. if (part->mcdi.updating) {
  474. part->mcdi.updating = false;
  475. rc = efx_mcdi_nvram_update_finish(efx, part->mcdi.nvram_type);
  476. }
  477. return rc;
  478. }
  479. static const struct efx_mtd_ops siena_mtd_ops = {
  480. .read = siena_mtd_read,
  481. .erase = siena_mtd_erase,
  482. .write = siena_mtd_write,
  483. .sync = siena_mtd_sync,
  484. };
  485. struct siena_nvram_type_info {
  486. int port;
  487. const char *name;
  488. };
  489. static const struct siena_nvram_type_info siena_nvram_types[] = {
  490. [MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO] = { 0, "sfc_dummy_phy" },
  491. [MC_CMD_NVRAM_TYPE_MC_FW] = { 0, "sfc_mcfw" },
  492. [MC_CMD_NVRAM_TYPE_MC_FW_BACKUP] = { 0, "sfc_mcfw_backup" },
  493. [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0] = { 0, "sfc_static_cfg" },
  494. [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1] = { 1, "sfc_static_cfg" },
  495. [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0] = { 0, "sfc_dynamic_cfg" },
  496. [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1] = { 1, "sfc_dynamic_cfg" },
  497. [MC_CMD_NVRAM_TYPE_EXP_ROM] = { 0, "sfc_exp_rom" },
  498. [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT0] = { 0, "sfc_exp_rom_cfg" },
  499. [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT1] = { 1, "sfc_exp_rom_cfg" },
  500. [MC_CMD_NVRAM_TYPE_PHY_PORT0] = { 0, "sfc_phy_fw" },
  501. [MC_CMD_NVRAM_TYPE_PHY_PORT1] = { 1, "sfc_phy_fw" },
  502. };
  503. static int siena_mtd_probe_partition(struct efx_nic *efx,
  504. struct efx_mtd *efx_mtd,
  505. unsigned int part_id,
  506. unsigned int type)
  507. {
  508. struct efx_mtd_partition *part = &efx_mtd->part[part_id];
  509. const struct siena_nvram_type_info *info;
  510. size_t size, erase_size;
  511. bool protected;
  512. int rc;
  513. if (type >= ARRAY_SIZE(siena_nvram_types))
  514. return -ENODEV;
  515. info = &siena_nvram_types[type];
  516. if (info->port != efx_port_num(efx))
  517. return -ENODEV;
  518. rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
  519. if (rc)
  520. return rc;
  521. if (protected)
  522. return -ENODEV; /* hide it */
  523. part->mcdi.nvram_type = type;
  524. part->type_name = info->name;
  525. part->mtd.type = MTD_NORFLASH;
  526. part->mtd.flags = MTD_CAP_NORFLASH;
  527. part->mtd.size = size;
  528. part->mtd.erasesize = erase_size;
  529. return 0;
  530. }
  531. static int siena_mtd_get_fw_subtypes(struct efx_nic *efx,
  532. struct efx_mtd *efx_mtd)
  533. {
  534. struct efx_mtd_partition *part;
  535. uint16_t fw_subtype_list[MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MINNUM];
  536. int rc;
  537. rc = efx_mcdi_get_board_cfg(efx, NULL, fw_subtype_list, NULL);
  538. if (rc)
  539. return rc;
  540. efx_for_each_partition(part, efx_mtd)
  541. part->mcdi.fw_subtype = fw_subtype_list[part->mcdi.nvram_type];
  542. return 0;
  543. }
  544. static int siena_mtd_probe(struct efx_nic *efx)
  545. {
  546. struct efx_mtd *efx_mtd;
  547. int rc = -ENODEV;
  548. u32 nvram_types;
  549. unsigned int type;
  550. ASSERT_RTNL();
  551. rc = efx_mcdi_nvram_types(efx, &nvram_types);
  552. if (rc)
  553. return rc;
  554. efx_mtd = kzalloc(sizeof(*efx_mtd) +
  555. hweight32(nvram_types) * sizeof(efx_mtd->part[0]),
  556. GFP_KERNEL);
  557. if (!efx_mtd)
  558. return -ENOMEM;
  559. efx_mtd->name = "Siena NVRAM manager";
  560. efx_mtd->ops = &siena_mtd_ops;
  561. type = 0;
  562. efx_mtd->n_parts = 0;
  563. while (nvram_types != 0) {
  564. if (nvram_types & 1) {
  565. rc = siena_mtd_probe_partition(efx, efx_mtd,
  566. efx_mtd->n_parts, type);
  567. if (rc == 0)
  568. efx_mtd->n_parts++;
  569. else if (rc != -ENODEV)
  570. goto fail;
  571. }
  572. type++;
  573. nvram_types >>= 1;
  574. }
  575. rc = siena_mtd_get_fw_subtypes(efx, efx_mtd);
  576. if (rc)
  577. goto fail;
  578. rc = efx_mtd_probe_device(efx, efx_mtd);
  579. fail:
  580. if (rc)
  581. kfree(efx_mtd);
  582. return rc;
  583. }