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