alloc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 = kzalloc(BITS_TO_LONGS(bitmap->max) *
  158. sizeof (long), GFP_KERNEL);
  159. if (!bitmap->table)
  160. return -ENOMEM;
  161. bitmap_set(bitmap->table, 0, reserved_bot);
  162. return 0;
  163. }
  164. void mlx4_bitmap_cleanup(struct mlx4_bitmap *bitmap)
  165. {
  166. kfree(bitmap->table);
  167. }
  168. struct mlx4_zone_allocator {
  169. struct list_head entries;
  170. struct list_head prios;
  171. u32 last_uid;
  172. u32 mask;
  173. /* protect the zone_allocator from concurrent accesses */
  174. spinlock_t lock;
  175. enum mlx4_zone_alloc_flags flags;
  176. };
  177. struct mlx4_zone_entry {
  178. struct list_head list;
  179. struct list_head prio_list;
  180. u32 uid;
  181. struct mlx4_zone_allocator *allocator;
  182. struct mlx4_bitmap *bitmap;
  183. int use_rr;
  184. int priority;
  185. int offset;
  186. enum mlx4_zone_flags flags;
  187. };
  188. struct mlx4_zone_allocator *mlx4_zone_allocator_create(enum mlx4_zone_alloc_flags flags)
  189. {
  190. struct mlx4_zone_allocator *zones = kmalloc(sizeof(*zones), GFP_KERNEL);
  191. if (NULL == zones)
  192. return NULL;
  193. INIT_LIST_HEAD(&zones->entries);
  194. INIT_LIST_HEAD(&zones->prios);
  195. spin_lock_init(&zones->lock);
  196. zones->last_uid = 0;
  197. zones->mask = 0;
  198. zones->flags = flags;
  199. return zones;
  200. }
  201. int mlx4_zone_add_one(struct mlx4_zone_allocator *zone_alloc,
  202. struct mlx4_bitmap *bitmap,
  203. u32 flags,
  204. int priority,
  205. int offset,
  206. u32 *puid)
  207. {
  208. u32 mask = mlx4_bitmap_masked_value(bitmap, (u32)-1);
  209. struct mlx4_zone_entry *it;
  210. struct mlx4_zone_entry *zone = kmalloc(sizeof(*zone), GFP_KERNEL);
  211. if (NULL == zone)
  212. return -ENOMEM;
  213. zone->flags = flags;
  214. zone->bitmap = bitmap;
  215. zone->use_rr = (flags & MLX4_ZONE_USE_RR) ? MLX4_USE_RR : 0;
  216. zone->priority = priority;
  217. zone->offset = offset;
  218. spin_lock(&zone_alloc->lock);
  219. zone->uid = zone_alloc->last_uid++;
  220. zone->allocator = zone_alloc;
  221. if (zone_alloc->mask < mask)
  222. zone_alloc->mask = mask;
  223. list_for_each_entry(it, &zone_alloc->prios, prio_list)
  224. if (it->priority >= priority)
  225. break;
  226. if (&it->prio_list == &zone_alloc->prios || it->priority > priority)
  227. list_add_tail(&zone->prio_list, &it->prio_list);
  228. list_add_tail(&zone->list, &it->list);
  229. spin_unlock(&zone_alloc->lock);
  230. *puid = zone->uid;
  231. return 0;
  232. }
  233. /* Should be called under a lock */
  234. static int __mlx4_zone_remove_one_entry(struct mlx4_zone_entry *entry)
  235. {
  236. struct mlx4_zone_allocator *zone_alloc = entry->allocator;
  237. if (!list_empty(&entry->prio_list)) {
  238. /* Check if we need to add an alternative node to the prio list */
  239. if (!list_is_last(&entry->list, &zone_alloc->entries)) {
  240. struct mlx4_zone_entry *next = list_first_entry(&entry->list,
  241. typeof(*next),
  242. list);
  243. if (next->priority == entry->priority)
  244. list_add_tail(&next->prio_list, &entry->prio_list);
  245. }
  246. list_del(&entry->prio_list);
  247. }
  248. list_del(&entry->list);
  249. if (zone_alloc->flags & MLX4_ZONE_ALLOC_FLAGS_NO_OVERLAP) {
  250. u32 mask = 0;
  251. struct mlx4_zone_entry *it;
  252. list_for_each_entry(it, &zone_alloc->prios, prio_list) {
  253. u32 cur_mask = mlx4_bitmap_masked_value(it->bitmap, (u32)-1);
  254. if (mask < cur_mask)
  255. mask = cur_mask;
  256. }
  257. zone_alloc->mask = mask;
  258. }
  259. return 0;
  260. }
  261. void mlx4_zone_allocator_destroy(struct mlx4_zone_allocator *zone_alloc)
  262. {
  263. struct mlx4_zone_entry *zone, *tmp;
  264. spin_lock(&zone_alloc->lock);
  265. list_for_each_entry_safe(zone, tmp, &zone_alloc->entries, list) {
  266. list_del(&zone->list);
  267. list_del(&zone->prio_list);
  268. kfree(zone);
  269. }
  270. spin_unlock(&zone_alloc->lock);
  271. kfree(zone_alloc);
  272. }
  273. /* Should be called under a lock */
  274. static u32 __mlx4_alloc_from_zone(struct mlx4_zone_entry *zone, int count,
  275. int align, u32 skip_mask, u32 *puid)
  276. {
  277. u32 uid;
  278. u32 res;
  279. struct mlx4_zone_allocator *zone_alloc = zone->allocator;
  280. struct mlx4_zone_entry *curr_node;
  281. res = mlx4_bitmap_alloc_range(zone->bitmap, count,
  282. align, skip_mask);
  283. if (res != (u32)-1) {
  284. res += zone->offset;
  285. uid = zone->uid;
  286. goto out;
  287. }
  288. list_for_each_entry(curr_node, &zone_alloc->prios, prio_list) {
  289. if (unlikely(curr_node->priority == zone->priority))
  290. break;
  291. }
  292. if (zone->flags & MLX4_ZONE_ALLOW_ALLOC_FROM_LOWER_PRIO) {
  293. struct mlx4_zone_entry *it = curr_node;
  294. list_for_each_entry_continue_reverse(it, &zone_alloc->entries, list) {
  295. res = mlx4_bitmap_alloc_range(it->bitmap, count,
  296. align, skip_mask);
  297. if (res != (u32)-1) {
  298. res += it->offset;
  299. uid = it->uid;
  300. goto out;
  301. }
  302. }
  303. }
  304. if (zone->flags & MLX4_ZONE_ALLOW_ALLOC_FROM_EQ_PRIO) {
  305. struct mlx4_zone_entry *it = curr_node;
  306. list_for_each_entry_from(it, &zone_alloc->entries, list) {
  307. if (unlikely(it == zone))
  308. continue;
  309. if (unlikely(it->priority != curr_node->priority))
  310. break;
  311. res = mlx4_bitmap_alloc_range(it->bitmap, count,
  312. align, skip_mask);
  313. if (res != (u32)-1) {
  314. res += it->offset;
  315. uid = it->uid;
  316. goto out;
  317. }
  318. }
  319. }
  320. if (zone->flags & MLX4_ZONE_FALLBACK_TO_HIGHER_PRIO) {
  321. if (list_is_last(&curr_node->prio_list, &zone_alloc->prios))
  322. goto out;
  323. curr_node = list_first_entry(&curr_node->prio_list,
  324. typeof(*curr_node),
  325. prio_list);
  326. list_for_each_entry_from(curr_node, &zone_alloc->entries, list) {
  327. res = mlx4_bitmap_alloc_range(curr_node->bitmap, count,
  328. align, skip_mask);
  329. if (res != (u32)-1) {
  330. res += curr_node->offset;
  331. uid = curr_node->uid;
  332. goto out;
  333. }
  334. }
  335. }
  336. out:
  337. if (NULL != puid && res != (u32)-1)
  338. *puid = uid;
  339. return res;
  340. }
  341. /* Should be called under a lock */
  342. static void __mlx4_free_from_zone(struct mlx4_zone_entry *zone, u32 obj,
  343. u32 count)
  344. {
  345. mlx4_bitmap_free_range(zone->bitmap, obj - zone->offset, count, zone->use_rr);
  346. }
  347. /* Should be called under a lock */
  348. static struct mlx4_zone_entry *__mlx4_find_zone_by_uid(
  349. struct mlx4_zone_allocator *zones, u32 uid)
  350. {
  351. struct mlx4_zone_entry *zone;
  352. list_for_each_entry(zone, &zones->entries, list) {
  353. if (zone->uid == uid)
  354. return zone;
  355. }
  356. return NULL;
  357. }
  358. struct mlx4_bitmap *mlx4_zone_get_bitmap(struct mlx4_zone_allocator *zones, u32 uid)
  359. {
  360. struct mlx4_zone_entry *zone;
  361. struct mlx4_bitmap *bitmap;
  362. spin_lock(&zones->lock);
  363. zone = __mlx4_find_zone_by_uid(zones, uid);
  364. bitmap = zone == NULL ? NULL : zone->bitmap;
  365. spin_unlock(&zones->lock);
  366. return bitmap;
  367. }
  368. int mlx4_zone_remove_one(struct mlx4_zone_allocator *zones, u32 uid)
  369. {
  370. struct mlx4_zone_entry *zone;
  371. int res;
  372. spin_lock(&zones->lock);
  373. zone = __mlx4_find_zone_by_uid(zones, uid);
  374. if (NULL == zone) {
  375. res = -1;
  376. goto out;
  377. }
  378. res = __mlx4_zone_remove_one_entry(zone);
  379. out:
  380. spin_unlock(&zones->lock);
  381. kfree(zone);
  382. return res;
  383. }
  384. /* Should be called under a lock */
  385. static struct mlx4_zone_entry *__mlx4_find_zone_by_uid_unique(
  386. struct mlx4_zone_allocator *zones, u32 obj)
  387. {
  388. struct mlx4_zone_entry *zone, *zone_candidate = NULL;
  389. u32 dist = (u32)-1;
  390. /* Search for the smallest zone that this obj could be
  391. * allocated from. This is done in order to handle
  392. * situations when small bitmaps are allocated from bigger
  393. * bitmaps (and the allocated space is marked as reserved in
  394. * the bigger bitmap.
  395. */
  396. list_for_each_entry(zone, &zones->entries, list) {
  397. if (obj >= zone->offset) {
  398. u32 mobj = (obj - zone->offset) & zones->mask;
  399. if (mobj < zone->bitmap->max) {
  400. u32 curr_dist = zone->bitmap->effective_len;
  401. if (curr_dist < dist) {
  402. dist = curr_dist;
  403. zone_candidate = zone;
  404. }
  405. }
  406. }
  407. }
  408. return zone_candidate;
  409. }
  410. u32 mlx4_zone_alloc_entries(struct mlx4_zone_allocator *zones, u32 uid, int count,
  411. int align, u32 skip_mask, u32 *puid)
  412. {
  413. struct mlx4_zone_entry *zone;
  414. int res = -1;
  415. spin_lock(&zones->lock);
  416. zone = __mlx4_find_zone_by_uid(zones, uid);
  417. if (NULL == zone)
  418. goto out;
  419. res = __mlx4_alloc_from_zone(zone, count, align, skip_mask, puid);
  420. out:
  421. spin_unlock(&zones->lock);
  422. return res;
  423. }
  424. u32 mlx4_zone_free_entries(struct mlx4_zone_allocator *zones, u32 uid, u32 obj, u32 count)
  425. {
  426. struct mlx4_zone_entry *zone;
  427. int res = 0;
  428. spin_lock(&zones->lock);
  429. zone = __mlx4_find_zone_by_uid(zones, uid);
  430. if (NULL == zone) {
  431. res = -1;
  432. goto out;
  433. }
  434. __mlx4_free_from_zone(zone, obj, count);
  435. out:
  436. spin_unlock(&zones->lock);
  437. return res;
  438. }
  439. u32 mlx4_zone_free_entries_unique(struct mlx4_zone_allocator *zones, u32 obj, u32 count)
  440. {
  441. struct mlx4_zone_entry *zone;
  442. int res;
  443. if (!(zones->flags & MLX4_ZONE_ALLOC_FLAGS_NO_OVERLAP))
  444. return -EFAULT;
  445. spin_lock(&zones->lock);
  446. zone = __mlx4_find_zone_by_uid_unique(zones, obj);
  447. if (NULL == zone) {
  448. res = -1;
  449. goto out;
  450. }
  451. __mlx4_free_from_zone(zone, obj, count);
  452. res = 0;
  453. out:
  454. spin_unlock(&zones->lock);
  455. return res;
  456. }
  457. static int mlx4_buf_direct_alloc(struct mlx4_dev *dev, int size,
  458. struct mlx4_buf *buf, gfp_t gfp)
  459. {
  460. dma_addr_t t;
  461. buf->nbufs = 1;
  462. buf->npages = 1;
  463. buf->page_shift = get_order(size) + PAGE_SHIFT;
  464. buf->direct.buf =
  465. dma_zalloc_coherent(&dev->persist->pdev->dev,
  466. size, &t, gfp);
  467. if (!buf->direct.buf)
  468. return -ENOMEM;
  469. buf->direct.map = t;
  470. while (t & ((1 << buf->page_shift) - 1)) {
  471. --buf->page_shift;
  472. buf->npages *= 2;
  473. }
  474. return 0;
  475. }
  476. /* Handling for queue buffers -- we allocate a bunch of memory and
  477. * register it in a memory region at HCA virtual address 0. If the
  478. * requested size is > max_direct, we split the allocation into
  479. * multiple pages, so we don't require too much contiguous memory.
  480. */
  481. int mlx4_buf_alloc(struct mlx4_dev *dev, int size, int max_direct,
  482. struct mlx4_buf *buf, gfp_t gfp)
  483. {
  484. if (size <= max_direct) {
  485. return mlx4_buf_direct_alloc(dev, size, buf, gfp);
  486. } else {
  487. dma_addr_t t;
  488. int i;
  489. buf->direct.buf = NULL;
  490. buf->nbufs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  491. buf->npages = buf->nbufs;
  492. buf->page_shift = PAGE_SHIFT;
  493. buf->page_list = kcalloc(buf->nbufs, sizeof(*buf->page_list),
  494. gfp);
  495. if (!buf->page_list)
  496. return -ENOMEM;
  497. for (i = 0; i < buf->nbufs; ++i) {
  498. buf->page_list[i].buf =
  499. dma_zalloc_coherent(&dev->persist->pdev->dev,
  500. PAGE_SIZE, &t, gfp);
  501. if (!buf->page_list[i].buf)
  502. goto err_free;
  503. buf->page_list[i].map = t;
  504. }
  505. }
  506. return 0;
  507. err_free:
  508. mlx4_buf_free(dev, size, buf);
  509. return -ENOMEM;
  510. }
  511. EXPORT_SYMBOL_GPL(mlx4_buf_alloc);
  512. void mlx4_buf_free(struct mlx4_dev *dev, int size, struct mlx4_buf *buf)
  513. {
  514. if (buf->nbufs == 1) {
  515. dma_free_coherent(&dev->persist->pdev->dev, size,
  516. buf->direct.buf, buf->direct.map);
  517. } else {
  518. int i;
  519. for (i = 0; i < buf->nbufs; ++i)
  520. if (buf->page_list[i].buf)
  521. dma_free_coherent(&dev->persist->pdev->dev,
  522. PAGE_SIZE,
  523. buf->page_list[i].buf,
  524. buf->page_list[i].map);
  525. kfree(buf->page_list);
  526. }
  527. }
  528. EXPORT_SYMBOL_GPL(mlx4_buf_free);
  529. static struct mlx4_db_pgdir *mlx4_alloc_db_pgdir(struct device *dma_device,
  530. gfp_t gfp)
  531. {
  532. struct mlx4_db_pgdir *pgdir;
  533. pgdir = kzalloc(sizeof *pgdir, gfp);
  534. if (!pgdir)
  535. return NULL;
  536. bitmap_fill(pgdir->order1, MLX4_DB_PER_PAGE / 2);
  537. pgdir->bits[0] = pgdir->order0;
  538. pgdir->bits[1] = pgdir->order1;
  539. pgdir->db_page = dma_alloc_coherent(dma_device, PAGE_SIZE,
  540. &pgdir->db_dma, gfp);
  541. if (!pgdir->db_page) {
  542. kfree(pgdir);
  543. return NULL;
  544. }
  545. return pgdir;
  546. }
  547. static int mlx4_alloc_db_from_pgdir(struct mlx4_db_pgdir *pgdir,
  548. struct mlx4_db *db, int order)
  549. {
  550. int o;
  551. int i;
  552. for (o = order; o <= 1; ++o) {
  553. i = find_first_bit(pgdir->bits[o], MLX4_DB_PER_PAGE >> o);
  554. if (i < MLX4_DB_PER_PAGE >> o)
  555. goto found;
  556. }
  557. return -ENOMEM;
  558. found:
  559. clear_bit(i, pgdir->bits[o]);
  560. i <<= o;
  561. if (o > order)
  562. set_bit(i ^ 1, pgdir->bits[order]);
  563. db->u.pgdir = pgdir;
  564. db->index = i;
  565. db->db = pgdir->db_page + db->index;
  566. db->dma = pgdir->db_dma + db->index * 4;
  567. db->order = order;
  568. return 0;
  569. }
  570. int mlx4_db_alloc(struct mlx4_dev *dev, struct mlx4_db *db, int order, gfp_t gfp)
  571. {
  572. struct mlx4_priv *priv = mlx4_priv(dev);
  573. struct mlx4_db_pgdir *pgdir;
  574. int ret = 0;
  575. mutex_lock(&priv->pgdir_mutex);
  576. list_for_each_entry(pgdir, &priv->pgdir_list, list)
  577. if (!mlx4_alloc_db_from_pgdir(pgdir, db, order))
  578. goto out;
  579. pgdir = mlx4_alloc_db_pgdir(&dev->persist->pdev->dev, gfp);
  580. if (!pgdir) {
  581. ret = -ENOMEM;
  582. goto out;
  583. }
  584. list_add(&pgdir->list, &priv->pgdir_list);
  585. /* This should never fail -- we just allocated an empty page: */
  586. WARN_ON(mlx4_alloc_db_from_pgdir(pgdir, db, order));
  587. out:
  588. mutex_unlock(&priv->pgdir_mutex);
  589. return ret;
  590. }
  591. EXPORT_SYMBOL_GPL(mlx4_db_alloc);
  592. void mlx4_db_free(struct mlx4_dev *dev, struct mlx4_db *db)
  593. {
  594. struct mlx4_priv *priv = mlx4_priv(dev);
  595. int o;
  596. int i;
  597. mutex_lock(&priv->pgdir_mutex);
  598. o = db->order;
  599. i = db->index;
  600. if (db->order == 0 && test_bit(i ^ 1, db->u.pgdir->order0)) {
  601. clear_bit(i ^ 1, db->u.pgdir->order0);
  602. ++o;
  603. }
  604. i >>= o;
  605. set_bit(i, db->u.pgdir->bits[o]);
  606. if (bitmap_full(db->u.pgdir->order1, MLX4_DB_PER_PAGE / 2)) {
  607. dma_free_coherent(&dev->persist->pdev->dev, PAGE_SIZE,
  608. db->u.pgdir->db_page, db->u.pgdir->db_dma);
  609. list_del(&db->u.pgdir->list);
  610. kfree(db->u.pgdir);
  611. }
  612. mutex_unlock(&priv->pgdir_mutex);
  613. }
  614. EXPORT_SYMBOL_GPL(mlx4_db_free);
  615. int mlx4_alloc_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
  616. int size)
  617. {
  618. int err;
  619. err = mlx4_db_alloc(dev, &wqres->db, 1, GFP_KERNEL);
  620. if (err)
  621. return err;
  622. *wqres->db.db = 0;
  623. err = mlx4_buf_direct_alloc(dev, size, &wqres->buf, GFP_KERNEL);
  624. if (err)
  625. goto err_db;
  626. err = mlx4_mtt_init(dev, wqres->buf.npages, wqres->buf.page_shift,
  627. &wqres->mtt);
  628. if (err)
  629. goto err_buf;
  630. err = mlx4_buf_write_mtt(dev, &wqres->mtt, &wqres->buf, GFP_KERNEL);
  631. if (err)
  632. goto err_mtt;
  633. return 0;
  634. err_mtt:
  635. mlx4_mtt_cleanup(dev, &wqres->mtt);
  636. err_buf:
  637. mlx4_buf_free(dev, size, &wqres->buf);
  638. err_db:
  639. mlx4_db_free(dev, &wqres->db);
  640. return err;
  641. }
  642. EXPORT_SYMBOL_GPL(mlx4_alloc_hwq_res);
  643. void mlx4_free_hwq_res(struct mlx4_dev *dev, struct mlx4_hwq_resources *wqres,
  644. int size)
  645. {
  646. mlx4_mtt_cleanup(dev, &wqres->mtt);
  647. mlx4_buf_free(dev, size, &wqres->buf);
  648. mlx4_db_free(dev, &wqres->db);
  649. }
  650. EXPORT_SYMBOL_GPL(mlx4_free_hwq_res);