icm.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/init.h>
  34. #include <linux/errno.h>
  35. #include <linux/mm.h>
  36. #include <linux/mlx4/cmd.h>
  37. #include "mlx4.h"
  38. #include "icm.h"
  39. #include "fw.h"
  40. /*
  41. * We allocate in as big chunks as we can, up to a maximum of 256 KB
  42. * per chunk.
  43. */
  44. enum {
  45. MLX4_ICM_ALLOC_SIZE = 1 << 18,
  46. MLX4_TABLE_CHUNK_SIZE = 1 << 18
  47. };
  48. void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm)
  49. {
  50. struct mlx4_icm_chunk *chunk, *tmp;
  51. int i;
  52. list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
  53. if (chunk->nsg > 0)
  54. pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages,
  55. PCI_DMA_BIDIRECTIONAL);
  56. for (i = 0; i < chunk->npages; ++i)
  57. __free_pages(chunk->mem[i].page,
  58. get_order(chunk->mem[i].length));
  59. kfree(chunk);
  60. }
  61. kfree(icm);
  62. }
  63. struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
  64. gfp_t gfp_mask)
  65. {
  66. struct mlx4_icm *icm;
  67. struct mlx4_icm_chunk *chunk = NULL;
  68. int cur_order;
  69. icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
  70. if (!icm)
  71. return icm;
  72. icm->refcount = 0;
  73. INIT_LIST_HEAD(&icm->chunk_list);
  74. cur_order = get_order(MLX4_ICM_ALLOC_SIZE);
  75. while (npages > 0) {
  76. if (!chunk) {
  77. chunk = kmalloc(sizeof *chunk,
  78. gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
  79. if (!chunk)
  80. goto fail;
  81. chunk->npages = 0;
  82. chunk->nsg = 0;
  83. list_add_tail(&chunk->list, &icm->chunk_list);
  84. }
  85. while (1 << cur_order > npages)
  86. --cur_order;
  87. chunk->mem[chunk->npages].page = alloc_pages(gfp_mask, cur_order);
  88. if (chunk->mem[chunk->npages].page) {
  89. chunk->mem[chunk->npages].length = PAGE_SIZE << cur_order;
  90. chunk->mem[chunk->npages].offset = 0;
  91. if (++chunk->npages == MLX4_ICM_CHUNK_LEN) {
  92. chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
  93. chunk->npages,
  94. PCI_DMA_BIDIRECTIONAL);
  95. if (chunk->nsg <= 0)
  96. goto fail;
  97. chunk = NULL;
  98. }
  99. npages -= 1 << cur_order;
  100. } else {
  101. --cur_order;
  102. if (cur_order < 0)
  103. goto fail;
  104. }
  105. }
  106. if (chunk) {
  107. chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
  108. chunk->npages,
  109. PCI_DMA_BIDIRECTIONAL);
  110. if (chunk->nsg <= 0)
  111. goto fail;
  112. }
  113. return icm;
  114. fail:
  115. mlx4_free_icm(dev, icm);
  116. return NULL;
  117. }
  118. static int mlx4_MAP_ICM(struct mlx4_dev *dev, struct mlx4_icm *icm, u64 virt)
  119. {
  120. return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM, icm, virt);
  121. }
  122. int mlx4_UNMAP_ICM(struct mlx4_dev *dev, u64 virt, u32 page_count)
  123. {
  124. return mlx4_cmd(dev, virt, page_count, 0, MLX4_CMD_UNMAP_ICM,
  125. MLX4_CMD_TIME_CLASS_B);
  126. }
  127. int mlx4_MAP_ICM_page(struct mlx4_dev *dev, u64 dma_addr, u64 virt)
  128. {
  129. struct mlx4_cmd_mailbox *mailbox;
  130. __be64 *inbox;
  131. int err;
  132. mailbox = mlx4_alloc_cmd_mailbox(dev);
  133. if (IS_ERR(mailbox))
  134. return PTR_ERR(mailbox);
  135. inbox = mailbox->buf;
  136. inbox[0] = cpu_to_be64(virt);
  137. inbox[1] = cpu_to_be64(dma_addr);
  138. err = mlx4_cmd(dev, mailbox->dma, 1, 0, MLX4_CMD_MAP_ICM,
  139. MLX4_CMD_TIME_CLASS_B);
  140. mlx4_free_cmd_mailbox(dev, mailbox);
  141. if (!err)
  142. mlx4_dbg(dev, "Mapped page at %llx to %llx for ICM.\n",
  143. (unsigned long long) dma_addr, (unsigned long long) virt);
  144. return err;
  145. }
  146. int mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm)
  147. {
  148. return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM_AUX, icm, -1);
  149. }
  150. int mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev)
  151. {
  152. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_UNMAP_ICM_AUX, MLX4_CMD_TIME_CLASS_B);
  153. }
  154. int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, int obj)
  155. {
  156. int i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
  157. int ret = 0;
  158. mutex_lock(&table->mutex);
  159. if (table->icm[i]) {
  160. ++table->icm[i]->refcount;
  161. goto out;
  162. }
  163. table->icm[i] = mlx4_alloc_icm(dev, MLX4_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
  164. (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
  165. __GFP_NOWARN);
  166. if (!table->icm[i]) {
  167. ret = -ENOMEM;
  168. goto out;
  169. }
  170. if (mlx4_MAP_ICM(dev, table->icm[i], table->virt +
  171. (u64) i * MLX4_TABLE_CHUNK_SIZE)) {
  172. mlx4_free_icm(dev, table->icm[i]);
  173. table->icm[i] = NULL;
  174. ret = -ENOMEM;
  175. goto out;
  176. }
  177. ++table->icm[i]->refcount;
  178. out:
  179. mutex_unlock(&table->mutex);
  180. return ret;
  181. }
  182. void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, int obj)
  183. {
  184. int i;
  185. i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
  186. mutex_lock(&table->mutex);
  187. if (--table->icm[i]->refcount == 0) {
  188. mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE,
  189. MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
  190. mlx4_free_icm(dev, table->icm[i]);
  191. table->icm[i] = NULL;
  192. }
  193. mutex_unlock(&table->mutex);
  194. }
  195. void *mlx4_table_find(struct mlx4_icm_table *table, int obj)
  196. {
  197. int idx, offset, i;
  198. struct mlx4_icm_chunk *chunk;
  199. struct mlx4_icm *icm;
  200. struct page *page = NULL;
  201. if (!table->lowmem)
  202. return NULL;
  203. mutex_lock(&table->mutex);
  204. idx = obj & (table->num_obj - 1);
  205. icm = table->icm[idx / (MLX4_TABLE_CHUNK_SIZE / table->obj_size)];
  206. offset = idx % (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
  207. if (!icm)
  208. goto out;
  209. list_for_each_entry(chunk, &icm->chunk_list, list) {
  210. for (i = 0; i < chunk->npages; ++i) {
  211. if (chunk->mem[i].length > offset) {
  212. page = chunk->mem[i].page;
  213. goto out;
  214. }
  215. offset -= chunk->mem[i].length;
  216. }
  217. }
  218. out:
  219. mutex_unlock(&table->mutex);
  220. return page ? lowmem_page_address(page) + offset : NULL;
  221. }
  222. int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  223. int start, int end)
  224. {
  225. int inc = MLX4_TABLE_CHUNK_SIZE / table->obj_size;
  226. int i, err;
  227. for (i = start; i <= end; i += inc) {
  228. err = mlx4_table_get(dev, table, i);
  229. if (err)
  230. goto fail;
  231. }
  232. return 0;
  233. fail:
  234. while (i > start) {
  235. i -= inc;
  236. mlx4_table_put(dev, table, i);
  237. }
  238. return err;
  239. }
  240. void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  241. int start, int end)
  242. {
  243. int i;
  244. for (i = start; i <= end; i += MLX4_TABLE_CHUNK_SIZE / table->obj_size)
  245. mlx4_table_put(dev, table, i);
  246. }
  247. int mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  248. u64 virt, int obj_size, int nobj, int reserved,
  249. int use_lowmem)
  250. {
  251. int obj_per_chunk;
  252. int num_icm;
  253. unsigned chunk_size;
  254. int i;
  255. obj_per_chunk = MLX4_TABLE_CHUNK_SIZE / obj_size;
  256. num_icm = (nobj + obj_per_chunk - 1) / obj_per_chunk;
  257. table->icm = kcalloc(num_icm, sizeof *table->icm, GFP_KERNEL);
  258. if (!table->icm)
  259. return -ENOMEM;
  260. table->virt = virt;
  261. table->num_icm = num_icm;
  262. table->num_obj = nobj;
  263. table->obj_size = obj_size;
  264. table->lowmem = use_lowmem;
  265. mutex_init(&table->mutex);
  266. for (i = 0; i * MLX4_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
  267. chunk_size = MLX4_TABLE_CHUNK_SIZE;
  268. if ((i + 1) * MLX4_TABLE_CHUNK_SIZE > nobj * obj_size)
  269. chunk_size = PAGE_ALIGN(nobj * obj_size - i * MLX4_TABLE_CHUNK_SIZE);
  270. table->icm[i] = mlx4_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
  271. (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
  272. __GFP_NOWARN);
  273. if (!table->icm[i])
  274. goto err;
  275. if (mlx4_MAP_ICM(dev, table->icm[i], virt + i * MLX4_TABLE_CHUNK_SIZE)) {
  276. mlx4_free_icm(dev, table->icm[i]);
  277. table->icm[i] = NULL;
  278. goto err;
  279. }
  280. /*
  281. * Add a reference to this ICM chunk so that it never
  282. * gets freed (since it contains reserved firmware objects).
  283. */
  284. ++table->icm[i]->refcount;
  285. }
  286. return 0;
  287. err:
  288. for (i = 0; i < num_icm; ++i)
  289. if (table->icm[i]) {
  290. mlx4_UNMAP_ICM(dev, virt + i * MLX4_TABLE_CHUNK_SIZE,
  291. MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
  292. mlx4_free_icm(dev, table->icm[i]);
  293. }
  294. return -ENOMEM;
  295. }
  296. void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table)
  297. {
  298. int i;
  299. for (i = 0; i < table->num_icm; ++i)
  300. if (table->icm[i]) {
  301. mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE,
  302. MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
  303. mlx4_free_icm(dev, table->icm[i]);
  304. }
  305. kfree(table->icm);
  306. }