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