IB/ipath: Fix port sharing on powerpc
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / infiniband / hw / ipath / ipath_file_ops.c
CommitLineData
7f510b46 1/*
759d5768 2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
7f510b46
BS
3 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 */
33
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>
40
41#include "ipath_kernel.h"
27b678dd 42#include "ipath_common.h"
7f510b46
BS
43
44static int ipath_open(struct inode *, struct file *);
45static int ipath_close(struct inode *, struct file *);
46static ssize_t ipath_write(struct file *, const char __user *, size_t,
47 loff_t *);
48static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
49static int ipath_mmap(struct file *, struct vm_area_struct *);
50
2b8693c0 51static const struct file_operations ipath_file_ops = {
7f510b46
BS
52 .owner = THIS_MODULE,
53 .write = ipath_write,
54 .open = ipath_open,
55 .release = ipath_close,
56 .poll = ipath_poll,
57 .mmap = ipath_mmap
58};
59
0a5a83cf
RC
60/*
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.
65 */
66static u64 cvt_kvaddr(void *p)
67{
68 struct page *page;
69 u64 paddr = 0;
70
71 page = vmalloc_to_page(p);
72 if (page)
73 paddr = page_to_pfn(page) << PAGE_SHIFT;
74
75 return paddr;
76}
77
9929b0fb 78static int ipath_get_base_info(struct file *fp,
7f510b46
BS
79 void __user *ubase, size_t ubase_size)
80{
9929b0fb 81 struct ipath_portdata *pd = port_fp(fp);
7f510b46
BS
82 int ret = 0;
83 struct ipath_base_info *kinfo = NULL;
84 struct ipath_devdata *dd = pd->port_dd;
9929b0fb
BS
85 unsigned subport_cnt;
86 int shared, master;
87 size_t sz;
88
89 subport_cnt = pd->port_subport_cnt;
90 if (!subport_cnt) {
91 shared = 0;
92 master = 0;
93 subport_cnt = 1;
94 } else {
95 shared = 1;
96 master = !subport_fp(fp);
97 }
7f510b46 98
9929b0fb
BS
99 sz = sizeof(*kinfo);
100 /* If port sharing is not requested, allow the old size structure */
101 if (!shared)
102 sz -= 3 * sizeof(u64);
103 if (ubase_size < sz) {
7f510b46 104 ipath_cdbg(PROC,
9929b0fb
BS
105 "Base size %zu, need %zu (version mismatch?)\n",
106 ubase_size, sz);
7f510b46
BS
107 ret = -EINVAL;
108 goto bail;
109 }
110
111 kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
112 if (kinfo == NULL) {
113 ret = -ENOMEM;
114 goto bail;
115 }
116
117 ret = dd->ipath_f_get_base_info(pd, kinfo);
118 if (ret < 0)
119 goto bail;
120
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;
125 /*
126 * have to mmap whole thing
127 */
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;
9929b0fb
BS
133 kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;
134 if (master)
135 kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;
7f510b46
BS
136 /*
137 * for this use, may be ipath_cfgports summed over all chips that
138 * are are configured and present
139 */
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;
145
146 /*
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
149 * as an array.
150 *
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
9929b0fb
BS
158 * no doubt some future kernel release will change that, and we'll be
159 * on to yet another method of dealing with this.
7f510b46
BS
160 */
161 kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
9929b0fb 162 kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;
7f510b46
BS
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;
9929b0fb
BS
168 if (!shared) {
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;
173 } else if (master) {
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 +
178 dd->ipath_palign *
179 (dd->ipath_pbufsport - kinfo->spi_piocnt);
180 kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
181 dd->ipath_palign * pd->port_port;
182 } else {
183 unsigned slave = subport_fp(fp) - 1;
7f510b46 184
9929b0fb
BS
185 kinfo->spi_piocnt = dd->ipath_pbufsport / subport_cnt;
186 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
187 dd->ipath_palign * kinfo->spi_piocnt * slave;
0a5a83cf
RC
188 kinfo->__spi_uregbase = cvt_kvaddr(pd->subport_uregbase +
189 PAGE_SIZE * slave);
9929b0fb 190
0a5a83cf
RC
191 kinfo->spi_rcvhdr_base = cvt_kvaddr(pd->subport_rcvhdr_base +
192 pd->port_rcvhdrq_size * slave);
947d7617 193 kinfo->spi_rcvhdr_tailaddr = 0;
0a5a83cf
RC
194 kinfo->spi_rcv_egrbufs = cvt_kvaddr(pd->subport_rcvegrbuf +
195 dd->ipath_rcvegrcnt * dd->ipath_rcvegrbufsize * slave);
9929b0fb
BS
196 }
197
198 kinfo->spi_pioindex = (kinfo->spi_piobufbase - dd->ipath_piobufbase) /
199 dd->ipath_palign;
7f510b46
BS
200 kinfo->spi_pioalign = dd->ipath_palign;
201
202 kinfo->spi_qpair = IPATH_KD_QP;
203 kinfo->spi_piosize = dd->ipath_ibmaxlen;
204 kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */
205 kinfo->spi_port = pd->port_port;
9929b0fb 206 kinfo->spi_subport = subport_fp(fp);
eaf6733b 207 kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
7f510b46
BS
208 kinfo->spi_hw_version = dd->ipath_revision;
209
9929b0fb
BS
210 if (master) {
211 kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;
212 kinfo->spi_subport_uregbase =
0a5a83cf 213 cvt_kvaddr(pd->subport_uregbase);
9929b0fb 214 kinfo->spi_subport_rcvegrbuf =
0a5a83cf 215 cvt_kvaddr(pd->subport_rcvegrbuf);
9929b0fb 216 kinfo->spi_subport_rcvhdr_base =
0a5a83cf 217 cvt_kvaddr(pd->subport_rcvhdr_base);
9929b0fb 218 ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",
0624b072
BS
219 kinfo->spi_port, kinfo->spi_runtime_flags,
220 (unsigned long long) kinfo->spi_subport_uregbase,
221 (unsigned long long) kinfo->spi_subport_rcvegrbuf,
222 (unsigned long long) kinfo->spi_subport_rcvhdr_base);
9929b0fb
BS
223 }
224
7f510b46
BS
225 if (copy_to_user(ubase, kinfo, sizeof(*kinfo)))
226 ret = -EFAULT;
227
228bail:
229 kfree(kinfo);
230 return ret;
231}
232
233/**
234 * ipath_tid_update - update a port TID
235 * @pd: the port
9929b0fb 236 * @fp: the ipath device file
7f510b46
BS
237 * @ti: the TID information
238 *
239 * The new implementation as of Oct 2004 is that the driver assigns
240 * the tid and returns it to the caller. To make it easier to
241 * catch bugs, and to reduce search time, we keep a cursor for
242 * each port, walking the shadow tid array to find one that's not
243 * in use.
244 *
245 * For now, if we can't allocate the full list, we fail, although
246 * in the long run, we'll allocate as many as we can, and the
247 * caller will deal with that by trying the remaining pages later.
248 * That means that when we fail, we have to mark the tids as not in
249 * use again, in our shadow copy.
250 *
251 * It's up to the caller to free the tids when they are done.
252 * We'll unlock the pages as they free them.
253 *
254 * Also, right now we are locking one page at a time, but since
255 * the intended use of this routine is for a single group of
256 * virtually contiguous pages, that should change to improve
257 * performance.
258 */
9929b0fb 259static int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,
7f510b46
BS
260 const struct ipath_tid_info *ti)
261{
262 int ret = 0, ntids;
9929b0fb 263 u32 tid, porttid, cnt, i, tidcnt, tidoff;
7f510b46
BS
264 u16 *tidlist;
265 struct ipath_devdata *dd = pd->port_dd;
266 u64 physaddr;
267 unsigned long vaddr;
268 u64 __iomem *tidbase;
269 unsigned long tidmap[8];
270 struct page **pagep = NULL;
9929b0fb 271 unsigned subport = subport_fp(fp);
7f510b46
BS
272
273 if (!dd->ipath_pageshadow) {
274 ret = -ENOMEM;
275 goto done;
276 }
277
278 cnt = ti->tidcnt;
279 if (!cnt) {
280 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
281 (unsigned long long) ti->tidlist);
282 /*
283 * Should we treat as success? likely a bug
284 */
285 ret = -EFAULT;
286 goto done;
287 }
9929b0fb
BS
288 porttid = pd->port_port * dd->ipath_rcvtidcnt;
289 if (!pd->port_subport_cnt) {
290 tidcnt = dd->ipath_rcvtidcnt;
291 tid = pd->port_tidcursor;
292 tidoff = 0;
293 } else if (!subport) {
294 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
295 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
296 tidoff = dd->ipath_rcvtidcnt - tidcnt;
297 porttid += tidoff;
298 tid = tidcursor_fp(fp);
299 } else {
300 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
301 tidoff = tidcnt * (subport - 1);
302 porttid += tidoff;
303 tid = tidcursor_fp(fp);
304 }
305 if (cnt > tidcnt) {
7f510b46
BS
306 /* make sure it all fits in port_tid_pg_list */
307 dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
308 "TIDs, only trying max (%u)\n", cnt, tidcnt);
309 cnt = tidcnt;
310 }
9929b0fb
BS
311 pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];
312 tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];
7f510b46
BS
313
314 memset(tidmap, 0, sizeof(tidmap));
7f510b46 315 /* before decrement; chip actual # */
7f510b46
BS
316 ntids = tidcnt;
317 tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
318 dd->ipath_rcvtidbase +
319 porttid * sizeof(*tidbase));
320
321 ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
322 pd->port_port, cnt, tid, tidbase);
323
324 /* virtual address of first page in transfer */
325 vaddr = ti->tidvaddr;
326 if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
327 cnt * PAGE_SIZE)) {
328 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
329 (void *)vaddr, cnt);
330 ret = -EFAULT;
331 goto done;
332 }
333 ret = ipath_get_user_pages(vaddr, cnt, pagep);
334 if (ret) {
335 if (ret == -EBUSY) {
336 ipath_dbg("Failed to lock addr %p, %u pages "
337 "(already locked)\n",
338 (void *) vaddr, cnt);
339 /*
340 * for now, continue, and see what happens but with
341 * the new implementation, this should never happen,
342 * unless perhaps the user has mpin'ed the pages
343 * themselves (something we need to test)
344 */
345 ret = 0;
346 } else {
347 dev_info(&dd->pcidev->dev,
348 "Failed to lock addr %p, %u pages: "
349 "errno %d\n", (void *) vaddr, cnt, -ret);
350 goto done;
351 }
352 }
353 for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
354 for (; ntids--; tid++) {
355 if (tid == tidcnt)
356 tid = 0;
357 if (!dd->ipath_pageshadow[porttid + tid])
358 break;
359 }
360 if (ntids < 0) {
361 /*
362 * oops, wrapped all the way through their TIDs,
363 * and didn't have enough free; see comments at
364 * start of routine
365 */
366 ipath_dbg("Not enough free TIDs for %u pages "
367 "(index %d), failing\n", cnt, i);
368 i--; /* last tidlist[i] not filled in */
369 ret = -ENOMEM;
370 break;
371 }
9929b0fb 372 tidlist[i] = tid + tidoff;
7f510b46 373 ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
9929b0fb 374 "vaddr %lx\n", i, tid + tidoff, vaddr);
7f510b46
BS
375 /* we "know" system pages and TID pages are same size */
376 dd->ipath_pageshadow[porttid + tid] = pagep[i];
1fd3b40f
BS
377 dd->ipath_physshadow[porttid + tid] = ipath_map_page(
378 dd->pcidev, pagep[i], 0, PAGE_SIZE,
379 PCI_DMA_FROMDEVICE);
7f510b46
BS
380 /*
381 * don't need atomic or it's overhead
382 */
383 __set_bit(tid, tidmap);
1fd3b40f 384 physaddr = dd->ipath_physshadow[porttid + tid];
7f510b46
BS
385 ipath_stats.sps_pagelocks++;
386 ipath_cdbg(VERBOSE,
387 "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
388 tid, vaddr, (unsigned long long) physaddr,
389 pagep[i]);
390 dd->ipath_f_put_tid(dd, &tidbase[tid], 1, physaddr);
391 /*
392 * don't check this tid in ipath_portshadow, since we
393 * just filled it in; start with the next one.
394 */
395 tid++;
396 }
397
398 if (ret) {
399 u32 limit;
400 cleanup:
401 /* jump here if copy out of updated info failed... */
402 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
403 -ret, i, cnt);
404 /* same code that's in ipath_free_tid() */
405 limit = sizeof(tidmap) * BITS_PER_BYTE;
406 if (limit > tidcnt)
407 /* just in case size changes in future */
408 limit = tidcnt;
409 tid = find_first_bit((const unsigned long *)tidmap, limit);
410 for (; tid < limit; tid++) {
411 if (!test_bit(tid, tidmap))
412 continue;
413 if (dd->ipath_pageshadow[porttid + tid]) {
414 ipath_cdbg(VERBOSE, "Freeing TID %u\n",
415 tid);
416 dd->ipath_f_put_tid(dd, &tidbase[tid], 1,
417 dd->ipath_tidinvalid);
1fd3b40f
BS
418 pci_unmap_page(dd->pcidev,
419 dd->ipath_physshadow[porttid + tid],
420 PAGE_SIZE, PCI_DMA_FROMDEVICE);
7f510b46
BS
421 dd->ipath_pageshadow[porttid + tid] = NULL;
422 ipath_stats.sps_pageunlocks++;
423 }
424 }
425 ipath_release_user_pages(pagep, cnt);
426 } else {
427 /*
428 * Copy the updated array, with ipath_tid's filled in, back
429 * to user. Since we did the copy in already, this "should
430 * never fail" If it does, we have to clean up...
431 */
432 if (copy_to_user((void __user *)
433 (unsigned long) ti->tidlist,
434 tidlist, cnt * sizeof(*tidlist))) {
435 ret = -EFAULT;
436 goto cleanup;
437 }
438 if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
439 tidmap, sizeof tidmap)) {
440 ret = -EFAULT;
441 goto cleanup;
442 }
443 if (tid == tidcnt)
444 tid = 0;
9929b0fb
BS
445 if (!pd->port_subport_cnt)
446 pd->port_tidcursor = tid;
447 else
448 tidcursor_fp(fp) = tid;
7f510b46
BS
449 }
450
451done:
452 if (ret)
453 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
454 ti->tidcnt, -ret);
455 return ret;
456}
457
458/**
459 * ipath_tid_free - free a port TID
460 * @pd: the port
9929b0fb 461 * @subport: the subport
7f510b46
BS
462 * @ti: the TID info
463 *
464 * right now we are unlocking one page at a time, but since
465 * the intended use of this routine is for a single group of
466 * virtually contiguous pages, that should change to improve
467 * performance. We check that the TID is in range for this port
468 * but otherwise don't check validity; if user has an error and
469 * frees the wrong tid, it's only their own data that can thereby
470 * be corrupted. We do check that the TID was in use, for sanity
471 * We always use our idea of the saved address, not the address that
472 * they pass in to us.
473 */
474
9929b0fb 475static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
7f510b46
BS
476 const struct ipath_tid_info *ti)
477{
478 int ret = 0;
479 u32 tid, porttid, cnt, limit, tidcnt;
480 struct ipath_devdata *dd = pd->port_dd;
481 u64 __iomem *tidbase;
482 unsigned long tidmap[8];
483
484 if (!dd->ipath_pageshadow) {
485 ret = -ENOMEM;
486 goto done;
487 }
488
489 if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
490 sizeof tidmap)) {
491 ret = -EFAULT;
492 goto done;
493 }
494
495 porttid = pd->port_port * dd->ipath_rcvtidcnt;
9929b0fb
BS
496 if (!pd->port_subport_cnt)
497 tidcnt = dd->ipath_rcvtidcnt;
498 else if (!subport) {
499 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
500 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
501 porttid += dd->ipath_rcvtidcnt - tidcnt;
502 } else {
503 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
504 porttid += tidcnt * (subport - 1);
505 }
7f510b46
BS
506 tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
507 dd->ipath_rcvtidbase +
508 porttid * sizeof(*tidbase));
509
7f510b46
BS
510 limit = sizeof(tidmap) * BITS_PER_BYTE;
511 if (limit > tidcnt)
512 /* just in case size changes in future */
513 limit = tidcnt;
514 tid = find_first_bit(tidmap, limit);
515 ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
516 "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
517 limit, tid, porttid);
518 for (cnt = 0; tid < limit; tid++) {
519 /*
520 * small optimization; if we detect a run of 3 or so without
521 * any set, use find_first_bit again. That's mainly to
522 * accelerate the case where we wrapped, so we have some at
523 * the beginning, and some at the end, and a big gap
524 * in the middle.
525 */
526 if (!test_bit(tid, tidmap))
527 continue;
528 cnt++;
529 if (dd->ipath_pageshadow[porttid + tid]) {
530 ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
531 pd->port_pid, tid);
532 dd->ipath_f_put_tid(dd, &tidbase[tid], 1,
533 dd->ipath_tidinvalid);
1fd3b40f
BS
534 pci_unmap_page(dd->pcidev,
535 dd->ipath_physshadow[porttid + tid],
536 PAGE_SIZE, PCI_DMA_FROMDEVICE);
7f510b46
BS
537 ipath_release_user_pages(
538 &dd->ipath_pageshadow[porttid + tid], 1);
539 dd->ipath_pageshadow[porttid + tid] = NULL;
540 ipath_stats.sps_pageunlocks++;
541 } else
542 ipath_dbg("Unused tid %u, ignoring\n", tid);
543 }
544 if (cnt != ti->tidcnt)
545 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
546 ti->tidcnt, cnt);
547done:
548 if (ret)
549 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
550 ti->tidcnt, -ret);
551 return ret;
552}
553
554/**
555 * ipath_set_part_key - set a partition key
556 * @pd: the port
557 * @key: the key
558 *
559 * We can have up to 4 active at a time (other than the default, which is
560 * always allowed). This is somewhat tricky, since multiple ports may set
561 * the same key, so we reference count them, and clean up at exit. All 4
562 * partition keys are packed into a single infinipath register. It's an
563 * error for a process to set the same pkey multiple times. We provide no
564 * mechanism to de-allocate a pkey at this time, we may eventually need to
565 * do that. I've used the atomic operations, and no locking, and only make
566 * a single pass through what's available. This should be more than
567 * adequate for some time. I'll think about spinlocks or the like if and as
568 * it's necessary.
569 */
570static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
571{
572 struct ipath_devdata *dd = pd->port_dd;
573 int i, any = 0, pidx = -1;
574 u16 lkey = key & 0x7FFF;
575 int ret;
576
27b678dd 577 if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
7f510b46
BS
578 /* nothing to do; this key always valid */
579 ret = 0;
580 goto bail;
581 }
582
583 ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
584 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
585 pd->port_port, key, dd->ipath_pkeys[0],
586 atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
587 atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
588 atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
589 atomic_read(&dd->ipath_pkeyrefs[3]));
590
591 if (!lkey) {
592 ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
593 pd->port_port);
594 ret = -EINVAL;
595 goto bail;
596 }
597
598 /*
599 * Set the full membership bit, because it has to be
600 * set in the register or the packet, and it seems
601 * cleaner to set in the register than to force all
602 * callers to set it. (see bug 4331)
603 */
604 key |= 0x8000;
605
606 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
607 if (!pd->port_pkeys[i] && pidx == -1)
608 pidx = i;
609 if (pd->port_pkeys[i] == key) {
610 ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
611 "(%x) more than once\n",
612 pd->port_port, key);
613 ret = -EEXIST;
614 goto bail;
615 }
616 }
617 if (pidx == -1) {
618 ipath_dbg("All pkeys for port %u already in use, "
619 "can't set %x\n", pd->port_port, key);
620 ret = -EBUSY;
621 goto bail;
622 }
623 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
624 if (!dd->ipath_pkeys[i]) {
625 any++;
626 continue;
627 }
628 if (dd->ipath_pkeys[i] == key) {
629 atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
630
631 if (atomic_inc_return(pkrefs) > 1) {
632 pd->port_pkeys[pidx] = key;
633 ipath_cdbg(VERBOSE, "p%u set key %x "
634 "matches #%d, count now %d\n",
635 pd->port_port, key, i,
636 atomic_read(pkrefs));
637 ret = 0;
638 goto bail;
639 } else {
640 /*
641 * lost race, decrement count, catch below
642 */
643 atomic_dec(pkrefs);
644 ipath_cdbg(VERBOSE, "Lost race, count was "
645 "0, after dec, it's %d\n",
646 atomic_read(pkrefs));
647 any++;
648 }
649 }
650 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
651 /*
652 * It makes no sense to have both the limited and
653 * full membership PKEY set at the same time since
654 * the unlimited one will disable the limited one.
655 */
656 ret = -EEXIST;
657 goto bail;
658 }
659 }
660 if (!any) {
661 ipath_dbg("port %u, all pkeys already in use, "
662 "can't set %x\n", pd->port_port, key);
663 ret = -EBUSY;
664 goto bail;
665 }
666 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
667 if (!dd->ipath_pkeys[i] &&
668 atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
669 u64 pkey;
670
671 /* for ipathstats, etc. */
672 ipath_stats.sps_pkeys[i] = lkey;
673 pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
674 pkey =
675 (u64) dd->ipath_pkeys[0] |
676 ((u64) dd->ipath_pkeys[1] << 16) |
677 ((u64) dd->ipath_pkeys[2] << 32) |
678 ((u64) dd->ipath_pkeys[3] << 48);
679 ipath_cdbg(PROC, "p%u set key %x in #%d, "
680 "portidx %d, new pkey reg %llx\n",
681 pd->port_port, key, i, pidx,
682 (unsigned long long) pkey);
683 ipath_write_kreg(
684 dd, dd->ipath_kregs->kr_partitionkey, pkey);
685
686 ret = 0;
687 goto bail;
688 }
689 }
690 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
691 "can't set %x\n", pd->port_port, key);
692 ret = -EBUSY;
693
694bail:
695 return ret;
696}
697
698/**
699 * ipath_manage_rcvq - manage a port's receive queue
700 * @pd: the port
9929b0fb 701 * @subport: the subport
7f510b46
BS
702 * @start_stop: action to carry out
703 *
704 * start_stop == 0 disables receive on the port, for use in queue
705 * overflow conditions. start_stop==1 re-enables, to be used to
706 * re-init the software copy of the head register
707 */
9929b0fb
BS
708static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
709 int start_stop)
7f510b46
BS
710{
711 struct ipath_devdata *dd = pd->port_dd;
7f510b46 712
9929b0fb 713 ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
7f510b46 714 start_stop ? "en" : "dis", dd->ipath_unit,
9929b0fb
BS
715 pd->port_port, subport);
716 if (subport)
717 goto bail;
7f510b46
BS
718 /* atomically clear receive enable port. */
719 if (start_stop) {
720 /*
721 * On enable, force in-memory copy of the tail register to
722 * 0, so that protocol code doesn't have to worry about
723 * whether or not the chip has yet updated the in-memory
724 * copy or not on return from the system call. The chip
725 * always resets it's tail register back to 0 on a
726 * transition from disabled to enabled. This could cause a
727 * problem if software was broken, and did the enable w/o
728 * the disable, but eventually the in-memory copy will be
729 * updated and correct itself, even in the face of software
730 * bugs.
731 */
1fd3b40f 732 *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0;
7f510b46
BS
733 set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
734 &dd->ipath_rcvctrl);
735 } else
736 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
737 &dd->ipath_rcvctrl);
738 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
739 dd->ipath_rcvctrl);
740 /* now be sure chip saw it before we return */
44f8e3f3 741 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
7f510b46
BS
742 if (start_stop) {
743 /*
744 * And try to be sure that tail reg update has happened too.
745 * This should in theory interlock with the RXE changes to
746 * the tail register. Don't assign it to the tail register
747 * in memory copy, since we could overwrite an update by the
748 * chip if we did.
749 */
44f8e3f3 750 ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
7f510b46
BS
751 }
752 /* always; new head should be equal to new tail; see above */
9929b0fb 753bail:
7f510b46
BS
754 return 0;
755}
756
757static void ipath_clean_part_key(struct ipath_portdata *pd,
758 struct ipath_devdata *dd)
759{
760 int i, j, pchanged = 0;
761 u64 oldpkey;
762
763 /* for debugging only */
764 oldpkey = (u64) dd->ipath_pkeys[0] |
765 ((u64) dd->ipath_pkeys[1] << 16) |
766 ((u64) dd->ipath_pkeys[2] << 32) |
767 ((u64) dd->ipath_pkeys[3] << 48);
768
769 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
770 if (!pd->port_pkeys[i])
771 continue;
772 ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
773 pd->port_pkeys[i]);
774 for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
775 /* check for match independent of the global bit */
776 if ((dd->ipath_pkeys[j] & 0x7fff) !=
777 (pd->port_pkeys[i] & 0x7fff))
778 continue;
779 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
780 ipath_cdbg(VERBOSE, "p%u clear key "
781 "%x matches #%d\n",
782 pd->port_port,
783 pd->port_pkeys[i], j);
784 ipath_stats.sps_pkeys[j] =
785 dd->ipath_pkeys[j] = 0;
786 pchanged++;
787 }
788 else ipath_cdbg(
789 VERBOSE, "p%u key %x matches #%d, "
790 "but ref still %d\n", pd->port_port,
791 pd->port_pkeys[i], j,
792 atomic_read(&dd->ipath_pkeyrefs[j]));
793 break;
794 }
795 pd->port_pkeys[i] = 0;
796 }
797 if (pchanged) {
798 u64 pkey = (u64) dd->ipath_pkeys[0] |
799 ((u64) dd->ipath_pkeys[1] << 16) |
800 ((u64) dd->ipath_pkeys[2] << 32) |
801 ((u64) dd->ipath_pkeys[3] << 48);
802 ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
803 "new pkey reg %llx\n", pd->port_port,
804 (unsigned long long) oldpkey,
805 (unsigned long long) pkey);
806 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
807 pkey);
808 }
809}
810
9929b0fb
BS
811/*
812 * Initialize the port data with the receive buffer sizes
813 * so this can be done while the master port is locked.
814 * Otherwise, there is a race with a slave opening the port
815 * and seeing these fields uninitialized.
816 */
817static void init_user_egr_sizes(struct ipath_portdata *pd)
818{
819 struct ipath_devdata *dd = pd->port_dd;
820 unsigned egrperchunk, egrcnt, size;
821
822 /*
823 * to avoid wasting a lot of memory, we allocate 32KB chunks of
824 * physically contiguous memory, advance through it until used up
825 * and then allocate more. Of course, we need memory to store those
826 * extra pointers, now. Started out with 256KB, but under heavy
827 * memory pressure (creating large files and then copying them over
828 * NFS while doing lots of MPI jobs), we hit some allocation
829 * failures, even though we can sleep... (2.6.10) Still get
830 * failures at 64K. 32K is the lowest we can go without wasting
831 * additional memory.
832 */
833 size = 0x8000;
834 egrperchunk = size / dd->ipath_rcvegrbufsize;
835 egrcnt = dd->ipath_rcvegrcnt;
836 pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
837 pd->port_rcvegrbufs_perchunk = egrperchunk;
838 pd->port_rcvegrbuf_size = size;
839}
840
7f510b46
BS
841/**
842 * ipath_create_user_egr - allocate eager TID buffers
843 * @pd: the port to allocate TID buffers for
844 *
845 * This routine is now quite different for user and kernel, because
846 * the kernel uses skb's, for the accelerated network performance
847 * This is the user port version
848 *
849 * Allocate the eager TID buffers and program them into infinipath
850 * They are no longer completely contiguous, we do multiple allocation
851 * calls.
852 */
853static int ipath_create_user_egr(struct ipath_portdata *pd)
854{
855 struct ipath_devdata *dd = pd->port_dd;
9929b0fb 856 unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
7f510b46
BS
857 size_t size;
858 int ret;
0ed9a4a0
BS
859 gfp_t gfp_flags;
860
861 /*
862 * GFP_USER, but without GFP_FS, so buffer cache can be
863 * coalesced (we hope); otherwise, even at order 4,
864 * heavy filesystem activity makes these fail, and we can
865 * use compound pages.
866 */
867 gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
7f510b46
BS
868
869 egrcnt = dd->ipath_rcvegrcnt;
870 /* TID number offset for this port */
871 egroff = pd->port_port * egrcnt;
872 egrsize = dd->ipath_rcvegrbufsize;
873 ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
874 "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
875
9929b0fb
BS
876 chunk = pd->port_rcvegrbuf_chunks;
877 egrperchunk = pd->port_rcvegrbufs_perchunk;
878 size = pd->port_rcvegrbuf_size;
879 pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
880 GFP_KERNEL);
7f510b46
BS
881 if (!pd->port_rcvegrbuf) {
882 ret = -ENOMEM;
883 goto bail;
884 }
885 pd->port_rcvegrbuf_phys =
9929b0fb
BS
886 kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
887 GFP_KERNEL);
7f510b46
BS
888 if (!pd->port_rcvegrbuf_phys) {
889 ret = -ENOMEM;
890 goto bail_rcvegrbuf;
891 }
892 for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
7f510b46
BS
893
894 pd->port_rcvegrbuf[e] = dma_alloc_coherent(
895 &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
896 gfp_flags);
897
898 if (!pd->port_rcvegrbuf[e]) {
899 ret = -ENOMEM;
900 goto bail_rcvegrbuf_phys;
901 }
902 }
903
904 pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
905
906 for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
907 dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
908 unsigned i;
909
910 for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
911 dd->ipath_f_put_tid(dd, e + egroff +
912 (u64 __iomem *)
913 ((char __iomem *)
914 dd->ipath_kregbase +
915 dd->ipath_rcvegrbase), 0, pa);
916 pa += egrsize;
917 }
918 cond_resched(); /* don't hog the cpu */
919 }
920
921 ret = 0;
922 goto bail;
923
924bail_rcvegrbuf_phys:
925 for (e = 0; e < pd->port_rcvegrbuf_chunks &&
f37bda92 926 pd->port_rcvegrbuf[e]; e++) {
7f510b46
BS
927 dma_free_coherent(&dd->pcidev->dev, size,
928 pd->port_rcvegrbuf[e],
929 pd->port_rcvegrbuf_phys[e]);
930
f37bda92 931 }
9929b0fb 932 kfree(pd->port_rcvegrbuf_phys);
7f510b46
BS
933 pd->port_rcvegrbuf_phys = NULL;
934bail_rcvegrbuf:
9929b0fb 935 kfree(pd->port_rcvegrbuf);
7f510b46
BS
936 pd->port_rcvegrbuf = NULL;
937bail:
938 return ret;
939}
940
f37bda92
BS
941
942/* common code for the mappings on dma_alloc_coherent mem */
943static int ipath_mmap_mem(struct vm_area_struct *vma,
1fd3b40f
BS
944 struct ipath_portdata *pd, unsigned len, int write_ok,
945 void *kvaddr, char *what)
f37bda92
BS
946{
947 struct ipath_devdata *dd = pd->port_dd;
1fd3b40f 948 unsigned long pfn;
f37bda92
BS
949 int ret;
950
951 if ((vma->vm_end - vma->vm_start) > len) {
952 dev_info(&dd->pcidev->dev,
953 "FAIL on %s: len %lx > %x\n", what,
954 vma->vm_end - vma->vm_start, len);
955 ret = -EFAULT;
956 goto bail;
957 }
958
959 if (!write_ok) {
960 if (vma->vm_flags & VM_WRITE) {
961 dev_info(&dd->pcidev->dev,
962 "%s must be mapped readonly\n", what);
963 ret = -EPERM;
964 goto bail;
965 }
966
967 /* don't allow them to later change with mprotect */
968 vma->vm_flags &= ~VM_MAYWRITE;
969 }
970
1fd3b40f 971 pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
f37bda92
BS
972 ret = remap_pfn_range(vma, vma->vm_start, pfn,
973 len, vma->vm_page_prot);
974 if (ret)
1fd3b40f
BS
975 dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
976 "bytes r%c failed: %d\n", what, pd->port_port,
977 pfn, len, write_ok?'w':'o', ret);
f37bda92 978 else
1fd3b40f
BS
979 ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
980 "r%c\n", what, pd->port_port, pfn, len,
981 write_ok?'w':'o');
f37bda92
BS
982bail:
983 return ret;
984}
985
7f510b46
BS
986static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
987 u64 ureg)
988{
989 unsigned long phys;
990 int ret;
991
f37bda92
BS
992 /*
993 * This is real hardware, so use io_remap. This is the mechanism
994 * for the user process to update the head registers for their port
995 * in the chip.
996 */
7f510b46
BS
997 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
998 dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
999 "%lx > PAGE\n", vma->vm_end - vma->vm_start);
1000 ret = -EFAULT;
1001 } else {
1002 phys = dd->ipath_physaddr + ureg;
1003 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1004
1005 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1006 ret = io_remap_pfn_range(vma, vma->vm_start,
1007 phys >> PAGE_SHIFT,
1008 vma->vm_end - vma->vm_start,
1009 vma->vm_page_prot);
1010 }
1011 return ret;
1012}
1013
1014static int mmap_piobufs(struct vm_area_struct *vma,
1015 struct ipath_devdata *dd,
9929b0fb
BS
1016 struct ipath_portdata *pd,
1017 unsigned piobufs, unsigned piocnt)
7f510b46
BS
1018{
1019 unsigned long phys;
1020 int ret;
1021
1022 /*
f37bda92
BS
1023 * When we map the PIO buffers in the chip, we want to map them as
1024 * writeonly, no read possible. This prevents access to previous
1025 * process data, and catches users who might try to read the i/o
1026 * space due to a bug.
7f510b46 1027 */
9929b0fb 1028 if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
7f510b46
BS
1029 dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
1030 "reqlen %lx > PAGE\n",
1031 vma->vm_end - vma->vm_start);
9929b0fb 1032 ret = -EINVAL;
7f510b46
BS
1033 goto bail;
1034 }
1035
9929b0fb 1036 phys = dd->ipath_physaddr + piobufs;
f37bda92 1037
7f510b46 1038 /*
f37bda92 1039 * Don't mark this as non-cached, or we don't get the
7f510b46 1040 * write combining behavior we want on the PIO buffers!
7f510b46
BS
1041 */
1042
eb9dc6f4
BS
1043#if defined(__powerpc__)
1044 /* There isn't a generic way to specify writethrough mappings */
1045 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1046 pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
1047 pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
1048#endif
1049
367fe711
BS
1050 /*
1051 * don't allow them to later change to readable with mprotect (for when
1052 * not initially mapped readable, as is normally the case)
1053 */
f37bda92 1054 vma->vm_flags &= ~VM_MAYREAD;
7f510b46
BS
1055 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1056
1057 ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
1058 vma->vm_end - vma->vm_start,
1059 vma->vm_page_prot);
1060bail:
1061 return ret;
1062}
1063
1064static int mmap_rcvegrbufs(struct vm_area_struct *vma,
1065 struct ipath_portdata *pd)
1066{
1067 struct ipath_devdata *dd = pd->port_dd;
1068 unsigned long start, size;
1069 size_t total_size, i;
1fd3b40f 1070 unsigned long pfn;
7f510b46
BS
1071 int ret;
1072
7f510b46
BS
1073 size = pd->port_rcvegrbuf_size;
1074 total_size = pd->port_rcvegrbuf_chunks * size;
1075 if ((vma->vm_end - vma->vm_start) > total_size) {
1076 dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
1077 "reqlen %lx > actual %lx\n",
1078 vma->vm_end - vma->vm_start,
1079 (unsigned long) total_size);
9929b0fb 1080 ret = -EINVAL;
7f510b46
BS
1081 goto bail;
1082 }
1083
1084 if (vma->vm_flags & VM_WRITE) {
1085 dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
1086 "writable (flags=%lx)\n", vma->vm_flags);
1087 ret = -EPERM;
1088 goto bail;
1089 }
f37bda92
BS
1090 /* don't allow them to later change to writeable with mprotect */
1091 vma->vm_flags &= ~VM_MAYWRITE;
7f510b46
BS
1092
1093 start = vma->vm_start;
7f510b46 1094
7f510b46 1095 for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
1fd3b40f
BS
1096 pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
1097 ret = remap_pfn_range(vma, start, pfn, size,
1098 vma->vm_page_prot);
7f510b46
BS
1099 if (ret < 0)
1100 goto bail;
1101 }
1102 ret = 0;
1103
1104bail:
1105 return ret;
1106}
1107
9929b0fb
BS
1108/*
1109 * ipath_file_vma_nopage - handle a VMA page fault.
1110 */
1111static struct page *ipath_file_vma_nopage(struct vm_area_struct *vma,
1112 unsigned long address, int *type)
1113{
1114 unsigned long offset = address - vma->vm_start;
1115 struct page *page = NOPAGE_SIGBUS;
1116 void *pageptr;
1117
1118 /*
1119 * Convert the vmalloc address into a struct page.
1120 */
1121 pageptr = (void *)(offset + (vma->vm_pgoff << PAGE_SHIFT));
1122 page = vmalloc_to_page(pageptr);
1123 if (!page)
1124 goto out;
1125
1126 /* Increment the reference count. */
1127 get_page(page);
1128 if (type)
1129 *type = VM_FAULT_MINOR;
1130out:
1131 return page;
1132}
1133
1134static struct vm_operations_struct ipath_file_vm_ops = {
1135 .nopage = ipath_file_vma_nopage,
1136};
1137
1138static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
1139 struct ipath_portdata *pd, unsigned subport)
1140{
1141 unsigned long len;
1142 struct ipath_devdata *dd;
1143 void *addr;
1144 size_t size;
0a5a83cf 1145 int ret = 0;
9929b0fb
BS
1146
1147 /* If the port is not shared, all addresses should be physical */
0a5a83cf 1148 if (!pd->port_subport_cnt)
9929b0fb 1149 goto bail;
9929b0fb
BS
1150
1151 dd = pd->port_dd;
1152 size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
1153
1154 /*
1155 * Master has all the slave uregbase, rcvhdrq, and
1156 * rcvegrbufs mmapped.
1157 */
1158 if (subport == 0) {
1159 unsigned num_slaves = pd->port_subport_cnt - 1;
1160
0a5a83cf 1161 if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
9929b0fb
BS
1162 addr = pd->subport_uregbase;
1163 size = PAGE_SIZE * num_slaves;
0a5a83cf 1164 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
9929b0fb
BS
1165 addr = pd->subport_rcvhdr_base;
1166 size = pd->port_rcvhdrq_size * num_slaves;
0a5a83cf 1167 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
9929b0fb
BS
1168 addr = pd->subport_rcvegrbuf;
1169 size *= num_slaves;
0a5a83cf 1170 } else
9929b0fb 1171 goto bail;
0a5a83cf
RC
1172 } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
1173 PAGE_SIZE * (subport - 1))) {
9929b0fb
BS
1174 addr = pd->subport_uregbase + PAGE_SIZE * (subport - 1);
1175 size = PAGE_SIZE;
0a5a83cf
RC
1176 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
1177 pd->port_rcvhdrq_size * (subport - 1))) {
9929b0fb
BS
1178 addr = pd->subport_rcvhdr_base +
1179 pd->port_rcvhdrq_size * (subport - 1);
1180 size = pd->port_rcvhdrq_size;
0a5a83cf
RC
1181 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
1182 size * (subport - 1))) {
9929b0fb
BS
1183 addr = pd->subport_rcvegrbuf + size * (subport - 1);
1184 /* rcvegrbufs are read-only on the slave */
1185 if (vma->vm_flags & VM_WRITE) {
1186 dev_info(&dd->pcidev->dev,
1187 "Can't map eager buffers as "
1188 "writable (flags=%lx)\n", vma->vm_flags);
1189 ret = -EPERM;
1190 goto bail;
1191 }
1192 /*
1193 * Don't allow permission to later change to writeable
1194 * with mprotect.
1195 */
1196 vma->vm_flags &= ~VM_MAYWRITE;
0a5a83cf 1197 } else
9929b0fb 1198 goto bail;
9929b0fb
BS
1199 len = vma->vm_end - vma->vm_start;
1200 if (len > size) {
1201 ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
1202 ret = -EINVAL;
1203 goto bail;
1204 }
1205
1206 vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
1207 vma->vm_ops = &ipath_file_vm_ops;
1208 vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
0a5a83cf 1209 ret = 1;
9929b0fb
BS
1210
1211bail:
1212 return ret;
1213}
1214
7f510b46
BS
1215/**
1216 * ipath_mmap - mmap various structures into user space
1217 * @fp: the file pointer
1218 * @vma: the VM area
1219 *
1220 * We use this to have a shared buffer between the kernel and the user code
1221 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1222 * buffers in the chip. We have the open and close entries so we can bump
1223 * the ref count and keep the driver from being unloaded while still mapped.
1224 */
1225static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
1226{
1227 struct ipath_portdata *pd;
1228 struct ipath_devdata *dd;
1229 u64 pgaddr, ureg;
9929b0fb 1230 unsigned piobufs, piocnt;
7f510b46
BS
1231 int ret;
1232
1233 pd = port_fp(fp);
9929b0fb
BS
1234 if (!pd) {
1235 ret = -EINVAL;
1236 goto bail;
1237 }
7f510b46 1238 dd = pd->port_dd;
f37bda92 1239
7f510b46
BS
1240 /*
1241 * This is the ipath_do_user_init() code, mapping the shared buffers
1242 * into the user process. The address referred to by vm_pgoff is the
9929b0fb
BS
1243 * file offset passed via mmap(). For shared ports, this is the
1244 * kernel vmalloc() address of the pages to share with the master.
1245 * For non-shared or master ports, this is a physical address.
1246 * We only do one mmap for each space mapped.
7f510b46
BS
1247 */
1248 pgaddr = vma->vm_pgoff << PAGE_SHIFT;
1249
1250 /*
9929b0fb
BS
1251 * Check for 0 in case one of the allocations failed, but user
1252 * called mmap anyway.
7f510b46 1253 */
9929b0fb
BS
1254 if (!pgaddr) {
1255 ret = -EINVAL;
1256 goto bail;
f37bda92 1257 }
7f510b46 1258
9929b0fb 1259 ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
7f510b46 1260 (unsigned long long) pgaddr, vma->vm_start,
9929b0fb
BS
1261 vma->vm_end - vma->vm_start, dd->ipath_unit,
1262 pd->port_port, subport_fp(fp));
7f510b46 1263
9929b0fb
BS
1264 /*
1265 * Physical addresses must fit in 40 bits for our hardware.
1266 * Check for kernel virtual addresses first, anything else must
1267 * match a HW or memory address.
1268 */
0a5a83cf
RC
1269 ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
1270 if (ret) {
1271 if (ret > 0)
1272 ret = 0;
9929b0fb
BS
1273 goto bail;
1274 }
1275
1276 if (!pd->port_subport_cnt) {
1277 /* port is not shared */
1278 ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
1279 piocnt = dd->ipath_pbufsport;
1280 piobufs = pd->port_piobufs;
1281 } else if (!subport_fp(fp)) {
1282 /* caller is the master */
1283 ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port;
1284 piocnt = (dd->ipath_pbufsport / pd->port_subport_cnt) +
1285 (dd->ipath_pbufsport % pd->port_subport_cnt);
1286 piobufs = pd->port_piobufs +
1287 dd->ipath_palign * (dd->ipath_pbufsport - piocnt);
1288 } else {
1289 unsigned slave = subport_fp(fp) - 1;
1290
1291 /* caller is a slave */
1292 ureg = 0;
1293 piocnt = dd->ipath_pbufsport / pd->port_subport_cnt;
1294 piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
f37bda92 1295 }
9929b0fb
BS
1296
1297 if (pgaddr == ureg)
7f510b46 1298 ret = mmap_ureg(vma, dd, ureg);
9929b0fb
BS
1299 else if (pgaddr == piobufs)
1300 ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
1301 else if (pgaddr == dd->ipath_pioavailregs_phys)
1302 /* in-memory copy of pioavail registers */
1303 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1fd3b40f 1304 (void *) dd->ipath_pioavailregs_dma,
9929b0fb
BS
1305 "pioavail registers");
1306 else if (subport_fp(fp))
1307 /* Subports don't mmap the physical receive buffers */
1308 ret = -EINVAL;
1309 else if (pgaddr == pd->port_rcvegr_phys)
7f510b46 1310 ret = mmap_rcvegrbufs(vma, pd);
9929b0fb 1311 else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
f37bda92 1312 /*
525d0ca1 1313 * The rcvhdrq itself; readonly except on HT (so have
f37bda92
BS
1314 * to allow writable mapping), multiple pages, contiguous
1315 * from an i/o perspective.
1316 */
9929b0fb 1317 ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
1fd3b40f 1318 pd->port_rcvhdrq,
f37bda92 1319 "rcvhdrq");
9929b0fb 1320 else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
f37bda92
BS
1321 /* in-memory copy of rcvhdrq tail register */
1322 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
1fd3b40f 1323 pd->port_rcvhdrtail_kvaddr,
f37bda92 1324 "rcvhdrq tail");
7f510b46
BS
1325 else
1326 ret = -EINVAL;
1327
1328 vma->vm_private_data = NULL;
1329
1330 if (ret < 0)
1331 dev_info(&dd->pcidev->dev,
9929b0fb
BS
1332 "Failure %d on off %llx len %lx\n",
1333 -ret, (unsigned long long)pgaddr,
1334 vma->vm_end - vma->vm_start);
1335bail:
7f510b46
BS
1336 return ret;
1337}
1338
1339static unsigned int ipath_poll(struct file *fp,
1340 struct poll_table_struct *pt)
1341{
1342 struct ipath_portdata *pd;
1343 u32 head, tail;
1344 int bit;
e35d710d 1345 unsigned pollflag = 0;
7f510b46
BS
1346 struct ipath_devdata *dd;
1347
1348 pd = port_fp(fp);
9929b0fb
BS
1349 if (!pd)
1350 goto bail;
7f510b46
BS
1351 dd = pd->port_dd;
1352
1353 bit = pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT;
1354 set_bit(bit, &dd->ipath_rcvctrl);
1355
1356 /*
1357 * Before blocking, make sure that head is still == tail,
1358 * reading from the chip, so we can be sure the interrupt
1359 * enable has made it to the chip. If not equal, disable
1360 * interrupt again and return immediately. This avoids races,
1361 * and the overhead of the chip read doesn't matter much at
1362 * this point, since we are waiting for something anyway.
1363 */
1364
1365 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1366 dd->ipath_rcvctrl);
1367
1368 head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
1369 tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
1370
1371 if (tail == head) {
1372 set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
9929b0fb 1373 if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
9dcc0e58
BS
1374 (void)ipath_write_ureg(dd, ur_rcvhdrhead,
1375 dd->ipath_rhdrhead_intr_off
1376 | head, pd->port_port);
7f510b46
BS
1377 poll_wait(fp, &pd->port_wait, pt);
1378
1379 if (test_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag)) {
1380 /* timed out, no packets received */
1381 clear_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
1382 pd->port_rcvwait_to++;
1383 }
e35d710d
BS
1384 else
1385 pollflag = POLLIN | POLLRDNORM;
7f510b46
BS
1386 }
1387 else {
1388 /* it's already happened; don't do wait_event overhead */
e35d710d 1389 pollflag = POLLIN | POLLRDNORM;
7f510b46
BS
1390 pd->port_rcvnowait++;
1391 }
1392
1393 clear_bit(bit, &dd->ipath_rcvctrl);
1394 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1395 dd->ipath_rcvctrl);
1396
9929b0fb 1397bail:
e35d710d 1398 return pollflag;
7f510b46
BS
1399}
1400
9929b0fb
BS
1401static int init_subports(struct ipath_devdata *dd,
1402 struct ipath_portdata *pd,
1403 const struct ipath_user_info *uinfo)
1404{
1405 int ret = 0;
1406 unsigned num_slaves;
1407 size_t size;
1408
1409 /* Old user binaries don't know about subports */
1410 if ((uinfo->spu_userversion & 0xffff) != IPATH_USER_SWMINOR)
1411 goto bail;
1412 /*
1413 * If the user is requesting zero or one port,
1414 * skip the subport allocation.
1415 */
1416 if (uinfo->spu_subport_cnt <= 1)
1417 goto bail;
0a5a83cf 1418 if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
9929b0fb
BS
1419 ret = -EINVAL;
1420 goto bail;
1421 }
1422
1423 num_slaves = uinfo->spu_subport_cnt - 1;
1424 pd->subport_uregbase = vmalloc(PAGE_SIZE * num_slaves);
1425 if (!pd->subport_uregbase) {
1426 ret = -ENOMEM;
1427 goto bail;
1428 }
1429 /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
1430 size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
1431 sizeof(u32), PAGE_SIZE) * num_slaves;
1432 pd->subport_rcvhdr_base = vmalloc(size);
1433 if (!pd->subport_rcvhdr_base) {
1434 ret = -ENOMEM;
1435 goto bail_ureg;
1436 }
1437
1438 pd->subport_rcvegrbuf = vmalloc(pd->port_rcvegrbuf_chunks *
1439 pd->port_rcvegrbuf_size *
1440 num_slaves);
1441 if (!pd->subport_rcvegrbuf) {
1442 ret = -ENOMEM;
1443 goto bail_rhdr;
1444 }
1445
1446 pd->port_subport_cnt = uinfo->spu_subport_cnt;
1447 pd->port_subport_id = uinfo->spu_subport_id;
1448 pd->active_slaves = 1;
947d7617 1449 set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
9929b0fb
BS
1450 goto bail;
1451
1452bail_rhdr:
1453 vfree(pd->subport_rcvhdr_base);
1454bail_ureg:
1455 vfree(pd->subport_uregbase);
1456 pd->subport_uregbase = NULL;
1457bail:
1458 return ret;
1459}
1460
7f510b46 1461static int try_alloc_port(struct ipath_devdata *dd, int port,
9929b0fb
BS
1462 struct file *fp,
1463 const struct ipath_user_info *uinfo)
7f510b46 1464{
9929b0fb 1465 struct ipath_portdata *pd;
7f510b46
BS
1466 int ret;
1467
9929b0fb
BS
1468 if (!(pd = dd->ipath_pd[port])) {
1469 void *ptmp;
7f510b46 1470
9929b0fb 1471 pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
7f510b46
BS
1472
1473 /*
1474 * Allocate memory for use in ipath_tid_update() just once
1475 * at open, not per call. Reduces cost of expected send
1476 * setup.
1477 */
1478 ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
1479 dd->ipath_rcvtidcnt * sizeof(struct page **),
1480 GFP_KERNEL);
9929b0fb 1481 if (!pd || !ptmp) {
7f510b46
BS
1482 ipath_dev_err(dd, "Unable to allocate portdata "
1483 "memory, failing open\n");
1484 ret = -ENOMEM;
9929b0fb 1485 kfree(pd);
7f510b46
BS
1486 kfree(ptmp);
1487 goto bail;
1488 }
9929b0fb 1489 dd->ipath_pd[port] = pd;
7f510b46
BS
1490 dd->ipath_pd[port]->port_port = port;
1491 dd->ipath_pd[port]->port_dd = dd;
1492 dd->ipath_pd[port]->port_tid_pg_list = ptmp;
1493 init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
1494 }
9929b0fb
BS
1495 if (!pd->port_cnt) {
1496 pd->userversion = uinfo->spu_userversion;
1497 init_user_egr_sizes(pd);
1498 if ((ret = init_subports(dd, pd, uinfo)) != 0)
1499 goto bail;
7f510b46
BS
1500 ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
1501 current->comm, current->pid, dd->ipath_unit,
1502 port);
9929b0fb
BS
1503 pd->port_cnt = 1;
1504 port_fp(fp) = pd;
1505 pd->port_pid = current->pid;
1506 strncpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
7f510b46
BS
1507 ipath_stats.sps_ports++;
1508 ret = 0;
9929b0fb
BS
1509 } else
1510 ret = -EBUSY;
7f510b46
BS
1511
1512bail:
1513 return ret;
1514}
1515
1516static inline int usable(struct ipath_devdata *dd)
1517{
1518 return dd &&
1519 (dd->ipath_flags & IPATH_PRESENT) &&
1520 dd->ipath_kregbase &&
1521 dd->ipath_lid &&
1522 !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
1523 | IPATH_LINKUNK));
1524}
1525
9929b0fb
BS
1526static int find_free_port(int unit, struct file *fp,
1527 const struct ipath_user_info *uinfo)
7f510b46
BS
1528{
1529 struct ipath_devdata *dd = ipath_lookup(unit);
1530 int ret, i;
1531
1532 if (!dd) {
1533 ret = -ENODEV;
1534 goto bail;
1535 }
1536
1537 if (!usable(dd)) {
1538 ret = -ENETDOWN;
1539 goto bail;
1540 }
1541
9929b0fb
BS
1542 for (i = 1; i < dd->ipath_cfgports; i++) {
1543 ret = try_alloc_port(dd, i, fp, uinfo);
7f510b46
BS
1544 if (ret != -EBUSY)
1545 goto bail;
1546 }
1547 ret = -EBUSY;
1548
1549bail:
1550 return ret;
1551}
1552
9929b0fb
BS
1553static int find_best_unit(struct file *fp,
1554 const struct ipath_user_info *uinfo)
7f510b46
BS
1555{
1556 int ret = 0, i, prefunit = -1, devmax;
1557 int maxofallports, npresent, nup;
1558 int ndev;
1559
9929b0fb 1560 devmax = ipath_count_units(&npresent, &nup, &maxofallports);
7f510b46
BS
1561
1562 /*
1563 * This code is present to allow a knowledgeable person to
1564 * specify the layout of processes to processors before opening
1565 * this driver, and then we'll assign the process to the "closest"
525d0ca1 1566 * InfiniPath chip to that processor (we assume reasonable connectivity,
7f510b46
BS
1567 * for now). This code assumes that if affinity has been set
1568 * before this point, that at most one cpu is set; for now this
1569 * is reasonable. I check for both cpus_empty() and cpus_full(),
1570 * in case some kernel variant sets none of the bits when no
1571 * affinity is set. 2.6.11 and 12 kernels have all present
1572 * cpus set. Some day we'll have to fix it up further to handle
525d0ca1 1573 * a cpu subset. This algorithm fails for two HT chips connected
7f510b46
BS
1574 * in tunnel fashion. Eventually this needs real topology
1575 * information. There may be some issues with dual core numbering
1576 * as well. This needs more work prior to release.
1577 */
1578 if (!cpus_empty(current->cpus_allowed) &&
1579 !cpus_full(current->cpus_allowed)) {
1580 int ncpus = num_online_cpus(), curcpu = -1;
1581 for (i = 0; i < ncpus; i++)
1582 if (cpu_isset(i, current->cpus_allowed)) {
1583 ipath_cdbg(PROC, "%s[%u] affinity set for "
1584 "cpu %d\n", current->comm,
1585 current->pid, i);
1586 curcpu = i;
1587 }
1588 if (curcpu != -1) {
1589 if (npresent) {
1590 prefunit = curcpu / (ncpus / npresent);
1591 ipath_dbg("%s[%u] %d chips, %d cpus, "
1592 "%d cpus/chip, select unit %d\n",
1593 current->comm, current->pid,
1594 npresent, ncpus, ncpus / npresent,
1595 prefunit);
1596 }
1597 }
1598 }
1599
1600 /*
1601 * user ports start at 1, kernel port is 0
1602 * For now, we do round-robin access across all chips
1603 */
1604
1605 if (prefunit != -1)
1606 devmax = prefunit + 1;
7f510b46
BS
1607recheck:
1608 for (i = 1; i < maxofallports; i++) {
1609 for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
1610 ndev++) {
1611 struct ipath_devdata *dd = ipath_lookup(ndev);
1612
1613 if (!usable(dd))
1614 continue; /* can't use this unit */
1615 if (i >= dd->ipath_cfgports)
1616 /*
1617 * Maxed out on users of this unit. Try
1618 * next.
1619 */
1620 continue;
9929b0fb 1621 ret = try_alloc_port(dd, i, fp, uinfo);
7f510b46
BS
1622 if (!ret)
1623 goto done;
1624 }
1625 }
1626
1627 if (npresent) {
1628 if (nup == 0) {
1629 ret = -ENETDOWN;
1630 ipath_dbg("No ports available (none initialized "
1631 "and ready)\n");
1632 } else {
1633 if (prefunit > 0) {
1634 /* if started above 0, retry from 0 */
1635 ipath_cdbg(PROC,
1636 "%s[%u] no ports on prefunit "
1637 "%d, clear and re-check\n",
1638 current->comm, current->pid,
1639 prefunit);
1640 devmax = ipath_count_units(NULL, NULL,
1641 NULL);
1642 prefunit = -1;
1643 goto recheck;
1644 }
1645 ret = -EBUSY;
1646 ipath_dbg("No ports available\n");
1647 }
1648 } else {
1649 ret = -ENXIO;
1650 ipath_dbg("No boards found\n");
1651 }
1652
1653done:
1654 return ret;
1655}
1656
9929b0fb
BS
1657static int find_shared_port(struct file *fp,
1658 const struct ipath_user_info *uinfo)
1659{
1660 int devmax, ndev, i;
1661 int ret = 0;
1662
1663 devmax = ipath_count_units(NULL, NULL, NULL);
1664
1665 for (ndev = 0; ndev < devmax; ndev++) {
1666 struct ipath_devdata *dd = ipath_lookup(ndev);
1667
1668 if (!dd)
1669 continue;
1670 for (i = 1; i < dd->ipath_cfgports; i++) {
1671 struct ipath_portdata *pd = dd->ipath_pd[i];
1672
1673 /* Skip ports which are not yet open */
1674 if (!pd || !pd->port_cnt)
1675 continue;
1676 /* Skip port if it doesn't match the requested one */
1677 if (pd->port_subport_id != uinfo->spu_subport_id)
1678 continue;
1679 /* Verify the sharing process matches the master */
1680 if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
1681 pd->userversion != uinfo->spu_userversion ||
1682 pd->port_cnt >= pd->port_subport_cnt) {
1683 ret = -EINVAL;
1684 goto done;
1685 }
1686 port_fp(fp) = pd;
1687 subport_fp(fp) = pd->port_cnt++;
1688 tidcursor_fp(fp) = 0;
1689 pd->active_slaves |= 1 << subport_fp(fp);
1690 ipath_cdbg(PROC,
1691 "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
1692 current->comm, current->pid,
1693 subport_fp(fp),
1694 pd->port_comm, pd->port_pid,
1695 dd->ipath_unit, pd->port_port);
1696 ret = 1;
1697 goto done;
1698 }
1699 }
1700
1701done:
1702 return ret;
1703}
1704
7f510b46
BS
1705static int ipath_open(struct inode *in, struct file *fp)
1706{
c97d27d8 1707 /* The real work is performed later in ipath_assign_port() */
9929b0fb
BS
1708 fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
1709 return fp->private_data ? 0 : -ENOMEM;
1710}
1711
c97d27d8
BS
1712
1713/* Get port early, so can set affinity prior to memory allocation */
1714static int ipath_assign_port(struct file *fp,
9929b0fb
BS
1715 const struct ipath_user_info *uinfo)
1716{
1717 int ret;
9929b0fb
BS
1718 int i_minor;
1719 unsigned swminor;
1720
1721 /* Check to be sure we haven't already initialized this file */
1722 if (port_fp(fp)) {
1723 ret = -EINVAL;
1724 goto done;
1725 }
1726
1727 /* for now, if major version is different, bail */
1728 if ((uinfo->spu_userversion >> 16) != IPATH_USER_SWMAJOR) {
1729 ipath_dbg("User major version %d not same as driver "
1730 "major %d\n", uinfo->spu_userversion >> 16,
1731 IPATH_USER_SWMAJOR);
1732 ret = -ENODEV;
1733 goto done;
1734 }
1735
1736 swminor = uinfo->spu_userversion & 0xffff;
1737 if (swminor != IPATH_USER_SWMINOR)
1738 ipath_dbg("User minor version %d not same as driver "
1739 "minor %d\n", swminor, IPATH_USER_SWMINOR);
7f510b46
BS
1740
1741 mutex_lock(&ipath_mutex);
1742
9929b0fb
BS
1743 if (swminor == IPATH_USER_SWMINOR && uinfo->spu_subport_cnt &&
1744 (ret = find_shared_port(fp, uinfo))) {
1745 mutex_unlock(&ipath_mutex);
1746 if (ret > 0)
1747 ret = 0;
1748 goto done;
1749 }
1750
1cfd6e64 1751 i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;
7f510b46 1752 ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
1cfd6e64 1753 (long)fp->f_path.dentry->d_inode->i_rdev, i_minor);
7f510b46 1754
9929b0fb
BS
1755 if (i_minor)
1756 ret = find_free_port(i_minor - 1, fp, uinfo);
7f510b46 1757 else
9929b0fb 1758 ret = find_best_unit(fp, uinfo);
7f510b46
BS
1759
1760 mutex_unlock(&ipath_mutex);
9929b0fb 1761
c97d27d8
BS
1762done:
1763 return ret;
1764}
1765
1766
1767static int ipath_do_user_init(struct file *fp,
1768 const struct ipath_user_info *uinfo)
1769{
1770 int ret;
947d7617 1771 struct ipath_portdata *pd = port_fp(fp);
c97d27d8
BS
1772 struct ipath_devdata *dd;
1773 u32 head32;
9929b0fb 1774
947d7617
RC
1775 /* Subports don't need to initialize anything since master did it. */
1776 if (subport_fp(fp)) {
1777 ret = wait_event_interruptible(pd->port_wait,
1778 !test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
1779 goto done;
1780 }
1781
9929b0fb
BS
1782 dd = pd->port_dd;
1783
1784 if (uinfo->spu_rcvhdrsize) {
1785 ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
1786 if (ret)
1787 goto done;
1788 }
1789
1790 /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
1791
1792 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
1793 pd->port_piobufs = dd->ipath_piobufbase +
1794 dd->ipath_pbufsport * (pd->port_port - 1) * dd->ipath_palign;
1795 ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n",
1796 pd->port_port, pd->port_piobufs);
1797
1798 /*
1799 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
1800 * array for time being. If pd->port_port > chip-supported,
1801 * we need to do extra stuff here to handle by handling overflow
1802 * through port 0, someday
1803 */
1804 ret = ipath_create_rcvhdrq(dd, pd);
1805 if (!ret)
1806 ret = ipath_create_user_egr(pd);
1807 if (ret)
1808 goto done;
1809
1810 /*
1811 * set the eager head register for this port to the current values
1812 * of the tail pointers, since we don't know if they were
1813 * updated on last use of the port.
1814 */
1815 head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
1816 ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
1817 dd->ipath_lastegrheads[pd->port_port] = -1;
1818 dd->ipath_lastrcvhdrqtails[pd->port_port] = -1;
1819 ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
1820 pd->port_port, head32);
1821 pd->port_tidcursor = 0; /* start at beginning after open */
1822 /*
1823 * now enable the port; the tail registers will be written to memory
1824 * by the chip as soon as it sees the write to
1825 * dd->ipath_kregs->kr_rcvctrl. The update only happens on
1826 * transition from 0 to 1, so clear it first, then set it as part of
1827 * enabling the port. This will (very briefly) affect any other
1828 * open ports, but it shouldn't be long enough to be an issue.
1829 * We explictly set the in-memory copy to 0 beforehand, so we don't
1830 * have to wait to be sure the DMA update has happened.
1831 */
1fd3b40f 1832 *(volatile u64 *)pd->port_rcvhdrtail_kvaddr = 0ULL;
9929b0fb
BS
1833 set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port,
1834 &dd->ipath_rcvctrl);
1835 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1836 dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD);
1837 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1838 dd->ipath_rcvctrl);
947d7617
RC
1839 /* Notify any waiting slaves */
1840 if (pd->port_subport_cnt) {
1841 clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1842 wake_up(&pd->port_wait);
1843 }
9929b0fb 1844done:
7f510b46
BS
1845 return ret;
1846}
1847
1848/**
1849 * unlock_exptid - unlock any expected TID entries port still had in use
1850 * @pd: port
1851 *
1852 * We don't actually update the chip here, because we do a bulk update
1853 * below, using ipath_f_clear_tids.
1854 */
1855static void unlock_expected_tids(struct ipath_portdata *pd)
1856{
1857 struct ipath_devdata *dd = pd->port_dd;
1858 int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
1859 int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
1860
1861 ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
1862 pd->port_port);
1863 for (i = port_tidbase; i < maxtid; i++) {
1864 if (!dd->ipath_pageshadow[i])
1865 continue;
1866
1fd3b40f
BS
1867 pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
1868 PAGE_SIZE, PCI_DMA_FROMDEVICE);
7f510b46
BS
1869 ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i],
1870 1);
1871 dd->ipath_pageshadow[i] = NULL;
1872 cnt++;
1873 ipath_stats.sps_pageunlocks++;
1874 }
1875 if (cnt)
1876 ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
1877 pd->port_port, cnt);
1878
1879 if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
1880 ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
1881 (unsigned long long) ipath_stats.sps_pagelocks,
1882 (unsigned long long)
1883 ipath_stats.sps_pageunlocks);
1884}
1885
1886static int ipath_close(struct inode *in, struct file *fp)
1887{
1888 int ret = 0;
9929b0fb 1889 struct ipath_filedata *fd;
7f510b46
BS
1890 struct ipath_portdata *pd;
1891 struct ipath_devdata *dd;
1892 unsigned port;
1893
1894 ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
1895 (long)in->i_rdev, fp->private_data);
1896
1897 mutex_lock(&ipath_mutex);
1898
9929b0fb 1899 fd = (struct ipath_filedata *) fp->private_data;
7f510b46 1900 fp->private_data = NULL;
9929b0fb
BS
1901 pd = fd->pd;
1902 if (!pd) {
1903 mutex_unlock(&ipath_mutex);
1904 goto bail;
1905 }
1906 if (--pd->port_cnt) {
1907 /*
1908 * XXX If the master closes the port before the slave(s),
1909 * revoke the mmap for the eager receive queue so
1910 * the slave(s) don't wait for receive data forever.
1911 */
1912 pd->active_slaves &= ~(1 << fd->subport);
1913 mutex_unlock(&ipath_mutex);
1914 goto bail;
1915 }
1916 port = pd->port_port;
7f510b46
BS
1917 dd = pd->port_dd;
1918
1919 if (pd->port_hdrqfull) {
1920 ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
1921 "during run\n", pd->port_comm, pd->port_pid,
1922 pd->port_hdrqfull);
1923 pd->port_hdrqfull = 0;
1924 }
1925
1926 if (pd->port_rcvwait_to || pd->port_piowait_to
1927 || pd->port_rcvnowait || pd->port_pionowait) {
1928 ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
1929 "%u rcv %u, pio already\n",
1930 pd->port_port, pd->port_rcvwait_to,
1931 pd->port_piowait_to, pd->port_rcvnowait,
1932 pd->port_pionowait);
1933 pd->port_rcvwait_to = pd->port_piowait_to =
1934 pd->port_rcvnowait = pd->port_pionowait = 0;
1935 }
1936 if (pd->port_flag) {
1937 ipath_dbg("port %u port_flag still set to 0x%lx\n",
1938 pd->port_port, pd->port_flag);
1939 pd->port_flag = 0;
1940 }
1941
1942 if (dd->ipath_kregbase) {
35783ec0
BS
1943 int i;
1944 /* atomically clear receive enable port. */
1945 clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port,
1946 &dd->ipath_rcvctrl);
1947 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
1948 dd->ipath_rcvctrl);
1949 /* and read back from chip to be sure that nothing
1950 * else is in flight when we do the rest */
1951 (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
7f510b46
BS
1952
1953 /* clean up the pkeys for this port user */
1954 ipath_clean_part_key(pd, dd);
35783ec0
BS
1955 /*
1956 * be paranoid, and never write 0's to these, just use an
1957 * unused part of the port 0 tail page. Of course,
1958 * rcvhdraddr points to a large chunk of memory, so this
1959 * could still trash things, but at least it won't trash
1960 * page 0, and by disabling the port, it should stop "soon",
1961 * even if a packet or two is in already in flight after we
1962 * disabled the port.
1963 */
1964 ipath_write_kreg_port(dd,
1965 dd->ipath_kregs->kr_rcvhdrtailaddr, port,
1966 dd->ipath_dummy_hdrq_phys);
1967 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
1968 pd->port_port, dd->ipath_dummy_hdrq_phys);
1969
1970 i = dd->ipath_pbufsport * (port - 1);
1971 ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport);
1972
1fd3b40f
BS
1973 dd->ipath_f_clear_tids(dd, pd->port_port);
1974
35783ec0
BS
1975 if (dd->ipath_pageshadow)
1976 unlock_expected_tids(pd);
1977 ipath_stats.sps_ports--;
1978 ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
1979 pd->port_comm, pd->port_pid,
1980 dd->ipath_unit, port);
7f510b46
BS
1981 }
1982
7f510b46 1983 pd->port_pid = 0;
f37bda92 1984 dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
7f510b46 1985 mutex_unlock(&ipath_mutex);
f37bda92 1986 ipath_free_pddata(dd, pd); /* after releasing the mutex */
7f510b46 1987
9929b0fb
BS
1988bail:
1989 kfree(fd);
7f510b46
BS
1990 return ret;
1991}
1992
9929b0fb 1993static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
7f510b46
BS
1994 struct ipath_port_info __user *uinfo)
1995{
1996 struct ipath_port_info info;
1997 int nup;
1998 int ret;
9929b0fb 1999 size_t sz;
7f510b46
BS
2000
2001 (void) ipath_count_units(NULL, &nup, NULL);
2002 info.num_active = nup;
2003 info.unit = pd->port_dd->ipath_unit;
2004 info.port = pd->port_port;
9929b0fb
BS
2005 info.subport = subport;
2006 /* Don't return new fields if old library opened the port. */
2007 if ((pd->userversion & 0xffff) == IPATH_USER_SWMINOR) {
2008 /* Number of user ports available for this device. */
2009 info.num_ports = pd->port_dd->ipath_cfgports - 1;
2010 info.num_subports = pd->port_subport_cnt;
2011 sz = sizeof(info);
2012 } else
2013 sz = sizeof(info) - 2 * sizeof(u16);
7f510b46 2014
9929b0fb 2015 if (copy_to_user(uinfo, &info, sz)) {
7f510b46
BS
2016 ret = -EFAULT;
2017 goto bail;
2018 }
2019 ret = 0;
2020
2021bail:
2022 return ret;
2023}
2024
9929b0fb
BS
2025static int ipath_get_slave_info(struct ipath_portdata *pd,
2026 void __user *slave_mask_addr)
2027{
2028 int ret = 0;
2029
2030 if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
2031 ret = -EFAULT;
2032 return ret;
2033}
2034
7f510b46
BS
2035static ssize_t ipath_write(struct file *fp, const char __user *data,
2036 size_t count, loff_t *off)
2037{
2038 const struct ipath_cmd __user *ucmd;
2039 struct ipath_portdata *pd;
2040 const void __user *src;
2041 size_t consumed, copy;
2042 struct ipath_cmd cmd;
2043 ssize_t ret = 0;
2044 void *dest;
2045
2046 if (count < sizeof(cmd.type)) {
2047 ret = -EINVAL;
2048 goto bail;
2049 }
2050
2051 ucmd = (const struct ipath_cmd __user *) data;
2052
2053 if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
2054 ret = -EFAULT;
2055 goto bail;
2056 }
2057
2058 consumed = sizeof(cmd.type);
2059
2060 switch (cmd.type) {
c97d27d8
BS
2061 case IPATH_CMD_ASSIGN_PORT:
2062 case __IPATH_CMD_USER_INIT:
7f510b46
BS
2063 case IPATH_CMD_USER_INIT:
2064 copy = sizeof(cmd.cmd.user_info);
2065 dest = &cmd.cmd.user_info;
2066 src = &ucmd->cmd.user_info;
2067 break;
2068 case IPATH_CMD_RECV_CTRL:
2069 copy = sizeof(cmd.cmd.recv_ctrl);
2070 dest = &cmd.cmd.recv_ctrl;
2071 src = &ucmd->cmd.recv_ctrl;
2072 break;
2073 case IPATH_CMD_PORT_INFO:
2074 copy = sizeof(cmd.cmd.port_info);
2075 dest = &cmd.cmd.port_info;
2076 src = &ucmd->cmd.port_info;
2077 break;
2078 case IPATH_CMD_TID_UPDATE:
2079 case IPATH_CMD_TID_FREE:
2080 copy = sizeof(cmd.cmd.tid_info);
2081 dest = &cmd.cmd.tid_info;
2082 src = &ucmd->cmd.tid_info;
2083 break;
2084 case IPATH_CMD_SET_PART_KEY:
2085 copy = sizeof(cmd.cmd.part_key);
2086 dest = &cmd.cmd.part_key;
2087 src = &ucmd->cmd.part_key;
2088 break;
9929b0fb
BS
2089 case IPATH_CMD_SLAVE_INFO:
2090 copy = sizeof(cmd.cmd.slave_mask_addr);
2091 dest = &cmd.cmd.slave_mask_addr;
2092 src = &ucmd->cmd.slave_mask_addr;
2093 break;
7f510b46
BS
2094 default:
2095 ret = -EINVAL;
2096 goto bail;
2097 }
2098
2099 if ((count - consumed) < copy) {
2100 ret = -EINVAL;
2101 goto bail;
2102 }
2103
2104 if (copy_from_user(dest, src, copy)) {
2105 ret = -EFAULT;
2106 goto bail;
2107 }
2108
2109 consumed += copy;
2110 pd = port_fp(fp);
c97d27d8
BS
2111 if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
2112 cmd.type != IPATH_CMD_ASSIGN_PORT) {
9929b0fb
BS
2113 ret = -EINVAL;
2114 goto bail;
2115 }
7f510b46
BS
2116
2117 switch (cmd.type) {
c97d27d8
BS
2118 case IPATH_CMD_ASSIGN_PORT:
2119 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2120 if (ret)
2121 goto bail;
2122 break;
2123 case __IPATH_CMD_USER_INIT:
2124 /* backwards compatibility, get port first */
2125 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2126 if (ret)
2127 goto bail;
2128 /* and fall through to current version. */
7f510b46 2129 case IPATH_CMD_USER_INIT:
9929b0fb
BS
2130 ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
2131 if (ret)
7f510b46
BS
2132 goto bail;
2133 ret = ipath_get_base_info(
9929b0fb 2134 fp, (void __user *) (unsigned long)
7f510b46
BS
2135 cmd.cmd.user_info.spu_base_info,
2136 cmd.cmd.user_info.spu_base_info_size);
2137 break;
2138 case IPATH_CMD_RECV_CTRL:
9929b0fb 2139 ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
7f510b46
BS
2140 break;
2141 case IPATH_CMD_PORT_INFO:
9929b0fb 2142 ret = ipath_port_info(pd, subport_fp(fp),
7f510b46
BS
2143 (struct ipath_port_info __user *)
2144 (unsigned long) cmd.cmd.port_info);
2145 break;
2146 case IPATH_CMD_TID_UPDATE:
9929b0fb 2147 ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
7f510b46
BS
2148 break;
2149 case IPATH_CMD_TID_FREE:
9929b0fb 2150 ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
7f510b46
BS
2151 break;
2152 case IPATH_CMD_SET_PART_KEY:
2153 ret = ipath_set_part_key(pd, cmd.cmd.part_key);
2154 break;
9929b0fb
BS
2155 case IPATH_CMD_SLAVE_INFO:
2156 ret = ipath_get_slave_info(pd,
2157 (void __user *) (unsigned long)
2158 cmd.cmd.slave_mask_addr);
2159 break;
7f510b46
BS
2160 }
2161
2162 if (ret >= 0)
2163 ret = consumed;
2164
2165bail:
2166 return ret;
2167}
2168
2169static struct class *ipath_class;
2170
2b8693c0 2171static int init_cdev(int minor, char *name, const struct file_operations *fops,
7f510b46
BS
2172 struct cdev **cdevp, struct class_device **class_devp)
2173{
2174 const dev_t dev = MKDEV(IPATH_MAJOR, minor);
2175 struct cdev *cdev = NULL;
2176 struct class_device *class_dev = NULL;
2177 int ret;
2178
2179 cdev = cdev_alloc();
2180 if (!cdev) {
2181 printk(KERN_ERR IPATH_DRV_NAME
2182 ": Could not allocate cdev for minor %d, %s\n",
2183 minor, name);
2184 ret = -ENOMEM;
2185 goto done;
2186 }
2187
2188 cdev->owner = THIS_MODULE;
2189 cdev->ops = fops;
2190 kobject_set_name(&cdev->kobj, name);
2191
2192 ret = cdev_add(cdev, dev, 1);
2193 if (ret < 0) {
2194 printk(KERN_ERR IPATH_DRV_NAME
2195 ": Could not add cdev for minor %d, %s (err %d)\n",
2196 minor, name, -ret);
2197 goto err_cdev;
2198 }
2199
2200 class_dev = class_device_create(ipath_class, NULL, dev, NULL, name);
2201
2202 if (IS_ERR(class_dev)) {
2203 ret = PTR_ERR(class_dev);
2204 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2205 "class_dev for minor %d, %s (err %d)\n",
2206 minor, name, -ret);
2207 goto err_cdev;
2208 }
2209
2210 goto done;
2211
2212err_cdev:
2213 cdev_del(cdev);
2214 cdev = NULL;
2215
2216done:
2217 if (ret >= 0) {
2218 *cdevp = cdev;
2219 *class_devp = class_dev;
2220 } else {
2221 *cdevp = NULL;
2222 *class_devp = NULL;
2223 }
2224
2225 return ret;
2226}
2227
2b8693c0 2228int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
7f510b46
BS
2229 struct cdev **cdevp, struct class_device **class_devp)
2230{
2231 return init_cdev(minor, name, fops, cdevp, class_devp);
2232}
2233
2234static void cleanup_cdev(struct cdev **cdevp,
2235 struct class_device **class_devp)
2236{
2237 struct class_device *class_dev = *class_devp;
2238
2239 if (class_dev) {
2240 class_device_unregister(class_dev);
2241 *class_devp = NULL;
2242 }
2243
2244 if (*cdevp) {
2245 cdev_del(*cdevp);
2246 *cdevp = NULL;
2247 }
2248}
2249
2250void ipath_cdev_cleanup(struct cdev **cdevp,
2251 struct class_device **class_devp)
2252{
2253 cleanup_cdev(cdevp, class_devp);
2254}
2255
2256static struct cdev *wildcard_cdev;
2257static struct class_device *wildcard_class_dev;
2258
2259static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
2260
2261static int user_init(void)
2262{
2263 int ret;
2264
2265 ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
2266 if (ret < 0) {
2267 printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
2268 "chrdev region (err %d)\n", -ret);
2269 goto done;
2270 }
2271
2272 ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
2273
2274 if (IS_ERR(ipath_class)) {
2275 ret = PTR_ERR(ipath_class);
2276 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2277 "device class (err %d)\n", -ret);
2278 goto bail;
2279 }
2280
2281 goto done;
2282bail:
2283 unregister_chrdev_region(dev, IPATH_NMINORS);
2284done:
2285 return ret;
2286}
2287
2288static void user_cleanup(void)
2289{
2290 if (ipath_class) {
2291 class_destroy(ipath_class);
2292 ipath_class = NULL;
2293 }
2294
2295 unregister_chrdev_region(dev, IPATH_NMINORS);
2296}
2297
2298static atomic_t user_count = ATOMIC_INIT(0);
2299static atomic_t user_setup = ATOMIC_INIT(0);
2300
2301int ipath_user_add(struct ipath_devdata *dd)
2302{
2303 char name[10];
2304 int ret;
2305
2306 if (atomic_inc_return(&user_count) == 1) {
2307 ret = user_init();
2308 if (ret < 0) {
2309 ipath_dev_err(dd, "Unable to set up user support: "
2310 "error %d\n", -ret);
2311 goto bail;
2312 }
7f510b46
BS
2313 ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
2314 &wildcard_class_dev);
2315 if (ret < 0) {
2316 ipath_dev_err(dd, "Could not create wildcard "
2317 "minor: error %d\n", -ret);
0fd41363 2318 goto bail_user;
7f510b46
BS
2319 }
2320
2321 atomic_set(&user_setup, 1);
2322 }
2323
2324 snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
2325
2326 ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
a2acb2ff 2327 &dd->user_cdev, &dd->user_class_dev);
7f510b46
BS
2328 if (ret < 0)
2329 ipath_dev_err(dd, "Could not create user minor %d, %s\n",
2330 dd->ipath_unit + 1, name);
2331
2332 goto bail;
2333
0fd41363 2334bail_user:
7f510b46
BS
2335 user_cleanup();
2336bail:
2337 return ret;
2338}
2339
a2acb2ff 2340void ipath_user_remove(struct ipath_devdata *dd)
7f510b46 2341{
a2acb2ff 2342 cleanup_cdev(&dd->user_cdev, &dd->user_class_dev);
7f510b46
BS
2343
2344 if (atomic_dec_return(&user_count) == 0) {
2345 if (atomic_read(&user_setup) == 0)
2346 goto bail;
2347
2348 cleanup_cdev(&wildcard_cdev, &wildcard_class_dev);
7f510b46
BS
2349 user_cleanup();
2350
2351 atomic_set(&user_setup, 0);
2352 }
2353bail:
2354 return;
2355}