mtd.c 2.9 KB

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