2 * Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
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 <linux/highmem.h>
41 #include <linux/jiffies.h>
42 #include <linux/smp_lock.h>
43 #include <asm/pgtable.h>
45 #include "ipath_kernel.h"
46 #include "ipath_common.h"
47 #include "ipath_user_sdma.h"
49 static int ipath_open(struct inode *, struct file *);
50 static int ipath_close(struct inode *, struct file *);
51 static ssize_t ipath_write(struct file *, const char __user *, size_t,
53 static ssize_t ipath_writev(struct kiocb *, const struct iovec *,
54 unsigned long , loff_t);
55 static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
56 static int ipath_mmap(struct file *, struct vm_area_struct *);
58 static const struct file_operations ipath_file_ops = {
61 .aio_write = ipath_writev,
63 .release = ipath_close,
69 * Convert kernel virtual addresses to physical addresses so they don't
70 * potentially conflict with the chip addresses used as mmap offsets.
71 * It doesn't really matter what mmap offset we use as long as we can
72 * interpret it correctly.
74 static u64 cvt_kvaddr(void *p)
79 page = vmalloc_to_page(p);
81 paddr = page_to_pfn(page) << PAGE_SHIFT;
86 static int ipath_get_base_info(struct file *fp,
87 void __user *ubase, size_t ubase_size)
89 struct ipath_portdata *pd = port_fp(fp);
91 struct ipath_base_info *kinfo = NULL;
92 struct ipath_devdata *dd = pd->port_dd;
97 subport_cnt = pd->port_subport_cnt;
104 master = !subport_fp(fp);
108 /* If port sharing is not requested, allow the old size structure */
110 sz -= 7 * sizeof(u64);
111 if (ubase_size < sz) {
113 "Base size %zu, need %zu (version mismatch?)\n",
119 kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
125 ret = dd->ipath_f_get_base_info(pd, kinfo);
129 kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
130 kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
131 kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
132 kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
134 * have to mmap whole thing
136 kinfo->spi_rcv_egrbuftotlen =
137 pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
138 kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
139 kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
140 pd->port_rcvegrbuf_chunks;
141 kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;
143 kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;
145 * for this use, may be ipath_cfgports summed over all chips that
146 * are are configured and present
148 kinfo->spi_nports = dd->ipath_cfgports;
149 /* unit (chip/board) our port is on */
150 kinfo->spi_unit = dd->ipath_unit;
151 /* for now, only a single page */
152 kinfo->spi_tid_maxsize = PAGE_SIZE;
155 * Doing this per port, and based on the skip value, etc. This has
156 * to be the actual buffer size, since the protocol code treats it
159 * These have to be set to user addresses in the user code via mmap.
160 * These values are used on return to user code for the mmap target
161 * addresses only. For 32 bit, same 44 bit address problem, so use
162 * the physical address, not virtual. Before 2.6.11, using the
163 * page_address() macro worked, but in 2.6.11, even that returns the
164 * full 64 bit address (upper bits all 1's). So far, using the
165 * physical addresses (or chip offsets, for chip mapping) works, but
166 * no doubt some future kernel release will change that, and we'll be
167 * on to yet another method of dealing with this.
169 kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
170 kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;
171 kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
172 kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
173 kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
174 (void *) dd->ipath_statusp -
175 (void *) dd->ipath_pioavailregs_dma;
177 kinfo->spi_piocnt = pd->port_piocnt;
178 kinfo->spi_piobufbase = (u64) pd->port_piobufs;
179 kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
180 dd->ipath_ureg_align * pd->port_port;
182 kinfo->spi_piocnt = (pd->port_piocnt / subport_cnt) +
183 (pd->port_piocnt % subport_cnt);
184 /* Master's PIO buffers are after all the slave's */
185 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
187 (pd->port_piocnt - kinfo->spi_piocnt);
189 unsigned slave = subport_fp(fp) - 1;
191 kinfo->spi_piocnt = pd->port_piocnt / subport_cnt;
192 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
193 dd->ipath_palign * kinfo->spi_piocnt * slave;
197 kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase +
198 dd->ipath_ureg_align * pd->port_port;
199 kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs;
200 kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base;
201 kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr;
203 kinfo->__spi_uregbase = cvt_kvaddr(pd->subport_uregbase +
204 PAGE_SIZE * subport_fp(fp));
206 kinfo->spi_rcvhdr_base = cvt_kvaddr(pd->subport_rcvhdr_base +
207 pd->port_rcvhdrq_size * subport_fp(fp));
208 kinfo->spi_rcvhdr_tailaddr = 0;
209 kinfo->spi_rcv_egrbufs = cvt_kvaddr(pd->subport_rcvegrbuf +
210 pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size *
213 kinfo->spi_subport_uregbase =
214 cvt_kvaddr(pd->subport_uregbase);
215 kinfo->spi_subport_rcvegrbuf =
216 cvt_kvaddr(pd->subport_rcvegrbuf);
217 kinfo->spi_subport_rcvhdr_base =
218 cvt_kvaddr(pd->subport_rcvhdr_base);
219 ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",
220 kinfo->spi_port, kinfo->spi_runtime_flags,
221 (unsigned long long) kinfo->spi_subport_uregbase,
222 (unsigned long long) kinfo->spi_subport_rcvegrbuf,
223 (unsigned long long) kinfo->spi_subport_rcvhdr_base);
226 kinfo->spi_pioindex = (kinfo->spi_piobufbase - dd->ipath_piobufbase) /
228 kinfo->spi_pioalign = dd->ipath_palign;
230 kinfo->spi_qpair = IPATH_KD_QP;
232 * user mode PIO buffers are always 2KB, even when 4KB can
233 * be received, and sent via the kernel; this is ibmaxlen
236 kinfo->spi_piosize = dd->ipath_piosize2k - 2 * sizeof(u32);
237 kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */
238 kinfo->spi_port = pd->port_port;
239 kinfo->spi_subport = subport_fp(fp);
240 kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
241 kinfo->spi_hw_version = dd->ipath_revision;
244 kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;
247 sz = (ubase_size < sizeof(*kinfo)) ? ubase_size : sizeof(*kinfo);
248 if (copy_to_user(ubase, kinfo, sz))
257 * ipath_tid_update - update a port TID
259 * @fp: the ipath device file
260 * @ti: the TID information
262 * The new implementation as of Oct 2004 is that the driver assigns
263 * the tid and returns it to the caller. To make it easier to
264 * catch bugs, and to reduce search time, we keep a cursor for
265 * each port, walking the shadow tid array to find one that's not
268 * For now, if we can't allocate the full list, we fail, although
269 * in the long run, we'll allocate as many as we can, and the
270 * caller will deal with that by trying the remaining pages later.
271 * That means that when we fail, we have to mark the tids as not in
272 * use again, in our shadow copy.
274 * It's up to the caller to free the tids when they are done.
275 * We'll unlock the pages as they free them.
277 * Also, right now we are locking one page at a time, but since
278 * the intended use of this routine is for a single group of
279 * virtually contiguous pages, that should change to improve
282 static int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,
283 const struct ipath_tid_info *ti)
286 u32 tid, porttid, cnt, i, tidcnt, tidoff;
288 struct ipath_devdata *dd = pd->port_dd;
291 u64 __iomem *tidbase;
292 unsigned long tidmap[8];
293 struct page **pagep = NULL;
294 unsigned subport = subport_fp(fp);
296 if (!dd->ipath_pageshadow) {
303 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
304 (unsigned long long) ti->tidlist);
306 * Should we treat as success? likely a bug
311 porttid = pd->port_port * dd->ipath_rcvtidcnt;
312 if (!pd->port_subport_cnt) {
313 tidcnt = dd->ipath_rcvtidcnt;
314 tid = pd->port_tidcursor;
316 } else if (!subport) {
317 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
318 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
319 tidoff = dd->ipath_rcvtidcnt - tidcnt;
321 tid = tidcursor_fp(fp);
323 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
324 tidoff = tidcnt * (subport - 1);
326 tid = tidcursor_fp(fp);
329 /* make sure it all fits in port_tid_pg_list */
330 dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
331 "TIDs, only trying max (%u)\n", cnt, tidcnt);
334 pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];
335 tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];
337 memset(tidmap, 0, sizeof(tidmap));
338 /* before decrement; chip actual # */
340 tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
341 dd->ipath_rcvtidbase +
342 porttid * sizeof(*tidbase));
344 ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
345 pd->port_port, cnt, tid, tidbase);
347 /* virtual address of first page in transfer */
348 vaddr = ti->tidvaddr;
349 if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
351 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
356 ret = ipath_get_user_pages(vaddr, cnt, pagep);
359 ipath_dbg("Failed to lock addr %p, %u pages "
360 "(already locked)\n",
361 (void *) vaddr, cnt);
363 * for now, continue, and see what happens but with
364 * the new implementation, this should never happen,
365 * unless perhaps the user has mpin'ed the pages
366 * themselves (something we need to test)
370 dev_info(&dd->pcidev->dev,
371 "Failed to lock addr %p, %u pages: "
372 "errno %d\n", (void *) vaddr, cnt, -ret);
376 for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
377 for (; ntids--; tid++) {
380 if (!dd->ipath_pageshadow[porttid + tid])
385 * oops, wrapped all the way through their TIDs,
386 * and didn't have enough free; see comments at
389 ipath_dbg("Not enough free TIDs for %u pages "
390 "(index %d), failing\n", cnt, i);
391 i--; /* last tidlist[i] not filled in */
395 tidlist[i] = tid + tidoff;
396 ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
397 "vaddr %lx\n", i, tid + tidoff, vaddr);
398 /* we "know" system pages and TID pages are same size */
399 dd->ipath_pageshadow[porttid + tid] = pagep[i];
400 dd->ipath_physshadow[porttid + tid] = ipath_map_page(
401 dd->pcidev, pagep[i], 0, PAGE_SIZE,
404 * don't need atomic or it's overhead
406 __set_bit(tid, tidmap);
407 physaddr = dd->ipath_physshadow[porttid + tid];
408 ipath_stats.sps_pagelocks++;
410 "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
411 tid, vaddr, (unsigned long long) physaddr,
413 dd->ipath_f_put_tid(dd, &tidbase[tid], RCVHQ_RCV_TYPE_EXPECTED,
416 * don't check this tid in ipath_portshadow, since we
417 * just filled it in; start with the next one.
425 /* jump here if copy out of updated info failed... */
426 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
428 /* same code that's in ipath_free_tid() */
429 limit = sizeof(tidmap) * BITS_PER_BYTE;
431 /* just in case size changes in future */
433 tid = find_first_bit((const unsigned long *)tidmap, limit);
434 for (; tid < limit; tid++) {
435 if (!test_bit(tid, tidmap))
437 if (dd->ipath_pageshadow[porttid + tid]) {
438 ipath_cdbg(VERBOSE, "Freeing TID %u\n",
440 dd->ipath_f_put_tid(dd, &tidbase[tid],
441 RCVHQ_RCV_TYPE_EXPECTED,
442 dd->ipath_tidinvalid);
443 pci_unmap_page(dd->pcidev,
444 dd->ipath_physshadow[porttid + tid],
445 PAGE_SIZE, PCI_DMA_FROMDEVICE);
446 dd->ipath_pageshadow[porttid + tid] = NULL;
447 ipath_stats.sps_pageunlocks++;
450 ipath_release_user_pages(pagep, cnt);
453 * Copy the updated array, with ipath_tid's filled in, back
454 * to user. Since we did the copy in already, this "should
455 * never fail" If it does, we have to clean up...
457 if (copy_to_user((void __user *)
458 (unsigned long) ti->tidlist,
459 tidlist, cnt * sizeof(*tidlist))) {
463 if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
464 tidmap, sizeof tidmap)) {
470 if (!pd->port_subport_cnt)
471 pd->port_tidcursor = tid;
473 tidcursor_fp(fp) = tid;
478 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
484 * ipath_tid_free - free a port TID
486 * @subport: the subport
489 * right now we are unlocking one page at a time, but since
490 * the intended use of this routine is for a single group of
491 * virtually contiguous pages, that should change to improve
492 * performance. We check that the TID is in range for this port
493 * but otherwise don't check validity; if user has an error and
494 * frees the wrong tid, it's only their own data that can thereby
495 * be corrupted. We do check that the TID was in use, for sanity
496 * We always use our idea of the saved address, not the address that
497 * they pass in to us.
500 static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
501 const struct ipath_tid_info *ti)
504 u32 tid, porttid, cnt, limit, tidcnt;
505 struct ipath_devdata *dd = pd->port_dd;
506 u64 __iomem *tidbase;
507 unsigned long tidmap[8];
509 if (!dd->ipath_pageshadow) {
514 if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
520 porttid = pd->port_port * dd->ipath_rcvtidcnt;
521 if (!pd->port_subport_cnt)
522 tidcnt = dd->ipath_rcvtidcnt;
524 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
525 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
526 porttid += dd->ipath_rcvtidcnt - tidcnt;
528 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
529 porttid += tidcnt * (subport - 1);
531 tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
532 dd->ipath_rcvtidbase +
533 porttid * sizeof(*tidbase));
535 limit = sizeof(tidmap) * BITS_PER_BYTE;
537 /* just in case size changes in future */
539 tid = find_first_bit(tidmap, limit);
540 ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
541 "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
542 limit, tid, porttid);
543 for (cnt = 0; tid < limit; tid++) {
545 * small optimization; if we detect a run of 3 or so without
546 * any set, use find_first_bit again. That's mainly to
547 * accelerate the case where we wrapped, so we have some at
548 * the beginning, and some at the end, and a big gap
551 if (!test_bit(tid, tidmap))
554 if (dd->ipath_pageshadow[porttid + tid]) {
556 p = dd->ipath_pageshadow[porttid + tid];
557 dd->ipath_pageshadow[porttid + tid] = NULL;
558 ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
559 pid_nr(pd->port_pid), tid);
560 dd->ipath_f_put_tid(dd, &tidbase[tid],
561 RCVHQ_RCV_TYPE_EXPECTED,
562 dd->ipath_tidinvalid);
563 pci_unmap_page(dd->pcidev,
564 dd->ipath_physshadow[porttid + tid],
565 PAGE_SIZE, PCI_DMA_FROMDEVICE);
566 ipath_release_user_pages(&p, 1);
567 ipath_stats.sps_pageunlocks++;
569 ipath_dbg("Unused tid %u, ignoring\n", tid);
571 if (cnt != ti->tidcnt)
572 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
576 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
582 * ipath_set_part_key - set a partition key
586 * We can have up to 4 active at a time (other than the default, which is
587 * always allowed). This is somewhat tricky, since multiple ports may set
588 * the same key, so we reference count them, and clean up at exit. All 4
589 * partition keys are packed into a single infinipath register. It's an
590 * error for a process to set the same pkey multiple times. We provide no
591 * mechanism to de-allocate a pkey at this time, we may eventually need to
592 * do that. I've used the atomic operations, and no locking, and only make
593 * a single pass through what's available. This should be more than
594 * adequate for some time. I'll think about spinlocks or the like if and as
597 static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
599 struct ipath_devdata *dd = pd->port_dd;
600 int i, any = 0, pidx = -1;
601 u16 lkey = key & 0x7FFF;
604 if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
605 /* nothing to do; this key always valid */
610 ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
611 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
612 pd->port_port, key, dd->ipath_pkeys[0],
613 atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
614 atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
615 atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
616 atomic_read(&dd->ipath_pkeyrefs[3]));
619 ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
626 * Set the full membership bit, because it has to be
627 * set in the register or the packet, and it seems
628 * cleaner to set in the register than to force all
629 * callers to set it. (see bug 4331)
633 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
634 if (!pd->port_pkeys[i] && pidx == -1)
636 if (pd->port_pkeys[i] == key) {
637 ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
638 "(%x) more than once\n",
645 ipath_dbg("All pkeys for port %u already in use, "
646 "can't set %x\n", pd->port_port, key);
650 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
651 if (!dd->ipath_pkeys[i]) {
655 if (dd->ipath_pkeys[i] == key) {
656 atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
658 if (atomic_inc_return(pkrefs) > 1) {
659 pd->port_pkeys[pidx] = key;
660 ipath_cdbg(VERBOSE, "p%u set key %x "
661 "matches #%d, count now %d\n",
662 pd->port_port, key, i,
663 atomic_read(pkrefs));
668 * lost race, decrement count, catch below
671 ipath_cdbg(VERBOSE, "Lost race, count was "
672 "0, after dec, it's %d\n",
673 atomic_read(pkrefs));
677 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
679 * It makes no sense to have both the limited and
680 * full membership PKEY set at the same time since
681 * the unlimited one will disable the limited one.
688 ipath_dbg("port %u, all pkeys already in use, "
689 "can't set %x\n", pd->port_port, key);
693 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
694 if (!dd->ipath_pkeys[i] &&
695 atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
698 /* for ipathstats, etc. */
699 ipath_stats.sps_pkeys[i] = lkey;
700 pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
702 (u64) dd->ipath_pkeys[0] |
703 ((u64) dd->ipath_pkeys[1] << 16) |
704 ((u64) dd->ipath_pkeys[2] << 32) |
705 ((u64) dd->ipath_pkeys[3] << 48);
706 ipath_cdbg(PROC, "p%u set key %x in #%d, "
707 "portidx %d, new pkey reg %llx\n",
708 pd->port_port, key, i, pidx,
709 (unsigned long long) pkey);
711 dd, dd->ipath_kregs->kr_partitionkey, pkey);
717 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
718 "can't set %x\n", pd->port_port, key);
726 * ipath_manage_rcvq - manage a port's receive queue
728 * @subport: the subport
729 * @start_stop: action to carry out
731 * start_stop == 0 disables receive on the port, for use in queue
732 * overflow conditions. start_stop==1 re-enables, to be used to
733 * re-init the software copy of the head register
735 static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
738 struct ipath_devdata *dd = pd->port_dd;
740 ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
741 start_stop ? "en" : "dis", dd->ipath_unit,
742 pd->port_port, subport);
745 /* atomically clear receive enable port. */
748 * On enable, force in-memory copy of the tail register to
749 * 0, so that protocol code doesn't have to worry about
750 * whether or not the chip has yet updated the in-memory
751 * copy or not on return from the system call. The chip
752 * always resets it's tail register back to 0 on a
753 * transition from disabled to enabled. This could cause a
754 * problem if software was broken, and did the enable w/o
755 * the disable, but eventually the in-memory copy will be
756 * updated and correct itself, even in the face of software
759 if (pd->port_rcvhdrtail_kvaddr)
760 ipath_clear_rcvhdrtail(pd);
761 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
764 clear_bit(dd->ipath_r_portenable_shift + pd->port_port,
766 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
768 /* now be sure chip saw it before we return */
769 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
772 * And try to be sure that tail reg update has happened too.
773 * This should in theory interlock with the RXE changes to
774 * the tail register. Don't assign it to the tail register
775 * in memory copy, since we could overwrite an update by the
778 ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
780 /* always; new head should be equal to new tail; see above */
785 static void ipath_clean_part_key(struct ipath_portdata *pd,
786 struct ipath_devdata *dd)
788 int i, j, pchanged = 0;
791 /* for debugging only */
792 oldpkey = (u64) dd->ipath_pkeys[0] |
793 ((u64) dd->ipath_pkeys[1] << 16) |
794 ((u64) dd->ipath_pkeys[2] << 32) |
795 ((u64) dd->ipath_pkeys[3] << 48);
797 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
798 if (!pd->port_pkeys[i])
800 ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
802 for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
803 /* check for match independent of the global bit */
804 if ((dd->ipath_pkeys[j] & 0x7fff) !=
805 (pd->port_pkeys[i] & 0x7fff))
807 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
808 ipath_cdbg(VERBOSE, "p%u clear key "
811 pd->port_pkeys[i], j);
812 ipath_stats.sps_pkeys[j] =
813 dd->ipath_pkeys[j] = 0;
817 VERBOSE, "p%u key %x matches #%d, "
818 "but ref still %d\n", pd->port_port,
819 pd->port_pkeys[i], j,
820 atomic_read(&dd->ipath_pkeyrefs[j]));
823 pd->port_pkeys[i] = 0;
826 u64 pkey = (u64) dd->ipath_pkeys[0] |
827 ((u64) dd->ipath_pkeys[1] << 16) |
828 ((u64) dd->ipath_pkeys[2] << 32) |
829 ((u64) dd->ipath_pkeys[3] << 48);
830 ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
831 "new pkey reg %llx\n", pd->port_port,
832 (unsigned long long) oldpkey,
833 (unsigned long long) pkey);
834 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
840 * Initialize the port data with the receive buffer sizes
841 * so this can be done while the master port is locked.
842 * Otherwise, there is a race with a slave opening the port
843 * and seeing these fields uninitialized.
845 static void init_user_egr_sizes(struct ipath_portdata *pd)
847 struct ipath_devdata *dd = pd->port_dd;
848 unsigned egrperchunk, egrcnt, size;
851 * to avoid wasting a lot of memory, we allocate 32KB chunks of
852 * physically contiguous memory, advance through it until used up
853 * and then allocate more. Of course, we need memory to store those
854 * extra pointers, now. Started out with 256KB, but under heavy
855 * memory pressure (creating large files and then copying them over
856 * NFS while doing lots of MPI jobs), we hit some allocation
857 * failures, even though we can sleep... (2.6.10) Still get
858 * failures at 64K. 32K is the lowest we can go without wasting
862 egrperchunk = size / dd->ipath_rcvegrbufsize;
863 egrcnt = dd->ipath_rcvegrcnt;
864 pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
865 pd->port_rcvegrbufs_perchunk = egrperchunk;
866 pd->port_rcvegrbuf_size = size;
870 * ipath_create_user_egr - allocate eager TID buffers
871 * @pd: the port to allocate TID buffers for
873 * This routine is now quite different for user and kernel, because
874 * the kernel uses skb's, for the accelerated network performance
875 * This is the user port version
877 * Allocate the eager TID buffers and program them into infinipath
878 * They are no longer completely contiguous, we do multiple allocation
881 static int ipath_create_user_egr(struct ipath_portdata *pd)
883 struct ipath_devdata *dd = pd->port_dd;
884 unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
890 * GFP_USER, but without GFP_FS, so buffer cache can be
891 * coalesced (we hope); otherwise, even at order 4,
892 * heavy filesystem activity makes these fail, and we can
893 * use compound pages.
895 gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
897 egrcnt = dd->ipath_rcvegrcnt;
898 /* TID number offset for this port */
899 egroff = (pd->port_port - 1) * egrcnt + dd->ipath_p0_rcvegrcnt;
900 egrsize = dd->ipath_rcvegrbufsize;
901 ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
902 "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
904 chunk = pd->port_rcvegrbuf_chunks;
905 egrperchunk = pd->port_rcvegrbufs_perchunk;
906 size = pd->port_rcvegrbuf_size;
907 pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
909 if (!pd->port_rcvegrbuf) {
913 pd->port_rcvegrbuf_phys =
914 kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
916 if (!pd->port_rcvegrbuf_phys) {
920 for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
922 pd->port_rcvegrbuf[e] = dma_alloc_coherent(
923 &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
926 if (!pd->port_rcvegrbuf[e]) {
928 goto bail_rcvegrbuf_phys;
932 pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
934 for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
935 dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
938 for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
939 dd->ipath_f_put_tid(dd, e + egroff +
943 dd->ipath_rcvegrbase),
944 RCVHQ_RCV_TYPE_EAGER, pa);
947 cond_resched(); /* don't hog the cpu */
954 for (e = 0; e < pd->port_rcvegrbuf_chunks &&
955 pd->port_rcvegrbuf[e]; e++) {
956 dma_free_coherent(&dd->pcidev->dev, size,
957 pd->port_rcvegrbuf[e],
958 pd->port_rcvegrbuf_phys[e]);
961 kfree(pd->port_rcvegrbuf_phys);
962 pd->port_rcvegrbuf_phys = NULL;
964 kfree(pd->port_rcvegrbuf);
965 pd->port_rcvegrbuf = NULL;
971 /* common code for the mappings on dma_alloc_coherent mem */
972 static int ipath_mmap_mem(struct vm_area_struct *vma,
973 struct ipath_portdata *pd, unsigned len, int write_ok,
974 void *kvaddr, char *what)
976 struct ipath_devdata *dd = pd->port_dd;
980 if ((vma->vm_end - vma->vm_start) > len) {
981 dev_info(&dd->pcidev->dev,
982 "FAIL on %s: len %lx > %x\n", what,
983 vma->vm_end - vma->vm_start, len);
989 if (vma->vm_flags & VM_WRITE) {
990 dev_info(&dd->pcidev->dev,
991 "%s must be mapped readonly\n", what);
996 /* don't allow them to later change with mprotect */
997 vma->vm_flags &= ~VM_MAYWRITE;
1000 pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
1001 ret = remap_pfn_range(vma, vma->vm_start, pfn,
1002 len, vma->vm_page_prot);
1004 dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
1005 "bytes r%c failed: %d\n", what, pd->port_port,
1006 pfn, len, write_ok?'w':'o', ret);
1008 ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
1009 "r%c\n", what, pd->port_port, pfn, len,
1015 static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
1022 * This is real hardware, so use io_remap. This is the mechanism
1023 * for the user process to update the head registers for their port
1026 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
1027 dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
1028 "%lx > PAGE\n", vma->vm_end - vma->vm_start);
1031 phys = dd->ipath_physaddr + ureg;
1032 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1034 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1035 ret = io_remap_pfn_range(vma, vma->vm_start,
1037 vma->vm_end - vma->vm_start,
1043 static int mmap_piobufs(struct vm_area_struct *vma,
1044 struct ipath_devdata *dd,
1045 struct ipath_portdata *pd,
1046 unsigned piobufs, unsigned piocnt)
1052 * When we map the PIO buffers in the chip, we want to map them as
1053 * writeonly, no read possible. This prevents access to previous
1054 * process data, and catches users who might try to read the i/o
1055 * space due to a bug.
1057 if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
1058 dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
1059 "reqlen %lx > PAGE\n",
1060 vma->vm_end - vma->vm_start);
1065 phys = dd->ipath_physaddr + piobufs;
1067 #if defined(__powerpc__)
1068 /* There isn't a generic way to specify writethrough mappings */
1069 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1070 pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
1071 pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
1075 * don't allow them to later change to readable with mprotect (for when
1076 * not initially mapped readable, as is normally the case)
1078 vma->vm_flags &= ~VM_MAYREAD;
1079 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1081 ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
1082 vma->vm_end - vma->vm_start,
1088 static int mmap_rcvegrbufs(struct vm_area_struct *vma,
1089 struct ipath_portdata *pd)
1091 struct ipath_devdata *dd = pd->port_dd;
1092 unsigned long start, size;
1093 size_t total_size, i;
1097 size = pd->port_rcvegrbuf_size;
1098 total_size = pd->port_rcvegrbuf_chunks * size;
1099 if ((vma->vm_end - vma->vm_start) > total_size) {
1100 dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
1101 "reqlen %lx > actual %lx\n",
1102 vma->vm_end - vma->vm_start,
1103 (unsigned long) total_size);
1108 if (vma->vm_flags & VM_WRITE) {
1109 dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
1110 "writable (flags=%lx)\n", vma->vm_flags);
1114 /* don't allow them to later change to writeable with mprotect */
1115 vma->vm_flags &= ~VM_MAYWRITE;
1117 start = vma->vm_start;
1119 for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
1120 pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
1121 ret = remap_pfn_range(vma, start, pfn, size,
1133 * ipath_file_vma_fault - handle a VMA page fault.
1135 static int ipath_file_vma_fault(struct vm_area_struct *vma,
1136 struct vm_fault *vmf)
1140 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
1142 return VM_FAULT_SIGBUS;
1149 static struct vm_operations_struct ipath_file_vm_ops = {
1150 .fault = ipath_file_vma_fault,
1153 static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
1154 struct ipath_portdata *pd, unsigned subport)
1157 struct ipath_devdata *dd;
1162 /* If the port is not shared, all addresses should be physical */
1163 if (!pd->port_subport_cnt)
1167 size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
1170 * Each process has all the subport uregbase, rcvhdrq, and
1171 * rcvegrbufs mmapped - as an array for all the processes,
1172 * and also separately for this process.
1174 if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
1175 addr = pd->subport_uregbase;
1176 size = PAGE_SIZE * pd->port_subport_cnt;
1177 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
1178 addr = pd->subport_rcvhdr_base;
1179 size = pd->port_rcvhdrq_size * pd->port_subport_cnt;
1180 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
1181 addr = pd->subport_rcvegrbuf;
1182 size *= pd->port_subport_cnt;
1183 } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
1184 PAGE_SIZE * subport)) {
1185 addr = pd->subport_uregbase + PAGE_SIZE * subport;
1187 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
1188 pd->port_rcvhdrq_size * subport)) {
1189 addr = pd->subport_rcvhdr_base +
1190 pd->port_rcvhdrq_size * subport;
1191 size = pd->port_rcvhdrq_size;
1192 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
1194 addr = pd->subport_rcvegrbuf + size * subport;
1195 /* rcvegrbufs are read-only on the slave */
1196 if (vma->vm_flags & VM_WRITE) {
1197 dev_info(&dd->pcidev->dev,
1198 "Can't map eager buffers as "
1199 "writable (flags=%lx)\n", vma->vm_flags);
1204 * Don't allow permission to later change to writeable
1207 vma->vm_flags &= ~VM_MAYWRITE;
1211 len = vma->vm_end - vma->vm_start;
1213 ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
1218 vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
1219 vma->vm_ops = &ipath_file_vm_ops;
1220 vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
1228 * ipath_mmap - mmap various structures into user space
1229 * @fp: the file pointer
1232 * We use this to have a shared buffer between the kernel and the user code
1233 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1234 * buffers in the chip. We have the open and close entries so we can bump
1235 * the ref count and keep the driver from being unloaded while still mapped.
1237 static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
1239 struct ipath_portdata *pd;
1240 struct ipath_devdata *dd;
1242 unsigned piobufs, piocnt;
1253 * This is the ipath_do_user_init() code, mapping the shared buffers
1254 * into the user process. The address referred to by vm_pgoff is the
1255 * file offset passed via mmap(). For shared ports, this is the
1256 * kernel vmalloc() address of the pages to share with the master.
1257 * For non-shared or master ports, this is a physical address.
1258 * We only do one mmap for each space mapped.
1260 pgaddr = vma->vm_pgoff << PAGE_SHIFT;
1263 * Check for 0 in case one of the allocations failed, but user
1264 * called mmap anyway.
1271 ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
1272 (unsigned long long) pgaddr, vma->vm_start,
1273 vma->vm_end - vma->vm_start, dd->ipath_unit,
1274 pd->port_port, subport_fp(fp));
1277 * Physical addresses must fit in 40 bits for our hardware.
1278 * Check for kernel virtual addresses first, anything else must
1279 * match a HW or memory address.
1281 ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
1288 ureg = dd->ipath_uregbase + dd->ipath_ureg_align * pd->port_port;
1289 if (!pd->port_subport_cnt) {
1290 /* port is not shared */
1291 piocnt = pd->port_piocnt;
1292 piobufs = pd->port_piobufs;
1293 } else if (!subport_fp(fp)) {
1294 /* caller is the master */
1295 piocnt = (pd->port_piocnt / pd->port_subport_cnt) +
1296 (pd->port_piocnt % pd->port_subport_cnt);
1297 piobufs = pd->port_piobufs +
1298 dd->ipath_palign * (pd->port_piocnt - piocnt);
1300 unsigned slave = subport_fp(fp) - 1;
1302 /* caller is a slave */
1303 piocnt = pd->port_piocnt / pd->port_subport_cnt;
1304 piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
1308 ret = mmap_ureg(vma, dd, ureg);
1309 else if (pgaddr == piobufs)
1310 ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
1311 else if (pgaddr == dd->ipath_pioavailregs_phys)
1312 /* in-memory copy of pioavail registers */
1313 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1314 (void *) dd->ipath_pioavailregs_dma,
1315 "pioavail registers");
1316 else if (pgaddr == pd->port_rcvegr_phys)
1317 ret = mmap_rcvegrbufs(vma, pd);
1318 else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
1320 * The rcvhdrq itself; readonly except on HT (so have
1321 * to allow writable mapping), multiple pages, contiguous
1322 * from an i/o perspective.
1324 ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
1327 else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
1328 /* in-memory copy of rcvhdrq tail register */
1329 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1330 pd->port_rcvhdrtail_kvaddr,
1335 vma->vm_private_data = NULL;
1338 dev_info(&dd->pcidev->dev,
1339 "Failure %d on off %llx len %lx\n",
1340 -ret, (unsigned long long)pgaddr,
1341 vma->vm_end - vma->vm_start);
1346 static unsigned ipath_poll_hdrqfull(struct ipath_portdata *pd)
1348 unsigned pollflag = 0;
1350 if ((pd->poll_type & IPATH_POLL_TYPE_OVERFLOW) &&
1351 pd->port_hdrqfull != pd->port_hdrqfull_poll) {
1352 pollflag |= POLLIN | POLLRDNORM;
1353 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1359 static unsigned int ipath_poll_urgent(struct ipath_portdata *pd,
1361 struct poll_table_struct *pt)
1363 unsigned pollflag = 0;
1364 struct ipath_devdata *dd;
1368 /* variable access in ipath_poll_hdrqfull() needs this */
1370 pollflag = ipath_poll_hdrqfull(pd);
1372 if (pd->port_urgent != pd->port_urgent_poll) {
1373 pollflag |= POLLIN | POLLRDNORM;
1374 pd->port_urgent_poll = pd->port_urgent;
1378 /* this saves a spin_lock/unlock in interrupt handler... */
1379 set_bit(IPATH_PORT_WAITING_URG, &pd->port_flag);
1380 /* flush waiting flag so don't miss an event... */
1382 poll_wait(fp, &pd->port_wait, pt);
1388 static unsigned int ipath_poll_next(struct ipath_portdata *pd,
1390 struct poll_table_struct *pt)
1394 unsigned pollflag = 0;
1395 struct ipath_devdata *dd;
1399 /* variable access in ipath_poll_hdrqfull() needs this */
1401 pollflag = ipath_poll_hdrqfull(pd);
1403 head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
1404 if (pd->port_rcvhdrtail_kvaddr)
1405 tail = ipath_get_rcvhdrtail(pd);
1407 tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
1410 pollflag |= POLLIN | POLLRDNORM;
1412 /* this saves a spin_lock/unlock in interrupt handler */
1413 set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
1414 /* flush waiting flag so we don't miss an event */
1417 set_bit(pd->port_port + dd->ipath_r_intravail_shift,
1418 &dd->ipath_rcvctrl);
1420 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1423 if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
1424 ipath_write_ureg(dd, ur_rcvhdrhead,
1425 dd->ipath_rhdrhead_intr_off | head,
1428 poll_wait(fp, &pd->port_wait, pt);
1434 static unsigned int ipath_poll(struct file *fp,
1435 struct poll_table_struct *pt)
1437 struct ipath_portdata *pd;
1443 else if (pd->poll_type & IPATH_POLL_TYPE_URGENT)
1444 pollflag = ipath_poll_urgent(pd, fp, pt);
1446 pollflag = ipath_poll_next(pd, fp, pt);
1451 static int ipath_supports_subports(int user_swmajor, int user_swminor)
1453 /* no subport implementation prior to software version 1.3 */
1454 return (user_swmajor > 1) || (user_swminor >= 3);
1457 static int ipath_compatible_subports(int user_swmajor, int user_swminor)
1459 /* this code is written long-hand for clarity */
1460 if (IPATH_USER_SWMAJOR != user_swmajor) {
1461 /* no promise of compatibility if major mismatch */
1464 if (IPATH_USER_SWMAJOR == 1) {
1465 switch (IPATH_USER_SWMINOR) {
1469 /* no subport implementation so cannot be compatible */
1472 /* 3 is only compatible with itself */
1473 return user_swminor == 3;
1475 /* >= 4 are compatible (or are expected to be) */
1476 return user_swminor >= 4;
1479 /* make no promises yet for future major versions */
1483 static int init_subports(struct ipath_devdata *dd,
1484 struct ipath_portdata *pd,
1485 const struct ipath_user_info *uinfo)
1488 unsigned num_subports;
1492 * If the user is requesting zero subports,
1493 * skip the subport allocation.
1495 if (uinfo->spu_subport_cnt <= 0)
1498 /* Self-consistency check for ipath_compatible_subports() */
1499 if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&
1500 !ipath_compatible_subports(IPATH_USER_SWMAJOR,
1501 IPATH_USER_SWMINOR)) {
1502 dev_info(&dd->pcidev->dev,
1503 "Inconsistent ipath_compatible_subports()\n");
1507 /* Check for subport compatibility */
1508 if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,
1509 uinfo->spu_userversion & 0xffff)) {
1510 dev_info(&dd->pcidev->dev,
1511 "Mismatched user version (%d.%d) and driver "
1512 "version (%d.%d) while port sharing. Ensure "
1513 "that driver and library are from the same "
1515 (int) (uinfo->spu_userversion >> 16),
1516 (int) (uinfo->spu_userversion & 0xffff),
1518 IPATH_USER_SWMINOR);
1521 if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
1526 num_subports = uinfo->spu_subport_cnt;
1527 pd->subport_uregbase = vmalloc(PAGE_SIZE * num_subports);
1528 if (!pd->subport_uregbase) {
1532 /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
1533 size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
1534 sizeof(u32), PAGE_SIZE) * num_subports;
1535 pd->subport_rcvhdr_base = vmalloc(size);
1536 if (!pd->subport_rcvhdr_base) {
1541 pd->subport_rcvegrbuf = vmalloc(pd->port_rcvegrbuf_chunks *
1542 pd->port_rcvegrbuf_size *
1544 if (!pd->subport_rcvegrbuf) {
1549 pd->port_subport_cnt = uinfo->spu_subport_cnt;
1550 pd->port_subport_id = uinfo->spu_subport_id;
1551 pd->active_slaves = 1;
1552 set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1553 memset(pd->subport_uregbase, 0, PAGE_SIZE * num_subports);
1554 memset(pd->subport_rcvhdr_base, 0, size);
1555 memset(pd->subport_rcvegrbuf, 0, pd->port_rcvegrbuf_chunks *
1556 pd->port_rcvegrbuf_size *
1561 vfree(pd->subport_rcvhdr_base);
1563 vfree(pd->subport_uregbase);
1564 pd->subport_uregbase = NULL;
1569 static int try_alloc_port(struct ipath_devdata *dd, int port,
1571 const struct ipath_user_info *uinfo)
1573 struct ipath_portdata *pd;
1576 if (!(pd = dd->ipath_pd[port])) {
1579 pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
1582 * Allocate memory for use in ipath_tid_update() just once
1583 * at open, not per call. Reduces cost of expected send
1586 ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
1587 dd->ipath_rcvtidcnt * sizeof(struct page **),
1590 ipath_dev_err(dd, "Unable to allocate portdata "
1591 "memory, failing open\n");
1597 dd->ipath_pd[port] = pd;
1598 dd->ipath_pd[port]->port_port = port;
1599 dd->ipath_pd[port]->port_dd = dd;
1600 dd->ipath_pd[port]->port_tid_pg_list = ptmp;
1601 init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
1603 if (!pd->port_cnt) {
1604 pd->userversion = uinfo->spu_userversion;
1605 init_user_egr_sizes(pd);
1606 if ((ret = init_subports(dd, pd, uinfo)) != 0)
1608 ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
1609 current->comm, current->pid, dd->ipath_unit,
1613 pd->port_pid = get_pid(task_pid(current));
1614 strncpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
1615 ipath_stats.sps_ports++;
1624 static inline int usable(struct ipath_devdata *dd)
1627 (dd->ipath_flags & IPATH_PRESENT) &&
1628 dd->ipath_kregbase &&
1630 !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
1634 static int find_free_port(int unit, struct file *fp,
1635 const struct ipath_user_info *uinfo)
1637 struct ipath_devdata *dd = ipath_lookup(unit);
1650 for (i = 1; i < dd->ipath_cfgports; i++) {
1651 ret = try_alloc_port(dd, i, fp, uinfo);
1661 static int find_best_unit(struct file *fp,
1662 const struct ipath_user_info *uinfo)
1664 int ret = 0, i, prefunit = -1, devmax;
1665 int maxofallports, npresent, nup;
1668 devmax = ipath_count_units(&npresent, &nup, &maxofallports);
1671 * This code is present to allow a knowledgeable person to
1672 * specify the layout of processes to processors before opening
1673 * this driver, and then we'll assign the process to the "closest"
1674 * InfiniPath chip to that processor (we assume reasonable connectivity,
1675 * for now). This code assumes that if affinity has been set
1676 * before this point, that at most one cpu is set; for now this
1677 * is reasonable. I check for both cpus_empty() and cpus_full(),
1678 * in case some kernel variant sets none of the bits when no
1679 * affinity is set. 2.6.11 and 12 kernels have all present
1680 * cpus set. Some day we'll have to fix it up further to handle
1681 * a cpu subset. This algorithm fails for two HT chips connected
1682 * in tunnel fashion. Eventually this needs real topology
1683 * information. There may be some issues with dual core numbering
1684 * as well. This needs more work prior to release.
1686 if (!cpus_empty(current->cpus_allowed) &&
1687 !cpus_full(current->cpus_allowed)) {
1688 int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
1689 for (i = 0; i < ncpus; i++)
1690 if (cpu_isset(i, current->cpus_allowed)) {
1691 ipath_cdbg(PROC, "%s[%u] affinity set for "
1692 "cpu %d/%d\n", current->comm,
1693 current->pid, i, ncpus);
1697 if (curcpu != -1 && nset != ncpus) {
1699 prefunit = curcpu / (ncpus / npresent);
1700 ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
1701 "%d cpus/chip, select unit %d\n",
1702 current->comm, current->pid,
1703 npresent, ncpus, ncpus / npresent,
1710 * user ports start at 1, kernel port is 0
1711 * For now, we do round-robin access across all chips
1715 devmax = prefunit + 1;
1717 for (i = 1; i < maxofallports; i++) {
1718 for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
1720 struct ipath_devdata *dd = ipath_lookup(ndev);
1723 continue; /* can't use this unit */
1724 if (i >= dd->ipath_cfgports)
1726 * Maxed out on users of this unit. Try
1730 ret = try_alloc_port(dd, i, fp, uinfo);
1739 ipath_dbg("No ports available (none initialized "
1743 /* if started above 0, retry from 0 */
1745 "%s[%u] no ports on prefunit "
1746 "%d, clear and re-check\n",
1747 current->comm, current->pid,
1749 devmax = ipath_count_units(NULL, NULL,
1755 ipath_dbg("No ports available\n");
1759 ipath_dbg("No boards found\n");
1766 static int find_shared_port(struct file *fp,
1767 const struct ipath_user_info *uinfo)
1769 int devmax, ndev, i;
1772 devmax = ipath_count_units(NULL, NULL, NULL);
1774 for (ndev = 0; ndev < devmax; ndev++) {
1775 struct ipath_devdata *dd = ipath_lookup(ndev);
1779 for (i = 1; i < dd->ipath_cfgports; i++) {
1780 struct ipath_portdata *pd = dd->ipath_pd[i];
1782 /* Skip ports which are not yet open */
1783 if (!pd || !pd->port_cnt)
1785 /* Skip port if it doesn't match the requested one */
1786 if (pd->port_subport_id != uinfo->spu_subport_id)
1788 /* Verify the sharing process matches the master */
1789 if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
1790 pd->userversion != uinfo->spu_userversion ||
1791 pd->port_cnt >= pd->port_subport_cnt) {
1796 subport_fp(fp) = pd->port_cnt++;
1797 pd->port_subpid[subport_fp(fp)] =
1798 get_pid(task_pid(current));
1799 tidcursor_fp(fp) = 0;
1800 pd->active_slaves |= 1 << subport_fp(fp);
1802 "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
1803 current->comm, current->pid,
1805 pd->port_comm, pid_nr(pd->port_pid),
1806 dd->ipath_unit, pd->port_port);
1816 static int ipath_open(struct inode *in, struct file *fp)
1818 /* The real work is performed later in ipath_assign_port() */
1819 cycle_kernel_lock();
1820 fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
1821 return fp->private_data ? 0 : -ENOMEM;
1824 /* Get port early, so can set affinity prior to memory allocation */
1825 static int ipath_assign_port(struct file *fp,
1826 const struct ipath_user_info *uinfo)
1830 unsigned swmajor, swminor;
1832 /* Check to be sure we haven't already initialized this file */
1838 /* for now, if major version is different, bail */
1839 swmajor = uinfo->spu_userversion >> 16;
1840 if (swmajor != IPATH_USER_SWMAJOR) {
1841 ipath_dbg("User major version %d not same as driver "
1842 "major %d\n", uinfo->spu_userversion >> 16,
1843 IPATH_USER_SWMAJOR);
1848 swminor = uinfo->spu_userversion & 0xffff;
1849 if (swminor != IPATH_USER_SWMINOR)
1850 ipath_dbg("User minor version %d not same as driver "
1851 "minor %d\n", swminor, IPATH_USER_SWMINOR);
1853 mutex_lock(&ipath_mutex);
1855 if (ipath_compatible_subports(swmajor, swminor) &&
1856 uinfo->spu_subport_cnt &&
1857 (ret = find_shared_port(fp, uinfo))) {
1863 i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;
1864 ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
1865 (long)fp->f_path.dentry->d_inode->i_rdev, i_minor);
1868 ret = find_free_port(i_minor - 1, fp, uinfo);
1870 ret = find_best_unit(fp, uinfo);
1874 struct ipath_filedata *fd = fp->private_data;
1875 const struct ipath_portdata *pd = fd->pd;
1876 const struct ipath_devdata *dd = pd->port_dd;
1878 fd->pq = ipath_user_sdma_queue_create(&dd->pcidev->dev,
1887 mutex_unlock(&ipath_mutex);
1894 static int ipath_do_user_init(struct file *fp,
1895 const struct ipath_user_info *uinfo)
1898 struct ipath_portdata *pd = port_fp(fp);
1899 struct ipath_devdata *dd;
1902 /* Subports don't need to initialize anything since master did it. */
1903 if (subport_fp(fp)) {
1904 ret = wait_event_interruptible(pd->port_wait,
1905 !test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
1911 if (uinfo->spu_rcvhdrsize) {
1912 ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
1917 /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
1919 /* some ports may get extra buffers, calculate that here */
1920 if (pd->port_port <= dd->ipath_ports_extrabuf)
1921 pd->port_piocnt = dd->ipath_pbufsport + 1;
1923 pd->port_piocnt = dd->ipath_pbufsport;
1925 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
1926 if (pd->port_port <= dd->ipath_ports_extrabuf)
1927 pd->port_pio_base = (dd->ipath_pbufsport + 1)
1928 * (pd->port_port - 1);
1930 pd->port_pio_base = dd->ipath_ports_extrabuf +
1931 dd->ipath_pbufsport * (pd->port_port - 1);
1932 pd->port_piobufs = dd->ipath_piobufbase +
1933 pd->port_pio_base * dd->ipath_palign;
1934 ipath_cdbg(VERBOSE, "piobuf base for port %u is 0x%x, piocnt %u,"
1935 " first pio %u\n", pd->port_port, pd->port_piobufs,
1936 pd->port_piocnt, pd->port_pio_base);
1937 ipath_chg_pioavailkernel(dd, pd->port_pio_base, pd->port_piocnt, 0);
1940 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
1941 * array for time being. If pd->port_port > chip-supported,
1942 * we need to do extra stuff here to handle by handling overflow
1943 * through port 0, someday
1945 ret = ipath_create_rcvhdrq(dd, pd);
1947 ret = ipath_create_user_egr(pd);
1952 * set the eager head register for this port to the current values
1953 * of the tail pointers, since we don't know if they were
1954 * updated on last use of the port.
1956 head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
1957 ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
1958 pd->port_lastrcvhdrqtail = -1;
1959 ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
1960 pd->port_port, head32);
1961 pd->port_tidcursor = 0; /* start at beginning after open */
1963 /* initialize poll variables... */
1964 pd->port_urgent = 0;
1965 pd->port_urgent_poll = 0;
1966 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1969 * Now enable the port for receive.
1970 * For chips that are set to DMA the tail register to memory
1971 * when they change (and when the update bit transitions from
1972 * 0 to 1. So for those chips, we turn it off and then back on.
1973 * This will (very briefly) affect any other open ports, but the
1974 * duration is very short, and therefore isn't an issue. We
1975 * explictly set the in-memory tail copy to 0 beforehand, so we
1976 * don't have to wait to be sure the DMA update has happened
1977 * (chip resets head/tail to 0 on transition to enable).
1979 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
1980 &dd->ipath_rcvctrl);
1981 if (!(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
1982 if (pd->port_rcvhdrtail_kvaddr)
1983 ipath_clear_rcvhdrtail(pd);
1984 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1986 ~(1ULL << dd->ipath_r_tailupd_shift));
1988 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1990 /* Notify any waiting slaves */
1991 if (pd->port_subport_cnt) {
1992 clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1993 wake_up(&pd->port_wait);
2000 * unlock_exptid - unlock any expected TID entries port still had in use
2003 * We don't actually update the chip here, because we do a bulk update
2004 * below, using ipath_f_clear_tids.
2006 static void unlock_expected_tids(struct ipath_portdata *pd)
2008 struct ipath_devdata *dd = pd->port_dd;
2009 int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
2010 int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
2012 ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
2014 for (i = port_tidbase; i < maxtid; i++) {
2015 struct page *ps = dd->ipath_pageshadow[i];
2020 dd->ipath_pageshadow[i] = NULL;
2021 pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
2022 PAGE_SIZE, PCI_DMA_FROMDEVICE);
2023 ipath_release_user_pages_on_close(&ps, 1);
2025 ipath_stats.sps_pageunlocks++;
2028 ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
2029 pd->port_port, cnt);
2031 if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
2032 ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
2033 (unsigned long long) ipath_stats.sps_pagelocks,
2034 (unsigned long long)
2035 ipath_stats.sps_pageunlocks);
2038 static int ipath_close(struct inode *in, struct file *fp)
2041 struct ipath_filedata *fd;
2042 struct ipath_portdata *pd;
2043 struct ipath_devdata *dd;
2046 ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
2047 (long)in->i_rdev, fp->private_data);
2049 mutex_lock(&ipath_mutex);
2051 fd = (struct ipath_filedata *) fp->private_data;
2052 fp->private_data = NULL;
2055 mutex_unlock(&ipath_mutex);
2061 /* drain user sdma queue */
2062 ipath_user_sdma_queue_drain(dd, fd->pq);
2063 ipath_user_sdma_queue_destroy(fd->pq);
2065 if (--pd->port_cnt) {
2067 * XXX If the master closes the port before the slave(s),
2068 * revoke the mmap for the eager receive queue so
2069 * the slave(s) don't wait for receive data forever.
2071 pd->active_slaves &= ~(1 << fd->subport);
2072 put_pid(pd->port_subpid[fd->subport]);
2073 pd->port_subpid[fd->subport] = NULL;
2074 mutex_unlock(&ipath_mutex);
2077 port = pd->port_port;
2079 if (pd->port_hdrqfull) {
2080 ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
2081 "during run\n", pd->port_comm, pid_nr(pd->port_pid),
2083 pd->port_hdrqfull = 0;
2086 if (pd->port_rcvwait_to || pd->port_piowait_to
2087 || pd->port_rcvnowait || pd->port_pionowait) {
2088 ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
2089 "%u rcv %u, pio already\n",
2090 pd->port_port, pd->port_rcvwait_to,
2091 pd->port_piowait_to, pd->port_rcvnowait,
2092 pd->port_pionowait);
2093 pd->port_rcvwait_to = pd->port_piowait_to =
2094 pd->port_rcvnowait = pd->port_pionowait = 0;
2096 if (pd->port_flag) {
2097 ipath_cdbg(PROC, "port %u port_flag set: 0x%lx\n",
2098 pd->port_port, pd->port_flag);
2102 if (dd->ipath_kregbase) {
2103 /* atomically clear receive enable port and intr avail. */
2104 clear_bit(dd->ipath_r_portenable_shift + port,
2105 &dd->ipath_rcvctrl);
2106 clear_bit(pd->port_port + dd->ipath_r_intravail_shift,
2107 &dd->ipath_rcvctrl);
2108 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
2110 /* and read back from chip to be sure that nothing
2111 * else is in flight when we do the rest */
2112 (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
2114 /* clean up the pkeys for this port user */
2115 ipath_clean_part_key(pd, dd);
2117 * be paranoid, and never write 0's to these, just use an
2118 * unused part of the port 0 tail page. Of course,
2119 * rcvhdraddr points to a large chunk of memory, so this
2120 * could still trash things, but at least it won't trash
2121 * page 0, and by disabling the port, it should stop "soon",
2122 * even if a packet or two is in already in flight after we
2123 * disabled the port.
2125 ipath_write_kreg_port(dd,
2126 dd->ipath_kregs->kr_rcvhdrtailaddr, port,
2127 dd->ipath_dummy_hdrq_phys);
2128 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
2129 pd->port_port, dd->ipath_dummy_hdrq_phys);
2131 ipath_disarm_piobufs(dd, pd->port_pio_base, pd->port_piocnt);
2132 ipath_chg_pioavailkernel(dd, pd->port_pio_base,
2133 pd->port_piocnt, 1);
2135 dd->ipath_f_clear_tids(dd, pd->port_port);
2137 if (dd->ipath_pageshadow)
2138 unlock_expected_tids(pd);
2139 ipath_stats.sps_ports--;
2140 ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
2141 pd->port_comm, pid_nr(pd->port_pid),
2142 dd->ipath_unit, port);
2145 put_pid(pd->port_pid);
2146 pd->port_pid = NULL;
2147 dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
2148 mutex_unlock(&ipath_mutex);
2149 ipath_free_pddata(dd, pd); /* after releasing the mutex */
2156 static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
2157 struct ipath_port_info __user *uinfo)
2159 struct ipath_port_info info;
2164 (void) ipath_count_units(NULL, &nup, NULL);
2165 info.num_active = nup;
2166 info.unit = pd->port_dd->ipath_unit;
2167 info.port = pd->port_port;
2168 info.subport = subport;
2169 /* Don't return new fields if old library opened the port. */
2170 if (ipath_supports_subports(pd->userversion >> 16,
2171 pd->userversion & 0xffff)) {
2172 /* Number of user ports available for this device. */
2173 info.num_ports = pd->port_dd->ipath_cfgports - 1;
2174 info.num_subports = pd->port_subport_cnt;
2177 sz = sizeof(info) - 2 * sizeof(u16);
2179 if (copy_to_user(uinfo, &info, sz)) {
2189 static int ipath_get_slave_info(struct ipath_portdata *pd,
2190 void __user *slave_mask_addr)
2194 if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
2199 static int ipath_sdma_get_inflight(struct ipath_user_sdma_queue *pq,
2200 u32 __user *inflightp)
2202 const u32 val = ipath_user_sdma_inflight_counter(pq);
2204 if (put_user(val, inflightp))
2210 static int ipath_sdma_get_complete(struct ipath_devdata *dd,
2211 struct ipath_user_sdma_queue *pq,
2212 u32 __user *completep)
2217 err = ipath_user_sdma_make_progress(dd, pq);
2221 val = ipath_user_sdma_complete_counter(pq);
2222 if (put_user(val, completep))
2228 static ssize_t ipath_write(struct file *fp, const char __user *data,
2229 size_t count, loff_t *off)
2231 const struct ipath_cmd __user *ucmd;
2232 struct ipath_portdata *pd;
2233 const void __user *src;
2234 size_t consumed, copy;
2235 struct ipath_cmd cmd;
2239 if (count < sizeof(cmd.type)) {
2244 ucmd = (const struct ipath_cmd __user *) data;
2246 if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
2251 consumed = sizeof(cmd.type);
2254 case IPATH_CMD_ASSIGN_PORT:
2255 case __IPATH_CMD_USER_INIT:
2256 case IPATH_CMD_USER_INIT:
2257 copy = sizeof(cmd.cmd.user_info);
2258 dest = &cmd.cmd.user_info;
2259 src = &ucmd->cmd.user_info;
2261 case IPATH_CMD_RECV_CTRL:
2262 copy = sizeof(cmd.cmd.recv_ctrl);
2263 dest = &cmd.cmd.recv_ctrl;
2264 src = &ucmd->cmd.recv_ctrl;
2266 case IPATH_CMD_PORT_INFO:
2267 copy = sizeof(cmd.cmd.port_info);
2268 dest = &cmd.cmd.port_info;
2269 src = &ucmd->cmd.port_info;
2271 case IPATH_CMD_TID_UPDATE:
2272 case IPATH_CMD_TID_FREE:
2273 copy = sizeof(cmd.cmd.tid_info);
2274 dest = &cmd.cmd.tid_info;
2275 src = &ucmd->cmd.tid_info;
2277 case IPATH_CMD_SET_PART_KEY:
2278 copy = sizeof(cmd.cmd.part_key);
2279 dest = &cmd.cmd.part_key;
2280 src = &ucmd->cmd.part_key;
2282 case __IPATH_CMD_SLAVE_INFO:
2283 copy = sizeof(cmd.cmd.slave_mask_addr);
2284 dest = &cmd.cmd.slave_mask_addr;
2285 src = &ucmd->cmd.slave_mask_addr;
2287 case IPATH_CMD_PIOAVAILUPD: // force an update of PIOAvail reg
2292 case IPATH_CMD_POLL_TYPE:
2293 copy = sizeof(cmd.cmd.poll_type);
2294 dest = &cmd.cmd.poll_type;
2295 src = &ucmd->cmd.poll_type;
2297 case IPATH_CMD_ARMLAUNCH_CTRL:
2298 copy = sizeof(cmd.cmd.armlaunch_ctrl);
2299 dest = &cmd.cmd.armlaunch_ctrl;
2300 src = &ucmd->cmd.armlaunch_ctrl;
2302 case IPATH_CMD_SDMA_INFLIGHT:
2303 copy = sizeof(cmd.cmd.sdma_inflight);
2304 dest = &cmd.cmd.sdma_inflight;
2305 src = &ucmd->cmd.sdma_inflight;
2307 case IPATH_CMD_SDMA_COMPLETE:
2308 copy = sizeof(cmd.cmd.sdma_complete);
2309 dest = &cmd.cmd.sdma_complete;
2310 src = &ucmd->cmd.sdma_complete;
2318 if ((count - consumed) < copy) {
2323 if (copy_from_user(dest, src, copy)) {
2332 if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
2333 cmd.type != IPATH_CMD_ASSIGN_PORT) {
2339 case IPATH_CMD_ASSIGN_PORT:
2340 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2344 case __IPATH_CMD_USER_INIT:
2345 /* backwards compatibility, get port first */
2346 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2349 /* and fall through to current version. */
2350 case IPATH_CMD_USER_INIT:
2351 ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
2354 ret = ipath_get_base_info(
2355 fp, (void __user *) (unsigned long)
2356 cmd.cmd.user_info.spu_base_info,
2357 cmd.cmd.user_info.spu_base_info_size);
2359 case IPATH_CMD_RECV_CTRL:
2360 ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
2362 case IPATH_CMD_PORT_INFO:
2363 ret = ipath_port_info(pd, subport_fp(fp),
2364 (struct ipath_port_info __user *)
2365 (unsigned long) cmd.cmd.port_info);
2367 case IPATH_CMD_TID_UPDATE:
2368 ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
2370 case IPATH_CMD_TID_FREE:
2371 ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
2373 case IPATH_CMD_SET_PART_KEY:
2374 ret = ipath_set_part_key(pd, cmd.cmd.part_key);
2376 case __IPATH_CMD_SLAVE_INFO:
2377 ret = ipath_get_slave_info(pd,
2378 (void __user *) (unsigned long)
2379 cmd.cmd.slave_mask_addr);
2381 case IPATH_CMD_PIOAVAILUPD:
2382 ipath_force_pio_avail_update(pd->port_dd);
2384 case IPATH_CMD_POLL_TYPE:
2385 pd->poll_type = cmd.cmd.poll_type;
2387 case IPATH_CMD_ARMLAUNCH_CTRL:
2388 if (cmd.cmd.armlaunch_ctrl)
2389 ipath_enable_armlaunch(pd->port_dd);
2391 ipath_disable_armlaunch(pd->port_dd);
2393 case IPATH_CMD_SDMA_INFLIGHT:
2394 ret = ipath_sdma_get_inflight(user_sdma_queue_fp(fp),
2395 (u32 __user *) (unsigned long)
2396 cmd.cmd.sdma_inflight);
2398 case IPATH_CMD_SDMA_COMPLETE:
2399 ret = ipath_sdma_get_complete(pd->port_dd,
2400 user_sdma_queue_fp(fp),
2401 (u32 __user *) (unsigned long)
2402 cmd.cmd.sdma_complete);
2413 static ssize_t ipath_writev(struct kiocb *iocb, const struct iovec *iov,
2414 unsigned long dim, loff_t off)
2416 struct file *filp = iocb->ki_filp;
2417 struct ipath_filedata *fp = filp->private_data;
2418 struct ipath_portdata *pd = port_fp(filp);
2419 struct ipath_user_sdma_queue *pq = fp->pq;
2424 return ipath_user_sdma_writev(pd->port_dd, pq, iov, dim);
2427 static struct class *ipath_class;
2429 static int init_cdev(int minor, char *name, const struct file_operations *fops,
2430 struct cdev **cdevp, struct device **devp)
2432 const dev_t dev = MKDEV(IPATH_MAJOR, minor);
2433 struct cdev *cdev = NULL;
2434 struct device *device = NULL;
2437 cdev = cdev_alloc();
2439 printk(KERN_ERR IPATH_DRV_NAME
2440 ": Could not allocate cdev for minor %d, %s\n",
2446 cdev->owner = THIS_MODULE;
2448 kobject_set_name(&cdev->kobj, name);
2450 ret = cdev_add(cdev, dev, 1);
2452 printk(KERN_ERR IPATH_DRV_NAME
2453 ": Could not add cdev for minor %d, %s (err %d)\n",
2458 device = device_create(ipath_class, NULL, dev, name);
2460 if (IS_ERR(device)) {
2461 ret = PTR_ERR(device);
2462 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2463 "device for minor %d, %s (err %d)\n",
2486 int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
2487 struct cdev **cdevp, struct device **devp)
2489 return init_cdev(minor, name, fops, cdevp, devp);
2492 static void cleanup_cdev(struct cdev **cdevp,
2493 struct device **devp)
2495 struct device *dev = *devp;
2498 device_unregister(dev);
2508 void ipath_cdev_cleanup(struct cdev **cdevp,
2509 struct device **devp)
2511 cleanup_cdev(cdevp, devp);
2514 static struct cdev *wildcard_cdev;
2515 static struct device *wildcard_dev;
2517 static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
2519 static int user_init(void)
2523 ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
2525 printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
2526 "chrdev region (err %d)\n", -ret);
2530 ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
2532 if (IS_ERR(ipath_class)) {
2533 ret = PTR_ERR(ipath_class);
2534 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2535 "device class (err %d)\n", -ret);
2541 unregister_chrdev_region(dev, IPATH_NMINORS);
2546 static void user_cleanup(void)
2549 class_destroy(ipath_class);
2553 unregister_chrdev_region(dev, IPATH_NMINORS);
2556 static atomic_t user_count = ATOMIC_INIT(0);
2557 static atomic_t user_setup = ATOMIC_INIT(0);
2559 int ipath_user_add(struct ipath_devdata *dd)
2564 if (atomic_inc_return(&user_count) == 1) {
2567 ipath_dev_err(dd, "Unable to set up user support: "
2568 "error %d\n", -ret);
2571 ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
2574 ipath_dev_err(dd, "Could not create wildcard "
2575 "minor: error %d\n", -ret);
2579 atomic_set(&user_setup, 1);
2582 snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
2584 ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
2585 &dd->user_cdev, &dd->user_dev);
2587 ipath_dev_err(dd, "Could not create user minor %d, %s\n",
2588 dd->ipath_unit + 1, name);
2598 void ipath_user_remove(struct ipath_devdata *dd)
2600 cleanup_cdev(&dd->user_cdev, &dd->user_dev);
2602 if (atomic_dec_return(&user_count) == 0) {
2603 if (atomic_read(&user_setup) == 0)
2606 cleanup_cdev(&wildcard_cdev, &wildcard_dev);
2609 atomic_set(&user_setup, 0);