1219820Sjeff/*
2219820Sjeff * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff */
32219820Sjeff
33273246Shselasky#include <linux/page.h>
34219820Sjeff
35219820Sjeff#include "mthca_dev.h"
36219820Sjeff#include "mthca_memfree.h"
37219820Sjeff
38219820Sjeffint mthca_uar_alloc(struct mthca_dev *dev, struct mthca_uar *uar)
39219820Sjeff{
40219820Sjeff	uar->index = mthca_alloc(&dev->uar_table.alloc);
41219820Sjeff	if (uar->index == -1)
42219820Sjeff		return -ENOMEM;
43219820Sjeff
44219820Sjeff	uar->pfn = (pci_resource_start(dev->pdev, 2) >> PAGE_SHIFT) + uar->index;
45219820Sjeff
46219820Sjeff	return 0;
47219820Sjeff}
48219820Sjeff
49219820Sjeffvoid mthca_uar_free(struct mthca_dev *dev, struct mthca_uar *uar)
50219820Sjeff{
51219820Sjeff	mthca_free(&dev->uar_table.alloc, uar->index);
52219820Sjeff}
53219820Sjeff
54219820Sjeffint mthca_init_uar_table(struct mthca_dev *dev)
55219820Sjeff{
56219820Sjeff	int ret;
57219820Sjeff
58219820Sjeff	ret = mthca_alloc_init(&dev->uar_table.alloc,
59219820Sjeff			       dev->limits.num_uars,
60219820Sjeff			       dev->limits.num_uars - 1,
61219820Sjeff			       dev->limits.reserved_uars + 1);
62219820Sjeff	if (ret)
63219820Sjeff		return ret;
64219820Sjeff
65219820Sjeff	ret = mthca_init_db_tab(dev);
66219820Sjeff	if (ret)
67219820Sjeff		mthca_alloc_cleanup(&dev->uar_table.alloc);
68219820Sjeff
69219820Sjeff	return ret;
70219820Sjeff}
71219820Sjeff
72219820Sjeffvoid mthca_cleanup_uar_table(struct mthca_dev *dev)
73219820Sjeff{
74219820Sjeff	mthca_cleanup_db_tab(dev);
75219820Sjeff
76219820Sjeff	/* XXX check if any UARs are still allocated? */
77219820Sjeff	mthca_alloc_cleanup(&dev->uar_table.alloc);
78219820Sjeff}
79