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