162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
362306a36Sopenharmony_ci * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This software is available to you under a choice of one of two
662306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
762306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
862306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
962306a36Sopenharmony_ci * OpenIB.org BSD license below:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1262306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1362306a36Sopenharmony_ci *     conditions are met:
1462306a36Sopenharmony_ci *
1562306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1662306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1762306a36Sopenharmony_ci *        disclaimer.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2062306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2162306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2262306a36Sopenharmony_ci *        provided with the distribution.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2562306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2662306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2762306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2862306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2962306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3062306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3162306a36Sopenharmony_ci * SOFTWARE.
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include <linux/mm.h>
3562306a36Sopenharmony_ci#include <linux/sched/signal.h>
3662306a36Sopenharmony_ci#include <linux/device.h>
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#include "qib.h"
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic void __qib_release_user_pages(struct page **p, size_t num_pages,
4162306a36Sopenharmony_ci				     int dirty)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	unpin_user_pages_dirty_lock(p, num_pages, dirty);
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/*
4762306a36Sopenharmony_ci * qib_map_page - a safety wrapper around pci_map_page()
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * A dma_addr of all 0's is interpreted by the chip as "disabled".
5062306a36Sopenharmony_ci * Unfortunately, it can also be a valid dma_addr returned on some
5162306a36Sopenharmony_ci * architectures.
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * The powerpc iommu assigns dma_addrs in ascending order, so we don't
5462306a36Sopenharmony_ci * have to bother with retries or mapping a dummy page to insure we
5562306a36Sopenharmony_ci * don't just get the same mapping again.
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * I'm sure we won't be so lucky with other iommu's, so FIXME.
5862306a36Sopenharmony_ci */
5962306a36Sopenharmony_ciint qib_map_page(struct pci_dev *hwdev, struct page *page, dma_addr_t *daddr)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	dma_addr_t phys;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	phys = dma_map_page(&hwdev->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
6462306a36Sopenharmony_ci	if (dma_mapping_error(&hwdev->dev, phys))
6562306a36Sopenharmony_ci		return -ENOMEM;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	if (!phys) {
6862306a36Sopenharmony_ci		dma_unmap_page(&hwdev->dev, phys, PAGE_SIZE, DMA_FROM_DEVICE);
6962306a36Sopenharmony_ci		phys = dma_map_page(&hwdev->dev, page, 0, PAGE_SIZE,
7062306a36Sopenharmony_ci				    DMA_FROM_DEVICE);
7162306a36Sopenharmony_ci		if (dma_mapping_error(&hwdev->dev, phys))
7262306a36Sopenharmony_ci			return -ENOMEM;
7362306a36Sopenharmony_ci		/*
7462306a36Sopenharmony_ci		 * FIXME: If we get 0 again, we should keep this page,
7562306a36Sopenharmony_ci		 * map another, then free the 0 page.
7662306a36Sopenharmony_ci		 */
7762306a36Sopenharmony_ci	}
7862306a36Sopenharmony_ci	*daddr = phys;
7962306a36Sopenharmony_ci	return 0;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci/**
8362306a36Sopenharmony_ci * qib_get_user_pages - lock user pages into memory
8462306a36Sopenharmony_ci * @start_page: the start page
8562306a36Sopenharmony_ci * @num_pages: the number of pages
8662306a36Sopenharmony_ci * @p: the output page structures
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * This function takes a given start page (page aligned user virtual
8962306a36Sopenharmony_ci * address) and pins it and the following specified number of pages.  For
9062306a36Sopenharmony_ci * now, num_pages is always 1, but that will probably change at some point
9162306a36Sopenharmony_ci * (because caller is doing expected sends on a single virtually contiguous
9262306a36Sopenharmony_ci * buffer, so we can do all pages at once).
9362306a36Sopenharmony_ci */
9462306a36Sopenharmony_ciint qib_get_user_pages(unsigned long start_page, size_t num_pages,
9562306a36Sopenharmony_ci		       struct page **p)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	unsigned long locked, lock_limit;
9862306a36Sopenharmony_ci	size_t got;
9962306a36Sopenharmony_ci	int ret;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
10262306a36Sopenharmony_ci	locked = atomic64_add_return(num_pages, &current->mm->pinned_vm);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
10562306a36Sopenharmony_ci		ret = -ENOMEM;
10662306a36Sopenharmony_ci		goto bail;
10762306a36Sopenharmony_ci	}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	mmap_read_lock(current->mm);
11062306a36Sopenharmony_ci	for (got = 0; got < num_pages; got += ret) {
11162306a36Sopenharmony_ci		ret = pin_user_pages(start_page + got * PAGE_SIZE,
11262306a36Sopenharmony_ci				     num_pages - got,
11362306a36Sopenharmony_ci				     FOLL_LONGTERM | FOLL_WRITE,
11462306a36Sopenharmony_ci				     p + got);
11562306a36Sopenharmony_ci		if (ret < 0) {
11662306a36Sopenharmony_ci			mmap_read_unlock(current->mm);
11762306a36Sopenharmony_ci			goto bail_release;
11862306a36Sopenharmony_ci		}
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci	mmap_read_unlock(current->mm);
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	return 0;
12362306a36Sopenharmony_cibail_release:
12462306a36Sopenharmony_ci	__qib_release_user_pages(p, got, 0);
12562306a36Sopenharmony_cibail:
12662306a36Sopenharmony_ci	atomic64_sub(num_pages, &current->mm->pinned_vm);
12762306a36Sopenharmony_ci	return ret;
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_civoid qib_release_user_pages(struct page **p, size_t num_pages)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	__qib_release_user_pages(p, num_pages, 1);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	/* during close after signal, mm can be NULL */
13562306a36Sopenharmony_ci	if (current->mm)
13662306a36Sopenharmony_ci		atomic64_sub(num_pages, &current->mm->pinned_vm);
13762306a36Sopenharmony_ci}
138