blob: 73933a41ce840ad4a3079f69259b7397fa654c67 [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>
Jonathan Corbetf2b98572008-05-18 15:32:43 -060042#include <linux/smp_lock.h>
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080043#include <asm/pgtable.h>
44
45#include "ipath_kernel.h"
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -070046#include "ipath_common.h"
Dave Olson124b4dc2008-04-16 21:09:32 -070047#include "ipath_user_sdma.h"
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080048
49static int ipath_open(struct inode *, struct file *);
50static int ipath_close(struct inode *, struct file *);
51static ssize_t ipath_write(struct file *, const char __user *, size_t,
52 loff_t *);
Dave Olson124b4dc2008-04-16 21:09:32 -070053static ssize_t ipath_writev(struct kiocb *, const struct iovec *,
54 unsigned long , loff_t);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080055static unsigned int ipath_poll(struct file *, struct poll_table_struct *);
56static int ipath_mmap(struct file *, struct vm_area_struct *);
57
Arjan van de Ven2b8693c2007-02-12 00:55:32 -080058static const struct file_operations ipath_file_ops = {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080059 .owner = THIS_MODULE,
60 .write = ipath_write,
Dave Olson124b4dc2008-04-16 21:09:32 -070061 .aio_write = ipath_writev,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080062 .open = ipath_open,
63 .release = ipath_close,
64 .poll = ipath_poll,
65 .mmap = ipath_mmap
66};
67
Ralph Campbell0a5a83c2007-03-15 14:44:58 -070068/*
69 * Convert kernel virtual addresses to physical addresses so they don't
70 * potentially conflict with the chip addresses used as mmap offsets.
71 * It doesn't really matter what mmap offset we use as long as we can
72 * interpret it correctly.
73 */
74static u64 cvt_kvaddr(void *p)
75{
76 struct page *page;
77 u64 paddr = 0;
78
79 page = vmalloc_to_page(p);
80 if (page)
81 paddr = page_to_pfn(page) << PAGE_SHIFT;
82
83 return paddr;
84}
85
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -070086static int ipath_get_base_info(struct file *fp,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080087 void __user *ubase, size_t ubase_size)
88{
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -070089 struct ipath_portdata *pd = port_fp(fp);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080090 int ret = 0;
91 struct ipath_base_info *kinfo = NULL;
92 struct ipath_devdata *dd = pd->port_dd;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -070093 unsigned subport_cnt;
94 int shared, master;
95 size_t sz;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -080096
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -070097 subport_cnt = pd->port_subport_cnt;
98 if (!subport_cnt) {
99 shared = 0;
100 master = 0;
101 subport_cnt = 1;
102 } else {
103 shared = 1;
104 master = !subport_fp(fp);
105 }
106
107 sz = sizeof(*kinfo);
108 /* If port sharing is not requested, allow the old size structure */
109 if (!shared)
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700110 sz -= 7 * sizeof(u64);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700111 if (ubase_size < sz) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800112 ipath_cdbg(PROC,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700113 "Base size %zu, need %zu (version mismatch?)\n",
114 ubase_size, sz);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800115 ret = -EINVAL;
116 goto bail;
117 }
118
119 kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL);
120 if (kinfo == NULL) {
121 ret = -ENOMEM;
122 goto bail;
123 }
124
125 ret = dd->ipath_f_get_base_info(pd, kinfo);
126 if (ret < 0)
127 goto bail;
128
129 kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt;
130 kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize;
131 kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt;
132 kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize;
133 /*
134 * have to mmap whole thing
135 */
136 kinfo->spi_rcv_egrbuftotlen =
137 pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
138 kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk;
139 kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen /
140 pd->port_rcvegrbuf_chunks;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700141 kinfo->spi_tidcnt = dd->ipath_rcvtidcnt / subport_cnt;
142 if (master)
143 kinfo->spi_tidcnt += dd->ipath_rcvtidcnt % subport_cnt;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800144 /*
145 * for this use, may be ipath_cfgports summed over all chips that
146 * are are configured and present
147 */
148 kinfo->spi_nports = dd->ipath_cfgports;
149 /* unit (chip/board) our port is on */
150 kinfo->spi_unit = dd->ipath_unit;
151 /* for now, only a single page */
152 kinfo->spi_tid_maxsize = PAGE_SIZE;
153
154 /*
155 * Doing this per port, and based on the skip value, etc. This has
156 * to be the actual buffer size, since the protocol code treats it
157 * as an array.
158 *
159 * These have to be set to user addresses in the user code via mmap.
160 * These values are used on return to user code for the mmap target
161 * addresses only. For 32 bit, same 44 bit address problem, so use
162 * the physical address, not virtual. Before 2.6.11, using the
163 * page_address() macro worked, but in 2.6.11, even that returns the
164 * full 64 bit address (upper bits all 1's). So far, using the
165 * physical addresses (or chip offsets, for chip mapping) works, but
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700166 * no doubt some future kernel release will change that, and we'll be
167 * on to yet another method of dealing with this.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800168 */
169 kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700170 kinfo->spi_rcvhdr_tailaddr = (u64) pd->port_rcvhdrqtailaddr_phys;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800171 kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys;
172 kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys;
173 kinfo->spi_status = (u64) kinfo->spi_pioavailaddr +
174 (void *) dd->ipath_statusp -
175 (void *) dd->ipath_pioavailregs_dma;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700176 if (!shared) {
Dave Olsone2ab41c2008-05-07 11:00:15 -0700177 kinfo->spi_piocnt = pd->port_piocnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700178 kinfo->spi_piobufbase = (u64) pd->port_piobufs;
179 kinfo->__spi_uregbase = (u64) dd->ipath_uregbase +
Ralph Campbella18e26a2008-01-06 21:02:34 -0800180 dd->ipath_ureg_align * pd->port_port;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700181 } else if (master) {
Dave Olsone2ab41c2008-05-07 11:00:15 -0700182 kinfo->spi_piocnt = (pd->port_piocnt / subport_cnt) +
183 (pd->port_piocnt % subport_cnt);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700184 /* Master's PIO buffers are after all the slave's */
185 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
186 dd->ipath_palign *
Dave Olsone2ab41c2008-05-07 11:00:15 -0700187 (pd->port_piocnt - kinfo->spi_piocnt);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700188 } else {
189 unsigned slave = subport_fp(fp) - 1;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800190
Dave Olsone2ab41c2008-05-07 11:00:15 -0700191 kinfo->spi_piocnt = pd->port_piocnt / subport_cnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700192 kinfo->spi_piobufbase = (u64) pd->port_piobufs +
193 dd->ipath_palign * kinfo->spi_piocnt * slave;
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700194 }
Dave Olson1d7c2e52008-04-16 21:09:30 -0700195
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700196 if (shared) {
197 kinfo->spi_port_uregbase = (u64) dd->ipath_uregbase +
Ralph Campbella18e26a2008-01-06 21:02:34 -0800198 dd->ipath_ureg_align * pd->port_port;
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700199 kinfo->spi_port_rcvegrbuf = kinfo->spi_rcv_egrbufs;
200 kinfo->spi_port_rcvhdr_base = kinfo->spi_rcvhdr_base;
201 kinfo->spi_port_rcvhdr_tailaddr = kinfo->spi_rcvhdr_tailaddr;
202
Ralph Campbell0a5a83c2007-03-15 14:44:58 -0700203 kinfo->__spi_uregbase = cvt_kvaddr(pd->subport_uregbase +
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700204 PAGE_SIZE * subport_fp(fp));
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700205
Ralph Campbell0a5a83c2007-03-15 14:44:58 -0700206 kinfo->spi_rcvhdr_base = cvt_kvaddr(pd->subport_rcvhdr_base +
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700207 pd->port_rcvhdrq_size * subport_fp(fp));
Ralph Campbell947d7612007-03-15 14:44:48 -0700208 kinfo->spi_rcvhdr_tailaddr = 0;
Ralph Campbell0a5a83c2007-03-15 14:44:58 -0700209 kinfo->spi_rcv_egrbufs = cvt_kvaddr(pd->subport_rcvegrbuf +
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700210 pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size *
211 subport_fp(fp));
212
213 kinfo->spi_subport_uregbase =
214 cvt_kvaddr(pd->subport_uregbase);
215 kinfo->spi_subport_rcvegrbuf =
216 cvt_kvaddr(pd->subport_rcvegrbuf);
217 kinfo->spi_subport_rcvhdr_base =
218 cvt_kvaddr(pd->subport_rcvhdr_base);
219 ipath_cdbg(PROC, "port %u flags %x %llx %llx %llx\n",
220 kinfo->spi_port, kinfo->spi_runtime_flags,
221 (unsigned long long) kinfo->spi_subport_uregbase,
222 (unsigned long long) kinfo->spi_subport_rcvegrbuf,
223 (unsigned long long) kinfo->spi_subport_rcvhdr_base);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700224 }
225
Dave Olson1bf77242008-12-05 11:13:19 -0800226 /*
227 * All user buffers are 2KB buffers. If we ever support
228 * giving 4KB buffers to user processes, this will need some
229 * work.
230 */
231 kinfo->spi_pioindex = (kinfo->spi_piobufbase -
232 (dd->ipath_piobufbase & 0xffffffff)) / dd->ipath_palign;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800233 kinfo->spi_pioalign = dd->ipath_palign;
234
235 kinfo->spi_qpair = IPATH_KD_QP;
Dave Olson826d8012008-04-16 21:01:12 -0700236 /*
237 * user mode PIO buffers are always 2KB, even when 4KB can
238 * be received, and sent via the kernel; this is ibmaxlen
239 * for 2K MTU.
240 */
241 kinfo->spi_piosize = dd->ipath_piosize2k - 2 * sizeof(u32);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800242 kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */
243 kinfo->spi_port = pd->port_port;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700244 kinfo->spi_subport = subport_fp(fp);
Bryan O'Sullivaneaf67332006-05-23 11:32:31 -0700245 kinfo->spi_sw_version = IPATH_KERN_SWVERSION;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800246 kinfo->spi_hw_version = dd->ipath_revision;
247
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700248 if (master) {
249 kinfo->spi_runtime_flags |= IPATH_RUNTIME_MASTER;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700250 }
251
Mark Debbagec7e29ff2007-03-15 14:44:59 -0700252 sz = (ubase_size < sizeof(*kinfo)) ? ubase_size : sizeof(*kinfo);
253 if (copy_to_user(ubase, kinfo, sz))
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800254 ret = -EFAULT;
255
256bail:
257 kfree(kinfo);
258 return ret;
259}
260
261/**
262 * ipath_tid_update - update a port TID
263 * @pd: the port
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700264 * @fp: the ipath device file
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800265 * @ti: the TID information
266 *
267 * The new implementation as of Oct 2004 is that the driver assigns
268 * the tid and returns it to the caller. To make it easier to
269 * catch bugs, and to reduce search time, we keep a cursor for
270 * each port, walking the shadow tid array to find one that's not
271 * in use.
272 *
273 * For now, if we can't allocate the full list, we fail, although
274 * in the long run, we'll allocate as many as we can, and the
275 * caller will deal with that by trying the remaining pages later.
276 * That means that when we fail, we have to mark the tids as not in
277 * use again, in our shadow copy.
278 *
279 * It's up to the caller to free the tids when they are done.
280 * We'll unlock the pages as they free them.
281 *
282 * Also, right now we are locking one page at a time, but since
283 * the intended use of this routine is for a single group of
284 * virtually contiguous pages, that should change to improve
285 * performance.
286 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700287static int ipath_tid_update(struct ipath_portdata *pd, struct file *fp,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800288 const struct ipath_tid_info *ti)
289{
290 int ret = 0, ntids;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700291 u32 tid, porttid, cnt, i, tidcnt, tidoff;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800292 u16 *tidlist;
293 struct ipath_devdata *dd = pd->port_dd;
294 u64 physaddr;
295 unsigned long vaddr;
296 u64 __iomem *tidbase;
297 unsigned long tidmap[8];
298 struct page **pagep = NULL;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700299 unsigned subport = subport_fp(fp);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800300
301 if (!dd->ipath_pageshadow) {
302 ret = -ENOMEM;
303 goto done;
304 }
305
306 cnt = ti->tidcnt;
307 if (!cnt) {
308 ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n",
309 (unsigned long long) ti->tidlist);
310 /*
311 * Should we treat as success? likely a bug
312 */
313 ret = -EFAULT;
314 goto done;
315 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700316 porttid = pd->port_port * dd->ipath_rcvtidcnt;
317 if (!pd->port_subport_cnt) {
318 tidcnt = dd->ipath_rcvtidcnt;
319 tid = pd->port_tidcursor;
320 tidoff = 0;
321 } else if (!subport) {
322 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
323 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
324 tidoff = dd->ipath_rcvtidcnt - tidcnt;
325 porttid += tidoff;
326 tid = tidcursor_fp(fp);
327 } else {
328 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
329 tidoff = tidcnt * (subport - 1);
330 porttid += tidoff;
331 tid = tidcursor_fp(fp);
332 }
333 if (cnt > tidcnt) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800334 /* make sure it all fits in port_tid_pg_list */
335 dev_info(&dd->pcidev->dev, "Process tried to allocate %u "
336 "TIDs, only trying max (%u)\n", cnt, tidcnt);
337 cnt = tidcnt;
338 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700339 pagep = &((struct page **) pd->port_tid_pg_list)[tidoff];
340 tidlist = &((u16 *) &pagep[dd->ipath_rcvtidcnt])[tidoff];
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800341
342 memset(tidmap, 0, sizeof(tidmap));
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800343 /* before decrement; chip actual # */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800344 ntids = tidcnt;
345 tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) +
346 dd->ipath_rcvtidbase +
347 porttid * sizeof(*tidbase));
348
349 ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n",
350 pd->port_port, cnt, tid, tidbase);
351
352 /* virtual address of first page in transfer */
353 vaddr = ti->tidvaddr;
354 if (!access_ok(VERIFY_WRITE, (void __user *) vaddr,
355 cnt * PAGE_SIZE)) {
356 ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n",
357 (void *)vaddr, cnt);
358 ret = -EFAULT;
359 goto done;
360 }
361 ret = ipath_get_user_pages(vaddr, cnt, pagep);
362 if (ret) {
363 if (ret == -EBUSY) {
364 ipath_dbg("Failed to lock addr %p, %u pages "
365 "(already locked)\n",
366 (void *) vaddr, cnt);
367 /*
368 * for now, continue, and see what happens but with
369 * the new implementation, this should never happen,
370 * unless perhaps the user has mpin'ed the pages
371 * themselves (something we need to test)
372 */
373 ret = 0;
374 } else {
375 dev_info(&dd->pcidev->dev,
376 "Failed to lock addr %p, %u pages: "
377 "errno %d\n", (void *) vaddr, cnt, -ret);
378 goto done;
379 }
380 }
381 for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) {
382 for (; ntids--; tid++) {
383 if (tid == tidcnt)
384 tid = 0;
385 if (!dd->ipath_pageshadow[porttid + tid])
386 break;
387 }
388 if (ntids < 0) {
389 /*
390 * oops, wrapped all the way through their TIDs,
391 * and didn't have enough free; see comments at
392 * start of routine
393 */
394 ipath_dbg("Not enough free TIDs for %u pages "
395 "(index %d), failing\n", cnt, i);
396 i--; /* last tidlist[i] not filled in */
397 ret = -ENOMEM;
398 break;
399 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700400 tidlist[i] = tid + tidoff;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800401 ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, "
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700402 "vaddr %lx\n", i, tid + tidoff, vaddr);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800403 /* we "know" system pages and TID pages are same size */
404 dd->ipath_pageshadow[porttid + tid] = pagep[i];
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700405 dd->ipath_physshadow[porttid + tid] = ipath_map_page(
406 dd->pcidev, pagep[i], 0, PAGE_SIZE,
407 PCI_DMA_FROMDEVICE);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800408 /*
409 * don't need atomic or it's overhead
410 */
411 __set_bit(tid, tidmap);
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700412 physaddr = dd->ipath_physshadow[porttid + tid];
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800413 ipath_stats.sps_pagelocks++;
414 ipath_cdbg(VERBOSE,
415 "TID %u, vaddr %lx, physaddr %llx pgp %p\n",
416 tid, vaddr, (unsigned long long) physaddr,
417 pagep[i]);
Joan Eslingerf716cdf2007-06-18 14:24:39 -0700418 dd->ipath_f_put_tid(dd, &tidbase[tid], RCVHQ_RCV_TYPE_EXPECTED,
419 physaddr);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800420 /*
421 * don't check this tid in ipath_portshadow, since we
422 * just filled it in; start with the next one.
423 */
424 tid++;
425 }
426
427 if (ret) {
428 u32 limit;
429 cleanup:
430 /* jump here if copy out of updated info failed... */
431 ipath_dbg("After failure (ret=%d), undo %d of %d entries\n",
432 -ret, i, cnt);
433 /* same code that's in ipath_free_tid() */
434 limit = sizeof(tidmap) * BITS_PER_BYTE;
435 if (limit > tidcnt)
436 /* just in case size changes in future */
437 limit = tidcnt;
438 tid = find_first_bit((const unsigned long *)tidmap, limit);
439 for (; tid < limit; tid++) {
440 if (!test_bit(tid, tidmap))
441 continue;
442 if (dd->ipath_pageshadow[porttid + tid]) {
443 ipath_cdbg(VERBOSE, "Freeing TID %u\n",
444 tid);
Joan Eslingerf716cdf2007-06-18 14:24:39 -0700445 dd->ipath_f_put_tid(dd, &tidbase[tid],
446 RCVHQ_RCV_TYPE_EXPECTED,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800447 dd->ipath_tidinvalid);
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700448 pci_unmap_page(dd->pcidev,
449 dd->ipath_physshadow[porttid + tid],
450 PAGE_SIZE, PCI_DMA_FROMDEVICE);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800451 dd->ipath_pageshadow[porttid + tid] = NULL;
452 ipath_stats.sps_pageunlocks++;
453 }
454 }
455 ipath_release_user_pages(pagep, cnt);
456 } else {
457 /*
458 * Copy the updated array, with ipath_tid's filled in, back
459 * to user. Since we did the copy in already, this "should
460 * never fail" If it does, we have to clean up...
461 */
462 if (copy_to_user((void __user *)
463 (unsigned long) ti->tidlist,
464 tidlist, cnt * sizeof(*tidlist))) {
465 ret = -EFAULT;
466 goto cleanup;
467 }
468 if (copy_to_user((void __user *) (unsigned long) ti->tidmap,
469 tidmap, sizeof tidmap)) {
470 ret = -EFAULT;
471 goto cleanup;
472 }
473 if (tid == tidcnt)
474 tid = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700475 if (!pd->port_subport_cnt)
476 pd->port_tidcursor = tid;
477 else
478 tidcursor_fp(fp) = tid;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800479 }
480
481done:
482 if (ret)
483 ipath_dbg("Failed to map %u TID pages, failing with %d\n",
484 ti->tidcnt, -ret);
485 return ret;
486}
487
488/**
489 * ipath_tid_free - free a port TID
490 * @pd: the port
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700491 * @subport: the subport
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800492 * @ti: the TID info
493 *
494 * right now we are unlocking one page at a time, but since
495 * the intended use of this routine is for a single group of
496 * virtually contiguous pages, that should change to improve
497 * performance. We check that the TID is in range for this port
498 * but otherwise don't check validity; if user has an error and
499 * frees the wrong tid, it's only their own data that can thereby
500 * be corrupted. We do check that the TID was in use, for sanity
501 * We always use our idea of the saved address, not the address that
502 * they pass in to us.
503 */
504
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700505static int ipath_tid_free(struct ipath_portdata *pd, unsigned subport,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800506 const struct ipath_tid_info *ti)
507{
508 int ret = 0;
509 u32 tid, porttid, cnt, limit, tidcnt;
510 struct ipath_devdata *dd = pd->port_dd;
511 u64 __iomem *tidbase;
512 unsigned long tidmap[8];
513
514 if (!dd->ipath_pageshadow) {
515 ret = -ENOMEM;
516 goto done;
517 }
518
519 if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap,
520 sizeof tidmap)) {
521 ret = -EFAULT;
522 goto done;
523 }
524
525 porttid = pd->port_port * dd->ipath_rcvtidcnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700526 if (!pd->port_subport_cnt)
527 tidcnt = dd->ipath_rcvtidcnt;
528 else if (!subport) {
529 tidcnt = (dd->ipath_rcvtidcnt / pd->port_subport_cnt) +
530 (dd->ipath_rcvtidcnt % pd->port_subport_cnt);
531 porttid += dd->ipath_rcvtidcnt - tidcnt;
532 } else {
533 tidcnt = dd->ipath_rcvtidcnt / pd->port_subport_cnt;
534 porttid += tidcnt * (subport - 1);
535 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800536 tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) +
537 dd->ipath_rcvtidbase +
538 porttid * sizeof(*tidbase));
539
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800540 limit = sizeof(tidmap) * BITS_PER_BYTE;
541 if (limit > tidcnt)
542 /* just in case size changes in future */
543 limit = tidcnt;
544 tid = find_first_bit(tidmap, limit);
545 ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) "
546 "set is %d, porttid %u\n", pd->port_port, ti->tidcnt,
547 limit, tid, porttid);
548 for (cnt = 0; tid < limit; tid++) {
549 /*
550 * small optimization; if we detect a run of 3 or so without
551 * any set, use find_first_bit again. That's mainly to
552 * accelerate the case where we wrapped, so we have some at
553 * the beginning, and some at the end, and a big gap
554 * in the middle.
555 */
556 if (!test_bit(tid, tidmap))
557 continue;
558 cnt++;
559 if (dd->ipath_pageshadow[porttid + tid]) {
Dave Olson3ac8c702007-10-03 12:47:00 -0700560 struct page *p;
561 p = dd->ipath_pageshadow[porttid + tid];
562 dd->ipath_pageshadow[porttid + tid] = NULL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800563 ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n",
Pavel Emelyanov40d97692008-05-13 11:45:32 -0700564 pid_nr(pd->port_pid), tid);
Joan Eslingerf716cdf2007-06-18 14:24:39 -0700565 dd->ipath_f_put_tid(dd, &tidbase[tid],
566 RCVHQ_RCV_TYPE_EXPECTED,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800567 dd->ipath_tidinvalid);
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700568 pci_unmap_page(dd->pcidev,
569 dd->ipath_physshadow[porttid + tid],
570 PAGE_SIZE, PCI_DMA_FROMDEVICE);
Dave Olson3ac8c702007-10-03 12:47:00 -0700571 ipath_release_user_pages(&p, 1);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800572 ipath_stats.sps_pageunlocks++;
573 } else
574 ipath_dbg("Unused tid %u, ignoring\n", tid);
575 }
576 if (cnt != ti->tidcnt)
577 ipath_dbg("passed in tidcnt %d, only %d bits set in map\n",
578 ti->tidcnt, cnt);
579done:
580 if (ret)
581 ipath_dbg("Failed to unmap %u TID pages, failing with %d\n",
582 ti->tidcnt, -ret);
583 return ret;
584}
585
586/**
587 * ipath_set_part_key - set a partition key
588 * @pd: the port
589 * @key: the key
590 *
591 * We can have up to 4 active at a time (other than the default, which is
592 * always allowed). This is somewhat tricky, since multiple ports may set
593 * the same key, so we reference count them, and clean up at exit. All 4
594 * partition keys are packed into a single infinipath register. It's an
595 * error for a process to set the same pkey multiple times. We provide no
596 * mechanism to de-allocate a pkey at this time, we may eventually need to
597 * do that. I've used the atomic operations, and no locking, and only make
598 * a single pass through what's available. This should be more than
599 * adequate for some time. I'll think about spinlocks or the like if and as
600 * it's necessary.
601 */
602static int ipath_set_part_key(struct ipath_portdata *pd, u16 key)
603{
604 struct ipath_devdata *dd = pd->port_dd;
605 int i, any = 0, pidx = -1;
606 u16 lkey = key & 0x7FFF;
607 int ret;
608
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700609 if (lkey == (IPATH_DEFAULT_P_KEY & 0x7FFF)) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800610 /* nothing to do; this key always valid */
611 ret = 0;
612 goto bail;
613 }
614
615 ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys "
616 "%hx:%x %hx:%x %hx:%x %hx:%x\n",
617 pd->port_port, key, dd->ipath_pkeys[0],
618 atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1],
619 atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2],
620 atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3],
621 atomic_read(&dd->ipath_pkeyrefs[3]));
622
623 if (!lkey) {
624 ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n",
625 pd->port_port);
626 ret = -EINVAL;
627 goto bail;
628 }
629
630 /*
631 * Set the full membership bit, because it has to be
632 * set in the register or the packet, and it seems
633 * cleaner to set in the register than to force all
634 * callers to set it. (see bug 4331)
635 */
636 key |= 0x8000;
637
638 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
639 if (!pd->port_pkeys[i] && pidx == -1)
640 pidx = i;
641 if (pd->port_pkeys[i] == key) {
642 ipath_cdbg(VERBOSE, "p%u tries to set same pkey "
643 "(%x) more than once\n",
644 pd->port_port, key);
645 ret = -EEXIST;
646 goto bail;
647 }
648 }
649 if (pidx == -1) {
650 ipath_dbg("All pkeys for port %u already in use, "
651 "can't set %x\n", pd->port_port, key);
652 ret = -EBUSY;
653 goto bail;
654 }
655 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
656 if (!dd->ipath_pkeys[i]) {
657 any++;
658 continue;
659 }
660 if (dd->ipath_pkeys[i] == key) {
661 atomic_t *pkrefs = &dd->ipath_pkeyrefs[i];
662
663 if (atomic_inc_return(pkrefs) > 1) {
664 pd->port_pkeys[pidx] = key;
665 ipath_cdbg(VERBOSE, "p%u set key %x "
666 "matches #%d, count now %d\n",
667 pd->port_port, key, i,
668 atomic_read(pkrefs));
669 ret = 0;
670 goto bail;
671 } else {
672 /*
673 * lost race, decrement count, catch below
674 */
675 atomic_dec(pkrefs);
676 ipath_cdbg(VERBOSE, "Lost race, count was "
677 "0, after dec, it's %d\n",
678 atomic_read(pkrefs));
679 any++;
680 }
681 }
682 if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) {
683 /*
684 * It makes no sense to have both the limited and
685 * full membership PKEY set at the same time since
686 * the unlimited one will disable the limited one.
687 */
688 ret = -EEXIST;
689 goto bail;
690 }
691 }
692 if (!any) {
693 ipath_dbg("port %u, all pkeys already in use, "
694 "can't set %x\n", pd->port_port, key);
695 ret = -EBUSY;
696 goto bail;
697 }
698 for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) {
699 if (!dd->ipath_pkeys[i] &&
700 atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) {
701 u64 pkey;
702
703 /* for ipathstats, etc. */
704 ipath_stats.sps_pkeys[i] = lkey;
705 pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key;
706 pkey =
707 (u64) dd->ipath_pkeys[0] |
708 ((u64) dd->ipath_pkeys[1] << 16) |
709 ((u64) dd->ipath_pkeys[2] << 32) |
710 ((u64) dd->ipath_pkeys[3] << 48);
711 ipath_cdbg(PROC, "p%u set key %x in #%d, "
712 "portidx %d, new pkey reg %llx\n",
713 pd->port_port, key, i, pidx,
714 (unsigned long long) pkey);
715 ipath_write_kreg(
716 dd, dd->ipath_kregs->kr_partitionkey, pkey);
717
718 ret = 0;
719 goto bail;
720 }
721 }
722 ipath_dbg("port %u, all pkeys already in use 2nd pass, "
723 "can't set %x\n", pd->port_port, key);
724 ret = -EBUSY;
725
726bail:
727 return ret;
728}
729
730/**
731 * ipath_manage_rcvq - manage a port's receive queue
732 * @pd: the port
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700733 * @subport: the subport
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800734 * @start_stop: action to carry out
735 *
736 * start_stop == 0 disables receive on the port, for use in queue
737 * overflow conditions. start_stop==1 re-enables, to be used to
738 * re-init the software copy of the head register
739 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700740static int ipath_manage_rcvq(struct ipath_portdata *pd, unsigned subport,
741 int start_stop)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800742{
743 struct ipath_devdata *dd = pd->port_dd;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800744
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700745 ipath_cdbg(PROC, "%sabling rcv for unit %u port %u:%u\n",
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800746 start_stop ? "en" : "dis", dd->ipath_unit,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700747 pd->port_port, subport);
748 if (subport)
749 goto bail;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800750 /* atomically clear receive enable port. */
751 if (start_stop) {
752 /*
753 * On enable, force in-memory copy of the tail register to
754 * 0, so that protocol code doesn't have to worry about
755 * whether or not the chip has yet updated the in-memory
756 * copy or not on return from the system call. The chip
757 * always resets it's tail register back to 0 on a
758 * transition from disabled to enabled. This could cause a
759 * problem if software was broken, and did the enable w/o
760 * the disable, but eventually the in-memory copy will be
761 * updated and correct itself, even in the face of software
762 * bugs.
763 */
Ralph Campbellc59a80a2007-12-20 02:43:23 -0800764 if (pd->port_rcvhdrtail_kvaddr)
765 ipath_clear_rcvhdrtail(pd);
Dave Olsond8274862007-12-21 01:50:59 -0800766 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800767 &dd->ipath_rcvctrl);
768 } else
Dave Olsond8274862007-12-21 01:50:59 -0800769 clear_bit(dd->ipath_r_portenable_shift + pd->port_port,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800770 &dd->ipath_rcvctrl);
771 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
772 dd->ipath_rcvctrl);
773 /* now be sure chip saw it before we return */
Roland Dreier44f8e3f2006-12-12 11:50:20 -0800774 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800775 if (start_stop) {
776 /*
777 * And try to be sure that tail reg update has happened too.
778 * This should in theory interlock with the RXE changes to
779 * the tail register. Don't assign it to the tail register
780 * in memory copy, since we could overwrite an update by the
781 * chip if we did.
782 */
Roland Dreier44f8e3f2006-12-12 11:50:20 -0800783 ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800784 }
785 /* always; new head should be equal to new tail; see above */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700786bail:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800787 return 0;
788}
789
790static void ipath_clean_part_key(struct ipath_portdata *pd,
791 struct ipath_devdata *dd)
792{
793 int i, j, pchanged = 0;
794 u64 oldpkey;
795
796 /* for debugging only */
797 oldpkey = (u64) dd->ipath_pkeys[0] |
798 ((u64) dd->ipath_pkeys[1] << 16) |
799 ((u64) dd->ipath_pkeys[2] << 32) |
800 ((u64) dd->ipath_pkeys[3] << 48);
801
802 for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) {
803 if (!pd->port_pkeys[i])
804 continue;
805 ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i,
806 pd->port_pkeys[i]);
807 for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) {
808 /* check for match independent of the global bit */
809 if ((dd->ipath_pkeys[j] & 0x7fff) !=
810 (pd->port_pkeys[i] & 0x7fff))
811 continue;
812 if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) {
813 ipath_cdbg(VERBOSE, "p%u clear key "
814 "%x matches #%d\n",
815 pd->port_port,
816 pd->port_pkeys[i], j);
817 ipath_stats.sps_pkeys[j] =
818 dd->ipath_pkeys[j] = 0;
819 pchanged++;
820 }
821 else ipath_cdbg(
822 VERBOSE, "p%u key %x matches #%d, "
823 "but ref still %d\n", pd->port_port,
824 pd->port_pkeys[i], j,
825 atomic_read(&dd->ipath_pkeyrefs[j]));
826 break;
827 }
828 pd->port_pkeys[i] = 0;
829 }
830 if (pchanged) {
831 u64 pkey = (u64) dd->ipath_pkeys[0] |
832 ((u64) dd->ipath_pkeys[1] << 16) |
833 ((u64) dd->ipath_pkeys[2] << 32) |
834 ((u64) dd->ipath_pkeys[3] << 48);
835 ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, "
836 "new pkey reg %llx\n", pd->port_port,
837 (unsigned long long) oldpkey,
838 (unsigned long long) pkey);
839 ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey,
840 pkey);
841 }
842}
843
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700844/*
845 * Initialize the port data with the receive buffer sizes
846 * so this can be done while the master port is locked.
847 * Otherwise, there is a race with a slave opening the port
848 * and seeing these fields uninitialized.
849 */
850static void init_user_egr_sizes(struct ipath_portdata *pd)
851{
852 struct ipath_devdata *dd = pd->port_dd;
853 unsigned egrperchunk, egrcnt, size;
854
855 /*
856 * to avoid wasting a lot of memory, we allocate 32KB chunks of
857 * physically contiguous memory, advance through it until used up
858 * and then allocate more. Of course, we need memory to store those
859 * extra pointers, now. Started out with 256KB, but under heavy
860 * memory pressure (creating large files and then copying them over
861 * NFS while doing lots of MPI jobs), we hit some allocation
862 * failures, even though we can sleep... (2.6.10) Still get
863 * failures at 64K. 32K is the lowest we can go without wasting
864 * additional memory.
865 */
866 size = 0x8000;
867 egrperchunk = size / dd->ipath_rcvegrbufsize;
868 egrcnt = dd->ipath_rcvegrcnt;
869 pd->port_rcvegrbuf_chunks = (egrcnt + egrperchunk - 1) / egrperchunk;
870 pd->port_rcvegrbufs_perchunk = egrperchunk;
871 pd->port_rcvegrbuf_size = size;
872}
873
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800874/**
875 * ipath_create_user_egr - allocate eager TID buffers
876 * @pd: the port to allocate TID buffers for
877 *
878 * This routine is now quite different for user and kernel, because
879 * the kernel uses skb's, for the accelerated network performance
880 * This is the user port version
881 *
882 * Allocate the eager TID buffers and program them into infinipath
883 * They are no longer completely contiguous, we do multiple allocation
884 * calls.
885 */
886static int ipath_create_user_egr(struct ipath_portdata *pd)
887{
888 struct ipath_devdata *dd = pd->port_dd;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700889 unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800890 size_t size;
891 int ret;
Bryan O'Sullivan0ed9a4a2006-07-01 04:36:01 -0700892 gfp_t gfp_flags;
893
894 /*
895 * GFP_USER, but without GFP_FS, so buffer cache can be
896 * coalesced (we hope); otherwise, even at order 4,
897 * heavy filesystem activity makes these fail, and we can
898 * use compound pages.
899 */
900 gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800901
902 egrcnt = dd->ipath_rcvegrcnt;
903 /* TID number offset for this port */
Ralph Campbell60948a42008-01-06 21:02:34 -0800904 egroff = (pd->port_port - 1) * egrcnt + dd->ipath_p0_rcvegrcnt;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800905 egrsize = dd->ipath_rcvegrbufsize;
906 ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid "
907 "offset %x, egrsize %u\n", egrcnt, egroff, egrsize);
908
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700909 chunk = pd->port_rcvegrbuf_chunks;
910 egrperchunk = pd->port_rcvegrbufs_perchunk;
911 size = pd->port_rcvegrbuf_size;
912 pd->port_rcvegrbuf = kmalloc(chunk * sizeof(pd->port_rcvegrbuf[0]),
913 GFP_KERNEL);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800914 if (!pd->port_rcvegrbuf) {
915 ret = -ENOMEM;
916 goto bail;
917 }
918 pd->port_rcvegrbuf_phys =
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700919 kmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0]),
920 GFP_KERNEL);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800921 if (!pd->port_rcvegrbuf_phys) {
922 ret = -ENOMEM;
923 goto bail_rcvegrbuf;
924 }
925 for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800926
927 pd->port_rcvegrbuf[e] = dma_alloc_coherent(
928 &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e],
929 gfp_flags);
930
931 if (!pd->port_rcvegrbuf[e]) {
932 ret = -ENOMEM;
933 goto bail_rcvegrbuf_phys;
934 }
935 }
936
937 pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0];
938
939 for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) {
940 dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk];
941 unsigned i;
942
943 for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
944 dd->ipath_f_put_tid(dd, e + egroff +
945 (u64 __iomem *)
946 ((char __iomem *)
947 dd->ipath_kregbase +
Joan Eslingerf716cdf2007-06-18 14:24:39 -0700948 dd->ipath_rcvegrbase),
949 RCVHQ_RCV_TYPE_EAGER, pa);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800950 pa += egrsize;
951 }
952 cond_resched(); /* don't hog the cpu */
953 }
954
955 ret = 0;
956 goto bail;
957
958bail_rcvegrbuf_phys:
959 for (e = 0; e < pd->port_rcvegrbuf_chunks &&
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700960 pd->port_rcvegrbuf[e]; e++) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800961 dma_free_coherent(&dd->pcidev->dev, size,
962 pd->port_rcvegrbuf[e],
963 pd->port_rcvegrbuf_phys[e]);
964
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700965 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700966 kfree(pd->port_rcvegrbuf_phys);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800967 pd->port_rcvegrbuf_phys = NULL;
968bail_rcvegrbuf:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -0700969 kfree(pd->port_rcvegrbuf);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -0800970 pd->port_rcvegrbuf = NULL;
971bail:
972 return ret;
973}
974
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700975
976/* common code for the mappings on dma_alloc_coherent mem */
977static int ipath_mmap_mem(struct vm_area_struct *vma,
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700978 struct ipath_portdata *pd, unsigned len, int write_ok,
979 void *kvaddr, char *what)
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700980{
981 struct ipath_devdata *dd = pd->port_dd;
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -0700982 unsigned long pfn;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700983 int ret;
984
985 if ((vma->vm_end - vma->vm_start) > len) {
986 dev_info(&dd->pcidev->dev,
987 "FAIL on %s: len %lx > %x\n", what,
988 vma->vm_end - vma->vm_start, len);
989 ret = -EFAULT;
990 goto bail;
991 }
992
993 if (!write_ok) {
994 if (vma->vm_flags & VM_WRITE) {
995 dev_info(&dd->pcidev->dev,
996 "%s must be mapped readonly\n", what);
997 ret = -EPERM;
998 goto bail;
999 }
1000
1001 /* don't allow them to later change with mprotect */
1002 vma->vm_flags &= ~VM_MAYWRITE;
1003 }
1004
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001005 pfn = virt_to_phys(kvaddr) >> PAGE_SHIFT;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001006 ret = remap_pfn_range(vma, vma->vm_start, pfn,
1007 len, vma->vm_page_prot);
1008 if (ret)
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001009 dev_info(&dd->pcidev->dev, "%s port%u mmap of %lx, %x "
1010 "bytes r%c failed: %d\n", what, pd->port_port,
1011 pfn, len, write_ok?'w':'o', ret);
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001012 else
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001013 ipath_cdbg(VERBOSE, "%s port%u mmaped %lx, %x bytes "
1014 "r%c\n", what, pd->port_port, pfn, len,
1015 write_ok?'w':'o');
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001016bail:
1017 return ret;
1018}
1019
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001020static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd,
1021 u64 ureg)
1022{
1023 unsigned long phys;
1024 int ret;
1025
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001026 /*
1027 * This is real hardware, so use io_remap. This is the mechanism
1028 * for the user process to update the head registers for their port
1029 * in the chip.
1030 */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001031 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
1032 dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen "
1033 "%lx > PAGE\n", vma->vm_end - vma->vm_start);
1034 ret = -EFAULT;
1035 } else {
1036 phys = dd->ipath_physaddr + ureg;
1037 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1038
1039 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1040 ret = io_remap_pfn_range(vma, vma->vm_start,
1041 phys >> PAGE_SHIFT,
1042 vma->vm_end - vma->vm_start,
1043 vma->vm_page_prot);
1044 }
1045 return ret;
1046}
1047
1048static int mmap_piobufs(struct vm_area_struct *vma,
1049 struct ipath_devdata *dd,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001050 struct ipath_portdata *pd,
1051 unsigned piobufs, unsigned piocnt)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001052{
1053 unsigned long phys;
1054 int ret;
1055
1056 /*
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001057 * When we map the PIO buffers in the chip, we want to map them as
1058 * writeonly, no read possible. This prevents access to previous
1059 * process data, and catches users who might try to read the i/o
1060 * space due to a bug.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001061 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001062 if ((vma->vm_end - vma->vm_start) > (piocnt * dd->ipath_palign)) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001063 dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: "
1064 "reqlen %lx > PAGE\n",
1065 vma->vm_end - vma->vm_start);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001066 ret = -EINVAL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001067 goto bail;
1068 }
1069
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001070 phys = dd->ipath_physaddr + piobufs;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001071
Bryan O'Sullivaneb9dc6f2006-08-25 11:24:26 -07001072#if defined(__powerpc__)
1073 /* There isn't a generic way to specify writethrough mappings */
1074 pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
1075 pgprot_val(vma->vm_page_prot) |= _PAGE_WRITETHRU;
1076 pgprot_val(vma->vm_page_prot) &= ~_PAGE_GUARDED;
1077#endif
1078
Bryan O'Sullivan367fe712006-08-25 11:24:30 -07001079 /*
1080 * don't allow them to later change to readable with mprotect (for when
1081 * not initially mapped readable, as is normally the case)
1082 */
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001083 vma->vm_flags &= ~VM_MAYREAD;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001084 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
1085
1086 ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT,
1087 vma->vm_end - vma->vm_start,
1088 vma->vm_page_prot);
1089bail:
1090 return ret;
1091}
1092
1093static int mmap_rcvegrbufs(struct vm_area_struct *vma,
1094 struct ipath_portdata *pd)
1095{
1096 struct ipath_devdata *dd = pd->port_dd;
1097 unsigned long start, size;
1098 size_t total_size, i;
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001099 unsigned long pfn;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001100 int ret;
1101
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001102 size = pd->port_rcvegrbuf_size;
1103 total_size = pd->port_rcvegrbuf_chunks * size;
1104 if ((vma->vm_end - vma->vm_start) > total_size) {
1105 dev_info(&dd->pcidev->dev, "FAIL on egr bufs: "
1106 "reqlen %lx > actual %lx\n",
1107 vma->vm_end - vma->vm_start,
1108 (unsigned long) total_size);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001109 ret = -EINVAL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001110 goto bail;
1111 }
1112
1113 if (vma->vm_flags & VM_WRITE) {
1114 dev_info(&dd->pcidev->dev, "Can't map eager buffers as "
1115 "writable (flags=%lx)\n", vma->vm_flags);
1116 ret = -EPERM;
1117 goto bail;
1118 }
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001119 /* don't allow them to later change to writeable with mprotect */
1120 vma->vm_flags &= ~VM_MAYWRITE;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001121
1122 start = vma->vm_start;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001123
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001124 for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) {
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001125 pfn = virt_to_phys(pd->port_rcvegrbuf[i]) >> PAGE_SHIFT;
1126 ret = remap_pfn_range(vma, start, pfn, size,
1127 vma->vm_page_prot);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001128 if (ret < 0)
1129 goto bail;
1130 }
1131 ret = 0;
1132
1133bail:
1134 return ret;
1135}
1136
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001137/*
Nick Piggin3c845082007-12-13 15:58:57 -08001138 * ipath_file_vma_fault - handle a VMA page fault.
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001139 */
Nick Piggin3c845082007-12-13 15:58:57 -08001140static int ipath_file_vma_fault(struct vm_area_struct *vma,
1141 struct vm_fault *vmf)
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001142{
Nick Piggin3c845082007-12-13 15:58:57 -08001143 struct page *page;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001144
Nick Piggin3c845082007-12-13 15:58:57 -08001145 page = vmalloc_to_page((void *)(vmf->pgoff << PAGE_SHIFT));
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001146 if (!page)
Nick Piggin3c845082007-12-13 15:58:57 -08001147 return VM_FAULT_SIGBUS;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001148 get_page(page);
Nick Piggin3c845082007-12-13 15:58:57 -08001149 vmf->page = page;
1150
1151 return 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001152}
1153
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001154static const struct vm_operations_struct ipath_file_vm_ops = {
Nick Piggin3c845082007-12-13 15:58:57 -08001155 .fault = ipath_file_vma_fault,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001156};
1157
1158static int mmap_kvaddr(struct vm_area_struct *vma, u64 pgaddr,
1159 struct ipath_portdata *pd, unsigned subport)
1160{
1161 unsigned long len;
1162 struct ipath_devdata *dd;
1163 void *addr;
1164 size_t size;
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001165 int ret = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001166
1167 /* If the port is not shared, all addresses should be physical */
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001168 if (!pd->port_subport_cnt)
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001169 goto bail;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001170
1171 dd = pd->port_dd;
1172 size = pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size;
1173
1174 /*
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001175 * Each process has all the subport uregbase, rcvhdrq, and
1176 * rcvegrbufs mmapped - as an array for all the processes,
1177 * and also separately for this process.
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001178 */
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001179 if (pgaddr == cvt_kvaddr(pd->subport_uregbase)) {
1180 addr = pd->subport_uregbase;
1181 size = PAGE_SIZE * pd->port_subport_cnt;
1182 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base)) {
1183 addr = pd->subport_rcvhdr_base;
1184 size = pd->port_rcvhdrq_size * pd->port_subport_cnt;
1185 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf)) {
1186 addr = pd->subport_rcvegrbuf;
1187 size *= pd->port_subport_cnt;
1188 } else if (pgaddr == cvt_kvaddr(pd->subport_uregbase +
1189 PAGE_SIZE * subport)) {
1190 addr = pd->subport_uregbase + PAGE_SIZE * subport;
1191 size = PAGE_SIZE;
1192 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvhdr_base +
1193 pd->port_rcvhdrq_size * subport)) {
1194 addr = pd->subport_rcvhdr_base +
1195 pd->port_rcvhdrq_size * subport;
1196 size = pd->port_rcvhdrq_size;
1197 } else if (pgaddr == cvt_kvaddr(pd->subport_rcvegrbuf +
1198 size * subport)) {
1199 addr = pd->subport_rcvegrbuf + size * subport;
1200 /* rcvegrbufs are read-only on the slave */
1201 if (vma->vm_flags & VM_WRITE) {
1202 dev_info(&dd->pcidev->dev,
1203 "Can't map eager buffers as "
1204 "writable (flags=%lx)\n", vma->vm_flags);
1205 ret = -EPERM;
1206 goto bail;
1207 }
1208 /*
1209 * Don't allow permission to later change to writeable
1210 * with mprotect.
1211 */
1212 vma->vm_flags &= ~VM_MAYWRITE;
1213 } else {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001214 goto bail;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001215 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001216 len = vma->vm_end - vma->vm_start;
1217 if (len > size) {
1218 ipath_cdbg(MM, "FAIL: reqlen %lx > %zx\n", len, size);
1219 ret = -EINVAL;
1220 goto bail;
1221 }
1222
1223 vma->vm_pgoff = (unsigned long) addr >> PAGE_SHIFT;
1224 vma->vm_ops = &ipath_file_vm_ops;
1225 vma->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001226 ret = 1;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001227
1228bail:
1229 return ret;
1230}
1231
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001232/**
1233 * ipath_mmap - mmap various structures into user space
1234 * @fp: the file pointer
1235 * @vma: the VM area
1236 *
1237 * We use this to have a shared buffer between the kernel and the user code
1238 * for the rcvhdr queue, egr buffers, and the per-port user regs and pio
1239 * buffers in the chip. We have the open and close entries so we can bump
1240 * the ref count and keep the driver from being unloaded while still mapped.
1241 */
1242static int ipath_mmap(struct file *fp, struct vm_area_struct *vma)
1243{
1244 struct ipath_portdata *pd;
1245 struct ipath_devdata *dd;
1246 u64 pgaddr, ureg;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001247 unsigned piobufs, piocnt;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001248 int ret;
1249
1250 pd = port_fp(fp);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001251 if (!pd) {
1252 ret = -EINVAL;
1253 goto bail;
1254 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001255 dd = pd->port_dd;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001256
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001257 /*
1258 * This is the ipath_do_user_init() code, mapping the shared buffers
1259 * into the user process. The address referred to by vm_pgoff is the
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001260 * file offset passed via mmap(). For shared ports, this is the
1261 * kernel vmalloc() address of the pages to share with the master.
1262 * For non-shared or master ports, this is a physical address.
1263 * We only do one mmap for each space mapped.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001264 */
1265 pgaddr = vma->vm_pgoff << PAGE_SHIFT;
1266
1267 /*
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001268 * Check for 0 in case one of the allocations failed, but user
1269 * called mmap anyway.
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001270 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001271 if (!pgaddr) {
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001272 ret = -EINVAL;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001273 goto bail;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001274 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001275
1276 ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u:%u\n",
1277 (unsigned long long) pgaddr, vma->vm_start,
1278 vma->vm_end - vma->vm_start, dd->ipath_unit,
1279 pd->port_port, subport_fp(fp));
1280
1281 /*
1282 * Physical addresses must fit in 40 bits for our hardware.
1283 * Check for kernel virtual addresses first, anything else must
1284 * match a HW or memory address.
1285 */
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001286 ret = mmap_kvaddr(vma, pgaddr, pd, subport_fp(fp));
1287 if (ret) {
1288 if (ret > 0)
1289 ret = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001290 goto bail;
1291 }
1292
Ralph Campbella18e26a2008-01-06 21:02:34 -08001293 ureg = dd->ipath_uregbase + dd->ipath_ureg_align * pd->port_port;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001294 if (!pd->port_subport_cnt) {
1295 /* port is not shared */
Dave Olsone2ab41c2008-05-07 11:00:15 -07001296 piocnt = pd->port_piocnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001297 piobufs = pd->port_piobufs;
1298 } else if (!subport_fp(fp)) {
1299 /* caller is the master */
Dave Olsone2ab41c2008-05-07 11:00:15 -07001300 piocnt = (pd->port_piocnt / pd->port_subport_cnt) +
1301 (pd->port_piocnt % pd->port_subport_cnt);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001302 piobufs = pd->port_piobufs +
Dave Olsone2ab41c2008-05-07 11:00:15 -07001303 dd->ipath_palign * (pd->port_piocnt - piocnt);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001304 } else {
1305 unsigned slave = subport_fp(fp) - 1;
1306
1307 /* caller is a slave */
Dave Olsone2ab41c2008-05-07 11:00:15 -07001308 piocnt = pd->port_piocnt / pd->port_subport_cnt;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001309 piobufs = pd->port_piobufs + dd->ipath_palign * piocnt * slave;
1310 }
1311
1312 if (pgaddr == ureg)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001313 ret = mmap_ureg(vma, dd, ureg);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001314 else if (pgaddr == piobufs)
1315 ret = mmap_piobufs(vma, dd, pd, piobufs, piocnt);
1316 else if (pgaddr == dd->ipath_pioavailregs_phys)
1317 /* in-memory copy of pioavail registers */
1318 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001319 (void *) dd->ipath_pioavailregs_dma,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001320 "pioavail registers");
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001321 else if (pgaddr == pd->port_rcvegr_phys)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001322 ret = mmap_rcvegrbufs(vma, pd);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001323 else if (pgaddr == (u64) pd->port_rcvhdrq_phys)
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001324 /*
Bryan O'Sullivan525d0ca2006-08-25 11:24:39 -07001325 * The rcvhdrq itself; readonly except on HT (so have
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001326 * to allow writable mapping), multiple pages, contiguous
1327 * from an i/o perspective.
1328 */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001329 ret = ipath_mmap_mem(vma, pd, pd->port_rcvhdrq_size, 1,
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001330 pd->port_rcvhdrq,
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001331 "rcvhdrq");
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001332 else if (pgaddr == (u64) pd->port_rcvhdrqtailaddr_phys)
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001333 /* in-memory copy of rcvhdrq tail register */
1334 ret = ipath_mmap_mem(vma, pd, PAGE_SIZE, 0,
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07001335 pd->port_rcvhdrtail_kvaddr,
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001336 "rcvhdrq tail");
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001337 else
1338 ret = -EINVAL;
1339
1340 vma->vm_private_data = NULL;
1341
1342 if (ret < 0)
1343 dev_info(&dd->pcidev->dev,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001344 "Failure %d on off %llx len %lx\n",
1345 -ret, (unsigned long long)pgaddr,
1346 vma->vm_end - vma->vm_start);
1347bail:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001348 return ret;
1349}
1350
Arthur Jones70c51da2007-09-14 12:22:49 -07001351static unsigned ipath_poll_hdrqfull(struct ipath_portdata *pd)
1352{
1353 unsigned pollflag = 0;
1354
1355 if ((pd->poll_type & IPATH_POLL_TYPE_OVERFLOW) &&
1356 pd->port_hdrqfull != pd->port_hdrqfull_poll) {
1357 pollflag |= POLLIN | POLLRDNORM;
1358 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1359 }
1360
1361 return pollflag;
1362}
1363
Robert Walshf2d04232007-06-18 14:24:49 -07001364static unsigned int ipath_poll_urgent(struct ipath_portdata *pd,
1365 struct file *fp,
1366 struct poll_table_struct *pt)
1367{
1368 unsigned pollflag = 0;
1369 struct ipath_devdata *dd;
1370
1371 dd = pd->port_dd;
1372
Arthur Jones70c51da2007-09-14 12:22:49 -07001373 /* variable access in ipath_poll_hdrqfull() needs this */
1374 rmb();
1375 pollflag = ipath_poll_hdrqfull(pd);
Robert Walshf2d04232007-06-18 14:24:49 -07001376
Arthur Jones70c51da2007-09-14 12:22:49 -07001377 if (pd->port_urgent != pd->port_urgent_poll) {
Robert Walshf2d04232007-06-18 14:24:49 -07001378 pollflag |= POLLIN | POLLRDNORM;
Arthur Jones70c51da2007-09-14 12:22:49 -07001379 pd->port_urgent_poll = pd->port_urgent;
Robert Walshf2d04232007-06-18 14:24:49 -07001380 }
1381
1382 if (!pollflag) {
Arthur Jones70c51da2007-09-14 12:22:49 -07001383 /* this saves a spin_lock/unlock in interrupt handler... */
Robert Walshf2d04232007-06-18 14:24:49 -07001384 set_bit(IPATH_PORT_WAITING_URG, &pd->port_flag);
Arthur Jones70c51da2007-09-14 12:22:49 -07001385 /* flush waiting flag so don't miss an event... */
1386 wmb();
Robert Walshf2d04232007-06-18 14:24:49 -07001387 poll_wait(fp, &pd->port_wait, pt);
1388 }
1389
1390 return pollflag;
1391}
1392
1393static unsigned int ipath_poll_next(struct ipath_portdata *pd,
1394 struct file *fp,
1395 struct poll_table_struct *pt)
1396{
Arthur Jones70c51da2007-09-14 12:22:49 -07001397 u32 head;
1398 u32 tail;
Robert Walshf2d04232007-06-18 14:24:49 -07001399 unsigned pollflag = 0;
1400 struct ipath_devdata *dd;
1401
1402 dd = pd->port_dd;
1403
Arthur Jones70c51da2007-09-14 12:22:49 -07001404 /* variable access in ipath_poll_hdrqfull() needs this */
1405 rmb();
1406 pollflag = ipath_poll_hdrqfull(pd);
1407
Robert Walshf2d04232007-06-18 14:24:49 -07001408 head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port);
Ralph Campbellc59a80a2007-12-20 02:43:23 -08001409 if (pd->port_rcvhdrtail_kvaddr)
1410 tail = ipath_get_rcvhdrtail(pd);
1411 else
1412 tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port);
Robert Walshf2d04232007-06-18 14:24:49 -07001413
Arthur Jones70c51da2007-09-14 12:22:49 -07001414 if (head != tail)
Robert Walshf2d04232007-06-18 14:24:49 -07001415 pollflag |= POLLIN | POLLRDNORM;
Arthur Jones70c51da2007-09-14 12:22:49 -07001416 else {
1417 /* this saves a spin_lock/unlock in interrupt handler */
Robert Walshf2d04232007-06-18 14:24:49 -07001418 set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag);
Arthur Jones70c51da2007-09-14 12:22:49 -07001419 /* flush waiting flag so we don't miss an event */
1420 wmb();
Robert Walshf2d04232007-06-18 14:24:49 -07001421
Dave Olsond8274862007-12-21 01:50:59 -08001422 set_bit(pd->port_port + dd->ipath_r_intravail_shift,
Robert Walshf2d04232007-06-18 14:24:49 -07001423 &dd->ipath_rcvctrl);
1424
1425 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1426 dd->ipath_rcvctrl);
1427
1428 if (dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */
1429 ipath_write_ureg(dd, ur_rcvhdrhead,
1430 dd->ipath_rhdrhead_intr_off | head,
1431 pd->port_port);
1432
1433 poll_wait(fp, &pd->port_wait, pt);
1434 }
1435
1436 return pollflag;
1437}
1438
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001439static unsigned int ipath_poll(struct file *fp,
1440 struct poll_table_struct *pt)
1441{
1442 struct ipath_portdata *pd;
Robert Walshf2d04232007-06-18 14:24:49 -07001443 unsigned pollflag;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001444
1445 pd = port_fp(fp);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001446 if (!pd)
Robert Walshf2d04232007-06-18 14:24:49 -07001447 pollflag = 0;
1448 else if (pd->poll_type & IPATH_POLL_TYPE_URGENT)
1449 pollflag = ipath_poll_urgent(pd, fp, pt);
1450 else
1451 pollflag = ipath_poll_next(pd, fp, pt);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001452
Bryan O'Sullivane35d7102006-08-25 11:24:46 -07001453 return pollflag;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001454}
1455
Mark Debbage0df62912007-06-18 14:24:45 -07001456static int ipath_supports_subports(int user_swmajor, int user_swminor)
1457{
1458 /* no subport implementation prior to software version 1.3 */
1459 return (user_swmajor > 1) || (user_swminor >= 3);
1460}
1461
1462static int ipath_compatible_subports(int user_swmajor, int user_swminor)
1463{
1464 /* this code is written long-hand for clarity */
1465 if (IPATH_USER_SWMAJOR != user_swmajor) {
1466 /* no promise of compatibility if major mismatch */
1467 return 0;
1468 }
1469 if (IPATH_USER_SWMAJOR == 1) {
1470 switch (IPATH_USER_SWMINOR) {
1471 case 0:
1472 case 1:
1473 case 2:
1474 /* no subport implementation so cannot be compatible */
1475 return 0;
1476 case 3:
1477 /* 3 is only compatible with itself */
1478 return user_swminor == 3;
1479 default:
1480 /* >= 4 are compatible (or are expected to be) */
1481 return user_swminor >= 4;
1482 }
1483 }
1484 /* make no promises yet for future major versions */
1485 return 0;
1486}
1487
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001488static int init_subports(struct ipath_devdata *dd,
1489 struct ipath_portdata *pd,
1490 const struct ipath_user_info *uinfo)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001491{
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001492 int ret = 0;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001493 unsigned num_subports;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001494 size_t size;
1495
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001496 /*
Mark Debbagebacf4012007-06-18 14:24:46 -07001497 * If the user is requesting zero subports,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001498 * skip the subport allocation.
1499 */
Mark Debbagebacf4012007-06-18 14:24:46 -07001500 if (uinfo->spu_subport_cnt <= 0)
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001501 goto bail;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001502
Mark Debbage0df62912007-06-18 14:24:45 -07001503 /* Self-consistency check for ipath_compatible_subports() */
1504 if (ipath_supports_subports(IPATH_USER_SWMAJOR, IPATH_USER_SWMINOR) &&
1505 !ipath_compatible_subports(IPATH_USER_SWMAJOR,
1506 IPATH_USER_SWMINOR)) {
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001507 dev_info(&dd->pcidev->dev,
Mark Debbage0df62912007-06-18 14:24:45 -07001508 "Inconsistent ipath_compatible_subports()\n");
1509 goto bail;
1510 }
1511
1512 /* Check for subport compatibility */
1513 if (!ipath_compatible_subports(uinfo->spu_userversion >> 16,
1514 uinfo->spu_userversion & 0xffff)) {
1515 dev_info(&dd->pcidev->dev,
1516 "Mismatched user version (%d.%d) and driver "
1517 "version (%d.%d) while port sharing. Ensure "
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001518 "that driver and library are from the same "
1519 "release.\n",
Mark Debbage0df62912007-06-18 14:24:45 -07001520 (int) (uinfo->spu_userversion >> 16),
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001521 (int) (uinfo->spu_userversion & 0xffff),
Mark Debbage0df62912007-06-18 14:24:45 -07001522 IPATH_USER_SWMAJOR,
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001523 IPATH_USER_SWMINOR);
1524 goto bail;
1525 }
Ralph Campbell0a5a83c2007-03-15 14:44:58 -07001526 if (uinfo->spu_subport_cnt > INFINIPATH_MAX_SUBPORT) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001527 ret = -EINVAL;
1528 goto bail;
1529 }
1530
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001531 num_subports = uinfo->spu_subport_cnt;
1532 pd->subport_uregbase = vmalloc(PAGE_SIZE * num_subports);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001533 if (!pd->subport_uregbase) {
1534 ret = -ENOMEM;
1535 goto bail;
1536 }
1537 /* Note: pd->port_rcvhdrq_size isn't initialized yet. */
1538 size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001539 sizeof(u32), PAGE_SIZE) * num_subports;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001540 pd->subport_rcvhdr_base = vmalloc(size);
1541 if (!pd->subport_rcvhdr_base) {
1542 ret = -ENOMEM;
1543 goto bail_ureg;
1544 }
1545
1546 pd->subport_rcvegrbuf = vmalloc(pd->port_rcvegrbuf_chunks *
1547 pd->port_rcvegrbuf_size *
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001548 num_subports);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001549 if (!pd->subport_rcvegrbuf) {
1550 ret = -ENOMEM;
1551 goto bail_rhdr;
1552 }
1553
1554 pd->port_subport_cnt = uinfo->spu_subport_cnt;
1555 pd->port_subport_id = uinfo->spu_subport_id;
1556 pd->active_slaves = 1;
Ralph Campbell947d7612007-03-15 14:44:48 -07001557 set_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001558 memset(pd->subport_uregbase, 0, PAGE_SIZE * num_subports);
1559 memset(pd->subport_rcvhdr_base, 0, size);
1560 memset(pd->subport_rcvegrbuf, 0, pd->port_rcvegrbuf_chunks *
1561 pd->port_rcvegrbuf_size *
1562 num_subports);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001563 goto bail;
1564
1565bail_rhdr:
1566 vfree(pd->subport_rcvhdr_base);
1567bail_ureg:
1568 vfree(pd->subport_uregbase);
1569 pd->subport_uregbase = NULL;
1570bail:
1571 return ret;
1572}
1573
1574static int try_alloc_port(struct ipath_devdata *dd, int port,
1575 struct file *fp,
1576 const struct ipath_user_info *uinfo)
1577{
1578 struct ipath_portdata *pd;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001579 int ret;
1580
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001581 if (!(pd = dd->ipath_pd[port])) {
1582 void *ptmp;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001583
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001584 pd = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001585
1586 /*
1587 * Allocate memory for use in ipath_tid_update() just once
1588 * at open, not per call. Reduces cost of expected send
1589 * setup.
1590 */
1591 ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) +
1592 dd->ipath_rcvtidcnt * sizeof(struct page **),
1593 GFP_KERNEL);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001594 if (!pd || !ptmp) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001595 ipath_dev_err(dd, "Unable to allocate portdata "
1596 "memory, failing open\n");
1597 ret = -ENOMEM;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001598 kfree(pd);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001599 kfree(ptmp);
1600 goto bail;
1601 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001602 dd->ipath_pd[port] = pd;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001603 dd->ipath_pd[port]->port_port = port;
1604 dd->ipath_pd[port]->port_dd = dd;
1605 dd->ipath_pd[port]->port_tid_pg_list = ptmp;
1606 init_waitqueue_head(&dd->ipath_pd[port]->port_wait);
1607 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001608 if (!pd->port_cnt) {
1609 pd->userversion = uinfo->spu_userversion;
1610 init_user_egr_sizes(pd);
1611 if ((ret = init_subports(dd, pd, uinfo)) != 0)
1612 goto bail;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001613 ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n",
1614 current->comm, current->pid, dd->ipath_unit,
1615 port);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001616 pd->port_cnt = 1;
1617 port_fp(fp) = pd;
Pavel Emelyanov40d97692008-05-13 11:45:32 -07001618 pd->port_pid = get_pid(task_pid(current));
Roel Kluin286b63d2009-09-05 20:23:21 -07001619 strlcpy(pd->port_comm, current->comm, sizeof(pd->port_comm));
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001620 ipath_stats.sps_ports++;
1621 ret = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001622 } else
1623 ret = -EBUSY;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001624
1625bail:
1626 return ret;
1627}
1628
1629static inline int usable(struct ipath_devdata *dd)
1630{
1631 return dd &&
1632 (dd->ipath_flags & IPATH_PRESENT) &&
1633 dd->ipath_kregbase &&
1634 dd->ipath_lid &&
1635 !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED
1636 | IPATH_LINKUNK));
1637}
1638
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001639static int find_free_port(int unit, struct file *fp,
1640 const struct ipath_user_info *uinfo)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001641{
1642 struct ipath_devdata *dd = ipath_lookup(unit);
1643 int ret, i;
1644
1645 if (!dd) {
1646 ret = -ENODEV;
1647 goto bail;
1648 }
1649
1650 if (!usable(dd)) {
1651 ret = -ENETDOWN;
1652 goto bail;
1653 }
1654
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001655 for (i = 1; i < dd->ipath_cfgports; i++) {
1656 ret = try_alloc_port(dd, i, fp, uinfo);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001657 if (ret != -EBUSY)
1658 goto bail;
1659 }
1660 ret = -EBUSY;
1661
1662bail:
1663 return ret;
1664}
1665
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001666static int find_best_unit(struct file *fp,
1667 const struct ipath_user_info *uinfo)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001668{
1669 int ret = 0, i, prefunit = -1, devmax;
1670 int maxofallports, npresent, nup;
1671 int ndev;
1672
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001673 devmax = ipath_count_units(&npresent, &nup, &maxofallports);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001674
1675 /*
1676 * This code is present to allow a knowledgeable person to
1677 * specify the layout of processes to processors before opening
1678 * this driver, and then we'll assign the process to the "closest"
Bryan O'Sullivan525d0ca2006-08-25 11:24:39 -07001679 * InfiniPath chip to that processor (we assume reasonable connectivity,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001680 * for now). This code assumes that if affinity has been set
1681 * before this point, that at most one cpu is set; for now this
Rusty Russellcbe31f02008-12-30 09:05:18 +10301682 * is reasonable. I check for both cpumask_empty() and cpumask_full(),
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001683 * in case some kernel variant sets none of the bits when no
1684 * affinity is set. 2.6.11 and 12 kernels have all present
1685 * cpus set. Some day we'll have to fix it up further to handle
Bryan O'Sullivan525d0ca2006-08-25 11:24:39 -07001686 * a cpu subset. This algorithm fails for two HT chips connected
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001687 * in tunnel fashion. Eventually this needs real topology
1688 * information. There may be some issues with dual core numbering
1689 * as well. This needs more work prior to release.
1690 */
Rusty Russellcbe31f02008-12-30 09:05:18 +10301691 if (!cpumask_empty(&current->cpus_allowed) &&
1692 !cpumask_full(&current->cpus_allowed)) {
Bryan O'Sullivanf0810da2007-03-15 14:45:13 -07001693 int ncpus = num_online_cpus(), curcpu = -1, nset = 0;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001694 for (i = 0; i < ncpus; i++)
Rusty Russellcbe31f02008-12-30 09:05:18 +10301695 if (cpumask_test_cpu(i, &current->cpus_allowed)) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001696 ipath_cdbg(PROC, "%s[%u] affinity set for "
Bryan O'Sullivanf0810da2007-03-15 14:45:13 -07001697 "cpu %d/%d\n", current->comm,
1698 current->pid, i, ncpus);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001699 curcpu = i;
Bryan O'Sullivanf0810da2007-03-15 14:45:13 -07001700 nset++;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001701 }
Bryan O'Sullivanf0810da2007-03-15 14:45:13 -07001702 if (curcpu != -1 && nset != ncpus) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001703 if (npresent) {
1704 prefunit = curcpu / (ncpus / npresent);
Mark Debbagec7e29ff2007-03-15 14:44:59 -07001705 ipath_cdbg(PROC,"%s[%u] %d chips, %d cpus, "
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001706 "%d cpus/chip, select unit %d\n",
1707 current->comm, current->pid,
1708 npresent, ncpus, ncpus / npresent,
1709 prefunit);
1710 }
1711 }
1712 }
1713
1714 /*
1715 * user ports start at 1, kernel port is 0
1716 * For now, we do round-robin access across all chips
1717 */
1718
1719 if (prefunit != -1)
1720 devmax = prefunit + 1;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001721recheck:
1722 for (i = 1; i < maxofallports; i++) {
1723 for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax;
1724 ndev++) {
1725 struct ipath_devdata *dd = ipath_lookup(ndev);
1726
1727 if (!usable(dd))
1728 continue; /* can't use this unit */
1729 if (i >= dd->ipath_cfgports)
1730 /*
1731 * Maxed out on users of this unit. Try
1732 * next.
1733 */
1734 continue;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001735 ret = try_alloc_port(dd, i, fp, uinfo);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001736 if (!ret)
1737 goto done;
1738 }
1739 }
1740
1741 if (npresent) {
1742 if (nup == 0) {
1743 ret = -ENETDOWN;
1744 ipath_dbg("No ports available (none initialized "
1745 "and ready)\n");
1746 } else {
1747 if (prefunit > 0) {
1748 /* if started above 0, retry from 0 */
1749 ipath_cdbg(PROC,
1750 "%s[%u] no ports on prefunit "
1751 "%d, clear and re-check\n",
1752 current->comm, current->pid,
1753 prefunit);
1754 devmax = ipath_count_units(NULL, NULL,
1755 NULL);
1756 prefunit = -1;
1757 goto recheck;
1758 }
1759 ret = -EBUSY;
1760 ipath_dbg("No ports available\n");
1761 }
1762 } else {
1763 ret = -ENXIO;
1764 ipath_dbg("No boards found\n");
1765 }
1766
1767done:
1768 return ret;
1769}
1770
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001771static int find_shared_port(struct file *fp,
1772 const struct ipath_user_info *uinfo)
1773{
1774 int devmax, ndev, i;
1775 int ret = 0;
1776
1777 devmax = ipath_count_units(NULL, NULL, NULL);
1778
1779 for (ndev = 0; ndev < devmax; ndev++) {
1780 struct ipath_devdata *dd = ipath_lookup(ndev);
1781
Dave Olson5d1ce032008-04-16 21:01:12 -07001782 if (!usable(dd))
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001783 continue;
1784 for (i = 1; i < dd->ipath_cfgports; i++) {
1785 struct ipath_portdata *pd = dd->ipath_pd[i];
1786
1787 /* Skip ports which are not yet open */
1788 if (!pd || !pd->port_cnt)
1789 continue;
1790 /* Skip port if it doesn't match the requested one */
1791 if (pd->port_subport_id != uinfo->spu_subport_id)
1792 continue;
1793 /* Verify the sharing process matches the master */
1794 if (pd->port_subport_cnt != uinfo->spu_subport_cnt ||
1795 pd->userversion != uinfo->spu_userversion ||
1796 pd->port_cnt >= pd->port_subport_cnt) {
1797 ret = -EINVAL;
1798 goto done;
1799 }
1800 port_fp(fp) = pd;
1801 subport_fp(fp) = pd->port_cnt++;
Pavel Emelyanov40d97692008-05-13 11:45:32 -07001802 pd->port_subpid[subport_fp(fp)] =
1803 get_pid(task_pid(current));
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001804 tidcursor_fp(fp) = 0;
1805 pd->active_slaves |= 1 << subport_fp(fp);
1806 ipath_cdbg(PROC,
1807 "%s[%u] %u sharing %s[%u] unit:port %u:%u\n",
1808 current->comm, current->pid,
1809 subport_fp(fp),
Pavel Emelyanov40d97692008-05-13 11:45:32 -07001810 pd->port_comm, pid_nr(pd->port_pid),
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001811 dd->ipath_unit, pd->port_port);
1812 ret = 1;
1813 goto done;
1814 }
1815 }
1816
1817done:
1818 return ret;
1819}
1820
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001821static int ipath_open(struct inode *in, struct file *fp)
1822{
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07001823 /* The real work is performed later in ipath_assign_port() */
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001824 fp->private_data = kzalloc(sizeof(struct ipath_filedata), GFP_KERNEL);
1825 return fp->private_data ? 0 : -ENOMEM;
1826}
1827
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07001828/* Get port early, so can set affinity prior to memory allocation */
1829static int ipath_assign_port(struct file *fp,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001830 const struct ipath_user_info *uinfo)
1831{
1832 int ret;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001833 int i_minor;
Mark Debbage0df62912007-06-18 14:24:45 -07001834 unsigned swmajor, swminor;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001835
1836 /* Check to be sure we haven't already initialized this file */
1837 if (port_fp(fp)) {
1838 ret = -EINVAL;
1839 goto done;
1840 }
1841
1842 /* for now, if major version is different, bail */
Mark Debbage0df62912007-06-18 14:24:45 -07001843 swmajor = uinfo->spu_userversion >> 16;
1844 if (swmajor != IPATH_USER_SWMAJOR) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001845 ipath_dbg("User major version %d not same as driver "
1846 "major %d\n", uinfo->spu_userversion >> 16,
1847 IPATH_USER_SWMAJOR);
1848 ret = -ENODEV;
1849 goto done;
1850 }
1851
1852 swminor = uinfo->spu_userversion & 0xffff;
1853 if (swminor != IPATH_USER_SWMINOR)
1854 ipath_dbg("User minor version %d not same as driver "
1855 "minor %d\n", swminor, IPATH_USER_SWMINOR);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001856
1857 mutex_lock(&ipath_mutex);
1858
Mark Debbage0df62912007-06-18 14:24:45 -07001859 if (ipath_compatible_subports(swmajor, swminor) &&
1860 uinfo->spu_subport_cnt &&
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001861 (ret = find_shared_port(fp, uinfo))) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001862 if (ret > 0)
1863 ret = 0;
Dave Olson124b4dc2008-04-16 21:09:32 -07001864 goto done_chk_sdma;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001865 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001866
Josef Sipek1cfd6e62006-12-08 02:37:10 -08001867 i_minor = iminor(fp->f_path.dentry->d_inode) - IPATH_USER_MINOR_BASE;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001868 ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n",
Josef Sipek1cfd6e62006-12-08 02:37:10 -08001869 (long)fp->f_path.dentry->d_inode->i_rdev, i_minor);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001870
1871 if (i_minor)
1872 ret = find_free_port(i_minor - 1, fp, uinfo);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001873 else
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001874 ret = find_best_unit(fp, uinfo);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001875
Dave Olson124b4dc2008-04-16 21:09:32 -07001876done_chk_sdma:
1877 if (!ret) {
1878 struct ipath_filedata *fd = fp->private_data;
1879 const struct ipath_portdata *pd = fd->pd;
1880 const struct ipath_devdata *dd = pd->port_dd;
1881
1882 fd->pq = ipath_user_sdma_queue_create(&dd->pcidev->dev,
1883 dd->ipath_unit,
1884 pd->port_port,
1885 fd->subport);
1886
1887 if (!fd->pq)
1888 ret = -ENOMEM;
1889 }
1890
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08001891 mutex_unlock(&ipath_mutex);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001892
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07001893done:
1894 return ret;
1895}
1896
1897
1898static int ipath_do_user_init(struct file *fp,
1899 const struct ipath_user_info *uinfo)
1900{
1901 int ret;
Ralph Campbell947d7612007-03-15 14:44:48 -07001902 struct ipath_portdata *pd = port_fp(fp);
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07001903 struct ipath_devdata *dd;
1904 u32 head32;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001905
Ralph Campbell947d7612007-03-15 14:44:48 -07001906 /* Subports don't need to initialize anything since master did it. */
1907 if (subport_fp(fp)) {
1908 ret = wait_event_interruptible(pd->port_wait,
1909 !test_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag));
1910 goto done;
1911 }
1912
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001913 dd = pd->port_dd;
1914
1915 if (uinfo->spu_rcvhdrsize) {
1916 ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize);
1917 if (ret)
1918 goto done;
1919 }
1920
1921 /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */
1922
Dave Olsone2ab41c2008-05-07 11:00:15 -07001923 /* some ports may get extra buffers, calculate that here */
1924 if (pd->port_port <= dd->ipath_ports_extrabuf)
1925 pd->port_piocnt = dd->ipath_pbufsport + 1;
1926 else
1927 pd->port_piocnt = dd->ipath_pbufsport;
1928
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001929 /* for right now, kernel piobufs are at end, so port 1 is at 0 */
Dave Olsone2ab41c2008-05-07 11:00:15 -07001930 if (pd->port_port <= dd->ipath_ports_extrabuf)
1931 pd->port_pio_base = (dd->ipath_pbufsport + 1)
1932 * (pd->port_port - 1);
1933 else
1934 pd->port_pio_base = dd->ipath_ports_extrabuf +
1935 dd->ipath_pbufsport * (pd->port_port - 1);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001936 pd->port_piobufs = dd->ipath_piobufbase +
Dave Olsone2ab41c2008-05-07 11:00:15 -07001937 pd->port_pio_base * dd->ipath_palign;
1938 ipath_cdbg(VERBOSE, "piobuf base for port %u is 0x%x, piocnt %u,"
1939 " first pio %u\n", pd->port_port, pd->port_piobufs,
1940 pd->port_piocnt, pd->port_pio_base);
1941 ipath_chg_pioavailkernel(dd, pd->port_pio_base, pd->port_piocnt, 0);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001942
1943 /*
1944 * Now allocate the rcvhdr Q and eager TIDs; skip the TID
1945 * array for time being. If pd->port_port > chip-supported,
1946 * we need to do extra stuff here to handle by handling overflow
1947 * through port 0, someday
1948 */
1949 ret = ipath_create_rcvhdrq(dd, pd);
1950 if (!ret)
1951 ret = ipath_create_user_egr(pd);
1952 if (ret)
1953 goto done;
1954
1955 /*
1956 * set the eager head register for this port to the current values
1957 * of the tail pointers, since we don't know if they were
1958 * updated on last use of the port.
1959 */
1960 head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port);
1961 ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port);
Dave Olson755807a2007-12-06 00:28:02 -08001962 pd->port_lastrcvhdrqtail = -1;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001963 ipath_cdbg(VERBOSE, "Wrote port%d egrhead %x from tail regs\n",
1964 pd->port_port, head32);
1965 pd->port_tidcursor = 0; /* start at beginning after open */
Arthur Jones70c51da2007-09-14 12:22:49 -07001966
1967 /* initialize poll variables... */
1968 pd->port_urgent = 0;
1969 pd->port_urgent_poll = 0;
1970 pd->port_hdrqfull_poll = pd->port_hdrqfull;
1971
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001972 /*
Ralph Campbell9355fb62008-04-16 21:09:29 -07001973 * Now enable the port for receive.
1974 * For chips that are set to DMA the tail register to memory
1975 * when they change (and when the update bit transitions from
1976 * 0 to 1. So for those chips, we turn it off and then back on.
1977 * This will (very briefly) affect any other open ports, but the
1978 * duration is very short, and therefore isn't an issue. We
1979 * explictly set the in-memory tail copy to 0 beforehand, so we
1980 * don't have to wait to be sure the DMA update has happened
1981 * (chip resets head/tail to 0 on transition to enable).
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001982 */
Dave Olsond8274862007-12-21 01:50:59 -08001983 set_bit(dd->ipath_r_portenable_shift + pd->port_port,
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001984 &dd->ipath_rcvctrl);
Ralph Campbell9355fb62008-04-16 21:09:29 -07001985 if (!(dd->ipath_flags & IPATH_NODMA_RTAIL)) {
1986 if (pd->port_rcvhdrtail_kvaddr)
1987 ipath_clear_rcvhdrtail(pd);
1988 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
Dave Olsond8274862007-12-21 01:50:59 -08001989 dd->ipath_rcvctrl &
1990 ~(1ULL << dd->ipath_r_tailupd_shift));
Ralph Campbell9355fb62008-04-16 21:09:29 -07001991 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001992 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1993 dd->ipath_rcvctrl);
Ralph Campbell947d7612007-03-15 14:44:48 -07001994 /* Notify any waiting slaves */
1995 if (pd->port_subport_cnt) {
1996 clear_bit(IPATH_PORT_MASTER_UNINIT, &pd->port_flag);
1997 wake_up(&pd->port_wait);
1998 }
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07001999done:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002000 return ret;
2001}
2002
2003/**
2004 * unlock_exptid - unlock any expected TID entries port still had in use
2005 * @pd: port
2006 *
2007 * We don't actually update the chip here, because we do a bulk update
2008 * below, using ipath_f_clear_tids.
2009 */
2010static void unlock_expected_tids(struct ipath_portdata *pd)
2011{
2012 struct ipath_devdata *dd = pd->port_dd;
2013 int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt;
2014 int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt;
2015
2016 ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n",
2017 pd->port_port);
2018 for (i = port_tidbase; i < maxtid; i++) {
Ralph Campbell9355fb62008-04-16 21:09:29 -07002019 struct page *ps = dd->ipath_pageshadow[i];
2020
2021 if (!ps)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002022 continue;
2023
Ralph Campbell9355fb62008-04-16 21:09:29 -07002024 dd->ipath_pageshadow[i] = NULL;
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07002025 pci_unmap_page(dd->pcidev, dd->ipath_physshadow[i],
2026 PAGE_SIZE, PCI_DMA_FROMDEVICE);
Ralph Campbell9355fb62008-04-16 21:09:29 -07002027 ipath_release_user_pages_on_close(&ps, 1);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002028 cnt++;
2029 ipath_stats.sps_pageunlocks++;
2030 }
2031 if (cnt)
2032 ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n",
2033 pd->port_port, cnt);
2034
2035 if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks)
2036 ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n",
2037 (unsigned long long) ipath_stats.sps_pagelocks,
2038 (unsigned long long)
2039 ipath_stats.sps_pageunlocks);
2040}
2041
2042static int ipath_close(struct inode *in, struct file *fp)
2043{
2044 int ret = 0;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002045 struct ipath_filedata *fd;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002046 struct ipath_portdata *pd;
2047 struct ipath_devdata *dd;
Dave Olson3d089092008-12-05 11:14:38 -08002048 unsigned long flags;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002049 unsigned port;
Dave Olson3d089092008-12-05 11:14:38 -08002050 struct pid *pid;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002051
2052 ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n",
2053 (long)in->i_rdev, fp->private_data);
2054
2055 mutex_lock(&ipath_mutex);
2056
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002057 fd = (struct ipath_filedata *) fp->private_data;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002058 fp->private_data = NULL;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002059 pd = fd->pd;
2060 if (!pd) {
2061 mutex_unlock(&ipath_mutex);
2062 goto bail;
2063 }
Dave Olson124b4dc2008-04-16 21:09:32 -07002064
2065 dd = pd->port_dd;
2066
2067 /* drain user sdma queue */
2068 ipath_user_sdma_queue_drain(dd, fd->pq);
2069 ipath_user_sdma_queue_destroy(fd->pq);
2070
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002071 if (--pd->port_cnt) {
2072 /*
2073 * XXX If the master closes the port before the slave(s),
2074 * revoke the mmap for the eager receive queue so
2075 * the slave(s) don't wait for receive data forever.
2076 */
2077 pd->active_slaves &= ~(1 << fd->subport);
Pavel Emelyanov40d97692008-05-13 11:45:32 -07002078 put_pid(pd->port_subpid[fd->subport]);
2079 pd->port_subpid[fd->subport] = NULL;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002080 mutex_unlock(&ipath_mutex);
2081 goto bail;
2082 }
Dave Olson3d089092008-12-05 11:14:38 -08002083 /* early; no interrupt users after this */
2084 spin_lock_irqsave(&dd->ipath_uctxt_lock, flags);
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002085 port = pd->port_port;
Dave Olson3d089092008-12-05 11:14:38 -08002086 dd->ipath_pd[port] = NULL;
2087 pid = pd->port_pid;
2088 pd->port_pid = NULL;
2089 spin_unlock_irqrestore(&dd->ipath_uctxt_lock, flags);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002090
2091 if (pd->port_rcvwait_to || pd->port_piowait_to
2092 || pd->port_rcvnowait || pd->port_pionowait) {
2093 ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; "
2094 "%u rcv %u, pio already\n",
2095 pd->port_port, pd->port_rcvwait_to,
2096 pd->port_piowait_to, pd->port_rcvnowait,
2097 pd->port_pionowait);
2098 pd->port_rcvwait_to = pd->port_piowait_to =
2099 pd->port_rcvnowait = pd->port_pionowait = 0;
2100 }
2101 if (pd->port_flag) {
Arthur Jonesbb917142008-04-16 21:09:31 -07002102 ipath_cdbg(PROC, "port %u port_flag set: 0x%lx\n",
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002103 pd->port_port, pd->port_flag);
2104 pd->port_flag = 0;
2105 }
2106
2107 if (dd->ipath_kregbase) {
Arthur Jones70c51da2007-09-14 12:22:49 -07002108 /* atomically clear receive enable port and intr avail. */
Dave Olsond8274862007-12-21 01:50:59 -08002109 clear_bit(dd->ipath_r_portenable_shift + port,
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002110 &dd->ipath_rcvctrl);
Dave Olsond8274862007-12-21 01:50:59 -08002111 clear_bit(pd->port_port + dd->ipath_r_intravail_shift,
Arthur Jones70c51da2007-09-14 12:22:49 -07002112 &dd->ipath_rcvctrl);
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002113 ipath_write_kreg( dd, dd->ipath_kregs->kr_rcvctrl,
2114 dd->ipath_rcvctrl);
2115 /* and read back from chip to be sure that nothing
2116 * else is in flight when we do the rest */
2117 (void)ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002118
2119 /* clean up the pkeys for this port user */
2120 ipath_clean_part_key(pd, dd);
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002121 /*
2122 * be paranoid, and never write 0's to these, just use an
2123 * unused part of the port 0 tail page. Of course,
2124 * rcvhdraddr points to a large chunk of memory, so this
2125 * could still trash things, but at least it won't trash
2126 * page 0, and by disabling the port, it should stop "soon",
2127 * even if a packet or two is in already in flight after we
2128 * disabled the port.
2129 */
2130 ipath_write_kreg_port(dd,
2131 dd->ipath_kregs->kr_rcvhdrtailaddr, port,
2132 dd->ipath_dummy_hdrq_phys);
2133 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
2134 pd->port_port, dd->ipath_dummy_hdrq_phys);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002135
Dave Olsone2ab41c2008-05-07 11:00:15 -07002136 ipath_disarm_piobufs(dd, pd->port_pio_base, pd->port_piocnt);
2137 ipath_chg_pioavailkernel(dd, pd->port_pio_base,
2138 pd->port_piocnt, 1);
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002139
Bryan O'Sullivan1fd3b402006-09-28 09:00:13 -07002140 dd->ipath_f_clear_tids(dd, pd->port_port);
2141
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002142 if (dd->ipath_pageshadow)
2143 unlock_expected_tids(pd);
2144 ipath_stats.sps_ports--;
2145 ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n",
Dave Olson3d089092008-12-05 11:14:38 -08002146 pd->port_comm, pid_nr(pid),
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07002147 dd->ipath_unit, port);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002148 }
2149
Dave Olson3d089092008-12-05 11:14:38 -08002150 put_pid(pid);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002151 mutex_unlock(&ipath_mutex);
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07002152 ipath_free_pddata(dd, pd); /* after releasing the mutex */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002153
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002154bail:
2155 kfree(fd);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002156 return ret;
2157}
2158
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002159static int ipath_port_info(struct ipath_portdata *pd, u16 subport,
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002160 struct ipath_port_info __user *uinfo)
2161{
2162 struct ipath_port_info info;
2163 int nup;
2164 int ret;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002165 size_t sz;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002166
2167 (void) ipath_count_units(NULL, &nup, NULL);
2168 info.num_active = nup;
2169 info.unit = pd->port_dd->ipath_unit;
2170 info.port = pd->port_port;
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002171 info.subport = subport;
2172 /* Don't return new fields if old library opened the port. */
Mark Debbage0df62912007-06-18 14:24:45 -07002173 if (ipath_supports_subports(pd->userversion >> 16,
2174 pd->userversion & 0xffff)) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002175 /* Number of user ports available for this device. */
2176 info.num_ports = pd->port_dd->ipath_cfgports - 1;
2177 info.num_subports = pd->port_subport_cnt;
2178 sz = sizeof(info);
2179 } else
2180 sz = sizeof(info) - 2 * sizeof(u16);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002181
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002182 if (copy_to_user(uinfo, &info, sz)) {
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002183 ret = -EFAULT;
2184 goto bail;
2185 }
2186 ret = 0;
2187
2188bail:
2189 return ret;
2190}
2191
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002192static int ipath_get_slave_info(struct ipath_portdata *pd,
2193 void __user *slave_mask_addr)
2194{
2195 int ret = 0;
2196
2197 if (copy_to_user(slave_mask_addr, &pd->active_slaves, sizeof(u32)))
2198 ret = -EFAULT;
2199 return ret;
2200}
2201
Dave Olson124b4dc2008-04-16 21:09:32 -07002202static int ipath_sdma_get_inflight(struct ipath_user_sdma_queue *pq,
2203 u32 __user *inflightp)
2204{
2205 const u32 val = ipath_user_sdma_inflight_counter(pq);
2206
2207 if (put_user(val, inflightp))
2208 return -EFAULT;
2209
2210 return 0;
2211}
2212
2213static int ipath_sdma_get_complete(struct ipath_devdata *dd,
2214 struct ipath_user_sdma_queue *pq,
2215 u32 __user *completep)
2216{
2217 u32 val;
2218 int err;
2219
2220 err = ipath_user_sdma_make_progress(dd, pq);
2221 if (err < 0)
2222 return err;
2223
2224 val = ipath_user_sdma_complete_counter(pq);
2225 if (put_user(val, completep))
2226 return -EFAULT;
2227
2228 return 0;
2229}
2230
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002231static ssize_t ipath_write(struct file *fp, const char __user *data,
2232 size_t count, loff_t *off)
2233{
2234 const struct ipath_cmd __user *ucmd;
2235 struct ipath_portdata *pd;
2236 const void __user *src;
2237 size_t consumed, copy;
2238 struct ipath_cmd cmd;
2239 ssize_t ret = 0;
2240 void *dest;
2241
2242 if (count < sizeof(cmd.type)) {
2243 ret = -EINVAL;
2244 goto bail;
2245 }
2246
2247 ucmd = (const struct ipath_cmd __user *) data;
2248
2249 if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) {
2250 ret = -EFAULT;
2251 goto bail;
2252 }
2253
2254 consumed = sizeof(cmd.type);
2255
2256 switch (cmd.type) {
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07002257 case IPATH_CMD_ASSIGN_PORT:
2258 case __IPATH_CMD_USER_INIT:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002259 case IPATH_CMD_USER_INIT:
2260 copy = sizeof(cmd.cmd.user_info);
2261 dest = &cmd.cmd.user_info;
2262 src = &ucmd->cmd.user_info;
2263 break;
2264 case IPATH_CMD_RECV_CTRL:
2265 copy = sizeof(cmd.cmd.recv_ctrl);
2266 dest = &cmd.cmd.recv_ctrl;
2267 src = &ucmd->cmd.recv_ctrl;
2268 break;
2269 case IPATH_CMD_PORT_INFO:
2270 copy = sizeof(cmd.cmd.port_info);
2271 dest = &cmd.cmd.port_info;
2272 src = &ucmd->cmd.port_info;
2273 break;
2274 case IPATH_CMD_TID_UPDATE:
2275 case IPATH_CMD_TID_FREE:
2276 copy = sizeof(cmd.cmd.tid_info);
2277 dest = &cmd.cmd.tid_info;
2278 src = &ucmd->cmd.tid_info;
2279 break;
2280 case IPATH_CMD_SET_PART_KEY:
2281 copy = sizeof(cmd.cmd.part_key);
2282 dest = &cmd.cmd.part_key;
2283 src = &ucmd->cmd.part_key;
2284 break;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07002285 case __IPATH_CMD_SLAVE_INFO:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002286 copy = sizeof(cmd.cmd.slave_mask_addr);
2287 dest = &cmd.cmd.slave_mask_addr;
2288 src = &ucmd->cmd.slave_mask_addr;
2289 break;
Arthur Jones569b87b2007-03-15 14:45:05 -07002290 case IPATH_CMD_PIOAVAILUPD: // force an update of PIOAvail reg
2291 copy = 0;
2292 src = NULL;
2293 dest = NULL;
2294 break;
Robert Walshf2d04232007-06-18 14:24:49 -07002295 case IPATH_CMD_POLL_TYPE:
2296 copy = sizeof(cmd.cmd.poll_type);
2297 dest = &cmd.cmd.poll_type;
2298 src = &ucmd->cmd.poll_type;
2299 break;
Dave Olson6ac50722007-08-09 03:11:38 -07002300 case IPATH_CMD_ARMLAUNCH_CTRL:
2301 copy = sizeof(cmd.cmd.armlaunch_ctrl);
2302 dest = &cmd.cmd.armlaunch_ctrl;
2303 src = &ucmd->cmd.armlaunch_ctrl;
2304 break;
Dave Olson124b4dc2008-04-16 21:09:32 -07002305 case IPATH_CMD_SDMA_INFLIGHT:
2306 copy = sizeof(cmd.cmd.sdma_inflight);
2307 dest = &cmd.cmd.sdma_inflight;
2308 src = &ucmd->cmd.sdma_inflight;
2309 break;
2310 case IPATH_CMD_SDMA_COMPLETE:
2311 copy = sizeof(cmd.cmd.sdma_complete);
2312 dest = &cmd.cmd.sdma_complete;
2313 src = &ucmd->cmd.sdma_complete;
2314 break;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002315 default:
2316 ret = -EINVAL;
2317 goto bail;
2318 }
2319
Arthur Jones569b87b2007-03-15 14:45:05 -07002320 if (copy) {
2321 if ((count - consumed) < copy) {
2322 ret = -EINVAL;
2323 goto bail;
2324 }
2325
2326 if (copy_from_user(dest, src, copy)) {
2327 ret = -EFAULT;
2328 goto bail;
2329 }
2330
2331 consumed += copy;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002332 }
2333
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002334 pd = port_fp(fp);
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07002335 if (!pd && cmd.type != __IPATH_CMD_USER_INIT &&
2336 cmd.type != IPATH_CMD_ASSIGN_PORT) {
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002337 ret = -EINVAL;
2338 goto bail;
2339 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002340
2341 switch (cmd.type) {
Bryan O'Sullivanc97d27d2006-09-28 09:00:21 -07002342 case IPATH_CMD_ASSIGN_PORT:
2343 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2344 if (ret)
2345 goto bail;
2346 break;
2347 case __IPATH_CMD_USER_INIT:
2348 /* backwards compatibility, get port first */
2349 ret = ipath_assign_port(fp, &cmd.cmd.user_info);
2350 if (ret)
2351 goto bail;
2352 /* and fall through to current version. */
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002353 case IPATH_CMD_USER_INIT:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002354 ret = ipath_do_user_init(fp, &cmd.cmd.user_info);
2355 if (ret)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002356 goto bail;
2357 ret = ipath_get_base_info(
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002358 fp, (void __user *) (unsigned long)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002359 cmd.cmd.user_info.spu_base_info,
2360 cmd.cmd.user_info.spu_base_info_size);
2361 break;
2362 case IPATH_CMD_RECV_CTRL:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002363 ret = ipath_manage_rcvq(pd, subport_fp(fp), cmd.cmd.recv_ctrl);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002364 break;
2365 case IPATH_CMD_PORT_INFO:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002366 ret = ipath_port_info(pd, subport_fp(fp),
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002367 (struct ipath_port_info __user *)
2368 (unsigned long) cmd.cmd.port_info);
2369 break;
2370 case IPATH_CMD_TID_UPDATE:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002371 ret = ipath_tid_update(pd, fp, &cmd.cmd.tid_info);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002372 break;
2373 case IPATH_CMD_TID_FREE:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002374 ret = ipath_tid_free(pd, subport_fp(fp), &cmd.cmd.tid_info);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002375 break;
2376 case IPATH_CMD_SET_PART_KEY:
2377 ret = ipath_set_part_key(pd, cmd.cmd.part_key);
2378 break;
Mark Debbagec7e29ff2007-03-15 14:44:59 -07002379 case __IPATH_CMD_SLAVE_INFO:
Bryan O'Sullivan9929b0f2006-09-28 08:59:59 -07002380 ret = ipath_get_slave_info(pd,
2381 (void __user *) (unsigned long)
2382 cmd.cmd.slave_mask_addr);
2383 break;
Arthur Jones569b87b2007-03-15 14:45:05 -07002384 case IPATH_CMD_PIOAVAILUPD:
Ralph Campbellc4b4d162008-04-16 21:09:26 -07002385 ipath_force_pio_avail_update(pd->port_dd);
Arthur Jones569b87b2007-03-15 14:45:05 -07002386 break;
Robert Walshf2d04232007-06-18 14:24:49 -07002387 case IPATH_CMD_POLL_TYPE:
2388 pd->poll_type = cmd.cmd.poll_type;
2389 break;
Dave Olson6ac50722007-08-09 03:11:38 -07002390 case IPATH_CMD_ARMLAUNCH_CTRL:
2391 if (cmd.cmd.armlaunch_ctrl)
2392 ipath_enable_armlaunch(pd->port_dd);
2393 else
2394 ipath_disable_armlaunch(pd->port_dd);
2395 break;
Dave Olson124b4dc2008-04-16 21:09:32 -07002396 case IPATH_CMD_SDMA_INFLIGHT:
2397 ret = ipath_sdma_get_inflight(user_sdma_queue_fp(fp),
2398 (u32 __user *) (unsigned long)
2399 cmd.cmd.sdma_inflight);
2400 break;
2401 case IPATH_CMD_SDMA_COMPLETE:
2402 ret = ipath_sdma_get_complete(pd->port_dd,
2403 user_sdma_queue_fp(fp),
2404 (u32 __user *) (unsigned long)
2405 cmd.cmd.sdma_complete);
2406 break;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002407 }
2408
2409 if (ret >= 0)
2410 ret = consumed;
2411
2412bail:
2413 return ret;
2414}
2415
Dave Olson124b4dc2008-04-16 21:09:32 -07002416static ssize_t ipath_writev(struct kiocb *iocb, const struct iovec *iov,
2417 unsigned long dim, loff_t off)
2418{
2419 struct file *filp = iocb->ki_filp;
2420 struct ipath_filedata *fp = filp->private_data;
2421 struct ipath_portdata *pd = port_fp(filp);
2422 struct ipath_user_sdma_queue *pq = fp->pq;
2423
2424 if (!dim)
2425 return -EINVAL;
2426
2427 return ipath_user_sdma_writev(pd->port_dd, pq, iov, dim);
2428}
2429
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002430static struct class *ipath_class;
2431
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002432static int init_cdev(int minor, char *name, const struct file_operations *fops,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002433 struct cdev **cdevp, struct device **devp)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002434{
2435 const dev_t dev = MKDEV(IPATH_MAJOR, minor);
2436 struct cdev *cdev = NULL;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002437 struct device *device = NULL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002438 int ret;
2439
2440 cdev = cdev_alloc();
2441 if (!cdev) {
2442 printk(KERN_ERR IPATH_DRV_NAME
2443 ": Could not allocate cdev for minor %d, %s\n",
2444 minor, name);
2445 ret = -ENOMEM;
2446 goto done;
2447 }
2448
2449 cdev->owner = THIS_MODULE;
2450 cdev->ops = fops;
2451 kobject_set_name(&cdev->kobj, name);
2452
2453 ret = cdev_add(cdev, dev, 1);
2454 if (ret < 0) {
2455 printk(KERN_ERR IPATH_DRV_NAME
2456 ": Could not add cdev for minor %d, %s (err %d)\n",
2457 minor, name, -ret);
2458 goto err_cdev;
2459 }
2460
Greg Kroah-Hartman91bd4182008-07-21 20:03:34 -07002461 device = device_create(ipath_class, NULL, dev, NULL, name);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002462
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002463 if (IS_ERR(device)) {
2464 ret = PTR_ERR(device);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002465 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002466 "device for minor %d, %s (err %d)\n",
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002467 minor, name, -ret);
2468 goto err_cdev;
2469 }
2470
2471 goto done;
2472
2473err_cdev:
2474 cdev_del(cdev);
2475 cdev = NULL;
2476
2477done:
2478 if (ret >= 0) {
2479 *cdevp = cdev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002480 *devp = device;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002481 } else {
2482 *cdevp = NULL;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002483 *devp = NULL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002484 }
2485
2486 return ret;
2487}
2488
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08002489int ipath_cdev_init(int minor, char *name, const struct file_operations *fops,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002490 struct cdev **cdevp, struct device **devp)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002491{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002492 return init_cdev(minor, name, fops, cdevp, devp);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002493}
2494
2495static void cleanup_cdev(struct cdev **cdevp,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002496 struct device **devp)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002497{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002498 struct device *dev = *devp;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002499
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002500 if (dev) {
2501 device_unregister(dev);
2502 *devp = NULL;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002503 }
2504
2505 if (*cdevp) {
2506 cdev_del(*cdevp);
2507 *cdevp = NULL;
2508 }
2509}
2510
2511void ipath_cdev_cleanup(struct cdev **cdevp,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002512 struct device **devp)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002513{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002514 cleanup_cdev(cdevp, devp);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002515}
2516
2517static struct cdev *wildcard_cdev;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002518static struct device *wildcard_dev;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002519
2520static const dev_t dev = MKDEV(IPATH_MAJOR, 0);
2521
2522static int user_init(void)
2523{
2524 int ret;
2525
2526 ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME);
2527 if (ret < 0) {
2528 printk(KERN_ERR IPATH_DRV_NAME ": Could not register "
2529 "chrdev region (err %d)\n", -ret);
2530 goto done;
2531 }
2532
2533 ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME);
2534
2535 if (IS_ERR(ipath_class)) {
2536 ret = PTR_ERR(ipath_class);
2537 printk(KERN_ERR IPATH_DRV_NAME ": Could not create "
2538 "device class (err %d)\n", -ret);
2539 goto bail;
2540 }
2541
2542 goto done;
2543bail:
2544 unregister_chrdev_region(dev, IPATH_NMINORS);
2545done:
2546 return ret;
2547}
2548
2549static void user_cleanup(void)
2550{
2551 if (ipath_class) {
2552 class_destroy(ipath_class);
2553 ipath_class = NULL;
2554 }
2555
2556 unregister_chrdev_region(dev, IPATH_NMINORS);
2557}
2558
2559static atomic_t user_count = ATOMIC_INIT(0);
2560static atomic_t user_setup = ATOMIC_INIT(0);
2561
2562int ipath_user_add(struct ipath_devdata *dd)
2563{
2564 char name[10];
2565 int ret;
2566
2567 if (atomic_inc_return(&user_count) == 1) {
2568 ret = user_init();
2569 if (ret < 0) {
2570 ipath_dev_err(dd, "Unable to set up user support: "
2571 "error %d\n", -ret);
2572 goto bail;
2573 }
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002574 ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002575 &wildcard_dev);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002576 if (ret < 0) {
2577 ipath_dev_err(dd, "Could not create wildcard "
2578 "minor: error %d\n", -ret);
Bryan O'Sullivan0fd41362006-08-25 11:24:34 -07002579 goto bail_user;
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002580 }
2581
2582 atomic_set(&user_setup, 1);
2583 }
2584
2585 snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit);
2586
2587 ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002588 &dd->user_cdev, &dd->user_dev);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002589 if (ret < 0)
2590 ipath_dev_err(dd, "Could not create user minor %d, %s\n",
2591 dd->ipath_unit + 1, name);
2592
2593 goto bail;
2594
Bryan O'Sullivan0fd41362006-08-25 11:24:34 -07002595bail_user:
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002596 user_cleanup();
2597bail:
2598 return ret;
2599}
2600
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -07002601void ipath_user_remove(struct ipath_devdata *dd)
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002602{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002603 cleanup_cdev(&dd->user_cdev, &dd->user_dev);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002604
2605 if (atomic_dec_return(&user_count) == 0) {
2606 if (atomic_read(&user_setup) == 0)
2607 goto bail;
2608
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002609 cleanup_cdev(&wildcard_cdev, &wildcard_dev);
Bryan O'Sullivan7f510b42006-03-29 15:23:31 -08002610 user_cleanup();
2611
2612 atomic_set(&user_setup, 0);
2613 }
2614bail:
2615 return;
2616}