alloc.c 18 KB

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