snsc.c 11 KB

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