uar.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2013, Mellanox Technologies inc. 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/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/mlx5/driver.h>
  35. #include <linux/mlx5/cmd.h>
  36. #include "mlx5_core.h"
  37. enum {
  38. NUM_DRIVER_UARS = 4,
  39. NUM_LOW_LAT_UUARS = 4,
  40. };
  41. struct mlx5_alloc_uar_mbox_in {
  42. struct mlx5_inbox_hdr hdr;
  43. u8 rsvd[8];
  44. };
  45. struct mlx5_alloc_uar_mbox_out {
  46. struct mlx5_outbox_hdr hdr;
  47. __be32 uarn;
  48. u8 rsvd[4];
  49. };
  50. struct mlx5_free_uar_mbox_in {
  51. struct mlx5_inbox_hdr hdr;
  52. __be32 uarn;
  53. u8 rsvd[4];
  54. };
  55. struct mlx5_free_uar_mbox_out {
  56. struct mlx5_outbox_hdr hdr;
  57. u8 rsvd[8];
  58. };
  59. int mlx5_cmd_alloc_uar(struct mlx5_core_dev *dev, u32 *uarn)
  60. {
  61. struct mlx5_alloc_uar_mbox_in in;
  62. struct mlx5_alloc_uar_mbox_out out;
  63. int err;
  64. memset(&in, 0, sizeof(in));
  65. memset(&out, 0, sizeof(out));
  66. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_ALLOC_UAR);
  67. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  68. if (err)
  69. goto ex;
  70. if (out.hdr.status) {
  71. err = mlx5_cmd_status_to_err(&out.hdr);
  72. goto ex;
  73. }
  74. *uarn = be32_to_cpu(out.uarn) & 0xffffff;
  75. ex:
  76. return err;
  77. }
  78. EXPORT_SYMBOL(mlx5_cmd_alloc_uar);
  79. int mlx5_cmd_free_uar(struct mlx5_core_dev *dev, u32 uarn)
  80. {
  81. struct mlx5_free_uar_mbox_in in;
  82. struct mlx5_free_uar_mbox_out out;
  83. int err;
  84. memset(&in, 0, sizeof(in));
  85. in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DEALLOC_UAR);
  86. in.uarn = cpu_to_be32(uarn);
  87. err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
  88. if (err)
  89. goto ex;
  90. if (out.hdr.status)
  91. err = mlx5_cmd_status_to_err(&out.hdr);
  92. ex:
  93. return err;
  94. }
  95. EXPORT_SYMBOL(mlx5_cmd_free_uar);
  96. static int need_uuar_lock(int uuarn)
  97. {
  98. int tot_uuars = NUM_DRIVER_UARS * MLX5_BF_REGS_PER_PAGE;
  99. if (uuarn == 0 || tot_uuars - NUM_LOW_LAT_UUARS)
  100. return 0;
  101. return 1;
  102. }
  103. int mlx5_alloc_uuars(struct mlx5_core_dev *dev, struct mlx5_uuar_info *uuari)
  104. {
  105. int tot_uuars = NUM_DRIVER_UARS * MLX5_BF_REGS_PER_PAGE;
  106. struct mlx5_bf *bf;
  107. phys_addr_t addr;
  108. int err;
  109. int i;
  110. uuari->num_uars = NUM_DRIVER_UARS;
  111. uuari->num_low_latency_uuars = NUM_LOW_LAT_UUARS;
  112. mutex_init(&uuari->lock);
  113. uuari->uars = kcalloc(uuari->num_uars, sizeof(*uuari->uars), GFP_KERNEL);
  114. if (!uuari->uars)
  115. return -ENOMEM;
  116. uuari->bfs = kcalloc(tot_uuars, sizeof(*uuari->bfs), GFP_KERNEL);
  117. if (!uuari->bfs) {
  118. err = -ENOMEM;
  119. goto out_uars;
  120. }
  121. uuari->bitmap = kcalloc(BITS_TO_LONGS(tot_uuars), sizeof(*uuari->bitmap),
  122. GFP_KERNEL);
  123. if (!uuari->bitmap) {
  124. err = -ENOMEM;
  125. goto out_bfs;
  126. }
  127. uuari->count = kcalloc(tot_uuars, sizeof(*uuari->count), GFP_KERNEL);
  128. if (!uuari->count) {
  129. err = -ENOMEM;
  130. goto out_bitmap;
  131. }
  132. for (i = 0; i < uuari->num_uars; i++) {
  133. err = mlx5_cmd_alloc_uar(dev, &uuari->uars[i].index);
  134. if (err)
  135. goto out_count;
  136. addr = dev->iseg_base + ((phys_addr_t)(uuari->uars[i].index) << PAGE_SHIFT);
  137. uuari->uars[i].map = ioremap(addr, PAGE_SIZE);
  138. if (!uuari->uars[i].map) {
  139. mlx5_cmd_free_uar(dev, uuari->uars[i].index);
  140. err = -ENOMEM;
  141. goto out_count;
  142. }
  143. mlx5_core_dbg(dev, "allocated uar index 0x%x, mmaped at %p\n",
  144. uuari->uars[i].index, uuari->uars[i].map);
  145. }
  146. for (i = 0; i < tot_uuars; i++) {
  147. bf = &uuari->bfs[i];
  148. bf->buf_size = dev->caps.gen.bf_reg_size / 2;
  149. bf->uar = &uuari->uars[i / MLX5_BF_REGS_PER_PAGE];
  150. bf->regreg = uuari->uars[i / MLX5_BF_REGS_PER_PAGE].map;
  151. bf->reg = NULL; /* Add WC support */
  152. bf->offset = (i % MLX5_BF_REGS_PER_PAGE) * dev->caps.gen.bf_reg_size +
  153. MLX5_BF_OFFSET;
  154. bf->need_lock = need_uuar_lock(i);
  155. spin_lock_init(&bf->lock);
  156. spin_lock_init(&bf->lock32);
  157. bf->uuarn = i;
  158. }
  159. return 0;
  160. out_count:
  161. for (i--; i >= 0; i--) {
  162. iounmap(uuari->uars[i].map);
  163. mlx5_cmd_free_uar(dev, uuari->uars[i].index);
  164. }
  165. kfree(uuari->count);
  166. out_bitmap:
  167. kfree(uuari->bitmap);
  168. out_bfs:
  169. kfree(uuari->bfs);
  170. out_uars:
  171. kfree(uuari->uars);
  172. return err;
  173. }
  174. int mlx5_free_uuars(struct mlx5_core_dev *dev, struct mlx5_uuar_info *uuari)
  175. {
  176. int i = uuari->num_uars;
  177. for (i--; i >= 0; i--) {
  178. iounmap(uuari->uars[i].map);
  179. mlx5_cmd_free_uar(dev, uuari->uars[i].index);
  180. }
  181. kfree(uuari->count);
  182. kfree(uuari->bitmap);
  183. kfree(uuari->bfs);
  184. kfree(uuari->uars);
  185. return 0;
  186. }