2 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/pci.h>
34 #include <linux/poll.h>
35 #include <linux/cdev.h>
36 #include <linux/swap.h>
37 #include <linux/vmalloc.h>
38 #include <asm/pgtable.h>
40 #include "ipath_kernel.h"
41 #include "ips_common.h"
42 #include "ipath_layer.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_rcv_egrbufs
= (u64
) pd
->port_rcvegr_phys
;
126 kinfo
->spi_pioavailaddr
= (u64
) dd
->ipath_pioavailregs_phys
;
127 kinfo
->spi_status
= (u64
) kinfo
->spi_pioavailaddr
+
128 (void *) dd
->ipath_statusp
-
129 (void *) dd
->ipath_pioavailregs_dma
;
130 kinfo
->spi_piobufbase
= (u64
) pd
->port_piobufs
;
131 kinfo
->__spi_uregbase
=
132 dd
->ipath_uregbase
+ dd
->ipath_palign
* pd
->port_port
;
134 kinfo
->spi_pioindex
= dd
->ipath_pbufsport
* (pd
->port_port
- 1);
135 kinfo
->spi_piocnt
= dd
->ipath_pbufsport
;
136 kinfo
->spi_pioalign
= dd
->ipath_palign
;
138 kinfo
->spi_qpair
= IPATH_KD_QP
;
139 kinfo
->spi_piosize
= dd
->ipath_ibmaxlen
;
140 kinfo
->spi_mtu
= dd
->ipath_ibmaxlen
; /* maxlen, not ibmtu */
141 kinfo
->spi_port
= pd
->port_port
;
142 kinfo
->spi_sw_version
= IPATH_KERN_SWVERSION
;
143 kinfo
->spi_hw_version
= dd
->ipath_revision
;
145 if (copy_to_user(ubase
, kinfo
, sizeof(*kinfo
)))
154 * ipath_tid_update - update a port TID
156 * @ti: the TID information
158 * The new implementation as of Oct 2004 is that the driver assigns
159 * the tid and returns it to the caller. To make it easier to
160 * catch bugs, and to reduce search time, we keep a cursor for
161 * each port, walking the shadow tid array to find one that's not
164 * For now, if we can't allocate the full list, we fail, although
165 * in the long run, we'll allocate as many as we can, and the
166 * caller will deal with that by trying the remaining pages later.
167 * That means that when we fail, we have to mark the tids as not in
168 * use again, in our shadow copy.
170 * It's up to the caller to free the tids when they are done.
171 * We'll unlock the pages as they free them.
173 * Also, right now we are locking one page at a time, but since
174 * the intended use of this routine is for a single group of
175 * virtually contiguous pages, that should change to improve
178 static int ipath_tid_update(struct ipath_portdata
*pd
,
179 const struct ipath_tid_info
*ti
)
182 u32 tid
, porttid
, cnt
, i
, tidcnt
;
184 struct ipath_devdata
*dd
= pd
->port_dd
;
187 u64 __iomem
*tidbase
;
188 unsigned long tidmap
[8];
189 struct page
**pagep
= NULL
;
191 if (!dd
->ipath_pageshadow
) {
198 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
199 (unsigned long long) ti
->tidlist
);
201 * Should we treat as success? likely a bug
206 tidcnt
= dd
->ipath_rcvtidcnt
;
208 /* make sure it all fits in port_tid_pg_list */
209 dev_info(&dd
->pcidev
->dev
, "Process tried to allocate %u "
210 "TIDs, only trying max (%u)\n", cnt
, tidcnt
);
213 pagep
= (struct page
**)pd
->port_tid_pg_list
;
214 tidlist
= (u16
*) (&pagep
[cnt
]);
216 memset(tidmap
, 0, sizeof(tidmap
));
217 tid
= pd
->port_tidcursor
;
218 /* before decrement; chip actual # */
219 porttid
= pd
->port_port
* tidcnt
;
221 tidbase
= (u64 __iomem
*) (((char __iomem
*) dd
->ipath_kregbase
) +
222 dd
->ipath_rcvtidbase
+
223 porttid
* sizeof(*tidbase
));
225 ipath_cdbg(VERBOSE
, "Port%u %u tids, cursor %u, tidbase %p\n",
226 pd
->port_port
, cnt
, tid
, tidbase
);
228 /* virtual address of first page in transfer */
229 vaddr
= ti
->tidvaddr
;
230 if (!access_ok(VERIFY_WRITE
, (void __user
*) vaddr
,
232 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
237 ret
= ipath_get_user_pages(vaddr
, cnt
, pagep
);
240 ipath_dbg("Failed to lock addr %p, %u pages "
241 "(already locked)\n",
242 (void *) vaddr
, cnt
);
244 * for now, continue, and see what happens but with
245 * the new implementation, this should never happen,
246 * unless perhaps the user has mpin'ed the pages
247 * themselves (something we need to test)
251 dev_info(&dd
->pcidev
->dev
,
252 "Failed to lock addr %p, %u pages: "
253 "errno %d\n", (void *) vaddr
, cnt
, -ret
);
257 for (i
= 0; i
< cnt
; i
++, vaddr
+= PAGE_SIZE
) {
258 for (; ntids
--; tid
++) {
261 if (!dd
->ipath_pageshadow
[porttid
+ tid
])
266 * oops, wrapped all the way through their TIDs,
267 * and didn't have enough free; see comments at
270 ipath_dbg("Not enough free TIDs for %u pages "
271 "(index %d), failing\n", cnt
, i
);
272 i
--; /* last tidlist[i] not filled in */
277 ipath_cdbg(VERBOSE
, "Updating idx %u to TID %u, "
278 "vaddr %lx\n", i
, tid
, vaddr
);
279 /* we "know" system pages and TID pages are same size */
280 dd
->ipath_pageshadow
[porttid
+ tid
] = pagep
[i
];
282 * don't need atomic or it's overhead
284 __set_bit(tid
, tidmap
);
285 physaddr
= page_to_phys(pagep
[i
]);
286 ipath_stats
.sps_pagelocks
++;
288 "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
289 tid
, vaddr
, (unsigned long long) physaddr
,
291 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
], 1, physaddr
);
293 * don't check this tid in ipath_portshadow, since we
294 * just filled it in; start with the next one.
302 /* jump here if copy out of updated info failed... */
303 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
305 /* same code that's in ipath_free_tid() */
306 limit
= sizeof(tidmap
) * BITS_PER_BYTE
;
308 /* just in case size changes in future */
310 tid
= find_first_bit((const unsigned long *)tidmap
, limit
);
311 for (; tid
< limit
; tid
++) {
312 if (!test_bit(tid
, tidmap
))
314 if (dd
->ipath_pageshadow
[porttid
+ tid
]) {
315 ipath_cdbg(VERBOSE
, "Freeing TID %u\n",
317 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
], 1,
318 dd
->ipath_tidinvalid
);
319 dd
->ipath_pageshadow
[porttid
+ tid
] = NULL
;
320 ipath_stats
.sps_pageunlocks
++;
323 ipath_release_user_pages(pagep
, cnt
);
326 * Copy the updated array, with ipath_tid's filled in, back
327 * to user. Since we did the copy in already, this "should
328 * never fail" If it does, we have to clean up...
330 if (copy_to_user((void __user
*)
331 (unsigned long) ti
->tidlist
,
332 tidlist
, cnt
* sizeof(*tidlist
))) {
336 if (copy_to_user((void __user
*) (unsigned long) ti
->tidmap
,
337 tidmap
, sizeof tidmap
)) {
343 pd
->port_tidcursor
= tid
;
348 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
354 * ipath_tid_free - free a port TID
358 * right now we are unlocking one page at a time, but since
359 * the intended use of this routine is for a single group of
360 * virtually contiguous pages, that should change to improve
361 * performance. We check that the TID is in range for this port
362 * but otherwise don't check validity; if user has an error and
363 * frees the wrong tid, it's only their own data that can thereby
364 * be corrupted. We do check that the TID was in use, for sanity
365 * We always use our idea of the saved address, not the address that
366 * they pass in to us.
369 static int ipath_tid_free(struct ipath_portdata
*pd
,
370 const struct ipath_tid_info
*ti
)
373 u32 tid
, porttid
, cnt
, limit
, tidcnt
;
374 struct ipath_devdata
*dd
= pd
->port_dd
;
375 u64 __iomem
*tidbase
;
376 unsigned long tidmap
[8];
378 if (!dd
->ipath_pageshadow
) {
383 if (copy_from_user(tidmap
, (void __user
*)(unsigned long)ti
->tidmap
,
389 porttid
= pd
->port_port
* dd
->ipath_rcvtidcnt
;
390 tidbase
= (u64 __iomem
*) ((char __iomem
*)(dd
->ipath_kregbase
) +
391 dd
->ipath_rcvtidbase
+
392 porttid
* sizeof(*tidbase
));
394 tidcnt
= dd
->ipath_rcvtidcnt
;
395 limit
= sizeof(tidmap
) * BITS_PER_BYTE
;
397 /* just in case size changes in future */
399 tid
= find_first_bit(tidmap
, limit
);
400 ipath_cdbg(VERBOSE
, "Port%u free %u tids; first bit (max=%d) "
401 "set is %d, porttid %u\n", pd
->port_port
, ti
->tidcnt
,
402 limit
, tid
, porttid
);
403 for (cnt
= 0; tid
< limit
; tid
++) {
405 * small optimization; if we detect a run of 3 or so without
406 * any set, use find_first_bit again. That's mainly to
407 * accelerate the case where we wrapped, so we have some at
408 * the beginning, and some at the end, and a big gap
411 if (!test_bit(tid
, tidmap
))
414 if (dd
->ipath_pageshadow
[porttid
+ tid
]) {
415 ipath_cdbg(VERBOSE
, "PID %u freeing TID %u\n",
417 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
], 1,
418 dd
->ipath_tidinvalid
);
419 ipath_release_user_pages(
420 &dd
->ipath_pageshadow
[porttid
+ tid
], 1);
421 dd
->ipath_pageshadow
[porttid
+ tid
] = NULL
;
422 ipath_stats
.sps_pageunlocks
++;
424 ipath_dbg("Unused tid %u, ignoring\n", tid
);
426 if (cnt
!= ti
->tidcnt
)
427 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
431 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
437 * ipath_set_part_key - set a partition key
441 * We can have up to 4 active at a time (other than the default, which is
442 * always allowed). This is somewhat tricky, since multiple ports may set
443 * the same key, so we reference count them, and clean up at exit. All 4
444 * partition keys are packed into a single infinipath register. It's an
445 * error for a process to set the same pkey multiple times. We provide no
446 * mechanism to de-allocate a pkey at this time, we may eventually need to
447 * do that. I've used the atomic operations, and no locking, and only make
448 * a single pass through what's available. This should be more than
449 * adequate for some time. I'll think about spinlocks or the like if and as
452 static int ipath_set_part_key(struct ipath_portdata
*pd
, u16 key
)
454 struct ipath_devdata
*dd
= pd
->port_dd
;
455 int i
, any
= 0, pidx
= -1;
456 u16 lkey
= key
& 0x7FFF;
459 if (lkey
== (IPS_DEFAULT_P_KEY
& 0x7FFF)) {
460 /* nothing to do; this key always valid */
465 ipath_cdbg(VERBOSE
, "p%u try to set pkey %hx, current keys "
466 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
467 pd
->port_port
, key
, dd
->ipath_pkeys
[0],
468 atomic_read(&dd
->ipath_pkeyrefs
[0]), dd
->ipath_pkeys
[1],
469 atomic_read(&dd
->ipath_pkeyrefs
[1]), dd
->ipath_pkeys
[2],
470 atomic_read(&dd
->ipath_pkeyrefs
[2]), dd
->ipath_pkeys
[3],
471 atomic_read(&dd
->ipath_pkeyrefs
[3]));
474 ipath_cdbg(PROC
, "p%u tries to set key 0, not allowed\n",
481 * Set the full membership bit, because it has to be
482 * set in the register or the packet, and it seems
483 * cleaner to set in the register than to force all
484 * callers to set it. (see bug 4331)
488 for (i
= 0; i
< ARRAY_SIZE(pd
->port_pkeys
); i
++) {
489 if (!pd
->port_pkeys
[i
] && pidx
== -1)
491 if (pd
->port_pkeys
[i
] == key
) {
492 ipath_cdbg(VERBOSE
, "p%u tries to set same pkey "
493 "(%x) more than once\n",
500 ipath_dbg("All pkeys for port %u already in use, "
501 "can't set %x\n", pd
->port_port
, key
);
505 for (any
= i
= 0; i
< ARRAY_SIZE(dd
->ipath_pkeys
); i
++) {
506 if (!dd
->ipath_pkeys
[i
]) {
510 if (dd
->ipath_pkeys
[i
] == key
) {
511 atomic_t
*pkrefs
= &dd
->ipath_pkeyrefs
[i
];
513 if (atomic_inc_return(pkrefs
) > 1) {
514 pd
->port_pkeys
[pidx
] = key
;
515 ipath_cdbg(VERBOSE
, "p%u set key %x "
516 "matches #%d, count now %d\n",
517 pd
->port_port
, key
, i
,
518 atomic_read(pkrefs
));
523 * lost race, decrement count, catch below
526 ipath_cdbg(VERBOSE
, "Lost race, count was "
527 "0, after dec, it's %d\n",
528 atomic_read(pkrefs
));
532 if ((dd
->ipath_pkeys
[i
] & 0x7FFF) == lkey
) {
534 * It makes no sense to have both the limited and
535 * full membership PKEY set at the same time since
536 * the unlimited one will disable the limited one.
543 ipath_dbg("port %u, all pkeys already in use, "
544 "can't set %x\n", pd
->port_port
, key
);
548 for (any
= i
= 0; i
< ARRAY_SIZE(dd
->ipath_pkeys
); i
++) {
549 if (!dd
->ipath_pkeys
[i
] &&
550 atomic_inc_return(&dd
->ipath_pkeyrefs
[i
]) == 1) {
553 /* for ipathstats, etc. */
554 ipath_stats
.sps_pkeys
[i
] = lkey
;
555 pd
->port_pkeys
[pidx
] = dd
->ipath_pkeys
[i
] = key
;
557 (u64
) dd
->ipath_pkeys
[0] |
558 ((u64
) dd
->ipath_pkeys
[1] << 16) |
559 ((u64
) dd
->ipath_pkeys
[2] << 32) |
560 ((u64
) dd
->ipath_pkeys
[3] << 48);
561 ipath_cdbg(PROC
, "p%u set key %x in #%d, "
562 "portidx %d, new pkey reg %llx\n",
563 pd
->port_port
, key
, i
, pidx
,
564 (unsigned long long) pkey
);
566 dd
, dd
->ipath_kregs
->kr_partitionkey
, pkey
);
572 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
573 "can't set %x\n", pd
->port_port
, key
);
581 * ipath_manage_rcvq - manage a port's receive queue
583 * @start_stop: action to carry out
585 * start_stop == 0 disables receive on the port, for use in queue
586 * overflow conditions. start_stop==1 re-enables, to be used to
587 * re-init the software copy of the head register
589 static int ipath_manage_rcvq(struct ipath_portdata
*pd
, int start_stop
)
591 struct ipath_devdata
*dd
= pd
->port_dd
;
594 ipath_cdbg(PROC
, "%sabling rcv for unit %u port %u\n",
595 start_stop
? "en" : "dis", dd
->ipath_unit
,
597 /* atomically clear receive enable port. */
600 * On enable, force in-memory copy of the tail register to
601 * 0, so that protocol code doesn't have to worry about
602 * whether or not the chip has yet updated the in-memory
603 * copy or not on return from the system call. The chip
604 * always resets it's tail register back to 0 on a
605 * transition from disabled to enabled. This could cause a
606 * problem if software was broken, and did the enable w/o
607 * the disable, but eventually the in-memory copy will be
608 * updated and correct itself, even in the face of software
611 *pd
->port_rcvhdrtail_kvaddr
= 0;
612 set_bit(INFINIPATH_R_PORTENABLE_SHIFT
+ pd
->port_port
,
615 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT
+ pd
->port_port
,
617 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
619 /* now be sure chip saw it before we return */
620 tval
= ipath_read_kreg64(dd
, dd
->ipath_kregs
->kr_scratch
);
623 * And try to be sure that tail reg update has happened too.
624 * This should in theory interlock with the RXE changes to
625 * the tail register. Don't assign it to the tail register
626 * in memory copy, since we could overwrite an update by the
629 tval
= ipath_read_ureg32(dd
, ur_rcvhdrtail
, pd
->port_port
);
631 /* always; new head should be equal to new tail; see above */
635 static void ipath_clean_part_key(struct ipath_portdata
*pd
,
636 struct ipath_devdata
*dd
)
638 int i
, j
, pchanged
= 0;
641 /* for debugging only */
642 oldpkey
= (u64
) dd
->ipath_pkeys
[0] |
643 ((u64
) dd
->ipath_pkeys
[1] << 16) |
644 ((u64
) dd
->ipath_pkeys
[2] << 32) |
645 ((u64
) dd
->ipath_pkeys
[3] << 48);
647 for (i
= 0; i
< ARRAY_SIZE(pd
->port_pkeys
); i
++) {
648 if (!pd
->port_pkeys
[i
])
650 ipath_cdbg(VERBOSE
, "look for key[%d] %hx in pkeys\n", i
,
652 for (j
= 0; j
< ARRAY_SIZE(dd
->ipath_pkeys
); j
++) {
653 /* check for match independent of the global bit */
654 if ((dd
->ipath_pkeys
[j
] & 0x7fff) !=
655 (pd
->port_pkeys
[i
] & 0x7fff))
657 if (atomic_dec_and_test(&dd
->ipath_pkeyrefs
[j
])) {
658 ipath_cdbg(VERBOSE
, "p%u clear key "
661 pd
->port_pkeys
[i
], j
);
662 ipath_stats
.sps_pkeys
[j
] =
663 dd
->ipath_pkeys
[j
] = 0;
667 VERBOSE
, "p%u key %x matches #%d, "
668 "but ref still %d\n", pd
->port_port
,
669 pd
->port_pkeys
[i
], j
,
670 atomic_read(&dd
->ipath_pkeyrefs
[j
]));
673 pd
->port_pkeys
[i
] = 0;
676 u64 pkey
= (u64
) dd
->ipath_pkeys
[0] |
677 ((u64
) dd
->ipath_pkeys
[1] << 16) |
678 ((u64
) dd
->ipath_pkeys
[2] << 32) |
679 ((u64
) dd
->ipath_pkeys
[3] << 48);
680 ipath_cdbg(VERBOSE
, "p%u old pkey reg %llx, "
681 "new pkey reg %llx\n", pd
->port_port
,
682 (unsigned long long) oldpkey
,
683 (unsigned long long) pkey
);
684 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_partitionkey
,
690 * ipath_create_user_egr - allocate eager TID buffers
691 * @pd: the port to allocate TID buffers for
693 * This routine is now quite different for user and kernel, because
694 * the kernel uses skb's, for the accelerated network performance
695 * This is the user port version
697 * Allocate the eager TID buffers and program them into infinipath
698 * They are no longer completely contiguous, we do multiple allocation
701 static int ipath_create_user_egr(struct ipath_portdata
*pd
)
703 struct ipath_devdata
*dd
= pd
->port_dd
;
704 unsigned e
, egrcnt
, alloced
, egrperchunk
, chunk
, egrsize
, egroff
;
708 egrcnt
= dd
->ipath_rcvegrcnt
;
709 /* TID number offset for this port */
710 egroff
= pd
->port_port
* egrcnt
;
711 egrsize
= dd
->ipath_rcvegrbufsize
;
712 ipath_cdbg(VERBOSE
, "Allocating %d egr buffers, at egrtid "
713 "offset %x, egrsize %u\n", egrcnt
, egroff
, egrsize
);
716 * to avoid wasting a lot of memory, we allocate 32KB chunks of
717 * physically contiguous memory, advance through it until used up
718 * and then allocate more. Of course, we need memory to store those
719 * extra pointers, now. Started out with 256KB, but under heavy
720 * memory pressure (creating large files and then copying them over
721 * NFS while doing lots of MPI jobs), we hit some allocation
722 * failures, even though we can sleep... (2.6.10) Still get
723 * failures at 64K. 32K is the lowest we can go without waiting
724 * more memory again. It seems likely that the coalescing in
725 * free_pages, etc. still has issues (as it has had previously
726 * during 2.6.x development).
729 alloced
= ALIGN(egrsize
* egrcnt
, size
);
730 egrperchunk
= size
/ egrsize
;
731 chunk
= (egrcnt
+ egrperchunk
- 1) / egrperchunk
;
732 pd
->port_rcvegrbuf_chunks
= chunk
;
733 pd
->port_rcvegrbufs_perchunk
= egrperchunk
;
734 pd
->port_rcvegrbuf_size
= size
;
735 pd
->port_rcvegrbuf
= vmalloc(chunk
* sizeof(pd
->port_rcvegrbuf
[0]));
736 if (!pd
->port_rcvegrbuf
) {
740 pd
->port_rcvegrbuf_phys
=
741 vmalloc(chunk
* sizeof(pd
->port_rcvegrbuf_phys
[0]));
742 if (!pd
->port_rcvegrbuf_phys
) {
746 for (e
= 0; e
< pd
->port_rcvegrbuf_chunks
; e
++) {
748 * GFP_USER, but without GFP_FS, so buffer cache can be
749 * coalesced (we hope); otherwise, even at order 4,
750 * heavy filesystem activity makes these fail
752 gfp_t gfp_flags
= __GFP_WAIT
| __GFP_IO
| __GFP_COMP
;
754 pd
->port_rcvegrbuf
[e
] = dma_alloc_coherent(
755 &dd
->pcidev
->dev
, size
, &pd
->port_rcvegrbuf_phys
[e
],
758 if (!pd
->port_rcvegrbuf
[e
]) {
760 goto bail_rcvegrbuf_phys
;
764 pd
->port_rcvegr_phys
= pd
->port_rcvegrbuf_phys
[0];
766 for (e
= chunk
= 0; chunk
< pd
->port_rcvegrbuf_chunks
; chunk
++) {
767 dma_addr_t pa
= pd
->port_rcvegrbuf_phys
[chunk
];
770 for (i
= 0; e
< egrcnt
&& i
< egrperchunk
; e
++, i
++) {
771 dd
->ipath_f_put_tid(dd
, e
+ egroff
+
775 dd
->ipath_rcvegrbase
), 0, pa
);
778 cond_resched(); /* don't hog the cpu */
785 for (e
= 0; e
< pd
->port_rcvegrbuf_chunks
&&
786 pd
->port_rcvegrbuf
[e
]; e
++)
787 dma_free_coherent(&dd
->pcidev
->dev
, size
,
788 pd
->port_rcvegrbuf
[e
],
789 pd
->port_rcvegrbuf_phys
[e
]);
791 vfree(pd
->port_rcvegrbuf_phys
);
792 pd
->port_rcvegrbuf_phys
= NULL
;
794 vfree(pd
->port_rcvegrbuf
);
795 pd
->port_rcvegrbuf
= NULL
;
800 static int ipath_do_user_init(struct ipath_portdata
*pd
,
801 const struct ipath_user_info
*uinfo
)
804 struct ipath_devdata
*dd
= pd
->port_dd
;
805 u64 physaddr
, uaddr
, off
, atmp
;
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 /* set up for the rcvhdr Q tail register writeback to user memory */
834 if (!uinfo
->spu_rcvhdraddr
||
835 !access_ok(VERIFY_WRITE
, (u64 __user
*) (unsigned long)
836 uinfo
->spu_rcvhdraddr
, sizeof(u64
))) {
837 ipath_dbg("Port %d rcvhdrtail addr %llx not valid\n",
839 (unsigned long long) uinfo
->spu_rcvhdraddr
);
844 off
= offset_in_page(uinfo
->spu_rcvhdraddr
);
845 uaddr
= PAGE_MASK
& (unsigned long) uinfo
->spu_rcvhdraddr
;
846 ret
= ipath_get_user_pages_nocopy(uaddr
, &pagep
);
848 dev_info(&dd
->pcidev
->dev
, "Failed to lookup and lock "
849 "address %llx for rcvhdrtail: errno %d\n",
850 (unsigned long long) uinfo
->spu_rcvhdraddr
, -ret
);
853 ipath_stats
.sps_pagelocks
++;
854 pd
->port_rcvhdrtail_uaddr
= uaddr
;
855 pd
->port_rcvhdrtail_pagep
= pagep
;
856 pd
->port_rcvhdrtail_kvaddr
=
858 pd
->port_rcvhdrtail_kvaddr
+= off
;
859 physaddr
= page_to_phys(pagep
) + off
;
860 ipath_cdbg(VERBOSE
, "port %d user addr %llx hdrtailaddr, %llx "
861 "physical (off=%llx)\n",
863 (unsigned long long) uinfo
->spu_rcvhdraddr
,
864 (unsigned long long) physaddr
, (unsigned long long) off
);
865 ipath_write_kreg_port(dd
, dd
->ipath_kregs
->kr_rcvhdrtailaddr
,
866 pd
->port_port
, physaddr
);
867 atmp
= ipath_read_kreg64_port(dd
,
868 dd
->ipath_kregs
->kr_rcvhdrtailaddr
,
870 if (physaddr
!= atmp
) {
872 "Catastrophic software error, "
873 "RcvHdrTailAddr%u written as %llx, "
874 "read back as %llx\n", pd
->port_port
,
875 (unsigned long long) physaddr
,
876 (unsigned long long) atmp
);
881 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
882 pd
->port_piobufs
= dd
->ipath_piobufbase
+
883 dd
->ipath_pbufsport
* (pd
->port_port
-
884 1) * dd
->ipath_palign
;
885 ipath_cdbg(VERBOSE
, "Set base of piobufs for port %u to 0x%x\n",
886 pd
->port_port
, pd
->port_piobufs
);
889 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
890 * array for time being. If pd->port_port > chip-supported,
891 * we need to do extra stuff here to handle by handling overflow
892 * through port 0, someday
894 ret
= ipath_create_rcvhdrq(dd
, pd
);
896 ret
= ipath_create_user_egr(pd
);
899 /* enable receives now */
900 /* atomically set enable bit for this port */
901 set_bit(INFINIPATH_R_PORTENABLE_SHIFT
+ pd
->port_port
,
905 * set the head registers for this port to the current values
906 * of the tail pointers, since we don't know if they were
907 * updated on last use of the port.
909 head32
= ipath_read_ureg32(dd
, ur_rcvhdrtail
, pd
->port_port
);
911 ipath_write_ureg(dd
, ur_rcvhdrhead
, head
, pd
->port_port
);
912 head32
= ipath_read_ureg32(dd
, ur_rcvegrindextail
, pd
->port_port
);
913 ipath_write_ureg(dd
, ur_rcvegrindexhead
, head32
, pd
->port_port
);
914 dd
->ipath_lastegrheads
[pd
->port_port
] = -1;
915 dd
->ipath_lastrcvhdrqtails
[pd
->port_port
] = -1;
916 ipath_cdbg(VERBOSE
, "Wrote port%d head %llx, egrhead %x from "
917 "tail regs\n", pd
->port_port
,
918 (unsigned long long) head
, head32
);
919 pd
->port_tidcursor
= 0; /* start at beginning after open */
921 * now enable the port; the tail registers will be written to memory
922 * by the chip as soon as it sees the write to
923 * dd->ipath_kregs->kr_rcvctrl. The update only happens on
924 * transition from 0 to 1, so clear it first, then set it as part of
925 * enabling the port. This will (very briefly) affect any other
926 * open ports, but it shouldn't be long enough to be an issue.
928 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
929 dd
->ipath_rcvctrl
& ~INFINIPATH_R_TAILUPD
);
930 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
937 static int mmap_ureg(struct vm_area_struct
*vma
, struct ipath_devdata
*dd
,
943 /* it's the real hardware, so io_remap works */
945 if ((vma
->vm_end
- vma
->vm_start
) > PAGE_SIZE
) {
946 dev_info(&dd
->pcidev
->dev
, "FAIL mmap userreg: reqlen "
947 "%lx > PAGE\n", vma
->vm_end
- vma
->vm_start
);
950 phys
= dd
->ipath_physaddr
+ ureg
;
951 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
953 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
954 ret
= io_remap_pfn_range(vma
, vma
->vm_start
,
956 vma
->vm_end
- vma
->vm_start
,
962 static int mmap_piobufs(struct vm_area_struct
*vma
,
963 struct ipath_devdata
*dd
,
964 struct ipath_portdata
*pd
)
970 * When we map the PIO buffers, we want to map them as writeonly, no
974 if ((vma
->vm_end
- vma
->vm_start
) >
975 (dd
->ipath_pbufsport
* dd
->ipath_palign
)) {
976 dev_info(&dd
->pcidev
->dev
, "FAIL mmap piobufs: "
977 "reqlen %lx > PAGE\n",
978 vma
->vm_end
- vma
->vm_start
);
983 phys
= dd
->ipath_physaddr
+ pd
->port_piobufs
;
985 * Do *NOT* mark this as non-cached (PWT bit), or we don't get the
986 * write combining behavior we want on the PIO buffers!
987 * vma->vm_page_prot =
988 * pgprot_noncached(vma->vm_page_prot);
991 if (vma
->vm_flags
& VM_READ
) {
992 dev_info(&dd
->pcidev
->dev
,
993 "Can't map piobufs as readable (flags=%lx)\n",
999 /* don't allow them to later change to readable with mprotect */
1001 vma
->vm_flags
&= ~VM_MAYWRITE
;
1002 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
1004 ret
= io_remap_pfn_range(vma
, vma
->vm_start
, phys
>> PAGE_SHIFT
,
1005 vma
->vm_end
- vma
->vm_start
,
1011 static int mmap_rcvegrbufs(struct vm_area_struct
*vma
,
1012 struct ipath_portdata
*pd
)
1014 struct ipath_devdata
*dd
= pd
->port_dd
;
1015 unsigned long start
, size
;
1016 size_t total_size
, i
;
1020 if (!pd
->port_rcvegrbuf
) {
1025 size
= pd
->port_rcvegrbuf_size
;
1026 total_size
= pd
->port_rcvegrbuf_chunks
* size
;
1027 if ((vma
->vm_end
- vma
->vm_start
) > total_size
) {
1028 dev_info(&dd
->pcidev
->dev
, "FAIL on egr bufs: "
1029 "reqlen %lx > actual %lx\n",
1030 vma
->vm_end
- vma
->vm_start
,
1031 (unsigned long) total_size
);
1036 if (vma
->vm_flags
& VM_WRITE
) {
1037 dev_info(&dd
->pcidev
->dev
, "Can't map eager buffers as "
1038 "writable (flags=%lx)\n", vma
->vm_flags
);
1043 start
= vma
->vm_start
;
1044 phys
= pd
->port_rcvegrbuf_phys
;
1046 /* don't allow them to later change to writeable with mprotect */
1047 vma
->vm_flags
&= ~VM_MAYWRITE
;
1049 for (i
= 0; i
< pd
->port_rcvegrbuf_chunks
; i
++, start
+= size
) {
1050 ret
= remap_pfn_range(vma
, start
, phys
[i
] >> PAGE_SHIFT
,
1051 size
, vma
->vm_page_prot
);
1061 static int mmap_rcvhdrq(struct vm_area_struct
*vma
,
1062 struct ipath_portdata
*pd
)
1064 struct ipath_devdata
*dd
= pd
->port_dd
;
1069 * kmalloc'ed memory, physically contiguous; this is from
1070 * spi_rcvhdr_base; we allow user to map read-write so they can
1071 * write hdrq entries to allow protocol code to directly poll
1072 * whether a hdrq entry has been written.
1074 total_size
= ALIGN(dd
->ipath_rcvhdrcnt
* dd
->ipath_rcvhdrentsize
*
1075 sizeof(u32
), PAGE_SIZE
);
1076 if ((vma
->vm_end
- vma
->vm_start
) > total_size
) {
1077 dev_info(&dd
->pcidev
->dev
,
1078 "FAIL on rcvhdrq: reqlen %lx > actual %lx\n",
1079 vma
->vm_end
- vma
->vm_start
,
1080 (unsigned long) total_size
);
1085 ret
= remap_pfn_range(vma
, vma
->vm_start
,
1086 pd
->port_rcvhdrq_phys
>> PAGE_SHIFT
,
1087 vma
->vm_end
- vma
->vm_start
,
1093 static int mmap_pioavailregs(struct vm_area_struct
*vma
,
1094 struct ipath_portdata
*pd
)
1096 struct ipath_devdata
*dd
= pd
->port_dd
;
1100 * when we map the PIO bufferavail registers, we want to map them as
1101 * readonly, no write possible.
1103 * kmalloc'ed memory, physically contiguous, one page only, readonly
1106 if ((vma
->vm_end
- vma
->vm_start
) > PAGE_SIZE
) {
1107 dev_info(&dd
->pcidev
->dev
, "FAIL on pioavailregs_dma: "
1108 "reqlen %lx > actual %lx\n",
1109 vma
->vm_end
- vma
->vm_start
,
1110 (unsigned long) PAGE_SIZE
);
1115 if (vma
->vm_flags
& VM_WRITE
) {
1116 dev_info(&dd
->pcidev
->dev
,
1117 "Can't map pioavailregs as writable (flags=%lx)\n",
1123 /* don't allow them to later change with mprotect */
1124 vma
->vm_flags
&= ~VM_MAYWRITE
;
1126 ret
= remap_pfn_range(vma
, vma
->vm_start
,
1127 dd
->ipath_pioavailregs_phys
>> PAGE_SHIFT
,
1128 PAGE_SIZE
, vma
->vm_page_prot
);
1134 * ipath_mmap - mmap various structures into user space
1135 * @fp: the file pointer
1138 * We use this to have a shared buffer between the kernel and the user code
1139 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1140 * buffers in the chip. We have the open and close entries so we can bump
1141 * the ref count and keep the driver from being unloaded while still mapped.
1143 static int ipath_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
1145 struct ipath_portdata
*pd
;
1146 struct ipath_devdata
*dd
;
1153 * This is the ipath_do_user_init() code, mapping the shared buffers
1154 * into the user process. The address referred to by vm_pgoff is the
1155 * virtual, not physical, address; we only do one mmap for each
1158 pgaddr
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1161 * note that ureg does *NOT* have the kregvirt as part of it, to be
1162 * sure that for 32 bit programs, we don't end up trying to map a >
1163 * 44 address. Has to match ipath_get_base_info() code that sets
1167 ureg
= dd
->ipath_uregbase
+ dd
->ipath_palign
* pd
->port_port
;
1169 ipath_cdbg(MM
, "ushare: pgaddr %llx vm_start=%lx, vmlen %lx\n",
1170 (unsigned long long) pgaddr
, vma
->vm_start
,
1171 vma
->vm_end
- vma
->vm_start
);
1174 ret
= mmap_ureg(vma
, dd
, ureg
);
1175 else if (pgaddr
== pd
->port_piobufs
)
1176 ret
= mmap_piobufs(vma
, dd
, pd
);
1177 else if (pgaddr
== (u64
) pd
->port_rcvegr_phys
)
1178 ret
= mmap_rcvegrbufs(vma
, pd
);
1179 else if (pgaddr
== (u64
) pd
->port_rcvhdrq_phys
)
1180 ret
= mmap_rcvhdrq(vma
, pd
);
1181 else if (pgaddr
== dd
->ipath_pioavailregs_phys
)
1182 ret
= mmap_pioavailregs(vma
, pd
);
1186 vma
->vm_private_data
= NULL
;
1189 dev_info(&dd
->pcidev
->dev
,
1190 "Failure %d on addr %lx, off %lx\n",
1191 -ret
, vma
->vm_start
, vma
->vm_pgoff
);
1196 static unsigned int ipath_poll(struct file
*fp
,
1197 struct poll_table_struct
*pt
)
1199 struct ipath_portdata
*pd
;
1202 struct ipath_devdata
*dd
;
1207 bit
= pd
->port_port
+ INFINIPATH_R_INTRAVAIL_SHIFT
;
1208 set_bit(bit
, &dd
->ipath_rcvctrl
);
1211 * Before blocking, make sure that head is still == tail,
1212 * reading from the chip, so we can be sure the interrupt
1213 * enable has made it to the chip. If not equal, disable
1214 * interrupt again and return immediately. This avoids races,
1215 * and the overhead of the chip read doesn't matter much at
1216 * this point, since we are waiting for something anyway.
1219 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1222 head
= ipath_read_ureg32(dd
, ur_rcvhdrhead
, pd
->port_port
);
1223 tail
= ipath_read_ureg32(dd
, ur_rcvhdrtail
, pd
->port_port
);
1226 set_bit(IPATH_PORT_WAITING_RCV
, &pd
->port_flag
);
1227 if(dd
->ipath_rhdrhead_intr_off
) /* arm rcv interrupt */
1228 (void)ipath_write_ureg(dd
, ur_rcvhdrhead
,
1229 dd
->ipath_rhdrhead_intr_off
1230 | head
, pd
->port_port
);
1231 poll_wait(fp
, &pd
->port_wait
, pt
);
1233 if (test_bit(IPATH_PORT_WAITING_RCV
, &pd
->port_flag
)) {
1234 /* timed out, no packets received */
1235 clear_bit(IPATH_PORT_WAITING_RCV
, &pd
->port_flag
);
1236 pd
->port_rcvwait_to
++;
1240 /* it's already happened; don't do wait_event overhead */
1241 pd
->port_rcvnowait
++;
1244 clear_bit(bit
, &dd
->ipath_rcvctrl
);
1245 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1251 static int try_alloc_port(struct ipath_devdata
*dd
, int port
,
1256 if (!dd
->ipath_pd
[port
]) {
1259 p
= kzalloc(sizeof(struct ipath_portdata
), GFP_KERNEL
);
1262 * Allocate memory for use in ipath_tid_update() just once
1263 * at open, not per call. Reduces cost of expected send
1266 ptmp
= kmalloc(dd
->ipath_rcvtidcnt
* sizeof(u16
) +
1267 dd
->ipath_rcvtidcnt
* sizeof(struct page
**),
1270 ipath_dev_err(dd
, "Unable to allocate portdata "
1271 "memory, failing open\n");
1277 dd
->ipath_pd
[port
] = p
;
1278 dd
->ipath_pd
[port
]->port_port
= port
;
1279 dd
->ipath_pd
[port
]->port_dd
= dd
;
1280 dd
->ipath_pd
[port
]->port_tid_pg_list
= ptmp
;
1281 init_waitqueue_head(&dd
->ipath_pd
[port
]->port_wait
);
1283 if (!dd
->ipath_pd
[port
]->port_cnt
) {
1284 dd
->ipath_pd
[port
]->port_cnt
= 1;
1285 fp
->private_data
= (void *) dd
->ipath_pd
[port
];
1286 ipath_cdbg(PROC
, "%s[%u] opened unit:port %u:%u\n",
1287 current
->comm
, current
->pid
, dd
->ipath_unit
,
1289 dd
->ipath_pd
[port
]->port_pid
= current
->pid
;
1290 strncpy(dd
->ipath_pd
[port
]->port_comm
, current
->comm
,
1291 sizeof(dd
->ipath_pd
[port
]->port_comm
));
1292 ipath_stats
.sps_ports
++;
1302 static inline int usable(struct ipath_devdata
*dd
)
1305 (dd
->ipath_flags
& IPATH_PRESENT
) &&
1306 dd
->ipath_kregbase
&&
1308 !(dd
->ipath_flags
& (IPATH_LINKDOWN
| IPATH_DISABLED
1312 static int find_free_port(int unit
, struct file
*fp
)
1314 struct ipath_devdata
*dd
= ipath_lookup(unit
);
1327 for (i
= 0; i
< dd
->ipath_cfgports
; i
++) {
1328 ret
= try_alloc_port(dd
, i
, fp
);
1338 static int find_best_unit(struct file
*fp
)
1340 int ret
= 0, i
, prefunit
= -1, devmax
;
1341 int maxofallports
, npresent
, nup
;
1344 (void) ipath_count_units(&npresent
, &nup
, &maxofallports
);
1347 * This code is present to allow a knowledgeable person to
1348 * specify the layout of processes to processors before opening
1349 * this driver, and then we'll assign the process to the "closest"
1350 * HT-400 to that processor (we assume reasonable connectivity,
1351 * for now). This code assumes that if affinity has been set
1352 * before this point, that at most one cpu is set; for now this
1353 * is reasonable. I check for both cpus_empty() and cpus_full(),
1354 * in case some kernel variant sets none of the bits when no
1355 * affinity is set. 2.6.11 and 12 kernels have all present
1356 * cpus set. Some day we'll have to fix it up further to handle
1357 * a cpu subset. This algorithm fails for two HT-400's connected
1358 * in tunnel fashion. Eventually this needs real topology
1359 * information. There may be some issues with dual core numbering
1360 * as well. This needs more work prior to release.
1362 if (!cpus_empty(current
->cpus_allowed
) &&
1363 !cpus_full(current
->cpus_allowed
)) {
1364 int ncpus
= num_online_cpus(), curcpu
= -1;
1365 for (i
= 0; i
< ncpus
; i
++)
1366 if (cpu_isset(i
, current
->cpus_allowed
)) {
1367 ipath_cdbg(PROC
, "%s[%u] affinity set for "
1368 "cpu %d\n", current
->comm
,
1374 prefunit
= curcpu
/ (ncpus
/ npresent
);
1375 ipath_dbg("%s[%u] %d chips, %d cpus, "
1376 "%d cpus/chip, select unit %d\n",
1377 current
->comm
, current
->pid
,
1378 npresent
, ncpus
, ncpus
/ npresent
,
1385 * user ports start at 1, kernel port is 0
1386 * For now, we do round-robin access across all chips
1390 devmax
= prefunit
+ 1;
1392 devmax
= ipath_count_units(NULL
, NULL
, NULL
);
1394 for (i
= 1; i
< maxofallports
; i
++) {
1395 for (ndev
= prefunit
!= -1 ? prefunit
: 0; ndev
< devmax
;
1397 struct ipath_devdata
*dd
= ipath_lookup(ndev
);
1400 continue; /* can't use this unit */
1401 if (i
>= dd
->ipath_cfgports
)
1403 * Maxed out on users of this unit. Try
1407 ret
= try_alloc_port(dd
, i
, fp
);
1416 ipath_dbg("No ports available (none initialized "
1420 /* if started above 0, retry from 0 */
1422 "%s[%u] no ports on prefunit "
1423 "%d, clear and re-check\n",
1424 current
->comm
, current
->pid
,
1426 devmax
= ipath_count_units(NULL
, NULL
,
1432 ipath_dbg("No ports available\n");
1436 ipath_dbg("No boards found\n");
1443 static int ipath_open(struct inode
*in
, struct file
*fp
)
1447 mutex_lock(&ipath_mutex
);
1450 ipath_cdbg(VERBOSE
, "open on dev %lx (minor %d)\n",
1451 (long)in
->i_rdev
, minor
);
1454 ret
= find_free_port(minor
- 1, fp
);
1456 ret
= find_best_unit(fp
);
1458 mutex_unlock(&ipath_mutex
);
1463 * unlock_exptid - unlock any expected TID entries port still had in use
1466 * We don't actually update the chip here, because we do a bulk update
1467 * below, using ipath_f_clear_tids.
1469 static void unlock_expected_tids(struct ipath_portdata
*pd
)
1471 struct ipath_devdata
*dd
= pd
->port_dd
;
1472 int port_tidbase
= pd
->port_port
* dd
->ipath_rcvtidcnt
;
1473 int i
, cnt
= 0, maxtid
= port_tidbase
+ dd
->ipath_rcvtidcnt
;
1475 ipath_cdbg(VERBOSE
, "Port %u unlocking any locked expTID pages\n",
1477 for (i
= port_tidbase
; i
< maxtid
; i
++) {
1478 if (!dd
->ipath_pageshadow
[i
])
1481 ipath_release_user_pages_on_close(&dd
->ipath_pageshadow
[i
],
1483 dd
->ipath_pageshadow
[i
] = NULL
;
1485 ipath_stats
.sps_pageunlocks
++;
1488 ipath_cdbg(VERBOSE
, "Port %u locked %u expTID entries\n",
1489 pd
->port_port
, cnt
);
1491 if (ipath_stats
.sps_pagelocks
|| ipath_stats
.sps_pageunlocks
)
1492 ipath_cdbg(VERBOSE
, "%llu pages locked, %llu unlocked\n",
1493 (unsigned long long) ipath_stats
.sps_pagelocks
,
1494 (unsigned long long)
1495 ipath_stats
.sps_pageunlocks
);
1498 static int ipath_close(struct inode
*in
, struct file
*fp
)
1501 struct ipath_portdata
*pd
;
1502 struct ipath_devdata
*dd
;
1505 ipath_cdbg(VERBOSE
, "close on dev %lx, private data %p\n",
1506 (long)in
->i_rdev
, fp
->private_data
);
1508 mutex_lock(&ipath_mutex
);
1511 port
= pd
->port_port
;
1512 fp
->private_data
= NULL
;
1515 if (pd
->port_hdrqfull
) {
1516 ipath_cdbg(PROC
, "%s[%u] had %u rcvhdrqfull errors "
1517 "during run\n", pd
->port_comm
, pd
->port_pid
,
1519 pd
->port_hdrqfull
= 0;
1522 if (pd
->port_rcvwait_to
|| pd
->port_piowait_to
1523 || pd
->port_rcvnowait
|| pd
->port_pionowait
) {
1524 ipath_cdbg(VERBOSE
, "port%u, %u rcv, %u pio wait timeo; "
1525 "%u rcv %u, pio already\n",
1526 pd
->port_port
, pd
->port_rcvwait_to
,
1527 pd
->port_piowait_to
, pd
->port_rcvnowait
,
1528 pd
->port_pionowait
);
1529 pd
->port_rcvwait_to
= pd
->port_piowait_to
=
1530 pd
->port_rcvnowait
= pd
->port_pionowait
= 0;
1532 if (pd
->port_flag
) {
1533 ipath_dbg("port %u port_flag still set to 0x%lx\n",
1534 pd
->port_port
, pd
->port_flag
);
1538 if (dd
->ipath_kregbase
) {
1539 if (pd
->port_rcvhdrtail_uaddr
) {
1540 pd
->port_rcvhdrtail_uaddr
= 0;
1541 pd
->port_rcvhdrtail_kvaddr
= NULL
;
1542 ipath_release_user_pages_on_close(
1543 &pd
->port_rcvhdrtail_pagep
, 1);
1544 pd
->port_rcvhdrtail_pagep
= NULL
;
1545 ipath_stats
.sps_pageunlocks
++;
1547 ipath_write_kreg_port(
1548 dd
, dd
->ipath_kregs
->kr_rcvhdrtailaddr
,
1550 ipath_write_kreg_port(
1551 dd
, dd
->ipath_kregs
->kr_rcvhdraddr
,
1554 /* clean up the pkeys for this port user */
1555 ipath_clean_part_key(pd
, dd
);
1557 if (port
< dd
->ipath_cfgports
) {
1558 int i
= dd
->ipath_pbufsport
* (port
- 1);
1559 ipath_disarm_piobufs(dd
, i
, dd
->ipath_pbufsport
);
1561 /* atomically clear receive enable port. */
1562 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT
+ port
,
1563 &dd
->ipath_rcvctrl
);
1566 dd
->ipath_kregs
->kr_rcvctrl
,
1569 if (dd
->ipath_pageshadow
)
1570 unlock_expected_tids(pd
);
1571 ipath_stats
.sps_ports
--;
1572 ipath_cdbg(PROC
, "%s[%u] closed port %u:%u\n",
1573 pd
->port_comm
, pd
->port_pid
,
1574 dd
->ipath_unit
, port
);
1581 dd
->ipath_f_clear_tids(dd
, pd
->port_port
);
1583 ipath_free_pddata(dd
, pd
->port_port
, 0);
1585 mutex_unlock(&ipath_mutex
);
1590 static int ipath_port_info(struct ipath_portdata
*pd
,
1591 struct ipath_port_info __user
*uinfo
)
1593 struct ipath_port_info info
;
1597 (void) ipath_count_units(NULL
, &nup
, NULL
);
1598 info
.num_active
= nup
;
1599 info
.unit
= pd
->port_dd
->ipath_unit
;
1600 info
.port
= pd
->port_port
;
1602 if (copy_to_user(uinfo
, &info
, sizeof(info
))) {
1612 static ssize_t
ipath_write(struct file
*fp
, const char __user
*data
,
1613 size_t count
, loff_t
*off
)
1615 const struct ipath_cmd __user
*ucmd
;
1616 struct ipath_portdata
*pd
;
1617 const void __user
*src
;
1618 size_t consumed
, copy
;
1619 struct ipath_cmd cmd
;
1623 if (count
< sizeof(cmd
.type
)) {
1628 ucmd
= (const struct ipath_cmd __user
*) data
;
1630 if (copy_from_user(&cmd
.type
, &ucmd
->type
, sizeof(cmd
.type
))) {
1635 consumed
= sizeof(cmd
.type
);
1638 case IPATH_CMD_USER_INIT
:
1639 copy
= sizeof(cmd
.cmd
.user_info
);
1640 dest
= &cmd
.cmd
.user_info
;
1641 src
= &ucmd
->cmd
.user_info
;
1643 case IPATH_CMD_RECV_CTRL
:
1644 copy
= sizeof(cmd
.cmd
.recv_ctrl
);
1645 dest
= &cmd
.cmd
.recv_ctrl
;
1646 src
= &ucmd
->cmd
.recv_ctrl
;
1648 case IPATH_CMD_PORT_INFO
:
1649 copy
= sizeof(cmd
.cmd
.port_info
);
1650 dest
= &cmd
.cmd
.port_info
;
1651 src
= &ucmd
->cmd
.port_info
;
1653 case IPATH_CMD_TID_UPDATE
:
1654 case IPATH_CMD_TID_FREE
:
1655 copy
= sizeof(cmd
.cmd
.tid_info
);
1656 dest
= &cmd
.cmd
.tid_info
;
1657 src
= &ucmd
->cmd
.tid_info
;
1659 case IPATH_CMD_SET_PART_KEY
:
1660 copy
= sizeof(cmd
.cmd
.part_key
);
1661 dest
= &cmd
.cmd
.part_key
;
1662 src
= &ucmd
->cmd
.part_key
;
1669 if ((count
- consumed
) < copy
) {
1674 if (copy_from_user(dest
, src
, copy
)) {
1683 case IPATH_CMD_USER_INIT
:
1684 ret
= ipath_do_user_init(pd
, &cmd
.cmd
.user_info
);
1687 ret
= ipath_get_base_info(
1688 pd
, (void __user
*) (unsigned long)
1689 cmd
.cmd
.user_info
.spu_base_info
,
1690 cmd
.cmd
.user_info
.spu_base_info_size
);
1692 case IPATH_CMD_RECV_CTRL
:
1693 ret
= ipath_manage_rcvq(pd
, cmd
.cmd
.recv_ctrl
);
1695 case IPATH_CMD_PORT_INFO
:
1696 ret
= ipath_port_info(pd
,
1697 (struct ipath_port_info __user
*)
1698 (unsigned long) cmd
.cmd
.port_info
);
1700 case IPATH_CMD_TID_UPDATE
:
1701 ret
= ipath_tid_update(pd
, &cmd
.cmd
.tid_info
);
1703 case IPATH_CMD_TID_FREE
:
1704 ret
= ipath_tid_free(pd
, &cmd
.cmd
.tid_info
);
1706 case IPATH_CMD_SET_PART_KEY
:
1707 ret
= ipath_set_part_key(pd
, cmd
.cmd
.part_key
);
1718 static struct class *ipath_class
;
1720 static int init_cdev(int minor
, char *name
, struct file_operations
*fops
,
1721 struct cdev
**cdevp
, struct class_device
**class_devp
)
1723 const dev_t dev
= MKDEV(IPATH_MAJOR
, minor
);
1724 struct cdev
*cdev
= NULL
;
1725 struct class_device
*class_dev
= NULL
;
1728 cdev
= cdev_alloc();
1730 printk(KERN_ERR IPATH_DRV_NAME
1731 ": Could not allocate cdev for minor %d, %s\n",
1737 cdev
->owner
= THIS_MODULE
;
1739 kobject_set_name(&cdev
->kobj
, name
);
1741 ret
= cdev_add(cdev
, dev
, 1);
1743 printk(KERN_ERR IPATH_DRV_NAME
1744 ": Could not add cdev for minor %d, %s (err %d)\n",
1749 class_dev
= class_device_create(ipath_class
, NULL
, dev
, NULL
, name
);
1751 if (IS_ERR(class_dev
)) {
1752 ret
= PTR_ERR(class_dev
);
1753 printk(KERN_ERR IPATH_DRV_NAME
": Could not create "
1754 "class_dev for minor %d, %s (err %d)\n",
1768 *class_devp
= class_dev
;
1777 int ipath_cdev_init(int minor
, char *name
, struct file_operations
*fops
,
1778 struct cdev
**cdevp
, struct class_device
**class_devp
)
1780 return init_cdev(minor
, name
, fops
, cdevp
, class_devp
);
1783 static void cleanup_cdev(struct cdev
**cdevp
,
1784 struct class_device
**class_devp
)
1786 struct class_device
*class_dev
= *class_devp
;
1789 class_device_unregister(class_dev
);
1799 void ipath_cdev_cleanup(struct cdev
**cdevp
,
1800 struct class_device
**class_devp
)
1802 cleanup_cdev(cdevp
, class_devp
);
1805 static struct cdev
*wildcard_cdev
;
1806 static struct class_device
*wildcard_class_dev
;
1808 static const dev_t dev
= MKDEV(IPATH_MAJOR
, 0);
1810 static int user_init(void)
1814 ret
= register_chrdev_region(dev
, IPATH_NMINORS
, IPATH_DRV_NAME
);
1816 printk(KERN_ERR IPATH_DRV_NAME
": Could not register "
1817 "chrdev region (err %d)\n", -ret
);
1821 ipath_class
= class_create(THIS_MODULE
, IPATH_DRV_NAME
);
1823 if (IS_ERR(ipath_class
)) {
1824 ret
= PTR_ERR(ipath_class
);
1825 printk(KERN_ERR IPATH_DRV_NAME
": Could not create "
1826 "device class (err %d)\n", -ret
);
1832 unregister_chrdev_region(dev
, IPATH_NMINORS
);
1837 static void user_cleanup(void)
1840 class_destroy(ipath_class
);
1844 unregister_chrdev_region(dev
, IPATH_NMINORS
);
1847 static atomic_t user_count
= ATOMIC_INIT(0);
1848 static atomic_t user_setup
= ATOMIC_INIT(0);
1850 int ipath_user_add(struct ipath_devdata
*dd
)
1855 if (atomic_inc_return(&user_count
) == 1) {
1858 ipath_dev_err(dd
, "Unable to set up user support: "
1859 "error %d\n", -ret
);
1862 ret
= ipath_diag_init();
1864 ipath_dev_err(dd
, "Unable to set up diag support: "
1865 "error %d\n", -ret
);
1869 ret
= init_cdev(0, "ipath", &ipath_file_ops
, &wildcard_cdev
,
1870 &wildcard_class_dev
);
1872 ipath_dev_err(dd
, "Could not create wildcard "
1873 "minor: error %d\n", -ret
);
1877 atomic_set(&user_setup
, 1);
1880 snprintf(name
, sizeof(name
), "ipath%d", dd
->ipath_unit
);
1882 ret
= init_cdev(dd
->ipath_unit
+ 1, name
, &ipath_file_ops
,
1883 &dd
->cdev
, &dd
->class_dev
);
1885 ipath_dev_err(dd
, "Could not create user minor %d, %s\n",
1886 dd
->ipath_unit
+ 1, name
);
1891 ipath_diag_cleanup();
1898 void ipath_user_del(struct ipath_devdata
*dd
)
1900 cleanup_cdev(&dd
->cdev
, &dd
->class_dev
);
1902 if (atomic_dec_return(&user_count
) == 0) {
1903 if (atomic_read(&user_setup
) == 0)
1906 cleanup_cdev(&wildcard_cdev
, &wildcard_class_dev
);
1907 ipath_diag_cleanup();
1910 atomic_set(&user_setup
, 0);