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