1329222Shselasky/*
2329222Shselasky * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3329222Shselasky *
4329222Shselasky * This software is available to you under a choice of one of two
5329222Shselasky * licenses.  You may choose to be licensed under the terms of the GNU
6329222Shselasky * General Public License (GPL) Version 2, available from the file
7329222Shselasky * COPYING in the main directory of this source tree, or the
8329222Shselasky * OpenIB.org BSD license below:
9329222Shselasky *
10329222Shselasky *     Redistribution and use in source and binary forms, with or
11329222Shselasky *     without modification, are permitted provided that the following
12329222Shselasky *     conditions are met:
13329222Shselasky *
14329222Shselasky *      - Redistributions of source code must retain the above
15329222Shselasky *        copyright notice, this list of conditions and the following
16329222Shselasky *        disclaimer.
17329222Shselasky *
18329222Shselasky *      - Redistributions in binary form must reproduce the above
19329222Shselasky *        copyright notice, this list of conditions and the following
20329222Shselasky *        disclaimer in the documentation and/or other materials
21329222Shselasky *        provided with the distribution.
22329222Shselasky *
23329222Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24329222Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25329222Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26329222Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27329222Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28329222Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29329222Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30329222Shselasky * SOFTWARE.
31329222Shselasky */
32329222Shselasky
33329222Shselasky#include "mthca_dev.h"
34329222Shselasky#include "mthca_memfree.h"
35329222Shselasky
36329222Shselaskyint mthca_uar_alloc(struct mthca_dev *dev, struct mthca_uar *uar)
37329222Shselasky{
38329222Shselasky	uar->index = mthca_alloc(&dev->uar_table.alloc);
39329222Shselasky	if (uar->index == -1)
40329222Shselasky		return -ENOMEM;
41329222Shselasky
42329222Shselasky	uar->pfn = (pci_resource_start(dev->pdev, 2) >> PAGE_SHIFT) + uar->index;
43329222Shselasky
44329222Shselasky	return 0;
45329222Shselasky}
46329222Shselasky
47329222Shselaskyvoid mthca_uar_free(struct mthca_dev *dev, struct mthca_uar *uar)
48329222Shselasky{
49329222Shselasky	mthca_free(&dev->uar_table.alloc, uar->index);
50329222Shselasky}
51329222Shselasky
52329222Shselaskyint mthca_init_uar_table(struct mthca_dev *dev)
53329222Shselasky{
54329222Shselasky	int ret;
55329222Shselasky
56329222Shselasky	ret = mthca_alloc_init(&dev->uar_table.alloc,
57329222Shselasky			       dev->limits.num_uars,
58329222Shselasky			       dev->limits.num_uars - 1,
59329222Shselasky			       dev->limits.reserved_uars + 1);
60329222Shselasky	if (ret)
61329222Shselasky		return ret;
62329222Shselasky
63329222Shselasky	ret = mthca_init_db_tab(dev);
64329222Shselasky	if (ret)
65329222Shselasky		mthca_alloc_cleanup(&dev->uar_table.alloc);
66329222Shselasky
67329222Shselasky	return ret;
68329222Shselasky}
69329222Shselasky
70329222Shselaskyvoid mthca_cleanup_uar_table(struct mthca_dev *dev)
71329222Shselasky{
72329222Shselasky	mthca_cleanup_db_tab(dev);
73329222Shselasky
74329222Shselasky	/* XXX check if any UARs are still allocated? */
75329222Shselasky	mthca_alloc_cleanup(&dev->uar_table.alloc);
76329222Shselasky}
77