1*33ec1ccbSHans Petter Selasky /*
2*33ec1ccbSHans Petter Selasky * Copyright (c) 2005 Topspin Communications. All rights reserved.
3*33ec1ccbSHans Petter Selasky *
4*33ec1ccbSHans Petter Selasky * This software is available to you under a choice of one of two
5*33ec1ccbSHans Petter Selasky * licenses. You may choose to be licensed under the terms of the GNU
6*33ec1ccbSHans Petter Selasky * General Public License (GPL) Version 2, available from the file
7*33ec1ccbSHans Petter Selasky * COPYING in the main directory of this source tree, or the
8*33ec1ccbSHans Petter Selasky * OpenIB.org BSD license below:
9*33ec1ccbSHans Petter Selasky *
10*33ec1ccbSHans Petter Selasky * Redistribution and use in source and binary forms, with or
11*33ec1ccbSHans Petter Selasky * without modification, are permitted provided that the following
12*33ec1ccbSHans Petter Selasky * conditions are met:
13*33ec1ccbSHans Petter Selasky *
14*33ec1ccbSHans Petter Selasky * - Redistributions of source code must retain the above
15*33ec1ccbSHans Petter Selasky * copyright notice, this list of conditions and the following
16*33ec1ccbSHans Petter Selasky * disclaimer.
17*33ec1ccbSHans Petter Selasky *
18*33ec1ccbSHans Petter Selasky * - Redistributions in binary form must reproduce the above
19*33ec1ccbSHans Petter Selasky * copyright notice, this list of conditions and the following
20*33ec1ccbSHans Petter Selasky * disclaimer in the documentation and/or other materials
21*33ec1ccbSHans Petter Selasky * provided with the distribution.
22*33ec1ccbSHans Petter Selasky *
23*33ec1ccbSHans Petter Selasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*33ec1ccbSHans Petter Selasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*33ec1ccbSHans Petter Selasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*33ec1ccbSHans Petter Selasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*33ec1ccbSHans Petter Selasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*33ec1ccbSHans Petter Selasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*33ec1ccbSHans Petter Selasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*33ec1ccbSHans Petter Selasky * SOFTWARE.
31*33ec1ccbSHans Petter Selasky */
32*33ec1ccbSHans Petter Selasky
33*33ec1ccbSHans Petter Selasky #include "mthca_dev.h"
34*33ec1ccbSHans Petter Selasky #include "mthca_memfree.h"
35*33ec1ccbSHans Petter Selasky
mthca_uar_alloc(struct mthca_dev * dev,struct mthca_uar * uar)36*33ec1ccbSHans Petter Selasky int mthca_uar_alloc(struct mthca_dev *dev, struct mthca_uar *uar)
37*33ec1ccbSHans Petter Selasky {
38*33ec1ccbSHans Petter Selasky uar->index = mthca_alloc(&dev->uar_table.alloc);
39*33ec1ccbSHans Petter Selasky if (uar->index == -1)
40*33ec1ccbSHans Petter Selasky return -ENOMEM;
41*33ec1ccbSHans Petter Selasky
42*33ec1ccbSHans Petter Selasky uar->pfn = (pci_resource_start(dev->pdev, 2) >> PAGE_SHIFT) + uar->index;
43*33ec1ccbSHans Petter Selasky
44*33ec1ccbSHans Petter Selasky return 0;
45*33ec1ccbSHans Petter Selasky }
46*33ec1ccbSHans Petter Selasky
mthca_uar_free(struct mthca_dev * dev,struct mthca_uar * uar)47*33ec1ccbSHans Petter Selasky void mthca_uar_free(struct mthca_dev *dev, struct mthca_uar *uar)
48*33ec1ccbSHans Petter Selasky {
49*33ec1ccbSHans Petter Selasky mthca_free(&dev->uar_table.alloc, uar->index);
50*33ec1ccbSHans Petter Selasky }
51*33ec1ccbSHans Petter Selasky
mthca_init_uar_table(struct mthca_dev * dev)52*33ec1ccbSHans Petter Selasky int mthca_init_uar_table(struct mthca_dev *dev)
53*33ec1ccbSHans Petter Selasky {
54*33ec1ccbSHans Petter Selasky int ret;
55*33ec1ccbSHans Petter Selasky
56*33ec1ccbSHans Petter Selasky ret = mthca_alloc_init(&dev->uar_table.alloc,
57*33ec1ccbSHans Petter Selasky dev->limits.num_uars,
58*33ec1ccbSHans Petter Selasky dev->limits.num_uars - 1,
59*33ec1ccbSHans Petter Selasky dev->limits.reserved_uars + 1);
60*33ec1ccbSHans Petter Selasky if (ret)
61*33ec1ccbSHans Petter Selasky return ret;
62*33ec1ccbSHans Petter Selasky
63*33ec1ccbSHans Petter Selasky ret = mthca_init_db_tab(dev);
64*33ec1ccbSHans Petter Selasky if (ret)
65*33ec1ccbSHans Petter Selasky mthca_alloc_cleanup(&dev->uar_table.alloc);
66*33ec1ccbSHans Petter Selasky
67*33ec1ccbSHans Petter Selasky return ret;
68*33ec1ccbSHans Petter Selasky }
69*33ec1ccbSHans Petter Selasky
mthca_cleanup_uar_table(struct mthca_dev * dev)70*33ec1ccbSHans Petter Selasky void mthca_cleanup_uar_table(struct mthca_dev *dev)
71*33ec1ccbSHans Petter Selasky {
72*33ec1ccbSHans Petter Selasky mthca_cleanup_db_tab(dev);
73*33ec1ccbSHans Petter Selasky
74*33ec1ccbSHans Petter Selasky /* XXX check if any UARs are still allocated? */
75*33ec1ccbSHans Petter Selasky mthca_alloc_cleanup(&dev->uar_table.alloc);
76*33ec1ccbSHans Petter Selasky }
77