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/slab.h>
40 #include <linux/highmem.h>
42 #include <linux/jiffies.h>
43 #include <linux/smp_lock.h>
44 #include <asm/pgtable.h>
46 #include "ipath_kernel.h"
47 #include "ipath_common.h"
48 #include "ipath_user_sdma.h"
50 static int ipath_open(struct inode
*, struct file
*);
51 static int ipath_close(struct inode
*, struct file
*);
52 static ssize_t
ipath_write(struct file
*, const char __user
*, size_t,
54 static ssize_t
ipath_writev(struct kiocb
*, const struct iovec
*,
55 unsigned long , loff_t
);
56 static unsigned int ipath_poll(struct file
*, struct poll_table_struct
*);
57 static int ipath_mmap(struct file
*, struct vm_area_struct
*);
59 static const struct file_operations ipath_file_ops
= {
62 .aio_write
= ipath_writev
,
64 .release
= ipath_close
,
67 .llseek
= noop_llseek
,
71 * Convert kernel virtual addresses to physical addresses so they don't
72 * potentially conflict with the chip addresses used as mmap offsets.
73 * It doesn't really matter what mmap offset we use as long as we can
74 * interpret it correctly.
76 static u64
cvt_kvaddr(void *p
)
81 page
= vmalloc_to_page(p
);
83 paddr
= page_to_pfn(page
) << PAGE_SHIFT
;
88 static int ipath_get_base_info(struct file
*fp
,
89 void __user
*ubase
, size_t ubase_size
)
91 struct ipath_portdata
*pd
= port_fp(fp
);
93 struct ipath_base_info
*kinfo
= NULL
;
94 struct ipath_devdata
*dd
= pd
->port_dd
;
99 subport_cnt
= pd
->port_subport_cnt
;
106 master
= !subport_fp(fp
);
110 /* If port sharing is not requested, allow the old size structure */
112 sz
-= 7 * sizeof(u64
);
113 if (ubase_size
< sz
) {
115 "Base size %zu, need %zu (version mismatch?)\n",
121 kinfo
= kzalloc(sizeof(*kinfo
), GFP_KERNEL
);
127 ret
= dd
->ipath_f_get_base_info(pd
, kinfo
);
131 kinfo
->spi_rcvhdr_cnt
= dd
->ipath_rcvhdrcnt
;
132 kinfo
->spi_rcvhdrent_size
= dd
->ipath_rcvhdrentsize
;
133 kinfo
->spi_tidegrcnt
= dd
->ipath_rcvegrcnt
;
134 kinfo
->spi_rcv_egrbufsize
= dd
->ipath_rcvegrbufsize
;
136 * have to mmap whole thing
138 kinfo
->spi_rcv_egrbuftotlen
=
139 pd
->port_rcvegrbuf_chunks
* pd
->port_rcvegrbuf_size
;
140 kinfo
->spi_rcv_egrperchunk
= pd
->port_rcvegrbufs_perchunk
;
141 kinfo
->spi_rcv_egrchunksize
= kinfo
->spi_rcv_egrbuftotlen
/
142 pd
->port_rcvegrbuf_chunks
;
143 kinfo
->spi_tidcnt
= dd
->ipath_rcvtidcnt
/ subport_cnt
;
145 kinfo
->spi_tidcnt
+= dd
->ipath_rcvtidcnt
% subport_cnt
;
147 * for this use, may be ipath_cfgports summed over all chips that
148 * are are configured and present
150 kinfo
->spi_nports
= dd
->ipath_cfgports
;
151 /* unit (chip/board) our port is on */
152 kinfo
->spi_unit
= dd
->ipath_unit
;
153 /* for now, only a single page */
154 kinfo
->spi_tid_maxsize
= PAGE_SIZE
;
157 * Doing this per port, and based on the skip value, etc. This has
158 * to be the actual buffer size, since the protocol code treats it
161 * These have to be set to user addresses in the user code via mmap.
162 * These values are used on return to user code for the mmap target
163 * addresses only. For 32 bit, same 44 bit address problem, so use
164 * the physical address, not virtual. Before 2.6.11, using the
165 * page_address() macro worked, but in 2.6.11, even that returns the
166 * full 64 bit address (upper bits all 1's). So far, using the
167 * physical addresses (or chip offsets, for chip mapping) works, but
168 * no doubt some future kernel release will change that, and we'll be
169 * on to yet another method of dealing with this.
171 kinfo
->spi_rcvhdr_base
= (u64
) pd
->port_rcvhdrq_phys
;
172 kinfo
->spi_rcvhdr_tailaddr
= (u64
) pd
->port_rcvhdrqtailaddr_phys
;
173 kinfo
->spi_rcv_egrbufs
= (u64
) pd
->port_rcvegr_phys
;
174 kinfo
->spi_pioavailaddr
= (u64
) dd
->ipath_pioavailregs_phys
;
175 kinfo
->spi_status
= (u64
) kinfo
->spi_pioavailaddr
+
176 (void *) dd
->ipath_statusp
-
177 (void *) dd
->ipath_pioavailregs_dma
;
179 kinfo
->spi_piocnt
= pd
->port_piocnt
;
180 kinfo
->spi_piobufbase
= (u64
) pd
->port_piobufs
;
181 kinfo
->__spi_uregbase
= (u64
) dd
->ipath_uregbase
+
182 dd
->ipath_ureg_align
* pd
->port_port
;
184 kinfo
->spi_piocnt
= (pd
->port_piocnt
/ subport_cnt
) +
185 (pd
->port_piocnt
% subport_cnt
);
186 /* Master's PIO buffers are after all the slave's */
187 kinfo
->spi_piobufbase
= (u64
) pd
->port_piobufs
+
189 (pd
->port_piocnt
- kinfo
->spi_piocnt
);
191 unsigned slave
= subport_fp(fp
) - 1;
193 kinfo
->spi_piocnt
= pd
->port_piocnt
/ subport_cnt
;
194 kinfo
->spi_piobufbase
= (u64
) pd
->port_piobufs
+
195 dd
->ipath_palign
* kinfo
->spi_piocnt
* slave
;
199 kinfo
->spi_port_uregbase
= (u64
) dd
->ipath_uregbase
+
200 dd
->ipath_ureg_align
* pd
->port_port
;
201 kinfo
->spi_port_rcvegrbuf
= kinfo
->spi_rcv_egrbufs
;
202 kinfo
->spi_port_rcvhdr_base
= kinfo
->spi_rcvhdr_base
;
203 kinfo
->spi_port_rcvhdr_tailaddr
= kinfo
->spi_rcvhdr_tailaddr
;
205 kinfo
->__spi_uregbase
= cvt_kvaddr(pd
->subport_uregbase
+
206 PAGE_SIZE
* subport_fp(fp
));
208 kinfo
->spi_rcvhdr_base
= cvt_kvaddr(pd
->subport_rcvhdr_base
+
209 pd
->port_rcvhdrq_size
* subport_fp(fp
));
210 kinfo
->spi_rcvhdr_tailaddr
= 0;
211 kinfo
->spi_rcv_egrbufs
= cvt_kvaddr(pd
->subport_rcvegrbuf
+
212 pd
->port_rcvegrbuf_chunks
* pd
->port_rcvegrbuf_size
*
215 kinfo
->spi_subport_uregbase
=
216 cvt_kvaddr(pd
->subport_uregbase
);
217 kinfo
->spi_subport_rcvegrbuf
=
218 cvt_kvaddr(pd
->subport_rcvegrbuf
);
219 kinfo
->spi_subport_rcvhdr_base
=
220 cvt_kvaddr(pd
->subport_rcvhdr_base
);
221 ipath_cdbg(PROC
, "port %u flags %x %llx %llx %llx\n",
222 kinfo
->spi_port
, kinfo
->spi_runtime_flags
,
223 (unsigned long long) kinfo
->spi_subport_uregbase
,
224 (unsigned long long) kinfo
->spi_subport_rcvegrbuf
,
225 (unsigned long long) kinfo
->spi_subport_rcvhdr_base
);
229 * All user buffers are 2KB buffers. If we ever support
230 * giving 4KB buffers to user processes, this will need some
233 kinfo
->spi_pioindex
= (kinfo
->spi_piobufbase
-
234 (dd
->ipath_piobufbase
& 0xffffffff)) / dd
->ipath_palign
;
235 kinfo
->spi_pioalign
= dd
->ipath_palign
;
237 kinfo
->spi_qpair
= IPATH_KD_QP
;
239 * user mode PIO buffers are always 2KB, even when 4KB can
240 * be received, and sent via the kernel; this is ibmaxlen
243 kinfo
->spi_piosize
= dd
->ipath_piosize2k
- 2 * sizeof(u32
);
244 kinfo
->spi_mtu
= dd
->ipath_ibmaxlen
; /* maxlen, not ibmtu */
245 kinfo
->spi_port
= pd
->port_port
;
246 kinfo
->spi_subport
= subport_fp(fp
);
247 kinfo
->spi_sw_version
= IPATH_KERN_SWVERSION
;
248 kinfo
->spi_hw_version
= dd
->ipath_revision
;
251 kinfo
->spi_runtime_flags
|= IPATH_RUNTIME_MASTER
;
254 sz
= (ubase_size
< sizeof(*kinfo
)) ? ubase_size
: sizeof(*kinfo
);
255 if (copy_to_user(ubase
, kinfo
, sz
))
264 * ipath_tid_update - update a port TID
266 * @fp: the ipath device file
267 * @ti: the TID information
269 * The new implementation as of Oct 2004 is that the driver assigns
270 * the tid and returns it to the caller. To make it easier to
271 * catch bugs, and to reduce search time, we keep a cursor for
272 * each port, walking the shadow tid array to find one that's not
275 * For now, if we can't allocate the full list, we fail, although
276 * in the long run, we'll allocate as many as we can, and the
277 * caller will deal with that by trying the remaining pages later.
278 * That means that when we fail, we have to mark the tids as not in
279 * use again, in our shadow copy.
281 * It's up to the caller to free the tids when they are done.
282 * We'll unlock the pages as they free them.
284 * Also, right now we are locking one page at a time, but since
285 * the intended use of this routine is for a single group of
286 * virtually contiguous pages, that should change to improve
289 static int ipath_tid_update(struct ipath_portdata
*pd
, struct file
*fp
,
290 const struct ipath_tid_info
*ti
)
293 u32 tid
, porttid
, cnt
, i
, tidcnt
, tidoff
;
295 struct ipath_devdata
*dd
= pd
->port_dd
;
298 u64 __iomem
*tidbase
;
299 unsigned long tidmap
[8];
300 struct page
**pagep
= NULL
;
301 unsigned subport
= subport_fp(fp
);
303 if (!dd
->ipath_pageshadow
) {
310 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
311 (unsigned long long) ti
->tidlist
);
313 * Should we treat as success? likely a bug
318 porttid
= pd
->port_port
* dd
->ipath_rcvtidcnt
;
319 if (!pd
->port_subport_cnt
) {
320 tidcnt
= dd
->ipath_rcvtidcnt
;
321 tid
= pd
->port_tidcursor
;
323 } else if (!subport
) {
324 tidcnt
= (dd
->ipath_rcvtidcnt
/ pd
->port_subport_cnt
) +
325 (dd
->ipath_rcvtidcnt
% pd
->port_subport_cnt
);
326 tidoff
= dd
->ipath_rcvtidcnt
- tidcnt
;
328 tid
= tidcursor_fp(fp
);
330 tidcnt
= dd
->ipath_rcvtidcnt
/ pd
->port_subport_cnt
;
331 tidoff
= tidcnt
* (subport
- 1);
333 tid
= tidcursor_fp(fp
);
336 /* make sure it all fits in port_tid_pg_list */
337 dev_info(&dd
->pcidev
->dev
, "Process tried to allocate %u "
338 "TIDs, only trying max (%u)\n", cnt
, tidcnt
);
341 pagep
= &((struct page
**) pd
->port_tid_pg_list
)[tidoff
];
342 tidlist
= &((u16
*) &pagep
[dd
->ipath_rcvtidcnt
])[tidoff
];
344 memset(tidmap
, 0, sizeof(tidmap
));
345 /* before decrement; chip actual # */
347 tidbase
= (u64 __iomem
*) (((char __iomem
*) dd
->ipath_kregbase
) +
348 dd
->ipath_rcvtidbase
+
349 porttid
* sizeof(*tidbase
));
351 ipath_cdbg(VERBOSE
, "Port%u %u tids, cursor %u, tidbase %p\n",
352 pd
->port_port
, cnt
, tid
, tidbase
);
354 /* virtual address of first page in transfer */
355 vaddr
= ti
->tidvaddr
;
356 if (!access_ok(VERIFY_WRITE
, (void __user
*) vaddr
,
358 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
363 ret
= ipath_get_user_pages(vaddr
, cnt
, pagep
);
366 ipath_dbg("Failed to lock addr %p, %u pages "
367 "(already locked)\n",
368 (void *) vaddr
, cnt
);
370 * for now, continue, and see what happens but with
371 * the new implementation, this should never happen,
372 * unless perhaps the user has mpin'ed the pages
373 * themselves (something we need to test)
377 dev_info(&dd
->pcidev
->dev
,
378 "Failed to lock addr %p, %u pages: "
379 "errno %d\n", (void *) vaddr
, cnt
, -ret
);
383 for (i
= 0; i
< cnt
; i
++, vaddr
+= PAGE_SIZE
) {
384 for (; ntids
--; tid
++) {
387 if (!dd
->ipath_pageshadow
[porttid
+ tid
])
392 * oops, wrapped all the way through their TIDs,
393 * and didn't have enough free; see comments at
396 ipath_dbg("Not enough free TIDs for %u pages "
397 "(index %d), failing\n", cnt
, i
);
398 i
--; /* last tidlist[i] not filled in */
402 tidlist
[i
] = tid
+ tidoff
;
403 ipath_cdbg(VERBOSE
, "Updating idx %u to TID %u, "
404 "vaddr %lx\n", i
, tid
+ tidoff
, vaddr
);
405 /* we "know" system pages and TID pages are same size */
406 dd
->ipath_pageshadow
[porttid
+ tid
] = pagep
[i
];
407 dd
->ipath_physshadow
[porttid
+ tid
] = ipath_map_page(
408 dd
->pcidev
, pagep
[i
], 0, PAGE_SIZE
,
411 * don't need atomic or it's overhead
413 __set_bit(tid
, tidmap
);
414 physaddr
= dd
->ipath_physshadow
[porttid
+ tid
];
415 ipath_stats
.sps_pagelocks
++;
417 "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
418 tid
, vaddr
, (unsigned long long) physaddr
,
420 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
], RCVHQ_RCV_TYPE_EXPECTED
,
423 * don't check this tid in ipath_portshadow, since we
424 * just filled it in; start with the next one.
432 /* jump here if copy out of updated info failed... */
433 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
435 /* same code that's in ipath_free_tid() */
436 limit
= sizeof(tidmap
) * BITS_PER_BYTE
;
438 /* just in case size changes in future */
440 tid
= find_first_bit((const unsigned long *)tidmap
, limit
);
441 for (; tid
< limit
; tid
++) {
442 if (!test_bit(tid
, tidmap
))
444 if (dd
->ipath_pageshadow
[porttid
+ tid
]) {
445 ipath_cdbg(VERBOSE
, "Freeing TID %u\n",
447 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
],
448 RCVHQ_RCV_TYPE_EXPECTED
,
449 dd
->ipath_tidinvalid
);
450 pci_unmap_page(dd
->pcidev
,
451 dd
->ipath_physshadow
[porttid
+ tid
],
452 PAGE_SIZE
, PCI_DMA_FROMDEVICE
);
453 dd
->ipath_pageshadow
[porttid
+ tid
] = NULL
;
454 ipath_stats
.sps_pageunlocks
++;
457 ipath_release_user_pages(pagep
, cnt
);
460 * Copy the updated array, with ipath_tid's filled in, back
461 * to user. Since we did the copy in already, this "should
462 * never fail" If it does, we have to clean up...
464 if (copy_to_user((void __user
*)
465 (unsigned long) ti
->tidlist
,
466 tidlist
, cnt
* sizeof(*tidlist
))) {
470 if (copy_to_user((void __user
*) (unsigned long) ti
->tidmap
,
471 tidmap
, sizeof tidmap
)) {
477 if (!pd
->port_subport_cnt
)
478 pd
->port_tidcursor
= tid
;
480 tidcursor_fp(fp
) = tid
;
485 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
491 * ipath_tid_free - free a port TID
493 * @subport: the subport
496 * right now we are unlocking one page at a time, but since
497 * the intended use of this routine is for a single group of
498 * virtually contiguous pages, that should change to improve
499 * performance. We check that the TID is in range for this port
500 * but otherwise don't check validity; if user has an error and
501 * frees the wrong tid, it's only their own data that can thereby
502 * be corrupted. We do check that the TID was in use, for sanity
503 * We always use our idea of the saved address, not the address that
504 * they pass in to us.
507 static int ipath_tid_free(struct ipath_portdata
*pd
, unsigned subport
,
508 const struct ipath_tid_info
*ti
)
511 u32 tid
, porttid
, cnt
, limit
, tidcnt
;
512 struct ipath_devdata
*dd
= pd
->port_dd
;
513 u64 __iomem
*tidbase
;
514 unsigned long tidmap
[8];
516 if (!dd
->ipath_pageshadow
) {
521 if (copy_from_user(tidmap
, (void __user
*)(unsigned long)ti
->tidmap
,
527 porttid
= pd
->port_port
* dd
->ipath_rcvtidcnt
;
528 if (!pd
->port_subport_cnt
)
529 tidcnt
= dd
->ipath_rcvtidcnt
;
531 tidcnt
= (dd
->ipath_rcvtidcnt
/ pd
->port_subport_cnt
) +
532 (dd
->ipath_rcvtidcnt
% pd
->port_subport_cnt
);
533 porttid
+= dd
->ipath_rcvtidcnt
- tidcnt
;
535 tidcnt
= dd
->ipath_rcvtidcnt
/ pd
->port_subport_cnt
;
536 porttid
+= tidcnt
* (subport
- 1);
538 tidbase
= (u64 __iomem
*) ((char __iomem
*)(dd
->ipath_kregbase
) +
539 dd
->ipath_rcvtidbase
+
540 porttid
* sizeof(*tidbase
));
542 limit
= sizeof(tidmap
) * BITS_PER_BYTE
;
544 /* just in case size changes in future */
546 tid
= find_first_bit(tidmap
, limit
);
547 ipath_cdbg(VERBOSE
, "Port%u free %u tids; first bit (max=%d) "
548 "set is %d, porttid %u\n", pd
->port_port
, ti
->tidcnt
,
549 limit
, tid
, porttid
);
550 for (cnt
= 0; tid
< limit
; tid
++) {
552 * small optimization; if we detect a run of 3 or so without
553 * any set, use find_first_bit again. That's mainly to
554 * accelerate the case where we wrapped, so we have some at
555 * the beginning, and some at the end, and a big gap
558 if (!test_bit(tid
, tidmap
))
561 if (dd
->ipath_pageshadow
[porttid
+ tid
]) {
563 p
= dd
->ipath_pageshadow
[porttid
+ tid
];
564 dd
->ipath_pageshadow
[porttid
+ tid
] = NULL
;
565 ipath_cdbg(VERBOSE
, "PID %u freeing TID %u\n",
566 pid_nr(pd
->port_pid
), tid
);
567 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
],
568 RCVHQ_RCV_TYPE_EXPECTED
,
569 dd
->ipath_tidinvalid
);
570 pci_unmap_page(dd
->pcidev
,
571 dd
->ipath_physshadow
[porttid
+ tid
],
572 PAGE_SIZE
, PCI_DMA_FROMDEVICE
);
573 ipath_release_user_pages(&p
, 1);
574 ipath_stats
.sps_pageunlocks
++;
576 ipath_dbg("Unused tid %u, ignoring\n", tid
);
578 if (cnt
!= ti
->tidcnt
)
579 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
583 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
589 * ipath_set_part_key - set a partition key
593 * We can have up to 4 active at a time (other than the default, which is
594 * always allowed). This is somewhat tricky, since multiple ports may set
595 * the same key, so we reference count them, and clean up at exit. All 4
596 * partition keys are packed into a single infinipath register. It's an
597 * error for a process to set the same pkey multiple times. We provide no
598 * mechanism to de-allocate a pkey at this time, we may eventually need to
599 * do that. I've used the atomic operations, and no locking, and only make
600 * a single pass through what's available. This should be more than
601 * adequate for some time. I'll think about spinlocks or the like if and as
604 static int ipath_set_part_key(struct ipath_portdata
*pd
, u16 key
)
606 struct ipath_devdata
*dd
= pd
->port_dd
;
607 int i
, any
= 0, pidx
= -1;
608 u16 lkey
= key
& 0x7FFF;
611 if (lkey
== (IPATH_DEFAULT_P_KEY
& 0x7FFF)) {
612 /* nothing to do; this key always valid */
617 ipath_cdbg(VERBOSE
, "p%u try to set pkey %hx, current keys "
618 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
619 pd
->port_port
, key
, dd
->ipath_pkeys
[0],
620 atomic_read(&dd
->ipath_pkeyrefs
[0]), dd
->ipath_pkeys
[1],
621 atomic_read(&dd
->ipath_pkeyrefs
[1]), dd
->ipath_pkeys
[2],
622 atomic_read(&dd
->ipath_pkeyrefs
[2]), dd
->ipath_pkeys
[3],
623 atomic_read(&dd
->ipath_pkeyrefs
[3]));
626 ipath_cdbg(PROC
, "p%u tries to set key 0, not allowed\n",
633 * Set the full membership bit, because it has to be
634 * set in the register or the packet, and it seems
635 * cleaner to set in the register than to force all
636 * callers to set it. (see bug 4331)
640 for (i
= 0; i
< ARRAY_SIZE(pd
->port_pkeys
); i
++) {
641 if (!pd
->port_pkeys
[i
] && pidx
== -1)
643 if (pd
->port_pkeys
[i
] == key
) {
644 ipath_cdbg(VERBOSE
, "p%u tries to set same pkey "
645 "(%x) more than once\n",
652 ipath_dbg("All pkeys for port %u already in use, "
653 "can't set %x\n", pd
->port_port
, key
);
657 for (any
= i
= 0; i
< ARRAY_SIZE(dd
->ipath_pkeys
); i
++) {
658 if (!dd
->ipath_pkeys
[i
]) {
662 if (dd
->ipath_pkeys
[i
] == key
) {
663 atomic_t
*pkrefs
= &dd
->ipath_pkeyrefs
[i
];
665 if (atomic_inc_return(pkrefs
) > 1) {
666 pd
->port_pkeys
[pidx
] = key
;
667 ipath_cdbg(VERBOSE
, "p%u set key %x "
668 "matches #%d, count now %d\n",
669 pd
->port_port
, key
, i
,
670 atomic_read(pkrefs
));
675 * lost race, decrement count, catch below
678 ipath_cdbg(VERBOSE
, "Lost race, count was "
679 "0, after dec, it's %d\n",
680 atomic_read(pkrefs
));
684 if ((dd
->ipath_pkeys
[i
] & 0x7FFF) == lkey
) {
686 * It makes no sense to have both the limited and
687 * full membership PKEY set at the same time since
688 * the unlimited one will disable the limited one.
695 ipath_dbg("port %u, all pkeys already in use, "
696 "can't set %x\n", pd
->port_port
, key
);
700 for (any
= i
= 0; i
< ARRAY_SIZE(dd
->ipath_pkeys
); i
++) {
701 if (!dd
->ipath_pkeys
[i
] &&
702 atomic_inc_return(&dd
->ipath_pkeyrefs
[i
]) == 1) {
705 /* for ipathstats, etc. */
706 ipath_stats
.sps_pkeys
[i
] = lkey
;
707 pd
->port_pkeys
[pidx
] = dd
->ipath_pkeys
[i
] = key
;
709 (u64
) dd
->ipath_pkeys
[0] |
710 ((u64
) dd
->ipath_pkeys
[1] << 16) |
711 ((u64
) dd
->ipath_pkeys
[2] << 32) |
712 ((u64
) dd
->ipath_pkeys
[3] << 48);
713 ipath_cdbg(PROC
, "p%u set key %x in #%d, "
714 "portidx %d, new pkey reg %llx\n",
715 pd
->port_port
, key
, i
, pidx
,
716 (unsigned long long) pkey
);
718 dd
, dd
->ipath_kregs
->kr_partitionkey
, pkey
);
724 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
725 "can't set %x\n", pd
->port_port
, key
);
733 * ipath_manage_rcvq - manage a port's receive queue
735 * @subport: the subport
736 * @start_stop: action to carry out
738 * start_stop == 0 disables receive on the port, for use in queue
739 * overflow conditions. start_stop==1 re-enables, to be used to
740 * re-init the software copy of the head register
742 static int ipath_manage_rcvq(struct ipath_portdata
*pd
, unsigned subport
,
745 struct ipath_devdata
*dd
= pd
->port_dd
;
747 ipath_cdbg(PROC
, "%sabling rcv for unit %u port %u:%u\n",
748 start_stop
? "en" : "dis", dd
->ipath_unit
,
749 pd
->port_port
, subport
);
752 /* atomically clear receive enable port. */
755 * On enable, force in-memory copy of the tail register to
756 * 0, so that protocol code doesn't have to worry about
757 * whether or not the chip has yet updated the in-memory
758 * copy or not on return from the system call. The chip
759 * always resets it's tail register back to 0 on a
760 * transition from disabled to enabled. This could cause a
761 * problem if software was broken, and did the enable w/o
762 * the disable, but eventually the in-memory copy will be
763 * updated and correct itself, even in the face of software
766 if (pd
->port_rcvhdrtail_kvaddr
)
767 ipath_clear_rcvhdrtail(pd
);
768 set_bit(dd
->ipath_r_portenable_shift
+ pd
->port_port
,
771 clear_bit(dd
->ipath_r_portenable_shift
+ pd
->port_port
,
773 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
775 /* now be sure chip saw it before we return */
776 ipath_read_kreg64(dd
, dd
->ipath_kregs
->kr_scratch
);
779 * And try to be sure that tail reg update has happened too.
780 * This should in theory interlock with the RXE changes to
781 * the tail register. Don't assign it to the tail register
782 * in memory copy, since we could overwrite an update by the
785 ipath_read_ureg32(dd
, ur_rcvhdrtail
, pd
->port_port
);
787 /* always; new head should be equal to new tail; see above */
792 static void ipath_clean_part_key(struct ipath_portdata
*pd
,
793 struct ipath_devdata
*dd
)
795 int i
, j
, pchanged
= 0;
798 /* for debugging only */
799 oldpkey
= (u64
) dd
->ipath_pkeys
[0] |
800 ((u64
) dd
->ipath_pkeys
[1] << 16) |
801 ((u64
) dd
->ipath_pkeys
[2] << 32) |
802 ((u64
) dd
->ipath_pkeys
[3] << 48);
804 for (i
= 0; i
< ARRAY_SIZE(pd
->port_pkeys
); i
++) {
805 if (!pd
->port_pkeys
[i
])
807 ipath_cdbg(VERBOSE
, "look for key[%d] %hx in pkeys\n", i
,
809 for (j
= 0; j
< ARRAY_SIZE(dd
->ipath_pkeys
); j
++) {
810 /* check for match independent of the global bit */
811 if ((dd
->ipath_pkeys
[j
] & 0x7fff) !=
812 (pd
->port_pkeys
[i
] & 0x7fff))
814 if (atomic_dec_and_test(&dd
->ipath_pkeyrefs
[j
])) {
815 ipath_cdbg(VERBOSE
, "p%u clear key "
818 pd
->port_pkeys
[i
], j
);
819 ipath_stats
.sps_pkeys
[j
] =
820 dd
->ipath_pkeys
[j
] = 0;
824 VERBOSE
, "p%u key %x matches #%d, "
825 "but ref still %d\n", pd
->port_port
,
826 pd
->port_pkeys
[i
], j
,
827 atomic_read(&dd
->ipath_pkeyrefs
[j
]));
830 pd
->port_pkeys
[i
] = 0;
833 u64 pkey
= (u64
) dd
->ipath_pkeys
[0] |
834 ((u64
) dd
->ipath_pkeys
[1] << 16) |
835 ((u64
) dd
->ipath_pkeys
[2] << 32) |
836 ((u64
) dd
->ipath_pkeys
[3] << 48);
837 ipath_cdbg(VERBOSE
, "p%u old pkey reg %llx, "
838 "new pkey reg %llx\n", pd
->port_port
,
839 (unsigned long long) oldpkey
,
840 (unsigned long long) pkey
);
841 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_partitionkey
,
847 * Initialize the port data with the receive buffer sizes
848 * so this can be done while the master port is locked.
849 * Otherwise, there is a race with a slave opening the port
850 * and seeing these fields uninitialized.
852 static void init_user_egr_sizes(struct ipath_portdata
*pd
)
854 struct ipath_devdata
*dd
= pd
->port_dd
;
855 unsigned egrperchunk
, egrcnt
, size
;
858 * to avoid wasting a lot of memory, we allocate 32KB chunks of
859 * physically contiguous memory, advance through it until used up
860 * and then allocate more. Of course, we need memory to store those
861 * extra pointers, now. Started out with 256KB, but under heavy
862 * memory pressure (creating large files and then copying them over
863 * NFS while doing lots of MPI jobs), we hit some allocation
864 * failures, even though we can sleep... (2.6.10) Still get
865 * failures at 64K. 32K is the lowest we can go without wasting
869 egrperchunk
= size
/ dd
->ipath_rcvegrbufsize
;
870 egrcnt
= dd
->ipath_rcvegrcnt
;
871 pd
->port_rcvegrbuf_chunks
= (egrcnt
+ egrperchunk
- 1) / egrperchunk
;
872 pd
->port_rcvegrbufs_perchunk
= egrperchunk
;
873 pd
->port_rcvegrbuf_size
= size
;
877 * ipath_create_user_egr - allocate eager TID buffers
878 * @pd: the port to allocate TID buffers for
880 * This routine is now quite different for user and kernel, because
881 * the kernel uses skb's, for the accelerated network performance
882 * This is the user port version
884 * Allocate the eager TID buffers and program them into infinipath
885 * They are no longer completely contiguous, we do multiple allocation
888 static int ipath_create_user_egr(struct ipath_portdata
*pd
)
890 struct ipath_devdata
*dd
= pd
->port_dd
;
891 unsigned e
, egrcnt
, egrperchunk
, chunk
, egrsize
, egroff
;
897 * GFP_USER, but without GFP_FS, so buffer cache can be
898 * coalesced (we hope); otherwise, even at order 4,
899 * heavy filesystem activity makes these fail, and we can
900 * use compound pages.
902 gfp_flags
= __GFP_WAIT
| __GFP_IO
| __GFP_COMP
;
904 egrcnt
= dd
->ipath_rcvegrcnt
;
905 /* TID number offset for this port */
906 egroff
= (pd
->port_port
- 1) * egrcnt
+ dd
->ipath_p0_rcvegrcnt
;
907 egrsize
= dd
->ipath_rcvegrbufsize
;
908 ipath_cdbg(VERBOSE
, "Allocating %d egr buffers, at egrtid "
909 "offset %x, egrsize %u\n", egrcnt
, egroff
, egrsize
);
911 chunk
= pd
->port_rcvegrbuf_chunks
;
912 egrperchunk
= pd
->port_rcvegrbufs_perchunk
;
913 size
= pd
->port_rcvegrbuf_size
;
914 pd
->port_rcvegrbuf
= kmalloc(chunk
* sizeof(pd
->port_rcvegrbuf
[0]),
916 if (!pd
->port_rcvegrbuf
) {
920 pd
->port_rcvegrbuf_phys
=
921 kmalloc(chunk
* sizeof(pd
->port_rcvegrbuf_phys
[0]),
923 if (!pd
->port_rcvegrbuf_phys
) {
927 for (e
= 0; e
< pd
->port_rcvegrbuf_chunks
; e
++) {
929 pd
->port_rcvegrbuf
[e
] = dma_alloc_coherent(
930 &dd
->pcidev
->dev
, size
, &pd
->port_rcvegrbuf_phys
[e
],
933 if (!pd
->port_rcvegrbuf
[e
]) {
935 goto bail_rcvegrbuf_phys
;
939 pd
->port_rcvegr_phys
= pd
->port_rcvegrbuf_phys
[0];
941 for (e
= chunk
= 0; chunk
< pd
->port_rcvegrbuf_chunks
; chunk
++) {
942 dma_addr_t pa
= pd
->port_rcvegrbuf_phys
[chunk
];
945 for (i
= 0; e
< egrcnt
&& i
< egrperchunk
; e
++, i
++) {
946 dd
->ipath_f_put_tid(dd
, e
+ egroff
+
950 dd
->ipath_rcvegrbase
),
951 RCVHQ_RCV_TYPE_EAGER
, pa
);
954 cond_resched(); /* don't hog the cpu */
961 for (e
= 0; e
< pd
->port_rcvegrbuf_chunks
&&
962 pd
->port_rcvegrbuf
[e
]; e
++) {
963 dma_free_coherent(&dd
->pcidev
->dev
, size
,
964 pd
->port_rcvegrbuf
[e
],
965 pd
->port_rcvegrbuf_phys
[e
]);
968 kfree(pd
->port_rcvegrbuf_phys
);
969 pd
->port_rcvegrbuf_phys
= NULL
;
971 kfree(pd
->port_rcvegrbuf
);
972 pd
->port_rcvegrbuf
= NULL
;
978 /* common code for the mappings on dma_alloc_coherent mem */
979 static int ipath_mmap_mem(struct vm_area_struct
*vma
,
980 struct ipath_portdata
*pd
, unsigned len
, int write_ok
,
981 void *kvaddr
, char *what
)
983 struct ipath_devdata
*dd
= pd
->port_dd
;
987 if ((vma
->vm_end
- vma
->vm_start
) > len
) {
988 dev_info(&dd
->pcidev
->dev
,
989 "FAIL on %s: len %lx > %x\n", what
,
990 vma
->vm_end
- vma
->vm_start
, len
);
996 if (vma
->vm_flags
& VM_WRITE
) {
997 dev_info(&dd
->pcidev
->dev
,
998 "%s must be mapped readonly\n", what
);
1003 /* don't allow them to later change with mprotect */
1004 vma
->vm_flags
&= ~VM_MAYWRITE
;
1007 pfn
= virt_to_phys(kvaddr
) >> PAGE_SHIFT
;
1008 ret
= remap_pfn_range(vma
, vma
->vm_start
, pfn
,
1009 len
, vma
->vm_page_prot
);
1011 dev_info(&dd
->pcidev
->dev
, "%s port%u mmap of %lx, %x "
1012 "bytes r%c failed: %d\n", what
, pd
->port_port
,
1013 pfn
, len
, write_ok
?'w':'o', ret
);
1015 ipath_cdbg(VERBOSE
, "%s port%u mmaped %lx, %x bytes "
1016 "r%c\n", what
, pd
->port_port
, pfn
, len
,
1022 static int mmap_ureg(struct vm_area_struct
*vma
, struct ipath_devdata
*dd
,
1029 * This is real hardware, so use io_remap. This is the mechanism
1030 * for the user process to update the head registers for their port
1033 if ((vma
->vm_end
- vma
->vm_start
) > PAGE_SIZE
) {
1034 dev_info(&dd
->pcidev
->dev
, "FAIL mmap userreg: reqlen "
1035 "%lx > PAGE\n", vma
->vm_end
- vma
->vm_start
);
1038 phys
= dd
->ipath_physaddr
+ ureg
;
1039 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1041 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
1042 ret
= io_remap_pfn_range(vma
, vma
->vm_start
,
1044 vma
->vm_end
- vma
->vm_start
,
1050 static int mmap_piobufs(struct vm_area_struct
*vma
,
1051 struct ipath_devdata
*dd
,
1052 struct ipath_portdata
*pd
,
1053 unsigned piobufs
, unsigned piocnt
)
1059 * When we map the PIO buffers in the chip, we want to map them as
1060 * writeonly, no read possible. This prevents access to previous
1061 * process data, and catches users who might try to read the i/o
1062 * space due to a bug.
1064 if ((vma
->vm_end
- vma
->vm_start
) > (piocnt
* dd
->ipath_palign
)) {
1065 dev_info(&dd
->pcidev
->dev
, "FAIL mmap piobufs: "
1066 "reqlen %lx > PAGE\n",
1067 vma
->vm_end
- vma
->vm_start
);
1072 phys
= dd
->ipath_physaddr
+ piobufs
;
1074 #if defined(__powerpc__)
1075 /* There isn't a generic way to specify writethrough mappings */
1076 pgprot_val(vma
->vm_page_prot
) |= _PAGE_NO_CACHE
;
1077 pgprot_val(vma
->vm_page_prot
) |= _PAGE_WRITETHRU
;
1078 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_GUARDED
;
1082 * don't allow them to later change to readable with mprotect (for when
1083 * not initially mapped readable, as is normally the case)
1085 vma
->vm_flags
&= ~VM_MAYREAD
;
1086 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
1088 ret
= io_remap_pfn_range(vma
, vma
->vm_start
, phys
>> PAGE_SHIFT
,
1089 vma
->vm_end
- vma
->vm_start
,
1095 static int mmap_rcvegrbufs(struct vm_area_struct
*vma
,
1096 struct ipath_portdata
*pd
)
1098 struct ipath_devdata
*dd
= pd
->port_dd
;
1099 unsigned long start
, size
;
1100 size_t total_size
, i
;
1104 size
= pd
->port_rcvegrbuf_size
;
1105 total_size
= pd
->port_rcvegrbuf_chunks
* size
;
1106 if ((vma
->vm_end
- vma
->vm_start
) > total_size
) {
1107 dev_info(&dd
->pcidev
->dev
, "FAIL on egr bufs: "
1108 "reqlen %lx > actual %lx\n",
1109 vma
->vm_end
- vma
->vm_start
,
1110 (unsigned long) total_size
);
1115 if (vma
->vm_flags
& VM_WRITE
) {
1116 dev_info(&dd
->pcidev
->dev
, "Can't map eager buffers as "
1117 "writable (flags=%lx)\n", vma
->vm_flags
);
1121 /* don't allow them to later change to writeable with mprotect */
1122 vma
->vm_flags
&= ~VM_MAYWRITE
;
1124 start
= vma
->vm_start
;
1126 for (i
= 0; i
< pd
->port_rcvegrbuf_chunks
; i
++, start
+= size
) {
1127 pfn
= virt_to_phys(pd
->port_rcvegrbuf
[i
]) >> PAGE_SHIFT
;
1128 ret
= remap_pfn_range(vma
, start
, pfn
, size
,
1140 * ipath_file_vma_fault - handle a VMA page fault.
1142 static int ipath_file_vma_fault(struct vm_area_struct
*vma
,
1143 struct vm_fault
*vmf
)
1147 page
= vmalloc_to_page((void *)(vmf
->pgoff
<< PAGE_SHIFT
));
1149 return VM_FAULT_SIGBUS
;
1156 static const struct vm_operations_struct ipath_file_vm_ops
= {
1157 .fault
= ipath_file_vma_fault
,
1160 static int mmap_kvaddr(struct vm_area_struct
*vma
, u64 pgaddr
,
1161 struct ipath_portdata
*pd
, unsigned subport
)
1164 struct ipath_devdata
*dd
;
1169 /* If the port is not shared, all addresses should be physical */
1170 if (!pd
->port_subport_cnt
)
1174 size
= pd
->port_rcvegrbuf_chunks
* pd
->port_rcvegrbuf_size
;
1177 * Each process has all the subport uregbase, rcvhdrq, and
1178 * rcvegrbufs mmapped - as an array for all the processes,
1179 * and also separately for this process.
1181 if (pgaddr
== cvt_kvaddr(pd
->subport_uregbase
)) {
1182 addr
= pd
->subport_uregbase
;
1183 size
= PAGE_SIZE
* pd
->port_subport_cnt
;
1184 } else if (pgaddr
== cvt_kvaddr(pd
->subport_rcvhdr_base
)) {
1185 addr
= pd
->subport_rcvhdr_base
;
1186 size
= pd
->port_rcvhdrq_size
* pd
->port_subport_cnt
;
1187 } else if (pgaddr
== cvt_kvaddr(pd
->subport_rcvegrbuf
)) {
1188 addr
= pd
->subport_rcvegrbuf
;
1189 size
*= pd
->port_subport_cnt
;
1190 } else if (pgaddr
== cvt_kvaddr(pd
->subport_uregbase
+
1191 PAGE_SIZE
* subport
)) {
1192 addr
= pd
->subport_uregbase
+ PAGE_SIZE
* subport
;
1194 } else if (pgaddr
== cvt_kvaddr(pd
->subport_rcvhdr_base
+
1195 pd
->port_rcvhdrq_size
* subport
)) {
1196 addr
= pd
->subport_rcvhdr_base
+
1197 pd
->port_rcvhdrq_size
* subport
;
1198 size
= pd
->port_rcvhdrq_size
;
1199 } else if (pgaddr
== cvt_kvaddr(pd
->subport_rcvegrbuf
+
1201 addr
= pd
->subport_rcvegrbuf
+ size
* subport
;
1202 /* rcvegrbufs are read-only on the slave */
1203 if (vma
->vm_flags
& VM_WRITE
) {
1204 dev_info(&dd
->pcidev
->dev
,
1205 "Can't map eager buffers as "
1206 "writable (flags=%lx)\n", vma
->vm_flags
);
1211 * Don't allow permission to later change to writeable
1214 vma
->vm_flags
&= ~VM_MAYWRITE
;
1218 len
= vma
->vm_end
- vma
->vm_start
;
1220 ipath_cdbg(MM
, "FAIL: reqlen %lx > %zx\n", len
, size
);
1225 vma
->vm_pgoff
= (unsigned long) addr
>> PAGE_SHIFT
;
1226 vma
->vm_ops
= &ipath_file_vm_ops
;
1227 vma
->vm_flags
|= VM_RESERVED
| VM_DONTEXPAND
;
1235 * ipath_mmap - mmap various structures into user space
1236 * @fp: the file pointer
1239 * We use this to have a shared buffer between the kernel and the user code
1240 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1241 * buffers in the chip. We have the open and close entries so we can bump
1242 * the ref count and keep the driver from being unloaded while still mapped.
1244 static int ipath_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
1246 struct ipath_portdata
*pd
;
1247 struct ipath_devdata
*dd
;
1249 unsigned piobufs
, piocnt
;
1260 * This is the ipath_do_user_init() code, mapping the shared buffers
1261 * into the user process. The address referred to by vm_pgoff is the
1262 * file offset passed via mmap(). For shared ports, this is the
1263 * kernel vmalloc() address of the pages to share with the master.
1264 * For non-shared or master ports, this is a physical address.
1265 * We only do one mmap for each space mapped.
1267 pgaddr
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1270 * Check for 0 in case one of the allocations failed, but user
1271 * called mmap anyway.
1278 ipath_cdbg(MM
, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
1279 (unsigned long long) pgaddr
, vma
->vm_start
,
1280 vma
->vm_end
- vma
->vm_start
, dd
->ipath_unit
,
1281 pd
->port_port
, subport_fp(fp
));
1284 * Physical addresses must fit in 40 bits for our hardware.
1285 * Check for kernel virtual addresses first, anything else must
1286 * match a HW or memory address.
1288 ret
= mmap_kvaddr(vma
, pgaddr
, pd
, subport_fp(fp
));
1295 ureg
= dd
->ipath_uregbase
+ dd
->ipath_ureg_align
* pd
->port_port
;
1296 if (!pd
->port_subport_cnt
) {
1297 /* port is not shared */
1298 piocnt
= pd
->port_piocnt
;
1299 piobufs
= pd
->port_piobufs
;
1300 } else if (!subport_fp(fp
)) {
1301 /* caller is the master */
1302 piocnt
= (pd
->port_piocnt
/ pd
->port_subport_cnt
) +
1303 (pd
->port_piocnt
% pd
->port_subport_cnt
);
1304 piobufs
= pd
->port_piobufs
+
1305 dd
->ipath_palign
* (pd
->port_piocnt
- piocnt
);
1307 unsigned slave
= subport_fp(fp
) - 1;
1309 /* caller is a slave */
1310 piocnt
= pd
->port_piocnt
/ pd
->port_subport_cnt
;
1311 piobufs
= pd
->port_piobufs
+ dd
->ipath_palign
* piocnt
* slave
;
1315 ret
= mmap_ureg(vma
, dd
, ureg
);
1316 else if (pgaddr
== piobufs
)
1317 ret
= mmap_piobufs(vma
, dd
, pd
, piobufs
, piocnt
);
1318 else if (pgaddr
== dd
->ipath_pioavailregs_phys
)
1319 /* in-memory copy of pioavail registers */
1320 ret
= ipath_mmap_mem(vma
, pd
, PAGE_SIZE
, 0,
1321 (void *) dd
->ipath_pioavailregs_dma
,
1322 "pioavail registers");
1323 else if (pgaddr
== pd
->port_rcvegr_phys
)
1324 ret
= mmap_rcvegrbufs(vma
, pd
);
1325 else if (pgaddr
== (u64
) pd
->port_rcvhdrq_phys
)
1327 * The rcvhdrq itself; readonly except on HT (so have
1328 * to allow writable mapping), multiple pages, contiguous
1329 * from an i/o perspective.
1331 ret
= ipath_mmap_mem(vma
, pd
, pd
->port_rcvhdrq_size
, 1,
1334 else if (pgaddr
== (u64
) pd
->port_rcvhdrqtailaddr_phys
)
1335 /* in-memory copy of rcvhdrq tail register */
1336 ret
= ipath_mmap_mem(vma
, pd
, PAGE_SIZE
, 0,
1337 pd
->port_rcvhdrtail_kvaddr
,
1342 vma
->vm_private_data
= NULL
;
1345 dev_info(&dd
->pcidev
->dev
,
1346 "Failure %d on off %llx len %lx\n",
1347 -ret
, (unsigned long long)pgaddr
,
1348 vma
->vm_end
- vma
->vm_start
);
1353 static unsigned ipath_poll_hdrqfull(struct ipath_portdata
*pd
)
1355 unsigned pollflag
= 0;
1357 if ((pd
->poll_type
& IPATH_POLL_TYPE_OVERFLOW
) &&
1358 pd
->port_hdrqfull
!= pd
->port_hdrqfull_poll
) {
1359 pollflag
|= POLLIN
| POLLRDNORM
;
1360 pd
->port_hdrqfull_poll
= pd
->port_hdrqfull
;
1366 static unsigned int ipath_poll_urgent(struct ipath_portdata
*pd
,
1368 struct poll_table_struct
*pt
)
1370 unsigned pollflag
= 0;
1371 struct ipath_devdata
*dd
;
1375 /* variable access in ipath_poll_hdrqfull() needs this */
1377 pollflag
= ipath_poll_hdrqfull(pd
);
1379 if (pd
->port_urgent
!= pd
->port_urgent_poll
) {
1380 pollflag
|= POLLIN
| POLLRDNORM
;
1381 pd
->port_urgent_poll
= pd
->port_urgent
;
1385 /* this saves a spin_lock/unlock in interrupt handler... */
1386 set_bit(IPATH_PORT_WAITING_URG
, &pd
->port_flag
);
1387 /* flush waiting flag so don't miss an event... */
1389 poll_wait(fp
, &pd
->port_wait
, pt
);
1395 static unsigned int ipath_poll_next(struct ipath_portdata
*pd
,
1397 struct poll_table_struct
*pt
)
1401 unsigned pollflag
= 0;
1402 struct ipath_devdata
*dd
;
1406 /* variable access in ipath_poll_hdrqfull() needs this */
1408 pollflag
= ipath_poll_hdrqfull(pd
);
1410 head
= ipath_read_ureg32(dd
, ur_rcvhdrhead
, pd
->port_port
);
1411 if (pd
->port_rcvhdrtail_kvaddr
)
1412 tail
= ipath_get_rcvhdrtail(pd
);
1414 tail
= ipath_read_ureg32(dd
, ur_rcvhdrtail
, pd
->port_port
);
1417 pollflag
|= POLLIN
| POLLRDNORM
;
1419 /* this saves a spin_lock/unlock in interrupt handler */
1420 set_bit(IPATH_PORT_WAITING_RCV
, &pd
->port_flag
);
1421 /* flush waiting flag so we don't miss an event */
1424 set_bit(pd
->port_port
+ dd
->ipath_r_intravail_shift
,
1425 &dd
->ipath_rcvctrl
);
1427 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1430 if (dd
->ipath_rhdrhead_intr_off
) /* arm rcv interrupt */
1431 ipath_write_ureg(dd
, ur_rcvhdrhead
,
1432 dd
->ipath_rhdrhead_intr_off
| head
,
1435 poll_wait(fp
, &pd
->port_wait
, pt
);
1441 static unsigned int ipath_poll(struct file
*fp
,
1442 struct poll_table_struct
*pt
)
1444 struct ipath_portdata
*pd
;
1450 else if (pd
->poll_type
& IPATH_POLL_TYPE_URGENT
)
1451 pollflag
= ipath_poll_urgent(pd
, fp
, pt
);
1453 pollflag
= ipath_poll_next(pd
, fp
, pt
);
1458 static int ipath_supports_subports(int user_swmajor
, int user_swminor
)
1460 /* no subport implementation prior to software version 1.3 */
1461 return (user_swmajor
> 1) || (user_swminor
>= 3);
1464 static int ipath_compatible_subports(int user_swmajor
, int user_swminor
)
1466 /* this code is written long-hand for clarity */
1467 if (IPATH_USER_SWMAJOR
!= user_swmajor
) {
1468 /* no promise of compatibility if major mismatch */
1471 if (IPATH_USER_SWMAJOR
== 1) {
1472 switch (IPATH_USER_SWMINOR
) {
1476 /* no subport implementation so cannot be compatible */
1479 /* 3 is only compatible with itself */
1480 return user_swminor
== 3;
1482 /* >= 4 are compatible (or are expected to be) */
1483 return user_swminor
>= 4;
1486 /* make no promises yet for future major versions */
1490 static int init_subports(struct ipath_devdata
*dd
,
1491 struct ipath_portdata
*pd
,
1492 const struct ipath_user_info
*uinfo
)
1495 unsigned num_subports
;
1499 * If the user is requesting zero subports,
1500 * skip the subport allocation.
1502 if (uinfo
->spu_subport_cnt
<= 0)
1505 /* Self-consistency check for ipath_compatible_subports() */
1506 if (ipath_supports_subports(IPATH_USER_SWMAJOR
, IPATH_USER_SWMINOR
) &&
1507 !ipath_compatible_subports(IPATH_USER_SWMAJOR
,
1508 IPATH_USER_SWMINOR
)) {
1509 dev_info(&dd
->pcidev
->dev
,
1510 "Inconsistent ipath_compatible_subports()\n");
1514 /* Check for subport compatibility */
1515 if (!ipath_compatible_subports(uinfo
->spu_userversion
>> 16,
1516 uinfo
->spu_userversion
& 0xffff)) {
1517 dev_info(&dd
->pcidev
->dev
,
1518 "Mismatched user version (%d.%d) and driver "
1519 "version (%d.%d) while port sharing. Ensure "
1520 "that driver and library are from the same "
1522 (int) (uinfo
->spu_userversion
>> 16),
1523 (int) (uinfo
->spu_userversion
& 0xffff),
1525 IPATH_USER_SWMINOR
);
1528 if (uinfo
->spu_subport_cnt
> INFINIPATH_MAX_SUBPORT
) {
1533 num_subports
= uinfo
->spu_subport_cnt
;
1534 pd
->subport_uregbase
= vmalloc(PAGE_SIZE
* num_subports
);
1535 if (!pd
->subport_uregbase
) {
1539 /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
1540 size
= ALIGN(dd
->ipath_rcvhdrcnt
* dd
->ipath_rcvhdrentsize
*
1541 sizeof(u32
), PAGE_SIZE
) * num_subports
;
1542 pd
->subport_rcvhdr_base
= vmalloc(size
);
1543 if (!pd
->subport_rcvhdr_base
) {
1548 pd
->subport_rcvegrbuf
= vmalloc(pd
->port_rcvegrbuf_chunks
*
1549 pd
->port_rcvegrbuf_size
*
1551 if (!pd
->subport_rcvegrbuf
) {
1556 pd
->port_subport_cnt
= uinfo
->spu_subport_cnt
;
1557 pd
->port_subport_id
= uinfo
->spu_subport_id
;
1558 pd
->active_slaves
= 1;
1559 set_bit(IPATH_PORT_MASTER_UNINIT
, &pd
->port_flag
);
1560 memset(pd
->subport_uregbase
, 0, PAGE_SIZE
* num_subports
);
1561 memset(pd
->subport_rcvhdr_base
, 0, size
);
1562 memset(pd
->subport_rcvegrbuf
, 0, pd
->port_rcvegrbuf_chunks
*
1563 pd
->port_rcvegrbuf_size
*
1568 vfree(pd
->subport_rcvhdr_base
);
1570 vfree(pd
->subport_uregbase
);
1571 pd
->subport_uregbase
= NULL
;
1576 static int try_alloc_port(struct ipath_devdata
*dd
, int port
,
1578 const struct ipath_user_info
*uinfo
)
1580 struct ipath_portdata
*pd
;
1583 if (!(pd
= dd
->ipath_pd
[port
])) {
1586 pd
= kzalloc(sizeof(struct ipath_portdata
), GFP_KERNEL
);
1589 * Allocate memory for use in ipath_tid_update() just once
1590 * at open, not per call. Reduces cost of expected send
1593 ptmp
= kmalloc(dd
->ipath_rcvtidcnt
* sizeof(u16
) +
1594 dd
->ipath_rcvtidcnt
* sizeof(struct page
**),
1597 ipath_dev_err(dd
, "Unable to allocate portdata "
1598 "memory, failing open\n");
1604 dd
->ipath_pd
[port
] = pd
;
1605 dd
->ipath_pd
[port
]->port_port
= port
;
1606 dd
->ipath_pd
[port
]->port_dd
= dd
;
1607 dd
->ipath_pd
[port
]->port_tid_pg_list
= ptmp
;
1608 init_waitqueue_head(&dd
->ipath_pd
[port
]->port_wait
);
1610 if (!pd
->port_cnt
) {
1611 pd
->userversion
= uinfo
->spu_userversion
;
1612 init_user_egr_sizes(pd
);
1613 if ((ret
= init_subports(dd
, pd
, uinfo
)) != 0)
1615 ipath_cdbg(PROC
, "%s[%u] opened unit:port %u:%u\n",
1616 current
->comm
, current
->pid
, dd
->ipath_unit
,
1620 pd
->port_pid
= get_pid(task_pid(current
));
1621 strlcpy(pd
->port_comm
, current
->comm
, sizeof(pd
->port_comm
));
1622 ipath_stats
.sps_ports
++;
1631 static inline int usable(struct ipath_devdata
*dd
)
1634 (dd
->ipath_flags
& IPATH_PRESENT
) &&
1635 dd
->ipath_kregbase
&&
1637 !(dd
->ipath_flags
& (IPATH_LINKDOWN
| IPATH_DISABLED
1641 static int find_free_port(int unit
, struct file
*fp
,
1642 const struct ipath_user_info
*uinfo
)
1644 struct ipath_devdata
*dd
= ipath_lookup(unit
);
1657 for (i
= 1; i
< dd
->ipath_cfgports
; i
++) {
1658 ret
= try_alloc_port(dd
, i
, fp
, uinfo
);
1668 static int find_best_unit(struct file
*fp
,
1669 const struct ipath_user_info
*uinfo
)
1671 int ret
= 0, i
, prefunit
= -1, devmax
;
1672 int maxofallports
, npresent
, nup
;
1675 devmax
= ipath_count_units(&npresent
, &nup
, &maxofallports
);
1678 * This code is present to allow a knowledgeable person to
1679 * specify the layout of processes to processors before opening
1680 * this driver, and then we'll assign the process to the "closest"
1681 * InfiniPath chip to that processor (we assume reasonable connectivity,
1682 * for now). This code assumes that if affinity has been set
1683 * before this point, that at most one cpu is set; for now this
1684 * is reasonable. I check for both cpumask_empty() and cpumask_full(),
1685 * in case some kernel variant sets none of the bits when no
1686 * affinity is set. 2.6.11 and 12 kernels have all present
1687 * cpus set. Some day we'll have to fix it up further to handle
1688 * a cpu subset. This algorithm fails for two HT chips connected
1689 * in tunnel fashion. Eventually this needs real topology
1690 * information. There may be some issues with dual core numbering
1691 * as well. This needs more work prior to release.
1693 if (!cpumask_empty(¤t
->cpus_allowed
) &&
1694 !cpumask_full(¤t
->cpus_allowed
)) {
1695 int ncpus
= num_online_cpus(), curcpu
= -1, nset
= 0;
1696 for (i
= 0; i
< ncpus
; i
++)
1697 if (cpumask_test_cpu(i
, ¤t
->cpus_allowed
)) {
1698 ipath_cdbg(PROC
, "%s[%u] affinity set for "
1699 "cpu %d/%d\n", current
->comm
,
1700 current
->pid
, i
, ncpus
);
1704 if (curcpu
!= -1 && nset
!= ncpus
) {
1706 prefunit
= curcpu
/ (ncpus
/ npresent
);
1707 ipath_cdbg(PROC
,"%s[%u] %d chips, %d cpus, "
1708 "%d cpus/chip, select unit %d\n",
1709 current
->comm
, current
->pid
,
1710 npresent
, ncpus
, ncpus
/ npresent
,
1717 * user ports start at 1, kernel port is 0
1718 * For now, we do round-robin access across all chips
1722 devmax
= prefunit
+ 1;
1724 for (i
= 1; i
< maxofallports
; i
++) {
1725 for (ndev
= prefunit
!= -1 ? prefunit
: 0; ndev
< devmax
;
1727 struct ipath_devdata
*dd
= ipath_lookup(ndev
);
1730 continue; /* can't use this unit */
1731 if (i
>= dd
->ipath_cfgports
)
1733 * Maxed out on users of this unit. Try
1737 ret
= try_alloc_port(dd
, i
, fp
, uinfo
);
1746 ipath_dbg("No ports available (none initialized "
1750 /* if started above 0, retry from 0 */
1752 "%s[%u] no ports on prefunit "
1753 "%d, clear and re-check\n",
1754 current
->comm
, current
->pid
,
1756 devmax
= ipath_count_units(NULL
, NULL
,
1762 ipath_dbg("No ports available\n");
1766 ipath_dbg("No boards found\n");
1773 static int find_shared_port(struct file
*fp
,
1774 const struct ipath_user_info
*uinfo
)
1776 int devmax
, ndev
, i
;
1779 devmax
= ipath_count_units(NULL
, NULL
, NULL
);
1781 for (ndev
= 0; ndev
< devmax
; ndev
++) {
1782 struct ipath_devdata
*dd
= ipath_lookup(ndev
);
1786 for (i
= 1; i
< dd
->ipath_cfgports
; i
++) {
1787 struct ipath_portdata
*pd
= dd
->ipath_pd
[i
];
1789 /* Skip ports which are not yet open */
1790 if (!pd
|| !pd
->port_cnt
)
1792 /* Skip port if it doesn't match the requested one */
1793 if (pd
->port_subport_id
!= uinfo
->spu_subport_id
)
1795 /* Verify the sharing process matches the master */
1796 if (pd
->port_subport_cnt
!= uinfo
->spu_subport_cnt
||
1797 pd
->userversion
!= uinfo
->spu_userversion
||
1798 pd
->port_cnt
>= pd
->port_subport_cnt
) {
1803 subport_fp(fp
) = pd
->port_cnt
++;
1804 pd
->port_subpid
[subport_fp(fp
)] =
1805 get_pid(task_pid(current
));
1806 tidcursor_fp(fp
) = 0;
1807 pd
->active_slaves
|= 1 << subport_fp(fp
);
1809 "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
1810 current
->comm
, current
->pid
,
1812 pd
->port_comm
, pid_nr(pd
->port_pid
),
1813 dd
->ipath_unit
, pd
->port_port
);
1823 static int ipath_open(struct inode
*in
, struct file
*fp
)
1825 /* The real work is performed later in ipath_assign_port() */
1826 fp
->private_data
= kzalloc(sizeof(struct ipath_filedata
), GFP_KERNEL
);
1827 return fp
->private_data
? 0 : -ENOMEM
;
1830 /* Get port early, so can set affinity prior to memory allocation */
1831 static int ipath_assign_port(struct file
*fp
,
1832 const struct ipath_user_info
*uinfo
)
1836 unsigned swmajor
, swminor
;
1838 /* Check to be sure we haven't already initialized this file */
1844 /* for now, if major version is different, bail */
1845 swmajor
= uinfo
->spu_userversion
>> 16;
1846 if (swmajor
!= IPATH_USER_SWMAJOR
) {
1847 ipath_dbg("User major version %d not same as driver "
1848 "major %d\n", uinfo
->spu_userversion
>> 16,
1849 IPATH_USER_SWMAJOR
);
1854 swminor
= uinfo
->spu_userversion
& 0xffff;
1855 if (swminor
!= IPATH_USER_SWMINOR
)
1856 ipath_dbg("User minor version %d not same as driver "
1857 "minor %d\n", swminor
, IPATH_USER_SWMINOR
);
1859 mutex_lock(&ipath_mutex
);
1861 if (ipath_compatible_subports(swmajor
, swminor
) &&
1862 uinfo
->spu_subport_cnt
&&
1863 (ret
= find_shared_port(fp
, uinfo
))) {
1869 i_minor
= iminor(fp
->f_path
.dentry
->d_inode
) - IPATH_USER_MINOR_BASE
;
1870 ipath_cdbg(VERBOSE
, "open on dev %lx (minor %d)\n",
1871 (long)fp
->f_path
.dentry
->d_inode
->i_rdev
, i_minor
);
1874 ret
= find_free_port(i_minor
- 1, fp
, uinfo
);
1876 ret
= find_best_unit(fp
, uinfo
);
1880 struct ipath_filedata
*fd
= fp
->private_data
;
1881 const struct ipath_portdata
*pd
= fd
->pd
;
1882 const struct ipath_devdata
*dd
= pd
->port_dd
;
1884 fd
->pq
= ipath_user_sdma_queue_create(&dd
->pcidev
->dev
,
1893 mutex_unlock(&ipath_mutex
);
1900 static int ipath_do_user_init(struct file
*fp
,
1901 const struct ipath_user_info
*uinfo
)
1904 struct ipath_portdata
*pd
= port_fp(fp
);
1905 struct ipath_devdata
*dd
;
1908 /* Subports don't need to initialize anything since master did it. */
1909 if (subport_fp(fp
)) {
1910 ret
= wait_event_interruptible(pd
->port_wait
,
1911 !test_bit(IPATH_PORT_MASTER_UNINIT
, &pd
->port_flag
));
1917 if (uinfo
->spu_rcvhdrsize
) {
1918 ret
= ipath_setrcvhdrsize(dd
, uinfo
->spu_rcvhdrsize
);
1923 /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
1925 /* some ports may get extra buffers, calculate that here */
1926 if (pd
->port_port
<= dd
->ipath_ports_extrabuf
)
1927 pd
->port_piocnt
= dd
->ipath_pbufsport
+ 1;
1929 pd
->port_piocnt
= dd
->ipath_pbufsport
;
1931 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
1932 if (pd
->port_port
<= dd
->ipath_ports_extrabuf
)
1933 pd
->port_pio_base
= (dd
->ipath_pbufsport
+ 1)
1934 * (pd
->port_port
- 1);
1936 pd
->port_pio_base
= dd
->ipath_ports_extrabuf
+
1937 dd
->ipath_pbufsport
* (pd
->port_port
- 1);
1938 pd
->port_piobufs
= dd
->ipath_piobufbase
+
1939 pd
->port_pio_base
* dd
->ipath_palign
;
1940 ipath_cdbg(VERBOSE
, "piobuf base for port %u is 0x%x, piocnt %u,"
1941 " first pio %u\n", pd
->port_port
, pd
->port_piobufs
,
1942 pd
->port_piocnt
, pd
->port_pio_base
);
1943 ipath_chg_pioavailkernel(dd
, pd
->port_pio_base
, pd
->port_piocnt
, 0);
1946 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
1947 * array for time being. If pd->port_port > chip-supported,
1948 * we need to do extra stuff here to handle by handling overflow
1949 * through port 0, someday
1951 ret
= ipath_create_rcvhdrq(dd
, pd
);
1953 ret
= ipath_create_user_egr(pd
);
1958 * set the eager head register for this port to the current values
1959 * of the tail pointers, since we don't know if they were
1960 * updated on last use of the port.
1962 head32
= ipath_read_ureg32(dd
, ur_rcvegrindextail
, pd
->port_port
);
1963 ipath_write_ureg(dd
, ur_rcvegrindexhead
, head32
, pd
->port_port
);
1964 pd
->port_lastrcvhdrqtail
= -1;
1965 ipath_cdbg(VERBOSE
, "Wrote port%d egrhead %x from tail regs\n",
1966 pd
->port_port
, head32
);
1967 pd
->port_tidcursor
= 0; /* start at beginning after open */
1969 /* initialize poll variables... */
1970 pd
->port_urgent
= 0;
1971 pd
->port_urgent_poll
= 0;
1972 pd
->port_hdrqfull_poll
= pd
->port_hdrqfull
;
1975 * Now enable the port for receive.
1976 * For chips that are set to DMA the tail register to memory
1977 * when they change (and when the update bit transitions from
1978 * 0 to 1. So for those chips, we turn it off and then back on.
1979 * This will (very briefly) affect any other open ports, but the
1980 * duration is very short, and therefore isn't an issue. We
1981 * explictly set the in-memory tail copy to 0 beforehand, so we
1982 * don't have to wait to be sure the DMA update has happened
1983 * (chip resets head/tail to 0 on transition to enable).
1985 set_bit(dd
->ipath_r_portenable_shift
+ pd
->port_port
,
1986 &dd
->ipath_rcvctrl
);
1987 if (!(dd
->ipath_flags
& IPATH_NODMA_RTAIL
)) {
1988 if (pd
->port_rcvhdrtail_kvaddr
)
1989 ipath_clear_rcvhdrtail(pd
);
1990 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1992 ~(1ULL << dd
->ipath_r_tailupd_shift
));
1994 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1996 /* Notify any waiting slaves */
1997 if (pd
->port_subport_cnt
) {
1998 clear_bit(IPATH_PORT_MASTER_UNINIT
, &pd
->port_flag
);
1999 wake_up(&pd
->port_wait
);
2006 * unlock_exptid - unlock any expected TID entries port still had in use
2009 * We don't actually update the chip here, because we do a bulk update
2010 * below, using ipath_f_clear_tids.
2012 static void unlock_expected_tids(struct ipath_portdata
*pd
)
2014 struct ipath_devdata
*dd
= pd
->port_dd
;
2015 int port_tidbase
= pd
->port_port
* dd
->ipath_rcvtidcnt
;
2016 int i
, cnt
= 0, maxtid
= port_tidbase
+ dd
->ipath_rcvtidcnt
;
2018 ipath_cdbg(VERBOSE
, "Port %u unlocking any locked expTID pages\n",
2020 for (i
= port_tidbase
; i
< maxtid
; i
++) {
2021 struct page
*ps
= dd
->ipath_pageshadow
[i
];
2026 dd
->ipath_pageshadow
[i
] = NULL
;
2027 pci_unmap_page(dd
->pcidev
, dd
->ipath_physshadow
[i
],
2028 PAGE_SIZE
, PCI_DMA_FROMDEVICE
);
2029 ipath_release_user_pages_on_close(&ps
, 1);
2031 ipath_stats
.sps_pageunlocks
++;
2034 ipath_cdbg(VERBOSE
, "Port %u locked %u expTID entries\n",
2035 pd
->port_port
, cnt
);
2037 if (ipath_stats
.sps_pagelocks
|| ipath_stats
.sps_pageunlocks
)
2038 ipath_cdbg(VERBOSE
, "%llu pages locked, %llu unlocked\n",
2039 (unsigned long long) ipath_stats
.sps_pagelocks
,
2040 (unsigned long long)
2041 ipath_stats
.sps_pageunlocks
);
2044 static int ipath_close(struct inode
*in
, struct file
*fp
)
2047 struct ipath_filedata
*fd
;
2048 struct ipath_portdata
*pd
;
2049 struct ipath_devdata
*dd
;
2050 unsigned long flags
;
2054 ipath_cdbg(VERBOSE
, "close on dev %lx, private data %p\n",
2055 (long)in
->i_rdev
, fp
->private_data
);
2057 mutex_lock(&ipath_mutex
);
2059 fd
= fp
->private_data
;
2060 fp
->private_data
= NULL
;
2063 mutex_unlock(&ipath_mutex
);
2069 /* drain user sdma queue */
2070 ipath_user_sdma_queue_drain(dd
, fd
->pq
);
2071 ipath_user_sdma_queue_destroy(fd
->pq
);
2073 if (--pd
->port_cnt
) {
2075 * XXX If the master closes the port before the slave(s),
2076 * revoke the mmap for the eager receive queue so
2077 * the slave(s) don't wait for receive data forever.
2079 pd
->active_slaves
&= ~(1 << fd
->subport
);
2080 put_pid(pd
->port_subpid
[fd
->subport
]);
2081 pd
->port_subpid
[fd
->subport
] = NULL
;
2082 mutex_unlock(&ipath_mutex
);
2085 /* early; no interrupt users after this */
2086 spin_lock_irqsave(&dd
->ipath_uctxt_lock
, flags
);
2087 port
= pd
->port_port
;
2088 dd
->ipath_pd
[port
] = NULL
;
2090 pd
->port_pid
= NULL
;
2091 spin_unlock_irqrestore(&dd
->ipath_uctxt_lock
, flags
);
2093 if (pd
->port_rcvwait_to
|| pd
->port_piowait_to
2094 || pd
->port_rcvnowait
|| pd
->port_pionowait
) {
2095 ipath_cdbg(VERBOSE
, "port%u, %u rcv, %u pio wait timeo; "
2096 "%u rcv %u, pio already\n",
2097 pd
->port_port
, pd
->port_rcvwait_to
,
2098 pd
->port_piowait_to
, pd
->port_rcvnowait
,
2099 pd
->port_pionowait
);
2100 pd
->port_rcvwait_to
= pd
->port_piowait_to
=
2101 pd
->port_rcvnowait
= pd
->port_pionowait
= 0;
2103 if (pd
->port_flag
) {
2104 ipath_cdbg(PROC
, "port %u port_flag set: 0x%lx\n",
2105 pd
->port_port
, pd
->port_flag
);
2109 if (dd
->ipath_kregbase
) {
2110 /* atomically clear receive enable port and intr avail. */
2111 clear_bit(dd
->ipath_r_portenable_shift
+ port
,
2112 &dd
->ipath_rcvctrl
);
2113 clear_bit(pd
->port_port
+ dd
->ipath_r_intravail_shift
,
2114 &dd
->ipath_rcvctrl
);
2115 ipath_write_kreg( dd
, dd
->ipath_kregs
->kr_rcvctrl
,
2117 /* and read back from chip to be sure that nothing
2118 * else is in flight when we do the rest */
2119 (void)ipath_read_kreg64(dd
, dd
->ipath_kregs
->kr_scratch
);
2121 /* clean up the pkeys for this port user */
2122 ipath_clean_part_key(pd
, dd
);
2124 * be paranoid, and never write 0's to these, just use an
2125 * unused part of the port 0 tail page. Of course,
2126 * rcvhdraddr points to a large chunk of memory, so this
2127 * could still trash things, but at least it won't trash
2128 * page 0, and by disabling the port, it should stop "soon",
2129 * even if a packet or two is in already in flight after we
2130 * disabled the port.
2132 ipath_write_kreg_port(dd
,
2133 dd
->ipath_kregs
->kr_rcvhdrtailaddr
, port
,
2134 dd
->ipath_dummy_hdrq_phys
);
2135 ipath_write_kreg_port(dd
, dd
->ipath_kregs
->kr_rcvhdraddr
,
2136 pd
->port_port
, dd
->ipath_dummy_hdrq_phys
);
2138 ipath_disarm_piobufs(dd
, pd
->port_pio_base
, pd
->port_piocnt
);
2139 ipath_chg_pioavailkernel(dd
, pd
->port_pio_base
,
2140 pd
->port_piocnt
, 1);
2142 dd
->ipath_f_clear_tids(dd
, pd
->port_port
);
2144 if (dd
->ipath_pageshadow
)
2145 unlock_expected_tids(pd
);
2146 ipath_stats
.sps_ports
--;
2147 ipath_cdbg(PROC
, "%s[%u] closed port %u:%u\n",
2148 pd
->port_comm
, pid_nr(pid
),
2149 dd
->ipath_unit
, port
);
2153 mutex_unlock(&ipath_mutex
);
2154 ipath_free_pddata(dd
, pd
); /* after releasing the mutex */
2161 static int ipath_port_info(struct ipath_portdata
*pd
, u16 subport
,
2162 struct ipath_port_info __user
*uinfo
)
2164 struct ipath_port_info info
;
2169 (void) ipath_count_units(NULL
, &nup
, NULL
);
2170 info
.num_active
= nup
;
2171 info
.unit
= pd
->port_dd
->ipath_unit
;
2172 info
.port
= pd
->port_port
;
2173 info
.subport
= subport
;
2174 /* Don't return new fields if old library opened the port. */
2175 if (ipath_supports_subports(pd
->userversion
>> 16,
2176 pd
->userversion
& 0xffff)) {
2177 /* Number of user ports available for this device. */
2178 info
.num_ports
= pd
->port_dd
->ipath_cfgports
- 1;
2179 info
.num_subports
= pd
->port_subport_cnt
;
2182 sz
= sizeof(info
) - 2 * sizeof(u16
);
2184 if (copy_to_user(uinfo
, &info
, sz
)) {
2194 static int ipath_get_slave_info(struct ipath_portdata
*pd
,
2195 void __user
*slave_mask_addr
)
2199 if (copy_to_user(slave_mask_addr
, &pd
->active_slaves
, sizeof(u32
)))
2204 static int ipath_sdma_get_inflight(struct ipath_user_sdma_queue
*pq
,
2205 u32 __user
*inflightp
)
2207 const u32 val
= ipath_user_sdma_inflight_counter(pq
);
2209 if (put_user(val
, inflightp
))
2215 static int ipath_sdma_get_complete(struct ipath_devdata
*dd
,
2216 struct ipath_user_sdma_queue
*pq
,
2217 u32 __user
*completep
)
2222 err
= ipath_user_sdma_make_progress(dd
, pq
);
2226 val
= ipath_user_sdma_complete_counter(pq
);
2227 if (put_user(val
, completep
))
2233 static ssize_t
ipath_write(struct file
*fp
, const char __user
*data
,
2234 size_t count
, loff_t
*off
)
2236 const struct ipath_cmd __user
*ucmd
;
2237 struct ipath_portdata
*pd
;
2238 const void __user
*src
;
2239 size_t consumed
, copy
;
2240 struct ipath_cmd cmd
;
2244 if (count
< sizeof(cmd
.type
)) {
2249 ucmd
= (const struct ipath_cmd __user
*) data
;
2251 if (copy_from_user(&cmd
.type
, &ucmd
->type
, sizeof(cmd
.type
))) {
2256 consumed
= sizeof(cmd
.type
);
2259 case IPATH_CMD_ASSIGN_PORT
:
2260 case __IPATH_CMD_USER_INIT
:
2261 case IPATH_CMD_USER_INIT
:
2262 copy
= sizeof(cmd
.cmd
.user_info
);
2263 dest
= &cmd
.cmd
.user_info
;
2264 src
= &ucmd
->cmd
.user_info
;
2266 case IPATH_CMD_RECV_CTRL
:
2267 copy
= sizeof(cmd
.cmd
.recv_ctrl
);
2268 dest
= &cmd
.cmd
.recv_ctrl
;
2269 src
= &ucmd
->cmd
.recv_ctrl
;
2271 case IPATH_CMD_PORT_INFO
:
2272 copy
= sizeof(cmd
.cmd
.port_info
);
2273 dest
= &cmd
.cmd
.port_info
;
2274 src
= &ucmd
->cmd
.port_info
;
2276 case IPATH_CMD_TID_UPDATE
:
2277 case IPATH_CMD_TID_FREE
:
2278 copy
= sizeof(cmd
.cmd
.tid_info
);
2279 dest
= &cmd
.cmd
.tid_info
;
2280 src
= &ucmd
->cmd
.tid_info
;
2282 case IPATH_CMD_SET_PART_KEY
:
2283 copy
= sizeof(cmd
.cmd
.part_key
);
2284 dest
= &cmd
.cmd
.part_key
;
2285 src
= &ucmd
->cmd
.part_key
;
2287 case __IPATH_CMD_SLAVE_INFO
:
2288 copy
= sizeof(cmd
.cmd
.slave_mask_addr
);
2289 dest
= &cmd
.cmd
.slave_mask_addr
;
2290 src
= &ucmd
->cmd
.slave_mask_addr
;
2292 case IPATH_CMD_PIOAVAILUPD
: // force an update of PIOAvail reg
2297 case IPATH_CMD_POLL_TYPE
:
2298 copy
= sizeof(cmd
.cmd
.poll_type
);
2299 dest
= &cmd
.cmd
.poll_type
;
2300 src
= &ucmd
->cmd
.poll_type
;
2302 case IPATH_CMD_ARMLAUNCH_CTRL
:
2303 copy
= sizeof(cmd
.cmd
.armlaunch_ctrl
);
2304 dest
= &cmd
.cmd
.armlaunch_ctrl
;
2305 src
= &ucmd
->cmd
.armlaunch_ctrl
;
2307 case IPATH_CMD_SDMA_INFLIGHT
:
2308 copy
= sizeof(cmd
.cmd
.sdma_inflight
);
2309 dest
= &cmd
.cmd
.sdma_inflight
;
2310 src
= &ucmd
->cmd
.sdma_inflight
;
2312 case IPATH_CMD_SDMA_COMPLETE
:
2313 copy
= sizeof(cmd
.cmd
.sdma_complete
);
2314 dest
= &cmd
.cmd
.sdma_complete
;
2315 src
= &ucmd
->cmd
.sdma_complete
;
2323 if ((count
- consumed
) < copy
) {
2328 if (copy_from_user(dest
, src
, copy
)) {
2337 if (!pd
&& cmd
.type
!= __IPATH_CMD_USER_INIT
&&
2338 cmd
.type
!= IPATH_CMD_ASSIGN_PORT
) {
2344 case IPATH_CMD_ASSIGN_PORT
:
2345 ret
= ipath_assign_port(fp
, &cmd
.cmd
.user_info
);
2349 case __IPATH_CMD_USER_INIT
:
2350 /* backwards compatibility, get port first */
2351 ret
= ipath_assign_port(fp
, &cmd
.cmd
.user_info
);
2354 /* and fall through to current version. */
2355 case IPATH_CMD_USER_INIT
:
2356 ret
= ipath_do_user_init(fp
, &cmd
.cmd
.user_info
);
2359 ret
= ipath_get_base_info(
2360 fp
, (void __user
*) (unsigned long)
2361 cmd
.cmd
.user_info
.spu_base_info
,
2362 cmd
.cmd
.user_info
.spu_base_info_size
);
2364 case IPATH_CMD_RECV_CTRL
:
2365 ret
= ipath_manage_rcvq(pd
, subport_fp(fp
), cmd
.cmd
.recv_ctrl
);
2367 case IPATH_CMD_PORT_INFO
:
2368 ret
= ipath_port_info(pd
, subport_fp(fp
),
2369 (struct ipath_port_info __user
*)
2370 (unsigned long) cmd
.cmd
.port_info
);
2372 case IPATH_CMD_TID_UPDATE
:
2373 ret
= ipath_tid_update(pd
, fp
, &cmd
.cmd
.tid_info
);
2375 case IPATH_CMD_TID_FREE
:
2376 ret
= ipath_tid_free(pd
, subport_fp(fp
), &cmd
.cmd
.tid_info
);
2378 case IPATH_CMD_SET_PART_KEY
:
2379 ret
= ipath_set_part_key(pd
, cmd
.cmd
.part_key
);
2381 case __IPATH_CMD_SLAVE_INFO
:
2382 ret
= ipath_get_slave_info(pd
,
2383 (void __user
*) (unsigned long)
2384 cmd
.cmd
.slave_mask_addr
);
2386 case IPATH_CMD_PIOAVAILUPD
:
2387 ipath_force_pio_avail_update(pd
->port_dd
);
2389 case IPATH_CMD_POLL_TYPE
:
2390 pd
->poll_type
= cmd
.cmd
.poll_type
;
2392 case IPATH_CMD_ARMLAUNCH_CTRL
:
2393 if (cmd
.cmd
.armlaunch_ctrl
)
2394 ipath_enable_armlaunch(pd
->port_dd
);
2396 ipath_disable_armlaunch(pd
->port_dd
);
2398 case IPATH_CMD_SDMA_INFLIGHT
:
2399 ret
= ipath_sdma_get_inflight(user_sdma_queue_fp(fp
),
2400 (u32 __user
*) (unsigned long)
2401 cmd
.cmd
.sdma_inflight
);
2403 case IPATH_CMD_SDMA_COMPLETE
:
2404 ret
= ipath_sdma_get_complete(pd
->port_dd
,
2405 user_sdma_queue_fp(fp
),
2406 (u32 __user
*) (unsigned long)
2407 cmd
.cmd
.sdma_complete
);
2418 static ssize_t
ipath_writev(struct kiocb
*iocb
, const struct iovec
*iov
,
2419 unsigned long dim
, loff_t off
)
2421 struct file
*filp
= iocb
->ki_filp
;
2422 struct ipath_filedata
*fp
= filp
->private_data
;
2423 struct ipath_portdata
*pd
= port_fp(filp
);
2424 struct ipath_user_sdma_queue
*pq
= fp
->pq
;
2429 return ipath_user_sdma_writev(pd
->port_dd
, pq
, iov
, dim
);
2432 static struct class *ipath_class
;
2434 static int init_cdev(int minor
, char *name
, const struct file_operations
*fops
,
2435 struct cdev
**cdevp
, struct device
**devp
)
2437 const dev_t dev
= MKDEV(IPATH_MAJOR
, minor
);
2438 struct cdev
*cdev
= NULL
;
2439 struct device
*device
= NULL
;
2442 cdev
= cdev_alloc();
2444 printk(KERN_ERR IPATH_DRV_NAME
2445 ": Could not allocate cdev for minor %d, %s\n",
2451 cdev
->owner
= THIS_MODULE
;
2453 kobject_set_name(&cdev
->kobj
, name
);
2455 ret
= cdev_add(cdev
, dev
, 1);
2457 printk(KERN_ERR IPATH_DRV_NAME
2458 ": Could not add cdev for minor %d, %s (err %d)\n",
2463 device
= device_create(ipath_class
, NULL
, dev
, NULL
, name
);
2465 if (IS_ERR(device
)) {
2466 ret
= PTR_ERR(device
);
2467 printk(KERN_ERR IPATH_DRV_NAME
": Could not create "
2468 "device for minor %d, %s (err %d)\n",
2491 int ipath_cdev_init(int minor
, char *name
, const struct file_operations
*fops
,
2492 struct cdev
**cdevp
, struct device
**devp
)
2494 return init_cdev(minor
, name
, fops
, cdevp
, devp
);
2497 static void cleanup_cdev(struct cdev
**cdevp
,
2498 struct device
**devp
)
2500 struct device
*dev
= *devp
;
2503 device_unregister(dev
);
2513 void ipath_cdev_cleanup(struct cdev
**cdevp
,
2514 struct device
**devp
)
2516 cleanup_cdev(cdevp
, devp
);
2519 static struct cdev
*wildcard_cdev
;
2520 static struct device
*wildcard_dev
;
2522 static const dev_t dev
= MKDEV(IPATH_MAJOR
, 0);
2524 static int user_init(void)
2528 ret
= register_chrdev_region(dev
, IPATH_NMINORS
, IPATH_DRV_NAME
);
2530 printk(KERN_ERR IPATH_DRV_NAME
": Could not register "
2531 "chrdev region (err %d)\n", -ret
);
2535 ipath_class
= class_create(THIS_MODULE
, IPATH_DRV_NAME
);
2537 if (IS_ERR(ipath_class
)) {
2538 ret
= PTR_ERR(ipath_class
);
2539 printk(KERN_ERR IPATH_DRV_NAME
": Could not create "
2540 "device class (err %d)\n", -ret
);
2546 unregister_chrdev_region(dev
, IPATH_NMINORS
);
2551 static void user_cleanup(void)
2554 class_destroy(ipath_class
);
2558 unregister_chrdev_region(dev
, IPATH_NMINORS
);
2561 static atomic_t user_count
= ATOMIC_INIT(0);
2562 static atomic_t user_setup
= ATOMIC_INIT(0);
2564 int ipath_user_add(struct ipath_devdata
*dd
)
2569 if (atomic_inc_return(&user_count
) == 1) {
2572 ipath_dev_err(dd
, "Unable to set up user support: "
2573 "error %d\n", -ret
);
2576 ret
= init_cdev(0, "ipath", &ipath_file_ops
, &wildcard_cdev
,
2579 ipath_dev_err(dd
, "Could not create wildcard "
2580 "minor: error %d\n", -ret
);
2584 atomic_set(&user_setup
, 1);
2587 snprintf(name
, sizeof(name
), "ipath%d", dd
->ipath_unit
);
2589 ret
= init_cdev(dd
->ipath_unit
+ 1, name
, &ipath_file_ops
,
2590 &dd
->user_cdev
, &dd
->user_dev
);
2592 ipath_dev_err(dd
, "Could not create user minor %d, %s\n",
2593 dd
->ipath_unit
+ 1, name
);
2603 void ipath_user_remove(struct ipath_devdata
*dd
)
2605 cleanup_cdev(&dd
->user_cdev
, &dd
->user_dev
);
2607 if (atomic_dec_return(&user_count
) == 0) {
2608 if (atomic_read(&user_setup
) == 0)
2611 cleanup_cdev(&wildcard_cdev
, &wildcard_dev
);
2614 atomic_set(&user_setup
, 0);