snsc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/signal.h>
  18. #include <linux/device.h>
  19. #include <linux/poll.h>
  20. #include <linux/init.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, 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. mutex_unlock(&scdrv_mutex);
  101. return -EBUSY;
  102. }
  103. mutex_unlock(&scdrv_mutex);
  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(msecs_to_jiffies(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_irqrestore(&sd->sd_wlock, flags);
  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(msecs_to_jiffies(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 __poll_t
  276. scdrv_poll(struct file *file, struct poll_table_struct *wait)
  277. {
  278. __poll_t 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 |= EPOLLIN | EPOLLRDNORM;
  292. }
  293. if (status & SAL_IROUTER_INTR_XMIT) {
  294. mask |= EPOLLOUT | EPOLLWRNORM;
  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. .llseek = noop_llseek,
  307. };
  308. static struct class *snsc_class;
  309. /*
  310. * scdrv_init
  311. *
  312. * Called at boot time to initialize the system controller communication
  313. * facility.
  314. */
  315. int __init
  316. scdrv_init(void)
  317. {
  318. geoid_t geoid;
  319. cnodeid_t cnode;
  320. char devname[32];
  321. char *devnamep;
  322. struct sysctl_data_s *scd;
  323. void *salbuf;
  324. dev_t first_dev, dev;
  325. nasid_t event_nasid;
  326. if (!ia64_platform_is("sn2"))
  327. return -ENODEV;
  328. event_nasid = ia64_sn_get_console_nasid();
  329. snsc_class = class_create(THIS_MODULE, SYSCTL_BASENAME);
  330. if (IS_ERR(snsc_class)) {
  331. printk("%s: failed to allocate class\n", __func__);
  332. return PTR_ERR(snsc_class);
  333. }
  334. if (alloc_chrdev_region(&first_dev, 0, num_cnodes,
  335. SYSCTL_BASENAME) < 0) {
  336. printk("%s: failed to register SN system controller device\n",
  337. __func__);
  338. return -ENODEV;
  339. }
  340. for (cnode = 0; cnode < num_cnodes; cnode++) {
  341. geoid = cnodeid_get_geoid(cnode);
  342. devnamep = devname;
  343. format_module_id(devnamep, geo_module(geoid),
  344. MODULE_FORMAT_BRIEF);
  345. devnamep = devname + strlen(devname);
  346. sprintf(devnamep, "^%d#%d", geo_slot(geoid),
  347. geo_slab(geoid));
  348. /* allocate sysctl device data */
  349. scd = kzalloc(sizeof (struct sysctl_data_s),
  350. GFP_KERNEL);
  351. if (!scd) {
  352. printk("%s: failed to allocate device info"
  353. "for %s/%s\n", __func__,
  354. SYSCTL_BASENAME, devname);
  355. continue;
  356. }
  357. /* initialize sysctl device data fields */
  358. scd->scd_nasid = cnodeid_to_nasid(cnode);
  359. if (!(salbuf = kmalloc(SCDRV_BUFSZ, GFP_KERNEL))) {
  360. printk("%s: failed to allocate driver buffer"
  361. "(%s%s)\n", __func__,
  362. SYSCTL_BASENAME, devname);
  363. kfree(scd);
  364. continue;
  365. }
  366. if (ia64_sn_irtr_init(scd->scd_nasid, salbuf,
  367. SCDRV_BUFSZ) < 0) {
  368. printk
  369. ("%s: failed to initialize SAL for"
  370. " system controller communication"
  371. " (%s/%s): outdated PROM?\n",
  372. __func__, SYSCTL_BASENAME, devname);
  373. kfree(scd);
  374. kfree(salbuf);
  375. continue;
  376. }
  377. dev = first_dev + cnode;
  378. cdev_init(&scd->scd_cdev, &scdrv_fops);
  379. if (cdev_add(&scd->scd_cdev, dev, 1)) {
  380. printk("%s: failed to register system"
  381. " controller device (%s%s)\n",
  382. __func__, SYSCTL_BASENAME, devname);
  383. kfree(scd);
  384. kfree(salbuf);
  385. continue;
  386. }
  387. device_create(snsc_class, NULL, dev, NULL,
  388. "%s", devname);
  389. ia64_sn_irtr_intr_enable(scd->scd_nasid,
  390. 0 /*ignored */ ,
  391. SAL_IROUTER_INTR_RECV);
  392. /* on the console nasid, prepare to receive
  393. * system controller environmental events
  394. */
  395. if(scd->scd_nasid == event_nasid) {
  396. scdrv_event_init(scd);
  397. }
  398. }
  399. return 0;
  400. }
  401. device_initcall(scdrv_init);