alloc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/errno.h>
  34. #include <linux/slab.h>
  35. #include <linux/mm.h>
  36. #include <linux/export.h>
  37. #include <linux/bitmap.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/vmalloc.h>
  40. #include "mlx4.h"
  41. u32 mlx4_bitmap_alloc(struct mlx4_bitmap *bitmap)
  42. {
  43. u32 obj;
  44. spin_lock(&bitmap->lock);
  45. obj = find_next_zero_bit(bitmap->table, bitmap->max, bitmap->last);
  46. if (obj >= bitmap->max) {
  47. bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
  48. & bitmap->mask;
  49. obj = find_first_zero_bit(bitmap->table, bitmap->max);
  50. }
  51. if (obj < bitmap->max) {
  52. set_bit(obj, bitmap->table);
  53. bitmap->last = (obj + 1);
  54. if (bitmap->last == bitmap->max)
  55. bitmap->last = 0;
  56. obj |= bitmap->top;
  57. } else
  58. obj = -1;
  59. if (obj != -1)
  60. --bitmap->avail;
  61. spin_unlock(&bitmap->lock);
  62. return obj;
  63. }
  64. void mlx4_bitmap_free(struct mlx4_bitmap *bitmap, u32 obj, int use_rr)
  65. {
  66. mlx4_bitmap_free_range(bitmap, obj, 1, use_rr);
  67. }
  68. static unsigned long find_aligned_range(unsigned long *bitmap,
  69. u32 start, u32 nbits,
  70. int len, int align, u32 skip_mask)
  71. {
  72. unsigned long end, i;
  73. again:
  74. start = ALIGN(start, align);
  75. while ((start < nbits) && (test_bit(start, bitmap) ||
  76. (start & skip_mask)))
  77. start += align;
  78. if (start >= nbits)
  79. return -1;
  80. end = start+len;
  81. if (end > nbits)
  82. return -1;
  83. for (i = start + 1; i < end; i++) {
  84. if (test_bit(i, bitmap) || ((u32)i & skip_mask)) {
  85. start = i + 1;
  86. goto again;
  87. }
  88. }
  89. return start;
  90. }
  91. u32 mlx4_bitmap_alloc_range(struct mlx4_bitmap *bitmap, int cnt,
  92. int align, u32 skip_mask)
  93. {
  94. u32 obj;
  95. if (likely(cnt == 1 && align == 1 && !skip_mask))
  96. return mlx4_bitmap_alloc(bitmap);
  97. spin_lock(&bitmap->lock);
  98. obj = find_aligned_range(bitmap->table, bitmap->last,
  99. bitmap->max, cnt, align, skip_mask);
  100. if (obj >= bitmap->max) {
  101. bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
  102. & bitmap->mask;
  103. obj = find_aligned_range(bitmap->table, 0, bitmap->max,
  104. cnt, align, skip_mask);
  105. }
  106. if (obj < bitmap->max) {
  107. bitmap_set(bitmap->table, obj, cnt);
  108. if (obj == bitmap->last) {
  109. bitmap->last = (obj + cnt);
  110. if (bitmap->last >= bitmap->max)
  111. bitmap->last = 0;
  112. }
  113. obj |= bitmap->top;
  114. } else
  115. obj = -1;
  116. if (obj != -1)
  117. bitmap->avail -= cnt;
  118. spin_unlock(&bitmap->lock);
  119. return obj;
  120. }
  121. u32 mlx4_bitmap_avail(struct mlx4_bitmap *bitmap)
  122. {
  123. return bitmap->avail;
  124. }
  125. static u32 mlx4_bitmap_masked_value(struct mlx4_bitmap *bitmap, u32 obj)
  126. {
  127. return obj & (bitmap->max + bitmap->reserved_top - 1);
  128. }
  129. void mlx4_bitmap_free_range(struct mlx4_bitmap *bitmap, u32 obj, int cnt,
  130. int use_rr)
  131. {
  132. obj &= bitmap->max + bitmap->reserved_top - 1;
  133. spin_lock(&bitmap->lock);
  134. if (!use_rr) {
  135. bitmap->last = min(bitmap->last, obj);
  136. bitmap->top = (bitmap->top + bitmap->max + bitmap->reserved_top)
  137. & bitmap->mask;
  138. }
  139. bitmap_clear(bitmap->table, obj, cnt);
  140. bitmap->avail += cnt;
  141. spin_unlock(&bitmap->lock);
  142. }
  143. int mlx4_bitmap_init(struct mlx4_bitmap *bitmap, u32 num, u32 mask,
  144. u32 reserved_bot, u32 reserved_top)
  145. {
  146. /* num must be a power of 2 */
  147. if (num != roundup_pow_of_two(num))
  148. return -EINVAL;
  149. bitmap->last = 0;
  150. bitmap->top = 0;
  151. bitmap->max = num - reserved_top;
  152. bitmap->mask = mask;
  153. bitmap->reserved_top = reserved_top;
  154. bitmap->avail = num - reserved_top - reserved_bot;
  155. bitmap->effective_len = bitmap->avail;
  156. spin_lock_init(&bitmap->lock);
  157. bitmap->table = bitmap_zalloc(bitmap->max, GFP_KERNEL);
  158. if (!bitmap->table)
  159. return -ENOMEM;
  160. bitmap_set(bitmap->table, 0, reserved_bot);
  161. return 0;
  162. }
  163. void mlx4_bitmap_cleanup(struct mlx4_bitmap *bitmap)
  164. {
  165. bitmap_free(bitmap->table);
  166. }
  167. struct mlx4_zone_allocator {
  168. struct list_head entries;
  169. struct list_head prios;
  170. u32 last_uid;
  171. u32 mask;
  172. /* protect the zone_allocator from concurrent accesses */
  173. spinlock_t lock;
  174. enum mlx4_zone_alloc_flags flags;
  175. };
  176. struct mlx4_zone_entry {
  177. struct list_head list;
  178. struct list_head prio_list;
  179. u32 uid;
  180. struct mlx4_zone_allocator *allocator;
  181. struct mlx4_bitmap *bitmap;
  182. int use_rr;
  183. int priority;
  184. int offset;
  185. enum mlx4_zone_flags flags;
  186. };
  187. struct mlx4_zone_allocator *mlx4_zone_allocator_create(enum mlx4_zone_alloc_flags flags)
  188. {
  189. struct mlx4_zone_allocator *zones = kmalloc(sizeof(*zones), GFP_KERNEL);
  190. if (NULL == zones)
  191. return NULL;
  192. INIT_LIST_HEAD(&zones->entries);
  193. INIT_LIST_HEAD(&zones->prios);
  194. spin_lock_init(&zones->lock);
  195. zones->last_uid = 0;
  196. zones->mask = 0;
  197. zones->flags = flags;
  198. return zones;
  199. }
  200. int mlx4_zone_add_one(struct mlx4_zone_allocator *zone_alloc,
  201. struct mlx4_bitmap *bitmap,
  202. u32 flags,
  203. int priority,
  204. int offset,
  205. u32 *puid)
  206. {
  207. u32 mask = mlx4_bitmap_masked_value(bitmap, (u32)-1);
  208. struct mlx4_zone_entry *it;
  209. struct mlx4_zone_entry *zone = kmalloc(sizeof(*zone), GFP_KERNEL);
  210. if (NULL == zone)
  211. return -ENOMEM;
  212. zone->flags = flags;
  213. zone->bitmap = bitmap;
  214. zone->use_rr = (flags & MLX4_ZONE_USE_RR) ? MLX4_USE_RR : 0;
  215. zone->priority = priority;
  216. zone->offset = offset;
  217. spin_lock(&zone_alloc->lock);
  218. zone->uid = zone_alloc->last_uid++;
  219. zone->allocator = zone_alloc;
  220. if (zone_alloc->mask < mask)
  221. zone_alloc->mask = mask;
  222. list_for_each_entry(it, &zone_alloc->prios, prio_list)
  223. if (it->priority >= priority)
  224. break;
  225. if (&it->prio_list == &zone_alloc->prios || it->priority > priority)
  226. list_add_tail(&zone->prio_list, &it->prio_list);
  227. list_add_tail(&zone->list, &it->list);
  228. spin_unlock(&zone_alloc->lock);
  229. *puid = zone->uid;
  230. return 0;
  231. }
  232. /* Should be called under a lock */
  233. static void __mlx4_zone_remove_one_entry(struct mlx4_zone_entry *entry)
  234. {
  235. struct mlx4_zone_allocator *zone_alloc = entry->allocator;
  236. if (!list_empty(&entry->prio_list)) {
  237. /* Check if we need to add an alternative node to the prio list */
  238. if (!list_is_last(&entry->list, &zone_alloc->entries)) {
  239. struct mlx4_zone_entry *next = list_first_entry(&entry->list,
  240. typeof(*next),
  241. list);
  242. if (next->priority == entry->priority)
  243. list_add_tail(&next->prio_list, &entry->prio_list);
  244. }
  245. list_del(&entry->prio_list);
  246. }
  247. list_del(&entry->list);
  248. if (zone_alloc->flags & MLX4_ZONE_ALLOC_FLAGS_NO_OVERLAP) {
  249. u32 mask = 0;
  250. struct mlx4_zone_entry *it;
  251. list_for_each_entry(it, &zone_alloc->prios, prio_list) {
  252. u32 cur_mask = mlx4_bitmap_masked_value(it->bitmap, (u32)-1);
  253. if (mask < cur_mask)
  254. mask = cur_mask;
  255. }
  256. zone_alloc->mask = mask;
  257. }
  258. }
  259. void mlx4_zone_allocator_destroy(struct mlx4_zone_allocator *zone_alloc)
  260. {
  261. struct mlx4_zone_entry *zone, *tmp;
  262. spin_lock(&zone_alloc->lock);
  263. list_for_each_entry_safe(zone, tmp, &zone_alloc->entries, list) {
  264. list_del(&zone->list);
  265. list_del(&zone->prio_list);
  266. kfree(zone);
  267. }
  268. spin_unlock(&zone_alloc->lock);
  269. kfree(zone_alloc);
  270. }
  271. /* Should be called under a lock */
  272. static u32 __mlx4_alloc_from_zone(struct mlx4_zone_entry *zone, int count,
  273. int align, u32 skip_mask, u32 *puid)
  274. {
  275. u32 uid = 0;
  276. u32 res;
  277. struct mlx4_zone_allocator *zone_alloc = zone->allocator;
  278. struct mlx4_zone_entry *curr_node;
  279. res = mlx4_bitmap_alloc_range(zone->bitmap, count,
  280. align, skip_mask);
  281. if (res != (u32)-1) {
  282. res += zone->offset;
  283. uid = zone->uid;
  284. goto out;
  285. }
  286. list_for_each_entry(curr_node, &zone_alloc->prios, prio_list) {
  287. if (unlikely(curr_node->priority == zone->priority))
  288. break;
  289. }
  290. if (zone->flags & MLX4_ZONE_ALLOW_ALLOC_FROM_LOWER_PRIO) {
  291. struct mlx4_zone_entry *it = curr_node;
  292. list_for_each_entry_continue_reverse(it, &zone_alloc->entries, list) {
  293. res = mlx4_bitmap_alloc_range(it->bitmap, count,
  294. align, skip_mask);
  295. if (res != (u32)-1) {
  296. res += it->offset;
  297. uid = it->uid;
  298. goto out;
  299. }
  300. }
  301. }
  302. if (zone->flags & MLX4_ZONE_ALLOW_ALLOC_FROM_EQ_PRIO) {
  303. struct mlx4_zone_entry *it = curr_node;
  304. list_for_each_entry_from(it, &zone_alloc->entries, list) {
  305. if (unlikely(it == zone))
  306. continue;
  307. if (unlikely(it->priority != curr_node->priority))
  308. break;
  309. res = mlx4_bitmap_alloc_range(it->bitmap, count,
  310. align, skip_mask);
  311. if (res != (u32)-1) {
  312. res += it->offset;
  313. uid = it->uid;
  314. goto out;
  315. }
  316. }
  317. }
  318. if (zone->flags & MLX4_ZONE_FALLBACK_TO_HIGHER_PRIO) {
  319. if (list_is_last(&curr_node->prio_list, &zone_alloc->prios))
  320. goto out;
  321. curr_node = list_first_entry(&curr_node->prio_list,
  322. typeof(*curr_node),
  323. prio_list);
  324. list_for_each_entry_from(curr_node, &zone_alloc->entries, list) {
  325. res = mlx4_bitmap_alloc_range(curr_node->bitmap, count,
  326. align, skip_mask);
  327. if (res != (u32)-1) {
  328. res += curr_node->offset;
  329. uid = curr_node->uid;
  330. goto out;
  331. }
  332. }
  333. }
  334. out:
  335. if (NULL != puid && res != (u32)-1)
  336. *puid = uid;
  337. return res;
  338. }
  339. /* Should be called under a lock */
  340. static void __mlx4_free_from_zone(struct mlx4_zone_entry *zone, u32 obj,
  341. u32 count)
  342. {
  343. mlx4_bitmap_free_range(zone->bitmap, obj - zone->offset, count, zone->use_rr);
  344. }
  345. /* Should be called under a lock */
  346. static struct mlx4_zone_entry *__mlx4_find_zone_by_uid(
  347. struct mlx4_zone_allocator *zones, u32 uid)
  348. {
  349. struct mlx4_zone_entry *zone;
  350. list_for_each_entry(zone, &zones->entries, list) {
  351. if (zone->uid == uid)
  352. return zone;
  353. }
  354. return NULL;
  355. }
  356. struct mlx4_bitmap *mlx4_zone_get_bitmap(struct mlx4_zone_allocator *zones, u32 uid)
  357. {
  358. struct mlx4_zone_entry *zone;
  359. struct mlx4_bitmap *bitmap;
  360. spin_lock(&zones->lock);
  361. zone = __mlx4_find_zone_by_uid(zones, uid);
  362. bitmap = zone == NULL ? NULL : zone->bitmap;
  363. spin_unlock(&zones->lock);
  364. return bitmap;
  365. }
  366. int mlx4_zone_remove_one(struct mlx4_zone_allocator *zones, u32 uid)
  367. {
  368. struct mlx4_zone_entry *zone;
  369. int res = 0;
  370. spin_lock(&zones->lock);
  371. zone = __mlx4_find_zone_by_uid(zones, uid);
  372. if (NULL == zone) {
  373. res = -1;
  374. goto out;
  375. }
  376. __mlx4_zone_remove_one_entry(zone);
  377. out:
  378. spin_unlock(&zones->lock);
  379. kfree(zone);
  380. return res;
  381. }
  382. /* Should be called under a lock */
  383. static struct mlx4_zone_entry *__mlx4_find_zone_by_uid_unique(
  384. struct mlx4_zone_allocator *zones, u32 obj)
  385. {
  386. struct mlx4_zone_entry *zone, *zone_candidate = NULL;
  387. u32 dist = (u32)-1;
  388. /* Search for the smallest zone that this obj could be
  389. * allocated from. This is done in order to handle
  390. * situations when small bitmaps are allocated from bigger
  391. * bitmaps (and the allocated space is marked as reserved in
  392. * the bigger bitmap.
  393. */
  394. list_for_each_entry(zone, &zones->entries, list) {
  395. if (obj >= zone->offset) {
  396. u32 mobj = (obj - zone->offset) & zones->mask;
  397. if (mobj < zone->bitmap->max) {
  398. u32 curr_dist = zone->bitmap->effective_len;
  399. if (curr_dist < dist) {
  400. dist = curr_dist;
  401. zone_candidate = zone;
  402. }
  403. }
  404. }
  405. }
  406. return zone_candidate;
  407. }
  408. u32 mlx4_zone_alloc_entries(struct mlx4_zone_allocator *zones, u32 uid, int count,
  409. int align, u32 skip_mask, u32 *puid)
  410. {
  411. struct mlx4_zone_entry *zone;
  412. int res = -1;
  413. spin_lock(&zones->lock);
  414. zone = __mlx4_find_zone_by_uid(zones, uid);
  415. if (NULL == zone)
  416. goto out;
  417. res = __mlx4_alloc_from_zone(zone, count, align, skip_mask, puid);
  418. out:
  419. spin_unlock(&zones->lock);
  420. return res;
  421. }
  422. u32 mlx4_zone_free_entries(struct mlx4_zone_allocator *zones, u32 uid, u32 obj, u32 count)
  423. {
  424. struct mlx4_zone_entry *zone;
  425. int res = 0;
  426. spin_lock(&zones->lock);
  427. zone = __mlx4_find_zone_by_uid(zones, uid);
  428. if (NULL == zone) {
  429. res = -1;
  430. goto out;
  431. }
  432. __mlx4_free_from_zone(zone, obj, count);
  433. out:
  434. spin_unlock(&zones->lock);
  435. return res;
  436. }
  437. u32 mlx4_zone_free_entries_unique(struct mlx4_zone_allocator *zones, u32 obj, u32 count)
  438. {
  439. struct mlx4_zone_entry *zone;
  440. int res;
  441. if (!(zones->flags & MLX4_ZONE_ALLOC_FLAGS_NO_OVERLAP))
  442. return -EFAULT;
  443. spin_lock(&zones->lock);
  444. zone = __mlx4_find_zone_by_uid_unique(zones, obj);
  445. if (NULL == zone) {
  446. res = -1;
  447. goto out;
  448. }
  449. __mlx4_free_from_zone(zone, obj, count);
  450. res = 0;
  451. out:
  452. spin_unlock(&zones->lock);
  453. return res;
  454. }
  455. static int mlx4_buf_direct_alloc(struct mlx4_dev *dev, int size,
  456. struct mlx4_buf *buf)
  457. {
  458. dma_addr_t t;
  459. buf->nbufs = 1;
  460. buf->npages = 1;
  461. buf->page_shift = get_order(size) + PAGE_SHIFT;
  462. buf->direct.buf =
  463. dma_alloc_coherent(&dev->persist->pdev->dev, size, &t,
  464. GFP_KERNEL);
  465. if (!buf->direct.buf)
  466. return -ENOMEM;
  467. buf->direct.map = t;
  468. while (t & ((1 << buf->page_shift) - 1)) {
  469. --buf->page_shift;
  470. buf->npages *= 2;
  471. }
  472. return 0;
  473. }
  474. /* Handling for queue buffers -- we allocate a bunch of memory and
  475. * register it in a memory region at HCA virtual address 0. If the
  476. * requested size is > max_direct, we split the allocation into
  477. * multiple pages, so we don't require too much contiguous memory.
  478. */
  479. int mlx4_buf_alloc(struct mlx4_dev *dev, int size, int max_direct,
  480. struct mlx4_buf *buf)
  481. {
  482. if (size <= max_direct) {
  483. return mlx4_buf_direct_alloc(dev, size, buf);
  484. } else {
  485. dma_addr_t t;
  486. int i;
  487. buf->direct.buf = NULL;
  488. buf->nbufs = DIV_ROUND_UP(size, PAGE_SIZE);
  489. buf->npages = buf->nbufs;
  490. buf->page_shift = PAGE_SHIFT;
  491. buf->page_list = kcalloc(buf->nbufs, sizeof(*buf->page_list),
  492. GFP_KERNEL);
  493. if (!buf->page_list)
  494. return -ENOMEM;
  495. for (i = 0; i < buf->nbufs; ++i) {
  496. buf->page_list[i].buf =
  497. dma_alloc_coherent(&dev->persist->pdev->dev,
  498. PAGE_SIZE, &t, GFP_KERNEL);
  499. if (!buf->page_list[i].buf)
  500. goto err_free;
  501. buf->page_list[i].map = t;
  502. }
  503. }
  504. return 0;
  505. err_free:
  506. mlx4_buf_free(dev, size, buf);
  507. return -ENOMEM;
  508. }
  509. EXPORT_SYMBOL_GPL(mlx4_buf_alloc);
  510. void mlx4_buf_free(struct mlx4_dev *dev, int size, struct mlx4_buf *buf)
  511. {
  512. if (buf->nbufs == 1) {
  513. dma_free_coherent(&dev->persist->pdev->dev, size,
  514. buf->direct.buf, buf->direct.map);
  515. } else {
  516. int i;
  517. for (i = 0; i < buf->nbufs; ++i)
  518. if (buf->page_list[i].buf)
  519. dma_free_coherent(&dev->persist->pdev->dev,
  520. PAGE_SIZE,
  521. buf->page_list[i].buf,
  522. buf->page_list[i].map);
  523. kfree(buf->page_list);
  524. }
  525. }
  526. EXPORT_SYMBOL_GPL(mlx4_buf_free);
  527. static struct mlx4_db_pgdir *mlx4_alloc_db_pgdir(struct device *dma_device)
  528. {
  529. struct mlx4_db_pgdir *pgdir;
  530. pgdir = kzalloc(sizeof(*pgdir), GFP_KERNEL);
  531. if (!pgdir)
  532. return NULL;
  533. bitmap_fill(pgdir->order1, MLX4_DB_PER_PAGE / 2);
  534. pgdir->bits[0] = pgdir->order0;
  535. pgdir->bits[1] = pgdir->order1;
  536. pgdir->db_page = dma_alloc_coherent(dma_device, PAGE_SIZE,
  537. &pgdir->db_dma, GFP_KERNEL);
  538. if (!pgdir->db_page) {
  539. kfree(pgdir);
  540. return NULL;
  541. }
  542. return pgdir;
  543. }
  544. static int mlx4_alloc_db_from_pgdir(struct mlx4_db_pgdir *pgdir,
  545. struct mlx4_db *db, int order)
  546. {
  547. int o;
  548. int i;
  549. for (o = order; o <= 1; ++o) {
  550. i = find_first_bit(pgdir->bits[o], MLX4_DB_PER_PAGE >> o);
  551. if (i < MLX4_DB_PER_PAGE >> o)
  552. goto found;
  553. }
  554. return -ENOMEM;
  555. found:
  556. clear_bit(i, pgdir->bits[o]);
  557. i <<= o;
  558. if (o > order)
  559. set_bit(i ^ 1, pgdir->bits[order]);
  560. db->u.pgdir = pgdir;
  561. db->index = i;
  562. db->db = pgdir->db_page + db->index;
  563. db->dma = pgdir->db_dma + db->index * 4;
  564. db->order = order;
  565. return 0;
  566. }
  567. int mlx4_db_alloc(struct mlx4_dev *dev, struct mlx4_db *db, int order)
  568. {
  569. struct mlx4_priv *priv = mlx4_priv(dev);
  570. struct mlx4_db_pgdir *pgdir;
  571. int ret = 0;
  572. mutex_lock(&priv->pgdir_mutex);
  573. list_for_each_entry(pgdir, &priv->pgdir_list, list)
  574. if (!mlx4_alloc_db_from_pgdir(pgdir, db, order))
  575. goto out;
  576. pgdir = mlx4_alloc_db_pgdir(&dev->persist->pdev->dev);
  577. if (!pgdir) {
  578. ret = -ENOMEM;
  579. goto out;
  580. }
  581. list_add(&pgdir->list, &priv->pgdir_list);
  582. /* This should never fail -- we just allocated an empty page: */
  583. WARN_ON(mlx4_alloc_db_from_pgdir(pgdir, db, order));
  584. out:
  585. mutex_unlock(&priv->pgdir_mutex);
  586. return ret;
  587. }
  588. EXPORT_SYMBOL_GPL(mlx4_db_alloc);
  589. void mlx4_db_free(struct mlx4_dev *dev, struct mlx4_db *db)
  590. {
  591. struct mlx4_priv *priv = mlx4_priv(dev);
  592. int o;
  593. int i;
  594. mutex_lock(&priv->pgdir_mutex);
  595. o = db->order;
  596. i = db->index;
  597. if (db->order == 0 && test_bit(i ^ 1, db->u.pgdir->order0)) {
  598. clear_bit(i ^ 1, db->u.pgdir->order0);
  599. ++o;
  600. }
  601. i >>= o;
  602. set_bit(i, db->u.pgdir->bits[o]);
  603. if (bitmap_full(db->u.pgdir->order1, MLX4_DB_PER_PAGE / 2)) {
  604. dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
  605. db->u.pgdir->db_page, db->u.pgdir->db_dma);
  606. list_del(&db->u.pgdir->list);
  607. kfree(db->u.pgdir);
  608. }
  609. mutex_unlock(&priv->pgdir_mutex);
  610. }
  611. EXPORT_SYMBOL_GPL(mlx4_db_free);
  612. int mlx4_alloc_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
  613. int size)
  614. {
  615. int err;
  616. err = mlx4_db_alloc(dev, &wqres->db, 1);
  617. if (err)
  618. return err;
  619. *wqres->db.db = 0;
  620. err = mlx4_buf_direct_alloc(dev, size, &wqres->buf);
  621. if (err)
  622. goto err_db;
  623. err = mlx4_mtt_init(dev, wqres->buf.npages, wqres->buf.page_shift,
  624. &wqres->mtt);
  625. if (err)
  626. goto err_buf;
  627. err = mlx4_buf_write_mtt(dev, &wqres->mtt, &wqres->buf);
  628. if (err)
  629. goto err_mtt;
  630. return 0;
  631. err_mtt:
  632. mlx4_mtt_cleanup(dev, &wqres->mtt);
  633. err_buf:
  634. mlx4_buf_free(dev, size, &wqres->buf);
  635. err_db:
  636. mlx4_db_free(dev, &wqres->db);
  637. return err;
  638. }
  639. EXPORT_SYMBOL_GPL(mlx4_alloc_hwq_res);
  640. void mlx4_free_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
  641. int size)
  642. {
  643. mlx4_mtt_cleanup(dev, &wqres->mtt);
  644. mlx4_buf_free(dev, size, &wqres->buf);
  645. mlx4_db_free(dev, &wqres->db);
  646. }
  647. EXPORT_SYMBOL_GPL(mlx4_free_hwq_res);