mthca_memfree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. 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. * $Id$
  33. */
  34. #include <linux/mm.h>
  35. #include "mthca_memfree.h"
  36. #include "mthca_dev.h"
  37. #include "mthca_cmd.h"
  38. /*
  39. * We allocate in as big chunks as we can, up to a maximum of 256 KB
  40. * per chunk.
  41. */
  42. enum {
  43. MTHCA_ICM_ALLOC_SIZE = 1 << 18,
  44. MTHCA_TABLE_CHUNK_SIZE = 1 << 18
  45. };
  46. void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm)
  47. {
  48. struct mthca_icm_chunk *chunk, *tmp;
  49. int i;
  50. if (!icm)
  51. return;
  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 mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
  64. unsigned int gfp_mask)
  65. {
  66. struct mthca_icm *icm;
  67. struct mthca_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(MTHCA_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 == MTHCA_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. mthca_free_icm(dev, icm);
  116. return NULL;
  117. }
  118. int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
  119. {
  120. int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
  121. int ret = 0;
  122. u8 status;
  123. down(&table->mutex);
  124. if (table->icm[i]) {
  125. ++table->icm[i]->refcount;
  126. goto out;
  127. }
  128. table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
  129. (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
  130. __GFP_NOWARN);
  131. if (!table->icm[i]) {
  132. ret = -ENOMEM;
  133. goto out;
  134. }
  135. if (mthca_MAP_ICM(dev, table->icm[i], table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
  136. &status) || status) {
  137. mthca_free_icm(dev, table->icm[i]);
  138. table->icm[i] = NULL;
  139. ret = -ENOMEM;
  140. goto out;
  141. }
  142. ++table->icm[i]->refcount;
  143. out:
  144. up(&table->mutex);
  145. return ret;
  146. }
  147. void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
  148. {
  149. int i;
  150. u8 status;
  151. if (!mthca_is_memfree(dev))
  152. return;
  153. i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
  154. down(&table->mutex);
  155. if (--table->icm[i]->refcount == 0) {
  156. mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
  157. MTHCA_TABLE_CHUNK_SIZE >> 12, &status);
  158. mthca_free_icm(dev, table->icm[i]);
  159. table->icm[i] = NULL;
  160. }
  161. up(&table->mutex);
  162. }
  163. void *mthca_table_find(struct mthca_icm_table *table, int obj)
  164. {
  165. int idx, offset, i;
  166. struct mthca_icm_chunk *chunk;
  167. struct mthca_icm *icm;
  168. struct page *page = NULL;
  169. if (!table->lowmem)
  170. return NULL;
  171. down(&table->mutex);
  172. idx = (obj & (table->num_obj - 1)) * table->obj_size;
  173. icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE];
  174. offset = idx % MTHCA_TABLE_CHUNK_SIZE;
  175. if (!icm)
  176. goto out;
  177. list_for_each_entry(chunk, &icm->chunk_list, list) {
  178. for (i = 0; i < chunk->npages; ++i) {
  179. if (chunk->mem[i].length >= offset) {
  180. page = chunk->mem[i].page;
  181. break;
  182. }
  183. offset -= chunk->mem[i].length;
  184. }
  185. }
  186. out:
  187. up(&table->mutex);
  188. return page ? lowmem_page_address(page) + offset : NULL;
  189. }
  190. int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
  191. int start, int end)
  192. {
  193. int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size;
  194. int i, err;
  195. for (i = start; i <= end; i += inc) {
  196. err = mthca_table_get(dev, table, i);
  197. if (err)
  198. goto fail;
  199. }
  200. return 0;
  201. fail:
  202. while (i > start) {
  203. i -= inc;
  204. mthca_table_put(dev, table, i);
  205. }
  206. return err;
  207. }
  208. void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
  209. int start, int end)
  210. {
  211. int i;
  212. if (!mthca_is_memfree(dev))
  213. return;
  214. for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size)
  215. mthca_table_put(dev, table, i);
  216. }
  217. struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
  218. u64 virt, int obj_size,
  219. int nobj, int reserved,
  220. int use_lowmem)
  221. {
  222. struct mthca_icm_table *table;
  223. int num_icm;
  224. int i;
  225. u8 status;
  226. num_icm = obj_size * nobj / MTHCA_TABLE_CHUNK_SIZE;
  227. table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
  228. if (!table)
  229. return NULL;
  230. table->virt = virt;
  231. table->num_icm = num_icm;
  232. table->num_obj = nobj;
  233. table->obj_size = obj_size;
  234. table->lowmem = use_lowmem;
  235. init_MUTEX(&table->mutex);
  236. for (i = 0; i < num_icm; ++i)
  237. table->icm[i] = NULL;
  238. for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
  239. table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
  240. (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
  241. __GFP_NOWARN);
  242. if (!table->icm[i])
  243. goto err;
  244. if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE,
  245. &status) || status) {
  246. mthca_free_icm(dev, table->icm[i]);
  247. table->icm[i] = NULL;
  248. goto err;
  249. }
  250. /*
  251. * Add a reference to this ICM chunk so that it never
  252. * gets freed (since it contains reserved firmware objects).
  253. */
  254. ++table->icm[i]->refcount;
  255. }
  256. return table;
  257. err:
  258. for (i = 0; i < num_icm; ++i)
  259. if (table->icm[i]) {
  260. mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE,
  261. MTHCA_TABLE_CHUNK_SIZE >> 12, &status);
  262. mthca_free_icm(dev, table->icm[i]);
  263. }
  264. kfree(table);
  265. return NULL;
  266. }
  267. void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
  268. {
  269. int i;
  270. u8 status;
  271. for (i = 0; i < table->num_icm; ++i)
  272. if (table->icm[i]) {
  273. mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
  274. MTHCA_TABLE_CHUNK_SIZE >> 12, &status);
  275. mthca_free_icm(dev, table->icm[i]);
  276. }
  277. kfree(table);
  278. }
  279. static u64 mthca_uarc_virt(struct mthca_dev *dev, int page)
  280. {
  281. return dev->uar_table.uarc_base +
  282. dev->driver_uar.index * dev->uar_table.uarc_size +
  283. page * 4096;
  284. }
  285. int mthca_alloc_db(struct mthca_dev *dev, int type, u32 qn, u32 **db)
  286. {
  287. int group;
  288. int start, end, dir;
  289. int i, j;
  290. struct mthca_db_page *page;
  291. int ret = 0;
  292. u8 status;
  293. down(&dev->db_tab->mutex);
  294. switch (type) {
  295. case MTHCA_DB_TYPE_CQ_ARM:
  296. case MTHCA_DB_TYPE_SQ:
  297. group = 0;
  298. start = 0;
  299. end = dev->db_tab->max_group1;
  300. dir = 1;
  301. break;
  302. case MTHCA_DB_TYPE_CQ_SET_CI:
  303. case MTHCA_DB_TYPE_RQ:
  304. case MTHCA_DB_TYPE_SRQ:
  305. group = 1;
  306. start = dev->db_tab->npages - 1;
  307. end = dev->db_tab->min_group2;
  308. dir = -1;
  309. break;
  310. default:
  311. ret = -EINVAL;
  312. goto out;
  313. }
  314. for (i = start; i != end; i += dir)
  315. if (dev->db_tab->page[i].db_rec &&
  316. !bitmap_full(dev->db_tab->page[i].used,
  317. MTHCA_DB_REC_PER_PAGE)) {
  318. page = dev->db_tab->page + i;
  319. goto found;
  320. }
  321. if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) {
  322. ret = -ENOMEM;
  323. goto out;
  324. }
  325. page = dev->db_tab->page + end;
  326. page->db_rec = dma_alloc_coherent(&dev->pdev->dev, 4096,
  327. &page->mapping, GFP_KERNEL);
  328. if (!page->db_rec) {
  329. ret = -ENOMEM;
  330. goto out;
  331. }
  332. memset(page->db_rec, 0, 4096);
  333. ret = mthca_MAP_ICM_page(dev, page->mapping, mthca_uarc_virt(dev, i), &status);
  334. if (!ret && status)
  335. ret = -EINVAL;
  336. if (ret) {
  337. dma_free_coherent(&dev->pdev->dev, 4096,
  338. page->db_rec, page->mapping);
  339. goto out;
  340. }
  341. bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE);
  342. if (group == 0)
  343. ++dev->db_tab->max_group1;
  344. else
  345. --dev->db_tab->min_group2;
  346. found:
  347. j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE);
  348. set_bit(j, page->used);
  349. if (group == 1)
  350. j = MTHCA_DB_REC_PER_PAGE - 1 - j;
  351. ret = i * MTHCA_DB_REC_PER_PAGE + j;
  352. page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5));
  353. *db = (u32 *) &page->db_rec[j];
  354. out:
  355. up(&dev->db_tab->mutex);
  356. return ret;
  357. }
  358. void mthca_free_db(struct mthca_dev *dev, int type, int db_index)
  359. {
  360. int i, j;
  361. struct mthca_db_page *page;
  362. u8 status;
  363. i = db_index / MTHCA_DB_REC_PER_PAGE;
  364. j = db_index % MTHCA_DB_REC_PER_PAGE;
  365. page = dev->db_tab->page + i;
  366. down(&dev->db_tab->mutex);
  367. page->db_rec[j] = 0;
  368. if (i >= dev->db_tab->min_group2)
  369. j = MTHCA_DB_REC_PER_PAGE - 1 - j;
  370. clear_bit(j, page->used);
  371. if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) &&
  372. i >= dev->db_tab->max_group1 - 1) {
  373. mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, i), 1, &status);
  374. dma_free_coherent(&dev->pdev->dev, 4096,
  375. page->db_rec, page->mapping);
  376. page->db_rec = NULL;
  377. if (i == dev->db_tab->max_group1) {
  378. --dev->db_tab->max_group1;
  379. /* XXX may be able to unmap more pages now */
  380. }
  381. if (i == dev->db_tab->min_group2)
  382. ++dev->db_tab->min_group2;
  383. }
  384. up(&dev->db_tab->mutex);
  385. }
  386. int mthca_init_db_tab(struct mthca_dev *dev)
  387. {
  388. int i;
  389. if (!mthca_is_memfree(dev))
  390. return 0;
  391. dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL);
  392. if (!dev->db_tab)
  393. return -ENOMEM;
  394. init_MUTEX(&dev->db_tab->mutex);
  395. dev->db_tab->npages = dev->uar_table.uarc_size / 4096;
  396. dev->db_tab->max_group1 = 0;
  397. dev->db_tab->min_group2 = dev->db_tab->npages - 1;
  398. dev->db_tab->page = kmalloc(dev->db_tab->npages *
  399. sizeof *dev->db_tab->page,
  400. GFP_KERNEL);
  401. if (!dev->db_tab->page) {
  402. kfree(dev->db_tab);
  403. return -ENOMEM;
  404. }
  405. for (i = 0; i < dev->db_tab->npages; ++i)
  406. dev->db_tab->page[i].db_rec = NULL;
  407. return 0;
  408. }
  409. void mthca_cleanup_db_tab(struct mthca_dev *dev)
  410. {
  411. int i;
  412. u8 status;
  413. if (!mthca_is_memfree(dev))
  414. return;
  415. /*
  416. * Because we don't always free our UARC pages when they
  417. * become empty to make mthca_free_db() simpler we need to
  418. * make a sweep through the doorbell pages and free any
  419. * leftover pages now.
  420. */
  421. for (i = 0; i < dev->db_tab->npages; ++i) {
  422. if (!dev->db_tab->page[i].db_rec)
  423. continue;
  424. if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE))
  425. mthca_warn(dev, "Kernel UARC page %d not empty\n", i);
  426. mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, i), 1, &status);
  427. dma_free_coherent(&dev->pdev->dev, 4096,
  428. dev->db_tab->page[i].db_rec,
  429. dev->db_tab->page[i].mapping);
  430. }
  431. kfree(dev->db_tab->page);
  432. kfree(dev->db_tab);
  433. }