snsc.c 11 KB

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