Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1 | /* |
Bryan O'Sullivan | 759d576 | 2006-07-01 04:35:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2006 QLogic, Inc. All rights reserved. |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 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" |
| 42 | #include "ips_common.h" |
| 43 | #include "ipath_layer.h" |
| 44 | |
| 45 | static int ipath_open(struct inode *, struct file *); |
| 46 | static int ipath_close(struct inode *, struct file *); |
| 47 | static ssize_t ipath_write(struct file *, const char __user *, size_t, |
| 48 | loff_t *); |
| 49 | static unsigned int ipath_poll(struct file *, struct poll_table_struct *); |
| 50 | static int ipath_mmap(struct file *, struct vm_area_struct *); |
| 51 | |
| 52 | static struct file_operations ipath_file_ops = { |
| 53 | .owner = THIS_MODULE, |
| 54 | .write = ipath_write, |
| 55 | .open = ipath_open, |
| 56 | .release = ipath_close, |
| 57 | .poll = ipath_poll, |
| 58 | .mmap = ipath_mmap |
| 59 | }; |
| 60 | |
| 61 | static int ipath_get_base_info(struct ipath_portdata *pd, |
| 62 | void __user *ubase, size_t ubase_size) |
| 63 | { |
| 64 | int ret = 0; |
| 65 | struct ipath_base_info *kinfo = NULL; |
| 66 | struct ipath_devdata *dd = pd->port_dd; |
| 67 | |
| 68 | if (ubase_size < sizeof(*kinfo)) { |
| 69 | ipath_cdbg(PROC, |
| 70 | "Base size %lu, need %lu (version mismatch?)\n", |
| 71 | (unsigned long) ubase_size, |
| 72 | (unsigned long) sizeof(*kinfo)); |
| 73 | ret = -EINVAL; |
| 74 | goto bail; |
| 75 | } |
| 76 | |
| 77 | kinfo = kzalloc(sizeof(*kinfo), GFP_KERNEL); |
| 78 | if (kinfo == NULL) { |
| 79 | ret = -ENOMEM; |
| 80 | goto bail; |
| 81 | } |
| 82 | |
| 83 | ret = dd->ipath_f_get_base_info(pd, kinfo); |
| 84 | if (ret < 0) |
| 85 | goto bail; |
| 86 | |
| 87 | kinfo->spi_rcvhdr_cnt = dd->ipath_rcvhdrcnt; |
| 88 | kinfo->spi_rcvhdrent_size = dd->ipath_rcvhdrentsize; |
| 89 | kinfo->spi_tidegrcnt = dd->ipath_rcvegrcnt; |
| 90 | kinfo->spi_rcv_egrbufsize = dd->ipath_rcvegrbufsize; |
| 91 | /* |
| 92 | * have to mmap whole thing |
| 93 | */ |
| 94 | kinfo->spi_rcv_egrbuftotlen = |
| 95 | pd->port_rcvegrbuf_chunks * pd->port_rcvegrbuf_size; |
| 96 | kinfo->spi_rcv_egrperchunk = pd->port_rcvegrbufs_perchunk; |
| 97 | kinfo->spi_rcv_egrchunksize = kinfo->spi_rcv_egrbuftotlen / |
| 98 | pd->port_rcvegrbuf_chunks; |
| 99 | kinfo->spi_tidcnt = dd->ipath_rcvtidcnt; |
| 100 | /* |
| 101 | * for this use, may be ipath_cfgports summed over all chips that |
| 102 | * are are configured and present |
| 103 | */ |
| 104 | kinfo->spi_nports = dd->ipath_cfgports; |
| 105 | /* unit (chip/board) our port is on */ |
| 106 | kinfo->spi_unit = dd->ipath_unit; |
| 107 | /* for now, only a single page */ |
| 108 | kinfo->spi_tid_maxsize = PAGE_SIZE; |
| 109 | |
| 110 | /* |
| 111 | * Doing this per port, and based on the skip value, etc. This has |
| 112 | * to be the actual buffer size, since the protocol code treats it |
| 113 | * as an array. |
| 114 | * |
| 115 | * These have to be set to user addresses in the user code via mmap. |
| 116 | * These values are used on return to user code for the mmap target |
| 117 | * addresses only. For 32 bit, same 44 bit address problem, so use |
| 118 | * the physical address, not virtual. Before 2.6.11, using the |
| 119 | * page_address() macro worked, but in 2.6.11, even that returns the |
| 120 | * full 64 bit address (upper bits all 1's). So far, using the |
| 121 | * physical addresses (or chip offsets, for chip mapping) works, but |
| 122 | * no doubt some future kernel release will chang that, and we'll be |
| 123 | * on to yet another method of dealing with this |
| 124 | */ |
| 125 | kinfo->spi_rcvhdr_base = (u64) pd->port_rcvhdrq_phys; |
| 126 | kinfo->spi_rcv_egrbufs = (u64) pd->port_rcvegr_phys; |
| 127 | kinfo->spi_pioavailaddr = (u64) dd->ipath_pioavailregs_phys; |
| 128 | kinfo->spi_status = (u64) kinfo->spi_pioavailaddr + |
| 129 | (void *) dd->ipath_statusp - |
| 130 | (void *) dd->ipath_pioavailregs_dma; |
| 131 | kinfo->spi_piobufbase = (u64) pd->port_piobufs; |
| 132 | kinfo->__spi_uregbase = |
| 133 | dd->ipath_uregbase + dd->ipath_palign * pd->port_port; |
| 134 | |
| 135 | kinfo->spi_pioindex = dd->ipath_pbufsport * (pd->port_port - 1); |
| 136 | kinfo->spi_piocnt = dd->ipath_pbufsport; |
| 137 | kinfo->spi_pioalign = dd->ipath_palign; |
| 138 | |
| 139 | kinfo->spi_qpair = IPATH_KD_QP; |
| 140 | kinfo->spi_piosize = dd->ipath_ibmaxlen; |
| 141 | kinfo->spi_mtu = dd->ipath_ibmaxlen; /* maxlen, not ibmtu */ |
| 142 | kinfo->spi_port = pd->port_port; |
Bryan O'Sullivan | eaf6733 | 2006-05-23 11:32:31 -0700 | [diff] [blame] | 143 | kinfo->spi_sw_version = IPATH_KERN_SWVERSION; |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 144 | kinfo->spi_hw_version = dd->ipath_revision; |
| 145 | |
| 146 | if (copy_to_user(ubase, kinfo, sizeof(*kinfo))) |
| 147 | ret = -EFAULT; |
| 148 | |
| 149 | bail: |
| 150 | kfree(kinfo); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * ipath_tid_update - update a port TID |
| 156 | * @pd: the port |
| 157 | * @ti: the TID information |
| 158 | * |
| 159 | * The new implementation as of Oct 2004 is that the driver assigns |
| 160 | * the tid and returns it to the caller. To make it easier to |
| 161 | * catch bugs, and to reduce search time, we keep a cursor for |
| 162 | * each port, walking the shadow tid array to find one that's not |
| 163 | * in use. |
| 164 | * |
| 165 | * For now, if we can't allocate the full list, we fail, although |
| 166 | * in the long run, we'll allocate as many as we can, and the |
| 167 | * caller will deal with that by trying the remaining pages later. |
| 168 | * That means that when we fail, we have to mark the tids as not in |
| 169 | * use again, in our shadow copy. |
| 170 | * |
| 171 | * It's up to the caller to free the tids when they are done. |
| 172 | * We'll unlock the pages as they free them. |
| 173 | * |
| 174 | * Also, right now we are locking one page at a time, but since |
| 175 | * the intended use of this routine is for a single group of |
| 176 | * virtually contiguous pages, that should change to improve |
| 177 | * performance. |
| 178 | */ |
| 179 | static int ipath_tid_update(struct ipath_portdata *pd, |
| 180 | const struct ipath_tid_info *ti) |
| 181 | { |
| 182 | int ret = 0, ntids; |
| 183 | u32 tid, porttid, cnt, i, tidcnt; |
| 184 | u16 *tidlist; |
| 185 | struct ipath_devdata *dd = pd->port_dd; |
| 186 | u64 physaddr; |
| 187 | unsigned long vaddr; |
| 188 | u64 __iomem *tidbase; |
| 189 | unsigned long tidmap[8]; |
| 190 | struct page **pagep = NULL; |
| 191 | |
| 192 | if (!dd->ipath_pageshadow) { |
| 193 | ret = -ENOMEM; |
| 194 | goto done; |
| 195 | } |
| 196 | |
| 197 | cnt = ti->tidcnt; |
| 198 | if (!cnt) { |
| 199 | ipath_dbg("After copyin, tidcnt 0, tidlist %llx\n", |
| 200 | (unsigned long long) ti->tidlist); |
| 201 | /* |
| 202 | * Should we treat as success? likely a bug |
| 203 | */ |
| 204 | ret = -EFAULT; |
| 205 | goto done; |
| 206 | } |
| 207 | tidcnt = dd->ipath_rcvtidcnt; |
| 208 | if (cnt >= tidcnt) { |
| 209 | /* make sure it all fits in port_tid_pg_list */ |
| 210 | dev_info(&dd->pcidev->dev, "Process tried to allocate %u " |
| 211 | "TIDs, only trying max (%u)\n", cnt, tidcnt); |
| 212 | cnt = tidcnt; |
| 213 | } |
| 214 | pagep = (struct page **)pd->port_tid_pg_list; |
| 215 | tidlist = (u16 *) (&pagep[cnt]); |
| 216 | |
| 217 | memset(tidmap, 0, sizeof(tidmap)); |
| 218 | tid = pd->port_tidcursor; |
| 219 | /* before decrement; chip actual # */ |
| 220 | porttid = pd->port_port * tidcnt; |
| 221 | ntids = tidcnt; |
| 222 | tidbase = (u64 __iomem *) (((char __iomem *) dd->ipath_kregbase) + |
| 223 | dd->ipath_rcvtidbase + |
| 224 | porttid * sizeof(*tidbase)); |
| 225 | |
| 226 | ipath_cdbg(VERBOSE, "Port%u %u tids, cursor %u, tidbase %p\n", |
| 227 | pd->port_port, cnt, tid, tidbase); |
| 228 | |
| 229 | /* virtual address of first page in transfer */ |
| 230 | vaddr = ti->tidvaddr; |
| 231 | if (!access_ok(VERIFY_WRITE, (void __user *) vaddr, |
| 232 | cnt * PAGE_SIZE)) { |
| 233 | ipath_dbg("Fail vaddr %p, %u pages, !access_ok\n", |
| 234 | (void *)vaddr, cnt); |
| 235 | ret = -EFAULT; |
| 236 | goto done; |
| 237 | } |
| 238 | ret = ipath_get_user_pages(vaddr, cnt, pagep); |
| 239 | if (ret) { |
| 240 | if (ret == -EBUSY) { |
| 241 | ipath_dbg("Failed to lock addr %p, %u pages " |
| 242 | "(already locked)\n", |
| 243 | (void *) vaddr, cnt); |
| 244 | /* |
| 245 | * for now, continue, and see what happens but with |
| 246 | * the new implementation, this should never happen, |
| 247 | * unless perhaps the user has mpin'ed the pages |
| 248 | * themselves (something we need to test) |
| 249 | */ |
| 250 | ret = 0; |
| 251 | } else { |
| 252 | dev_info(&dd->pcidev->dev, |
| 253 | "Failed to lock addr %p, %u pages: " |
| 254 | "errno %d\n", (void *) vaddr, cnt, -ret); |
| 255 | goto done; |
| 256 | } |
| 257 | } |
| 258 | for (i = 0; i < cnt; i++, vaddr += PAGE_SIZE) { |
| 259 | for (; ntids--; tid++) { |
| 260 | if (tid == tidcnt) |
| 261 | tid = 0; |
| 262 | if (!dd->ipath_pageshadow[porttid + tid]) |
| 263 | break; |
| 264 | } |
| 265 | if (ntids < 0) { |
| 266 | /* |
| 267 | * oops, wrapped all the way through their TIDs, |
| 268 | * and didn't have enough free; see comments at |
| 269 | * start of routine |
| 270 | */ |
| 271 | ipath_dbg("Not enough free TIDs for %u pages " |
| 272 | "(index %d), failing\n", cnt, i); |
| 273 | i--; /* last tidlist[i] not filled in */ |
| 274 | ret = -ENOMEM; |
| 275 | break; |
| 276 | } |
| 277 | tidlist[i] = tid; |
| 278 | ipath_cdbg(VERBOSE, "Updating idx %u to TID %u, " |
| 279 | "vaddr %lx\n", i, tid, vaddr); |
| 280 | /* we "know" system pages and TID pages are same size */ |
| 281 | dd->ipath_pageshadow[porttid + tid] = pagep[i]; |
| 282 | /* |
| 283 | * don't need atomic or it's overhead |
| 284 | */ |
| 285 | __set_bit(tid, tidmap); |
| 286 | physaddr = page_to_phys(pagep[i]); |
| 287 | ipath_stats.sps_pagelocks++; |
| 288 | ipath_cdbg(VERBOSE, |
| 289 | "TID %u, vaddr %lx, physaddr %llx pgp %p\n", |
| 290 | tid, vaddr, (unsigned long long) physaddr, |
| 291 | pagep[i]); |
| 292 | dd->ipath_f_put_tid(dd, &tidbase[tid], 1, physaddr); |
| 293 | /* |
| 294 | * don't check this tid in ipath_portshadow, since we |
| 295 | * just filled it in; start with the next one. |
| 296 | */ |
| 297 | tid++; |
| 298 | } |
| 299 | |
| 300 | if (ret) { |
| 301 | u32 limit; |
| 302 | cleanup: |
| 303 | /* jump here if copy out of updated info failed... */ |
| 304 | ipath_dbg("After failure (ret=%d), undo %d of %d entries\n", |
| 305 | -ret, i, cnt); |
| 306 | /* same code that's in ipath_free_tid() */ |
| 307 | limit = sizeof(tidmap) * BITS_PER_BYTE; |
| 308 | if (limit > tidcnt) |
| 309 | /* just in case size changes in future */ |
| 310 | limit = tidcnt; |
| 311 | tid = find_first_bit((const unsigned long *)tidmap, limit); |
| 312 | for (; tid < limit; tid++) { |
| 313 | if (!test_bit(tid, tidmap)) |
| 314 | continue; |
| 315 | if (dd->ipath_pageshadow[porttid + tid]) { |
| 316 | ipath_cdbg(VERBOSE, "Freeing TID %u\n", |
| 317 | tid); |
| 318 | dd->ipath_f_put_tid(dd, &tidbase[tid], 1, |
| 319 | dd->ipath_tidinvalid); |
| 320 | dd->ipath_pageshadow[porttid + tid] = NULL; |
| 321 | ipath_stats.sps_pageunlocks++; |
| 322 | } |
| 323 | } |
| 324 | ipath_release_user_pages(pagep, cnt); |
| 325 | } else { |
| 326 | /* |
| 327 | * Copy the updated array, with ipath_tid's filled in, back |
| 328 | * to user. Since we did the copy in already, this "should |
| 329 | * never fail" If it does, we have to clean up... |
| 330 | */ |
| 331 | if (copy_to_user((void __user *) |
| 332 | (unsigned long) ti->tidlist, |
| 333 | tidlist, cnt * sizeof(*tidlist))) { |
| 334 | ret = -EFAULT; |
| 335 | goto cleanup; |
| 336 | } |
| 337 | if (copy_to_user((void __user *) (unsigned long) ti->tidmap, |
| 338 | tidmap, sizeof tidmap)) { |
| 339 | ret = -EFAULT; |
| 340 | goto cleanup; |
| 341 | } |
| 342 | if (tid == tidcnt) |
| 343 | tid = 0; |
| 344 | pd->port_tidcursor = tid; |
| 345 | } |
| 346 | |
| 347 | done: |
| 348 | if (ret) |
| 349 | ipath_dbg("Failed to map %u TID pages, failing with %d\n", |
| 350 | ti->tidcnt, -ret); |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * ipath_tid_free - free a port TID |
| 356 | * @pd: the port |
| 357 | * @ti: the TID info |
| 358 | * |
| 359 | * right now we are unlocking one page at a time, but since |
| 360 | * the intended use of this routine is for a single group of |
| 361 | * virtually contiguous pages, that should change to improve |
| 362 | * performance. We check that the TID is in range for this port |
| 363 | * but otherwise don't check validity; if user has an error and |
| 364 | * frees the wrong tid, it's only their own data that can thereby |
| 365 | * be corrupted. We do check that the TID was in use, for sanity |
| 366 | * We always use our idea of the saved address, not the address that |
| 367 | * they pass in to us. |
| 368 | */ |
| 369 | |
| 370 | static int ipath_tid_free(struct ipath_portdata *pd, |
| 371 | const struct ipath_tid_info *ti) |
| 372 | { |
| 373 | int ret = 0; |
| 374 | u32 tid, porttid, cnt, limit, tidcnt; |
| 375 | struct ipath_devdata *dd = pd->port_dd; |
| 376 | u64 __iomem *tidbase; |
| 377 | unsigned long tidmap[8]; |
| 378 | |
| 379 | if (!dd->ipath_pageshadow) { |
| 380 | ret = -ENOMEM; |
| 381 | goto done; |
| 382 | } |
| 383 | |
| 384 | if (copy_from_user(tidmap, (void __user *)(unsigned long)ti->tidmap, |
| 385 | sizeof tidmap)) { |
| 386 | ret = -EFAULT; |
| 387 | goto done; |
| 388 | } |
| 389 | |
| 390 | porttid = pd->port_port * dd->ipath_rcvtidcnt; |
| 391 | tidbase = (u64 __iomem *) ((char __iomem *)(dd->ipath_kregbase) + |
| 392 | dd->ipath_rcvtidbase + |
| 393 | porttid * sizeof(*tidbase)); |
| 394 | |
| 395 | tidcnt = dd->ipath_rcvtidcnt; |
| 396 | limit = sizeof(tidmap) * BITS_PER_BYTE; |
| 397 | if (limit > tidcnt) |
| 398 | /* just in case size changes in future */ |
| 399 | limit = tidcnt; |
| 400 | tid = find_first_bit(tidmap, limit); |
| 401 | ipath_cdbg(VERBOSE, "Port%u free %u tids; first bit (max=%d) " |
| 402 | "set is %d, porttid %u\n", pd->port_port, ti->tidcnt, |
| 403 | limit, tid, porttid); |
| 404 | for (cnt = 0; tid < limit; tid++) { |
| 405 | /* |
| 406 | * small optimization; if we detect a run of 3 or so without |
| 407 | * any set, use find_first_bit again. That's mainly to |
| 408 | * accelerate the case where we wrapped, so we have some at |
| 409 | * the beginning, and some at the end, and a big gap |
| 410 | * in the middle. |
| 411 | */ |
| 412 | if (!test_bit(tid, tidmap)) |
| 413 | continue; |
| 414 | cnt++; |
| 415 | if (dd->ipath_pageshadow[porttid + tid]) { |
| 416 | ipath_cdbg(VERBOSE, "PID %u freeing TID %u\n", |
| 417 | pd->port_pid, tid); |
| 418 | dd->ipath_f_put_tid(dd, &tidbase[tid], 1, |
| 419 | dd->ipath_tidinvalid); |
| 420 | ipath_release_user_pages( |
| 421 | &dd->ipath_pageshadow[porttid + tid], 1); |
| 422 | dd->ipath_pageshadow[porttid + tid] = NULL; |
| 423 | ipath_stats.sps_pageunlocks++; |
| 424 | } else |
| 425 | ipath_dbg("Unused tid %u, ignoring\n", tid); |
| 426 | } |
| 427 | if (cnt != ti->tidcnt) |
| 428 | ipath_dbg("passed in tidcnt %d, only %d bits set in map\n", |
| 429 | ti->tidcnt, cnt); |
| 430 | done: |
| 431 | if (ret) |
| 432 | ipath_dbg("Failed to unmap %u TID pages, failing with %d\n", |
| 433 | ti->tidcnt, -ret); |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * ipath_set_part_key - set a partition key |
| 439 | * @pd: the port |
| 440 | * @key: the key |
| 441 | * |
| 442 | * We can have up to 4 active at a time (other than the default, which is |
| 443 | * always allowed). This is somewhat tricky, since multiple ports may set |
| 444 | * the same key, so we reference count them, and clean up at exit. All 4 |
| 445 | * partition keys are packed into a single infinipath register. It's an |
| 446 | * error for a process to set the same pkey multiple times. We provide no |
| 447 | * mechanism to de-allocate a pkey at this time, we may eventually need to |
| 448 | * do that. I've used the atomic operations, and no locking, and only make |
| 449 | * a single pass through what's available. This should be more than |
| 450 | * adequate for some time. I'll think about spinlocks or the like if and as |
| 451 | * it's necessary. |
| 452 | */ |
| 453 | static int ipath_set_part_key(struct ipath_portdata *pd, u16 key) |
| 454 | { |
| 455 | struct ipath_devdata *dd = pd->port_dd; |
| 456 | int i, any = 0, pidx = -1; |
| 457 | u16 lkey = key & 0x7FFF; |
| 458 | int ret; |
| 459 | |
| 460 | if (lkey == (IPS_DEFAULT_P_KEY & 0x7FFF)) { |
| 461 | /* nothing to do; this key always valid */ |
| 462 | ret = 0; |
| 463 | goto bail; |
| 464 | } |
| 465 | |
| 466 | ipath_cdbg(VERBOSE, "p%u try to set pkey %hx, current keys " |
| 467 | "%hx:%x %hx:%x %hx:%x %hx:%x\n", |
| 468 | pd->port_port, key, dd->ipath_pkeys[0], |
| 469 | atomic_read(&dd->ipath_pkeyrefs[0]), dd->ipath_pkeys[1], |
| 470 | atomic_read(&dd->ipath_pkeyrefs[1]), dd->ipath_pkeys[2], |
| 471 | atomic_read(&dd->ipath_pkeyrefs[2]), dd->ipath_pkeys[3], |
| 472 | atomic_read(&dd->ipath_pkeyrefs[3])); |
| 473 | |
| 474 | if (!lkey) { |
| 475 | ipath_cdbg(PROC, "p%u tries to set key 0, not allowed\n", |
| 476 | pd->port_port); |
| 477 | ret = -EINVAL; |
| 478 | goto bail; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Set the full membership bit, because it has to be |
| 483 | * set in the register or the packet, and it seems |
| 484 | * cleaner to set in the register than to force all |
| 485 | * callers to set it. (see bug 4331) |
| 486 | */ |
| 487 | key |= 0x8000; |
| 488 | |
| 489 | for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) { |
| 490 | if (!pd->port_pkeys[i] && pidx == -1) |
| 491 | pidx = i; |
| 492 | if (pd->port_pkeys[i] == key) { |
| 493 | ipath_cdbg(VERBOSE, "p%u tries to set same pkey " |
| 494 | "(%x) more than once\n", |
| 495 | pd->port_port, key); |
| 496 | ret = -EEXIST; |
| 497 | goto bail; |
| 498 | } |
| 499 | } |
| 500 | if (pidx == -1) { |
| 501 | ipath_dbg("All pkeys for port %u already in use, " |
| 502 | "can't set %x\n", pd->port_port, key); |
| 503 | ret = -EBUSY; |
| 504 | goto bail; |
| 505 | } |
| 506 | for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) { |
| 507 | if (!dd->ipath_pkeys[i]) { |
| 508 | any++; |
| 509 | continue; |
| 510 | } |
| 511 | if (dd->ipath_pkeys[i] == key) { |
| 512 | atomic_t *pkrefs = &dd->ipath_pkeyrefs[i]; |
| 513 | |
| 514 | if (atomic_inc_return(pkrefs) > 1) { |
| 515 | pd->port_pkeys[pidx] = key; |
| 516 | ipath_cdbg(VERBOSE, "p%u set key %x " |
| 517 | "matches #%d, count now %d\n", |
| 518 | pd->port_port, key, i, |
| 519 | atomic_read(pkrefs)); |
| 520 | ret = 0; |
| 521 | goto bail; |
| 522 | } else { |
| 523 | /* |
| 524 | * lost race, decrement count, catch below |
| 525 | */ |
| 526 | atomic_dec(pkrefs); |
| 527 | ipath_cdbg(VERBOSE, "Lost race, count was " |
| 528 | "0, after dec, it's %d\n", |
| 529 | atomic_read(pkrefs)); |
| 530 | any++; |
| 531 | } |
| 532 | } |
| 533 | if ((dd->ipath_pkeys[i] & 0x7FFF) == lkey) { |
| 534 | /* |
| 535 | * It makes no sense to have both the limited and |
| 536 | * full membership PKEY set at the same time since |
| 537 | * the unlimited one will disable the limited one. |
| 538 | */ |
| 539 | ret = -EEXIST; |
| 540 | goto bail; |
| 541 | } |
| 542 | } |
| 543 | if (!any) { |
| 544 | ipath_dbg("port %u, all pkeys already in use, " |
| 545 | "can't set %x\n", pd->port_port, key); |
| 546 | ret = -EBUSY; |
| 547 | goto bail; |
| 548 | } |
| 549 | for (any = i = 0; i < ARRAY_SIZE(dd->ipath_pkeys); i++) { |
| 550 | if (!dd->ipath_pkeys[i] && |
| 551 | atomic_inc_return(&dd->ipath_pkeyrefs[i]) == 1) { |
| 552 | u64 pkey; |
| 553 | |
| 554 | /* for ipathstats, etc. */ |
| 555 | ipath_stats.sps_pkeys[i] = lkey; |
| 556 | pd->port_pkeys[pidx] = dd->ipath_pkeys[i] = key; |
| 557 | pkey = |
| 558 | (u64) dd->ipath_pkeys[0] | |
| 559 | ((u64) dd->ipath_pkeys[1] << 16) | |
| 560 | ((u64) dd->ipath_pkeys[2] << 32) | |
| 561 | ((u64) dd->ipath_pkeys[3] << 48); |
| 562 | ipath_cdbg(PROC, "p%u set key %x in #%d, " |
| 563 | "portidx %d, new pkey reg %llx\n", |
| 564 | pd->port_port, key, i, pidx, |
| 565 | (unsigned long long) pkey); |
| 566 | ipath_write_kreg( |
| 567 | dd, dd->ipath_kregs->kr_partitionkey, pkey); |
| 568 | |
| 569 | ret = 0; |
| 570 | goto bail; |
| 571 | } |
| 572 | } |
| 573 | ipath_dbg("port %u, all pkeys already in use 2nd pass, " |
| 574 | "can't set %x\n", pd->port_port, key); |
| 575 | ret = -EBUSY; |
| 576 | |
| 577 | bail: |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * ipath_manage_rcvq - manage a port's receive queue |
| 583 | * @pd: the port |
| 584 | * @start_stop: action to carry out |
| 585 | * |
| 586 | * start_stop == 0 disables receive on the port, for use in queue |
| 587 | * overflow conditions. start_stop==1 re-enables, to be used to |
| 588 | * re-init the software copy of the head register |
| 589 | */ |
| 590 | static int ipath_manage_rcvq(struct ipath_portdata *pd, int start_stop) |
| 591 | { |
| 592 | struct ipath_devdata *dd = pd->port_dd; |
| 593 | u64 tval; |
| 594 | |
| 595 | ipath_cdbg(PROC, "%sabling rcv for unit %u port %u\n", |
| 596 | start_stop ? "en" : "dis", dd->ipath_unit, |
| 597 | pd->port_port); |
| 598 | /* atomically clear receive enable port. */ |
| 599 | if (start_stop) { |
| 600 | /* |
| 601 | * On enable, force in-memory copy of the tail register to |
| 602 | * 0, so that protocol code doesn't have to worry about |
| 603 | * whether or not the chip has yet updated the in-memory |
| 604 | * copy or not on return from the system call. The chip |
| 605 | * always resets it's tail register back to 0 on a |
| 606 | * transition from disabled to enabled. This could cause a |
| 607 | * problem if software was broken, and did the enable w/o |
| 608 | * the disable, but eventually the in-memory copy will be |
| 609 | * updated and correct itself, even in the face of software |
| 610 | * bugs. |
| 611 | */ |
| 612 | *pd->port_rcvhdrtail_kvaddr = 0; |
| 613 | set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port, |
| 614 | &dd->ipath_rcvctrl); |
| 615 | } else |
| 616 | clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port, |
| 617 | &dd->ipath_rcvctrl); |
| 618 | ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, |
| 619 | dd->ipath_rcvctrl); |
| 620 | /* now be sure chip saw it before we return */ |
| 621 | tval = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch); |
| 622 | if (start_stop) { |
| 623 | /* |
| 624 | * And try to be sure that tail reg update has happened too. |
| 625 | * This should in theory interlock with the RXE changes to |
| 626 | * the tail register. Don't assign it to the tail register |
| 627 | * in memory copy, since we could overwrite an update by the |
| 628 | * chip if we did. |
| 629 | */ |
| 630 | tval = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port); |
| 631 | } |
| 632 | /* always; new head should be equal to new tail; see above */ |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static void ipath_clean_part_key(struct ipath_portdata *pd, |
| 637 | struct ipath_devdata *dd) |
| 638 | { |
| 639 | int i, j, pchanged = 0; |
| 640 | u64 oldpkey; |
| 641 | |
| 642 | /* for debugging only */ |
| 643 | oldpkey = (u64) dd->ipath_pkeys[0] | |
| 644 | ((u64) dd->ipath_pkeys[1] << 16) | |
| 645 | ((u64) dd->ipath_pkeys[2] << 32) | |
| 646 | ((u64) dd->ipath_pkeys[3] << 48); |
| 647 | |
| 648 | for (i = 0; i < ARRAY_SIZE(pd->port_pkeys); i++) { |
| 649 | if (!pd->port_pkeys[i]) |
| 650 | continue; |
| 651 | ipath_cdbg(VERBOSE, "look for key[%d] %hx in pkeys\n", i, |
| 652 | pd->port_pkeys[i]); |
| 653 | for (j = 0; j < ARRAY_SIZE(dd->ipath_pkeys); j++) { |
| 654 | /* check for match independent of the global bit */ |
| 655 | if ((dd->ipath_pkeys[j] & 0x7fff) != |
| 656 | (pd->port_pkeys[i] & 0x7fff)) |
| 657 | continue; |
| 658 | if (atomic_dec_and_test(&dd->ipath_pkeyrefs[j])) { |
| 659 | ipath_cdbg(VERBOSE, "p%u clear key " |
| 660 | "%x matches #%d\n", |
| 661 | pd->port_port, |
| 662 | pd->port_pkeys[i], j); |
| 663 | ipath_stats.sps_pkeys[j] = |
| 664 | dd->ipath_pkeys[j] = 0; |
| 665 | pchanged++; |
| 666 | } |
| 667 | else ipath_cdbg( |
| 668 | VERBOSE, "p%u key %x matches #%d, " |
| 669 | "but ref still %d\n", pd->port_port, |
| 670 | pd->port_pkeys[i], j, |
| 671 | atomic_read(&dd->ipath_pkeyrefs[j])); |
| 672 | break; |
| 673 | } |
| 674 | pd->port_pkeys[i] = 0; |
| 675 | } |
| 676 | if (pchanged) { |
| 677 | u64 pkey = (u64) dd->ipath_pkeys[0] | |
| 678 | ((u64) dd->ipath_pkeys[1] << 16) | |
| 679 | ((u64) dd->ipath_pkeys[2] << 32) | |
| 680 | ((u64) dd->ipath_pkeys[3] << 48); |
| 681 | ipath_cdbg(VERBOSE, "p%u old pkey reg %llx, " |
| 682 | "new pkey reg %llx\n", pd->port_port, |
| 683 | (unsigned long long) oldpkey, |
| 684 | (unsigned long long) pkey); |
| 685 | ipath_write_kreg(dd, dd->ipath_kregs->kr_partitionkey, |
| 686 | pkey); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * ipath_create_user_egr - allocate eager TID buffers |
| 692 | * @pd: the port to allocate TID buffers for |
| 693 | * |
| 694 | * This routine is now quite different for user and kernel, because |
| 695 | * the kernel uses skb's, for the accelerated network performance |
| 696 | * This is the user port version |
| 697 | * |
| 698 | * Allocate the eager TID buffers and program them into infinipath |
| 699 | * They are no longer completely contiguous, we do multiple allocation |
| 700 | * calls. |
| 701 | */ |
| 702 | static int ipath_create_user_egr(struct ipath_portdata *pd) |
| 703 | { |
| 704 | struct ipath_devdata *dd = pd->port_dd; |
| 705 | unsigned e, egrcnt, alloced, egrperchunk, chunk, egrsize, egroff; |
| 706 | size_t size; |
| 707 | int ret; |
Bryan O'Sullivan | 0ed9a4a | 2006-07-01 04:36:01 -0700 | [diff] [blame^] | 708 | gfp_t gfp_flags; |
| 709 | |
| 710 | /* |
| 711 | * GFP_USER, but without GFP_FS, so buffer cache can be |
| 712 | * coalesced (we hope); otherwise, even at order 4, |
| 713 | * heavy filesystem activity makes these fail, and we can |
| 714 | * use compound pages. |
| 715 | */ |
| 716 | gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP; |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 717 | |
| 718 | egrcnt = dd->ipath_rcvegrcnt; |
| 719 | /* TID number offset for this port */ |
| 720 | egroff = pd->port_port * egrcnt; |
| 721 | egrsize = dd->ipath_rcvegrbufsize; |
| 722 | ipath_cdbg(VERBOSE, "Allocating %d egr buffers, at egrtid " |
| 723 | "offset %x, egrsize %u\n", egrcnt, egroff, egrsize); |
| 724 | |
| 725 | /* |
| 726 | * to avoid wasting a lot of memory, we allocate 32KB chunks of |
| 727 | * physically contiguous memory, advance through it until used up |
| 728 | * and then allocate more. Of course, we need memory to store those |
| 729 | * extra pointers, now. Started out with 256KB, but under heavy |
| 730 | * memory pressure (creating large files and then copying them over |
| 731 | * NFS while doing lots of MPI jobs), we hit some allocation |
| 732 | * failures, even though we can sleep... (2.6.10) Still get |
Bryan O'Sullivan | 0ed9a4a | 2006-07-01 04:36:01 -0700 | [diff] [blame^] | 733 | * failures at 64K. 32K is the lowest we can go without wasting |
| 734 | * additional memory. |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 735 | */ |
| 736 | size = 0x8000; |
| 737 | alloced = ALIGN(egrsize * egrcnt, size); |
| 738 | egrperchunk = size / egrsize; |
| 739 | chunk = (egrcnt + egrperchunk - 1) / egrperchunk; |
| 740 | pd->port_rcvegrbuf_chunks = chunk; |
| 741 | pd->port_rcvegrbufs_perchunk = egrperchunk; |
| 742 | pd->port_rcvegrbuf_size = size; |
| 743 | pd->port_rcvegrbuf = vmalloc(chunk * sizeof(pd->port_rcvegrbuf[0])); |
| 744 | if (!pd->port_rcvegrbuf) { |
| 745 | ret = -ENOMEM; |
| 746 | goto bail; |
| 747 | } |
| 748 | pd->port_rcvegrbuf_phys = |
| 749 | vmalloc(chunk * sizeof(pd->port_rcvegrbuf_phys[0])); |
| 750 | if (!pd->port_rcvegrbuf_phys) { |
| 751 | ret = -ENOMEM; |
| 752 | goto bail_rcvegrbuf; |
| 753 | } |
| 754 | for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) { |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 755 | |
| 756 | pd->port_rcvegrbuf[e] = dma_alloc_coherent( |
| 757 | &dd->pcidev->dev, size, &pd->port_rcvegrbuf_phys[e], |
| 758 | gfp_flags); |
| 759 | |
| 760 | if (!pd->port_rcvegrbuf[e]) { |
| 761 | ret = -ENOMEM; |
| 762 | goto bail_rcvegrbuf_phys; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | pd->port_rcvegr_phys = pd->port_rcvegrbuf_phys[0]; |
| 767 | |
| 768 | for (e = chunk = 0; chunk < pd->port_rcvegrbuf_chunks; chunk++) { |
| 769 | dma_addr_t pa = pd->port_rcvegrbuf_phys[chunk]; |
| 770 | unsigned i; |
| 771 | |
| 772 | for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) { |
| 773 | dd->ipath_f_put_tid(dd, e + egroff + |
| 774 | (u64 __iomem *) |
| 775 | ((char __iomem *) |
| 776 | dd->ipath_kregbase + |
| 777 | dd->ipath_rcvegrbase), 0, pa); |
| 778 | pa += egrsize; |
| 779 | } |
| 780 | cond_resched(); /* don't hog the cpu */ |
| 781 | } |
| 782 | |
| 783 | ret = 0; |
| 784 | goto bail; |
| 785 | |
| 786 | bail_rcvegrbuf_phys: |
| 787 | for (e = 0; e < pd->port_rcvegrbuf_chunks && |
| 788 | pd->port_rcvegrbuf[e]; e++) |
| 789 | dma_free_coherent(&dd->pcidev->dev, size, |
| 790 | pd->port_rcvegrbuf[e], |
| 791 | pd->port_rcvegrbuf_phys[e]); |
| 792 | |
| 793 | vfree(pd->port_rcvegrbuf_phys); |
| 794 | pd->port_rcvegrbuf_phys = NULL; |
| 795 | bail_rcvegrbuf: |
| 796 | vfree(pd->port_rcvegrbuf); |
| 797 | pd->port_rcvegrbuf = NULL; |
| 798 | bail: |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | static int ipath_do_user_init(struct ipath_portdata *pd, |
| 803 | const struct ipath_user_info *uinfo) |
| 804 | { |
| 805 | int ret = 0; |
| 806 | struct ipath_devdata *dd = pd->port_dd; |
| 807 | u64 physaddr, uaddr, off, atmp; |
| 808 | struct page *pagep; |
| 809 | u32 head32; |
| 810 | u64 head; |
| 811 | |
| 812 | /* for now, if major version is different, bail */ |
| 813 | if ((uinfo->spu_userversion >> 16) != IPATH_USER_SWMAJOR) { |
| 814 | dev_info(&dd->pcidev->dev, |
| 815 | "User major version %d not same as driver " |
| 816 | "major %d\n", uinfo->spu_userversion >> 16, |
| 817 | IPATH_USER_SWMAJOR); |
| 818 | ret = -ENODEV; |
| 819 | goto done; |
| 820 | } |
| 821 | |
| 822 | if ((uinfo->spu_userversion & 0xffff) != IPATH_USER_SWMINOR) |
| 823 | ipath_dbg("User minor version %d not same as driver " |
| 824 | "minor %d\n", uinfo->spu_userversion & 0xffff, |
| 825 | IPATH_USER_SWMINOR); |
| 826 | |
| 827 | if (uinfo->spu_rcvhdrsize) { |
| 828 | ret = ipath_setrcvhdrsize(dd, uinfo->spu_rcvhdrsize); |
| 829 | if (ret) |
| 830 | goto done; |
| 831 | } |
| 832 | |
| 833 | /* for now we do nothing with rcvhdrcnt: uinfo->spu_rcvhdrcnt */ |
| 834 | |
| 835 | /* set up for the rcvhdr Q tail register writeback to user memory */ |
| 836 | if (!uinfo->spu_rcvhdraddr || |
| 837 | !access_ok(VERIFY_WRITE, (u64 __user *) (unsigned long) |
| 838 | uinfo->spu_rcvhdraddr, sizeof(u64))) { |
| 839 | ipath_dbg("Port %d rcvhdrtail addr %llx not valid\n", |
| 840 | pd->port_port, |
| 841 | (unsigned long long) uinfo->spu_rcvhdraddr); |
| 842 | ret = -EINVAL; |
| 843 | goto done; |
| 844 | } |
| 845 | |
| 846 | off = offset_in_page(uinfo->spu_rcvhdraddr); |
| 847 | uaddr = PAGE_MASK & (unsigned long) uinfo->spu_rcvhdraddr; |
| 848 | ret = ipath_get_user_pages_nocopy(uaddr, &pagep); |
| 849 | if (ret) { |
| 850 | dev_info(&dd->pcidev->dev, "Failed to lookup and lock " |
| 851 | "address %llx for rcvhdrtail: errno %d\n", |
| 852 | (unsigned long long) uinfo->spu_rcvhdraddr, -ret); |
| 853 | goto done; |
| 854 | } |
| 855 | ipath_stats.sps_pagelocks++; |
| 856 | pd->port_rcvhdrtail_uaddr = uaddr; |
| 857 | pd->port_rcvhdrtail_pagep = pagep; |
| 858 | pd->port_rcvhdrtail_kvaddr = |
| 859 | page_address(pagep); |
| 860 | pd->port_rcvhdrtail_kvaddr += off; |
| 861 | physaddr = page_to_phys(pagep) + off; |
| 862 | ipath_cdbg(VERBOSE, "port %d user addr %llx hdrtailaddr, %llx " |
| 863 | "physical (off=%llx)\n", |
| 864 | pd->port_port, |
| 865 | (unsigned long long) uinfo->spu_rcvhdraddr, |
| 866 | (unsigned long long) physaddr, (unsigned long long) off); |
| 867 | ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdrtailaddr, |
| 868 | pd->port_port, physaddr); |
| 869 | atmp = ipath_read_kreg64_port(dd, |
| 870 | dd->ipath_kregs->kr_rcvhdrtailaddr, |
| 871 | pd->port_port); |
| 872 | if (physaddr != atmp) { |
| 873 | ipath_dev_err(dd, |
| 874 | "Catastrophic software error, " |
| 875 | "RcvHdrTailAddr%u written as %llx, " |
| 876 | "read back as %llx\n", pd->port_port, |
| 877 | (unsigned long long) physaddr, |
| 878 | (unsigned long long) atmp); |
| 879 | ret = -EINVAL; |
| 880 | goto done; |
| 881 | } |
| 882 | |
| 883 | /* for right now, kernel piobufs are at end, so port 1 is at 0 */ |
| 884 | pd->port_piobufs = dd->ipath_piobufbase + |
| 885 | dd->ipath_pbufsport * (pd->port_port - |
| 886 | 1) * dd->ipath_palign; |
| 887 | ipath_cdbg(VERBOSE, "Set base of piobufs for port %u to 0x%x\n", |
| 888 | pd->port_port, pd->port_piobufs); |
| 889 | |
| 890 | /* |
| 891 | * Now allocate the rcvhdr Q and eager TIDs; skip the TID |
| 892 | * array for time being. If pd->port_port > chip-supported, |
| 893 | * we need to do extra stuff here to handle by handling overflow |
| 894 | * through port 0, someday |
| 895 | */ |
| 896 | ret = ipath_create_rcvhdrq(dd, pd); |
| 897 | if (!ret) |
| 898 | ret = ipath_create_user_egr(pd); |
| 899 | if (ret) |
| 900 | goto done; |
| 901 | /* enable receives now */ |
| 902 | /* atomically set enable bit for this port */ |
| 903 | set_bit(INFINIPATH_R_PORTENABLE_SHIFT + pd->port_port, |
| 904 | &dd->ipath_rcvctrl); |
| 905 | |
| 906 | /* |
| 907 | * set the head registers for this port to the current values |
| 908 | * of the tail pointers, since we don't know if they were |
| 909 | * updated on last use of the port. |
| 910 | */ |
| 911 | head32 = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port); |
| 912 | head = (u64) head32; |
| 913 | ipath_write_ureg(dd, ur_rcvhdrhead, head, pd->port_port); |
| 914 | head32 = ipath_read_ureg32(dd, ur_rcvegrindextail, pd->port_port); |
| 915 | ipath_write_ureg(dd, ur_rcvegrindexhead, head32, pd->port_port); |
| 916 | dd->ipath_lastegrheads[pd->port_port] = -1; |
| 917 | dd->ipath_lastrcvhdrqtails[pd->port_port] = -1; |
| 918 | ipath_cdbg(VERBOSE, "Wrote port%d head %llx, egrhead %x from " |
| 919 | "tail regs\n", pd->port_port, |
| 920 | (unsigned long long) head, head32); |
| 921 | pd->port_tidcursor = 0; /* start at beginning after open */ |
| 922 | /* |
| 923 | * now enable the port; the tail registers will be written to memory |
| 924 | * by the chip as soon as it sees the write to |
| 925 | * dd->ipath_kregs->kr_rcvctrl. The update only happens on |
| 926 | * transition from 0 to 1, so clear it first, then set it as part of |
| 927 | * enabling the port. This will (very briefly) affect any other |
| 928 | * open ports, but it shouldn't be long enough to be an issue. |
| 929 | */ |
| 930 | ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, |
| 931 | dd->ipath_rcvctrl & ~INFINIPATH_R_TAILUPD); |
| 932 | ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, |
| 933 | dd->ipath_rcvctrl); |
| 934 | |
| 935 | done: |
| 936 | return ret; |
| 937 | } |
| 938 | |
| 939 | static int mmap_ureg(struct vm_area_struct *vma, struct ipath_devdata *dd, |
| 940 | u64 ureg) |
| 941 | { |
| 942 | unsigned long phys; |
| 943 | int ret; |
| 944 | |
| 945 | /* it's the real hardware, so io_remap works */ |
| 946 | |
| 947 | if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) { |
| 948 | dev_info(&dd->pcidev->dev, "FAIL mmap userreg: reqlen " |
| 949 | "%lx > PAGE\n", vma->vm_end - vma->vm_start); |
| 950 | ret = -EFAULT; |
| 951 | } else { |
| 952 | phys = dd->ipath_physaddr + ureg; |
| 953 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 954 | |
| 955 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND; |
| 956 | ret = io_remap_pfn_range(vma, vma->vm_start, |
| 957 | phys >> PAGE_SHIFT, |
| 958 | vma->vm_end - vma->vm_start, |
| 959 | vma->vm_page_prot); |
| 960 | } |
| 961 | return ret; |
| 962 | } |
| 963 | |
| 964 | static int mmap_piobufs(struct vm_area_struct *vma, |
| 965 | struct ipath_devdata *dd, |
| 966 | struct ipath_portdata *pd) |
| 967 | { |
| 968 | unsigned long phys; |
| 969 | int ret; |
| 970 | |
| 971 | /* |
| 972 | * When we map the PIO buffers, we want to map them as writeonly, no |
| 973 | * read possible. |
| 974 | */ |
| 975 | |
| 976 | if ((vma->vm_end - vma->vm_start) > |
| 977 | (dd->ipath_pbufsport * dd->ipath_palign)) { |
| 978 | dev_info(&dd->pcidev->dev, "FAIL mmap piobufs: " |
| 979 | "reqlen %lx > PAGE\n", |
| 980 | vma->vm_end - vma->vm_start); |
| 981 | ret = -EFAULT; |
| 982 | goto bail; |
| 983 | } |
| 984 | |
| 985 | phys = dd->ipath_physaddr + pd->port_piobufs; |
| 986 | /* |
| 987 | * Do *NOT* mark this as non-cached (PWT bit), or we don't get the |
| 988 | * write combining behavior we want on the PIO buffers! |
| 989 | * vma->vm_page_prot = |
| 990 | * pgprot_noncached(vma->vm_page_prot); |
| 991 | */ |
| 992 | |
| 993 | if (vma->vm_flags & VM_READ) { |
| 994 | dev_info(&dd->pcidev->dev, |
| 995 | "Can't map piobufs as readable (flags=%lx)\n", |
| 996 | vma->vm_flags); |
| 997 | ret = -EPERM; |
| 998 | goto bail; |
| 999 | } |
| 1000 | |
| 1001 | /* don't allow them to later change to readable with mprotect */ |
| 1002 | |
| 1003 | vma->vm_flags &= ~VM_MAYWRITE; |
| 1004 | vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND; |
| 1005 | |
| 1006 | ret = io_remap_pfn_range(vma, vma->vm_start, phys >> PAGE_SHIFT, |
| 1007 | vma->vm_end - vma->vm_start, |
| 1008 | vma->vm_page_prot); |
| 1009 | bail: |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | static int mmap_rcvegrbufs(struct vm_area_struct *vma, |
| 1014 | struct ipath_portdata *pd) |
| 1015 | { |
| 1016 | struct ipath_devdata *dd = pd->port_dd; |
| 1017 | unsigned long start, size; |
| 1018 | size_t total_size, i; |
| 1019 | dma_addr_t *phys; |
| 1020 | int ret; |
| 1021 | |
| 1022 | if (!pd->port_rcvegrbuf) { |
| 1023 | ret = -EFAULT; |
| 1024 | goto bail; |
| 1025 | } |
| 1026 | |
| 1027 | size = pd->port_rcvegrbuf_size; |
| 1028 | total_size = pd->port_rcvegrbuf_chunks * size; |
| 1029 | if ((vma->vm_end - vma->vm_start) > total_size) { |
| 1030 | dev_info(&dd->pcidev->dev, "FAIL on egr bufs: " |
| 1031 | "reqlen %lx > actual %lx\n", |
| 1032 | vma->vm_end - vma->vm_start, |
| 1033 | (unsigned long) total_size); |
| 1034 | ret = -EFAULT; |
| 1035 | goto bail; |
| 1036 | } |
| 1037 | |
| 1038 | if (vma->vm_flags & VM_WRITE) { |
| 1039 | dev_info(&dd->pcidev->dev, "Can't map eager buffers as " |
| 1040 | "writable (flags=%lx)\n", vma->vm_flags); |
| 1041 | ret = -EPERM; |
| 1042 | goto bail; |
| 1043 | } |
| 1044 | |
| 1045 | start = vma->vm_start; |
| 1046 | phys = pd->port_rcvegrbuf_phys; |
| 1047 | |
| 1048 | /* don't allow them to later change to writeable with mprotect */ |
| 1049 | vma->vm_flags &= ~VM_MAYWRITE; |
| 1050 | |
| 1051 | for (i = 0; i < pd->port_rcvegrbuf_chunks; i++, start += size) { |
| 1052 | ret = remap_pfn_range(vma, start, phys[i] >> PAGE_SHIFT, |
| 1053 | size, vma->vm_page_prot); |
| 1054 | if (ret < 0) |
| 1055 | goto bail; |
| 1056 | } |
| 1057 | ret = 0; |
| 1058 | |
| 1059 | bail: |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | static int mmap_rcvhdrq(struct vm_area_struct *vma, |
| 1064 | struct ipath_portdata *pd) |
| 1065 | { |
| 1066 | struct ipath_devdata *dd = pd->port_dd; |
| 1067 | size_t total_size; |
| 1068 | int ret; |
| 1069 | |
| 1070 | /* |
| 1071 | * kmalloc'ed memory, physically contiguous; this is from |
| 1072 | * spi_rcvhdr_base; we allow user to map read-write so they can |
| 1073 | * write hdrq entries to allow protocol code to directly poll |
| 1074 | * whether a hdrq entry has been written. |
| 1075 | */ |
| 1076 | total_size = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize * |
| 1077 | sizeof(u32), PAGE_SIZE); |
| 1078 | if ((vma->vm_end - vma->vm_start) > total_size) { |
| 1079 | dev_info(&dd->pcidev->dev, |
| 1080 | "FAIL on rcvhdrq: reqlen %lx > actual %lx\n", |
| 1081 | vma->vm_end - vma->vm_start, |
| 1082 | (unsigned long) total_size); |
| 1083 | ret = -EFAULT; |
| 1084 | goto bail; |
| 1085 | } |
| 1086 | |
| 1087 | ret = remap_pfn_range(vma, vma->vm_start, |
| 1088 | pd->port_rcvhdrq_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_pioavailregs(struct vm_area_struct *vma, |
| 1096 | struct ipath_portdata *pd) |
| 1097 | { |
| 1098 | struct ipath_devdata *dd = pd->port_dd; |
| 1099 | int ret; |
| 1100 | |
| 1101 | /* |
| 1102 | * when we map the PIO bufferavail registers, we want to map them as |
| 1103 | * readonly, no write possible. |
| 1104 | * |
| 1105 | * kmalloc'ed memory, physically contiguous, one page only, readonly |
| 1106 | */ |
| 1107 | |
| 1108 | if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) { |
| 1109 | dev_info(&dd->pcidev->dev, "FAIL on pioavailregs_dma: " |
| 1110 | "reqlen %lx > actual %lx\n", |
| 1111 | vma->vm_end - vma->vm_start, |
| 1112 | (unsigned long) PAGE_SIZE); |
| 1113 | ret = -EFAULT; |
| 1114 | goto bail; |
| 1115 | } |
| 1116 | |
| 1117 | if (vma->vm_flags & VM_WRITE) { |
| 1118 | dev_info(&dd->pcidev->dev, |
| 1119 | "Can't map pioavailregs as writable (flags=%lx)\n", |
| 1120 | vma->vm_flags); |
| 1121 | ret = -EPERM; |
| 1122 | goto bail; |
| 1123 | } |
| 1124 | |
| 1125 | /* don't allow them to later change with mprotect */ |
| 1126 | vma->vm_flags &= ~VM_MAYWRITE; |
| 1127 | |
| 1128 | ret = remap_pfn_range(vma, vma->vm_start, |
| 1129 | dd->ipath_pioavailregs_phys >> PAGE_SHIFT, |
| 1130 | PAGE_SIZE, vma->vm_page_prot); |
| 1131 | bail: |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * ipath_mmap - mmap various structures into user space |
| 1137 | * @fp: the file pointer |
| 1138 | * @vma: the VM area |
| 1139 | * |
| 1140 | * We use this to have a shared buffer between the kernel and the user code |
| 1141 | * for the rcvhdr queue, egr buffers, and the per-port user regs and pio |
| 1142 | * buffers in the chip. We have the open and close entries so we can bump |
| 1143 | * the ref count and keep the driver from being unloaded while still mapped. |
| 1144 | */ |
| 1145 | static int ipath_mmap(struct file *fp, struct vm_area_struct *vma) |
| 1146 | { |
| 1147 | struct ipath_portdata *pd; |
| 1148 | struct ipath_devdata *dd; |
| 1149 | u64 pgaddr, ureg; |
| 1150 | int ret; |
| 1151 | |
| 1152 | pd = port_fp(fp); |
| 1153 | dd = pd->port_dd; |
| 1154 | /* |
| 1155 | * This is the ipath_do_user_init() code, mapping the shared buffers |
| 1156 | * into the user process. The address referred to by vm_pgoff is the |
| 1157 | * virtual, not physical, address; we only do one mmap for each |
| 1158 | * space mapped. |
| 1159 | */ |
| 1160 | pgaddr = vma->vm_pgoff << PAGE_SHIFT; |
| 1161 | |
| 1162 | /* |
| 1163 | * note that ureg does *NOT* have the kregvirt as part of it, to be |
| 1164 | * sure that for 32 bit programs, we don't end up trying to map a > |
| 1165 | * 44 address. Has to match ipath_get_base_info() code that sets |
| 1166 | * __spi_uregbase |
| 1167 | */ |
| 1168 | |
| 1169 | ureg = dd->ipath_uregbase + dd->ipath_palign * pd->port_port; |
| 1170 | |
Bryan O'Sullivan | 0ed9a4a | 2006-07-01 04:36:01 -0700 | [diff] [blame^] | 1171 | ipath_cdbg(MM, "pgaddr %llx vm_start=%lx len %lx port %u:%u\n", |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1172 | (unsigned long long) pgaddr, vma->vm_start, |
Bryan O'Sullivan | 0ed9a4a | 2006-07-01 04:36:01 -0700 | [diff] [blame^] | 1173 | vma->vm_end - vma->vm_start, dd->ipath_unit, |
| 1174 | pd->port_port); |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1175 | |
| 1176 | if (pgaddr == ureg) |
| 1177 | ret = mmap_ureg(vma, dd, ureg); |
| 1178 | else if (pgaddr == pd->port_piobufs) |
| 1179 | ret = mmap_piobufs(vma, dd, pd); |
| 1180 | else if (pgaddr == (u64) pd->port_rcvegr_phys) |
| 1181 | ret = mmap_rcvegrbufs(vma, pd); |
| 1182 | else if (pgaddr == (u64) pd->port_rcvhdrq_phys) |
| 1183 | ret = mmap_rcvhdrq(vma, pd); |
| 1184 | else if (pgaddr == dd->ipath_pioavailregs_phys) |
| 1185 | ret = mmap_pioavailregs(vma, pd); |
| 1186 | else |
| 1187 | ret = -EINVAL; |
| 1188 | |
| 1189 | vma->vm_private_data = NULL; |
| 1190 | |
| 1191 | if (ret < 0) |
| 1192 | dev_info(&dd->pcidev->dev, |
| 1193 | "Failure %d on addr %lx, off %lx\n", |
| 1194 | -ret, vma->vm_start, vma->vm_pgoff); |
| 1195 | |
| 1196 | return ret; |
| 1197 | } |
| 1198 | |
| 1199 | static unsigned int ipath_poll(struct file *fp, |
| 1200 | struct poll_table_struct *pt) |
| 1201 | { |
| 1202 | struct ipath_portdata *pd; |
| 1203 | u32 head, tail; |
| 1204 | int bit; |
| 1205 | struct ipath_devdata *dd; |
| 1206 | |
| 1207 | pd = port_fp(fp); |
| 1208 | dd = pd->port_dd; |
| 1209 | |
| 1210 | bit = pd->port_port + INFINIPATH_R_INTRAVAIL_SHIFT; |
| 1211 | set_bit(bit, &dd->ipath_rcvctrl); |
| 1212 | |
| 1213 | /* |
| 1214 | * Before blocking, make sure that head is still == tail, |
| 1215 | * reading from the chip, so we can be sure the interrupt |
| 1216 | * enable has made it to the chip. If not equal, disable |
| 1217 | * interrupt again and return immediately. This avoids races, |
| 1218 | * and the overhead of the chip read doesn't matter much at |
| 1219 | * this point, since we are waiting for something anyway. |
| 1220 | */ |
| 1221 | |
| 1222 | ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, |
| 1223 | dd->ipath_rcvctrl); |
| 1224 | |
| 1225 | head = ipath_read_ureg32(dd, ur_rcvhdrhead, pd->port_port); |
| 1226 | tail = ipath_read_ureg32(dd, ur_rcvhdrtail, pd->port_port); |
| 1227 | |
| 1228 | if (tail == head) { |
| 1229 | set_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag); |
Bryan O'Sullivan | 9dcc0e5 | 2006-05-23 11:32:35 -0700 | [diff] [blame] | 1230 | if(dd->ipath_rhdrhead_intr_off) /* arm rcv interrupt */ |
| 1231 | (void)ipath_write_ureg(dd, ur_rcvhdrhead, |
| 1232 | dd->ipath_rhdrhead_intr_off |
| 1233 | | head, pd->port_port); |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1234 | poll_wait(fp, &pd->port_wait, pt); |
| 1235 | |
| 1236 | if (test_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag)) { |
| 1237 | /* timed out, no packets received */ |
| 1238 | clear_bit(IPATH_PORT_WAITING_RCV, &pd->port_flag); |
| 1239 | pd->port_rcvwait_to++; |
| 1240 | } |
| 1241 | } |
| 1242 | else { |
| 1243 | /* it's already happened; don't do wait_event overhead */ |
| 1244 | pd->port_rcvnowait++; |
| 1245 | } |
| 1246 | |
| 1247 | clear_bit(bit, &dd->ipath_rcvctrl); |
| 1248 | ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl, |
| 1249 | dd->ipath_rcvctrl); |
| 1250 | |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | static int try_alloc_port(struct ipath_devdata *dd, int port, |
| 1255 | struct file *fp) |
| 1256 | { |
| 1257 | int ret; |
| 1258 | |
| 1259 | if (!dd->ipath_pd[port]) { |
| 1260 | void *p, *ptmp; |
| 1261 | |
| 1262 | p = kzalloc(sizeof(struct ipath_portdata), GFP_KERNEL); |
| 1263 | |
| 1264 | /* |
| 1265 | * Allocate memory for use in ipath_tid_update() just once |
| 1266 | * at open, not per call. Reduces cost of expected send |
| 1267 | * setup. |
| 1268 | */ |
| 1269 | ptmp = kmalloc(dd->ipath_rcvtidcnt * sizeof(u16) + |
| 1270 | dd->ipath_rcvtidcnt * sizeof(struct page **), |
| 1271 | GFP_KERNEL); |
| 1272 | if (!p || !ptmp) { |
| 1273 | ipath_dev_err(dd, "Unable to allocate portdata " |
| 1274 | "memory, failing open\n"); |
| 1275 | ret = -ENOMEM; |
| 1276 | kfree(p); |
| 1277 | kfree(ptmp); |
| 1278 | goto bail; |
| 1279 | } |
| 1280 | dd->ipath_pd[port] = p; |
| 1281 | dd->ipath_pd[port]->port_port = port; |
| 1282 | dd->ipath_pd[port]->port_dd = dd; |
| 1283 | dd->ipath_pd[port]->port_tid_pg_list = ptmp; |
| 1284 | init_waitqueue_head(&dd->ipath_pd[port]->port_wait); |
| 1285 | } |
| 1286 | if (!dd->ipath_pd[port]->port_cnt) { |
| 1287 | dd->ipath_pd[port]->port_cnt = 1; |
| 1288 | fp->private_data = (void *) dd->ipath_pd[port]; |
| 1289 | ipath_cdbg(PROC, "%s[%u] opened unit:port %u:%u\n", |
| 1290 | current->comm, current->pid, dd->ipath_unit, |
| 1291 | port); |
| 1292 | dd->ipath_pd[port]->port_pid = current->pid; |
| 1293 | strncpy(dd->ipath_pd[port]->port_comm, current->comm, |
| 1294 | sizeof(dd->ipath_pd[port]->port_comm)); |
| 1295 | ipath_stats.sps_ports++; |
| 1296 | ret = 0; |
| 1297 | goto bail; |
| 1298 | } |
| 1299 | ret = -EBUSY; |
| 1300 | |
| 1301 | bail: |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | static inline int usable(struct ipath_devdata *dd) |
| 1306 | { |
| 1307 | return dd && |
| 1308 | (dd->ipath_flags & IPATH_PRESENT) && |
| 1309 | dd->ipath_kregbase && |
| 1310 | dd->ipath_lid && |
| 1311 | !(dd->ipath_flags & (IPATH_LINKDOWN | IPATH_DISABLED |
| 1312 | | IPATH_LINKUNK)); |
| 1313 | } |
| 1314 | |
| 1315 | static int find_free_port(int unit, struct file *fp) |
| 1316 | { |
| 1317 | struct ipath_devdata *dd = ipath_lookup(unit); |
| 1318 | int ret, i; |
| 1319 | |
| 1320 | if (!dd) { |
| 1321 | ret = -ENODEV; |
| 1322 | goto bail; |
| 1323 | } |
| 1324 | |
| 1325 | if (!usable(dd)) { |
| 1326 | ret = -ENETDOWN; |
| 1327 | goto bail; |
| 1328 | } |
| 1329 | |
| 1330 | for (i = 0; i < dd->ipath_cfgports; i++) { |
| 1331 | ret = try_alloc_port(dd, i, fp); |
| 1332 | if (ret != -EBUSY) |
| 1333 | goto bail; |
| 1334 | } |
| 1335 | ret = -EBUSY; |
| 1336 | |
| 1337 | bail: |
| 1338 | return ret; |
| 1339 | } |
| 1340 | |
| 1341 | static int find_best_unit(struct file *fp) |
| 1342 | { |
| 1343 | int ret = 0, i, prefunit = -1, devmax; |
| 1344 | int maxofallports, npresent, nup; |
| 1345 | int ndev; |
| 1346 | |
| 1347 | (void) ipath_count_units(&npresent, &nup, &maxofallports); |
| 1348 | |
| 1349 | /* |
| 1350 | * This code is present to allow a knowledgeable person to |
| 1351 | * specify the layout of processes to processors before opening |
| 1352 | * this driver, and then we'll assign the process to the "closest" |
| 1353 | * HT-400 to that processor (we assume reasonable connectivity, |
| 1354 | * for now). This code assumes that if affinity has been set |
| 1355 | * before this point, that at most one cpu is set; for now this |
| 1356 | * is reasonable. I check for both cpus_empty() and cpus_full(), |
| 1357 | * in case some kernel variant sets none of the bits when no |
| 1358 | * affinity is set. 2.6.11 and 12 kernels have all present |
| 1359 | * cpus set. Some day we'll have to fix it up further to handle |
| 1360 | * a cpu subset. This algorithm fails for two HT-400's connected |
| 1361 | * in tunnel fashion. Eventually this needs real topology |
| 1362 | * information. There may be some issues with dual core numbering |
| 1363 | * as well. This needs more work prior to release. |
| 1364 | */ |
| 1365 | if (!cpus_empty(current->cpus_allowed) && |
| 1366 | !cpus_full(current->cpus_allowed)) { |
| 1367 | int ncpus = num_online_cpus(), curcpu = -1; |
| 1368 | for (i = 0; i < ncpus; i++) |
| 1369 | if (cpu_isset(i, current->cpus_allowed)) { |
| 1370 | ipath_cdbg(PROC, "%s[%u] affinity set for " |
| 1371 | "cpu %d\n", current->comm, |
| 1372 | current->pid, i); |
| 1373 | curcpu = i; |
| 1374 | } |
| 1375 | if (curcpu != -1) { |
| 1376 | if (npresent) { |
| 1377 | prefunit = curcpu / (ncpus / npresent); |
| 1378 | ipath_dbg("%s[%u] %d chips, %d cpus, " |
| 1379 | "%d cpus/chip, select unit %d\n", |
| 1380 | current->comm, current->pid, |
| 1381 | npresent, ncpus, ncpus / npresent, |
| 1382 | prefunit); |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * user ports start at 1, kernel port is 0 |
| 1389 | * For now, we do round-robin access across all chips |
| 1390 | */ |
| 1391 | |
| 1392 | if (prefunit != -1) |
| 1393 | devmax = prefunit + 1; |
| 1394 | else |
| 1395 | devmax = ipath_count_units(NULL, NULL, NULL); |
| 1396 | recheck: |
| 1397 | for (i = 1; i < maxofallports; i++) { |
| 1398 | for (ndev = prefunit != -1 ? prefunit : 0; ndev < devmax; |
| 1399 | ndev++) { |
| 1400 | struct ipath_devdata *dd = ipath_lookup(ndev); |
| 1401 | |
| 1402 | if (!usable(dd)) |
| 1403 | continue; /* can't use this unit */ |
| 1404 | if (i >= dd->ipath_cfgports) |
| 1405 | /* |
| 1406 | * Maxed out on users of this unit. Try |
| 1407 | * next. |
| 1408 | */ |
| 1409 | continue; |
| 1410 | ret = try_alloc_port(dd, i, fp); |
| 1411 | if (!ret) |
| 1412 | goto done; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | if (npresent) { |
| 1417 | if (nup == 0) { |
| 1418 | ret = -ENETDOWN; |
| 1419 | ipath_dbg("No ports available (none initialized " |
| 1420 | "and ready)\n"); |
| 1421 | } else { |
| 1422 | if (prefunit > 0) { |
| 1423 | /* if started above 0, retry from 0 */ |
| 1424 | ipath_cdbg(PROC, |
| 1425 | "%s[%u] no ports on prefunit " |
| 1426 | "%d, clear and re-check\n", |
| 1427 | current->comm, current->pid, |
| 1428 | prefunit); |
| 1429 | devmax = ipath_count_units(NULL, NULL, |
| 1430 | NULL); |
| 1431 | prefunit = -1; |
| 1432 | goto recheck; |
| 1433 | } |
| 1434 | ret = -EBUSY; |
| 1435 | ipath_dbg("No ports available\n"); |
| 1436 | } |
| 1437 | } else { |
| 1438 | ret = -ENXIO; |
| 1439 | ipath_dbg("No boards found\n"); |
| 1440 | } |
| 1441 | |
| 1442 | done: |
| 1443 | return ret; |
| 1444 | } |
| 1445 | |
| 1446 | static int ipath_open(struct inode *in, struct file *fp) |
| 1447 | { |
Bryan O'Sullivan | a2acb2f | 2006-07-01 04:35:52 -0700 | [diff] [blame] | 1448 | int ret, user_minor; |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1449 | |
| 1450 | mutex_lock(&ipath_mutex); |
| 1451 | |
Bryan O'Sullivan | a2acb2f | 2006-07-01 04:35:52 -0700 | [diff] [blame] | 1452 | user_minor = iminor(in) - IPATH_USER_MINOR_BASE; |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1453 | ipath_cdbg(VERBOSE, "open on dev %lx (minor %d)\n", |
Bryan O'Sullivan | a2acb2f | 2006-07-01 04:35:52 -0700 | [diff] [blame] | 1454 | (long)in->i_rdev, user_minor); |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1455 | |
Bryan O'Sullivan | a2acb2f | 2006-07-01 04:35:52 -0700 | [diff] [blame] | 1456 | if (user_minor) |
| 1457 | ret = find_free_port(user_minor - 1, fp); |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1458 | else |
| 1459 | ret = find_best_unit(fp); |
| 1460 | |
| 1461 | mutex_unlock(&ipath_mutex); |
| 1462 | return ret; |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * unlock_exptid - unlock any expected TID entries port still had in use |
| 1467 | * @pd: port |
| 1468 | * |
| 1469 | * We don't actually update the chip here, because we do a bulk update |
| 1470 | * below, using ipath_f_clear_tids. |
| 1471 | */ |
| 1472 | static void unlock_expected_tids(struct ipath_portdata *pd) |
| 1473 | { |
| 1474 | struct ipath_devdata *dd = pd->port_dd; |
| 1475 | int port_tidbase = pd->port_port * dd->ipath_rcvtidcnt; |
| 1476 | int i, cnt = 0, maxtid = port_tidbase + dd->ipath_rcvtidcnt; |
| 1477 | |
| 1478 | ipath_cdbg(VERBOSE, "Port %u unlocking any locked expTID pages\n", |
| 1479 | pd->port_port); |
| 1480 | for (i = port_tidbase; i < maxtid; i++) { |
| 1481 | if (!dd->ipath_pageshadow[i]) |
| 1482 | continue; |
| 1483 | |
| 1484 | ipath_release_user_pages_on_close(&dd->ipath_pageshadow[i], |
| 1485 | 1); |
| 1486 | dd->ipath_pageshadow[i] = NULL; |
| 1487 | cnt++; |
| 1488 | ipath_stats.sps_pageunlocks++; |
| 1489 | } |
| 1490 | if (cnt) |
| 1491 | ipath_cdbg(VERBOSE, "Port %u locked %u expTID entries\n", |
| 1492 | pd->port_port, cnt); |
| 1493 | |
| 1494 | if (ipath_stats.sps_pagelocks || ipath_stats.sps_pageunlocks) |
| 1495 | ipath_cdbg(VERBOSE, "%llu pages locked, %llu unlocked\n", |
| 1496 | (unsigned long long) ipath_stats.sps_pagelocks, |
| 1497 | (unsigned long long) |
| 1498 | ipath_stats.sps_pageunlocks); |
| 1499 | } |
| 1500 | |
| 1501 | static int ipath_close(struct inode *in, struct file *fp) |
| 1502 | { |
| 1503 | int ret = 0; |
| 1504 | struct ipath_portdata *pd; |
| 1505 | struct ipath_devdata *dd; |
| 1506 | unsigned port; |
| 1507 | |
| 1508 | ipath_cdbg(VERBOSE, "close on dev %lx, private data %p\n", |
| 1509 | (long)in->i_rdev, fp->private_data); |
| 1510 | |
| 1511 | mutex_lock(&ipath_mutex); |
| 1512 | |
| 1513 | pd = port_fp(fp); |
| 1514 | port = pd->port_port; |
| 1515 | fp->private_data = NULL; |
| 1516 | dd = pd->port_dd; |
| 1517 | |
| 1518 | if (pd->port_hdrqfull) { |
| 1519 | ipath_cdbg(PROC, "%s[%u] had %u rcvhdrqfull errors " |
| 1520 | "during run\n", pd->port_comm, pd->port_pid, |
| 1521 | pd->port_hdrqfull); |
| 1522 | pd->port_hdrqfull = 0; |
| 1523 | } |
| 1524 | |
| 1525 | if (pd->port_rcvwait_to || pd->port_piowait_to |
| 1526 | || pd->port_rcvnowait || pd->port_pionowait) { |
| 1527 | ipath_cdbg(VERBOSE, "port%u, %u rcv, %u pio wait timeo; " |
| 1528 | "%u rcv %u, pio already\n", |
| 1529 | pd->port_port, pd->port_rcvwait_to, |
| 1530 | pd->port_piowait_to, pd->port_rcvnowait, |
| 1531 | pd->port_pionowait); |
| 1532 | pd->port_rcvwait_to = pd->port_piowait_to = |
| 1533 | pd->port_rcvnowait = pd->port_pionowait = 0; |
| 1534 | } |
| 1535 | if (pd->port_flag) { |
| 1536 | ipath_dbg("port %u port_flag still set to 0x%lx\n", |
| 1537 | pd->port_port, pd->port_flag); |
| 1538 | pd->port_flag = 0; |
| 1539 | } |
| 1540 | |
| 1541 | if (dd->ipath_kregbase) { |
| 1542 | if (pd->port_rcvhdrtail_uaddr) { |
| 1543 | pd->port_rcvhdrtail_uaddr = 0; |
| 1544 | pd->port_rcvhdrtail_kvaddr = NULL; |
| 1545 | ipath_release_user_pages_on_close( |
| 1546 | &pd->port_rcvhdrtail_pagep, 1); |
| 1547 | pd->port_rcvhdrtail_pagep = NULL; |
| 1548 | ipath_stats.sps_pageunlocks++; |
| 1549 | } |
| 1550 | ipath_write_kreg_port( |
| 1551 | dd, dd->ipath_kregs->kr_rcvhdrtailaddr, |
| 1552 | port, 0ULL); |
| 1553 | ipath_write_kreg_port( |
| 1554 | dd, dd->ipath_kregs->kr_rcvhdraddr, |
| 1555 | pd->port_port, 0); |
| 1556 | |
| 1557 | /* clean up the pkeys for this port user */ |
| 1558 | ipath_clean_part_key(pd, dd); |
| 1559 | |
| 1560 | if (port < dd->ipath_cfgports) { |
| 1561 | int i = dd->ipath_pbufsport * (port - 1); |
| 1562 | ipath_disarm_piobufs(dd, i, dd->ipath_pbufsport); |
| 1563 | |
| 1564 | /* atomically clear receive enable port. */ |
| 1565 | clear_bit(INFINIPATH_R_PORTENABLE_SHIFT + port, |
| 1566 | &dd->ipath_rcvctrl); |
| 1567 | ipath_write_kreg( |
| 1568 | dd, |
| 1569 | dd->ipath_kregs->kr_rcvctrl, |
| 1570 | dd->ipath_rcvctrl); |
| 1571 | |
| 1572 | if (dd->ipath_pageshadow) |
| 1573 | unlock_expected_tids(pd); |
| 1574 | ipath_stats.sps_ports--; |
| 1575 | ipath_cdbg(PROC, "%s[%u] closed port %u:%u\n", |
| 1576 | pd->port_comm, pd->port_pid, |
| 1577 | dd->ipath_unit, port); |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | pd->port_cnt = 0; |
| 1582 | pd->port_pid = 0; |
| 1583 | |
| 1584 | dd->ipath_f_clear_tids(dd, pd->port_port); |
| 1585 | |
| 1586 | ipath_free_pddata(dd, pd->port_port, 0); |
| 1587 | |
| 1588 | mutex_unlock(&ipath_mutex); |
| 1589 | |
| 1590 | return ret; |
| 1591 | } |
| 1592 | |
| 1593 | static int ipath_port_info(struct ipath_portdata *pd, |
| 1594 | struct ipath_port_info __user *uinfo) |
| 1595 | { |
| 1596 | struct ipath_port_info info; |
| 1597 | int nup; |
| 1598 | int ret; |
| 1599 | |
| 1600 | (void) ipath_count_units(NULL, &nup, NULL); |
| 1601 | info.num_active = nup; |
| 1602 | info.unit = pd->port_dd->ipath_unit; |
| 1603 | info.port = pd->port_port; |
| 1604 | |
| 1605 | if (copy_to_user(uinfo, &info, sizeof(info))) { |
| 1606 | ret = -EFAULT; |
| 1607 | goto bail; |
| 1608 | } |
| 1609 | ret = 0; |
| 1610 | |
| 1611 | bail: |
| 1612 | return ret; |
| 1613 | } |
| 1614 | |
| 1615 | static ssize_t ipath_write(struct file *fp, const char __user *data, |
| 1616 | size_t count, loff_t *off) |
| 1617 | { |
| 1618 | const struct ipath_cmd __user *ucmd; |
| 1619 | struct ipath_portdata *pd; |
| 1620 | const void __user *src; |
| 1621 | size_t consumed, copy; |
| 1622 | struct ipath_cmd cmd; |
| 1623 | ssize_t ret = 0; |
| 1624 | void *dest; |
| 1625 | |
| 1626 | if (count < sizeof(cmd.type)) { |
| 1627 | ret = -EINVAL; |
| 1628 | goto bail; |
| 1629 | } |
| 1630 | |
| 1631 | ucmd = (const struct ipath_cmd __user *) data; |
| 1632 | |
| 1633 | if (copy_from_user(&cmd.type, &ucmd->type, sizeof(cmd.type))) { |
| 1634 | ret = -EFAULT; |
| 1635 | goto bail; |
| 1636 | } |
| 1637 | |
| 1638 | consumed = sizeof(cmd.type); |
| 1639 | |
| 1640 | switch (cmd.type) { |
| 1641 | case IPATH_CMD_USER_INIT: |
| 1642 | copy = sizeof(cmd.cmd.user_info); |
| 1643 | dest = &cmd.cmd.user_info; |
| 1644 | src = &ucmd->cmd.user_info; |
| 1645 | break; |
| 1646 | case IPATH_CMD_RECV_CTRL: |
| 1647 | copy = sizeof(cmd.cmd.recv_ctrl); |
| 1648 | dest = &cmd.cmd.recv_ctrl; |
| 1649 | src = &ucmd->cmd.recv_ctrl; |
| 1650 | break; |
| 1651 | case IPATH_CMD_PORT_INFO: |
| 1652 | copy = sizeof(cmd.cmd.port_info); |
| 1653 | dest = &cmd.cmd.port_info; |
| 1654 | src = &ucmd->cmd.port_info; |
| 1655 | break; |
| 1656 | case IPATH_CMD_TID_UPDATE: |
| 1657 | case IPATH_CMD_TID_FREE: |
| 1658 | copy = sizeof(cmd.cmd.tid_info); |
| 1659 | dest = &cmd.cmd.tid_info; |
| 1660 | src = &ucmd->cmd.tid_info; |
| 1661 | break; |
| 1662 | case IPATH_CMD_SET_PART_KEY: |
| 1663 | copy = sizeof(cmd.cmd.part_key); |
| 1664 | dest = &cmd.cmd.part_key; |
| 1665 | src = &ucmd->cmd.part_key; |
| 1666 | break; |
| 1667 | default: |
| 1668 | ret = -EINVAL; |
| 1669 | goto bail; |
| 1670 | } |
| 1671 | |
| 1672 | if ((count - consumed) < copy) { |
| 1673 | ret = -EINVAL; |
| 1674 | goto bail; |
| 1675 | } |
| 1676 | |
| 1677 | if (copy_from_user(dest, src, copy)) { |
| 1678 | ret = -EFAULT; |
| 1679 | goto bail; |
| 1680 | } |
| 1681 | |
| 1682 | consumed += copy; |
| 1683 | pd = port_fp(fp); |
| 1684 | |
| 1685 | switch (cmd.type) { |
| 1686 | case IPATH_CMD_USER_INIT: |
| 1687 | ret = ipath_do_user_init(pd, &cmd.cmd.user_info); |
| 1688 | if (ret < 0) |
| 1689 | goto bail; |
| 1690 | ret = ipath_get_base_info( |
| 1691 | pd, (void __user *) (unsigned long) |
| 1692 | cmd.cmd.user_info.spu_base_info, |
| 1693 | cmd.cmd.user_info.spu_base_info_size); |
| 1694 | break; |
| 1695 | case IPATH_CMD_RECV_CTRL: |
| 1696 | ret = ipath_manage_rcvq(pd, cmd.cmd.recv_ctrl); |
| 1697 | break; |
| 1698 | case IPATH_CMD_PORT_INFO: |
| 1699 | ret = ipath_port_info(pd, |
| 1700 | (struct ipath_port_info __user *) |
| 1701 | (unsigned long) cmd.cmd.port_info); |
| 1702 | break; |
| 1703 | case IPATH_CMD_TID_UPDATE: |
| 1704 | ret = ipath_tid_update(pd, &cmd.cmd.tid_info); |
| 1705 | break; |
| 1706 | case IPATH_CMD_TID_FREE: |
| 1707 | ret = ipath_tid_free(pd, &cmd.cmd.tid_info); |
| 1708 | break; |
| 1709 | case IPATH_CMD_SET_PART_KEY: |
| 1710 | ret = ipath_set_part_key(pd, cmd.cmd.part_key); |
| 1711 | break; |
| 1712 | } |
| 1713 | |
| 1714 | if (ret >= 0) |
| 1715 | ret = consumed; |
| 1716 | |
| 1717 | bail: |
| 1718 | return ret; |
| 1719 | } |
| 1720 | |
| 1721 | static struct class *ipath_class; |
| 1722 | |
| 1723 | static int init_cdev(int minor, char *name, struct file_operations *fops, |
| 1724 | struct cdev **cdevp, struct class_device **class_devp) |
| 1725 | { |
| 1726 | const dev_t dev = MKDEV(IPATH_MAJOR, minor); |
| 1727 | struct cdev *cdev = NULL; |
| 1728 | struct class_device *class_dev = NULL; |
| 1729 | int ret; |
| 1730 | |
| 1731 | cdev = cdev_alloc(); |
| 1732 | if (!cdev) { |
| 1733 | printk(KERN_ERR IPATH_DRV_NAME |
| 1734 | ": Could not allocate cdev for minor %d, %s\n", |
| 1735 | minor, name); |
| 1736 | ret = -ENOMEM; |
| 1737 | goto done; |
| 1738 | } |
| 1739 | |
| 1740 | cdev->owner = THIS_MODULE; |
| 1741 | cdev->ops = fops; |
| 1742 | kobject_set_name(&cdev->kobj, name); |
| 1743 | |
| 1744 | ret = cdev_add(cdev, dev, 1); |
| 1745 | if (ret < 0) { |
| 1746 | printk(KERN_ERR IPATH_DRV_NAME |
| 1747 | ": Could not add cdev for minor %d, %s (err %d)\n", |
| 1748 | minor, name, -ret); |
| 1749 | goto err_cdev; |
| 1750 | } |
| 1751 | |
| 1752 | class_dev = class_device_create(ipath_class, NULL, dev, NULL, name); |
| 1753 | |
| 1754 | if (IS_ERR(class_dev)) { |
| 1755 | ret = PTR_ERR(class_dev); |
| 1756 | printk(KERN_ERR IPATH_DRV_NAME ": Could not create " |
| 1757 | "class_dev for minor %d, %s (err %d)\n", |
| 1758 | minor, name, -ret); |
| 1759 | goto err_cdev; |
| 1760 | } |
| 1761 | |
| 1762 | goto done; |
| 1763 | |
| 1764 | err_cdev: |
| 1765 | cdev_del(cdev); |
| 1766 | cdev = NULL; |
| 1767 | |
| 1768 | done: |
| 1769 | if (ret >= 0) { |
| 1770 | *cdevp = cdev; |
| 1771 | *class_devp = class_dev; |
| 1772 | } else { |
| 1773 | *cdevp = NULL; |
| 1774 | *class_devp = NULL; |
| 1775 | } |
| 1776 | |
| 1777 | return ret; |
| 1778 | } |
| 1779 | |
| 1780 | int ipath_cdev_init(int minor, char *name, struct file_operations *fops, |
| 1781 | struct cdev **cdevp, struct class_device **class_devp) |
| 1782 | { |
| 1783 | return init_cdev(minor, name, fops, cdevp, class_devp); |
| 1784 | } |
| 1785 | |
| 1786 | static void cleanup_cdev(struct cdev **cdevp, |
| 1787 | struct class_device **class_devp) |
| 1788 | { |
| 1789 | struct class_device *class_dev = *class_devp; |
| 1790 | |
| 1791 | if (class_dev) { |
| 1792 | class_device_unregister(class_dev); |
| 1793 | *class_devp = NULL; |
| 1794 | } |
| 1795 | |
| 1796 | if (*cdevp) { |
| 1797 | cdev_del(*cdevp); |
| 1798 | *cdevp = NULL; |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | void ipath_cdev_cleanup(struct cdev **cdevp, |
| 1803 | struct class_device **class_devp) |
| 1804 | { |
| 1805 | cleanup_cdev(cdevp, class_devp); |
| 1806 | } |
| 1807 | |
| 1808 | static struct cdev *wildcard_cdev; |
| 1809 | static struct class_device *wildcard_class_dev; |
| 1810 | |
| 1811 | static const dev_t dev = MKDEV(IPATH_MAJOR, 0); |
| 1812 | |
| 1813 | static int user_init(void) |
| 1814 | { |
| 1815 | int ret; |
| 1816 | |
| 1817 | ret = register_chrdev_region(dev, IPATH_NMINORS, IPATH_DRV_NAME); |
| 1818 | if (ret < 0) { |
| 1819 | printk(KERN_ERR IPATH_DRV_NAME ": Could not register " |
| 1820 | "chrdev region (err %d)\n", -ret); |
| 1821 | goto done; |
| 1822 | } |
| 1823 | |
| 1824 | ipath_class = class_create(THIS_MODULE, IPATH_DRV_NAME); |
| 1825 | |
| 1826 | if (IS_ERR(ipath_class)) { |
| 1827 | ret = PTR_ERR(ipath_class); |
| 1828 | printk(KERN_ERR IPATH_DRV_NAME ": Could not create " |
| 1829 | "device class (err %d)\n", -ret); |
| 1830 | goto bail; |
| 1831 | } |
| 1832 | |
| 1833 | goto done; |
| 1834 | bail: |
| 1835 | unregister_chrdev_region(dev, IPATH_NMINORS); |
| 1836 | done: |
| 1837 | return ret; |
| 1838 | } |
| 1839 | |
| 1840 | static void user_cleanup(void) |
| 1841 | { |
| 1842 | if (ipath_class) { |
| 1843 | class_destroy(ipath_class); |
| 1844 | ipath_class = NULL; |
| 1845 | } |
| 1846 | |
| 1847 | unregister_chrdev_region(dev, IPATH_NMINORS); |
| 1848 | } |
| 1849 | |
| 1850 | static atomic_t user_count = ATOMIC_INIT(0); |
| 1851 | static atomic_t user_setup = ATOMIC_INIT(0); |
| 1852 | |
| 1853 | int ipath_user_add(struct ipath_devdata *dd) |
| 1854 | { |
| 1855 | char name[10]; |
| 1856 | int ret; |
| 1857 | |
| 1858 | if (atomic_inc_return(&user_count) == 1) { |
| 1859 | ret = user_init(); |
| 1860 | if (ret < 0) { |
| 1861 | ipath_dev_err(dd, "Unable to set up user support: " |
| 1862 | "error %d\n", -ret); |
| 1863 | goto bail; |
| 1864 | } |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1865 | ret = init_cdev(0, "ipath", &ipath_file_ops, &wildcard_cdev, |
| 1866 | &wildcard_class_dev); |
| 1867 | if (ret < 0) { |
| 1868 | ipath_dev_err(dd, "Could not create wildcard " |
| 1869 | "minor: error %d\n", -ret); |
Bryan O'Sullivan | a2acb2f | 2006-07-01 04:35:52 -0700 | [diff] [blame] | 1870 | goto bail_sma; |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | atomic_set(&user_setup, 1); |
| 1874 | } |
| 1875 | |
| 1876 | snprintf(name, sizeof(name), "ipath%d", dd->ipath_unit); |
| 1877 | |
| 1878 | ret = init_cdev(dd->ipath_unit + 1, name, &ipath_file_ops, |
Bryan O'Sullivan | a2acb2f | 2006-07-01 04:35:52 -0700 | [diff] [blame] | 1879 | &dd->user_cdev, &dd->user_class_dev); |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1880 | if (ret < 0) |
| 1881 | ipath_dev_err(dd, "Could not create user minor %d, %s\n", |
| 1882 | dd->ipath_unit + 1, name); |
| 1883 | |
| 1884 | goto bail; |
| 1885 | |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1886 | bail_sma: |
| 1887 | user_cleanup(); |
| 1888 | bail: |
| 1889 | return ret; |
| 1890 | } |
| 1891 | |
Bryan O'Sullivan | a2acb2f | 2006-07-01 04:35:52 -0700 | [diff] [blame] | 1892 | void ipath_user_remove(struct ipath_devdata *dd) |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1893 | { |
Bryan O'Sullivan | a2acb2f | 2006-07-01 04:35:52 -0700 | [diff] [blame] | 1894 | cleanup_cdev(&dd->user_cdev, &dd->user_class_dev); |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1895 | |
| 1896 | if (atomic_dec_return(&user_count) == 0) { |
| 1897 | if (atomic_read(&user_setup) == 0) |
| 1898 | goto bail; |
| 1899 | |
| 1900 | cleanup_cdev(&wildcard_cdev, &wildcard_class_dev); |
Bryan O'Sullivan | 7f510b4 | 2006-03-29 15:23:31 -0800 | [diff] [blame] | 1901 | user_cleanup(); |
| 1902 | |
| 1903 | atomic_set(&user_setup, 0); |
| 1904 | } |
| 1905 | bail: |
| 1906 | return; |
| 1907 | } |