mthca_memfree.c 11 KB

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