mtd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /****************************************************************************
  3. * Driver for Solarflare network controllers and boards
  4. * Copyright 2005-2006 Fen Systems Ltd.
  5. * Copyright 2006-2013 Solarflare Communications Inc.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mtd/mtd.h>
  9. #include <linux/slab.h>
  10. #include <linux/rtnetlink.h>
  11. #include "net_driver.h"
  12. #include "efx.h"
  13. #define to_efx_mtd_partition(mtd) \
  14. container_of(mtd, struct efx_mtd_partition, mtd)
  15. /* MTD interface */
  16. static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
  17. {
  18. struct efx_nic *efx = mtd->priv;
  19. return efx->type->mtd_erase(mtd, erase->addr, erase->len);
  20. }
  21. static void efx_mtd_sync(struct mtd_info *mtd)
  22. {
  23. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  24. struct efx_nic *efx = mtd->priv;
  25. int rc;
  26. rc = efx->type->mtd_sync(mtd);
  27. if (rc)
  28. pr_err("%s: %s sync failed (%d)\n",
  29. part->name, part->dev_type_name, rc);
  30. }
  31. static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
  32. {
  33. int rc;
  34. for (;;) {
  35. rc = mtd_device_unregister(&part->mtd);
  36. if (rc != -EBUSY)
  37. break;
  38. ssleep(1);
  39. }
  40. WARN_ON(rc);
  41. list_del(&part->node);
  42. }
  43. int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
  44. size_t n_parts, size_t sizeof_part)
  45. {
  46. struct efx_mtd_partition *part;
  47. size_t i;
  48. for (i = 0; i < n_parts; i++) {
  49. part = (struct efx_mtd_partition *)((char *)parts +
  50. i * sizeof_part);
  51. part->mtd.writesize = 1;
  52. if (!(part->mtd.flags & MTD_NO_ERASE))
  53. part->mtd.flags |= MTD_WRITEABLE;
  54. part->mtd.owner = THIS_MODULE;
  55. part->mtd.priv = efx;
  56. part->mtd.name = part->name;
  57. part->mtd._erase = efx_mtd_erase;
  58. part->mtd._read = efx->type->mtd_read;
  59. part->mtd._write = efx->type->mtd_write;
  60. part->mtd._sync = efx_mtd_sync;
  61. efx->type->mtd_rename(part);
  62. if (mtd_device_register(&part->mtd, NULL, 0))
  63. goto fail;
  64. /* Add to list in order - efx_mtd_remove() depends on this */
  65. list_add_tail(&part->node, &efx->mtd_list);
  66. }
  67. return 0;
  68. fail:
  69. while (i--) {
  70. part = (struct efx_mtd_partition *)((char *)parts +
  71. i * sizeof_part);
  72. efx_mtd_remove_partition(part);
  73. }
  74. /* Failure is unlikely here, but probably means we're out of memory */
  75. return -ENOMEM;
  76. }
  77. void efx_mtd_remove(struct efx_nic *efx)
  78. {
  79. struct efx_mtd_partition *parts, *part, *next;
  80. WARN_ON(efx_dev_registered(efx));
  81. if (list_empty(&efx->mtd_list))
  82. return;
  83. parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
  84. node);
  85. list_for_each_entry_safe(part, next, &efx->mtd_list, node)
  86. efx_mtd_remove_partition(part);
  87. kfree(parts);
  88. }
  89. void efx_mtd_rename(struct efx_nic *efx)
  90. {
  91. struct efx_mtd_partition *part;
  92. ASSERT_RTNL();
  93. list_for_each_entry(part, &efx->mtd_list, node)
  94. efx->type->mtd_rename(part);
  95. }