snsc.c 11 KB

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