mthca_memfree.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. *
  34. * $Id$
  35. */
  36. #include <linux/mm.h>
  37. #include "mthca_memfree.h"
  38. #include "mthca_dev.h"
  39. #include "mthca_cmd.h"
  40. /*
  41. * We allocate in as big chunks as we can, up to a maximum of 256 KB
  42. * per chunk.
  43. */
  44. enum {
  45. MTHCA_ICM_ALLOC_SIZE = 1 << 18,
  46. MTHCA_TABLE_CHUNK_SIZE = 1 << 18
  47. };
  48. struct mthca_user_db_table {
  49. struct mutex mutex;
  50. struct {
  51. u64 uvirt;
  52. struct scatterlist mem;
  53. int refcount;
  54. } page[0];
  55. };
  56. void mthca_free_icm(struct mthca_dev *dev, struct mthca_icm *icm)
  57. {
  58. struct mthca_icm_chunk *chunk, *tmp;
  59. int i;
  60. if (!icm)
  61. return;
  62. list_for_each_entry_safe(chunk, tmp, &icm->chunk_list, list) {
  63. if (chunk->nsg > 0)
  64. pci_unmap_sg(dev->pdev, chunk->mem, chunk->npages,
  65. PCI_DMA_BIDIRECTIONAL);
  66. for (i = 0; i < chunk->npages; ++i)
  67. __free_pages(chunk->mem[i].page,
  68. get_order(chunk->mem[i].length));
  69. kfree(chunk);
  70. }
  71. kfree(icm);
  72. }
  73. struct mthca_icm *mthca_alloc_icm(struct mthca_dev *dev, int npages,
  74. gfp_t gfp_mask)
  75. {
  76. struct mthca_icm *icm;
  77. struct mthca_icm_chunk *chunk = NULL;
  78. int cur_order;
  79. icm = kmalloc(sizeof *icm, gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
  80. if (!icm)
  81. return icm;
  82. icm->refcount = 0;
  83. INIT_LIST_HEAD(&icm->chunk_list);
  84. cur_order = get_order(MTHCA_ICM_ALLOC_SIZE);
  85. while (npages > 0) {
  86. if (!chunk) {
  87. chunk = kmalloc(sizeof *chunk,
  88. gfp_mask & ~(__GFP_HIGHMEM | __GFP_NOWARN));
  89. if (!chunk)
  90. goto fail;
  91. chunk->npages = 0;
  92. chunk->nsg = 0;
  93. list_add_tail(&chunk->list, &icm->chunk_list);
  94. }
  95. while (1 << cur_order > npages)
  96. --cur_order;
  97. chunk->mem[chunk->npages].page = alloc_pages(gfp_mask, cur_order);
  98. if (chunk->mem[chunk->npages].page) {
  99. chunk->mem[chunk->npages].length = PAGE_SIZE << cur_order;
  100. chunk->mem[chunk->npages].offset = 0;
  101. if (++chunk->npages == MTHCA_ICM_CHUNK_LEN) {
  102. chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
  103. chunk->npages,
  104. PCI_DMA_BIDIRECTIONAL);
  105. if (chunk->nsg <= 0)
  106. goto fail;
  107. chunk = NULL;
  108. }
  109. npages -= 1 << cur_order;
  110. } else {
  111. --cur_order;
  112. if (cur_order < 0)
  113. goto fail;
  114. }
  115. }
  116. if (chunk) {
  117. chunk->nsg = pci_map_sg(dev->pdev, chunk->mem,
  118. chunk->npages,
  119. PCI_DMA_BIDIRECTIONAL);
  120. if (chunk->nsg <= 0)
  121. goto fail;
  122. }
  123. return icm;
  124. fail:
  125. mthca_free_icm(dev, icm);
  126. return NULL;
  127. }
  128. int mthca_table_get(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
  129. {
  130. int i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
  131. int ret = 0;
  132. u8 status;
  133. mutex_lock(&table->mutex);
  134. if (table->icm[i]) {
  135. ++table->icm[i]->refcount;
  136. goto out;
  137. }
  138. table->icm[i] = mthca_alloc_icm(dev, MTHCA_TABLE_CHUNK_SIZE >> PAGE_SHIFT,
  139. (table->lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
  140. __GFP_NOWARN);
  141. if (!table->icm[i]) {
  142. ret = -ENOMEM;
  143. goto out;
  144. }
  145. if (mthca_MAP_ICM(dev, table->icm[i], table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
  146. &status) || status) {
  147. mthca_free_icm(dev, table->icm[i]);
  148. table->icm[i] = NULL;
  149. ret = -ENOMEM;
  150. goto out;
  151. }
  152. ++table->icm[i]->refcount;
  153. out:
  154. mutex_unlock(&table->mutex);
  155. return ret;
  156. }
  157. void mthca_table_put(struct mthca_dev *dev, struct mthca_icm_table *table, int obj)
  158. {
  159. int i;
  160. u8 status;
  161. if (!mthca_is_memfree(dev))
  162. return;
  163. i = (obj & (table->num_obj - 1)) * table->obj_size / MTHCA_TABLE_CHUNK_SIZE;
  164. mutex_lock(&table->mutex);
  165. if (--table->icm[i]->refcount == 0) {
  166. mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
  167. MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
  168. &status);
  169. mthca_free_icm(dev, table->icm[i]);
  170. table->icm[i] = NULL;
  171. }
  172. mutex_unlock(&table->mutex);
  173. }
  174. void *mthca_table_find(struct mthca_icm_table *table, int obj)
  175. {
  176. int idx, offset, i;
  177. struct mthca_icm_chunk *chunk;
  178. struct mthca_icm *icm;
  179. struct page *page = NULL;
  180. if (!table->lowmem)
  181. return NULL;
  182. mutex_lock(&table->mutex);
  183. idx = (obj & (table->num_obj - 1)) * table->obj_size;
  184. icm = table->icm[idx / MTHCA_TABLE_CHUNK_SIZE];
  185. offset = idx % MTHCA_TABLE_CHUNK_SIZE;
  186. if (!icm)
  187. goto out;
  188. list_for_each_entry(chunk, &icm->chunk_list, list) {
  189. for (i = 0; i < chunk->npages; ++i) {
  190. if (chunk->mem[i].length >= offset) {
  191. page = chunk->mem[i].page;
  192. goto out;
  193. }
  194. offset -= chunk->mem[i].length;
  195. }
  196. }
  197. out:
  198. mutex_unlock(&table->mutex);
  199. return page ? lowmem_page_address(page) + offset : NULL;
  200. }
  201. int mthca_table_get_range(struct mthca_dev *dev, struct mthca_icm_table *table,
  202. int start, int end)
  203. {
  204. int inc = MTHCA_TABLE_CHUNK_SIZE / table->obj_size;
  205. int i, err;
  206. for (i = start; i <= end; i += inc) {
  207. err = mthca_table_get(dev, table, i);
  208. if (err)
  209. goto fail;
  210. }
  211. return 0;
  212. fail:
  213. while (i > start) {
  214. i -= inc;
  215. mthca_table_put(dev, table, i);
  216. }
  217. return err;
  218. }
  219. void mthca_table_put_range(struct mthca_dev *dev, struct mthca_icm_table *table,
  220. int start, int end)
  221. {
  222. int i;
  223. if (!mthca_is_memfree(dev))
  224. return;
  225. for (i = start; i <= end; i += MTHCA_TABLE_CHUNK_SIZE / table->obj_size)
  226. mthca_table_put(dev, table, i);
  227. }
  228. struct mthca_icm_table *mthca_alloc_icm_table(struct mthca_dev *dev,
  229. u64 virt, int obj_size,
  230. int nobj, int reserved,
  231. int use_lowmem)
  232. {
  233. struct mthca_icm_table *table;
  234. int num_icm;
  235. unsigned chunk_size;
  236. int i;
  237. u8 status;
  238. num_icm = (obj_size * nobj + MTHCA_TABLE_CHUNK_SIZE - 1) / MTHCA_TABLE_CHUNK_SIZE;
  239. table = kmalloc(sizeof *table + num_icm * sizeof *table->icm, GFP_KERNEL);
  240. if (!table)
  241. return NULL;
  242. table->virt = virt;
  243. table->num_icm = num_icm;
  244. table->num_obj = nobj;
  245. table->obj_size = obj_size;
  246. table->lowmem = use_lowmem;
  247. mutex_init(&table->mutex);
  248. for (i = 0; i < num_icm; ++i)
  249. table->icm[i] = NULL;
  250. for (i = 0; i * MTHCA_TABLE_CHUNK_SIZE < reserved * obj_size; ++i) {
  251. chunk_size = MTHCA_TABLE_CHUNK_SIZE;
  252. if ((i + 1) * MTHCA_TABLE_CHUNK_SIZE > nobj * obj_size)
  253. chunk_size = nobj * obj_size - i * MTHCA_TABLE_CHUNK_SIZE;
  254. table->icm[i] = mthca_alloc_icm(dev, chunk_size >> PAGE_SHIFT,
  255. (use_lowmem ? GFP_KERNEL : GFP_HIGHUSER) |
  256. __GFP_NOWARN);
  257. if (!table->icm[i])
  258. goto err;
  259. if (mthca_MAP_ICM(dev, table->icm[i], virt + i * MTHCA_TABLE_CHUNK_SIZE,
  260. &status) || status) {
  261. mthca_free_icm(dev, table->icm[i]);
  262. table->icm[i] = NULL;
  263. goto err;
  264. }
  265. /*
  266. * Add a reference to this ICM chunk so that it never
  267. * gets freed (since it contains reserved firmware objects).
  268. */
  269. ++table->icm[i]->refcount;
  270. }
  271. return table;
  272. err:
  273. for (i = 0; i < num_icm; ++i)
  274. if (table->icm[i]) {
  275. mthca_UNMAP_ICM(dev, virt + i * MTHCA_TABLE_CHUNK_SIZE,
  276. MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
  277. &status);
  278. mthca_free_icm(dev, table->icm[i]);
  279. }
  280. kfree(table);
  281. return NULL;
  282. }
  283. void mthca_free_icm_table(struct mthca_dev *dev, struct mthca_icm_table *table)
  284. {
  285. int i;
  286. u8 status;
  287. for (i = 0; i < table->num_icm; ++i)
  288. if (table->icm[i]) {
  289. mthca_UNMAP_ICM(dev, table->virt + i * MTHCA_TABLE_CHUNK_SIZE,
  290. MTHCA_TABLE_CHUNK_SIZE / MTHCA_ICM_PAGE_SIZE,
  291. &status);
  292. mthca_free_icm(dev, table->icm[i]);
  293. }
  294. kfree(table);
  295. }
  296. static u64 mthca_uarc_virt(struct mthca_dev *dev, struct mthca_uar *uar, int page)
  297. {
  298. return dev->uar_table.uarc_base +
  299. uar->index * dev->uar_table.uarc_size +
  300. page * MTHCA_ICM_PAGE_SIZE;
  301. }
  302. int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
  303. struct mthca_user_db_table *db_tab, int index, u64 uaddr)
  304. {
  305. int ret = 0;
  306. u8 status;
  307. int i;
  308. if (!mthca_is_memfree(dev))
  309. return 0;
  310. if (index < 0 || index > dev->uar_table.uarc_size / 8)
  311. return -EINVAL;
  312. mutex_lock(&db_tab->mutex);
  313. i = index / MTHCA_DB_REC_PER_PAGE;
  314. if ((db_tab->page[i].refcount >= MTHCA_DB_REC_PER_PAGE) ||
  315. (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr) ||
  316. (uaddr & 4095)) {
  317. ret = -EINVAL;
  318. goto out;
  319. }
  320. if (db_tab->page[i].refcount) {
  321. ++db_tab->page[i].refcount;
  322. goto out;
  323. }
  324. ret = get_user_pages(current, current->mm, uaddr & PAGE_MASK, 1, 1, 0,
  325. &db_tab->page[i].mem.page, NULL);
  326. if (ret < 0)
  327. goto out;
  328. db_tab->page[i].mem.length = MTHCA_ICM_PAGE_SIZE;
  329. db_tab->page[i].mem.offset = uaddr & ~PAGE_MASK;
  330. ret = pci_map_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
  331. if (ret < 0) {
  332. put_page(db_tab->page[i].mem.page);
  333. goto out;
  334. }
  335. ret = mthca_MAP_ICM_page(dev, sg_dma_address(&db_tab->page[i].mem),
  336. mthca_uarc_virt(dev, uar, i), &status);
  337. if (!ret && status)
  338. ret = -EINVAL;
  339. if (ret) {
  340. pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
  341. put_page(db_tab->page[i].mem.page);
  342. goto out;
  343. }
  344. db_tab->page[i].uvirt = uaddr;
  345. db_tab->page[i].refcount = 1;
  346. out:
  347. mutex_unlock(&db_tab->mutex);
  348. return ret;
  349. }
  350. void mthca_unmap_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
  351. struct mthca_user_db_table *db_tab, int index)
  352. {
  353. if (!mthca_is_memfree(dev))
  354. return;
  355. /*
  356. * To make our bookkeeping simpler, we don't unmap DB
  357. * pages until we clean up the whole db table.
  358. */
  359. mutex_lock(&db_tab->mutex);
  360. --db_tab->page[index / MTHCA_DB_REC_PER_PAGE].refcount;
  361. mutex_unlock(&db_tab->mutex);
  362. }
  363. struct mthca_user_db_table *mthca_init_user_db_tab(struct mthca_dev *dev)
  364. {
  365. struct mthca_user_db_table *db_tab;
  366. int npages;
  367. int i;
  368. if (!mthca_is_memfree(dev))
  369. return NULL;
  370. npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
  371. db_tab = kmalloc(sizeof *db_tab + npages * sizeof *db_tab->page, GFP_KERNEL);
  372. if (!db_tab)
  373. return ERR_PTR(-ENOMEM);
  374. mutex_init(&db_tab->mutex);
  375. for (i = 0; i < npages; ++i) {
  376. db_tab->page[i].refcount = 0;
  377. db_tab->page[i].uvirt = 0;
  378. }
  379. return db_tab;
  380. }
  381. void mthca_cleanup_user_db_tab(struct mthca_dev *dev, struct mthca_uar *uar,
  382. struct mthca_user_db_table *db_tab)
  383. {
  384. int i;
  385. u8 status;
  386. if (!mthca_is_memfree(dev))
  387. return;
  388. for (i = 0; i < dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE; ++i) {
  389. if (db_tab->page[i].uvirt) {
  390. mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, uar, i), 1, &status);
  391. pci_unmap_sg(dev->pdev, &db_tab->page[i].mem, 1, PCI_DMA_TODEVICE);
  392. put_page(db_tab->page[i].mem.page);
  393. }
  394. }
  395. kfree(db_tab);
  396. }
  397. int mthca_alloc_db(struct mthca_dev *dev, enum mthca_db_type type,
  398. u32 qn, __be32 **db)
  399. {
  400. int group;
  401. int start, end, dir;
  402. int i, j;
  403. struct mthca_db_page *page;
  404. int ret = 0;
  405. u8 status;
  406. mutex_lock(&dev->db_tab->mutex);
  407. switch (type) {
  408. case MTHCA_DB_TYPE_CQ_ARM:
  409. case MTHCA_DB_TYPE_SQ:
  410. group = 0;
  411. start = 0;
  412. end = dev->db_tab->max_group1;
  413. dir = 1;
  414. break;
  415. case MTHCA_DB_TYPE_CQ_SET_CI:
  416. case MTHCA_DB_TYPE_RQ:
  417. case MTHCA_DB_TYPE_SRQ:
  418. group = 1;
  419. start = dev->db_tab->npages - 1;
  420. end = dev->db_tab->min_group2;
  421. dir = -1;
  422. break;
  423. default:
  424. ret = -EINVAL;
  425. goto out;
  426. }
  427. for (i = start; i != end; i += dir)
  428. if (dev->db_tab->page[i].db_rec &&
  429. !bitmap_full(dev->db_tab->page[i].used,
  430. MTHCA_DB_REC_PER_PAGE)) {
  431. page = dev->db_tab->page + i;
  432. goto found;
  433. }
  434. for (i = start; i != end; i += dir)
  435. if (!dev->db_tab->page[i].db_rec) {
  436. page = dev->db_tab->page + i;
  437. goto alloc;
  438. }
  439. if (dev->db_tab->max_group1 >= dev->db_tab->min_group2 - 1) {
  440. ret = -ENOMEM;
  441. goto out;
  442. }
  443. if (group == 0)
  444. ++dev->db_tab->max_group1;
  445. else
  446. --dev->db_tab->min_group2;
  447. page = dev->db_tab->page + end;
  448. alloc:
  449. page->db_rec = dma_alloc_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
  450. &page->mapping, GFP_KERNEL);
  451. if (!page->db_rec) {
  452. ret = -ENOMEM;
  453. goto out;
  454. }
  455. memset(page->db_rec, 0, MTHCA_ICM_PAGE_SIZE);
  456. ret = mthca_MAP_ICM_page(dev, page->mapping,
  457. mthca_uarc_virt(dev, &dev->driver_uar, i), &status);
  458. if (!ret && status)
  459. ret = -EINVAL;
  460. if (ret) {
  461. dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
  462. page->db_rec, page->mapping);
  463. goto out;
  464. }
  465. bitmap_zero(page->used, MTHCA_DB_REC_PER_PAGE);
  466. found:
  467. j = find_first_zero_bit(page->used, MTHCA_DB_REC_PER_PAGE);
  468. set_bit(j, page->used);
  469. if (group == 1)
  470. j = MTHCA_DB_REC_PER_PAGE - 1 - j;
  471. ret = i * MTHCA_DB_REC_PER_PAGE + j;
  472. page->db_rec[j] = cpu_to_be64((qn << 8) | (type << 5));
  473. *db = (__be32 *) &page->db_rec[j];
  474. out:
  475. mutex_unlock(&dev->db_tab->mutex);
  476. return ret;
  477. }
  478. void mthca_free_db(struct mthca_dev *dev, int type, int db_index)
  479. {
  480. int i, j;
  481. struct mthca_db_page *page;
  482. u8 status;
  483. i = db_index / MTHCA_DB_REC_PER_PAGE;
  484. j = db_index % MTHCA_DB_REC_PER_PAGE;
  485. page = dev->db_tab->page + i;
  486. mutex_lock(&dev->db_tab->mutex);
  487. page->db_rec[j] = 0;
  488. if (i >= dev->db_tab->min_group2)
  489. j = MTHCA_DB_REC_PER_PAGE - 1 - j;
  490. clear_bit(j, page->used);
  491. if (bitmap_empty(page->used, MTHCA_DB_REC_PER_PAGE) &&
  492. i >= dev->db_tab->max_group1 - 1) {
  493. mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
  494. dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
  495. page->db_rec, page->mapping);
  496. page->db_rec = NULL;
  497. if (i == dev->db_tab->max_group1) {
  498. --dev->db_tab->max_group1;
  499. /* XXX may be able to unmap more pages now */
  500. }
  501. if (i == dev->db_tab->min_group2)
  502. ++dev->db_tab->min_group2;
  503. }
  504. mutex_unlock(&dev->db_tab->mutex);
  505. }
  506. int mthca_init_db_tab(struct mthca_dev *dev)
  507. {
  508. int i;
  509. if (!mthca_is_memfree(dev))
  510. return 0;
  511. dev->db_tab = kmalloc(sizeof *dev->db_tab, GFP_KERNEL);
  512. if (!dev->db_tab)
  513. return -ENOMEM;
  514. mutex_init(&dev->db_tab->mutex);
  515. dev->db_tab->npages = dev->uar_table.uarc_size / MTHCA_ICM_PAGE_SIZE;
  516. dev->db_tab->max_group1 = 0;
  517. dev->db_tab->min_group2 = dev->db_tab->npages - 1;
  518. dev->db_tab->page = kmalloc(dev->db_tab->npages *
  519. sizeof *dev->db_tab->page,
  520. GFP_KERNEL);
  521. if (!dev->db_tab->page) {
  522. kfree(dev->db_tab);
  523. return -ENOMEM;
  524. }
  525. for (i = 0; i < dev->db_tab->npages; ++i)
  526. dev->db_tab->page[i].db_rec = NULL;
  527. return 0;
  528. }
  529. void mthca_cleanup_db_tab(struct mthca_dev *dev)
  530. {
  531. int i;
  532. u8 status;
  533. if (!mthca_is_memfree(dev))
  534. return;
  535. /*
  536. * Because we don't always free our UARC pages when they
  537. * become empty to make mthca_free_db() simpler we need to
  538. * make a sweep through the doorbell pages and free any
  539. * leftover pages now.
  540. */
  541. for (i = 0; i < dev->db_tab->npages; ++i) {
  542. if (!dev->db_tab->page[i].db_rec)
  543. continue;
  544. if (!bitmap_empty(dev->db_tab->page[i].used, MTHCA_DB_REC_PER_PAGE))
  545. mthca_warn(dev, "Kernel UARC page %d not empty\n", i);
  546. mthca_UNMAP_ICM(dev, mthca_uarc_virt(dev, &dev->driver_uar, i), 1, &status);
  547. dma_free_coherent(&dev->pdev->dev, MTHCA_ICM_PAGE_SIZE,
  548. dev->db_tab->page[i].db_rec,
  549. dev->db_tab->page[i].mapping);
  550. }
  551. kfree(dev->db_tab->page);
  552. kfree(dev->db_tab);
  553. }