Path: blob/master/drivers/infiniband/hw/ipath/ipath_file_ops.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.2* Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/pci.h>34#include <linux/poll.h>35#include <linux/cdev.h>36#include <linux/swap.h>37#include <linux/vmalloc.h>38#include <linux/slab.h>39#include <linux/highmem.h>40#include <linux/io.h>41#include <linux/jiffies.h>42#include <asm/pgtable.h>4344#include "ipath_kernel.h"45#include "ipath_common.h"46#include "ipath_user_sdma.h"4748static int ipath_open(struct inode *, struct file *);49static int ipath_close(struct inode *, struct file *);50static ssize_t ipath_write(struct file *, const char __user *, size_t,51loff_t *);52static ssize_t ipath_writev(struct kiocb *, const struct iovec *,53unsigned long , loff_t);54static unsigned int ipath_poll(struct file *, struct poll_table_struct *);55static int ipath_mmap(struct file *, struct vm_area_struct *);5657static const struct file_operations ipath_file_ops = {58.owner = THIS_MODULE,59.write = ipath_write,60.aio_write = ipath_writev,61.open = ipath_open,62.release = ipath_close,63.poll = ipath_poll,64.mmap = ipath_mmap,65.llseek = noop_llseek,66};6768/*69* Convert kernel virtual addresses to physical addresses so they don't70* potentially conflict with the chip addresses used as mmap offsets.71* It doesn't really matter what mmap offset we use as long as we can72* interpret it correctly.73*/74static u64 cvt_kvaddr(void *p)75{76struct page *page;77u64 paddr = 0;7879page = vmalloc_to_page(p);80if (page)81paddr = page_to_pfn(page) << PAGE_SHIFT;8283return paddr;84}8586static int ipath_get_base_info(struct file *fp,87void __user *ubase, size_t ubase_size)88{89struct ipath_portdata *pd = port_fp(fp);90int ret = 0;91struct ipath_base_info *kinfo = NULL;92struct ipath_devdata *dd = pd->port_dd;93unsigned subport_cnt;94int shared, master;95size_t sz;9697subport_cnt = pd->port_subport_cnt;98if (!subport_cnt) {99shared = 0;100master = 0;101subport_cnt = 1;102} else {103shared = 1;104master = !subport_fp(fp);105}106107sz = sizeof(*kinfo);108/* If port sharing is not requested, allow the old size structure */109if (!shared)110sz -= 7 * sizeof(u64);111if (ubase_size < sz) {112ipath_cdbg(PROC,113"Base size %zu, need %zu (version mismatch?)\n",114ubase_size, sz);115ret = -EINVAL;116goto bail;117}118119kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);120if (kinfo == NULL) {121ret = -ENOMEM;122goto bail;123}124125ret = dd->ipath_f_get_base_info(pd, kinfo);126if (ret < 0)127goto bail;128129kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;130kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;131kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;132kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;133/*134* have to mmap whole thing135*/136kinfo->spi_rcv_egrbuftotlen =137pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;138kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;139kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /140pd->port_rcvegrbuf_chunks;141kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;142if (master)143kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;144/*145* for this use, may be ipath_cfgports summed over all chips that146* are are configured and present147*/148kinfo->spi_nports = dd->ipath_cfgports;149/* unit (chip/board) our port is on */150kinfo->spi_unit = dd->ipath_unit;151/* for now, only a single page */152kinfo->spi_tid_maxsize = PAGE_SIZE;153154/*155* Doing this per port, and based on the skip value, etc. This has156* to be the actual buffer size, since the protocol code treats it157* as an array.158*159* These have to be set to user addresses in the user code via mmap.160* These values are used on return to user code for the mmap target161* addresses only. For 32 bit, same 44 bit address problem, so use162* the physical address, not virtual. Before 2.6.11, using the163* page_address() macro worked, but in 2.6.11, even that returns the164* full 64 bit address (upper bits all 1's). So far, using the165* physical addresses (or chip offsets, for chip mapping) works, but166* no doubt some future kernel release will change that, and we'll be167* on to yet another method of dealing with this.168*/169kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;170kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;171kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;172kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;173kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +174(void *) dd->ipath_statusp -175(void *) dd->ipath_pioavailregs_dma;176if (!shared) {177kinfo->spi_piocnt = pd->port_piocnt;178kinfo->spi_piobufbase = (u64) pd->port_piobufs;179kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +180dd->ipath_ureg_align * pd->port_port;181} else if (master) {182kinfo->spi_piocnt = (pd->port_piocnt / subport_cnt) +183(pd->port_piocnt % subport_cnt);184/* Master's PIO buffers are after all the slave's */185kinfo->spi_piobufbase = (u64) pd->port_piobufs +186dd->ipath_palign *187(pd->port_piocnt - kinfo->spi_piocnt);188} else {189unsigned slave = subport_fp(fp) - 1;190191kinfo->spi_piocnt = pd->port_piocnt / subport_cnt;192kinfo->spi_piobufbase = (u64) pd->port_piobufs +193dd->ipath_palign * kinfo->spi_piocnt * slave;194}195196if (shared) {197kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase +198dd->ipath_ureg_align * pd->port_port;199kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs;200kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base;201kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr;202203kinfo->__spi_uregbase = cvt_kvaddr(pd->subport_uregbase +204PAGE_SIZE * subport_fp(fp));205206kinfo->spi_rcvhdr_base = cvt_kvaddr(pd->subport_rcvhdr_base +207pd->port_rcvhdrq_size * subport_fp(fp));208kinfo->spi_rcvhdr_tailaddr = 0;209kinfo->spi_rcv_egrbufs = cvt_kvaddr(pd->subport_rcvegrbuf +210pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size *211subport_fp(fp));212213kinfo->spi_subport_uregbase =214cvt_kvaddr(pd->subport_uregbase);215kinfo->spi_subport_rcvegrbuf =216cvt_kvaddr(pd->subport_rcvegrbuf);217kinfo->spi_subport_rcvhdr_base =218cvt_kvaddr(pd->subport_rcvhdr_base);219ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",220kinfo->spi_port, kinfo->spi_runtime_flags,221(unsigned long long) kinfo->spi_subport_uregbase,222(unsigned long long) kinfo->spi_subport_rcvegrbuf,223(unsigned long long) kinfo->spi_subport_rcvhdr_base);224}225226/*227* All user buffers are 2KB buffers. If we ever support228* giving 4KB buffers to user processes, this will need some229* work.230*/231kinfo->spi_pioindex = (kinfo->spi_piobufbase -232(dd->ipath_piobufbase & 0xffffffff)) / dd->ipath_palign;233kinfo->spi_pioalign = dd->ipath_palign;234235kinfo->spi_qpair = IPATH_KD_QP;236/*237* user mode PIO buffers are always 2KB, even when 4KB can238* be received, and sent via the kernel; this is ibmaxlen239* for 2K MTU.240*/241kinfo->spi_piosize = dd->ipath_piosize2k - 2 * sizeof(u32);242kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */243kinfo->spi_port = pd->port_port;244kinfo->spi_subport = subport_fp(fp);245kinfo->spi_sw_version = IPATH_KERN_SWVERSION;246kinfo->spi_hw_version = dd->ipath_revision;247248if (master) {249kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;250}251252sz = (ubase_size < sizeof(*kinfo)) ? ubase_size : sizeof(*kinfo);253if (copy_to_user(ubase, kinfo, sz))254ret = -EFAULT;255256bail:257kfree(kinfo);258return ret;259}260261/**262* ipath_tid_update - update a port TID263* @pd: the port264* @fp: the ipath device file265* @ti: the TID information266*267* The new implementation as of Oct 2004 is that the driver assigns268* the tid and returns it to the caller. To make it easier to269* catch bugs, and to reduce search time, we keep a cursor for270* each port, walking the shadow tid array to find one that's not271* in use.272*273* For now, if we can't allocate the full list, we fail, although274* in the long run, we'll allocate as many as we can, and the275* caller will deal with that by trying the remaining pages later.276* That means that when we fail, we have to mark the tids as not in277* use again, in our shadow copy.278*279* It's up to the caller to free the tids when they are done.280* We'll unlock the pages as they free them.281*282* Also, right now we are locking one page at a time, but since283* the intended use of this routine is for a single group of284* virtually contiguous pages, that should change to improve285* performance.286*/287static int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,288const struct ipath_tid_info *ti)289{290int ret = 0, ntids;291u32 tid, porttid, cnt, i, tidcnt, tidoff;292u16 *tidlist;293struct ipath_devdata *dd = pd->port_dd;294u64 physaddr;295unsigned long vaddr;296u64 __iomem *tidbase;297unsigned long tidmap[8];298struct page **pagep = NULL;299unsigned subport = subport_fp(fp);300301if (!dd->ipath_pageshadow) {302ret = -ENOMEM;303goto done;304}305306cnt = ti->tidcnt;307if (!cnt) {308ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",309(unsigned long long) ti->tidlist);310/*311* Should we treat as success? likely a bug312*/313ret = -EFAULT;314goto done;315}316porttid = pd->port_port * dd->ipath_rcvtidcnt;317if (!pd->port_subport_cnt) {318tidcnt = dd->ipath_rcvtidcnt;319tid = pd->port_tidcursor;320tidoff = 0;321} else if (!subport) {322tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +323(dd->ipath_rcvtidcnt % pd->port_subport_cnt);324tidoff = dd->ipath_rcvtidcnt - tidcnt;325porttid += tidoff;326tid = tidcursor_fp(fp);327} else {328tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;329tidoff = tidcnt * (subport - 1);330porttid += tidoff;331tid = tidcursor_fp(fp);332}333if (cnt > tidcnt) {334/* make sure it all fits in port_tid_pg_list */335dev_info(&dd->pcidev->dev, "Process tried to allocate %u "336"TIDs, only trying max (%u)\n", cnt, tidcnt);337cnt = tidcnt;338}339pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];340tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];341342memset(tidmap, 0, sizeof(tidmap));343/* before decrement; chip actual # */344ntids = tidcnt;345tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +346dd->ipath_rcvtidbase +347porttid * sizeof(*tidbase));348349ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",350pd->port_port, cnt, tid, tidbase);351352/* virtual address of first page in transfer */353vaddr = ti->tidvaddr;354if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,355cnt * PAGE_SIZE)) {356ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",357(void *)vaddr, cnt);358ret = -EFAULT;359goto done;360}361ret = ipath_get_user_pages(vaddr, cnt, pagep);362if (ret) {363if (ret == -EBUSY) {364ipath_dbg("Failed to lock addr %p, %u pages "365"(already locked)\n",366(void *) vaddr, cnt);367/*368* for now, continue, and see what happens but with369* the new implementation, this should never happen,370* unless perhaps the user has mpin'ed the pages371* themselves (something we need to test)372*/373ret = 0;374} else {375dev_info(&dd->pcidev->dev,376"Failed to lock addr %p, %u pages: "377"errno %d\n", (void *) vaddr, cnt, -ret);378goto done;379}380}381for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {382for (; ntids--; tid++) {383if (tid == tidcnt)384tid = 0;385if (!dd->ipath_pageshadow[porttid + tid])386break;387}388if (ntids < 0) {389/*390* oops, wrapped all the way through their TIDs,391* and didn't have enough free; see comments at392* start of routine393*/394ipath_dbg("Not enough free TIDs for %u pages "395"(index %d), failing\n", cnt, i);396i--; /* last tidlist[i] not filled in */397ret = -ENOMEM;398break;399}400tidlist[i] = tid + tidoff;401ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "402"vaddr %lx\n", i, tid + tidoff, vaddr);403/* we "know" system pages and TID pages are same size */404dd->ipath_pageshadow[porttid + tid] = pagep[i];405dd->ipath_physshadow[porttid + tid] = ipath_map_page(406dd->pcidev, pagep[i], 0, PAGE_SIZE,407PCI_DMA_FROMDEVICE);408/*409* don't need atomic or it's overhead410*/411__set_bit(tid, tidmap);412physaddr = dd->ipath_physshadow[porttid + tid];413ipath_stats.sps_pagelocks++;414ipath_cdbg(VERBOSE,415"TID %u, vaddr %lx, physaddr %llx pgp %p\n",416tid, vaddr, (unsigned long long) physaddr,417pagep[i]);418dd->ipath_f_put_tid(dd, &tidbase[tid], RCVHQ_RCV_TYPE_EXPECTED,419physaddr);420/*421* don't check this tid in ipath_portshadow, since we422* just filled it in; start with the next one.423*/424tid++;425}426427if (ret) {428u32 limit;429cleanup:430/* jump here if copy out of updated info failed... */431ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",432-ret, i, cnt);433/* same code that's in ipath_free_tid() */434limit = sizeof(tidmap) * BITS_PER_BYTE;435if (limit > tidcnt)436/* just in case size changes in future */437limit = tidcnt;438tid = find_first_bit((const unsigned long *)tidmap, limit);439for (; tid < limit; tid++) {440if (!test_bit(tid, tidmap))441continue;442if (dd->ipath_pageshadow[porttid + tid]) {443ipath_cdbg(VERBOSE, "Freeing TID %u\n",444tid);445dd->ipath_f_put_tid(dd, &tidbase[tid],446RCVHQ_RCV_TYPE_EXPECTED,447dd->ipath_tidinvalid);448pci_unmap_page(dd->pcidev,449dd->ipath_physshadow[porttid + tid],450PAGE_SIZE, PCI_DMA_FROMDEVICE);451dd->ipath_pageshadow[porttid + tid] = NULL;452ipath_stats.sps_pageunlocks++;453}454}455ipath_release_user_pages(pagep, cnt);456} else {457/*458* Copy the updated array, with ipath_tid's filled in, back459* to user. Since we did the copy in already, this "should460* never fail" If it does, we have to clean up...461*/462if (copy_to_user((void __user *)463(unsigned long) ti->tidlist,464tidlist, cnt * sizeof(*tidlist))) {465ret = -EFAULT;466goto cleanup;467}468if (copy_to_user((void __user *) (unsigned long) ti->tidmap,469tidmap, sizeof tidmap)) {470ret = -EFAULT;471goto cleanup;472}473if (tid == tidcnt)474tid = 0;475if (!pd->port_subport_cnt)476pd->port_tidcursor = tid;477else478tidcursor_fp(fp) = tid;479}480481done:482if (ret)483ipath_dbg("Failed to map %u TID pages, failing with %d\n",484ti->tidcnt, -ret);485return ret;486}487488/**489* ipath_tid_free - free a port TID490* @pd: the port491* @subport: the subport492* @ti: the TID info493*494* right now we are unlocking one page at a time, but since495* the intended use of this routine is for a single group of496* virtually contiguous pages, that should change to improve497* performance. We check that the TID is in range for this port498* but otherwise don't check validity; if user has an error and499* frees the wrong tid, it's only their own data that can thereby500* be corrupted. We do check that the TID was in use, for sanity501* We always use our idea of the saved address, not the address that502* they pass in to us.503*/504505static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,506const struct ipath_tid_info *ti)507{508int ret = 0;509u32 tid, porttid, cnt, limit, tidcnt;510struct ipath_devdata *dd = pd->port_dd;511u64 __iomem *tidbase;512unsigned long tidmap[8];513514if (!dd->ipath_pageshadow) {515ret = -ENOMEM;516goto done;517}518519if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,520sizeof tidmap)) {521ret = -EFAULT;522goto done;523}524525porttid = pd->port_port * dd->ipath_rcvtidcnt;526if (!pd->port_subport_cnt)527tidcnt = dd->ipath_rcvtidcnt;528else if (!subport) {529tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +530(dd->ipath_rcvtidcnt % pd->port_subport_cnt);531porttid += dd->ipath_rcvtidcnt - tidcnt;532} else {533tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;534porttid += tidcnt * (subport - 1);535}536tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +537dd->ipath_rcvtidbase +538porttid * sizeof(*tidbase));539540limit = sizeof(tidmap) * BITS_PER_BYTE;541if (limit > tidcnt)542/* just in case size changes in future */543limit = tidcnt;544tid = find_first_bit(tidmap, limit);545ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "546"set is %d, porttid %u\n", pd->port_port, ti->tidcnt,547limit, tid, porttid);548for (cnt = 0; tid < limit; tid++) {549/*550* small optimization; if we detect a run of 3 or so without551* any set, use find_first_bit again. That's mainly to552* accelerate the case where we wrapped, so we have some at553* the beginning, and some at the end, and a big gap554* in the middle.555*/556if (!test_bit(tid, tidmap))557continue;558cnt++;559if (dd->ipath_pageshadow[porttid + tid]) {560struct page *p;561p = dd->ipath_pageshadow[porttid + tid];562dd->ipath_pageshadow[porttid + tid] = NULL;563ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",564pid_nr(pd->port_pid), tid);565dd->ipath_f_put_tid(dd, &tidbase[tid],566RCVHQ_RCV_TYPE_EXPECTED,567dd->ipath_tidinvalid);568pci_unmap_page(dd->pcidev,569dd->ipath_physshadow[porttid + tid],570PAGE_SIZE, PCI_DMA_FROMDEVICE);571ipath_release_user_pages(&p, 1);572ipath_stats.sps_pageunlocks++;573} else574ipath_dbg("Unused tid %u, ignoring\n", tid);575}576if (cnt != ti->tidcnt)577ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",578ti->tidcnt, cnt);579done:580if (ret)581ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",582ti->tidcnt, -ret);583return ret;584}585586/**587* ipath_set_part_key - set a partition key588* @pd: the port589* @key: the key590*591* We can have up to 4 active at a time (other than the default, which is592* always allowed). This is somewhat tricky, since multiple ports may set593* the same key, so we reference count them, and clean up at exit. All 4594* partition keys are packed into a single infinipath register. It's an595* error for a process to set the same pkey multiple times. We provide no596* mechanism to de-allocate a pkey at this time, we may eventually need to597* do that. I've used the atomic operations, and no locking, and only make598* a single pass through what's available. This should be more than599* adequate for some time. I'll think about spinlocks or the like if and as600* it's necessary.601*/602static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)603{604struct ipath_devdata *dd = pd->port_dd;605int i, any = 0, pidx = -1;606u16 lkey = key & 0x7FFF;607int ret;608609if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {610/* nothing to do; this key always valid */611ret = 0;612goto bail;613}614615ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "616"%hx:%x %hx:%x %hx:%x %hx:%x\n",617pd->port_port, key, dd->ipath_pkeys[0],618atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],619atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],620atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],621atomic_read(&dd->ipath_pkeyrefs[3]));622623if (!lkey) {624ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",625pd->port_port);626ret = -EINVAL;627goto bail;628}629630/*631* Set the full membership bit, because it has to be632* set in the register or the packet, and it seems633* cleaner to set in the register than to force all634* callers to set it. (see bug 4331)635*/636key |= 0x8000;637638for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {639if (!pd->port_pkeys[i] && pidx == -1)640pidx = i;641if (pd->port_pkeys[i] == key) {642ipath_cdbg(VERBOSE, "p%u tries to set same pkey "643"(%x) more than once\n",644pd->port_port, key);645ret = -EEXIST;646goto bail;647}648}649if (pidx == -1) {650ipath_dbg("All pkeys for port %u already in use, "651"can't set %x\n", pd->port_port, key);652ret = -EBUSY;653goto bail;654}655for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {656if (!dd->ipath_pkeys[i]) {657any++;658continue;659}660if (dd->ipath_pkeys[i] == key) {661atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];662663if (atomic_inc_return(pkrefs) > 1) {664pd->port_pkeys[pidx] = key;665ipath_cdbg(VERBOSE, "p%u set key %x "666"matches #%d, count now %d\n",667pd->port_port, key, i,668atomic_read(pkrefs));669ret = 0;670goto bail;671} else {672/*673* lost race, decrement count, catch below674*/675atomic_dec(pkrefs);676ipath_cdbg(VERBOSE, "Lost race, count was "677"0, after dec, it's %d\n",678atomic_read(pkrefs));679any++;680}681}682if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {683/*684* It makes no sense to have both the limited and685* full membership PKEY set at the same time since686* the unlimited one will disable the limited one.687*/688ret = -EEXIST;689goto bail;690}691}692if (!any) {693ipath_dbg("port %u, all pkeys already in use, "694"can't set %x\n", pd->port_port, key);695ret = -EBUSY;696goto bail;697}698for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {699if (!dd->ipath_pkeys[i] &&700atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {701u64 pkey;702703/* for ipathstats, etc. */704ipath_stats.sps_pkeys[i] = lkey;705pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;706pkey =707(u64) dd->ipath_pkeys[0] |708((u64) dd->ipath_pkeys[1] << 16) |709((u64) dd->ipath_pkeys[2] << 32) |710((u64) dd->ipath_pkeys[3] << 48);711ipath_cdbg(PROC, "p%u set key %x in #%d, "712"portidx %d, new pkey reg %llx\n",713pd->port_port, key, i, pidx,714(unsigned long long) pkey);715ipath_write_kreg(716dd, dd->ipath_kregs->kr_partitionkey, pkey);717718ret = 0;719goto bail;720}721}722ipath_dbg("port %u, all pkeys already in use 2nd pass, "723"can't set %x\n", pd->port_port, key);724ret = -EBUSY;725726bail:727return ret;728}729730/**731* ipath_manage_rcvq - manage a port's receive queue732* @pd: the port733* @subport: the subport734* @start_stop: action to carry out735*736* start_stop == 0 disables receive on the port, for use in queue737* overflow conditions. start_stop==1 re-enables, to be used to738* re-init the software copy of the head register739*/740static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,741int start_stop)742{743struct ipath_devdata *dd = pd->port_dd;744745ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",746start_stop ? "en" : "dis", dd->ipath_unit,747pd->port_port, subport);748if (subport)749goto bail;750/* atomically clear receive enable port. */751if (start_stop) {752/*753* On enable, force in-memory copy of the tail register to754* 0, so that protocol code doesn't have to worry about755* whether or not the chip has yet updated the in-memory756* copy or not on return from the system call. The chip757* always resets it's tail register back to 0 on a758* transition from disabled to enabled. This could cause a759* problem if software was broken, and did the enable w/o760* the disable, but eventually the in-memory copy will be761* updated and correct itself, even in the face of software762* bugs.763*/764if (pd->port_rcvhdrtail_kvaddr)765ipath_clear_rcvhdrtail(pd);766set_bit(dd->ipath_r_portenable_shift + pd->port_port,767&dd->ipath_rcvctrl);768} else769clear_bit(dd->ipath_r_portenable_shift + pd->port_port,770&dd->ipath_rcvctrl);771ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,772dd->ipath_rcvctrl);773/* now be sure chip saw it before we return */774ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);775if (start_stop) {776/*777* And try to be sure that tail reg update has happened too.778* This should in theory interlock with the RXE changes to779* the tail register. Don't assign it to the tail register780* in memory copy, since we could overwrite an update by the781* chip if we did.782*/783ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);784}785/* always; new head should be equal to new tail; see above */786bail:787return 0;788}789790static void ipath_clean_part_key(struct ipath_portdata *pd,791struct ipath_devdata *dd)792{793int i, j, pchanged = 0;794u64 oldpkey;795796/* for debugging only */797oldpkey = (u64) dd->ipath_pkeys[0] |798((u64) dd->ipath_pkeys[1] << 16) |799((u64) dd->ipath_pkeys[2] << 32) |800((u64) dd->ipath_pkeys[3] << 48);801802for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {803if (!pd->port_pkeys[i])804continue;805ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,806pd->port_pkeys[i]);807for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {808/* check for match independent of the global bit */809if ((dd->ipath_pkeys[j] & 0x7fff) !=810(pd->port_pkeys[i] & 0x7fff))811continue;812if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {813ipath_cdbg(VERBOSE, "p%u clear key "814"%x matches #%d\n",815pd->port_port,816pd->port_pkeys[i], j);817ipath_stats.sps_pkeys[j] =818dd->ipath_pkeys[j] = 0;819pchanged++;820}821else ipath_cdbg(822VERBOSE, "p%u key %x matches #%d, "823"but ref still %d\n", pd->port_port,824pd->port_pkeys[i], j,825atomic_read(&dd->ipath_pkeyrefs[j]));826break;827}828pd->port_pkeys[i] = 0;829}830if (pchanged) {831u64 pkey = (u64) dd->ipath_pkeys[0] |832((u64) dd->ipath_pkeys[1] << 16) |833((u64) dd->ipath_pkeys[2] << 32) |834((u64) dd->ipath_pkeys[3] << 48);835ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "836"new pkey reg %llx\n", pd->port_port,837(unsigned long long) oldpkey,838(unsigned long long) pkey);839ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,840pkey);841}842}843844/*845* Initialize the port data with the receive buffer sizes846* so this can be done while the master port is locked.847* Otherwise, there is a race with a slave opening the port848* and seeing these fields uninitialized.849*/850static void init_user_egr_sizes(struct ipath_portdata *pd)851{852struct ipath_devdata *dd = pd->port_dd;853unsigned egrperchunk, egrcnt, size;854855/*856* to avoid wasting a lot of memory, we allocate 32KB chunks of857* physically contiguous memory, advance through it until used up858* and then allocate more. Of course, we need memory to store those859* extra pointers, now. Started out with 256KB, but under heavy860* memory pressure (creating large files and then copying them over861* NFS while doing lots of MPI jobs), we hit some allocation862* failures, even though we can sleep... (2.6.10) Still get863* failures at 64K. 32K is the lowest we can go without wasting864* additional memory.865*/866size = 0x8000;867egrperchunk = size / dd->ipath_rcvegrbufsize;868egrcnt = dd->ipath_rcvegrcnt;869pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;870pd->port_rcvegrbufs_perchunk = egrperchunk;871pd->port_rcvegrbuf_size = size;872}873874/**875* ipath_create_user_egr - allocate eager TID buffers876* @pd: the port to allocate TID buffers for877*878* This routine is now quite different for user and kernel, because879* the kernel uses skb's, for the accelerated network performance880* This is the user port version881*882* Allocate the eager TID buffers and program them into infinipath883* They are no longer completely contiguous, we do multiple allocation884* calls.885*/886static int ipath_create_user_egr(struct ipath_portdata *pd)887{888struct ipath_devdata *dd = pd->port_dd;889unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;890size_t size;891int ret;892gfp_t gfp_flags;893894/*895* GFP_USER, but without GFP_FS, so buffer cache can be896* coalesced (we hope); otherwise, even at order 4,897* heavy filesystem activity makes these fail, and we can898* use compound pages.899*/900gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;901902egrcnt = dd->ipath_rcvegrcnt;903/* TID number offset for this port */904egroff = (pd->port_port - 1) * egrcnt + dd->ipath_p0_rcvegrcnt;905egrsize = dd->ipath_rcvegrbufsize;906ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "907"offset %x, egrsize %u\n", egrcnt, egroff, egrsize);908909chunk = pd->port_rcvegrbuf_chunks;910egrperchunk = pd->port_rcvegrbufs_perchunk;911size = pd->port_rcvegrbuf_size;912pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),913GFP_KERNEL);914if (!pd->port_rcvegrbuf) {915ret = -ENOMEM;916goto bail;917}918pd->port_rcvegrbuf_phys =919kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),920GFP_KERNEL);921if (!pd->port_rcvegrbuf_phys) {922ret = -ENOMEM;923goto bail_rcvegrbuf;924}925for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {926927pd->port_rcvegrbuf[e] = dma_alloc_coherent(928&dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],929gfp_flags);930931if (!pd->port_rcvegrbuf[e]) {932ret = -ENOMEM;933goto bail_rcvegrbuf_phys;934}935}936937pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];938939for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {940dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];941unsigned i;942943for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {944dd->ipath_f_put_tid(dd, e + egroff +945(u64 __iomem *)946((char __iomem *)947dd->ipath_kregbase +948dd->ipath_rcvegrbase),949RCVHQ_RCV_TYPE_EAGER, pa);950pa += egrsize;951}952cond_resched(); /* don't hog the cpu */953}954955ret = 0;956goto bail;957958bail_rcvegrbuf_phys:959for (e = 0; e < pd->port_rcvegrbuf_chunks &&960pd->port_rcvegrbuf[e]; e++) {961dma_free_coherent(&dd->pcidev->dev, size,962pd->port_rcvegrbuf[e],963pd->port_rcvegrbuf_phys[e]);964965}966kfree(pd->port_rcvegrbuf_phys);967pd->port_rcvegrbuf_phys = NULL;968bail_rcvegrbuf:969kfree(pd->port_rcvegrbuf);970pd->port_rcvegrbuf = NULL;971bail:972return ret;973}974975976/* common code for the mappings on dma_alloc_coherent mem */977static int ipath_mmap_mem(struct vm_area_struct *vma,978struct ipath_portdata *pd, unsigned len, int write_ok,979void *kvaddr, char *what)980{981struct ipath_devdata *dd = pd->port_dd;982unsigned long pfn;983int ret;984985if ((vma->vm_end - vma->vm_start) > len) {986dev_info(&dd->pcidev->dev,987"FAIL on %s: len %lx > %x\n", what,988vma->vm_end - vma->vm_start, len);989ret = -EFAULT;990goto bail;991}992993if (!write_ok) {994if (vma->vm_flags & VM_WRITE) {995dev_info(&dd->pcidev->dev,996"%s must be mapped readonly\n", what);997ret = -EPERM;998goto bail;999}10001001/* don't allow them to later change with mprotect */1002vma->vm_flags &= ~VM_MAYWRITE;1003}10041005pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;1006ret = remap_pfn_range(vma, vma->vm_start, pfn,1007len, vma->vm_page_prot);1008if (ret)1009dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "1010"bytes r%c failed: %d\n", what, pd->port_port,1011pfn, len, write_ok?'w':'o', ret);1012else1013ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "1014"r%c\n", what, pd->port_port, pfn, len,1015write_ok?'w':'o');1016bail:1017return ret;1018}10191020static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,1021u64 ureg)1022{1023unsigned long phys;1024int ret;10251026/*1027* This is real hardware, so use io_remap. This is the mechanism1028* for the user process to update the head registers for their port1029* in the chip.1030*/1031if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {1032dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "1033"%lx > PAGE\n", vma->vm_end - vma->vm_start);1034ret = -EFAULT;1035} else {1036phys = dd->ipath_physaddr + ureg;1037vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);10381039vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;1040ret = io_remap_pfn_range(vma, vma->vm_start,1041phys >> PAGE_SHIFT,1042vma->vm_end - vma->vm_start,1043vma->vm_page_prot);1044}1045return ret;1046}10471048static int mmap_piobufs(struct vm_area_struct *vma,1049struct ipath_devdata *dd,1050struct ipath_portdata *pd,1051unsigned piobufs, unsigned piocnt)1052{1053unsigned long phys;1054int ret;10551056/*1057* When we map the PIO buffers in the chip, we want to map them as1058* writeonly, no read possible. This prevents access to previous1059* process data, and catches users who might try to read the i/o1060* space due to a bug.1061*/1062if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {1063dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "1064"reqlen %lx > PAGE\n",1065vma->vm_end - vma->vm_start);1066ret = -EINVAL;1067goto bail;1068}10691070phys = dd->ipath_physaddr + piobufs;10711072#if defined(__powerpc__)1073/* There isn't a generic way to specify writethrough mappings */1074pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;1075pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;1076pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;1077#endif10781079/*1080* don't allow them to later change to readable with mprotect (for when1081* not initially mapped readable, as is normally the case)1082*/1083vma->vm_flags &= ~VM_MAYREAD;1084vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;10851086ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,1087vma->vm_end - vma->vm_start,1088vma->vm_page_prot);1089bail:1090return ret;1091}10921093static int mmap_rcvegrbufs(struct vm_area_struct *vma,1094struct ipath_portdata *pd)1095{1096struct ipath_devdata *dd = pd->port_dd;1097unsigned long start, size;1098size_t total_size, i;1099unsigned long pfn;1100int ret;11011102size = pd->port_rcvegrbuf_size;1103total_size = pd->port_rcvegrbuf_chunks * size;1104if ((vma->vm_end - vma->vm_start) > total_size) {1105dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "1106"reqlen %lx > actual %lx\n",1107vma->vm_end - vma->vm_start,1108(unsigned long) total_size);1109ret = -EINVAL;1110goto bail;1111}11121113if (vma->vm_flags & VM_WRITE) {1114dev_info(&dd->pcidev->dev, "Can't map eager buffers as "1115"writable (flags=%lx)\n", vma->vm_flags);1116ret = -EPERM;1117goto bail;1118}1119/* don't allow them to later change to writeable with mprotect */1120vma->vm_flags &= ~VM_MAYWRITE;11211122start = vma->vm_start;11231124for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {1125pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;1126ret = remap_pfn_range(vma, start, pfn, size,1127vma->vm_page_prot);1128if (ret < 0)1129goto bail;1130}1131ret = 0;11321133bail:1134return ret;1135}11361137/*1138* ipath_file_vma_fault - handle a VMA page fault.1139*/1140static int ipath_file_vma_fault(struct vm_area_struct *vma,1141struct vm_fault *vmf)1142{1143struct page *page;11441145page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));1146if (!page)1147return VM_FAULT_SIGBUS;1148get_page(page);1149vmf->page = page;11501151return 0;1152}11531154static const struct vm_operations_struct ipath_file_vm_ops = {1155.fault = ipath_file_vma_fault,1156};11571158static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,1159struct ipath_portdata *pd, unsigned subport)1160{1161unsigned long len;1162struct ipath_devdata *dd;1163void *addr;1164size_t size;1165int ret = 0;11661167/* If the port is not shared, all addresses should be physical */1168if (!pd->port_subport_cnt)1169goto bail;11701171dd = pd->port_dd;1172size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;11731174/*1175* Each process has all the subport uregbase, rcvhdrq, and1176* rcvegrbufs mmapped - as an array for all the processes,1177* and also separately for this process.1178*/1179if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {1180addr = pd->subport_uregbase;1181size = PAGE_SIZE * pd->port_subport_cnt;1182} else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {1183addr = pd->subport_rcvhdr_base;1184size = pd->port_rcvhdrq_size * pd->port_subport_cnt;1185} else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {1186addr = pd->subport_rcvegrbuf;1187size *= pd->port_subport_cnt;1188} else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +1189PAGE_SIZE * subport)) {1190addr = pd->subport_uregbase + PAGE_SIZE * subport;1191size = PAGE_SIZE;1192} else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +1193pd->port_rcvhdrq_size * subport)) {1194addr = pd->subport_rcvhdr_base +1195pd->port_rcvhdrq_size * subport;1196size = pd->port_rcvhdrq_size;1197} else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +1198size * subport)) {1199addr = pd->subport_rcvegrbuf + size * subport;1200/* rcvegrbufs are read-only on the slave */1201if (vma->vm_flags & VM_WRITE) {1202dev_info(&dd->pcidev->dev,1203"Can't map eager buffers as "1204"writable (flags=%lx)\n", vma->vm_flags);1205ret = -EPERM;1206goto bail;1207}1208/*1209* Don't allow permission to later change to writeable1210* with mprotect.1211*/1212vma->vm_flags &= ~VM_MAYWRITE;1213} else {1214goto bail;1215}1216len = vma->vm_end - vma->vm_start;1217if (len > size) {1218ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);1219ret = -EINVAL;1220goto bail;1221}12221223vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;1224vma->vm_ops = &ipath_file_vm_ops;1225vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;1226ret = 1;12271228bail:1229return ret;1230}12311232/**1233* ipath_mmap - mmap various structures into user space1234* @fp: the file pointer1235* @vma: the VM area1236*1237* We use this to have a shared buffer between the kernel and the user code1238* for the rcvhdr queue, egr buffers, and the per-port user regs and pio1239* buffers in the chip. We have the open and close entries so we can bump1240* the ref count and keep the driver from being unloaded while still mapped.1241*/1242static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)1243{1244struct ipath_portdata *pd;1245struct ipath_devdata *dd;1246u64 pgaddr, ureg;1247unsigned piobufs, piocnt;1248int ret;12491250pd = port_fp(fp);1251if (!pd) {1252ret = -EINVAL;1253goto bail;1254}1255dd = pd->port_dd;12561257/*1258* This is the ipath_do_user_init() code, mapping the shared buffers1259* into the user process. The address referred to by vm_pgoff is the1260* file offset passed via mmap(). For shared ports, this is the1261* kernel vmalloc() address of the pages to share with the master.1262* For non-shared or master ports, this is a physical address.1263* We only do one mmap for each space mapped.1264*/1265pgaddr = vma->vm_pgoff << PAGE_SHIFT;12661267/*1268* Check for 0 in case one of the allocations failed, but user1269* called mmap anyway.1270*/1271if (!pgaddr) {1272ret = -EINVAL;1273goto bail;1274}12751276ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",1277(unsigned long long) pgaddr, vma->vm_start,1278vma->vm_end - vma->vm_start, dd->ipath_unit,1279pd->port_port, subport_fp(fp));12801281/*1282* Physical addresses must fit in 40 bits for our hardware.1283* Check for kernel virtual addresses first, anything else must1284* match a HW or memory address.1285*/1286ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));1287if (ret) {1288if (ret > 0)1289ret = 0;1290goto bail;1291}12921293ureg = dd->ipath_uregbase + dd->ipath_ureg_align * pd->port_port;1294if (!pd->port_subport_cnt) {1295/* port is not shared */1296piocnt = pd->port_piocnt;1297piobufs = pd->port_piobufs;1298} else if (!subport_fp(fp)) {1299/* caller is the master */1300piocnt = (pd->port_piocnt / pd->port_subport_cnt) +1301(pd->port_piocnt % pd->port_subport_cnt);1302piobufs = pd->port_piobufs +1303dd->ipath_palign * (pd->port_piocnt - piocnt);1304} else {1305unsigned slave = subport_fp(fp) - 1;13061307/* caller is a slave */1308piocnt = pd->port_piocnt / pd->port_subport_cnt;1309piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;1310}13111312if (pgaddr == ureg)1313ret = mmap_ureg(vma, dd, ureg);1314else if (pgaddr == piobufs)1315ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);1316else if (pgaddr == dd->ipath_pioavailregs_phys)1317/* in-memory copy of pioavail registers */1318ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,1319(void *) dd->ipath_pioavailregs_dma,1320"pioavail registers");1321else if (pgaddr == pd->port_rcvegr_phys)1322ret = mmap_rcvegrbufs(vma, pd);1323else if (pgaddr == (u64) pd->port_rcvhdrq_phys)1324/*1325* The rcvhdrq itself; readonly except on HT (so have1326* to allow writable mapping), multiple pages, contiguous1327* from an i/o perspective.1328*/1329ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,1330pd->port_rcvhdrq,1331"rcvhdrq");1332else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)1333/* in-memory copy of rcvhdrq tail register */1334ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,1335pd->port_rcvhdrtail_kvaddr,1336"rcvhdrq tail");1337else1338ret = -EINVAL;13391340vma->vm_private_data = NULL;13411342if (ret < 0)1343dev_info(&dd->pcidev->dev,1344"Failure %d on off %llx len %lx\n",1345-ret, (unsigned long long)pgaddr,1346vma->vm_end - vma->vm_start);1347bail:1348return ret;1349}13501351static unsigned ipath_poll_hdrqfull(struct ipath_portdata *pd)1352{1353unsigned pollflag = 0;13541355if ((pd->poll_type & IPATH_POLL_TYPE_OVERFLOW) &&1356pd->port_hdrqfull != pd->port_hdrqfull_poll) {1357pollflag |= POLLIN | POLLRDNORM;1358pd->port_hdrqfull_poll = pd->port_hdrqfull;1359}13601361return pollflag;1362}13631364static unsigned int ipath_poll_urgent(struct ipath_portdata *pd,1365struct file *fp,1366struct poll_table_struct *pt)1367{1368unsigned pollflag = 0;1369struct ipath_devdata *dd;13701371dd = pd->port_dd;13721373/* variable access in ipath_poll_hdrqfull() needs this */1374rmb();1375pollflag = ipath_poll_hdrqfull(pd);13761377if (pd->port_urgent != pd->port_urgent_poll) {1378pollflag |= POLLIN | POLLRDNORM;1379pd->port_urgent_poll = pd->port_urgent;1380}13811382if (!pollflag) {1383/* this saves a spin_lock/unlock in interrupt handler... */1384set_bit(IPATH_PORT_WAITING_URG, &pd->port_flag);1385/* flush waiting flag so don't miss an event... */1386wmb();1387poll_wait(fp, &pd->port_wait, pt);1388}13891390return pollflag;1391}13921393static unsigned int ipath_poll_next(struct ipath_portdata *pd,1394struct file *fp,1395struct poll_table_struct *pt)1396{1397u32 head;1398u32 tail;1399unsigned pollflag = 0;1400struct ipath_devdata *dd;14011402dd = pd->port_dd;14031404/* variable access in ipath_poll_hdrqfull() needs this */1405rmb();1406pollflag = ipath_poll_hdrqfull(pd);14071408head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);1409if (pd->port_rcvhdrtail_kvaddr)1410tail = ipath_get_rcvhdrtail(pd);1411else1412tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);14131414if (head != tail)1415pollflag |= POLLIN | POLLRDNORM;1416else {1417/* this saves a spin_lock/unlock in interrupt handler */1418set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);1419/* flush waiting flag so we don't miss an event */1420wmb();14211422set_bit(pd->port_port + dd->ipath_r_intravail_shift,1423&dd->ipath_rcvctrl);14241425ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,1426dd->ipath_rcvctrl);14271428if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */1429ipath_write_ureg(dd, ur_rcvhdrhead,1430dd->ipath_rhdrhead_intr_off | head,1431pd->port_port);14321433poll_wait(fp, &pd->port_wait, pt);1434}14351436return pollflag;1437}14381439static unsigned int ipath_poll(struct file *fp,1440struct poll_table_struct *pt)1441{1442struct ipath_portdata *pd;1443unsigned pollflag;14441445pd = port_fp(fp);1446if (!pd)1447pollflag = 0;1448else if (pd->poll_type & IPATH_POLL_TYPE_URGENT)1449pollflag = ipath_poll_urgent(pd, fp, pt);1450else1451pollflag = ipath_poll_next(pd, fp, pt);14521453return pollflag;1454}14551456static int ipath_supports_subports(int user_swmajor, int user_swminor)1457{1458/* no subport implementation prior to software version 1.3 */1459return (user_swmajor > 1) || (user_swminor >= 3);1460}14611462static int ipath_compatible_subports(int user_swmajor, int user_swminor)1463{1464/* this code is written long-hand for clarity */1465if (IPATH_USER_SWMAJOR != user_swmajor) {1466/* no promise of compatibility if major mismatch */1467return 0;1468}1469if (IPATH_USER_SWMAJOR == 1) {1470switch (IPATH_USER_SWMINOR) {1471case 0:1472case 1:1473case 2:1474/* no subport implementation so cannot be compatible */1475return 0;1476case 3:1477/* 3 is only compatible with itself */1478return user_swminor == 3;1479default:1480/* >= 4 are compatible (or are expected to be) */1481return user_swminor >= 4;1482}1483}1484/* make no promises yet for future major versions */1485return 0;1486}14871488static int init_subports(struct ipath_devdata *dd,1489struct ipath_portdata *pd,1490const struct ipath_user_info *uinfo)1491{1492int ret = 0;1493unsigned num_subports;1494size_t size;14951496/*1497* If the user is requesting zero subports,1498* skip the subport allocation.1499*/1500if (uinfo->spu_subport_cnt <= 0)1501goto bail;15021503/* Self-consistency check for ipath_compatible_subports() */1504if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&1505!ipath_compatible_subports(IPATH_USER_SWMAJOR,1506IPATH_USER_SWMINOR)) {1507dev_info(&dd->pcidev->dev,1508"Inconsistent ipath_compatible_subports()\n");1509goto bail;1510}15111512/* Check for subport compatibility */1513if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,1514uinfo->spu_userversion & 0xffff)) {1515dev_info(&dd->pcidev->dev,1516"Mismatched user version (%d.%d) and driver "1517"version (%d.%d) while port sharing. Ensure "1518"that driver and library are from the same "1519"release.\n",1520(int) (uinfo->spu_userversion >> 16),1521(int) (uinfo->spu_userversion & 0xffff),1522IPATH_USER_SWMAJOR,1523IPATH_USER_SWMINOR);1524goto bail;1525}1526if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {1527ret = -EINVAL;1528goto bail;1529}15301531num_subports = uinfo->spu_subport_cnt;1532pd->subport_uregbase = vzalloc(PAGE_SIZE * num_subports);1533if (!pd->subport_uregbase) {1534ret = -ENOMEM;1535goto bail;1536}1537/* Note: pd->port_rcvhdrq_size isn't initialized yet. */1538size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *1539sizeof(u32), PAGE_SIZE) * num_subports;1540pd->subport_rcvhdr_base = vzalloc(size);1541if (!pd->subport_rcvhdr_base) {1542ret = -ENOMEM;1543goto bail_ureg;1544}15451546pd->subport_rcvegrbuf = vzalloc(pd->port_rcvegrbuf_chunks *1547pd->port_rcvegrbuf_size *1548num_subports);1549if (!pd->subport_rcvegrbuf) {1550ret = -ENOMEM;1551goto bail_rhdr;1552}15531554pd->port_subport_cnt = uinfo->spu_subport_cnt;1555pd->port_subport_id = uinfo->spu_subport_id;1556pd->active_slaves = 1;1557set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);1558goto bail;15591560bail_rhdr:1561vfree(pd->subport_rcvhdr_base);1562bail_ureg:1563vfree(pd->subport_uregbase);1564pd->subport_uregbase = NULL;1565bail:1566return ret;1567}15681569static int try_alloc_port(struct ipath_devdata *dd, int port,1570struct file *fp,1571const struct ipath_user_info *uinfo)1572{1573struct ipath_portdata *pd;1574int ret;15751576if (!(pd = dd->ipath_pd[port])) {1577void *ptmp;15781579pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);15801581/*1582* Allocate memory for use in ipath_tid_update() just once1583* at open, not per call. Reduces cost of expected send1584* setup.1585*/1586ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +1587dd->ipath_rcvtidcnt * sizeof(struct page **),1588GFP_KERNEL);1589if (!pd || !ptmp) {1590ipath_dev_err(dd, "Unable to allocate portdata "1591"memory, failing open\n");1592ret = -ENOMEM;1593kfree(pd);1594kfree(ptmp);1595goto bail;1596}1597dd->ipath_pd[port] = pd;1598dd->ipath_pd[port]->port_port = port;1599dd->ipath_pd[port]->port_dd = dd;1600dd->ipath_pd[port]->port_tid_pg_list = ptmp;1601init_waitqueue_head(&dd->ipath_pd[port]->port_wait);1602}1603if (!pd->port_cnt) {1604pd->userversion = uinfo->spu_userversion;1605init_user_egr_sizes(pd);1606if ((ret = init_subports(dd, pd, uinfo)) != 0)1607goto bail;1608ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",1609current->comm, current->pid, dd->ipath_unit,1610port);1611pd->port_cnt = 1;1612port_fp(fp) = pd;1613pd->port_pid = get_pid(task_pid(current));1614strlcpy(pd->port_comm, current->comm, sizeof(pd->port_comm));1615ipath_stats.sps_ports++;1616ret = 0;1617} else1618ret = -EBUSY;16191620bail:1621return ret;1622}16231624static inline int usable(struct ipath_devdata *dd)1625{1626return dd &&1627(dd->ipath_flags & IPATH_PRESENT) &&1628dd->ipath_kregbase &&1629dd->ipath_lid &&1630!(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED1631| IPATH_LINKUNK));1632}16331634static int find_free_port(int unit, struct file *fp,1635const struct ipath_user_info *uinfo)1636{1637struct ipath_devdata *dd = ipath_lookup(unit);1638int ret, i;16391640if (!dd) {1641ret = -ENODEV;1642goto bail;1643}16441645if (!usable(dd)) {1646ret = -ENETDOWN;1647goto bail;1648}16491650for (i = 1; i < dd->ipath_cfgports; i++) {1651ret = try_alloc_port(dd, i, fp, uinfo);1652if (ret != -EBUSY)1653goto bail;1654}1655ret = -EBUSY;16561657bail:1658return ret;1659}16601661static int find_best_unit(struct file *fp,1662const struct ipath_user_info *uinfo)1663{1664int ret = 0, i, prefunit = -1, devmax;1665int maxofallports, npresent, nup;1666int ndev;16671668devmax = ipath_count_units(&npresent, &nup, &maxofallports);16691670/*1671* This code is present to allow a knowledgeable person to1672* specify the layout of processes to processors before opening1673* this driver, and then we'll assign the process to the "closest"1674* InfiniPath chip to that processor (we assume reasonable connectivity,1675* for now). This code assumes that if affinity has been set1676* before this point, that at most one cpu is set; for now this1677* is reasonable. I check for both cpumask_empty() and cpumask_full(),1678* in case some kernel variant sets none of the bits when no1679* affinity is set. 2.6.11 and 12 kernels have all present1680* cpus set. Some day we'll have to fix it up further to handle1681* a cpu subset. This algorithm fails for two HT chips connected1682* in tunnel fashion. Eventually this needs real topology1683* information. There may be some issues with dual core numbering1684* as well. This needs more work prior to release.1685*/1686if (!cpumask_empty(¤t->cpus_allowed) &&1687!cpumask_full(¤t->cpus_allowed)) {1688int ncpus = num_online_cpus(), curcpu = -1, nset = 0;1689for (i = 0; i < ncpus; i++)1690if (cpumask_test_cpu(i, ¤t->cpus_allowed)) {1691ipath_cdbg(PROC, "%s[%u] affinity set for "1692"cpu %d/%d\n", current->comm,1693current->pid, i, ncpus);1694curcpu = i;1695nset++;1696}1697if (curcpu != -1 && nset != ncpus) {1698if (npresent) {1699prefunit = curcpu / (ncpus / npresent);1700ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "1701"%d cpus/chip, select unit %d\n",1702current->comm, current->pid,1703npresent, ncpus, ncpus / npresent,1704prefunit);1705}1706}1707}17081709/*1710* user ports start at 1, kernel port is 01711* For now, we do round-robin access across all chips1712*/17131714if (prefunit != -1)1715devmax = prefunit + 1;1716recheck:1717for (i = 1; i < maxofallports; i++) {1718for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;1719ndev++) {1720struct ipath_devdata *dd = ipath_lookup(ndev);17211722if (!usable(dd))1723continue; /* can't use this unit */1724if (i >= dd->ipath_cfgports)1725/*1726* Maxed out on users of this unit. Try1727* next.1728*/1729continue;1730ret = try_alloc_port(dd, i, fp, uinfo);1731if (!ret)1732goto done;1733}1734}17351736if (npresent) {1737if (nup == 0) {1738ret = -ENETDOWN;1739ipath_dbg("No ports available (none initialized "1740"and ready)\n");1741} else {1742if (prefunit > 0) {1743/* if started above 0, retry from 0 */1744ipath_cdbg(PROC,1745"%s[%u] no ports on prefunit "1746"%d, clear and re-check\n",1747current->comm, current->pid,1748prefunit);1749devmax = ipath_count_units(NULL, NULL,1750NULL);1751prefunit = -1;1752goto recheck;1753}1754ret = -EBUSY;1755ipath_dbg("No ports available\n");1756}1757} else {1758ret = -ENXIO;1759ipath_dbg("No boards found\n");1760}17611762done:1763return ret;1764}17651766static int find_shared_port(struct file *fp,1767const struct ipath_user_info *uinfo)1768{1769int devmax, ndev, i;1770int ret = 0;17711772devmax = ipath_count_units(NULL, NULL, NULL);17731774for (ndev = 0; ndev < devmax; ndev++) {1775struct ipath_devdata *dd = ipath_lookup(ndev);17761777if (!usable(dd))1778continue;1779for (i = 1; i < dd->ipath_cfgports; i++) {1780struct ipath_portdata *pd = dd->ipath_pd[i];17811782/* Skip ports which are not yet open */1783if (!pd || !pd->port_cnt)1784continue;1785/* Skip port if it doesn't match the requested one */1786if (pd->port_subport_id != uinfo->spu_subport_id)1787continue;1788/* Verify the sharing process matches the master */1789if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||1790pd->userversion != uinfo->spu_userversion ||1791pd->port_cnt >= pd->port_subport_cnt) {1792ret = -EINVAL;1793goto done;1794}1795port_fp(fp) = pd;1796subport_fp(fp) = pd->port_cnt++;1797pd->port_subpid[subport_fp(fp)] =1798get_pid(task_pid(current));1799tidcursor_fp(fp) = 0;1800pd->active_slaves |= 1 << subport_fp(fp);1801ipath_cdbg(PROC,1802"%s[%u] %u sharing %s[%u] unit:port %u:%u\n",1803current->comm, current->pid,1804subport_fp(fp),1805pd->port_comm, pid_nr(pd->port_pid),1806dd->ipath_unit, pd->port_port);1807ret = 1;1808goto done;1809}1810}18111812done:1813return ret;1814}18151816static int ipath_open(struct inode *in, struct file *fp)1817{1818/* The real work is performed later in ipath_assign_port() */1819fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);1820return fp->private_data ? 0 : -ENOMEM;1821}18221823/* Get port early, so can set affinity prior to memory allocation */1824static int ipath_assign_port(struct file *fp,1825const struct ipath_user_info *uinfo)1826{1827int ret;1828int i_minor;1829unsigned swmajor, swminor;18301831/* Check to be sure we haven't already initialized this file */1832if (port_fp(fp)) {1833ret = -EINVAL;1834goto done;1835}18361837/* for now, if major version is different, bail */1838swmajor = uinfo->spu_userversion >> 16;1839if (swmajor != IPATH_USER_SWMAJOR) {1840ipath_dbg("User major version %d not same as driver "1841"major %d\n", uinfo->spu_userversion >> 16,1842IPATH_USER_SWMAJOR);1843ret = -ENODEV;1844goto done;1845}18461847swminor = uinfo->spu_userversion & 0xffff;1848if (swminor != IPATH_USER_SWMINOR)1849ipath_dbg("User minor version %d not same as driver "1850"minor %d\n", swminor, IPATH_USER_SWMINOR);18511852mutex_lock(&ipath_mutex);18531854if (ipath_compatible_subports(swmajor, swminor) &&1855uinfo->spu_subport_cnt &&1856(ret = find_shared_port(fp, uinfo))) {1857if (ret > 0)1858ret = 0;1859goto done_chk_sdma;1860}18611862i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;1863ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",1864(long)fp->f_path.dentry->d_inode->i_rdev, i_minor);18651866if (i_minor)1867ret = find_free_port(i_minor - 1, fp, uinfo);1868else1869ret = find_best_unit(fp, uinfo);18701871done_chk_sdma:1872if (!ret) {1873struct ipath_filedata *fd = fp->private_data;1874const struct ipath_portdata *pd = fd->pd;1875const struct ipath_devdata *dd = pd->port_dd;18761877fd->pq = ipath_user_sdma_queue_create(&dd->pcidev->dev,1878dd->ipath_unit,1879pd->port_port,1880fd->subport);18811882if (!fd->pq)1883ret = -ENOMEM;1884}18851886mutex_unlock(&ipath_mutex);18871888done:1889return ret;1890}189118921893static int ipath_do_user_init(struct file *fp,1894const struct ipath_user_info *uinfo)1895{1896int ret;1897struct ipath_portdata *pd = port_fp(fp);1898struct ipath_devdata *dd;1899u32 head32;19001901/* Subports don't need to initialize anything since master did it. */1902if (subport_fp(fp)) {1903ret = wait_event_interruptible(pd->port_wait,1904!test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));1905goto done;1906}19071908dd = pd->port_dd;19091910if (uinfo->spu_rcvhdrsize) {1911ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);1912if (ret)1913goto done;1914}19151916/* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */19171918/* some ports may get extra buffers, calculate that here */1919if (pd->port_port <= dd->ipath_ports_extrabuf)1920pd->port_piocnt = dd->ipath_pbufsport + 1;1921else1922pd->port_piocnt = dd->ipath_pbufsport;19231924/* for right now, kernel piobufs are at end, so port 1 is at 0 */1925if (pd->port_port <= dd->ipath_ports_extrabuf)1926pd->port_pio_base = (dd->ipath_pbufsport + 1)1927* (pd->port_port - 1);1928else1929pd->port_pio_base = dd->ipath_ports_extrabuf +1930dd->ipath_pbufsport * (pd->port_port - 1);1931pd->port_piobufs = dd->ipath_piobufbase +1932pd->port_pio_base * dd->ipath_palign;1933ipath_cdbg(VERBOSE, "piobuf base for port %u is 0x%x, piocnt %u,"1934" first pio %u\n", pd->port_port, pd->port_piobufs,1935pd->port_piocnt, pd->port_pio_base);1936ipath_chg_pioavailkernel(dd, pd->port_pio_base, pd->port_piocnt, 0);19371938/*1939* Now allocate the rcvhdr Q and eager TIDs; skip the TID1940* array for time being. If pd->port_port > chip-supported,1941* we need to do extra stuff here to handle by handling overflow1942* through port 0, someday1943*/1944ret = ipath_create_rcvhdrq(dd, pd);1945if (!ret)1946ret = ipath_create_user_egr(pd);1947if (ret)1948goto done;19491950/*1951* set the eager head register for this port to the current values1952* of the tail pointers, since we don't know if they were1953* updated on last use of the port.1954*/1955head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);1956ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);1957pd->port_lastrcvhdrqtail = -1;1958ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",1959pd->port_port, head32);1960pd->port_tidcursor = 0; /* start at beginning after open */19611962/* initialize poll variables... */1963pd->port_urgent = 0;1964pd->port_urgent_poll = 0;1965pd->port_hdrqfull_poll = pd->port_hdrqfull;19661967/*1968* Now enable the port for receive.1969* For chips that are set to DMA the tail register to memory1970* when they change (and when the update bit transitions from1971* 0 to 1. So for those chips, we turn it off and then back on.1972* This will (very briefly) affect any other open ports, but the1973* duration is very short, and therefore isn't an issue. We1974* explicitly set the in-memory tail copy to 0 beforehand, so we1975* don't have to wait to be sure the DMA update has happened1976* (chip resets head/tail to 0 on transition to enable).1977*/1978set_bit(dd->ipath_r_portenable_shift + pd->port_port,1979&dd->ipath_rcvctrl);1980if (!(dd->ipath_flags & IPATH_NODMA_RTAIL)) {1981if (pd->port_rcvhdrtail_kvaddr)1982ipath_clear_rcvhdrtail(pd);1983ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,1984dd->ipath_rcvctrl &1985~(1ULL << dd->ipath_r_tailupd_shift));1986}1987ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,1988dd->ipath_rcvctrl);1989/* Notify any waiting slaves */1990if (pd->port_subport_cnt) {1991clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);1992wake_up(&pd->port_wait);1993}1994done:1995return ret;1996}19971998/**1999* unlock_exptid - unlock any expected TID entries port still had in use2000* @pd: port2001*2002* We don't actually update the chip here, because we do a bulk update2003* below, using ipath_f_clear_tids.2004*/2005static void unlock_expected_tids(struct ipath_portdata *pd)2006{2007struct ipath_devdata *dd = pd->port_dd;2008int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;2009int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;20102011ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",2012pd->port_port);2013for (i = port_tidbase; i < maxtid; i++) {2014struct page *ps = dd->ipath_pageshadow[i];20152016if (!ps)2017continue;20182019dd->ipath_pageshadow[i] = NULL;2020pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],2021PAGE_SIZE, PCI_DMA_FROMDEVICE);2022ipath_release_user_pages_on_close(&ps, 1);2023cnt++;2024ipath_stats.sps_pageunlocks++;2025}2026if (cnt)2027ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",2028pd->port_port, cnt);20292030if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)2031ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",2032(unsigned long long) ipath_stats.sps_pagelocks,2033(unsigned long long)2034ipath_stats.sps_pageunlocks);2035}20362037static int ipath_close(struct inode *in, struct file *fp)2038{2039int ret = 0;2040struct ipath_filedata *fd;2041struct ipath_portdata *pd;2042struct ipath_devdata *dd;2043unsigned long flags;2044unsigned port;2045struct pid *pid;20462047ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",2048(long)in->i_rdev, fp->private_data);20492050mutex_lock(&ipath_mutex);20512052fd = fp->private_data;2053fp->private_data = NULL;2054pd = fd->pd;2055if (!pd) {2056mutex_unlock(&ipath_mutex);2057goto bail;2058}20592060dd = pd->port_dd;20612062/* drain user sdma queue */2063ipath_user_sdma_queue_drain(dd, fd->pq);2064ipath_user_sdma_queue_destroy(fd->pq);20652066if (--pd->port_cnt) {2067/*2068* XXX If the master closes the port before the slave(s),2069* revoke the mmap for the eager receive queue so2070* the slave(s) don't wait for receive data forever.2071*/2072pd->active_slaves &= ~(1 << fd->subport);2073put_pid(pd->port_subpid[fd->subport]);2074pd->port_subpid[fd->subport] = NULL;2075mutex_unlock(&ipath_mutex);2076goto bail;2077}2078/* early; no interrupt users after this */2079spin_lock_irqsave(&dd->ipath_uctxt_lock, flags);2080port = pd->port_port;2081dd->ipath_pd[port] = NULL;2082pid = pd->port_pid;2083pd->port_pid = NULL;2084spin_unlock_irqrestore(&dd->ipath_uctxt_lock, flags);20852086if (pd->port_rcvwait_to || pd->port_piowait_to2087|| pd->port_rcvnowait || pd->port_pionowait) {2088ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "2089"%u rcv %u, pio already\n",2090pd->port_port, pd->port_rcvwait_to,2091pd->port_piowait_to, pd->port_rcvnowait,2092pd->port_pionowait);2093pd->port_rcvwait_to = pd->port_piowait_to =2094pd->port_rcvnowait = pd->port_pionowait = 0;2095}2096if (pd->port_flag) {2097ipath_cdbg(PROC, "port %u port_flag set: 0x%lx\n",2098pd->port_port, pd->port_flag);2099pd->port_flag = 0;2100}21012102if (dd->ipath_kregbase) {2103/* atomically clear receive enable port and intr avail. */2104clear_bit(dd->ipath_r_portenable_shift + port,2105&dd->ipath_rcvctrl);2106clear_bit(pd->port_port + dd->ipath_r_intravail_shift,2107&dd->ipath_rcvctrl);2108ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,2109dd->ipath_rcvctrl);2110/* and read back from chip to be sure that nothing2111* else is in flight when we do the rest */2112(void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);21132114/* clean up the pkeys for this port user */2115ipath_clean_part_key(pd, dd);2116/*2117* be paranoid, and never write 0's to these, just use an2118* unused part of the port 0 tail page. Of course,2119* rcvhdraddr points to a large chunk of memory, so this2120* could still trash things, but at least it won't trash2121* page 0, and by disabling the port, it should stop "soon",2122* even if a packet or two is in already in flight after we2123* disabled the port.2124*/2125ipath_write_kreg_port(dd,2126dd->ipath_kregs->kr_rcvhdrtailaddr, port,2127dd->ipath_dummy_hdrq_phys);2128ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,2129pd->port_port, dd->ipath_dummy_hdrq_phys);21302131ipath_disarm_piobufs(dd, pd->port_pio_base, pd->port_piocnt);2132ipath_chg_pioavailkernel(dd, pd->port_pio_base,2133pd->port_piocnt, 1);21342135dd->ipath_f_clear_tids(dd, pd->port_port);21362137if (dd->ipath_pageshadow)2138unlock_expected_tids(pd);2139ipath_stats.sps_ports--;2140ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",2141pd->port_comm, pid_nr(pid),2142dd->ipath_unit, port);2143}21442145put_pid(pid);2146mutex_unlock(&ipath_mutex);2147ipath_free_pddata(dd, pd); /* after releasing the mutex */21482149bail:2150kfree(fd);2151return ret;2152}21532154static int ipath_port_info(struct ipath_portdata *pd, u16 subport,2155struct ipath_port_info __user *uinfo)2156{2157struct ipath_port_info info;2158int nup;2159int ret;2160size_t sz;21612162(void) ipath_count_units(NULL, &nup, NULL);2163info.num_active = nup;2164info.unit = pd->port_dd->ipath_unit;2165info.port = pd->port_port;2166info.subport = subport;2167/* Don't return new fields if old library opened the port. */2168if (ipath_supports_subports(pd->userversion >> 16,2169pd->userversion & 0xffff)) {2170/* Number of user ports available for this device. */2171info.num_ports = pd->port_dd->ipath_cfgports - 1;2172info.num_subports = pd->port_subport_cnt;2173sz = sizeof(info);2174} else2175sz = sizeof(info) - 2 * sizeof(u16);21762177if (copy_to_user(uinfo, &info, sz)) {2178ret = -EFAULT;2179goto bail;2180}2181ret = 0;21822183bail:2184return ret;2185}21862187static int ipath_get_slave_info(struct ipath_portdata *pd,2188void __user *slave_mask_addr)2189{2190int ret = 0;21912192if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))2193ret = -EFAULT;2194return ret;2195}21962197static int ipath_sdma_get_inflight(struct ipath_user_sdma_queue *pq,2198u32 __user *inflightp)2199{2200const u32 val = ipath_user_sdma_inflight_counter(pq);22012202if (put_user(val, inflightp))2203return -EFAULT;22042205return 0;2206}22072208static int ipath_sdma_get_complete(struct ipath_devdata *dd,2209struct ipath_user_sdma_queue *pq,2210u32 __user *completep)2211{2212u32 val;2213int err;22142215err = ipath_user_sdma_make_progress(dd, pq);2216if (err < 0)2217return err;22182219val = ipath_user_sdma_complete_counter(pq);2220if (put_user(val, completep))2221return -EFAULT;22222223return 0;2224}22252226static ssize_t ipath_write(struct file *fp, const char __user *data,2227size_t count, loff_t *off)2228{2229const struct ipath_cmd __user *ucmd;2230struct ipath_portdata *pd;2231const void __user *src;2232size_t consumed, copy;2233struct ipath_cmd cmd;2234ssize_t ret = 0;2235void *dest;22362237if (count < sizeof(cmd.type)) {2238ret = -EINVAL;2239goto bail;2240}22412242ucmd = (const struct ipath_cmd __user *) data;22432244if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {2245ret = -EFAULT;2246goto bail;2247}22482249consumed = sizeof(cmd.type);22502251switch (cmd.type) {2252case IPATH_CMD_ASSIGN_PORT:2253case __IPATH_CMD_USER_INIT:2254case IPATH_CMD_USER_INIT:2255copy = sizeof(cmd.cmd.user_info);2256dest = &cmd.cmd.user_info;2257src = &ucmd->cmd.user_info;2258break;2259case IPATH_CMD_RECV_CTRL:2260copy = sizeof(cmd.cmd.recv_ctrl);2261dest = &cmd.cmd.recv_ctrl;2262src = &ucmd->cmd.recv_ctrl;2263break;2264case IPATH_CMD_PORT_INFO:2265copy = sizeof(cmd.cmd.port_info);2266dest = &cmd.cmd.port_info;2267src = &ucmd->cmd.port_info;2268break;2269case IPATH_CMD_TID_UPDATE:2270case IPATH_CMD_TID_FREE:2271copy = sizeof(cmd.cmd.tid_info);2272dest = &cmd.cmd.tid_info;2273src = &ucmd->cmd.tid_info;2274break;2275case IPATH_CMD_SET_PART_KEY:2276copy = sizeof(cmd.cmd.part_key);2277dest = &cmd.cmd.part_key;2278src = &ucmd->cmd.part_key;2279break;2280case __IPATH_CMD_SLAVE_INFO:2281copy = sizeof(cmd.cmd.slave_mask_addr);2282dest = &cmd.cmd.slave_mask_addr;2283src = &ucmd->cmd.slave_mask_addr;2284break;2285case IPATH_CMD_PIOAVAILUPD: // force an update of PIOAvail reg2286copy = 0;2287src = NULL;2288dest = NULL;2289break;2290case IPATH_CMD_POLL_TYPE:2291copy = sizeof(cmd.cmd.poll_type);2292dest = &cmd.cmd.poll_type;2293src = &ucmd->cmd.poll_type;2294break;2295case IPATH_CMD_ARMLAUNCH_CTRL:2296copy = sizeof(cmd.cmd.armlaunch_ctrl);2297dest = &cmd.cmd.armlaunch_ctrl;2298src = &ucmd->cmd.armlaunch_ctrl;2299break;2300case IPATH_CMD_SDMA_INFLIGHT:2301copy = sizeof(cmd.cmd.sdma_inflight);2302dest = &cmd.cmd.sdma_inflight;2303src = &ucmd->cmd.sdma_inflight;2304break;2305case IPATH_CMD_SDMA_COMPLETE:2306copy = sizeof(cmd.cmd.sdma_complete);2307dest = &cmd.cmd.sdma_complete;2308src = &ucmd->cmd.sdma_complete;2309break;2310default:2311ret = -EINVAL;2312goto bail;2313}23142315if (copy) {2316if ((count - consumed) < copy) {2317ret = -EINVAL;2318goto bail;2319}23202321if (copy_from_user(dest, src, copy)) {2322ret = -EFAULT;2323goto bail;2324}23252326consumed += copy;2327}23282329pd = port_fp(fp);2330if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&2331cmd.type != IPATH_CMD_ASSIGN_PORT) {2332ret = -EINVAL;2333goto bail;2334}23352336switch (cmd.type) {2337case IPATH_CMD_ASSIGN_PORT:2338ret = ipath_assign_port(fp, &cmd.cmd.user_info);2339if (ret)2340goto bail;2341break;2342case __IPATH_CMD_USER_INIT:2343/* backwards compatibility, get port first */2344ret = ipath_assign_port(fp, &cmd.cmd.user_info);2345if (ret)2346goto bail;2347/* and fall through to current version. */2348case IPATH_CMD_USER_INIT:2349ret = ipath_do_user_init(fp, &cmd.cmd.user_info);2350if (ret)2351goto bail;2352ret = ipath_get_base_info(2353fp, (void __user *) (unsigned long)2354cmd.cmd.user_info.spu_base_info,2355cmd.cmd.user_info.spu_base_info_size);2356break;2357case IPATH_CMD_RECV_CTRL:2358ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);2359break;2360case IPATH_CMD_PORT_INFO:2361ret = ipath_port_info(pd, subport_fp(fp),2362(struct ipath_port_info __user *)2363(unsigned long) cmd.cmd.port_info);2364break;2365case IPATH_CMD_TID_UPDATE:2366ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);2367break;2368case IPATH_CMD_TID_FREE:2369ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);2370break;2371case IPATH_CMD_SET_PART_KEY:2372ret = ipath_set_part_key(pd, cmd.cmd.part_key);2373break;2374case __IPATH_CMD_SLAVE_INFO:2375ret = ipath_get_slave_info(pd,2376(void __user *) (unsigned long)2377cmd.cmd.slave_mask_addr);2378break;2379case IPATH_CMD_PIOAVAILUPD:2380ipath_force_pio_avail_update(pd->port_dd);2381break;2382case IPATH_CMD_POLL_TYPE:2383pd->poll_type = cmd.cmd.poll_type;2384break;2385case IPATH_CMD_ARMLAUNCH_CTRL:2386if (cmd.cmd.armlaunch_ctrl)2387ipath_enable_armlaunch(pd->port_dd);2388else2389ipath_disable_armlaunch(pd->port_dd);2390break;2391case IPATH_CMD_SDMA_INFLIGHT:2392ret = ipath_sdma_get_inflight(user_sdma_queue_fp(fp),2393(u32 __user *) (unsigned long)2394cmd.cmd.sdma_inflight);2395break;2396case IPATH_CMD_SDMA_COMPLETE:2397ret = ipath_sdma_get_complete(pd->port_dd,2398user_sdma_queue_fp(fp),2399(u32 __user *) (unsigned long)2400cmd.cmd.sdma_complete);2401break;2402}24032404if (ret >= 0)2405ret = consumed;24062407bail:2408return ret;2409}24102411static ssize_t ipath_writev(struct kiocb *iocb, const struct iovec *iov,2412unsigned long dim, loff_t off)2413{2414struct file *filp = iocb->ki_filp;2415struct ipath_filedata *fp = filp->private_data;2416struct ipath_portdata *pd = port_fp(filp);2417struct ipath_user_sdma_queue *pq = fp->pq;24182419if (!dim)2420return -EINVAL;24212422return ipath_user_sdma_writev(pd->port_dd, pq, iov, dim);2423}24242425static struct class *ipath_class;24262427static int init_cdev(int minor, char *name, const struct file_operations *fops,2428struct cdev **cdevp, struct device **devp)2429{2430const dev_t dev = MKDEV(IPATH_MAJOR, minor);2431struct cdev *cdev = NULL;2432struct device *device = NULL;2433int ret;24342435cdev = cdev_alloc();2436if (!cdev) {2437printk(KERN_ERR IPATH_DRV_NAME2438": Could not allocate cdev for minor %d, %s\n",2439minor, name);2440ret = -ENOMEM;2441goto done;2442}24432444cdev->owner = THIS_MODULE;2445cdev->ops = fops;2446kobject_set_name(&cdev->kobj, name);24472448ret = cdev_add(cdev, dev, 1);2449if (ret < 0) {2450printk(KERN_ERR IPATH_DRV_NAME2451": Could not add cdev for minor %d, %s (err %d)\n",2452minor, name, -ret);2453goto err_cdev;2454}24552456device = device_create(ipath_class, NULL, dev, NULL, name);24572458if (IS_ERR(device)) {2459ret = PTR_ERR(device);2460printk(KERN_ERR IPATH_DRV_NAME ": Could not create "2461"device for minor %d, %s (err %d)\n",2462minor, name, -ret);2463goto err_cdev;2464}24652466goto done;24672468err_cdev:2469cdev_del(cdev);2470cdev = NULL;24712472done:2473if (ret >= 0) {2474*cdevp = cdev;2475*devp = device;2476} else {2477*cdevp = NULL;2478*devp = NULL;2479}24802481return ret;2482}24832484int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,2485struct cdev **cdevp, struct device **devp)2486{2487return init_cdev(minor, name, fops, cdevp, devp);2488}24892490static void cleanup_cdev(struct cdev **cdevp,2491struct device **devp)2492{2493struct device *dev = *devp;24942495if (dev) {2496device_unregister(dev);2497*devp = NULL;2498}24992500if (*cdevp) {2501cdev_del(*cdevp);2502*cdevp = NULL;2503}2504}25052506void ipath_cdev_cleanup(struct cdev **cdevp,2507struct device **devp)2508{2509cleanup_cdev(cdevp, devp);2510}25112512static struct cdev *wildcard_cdev;2513static struct device *wildcard_dev;25142515static const dev_t dev = MKDEV(IPATH_MAJOR, 0);25162517static int user_init(void)2518{2519int ret;25202521ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);2522if (ret < 0) {2523printk(KERN_ERR IPATH_DRV_NAME ": Could not register "2524"chrdev region (err %d)\n", -ret);2525goto done;2526}25272528ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);25292530if (IS_ERR(ipath_class)) {2531ret = PTR_ERR(ipath_class);2532printk(KERN_ERR IPATH_DRV_NAME ": Could not create "2533"device class (err %d)\n", -ret);2534goto bail;2535}25362537goto done;2538bail:2539unregister_chrdev_region(dev, IPATH_NMINORS);2540done:2541return ret;2542}25432544static void user_cleanup(void)2545{2546if (ipath_class) {2547class_destroy(ipath_class);2548ipath_class = NULL;2549}25502551unregister_chrdev_region(dev, IPATH_NMINORS);2552}25532554static atomic_t user_count = ATOMIC_INIT(0);2555static atomic_t user_setup = ATOMIC_INIT(0);25562557int ipath_user_add(struct ipath_devdata *dd)2558{2559char name[10];2560int ret;25612562if (atomic_inc_return(&user_count) == 1) {2563ret = user_init();2564if (ret < 0) {2565ipath_dev_err(dd, "Unable to set up user support: "2566"error %d\n", -ret);2567goto bail;2568}2569ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,2570&wildcard_dev);2571if (ret < 0) {2572ipath_dev_err(dd, "Could not create wildcard "2573"minor: error %d\n", -ret);2574goto bail_user;2575}25762577atomic_set(&user_setup, 1);2578}25792580snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);25812582ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,2583&dd->user_cdev, &dd->user_dev);2584if (ret < 0)2585ipath_dev_err(dd, "Could not create user minor %d, %s\n",2586dd->ipath_unit + 1, name);25872588goto bail;25892590bail_user:2591user_cleanup();2592bail:2593return ret;2594}25952596void ipath_user_remove(struct ipath_devdata *dd)2597{2598cleanup_cdev(&dd->user_cdev, &dd->user_dev);25992600if (atomic_dec_return(&user_count) == 0) {2601if (atomic_read(&user_setup) == 0)2602goto bail;26032604cleanup_cdev(&wildcard_cdev, &wildcard_dev);2605user_cleanup();26062607atomic_set(&user_setup, 0);2608}2609bail:2610return;2611}261226132614