]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/rdma/ipath/ipath_file_ops.c
Merge branch 'akpm-current/current'
[karo-tx-linux.git] / drivers / staging / rdma / ipath / ipath_file_ops.c
1 /*
2  * Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.
3  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/pci.h>
35 #include <linux/poll.h>
36 #include <linux/cdev.h>
37 #include <linux/swap.h>
38 #include <linux/export.h>
39 #include <linux/vmalloc.h>
40 #include <linux/slab.h>
41 #include <linux/highmem.h>
42 #include <linux/io.h>
43 #include <linux/jiffies.h>
44 #include <linux/cpu.h>
45 #include <linux/uio.h>
46 #include <asm/pgtable.h>
47
48 #include "ipath_kernel.h"
49 #include "ipath_common.h"
50 #include "ipath_user_sdma.h"
51
52 static int ipath_open(struct inode *, struct file *);
53 static int ipath_close(struct inode *, struct file *);
54 static ssize_t ipath_write(struct file *, const char __user *, size_t,
55                            loff_t *);
56 static ssize_t ipath_write_iter(struct kiocb *, struct iov_iter *from);
57 static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
58 static int ipath_mmap(struct file *, struct vm_area_struct *);
59
60 /*
61  * This is really, really weird shit - write() and writev() here
62  * have completely unrelated semantics.  Sucky userland ABI,
63  * film at 11.
64  */
65 static const struct file_operations ipath_file_ops = {
66         .owner = THIS_MODULE,
67         .write = ipath_write,
68         .write_iter = ipath_write_iter,
69         .open = ipath_open,
70         .release = ipath_close,
71         .poll = ipath_poll,
72         .mmap = ipath_mmap,
73         .llseek = noop_llseek,
74 };
75
76 /*
77  * Convert kernel virtual addresses to physical addresses so they don't
78  * potentially conflict with the chip addresses used as mmap offsets.
79  * It doesn't really matter what mmap offset we use as long as we can
80  * interpret it correctly.
81  */
82 static u64 cvt_kvaddr(void *p)
83 {
84         struct page *page;
85         u64 paddr = 0;
86
87         page = vmalloc_to_page(p);
88         if (page)
89                 paddr = page_to_pfn(page) << PAGE_SHIFT;
90
91         return paddr;
92 }
93
94 static int ipath_get_base_info(struct file *fp,
95                                void __user *ubase, size_t ubase_size)
96 {
97         struct ipath_portdata *pd = port_fp(fp);
98         int ret = 0;
99         struct ipath_base_info *kinfo = NULL;
100         struct ipath_devdata *dd = pd->port_dd;
101         unsigned subport_cnt;
102         int shared, master;
103         size_t sz;
104
105         subport_cnt = pd->port_subport_cnt;
106         if (!subport_cnt) {
107                 shared = 0;
108                 master = 0;
109                 subport_cnt = 1;
110         } else {
111                 shared = 1;
112                 master = !subport_fp(fp);
113         }
114
115         sz = sizeof(*kinfo);
116         /* If port sharing is not requested, allow the old size structure */
117         if (!shared)
118                 sz -= 7 * sizeof(u64);
119         if (ubase_size < sz) {
120                 ipath_cdbg(PROC,
121                            "Base size %zu, need %zu (version mismatch?)\n",
122                            ubase_size, sz);
123                 ret = -EINVAL;
124                 goto bail;
125         }
126
127         kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
128         if (kinfo == NULL) {
129                 ret = -ENOMEM;
130                 goto bail;
131         }
132
133         ret = dd->ipath_f_get_base_info(pd, kinfo);
134         if (ret < 0)
135                 goto bail;
136
137         kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
138         kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
139         kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
140         kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
141         /*
142          * have to mmap whole thing
143          */
144         kinfo->spi_rcv_egrbuftotlen =
145                 pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
146         kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
147         kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
148                 pd->port_rcvegrbuf_chunks;
149         kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;
150         if (master)
151                 kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;
152         /*
153          * for this use, may be ipath_cfgports summed over all chips that
154          * are are configured and present
155          */
156         kinfo->spi_nports = dd->ipath_cfgports;
157         /* unit (chip/board) our port is on */
158         kinfo->spi_unit = dd->ipath_unit;
159         /* for now, only a single page */
160         kinfo->spi_tid_maxsize = PAGE_SIZE;
161
162         /*
163          * Doing this per port, and based on the skip value, etc.  This has
164          * to be the actual buffer size, since the protocol code treats it
165          * as an array.
166          *
167          * These have to be set to user addresses in the user code via mmap.
168          * These values are used on return to user code for the mmap target
169          * addresses only.  For 32 bit, same 44 bit address problem, so use
170          * the physical address, not virtual.  Before 2.6.11, using the
171          * page_address() macro worked, but in 2.6.11, even that returns the
172          * full 64 bit address (upper bits all 1's).  So far, using the
173          * physical addresses (or chip offsets, for chip mapping) works, but
174          * no doubt some future kernel release will change that, and we'll be
175          * on to yet another method of dealing with this.
176          */
177         kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
178         kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;
179         kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
180         kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
181         kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
182                 (void *) dd->ipath_statusp -
183                 (void *) dd->ipath_pioavailregs_dma;
184         if (!shared) {
185                 kinfo->spi_piocnt = pd->port_piocnt;
186                 kinfo->spi_piobufbase = (u64) pd->port_piobufs;
187                 kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
188                         dd->ipath_ureg_align * pd->port_port;
189         } else if (master) {
190                 kinfo->spi_piocnt = (pd->port_piocnt / subport_cnt) +
191                                     (pd->port_piocnt % subport_cnt);
192                 /* Master's PIO buffers are after all the slave's */
193                 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
194                         dd->ipath_palign *
195                         (pd->port_piocnt - kinfo->spi_piocnt);
196         } else {
197                 unsigned slave = subport_fp(fp) - 1;
198
199                 kinfo->spi_piocnt = pd->port_piocnt / subport_cnt;
200                 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
201                         dd->ipath_palign * kinfo->spi_piocnt * slave;
202         }
203
204         if (shared) {
205                 kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase +
206                         dd->ipath_ureg_align * pd->port_port;
207                 kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs;
208                 kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base;
209                 kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr;
210
211                 kinfo->__spi_uregbase = cvt_kvaddr(pd->subport_uregbase +
212                         PAGE_SIZE * subport_fp(fp));
213
214                 kinfo->spi_rcvhdr_base = cvt_kvaddr(pd->subport_rcvhdr_base +
215                         pd->port_rcvhdrq_size * subport_fp(fp));
216                 kinfo->spi_rcvhdr_tailaddr = 0;
217                 kinfo->spi_rcv_egrbufs = cvt_kvaddr(pd->subport_rcvegrbuf +
218                         pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size *
219                         subport_fp(fp));
220
221                 kinfo->spi_subport_uregbase =
222                         cvt_kvaddr(pd->subport_uregbase);
223                 kinfo->spi_subport_rcvegrbuf =
224                         cvt_kvaddr(pd->subport_rcvegrbuf);
225                 kinfo->spi_subport_rcvhdr_base =
226                         cvt_kvaddr(pd->subport_rcvhdr_base);
227                 ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",
228                         kinfo->spi_port, kinfo->spi_runtime_flags,
229                         (unsigned long long) kinfo->spi_subport_uregbase,
230                         (unsigned long long) kinfo->spi_subport_rcvegrbuf,
231                         (unsigned long long) kinfo->spi_subport_rcvhdr_base);
232         }
233
234         /*
235          * All user buffers are 2KB buffers.  If we ever support
236          * giving 4KB buffers to user processes, this will need some
237          * work.
238          */
239         kinfo->spi_pioindex = (kinfo->spi_piobufbase -
240                 (dd->ipath_piobufbase & 0xffffffff)) / dd->ipath_palign;
241         kinfo->spi_pioalign = dd->ipath_palign;
242
243         kinfo->spi_qpair = IPATH_KD_QP;
244         /*
245          * user mode PIO buffers are always 2KB, even when 4KB can
246          * be received, and sent via the kernel; this is ibmaxlen
247          * for 2K MTU.
248          */
249         kinfo->spi_piosize = dd->ipath_piosize2k - 2 * sizeof(u32);
250         kinfo->spi_mtu = dd->ipath_ibmaxlen;    /* maxlen, not ibmtu */
251         kinfo->spi_port = pd->port_port;
252         kinfo->spi_subport = subport_fp(fp);
253         kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
254         kinfo->spi_hw_version = dd->ipath_revision;
255
256         if (master) {
257                 kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;
258         }
259
260         sz = (ubase_size < sizeof(*kinfo)) ? ubase_size : sizeof(*kinfo);
261         if (copy_to_user(ubase, kinfo, sz))
262                 ret = -EFAULT;
263
264 bail:
265         kfree(kinfo);
266         return ret;
267 }
268
269 /**
270  * ipath_tid_update - update a port TID
271  * @pd: the port
272  * @fp: the ipath device file
273  * @ti: the TID information
274  *
275  * The new implementation as of Oct 2004 is that the driver assigns
276  * the tid and returns it to the caller.   To make it easier to
277  * catch bugs, and to reduce search time, we keep a cursor for
278  * each port, walking the shadow tid array to find one that's not
279  * in use.
280  *
281  * For now, if we can't allocate the full list, we fail, although
282  * in the long run, we'll allocate as many as we can, and the
283  * caller will deal with that by trying the remaining pages later.
284  * That means that when we fail, we have to mark the tids as not in
285  * use again, in our shadow copy.
286  *
287  * It's up to the caller to free the tids when they are done.
288  * We'll unlock the pages as they free them.
289  *
290  * Also, right now we are locking one page at a time, but since
291  * the intended use of this routine is for a single group of
292  * virtually contiguous pages, that should change to improve
293  * performance.
294  */
295 static int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,
296                             const struct ipath_tid_info *ti)
297 {
298         int ret = 0, ntids;
299         u32 tid, porttid, cnt, i, tidcnt, tidoff;
300         u16 *tidlist;
301         struct ipath_devdata *dd = pd->port_dd;
302         u64 physaddr;
303         unsigned long vaddr;
304         u64 __iomem *tidbase;
305         unsigned long tidmap[8];
306         struct page **pagep = NULL;
307         unsigned subport = subport_fp(fp);
308
309         if (!dd->ipath_pageshadow) {
310                 ret = -ENOMEM;
311                 goto done;
312         }
313
314         cnt = ti->tidcnt;
315         if (!cnt) {
316                 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
317                           (unsigned long long) ti->tidlist);
318                 /*
319                  * Should we treat as success?  likely a bug
320                  */
321                 ret = -EFAULT;
322                 goto done;
323         }
324         porttid = pd->port_port * dd->ipath_rcvtidcnt;
325         if (!pd->port_subport_cnt) {
326                 tidcnt = dd->ipath_rcvtidcnt;
327                 tid = pd->port_tidcursor;
328                 tidoff = 0;
329         } else if (!subport) {
330                 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
331                          (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
332                 tidoff = dd->ipath_rcvtidcnt - tidcnt;
333                 porttid += tidoff;
334                 tid = tidcursor_fp(fp);
335         } else {
336                 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
337                 tidoff = tidcnt * (subport - 1);
338                 porttid += tidoff;
339                 tid = tidcursor_fp(fp);
340         }
341         if (cnt > tidcnt) {
342                 /* make sure it all fits in port_tid_pg_list */
343                 dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
344                          "TIDs, only trying max (%u)\n", cnt, tidcnt);
345                 cnt = tidcnt;
346         }
347         pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];
348         tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];
349
350         memset(tidmap, 0, sizeof(tidmap));
351         /* before decrement; chip actual # */
352         ntids = tidcnt;
353         tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
354                                    dd->ipath_rcvtidbase +
355                                    porttid * sizeof(*tidbase));
356
357         ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
358                    pd->port_port, cnt, tid, tidbase);
359
360         /* virtual address of first page in transfer */
361         vaddr = ti->tidvaddr;
362         if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
363                        cnt * PAGE_SIZE)) {
364                 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
365                           (void *)vaddr, cnt);
366                 ret = -EFAULT;
367                 goto done;
368         }
369         ret = ipath_get_user_pages(vaddr, cnt, pagep);
370         if (ret) {
371                 if (ret == -EBUSY) {
372                         ipath_dbg("Failed to lock addr %p, %u pages "
373                                   "(already locked)\n",
374                                   (void *) vaddr, cnt);
375                         /*
376                          * for now, continue, and see what happens but with
377                          * the new implementation, this should never happen,
378                          * unless perhaps the user has mpin'ed the pages
379                          * themselves (something we need to test)
380                          */
381                         ret = 0;
382                 } else {
383                         dev_info(&dd->pcidev->dev,
384                                  "Failed to lock addr %p, %u pages: "
385                                  "errno %d\n", (void *) vaddr, cnt, -ret);
386                         goto done;
387                 }
388         }
389         for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
390                 for (; ntids--; tid++) {
391                         if (tid == tidcnt)
392                                 tid = 0;
393                         if (!dd->ipath_pageshadow[porttid + tid])
394                                 break;
395                 }
396                 if (ntids < 0) {
397                         /*
398                          * oops, wrapped all the way through their TIDs,
399                          * and didn't have enough free; see comments at
400                          * start of routine
401                          */
402                         ipath_dbg("Not enough free TIDs for %u pages "
403                                   "(index %d), failing\n", cnt, i);
404                         i--;    /* last tidlist[i] not filled in */
405                         ret = -ENOMEM;
406                         break;
407                 }
408                 tidlist[i] = tid + tidoff;
409                 ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
410                            "vaddr %lx\n", i, tid + tidoff, vaddr);
411                 /* we "know" system pages and TID pages are same size */
412                 dd->ipath_pageshadow[porttid + tid] = pagep[i];
413                 dd->ipath_physshadow[porttid + tid] = ipath_map_page(
414                         dd->pcidev, pagep[i], 0, PAGE_SIZE,
415                         PCI_DMA_FROMDEVICE);
416                 /*
417                  * don't need atomic or it's overhead
418                  */
419                 __set_bit(tid, tidmap);
420                 physaddr = dd->ipath_physshadow[porttid + tid];
421                 ipath_stats.sps_pagelocks++;
422                 ipath_cdbg(VERBOSE,
423                            "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
424                            tid, vaddr, (unsigned long long) physaddr,
425                            pagep[i]);
426                 dd->ipath_f_put_tid(dd, &tidbase[tid], RCVHQ_RCV_TYPE_EXPECTED,
427                                     physaddr);
428                 /*
429                  * don't check this tid in ipath_portshadow, since we
430                  * just filled it in; start with the next one.
431                  */
432                 tid++;
433         }
434
435         if (ret) {
436                 u32 limit;
437         cleanup:
438                 /* jump here if copy out of updated info failed... */
439                 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
440                           -ret, i, cnt);
441                 /* same code that's in ipath_free_tid() */
442                 limit = sizeof(tidmap) * BITS_PER_BYTE;
443                 if (limit > tidcnt)
444                         /* just in case size changes in future */
445                         limit = tidcnt;
446                 tid = find_first_bit((const unsigned long *)tidmap, limit);
447                 for (; tid < limit; tid++) {
448                         if (!test_bit(tid, tidmap))
449                                 continue;
450                         if (dd->ipath_pageshadow[porttid + tid]) {
451                                 ipath_cdbg(VERBOSE, "Freeing TID %u\n",
452                                            tid);
453                                 dd->ipath_f_put_tid(dd, &tidbase[tid],
454                                                     RCVHQ_RCV_TYPE_EXPECTED,
455                                                     dd->ipath_tidinvalid);
456                                 pci_unmap_page(dd->pcidev,
457                                         dd->ipath_physshadow[porttid + tid],
458                                         PAGE_SIZE, PCI_DMA_FROMDEVICE);
459                                 dd->ipath_pageshadow[porttid + tid] = NULL;
460                                 ipath_stats.sps_pageunlocks++;
461                         }
462                 }
463                 ipath_release_user_pages(pagep, cnt);
464         } else {
465                 /*
466                  * Copy the updated array, with ipath_tid's filled in, back
467                  * to user.  Since we did the copy in already, this "should
468                  * never fail" If it does, we have to clean up...
469                  */
470                 if (copy_to_user((void __user *)
471                                  (unsigned long) ti->tidlist,
472                                  tidlist, cnt * sizeof(*tidlist))) {
473                         ret = -EFAULT;
474                         goto cleanup;
475                 }
476                 if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
477                                  tidmap, sizeof tidmap)) {
478                         ret = -EFAULT;
479                         goto cleanup;
480                 }
481                 if (tid == tidcnt)
482                         tid = 0;
483                 if (!pd->port_subport_cnt)
484                         pd->port_tidcursor = tid;
485                 else
486                         tidcursor_fp(fp) = tid;
487         }
488
489 done:
490         if (ret)
491                 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
492                           ti->tidcnt, -ret);
493         return ret;
494 }
495
496 /**
497  * ipath_tid_free - free a port TID
498  * @pd: the port
499  * @subport: the subport
500  * @ti: the TID info
501  *
502  * right now we are unlocking one page at a time, but since
503  * the intended use of this routine is for a single group of
504  * virtually contiguous pages, that should change to improve
505  * performance.  We check that the TID is in range for this port
506  * but otherwise don't check validity; if user has an error and
507  * frees the wrong tid, it's only their own data that can thereby
508  * be corrupted.  We do check that the TID was in use, for sanity
509  * We always use our idea of the saved address, not the address that
510  * they pass in to us.
511  */
512
513 static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
514                           const struct ipath_tid_info *ti)
515 {
516         int ret = 0;
517         u32 tid, porttid, cnt, limit, tidcnt;
518         struct ipath_devdata *dd = pd->port_dd;
519         u64 __iomem *tidbase;
520         unsigned long tidmap[8];
521
522         if (!dd->ipath_pageshadow) {
523                 ret = -ENOMEM;
524                 goto done;
525         }
526
527         if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
528                            sizeof tidmap)) {
529                 ret = -EFAULT;
530                 goto done;
531         }
532
533         porttid = pd->port_port * dd->ipath_rcvtidcnt;
534         if (!pd->port_subport_cnt)
535                 tidcnt = dd->ipath_rcvtidcnt;
536         else if (!subport) {
537                 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
538                          (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
539                 porttid += dd->ipath_rcvtidcnt - tidcnt;
540         } else {
541                 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
542                 porttid += tidcnt * (subport - 1);
543         }
544         tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
545                                    dd->ipath_rcvtidbase +
546                                    porttid * sizeof(*tidbase));
547
548         limit = sizeof(tidmap) * BITS_PER_BYTE;
549         if (limit > tidcnt)
550                 /* just in case size changes in future */
551                 limit = tidcnt;
552         tid = find_first_bit(tidmap, limit);
553         ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
554                    "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
555                    limit, tid, porttid);
556         for (cnt = 0; tid < limit; tid++) {
557                 /*
558                  * small optimization; if we detect a run of 3 or so without
559                  * any set, use find_first_bit again.  That's mainly to
560                  * accelerate the case where we wrapped, so we have some at
561                  * the beginning, and some at the end, and a big gap
562                  * in the middle.
563                  */
564                 if (!test_bit(tid, tidmap))
565                         continue;
566                 cnt++;
567                 if (dd->ipath_pageshadow[porttid + tid]) {
568                         struct page *p;
569                         p = dd->ipath_pageshadow[porttid + tid];
570                         dd->ipath_pageshadow[porttid + tid] = NULL;
571                         ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
572                                    pid_nr(pd->port_pid), tid);
573                         dd->ipath_f_put_tid(dd, &tidbase[tid],
574                                             RCVHQ_RCV_TYPE_EXPECTED,
575                                             dd->ipath_tidinvalid);
576                         pci_unmap_page(dd->pcidev,
577                                 dd->ipath_physshadow[porttid + tid],
578                                 PAGE_SIZE, PCI_DMA_FROMDEVICE);
579                         ipath_release_user_pages(&p, 1);
580                         ipath_stats.sps_pageunlocks++;
581                 } else
582                         ipath_dbg("Unused tid %u, ignoring\n", tid);
583         }
584         if (cnt != ti->tidcnt)
585                 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
586                           ti->tidcnt, cnt);
587 done:
588         if (ret)
589                 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
590                           ti->tidcnt, -ret);
591         return ret;
592 }
593
594 /**
595  * ipath_set_part_key - set a partition key
596  * @pd: the port
597  * @key: the key
598  *
599  * We can have up to 4 active at a time (other than the default, which is
600  * always allowed).  This is somewhat tricky, since multiple ports may set
601  * the same key, so we reference count them, and clean up at exit.  All 4
602  * partition keys are packed into a single infinipath register.  It's an
603  * error for a process to set the same pkey multiple times.  We provide no
604  * mechanism to de-allocate a pkey at this time, we may eventually need to
605  * do that.  I've used the atomic operations, and no locking, and only make
606  * a single pass through what's available.  This should be more than
607  * adequate for some time. I'll think about spinlocks or the like if and as
608  * it's necessary.
609  */
610 static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
611 {
612         struct ipath_devdata *dd = pd->port_dd;
613         int i, any = 0, pidx = -1;
614         u16 lkey = key & 0x7FFF;
615         int ret;
616
617         if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
618                 /* nothing to do; this key always valid */
619                 ret = 0;
620                 goto bail;
621         }
622
623         ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
624                    "%hx:%x %hx:%x %hx:%x %hx:%x\n",
625                    pd->port_port, key, dd->ipath_pkeys[0],
626                    atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
627                    atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
628                    atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
629                    atomic_read(&dd->ipath_pkeyrefs[3]));
630
631         if (!lkey) {
632                 ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
633                            pd->port_port);
634                 ret = -EINVAL;
635                 goto bail;
636         }
637
638         /*
639          * Set the full membership bit, because it has to be
640          * set in the register or the packet, and it seems
641          * cleaner to set in the register than to force all
642          * callers to set it. (see bug 4331)
643          */
644         key |= 0x8000;
645
646         for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
647                 if (!pd->port_pkeys[i] && pidx == -1)
648                         pidx = i;
649                 if (pd->port_pkeys[i] == key) {
650                         ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
651                                    "(%x) more than once\n",
652                                    pd->port_port, key);
653                         ret = -EEXIST;
654                         goto bail;
655                 }
656         }
657         if (pidx == -1) {
658                 ipath_dbg("All pkeys for port %u already in use, "
659                           "can't set %x\n", pd->port_port, key);
660                 ret = -EBUSY;
661                 goto bail;
662         }
663         for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
664                 if (!dd->ipath_pkeys[i]) {
665                         any++;
666                         continue;
667                 }
668                 if (dd->ipath_pkeys[i] == key) {
669                         atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
670
671                         if (atomic_inc_return(pkrefs) > 1) {
672                                 pd->port_pkeys[pidx] = key;
673                                 ipath_cdbg(VERBOSE, "p%u set key %x "
674                                            "matches #%d, count now %d\n",
675                                            pd->port_port, key, i,
676                                            atomic_read(pkrefs));
677                                 ret = 0;
678                                 goto bail;
679                         } else {
680                                 /*
681                                  * lost race, decrement count, catch below
682                                  */
683                                 atomic_dec(pkrefs);
684                                 ipath_cdbg(VERBOSE, "Lost race, count was "
685                                            "0, after dec, it's %d\n",
686                                            atomic_read(pkrefs));
687                                 any++;
688                         }
689                 }
690                 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
691                         /*
692                          * It makes no sense to have both the limited and
693                          * full membership PKEY set at the same time since
694                          * the unlimited one will disable the limited one.
695                          */
696                         ret = -EEXIST;
697                         goto bail;
698                 }
699         }
700         if (!any) {
701                 ipath_dbg("port %u, all pkeys already in use, "
702                           "can't set %x\n", pd->port_port, key);
703                 ret = -EBUSY;
704                 goto bail;
705         }
706         for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
707                 if (!dd->ipath_pkeys[i] &&
708                     atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
709                         u64 pkey;
710
711                         /* for ipathstats, etc. */
712                         ipath_stats.sps_pkeys[i] = lkey;
713                         pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
714                         pkey =
715                                 (u64) dd->ipath_pkeys[0] |
716                                 ((u64) dd->ipath_pkeys[1] << 16) |
717                                 ((u64) dd->ipath_pkeys[2] << 32) |
718                                 ((u64) dd->ipath_pkeys[3] << 48);
719                         ipath_cdbg(PROC, "p%u set key %x in #%d, "
720                                    "portidx %d, new pkey reg %llx\n",
721                                    pd->port_port, key, i, pidx,
722                                    (unsigned long long) pkey);
723                         ipath_write_kreg(
724                                 dd, dd->ipath_kregs->kr_partitionkey, pkey);
725
726                         ret = 0;
727                         goto bail;
728                 }
729         }
730         ipath_dbg("port %u, all pkeys already in use 2nd pass, "
731                   "can't set %x\n", pd->port_port, key);
732         ret = -EBUSY;
733
734 bail:
735         return ret;
736 }
737
738 /**
739  * ipath_manage_rcvq - manage a port's receive queue
740  * @pd: the port
741  * @subport: the subport
742  * @start_stop: action to carry out
743  *
744  * start_stop == 0 disables receive on the port, for use in queue
745  * overflow conditions.  start_stop==1 re-enables, to be used to
746  * re-init the software copy of the head register
747  */
748 static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
749                              int start_stop)
750 {
751         struct ipath_devdata *dd = pd->port_dd;
752
753         ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
754                    start_stop ? "en" : "dis", dd->ipath_unit,
755                    pd->port_port, subport);
756         if (subport)
757                 goto bail;
758         /* atomically clear receive enable port. */
759         if (start_stop) {
760                 /*
761                  * On enable, force in-memory copy of the tail register to
762                  * 0, so that protocol code doesn't have to worry about
763                  * whether or not the chip has yet updated the in-memory
764                  * copy or not on return from the system call. The chip
765                  * always resets it's tail register back to 0 on a
766                  * transition from disabled to enabled.  This could cause a
767                  * problem if software was broken, and did the enable w/o
768                  * the disable, but eventually the in-memory copy will be
769                  * updated and correct itself, even in the face of software
770                  * bugs.
771                  */
772                 if (pd->port_rcvhdrtail_kvaddr)
773                         ipath_clear_rcvhdrtail(pd);
774                 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
775                         &dd->ipath_rcvctrl);
776         } else
777                 clear_bit(dd->ipath_r_portenable_shift + pd->port_port,
778                           &dd->ipath_rcvctrl);
779         ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
780                          dd->ipath_rcvctrl);
781         /* now be sure chip saw it before we return */
782         ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
783         if (start_stop) {
784                 /*
785                  * And try to be sure that tail reg update has happened too.
786                  * This should in theory interlock with the RXE changes to
787                  * the tail register.  Don't assign it to the tail register
788                  * in memory copy, since we could overwrite an update by the
789                  * chip if we did.
790                  */
791                 ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
792         }
793         /* always; new head should be equal to new tail; see above */
794 bail:
795         return 0;
796 }
797
798 static void ipath_clean_part_key(struct ipath_portdata *pd,
799                                  struct ipath_devdata *dd)
800 {
801         int i, j, pchanged = 0;
802         u64 oldpkey;
803
804         /* for debugging only */
805         oldpkey = (u64) dd->ipath_pkeys[0] |
806                 ((u64) dd->ipath_pkeys[1] << 16) |
807                 ((u64) dd->ipath_pkeys[2] << 32) |
808                 ((u64) dd->ipath_pkeys[3] << 48);
809
810         for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
811                 if (!pd->port_pkeys[i])
812                         continue;
813                 ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
814                            pd->port_pkeys[i]);
815                 for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
816                         /* check for match independent of the global bit */
817                         if ((dd->ipath_pkeys[j] & 0x7fff) !=
818                             (pd->port_pkeys[i] & 0x7fff))
819                                 continue;
820                         if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
821                                 ipath_cdbg(VERBOSE, "p%u clear key "
822                                            "%x matches #%d\n",
823                                            pd->port_port,
824                                            pd->port_pkeys[i], j);
825                                 ipath_stats.sps_pkeys[j] =
826                                         dd->ipath_pkeys[j] = 0;
827                                 pchanged++;
828                         } else {
829                                 ipath_cdbg(VERBOSE, "p%u key %x matches #%d, "
830                                            "but ref still %d\n", pd->port_port,
831                                            pd->port_pkeys[i], j,
832                                            atomic_read(&dd->ipath_pkeyrefs[j]));
833                                 break;
834                         }
835                 }
836                 pd->port_pkeys[i] = 0;
837         }
838         if (pchanged) {
839                 u64 pkey = (u64) dd->ipath_pkeys[0] |
840                         ((u64) dd->ipath_pkeys[1] << 16) |
841                         ((u64) dd->ipath_pkeys[2] << 32) |
842                         ((u64) dd->ipath_pkeys[3] << 48);
843                 ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
844                            "new pkey reg %llx\n", pd->port_port,
845                            (unsigned long long) oldpkey,
846                            (unsigned long long) pkey);
847                 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
848                                  pkey);
849         }
850 }
851
852 /*
853  * Initialize the port data with the receive buffer sizes
854  * so this can be done while the master port is locked.
855  * Otherwise, there is a race with a slave opening the port
856  * and seeing these fields uninitialized.
857  */
858 static void init_user_egr_sizes(struct ipath_portdata *pd)
859 {
860         struct ipath_devdata *dd = pd->port_dd;
861         unsigned egrperchunk, egrcnt, size;
862
863         /*
864          * to avoid wasting a lot of memory, we allocate 32KB chunks of
865          * physically contiguous memory, advance through it until used up
866          * and then allocate more.  Of course, we need memory to store those
867          * extra pointers, now.  Started out with 256KB, but under heavy
868          * memory pressure (creating large files and then copying them over
869          * NFS while doing lots of MPI jobs), we hit some allocation
870          * failures, even though we can sleep...  (2.6.10) Still get
871          * failures at 64K.  32K is the lowest we can go without wasting
872          * additional memory.
873          */
874         size = 0x8000;
875         egrperchunk = size / dd->ipath_rcvegrbufsize;
876         egrcnt = dd->ipath_rcvegrcnt;
877         pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
878         pd->port_rcvegrbufs_perchunk = egrperchunk;
879         pd->port_rcvegrbuf_size = size;
880 }
881
882 /**
883  * ipath_create_user_egr - allocate eager TID buffers
884  * @pd: the port to allocate TID buffers for
885  *
886  * This routine is now quite different for user and kernel, because
887  * the kernel uses skb's, for the accelerated network performance
888  * This is the user port version
889  *
890  * Allocate the eager TID buffers and program them into infinipath
891  * They are no longer completely contiguous, we do multiple allocation
892  * calls.
893  */
894 static int ipath_create_user_egr(struct ipath_portdata *pd)
895 {
896         struct ipath_devdata *dd = pd->port_dd;
897         unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
898         size_t size;
899         int ret;
900         gfp_t gfp_flags;
901
902         /*
903          * GFP_USER, but without GFP_FS, so buffer cache can be
904          * coalesced (we hope); otherwise, even at order 4,
905          * heavy filesystem activity makes these fail, and we can
906          * use compound pages.
907          */
908         gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
909
910         egrcnt = dd->ipath_rcvegrcnt;
911         /* TID number offset for this port */
912         egroff = (pd->port_port - 1) * egrcnt + dd->ipath_p0_rcvegrcnt;
913         egrsize = dd->ipath_rcvegrbufsize;
914         ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
915                    "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
916
917         chunk = pd->port_rcvegrbuf_chunks;
918         egrperchunk = pd->port_rcvegrbufs_perchunk;
919         size = pd->port_rcvegrbuf_size;
920         pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
921                                      GFP_KERNEL);
922         if (!pd->port_rcvegrbuf) {
923                 ret = -ENOMEM;
924                 goto bail;
925         }
926         pd->port_rcvegrbuf_phys =
927                 kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
928                         GFP_KERNEL);
929         if (!pd->port_rcvegrbuf_phys) {
930                 ret = -ENOMEM;
931                 goto bail_rcvegrbuf;
932         }
933         for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
934
935                 pd->port_rcvegrbuf[e] = dma_alloc_coherent(
936                         &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
937                         gfp_flags);
938
939                 if (!pd->port_rcvegrbuf[e]) {
940                         ret = -ENOMEM;
941                         goto bail_rcvegrbuf_phys;
942                 }
943         }
944
945         pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
946
947         for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
948                 dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
949                 unsigned i;
950
951                 for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
952                         dd->ipath_f_put_tid(dd, e + egroff +
953                                             (u64 __iomem *)
954                                             ((char __iomem *)
955                                              dd->ipath_kregbase +
956                                              dd->ipath_rcvegrbase),
957                                             RCVHQ_RCV_TYPE_EAGER, pa);
958                         pa += egrsize;
959                 }
960                 cond_resched(); /* don't hog the cpu */
961         }
962
963         ret = 0;
964         goto bail;
965
966 bail_rcvegrbuf_phys:
967         for (e = 0; e < pd->port_rcvegrbuf_chunks &&
968                 pd->port_rcvegrbuf[e]; e++) {
969                 dma_free_coherent(&dd->pcidev->dev, size,
970                                   pd->port_rcvegrbuf[e],
971                                   pd->port_rcvegrbuf_phys[e]);
972
973         }
974         kfree(pd->port_rcvegrbuf_phys);
975         pd->port_rcvegrbuf_phys = NULL;
976 bail_rcvegrbuf:
977         kfree(pd->port_rcvegrbuf);
978         pd->port_rcvegrbuf = NULL;
979 bail:
980         return ret;
981 }
982
983
984 /* common code for the mappings on dma_alloc_coherent mem */
985 static int ipath_mmap_mem(struct vm_area_struct *vma,
986         struct ipath_portdata *pd, unsigned len, int write_ok,
987         void *kvaddr, char *what)
988 {
989         struct ipath_devdata *dd = pd->port_dd;
990         unsigned long pfn;
991         int ret;
992
993         if ((vma->vm_end - vma->vm_start) > len) {
994                 dev_info(&dd->pcidev->dev,
995                          "FAIL on %s: len %lx > %x\n", what,
996                          vma->vm_end - vma->vm_start, len);
997                 ret = -EFAULT;
998                 goto bail;
999         }
1000
1001         if (!write_ok) {
1002                 if (vma->vm_flags & VM_WRITE) {
1003                         dev_info(&dd->pcidev->dev,
1004                                  "%s must be mapped readonly\n", what);
1005                         ret = -EPERM;
1006                         goto bail;
1007                 }
1008
1009                 /* don't allow them to later change with mprotect */
1010                 vma->vm_flags &= ~VM_MAYWRITE;
1011         }
1012
1013         pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
1014         ret = remap_pfn_range(vma, vma->vm_start, pfn,
1015                               len, vma->vm_page_prot);
1016         if (ret)
1017                 dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
1018                          "bytes r%c failed: %d\n", what, pd->port_port,
1019                          pfn, len, write_ok?'w':'o', ret);
1020         else
1021                 ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
1022                            "r%c\n", what, pd->port_port, pfn, len,
1023                            write_ok?'w':'o');
1024 bail:
1025         return ret;
1026 }
1027
1028 static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
1029                      u64 ureg)
1030 {
1031         unsigned long phys;
1032         int ret;
1033
1034         /*
1035          * This is real hardware, so use io_remap.  This is the mechanism
1036          * for the user process to update the head registers for their port
1037          * in the chip.
1038          */
1039         if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
1040                 dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
1041                          "%lx > PAGE\n", vma->vm_end - vma->vm_start);
1042                 ret = -EFAULT;
1043         } else {
1044                 phys = dd->ipath_physaddr + ureg;
1045                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1046
1047                 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1048                 ret = io_remap_pfn_range(vma, vma->vm_start,
1049                                          phys >> PAGE_SHIFT,
1050                                          vma->vm_end - vma->vm_start,
1051                                          vma->vm_page_prot);
1052         }
1053         return ret;
1054 }
1055
1056 static int mmap_piobufs(struct vm_area_struct *vma,
1057                         struct ipath_devdata *dd,
1058                         struct ipath_portdata *pd,
1059                         unsigned piobufs, unsigned piocnt)
1060 {
1061         unsigned long phys;
1062         int ret;
1063
1064         /*
1065          * When we map the PIO buffers in the chip, we want to map them as
1066          * writeonly, no read possible.   This prevents access to previous
1067          * process data, and catches users who might try to read the i/o
1068          * space due to a bug.
1069          */
1070         if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
1071                 dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
1072                          "reqlen %lx > PAGE\n",
1073                          vma->vm_end - vma->vm_start);
1074                 ret = -EINVAL;
1075                 goto bail;
1076         }
1077
1078         phys = dd->ipath_physaddr + piobufs;
1079
1080 #if defined(__powerpc__)
1081         /* There isn't a generic way to specify writethrough mappings */
1082         pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1083         pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
1084         pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
1085 #endif
1086
1087         /*
1088          * don't allow them to later change to readable with mprotect (for when
1089          * not initially mapped readable, as is normally the case)
1090          */
1091         vma->vm_flags &= ~VM_MAYREAD;
1092         vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1093
1094         ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
1095                                  vma->vm_end - vma->vm_start,
1096                                  vma->vm_page_prot);
1097 bail:
1098         return ret;
1099 }
1100
1101 static int mmap_rcvegrbufs(struct vm_area_struct *vma,
1102                            struct ipath_portdata *pd)
1103 {
1104         struct ipath_devdata *dd = pd->port_dd;
1105         unsigned long start, size;
1106         size_t total_size, i;
1107         unsigned long pfn;
1108         int ret;
1109
1110         size = pd->port_rcvegrbuf_size;
1111         total_size = pd->port_rcvegrbuf_chunks * size;
1112         if ((vma->vm_end - vma->vm_start) > total_size) {
1113                 dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
1114                          "reqlen %lx > actual %lx\n",
1115                          vma->vm_end - vma->vm_start,
1116                          (unsigned long) total_size);
1117                 ret = -EINVAL;
1118                 goto bail;
1119         }
1120
1121         if (vma->vm_flags & VM_WRITE) {
1122                 dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
1123                          "writable (flags=%lx)\n", vma->vm_flags);
1124                 ret = -EPERM;
1125                 goto bail;
1126         }
1127         /* don't allow them to later change to writeable with mprotect */
1128         vma->vm_flags &= ~VM_MAYWRITE;
1129
1130         start = vma->vm_start;
1131
1132         for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
1133                 pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
1134                 ret = remap_pfn_range(vma, start, pfn, size,
1135                                       vma->vm_page_prot);
1136                 if (ret < 0)
1137                         goto bail;
1138         }
1139         ret = 0;
1140
1141 bail:
1142         return ret;
1143 }
1144
1145 /*
1146  * ipath_file_vma_fault - handle a VMA page fault.
1147  */
1148 static int ipath_file_vma_fault(struct vm_area_struct *vma,
1149                                         struct vm_fault *vmf)
1150 {
1151         struct page *page;
1152
1153         page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
1154         if (!page)
1155                 return VM_FAULT_SIGBUS;
1156         get_page(page);
1157         vmf->page = page;
1158
1159         return 0;
1160 }
1161
1162 static const struct vm_operations_struct ipath_file_vm_ops = {
1163         .fault = ipath_file_vma_fault,
1164 };
1165
1166 static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
1167                        struct ipath_portdata *pd, unsigned subport)
1168 {
1169         unsigned long len;
1170         struct ipath_devdata *dd;
1171         void *addr;
1172         size_t size;
1173         int ret = 0;
1174
1175         /* If the port is not shared, all addresses should be physical */
1176         if (!pd->port_subport_cnt)
1177                 goto bail;
1178
1179         dd = pd->port_dd;
1180         size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
1181
1182         /*
1183          * Each process has all the subport uregbase, rcvhdrq, and
1184          * rcvegrbufs mmapped - as an array for all the processes,
1185          * and also separately for this process.
1186          */
1187         if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
1188                 addr = pd->subport_uregbase;
1189                 size = PAGE_SIZE * pd->port_subport_cnt;
1190         } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
1191                 addr = pd->subport_rcvhdr_base;
1192                 size = pd->port_rcvhdrq_size * pd->port_subport_cnt;
1193         } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
1194                 addr = pd->subport_rcvegrbuf;
1195                 size *= pd->port_subport_cnt;
1196         } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
1197                                         PAGE_SIZE * subport)) {
1198                 addr = pd->subport_uregbase + PAGE_SIZE * subport;
1199                 size = PAGE_SIZE;
1200         } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
1201                                 pd->port_rcvhdrq_size * subport)) {
1202                 addr = pd->subport_rcvhdr_base +
1203                         pd->port_rcvhdrq_size * subport;
1204                 size = pd->port_rcvhdrq_size;
1205         } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
1206                                size * subport)) {
1207                 addr = pd->subport_rcvegrbuf + size * subport;
1208                 /* rcvegrbufs are read-only on the slave */
1209                 if (vma->vm_flags & VM_WRITE) {
1210                         dev_info(&dd->pcidev->dev,
1211                                  "Can't map eager buffers as "
1212                                  "writable (flags=%lx)\n", vma->vm_flags);
1213                         ret = -EPERM;
1214                         goto bail;
1215                 }
1216                 /*
1217                  * Don't allow permission to later change to writeable
1218                  * with mprotect.
1219                  */
1220                 vma->vm_flags &= ~VM_MAYWRITE;
1221         } else {
1222                 goto bail;
1223         }
1224         len = vma->vm_end - vma->vm_start;
1225         if (len > size) {
1226                 ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
1227                 ret = -EINVAL;
1228                 goto bail;
1229         }
1230
1231         vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
1232         vma->vm_ops = &ipath_file_vm_ops;
1233         vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1234         ret = 1;
1235
1236 bail:
1237         return ret;
1238 }
1239
1240 /**
1241  * ipath_mmap - mmap various structures into user space
1242  * @fp: the file pointer
1243  * @vma: the VM area
1244  *
1245  * We use this to have a shared buffer between the kernel and the user code
1246  * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1247  * buffers in the chip.  We have the open and close entries so we can bump
1248  * the ref count and keep the driver from being unloaded while still mapped.
1249  */
1250 static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
1251 {
1252         struct ipath_portdata *pd;
1253         struct ipath_devdata *dd;
1254         u64 pgaddr, ureg;
1255         unsigned piobufs, piocnt;
1256         int ret;
1257
1258         pd = port_fp(fp);
1259         if (!pd) {
1260                 ret = -EINVAL;
1261                 goto bail;
1262         }
1263         dd = pd->port_dd;
1264
1265         /*
1266          * This is the ipath_do_user_init() code, mapping the shared buffers
1267          * into the user process. The address referred to by vm_pgoff is the
1268          * file offset passed via mmap().  For shared ports, this is the
1269          * kernel vmalloc() address of the pages to share with the master.
1270          * For non-shared or master ports, this is a physical address.
1271          * We only do one mmap for each space mapped.
1272          */
1273         pgaddr = vma->vm_pgoff << PAGE_SHIFT;
1274
1275         /*
1276          * Check for 0 in case one of the allocations failed, but user
1277          * called mmap anyway.
1278          */
1279         if (!pgaddr)  {
1280                 ret = -EINVAL;
1281                 goto bail;
1282         }
1283
1284         ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
1285                    (unsigned long long) pgaddr, vma->vm_start,
1286                    vma->vm_end - vma->vm_start, dd->ipath_unit,
1287                    pd->port_port, subport_fp(fp));
1288
1289         /*
1290          * Physical addresses must fit in 40 bits for our hardware.
1291          * Check for kernel virtual addresses first, anything else must
1292          * match a HW or memory address.
1293          */
1294         ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
1295         if (ret) {
1296                 if (ret > 0)
1297                         ret = 0;
1298                 goto bail;
1299         }
1300
1301         ureg = dd->ipath_uregbase + dd->ipath_ureg_align * pd->port_port;
1302         if (!pd->port_subport_cnt) {
1303                 /* port is not shared */
1304                 piocnt = pd->port_piocnt;
1305                 piobufs = pd->port_piobufs;
1306         } else if (!subport_fp(fp)) {
1307                 /* caller is the master */
1308                 piocnt = (pd->port_piocnt / pd->port_subport_cnt) +
1309                          (pd->port_piocnt % pd->port_subport_cnt);
1310                 piobufs = pd->port_piobufs +
1311                         dd->ipath_palign * (pd->port_piocnt - piocnt);
1312         } else {
1313                 unsigned slave = subport_fp(fp) - 1;
1314
1315                 /* caller is a slave */
1316                 piocnt = pd->port_piocnt / pd->port_subport_cnt;
1317                 piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
1318         }
1319
1320         if (pgaddr == ureg)
1321                 ret = mmap_ureg(vma, dd, ureg);
1322         else if (pgaddr == piobufs)
1323                 ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
1324         else if (pgaddr == dd->ipath_pioavailregs_phys)
1325                 /* in-memory copy of pioavail registers */
1326                 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1327                                      (void *) dd->ipath_pioavailregs_dma,
1328                                      "pioavail registers");
1329         else if (pgaddr == pd->port_rcvegr_phys)
1330                 ret = mmap_rcvegrbufs(vma, pd);
1331         else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
1332                 /*
1333                  * The rcvhdrq itself; readonly except on HT (so have
1334                  * to allow writable mapping), multiple pages, contiguous
1335                  * from an i/o perspective.
1336                  */
1337                 ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
1338                                      pd->port_rcvhdrq,
1339                                      "rcvhdrq");
1340         else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
1341                 /* in-memory copy of rcvhdrq tail register */
1342                 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1343                                      pd->port_rcvhdrtail_kvaddr,
1344                                      "rcvhdrq tail");
1345         else
1346                 ret = -EINVAL;
1347
1348         vma->vm_private_data = NULL;
1349
1350         if (ret < 0)
1351                 dev_info(&dd->pcidev->dev,
1352                          "Failure %d on off %llx len %lx\n",
1353                          -ret, (unsigned long long)pgaddr,
1354                          vma->vm_end - vma->vm_start);
1355 bail:
1356         return ret;
1357 }
1358
1359 static unsigned ipath_poll_hdrqfull(struct ipath_portdata *pd)
1360 {
1361         unsigned pollflag = 0;
1362
1363         if ((pd->poll_type & IPATH_POLL_TYPE_OVERFLOW) &&
1364             pd->port_hdrqfull != pd->port_hdrqfull_poll) {
1365                 pollflag |= POLLIN | POLLRDNORM;
1366                 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1367         }
1368
1369         return pollflag;
1370 }
1371
1372 static unsigned int ipath_poll_urgent(struct ipath_portdata *pd,
1373                                       struct file *fp,
1374                                       struct poll_table_struct *pt)
1375 {
1376         unsigned pollflag = 0;
1377         struct ipath_devdata *dd;
1378
1379         dd = pd->port_dd;
1380
1381         /* variable access in ipath_poll_hdrqfull() needs this */
1382         rmb();
1383         pollflag = ipath_poll_hdrqfull(pd);
1384
1385         if (pd->port_urgent != pd->port_urgent_poll) {
1386                 pollflag |= POLLIN | POLLRDNORM;
1387                 pd->port_urgent_poll = pd->port_urgent;
1388         }
1389
1390         if (!pollflag) {
1391                 /* this saves a spin_lock/unlock in interrupt handler... */
1392                 set_bit(IPATH_PORT_WAITING_URG, &pd->port_flag);
1393                 /* flush waiting flag so don't miss an event... */
1394                 wmb();
1395                 poll_wait(fp, &pd->port_wait, pt);
1396         }
1397
1398         return pollflag;
1399 }
1400
1401 static unsigned int ipath_poll_next(struct ipath_portdata *pd,
1402                                     struct file *fp,
1403                                     struct poll_table_struct *pt)
1404 {
1405         u32 head;
1406         u32 tail;
1407         unsigned pollflag = 0;
1408         struct ipath_devdata *dd;
1409
1410         dd = pd->port_dd;
1411
1412         /* variable access in ipath_poll_hdrqfull() needs this */
1413         rmb();
1414         pollflag = ipath_poll_hdrqfull(pd);
1415
1416         head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
1417         if (pd->port_rcvhdrtail_kvaddr)
1418                 tail = ipath_get_rcvhdrtail(pd);
1419         else
1420                 tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
1421
1422         if (head != tail)
1423                 pollflag |= POLLIN | POLLRDNORM;
1424         else {
1425                 /* this saves a spin_lock/unlock in interrupt handler */
1426                 set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
1427                 /* flush waiting flag so we don't miss an event */
1428                 wmb();
1429
1430                 set_bit(pd->port_port + dd->ipath_r_intravail_shift,
1431                         &dd->ipath_rcvctrl);
1432
1433                 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1434                                  dd->ipath_rcvctrl);
1435
1436                 if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
1437                         ipath_write_ureg(dd, ur_rcvhdrhead,
1438                                          dd->ipath_rhdrhead_intr_off | head,
1439                                          pd->port_port);
1440
1441                 poll_wait(fp, &pd->port_wait, pt);
1442         }
1443
1444         return pollflag;
1445 }
1446
1447 static unsigned int ipath_poll(struct file *fp,
1448                                struct poll_table_struct *pt)
1449 {
1450         struct ipath_portdata *pd;
1451         unsigned pollflag;
1452
1453         pd = port_fp(fp);
1454         if (!pd)
1455                 pollflag = 0;
1456         else if (pd->poll_type & IPATH_POLL_TYPE_URGENT)
1457                 pollflag = ipath_poll_urgent(pd, fp, pt);
1458         else
1459                 pollflag = ipath_poll_next(pd, fp, pt);
1460
1461         return pollflag;
1462 }
1463
1464 static int ipath_supports_subports(int user_swmajor, int user_swminor)
1465 {
1466         /* no subport implementation prior to software version 1.3 */
1467         return (user_swmajor > 1) || (user_swminor >= 3);
1468 }
1469
1470 static int ipath_compatible_subports(int user_swmajor, int user_swminor)
1471 {
1472         /* this code is written long-hand for clarity */
1473         if (IPATH_USER_SWMAJOR != user_swmajor) {
1474                 /* no promise of compatibility if major mismatch */
1475                 return 0;
1476         }
1477         if (IPATH_USER_SWMAJOR == 1) {
1478                 switch (IPATH_USER_SWMINOR) {
1479                 case 0:
1480                 case 1:
1481                 case 2:
1482                         /* no subport implementation so cannot be compatible */
1483                         return 0;
1484                 case 3:
1485                         /* 3 is only compatible with itself */
1486                         return user_swminor == 3;
1487                 default:
1488                         /* >= 4 are compatible (or are expected to be) */
1489                         return user_swminor >= 4;
1490                 }
1491         }
1492         /* make no promises yet for future major versions */
1493         return 0;
1494 }
1495
1496 static int init_subports(struct ipath_devdata *dd,
1497                          struct ipath_portdata *pd,
1498                          const struct ipath_user_info *uinfo)
1499 {
1500         int ret = 0;
1501         unsigned num_subports;
1502         size_t size;
1503
1504         /*
1505          * If the user is requesting zero subports,
1506          * skip the subport allocation.
1507          */
1508         if (uinfo->spu_subport_cnt <= 0)
1509                 goto bail;
1510
1511         /* Self-consistency check for ipath_compatible_subports() */
1512         if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&
1513             !ipath_compatible_subports(IPATH_USER_SWMAJOR,
1514                                        IPATH_USER_SWMINOR)) {
1515                 dev_info(&dd->pcidev->dev,
1516                          "Inconsistent ipath_compatible_subports()\n");
1517                 goto bail;
1518         }
1519
1520         /* Check for subport compatibility */
1521         if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,
1522                                        uinfo->spu_userversion & 0xffff)) {
1523                 dev_info(&dd->pcidev->dev,
1524                          "Mismatched user version (%d.%d) and driver "
1525                          "version (%d.%d) while port sharing. Ensure "
1526                          "that driver and library are from the same "
1527                          "release.\n",
1528                          (int) (uinfo->spu_userversion >> 16),
1529                          (int) (uinfo->spu_userversion & 0xffff),
1530                          IPATH_USER_SWMAJOR,
1531                          IPATH_USER_SWMINOR);
1532                 goto bail;
1533         }
1534         if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
1535                 ret = -EINVAL;
1536                 goto bail;
1537         }
1538
1539         num_subports = uinfo->spu_subport_cnt;
1540         pd->subport_uregbase = vzalloc(PAGE_SIZE * num_subports);
1541         if (!pd->subport_uregbase) {
1542                 ret = -ENOMEM;
1543                 goto bail;
1544         }
1545         /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
1546         size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
1547                      sizeof(u32), PAGE_SIZE) * num_subports;
1548         pd->subport_rcvhdr_base = vzalloc(size);
1549         if (!pd->subport_rcvhdr_base) {
1550                 ret = -ENOMEM;
1551                 goto bail_ureg;
1552         }
1553
1554         pd->subport_rcvegrbuf = vzalloc(pd->port_rcvegrbuf_chunks *
1555                                         pd->port_rcvegrbuf_size *
1556                                         num_subports);
1557         if (!pd->subport_rcvegrbuf) {
1558                 ret = -ENOMEM;
1559                 goto bail_rhdr;
1560         }
1561
1562         pd->port_subport_cnt = uinfo->spu_subport_cnt;
1563         pd->port_subport_id = uinfo->spu_subport_id;
1564         pd->active_slaves = 1;
1565         set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1566         goto bail;
1567
1568 bail_rhdr:
1569         vfree(pd->subport_rcvhdr_base);
1570 bail_ureg:
1571         vfree(pd->subport_uregbase);
1572         pd->subport_uregbase = NULL;
1573 bail:
1574         return ret;
1575 }
1576
1577 static int try_alloc_port(struct ipath_devdata *dd, int port,
1578                           struct file *fp,
1579                           const struct ipath_user_info *uinfo)
1580 {
1581         struct ipath_portdata *pd;
1582         int ret;
1583
1584         if (!(pd = dd->ipath_pd[port])) {
1585                 void *ptmp;
1586
1587                 pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
1588
1589                 /*
1590                  * Allocate memory for use in ipath_tid_update() just once
1591                  * at open, not per call.  Reduces cost of expected send
1592                  * setup.
1593                  */
1594                 ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
1595                                dd->ipath_rcvtidcnt * sizeof(struct page **),
1596                                GFP_KERNEL);
1597                 if (!pd || !ptmp) {
1598                         ipath_dev_err(dd, "Unable to allocate portdata "
1599                                       "memory, failing open\n");
1600                         ret = -ENOMEM;
1601                         kfree(pd);
1602                         kfree(ptmp);
1603                         goto bail;
1604                 }
1605                 dd->ipath_pd[port] = pd;
1606                 dd->ipath_pd[port]->port_port = port;
1607                 dd->ipath_pd[port]->port_dd = dd;
1608                 dd->ipath_pd[port]->port_tid_pg_list = ptmp;
1609                 init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
1610         }
1611         if (!pd->port_cnt) {
1612                 pd->userversion = uinfo->spu_userversion;
1613                 init_user_egr_sizes(pd);
1614                 if ((ret = init_subports(dd, pd, uinfo)) != 0)
1615                         goto bail;
1616                 ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
1617                            current->comm, current->pid, dd->ipath_unit,
1618                            port);
1619                 pd->port_cnt = 1;
1620                 port_fp(fp) = pd;
1621                 pd->port_pid = get_pid(task_pid(current));
1622                 strlcpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
1623                 ipath_stats.sps_ports++;
1624                 ret = 0;
1625         } else
1626                 ret = -EBUSY;
1627
1628 bail:
1629         return ret;
1630 }
1631
1632 static inline int usable(struct ipath_devdata *dd)
1633 {
1634         return dd &&
1635                 (dd->ipath_flags & IPATH_PRESENT) &&
1636                 dd->ipath_kregbase &&
1637                 dd->ipath_lid &&
1638                 !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
1639                                      | IPATH_LINKUNK));
1640 }
1641
1642 static int find_free_port(int unit, struct file *fp,
1643                           const struct ipath_user_info *uinfo)
1644 {
1645         struct ipath_devdata *dd = ipath_lookup(unit);
1646         int ret, i;
1647
1648         if (!dd) {
1649                 ret = -ENODEV;
1650                 goto bail;
1651         }
1652
1653         if (!usable(dd)) {
1654                 ret = -ENETDOWN;
1655                 goto bail;
1656         }
1657
1658         for (i = 1; i < dd->ipath_cfgports; i++) {
1659                 ret = try_alloc_port(dd, i, fp, uinfo);
1660                 if (ret != -EBUSY)
1661                         goto bail;
1662         }
1663         ret = -EBUSY;
1664
1665 bail:
1666         return ret;
1667 }
1668
1669 static int find_best_unit(struct file *fp,
1670                           const struct ipath_user_info *uinfo)
1671 {
1672         int ret = 0, i, prefunit = -1, devmax;
1673         int maxofallports, npresent, nup;
1674         int ndev;
1675
1676         devmax = ipath_count_units(&npresent, &nup, &maxofallports);
1677
1678         /*
1679          * This code is present to allow a knowledgeable person to
1680          * specify the layout of processes to processors before opening
1681          * this driver, and then we'll assign the process to the "closest"
1682          * InfiniPath chip to that processor (we assume reasonable connectivity,
1683          * for now).  This code assumes that if affinity has been set
1684          * before this point, that at most one cpu is set; for now this
1685          * is reasonable.  I check for both cpumask_empty() and cpumask_full(),
1686          * in case some kernel variant sets none of the bits when no
1687          * affinity is set.  2.6.11 and 12 kernels have all present
1688          * cpus set.  Some day we'll have to fix it up further to handle
1689          * a cpu subset.  This algorithm fails for two HT chips connected
1690          * in tunnel fashion.  Eventually this needs real topology
1691          * information.  There may be some issues with dual core numbering
1692          * as well.  This needs more work prior to release.
1693          */
1694         if (!cpumask_empty(tsk_cpus_allowed(current)) &&
1695             !cpumask_full(tsk_cpus_allowed(current))) {
1696                 int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
1697                 get_online_cpus();
1698                 for_each_online_cpu(i)
1699                         if (cpumask_test_cpu(i, tsk_cpus_allowed(current))) {
1700                                 ipath_cdbg(PROC, "%s[%u] affinity set for "
1701                                            "cpu %d/%d\n", current->comm,
1702                                            current->pid, i, ncpus);
1703                                 curcpu = i;
1704                                 nset++;
1705                         }
1706                 put_online_cpus();
1707                 if (curcpu != -1 && nset != ncpus) {
1708                         if (npresent) {
1709                                 prefunit = curcpu / (ncpus / npresent);
1710                                 ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
1711                                           "%d cpus/chip, select unit %d\n",
1712                                           current->comm, current->pid,
1713                                           npresent, ncpus, ncpus / npresent,
1714                                           prefunit);
1715                         }
1716                 }
1717         }
1718
1719         /*
1720          * user ports start at 1, kernel port is 0
1721          * For now, we do round-robin access across all chips
1722          */
1723
1724         if (prefunit != -1)
1725                 devmax = prefunit + 1;
1726 recheck:
1727         for (i = 1; i < maxofallports; i++) {
1728                 for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
1729                      ndev++) {
1730                         struct ipath_devdata *dd = ipath_lookup(ndev);
1731
1732                         if (!usable(dd))
1733                                 continue; /* can't use this unit */
1734                         if (i >= dd->ipath_cfgports)
1735                                 /*
1736                                  * Maxed out on users of this unit. Try
1737                                  * next.
1738                                  */
1739                                 continue;
1740                         ret = try_alloc_port(dd, i, fp, uinfo);
1741                         if (!ret)
1742                                 goto done;
1743                 }
1744         }
1745
1746         if (npresent) {
1747                 if (nup == 0) {
1748                         ret = -ENETDOWN;
1749                         ipath_dbg("No ports available (none initialized "
1750                                   "and ready)\n");
1751                 } else {
1752                         if (prefunit > 0) {
1753                                 /* if started above 0, retry from 0 */
1754                                 ipath_cdbg(PROC,
1755                                            "%s[%u] no ports on prefunit "
1756                                            "%d, clear and re-check\n",
1757                                            current->comm, current->pid,
1758                                            prefunit);
1759                                 devmax = ipath_count_units(NULL, NULL,
1760                                                            NULL);
1761                                 prefunit = -1;
1762                                 goto recheck;
1763                         }
1764                         ret = -EBUSY;
1765                         ipath_dbg("No ports available\n");
1766                 }
1767         } else {
1768                 ret = -ENXIO;
1769                 ipath_dbg("No boards found\n");
1770         }
1771
1772 done:
1773         return ret;
1774 }
1775
1776 static int find_shared_port(struct file *fp,
1777                             const struct ipath_user_info *uinfo)
1778 {
1779         int devmax, ndev, i;
1780         int ret = 0;
1781
1782         devmax = ipath_count_units(NULL, NULL, NULL);
1783
1784         for (ndev = 0; ndev < devmax; ndev++) {
1785                 struct ipath_devdata *dd = ipath_lookup(ndev);
1786
1787                 if (!usable(dd))
1788                         continue;
1789                 for (i = 1; i < dd->ipath_cfgports; i++) {
1790                         struct ipath_portdata *pd = dd->ipath_pd[i];
1791
1792                         /* Skip ports which are not yet open */
1793                         if (!pd || !pd->port_cnt)
1794                                 continue;
1795                         /* Skip port if it doesn't match the requested one */
1796                         if (pd->port_subport_id != uinfo->spu_subport_id)
1797                                 continue;
1798                         /* Verify the sharing process matches the master */
1799                         if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
1800                             pd->userversion != uinfo->spu_userversion ||
1801                             pd->port_cnt >= pd->port_subport_cnt) {
1802                                 ret = -EINVAL;
1803                                 goto done;
1804                         }
1805                         port_fp(fp) = pd;
1806                         subport_fp(fp) = pd->port_cnt++;
1807                         pd->port_subpid[subport_fp(fp)] =
1808                                 get_pid(task_pid(current));
1809                         tidcursor_fp(fp) = 0;
1810                         pd->active_slaves |= 1 << subport_fp(fp);
1811                         ipath_cdbg(PROC,
1812                                    "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
1813                                    current->comm, current->pid,
1814                                    subport_fp(fp),
1815                                    pd->port_comm, pid_nr(pd->port_pid),
1816                                    dd->ipath_unit, pd->port_port);
1817                         ret = 1;
1818                         goto done;
1819                 }
1820         }
1821
1822 done:
1823         return ret;
1824 }
1825
1826 static int ipath_open(struct inode *in, struct file *fp)
1827 {
1828         /* The real work is performed later in ipath_assign_port() */
1829         fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
1830         return fp->private_data ? 0 : -ENOMEM;
1831 }
1832
1833 /* Get port early, so can set affinity prior to memory allocation */
1834 static int ipath_assign_port(struct file *fp,
1835                               const struct ipath_user_info *uinfo)
1836 {
1837         int ret;
1838         int i_minor;
1839         unsigned swmajor, swminor;
1840
1841         /* Check to be sure we haven't already initialized this file */
1842         if (port_fp(fp)) {
1843                 ret = -EINVAL;
1844                 goto done;
1845         }
1846
1847         /* for now, if major version is different, bail */
1848         swmajor = uinfo->spu_userversion >> 16;
1849         if (swmajor != IPATH_USER_SWMAJOR) {
1850                 ipath_dbg("User major version %d not same as driver "
1851                           "major %d\n", uinfo->spu_userversion >> 16,
1852                           IPATH_USER_SWMAJOR);
1853                 ret = -ENODEV;
1854                 goto done;
1855         }
1856
1857         swminor = uinfo->spu_userversion & 0xffff;
1858         if (swminor != IPATH_USER_SWMINOR)
1859                 ipath_dbg("User minor version %d not same as driver "
1860                           "minor %d\n", swminor, IPATH_USER_SWMINOR);
1861
1862         mutex_lock(&ipath_mutex);
1863
1864         if (ipath_compatible_subports(swmajor, swminor) &&
1865             uinfo->spu_subport_cnt &&
1866             (ret = find_shared_port(fp, uinfo))) {
1867                 if (ret > 0)
1868                         ret = 0;
1869                 goto done_chk_sdma;
1870         }
1871
1872         i_minor = iminor(file_inode(fp)) - IPATH_USER_MINOR_BASE;
1873         ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
1874                    (long)file_inode(fp)->i_rdev, i_minor);
1875
1876         if (i_minor)
1877                 ret = find_free_port(i_minor - 1, fp, uinfo);
1878         else
1879                 ret = find_best_unit(fp, uinfo);
1880
1881 done_chk_sdma:
1882         if (!ret) {
1883                 struct ipath_filedata *fd = fp->private_data;
1884                 const struct ipath_portdata *pd = fd->pd;
1885                 const struct ipath_devdata *dd = pd->port_dd;
1886
1887                 fd->pq = ipath_user_sdma_queue_create(&dd->pcidev->dev,
1888                                                       dd->ipath_unit,
1889                                                       pd->port_port,
1890                                                       fd->subport);
1891
1892                 if (!fd->pq)
1893                         ret = -ENOMEM;
1894         }
1895
1896         mutex_unlock(&ipath_mutex);
1897
1898 done:
1899         return ret;
1900 }
1901
1902
1903 static int ipath_do_user_init(struct file *fp,
1904                               const struct ipath_user_info *uinfo)
1905 {
1906         int ret;
1907         struct ipath_portdata *pd = port_fp(fp);
1908         struct ipath_devdata *dd;
1909         u32 head32;
1910
1911         /* Subports don't need to initialize anything since master did it. */
1912         if (subport_fp(fp)) {
1913                 ret = wait_event_interruptible(pd->port_wait,
1914                         !test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
1915                 goto done;
1916         }
1917
1918         dd = pd->port_dd;
1919
1920         if (uinfo->spu_rcvhdrsize) {
1921                 ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
1922                 if (ret)
1923                         goto done;
1924         }
1925
1926         /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
1927
1928         /* some ports may get extra buffers, calculate that here */
1929         if (pd->port_port <= dd->ipath_ports_extrabuf)
1930                 pd->port_piocnt = dd->ipath_pbufsport + 1;
1931         else
1932                 pd->port_piocnt = dd->ipath_pbufsport;
1933
1934         /* for right now, kernel piobufs are at end, so port 1 is at 0 */
1935         if (pd->port_port <= dd->ipath_ports_extrabuf)
1936                 pd->port_pio_base = (dd->ipath_pbufsport + 1)
1937                         * (pd->port_port - 1);
1938         else
1939                 pd->port_pio_base = dd->ipath_ports_extrabuf +
1940                         dd->ipath_pbufsport * (pd->port_port - 1);
1941         pd->port_piobufs = dd->ipath_piobufbase +
1942                 pd->port_pio_base * dd->ipath_palign;
1943         ipath_cdbg(VERBOSE, "piobuf base for port %u is 0x%x, piocnt %u,"
1944                 " first pio %u\n", pd->port_port, pd->port_piobufs,
1945                 pd->port_piocnt, pd->port_pio_base);
1946         ipath_chg_pioavailkernel(dd, pd->port_pio_base, pd->port_piocnt, 0);
1947
1948         /*
1949          * Now allocate the rcvhdr Q and eager TIDs; skip the TID
1950          * array for time being.  If pd->port_port > chip-supported,
1951          * we need to do extra stuff here to handle by handling overflow
1952          * through port 0, someday
1953          */
1954         ret = ipath_create_rcvhdrq(dd, pd);
1955         if (!ret)
1956                 ret = ipath_create_user_egr(pd);
1957         if (ret)
1958                 goto done;
1959
1960         /*
1961          * set the eager head register for this port to the current values
1962          * of the tail pointers, since we don't know if they were
1963          * updated on last use of the port.
1964          */
1965         head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
1966         ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
1967         pd->port_lastrcvhdrqtail = -1;
1968         ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
1969                 pd->port_port, head32);
1970         pd->port_tidcursor = 0; /* start at beginning after open */
1971
1972         /* initialize poll variables... */
1973         pd->port_urgent = 0;
1974         pd->port_urgent_poll = 0;
1975         pd->port_hdrqfull_poll = pd->port_hdrqfull;
1976
1977         /*
1978          * Now enable the port for receive.
1979          * For chips that are set to DMA the tail register to memory
1980          * when they change (and when the update bit transitions from
1981          * 0 to 1.  So for those chips, we turn it off and then back on.
1982          * This will (very briefly) affect any other open ports, but the
1983          * duration is very short, and therefore isn't an issue.  We
1984          * explicitly set the in-memory tail copy to 0 beforehand, so we
1985          * don't have to wait to be sure the DMA update has happened
1986          * (chip resets head/tail to 0 on transition to enable).
1987          */
1988         set_bit(dd->ipath_r_portenable_shift + pd->port_port,
1989                 &dd->ipath_rcvctrl);
1990         if (!(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
1991                 if (pd->port_rcvhdrtail_kvaddr)
1992                         ipath_clear_rcvhdrtail(pd);
1993                 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1994                         dd->ipath_rcvctrl &
1995                         ~(1ULL << dd->ipath_r_tailupd_shift));
1996         }
1997         ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1998                          dd->ipath_rcvctrl);
1999         /* Notify any waiting slaves */
2000         if (pd->port_subport_cnt) {
2001                 clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
2002                 wake_up(&pd->port_wait);
2003         }
2004 done:
2005         return ret;
2006 }
2007
2008 /**
2009  * unlock_exptid - unlock any expected TID entries port still had in use
2010  * @pd: port
2011  *
2012  * We don't actually update the chip here, because we do a bulk update
2013  * below, using ipath_f_clear_tids.
2014  */
2015 static void unlock_expected_tids(struct ipath_portdata *pd)
2016 {
2017         struct ipath_devdata *dd = pd->port_dd;
2018         int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
2019         int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
2020
2021         ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
2022                    pd->port_port);
2023         for (i = port_tidbase; i < maxtid; i++) {
2024                 struct page *ps = dd->ipath_pageshadow[i];
2025
2026                 if (!ps)
2027                         continue;
2028
2029                 dd->ipath_pageshadow[i] = NULL;
2030                 pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
2031                         PAGE_SIZE, PCI_DMA_FROMDEVICE);
2032                 ipath_release_user_pages_on_close(&ps, 1);
2033                 cnt++;
2034                 ipath_stats.sps_pageunlocks++;
2035         }
2036         if (cnt)
2037                 ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
2038                            pd->port_port, cnt);
2039
2040         if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
2041                 ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
2042                            (unsigned long long) ipath_stats.sps_pagelocks,
2043                            (unsigned long long)
2044                            ipath_stats.sps_pageunlocks);
2045 }
2046
2047 static int ipath_close(struct inode *in, struct file *fp)
2048 {
2049         struct ipath_filedata *fd;
2050         struct ipath_portdata *pd;
2051         struct ipath_devdata *dd;
2052         unsigned long flags;
2053         unsigned port;
2054         struct pid *pid;
2055
2056         ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
2057                    (long)in->i_rdev, fp->private_data);
2058
2059         mutex_lock(&ipath_mutex);
2060
2061         fd = fp->private_data;
2062         fp->private_data = NULL;
2063         pd = fd->pd;
2064         if (!pd) {
2065                 mutex_unlock(&ipath_mutex);
2066                 goto bail;
2067         }
2068
2069         dd = pd->port_dd;
2070
2071         /* drain user sdma queue */
2072         ipath_user_sdma_queue_drain(dd, fd->pq);
2073         ipath_user_sdma_queue_destroy(fd->pq);
2074
2075         if (--pd->port_cnt) {
2076                 /*
2077                  * XXX If the master closes the port before the slave(s),
2078                  * revoke the mmap for the eager receive queue so
2079                  * the slave(s) don't wait for receive data forever.
2080                  */
2081                 pd->active_slaves &= ~(1 << fd->subport);
2082                 put_pid(pd->port_subpid[fd->subport]);
2083                 pd->port_subpid[fd->subport] = NULL;
2084                 mutex_unlock(&ipath_mutex);
2085                 goto bail;
2086         }
2087         /* early; no interrupt users after this */
2088         spin_lock_irqsave(&dd->ipath_uctxt_lock, flags);
2089         port = pd->port_port;
2090         dd->ipath_pd[port] = NULL;
2091         pid = pd->port_pid;
2092         pd->port_pid = NULL;
2093         spin_unlock_irqrestore(&dd->ipath_uctxt_lock, flags);
2094
2095         if (pd->port_rcvwait_to || pd->port_piowait_to
2096             || pd->port_rcvnowait || pd->port_pionowait) {
2097                 ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
2098                            "%u rcv %u, pio already\n",
2099                            pd->port_port, pd->port_rcvwait_to,
2100                            pd->port_piowait_to, pd->port_rcvnowait,
2101                            pd->port_pionowait);
2102                 pd->port_rcvwait_to = pd->port_piowait_to =
2103                         pd->port_rcvnowait = pd->port_pionowait = 0;
2104         }
2105         if (pd->port_flag) {
2106                 ipath_cdbg(PROC, "port %u port_flag set: 0x%lx\n",
2107                           pd->port_port, pd->port_flag);
2108                 pd->port_flag = 0;
2109         }
2110
2111         if (dd->ipath_kregbase) {
2112                 /* atomically clear receive enable port and intr avail. */
2113                 clear_bit(dd->ipath_r_portenable_shift + port,
2114                           &dd->ipath_rcvctrl);
2115                 clear_bit(pd->port_port + dd->ipath_r_intravail_shift,
2116                           &dd->ipath_rcvctrl);
2117                 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
2118                         dd->ipath_rcvctrl);
2119                 /* and read back from chip to be sure that nothing
2120                  * else is in flight when we do the rest */
2121                 (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
2122
2123                 /* clean up the pkeys for this port user */
2124                 ipath_clean_part_key(pd, dd);
2125                 /*
2126                  * be paranoid, and never write 0's to these, just use an
2127                  * unused part of the port 0 tail page.  Of course,
2128                  * rcvhdraddr points to a large chunk of memory, so this
2129                  * could still trash things, but at least it won't trash
2130                  * page 0, and by disabling the port, it should stop "soon",
2131                  * even if a packet or two is in already in flight after we
2132                  * disabled the port.
2133                  */
2134                 ipath_write_kreg_port(dd,
2135                         dd->ipath_kregs->kr_rcvhdrtailaddr, port,
2136                         dd->ipath_dummy_hdrq_phys);
2137                 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
2138                         pd->port_port, dd->ipath_dummy_hdrq_phys);
2139
2140                 ipath_disarm_piobufs(dd, pd->port_pio_base, pd->port_piocnt);
2141                 ipath_chg_pioavailkernel(dd, pd->port_pio_base,
2142                         pd->port_piocnt, 1);
2143
2144                 dd->ipath_f_clear_tids(dd, pd->port_port);
2145
2146                 if (dd->ipath_pageshadow)
2147                         unlock_expected_tids(pd);
2148                 ipath_stats.sps_ports--;
2149                 ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
2150                            pd->port_comm, pid_nr(pid),
2151                            dd->ipath_unit, port);
2152         }
2153
2154         put_pid(pid);
2155         mutex_unlock(&ipath_mutex);
2156         ipath_free_pddata(dd, pd); /* after releasing the mutex */
2157
2158 bail:
2159         kfree(fd);
2160         return 0;
2161 }
2162
2163 static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
2164                            struct ipath_port_info __user *uinfo)
2165 {
2166         struct ipath_port_info info;
2167         int nup;
2168         int ret;
2169         size_t sz;
2170
2171         (void) ipath_count_units(NULL, &nup, NULL);
2172         info.num_active = nup;
2173         info.unit = pd->port_dd->ipath_unit;
2174         info.port = pd->port_port;
2175         info.subport = subport;
2176         /* Don't return new fields if old library opened the port. */
2177         if (ipath_supports_subports(pd->userversion >> 16,
2178                                     pd->userversion & 0xffff)) {
2179                 /* Number of user ports available for this device. */
2180                 info.num_ports = pd->port_dd->ipath_cfgports - 1;
2181                 info.num_subports = pd->port_subport_cnt;
2182                 sz = sizeof(info);
2183         } else
2184                 sz = sizeof(info) - 2 * sizeof(u16);
2185
2186         if (copy_to_user(uinfo, &info, sz)) {
2187                 ret = -EFAULT;
2188                 goto bail;
2189         }
2190         ret = 0;
2191
2192 bail:
2193         return ret;
2194 }
2195
2196 static int ipath_get_slave_info(struct ipath_portdata *pd,
2197                                 void __user *slave_mask_addr)
2198 {
2199         int ret = 0;
2200
2201         if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
2202                 ret = -EFAULT;
2203         return ret;
2204 }
2205
2206 static int ipath_sdma_get_inflight(struct ipath_user_sdma_queue *pq,
2207                                    u32 __user *inflightp)
2208 {
2209         const u32 val = ipath_user_sdma_inflight_counter(pq);
2210
2211         if (put_user(val, inflightp))
2212                 return -EFAULT;
2213
2214         return 0;
2215 }
2216
2217 static int ipath_sdma_get_complete(struct ipath_devdata *dd,
2218                                    struct ipath_user_sdma_queue *pq,
2219                                    u32 __user *completep)
2220 {
2221         u32 val;
2222         int err;
2223
2224         err = ipath_user_sdma_make_progress(dd, pq);
2225         if (err < 0)
2226                 return err;
2227
2228         val = ipath_user_sdma_complete_counter(pq);
2229         if (put_user(val, completep))
2230                 return -EFAULT;
2231
2232         return 0;
2233 }
2234
2235 static ssize_t ipath_write(struct file *fp, const char __user *data,
2236                            size_t count, loff_t *off)
2237 {
2238         const struct ipath_cmd __user *ucmd;
2239         struct ipath_portdata *pd;
2240         const void __user *src;
2241         size_t consumed, copy;
2242         struct ipath_cmd cmd;
2243         ssize_t ret = 0;
2244         void *dest;
2245
2246         if (count < sizeof(cmd.type)) {
2247                 ret = -EINVAL;
2248                 goto bail;
2249         }
2250
2251         ucmd = (const struct ipath_cmd __user *) data;
2252
2253         if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
2254                 ret = -EFAULT;
2255                 goto bail;
2256         }
2257
2258         consumed = sizeof(cmd.type);
2259
2260         switch (cmd.type) {
2261         case IPATH_CMD_ASSIGN_PORT:
2262         case __IPATH_CMD_USER_INIT:
2263         case IPATH_CMD_USER_INIT:
2264                 copy = sizeof(cmd.cmd.user_info);
2265                 dest = &cmd.cmd.user_info;
2266                 src = &ucmd->cmd.user_info;
2267                 break;
2268         case IPATH_CMD_RECV_CTRL:
2269                 copy = sizeof(cmd.cmd.recv_ctrl);
2270                 dest = &cmd.cmd.recv_ctrl;
2271                 src = &ucmd->cmd.recv_ctrl;
2272                 break;
2273         case IPATH_CMD_PORT_INFO:
2274                 copy = sizeof(cmd.cmd.port_info);
2275                 dest = &cmd.cmd.port_info;
2276                 src = &ucmd->cmd.port_info;
2277                 break;
2278         case IPATH_CMD_TID_UPDATE:
2279         case IPATH_CMD_TID_FREE:
2280                 copy = sizeof(cmd.cmd.tid_info);
2281                 dest = &cmd.cmd.tid_info;
2282                 src = &ucmd->cmd.tid_info;
2283                 break;
2284         case IPATH_CMD_SET_PART_KEY:
2285                 copy = sizeof(cmd.cmd.part_key);
2286                 dest = &cmd.cmd.part_key;
2287                 src = &ucmd->cmd.part_key;
2288                 break;
2289         case __IPATH_CMD_SLAVE_INFO:
2290                 copy = sizeof(cmd.cmd.slave_mask_addr);
2291                 dest = &cmd.cmd.slave_mask_addr;
2292                 src = &ucmd->cmd.slave_mask_addr;
2293                 break;
2294         case IPATH_CMD_PIOAVAILUPD:     // force an update of PIOAvail reg
2295                 copy = 0;
2296                 src = NULL;
2297                 dest = NULL;
2298                 break;
2299         case IPATH_CMD_POLL_TYPE:
2300                 copy = sizeof(cmd.cmd.poll_type);
2301                 dest = &cmd.cmd.poll_type;
2302                 src = &ucmd->cmd.poll_type;
2303                 break;
2304         case IPATH_CMD_ARMLAUNCH_CTRL:
2305                 copy = sizeof(cmd.cmd.armlaunch_ctrl);
2306                 dest = &cmd.cmd.armlaunch_ctrl;
2307                 src = &ucmd->cmd.armlaunch_ctrl;
2308                 break;
2309         case IPATH_CMD_SDMA_INFLIGHT:
2310                 copy = sizeof(cmd.cmd.sdma_inflight);
2311                 dest = &cmd.cmd.sdma_inflight;
2312                 src = &ucmd->cmd.sdma_inflight;
2313                 break;
2314         case IPATH_CMD_SDMA_COMPLETE:
2315                 copy = sizeof(cmd.cmd.sdma_complete);
2316                 dest = &cmd.cmd.sdma_complete;
2317                 src = &ucmd->cmd.sdma_complete;
2318                 break;
2319         default:
2320                 ret = -EINVAL;
2321                 goto bail;
2322         }
2323
2324         if (copy) {
2325                 if ((count - consumed) < copy) {
2326                         ret = -EINVAL;
2327                         goto bail;
2328                 }
2329
2330                 if (copy_from_user(dest, src, copy)) {
2331                         ret = -EFAULT;
2332                         goto bail;
2333                 }
2334
2335                 consumed += copy;
2336         }
2337
2338         pd = port_fp(fp);
2339         if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
2340                 cmd.type != IPATH_CMD_ASSIGN_PORT) {
2341                 ret = -EINVAL;
2342                 goto bail;
2343         }
2344
2345         switch (cmd.type) {
2346         case IPATH_CMD_ASSIGN_PORT:
2347                 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2348                 if (ret)
2349                         goto bail;
2350                 break;
2351         case __IPATH_CMD_USER_INIT:
2352                 /* backwards compatibility, get port first */
2353                 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2354                 if (ret)
2355                         goto bail;
2356                 /* and fall through to current version. */
2357         case IPATH_CMD_USER_INIT:
2358                 ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
2359                 if (ret)
2360                         goto bail;
2361                 ret = ipath_get_base_info(
2362                         fp, (void __user *) (unsigned long)
2363                         cmd.cmd.user_info.spu_base_info,
2364                         cmd.cmd.user_info.spu_base_info_size);
2365                 break;
2366         case IPATH_CMD_RECV_CTRL:
2367                 ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
2368                 break;
2369         case IPATH_CMD_PORT_INFO:
2370                 ret = ipath_port_info(pd, subport_fp(fp),
2371                                       (struct ipath_port_info __user *)
2372                                       (unsigned long) cmd.cmd.port_info);
2373                 break;
2374         case IPATH_CMD_TID_UPDATE:
2375                 ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
2376                 break;
2377         case IPATH_CMD_TID_FREE:
2378                 ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
2379                 break;
2380         case IPATH_CMD_SET_PART_KEY:
2381                 ret = ipath_set_part_key(pd, cmd.cmd.part_key);
2382                 break;
2383         case __IPATH_CMD_SLAVE_INFO:
2384                 ret = ipath_get_slave_info(pd,
2385                                            (void __user *) (unsigned long)
2386                                            cmd.cmd.slave_mask_addr);
2387                 break;
2388         case IPATH_CMD_PIOAVAILUPD:
2389                 ipath_force_pio_avail_update(pd->port_dd);
2390                 break;
2391         case IPATH_CMD_POLL_TYPE:
2392                 pd->poll_type = cmd.cmd.poll_type;
2393                 break;
2394         case IPATH_CMD_ARMLAUNCH_CTRL:
2395                 if (cmd.cmd.armlaunch_ctrl)
2396                         ipath_enable_armlaunch(pd->port_dd);
2397                 else
2398                         ipath_disable_armlaunch(pd->port_dd);
2399                 break;
2400         case IPATH_CMD_SDMA_INFLIGHT:
2401                 ret = ipath_sdma_get_inflight(user_sdma_queue_fp(fp),
2402                                               (u32 __user *) (unsigned long)
2403                                               cmd.cmd.sdma_inflight);
2404                 break;
2405         case IPATH_CMD_SDMA_COMPLETE:
2406                 ret = ipath_sdma_get_complete(pd->port_dd,
2407                                               user_sdma_queue_fp(fp),
2408                                               (u32 __user *) (unsigned long)
2409                                               cmd.cmd.sdma_complete);
2410                 break;
2411         }
2412
2413         if (ret >= 0)
2414                 ret = consumed;
2415
2416 bail:
2417         return ret;
2418 }
2419
2420 static ssize_t ipath_write_iter(struct kiocb *iocb, struct iov_iter *from)
2421 {
2422         struct file *filp = iocb->ki_filp;
2423         struct ipath_filedata *fp = filp->private_data;
2424         struct ipath_portdata *pd = port_fp(filp);
2425         struct ipath_user_sdma_queue *pq = fp->pq;
2426
2427         if (!iter_is_iovec(from) || !from->nr_segs)
2428                 return -EINVAL;
2429
2430         return ipath_user_sdma_writev(pd->port_dd, pq, from->iov, from->nr_segs);
2431 }
2432
2433 static struct class *ipath_class;
2434
2435 static int init_cdev(int minor, char *name, const struct file_operations *fops,
2436                      struct cdev **cdevp, struct device **devp)
2437 {
2438         const dev_t dev = MKDEV(IPATH_MAJOR, minor);
2439         struct cdev *cdev = NULL;
2440         struct device *device = NULL;
2441         int ret;
2442
2443         cdev = cdev_alloc();
2444         if (!cdev) {
2445                 printk(KERN_ERR IPATH_DRV_NAME
2446                        ": Could not allocate cdev for minor %d, %s\n",
2447                        minor, name);
2448                 ret = -ENOMEM;
2449                 goto done;
2450         }
2451
2452         cdev->owner = THIS_MODULE;
2453         cdev->ops = fops;
2454         kobject_set_name(&cdev->kobj, name);
2455
2456         ret = cdev_add(cdev, dev, 1);
2457         if (ret < 0) {
2458                 printk(KERN_ERR IPATH_DRV_NAME
2459                        ": Could not add cdev for minor %d, %s (err %d)\n",
2460                        minor, name, -ret);
2461                 goto err_cdev;
2462         }
2463
2464         device = device_create(ipath_class, NULL, dev, NULL, name);
2465
2466         if (IS_ERR(device)) {
2467                 ret = PTR_ERR(device);
2468                 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2469                        "device for minor %d, %s (err %d)\n",
2470                        minor, name, -ret);
2471                 goto err_cdev;
2472         }
2473
2474         goto done;
2475
2476 err_cdev:
2477         cdev_del(cdev);
2478         cdev = NULL;
2479
2480 done:
2481         if (ret >= 0) {
2482                 *cdevp = cdev;
2483                 *devp = device;
2484         } else {
2485                 *cdevp = NULL;
2486                 *devp = NULL;
2487         }
2488
2489         return ret;
2490 }
2491
2492 int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
2493                     struct cdev **cdevp, struct device **devp)
2494 {
2495         return init_cdev(minor, name, fops, cdevp, devp);
2496 }
2497
2498 static void cleanup_cdev(struct cdev **cdevp,
2499                          struct device **devp)
2500 {
2501         struct device *dev = *devp;
2502
2503         if (dev) {
2504                 device_unregister(dev);
2505                 *devp = NULL;
2506         }
2507
2508         if (*cdevp) {
2509                 cdev_del(*cdevp);
2510                 *cdevp = NULL;
2511         }
2512 }
2513
2514 void ipath_cdev_cleanup(struct cdev **cdevp,
2515                         struct device **devp)
2516 {
2517         cleanup_cdev(cdevp, devp);
2518 }
2519
2520 static struct cdev *wildcard_cdev;
2521 static struct device *wildcard_dev;
2522
2523 static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
2524
2525 static int user_init(void)
2526 {
2527         int ret;
2528
2529         ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
2530         if (ret < 0) {
2531                 printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
2532                        "chrdev region (err %d)\n", -ret);
2533                 goto done;
2534         }
2535
2536         ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
2537
2538         if (IS_ERR(ipath_class)) {
2539                 ret = PTR_ERR(ipath_class);
2540                 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2541                        "device class (err %d)\n", -ret);
2542                 goto bail;
2543         }
2544
2545         goto done;
2546 bail:
2547         unregister_chrdev_region(dev, IPATH_NMINORS);
2548 done:
2549         return ret;
2550 }
2551
2552 static void user_cleanup(void)
2553 {
2554         if (ipath_class) {
2555                 class_destroy(ipath_class);
2556                 ipath_class = NULL;
2557         }
2558
2559         unregister_chrdev_region(dev, IPATH_NMINORS);
2560 }
2561
2562 static atomic_t user_count = ATOMIC_INIT(0);
2563 static atomic_t user_setup = ATOMIC_INIT(0);
2564
2565 int ipath_user_add(struct ipath_devdata *dd)
2566 {
2567         char name[10];
2568         int ret;
2569
2570         if (atomic_inc_return(&user_count) == 1) {
2571                 ret = user_init();
2572                 if (ret < 0) {
2573                         ipath_dev_err(dd, "Unable to set up user support: "
2574                                       "error %d\n", -ret);
2575                         goto bail;
2576                 }
2577                 ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
2578                                 &wildcard_dev);
2579                 if (ret < 0) {
2580                         ipath_dev_err(dd, "Could not create wildcard "
2581                                       "minor: error %d\n", -ret);
2582                         goto bail_user;
2583                 }
2584
2585                 atomic_set(&user_setup, 1);
2586         }
2587
2588         snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
2589
2590         ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
2591                         &dd->user_cdev, &dd->user_dev);
2592         if (ret < 0)
2593                 ipath_dev_err(dd, "Could not create user minor %d, %s\n",
2594                               dd->ipath_unit + 1, name);
2595
2596         goto bail;
2597
2598 bail_user:
2599         user_cleanup();
2600 bail:
2601         return ret;
2602 }
2603
2604 void ipath_user_remove(struct ipath_devdata *dd)
2605 {
2606         cleanup_cdev(&dd->user_cdev, &dd->user_dev);
2607
2608         if (atomic_dec_return(&user_count) == 0) {
2609                 if (atomic_read(&user_setup) == 0)
2610                         goto bail;
2611
2612                 cleanup_cdev(&wildcard_cdev, &wildcard_dev);
2613                 user_cleanup();
2614
2615                 atomic_set(&user_setup, 0);
2616         }
2617 bail:
2618         return;
2619 }