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