18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2005 Topspin Communications.  All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <asm/page.h>		/* PAGE_SHIFT */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include "mthca_dev.h"
368c2ecf20Sopenharmony_ci#include "mthca_memfree.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciint mthca_uar_alloc(struct mthca_dev *dev, struct mthca_uar *uar)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	uar->index = mthca_alloc(&dev->uar_table.alloc);
418c2ecf20Sopenharmony_ci	if (uar->index == -1)
428c2ecf20Sopenharmony_ci		return -ENOMEM;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	uar->pfn = (pci_resource_start(dev->pdev, 2) >> PAGE_SHIFT) + uar->index;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	return 0;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_civoid mthca_uar_free(struct mthca_dev *dev, struct mthca_uar *uar)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	mthca_free(&dev->uar_table.alloc, uar->index);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciint mthca_init_uar_table(struct mthca_dev *dev)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	int ret;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	ret = mthca_alloc_init(&dev->uar_table.alloc,
598c2ecf20Sopenharmony_ci			       dev->limits.num_uars,
608c2ecf20Sopenharmony_ci			       dev->limits.num_uars - 1,
618c2ecf20Sopenharmony_ci			       dev->limits.reserved_uars + 1);
628c2ecf20Sopenharmony_ci	if (ret)
638c2ecf20Sopenharmony_ci		return ret;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ret = mthca_init_db_tab(dev);
668c2ecf20Sopenharmony_ci	if (ret)
678c2ecf20Sopenharmony_ci		mthca_alloc_cleanup(&dev->uar_table.alloc);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	return ret;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_civoid mthca_cleanup_uar_table(struct mthca_dev *dev)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	mthca_cleanup_db_tab(dev);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* XXX check if any UARs are still allocated? */
778c2ecf20Sopenharmony_ci	mthca_alloc_cleanup(&dev->uar_table.alloc);
788c2ecf20Sopenharmony_ci}
79