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