mthca_memfree.c 12 KB

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