Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * SN Platform system controller communication support |
| 3 | * |
| 4 | * This file is subject to the terms and conditions of the GNU General Public |
| 5 | * License. See the file "COPYING" in the main directory of this archive |
| 6 | * for more details. |
| 7 | * |
| 8 | * Copyright (C) 2004 Silicon Graphics, Inc. All rights reserved. |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * System controller communication driver |
| 13 | * |
| 14 | * This driver allows a user process to communicate with the system |
| 15 | * controller (a.k.a. "IRouter") network in an SGI SN system. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/poll.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <asm/sn/io.h> |
| 25 | #include <asm/sn/sn_sal.h> |
| 26 | #include <asm/sn/module.h> |
| 27 | #include <asm/sn/geo.h> |
| 28 | #include <asm/sn/nodepda.h> |
| 29 | #include "snsc.h" |
| 30 | |
| 31 | #define SYSCTL_BASENAME "snsc" |
| 32 | |
| 33 | #define SCDRV_BUFSZ 2048 |
| 34 | #define SCDRV_TIMEOUT 1000 |
| 35 | |
| 36 | static irqreturn_t |
| 37 | scdrv_interrupt(int irq, void *subch_data, struct pt_regs *regs) |
| 38 | { |
| 39 | struct subch_data_s *sd = subch_data; |
| 40 | unsigned long flags; |
| 41 | int status; |
| 42 | |
| 43 | spin_lock_irqsave(&sd->sd_rlock, flags); |
| 44 | spin_lock(&sd->sd_wlock); |
| 45 | status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch); |
| 46 | |
| 47 | if (status > 0) { |
| 48 | if (status & SAL_IROUTER_INTR_RECV) { |
| 49 | wake_up(&sd->sd_rq); |
| 50 | } |
| 51 | if (status & SAL_IROUTER_INTR_XMIT) { |
| 52 | ia64_sn_irtr_intr_disable |
| 53 | (sd->sd_nasid, sd->sd_subch, |
| 54 | SAL_IROUTER_INTR_XMIT); |
| 55 | wake_up(&sd->sd_wq); |
| 56 | } |
| 57 | } |
| 58 | spin_unlock(&sd->sd_wlock); |
| 59 | spin_unlock_irqrestore(&sd->sd_rlock, flags); |
| 60 | return IRQ_HANDLED; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * scdrv_open |
| 65 | * |
| 66 | * Reserve a subchannel for system controller communication. |
| 67 | */ |
| 68 | |
| 69 | static int |
| 70 | scdrv_open(struct inode *inode, struct file *file) |
| 71 | { |
| 72 | struct sysctl_data_s *scd; |
| 73 | struct subch_data_s *sd; |
| 74 | int rv; |
| 75 | |
| 76 | /* look up device info for this device file */ |
| 77 | scd = container_of(inode->i_cdev, struct sysctl_data_s, scd_cdev); |
| 78 | |
| 79 | /* allocate memory for subchannel data */ |
| 80 | sd = kmalloc(sizeof (struct subch_data_s), GFP_KERNEL); |
| 81 | if (sd == NULL) { |
| 82 | printk("%s: couldn't allocate subchannel data\n", |
| 83 | __FUNCTION__); |
| 84 | return -ENOMEM; |
| 85 | } |
| 86 | |
| 87 | /* initialize subch_data_s fields */ |
| 88 | memset(sd, 0, sizeof (struct subch_data_s)); |
| 89 | sd->sd_nasid = scd->scd_nasid; |
| 90 | sd->sd_subch = ia64_sn_irtr_open(scd->scd_nasid); |
| 91 | |
| 92 | if (sd->sd_subch < 0) { |
| 93 | kfree(sd); |
| 94 | printk("%s: couldn't allocate subchannel\n", __FUNCTION__); |
| 95 | return -EBUSY; |
| 96 | } |
| 97 | |
| 98 | spin_lock_init(&sd->sd_rlock); |
| 99 | spin_lock_init(&sd->sd_wlock); |
| 100 | init_waitqueue_head(&sd->sd_rq); |
| 101 | init_waitqueue_head(&sd->sd_wq); |
| 102 | sema_init(&sd->sd_rbs, 1); |
| 103 | sema_init(&sd->sd_wbs, 1); |
| 104 | |
| 105 | file->private_data = sd; |
| 106 | |
| 107 | /* hook this subchannel up to the system controller interrupt */ |
| 108 | rv = request_irq(SGI_UART_VECTOR, scdrv_interrupt, |
| 109 | SA_SHIRQ | SA_INTERRUPT, |
| 110 | SYSCTL_BASENAME, sd); |
| 111 | if (rv) { |
| 112 | ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch); |
| 113 | kfree(sd); |
| 114 | printk("%s: irq request failed (%d)\n", __FUNCTION__, rv); |
| 115 | return -EBUSY; |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * scdrv_release |
| 123 | * |
| 124 | * Release a previously-reserved subchannel. |
| 125 | */ |
| 126 | |
| 127 | static int |
| 128 | scdrv_release(struct inode *inode, struct file *file) |
| 129 | { |
| 130 | struct subch_data_s *sd = (struct subch_data_s *) file->private_data; |
| 131 | int rv; |
| 132 | |
| 133 | /* free the interrupt */ |
| 134 | free_irq(SGI_UART_VECTOR, sd); |
| 135 | |
| 136 | /* ask SAL to close the subchannel */ |
| 137 | rv = ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch); |
| 138 | |
| 139 | kfree(sd); |
| 140 | return rv; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * scdrv_read |
| 145 | * |
| 146 | * Called to read bytes from the open IRouter pipe. |
| 147 | * |
| 148 | */ |
| 149 | |
| 150 | static inline int |
| 151 | read_status_check(struct subch_data_s *sd, int *len) |
| 152 | { |
| 153 | return ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch, sd->sd_rb, len); |
| 154 | } |
| 155 | |
| 156 | static ssize_t |
| 157 | scdrv_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos) |
| 158 | { |
| 159 | int status; |
| 160 | int len; |
| 161 | unsigned long flags; |
| 162 | struct subch_data_s *sd = (struct subch_data_s *) file->private_data; |
| 163 | |
| 164 | /* try to get control of the read buffer */ |
| 165 | if (down_trylock(&sd->sd_rbs)) { |
| 166 | /* somebody else has it now; |
| 167 | * if we're non-blocking, then exit... |
| 168 | */ |
| 169 | if (file->f_flags & O_NONBLOCK) { |
| 170 | return -EAGAIN; |
| 171 | } |
| 172 | /* ...or if we want to block, then do so here */ |
| 173 | if (down_interruptible(&sd->sd_rbs)) { |
| 174 | /* something went wrong with wait */ |
| 175 | return -ERESTARTSYS; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* anything to read? */ |
| 180 | len = CHUNKSIZE; |
| 181 | spin_lock_irqsave(&sd->sd_rlock, flags); |
| 182 | status = read_status_check(sd, &len); |
| 183 | |
| 184 | /* if not, and we're blocking I/O, loop */ |
| 185 | while (status < 0) { |
| 186 | DECLARE_WAITQUEUE(wait, current); |
| 187 | |
| 188 | if (file->f_flags & O_NONBLOCK) { |
| 189 | spin_unlock_irqrestore(&sd->sd_rlock, flags); |
| 190 | up(&sd->sd_rbs); |
| 191 | return -EAGAIN; |
| 192 | } |
| 193 | |
| 194 | len = CHUNKSIZE; |
| 195 | set_current_state(TASK_INTERRUPTIBLE); |
| 196 | add_wait_queue(&sd->sd_rq, &wait); |
| 197 | spin_unlock_irqrestore(&sd->sd_rlock, flags); |
| 198 | |
| 199 | schedule_timeout(SCDRV_TIMEOUT); |
| 200 | |
| 201 | remove_wait_queue(&sd->sd_rq, &wait); |
| 202 | if (signal_pending(current)) { |
| 203 | /* wait was interrupted */ |
| 204 | up(&sd->sd_rbs); |
| 205 | return -ERESTARTSYS; |
| 206 | } |
| 207 | |
| 208 | spin_lock_irqsave(&sd->sd_rlock, flags); |
| 209 | status = read_status_check(sd, &len); |
| 210 | } |
| 211 | spin_unlock_irqrestore(&sd->sd_rlock, flags); |
| 212 | |
| 213 | if (len > 0) { |
| 214 | /* we read something in the last read_status_check(); copy |
| 215 | * it out to user space |
| 216 | */ |
| 217 | if (count < len) { |
| 218 | pr_debug("%s: only accepting %d of %d bytes\n", |
| 219 | __FUNCTION__, (int) count, len); |
| 220 | } |
| 221 | len = min((int) count, len); |
| 222 | if (copy_to_user(buf, sd->sd_rb, len)) |
| 223 | len = -EFAULT; |
| 224 | } |
| 225 | |
| 226 | /* release the read buffer and wake anyone who might be |
| 227 | * waiting for it |
| 228 | */ |
| 229 | up(&sd->sd_rbs); |
| 230 | |
| 231 | /* return the number of characters read in */ |
| 232 | return len; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * scdrv_write |
| 237 | * |
| 238 | * Writes a chunk of an IRouter packet (or other system controller data) |
| 239 | * to the system controller. |
| 240 | * |
| 241 | */ |
| 242 | static inline int |
| 243 | write_status_check(struct subch_data_s *sd, int count) |
| 244 | { |
| 245 | return ia64_sn_irtr_send(sd->sd_nasid, sd->sd_subch, sd->sd_wb, count); |
| 246 | } |
| 247 | |
| 248 | static ssize_t |
| 249 | scdrv_write(struct file *file, const char __user *buf, |
| 250 | size_t count, loff_t *f_pos) |
| 251 | { |
| 252 | unsigned long flags; |
| 253 | int status; |
| 254 | struct subch_data_s *sd = (struct subch_data_s *) file->private_data; |
| 255 | |
| 256 | /* try to get control of the write buffer */ |
| 257 | if (down_trylock(&sd->sd_wbs)) { |
| 258 | /* somebody else has it now; |
| 259 | * if we're non-blocking, then exit... |
| 260 | */ |
| 261 | if (file->f_flags & O_NONBLOCK) { |
| 262 | return -EAGAIN; |
| 263 | } |
| 264 | /* ...or if we want to block, then do so here */ |
| 265 | if (down_interruptible(&sd->sd_wbs)) { |
| 266 | /* something went wrong with wait */ |
| 267 | return -ERESTARTSYS; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | count = min((int) count, CHUNKSIZE); |
| 272 | if (copy_from_user(sd->sd_wb, buf, count)) { |
| 273 | up(&sd->sd_wbs); |
| 274 | return -EFAULT; |
| 275 | } |
| 276 | |
| 277 | /* try to send the buffer */ |
| 278 | spin_lock_irqsave(&sd->sd_wlock, flags); |
| 279 | status = write_status_check(sd, count); |
| 280 | |
| 281 | /* if we failed, and we want to block, then loop */ |
| 282 | while (status <= 0) { |
| 283 | DECLARE_WAITQUEUE(wait, current); |
| 284 | |
| 285 | if (file->f_flags & O_NONBLOCK) { |
| 286 | spin_unlock(&sd->sd_wlock); |
| 287 | up(&sd->sd_wbs); |
| 288 | return -EAGAIN; |
| 289 | } |
| 290 | |
| 291 | set_current_state(TASK_INTERRUPTIBLE); |
| 292 | add_wait_queue(&sd->sd_wq, &wait); |
| 293 | spin_unlock_irqrestore(&sd->sd_wlock, flags); |
| 294 | |
| 295 | schedule_timeout(SCDRV_TIMEOUT); |
| 296 | |
| 297 | remove_wait_queue(&sd->sd_wq, &wait); |
| 298 | if (signal_pending(current)) { |
| 299 | /* wait was interrupted */ |
| 300 | up(&sd->sd_wbs); |
| 301 | return -ERESTARTSYS; |
| 302 | } |
| 303 | |
| 304 | spin_lock_irqsave(&sd->sd_wlock, flags); |
| 305 | status = write_status_check(sd, count); |
| 306 | } |
| 307 | spin_unlock_irqrestore(&sd->sd_wlock, flags); |
| 308 | |
| 309 | /* release the write buffer and wake anyone who's waiting for it */ |
| 310 | up(&sd->sd_wbs); |
| 311 | |
| 312 | /* return the number of characters accepted (should be the complete |
| 313 | * "chunk" as requested) |
| 314 | */ |
| 315 | if ((status >= 0) && (status < count)) { |
| 316 | pr_debug("Didn't accept the full chunk; %d of %d\n", |
| 317 | status, (int) count); |
| 318 | } |
| 319 | return status; |
| 320 | } |
| 321 | |
| 322 | static unsigned int |
| 323 | scdrv_poll(struct file *file, struct poll_table_struct *wait) |
| 324 | { |
| 325 | unsigned int mask = 0; |
| 326 | int status = 0; |
| 327 | struct subch_data_s *sd = (struct subch_data_s *) file->private_data; |
| 328 | unsigned long flags; |
| 329 | |
| 330 | poll_wait(file, &sd->sd_rq, wait); |
| 331 | poll_wait(file, &sd->sd_wq, wait); |
| 332 | |
| 333 | spin_lock_irqsave(&sd->sd_rlock, flags); |
| 334 | spin_lock(&sd->sd_wlock); |
| 335 | status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch); |
| 336 | spin_unlock(&sd->sd_wlock); |
| 337 | spin_unlock_irqrestore(&sd->sd_rlock, flags); |
| 338 | |
| 339 | if (status > 0) { |
| 340 | if (status & SAL_IROUTER_INTR_RECV) { |
| 341 | mask |= POLLIN | POLLRDNORM; |
| 342 | } |
| 343 | if (status & SAL_IROUTER_INTR_XMIT) { |
| 344 | mask |= POLLOUT | POLLWRNORM; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return mask; |
| 349 | } |
| 350 | |
| 351 | static struct file_operations scdrv_fops = { |
| 352 | .owner = THIS_MODULE, |
| 353 | .read = scdrv_read, |
| 354 | .write = scdrv_write, |
| 355 | .poll = scdrv_poll, |
| 356 | .open = scdrv_open, |
| 357 | .release = scdrv_release, |
| 358 | }; |
| 359 | |
| 360 | /* |
| 361 | * scdrv_init |
| 362 | * |
| 363 | * Called at boot time to initialize the system controller communication |
| 364 | * facility. |
| 365 | */ |
| 366 | int __init |
| 367 | scdrv_init(void) |
| 368 | { |
| 369 | geoid_t geoid; |
| 370 | cnodeid_t cnode; |
| 371 | char devname[32]; |
| 372 | char *devnamep; |
| 373 | struct sysctl_data_s *scd; |
| 374 | void *salbuf; |
| 375 | struct class_simple *snsc_class; |
| 376 | dev_t first_dev, dev; |
| 377 | |
| 378 | if (alloc_chrdev_region(&first_dev, 0, numionodes, |
| 379 | SYSCTL_BASENAME) < 0) { |
| 380 | printk("%s: failed to register SN system controller device\n", |
| 381 | __FUNCTION__); |
| 382 | return -ENODEV; |
| 383 | } |
| 384 | snsc_class = class_simple_create(THIS_MODULE, SYSCTL_BASENAME); |
| 385 | |
| 386 | for (cnode = 0; cnode < numionodes; cnode++) { |
| 387 | geoid = cnodeid_get_geoid(cnode); |
| 388 | devnamep = devname; |
| 389 | format_module_id(devnamep, geo_module(geoid), |
| 390 | MODULE_FORMAT_BRIEF); |
| 391 | devnamep = devname + strlen(devname); |
| 392 | sprintf(devnamep, "#%d", geo_slab(geoid)); |
| 393 | |
| 394 | /* allocate sysctl device data */ |
| 395 | scd = kmalloc(sizeof (struct sysctl_data_s), |
| 396 | GFP_KERNEL); |
| 397 | if (!scd) { |
| 398 | printk("%s: failed to allocate device info" |
| 399 | "for %s/%s\n", __FUNCTION__, |
| 400 | SYSCTL_BASENAME, devname); |
| 401 | continue; |
| 402 | } |
| 403 | memset(scd, 0, sizeof (struct sysctl_data_s)); |
| 404 | |
| 405 | /* initialize sysctl device data fields */ |
| 406 | scd->scd_nasid = cnodeid_to_nasid(cnode); |
| 407 | if (!(salbuf = kmalloc(SCDRV_BUFSZ, GFP_KERNEL))) { |
| 408 | printk("%s: failed to allocate driver buffer" |
| 409 | "(%s%s)\n", __FUNCTION__, |
| 410 | SYSCTL_BASENAME, devname); |
| 411 | kfree(scd); |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | if (ia64_sn_irtr_init(scd->scd_nasid, salbuf, |
| 416 | SCDRV_BUFSZ) < 0) { |
| 417 | printk |
| 418 | ("%s: failed to initialize SAL for" |
| 419 | " system controller communication" |
| 420 | " (%s/%s): outdated PROM?\n", |
| 421 | __FUNCTION__, SYSCTL_BASENAME, devname); |
| 422 | kfree(scd); |
| 423 | kfree(salbuf); |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | dev = first_dev + cnode; |
| 428 | cdev_init(&scd->scd_cdev, &scdrv_fops); |
| 429 | if (cdev_add(&scd->scd_cdev, dev, 1)) { |
| 430 | printk("%s: failed to register system" |
| 431 | " controller device (%s%s)\n", |
| 432 | __FUNCTION__, SYSCTL_BASENAME, devname); |
| 433 | kfree(scd); |
| 434 | kfree(salbuf); |
| 435 | continue; |
| 436 | } |
| 437 | |
| 438 | class_simple_device_add(snsc_class, dev, NULL, |
| 439 | "%s", devname); |
| 440 | |
| 441 | ia64_sn_irtr_intr_enable(scd->scd_nasid, |
| 442 | 0 /*ignored */ , |
| 443 | SAL_IROUTER_INTR_RECV); |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | module_init(scdrv_init); |