2 * Copyright (c) 2006, 2007 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 <asm/pgtable.h>
41 #include "ipath_kernel.h"
42 #include "ipath_common.h"
44 static int ipath_open(struct inode
*, struct file
*);
45 static int ipath_close(struct inode
*, struct file
*);
46 static ssize_t
ipath_write(struct file
*, const char __user
*, size_t,
48 static unsigned int ipath_poll(struct file
*, struct poll_table_struct
*);
49 static int ipath_mmap(struct file
*, struct vm_area_struct
*);
51 static const struct file_operations ipath_file_ops
= {
55 .release
= ipath_close
,
61 * Convert kernel virtual addresses to physical addresses so they don't
62 * potentially conflict with the chip addresses used as mmap offsets.
63 * It doesn't really matter what mmap offset we use as long as we can
64 * interpret it correctly.
66 static u64
cvt_kvaddr(void *p
)
71 page
= vmalloc_to_page(p
);
73 paddr
= page_to_pfn(page
) << PAGE_SHIFT
;
78 static int ipath_get_base_info(struct file
*fp
,
79 void __user
*ubase
, size_t ubase_size
)
81 struct ipath_portdata
*pd
= port_fp(fp
);
83 struct ipath_base_info
*kinfo
= NULL
;
84 struct ipath_devdata
*dd
= pd
->port_dd
;
89 subport_cnt
= pd
->port_subport_cnt
;
96 master
= !subport_fp(fp
);
100 /* If port sharing is not requested, allow the old size structure */
102 sz
-= 7 * sizeof(u64
);
103 if (ubase_size
< sz
) {
105 "Base size %zu, need %zu (version mismatch?)\n",
111 kinfo
= kzalloc(sizeof(*kinfo
), GFP_KERNEL
);
117 ret
= dd
->ipath_f_get_base_info(pd
, kinfo
);
121 kinfo
->spi_rcvhdr_cnt
= dd
->ipath_rcvhdrcnt
;
122 kinfo
->spi_rcvhdrent_size
= dd
->ipath_rcvhdrentsize
;
123 kinfo
->spi_tidegrcnt
= dd
->ipath_rcvegrcnt
;
124 kinfo
->spi_rcv_egrbufsize
= dd
->ipath_rcvegrbufsize
;
126 * have to mmap whole thing
128 kinfo
->spi_rcv_egrbuftotlen
=
129 pd
->port_rcvegrbuf_chunks
* pd
->port_rcvegrbuf_size
;
130 kinfo
->spi_rcv_egrperchunk
= pd
->port_rcvegrbufs_perchunk
;
131 kinfo
->spi_rcv_egrchunksize
= kinfo
->spi_rcv_egrbuftotlen
/
132 pd
->port_rcvegrbuf_chunks
;
133 kinfo
->spi_tidcnt
= dd
->ipath_rcvtidcnt
/ subport_cnt
;
135 kinfo
->spi_tidcnt
+= dd
->ipath_rcvtidcnt
% subport_cnt
;
137 * for this use, may be ipath_cfgports summed over all chips that
138 * are are configured and present
140 kinfo
->spi_nports
= dd
->ipath_cfgports
;
141 /* unit (chip/board) our port is on */
142 kinfo
->spi_unit
= dd
->ipath_unit
;
143 /* for now, only a single page */
144 kinfo
->spi_tid_maxsize
= PAGE_SIZE
;
147 * Doing this per port, and based on the skip value, etc. This has
148 * to be the actual buffer size, since the protocol code treats it
151 * These have to be set to user addresses in the user code via mmap.
152 * These values are used on return to user code for the mmap target
153 * addresses only. For 32 bit, same 44 bit address problem, so use
154 * the physical address, not virtual. Before 2.6.11, using the
155 * page_address() macro worked, but in 2.6.11, even that returns the
156 * full 64 bit address (upper bits all 1's). So far, using the
157 * physical addresses (or chip offsets, for chip mapping) works, but
158 * no doubt some future kernel release will change that, and we'll be
159 * on to yet another method of dealing with this.
161 kinfo
->spi_rcvhdr_base
= (u64
) pd
->port_rcvhdrq_phys
;
162 kinfo
->spi_rcvhdr_tailaddr
= (u64
) pd
->port_rcvhdrqtailaddr_phys
;
163 kinfo
->spi_rcv_egrbufs
= (u64
) pd
->port_rcvegr_phys
;
164 kinfo
->spi_pioavailaddr
= (u64
) dd
->ipath_pioavailregs_phys
;
165 kinfo
->spi_status
= (u64
) kinfo
->spi_pioavailaddr
+
166 (void *) dd
->ipath_statusp
-
167 (void *) dd
->ipath_pioavailregs_dma
;
169 kinfo
->spi_piocnt
= dd
->ipath_pbufsport
;
170 kinfo
->spi_piobufbase
= (u64
) pd
->port_piobufs
;
171 kinfo
->__spi_uregbase
= (u64
) dd
->ipath_uregbase
+
172 dd
->ipath_palign
* pd
->port_port
;
174 kinfo
->spi_piocnt
= (dd
->ipath_pbufsport
/ subport_cnt
) +
175 (dd
->ipath_pbufsport
% subport_cnt
);
176 /* Master's PIO buffers are after all the slave's */
177 kinfo
->spi_piobufbase
= (u64
) pd
->port_piobufs
+
179 (dd
->ipath_pbufsport
- kinfo
->spi_piocnt
);
181 unsigned slave
= subport_fp(fp
) - 1;
183 kinfo
->spi_piocnt
= dd
->ipath_pbufsport
/ subport_cnt
;
184 kinfo
->spi_piobufbase
= (u64
) pd
->port_piobufs
+
185 dd
->ipath_palign
* kinfo
->spi_piocnt
* slave
;
188 kinfo
->spi_port_uregbase
= (u64
) dd
->ipath_uregbase
+
189 dd
->ipath_palign
* pd
->port_port
;
190 kinfo
->spi_port_rcvegrbuf
= kinfo
->spi_rcv_egrbufs
;
191 kinfo
->spi_port_rcvhdr_base
= kinfo
->spi_rcvhdr_base
;
192 kinfo
->spi_port_rcvhdr_tailaddr
= kinfo
->spi_rcvhdr_tailaddr
;
194 kinfo
->__spi_uregbase
= cvt_kvaddr(pd
->subport_uregbase
+
195 PAGE_SIZE
* subport_fp(fp
));
197 kinfo
->spi_rcvhdr_base
= cvt_kvaddr(pd
->subport_rcvhdr_base
+
198 pd
->port_rcvhdrq_size
* subport_fp(fp
));
199 kinfo
->spi_rcvhdr_tailaddr
= 0;
200 kinfo
->spi_rcv_egrbufs
= cvt_kvaddr(pd
->subport_rcvegrbuf
+
201 pd
->port_rcvegrbuf_chunks
* pd
->port_rcvegrbuf_size
*
204 kinfo
->spi_subport_uregbase
=
205 cvt_kvaddr(pd
->subport_uregbase
);
206 kinfo
->spi_subport_rcvegrbuf
=
207 cvt_kvaddr(pd
->subport_rcvegrbuf
);
208 kinfo
->spi_subport_rcvhdr_base
=
209 cvt_kvaddr(pd
->subport_rcvhdr_base
);
210 ipath_cdbg(PROC
, "port %u flags %x %llx %llx %llx\n",
211 kinfo
->spi_port
, kinfo
->spi_runtime_flags
,
212 (unsigned long long) kinfo
->spi_subport_uregbase
,
213 (unsigned long long) kinfo
->spi_subport_rcvegrbuf
,
214 (unsigned long long) kinfo
->spi_subport_rcvhdr_base
);
217 kinfo
->spi_pioindex
= (kinfo
->spi_piobufbase
- dd
->ipath_piobufbase
) /
219 kinfo
->spi_pioalign
= dd
->ipath_palign
;
221 kinfo
->spi_qpair
= IPATH_KD_QP
;
222 kinfo
->spi_piosize
= dd
->ipath_ibmaxlen
;
223 kinfo
->spi_mtu
= dd
->ipath_ibmaxlen
; /* maxlen, not ibmtu */
224 kinfo
->spi_port
= pd
->port_port
;
225 kinfo
->spi_subport
= subport_fp(fp
);
226 kinfo
->spi_sw_version
= IPATH_KERN_SWVERSION
;
227 kinfo
->spi_hw_version
= dd
->ipath_revision
;
230 kinfo
->spi_runtime_flags
|= IPATH_RUNTIME_MASTER
;
233 sz
= (ubase_size
< sizeof(*kinfo
)) ? ubase_size
: sizeof(*kinfo
);
234 if (copy_to_user(ubase
, kinfo
, sz
))
243 * ipath_tid_update - update a port TID
245 * @fp: the ipath device file
246 * @ti: the TID information
248 * The new implementation as of Oct 2004 is that the driver assigns
249 * the tid and returns it to the caller. To make it easier to
250 * catch bugs, and to reduce search time, we keep a cursor for
251 * each port, walking the shadow tid array to find one that's not
254 * For now, if we can't allocate the full list, we fail, although
255 * in the long run, we'll allocate as many as we can, and the
256 * caller will deal with that by trying the remaining pages later.
257 * That means that when we fail, we have to mark the tids as not in
258 * use again, in our shadow copy.
260 * It's up to the caller to free the tids when they are done.
261 * We'll unlock the pages as they free them.
263 * Also, right now we are locking one page at a time, but since
264 * the intended use of this routine is for a single group of
265 * virtually contiguous pages, that should change to improve
268 static int ipath_tid_update(struct ipath_portdata
*pd
, struct file
*fp
,
269 const struct ipath_tid_info
*ti
)
272 u32 tid
, porttid
, cnt
, i
, tidcnt
, tidoff
;
274 struct ipath_devdata
*dd
= pd
->port_dd
;
277 u64 __iomem
*tidbase
;
278 unsigned long tidmap
[8];
279 struct page
**pagep
= NULL
;
280 unsigned subport
= subport_fp(fp
);
282 if (!dd
->ipath_pageshadow
) {
289 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
290 (unsigned long long) ti
->tidlist
);
292 * Should we treat as success? likely a bug
297 porttid
= pd
->port_port
* dd
->ipath_rcvtidcnt
;
298 if (!pd
->port_subport_cnt
) {
299 tidcnt
= dd
->ipath_rcvtidcnt
;
300 tid
= pd
->port_tidcursor
;
302 } else if (!subport
) {
303 tidcnt
= (dd
->ipath_rcvtidcnt
/ pd
->port_subport_cnt
) +
304 (dd
->ipath_rcvtidcnt
% pd
->port_subport_cnt
);
305 tidoff
= dd
->ipath_rcvtidcnt
- tidcnt
;
307 tid
= tidcursor_fp(fp
);
309 tidcnt
= dd
->ipath_rcvtidcnt
/ pd
->port_subport_cnt
;
310 tidoff
= tidcnt
* (subport
- 1);
312 tid
= tidcursor_fp(fp
);
315 /* make sure it all fits in port_tid_pg_list */
316 dev_info(&dd
->pcidev
->dev
, "Process tried to allocate %u "
317 "TIDs, only trying max (%u)\n", cnt
, tidcnt
);
320 pagep
= &((struct page
**) pd
->port_tid_pg_list
)[tidoff
];
321 tidlist
= &((u16
*) &pagep
[dd
->ipath_rcvtidcnt
])[tidoff
];
323 memset(tidmap
, 0, sizeof(tidmap
));
324 /* before decrement; chip actual # */
326 tidbase
= (u64 __iomem
*) (((char __iomem
*) dd
->ipath_kregbase
) +
327 dd
->ipath_rcvtidbase
+
328 porttid
* sizeof(*tidbase
));
330 ipath_cdbg(VERBOSE
, "Port%u %u tids, cursor %u, tidbase %p\n",
331 pd
->port_port
, cnt
, tid
, tidbase
);
333 /* virtual address of first page in transfer */
334 vaddr
= ti
->tidvaddr
;
335 if (!access_ok(VERIFY_WRITE
, (void __user
*) vaddr
,
337 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
342 ret
= ipath_get_user_pages(vaddr
, cnt
, pagep
);
345 ipath_dbg("Failed to lock addr %p, %u pages "
346 "(already locked)\n",
347 (void *) vaddr
, cnt
);
349 * for now, continue, and see what happens but with
350 * the new implementation, this should never happen,
351 * unless perhaps the user has mpin'ed the pages
352 * themselves (something we need to test)
356 dev_info(&dd
->pcidev
->dev
,
357 "Failed to lock addr %p, %u pages: "
358 "errno %d\n", (void *) vaddr
, cnt
, -ret
);
362 for (i
= 0; i
< cnt
; i
++, vaddr
+= PAGE_SIZE
) {
363 for (; ntids
--; tid
++) {
366 if (!dd
->ipath_pageshadow
[porttid
+ tid
])
371 * oops, wrapped all the way through their TIDs,
372 * and didn't have enough free; see comments at
375 ipath_dbg("Not enough free TIDs for %u pages "
376 "(index %d), failing\n", cnt
, i
);
377 i
--; /* last tidlist[i] not filled in */
381 tidlist
[i
] = tid
+ tidoff
;
382 ipath_cdbg(VERBOSE
, "Updating idx %u to TID %u, "
383 "vaddr %lx\n", i
, tid
+ tidoff
, vaddr
);
384 /* we "know" system pages and TID pages are same size */
385 dd
->ipath_pageshadow
[porttid
+ tid
] = pagep
[i
];
386 dd
->ipath_physshadow
[porttid
+ tid
] = ipath_map_page(
387 dd
->pcidev
, pagep
[i
], 0, PAGE_SIZE
,
390 * don't need atomic or it's overhead
392 __set_bit(tid
, tidmap
);
393 physaddr
= dd
->ipath_physshadow
[porttid
+ tid
];
394 ipath_stats
.sps_pagelocks
++;
396 "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
397 tid
, vaddr
, (unsigned long long) physaddr
,
399 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
], 1, physaddr
);
401 * don't check this tid in ipath_portshadow, since we
402 * just filled it in; start with the next one.
410 /* jump here if copy out of updated info failed... */
411 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
413 /* same code that's in ipath_free_tid() */
414 limit
= sizeof(tidmap
) * BITS_PER_BYTE
;
416 /* just in case size changes in future */
418 tid
= find_first_bit((const unsigned long *)tidmap
, limit
);
419 for (; tid
< limit
; tid
++) {
420 if (!test_bit(tid
, tidmap
))
422 if (dd
->ipath_pageshadow
[porttid
+ tid
]) {
423 ipath_cdbg(VERBOSE
, "Freeing TID %u\n",
425 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
], 1,
426 dd
->ipath_tidinvalid
);
427 pci_unmap_page(dd
->pcidev
,
428 dd
->ipath_physshadow
[porttid
+ tid
],
429 PAGE_SIZE
, PCI_DMA_FROMDEVICE
);
430 dd
->ipath_pageshadow
[porttid
+ tid
] = NULL
;
431 ipath_stats
.sps_pageunlocks
++;
434 ipath_release_user_pages(pagep
, cnt
);
437 * Copy the updated array, with ipath_tid's filled in, back
438 * to user. Since we did the copy in already, this "should
439 * never fail" If it does, we have to clean up...
441 if (copy_to_user((void __user
*)
442 (unsigned long) ti
->tidlist
,
443 tidlist
, cnt
* sizeof(*tidlist
))) {
447 if (copy_to_user((void __user
*) (unsigned long) ti
->tidmap
,
448 tidmap
, sizeof tidmap
)) {
454 if (!pd
->port_subport_cnt
)
455 pd
->port_tidcursor
= tid
;
457 tidcursor_fp(fp
) = tid
;
462 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
468 * ipath_tid_free - free a port TID
470 * @subport: the subport
473 * right now we are unlocking one page at a time, but since
474 * the intended use of this routine is for a single group of
475 * virtually contiguous pages, that should change to improve
476 * performance. We check that the TID is in range for this port
477 * but otherwise don't check validity; if user has an error and
478 * frees the wrong tid, it's only their own data that can thereby
479 * be corrupted. We do check that the TID was in use, for sanity
480 * We always use our idea of the saved address, not the address that
481 * they pass in to us.
484 static int ipath_tid_free(struct ipath_portdata
*pd
, unsigned subport
,
485 const struct ipath_tid_info
*ti
)
488 u32 tid
, porttid
, cnt
, limit
, tidcnt
;
489 struct ipath_devdata
*dd
= pd
->port_dd
;
490 u64 __iomem
*tidbase
;
491 unsigned long tidmap
[8];
493 if (!dd
->ipath_pageshadow
) {
498 if (copy_from_user(tidmap
, (void __user
*)(unsigned long)ti
->tidmap
,
504 porttid
= pd
->port_port
* dd
->ipath_rcvtidcnt
;
505 if (!pd
->port_subport_cnt
)
506 tidcnt
= dd
->ipath_rcvtidcnt
;
508 tidcnt
= (dd
->ipath_rcvtidcnt
/ pd
->port_subport_cnt
) +
509 (dd
->ipath_rcvtidcnt
% pd
->port_subport_cnt
);
510 porttid
+= dd
->ipath_rcvtidcnt
- tidcnt
;
512 tidcnt
= dd
->ipath_rcvtidcnt
/ pd
->port_subport_cnt
;
513 porttid
+= tidcnt
* (subport
- 1);
515 tidbase
= (u64 __iomem
*) ((char __iomem
*)(dd
->ipath_kregbase
) +
516 dd
->ipath_rcvtidbase
+
517 porttid
* sizeof(*tidbase
));
519 limit
= sizeof(tidmap
) * BITS_PER_BYTE
;
521 /* just in case size changes in future */
523 tid
= find_first_bit(tidmap
, limit
);
524 ipath_cdbg(VERBOSE
, "Port%u free %u tids; first bit (max=%d) "
525 "set is %d, porttid %u\n", pd
->port_port
, ti
->tidcnt
,
526 limit
, tid
, porttid
);
527 for (cnt
= 0; tid
< limit
; tid
++) {
529 * small optimization; if we detect a run of 3 or so without
530 * any set, use find_first_bit again. That's mainly to
531 * accelerate the case where we wrapped, so we have some at
532 * the beginning, and some at the end, and a big gap
535 if (!test_bit(tid
, tidmap
))
538 if (dd
->ipath_pageshadow
[porttid
+ tid
]) {
539 ipath_cdbg(VERBOSE
, "PID %u freeing TID %u\n",
541 dd
->ipath_f_put_tid(dd
, &tidbase
[tid
], 1,
542 dd
->ipath_tidinvalid
);
543 pci_unmap_page(dd
->pcidev
,
544 dd
->ipath_physshadow
[porttid
+ tid
],
545 PAGE_SIZE
, PCI_DMA_FROMDEVICE
);
546 ipath_release_user_pages(
547 &dd
->ipath_pageshadow
[porttid
+ tid
], 1);
548 dd
->ipath_pageshadow
[porttid
+ tid
] = NULL
;
549 ipath_stats
.sps_pageunlocks
++;
551 ipath_dbg("Unused tid %u, ignoring\n", tid
);
553 if (cnt
!= ti
->tidcnt
)
554 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
558 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
564 * ipath_set_part_key - set a partition key
568 * We can have up to 4 active at a time (other than the default, which is
569 * always allowed). This is somewhat tricky, since multiple ports may set
570 * the same key, so we reference count them, and clean up at exit. All 4
571 * partition keys are packed into a single infinipath register. It's an
572 * error for a process to set the same pkey multiple times. We provide no
573 * mechanism to de-allocate a pkey at this time, we may eventually need to
574 * do that. I've used the atomic operations, and no locking, and only make
575 * a single pass through what's available. This should be more than
576 * adequate for some time. I'll think about spinlocks or the like if and as
579 static int ipath_set_part_key(struct ipath_portdata
*pd
, u16 key
)
581 struct ipath_devdata
*dd
= pd
->port_dd
;
582 int i
, any
= 0, pidx
= -1;
583 u16 lkey
= key
& 0x7FFF;
586 if (lkey
== (IPATH_DEFAULT_P_KEY
& 0x7FFF)) {
587 /* nothing to do; this key always valid */
592 ipath_cdbg(VERBOSE
, "p%u try to set pkey %hx, current keys "
593 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
594 pd
->port_port
, key
, dd
->ipath_pkeys
[0],
595 atomic_read(&dd
->ipath_pkeyrefs
[0]), dd
->ipath_pkeys
[1],
596 atomic_read(&dd
->ipath_pkeyrefs
[1]), dd
->ipath_pkeys
[2],
597 atomic_read(&dd
->ipath_pkeyrefs
[2]), dd
->ipath_pkeys
[3],
598 atomic_read(&dd
->ipath_pkeyrefs
[3]));
601 ipath_cdbg(PROC
, "p%u tries to set key 0, not allowed\n",
608 * Set the full membership bit, because it has to be
609 * set in the register or the packet, and it seems
610 * cleaner to set in the register than to force all
611 * callers to set it. (see bug 4331)
615 for (i
= 0; i
< ARRAY_SIZE(pd
->port_pkeys
); i
++) {
616 if (!pd
->port_pkeys
[i
] && pidx
== -1)
618 if (pd
->port_pkeys
[i
] == key
) {
619 ipath_cdbg(VERBOSE
, "p%u tries to set same pkey "
620 "(%x) more than once\n",
627 ipath_dbg("All pkeys for port %u already in use, "
628 "can't set %x\n", pd
->port_port
, key
);
632 for (any
= i
= 0; i
< ARRAY_SIZE(dd
->ipath_pkeys
); i
++) {
633 if (!dd
->ipath_pkeys
[i
]) {
637 if (dd
->ipath_pkeys
[i
] == key
) {
638 atomic_t
*pkrefs
= &dd
->ipath_pkeyrefs
[i
];
640 if (atomic_inc_return(pkrefs
) > 1) {
641 pd
->port_pkeys
[pidx
] = key
;
642 ipath_cdbg(VERBOSE
, "p%u set key %x "
643 "matches #%d, count now %d\n",
644 pd
->port_port
, key
, i
,
645 atomic_read(pkrefs
));
650 * lost race, decrement count, catch below
653 ipath_cdbg(VERBOSE
, "Lost race, count was "
654 "0, after dec, it's %d\n",
655 atomic_read(pkrefs
));
659 if ((dd
->ipath_pkeys
[i
] & 0x7FFF) == lkey
) {
661 * It makes no sense to have both the limited and
662 * full membership PKEY set at the same time since
663 * the unlimited one will disable the limited one.
670 ipath_dbg("port %u, all pkeys already in use, "
671 "can't set %x\n", pd
->port_port
, key
);
675 for (any
= i
= 0; i
< ARRAY_SIZE(dd
->ipath_pkeys
); i
++) {
676 if (!dd
->ipath_pkeys
[i
] &&
677 atomic_inc_return(&dd
->ipath_pkeyrefs
[i
]) == 1) {
680 /* for ipathstats, etc. */
681 ipath_stats
.sps_pkeys
[i
] = lkey
;
682 pd
->port_pkeys
[pidx
] = dd
->ipath_pkeys
[i
] = key
;
684 (u64
) dd
->ipath_pkeys
[0] |
685 ((u64
) dd
->ipath_pkeys
[1] << 16) |
686 ((u64
) dd
->ipath_pkeys
[2] << 32) |
687 ((u64
) dd
->ipath_pkeys
[3] << 48);
688 ipath_cdbg(PROC
, "p%u set key %x in #%d, "
689 "portidx %d, new pkey reg %llx\n",
690 pd
->port_port
, key
, i
, pidx
,
691 (unsigned long long) pkey
);
693 dd
, dd
->ipath_kregs
->kr_partitionkey
, pkey
);
699 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
700 "can't set %x\n", pd
->port_port
, key
);
708 * ipath_manage_rcvq - manage a port's receive queue
710 * @subport: the subport
711 * @start_stop: action to carry out
713 * start_stop == 0 disables receive on the port, for use in queue
714 * overflow conditions. start_stop==1 re-enables, to be used to
715 * re-init the software copy of the head register
717 static int ipath_manage_rcvq(struct ipath_portdata
*pd
, unsigned subport
,
720 struct ipath_devdata
*dd
= pd
->port_dd
;
722 ipath_cdbg(PROC
, "%sabling rcv for unit %u port %u:%u\n",
723 start_stop
? "en" : "dis", dd
->ipath_unit
,
724 pd
->port_port
, subport
);
727 /* atomically clear receive enable port. */
730 * On enable, force in-memory copy of the tail register to
731 * 0, so that protocol code doesn't have to worry about
732 * whether or not the chip has yet updated the in-memory
733 * copy or not on return from the system call. The chip
734 * always resets it's tail register back to 0 on a
735 * transition from disabled to enabled. This could cause a
736 * problem if software was broken, and did the enable w/o
737 * the disable, but eventually the in-memory copy will be
738 * updated and correct itself, even in the face of software
741 *(volatile u64
*)pd
->port_rcvhdrtail_kvaddr
= 0;
742 set_bit(INFINIPATH_R_PORTENABLE_SHIFT
+ pd
->port_port
,
745 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT
+ pd
->port_port
,
747 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
749 /* now be sure chip saw it before we return */
750 ipath_read_kreg64(dd
, dd
->ipath_kregs
->kr_scratch
);
753 * And try to be sure that tail reg update has happened too.
754 * This should in theory interlock with the RXE changes to
755 * the tail register. Don't assign it to the tail register
756 * in memory copy, since we could overwrite an update by the
759 ipath_read_ureg32(dd
, ur_rcvhdrtail
, pd
->port_port
);
761 /* always; new head should be equal to new tail; see above */
766 static void ipath_clean_part_key(struct ipath_portdata
*pd
,
767 struct ipath_devdata
*dd
)
769 int i
, j
, pchanged
= 0;
772 /* for debugging only */
773 oldpkey
= (u64
) dd
->ipath_pkeys
[0] |
774 ((u64
) dd
->ipath_pkeys
[1] << 16) |
775 ((u64
) dd
->ipath_pkeys
[2] << 32) |
776 ((u64
) dd
->ipath_pkeys
[3] << 48);
778 for (i
= 0; i
< ARRAY_SIZE(pd
->port_pkeys
); i
++) {
779 if (!pd
->port_pkeys
[i
])
781 ipath_cdbg(VERBOSE
, "look for key[%d] %hx in pkeys\n", i
,
783 for (j
= 0; j
< ARRAY_SIZE(dd
->ipath_pkeys
); j
++) {
784 /* check for match independent of the global bit */
785 if ((dd
->ipath_pkeys
[j
] & 0x7fff) !=
786 (pd
->port_pkeys
[i
] & 0x7fff))
788 if (atomic_dec_and_test(&dd
->ipath_pkeyrefs
[j
])) {
789 ipath_cdbg(VERBOSE
, "p%u clear key "
792 pd
->port_pkeys
[i
], j
);
793 ipath_stats
.sps_pkeys
[j
] =
794 dd
->ipath_pkeys
[j
] = 0;
798 VERBOSE
, "p%u key %x matches #%d, "
799 "but ref still %d\n", pd
->port_port
,
800 pd
->port_pkeys
[i
], j
,
801 atomic_read(&dd
->ipath_pkeyrefs
[j
]));
804 pd
->port_pkeys
[i
] = 0;
807 u64 pkey
= (u64
) dd
->ipath_pkeys
[0] |
808 ((u64
) dd
->ipath_pkeys
[1] << 16) |
809 ((u64
) dd
->ipath_pkeys
[2] << 32) |
810 ((u64
) dd
->ipath_pkeys
[3] << 48);
811 ipath_cdbg(VERBOSE
, "p%u old pkey reg %llx, "
812 "new pkey reg %llx\n", pd
->port_port
,
813 (unsigned long long) oldpkey
,
814 (unsigned long long) pkey
);
815 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_partitionkey
,
821 * Initialize the port data with the receive buffer sizes
822 * so this can be done while the master port is locked.
823 * Otherwise, there is a race with a slave opening the port
824 * and seeing these fields uninitialized.
826 static void init_user_egr_sizes(struct ipath_portdata
*pd
)
828 struct ipath_devdata
*dd
= pd
->port_dd
;
829 unsigned egrperchunk
, egrcnt
, size
;
832 * to avoid wasting a lot of memory, we allocate 32KB chunks of
833 * physically contiguous memory, advance through it until used up
834 * and then allocate more. Of course, we need memory to store those
835 * extra pointers, now. Started out with 256KB, but under heavy
836 * memory pressure (creating large files and then copying them over
837 * NFS while doing lots of MPI jobs), we hit some allocation
838 * failures, even though we can sleep... (2.6.10) Still get
839 * failures at 64K. 32K is the lowest we can go without wasting
843 egrperchunk
= size
/ dd
->ipath_rcvegrbufsize
;
844 egrcnt
= dd
->ipath_rcvegrcnt
;
845 pd
->port_rcvegrbuf_chunks
= (egrcnt
+ egrperchunk
- 1) / egrperchunk
;
846 pd
->port_rcvegrbufs_perchunk
= egrperchunk
;
847 pd
->port_rcvegrbuf_size
= size
;
851 * ipath_create_user_egr - allocate eager TID buffers
852 * @pd: the port to allocate TID buffers for
854 * This routine is now quite different for user and kernel, because
855 * the kernel uses skb's, for the accelerated network performance
856 * This is the user port version
858 * Allocate the eager TID buffers and program them into infinipath
859 * They are no longer completely contiguous, we do multiple allocation
862 static int ipath_create_user_egr(struct ipath_portdata
*pd
)
864 struct ipath_devdata
*dd
= pd
->port_dd
;
865 unsigned e
, egrcnt
, egrperchunk
, chunk
, egrsize
, egroff
;
871 * GFP_USER, but without GFP_FS, so buffer cache can be
872 * coalesced (we hope); otherwise, even at order 4,
873 * heavy filesystem activity makes these fail, and we can
874 * use compound pages.
876 gfp_flags
= __GFP_WAIT
| __GFP_IO
| __GFP_COMP
;
878 egrcnt
= dd
->ipath_rcvegrcnt
;
879 /* TID number offset for this port */
880 egroff
= pd
->port_port
* egrcnt
;
881 egrsize
= dd
->ipath_rcvegrbufsize
;
882 ipath_cdbg(VERBOSE
, "Allocating %d egr buffers, at egrtid "
883 "offset %x, egrsize %u\n", egrcnt
, egroff
, egrsize
);
885 chunk
= pd
->port_rcvegrbuf_chunks
;
886 egrperchunk
= pd
->port_rcvegrbufs_perchunk
;
887 size
= pd
->port_rcvegrbuf_size
;
888 pd
->port_rcvegrbuf
= kmalloc(chunk
* sizeof(pd
->port_rcvegrbuf
[0]),
890 if (!pd
->port_rcvegrbuf
) {
894 pd
->port_rcvegrbuf_phys
=
895 kmalloc(chunk
* sizeof(pd
->port_rcvegrbuf_phys
[0]),
897 if (!pd
->port_rcvegrbuf_phys
) {
901 for (e
= 0; e
< pd
->port_rcvegrbuf_chunks
; e
++) {
903 pd
->port_rcvegrbuf
[e
] = dma_alloc_coherent(
904 &dd
->pcidev
->dev
, size
, &pd
->port_rcvegrbuf_phys
[e
],
907 if (!pd
->port_rcvegrbuf
[e
]) {
909 goto bail_rcvegrbuf_phys
;
913 pd
->port_rcvegr_phys
= pd
->port_rcvegrbuf_phys
[0];
915 for (e
= chunk
= 0; chunk
< pd
->port_rcvegrbuf_chunks
; chunk
++) {
916 dma_addr_t pa
= pd
->port_rcvegrbuf_phys
[chunk
];
919 for (i
= 0; e
< egrcnt
&& i
< egrperchunk
; e
++, i
++) {
920 dd
->ipath_f_put_tid(dd
, e
+ egroff
+
924 dd
->ipath_rcvegrbase
), 0, pa
);
927 cond_resched(); /* don't hog the cpu */
934 for (e
= 0; e
< pd
->port_rcvegrbuf_chunks
&&
935 pd
->port_rcvegrbuf
[e
]; e
++) {
936 dma_free_coherent(&dd
->pcidev
->dev
, size
,
937 pd
->port_rcvegrbuf
[e
],
938 pd
->port_rcvegrbuf_phys
[e
]);
941 kfree(pd
->port_rcvegrbuf_phys
);
942 pd
->port_rcvegrbuf_phys
= NULL
;
944 kfree(pd
->port_rcvegrbuf
);
945 pd
->port_rcvegrbuf
= NULL
;
951 /* common code for the mappings on dma_alloc_coherent mem */
952 static int ipath_mmap_mem(struct vm_area_struct
*vma
,
953 struct ipath_portdata
*pd
, unsigned len
, int write_ok
,
954 void *kvaddr
, char *what
)
956 struct ipath_devdata
*dd
= pd
->port_dd
;
960 if ((vma
->vm_end
- vma
->vm_start
) > len
) {
961 dev_info(&dd
->pcidev
->dev
,
962 "FAIL on %s: len %lx > %x\n", what
,
963 vma
->vm_end
- vma
->vm_start
, len
);
969 if (vma
->vm_flags
& VM_WRITE
) {
970 dev_info(&dd
->pcidev
->dev
,
971 "%s must be mapped readonly\n", what
);
976 /* don't allow them to later change with mprotect */
977 vma
->vm_flags
&= ~VM_MAYWRITE
;
980 pfn
= virt_to_phys(kvaddr
) >> PAGE_SHIFT
;
981 ret
= remap_pfn_range(vma
, vma
->vm_start
, pfn
,
982 len
, vma
->vm_page_prot
);
984 dev_info(&dd
->pcidev
->dev
, "%s port%u mmap of %lx, %x "
985 "bytes r%c failed: %d\n", what
, pd
->port_port
,
986 pfn
, len
, write_ok
?'w':'o', ret
);
988 ipath_cdbg(VERBOSE
, "%s port%u mmaped %lx, %x bytes "
989 "r%c\n", what
, pd
->port_port
, pfn
, len
,
995 static int mmap_ureg(struct vm_area_struct
*vma
, struct ipath_devdata
*dd
,
1002 * This is real hardware, so use io_remap. This is the mechanism
1003 * for the user process to update the head registers for their port
1006 if ((vma
->vm_end
- vma
->vm_start
) > PAGE_SIZE
) {
1007 dev_info(&dd
->pcidev
->dev
, "FAIL mmap userreg: reqlen "
1008 "%lx > PAGE\n", vma
->vm_end
- vma
->vm_start
);
1011 phys
= dd
->ipath_physaddr
+ ureg
;
1012 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1014 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
1015 ret
= io_remap_pfn_range(vma
, vma
->vm_start
,
1017 vma
->vm_end
- vma
->vm_start
,
1023 static int mmap_piobufs(struct vm_area_struct
*vma
,
1024 struct ipath_devdata
*dd
,
1025 struct ipath_portdata
*pd
,
1026 unsigned piobufs
, unsigned piocnt
)
1032 * When we map the PIO buffers in the chip, we want to map them as
1033 * writeonly, no read possible. This prevents access to previous
1034 * process data, and catches users who might try to read the i/o
1035 * space due to a bug.
1037 if ((vma
->vm_end
- vma
->vm_start
) > (piocnt
* dd
->ipath_palign
)) {
1038 dev_info(&dd
->pcidev
->dev
, "FAIL mmap piobufs: "
1039 "reqlen %lx > PAGE\n",
1040 vma
->vm_end
- vma
->vm_start
);
1045 phys
= dd
->ipath_physaddr
+ piobufs
;
1048 * Don't mark this as non-cached, or we don't get the
1049 * write combining behavior we want on the PIO buffers!
1052 #if defined(__powerpc__)
1053 /* There isn't a generic way to specify writethrough mappings */
1054 pgprot_val(vma
->vm_page_prot
) |= _PAGE_NO_CACHE
;
1055 pgprot_val(vma
->vm_page_prot
) |= _PAGE_WRITETHRU
;
1056 pgprot_val(vma
->vm_page_prot
) &= ~_PAGE_GUARDED
;
1060 * don't allow them to later change to readable with mprotect (for when
1061 * not initially mapped readable, as is normally the case)
1063 vma
->vm_flags
&= ~VM_MAYREAD
;
1064 vma
->vm_flags
|= VM_DONTCOPY
| VM_DONTEXPAND
;
1066 ret
= io_remap_pfn_range(vma
, vma
->vm_start
, phys
>> PAGE_SHIFT
,
1067 vma
->vm_end
- vma
->vm_start
,
1073 static int mmap_rcvegrbufs(struct vm_area_struct
*vma
,
1074 struct ipath_portdata
*pd
)
1076 struct ipath_devdata
*dd
= pd
->port_dd
;
1077 unsigned long start
, size
;
1078 size_t total_size
, i
;
1082 size
= pd
->port_rcvegrbuf_size
;
1083 total_size
= pd
->port_rcvegrbuf_chunks
* size
;
1084 if ((vma
->vm_end
- vma
->vm_start
) > total_size
) {
1085 dev_info(&dd
->pcidev
->dev
, "FAIL on egr bufs: "
1086 "reqlen %lx > actual %lx\n",
1087 vma
->vm_end
- vma
->vm_start
,
1088 (unsigned long) total_size
);
1093 if (vma
->vm_flags
& VM_WRITE
) {
1094 dev_info(&dd
->pcidev
->dev
, "Can't map eager buffers as "
1095 "writable (flags=%lx)\n", vma
->vm_flags
);
1099 /* don't allow them to later change to writeable with mprotect */
1100 vma
->vm_flags
&= ~VM_MAYWRITE
;
1102 start
= vma
->vm_start
;
1104 for (i
= 0; i
< pd
->port_rcvegrbuf_chunks
; i
++, start
+= size
) {
1105 pfn
= virt_to_phys(pd
->port_rcvegrbuf
[i
]) >> PAGE_SHIFT
;
1106 ret
= remap_pfn_range(vma
, start
, pfn
, size
,
1118 * ipath_file_vma_nopage - handle a VMA page fault.
1120 static struct page
*ipath_file_vma_nopage(struct vm_area_struct
*vma
,
1121 unsigned long address
, int *type
)
1123 unsigned long offset
= address
- vma
->vm_start
;
1124 struct page
*page
= NOPAGE_SIGBUS
;
1128 * Convert the vmalloc address into a struct page.
1130 pageptr
= (void *)(offset
+ (vma
->vm_pgoff
<< PAGE_SHIFT
));
1131 page
= vmalloc_to_page(pageptr
);
1135 /* Increment the reference count. */
1138 *type
= VM_FAULT_MINOR
;
1143 static struct vm_operations_struct ipath_file_vm_ops
= {
1144 .nopage
= ipath_file_vma_nopage
,
1147 static int mmap_kvaddr(struct vm_area_struct
*vma
, u64 pgaddr
,
1148 struct ipath_portdata
*pd
, unsigned subport
)
1151 struct ipath_devdata
*dd
;
1156 /* If the port is not shared, all addresses should be physical */
1157 if (!pd
->port_subport_cnt
)
1161 size
= pd
->port_rcvegrbuf_chunks
* pd
->port_rcvegrbuf_size
;
1164 * Each process has all the subport uregbase, rcvhdrq, and
1165 * rcvegrbufs mmapped - as an array for all the processes,
1166 * and also separately for this process.
1168 if (pgaddr
== cvt_kvaddr(pd
->subport_uregbase
)) {
1169 addr
= pd
->subport_uregbase
;
1170 size
= PAGE_SIZE
* pd
->port_subport_cnt
;
1171 } else if (pgaddr
== cvt_kvaddr(pd
->subport_rcvhdr_base
)) {
1172 addr
= pd
->subport_rcvhdr_base
;
1173 size
= pd
->port_rcvhdrq_size
* pd
->port_subport_cnt
;
1174 } else if (pgaddr
== cvt_kvaddr(pd
->subport_rcvegrbuf
)) {
1175 addr
= pd
->subport_rcvegrbuf
;
1176 size
*= pd
->port_subport_cnt
;
1177 } else if (pgaddr
== cvt_kvaddr(pd
->subport_uregbase
+
1178 PAGE_SIZE
* subport
)) {
1179 addr
= pd
->subport_uregbase
+ PAGE_SIZE
* subport
;
1181 } else if (pgaddr
== cvt_kvaddr(pd
->subport_rcvhdr_base
+
1182 pd
->port_rcvhdrq_size
* subport
)) {
1183 addr
= pd
->subport_rcvhdr_base
+
1184 pd
->port_rcvhdrq_size
* subport
;
1185 size
= pd
->port_rcvhdrq_size
;
1186 } else if (pgaddr
== cvt_kvaddr(pd
->subport_rcvegrbuf
+
1188 addr
= pd
->subport_rcvegrbuf
+ size
* subport
;
1189 /* rcvegrbufs are read-only on the slave */
1190 if (vma
->vm_flags
& VM_WRITE
) {
1191 dev_info(&dd
->pcidev
->dev
,
1192 "Can't map eager buffers as "
1193 "writable (flags=%lx)\n", vma
->vm_flags
);
1198 * Don't allow permission to later change to writeable
1201 vma
->vm_flags
&= ~VM_MAYWRITE
;
1205 len
= vma
->vm_end
- vma
->vm_start
;
1207 ipath_cdbg(MM
, "FAIL: reqlen %lx > %zx\n", len
, size
);
1212 vma
->vm_pgoff
= (unsigned long) addr
>> PAGE_SHIFT
;
1213 vma
->vm_ops
= &ipath_file_vm_ops
;
1214 vma
->vm_flags
|= VM_RESERVED
| VM_DONTEXPAND
;
1222 * ipath_mmap - mmap various structures into user space
1223 * @fp: the file pointer
1226 * We use this to have a shared buffer between the kernel and the user code
1227 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1228 * buffers in the chip. We have the open and close entries so we can bump
1229 * the ref count and keep the driver from being unloaded while still mapped.
1231 static int ipath_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
1233 struct ipath_portdata
*pd
;
1234 struct ipath_devdata
*dd
;
1236 unsigned piobufs
, piocnt
;
1247 * This is the ipath_do_user_init() code, mapping the shared buffers
1248 * into the user process. The address referred to by vm_pgoff is the
1249 * file offset passed via mmap(). For shared ports, this is the
1250 * kernel vmalloc() address of the pages to share with the master.
1251 * For non-shared or master ports, this is a physical address.
1252 * We only do one mmap for each space mapped.
1254 pgaddr
= vma
->vm_pgoff
<< PAGE_SHIFT
;
1257 * Check for 0 in case one of the allocations failed, but user
1258 * called mmap anyway.
1265 ipath_cdbg(MM
, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
1266 (unsigned long long) pgaddr
, vma
->vm_start
,
1267 vma
->vm_end
- vma
->vm_start
, dd
->ipath_unit
,
1268 pd
->port_port
, subport_fp(fp
));
1271 * Physical addresses must fit in 40 bits for our hardware.
1272 * Check for kernel virtual addresses first, anything else must
1273 * match a HW or memory address.
1275 ret
= mmap_kvaddr(vma
, pgaddr
, pd
, subport_fp(fp
));
1282 ureg
= dd
->ipath_uregbase
+ dd
->ipath_palign
* pd
->port_port
;
1283 if (!pd
->port_subport_cnt
) {
1284 /* port is not shared */
1285 piocnt
= dd
->ipath_pbufsport
;
1286 piobufs
= pd
->port_piobufs
;
1287 } else if (!subport_fp(fp
)) {
1288 /* caller is the master */
1289 piocnt
= (dd
->ipath_pbufsport
/ pd
->port_subport_cnt
) +
1290 (dd
->ipath_pbufsport
% pd
->port_subport_cnt
);
1291 piobufs
= pd
->port_piobufs
+
1292 dd
->ipath_palign
* (dd
->ipath_pbufsport
- piocnt
);
1294 unsigned slave
= subport_fp(fp
) - 1;
1296 /* caller is a slave */
1297 piocnt
= dd
->ipath_pbufsport
/ pd
->port_subport_cnt
;
1298 piobufs
= pd
->port_piobufs
+ dd
->ipath_palign
* piocnt
* slave
;
1302 ret
= mmap_ureg(vma
, dd
, ureg
);
1303 else if (pgaddr
== piobufs
)
1304 ret
= mmap_piobufs(vma
, dd
, pd
, piobufs
, piocnt
);
1305 else if (pgaddr
== dd
->ipath_pioavailregs_phys
)
1306 /* in-memory copy of pioavail registers */
1307 ret
= ipath_mmap_mem(vma
, pd
, PAGE_SIZE
, 0,
1308 (void *) dd
->ipath_pioavailregs_dma
,
1309 "pioavail registers");
1310 else if (pgaddr
== pd
->port_rcvegr_phys
)
1311 ret
= mmap_rcvegrbufs(vma
, pd
);
1312 else if (pgaddr
== (u64
) pd
->port_rcvhdrq_phys
)
1314 * The rcvhdrq itself; readonly except on HT (so have
1315 * to allow writable mapping), multiple pages, contiguous
1316 * from an i/o perspective.
1318 ret
= ipath_mmap_mem(vma
, pd
, pd
->port_rcvhdrq_size
, 1,
1321 else if (pgaddr
== (u64
) pd
->port_rcvhdrqtailaddr_phys
)
1322 /* in-memory copy of rcvhdrq tail register */
1323 ret
= ipath_mmap_mem(vma
, pd
, PAGE_SIZE
, 0,
1324 pd
->port_rcvhdrtail_kvaddr
,
1329 vma
->vm_private_data
= NULL
;
1332 dev_info(&dd
->pcidev
->dev
,
1333 "Failure %d on off %llx len %lx\n",
1334 -ret
, (unsigned long long)pgaddr
,
1335 vma
->vm_end
- vma
->vm_start
);
1340 static unsigned int ipath_poll(struct file
*fp
,
1341 struct poll_table_struct
*pt
)
1343 struct ipath_portdata
*pd
;
1346 unsigned pollflag
= 0;
1347 struct ipath_devdata
*dd
;
1354 bit
= pd
->port_port
+ INFINIPATH_R_INTRAVAIL_SHIFT
;
1355 set_bit(bit
, &dd
->ipath_rcvctrl
);
1358 * Before blocking, make sure that head is still == tail,
1359 * reading from the chip, so we can be sure the interrupt
1360 * enable has made it to the chip. If not equal, disable
1361 * interrupt again and return immediately. This avoids races,
1362 * and the overhead of the chip read doesn't matter much at
1363 * this point, since we are waiting for something anyway.
1366 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1369 head
= ipath_read_ureg32(dd
, ur_rcvhdrhead
, pd
->port_port
);
1370 tail
= ipath_read_ureg32(dd
, ur_rcvhdrtail
, pd
->port_port
);
1373 set_bit(IPATH_PORT_WAITING_RCV
, &pd
->port_flag
);
1374 if (dd
->ipath_rhdrhead_intr_off
) /* arm rcv interrupt */
1375 (void)ipath_write_ureg(dd
, ur_rcvhdrhead
,
1376 dd
->ipath_rhdrhead_intr_off
1377 | head
, pd
->port_port
);
1378 poll_wait(fp
, &pd
->port_wait
, pt
);
1380 if (test_bit(IPATH_PORT_WAITING_RCV
, &pd
->port_flag
)) {
1381 /* timed out, no packets received */
1382 clear_bit(IPATH_PORT_WAITING_RCV
, &pd
->port_flag
);
1383 pd
->port_rcvwait_to
++;
1386 pollflag
= POLLIN
| POLLRDNORM
;
1389 /* it's already happened; don't do wait_event overhead */
1390 pollflag
= POLLIN
| POLLRDNORM
;
1391 pd
->port_rcvnowait
++;
1394 clear_bit(bit
, &dd
->ipath_rcvctrl
);
1395 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1402 static int init_subports(struct ipath_devdata
*dd
,
1403 struct ipath_portdata
*pd
,
1404 const struct ipath_user_info
*uinfo
)
1407 unsigned num_subports
;
1411 * If the user is requesting zero or one port,
1412 * skip the subport allocation.
1414 if (uinfo
->spu_subport_cnt
<= 1)
1417 /* Old user binaries don't know about new subport implementation */
1418 if ((uinfo
->spu_userversion
& 0xffff) != IPATH_USER_SWMINOR
) {
1419 dev_info(&dd
->pcidev
->dev
,
1420 "Mismatched user minor version (%d) and driver "
1421 "minor version (%d) while port sharing. Ensure "
1422 "that driver and library are from the same "
1424 (int) (uinfo
->spu_userversion
& 0xffff),
1425 IPATH_USER_SWMINOR
);
1428 if (uinfo
->spu_subport_cnt
> INFINIPATH_MAX_SUBPORT
) {
1433 num_subports
= uinfo
->spu_subport_cnt
;
1434 pd
->subport_uregbase
= vmalloc(PAGE_SIZE
* num_subports
);
1435 if (!pd
->subport_uregbase
) {
1439 /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
1440 size
= ALIGN(dd
->ipath_rcvhdrcnt
* dd
->ipath_rcvhdrentsize
*
1441 sizeof(u32
), PAGE_SIZE
) * num_subports
;
1442 pd
->subport_rcvhdr_base
= vmalloc(size
);
1443 if (!pd
->subport_rcvhdr_base
) {
1448 pd
->subport_rcvegrbuf
= vmalloc(pd
->port_rcvegrbuf_chunks
*
1449 pd
->port_rcvegrbuf_size
*
1451 if (!pd
->subport_rcvegrbuf
) {
1456 pd
->port_subport_cnt
= uinfo
->spu_subport_cnt
;
1457 pd
->port_subport_id
= uinfo
->spu_subport_id
;
1458 pd
->active_slaves
= 1;
1459 set_bit(IPATH_PORT_MASTER_UNINIT
, &pd
->port_flag
);
1460 memset(pd
->subport_uregbase
, 0, PAGE_SIZE
* num_subports
);
1461 memset(pd
->subport_rcvhdr_base
, 0, size
);
1462 memset(pd
->subport_rcvegrbuf
, 0, pd
->port_rcvegrbuf_chunks
*
1463 pd
->port_rcvegrbuf_size
*
1468 vfree(pd
->subport_rcvhdr_base
);
1470 vfree(pd
->subport_uregbase
);
1471 pd
->subport_uregbase
= NULL
;
1476 static int try_alloc_port(struct ipath_devdata
*dd
, int port
,
1478 const struct ipath_user_info
*uinfo
)
1480 struct ipath_portdata
*pd
;
1483 if (!(pd
= dd
->ipath_pd
[port
])) {
1486 pd
= kzalloc(sizeof(struct ipath_portdata
), GFP_KERNEL
);
1489 * Allocate memory for use in ipath_tid_update() just once
1490 * at open, not per call. Reduces cost of expected send
1493 ptmp
= kmalloc(dd
->ipath_rcvtidcnt
* sizeof(u16
) +
1494 dd
->ipath_rcvtidcnt
* sizeof(struct page
**),
1497 ipath_dev_err(dd
, "Unable to allocate portdata "
1498 "memory, failing open\n");
1504 dd
->ipath_pd
[port
] = pd
;
1505 dd
->ipath_pd
[port
]->port_port
= port
;
1506 dd
->ipath_pd
[port
]->port_dd
= dd
;
1507 dd
->ipath_pd
[port
]->port_tid_pg_list
= ptmp
;
1508 init_waitqueue_head(&dd
->ipath_pd
[port
]->port_wait
);
1510 if (!pd
->port_cnt
) {
1511 pd
->userversion
= uinfo
->spu_userversion
;
1512 init_user_egr_sizes(pd
);
1513 if ((ret
= init_subports(dd
, pd
, uinfo
)) != 0)
1515 ipath_cdbg(PROC
, "%s[%u] opened unit:port %u:%u\n",
1516 current
->comm
, current
->pid
, dd
->ipath_unit
,
1520 pd
->port_pid
= current
->pid
;
1521 strncpy(pd
->port_comm
, current
->comm
, sizeof(pd
->port_comm
));
1522 ipath_stats
.sps_ports
++;
1531 static inline int usable(struct ipath_devdata
*dd
)
1534 (dd
->ipath_flags
& IPATH_PRESENT
) &&
1535 dd
->ipath_kregbase
&&
1537 !(dd
->ipath_flags
& (IPATH_LINKDOWN
| IPATH_DISABLED
1541 static int find_free_port(int unit
, struct file
*fp
,
1542 const struct ipath_user_info
*uinfo
)
1544 struct ipath_devdata
*dd
= ipath_lookup(unit
);
1557 for (i
= 1; i
< dd
->ipath_cfgports
; i
++) {
1558 ret
= try_alloc_port(dd
, i
, fp
, uinfo
);
1568 static int find_best_unit(struct file
*fp
,
1569 const struct ipath_user_info
*uinfo
)
1571 int ret
= 0, i
, prefunit
= -1, devmax
;
1572 int maxofallports
, npresent
, nup
;
1575 devmax
= ipath_count_units(&npresent
, &nup
, &maxofallports
);
1578 * This code is present to allow a knowledgeable person to
1579 * specify the layout of processes to processors before opening
1580 * this driver, and then we'll assign the process to the "closest"
1581 * InfiniPath chip to that processor (we assume reasonable connectivity,
1582 * for now). This code assumes that if affinity has been set
1583 * before this point, that at most one cpu is set; for now this
1584 * is reasonable. I check for both cpus_empty() and cpus_full(),
1585 * in case some kernel variant sets none of the bits when no
1586 * affinity is set. 2.6.11 and 12 kernels have all present
1587 * cpus set. Some day we'll have to fix it up further to handle
1588 * a cpu subset. This algorithm fails for two HT chips connected
1589 * in tunnel fashion. Eventually this needs real topology
1590 * information. There may be some issues with dual core numbering
1591 * as well. This needs more work prior to release.
1593 if (!cpus_empty(current
->cpus_allowed
) &&
1594 !cpus_full(current
->cpus_allowed
)) {
1595 int ncpus
= num_online_cpus(), curcpu
= -1, nset
= 0;
1596 for (i
= 0; i
< ncpus
; i
++)
1597 if (cpu_isset(i
, current
->cpus_allowed
)) {
1598 ipath_cdbg(PROC
, "%s[%u] affinity set for "
1599 "cpu %d/%d\n", current
->comm
,
1600 current
->pid
, i
, ncpus
);
1604 if (curcpu
!= -1 && nset
!= ncpus
) {
1606 prefunit
= curcpu
/ (ncpus
/ npresent
);
1607 ipath_cdbg(PROC
,"%s[%u] %d chips, %d cpus, "
1608 "%d cpus/chip, select unit %d\n",
1609 current
->comm
, current
->pid
,
1610 npresent
, ncpus
, ncpus
/ npresent
,
1617 * user ports start at 1, kernel port is 0
1618 * For now, we do round-robin access across all chips
1622 devmax
= prefunit
+ 1;
1624 for (i
= 1; i
< maxofallports
; i
++) {
1625 for (ndev
= prefunit
!= -1 ? prefunit
: 0; ndev
< devmax
;
1627 struct ipath_devdata
*dd
= ipath_lookup(ndev
);
1630 continue; /* can't use this unit */
1631 if (i
>= dd
->ipath_cfgports
)
1633 * Maxed out on users of this unit. Try
1637 ret
= try_alloc_port(dd
, i
, fp
, uinfo
);
1646 ipath_dbg("No ports available (none initialized "
1650 /* if started above 0, retry from 0 */
1652 "%s[%u] no ports on prefunit "
1653 "%d, clear and re-check\n",
1654 current
->comm
, current
->pid
,
1656 devmax
= ipath_count_units(NULL
, NULL
,
1662 ipath_dbg("No ports available\n");
1666 ipath_dbg("No boards found\n");
1673 static int find_shared_port(struct file
*fp
,
1674 const struct ipath_user_info
*uinfo
)
1676 int devmax
, ndev
, i
;
1679 devmax
= ipath_count_units(NULL
, NULL
, NULL
);
1681 for (ndev
= 0; ndev
< devmax
; ndev
++) {
1682 struct ipath_devdata
*dd
= ipath_lookup(ndev
);
1686 for (i
= 1; i
< dd
->ipath_cfgports
; i
++) {
1687 struct ipath_portdata
*pd
= dd
->ipath_pd
[i
];
1689 /* Skip ports which are not yet open */
1690 if (!pd
|| !pd
->port_cnt
)
1692 /* Skip port if it doesn't match the requested one */
1693 if (pd
->port_subport_id
!= uinfo
->spu_subport_id
)
1695 /* Verify the sharing process matches the master */
1696 if (pd
->port_subport_cnt
!= uinfo
->spu_subport_cnt
||
1697 pd
->userversion
!= uinfo
->spu_userversion
||
1698 pd
->port_cnt
>= pd
->port_subport_cnt
) {
1703 subport_fp(fp
) = pd
->port_cnt
++;
1704 tidcursor_fp(fp
) = 0;
1705 pd
->active_slaves
|= 1 << subport_fp(fp
);
1707 "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
1708 current
->comm
, current
->pid
,
1710 pd
->port_comm
, pd
->port_pid
,
1711 dd
->ipath_unit
, pd
->port_port
);
1721 static int ipath_open(struct inode
*in
, struct file
*fp
)
1723 /* The real work is performed later in ipath_assign_port() */
1724 fp
->private_data
= kzalloc(sizeof(struct ipath_filedata
), GFP_KERNEL
);
1725 return fp
->private_data
? 0 : -ENOMEM
;
1729 /* Get port early, so can set affinity prior to memory allocation */
1730 static int ipath_assign_port(struct file
*fp
,
1731 const struct ipath_user_info
*uinfo
)
1737 /* Check to be sure we haven't already initialized this file */
1743 /* for now, if major version is different, bail */
1744 if ((uinfo
->spu_userversion
>> 16) != IPATH_USER_SWMAJOR
) {
1745 ipath_dbg("User major version %d not same as driver "
1746 "major %d\n", uinfo
->spu_userversion
>> 16,
1747 IPATH_USER_SWMAJOR
);
1752 swminor
= uinfo
->spu_userversion
& 0xffff;
1753 if (swminor
!= IPATH_USER_SWMINOR
)
1754 ipath_dbg("User minor version %d not same as driver "
1755 "minor %d\n", swminor
, IPATH_USER_SWMINOR
);
1757 mutex_lock(&ipath_mutex
);
1759 if (swminor
== IPATH_USER_SWMINOR
&& uinfo
->spu_subport_cnt
&&
1760 (ret
= find_shared_port(fp
, uinfo
))) {
1761 mutex_unlock(&ipath_mutex
);
1767 i_minor
= iminor(fp
->f_path
.dentry
->d_inode
) - IPATH_USER_MINOR_BASE
;
1768 ipath_cdbg(VERBOSE
, "open on dev %lx (minor %d)\n",
1769 (long)fp
->f_path
.dentry
->d_inode
->i_rdev
, i_minor
);
1772 ret
= find_free_port(i_minor
- 1, fp
, uinfo
);
1774 ret
= find_best_unit(fp
, uinfo
);
1776 mutex_unlock(&ipath_mutex
);
1783 static int ipath_do_user_init(struct file
*fp
,
1784 const struct ipath_user_info
*uinfo
)
1787 struct ipath_portdata
*pd
= port_fp(fp
);
1788 struct ipath_devdata
*dd
;
1791 /* Subports don't need to initialize anything since master did it. */
1792 if (subport_fp(fp
)) {
1793 ret
= wait_event_interruptible(pd
->port_wait
,
1794 !test_bit(IPATH_PORT_MASTER_UNINIT
, &pd
->port_flag
));
1800 if (uinfo
->spu_rcvhdrsize
) {
1801 ret
= ipath_setrcvhdrsize(dd
, uinfo
->spu_rcvhdrsize
);
1806 /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
1808 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
1809 pd
->port_piobufs
= dd
->ipath_piobufbase
+
1810 dd
->ipath_pbufsport
* (pd
->port_port
- 1) * dd
->ipath_palign
;
1811 ipath_cdbg(VERBOSE
, "Set base of piobufs for port %u to 0x%x\n",
1812 pd
->port_port
, pd
->port_piobufs
);
1815 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
1816 * array for time being. If pd->port_port > chip-supported,
1817 * we need to do extra stuff here to handle by handling overflow
1818 * through port 0, someday
1820 ret
= ipath_create_rcvhdrq(dd
, pd
);
1822 ret
= ipath_create_user_egr(pd
);
1827 * set the eager head register for this port to the current values
1828 * of the tail pointers, since we don't know if they were
1829 * updated on last use of the port.
1831 head32
= ipath_read_ureg32(dd
, ur_rcvegrindextail
, pd
->port_port
);
1832 ipath_write_ureg(dd
, ur_rcvegrindexhead
, head32
, pd
->port_port
);
1833 dd
->ipath_lastegrheads
[pd
->port_port
] = -1;
1834 dd
->ipath_lastrcvhdrqtails
[pd
->port_port
] = -1;
1835 ipath_cdbg(VERBOSE
, "Wrote port%d egrhead %x from tail regs\n",
1836 pd
->port_port
, head32
);
1837 pd
->port_tidcursor
= 0; /* start at beginning after open */
1839 * now enable the port; the tail registers will be written to memory
1840 * by the chip as soon as it sees the write to
1841 * dd->ipath_kregs->kr_rcvctrl. The update only happens on
1842 * transition from 0 to 1, so clear it first, then set it as part of
1843 * enabling the port. This will (very briefly) affect any other
1844 * open ports, but it shouldn't be long enough to be an issue.
1845 * We explictly set the in-memory copy to 0 beforehand, so we don't
1846 * have to wait to be sure the DMA update has happened.
1848 *(volatile u64
*)pd
->port_rcvhdrtail_kvaddr
= 0ULL;
1849 set_bit(INFINIPATH_R_PORTENABLE_SHIFT
+ pd
->port_port
,
1850 &dd
->ipath_rcvctrl
);
1851 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1852 dd
->ipath_rcvctrl
& ~INFINIPATH_R_TAILUPD
);
1853 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1855 /* Notify any waiting slaves */
1856 if (pd
->port_subport_cnt
) {
1857 clear_bit(IPATH_PORT_MASTER_UNINIT
, &pd
->port_flag
);
1858 wake_up(&pd
->port_wait
);
1865 * unlock_exptid - unlock any expected TID entries port still had in use
1868 * We don't actually update the chip here, because we do a bulk update
1869 * below, using ipath_f_clear_tids.
1871 static void unlock_expected_tids(struct ipath_portdata
*pd
)
1873 struct ipath_devdata
*dd
= pd
->port_dd
;
1874 int port_tidbase
= pd
->port_port
* dd
->ipath_rcvtidcnt
;
1875 int i
, cnt
= 0, maxtid
= port_tidbase
+ dd
->ipath_rcvtidcnt
;
1877 ipath_cdbg(VERBOSE
, "Port %u unlocking any locked expTID pages\n",
1879 for (i
= port_tidbase
; i
< maxtid
; i
++) {
1880 if (!dd
->ipath_pageshadow
[i
])
1883 pci_unmap_page(dd
->pcidev
, dd
->ipath_physshadow
[i
],
1884 PAGE_SIZE
, PCI_DMA_FROMDEVICE
);
1885 ipath_release_user_pages_on_close(&dd
->ipath_pageshadow
[i
],
1887 dd
->ipath_pageshadow
[i
] = NULL
;
1889 ipath_stats
.sps_pageunlocks
++;
1892 ipath_cdbg(VERBOSE
, "Port %u locked %u expTID entries\n",
1893 pd
->port_port
, cnt
);
1895 if (ipath_stats
.sps_pagelocks
|| ipath_stats
.sps_pageunlocks
)
1896 ipath_cdbg(VERBOSE
, "%llu pages locked, %llu unlocked\n",
1897 (unsigned long long) ipath_stats
.sps_pagelocks
,
1898 (unsigned long long)
1899 ipath_stats
.sps_pageunlocks
);
1902 static int ipath_close(struct inode
*in
, struct file
*fp
)
1905 struct ipath_filedata
*fd
;
1906 struct ipath_portdata
*pd
;
1907 struct ipath_devdata
*dd
;
1910 ipath_cdbg(VERBOSE
, "close on dev %lx, private data %p\n",
1911 (long)in
->i_rdev
, fp
->private_data
);
1913 mutex_lock(&ipath_mutex
);
1915 fd
= (struct ipath_filedata
*) fp
->private_data
;
1916 fp
->private_data
= NULL
;
1919 mutex_unlock(&ipath_mutex
);
1922 if (--pd
->port_cnt
) {
1924 * XXX If the master closes the port before the slave(s),
1925 * revoke the mmap for the eager receive queue so
1926 * the slave(s) don't wait for receive data forever.
1928 pd
->active_slaves
&= ~(1 << fd
->subport
);
1929 mutex_unlock(&ipath_mutex
);
1932 port
= pd
->port_port
;
1935 if (pd
->port_hdrqfull
) {
1936 ipath_cdbg(PROC
, "%s[%u] had %u rcvhdrqfull errors "
1937 "during run\n", pd
->port_comm
, pd
->port_pid
,
1939 pd
->port_hdrqfull
= 0;
1942 if (pd
->port_rcvwait_to
|| pd
->port_piowait_to
1943 || pd
->port_rcvnowait
|| pd
->port_pionowait
) {
1944 ipath_cdbg(VERBOSE
, "port%u, %u rcv, %u pio wait timeo; "
1945 "%u rcv %u, pio already\n",
1946 pd
->port_port
, pd
->port_rcvwait_to
,
1947 pd
->port_piowait_to
, pd
->port_rcvnowait
,
1948 pd
->port_pionowait
);
1949 pd
->port_rcvwait_to
= pd
->port_piowait_to
=
1950 pd
->port_rcvnowait
= pd
->port_pionowait
= 0;
1952 if (pd
->port_flag
) {
1953 ipath_dbg("port %u port_flag still set to 0x%lx\n",
1954 pd
->port_port
, pd
->port_flag
);
1958 if (dd
->ipath_kregbase
) {
1960 /* atomically clear receive enable port. */
1961 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT
+ port
,
1962 &dd
->ipath_rcvctrl
);
1963 ipath_write_kreg( dd
, dd
->ipath_kregs
->kr_rcvctrl
,
1965 /* and read back from chip to be sure that nothing
1966 * else is in flight when we do the rest */
1967 (void)ipath_read_kreg64(dd
, dd
->ipath_kregs
->kr_scratch
);
1969 /* clean up the pkeys for this port user */
1970 ipath_clean_part_key(pd
, dd
);
1972 * be paranoid, and never write 0's to these, just use an
1973 * unused part of the port 0 tail page. Of course,
1974 * rcvhdraddr points to a large chunk of memory, so this
1975 * could still trash things, but at least it won't trash
1976 * page 0, and by disabling the port, it should stop "soon",
1977 * even if a packet or two is in already in flight after we
1978 * disabled the port.
1980 ipath_write_kreg_port(dd
,
1981 dd
->ipath_kregs
->kr_rcvhdrtailaddr
, port
,
1982 dd
->ipath_dummy_hdrq_phys
);
1983 ipath_write_kreg_port(dd
, dd
->ipath_kregs
->kr_rcvhdraddr
,
1984 pd
->port_port
, dd
->ipath_dummy_hdrq_phys
);
1986 i
= dd
->ipath_pbufsport
* (port
- 1);
1987 ipath_disarm_piobufs(dd
, i
, dd
->ipath_pbufsport
);
1989 dd
->ipath_f_clear_tids(dd
, pd
->port_port
);
1991 if (dd
->ipath_pageshadow
)
1992 unlock_expected_tids(pd
);
1993 ipath_stats
.sps_ports
--;
1994 ipath_cdbg(PROC
, "%s[%u] closed port %u:%u\n",
1995 pd
->port_comm
, pd
->port_pid
,
1996 dd
->ipath_unit
, port
);
2000 dd
->ipath_pd
[pd
->port_port
] = NULL
; /* before releasing mutex */
2001 mutex_unlock(&ipath_mutex
);
2002 ipath_free_pddata(dd
, pd
); /* after releasing the mutex */
2009 static int ipath_port_info(struct ipath_portdata
*pd
, u16 subport
,
2010 struct ipath_port_info __user
*uinfo
)
2012 struct ipath_port_info info
;
2017 (void) ipath_count_units(NULL
, &nup
, NULL
);
2018 info
.num_active
= nup
;
2019 info
.unit
= pd
->port_dd
->ipath_unit
;
2020 info
.port
= pd
->port_port
;
2021 info
.subport
= subport
;
2022 /* Don't return new fields if old library opened the port. */
2023 if ((pd
->userversion
& 0xffff) == IPATH_USER_SWMINOR
) {
2024 /* Number of user ports available for this device. */
2025 info
.num_ports
= pd
->port_dd
->ipath_cfgports
- 1;
2026 info
.num_subports
= pd
->port_subport_cnt
;
2029 sz
= sizeof(info
) - 2 * sizeof(u16
);
2031 if (copy_to_user(uinfo
, &info
, sz
)) {
2041 static int ipath_get_slave_info(struct ipath_portdata
*pd
,
2042 void __user
*slave_mask_addr
)
2046 if (copy_to_user(slave_mask_addr
, &pd
->active_slaves
, sizeof(u32
)))
2051 static int ipath_force_pio_avail_update(struct ipath_devdata
*dd
)
2053 u64 reg
= dd
->ipath_sendctrl
;
2055 clear_bit(IPATH_S_PIOBUFAVAILUPD
, ®
);
2056 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_sendctrl
, reg
);
2057 ipath_write_kreg(dd
, dd
->ipath_kregs
->kr_sendctrl
, dd
->ipath_sendctrl
);
2062 static ssize_t
ipath_write(struct file
*fp
, const char __user
*data
,
2063 size_t count
, loff_t
*off
)
2065 const struct ipath_cmd __user
*ucmd
;
2066 struct ipath_portdata
*pd
;
2067 const void __user
*src
;
2068 size_t consumed
, copy
;
2069 struct ipath_cmd cmd
;
2073 if (count
< sizeof(cmd
.type
)) {
2078 ucmd
= (const struct ipath_cmd __user
*) data
;
2080 if (copy_from_user(&cmd
.type
, &ucmd
->type
, sizeof(cmd
.type
))) {
2085 consumed
= sizeof(cmd
.type
);
2088 case IPATH_CMD_ASSIGN_PORT
:
2089 case __IPATH_CMD_USER_INIT
:
2090 case IPATH_CMD_USER_INIT
:
2091 copy
= sizeof(cmd
.cmd
.user_info
);
2092 dest
= &cmd
.cmd
.user_info
;
2093 src
= &ucmd
->cmd
.user_info
;
2095 case IPATH_CMD_RECV_CTRL
:
2096 copy
= sizeof(cmd
.cmd
.recv_ctrl
);
2097 dest
= &cmd
.cmd
.recv_ctrl
;
2098 src
= &ucmd
->cmd
.recv_ctrl
;
2100 case IPATH_CMD_PORT_INFO
:
2101 copy
= sizeof(cmd
.cmd
.port_info
);
2102 dest
= &cmd
.cmd
.port_info
;
2103 src
= &ucmd
->cmd
.port_info
;
2105 case IPATH_CMD_TID_UPDATE
:
2106 case IPATH_CMD_TID_FREE
:
2107 copy
= sizeof(cmd
.cmd
.tid_info
);
2108 dest
= &cmd
.cmd
.tid_info
;
2109 src
= &ucmd
->cmd
.tid_info
;
2111 case IPATH_CMD_SET_PART_KEY
:
2112 copy
= sizeof(cmd
.cmd
.part_key
);
2113 dest
= &cmd
.cmd
.part_key
;
2114 src
= &ucmd
->cmd
.part_key
;
2116 case __IPATH_CMD_SLAVE_INFO
:
2117 copy
= sizeof(cmd
.cmd
.slave_mask_addr
);
2118 dest
= &cmd
.cmd
.slave_mask_addr
;
2119 src
= &ucmd
->cmd
.slave_mask_addr
;
2121 case IPATH_CMD_PIOAVAILUPD
: // force an update of PIOAvail reg
2132 if ((count
- consumed
) < copy
) {
2137 if (copy_from_user(dest
, src
, copy
)) {
2146 if (!pd
&& cmd
.type
!= __IPATH_CMD_USER_INIT
&&
2147 cmd
.type
!= IPATH_CMD_ASSIGN_PORT
) {
2153 case IPATH_CMD_ASSIGN_PORT
:
2154 ret
= ipath_assign_port(fp
, &cmd
.cmd
.user_info
);
2158 case __IPATH_CMD_USER_INIT
:
2159 /* backwards compatibility, get port first */
2160 ret
= ipath_assign_port(fp
, &cmd
.cmd
.user_info
);
2163 /* and fall through to current version. */
2164 case IPATH_CMD_USER_INIT
:
2165 ret
= ipath_do_user_init(fp
, &cmd
.cmd
.user_info
);
2168 ret
= ipath_get_base_info(
2169 fp
, (void __user
*) (unsigned long)
2170 cmd
.cmd
.user_info
.spu_base_info
,
2171 cmd
.cmd
.user_info
.spu_base_info_size
);
2173 case IPATH_CMD_RECV_CTRL
:
2174 ret
= ipath_manage_rcvq(pd
, subport_fp(fp
), cmd
.cmd
.recv_ctrl
);
2176 case IPATH_CMD_PORT_INFO
:
2177 ret
= ipath_port_info(pd
, subport_fp(fp
),
2178 (struct ipath_port_info __user
*)
2179 (unsigned long) cmd
.cmd
.port_info
);
2181 case IPATH_CMD_TID_UPDATE
:
2182 ret
= ipath_tid_update(pd
, fp
, &cmd
.cmd
.tid_info
);
2184 case IPATH_CMD_TID_FREE
:
2185 ret
= ipath_tid_free(pd
, subport_fp(fp
), &cmd
.cmd
.tid_info
);
2187 case IPATH_CMD_SET_PART_KEY
:
2188 ret
= ipath_set_part_key(pd
, cmd
.cmd
.part_key
);
2190 case __IPATH_CMD_SLAVE_INFO
:
2191 ret
= ipath_get_slave_info(pd
,
2192 (void __user
*) (unsigned long)
2193 cmd
.cmd
.slave_mask_addr
);
2195 case IPATH_CMD_PIOAVAILUPD
:
2196 ret
= ipath_force_pio_avail_update(pd
->port_dd
);
2207 static struct class *ipath_class
;
2209 static int init_cdev(int minor
, char *name
, const struct file_operations
*fops
,
2210 struct cdev
**cdevp
, struct class_device
**class_devp
)
2212 const dev_t dev
= MKDEV(IPATH_MAJOR
, minor
);
2213 struct cdev
*cdev
= NULL
;
2214 struct class_device
*class_dev
= NULL
;
2217 cdev
= cdev_alloc();
2219 printk(KERN_ERR IPATH_DRV_NAME
2220 ": Could not allocate cdev for minor %d, %s\n",
2226 cdev
->owner
= THIS_MODULE
;
2228 kobject_set_name(&cdev
->kobj
, name
);
2230 ret
= cdev_add(cdev
, dev
, 1);
2232 printk(KERN_ERR IPATH_DRV_NAME
2233 ": Could not add cdev for minor %d, %s (err %d)\n",
2238 class_dev
= class_device_create(ipath_class
, NULL
, dev
, NULL
, name
);
2240 if (IS_ERR(class_dev
)) {
2241 ret
= PTR_ERR(class_dev
);
2242 printk(KERN_ERR IPATH_DRV_NAME
": Could not create "
2243 "class_dev for minor %d, %s (err %d)\n",
2257 *class_devp
= class_dev
;
2266 int ipath_cdev_init(int minor
, char *name
, const struct file_operations
*fops
,
2267 struct cdev
**cdevp
, struct class_device
**class_devp
)
2269 return init_cdev(minor
, name
, fops
, cdevp
, class_devp
);
2272 static void cleanup_cdev(struct cdev
**cdevp
,
2273 struct class_device
**class_devp
)
2275 struct class_device
*class_dev
= *class_devp
;
2278 class_device_unregister(class_dev
);
2288 void ipath_cdev_cleanup(struct cdev
**cdevp
,
2289 struct class_device
**class_devp
)
2291 cleanup_cdev(cdevp
, class_devp
);
2294 static struct cdev
*wildcard_cdev
;
2295 static struct class_device
*wildcard_class_dev
;
2297 static const dev_t dev
= MKDEV(IPATH_MAJOR
, 0);
2299 static int user_init(void)
2303 ret
= register_chrdev_region(dev
, IPATH_NMINORS
, IPATH_DRV_NAME
);
2305 printk(KERN_ERR IPATH_DRV_NAME
": Could not register "
2306 "chrdev region (err %d)\n", -ret
);
2310 ipath_class
= class_create(THIS_MODULE
, IPATH_DRV_NAME
);
2312 if (IS_ERR(ipath_class
)) {
2313 ret
= PTR_ERR(ipath_class
);
2314 printk(KERN_ERR IPATH_DRV_NAME
": Could not create "
2315 "device class (err %d)\n", -ret
);
2321 unregister_chrdev_region(dev
, IPATH_NMINORS
);
2326 static void user_cleanup(void)
2329 class_destroy(ipath_class
);
2333 unregister_chrdev_region(dev
, IPATH_NMINORS
);
2336 static atomic_t user_count
= ATOMIC_INIT(0);
2337 static atomic_t user_setup
= ATOMIC_INIT(0);
2339 int ipath_user_add(struct ipath_devdata
*dd
)
2344 if (atomic_inc_return(&user_count
) == 1) {
2347 ipath_dev_err(dd
, "Unable to set up user support: "
2348 "error %d\n", -ret
);
2351 ret
= init_cdev(0, "ipath", &ipath_file_ops
, &wildcard_cdev
,
2352 &wildcard_class_dev
);
2354 ipath_dev_err(dd
, "Could not create wildcard "
2355 "minor: error %d\n", -ret
);
2359 atomic_set(&user_setup
, 1);
2362 snprintf(name
, sizeof(name
), "ipath%d", dd
->ipath_unit
);
2364 ret
= init_cdev(dd
->ipath_unit
+ 1, name
, &ipath_file_ops
,
2365 &dd
->user_cdev
, &dd
->user_class_dev
);
2367 ipath_dev_err(dd
, "Could not create user minor %d, %s\n",
2368 dd
->ipath_unit
+ 1, name
);
2378 void ipath_user_remove(struct ipath_devdata
*dd
)
2380 cleanup_cdev(&dd
->user_cdev
, &dd
->user_class_dev
);
2382 if (atomic_dec_return(&user_count
) == 0) {
2383 if (atomic_read(&user_setup
) == 0)
2386 cleanup_cdev(&wildcard_cdev
, &wildcard_class_dev
);
2389 atomic_set(&user_setup
, 0);