2 * Copyright (c) 2006 QLogic, Inc. 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 <asm/pgtable.h>
41 #include "ipath_kernel.h"
42 #include "ipath_common.h"
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,
48 static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
49 static int ipath_mmap(struct file *, struct vm_area_struct *);
51 static struct file_operations ipath_file_ops = {
55 .release = ipath_close,
60 static int ipath_get_base_info(struct ipath_portdata *pd,
61 void __user *ubase, size_t ubase_size)
64 struct ipath_base_info *kinfo = NULL;
65 struct ipath_devdata *dd = pd->port_dd;
67 if (ubase_size < sizeof(*kinfo)) {
69 "Base size %lu, need %lu (version mismatch?)\n",
70 (unsigned long) ubase_size,
71 (unsigned long) sizeof(*kinfo));
76 kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
82 ret = dd->ipath_f_get_base_info(pd, kinfo);
86 kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
87 kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
88 kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
89 kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
91 * have to mmap whole thing
93 kinfo->spi_rcv_egrbuftotlen =
94 pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
95 kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
96 kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
97 pd->port_rcvegrbuf_chunks;
98 kinfo->spi_tidcnt = dd->ipath_rcvtidcnt;
100 * for this use, may be ipath_cfgports summed over all chips that
101 * are are configured and present
103 kinfo->spi_nports = dd->ipath_cfgports;
104 /* unit (chip/board) our port is on */
105 kinfo->spi_unit = dd->ipath_unit;
106 /* for now, only a single page */
107 kinfo->spi_tid_maxsize = PAGE_SIZE;
110 * Doing this per port, and based on the skip value, etc. This has
111 * to be the actual buffer size, since the protocol code treats it
114 * These have to be set to user addresses in the user code via mmap.
115 * These values are used on return to user code for the mmap target
116 * addresses only. For 32 bit, same 44 bit address problem, so use
117 * the physical address, not virtual. Before 2.6.11, using the
118 * page_address() macro worked, but in 2.6.11, even that returns the
119 * full 64 bit address (upper bits all 1's). So far, using the
120 * physical addresses (or chip offsets, for chip mapping) works, but
121 * no doubt some future kernel release will chang that, and we'll be
122 * on to yet another method of dealing with this
124 kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
125 kinfo->spi_rcvhdr_tailaddr = (u64)pd->port_rcvhdrqtailaddr_phys;
126 kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
127 kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
128 kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
129 (void *) dd->ipath_statusp -
130 (void *) dd->ipath_pioavailregs_dma;
131 kinfo->spi_piobufbase = (u64) pd->port_piobufs;
132 kinfo->__spi_uregbase =
133 dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
135 kinfo->spi_pioindex = dd->ipath_pbufsport * (pd->port_port - 1);
136 kinfo->spi_piocnt = dd->ipath_pbufsport;
137 kinfo->spi_pioalign = dd->ipath_palign;
139 kinfo->spi_qpair = IPATH_KD_QP;
140 kinfo->spi_piosize = dd->ipath_ibmaxlen;
141 kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */
142 kinfo->spi_port = pd->port_port;
143 kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
144 kinfo->spi_hw_version = dd->ipath_revision;
146 if (copy_to_user(ubase, kinfo, sizeof(*kinfo)))
155 * ipath_tid_update - update a port TID
157 * @ti: the TID information
159 * The new implementation as of Oct 2004 is that the driver assigns
160 * the tid and returns it to the caller. To make it easier to
161 * catch bugs, and to reduce search time, we keep a cursor for
162 * each port, walking the shadow tid array to find one that's not
165 * For now, if we can't allocate the full list, we fail, although
166 * in the long run, we'll allocate as many as we can, and the
167 * caller will deal with that by trying the remaining pages later.
168 * That means that when we fail, we have to mark the tids as not in
169 * use again, in our shadow copy.
171 * It's up to the caller to free the tids when they are done.
172 * We'll unlock the pages as they free them.
174 * Also, right now we are locking one page at a time, but since
175 * the intended use of this routine is for a single group of
176 * virtually contiguous pages, that should change to improve
179 static int ipath_tid_update(struct ipath_portdata *pd,
180 const struct ipath_tid_info *ti)
183 u32 tid, porttid, cnt, i, tidcnt;
185 struct ipath_devdata *dd = pd->port_dd;
188 u64 __iomem *tidbase;
189 unsigned long tidmap[8];
190 struct page **pagep = NULL;
192 if (!dd->ipath_pageshadow) {
199 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
200 (unsigned long long) ti->tidlist);
202 * Should we treat as success? likely a bug
207 tidcnt = dd->ipath_rcvtidcnt;
209 /* make sure it all fits in port_tid_pg_list */
210 dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
211 "TIDs, only trying max (%u)\n", cnt, tidcnt);
214 pagep = (struct page **)pd->port_tid_pg_list;
215 tidlist = (u16 *) (&pagep[cnt]);
217 memset(tidmap, 0, sizeof(tidmap));
218 tid = pd->port_tidcursor;
219 /* before decrement; chip actual # */
220 porttid = pd->port_port * tidcnt;
222 tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
223 dd->ipath_rcvtidbase +
224 porttid * sizeof(*tidbase));
226 ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
227 pd->port_port, cnt, tid, tidbase);
229 /* virtual address of first page in transfer */
230 vaddr = ti->tidvaddr;
231 if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
233 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
238 ret = ipath_get_user_pages(vaddr, cnt, pagep);
241 ipath_dbg("Failed to lock addr %p, %u pages "
242 "(already locked)\n",
243 (void *) vaddr, cnt);
245 * for now, continue, and see what happens but with
246 * the new implementation, this should never happen,
247 * unless perhaps the user has mpin'ed the pages
248 * themselves (something we need to test)
252 dev_info(&dd->pcidev->dev,
253 "Failed to lock addr %p, %u pages: "
254 "errno %d\n", (void *) vaddr, cnt, -ret);
258 for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
259 for (; ntids--; tid++) {
262 if (!dd->ipath_pageshadow[porttid + tid])
267 * oops, wrapped all the way through their TIDs,
268 * and didn't have enough free; see comments at
271 ipath_dbg("Not enough free TIDs for %u pages "
272 "(index %d), failing\n", cnt, i);
273 i--; /* last tidlist[i] not filled in */
278 ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
279 "vaddr %lx\n", i, tid, vaddr);
280 /* we "know" system pages and TID pages are same size */
281 dd->ipath_pageshadow[porttid + tid] = pagep[i];
283 * don't need atomic or it's overhead
285 __set_bit(tid, tidmap);
286 physaddr = page_to_phys(pagep[i]);
287 ipath_stats.sps_pagelocks++;
289 "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
290 tid, vaddr, (unsigned long long) physaddr,
292 dd->ipath_f_put_tid(dd, &tidbase[tid], 1, physaddr);
294 * don't check this tid in ipath_portshadow, since we
295 * just filled it in; start with the next one.
303 /* jump here if copy out of updated info failed... */
304 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
306 /* same code that's in ipath_free_tid() */
307 limit = sizeof(tidmap) * BITS_PER_BYTE;
309 /* just in case size changes in future */
311 tid = find_first_bit((const unsigned long *)tidmap, limit);
312 for (; tid < limit; tid++) {
313 if (!test_bit(tid, tidmap))
315 if (dd->ipath_pageshadow[porttid + tid]) {
316 ipath_cdbg(VERBOSE, "Freeing TID %u\n",
318 dd->ipath_f_put_tid(dd, &tidbase[tid], 1,
319 dd->ipath_tidinvalid);
320 dd->ipath_pageshadow[porttid + tid] = NULL;
321 ipath_stats.sps_pageunlocks++;
324 ipath_release_user_pages(pagep, cnt);
327 * Copy the updated array, with ipath_tid's filled in, back
328 * to user. Since we did the copy in already, this "should
329 * never fail" If it does, we have to clean up...
331 if (copy_to_user((void __user *)
332 (unsigned long) ti->tidlist,
333 tidlist, cnt * sizeof(*tidlist))) {
337 if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
338 tidmap, sizeof tidmap)) {
344 pd->port_tidcursor = tid;
349 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
355 * ipath_tid_free - free a port TID
359 * right now we are unlocking one page at a time, but since
360 * the intended use of this routine is for a single group of
361 * virtually contiguous pages, that should change to improve
362 * performance. We check that the TID is in range for this port
363 * but otherwise don't check validity; if user has an error and
364 * frees the wrong tid, it's only their own data that can thereby
365 * be corrupted. We do check that the TID was in use, for sanity
366 * We always use our idea of the saved address, not the address that
367 * they pass in to us.
370 static int ipath_tid_free(struct ipath_portdata *pd,
371 const struct ipath_tid_info *ti)
374 u32 tid, porttid, cnt, limit, tidcnt;
375 struct ipath_devdata *dd = pd->port_dd;
376 u64 __iomem *tidbase;
377 unsigned long tidmap[8];
379 if (!dd->ipath_pageshadow) {
384 if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
390 porttid = pd->port_port * dd->ipath_rcvtidcnt;
391 tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
392 dd->ipath_rcvtidbase +
393 porttid * sizeof(*tidbase));
395 tidcnt = dd->ipath_rcvtidcnt;
396 limit = sizeof(tidmap) * BITS_PER_BYTE;
398 /* just in case size changes in future */
400 tid = find_first_bit(tidmap, limit);
401 ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
402 "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
403 limit, tid, porttid);
404 for (cnt = 0; tid < limit; tid++) {
406 * small optimization; if we detect a run of 3 or so without
407 * any set, use find_first_bit again. That's mainly to
408 * accelerate the case where we wrapped, so we have some at
409 * the beginning, and some at the end, and a big gap
412 if (!test_bit(tid, tidmap))
415 if (dd->ipath_pageshadow[porttid + tid]) {
416 ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
418 dd->ipath_f_put_tid(dd, &tidbase[tid], 1,
419 dd->ipath_tidinvalid);
420 ipath_release_user_pages(
421 &dd->ipath_pageshadow[porttid + tid], 1);
422 dd->ipath_pageshadow[porttid + tid] = NULL;
423 ipath_stats.sps_pageunlocks++;
425 ipath_dbg("Unused tid %u, ignoring\n", tid);
427 if (cnt != ti->tidcnt)
428 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
432 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
438 * ipath_set_part_key - set a partition key
442 * We can have up to 4 active at a time (other than the default, which is
443 * always allowed). This is somewhat tricky, since multiple ports may set
444 * the same key, so we reference count them, and clean up at exit. All 4
445 * partition keys are packed into a single infinipath register. It's an
446 * error for a process to set the same pkey multiple times. We provide no
447 * mechanism to de-allocate a pkey at this time, we may eventually need to
448 * do that. I've used the atomic operations, and no locking, and only make
449 * a single pass through what's available. This should be more than
450 * adequate for some time. I'll think about spinlocks or the like if and as
453 static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
455 struct ipath_devdata *dd = pd->port_dd;
456 int i, any = 0, pidx = -1;
457 u16 lkey = key & 0x7FFF;
460 if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
461 /* nothing to do; this key always valid */
466 ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
467 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
468 pd->port_port, key, dd->ipath_pkeys[0],
469 atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
470 atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
471 atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
472 atomic_read(&dd->ipath_pkeyrefs[3]));
475 ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
482 * Set the full membership bit, because it has to be
483 * set in the register or the packet, and it seems
484 * cleaner to set in the register than to force all
485 * callers to set it. (see bug 4331)
489 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
490 if (!pd->port_pkeys[i] && pidx == -1)
492 if (pd->port_pkeys[i] == key) {
493 ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
494 "(%x) more than once\n",
501 ipath_dbg("All pkeys for port %u already in use, "
502 "can't set %x\n", pd->port_port, key);
506 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
507 if (!dd->ipath_pkeys[i]) {
511 if (dd->ipath_pkeys[i] == key) {
512 atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
514 if (atomic_inc_return(pkrefs) > 1) {
515 pd->port_pkeys[pidx] = key;
516 ipath_cdbg(VERBOSE, "p%u set key %x "
517 "matches #%d, count now %d\n",
518 pd->port_port, key, i,
519 atomic_read(pkrefs));
524 * lost race, decrement count, catch below
527 ipath_cdbg(VERBOSE, "Lost race, count was "
528 "0, after dec, it's %d\n",
529 atomic_read(pkrefs));
533 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
535 * It makes no sense to have both the limited and
536 * full membership PKEY set at the same time since
537 * the unlimited one will disable the limited one.
544 ipath_dbg("port %u, all pkeys already in use, "
545 "can't set %x\n", pd->port_port, key);
549 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
550 if (!dd->ipath_pkeys[i] &&
551 atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
554 /* for ipathstats, etc. */
555 ipath_stats.sps_pkeys[i] = lkey;
556 pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
558 (u64) dd->ipath_pkeys[0] |
559 ((u64) dd->ipath_pkeys[1] << 16) |
560 ((u64) dd->ipath_pkeys[2] << 32) |
561 ((u64) dd->ipath_pkeys[3] << 48);
562 ipath_cdbg(PROC, "p%u set key %x in #%d, "
563 "portidx %d, new pkey reg %llx\n",
564 pd->port_port, key, i, pidx,
565 (unsigned long long) pkey);
567 dd, dd->ipath_kregs->kr_partitionkey, pkey);
573 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
574 "can't set %x\n", pd->port_port, key);
582 * ipath_manage_rcvq - manage a port's receive queue
584 * @start_stop: action to carry out
586 * start_stop == 0 disables receive on the port, for use in queue
587 * overflow conditions. start_stop==1 re-enables, to be used to
588 * re-init the software copy of the head register
590 static int ipath_manage_rcvq(struct ipath_portdata *pd, int start_stop)
592 struct ipath_devdata *dd = pd->port_dd;
595 ipath_cdbg(PROC, "%sabling rcv for unit %u port %u\n",
596 start_stop ? "en" : "dis", dd->ipath_unit,
598 /* atomically clear receive enable port. */
601 * On enable, force in-memory copy of the tail register to
602 * 0, so that protocol code doesn't have to worry about
603 * whether or not the chip has yet updated the in-memory
604 * copy or not on return from the system call. The chip
605 * always resets it's tail register back to 0 on a
606 * transition from disabled to enabled. This could cause a
607 * problem if software was broken, and did the enable w/o
608 * the disable, but eventually the in-memory copy will be
609 * updated and correct itself, even in the face of software
612 *pd->port_rcvhdrtail_kvaddr = 0;
613 set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
616 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
618 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
620 /* now be sure chip saw it before we return */
621 tval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
624 * And try to be sure that tail reg update has happened too.
625 * This should in theory interlock with the RXE changes to
626 * the tail register. Don't assign it to the tail register
627 * in memory copy, since we could overwrite an update by the
630 tval = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
632 /* always; new head should be equal to new tail; see above */
636 static void ipath_clean_part_key(struct ipath_portdata *pd,
637 struct ipath_devdata *dd)
639 int i, j, pchanged = 0;
642 /* for debugging only */
643 oldpkey = (u64) dd->ipath_pkeys[0] |
644 ((u64) dd->ipath_pkeys[1] << 16) |
645 ((u64) dd->ipath_pkeys[2] << 32) |
646 ((u64) dd->ipath_pkeys[3] << 48);
648 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
649 if (!pd->port_pkeys[i])
651 ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
653 for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
654 /* check for match independent of the global bit */
655 if ((dd->ipath_pkeys[j] & 0x7fff) !=
656 (pd->port_pkeys[i] & 0x7fff))
658 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
659 ipath_cdbg(VERBOSE, "p%u clear key "
662 pd->port_pkeys[i], j);
663 ipath_stats.sps_pkeys[j] =
664 dd->ipath_pkeys[j] = 0;
668 VERBOSE, "p%u key %x matches #%d, "
669 "but ref still %d\n", pd->port_port,
670 pd->port_pkeys[i], j,
671 atomic_read(&dd->ipath_pkeyrefs[j]));
674 pd->port_pkeys[i] = 0;
677 u64 pkey = (u64) dd->ipath_pkeys[0] |
678 ((u64) dd->ipath_pkeys[1] << 16) |
679 ((u64) dd->ipath_pkeys[2] << 32) |
680 ((u64) dd->ipath_pkeys[3] << 48);
681 ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
682 "new pkey reg %llx\n", pd->port_port,
683 (unsigned long long) oldpkey,
684 (unsigned long long) pkey);
685 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
691 * ipath_create_user_egr - allocate eager TID buffers
692 * @pd: the port to allocate TID buffers for
694 * This routine is now quite different for user and kernel, because
695 * the kernel uses skb's, for the accelerated network performance
696 * This is the user port version
698 * Allocate the eager TID buffers and program them into infinipath
699 * They are no longer completely contiguous, we do multiple allocation
702 static int ipath_create_user_egr(struct ipath_portdata *pd)
704 struct ipath_devdata *dd = pd->port_dd;
705 unsigned e, egrcnt, alloced, egrperchunk, chunk, egrsize, egroff;
711 * GFP_USER, but without GFP_FS, so buffer cache can be
712 * coalesced (we hope); otherwise, even at order 4,
713 * heavy filesystem activity makes these fail, and we can
714 * use compound pages.
716 gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
718 egrcnt = dd->ipath_rcvegrcnt;
719 /* TID number offset for this port */
720 egroff = pd->port_port * egrcnt;
721 egrsize = dd->ipath_rcvegrbufsize;
722 ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
723 "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
726 * to avoid wasting a lot of memory, we allocate 32KB chunks of
727 * physically contiguous memory, advance through it until used up
728 * and then allocate more. Of course, we need memory to store those
729 * extra pointers, now. Started out with 256KB, but under heavy
730 * memory pressure (creating large files and then copying them over
731 * NFS while doing lots of MPI jobs), we hit some allocation
732 * failures, even though we can sleep... (2.6.10) Still get
733 * failures at 64K. 32K is the lowest we can go without wasting
737 alloced = ALIGN(egrsize * egrcnt, size);
738 egrperchunk = size / egrsize;
739 chunk = (egrcnt + egrperchunk - 1) / egrperchunk;
740 pd->port_rcvegrbuf_chunks = chunk;
741 pd->port_rcvegrbufs_perchunk = egrperchunk;
742 pd->port_rcvegrbuf_size = size;
743 pd->port_rcvegrbuf = vmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]));
744 if (!pd->port_rcvegrbuf) {
748 pd->port_rcvegrbuf_phys =
749 vmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]));
750 if (!pd->port_rcvegrbuf_phys) {
754 for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
756 pd->port_rcvegrbuf[e] = dma_alloc_coherent(
757 &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
760 if (!pd->port_rcvegrbuf[e]) {
762 goto bail_rcvegrbuf_phys;
766 pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
768 for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
769 dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
772 for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
773 dd->ipath_f_put_tid(dd, e + egroff +
777 dd->ipath_rcvegrbase), 0, pa);
780 cond_resched(); /* don't hog the cpu */
787 for (e = 0; e < pd->port_rcvegrbuf_chunks &&
788 pd->port_rcvegrbuf[e]; e++) {
789 dma_free_coherent(&dd->pcidev->dev, size,
790 pd->port_rcvegrbuf[e],
791 pd->port_rcvegrbuf_phys[e]);
794 vfree(pd->port_rcvegrbuf_phys);
795 pd->port_rcvegrbuf_phys = NULL;
797 vfree(pd->port_rcvegrbuf);
798 pd->port_rcvegrbuf = NULL;
803 static int ipath_do_user_init(struct ipath_portdata *pd,
804 const struct ipath_user_info *uinfo)
807 struct ipath_devdata *dd = pd->port_dd;
810 /* for now, if major version is different, bail */
811 if ((uinfo->spu_userversion >> 16) != IPATH_USER_SWMAJOR) {
812 dev_info(&dd->pcidev->dev,
813 "User major version %d not same as driver "
814 "major %d\n", uinfo->spu_userversion >> 16,
820 if ((uinfo->spu_userversion & 0xffff) != IPATH_USER_SWMINOR)
821 ipath_dbg("User minor version %d not same as driver "
822 "minor %d\n", uinfo->spu_userversion & 0xffff,
825 if (uinfo->spu_rcvhdrsize) {
826 ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
831 /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
833 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
834 pd->port_piobufs = dd->ipath_piobufbase +
835 dd->ipath_pbufsport * (pd->port_port -
836 1) * dd->ipath_palign;
837 ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n",
838 pd->port_port, pd->port_piobufs);
841 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
842 * array for time being. If pd->port_port > chip-supported,
843 * we need to do extra stuff here to handle by handling overflow
844 * through port 0, someday
846 ret = ipath_create_rcvhdrq(dd, pd);
848 ret = ipath_create_user_egr(pd);
853 * set the eager head register for this port to the current values
854 * of the tail pointers, since we don't know if they were
855 * updated on last use of the port.
857 head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
858 ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
859 dd->ipath_lastegrheads[pd->port_port] = -1;
860 dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
861 ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
862 pd->port_port, head32);
863 pd->port_tidcursor = 0; /* start at beginning after open */
865 * now enable the port; the tail registers will be written to memory
866 * by the chip as soon as it sees the write to
867 * dd->ipath_kregs->kr_rcvctrl. The update only happens on
868 * transition from 0 to 1, so clear it first, then set it as part of
869 * enabling the port. This will (very briefly) affect any other
870 * open ports, but it shouldn't be long enough to be an issue.
871 * We explictly set the in-memory copy to 0 beforehand, so we don't
872 * have to wait to be sure the DMA update has happened.
874 *pd->port_rcvhdrtail_kvaddr = 0ULL;
875 set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
877 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
878 dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD);
879 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
886 /* common code for the mappings on dma_alloc_coherent mem */
887 static int ipath_mmap_mem(struct vm_area_struct *vma,
888 struct ipath_portdata *pd, unsigned len,
889 int write_ok, dma_addr_t addr, char *what)
891 struct ipath_devdata *dd = pd->port_dd;
892 unsigned pfn = (unsigned long)addr >> PAGE_SHIFT;
895 if ((vma->vm_end - vma->vm_start) > len) {
896 dev_info(&dd->pcidev->dev,
897 "FAIL on %s: len %lx > %x\n", what,
898 vma->vm_end - vma->vm_start, len);
904 if (vma->vm_flags & VM_WRITE) {
905 dev_info(&dd->pcidev->dev,
906 "%s must be mapped readonly\n", what);
911 /* don't allow them to later change with mprotect */
912 vma->vm_flags &= ~VM_MAYWRITE;
915 ret = remap_pfn_range(vma, vma->vm_start, pfn,
916 len, vma->vm_page_prot);
918 dev_info(&dd->pcidev->dev,
919 "%s port%u mmap of %lx, %x bytes r%c failed: %d\n",
920 what, pd->port_port, (unsigned long)addr, len,
921 write_ok?'w':'o', ret);
923 ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes r%c\n",
924 what, pd->port_port, (unsigned long)addr, len,
930 static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
937 * This is real hardware, so use io_remap. This is the mechanism
938 * for the user process to update the head registers for their port
941 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
942 dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
943 "%lx > PAGE\n", vma->vm_end - vma->vm_start);
946 phys = dd->ipath_physaddr + ureg;
947 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
949 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
950 ret = io_remap_pfn_range(vma, vma->vm_start,
952 vma->vm_end - vma->vm_start,
958 static int mmap_piobufs(struct vm_area_struct *vma,
959 struct ipath_devdata *dd,
960 struct ipath_portdata *pd)
966 * When we map the PIO buffers in the chip, we want to map them as
967 * writeonly, no read possible. This prevents access to previous
968 * process data, and catches users who might try to read the i/o
969 * space due to a bug.
971 if ((vma->vm_end - vma->vm_start) >
972 (dd->ipath_pbufsport * dd->ipath_palign)) {
973 dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
974 "reqlen %lx > PAGE\n",
975 vma->vm_end - vma->vm_start);
980 phys = dd->ipath_physaddr + pd->port_piobufs;
983 * Don't mark this as non-cached, or we don't get the
984 * write combining behavior we want on the PIO buffers!
987 #if defined(__powerpc__)
988 /* There isn't a generic way to specify writethrough mappings */
989 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
990 pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
991 pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
995 * don't allow them to later change to readable with mprotect (for when
996 * not initially mapped readable, as is normally the case)
998 vma->vm_flags &= ~VM_MAYREAD;
999 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1001 ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
1002 vma->vm_end - vma->vm_start,
1008 static int mmap_rcvegrbufs(struct vm_area_struct *vma,
1009 struct ipath_portdata *pd)
1011 struct ipath_devdata *dd = pd->port_dd;
1012 unsigned long start, size;
1013 size_t total_size, i;
1017 size = pd->port_rcvegrbuf_size;
1018 total_size = pd->port_rcvegrbuf_chunks * size;
1019 if ((vma->vm_end - vma->vm_start) > total_size) {
1020 dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
1021 "reqlen %lx > actual %lx\n",
1022 vma->vm_end - vma->vm_start,
1023 (unsigned long) total_size);
1028 if (vma->vm_flags & VM_WRITE) {
1029 dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
1030 "writable (flags=%lx)\n", vma->vm_flags);
1034 /* don't allow them to later change to writeable with mprotect */
1035 vma->vm_flags &= ~VM_MAYWRITE;
1037 start = vma->vm_start;
1038 phys = pd->port_rcvegrbuf_phys;
1040 for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
1041 ret = remap_pfn_range(vma, start, phys[i] >> PAGE_SHIFT,
1042 size, vma->vm_page_prot);
1053 * ipath_mmap - mmap various structures into user space
1054 * @fp: the file pointer
1057 * We use this to have a shared buffer between the kernel and the user code
1058 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1059 * buffers in the chip. We have the open and close entries so we can bump
1060 * the ref count and keep the driver from being unloaded while still mapped.
1062 static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
1064 struct ipath_portdata *pd;
1065 struct ipath_devdata *dd;
1073 * This is the ipath_do_user_init() code, mapping the shared buffers
1074 * into the user process. The address referred to by vm_pgoff is the
1075 * virtual, not physical, address; we only do one mmap for each
1078 pgaddr = vma->vm_pgoff << PAGE_SHIFT;
1081 * Must fit in 40 bits for our hardware; some checked elsewhere,
1082 * but we'll be paranoid. Check for 0 is mostly in case one of the
1083 * allocations failed, but user called mmap anyway. We want to catch
1084 * that before it can match.
1086 if (!pgaddr || pgaddr >= (1ULL<<40)) {
1087 ipath_dev_err(dd, "Bad phys addr %llx, start %lx, end %lx\n",
1088 (unsigned long long)pgaddr, vma->vm_start, vma->vm_end);
1092 /* just the offset of the port user registers, not physical addr */
1093 ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
1095 ipath_cdbg(MM, "ushare: pgaddr %llx vm_start=%lx, vmlen %lx\n",
1096 (unsigned long long) pgaddr, vma->vm_start,
1097 vma->vm_end - vma->vm_start);
1099 if (vma->vm_start & (PAGE_SIZE-1)) {
1101 "vm_start not aligned: %lx, end=%lx phys %lx\n",
1102 vma->vm_start, vma->vm_end, (unsigned long)pgaddr);
1105 else if (pgaddr == ureg)
1106 ret = mmap_ureg(vma, dd, ureg);
1107 else if (pgaddr == pd->port_piobufs)
1108 ret = mmap_piobufs(vma, dd, pd);
1109 else if (pgaddr == (u64) pd->port_rcvegr_phys)
1110 ret = mmap_rcvegrbufs(vma, pd);
1111 else if (pgaddr == (u64) pd->port_rcvhdrq_phys) {
1113 * The rcvhdrq itself; readonly except on HT (so have
1114 * to allow writable mapping), multiple pages, contiguous
1115 * from an i/o perspective.
1117 unsigned total_size =
1118 ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize
1119 * sizeof(u32), PAGE_SIZE);
1120 ret = ipath_mmap_mem(vma, pd, total_size, 1,
1121 pd->port_rcvhdrq_phys,
1124 else if (pgaddr == (u64)pd->port_rcvhdrqtailaddr_phys)
1125 /* in-memory copy of rcvhdrq tail register */
1126 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1127 pd->port_rcvhdrqtailaddr_phys,
1129 else if (pgaddr == dd->ipath_pioavailregs_phys)
1130 /* in-memory copy of pioavail registers */
1131 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1132 dd->ipath_pioavailregs_phys,
1133 "pioavail registers");
1137 vma->vm_private_data = NULL;
1140 dev_info(&dd->pcidev->dev,
1141 "Failure %d on addr %lx, off %lx\n",
1142 -ret, vma->vm_start, vma->vm_pgoff);
1147 static unsigned int ipath_poll(struct file *fp,
1148 struct poll_table_struct *pt)
1150 struct ipath_portdata *pd;
1153 unsigned pollflag = 0;
1154 struct ipath_devdata *dd;
1159 bit = pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT;
1160 set_bit(bit, &dd->ipath_rcvctrl);
1163 * Before blocking, make sure that head is still == tail,
1164 * reading from the chip, so we can be sure the interrupt
1165 * enable has made it to the chip. If not equal, disable
1166 * interrupt again and return immediately. This avoids races,
1167 * and the overhead of the chip read doesn't matter much at
1168 * this point, since we are waiting for something anyway.
1171 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1174 head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
1175 tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
1178 set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
1179 if(dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
1180 (void)ipath_write_ureg(dd, ur_rcvhdrhead,
1181 dd->ipath_rhdrhead_intr_off
1182 | head, pd->port_port);
1183 poll_wait(fp, &pd->port_wait, pt);
1185 if (test_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag)) {
1186 /* timed out, no packets received */
1187 clear_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
1188 pd->port_rcvwait_to++;
1191 pollflag = POLLIN | POLLRDNORM;
1194 /* it's already happened; don't do wait_event overhead */
1195 pollflag = POLLIN | POLLRDNORM;
1196 pd->port_rcvnowait++;
1199 clear_bit(bit, &dd->ipath_rcvctrl);
1200 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1206 static int try_alloc_port(struct ipath_devdata *dd, int port,
1211 if (!dd->ipath_pd[port]) {
1214 p = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
1217 * Allocate memory for use in ipath_tid_update() just once
1218 * at open, not per call. Reduces cost of expected send
1221 ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
1222 dd->ipath_rcvtidcnt * sizeof(struct page **),
1225 ipath_dev_err(dd, "Unable to allocate portdata "
1226 "memory, failing open\n");
1232 dd->ipath_pd[port] = p;
1233 dd->ipath_pd[port]->port_port = port;
1234 dd->ipath_pd[port]->port_dd = dd;
1235 dd->ipath_pd[port]->port_tid_pg_list = ptmp;
1236 init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
1238 if (!dd->ipath_pd[port]->port_cnt) {
1239 dd->ipath_pd[port]->port_cnt = 1;
1240 fp->private_data = (void *) dd->ipath_pd[port];
1241 ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
1242 current->comm, current->pid, dd->ipath_unit,
1244 dd->ipath_pd[port]->port_pid = current->pid;
1245 strncpy(dd->ipath_pd[port]->port_comm, current->comm,
1246 sizeof(dd->ipath_pd[port]->port_comm));
1247 ipath_stats.sps_ports++;
1257 static inline int usable(struct ipath_devdata *dd)
1260 (dd->ipath_flags & IPATH_PRESENT) &&
1261 dd->ipath_kregbase &&
1263 !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
1267 static int find_free_port(int unit, struct file *fp)
1269 struct ipath_devdata *dd = ipath_lookup(unit);
1282 for (i = 0; i < dd->ipath_cfgports; i++) {
1283 ret = try_alloc_port(dd, i, fp);
1293 static int find_best_unit(struct file *fp)
1295 int ret = 0, i, prefunit = -1, devmax;
1296 int maxofallports, npresent, nup;
1299 (void) ipath_count_units(&npresent, &nup, &maxofallports);
1302 * This code is present to allow a knowledgeable person to
1303 * specify the layout of processes to processors before opening
1304 * this driver, and then we'll assign the process to the "closest"
1305 * InfiniPath chip to that processor (we assume reasonable connectivity,
1306 * for now). This code assumes that if affinity has been set
1307 * before this point, that at most one cpu is set; for now this
1308 * is reasonable. I check for both cpus_empty() and cpus_full(),
1309 * in case some kernel variant sets none of the bits when no
1310 * affinity is set. 2.6.11 and 12 kernels have all present
1311 * cpus set. Some day we'll have to fix it up further to handle
1312 * a cpu subset. This algorithm fails for two HT chips connected
1313 * in tunnel fashion. Eventually this needs real topology
1314 * information. There may be some issues with dual core numbering
1315 * as well. This needs more work prior to release.
1317 if (!cpus_empty(current->cpus_allowed) &&
1318 !cpus_full(current->cpus_allowed)) {
1319 int ncpus = num_online_cpus(), curcpu = -1;
1320 for (i = 0; i < ncpus; i++)
1321 if (cpu_isset(i, current->cpus_allowed)) {
1322 ipath_cdbg(PROC, "%s[%u] affinity set for "
1323 "cpu %d\n", current->comm,
1329 prefunit = curcpu / (ncpus / npresent);
1330 ipath_dbg("%s[%u] %d chips, %d cpus, "
1331 "%d cpus/chip, select unit %d\n",
1332 current->comm, current->pid,
1333 npresent, ncpus, ncpus / npresent,
1340 * user ports start at 1, kernel port is 0
1341 * For now, we do round-robin access across all chips
1345 devmax = prefunit + 1;
1347 devmax = ipath_count_units(NULL, NULL, NULL);
1349 for (i = 1; i < maxofallports; i++) {
1350 for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
1352 struct ipath_devdata *dd = ipath_lookup(ndev);
1355 continue; /* can't use this unit */
1356 if (i >= dd->ipath_cfgports)
1358 * Maxed out on users of this unit. Try
1362 ret = try_alloc_port(dd, i, fp);
1371 ipath_dbg("No ports available (none initialized "
1375 /* if started above 0, retry from 0 */
1377 "%s[%u] no ports on prefunit "
1378 "%d, clear and re-check\n",
1379 current->comm, current->pid,
1381 devmax = ipath_count_units(NULL, NULL,
1387 ipath_dbg("No ports available\n");
1391 ipath_dbg("No boards found\n");
1398 static int ipath_open(struct inode *in, struct file *fp)
1400 int ret, user_minor;
1402 mutex_lock(&ipath_mutex);
1404 user_minor = iminor(in) - IPATH_USER_MINOR_BASE;
1405 ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
1406 (long)in->i_rdev, user_minor);
1409 ret = find_free_port(user_minor - 1, fp);
1411 ret = find_best_unit(fp);
1413 mutex_unlock(&ipath_mutex);
1418 * unlock_exptid - unlock any expected TID entries port still had in use
1421 * We don't actually update the chip here, because we do a bulk update
1422 * below, using ipath_f_clear_tids.
1424 static void unlock_expected_tids(struct ipath_portdata *pd)
1426 struct ipath_devdata *dd = pd->port_dd;
1427 int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
1428 int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
1430 ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
1432 for (i = port_tidbase; i < maxtid; i++) {
1433 if (!dd->ipath_pageshadow[i])
1436 ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i],
1438 dd->ipath_pageshadow[i] = NULL;
1440 ipath_stats.sps_pageunlocks++;
1443 ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
1444 pd->port_port, cnt);
1446 if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
1447 ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
1448 (unsigned long long) ipath_stats.sps_pagelocks,
1449 (unsigned long long)
1450 ipath_stats.sps_pageunlocks);
1453 static int ipath_close(struct inode *in, struct file *fp)
1456 struct ipath_portdata *pd;
1457 struct ipath_devdata *dd;
1460 ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
1461 (long)in->i_rdev, fp->private_data);
1463 mutex_lock(&ipath_mutex);
1466 port = pd->port_port;
1467 fp->private_data = NULL;
1470 if (pd->port_hdrqfull) {
1471 ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
1472 "during run\n", pd->port_comm, pd->port_pid,
1474 pd->port_hdrqfull = 0;
1477 if (pd->port_rcvwait_to || pd->port_piowait_to
1478 || pd->port_rcvnowait || pd->port_pionowait) {
1479 ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
1480 "%u rcv %u, pio already\n",
1481 pd->port_port, pd->port_rcvwait_to,
1482 pd->port_piowait_to, pd->port_rcvnowait,
1483 pd->port_pionowait);
1484 pd->port_rcvwait_to = pd->port_piowait_to =
1485 pd->port_rcvnowait = pd->port_pionowait = 0;
1487 if (pd->port_flag) {
1488 ipath_dbg("port %u port_flag still set to 0x%lx\n",
1489 pd->port_port, pd->port_flag);
1493 if (dd->ipath_kregbase) {
1495 /* atomically clear receive enable port. */
1496 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port,
1497 &dd->ipath_rcvctrl);
1498 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
1500 /* and read back from chip to be sure that nothing
1501 * else is in flight when we do the rest */
1502 (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
1504 /* clean up the pkeys for this port user */
1505 ipath_clean_part_key(pd, dd);
1509 * be paranoid, and never write 0's to these, just use an
1510 * unused part of the port 0 tail page. Of course,
1511 * rcvhdraddr points to a large chunk of memory, so this
1512 * could still trash things, but at least it won't trash
1513 * page 0, and by disabling the port, it should stop "soon",
1514 * even if a packet or two is in already in flight after we
1515 * disabled the port.
1517 ipath_write_kreg_port(dd,
1518 dd->ipath_kregs->kr_rcvhdrtailaddr, port,
1519 dd->ipath_dummy_hdrq_phys);
1520 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
1521 pd->port_port, dd->ipath_dummy_hdrq_phys);
1523 i = dd->ipath_pbufsport * (port - 1);
1524 ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport);
1526 if (dd->ipath_pageshadow)
1527 unlock_expected_tids(pd);
1528 ipath_stats.sps_ports--;
1529 ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
1530 pd->port_comm, pd->port_pid,
1531 dd->ipath_unit, port);
1533 dd->ipath_f_clear_tids(dd, pd->port_port);
1539 dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
1540 mutex_unlock(&ipath_mutex);
1541 ipath_free_pddata(dd, pd); /* after releasing the mutex */
1546 static int ipath_port_info(struct ipath_portdata *pd,
1547 struct ipath_port_info __user *uinfo)
1549 struct ipath_port_info info;
1553 (void) ipath_count_units(NULL, &nup, NULL);
1554 info.num_active = nup;
1555 info.unit = pd->port_dd->ipath_unit;
1556 info.port = pd->port_port;
1558 if (copy_to_user(uinfo, &info, sizeof(info))) {
1568 static ssize_t ipath_write(struct file *fp, const char __user *data,
1569 size_t count, loff_t *off)
1571 const struct ipath_cmd __user *ucmd;
1572 struct ipath_portdata *pd;
1573 const void __user *src;
1574 size_t consumed, copy;
1575 struct ipath_cmd cmd;
1579 if (count < sizeof(cmd.type)) {
1584 ucmd = (const struct ipath_cmd __user *) data;
1586 if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
1591 consumed = sizeof(cmd.type);
1594 case IPATH_CMD_USER_INIT:
1595 copy = sizeof(cmd.cmd.user_info);
1596 dest = &cmd.cmd.user_info;
1597 src = &ucmd->cmd.user_info;
1599 case IPATH_CMD_RECV_CTRL:
1600 copy = sizeof(cmd.cmd.recv_ctrl);
1601 dest = &cmd.cmd.recv_ctrl;
1602 src = &ucmd->cmd.recv_ctrl;
1604 case IPATH_CMD_PORT_INFO:
1605 copy = sizeof(cmd.cmd.port_info);
1606 dest = &cmd.cmd.port_info;
1607 src = &ucmd->cmd.port_info;
1609 case IPATH_CMD_TID_UPDATE:
1610 case IPATH_CMD_TID_FREE:
1611 copy = sizeof(cmd.cmd.tid_info);
1612 dest = &cmd.cmd.tid_info;
1613 src = &ucmd->cmd.tid_info;
1615 case IPATH_CMD_SET_PART_KEY:
1616 copy = sizeof(cmd.cmd.part_key);
1617 dest = &cmd.cmd.part_key;
1618 src = &ucmd->cmd.part_key;
1625 if ((count - consumed) < copy) {
1630 if (copy_from_user(dest, src, copy)) {
1639 case IPATH_CMD_USER_INIT:
1640 ret = ipath_do_user_init(pd, &cmd.cmd.user_info);
1643 ret = ipath_get_base_info(
1644 pd, (void __user *) (unsigned long)
1645 cmd.cmd.user_info.spu_base_info,
1646 cmd.cmd.user_info.spu_base_info_size);
1648 case IPATH_CMD_RECV_CTRL:
1649 ret = ipath_manage_rcvq(pd, cmd.cmd.recv_ctrl);
1651 case IPATH_CMD_PORT_INFO:
1652 ret = ipath_port_info(pd,
1653 (struct ipath_port_info __user *)
1654 (unsigned long) cmd.cmd.port_info);
1656 case IPATH_CMD_TID_UPDATE:
1657 ret = ipath_tid_update(pd, &cmd.cmd.tid_info);
1659 case IPATH_CMD_TID_FREE:
1660 ret = ipath_tid_free(pd, &cmd.cmd.tid_info);
1662 case IPATH_CMD_SET_PART_KEY:
1663 ret = ipath_set_part_key(pd, cmd.cmd.part_key);
1674 static struct class *ipath_class;
1676 static int init_cdev(int minor, char *name, struct file_operations *fops,
1677 struct cdev **cdevp, struct class_device **class_devp)
1679 const dev_t dev = MKDEV(IPATH_MAJOR, minor);
1680 struct cdev *cdev = NULL;
1681 struct class_device *class_dev = NULL;
1684 cdev = cdev_alloc();
1686 printk(KERN_ERR IPATH_DRV_NAME
1687 ": Could not allocate cdev for minor %d, %s\n",
1693 cdev->owner = THIS_MODULE;
1695 kobject_set_name(&cdev->kobj, name);
1697 ret = cdev_add(cdev, dev, 1);
1699 printk(KERN_ERR IPATH_DRV_NAME
1700 ": Could not add cdev for minor %d, %s (err %d)\n",
1705 class_dev = class_device_create(ipath_class, NULL, dev, NULL, name);
1707 if (IS_ERR(class_dev)) {
1708 ret = PTR_ERR(class_dev);
1709 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
1710 "class_dev for minor %d, %s (err %d)\n",
1724 *class_devp = class_dev;
1733 int ipath_cdev_init(int minor, char *name, struct file_operations *fops,
1734 struct cdev **cdevp, struct class_device **class_devp)
1736 return init_cdev(minor, name, fops, cdevp, class_devp);
1739 static void cleanup_cdev(struct cdev **cdevp,
1740 struct class_device **class_devp)
1742 struct class_device *class_dev = *class_devp;
1745 class_device_unregister(class_dev);
1755 void ipath_cdev_cleanup(struct cdev **cdevp,
1756 struct class_device **class_devp)
1758 cleanup_cdev(cdevp, class_devp);
1761 static struct cdev *wildcard_cdev;
1762 static struct class_device *wildcard_class_dev;
1764 static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
1766 static int user_init(void)
1770 ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
1772 printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
1773 "chrdev region (err %d)\n", -ret);
1777 ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
1779 if (IS_ERR(ipath_class)) {
1780 ret = PTR_ERR(ipath_class);
1781 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
1782 "device class (err %d)\n", -ret);
1788 unregister_chrdev_region(dev, IPATH_NMINORS);
1793 static void user_cleanup(void)
1796 class_destroy(ipath_class);
1800 unregister_chrdev_region(dev, IPATH_NMINORS);
1803 static atomic_t user_count = ATOMIC_INIT(0);
1804 static atomic_t user_setup = ATOMIC_INIT(0);
1806 int ipath_user_add(struct ipath_devdata *dd)
1811 if (atomic_inc_return(&user_count) == 1) {
1814 ipath_dev_err(dd, "Unable to set up user support: "
1815 "error %d\n", -ret);
1818 ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
1819 &wildcard_class_dev);
1821 ipath_dev_err(dd, "Could not create wildcard "
1822 "minor: error %d\n", -ret);
1826 atomic_set(&user_setup, 1);
1829 snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
1831 ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
1832 &dd->user_cdev, &dd->user_class_dev);
1834 ipath_dev_err(dd, "Could not create user minor %d, %s\n",
1835 dd->ipath_unit + 1, name);
1845 void ipath_user_remove(struct ipath_devdata *dd)
1847 cleanup_cdev(&dd->user_cdev, &dd->user_class_dev);
1849 if (atomic_dec_return(&user_count) == 0) {
1850 if (atomic_read(&user_setup) == 0)
1853 cleanup_cdev(&wildcard_cdev, &wildcard_class_dev);
1856 atomic_set(&user_setup, 0);