icm.c 11 KB

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