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