blob: 3295177c937e3ef530cfa63c1affaf956db8160b [file] [log] [blame]
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001/*
Ralph Campbelle7eacd32008-04-16 21:09:32 -07002 * Copyright (c) 2006, 2007, 2008 QLogic Corporation. All rights reserved.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08003 * 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>
Dave Olson124b4dc2008-04-16 21:09:32 -070039#include <linux/highmem.h>
40#include <linux/io.h>
41#include <linux/jiffies.h>
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080042#include <asm/pgtable.h>
43
44#include "ipath_kernel.h"
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -070045#include "ipath_common.h"
Dave Olson124b4dc2008-04-16 21:09:32 -070046#include "ipath_user_sdma.h"
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080047
48static int ipath_open(struct inode *, struct file *);
49static int ipath_close(struct inode *, struct file *);
50static ssize_t ipath_write(struct file *, const char __user *, size_t,
51 loff_t *);
Dave Olson124b4dc2008-04-16 21:09:32 -070052static ssize_t ipath_writev(struct kiocb *, const struct iovec *,
53 unsigned long , loff_t);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080054static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
55static int ipath_mmap(struct file *, struct vm_area_struct *);
56
Arjan van de Ven2b8693c2007-02-12 00:55:32 -080057static const struct file_operations ipath_file_ops = {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080058 .owner = THIS_MODULE,
59 .write = ipath_write,
Dave Olson124b4dc2008-04-16 21:09:32 -070060 .aio_write = ipath_writev,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080061 .open = ipath_open,
62 .release = ipath_close,
63 .poll = ipath_poll,
64 .mmap = ipath_mmap
65};
66
Ralph Campbell0a5a83c2007-03-15 14:44:58 -070067/*
68 * Convert kernel virtual addresses to physical addresses so they don't
69 * potentially conflict with the chip addresses used as mmap offsets.
70 * It doesn't really matter what mmap offset we use as long as we can
71 * interpret it correctly.
72 */
73static u64 cvt_kvaddr(void *p)
74{
75 struct page *page;
76 u64 paddr = 0;
77
78 page = vmalloc_to_page(p);
79 if (page)
80 paddr = page_to_pfn(page) << PAGE_SHIFT;
81
82 return paddr;
83}
84
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -070085static int ipath_get_base_info(struct file *fp,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080086 void __user *ubase, size_t ubase_size)
87{
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -070088 struct ipath_portdata *pd = port_fp(fp);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080089 int ret = 0;
90 struct ipath_base_info *kinfo = NULL;
91 struct ipath_devdata *dd = pd->port_dd;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -070092 unsigned subport_cnt;
93 int shared, master;
94 size_t sz;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080095
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -070096 subport_cnt = pd->port_subport_cnt;
97 if (!subport_cnt) {
98 shared = 0;
99 master = 0;
100 subport_cnt = 1;
101 } else {
102 shared = 1;
103 master = !subport_fp(fp);
104 }
105
106 sz = sizeof(*kinfo);
107 /* If port sharing is not requested, allow the old size structure */
108 if (!shared)
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700109 sz -= 7 * sizeof(u64);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700110 if (ubase_size < sz) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800111 ipath_cdbg(PROC,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700112 "Base size %zu, need %zu (version mismatch?)\n",
113 ubase_size, sz);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800114 ret = -EINVAL;
115 goto bail;
116 }
117
118 kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
119 if (kinfo == NULL) {
120 ret = -ENOMEM;
121 goto bail;
122 }
123
124 ret = dd->ipath_f_get_base_info(pd, kinfo);
125 if (ret < 0)
126 goto bail;
127
128 kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
129 kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
130 kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
131 kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
132 /*
133 * have to mmap whole thing
134 */
135 kinfo->spi_rcv_egrbuftotlen =
136 pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
137 kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
138 kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
139 pd->port_rcvegrbuf_chunks;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700140 kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;
141 if (master)
142 kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800143 /*
144 * for this use, may be ipath_cfgports summed over all chips that
145 * are are configured and present
146 */
147 kinfo->spi_nports = dd->ipath_cfgports;
148 /* unit (chip/board) our port is on */
149 kinfo->spi_unit = dd->ipath_unit;
150 /* for now, only a single page */
151 kinfo->spi_tid_maxsize = PAGE_SIZE;
152
153 /*
154 * Doing this per port, and based on the skip value, etc. This has
155 * to be the actual buffer size, since the protocol code treats it
156 * as an array.
157 *
158 * These have to be set to user addresses in the user code via mmap.
159 * These values are used on return to user code for the mmap target
160 * addresses only. For 32 bit, same 44 bit address problem, so use
161 * the physical address, not virtual. Before 2.6.11, using the
162 * page_address() macro worked, but in 2.6.11, even that returns the
163 * full 64 bit address (upper bits all 1's). So far, using the
164 * physical addresses (or chip offsets, for chip mapping) works, but
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700165 * no doubt some future kernel release will change that, and we'll be
166 * on to yet another method of dealing with this.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800167 */
168 kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700169 kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800170 kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
171 kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
172 kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
173 (void *) dd->ipath_statusp -
174 (void *) dd->ipath_pioavailregs_dma;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700175 if (!shared) {
Dave Olsone2ab41c2008-05-07 11:00:15 -0700176 kinfo->spi_piocnt = pd->port_piocnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700177 kinfo->spi_piobufbase = (u64) pd->port_piobufs;
178 kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
Ralph Campbella18e26a2008-01-06 21:02:34 -0800179 dd->ipath_ureg_align * pd->port_port;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700180 } else if (master) {
Dave Olsone2ab41c2008-05-07 11:00:15 -0700181 kinfo->spi_piocnt = (pd->port_piocnt / subport_cnt) +
182 (pd->port_piocnt % subport_cnt);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700183 /* Master's PIO buffers are after all the slave's */
184 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
185 dd->ipath_palign *
Dave Olsone2ab41c2008-05-07 11:00:15 -0700186 (pd->port_piocnt - kinfo->spi_piocnt);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700187 } else {
188 unsigned slave = subport_fp(fp) - 1;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800189
Dave Olsone2ab41c2008-05-07 11:00:15 -0700190 kinfo->spi_piocnt = pd->port_piocnt / subport_cnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700191 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
192 dd->ipath_palign * kinfo->spi_piocnt * slave;
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700193 }
Dave Olson1d7c2e52008-04-16 21:09:30 -0700194
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700195 if (shared) {
196 kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase +
Ralph Campbella18e26a2008-01-06 21:02:34 -0800197 dd->ipath_ureg_align * pd->port_port;
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700198 kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs;
199 kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base;
200 kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr;
201
Ralph Campbell0a5a83c2007-03-15 14:44:58 -0700202 kinfo->__spi_uregbase = cvt_kvaddr(pd->subport_uregbase +
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700203 PAGE_SIZE * subport_fp(fp));
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700204
Ralph Campbell0a5a83c2007-03-15 14:44:58 -0700205 kinfo->spi_rcvhdr_base = cvt_kvaddr(pd->subport_rcvhdr_base +
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700206 pd->port_rcvhdrq_size * subport_fp(fp));
Ralph Campbell947d7612007-03-15 14:44:48 -0700207 kinfo->spi_rcvhdr_tailaddr = 0;
Ralph Campbell0a5a83c2007-03-15 14:44:58 -0700208 kinfo->spi_rcv_egrbufs = cvt_kvaddr(pd->subport_rcvegrbuf +
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700209 pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size *
210 subport_fp(fp));
211
212 kinfo->spi_subport_uregbase =
213 cvt_kvaddr(pd->subport_uregbase);
214 kinfo->spi_subport_rcvegrbuf =
215 cvt_kvaddr(pd->subport_rcvegrbuf);
216 kinfo->spi_subport_rcvhdr_base =
217 cvt_kvaddr(pd->subport_rcvhdr_base);
218 ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",
219 kinfo->spi_port, kinfo->spi_runtime_flags,
220 (unsigned long long) kinfo->spi_subport_uregbase,
221 (unsigned long long) kinfo->spi_subport_rcvegrbuf,
222 (unsigned long long) kinfo->spi_subport_rcvhdr_base);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700223 }
224
225 kinfo->spi_pioindex = (kinfo->spi_piobufbase - dd->ipath_piobufbase) /
226 dd->ipath_palign;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800227 kinfo->spi_pioalign = dd->ipath_palign;
228
229 kinfo->spi_qpair = IPATH_KD_QP;
Dave Olson826d8012008-04-16 21:01:12 -0700230 /*
231 * user mode PIO buffers are always 2KB, even when 4KB can
232 * be received, and sent via the kernel; this is ibmaxlen
233 * for 2K MTU.
234 */
235 kinfo->spi_piosize = dd->ipath_piosize2k - 2 * sizeof(u32);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800236 kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */
237 kinfo->spi_port = pd->port_port;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700238 kinfo->spi_subport = subport_fp(fp);
Bryan O'Sullivaneaf67332006-05-23 11:32:31 -0700239 kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800240 kinfo->spi_hw_version = dd->ipath_revision;
241
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700242 if (master) {
243 kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700244 }
245
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700246 sz = (ubase_size < sizeof(*kinfo)) ? ubase_size : sizeof(*kinfo);
247 if (copy_to_user(ubase, kinfo, sz))
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800248 ret = -EFAULT;
249
250bail:
251 kfree(kinfo);
252 return ret;
253}
254
255/**
256 * ipath_tid_update - update a port TID
257 * @pd: the port
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700258 * @fp: the ipath device file
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800259 * @ti: the TID information
260 *
261 * The new implementation as of Oct 2004 is that the driver assigns
262 * the tid and returns it to the caller. To make it easier to
263 * catch bugs, and to reduce search time, we keep a cursor for
264 * each port, walking the shadow tid array to find one that's not
265 * in use.
266 *
267 * For now, if we can't allocate the full list, we fail, although
268 * in the long run, we'll allocate as many as we can, and the
269 * caller will deal with that by trying the remaining pages later.
270 * That means that when we fail, we have to mark the tids as not in
271 * use again, in our shadow copy.
272 *
273 * It's up to the caller to free the tids when they are done.
274 * We'll unlock the pages as they free them.
275 *
276 * Also, right now we are locking one page at a time, but since
277 * the intended use of this routine is for a single group of
278 * virtually contiguous pages, that should change to improve
279 * performance.
280 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700281static int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800282 const struct ipath_tid_info *ti)
283{
284 int ret = 0, ntids;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700285 u32 tid, porttid, cnt, i, tidcnt, tidoff;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800286 u16 *tidlist;
287 struct ipath_devdata *dd = pd->port_dd;
288 u64 physaddr;
289 unsigned long vaddr;
290 u64 __iomem *tidbase;
291 unsigned long tidmap[8];
292 struct page **pagep = NULL;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700293 unsigned subport = subport_fp(fp);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800294
295 if (!dd->ipath_pageshadow) {
296 ret = -ENOMEM;
297 goto done;
298 }
299
300 cnt = ti->tidcnt;
301 if (!cnt) {
302 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
303 (unsigned long long) ti->tidlist);
304 /*
305 * Should we treat as success? likely a bug
306 */
307 ret = -EFAULT;
308 goto done;
309 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700310 porttid = pd->port_port * dd->ipath_rcvtidcnt;
311 if (!pd->port_subport_cnt) {
312 tidcnt = dd->ipath_rcvtidcnt;
313 tid = pd->port_tidcursor;
314 tidoff = 0;
315 } else if (!subport) {
316 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
317 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
318 tidoff = dd->ipath_rcvtidcnt - tidcnt;
319 porttid += tidoff;
320 tid = tidcursor_fp(fp);
321 } else {
322 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
323 tidoff = tidcnt * (subport - 1);
324 porttid += tidoff;
325 tid = tidcursor_fp(fp);
326 }
327 if (cnt > tidcnt) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800328 /* make sure it all fits in port_tid_pg_list */
329 dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
330 "TIDs, only trying max (%u)\n", cnt, tidcnt);
331 cnt = tidcnt;
332 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700333 pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];
334 tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800335
336 memset(tidmap, 0, sizeof(tidmap));
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800337 /* before decrement; chip actual # */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800338 ntids = tidcnt;
339 tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
340 dd->ipath_rcvtidbase +
341 porttid * sizeof(*tidbase));
342
343 ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
344 pd->port_port, cnt, tid, tidbase);
345
346 /* virtual address of first page in transfer */
347 vaddr = ti->tidvaddr;
348 if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
349 cnt * PAGE_SIZE)) {
350 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
351 (void *)vaddr, cnt);
352 ret = -EFAULT;
353 goto done;
354 }
355 ret = ipath_get_user_pages(vaddr, cnt, pagep);
356 if (ret) {
357 if (ret == -EBUSY) {
358 ipath_dbg("Failed to lock addr %p, %u pages "
359 "(already locked)\n",
360 (void *) vaddr, cnt);
361 /*
362 * for now, continue, and see what happens but with
363 * the new implementation, this should never happen,
364 * unless perhaps the user has mpin'ed the pages
365 * themselves (something we need to test)
366 */
367 ret = 0;
368 } else {
369 dev_info(&dd->pcidev->dev,
370 "Failed to lock addr %p, %u pages: "
371 "errno %d\n", (void *) vaddr, cnt, -ret);
372 goto done;
373 }
374 }
375 for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
376 for (; ntids--; tid++) {
377 if (tid == tidcnt)
378 tid = 0;
379 if (!dd->ipath_pageshadow[porttid + tid])
380 break;
381 }
382 if (ntids < 0) {
383 /*
384 * oops, wrapped all the way through their TIDs,
385 * and didn't have enough free; see comments at
386 * start of routine
387 */
388 ipath_dbg("Not enough free TIDs for %u pages "
389 "(index %d), failing\n", cnt, i);
390 i--; /* last tidlist[i] not filled in */
391 ret = -ENOMEM;
392 break;
393 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700394 tidlist[i] = tid + tidoff;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800395 ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700396 "vaddr %lx\n", i, tid + tidoff, vaddr);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800397 /* we "know" system pages and TID pages are same size */
398 dd->ipath_pageshadow[porttid + tid] = pagep[i];
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700399 dd->ipath_physshadow[porttid + tid] = ipath_map_page(
400 dd->pcidev, pagep[i], 0, PAGE_SIZE,
401 PCI_DMA_FROMDEVICE);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800402 /*
403 * don't need atomic or it's overhead
404 */
405 __set_bit(tid, tidmap);
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700406 physaddr = dd->ipath_physshadow[porttid + tid];
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800407 ipath_stats.sps_pagelocks++;
408 ipath_cdbg(VERBOSE,
409 "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
410 tid, vaddr, (unsigned long long) physaddr,
411 pagep[i]);
Joan Eslingerf716cdf2007-06-18 14:24:39 -0700412 dd->ipath_f_put_tid(dd, &tidbase[tid], RCVHQ_RCV_TYPE_EXPECTED,
413 physaddr);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800414 /*
415 * don't check this tid in ipath_portshadow, since we
416 * just filled it in; start with the next one.
417 */
418 tid++;
419 }
420
421 if (ret) {
422 u32 limit;
423 cleanup:
424 /* jump here if copy out of updated info failed... */
425 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
426 -ret, i, cnt);
427 /* same code that's in ipath_free_tid() */
428 limit = sizeof(tidmap) * BITS_PER_BYTE;
429 if (limit > tidcnt)
430 /* just in case size changes in future */
431 limit = tidcnt;
432 tid = find_first_bit((const unsigned long *)tidmap, limit);
433 for (; tid < limit; tid++) {
434 if (!test_bit(tid, tidmap))
435 continue;
436 if (dd->ipath_pageshadow[porttid + tid]) {
437 ipath_cdbg(VERBOSE, "Freeing TID %u\n",
438 tid);
Joan Eslingerf716cdf2007-06-18 14:24:39 -0700439 dd->ipath_f_put_tid(dd, &tidbase[tid],
440 RCVHQ_RCV_TYPE_EXPECTED,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800441 dd->ipath_tidinvalid);
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700442 pci_unmap_page(dd->pcidev,
443 dd->ipath_physshadow[porttid + tid],
444 PAGE_SIZE, PCI_DMA_FROMDEVICE);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800445 dd->ipath_pageshadow[porttid + tid] = NULL;
446 ipath_stats.sps_pageunlocks++;
447 }
448 }
449 ipath_release_user_pages(pagep, cnt);
450 } else {
451 /*
452 * Copy the updated array, with ipath_tid's filled in, back
453 * to user. Since we did the copy in already, this "should
454 * never fail" If it does, we have to clean up...
455 */
456 if (copy_to_user((void __user *)
457 (unsigned long) ti->tidlist,
458 tidlist, cnt * sizeof(*tidlist))) {
459 ret = -EFAULT;
460 goto cleanup;
461 }
462 if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
463 tidmap, sizeof tidmap)) {
464 ret = -EFAULT;
465 goto cleanup;
466 }
467 if (tid == tidcnt)
468 tid = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700469 if (!pd->port_subport_cnt)
470 pd->port_tidcursor = tid;
471 else
472 tidcursor_fp(fp) = tid;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800473 }
474
475done:
476 if (ret)
477 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
478 ti->tidcnt, -ret);
479 return ret;
480}
481
482/**
483 * ipath_tid_free - free a port TID
484 * @pd: the port
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700485 * @subport: the subport
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800486 * @ti: the TID info
487 *
488 * right now we are unlocking one page at a time, but since
489 * the intended use of this routine is for a single group of
490 * virtually contiguous pages, that should change to improve
491 * performance. We check that the TID is in range for this port
492 * but otherwise don't check validity; if user has an error and
493 * frees the wrong tid, it's only their own data that can thereby
494 * be corrupted. We do check that the TID was in use, for sanity
495 * We always use our idea of the saved address, not the address that
496 * they pass in to us.
497 */
498
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700499static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800500 const struct ipath_tid_info *ti)
501{
502 int ret = 0;
503 u32 tid, porttid, cnt, limit, tidcnt;
504 struct ipath_devdata *dd = pd->port_dd;
505 u64 __iomem *tidbase;
506 unsigned long tidmap[8];
507
508 if (!dd->ipath_pageshadow) {
509 ret = -ENOMEM;
510 goto done;
511 }
512
513 if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
514 sizeof tidmap)) {
515 ret = -EFAULT;
516 goto done;
517 }
518
519 porttid = pd->port_port * dd->ipath_rcvtidcnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700520 if (!pd->port_subport_cnt)
521 tidcnt = dd->ipath_rcvtidcnt;
522 else if (!subport) {
523 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
524 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
525 porttid += dd->ipath_rcvtidcnt - tidcnt;
526 } else {
527 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
528 porttid += tidcnt * (subport - 1);
529 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800530 tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
531 dd->ipath_rcvtidbase +
532 porttid * sizeof(*tidbase));
533
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800534 limit = sizeof(tidmap) * BITS_PER_BYTE;
535 if (limit > tidcnt)
536 /* just in case size changes in future */
537 limit = tidcnt;
538 tid = find_first_bit(tidmap, limit);
539 ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
540 "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
541 limit, tid, porttid);
542 for (cnt = 0; tid < limit; tid++) {
543 /*
544 * small optimization; if we detect a run of 3 or so without
545 * any set, use find_first_bit again. That's mainly to
546 * accelerate the case where we wrapped, so we have some at
547 * the beginning, and some at the end, and a big gap
548 * in the middle.
549 */
550 if (!test_bit(tid, tidmap))
551 continue;
552 cnt++;
553 if (dd->ipath_pageshadow[porttid + tid]) {
Dave Olson3ac8c702007-10-03 12:47:00 -0700554 struct page *p;
555 p = dd->ipath_pageshadow[porttid + tid];
556 dd->ipath_pageshadow[porttid + tid] = NULL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800557 ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
558 pd->port_pid, tid);
Joan Eslingerf716cdf2007-06-18 14:24:39 -0700559 dd->ipath_f_put_tid(dd, &tidbase[tid],
560 RCVHQ_RCV_TYPE_EXPECTED,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800561 dd->ipath_tidinvalid);
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700562 pci_unmap_page(dd->pcidev,
563 dd->ipath_physshadow[porttid + tid],
564 PAGE_SIZE, PCI_DMA_FROMDEVICE);
Dave Olson3ac8c702007-10-03 12:47:00 -0700565 ipath_release_user_pages(&p, 1);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800566 ipath_stats.sps_pageunlocks++;
567 } else
568 ipath_dbg("Unused tid %u, ignoring\n", tid);
569 }
570 if (cnt != ti->tidcnt)
571 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
572 ti->tidcnt, cnt);
573done:
574 if (ret)
575 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
576 ti->tidcnt, -ret);
577 return ret;
578}
579
580/**
581 * ipath_set_part_key - set a partition key
582 * @pd: the port
583 * @key: the key
584 *
585 * We can have up to 4 active at a time (other than the default, which is
586 * always allowed). This is somewhat tricky, since multiple ports may set
587 * the same key, so we reference count them, and clean up at exit. All 4
588 * partition keys are packed into a single infinipath register. It's an
589 * error for a process to set the same pkey multiple times. We provide no
590 * mechanism to de-allocate a pkey at this time, we may eventually need to
591 * do that. I've used the atomic operations, and no locking, and only make
592 * a single pass through what's available. This should be more than
593 * adequate for some time. I'll think about spinlocks or the like if and as
594 * it's necessary.
595 */
596static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
597{
598 struct ipath_devdata *dd = pd->port_dd;
599 int i, any = 0, pidx = -1;
600 u16 lkey = key & 0x7FFF;
601 int ret;
602
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700603 if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800604 /* nothing to do; this key always valid */
605 ret = 0;
606 goto bail;
607 }
608
609 ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
610 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
611 pd->port_port, key, dd->ipath_pkeys[0],
612 atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
613 atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
614 atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
615 atomic_read(&dd->ipath_pkeyrefs[3]));
616
617 if (!lkey) {
618 ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
619 pd->port_port);
620 ret = -EINVAL;
621 goto bail;
622 }
623
624 /*
625 * Set the full membership bit, because it has to be
626 * set in the register or the packet, and it seems
627 * cleaner to set in the register than to force all
628 * callers to set it. (see bug 4331)
629 */
630 key |= 0x8000;
631
632 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
633 if (!pd->port_pkeys[i] && pidx == -1)
634 pidx = i;
635 if (pd->port_pkeys[i] == key) {
636 ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
637 "(%x) more than once\n",
638 pd->port_port, key);
639 ret = -EEXIST;
640 goto bail;
641 }
642 }
643 if (pidx == -1) {
644 ipath_dbg("All pkeys for port %u already in use, "
645 "can't set %x\n", pd->port_port, key);
646 ret = -EBUSY;
647 goto bail;
648 }
649 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
650 if (!dd->ipath_pkeys[i]) {
651 any++;
652 continue;
653 }
654 if (dd->ipath_pkeys[i] == key) {
655 atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
656
657 if (atomic_inc_return(pkrefs) > 1) {
658 pd->port_pkeys[pidx] = key;
659 ipath_cdbg(VERBOSE, "p%u set key %x "
660 "matches #%d, count now %d\n",
661 pd->port_port, key, i,
662 atomic_read(pkrefs));
663 ret = 0;
664 goto bail;
665 } else {
666 /*
667 * lost race, decrement count, catch below
668 */
669 atomic_dec(pkrefs);
670 ipath_cdbg(VERBOSE, "Lost race, count was "
671 "0, after dec, it's %d\n",
672 atomic_read(pkrefs));
673 any++;
674 }
675 }
676 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
677 /*
678 * It makes no sense to have both the limited and
679 * full membership PKEY set at the same time since
680 * the unlimited one will disable the limited one.
681 */
682 ret = -EEXIST;
683 goto bail;
684 }
685 }
686 if (!any) {
687 ipath_dbg("port %u, all pkeys already in use, "
688 "can't set %x\n", pd->port_port, key);
689 ret = -EBUSY;
690 goto bail;
691 }
692 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
693 if (!dd->ipath_pkeys[i] &&
694 atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
695 u64 pkey;
696
697 /* for ipathstats, etc. */
698 ipath_stats.sps_pkeys[i] = lkey;
699 pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
700 pkey =
701 (u64) dd->ipath_pkeys[0] |
702 ((u64) dd->ipath_pkeys[1] << 16) |
703 ((u64) dd->ipath_pkeys[2] << 32) |
704 ((u64) dd->ipath_pkeys[3] << 48);
705 ipath_cdbg(PROC, "p%u set key %x in #%d, "
706 "portidx %d, new pkey reg %llx\n",
707 pd->port_port, key, i, pidx,
708 (unsigned long long) pkey);
709 ipath_write_kreg(
710 dd, dd->ipath_kregs->kr_partitionkey, pkey);
711
712 ret = 0;
713 goto bail;
714 }
715 }
716 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
717 "can't set %x\n", pd->port_port, key);
718 ret = -EBUSY;
719
720bail:
721 return ret;
722}
723
724/**
725 * ipath_manage_rcvq - manage a port's receive queue
726 * @pd: the port
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700727 * @subport: the subport
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800728 * @start_stop: action to carry out
729 *
730 * start_stop == 0 disables receive on the port, for use in queue
731 * overflow conditions. start_stop==1 re-enables, to be used to
732 * re-init the software copy of the head register
733 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700734static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
735 int start_stop)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800736{
737 struct ipath_devdata *dd = pd->port_dd;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800738
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700739 ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800740 start_stop ? "en" : "dis", dd->ipath_unit,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700741 pd->port_port, subport);
742 if (subport)
743 goto bail;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800744 /* atomically clear receive enable port. */
745 if (start_stop) {
746 /*
747 * On enable, force in-memory copy of the tail register to
748 * 0, so that protocol code doesn't have to worry about
749 * whether or not the chip has yet updated the in-memory
750 * copy or not on return from the system call. The chip
751 * always resets it's tail register back to 0 on a
752 * transition from disabled to enabled. This could cause a
753 * problem if software was broken, and did the enable w/o
754 * the disable, but eventually the in-memory copy will be
755 * updated and correct itself, even in the face of software
756 * bugs.
757 */
Ralph Campbellc59a80a2007-12-20 02:43:23 -0800758 if (pd->port_rcvhdrtail_kvaddr)
759 ipath_clear_rcvhdrtail(pd);
Dave Olsond8274862007-12-21 01:50:59 -0800760 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800761 &dd->ipath_rcvctrl);
762 } else
Dave Olsond8274862007-12-21 01:50:59 -0800763 clear_bit(dd->ipath_r_portenable_shift + pd->port_port,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800764 &dd->ipath_rcvctrl);
765 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
766 dd->ipath_rcvctrl);
767 /* now be sure chip saw it before we return */
Roland Dreier44f8e3f2006-12-12 11:50:20 -0800768 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800769 if (start_stop) {
770 /*
771 * And try to be sure that tail reg update has happened too.
772 * This should in theory interlock with the RXE changes to
773 * the tail register. Don't assign it to the tail register
774 * in memory copy, since we could overwrite an update by the
775 * chip if we did.
776 */
Roland Dreier44f8e3f2006-12-12 11:50:20 -0800777 ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800778 }
779 /* always; new head should be equal to new tail; see above */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700780bail:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800781 return 0;
782}
783
784static void ipath_clean_part_key(struct ipath_portdata *pd,
785 struct ipath_devdata *dd)
786{
787 int i, j, pchanged = 0;
788 u64 oldpkey;
789
790 /* for debugging only */
791 oldpkey = (u64) dd->ipath_pkeys[0] |
792 ((u64) dd->ipath_pkeys[1] << 16) |
793 ((u64) dd->ipath_pkeys[2] << 32) |
794 ((u64) dd->ipath_pkeys[3] << 48);
795
796 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
797 if (!pd->port_pkeys[i])
798 continue;
799 ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
800 pd->port_pkeys[i]);
801 for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
802 /* check for match independent of the global bit */
803 if ((dd->ipath_pkeys[j] & 0x7fff) !=
804 (pd->port_pkeys[i] & 0x7fff))
805 continue;
806 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
807 ipath_cdbg(VERBOSE, "p%u clear key "
808 "%x matches #%d\n",
809 pd->port_port,
810 pd->port_pkeys[i], j);
811 ipath_stats.sps_pkeys[j] =
812 dd->ipath_pkeys[j] = 0;
813 pchanged++;
814 }
815 else ipath_cdbg(
816 VERBOSE, "p%u key %x matches #%d, "
817 "but ref still %d\n", pd->port_port,
818 pd->port_pkeys[i], j,
819 atomic_read(&dd->ipath_pkeyrefs[j]));
820 break;
821 }
822 pd->port_pkeys[i] = 0;
823 }
824 if (pchanged) {
825 u64 pkey = (u64) dd->ipath_pkeys[0] |
826 ((u64) dd->ipath_pkeys[1] << 16) |
827 ((u64) dd->ipath_pkeys[2] << 32) |
828 ((u64) dd->ipath_pkeys[3] << 48);
829 ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
830 "new pkey reg %llx\n", pd->port_port,
831 (unsigned long long) oldpkey,
832 (unsigned long long) pkey);
833 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
834 pkey);
835 }
836}
837
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700838/*
839 * Initialize the port data with the receive buffer sizes
840 * so this can be done while the master port is locked.
841 * Otherwise, there is a race with a slave opening the port
842 * and seeing these fields uninitialized.
843 */
844static void init_user_egr_sizes(struct ipath_portdata *pd)
845{
846 struct ipath_devdata *dd = pd->port_dd;
847 unsigned egrperchunk, egrcnt, size;
848
849 /*
850 * to avoid wasting a lot of memory, we allocate 32KB chunks of
851 * physically contiguous memory, advance through it until used up
852 * and then allocate more. Of course, we need memory to store those
853 * extra pointers, now. Started out with 256KB, but under heavy
854 * memory pressure (creating large files and then copying them over
855 * NFS while doing lots of MPI jobs), we hit some allocation
856 * failures, even though we can sleep... (2.6.10) Still get
857 * failures at 64K. 32K is the lowest we can go without wasting
858 * additional memory.
859 */
860 size = 0x8000;
861 egrperchunk = size / dd->ipath_rcvegrbufsize;
862 egrcnt = dd->ipath_rcvegrcnt;
863 pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
864 pd->port_rcvegrbufs_perchunk = egrperchunk;
865 pd->port_rcvegrbuf_size = size;
866}
867
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800868/**
869 * ipath_create_user_egr - allocate eager TID buffers
870 * @pd: the port to allocate TID buffers for
871 *
872 * This routine is now quite different for user and kernel, because
873 * the kernel uses skb's, for the accelerated network performance
874 * This is the user port version
875 *
876 * Allocate the eager TID buffers and program them into infinipath
877 * They are no longer completely contiguous, we do multiple allocation
878 * calls.
879 */
880static int ipath_create_user_egr(struct ipath_portdata *pd)
881{
882 struct ipath_devdata *dd = pd->port_dd;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700883 unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800884 size_t size;
885 int ret;
Bryan O'Sullivan0ed9a4a2006-07-01 04:36:01 -0700886 gfp_t gfp_flags;
887
888 /*
889 * GFP_USER, but without GFP_FS, so buffer cache can be
890 * coalesced (we hope); otherwise, even at order 4,
891 * heavy filesystem activity makes these fail, and we can
892 * use compound pages.
893 */
894 gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800895
896 egrcnt = dd->ipath_rcvegrcnt;
897 /* TID number offset for this port */
Ralph Campbell60948a42008-01-06 21:02:34 -0800898 egroff = (pd->port_port - 1) * egrcnt + dd->ipath_p0_rcvegrcnt;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800899 egrsize = dd->ipath_rcvegrbufsize;
900 ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
901 "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
902
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700903 chunk = pd->port_rcvegrbuf_chunks;
904 egrperchunk = pd->port_rcvegrbufs_perchunk;
905 size = pd->port_rcvegrbuf_size;
906 pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
907 GFP_KERNEL);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800908 if (!pd->port_rcvegrbuf) {
909 ret = -ENOMEM;
910 goto bail;
911 }
912 pd->port_rcvegrbuf_phys =
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700913 kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
914 GFP_KERNEL);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800915 if (!pd->port_rcvegrbuf_phys) {
916 ret = -ENOMEM;
917 goto bail_rcvegrbuf;
918 }
919 for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800920
921 pd->port_rcvegrbuf[e] = dma_alloc_coherent(
922 &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
923 gfp_flags);
924
925 if (!pd->port_rcvegrbuf[e]) {
926 ret = -ENOMEM;
927 goto bail_rcvegrbuf_phys;
928 }
929 }
930
931 pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
932
933 for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
934 dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
935 unsigned i;
936
937 for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
938 dd->ipath_f_put_tid(dd, e + egroff +
939 (u64 __iomem *)
940 ((char __iomem *)
941 dd->ipath_kregbase +
Joan Eslingerf716cdf2007-06-18 14:24:39 -0700942 dd->ipath_rcvegrbase),
943 RCVHQ_RCV_TYPE_EAGER, pa);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800944 pa += egrsize;
945 }
946 cond_resched(); /* don't hog the cpu */
947 }
948
949 ret = 0;
950 goto bail;
951
952bail_rcvegrbuf_phys:
953 for (e = 0; e < pd->port_rcvegrbuf_chunks &&
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700954 pd->port_rcvegrbuf[e]; e++) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800955 dma_free_coherent(&dd->pcidev->dev, size,
956 pd->port_rcvegrbuf[e],
957 pd->port_rcvegrbuf_phys[e]);
958
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700959 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700960 kfree(pd->port_rcvegrbuf_phys);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800961 pd->port_rcvegrbuf_phys = NULL;
962bail_rcvegrbuf:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700963 kfree(pd->port_rcvegrbuf);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800964 pd->port_rcvegrbuf = NULL;
965bail:
966 return ret;
967}
968
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700969
970/* common code for the mappings on dma_alloc_coherent mem */
971static int ipath_mmap_mem(struct vm_area_struct *vma,
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700972 struct ipath_portdata *pd, unsigned len, int write_ok,
973 void *kvaddr, char *what)
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700974{
975 struct ipath_devdata *dd = pd->port_dd;
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700976 unsigned long pfn;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700977 int ret;
978
979 if ((vma->vm_end - vma->vm_start) > len) {
980 dev_info(&dd->pcidev->dev,
981 "FAIL on %s: len %lx > %x\n", what,
982 vma->vm_end - vma->vm_start, len);
983 ret = -EFAULT;
984 goto bail;
985 }
986
987 if (!write_ok) {
988 if (vma->vm_flags & VM_WRITE) {
989 dev_info(&dd->pcidev->dev,
990 "%s must be mapped readonly\n", what);
991 ret = -EPERM;
992 goto bail;
993 }
994
995 /* don't allow them to later change with mprotect */
996 vma->vm_flags &= ~VM_MAYWRITE;
997 }
998
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700999 pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001000 ret = remap_pfn_range(vma, vma->vm_start, pfn,
1001 len, vma->vm_page_prot);
1002 if (ret)
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001003 dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
1004 "bytes r%c failed: %d\n", what, pd->port_port,
1005 pfn, len, write_ok?'w':'o', ret);
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001006 else
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001007 ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
1008 "r%c\n", what, pd->port_port, pfn, len,
1009 write_ok?'w':'o');
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001010bail:
1011 return ret;
1012}
1013
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001014static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
1015 u64 ureg)
1016{
1017 unsigned long phys;
1018 int ret;
1019
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001020 /*
1021 * This is real hardware, so use io_remap. This is the mechanism
1022 * for the user process to update the head registers for their port
1023 * in the chip.
1024 */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001025 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
1026 dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
1027 "%lx > PAGE\n", vma->vm_end - vma->vm_start);
1028 ret = -EFAULT;
1029 } else {
1030 phys = dd->ipath_physaddr + ureg;
1031 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1032
1033 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1034 ret = io_remap_pfn_range(vma, vma->vm_start,
1035 phys >> PAGE_SHIFT,
1036 vma->vm_end - vma->vm_start,
1037 vma->vm_page_prot);
1038 }
1039 return ret;
1040}
1041
1042static int mmap_piobufs(struct vm_area_struct *vma,
1043 struct ipath_devdata *dd,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001044 struct ipath_portdata *pd,
1045 unsigned piobufs, unsigned piocnt)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001046{
1047 unsigned long phys;
1048 int ret;
1049
1050 /*
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001051 * When we map the PIO buffers in the chip, we want to map them as
1052 * writeonly, no read possible. This prevents access to previous
1053 * process data, and catches users who might try to read the i/o
1054 * space due to a bug.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001055 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001056 if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001057 dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
1058 "reqlen %lx > PAGE\n",
1059 vma->vm_end - vma->vm_start);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001060 ret = -EINVAL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001061 goto bail;
1062 }
1063
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001064 phys = dd->ipath_physaddr + piobufs;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001065
Bryan O'Sullivaneb9dc6f2006-08-25 11:24:26 -07001066#if defined(__powerpc__)
1067 /* There isn't a generic way to specify writethrough mappings */
1068 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1069 pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
1070 pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
1071#endif
1072
Bryan O'Sullivan367fe712006-08-25 11:24:30 -07001073 /*
1074 * don't allow them to later change to readable with mprotect (for when
1075 * not initially mapped readable, as is normally the case)
1076 */
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001077 vma->vm_flags &= ~VM_MAYREAD;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001078 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1079
1080 ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
1081 vma->vm_end - vma->vm_start,
1082 vma->vm_page_prot);
1083bail:
1084 return ret;
1085}
1086
1087static int mmap_rcvegrbufs(struct vm_area_struct *vma,
1088 struct ipath_portdata *pd)
1089{
1090 struct ipath_devdata *dd = pd->port_dd;
1091 unsigned long start, size;
1092 size_t total_size, i;
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001093 unsigned long pfn;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001094 int ret;
1095
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001096 size = pd->port_rcvegrbuf_size;
1097 total_size = pd->port_rcvegrbuf_chunks * size;
1098 if ((vma->vm_end - vma->vm_start) > total_size) {
1099 dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
1100 "reqlen %lx > actual %lx\n",
1101 vma->vm_end - vma->vm_start,
1102 (unsigned long) total_size);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001103 ret = -EINVAL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001104 goto bail;
1105 }
1106
1107 if (vma->vm_flags & VM_WRITE) {
1108 dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
1109 "writable (flags=%lx)\n", vma->vm_flags);
1110 ret = -EPERM;
1111 goto bail;
1112 }
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001113 /* don't allow them to later change to writeable with mprotect */
1114 vma->vm_flags &= ~VM_MAYWRITE;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001115
1116 start = vma->vm_start;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001117
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001118 for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001119 pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
1120 ret = remap_pfn_range(vma, start, pfn, size,
1121 vma->vm_page_prot);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001122 if (ret < 0)
1123 goto bail;
1124 }
1125 ret = 0;
1126
1127bail:
1128 return ret;
1129}
1130
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001131/*
Nick Piggin3c845082007-12-13 15:58:57 -08001132 * ipath_file_vma_fault - handle a VMA page fault.
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001133 */
Nick Piggin3c845082007-12-13 15:58:57 -08001134static int ipath_file_vma_fault(struct vm_area_struct *vma,
1135 struct vm_fault *vmf)
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001136{
Nick Piggin3c845082007-12-13 15:58:57 -08001137 struct page *page;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001138
Nick Piggin3c845082007-12-13 15:58:57 -08001139 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001140 if (!page)
Nick Piggin3c845082007-12-13 15:58:57 -08001141 return VM_FAULT_SIGBUS;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001142 get_page(page);
Nick Piggin3c845082007-12-13 15:58:57 -08001143 vmf->page = page;
1144
1145 return 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001146}
1147
1148static struct vm_operations_struct ipath_file_vm_ops = {
Nick Piggin3c845082007-12-13 15:58:57 -08001149 .fault = ipath_file_vma_fault,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001150};
1151
1152static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
1153 struct ipath_portdata *pd, unsigned subport)
1154{
1155 unsigned long len;
1156 struct ipath_devdata *dd;
1157 void *addr;
1158 size_t size;
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001159 int ret = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001160
1161 /* If the port is not shared, all addresses should be physical */
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001162 if (!pd->port_subport_cnt)
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001163 goto bail;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001164
1165 dd = pd->port_dd;
1166 size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
1167
1168 /*
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001169 * Each process has all the subport uregbase, rcvhdrq, and
1170 * rcvegrbufs mmapped - as an array for all the processes,
1171 * and also separately for this process.
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001172 */
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001173 if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
1174 addr = pd->subport_uregbase;
1175 size = PAGE_SIZE * pd->port_subport_cnt;
1176 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
1177 addr = pd->subport_rcvhdr_base;
1178 size = pd->port_rcvhdrq_size * pd->port_subport_cnt;
1179 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
1180 addr = pd->subport_rcvegrbuf;
1181 size *= pd->port_subport_cnt;
1182 } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
1183 PAGE_SIZE * subport)) {
1184 addr = pd->subport_uregbase + PAGE_SIZE * subport;
1185 size = PAGE_SIZE;
1186 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
1187 pd->port_rcvhdrq_size * subport)) {
1188 addr = pd->subport_rcvhdr_base +
1189 pd->port_rcvhdrq_size * subport;
1190 size = pd->port_rcvhdrq_size;
1191 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
1192 size * subport)) {
1193 addr = pd->subport_rcvegrbuf + size * subport;
1194 /* rcvegrbufs are read-only on the slave */
1195 if (vma->vm_flags & VM_WRITE) {
1196 dev_info(&dd->pcidev->dev,
1197 "Can't map eager buffers as "
1198 "writable (flags=%lx)\n", vma->vm_flags);
1199 ret = -EPERM;
1200 goto bail;
1201 }
1202 /*
1203 * Don't allow permission to later change to writeable
1204 * with mprotect.
1205 */
1206 vma->vm_flags &= ~VM_MAYWRITE;
1207 } else {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001208 goto bail;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001209 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001210 len = vma->vm_end - vma->vm_start;
1211 if (len > size) {
1212 ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
1213 ret = -EINVAL;
1214 goto bail;
1215 }
1216
1217 vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
1218 vma->vm_ops = &ipath_file_vm_ops;
1219 vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001220 ret = 1;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001221
1222bail:
1223 return ret;
1224}
1225
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001226/**
1227 * ipath_mmap - mmap various structures into user space
1228 * @fp: the file pointer
1229 * @vma: the VM area
1230 *
1231 * We use this to have a shared buffer between the kernel and the user code
1232 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1233 * buffers in the chip. We have the open and close entries so we can bump
1234 * the ref count and keep the driver from being unloaded while still mapped.
1235 */
1236static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
1237{
1238 struct ipath_portdata *pd;
1239 struct ipath_devdata *dd;
1240 u64 pgaddr, ureg;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001241 unsigned piobufs, piocnt;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001242 int ret;
1243
1244 pd = port_fp(fp);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001245 if (!pd) {
1246 ret = -EINVAL;
1247 goto bail;
1248 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001249 dd = pd->port_dd;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001250
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001251 /*
1252 * This is the ipath_do_user_init() code, mapping the shared buffers
1253 * into the user process. The address referred to by vm_pgoff is the
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001254 * file offset passed via mmap(). For shared ports, this is the
1255 * kernel vmalloc() address of the pages to share with the master.
1256 * For non-shared or master ports, this is a physical address.
1257 * We only do one mmap for each space mapped.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001258 */
1259 pgaddr = vma->vm_pgoff << PAGE_SHIFT;
1260
1261 /*
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001262 * Check for 0 in case one of the allocations failed, but user
1263 * called mmap anyway.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001264 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001265 if (!pgaddr) {
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001266 ret = -EINVAL;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001267 goto bail;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001268 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001269
1270 ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
1271 (unsigned long long) pgaddr, vma->vm_start,
1272 vma->vm_end - vma->vm_start, dd->ipath_unit,
1273 pd->port_port, subport_fp(fp));
1274
1275 /*
1276 * Physical addresses must fit in 40 bits for our hardware.
1277 * Check for kernel virtual addresses first, anything else must
1278 * match a HW or memory address.
1279 */
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001280 ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
1281 if (ret) {
1282 if (ret > 0)
1283 ret = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001284 goto bail;
1285 }
1286
Ralph Campbella18e26a2008-01-06 21:02:34 -08001287 ureg = dd->ipath_uregbase + dd->ipath_ureg_align * pd->port_port;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001288 if (!pd->port_subport_cnt) {
1289 /* port is not shared */
Dave Olsone2ab41c2008-05-07 11:00:15 -07001290 piocnt = pd->port_piocnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001291 piobufs = pd->port_piobufs;
1292 } else if (!subport_fp(fp)) {
1293 /* caller is the master */
Dave Olsone2ab41c2008-05-07 11:00:15 -07001294 piocnt = (pd->port_piocnt / pd->port_subport_cnt) +
1295 (pd->port_piocnt % pd->port_subport_cnt);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001296 piobufs = pd->port_piobufs +
Dave Olsone2ab41c2008-05-07 11:00:15 -07001297 dd->ipath_palign * (pd->port_piocnt - piocnt);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001298 } else {
1299 unsigned slave = subport_fp(fp) - 1;
1300
1301 /* caller is a slave */
Dave Olsone2ab41c2008-05-07 11:00:15 -07001302 piocnt = pd->port_piocnt / pd->port_subport_cnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001303 piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
1304 }
1305
1306 if (pgaddr == ureg)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001307 ret = mmap_ureg(vma, dd, ureg);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001308 else if (pgaddr == piobufs)
1309 ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
1310 else if (pgaddr == dd->ipath_pioavailregs_phys)
1311 /* in-memory copy of pioavail registers */
1312 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001313 (void *) dd->ipath_pioavailregs_dma,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001314 "pioavail registers");
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001315 else if (pgaddr == pd->port_rcvegr_phys)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001316 ret = mmap_rcvegrbufs(vma, pd);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001317 else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001318 /*
Bryan O'Sullivan525d0ca2006-08-25 11:24:39 -07001319 * The rcvhdrq itself; readonly except on HT (so have
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001320 * to allow writable mapping), multiple pages, contiguous
1321 * from an i/o perspective.
1322 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001323 ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001324 pd->port_rcvhdrq,
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001325 "rcvhdrq");
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001326 else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001327 /* in-memory copy of rcvhdrq tail register */
1328 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001329 pd->port_rcvhdrtail_kvaddr,
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001330 "rcvhdrq tail");
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001331 else
1332 ret = -EINVAL;
1333
1334 vma->vm_private_data = NULL;
1335
1336 if (ret < 0)
1337 dev_info(&dd->pcidev->dev,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001338 "Failure %d on off %llx len %lx\n",
1339 -ret, (unsigned long long)pgaddr,
1340 vma->vm_end - vma->vm_start);
1341bail:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001342 return ret;
1343}
1344
Arthur Jones70c51da2007-09-14 12:22:49 -07001345static unsigned ipath_poll_hdrqfull(struct ipath_portdata *pd)
1346{
1347 unsigned pollflag = 0;
1348
1349 if ((pd->poll_type & IPATH_POLL_TYPE_OVERFLOW) &&
1350 pd->port_hdrqfull != pd->port_hdrqfull_poll) {
1351 pollflag |= POLLIN | POLLRDNORM;
1352 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1353 }
1354
1355 return pollflag;
1356}
1357
Robert Walshf2d04232007-06-18 14:24:49 -07001358static unsigned int ipath_poll_urgent(struct ipath_portdata *pd,
1359 struct file *fp,
1360 struct poll_table_struct *pt)
1361{
1362 unsigned pollflag = 0;
1363 struct ipath_devdata *dd;
1364
1365 dd = pd->port_dd;
1366
Arthur Jones70c51da2007-09-14 12:22:49 -07001367 /* variable access in ipath_poll_hdrqfull() needs this */
1368 rmb();
1369 pollflag = ipath_poll_hdrqfull(pd);
Robert Walshf2d04232007-06-18 14:24:49 -07001370
Arthur Jones70c51da2007-09-14 12:22:49 -07001371 if (pd->port_urgent != pd->port_urgent_poll) {
Robert Walshf2d04232007-06-18 14:24:49 -07001372 pollflag |= POLLIN | POLLRDNORM;
Arthur Jones70c51da2007-09-14 12:22:49 -07001373 pd->port_urgent_poll = pd->port_urgent;
Robert Walshf2d04232007-06-18 14:24:49 -07001374 }
1375
1376 if (!pollflag) {
Arthur Jones70c51da2007-09-14 12:22:49 -07001377 /* this saves a spin_lock/unlock in interrupt handler... */
Robert Walshf2d04232007-06-18 14:24:49 -07001378 set_bit(IPATH_PORT_WAITING_URG, &pd->port_flag);
Arthur Jones70c51da2007-09-14 12:22:49 -07001379 /* flush waiting flag so don't miss an event... */
1380 wmb();
Robert Walshf2d04232007-06-18 14:24:49 -07001381 poll_wait(fp, &pd->port_wait, pt);
1382 }
1383
1384 return pollflag;
1385}
1386
1387static unsigned int ipath_poll_next(struct ipath_portdata *pd,
1388 struct file *fp,
1389 struct poll_table_struct *pt)
1390{
Arthur Jones70c51da2007-09-14 12:22:49 -07001391 u32 head;
1392 u32 tail;
Robert Walshf2d04232007-06-18 14:24:49 -07001393 unsigned pollflag = 0;
1394 struct ipath_devdata *dd;
1395
1396 dd = pd->port_dd;
1397
Arthur Jones70c51da2007-09-14 12:22:49 -07001398 /* variable access in ipath_poll_hdrqfull() needs this */
1399 rmb();
1400 pollflag = ipath_poll_hdrqfull(pd);
1401
Robert Walshf2d04232007-06-18 14:24:49 -07001402 head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
Ralph Campbellc59a80a2007-12-20 02:43:23 -08001403 if (pd->port_rcvhdrtail_kvaddr)
1404 tail = ipath_get_rcvhdrtail(pd);
1405 else
1406 tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
Robert Walshf2d04232007-06-18 14:24:49 -07001407
Arthur Jones70c51da2007-09-14 12:22:49 -07001408 if (head != tail)
Robert Walshf2d04232007-06-18 14:24:49 -07001409 pollflag |= POLLIN | POLLRDNORM;
Arthur Jones70c51da2007-09-14 12:22:49 -07001410 else {
1411 /* this saves a spin_lock/unlock in interrupt handler */
Robert Walshf2d04232007-06-18 14:24:49 -07001412 set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
Arthur Jones70c51da2007-09-14 12:22:49 -07001413 /* flush waiting flag so we don't miss an event */
1414 wmb();
Robert Walshf2d04232007-06-18 14:24:49 -07001415
Dave Olsond8274862007-12-21 01:50:59 -08001416 set_bit(pd->port_port + dd->ipath_r_intravail_shift,
Robert Walshf2d04232007-06-18 14:24:49 -07001417 &dd->ipath_rcvctrl);
1418
1419 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1420 dd->ipath_rcvctrl);
1421
1422 if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
1423 ipath_write_ureg(dd, ur_rcvhdrhead,
1424 dd->ipath_rhdrhead_intr_off | head,
1425 pd->port_port);
1426
1427 poll_wait(fp, &pd->port_wait, pt);
1428 }
1429
1430 return pollflag;
1431}
1432
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001433static unsigned int ipath_poll(struct file *fp,
1434 struct poll_table_struct *pt)
1435{
1436 struct ipath_portdata *pd;
Robert Walshf2d04232007-06-18 14:24:49 -07001437 unsigned pollflag;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001438
1439 pd = port_fp(fp);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001440 if (!pd)
Robert Walshf2d04232007-06-18 14:24:49 -07001441 pollflag = 0;
1442 else if (pd->poll_type & IPATH_POLL_TYPE_URGENT)
1443 pollflag = ipath_poll_urgent(pd, fp, pt);
1444 else
1445 pollflag = ipath_poll_next(pd, fp, pt);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001446
Bryan O'Sullivane35d7102006-08-25 11:24:46 -07001447 return pollflag;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001448}
1449
Mark Debbage0df62912007-06-18 14:24:45 -07001450static int ipath_supports_subports(int user_swmajor, int user_swminor)
1451{
1452 /* no subport implementation prior to software version 1.3 */
1453 return (user_swmajor > 1) || (user_swminor >= 3);
1454}
1455
1456static int ipath_compatible_subports(int user_swmajor, int user_swminor)
1457{
1458 /* this code is written long-hand for clarity */
1459 if (IPATH_USER_SWMAJOR != user_swmajor) {
1460 /* no promise of compatibility if major mismatch */
1461 return 0;
1462 }
1463 if (IPATH_USER_SWMAJOR == 1) {
1464 switch (IPATH_USER_SWMINOR) {
1465 case 0:
1466 case 1:
1467 case 2:
1468 /* no subport implementation so cannot be compatible */
1469 return 0;
1470 case 3:
1471 /* 3 is only compatible with itself */
1472 return user_swminor == 3;
1473 default:
1474 /* >= 4 are compatible (or are expected to be) */
1475 return user_swminor >= 4;
1476 }
1477 }
1478 /* make no promises yet for future major versions */
1479 return 0;
1480}
1481
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001482static int init_subports(struct ipath_devdata *dd,
1483 struct ipath_portdata *pd,
1484 const struct ipath_user_info *uinfo)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001485{
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001486 int ret = 0;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001487 unsigned num_subports;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001488 size_t size;
1489
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001490 /*
Mark Debbagebacf4012007-06-18 14:24:46 -07001491 * If the user is requesting zero subports,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001492 * skip the subport allocation.
1493 */
Mark Debbagebacf4012007-06-18 14:24:46 -07001494 if (uinfo->spu_subport_cnt <= 0)
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001495 goto bail;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001496
Mark Debbage0df62912007-06-18 14:24:45 -07001497 /* Self-consistency check for ipath_compatible_subports() */
1498 if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&
1499 !ipath_compatible_subports(IPATH_USER_SWMAJOR,
1500 IPATH_USER_SWMINOR)) {
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001501 dev_info(&dd->pcidev->dev,
Mark Debbage0df62912007-06-18 14:24:45 -07001502 "Inconsistent ipath_compatible_subports()\n");
1503 goto bail;
1504 }
1505
1506 /* Check for subport compatibility */
1507 if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,
1508 uinfo->spu_userversion & 0xffff)) {
1509 dev_info(&dd->pcidev->dev,
1510 "Mismatched user version (%d.%d) and driver "
1511 "version (%d.%d) while port sharing. Ensure "
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001512 "that driver and library are from the same "
1513 "release.\n",
Mark Debbage0df62912007-06-18 14:24:45 -07001514 (int) (uinfo->spu_userversion >> 16),
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001515 (int) (uinfo->spu_userversion & 0xffff),
Mark Debbage0df62912007-06-18 14:24:45 -07001516 IPATH_USER_SWMAJOR,
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001517 IPATH_USER_SWMINOR);
1518 goto bail;
1519 }
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001520 if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001521 ret = -EINVAL;
1522 goto bail;
1523 }
1524
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001525 num_subports = uinfo->spu_subport_cnt;
1526 pd->subport_uregbase = vmalloc(PAGE_SIZE * num_subports);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001527 if (!pd->subport_uregbase) {
1528 ret = -ENOMEM;
1529 goto bail;
1530 }
1531 /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
1532 size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001533 sizeof(u32), PAGE_SIZE) * num_subports;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001534 pd->subport_rcvhdr_base = vmalloc(size);
1535 if (!pd->subport_rcvhdr_base) {
1536 ret = -ENOMEM;
1537 goto bail_ureg;
1538 }
1539
1540 pd->subport_rcvegrbuf = vmalloc(pd->port_rcvegrbuf_chunks *
1541 pd->port_rcvegrbuf_size *
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001542 num_subports);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001543 if (!pd->subport_rcvegrbuf) {
1544 ret = -ENOMEM;
1545 goto bail_rhdr;
1546 }
1547
1548 pd->port_subport_cnt = uinfo->spu_subport_cnt;
1549 pd->port_subport_id = uinfo->spu_subport_id;
1550 pd->active_slaves = 1;
Ralph Campbell947d7612007-03-15 14:44:48 -07001551 set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001552 memset(pd->subport_uregbase, 0, PAGE_SIZE * num_subports);
1553 memset(pd->subport_rcvhdr_base, 0, size);
1554 memset(pd->subport_rcvegrbuf, 0, pd->port_rcvegrbuf_chunks *
1555 pd->port_rcvegrbuf_size *
1556 num_subports);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001557 goto bail;
1558
1559bail_rhdr:
1560 vfree(pd->subport_rcvhdr_base);
1561bail_ureg:
1562 vfree(pd->subport_uregbase);
1563 pd->subport_uregbase = NULL;
1564bail:
1565 return ret;
1566}
1567
1568static int try_alloc_port(struct ipath_devdata *dd, int port,
1569 struct file *fp,
1570 const struct ipath_user_info *uinfo)
1571{
1572 struct ipath_portdata *pd;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001573 int ret;
1574
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001575 if (!(pd = dd->ipath_pd[port])) {
1576 void *ptmp;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001577
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001578 pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001579
1580 /*
1581 * Allocate memory for use in ipath_tid_update() just once
1582 * at open, not per call. Reduces cost of expected send
1583 * setup.
1584 */
1585 ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
1586 dd->ipath_rcvtidcnt * sizeof(struct page **),
1587 GFP_KERNEL);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001588 if (!pd || !ptmp) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001589 ipath_dev_err(dd, "Unable to allocate portdata "
1590 "memory, failing open\n");
1591 ret = -ENOMEM;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001592 kfree(pd);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001593 kfree(ptmp);
1594 goto bail;
1595 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001596 dd->ipath_pd[port] = pd;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001597 dd->ipath_pd[port]->port_port = port;
1598 dd->ipath_pd[port]->port_dd = dd;
1599 dd->ipath_pd[port]->port_tid_pg_list = ptmp;
1600 init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
1601 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001602 if (!pd->port_cnt) {
1603 pd->userversion = uinfo->spu_userversion;
1604 init_user_egr_sizes(pd);
1605 if ((ret = init_subports(dd, pd, uinfo)) != 0)
1606 goto bail;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001607 ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
1608 current->comm, current->pid, dd->ipath_unit,
1609 port);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001610 pd->port_cnt = 1;
1611 port_fp(fp) = pd;
1612 pd->port_pid = current->pid;
1613 strncpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001614 ipath_stats.sps_ports++;
1615 ret = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001616 } else
1617 ret = -EBUSY;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001618
1619bail:
1620 return ret;
1621}
1622
1623static inline int usable(struct ipath_devdata *dd)
1624{
1625 return dd &&
1626 (dd->ipath_flags & IPATH_PRESENT) &&
1627 dd->ipath_kregbase &&
1628 dd->ipath_lid &&
1629 !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
1630 | IPATH_LINKUNK));
1631}
1632
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001633static int find_free_port(int unit, struct file *fp,
1634 const struct ipath_user_info *uinfo)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001635{
1636 struct ipath_devdata *dd = ipath_lookup(unit);
1637 int ret, i;
1638
1639 if (!dd) {
1640 ret = -ENODEV;
1641 goto bail;
1642 }
1643
1644 if (!usable(dd)) {
1645 ret = -ENETDOWN;
1646 goto bail;
1647 }
1648
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001649 for (i = 1; i < dd->ipath_cfgports; i++) {
1650 ret = try_alloc_port(dd, i, fp, uinfo);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001651 if (ret != -EBUSY)
1652 goto bail;
1653 }
1654 ret = -EBUSY;
1655
1656bail:
1657 return ret;
1658}
1659
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001660static int find_best_unit(struct file *fp,
1661 const struct ipath_user_info *uinfo)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001662{
1663 int ret = 0, i, prefunit = -1, devmax;
1664 int maxofallports, npresent, nup;
1665 int ndev;
1666
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001667 devmax = ipath_count_units(&npresent, &nup, &maxofallports);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001668
1669 /*
1670 * This code is present to allow a knowledgeable person to
1671 * specify the layout of processes to processors before opening
1672 * this driver, and then we'll assign the process to the "closest"
Bryan O'Sullivan525d0ca2006-08-25 11:24:39 -07001673 * InfiniPath chip to that processor (we assume reasonable connectivity,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001674 * for now). This code assumes that if affinity has been set
1675 * before this point, that at most one cpu is set; for now this
1676 * is reasonable. I check for both cpus_empty() and cpus_full(),
1677 * in case some kernel variant sets none of the bits when no
1678 * affinity is set. 2.6.11 and 12 kernels have all present
1679 * cpus set. Some day we'll have to fix it up further to handle
Bryan O'Sullivan525d0ca2006-08-25 11:24:39 -07001680 * a cpu subset. This algorithm fails for two HT chips connected
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001681 * in tunnel fashion. Eventually this needs real topology
1682 * information. There may be some issues with dual core numbering
1683 * as well. This needs more work prior to release.
1684 */
1685 if (!cpus_empty(current->cpus_allowed) &&
1686 !cpus_full(current->cpus_allowed)) {
Bryan O'Sullivanf0810da2007-03-15 14:45:13 -07001687 int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001688 for (i = 0; i < ncpus; i++)
1689 if (cpu_isset(i, current->cpus_allowed)) {
1690 ipath_cdbg(PROC, "%s[%u] affinity set for "
Bryan O'Sullivanf0810da2007-03-15 14:45:13 -07001691 "cpu %d/%d\n", current->comm,
1692 current->pid, i, ncpus);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001693 curcpu = i;
Bryan O'Sullivanf0810da2007-03-15 14:45:13 -07001694 nset++;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001695 }
Bryan O'Sullivanf0810da2007-03-15 14:45:13 -07001696 if (curcpu != -1 && nset != ncpus) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001697 if (npresent) {
1698 prefunit = curcpu / (ncpus / npresent);
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001699 ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001700 "%d cpus/chip, select unit %d\n",
1701 current->comm, current->pid,
1702 npresent, ncpus, ncpus / npresent,
1703 prefunit);
1704 }
1705 }
1706 }
1707
1708 /*
1709 * user ports start at 1, kernel port is 0
1710 * For now, we do round-robin access across all chips
1711 */
1712
1713 if (prefunit != -1)
1714 devmax = prefunit + 1;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001715recheck:
1716 for (i = 1; i < maxofallports; i++) {
1717 for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
1718 ndev++) {
1719 struct ipath_devdata *dd = ipath_lookup(ndev);
1720
1721 if (!usable(dd))
1722 continue; /* can't use this unit */
1723 if (i >= dd->ipath_cfgports)
1724 /*
1725 * Maxed out on users of this unit. Try
1726 * next.
1727 */
1728 continue;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001729 ret = try_alloc_port(dd, i, fp, uinfo);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001730 if (!ret)
1731 goto done;
1732 }
1733 }
1734
1735 if (npresent) {
1736 if (nup == 0) {
1737 ret = -ENETDOWN;
1738 ipath_dbg("No ports available (none initialized "
1739 "and ready)\n");
1740 } else {
1741 if (prefunit > 0) {
1742 /* if started above 0, retry from 0 */
1743 ipath_cdbg(PROC,
1744 "%s[%u] no ports on prefunit "
1745 "%d, clear and re-check\n",
1746 current->comm, current->pid,
1747 prefunit);
1748 devmax = ipath_count_units(NULL, NULL,
1749 NULL);
1750 prefunit = -1;
1751 goto recheck;
1752 }
1753 ret = -EBUSY;
1754 ipath_dbg("No ports available\n");
1755 }
1756 } else {
1757 ret = -ENXIO;
1758 ipath_dbg("No boards found\n");
1759 }
1760
1761done:
1762 return ret;
1763}
1764
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001765static int find_shared_port(struct file *fp,
1766 const struct ipath_user_info *uinfo)
1767{
1768 int devmax, ndev, i;
1769 int ret = 0;
1770
1771 devmax = ipath_count_units(NULL, NULL, NULL);
1772
1773 for (ndev = 0; ndev < devmax; ndev++) {
1774 struct ipath_devdata *dd = ipath_lookup(ndev);
1775
Dave Olson5d1ce032008-04-16 21:01:12 -07001776 if (!usable(dd))
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001777 continue;
1778 for (i = 1; i < dd->ipath_cfgports; i++) {
1779 struct ipath_portdata *pd = dd->ipath_pd[i];
1780
1781 /* Skip ports which are not yet open */
1782 if (!pd || !pd->port_cnt)
1783 continue;
1784 /* Skip port if it doesn't match the requested one */
1785 if (pd->port_subport_id != uinfo->spu_subport_id)
1786 continue;
1787 /* Verify the sharing process matches the master */
1788 if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
1789 pd->userversion != uinfo->spu_userversion ||
1790 pd->port_cnt >= pd->port_subport_cnt) {
1791 ret = -EINVAL;
1792 goto done;
1793 }
1794 port_fp(fp) = pd;
1795 subport_fp(fp) = pd->port_cnt++;
Dave Olson755807a2007-12-06 00:28:02 -08001796 pd->port_subpid[subport_fp(fp)] = current->pid;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001797 tidcursor_fp(fp) = 0;
1798 pd->active_slaves |= 1 << subport_fp(fp);
1799 ipath_cdbg(PROC,
1800 "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
1801 current->comm, current->pid,
1802 subport_fp(fp),
1803 pd->port_comm, pd->port_pid,
1804 dd->ipath_unit, pd->port_port);
1805 ret = 1;
1806 goto done;
1807 }
1808 }
1809
1810done:
1811 return ret;
1812}
1813
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001814static int ipath_open(struct inode *in, struct file *fp)
1815{
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07001816 /* The real work is performed later in ipath_assign_port() */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001817 fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
1818 return fp->private_data ? 0 : -ENOMEM;
1819}
1820
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07001821/* Get port early, so can set affinity prior to memory allocation */
1822static int ipath_assign_port(struct file *fp,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001823 const struct ipath_user_info *uinfo)
1824{
1825 int ret;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001826 int i_minor;
Mark Debbage0df62912007-06-18 14:24:45 -07001827 unsigned swmajor, swminor;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001828
1829 /* Check to be sure we haven't already initialized this file */
1830 if (port_fp(fp)) {
1831 ret = -EINVAL;
1832 goto done;
1833 }
1834
1835 /* for now, if major version is different, bail */
Mark Debbage0df62912007-06-18 14:24:45 -07001836 swmajor = uinfo->spu_userversion >> 16;
1837 if (swmajor != IPATH_USER_SWMAJOR) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001838 ipath_dbg("User major version %d not same as driver "
1839 "major %d\n", uinfo->spu_userversion >> 16,
1840 IPATH_USER_SWMAJOR);
1841 ret = -ENODEV;
1842 goto done;
1843 }
1844
1845 swminor = uinfo->spu_userversion & 0xffff;
1846 if (swminor != IPATH_USER_SWMINOR)
1847 ipath_dbg("User minor version %d not same as driver "
1848 "minor %d\n", swminor, IPATH_USER_SWMINOR);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001849
1850 mutex_lock(&ipath_mutex);
1851
Mark Debbage0df62912007-06-18 14:24:45 -07001852 if (ipath_compatible_subports(swmajor, swminor) &&
1853 uinfo->spu_subport_cnt &&
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001854 (ret = find_shared_port(fp, uinfo))) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001855 if (ret > 0)
1856 ret = 0;
Dave Olson124b4dc2008-04-16 21:09:32 -07001857 goto done_chk_sdma;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001858 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001859
Josef Sipek1cfd6e62006-12-08 02:37:10 -08001860 i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001861 ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
Josef Sipek1cfd6e62006-12-08 02:37:10 -08001862 (long)fp->f_path.dentry->d_inode->i_rdev, i_minor);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001863
1864 if (i_minor)
1865 ret = find_free_port(i_minor - 1, fp, uinfo);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001866 else
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001867 ret = find_best_unit(fp, uinfo);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001868
Dave Olson124b4dc2008-04-16 21:09:32 -07001869done_chk_sdma:
1870 if (!ret) {
1871 struct ipath_filedata *fd = fp->private_data;
1872 const struct ipath_portdata *pd = fd->pd;
1873 const struct ipath_devdata *dd = pd->port_dd;
1874
1875 fd->pq = ipath_user_sdma_queue_create(&dd->pcidev->dev,
1876 dd->ipath_unit,
1877 pd->port_port,
1878 fd->subport);
1879
1880 if (!fd->pq)
1881 ret = -ENOMEM;
1882 }
1883
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001884 mutex_unlock(&ipath_mutex);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001885
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07001886done:
1887 return ret;
1888}
1889
1890
1891static int ipath_do_user_init(struct file *fp,
1892 const struct ipath_user_info *uinfo)
1893{
1894 int ret;
Ralph Campbell947d7612007-03-15 14:44:48 -07001895 struct ipath_portdata *pd = port_fp(fp);
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07001896 struct ipath_devdata *dd;
1897 u32 head32;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001898
Ralph Campbell947d7612007-03-15 14:44:48 -07001899 /* Subports don't need to initialize anything since master did it. */
1900 if (subport_fp(fp)) {
1901 ret = wait_event_interruptible(pd->port_wait,
1902 !test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
1903 goto done;
1904 }
1905
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001906 dd = pd->port_dd;
1907
1908 if (uinfo->spu_rcvhdrsize) {
1909 ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
1910 if (ret)
1911 goto done;
1912 }
1913
1914 /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
1915
Dave Olsone2ab41c2008-05-07 11:00:15 -07001916 /* some ports may get extra buffers, calculate that here */
1917 if (pd->port_port <= dd->ipath_ports_extrabuf)
1918 pd->port_piocnt = dd->ipath_pbufsport + 1;
1919 else
1920 pd->port_piocnt = dd->ipath_pbufsport;
1921
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001922 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
Dave Olsone2ab41c2008-05-07 11:00:15 -07001923 if (pd->port_port <= dd->ipath_ports_extrabuf)
1924 pd->port_pio_base = (dd->ipath_pbufsport + 1)
1925 * (pd->port_port - 1);
1926 else
1927 pd->port_pio_base = dd->ipath_ports_extrabuf +
1928 dd->ipath_pbufsport * (pd->port_port - 1);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001929 pd->port_piobufs = dd->ipath_piobufbase +
Dave Olsone2ab41c2008-05-07 11:00:15 -07001930 pd->port_pio_base * dd->ipath_palign;
1931 ipath_cdbg(VERBOSE, "piobuf base for port %u is 0x%x, piocnt %u,"
1932 " first pio %u\n", pd->port_port, pd->port_piobufs,
1933 pd->port_piocnt, pd->port_pio_base);
1934 ipath_chg_pioavailkernel(dd, pd->port_pio_base, pd->port_piocnt, 0);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001935
1936 /*
1937 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
1938 * array for time being. If pd->port_port > chip-supported,
1939 * we need to do extra stuff here to handle by handling overflow
1940 * through port 0, someday
1941 */
1942 ret = ipath_create_rcvhdrq(dd, pd);
1943 if (!ret)
1944 ret = ipath_create_user_egr(pd);
1945 if (ret)
1946 goto done;
1947
1948 /*
1949 * set the eager head register for this port to the current values
1950 * of the tail pointers, since we don't know if they were
1951 * updated on last use of the port.
1952 */
1953 head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
1954 ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
Dave Olson755807a2007-12-06 00:28:02 -08001955 pd->port_lastrcvhdrqtail = -1;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001956 ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
1957 pd->port_port, head32);
1958 pd->port_tidcursor = 0; /* start at beginning after open */
Arthur Jones70c51da2007-09-14 12:22:49 -07001959
1960 /* initialize poll variables... */
1961 pd->port_urgent = 0;
1962 pd->port_urgent_poll = 0;
1963 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1964
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001965 /*
Ralph Campbell9355fb62008-04-16 21:09:29 -07001966 * Now enable the port for receive.
1967 * For chips that are set to DMA the tail register to memory
1968 * when they change (and when the update bit transitions from
1969 * 0 to 1. So for those chips, we turn it off and then back on.
1970 * This will (very briefly) affect any other open ports, but the
1971 * duration is very short, and therefore isn't an issue. We
1972 * explictly set the in-memory tail copy to 0 beforehand, so we
1973 * don't have to wait to be sure the DMA update has happened
1974 * (chip resets head/tail to 0 on transition to enable).
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001975 */
Dave Olsond8274862007-12-21 01:50:59 -08001976 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001977 &dd->ipath_rcvctrl);
Ralph Campbell9355fb62008-04-16 21:09:29 -07001978 if (!(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
1979 if (pd->port_rcvhdrtail_kvaddr)
1980 ipath_clear_rcvhdrtail(pd);
1981 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
Dave Olsond8274862007-12-21 01:50:59 -08001982 dd->ipath_rcvctrl &
1983 ~(1ULL << dd->ipath_r_tailupd_shift));
Ralph Campbell9355fb62008-04-16 21:09:29 -07001984 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001985 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1986 dd->ipath_rcvctrl);
Ralph Campbell947d7612007-03-15 14:44:48 -07001987 /* Notify any waiting slaves */
1988 if (pd->port_subport_cnt) {
1989 clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1990 wake_up(&pd->port_wait);
1991 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001992done:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001993 return ret;
1994}
1995
1996/**
1997 * unlock_exptid - unlock any expected TID entries port still had in use
1998 * @pd: port
1999 *
2000 * We don't actually update the chip here, because we do a bulk update
2001 * below, using ipath_f_clear_tids.
2002 */
2003static void unlock_expected_tids(struct ipath_portdata *pd)
2004{
2005 struct ipath_devdata *dd = pd->port_dd;
2006 int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
2007 int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
2008
2009 ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
2010 pd->port_port);
2011 for (i = port_tidbase; i < maxtid; i++) {
Ralph Campbell9355fb62008-04-16 21:09:29 -07002012 struct page *ps = dd->ipath_pageshadow[i];
2013
2014 if (!ps)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002015 continue;
2016
Ralph Campbell9355fb62008-04-16 21:09:29 -07002017 dd->ipath_pageshadow[i] = NULL;
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07002018 pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
2019 PAGE_SIZE, PCI_DMA_FROMDEVICE);
Ralph Campbell9355fb62008-04-16 21:09:29 -07002020 ipath_release_user_pages_on_close(&ps, 1);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002021 cnt++;
2022 ipath_stats.sps_pageunlocks++;
2023 }
2024 if (cnt)
2025 ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
2026 pd->port_port, cnt);
2027
2028 if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
2029 ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
2030 (unsigned long long) ipath_stats.sps_pagelocks,
2031 (unsigned long long)
2032 ipath_stats.sps_pageunlocks);
2033}
2034
2035static int ipath_close(struct inode *in, struct file *fp)
2036{
2037 int ret = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002038 struct ipath_filedata *fd;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002039 struct ipath_portdata *pd;
2040 struct ipath_devdata *dd;
2041 unsigned port;
2042
2043 ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
2044 (long)in->i_rdev, fp->private_data);
2045
2046 mutex_lock(&ipath_mutex);
2047
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002048 fd = (struct ipath_filedata *) fp->private_data;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002049 fp->private_data = NULL;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002050 pd = fd->pd;
2051 if (!pd) {
2052 mutex_unlock(&ipath_mutex);
2053 goto bail;
2054 }
Dave Olson124b4dc2008-04-16 21:09:32 -07002055
2056 dd = pd->port_dd;
2057
2058 /* drain user sdma queue */
2059 ipath_user_sdma_queue_drain(dd, fd->pq);
2060 ipath_user_sdma_queue_destroy(fd->pq);
2061
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002062 if (--pd->port_cnt) {
2063 /*
2064 * XXX If the master closes the port before the slave(s),
2065 * revoke the mmap for the eager receive queue so
2066 * the slave(s) don't wait for receive data forever.
2067 */
2068 pd->active_slaves &= ~(1 << fd->subport);
Dave Olson755807a2007-12-06 00:28:02 -08002069 pd->port_subpid[fd->subport] = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002070 mutex_unlock(&ipath_mutex);
2071 goto bail;
2072 }
2073 port = pd->port_port;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002074
2075 if (pd->port_hdrqfull) {
2076 ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors "
2077 "during run\n", pd->port_comm, pd->port_pid,
2078 pd->port_hdrqfull);
2079 pd->port_hdrqfull = 0;
2080 }
2081
2082 if (pd->port_rcvwait_to || pd->port_piowait_to
2083 || pd->port_rcvnowait || pd->port_pionowait) {
2084 ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
2085 "%u rcv %u, pio already\n",
2086 pd->port_port, pd->port_rcvwait_to,
2087 pd->port_piowait_to, pd->port_rcvnowait,
2088 pd->port_pionowait);
2089 pd->port_rcvwait_to = pd->port_piowait_to =
2090 pd->port_rcvnowait = pd->port_pionowait = 0;
2091 }
2092 if (pd->port_flag) {
Arthur Jonesbb917142008-04-16 21:09:31 -07002093 ipath_cdbg(PROC, "port %u port_flag set: 0x%lx\n",
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002094 pd->port_port, pd->port_flag);
2095 pd->port_flag = 0;
2096 }
2097
2098 if (dd->ipath_kregbase) {
Arthur Jones70c51da2007-09-14 12:22:49 -07002099 /* atomically clear receive enable port and intr avail. */
Dave Olsond8274862007-12-21 01:50:59 -08002100 clear_bit(dd->ipath_r_portenable_shift + port,
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002101 &dd->ipath_rcvctrl);
Dave Olsond8274862007-12-21 01:50:59 -08002102 clear_bit(pd->port_port + dd->ipath_r_intravail_shift,
Arthur Jones70c51da2007-09-14 12:22:49 -07002103 &dd->ipath_rcvctrl);
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002104 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
2105 dd->ipath_rcvctrl);
2106 /* and read back from chip to be sure that nothing
2107 * else is in flight when we do the rest */
2108 (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002109
2110 /* clean up the pkeys for this port user */
2111 ipath_clean_part_key(pd, dd);
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002112 /*
2113 * be paranoid, and never write 0's to these, just use an
2114 * unused part of the port 0 tail page. Of course,
2115 * rcvhdraddr points to a large chunk of memory, so this
2116 * could still trash things, but at least it won't trash
2117 * page 0, and by disabling the port, it should stop "soon",
2118 * even if a packet or two is in already in flight after we
2119 * disabled the port.
2120 */
2121 ipath_write_kreg_port(dd,
2122 dd->ipath_kregs->kr_rcvhdrtailaddr, port,
2123 dd->ipath_dummy_hdrq_phys);
2124 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
2125 pd->port_port, dd->ipath_dummy_hdrq_phys);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002126
Dave Olsone2ab41c2008-05-07 11:00:15 -07002127 ipath_disarm_piobufs(dd, pd->port_pio_base, pd->port_piocnt);
2128 ipath_chg_pioavailkernel(dd, pd->port_pio_base,
2129 pd->port_piocnt, 1);
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002130
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07002131 dd->ipath_f_clear_tids(dd, pd->port_port);
2132
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002133 if (dd->ipath_pageshadow)
2134 unlock_expected_tids(pd);
2135 ipath_stats.sps_ports--;
2136 ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
2137 pd->port_comm, pd->port_pid,
2138 dd->ipath_unit, port);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002139 }
2140
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002141 pd->port_pid = 0;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07002142 dd->ipath_pd[pd->port_port] = NULL; /* before releasing mutex */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002143 mutex_unlock(&ipath_mutex);
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07002144 ipath_free_pddata(dd, pd); /* after releasing the mutex */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002145
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002146bail:
2147 kfree(fd);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002148 return ret;
2149}
2150
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002151static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002152 struct ipath_port_info __user *uinfo)
2153{
2154 struct ipath_port_info info;
2155 int nup;
2156 int ret;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002157 size_t sz;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002158
2159 (void) ipath_count_units(NULL, &nup, NULL);
2160 info.num_active = nup;
2161 info.unit = pd->port_dd->ipath_unit;
2162 info.port = pd->port_port;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002163 info.subport = subport;
2164 /* Don't return new fields if old library opened the port. */
Mark Debbage0df62912007-06-18 14:24:45 -07002165 if (ipath_supports_subports(pd->userversion >> 16,
2166 pd->userversion & 0xffff)) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002167 /* Number of user ports available for this device. */
2168 info.num_ports = pd->port_dd->ipath_cfgports - 1;
2169 info.num_subports = pd->port_subport_cnt;
2170 sz = sizeof(info);
2171 } else
2172 sz = sizeof(info) - 2 * sizeof(u16);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002173
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002174 if (copy_to_user(uinfo, &info, sz)) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002175 ret = -EFAULT;
2176 goto bail;
2177 }
2178 ret = 0;
2179
2180bail:
2181 return ret;
2182}
2183
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002184static int ipath_get_slave_info(struct ipath_portdata *pd,
2185 void __user *slave_mask_addr)
2186{
2187 int ret = 0;
2188
2189 if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
2190 ret = -EFAULT;
2191 return ret;
2192}
2193
Dave Olson124b4dc2008-04-16 21:09:32 -07002194static int ipath_sdma_get_inflight(struct ipath_user_sdma_queue *pq,
2195 u32 __user *inflightp)
2196{
2197 const u32 val = ipath_user_sdma_inflight_counter(pq);
2198
2199 if (put_user(val, inflightp))
2200 return -EFAULT;
2201
2202 return 0;
2203}
2204
2205static int ipath_sdma_get_complete(struct ipath_devdata *dd,
2206 struct ipath_user_sdma_queue *pq,
2207 u32 __user *completep)
2208{
2209 u32 val;
2210 int err;
2211
2212 err = ipath_user_sdma_make_progress(dd, pq);
2213 if (err < 0)
2214 return err;
2215
2216 val = ipath_user_sdma_complete_counter(pq);
2217 if (put_user(val, completep))
2218 return -EFAULT;
2219
2220 return 0;
2221}
2222
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002223static ssize_t ipath_write(struct file *fp, const char __user *data,
2224 size_t count, loff_t *off)
2225{
2226 const struct ipath_cmd __user *ucmd;
2227 struct ipath_portdata *pd;
2228 const void __user *src;
2229 size_t consumed, copy;
2230 struct ipath_cmd cmd;
2231 ssize_t ret = 0;
2232 void *dest;
2233
2234 if (count < sizeof(cmd.type)) {
2235 ret = -EINVAL;
2236 goto bail;
2237 }
2238
2239 ucmd = (const struct ipath_cmd __user *) data;
2240
2241 if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
2242 ret = -EFAULT;
2243 goto bail;
2244 }
2245
2246 consumed = sizeof(cmd.type);
2247
2248 switch (cmd.type) {
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07002249 case IPATH_CMD_ASSIGN_PORT:
2250 case __IPATH_CMD_USER_INIT:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002251 case IPATH_CMD_USER_INIT:
2252 copy = sizeof(cmd.cmd.user_info);
2253 dest = &cmd.cmd.user_info;
2254 src = &ucmd->cmd.user_info;
2255 break;
2256 case IPATH_CMD_RECV_CTRL:
2257 copy = sizeof(cmd.cmd.recv_ctrl);
2258 dest = &cmd.cmd.recv_ctrl;
2259 src = &ucmd->cmd.recv_ctrl;
2260 break;
2261 case IPATH_CMD_PORT_INFO:
2262 copy = sizeof(cmd.cmd.port_info);
2263 dest = &cmd.cmd.port_info;
2264 src = &ucmd->cmd.port_info;
2265 break;
2266 case IPATH_CMD_TID_UPDATE:
2267 case IPATH_CMD_TID_FREE:
2268 copy = sizeof(cmd.cmd.tid_info);
2269 dest = &cmd.cmd.tid_info;
2270 src = &ucmd->cmd.tid_info;
2271 break;
2272 case IPATH_CMD_SET_PART_KEY:
2273 copy = sizeof(cmd.cmd.part_key);
2274 dest = &cmd.cmd.part_key;
2275 src = &ucmd->cmd.part_key;
2276 break;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07002277 case __IPATH_CMD_SLAVE_INFO:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002278 copy = sizeof(cmd.cmd.slave_mask_addr);
2279 dest = &cmd.cmd.slave_mask_addr;
2280 src = &ucmd->cmd.slave_mask_addr;
2281 break;
Arthur Jones569b87b2007-03-15 14:45:05 -07002282 case IPATH_CMD_PIOAVAILUPD: // force an update of PIOAvail reg
2283 copy = 0;
2284 src = NULL;
2285 dest = NULL;
2286 break;
Robert Walshf2d04232007-06-18 14:24:49 -07002287 case IPATH_CMD_POLL_TYPE:
2288 copy = sizeof(cmd.cmd.poll_type);
2289 dest = &cmd.cmd.poll_type;
2290 src = &ucmd->cmd.poll_type;
2291 break;
Dave Olson6ac50722007-08-09 03:11:38 -07002292 case IPATH_CMD_ARMLAUNCH_CTRL:
2293 copy = sizeof(cmd.cmd.armlaunch_ctrl);
2294 dest = &cmd.cmd.armlaunch_ctrl;
2295 src = &ucmd->cmd.armlaunch_ctrl;
2296 break;
Dave Olson124b4dc2008-04-16 21:09:32 -07002297 case IPATH_CMD_SDMA_INFLIGHT:
2298 copy = sizeof(cmd.cmd.sdma_inflight);
2299 dest = &cmd.cmd.sdma_inflight;
2300 src = &ucmd->cmd.sdma_inflight;
2301 break;
2302 case IPATH_CMD_SDMA_COMPLETE:
2303 copy = sizeof(cmd.cmd.sdma_complete);
2304 dest = &cmd.cmd.sdma_complete;
2305 src = &ucmd->cmd.sdma_complete;
2306 break;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002307 default:
2308 ret = -EINVAL;
2309 goto bail;
2310 }
2311
Arthur Jones569b87b2007-03-15 14:45:05 -07002312 if (copy) {
2313 if ((count - consumed) < copy) {
2314 ret = -EINVAL;
2315 goto bail;
2316 }
2317
2318 if (copy_from_user(dest, src, copy)) {
2319 ret = -EFAULT;
2320 goto bail;
2321 }
2322
2323 consumed += copy;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002324 }
2325
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002326 pd = port_fp(fp);
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07002327 if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
2328 cmd.type != IPATH_CMD_ASSIGN_PORT) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002329 ret = -EINVAL;
2330 goto bail;
2331 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002332
2333 switch (cmd.type) {
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07002334 case IPATH_CMD_ASSIGN_PORT:
2335 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2336 if (ret)
2337 goto bail;
2338 break;
2339 case __IPATH_CMD_USER_INIT:
2340 /* backwards compatibility, get port first */
2341 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2342 if (ret)
2343 goto bail;
2344 /* and fall through to current version. */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002345 case IPATH_CMD_USER_INIT:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002346 ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
2347 if (ret)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002348 goto bail;
2349 ret = ipath_get_base_info(
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002350 fp, (void __user *) (unsigned long)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002351 cmd.cmd.user_info.spu_base_info,
2352 cmd.cmd.user_info.spu_base_info_size);
2353 break;
2354 case IPATH_CMD_RECV_CTRL:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002355 ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002356 break;
2357 case IPATH_CMD_PORT_INFO:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002358 ret = ipath_port_info(pd, subport_fp(fp),
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002359 (struct ipath_port_info __user *)
2360 (unsigned long) cmd.cmd.port_info);
2361 break;
2362 case IPATH_CMD_TID_UPDATE:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002363 ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002364 break;
2365 case IPATH_CMD_TID_FREE:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002366 ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002367 break;
2368 case IPATH_CMD_SET_PART_KEY:
2369 ret = ipath_set_part_key(pd, cmd.cmd.part_key);
2370 break;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07002371 case __IPATH_CMD_SLAVE_INFO:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002372 ret = ipath_get_slave_info(pd,
2373 (void __user *) (unsigned long)
2374 cmd.cmd.slave_mask_addr);
2375 break;
Arthur Jones569b87b2007-03-15 14:45:05 -07002376 case IPATH_CMD_PIOAVAILUPD:
Ralph Campbellc4b4d162008-04-16 21:09:26 -07002377 ipath_force_pio_avail_update(pd->port_dd);
Arthur Jones569b87b2007-03-15 14:45:05 -07002378 break;
Robert Walshf2d04232007-06-18 14:24:49 -07002379 case IPATH_CMD_POLL_TYPE:
2380 pd->poll_type = cmd.cmd.poll_type;
2381 break;
Dave Olson6ac50722007-08-09 03:11:38 -07002382 case IPATH_CMD_ARMLAUNCH_CTRL:
2383 if (cmd.cmd.armlaunch_ctrl)
2384 ipath_enable_armlaunch(pd->port_dd);
2385 else
2386 ipath_disable_armlaunch(pd->port_dd);
2387 break;
Dave Olson124b4dc2008-04-16 21:09:32 -07002388 case IPATH_CMD_SDMA_INFLIGHT:
2389 ret = ipath_sdma_get_inflight(user_sdma_queue_fp(fp),
2390 (u32 __user *) (unsigned long)
2391 cmd.cmd.sdma_inflight);
2392 break;
2393 case IPATH_CMD_SDMA_COMPLETE:
2394 ret = ipath_sdma_get_complete(pd->port_dd,
2395 user_sdma_queue_fp(fp),
2396 (u32 __user *) (unsigned long)
2397 cmd.cmd.sdma_complete);
2398 break;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002399 }
2400
2401 if (ret >= 0)
2402 ret = consumed;
2403
2404bail:
2405 return ret;
2406}
2407
Dave Olson124b4dc2008-04-16 21:09:32 -07002408static ssize_t ipath_writev(struct kiocb *iocb, const struct iovec *iov,
2409 unsigned long dim, loff_t off)
2410{
2411 struct file *filp = iocb->ki_filp;
2412 struct ipath_filedata *fp = filp->private_data;
2413 struct ipath_portdata *pd = port_fp(filp);
2414 struct ipath_user_sdma_queue *pq = fp->pq;
2415
2416 if (!dim)
2417 return -EINVAL;
2418
2419 return ipath_user_sdma_writev(pd->port_dd, pq, iov, dim);
2420}
2421
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002422static struct class *ipath_class;
2423
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002424static int init_cdev(int minor, char *name, const struct file_operations *fops,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002425 struct cdev **cdevp, struct device **devp)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002426{
2427 const dev_t dev = MKDEV(IPATH_MAJOR, minor);
2428 struct cdev *cdev = NULL;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002429 struct device *device = NULL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002430 int ret;
2431
2432 cdev = cdev_alloc();
2433 if (!cdev) {
2434 printk(KERN_ERR IPATH_DRV_NAME
2435 ": Could not allocate cdev for minor %d, %s\n",
2436 minor, name);
2437 ret = -ENOMEM;
2438 goto done;
2439 }
2440
2441 cdev->owner = THIS_MODULE;
2442 cdev->ops = fops;
2443 kobject_set_name(&cdev->kobj, name);
2444
2445 ret = cdev_add(cdev, dev, 1);
2446 if (ret < 0) {
2447 printk(KERN_ERR IPATH_DRV_NAME
2448 ": Could not add cdev for minor %d, %s (err %d)\n",
2449 minor, name, -ret);
2450 goto err_cdev;
2451 }
2452
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002453 device = device_create(ipath_class, NULL, dev, name);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002454
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002455 if (IS_ERR(device)) {
2456 ret = PTR_ERR(device);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002457 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002458 "device for minor %d, %s (err %d)\n",
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002459 minor, name, -ret);
2460 goto err_cdev;
2461 }
2462
2463 goto done;
2464
2465err_cdev:
2466 cdev_del(cdev);
2467 cdev = NULL;
2468
2469done:
2470 if (ret >= 0) {
2471 *cdevp = cdev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002472 *devp = device;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002473 } else {
2474 *cdevp = NULL;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002475 *devp = NULL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002476 }
2477
2478 return ret;
2479}
2480
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002481int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002482 struct cdev **cdevp, struct device **devp)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002483{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002484 return init_cdev(minor, name, fops, cdevp, devp);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002485}
2486
2487static void cleanup_cdev(struct cdev **cdevp,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002488 struct device **devp)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002489{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002490 struct device *dev = *devp;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002491
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002492 if (dev) {
2493 device_unregister(dev);
2494 *devp = NULL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002495 }
2496
2497 if (*cdevp) {
2498 cdev_del(*cdevp);
2499 *cdevp = NULL;
2500 }
2501}
2502
2503void ipath_cdev_cleanup(struct cdev **cdevp,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002504 struct device **devp)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002505{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002506 cleanup_cdev(cdevp, devp);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002507}
2508
2509static struct cdev *wildcard_cdev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002510static struct device *wildcard_dev;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002511
2512static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
2513
2514static int user_init(void)
2515{
2516 int ret;
2517
2518 ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
2519 if (ret < 0) {
2520 printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
2521 "chrdev region (err %d)\n", -ret);
2522 goto done;
2523 }
2524
2525 ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
2526
2527 if (IS_ERR(ipath_class)) {
2528 ret = PTR_ERR(ipath_class);
2529 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2530 "device class (err %d)\n", -ret);
2531 goto bail;
2532 }
2533
2534 goto done;
2535bail:
2536 unregister_chrdev_region(dev, IPATH_NMINORS);
2537done:
2538 return ret;
2539}
2540
2541static void user_cleanup(void)
2542{
2543 if (ipath_class) {
2544 class_destroy(ipath_class);
2545 ipath_class = NULL;
2546 }
2547
2548 unregister_chrdev_region(dev, IPATH_NMINORS);
2549}
2550
2551static atomic_t user_count = ATOMIC_INIT(0);
2552static atomic_t user_setup = ATOMIC_INIT(0);
2553
2554int ipath_user_add(struct ipath_devdata *dd)
2555{
2556 char name[10];
2557 int ret;
2558
2559 if (atomic_inc_return(&user_count) == 1) {
2560 ret = user_init();
2561 if (ret < 0) {
2562 ipath_dev_err(dd, "Unable to set up user support: "
2563 "error %d\n", -ret);
2564 goto bail;
2565 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002566 ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002567 &wildcard_dev);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002568 if (ret < 0) {
2569 ipath_dev_err(dd, "Could not create wildcard "
2570 "minor: error %d\n", -ret);
Bryan O'Sullivan0fd41362006-08-25 11:24:34 -07002571 goto bail_user;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002572 }
2573
2574 atomic_set(&user_setup, 1);
2575 }
2576
2577 snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
2578
2579 ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002580 &dd->user_cdev, &dd->user_dev);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002581 if (ret < 0)
2582 ipath_dev_err(dd, "Could not create user minor %d, %s\n",
2583 dd->ipath_unit + 1, name);
2584
2585 goto bail;
2586
Bryan O'Sullivan0fd41362006-08-25 11:24:34 -07002587bail_user:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002588 user_cleanup();
2589bail:
2590 return ret;
2591}
2592
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -07002593void ipath_user_remove(struct ipath_devdata *dd)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002594{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002595 cleanup_cdev(&dd->user_cdev, &dd->user_dev);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002596
2597 if (atomic_dec_return(&user_count) == 0) {
2598 if (atomic_read(&user_setup) == 0)
2599 goto bail;
2600
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002601 cleanup_cdev(&wildcard_cdev, &wildcard_dev);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002602 user_cleanup();
2603
2604 atomic_set(&user_setup, 0);
2605 }
2606bail:
2607 return;
2608}