ipath_file_ops.c revision 0df6291c8af2778d05f278d5738eef2c8fafa2dd
17f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan/*
2c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
37f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
47f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
57f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * This software is available to you under a choice of one of two
67f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * licenses.  You may choose to be licensed under the terms of the GNU
77f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * General Public License (GPL) Version 2, available from the file
87f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * COPYING in the main directory of this source tree, or the
97f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * OpenIB.org BSD license below:
107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *     Redistribution and use in source and binary forms, with or
127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *     without modification, are permitted provided that the following
137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *     conditions are met:
147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *      - Redistributions of source code must retain the above
167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *        copyright notice, this list of conditions and the following
177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *        disclaimer.
187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *      - Redistributions in binary form must reproduce the above
207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *        copyright notice, this list of conditions and the following
217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *        disclaimer in the documentation and/or other materials
227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *        provided with the distribution.
237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * SOFTWARE.
327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan */
337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan#include <linux/pci.h>
357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan#include <linux/poll.h>
367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan#include <linux/cdev.h>
377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan#include <linux/swap.h>
387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan#include <linux/vmalloc.h>
397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan#include <asm/pgtable.h>
407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan#include "ipath_kernel.h"
4227b678dd04a636f2c351816f4b3042c8815d4e9dBryan O'Sullivan#include "ipath_common.h"
437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int ipath_open(struct inode *, struct file *);
457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int ipath_close(struct inode *, struct file *);
467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic ssize_t ipath_write(struct file *, const char __user *, size_t,
477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   loff_t *);
487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic unsigned int ipath_poll(struct file *, struct poll_table_struct *);
497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int ipath_mmap(struct file *, struct vm_area_struct *);
507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
512b8693c0617e972fc0b2fd1ebf8de97e15b656c3Arjan van de Venstatic const struct file_operations ipath_file_ops = {
527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	.owner = THIS_MODULE,
537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	.write = ipath_write,
547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	.open = ipath_open,
557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	.release = ipath_close,
567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	.poll = ipath_poll,
577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	.mmap = ipath_mmap
587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan};
597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
600a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell/*
610a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell * Convert kernel virtual addresses to physical addresses so they don't
620a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell * potentially conflict with the chip addresses used as mmap offsets.
630a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell * It doesn't really matter what mmap offset we use as long as we can
640a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell * interpret it correctly.
650a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell */
660a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbellstatic u64 cvt_kvaddr(void *p)
670a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell{
680a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	struct page *page;
690a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	u64 paddr = 0;
700a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell
710a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	page = vmalloc_to_page(p);
720a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	if (page)
730a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell		paddr = page_to_pfn(page) << PAGE_SHIFT;
740a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell
750a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	return paddr;
760a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell}
770a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell
789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int ipath_get_base_info(struct file *fp,
797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			       void __user *ubase, size_t ubase_size)
807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
819929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	struct ipath_portdata *pd = port_fp(fp);
827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret = 0;
837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_base_info *kinfo = NULL;
847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
859929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	unsigned subport_cnt;
869929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	int shared, master;
879929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	size_t sz;
889929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
899929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	subport_cnt = pd->port_subport_cnt;
909929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!subport_cnt) {
919929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		shared = 0;
929929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		master = 0;
939929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		subport_cnt = 1;
949929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else {
959929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		shared = 1;
969929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		master = !subport_fp(fp);
979929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	sz = sizeof(*kinfo);
1009929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* If port sharing is not requested, allow the old size structure */
1019929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!shared)
102c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		sz -= 7 * sizeof(u64);
1039929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (ubase_size < sz) {
1047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(PROC,
1059929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			   "Base size %zu, need %zu (version mismatch?)\n",
1069929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			   ubase_size, sz);
1077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EINVAL;
1087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
1097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
1107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
1117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
1127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (kinfo == NULL) {
1137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENOMEM;
1147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
1157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
1167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
1177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = dd->ipath_f_get_base_info(pd, kinfo);
1187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret < 0)
1197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
1207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
1217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
1227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
1237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
1247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
1257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
1267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * have to mmap whole thing
1277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
1287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_rcv_egrbuftotlen =
1297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
1307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
1317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
1327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		pd->port_rcvegrbuf_chunks;
1339929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;
1349929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (master)
1359929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;
1367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
1377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * for this use, may be ipath_cfgports summed over all chips that
1387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * are are configured and present
1397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
1407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_nports = dd->ipath_cfgports;
1417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* unit (chip/board) our port is on */
1427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_unit = dd->ipath_unit;
1437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* for now, only a single page */
1447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_tid_maxsize = PAGE_SIZE;
1457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
1467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
1477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * Doing this per port, and based on the skip value, etc.  This has
1487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * to be the actual buffer size, since the protocol code treats it
1497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * as an array.
1507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 *
1517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * These have to be set to user addresses in the user code via mmap.
1527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * These values are used on return to user code for the mmap target
1537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * addresses only.  For 32 bit, same 44 bit address problem, so use
1547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * the physical address, not virtual.  Before 2.6.11, using the
1557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * page_address() macro worked, but in 2.6.11, even that returns the
1567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * full 64 bit address (upper bits all 1's).  So far, using the
1577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * physical addresses (or chip offsets, for chip mapping) works, but
1589929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * no doubt some future kernel release will change that, and we'll be
1599929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * on to yet another method of dealing with this.
1607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
1617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
1629929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;
1637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
1647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
1657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
1667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		(void *) dd->ipath_statusp -
1677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		(void *) dd->ipath_pioavailregs_dma;
1689929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!shared) {
1699929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->spi_piocnt = dd->ipath_pbufsport;
1709929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->spi_piobufbase = (u64) pd->port_piobufs;
1719929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
1729929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			dd->ipath_palign * pd->port_port;
1739929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else if (master) {
1749929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->spi_piocnt = (dd->ipath_pbufsport / subport_cnt) +
1759929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				    (dd->ipath_pbufsport % subport_cnt);
1769929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		/* Master's PIO buffers are after all the slave's */
1779929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->spi_piobufbase = (u64) pd->port_piobufs +
1789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			dd->ipath_palign *
1799929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			(dd->ipath_pbufsport - kinfo->spi_piocnt);
1809929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else {
1819929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		unsigned slave = subport_fp(fp) - 1;
1827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
1839929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->spi_piocnt = dd->ipath_pbufsport / subport_cnt;
1849929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->spi_piobufbase = (u64) pd->port_piobufs +
1859929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			dd->ipath_palign * kinfo->spi_piocnt * slave;
186c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	}
187c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	if (shared) {
188c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase +
189c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			dd->ipath_palign * pd->port_port;
190c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs;
191c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base;
192c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr;
193c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage
1940a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell		kinfo->__spi_uregbase = cvt_kvaddr(pd->subport_uregbase +
195c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			PAGE_SIZE * subport_fp(fp));
1969929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
1970a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell		kinfo->spi_rcvhdr_base = cvt_kvaddr(pd->subport_rcvhdr_base +
198c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			pd->port_rcvhdrq_size * subport_fp(fp));
199947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell		kinfo->spi_rcvhdr_tailaddr = 0;
2000a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell		kinfo->spi_rcv_egrbufs = cvt_kvaddr(pd->subport_rcvegrbuf +
201c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size *
202c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			subport_fp(fp));
203c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage
204c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		kinfo->spi_subport_uregbase =
205c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			cvt_kvaddr(pd->subport_uregbase);
206c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		kinfo->spi_subport_rcvegrbuf =
207c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			cvt_kvaddr(pd->subport_rcvegrbuf);
208c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		kinfo->spi_subport_rcvhdr_base =
209c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			cvt_kvaddr(pd->subport_rcvhdr_base);
210c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",
211c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			kinfo->spi_port, kinfo->spi_runtime_flags,
212c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			(unsigned long long) kinfo->spi_subport_uregbase,
213c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			(unsigned long long) kinfo->spi_subport_rcvegrbuf,
214c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage			(unsigned long long) kinfo->spi_subport_rcvhdr_base);
2159929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
2169929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
2179929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	kinfo->spi_pioindex = (kinfo->spi_piobufbase - dd->ipath_piobufbase) /
2189929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		dd->ipath_palign;
2197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_pioalign = dd->ipath_palign;
2207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_qpair = IPATH_KD_QP;
2227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_piosize = dd->ipath_ibmaxlen;
2237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_mtu = dd->ipath_ibmaxlen;	/* maxlen, not ibmtu */
2247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_port = pd->port_port;
2259929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	kinfo->spi_subport = subport_fp(fp);
226eaf6733bc176742fb08def2269441684e963c275Bryan O'Sullivan	kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
2277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kinfo->spi_hw_version = dd->ipath_revision;
2287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2299929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (master) {
2309929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;
2319929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
2329929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
233c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	sz = (ubase_size < sizeof(*kinfo)) ? ubase_size : sizeof(*kinfo);
234c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	if (copy_to_user(ubase, kinfo, sz))
2357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EFAULT;
2367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
2387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kfree(kinfo);
2397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
2407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
2417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan/**
2437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * ipath_tid_update - update a port TID
2447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @pd: the port
2459929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan * @fp: the ipath device file
2467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @ti: the TID information
2477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
2487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * The new implementation as of Oct 2004 is that the driver assigns
2497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * the tid and returns it to the caller.   To make it easier to
2507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * catch bugs, and to reduce search time, we keep a cursor for
2517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * each port, walking the shadow tid array to find one that's not
2527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * in use.
2537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
2547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * For now, if we can't allocate the full list, we fail, although
2557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * in the long run, we'll allocate as many as we can, and the
2567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * caller will deal with that by trying the remaining pages later.
2577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * That means that when we fail, we have to mark the tids as not in
2587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * use again, in our shadow copy.
2597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
2607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * It's up to the caller to free the tids when they are done.
2617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * We'll unlock the pages as they free them.
2627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
2637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * Also, right now we are locking one page at a time, but since
2647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * the intended use of this routine is for a single group of
2657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * virtually contiguous pages, that should change to improve
2667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * performance.
2677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan */
2689929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,
2697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			    const struct ipath_tid_info *ti)
2707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
2717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret = 0, ntids;
2729929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	u32 tid, porttid, cnt, i, tidcnt, tidoff;
2737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u16 *tidlist;
2747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
2757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u64 physaddr;
2767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unsigned long vaddr;
2777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u64 __iomem *tidbase;
2787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unsigned long tidmap[8];
2797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct page **pagep = NULL;
2809929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	unsigned subport = subport_fp(fp);
2817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!dd->ipath_pageshadow) {
2837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENOMEM;
2847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto done;
2857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
2867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	cnt = ti->tidcnt;
2887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!cnt) {
2897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
2907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  (unsigned long long) ti->tidlist);
2917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/*
2927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * Should we treat as success?  likely a bug
2937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 */
2947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EFAULT;
2957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto done;
2967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
2979929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	porttid = pd->port_port * dd->ipath_rcvtidcnt;
2989929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd->port_subport_cnt) {
2999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidcnt = dd->ipath_rcvtidcnt;
3009929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tid = pd->port_tidcursor;
3019929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidoff = 0;
3029929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else if (!subport) {
3039929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
3049929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
3059929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidoff = dd->ipath_rcvtidcnt - tidcnt;
3069929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		porttid += tidoff;
3079929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tid = tidcursor_fp(fp);
3089929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else {
3099929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
3109929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidoff = tidcnt * (subport - 1);
3119929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		porttid += tidoff;
3129929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tid = tidcursor_fp(fp);
3139929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
3149929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (cnt > tidcnt) {
3157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/* make sure it all fits in port_tid_pg_list */
3167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
3177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 "TIDs, only trying max (%u)\n", cnt, tidcnt);
3187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		cnt = tidcnt;
3197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
3209929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];
3219929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];
3227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
3237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	memset(tidmap, 0, sizeof(tidmap));
3247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* before decrement; chip actual # */
3257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ntids = tidcnt;
3267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
3277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   dd->ipath_rcvtidbase +
3287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   porttid * sizeof(*tidbase));
3297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
3307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
3317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   pd->port_port, cnt, tid, tidbase);
3327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
3337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* virtual address of first page in transfer */
3347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	vaddr = ti->tidvaddr;
3357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
3367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       cnt * PAGE_SIZE)) {
3377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
3387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  (void *)vaddr, cnt);
3397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EFAULT;
3407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto done;
3417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
3427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = ipath_get_user_pages(vaddr, cnt, pagep);
3437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret) {
3447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (ret == -EBUSY) {
3457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_dbg("Failed to lock addr %p, %u pages "
3467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				  "(already locked)\n",
3477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				  (void *) vaddr, cnt);
3487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			/*
3497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * for now, continue, and see what happens but with
3507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * the new implementation, this should never happen,
3517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * unless perhaps the user has mpin'ed the pages
3527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * themselves (something we need to test)
3537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 */
3547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = 0;
3557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		} else {
3567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			dev_info(&dd->pcidev->dev,
3577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 "Failed to lock addr %p, %u pages: "
3587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 "errno %d\n", (void *) vaddr, cnt, -ret);
3597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto done;
3607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
3617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
3627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
3637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		for (; ntids--; tid++) {
3647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (tid == tidcnt)
3657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				tid = 0;
3667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (!dd->ipath_pageshadow[porttid + tid])
3677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				break;
3687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
3697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (ntids < 0) {
3707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			/*
3717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * oops, wrapped all the way through their TIDs,
3727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * and didn't have enough free; see comments at
3737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * start of routine
3747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 */
3757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_dbg("Not enough free TIDs for %u pages "
3767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				  "(index %d), failing\n", cnt, i);
3777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			i--;	/* last tidlist[i] not filled in */
3787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -ENOMEM;
3797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			break;
3807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
3819929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidlist[i] = tid + tidoff;
3827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
3839929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			   "vaddr %lx\n", i, tid + tidoff, vaddr);
3847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/* we "know" system pages and TID pages are same size */
3857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dd->ipath_pageshadow[porttid + tid] = pagep[i];
3861fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		dd->ipath_physshadow[porttid + tid] = ipath_map_page(
3871fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			dd->pcidev, pagep[i], 0, PAGE_SIZE,
3881fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			PCI_DMA_FROMDEVICE);
3897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/*
3907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * don't need atomic or it's overhead
3917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 */
3927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		__set_bit(tid, tidmap);
3931fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		physaddr = dd->ipath_physshadow[porttid + tid];
3947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_stats.sps_pagelocks++;
3957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(VERBOSE,
3967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
3977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   tid, vaddr, (unsigned long long) physaddr,
3987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   pagep[i]);
399f716cdfe57f217966f41a7add190d6f5b9fd0769Joan Eslinger		dd->ipath_f_put_tid(dd, &tidbase[tid], RCVHQ_RCV_TYPE_EXPECTED,
400f716cdfe57f217966f41a7add190d6f5b9fd0769Joan Eslinger				    physaddr);
4017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/*
4027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * don't check this tid in ipath_portshadow, since we
4037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * just filled it in; start with the next one.
4047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 */
4057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		tid++;
4067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
4077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
4087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret) {
4097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		u32 limit;
4107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	cleanup:
4117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/* jump here if copy out of updated info failed... */
4127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
4137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  -ret, i, cnt);
4147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/* same code that's in ipath_free_tid() */
4157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		limit = sizeof(tidmap) * BITS_PER_BYTE;
4167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (limit > tidcnt)
4177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			/* just in case size changes in future */
4187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			limit = tidcnt;
4197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		tid = find_first_bit((const unsigned long *)tidmap, limit);
4207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		for (; tid < limit; tid++) {
4217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (!test_bit(tid, tidmap))
4227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				continue;
4237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (dd->ipath_pageshadow[porttid + tid]) {
4247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ipath_cdbg(VERBOSE, "Freeing TID %u\n",
4257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   tid);
426f716cdfe57f217966f41a7add190d6f5b9fd0769Joan Eslinger				dd->ipath_f_put_tid(dd, &tidbase[tid],
427f716cdfe57f217966f41a7add190d6f5b9fd0769Joan Eslinger						    RCVHQ_RCV_TYPE_EXPECTED,
4287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan						    dd->ipath_tidinvalid);
4291fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan				pci_unmap_page(dd->pcidev,
4301fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan					dd->ipath_physshadow[porttid + tid],
4311fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan					PAGE_SIZE, PCI_DMA_FROMDEVICE);
4327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				dd->ipath_pageshadow[porttid + tid] = NULL;
4337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ipath_stats.sps_pageunlocks++;
4347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			}
4357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
4367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_release_user_pages(pagep, cnt);
4377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	} else {
4387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/*
4397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * Copy the updated array, with ipath_tid's filled in, back
4407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * to user.  Since we did the copy in already, this "should
4417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * never fail" If it does, we have to clean up...
4427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 */
4437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (copy_to_user((void __user *)
4447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 (unsigned long) ti->tidlist,
4457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 tidlist, cnt * sizeof(*tidlist))) {
4467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -EFAULT;
4477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto cleanup;
4487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
4497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
4507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 tidmap, sizeof tidmap)) {
4517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -EFAULT;
4527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto cleanup;
4537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
4547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (tid == tidcnt)
4557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			tid = 0;
4569929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		if (!pd->port_subport_cnt)
4579929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			pd->port_tidcursor = tid;
4589929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		else
4599929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			tidcursor_fp(fp) = tid;
4607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
4617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
4627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivandone:
4637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret)
4647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("Failed to map %u TID pages, failing with %d\n",
4657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  ti->tidcnt, -ret);
4667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
4677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
4687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
4697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan/**
4707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * ipath_tid_free - free a port TID
4717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @pd: the port
4729929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan * @subport: the subport
4737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @ti: the TID info
4747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
4757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * right now we are unlocking one page at a time, but since
4767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * the intended use of this routine is for a single group of
4777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * virtually contiguous pages, that should change to improve
4787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * performance.  We check that the TID is in range for this port
4797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * but otherwise don't check validity; if user has an error and
4807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * frees the wrong tid, it's only their own data that can thereby
4817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * be corrupted.  We do check that the TID was in use, for sanity
4827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * We always use our idea of the saved address, not the address that
4837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * they pass in to us.
4847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan */
4857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
4869929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
4877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  const struct ipath_tid_info *ti)
4887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
4897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret = 0;
4907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u32 tid, porttid, cnt, limit, tidcnt;
4917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
4927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u64 __iomem *tidbase;
4937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unsigned long tidmap[8];
4947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
4957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!dd->ipath_pageshadow) {
4967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENOMEM;
4977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto done;
4987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
4997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
5007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
5017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   sizeof tidmap)) {
5027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EFAULT;
5037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto done;
5047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
5057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
5067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	porttid = pd->port_port * dd->ipath_rcvtidcnt;
5079929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd->port_subport_cnt)
5089929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidcnt = dd->ipath_rcvtidcnt;
5099929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	else if (!subport) {
5109929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
5119929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
5129929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		porttid += dd->ipath_rcvtidcnt - tidcnt;
5139929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else {
5149929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
5159929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		porttid += tidcnt * (subport - 1);
5169929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
5177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
5187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   dd->ipath_rcvtidbase +
5197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   porttid * sizeof(*tidbase));
5207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
5217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	limit = sizeof(tidmap) * BITS_PER_BYTE;
5227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (limit > tidcnt)
5237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/* just in case size changes in future */
5247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		limit = tidcnt;
5257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	tid = find_first_bit(tidmap, limit);
5267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
5277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
5287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   limit, tid, porttid);
5297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (cnt = 0; tid < limit; tid++) {
5307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/*
5317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * small optimization; if we detect a run of 3 or so without
5327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * any set, use find_first_bit again.  That's mainly to
5337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * accelerate the case where we wrapped, so we have some at
5347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * the beginning, and some at the end, and a big gap
5357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * in the middle.
5367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 */
5377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (!test_bit(tid, tidmap))
5387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			continue;
5397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		cnt++;
5407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (dd->ipath_pageshadow[porttid + tid]) {
5417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
5427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   pd->port_pid, tid);
543f716cdfe57f217966f41a7add190d6f5b9fd0769Joan Eslinger			dd->ipath_f_put_tid(dd, &tidbase[tid],
544f716cdfe57f217966f41a7add190d6f5b9fd0769Joan Eslinger					    RCVHQ_RCV_TYPE_EXPECTED,
5457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					    dd->ipath_tidinvalid);
5461fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			pci_unmap_page(dd->pcidev,
5471fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan				dd->ipath_physshadow[porttid + tid],
5481fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan				PAGE_SIZE, PCI_DMA_FROMDEVICE);
5497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_release_user_pages(
5507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				&dd->ipath_pageshadow[porttid + tid], 1);
5517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			dd->ipath_pageshadow[porttid + tid] = NULL;
5527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_stats.sps_pageunlocks++;
5537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		} else
5547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_dbg("Unused tid %u, ignoring\n", tid);
5557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
5567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (cnt != ti->tidcnt)
5577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
5587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  ti->tidcnt, cnt);
5597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivandone:
5607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret)
5617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
5627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  ti->tidcnt, -ret);
5637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
5647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
5657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
5667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan/**
5677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * ipath_set_part_key - set a partition key
5687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @pd: the port
5697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @key: the key
5707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
5717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * We can have up to 4 active at a time (other than the default, which is
5727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * always allowed).  This is somewhat tricky, since multiple ports may set
5737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * the same key, so we reference count them, and clean up at exit.  All 4
5747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * partition keys are packed into a single infinipath register.  It's an
5757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * error for a process to set the same pkey multiple times.  We provide no
5767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * mechanism to de-allocate a pkey at this time, we may eventually need to
5777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * do that.  I've used the atomic operations, and no locking, and only make
5787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * a single pass through what's available.  This should be more than
5797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * adequate for some time. I'll think about spinlocks or the like if and as
5807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * it's necessary.
5817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan */
5827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
5837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
5847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
5857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int i, any = 0, pidx = -1;
5867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u16 lkey = key & 0x7FFF;
5877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
5887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
58927b678dd04a636f2c351816f4b3042c8815d4e9dBryan O'Sullivan	if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
5907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/* nothing to do; this key always valid */
5917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = 0;
5927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
5937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
5947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
5957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
5967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   "%hx:%x %hx:%x %hx:%x %hx:%x\n",
5977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   pd->port_port, key, dd->ipath_pkeys[0],
5987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
5997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
6007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
6017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   atomic_read(&dd->ipath_pkeyrefs[3]));
6027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
6037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!lkey) {
6047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
6057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   pd->port_port);
6067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EINVAL;
6077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
6087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
6097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
6107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
6117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * Set the full membership bit, because it has to be
6127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * set in the register or the packet, and it seems
6137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * cleaner to set in the register than to force all
6147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * callers to set it. (see bug 4331)
6157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
6167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	key |= 0x8000;
6177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
6187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
6197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (!pd->port_pkeys[i] && pidx == -1)
6207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			pidx = i;
6217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (pd->port_pkeys[i] == key) {
6227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
6237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   "(%x) more than once\n",
6247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   pd->port_port, key);
6257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -EEXIST;
6267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
6277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
6287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
6297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (pidx == -1) {
6307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("All pkeys for port %u already in use, "
6317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  "can't set %x\n", pd->port_port, key);
6327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EBUSY;
6337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
6347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
6357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
6367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (!dd->ipath_pkeys[i]) {
6377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			any++;
6387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			continue;
6397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
6407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (dd->ipath_pkeys[i] == key) {
6417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
6427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
6437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (atomic_inc_return(pkrefs) > 1) {
6447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				pd->port_pkeys[pidx] = key;
6457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ipath_cdbg(VERBOSE, "p%u set key %x "
6467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   "matches #%d, count now %d\n",
6477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   pd->port_port, key, i,
6487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   atomic_read(pkrefs));
6497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ret = 0;
6507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				goto bail;
6517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			} else {
6527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				/*
6537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 * lost race, decrement count, catch below
6547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 */
6557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				atomic_dec(pkrefs);
6567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ipath_cdbg(VERBOSE, "Lost race, count was "
6577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   "0, after dec, it's %d\n",
6587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   atomic_read(pkrefs));
6597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				any++;
6607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			}
6617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
6627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
6637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			/*
6647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * It makes no sense to have both the limited and
6657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * full membership PKEY set at the same time since
6667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 * the unlimited one will disable the limited one.
6677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 */
6687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -EEXIST;
6697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
6707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
6717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
6727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!any) {
6737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("port %u, all pkeys already in use, "
6747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  "can't set %x\n", pd->port_port, key);
6757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EBUSY;
6767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
6777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
6787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
6797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (!dd->ipath_pkeys[i] &&
6807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		    atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
6817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			u64 pkey;
6827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
6837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			/* for ipathstats, etc. */
6847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_stats.sps_pkeys[i] = lkey;
6857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
6867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			pkey =
6877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				(u64) dd->ipath_pkeys[0] |
6887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				((u64) dd->ipath_pkeys[1] << 16) |
6897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				((u64) dd->ipath_pkeys[2] << 32) |
6907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				((u64) dd->ipath_pkeys[3] << 48);
6917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_cdbg(PROC, "p%u set key %x in #%d, "
6927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   "portidx %d, new pkey reg %llx\n",
6937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   pd->port_port, key, i, pidx,
6947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				   (unsigned long long) pkey);
6957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_write_kreg(
6967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				dd, dd->ipath_kregs->kr_partitionkey, pkey);
6977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
6987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = 0;
6997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
7007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
7017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
7027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_dbg("port %u, all pkeys already in use 2nd pass, "
7037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		  "can't set %x\n", pd->port_port, key);
7047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = -EBUSY;
7057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
7067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
7077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
7087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
7097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
7107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan/**
7117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * ipath_manage_rcvq - manage a port's receive queue
7127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @pd: the port
7139929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan * @subport: the subport
7147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @start_stop: action to carry out
7157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
7167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * start_stop == 0 disables receive on the port, for use in queue
7177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * overflow conditions.  start_stop==1 re-enables, to be used to
7187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * re-init the software copy of the head register
7197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan */
7209929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
7219929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			     int start_stop)
7227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
7237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
7247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
7259929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
7267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   start_stop ? "en" : "dis", dd->ipath_unit,
7279929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		   pd->port_port, subport);
7289929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (subport)
7299929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
7307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* atomically clear receive enable port. */
7317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (start_stop) {
7327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/*
7337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * On enable, force in-memory copy of the tail register to
7347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * 0, so that protocol code doesn't have to worry about
7357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * whether or not the chip has yet updated the in-memory
7367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * copy or not on return from the system call. The chip
7377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * always resets it's tail register back to 0 on a
7387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * transition from disabled to enabled.  This could cause a
7397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * problem if software was broken, and did the enable w/o
7407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * the disable, but eventually the in-memory copy will be
7417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * updated and correct itself, even in the face of software
7427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * bugs.
7437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 */
7441fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		*(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0;
7457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
7467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			&dd->ipath_rcvctrl);
7477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	} else
7487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
7497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  &dd->ipath_rcvctrl);
7507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
7517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 dd->ipath_rcvctrl);
7527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* now be sure chip saw it before we return */
75344f8e3f3f7d8e61b4aafced278403955fe18ad88Roland Dreier	ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
7547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (start_stop) {
7557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/*
7567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * And try to be sure that tail reg update has happened too.
7577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * This should in theory interlock with the RXE changes to
7587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * the tail register.  Don't assign it to the tail register
7597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * in memory copy, since we could overwrite an update by the
7607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * chip if we did.
7617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 */
76244f8e3f3f7d8e61b4aafced278403955fe18ad88Roland Dreier		ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
7637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
7647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* always; new head should be equal to new tail; see above */
7659929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanbail:
7667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return 0;
7677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
7687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
7697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic void ipath_clean_part_key(struct ipath_portdata *pd,
7707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 struct ipath_devdata *dd)
7717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
7727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int i, j, pchanged = 0;
7737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u64 oldpkey;
7747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
7757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* for debugging only */
7767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	oldpkey = (u64) dd->ipath_pkeys[0] |
7777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		((u64) dd->ipath_pkeys[1] << 16) |
7787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		((u64) dd->ipath_pkeys[2] << 32) |
7797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		((u64) dd->ipath_pkeys[3] << 48);
7807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
7817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
7827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (!pd->port_pkeys[i])
7837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			continue;
7847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
7857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   pd->port_pkeys[i]);
7867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
7877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			/* check for match independent of the global bit */
7887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if ((dd->ipath_pkeys[j] & 0x7fff) !=
7897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			    (pd->port_pkeys[i] & 0x7fff))
7907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				continue;
7917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
7927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ipath_cdbg(VERBOSE, "p%u clear key "
7937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   "%x matches #%d\n",
7947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   pd->port_port,
7957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   pd->port_pkeys[i], j);
7967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ipath_stats.sps_pkeys[j] =
7977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					dd->ipath_pkeys[j] = 0;
7987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				pchanged++;
7997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			}
8007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			else ipath_cdbg(
8017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				VERBOSE, "p%u key %x matches #%d, "
8027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				"but ref still %d\n", pd->port_port,
8037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				pd->port_pkeys[i], j,
8047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				atomic_read(&dd->ipath_pkeyrefs[j]));
8057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			break;
8067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
8077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		pd->port_pkeys[i] = 0;
8087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
8097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (pchanged) {
8107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		u64 pkey = (u64) dd->ipath_pkeys[0] |
8117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			((u64) dd->ipath_pkeys[1] << 16) |
8127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			((u64) dd->ipath_pkeys[2] << 32) |
8137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			((u64) dd->ipath_pkeys[3] << 48);
8147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
8157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   "new pkey reg %llx\n", pd->port_port,
8167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   (unsigned long long) oldpkey,
8177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   (unsigned long long) pkey);
8187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
8197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 pkey);
8207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
8217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
8227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
8239929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan/*
8249929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan * Initialize the port data with the receive buffer sizes
8259929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan * so this can be done while the master port is locked.
8269929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan * Otherwise, there is a race with a slave opening the port
8279929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan * and seeing these fields uninitialized.
8289929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan */
8299929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic void init_user_egr_sizes(struct ipath_portdata *pd)
8309929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan{
8319929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
8329929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	unsigned egrperchunk, egrcnt, size;
8339929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
8349929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/*
8359929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * to avoid wasting a lot of memory, we allocate 32KB chunks of
8369929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * physically contiguous memory, advance through it until used up
8379929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * and then allocate more.  Of course, we need memory to store those
8389929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * extra pointers, now.  Started out with 256KB, but under heavy
8399929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * memory pressure (creating large files and then copying them over
8409929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * NFS while doing lots of MPI jobs), we hit some allocation
8419929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * failures, even though we can sleep...  (2.6.10) Still get
8429929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * failures at 64K.  32K is the lowest we can go without wasting
8439929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * additional memory.
8449929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 */
8459929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	size = 0x8000;
8469929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	egrperchunk = size / dd->ipath_rcvegrbufsize;
8479929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	egrcnt = dd->ipath_rcvegrcnt;
8489929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
8499929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->port_rcvegrbufs_perchunk = egrperchunk;
8509929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->port_rcvegrbuf_size = size;
8519929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan}
8529929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
8537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan/**
8547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * ipath_create_user_egr - allocate eager TID buffers
8557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @pd: the port to allocate TID buffers for
8567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
8577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * This routine is now quite different for user and kernel, because
8587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * the kernel uses skb's, for the accelerated network performance
8597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * This is the user port version
8607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
8617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * Allocate the eager TID buffers and program them into infinipath
8627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * They are no longer completely contiguous, we do multiple allocation
8637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * calls.
8647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan */
8657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int ipath_create_user_egr(struct ipath_portdata *pd)
8667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
8677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
8689929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
8697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	size_t size;
8707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
8710ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan	gfp_t gfp_flags;
8720ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan
8730ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan	/*
8740ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan	 * GFP_USER, but without GFP_FS, so buffer cache can be
8750ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan	 * coalesced (we hope); otherwise, even at order 4,
8760ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan	 * heavy filesystem activity makes these fail, and we can
8770ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan	 * use compound pages.
8780ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan	 */
8790ed9a4a0b6df0548f9ccadb62add2c0155d5262cBryan O'Sullivan	gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
8807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
8817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	egrcnt = dd->ipath_rcvegrcnt;
8827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/* TID number offset for this port */
8837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	egroff = pd->port_port * egrcnt;
8847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	egrsize = dd->ipath_rcvegrbufsize;
8857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
8867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
8877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
8889929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	chunk = pd->port_rcvegrbuf_chunks;
8899929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	egrperchunk = pd->port_rcvegrbufs_perchunk;
8909929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	size = pd->port_rcvegrbuf_size;
8919929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
8929929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				     GFP_KERNEL);
8937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!pd->port_rcvegrbuf) {
8947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENOMEM;
8957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
8967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
8977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pd->port_rcvegrbuf_phys =
8989929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
8999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			GFP_KERNEL);
9007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!pd->port_rcvegrbuf_phys) {
9017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENOMEM;
9027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail_rcvegrbuf;
9037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
9047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
9057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
9067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		pd->port_rcvegrbuf[e] = dma_alloc_coherent(
9077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			&dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
9087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			gfp_flags);
9097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
9107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (!pd->port_rcvegrbuf[e]) {
9117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -ENOMEM;
9127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail_rcvegrbuf_phys;
9137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
9147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
9157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
9167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
9177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
9187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
9197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
9207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		unsigned i;
9217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
9227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
9237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			dd->ipath_f_put_tid(dd, e + egroff +
9247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					    (u64 __iomem *)
9257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					    ((char __iomem *)
9267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					     dd->ipath_kregbase +
927f716cdfe57f217966f41a7add190d6f5b9fd0769Joan Eslinger					     dd->ipath_rcvegrbase),
928f716cdfe57f217966f41a7add190d6f5b9fd0769Joan Eslinger					    RCVHQ_RCV_TYPE_EAGER, pa);
9297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			pa += egrsize;
9307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
9317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		cond_resched();	/* don't hog the cpu */
9327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
9337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
9347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = 0;
9357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	goto bail;
9367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
9377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail_rcvegrbuf_phys:
9387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (e = 0; e < pd->port_rcvegrbuf_chunks &&
939f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		pd->port_rcvegrbuf[e]; e++) {
9407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dma_free_coherent(&dd->pcidev->dev, size,
9417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				  pd->port_rcvegrbuf[e],
9427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				  pd->port_rcvegrbuf_phys[e]);
9437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
944f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	}
9459929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	kfree(pd->port_rcvegrbuf_phys);
9467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pd->port_rcvegrbuf_phys = NULL;
9477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail_rcvegrbuf:
9489929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	kfree(pd->port_rcvegrbuf);
9497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pd->port_rcvegrbuf = NULL;
9507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
9517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
9527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
9537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
954f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan
955f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan/* common code for the mappings on dma_alloc_coherent mem */
956f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivanstatic int ipath_mmap_mem(struct vm_area_struct *vma,
9571fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan	struct ipath_portdata *pd, unsigned len, int write_ok,
9581fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan	void *kvaddr, char *what)
959f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan{
960f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
9611fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan	unsigned long pfn;
962f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	int ret;
963f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan
964f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	if ((vma->vm_end - vma->vm_start) > len) {
965f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		dev_info(&dd->pcidev->dev,
966f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		         "FAIL on %s: len %lx > %x\n", what,
967f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan			 vma->vm_end - vma->vm_start, len);
968f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		ret = -EFAULT;
969f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		goto bail;
970f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	}
971f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan
972f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	if (!write_ok) {
973f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		if (vma->vm_flags & VM_WRITE) {
974f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan			dev_info(&dd->pcidev->dev,
975f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan				 "%s must be mapped readonly\n", what);
976f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan			ret = -EPERM;
977f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan			goto bail;
978f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		}
979f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan
980f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		/* don't allow them to later change with mprotect */
981f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		vma->vm_flags &= ~VM_MAYWRITE;
982f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	}
983f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan
9841fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan	pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
985f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	ret = remap_pfn_range(vma, vma->vm_start, pfn,
986f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan			      len, vma->vm_page_prot);
987f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	if (ret)
9881fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
9891fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			 "bytes r%c failed: %d\n", what, pd->port_port,
9901fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			 pfn, len, write_ok?'w':'o', ret);
991f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	else
9921fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
9931fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			   "r%c\n", what, pd->port_port, pfn, len,
9941fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			   write_ok?'w':'o');
995f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivanbail:
996f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	return ret;
997f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan}
998f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan
9997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
10007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		     u64 ureg)
10017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
10027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unsigned long phys;
10037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
10047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
1005f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	/*
1006f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 * This is real hardware, so use io_remap.  This is the mechanism
1007f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 * for the user process to update the head registers for their port
1008f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 * in the chip.
1009f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 */
10107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
10117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
10127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 "%lx > PAGE\n", vma->vm_end - vma->vm_start);
10137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EFAULT;
10147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	} else {
10157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		phys = dd->ipath_physaddr + ureg;
10167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
10177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
10187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
10197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = io_remap_pfn_range(vma, vma->vm_start,
10207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					 phys >> PAGE_SHIFT,
10217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					 vma->vm_end - vma->vm_start,
10227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					 vma->vm_page_prot);
10237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
10247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
10257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
10267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
10277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int mmap_piobufs(struct vm_area_struct *vma,
10287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			struct ipath_devdata *dd,
10299929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			struct ipath_portdata *pd,
10309929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			unsigned piobufs, unsigned piocnt)
10317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
10327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unsigned long phys;
10337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
10347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
10357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
1036f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 * When we map the PIO buffers in the chip, we want to map them as
1037f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 * writeonly, no read possible.   This prevents access to previous
1038f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 * process data, and catches users who might try to read the i/o
1039f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 * space due to a bug.
10407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
10419929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
10427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
10437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 "reqlen %lx > PAGE\n",
10447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 vma->vm_end - vma->vm_start);
10459929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EINVAL;
10467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
10477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
10487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
10499929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	phys = dd->ipath_physaddr + piobufs;
1050f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan
10517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
1052f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	 * Don't mark this as non-cached, or we don't get the
10537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * write combining behavior we want on the PIO buffers!
10547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
10557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
1056eb9dc6f48dc7537ce53163109625bd992150e0cfBryan O'Sullivan#if defined(__powerpc__)
1057eb9dc6f48dc7537ce53163109625bd992150e0cfBryan O'Sullivan	/* There isn't a generic way to specify writethrough mappings */
1058eb9dc6f48dc7537ce53163109625bd992150e0cfBryan O'Sullivan	pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1059eb9dc6f48dc7537ce53163109625bd992150e0cfBryan O'Sullivan	pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
1060eb9dc6f48dc7537ce53163109625bd992150e0cfBryan O'Sullivan	pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
1061eb9dc6f48dc7537ce53163109625bd992150e0cfBryan O'Sullivan#endif
1062eb9dc6f48dc7537ce53163109625bd992150e0cfBryan O'Sullivan
1063367fe711c5dc85dbc3265cf01e34d4d6fbd55f06Bryan O'Sullivan	/*
1064367fe711c5dc85dbc3265cf01e34d4d6fbd55f06Bryan O'Sullivan	 * don't allow them to later change to readable with mprotect (for when
1065367fe711c5dc85dbc3265cf01e34d4d6fbd55f06Bryan O'Sullivan	 * not initially mapped readable, as is normally the case)
1066367fe711c5dc85dbc3265cf01e34d4d6fbd55f06Bryan O'Sullivan	 */
1067f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	vma->vm_flags &= ~VM_MAYREAD;
10687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
10697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
10707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
10717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 vma->vm_end - vma->vm_start,
10727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 vma->vm_page_prot);
10737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
10747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
10757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
10767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
10777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int mmap_rcvegrbufs(struct vm_area_struct *vma,
10787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   struct ipath_portdata *pd)
10797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
10807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
10817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unsigned long start, size;
10827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	size_t total_size, i;
10831fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan	unsigned long pfn;
10847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
10857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
10867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	size = pd->port_rcvegrbuf_size;
10877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	total_size = pd->port_rcvegrbuf_chunks * size;
10887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if ((vma->vm_end - vma->vm_start) > total_size) {
10897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
10907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 "reqlen %lx > actual %lx\n",
10917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 vma->vm_end - vma->vm_start,
10927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 (unsigned long) total_size);
10939929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EINVAL;
10947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
10957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
10967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
10977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (vma->vm_flags & VM_WRITE) {
10987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
10997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 "writable (flags=%lx)\n", vma->vm_flags);
11007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EPERM;
11017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
11027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
1103f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	/* don't allow them to later change to writeable with mprotect */
1104f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	vma->vm_flags &= ~VM_MAYWRITE;
11057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
11067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	start = vma->vm_start;
11077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
11087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
11091fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
11101fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		ret = remap_pfn_range(vma, start, pfn, size,
11111fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan				      vma->vm_page_prot);
11127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (ret < 0)
11137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
11147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
11157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = 0;
11167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
11177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
11187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
11197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
11207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
11219929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan/*
11229929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan * ipath_file_vma_nopage - handle a VMA page fault.
11239929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan */
11249929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic struct page *ipath_file_vma_nopage(struct vm_area_struct *vma,
11259929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan					  unsigned long address, int *type)
11269929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan{
11279929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	unsigned long offset = address - vma->vm_start;
11289929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	struct page *page = NOPAGE_SIGBUS;
11299929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	void *pageptr;
11309929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
11319929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/*
11329929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * Convert the vmalloc address into a struct page.
11339929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 */
11349929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pageptr = (void *)(offset + (vma->vm_pgoff << PAGE_SHIFT));
11359929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	page = vmalloc_to_page(pageptr);
11369929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!page)
11379929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto out;
11389929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
11399929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* Increment the reference count. */
11409929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	get_page(page);
11419929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (type)
11429929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		*type = VM_FAULT_MINOR;
11439929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanout:
11449929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	return page;
11459929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan}
11469929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
11479929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic struct vm_operations_struct ipath_file_vm_ops = {
11489929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	.nopage = ipath_file_vma_nopage,
11499929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan};
11509929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
11519929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
11529929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		       struct ipath_portdata *pd, unsigned subport)
11539929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan{
11549929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	unsigned long len;
11559929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	struct ipath_devdata *dd;
11569929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	void *addr;
11579929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	size_t size;
11580a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	int ret = 0;
11599929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
11609929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* If the port is not shared, all addresses should be physical */
11610a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	if (!pd->port_subport_cnt)
11629929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
11639929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
11649929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	dd = pd->port_dd;
11659929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
11669929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
11679929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/*
1168c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	 * Each process has all the subport uregbase, rcvhdrq, and
1169c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	 * rcvegrbufs mmapped - as an array for all the processes,
1170c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	 * and also separately for this process.
11719929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 */
1172c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
1173c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		addr = pd->subport_uregbase;
1174c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		size = PAGE_SIZE * pd->port_subport_cnt;
1175c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	} else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
1176c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		addr = pd->subport_rcvhdr_base;
1177c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		size = pd->port_rcvhdrq_size * pd->port_subport_cnt;
1178c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	} else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
1179c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		addr = pd->subport_rcvegrbuf;
1180c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		size *= pd->port_subport_cnt;
1181c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage        } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
1182c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                                        PAGE_SIZE * subport)) {
1183c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                addr = pd->subport_uregbase + PAGE_SIZE * subport;
1184c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                size = PAGE_SIZE;
1185c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage        } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
1186c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                                pd->port_rcvhdrq_size * subport)) {
1187c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                addr = pd->subport_rcvhdr_base +
1188c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                        pd->port_rcvhdrq_size * subport;
1189c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                size = pd->port_rcvhdrq_size;
1190c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage        } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
1191c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                               size * subport)) {
1192c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                addr = pd->subport_rcvegrbuf + size * subport;
1193c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                /* rcvegrbufs are read-only on the slave */
1194c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                if (vma->vm_flags & VM_WRITE) {
1195c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                        dev_info(&dd->pcidev->dev,
1196c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                                 "Can't map eager buffers as "
1197c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                                 "writable (flags=%lx)\n", vma->vm_flags);
1198c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                        ret = -EPERM;
1199c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                        goto bail;
1200c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                }
1201c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                /*
1202c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                 * Don't allow permission to later change to writeable
1203c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                 * with mprotect.
1204c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                 */
1205c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                vma->vm_flags &= ~VM_MAYWRITE;
1206c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	} else {
12079929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
1208c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	}
12099929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	len = vma->vm_end - vma->vm_start;
12109929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (len > size) {
12119929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
12129929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EINVAL;
12139929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
12149929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
12159929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
12169929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
12179929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	vma->vm_ops = &ipath_file_vm_ops;
12189929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
12190a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	ret = 1;
12209929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
12219929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanbail:
12229929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	return ret;
12239929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan}
12249929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
12257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan/**
12267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * ipath_mmap - mmap various structures into user space
12277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @fp: the file pointer
12287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @vma: the VM area
12297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
12307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * We use this to have a shared buffer between the kernel and the user code
12317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
12327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * buffers in the chip.  We have the open and close entries so we can bump
12337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * the ref count and keep the driver from being unloaded while still mapped.
12347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan */
12357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
12367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
12377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_portdata *pd;
12387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd;
12397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u64 pgaddr, ureg;
12409929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	unsigned piobufs, piocnt;
12417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
12427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
12437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pd = port_fp(fp);
12449929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd) {
12459929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EINVAL;
12469929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
12479929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
12487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	dd = pd->port_dd;
1249f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan
12507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
12517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * This is the ipath_do_user_init() code, mapping the shared buffers
12527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * into the user process. The address referred to by vm_pgoff is the
12539929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * file offset passed via mmap().  For shared ports, this is the
12549929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * kernel vmalloc() address of the pages to share with the master.
12559929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * For non-shared or master ports, this is a physical address.
12569929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * We only do one mmap for each space mapped.
12577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
12587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pgaddr = vma->vm_pgoff << PAGE_SHIFT;
12597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
12607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
12619929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * Check for 0 in case one of the allocations failed, but user
12629929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * called mmap anyway.
12637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
12649929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pgaddr)  {
12659929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EINVAL;
12669929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
1267f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	}
12687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
12699929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
12707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   (unsigned long long) pgaddr, vma->vm_start,
12719929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		   vma->vm_end - vma->vm_start, dd->ipath_unit,
12729929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		   pd->port_port, subport_fp(fp));
12737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
12749929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/*
12759929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * Physical addresses must fit in 40 bits for our hardware.
12769929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * Check for kernel virtual addresses first, anything else must
12779929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * match a HW or memory address.
12789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 */
12790a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
12800a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	if (ret) {
12810a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell		if (ret > 0)
12820a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell			ret = 0;
12839929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
12849929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
12859929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
1286c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
12879929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd->port_subport_cnt) {
12889929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		/* port is not shared */
12899929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		piocnt = dd->ipath_pbufsport;
12909929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		piobufs = pd->port_piobufs;
12919929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else if (!subport_fp(fp)) {
12929929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		/* caller is the master */
12939929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		piocnt = (dd->ipath_pbufsport / pd->port_subport_cnt) +
12949929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 (dd->ipath_pbufsport % pd->port_subport_cnt);
12959929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		piobufs = pd->port_piobufs +
12969929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			dd->ipath_palign * (dd->ipath_pbufsport - piocnt);
12979929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else {
12989929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		unsigned slave = subport_fp(fp) - 1;
12999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
13009929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		/* caller is a slave */
13019929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		piocnt = dd->ipath_pbufsport / pd->port_subport_cnt;
13029929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
1303f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	}
13049929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
13059929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (pgaddr == ureg)
13067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = mmap_ureg(vma, dd, ureg);
13079929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	else if (pgaddr == piobufs)
13089929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
13099929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	else if (pgaddr == dd->ipath_pioavailregs_phys)
13109929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		/* in-memory copy of pioavail registers */
13119929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
13121fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			      	     (void *) dd->ipath_pioavailregs_dma,
13139929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				     "pioavail registers");
13149929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	else if (pgaddr == pd->port_rcvegr_phys)
13157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = mmap_rcvegrbufs(vma, pd);
13169929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
1317f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		/*
1318525d0ca1d452ed336c1d907fb20c104467a8a47bBryan O'Sullivan		 * The rcvhdrq itself; readonly except on HT (so have
1319f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		 * to allow writable mapping), multiple pages, contiguous
1320f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		 * from an i/o perspective.
1321f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		 */
13229929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
13231fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan				     pd->port_rcvhdrq,
1324f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan				     "rcvhdrq");
13259929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
1326f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		/* in-memory copy of rcvhdrq tail register */
1327f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan		ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
13281fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan				     pd->port_rcvhdrtail_kvaddr,
1329f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan				     "rcvhdrq tail");
13307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	else
13317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EINVAL;
13327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	vma->vm_private_data = NULL;
13347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret < 0)
13367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dev_info(&dd->pcidev->dev,
13379929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 "Failure %d on off %llx len %lx\n",
13389929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 -ret, (unsigned long long)pgaddr,
13399929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 vma->vm_end - vma->vm_start);
13409929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanbail:
13417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
13427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
13437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic unsigned int ipath_poll(struct file *fp,
13457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			       struct poll_table_struct *pt)
13467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
13477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_portdata *pd;
13487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	u32 head, tail;
13497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int bit;
1350e35d710d0c5b74bc9833d6a3791706bd577a3724Bryan O'Sullivan	unsigned pollflag = 0;
13517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd;
13527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pd = port_fp(fp);
13549929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd)
13559929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
13567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	dd = pd->port_dd;
13577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	bit = pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT;
13597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	set_bit(bit, &dd->ipath_rcvctrl);
13607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
13627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * Before blocking, make sure that head is still == tail,
13637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * reading from the chip, so we can be sure the interrupt
13647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * enable has made it to the chip.  If not equal, disable
13657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * interrupt again and return immediately.  This avoids races,
13667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * and the overhead of the chip read doesn't matter much at
13677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * this point, since we are waiting for something anyway.
13687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
13697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
13717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 dd->ipath_rcvctrl);
13727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
13747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
13757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (tail == head) {
13777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
13789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
13799dcc0e58e2913f7e6ffba64c27fe5c2f2c7b845cBryan O'Sullivan			(void)ipath_write_ureg(dd, ur_rcvhdrhead,
13809dcc0e58e2913f7e6ffba64c27fe5c2f2c7b845cBryan O'Sullivan					       dd->ipath_rhdrhead_intr_off
13819dcc0e58e2913f7e6ffba64c27fe5c2f2c7b845cBryan O'Sullivan					       | head, pd->port_port);
13827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		poll_wait(fp, &pd->port_wait, pt);
13837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (test_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag)) {
13857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			/* timed out, no packets received */
13867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			clear_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
13877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			pd->port_rcvwait_to++;
13887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
1389e35d710d0c5b74bc9833d6a3791706bd577a3724Bryan O'Sullivan		else
1390e35d710d0c5b74bc9833d6a3791706bd577a3724Bryan O'Sullivan			pollflag = POLLIN | POLLRDNORM;
13917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
13927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	else {
13937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/* it's already happened; don't do wait_event overhead */
1394e35d710d0c5b74bc9833d6a3791706bd577a3724Bryan O'Sullivan		pollflag = POLLIN | POLLRDNORM;
13957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		pd->port_rcvnowait++;
13967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
13977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
13987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	clear_bit(bit, &dd->ipath_rcvctrl);
13997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
14007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 dd->ipath_rcvctrl);
14017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
14029929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanbail:
1403e35d710d0c5b74bc9833d6a3791706bd577a3724Bryan O'Sullivan	return pollflag;
14047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
14057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
14060df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbagestatic int ipath_supports_subports(int user_swmajor, int user_swminor)
14070df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage{
14080df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	/* no subport implementation prior to software version 1.3 */
14090df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	return (user_swmajor > 1) || (user_swminor >= 3);
14100df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage}
14110df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage
14120df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbagestatic int ipath_compatible_subports(int user_swmajor, int user_swminor)
14130df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage{
14140df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	/* this code is written long-hand for clarity */
14150df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	if (IPATH_USER_SWMAJOR != user_swmajor) {
14160df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		/* no promise of compatibility if major mismatch */
14170df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		return 0;
14180df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	}
14190df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	if (IPATH_USER_SWMAJOR == 1) {
14200df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		switch (IPATH_USER_SWMINOR) {
14210df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		case 0:
14220df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		case 1:
14230df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		case 2:
14240df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			/* no subport implementation so cannot be compatible */
14250df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			return 0;
14260df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		case 3:
14270df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			/* 3 is only compatible with itself */
14280df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			return user_swminor == 3;
14290df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		default:
14300df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			/* >= 4 are compatible (or are expected to be) */
14310df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			return user_swminor >= 4;
14320df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		}
14330df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	}
14340df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	/* make no promises yet for future major versions */
14350df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	return 0;
14360df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage}
14370df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage
14389929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int init_subports(struct ipath_devdata *dd,
14399929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 struct ipath_portdata *pd,
14409929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 const struct ipath_user_info *uinfo)
14419929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan{
14429929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	int ret = 0;
1443c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	unsigned num_subports;
14449929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	size_t size;
14459929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
14469929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/*
14479929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * If the user is requesting zero or one port,
14489929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * skip the subport allocation.
14499929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 */
14509929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (uinfo->spu_subport_cnt <= 1)
14519929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
1452c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage
14530df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	/* Self-consistency check for ipath_compatible_subports() */
14540df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&
14550df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	    !ipath_compatible_subports(IPATH_USER_SWMAJOR,
14560df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage				       IPATH_USER_SWMINOR)) {
1457c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		dev_info(&dd->pcidev->dev,
14580df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			 "Inconsistent ipath_compatible_subports()\n");
14590df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		goto bail;
14600df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	}
14610df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage
14620df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	/* Check for subport compatibility */
14630df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,
14640df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage				       uinfo->spu_userversion & 0xffff)) {
14650df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage		dev_info(&dd->pcidev->dev,
14660df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			 "Mismatched user version (%d.%d) and driver "
14670df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			 "version (%d.%d) while port sharing. Ensure "
1468c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                         "that driver and library are from the same "
1469c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                         "release.\n",
14700df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			 (int) (uinfo->spu_userversion >> 16),
1471c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage                         (int) (uinfo->spu_userversion & 0xffff),
14720df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage			 IPATH_USER_SWMAJOR,
1473c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	                 IPATH_USER_SWMINOR);
1474c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		goto bail;
1475c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	}
14760a5a83cffc03592c2102ad07b7532b596a16f8cdRalph Campbell	if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
14779929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EINVAL;
14789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
14799929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
14809929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
1481c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	num_subports = uinfo->spu_subport_cnt;
1482c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	pd->subport_uregbase = vmalloc(PAGE_SIZE * num_subports);
14839929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd->subport_uregbase) {
14849929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -ENOMEM;
14859929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
14869929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
14879929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* Note: pd->port_rcvhdrq_size isn't initialized yet. */
14889929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
1489c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage		     sizeof(u32), PAGE_SIZE) * num_subports;
14909929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->subport_rcvhdr_base = vmalloc(size);
14919929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd->subport_rcvhdr_base) {
14929929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -ENOMEM;
14939929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail_ureg;
14949929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
14959929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
14969929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->subport_rcvegrbuf = vmalloc(pd->port_rcvegrbuf_chunks *
14979929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan					pd->port_rcvegrbuf_size *
1498c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage					num_subports);
14999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd->subport_rcvegrbuf) {
15009929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -ENOMEM;
15019929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail_rhdr;
15029929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
15039929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
15049929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->port_subport_cnt = uinfo->spu_subport_cnt;
15059929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->port_subport_id = uinfo->spu_subport_id;
15069929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->active_slaves = 1;
1507947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell	set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1508c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	memset(pd->subport_uregbase, 0, PAGE_SIZE * num_subports);
1509c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	memset(pd->subport_rcvhdr_base, 0, size);
1510c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	memset(pd->subport_rcvegrbuf, 0, pd->port_rcvegrbuf_chunks *
1511c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage				         pd->port_rcvegrbuf_size *
1512c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage				         num_subports);
15139929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	goto bail;
15149929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
15159929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanbail_rhdr:
15169929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	vfree(pd->subport_rcvhdr_base);
15179929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanbail_ureg:
15189929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	vfree(pd->subport_uregbase);
15199929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->subport_uregbase = NULL;
15209929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanbail:
15219929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	return ret;
15229929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan}
15239929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
15247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int try_alloc_port(struct ipath_devdata *dd, int port,
15259929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			  struct file *fp,
15269929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			  const struct ipath_user_info *uinfo)
15277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
15289929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	struct ipath_portdata *pd;
15297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
15307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
15319929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!(pd = dd->ipath_pd[port])) {
15329929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		void *ptmp;
15337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
15349929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
15357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
15367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/*
15377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * Allocate memory for use in ipath_tid_update() just once
15387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * at open, not per call.  Reduces cost of expected send
15397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 * setup.
15407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		 */
15417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
15427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			       dd->ipath_rcvtidcnt * sizeof(struct page **),
15437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			       GFP_KERNEL);
15449929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		if (!pd || !ptmp) {
15457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_dev_err(dd, "Unable to allocate portdata "
15467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				      "memory, failing open\n");
15477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -ENOMEM;
15489929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			kfree(pd);
15497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			kfree(ptmp);
15507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
15517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
15529929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		dd->ipath_pd[port] = pd;
15537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dd->ipath_pd[port]->port_port = port;
15547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dd->ipath_pd[port]->port_dd = dd;
15557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dd->ipath_pd[port]->port_tid_pg_list = ptmp;
15567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
15577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
15589929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd->port_cnt) {
15599929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		pd->userversion = uinfo->spu_userversion;
15609929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		init_user_egr_sizes(pd);
15619929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		if ((ret = init_subports(dd, pd, uinfo)) != 0)
15629929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			goto bail;
15637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
15647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   current->comm, current->pid, dd->ipath_unit,
15657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   port);
15669929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		pd->port_cnt = 1;
15679929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		port_fp(fp) = pd;
15689929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		pd->port_pid = current->pid;
15699929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		strncpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
15707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_stats.sps_ports++;
15717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = 0;
15729929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else
15739929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EBUSY;
15747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
15757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
15767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
15777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
15787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
15797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic inline int usable(struct ipath_devdata *dd)
15807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
15817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return dd &&
15827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		(dd->ipath_flags & IPATH_PRESENT) &&
15837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dd->ipath_kregbase &&
15847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dd->ipath_lid &&
15857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		!(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
15867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				     | IPATH_LINKUNK));
15877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
15887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
15899929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int find_free_port(int unit, struct file *fp,
15909929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			  const struct ipath_user_info *uinfo)
15917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
15927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = ipath_lookup(unit);
15937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret, i;
15947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
15957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!dd) {
15967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENODEV;
15977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
15987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
15997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!usable(dd)) {
16017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENETDOWN;
16027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
16037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
16047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16059929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	for (i = 1; i < dd->ipath_cfgports; i++) {
16069929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = try_alloc_port(dd, i, fp, uinfo);
16077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (ret != -EBUSY)
16087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
16097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
16107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = -EBUSY;
16117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
16137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
16147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
16157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16169929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int find_best_unit(struct file *fp,
16179929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			  const struct ipath_user_info *uinfo)
16187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
16197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret = 0, i, prefunit = -1, devmax;
16207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int maxofallports, npresent, nup;
16217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ndev;
16227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16239929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	devmax = ipath_count_units(&npresent, &nup, &maxofallports);
16247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
16267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * This code is present to allow a knowledgeable person to
16277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * specify the layout of processes to processors before opening
16287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * this driver, and then we'll assign the process to the "closest"
1629525d0ca1d452ed336c1d907fb20c104467a8a47bBryan O'Sullivan	 * InfiniPath chip to that processor (we assume reasonable connectivity,
16307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * for now).  This code assumes that if affinity has been set
16317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * before this point, that at most one cpu is set; for now this
16327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * is reasonable.  I check for both cpus_empty() and cpus_full(),
16337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * in case some kernel variant sets none of the bits when no
16347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * affinity is set.  2.6.11 and 12 kernels have all present
16357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * cpus set.  Some day we'll have to fix it up further to handle
1636525d0ca1d452ed336c1d907fb20c104467a8a47bBryan O'Sullivan	 * a cpu subset.  This algorithm fails for two HT chips connected
16377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * in tunnel fashion.  Eventually this needs real topology
16387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * information.  There may be some issues with dual core numbering
16397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * as well.  This needs more work prior to release.
16407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
16417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!cpus_empty(current->cpus_allowed) &&
16427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	    !cpus_full(current->cpus_allowed)) {
1643f0810daf74c564a3615eba5002cc11c21a0949baBryan O'Sullivan		int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
16447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		for (i = 0; i < ncpus; i++)
16457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (cpu_isset(i, current->cpus_allowed)) {
16467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ipath_cdbg(PROC, "%s[%u] affinity set for "
1647f0810daf74c564a3615eba5002cc11c21a0949baBryan O'Sullivan					   "cpu %d/%d\n", current->comm,
1648f0810daf74c564a3615eba5002cc11c21a0949baBryan O'Sullivan					   current->pid, i, ncpus);
16497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				curcpu = i;
1650f0810daf74c564a3615eba5002cc11c21a0949baBryan O'Sullivan				nset++;
16517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			}
1652f0810daf74c564a3615eba5002cc11c21a0949baBryan O'Sullivan		if (curcpu != -1 && nset != ncpus) {
16537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (npresent) {
16547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				prefunit = curcpu / (ncpus / npresent);
1655c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage				ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
16567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					  "%d cpus/chip, select unit %d\n",
16577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					  current->comm, current->pid,
16587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					  npresent, ncpus, ncpus / npresent,
16597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					  prefunit);
16607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			}
16617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
16627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
16637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	/*
16657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * user ports start at 1, kernel port is 0
16667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 * For now, we do round-robin access across all chips
16677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	 */
16687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (prefunit != -1)
16707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		devmax = prefunit + 1;
16717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanrecheck:
16727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (i = 1; i < maxofallports; i++) {
16737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
16747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		     ndev++) {
16757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			struct ipath_devdata *dd = ipath_lookup(ndev);
16767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (!usable(dd))
16787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				continue; /* can't use this unit */
16797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (i >= dd->ipath_cfgports)
16807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				/*
16817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 * Maxed out on users of this unit. Try
16827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 * next.
16837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				 */
16847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				continue;
16859929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			ret = try_alloc_port(dd, i, fp, uinfo);
16867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (!ret)
16877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				goto done;
16887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
16897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
16907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
16917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (npresent) {
16927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (nup == 0) {
16937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -ENETDOWN;
16947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_dbg("No ports available (none initialized "
16957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				  "and ready)\n");
16967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		} else {
16977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			if (prefunit > 0) {
16987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				/* if started above 0, retry from 0 */
16997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				ipath_cdbg(PROC,
17007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   "%s[%u] no ports on prefunit "
17017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   "%d, clear and re-check\n",
17027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   current->comm, current->pid,
17037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan					   prefunit);
17047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				devmax = ipath_count_units(NULL, NULL,
17057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan							   NULL);
17067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				prefunit = -1;
17077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				goto recheck;
17087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			}
17097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ret = -EBUSY;
17107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_dbg("No ports available\n");
17117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
17127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	} else {
17137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENXIO;
17147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("No boards found\n");
17157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
17167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
17177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivandone:
17187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
17197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
17207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
17219929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int find_shared_port(struct file *fp,
17229929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			    const struct ipath_user_info *uinfo)
17239929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan{
17249929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	int devmax, ndev, i;
17259929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	int ret = 0;
17269929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
17279929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	devmax = ipath_count_units(NULL, NULL, NULL);
17289929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
17299929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	for (ndev = 0; ndev < devmax; ndev++) {
17309929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		struct ipath_devdata *dd = ipath_lookup(ndev);
17319929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
17329929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		if (!dd)
17339929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			continue;
17349929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		for (i = 1; i < dd->ipath_cfgports; i++) {
17359929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			struct ipath_portdata *pd = dd->ipath_pd[i];
17369929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
17379929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			/* Skip ports which are not yet open */
17389929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			if (!pd || !pd->port_cnt)
17399929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				continue;
17409929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			/* Skip port if it doesn't match the requested one */
17419929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			if (pd->port_subport_id != uinfo->spu_subport_id)
17429929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				continue;
17439929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			/* Verify the sharing process matches the master */
17449929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
17459929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			    pd->userversion != uinfo->spu_userversion ||
17469929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			    pd->port_cnt >= pd->port_subport_cnt) {
17479929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				ret = -EINVAL;
17489929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				goto done;
17499929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			}
17509929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			port_fp(fp) = pd;
17519929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			subport_fp(fp) = pd->port_cnt++;
17529929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			tidcursor_fp(fp) = 0;
17539929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			pd->active_slaves |= 1 << subport_fp(fp);
17549929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			ipath_cdbg(PROC,
17559929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				   "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
17569929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				   current->comm, current->pid,
17579929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				   subport_fp(fp),
17589929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				   pd->port_comm, pd->port_pid,
17599929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				   dd->ipath_unit, pd->port_port);
17609929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			ret = 1;
17619929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			goto done;
17629929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		}
17639929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
17649929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
17659929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivandone:
17669929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	return ret;
17679929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan}
17689929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
17697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int ipath_open(struct inode *in, struct file *fp)
17707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
1771c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	/* The real work is performed later in ipath_assign_port() */
17729929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
17739929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	return fp->private_data ? 0 : -ENOMEM;
17749929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan}
17759929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
1776c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan/* Get port early, so can set affinity prior to memory allocation */
1777c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivanstatic int ipath_assign_port(struct file *fp,
17789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			      const struct ipath_user_info *uinfo)
17799929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan{
17809929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	int ret;
17819929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	int i_minor;
17820df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	unsigned swmajor, swminor;
17839929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
17849929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* Check to be sure we haven't already initialized this file */
17859929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (port_fp(fp)) {
17869929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EINVAL;
17879929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto done;
17889929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
17899929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
17909929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* for now, if major version is different, bail */
17910df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	swmajor = uinfo->spu_userversion >> 16;
17920df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	if (swmajor != IPATH_USER_SWMAJOR) {
17939929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ipath_dbg("User major version %d not same as driver "
17949929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			  "major %d\n", uinfo->spu_userversion >> 16,
17959929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			  IPATH_USER_SWMAJOR);
17969929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -ENODEV;
17979929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto done;
17989929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
17999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
18009929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	swminor = uinfo->spu_userversion & 0xffff;
18019929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (swminor != IPATH_USER_SWMINOR)
18029929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ipath_dbg("User minor version %d not same as driver "
18039929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			  "minor %d\n", swminor, IPATH_USER_SWMINOR);
18047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
18057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	mutex_lock(&ipath_mutex);
18067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
18070df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	if (ipath_compatible_subports(swmajor, swminor) &&
18080df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	    uinfo->spu_subport_cnt &&
18099929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	    (ret = find_shared_port(fp, uinfo))) {
18109929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		mutex_unlock(&ipath_mutex);
18119929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		if (ret > 0)
18129929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			ret = 0;
18139929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto done;
18149929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
18159929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
18161cfd6e648b5ea21b0d48bf6f18b129e4576557d5Josef Sipek	i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;
18177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
18181cfd6e648b5ea21b0d48bf6f18b129e4576557d5Josef Sipek		   (long)fp->f_path.dentry->d_inode->i_rdev, i_minor);
18197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
18209929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (i_minor)
18219929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = find_free_port(i_minor - 1, fp, uinfo);
18227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	else
18239929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = find_best_unit(fp, uinfo);
18247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
18257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	mutex_unlock(&ipath_mutex);
18269929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
1827c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivandone:
1828c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	return ret;
1829c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan}
1830c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan
1831c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan
1832c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivanstatic int ipath_do_user_init(struct file *fp,
1833c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan			      const struct ipath_user_info *uinfo)
1834c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan{
1835c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	int ret;
1836947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell	struct ipath_portdata *pd = port_fp(fp);
1837c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	struct ipath_devdata *dd;
1838c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	u32 head32;
18399929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
1840947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell	/* Subports don't need to initialize anything since master did it. */
1841947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell	if (subport_fp(fp)) {
1842947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell		ret = wait_event_interruptible(pd->port_wait,
1843947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell			!test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
1844947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell		goto done;
1845947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell	}
1846947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell
18479929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	dd = pd->port_dd;
18489929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
18499929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (uinfo->spu_rcvhdrsize) {
18509929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
18519929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		if (ret)
18529929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			goto done;
18539929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
18549929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
18559929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
18569929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
18579929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* for right now, kernel piobufs are at end, so port 1 is at 0 */
18589929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->port_piobufs = dd->ipath_piobufbase +
18599929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		dd->ipath_pbufsport * (pd->port_port - 1) * dd->ipath_palign;
18609929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n",
18619929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		   pd->port_port, pd->port_piobufs);
18629929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
18639929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/*
18649929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
18659929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * array for time being.  If pd->port_port > chip-supported,
18669929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * we need to do extra stuff here to handle by handling overflow
18679929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * through port 0, someday
18689929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 */
18699929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	ret = ipath_create_rcvhdrq(dd, pd);
18709929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!ret)
18719929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_create_user_egr(pd);
18729929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (ret)
18739929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto done;
18749929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
18759929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/*
18769929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * set the eager head register for this port to the current values
18779929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * of the tail pointers, since we don't know if they were
18789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * updated on last use of the port.
18799929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 */
18809929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
18819929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
18829929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	dd->ipath_lastegrheads[pd->port_port] = -1;
18839929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
18849929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
18859929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		pd->port_port, head32);
18869929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd->port_tidcursor = 0;	/* start at beginning after open */
18879929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/*
18889929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * now enable the port; the tail registers will be written to memory
18899929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * by the chip as soon as it sees the write to
18909929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * dd->ipath_kregs->kr_rcvctrl.  The update only happens on
18919929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * transition from 0 to 1, so clear it first, then set it as part of
18929929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * enabling the port.  This will (very briefly) affect any other
18939929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * open ports, but it shouldn't be long enough to be an issue.
18949929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * We explictly set the in-memory copy to 0 beforehand, so we don't
18959929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 * have to wait to be sure the DMA update has happened.
18969929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	 */
18971fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan	*(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0ULL;
18989929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
18999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		&dd->ipath_rcvctrl);
19009929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
19019929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD);
19029929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
19039929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			 dd->ipath_rcvctrl);
1904947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell	/* Notify any waiting slaves */
1905947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell	if (pd->port_subport_cnt) {
1906947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell		clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1907947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell		wake_up(&pd->port_wait);
1908947d7617a1d876c2c93f73017a734e070c64d43bRalph Campbell	}
19099929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivandone:
19107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
19117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
19127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan/**
19147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * unlock_exptid - unlock any expected TID entries port still had in use
19157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * @pd: port
19167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan *
19177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * We don't actually update the chip here, because we do a bulk update
19187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan * below, using ipath_f_clear_tids.
19197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan */
19207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic void unlock_expected_tids(struct ipath_portdata *pd)
19217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
19227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd = pd->port_dd;
19237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
19247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
19257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
19277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   pd->port_port);
19287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	for (i = port_tidbase; i < maxtid; i++) {
19297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (!dd->ipath_pageshadow[i])
19307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			continue;
19317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19321fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
19331fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan			PAGE_SIZE, PCI_DMA_FROMDEVICE);
19347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i],
19357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan						  1);
19367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dd->ipath_pageshadow[i] = NULL;
19377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		cnt++;
19387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_stats.sps_pageunlocks++;
19397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
19407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (cnt)
19417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
19427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   pd->port_port, cnt);
19437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
19457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
19467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   (unsigned long long) ipath_stats.sps_pagelocks,
19477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   (unsigned long long)
19487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   ipath_stats.sps_pageunlocks);
19497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
19507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int ipath_close(struct inode *in, struct file *fp)
19527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
19537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret = 0;
19549929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	struct ipath_filedata *fd;
19557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_portdata *pd;
19567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_devdata *dd;
19577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unsigned port;
19587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
19607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		   (long)in->i_rdev, fp->private_data);
19617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	mutex_lock(&ipath_mutex);
19637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19649929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	fd = (struct ipath_filedata *) fp->private_data;
19657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	fp->private_data = NULL;
19669929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	pd = fd->pd;
19679929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (!pd) {
19689929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		mutex_unlock(&ipath_mutex);
19699929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
19709929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
19719929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (--pd->port_cnt) {
19729929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		/*
19739929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		 * XXX If the master closes the port before the slave(s),
19749929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		 * revoke the mmap for the eager receive queue so
19759929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		 * the slave(s) don't wait for receive data forever.
19769929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		 */
19779929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		pd->active_slaves &= ~(1 << fd->subport);
19789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		mutex_unlock(&ipath_mutex);
19799929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
19809929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
19819929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	port = pd->port_port;
19827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	dd = pd->port_dd;
19837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (pd->port_hdrqfull) {
19857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
19867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   "during run\n", pd->port_comm, pd->port_pid,
19877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   pd->port_hdrqfull);
19887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		pd->port_hdrqfull = 0;
19897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
19907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
19917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (pd->port_rcvwait_to || pd->port_piowait_to
19927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	    || pd->port_rcvnowait || pd->port_pionowait) {
19937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
19947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   "%u rcv %u, pio already\n",
19957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   pd->port_port, pd->port_rcvwait_to,
19967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   pd->port_piowait_to, pd->port_rcvnowait,
19977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   pd->port_pionowait);
19987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		pd->port_rcvwait_to = pd->port_piowait_to =
19997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			pd->port_rcvnowait = pd->port_pionowait = 0;
20007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
20017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (pd->port_flag) {
20027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dbg("port %u port_flag still set to 0x%lx\n",
20037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			  pd->port_port, pd->port_flag);
20047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		pd->port_flag = 0;
20057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
20067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (dd->ipath_kregbase) {
200835783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		int i;
200935783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		/* atomically clear receive enable port. */
201035783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port,
201135783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan			  &dd->ipath_rcvctrl);
201235783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
201335783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan			dd->ipath_rcvctrl);
201435783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		/* and read back from chip to be sure that nothing
201535783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 * else is in flight when we do the rest */
201635783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		(void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
20177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		/* clean up the pkeys for this port user */
20197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_clean_part_key(pd, dd);
202035783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		/*
202135783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 * be paranoid, and never write 0's to these, just use an
202235783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 * unused part of the port 0 tail page.  Of course,
202335783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 * rcvhdraddr points to a large chunk of memory, so this
202435783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 * could still trash things, but at least it won't trash
202535783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 * page 0, and by disabling the port, it should stop "soon",
202635783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 * even if a packet or two is in already in flight after we
202735783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 * disabled the port.
202835783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		 */
202935783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		ipath_write_kreg_port(dd,
203035783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		        dd->ipath_kregs->kr_rcvhdrtailaddr, port,
203135783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan			dd->ipath_dummy_hdrq_phys);
203235783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
203335783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan			pd->port_port, dd->ipath_dummy_hdrq_phys);
203435783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan
203535783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		i = dd->ipath_pbufsport * (port - 1);
203635783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport);
203735783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan
20381fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan		dd->ipath_f_clear_tids(dd, pd->port_port);
20391fd3b40fde3bfacdf742cadfe99cfd47ffd05219Bryan O'Sullivan
204035783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		if (dd->ipath_pageshadow)
204135783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan			unlock_expected_tids(pd);
204235783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		ipath_stats.sps_ports--;
204335783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan		ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
204435783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan			   pd->port_comm, pd->port_pid,
204535783ec07c3f7f6902abe4433e7be1b664d0bbafBryan O'Sullivan			   dd->ipath_unit, port);
20467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
20477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pd->port_pid = 0;
2049f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
20507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	mutex_unlock(&ipath_mutex);
2051f37bda92461313ad3bbfbf5660adc849c69718bfBryan O'Sullivan	ipath_free_pddata(dd, pd); /* after releasing the mutex */
20527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20539929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanbail:
20549929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	kfree(fd);
20557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
20567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
20577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20589929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int ipath_port_info(struct ipath_portdata *pd, u16 subport,
20597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   struct ipath_port_info __user *uinfo)
20607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
20617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_port_info info;
20627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int nup;
20637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
20649929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	size_t sz;
20657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	(void) ipath_count_units(NULL, &nup, NULL);
20677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	info.num_active = nup;
20687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	info.unit = pd->port_dd->ipath_unit;
20697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	info.port = pd->port_port;
20709929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	info.subport = subport;
20719929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	/* Don't return new fields if old library opened the port. */
20720df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage	if (ipath_supports_subports(pd->userversion >> 16,
20730df6291c8af2778d05f278d5738eef2c8fafa2ddMark Debbage				    pd->userversion & 0xffff)) {
20749929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		/* Number of user ports available for this device. */
20759929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		info.num_ports = pd->port_dd->ipath_cfgports - 1;
20769929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		info.num_subports = pd->port_subport_cnt;
20779929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		sz = sizeof(info);
20789929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	} else
20799929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		sz = sizeof(info) - 2 * sizeof(u16);
20807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20819929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (copy_to_user(uinfo, &info, sz)) {
20827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EFAULT;
20837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
20847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
20857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = 0;
20867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
20887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
20897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
20907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
20919929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivanstatic int ipath_get_slave_info(struct ipath_portdata *pd,
20929929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan				void __user *slave_mask_addr)
20939929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan{
20949929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	int ret = 0;
20959929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
20969929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
20979929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EFAULT;
20989929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	return ret;
20999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan}
21009929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan
2101569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jonesstatic int ipath_force_pio_avail_update(struct ipath_devdata *dd)
2102569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones{
2103569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones	u64 reg = dd->ipath_sendctrl;
2104569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones
2105569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones	clear_bit(IPATH_S_PIOBUFAVAILUPD, &reg);
2106569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones	ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, reg);
2107569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones	ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
2108569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones
2109569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones	return 0;
2110569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones}
2111569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones
21127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic ssize_t ipath_write(struct file *fp, const char __user *data,
21137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			   size_t count, loff_t *off)
21147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
21157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	const struct ipath_cmd __user *ucmd;
21167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_portdata *pd;
21177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	const void __user *src;
21187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	size_t consumed, copy;
21197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct ipath_cmd cmd;
21207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ssize_t ret = 0;
21217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	void *dest;
21227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
21237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (count < sizeof(cmd.type)) {
21247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EINVAL;
21257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
21267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
21277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
21287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ucmd = (const struct ipath_cmd __user *) data;
21297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
21307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
21317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EFAULT;
21327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
21337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
21347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
21357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	consumed = sizeof(cmd.type);
21367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
21377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	switch (cmd.type) {
2138c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	case IPATH_CMD_ASSIGN_PORT:
2139c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	case __IPATH_CMD_USER_INIT:
21407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_USER_INIT:
21417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		copy = sizeof(cmd.cmd.user_info);
21427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dest = &cmd.cmd.user_info;
21437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		src = &ucmd->cmd.user_info;
21447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
21457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_RECV_CTRL:
21467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		copy = sizeof(cmd.cmd.recv_ctrl);
21477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dest = &cmd.cmd.recv_ctrl;
21487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		src = &ucmd->cmd.recv_ctrl;
21497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
21507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_PORT_INFO:
21517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		copy = sizeof(cmd.cmd.port_info);
21527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dest = &cmd.cmd.port_info;
21537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		src = &ucmd->cmd.port_info;
21547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
21557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_TID_UPDATE:
21567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_TID_FREE:
21577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		copy = sizeof(cmd.cmd.tid_info);
21587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dest = &cmd.cmd.tid_info;
21597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		src = &ucmd->cmd.tid_info;
21607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
21617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_SET_PART_KEY:
21627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		copy = sizeof(cmd.cmd.part_key);
21637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		dest = &cmd.cmd.part_key;
21647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		src = &ucmd->cmd.part_key;
21657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
2166c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	case __IPATH_CMD_SLAVE_INFO:
21679929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		copy = sizeof(cmd.cmd.slave_mask_addr);
21689929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		dest = &cmd.cmd.slave_mask_addr;
21699929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		src = &ucmd->cmd.slave_mask_addr;
21709929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		break;
2171569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones	case IPATH_CMD_PIOAVAILUPD:	// force an update of PIOAvail reg
2172569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		copy = 0;
2173569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		src = NULL;
2174569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		dest = NULL;
2175569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		break;
21767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	default:
21777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -EINVAL;
21787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
21797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
21807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2181569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones	if (copy) {
2182569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		if ((count - consumed) < copy) {
2183569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones			ret = -EINVAL;
2184569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones			goto bail;
2185569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		}
21867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2187569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		if (copy_from_user(dest, src, copy)) {
2188569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones			ret = -EFAULT;
2189569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones			goto bail;
2190569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		}
2191569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones
2192569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		consumed += copy;
21937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
21947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
21957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	pd = port_fp(fp);
2196c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
2197c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan		cmd.type != IPATH_CMD_ASSIGN_PORT) {
21989929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = -EINVAL;
21999929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		goto bail;
22009929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan	}
22017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	switch (cmd.type) {
2203c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	case IPATH_CMD_ASSIGN_PORT:
2204c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan		ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2205c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan		if (ret)
2206c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan			goto bail;
2207c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan		break;
2208c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan	case __IPATH_CMD_USER_INIT:
2209c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan		/* backwards compatibility, get port first */
2210c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan		ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2211c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan		if (ret)
2212c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan			goto bail;
2213c97d27d8a992cf6cefee945489d5f93fca160aa2Bryan O'Sullivan		/* and fall through to current version. */
22147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_USER_INIT:
22159929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
22169929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		if (ret)
22177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
22187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = ipath_get_base_info(
22199929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan			fp, (void __user *) (unsigned long)
22207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			cmd.cmd.user_info.spu_base_info,
22217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			cmd.cmd.user_info.spu_base_info_size);
22227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
22237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_RECV_CTRL:
22249929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
22257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
22267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_PORT_INFO:
22279929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_port_info(pd, subport_fp(fp),
22287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				      (struct ipath_port_info __user *)
22297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				      (unsigned long) cmd.cmd.port_info);
22307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
22317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_TID_UPDATE:
22329929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
22337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
22347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_TID_FREE:
22359929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
22367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
22377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	case IPATH_CMD_SET_PART_KEY:
22387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = ipath_set_part_key(pd, cmd.cmd.part_key);
22397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		break;
2240c7e29ff11f23ec78b3caf691789c2b791bb596bfMark Debbage	case __IPATH_CMD_SLAVE_INFO:
22419929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		ret = ipath_get_slave_info(pd,
22429929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan					   (void __user *) (unsigned long)
22439929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan					   cmd.cmd.slave_mask_addr);
22449929b0fb0f35f54371e9364bab809bcd753f9d3aBryan O'Sullivan		break;
2245569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones	case IPATH_CMD_PIOAVAILUPD:
2246569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		ret = ipath_force_pio_avail_update(pd->port_dd);
2247569b87b47f906d65ee35d6ecc4767f20a6390b9bArthur Jones		break;
22487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
22497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret >= 0)
22517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = consumed;
22527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
22547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
22557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
22567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic struct class *ipath_class;
22587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22592b8693c0617e972fc0b2fd1ebf8de97e15b656c3Arjan van de Venstatic int init_cdev(int minor, char *name, const struct file_operations *fops,
22607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		     struct cdev **cdevp, struct class_device **class_devp)
22617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
22627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	const dev_t dev = MKDEV(IPATH_MAJOR, minor);
22637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct cdev *cdev = NULL;
22647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct class_device *class_dev = NULL;
22657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
22667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	cdev = cdev_alloc();
22687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (!cdev) {
22697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		printk(KERN_ERR IPATH_DRV_NAME
22707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       ": Could not allocate cdev for minor %d, %s\n",
22717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       minor, name);
22727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = -ENOMEM;
22737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto done;
22747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
22757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	cdev->owner = THIS_MODULE;
22777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	cdev->ops = fops;
22787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	kobject_set_name(&cdev->kobj, name);
22797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = cdev_add(cdev, dev, 1);
22817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret < 0) {
22827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		printk(KERN_ERR IPATH_DRV_NAME
22837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       ": Could not add cdev for minor %d, %s (err %d)\n",
22847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       minor, name, -ret);
22857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto err_cdev;
22867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
22877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	class_dev = class_device_create(ipath_class, NULL, dev, NULL, name);
22897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (IS_ERR(class_dev)) {
22917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = PTR_ERR(class_dev);
22927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
22937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       "class_dev for minor %d, %s (err %d)\n",
22947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       minor, name, -ret);
22957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto err_cdev;
22967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
22977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
22987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	goto done;
22997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanerr_cdev:
23017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	cdev_del(cdev);
23027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	cdev = NULL;
23037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivandone:
23057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret >= 0) {
23067f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		*cdevp = cdev;
23077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		*class_devp = class_dev;
23087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	} else {
23097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		*cdevp = NULL;
23107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		*class_devp = NULL;
23117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
23127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
23147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
23157f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23162b8693c0617e972fc0b2fd1ebf8de97e15b656c3Arjan van de Venint ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
23177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		    struct cdev **cdevp, struct class_device **class_devp)
23187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
23197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return init_cdev(minor, name, fops, cdevp, class_devp);
23207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
23217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23227f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic void cleanup_cdev(struct cdev **cdevp,
23237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			 struct class_device **class_devp)
23247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
23257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	struct class_device *class_dev = *class_devp;
23267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (class_dev) {
23287f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		class_device_unregister(class_dev);
23297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		*class_devp = NULL;
23307f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
23317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (*cdevp) {
23337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		cdev_del(*cdevp);
23347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		*cdevp = NULL;
23357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
23367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
23377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanvoid ipath_cdev_cleanup(struct cdev **cdevp,
23397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			struct class_device **class_devp)
23407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
23417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	cleanup_cdev(cdevp, class_devp);
23427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
23437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23447f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic struct cdev *wildcard_cdev;
23457f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic struct class_device *wildcard_class_dev;
23467f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23477f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic const dev_t dev = MKDEV(IPATH_MAJOR, 0);
23487f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23497f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic int user_init(void)
23507f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
23517f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
23527f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23537f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
23547f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret < 0) {
23557f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
23567f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       "chrdev region (err %d)\n", -ret);
23577f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto done;
23587f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
23597f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23607f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
23617f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23627f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (IS_ERR(ipath_class)) {
23637f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = PTR_ERR(ipath_class);
23647f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
23657f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		       "device class (err %d)\n", -ret);
23667f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		goto bail;
23677f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
23687f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23697f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	goto done;
23707f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
23717f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unregister_chrdev_region(dev, IPATH_NMINORS);
23727f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivandone:
23737f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
23747f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
23757f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23767f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic void user_cleanup(void)
23777f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
23787f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ipath_class) {
23797f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		class_destroy(ipath_class);
23807f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_class = NULL;
23817f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
23827f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23837f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	unregister_chrdev_region(dev, IPATH_NMINORS);
23847f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
23857f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23867f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic atomic_t user_count = ATOMIC_INIT(0);
23877f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanstatic atomic_t user_setup = ATOMIC_INIT(0);
23887f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23897f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanint ipath_user_add(struct ipath_devdata *dd)
23907f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
23917f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	char name[10];
23927f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	int ret;
23937f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
23947f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (atomic_inc_return(&user_count) == 1) {
23957f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = user_init();
23967f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (ret < 0) {
23977f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_dev_err(dd, "Unable to set up user support: "
23987f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				      "error %d\n", -ret);
23997f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
24007f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
24017f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
24027f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				&wildcard_class_dev);
24037f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (ret < 0) {
24047f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			ipath_dev_err(dd, "Could not create wildcard "
24057f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan				      "minor: error %d\n", -ret);
24060fd41363e0785247b7c19127318abc8b5eacc86bBryan O'Sullivan			goto bail_user;
24077f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		}
24087f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
24097f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		atomic_set(&user_setup, 1);
24107f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
24117f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
24127f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
24137f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
24147f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
2415a2acb2ff36876fdfa5f7a8bf811765aadc74c1c1Bryan O'Sullivan			&dd->user_cdev, &dd->user_class_dev);
24167f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (ret < 0)
24177f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		ipath_dev_err(dd, "Could not create user minor %d, %s\n",
24187f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			      dd->ipath_unit + 1, name);
24197f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
24207f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	goto bail;
24217f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
24220fd41363e0785247b7c19127318abc8b5eacc86bBryan O'Sullivanbail_user:
24237f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	user_cleanup();
24247f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
24257f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return ret;
24267f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
24277f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
2428a2acb2ff36876fdfa5f7a8bf811765aadc74c1c1Bryan O'Sullivanvoid ipath_user_remove(struct ipath_devdata *dd)
24297f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan{
2430a2acb2ff36876fdfa5f7a8bf811765aadc74c1c1Bryan O'Sullivan	cleanup_cdev(&dd->user_cdev, &dd->user_class_dev);
24317f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
24327f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	if (atomic_dec_return(&user_count) == 0) {
24337f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		if (atomic_read(&user_setup) == 0)
24347f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan			goto bail;
24357f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
24367f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		cleanup_cdev(&wildcard_cdev, &wildcard_class_dev);
24377f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		user_cleanup();
24387f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan
24397f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan		atomic_set(&user_setup, 0);
24407f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	}
24417f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivanbail:
24427f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan	return;
24437f510b46e4771cfb89af134b3aa827d46125a2ceBryan O'Sullivan}
2444