mtd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_ef4_mtd_partition(mtd) \
  14. container_of(mtd, struct ef4_mtd_partition, mtd)
  15. /* MTD interface */
  16. static int ef4_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
  17. {
  18. struct ef4_nic *efx = mtd->priv;
  19. return efx->type->mtd_erase(mtd, erase->addr, erase->len);
  20. }
  21. static void ef4_mtd_sync(struct mtd_info *mtd)
  22. {
  23. struct ef4_mtd_partition *part = to_ef4_mtd_partition(mtd);
  24. struct ef4_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 ef4_mtd_remove_partition(struct ef4_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 ef4_mtd_add(struct ef4_nic *efx, struct ef4_mtd_partition *parts,
  44. size_t n_parts, size_t sizeof_part)
  45. {
  46. struct ef4_mtd_partition *part;
  47. size_t i;
  48. for (i = 0; i < n_parts; i++) {
  49. part = (struct ef4_mtd_partition *)((char *)parts +
  50. i * sizeof_part);
  51. part->mtd.writesize = 1;
  52. part->mtd.owner = THIS_MODULE;
  53. part->mtd.priv = efx;
  54. part->mtd.name = part->name;
  55. part->mtd._erase = ef4_mtd_erase;
  56. part->mtd._read = efx->type->mtd_read;
  57. part->mtd._write = efx->type->mtd_write;
  58. part->mtd._sync = ef4_mtd_sync;
  59. efx->type->mtd_rename(part);
  60. if (mtd_device_register(&part->mtd, NULL, 0))
  61. goto fail;
  62. /* Add to list in order - ef4_mtd_remove() depends on this */
  63. list_add_tail(&part->node, &efx->mtd_list);
  64. }
  65. return 0;
  66. fail:
  67. while (i--) {
  68. part = (struct ef4_mtd_partition *)((char *)parts +
  69. i * sizeof_part);
  70. ef4_mtd_remove_partition(part);
  71. }
  72. /* Failure is unlikely here, but probably means we're out of memory */
  73. return -ENOMEM;
  74. }
  75. void ef4_mtd_remove(struct ef4_nic *efx)
  76. {
  77. struct ef4_mtd_partition *parts, *part, *next;
  78. WARN_ON(ef4_dev_registered(efx));
  79. if (list_empty(&efx->mtd_list))
  80. return;
  81. parts = list_first_entry(&efx->mtd_list, struct ef4_mtd_partition,
  82. node);
  83. list_for_each_entry_safe(part, next, &efx->mtd_list, node)
  84. ef4_mtd_remove_partition(part);
  85. kfree(parts);
  86. }
  87. void ef4_mtd_rename(struct ef4_nic *efx)
  88. {
  89. struct ef4_mtd_partition *part;
  90. ASSERT_RTNL();
  91. list_for_each_entry(part, &efx->mtd_list, node)
  92. efx->type->mtd_rename(part);
  93. }