alloc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/errno.h>
  33. #include <linux/slab.h>
  34. #include <linux/mm.h>
  35. #include <linux/export.h>
  36. #include <linux/bitmap.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/mlx5/driver.h>
  40. #include "mlx5_core.h"
  41. /* Handling for queue buffers -- we allocate a bunch of memory and
  42. * register it in a memory region at HCA virtual address 0.
  43. */
  44. int mlx5_buf_alloc(struct mlx5_core_dev *dev, int size, struct mlx5_buf *buf)
  45. {
  46. dma_addr_t t;
  47. buf->size = size;
  48. buf->npages = 1;
  49. buf->page_shift = (u8)get_order(size) + PAGE_SHIFT;
  50. buf->direct.buf = dma_zalloc_coherent(&dev->pdev->dev,
  51. size, &t, GFP_KERNEL);
  52. if (!buf->direct.buf)
  53. return -ENOMEM;
  54. buf->direct.map = t;
  55. while (t & ((1 << buf->page_shift) - 1)) {
  56. --buf->page_shift;
  57. buf->npages *= 2;
  58. }
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(mlx5_buf_alloc);
  62. void mlx5_buf_free(struct mlx5_core_dev *dev, struct mlx5_buf *buf)
  63. {
  64. dma_free_coherent(&dev->pdev->dev, buf->size, buf->direct.buf,
  65. buf->direct.map);
  66. }
  67. EXPORT_SYMBOL_GPL(mlx5_buf_free);
  68. static struct mlx5_db_pgdir *mlx5_alloc_db_pgdir(struct device *dma_device)
  69. {
  70. struct mlx5_db_pgdir *pgdir;
  71. pgdir = kzalloc(sizeof(*pgdir), GFP_KERNEL);
  72. if (!pgdir)
  73. return NULL;
  74. bitmap_fill(pgdir->bitmap, MLX5_DB_PER_PAGE);
  75. pgdir->db_page = dma_alloc_coherent(dma_device, PAGE_SIZE,
  76. &pgdir->db_dma, GFP_KERNEL);
  77. if (!pgdir->db_page) {
  78. kfree(pgdir);
  79. return NULL;
  80. }
  81. return pgdir;
  82. }
  83. static int mlx5_alloc_db_from_pgdir(struct mlx5_db_pgdir *pgdir,
  84. struct mlx5_db *db)
  85. {
  86. int offset;
  87. int i;
  88. i = find_first_bit(pgdir->bitmap, MLX5_DB_PER_PAGE);
  89. if (i >= MLX5_DB_PER_PAGE)
  90. return -ENOMEM;
  91. __clear_bit(i, pgdir->bitmap);
  92. db->u.pgdir = pgdir;
  93. db->index = i;
  94. offset = db->index * L1_CACHE_BYTES;
  95. db->db = pgdir->db_page + offset / sizeof(*pgdir->db_page);
  96. db->dma = pgdir->db_dma + offset;
  97. db->db[0] = 0;
  98. db->db[1] = 0;
  99. return 0;
  100. }
  101. int mlx5_db_alloc(struct mlx5_core_dev *dev, struct mlx5_db *db)
  102. {
  103. struct mlx5_db_pgdir *pgdir;
  104. int ret = 0;
  105. mutex_lock(&dev->priv.pgdir_mutex);
  106. list_for_each_entry(pgdir, &dev->priv.pgdir_list, list)
  107. if (!mlx5_alloc_db_from_pgdir(pgdir, db))
  108. goto out;
  109. pgdir = mlx5_alloc_db_pgdir(&(dev->pdev->dev));
  110. if (!pgdir) {
  111. ret = -ENOMEM;
  112. goto out;
  113. }
  114. list_add(&pgdir->list, &dev->priv.pgdir_list);
  115. /* This should never fail -- we just allocated an empty page: */
  116. WARN_ON(mlx5_alloc_db_from_pgdir(pgdir, db));
  117. out:
  118. mutex_unlock(&dev->priv.pgdir_mutex);
  119. return ret;
  120. }
  121. EXPORT_SYMBOL_GPL(mlx5_db_alloc);
  122. void mlx5_db_free(struct mlx5_core_dev *dev, struct mlx5_db *db)
  123. {
  124. mutex_lock(&dev->priv.pgdir_mutex);
  125. __set_bit(db->index, db->u.pgdir->bitmap);
  126. if (bitmap_full(db->u.pgdir->bitmap, MLX5_DB_PER_PAGE)) {
  127. dma_free_coherent(&(dev->pdev->dev), PAGE_SIZE,
  128. db->u.pgdir->db_page, db->u.pgdir->db_dma);
  129. list_del(&db->u.pgdir->list);
  130. kfree(db->u.pgdir);
  131. }
  132. mutex_unlock(&dev->priv.pgdir_mutex);
  133. }
  134. EXPORT_SYMBOL_GPL(mlx5_db_free);
  135. void mlx5_fill_page_array(struct mlx5_buf *buf, __be64 *pas)
  136. {
  137. u64 addr;
  138. int i;
  139. for (i = 0; i < buf->npages; i++) {
  140. addr = buf->direct.map + (i << buf->page_shift);
  141. pas[i] = cpu_to_be64(addr);
  142. }
  143. }
  144. EXPORT_SYMBOL_GPL(mlx5_fill_page_array);