mtd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. int rc;
  23. rc = efx->type->mtd_erase(mtd, erase->addr, erase->len);
  24. if (rc == 0) {
  25. erase->state = MTD_ERASE_DONE;
  26. } else {
  27. erase->state = MTD_ERASE_FAILED;
  28. erase->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  29. }
  30. mtd_erase_callback(erase);
  31. return rc;
  32. }
  33. static void efx_mtd_sync(struct mtd_info *mtd)
  34. {
  35. struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
  36. struct efx_nic *efx = mtd->priv;
  37. int rc;
  38. rc = efx->type->mtd_sync(mtd);
  39. if (rc)
  40. pr_err("%s: %s sync failed (%d)\n",
  41. part->name, part->dev_type_name, rc);
  42. }
  43. static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
  44. {
  45. int rc;
  46. for (;;) {
  47. rc = mtd_device_unregister(&part->mtd);
  48. if (rc != -EBUSY)
  49. break;
  50. ssleep(1);
  51. }
  52. WARN_ON(rc);
  53. list_del(&part->node);
  54. }
  55. int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
  56. size_t n_parts, size_t sizeof_part)
  57. {
  58. struct efx_mtd_partition *part;
  59. size_t i;
  60. for (i = 0; i < n_parts; i++) {
  61. part = (struct efx_mtd_partition *)((char *)parts +
  62. i * sizeof_part);
  63. part->mtd.writesize = 1;
  64. part->mtd.owner = THIS_MODULE;
  65. part->mtd.priv = efx;
  66. part->mtd.name = part->name;
  67. part->mtd._erase = efx_mtd_erase;
  68. part->mtd._read = efx->type->mtd_read;
  69. part->mtd._write = efx->type->mtd_write;
  70. part->mtd._sync = efx_mtd_sync;
  71. efx->type->mtd_rename(part);
  72. if (mtd_device_register(&part->mtd, NULL, 0))
  73. goto fail;
  74. /* Add to list in order - efx_mtd_remove() depends on this */
  75. list_add_tail(&part->node, &efx->mtd_list);
  76. }
  77. return 0;
  78. fail:
  79. while (i--) {
  80. part = (struct efx_mtd_partition *)((char *)parts +
  81. i * sizeof_part);
  82. efx_mtd_remove_partition(part);
  83. }
  84. /* Failure is unlikely here, but probably means we're out of memory */
  85. return -ENOMEM;
  86. }
  87. void efx_mtd_remove(struct efx_nic *efx)
  88. {
  89. struct efx_mtd_partition *parts, *part, *next;
  90. WARN_ON(efx_dev_registered(efx));
  91. if (list_empty(&efx->mtd_list))
  92. return;
  93. parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
  94. node);
  95. list_for_each_entry_safe(part, next, &efx->mtd_list, node)
  96. efx_mtd_remove_partition(part);
  97. kfree(parts);
  98. }
  99. void efx_mtd_rename(struct efx_nic *efx)
  100. {
  101. struct efx_mtd_partition *part;
  102. ASSERT_RTNL();
  103. list_for_each_entry(part, &efx->mtd_list, node)
  104. efx->type->mtd_rename(part);
  105. }