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