snsc.c 11 KB

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