icm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright (c) 2005, 2006, 2007, 2008 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/errno.h>
  34. #include <linux/mm.h>
  35. #include <linux/scatterlist.h>
  36. #include <linux/slab.h>
  37. #include <linux/mlx4/cmd.h>
  38. #include "mlx4.h"
  39. #include "icm.h"
  40. #include "fw.h"
  41. /*
  42. * We allocate in as big chunks as we can, up to a maximum of 256 KB
  43. * per chunk.
  44. */
  45. enum {
  46. MLX4_ICM_ALLOC_SIZE = 1 << 18,
  47. MLX4_TABLE_CHUNK_SIZE = 1 << 18
  48. };
  49. static void mlx4_free_icm_pages(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
  50. {
  51. int i;
  52. if (chunk->nsg > 0)
  53. pci_unmap_sg(dev->persist->pdev, chunk->mem, chunk->npages,
  54. PCI_DMA_BIDIRECTIONAL);
  55. for (i = 0; i < chunk->npages; ++i)
  56. __free_pages(sg_page(&chunk->mem[i]),
  57. get_order(chunk->mem[i].length));
  58. }
  59. static void mlx4_free_icm_coherent(struct mlx4_dev *dev, struct mlx4_icm_chunk *chunk)
  60. {
  61. int i;
  62. for (i = 0; i < chunk->npages; ++i)
  63. dma_free_coherent(&dev->persist->pdev->dev,
  64. chunk->mem[i].length,
  65. lowmem_page_address(sg_page(&chunk->mem[i])),
  66. sg_dma_address(&chunk->mem[i]));
  67. }
  68. void mlx4_free_icm(struct mlx4_dev *dev, struct mlx4_icm *icm, int coherent)
  69. {
  70. struct mlx4_icm_chunk *chunk, *tmp;
  71. if (!icm)
  72. return;
  73. list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
  74. if (coherent)
  75. mlx4_free_icm_coherent(dev, chunk);
  76. else
  77. mlx4_free_icm_pages(dev, chunk);
  78. kfree(chunk);
  79. }
  80. kfree(icm);
  81. }
  82. static int mlx4_alloc_icm_pages(struct scatterlist *mem, int order,
  83. gfp_t gfp_mask, int node)
  84. {
  85. struct page *page;
  86. page = alloc_pages_node(node, gfp_mask, order);
  87. if (!page) {
  88. page = alloc_pages(gfp_mask, order);
  89. if (!page)
  90. return -ENOMEM;
  91. }
  92. sg_set_page(mem, page, PAGE_SIZE << order, 0);
  93. return 0;
  94. }
  95. static int mlx4_alloc_icm_coherent(struct device *dev, struct scatterlist *mem,
  96. int order, gfp_t gfp_mask)
  97. {
  98. void *buf = dma_alloc_coherent(dev, PAGE_SIZE << order,
  99. &sg_dma_address(mem), gfp_mask);
  100. if (!buf)
  101. return -ENOMEM;
  102. if (offset_in_page(buf)) {
  103. dma_free_coherent(dev, PAGE_SIZE << order,
  104. buf, sg_dma_address(mem));
  105. return -ENOMEM;
  106. }
  107. sg_set_buf(mem, buf, PAGE_SIZE << order);
  108. sg_dma_len(mem) = PAGE_SIZE << order;
  109. return 0;
  110. }
  111. struct mlx4_icm *mlx4_alloc_icm(struct mlx4_dev *dev, int npages,
  112. gfp_t gfp_mask, int coherent)
  113. {
  114. struct mlx4_icm *icm;
  115. struct mlx4_icm_chunk *chunk = NULL;
  116. int cur_order;
  117. int ret;
  118. /* We use sg_set_buf for coherent allocs, which assumes low memory */
  119. BUG_ON(coherent && (gfp_mask & __GFP_HIGHMEM));
  120. icm = kmalloc_node(sizeof(*icm),
  121. gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN),
  122. dev->numa_node);
  123. if (!icm) {
  124. icm = kmalloc(sizeof(*icm),
  125. gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
  126. if (!icm)
  127. return NULL;
  128. }
  129. icm->refcount = 0;
  130. INIT_LIST_HEAD(&icm->chunk_list);
  131. cur_order = get_order(MLX4_ICM_ALLOC_SIZE);
  132. while (npages > 0) {
  133. if (!chunk) {
  134. chunk = kmalloc_node(sizeof(*chunk),
  135. gfp_mask & ~(__GFP_HIGHMEM |
  136. __GFP_NOWARN),
  137. dev->numa_node);
  138. if (!chunk) {
  139. chunk = kmalloc(sizeof(*chunk),
  140. gfp_mask & ~(__GFP_HIGHMEM |
  141. __GFP_NOWARN));
  142. if (!chunk)
  143. goto fail;
  144. }
  145. sg_init_table(chunk->mem, MLX4_ICM_CHUNK_LEN);
  146. chunk->npages = 0;
  147. chunk->nsg = 0;
  148. list_add_tail(&chunk->list, &icm->chunk_list);
  149. }
  150. while (1 << cur_order > npages)
  151. --cur_order;
  152. if (coherent)
  153. ret = mlx4_alloc_icm_coherent(&dev->persist->pdev->dev,
  154. &chunk->mem[chunk->npages],
  155. cur_order, gfp_mask);
  156. else
  157. ret = mlx4_alloc_icm_pages(&chunk->mem[chunk->npages],
  158. cur_order, gfp_mask,
  159. dev->numa_node);
  160. if (ret) {
  161. if (--cur_order < 0)
  162. goto fail;
  163. else
  164. continue;
  165. }
  166. ++chunk->npages;
  167. if (coherent)
  168. ++chunk->nsg;
  169. else if (chunk->npages == MLX4_ICM_CHUNK_LEN) {
  170. chunk->nsg = pci_map_sg(dev->persist->pdev, chunk->mem,
  171. chunk->npages,
  172. PCI_DMA_BIDIRECTIONAL);
  173. if (chunk->nsg <= 0)
  174. goto fail;
  175. }
  176. if (chunk->npages == MLX4_ICM_CHUNK_LEN)
  177. chunk = NULL;
  178. npages -= 1 << cur_order;
  179. }
  180. if (!coherent && chunk) {
  181. chunk->nsg = pci_map_sg(dev->persist->pdev, chunk->mem,
  182. chunk->npages,
  183. PCI_DMA_BIDIRECTIONAL);
  184. if (chunk->nsg <= 0)
  185. goto fail;
  186. }
  187. return icm;
  188. fail:
  189. mlx4_free_icm(dev, icm, coherent);
  190. return NULL;
  191. }
  192. static int mlx4_MAP_ICM(struct mlx4_dev *dev, struct mlx4_icm *icm, u64 virt)
  193. {
  194. return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM, icm, virt);
  195. }
  196. static int mlx4_UNMAP_ICM(struct mlx4_dev *dev, u64 virt, u32 page_count)
  197. {
  198. return mlx4_cmd(dev, virt, page_count, 0, MLX4_CMD_UNMAP_ICM,
  199. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  200. }
  201. int mlx4_MAP_ICM_AUX(struct mlx4_dev *dev, struct mlx4_icm *icm)
  202. {
  203. return mlx4_map_cmd(dev, MLX4_CMD_MAP_ICM_AUX, icm, -1);
  204. }
  205. int mlx4_UNMAP_ICM_AUX(struct mlx4_dev *dev)
  206. {
  207. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_UNMAP_ICM_AUX,
  208. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  209. }
  210. int mlx4_table_get(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj,
  211. gfp_t gfp)
  212. {
  213. u32 i = (obj & (table->num_obj - 1)) /
  214. (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
  215. int ret = 0;
  216. mutex_lock(&table->mutex);
  217. if (table->icm[i]) {
  218. ++table->icm[i]->refcount;
  219. goto out;
  220. }
  221. table->icm[i] = mlx4_alloc_icm(dev, MLX4_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
  222. (table->lowmem ? gfp : GFP_HIGHUSER) |
  223. __GFP_NOWARN, table->coherent);
  224. if (!table->icm[i]) {
  225. ret = -ENOMEM;
  226. goto out;
  227. }
  228. if (mlx4_MAP_ICM(dev, table->icm[i], table->virt +
  229. (u64) i * MLX4_TABLE_CHUNK_SIZE)) {
  230. mlx4_free_icm(dev, table->icm[i], table->coherent);
  231. table->icm[i] = NULL;
  232. ret = -ENOMEM;
  233. goto out;
  234. }
  235. ++table->icm[i]->refcount;
  236. out:
  237. mutex_unlock(&table->mutex);
  238. return ret;
  239. }
  240. void mlx4_table_put(struct mlx4_dev *dev, struct mlx4_icm_table *table, u32 obj)
  241. {
  242. u32 i;
  243. u64 offset;
  244. i = (obj & (table->num_obj - 1)) / (MLX4_TABLE_CHUNK_SIZE / table->obj_size);
  245. mutex_lock(&table->mutex);
  246. if (--table->icm[i]->refcount == 0) {
  247. offset = (u64) i * MLX4_TABLE_CHUNK_SIZE;
  248. mlx4_UNMAP_ICM(dev, table->virt + offset,
  249. MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
  250. mlx4_free_icm(dev, table->icm[i], table->coherent);
  251. table->icm[i] = NULL;
  252. }
  253. mutex_unlock(&table->mutex);
  254. }
  255. void *mlx4_table_find(struct mlx4_icm_table *table, u32 obj,
  256. dma_addr_t *dma_handle)
  257. {
  258. int offset, dma_offset, i;
  259. u64 idx;
  260. struct mlx4_icm_chunk *chunk;
  261. struct mlx4_icm *icm;
  262. struct page *page = NULL;
  263. if (!table->lowmem)
  264. return NULL;
  265. mutex_lock(&table->mutex);
  266. idx = (u64) (obj & (table->num_obj - 1)) * table->obj_size;
  267. icm = table->icm[idx / MLX4_TABLE_CHUNK_SIZE];
  268. dma_offset = offset = idx % MLX4_TABLE_CHUNK_SIZE;
  269. if (!icm)
  270. goto out;
  271. list_for_each_entry(chunk, &icm->chunk_list, list) {
  272. for (i = 0; i < chunk->npages; ++i) {
  273. if (dma_handle && dma_offset >= 0) {
  274. if (sg_dma_len(&chunk->mem[i]) > dma_offset)
  275. *dma_handle = sg_dma_address(&chunk->mem[i]) +
  276. dma_offset;
  277. dma_offset -= sg_dma_len(&chunk->mem[i]);
  278. }
  279. /*
  280. * DMA mapping can merge pages but not split them,
  281. * so if we found the page, dma_handle has already
  282. * been assigned to.
  283. */
  284. if (chunk->mem[i].length > offset) {
  285. page = sg_page(&chunk->mem[i]);
  286. goto out;
  287. }
  288. offset -= chunk->mem[i].length;
  289. }
  290. }
  291. out:
  292. mutex_unlock(&table->mutex);
  293. return page ? lowmem_page_address(page) + offset : NULL;
  294. }
  295. int mlx4_table_get_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  296. u32 start, u32 end)
  297. {
  298. int inc = MLX4_TABLE_CHUNK_SIZE / table->obj_size;
  299. int err;
  300. u32 i;
  301. for (i = start; i <= end; i += inc) {
  302. err = mlx4_table_get(dev, table, i, GFP_KERNEL);
  303. if (err)
  304. goto fail;
  305. }
  306. return 0;
  307. fail:
  308. while (i > start) {
  309. i -= inc;
  310. mlx4_table_put(dev, table, i);
  311. }
  312. return err;
  313. }
  314. void mlx4_table_put_range(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  315. u32 start, u32 end)
  316. {
  317. u32 i;
  318. for (i = start; i <= end; i += MLX4_TABLE_CHUNK_SIZE / table->obj_size)
  319. mlx4_table_put(dev, table, i);
  320. }
  321. int mlx4_init_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table,
  322. u64 virt, int obj_size, u32 nobj, int reserved,
  323. int use_lowmem, int use_coherent)
  324. {
  325. int obj_per_chunk;
  326. int num_icm;
  327. unsigned chunk_size;
  328. int i;
  329. u64 size;
  330. obj_per_chunk = MLX4_TABLE_CHUNK_SIZE / obj_size;
  331. num_icm = (nobj + obj_per_chunk - 1) / obj_per_chunk;
  332. table->icm = kcalloc(num_icm, sizeof *table->icm, GFP_KERNEL);
  333. if (!table->icm)
  334. return -ENOMEM;
  335. table->virt = virt;
  336. table->num_icm = num_icm;
  337. table->num_obj = nobj;
  338. table->obj_size = obj_size;
  339. table->lowmem = use_lowmem;
  340. table->coherent = use_coherent;
  341. mutex_init(&table->mutex);
  342. size = (u64) nobj * obj_size;
  343. for (i = 0; i * MLX4_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
  344. chunk_size = MLX4_TABLE_CHUNK_SIZE;
  345. if ((i + 1) * MLX4_TABLE_CHUNK_SIZE > size)
  346. chunk_size = PAGE_ALIGN(size -
  347. i * MLX4_TABLE_CHUNK_SIZE);
  348. table->icm[i] = mlx4_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
  349. (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
  350. __GFP_NOWARN, use_coherent);
  351. if (!table->icm[i])
  352. goto err;
  353. if (mlx4_MAP_ICM(dev, table->icm[i], virt + i * MLX4_TABLE_CHUNK_SIZE)) {
  354. mlx4_free_icm(dev, table->icm[i], use_coherent);
  355. table->icm[i] = NULL;
  356. goto err;
  357. }
  358. /*
  359. * Add a reference to this ICM chunk so that it never
  360. * gets freed (since it contains reserved firmware objects).
  361. */
  362. ++table->icm[i]->refcount;
  363. }
  364. return 0;
  365. err:
  366. for (i = 0; i < num_icm; ++i)
  367. if (table->icm[i]) {
  368. mlx4_UNMAP_ICM(dev, virt + i * MLX4_TABLE_CHUNK_SIZE,
  369. MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
  370. mlx4_free_icm(dev, table->icm[i], use_coherent);
  371. }
  372. kfree(table->icm);
  373. return -ENOMEM;
  374. }
  375. void mlx4_cleanup_icm_table(struct mlx4_dev *dev, struct mlx4_icm_table *table)
  376. {
  377. int i;
  378. for (i = 0; i < table->num_icm; ++i)
  379. if (table->icm[i]) {
  380. mlx4_UNMAP_ICM(dev, table->virt + i * MLX4_TABLE_CHUNK_SIZE,
  381. MLX4_TABLE_CHUNK_SIZE / MLX4_ICM_PAGE_SIZE);
  382. mlx4_free_icm(dev, table->icm[i], table->coherent);
  383. }
  384. kfree(table->icm);
  385. }