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