icm.c 11 KB

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