mthca_memfree.c 12 KB

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