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