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], RCVHQ_RCV_TYPE_EXPECTED,
402 * don't check this tid in ipath_portshadow, since we
403 * just filled it in; start with the next one.
411 /* jump here if copy out of updated info failed... */
412 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
414 /* same code that's in ipath_free_tid() */
415 limit = sizeof(tidmap) * BITS_PER_BYTE;
417 /* just in case size changes in future */
419 tid = find_first_bit((const unsigned long *)tidmap, limit);
420 for (; tid < limit; tid++) {
421 if (!test_bit(tid, tidmap))
423 if (dd->ipath_pageshadow[porttid + tid]) {
424 ipath_cdbg(VERBOSE, "Freeing TID %u\n",
426 dd->ipath_f_put_tid(dd, &tidbase[tid],
427 RCVHQ_RCV_TYPE_EXPECTED,
428 dd->ipath_tidinvalid);
429 pci_unmap_page(dd->pcidev,
430 dd->ipath_physshadow[porttid + tid],
431 PAGE_SIZE, PCI_DMA_FROMDEVICE);
432 dd->ipath_pageshadow[porttid + tid] = NULL;
433 ipath_stats.sps_pageunlocks++;
436 ipath_release_user_pages(pagep, cnt);
439 * Copy the updated array, with ipath_tid's filled in, back
440 * to user. Since we did the copy in already, this "should
441 * never fail" If it does, we have to clean up...
443 if (copy_to_user((void __user *)
444 (unsigned long) ti->tidlist,
445 tidlist, cnt * sizeof(*tidlist))) {
449 if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
450 tidmap, sizeof tidmap)) {
456 if (!pd->port_subport_cnt)
457 pd->port_tidcursor = tid;
459 tidcursor_fp(fp) = tid;
464 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
470 * ipath_tid_free - free a port TID
472 * @subport: the subport
475 * right now we are unlocking one page at a time, but since
476 * the intended use of this routine is for a single group of
477 * virtually contiguous pages, that should change to improve
478 * performance. We check that the TID is in range for this port
479 * but otherwise don't check validity; if user has an error and
480 * frees the wrong tid, it's only their own data that can thereby
481 * be corrupted. We do check that the TID was in use, for sanity
482 * We always use our idea of the saved address, not the address that
483 * they pass in to us.
486 static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
487 const struct ipath_tid_info *ti)
490 u32 tid, porttid, cnt, limit, tidcnt;
491 struct ipath_devdata *dd = pd->port_dd;
492 u64 __iomem *tidbase;
493 unsigned long tidmap[8];
495 if (!dd->ipath_pageshadow) {
500 if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
506 porttid = pd->port_port * dd->ipath_rcvtidcnt;
507 if (!pd->port_subport_cnt)
508 tidcnt = dd->ipath_rcvtidcnt;
510 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
511 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
512 porttid += dd->ipath_rcvtidcnt - tidcnt;
514 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
515 porttid += tidcnt * (subport - 1);
517 tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
518 dd->ipath_rcvtidbase +
519 porttid * sizeof(*tidbase));
521 limit = sizeof(tidmap) * BITS_PER_BYTE;
523 /* just in case size changes in future */
525 tid = find_first_bit(tidmap, limit);
526 ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
527 "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
528 limit, tid, porttid);
529 for (cnt = 0; tid < limit; tid++) {
531 * small optimization; if we detect a run of 3 or so without
532 * any set, use find_first_bit again. That's mainly to
533 * accelerate the case where we wrapped, so we have some at
534 * the beginning, and some at the end, and a big gap
537 if (!test_bit(tid, tidmap))
540 if (dd->ipath_pageshadow[porttid + tid]) {
542 p = dd->ipath_pageshadow[porttid + tid];
543 dd->ipath_pageshadow[porttid + tid] = NULL;
544 ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
546 dd->ipath_f_put_tid(dd, &tidbase[tid],
547 RCVHQ_RCV_TYPE_EXPECTED,
548 dd->ipath_tidinvalid);
549 pci_unmap_page(dd->pcidev,
550 dd->ipath_physshadow[porttid + tid],
551 PAGE_SIZE, PCI_DMA_FROMDEVICE);
552 ipath_release_user_pages(&p, 1);
553 ipath_stats.sps_pageunlocks++;
555 ipath_dbg("Unused tid %u, ignoring\n", tid);
557 if (cnt != ti->tidcnt)
558 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
562 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
568 * ipath_set_part_key - set a partition key
572 * We can have up to 4 active at a time (other than the default, which is
573 * always allowed). This is somewhat tricky, since multiple ports may set
574 * the same key, so we reference count them, and clean up at exit. All 4
575 * partition keys are packed into a single infinipath register. It's an
576 * error for a process to set the same pkey multiple times. We provide no
577 * mechanism to de-allocate a pkey at this time, we may eventually need to
578 * do that. I've used the atomic operations, and no locking, and only make
579 * a single pass through what's available. This should be more than
580 * adequate for some time. I'll think about spinlocks or the like if and as
583 static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
585 struct ipath_devdata *dd = pd->port_dd;
586 int i, any = 0, pidx = -1;
587 u16 lkey = key & 0x7FFF;
590 if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
591 /* nothing to do; this key always valid */
596 ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
597 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
598 pd->port_port, key, dd->ipath_pkeys[0],
599 atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
600 atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
601 atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
602 atomic_read(&dd->ipath_pkeyrefs[3]));
605 ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
612 * Set the full membership bit, because it has to be
613 * set in the register or the packet, and it seems
614 * cleaner to set in the register than to force all
615 * callers to set it. (see bug 4331)
619 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
620 if (!pd->port_pkeys[i] && pidx == -1)
622 if (pd->port_pkeys[i] == key) {
623 ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
624 "(%x) more than once\n",
631 ipath_dbg("All pkeys for port %u already in use, "
632 "can't set %x\n", pd->port_port, key);
636 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
637 if (!dd->ipath_pkeys[i]) {
641 if (dd->ipath_pkeys[i] == key) {
642 atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
644 if (atomic_inc_return(pkrefs) > 1) {
645 pd->port_pkeys[pidx] = key;
646 ipath_cdbg(VERBOSE, "p%u set key %x "
647 "matches #%d, count now %d\n",
648 pd->port_port, key, i,
649 atomic_read(pkrefs));
654 * lost race, decrement count, catch below
657 ipath_cdbg(VERBOSE, "Lost race, count was "
658 "0, after dec, it's %d\n",
659 atomic_read(pkrefs));
663 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
665 * It makes no sense to have both the limited and
666 * full membership PKEY set at the same time since
667 * the unlimited one will disable the limited one.
674 ipath_dbg("port %u, all pkeys already in use, "
675 "can't set %x\n", pd->port_port, key);
679 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
680 if (!dd->ipath_pkeys[i] &&
681 atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
684 /* for ipathstats, etc. */
685 ipath_stats.sps_pkeys[i] = lkey;
686 pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
688 (u64) dd->ipath_pkeys[0] |
689 ((u64) dd->ipath_pkeys[1] << 16) |
690 ((u64) dd->ipath_pkeys[2] << 32) |
691 ((u64) dd->ipath_pkeys[3] << 48);
692 ipath_cdbg(PROC, "p%u set key %x in #%d, "
693 "portidx %d, new pkey reg %llx\n",
694 pd->port_port, key, i, pidx,
695 (unsigned long long) pkey);
697 dd, dd->ipath_kregs->kr_partitionkey, pkey);
703 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
704 "can't set %x\n", pd->port_port, key);
712 * ipath_manage_rcvq - manage a port's receive queue
714 * @subport: the subport
715 * @start_stop: action to carry out
717 * start_stop == 0 disables receive on the port, for use in queue
718 * overflow conditions. start_stop==1 re-enables, to be used to
719 * re-init the software copy of the head register
721 static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
724 struct ipath_devdata *dd = pd->port_dd;
726 ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
727 start_stop ? "en" : "dis", dd->ipath_unit,
728 pd->port_port, subport);
731 /* atomically clear receive enable port. */
734 * On enable, force in-memory copy of the tail register to
735 * 0, so that protocol code doesn't have to worry about
736 * whether or not the chip has yet updated the in-memory
737 * copy or not on return from the system call. The chip
738 * always resets it's tail register back to 0 on a
739 * transition from disabled to enabled. This could cause a
740 * problem if software was broken, and did the enable w/o
741 * the disable, but eventually the in-memory copy will be
742 * updated and correct itself, even in the face of software
745 *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0;
746 set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
749 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
751 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
753 /* now be sure chip saw it before we return */
754 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
757 * And try to be sure that tail reg update has happened too.
758 * This should in theory interlock with the RXE changes to
759 * the tail register. Don't assign it to the tail register
760 * in memory copy, since we could overwrite an update by the
763 ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
765 /* always; new head should be equal to new tail; see above */
770 static void ipath_clean_part_key(struct ipath_portdata *pd,
771 struct ipath_devdata *dd)
773 int i, j, pchanged = 0;
776 /* for debugging only */
777 oldpkey = (u64) dd->ipath_pkeys[0] |
778 ((u64) dd->ipath_pkeys[1] << 16) |
779 ((u64) dd->ipath_pkeys[2] << 32) |
780 ((u64) dd->ipath_pkeys[3] << 48);
782 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
783 if (!pd->port_pkeys[i])
785 ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
787 for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
788 /* check for match independent of the global bit */
789 if ((dd->ipath_pkeys[j] & 0x7fff) !=
790 (pd->port_pkeys[i] & 0x7fff))
792 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
793 ipath_cdbg(VERBOSE, "p%u clear key "
796 pd->port_pkeys[i], j);
797 ipath_stats.sps_pkeys[j] =
798 dd->ipath_pkeys[j] = 0;
802 VERBOSE, "p%u key %x matches #%d, "
803 "but ref still %d\n", pd->port_port,
804 pd->port_pkeys[i], j,
805 atomic_read(&dd->ipath_pkeyrefs[j]));
808 pd->port_pkeys[i] = 0;
811 u64 pkey = (u64) dd->ipath_pkeys[0] |
812 ((u64) dd->ipath_pkeys[1] << 16) |
813 ((u64) dd->ipath_pkeys[2] << 32) |
814 ((u64) dd->ipath_pkeys[3] << 48);
815 ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
816 "new pkey reg %llx\n", pd->port_port,
817 (unsigned long long) oldpkey,
818 (unsigned long long) pkey);
819 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
825 * Initialize the port data with the receive buffer sizes
826 * so this can be done while the master port is locked.
827 * Otherwise, there is a race with a slave opening the port
828 * and seeing these fields uninitialized.
830 static void init_user_egr_sizes(struct ipath_portdata *pd)
832 struct ipath_devdata *dd = pd->port_dd;
833 unsigned egrperchunk, egrcnt, size;
836 * to avoid wasting a lot of memory, we allocate 32KB chunks of
837 * physically contiguous memory, advance through it until used up
838 * and then allocate more. Of course, we need memory to store those
839 * extra pointers, now. Started out with 256KB, but under heavy
840 * memory pressure (creating large files and then copying them over
841 * NFS while doing lots of MPI jobs), we hit some allocation
842 * failures, even though we can sleep... (2.6.10) Still get
843 * failures at 64K. 32K is the lowest we can go without wasting
847 egrperchunk = size / dd->ipath_rcvegrbufsize;
848 egrcnt = dd->ipath_rcvegrcnt;
849 pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
850 pd->port_rcvegrbufs_perchunk = egrperchunk;
851 pd->port_rcvegrbuf_size = size;
855 * ipath_create_user_egr - allocate eager TID buffers
856 * @pd: the port to allocate TID buffers for
858 * This routine is now quite different for user and kernel, because
859 * the kernel uses skb's, for the accelerated network performance
860 * This is the user port version
862 * Allocate the eager TID buffers and program them into infinipath
863 * They are no longer completely contiguous, we do multiple allocation
866 static int ipath_create_user_egr(struct ipath_portdata *pd)
868 struct ipath_devdata *dd = pd->port_dd;
869 unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
875 * GFP_USER, but without GFP_FS, so buffer cache can be
876 * coalesced (we hope); otherwise, even at order 4,
877 * heavy filesystem activity makes these fail, and we can
878 * use compound pages.
880 gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
882 egrcnt = dd->ipath_rcvegrcnt;
883 /* TID number offset for this port */
884 egroff = pd->port_port * egrcnt;
885 egrsize = dd->ipath_rcvegrbufsize;
886 ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
887 "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
889 chunk = pd->port_rcvegrbuf_chunks;
890 egrperchunk = pd->port_rcvegrbufs_perchunk;
891 size = pd->port_rcvegrbuf_size;
892 pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
894 if (!pd->port_rcvegrbuf) {
898 pd->port_rcvegrbuf_phys =
899 kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
901 if (!pd->port_rcvegrbuf_phys) {
905 for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
907 pd->port_rcvegrbuf[e] = dma_alloc_coherent(
908 &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
911 if (!pd->port_rcvegrbuf[e]) {
913 goto bail_rcvegrbuf_phys;
917 pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
919 for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
920 dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
923 for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
924 dd->ipath_f_put_tid(dd, e + egroff +
928 dd->ipath_rcvegrbase),
929 RCVHQ_RCV_TYPE_EAGER, pa);
932 cond_resched(); /* don't hog the cpu */
939 for (e = 0; e < pd->port_rcvegrbuf_chunks &&
940 pd->port_rcvegrbuf[e]; e++) {
941 dma_free_coherent(&dd->pcidev->dev, size,
942 pd->port_rcvegrbuf[e],
943 pd->port_rcvegrbuf_phys[e]);
946 kfree(pd->port_rcvegrbuf_phys);
947 pd->port_rcvegrbuf_phys = NULL;
949 kfree(pd->port_rcvegrbuf);
950 pd->port_rcvegrbuf = NULL;
956 /* common code for the mappings on dma_alloc_coherent mem */
957 static int ipath_mmap_mem(struct vm_area_struct *vma,
958 struct ipath_portdata *pd, unsigned len, int write_ok,
959 void *kvaddr, char *what)
961 struct ipath_devdata *dd = pd->port_dd;
965 if ((vma->vm_end - vma->vm_start) > len) {
966 dev_info(&dd->pcidev->dev,
967 "FAIL on %s: len %lx > %x\n", what,
968 vma->vm_end - vma->vm_start, len);
974 if (vma->vm_flags & VM_WRITE) {
975 dev_info(&dd->pcidev->dev,
976 "%s must be mapped readonly\n", what);
981 /* don't allow them to later change with mprotect */
982 vma->vm_flags &= ~VM_MAYWRITE;
985 pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
986 ret = remap_pfn_range(vma, vma->vm_start, pfn,
987 len, vma->vm_page_prot);
989 dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
990 "bytes r%c failed: %d\n", what, pd->port_port,
991 pfn, len, write_ok?'w':'o', ret);
993 ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
994 "r%c\n", what, pd->port_port, pfn, len,
1000 static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
1007 * This is real hardware, so use io_remap. This is the mechanism
1008 * for the user process to update the head registers for their port
1011 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
1012 dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
1013 "%lx > PAGE\n", vma->vm_end - vma->vm_start);
1016 phys = dd->ipath_physaddr + ureg;
1017 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1019 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1020 ret = io_remap_pfn_range(vma, vma->vm_start,
1022 vma->vm_end - vma->vm_start,
1028 static int mmap_piobufs(struct vm_area_struct *vma,
1029 struct ipath_devdata *dd,
1030 struct ipath_portdata *pd,
1031 unsigned piobufs, unsigned piocnt)
1037 * When we map the PIO buffers in the chip, we want to map them as
1038 * writeonly, no read possible. This prevents access to previous
1039 * process data, and catches users who might try to read the i/o
1040 * space due to a bug.
1042 if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
1043 dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
1044 "reqlen %lx > PAGE\n",
1045 vma->vm_end - vma->vm_start);
1050 phys = dd->ipath_physaddr + piobufs;
1053 * Don't mark this as non-cached, or we don't get the
1054 * write combining behavior we want on the PIO buffers!
1057 #if defined(__powerpc__)
1058 /* There isn't a generic way to specify writethrough mappings */
1059 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1060 pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
1061 pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
1065 * don't allow them to later change to readable with mprotect (for when
1066 * not initially mapped readable, as is normally the case)
1068 vma->vm_flags &= ~VM_MAYREAD;
1069 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1071 ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
1072 vma->vm_end - vma->vm_start,
1078 static int mmap_rcvegrbufs(struct vm_area_struct *vma,
1079 struct ipath_portdata *pd)
1081 struct ipath_devdata *dd = pd->port_dd;
1082 unsigned long start, size;
1083 size_t total_size, i;
1087 size = pd->port_rcvegrbuf_size;
1088 total_size = pd->port_rcvegrbuf_chunks * size;
1089 if ((vma->vm_end - vma->vm_start) > total_size) {
1090 dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
1091 "reqlen %lx > actual %lx\n",
1092 vma->vm_end - vma->vm_start,
1093 (unsigned long) total_size);
1098 if (vma->vm_flags & VM_WRITE) {
1099 dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
1100 "writable (flags=%lx)\n", vma->vm_flags);
1104 /* don't allow them to later change to writeable with mprotect */
1105 vma->vm_flags &= ~VM_MAYWRITE;
1107 start = vma->vm_start;
1109 for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
1110 pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
1111 ret = remap_pfn_range(vma, start, pfn, size,
1123 * ipath_file_vma_nopage - handle a VMA page fault.
1125 static struct page *ipath_file_vma_nopage(struct vm_area_struct *vma,
1126 unsigned long address, int *type)
1128 unsigned long offset = address - vma->vm_start;
1129 struct page *page = NOPAGE_SIGBUS;
1133 * Convert the vmalloc address into a struct page.
1135 pageptr = (void *)(offset + (vma->vm_pgoff << PAGE_SHIFT));
1136 page = vmalloc_to_page(pageptr);
1140 /* Increment the reference count. */
1143 *type = VM_FAULT_MINOR;
1148 static struct vm_operations_struct ipath_file_vm_ops = {
1149 .nopage = ipath_file_vma_nopage,
1152 static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
1153 struct ipath_portdata *pd, unsigned subport)
1156 struct ipath_devdata *dd;
1161 /* If the port is not shared, all addresses should be physical */
1162 if (!pd->port_subport_cnt)
1166 size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
1169 * Each process has all the subport uregbase, rcvhdrq, and
1170 * rcvegrbufs mmapped - as an array for all the processes,
1171 * and also separately for this process.
1173 if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
1174 addr = pd->subport_uregbase;
1175 size = PAGE_SIZE * pd->port_subport_cnt;
1176 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
1177 addr = pd->subport_rcvhdr_base;
1178 size = pd->port_rcvhdrq_size * pd->port_subport_cnt;
1179 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
1180 addr = pd->subport_rcvegrbuf;
1181 size *= pd->port_subport_cnt;
1182 } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
1183 PAGE_SIZE * subport)) {
1184 addr = pd->subport_uregbase + PAGE_SIZE * subport;
1186 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
1187 pd->port_rcvhdrq_size * subport)) {
1188 addr = pd->subport_rcvhdr_base +
1189 pd->port_rcvhdrq_size * subport;
1190 size = pd->port_rcvhdrq_size;
1191 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
1193 addr = pd->subport_rcvegrbuf + size * subport;
1194 /* rcvegrbufs are read-only on the slave */
1195 if (vma->vm_flags & VM_WRITE) {
1196 dev_info(&dd->pcidev->dev,
1197 "Can't map eager buffers as "
1198 "writable (flags=%lx)\n", vma->vm_flags);
1203 * Don't allow permission to later change to writeable
1206 vma->vm_flags &= ~VM_MAYWRITE;
1210 len = vma->vm_end - vma->vm_start;
1212 ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
1217 vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
1218 vma->vm_ops = &ipath_file_vm_ops;
1219 vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
1227 * ipath_mmap - mmap various structures into user space
1228 * @fp: the file pointer
1231 * We use this to have a shared buffer between the kernel and the user code
1232 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1233 * buffers in the chip. We have the open and close entries so we can bump
1234 * the ref count and keep the driver from being unloaded while still mapped.
1236 static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
1238 struct ipath_portdata *pd;
1239 struct ipath_devdata *dd;
1241 unsigned piobufs, piocnt;
1252 * This is the ipath_do_user_init() code, mapping the shared buffers
1253 * into the user process. The address referred to by vm_pgoff is the
1254 * file offset passed via mmap(). For shared ports, this is the
1255 * kernel vmalloc() address of the pages to share with the master.
1256 * For non-shared or master ports, this is a physical address.
1257 * We only do one mmap for each space mapped.
1259 pgaddr = vma->vm_pgoff << PAGE_SHIFT;
1262 * Check for 0 in case one of the allocations failed, but user
1263 * called mmap anyway.
1270 ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
1271 (unsigned long long) pgaddr, vma->vm_start,
1272 vma->vm_end - vma->vm_start, dd->ipath_unit,
1273 pd->port_port, subport_fp(fp));
1276 * Physical addresses must fit in 40 bits for our hardware.
1277 * Check for kernel virtual addresses first, anything else must
1278 * match a HW or memory address.
1280 ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
1287 ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
1288 if (!pd->port_subport_cnt) {
1289 /* port is not shared */
1290 piocnt = dd->ipath_pbufsport;
1291 piobufs = pd->port_piobufs;
1292 } else if (!subport_fp(fp)) {
1293 /* caller is the master */
1294 piocnt = (dd->ipath_pbufsport / pd->port_subport_cnt) +
1295 (dd->ipath_pbufsport % pd->port_subport_cnt);
1296 piobufs = pd->port_piobufs +
1297 dd->ipath_palign * (dd->ipath_pbufsport - piocnt);
1299 unsigned slave = subport_fp(fp) - 1;
1301 /* caller is a slave */
1302 piocnt = dd->ipath_pbufsport / pd->port_subport_cnt;
1303 piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
1307 ret = mmap_ureg(vma, dd, ureg);
1308 else if (pgaddr == piobufs)
1309 ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
1310 else if (pgaddr == dd->ipath_pioavailregs_phys)
1311 /* in-memory copy of pioavail registers */
1312 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1313 (void *) dd->ipath_pioavailregs_dma,
1314 "pioavail registers");
1315 else if (pgaddr == pd->port_rcvegr_phys)
1316 ret = mmap_rcvegrbufs(vma, pd);
1317 else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
1319 * The rcvhdrq itself; readonly except on HT (so have
1320 * to allow writable mapping), multiple pages, contiguous
1321 * from an i/o perspective.
1323 ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
1326 else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
1327 /* in-memory copy of rcvhdrq tail register */
1328 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1329 pd->port_rcvhdrtail_kvaddr,
1334 vma->vm_private_data = NULL;
1337 dev_info(&dd->pcidev->dev,
1338 "Failure %d on off %llx len %lx\n",
1339 -ret, (unsigned long long)pgaddr,
1340 vma->vm_end - vma->vm_start);
1345 static unsigned ipath_poll_hdrqfull(struct ipath_portdata *pd)
1347 unsigned pollflag = 0;
1349 if ((pd->poll_type & IPATH_POLL_TYPE_OVERFLOW) &&
1350 pd->port_hdrqfull != pd->port_hdrqfull_poll) {
1351 pollflag |= POLLIN | POLLRDNORM;
1352 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1358 static unsigned int ipath_poll_urgent(struct ipath_portdata *pd,
1360 struct poll_table_struct *pt)
1362 unsigned pollflag = 0;
1363 struct ipath_devdata *dd;
1367 /* variable access in ipath_poll_hdrqfull() needs this */
1369 pollflag = ipath_poll_hdrqfull(pd);
1371 if (pd->port_urgent != pd->port_urgent_poll) {
1372 pollflag |= POLLIN | POLLRDNORM;
1373 pd->port_urgent_poll = pd->port_urgent;
1377 /* this saves a spin_lock/unlock in interrupt handler... */
1378 set_bit(IPATH_PORT_WAITING_URG, &pd->port_flag);
1379 /* flush waiting flag so don't miss an event... */
1381 poll_wait(fp, &pd->port_wait, pt);
1387 static unsigned int ipath_poll_next(struct ipath_portdata *pd,
1389 struct poll_table_struct *pt)
1393 unsigned pollflag = 0;
1394 struct ipath_devdata *dd;
1398 /* variable access in ipath_poll_hdrqfull() needs this */
1400 pollflag = ipath_poll_hdrqfull(pd);
1402 head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
1403 tail = *(volatile u64 *)pd->port_rcvhdrtail_kvaddr;
1406 pollflag |= POLLIN | POLLRDNORM;
1408 /* this saves a spin_lock/unlock in interrupt handler */
1409 set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
1410 /* flush waiting flag so we don't miss an event */
1413 set_bit(pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT,
1414 &dd->ipath_rcvctrl);
1416 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1419 if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
1420 ipath_write_ureg(dd, ur_rcvhdrhead,
1421 dd->ipath_rhdrhead_intr_off | head,
1424 poll_wait(fp, &pd->port_wait, pt);
1430 static unsigned int ipath_poll(struct file *fp,
1431 struct poll_table_struct *pt)
1433 struct ipath_portdata *pd;
1439 else if (pd->poll_type & IPATH_POLL_TYPE_URGENT)
1440 pollflag = ipath_poll_urgent(pd, fp, pt);
1442 pollflag = ipath_poll_next(pd, fp, pt);
1447 static int ipath_supports_subports(int user_swmajor, int user_swminor)
1449 /* no subport implementation prior to software version 1.3 */
1450 return (user_swmajor > 1) || (user_swminor >= 3);
1453 static int ipath_compatible_subports(int user_swmajor, int user_swminor)
1455 /* this code is written long-hand for clarity */
1456 if (IPATH_USER_SWMAJOR != user_swmajor) {
1457 /* no promise of compatibility if major mismatch */
1460 if (IPATH_USER_SWMAJOR == 1) {
1461 switch (IPATH_USER_SWMINOR) {
1465 /* no subport implementation so cannot be compatible */
1468 /* 3 is only compatible with itself */
1469 return user_swminor == 3;
1471 /* >= 4 are compatible (or are expected to be) */
1472 return user_swminor >= 4;
1475 /* make no promises yet for future major versions */
1479 static int init_subports(struct ipath_devdata *dd,
1480 struct ipath_portdata *pd,
1481 const struct ipath_user_info *uinfo)
1484 unsigned num_subports;
1488 * If the user is requesting zero subports,
1489 * skip the subport allocation.
1491 if (uinfo->spu_subport_cnt <= 0)
1494 /* Self-consistency check for ipath_compatible_subports() */
1495 if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&
1496 !ipath_compatible_subports(IPATH_USER_SWMAJOR,
1497 IPATH_USER_SWMINOR)) {
1498 dev_info(&dd->pcidev->dev,
1499 "Inconsistent ipath_compatible_subports()\n");
1503 /* Check for subport compatibility */
1504 if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,
1505 uinfo->spu_userversion & 0xffff)) {
1506 dev_info(&dd->pcidev->dev,
1507 "Mismatched user version (%d.%d) and driver "
1508 "version (%d.%d) while port sharing. Ensure "
1509 "that driver and library are from the same "
1511 (int) (uinfo->spu_userversion >> 16),
1512 (int) (uinfo->spu_userversion & 0xffff),
1514 IPATH_USER_SWMINOR);
1517 if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
1522 num_subports = uinfo->spu_subport_cnt;
1523 pd->subport_uregbase = vmalloc(PAGE_SIZE * num_subports);
1524 if (!pd->subport_uregbase) {
1528 /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
1529 size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
1530 sizeof(u32), PAGE_SIZE) * num_subports;
1531 pd->subport_rcvhdr_base = vmalloc(size);
1532 if (!pd->subport_rcvhdr_base) {
1537 pd->subport_rcvegrbuf = vmalloc(pd->port_rcvegrbuf_chunks *
1538 pd->port_rcvegrbuf_size *
1540 if (!pd->subport_rcvegrbuf) {
1545 pd->port_subport_cnt = uinfo->spu_subport_cnt;
1546 pd->port_subport_id = uinfo->spu_subport_id;
1547 pd->active_slaves = 1;
1548 set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1549 memset(pd->subport_uregbase, 0, PAGE_SIZE * num_subports);
1550 memset(pd->subport_rcvhdr_base, 0, size);
1551 memset(pd->subport_rcvegrbuf, 0, pd->port_rcvegrbuf_chunks *
1552 pd->port_rcvegrbuf_size *
1557 vfree(pd->subport_rcvhdr_base);
1559 vfree(pd->subport_uregbase);
1560 pd->subport_uregbase = NULL;
1565 static int try_alloc_port(struct ipath_devdata *dd, int port,
1567 const struct ipath_user_info *uinfo)
1569 struct ipath_portdata *pd;
1572 if (!(pd = dd->ipath_pd[port])) {
1575 pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
1578 * Allocate memory for use in ipath_tid_update() just once
1579 * at open, not per call. Reduces cost of expected send
1582 ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
1583 dd->ipath_rcvtidcnt * sizeof(struct page **),
1586 ipath_dev_err(dd, "Unable to allocate portdata "
1587 "memory, failing open\n");
1593 dd->ipath_pd[port] = pd;
1594 dd->ipath_pd[port]->port_port = port;
1595 dd->ipath_pd[port]->port_dd = dd;
1596 dd->ipath_pd[port]->port_tid_pg_list = ptmp;
1597 init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
1599 if (!pd->port_cnt) {
1600 pd->userversion = uinfo->spu_userversion;
1601 init_user_egr_sizes(pd);
1602 if ((ret = init_subports(dd, pd, uinfo)) != 0)
1604 ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
1605 current->comm, current->pid, dd->ipath_unit,
1609 pd->port_pid = current->pid;
1610 strncpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
1611 ipath_stats.sps_ports++;
1620 static inline int usable(struct ipath_devdata *dd)
1623 (dd->ipath_flags & IPATH_PRESENT) &&
1624 dd->ipath_kregbase &&
1626 !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
1630 static int find_free_port(int unit, struct file *fp,
1631 const struct ipath_user_info *uinfo)
1633 struct ipath_devdata *dd = ipath_lookup(unit);
1646 for (i = 1; i < dd->ipath_cfgports; i++) {
1647 ret = try_alloc_port(dd, i, fp, uinfo);
1657 static int find_best_unit(struct file *fp,
1658 const struct ipath_user_info *uinfo)
1660 int ret = 0, i, prefunit = -1, devmax;
1661 int maxofallports, npresent, nup;
1664 devmax = ipath_count_units(&npresent, &nup, &maxofallports);
1667 * This code is present to allow a knowledgeable person to
1668 * specify the layout of processes to processors before opening
1669 * this driver, and then we'll assign the process to the "closest"
1670 * InfiniPath chip to that processor (we assume reasonable connectivity,
1671 * for now). This code assumes that if affinity has been set
1672 * before this point, that at most one cpu is set; for now this
1673 * is reasonable. I check for both cpus_empty() and cpus_full(),
1674 * in case some kernel variant sets none of the bits when no
1675 * affinity is set. 2.6.11 and 12 kernels have all present
1676 * cpus set. Some day we'll have to fix it up further to handle
1677 * a cpu subset. This algorithm fails for two HT chips connected
1678 * in tunnel fashion. Eventually this needs real topology
1679 * information. There may be some issues with dual core numbering
1680 * as well. This needs more work prior to release.
1682 if (!cpus_empty(current->cpus_allowed) &&
1683 !cpus_full(current->cpus_allowed)) {
1684 int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
1685 for (i = 0; i < ncpus; i++)
1686 if (cpu_isset(i, current->cpus_allowed)) {
1687 ipath_cdbg(PROC, "%s[%u] affinity set for "
1688 "cpu %d/%d\n", current->comm,
1689 current->pid, i, ncpus);
1693 if (curcpu != -1 && nset != ncpus) {
1695 prefunit = curcpu / (ncpus / npresent);
1696 ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
1697 "%d cpus/chip, select unit %d\n",
1698 current->comm, current->pid,
1699 npresent, ncpus, ncpus / npresent,
1706 * user ports start at 1, kernel port is 0
1707 * For now, we do round-robin access across all chips
1711 devmax = prefunit + 1;
1713 for (i = 1; i < maxofallports; i++) {
1714 for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
1716 struct ipath_devdata *dd = ipath_lookup(ndev);
1719 continue; /* can't use this unit */
1720 if (i >= dd->ipath_cfgports)
1722 * Maxed out on users of this unit. Try
1726 ret = try_alloc_port(dd, i, fp, uinfo);
1735 ipath_dbg("No ports available (none initialized "
1739 /* if started above 0, retry from 0 */
1741 "%s[%u] no ports on prefunit "
1742 "%d, clear and re-check\n",
1743 current->comm, current->pid,
1745 devmax = ipath_count_units(NULL, NULL,
1751 ipath_dbg("No ports available\n");
1755 ipath_dbg("No boards found\n");
1762 static int find_shared_port(struct file *fp,
1763 const struct ipath_user_info *uinfo)
1765 int devmax, ndev, i;
1768 devmax = ipath_count_units(NULL, NULL, NULL);
1770 for (ndev = 0; ndev < devmax; ndev++) {
1771 struct ipath_devdata *dd = ipath_lookup(ndev);
1775 for (i = 1; i < dd->ipath_cfgports; i++) {
1776 struct ipath_portdata *pd = dd->ipath_pd[i];
1778 /* Skip ports which are not yet open */
1779 if (!pd || !pd->port_cnt)
1781 /* Skip port if it doesn't match the requested one */
1782 if (pd->port_subport_id != uinfo->spu_subport_id)
1784 /* Verify the sharing process matches the master */
1785 if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
1786 pd->userversion != uinfo->spu_userversion ||
1787 pd->port_cnt >= pd->port_subport_cnt) {
1792 subport_fp(fp) = pd->port_cnt++;
1793 tidcursor_fp(fp) = 0;
1794 pd->active_slaves |= 1 << subport_fp(fp);
1796 "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
1797 current->comm, current->pid,
1799 pd->port_comm, pd->port_pid,
1800 dd->ipath_unit, pd->port_port);
1810 static int ipath_open(struct inode *in, struct file *fp)
1812 /* The real work is performed later in ipath_assign_port() */
1813 fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
1814 return fp->private_data ? 0 : -ENOMEM;
1817 /* Get port early, so can set affinity prior to memory allocation */
1818 static int ipath_assign_port(struct file *fp,
1819 const struct ipath_user_info *uinfo)
1823 unsigned swmajor, swminor;
1825 /* Check to be sure we haven't already initialized this file */
1831 /* for now, if major version is different, bail */
1832 swmajor = uinfo->spu_userversion >> 16;
1833 if (swmajor != IPATH_USER_SWMAJOR) {
1834 ipath_dbg("User major version %d not same as driver "
1835 "major %d\n", uinfo->spu_userversion >> 16,
1836 IPATH_USER_SWMAJOR);
1841 swminor = uinfo->spu_userversion & 0xffff;
1842 if (swminor != IPATH_USER_SWMINOR)
1843 ipath_dbg("User minor version %d not same as driver "
1844 "minor %d\n", swminor, IPATH_USER_SWMINOR);
1846 mutex_lock(&ipath_mutex);
1848 if (ipath_compatible_subports(swmajor, swminor) &&
1849 uinfo->spu_subport_cnt &&
1850 (ret = find_shared_port(fp, uinfo))) {
1851 mutex_unlock(&ipath_mutex);
1857 i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;
1858 ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
1859 (long)fp->f_path.dentry->d_inode->i_rdev, i_minor);
1862 ret = find_free_port(i_minor - 1, fp, uinfo);
1864 ret = find_best_unit(fp, uinfo);
1866 mutex_unlock(&ipath_mutex);
1873 static int ipath_do_user_init(struct file *fp,
1874 const struct ipath_user_info *uinfo)
1877 struct ipath_portdata *pd = port_fp(fp);
1878 struct ipath_devdata *dd;
1881 /* Subports don't need to initialize anything since master did it. */
1882 if (subport_fp(fp)) {
1883 ret = wait_event_interruptible(pd->port_wait,
1884 !test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
1890 if (uinfo->spu_rcvhdrsize) {
1891 ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
1896 /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
1898 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
1899 pd->port_piobufs = dd->ipath_piobufbase +
1900 dd->ipath_pbufsport * (pd->port_port - 1) * dd->ipath_palign;
1901 ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n",
1902 pd->port_port, pd->port_piobufs);
1905 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
1906 * array for time being. If pd->port_port > chip-supported,
1907 * we need to do extra stuff here to handle by handling overflow
1908 * through port 0, someday
1910 ret = ipath_create_rcvhdrq(dd, pd);
1912 ret = ipath_create_user_egr(pd);
1917 * set the eager head register for this port to the current values
1918 * of the tail pointers, since we don't know if they were
1919 * updated on last use of the port.
1921 head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
1922 ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
1923 dd->ipath_lastegrheads[pd->port_port] = -1;
1924 dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
1925 ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
1926 pd->port_port, head32);
1927 pd->port_tidcursor = 0; /* start at beginning after open */
1929 /* initialize poll variables... */
1930 pd->port_urgent = 0;
1931 pd->port_urgent_poll = 0;
1932 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1935 * now enable the port; the tail registers will be written to memory
1936 * by the chip as soon as it sees the write to
1937 * dd->ipath_kregs->kr_rcvctrl. The update only happens on
1938 * transition from 0 to 1, so clear it first, then set it as part of
1939 * enabling the port. This will (very briefly) affect any other
1940 * open ports, but it shouldn't be long enough to be an issue.
1941 * We explictly set the in-memory copy to 0 beforehand, so we don't
1942 * have to wait to be sure the DMA update has happened.
1944 *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0ULL;
1945 set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
1946 &dd->ipath_rcvctrl);
1947 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1948 dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD);
1949 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1951 /* Notify any waiting slaves */
1952 if (pd->port_subport_cnt) {
1953 clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1954 wake_up(&pd->port_wait);
1961 * unlock_exptid - unlock any expected TID entries port still had in use
1964 * We don't actually update the chip here, because we do a bulk update
1965 * below, using ipath_f_clear_tids.
1967 static void unlock_expected_tids(struct ipath_portdata *pd)
1969 struct ipath_devdata *dd = pd->port_dd;
1970 int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
1971 int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
1973 ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
1975 for (i = port_tidbase; i < maxtid; i++) {
1976 if (!dd->ipath_pageshadow[i])
1979 pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
1980 PAGE_SIZE, PCI_DMA_FROMDEVICE);
1981 ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i],
1983 dd->ipath_pageshadow[i] = NULL;
1985 ipath_stats.sps_pageunlocks++;
1988 ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
1989 pd->port_port, cnt);
1991 if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
1992 ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
1993 (unsigned long long) ipath_stats.sps_pagelocks,
1994 (unsigned long long)
1995 ipath_stats.sps_pageunlocks);
1998 static int ipath_close(struct inode *in, struct file *fp)
2001 struct ipath_filedata *fd;
2002 struct ipath_portdata *pd;
2003 struct ipath_devdata *dd;
2006 ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
2007 (long)in->i_rdev, fp->private_data);
2009 mutex_lock(&ipath_mutex);
2011 fd = (struct ipath_filedata *) fp->private_data;
2012 fp->private_data = NULL;
2015 mutex_unlock(&ipath_mutex);
2018 if (--pd->port_cnt) {
2020 * XXX If the master closes the port before the slave(s),
2021 * revoke the mmap for the eager receive queue so
2022 * the slave(s) don't wait for receive data forever.
2024 pd->active_slaves &= ~(1 << fd->subport);
2025 mutex_unlock(&ipath_mutex);
2028 port = pd->port_port;
2031 if (pd->port_hdrqfull) {
2032 ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
2033 "during run\n", pd->port_comm, pd->port_pid,
2035 pd->port_hdrqfull = 0;
2038 if (pd->port_rcvwait_to || pd->port_piowait_to
2039 || pd->port_rcvnowait || pd->port_pionowait) {
2040 ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
2041 "%u rcv %u, pio already\n",
2042 pd->port_port, pd->port_rcvwait_to,
2043 pd->port_piowait_to, pd->port_rcvnowait,
2044 pd->port_pionowait);
2045 pd->port_rcvwait_to = pd->port_piowait_to =
2046 pd->port_rcvnowait = pd->port_pionowait = 0;
2048 if (pd->port_flag) {
2049 ipath_dbg("port %u port_flag still set to 0x%lx\n",
2050 pd->port_port, pd->port_flag);
2054 if (dd->ipath_kregbase) {
2056 /* atomically clear receive enable port and intr avail. */
2057 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port,
2058 &dd->ipath_rcvctrl);
2059 clear_bit(pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT,
2060 &dd->ipath_rcvctrl);
2061 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
2063 /* and read back from chip to be sure that nothing
2064 * else is in flight when we do the rest */
2065 (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
2067 /* clean up the pkeys for this port user */
2068 ipath_clean_part_key(pd, dd);
2070 * be paranoid, and never write 0's to these, just use an
2071 * unused part of the port 0 tail page. Of course,
2072 * rcvhdraddr points to a large chunk of memory, so this
2073 * could still trash things, but at least it won't trash
2074 * page 0, and by disabling the port, it should stop "soon",
2075 * even if a packet or two is in already in flight after we
2076 * disabled the port.
2078 ipath_write_kreg_port(dd,
2079 dd->ipath_kregs->kr_rcvhdrtailaddr, port,
2080 dd->ipath_dummy_hdrq_phys);
2081 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
2082 pd->port_port, dd->ipath_dummy_hdrq_phys);
2084 i = dd->ipath_pbufsport * (port - 1);
2085 ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport);
2087 dd->ipath_f_clear_tids(dd, pd->port_port);
2089 if (dd->ipath_pageshadow)
2090 unlock_expected_tids(pd);
2091 ipath_stats.sps_ports--;
2092 ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
2093 pd->port_comm, pd->port_pid,
2094 dd->ipath_unit, port);
2098 dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
2099 mutex_unlock(&ipath_mutex);
2100 ipath_free_pddata(dd, pd); /* after releasing the mutex */
2107 static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
2108 struct ipath_port_info __user *uinfo)
2110 struct ipath_port_info info;
2115 (void) ipath_count_units(NULL, &nup, NULL);
2116 info.num_active = nup;
2117 info.unit = pd->port_dd->ipath_unit;
2118 info.port = pd->port_port;
2119 info.subport = subport;
2120 /* Don't return new fields if old library opened the port. */
2121 if (ipath_supports_subports(pd->userversion >> 16,
2122 pd->userversion & 0xffff)) {
2123 /* Number of user ports available for this device. */
2124 info.num_ports = pd->port_dd->ipath_cfgports - 1;
2125 info.num_subports = pd->port_subport_cnt;
2128 sz = sizeof(info) - 2 * sizeof(u16);
2130 if (copy_to_user(uinfo, &info, sz)) {
2140 static int ipath_get_slave_info(struct ipath_portdata *pd,
2141 void __user *slave_mask_addr)
2145 if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
2150 static int ipath_force_pio_avail_update(struct ipath_devdata *dd)
2152 u64 reg = dd->ipath_sendctrl;
2154 clear_bit(IPATH_S_PIOBUFAVAILUPD, ®);
2155 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, reg);
2156 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, dd->ipath_sendctrl);
2161 static ssize_t ipath_write(struct file *fp, const char __user *data,
2162 size_t count, loff_t *off)
2164 const struct ipath_cmd __user *ucmd;
2165 struct ipath_portdata *pd;
2166 const void __user *src;
2167 size_t consumed, copy;
2168 struct ipath_cmd cmd;
2172 if (count < sizeof(cmd.type)) {
2177 ucmd = (const struct ipath_cmd __user *) data;
2179 if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
2184 consumed = sizeof(cmd.type);
2187 case IPATH_CMD_ASSIGN_PORT:
2188 case __IPATH_CMD_USER_INIT:
2189 case IPATH_CMD_USER_INIT:
2190 copy = sizeof(cmd.cmd.user_info);
2191 dest = &cmd.cmd.user_info;
2192 src = &ucmd->cmd.user_info;
2194 case IPATH_CMD_RECV_CTRL:
2195 copy = sizeof(cmd.cmd.recv_ctrl);
2196 dest = &cmd.cmd.recv_ctrl;
2197 src = &ucmd->cmd.recv_ctrl;
2199 case IPATH_CMD_PORT_INFO:
2200 copy = sizeof(cmd.cmd.port_info);
2201 dest = &cmd.cmd.port_info;
2202 src = &ucmd->cmd.port_info;
2204 case IPATH_CMD_TID_UPDATE:
2205 case IPATH_CMD_TID_FREE:
2206 copy = sizeof(cmd.cmd.tid_info);
2207 dest = &cmd.cmd.tid_info;
2208 src = &ucmd->cmd.tid_info;
2210 case IPATH_CMD_SET_PART_KEY:
2211 copy = sizeof(cmd.cmd.part_key);
2212 dest = &cmd.cmd.part_key;
2213 src = &ucmd->cmd.part_key;
2215 case __IPATH_CMD_SLAVE_INFO:
2216 copy = sizeof(cmd.cmd.slave_mask_addr);
2217 dest = &cmd.cmd.slave_mask_addr;
2218 src = &ucmd->cmd.slave_mask_addr;
2220 case IPATH_CMD_PIOAVAILUPD: // force an update of PIOAvail reg
2225 case IPATH_CMD_POLL_TYPE:
2226 copy = sizeof(cmd.cmd.poll_type);
2227 dest = &cmd.cmd.poll_type;
2228 src = &ucmd->cmd.poll_type;
2236 if ((count - consumed) < copy) {
2241 if (copy_from_user(dest, src, copy)) {
2250 if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
2251 cmd.type != IPATH_CMD_ASSIGN_PORT) {
2257 case IPATH_CMD_ASSIGN_PORT:
2258 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2262 case __IPATH_CMD_USER_INIT:
2263 /* backwards compatibility, get port first */
2264 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2267 /* and fall through to current version. */
2268 case IPATH_CMD_USER_INIT:
2269 ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
2272 ret = ipath_get_base_info(
2273 fp, (void __user *) (unsigned long)
2274 cmd.cmd.user_info.spu_base_info,
2275 cmd.cmd.user_info.spu_base_info_size);
2277 case IPATH_CMD_RECV_CTRL:
2278 ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
2280 case IPATH_CMD_PORT_INFO:
2281 ret = ipath_port_info(pd, subport_fp(fp),
2282 (struct ipath_port_info __user *)
2283 (unsigned long) cmd.cmd.port_info);
2285 case IPATH_CMD_TID_UPDATE:
2286 ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
2288 case IPATH_CMD_TID_FREE:
2289 ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
2291 case IPATH_CMD_SET_PART_KEY:
2292 ret = ipath_set_part_key(pd, cmd.cmd.part_key);
2294 case __IPATH_CMD_SLAVE_INFO:
2295 ret = ipath_get_slave_info(pd,
2296 (void __user *) (unsigned long)
2297 cmd.cmd.slave_mask_addr);
2299 case IPATH_CMD_PIOAVAILUPD:
2300 ret = ipath_force_pio_avail_update(pd->port_dd);
2302 case IPATH_CMD_POLL_TYPE:
2303 pd->poll_type = cmd.cmd.poll_type;
2314 static struct class *ipath_class;
2316 static int init_cdev(int minor, char *name, const struct file_operations *fops,
2317 struct cdev **cdevp, struct class_device **class_devp)
2319 const dev_t dev = MKDEV(IPATH_MAJOR, minor);
2320 struct cdev *cdev = NULL;
2321 struct class_device *class_dev = NULL;
2324 cdev = cdev_alloc();
2326 printk(KERN_ERR IPATH_DRV_NAME
2327 ": Could not allocate cdev for minor %d, %s\n",
2333 cdev->owner = THIS_MODULE;
2335 kobject_set_name(&cdev->kobj, name);
2337 ret = cdev_add(cdev, dev, 1);
2339 printk(KERN_ERR IPATH_DRV_NAME
2340 ": Could not add cdev for minor %d, %s (err %d)\n",
2345 class_dev = class_device_create(ipath_class, NULL, dev, NULL, name);
2347 if (IS_ERR(class_dev)) {
2348 ret = PTR_ERR(class_dev);
2349 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2350 "class_dev for minor %d, %s (err %d)\n",
2364 *class_devp = class_dev;
2373 int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
2374 struct cdev **cdevp, struct class_device **class_devp)
2376 return init_cdev(minor, name, fops, cdevp, class_devp);
2379 static void cleanup_cdev(struct cdev **cdevp,
2380 struct class_device **class_devp)
2382 struct class_device *class_dev = *class_devp;
2385 class_device_unregister(class_dev);
2395 void ipath_cdev_cleanup(struct cdev **cdevp,
2396 struct class_device **class_devp)
2398 cleanup_cdev(cdevp, class_devp);
2401 static struct cdev *wildcard_cdev;
2402 static struct class_device *wildcard_class_dev;
2404 static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
2406 static int user_init(void)
2410 ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
2412 printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
2413 "chrdev region (err %d)\n", -ret);
2417 ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
2419 if (IS_ERR(ipath_class)) {
2420 ret = PTR_ERR(ipath_class);
2421 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2422 "device class (err %d)\n", -ret);
2428 unregister_chrdev_region(dev, IPATH_NMINORS);
2433 static void user_cleanup(void)
2436 class_destroy(ipath_class);
2440 unregister_chrdev_region(dev, IPATH_NMINORS);
2443 static atomic_t user_count = ATOMIC_INIT(0);
2444 static atomic_t user_setup = ATOMIC_INIT(0);
2446 int ipath_user_add(struct ipath_devdata *dd)
2451 if (atomic_inc_return(&user_count) == 1) {
2454 ipath_dev_err(dd, "Unable to set up user support: "
2455 "error %d\n", -ret);
2458 ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
2459 &wildcard_class_dev);
2461 ipath_dev_err(dd, "Could not create wildcard "
2462 "minor: error %d\n", -ret);
2466 atomic_set(&user_setup, 1);
2469 snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
2471 ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
2472 &dd->user_cdev, &dd->user_class_dev);
2474 ipath_dev_err(dd, "Could not create user minor %d, %s\n",
2475 dd->ipath_unit + 1, name);
2485 void ipath_user_remove(struct ipath_devdata *dd)
2487 cleanup_cdev(&dd->user_cdev, &dd->user_class_dev);
2489 if (atomic_dec_return(&user_count) == 0) {
2490 if (atomic_read(&user_setup) == 0)
2493 cleanup_cdev(&wildcard_cdev, &wildcard_class_dev);
2496 atomic_set(&user_setup, 0);